2 * Resizable virtual memory filesystem for Linux.
4 * Copyright (C) 2000 Linus Torvalds.
6 * 2000-2001 Christoph Rohland
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
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>
19 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
21 * This file is released under the GPL.
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>
32 #include <linux/export.h>
33 #include <linux/swap.h>
34 #include <linux/aio.h>
36 static struct vfsmount
*shm_mnt
;
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.
45 #include <linux/xattr.h>
46 #include <linux/exportfs.h>
47 #include <linux/posix_acl.h>
48 #include <linux/generic_acl.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/syscalls.h>
69 #include <linux/magic.h>
70 #include <linux/fcntl.h>
71 #include <uapi/linux/memfd.h>
73 #include <asm/uaccess.h>
74 #include <asm/pgtable.h>
76 #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
77 #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
79 /* Pretend that each entry is of this size in directory's i_size */
80 #define BOGO_DIRENT_SIZE 20
82 /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
83 #define SHORT_SYMLINK_LEN 128
86 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
87 * inode->i_private (with i_mutex making sure that it has only one user at
88 * a time): we would prefer not to enlarge the shmem inode just for that.
91 wait_queue_head_t
*waitq
; /* faults into hole wait for punch to end */
92 pgoff_t start
; /* start of range currently being fallocated */
93 pgoff_t next
; /* the next page offset to be fallocated */
94 pgoff_t nr_falloced
; /* how many new pages have been fallocated */
95 pgoff_t nr_unswapped
; /* how often writepage refused to swap out */
98 /* Flag allocation requirements to shmem_getpage */
100 SGP_READ
, /* don't exceed i_size, don't allocate page */
101 SGP_CACHE
, /* don't exceed i_size, may allocate page */
102 SGP_DIRTY
, /* like SGP_CACHE, but set new page dirty */
103 SGP_WRITE
, /* may exceed i_size, may allocate !Uptodate page */
104 SGP_FALLOC
, /* like SGP_WRITE, but make existing page Uptodate */
108 static unsigned long shmem_default_max_blocks(void)
110 return totalram_pages
/ 2;
113 static unsigned long shmem_default_max_inodes(void)
115 return min(totalram_pages
- totalhigh_pages
, totalram_pages
/ 2);
119 static bool shmem_should_replace_page(struct page
*page
, gfp_t gfp
);
120 static int shmem_replace_page(struct page
**pagep
, gfp_t gfp
,
121 struct shmem_inode_info
*info
, pgoff_t index
);
122 static int shmem_getpage_gfp(struct inode
*inode
, pgoff_t index
,
123 struct page
**pagep
, enum sgp_type sgp
, gfp_t gfp
, int *fault_type
);
125 static inline int shmem_getpage(struct inode
*inode
, pgoff_t index
,
126 struct page
**pagep
, enum sgp_type sgp
, int *fault_type
)
128 return shmem_getpage_gfp(inode
, index
, pagep
, sgp
,
129 mapping_gfp_mask(inode
->i_mapping
), fault_type
);
132 static inline struct shmem_sb_info
*SHMEM_SB(struct super_block
*sb
)
134 return sb
->s_fs_info
;
138 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
139 * for shared memory and for shared anonymous (/dev/zero) mappings
140 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
141 * consistent with the pre-accounting of private mappings ...
143 static inline int shmem_acct_size(unsigned long flags
, loff_t size
)
145 return (flags
& VM_NORESERVE
) ?
146 0 : security_vm_enough_memory_mm(current
->mm
, VM_ACCT(size
));
149 static inline void shmem_unacct_size(unsigned long flags
, loff_t size
)
151 if (!(flags
& VM_NORESERVE
))
152 vm_unacct_memory(VM_ACCT(size
));
156 * ... whereas tmpfs objects are accounted incrementally as
157 * pages are allocated, in order to allow huge sparse files.
158 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
159 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
161 static inline int shmem_acct_block(unsigned long flags
)
163 return (flags
& VM_NORESERVE
) ?
164 security_vm_enough_memory_mm(current
->mm
, VM_ACCT(PAGE_CACHE_SIZE
)) : 0;
167 static inline void shmem_unacct_blocks(unsigned long flags
, long pages
)
169 if (flags
& VM_NORESERVE
)
170 vm_unacct_memory(pages
* VM_ACCT(PAGE_CACHE_SIZE
));
173 static const struct super_operations shmem_ops
;
174 static const struct address_space_operations shmem_aops
;
175 static const struct file_operations shmem_file_operations
;
176 static const struct inode_operations shmem_inode_operations
;
177 static const struct inode_operations shmem_dir_inode_operations
;
178 static const struct inode_operations shmem_special_inode_operations
;
179 static const struct vm_operations_struct shmem_vm_ops
;
181 static struct backing_dev_info shmem_backing_dev_info __read_mostly
= {
182 .ra_pages
= 0, /* No readahead */
183 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
| BDI_CAP_SWAP_BACKED
,
186 static LIST_HEAD(shmem_swaplist
);
187 static DEFINE_MUTEX(shmem_swaplist_mutex
);
189 static int shmem_reserve_inode(struct super_block
*sb
)
191 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
192 if (sbinfo
->max_inodes
) {
193 spin_lock(&sbinfo
->stat_lock
);
194 if (!sbinfo
->free_inodes
) {
195 spin_unlock(&sbinfo
->stat_lock
);
198 sbinfo
->free_inodes
--;
199 spin_unlock(&sbinfo
->stat_lock
);
204 static void shmem_free_inode(struct super_block
*sb
)
206 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
207 if (sbinfo
->max_inodes
) {
208 spin_lock(&sbinfo
->stat_lock
);
209 sbinfo
->free_inodes
++;
210 spin_unlock(&sbinfo
->stat_lock
);
215 * shmem_recalc_inode - recalculate the block usage of an inode
216 * @inode: inode to recalc
218 * We have to calculate the free blocks since the mm can drop
219 * undirtied hole pages behind our back.
221 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
222 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
224 * It has to be called with the spinlock held.
226 static void shmem_recalc_inode(struct inode
*inode
)
228 struct shmem_inode_info
*info
= SHMEM_I(inode
);
231 freed
= info
->alloced
- info
->swapped
- inode
->i_mapping
->nrpages
;
233 struct shmem_sb_info
*sbinfo
= SHMEM_SB(inode
->i_sb
);
234 if (sbinfo
->max_blocks
)
235 percpu_counter_add(&sbinfo
->used_blocks
, -freed
);
236 info
->alloced
-= freed
;
237 inode
->i_blocks
-= freed
* BLOCKS_PER_PAGE
;
238 shmem_unacct_blocks(info
->flags
, freed
);
243 * Replace item expected in radix tree by a new item, while holding tree lock.
245 static int shmem_radix_tree_replace(struct address_space
*mapping
,
246 pgoff_t index
, void *expected
, void *replacement
)
251 VM_BUG_ON(!expected
);
252 pslot
= radix_tree_lookup_slot(&mapping
->page_tree
, index
);
254 item
= radix_tree_deref_slot_protected(pslot
,
255 &mapping
->tree_lock
);
256 if (item
!= expected
)
259 radix_tree_replace_slot(pslot
, replacement
);
261 radix_tree_delete(&mapping
->page_tree
, index
);
266 * Sometimes, before we decide whether to proceed or to fail, we must check
267 * that an entry was not already brought back from swap by a racing thread.
269 * Checking page is not enough: by the time a SwapCache page is locked, it
270 * might be reused, and again be SwapCache, using the same swap as before.
272 static bool shmem_confirm_swap(struct address_space
*mapping
,
273 pgoff_t index
, swp_entry_t swap
)
278 item
= radix_tree_lookup(&mapping
->page_tree
, index
);
280 return item
== swp_to_radix_entry(swap
);
284 * Like add_to_page_cache_locked, but error if expected item has gone.
286 static int shmem_add_to_page_cache(struct page
*page
,
287 struct address_space
*mapping
,
288 pgoff_t index
, gfp_t gfp
, void *expected
)
292 VM_BUG_ON(!PageLocked(page
));
293 VM_BUG_ON(!PageSwapBacked(page
));
295 page_cache_get(page
);
296 page
->mapping
= mapping
;
299 spin_lock_irq(&mapping
->tree_lock
);
301 error
= radix_tree_insert(&mapping
->page_tree
, index
, page
);
303 error
= shmem_radix_tree_replace(mapping
, index
, expected
,
307 __inc_zone_page_state(page
, NR_FILE_PAGES
);
308 __inc_zone_page_state(page
, NR_SHMEM
);
309 spin_unlock_irq(&mapping
->tree_lock
);
311 page
->mapping
= NULL
;
312 spin_unlock_irq(&mapping
->tree_lock
);
313 page_cache_release(page
);
319 * Like delete_from_page_cache, but substitutes swap for page.
321 static void shmem_delete_from_page_cache(struct page
*page
, void *radswap
)
323 struct address_space
*mapping
= page
->mapping
;
326 spin_lock_irq(&mapping
->tree_lock
);
327 error
= shmem_radix_tree_replace(mapping
, page
->index
, page
, radswap
);
328 page
->mapping
= NULL
;
330 __dec_zone_page_state(page
, NR_FILE_PAGES
);
331 __dec_zone_page_state(page
, NR_SHMEM
);
332 spin_unlock_irq(&mapping
->tree_lock
);
333 page_cache_release(page
);
338 * Like find_get_pages, but collecting swap entries as well as pages.
340 static unsigned shmem_find_get_pages_and_swap(struct address_space
*mapping
,
341 pgoff_t start
, unsigned int nr_pages
,
342 struct page
**pages
, pgoff_t
*indices
)
345 unsigned int ret
= 0;
346 struct radix_tree_iter iter
;
353 radix_tree_for_each_slot(slot
, &mapping
->page_tree
, &iter
, start
) {
356 page
= radix_tree_deref_slot(slot
);
359 if (radix_tree_exception(page
)) {
360 if (radix_tree_deref_retry(page
))
363 * Otherwise, we must be storing a swap entry
364 * here as an exceptional entry: so return it
365 * without attempting to raise page count.
369 if (!page_cache_get_speculative(page
))
372 /* Has the page moved? */
373 if (unlikely(page
!= *slot
)) {
374 page_cache_release(page
);
378 indices
[ret
] = iter
.index
;
380 if (++ret
== nr_pages
)
388 * Remove swap entry from radix tree, free the swap and its page cache.
390 static int shmem_free_swap(struct address_space
*mapping
,
391 pgoff_t index
, void *radswap
)
395 spin_lock_irq(&mapping
->tree_lock
);
396 error
= shmem_radix_tree_replace(mapping
, index
, radswap
, NULL
);
397 spin_unlock_irq(&mapping
->tree_lock
);
399 free_swap_and_cache(radix_to_swp_entry(radswap
));
404 * Pagevec may contain swap entries, so shuffle up pages before releasing.
406 static void shmem_deswap_pagevec(struct pagevec
*pvec
)
410 for (i
= 0, j
= 0; i
< pagevec_count(pvec
); i
++) {
411 struct page
*page
= pvec
->pages
[i
];
412 if (!radix_tree_exceptional_entry(page
))
413 pvec
->pages
[j
++] = page
;
419 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
421 void shmem_unlock_mapping(struct address_space
*mapping
)
424 pgoff_t indices
[PAGEVEC_SIZE
];
427 pagevec_init(&pvec
, 0);
429 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
431 while (!mapping_unevictable(mapping
)) {
433 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
434 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
436 pvec
.nr
= shmem_find_get_pages_and_swap(mapping
, index
,
437 PAGEVEC_SIZE
, pvec
.pages
, indices
);
440 index
= indices
[pvec
.nr
- 1] + 1;
441 shmem_deswap_pagevec(&pvec
);
442 check_move_unevictable_pages(pvec
.pages
, pvec
.nr
);
443 pagevec_release(&pvec
);
449 * Remove range of pages and swap entries from radix tree, and free them.
450 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
452 static void shmem_undo_range(struct inode
*inode
, loff_t lstart
, loff_t lend
,
455 struct address_space
*mapping
= inode
->i_mapping
;
456 struct shmem_inode_info
*info
= SHMEM_I(inode
);
457 pgoff_t start
= (lstart
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
458 pgoff_t end
= (lend
+ 1) >> PAGE_CACHE_SHIFT
;
459 unsigned int partial_start
= lstart
& (PAGE_CACHE_SIZE
- 1);
460 unsigned int partial_end
= (lend
+ 1) & (PAGE_CACHE_SIZE
- 1);
462 pgoff_t indices
[PAGEVEC_SIZE
];
463 long nr_swaps_freed
= 0;
468 end
= -1; /* unsigned, so actually very big */
470 pagevec_init(&pvec
, 0);
472 while (index
< end
) {
473 pvec
.nr
= shmem_find_get_pages_and_swap(mapping
, index
,
474 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
),
475 pvec
.pages
, indices
);
478 mem_cgroup_uncharge_start();
479 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
480 struct page
*page
= pvec
.pages
[i
];
486 if (radix_tree_exceptional_entry(page
)) {
489 if (shmem_free_swap(mapping
, index
, page
)) {
490 /* Swap was replaced by page: retry */
498 if (!trylock_page(page
))
500 if (!unfalloc
|| !PageUptodate(page
)) {
501 if (page
->mapping
== mapping
) {
502 VM_BUG_ON(PageWriteback(page
));
503 truncate_inode_page(mapping
, page
);
505 /* Page was replaced by swap: retry */
513 shmem_deswap_pagevec(&pvec
);
514 pagevec_release(&pvec
);
515 mem_cgroup_uncharge_end();
521 struct page
*page
= NULL
;
522 shmem_getpage(inode
, start
- 1, &page
, SGP_READ
, NULL
);
524 unsigned int top
= PAGE_CACHE_SIZE
;
529 zero_user_segment(page
, partial_start
, top
);
530 set_page_dirty(page
);
532 page_cache_release(page
);
536 struct page
*page
= NULL
;
537 shmem_getpage(inode
, end
, &page
, SGP_READ
, NULL
);
539 zero_user_segment(page
, 0, partial_end
);
540 set_page_dirty(page
);
542 page_cache_release(page
);
549 while (index
< end
) {
551 pvec
.nr
= shmem_find_get_pages_and_swap(mapping
, index
,
552 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
),
553 pvec
.pages
, indices
);
555 /* If all gone or hole-punch or unfalloc, we're done */
556 if (index
== start
|| end
!= -1)
558 /* But if truncating, restart to make sure all gone */
562 mem_cgroup_uncharge_start();
563 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
564 struct page
*page
= pvec
.pages
[i
];
570 if (radix_tree_exceptional_entry(page
)) {
573 if (shmem_free_swap(mapping
, index
, page
)) {
574 /* Swap was replaced by page: retry */
583 if (!unfalloc
|| !PageUptodate(page
)) {
584 if (page
->mapping
== mapping
) {
585 VM_BUG_ON(PageWriteback(page
));
586 truncate_inode_page(mapping
, page
);
588 /* Page was replaced by swap: retry */
596 shmem_deswap_pagevec(&pvec
);
597 pagevec_release(&pvec
);
598 mem_cgroup_uncharge_end();
602 spin_lock(&info
->lock
);
603 info
->swapped
-= nr_swaps_freed
;
604 shmem_recalc_inode(inode
);
605 spin_unlock(&info
->lock
);
608 void shmem_truncate_range(struct inode
*inode
, loff_t lstart
, loff_t lend
)
610 shmem_undo_range(inode
, lstart
, lend
, false);
611 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
613 EXPORT_SYMBOL_GPL(shmem_truncate_range
);
615 static int shmem_setattr(struct dentry
*dentry
, struct iattr
*attr
)
617 struct inode
*inode
= dentry
->d_inode
;
618 struct shmem_inode_info
*info
= SHMEM_I(inode
);
621 error
= inode_change_ok(inode
, attr
);
625 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
626 loff_t oldsize
= inode
->i_size
;
627 loff_t newsize
= attr
->ia_size
;
629 /* protected by i_mutex */
630 if ((newsize
< oldsize
&& (info
->seals
& F_SEAL_SHRINK
)) ||
631 (newsize
> oldsize
&& (info
->seals
& F_SEAL_GROW
)))
634 if (newsize
!= oldsize
) {
635 i_size_write(inode
, newsize
);
636 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
638 if (newsize
< oldsize
) {
639 loff_t holebegin
= round_up(newsize
, PAGE_SIZE
);
640 unmap_mapping_range(inode
->i_mapping
, holebegin
, 0, 1);
641 shmem_truncate_range(inode
, newsize
, (loff_t
)-1);
642 /* unmap again to remove racily COWed private pages */
643 unmap_mapping_range(inode
->i_mapping
, holebegin
, 0, 1);
647 setattr_copy(inode
, attr
);
648 #ifdef CONFIG_TMPFS_POSIX_ACL
649 if (attr
->ia_valid
& ATTR_MODE
)
650 error
= generic_acl_chmod(inode
);
655 static void shmem_evict_inode(struct inode
*inode
)
657 struct shmem_inode_info
*info
= SHMEM_I(inode
);
659 if (inode
->i_mapping
->a_ops
== &shmem_aops
) {
660 shmem_unacct_size(info
->flags
, inode
->i_size
);
662 shmem_truncate_range(inode
, 0, (loff_t
)-1);
663 if (!list_empty(&info
->swaplist
)) {
664 mutex_lock(&shmem_swaplist_mutex
);
665 list_del_init(&info
->swaplist
);
666 mutex_unlock(&shmem_swaplist_mutex
);
669 kfree(info
->symlink
);
671 simple_xattrs_free(&info
->xattrs
);
672 WARN_ON(inode
->i_blocks
);
673 shmem_free_inode(inode
->i_sb
);
678 * If swap found in inode, free it and move page from swapcache to filecache.
680 static int shmem_unuse_inode(struct shmem_inode_info
*info
,
681 swp_entry_t swap
, struct page
**pagep
)
683 struct address_space
*mapping
= info
->vfs_inode
.i_mapping
;
689 radswap
= swp_to_radix_entry(swap
);
690 index
= radix_tree_locate_item(&mapping
->page_tree
, radswap
);
695 * Move _head_ to start search for next from here.
696 * But be careful: shmem_evict_inode checks list_empty without taking
697 * mutex, and there's an instant in list_move_tail when info->swaplist
698 * would appear empty, if it were the only one on shmem_swaplist.
700 if (shmem_swaplist
.next
!= &info
->swaplist
)
701 list_move_tail(&shmem_swaplist
, &info
->swaplist
);
703 gfp
= mapping_gfp_mask(mapping
);
704 if (shmem_should_replace_page(*pagep
, gfp
)) {
705 mutex_unlock(&shmem_swaplist_mutex
);
706 error
= shmem_replace_page(pagep
, gfp
, info
, index
);
707 mutex_lock(&shmem_swaplist_mutex
);
709 * We needed to drop mutex to make that restrictive page
710 * allocation, but the inode might have been freed while we
711 * dropped it: although a racing shmem_evict_inode() cannot
712 * complete without emptying the radix_tree, our page lock
713 * on this swapcache page is not enough to prevent that -
714 * free_swap_and_cache() of our swap entry will only
715 * trylock_page(), removing swap from radix_tree whatever.
717 * We must not proceed to shmem_add_to_page_cache() if the
718 * inode has been freed, but of course we cannot rely on
719 * inode or mapping or info to check that. However, we can
720 * safely check if our swap entry is still in use (and here
721 * it can't have got reused for another page): if it's still
722 * in use, then the inode cannot have been freed yet, and we
723 * can safely proceed (if it's no longer in use, that tells
724 * nothing about the inode, but we don't need to unuse swap).
726 if (!page_swapcount(*pagep
))
731 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
732 * but also to hold up shmem_evict_inode(): so inode cannot be freed
733 * beneath us (pagelock doesn't help until the page is in pagecache).
736 error
= shmem_add_to_page_cache(*pagep
, mapping
, index
,
737 GFP_NOWAIT
, radswap
);
738 if (error
!= -ENOMEM
) {
740 * Truncation and eviction use free_swap_and_cache(), which
741 * only does trylock page: if we raced, best clean up here.
743 delete_from_swap_cache(*pagep
);
744 set_page_dirty(*pagep
);
746 spin_lock(&info
->lock
);
748 spin_unlock(&info
->lock
);
751 error
= 1; /* not an error, but entry was found */
757 * Search through swapped inodes to find and replace swap by page.
759 int shmem_unuse(swp_entry_t swap
, struct page
*page
)
761 struct list_head
*this, *next
;
762 struct shmem_inode_info
*info
;
767 * There's a faint possibility that swap page was replaced before
768 * caller locked it: caller will come back later with the right page.
770 if (unlikely(!PageSwapCache(page
) || page_private(page
) != swap
.val
))
774 * Charge page using GFP_KERNEL while we can wait, before taking
775 * the shmem_swaplist_mutex which might hold up shmem_writepage().
776 * Charged back to the user (not to caller) when swap account is used.
778 error
= mem_cgroup_cache_charge(page
, current
->mm
, GFP_KERNEL
);
781 /* No radix_tree_preload: swap entry keeps a place for page in tree */
783 mutex_lock(&shmem_swaplist_mutex
);
784 list_for_each_safe(this, next
, &shmem_swaplist
) {
785 info
= list_entry(this, struct shmem_inode_info
, swaplist
);
787 found
= shmem_unuse_inode(info
, swap
, &page
);
789 list_del_init(&info
->swaplist
);
794 mutex_unlock(&shmem_swaplist_mutex
);
800 page_cache_release(page
);
805 * Move the page from the page cache to the swap cache.
807 static int shmem_writepage(struct page
*page
, struct writeback_control
*wbc
)
809 struct shmem_inode_info
*info
;
810 struct address_space
*mapping
;
815 BUG_ON(!PageLocked(page
));
816 mapping
= page
->mapping
;
818 inode
= mapping
->host
;
819 info
= SHMEM_I(inode
);
820 if (info
->flags
& VM_LOCKED
)
822 if (!total_swap_pages
)
826 * shmem_backing_dev_info's capabilities prevent regular writeback or
827 * sync from ever calling shmem_writepage; but a stacking filesystem
828 * might use ->writepage of its underlying filesystem, in which case
829 * tmpfs should write out to swap only in response to memory pressure,
830 * and not for the writeback threads or sync.
832 if (!wbc
->for_reclaim
) {
833 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
838 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
839 * value into swapfile.c, the only way we can correctly account for a
840 * fallocated page arriving here is now to initialize it and write it.
842 * That's okay for a page already fallocated earlier, but if we have
843 * not yet completed the fallocation, then (a) we want to keep track
844 * of this page in case we have to undo it, and (b) it may not be a
845 * good idea to continue anyway, once we're pushing into swap. So
846 * reactivate the page, and let shmem_fallocate() quit when too many.
848 if (!PageUptodate(page
)) {
849 if (inode
->i_private
) {
850 struct shmem_falloc
*shmem_falloc
;
851 spin_lock(&inode
->i_lock
);
852 shmem_falloc
= inode
->i_private
;
854 !shmem_falloc
->waitq
&&
855 index
>= shmem_falloc
->start
&&
856 index
< shmem_falloc
->next
)
857 shmem_falloc
->nr_unswapped
++;
860 spin_unlock(&inode
->i_lock
);
864 clear_highpage(page
);
865 flush_dcache_page(page
);
866 SetPageUptodate(page
);
869 swap
= get_swap_page();
874 * Add inode to shmem_unuse()'s list of swapped-out inodes,
875 * if it's not already there. Do it now before the page is
876 * moved to swap cache, when its pagelock no longer protects
877 * the inode from eviction. But don't unlock the mutex until
878 * we've incremented swapped, because shmem_unuse_inode() will
879 * prune a !swapped inode from the swaplist under this mutex.
881 mutex_lock(&shmem_swaplist_mutex
);
882 if (list_empty(&info
->swaplist
))
883 list_add_tail(&info
->swaplist
, &shmem_swaplist
);
885 if (add_to_swap_cache(page
, swap
, GFP_ATOMIC
) == 0) {
886 swap_shmem_alloc(swap
);
887 shmem_delete_from_page_cache(page
, swp_to_radix_entry(swap
));
889 spin_lock(&info
->lock
);
891 shmem_recalc_inode(inode
);
892 spin_unlock(&info
->lock
);
894 mutex_unlock(&shmem_swaplist_mutex
);
895 BUG_ON(page_mapped(page
));
896 swap_writepage(page
, wbc
);
900 mutex_unlock(&shmem_swaplist_mutex
);
901 swapcache_free(swap
, NULL
);
903 set_page_dirty(page
);
904 if (wbc
->for_reclaim
)
905 return AOP_WRITEPAGE_ACTIVATE
; /* Return with page locked */
912 static void shmem_show_mpol(struct seq_file
*seq
, struct mempolicy
*mpol
)
916 if (!mpol
|| mpol
->mode
== MPOL_DEFAULT
)
917 return; /* show nothing */
919 mpol_to_str(buffer
, sizeof(buffer
), mpol
);
921 seq_printf(seq
, ",mpol=%s", buffer
);
924 static struct mempolicy
*shmem_get_sbmpol(struct shmem_sb_info
*sbinfo
)
926 struct mempolicy
*mpol
= NULL
;
928 spin_lock(&sbinfo
->stat_lock
); /* prevent replace/use races */
931 spin_unlock(&sbinfo
->stat_lock
);
935 #endif /* CONFIG_TMPFS */
937 static struct page
*shmem_swapin(swp_entry_t swap
, gfp_t gfp
,
938 struct shmem_inode_info
*info
, pgoff_t index
)
940 struct vm_area_struct pvma
;
943 /* Create a pseudo vma that just contains the policy */
945 /* Bias interleave by inode number to distribute better across nodes */
946 pvma
.vm_pgoff
= index
+ info
->vfs_inode
.i_ino
;
948 pvma
.vm_policy
= mpol_shared_policy_lookup(&info
->policy
, index
);
950 page
= swapin_readahead(swap
, gfp
, &pvma
, 0);
952 /* Drop reference taken by mpol_shared_policy_lookup() */
953 mpol_cond_put(pvma
.vm_policy
);
958 static struct page
*shmem_alloc_page(gfp_t gfp
,
959 struct shmem_inode_info
*info
, pgoff_t index
)
961 struct vm_area_struct pvma
;
964 /* Create a pseudo vma that just contains the policy */
966 /* Bias interleave by inode number to distribute better across nodes */
967 pvma
.vm_pgoff
= index
+ info
->vfs_inode
.i_ino
;
969 pvma
.vm_policy
= mpol_shared_policy_lookup(&info
->policy
, index
);
971 page
= alloc_page_vma(gfp
, &pvma
, 0);
972 if (page
&& is_cma_pageblock(page
)) {
974 page
= alloc_pages(gfp
& ~__GFP_MOVABLE
, 0);
977 /* Drop reference taken by mpol_shared_policy_lookup() */
978 mpol_cond_put(pvma
.vm_policy
);
982 #else /* !CONFIG_NUMA */
984 static inline void shmem_show_mpol(struct seq_file
*seq
, struct mempolicy
*mpol
)
987 #endif /* CONFIG_TMPFS */
989 static inline struct page
*shmem_swapin(swp_entry_t swap
, gfp_t gfp
,
990 struct shmem_inode_info
*info
, pgoff_t index
)
992 return swapin_readahead(swap
, gfp
, NULL
, 0);
995 static inline struct page
*shmem_alloc_page(gfp_t gfp
,
996 struct shmem_inode_info
*info
, pgoff_t index
)
998 return alloc_page(gfp
);
1000 #endif /* CONFIG_NUMA */
1002 #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
1003 static inline struct mempolicy
*shmem_get_sbmpol(struct shmem_sb_info
*sbinfo
)
1010 * When a page is moved from swapcache to shmem filecache (either by the
1011 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1012 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1013 * ignorance of the mapping it belongs to. If that mapping has special
1014 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1015 * we may need to copy to a suitable page before moving to filecache.
1017 * In a future release, this may well be extended to respect cpuset and
1018 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1019 * but for now it is a simple matter of zone.
1021 static bool shmem_should_replace_page(struct page
*page
, gfp_t gfp
)
1023 return page_zonenum(page
) > gfp_zone(gfp
);
1026 static int shmem_replace_page(struct page
**pagep
, gfp_t gfp
,
1027 struct shmem_inode_info
*info
, pgoff_t index
)
1029 struct page
*oldpage
, *newpage
;
1030 struct address_space
*swap_mapping
;
1035 swap_index
= page_private(oldpage
);
1036 swap_mapping
= page_mapping(oldpage
);
1039 * We have arrived here because our zones are constrained, so don't
1040 * limit chance of success by further cpuset and node constraints.
1042 gfp
&= ~GFP_CONSTRAINT_MASK
;
1043 newpage
= shmem_alloc_page(gfp
, info
, index
);
1047 page_cache_get(newpage
);
1048 copy_highpage(newpage
, oldpage
);
1049 flush_dcache_page(newpage
);
1051 __set_page_locked(newpage
);
1052 SetPageUptodate(newpage
);
1053 SetPageSwapBacked(newpage
);
1054 set_page_private(newpage
, swap_index
);
1055 SetPageSwapCache(newpage
);
1058 * Our caller will very soon move newpage out of swapcache, but it's
1059 * a nice clean interface for us to replace oldpage by newpage there.
1061 spin_lock_irq(&swap_mapping
->tree_lock
);
1062 error
= shmem_radix_tree_replace(swap_mapping
, swap_index
, oldpage
,
1065 __inc_zone_page_state(newpage
, NR_FILE_PAGES
);
1066 __dec_zone_page_state(oldpage
, NR_FILE_PAGES
);
1068 spin_unlock_irq(&swap_mapping
->tree_lock
);
1070 if (unlikely(error
)) {
1072 * Is this possible? I think not, now that our callers check
1073 * both PageSwapCache and page_private after getting page lock;
1074 * but be defensive. Reverse old to newpage for clear and free.
1078 mem_cgroup_replace_page_cache(oldpage
, newpage
);
1079 lru_cache_add_anon(newpage
);
1083 ClearPageSwapCache(oldpage
);
1084 set_page_private(oldpage
, 0);
1086 unlock_page(oldpage
);
1087 page_cache_release(oldpage
);
1088 page_cache_release(oldpage
);
1093 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
1095 * If we allocate a new one we do not mark it dirty. That's up to the
1096 * vm. If we swap it in we mark it dirty since we also free the swap
1097 * entry since a page cannot live in both the swap and page cache
1099 static int shmem_getpage_gfp(struct inode
*inode
, pgoff_t index
,
1100 struct page
**pagep
, enum sgp_type sgp
, gfp_t gfp
, int *fault_type
)
1102 struct address_space
*mapping
= inode
->i_mapping
;
1103 struct shmem_inode_info
*info
;
1104 struct shmem_sb_info
*sbinfo
;
1111 if (index
> (MAX_LFS_FILESIZE
>> PAGE_CACHE_SHIFT
))
1115 page
= find_lock_page(mapping
, index
);
1116 if (radix_tree_exceptional_entry(page
)) {
1117 swap
= radix_to_swp_entry(page
);
1121 if (sgp
!= SGP_WRITE
&& sgp
!= SGP_FALLOC
&&
1122 ((loff_t
)index
<< PAGE_CACHE_SHIFT
) >= i_size_read(inode
)) {
1127 /* fallocated page? */
1128 if (page
&& !PageUptodate(page
)) {
1129 if (sgp
!= SGP_READ
)
1132 page_cache_release(page
);
1135 if (page
|| (sgp
== SGP_READ
&& !swap
.val
)) {
1141 * Fast cache lookup did not find it:
1142 * bring it back from swap or allocate.
1144 info
= SHMEM_I(inode
);
1145 sbinfo
= SHMEM_SB(inode
->i_sb
);
1148 /* Look it up and read it in.. */
1149 page
= lookup_swap_cache(swap
);
1151 /* here we actually do the io */
1153 *fault_type
|= VM_FAULT_MAJOR
;
1154 page
= shmem_swapin(swap
, gfp
, info
, index
);
1161 /* We have to do this with page locked to prevent races */
1163 if (!PageSwapCache(page
) || page_private(page
) != swap
.val
||
1164 !shmem_confirm_swap(mapping
, index
, swap
)) {
1165 error
= -EEXIST
; /* try again */
1168 if (!PageUptodate(page
)) {
1172 wait_on_page_writeback(page
);
1174 if (shmem_should_replace_page(page
, gfp
)) {
1175 error
= shmem_replace_page(&page
, gfp
, info
, index
);
1180 error
= mem_cgroup_cache_charge(page
, current
->mm
,
1181 gfp
& GFP_RECLAIM_MASK
);
1183 error
= shmem_add_to_page_cache(page
, mapping
, index
,
1184 gfp
, swp_to_radix_entry(swap
));
1186 * We already confirmed swap under page lock, and make
1187 * no memory allocation here, so usually no possibility
1188 * of error; but free_swap_and_cache() only trylocks a
1189 * page, so it is just possible that the entry has been
1190 * truncated or holepunched since swap was confirmed.
1191 * shmem_undo_range() will have done some of the
1192 * unaccounting, now delete_from_swap_cache() will do
1193 * the rest (including mem_cgroup_uncharge_swapcache).
1194 * Reset swap.val? No, leave it so "failed" goes back to
1195 * "repeat": reading a hole and writing should succeed.
1198 delete_from_swap_cache(page
);
1203 spin_lock(&info
->lock
);
1205 shmem_recalc_inode(inode
);
1206 spin_unlock(&info
->lock
);
1208 delete_from_swap_cache(page
);
1209 set_page_dirty(page
);
1213 if (shmem_acct_block(info
->flags
)) {
1217 if (sbinfo
->max_blocks
) {
1218 if (percpu_counter_compare(&sbinfo
->used_blocks
,
1219 sbinfo
->max_blocks
) >= 0) {
1223 percpu_counter_inc(&sbinfo
->used_blocks
);
1226 page
= shmem_alloc_page(gfp
, info
, index
);
1232 SetPageSwapBacked(page
);
1233 __set_page_locked(page
);
1234 error
= mem_cgroup_cache_charge(page
, current
->mm
,
1235 gfp
& GFP_RECLAIM_MASK
);
1238 error
= radix_tree_preload(gfp
& GFP_RECLAIM_MASK
);
1240 error
= shmem_add_to_page_cache(page
, mapping
, index
,
1242 radix_tree_preload_end();
1245 mem_cgroup_uncharge_cache_page(page
);
1248 lru_cache_add_anon(page
);
1250 spin_lock(&info
->lock
);
1252 inode
->i_blocks
+= BLOCKS_PER_PAGE
;
1253 shmem_recalc_inode(inode
);
1254 spin_unlock(&info
->lock
);
1258 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1260 if (sgp
== SGP_FALLOC
)
1264 * Let SGP_WRITE caller clear ends if write does not fill page;
1265 * but SGP_FALLOC on a page fallocated earlier must initialize
1266 * it now, lest undo on failure cancel our earlier guarantee.
1268 if (sgp
!= SGP_WRITE
) {
1269 clear_highpage(page
);
1270 flush_dcache_page(page
);
1271 SetPageUptodate(page
);
1273 if (sgp
== SGP_DIRTY
)
1274 set_page_dirty(page
);
1277 /* Perhaps the file has been truncated since we checked */
1278 if (sgp
!= SGP_WRITE
&& sgp
!= SGP_FALLOC
&&
1279 ((loff_t
)index
<< PAGE_CACHE_SHIFT
) >= i_size_read(inode
)) {
1293 info
= SHMEM_I(inode
);
1294 ClearPageDirty(page
);
1295 delete_from_page_cache(page
);
1296 spin_lock(&info
->lock
);
1298 inode
->i_blocks
-= BLOCKS_PER_PAGE
;
1299 spin_unlock(&info
->lock
);
1301 sbinfo
= SHMEM_SB(inode
->i_sb
);
1302 if (sbinfo
->max_blocks
)
1303 percpu_counter_add(&sbinfo
->used_blocks
, -1);
1305 shmem_unacct_blocks(info
->flags
, 1);
1307 if (swap
.val
&& error
!= -EINVAL
&&
1308 !shmem_confirm_swap(mapping
, index
, swap
))
1313 page_cache_release(page
);
1315 if (error
== -ENOSPC
&& !once
++) {
1316 info
= SHMEM_I(inode
);
1317 spin_lock(&info
->lock
);
1318 shmem_recalc_inode(inode
);
1319 spin_unlock(&info
->lock
);
1322 if (error
== -EEXIST
) /* from above or from radix_tree_insert */
1327 static int shmem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1329 struct inode
*inode
= file_inode(vma
->vm_file
);
1331 int ret
= VM_FAULT_LOCKED
;
1334 * Trinity finds that probing a hole which tmpfs is punching can
1335 * prevent the hole-punch from ever completing: which in turn
1336 * locks writers out with its hold on i_mutex. So refrain from
1337 * faulting pages into the hole while it's being punched. Although
1338 * shmem_undo_range() does remove the additions, it may be unable to
1339 * keep up, as each new page needs its own unmap_mapping_range() call,
1340 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1342 * It does not matter if we sometimes reach this check just before the
1343 * hole-punch begins, so that one fault then races with the punch:
1344 * we just need to make racing faults a rare case.
1346 * The implementation below would be much simpler if we just used a
1347 * standard mutex or completion: but we cannot take i_mutex in fault,
1348 * and bloating every shmem inode for this unlikely case would be sad.
1350 if (unlikely(inode
->i_private
)) {
1351 struct shmem_falloc
*shmem_falloc
;
1353 spin_lock(&inode
->i_lock
);
1354 shmem_falloc
= inode
->i_private
;
1356 shmem_falloc
->waitq
&&
1357 vmf
->pgoff
>= shmem_falloc
->start
&&
1358 vmf
->pgoff
< shmem_falloc
->next
) {
1359 wait_queue_head_t
*shmem_falloc_waitq
;
1360 DEFINE_WAIT(shmem_fault_wait
);
1362 ret
= VM_FAULT_NOPAGE
;
1363 if ((vmf
->flags
& FAULT_FLAG_ALLOW_RETRY
) &&
1364 !(vmf
->flags
& FAULT_FLAG_RETRY_NOWAIT
)) {
1365 /* It's polite to up mmap_sem if we can */
1366 up_read(&vma
->vm_mm
->mmap_sem
);
1367 ret
= VM_FAULT_RETRY
;
1370 shmem_falloc_waitq
= shmem_falloc
->waitq
;
1371 prepare_to_wait(shmem_falloc_waitq
, &shmem_fault_wait
,
1372 TASK_UNINTERRUPTIBLE
);
1373 spin_unlock(&inode
->i_lock
);
1377 * shmem_falloc_waitq points into the shmem_fallocate()
1378 * stack of the hole-punching task: shmem_falloc_waitq
1379 * is usually invalid by the time we reach here, but
1380 * finish_wait() does not dereference it in that case;
1381 * though i_lock needed lest racing with wake_up_all().
1383 spin_lock(&inode
->i_lock
);
1384 finish_wait(shmem_falloc_waitq
, &shmem_fault_wait
);
1385 spin_unlock(&inode
->i_lock
);
1388 spin_unlock(&inode
->i_lock
);
1391 error
= shmem_getpage(inode
, vmf
->pgoff
, &vmf
->page
, SGP_CACHE
, &ret
);
1393 return ((error
== -ENOMEM
) ? VM_FAULT_OOM
: VM_FAULT_SIGBUS
);
1395 if (ret
& VM_FAULT_MAJOR
) {
1396 count_vm_event(PGMAJFAULT
);
1397 mem_cgroup_count_vm_event(vma
->vm_mm
, PGMAJFAULT
);
1403 static int shmem_set_policy(struct vm_area_struct
*vma
, struct mempolicy
*mpol
)
1405 struct inode
*inode
= file_inode(vma
->vm_file
);
1406 return mpol_set_shared_policy(&SHMEM_I(inode
)->policy
, vma
, mpol
);
1409 static struct mempolicy
*shmem_get_policy(struct vm_area_struct
*vma
,
1412 struct inode
*inode
= file_inode(vma
->vm_file
);
1415 index
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
1416 return mpol_shared_policy_lookup(&SHMEM_I(inode
)->policy
, index
);
1420 int shmem_lock(struct file
*file
, int lock
, struct user_struct
*user
)
1422 struct inode
*inode
= file_inode(file
);
1423 struct shmem_inode_info
*info
= SHMEM_I(inode
);
1424 int retval
= -ENOMEM
;
1426 spin_lock(&info
->lock
);
1427 if (lock
&& !(info
->flags
& VM_LOCKED
)) {
1428 if (!user_shm_lock(inode
->i_size
, user
))
1430 info
->flags
|= VM_LOCKED
;
1431 mapping_set_unevictable(file
->f_mapping
);
1433 if (!lock
&& (info
->flags
& VM_LOCKED
) && user
) {
1434 user_shm_unlock(inode
->i_size
, user
);
1435 info
->flags
&= ~VM_LOCKED
;
1436 mapping_clear_unevictable(file
->f_mapping
);
1441 spin_unlock(&info
->lock
);
1445 static int shmem_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1447 file_accessed(file
);
1448 vma
->vm_ops
= &shmem_vm_ops
;
1452 static struct inode
*shmem_get_inode(struct super_block
*sb
, const struct inode
*dir
,
1453 umode_t mode
, dev_t dev
, unsigned long flags
)
1455 struct inode
*inode
;
1456 struct shmem_inode_info
*info
;
1457 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
1459 if (shmem_reserve_inode(sb
))
1462 inode
= new_inode(sb
);
1464 inode
->i_ino
= get_next_ino();
1465 inode_init_owner(inode
, dir
, mode
);
1466 inode
->i_blocks
= 0;
1467 inode
->i_mapping
->backing_dev_info
= &shmem_backing_dev_info
;
1468 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1469 inode
->i_generation
= get_seconds();
1470 info
= SHMEM_I(inode
);
1471 memset(info
, 0, (char *)inode
- (char *)info
);
1472 spin_lock_init(&info
->lock
);
1473 info
->seals
= F_SEAL_SEAL
;
1474 info
->flags
= flags
& VM_NORESERVE
;
1475 INIT_LIST_HEAD(&info
->swaplist
);
1476 simple_xattrs_init(&info
->xattrs
);
1477 cache_no_acl(inode
);
1479 switch (mode
& S_IFMT
) {
1481 inode
->i_op
= &shmem_special_inode_operations
;
1482 init_special_inode(inode
, mode
, dev
);
1485 inode
->i_mapping
->a_ops
= &shmem_aops
;
1486 inode
->i_op
= &shmem_inode_operations
;
1487 inode
->i_fop
= &shmem_file_operations
;
1488 mpol_shared_policy_init(&info
->policy
,
1489 shmem_get_sbmpol(sbinfo
));
1493 /* Some things misbehave if size == 0 on a directory */
1494 inode
->i_size
= 2 * BOGO_DIRENT_SIZE
;
1495 inode
->i_op
= &shmem_dir_inode_operations
;
1496 inode
->i_fop
= &simple_dir_operations
;
1500 * Must not load anything in the rbtree,
1501 * mpol_free_shared_policy will not be called.
1503 mpol_shared_policy_init(&info
->policy
, NULL
);
1507 shmem_free_inode(sb
);
1512 static const struct inode_operations shmem_symlink_inode_operations
;
1513 static const struct inode_operations shmem_short_symlink_operations
;
1515 #ifdef CONFIG_TMPFS_XATTR
1516 static int shmem_initxattrs(struct inode
*, const struct xattr
*, void *);
1518 #define shmem_initxattrs NULL
1522 shmem_write_begin(struct file
*file
, struct address_space
*mapping
,
1523 loff_t pos
, unsigned len
, unsigned flags
,
1524 struct page
**pagep
, void **fsdata
)
1526 struct inode
*inode
= mapping
->host
;
1527 struct shmem_inode_info
*info
= SHMEM_I(inode
);
1528 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
1530 /* i_mutex is held by caller */
1531 if (unlikely(info
->seals
)) {
1532 if (info
->seals
& F_SEAL_WRITE
)
1534 if ((info
->seals
& F_SEAL_GROW
) && pos
+ len
> inode
->i_size
)
1538 return shmem_getpage(inode
, index
, pagep
, SGP_WRITE
, NULL
);
1542 shmem_write_end(struct file
*file
, struct address_space
*mapping
,
1543 loff_t pos
, unsigned len
, unsigned copied
,
1544 struct page
*page
, void *fsdata
)
1546 struct inode
*inode
= mapping
->host
;
1548 if (pos
+ copied
> inode
->i_size
)
1549 i_size_write(inode
, pos
+ copied
);
1551 if (!PageUptodate(page
)) {
1552 if (copied
< PAGE_CACHE_SIZE
) {
1553 unsigned from
= pos
& (PAGE_CACHE_SIZE
- 1);
1554 zero_user_segments(page
, 0, from
,
1555 from
+ copied
, PAGE_CACHE_SIZE
);
1557 SetPageUptodate(page
);
1559 set_page_dirty(page
);
1561 page_cache_release(page
);
1566 static void do_shmem_file_read(struct file
*filp
, loff_t
*ppos
, read_descriptor_t
*desc
, read_actor_t actor
)
1568 struct inode
*inode
= file_inode(filp
);
1569 struct address_space
*mapping
= inode
->i_mapping
;
1571 unsigned long offset
;
1572 enum sgp_type sgp
= SGP_READ
;
1575 * Might this read be for a stacking filesystem? Then when reading
1576 * holes of a sparse file, we actually need to allocate those pages,
1577 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1579 if (segment_eq(get_fs(), KERNEL_DS
))
1582 index
= *ppos
>> PAGE_CACHE_SHIFT
;
1583 offset
= *ppos
& ~PAGE_CACHE_MASK
;
1586 struct page
*page
= NULL
;
1588 unsigned long nr
, ret
;
1589 loff_t i_size
= i_size_read(inode
);
1591 end_index
= i_size
>> PAGE_CACHE_SHIFT
;
1592 if (index
> end_index
)
1594 if (index
== end_index
) {
1595 nr
= i_size
& ~PAGE_CACHE_MASK
;
1600 desc
->error
= shmem_getpage(inode
, index
, &page
, sgp
, NULL
);
1602 if (desc
->error
== -EINVAL
)
1610 * We must evaluate after, since reads (unlike writes)
1611 * are called without i_mutex protection against truncate
1613 nr
= PAGE_CACHE_SIZE
;
1614 i_size
= i_size_read(inode
);
1615 end_index
= i_size
>> PAGE_CACHE_SHIFT
;
1616 if (index
== end_index
) {
1617 nr
= i_size
& ~PAGE_CACHE_MASK
;
1620 page_cache_release(page
);
1628 * If users can be writing to this page using arbitrary
1629 * virtual addresses, take care about potential aliasing
1630 * before reading the page on the kernel side.
1632 if (mapping_writably_mapped(mapping
))
1633 flush_dcache_page(page
);
1635 * Mark the page accessed if we read the beginning.
1638 mark_page_accessed(page
);
1640 page
= ZERO_PAGE(0);
1641 page_cache_get(page
);
1645 * Ok, we have the page, and it's up-to-date, so
1646 * now we can copy it to user space...
1648 * The actor routine returns how many bytes were actually used..
1649 * NOTE! This may not be the same as how much of a user buffer
1650 * we filled up (we may be padding etc), so we can only update
1651 * "pos" here (the actor routine has to update the user buffer
1652 * pointers and the remaining count).
1654 ret
= actor(desc
, page
, offset
, nr
);
1656 index
+= offset
>> PAGE_CACHE_SHIFT
;
1657 offset
&= ~PAGE_CACHE_MASK
;
1659 page_cache_release(page
);
1660 if (ret
!= nr
|| !desc
->count
)
1666 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
1667 file_accessed(filp
);
1670 static ssize_t
shmem_file_aio_read(struct kiocb
*iocb
,
1671 const struct iovec
*iov
, unsigned long nr_segs
, loff_t pos
)
1673 struct file
*filp
= iocb
->ki_filp
;
1677 loff_t
*ppos
= &iocb
->ki_pos
;
1679 retval
= generic_segment_checks(iov
, &nr_segs
, &count
, VERIFY_WRITE
);
1683 for (seg
= 0; seg
< nr_segs
; seg
++) {
1684 read_descriptor_t desc
;
1687 desc
.arg
.buf
= iov
[seg
].iov_base
;
1688 desc
.count
= iov
[seg
].iov_len
;
1689 if (desc
.count
== 0)
1692 do_shmem_file_read(filp
, ppos
, &desc
, file_read_actor
);
1693 retval
+= desc
.written
;
1695 retval
= retval
?: desc
.error
;
1704 static ssize_t
shmem_file_splice_read(struct file
*in
, loff_t
*ppos
,
1705 struct pipe_inode_info
*pipe
, size_t len
,
1708 struct address_space
*mapping
= in
->f_mapping
;
1709 struct inode
*inode
= mapping
->host
;
1710 unsigned int loff
, nr_pages
, req_pages
;
1711 struct page
*pages
[PIPE_DEF_BUFFERS
];
1712 struct partial_page partial
[PIPE_DEF_BUFFERS
];
1714 pgoff_t index
, end_index
;
1717 struct splice_pipe_desc spd
= {
1720 .nr_pages_max
= PIPE_DEF_BUFFERS
,
1722 .ops
= &page_cache_pipe_buf_ops
,
1723 .spd_release
= spd_release_page
,
1726 isize
= i_size_read(inode
);
1727 if (unlikely(*ppos
>= isize
))
1730 left
= isize
- *ppos
;
1731 if (unlikely(left
< len
))
1734 if (splice_grow_spd(pipe
, &spd
))
1737 index
= *ppos
>> PAGE_CACHE_SHIFT
;
1738 loff
= *ppos
& ~PAGE_CACHE_MASK
;
1739 req_pages
= (len
+ loff
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1740 nr_pages
= min(req_pages
, pipe
->buffers
);
1742 spd
.nr_pages
= find_get_pages_contig(mapping
, index
,
1743 nr_pages
, spd
.pages
);
1744 index
+= spd
.nr_pages
;
1747 while (spd
.nr_pages
< nr_pages
) {
1748 error
= shmem_getpage(inode
, index
, &page
, SGP_CACHE
, NULL
);
1752 spd
.pages
[spd
.nr_pages
++] = page
;
1756 index
= *ppos
>> PAGE_CACHE_SHIFT
;
1757 nr_pages
= spd
.nr_pages
;
1760 for (page_nr
= 0; page_nr
< nr_pages
; page_nr
++) {
1761 unsigned int this_len
;
1766 this_len
= min_t(unsigned long, len
, PAGE_CACHE_SIZE
- loff
);
1767 page
= spd
.pages
[page_nr
];
1769 if (!PageUptodate(page
) || page
->mapping
!= mapping
) {
1770 error
= shmem_getpage(inode
, index
, &page
,
1775 page_cache_release(spd
.pages
[page_nr
]);
1776 spd
.pages
[page_nr
] = page
;
1779 isize
= i_size_read(inode
);
1780 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1781 if (unlikely(!isize
|| index
> end_index
))
1784 if (end_index
== index
) {
1787 plen
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
1791 this_len
= min(this_len
, plen
- loff
);
1795 spd
.partial
[page_nr
].offset
= loff
;
1796 spd
.partial
[page_nr
].len
= this_len
;
1803 while (page_nr
< nr_pages
)
1804 page_cache_release(spd
.pages
[page_nr
++]);
1807 error
= splice_to_pipe(pipe
, &spd
);
1809 splice_shrink_spd(&spd
);
1819 * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
1821 static pgoff_t
shmem_seek_hole_data(struct address_space
*mapping
,
1822 pgoff_t index
, pgoff_t end
, int whence
)
1825 struct pagevec pvec
;
1826 pgoff_t indices
[PAGEVEC_SIZE
];
1830 pagevec_init(&pvec
, 0);
1831 pvec
.nr
= 1; /* start small: we may be there already */
1833 pvec
.nr
= shmem_find_get_pages_and_swap(mapping
, index
,
1834 pvec
.nr
, pvec
.pages
, indices
);
1836 if (whence
== SEEK_DATA
)
1840 for (i
= 0; i
< pvec
.nr
; i
++, index
++) {
1841 if (index
< indices
[i
]) {
1842 if (whence
== SEEK_HOLE
) {
1848 page
= pvec
.pages
[i
];
1849 if (page
&& !radix_tree_exceptional_entry(page
)) {
1850 if (!PageUptodate(page
))
1854 (page
&& whence
== SEEK_DATA
) ||
1855 (!page
&& whence
== SEEK_HOLE
)) {
1860 shmem_deswap_pagevec(&pvec
);
1861 pagevec_release(&pvec
);
1862 pvec
.nr
= PAGEVEC_SIZE
;
1868 static loff_t
shmem_file_llseek(struct file
*file
, loff_t offset
, int whence
)
1870 struct address_space
*mapping
= file
->f_mapping
;
1871 struct inode
*inode
= mapping
->host
;
1875 if (whence
!= SEEK_DATA
&& whence
!= SEEK_HOLE
)
1876 return generic_file_llseek_size(file
, offset
, whence
,
1877 MAX_LFS_FILESIZE
, i_size_read(inode
));
1878 mutex_lock(&inode
->i_mutex
);
1879 /* We're holding i_mutex so we can access i_size directly */
1883 else if (offset
>= inode
->i_size
)
1886 start
= offset
>> PAGE_CACHE_SHIFT
;
1887 end
= (inode
->i_size
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1888 new_offset
= shmem_seek_hole_data(mapping
, start
, end
, whence
);
1889 new_offset
<<= PAGE_CACHE_SHIFT
;
1890 if (new_offset
> offset
) {
1891 if (new_offset
< inode
->i_size
)
1892 offset
= new_offset
;
1893 else if (whence
== SEEK_DATA
)
1896 offset
= inode
->i_size
;
1900 if (offset
>= 0 && offset
!= file
->f_pos
) {
1901 file
->f_pos
= offset
;
1902 file
->f_version
= 0;
1904 mutex_unlock(&inode
->i_mutex
);
1909 * We need a tag: a new tag would expand every radix_tree_node by 8 bytes,
1910 * so reuse a tag which we firmly believe is never set or cleared on shmem.
1912 #define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE
1913 #define LAST_SCAN 4 /* about 150ms max */
1915 static void shmem_tag_pins(struct address_space
*mapping
)
1917 struct radix_tree_iter iter
;
1927 radix_tree_for_each_slot(slot
, &mapping
->page_tree
, &iter
, start
) {
1928 page
= radix_tree_deref_slot(slot
);
1929 if (!page
|| radix_tree_exception(page
)) {
1930 if (radix_tree_deref_retry(page
))
1932 } else if (page_count(page
) - page_mapcount(page
) > 1) {
1933 spin_lock_irq(&mapping
->tree_lock
);
1934 radix_tree_tag_set(&mapping
->page_tree
, iter
.index
,
1936 spin_unlock_irq(&mapping
->tree_lock
);
1939 if (need_resched()) {
1942 start
= iter
.index
+ 1;
1950 * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
1951 * via get_user_pages(), drivers might have some pending I/O without any active
1952 * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
1953 * and see whether it has an elevated ref-count. If so, we tag them and wait for
1954 * them to be dropped.
1955 * The caller must guarantee that no new user will acquire writable references
1956 * to those pages to avoid races.
1958 static int shmem_wait_for_pins(struct address_space
*mapping
)
1960 struct radix_tree_iter iter
;
1966 shmem_tag_pins(mapping
);
1969 for (scan
= 0; scan
<= LAST_SCAN
; scan
++) {
1970 if (!radix_tree_tagged(&mapping
->page_tree
, SHMEM_TAG_PINNED
))
1974 lru_add_drain_all();
1975 else if (schedule_timeout_killable((HZ
<< scan
) / 200))
1981 radix_tree_for_each_tagged(slot
, &mapping
->page_tree
, &iter
,
1982 start
, SHMEM_TAG_PINNED
) {
1984 page
= radix_tree_deref_slot(slot
);
1985 if (radix_tree_exception(page
)) {
1986 if (radix_tree_deref_retry(page
))
1993 page_count(page
) - page_mapcount(page
) != 1) {
1994 if (scan
< LAST_SCAN
)
1995 goto continue_resched
;
1998 * On the last scan, we clean up all those tags
1999 * we inserted; but make a note that we still
2000 * found pages pinned.
2005 spin_lock_irq(&mapping
->tree_lock
);
2006 radix_tree_tag_clear(&mapping
->page_tree
,
2007 iter
.index
, SHMEM_TAG_PINNED
);
2008 spin_unlock_irq(&mapping
->tree_lock
);
2010 if (need_resched()) {
2013 start
= iter
.index
+ 1;
2023 #define F_ALL_SEALS (F_SEAL_SEAL | \
2028 int shmem_add_seals(struct file
*file
, unsigned int seals
)
2030 struct inode
*inode
= file_inode(file
);
2031 struct shmem_inode_info
*info
= SHMEM_I(inode
);
2036 * Sealing allows multiple parties to share a shmem-file but restrict
2037 * access to a specific subset of file operations. Seals can only be
2038 * added, but never removed. This way, mutually untrusted parties can
2039 * share common memory regions with a well-defined policy. A malicious
2040 * peer can thus never perform unwanted operations on a shared object.
2042 * Seals are only supported on special shmem-files and always affect
2043 * the whole underlying inode. Once a seal is set, it may prevent some
2044 * kinds of access to the file. Currently, the following seals are
2046 * SEAL_SEAL: Prevent further seals from being set on this file
2047 * SEAL_SHRINK: Prevent the file from shrinking
2048 * SEAL_GROW: Prevent the file from growing
2049 * SEAL_WRITE: Prevent write access to the file
2051 * As we don't require any trust relationship between two parties, we
2052 * must prevent seals from being removed. Therefore, sealing a file
2053 * only adds a given set of seals to the file, it never touches
2054 * existing seals. Furthermore, the "setting seals"-operation can be
2055 * sealed itself, which basically prevents any further seal from being
2058 * Semantics of sealing are only defined on volatile files. Only
2059 * anonymous shmem files support sealing. More importantly, seals are
2060 * never written to disk. Therefore, there's no plan to support it on
2064 if (file
->f_op
!= &shmem_file_operations
)
2066 if (!(file
->f_mode
& FMODE_WRITE
))
2068 if (seals
& ~(unsigned int)F_ALL_SEALS
)
2071 mutex_lock(&inode
->i_mutex
);
2073 if (info
->seals
& F_SEAL_SEAL
) {
2078 if ((seals
& F_SEAL_WRITE
) && !(info
->seals
& F_SEAL_WRITE
)) {
2079 error
= mapping_deny_writable(file
->f_mapping
);
2083 error
= shmem_wait_for_pins(file
->f_mapping
);
2085 mapping_allow_writable(file
->f_mapping
);
2090 info
->seals
|= seals
;
2094 mutex_unlock(&inode
->i_mutex
);
2097 EXPORT_SYMBOL_GPL(shmem_add_seals
);
2099 int shmem_get_seals(struct file
*file
)
2101 if (file
->f_op
!= &shmem_file_operations
)
2104 return SHMEM_I(file_inode(file
))->seals
;
2106 EXPORT_SYMBOL_GPL(shmem_get_seals
);
2108 long shmem_fcntl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2114 /* disallow upper 32bit */
2118 error
= shmem_add_seals(file
, arg
);
2121 error
= shmem_get_seals(file
);
2131 static long shmem_fallocate(struct file
*file
, int mode
, loff_t offset
,
2134 struct inode
*inode
= file_inode(file
);
2135 struct shmem_sb_info
*sbinfo
= SHMEM_SB(inode
->i_sb
);
2136 struct shmem_inode_info
*info
= SHMEM_I(inode
);
2137 struct shmem_falloc shmem_falloc
;
2138 pgoff_t start
, index
, end
;
2141 mutex_lock(&inode
->i_mutex
);
2143 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
2144 struct address_space
*mapping
= file
->f_mapping
;
2145 loff_t unmap_start
= round_up(offset
, PAGE_SIZE
);
2146 loff_t unmap_end
= round_down(offset
+ len
, PAGE_SIZE
) - 1;
2147 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq
);
2149 /* protected by i_mutex */
2150 if (info
->seals
& F_SEAL_WRITE
) {
2155 shmem_falloc
.waitq
= &shmem_falloc_waitq
;
2156 shmem_falloc
.start
= unmap_start
>> PAGE_SHIFT
;
2157 shmem_falloc
.next
= (unmap_end
+ 1) >> PAGE_SHIFT
;
2158 spin_lock(&inode
->i_lock
);
2159 inode
->i_private
= &shmem_falloc
;
2160 spin_unlock(&inode
->i_lock
);
2162 if ((u64
)unmap_end
> (u64
)unmap_start
)
2163 unmap_mapping_range(mapping
, unmap_start
,
2164 1 + unmap_end
- unmap_start
, 0);
2165 shmem_truncate_range(inode
, offset
, offset
+ len
- 1);
2166 /* No need to unmap again: hole-punching leaves COWed pages */
2168 spin_lock(&inode
->i_lock
);
2169 inode
->i_private
= NULL
;
2170 wake_up_all(&shmem_falloc_waitq
);
2171 spin_unlock(&inode
->i_lock
);
2176 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2177 error
= inode_newsize_ok(inode
, offset
+ len
);
2181 if ((info
->seals
& F_SEAL_GROW
) && offset
+ len
> inode
->i_size
) {
2186 start
= offset
>> PAGE_CACHE_SHIFT
;
2187 end
= (offset
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
2188 /* Try to avoid a swapstorm if len is impossible to satisfy */
2189 if (sbinfo
->max_blocks
&& end
- start
> sbinfo
->max_blocks
) {
2194 shmem_falloc
.waitq
= NULL
;
2195 shmem_falloc
.start
= start
;
2196 shmem_falloc
.next
= start
;
2197 shmem_falloc
.nr_falloced
= 0;
2198 shmem_falloc
.nr_unswapped
= 0;
2199 spin_lock(&inode
->i_lock
);
2200 inode
->i_private
= &shmem_falloc
;
2201 spin_unlock(&inode
->i_lock
);
2203 for (index
= start
; index
< end
; index
++) {
2207 * Good, the fallocate(2) manpage permits EINTR: we may have
2208 * been interrupted because we are using up too much memory.
2210 if (signal_pending(current
))
2212 else if (shmem_falloc
.nr_unswapped
> shmem_falloc
.nr_falloced
)
2215 error
= shmem_getpage(inode
, index
, &page
, SGP_FALLOC
,
2218 /* Remove the !PageUptodate pages we added */
2219 if (index
> start
) {
2220 shmem_undo_range(inode
,
2221 (loff_t
)start
<< PAGE_CACHE_SHIFT
,
2222 ((loff_t
)index
<< PAGE_CACHE_SHIFT
) - 1, true);
2228 * Inform shmem_writepage() how far we have reached.
2229 * No need for lock or barrier: we have the page lock.
2231 shmem_falloc
.next
++;
2232 if (!PageUptodate(page
))
2233 shmem_falloc
.nr_falloced
++;
2236 * If !PageUptodate, leave it that way so that freeable pages
2237 * can be recognized if we need to rollback on error later.
2238 * But set_page_dirty so that memory pressure will swap rather
2239 * than free the pages we are allocating (and SGP_CACHE pages
2240 * might still be clean: we now need to mark those dirty too).
2242 set_page_dirty(page
);
2244 page_cache_release(page
);
2248 if (!(mode
& FALLOC_FL_KEEP_SIZE
) && offset
+ len
> inode
->i_size
)
2249 i_size_write(inode
, offset
+ len
);
2250 inode
->i_ctime
= CURRENT_TIME
;
2252 spin_lock(&inode
->i_lock
);
2253 inode
->i_private
= NULL
;
2254 spin_unlock(&inode
->i_lock
);
2256 mutex_unlock(&inode
->i_mutex
);
2260 static int shmem_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
2262 struct shmem_sb_info
*sbinfo
= SHMEM_SB(dentry
->d_sb
);
2264 buf
->f_type
= TMPFS_MAGIC
;
2265 buf
->f_bsize
= PAGE_CACHE_SIZE
;
2266 buf
->f_namelen
= NAME_MAX
;
2267 if (sbinfo
->max_blocks
) {
2268 buf
->f_blocks
= sbinfo
->max_blocks
;
2270 buf
->f_bfree
= sbinfo
->max_blocks
-
2271 percpu_counter_sum(&sbinfo
->used_blocks
);
2273 if (sbinfo
->max_inodes
) {
2274 buf
->f_files
= sbinfo
->max_inodes
;
2275 buf
->f_ffree
= sbinfo
->free_inodes
;
2277 /* else leave those fields 0 like simple_statfs */
2282 * File creation. Allocate an inode, and we're done..
2285 shmem_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
2287 struct inode
*inode
;
2288 int error
= -ENOSPC
;
2290 inode
= shmem_get_inode(dir
->i_sb
, dir
, mode
, dev
, VM_NORESERVE
);
2292 error
= security_inode_init_security(inode
, dir
,
2294 shmem_initxattrs
, NULL
);
2296 if (error
!= -EOPNOTSUPP
) {
2301 #ifdef CONFIG_TMPFS_POSIX_ACL
2302 error
= generic_acl_init(inode
, dir
);
2310 dir
->i_size
+= BOGO_DIRENT_SIZE
;
2311 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
2312 d_instantiate(dentry
, inode
);
2313 dget(dentry
); /* Extra count - pin the dentry in core */
2319 shmem_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2321 struct inode
*inode
;
2322 int error
= -ENOSPC
;
2324 inode
= shmem_get_inode(dir
->i_sb
, dir
, mode
, 0, VM_NORESERVE
);
2326 error
= security_inode_init_security(inode
, dir
,
2328 shmem_initxattrs
, NULL
);
2330 if (error
!= -EOPNOTSUPP
) {
2335 #ifdef CONFIG_TMPFS_POSIX_ACL
2336 error
= generic_acl_init(inode
, dir
);
2344 d_tmpfile(dentry
, inode
);
2349 static int shmem_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2353 if ((error
= shmem_mknod(dir
, dentry
, mode
| S_IFDIR
, 0)))
2359 static int shmem_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2362 return shmem_mknod(dir
, dentry
, mode
| S_IFREG
, 0);
2368 static int shmem_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
2370 struct inode
*inode
= old_dentry
->d_inode
;
2374 * No ordinary (disk based) filesystem counts links as inodes;
2375 * but each new link needs a new dentry, pinning lowmem, and
2376 * tmpfs dentries cannot be pruned until they are unlinked.
2378 ret
= shmem_reserve_inode(inode
->i_sb
);
2382 dir
->i_size
+= BOGO_DIRENT_SIZE
;
2383 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
2385 ihold(inode
); /* New dentry reference */
2386 dget(dentry
); /* Extra pinning count for the created dentry */
2387 d_instantiate(dentry
, inode
);
2392 static int shmem_unlink(struct inode
*dir
, struct dentry
*dentry
)
2394 struct inode
*inode
= dentry
->d_inode
;
2396 if (inode
->i_nlink
> 1 && !S_ISDIR(inode
->i_mode
))
2397 shmem_free_inode(inode
->i_sb
);
2399 dir
->i_size
-= BOGO_DIRENT_SIZE
;
2400 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
2402 dput(dentry
); /* Undo the count from "create" - this does all the work */
2406 static int shmem_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2408 if (!simple_empty(dentry
))
2411 drop_nlink(dentry
->d_inode
);
2413 return shmem_unlink(dir
, dentry
);
2417 * The VFS layer already does all the dentry stuff for rename,
2418 * we just have to decrement the usage count for the target if
2419 * it exists so that the VFS layer correctly free's it when it
2422 static int shmem_rename(struct inode
*old_dir
, struct dentry
*old_dentry
, struct inode
*new_dir
, struct dentry
*new_dentry
)
2424 struct inode
*inode
= old_dentry
->d_inode
;
2425 int they_are_dirs
= S_ISDIR(inode
->i_mode
);
2427 if (!simple_empty(new_dentry
))
2430 if (new_dentry
->d_inode
) {
2431 (void) shmem_unlink(new_dir
, new_dentry
);
2432 if (they_are_dirs
) {
2433 drop_nlink(new_dentry
->d_inode
);
2434 drop_nlink(old_dir
);
2436 } else if (they_are_dirs
) {
2437 drop_nlink(old_dir
);
2441 old_dir
->i_size
-= BOGO_DIRENT_SIZE
;
2442 new_dir
->i_size
+= BOGO_DIRENT_SIZE
;
2443 old_dir
->i_ctime
= old_dir
->i_mtime
=
2444 new_dir
->i_ctime
= new_dir
->i_mtime
=
2445 inode
->i_ctime
= CURRENT_TIME
;
2449 static int shmem_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
2453 struct inode
*inode
;
2456 struct shmem_inode_info
*info
;
2458 len
= strlen(symname
) + 1;
2459 if (len
> PAGE_CACHE_SIZE
)
2460 return -ENAMETOOLONG
;
2462 inode
= shmem_get_inode(dir
->i_sb
, dir
, S_IFLNK
|S_IRWXUGO
, 0, VM_NORESERVE
);
2466 error
= security_inode_init_security(inode
, dir
, &dentry
->d_name
,
2467 shmem_initxattrs
, NULL
);
2469 if (error
!= -EOPNOTSUPP
) {
2476 info
= SHMEM_I(inode
);
2477 inode
->i_size
= len
-1;
2478 if (len
<= SHORT_SYMLINK_LEN
) {
2479 info
->symlink
= kmemdup(symname
, len
, GFP_KERNEL
);
2480 if (!info
->symlink
) {
2484 inode
->i_op
= &shmem_short_symlink_operations
;
2486 error
= shmem_getpage(inode
, 0, &page
, SGP_WRITE
, NULL
);
2491 inode
->i_mapping
->a_ops
= &shmem_aops
;
2492 inode
->i_op
= &shmem_symlink_inode_operations
;
2493 kaddr
= kmap_atomic(page
);
2494 memcpy(kaddr
, symname
, len
);
2495 kunmap_atomic(kaddr
);
2496 SetPageUptodate(page
);
2497 set_page_dirty(page
);
2499 page_cache_release(page
);
2501 dir
->i_size
+= BOGO_DIRENT_SIZE
;
2502 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
2503 d_instantiate(dentry
, inode
);
2508 static void *shmem_follow_short_symlink(struct dentry
*dentry
, struct nameidata
*nd
)
2510 nd_set_link(nd
, SHMEM_I(dentry
->d_inode
)->symlink
);
2514 static void *shmem_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2516 struct page
*page
= NULL
;
2517 int error
= shmem_getpage(dentry
->d_inode
, 0, &page
, SGP_READ
, NULL
);
2518 nd_set_link(nd
, error
? ERR_PTR(error
) : kmap(page
));
2524 static void shmem_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
2526 if (!IS_ERR(nd_get_link(nd
))) {
2527 struct page
*page
= cookie
;
2529 mark_page_accessed(page
);
2530 page_cache_release(page
);
2534 #ifdef CONFIG_TMPFS_XATTR
2536 * Superblocks without xattr inode operations may get some security.* xattr
2537 * support from the LSM "for free". As soon as we have any other xattrs
2538 * like ACLs, we also need to implement the security.* handlers at
2539 * filesystem level, though.
2543 * Callback for security_inode_init_security() for acquiring xattrs.
2545 static int shmem_initxattrs(struct inode
*inode
,
2546 const struct xattr
*xattr_array
,
2549 struct shmem_inode_info
*info
= SHMEM_I(inode
);
2550 const struct xattr
*xattr
;
2551 struct simple_xattr
*new_xattr
;
2554 for (xattr
= xattr_array
; xattr
->name
!= NULL
; xattr
++) {
2555 new_xattr
= simple_xattr_alloc(xattr
->value
, xattr
->value_len
);
2559 len
= strlen(xattr
->name
) + 1;
2560 new_xattr
->name
= kmalloc(XATTR_SECURITY_PREFIX_LEN
+ len
,
2562 if (!new_xattr
->name
) {
2567 memcpy(new_xattr
->name
, XATTR_SECURITY_PREFIX
,
2568 XATTR_SECURITY_PREFIX_LEN
);
2569 memcpy(new_xattr
->name
+ XATTR_SECURITY_PREFIX_LEN
,
2572 simple_xattr_list_add(&info
->xattrs
, new_xattr
);
2578 static const struct xattr_handler
*shmem_xattr_handlers
[] = {
2579 #ifdef CONFIG_TMPFS_POSIX_ACL
2580 &generic_acl_access_handler
,
2581 &generic_acl_default_handler
,
2586 static int shmem_xattr_validate(const char *name
)
2588 struct { const char *prefix
; size_t len
; } arr
[] = {
2589 { XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
},
2590 { XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
}
2594 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
2595 size_t preflen
= arr
[i
].len
;
2596 if (strncmp(name
, arr
[i
].prefix
, preflen
) == 0) {
2605 static ssize_t
shmem_getxattr(struct dentry
*dentry
, const char *name
,
2606 void *buffer
, size_t size
)
2608 struct shmem_inode_info
*info
= SHMEM_I(dentry
->d_inode
);
2612 * If this is a request for a synthetic attribute in the system.*
2613 * namespace use the generic infrastructure to resolve a handler
2614 * for it via sb->s_xattr.
2616 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
2617 return generic_getxattr(dentry
, name
, buffer
, size
);
2619 err
= shmem_xattr_validate(name
);
2623 return simple_xattr_get(&info
->xattrs
, name
, buffer
, size
);
2626 static int shmem_setxattr(struct dentry
*dentry
, const char *name
,
2627 const void *value
, size_t size
, int flags
)
2629 struct shmem_inode_info
*info
= SHMEM_I(dentry
->d_inode
);
2633 * If this is a request for a synthetic attribute in the system.*
2634 * namespace use the generic infrastructure to resolve a handler
2635 * for it via sb->s_xattr.
2637 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
2638 return generic_setxattr(dentry
, name
, value
, size
, flags
);
2640 err
= shmem_xattr_validate(name
);
2644 return simple_xattr_set(&info
->xattrs
, name
, value
, size
, flags
);
2647 static int shmem_removexattr(struct dentry
*dentry
, const char *name
)
2649 struct shmem_inode_info
*info
= SHMEM_I(dentry
->d_inode
);
2653 * If this is a request for a synthetic attribute in the system.*
2654 * namespace use the generic infrastructure to resolve a handler
2655 * for it via sb->s_xattr.
2657 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
2658 return generic_removexattr(dentry
, name
);
2660 err
= shmem_xattr_validate(name
);
2664 return simple_xattr_remove(&info
->xattrs
, name
);
2667 static ssize_t
shmem_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
2669 struct shmem_inode_info
*info
= SHMEM_I(dentry
->d_inode
);
2670 return simple_xattr_list(&info
->xattrs
, buffer
, size
);
2672 #endif /* CONFIG_TMPFS_XATTR */
2674 static const struct inode_operations shmem_short_symlink_operations
= {
2675 .readlink
= generic_readlink
,
2676 .follow_link
= shmem_follow_short_symlink
,
2677 #ifdef CONFIG_TMPFS_XATTR
2678 .setxattr
= shmem_setxattr
,
2679 .getxattr
= shmem_getxattr
,
2680 .listxattr
= shmem_listxattr
,
2681 .removexattr
= shmem_removexattr
,
2685 static const struct inode_operations shmem_symlink_inode_operations
= {
2686 .readlink
= generic_readlink
,
2687 .follow_link
= shmem_follow_link
,
2688 .put_link
= shmem_put_link
,
2689 #ifdef CONFIG_TMPFS_XATTR
2690 .setxattr
= shmem_setxattr
,
2691 .getxattr
= shmem_getxattr
,
2692 .listxattr
= shmem_listxattr
,
2693 .removexattr
= shmem_removexattr
,
2697 static struct dentry
*shmem_get_parent(struct dentry
*child
)
2699 return ERR_PTR(-ESTALE
);
2702 static int shmem_match(struct inode
*ino
, void *vfh
)
2706 inum
= (inum
<< 32) | fh
[1];
2707 return ino
->i_ino
== inum
&& fh
[0] == ino
->i_generation
;
2710 static struct dentry
*shmem_fh_to_dentry(struct super_block
*sb
,
2711 struct fid
*fid
, int fh_len
, int fh_type
)
2713 struct inode
*inode
;
2714 struct dentry
*dentry
= NULL
;
2721 inum
= (inum
<< 32) | fid
->raw
[1];
2723 inode
= ilookup5(sb
, (unsigned long)(inum
+ fid
->raw
[0]),
2724 shmem_match
, fid
->raw
);
2726 dentry
= d_find_alias(inode
);
2733 static int shmem_encode_fh(struct inode
*inode
, __u32
*fh
, int *len
,
2734 struct inode
*parent
)
2738 return FILEID_INVALID
;
2741 if (inode_unhashed(inode
)) {
2742 /* Unfortunately insert_inode_hash is not idempotent,
2743 * so as we hash inodes here rather than at creation
2744 * time, we need a lock to ensure we only try
2747 static DEFINE_SPINLOCK(lock
);
2749 if (inode_unhashed(inode
))
2750 __insert_inode_hash(inode
,
2751 inode
->i_ino
+ inode
->i_generation
);
2755 fh
[0] = inode
->i_generation
;
2756 fh
[1] = inode
->i_ino
;
2757 fh
[2] = ((__u64
)inode
->i_ino
) >> 32;
2763 static const struct export_operations shmem_export_ops
= {
2764 .get_parent
= shmem_get_parent
,
2765 .encode_fh
= shmem_encode_fh
,
2766 .fh_to_dentry
= shmem_fh_to_dentry
,
2769 static int shmem_parse_options(char *options
, struct shmem_sb_info
*sbinfo
,
2772 char *this_char
, *value
, *rest
;
2773 struct mempolicy
*mpol
= NULL
;
2777 while (options
!= NULL
) {
2778 this_char
= options
;
2781 * NUL-terminate this option: unfortunately,
2782 * mount options form a comma-separated list,
2783 * but mpol's nodelist may also contain commas.
2785 options
= strchr(options
, ',');
2786 if (options
== NULL
)
2789 if (!isdigit(*options
)) {
2796 if ((value
= strchr(this_char
,'=')) != NULL
) {
2800 "tmpfs: No value for mount option '%s'\n",
2805 if (!strcmp(this_char
,"size")) {
2806 unsigned long long size
;
2807 size
= memparse(value
,&rest
);
2809 size
<<= PAGE_SHIFT
;
2810 size
*= totalram_pages
;
2816 sbinfo
->max_blocks
=
2817 DIV_ROUND_UP(size
, PAGE_CACHE_SIZE
);
2818 } else if (!strcmp(this_char
,"nr_blocks")) {
2819 sbinfo
->max_blocks
= memparse(value
, &rest
);
2822 } else if (!strcmp(this_char
,"nr_inodes")) {
2823 sbinfo
->max_inodes
= memparse(value
, &rest
);
2826 } else if (!strcmp(this_char
,"mode")) {
2829 sbinfo
->mode
= simple_strtoul(value
, &rest
, 8) & 07777;
2832 } else if (!strcmp(this_char
,"uid")) {
2835 uid
= simple_strtoul(value
, &rest
, 0);
2838 sbinfo
->uid
= make_kuid(current_user_ns(), uid
);
2839 if (!uid_valid(sbinfo
->uid
))
2841 } else if (!strcmp(this_char
,"gid")) {
2844 gid
= simple_strtoul(value
, &rest
, 0);
2847 sbinfo
->gid
= make_kgid(current_user_ns(), gid
);
2848 if (!gid_valid(sbinfo
->gid
))
2850 } else if (!strcmp(this_char
,"mpol")) {
2853 if (mpol_parse_str(value
, &mpol
))
2856 printk(KERN_ERR
"tmpfs: Bad mount option %s\n",
2861 sbinfo
->mpol
= mpol
;
2865 printk(KERN_ERR
"tmpfs: Bad value '%s' for mount option '%s'\n",
2873 static int shmem_remount_fs(struct super_block
*sb
, int *flags
, char *data
)
2875 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
2876 struct shmem_sb_info config
= *sbinfo
;
2877 unsigned long inodes
;
2878 int error
= -EINVAL
;
2881 if (shmem_parse_options(data
, &config
, true))
2884 spin_lock(&sbinfo
->stat_lock
);
2885 inodes
= sbinfo
->max_inodes
- sbinfo
->free_inodes
;
2886 if (percpu_counter_compare(&sbinfo
->used_blocks
, config
.max_blocks
) > 0)
2888 if (config
.max_inodes
< inodes
)
2891 * Those tests disallow limited->unlimited while any are in use;
2892 * but we must separately disallow unlimited->limited, because
2893 * in that case we have no record of how much is already in use.
2895 if (config
.max_blocks
&& !sbinfo
->max_blocks
)
2897 if (config
.max_inodes
&& !sbinfo
->max_inodes
)
2901 sbinfo
->max_blocks
= config
.max_blocks
;
2902 sbinfo
->max_inodes
= config
.max_inodes
;
2903 sbinfo
->free_inodes
= config
.max_inodes
- inodes
;
2906 * Preserve previous mempolicy unless mpol remount option was specified.
2909 mpol_put(sbinfo
->mpol
);
2910 sbinfo
->mpol
= config
.mpol
; /* transfers initial ref */
2913 spin_unlock(&sbinfo
->stat_lock
);
2917 static int shmem_show_options(struct seq_file
*seq
, struct dentry
*root
)
2919 struct shmem_sb_info
*sbinfo
= SHMEM_SB(root
->d_sb
);
2921 if (sbinfo
->max_blocks
!= shmem_default_max_blocks())
2922 seq_printf(seq
, ",size=%luk",
2923 sbinfo
->max_blocks
<< (PAGE_CACHE_SHIFT
- 10));
2924 if (sbinfo
->max_inodes
!= shmem_default_max_inodes())
2925 seq_printf(seq
, ",nr_inodes=%lu", sbinfo
->max_inodes
);
2926 if (sbinfo
->mode
!= (S_IRWXUGO
| S_ISVTX
))
2927 seq_printf(seq
, ",mode=%03ho", sbinfo
->mode
);
2928 if (!uid_eq(sbinfo
->uid
, GLOBAL_ROOT_UID
))
2929 seq_printf(seq
, ",uid=%u",
2930 from_kuid_munged(&init_user_ns
, sbinfo
->uid
));
2931 if (!gid_eq(sbinfo
->gid
, GLOBAL_ROOT_GID
))
2932 seq_printf(seq
, ",gid=%u",
2933 from_kgid_munged(&init_user_ns
, sbinfo
->gid
));
2934 shmem_show_mpol(seq
, sbinfo
->mpol
);
2938 #define MFD_NAME_PREFIX "memfd:"
2939 #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
2940 #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
2942 #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
2944 SYSCALL_DEFINE2(memfd_create
,
2945 const char __user
*, uname
,
2946 unsigned int, flags
)
2948 struct shmem_inode_info
*info
;
2954 if (flags
& ~(unsigned int)MFD_ALL_FLAGS
)
2957 /* length includes terminating zero */
2958 len
= strnlen_user(uname
, MFD_NAME_MAX_LEN
+ 1);
2961 if (len
> MFD_NAME_MAX_LEN
+ 1)
2964 name
= kmalloc(len
+ MFD_NAME_PREFIX_LEN
, GFP_TEMPORARY
);
2968 strcpy(name
, MFD_NAME_PREFIX
);
2969 if (copy_from_user(&name
[MFD_NAME_PREFIX_LEN
], uname
, len
)) {
2974 /* terminating-zero may have changed after strnlen_user() returned */
2975 if (name
[len
+ MFD_NAME_PREFIX_LEN
- 1]) {
2980 fd
= get_unused_fd_flags((flags
& MFD_CLOEXEC
) ? O_CLOEXEC
: 0);
2986 file
= shmem_file_setup(name
, 0, VM_NORESERVE
);
2988 error
= PTR_ERR(file
);
2991 info
= SHMEM_I(file
->f_path
.dentry
->d_inode
);
2992 file
->f_mode
|= FMODE_LSEEK
| FMODE_PREAD
| FMODE_PWRITE
;
2993 file
->f_flags
|= O_RDWR
| O_LARGEFILE
;
2995 fd_install(fd
, file
);
3006 #endif /* CONFIG_TMPFS */
3008 static void shmem_put_super(struct super_block
*sb
)
3010 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
3012 percpu_counter_destroy(&sbinfo
->used_blocks
);
3013 mpol_put(sbinfo
->mpol
);
3015 sb
->s_fs_info
= NULL
;
3018 int shmem_fill_super(struct super_block
*sb
, void *data
, int silent
)
3020 struct inode
*inode
;
3021 struct shmem_sb_info
*sbinfo
;
3024 /* Round up to L1_CACHE_BYTES to resist false sharing */
3025 sbinfo
= kzalloc(max((int)sizeof(struct shmem_sb_info
),
3026 L1_CACHE_BYTES
), GFP_KERNEL
);
3030 sbinfo
->mode
= S_IRWXUGO
| S_ISVTX
;
3031 sbinfo
->uid
= current_fsuid();
3032 sbinfo
->gid
= current_fsgid();
3033 sb
->s_fs_info
= sbinfo
;
3037 * Per default we only allow half of the physical ram per
3038 * tmpfs instance, limiting inodes to one per page of lowmem;
3039 * but the internal instance is left unlimited.
3041 if (!(sb
->s_flags
& MS_NOUSER
)) {
3042 sbinfo
->max_blocks
= shmem_default_max_blocks();
3043 sbinfo
->max_inodes
= shmem_default_max_inodes();
3044 if (shmem_parse_options(data
, sbinfo
, false)) {
3049 sb
->s_export_op
= &shmem_export_ops
;
3050 sb
->s_flags
|= MS_NOSEC
;
3052 sb
->s_flags
|= MS_NOUSER
;
3055 spin_lock_init(&sbinfo
->stat_lock
);
3056 if (percpu_counter_init(&sbinfo
->used_blocks
, 0))
3058 sbinfo
->free_inodes
= sbinfo
->max_inodes
;
3060 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
3061 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
3062 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
3063 sb
->s_magic
= TMPFS_MAGIC
;
3064 sb
->s_op
= &shmem_ops
;
3065 sb
->s_time_gran
= 1;
3066 #ifdef CONFIG_TMPFS_XATTR
3067 sb
->s_xattr
= shmem_xattr_handlers
;
3069 #ifdef CONFIG_TMPFS_POSIX_ACL
3070 sb
->s_flags
|= MS_POSIXACL
;
3073 inode
= shmem_get_inode(sb
, NULL
, S_IFDIR
| sbinfo
->mode
, 0, VM_NORESERVE
);
3076 inode
->i_uid
= sbinfo
->uid
;
3077 inode
->i_gid
= sbinfo
->gid
;
3078 sb
->s_root
= d_make_root(inode
);
3084 shmem_put_super(sb
);
3088 static struct kmem_cache
*shmem_inode_cachep
;
3090 static struct inode
*shmem_alloc_inode(struct super_block
*sb
)
3092 struct shmem_inode_info
*info
;
3093 info
= kmem_cache_alloc(shmem_inode_cachep
, GFP_KERNEL
);
3096 return &info
->vfs_inode
;
3099 static void shmem_destroy_callback(struct rcu_head
*head
)
3101 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
3102 kmem_cache_free(shmem_inode_cachep
, SHMEM_I(inode
));
3105 static void shmem_destroy_inode(struct inode
*inode
)
3107 if (S_ISREG(inode
->i_mode
))
3108 mpol_free_shared_policy(&SHMEM_I(inode
)->policy
);
3109 call_rcu(&inode
->i_rcu
, shmem_destroy_callback
);
3112 static void shmem_init_inode(void *foo
)
3114 struct shmem_inode_info
*info
= foo
;
3115 inode_init_once(&info
->vfs_inode
);
3118 static int shmem_init_inodecache(void)
3120 shmem_inode_cachep
= kmem_cache_create("shmem_inode_cache",
3121 sizeof(struct shmem_inode_info
),
3122 0, SLAB_PANIC
, shmem_init_inode
);
3126 static void shmem_destroy_inodecache(void)
3128 kmem_cache_destroy(shmem_inode_cachep
);
3131 static const struct address_space_operations shmem_aops
= {
3132 .writepage
= shmem_writepage
,
3133 .set_page_dirty
= __set_page_dirty_no_writeback
,
3135 .write_begin
= shmem_write_begin
,
3136 .write_end
= shmem_write_end
,
3138 .migratepage
= migrate_page
,
3139 .error_remove_page
= generic_error_remove_page
,
3142 static const struct file_operations shmem_file_operations
= {
3145 .llseek
= shmem_file_llseek
,
3146 .read
= do_sync_read
,
3147 .write
= do_sync_write
,
3148 .aio_read
= shmem_file_aio_read
,
3149 .aio_write
= generic_file_aio_write
,
3150 .fsync
= noop_fsync
,
3151 .splice_read
= shmem_file_splice_read
,
3152 .splice_write
= generic_file_splice_write
,
3153 .fallocate
= shmem_fallocate
,
3157 static const struct inode_operations shmem_inode_operations
= {
3158 .setattr
= shmem_setattr
,
3159 #ifdef CONFIG_TMPFS_XATTR
3160 .setxattr
= shmem_setxattr
,
3161 .getxattr
= shmem_getxattr
,
3162 .listxattr
= shmem_listxattr
,
3163 .removexattr
= shmem_removexattr
,
3167 static const struct inode_operations shmem_dir_inode_operations
= {
3169 .create
= shmem_create
,
3170 .lookup
= simple_lookup
,
3172 .unlink
= shmem_unlink
,
3173 .symlink
= shmem_symlink
,
3174 .mkdir
= shmem_mkdir
,
3175 .rmdir
= shmem_rmdir
,
3176 .mknod
= shmem_mknod
,
3177 .rename
= shmem_rename
,
3178 .tmpfile
= shmem_tmpfile
,
3180 #ifdef CONFIG_TMPFS_XATTR
3181 .setxattr
= shmem_setxattr
,
3182 .getxattr
= shmem_getxattr
,
3183 .listxattr
= shmem_listxattr
,
3184 .removexattr
= shmem_removexattr
,
3186 #ifdef CONFIG_TMPFS_POSIX_ACL
3187 .setattr
= shmem_setattr
,
3191 static const struct inode_operations shmem_special_inode_operations
= {
3192 #ifdef CONFIG_TMPFS_XATTR
3193 .setxattr
= shmem_setxattr
,
3194 .getxattr
= shmem_getxattr
,
3195 .listxattr
= shmem_listxattr
,
3196 .removexattr
= shmem_removexattr
,
3198 #ifdef CONFIG_TMPFS_POSIX_ACL
3199 .setattr
= shmem_setattr
,
3203 static const struct super_operations shmem_ops
= {
3204 .alloc_inode
= shmem_alloc_inode
,
3205 .destroy_inode
= shmem_destroy_inode
,
3207 .statfs
= shmem_statfs
,
3208 .remount_fs
= shmem_remount_fs
,
3209 .show_options
= shmem_show_options
,
3211 .evict_inode
= shmem_evict_inode
,
3212 .drop_inode
= generic_delete_inode
,
3213 .put_super
= shmem_put_super
,
3216 static const struct vm_operations_struct shmem_vm_ops
= {
3217 .fault
= shmem_fault
,
3219 .set_policy
= shmem_set_policy
,
3220 .get_policy
= shmem_get_policy
,
3222 .remap_pages
= generic_file_remap_pages
,
3225 static struct dentry
*shmem_mount(struct file_system_type
*fs_type
,
3226 int flags
, const char *dev_name
, void *data
)
3228 return mount_nodev(fs_type
, flags
, data
, shmem_fill_super
);
3231 static struct file_system_type shmem_fs_type
= {
3232 .owner
= THIS_MODULE
,
3234 .mount
= shmem_mount
,
3235 .kill_sb
= kill_litter_super
,
3236 .fs_flags
= FS_USERNS_MOUNT
,
3239 int __init
shmem_init(void)
3243 error
= bdi_init(&shmem_backing_dev_info
);
3247 error
= shmem_init_inodecache();
3251 error
= register_filesystem(&shmem_fs_type
);
3253 printk(KERN_ERR
"Could not register tmpfs\n");
3257 shm_mnt
= vfs_kern_mount(&shmem_fs_type
, MS_NOUSER
,
3258 shmem_fs_type
.name
, NULL
);
3259 if (IS_ERR(shm_mnt
)) {
3260 error
= PTR_ERR(shm_mnt
);
3261 printk(KERN_ERR
"Could not kern_mount tmpfs\n");
3267 unregister_filesystem(&shmem_fs_type
);
3269 shmem_destroy_inodecache();
3271 bdi_destroy(&shmem_backing_dev_info
);
3273 shm_mnt
= ERR_PTR(error
);
3277 #else /* !CONFIG_SHMEM */
3280 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3282 * This is intended for small system where the benefits of the full
3283 * shmem code (swap-backed and resource-limited) are outweighed by
3284 * their complexity. On systems without swap this code should be
3285 * effectively equivalent, but much lighter weight.
3288 static struct file_system_type shmem_fs_type
= {
3290 .mount
= ramfs_mount
,
3291 .kill_sb
= kill_litter_super
,
3292 .fs_flags
= FS_USERNS_MOUNT
,
3295 int __init
shmem_init(void)
3297 BUG_ON(register_filesystem(&shmem_fs_type
) != 0);
3299 shm_mnt
= kern_mount(&shmem_fs_type
);
3300 BUG_ON(IS_ERR(shm_mnt
));
3305 int shmem_unuse(swp_entry_t swap
, struct page
*page
)
3310 int shmem_lock(struct file
*file
, int lock
, struct user_struct
*user
)
3315 void shmem_unlock_mapping(struct address_space
*mapping
)
3319 void shmem_truncate_range(struct inode
*inode
, loff_t lstart
, loff_t lend
)
3321 truncate_inode_pages_range(inode
->i_mapping
, lstart
, lend
);
3323 EXPORT_SYMBOL_GPL(shmem_truncate_range
);
3325 #define shmem_vm_ops generic_file_vm_ops
3326 #define shmem_file_operations ramfs_file_operations
3327 #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
3328 #define shmem_acct_size(flags, size) 0
3329 #define shmem_unacct_size(flags, size) do {} while (0)
3331 #endif /* CONFIG_SHMEM */
3335 static struct dentry_operations anon_ops
= {
3336 .d_dname
= simple_dname
3340 * shmem_file_setup - get an unlinked file living in tmpfs
3341 * @name: name for dentry (to be seen in /proc/<pid>/maps
3342 * @size: size to be set for the file
3343 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3345 struct file
*shmem_file_setup(const char *name
, loff_t size
, unsigned long flags
)
3348 struct inode
*inode
;
3350 struct super_block
*sb
;
3353 if (IS_ERR(shm_mnt
))
3354 return ERR_CAST(shm_mnt
);
3356 if (size
< 0 || size
> MAX_LFS_FILESIZE
)
3357 return ERR_PTR(-EINVAL
);
3359 if (shmem_acct_size(flags
, size
))
3360 return ERR_PTR(-ENOMEM
);
3362 res
= ERR_PTR(-ENOMEM
);
3364 this.len
= strlen(name
);
3365 this.hash
= 0; /* will go */
3366 sb
= shm_mnt
->mnt_sb
;
3367 path
.dentry
= d_alloc_pseudo(sb
, &this);
3370 d_set_d_op(path
.dentry
, &anon_ops
);
3371 path
.mnt
= mntget(shm_mnt
);
3373 res
= ERR_PTR(-ENOSPC
);
3374 inode
= shmem_get_inode(sb
, NULL
, S_IFREG
| S_IRWXUGO
, 0, flags
);
3378 d_instantiate(path
.dentry
, inode
);
3379 inode
->i_size
= size
;
3380 clear_nlink(inode
); /* It is unlinked */
3381 res
= ERR_PTR(ramfs_nommu_expand_for_mapping(inode
, size
));
3385 res
= alloc_file(&path
, FMODE_WRITE
| FMODE_READ
,
3386 &shmem_file_operations
);
3395 shmem_unacct_size(flags
, size
);
3398 EXPORT_SYMBOL_GPL(shmem_file_setup
);
3400 void shmem_set_file(struct vm_area_struct
*vma
, struct file
*file
)
3404 vma
->vm_file
= file
;
3405 vma
->vm_ops
= &shmem_vm_ops
;
3409 * shmem_zero_setup - setup a shared anonymous mapping
3410 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
3412 int shmem_zero_setup(struct vm_area_struct
*vma
)
3415 loff_t size
= vma
->vm_end
- vma
->vm_start
;
3417 file
= shmem_file_setup("dev/zero", size
, vma
->vm_flags
);
3419 return PTR_ERR(file
);
3421 shmem_set_file(vma
, file
);
3426 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3427 * @mapping: the page's address_space
3428 * @index: the page index
3429 * @gfp: the page allocator flags to use if allocating
3431 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3432 * with any new page allocations done using the specified allocation flags.
3433 * But read_cache_page_gfp() uses the ->readpage() method: which does not
3434 * suit tmpfs, since it may have pages in swapcache, and needs to find those
3435 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
3437 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
3438 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
3440 struct page
*shmem_read_mapping_page_gfp(struct address_space
*mapping
,
3441 pgoff_t index
, gfp_t gfp
)
3444 struct inode
*inode
= mapping
->host
;
3448 BUG_ON(mapping
->a_ops
!= &shmem_aops
);
3449 error
= shmem_getpage_gfp(inode
, index
, &page
, SGP_CACHE
, gfp
, NULL
);
3451 page
= ERR_PTR(error
);
3457 * The tiny !SHMEM case uses ramfs without swap
3459 return read_cache_page_gfp(mapping
, index
, gfp
);
3462 EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp
);