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/pagemap.h>
29 #include <linux/file.h>
31 #include <linux/export.h>
32 #include <linux/swap.h>
34 static struct vfsmount *shm_mnt;
38 * This virtual memory filesystem is heavily based on the ramfs. It
39 * extends ramfs by the ability to use swap and honor resource limits
40 * which makes it a completely usable filesystem.
43 #include <linux/xattr.h>
44 #include <linux/exportfs.h>
45 #include <linux/posix_acl.h>
46 #include <linux/generic_acl.h>
47 #include <linux/mman.h>
48 #include <linux/string.h>
49 #include <linux/slab.h>
50 #include <linux/backing-dev.h>
51 #include <linux/shmem_fs.h>
52 #include <linux/writeback.h>
53 #include <linux/blkdev.h>
54 #include <linux/pagevec.h>
55 #include <linux/percpu_counter.h>
56 #include <linux/splice.h>
57 #include <linux/security.h>
58 #include <linux/swapops.h>
59 #include <linux/mempolicy.h>
60 #include <linux/namei.h>
61 #include <linux/ctype.h>
62 #include <linux/migrate.h>
63 #include <linux/highmem.h>
64 #include <linux/seq_file.h>
65 #include <linux/magic.h>
66 #include <linux/syscalls.h>
67 #include <linux/fcntl.h>
68 #include <uapi/linux/memfd.h>
70 #include <asm/uaccess.h>
71 #include <asm/pgtable.h>
73 #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
74 #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
76 /* Pretend that each entry is of this size in directory's i_size */
77 #define BOGO_DIRENT_SIZE 20
79 /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
80 #define SHORT_SYMLINK_LEN 128
83 * vmtruncate_range() communicates with shmem_fault via
84 * inode->i_private (with i_mutex making sure that it has only one user at
85 * a time): we would prefer not to enlarge the shmem inode just for that.
88 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
89 pgoff_t start; /* start of range currently being fallocated */
90 pgoff_t next; /* the next page offset to be fallocated */
94 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
95 char *name; /* xattr name */
100 /* Flag allocation requirements to shmem_getpage */
102 SGP_READ, /* don't exceed i_size, don't allocate page */
103 SGP_CACHE, /* don't exceed i_size, may allocate page */
104 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
105 SGP_WRITE, /* may exceed i_size, may allocate page */
109 static unsigned long shmem_default_max_blocks(void)
111 return totalram_pages / 2;
114 static unsigned long shmem_default_max_inodes(void)
116 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
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_kern(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));
154 * ... whereas tmpfs objects are accounted incrementally as
155 * pages are allocated, in order to allow huge sparse files.
156 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
157 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
159 static inline int shmem_acct_block(unsigned long flags)
161 return (flags & VM_NORESERVE) ?
162 security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
165 static inline void shmem_unacct_blocks(unsigned long flags, long pages)
167 if (flags & VM_NORESERVE)
168 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
171 static const struct super_operations shmem_ops;
172 static const struct address_space_operations shmem_aops;
173 static const struct file_operations shmem_file_operations;
174 static const struct inode_operations shmem_inode_operations;
175 static const struct inode_operations shmem_dir_inode_operations;
176 static const struct inode_operations shmem_special_inode_operations;
177 static const struct vm_operations_struct shmem_vm_ops;
179 static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
180 .ra_pages = 0, /* No readahead */
181 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
184 static LIST_HEAD(shmem_swaplist);
185 static DEFINE_MUTEX(shmem_swaplist_mutex);
187 static int shmem_reserve_inode(struct super_block *sb)
189 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
190 if (sbinfo->max_inodes) {
191 spin_lock(&sbinfo->stat_lock);
192 if (!sbinfo->free_inodes) {
193 spin_unlock(&sbinfo->stat_lock);
196 sbinfo->free_inodes--;
197 spin_unlock(&sbinfo->stat_lock);
202 static void shmem_free_inode(struct super_block *sb)
204 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
205 if (sbinfo->max_inodes) {
206 spin_lock(&sbinfo->stat_lock);
207 sbinfo->free_inodes++;
208 spin_unlock(&sbinfo->stat_lock);
213 * shmem_recalc_inode - recalculate the block usage of an inode
214 * @inode: inode to recalc
216 * We have to calculate the free blocks since the mm can drop
217 * undirtied hole pages behind our back.
219 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
220 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
222 * It has to be called with the spinlock held.
224 static void shmem_recalc_inode(struct inode *inode)
226 struct shmem_inode_info *info = SHMEM_I(inode);
229 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
231 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
232 if (sbinfo->max_blocks)
233 percpu_counter_add(&sbinfo->used_blocks, -freed);
234 info->alloced -= freed;
235 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
236 shmem_unacct_blocks(info->flags, freed);
241 * Replace item expected in radix tree by a new item, while holding tree lock.
243 static int shmem_radix_tree_replace(struct address_space *mapping,
244 pgoff_t index, void *expected, void *replacement)
249 VM_BUG_ON(!expected);
250 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
252 item = radix_tree_deref_slot_protected(pslot,
253 &mapping->tree_lock);
254 if (item != expected)
257 radix_tree_replace_slot(pslot, replacement);
259 radix_tree_delete(&mapping->page_tree, index);
264 * Like add_to_page_cache_locked, but error if expected item has gone.
266 static int shmem_add_to_page_cache(struct page *page,
267 struct address_space *mapping,
268 pgoff_t index, gfp_t gfp, void *expected)
272 VM_BUG_ON(!PageLocked(page));
273 VM_BUG_ON(!PageSwapBacked(page));
276 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
278 page_cache_get(page);
279 page->mapping = mapping;
282 spin_lock_irq(&mapping->tree_lock);
284 error = radix_tree_insert(&mapping->page_tree,
287 error = shmem_radix_tree_replace(mapping, index,
291 __inc_zone_page_state(page, NR_FILE_PAGES);
292 __inc_zone_page_state(page, NR_SHMEM);
293 spin_unlock_irq(&mapping->tree_lock);
295 page->mapping = NULL;
296 spin_unlock_irq(&mapping->tree_lock);
297 page_cache_release(page);
300 radix_tree_preload_end();
303 mem_cgroup_uncharge_cache_page(page);
308 * Like delete_from_page_cache, but substitutes swap for page.
310 static void shmem_delete_from_page_cache(struct page *page, void *radswap)
312 struct address_space *mapping = page->mapping;
315 spin_lock_irq(&mapping->tree_lock);
316 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
317 page->mapping = NULL;
319 __dec_zone_page_state(page, NR_FILE_PAGES);
320 __dec_zone_page_state(page, NR_SHMEM);
321 spin_unlock_irq(&mapping->tree_lock);
322 page_cache_release(page);
327 * Like find_get_pages, but collecting swap entries as well as pages.
329 static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
330 pgoff_t start, unsigned int nr_pages,
331 struct page **pages, pgoff_t *indices)
335 unsigned int nr_found;
339 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
340 (void ***)pages, indices, start, nr_pages);
342 for (i = 0; i < nr_found; i++) {
345 page = radix_tree_deref_slot((void **)pages[i]);
348 if (radix_tree_exception(page)) {
349 if (radix_tree_deref_retry(page))
352 * Otherwise, we must be storing a swap entry
353 * here as an exceptional entry: so return it
354 * without attempting to raise page count.
358 if (!page_cache_get_speculative(page))
361 /* Has the page moved? */
362 if (unlikely(page != *((void **)pages[i]))) {
363 page_cache_release(page);
367 indices[ret] = indices[i];
371 if (unlikely(!ret && nr_found))
378 * Remove swap entry from radix tree, free the swap and its page cache.
380 static int shmem_free_swap(struct address_space *mapping,
381 pgoff_t index, void *radswap)
385 spin_lock_irq(&mapping->tree_lock);
386 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
387 spin_unlock_irq(&mapping->tree_lock);
389 free_swap_and_cache(radix_to_swp_entry(radswap));
394 * Pagevec may contain swap entries, so shuffle up pages before releasing.
396 static void shmem_deswap_pagevec(struct pagevec *pvec)
400 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
401 struct page *page = pvec->pages[i];
402 if (!radix_tree_exceptional_entry(page))
403 pvec->pages[j++] = page;
409 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
411 void shmem_unlock_mapping(struct address_space *mapping)
414 pgoff_t indices[PAGEVEC_SIZE];
417 pagevec_init(&pvec, 0);
419 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
421 while (!mapping_unevictable(mapping)) {
423 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
424 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
426 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
427 PAGEVEC_SIZE, pvec.pages, indices);
430 index = indices[pvec.nr - 1] + 1;
431 shmem_deswap_pagevec(&pvec);
432 check_move_unevictable_pages(pvec.pages, pvec.nr);
433 pagevec_release(&pvec);
439 * Remove range of pages and swap entries from radix tree, and free them.
441 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
443 struct address_space *mapping = inode->i_mapping;
444 struct shmem_inode_info *info = SHMEM_I(inode);
445 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
446 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
447 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
449 pgoff_t indices[PAGEVEC_SIZE];
450 long nr_swaps_freed = 0;
454 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
456 pagevec_init(&pvec, 0);
458 while (index <= end) {
459 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
460 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
461 pvec.pages, indices);
464 mem_cgroup_uncharge_start();
465 for (i = 0; i < pagevec_count(&pvec); i++) {
466 struct page *page = pvec.pages[i];
472 if (radix_tree_exceptional_entry(page)) {
473 nr_swaps_freed += !shmem_free_swap(mapping,
478 if (!trylock_page(page))
480 if (page->mapping == mapping) {
481 VM_BUG_ON(PageWriteback(page));
482 truncate_inode_page(mapping, page);
486 shmem_deswap_pagevec(&pvec);
487 pagevec_release(&pvec);
488 mem_cgroup_uncharge_end();
494 struct page *page = NULL;
495 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
497 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
498 set_page_dirty(page);
500 page_cache_release(page);
505 while (index <= end) {
507 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
508 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
509 pvec.pages, indices);
511 /* If all gone or hole-punch, we're done */
512 if (index == start || end != -1)
514 /* But if truncating, restart to make sure all gone */
518 mem_cgroup_uncharge_start();
519 for (i = 0; i < pagevec_count(&pvec); i++) {
520 struct page *page = pvec.pages[i];
526 if (radix_tree_exceptional_entry(page)) {
527 if (shmem_free_swap(mapping, index, page)) {
528 /* Swap was replaced by page: retry */
537 if (page->mapping == mapping) {
538 VM_BUG_ON(PageWriteback(page));
539 truncate_inode_page(mapping, page);
541 /* Page was replaced by swap: retry */
548 shmem_deswap_pagevec(&pvec);
549 pagevec_release(&pvec);
550 mem_cgroup_uncharge_end();
554 spin_lock(&info->lock);
555 info->swapped -= nr_swaps_freed;
556 shmem_recalc_inode(inode);
557 spin_unlock(&info->lock);
559 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
561 EXPORT_SYMBOL_GPL(shmem_truncate_range);
563 static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
565 struct inode *inode = dentry->d_inode;
566 struct shmem_inode_info *info = SHMEM_I(inode);
569 error = setattr_prepare(dentry, attr);
573 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
574 loff_t oldsize = inode->i_size;
575 loff_t newsize = attr->ia_size;
577 /* protected by i_mutex */
578 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
579 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
582 if (newsize != oldsize) {
583 i_size_write(inode, newsize);
584 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
586 if (newsize < oldsize) {
587 loff_t holebegin = round_up(newsize, PAGE_SIZE);
588 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
589 shmem_truncate_range(inode, newsize, (loff_t)-1);
590 /* unmap again to remove racily COWed private pages */
591 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
595 setattr_copy(inode, attr);
596 #ifdef CONFIG_TMPFS_POSIX_ACL
597 if (attr->ia_valid & ATTR_MODE)
598 error = generic_acl_chmod(inode);
603 static void shmem_evict_inode(struct inode *inode)
605 struct shmem_inode_info *info = SHMEM_I(inode);
606 struct shmem_xattr *xattr, *nxattr;
608 if (inode->i_mapping->a_ops == &shmem_aops) {
609 shmem_unacct_size(info->flags, inode->i_size);
611 shmem_truncate_range(inode, 0, (loff_t)-1);
612 if (!list_empty(&info->swaplist)) {
613 mutex_lock(&shmem_swaplist_mutex);
614 list_del_init(&info->swaplist);
615 mutex_unlock(&shmem_swaplist_mutex);
618 kfree(info->symlink);
620 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
624 WARN_ON(inode->i_blocks);
625 shmem_free_inode(inode->i_sb);
626 end_writeback(inode);
630 * If swap found in inode, free it and move page from swapcache to filecache.
632 static int shmem_unuse_inode(struct shmem_inode_info *info,
633 swp_entry_t swap, struct page *page)
635 struct address_space *mapping = info->vfs_inode.i_mapping;
640 radswap = swp_to_radix_entry(swap);
641 index = radix_tree_locate_item(&mapping->page_tree, radswap);
646 * Move _head_ to start search for next from here.
647 * But be careful: shmem_evict_inode checks list_empty without taking
648 * mutex, and there's an instant in list_move_tail when info->swaplist
649 * would appear empty, if it were the only one on shmem_swaplist.
651 if (shmem_swaplist.next != &info->swaplist)
652 list_move_tail(&shmem_swaplist, &info->swaplist);
655 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
656 * but also to hold up shmem_evict_inode(): so inode cannot be freed
657 * beneath us (pagelock doesn't help until the page is in pagecache).
659 error = shmem_add_to_page_cache(page, mapping, index,
660 GFP_NOWAIT, radswap);
661 /* which does mem_cgroup_uncharge_cache_page on error */
663 if (error != -ENOMEM) {
665 * Truncation and eviction use free_swap_and_cache(), which
666 * only does trylock page: if we raced, best clean up here.
668 delete_from_swap_cache(page);
669 set_page_dirty(page);
671 spin_lock(&info->lock);
673 spin_unlock(&info->lock);
676 error = 1; /* not an error, but entry was found */
682 * Search through swapped inodes to find and replace swap by page.
684 int shmem_unuse(swp_entry_t swap, struct page *page)
686 struct list_head *this, *next;
687 struct shmem_inode_info *info;
692 * Charge page using GFP_KERNEL while we can wait, before taking
693 * the shmem_swaplist_mutex which might hold up shmem_writepage().
694 * Charged back to the user (not to caller) when swap account is used.
696 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
699 /* No radix_tree_preload: swap entry keeps a place for page in tree */
701 mutex_lock(&shmem_swaplist_mutex);
702 list_for_each_safe(this, next, &shmem_swaplist) {
703 info = list_entry(this, struct shmem_inode_info, swaplist);
705 found = shmem_unuse_inode(info, swap, page);
707 list_del_init(&info->swaplist);
712 mutex_unlock(&shmem_swaplist_mutex);
715 mem_cgroup_uncharge_cache_page(page);
720 page_cache_release(page);
725 * Move the page from the page cache to the swap cache.
727 static int shmem_writepage(struct page *page, struct writeback_control *wbc)
729 struct shmem_inode_info *info;
730 struct address_space *mapping;
735 BUG_ON(!PageLocked(page));
736 mapping = page->mapping;
738 inode = mapping->host;
739 info = SHMEM_I(inode);
740 if (info->flags & VM_LOCKED)
742 if (!total_swap_pages)
746 * shmem_backing_dev_info's capabilities prevent regular writeback or
747 * sync from ever calling shmem_writepage; but a stacking filesystem
748 * might use ->writepage of its underlying filesystem, in which case
749 * tmpfs should write out to swap only in response to memory pressure,
750 * and not for the writeback threads or sync.
752 if (!wbc->for_reclaim) {
753 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
756 swap = get_swap_page();
761 * Add inode to shmem_unuse()'s list of swapped-out inodes,
762 * if it's not already there. Do it now before the page is
763 * moved to swap cache, when its pagelock no longer protects
764 * the inode from eviction. But don't unlock the mutex until
765 * we've incremented swapped, because shmem_unuse_inode() will
766 * prune a !swapped inode from the swaplist under this mutex.
768 mutex_lock(&shmem_swaplist_mutex);
769 if (list_empty(&info->swaplist))
770 list_add_tail(&info->swaplist, &shmem_swaplist);
772 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
773 swap_shmem_alloc(swap);
774 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
776 spin_lock(&info->lock);
778 shmem_recalc_inode(inode);
779 spin_unlock(&info->lock);
781 mutex_unlock(&shmem_swaplist_mutex);
782 BUG_ON(page_mapped(page));
783 swap_writepage(page, wbc);
787 mutex_unlock(&shmem_swaplist_mutex);
788 swapcache_free(swap, NULL);
790 set_page_dirty(page);
791 if (wbc->for_reclaim)
792 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
799 static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
803 if (!mpol || mpol->mode == MPOL_DEFAULT)
804 return; /* show nothing */
806 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
808 seq_printf(seq, ",mpol=%s", buffer);
811 static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
813 struct mempolicy *mpol = NULL;
815 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
818 spin_unlock(&sbinfo->stat_lock);
822 #endif /* CONFIG_TMPFS */
824 static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
825 struct shmem_inode_info *info, pgoff_t index)
827 struct vm_area_struct pvma;
830 /* Create a pseudo vma that just contains the policy */
832 pvma.vm_pgoff = index;
834 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
836 page = swapin_readahead(swap, gfp, &pvma, 0);
838 /* Drop reference taken by mpol_shared_policy_lookup() */
839 mpol_cond_put(pvma.vm_policy);
844 static struct page *shmem_alloc_page(gfp_t gfp,
845 struct shmem_inode_info *info, pgoff_t index)
847 struct vm_area_struct pvma;
850 /* Create a pseudo vma that just contains the policy */
852 pvma.vm_pgoff = index;
854 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
856 page = alloc_page_vma(gfp, &pvma, 0);
858 /* Drop reference taken by mpol_shared_policy_lookup() */
859 mpol_cond_put(pvma.vm_policy);
863 #else /* !CONFIG_NUMA */
865 static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
868 #endif /* CONFIG_TMPFS */
870 static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
871 struct shmem_inode_info *info, pgoff_t index)
873 return swapin_readahead(swap, gfp, NULL, 0);
876 static inline struct page *shmem_alloc_page(gfp_t gfp,
877 struct shmem_inode_info *info, pgoff_t index)
879 return alloc_page(gfp);
881 #endif /* CONFIG_NUMA */
883 #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
884 static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
891 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
893 * If we allocate a new one we do not mark it dirty. That's up to the
894 * vm. If we swap it in we mark it dirty since we also free the swap
895 * entry since a page cannot live in both the swap and page cache
897 static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
898 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
900 struct address_space *mapping = inode->i_mapping;
901 struct shmem_inode_info *info;
902 struct shmem_sb_info *sbinfo;
908 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
912 page = find_lock_page(mapping, index);
913 if (radix_tree_exceptional_entry(page)) {
914 swap = radix_to_swp_entry(page);
918 if (sgp != SGP_WRITE &&
919 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
924 if (page || (sgp == SGP_READ && !swap.val)) {
926 * Once we can get the page lock, it must be uptodate:
927 * if there were an error in reading back from swap,
928 * the page would not be inserted into the filecache.
930 BUG_ON(page && !PageUptodate(page));
936 * Fast cache lookup did not find it:
937 * bring it back from swap or allocate.
939 info = SHMEM_I(inode);
940 sbinfo = SHMEM_SB(inode->i_sb);
943 /* Look it up and read it in.. */
944 page = lookup_swap_cache(swap);
946 /* here we actually do the io */
948 *fault_type |= VM_FAULT_MAJOR;
949 page = shmem_swapin(swap, gfp, info, index);
956 /* We have to do this with page locked to prevent races */
958 if (!PageUptodate(page)) {
962 wait_on_page_writeback(page);
964 /* Someone may have already done it for us */
966 if (page->mapping == mapping &&
967 page->index == index)
973 error = mem_cgroup_cache_charge(page, current->mm,
974 gfp & GFP_RECLAIM_MASK);
976 error = shmem_add_to_page_cache(page, mapping, index,
977 gfp, swp_to_radix_entry(swap));
981 spin_lock(&info->lock);
983 shmem_recalc_inode(inode);
984 spin_unlock(&info->lock);
986 delete_from_swap_cache(page);
987 set_page_dirty(page);
991 if (shmem_acct_block(info->flags)) {
995 if (sbinfo->max_blocks) {
996 if (percpu_counter_compare(&sbinfo->used_blocks,
997 sbinfo->max_blocks) >= 0) {
1001 percpu_counter_inc(&sbinfo->used_blocks);
1004 page = shmem_alloc_page(gfp, info, index);
1010 SetPageSwapBacked(page);
1011 __set_page_locked(page);
1012 error = mem_cgroup_cache_charge(page, current->mm,
1013 gfp & GFP_RECLAIM_MASK);
1015 error = shmem_add_to_page_cache(page, mapping, index,
1019 lru_cache_add_anon(page);
1021 spin_lock(&info->lock);
1023 inode->i_blocks += BLOCKS_PER_PAGE;
1024 shmem_recalc_inode(inode);
1025 spin_unlock(&info->lock);
1027 clear_highpage(page);
1028 flush_dcache_page(page);
1029 SetPageUptodate(page);
1030 if (sgp == SGP_DIRTY)
1031 set_page_dirty(page);
1034 /* Perhaps the file has been truncated since we checked */
1035 if (sgp != SGP_WRITE &&
1036 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1047 ClearPageDirty(page);
1048 delete_from_page_cache(page);
1049 spin_lock(&info->lock);
1051 inode->i_blocks -= BLOCKS_PER_PAGE;
1052 spin_unlock(&info->lock);
1054 if (sbinfo->max_blocks)
1055 percpu_counter_add(&sbinfo->used_blocks, -1);
1057 shmem_unacct_blocks(info->flags, 1);
1059 if (swap.val && error != -EINVAL) {
1060 struct page *test = find_get_page(mapping, index);
1061 if (test && !radix_tree_exceptional_entry(test))
1062 page_cache_release(test);
1063 /* Have another try if the entry has changed */
1064 if (test != swp_to_radix_entry(swap))
1069 page_cache_release(page);
1071 if (error == -ENOSPC && !once++) {
1072 info = SHMEM_I(inode);
1073 spin_lock(&info->lock);
1074 shmem_recalc_inode(inode);
1075 spin_unlock(&info->lock);
1078 if (error == -EEXIST)
1083 static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1085 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1087 int ret = VM_FAULT_LOCKED;
1090 * Trinity finds that probing a hole which tmpfs is punching can
1091 * prevent the hole-punch from ever completing: which in turn
1092 * locks writers out with its hold on i_mutex. So refrain from
1093 * faulting pages into the hole while it's being punched. Although
1094 * shmem_truncate_range() does remove the additions, it may be unable to
1095 * keep up, as each new page needs its own unmap_mapping_range() call,
1096 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1098 * It does not matter if we sometimes reach this check just before the
1099 * hole-punch begins, so that one fault then races with the punch:
1100 * we just need to make racing faults a rare case.
1102 * The implementation below would be much simpler if we just used a
1103 * standard mutex or completion: but we cannot take i_mutex in fault,
1104 * and bloating every shmem inode for this unlikely case would be sad.
1106 if (unlikely(inode->i_private)) {
1107 struct shmem_falloc *shmem_falloc;
1109 spin_lock(&inode->i_lock);
1110 shmem_falloc = inode->i_private;
1112 vmf->pgoff >= shmem_falloc->start &&
1113 vmf->pgoff < shmem_falloc->next) {
1114 wait_queue_head_t *shmem_falloc_waitq;
1115 DEFINE_WAIT(shmem_fault_wait);
1117 ret = VM_FAULT_NOPAGE;
1118 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1119 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
1120 /* It's polite to up mmap_sem if we can */
1121 up_read(&vma->vm_mm->mmap_sem);
1122 ret = VM_FAULT_RETRY;
1125 shmem_falloc_waitq = shmem_falloc->waitq;
1126 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
1127 TASK_UNINTERRUPTIBLE);
1128 spin_unlock(&inode->i_lock);
1132 * shmem_falloc_waitq points into the vmtruncate_range()
1133 * stack of the hole-punching task: shmem_falloc_waitq
1134 * is usually invalid by the time we reach here, but
1135 * finish_wait() does not dereference it in that case;
1136 * though i_lock needed lest racing with wake_up_all().
1138 spin_lock(&inode->i_lock);
1139 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
1140 spin_unlock(&inode->i_lock);
1143 spin_unlock(&inode->i_lock);
1146 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1148 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
1150 if (ret & VM_FAULT_MAJOR) {
1151 count_vm_event(PGMAJFAULT);
1152 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1157 int vmtruncate_range(struct inode *inode, loff_t lstart, loff_t lend)
1160 * If the underlying filesystem is not going to provide
1161 * a way to truncate a range of blocks (punch a hole) -
1162 * we should return failure right now.
1163 * Only CONFIG_SHMEM shmem.c ever supported i_op->truncate_range().
1165 if (inode->i_op->truncate_range != shmem_truncate_range)
1168 mutex_lock(&inode->i_mutex);
1170 struct shmem_falloc shmem_falloc;
1171 struct address_space *mapping = inode->i_mapping;
1172 loff_t unmap_start = round_up(lstart, PAGE_SIZE);
1173 loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
1174 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
1176 shmem_falloc.waitq = &shmem_falloc_waitq;
1177 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
1178 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
1179 spin_lock(&inode->i_lock);
1180 inode->i_private = &shmem_falloc;
1181 spin_unlock(&inode->i_lock);
1183 if ((u64)unmap_end > (u64)unmap_start)
1184 unmap_mapping_range(mapping, unmap_start,
1185 1 + unmap_end - unmap_start, 0);
1186 shmem_truncate_range(inode, lstart, lend);
1187 /* No need to unmap again: hole-punching leaves COWed pages */
1189 spin_lock(&inode->i_lock);
1190 inode->i_private = NULL;
1191 wake_up_all(&shmem_falloc_waitq);
1192 spin_unlock(&inode->i_lock);
1194 mutex_unlock(&inode->i_mutex);
1197 EXPORT_SYMBOL_GPL(vmtruncate_range);
1200 static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
1202 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1203 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
1206 static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1209 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1212 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1213 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
1217 int shmem_lock(struct file *file, int lock, struct user_struct *user)
1219 struct inode *inode = file->f_path.dentry->d_inode;
1220 struct shmem_inode_info *info = SHMEM_I(inode);
1221 int retval = -ENOMEM;
1223 spin_lock(&info->lock);
1224 if (lock && !(info->flags & VM_LOCKED)) {
1225 if (!user_shm_lock(inode->i_size, user))
1227 info->flags |= VM_LOCKED;
1228 mapping_set_unevictable(file->f_mapping);
1230 if (!lock && (info->flags & VM_LOCKED) && user) {
1231 user_shm_unlock(inode->i_size, user);
1232 info->flags &= ~VM_LOCKED;
1233 mapping_clear_unevictable(file->f_mapping);
1238 spin_unlock(&info->lock);
1242 static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1244 file_accessed(file);
1245 vma->vm_ops = &shmem_vm_ops;
1246 vma->vm_flags |= VM_CAN_NONLINEAR;
1250 static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1251 int mode, dev_t dev, unsigned long flags)
1253 struct inode *inode;
1254 struct shmem_inode_info *info;
1255 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1257 if (shmem_reserve_inode(sb))
1260 inode = new_inode(sb);
1262 inode->i_ino = get_next_ino();
1263 inode_init_owner(inode, dir, mode);
1264 inode->i_blocks = 0;
1265 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1266 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1267 inode->i_generation = get_seconds();
1268 info = SHMEM_I(inode);
1269 memset(info, 0, (char *)inode - (char *)info);
1270 spin_lock_init(&info->lock);
1271 info->seals = F_SEAL_SEAL;
1272 info->flags = flags & VM_NORESERVE;
1273 INIT_LIST_HEAD(&info->swaplist);
1274 INIT_LIST_HEAD(&info->xattr_list);
1275 cache_no_acl(inode);
1277 switch (mode & S_IFMT) {
1279 inode->i_op = &shmem_special_inode_operations;
1280 init_special_inode(inode, mode, dev);
1283 inode->i_mapping->a_ops = &shmem_aops;
1284 inode->i_op = &shmem_inode_operations;
1285 inode->i_fop = &shmem_file_operations;
1286 mpol_shared_policy_init(&info->policy,
1287 shmem_get_sbmpol(sbinfo));
1291 /* Some things misbehave if size == 0 on a directory */
1292 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1293 inode->i_op = &shmem_dir_inode_operations;
1294 inode->i_fop = &simple_dir_operations;
1298 * Must not load anything in the rbtree,
1299 * mpol_free_shared_policy will not be called.
1301 mpol_shared_policy_init(&info->policy, NULL);
1305 shmem_free_inode(sb);
1310 static const struct inode_operations shmem_symlink_inode_operations;
1311 static const struct inode_operations shmem_short_symlink_operations;
1314 shmem_write_begin(struct file *file, struct address_space *mapping,
1315 loff_t pos, unsigned len, unsigned flags,
1316 struct page **pagep, void **fsdata)
1318 struct inode *inode = mapping->host;
1319 struct shmem_inode_info *info = SHMEM_I(inode);
1320 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1322 /* i_mutex is held by caller */
1323 if (unlikely(info->seals)) {
1324 if (info->seals & F_SEAL_WRITE)
1326 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
1330 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1334 shmem_write_end(struct file *file, struct address_space *mapping,
1335 loff_t pos, unsigned len, unsigned copied,
1336 struct page *page, void *fsdata)
1338 struct inode *inode = mapping->host;
1340 if (pos + copied > inode->i_size)
1341 i_size_write(inode, pos + copied);
1343 set_page_dirty(page);
1345 page_cache_release(page);
1350 static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1352 struct inode *inode = filp->f_path.dentry->d_inode;
1353 struct address_space *mapping = inode->i_mapping;
1355 unsigned long offset;
1356 enum sgp_type sgp = SGP_READ;
1359 * Might this read be for a stacking filesystem? Then when reading
1360 * holes of a sparse file, we actually need to allocate those pages,
1361 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1363 if (segment_eq(get_fs(), KERNEL_DS))
1366 index = *ppos >> PAGE_CACHE_SHIFT;
1367 offset = *ppos & ~PAGE_CACHE_MASK;
1370 struct page *page = NULL;
1372 unsigned long nr, ret;
1373 loff_t i_size = i_size_read(inode);
1375 end_index = i_size >> PAGE_CACHE_SHIFT;
1376 if (index > end_index)
1378 if (index == end_index) {
1379 nr = i_size & ~PAGE_CACHE_MASK;
1384 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
1386 if (desc->error == -EINVAL)
1394 * We must evaluate after, since reads (unlike writes)
1395 * are called without i_mutex protection against truncate
1397 nr = PAGE_CACHE_SIZE;
1398 i_size = i_size_read(inode);
1399 end_index = i_size >> PAGE_CACHE_SHIFT;
1400 if (index == end_index) {
1401 nr = i_size & ~PAGE_CACHE_MASK;
1404 page_cache_release(page);
1412 * If users can be writing to this page using arbitrary
1413 * virtual addresses, take care about potential aliasing
1414 * before reading the page on the kernel side.
1416 if (mapping_writably_mapped(mapping))
1417 flush_dcache_page(page);
1419 * Mark the page accessed if we read the beginning.
1422 mark_page_accessed(page);
1424 page = ZERO_PAGE(0);
1425 page_cache_get(page);
1429 * Ok, we have the page, and it's up-to-date, so
1430 * now we can copy it to user space...
1432 * The actor routine returns how many bytes were actually used..
1433 * NOTE! This may not be the same as how much of a user buffer
1434 * we filled up (we may be padding etc), so we can only update
1435 * "pos" here (the actor routine has to update the user buffer
1436 * pointers and the remaining count).
1438 ret = actor(desc, page, offset, nr);
1440 index += offset >> PAGE_CACHE_SHIFT;
1441 offset &= ~PAGE_CACHE_MASK;
1443 page_cache_release(page);
1444 if (ret != nr || !desc->count)
1450 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1451 file_accessed(filp);
1454 static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1455 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
1457 struct file *filp = iocb->ki_filp;
1461 loff_t *ppos = &iocb->ki_pos;
1463 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1467 for (seg = 0; seg < nr_segs; seg++) {
1468 read_descriptor_t desc;
1471 desc.arg.buf = iov[seg].iov_base;
1472 desc.count = iov[seg].iov_len;
1473 if (desc.count == 0)
1476 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1477 retval += desc.written;
1479 retval = retval ?: desc.error;
1488 static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1489 struct pipe_inode_info *pipe, size_t len,
1492 struct address_space *mapping = in->f_mapping;
1493 struct inode *inode = mapping->host;
1494 unsigned int loff, nr_pages, req_pages;
1495 struct page *pages[PIPE_DEF_BUFFERS];
1496 struct partial_page partial[PIPE_DEF_BUFFERS];
1498 pgoff_t index, end_index;
1501 struct splice_pipe_desc spd = {
1504 .nr_pages_max = PIPE_DEF_BUFFERS,
1506 .ops = &page_cache_pipe_buf_ops,
1507 .spd_release = spd_release_page,
1510 isize = i_size_read(inode);
1511 if (unlikely(*ppos >= isize))
1514 left = isize - *ppos;
1515 if (unlikely(left < len))
1518 if (splice_grow_spd(pipe, &spd))
1521 index = *ppos >> PAGE_CACHE_SHIFT;
1522 loff = *ppos & ~PAGE_CACHE_MASK;
1523 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1524 nr_pages = min(req_pages, pipe->buffers);
1526 spd.nr_pages = find_get_pages_contig(mapping, index,
1527 nr_pages, spd.pages);
1528 index += spd.nr_pages;
1531 while (spd.nr_pages < nr_pages) {
1532 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1536 spd.pages[spd.nr_pages++] = page;
1540 index = *ppos >> PAGE_CACHE_SHIFT;
1541 nr_pages = spd.nr_pages;
1544 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1545 unsigned int this_len;
1550 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1551 page = spd.pages[page_nr];
1553 if (!PageUptodate(page) || page->mapping != mapping) {
1554 error = shmem_getpage(inode, index, &page,
1559 page_cache_release(spd.pages[page_nr]);
1560 spd.pages[page_nr] = page;
1563 isize = i_size_read(inode);
1564 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1565 if (unlikely(!isize || index > end_index))
1568 if (end_index == index) {
1571 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1575 this_len = min(this_len, plen - loff);
1579 spd.partial[page_nr].offset = loff;
1580 spd.partial[page_nr].len = this_len;
1587 while (page_nr < nr_pages)
1588 page_cache_release(spd.pages[page_nr++]);
1591 error = splice_to_pipe(pipe, &spd);
1593 splice_shrink_spd(&spd);
1602 static int shmem_wait_for_pins(struct address_space *mapping)
1607 #define F_ALL_SEALS (F_SEAL_SEAL | \
1612 int shmem_add_seals(struct file *file, unsigned int seals)
1614 struct inode *inode = file_inode(file);
1615 struct shmem_inode_info *info = SHMEM_I(inode);
1620 * Sealing allows multiple parties to share a shmem-file but restrict
1621 * access to a specific subset of file operations. Seals can only be
1622 * added, but never removed. This way, mutually untrusted parties can
1623 * share common memory regions with a well-defined policy. A malicious
1624 * peer can thus never perform unwanted operations on a shared object.
1626 * Seals are only supported on special shmem-files and always affect
1627 * the whole underlying inode. Once a seal is set, it may prevent some
1628 * kinds of access to the file. Currently, the following seals are
1630 * SEAL_SEAL: Prevent further seals from being set on this file
1631 * SEAL_SHRINK: Prevent the file from shrinking
1632 * SEAL_GROW: Prevent the file from growing
1633 * SEAL_WRITE: Prevent write access to the file
1635 * As we don't require any trust relationship between two parties, we
1636 * must prevent seals from being removed. Therefore, sealing a file
1637 * only adds a given set of seals to the file, it never touches
1638 * existing seals. Furthermore, the "setting seals"-operation can be
1639 * sealed itself, which basically prevents any further seal from being
1642 * Semantics of sealing are only defined on volatile files. Only
1643 * anonymous shmem files support sealing. More importantly, seals are
1644 * never written to disk. Therefore, there's no plan to support it on
1648 if (file->f_op != &shmem_file_operations)
1650 if (!(file->f_mode & FMODE_WRITE))
1652 if (seals & ~(unsigned int)F_ALL_SEALS)
1655 mutex_lock(&inode->i_mutex);
1657 if (info->seals & F_SEAL_SEAL) {
1662 if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
1663 error = mapping_deny_writable(file->f_mapping);
1667 error = shmem_wait_for_pins(file->f_mapping);
1669 mapping_allow_writable(file->f_mapping);
1674 info->seals |= seals;
1678 mutex_unlock(&inode->i_mutex);
1681 EXPORT_SYMBOL_GPL(shmem_add_seals);
1683 int shmem_get_seals(struct file *file)
1685 if (file->f_op != &shmem_file_operations)
1688 return SHMEM_I(file_inode(file))->seals;
1690 EXPORT_SYMBOL_GPL(shmem_get_seals);
1692 long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1698 /* disallow upper 32bit */
1702 error = shmem_add_seals(file, arg);
1705 error = shmem_get_seals(file);
1715 static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1717 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1719 buf->f_type = TMPFS_MAGIC;
1720 buf->f_bsize = PAGE_CACHE_SIZE;
1721 buf->f_namelen = NAME_MAX;
1722 if (sbinfo->max_blocks) {
1723 buf->f_blocks = sbinfo->max_blocks;
1725 buf->f_bfree = sbinfo->max_blocks -
1726 percpu_counter_sum(&sbinfo->used_blocks);
1728 if (sbinfo->max_inodes) {
1729 buf->f_files = sbinfo->max_inodes;
1730 buf->f_ffree = sbinfo->free_inodes;
1732 /* else leave those fields 0 like simple_statfs */
1737 * File creation. Allocate an inode, and we're done..
1740 shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1742 struct inode *inode;
1743 int error = -ENOSPC;
1745 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
1747 error = security_inode_init_security(inode, dir,
1751 if (error != -EOPNOTSUPP) {
1756 #ifdef CONFIG_TMPFS_POSIX_ACL
1757 error = generic_acl_init(inode, dir);
1765 dir->i_size += BOGO_DIRENT_SIZE;
1766 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1767 d_instantiate(dentry, inode);
1768 dget(dentry); /* Extra count - pin the dentry in core */
1773 static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1777 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1783 static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1784 struct nameidata *nd)
1786 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1792 static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1794 struct inode *inode = old_dentry->d_inode;
1798 * No ordinary (disk based) filesystem counts links as inodes;
1799 * but each new link needs a new dentry, pinning lowmem, and
1800 * tmpfs dentries cannot be pruned until they are unlinked.
1802 ret = shmem_reserve_inode(inode->i_sb);
1806 dir->i_size += BOGO_DIRENT_SIZE;
1807 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1809 ihold(inode); /* New dentry reference */
1810 dget(dentry); /* Extra pinning count for the created dentry */
1811 d_instantiate(dentry, inode);
1816 static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1818 struct inode *inode = dentry->d_inode;
1820 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1821 shmem_free_inode(inode->i_sb);
1823 dir->i_size -= BOGO_DIRENT_SIZE;
1824 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1826 dput(dentry); /* Undo the count from "create" - this does all the work */
1830 static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1832 if (!simple_empty(dentry))
1835 drop_nlink(dentry->d_inode);
1837 return shmem_unlink(dir, dentry);
1841 * The VFS layer already does all the dentry stuff for rename,
1842 * we just have to decrement the usage count for the target if
1843 * it exists so that the VFS layer correctly free's it when it
1846 static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1848 struct inode *inode = old_dentry->d_inode;
1849 int they_are_dirs = S_ISDIR(inode->i_mode);
1851 if (!simple_empty(new_dentry))
1854 if (new_dentry->d_inode) {
1855 (void) shmem_unlink(new_dir, new_dentry);
1856 if (they_are_dirs) {
1857 drop_nlink(new_dentry->d_inode);
1858 drop_nlink(old_dir);
1860 } else if (they_are_dirs) {
1861 drop_nlink(old_dir);
1865 old_dir->i_size -= BOGO_DIRENT_SIZE;
1866 new_dir->i_size += BOGO_DIRENT_SIZE;
1867 old_dir->i_ctime = old_dir->i_mtime =
1868 new_dir->i_ctime = new_dir->i_mtime =
1869 inode->i_ctime = CURRENT_TIME;
1873 static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1877 struct inode *inode;
1880 struct shmem_inode_info *info;
1882 len = strlen(symname) + 1;
1883 if (len > PAGE_CACHE_SIZE)
1884 return -ENAMETOOLONG;
1886 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
1890 error = security_inode_init_security(inode, dir, &dentry->d_name,
1893 if (error != -EOPNOTSUPP) {
1900 info = SHMEM_I(inode);
1901 inode->i_size = len-1;
1902 if (len <= SHORT_SYMLINK_LEN) {
1903 info->symlink = kmemdup(symname, len, GFP_KERNEL);
1904 if (!info->symlink) {
1908 inode->i_op = &shmem_short_symlink_operations;
1910 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1915 inode->i_mapping->a_ops = &shmem_aops;
1916 inode->i_op = &shmem_symlink_inode_operations;
1917 kaddr = kmap_atomic(page, KM_USER0);
1918 memcpy(kaddr, symname, len);
1919 kunmap_atomic(kaddr, KM_USER0);
1920 set_page_dirty(page);
1922 page_cache_release(page);
1924 dir->i_size += BOGO_DIRENT_SIZE;
1925 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1926 d_instantiate(dentry, inode);
1931 static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
1933 nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
1937 static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1939 struct page *page = NULL;
1940 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1941 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
1947 static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1949 if (!IS_ERR(nd_get_link(nd))) {
1950 struct page *page = cookie;
1952 mark_page_accessed(page);
1953 page_cache_release(page);
1957 #ifdef CONFIG_TMPFS_XATTR
1959 * Superblocks without xattr inode operations may get some security.* xattr
1960 * support from the LSM "for free". As soon as we have any other xattrs
1961 * like ACLs, we also need to implement the security.* handlers at
1962 * filesystem level, though.
1965 static int shmem_xattr_get(struct dentry *dentry, const char *name,
1966 void *buffer, size_t size)
1968 struct shmem_inode_info *info;
1969 struct shmem_xattr *xattr;
1972 info = SHMEM_I(dentry->d_inode);
1974 spin_lock(&info->lock);
1975 list_for_each_entry(xattr, &info->xattr_list, list) {
1976 if (strcmp(name, xattr->name))
1981 if (size < xattr->size)
1984 memcpy(buffer, xattr->value, xattr->size);
1988 spin_unlock(&info->lock);
1992 static int shmem_xattr_set(struct dentry *dentry, const char *name,
1993 const void *value, size_t size, int flags)
1995 struct inode *inode = dentry->d_inode;
1996 struct shmem_inode_info *info = SHMEM_I(inode);
1997 struct shmem_xattr *xattr;
1998 struct shmem_xattr *new_xattr = NULL;
2002 /* value == NULL means remove */
2005 len = sizeof(*new_xattr) + size;
2006 if (len <= sizeof(*new_xattr))
2009 new_xattr = kmalloc(len, GFP_KERNEL);
2013 new_xattr->name = kstrdup(name, GFP_KERNEL);
2014 if (!new_xattr->name) {
2019 new_xattr->size = size;
2020 memcpy(new_xattr->value, value, size);
2023 spin_lock(&info->lock);
2024 list_for_each_entry(xattr, &info->xattr_list, list) {
2025 if (!strcmp(name, xattr->name)) {
2026 if (flags & XATTR_CREATE) {
2029 } else if (new_xattr) {
2030 list_replace(&xattr->list, &new_xattr->list);
2032 list_del(&xattr->list);
2037 if (flags & XATTR_REPLACE) {
2041 list_add(&new_xattr->list, &info->xattr_list);
2045 spin_unlock(&info->lock);
2052 static const struct xattr_handler *shmem_xattr_handlers[] = {
2053 #ifdef CONFIG_TMPFS_POSIX_ACL
2054 &generic_acl_access_handler,
2055 &generic_acl_default_handler,
2060 static int shmem_xattr_validate(const char *name)
2062 struct { const char *prefix; size_t len; } arr[] = {
2063 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
2064 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
2068 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2069 size_t preflen = arr[i].len;
2070 if (strncmp(name, arr[i].prefix, preflen) == 0) {
2079 static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
2080 void *buffer, size_t size)
2085 * If this is a request for a synthetic attribute in the system.*
2086 * namespace use the generic infrastructure to resolve a handler
2087 * for it via sb->s_xattr.
2089 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2090 return generic_getxattr(dentry, name, buffer, size);
2092 err = shmem_xattr_validate(name);
2096 return shmem_xattr_get(dentry, name, buffer, size);
2099 static int shmem_setxattr(struct dentry *dentry, const char *name,
2100 const void *value, size_t size, int flags)
2105 * If this is a request for a synthetic attribute in the system.*
2106 * namespace use the generic infrastructure to resolve a handler
2107 * for it via sb->s_xattr.
2109 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2110 return generic_setxattr(dentry, name, value, size, flags);
2112 err = shmem_xattr_validate(name);
2117 value = ""; /* empty EA, do not remove */
2119 return shmem_xattr_set(dentry, name, value, size, flags);
2123 static int shmem_removexattr(struct dentry *dentry, const char *name)
2128 * If this is a request for a synthetic attribute in the system.*
2129 * namespace use the generic infrastructure to resolve a handler
2130 * for it via sb->s_xattr.
2132 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2133 return generic_removexattr(dentry, name);
2135 err = shmem_xattr_validate(name);
2139 return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
2142 static bool xattr_is_trusted(const char *name)
2144 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
2147 static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2149 bool trusted = capable(CAP_SYS_ADMIN);
2150 struct shmem_xattr *xattr;
2151 struct shmem_inode_info *info;
2154 info = SHMEM_I(dentry->d_inode);
2156 spin_lock(&info->lock);
2157 list_for_each_entry(xattr, &info->xattr_list, list) {
2160 /* skip "trusted." attributes for unprivileged callers */
2161 if (!trusted && xattr_is_trusted(xattr->name))
2164 len = strlen(xattr->name) + 1;
2171 memcpy(buffer, xattr->name, len);
2175 spin_unlock(&info->lock);
2179 #endif /* CONFIG_TMPFS_XATTR */
2181 static const struct inode_operations shmem_short_symlink_operations = {
2182 .readlink = generic_readlink,
2183 .follow_link = shmem_follow_short_symlink,
2184 #ifdef CONFIG_TMPFS_XATTR
2185 .setxattr = shmem_setxattr,
2186 .getxattr = shmem_getxattr,
2187 .listxattr = shmem_listxattr,
2188 .removexattr = shmem_removexattr,
2192 static const struct inode_operations shmem_symlink_inode_operations = {
2193 .readlink = generic_readlink,
2194 .follow_link = shmem_follow_link,
2195 .put_link = shmem_put_link,
2196 #ifdef CONFIG_TMPFS_XATTR
2197 .setxattr = shmem_setxattr,
2198 .getxattr = shmem_getxattr,
2199 .listxattr = shmem_listxattr,
2200 .removexattr = shmem_removexattr,
2204 static struct dentry *shmem_get_parent(struct dentry *child)
2206 return ERR_PTR(-ESTALE);
2209 static int shmem_match(struct inode *ino, void *vfh)
2213 inum = (inum << 32) | fh[1];
2214 return ino->i_ino == inum && fh[0] == ino->i_generation;
2217 static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2218 struct fid *fid, int fh_len, int fh_type)
2220 struct inode *inode;
2221 struct dentry *dentry = NULL;
2228 inum = (inum << 32) | fid->raw[1];
2230 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2231 shmem_match, fid->raw);
2233 dentry = d_find_alias(inode);
2240 static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2243 struct inode *inode = dentry->d_inode;
2250 if (inode_unhashed(inode)) {
2251 /* Unfortunately insert_inode_hash is not idempotent,
2252 * so as we hash inodes here rather than at creation
2253 * time, we need a lock to ensure we only try
2256 static DEFINE_SPINLOCK(lock);
2258 if (inode_unhashed(inode))
2259 __insert_inode_hash(inode,
2260 inode->i_ino + inode->i_generation);
2264 fh[0] = inode->i_generation;
2265 fh[1] = inode->i_ino;
2266 fh[2] = ((__u64)inode->i_ino) >> 32;
2272 static const struct export_operations shmem_export_ops = {
2273 .get_parent = shmem_get_parent,
2274 .encode_fh = shmem_encode_fh,
2275 .fh_to_dentry = shmem_fh_to_dentry,
2278 static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2281 char *this_char, *value, *rest;
2283 while (options != NULL) {
2284 this_char = options;
2287 * NUL-terminate this option: unfortunately,
2288 * mount options form a comma-separated list,
2289 * but mpol's nodelist may also contain commas.
2291 options = strchr(options, ',');
2292 if (options == NULL)
2295 if (!isdigit(*options)) {
2302 if ((value = strchr(this_char,'=')) != NULL) {
2306 "tmpfs: No value for mount option '%s'\n",
2311 if (!strcmp(this_char,"size")) {
2312 unsigned long long size;
2313 size = memparse(value,&rest);
2315 size <<= PAGE_SHIFT;
2316 size *= totalram_pages;
2322 sbinfo->max_blocks =
2323 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
2324 } else if (!strcmp(this_char,"nr_blocks")) {
2325 sbinfo->max_blocks = memparse(value, &rest);
2328 } else if (!strcmp(this_char,"nr_inodes")) {
2329 sbinfo->max_inodes = memparse(value, &rest);
2332 } else if (!strcmp(this_char,"mode")) {
2335 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
2338 } else if (!strcmp(this_char,"uid")) {
2341 sbinfo->uid = simple_strtoul(value, &rest, 0);
2344 } else if (!strcmp(this_char,"gid")) {
2347 sbinfo->gid = simple_strtoul(value, &rest, 0);
2350 } else if (!strcmp(this_char,"mpol")) {
2351 if (mpol_parse_str(value, &sbinfo->mpol, 1))
2354 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2362 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2368 static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2370 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2371 struct shmem_sb_info config = *sbinfo;
2372 unsigned long inodes;
2373 int error = -EINVAL;
2376 if (shmem_parse_options(data, &config, true))
2379 spin_lock(&sbinfo->stat_lock);
2380 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
2381 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
2383 if (config.max_inodes < inodes)
2386 * Those tests disallow limited->unlimited while any are in use;
2387 * but we must separately disallow unlimited->limited, because
2388 * in that case we have no record of how much is already in use.
2390 if (config.max_blocks && !sbinfo->max_blocks)
2392 if (config.max_inodes && !sbinfo->max_inodes)
2396 sbinfo->max_blocks = config.max_blocks;
2397 sbinfo->max_inodes = config.max_inodes;
2398 sbinfo->free_inodes = config.max_inodes - inodes;
2401 * Preserve previous mempolicy unless mpol remount option was specified.
2404 mpol_put(sbinfo->mpol);
2405 sbinfo->mpol = config.mpol; /* transfers initial ref */
2408 spin_unlock(&sbinfo->stat_lock);
2412 static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2414 struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2416 if (sbinfo->max_blocks != shmem_default_max_blocks())
2417 seq_printf(seq, ",size=%luk",
2418 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2419 if (sbinfo->max_inodes != shmem_default_max_inodes())
2420 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2421 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2422 seq_printf(seq, ",mode=%03o", sbinfo->mode);
2423 if (sbinfo->uid != 0)
2424 seq_printf(seq, ",uid=%u", sbinfo->uid);
2425 if (sbinfo->gid != 0)
2426 seq_printf(seq, ",gid=%u", sbinfo->gid);
2427 shmem_show_mpol(seq, sbinfo->mpol);
2431 #define MFD_NAME_PREFIX "memfd:"
2432 #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
2433 #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
2435 #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
2437 SYSCALL_DEFINE2(memfd_create,
2438 const char __user *, uname,
2439 unsigned int, flags)
2441 struct shmem_inode_info *info;
2447 if (flags & ~(unsigned int)MFD_ALL_FLAGS)
2450 /* length includes terminating zero */
2451 len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
2454 if (len > MFD_NAME_MAX_LEN + 1)
2457 name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
2461 strcpy(name, MFD_NAME_PREFIX);
2462 if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
2467 /* terminating-zero may have changed after strnlen_user() returned */
2468 if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
2473 fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
2479 file = shmem_file_setup(name, 0, VM_NORESERVE);
2481 error = PTR_ERR(file);
2484 info = SHMEM_I(file_inode(file));
2485 file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
2486 file->f_flags |= O_RDWR | O_LARGEFILE;
2487 if (flags & MFD_ALLOW_SEALING)
2488 info->seals &= ~F_SEAL_SEAL;
2490 fd_install(fd, file);
2501 #endif /* CONFIG_TMPFS */
2503 static void shmem_put_super(struct super_block *sb)
2505 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2507 percpu_counter_destroy(&sbinfo->used_blocks);
2509 sb->s_fs_info = NULL;
2512 int shmem_fill_super(struct super_block *sb, void *data, int silent)
2514 struct inode *inode;
2515 struct dentry *root;
2516 struct shmem_sb_info *sbinfo;
2519 /* Round up to L1_CACHE_BYTES to resist false sharing */
2520 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
2521 L1_CACHE_BYTES), GFP_KERNEL);
2525 sbinfo->mode = S_IRWXUGO | S_ISVTX;
2526 sbinfo->uid = current_fsuid();
2527 sbinfo->gid = current_fsgid();
2528 sb->s_fs_info = sbinfo;
2532 * Per default we only allow half of the physical ram per
2533 * tmpfs instance, limiting inodes to one per page of lowmem;
2534 * but the internal instance is left unlimited.
2536 if (!(sb->s_flags & MS_NOUSER)) {
2537 sbinfo->max_blocks = shmem_default_max_blocks();
2538 sbinfo->max_inodes = shmem_default_max_inodes();
2539 if (shmem_parse_options(data, sbinfo, false)) {
2544 sb->s_export_op = &shmem_export_ops;
2546 sb->s_flags |= MS_NOUSER;
2549 spin_lock_init(&sbinfo->stat_lock);
2550 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2552 sbinfo->free_inodes = sbinfo->max_inodes;
2554 sb->s_maxbytes = MAX_LFS_FILESIZE;
2555 sb->s_blocksize = PAGE_CACHE_SIZE;
2556 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2557 sb->s_magic = TMPFS_MAGIC;
2558 sb->s_op = &shmem_ops;
2559 sb->s_time_gran = 1;
2560 #ifdef CONFIG_TMPFS_XATTR
2561 sb->s_xattr = shmem_xattr_handlers;
2563 #ifdef CONFIG_TMPFS_POSIX_ACL
2564 sb->s_flags |= MS_POSIXACL;
2567 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
2570 inode->i_uid = sbinfo->uid;
2571 inode->i_gid = sbinfo->gid;
2572 root = d_alloc_root(inode);
2581 shmem_put_super(sb);
2585 static struct kmem_cache *shmem_inode_cachep;
2587 static struct inode *shmem_alloc_inode(struct super_block *sb)
2589 struct shmem_inode_info *info;
2590 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2593 return &info->vfs_inode;
2596 static void shmem_destroy_callback(struct rcu_head *head)
2598 struct inode *inode = container_of(head, struct inode, i_rcu);
2599 INIT_LIST_HEAD(&inode->i_dentry);
2600 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2603 static void shmem_destroy_inode(struct inode *inode)
2605 if ((inode->i_mode & S_IFMT) == S_IFREG)
2606 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2607 call_rcu(&inode->i_rcu, shmem_destroy_callback);
2610 static void shmem_init_inode(void *foo)
2612 struct shmem_inode_info *info = foo;
2613 inode_init_once(&info->vfs_inode);
2616 static int shmem_init_inodecache(void)
2618 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2619 sizeof(struct shmem_inode_info),
2620 0, SLAB_PANIC, shmem_init_inode);
2624 static void shmem_destroy_inodecache(void)
2626 kmem_cache_destroy(shmem_inode_cachep);
2629 static const struct address_space_operations shmem_aops = {
2630 .writepage = shmem_writepage,
2631 .set_page_dirty = __set_page_dirty_no_writeback,
2633 .write_begin = shmem_write_begin,
2634 .write_end = shmem_write_end,
2636 .migratepage = migrate_page,
2637 .error_remove_page = generic_error_remove_page,
2640 static const struct file_operations shmem_file_operations = {
2643 .llseek = generic_file_llseek,
2644 .read = do_sync_read,
2645 .write = do_sync_write,
2646 .aio_read = shmem_file_aio_read,
2647 .aio_write = generic_file_aio_write,
2648 .fsync = noop_fsync,
2649 .splice_read = shmem_file_splice_read,
2650 .splice_write = generic_file_splice_write,
2654 static const struct inode_operations shmem_inode_operations = {
2655 .setattr = shmem_setattr,
2656 .truncate_range = shmem_truncate_range,
2657 #ifdef CONFIG_TMPFS_XATTR
2658 .setxattr = shmem_setxattr,
2659 .getxattr = shmem_getxattr,
2660 .listxattr = shmem_listxattr,
2661 .removexattr = shmem_removexattr,
2665 static const struct inode_operations shmem_dir_inode_operations = {
2667 .create = shmem_create,
2668 .lookup = simple_lookup,
2670 .unlink = shmem_unlink,
2671 .symlink = shmem_symlink,
2672 .mkdir = shmem_mkdir,
2673 .rmdir = shmem_rmdir,
2674 .mknod = shmem_mknod,
2675 .rename = shmem_rename,
2677 #ifdef CONFIG_TMPFS_XATTR
2678 .setxattr = shmem_setxattr,
2679 .getxattr = shmem_getxattr,
2680 .listxattr = shmem_listxattr,
2681 .removexattr = shmem_removexattr,
2683 #ifdef CONFIG_TMPFS_POSIX_ACL
2684 .setattr = shmem_setattr,
2688 static const struct inode_operations shmem_special_inode_operations = {
2689 #ifdef CONFIG_TMPFS_XATTR
2690 .setxattr = shmem_setxattr,
2691 .getxattr = shmem_getxattr,
2692 .listxattr = shmem_listxattr,
2693 .removexattr = shmem_removexattr,
2695 #ifdef CONFIG_TMPFS_POSIX_ACL
2696 .setattr = shmem_setattr,
2700 static const struct super_operations shmem_ops = {
2701 .alloc_inode = shmem_alloc_inode,
2702 .destroy_inode = shmem_destroy_inode,
2704 .statfs = shmem_statfs,
2705 .remount_fs = shmem_remount_fs,
2706 .show_options = shmem_show_options,
2708 .evict_inode = shmem_evict_inode,
2709 .drop_inode = generic_delete_inode,
2710 .put_super = shmem_put_super,
2713 static const struct vm_operations_struct shmem_vm_ops = {
2714 .fault = shmem_fault,
2716 .set_policy = shmem_set_policy,
2717 .get_policy = shmem_get_policy,
2721 static struct dentry *shmem_mount(struct file_system_type *fs_type,
2722 int flags, const char *dev_name, void *data)
2724 return mount_nodev(fs_type, flags, data, shmem_fill_super);
2727 static struct file_system_type shmem_fs_type = {
2728 .owner = THIS_MODULE,
2730 .mount = shmem_mount,
2731 .kill_sb = kill_litter_super,
2734 int __init shmem_init(void)
2738 error = bdi_init(&shmem_backing_dev_info);
2742 error = shmem_init_inodecache();
2746 error = register_filesystem(&shmem_fs_type);
2748 printk(KERN_ERR "Could not register tmpfs\n");
2752 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2753 shmem_fs_type.name, NULL);
2754 if (IS_ERR(shm_mnt)) {
2755 error = PTR_ERR(shm_mnt);
2756 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2762 unregister_filesystem(&shmem_fs_type);
2764 shmem_destroy_inodecache();
2766 bdi_destroy(&shmem_backing_dev_info);
2768 shm_mnt = ERR_PTR(error);
2772 #else /* !CONFIG_SHMEM */
2775 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2777 * This is intended for small system where the benefits of the full
2778 * shmem code (swap-backed and resource-limited) are outweighed by
2779 * their complexity. On systems without swap this code should be
2780 * effectively equivalent, but much lighter weight.
2783 #include <linux/ramfs.h>
2785 static struct file_system_type shmem_fs_type = {
2787 .mount = ramfs_mount,
2788 .kill_sb = kill_litter_super,
2791 int __init shmem_init(void)
2793 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
2795 shm_mnt = kern_mount(&shmem_fs_type);
2796 BUG_ON(IS_ERR(shm_mnt));
2801 int shmem_unuse(swp_entry_t swap, struct page *page)
2806 int shmem_lock(struct file *file, int lock, struct user_struct *user)
2811 void shmem_unlock_mapping(struct address_space *mapping)
2815 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
2817 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
2819 EXPORT_SYMBOL_GPL(shmem_truncate_range);
2821 int vmtruncate_range(struct inode *inode, loff_t lstart, loff_t lend)
2823 /* Only CONFIG_SHMEM shmem.c ever supported i_op->truncate_range(). */
2827 #define shmem_vm_ops generic_file_vm_ops
2828 #define shmem_file_operations ramfs_file_operations
2829 #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
2830 #define shmem_acct_size(flags, size) 0
2831 #define shmem_unacct_size(flags, size) do {} while (0)
2833 #endif /* CONFIG_SHMEM */
2838 * shmem_file_setup - get an unlinked file living in tmpfs
2839 * @name: name for dentry (to be seen in /proc/<pid>/maps
2840 * @size: size to be set for the file
2841 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
2843 struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
2847 struct inode *inode;
2849 struct dentry *root;
2852 if (IS_ERR(shm_mnt))
2853 return (void *)shm_mnt;
2855 if (size < 0 || size > MAX_LFS_FILESIZE)
2856 return ERR_PTR(-EINVAL);
2858 if (shmem_acct_size(flags, size))
2859 return ERR_PTR(-ENOMEM);
2863 this.len = strlen(name);
2864 this.hash = 0; /* will go */
2865 root = shm_mnt->mnt_root;
2866 path.dentry = d_alloc(root, &this);
2869 path.mnt = mntget(shm_mnt);
2872 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
2876 d_instantiate(path.dentry, inode);
2877 inode->i_size = size;
2878 clear_nlink(inode); /* It is unlinked */
2880 error = ramfs_nommu_expand_for_mapping(inode, size);
2886 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
2887 &shmem_file_operations);
2896 shmem_unacct_size(flags, size);
2897 return ERR_PTR(error);
2899 EXPORT_SYMBOL_GPL(shmem_file_setup);
2902 * shmem_zero_setup - setup a shared anonymous mapping
2903 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2905 int shmem_zero_setup(struct vm_area_struct *vma)
2908 loff_t size = vma->vm_end - vma->vm_start;
2910 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2912 return PTR_ERR(file);
2916 vma->vm_file = file;
2917 vma->vm_ops = &shmem_vm_ops;
2918 vma->vm_flags |= VM_CAN_NONLINEAR;
2921 EXPORT_SYMBOL_GPL(shmem_zero_setup);
2924 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2925 * @mapping: the page's address_space
2926 * @index: the page index
2927 * @gfp: the page allocator flags to use if allocating
2929 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2930 * with any new page allocations done using the specified allocation flags.
2931 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2932 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2933 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2935 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2936 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
2938 struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2939 pgoff_t index, gfp_t gfp)
2942 struct inode *inode = mapping->host;
2946 BUG_ON(mapping->a_ops != &shmem_aops);
2947 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2949 page = ERR_PTR(error);
2955 * The tiny !SHMEM case uses ramfs without swap
2957 return read_cache_page_gfp(mapping, index, gfp);
2960 EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);