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