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