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/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/fcntl.h>
71 #include <asm/uaccess.h>
72 #include <asm/pgtable.h>
74 #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
75 #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
77 /* Pretend that each entry is of this size in directory's i_size */
78 #define BOGO_DIRENT_SIZE 20
80 /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
81 #define SHORT_SYMLINK_LEN 128
84 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
85 * inode->i_private (with i_mutex making sure that it has only one user at
86 * a time): we would prefer not to enlarge the shmem inode just for that.
89 wait_queue_head_t
*waitq
; /* faults into hole wait for punch to end */
90 pgoff_t start
; /* start of range currently being fallocated */
91 pgoff_t next
; /* the next page offset to be fallocated */
92 pgoff_t nr_falloced
; /* how many new pages have been fallocated */
93 pgoff_t nr_unswapped
; /* how often writepage refused to swap out */
96 /* Flag allocation requirements to shmem_getpage */
98 SGP_READ
, /* don't exceed i_size, don't allocate page */
99 SGP_CACHE
, /* don't exceed i_size, may allocate page */
100 SGP_DIRTY
, /* like SGP_CACHE, but set new page dirty */
101 SGP_WRITE
, /* may exceed i_size, may allocate !Uptodate page */
102 SGP_FALLOC
, /* like SGP_WRITE, but make existing page Uptodate */
106 static unsigned long shmem_default_max_blocks(void)
108 return totalram_pages
/ 2;
111 static unsigned long shmem_default_max_inodes(void)
113 return min(totalram_pages
- totalhigh_pages
, totalram_pages
/ 2);
117 static bool shmem_should_replace_page(struct page
*page
, gfp_t gfp
);
118 static int shmem_replace_page(struct page
**pagep
, gfp_t gfp
,
119 struct shmem_inode_info
*info
, pgoff_t index
);
120 static int shmem_getpage_gfp(struct inode
*inode
, pgoff_t index
,
121 struct page
**pagep
, enum sgp_type sgp
, gfp_t gfp
, int *fault_type
);
123 static inline int shmem_getpage(struct inode
*inode
, pgoff_t index
,
124 struct page
**pagep
, enum sgp_type sgp
, int *fault_type
)
126 return shmem_getpage_gfp(inode
, index
, pagep
, sgp
,
127 mapping_gfp_mask(inode
->i_mapping
), fault_type
);
130 static inline struct shmem_sb_info
*SHMEM_SB(struct super_block
*sb
)
132 return sb
->s_fs_info
;
136 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
137 * for shared memory and for shared anonymous (/dev/zero) mappings
138 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
139 * consistent with the pre-accounting of private mappings ...
141 static inline int shmem_acct_size(unsigned long flags
, loff_t size
)
143 return (flags
& VM_NORESERVE
) ?
144 0 : security_vm_enough_memory_mm(current
->mm
, VM_ACCT(size
));
147 static inline void shmem_unacct_size(unsigned long flags
, loff_t size
)
149 if (!(flags
& VM_NORESERVE
))
150 vm_unacct_memory(VM_ACCT(size
));
153 static inline int shmem_reacct_size(unsigned long flags
,
154 loff_t oldsize
, loff_t newsize
)
156 if (!(flags
& VM_NORESERVE
)) {
157 if (VM_ACCT(newsize
) > VM_ACCT(oldsize
))
158 return security_vm_enough_memory_mm(current
->mm
,
159 VM_ACCT(newsize
) - VM_ACCT(oldsize
));
160 else if (VM_ACCT(newsize
) < VM_ACCT(oldsize
))
161 vm_unacct_memory(VM_ACCT(oldsize
) - VM_ACCT(newsize
));
167 * ... whereas tmpfs objects are accounted incrementally as
168 * pages are allocated, in order to allow huge sparse files.
169 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
170 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
172 static inline int shmem_acct_block(unsigned long flags
)
174 return (flags
& VM_NORESERVE
) ?
175 security_vm_enough_memory_mm(current
->mm
, VM_ACCT(PAGE_CACHE_SIZE
)) : 0;
178 static inline void shmem_unacct_blocks(unsigned long flags
, long pages
)
180 if (flags
& VM_NORESERVE
)
181 vm_unacct_memory(pages
* VM_ACCT(PAGE_CACHE_SIZE
));
184 static const struct super_operations shmem_ops
;
185 static const struct address_space_operations shmem_aops
;
186 static const struct file_operations shmem_file_operations
;
187 static const struct inode_operations shmem_inode_operations
;
188 static const struct inode_operations shmem_dir_inode_operations
;
189 static const struct inode_operations shmem_special_inode_operations
;
190 static const struct vm_operations_struct shmem_vm_ops
;
192 static struct backing_dev_info shmem_backing_dev_info __read_mostly
= {
193 .ra_pages
= 0, /* No readahead */
194 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
| BDI_CAP_SWAP_BACKED
,
197 static LIST_HEAD(shmem_swaplist
);
198 static DEFINE_MUTEX(shmem_swaplist_mutex
);
200 static int shmem_reserve_inode(struct super_block
*sb
)
202 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
203 if (sbinfo
->max_inodes
) {
204 spin_lock(&sbinfo
->stat_lock
);
205 if (!sbinfo
->free_inodes
) {
206 spin_unlock(&sbinfo
->stat_lock
);
209 sbinfo
->free_inodes
--;
210 spin_unlock(&sbinfo
->stat_lock
);
215 static void shmem_free_inode(struct super_block
*sb
)
217 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
218 if (sbinfo
->max_inodes
) {
219 spin_lock(&sbinfo
->stat_lock
);
220 sbinfo
->free_inodes
++;
221 spin_unlock(&sbinfo
->stat_lock
);
226 * shmem_recalc_inode - recalculate the block usage of an inode
227 * @inode: inode to recalc
229 * We have to calculate the free blocks since the mm can drop
230 * undirtied hole pages behind our back.
232 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
233 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
235 * It has to be called with the spinlock held.
237 static void shmem_recalc_inode(struct inode
*inode
)
239 struct shmem_inode_info
*info
= SHMEM_I(inode
);
242 freed
= info
->alloced
- info
->swapped
- inode
->i_mapping
->nrpages
;
244 struct shmem_sb_info
*sbinfo
= SHMEM_SB(inode
->i_sb
);
245 if (sbinfo
->max_blocks
)
246 percpu_counter_add(&sbinfo
->used_blocks
, -freed
);
247 info
->alloced
-= freed
;
248 inode
->i_blocks
-= freed
* BLOCKS_PER_PAGE
;
249 shmem_unacct_blocks(info
->flags
, freed
);
254 * Replace item expected in radix tree by a new item, while holding tree lock.
256 static int shmem_radix_tree_replace(struct address_space
*mapping
,
257 pgoff_t index
, void *expected
, void *replacement
)
262 VM_BUG_ON(!expected
);
263 VM_BUG_ON(!replacement
);
264 pslot
= radix_tree_lookup_slot(&mapping
->page_tree
, index
);
267 item
= radix_tree_deref_slot_protected(pslot
, &mapping
->tree_lock
);
268 if (item
!= expected
)
270 radix_tree_replace_slot(pslot
, replacement
);
275 * Sometimes, before we decide whether to proceed or to fail, we must check
276 * that an entry was not already brought back from swap by a racing thread.
278 * Checking page is not enough: by the time a SwapCache page is locked, it
279 * might be reused, and again be SwapCache, using the same swap as before.
281 static bool shmem_confirm_swap(struct address_space
*mapping
,
282 pgoff_t index
, swp_entry_t swap
)
287 item
= radix_tree_lookup(&mapping
->page_tree
, index
);
289 return item
== swp_to_radix_entry(swap
);
293 * Like add_to_page_cache_locked, but error if expected item has gone.
295 static int shmem_add_to_page_cache(struct page
*page
,
296 struct address_space
*mapping
,
297 pgoff_t index
, void *expected
)
301 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
302 VM_BUG_ON_PAGE(!PageSwapBacked(page
), page
);
304 page_cache_get(page
);
305 page
->mapping
= mapping
;
308 spin_lock_irq(&mapping
->tree_lock
);
310 error
= radix_tree_insert(&mapping
->page_tree
, index
, page
);
312 error
= shmem_radix_tree_replace(mapping
, index
, expected
,
316 __inc_zone_page_state(page
, NR_FILE_PAGES
);
317 __inc_zone_page_state(page
, NR_SHMEM
);
318 spin_unlock_irq(&mapping
->tree_lock
);
320 page
->mapping
= NULL
;
321 spin_unlock_irq(&mapping
->tree_lock
);
322 page_cache_release(page
);
328 * Like delete_from_page_cache, but substitutes swap for page.
330 static void shmem_delete_from_page_cache(struct page
*page
, void *radswap
)
332 struct address_space
*mapping
= page
->mapping
;
335 spin_lock_irq(&mapping
->tree_lock
);
336 error
= shmem_radix_tree_replace(mapping
, page
->index
, page
, radswap
);
337 page
->mapping
= NULL
;
339 __dec_zone_page_state(page
, NR_FILE_PAGES
);
340 __dec_zone_page_state(page
, NR_SHMEM
);
341 spin_unlock_irq(&mapping
->tree_lock
);
342 page_cache_release(page
);
347 * Remove swap entry from radix tree, free the swap and its page cache.
349 static int shmem_free_swap(struct address_space
*mapping
,
350 pgoff_t index
, void *radswap
)
354 spin_lock_irq(&mapping
->tree_lock
);
355 old
= radix_tree_delete_item(&mapping
->page_tree
, index
, radswap
);
356 spin_unlock_irq(&mapping
->tree_lock
);
359 free_swap_and_cache(radix_to_swp_entry(radswap
));
364 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
366 void shmem_unlock_mapping(struct address_space
*mapping
)
369 pgoff_t indices
[PAGEVEC_SIZE
];
372 pagevec_init(&pvec
, 0);
374 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
376 while (!mapping_unevictable(mapping
)) {
378 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
379 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
381 pvec
.nr
= find_get_entries(mapping
, index
,
382 PAGEVEC_SIZE
, pvec
.pages
, indices
);
385 index
= indices
[pvec
.nr
- 1] + 1;
386 pagevec_remove_exceptionals(&pvec
);
387 check_move_unevictable_pages(pvec
.pages
, pvec
.nr
);
388 pagevec_release(&pvec
);
394 * Remove range of pages and swap entries from radix tree, and free them.
395 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
397 static void shmem_undo_range(struct inode
*inode
, loff_t lstart
, loff_t lend
,
400 struct address_space
*mapping
= inode
->i_mapping
;
401 struct shmem_inode_info
*info
= SHMEM_I(inode
);
402 pgoff_t start
= (lstart
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
403 pgoff_t end
= (lend
+ 1) >> PAGE_CACHE_SHIFT
;
404 unsigned int partial_start
= lstart
& (PAGE_CACHE_SIZE
- 1);
405 unsigned int partial_end
= (lend
+ 1) & (PAGE_CACHE_SIZE
- 1);
407 pgoff_t indices
[PAGEVEC_SIZE
];
408 long nr_swaps_freed
= 0;
413 end
= -1; /* unsigned, so actually very big */
415 pagevec_init(&pvec
, 0);
417 while (index
< end
) {
418 pvec
.nr
= find_get_entries(mapping
, index
,
419 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
),
420 pvec
.pages
, indices
);
423 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
424 struct page
*page
= pvec
.pages
[i
];
430 if (radix_tree_exceptional_entry(page
)) {
433 nr_swaps_freed
+= !shmem_free_swap(mapping
,
438 if (!trylock_page(page
))
440 if (!unfalloc
|| !PageUptodate(page
)) {
441 if (page
->mapping
== mapping
) {
442 VM_BUG_ON_PAGE(PageWriteback(page
), page
);
443 truncate_inode_page(mapping
, page
);
448 pagevec_remove_exceptionals(&pvec
);
449 pagevec_release(&pvec
);
455 struct page
*page
= NULL
;
456 shmem_getpage(inode
, start
- 1, &page
, SGP_READ
, NULL
);
458 unsigned int top
= PAGE_CACHE_SIZE
;
463 zero_user_segment(page
, partial_start
, top
);
464 set_page_dirty(page
);
466 page_cache_release(page
);
470 struct page
*page
= NULL
;
471 shmem_getpage(inode
, end
, &page
, SGP_READ
, NULL
);
473 zero_user_segment(page
, 0, partial_end
);
474 set_page_dirty(page
);
476 page_cache_release(page
);
483 while (index
< end
) {
486 pvec
.nr
= find_get_entries(mapping
, index
,
487 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
),
488 pvec
.pages
, indices
);
490 /* If all gone or hole-punch or unfalloc, we're done */
491 if (index
== start
|| end
!= -1)
493 /* But if truncating, restart to make sure all gone */
497 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
498 struct page
*page
= pvec
.pages
[i
];
504 if (radix_tree_exceptional_entry(page
)) {
507 if (shmem_free_swap(mapping
, index
, page
)) {
508 /* Swap was replaced by page: retry */
517 if (!unfalloc
|| !PageUptodate(page
)) {
518 if (page
->mapping
== mapping
) {
519 VM_BUG_ON_PAGE(PageWriteback(page
), page
);
520 truncate_inode_page(mapping
, page
);
522 /* Page was replaced by swap: retry */
530 pagevec_remove_exceptionals(&pvec
);
531 pagevec_release(&pvec
);
535 spin_lock(&info
->lock
);
536 info
->swapped
-= nr_swaps_freed
;
537 shmem_recalc_inode(inode
);
538 spin_unlock(&info
->lock
);
541 void shmem_truncate_range(struct inode
*inode
, loff_t lstart
, loff_t lend
)
543 shmem_undo_range(inode
, lstart
, lend
, false);
544 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
546 EXPORT_SYMBOL_GPL(shmem_truncate_range
);
548 static int shmem_setattr(struct dentry
*dentry
, struct iattr
*attr
)
550 struct inode
*inode
= dentry
->d_inode
;
551 struct shmem_inode_info
*info
= SHMEM_I(inode
);
554 error
= inode_change_ok(inode
, attr
);
558 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
559 loff_t oldsize
= inode
->i_size
;
560 loff_t newsize
= attr
->ia_size
;
562 /* protected by i_mutex */
563 if ((newsize
< oldsize
&& (info
->seals
& F_SEAL_SHRINK
)) ||
564 (newsize
> oldsize
&& (info
->seals
& F_SEAL_GROW
)))
567 if (newsize
!= oldsize
) {
568 error
= shmem_reacct_size(SHMEM_I(inode
)->flags
,
572 i_size_write(inode
, newsize
);
573 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
575 if (newsize
< oldsize
) {
576 loff_t holebegin
= round_up(newsize
, PAGE_SIZE
);
577 unmap_mapping_range(inode
->i_mapping
, holebegin
, 0, 1);
578 shmem_truncate_range(inode
, newsize
, (loff_t
)-1);
579 /* unmap again to remove racily COWed private pages */
580 unmap_mapping_range(inode
->i_mapping
, holebegin
, 0, 1);
584 setattr_copy(inode
, attr
);
585 if (attr
->ia_valid
& ATTR_MODE
)
586 error
= posix_acl_chmod(inode
, inode
->i_mode
);
590 static void shmem_evict_inode(struct inode
*inode
)
592 struct shmem_inode_info
*info
= SHMEM_I(inode
);
594 if (inode
->i_mapping
->a_ops
== &shmem_aops
) {
595 shmem_unacct_size(info
->flags
, inode
->i_size
);
597 shmem_truncate_range(inode
, 0, (loff_t
)-1);
598 if (!list_empty(&info
->swaplist
)) {
599 mutex_lock(&shmem_swaplist_mutex
);
600 list_del_init(&info
->swaplist
);
601 mutex_unlock(&shmem_swaplist_mutex
);
604 kfree(info
->symlink
);
606 simple_xattrs_free(&info
->xattrs
);
607 WARN_ON(inode
->i_blocks
);
608 shmem_free_inode(inode
->i_sb
);
613 * If swap found in inode, free it and move page from swapcache to filecache.
615 static int shmem_unuse_inode(struct shmem_inode_info
*info
,
616 swp_entry_t swap
, struct page
**pagep
)
618 struct address_space
*mapping
= info
->vfs_inode
.i_mapping
;
624 radswap
= swp_to_radix_entry(swap
);
625 index
= radix_tree_locate_item(&mapping
->page_tree
, radswap
);
627 return -EAGAIN
; /* tell shmem_unuse we found nothing */
630 * Move _head_ to start search for next from here.
631 * But be careful: shmem_evict_inode checks list_empty without taking
632 * mutex, and there's an instant in list_move_tail when info->swaplist
633 * would appear empty, if it were the only one on shmem_swaplist.
635 if (shmem_swaplist
.next
!= &info
->swaplist
)
636 list_move_tail(&shmem_swaplist
, &info
->swaplist
);
638 gfp
= mapping_gfp_mask(mapping
);
639 if (shmem_should_replace_page(*pagep
, gfp
)) {
640 mutex_unlock(&shmem_swaplist_mutex
);
641 error
= shmem_replace_page(pagep
, gfp
, info
, index
);
642 mutex_lock(&shmem_swaplist_mutex
);
644 * We needed to drop mutex to make that restrictive page
645 * allocation, but the inode might have been freed while we
646 * dropped it: although a racing shmem_evict_inode() cannot
647 * complete without emptying the radix_tree, our page lock
648 * on this swapcache page is not enough to prevent that -
649 * free_swap_and_cache() of our swap entry will only
650 * trylock_page(), removing swap from radix_tree whatever.
652 * We must not proceed to shmem_add_to_page_cache() if the
653 * inode has been freed, but of course we cannot rely on
654 * inode or mapping or info to check that. However, we can
655 * safely check if our swap entry is still in use (and here
656 * it can't have got reused for another page): if it's still
657 * in use, then the inode cannot have been freed yet, and we
658 * can safely proceed (if it's no longer in use, that tells
659 * nothing about the inode, but we don't need to unuse swap).
661 if (!page_swapcount(*pagep
))
666 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
667 * but also to hold up shmem_evict_inode(): so inode cannot be freed
668 * beneath us (pagelock doesn't help until the page is in pagecache).
671 error
= shmem_add_to_page_cache(*pagep
, mapping
, index
,
673 if (error
!= -ENOMEM
) {
675 * Truncation and eviction use free_swap_and_cache(), which
676 * only does trylock page: if we raced, best clean up here.
678 delete_from_swap_cache(*pagep
);
679 set_page_dirty(*pagep
);
681 spin_lock(&info
->lock
);
683 spin_unlock(&info
->lock
);
691 * Search through swapped inodes to find and replace swap by page.
693 int shmem_unuse(swp_entry_t swap
, struct page
*page
)
695 struct list_head
*this, *next
;
696 struct shmem_inode_info
*info
;
697 struct mem_cgroup
*memcg
;
701 * There's a faint possibility that swap page was replaced before
702 * caller locked it: caller will come back later with the right page.
704 if (unlikely(!PageSwapCache(page
) || page_private(page
) != swap
.val
))
708 * Charge page using GFP_KERNEL while we can wait, before taking
709 * the shmem_swaplist_mutex which might hold up shmem_writepage().
710 * Charged back to the user (not to caller) when swap account is used.
712 error
= mem_cgroup_try_charge(page
, current
->mm
, GFP_KERNEL
, &memcg
);
715 /* No radix_tree_preload: swap entry keeps a place for page in tree */
718 mutex_lock(&shmem_swaplist_mutex
);
719 list_for_each_safe(this, next
, &shmem_swaplist
) {
720 info
= list_entry(this, struct shmem_inode_info
, swaplist
);
722 error
= shmem_unuse_inode(info
, swap
, &page
);
724 list_del_init(&info
->swaplist
);
726 if (error
!= -EAGAIN
)
728 /* found nothing in this: move on to search the next */
730 mutex_unlock(&shmem_swaplist_mutex
);
733 if (error
!= -ENOMEM
)
735 mem_cgroup_cancel_charge(page
, memcg
);
737 mem_cgroup_commit_charge(page
, memcg
, true);
740 page_cache_release(page
);
745 * Move the page from the page cache to the swap cache.
747 static int shmem_writepage(struct page
*page
, struct writeback_control
*wbc
)
749 struct shmem_inode_info
*info
;
750 struct address_space
*mapping
;
755 BUG_ON(!PageLocked(page
));
756 mapping
= page
->mapping
;
758 inode
= mapping
->host
;
759 info
= SHMEM_I(inode
);
760 if (info
->flags
& VM_LOCKED
)
762 if (!total_swap_pages
)
766 * shmem_backing_dev_info's capabilities prevent regular writeback or
767 * sync from ever calling shmem_writepage; but a stacking filesystem
768 * might use ->writepage of its underlying filesystem, in which case
769 * tmpfs should write out to swap only in response to memory pressure,
770 * and not for the writeback threads or sync.
772 if (!wbc
->for_reclaim
) {
773 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
778 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
779 * value into swapfile.c, the only way we can correctly account for a
780 * fallocated page arriving here is now to initialize it and write it.
782 * That's okay for a page already fallocated earlier, but if we have
783 * not yet completed the fallocation, then (a) we want to keep track
784 * of this page in case we have to undo it, and (b) it may not be a
785 * good idea to continue anyway, once we're pushing into swap. So
786 * reactivate the page, and let shmem_fallocate() quit when too many.
788 if (!PageUptodate(page
)) {
789 if (inode
->i_private
) {
790 struct shmem_falloc
*shmem_falloc
;
791 spin_lock(&inode
->i_lock
);
792 shmem_falloc
= inode
->i_private
;
794 !shmem_falloc
->waitq
&&
795 index
>= shmem_falloc
->start
&&
796 index
< shmem_falloc
->next
)
797 shmem_falloc
->nr_unswapped
++;
800 spin_unlock(&inode
->i_lock
);
804 clear_highpage(page
);
805 flush_dcache_page(page
);
806 SetPageUptodate(page
);
809 swap
= get_swap_page();
814 * Add inode to shmem_unuse()'s list of swapped-out inodes,
815 * if it's not already there. Do it now before the page is
816 * moved to swap cache, when its pagelock no longer protects
817 * the inode from eviction. But don't unlock the mutex until
818 * we've incremented swapped, because shmem_unuse_inode() will
819 * prune a !swapped inode from the swaplist under this mutex.
821 mutex_lock(&shmem_swaplist_mutex
);
822 if (list_empty(&info
->swaplist
))
823 list_add_tail(&info
->swaplist
, &shmem_swaplist
);
825 if (add_to_swap_cache(page
, swap
, GFP_ATOMIC
) == 0) {
826 swap_shmem_alloc(swap
);
827 shmem_delete_from_page_cache(page
, swp_to_radix_entry(swap
));
829 spin_lock(&info
->lock
);
831 shmem_recalc_inode(inode
);
832 spin_unlock(&info
->lock
);
834 mutex_unlock(&shmem_swaplist_mutex
);
835 BUG_ON(page_mapped(page
));
836 swap_writepage(page
, wbc
);
840 mutex_unlock(&shmem_swaplist_mutex
);
841 swapcache_free(swap
);
843 set_page_dirty(page
);
844 if (wbc
->for_reclaim
)
845 return AOP_WRITEPAGE_ACTIVATE
; /* Return with page locked */
852 static void shmem_show_mpol(struct seq_file
*seq
, struct mempolicy
*mpol
)
856 if (!mpol
|| mpol
->mode
== MPOL_DEFAULT
)
857 return; /* show nothing */
859 mpol_to_str(buffer
, sizeof(buffer
), mpol
);
861 seq_printf(seq
, ",mpol=%s", buffer
);
864 static struct mempolicy
*shmem_get_sbmpol(struct shmem_sb_info
*sbinfo
)
866 struct mempolicy
*mpol
= NULL
;
868 spin_lock(&sbinfo
->stat_lock
); /* prevent replace/use races */
871 spin_unlock(&sbinfo
->stat_lock
);
875 #endif /* CONFIG_TMPFS */
877 static struct page
*shmem_swapin(swp_entry_t swap
, gfp_t gfp
,
878 struct shmem_inode_info
*info
, pgoff_t index
)
880 struct vm_area_struct pvma
;
883 /* Create a pseudo vma that just contains the policy */
885 /* Bias interleave by inode number to distribute better across nodes */
886 pvma
.vm_pgoff
= index
+ info
->vfs_inode
.i_ino
;
888 pvma
.vm_policy
= mpol_shared_policy_lookup(&info
->policy
, index
);
890 page
= swapin_readahead(swap
, gfp
, &pvma
, 0);
892 /* Drop reference taken by mpol_shared_policy_lookup() */
893 mpol_cond_put(pvma
.vm_policy
);
898 static struct page
*shmem_alloc_page(gfp_t gfp
,
899 struct shmem_inode_info
*info
, pgoff_t index
)
901 struct vm_area_struct pvma
;
904 /* Create a pseudo vma that just contains the policy */
906 /* Bias interleave by inode number to distribute better across nodes */
907 pvma
.vm_pgoff
= index
+ info
->vfs_inode
.i_ino
;
909 pvma
.vm_policy
= mpol_shared_policy_lookup(&info
->policy
, index
);
911 page
= alloc_page_vma(gfp
, &pvma
, 0);
913 /* Drop reference taken by mpol_shared_policy_lookup() */
914 mpol_cond_put(pvma
.vm_policy
);
918 #else /* !CONFIG_NUMA */
920 static inline void shmem_show_mpol(struct seq_file
*seq
, struct mempolicy
*mpol
)
923 #endif /* CONFIG_TMPFS */
925 static inline struct page
*shmem_swapin(swp_entry_t swap
, gfp_t gfp
,
926 struct shmem_inode_info
*info
, pgoff_t index
)
928 return swapin_readahead(swap
, gfp
, NULL
, 0);
931 static inline struct page
*shmem_alloc_page(gfp_t gfp
,
932 struct shmem_inode_info
*info
, pgoff_t index
)
934 return alloc_page(gfp
);
936 #endif /* CONFIG_NUMA */
938 #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
939 static inline struct mempolicy
*shmem_get_sbmpol(struct shmem_sb_info
*sbinfo
)
946 * When a page is moved from swapcache to shmem filecache (either by the
947 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
948 * shmem_unuse_inode()), it may have been read in earlier from swap, in
949 * ignorance of the mapping it belongs to. If that mapping has special
950 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
951 * we may need to copy to a suitable page before moving to filecache.
953 * In a future release, this may well be extended to respect cpuset and
954 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
955 * but for now it is a simple matter of zone.
957 static bool shmem_should_replace_page(struct page
*page
, gfp_t gfp
)
959 return page_zonenum(page
) > gfp_zone(gfp
);
962 static int shmem_replace_page(struct page
**pagep
, gfp_t gfp
,
963 struct shmem_inode_info
*info
, pgoff_t index
)
965 struct page
*oldpage
, *newpage
;
966 struct address_space
*swap_mapping
;
971 swap_index
= page_private(oldpage
);
972 swap_mapping
= page_mapping(oldpage
);
975 * We have arrived here because our zones are constrained, so don't
976 * limit chance of success by further cpuset and node constraints.
978 gfp
&= ~GFP_CONSTRAINT_MASK
;
979 newpage
= shmem_alloc_page(gfp
, info
, index
);
983 page_cache_get(newpage
);
984 copy_highpage(newpage
, oldpage
);
985 flush_dcache_page(newpage
);
987 __set_page_locked(newpage
);
988 SetPageUptodate(newpage
);
989 SetPageSwapBacked(newpage
);
990 set_page_private(newpage
, swap_index
);
991 SetPageSwapCache(newpage
);
994 * Our caller will very soon move newpage out of swapcache, but it's
995 * a nice clean interface for us to replace oldpage by newpage there.
997 spin_lock_irq(&swap_mapping
->tree_lock
);
998 error
= shmem_radix_tree_replace(swap_mapping
, swap_index
, oldpage
,
1001 __inc_zone_page_state(newpage
, NR_FILE_PAGES
);
1002 __dec_zone_page_state(oldpage
, NR_FILE_PAGES
);
1004 spin_unlock_irq(&swap_mapping
->tree_lock
);
1006 if (unlikely(error
)) {
1008 * Is this possible? I think not, now that our callers check
1009 * both PageSwapCache and page_private after getting page lock;
1010 * but be defensive. Reverse old to newpage for clear and free.
1014 mem_cgroup_migrate(oldpage
, newpage
, false);
1015 lru_cache_add_anon(newpage
);
1019 ClearPageSwapCache(oldpage
);
1020 set_page_private(oldpage
, 0);
1022 unlock_page(oldpage
);
1023 page_cache_release(oldpage
);
1024 page_cache_release(oldpage
);
1029 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
1031 * If we allocate a new one we do not mark it dirty. That's up to the
1032 * vm. If we swap it in we mark it dirty since we also free the swap
1033 * entry since a page cannot live in both the swap and page cache
1035 static int shmem_getpage_gfp(struct inode
*inode
, pgoff_t index
,
1036 struct page
**pagep
, enum sgp_type sgp
, gfp_t gfp
, int *fault_type
)
1038 struct address_space
*mapping
= inode
->i_mapping
;
1039 struct shmem_inode_info
*info
;
1040 struct shmem_sb_info
*sbinfo
;
1041 struct mem_cgroup
*memcg
;
1048 if (index
> (MAX_LFS_FILESIZE
>> PAGE_CACHE_SHIFT
))
1052 page
= find_lock_entry(mapping
, index
);
1053 if (radix_tree_exceptional_entry(page
)) {
1054 swap
= radix_to_swp_entry(page
);
1058 if (sgp
!= SGP_WRITE
&& sgp
!= SGP_FALLOC
&&
1059 ((loff_t
)index
<< PAGE_CACHE_SHIFT
) >= i_size_read(inode
)) {
1064 if (page
&& sgp
== SGP_WRITE
)
1065 mark_page_accessed(page
);
1067 /* fallocated page? */
1068 if (page
&& !PageUptodate(page
)) {
1069 if (sgp
!= SGP_READ
)
1072 page_cache_release(page
);
1075 if (page
|| (sgp
== SGP_READ
&& !swap
.val
)) {
1081 * Fast cache lookup did not find it:
1082 * bring it back from swap or allocate.
1084 info
= SHMEM_I(inode
);
1085 sbinfo
= SHMEM_SB(inode
->i_sb
);
1088 /* Look it up and read it in.. */
1089 page
= lookup_swap_cache(swap
);
1091 /* here we actually do the io */
1093 *fault_type
|= VM_FAULT_MAJOR
;
1094 page
= shmem_swapin(swap
, gfp
, info
, index
);
1101 /* We have to do this with page locked to prevent races */
1103 if (!PageSwapCache(page
) || page_private(page
) != swap
.val
||
1104 !shmem_confirm_swap(mapping
, index
, swap
)) {
1105 error
= -EEXIST
; /* try again */
1108 if (!PageUptodate(page
)) {
1112 wait_on_page_writeback(page
);
1114 if (shmem_should_replace_page(page
, gfp
)) {
1115 error
= shmem_replace_page(&page
, gfp
, info
, index
);
1120 error
= mem_cgroup_try_charge(page
, current
->mm
, gfp
, &memcg
);
1122 error
= shmem_add_to_page_cache(page
, mapping
, index
,
1123 swp_to_radix_entry(swap
));
1125 * We already confirmed swap under page lock, and make
1126 * no memory allocation here, so usually no possibility
1127 * of error; but free_swap_and_cache() only trylocks a
1128 * page, so it is just possible that the entry has been
1129 * truncated or holepunched since swap was confirmed.
1130 * shmem_undo_range() will have done some of the
1131 * unaccounting, now delete_from_swap_cache() will do
1132 * the rest (including mem_cgroup_uncharge_swapcache).
1133 * Reset swap.val? No, leave it so "failed" goes back to
1134 * "repeat": reading a hole and writing should succeed.
1137 mem_cgroup_cancel_charge(page
, memcg
);
1138 delete_from_swap_cache(page
);
1144 mem_cgroup_commit_charge(page
, memcg
, true);
1146 spin_lock(&info
->lock
);
1148 shmem_recalc_inode(inode
);
1149 spin_unlock(&info
->lock
);
1151 if (sgp
== SGP_WRITE
)
1152 mark_page_accessed(page
);
1154 delete_from_swap_cache(page
);
1155 set_page_dirty(page
);
1159 if (shmem_acct_block(info
->flags
)) {
1163 if (sbinfo
->max_blocks
) {
1164 if (percpu_counter_compare(&sbinfo
->used_blocks
,
1165 sbinfo
->max_blocks
) >= 0) {
1169 percpu_counter_inc(&sbinfo
->used_blocks
);
1172 page
= shmem_alloc_page(gfp
, info
, index
);
1178 __SetPageSwapBacked(page
);
1179 __set_page_locked(page
);
1180 if (sgp
== SGP_WRITE
)
1181 __SetPageReferenced(page
);
1183 error
= mem_cgroup_try_charge(page
, current
->mm
, gfp
, &memcg
);
1186 error
= radix_tree_maybe_preload(gfp
& GFP_RECLAIM_MASK
);
1188 error
= shmem_add_to_page_cache(page
, mapping
, index
,
1190 radix_tree_preload_end();
1193 mem_cgroup_cancel_charge(page
, memcg
);
1196 mem_cgroup_commit_charge(page
, memcg
, false);
1197 lru_cache_add_anon(page
);
1199 spin_lock(&info
->lock
);
1201 inode
->i_blocks
+= BLOCKS_PER_PAGE
;
1202 shmem_recalc_inode(inode
);
1203 spin_unlock(&info
->lock
);
1207 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1209 if (sgp
== SGP_FALLOC
)
1213 * Let SGP_WRITE caller clear ends if write does not fill page;
1214 * but SGP_FALLOC on a page fallocated earlier must initialize
1215 * it now, lest undo on failure cancel our earlier guarantee.
1217 if (sgp
!= SGP_WRITE
) {
1218 clear_highpage(page
);
1219 flush_dcache_page(page
);
1220 SetPageUptodate(page
);
1222 if (sgp
== SGP_DIRTY
)
1223 set_page_dirty(page
);
1226 /* Perhaps the file has been truncated since we checked */
1227 if (sgp
!= SGP_WRITE
&& sgp
!= SGP_FALLOC
&&
1228 ((loff_t
)index
<< PAGE_CACHE_SHIFT
) >= i_size_read(inode
)) {
1242 info
= SHMEM_I(inode
);
1243 ClearPageDirty(page
);
1244 delete_from_page_cache(page
);
1245 spin_lock(&info
->lock
);
1247 inode
->i_blocks
-= BLOCKS_PER_PAGE
;
1248 spin_unlock(&info
->lock
);
1250 sbinfo
= SHMEM_SB(inode
->i_sb
);
1251 if (sbinfo
->max_blocks
)
1252 percpu_counter_add(&sbinfo
->used_blocks
, -1);
1254 shmem_unacct_blocks(info
->flags
, 1);
1256 if (swap
.val
&& error
!= -EINVAL
&&
1257 !shmem_confirm_swap(mapping
, index
, swap
))
1262 page_cache_release(page
);
1264 if (error
== -ENOSPC
&& !once
++) {
1265 info
= SHMEM_I(inode
);
1266 spin_lock(&info
->lock
);
1267 shmem_recalc_inode(inode
);
1268 spin_unlock(&info
->lock
);
1271 if (error
== -EEXIST
) /* from above or from radix_tree_insert */
1276 static int shmem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1278 struct inode
*inode
= file_inode(vma
->vm_file
);
1280 int ret
= VM_FAULT_LOCKED
;
1283 * Trinity finds that probing a hole which tmpfs is punching can
1284 * prevent the hole-punch from ever completing: which in turn
1285 * locks writers out with its hold on i_mutex. So refrain from
1286 * faulting pages into the hole while it's being punched. Although
1287 * shmem_undo_range() does remove the additions, it may be unable to
1288 * keep up, as each new page needs its own unmap_mapping_range() call,
1289 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1291 * It does not matter if we sometimes reach this check just before the
1292 * hole-punch begins, so that one fault then races with the punch:
1293 * we just need to make racing faults a rare case.
1295 * The implementation below would be much simpler if we just used a
1296 * standard mutex or completion: but we cannot take i_mutex in fault,
1297 * and bloating every shmem inode for this unlikely case would be sad.
1299 if (unlikely(inode
->i_private
)) {
1300 struct shmem_falloc
*shmem_falloc
;
1302 spin_lock(&inode
->i_lock
);
1303 shmem_falloc
= inode
->i_private
;
1305 shmem_falloc
->waitq
&&
1306 vmf
->pgoff
>= shmem_falloc
->start
&&
1307 vmf
->pgoff
< shmem_falloc
->next
) {
1308 wait_queue_head_t
*shmem_falloc_waitq
;
1309 DEFINE_WAIT(shmem_fault_wait
);
1311 ret
= VM_FAULT_NOPAGE
;
1312 if ((vmf
->flags
& FAULT_FLAG_ALLOW_RETRY
) &&
1313 !(vmf
->flags
& FAULT_FLAG_RETRY_NOWAIT
)) {
1314 /* It's polite to up mmap_sem if we can */
1315 up_read(&vma
->vm_mm
->mmap_sem
);
1316 ret
= VM_FAULT_RETRY
;
1319 shmem_falloc_waitq
= shmem_falloc
->waitq
;
1320 prepare_to_wait(shmem_falloc_waitq
, &shmem_fault_wait
,
1321 TASK_UNINTERRUPTIBLE
);
1322 spin_unlock(&inode
->i_lock
);
1326 * shmem_falloc_waitq points into the shmem_fallocate()
1327 * stack of the hole-punching task: shmem_falloc_waitq
1328 * is usually invalid by the time we reach here, but
1329 * finish_wait() does not dereference it in that case;
1330 * though i_lock needed lest racing with wake_up_all().
1332 spin_lock(&inode
->i_lock
);
1333 finish_wait(shmem_falloc_waitq
, &shmem_fault_wait
);
1334 spin_unlock(&inode
->i_lock
);
1337 spin_unlock(&inode
->i_lock
);
1340 error
= shmem_getpage(inode
, vmf
->pgoff
, &vmf
->page
, SGP_CACHE
, &ret
);
1342 return ((error
== -ENOMEM
) ? VM_FAULT_OOM
: VM_FAULT_SIGBUS
);
1344 if (ret
& VM_FAULT_MAJOR
) {
1345 count_vm_event(PGMAJFAULT
);
1346 mem_cgroup_count_vm_event(vma
->vm_mm
, PGMAJFAULT
);
1352 static int shmem_set_policy(struct vm_area_struct
*vma
, struct mempolicy
*mpol
)
1354 struct inode
*inode
= file_inode(vma
->vm_file
);
1355 return mpol_set_shared_policy(&SHMEM_I(inode
)->policy
, vma
, mpol
);
1358 static struct mempolicy
*shmem_get_policy(struct vm_area_struct
*vma
,
1361 struct inode
*inode
= file_inode(vma
->vm_file
);
1364 index
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
1365 return mpol_shared_policy_lookup(&SHMEM_I(inode
)->policy
, index
);
1369 int shmem_lock(struct file
*file
, int lock
, struct user_struct
*user
)
1371 struct inode
*inode
= file_inode(file
);
1372 struct shmem_inode_info
*info
= SHMEM_I(inode
);
1373 int retval
= -ENOMEM
;
1375 spin_lock(&info
->lock
);
1376 if (lock
&& !(info
->flags
& VM_LOCKED
)) {
1377 if (!user_shm_lock(inode
->i_size
, user
))
1379 info
->flags
|= VM_LOCKED
;
1380 mapping_set_unevictable(file
->f_mapping
);
1382 if (!lock
&& (info
->flags
& VM_LOCKED
) && user
) {
1383 user_shm_unlock(inode
->i_size
, user
);
1384 info
->flags
&= ~VM_LOCKED
;
1385 mapping_clear_unevictable(file
->f_mapping
);
1390 spin_unlock(&info
->lock
);
1394 static int shmem_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1396 file_accessed(file
);
1397 vma
->vm_ops
= &shmem_vm_ops
;
1401 static struct inode
*shmem_get_inode(struct super_block
*sb
, const struct inode
*dir
,
1402 umode_t mode
, dev_t dev
, unsigned long flags
)
1404 struct inode
*inode
;
1405 struct shmem_inode_info
*info
;
1406 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
1408 if (shmem_reserve_inode(sb
))
1411 inode
= new_inode(sb
);
1413 inode
->i_ino
= get_next_ino();
1414 inode_init_owner(inode
, dir
, mode
);
1415 inode
->i_blocks
= 0;
1416 inode
->i_mapping
->backing_dev_info
= &shmem_backing_dev_info
;
1417 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1418 inode
->i_generation
= get_seconds();
1419 info
= SHMEM_I(inode
);
1420 memset(info
, 0, (char *)inode
- (char *)info
);
1421 spin_lock_init(&info
->lock
);
1422 info
->seals
= F_SEAL_SEAL
;
1423 info
->flags
= flags
& VM_NORESERVE
;
1424 INIT_LIST_HEAD(&info
->swaplist
);
1425 simple_xattrs_init(&info
->xattrs
);
1426 cache_no_acl(inode
);
1428 switch (mode
& S_IFMT
) {
1430 inode
->i_op
= &shmem_special_inode_operations
;
1431 init_special_inode(inode
, mode
, dev
);
1434 inode
->i_mapping
->a_ops
= &shmem_aops
;
1435 inode
->i_op
= &shmem_inode_operations
;
1436 inode
->i_fop
= &shmem_file_operations
;
1437 mpol_shared_policy_init(&info
->policy
,
1438 shmem_get_sbmpol(sbinfo
));
1442 /* Some things misbehave if size == 0 on a directory */
1443 inode
->i_size
= 2 * BOGO_DIRENT_SIZE
;
1444 inode
->i_op
= &shmem_dir_inode_operations
;
1445 inode
->i_fop
= &simple_dir_operations
;
1449 * Must not load anything in the rbtree,
1450 * mpol_free_shared_policy will not be called.
1452 mpol_shared_policy_init(&info
->policy
, NULL
);
1456 shmem_free_inode(sb
);
1460 bool shmem_mapping(struct address_space
*mapping
)
1462 return mapping
->backing_dev_info
== &shmem_backing_dev_info
;
1466 static const struct inode_operations shmem_symlink_inode_operations
;
1467 static const struct inode_operations shmem_short_symlink_operations
;
1469 #ifdef CONFIG_TMPFS_XATTR
1470 static int shmem_initxattrs(struct inode
*, const struct xattr
*, void *);
1472 #define shmem_initxattrs NULL
1476 shmem_write_begin(struct file
*file
, struct address_space
*mapping
,
1477 loff_t pos
, unsigned len
, unsigned flags
,
1478 struct page
**pagep
, void **fsdata
)
1480 struct inode
*inode
= mapping
->host
;
1481 struct shmem_inode_info
*info
= SHMEM_I(inode
);
1482 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
1484 /* i_mutex is held by caller */
1485 if (unlikely(info
->seals
)) {
1486 if (info
->seals
& F_SEAL_WRITE
)
1488 if ((info
->seals
& F_SEAL_GROW
) && pos
+ len
> inode
->i_size
)
1492 return shmem_getpage(inode
, index
, pagep
, SGP_WRITE
, NULL
);
1496 shmem_write_end(struct file
*file
, struct address_space
*mapping
,
1497 loff_t pos
, unsigned len
, unsigned copied
,
1498 struct page
*page
, void *fsdata
)
1500 struct inode
*inode
= mapping
->host
;
1502 if (pos
+ copied
> inode
->i_size
)
1503 i_size_write(inode
, pos
+ copied
);
1505 if (!PageUptodate(page
)) {
1506 if (copied
< PAGE_CACHE_SIZE
) {
1507 unsigned from
= pos
& (PAGE_CACHE_SIZE
- 1);
1508 zero_user_segments(page
, 0, from
,
1509 from
+ copied
, PAGE_CACHE_SIZE
);
1511 SetPageUptodate(page
);
1513 set_page_dirty(page
);
1515 page_cache_release(page
);
1520 static ssize_t
shmem_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1522 struct file
*file
= iocb
->ki_filp
;
1523 struct inode
*inode
= file_inode(file
);
1524 struct address_space
*mapping
= inode
->i_mapping
;
1526 unsigned long offset
;
1527 enum sgp_type sgp
= SGP_READ
;
1530 loff_t
*ppos
= &iocb
->ki_pos
;
1533 * Might this read be for a stacking filesystem? Then when reading
1534 * holes of a sparse file, we actually need to allocate those pages,
1535 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1537 if (segment_eq(get_fs(), KERNEL_DS
))
1540 index
= *ppos
>> PAGE_CACHE_SHIFT
;
1541 offset
= *ppos
& ~PAGE_CACHE_MASK
;
1544 struct page
*page
= NULL
;
1546 unsigned long nr
, ret
;
1547 loff_t i_size
= i_size_read(inode
);
1549 end_index
= i_size
>> PAGE_CACHE_SHIFT
;
1550 if (index
> end_index
)
1552 if (index
== end_index
) {
1553 nr
= i_size
& ~PAGE_CACHE_MASK
;
1558 error
= shmem_getpage(inode
, index
, &page
, sgp
, NULL
);
1560 if (error
== -EINVAL
)
1568 * We must evaluate after, since reads (unlike writes)
1569 * are called without i_mutex protection against truncate
1571 nr
= PAGE_CACHE_SIZE
;
1572 i_size
= i_size_read(inode
);
1573 end_index
= i_size
>> PAGE_CACHE_SHIFT
;
1574 if (index
== end_index
) {
1575 nr
= i_size
& ~PAGE_CACHE_MASK
;
1578 page_cache_release(page
);
1586 * If users can be writing to this page using arbitrary
1587 * virtual addresses, take care about potential aliasing
1588 * before reading the page on the kernel side.
1590 if (mapping_writably_mapped(mapping
))
1591 flush_dcache_page(page
);
1593 * Mark the page accessed if we read the beginning.
1596 mark_page_accessed(page
);
1598 page
= ZERO_PAGE(0);
1599 page_cache_get(page
);
1603 * Ok, we have the page, and it's up-to-date, so
1604 * now we can copy it to user space...
1606 ret
= copy_page_to_iter(page
, offset
, nr
, to
);
1609 index
+= offset
>> PAGE_CACHE_SHIFT
;
1610 offset
&= ~PAGE_CACHE_MASK
;
1612 page_cache_release(page
);
1613 if (!iov_iter_count(to
))
1622 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
1623 file_accessed(file
);
1624 return retval
? retval
: error
;
1627 static ssize_t
shmem_file_splice_read(struct file
*in
, loff_t
*ppos
,
1628 struct pipe_inode_info
*pipe
, size_t len
,
1631 struct address_space
*mapping
= in
->f_mapping
;
1632 struct inode
*inode
= mapping
->host
;
1633 unsigned int loff
, nr_pages
, req_pages
;
1634 struct page
*pages
[PIPE_DEF_BUFFERS
];
1635 struct partial_page partial
[PIPE_DEF_BUFFERS
];
1637 pgoff_t index
, end_index
;
1640 struct splice_pipe_desc spd
= {
1643 .nr_pages_max
= PIPE_DEF_BUFFERS
,
1645 .ops
= &page_cache_pipe_buf_ops
,
1646 .spd_release
= spd_release_page
,
1649 isize
= i_size_read(inode
);
1650 if (unlikely(*ppos
>= isize
))
1653 left
= isize
- *ppos
;
1654 if (unlikely(left
< len
))
1657 if (splice_grow_spd(pipe
, &spd
))
1660 index
= *ppos
>> PAGE_CACHE_SHIFT
;
1661 loff
= *ppos
& ~PAGE_CACHE_MASK
;
1662 req_pages
= (len
+ loff
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1663 nr_pages
= min(req_pages
, spd
.nr_pages_max
);
1665 spd
.nr_pages
= find_get_pages_contig(mapping
, index
,
1666 nr_pages
, spd
.pages
);
1667 index
+= spd
.nr_pages
;
1670 while (spd
.nr_pages
< nr_pages
) {
1671 error
= shmem_getpage(inode
, index
, &page
, SGP_CACHE
, NULL
);
1675 spd
.pages
[spd
.nr_pages
++] = page
;
1679 index
= *ppos
>> PAGE_CACHE_SHIFT
;
1680 nr_pages
= spd
.nr_pages
;
1683 for (page_nr
= 0; page_nr
< nr_pages
; page_nr
++) {
1684 unsigned int this_len
;
1689 this_len
= min_t(unsigned long, len
, PAGE_CACHE_SIZE
- loff
);
1690 page
= spd
.pages
[page_nr
];
1692 if (!PageUptodate(page
) || page
->mapping
!= mapping
) {
1693 error
= shmem_getpage(inode
, index
, &page
,
1698 page_cache_release(spd
.pages
[page_nr
]);
1699 spd
.pages
[page_nr
] = page
;
1702 isize
= i_size_read(inode
);
1703 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1704 if (unlikely(!isize
|| index
> end_index
))
1707 if (end_index
== index
) {
1710 plen
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
1714 this_len
= min(this_len
, plen
- loff
);
1718 spd
.partial
[page_nr
].offset
= loff
;
1719 spd
.partial
[page_nr
].len
= this_len
;
1726 while (page_nr
< nr_pages
)
1727 page_cache_release(spd
.pages
[page_nr
++]);
1730 error
= splice_to_pipe(pipe
, &spd
);
1732 splice_shrink_spd(&spd
);
1742 * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
1744 static pgoff_t
shmem_seek_hole_data(struct address_space
*mapping
,
1745 pgoff_t index
, pgoff_t end
, int whence
)
1748 struct pagevec pvec
;
1749 pgoff_t indices
[PAGEVEC_SIZE
];
1753 pagevec_init(&pvec
, 0);
1754 pvec
.nr
= 1; /* start small: we may be there already */
1756 pvec
.nr
= find_get_entries(mapping
, index
,
1757 pvec
.nr
, pvec
.pages
, indices
);
1759 if (whence
== SEEK_DATA
)
1763 for (i
= 0; i
< pvec
.nr
; i
++, index
++) {
1764 if (index
< indices
[i
]) {
1765 if (whence
== SEEK_HOLE
) {
1771 page
= pvec
.pages
[i
];
1772 if (page
&& !radix_tree_exceptional_entry(page
)) {
1773 if (!PageUptodate(page
))
1777 (page
&& whence
== SEEK_DATA
) ||
1778 (!page
&& whence
== SEEK_HOLE
)) {
1783 pagevec_remove_exceptionals(&pvec
);
1784 pagevec_release(&pvec
);
1785 pvec
.nr
= PAGEVEC_SIZE
;
1791 static loff_t
shmem_file_llseek(struct file
*file
, loff_t offset
, int whence
)
1793 struct address_space
*mapping
= file
->f_mapping
;
1794 struct inode
*inode
= mapping
->host
;
1798 if (whence
!= SEEK_DATA
&& whence
!= SEEK_HOLE
)
1799 return generic_file_llseek_size(file
, offset
, whence
,
1800 MAX_LFS_FILESIZE
, i_size_read(inode
));
1801 mutex_lock(&inode
->i_mutex
);
1802 /* We're holding i_mutex so we can access i_size directly */
1806 else if (offset
>= inode
->i_size
)
1809 start
= offset
>> PAGE_CACHE_SHIFT
;
1810 end
= (inode
->i_size
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1811 new_offset
= shmem_seek_hole_data(mapping
, start
, end
, whence
);
1812 new_offset
<<= PAGE_CACHE_SHIFT
;
1813 if (new_offset
> offset
) {
1814 if (new_offset
< inode
->i_size
)
1815 offset
= new_offset
;
1816 else if (whence
== SEEK_DATA
)
1819 offset
= inode
->i_size
;
1824 offset
= vfs_setpos(file
, offset
, MAX_LFS_FILESIZE
);
1825 mutex_unlock(&inode
->i_mutex
);
1829 static int shmem_wait_for_pins(struct address_space
*mapping
)
1834 #define F_ALL_SEALS (F_SEAL_SEAL | \
1839 int shmem_add_seals(struct file
*file
, unsigned int seals
)
1841 struct inode
*inode
= file_inode(file
);
1842 struct shmem_inode_info
*info
= SHMEM_I(inode
);
1847 * Sealing allows multiple parties to share a shmem-file but restrict
1848 * access to a specific subset of file operations. Seals can only be
1849 * added, but never removed. This way, mutually untrusted parties can
1850 * share common memory regions with a well-defined policy. A malicious
1851 * peer can thus never perform unwanted operations on a shared object.
1853 * Seals are only supported on special shmem-files and always affect
1854 * the whole underlying inode. Once a seal is set, it may prevent some
1855 * kinds of access to the file. Currently, the following seals are
1857 * SEAL_SEAL: Prevent further seals from being set on this file
1858 * SEAL_SHRINK: Prevent the file from shrinking
1859 * SEAL_GROW: Prevent the file from growing
1860 * SEAL_WRITE: Prevent write access to the file
1862 * As we don't require any trust relationship between two parties, we
1863 * must prevent seals from being removed. Therefore, sealing a file
1864 * only adds a given set of seals to the file, it never touches
1865 * existing seals. Furthermore, the "setting seals"-operation can be
1866 * sealed itself, which basically prevents any further seal from being
1869 * Semantics of sealing are only defined on volatile files. Only
1870 * anonymous shmem files support sealing. More importantly, seals are
1871 * never written to disk. Therefore, there's no plan to support it on
1875 if (file
->f_op
!= &shmem_file_operations
)
1877 if (!(file
->f_mode
& FMODE_WRITE
))
1879 if (seals
& ~(unsigned int)F_ALL_SEALS
)
1882 mutex_lock(&inode
->i_mutex
);
1884 if (info
->seals
& F_SEAL_SEAL
) {
1889 if ((seals
& F_SEAL_WRITE
) && !(info
->seals
& F_SEAL_WRITE
)) {
1890 error
= mapping_deny_writable(file
->f_mapping
);
1894 error
= shmem_wait_for_pins(file
->f_mapping
);
1896 mapping_allow_writable(file
->f_mapping
);
1901 info
->seals
|= seals
;
1905 mutex_unlock(&inode
->i_mutex
);
1908 EXPORT_SYMBOL_GPL(shmem_add_seals
);
1910 int shmem_get_seals(struct file
*file
)
1912 if (file
->f_op
!= &shmem_file_operations
)
1915 return SHMEM_I(file_inode(file
))->seals
;
1917 EXPORT_SYMBOL_GPL(shmem_get_seals
);
1919 long shmem_fcntl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1925 /* disallow upper 32bit */
1929 error
= shmem_add_seals(file
, arg
);
1932 error
= shmem_get_seals(file
);
1942 static long shmem_fallocate(struct file
*file
, int mode
, loff_t offset
,
1945 struct inode
*inode
= file_inode(file
);
1946 struct shmem_sb_info
*sbinfo
= SHMEM_SB(inode
->i_sb
);
1947 struct shmem_inode_info
*info
= SHMEM_I(inode
);
1948 struct shmem_falloc shmem_falloc
;
1949 pgoff_t start
, index
, end
;
1952 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
1955 mutex_lock(&inode
->i_mutex
);
1957 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
1958 struct address_space
*mapping
= file
->f_mapping
;
1959 loff_t unmap_start
= round_up(offset
, PAGE_SIZE
);
1960 loff_t unmap_end
= round_down(offset
+ len
, PAGE_SIZE
) - 1;
1961 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq
);
1963 /* protected by i_mutex */
1964 if (info
->seals
& F_SEAL_WRITE
) {
1969 shmem_falloc
.waitq
= &shmem_falloc_waitq
;
1970 shmem_falloc
.start
= unmap_start
>> PAGE_SHIFT
;
1971 shmem_falloc
.next
= (unmap_end
+ 1) >> PAGE_SHIFT
;
1972 spin_lock(&inode
->i_lock
);
1973 inode
->i_private
= &shmem_falloc
;
1974 spin_unlock(&inode
->i_lock
);
1976 if ((u64
)unmap_end
> (u64
)unmap_start
)
1977 unmap_mapping_range(mapping
, unmap_start
,
1978 1 + unmap_end
- unmap_start
, 0);
1979 shmem_truncate_range(inode
, offset
, offset
+ len
- 1);
1980 /* No need to unmap again: hole-punching leaves COWed pages */
1982 spin_lock(&inode
->i_lock
);
1983 inode
->i_private
= NULL
;
1984 wake_up_all(&shmem_falloc_waitq
);
1985 spin_unlock(&inode
->i_lock
);
1990 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
1991 error
= inode_newsize_ok(inode
, offset
+ len
);
1995 if ((info
->seals
& F_SEAL_GROW
) && offset
+ len
> inode
->i_size
) {
2000 start
= offset
>> PAGE_CACHE_SHIFT
;
2001 end
= (offset
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
2002 /* Try to avoid a swapstorm if len is impossible to satisfy */
2003 if (sbinfo
->max_blocks
&& end
- start
> sbinfo
->max_blocks
) {
2008 shmem_falloc
.waitq
= NULL
;
2009 shmem_falloc
.start
= start
;
2010 shmem_falloc
.next
= start
;
2011 shmem_falloc
.nr_falloced
= 0;
2012 shmem_falloc
.nr_unswapped
= 0;
2013 spin_lock(&inode
->i_lock
);
2014 inode
->i_private
= &shmem_falloc
;
2015 spin_unlock(&inode
->i_lock
);
2017 for (index
= start
; index
< end
; index
++) {
2021 * Good, the fallocate(2) manpage permits EINTR: we may have
2022 * been interrupted because we are using up too much memory.
2024 if (signal_pending(current
))
2026 else if (shmem_falloc
.nr_unswapped
> shmem_falloc
.nr_falloced
)
2029 error
= shmem_getpage(inode
, index
, &page
, SGP_FALLOC
,
2032 /* Remove the !PageUptodate pages we added */
2033 shmem_undo_range(inode
,
2034 (loff_t
)start
<< PAGE_CACHE_SHIFT
,
2035 (loff_t
)index
<< PAGE_CACHE_SHIFT
, true);
2040 * Inform shmem_writepage() how far we have reached.
2041 * No need for lock or barrier: we have the page lock.
2043 shmem_falloc
.next
++;
2044 if (!PageUptodate(page
))
2045 shmem_falloc
.nr_falloced
++;
2048 * If !PageUptodate, leave it that way so that freeable pages
2049 * can be recognized if we need to rollback on error later.
2050 * But set_page_dirty so that memory pressure will swap rather
2051 * than free the pages we are allocating (and SGP_CACHE pages
2052 * might still be clean: we now need to mark those dirty too).
2054 set_page_dirty(page
);
2056 page_cache_release(page
);
2060 if (!(mode
& FALLOC_FL_KEEP_SIZE
) && offset
+ len
> inode
->i_size
)
2061 i_size_write(inode
, offset
+ len
);
2062 inode
->i_ctime
= CURRENT_TIME
;
2064 spin_lock(&inode
->i_lock
);
2065 inode
->i_private
= NULL
;
2066 spin_unlock(&inode
->i_lock
);
2068 mutex_unlock(&inode
->i_mutex
);
2072 static int shmem_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
2074 struct shmem_sb_info
*sbinfo
= SHMEM_SB(dentry
->d_sb
);
2076 buf
->f_type
= TMPFS_MAGIC
;
2077 buf
->f_bsize
= PAGE_CACHE_SIZE
;
2078 buf
->f_namelen
= NAME_MAX
;
2079 if (sbinfo
->max_blocks
) {
2080 buf
->f_blocks
= sbinfo
->max_blocks
;
2082 buf
->f_bfree
= sbinfo
->max_blocks
-
2083 percpu_counter_sum(&sbinfo
->used_blocks
);
2085 if (sbinfo
->max_inodes
) {
2086 buf
->f_files
= sbinfo
->max_inodes
;
2087 buf
->f_ffree
= sbinfo
->free_inodes
;
2089 /* else leave those fields 0 like simple_statfs */
2094 * File creation. Allocate an inode, and we're done..
2097 shmem_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
2099 struct inode
*inode
;
2100 int error
= -ENOSPC
;
2102 inode
= shmem_get_inode(dir
->i_sb
, dir
, mode
, dev
, VM_NORESERVE
);
2104 error
= simple_acl_create(dir
, inode
);
2107 error
= security_inode_init_security(inode
, dir
,
2109 shmem_initxattrs
, NULL
);
2110 if (error
&& error
!= -EOPNOTSUPP
)
2114 dir
->i_size
+= BOGO_DIRENT_SIZE
;
2115 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
2116 d_instantiate(dentry
, inode
);
2117 dget(dentry
); /* Extra count - pin the dentry in core */
2126 shmem_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2128 struct inode
*inode
;
2129 int error
= -ENOSPC
;
2131 inode
= shmem_get_inode(dir
->i_sb
, dir
, mode
, 0, VM_NORESERVE
);
2133 error
= security_inode_init_security(inode
, dir
,
2135 shmem_initxattrs
, NULL
);
2136 if (error
&& error
!= -EOPNOTSUPP
)
2138 error
= simple_acl_create(dir
, inode
);
2141 d_tmpfile(dentry
, inode
);
2149 static int shmem_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2153 if ((error
= shmem_mknod(dir
, dentry
, mode
| S_IFDIR
, 0)))
2159 static int shmem_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2162 return shmem_mknod(dir
, dentry
, mode
| S_IFREG
, 0);
2168 static int shmem_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
2170 struct inode
*inode
= old_dentry
->d_inode
;
2174 * No ordinary (disk based) filesystem counts links as inodes;
2175 * but each new link needs a new dentry, pinning lowmem, and
2176 * tmpfs dentries cannot be pruned until they are unlinked.
2178 ret
= shmem_reserve_inode(inode
->i_sb
);
2182 dir
->i_size
+= BOGO_DIRENT_SIZE
;
2183 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
2185 ihold(inode
); /* New dentry reference */
2186 dget(dentry
); /* Extra pinning count for the created dentry */
2187 d_instantiate(dentry
, inode
);
2192 static int shmem_unlink(struct inode
*dir
, struct dentry
*dentry
)
2194 struct inode
*inode
= dentry
->d_inode
;
2196 if (inode
->i_nlink
> 1 && !S_ISDIR(inode
->i_mode
))
2197 shmem_free_inode(inode
->i_sb
);
2199 dir
->i_size
-= BOGO_DIRENT_SIZE
;
2200 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
2202 dput(dentry
); /* Undo the count from "create" - this does all the work */
2206 static int shmem_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2208 if (!simple_empty(dentry
))
2211 drop_nlink(dentry
->d_inode
);
2213 return shmem_unlink(dir
, dentry
);
2217 * The VFS layer already does all the dentry stuff for rename,
2218 * we just have to decrement the usage count for the target if
2219 * it exists so that the VFS layer correctly free's it when it
2222 static int shmem_rename(struct inode
*old_dir
, struct dentry
*old_dentry
, struct inode
*new_dir
, struct dentry
*new_dentry
)
2224 struct inode
*inode
= old_dentry
->d_inode
;
2225 int they_are_dirs
= S_ISDIR(inode
->i_mode
);
2227 if (!simple_empty(new_dentry
))
2230 if (new_dentry
->d_inode
) {
2231 (void) shmem_unlink(new_dir
, new_dentry
);
2233 drop_nlink(old_dir
);
2234 } else if (they_are_dirs
) {
2235 drop_nlink(old_dir
);
2239 old_dir
->i_size
-= BOGO_DIRENT_SIZE
;
2240 new_dir
->i_size
+= BOGO_DIRENT_SIZE
;
2241 old_dir
->i_ctime
= old_dir
->i_mtime
=
2242 new_dir
->i_ctime
= new_dir
->i_mtime
=
2243 inode
->i_ctime
= CURRENT_TIME
;
2247 static int shmem_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
2251 struct inode
*inode
;
2254 struct shmem_inode_info
*info
;
2256 len
= strlen(symname
) + 1;
2257 if (len
> PAGE_CACHE_SIZE
)
2258 return -ENAMETOOLONG
;
2260 inode
= shmem_get_inode(dir
->i_sb
, dir
, S_IFLNK
|S_IRWXUGO
, 0, VM_NORESERVE
);
2264 error
= security_inode_init_security(inode
, dir
, &dentry
->d_name
,
2265 shmem_initxattrs
, NULL
);
2267 if (error
!= -EOPNOTSUPP
) {
2274 info
= SHMEM_I(inode
);
2275 inode
->i_size
= len
-1;
2276 if (len
<= SHORT_SYMLINK_LEN
) {
2277 info
->symlink
= kmemdup(symname
, len
, GFP_KERNEL
);
2278 if (!info
->symlink
) {
2282 inode
->i_op
= &shmem_short_symlink_operations
;
2284 error
= shmem_getpage(inode
, 0, &page
, SGP_WRITE
, NULL
);
2289 inode
->i_mapping
->a_ops
= &shmem_aops
;
2290 inode
->i_op
= &shmem_symlink_inode_operations
;
2291 kaddr
= kmap_atomic(page
);
2292 memcpy(kaddr
, symname
, len
);
2293 kunmap_atomic(kaddr
);
2294 SetPageUptodate(page
);
2295 set_page_dirty(page
);
2297 page_cache_release(page
);
2299 dir
->i_size
+= BOGO_DIRENT_SIZE
;
2300 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
2301 d_instantiate(dentry
, inode
);
2306 static void *shmem_follow_short_symlink(struct dentry
*dentry
, struct nameidata
*nd
)
2308 nd_set_link(nd
, SHMEM_I(dentry
->d_inode
)->symlink
);
2312 static void *shmem_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2314 struct page
*page
= NULL
;
2315 int error
= shmem_getpage(dentry
->d_inode
, 0, &page
, SGP_READ
, NULL
);
2316 nd_set_link(nd
, error
? ERR_PTR(error
) : kmap(page
));
2322 static void shmem_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
2324 if (!IS_ERR(nd_get_link(nd
))) {
2325 struct page
*page
= cookie
;
2327 mark_page_accessed(page
);
2328 page_cache_release(page
);
2332 #ifdef CONFIG_TMPFS_XATTR
2334 * Superblocks without xattr inode operations may get some security.* xattr
2335 * support from the LSM "for free". As soon as we have any other xattrs
2336 * like ACLs, we also need to implement the security.* handlers at
2337 * filesystem level, though.
2341 * Callback for security_inode_init_security() for acquiring xattrs.
2343 static int shmem_initxattrs(struct inode
*inode
,
2344 const struct xattr
*xattr_array
,
2347 struct shmem_inode_info
*info
= SHMEM_I(inode
);
2348 const struct xattr
*xattr
;
2349 struct simple_xattr
*new_xattr
;
2352 for (xattr
= xattr_array
; xattr
->name
!= NULL
; xattr
++) {
2353 new_xattr
= simple_xattr_alloc(xattr
->value
, xattr
->value_len
);
2357 len
= strlen(xattr
->name
) + 1;
2358 new_xattr
->name
= kmalloc(XATTR_SECURITY_PREFIX_LEN
+ len
,
2360 if (!new_xattr
->name
) {
2365 memcpy(new_xattr
->name
, XATTR_SECURITY_PREFIX
,
2366 XATTR_SECURITY_PREFIX_LEN
);
2367 memcpy(new_xattr
->name
+ XATTR_SECURITY_PREFIX_LEN
,
2370 simple_xattr_list_add(&info
->xattrs
, new_xattr
);
2376 static const struct xattr_handler
*shmem_xattr_handlers
[] = {
2377 #ifdef CONFIG_TMPFS_POSIX_ACL
2378 &posix_acl_access_xattr_handler
,
2379 &posix_acl_default_xattr_handler
,
2384 static int shmem_xattr_validate(const char *name
)
2386 struct { const char *prefix
; size_t len
; } arr
[] = {
2387 { XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
},
2388 { XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
}
2392 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
2393 size_t preflen
= arr
[i
].len
;
2394 if (strncmp(name
, arr
[i
].prefix
, preflen
) == 0) {
2403 static ssize_t
shmem_getxattr(struct dentry
*dentry
, const char *name
,
2404 void *buffer
, size_t size
)
2406 struct shmem_inode_info
*info
= SHMEM_I(dentry
->d_inode
);
2410 * If this is a request for a synthetic attribute in the system.*
2411 * namespace use the generic infrastructure to resolve a handler
2412 * for it via sb->s_xattr.
2414 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
2415 return generic_getxattr(dentry
, name
, buffer
, size
);
2417 err
= shmem_xattr_validate(name
);
2421 return simple_xattr_get(&info
->xattrs
, name
, buffer
, size
);
2424 static int shmem_setxattr(struct dentry
*dentry
, const char *name
,
2425 const void *value
, size_t size
, int flags
)
2427 struct shmem_inode_info
*info
= SHMEM_I(dentry
->d_inode
);
2431 * If this is a request for a synthetic attribute in the system.*
2432 * namespace use the generic infrastructure to resolve a handler
2433 * for it via sb->s_xattr.
2435 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
2436 return generic_setxattr(dentry
, name
, value
, size
, flags
);
2438 err
= shmem_xattr_validate(name
);
2442 return simple_xattr_set(&info
->xattrs
, name
, value
, size
, flags
);
2445 static int shmem_removexattr(struct dentry
*dentry
, const char *name
)
2447 struct shmem_inode_info
*info
= SHMEM_I(dentry
->d_inode
);
2451 * If this is a request for a synthetic attribute in the system.*
2452 * namespace use the generic infrastructure to resolve a handler
2453 * for it via sb->s_xattr.
2455 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
2456 return generic_removexattr(dentry
, name
);
2458 err
= shmem_xattr_validate(name
);
2462 return simple_xattr_remove(&info
->xattrs
, name
);
2465 static ssize_t
shmem_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
2467 struct shmem_inode_info
*info
= SHMEM_I(dentry
->d_inode
);
2468 return simple_xattr_list(&info
->xattrs
, buffer
, size
);
2470 #endif /* CONFIG_TMPFS_XATTR */
2472 static const struct inode_operations shmem_short_symlink_operations
= {
2473 .readlink
= generic_readlink
,
2474 .follow_link
= shmem_follow_short_symlink
,
2475 #ifdef CONFIG_TMPFS_XATTR
2476 .setxattr
= shmem_setxattr
,
2477 .getxattr
= shmem_getxattr
,
2478 .listxattr
= shmem_listxattr
,
2479 .removexattr
= shmem_removexattr
,
2483 static const struct inode_operations shmem_symlink_inode_operations
= {
2484 .readlink
= generic_readlink
,
2485 .follow_link
= shmem_follow_link
,
2486 .put_link
= shmem_put_link
,
2487 #ifdef CONFIG_TMPFS_XATTR
2488 .setxattr
= shmem_setxattr
,
2489 .getxattr
= shmem_getxattr
,
2490 .listxattr
= shmem_listxattr
,
2491 .removexattr
= shmem_removexattr
,
2495 static struct dentry
*shmem_get_parent(struct dentry
*child
)
2497 return ERR_PTR(-ESTALE
);
2500 static int shmem_match(struct inode
*ino
, void *vfh
)
2504 inum
= (inum
<< 32) | fh
[1];
2505 return ino
->i_ino
== inum
&& fh
[0] == ino
->i_generation
;
2508 static struct dentry
*shmem_fh_to_dentry(struct super_block
*sb
,
2509 struct fid
*fid
, int fh_len
, int fh_type
)
2511 struct inode
*inode
;
2512 struct dentry
*dentry
= NULL
;
2519 inum
= (inum
<< 32) | fid
->raw
[1];
2521 inode
= ilookup5(sb
, (unsigned long)(inum
+ fid
->raw
[0]),
2522 shmem_match
, fid
->raw
);
2524 dentry
= d_find_alias(inode
);
2531 static int shmem_encode_fh(struct inode
*inode
, __u32
*fh
, int *len
,
2532 struct inode
*parent
)
2536 return FILEID_INVALID
;
2539 if (inode_unhashed(inode
)) {
2540 /* Unfortunately insert_inode_hash is not idempotent,
2541 * so as we hash inodes here rather than at creation
2542 * time, we need a lock to ensure we only try
2545 static DEFINE_SPINLOCK(lock
);
2547 if (inode_unhashed(inode
))
2548 __insert_inode_hash(inode
,
2549 inode
->i_ino
+ inode
->i_generation
);
2553 fh
[0] = inode
->i_generation
;
2554 fh
[1] = inode
->i_ino
;
2555 fh
[2] = ((__u64
)inode
->i_ino
) >> 32;
2561 static const struct export_operations shmem_export_ops
= {
2562 .get_parent
= shmem_get_parent
,
2563 .encode_fh
= shmem_encode_fh
,
2564 .fh_to_dentry
= shmem_fh_to_dentry
,
2567 static int shmem_parse_options(char *options
, struct shmem_sb_info
*sbinfo
,
2570 char *this_char
, *value
, *rest
;
2571 struct mempolicy
*mpol
= NULL
;
2575 while (options
!= NULL
) {
2576 this_char
= options
;
2579 * NUL-terminate this option: unfortunately,
2580 * mount options form a comma-separated list,
2581 * but mpol's nodelist may also contain commas.
2583 options
= strchr(options
, ',');
2584 if (options
== NULL
)
2587 if (!isdigit(*options
)) {
2594 if ((value
= strchr(this_char
,'=')) != NULL
) {
2598 "tmpfs: No value for mount option '%s'\n",
2603 if (!strcmp(this_char
,"size")) {
2604 unsigned long long size
;
2605 size
= memparse(value
,&rest
);
2607 size
<<= PAGE_SHIFT
;
2608 size
*= totalram_pages
;
2614 sbinfo
->max_blocks
=
2615 DIV_ROUND_UP(size
, PAGE_CACHE_SIZE
);
2616 } else if (!strcmp(this_char
,"nr_blocks")) {
2617 sbinfo
->max_blocks
= memparse(value
, &rest
);
2620 } else if (!strcmp(this_char
,"nr_inodes")) {
2621 sbinfo
->max_inodes
= memparse(value
, &rest
);
2624 } else if (!strcmp(this_char
,"mode")) {
2627 sbinfo
->mode
= simple_strtoul(value
, &rest
, 8) & 07777;
2630 } else if (!strcmp(this_char
,"uid")) {
2633 uid
= simple_strtoul(value
, &rest
, 0);
2636 sbinfo
->uid
= make_kuid(current_user_ns(), uid
);
2637 if (!uid_valid(sbinfo
->uid
))
2639 } else if (!strcmp(this_char
,"gid")) {
2642 gid
= simple_strtoul(value
, &rest
, 0);
2645 sbinfo
->gid
= make_kgid(current_user_ns(), gid
);
2646 if (!gid_valid(sbinfo
->gid
))
2648 } else if (!strcmp(this_char
,"mpol")) {
2651 if (mpol_parse_str(value
, &mpol
))
2654 printk(KERN_ERR
"tmpfs: Bad mount option %s\n",
2659 sbinfo
->mpol
= mpol
;
2663 printk(KERN_ERR
"tmpfs: Bad value '%s' for mount option '%s'\n",
2671 static int shmem_remount_fs(struct super_block
*sb
, int *flags
, char *data
)
2673 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
2674 struct shmem_sb_info config
= *sbinfo
;
2675 unsigned long inodes
;
2676 int error
= -EINVAL
;
2679 if (shmem_parse_options(data
, &config
, true))
2682 spin_lock(&sbinfo
->stat_lock
);
2683 inodes
= sbinfo
->max_inodes
- sbinfo
->free_inodes
;
2684 if (percpu_counter_compare(&sbinfo
->used_blocks
, config
.max_blocks
) > 0)
2686 if (config
.max_inodes
< inodes
)
2689 * Those tests disallow limited->unlimited while any are in use;
2690 * but we must separately disallow unlimited->limited, because
2691 * in that case we have no record of how much is already in use.
2693 if (config
.max_blocks
&& !sbinfo
->max_blocks
)
2695 if (config
.max_inodes
&& !sbinfo
->max_inodes
)
2699 sbinfo
->max_blocks
= config
.max_blocks
;
2700 sbinfo
->max_inodes
= config
.max_inodes
;
2701 sbinfo
->free_inodes
= config
.max_inodes
- inodes
;
2704 * Preserve previous mempolicy unless mpol remount option was specified.
2707 mpol_put(sbinfo
->mpol
);
2708 sbinfo
->mpol
= config
.mpol
; /* transfers initial ref */
2711 spin_unlock(&sbinfo
->stat_lock
);
2715 static int shmem_show_options(struct seq_file
*seq
, struct dentry
*root
)
2717 struct shmem_sb_info
*sbinfo
= SHMEM_SB(root
->d_sb
);
2719 if (sbinfo
->max_blocks
!= shmem_default_max_blocks())
2720 seq_printf(seq
, ",size=%luk",
2721 sbinfo
->max_blocks
<< (PAGE_CACHE_SHIFT
- 10));
2722 if (sbinfo
->max_inodes
!= shmem_default_max_inodes())
2723 seq_printf(seq
, ",nr_inodes=%lu", sbinfo
->max_inodes
);
2724 if (sbinfo
->mode
!= (S_IRWXUGO
| S_ISVTX
))
2725 seq_printf(seq
, ",mode=%03ho", sbinfo
->mode
);
2726 if (!uid_eq(sbinfo
->uid
, GLOBAL_ROOT_UID
))
2727 seq_printf(seq
, ",uid=%u",
2728 from_kuid_munged(&init_user_ns
, sbinfo
->uid
));
2729 if (!gid_eq(sbinfo
->gid
, GLOBAL_ROOT_GID
))
2730 seq_printf(seq
, ",gid=%u",
2731 from_kgid_munged(&init_user_ns
, sbinfo
->gid
));
2732 shmem_show_mpol(seq
, sbinfo
->mpol
);
2735 #endif /* CONFIG_TMPFS */
2737 static void shmem_put_super(struct super_block
*sb
)
2739 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
2741 percpu_counter_destroy(&sbinfo
->used_blocks
);
2742 mpol_put(sbinfo
->mpol
);
2744 sb
->s_fs_info
= NULL
;
2747 int shmem_fill_super(struct super_block
*sb
, void *data
, int silent
)
2749 struct inode
*inode
;
2750 struct shmem_sb_info
*sbinfo
;
2753 /* Round up to L1_CACHE_BYTES to resist false sharing */
2754 sbinfo
= kzalloc(max((int)sizeof(struct shmem_sb_info
),
2755 L1_CACHE_BYTES
), GFP_KERNEL
);
2759 sbinfo
->mode
= S_IRWXUGO
| S_ISVTX
;
2760 sbinfo
->uid
= current_fsuid();
2761 sbinfo
->gid
= current_fsgid();
2762 sb
->s_fs_info
= sbinfo
;
2766 * Per default we only allow half of the physical ram per
2767 * tmpfs instance, limiting inodes to one per page of lowmem;
2768 * but the internal instance is left unlimited.
2770 if (!(sb
->s_flags
& MS_KERNMOUNT
)) {
2771 sbinfo
->max_blocks
= shmem_default_max_blocks();
2772 sbinfo
->max_inodes
= shmem_default_max_inodes();
2773 if (shmem_parse_options(data
, sbinfo
, false)) {
2778 sb
->s_flags
|= MS_NOUSER
;
2780 sb
->s_export_op
= &shmem_export_ops
;
2781 sb
->s_flags
|= MS_NOSEC
;
2783 sb
->s_flags
|= MS_NOUSER
;
2786 spin_lock_init(&sbinfo
->stat_lock
);
2787 if (percpu_counter_init(&sbinfo
->used_blocks
, 0))
2789 sbinfo
->free_inodes
= sbinfo
->max_inodes
;
2791 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
2792 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
2793 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
2794 sb
->s_magic
= TMPFS_MAGIC
;
2795 sb
->s_op
= &shmem_ops
;
2796 sb
->s_time_gran
= 1;
2797 #ifdef CONFIG_TMPFS_XATTR
2798 sb
->s_xattr
= shmem_xattr_handlers
;
2800 #ifdef CONFIG_TMPFS_POSIX_ACL
2801 sb
->s_flags
|= MS_POSIXACL
;
2804 inode
= shmem_get_inode(sb
, NULL
, S_IFDIR
| sbinfo
->mode
, 0, VM_NORESERVE
);
2807 inode
->i_uid
= sbinfo
->uid
;
2808 inode
->i_gid
= sbinfo
->gid
;
2809 sb
->s_root
= d_make_root(inode
);
2815 shmem_put_super(sb
);
2819 static struct kmem_cache
*shmem_inode_cachep
;
2821 static struct inode
*shmem_alloc_inode(struct super_block
*sb
)
2823 struct shmem_inode_info
*info
;
2824 info
= kmem_cache_alloc(shmem_inode_cachep
, GFP_KERNEL
);
2827 return &info
->vfs_inode
;
2830 static void shmem_destroy_callback(struct rcu_head
*head
)
2832 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
2833 kmem_cache_free(shmem_inode_cachep
, SHMEM_I(inode
));
2836 static void shmem_destroy_inode(struct inode
*inode
)
2838 if (S_ISREG(inode
->i_mode
))
2839 mpol_free_shared_policy(&SHMEM_I(inode
)->policy
);
2840 call_rcu(&inode
->i_rcu
, shmem_destroy_callback
);
2843 static void shmem_init_inode(void *foo
)
2845 struct shmem_inode_info
*info
= foo
;
2846 inode_init_once(&info
->vfs_inode
);
2849 static int shmem_init_inodecache(void)
2851 shmem_inode_cachep
= kmem_cache_create("shmem_inode_cache",
2852 sizeof(struct shmem_inode_info
),
2853 0, SLAB_PANIC
, shmem_init_inode
);
2857 static void shmem_destroy_inodecache(void)
2859 kmem_cache_destroy(shmem_inode_cachep
);
2862 static const struct address_space_operations shmem_aops
= {
2863 .writepage
= shmem_writepage
,
2864 .set_page_dirty
= __set_page_dirty_no_writeback
,
2866 .write_begin
= shmem_write_begin
,
2867 .write_end
= shmem_write_end
,
2869 .migratepage
= migrate_page
,
2870 .error_remove_page
= generic_error_remove_page
,
2873 static const struct file_operations shmem_file_operations
= {
2876 .llseek
= shmem_file_llseek
,
2877 .read
= new_sync_read
,
2878 .write
= new_sync_write
,
2879 .read_iter
= shmem_file_read_iter
,
2880 .write_iter
= generic_file_write_iter
,
2881 .fsync
= noop_fsync
,
2882 .splice_read
= shmem_file_splice_read
,
2883 .splice_write
= iter_file_splice_write
,
2884 .fallocate
= shmem_fallocate
,
2888 static const struct inode_operations shmem_inode_operations
= {
2889 .setattr
= shmem_setattr
,
2890 #ifdef CONFIG_TMPFS_XATTR
2891 .setxattr
= shmem_setxattr
,
2892 .getxattr
= shmem_getxattr
,
2893 .listxattr
= shmem_listxattr
,
2894 .removexattr
= shmem_removexattr
,
2895 .set_acl
= simple_set_acl
,
2899 static const struct inode_operations shmem_dir_inode_operations
= {
2901 .create
= shmem_create
,
2902 .lookup
= simple_lookup
,
2904 .unlink
= shmem_unlink
,
2905 .symlink
= shmem_symlink
,
2906 .mkdir
= shmem_mkdir
,
2907 .rmdir
= shmem_rmdir
,
2908 .mknod
= shmem_mknod
,
2909 .rename
= shmem_rename
,
2910 .tmpfile
= shmem_tmpfile
,
2912 #ifdef CONFIG_TMPFS_XATTR
2913 .setxattr
= shmem_setxattr
,
2914 .getxattr
= shmem_getxattr
,
2915 .listxattr
= shmem_listxattr
,
2916 .removexattr
= shmem_removexattr
,
2918 #ifdef CONFIG_TMPFS_POSIX_ACL
2919 .setattr
= shmem_setattr
,
2920 .set_acl
= simple_set_acl
,
2924 static const struct inode_operations shmem_special_inode_operations
= {
2925 #ifdef CONFIG_TMPFS_XATTR
2926 .setxattr
= shmem_setxattr
,
2927 .getxattr
= shmem_getxattr
,
2928 .listxattr
= shmem_listxattr
,
2929 .removexattr
= shmem_removexattr
,
2931 #ifdef CONFIG_TMPFS_POSIX_ACL
2932 .setattr
= shmem_setattr
,
2933 .set_acl
= simple_set_acl
,
2937 static const struct super_operations shmem_ops
= {
2938 .alloc_inode
= shmem_alloc_inode
,
2939 .destroy_inode
= shmem_destroy_inode
,
2941 .statfs
= shmem_statfs
,
2942 .remount_fs
= shmem_remount_fs
,
2943 .show_options
= shmem_show_options
,
2945 .evict_inode
= shmem_evict_inode
,
2946 .drop_inode
= generic_delete_inode
,
2947 .put_super
= shmem_put_super
,
2950 static const struct vm_operations_struct shmem_vm_ops
= {
2951 .fault
= shmem_fault
,
2952 .map_pages
= filemap_map_pages
,
2954 .set_policy
= shmem_set_policy
,
2955 .get_policy
= shmem_get_policy
,
2957 .remap_pages
= generic_file_remap_pages
,
2960 static struct dentry
*shmem_mount(struct file_system_type
*fs_type
,
2961 int flags
, const char *dev_name
, void *data
)
2963 return mount_nodev(fs_type
, flags
, data
, shmem_fill_super
);
2966 static struct file_system_type shmem_fs_type
= {
2967 .owner
= THIS_MODULE
,
2969 .mount
= shmem_mount
,
2970 .kill_sb
= kill_litter_super
,
2971 .fs_flags
= FS_USERNS_MOUNT
,
2974 int __init
shmem_init(void)
2978 /* If rootfs called this, don't re-init */
2979 if (shmem_inode_cachep
)
2982 error
= bdi_init(&shmem_backing_dev_info
);
2986 error
= shmem_init_inodecache();
2990 error
= register_filesystem(&shmem_fs_type
);
2992 printk(KERN_ERR
"Could not register tmpfs\n");
2996 shm_mnt
= kern_mount(&shmem_fs_type
);
2997 if (IS_ERR(shm_mnt
)) {
2998 error
= PTR_ERR(shm_mnt
);
2999 printk(KERN_ERR
"Could not kern_mount tmpfs\n");
3005 unregister_filesystem(&shmem_fs_type
);
3007 shmem_destroy_inodecache();
3009 bdi_destroy(&shmem_backing_dev_info
);
3011 shm_mnt
= ERR_PTR(error
);
3015 #else /* !CONFIG_SHMEM */
3018 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3020 * This is intended for small system where the benefits of the full
3021 * shmem code (swap-backed and resource-limited) are outweighed by
3022 * their complexity. On systems without swap this code should be
3023 * effectively equivalent, but much lighter weight.
3026 static struct file_system_type shmem_fs_type
= {
3028 .mount
= ramfs_mount
,
3029 .kill_sb
= kill_litter_super
,
3030 .fs_flags
= FS_USERNS_MOUNT
,
3033 int __init
shmem_init(void)
3035 BUG_ON(register_filesystem(&shmem_fs_type
) != 0);
3037 shm_mnt
= kern_mount(&shmem_fs_type
);
3038 BUG_ON(IS_ERR(shm_mnt
));
3043 int shmem_unuse(swp_entry_t swap
, struct page
*page
)
3048 int shmem_lock(struct file
*file
, int lock
, struct user_struct
*user
)
3053 void shmem_unlock_mapping(struct address_space
*mapping
)
3057 void shmem_truncate_range(struct inode
*inode
, loff_t lstart
, loff_t lend
)
3059 truncate_inode_pages_range(inode
->i_mapping
, lstart
, lend
);
3061 EXPORT_SYMBOL_GPL(shmem_truncate_range
);
3063 #define shmem_vm_ops generic_file_vm_ops
3064 #define shmem_file_operations ramfs_file_operations
3065 #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
3066 #define shmem_acct_size(flags, size) 0
3067 #define shmem_unacct_size(flags, size) do {} while (0)
3069 #endif /* CONFIG_SHMEM */
3073 static struct dentry_operations anon_ops
= {
3074 .d_dname
= simple_dname
3077 static struct file
*__shmem_file_setup(const char *name
, loff_t size
,
3078 unsigned long flags
, unsigned int i_flags
)
3081 struct inode
*inode
;
3083 struct super_block
*sb
;
3086 if (IS_ERR(shm_mnt
))
3087 return ERR_CAST(shm_mnt
);
3089 if (size
< 0 || size
> MAX_LFS_FILESIZE
)
3090 return ERR_PTR(-EINVAL
);
3092 if (shmem_acct_size(flags
, size
))
3093 return ERR_PTR(-ENOMEM
);
3095 res
= ERR_PTR(-ENOMEM
);
3097 this.len
= strlen(name
);
3098 this.hash
= 0; /* will go */
3099 sb
= shm_mnt
->mnt_sb
;
3100 path
.mnt
= mntget(shm_mnt
);
3101 path
.dentry
= d_alloc_pseudo(sb
, &this);
3104 d_set_d_op(path
.dentry
, &anon_ops
);
3106 res
= ERR_PTR(-ENOSPC
);
3107 inode
= shmem_get_inode(sb
, NULL
, S_IFREG
| S_IRWXUGO
, 0, flags
);
3111 inode
->i_flags
|= i_flags
;
3112 d_instantiate(path
.dentry
, inode
);
3113 inode
->i_size
= size
;
3114 clear_nlink(inode
); /* It is unlinked */
3115 res
= ERR_PTR(ramfs_nommu_expand_for_mapping(inode
, size
));
3119 res
= alloc_file(&path
, FMODE_WRITE
| FMODE_READ
,
3120 &shmem_file_operations
);
3127 shmem_unacct_size(flags
, size
);
3134 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3135 * kernel internal. There will be NO LSM permission checks against the
3136 * underlying inode. So users of this interface must do LSM checks at a
3137 * higher layer. The one user is the big_key implementation. LSM checks
3138 * are provided at the key level rather than the inode level.
3139 * @name: name for dentry (to be seen in /proc/<pid>/maps
3140 * @size: size to be set for the file
3141 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3143 struct file
*shmem_kernel_file_setup(const char *name
, loff_t size
, unsigned long flags
)
3145 return __shmem_file_setup(name
, size
, flags
, S_PRIVATE
);
3149 * shmem_file_setup - get an unlinked file living in tmpfs
3150 * @name: name for dentry (to be seen in /proc/<pid>/maps
3151 * @size: size to be set for the file
3152 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3154 struct file
*shmem_file_setup(const char *name
, loff_t size
, unsigned long flags
)
3156 return __shmem_file_setup(name
, size
, flags
, 0);
3158 EXPORT_SYMBOL_GPL(shmem_file_setup
);
3161 * shmem_zero_setup - setup a shared anonymous mapping
3162 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
3164 int shmem_zero_setup(struct vm_area_struct
*vma
)
3167 loff_t size
= vma
->vm_end
- vma
->vm_start
;
3169 file
= shmem_file_setup("dev/zero", size
, vma
->vm_flags
);
3171 return PTR_ERR(file
);
3175 vma
->vm_file
= file
;
3176 vma
->vm_ops
= &shmem_vm_ops
;
3181 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3182 * @mapping: the page's address_space
3183 * @index: the page index
3184 * @gfp: the page allocator flags to use if allocating
3186 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3187 * with any new page allocations done using the specified allocation flags.
3188 * But read_cache_page_gfp() uses the ->readpage() method: which does not
3189 * suit tmpfs, since it may have pages in swapcache, and needs to find those
3190 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
3192 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
3193 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
3195 struct page
*shmem_read_mapping_page_gfp(struct address_space
*mapping
,
3196 pgoff_t index
, gfp_t gfp
)
3199 struct inode
*inode
= mapping
->host
;
3203 BUG_ON(mapping
->a_ops
!= &shmem_aops
);
3204 error
= shmem_getpage_gfp(inode
, index
, &page
, SGP_CACHE
, gfp
, NULL
);
3206 page
= ERR_PTR(error
);
3212 * The tiny !SHMEM case uses ramfs without swap
3214 return read_cache_page_gfp(mapping
, index
, gfp
);
3217 EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp
);