]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Resizable virtual memory filesystem for Linux. | |
3 | * | |
4 | * Copyright (C) 2000 Linus Torvalds. | |
5 | * 2000 Transmeta Corp. | |
6 | * 2000-2001 Christoph Rohland | |
7 | * 2000-2001 SAP AG | |
8 | * 2002 Red Hat Inc. | |
6922c0c7 HD |
9 | * Copyright (C) 2002-2011 Hugh Dickins. |
10 | * Copyright (C) 2011 Google Inc. | |
0edd73b3 | 11 | * Copyright (C) 2002-2005 VERITAS Software Corporation. |
1da177e4 LT |
12 | * Copyright (C) 2004 Andi Kleen, SuSE Labs |
13 | * | |
14 | * Extended attribute support for tmpfs: | |
15 | * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> | |
16 | * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> | |
17 | * | |
853ac43a MM |
18 | * tiny-shmem: |
19 | * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> | |
20 | * | |
1da177e4 LT |
21 | * This file is released under the GPL. |
22 | */ | |
23 | ||
853ac43a MM |
24 | #include <linux/fs.h> |
25 | #include <linux/init.h> | |
26 | #include <linux/vfs.h> | |
27 | #include <linux/mount.h> | |
250297ed | 28 | #include <linux/ramfs.h> |
caefba17 | 29 | #include <linux/pagemap.h> |
853ac43a MM |
30 | #include <linux/file.h> |
31 | #include <linux/mm.h> | |
46c9a946 | 32 | #include <linux/random.h> |
174cd4b1 | 33 | #include <linux/sched/signal.h> |
b95f1b31 | 34 | #include <linux/export.h> |
853ac43a | 35 | #include <linux/swap.h> |
e2e40f2c | 36 | #include <linux/uio.h> |
f3f0e1d2 | 37 | #include <linux/khugepaged.h> |
749df87b | 38 | #include <linux/hugetlb.h> |
853ac43a | 39 | |
95cc09d6 AA |
40 | #include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */ |
41 | ||
853ac43a MM |
42 | static struct vfsmount *shm_mnt; |
43 | ||
44 | #ifdef CONFIG_SHMEM | |
1da177e4 LT |
45 | /* |
46 | * This virtual memory filesystem is heavily based on the ramfs. It | |
47 | * extends ramfs by the ability to use swap and honor resource limits | |
48 | * which makes it a completely usable filesystem. | |
49 | */ | |
50 | ||
39f0247d | 51 | #include <linux/xattr.h> |
a5694255 | 52 | #include <linux/exportfs.h> |
1c7c474c | 53 | #include <linux/posix_acl.h> |
feda821e | 54 | #include <linux/posix_acl_xattr.h> |
1da177e4 | 55 | #include <linux/mman.h> |
1da177e4 LT |
56 | #include <linux/string.h> |
57 | #include <linux/slab.h> | |
58 | #include <linux/backing-dev.h> | |
59 | #include <linux/shmem_fs.h> | |
1da177e4 | 60 | #include <linux/writeback.h> |
1da177e4 | 61 | #include <linux/blkdev.h> |
bda97eab | 62 | #include <linux/pagevec.h> |
41ffe5d5 | 63 | #include <linux/percpu_counter.h> |
83e4fa9c | 64 | #include <linux/falloc.h> |
708e3508 | 65 | #include <linux/splice.h> |
1da177e4 LT |
66 | #include <linux/security.h> |
67 | #include <linux/swapops.h> | |
68 | #include <linux/mempolicy.h> | |
69 | #include <linux/namei.h> | |
b00dc3ad | 70 | #include <linux/ctype.h> |
304dbdb7 | 71 | #include <linux/migrate.h> |
c1f60a5a | 72 | #include <linux/highmem.h> |
680d794b | 73 | #include <linux/seq_file.h> |
92562927 | 74 | #include <linux/magic.h> |
9183df25 | 75 | #include <linux/syscalls.h> |
40e041a2 | 76 | #include <linux/fcntl.h> |
9183df25 | 77 | #include <uapi/linux/memfd.h> |
cfda0526 | 78 | #include <linux/userfaultfd_k.h> |
4c27fe4c | 79 | #include <linux/rmap.h> |
2b4db796 | 80 | #include <linux/uuid.h> |
304dbdb7 | 81 | |
7c0f6ba6 | 82 | #include <linux/uaccess.h> |
1da177e4 LT |
83 | #include <asm/pgtable.h> |
84 | ||
dd56b046 MG |
85 | #include "internal.h" |
86 | ||
09cbfeaf KS |
87 | #define BLOCKS_PER_PAGE (PAGE_SIZE/512) |
88 | #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) | |
1da177e4 | 89 | |
1da177e4 LT |
90 | /* Pretend that each entry is of this size in directory's i_size */ |
91 | #define BOGO_DIRENT_SIZE 20 | |
92 | ||
69f07ec9 HD |
93 | /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ |
94 | #define SHORT_SYMLINK_LEN 128 | |
95 | ||
1aac1400 | 96 | /* |
f00cdc6d HD |
97 | * shmem_fallocate communicates with shmem_fault or shmem_writepage via |
98 | * inode->i_private (with i_mutex making sure that it has only one user at | |
99 | * a time): we would prefer not to enlarge the shmem inode just for that. | |
1aac1400 HD |
100 | */ |
101 | struct shmem_falloc { | |
8e205f77 | 102 | wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ |
1aac1400 HD |
103 | pgoff_t start; /* start of range currently being fallocated */ |
104 | pgoff_t next; /* the next page offset to be fallocated */ | |
105 | pgoff_t nr_falloced; /* how many new pages have been fallocated */ | |
106 | pgoff_t nr_unswapped; /* how often writepage refused to swap out */ | |
107 | }; | |
108 | ||
b76db735 | 109 | #ifdef CONFIG_TMPFS |
680d794b AM |
110 | static unsigned long shmem_default_max_blocks(void) |
111 | { | |
112 | return totalram_pages / 2; | |
113 | } | |
114 | ||
115 | static unsigned long shmem_default_max_inodes(void) | |
116 | { | |
117 | return min(totalram_pages - totalhigh_pages, totalram_pages / 2); | |
118 | } | |
b76db735 | 119 | #endif |
680d794b | 120 | |
bde05d1c HD |
121 | static bool shmem_should_replace_page(struct page *page, gfp_t gfp); |
122 | static int shmem_replace_page(struct page **pagep, gfp_t gfp, | |
123 | struct shmem_inode_info *info, pgoff_t index); | |
68da9f05 | 124 | static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, |
9e18eb29 | 125 | struct page **pagep, enum sgp_type sgp, |
cfda0526 | 126 | gfp_t gfp, struct vm_area_struct *vma, |
2b740303 | 127 | struct vm_fault *vmf, vm_fault_t *fault_type); |
68da9f05 | 128 | |
f3f0e1d2 | 129 | int shmem_getpage(struct inode *inode, pgoff_t index, |
9e18eb29 | 130 | struct page **pagep, enum sgp_type sgp) |
68da9f05 HD |
131 | { |
132 | return shmem_getpage_gfp(inode, index, pagep, sgp, | |
cfda0526 | 133 | mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL); |
68da9f05 | 134 | } |
1da177e4 | 135 | |
1da177e4 LT |
136 | static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) |
137 | { | |
138 | return sb->s_fs_info; | |
139 | } | |
140 | ||
141 | /* | |
142 | * shmem_file_setup pre-accounts the whole fixed size of a VM object, | |
143 | * for shared memory and for shared anonymous (/dev/zero) mappings | |
144 | * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), | |
145 | * consistent with the pre-accounting of private mappings ... | |
146 | */ | |
147 | static inline int shmem_acct_size(unsigned long flags, loff_t size) | |
148 | { | |
0b0a0806 | 149 | return (flags & VM_NORESERVE) ? |
191c5424 | 150 | 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); |
1da177e4 LT |
151 | } |
152 | ||
153 | static inline void shmem_unacct_size(unsigned long flags, loff_t size) | |
154 | { | |
0b0a0806 | 155 | if (!(flags & VM_NORESERVE)) |
1da177e4 LT |
156 | vm_unacct_memory(VM_ACCT(size)); |
157 | } | |
158 | ||
77142517 KK |
159 | static inline int shmem_reacct_size(unsigned long flags, |
160 | loff_t oldsize, loff_t newsize) | |
161 | { | |
162 | if (!(flags & VM_NORESERVE)) { | |
163 | if (VM_ACCT(newsize) > VM_ACCT(oldsize)) | |
164 | return security_vm_enough_memory_mm(current->mm, | |
165 | VM_ACCT(newsize) - VM_ACCT(oldsize)); | |
166 | else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) | |
167 | vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); | |
168 | } | |
169 | return 0; | |
170 | } | |
171 | ||
1da177e4 LT |
172 | /* |
173 | * ... whereas tmpfs objects are accounted incrementally as | |
75edd345 | 174 | * pages are allocated, in order to allow large sparse files. |
1da177e4 LT |
175 | * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, |
176 | * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. | |
177 | */ | |
800d8c63 | 178 | static inline int shmem_acct_block(unsigned long flags, long pages) |
1da177e4 | 179 | { |
800d8c63 KS |
180 | if (!(flags & VM_NORESERVE)) |
181 | return 0; | |
182 | ||
183 | return security_vm_enough_memory_mm(current->mm, | |
184 | pages * VM_ACCT(PAGE_SIZE)); | |
1da177e4 LT |
185 | } |
186 | ||
187 | static inline void shmem_unacct_blocks(unsigned long flags, long pages) | |
188 | { | |
0b0a0806 | 189 | if (flags & VM_NORESERVE) |
09cbfeaf | 190 | vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); |
1da177e4 LT |
191 | } |
192 | ||
0f079694 MR |
193 | static inline bool shmem_inode_acct_block(struct inode *inode, long pages) |
194 | { | |
195 | struct shmem_inode_info *info = SHMEM_I(inode); | |
196 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); | |
197 | ||
198 | if (shmem_acct_block(info->flags, pages)) | |
199 | return false; | |
200 | ||
201 | if (sbinfo->max_blocks) { | |
202 | if (percpu_counter_compare(&sbinfo->used_blocks, | |
203 | sbinfo->max_blocks - pages) > 0) | |
204 | goto unacct; | |
205 | percpu_counter_add(&sbinfo->used_blocks, pages); | |
206 | } | |
207 | ||
208 | return true; | |
209 | ||
210 | unacct: | |
211 | shmem_unacct_blocks(info->flags, pages); | |
212 | return false; | |
213 | } | |
214 | ||
215 | static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) | |
216 | { | |
217 | struct shmem_inode_info *info = SHMEM_I(inode); | |
218 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); | |
219 | ||
220 | if (sbinfo->max_blocks) | |
221 | percpu_counter_sub(&sbinfo->used_blocks, pages); | |
222 | shmem_unacct_blocks(info->flags, pages); | |
223 | } | |
224 | ||
759b9775 | 225 | static const struct super_operations shmem_ops; |
f5e54d6e | 226 | static const struct address_space_operations shmem_aops; |
15ad7cdc | 227 | static const struct file_operations shmem_file_operations; |
92e1d5be AV |
228 | static const struct inode_operations shmem_inode_operations; |
229 | static const struct inode_operations shmem_dir_inode_operations; | |
230 | static const struct inode_operations shmem_special_inode_operations; | |
f0f37e2f | 231 | static const struct vm_operations_struct shmem_vm_ops; |
779750d2 | 232 | static struct file_system_type shmem_fs_type; |
1da177e4 | 233 | |
b0506e48 MR |
234 | bool vma_is_shmem(struct vm_area_struct *vma) |
235 | { | |
236 | return vma->vm_ops == &shmem_vm_ops; | |
237 | } | |
238 | ||
1da177e4 | 239 | static LIST_HEAD(shmem_swaplist); |
cb5f7b9a | 240 | static DEFINE_MUTEX(shmem_swaplist_mutex); |
1da177e4 | 241 | |
5b04c689 PE |
242 | static int shmem_reserve_inode(struct super_block *sb) |
243 | { | |
244 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
245 | if (sbinfo->max_inodes) { | |
246 | spin_lock(&sbinfo->stat_lock); | |
247 | if (!sbinfo->free_inodes) { | |
248 | spin_unlock(&sbinfo->stat_lock); | |
249 | return -ENOSPC; | |
250 | } | |
251 | sbinfo->free_inodes--; | |
252 | spin_unlock(&sbinfo->stat_lock); | |
253 | } | |
254 | return 0; | |
255 | } | |
256 | ||
257 | static void shmem_free_inode(struct super_block *sb) | |
258 | { | |
259 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
260 | if (sbinfo->max_inodes) { | |
261 | spin_lock(&sbinfo->stat_lock); | |
262 | sbinfo->free_inodes++; | |
263 | spin_unlock(&sbinfo->stat_lock); | |
264 | } | |
265 | } | |
266 | ||
46711810 | 267 | /** |
41ffe5d5 | 268 | * shmem_recalc_inode - recalculate the block usage of an inode |
1da177e4 LT |
269 | * @inode: inode to recalc |
270 | * | |
271 | * We have to calculate the free blocks since the mm can drop | |
272 | * undirtied hole pages behind our back. | |
273 | * | |
274 | * But normally info->alloced == inode->i_mapping->nrpages + info->swapped | |
275 | * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) | |
276 | * | |
277 | * It has to be called with the spinlock held. | |
278 | */ | |
279 | static void shmem_recalc_inode(struct inode *inode) | |
280 | { | |
281 | struct shmem_inode_info *info = SHMEM_I(inode); | |
282 | long freed; | |
283 | ||
284 | freed = info->alloced - info->swapped - inode->i_mapping->nrpages; | |
285 | if (freed > 0) { | |
286 | info->alloced -= freed; | |
54af6042 | 287 | inode->i_blocks -= freed * BLOCKS_PER_PAGE; |
0f079694 | 288 | shmem_inode_unacct_blocks(inode, freed); |
1da177e4 LT |
289 | } |
290 | } | |
291 | ||
800d8c63 KS |
292 | bool shmem_charge(struct inode *inode, long pages) |
293 | { | |
294 | struct shmem_inode_info *info = SHMEM_I(inode); | |
4595ef88 | 295 | unsigned long flags; |
800d8c63 | 296 | |
0f079694 | 297 | if (!shmem_inode_acct_block(inode, pages)) |
800d8c63 | 298 | return false; |
b1cc94ab | 299 | |
4595ef88 | 300 | spin_lock_irqsave(&info->lock, flags); |
800d8c63 KS |
301 | info->alloced += pages; |
302 | inode->i_blocks += pages * BLOCKS_PER_PAGE; | |
303 | shmem_recalc_inode(inode); | |
4595ef88 | 304 | spin_unlock_irqrestore(&info->lock, flags); |
800d8c63 KS |
305 | inode->i_mapping->nrpages += pages; |
306 | ||
800d8c63 KS |
307 | return true; |
308 | } | |
309 | ||
310 | void shmem_uncharge(struct inode *inode, long pages) | |
311 | { | |
312 | struct shmem_inode_info *info = SHMEM_I(inode); | |
4595ef88 | 313 | unsigned long flags; |
800d8c63 | 314 | |
4595ef88 | 315 | spin_lock_irqsave(&info->lock, flags); |
800d8c63 KS |
316 | info->alloced -= pages; |
317 | inode->i_blocks -= pages * BLOCKS_PER_PAGE; | |
318 | shmem_recalc_inode(inode); | |
4595ef88 | 319 | spin_unlock_irqrestore(&info->lock, flags); |
800d8c63 | 320 | |
0f079694 | 321 | shmem_inode_unacct_blocks(inode, pages); |
800d8c63 KS |
322 | } |
323 | ||
7a5d0fbb | 324 | /* |
62f945b6 | 325 | * Replace item expected in xarray by a new item, while holding xa_lock. |
7a5d0fbb | 326 | */ |
62f945b6 | 327 | static int shmem_replace_entry(struct address_space *mapping, |
7a5d0fbb HD |
328 | pgoff_t index, void *expected, void *replacement) |
329 | { | |
62f945b6 | 330 | XA_STATE(xas, &mapping->i_pages, index); |
6dbaf22c | 331 | void *item; |
7a5d0fbb HD |
332 | |
333 | VM_BUG_ON(!expected); | |
6dbaf22c | 334 | VM_BUG_ON(!replacement); |
62f945b6 | 335 | item = xas_load(&xas); |
7a5d0fbb HD |
336 | if (item != expected) |
337 | return -ENOENT; | |
62f945b6 | 338 | xas_store(&xas, replacement); |
7a5d0fbb HD |
339 | return 0; |
340 | } | |
341 | ||
d1899228 HD |
342 | /* |
343 | * Sometimes, before we decide whether to proceed or to fail, we must check | |
344 | * that an entry was not already brought back from swap by a racing thread. | |
345 | * | |
346 | * Checking page is not enough: by the time a SwapCache page is locked, it | |
347 | * might be reused, and again be SwapCache, using the same swap as before. | |
348 | */ | |
349 | static bool shmem_confirm_swap(struct address_space *mapping, | |
350 | pgoff_t index, swp_entry_t swap) | |
351 | { | |
352 | void *item; | |
353 | ||
354 | rcu_read_lock(); | |
b93b0163 | 355 | item = radix_tree_lookup(&mapping->i_pages, index); |
d1899228 HD |
356 | rcu_read_unlock(); |
357 | return item == swp_to_radix_entry(swap); | |
358 | } | |
359 | ||
5a6e75f8 KS |
360 | /* |
361 | * Definitions for "huge tmpfs": tmpfs mounted with the huge= option | |
362 | * | |
363 | * SHMEM_HUGE_NEVER: | |
364 | * disables huge pages for the mount; | |
365 | * SHMEM_HUGE_ALWAYS: | |
366 | * enables huge pages for the mount; | |
367 | * SHMEM_HUGE_WITHIN_SIZE: | |
368 | * only allocate huge pages if the page will be fully within i_size, | |
369 | * also respect fadvise()/madvise() hints; | |
370 | * SHMEM_HUGE_ADVISE: | |
371 | * only allocate huge pages if requested with fadvise()/madvise(); | |
372 | */ | |
373 | ||
374 | #define SHMEM_HUGE_NEVER 0 | |
375 | #define SHMEM_HUGE_ALWAYS 1 | |
376 | #define SHMEM_HUGE_WITHIN_SIZE 2 | |
377 | #define SHMEM_HUGE_ADVISE 3 | |
378 | ||
379 | /* | |
380 | * Special values. | |
381 | * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: | |
382 | * | |
383 | * SHMEM_HUGE_DENY: | |
384 | * disables huge on shm_mnt and all mounts, for emergency use; | |
385 | * SHMEM_HUGE_FORCE: | |
386 | * enables huge on shm_mnt and all mounts, w/o needing option, for testing; | |
387 | * | |
388 | */ | |
389 | #define SHMEM_HUGE_DENY (-1) | |
390 | #define SHMEM_HUGE_FORCE (-2) | |
391 | ||
e496cf3d | 392 | #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE |
5a6e75f8 KS |
393 | /* ifdef here to avoid bloating shmem.o when not necessary */ |
394 | ||
5b9c98f3 | 395 | static int shmem_huge __read_mostly; |
5a6e75f8 | 396 | |
f1f5929c | 397 | #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) |
5a6e75f8 KS |
398 | static int shmem_parse_huge(const char *str) |
399 | { | |
400 | if (!strcmp(str, "never")) | |
401 | return SHMEM_HUGE_NEVER; | |
402 | if (!strcmp(str, "always")) | |
403 | return SHMEM_HUGE_ALWAYS; | |
404 | if (!strcmp(str, "within_size")) | |
405 | return SHMEM_HUGE_WITHIN_SIZE; | |
406 | if (!strcmp(str, "advise")) | |
407 | return SHMEM_HUGE_ADVISE; | |
408 | if (!strcmp(str, "deny")) | |
409 | return SHMEM_HUGE_DENY; | |
410 | if (!strcmp(str, "force")) | |
411 | return SHMEM_HUGE_FORCE; | |
412 | return -EINVAL; | |
413 | } | |
414 | ||
415 | static const char *shmem_format_huge(int huge) | |
416 | { | |
417 | switch (huge) { | |
418 | case SHMEM_HUGE_NEVER: | |
419 | return "never"; | |
420 | case SHMEM_HUGE_ALWAYS: | |
421 | return "always"; | |
422 | case SHMEM_HUGE_WITHIN_SIZE: | |
423 | return "within_size"; | |
424 | case SHMEM_HUGE_ADVISE: | |
425 | return "advise"; | |
426 | case SHMEM_HUGE_DENY: | |
427 | return "deny"; | |
428 | case SHMEM_HUGE_FORCE: | |
429 | return "force"; | |
430 | default: | |
431 | VM_BUG_ON(1); | |
432 | return "bad_val"; | |
433 | } | |
434 | } | |
f1f5929c | 435 | #endif |
5a6e75f8 | 436 | |
779750d2 KS |
437 | static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, |
438 | struct shrink_control *sc, unsigned long nr_to_split) | |
439 | { | |
440 | LIST_HEAD(list), *pos, *next; | |
253fd0f0 | 441 | LIST_HEAD(to_remove); |
779750d2 KS |
442 | struct inode *inode; |
443 | struct shmem_inode_info *info; | |
444 | struct page *page; | |
445 | unsigned long batch = sc ? sc->nr_to_scan : 128; | |
446 | int removed = 0, split = 0; | |
447 | ||
448 | if (list_empty(&sbinfo->shrinklist)) | |
449 | return SHRINK_STOP; | |
450 | ||
451 | spin_lock(&sbinfo->shrinklist_lock); | |
452 | list_for_each_safe(pos, next, &sbinfo->shrinklist) { | |
453 | info = list_entry(pos, struct shmem_inode_info, shrinklist); | |
454 | ||
455 | /* pin the inode */ | |
456 | inode = igrab(&info->vfs_inode); | |
457 | ||
458 | /* inode is about to be evicted */ | |
459 | if (!inode) { | |
460 | list_del_init(&info->shrinklist); | |
461 | removed++; | |
462 | goto next; | |
463 | } | |
464 | ||
465 | /* Check if there's anything to gain */ | |
466 | if (round_up(inode->i_size, PAGE_SIZE) == | |
467 | round_up(inode->i_size, HPAGE_PMD_SIZE)) { | |
253fd0f0 | 468 | list_move(&info->shrinklist, &to_remove); |
779750d2 | 469 | removed++; |
779750d2 KS |
470 | goto next; |
471 | } | |
472 | ||
473 | list_move(&info->shrinklist, &list); | |
474 | next: | |
475 | if (!--batch) | |
476 | break; | |
477 | } | |
478 | spin_unlock(&sbinfo->shrinklist_lock); | |
479 | ||
253fd0f0 KS |
480 | list_for_each_safe(pos, next, &to_remove) { |
481 | info = list_entry(pos, struct shmem_inode_info, shrinklist); | |
482 | inode = &info->vfs_inode; | |
483 | list_del_init(&info->shrinklist); | |
484 | iput(inode); | |
485 | } | |
486 | ||
779750d2 KS |
487 | list_for_each_safe(pos, next, &list) { |
488 | int ret; | |
489 | ||
490 | info = list_entry(pos, struct shmem_inode_info, shrinklist); | |
491 | inode = &info->vfs_inode; | |
492 | ||
b3cd54b2 KS |
493 | if (nr_to_split && split >= nr_to_split) |
494 | goto leave; | |
779750d2 | 495 | |
b3cd54b2 | 496 | page = find_get_page(inode->i_mapping, |
779750d2 KS |
497 | (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT); |
498 | if (!page) | |
499 | goto drop; | |
500 | ||
b3cd54b2 | 501 | /* No huge page at the end of the file: nothing to split */ |
779750d2 | 502 | if (!PageTransHuge(page)) { |
779750d2 KS |
503 | put_page(page); |
504 | goto drop; | |
505 | } | |
506 | ||
b3cd54b2 KS |
507 | /* |
508 | * Leave the inode on the list if we failed to lock | |
509 | * the page at this time. | |
510 | * | |
511 | * Waiting for the lock may lead to deadlock in the | |
512 | * reclaim path. | |
513 | */ | |
514 | if (!trylock_page(page)) { | |
515 | put_page(page); | |
516 | goto leave; | |
517 | } | |
518 | ||
779750d2 KS |
519 | ret = split_huge_page(page); |
520 | unlock_page(page); | |
521 | put_page(page); | |
522 | ||
b3cd54b2 KS |
523 | /* If split failed leave the inode on the list */ |
524 | if (ret) | |
525 | goto leave; | |
779750d2 KS |
526 | |
527 | split++; | |
528 | drop: | |
529 | list_del_init(&info->shrinklist); | |
530 | removed++; | |
b3cd54b2 | 531 | leave: |
779750d2 KS |
532 | iput(inode); |
533 | } | |
534 | ||
535 | spin_lock(&sbinfo->shrinklist_lock); | |
536 | list_splice_tail(&list, &sbinfo->shrinklist); | |
537 | sbinfo->shrinklist_len -= removed; | |
538 | spin_unlock(&sbinfo->shrinklist_lock); | |
539 | ||
540 | return split; | |
541 | } | |
542 | ||
543 | static long shmem_unused_huge_scan(struct super_block *sb, | |
544 | struct shrink_control *sc) | |
545 | { | |
546 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
547 | ||
548 | if (!READ_ONCE(sbinfo->shrinklist_len)) | |
549 | return SHRINK_STOP; | |
550 | ||
551 | return shmem_unused_huge_shrink(sbinfo, sc, 0); | |
552 | } | |
553 | ||
554 | static long shmem_unused_huge_count(struct super_block *sb, | |
555 | struct shrink_control *sc) | |
556 | { | |
557 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
558 | return READ_ONCE(sbinfo->shrinklist_len); | |
559 | } | |
e496cf3d | 560 | #else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */ |
5a6e75f8 KS |
561 | |
562 | #define shmem_huge SHMEM_HUGE_DENY | |
563 | ||
779750d2 KS |
564 | static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, |
565 | struct shrink_control *sc, unsigned long nr_to_split) | |
566 | { | |
567 | return 0; | |
568 | } | |
e496cf3d | 569 | #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */ |
5a6e75f8 | 570 | |
89fdcd26 YS |
571 | static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo) |
572 | { | |
573 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && | |
574 | (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) && | |
575 | shmem_huge != SHMEM_HUGE_DENY) | |
576 | return true; | |
577 | return false; | |
578 | } | |
579 | ||
46f65ec1 HD |
580 | /* |
581 | * Like add_to_page_cache_locked, but error if expected item has gone. | |
582 | */ | |
583 | static int shmem_add_to_page_cache(struct page *page, | |
584 | struct address_space *mapping, | |
fed400a1 | 585 | pgoff_t index, void *expected) |
46f65ec1 | 586 | { |
800d8c63 | 587 | int error, nr = hpage_nr_pages(page); |
46f65ec1 | 588 | |
800d8c63 KS |
589 | VM_BUG_ON_PAGE(PageTail(page), page); |
590 | VM_BUG_ON_PAGE(index != round_down(index, nr), page); | |
309381fe SL |
591 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
592 | VM_BUG_ON_PAGE(!PageSwapBacked(page), page); | |
800d8c63 | 593 | VM_BUG_ON(expected && PageTransHuge(page)); |
46f65ec1 | 594 | |
800d8c63 | 595 | page_ref_add(page, nr); |
b065b432 HD |
596 | page->mapping = mapping; |
597 | page->index = index; | |
598 | ||
b93b0163 | 599 | xa_lock_irq(&mapping->i_pages); |
800d8c63 KS |
600 | if (PageTransHuge(page)) { |
601 | void __rcu **results; | |
602 | pgoff_t idx; | |
603 | int i; | |
604 | ||
605 | error = 0; | |
b93b0163 | 606 | if (radix_tree_gang_lookup_slot(&mapping->i_pages, |
800d8c63 KS |
607 | &results, &idx, index, 1) && |
608 | idx < index + HPAGE_PMD_NR) { | |
609 | error = -EEXIST; | |
610 | } | |
611 | ||
612 | if (!error) { | |
613 | for (i = 0; i < HPAGE_PMD_NR; i++) { | |
b93b0163 | 614 | error = radix_tree_insert(&mapping->i_pages, |
800d8c63 KS |
615 | index + i, page + i); |
616 | VM_BUG_ON(error); | |
617 | } | |
618 | count_vm_event(THP_FILE_ALLOC); | |
619 | } | |
620 | } else if (!expected) { | |
b93b0163 | 621 | error = radix_tree_insert(&mapping->i_pages, index, page); |
800d8c63 | 622 | } else { |
62f945b6 | 623 | error = shmem_replace_entry(mapping, index, expected, page); |
800d8c63 KS |
624 | } |
625 | ||
46f65ec1 | 626 | if (!error) { |
800d8c63 KS |
627 | mapping->nrpages += nr; |
628 | if (PageTransHuge(page)) | |
11fb9989 MG |
629 | __inc_node_page_state(page, NR_SHMEM_THPS); |
630 | __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr); | |
631 | __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr); | |
b93b0163 | 632 | xa_unlock_irq(&mapping->i_pages); |
b065b432 HD |
633 | } else { |
634 | page->mapping = NULL; | |
b93b0163 | 635 | xa_unlock_irq(&mapping->i_pages); |
800d8c63 | 636 | page_ref_sub(page, nr); |
46f65ec1 | 637 | } |
46f65ec1 HD |
638 | return error; |
639 | } | |
640 | ||
6922c0c7 HD |
641 | /* |
642 | * Like delete_from_page_cache, but substitutes swap for page. | |
643 | */ | |
644 | static void shmem_delete_from_page_cache(struct page *page, void *radswap) | |
645 | { | |
646 | struct address_space *mapping = page->mapping; | |
647 | int error; | |
648 | ||
800d8c63 KS |
649 | VM_BUG_ON_PAGE(PageCompound(page), page); |
650 | ||
b93b0163 | 651 | xa_lock_irq(&mapping->i_pages); |
62f945b6 | 652 | error = shmem_replace_entry(mapping, page->index, page, radswap); |
6922c0c7 HD |
653 | page->mapping = NULL; |
654 | mapping->nrpages--; | |
11fb9989 MG |
655 | __dec_node_page_state(page, NR_FILE_PAGES); |
656 | __dec_node_page_state(page, NR_SHMEM); | |
b93b0163 | 657 | xa_unlock_irq(&mapping->i_pages); |
09cbfeaf | 658 | put_page(page); |
6922c0c7 HD |
659 | BUG_ON(error); |
660 | } | |
661 | ||
7a5d0fbb HD |
662 | /* |
663 | * Remove swap entry from radix tree, free the swap and its page cache. | |
664 | */ | |
665 | static int shmem_free_swap(struct address_space *mapping, | |
666 | pgoff_t index, void *radswap) | |
667 | { | |
6dbaf22c | 668 | void *old; |
7a5d0fbb | 669 | |
b93b0163 MW |
670 | xa_lock_irq(&mapping->i_pages); |
671 | old = radix_tree_delete_item(&mapping->i_pages, index, radswap); | |
672 | xa_unlock_irq(&mapping->i_pages); | |
6dbaf22c JW |
673 | if (old != radswap) |
674 | return -ENOENT; | |
675 | free_swap_and_cache(radix_to_swp_entry(radswap)); | |
676 | return 0; | |
7a5d0fbb HD |
677 | } |
678 | ||
6a15a370 VB |
679 | /* |
680 | * Determine (in bytes) how many of the shmem object's pages mapped by the | |
48131e03 | 681 | * given offsets are swapped out. |
6a15a370 | 682 | * |
b93b0163 | 683 | * This is safe to call without i_mutex or the i_pages lock thanks to RCU, |
6a15a370 VB |
684 | * as long as the inode doesn't go away and racy results are not a problem. |
685 | */ | |
48131e03 VB |
686 | unsigned long shmem_partial_swap_usage(struct address_space *mapping, |
687 | pgoff_t start, pgoff_t end) | |
6a15a370 | 688 | { |
6a15a370 | 689 | struct radix_tree_iter iter; |
5b9c98f3 | 690 | void __rcu **slot; |
6a15a370 | 691 | struct page *page; |
48131e03 | 692 | unsigned long swapped = 0; |
6a15a370 VB |
693 | |
694 | rcu_read_lock(); | |
695 | ||
b93b0163 | 696 | radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) { |
6a15a370 VB |
697 | if (iter.index >= end) |
698 | break; | |
699 | ||
700 | page = radix_tree_deref_slot(slot); | |
701 | ||
2cf938aa MW |
702 | if (radix_tree_deref_retry(page)) { |
703 | slot = radix_tree_iter_retry(&iter); | |
704 | continue; | |
705 | } | |
6a15a370 | 706 | |
3159f943 | 707 | if (xa_is_value(page)) |
6a15a370 VB |
708 | swapped++; |
709 | ||
710 | if (need_resched()) { | |
148deab2 | 711 | slot = radix_tree_iter_resume(slot, &iter); |
6a15a370 | 712 | cond_resched_rcu(); |
6a15a370 VB |
713 | } |
714 | } | |
715 | ||
716 | rcu_read_unlock(); | |
717 | ||
718 | return swapped << PAGE_SHIFT; | |
719 | } | |
720 | ||
48131e03 VB |
721 | /* |
722 | * Determine (in bytes) how many of the shmem object's pages mapped by the | |
723 | * given vma is swapped out. | |
724 | * | |
b93b0163 | 725 | * This is safe to call without i_mutex or the i_pages lock thanks to RCU, |
48131e03 VB |
726 | * as long as the inode doesn't go away and racy results are not a problem. |
727 | */ | |
728 | unsigned long shmem_swap_usage(struct vm_area_struct *vma) | |
729 | { | |
730 | struct inode *inode = file_inode(vma->vm_file); | |
731 | struct shmem_inode_info *info = SHMEM_I(inode); | |
732 | struct address_space *mapping = inode->i_mapping; | |
733 | unsigned long swapped; | |
734 | ||
735 | /* Be careful as we don't hold info->lock */ | |
736 | swapped = READ_ONCE(info->swapped); | |
737 | ||
738 | /* | |
739 | * The easier cases are when the shmem object has nothing in swap, or | |
740 | * the vma maps it whole. Then we can simply use the stats that we | |
741 | * already track. | |
742 | */ | |
743 | if (!swapped) | |
744 | return 0; | |
745 | ||
746 | if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) | |
747 | return swapped << PAGE_SHIFT; | |
748 | ||
749 | /* Here comes the more involved part */ | |
750 | return shmem_partial_swap_usage(mapping, | |
751 | linear_page_index(vma, vma->vm_start), | |
752 | linear_page_index(vma, vma->vm_end)); | |
753 | } | |
754 | ||
24513264 HD |
755 | /* |
756 | * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. | |
757 | */ | |
758 | void shmem_unlock_mapping(struct address_space *mapping) | |
759 | { | |
760 | struct pagevec pvec; | |
761 | pgoff_t indices[PAGEVEC_SIZE]; | |
762 | pgoff_t index = 0; | |
763 | ||
86679820 | 764 | pagevec_init(&pvec); |
24513264 HD |
765 | /* |
766 | * Minor point, but we might as well stop if someone else SHM_LOCKs it. | |
767 | */ | |
768 | while (!mapping_unevictable(mapping)) { | |
769 | /* | |
770 | * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it | |
771 | * has finished, if it hits a row of PAGEVEC_SIZE swap entries. | |
772 | */ | |
0cd6144a JW |
773 | pvec.nr = find_get_entries(mapping, index, |
774 | PAGEVEC_SIZE, pvec.pages, indices); | |
24513264 HD |
775 | if (!pvec.nr) |
776 | break; | |
777 | index = indices[pvec.nr - 1] + 1; | |
0cd6144a | 778 | pagevec_remove_exceptionals(&pvec); |
24513264 HD |
779 | check_move_unevictable_pages(pvec.pages, pvec.nr); |
780 | pagevec_release(&pvec); | |
781 | cond_resched(); | |
782 | } | |
7a5d0fbb HD |
783 | } |
784 | ||
785 | /* | |
786 | * Remove range of pages and swap entries from radix tree, and free them. | |
1635f6a7 | 787 | * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. |
7a5d0fbb | 788 | */ |
1635f6a7 HD |
789 | static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, |
790 | bool unfalloc) | |
1da177e4 | 791 | { |
285b2c4f | 792 | struct address_space *mapping = inode->i_mapping; |
1da177e4 | 793 | struct shmem_inode_info *info = SHMEM_I(inode); |
09cbfeaf KS |
794 | pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; |
795 | pgoff_t end = (lend + 1) >> PAGE_SHIFT; | |
796 | unsigned int partial_start = lstart & (PAGE_SIZE - 1); | |
797 | unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1); | |
bda97eab | 798 | struct pagevec pvec; |
7a5d0fbb HD |
799 | pgoff_t indices[PAGEVEC_SIZE]; |
800 | long nr_swaps_freed = 0; | |
285b2c4f | 801 | pgoff_t index; |
bda97eab HD |
802 | int i; |
803 | ||
83e4fa9c HD |
804 | if (lend == -1) |
805 | end = -1; /* unsigned, so actually very big */ | |
bda97eab | 806 | |
86679820 | 807 | pagevec_init(&pvec); |
bda97eab | 808 | index = start; |
83e4fa9c | 809 | while (index < end) { |
0cd6144a JW |
810 | pvec.nr = find_get_entries(mapping, index, |
811 | min(end - index, (pgoff_t)PAGEVEC_SIZE), | |
812 | pvec.pages, indices); | |
7a5d0fbb HD |
813 | if (!pvec.nr) |
814 | break; | |
bda97eab HD |
815 | for (i = 0; i < pagevec_count(&pvec); i++) { |
816 | struct page *page = pvec.pages[i]; | |
817 | ||
7a5d0fbb | 818 | index = indices[i]; |
83e4fa9c | 819 | if (index >= end) |
bda97eab HD |
820 | break; |
821 | ||
3159f943 | 822 | if (xa_is_value(page)) { |
1635f6a7 HD |
823 | if (unfalloc) |
824 | continue; | |
7a5d0fbb HD |
825 | nr_swaps_freed += !shmem_free_swap(mapping, |
826 | index, page); | |
bda97eab | 827 | continue; |
7a5d0fbb HD |
828 | } |
829 | ||
800d8c63 KS |
830 | VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page); |
831 | ||
7a5d0fbb | 832 | if (!trylock_page(page)) |
bda97eab | 833 | continue; |
800d8c63 KS |
834 | |
835 | if (PageTransTail(page)) { | |
836 | /* Middle of THP: zero out the page */ | |
837 | clear_highpage(page); | |
838 | unlock_page(page); | |
839 | continue; | |
840 | } else if (PageTransHuge(page)) { | |
841 | if (index == round_down(end, HPAGE_PMD_NR)) { | |
842 | /* | |
843 | * Range ends in the middle of THP: | |
844 | * zero out the page | |
845 | */ | |
846 | clear_highpage(page); | |
847 | unlock_page(page); | |
848 | continue; | |
849 | } | |
850 | index += HPAGE_PMD_NR - 1; | |
851 | i += HPAGE_PMD_NR - 1; | |
852 | } | |
853 | ||
1635f6a7 | 854 | if (!unfalloc || !PageUptodate(page)) { |
800d8c63 KS |
855 | VM_BUG_ON_PAGE(PageTail(page), page); |
856 | if (page_mapping(page) == mapping) { | |
309381fe | 857 | VM_BUG_ON_PAGE(PageWriteback(page), page); |
1635f6a7 HD |
858 | truncate_inode_page(mapping, page); |
859 | } | |
bda97eab | 860 | } |
bda97eab HD |
861 | unlock_page(page); |
862 | } | |
0cd6144a | 863 | pagevec_remove_exceptionals(&pvec); |
24513264 | 864 | pagevec_release(&pvec); |
bda97eab HD |
865 | cond_resched(); |
866 | index++; | |
867 | } | |
1da177e4 | 868 | |
83e4fa9c | 869 | if (partial_start) { |
bda97eab | 870 | struct page *page = NULL; |
9e18eb29 | 871 | shmem_getpage(inode, start - 1, &page, SGP_READ); |
bda97eab | 872 | if (page) { |
09cbfeaf | 873 | unsigned int top = PAGE_SIZE; |
83e4fa9c HD |
874 | if (start > end) { |
875 | top = partial_end; | |
876 | partial_end = 0; | |
877 | } | |
878 | zero_user_segment(page, partial_start, top); | |
879 | set_page_dirty(page); | |
880 | unlock_page(page); | |
09cbfeaf | 881 | put_page(page); |
83e4fa9c HD |
882 | } |
883 | } | |
884 | if (partial_end) { | |
885 | struct page *page = NULL; | |
9e18eb29 | 886 | shmem_getpage(inode, end, &page, SGP_READ); |
83e4fa9c HD |
887 | if (page) { |
888 | zero_user_segment(page, 0, partial_end); | |
bda97eab HD |
889 | set_page_dirty(page); |
890 | unlock_page(page); | |
09cbfeaf | 891 | put_page(page); |
bda97eab HD |
892 | } |
893 | } | |
83e4fa9c HD |
894 | if (start >= end) |
895 | return; | |
bda97eab HD |
896 | |
897 | index = start; | |
b1a36650 | 898 | while (index < end) { |
bda97eab | 899 | cond_resched(); |
0cd6144a JW |
900 | |
901 | pvec.nr = find_get_entries(mapping, index, | |
83e4fa9c | 902 | min(end - index, (pgoff_t)PAGEVEC_SIZE), |
0cd6144a | 903 | pvec.pages, indices); |
7a5d0fbb | 904 | if (!pvec.nr) { |
b1a36650 HD |
905 | /* If all gone or hole-punch or unfalloc, we're done */ |
906 | if (index == start || end != -1) | |
bda97eab | 907 | break; |
b1a36650 | 908 | /* But if truncating, restart to make sure all gone */ |
bda97eab HD |
909 | index = start; |
910 | continue; | |
911 | } | |
bda97eab HD |
912 | for (i = 0; i < pagevec_count(&pvec); i++) { |
913 | struct page *page = pvec.pages[i]; | |
914 | ||
7a5d0fbb | 915 | index = indices[i]; |
83e4fa9c | 916 | if (index >= end) |
bda97eab HD |
917 | break; |
918 | ||
3159f943 | 919 | if (xa_is_value(page)) { |
1635f6a7 HD |
920 | if (unfalloc) |
921 | continue; | |
b1a36650 HD |
922 | if (shmem_free_swap(mapping, index, page)) { |
923 | /* Swap was replaced by page: retry */ | |
924 | index--; | |
925 | break; | |
926 | } | |
927 | nr_swaps_freed++; | |
7a5d0fbb HD |
928 | continue; |
929 | } | |
930 | ||
bda97eab | 931 | lock_page(page); |
800d8c63 KS |
932 | |
933 | if (PageTransTail(page)) { | |
934 | /* Middle of THP: zero out the page */ | |
935 | clear_highpage(page); | |
936 | unlock_page(page); | |
937 | /* | |
938 | * Partial thp truncate due 'start' in middle | |
939 | * of THP: don't need to look on these pages | |
940 | * again on !pvec.nr restart. | |
941 | */ | |
942 | if (index != round_down(end, HPAGE_PMD_NR)) | |
943 | start++; | |
944 | continue; | |
945 | } else if (PageTransHuge(page)) { | |
946 | if (index == round_down(end, HPAGE_PMD_NR)) { | |
947 | /* | |
948 | * Range ends in the middle of THP: | |
949 | * zero out the page | |
950 | */ | |
951 | clear_highpage(page); | |
952 | unlock_page(page); | |
953 | continue; | |
954 | } | |
955 | index += HPAGE_PMD_NR - 1; | |
956 | i += HPAGE_PMD_NR - 1; | |
957 | } | |
958 | ||
1635f6a7 | 959 | if (!unfalloc || !PageUptodate(page)) { |
800d8c63 KS |
960 | VM_BUG_ON_PAGE(PageTail(page), page); |
961 | if (page_mapping(page) == mapping) { | |
309381fe | 962 | VM_BUG_ON_PAGE(PageWriteback(page), page); |
1635f6a7 | 963 | truncate_inode_page(mapping, page); |
b1a36650 HD |
964 | } else { |
965 | /* Page was replaced by swap: retry */ | |
966 | unlock_page(page); | |
967 | index--; | |
968 | break; | |
1635f6a7 | 969 | } |
7a5d0fbb | 970 | } |
bda97eab HD |
971 | unlock_page(page); |
972 | } | |
0cd6144a | 973 | pagevec_remove_exceptionals(&pvec); |
24513264 | 974 | pagevec_release(&pvec); |
bda97eab HD |
975 | index++; |
976 | } | |
94c1e62d | 977 | |
4595ef88 | 978 | spin_lock_irq(&info->lock); |
7a5d0fbb | 979 | info->swapped -= nr_swaps_freed; |
1da177e4 | 980 | shmem_recalc_inode(inode); |
4595ef88 | 981 | spin_unlock_irq(&info->lock); |
1635f6a7 | 982 | } |
1da177e4 | 983 | |
1635f6a7 HD |
984 | void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) |
985 | { | |
986 | shmem_undo_range(inode, lstart, lend, false); | |
078cd827 | 987 | inode->i_ctime = inode->i_mtime = current_time(inode); |
1da177e4 | 988 | } |
94c1e62d | 989 | EXPORT_SYMBOL_GPL(shmem_truncate_range); |
1da177e4 | 990 | |
a528d35e DH |
991 | static int shmem_getattr(const struct path *path, struct kstat *stat, |
992 | u32 request_mask, unsigned int query_flags) | |
44a30220 | 993 | { |
a528d35e | 994 | struct inode *inode = path->dentry->d_inode; |
44a30220 | 995 | struct shmem_inode_info *info = SHMEM_I(inode); |
89fdcd26 | 996 | struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb); |
44a30220 | 997 | |
d0424c42 | 998 | if (info->alloced - info->swapped != inode->i_mapping->nrpages) { |
4595ef88 | 999 | spin_lock_irq(&info->lock); |
d0424c42 | 1000 | shmem_recalc_inode(inode); |
4595ef88 | 1001 | spin_unlock_irq(&info->lock); |
d0424c42 | 1002 | } |
44a30220 | 1003 | generic_fillattr(inode, stat); |
89fdcd26 YS |
1004 | |
1005 | if (is_huge_enabled(sb_info)) | |
1006 | stat->blksize = HPAGE_PMD_SIZE; | |
1007 | ||
44a30220 YZ |
1008 | return 0; |
1009 | } | |
1010 | ||
94c1e62d | 1011 | static int shmem_setattr(struct dentry *dentry, struct iattr *attr) |
1da177e4 | 1012 | { |
75c3cfa8 | 1013 | struct inode *inode = d_inode(dentry); |
40e041a2 | 1014 | struct shmem_inode_info *info = SHMEM_I(inode); |
779750d2 | 1015 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
1da177e4 LT |
1016 | int error; |
1017 | ||
31051c85 | 1018 | error = setattr_prepare(dentry, attr); |
db78b877 CH |
1019 | if (error) |
1020 | return error; | |
1021 | ||
94c1e62d HD |
1022 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
1023 | loff_t oldsize = inode->i_size; | |
1024 | loff_t newsize = attr->ia_size; | |
3889e6e7 | 1025 | |
40e041a2 DR |
1026 | /* protected by i_mutex */ |
1027 | if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || | |
1028 | (newsize > oldsize && (info->seals & F_SEAL_GROW))) | |
1029 | return -EPERM; | |
1030 | ||
94c1e62d | 1031 | if (newsize != oldsize) { |
77142517 KK |
1032 | error = shmem_reacct_size(SHMEM_I(inode)->flags, |
1033 | oldsize, newsize); | |
1034 | if (error) | |
1035 | return error; | |
94c1e62d | 1036 | i_size_write(inode, newsize); |
078cd827 | 1037 | inode->i_ctime = inode->i_mtime = current_time(inode); |
94c1e62d | 1038 | } |
afa2db2f | 1039 | if (newsize <= oldsize) { |
94c1e62d | 1040 | loff_t holebegin = round_up(newsize, PAGE_SIZE); |
d0424c42 HD |
1041 | if (oldsize > holebegin) |
1042 | unmap_mapping_range(inode->i_mapping, | |
1043 | holebegin, 0, 1); | |
1044 | if (info->alloced) | |
1045 | shmem_truncate_range(inode, | |
1046 | newsize, (loff_t)-1); | |
94c1e62d | 1047 | /* unmap again to remove racily COWed private pages */ |
d0424c42 HD |
1048 | if (oldsize > holebegin) |
1049 | unmap_mapping_range(inode->i_mapping, | |
1050 | holebegin, 0, 1); | |
779750d2 KS |
1051 | |
1052 | /* | |
1053 | * Part of the huge page can be beyond i_size: subject | |
1054 | * to shrink under memory pressure. | |
1055 | */ | |
1056 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { | |
1057 | spin_lock(&sbinfo->shrinklist_lock); | |
d041353d CW |
1058 | /* |
1059 | * _careful to defend against unlocked access to | |
1060 | * ->shrink_list in shmem_unused_huge_shrink() | |
1061 | */ | |
1062 | if (list_empty_careful(&info->shrinklist)) { | |
779750d2 KS |
1063 | list_add_tail(&info->shrinklist, |
1064 | &sbinfo->shrinklist); | |
1065 | sbinfo->shrinklist_len++; | |
1066 | } | |
1067 | spin_unlock(&sbinfo->shrinklist_lock); | |
1068 | } | |
94c1e62d | 1069 | } |
1da177e4 LT |
1070 | } |
1071 | ||
db78b877 | 1072 | setattr_copy(inode, attr); |
db78b877 | 1073 | if (attr->ia_valid & ATTR_MODE) |
feda821e | 1074 | error = posix_acl_chmod(inode, inode->i_mode); |
1da177e4 LT |
1075 | return error; |
1076 | } | |
1077 | ||
1f895f75 | 1078 | static void shmem_evict_inode(struct inode *inode) |
1da177e4 | 1079 | { |
1da177e4 | 1080 | struct shmem_inode_info *info = SHMEM_I(inode); |
779750d2 | 1081 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
1da177e4 | 1082 | |
3889e6e7 | 1083 | if (inode->i_mapping->a_ops == &shmem_aops) { |
1da177e4 LT |
1084 | shmem_unacct_size(info->flags, inode->i_size); |
1085 | inode->i_size = 0; | |
3889e6e7 | 1086 | shmem_truncate_range(inode, 0, (loff_t)-1); |
779750d2 KS |
1087 | if (!list_empty(&info->shrinklist)) { |
1088 | spin_lock(&sbinfo->shrinklist_lock); | |
1089 | if (!list_empty(&info->shrinklist)) { | |
1090 | list_del_init(&info->shrinklist); | |
1091 | sbinfo->shrinklist_len--; | |
1092 | } | |
1093 | spin_unlock(&sbinfo->shrinklist_lock); | |
1094 | } | |
1da177e4 | 1095 | if (!list_empty(&info->swaplist)) { |
cb5f7b9a | 1096 | mutex_lock(&shmem_swaplist_mutex); |
1da177e4 | 1097 | list_del_init(&info->swaplist); |
cb5f7b9a | 1098 | mutex_unlock(&shmem_swaplist_mutex); |
1da177e4 | 1099 | } |
3ed47db3 | 1100 | } |
b09e0fa4 | 1101 | |
38f38657 | 1102 | simple_xattrs_free(&info->xattrs); |
0f3c42f5 | 1103 | WARN_ON(inode->i_blocks); |
5b04c689 | 1104 | shmem_free_inode(inode->i_sb); |
dbd5768f | 1105 | clear_inode(inode); |
1da177e4 LT |
1106 | } |
1107 | ||
478922e2 MW |
1108 | static unsigned long find_swap_entry(struct radix_tree_root *root, void *item) |
1109 | { | |
1110 | struct radix_tree_iter iter; | |
5b9c98f3 | 1111 | void __rcu **slot; |
478922e2 MW |
1112 | unsigned long found = -1; |
1113 | unsigned int checked = 0; | |
1114 | ||
1115 | rcu_read_lock(); | |
1116 | radix_tree_for_each_slot(slot, root, &iter, 0) { | |
5b9c98f3 MK |
1117 | void *entry = radix_tree_deref_slot(slot); |
1118 | ||
1119 | if (radix_tree_deref_retry(entry)) { | |
1120 | slot = radix_tree_iter_retry(&iter); | |
1121 | continue; | |
1122 | } | |
1123 | if (entry == item) { | |
478922e2 MW |
1124 | found = iter.index; |
1125 | break; | |
1126 | } | |
1127 | checked++; | |
1128 | if ((checked % 4096) != 0) | |
1129 | continue; | |
1130 | slot = radix_tree_iter_resume(slot, &iter); | |
1131 | cond_resched_rcu(); | |
1132 | } | |
1133 | ||
1134 | rcu_read_unlock(); | |
1135 | return found; | |
1136 | } | |
1137 | ||
46f65ec1 HD |
1138 | /* |
1139 | * If swap found in inode, free it and move page from swapcache to filecache. | |
1140 | */ | |
41ffe5d5 | 1141 | static int shmem_unuse_inode(struct shmem_inode_info *info, |
bde05d1c | 1142 | swp_entry_t swap, struct page **pagep) |
1da177e4 | 1143 | { |
285b2c4f | 1144 | struct address_space *mapping = info->vfs_inode.i_mapping; |
46f65ec1 | 1145 | void *radswap; |
41ffe5d5 | 1146 | pgoff_t index; |
bde05d1c HD |
1147 | gfp_t gfp; |
1148 | int error = 0; | |
1da177e4 | 1149 | |
46f65ec1 | 1150 | radswap = swp_to_radix_entry(swap); |
b93b0163 | 1151 | index = find_swap_entry(&mapping->i_pages, radswap); |
46f65ec1 | 1152 | if (index == -1) |
00501b53 | 1153 | return -EAGAIN; /* tell shmem_unuse we found nothing */ |
2e0e26c7 | 1154 | |
1b1b32f2 HD |
1155 | /* |
1156 | * Move _head_ to start search for next from here. | |
1f895f75 | 1157 | * But be careful: shmem_evict_inode checks list_empty without taking |
1b1b32f2 | 1158 | * mutex, and there's an instant in list_move_tail when info->swaplist |
285b2c4f | 1159 | * would appear empty, if it were the only one on shmem_swaplist. |
1b1b32f2 HD |
1160 | */ |
1161 | if (shmem_swaplist.next != &info->swaplist) | |
1162 | list_move_tail(&shmem_swaplist, &info->swaplist); | |
2e0e26c7 | 1163 | |
bde05d1c HD |
1164 | gfp = mapping_gfp_mask(mapping); |
1165 | if (shmem_should_replace_page(*pagep, gfp)) { | |
1166 | mutex_unlock(&shmem_swaplist_mutex); | |
1167 | error = shmem_replace_page(pagep, gfp, info, index); | |
1168 | mutex_lock(&shmem_swaplist_mutex); | |
1169 | /* | |
1170 | * We needed to drop mutex to make that restrictive page | |
0142ef6c HD |
1171 | * allocation, but the inode might have been freed while we |
1172 | * dropped it: although a racing shmem_evict_inode() cannot | |
1173 | * complete without emptying the radix_tree, our page lock | |
1174 | * on this swapcache page is not enough to prevent that - | |
1175 | * free_swap_and_cache() of our swap entry will only | |
1176 | * trylock_page(), removing swap from radix_tree whatever. | |
1177 | * | |
1178 | * We must not proceed to shmem_add_to_page_cache() if the | |
1179 | * inode has been freed, but of course we cannot rely on | |
1180 | * inode or mapping or info to check that. However, we can | |
1181 | * safely check if our swap entry is still in use (and here | |
1182 | * it can't have got reused for another page): if it's still | |
1183 | * in use, then the inode cannot have been freed yet, and we | |
1184 | * can safely proceed (if it's no longer in use, that tells | |
1185 | * nothing about the inode, but we don't need to unuse swap). | |
bde05d1c HD |
1186 | */ |
1187 | if (!page_swapcount(*pagep)) | |
1188 | error = -ENOENT; | |
1189 | } | |
1190 | ||
d13d1443 | 1191 | /* |
778dd893 HD |
1192 | * We rely on shmem_swaplist_mutex, not only to protect the swaplist, |
1193 | * but also to hold up shmem_evict_inode(): so inode cannot be freed | |
1194 | * beneath us (pagelock doesn't help until the page is in pagecache). | |
d13d1443 | 1195 | */ |
bde05d1c HD |
1196 | if (!error) |
1197 | error = shmem_add_to_page_cache(*pagep, mapping, index, | |
fed400a1 | 1198 | radswap); |
48f170fb | 1199 | if (error != -ENOMEM) { |
46f65ec1 HD |
1200 | /* |
1201 | * Truncation and eviction use free_swap_and_cache(), which | |
1202 | * only does trylock page: if we raced, best clean up here. | |
1203 | */ | |
bde05d1c HD |
1204 | delete_from_swap_cache(*pagep); |
1205 | set_page_dirty(*pagep); | |
46f65ec1 | 1206 | if (!error) { |
4595ef88 | 1207 | spin_lock_irq(&info->lock); |
46f65ec1 | 1208 | info->swapped--; |
4595ef88 | 1209 | spin_unlock_irq(&info->lock); |
46f65ec1 HD |
1210 | swap_free(swap); |
1211 | } | |
1da177e4 | 1212 | } |
2e0e26c7 | 1213 | return error; |
1da177e4 LT |
1214 | } |
1215 | ||
1216 | /* | |
46f65ec1 | 1217 | * Search through swapped inodes to find and replace swap by page. |
1da177e4 | 1218 | */ |
41ffe5d5 | 1219 | int shmem_unuse(swp_entry_t swap, struct page *page) |
1da177e4 | 1220 | { |
41ffe5d5 | 1221 | struct list_head *this, *next; |
1da177e4 | 1222 | struct shmem_inode_info *info; |
00501b53 | 1223 | struct mem_cgroup *memcg; |
bde05d1c HD |
1224 | int error = 0; |
1225 | ||
1226 | /* | |
1227 | * There's a faint possibility that swap page was replaced before | |
0142ef6c | 1228 | * caller locked it: caller will come back later with the right page. |
bde05d1c | 1229 | */ |
0142ef6c | 1230 | if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val)) |
bde05d1c | 1231 | goto out; |
778dd893 HD |
1232 | |
1233 | /* | |
1234 | * Charge page using GFP_KERNEL while we can wait, before taking | |
1235 | * the shmem_swaplist_mutex which might hold up shmem_writepage(). | |
1236 | * Charged back to the user (not to caller) when swap account is used. | |
778dd893 | 1237 | */ |
2cf85583 TH |
1238 | error = mem_cgroup_try_charge_delay(page, current->mm, GFP_KERNEL, |
1239 | &memcg, false); | |
778dd893 HD |
1240 | if (error) |
1241 | goto out; | |
46f65ec1 | 1242 | /* No radix_tree_preload: swap entry keeps a place for page in tree */ |
00501b53 | 1243 | error = -EAGAIN; |
1da177e4 | 1244 | |
cb5f7b9a | 1245 | mutex_lock(&shmem_swaplist_mutex); |
41ffe5d5 HD |
1246 | list_for_each_safe(this, next, &shmem_swaplist) { |
1247 | info = list_entry(this, struct shmem_inode_info, swaplist); | |
285b2c4f | 1248 | if (info->swapped) |
00501b53 | 1249 | error = shmem_unuse_inode(info, swap, &page); |
6922c0c7 HD |
1250 | else |
1251 | list_del_init(&info->swaplist); | |
cb5f7b9a | 1252 | cond_resched(); |
00501b53 | 1253 | if (error != -EAGAIN) |
778dd893 | 1254 | break; |
00501b53 | 1255 | /* found nothing in this: move on to search the next */ |
1da177e4 | 1256 | } |
cb5f7b9a | 1257 | mutex_unlock(&shmem_swaplist_mutex); |
778dd893 | 1258 | |
00501b53 JW |
1259 | if (error) { |
1260 | if (error != -ENOMEM) | |
1261 | error = 0; | |
f627c2f5 | 1262 | mem_cgroup_cancel_charge(page, memcg, false); |
00501b53 | 1263 | } else |
f627c2f5 | 1264 | mem_cgroup_commit_charge(page, memcg, true, false); |
778dd893 | 1265 | out: |
aaa46865 | 1266 | unlock_page(page); |
09cbfeaf | 1267 | put_page(page); |
778dd893 | 1268 | return error; |
1da177e4 LT |
1269 | } |
1270 | ||
1271 | /* | |
1272 | * Move the page from the page cache to the swap cache. | |
1273 | */ | |
1274 | static int shmem_writepage(struct page *page, struct writeback_control *wbc) | |
1275 | { | |
1276 | struct shmem_inode_info *info; | |
1da177e4 | 1277 | struct address_space *mapping; |
1da177e4 | 1278 | struct inode *inode; |
6922c0c7 HD |
1279 | swp_entry_t swap; |
1280 | pgoff_t index; | |
1da177e4 | 1281 | |
800d8c63 | 1282 | VM_BUG_ON_PAGE(PageCompound(page), page); |
1da177e4 | 1283 | BUG_ON(!PageLocked(page)); |
1da177e4 LT |
1284 | mapping = page->mapping; |
1285 | index = page->index; | |
1286 | inode = mapping->host; | |
1287 | info = SHMEM_I(inode); | |
1288 | if (info->flags & VM_LOCKED) | |
1289 | goto redirty; | |
d9fe526a | 1290 | if (!total_swap_pages) |
1da177e4 LT |
1291 | goto redirty; |
1292 | ||
d9fe526a | 1293 | /* |
97b713ba CH |
1294 | * Our capabilities prevent regular writeback or sync from ever calling |
1295 | * shmem_writepage; but a stacking filesystem might use ->writepage of | |
1296 | * its underlying filesystem, in which case tmpfs should write out to | |
1297 | * swap only in response to memory pressure, and not for the writeback | |
1298 | * threads or sync. | |
d9fe526a | 1299 | */ |
48f170fb HD |
1300 | if (!wbc->for_reclaim) { |
1301 | WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ | |
1302 | goto redirty; | |
1303 | } | |
1635f6a7 HD |
1304 | |
1305 | /* | |
1306 | * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC | |
1307 | * value into swapfile.c, the only way we can correctly account for a | |
1308 | * fallocated page arriving here is now to initialize it and write it. | |
1aac1400 HD |
1309 | * |
1310 | * That's okay for a page already fallocated earlier, but if we have | |
1311 | * not yet completed the fallocation, then (a) we want to keep track | |
1312 | * of this page in case we have to undo it, and (b) it may not be a | |
1313 | * good idea to continue anyway, once we're pushing into swap. So | |
1314 | * reactivate the page, and let shmem_fallocate() quit when too many. | |
1635f6a7 HD |
1315 | */ |
1316 | if (!PageUptodate(page)) { | |
1aac1400 HD |
1317 | if (inode->i_private) { |
1318 | struct shmem_falloc *shmem_falloc; | |
1319 | spin_lock(&inode->i_lock); | |
1320 | shmem_falloc = inode->i_private; | |
1321 | if (shmem_falloc && | |
8e205f77 | 1322 | !shmem_falloc->waitq && |
1aac1400 HD |
1323 | index >= shmem_falloc->start && |
1324 | index < shmem_falloc->next) | |
1325 | shmem_falloc->nr_unswapped++; | |
1326 | else | |
1327 | shmem_falloc = NULL; | |
1328 | spin_unlock(&inode->i_lock); | |
1329 | if (shmem_falloc) | |
1330 | goto redirty; | |
1331 | } | |
1635f6a7 HD |
1332 | clear_highpage(page); |
1333 | flush_dcache_page(page); | |
1334 | SetPageUptodate(page); | |
1335 | } | |
1336 | ||
38d8b4e6 | 1337 | swap = get_swap_page(page); |
48f170fb HD |
1338 | if (!swap.val) |
1339 | goto redirty; | |
d9fe526a | 1340 | |
b1dea800 HD |
1341 | /* |
1342 | * Add inode to shmem_unuse()'s list of swapped-out inodes, | |
6922c0c7 HD |
1343 | * if it's not already there. Do it now before the page is |
1344 | * moved to swap cache, when its pagelock no longer protects | |
b1dea800 | 1345 | * the inode from eviction. But don't unlock the mutex until |
6922c0c7 HD |
1346 | * we've incremented swapped, because shmem_unuse_inode() will |
1347 | * prune a !swapped inode from the swaplist under this mutex. | |
b1dea800 | 1348 | */ |
48f170fb HD |
1349 | mutex_lock(&shmem_swaplist_mutex); |
1350 | if (list_empty(&info->swaplist)) | |
1351 | list_add_tail(&info->swaplist, &shmem_swaplist); | |
b1dea800 | 1352 | |
48f170fb | 1353 | if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { |
4595ef88 | 1354 | spin_lock_irq(&info->lock); |
6922c0c7 | 1355 | shmem_recalc_inode(inode); |
267a4c76 | 1356 | info->swapped++; |
4595ef88 | 1357 | spin_unlock_irq(&info->lock); |
6922c0c7 | 1358 | |
267a4c76 HD |
1359 | swap_shmem_alloc(swap); |
1360 | shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); | |
1361 | ||
6922c0c7 | 1362 | mutex_unlock(&shmem_swaplist_mutex); |
d9fe526a | 1363 | BUG_ON(page_mapped(page)); |
9fab5619 | 1364 | swap_writepage(page, wbc); |
1da177e4 LT |
1365 | return 0; |
1366 | } | |
1367 | ||
6922c0c7 | 1368 | mutex_unlock(&shmem_swaplist_mutex); |
75f6d6d2 | 1369 | put_swap_page(page, swap); |
1da177e4 LT |
1370 | redirty: |
1371 | set_page_dirty(page); | |
d9fe526a HD |
1372 | if (wbc->for_reclaim) |
1373 | return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ | |
1374 | unlock_page(page); | |
1375 | return 0; | |
1da177e4 LT |
1376 | } |
1377 | ||
75edd345 | 1378 | #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) |
71fe804b | 1379 | static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) |
680d794b | 1380 | { |
095f1fc4 | 1381 | char buffer[64]; |
680d794b | 1382 | |
71fe804b | 1383 | if (!mpol || mpol->mode == MPOL_DEFAULT) |
095f1fc4 | 1384 | return; /* show nothing */ |
680d794b | 1385 | |
a7a88b23 | 1386 | mpol_to_str(buffer, sizeof(buffer), mpol); |
095f1fc4 LS |
1387 | |
1388 | seq_printf(seq, ",mpol=%s", buffer); | |
680d794b | 1389 | } |
71fe804b LS |
1390 | |
1391 | static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) | |
1392 | { | |
1393 | struct mempolicy *mpol = NULL; | |
1394 | if (sbinfo->mpol) { | |
1395 | spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ | |
1396 | mpol = sbinfo->mpol; | |
1397 | mpol_get(mpol); | |
1398 | spin_unlock(&sbinfo->stat_lock); | |
1399 | } | |
1400 | return mpol; | |
1401 | } | |
75edd345 HD |
1402 | #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ |
1403 | static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) | |
1404 | { | |
1405 | } | |
1406 | static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) | |
1407 | { | |
1408 | return NULL; | |
1409 | } | |
1410 | #endif /* CONFIG_NUMA && CONFIG_TMPFS */ | |
1411 | #ifndef CONFIG_NUMA | |
1412 | #define vm_policy vm_private_data | |
1413 | #endif | |
680d794b | 1414 | |
800d8c63 KS |
1415 | static void shmem_pseudo_vma_init(struct vm_area_struct *vma, |
1416 | struct shmem_inode_info *info, pgoff_t index) | |
1417 | { | |
1418 | /* Create a pseudo vma that just contains the policy */ | |
2c4541e2 | 1419 | vma_init(vma, NULL); |
800d8c63 KS |
1420 | /* Bias interleave by inode number to distribute better across nodes */ |
1421 | vma->vm_pgoff = index + info->vfs_inode.i_ino; | |
800d8c63 KS |
1422 | vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index); |
1423 | } | |
1424 | ||
1425 | static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) | |
1426 | { | |
1427 | /* Drop reference taken by mpol_shared_policy_lookup() */ | |
1428 | mpol_cond_put(vma->vm_policy); | |
1429 | } | |
1430 | ||
41ffe5d5 HD |
1431 | static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, |
1432 | struct shmem_inode_info *info, pgoff_t index) | |
1da177e4 | 1433 | { |
1da177e4 | 1434 | struct vm_area_struct pvma; |
18a2f371 | 1435 | struct page *page; |
e9e9b7ec | 1436 | struct vm_fault vmf; |
52cd3b07 | 1437 | |
800d8c63 | 1438 | shmem_pseudo_vma_init(&pvma, info, index); |
e9e9b7ec MK |
1439 | vmf.vma = &pvma; |
1440 | vmf.address = 0; | |
1441 | page = swap_cluster_readahead(swap, gfp, &vmf); | |
800d8c63 | 1442 | shmem_pseudo_vma_destroy(&pvma); |
18a2f371 | 1443 | |
800d8c63 KS |
1444 | return page; |
1445 | } | |
1446 | ||
1447 | static struct page *shmem_alloc_hugepage(gfp_t gfp, | |
1448 | struct shmem_inode_info *info, pgoff_t index) | |
1449 | { | |
1450 | struct vm_area_struct pvma; | |
1451 | struct inode *inode = &info->vfs_inode; | |
1452 | struct address_space *mapping = inode->i_mapping; | |
4620a06e | 1453 | pgoff_t idx, hindex; |
800d8c63 KS |
1454 | void __rcu **results; |
1455 | struct page *page; | |
1456 | ||
e496cf3d | 1457 | if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) |
800d8c63 KS |
1458 | return NULL; |
1459 | ||
4620a06e | 1460 | hindex = round_down(index, HPAGE_PMD_NR); |
800d8c63 | 1461 | rcu_read_lock(); |
b93b0163 | 1462 | if (radix_tree_gang_lookup_slot(&mapping->i_pages, &results, &idx, |
800d8c63 KS |
1463 | hindex, 1) && idx < hindex + HPAGE_PMD_NR) { |
1464 | rcu_read_unlock(); | |
1465 | return NULL; | |
1466 | } | |
1467 | rcu_read_unlock(); | |
18a2f371 | 1468 | |
800d8c63 KS |
1469 | shmem_pseudo_vma_init(&pvma, info, hindex); |
1470 | page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN, | |
1471 | HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true); | |
1472 | shmem_pseudo_vma_destroy(&pvma); | |
1473 | if (page) | |
1474 | prep_transhuge_page(page); | |
18a2f371 | 1475 | return page; |
1da177e4 LT |
1476 | } |
1477 | ||
02098fea | 1478 | static struct page *shmem_alloc_page(gfp_t gfp, |
41ffe5d5 | 1479 | struct shmem_inode_info *info, pgoff_t index) |
1da177e4 LT |
1480 | { |
1481 | struct vm_area_struct pvma; | |
18a2f371 | 1482 | struct page *page; |
1da177e4 | 1483 | |
800d8c63 KS |
1484 | shmem_pseudo_vma_init(&pvma, info, index); |
1485 | page = alloc_page_vma(gfp, &pvma, 0); | |
1486 | shmem_pseudo_vma_destroy(&pvma); | |
1487 | ||
1488 | return page; | |
1489 | } | |
1490 | ||
1491 | static struct page *shmem_alloc_and_acct_page(gfp_t gfp, | |
0f079694 | 1492 | struct inode *inode, |
800d8c63 KS |
1493 | pgoff_t index, bool huge) |
1494 | { | |
0f079694 | 1495 | struct shmem_inode_info *info = SHMEM_I(inode); |
800d8c63 KS |
1496 | struct page *page; |
1497 | int nr; | |
1498 | int err = -ENOSPC; | |
52cd3b07 | 1499 | |
e496cf3d | 1500 | if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) |
800d8c63 KS |
1501 | huge = false; |
1502 | nr = huge ? HPAGE_PMD_NR : 1; | |
1503 | ||
0f079694 | 1504 | if (!shmem_inode_acct_block(inode, nr)) |
800d8c63 | 1505 | goto failed; |
800d8c63 KS |
1506 | |
1507 | if (huge) | |
1508 | page = shmem_alloc_hugepage(gfp, info, index); | |
1509 | else | |
1510 | page = shmem_alloc_page(gfp, info, index); | |
75edd345 HD |
1511 | if (page) { |
1512 | __SetPageLocked(page); | |
1513 | __SetPageSwapBacked(page); | |
800d8c63 | 1514 | return page; |
75edd345 | 1515 | } |
18a2f371 | 1516 | |
800d8c63 | 1517 | err = -ENOMEM; |
0f079694 | 1518 | shmem_inode_unacct_blocks(inode, nr); |
800d8c63 KS |
1519 | failed: |
1520 | return ERR_PTR(err); | |
1da177e4 | 1521 | } |
71fe804b | 1522 | |
bde05d1c HD |
1523 | /* |
1524 | * When a page is moved from swapcache to shmem filecache (either by the | |
1525 | * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of | |
1526 | * shmem_unuse_inode()), it may have been read in earlier from swap, in | |
1527 | * ignorance of the mapping it belongs to. If that mapping has special | |
1528 | * constraints (like the gma500 GEM driver, which requires RAM below 4GB), | |
1529 | * we may need to copy to a suitable page before moving to filecache. | |
1530 | * | |
1531 | * In a future release, this may well be extended to respect cpuset and | |
1532 | * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); | |
1533 | * but for now it is a simple matter of zone. | |
1534 | */ | |
1535 | static bool shmem_should_replace_page(struct page *page, gfp_t gfp) | |
1536 | { | |
1537 | return page_zonenum(page) > gfp_zone(gfp); | |
1538 | } | |
1539 | ||
1540 | static int shmem_replace_page(struct page **pagep, gfp_t gfp, | |
1541 | struct shmem_inode_info *info, pgoff_t index) | |
1542 | { | |
1543 | struct page *oldpage, *newpage; | |
1544 | struct address_space *swap_mapping; | |
1545 | pgoff_t swap_index; | |
1546 | int error; | |
1547 | ||
1548 | oldpage = *pagep; | |
1549 | swap_index = page_private(oldpage); | |
1550 | swap_mapping = page_mapping(oldpage); | |
1551 | ||
1552 | /* | |
1553 | * We have arrived here because our zones are constrained, so don't | |
1554 | * limit chance of success by further cpuset and node constraints. | |
1555 | */ | |
1556 | gfp &= ~GFP_CONSTRAINT_MASK; | |
1557 | newpage = shmem_alloc_page(gfp, info, index); | |
1558 | if (!newpage) | |
1559 | return -ENOMEM; | |
bde05d1c | 1560 | |
09cbfeaf | 1561 | get_page(newpage); |
bde05d1c | 1562 | copy_highpage(newpage, oldpage); |
0142ef6c | 1563 | flush_dcache_page(newpage); |
bde05d1c | 1564 | |
9956edf3 HD |
1565 | __SetPageLocked(newpage); |
1566 | __SetPageSwapBacked(newpage); | |
bde05d1c | 1567 | SetPageUptodate(newpage); |
bde05d1c | 1568 | set_page_private(newpage, swap_index); |
bde05d1c HD |
1569 | SetPageSwapCache(newpage); |
1570 | ||
1571 | /* | |
1572 | * Our caller will very soon move newpage out of swapcache, but it's | |
1573 | * a nice clean interface for us to replace oldpage by newpage there. | |
1574 | */ | |
b93b0163 | 1575 | xa_lock_irq(&swap_mapping->i_pages); |
62f945b6 | 1576 | error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage); |
0142ef6c | 1577 | if (!error) { |
11fb9989 MG |
1578 | __inc_node_page_state(newpage, NR_FILE_PAGES); |
1579 | __dec_node_page_state(oldpage, NR_FILE_PAGES); | |
0142ef6c | 1580 | } |
b93b0163 | 1581 | xa_unlock_irq(&swap_mapping->i_pages); |
bde05d1c | 1582 | |
0142ef6c HD |
1583 | if (unlikely(error)) { |
1584 | /* | |
1585 | * Is this possible? I think not, now that our callers check | |
1586 | * both PageSwapCache and page_private after getting page lock; | |
1587 | * but be defensive. Reverse old to newpage for clear and free. | |
1588 | */ | |
1589 | oldpage = newpage; | |
1590 | } else { | |
6a93ca8f | 1591 | mem_cgroup_migrate(oldpage, newpage); |
0142ef6c HD |
1592 | lru_cache_add_anon(newpage); |
1593 | *pagep = newpage; | |
1594 | } | |
bde05d1c HD |
1595 | |
1596 | ClearPageSwapCache(oldpage); | |
1597 | set_page_private(oldpage, 0); | |
1598 | ||
1599 | unlock_page(oldpage); | |
09cbfeaf KS |
1600 | put_page(oldpage); |
1601 | put_page(oldpage); | |
0142ef6c | 1602 | return error; |
bde05d1c HD |
1603 | } |
1604 | ||
1da177e4 | 1605 | /* |
68da9f05 | 1606 | * shmem_getpage_gfp - find page in cache, or get from swap, or allocate |
1da177e4 LT |
1607 | * |
1608 | * If we allocate a new one we do not mark it dirty. That's up to the | |
1609 | * vm. If we swap it in we mark it dirty since we also free the swap | |
9e18eb29 ALC |
1610 | * entry since a page cannot live in both the swap and page cache. |
1611 | * | |
1612 | * fault_mm and fault_type are only supplied by shmem_fault: | |
1613 | * otherwise they are NULL. | |
1da177e4 | 1614 | */ |
41ffe5d5 | 1615 | static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, |
9e18eb29 | 1616 | struct page **pagep, enum sgp_type sgp, gfp_t gfp, |
2b740303 SJ |
1617 | struct vm_area_struct *vma, struct vm_fault *vmf, |
1618 | vm_fault_t *fault_type) | |
1da177e4 LT |
1619 | { |
1620 | struct address_space *mapping = inode->i_mapping; | |
23f919d4 | 1621 | struct shmem_inode_info *info = SHMEM_I(inode); |
1da177e4 | 1622 | struct shmem_sb_info *sbinfo; |
9e18eb29 | 1623 | struct mm_struct *charge_mm; |
00501b53 | 1624 | struct mem_cgroup *memcg; |
27ab7006 | 1625 | struct page *page; |
1da177e4 | 1626 | swp_entry_t swap; |
657e3038 | 1627 | enum sgp_type sgp_huge = sgp; |
800d8c63 | 1628 | pgoff_t hindex = index; |
1da177e4 | 1629 | int error; |
54af6042 | 1630 | int once = 0; |
1635f6a7 | 1631 | int alloced = 0; |
1da177e4 | 1632 | |
09cbfeaf | 1633 | if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) |
1da177e4 | 1634 | return -EFBIG; |
657e3038 KS |
1635 | if (sgp == SGP_NOHUGE || sgp == SGP_HUGE) |
1636 | sgp = SGP_CACHE; | |
1da177e4 | 1637 | repeat: |
54af6042 | 1638 | swap.val = 0; |
0cd6144a | 1639 | page = find_lock_entry(mapping, index); |
3159f943 | 1640 | if (xa_is_value(page)) { |
54af6042 HD |
1641 | swap = radix_to_swp_entry(page); |
1642 | page = NULL; | |
1643 | } | |
1644 | ||
75edd345 | 1645 | if (sgp <= SGP_CACHE && |
09cbfeaf | 1646 | ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { |
54af6042 | 1647 | error = -EINVAL; |
267a4c76 | 1648 | goto unlock; |
54af6042 HD |
1649 | } |
1650 | ||
66d2f4d2 HD |
1651 | if (page && sgp == SGP_WRITE) |
1652 | mark_page_accessed(page); | |
1653 | ||
1635f6a7 HD |
1654 | /* fallocated page? */ |
1655 | if (page && !PageUptodate(page)) { | |
1656 | if (sgp != SGP_READ) | |
1657 | goto clear; | |
1658 | unlock_page(page); | |
09cbfeaf | 1659 | put_page(page); |
1635f6a7 HD |
1660 | page = NULL; |
1661 | } | |
54af6042 | 1662 | if (page || (sgp == SGP_READ && !swap.val)) { |
54af6042 HD |
1663 | *pagep = page; |
1664 | return 0; | |
27ab7006 HD |
1665 | } |
1666 | ||
1667 | /* | |
54af6042 HD |
1668 | * Fast cache lookup did not find it: |
1669 | * bring it back from swap or allocate. | |
27ab7006 | 1670 | */ |
54af6042 | 1671 | sbinfo = SHMEM_SB(inode->i_sb); |
cfda0526 | 1672 | charge_mm = vma ? vma->vm_mm : current->mm; |
1da177e4 | 1673 | |
1da177e4 LT |
1674 | if (swap.val) { |
1675 | /* Look it up and read it in.. */ | |
ec560175 | 1676 | page = lookup_swap_cache(swap, NULL, 0); |
27ab7006 | 1677 | if (!page) { |
9e18eb29 ALC |
1678 | /* Or update major stats only when swapin succeeds?? */ |
1679 | if (fault_type) { | |
68da9f05 | 1680 | *fault_type |= VM_FAULT_MAJOR; |
9e18eb29 | 1681 | count_vm_event(PGMAJFAULT); |
2262185c | 1682 | count_memcg_event_mm(charge_mm, PGMAJFAULT); |
9e18eb29 ALC |
1683 | } |
1684 | /* Here we actually start the io */ | |
41ffe5d5 | 1685 | page = shmem_swapin(swap, gfp, info, index); |
27ab7006 | 1686 | if (!page) { |
54af6042 HD |
1687 | error = -ENOMEM; |
1688 | goto failed; | |
1da177e4 | 1689 | } |
1da177e4 LT |
1690 | } |
1691 | ||
1692 | /* We have to do this with page locked to prevent races */ | |
54af6042 | 1693 | lock_page(page); |
0142ef6c | 1694 | if (!PageSwapCache(page) || page_private(page) != swap.val || |
d1899228 | 1695 | !shmem_confirm_swap(mapping, index, swap)) { |
bde05d1c | 1696 | error = -EEXIST; /* try again */ |
d1899228 | 1697 | goto unlock; |
bde05d1c | 1698 | } |
27ab7006 | 1699 | if (!PageUptodate(page)) { |
1da177e4 | 1700 | error = -EIO; |
54af6042 | 1701 | goto failed; |
1da177e4 | 1702 | } |
54af6042 HD |
1703 | wait_on_page_writeback(page); |
1704 | ||
bde05d1c HD |
1705 | if (shmem_should_replace_page(page, gfp)) { |
1706 | error = shmem_replace_page(&page, gfp, info, index); | |
1707 | if (error) | |
1708 | goto failed; | |
1da177e4 | 1709 | } |
27ab7006 | 1710 | |
2cf85583 | 1711 | error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg, |
f627c2f5 | 1712 | false); |
d1899228 | 1713 | if (!error) { |
aa3b1895 | 1714 | error = shmem_add_to_page_cache(page, mapping, index, |
fed400a1 | 1715 | swp_to_radix_entry(swap)); |
215c02bc HD |
1716 | /* |
1717 | * We already confirmed swap under page lock, and make | |
1718 | * no memory allocation here, so usually no possibility | |
1719 | * of error; but free_swap_and_cache() only trylocks a | |
1720 | * page, so it is just possible that the entry has been | |
1721 | * truncated or holepunched since swap was confirmed. | |
1722 | * shmem_undo_range() will have done some of the | |
1723 | * unaccounting, now delete_from_swap_cache() will do | |
93aa7d95 | 1724 | * the rest. |
215c02bc HD |
1725 | * Reset swap.val? No, leave it so "failed" goes back to |
1726 | * "repeat": reading a hole and writing should succeed. | |
1727 | */ | |
00501b53 | 1728 | if (error) { |
f627c2f5 | 1729 | mem_cgroup_cancel_charge(page, memcg, false); |
215c02bc | 1730 | delete_from_swap_cache(page); |
00501b53 | 1731 | } |
d1899228 | 1732 | } |
54af6042 HD |
1733 | if (error) |
1734 | goto failed; | |
1735 | ||
f627c2f5 | 1736 | mem_cgroup_commit_charge(page, memcg, true, false); |
00501b53 | 1737 | |
4595ef88 | 1738 | spin_lock_irq(&info->lock); |
285b2c4f | 1739 | info->swapped--; |
54af6042 | 1740 | shmem_recalc_inode(inode); |
4595ef88 | 1741 | spin_unlock_irq(&info->lock); |
54af6042 | 1742 | |
66d2f4d2 HD |
1743 | if (sgp == SGP_WRITE) |
1744 | mark_page_accessed(page); | |
1745 | ||
54af6042 | 1746 | delete_from_swap_cache(page); |
27ab7006 HD |
1747 | set_page_dirty(page); |
1748 | swap_free(swap); | |
1749 | ||
54af6042 | 1750 | } else { |
cfda0526 MR |
1751 | if (vma && userfaultfd_missing(vma)) { |
1752 | *fault_type = handle_userfault(vmf, VM_UFFD_MISSING); | |
1753 | return 0; | |
1754 | } | |
1755 | ||
800d8c63 KS |
1756 | /* shmem_symlink() */ |
1757 | if (mapping->a_ops != &shmem_aops) | |
1758 | goto alloc_nohuge; | |
657e3038 | 1759 | if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE) |
800d8c63 KS |
1760 | goto alloc_nohuge; |
1761 | if (shmem_huge == SHMEM_HUGE_FORCE) | |
1762 | goto alloc_huge; | |
1763 | switch (sbinfo->huge) { | |
1764 | loff_t i_size; | |
1765 | pgoff_t off; | |
1766 | case SHMEM_HUGE_NEVER: | |
1767 | goto alloc_nohuge; | |
1768 | case SHMEM_HUGE_WITHIN_SIZE: | |
1769 | off = round_up(index, HPAGE_PMD_NR); | |
1770 | i_size = round_up(i_size_read(inode), PAGE_SIZE); | |
1771 | if (i_size >= HPAGE_PMD_SIZE && | |
1772 | i_size >> PAGE_SHIFT >= off) | |
1773 | goto alloc_huge; | |
1774 | /* fallthrough */ | |
1775 | case SHMEM_HUGE_ADVISE: | |
657e3038 KS |
1776 | if (sgp_huge == SGP_HUGE) |
1777 | goto alloc_huge; | |
1778 | /* TODO: implement fadvise() hints */ | |
800d8c63 | 1779 | goto alloc_nohuge; |
54af6042 | 1780 | } |
1da177e4 | 1781 | |
800d8c63 | 1782 | alloc_huge: |
0f079694 | 1783 | page = shmem_alloc_and_acct_page(gfp, inode, index, true); |
800d8c63 | 1784 | if (IS_ERR(page)) { |
0f079694 | 1785 | alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode, |
800d8c63 | 1786 | index, false); |
1da177e4 | 1787 | } |
800d8c63 | 1788 | if (IS_ERR(page)) { |
779750d2 | 1789 | int retry = 5; |
800d8c63 KS |
1790 | error = PTR_ERR(page); |
1791 | page = NULL; | |
779750d2 KS |
1792 | if (error != -ENOSPC) |
1793 | goto failed; | |
1794 | /* | |
1795 | * Try to reclaim some spece by splitting a huge page | |
1796 | * beyond i_size on the filesystem. | |
1797 | */ | |
1798 | while (retry--) { | |
1799 | int ret; | |
1800 | ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); | |
1801 | if (ret == SHRINK_STOP) | |
1802 | break; | |
1803 | if (ret) | |
1804 | goto alloc_nohuge; | |
1805 | } | |
800d8c63 KS |
1806 | goto failed; |
1807 | } | |
1808 | ||
1809 | if (PageTransHuge(page)) | |
1810 | hindex = round_down(index, HPAGE_PMD_NR); | |
1811 | else | |
1812 | hindex = index; | |
1813 | ||
66d2f4d2 | 1814 | if (sgp == SGP_WRITE) |
eb39d618 | 1815 | __SetPageReferenced(page); |
66d2f4d2 | 1816 | |
2cf85583 | 1817 | error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg, |
800d8c63 | 1818 | PageTransHuge(page)); |
54af6042 | 1819 | if (error) |
800d8c63 KS |
1820 | goto unacct; |
1821 | error = radix_tree_maybe_preload_order(gfp & GFP_RECLAIM_MASK, | |
1822 | compound_order(page)); | |
b065b432 | 1823 | if (!error) { |
800d8c63 | 1824 | error = shmem_add_to_page_cache(page, mapping, hindex, |
fed400a1 | 1825 | NULL); |
b065b432 HD |
1826 | radix_tree_preload_end(); |
1827 | } | |
1828 | if (error) { | |
800d8c63 KS |
1829 | mem_cgroup_cancel_charge(page, memcg, |
1830 | PageTransHuge(page)); | |
1831 | goto unacct; | |
b065b432 | 1832 | } |
800d8c63 KS |
1833 | mem_cgroup_commit_charge(page, memcg, false, |
1834 | PageTransHuge(page)); | |
54af6042 HD |
1835 | lru_cache_add_anon(page); |
1836 | ||
4595ef88 | 1837 | spin_lock_irq(&info->lock); |
800d8c63 KS |
1838 | info->alloced += 1 << compound_order(page); |
1839 | inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page); | |
54af6042 | 1840 | shmem_recalc_inode(inode); |
4595ef88 | 1841 | spin_unlock_irq(&info->lock); |
1635f6a7 | 1842 | alloced = true; |
54af6042 | 1843 | |
779750d2 KS |
1844 | if (PageTransHuge(page) && |
1845 | DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < | |
1846 | hindex + HPAGE_PMD_NR - 1) { | |
1847 | /* | |
1848 | * Part of the huge page is beyond i_size: subject | |
1849 | * to shrink under memory pressure. | |
1850 | */ | |
1851 | spin_lock(&sbinfo->shrinklist_lock); | |
d041353d CW |
1852 | /* |
1853 | * _careful to defend against unlocked access to | |
1854 | * ->shrink_list in shmem_unused_huge_shrink() | |
1855 | */ | |
1856 | if (list_empty_careful(&info->shrinklist)) { | |
779750d2 KS |
1857 | list_add_tail(&info->shrinklist, |
1858 | &sbinfo->shrinklist); | |
1859 | sbinfo->shrinklist_len++; | |
1860 | } | |
1861 | spin_unlock(&sbinfo->shrinklist_lock); | |
1862 | } | |
1863 | ||
ec9516fb | 1864 | /* |
1635f6a7 HD |
1865 | * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. |
1866 | */ | |
1867 | if (sgp == SGP_FALLOC) | |
1868 | sgp = SGP_WRITE; | |
1869 | clear: | |
1870 | /* | |
1871 | * Let SGP_WRITE caller clear ends if write does not fill page; | |
1872 | * but SGP_FALLOC on a page fallocated earlier must initialize | |
1873 | * it now, lest undo on failure cancel our earlier guarantee. | |
ec9516fb | 1874 | */ |
800d8c63 KS |
1875 | if (sgp != SGP_WRITE && !PageUptodate(page)) { |
1876 | struct page *head = compound_head(page); | |
1877 | int i; | |
1878 | ||
1879 | for (i = 0; i < (1 << compound_order(head)); i++) { | |
1880 | clear_highpage(head + i); | |
1881 | flush_dcache_page(head + i); | |
1882 | } | |
1883 | SetPageUptodate(head); | |
ec9516fb | 1884 | } |
1da177e4 | 1885 | } |
bde05d1c | 1886 | |
54af6042 | 1887 | /* Perhaps the file has been truncated since we checked */ |
75edd345 | 1888 | if (sgp <= SGP_CACHE && |
09cbfeaf | 1889 | ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { |
267a4c76 HD |
1890 | if (alloced) { |
1891 | ClearPageDirty(page); | |
1892 | delete_from_page_cache(page); | |
4595ef88 | 1893 | spin_lock_irq(&info->lock); |
267a4c76 | 1894 | shmem_recalc_inode(inode); |
4595ef88 | 1895 | spin_unlock_irq(&info->lock); |
267a4c76 | 1896 | } |
54af6042 | 1897 | error = -EINVAL; |
267a4c76 | 1898 | goto unlock; |
e83c32e8 | 1899 | } |
800d8c63 | 1900 | *pagep = page + index - hindex; |
54af6042 | 1901 | return 0; |
1da177e4 | 1902 | |
59a16ead | 1903 | /* |
54af6042 | 1904 | * Error recovery. |
59a16ead | 1905 | */ |
54af6042 | 1906 | unacct: |
0f079694 | 1907 | shmem_inode_unacct_blocks(inode, 1 << compound_order(page)); |
800d8c63 KS |
1908 | |
1909 | if (PageTransHuge(page)) { | |
1910 | unlock_page(page); | |
1911 | put_page(page); | |
1912 | goto alloc_nohuge; | |
1913 | } | |
54af6042 | 1914 | failed: |
267a4c76 | 1915 | if (swap.val && !shmem_confirm_swap(mapping, index, swap)) |
d1899228 HD |
1916 | error = -EEXIST; |
1917 | unlock: | |
27ab7006 | 1918 | if (page) { |
54af6042 | 1919 | unlock_page(page); |
09cbfeaf | 1920 | put_page(page); |
54af6042 HD |
1921 | } |
1922 | if (error == -ENOSPC && !once++) { | |
4595ef88 | 1923 | spin_lock_irq(&info->lock); |
54af6042 | 1924 | shmem_recalc_inode(inode); |
4595ef88 | 1925 | spin_unlock_irq(&info->lock); |
27ab7006 | 1926 | goto repeat; |
ff36b801 | 1927 | } |
d1899228 | 1928 | if (error == -EEXIST) /* from above or from radix_tree_insert */ |
54af6042 HD |
1929 | goto repeat; |
1930 | return error; | |
1da177e4 LT |
1931 | } |
1932 | ||
10d20bd2 LT |
1933 | /* |
1934 | * This is like autoremove_wake_function, but it removes the wait queue | |
1935 | * entry unconditionally - even if something else had already woken the | |
1936 | * target. | |
1937 | */ | |
ac6424b9 | 1938 | static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) |
10d20bd2 LT |
1939 | { |
1940 | int ret = default_wake_function(wait, mode, sync, key); | |
2055da97 | 1941 | list_del_init(&wait->entry); |
10d20bd2 LT |
1942 | return ret; |
1943 | } | |
1944 | ||
20acce67 | 1945 | static vm_fault_t shmem_fault(struct vm_fault *vmf) |
1da177e4 | 1946 | { |
11bac800 | 1947 | struct vm_area_struct *vma = vmf->vma; |
496ad9aa | 1948 | struct inode *inode = file_inode(vma->vm_file); |
9e18eb29 | 1949 | gfp_t gfp = mapping_gfp_mask(inode->i_mapping); |
657e3038 | 1950 | enum sgp_type sgp; |
20acce67 SJ |
1951 | int err; |
1952 | vm_fault_t ret = VM_FAULT_LOCKED; | |
1da177e4 | 1953 | |
f00cdc6d HD |
1954 | /* |
1955 | * Trinity finds that probing a hole which tmpfs is punching can | |
1956 | * prevent the hole-punch from ever completing: which in turn | |
1957 | * locks writers out with its hold on i_mutex. So refrain from | |
8e205f77 HD |
1958 | * faulting pages into the hole while it's being punched. Although |
1959 | * shmem_undo_range() does remove the additions, it may be unable to | |
1960 | * keep up, as each new page needs its own unmap_mapping_range() call, | |
1961 | * and the i_mmap tree grows ever slower to scan if new vmas are added. | |
1962 | * | |
1963 | * It does not matter if we sometimes reach this check just before the | |
1964 | * hole-punch begins, so that one fault then races with the punch: | |
1965 | * we just need to make racing faults a rare case. | |
1966 | * | |
1967 | * The implementation below would be much simpler if we just used a | |
1968 | * standard mutex or completion: but we cannot take i_mutex in fault, | |
1969 | * and bloating every shmem inode for this unlikely case would be sad. | |
f00cdc6d HD |
1970 | */ |
1971 | if (unlikely(inode->i_private)) { | |
1972 | struct shmem_falloc *shmem_falloc; | |
1973 | ||
1974 | spin_lock(&inode->i_lock); | |
1975 | shmem_falloc = inode->i_private; | |
8e205f77 HD |
1976 | if (shmem_falloc && |
1977 | shmem_falloc->waitq && | |
1978 | vmf->pgoff >= shmem_falloc->start && | |
1979 | vmf->pgoff < shmem_falloc->next) { | |
1980 | wait_queue_head_t *shmem_falloc_waitq; | |
10d20bd2 | 1981 | DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function); |
8e205f77 HD |
1982 | |
1983 | ret = VM_FAULT_NOPAGE; | |
f00cdc6d HD |
1984 | if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) && |
1985 | !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { | |
8e205f77 | 1986 | /* It's polite to up mmap_sem if we can */ |
f00cdc6d | 1987 | up_read(&vma->vm_mm->mmap_sem); |
8e205f77 | 1988 | ret = VM_FAULT_RETRY; |
f00cdc6d | 1989 | } |
8e205f77 HD |
1990 | |
1991 | shmem_falloc_waitq = shmem_falloc->waitq; | |
1992 | prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, | |
1993 | TASK_UNINTERRUPTIBLE); | |
1994 | spin_unlock(&inode->i_lock); | |
1995 | schedule(); | |
1996 | ||
1997 | /* | |
1998 | * shmem_falloc_waitq points into the shmem_fallocate() | |
1999 | * stack of the hole-punching task: shmem_falloc_waitq | |
2000 | * is usually invalid by the time we reach here, but | |
2001 | * finish_wait() does not dereference it in that case; | |
2002 | * though i_lock needed lest racing with wake_up_all(). | |
2003 | */ | |
2004 | spin_lock(&inode->i_lock); | |
2005 | finish_wait(shmem_falloc_waitq, &shmem_fault_wait); | |
2006 | spin_unlock(&inode->i_lock); | |
2007 | return ret; | |
f00cdc6d | 2008 | } |
8e205f77 | 2009 | spin_unlock(&inode->i_lock); |
f00cdc6d HD |
2010 | } |
2011 | ||
657e3038 | 2012 | sgp = SGP_CACHE; |
18600332 MH |
2013 | |
2014 | if ((vma->vm_flags & VM_NOHUGEPAGE) || | |
2015 | test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) | |
657e3038 | 2016 | sgp = SGP_NOHUGE; |
18600332 MH |
2017 | else if (vma->vm_flags & VM_HUGEPAGE) |
2018 | sgp = SGP_HUGE; | |
657e3038 | 2019 | |
20acce67 | 2020 | err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp, |
cfda0526 | 2021 | gfp, vma, vmf, &ret); |
20acce67 SJ |
2022 | if (err) |
2023 | return vmf_error(err); | |
68da9f05 | 2024 | return ret; |
1da177e4 LT |
2025 | } |
2026 | ||
c01d5b30 HD |
2027 | unsigned long shmem_get_unmapped_area(struct file *file, |
2028 | unsigned long uaddr, unsigned long len, | |
2029 | unsigned long pgoff, unsigned long flags) | |
2030 | { | |
2031 | unsigned long (*get_area)(struct file *, | |
2032 | unsigned long, unsigned long, unsigned long, unsigned long); | |
2033 | unsigned long addr; | |
2034 | unsigned long offset; | |
2035 | unsigned long inflated_len; | |
2036 | unsigned long inflated_addr; | |
2037 | unsigned long inflated_offset; | |
2038 | ||
2039 | if (len > TASK_SIZE) | |
2040 | return -ENOMEM; | |
2041 | ||
2042 | get_area = current->mm->get_unmapped_area; | |
2043 | addr = get_area(file, uaddr, len, pgoff, flags); | |
2044 | ||
e496cf3d | 2045 | if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) |
c01d5b30 HD |
2046 | return addr; |
2047 | if (IS_ERR_VALUE(addr)) | |
2048 | return addr; | |
2049 | if (addr & ~PAGE_MASK) | |
2050 | return addr; | |
2051 | if (addr > TASK_SIZE - len) | |
2052 | return addr; | |
2053 | ||
2054 | if (shmem_huge == SHMEM_HUGE_DENY) | |
2055 | return addr; | |
2056 | if (len < HPAGE_PMD_SIZE) | |
2057 | return addr; | |
2058 | if (flags & MAP_FIXED) | |
2059 | return addr; | |
2060 | /* | |
2061 | * Our priority is to support MAP_SHARED mapped hugely; | |
2062 | * and support MAP_PRIVATE mapped hugely too, until it is COWed. | |
2063 | * But if caller specified an address hint, respect that as before. | |
2064 | */ | |
2065 | if (uaddr) | |
2066 | return addr; | |
2067 | ||
2068 | if (shmem_huge != SHMEM_HUGE_FORCE) { | |
2069 | struct super_block *sb; | |
2070 | ||
2071 | if (file) { | |
2072 | VM_BUG_ON(file->f_op != &shmem_file_operations); | |
2073 | sb = file_inode(file)->i_sb; | |
2074 | } else { | |
2075 | /* | |
2076 | * Called directly from mm/mmap.c, or drivers/char/mem.c | |
2077 | * for "/dev/zero", to create a shared anonymous object. | |
2078 | */ | |
2079 | if (IS_ERR(shm_mnt)) | |
2080 | return addr; | |
2081 | sb = shm_mnt->mnt_sb; | |
2082 | } | |
3089bf61 | 2083 | if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) |
c01d5b30 HD |
2084 | return addr; |
2085 | } | |
2086 | ||
2087 | offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); | |
2088 | if (offset && offset + len < 2 * HPAGE_PMD_SIZE) | |
2089 | return addr; | |
2090 | if ((addr & (HPAGE_PMD_SIZE-1)) == offset) | |
2091 | return addr; | |
2092 | ||
2093 | inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; | |
2094 | if (inflated_len > TASK_SIZE) | |
2095 | return addr; | |
2096 | if (inflated_len < len) | |
2097 | return addr; | |
2098 | ||
2099 | inflated_addr = get_area(NULL, 0, inflated_len, 0, flags); | |
2100 | if (IS_ERR_VALUE(inflated_addr)) | |
2101 | return addr; | |
2102 | if (inflated_addr & ~PAGE_MASK) | |
2103 | return addr; | |
2104 | ||
2105 | inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); | |
2106 | inflated_addr += offset - inflated_offset; | |
2107 | if (inflated_offset > offset) | |
2108 | inflated_addr += HPAGE_PMD_SIZE; | |
2109 | ||
2110 | if (inflated_addr > TASK_SIZE - len) | |
2111 | return addr; | |
2112 | return inflated_addr; | |
2113 | } | |
2114 | ||
1da177e4 | 2115 | #ifdef CONFIG_NUMA |
41ffe5d5 | 2116 | static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) |
1da177e4 | 2117 | { |
496ad9aa | 2118 | struct inode *inode = file_inode(vma->vm_file); |
41ffe5d5 | 2119 | return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); |
1da177e4 LT |
2120 | } |
2121 | ||
d8dc74f2 AB |
2122 | static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, |
2123 | unsigned long addr) | |
1da177e4 | 2124 | { |
496ad9aa | 2125 | struct inode *inode = file_inode(vma->vm_file); |
41ffe5d5 | 2126 | pgoff_t index; |
1da177e4 | 2127 | |
41ffe5d5 HD |
2128 | index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
2129 | return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); | |
1da177e4 LT |
2130 | } |
2131 | #endif | |
2132 | ||
2133 | int shmem_lock(struct file *file, int lock, struct user_struct *user) | |
2134 | { | |
496ad9aa | 2135 | struct inode *inode = file_inode(file); |
1da177e4 LT |
2136 | struct shmem_inode_info *info = SHMEM_I(inode); |
2137 | int retval = -ENOMEM; | |
2138 | ||
4595ef88 | 2139 | spin_lock_irq(&info->lock); |
1da177e4 LT |
2140 | if (lock && !(info->flags & VM_LOCKED)) { |
2141 | if (!user_shm_lock(inode->i_size, user)) | |
2142 | goto out_nomem; | |
2143 | info->flags |= VM_LOCKED; | |
89e004ea | 2144 | mapping_set_unevictable(file->f_mapping); |
1da177e4 LT |
2145 | } |
2146 | if (!lock && (info->flags & VM_LOCKED) && user) { | |
2147 | user_shm_unlock(inode->i_size, user); | |
2148 | info->flags &= ~VM_LOCKED; | |
89e004ea | 2149 | mapping_clear_unevictable(file->f_mapping); |
1da177e4 LT |
2150 | } |
2151 | retval = 0; | |
89e004ea | 2152 | |
1da177e4 | 2153 | out_nomem: |
4595ef88 | 2154 | spin_unlock_irq(&info->lock); |
1da177e4 LT |
2155 | return retval; |
2156 | } | |
2157 | ||
9b83a6a8 | 2158 | static int shmem_mmap(struct file *file, struct vm_area_struct *vma) |
1da177e4 LT |
2159 | { |
2160 | file_accessed(file); | |
2161 | vma->vm_ops = &shmem_vm_ops; | |
e496cf3d | 2162 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && |
f3f0e1d2 KS |
2163 | ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < |
2164 | (vma->vm_end & HPAGE_PMD_MASK)) { | |
2165 | khugepaged_enter(vma, vma->vm_flags); | |
2166 | } | |
1da177e4 LT |
2167 | return 0; |
2168 | } | |
2169 | ||
454abafe | 2170 | static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, |
09208d15 | 2171 | umode_t mode, dev_t dev, unsigned long flags) |
1da177e4 LT |
2172 | { |
2173 | struct inode *inode; | |
2174 | struct shmem_inode_info *info; | |
2175 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
2176 | ||
5b04c689 PE |
2177 | if (shmem_reserve_inode(sb)) |
2178 | return NULL; | |
1da177e4 LT |
2179 | |
2180 | inode = new_inode(sb); | |
2181 | if (inode) { | |
85fe4025 | 2182 | inode->i_ino = get_next_ino(); |
454abafe | 2183 | inode_init_owner(inode, dir, mode); |
1da177e4 | 2184 | inode->i_blocks = 0; |
078cd827 | 2185 | inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); |
46c9a946 | 2186 | inode->i_generation = prandom_u32(); |
1da177e4 LT |
2187 | info = SHMEM_I(inode); |
2188 | memset(info, 0, (char *)inode - (char *)info); | |
2189 | spin_lock_init(&info->lock); | |
40e041a2 | 2190 | info->seals = F_SEAL_SEAL; |
0b0a0806 | 2191 | info->flags = flags & VM_NORESERVE; |
779750d2 | 2192 | INIT_LIST_HEAD(&info->shrinklist); |
1da177e4 | 2193 | INIT_LIST_HEAD(&info->swaplist); |
38f38657 | 2194 | simple_xattrs_init(&info->xattrs); |
72c04902 | 2195 | cache_no_acl(inode); |
1da177e4 LT |
2196 | |
2197 | switch (mode & S_IFMT) { | |
2198 | default: | |
39f0247d | 2199 | inode->i_op = &shmem_special_inode_operations; |
1da177e4 LT |
2200 | init_special_inode(inode, mode, dev); |
2201 | break; | |
2202 | case S_IFREG: | |
14fcc23f | 2203 | inode->i_mapping->a_ops = &shmem_aops; |
1da177e4 LT |
2204 | inode->i_op = &shmem_inode_operations; |
2205 | inode->i_fop = &shmem_file_operations; | |
71fe804b LS |
2206 | mpol_shared_policy_init(&info->policy, |
2207 | shmem_get_sbmpol(sbinfo)); | |
1da177e4 LT |
2208 | break; |
2209 | case S_IFDIR: | |
d8c76e6f | 2210 | inc_nlink(inode); |
1da177e4 LT |
2211 | /* Some things misbehave if size == 0 on a directory */ |
2212 | inode->i_size = 2 * BOGO_DIRENT_SIZE; | |
2213 | inode->i_op = &shmem_dir_inode_operations; | |
2214 | inode->i_fop = &simple_dir_operations; | |
2215 | break; | |
2216 | case S_IFLNK: | |
2217 | /* | |
2218 | * Must not load anything in the rbtree, | |
2219 | * mpol_free_shared_policy will not be called. | |
2220 | */ | |
71fe804b | 2221 | mpol_shared_policy_init(&info->policy, NULL); |
1da177e4 LT |
2222 | break; |
2223 | } | |
b45d71fb JFG |
2224 | |
2225 | lockdep_annotate_inode_mutex_key(inode); | |
5b04c689 PE |
2226 | } else |
2227 | shmem_free_inode(sb); | |
1da177e4 LT |
2228 | return inode; |
2229 | } | |
2230 | ||
0cd6144a JW |
2231 | bool shmem_mapping(struct address_space *mapping) |
2232 | { | |
f8005451 | 2233 | return mapping->a_ops == &shmem_aops; |
0cd6144a JW |
2234 | } |
2235 | ||
8d103963 MR |
2236 | static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, |
2237 | pmd_t *dst_pmd, | |
2238 | struct vm_area_struct *dst_vma, | |
2239 | unsigned long dst_addr, | |
2240 | unsigned long src_addr, | |
2241 | bool zeropage, | |
2242 | struct page **pagep) | |
4c27fe4c MR |
2243 | { |
2244 | struct inode *inode = file_inode(dst_vma->vm_file); | |
2245 | struct shmem_inode_info *info = SHMEM_I(inode); | |
4c27fe4c MR |
2246 | struct address_space *mapping = inode->i_mapping; |
2247 | gfp_t gfp = mapping_gfp_mask(mapping); | |
2248 | pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); | |
2249 | struct mem_cgroup *memcg; | |
2250 | spinlock_t *ptl; | |
2251 | void *page_kaddr; | |
2252 | struct page *page; | |
2253 | pte_t _dst_pte, *dst_pte; | |
2254 | int ret; | |
2255 | ||
cb658a45 | 2256 | ret = -ENOMEM; |
0f079694 | 2257 | if (!shmem_inode_acct_block(inode, 1)) |
cb658a45 | 2258 | goto out; |
4c27fe4c | 2259 | |
cb658a45 | 2260 | if (!*pagep) { |
4c27fe4c MR |
2261 | page = shmem_alloc_page(gfp, info, pgoff); |
2262 | if (!page) | |
0f079694 | 2263 | goto out_unacct_blocks; |
4c27fe4c | 2264 | |
8d103963 MR |
2265 | if (!zeropage) { /* mcopy_atomic */ |
2266 | page_kaddr = kmap_atomic(page); | |
2267 | ret = copy_from_user(page_kaddr, | |
2268 | (const void __user *)src_addr, | |
2269 | PAGE_SIZE); | |
2270 | kunmap_atomic(page_kaddr); | |
2271 | ||
2272 | /* fallback to copy_from_user outside mmap_sem */ | |
2273 | if (unlikely(ret)) { | |
2274 | *pagep = page; | |
2275 | shmem_inode_unacct_blocks(inode, 1); | |
2276 | /* don't free the page */ | |
2277 | return -EFAULT; | |
2278 | } | |
2279 | } else { /* mfill_zeropage_atomic */ | |
2280 | clear_highpage(page); | |
4c27fe4c MR |
2281 | } |
2282 | } else { | |
2283 | page = *pagep; | |
2284 | *pagep = NULL; | |
2285 | } | |
2286 | ||
9cc90c66 AA |
2287 | VM_BUG_ON(PageLocked(page) || PageSwapBacked(page)); |
2288 | __SetPageLocked(page); | |
2289 | __SetPageSwapBacked(page); | |
a425d358 | 2290 | __SetPageUptodate(page); |
9cc90c66 | 2291 | |
2cf85583 | 2292 | ret = mem_cgroup_try_charge_delay(page, dst_mm, gfp, &memcg, false); |
4c27fe4c MR |
2293 | if (ret) |
2294 | goto out_release; | |
2295 | ||
2296 | ret = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK); | |
2297 | if (!ret) { | |
2298 | ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL); | |
2299 | radix_tree_preload_end(); | |
2300 | } | |
2301 | if (ret) | |
2302 | goto out_release_uncharge; | |
2303 | ||
2304 | mem_cgroup_commit_charge(page, memcg, false, false); | |
2305 | ||
2306 | _dst_pte = mk_pte(page, dst_vma->vm_page_prot); | |
2307 | if (dst_vma->vm_flags & VM_WRITE) | |
2308 | _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte)); | |
2309 | ||
2310 | ret = -EEXIST; | |
2311 | dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); | |
2312 | if (!pte_none(*dst_pte)) | |
2313 | goto out_release_uncharge_unlock; | |
2314 | ||
4c27fe4c MR |
2315 | lru_cache_add_anon(page); |
2316 | ||
2317 | spin_lock(&info->lock); | |
2318 | info->alloced++; | |
2319 | inode->i_blocks += BLOCKS_PER_PAGE; | |
2320 | shmem_recalc_inode(inode); | |
2321 | spin_unlock(&info->lock); | |
2322 | ||
2323 | inc_mm_counter(dst_mm, mm_counter_file(page)); | |
2324 | page_add_file_rmap(page, false); | |
2325 | set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); | |
2326 | ||
2327 | /* No need to invalidate - it was non-present before */ | |
2328 | update_mmu_cache(dst_vma, dst_addr, dst_pte); | |
2329 | unlock_page(page); | |
2330 | pte_unmap_unlock(dst_pte, ptl); | |
2331 | ret = 0; | |
2332 | out: | |
2333 | return ret; | |
2334 | out_release_uncharge_unlock: | |
2335 | pte_unmap_unlock(dst_pte, ptl); | |
2336 | out_release_uncharge: | |
2337 | mem_cgroup_cancel_charge(page, memcg, false); | |
2338 | out_release: | |
9cc90c66 | 2339 | unlock_page(page); |
4c27fe4c | 2340 | put_page(page); |
4c27fe4c | 2341 | out_unacct_blocks: |
0f079694 | 2342 | shmem_inode_unacct_blocks(inode, 1); |
4c27fe4c MR |
2343 | goto out; |
2344 | } | |
2345 | ||
8d103963 MR |
2346 | int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm, |
2347 | pmd_t *dst_pmd, | |
2348 | struct vm_area_struct *dst_vma, | |
2349 | unsigned long dst_addr, | |
2350 | unsigned long src_addr, | |
2351 | struct page **pagep) | |
2352 | { | |
2353 | return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, | |
2354 | dst_addr, src_addr, false, pagep); | |
2355 | } | |
2356 | ||
2357 | int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm, | |
2358 | pmd_t *dst_pmd, | |
2359 | struct vm_area_struct *dst_vma, | |
2360 | unsigned long dst_addr) | |
2361 | { | |
2362 | struct page *page = NULL; | |
2363 | ||
2364 | return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, | |
2365 | dst_addr, 0, true, &page); | |
2366 | } | |
2367 | ||
1da177e4 | 2368 | #ifdef CONFIG_TMPFS |
92e1d5be | 2369 | static const struct inode_operations shmem_symlink_inode_operations; |
69f07ec9 | 2370 | static const struct inode_operations shmem_short_symlink_operations; |
1da177e4 | 2371 | |
6d9d88d0 JS |
2372 | #ifdef CONFIG_TMPFS_XATTR |
2373 | static int shmem_initxattrs(struct inode *, const struct xattr *, void *); | |
2374 | #else | |
2375 | #define shmem_initxattrs NULL | |
2376 | #endif | |
2377 | ||
1da177e4 | 2378 | static int |
800d15a5 NP |
2379 | shmem_write_begin(struct file *file, struct address_space *mapping, |
2380 | loff_t pos, unsigned len, unsigned flags, | |
2381 | struct page **pagep, void **fsdata) | |
1da177e4 | 2382 | { |
800d15a5 | 2383 | struct inode *inode = mapping->host; |
40e041a2 | 2384 | struct shmem_inode_info *info = SHMEM_I(inode); |
09cbfeaf | 2385 | pgoff_t index = pos >> PAGE_SHIFT; |
40e041a2 DR |
2386 | |
2387 | /* i_mutex is held by caller */ | |
3f472cc9 | 2388 | if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) { |
40e041a2 DR |
2389 | if (info->seals & F_SEAL_WRITE) |
2390 | return -EPERM; | |
2391 | if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) | |
2392 | return -EPERM; | |
2393 | } | |
2394 | ||
9e18eb29 | 2395 | return shmem_getpage(inode, index, pagep, SGP_WRITE); |
800d15a5 NP |
2396 | } |
2397 | ||
2398 | static int | |
2399 | shmem_write_end(struct file *file, struct address_space *mapping, | |
2400 | loff_t pos, unsigned len, unsigned copied, | |
2401 | struct page *page, void *fsdata) | |
2402 | { | |
2403 | struct inode *inode = mapping->host; | |
2404 | ||
d3602444 HD |
2405 | if (pos + copied > inode->i_size) |
2406 | i_size_write(inode, pos + copied); | |
2407 | ||
ec9516fb | 2408 | if (!PageUptodate(page)) { |
800d8c63 KS |
2409 | struct page *head = compound_head(page); |
2410 | if (PageTransCompound(page)) { | |
2411 | int i; | |
2412 | ||
2413 | for (i = 0; i < HPAGE_PMD_NR; i++) { | |
2414 | if (head + i == page) | |
2415 | continue; | |
2416 | clear_highpage(head + i); | |
2417 | flush_dcache_page(head + i); | |
2418 | } | |
2419 | } | |
09cbfeaf KS |
2420 | if (copied < PAGE_SIZE) { |
2421 | unsigned from = pos & (PAGE_SIZE - 1); | |
ec9516fb | 2422 | zero_user_segments(page, 0, from, |
09cbfeaf | 2423 | from + copied, PAGE_SIZE); |
ec9516fb | 2424 | } |
800d8c63 | 2425 | SetPageUptodate(head); |
ec9516fb | 2426 | } |
800d15a5 | 2427 | set_page_dirty(page); |
6746aff7 | 2428 | unlock_page(page); |
09cbfeaf | 2429 | put_page(page); |
800d15a5 | 2430 | |
800d15a5 | 2431 | return copied; |
1da177e4 LT |
2432 | } |
2433 | ||
2ba5bbed | 2434 | static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
1da177e4 | 2435 | { |
6e58e79d AV |
2436 | struct file *file = iocb->ki_filp; |
2437 | struct inode *inode = file_inode(file); | |
1da177e4 | 2438 | struct address_space *mapping = inode->i_mapping; |
41ffe5d5 HD |
2439 | pgoff_t index; |
2440 | unsigned long offset; | |
a0ee5ec5 | 2441 | enum sgp_type sgp = SGP_READ; |
f7c1d074 | 2442 | int error = 0; |
cb66a7a1 | 2443 | ssize_t retval = 0; |
6e58e79d | 2444 | loff_t *ppos = &iocb->ki_pos; |
a0ee5ec5 HD |
2445 | |
2446 | /* | |
2447 | * Might this read be for a stacking filesystem? Then when reading | |
2448 | * holes of a sparse file, we actually need to allocate those pages, | |
2449 | * and even mark them dirty, so it cannot exceed the max_blocks limit. | |
2450 | */ | |
777eda2c | 2451 | if (!iter_is_iovec(to)) |
75edd345 | 2452 | sgp = SGP_CACHE; |
1da177e4 | 2453 | |
09cbfeaf KS |
2454 | index = *ppos >> PAGE_SHIFT; |
2455 | offset = *ppos & ~PAGE_MASK; | |
1da177e4 LT |
2456 | |
2457 | for (;;) { | |
2458 | struct page *page = NULL; | |
41ffe5d5 HD |
2459 | pgoff_t end_index; |
2460 | unsigned long nr, ret; | |
1da177e4 LT |
2461 | loff_t i_size = i_size_read(inode); |
2462 | ||
09cbfeaf | 2463 | end_index = i_size >> PAGE_SHIFT; |
1da177e4 LT |
2464 | if (index > end_index) |
2465 | break; | |
2466 | if (index == end_index) { | |
09cbfeaf | 2467 | nr = i_size & ~PAGE_MASK; |
1da177e4 LT |
2468 | if (nr <= offset) |
2469 | break; | |
2470 | } | |
2471 | ||
9e18eb29 | 2472 | error = shmem_getpage(inode, index, &page, sgp); |
6e58e79d AV |
2473 | if (error) { |
2474 | if (error == -EINVAL) | |
2475 | error = 0; | |
1da177e4 LT |
2476 | break; |
2477 | } | |
75edd345 HD |
2478 | if (page) { |
2479 | if (sgp == SGP_CACHE) | |
2480 | set_page_dirty(page); | |
d3602444 | 2481 | unlock_page(page); |
75edd345 | 2482 | } |
1da177e4 LT |
2483 | |
2484 | /* | |
2485 | * We must evaluate after, since reads (unlike writes) | |
1b1dcc1b | 2486 | * are called without i_mutex protection against truncate |
1da177e4 | 2487 | */ |
09cbfeaf | 2488 | nr = PAGE_SIZE; |
1da177e4 | 2489 | i_size = i_size_read(inode); |
09cbfeaf | 2490 | end_index = i_size >> PAGE_SHIFT; |
1da177e4 | 2491 | if (index == end_index) { |
09cbfeaf | 2492 | nr = i_size & ~PAGE_MASK; |
1da177e4 LT |
2493 | if (nr <= offset) { |
2494 | if (page) | |
09cbfeaf | 2495 | put_page(page); |
1da177e4 LT |
2496 | break; |
2497 | } | |
2498 | } | |
2499 | nr -= offset; | |
2500 | ||
2501 | if (page) { | |
2502 | /* | |
2503 | * If users can be writing to this page using arbitrary | |
2504 | * virtual addresses, take care about potential aliasing | |
2505 | * before reading the page on the kernel side. | |
2506 | */ | |
2507 | if (mapping_writably_mapped(mapping)) | |
2508 | flush_dcache_page(page); | |
2509 | /* | |
2510 | * Mark the page accessed if we read the beginning. | |
2511 | */ | |
2512 | if (!offset) | |
2513 | mark_page_accessed(page); | |
b5810039 | 2514 | } else { |
1da177e4 | 2515 | page = ZERO_PAGE(0); |
09cbfeaf | 2516 | get_page(page); |
b5810039 | 2517 | } |
1da177e4 LT |
2518 | |
2519 | /* | |
2520 | * Ok, we have the page, and it's up-to-date, so | |
2521 | * now we can copy it to user space... | |
1da177e4 | 2522 | */ |
2ba5bbed | 2523 | ret = copy_page_to_iter(page, offset, nr, to); |
6e58e79d | 2524 | retval += ret; |
1da177e4 | 2525 | offset += ret; |
09cbfeaf KS |
2526 | index += offset >> PAGE_SHIFT; |
2527 | offset &= ~PAGE_MASK; | |
1da177e4 | 2528 | |
09cbfeaf | 2529 | put_page(page); |
2ba5bbed | 2530 | if (!iov_iter_count(to)) |
1da177e4 | 2531 | break; |
6e58e79d AV |
2532 | if (ret < nr) { |
2533 | error = -EFAULT; | |
2534 | break; | |
2535 | } | |
1da177e4 LT |
2536 | cond_resched(); |
2537 | } | |
2538 | ||
09cbfeaf | 2539 | *ppos = ((loff_t) index << PAGE_SHIFT) + offset; |
6e58e79d AV |
2540 | file_accessed(file); |
2541 | return retval ? retval : error; | |
1da177e4 LT |
2542 | } |
2543 | ||
220f2ac9 HD |
2544 | /* |
2545 | * llseek SEEK_DATA or SEEK_HOLE through the radix_tree. | |
2546 | */ | |
2547 | static pgoff_t shmem_seek_hole_data(struct address_space *mapping, | |
965c8e59 | 2548 | pgoff_t index, pgoff_t end, int whence) |
220f2ac9 HD |
2549 | { |
2550 | struct page *page; | |
2551 | struct pagevec pvec; | |
2552 | pgoff_t indices[PAGEVEC_SIZE]; | |
2553 | bool done = false; | |
2554 | int i; | |
2555 | ||
86679820 | 2556 | pagevec_init(&pvec); |
220f2ac9 HD |
2557 | pvec.nr = 1; /* start small: we may be there already */ |
2558 | while (!done) { | |
0cd6144a | 2559 | pvec.nr = find_get_entries(mapping, index, |
220f2ac9 HD |
2560 | pvec.nr, pvec.pages, indices); |
2561 | if (!pvec.nr) { | |
965c8e59 | 2562 | if (whence == SEEK_DATA) |
220f2ac9 HD |
2563 | index = end; |
2564 | break; | |
2565 | } | |
2566 | for (i = 0; i < pvec.nr; i++, index++) { | |
2567 | if (index < indices[i]) { | |
965c8e59 | 2568 | if (whence == SEEK_HOLE) { |
220f2ac9 HD |
2569 | done = true; |
2570 | break; | |
2571 | } | |
2572 | index = indices[i]; | |
2573 | } | |
2574 | page = pvec.pages[i]; | |
3159f943 | 2575 | if (page && !xa_is_value(page)) { |
220f2ac9 HD |
2576 | if (!PageUptodate(page)) |
2577 | page = NULL; | |
2578 | } | |
2579 | if (index >= end || | |
965c8e59 AM |
2580 | (page && whence == SEEK_DATA) || |
2581 | (!page && whence == SEEK_HOLE)) { | |
220f2ac9 HD |
2582 | done = true; |
2583 | break; | |
2584 | } | |
2585 | } | |
0cd6144a | 2586 | pagevec_remove_exceptionals(&pvec); |
220f2ac9 HD |
2587 | pagevec_release(&pvec); |
2588 | pvec.nr = PAGEVEC_SIZE; | |
2589 | cond_resched(); | |
2590 | } | |
2591 | return index; | |
2592 | } | |
2593 | ||
965c8e59 | 2594 | static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) |
220f2ac9 HD |
2595 | { |
2596 | struct address_space *mapping = file->f_mapping; | |
2597 | struct inode *inode = mapping->host; | |
2598 | pgoff_t start, end; | |
2599 | loff_t new_offset; | |
2600 | ||
965c8e59 AM |
2601 | if (whence != SEEK_DATA && whence != SEEK_HOLE) |
2602 | return generic_file_llseek_size(file, offset, whence, | |
220f2ac9 | 2603 | MAX_LFS_FILESIZE, i_size_read(inode)); |
5955102c | 2604 | inode_lock(inode); |
220f2ac9 HD |
2605 | /* We're holding i_mutex so we can access i_size directly */ |
2606 | ||
2607 | if (offset < 0) | |
2608 | offset = -EINVAL; | |
2609 | else if (offset >= inode->i_size) | |
2610 | offset = -ENXIO; | |
2611 | else { | |
09cbfeaf KS |
2612 | start = offset >> PAGE_SHIFT; |
2613 | end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
965c8e59 | 2614 | new_offset = shmem_seek_hole_data(mapping, start, end, whence); |
09cbfeaf | 2615 | new_offset <<= PAGE_SHIFT; |
220f2ac9 HD |
2616 | if (new_offset > offset) { |
2617 | if (new_offset < inode->i_size) | |
2618 | offset = new_offset; | |
965c8e59 | 2619 | else if (whence == SEEK_DATA) |
220f2ac9 HD |
2620 | offset = -ENXIO; |
2621 | else | |
2622 | offset = inode->i_size; | |
2623 | } | |
2624 | } | |
2625 | ||
387aae6f HD |
2626 | if (offset >= 0) |
2627 | offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); | |
5955102c | 2628 | inode_unlock(inode); |
220f2ac9 HD |
2629 | return offset; |
2630 | } | |
2631 | ||
83e4fa9c HD |
2632 | static long shmem_fallocate(struct file *file, int mode, loff_t offset, |
2633 | loff_t len) | |
2634 | { | |
496ad9aa | 2635 | struct inode *inode = file_inode(file); |
e2d12e22 | 2636 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
40e041a2 | 2637 | struct shmem_inode_info *info = SHMEM_I(inode); |
1aac1400 | 2638 | struct shmem_falloc shmem_falloc; |
e2d12e22 HD |
2639 | pgoff_t start, index, end; |
2640 | int error; | |
83e4fa9c | 2641 | |
13ace4d0 HD |
2642 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
2643 | return -EOPNOTSUPP; | |
2644 | ||
5955102c | 2645 | inode_lock(inode); |
83e4fa9c HD |
2646 | |
2647 | if (mode & FALLOC_FL_PUNCH_HOLE) { | |
2648 | struct address_space *mapping = file->f_mapping; | |
2649 | loff_t unmap_start = round_up(offset, PAGE_SIZE); | |
2650 | loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; | |
8e205f77 | 2651 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); |
83e4fa9c | 2652 | |
40e041a2 DR |
2653 | /* protected by i_mutex */ |
2654 | if (info->seals & F_SEAL_WRITE) { | |
2655 | error = -EPERM; | |
2656 | goto out; | |
2657 | } | |
2658 | ||
8e205f77 | 2659 | shmem_falloc.waitq = &shmem_falloc_waitq; |
f00cdc6d HD |
2660 | shmem_falloc.start = unmap_start >> PAGE_SHIFT; |
2661 | shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; | |
2662 | spin_lock(&inode->i_lock); | |
2663 | inode->i_private = &shmem_falloc; | |
2664 | spin_unlock(&inode->i_lock); | |
2665 | ||
83e4fa9c HD |
2666 | if ((u64)unmap_end > (u64)unmap_start) |
2667 | unmap_mapping_range(mapping, unmap_start, | |
2668 | 1 + unmap_end - unmap_start, 0); | |
2669 | shmem_truncate_range(inode, offset, offset + len - 1); | |
2670 | /* No need to unmap again: hole-punching leaves COWed pages */ | |
8e205f77 HD |
2671 | |
2672 | spin_lock(&inode->i_lock); | |
2673 | inode->i_private = NULL; | |
2674 | wake_up_all(&shmem_falloc_waitq); | |
2055da97 | 2675 | WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head)); |
8e205f77 | 2676 | spin_unlock(&inode->i_lock); |
83e4fa9c | 2677 | error = 0; |
8e205f77 | 2678 | goto out; |
e2d12e22 HD |
2679 | } |
2680 | ||
2681 | /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ | |
2682 | error = inode_newsize_ok(inode, offset + len); | |
2683 | if (error) | |
2684 | goto out; | |
2685 | ||
40e041a2 DR |
2686 | if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { |
2687 | error = -EPERM; | |
2688 | goto out; | |
2689 | } | |
2690 | ||
09cbfeaf KS |
2691 | start = offset >> PAGE_SHIFT; |
2692 | end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
e2d12e22 HD |
2693 | /* Try to avoid a swapstorm if len is impossible to satisfy */ |
2694 | if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { | |
2695 | error = -ENOSPC; | |
2696 | goto out; | |
83e4fa9c HD |
2697 | } |
2698 | ||
8e205f77 | 2699 | shmem_falloc.waitq = NULL; |
1aac1400 HD |
2700 | shmem_falloc.start = start; |
2701 | shmem_falloc.next = start; | |
2702 | shmem_falloc.nr_falloced = 0; | |
2703 | shmem_falloc.nr_unswapped = 0; | |
2704 | spin_lock(&inode->i_lock); | |
2705 | inode->i_private = &shmem_falloc; | |
2706 | spin_unlock(&inode->i_lock); | |
2707 | ||
e2d12e22 HD |
2708 | for (index = start; index < end; index++) { |
2709 | struct page *page; | |
2710 | ||
2711 | /* | |
2712 | * Good, the fallocate(2) manpage permits EINTR: we may have | |
2713 | * been interrupted because we are using up too much memory. | |
2714 | */ | |
2715 | if (signal_pending(current)) | |
2716 | error = -EINTR; | |
1aac1400 HD |
2717 | else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) |
2718 | error = -ENOMEM; | |
e2d12e22 | 2719 | else |
9e18eb29 | 2720 | error = shmem_getpage(inode, index, &page, SGP_FALLOC); |
e2d12e22 | 2721 | if (error) { |
1635f6a7 | 2722 | /* Remove the !PageUptodate pages we added */ |
7f556567 HD |
2723 | if (index > start) { |
2724 | shmem_undo_range(inode, | |
2725 | (loff_t)start << PAGE_SHIFT, | |
2726 | ((loff_t)index << PAGE_SHIFT) - 1, true); | |
2727 | } | |
1aac1400 | 2728 | goto undone; |
e2d12e22 HD |
2729 | } |
2730 | ||
1aac1400 HD |
2731 | /* |
2732 | * Inform shmem_writepage() how far we have reached. | |
2733 | * No need for lock or barrier: we have the page lock. | |
2734 | */ | |
2735 | shmem_falloc.next++; | |
2736 | if (!PageUptodate(page)) | |
2737 | shmem_falloc.nr_falloced++; | |
2738 | ||
e2d12e22 | 2739 | /* |
1635f6a7 HD |
2740 | * If !PageUptodate, leave it that way so that freeable pages |
2741 | * can be recognized if we need to rollback on error later. | |
2742 | * But set_page_dirty so that memory pressure will swap rather | |
e2d12e22 HD |
2743 | * than free the pages we are allocating (and SGP_CACHE pages |
2744 | * might still be clean: we now need to mark those dirty too). | |
2745 | */ | |
2746 | set_page_dirty(page); | |
2747 | unlock_page(page); | |
09cbfeaf | 2748 | put_page(page); |
e2d12e22 HD |
2749 | cond_resched(); |
2750 | } | |
2751 | ||
2752 | if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) | |
2753 | i_size_write(inode, offset + len); | |
078cd827 | 2754 | inode->i_ctime = current_time(inode); |
1aac1400 HD |
2755 | undone: |
2756 | spin_lock(&inode->i_lock); | |
2757 | inode->i_private = NULL; | |
2758 | spin_unlock(&inode->i_lock); | |
e2d12e22 | 2759 | out: |
5955102c | 2760 | inode_unlock(inode); |
83e4fa9c HD |
2761 | return error; |
2762 | } | |
2763 | ||
726c3342 | 2764 | static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) |
1da177e4 | 2765 | { |
726c3342 | 2766 | struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); |
1da177e4 LT |
2767 | |
2768 | buf->f_type = TMPFS_MAGIC; | |
09cbfeaf | 2769 | buf->f_bsize = PAGE_SIZE; |
1da177e4 | 2770 | buf->f_namelen = NAME_MAX; |
0edd73b3 | 2771 | if (sbinfo->max_blocks) { |
1da177e4 | 2772 | buf->f_blocks = sbinfo->max_blocks; |
41ffe5d5 HD |
2773 | buf->f_bavail = |
2774 | buf->f_bfree = sbinfo->max_blocks - | |
2775 | percpu_counter_sum(&sbinfo->used_blocks); | |
0edd73b3 HD |
2776 | } |
2777 | if (sbinfo->max_inodes) { | |
1da177e4 LT |
2778 | buf->f_files = sbinfo->max_inodes; |
2779 | buf->f_ffree = sbinfo->free_inodes; | |
1da177e4 LT |
2780 | } |
2781 | /* else leave those fields 0 like simple_statfs */ | |
2782 | return 0; | |
2783 | } | |
2784 | ||
2785 | /* | |
2786 | * File creation. Allocate an inode, and we're done.. | |
2787 | */ | |
2788 | static int | |
1a67aafb | 2789 | shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
1da177e4 | 2790 | { |
0b0a0806 | 2791 | struct inode *inode; |
1da177e4 LT |
2792 | int error = -ENOSPC; |
2793 | ||
454abafe | 2794 | inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); |
1da177e4 | 2795 | if (inode) { |
feda821e CH |
2796 | error = simple_acl_create(dir, inode); |
2797 | if (error) | |
2798 | goto out_iput; | |
2a7dba39 | 2799 | error = security_inode_init_security(inode, dir, |
9d8f13ba | 2800 | &dentry->d_name, |
6d9d88d0 | 2801 | shmem_initxattrs, NULL); |
feda821e CH |
2802 | if (error && error != -EOPNOTSUPP) |
2803 | goto out_iput; | |
37ec43cd | 2804 | |
718deb6b | 2805 | error = 0; |
1da177e4 | 2806 | dir->i_size += BOGO_DIRENT_SIZE; |
078cd827 | 2807 | dir->i_ctime = dir->i_mtime = current_time(dir); |
1da177e4 LT |
2808 | d_instantiate(dentry, inode); |
2809 | dget(dentry); /* Extra count - pin the dentry in core */ | |
1da177e4 LT |
2810 | } |
2811 | return error; | |
feda821e CH |
2812 | out_iput: |
2813 | iput(inode); | |
2814 | return error; | |
1da177e4 LT |
2815 | } |
2816 | ||
60545d0d AV |
2817 | static int |
2818 | shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) | |
2819 | { | |
2820 | struct inode *inode; | |
2821 | int error = -ENOSPC; | |
2822 | ||
2823 | inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); | |
2824 | if (inode) { | |
2825 | error = security_inode_init_security(inode, dir, | |
2826 | NULL, | |
2827 | shmem_initxattrs, NULL); | |
feda821e CH |
2828 | if (error && error != -EOPNOTSUPP) |
2829 | goto out_iput; | |
2830 | error = simple_acl_create(dir, inode); | |
2831 | if (error) | |
2832 | goto out_iput; | |
60545d0d AV |
2833 | d_tmpfile(dentry, inode); |
2834 | } | |
2835 | return error; | |
feda821e CH |
2836 | out_iput: |
2837 | iput(inode); | |
2838 | return error; | |
60545d0d AV |
2839 | } |
2840 | ||
18bb1db3 | 2841 | static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
1da177e4 LT |
2842 | { |
2843 | int error; | |
2844 | ||
2845 | if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) | |
2846 | return error; | |
d8c76e6f | 2847 | inc_nlink(dir); |
1da177e4 LT |
2848 | return 0; |
2849 | } | |
2850 | ||
4acdaf27 | 2851 | static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
ebfc3b49 | 2852 | bool excl) |
1da177e4 LT |
2853 | { |
2854 | return shmem_mknod(dir, dentry, mode | S_IFREG, 0); | |
2855 | } | |
2856 | ||
2857 | /* | |
2858 | * Link a file.. | |
2859 | */ | |
2860 | static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) | |
2861 | { | |
75c3cfa8 | 2862 | struct inode *inode = d_inode(old_dentry); |
5b04c689 | 2863 | int ret; |
1da177e4 LT |
2864 | |
2865 | /* | |
2866 | * No ordinary (disk based) filesystem counts links as inodes; | |
2867 | * but each new link needs a new dentry, pinning lowmem, and | |
2868 | * tmpfs dentries cannot be pruned until they are unlinked. | |
2869 | */ | |
5b04c689 PE |
2870 | ret = shmem_reserve_inode(inode->i_sb); |
2871 | if (ret) | |
2872 | goto out; | |
1da177e4 LT |
2873 | |
2874 | dir->i_size += BOGO_DIRENT_SIZE; | |
078cd827 | 2875 | inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); |
d8c76e6f | 2876 | inc_nlink(inode); |
7de9c6ee | 2877 | ihold(inode); /* New dentry reference */ |
1da177e4 LT |
2878 | dget(dentry); /* Extra pinning count for the created dentry */ |
2879 | d_instantiate(dentry, inode); | |
5b04c689 PE |
2880 | out: |
2881 | return ret; | |
1da177e4 LT |
2882 | } |
2883 | ||
2884 | static int shmem_unlink(struct inode *dir, struct dentry *dentry) | |
2885 | { | |
75c3cfa8 | 2886 | struct inode *inode = d_inode(dentry); |
1da177e4 | 2887 | |
5b04c689 PE |
2888 | if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) |
2889 | shmem_free_inode(inode->i_sb); | |
1da177e4 LT |
2890 | |
2891 | dir->i_size -= BOGO_DIRENT_SIZE; | |
078cd827 | 2892 | inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); |
9a53c3a7 | 2893 | drop_nlink(inode); |
1da177e4 LT |
2894 | dput(dentry); /* Undo the count from "create" - this does all the work */ |
2895 | return 0; | |
2896 | } | |
2897 | ||
2898 | static int shmem_rmdir(struct inode *dir, struct dentry *dentry) | |
2899 | { | |
2900 | if (!simple_empty(dentry)) | |
2901 | return -ENOTEMPTY; | |
2902 | ||
75c3cfa8 | 2903 | drop_nlink(d_inode(dentry)); |
9a53c3a7 | 2904 | drop_nlink(dir); |
1da177e4 LT |
2905 | return shmem_unlink(dir, dentry); |
2906 | } | |
2907 | ||
37456771 MS |
2908 | static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) |
2909 | { | |
e36cb0b8 DH |
2910 | bool old_is_dir = d_is_dir(old_dentry); |
2911 | bool new_is_dir = d_is_dir(new_dentry); | |
37456771 MS |
2912 | |
2913 | if (old_dir != new_dir && old_is_dir != new_is_dir) { | |
2914 | if (old_is_dir) { | |
2915 | drop_nlink(old_dir); | |
2916 | inc_nlink(new_dir); | |
2917 | } else { | |
2918 | drop_nlink(new_dir); | |
2919 | inc_nlink(old_dir); | |
2920 | } | |
2921 | } | |
2922 | old_dir->i_ctime = old_dir->i_mtime = | |
2923 | new_dir->i_ctime = new_dir->i_mtime = | |
75c3cfa8 | 2924 | d_inode(old_dentry)->i_ctime = |
078cd827 | 2925 | d_inode(new_dentry)->i_ctime = current_time(old_dir); |
37456771 MS |
2926 | |
2927 | return 0; | |
2928 | } | |
2929 | ||
46fdb794 MS |
2930 | static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry) |
2931 | { | |
2932 | struct dentry *whiteout; | |
2933 | int error; | |
2934 | ||
2935 | whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); | |
2936 | if (!whiteout) | |
2937 | return -ENOMEM; | |
2938 | ||
2939 | error = shmem_mknod(old_dir, whiteout, | |
2940 | S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); | |
2941 | dput(whiteout); | |
2942 | if (error) | |
2943 | return error; | |
2944 | ||
2945 | /* | |
2946 | * Cheat and hash the whiteout while the old dentry is still in | |
2947 | * place, instead of playing games with FS_RENAME_DOES_D_MOVE. | |
2948 | * | |
2949 | * d_lookup() will consistently find one of them at this point, | |
2950 | * not sure which one, but that isn't even important. | |
2951 | */ | |
2952 | d_rehash(whiteout); | |
2953 | return 0; | |
2954 | } | |
2955 | ||
1da177e4 LT |
2956 | /* |
2957 | * The VFS layer already does all the dentry stuff for rename, | |
2958 | * we just have to decrement the usage count for the target if | |
2959 | * it exists so that the VFS layer correctly free's it when it | |
2960 | * gets overwritten. | |
2961 | */ | |
3b69ff51 | 2962 | static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags) |
1da177e4 | 2963 | { |
75c3cfa8 | 2964 | struct inode *inode = d_inode(old_dentry); |
1da177e4 LT |
2965 | int they_are_dirs = S_ISDIR(inode->i_mode); |
2966 | ||
46fdb794 | 2967 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
3b69ff51 MS |
2968 | return -EINVAL; |
2969 | ||
37456771 MS |
2970 | if (flags & RENAME_EXCHANGE) |
2971 | return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry); | |
2972 | ||
1da177e4 LT |
2973 | if (!simple_empty(new_dentry)) |
2974 | return -ENOTEMPTY; | |
2975 | ||
46fdb794 MS |
2976 | if (flags & RENAME_WHITEOUT) { |
2977 | int error; | |
2978 | ||
2979 | error = shmem_whiteout(old_dir, old_dentry); | |
2980 | if (error) | |
2981 | return error; | |
2982 | } | |
2983 | ||
75c3cfa8 | 2984 | if (d_really_is_positive(new_dentry)) { |
1da177e4 | 2985 | (void) shmem_unlink(new_dir, new_dentry); |
b928095b | 2986 | if (they_are_dirs) { |
75c3cfa8 | 2987 | drop_nlink(d_inode(new_dentry)); |
9a53c3a7 | 2988 | drop_nlink(old_dir); |
b928095b | 2989 | } |
1da177e4 | 2990 | } else if (they_are_dirs) { |
9a53c3a7 | 2991 | drop_nlink(old_dir); |
d8c76e6f | 2992 | inc_nlink(new_dir); |
1da177e4 LT |
2993 | } |
2994 | ||
2995 | old_dir->i_size -= BOGO_DIRENT_SIZE; | |
2996 | new_dir->i_size += BOGO_DIRENT_SIZE; | |
2997 | old_dir->i_ctime = old_dir->i_mtime = | |
2998 | new_dir->i_ctime = new_dir->i_mtime = | |
078cd827 | 2999 | inode->i_ctime = current_time(old_dir); |
1da177e4 LT |
3000 | return 0; |
3001 | } | |
3002 | ||
3003 | static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) | |
3004 | { | |
3005 | int error; | |
3006 | int len; | |
3007 | struct inode *inode; | |
9276aad6 | 3008 | struct page *page; |
1da177e4 LT |
3009 | |
3010 | len = strlen(symname) + 1; | |
09cbfeaf | 3011 | if (len > PAGE_SIZE) |
1da177e4 LT |
3012 | return -ENAMETOOLONG; |
3013 | ||
0825a6f9 JP |
3014 | inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0, |
3015 | VM_NORESERVE); | |
1da177e4 LT |
3016 | if (!inode) |
3017 | return -ENOSPC; | |
3018 | ||
9d8f13ba | 3019 | error = security_inode_init_security(inode, dir, &dentry->d_name, |
6d9d88d0 | 3020 | shmem_initxattrs, NULL); |
570bc1c2 SS |
3021 | if (error) { |
3022 | if (error != -EOPNOTSUPP) { | |
3023 | iput(inode); | |
3024 | return error; | |
3025 | } | |
3026 | error = 0; | |
3027 | } | |
3028 | ||
1da177e4 | 3029 | inode->i_size = len-1; |
69f07ec9 | 3030 | if (len <= SHORT_SYMLINK_LEN) { |
3ed47db3 AV |
3031 | inode->i_link = kmemdup(symname, len, GFP_KERNEL); |
3032 | if (!inode->i_link) { | |
69f07ec9 HD |
3033 | iput(inode); |
3034 | return -ENOMEM; | |
3035 | } | |
3036 | inode->i_op = &shmem_short_symlink_operations; | |
1da177e4 | 3037 | } else { |
e8ecde25 | 3038 | inode_nohighmem(inode); |
9e18eb29 | 3039 | error = shmem_getpage(inode, 0, &page, SGP_WRITE); |
1da177e4 LT |
3040 | if (error) { |
3041 | iput(inode); | |
3042 | return error; | |
3043 | } | |
14fcc23f | 3044 | inode->i_mapping->a_ops = &shmem_aops; |
1da177e4 | 3045 | inode->i_op = &shmem_symlink_inode_operations; |
21fc61c7 | 3046 | memcpy(page_address(page), symname, len); |
ec9516fb | 3047 | SetPageUptodate(page); |
1da177e4 | 3048 | set_page_dirty(page); |
6746aff7 | 3049 | unlock_page(page); |
09cbfeaf | 3050 | put_page(page); |
1da177e4 | 3051 | } |
1da177e4 | 3052 | dir->i_size += BOGO_DIRENT_SIZE; |
078cd827 | 3053 | dir->i_ctime = dir->i_mtime = current_time(dir); |
1da177e4 LT |
3054 | d_instantiate(dentry, inode); |
3055 | dget(dentry); | |
3056 | return 0; | |
3057 | } | |
3058 | ||
fceef393 | 3059 | static void shmem_put_link(void *arg) |
1da177e4 | 3060 | { |
fceef393 AV |
3061 | mark_page_accessed(arg); |
3062 | put_page(arg); | |
1da177e4 LT |
3063 | } |
3064 | ||
6b255391 | 3065 | static const char *shmem_get_link(struct dentry *dentry, |
fceef393 AV |
3066 | struct inode *inode, |
3067 | struct delayed_call *done) | |
1da177e4 | 3068 | { |
1da177e4 | 3069 | struct page *page = NULL; |
6b255391 | 3070 | int error; |
6a6c9904 AV |
3071 | if (!dentry) { |
3072 | page = find_get_page(inode->i_mapping, 0); | |
3073 | if (!page) | |
3074 | return ERR_PTR(-ECHILD); | |
3075 | if (!PageUptodate(page)) { | |
3076 | put_page(page); | |
3077 | return ERR_PTR(-ECHILD); | |
3078 | } | |
3079 | } else { | |
9e18eb29 | 3080 | error = shmem_getpage(inode, 0, &page, SGP_READ); |
6a6c9904 AV |
3081 | if (error) |
3082 | return ERR_PTR(error); | |
3083 | unlock_page(page); | |
3084 | } | |
fceef393 | 3085 | set_delayed_call(done, shmem_put_link, page); |
21fc61c7 | 3086 | return page_address(page); |
1da177e4 LT |
3087 | } |
3088 | ||
b09e0fa4 | 3089 | #ifdef CONFIG_TMPFS_XATTR |
46711810 | 3090 | /* |
b09e0fa4 EP |
3091 | * Superblocks without xattr inode operations may get some security.* xattr |
3092 | * support from the LSM "for free". As soon as we have any other xattrs | |
39f0247d AG |
3093 | * like ACLs, we also need to implement the security.* handlers at |
3094 | * filesystem level, though. | |
3095 | */ | |
3096 | ||
6d9d88d0 JS |
3097 | /* |
3098 | * Callback for security_inode_init_security() for acquiring xattrs. | |
3099 | */ | |
3100 | static int shmem_initxattrs(struct inode *inode, | |
3101 | const struct xattr *xattr_array, | |
3102 | void *fs_info) | |
3103 | { | |
3104 | struct shmem_inode_info *info = SHMEM_I(inode); | |
3105 | const struct xattr *xattr; | |
38f38657 | 3106 | struct simple_xattr *new_xattr; |
6d9d88d0 JS |
3107 | size_t len; |
3108 | ||
3109 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { | |
38f38657 | 3110 | new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); |
6d9d88d0 JS |
3111 | if (!new_xattr) |
3112 | return -ENOMEM; | |
3113 | ||
3114 | len = strlen(xattr->name) + 1; | |
3115 | new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, | |
3116 | GFP_KERNEL); | |
3117 | if (!new_xattr->name) { | |
3118 | kfree(new_xattr); | |
3119 | return -ENOMEM; | |
3120 | } | |
3121 | ||
3122 | memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, | |
3123 | XATTR_SECURITY_PREFIX_LEN); | |
3124 | memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, | |
3125 | xattr->name, len); | |
3126 | ||
38f38657 | 3127 | simple_xattr_list_add(&info->xattrs, new_xattr); |
6d9d88d0 JS |
3128 | } |
3129 | ||
3130 | return 0; | |
3131 | } | |
3132 | ||
aa7c5241 | 3133 | static int shmem_xattr_handler_get(const struct xattr_handler *handler, |
b296821a AV |
3134 | struct dentry *unused, struct inode *inode, |
3135 | const char *name, void *buffer, size_t size) | |
b09e0fa4 | 3136 | { |
b296821a | 3137 | struct shmem_inode_info *info = SHMEM_I(inode); |
b09e0fa4 | 3138 | |
aa7c5241 | 3139 | name = xattr_full_name(handler, name); |
38f38657 | 3140 | return simple_xattr_get(&info->xattrs, name, buffer, size); |
b09e0fa4 EP |
3141 | } |
3142 | ||
aa7c5241 | 3143 | static int shmem_xattr_handler_set(const struct xattr_handler *handler, |
59301226 AV |
3144 | struct dentry *unused, struct inode *inode, |
3145 | const char *name, const void *value, | |
3146 | size_t size, int flags) | |
b09e0fa4 | 3147 | { |
59301226 | 3148 | struct shmem_inode_info *info = SHMEM_I(inode); |
b09e0fa4 | 3149 | |
aa7c5241 | 3150 | name = xattr_full_name(handler, name); |
38f38657 | 3151 | return simple_xattr_set(&info->xattrs, name, value, size, flags); |
b09e0fa4 EP |
3152 | } |
3153 | ||
aa7c5241 AG |
3154 | static const struct xattr_handler shmem_security_xattr_handler = { |
3155 | .prefix = XATTR_SECURITY_PREFIX, | |
3156 | .get = shmem_xattr_handler_get, | |
3157 | .set = shmem_xattr_handler_set, | |
3158 | }; | |
b09e0fa4 | 3159 | |
aa7c5241 AG |
3160 | static const struct xattr_handler shmem_trusted_xattr_handler = { |
3161 | .prefix = XATTR_TRUSTED_PREFIX, | |
3162 | .get = shmem_xattr_handler_get, | |
3163 | .set = shmem_xattr_handler_set, | |
3164 | }; | |
b09e0fa4 | 3165 | |
aa7c5241 AG |
3166 | static const struct xattr_handler *shmem_xattr_handlers[] = { |
3167 | #ifdef CONFIG_TMPFS_POSIX_ACL | |
3168 | &posix_acl_access_xattr_handler, | |
3169 | &posix_acl_default_xattr_handler, | |
3170 | #endif | |
3171 | &shmem_security_xattr_handler, | |
3172 | &shmem_trusted_xattr_handler, | |
3173 | NULL | |
3174 | }; | |
b09e0fa4 EP |
3175 | |
3176 | static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) | |
3177 | { | |
75c3cfa8 | 3178 | struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); |
786534b9 | 3179 | return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); |
b09e0fa4 EP |
3180 | } |
3181 | #endif /* CONFIG_TMPFS_XATTR */ | |
3182 | ||
69f07ec9 | 3183 | static const struct inode_operations shmem_short_symlink_operations = { |
6b255391 | 3184 | .get_link = simple_get_link, |
b09e0fa4 | 3185 | #ifdef CONFIG_TMPFS_XATTR |
b09e0fa4 | 3186 | .listxattr = shmem_listxattr, |
b09e0fa4 EP |
3187 | #endif |
3188 | }; | |
3189 | ||
3190 | static const struct inode_operations shmem_symlink_inode_operations = { | |
6b255391 | 3191 | .get_link = shmem_get_link, |
b09e0fa4 | 3192 | #ifdef CONFIG_TMPFS_XATTR |
b09e0fa4 | 3193 | .listxattr = shmem_listxattr, |
39f0247d | 3194 | #endif |
b09e0fa4 | 3195 | }; |
39f0247d | 3196 | |
91828a40 DG |
3197 | static struct dentry *shmem_get_parent(struct dentry *child) |
3198 | { | |
3199 | return ERR_PTR(-ESTALE); | |
3200 | } | |
3201 | ||
3202 | static int shmem_match(struct inode *ino, void *vfh) | |
3203 | { | |
3204 | __u32 *fh = vfh; | |
3205 | __u64 inum = fh[2]; | |
3206 | inum = (inum << 32) | fh[1]; | |
3207 | return ino->i_ino == inum && fh[0] == ino->i_generation; | |
3208 | } | |
3209 | ||
12ba780d AG |
3210 | /* Find any alias of inode, but prefer a hashed alias */ |
3211 | static struct dentry *shmem_find_alias(struct inode *inode) | |
3212 | { | |
3213 | struct dentry *alias = d_find_alias(inode); | |
3214 | ||
3215 | return alias ?: d_find_any_alias(inode); | |
3216 | } | |
3217 | ||
3218 | ||
480b116c CH |
3219 | static struct dentry *shmem_fh_to_dentry(struct super_block *sb, |
3220 | struct fid *fid, int fh_len, int fh_type) | |
91828a40 | 3221 | { |
91828a40 | 3222 | struct inode *inode; |
480b116c | 3223 | struct dentry *dentry = NULL; |
35c2a7f4 | 3224 | u64 inum; |
480b116c CH |
3225 | |
3226 | if (fh_len < 3) | |
3227 | return NULL; | |
91828a40 | 3228 | |
35c2a7f4 HD |
3229 | inum = fid->raw[2]; |
3230 | inum = (inum << 32) | fid->raw[1]; | |
3231 | ||
480b116c CH |
3232 | inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), |
3233 | shmem_match, fid->raw); | |
91828a40 | 3234 | if (inode) { |
12ba780d | 3235 | dentry = shmem_find_alias(inode); |
91828a40 DG |
3236 | iput(inode); |
3237 | } | |
3238 | ||
480b116c | 3239 | return dentry; |
91828a40 DG |
3240 | } |
3241 | ||
b0b0382b AV |
3242 | static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, |
3243 | struct inode *parent) | |
91828a40 | 3244 | { |
5fe0c237 AK |
3245 | if (*len < 3) { |
3246 | *len = 3; | |
94e07a75 | 3247 | return FILEID_INVALID; |
5fe0c237 | 3248 | } |
91828a40 | 3249 | |
1d3382cb | 3250 | if (inode_unhashed(inode)) { |
91828a40 DG |
3251 | /* Unfortunately insert_inode_hash is not idempotent, |
3252 | * so as we hash inodes here rather than at creation | |
3253 | * time, we need a lock to ensure we only try | |
3254 | * to do it once | |
3255 | */ | |
3256 | static DEFINE_SPINLOCK(lock); | |
3257 | spin_lock(&lock); | |
1d3382cb | 3258 | if (inode_unhashed(inode)) |
91828a40 DG |
3259 | __insert_inode_hash(inode, |
3260 | inode->i_ino + inode->i_generation); | |
3261 | spin_unlock(&lock); | |
3262 | } | |
3263 | ||
3264 | fh[0] = inode->i_generation; | |
3265 | fh[1] = inode->i_ino; | |
3266 | fh[2] = ((__u64)inode->i_ino) >> 32; | |
3267 | ||
3268 | *len = 3; | |
3269 | return 1; | |
3270 | } | |
3271 | ||
39655164 | 3272 | static const struct export_operations shmem_export_ops = { |
91828a40 | 3273 | .get_parent = shmem_get_parent, |
91828a40 | 3274 | .encode_fh = shmem_encode_fh, |
480b116c | 3275 | .fh_to_dentry = shmem_fh_to_dentry, |
91828a40 DG |
3276 | }; |
3277 | ||
680d794b AM |
3278 | static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, |
3279 | bool remount) | |
1da177e4 LT |
3280 | { |
3281 | char *this_char, *value, *rest; | |
49cd0a5c | 3282 | struct mempolicy *mpol = NULL; |
8751e039 EB |
3283 | uid_t uid; |
3284 | gid_t gid; | |
1da177e4 | 3285 | |
b00dc3ad HD |
3286 | while (options != NULL) { |
3287 | this_char = options; | |
3288 | for (;;) { | |
3289 | /* | |
3290 | * NUL-terminate this option: unfortunately, | |
3291 | * mount options form a comma-separated list, | |
3292 | * but mpol's nodelist may also contain commas. | |
3293 | */ | |
3294 | options = strchr(options, ','); | |
3295 | if (options == NULL) | |
3296 | break; | |
3297 | options++; | |
3298 | if (!isdigit(*options)) { | |
3299 | options[-1] = '\0'; | |
3300 | break; | |
3301 | } | |
3302 | } | |
1da177e4 LT |
3303 | if (!*this_char) |
3304 | continue; | |
3305 | if ((value = strchr(this_char,'=')) != NULL) { | |
3306 | *value++ = 0; | |
3307 | } else { | |
1170532b JP |
3308 | pr_err("tmpfs: No value for mount option '%s'\n", |
3309 | this_char); | |
49cd0a5c | 3310 | goto error; |
1da177e4 LT |
3311 | } |
3312 | ||
3313 | if (!strcmp(this_char,"size")) { | |
3314 | unsigned long long size; | |
3315 | size = memparse(value,&rest); | |
3316 | if (*rest == '%') { | |
3317 | size <<= PAGE_SHIFT; | |
3318 | size *= totalram_pages; | |
3319 | do_div(size, 100); | |
3320 | rest++; | |
3321 | } | |
3322 | if (*rest) | |
3323 | goto bad_val; | |
680d794b | 3324 | sbinfo->max_blocks = |
09cbfeaf | 3325 | DIV_ROUND_UP(size, PAGE_SIZE); |
1da177e4 | 3326 | } else if (!strcmp(this_char,"nr_blocks")) { |
680d794b | 3327 | sbinfo->max_blocks = memparse(value, &rest); |
1da177e4 LT |
3328 | if (*rest) |
3329 | goto bad_val; | |
3330 | } else if (!strcmp(this_char,"nr_inodes")) { | |
680d794b | 3331 | sbinfo->max_inodes = memparse(value, &rest); |
1da177e4 LT |
3332 | if (*rest) |
3333 | goto bad_val; | |
3334 | } else if (!strcmp(this_char,"mode")) { | |
680d794b | 3335 | if (remount) |
1da177e4 | 3336 | continue; |
680d794b | 3337 | sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; |
1da177e4 LT |
3338 | if (*rest) |
3339 | goto bad_val; | |
3340 | } else if (!strcmp(this_char,"uid")) { | |
680d794b | 3341 | if (remount) |
1da177e4 | 3342 | continue; |
8751e039 | 3343 | uid = simple_strtoul(value, &rest, 0); |
1da177e4 LT |
3344 | if (*rest) |
3345 | goto bad_val; | |
8751e039 EB |
3346 | sbinfo->uid = make_kuid(current_user_ns(), uid); |
3347 | if (!uid_valid(sbinfo->uid)) | |
3348 | goto bad_val; | |
1da177e4 | 3349 | } else if (!strcmp(this_char,"gid")) { |
680d794b | 3350 | if (remount) |
1da177e4 | 3351 | continue; |
8751e039 | 3352 | gid = simple_strtoul(value, &rest, 0); |
1da177e4 LT |
3353 | if (*rest) |
3354 | goto bad_val; | |
8751e039 EB |
3355 | sbinfo->gid = make_kgid(current_user_ns(), gid); |
3356 | if (!gid_valid(sbinfo->gid)) | |
3357 | goto bad_val; | |
e496cf3d | 3358 | #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE |
5a6e75f8 KS |
3359 | } else if (!strcmp(this_char, "huge")) { |
3360 | int huge; | |
3361 | huge = shmem_parse_huge(value); | |
3362 | if (huge < 0) | |
3363 | goto bad_val; | |
3364 | if (!has_transparent_hugepage() && | |
3365 | huge != SHMEM_HUGE_NEVER) | |
3366 | goto bad_val; | |
3367 | sbinfo->huge = huge; | |
3368 | #endif | |
3369 | #ifdef CONFIG_NUMA | |
7339ff83 | 3370 | } else if (!strcmp(this_char,"mpol")) { |
49cd0a5c GT |
3371 | mpol_put(mpol); |
3372 | mpol = NULL; | |
3373 | if (mpol_parse_str(value, &mpol)) | |
7339ff83 | 3374 | goto bad_val; |
5a6e75f8 | 3375 | #endif |
1da177e4 | 3376 | } else { |
1170532b | 3377 | pr_err("tmpfs: Bad mount option %s\n", this_char); |
49cd0a5c | 3378 | goto error; |
1da177e4 LT |
3379 | } |
3380 | } | |
49cd0a5c | 3381 | sbinfo->mpol = mpol; |
1da177e4 LT |
3382 | return 0; |
3383 | ||
3384 | bad_val: | |
1170532b | 3385 | pr_err("tmpfs: Bad value '%s' for mount option '%s'\n", |
1da177e4 | 3386 | value, this_char); |
49cd0a5c GT |
3387 | error: |
3388 | mpol_put(mpol); | |
1da177e4 LT |
3389 | return 1; |
3390 | ||
3391 | } | |
3392 | ||
3393 | static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) | |
3394 | { | |
3395 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
680d794b | 3396 | struct shmem_sb_info config = *sbinfo; |
0edd73b3 HD |
3397 | unsigned long inodes; |
3398 | int error = -EINVAL; | |
3399 | ||
5f00110f | 3400 | config.mpol = NULL; |
680d794b | 3401 | if (shmem_parse_options(data, &config, true)) |
0edd73b3 | 3402 | return error; |
1da177e4 | 3403 | |
0edd73b3 | 3404 | spin_lock(&sbinfo->stat_lock); |
0edd73b3 | 3405 | inodes = sbinfo->max_inodes - sbinfo->free_inodes; |
7e496299 | 3406 | if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) |
0edd73b3 | 3407 | goto out; |
680d794b | 3408 | if (config.max_inodes < inodes) |
0edd73b3 HD |
3409 | goto out; |
3410 | /* | |
54af6042 | 3411 | * Those tests disallow limited->unlimited while any are in use; |
0edd73b3 HD |
3412 | * but we must separately disallow unlimited->limited, because |
3413 | * in that case we have no record of how much is already in use. | |
3414 | */ | |
680d794b | 3415 | if (config.max_blocks && !sbinfo->max_blocks) |
0edd73b3 | 3416 | goto out; |
680d794b | 3417 | if (config.max_inodes && !sbinfo->max_inodes) |
0edd73b3 HD |
3418 | goto out; |
3419 | ||
3420 | error = 0; | |
5a6e75f8 | 3421 | sbinfo->huge = config.huge; |
680d794b | 3422 | sbinfo->max_blocks = config.max_blocks; |
680d794b AM |
3423 | sbinfo->max_inodes = config.max_inodes; |
3424 | sbinfo->free_inodes = config.max_inodes - inodes; | |
71fe804b | 3425 | |
5f00110f GT |
3426 | /* |
3427 | * Preserve previous mempolicy unless mpol remount option was specified. | |
3428 | */ | |
3429 | if (config.mpol) { | |
3430 | mpol_put(sbinfo->mpol); | |
3431 | sbinfo->mpol = config.mpol; /* transfers initial ref */ | |
3432 | } | |
0edd73b3 HD |
3433 | out: |
3434 | spin_unlock(&sbinfo->stat_lock); | |
3435 | return error; | |
1da177e4 | 3436 | } |
680d794b | 3437 | |
34c80b1d | 3438 | static int shmem_show_options(struct seq_file *seq, struct dentry *root) |
680d794b | 3439 | { |
34c80b1d | 3440 | struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); |
680d794b AM |
3441 | |
3442 | if (sbinfo->max_blocks != shmem_default_max_blocks()) | |
3443 | seq_printf(seq, ",size=%luk", | |
09cbfeaf | 3444 | sbinfo->max_blocks << (PAGE_SHIFT - 10)); |
680d794b AM |
3445 | if (sbinfo->max_inodes != shmem_default_max_inodes()) |
3446 | seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); | |
0825a6f9 | 3447 | if (sbinfo->mode != (0777 | S_ISVTX)) |
09208d15 | 3448 | seq_printf(seq, ",mode=%03ho", sbinfo->mode); |
8751e039 EB |
3449 | if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) |
3450 | seq_printf(seq, ",uid=%u", | |
3451 | from_kuid_munged(&init_user_ns, sbinfo->uid)); | |
3452 | if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) | |
3453 | seq_printf(seq, ",gid=%u", | |
3454 | from_kgid_munged(&init_user_ns, sbinfo->gid)); | |
e496cf3d | 3455 | #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE |
5a6e75f8 KS |
3456 | /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ |
3457 | if (sbinfo->huge) | |
3458 | seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); | |
3459 | #endif | |
71fe804b | 3460 | shmem_show_mpol(seq, sbinfo->mpol); |
680d794b AM |
3461 | return 0; |
3462 | } | |
9183df25 | 3463 | |
680d794b | 3464 | #endif /* CONFIG_TMPFS */ |
1da177e4 LT |
3465 | |
3466 | static void shmem_put_super(struct super_block *sb) | |
3467 | { | |
602586a8 HD |
3468 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
3469 | ||
3470 | percpu_counter_destroy(&sbinfo->used_blocks); | |
49cd0a5c | 3471 | mpol_put(sbinfo->mpol); |
602586a8 | 3472 | kfree(sbinfo); |
1da177e4 LT |
3473 | sb->s_fs_info = NULL; |
3474 | } | |
3475 | ||
2b2af54a | 3476 | int shmem_fill_super(struct super_block *sb, void *data, int silent) |
1da177e4 LT |
3477 | { |
3478 | struct inode *inode; | |
0edd73b3 | 3479 | struct shmem_sb_info *sbinfo; |
680d794b AM |
3480 | int err = -ENOMEM; |
3481 | ||
3482 | /* Round up to L1_CACHE_BYTES to resist false sharing */ | |
425fbf04 | 3483 | sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), |
680d794b AM |
3484 | L1_CACHE_BYTES), GFP_KERNEL); |
3485 | if (!sbinfo) | |
3486 | return -ENOMEM; | |
3487 | ||
0825a6f9 | 3488 | sbinfo->mode = 0777 | S_ISVTX; |
76aac0e9 DH |
3489 | sbinfo->uid = current_fsuid(); |
3490 | sbinfo->gid = current_fsgid(); | |
680d794b | 3491 | sb->s_fs_info = sbinfo; |
1da177e4 | 3492 | |
0edd73b3 | 3493 | #ifdef CONFIG_TMPFS |
1da177e4 LT |
3494 | /* |
3495 | * Per default we only allow half of the physical ram per | |
3496 | * tmpfs instance, limiting inodes to one per page of lowmem; | |
3497 | * but the internal instance is left unlimited. | |
3498 | */ | |
1751e8a6 | 3499 | if (!(sb->s_flags & SB_KERNMOUNT)) { |
680d794b AM |
3500 | sbinfo->max_blocks = shmem_default_max_blocks(); |
3501 | sbinfo->max_inodes = shmem_default_max_inodes(); | |
3502 | if (shmem_parse_options(data, sbinfo, false)) { | |
3503 | err = -EINVAL; | |
3504 | goto failed; | |
3505 | } | |
ca4e0519 | 3506 | } else { |
1751e8a6 | 3507 | sb->s_flags |= SB_NOUSER; |
1da177e4 | 3508 | } |
91828a40 | 3509 | sb->s_export_op = &shmem_export_ops; |
1751e8a6 | 3510 | sb->s_flags |= SB_NOSEC; |
1da177e4 | 3511 | #else |
1751e8a6 | 3512 | sb->s_flags |= SB_NOUSER; |
1da177e4 LT |
3513 | #endif |
3514 | ||
0edd73b3 | 3515 | spin_lock_init(&sbinfo->stat_lock); |
908c7f19 | 3516 | if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) |
602586a8 | 3517 | goto failed; |
680d794b | 3518 | sbinfo->free_inodes = sbinfo->max_inodes; |
779750d2 KS |
3519 | spin_lock_init(&sbinfo->shrinklist_lock); |
3520 | INIT_LIST_HEAD(&sbinfo->shrinklist); | |
0edd73b3 | 3521 | |
285b2c4f | 3522 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
09cbfeaf KS |
3523 | sb->s_blocksize = PAGE_SIZE; |
3524 | sb->s_blocksize_bits = PAGE_SHIFT; | |
1da177e4 LT |
3525 | sb->s_magic = TMPFS_MAGIC; |
3526 | sb->s_op = &shmem_ops; | |
cfd95a9c | 3527 | sb->s_time_gran = 1; |
b09e0fa4 | 3528 | #ifdef CONFIG_TMPFS_XATTR |
39f0247d | 3529 | sb->s_xattr = shmem_xattr_handlers; |
b09e0fa4 EP |
3530 | #endif |
3531 | #ifdef CONFIG_TMPFS_POSIX_ACL | |
1751e8a6 | 3532 | sb->s_flags |= SB_POSIXACL; |
39f0247d | 3533 | #endif |
2b4db796 | 3534 | uuid_gen(&sb->s_uuid); |
0edd73b3 | 3535 | |
454abafe | 3536 | inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); |
1da177e4 LT |
3537 | if (!inode) |
3538 | goto failed; | |
680d794b AM |
3539 | inode->i_uid = sbinfo->uid; |
3540 | inode->i_gid = sbinfo->gid; | |
318ceed0 AV |
3541 | sb->s_root = d_make_root(inode); |
3542 | if (!sb->s_root) | |
48fde701 | 3543 | goto failed; |
1da177e4 LT |
3544 | return 0; |
3545 | ||
1da177e4 LT |
3546 | failed: |
3547 | shmem_put_super(sb); | |
3548 | return err; | |
3549 | } | |
3550 | ||
fcc234f8 | 3551 | static struct kmem_cache *shmem_inode_cachep; |
1da177e4 LT |
3552 | |
3553 | static struct inode *shmem_alloc_inode(struct super_block *sb) | |
3554 | { | |
41ffe5d5 HD |
3555 | struct shmem_inode_info *info; |
3556 | info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); | |
3557 | if (!info) | |
1da177e4 | 3558 | return NULL; |
41ffe5d5 | 3559 | return &info->vfs_inode; |
1da177e4 LT |
3560 | } |
3561 | ||
41ffe5d5 | 3562 | static void shmem_destroy_callback(struct rcu_head *head) |
fa0d7e3d NP |
3563 | { |
3564 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
84e710da AV |
3565 | if (S_ISLNK(inode->i_mode)) |
3566 | kfree(inode->i_link); | |
fa0d7e3d NP |
3567 | kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); |
3568 | } | |
3569 | ||
1da177e4 LT |
3570 | static void shmem_destroy_inode(struct inode *inode) |
3571 | { | |
09208d15 | 3572 | if (S_ISREG(inode->i_mode)) |
1da177e4 | 3573 | mpol_free_shared_policy(&SHMEM_I(inode)->policy); |
41ffe5d5 | 3574 | call_rcu(&inode->i_rcu, shmem_destroy_callback); |
1da177e4 LT |
3575 | } |
3576 | ||
41ffe5d5 | 3577 | static void shmem_init_inode(void *foo) |
1da177e4 | 3578 | { |
41ffe5d5 HD |
3579 | struct shmem_inode_info *info = foo; |
3580 | inode_init_once(&info->vfs_inode); | |
1da177e4 LT |
3581 | } |
3582 | ||
9a8ec03e | 3583 | static void shmem_init_inodecache(void) |
1da177e4 LT |
3584 | { |
3585 | shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", | |
3586 | sizeof(struct shmem_inode_info), | |
5d097056 | 3587 | 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); |
1da177e4 LT |
3588 | } |
3589 | ||
41ffe5d5 | 3590 | static void shmem_destroy_inodecache(void) |
1da177e4 | 3591 | { |
1a1d92c1 | 3592 | kmem_cache_destroy(shmem_inode_cachep); |
1da177e4 LT |
3593 | } |
3594 | ||
f5e54d6e | 3595 | static const struct address_space_operations shmem_aops = { |
1da177e4 | 3596 | .writepage = shmem_writepage, |
76719325 | 3597 | .set_page_dirty = __set_page_dirty_no_writeback, |
1da177e4 | 3598 | #ifdef CONFIG_TMPFS |
800d15a5 NP |
3599 | .write_begin = shmem_write_begin, |
3600 | .write_end = shmem_write_end, | |
1da177e4 | 3601 | #endif |
1c93923c | 3602 | #ifdef CONFIG_MIGRATION |
304dbdb7 | 3603 | .migratepage = migrate_page, |
1c93923c | 3604 | #endif |
aa261f54 | 3605 | .error_remove_page = generic_error_remove_page, |
1da177e4 LT |
3606 | }; |
3607 | ||
15ad7cdc | 3608 | static const struct file_operations shmem_file_operations = { |
1da177e4 | 3609 | .mmap = shmem_mmap, |
c01d5b30 | 3610 | .get_unmapped_area = shmem_get_unmapped_area, |
1da177e4 | 3611 | #ifdef CONFIG_TMPFS |
220f2ac9 | 3612 | .llseek = shmem_file_llseek, |
2ba5bbed | 3613 | .read_iter = shmem_file_read_iter, |
8174202b | 3614 | .write_iter = generic_file_write_iter, |
1b061d92 | 3615 | .fsync = noop_fsync, |
82c156f8 | 3616 | .splice_read = generic_file_splice_read, |
f6cb85d0 | 3617 | .splice_write = iter_file_splice_write, |
83e4fa9c | 3618 | .fallocate = shmem_fallocate, |
1da177e4 LT |
3619 | #endif |
3620 | }; | |
3621 | ||
92e1d5be | 3622 | static const struct inode_operations shmem_inode_operations = { |
44a30220 | 3623 | .getattr = shmem_getattr, |
94c1e62d | 3624 | .setattr = shmem_setattr, |
b09e0fa4 | 3625 | #ifdef CONFIG_TMPFS_XATTR |
b09e0fa4 | 3626 | .listxattr = shmem_listxattr, |
feda821e | 3627 | .set_acl = simple_set_acl, |
b09e0fa4 | 3628 | #endif |
1da177e4 LT |
3629 | }; |
3630 | ||
92e1d5be | 3631 | static const struct inode_operations shmem_dir_inode_operations = { |
1da177e4 LT |
3632 | #ifdef CONFIG_TMPFS |
3633 | .create = shmem_create, | |
3634 | .lookup = simple_lookup, | |
3635 | .link = shmem_link, | |
3636 | .unlink = shmem_unlink, | |
3637 | .symlink = shmem_symlink, | |
3638 | .mkdir = shmem_mkdir, | |
3639 | .rmdir = shmem_rmdir, | |
3640 | .mknod = shmem_mknod, | |
2773bf00 | 3641 | .rename = shmem_rename2, |
60545d0d | 3642 | .tmpfile = shmem_tmpfile, |
1da177e4 | 3643 | #endif |
b09e0fa4 | 3644 | #ifdef CONFIG_TMPFS_XATTR |
b09e0fa4 | 3645 | .listxattr = shmem_listxattr, |
b09e0fa4 | 3646 | #endif |
39f0247d | 3647 | #ifdef CONFIG_TMPFS_POSIX_ACL |
94c1e62d | 3648 | .setattr = shmem_setattr, |
feda821e | 3649 | .set_acl = simple_set_acl, |
39f0247d AG |
3650 | #endif |
3651 | }; | |
3652 | ||
92e1d5be | 3653 | static const struct inode_operations shmem_special_inode_operations = { |
b09e0fa4 | 3654 | #ifdef CONFIG_TMPFS_XATTR |
b09e0fa4 | 3655 | .listxattr = shmem_listxattr, |
b09e0fa4 | 3656 | #endif |
39f0247d | 3657 | #ifdef CONFIG_TMPFS_POSIX_ACL |
94c1e62d | 3658 | .setattr = shmem_setattr, |
feda821e | 3659 | .set_acl = simple_set_acl, |
39f0247d | 3660 | #endif |
1da177e4 LT |
3661 | }; |
3662 | ||
759b9775 | 3663 | static const struct super_operations shmem_ops = { |
1da177e4 LT |
3664 | .alloc_inode = shmem_alloc_inode, |
3665 | .destroy_inode = shmem_destroy_inode, | |
3666 | #ifdef CONFIG_TMPFS | |
3667 | .statfs = shmem_statfs, | |
3668 | .remount_fs = shmem_remount_fs, | |
680d794b | 3669 | .show_options = shmem_show_options, |
1da177e4 | 3670 | #endif |
1f895f75 | 3671 | .evict_inode = shmem_evict_inode, |
1da177e4 LT |
3672 | .drop_inode = generic_delete_inode, |
3673 | .put_super = shmem_put_super, | |
779750d2 KS |
3674 | #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE |
3675 | .nr_cached_objects = shmem_unused_huge_count, | |
3676 | .free_cached_objects = shmem_unused_huge_scan, | |
3677 | #endif | |
1da177e4 LT |
3678 | }; |
3679 | ||
f0f37e2f | 3680 | static const struct vm_operations_struct shmem_vm_ops = { |
54cb8821 | 3681 | .fault = shmem_fault, |
d7c17551 | 3682 | .map_pages = filemap_map_pages, |
1da177e4 LT |
3683 | #ifdef CONFIG_NUMA |
3684 | .set_policy = shmem_set_policy, | |
3685 | .get_policy = shmem_get_policy, | |
3686 | #endif | |
3687 | }; | |
3688 | ||
3c26ff6e AV |
3689 | static struct dentry *shmem_mount(struct file_system_type *fs_type, |
3690 | int flags, const char *dev_name, void *data) | |
1da177e4 | 3691 | { |
3c26ff6e | 3692 | return mount_nodev(fs_type, flags, data, shmem_fill_super); |
1da177e4 LT |
3693 | } |
3694 | ||
41ffe5d5 | 3695 | static struct file_system_type shmem_fs_type = { |
1da177e4 LT |
3696 | .owner = THIS_MODULE, |
3697 | .name = "tmpfs", | |
3c26ff6e | 3698 | .mount = shmem_mount, |
1da177e4 | 3699 | .kill_sb = kill_litter_super, |
2b8576cb | 3700 | .fs_flags = FS_USERNS_MOUNT, |
1da177e4 | 3701 | }; |
1da177e4 | 3702 | |
41ffe5d5 | 3703 | int __init shmem_init(void) |
1da177e4 LT |
3704 | { |
3705 | int error; | |
3706 | ||
16203a7a RL |
3707 | /* If rootfs called this, don't re-init */ |
3708 | if (shmem_inode_cachep) | |
3709 | return 0; | |
3710 | ||
9a8ec03e | 3711 | shmem_init_inodecache(); |
1da177e4 | 3712 | |
41ffe5d5 | 3713 | error = register_filesystem(&shmem_fs_type); |
1da177e4 | 3714 | if (error) { |
1170532b | 3715 | pr_err("Could not register tmpfs\n"); |
1da177e4 LT |
3716 | goto out2; |
3717 | } | |
95dc112a | 3718 | |
ca4e0519 | 3719 | shm_mnt = kern_mount(&shmem_fs_type); |
1da177e4 LT |
3720 | if (IS_ERR(shm_mnt)) { |
3721 | error = PTR_ERR(shm_mnt); | |
1170532b | 3722 | pr_err("Could not kern_mount tmpfs\n"); |
1da177e4 LT |
3723 | goto out1; |
3724 | } | |
5a6e75f8 | 3725 | |
e496cf3d | 3726 | #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE |
435c0b87 | 3727 | if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY) |
5a6e75f8 KS |
3728 | SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; |
3729 | else | |
3730 | shmem_huge = 0; /* just in case it was patched */ | |
3731 | #endif | |
1da177e4 LT |
3732 | return 0; |
3733 | ||
3734 | out1: | |
41ffe5d5 | 3735 | unregister_filesystem(&shmem_fs_type); |
1da177e4 | 3736 | out2: |
41ffe5d5 | 3737 | shmem_destroy_inodecache(); |
1da177e4 LT |
3738 | shm_mnt = ERR_PTR(error); |
3739 | return error; | |
3740 | } | |
853ac43a | 3741 | |
e496cf3d | 3742 | #if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS) |
5a6e75f8 KS |
3743 | static ssize_t shmem_enabled_show(struct kobject *kobj, |
3744 | struct kobj_attribute *attr, char *buf) | |
3745 | { | |
3746 | int values[] = { | |
3747 | SHMEM_HUGE_ALWAYS, | |
3748 | SHMEM_HUGE_WITHIN_SIZE, | |
3749 | SHMEM_HUGE_ADVISE, | |
3750 | SHMEM_HUGE_NEVER, | |
3751 | SHMEM_HUGE_DENY, | |
3752 | SHMEM_HUGE_FORCE, | |
3753 | }; | |
3754 | int i, count; | |
3755 | ||
3756 | for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) { | |
3757 | const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s "; | |
3758 | ||
3759 | count += sprintf(buf + count, fmt, | |
3760 | shmem_format_huge(values[i])); | |
3761 | } | |
3762 | buf[count - 1] = '\n'; | |
3763 | return count; | |
3764 | } | |
3765 | ||
3766 | static ssize_t shmem_enabled_store(struct kobject *kobj, | |
3767 | struct kobj_attribute *attr, const char *buf, size_t count) | |
3768 | { | |
3769 | char tmp[16]; | |
3770 | int huge; | |
3771 | ||
3772 | if (count + 1 > sizeof(tmp)) | |
3773 | return -EINVAL; | |
3774 | memcpy(tmp, buf, count); | |
3775 | tmp[count] = '\0'; | |
3776 | if (count && tmp[count - 1] == '\n') | |
3777 | tmp[count - 1] = '\0'; | |
3778 | ||
3779 | huge = shmem_parse_huge(tmp); | |
3780 | if (huge == -EINVAL) | |
3781 | return -EINVAL; | |
3782 | if (!has_transparent_hugepage() && | |
3783 | huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) | |
3784 | return -EINVAL; | |
3785 | ||
3786 | shmem_huge = huge; | |
435c0b87 | 3787 | if (shmem_huge > SHMEM_HUGE_DENY) |
5a6e75f8 KS |
3788 | SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; |
3789 | return count; | |
3790 | } | |
3791 | ||
3792 | struct kobj_attribute shmem_enabled_attr = | |
3793 | __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store); | |
3b33719c | 3794 | #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */ |
f3f0e1d2 | 3795 | |
3b33719c | 3796 | #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE |
f3f0e1d2 KS |
3797 | bool shmem_huge_enabled(struct vm_area_struct *vma) |
3798 | { | |
3799 | struct inode *inode = file_inode(vma->vm_file); | |
3800 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); | |
3801 | loff_t i_size; | |
3802 | pgoff_t off; | |
3803 | ||
3804 | if (shmem_huge == SHMEM_HUGE_FORCE) | |
3805 | return true; | |
3806 | if (shmem_huge == SHMEM_HUGE_DENY) | |
3807 | return false; | |
3808 | switch (sbinfo->huge) { | |
3809 | case SHMEM_HUGE_NEVER: | |
3810 | return false; | |
3811 | case SHMEM_HUGE_ALWAYS: | |
3812 | return true; | |
3813 | case SHMEM_HUGE_WITHIN_SIZE: | |
3814 | off = round_up(vma->vm_pgoff, HPAGE_PMD_NR); | |
3815 | i_size = round_up(i_size_read(inode), PAGE_SIZE); | |
3816 | if (i_size >= HPAGE_PMD_SIZE && | |
3817 | i_size >> PAGE_SHIFT >= off) | |
3818 | return true; | |
c8402871 | 3819 | /* fall through */ |
f3f0e1d2 KS |
3820 | case SHMEM_HUGE_ADVISE: |
3821 | /* TODO: implement fadvise() hints */ | |
3822 | return (vma->vm_flags & VM_HUGEPAGE); | |
3823 | default: | |
3824 | VM_BUG_ON(1); | |
3825 | return false; | |
3826 | } | |
3827 | } | |
3b33719c | 3828 | #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */ |
5a6e75f8 | 3829 | |
853ac43a MM |
3830 | #else /* !CONFIG_SHMEM */ |
3831 | ||
3832 | /* | |
3833 | * tiny-shmem: simple shmemfs and tmpfs using ramfs code | |
3834 | * | |
3835 | * This is intended for small system where the benefits of the full | |
3836 | * shmem code (swap-backed and resource-limited) are outweighed by | |
3837 | * their complexity. On systems without swap this code should be | |
3838 | * effectively equivalent, but much lighter weight. | |
3839 | */ | |
3840 | ||
41ffe5d5 | 3841 | static struct file_system_type shmem_fs_type = { |
853ac43a | 3842 | .name = "tmpfs", |
3c26ff6e | 3843 | .mount = ramfs_mount, |
853ac43a | 3844 | .kill_sb = kill_litter_super, |
2b8576cb | 3845 | .fs_flags = FS_USERNS_MOUNT, |
853ac43a MM |
3846 | }; |
3847 | ||
41ffe5d5 | 3848 | int __init shmem_init(void) |
853ac43a | 3849 | { |
41ffe5d5 | 3850 | BUG_ON(register_filesystem(&shmem_fs_type) != 0); |
853ac43a | 3851 | |
41ffe5d5 | 3852 | shm_mnt = kern_mount(&shmem_fs_type); |
853ac43a MM |
3853 | BUG_ON(IS_ERR(shm_mnt)); |
3854 | ||
3855 | return 0; | |
3856 | } | |
3857 | ||
41ffe5d5 | 3858 | int shmem_unuse(swp_entry_t swap, struct page *page) |
853ac43a MM |
3859 | { |
3860 | return 0; | |
3861 | } | |
3862 | ||
3f96b79a HD |
3863 | int shmem_lock(struct file *file, int lock, struct user_struct *user) |
3864 | { | |
3865 | return 0; | |
3866 | } | |
3867 | ||
24513264 HD |
3868 | void shmem_unlock_mapping(struct address_space *mapping) |
3869 | { | |
3870 | } | |
3871 | ||
c01d5b30 HD |
3872 | #ifdef CONFIG_MMU |
3873 | unsigned long shmem_get_unmapped_area(struct file *file, | |
3874 | unsigned long addr, unsigned long len, | |
3875 | unsigned long pgoff, unsigned long flags) | |
3876 | { | |
3877 | return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); | |
3878 | } | |
3879 | #endif | |
3880 | ||
41ffe5d5 | 3881 | void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) |
94c1e62d | 3882 | { |
41ffe5d5 | 3883 | truncate_inode_pages_range(inode->i_mapping, lstart, lend); |
94c1e62d HD |
3884 | } |
3885 | EXPORT_SYMBOL_GPL(shmem_truncate_range); | |
3886 | ||
0b0a0806 HD |
3887 | #define shmem_vm_ops generic_file_vm_ops |
3888 | #define shmem_file_operations ramfs_file_operations | |
454abafe | 3889 | #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) |
0b0a0806 HD |
3890 | #define shmem_acct_size(flags, size) 0 |
3891 | #define shmem_unacct_size(flags, size) do {} while (0) | |
853ac43a MM |
3892 | |
3893 | #endif /* CONFIG_SHMEM */ | |
3894 | ||
3895 | /* common code */ | |
1da177e4 | 3896 | |
703321b6 | 3897 | static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size, |
c7277090 | 3898 | unsigned long flags, unsigned int i_flags) |
1da177e4 | 3899 | { |
1da177e4 | 3900 | struct inode *inode; |
93dec2da | 3901 | struct file *res; |
1da177e4 | 3902 | |
703321b6 MA |
3903 | if (IS_ERR(mnt)) |
3904 | return ERR_CAST(mnt); | |
1da177e4 | 3905 | |
285b2c4f | 3906 | if (size < 0 || size > MAX_LFS_FILESIZE) |
1da177e4 LT |
3907 | return ERR_PTR(-EINVAL); |
3908 | ||
3909 | if (shmem_acct_size(flags, size)) | |
3910 | return ERR_PTR(-ENOMEM); | |
3911 | ||
93dec2da AV |
3912 | inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0, |
3913 | flags); | |
dac2d1f6 AV |
3914 | if (unlikely(!inode)) { |
3915 | shmem_unacct_size(flags, size); | |
3916 | return ERR_PTR(-ENOSPC); | |
3917 | } | |
c7277090 | 3918 | inode->i_flags |= i_flags; |
1da177e4 | 3919 | inode->i_size = size; |
6d6b77f1 | 3920 | clear_nlink(inode); /* It is unlinked */ |
26567cdb | 3921 | res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); |
93dec2da AV |
3922 | if (!IS_ERR(res)) |
3923 | res = alloc_file_pseudo(inode, mnt, name, O_RDWR, | |
3924 | &shmem_file_operations); | |
26567cdb | 3925 | if (IS_ERR(res)) |
93dec2da | 3926 | iput(inode); |
6b4d0b27 | 3927 | return res; |
1da177e4 | 3928 | } |
c7277090 EP |
3929 | |
3930 | /** | |
3931 | * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be | |
3932 | * kernel internal. There will be NO LSM permission checks against the | |
3933 | * underlying inode. So users of this interface must do LSM checks at a | |
e1832f29 SS |
3934 | * higher layer. The users are the big_key and shm implementations. LSM |
3935 | * checks are provided at the key or shm level rather than the inode. | |
c7277090 EP |
3936 | * @name: name for dentry (to be seen in /proc/<pid>/maps |
3937 | * @size: size to be set for the file | |
3938 | * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size | |
3939 | */ | |
3940 | struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) | |
3941 | { | |
703321b6 | 3942 | return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE); |
c7277090 EP |
3943 | } |
3944 | ||
3945 | /** | |
3946 | * shmem_file_setup - get an unlinked file living in tmpfs | |
3947 | * @name: name for dentry (to be seen in /proc/<pid>/maps | |
3948 | * @size: size to be set for the file | |
3949 | * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size | |
3950 | */ | |
3951 | struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) | |
3952 | { | |
703321b6 | 3953 | return __shmem_file_setup(shm_mnt, name, size, flags, 0); |
c7277090 | 3954 | } |
395e0ddc | 3955 | EXPORT_SYMBOL_GPL(shmem_file_setup); |
1da177e4 | 3956 | |
703321b6 MA |
3957 | /** |
3958 | * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs | |
3959 | * @mnt: the tmpfs mount where the file will be created | |
3960 | * @name: name for dentry (to be seen in /proc/<pid>/maps | |
3961 | * @size: size to be set for the file | |
3962 | * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size | |
3963 | */ | |
3964 | struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name, | |
3965 | loff_t size, unsigned long flags) | |
3966 | { | |
3967 | return __shmem_file_setup(mnt, name, size, flags, 0); | |
3968 | } | |
3969 | EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt); | |
3970 | ||
46711810 | 3971 | /** |
1da177e4 | 3972 | * shmem_zero_setup - setup a shared anonymous mapping |
1da177e4 LT |
3973 | * @vma: the vma to be mmapped is prepared by do_mmap_pgoff |
3974 | */ | |
3975 | int shmem_zero_setup(struct vm_area_struct *vma) | |
3976 | { | |
3977 | struct file *file; | |
3978 | loff_t size = vma->vm_end - vma->vm_start; | |
3979 | ||
66fc1303 HD |
3980 | /* |
3981 | * Cloning a new file under mmap_sem leads to a lock ordering conflict | |
3982 | * between XFS directory reading and selinux: since this file is only | |
3983 | * accessible to the user through its mapping, use S_PRIVATE flag to | |
3984 | * bypass file security, in the same way as shmem_kernel_file_setup(). | |
3985 | */ | |
703321b6 | 3986 | file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags); |
1da177e4 LT |
3987 | if (IS_ERR(file)) |
3988 | return PTR_ERR(file); | |
3989 | ||
3990 | if (vma->vm_file) | |
3991 | fput(vma->vm_file); | |
3992 | vma->vm_file = file; | |
3993 | vma->vm_ops = &shmem_vm_ops; | |
f3f0e1d2 | 3994 | |
e496cf3d | 3995 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && |
f3f0e1d2 KS |
3996 | ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < |
3997 | (vma->vm_end & HPAGE_PMD_MASK)) { | |
3998 | khugepaged_enter(vma, vma->vm_flags); | |
3999 | } | |
4000 | ||
1da177e4 LT |
4001 | return 0; |
4002 | } | |
d9d90e5e HD |
4003 | |
4004 | /** | |
4005 | * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. | |
4006 | * @mapping: the page's address_space | |
4007 | * @index: the page index | |
4008 | * @gfp: the page allocator flags to use if allocating | |
4009 | * | |
4010 | * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", | |
4011 | * with any new page allocations done using the specified allocation flags. | |
4012 | * But read_cache_page_gfp() uses the ->readpage() method: which does not | |
4013 | * suit tmpfs, since it may have pages in swapcache, and needs to find those | |
4014 | * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. | |
4015 | * | |
68da9f05 HD |
4016 | * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in |
4017 | * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. | |
d9d90e5e HD |
4018 | */ |
4019 | struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, | |
4020 | pgoff_t index, gfp_t gfp) | |
4021 | { | |
68da9f05 HD |
4022 | #ifdef CONFIG_SHMEM |
4023 | struct inode *inode = mapping->host; | |
9276aad6 | 4024 | struct page *page; |
68da9f05 HD |
4025 | int error; |
4026 | ||
4027 | BUG_ON(mapping->a_ops != &shmem_aops); | |
9e18eb29 | 4028 | error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, |
cfda0526 | 4029 | gfp, NULL, NULL, NULL); |
68da9f05 HD |
4030 | if (error) |
4031 | page = ERR_PTR(error); | |
4032 | else | |
4033 | unlock_page(page); | |
4034 | return page; | |
4035 | #else | |
4036 | /* | |
4037 | * The tiny !SHMEM case uses ramfs without swap | |
4038 | */ | |
d9d90e5e | 4039 | return read_cache_page_gfp(mapping, index, gfp); |
68da9f05 | 4040 | #endif |
d9d90e5e HD |
4041 | } |
4042 | EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); |