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/falloc.h>
57 #include <linux/splice.h>
58 #include <linux/security.h>
59 #include <linux/swapops.h>
60 #include <linux/mempolicy.h>
61 #include <linux/namei.h>
62 #include <linux/ctype.h>
63 #include <linux/migrate.h>
64 #include <linux/highmem.h>
65 #include <linux/seq_file.h>
66 #include <linux/magic.h>
68 #include <asm/uaccess.h>
69 #include <asm/pgtable.h>
71 #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
72 #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
74 /* Pretend that each entry is of this size in directory's i_size */
75 #define BOGO_DIRENT_SIZE 20
77 /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
78 #define SHORT_SYMLINK_LEN 128
81 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
82 char *name; /* xattr name */
87 /* Flag allocation requirements to shmem_getpage */
89 SGP_READ, /* don't exceed i_size, don't allocate page */
90 SGP_CACHE, /* don't exceed i_size, may allocate page */
91 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
92 SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
93 SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
97 static unsigned long shmem_default_max_blocks(void)
99 return totalram_pages / 2;
102 static unsigned long shmem_default_max_inodes(void)
104 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
108 static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
109 static int shmem_replace_page(struct page **pagep, gfp_t gfp,
110 struct shmem_inode_info *info, pgoff_t index);
111 static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
112 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
114 static inline int shmem_getpage(struct inode *inode, pgoff_t index,
115 struct page **pagep, enum sgp_type sgp, int *fault_type)
117 return shmem_getpage_gfp(inode, index, pagep, sgp,
118 mapping_gfp_mask(inode->i_mapping), fault_type);
121 static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
123 return sb->s_fs_info;
127 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
128 * for shared memory and for shared anonymous (/dev/zero) mappings
129 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
130 * consistent with the pre-accounting of private mappings ...
132 static inline int shmem_acct_size(unsigned long flags, loff_t size)
134 return (flags & VM_NORESERVE) ?
135 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
138 static inline void shmem_unacct_size(unsigned long flags, loff_t size)
140 if (!(flags & VM_NORESERVE))
141 vm_unacct_memory(VM_ACCT(size));
145 * ... whereas tmpfs objects are accounted incrementally as
146 * pages are allocated, in order to allow huge sparse files.
147 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
148 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
150 static inline int shmem_acct_block(unsigned long flags)
152 return (flags & VM_NORESERVE) ?
153 security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
156 static inline void shmem_unacct_blocks(unsigned long flags, long pages)
158 if (flags & VM_NORESERVE)
159 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
162 static const struct super_operations shmem_ops;
163 static const struct address_space_operations shmem_aops;
164 static const struct file_operations shmem_file_operations;
165 static const struct inode_operations shmem_inode_operations;
166 static const struct inode_operations shmem_dir_inode_operations;
167 static const struct inode_operations shmem_special_inode_operations;
168 static const struct vm_operations_struct shmem_vm_ops;
170 static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
171 .ra_pages = 0, /* No readahead */
172 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
175 static LIST_HEAD(shmem_swaplist);
176 static DEFINE_MUTEX(shmem_swaplist_mutex);
178 static int shmem_reserve_inode(struct super_block *sb)
180 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
181 if (sbinfo->max_inodes) {
182 spin_lock(&sbinfo->stat_lock);
183 if (!sbinfo->free_inodes) {
184 spin_unlock(&sbinfo->stat_lock);
187 sbinfo->free_inodes--;
188 spin_unlock(&sbinfo->stat_lock);
193 static void shmem_free_inode(struct super_block *sb)
195 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
196 if (sbinfo->max_inodes) {
197 spin_lock(&sbinfo->stat_lock);
198 sbinfo->free_inodes++;
199 spin_unlock(&sbinfo->stat_lock);
204 * shmem_recalc_inode - recalculate the block usage of an inode
205 * @inode: inode to recalc
207 * We have to calculate the free blocks since the mm can drop
208 * undirtied hole pages behind our back.
210 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
211 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
213 * It has to be called with the spinlock held.
215 static void shmem_recalc_inode(struct inode *inode)
217 struct shmem_inode_info *info = SHMEM_I(inode);
220 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
222 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
223 if (sbinfo->max_blocks)
224 percpu_counter_add(&sbinfo->used_blocks, -freed);
225 info->alloced -= freed;
226 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
227 shmem_unacct_blocks(info->flags, freed);
232 * Replace item expected in radix tree by a new item, while holding tree lock.
234 static int shmem_radix_tree_replace(struct address_space *mapping,
235 pgoff_t index, void *expected, void *replacement)
240 VM_BUG_ON(!expected);
241 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
243 item = radix_tree_deref_slot_protected(pslot,
244 &mapping->tree_lock);
245 if (item != expected)
248 radix_tree_replace_slot(pslot, replacement);
250 radix_tree_delete(&mapping->page_tree, index);
255 * Like add_to_page_cache_locked, but error if expected item has gone.
257 static int shmem_add_to_page_cache(struct page *page,
258 struct address_space *mapping,
259 pgoff_t index, gfp_t gfp, void *expected)
263 VM_BUG_ON(!PageLocked(page));
264 VM_BUG_ON(!PageSwapBacked(page));
267 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
269 page_cache_get(page);
270 page->mapping = mapping;
273 spin_lock_irq(&mapping->tree_lock);
275 error = radix_tree_insert(&mapping->page_tree,
278 error = shmem_radix_tree_replace(mapping, index,
282 __inc_zone_page_state(page, NR_FILE_PAGES);
283 __inc_zone_page_state(page, NR_SHMEM);
284 spin_unlock_irq(&mapping->tree_lock);
286 page->mapping = NULL;
287 spin_unlock_irq(&mapping->tree_lock);
288 page_cache_release(page);
291 radix_tree_preload_end();
294 mem_cgroup_uncharge_cache_page(page);
299 * Like delete_from_page_cache, but substitutes swap for page.
301 static void shmem_delete_from_page_cache(struct page *page, void *radswap)
303 struct address_space *mapping = page->mapping;
306 spin_lock_irq(&mapping->tree_lock);
307 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
308 page->mapping = NULL;
310 __dec_zone_page_state(page, NR_FILE_PAGES);
311 __dec_zone_page_state(page, NR_SHMEM);
312 spin_unlock_irq(&mapping->tree_lock);
313 page_cache_release(page);
318 * Like find_get_pages, but collecting swap entries as well as pages.
320 static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
321 pgoff_t start, unsigned int nr_pages,
322 struct page **pages, pgoff_t *indices)
326 unsigned int nr_found;
330 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
331 (void ***)pages, indices, start, nr_pages);
333 for (i = 0; i < nr_found; i++) {
336 page = radix_tree_deref_slot((void **)pages[i]);
339 if (radix_tree_exception(page)) {
340 if (radix_tree_deref_retry(page))
343 * Otherwise, we must be storing a swap entry
344 * here as an exceptional entry: so return it
345 * without attempting to raise page count.
349 if (!page_cache_get_speculative(page))
352 /* Has the page moved? */
353 if (unlikely(page != *((void **)pages[i]))) {
354 page_cache_release(page);
358 indices[ret] = indices[i];
362 if (unlikely(!ret && nr_found))
369 * Remove swap entry from radix tree, free the swap and its page cache.
371 static int shmem_free_swap(struct address_space *mapping,
372 pgoff_t index, void *radswap)
376 spin_lock_irq(&mapping->tree_lock);
377 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
378 spin_unlock_irq(&mapping->tree_lock);
380 free_swap_and_cache(radix_to_swp_entry(radswap));
385 * Pagevec may contain swap entries, so shuffle up pages before releasing.
387 static void shmem_deswap_pagevec(struct pagevec *pvec)
391 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
392 struct page *page = pvec->pages[i];
393 if (!radix_tree_exceptional_entry(page))
394 pvec->pages[j++] = page;
400 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
402 void shmem_unlock_mapping(struct address_space *mapping)
405 pgoff_t indices[PAGEVEC_SIZE];
408 pagevec_init(&pvec, 0);
410 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
412 while (!mapping_unevictable(mapping)) {
414 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
415 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
417 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
418 PAGEVEC_SIZE, pvec.pages, indices);
421 index = indices[pvec.nr - 1] + 1;
422 shmem_deswap_pagevec(&pvec);
423 check_move_unevictable_pages(pvec.pages, pvec.nr);
424 pagevec_release(&pvec);
430 * Remove range of pages and swap entries from radix tree, and free them.
431 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
433 static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
436 struct address_space *mapping = inode->i_mapping;
437 struct shmem_inode_info *info = SHMEM_I(inode);
438 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
439 pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
440 unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
441 unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
443 pgoff_t indices[PAGEVEC_SIZE];
444 long nr_swaps_freed = 0;
449 end = -1; /* unsigned, so actually very big */
451 pagevec_init(&pvec, 0);
453 while (index < end) {
454 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
455 min(end - index, (pgoff_t)PAGEVEC_SIZE),
456 pvec.pages, indices);
459 mem_cgroup_uncharge_start();
460 for (i = 0; i < pagevec_count(&pvec); i++) {
461 struct page *page = pvec.pages[i];
467 if (radix_tree_exceptional_entry(page)) {
470 nr_swaps_freed += !shmem_free_swap(mapping,
475 if (!trylock_page(page))
477 if (!unfalloc || !PageUptodate(page)) {
478 if (page->mapping == mapping) {
479 VM_BUG_ON(PageWriteback(page));
480 truncate_inode_page(mapping, page);
485 shmem_deswap_pagevec(&pvec);
486 pagevec_release(&pvec);
487 mem_cgroup_uncharge_end();
493 struct page *page = NULL;
494 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
496 unsigned int top = PAGE_CACHE_SIZE;
501 zero_user_segment(page, partial_start, top);
502 set_page_dirty(page);
504 page_cache_release(page);
508 struct page *page = NULL;
509 shmem_getpage(inode, end, &page, SGP_READ, NULL);
511 zero_user_segment(page, 0, partial_end);
512 set_page_dirty(page);
514 page_cache_release(page);
523 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
524 min(end - index, (pgoff_t)PAGEVEC_SIZE),
525 pvec.pages, indices);
527 if (index == start || unfalloc)
532 if ((index == start || unfalloc) && indices[0] >= end) {
533 shmem_deswap_pagevec(&pvec);
534 pagevec_release(&pvec);
537 mem_cgroup_uncharge_start();
538 for (i = 0; i < pagevec_count(&pvec); i++) {
539 struct page *page = pvec.pages[i];
545 if (radix_tree_exceptional_entry(page)) {
548 nr_swaps_freed += !shmem_free_swap(mapping,
554 if (!unfalloc || !PageUptodate(page)) {
555 if (page->mapping == mapping) {
556 VM_BUG_ON(PageWriteback(page));
557 truncate_inode_page(mapping, page);
562 shmem_deswap_pagevec(&pvec);
563 pagevec_release(&pvec);
564 mem_cgroup_uncharge_end();
568 spin_lock(&info->lock);
569 info->swapped -= nr_swaps_freed;
570 shmem_recalc_inode(inode);
571 spin_unlock(&info->lock);
574 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
576 shmem_undo_range(inode, lstart, lend, false);
577 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
579 EXPORT_SYMBOL_GPL(shmem_truncate_range);
581 static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
583 struct inode *inode = dentry->d_inode;
586 error = inode_change_ok(inode, attr);
590 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
591 loff_t oldsize = inode->i_size;
592 loff_t newsize = attr->ia_size;
594 if (newsize != oldsize) {
595 i_size_write(inode, newsize);
596 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
598 if (newsize < oldsize) {
599 loff_t holebegin = round_up(newsize, PAGE_SIZE);
600 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
601 shmem_truncate_range(inode, newsize, (loff_t)-1);
602 /* unmap again to remove racily COWed private pages */
603 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
607 setattr_copy(inode, attr);
608 #ifdef CONFIG_TMPFS_POSIX_ACL
609 if (attr->ia_valid & ATTR_MODE)
610 error = generic_acl_chmod(inode);
615 static void shmem_evict_inode(struct inode *inode)
617 struct shmem_inode_info *info = SHMEM_I(inode);
618 struct shmem_xattr *xattr, *nxattr;
620 if (inode->i_mapping->a_ops == &shmem_aops) {
621 shmem_unacct_size(info->flags, inode->i_size);
623 shmem_truncate_range(inode, 0, (loff_t)-1);
624 if (!list_empty(&info->swaplist)) {
625 mutex_lock(&shmem_swaplist_mutex);
626 list_del_init(&info->swaplist);
627 mutex_unlock(&shmem_swaplist_mutex);
630 kfree(info->symlink);
632 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
636 BUG_ON(inode->i_blocks);
637 shmem_free_inode(inode->i_sb);
642 * If swap found in inode, free it and move page from swapcache to filecache.
644 static int shmem_unuse_inode(struct shmem_inode_info *info,
645 swp_entry_t swap, struct page **pagep)
647 struct address_space *mapping = info->vfs_inode.i_mapping;
653 radswap = swp_to_radix_entry(swap);
654 index = radix_tree_locate_item(&mapping->page_tree, radswap);
659 * Move _head_ to start search for next from here.
660 * But be careful: shmem_evict_inode checks list_empty without taking
661 * mutex, and there's an instant in list_move_tail when info->swaplist
662 * would appear empty, if it were the only one on shmem_swaplist.
664 if (shmem_swaplist.next != &info->swaplist)
665 list_move_tail(&shmem_swaplist, &info->swaplist);
667 gfp = mapping_gfp_mask(mapping);
668 if (shmem_should_replace_page(*pagep, gfp)) {
669 mutex_unlock(&shmem_swaplist_mutex);
670 error = shmem_replace_page(pagep, gfp, info, index);
671 mutex_lock(&shmem_swaplist_mutex);
673 * We needed to drop mutex to make that restrictive page
674 * allocation; but the inode might already be freed by now,
675 * and we cannot refer to inode or mapping or info to check.
676 * However, we do hold page lock on the PageSwapCache page,
677 * so can check if that still has our reference remaining.
679 if (!page_swapcount(*pagep))
684 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
685 * but also to hold up shmem_evict_inode(): so inode cannot be freed
686 * beneath us (pagelock doesn't help until the page is in pagecache).
689 error = shmem_add_to_page_cache(*pagep, mapping, index,
690 GFP_NOWAIT, radswap);
691 if (error != -ENOMEM) {
693 * Truncation and eviction use free_swap_and_cache(), which
694 * only does trylock page: if we raced, best clean up here.
696 delete_from_swap_cache(*pagep);
697 set_page_dirty(*pagep);
699 spin_lock(&info->lock);
701 spin_unlock(&info->lock);
704 error = 1; /* not an error, but entry was found */
710 * Search through swapped inodes to find and replace swap by page.
712 int shmem_unuse(swp_entry_t swap, struct page *page)
714 struct list_head *this, *next;
715 struct shmem_inode_info *info;
720 * There's a faint possibility that swap page was replaced before
721 * caller locked it: it will come back later with the right page.
723 if (unlikely(!PageSwapCache(page)))
727 * Charge page using GFP_KERNEL while we can wait, before taking
728 * the shmem_swaplist_mutex which might hold up shmem_writepage().
729 * Charged back to the user (not to caller) when swap account is used.
731 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
734 /* No radix_tree_preload: swap entry keeps a place for page in tree */
736 mutex_lock(&shmem_swaplist_mutex);
737 list_for_each_safe(this, next, &shmem_swaplist) {
738 info = list_entry(this, struct shmem_inode_info, swaplist);
740 found = shmem_unuse_inode(info, swap, &page);
742 list_del_init(&info->swaplist);
747 mutex_unlock(&shmem_swaplist_mutex);
753 page_cache_release(page);
758 * Move the page from the page cache to the swap cache.
760 static int shmem_writepage(struct page *page, struct writeback_control *wbc)
762 struct shmem_inode_info *info;
763 struct address_space *mapping;
768 BUG_ON(!PageLocked(page));
769 mapping = page->mapping;
771 inode = mapping->host;
772 info = SHMEM_I(inode);
773 if (info->flags & VM_LOCKED)
775 if (!total_swap_pages)
779 * shmem_backing_dev_info's capabilities prevent regular writeback or
780 * sync from ever calling shmem_writepage; but a stacking filesystem
781 * might use ->writepage of its underlying filesystem, in which case
782 * tmpfs should write out to swap only in response to memory pressure,
783 * and not for the writeback threads or sync.
785 if (!wbc->for_reclaim) {
786 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
791 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
792 * value into swapfile.c, the only way we can correctly account for a
793 * fallocated page arriving here is now to initialize it and write it.
795 if (!PageUptodate(page)) {
796 clear_highpage(page);
797 flush_dcache_page(page);
798 SetPageUptodate(page);
801 swap = get_swap_page();
806 * Add inode to shmem_unuse()'s list of swapped-out inodes,
807 * if it's not already there. Do it now before the page is
808 * moved to swap cache, when its pagelock no longer protects
809 * the inode from eviction. But don't unlock the mutex until
810 * we've incremented swapped, because shmem_unuse_inode() will
811 * prune a !swapped inode from the swaplist under this mutex.
813 mutex_lock(&shmem_swaplist_mutex);
814 if (list_empty(&info->swaplist))
815 list_add_tail(&info->swaplist, &shmem_swaplist);
817 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
818 swap_shmem_alloc(swap);
819 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
821 spin_lock(&info->lock);
823 shmem_recalc_inode(inode);
824 spin_unlock(&info->lock);
826 mutex_unlock(&shmem_swaplist_mutex);
827 BUG_ON(page_mapped(page));
828 swap_writepage(page, wbc);
832 mutex_unlock(&shmem_swaplist_mutex);
833 swapcache_free(swap, NULL);
835 set_page_dirty(page);
836 if (wbc->for_reclaim)
837 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
844 static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
848 if (!mpol || mpol->mode == MPOL_DEFAULT)
849 return; /* show nothing */
851 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
853 seq_printf(seq, ",mpol=%s", buffer);
856 static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
858 struct mempolicy *mpol = NULL;
860 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
863 spin_unlock(&sbinfo->stat_lock);
867 #endif /* CONFIG_TMPFS */
869 static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
870 struct shmem_inode_info *info, pgoff_t index)
872 struct mempolicy mpol, *spol;
873 struct vm_area_struct pvma;
875 spol = mpol_cond_copy(&mpol,
876 mpol_shared_policy_lookup(&info->policy, index));
878 /* Create a pseudo vma that just contains the policy */
880 pvma.vm_pgoff = index;
882 pvma.vm_policy = spol;
883 return swapin_readahead(swap, gfp, &pvma, 0);
886 static struct page *shmem_alloc_page(gfp_t gfp,
887 struct shmem_inode_info *info, pgoff_t index)
889 struct vm_area_struct pvma;
891 /* Create a pseudo vma that just contains the policy */
893 pvma.vm_pgoff = index;
895 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
898 * alloc_page_vma() will drop the shared policy reference
900 return alloc_page_vma(gfp, &pvma, 0);
902 #else /* !CONFIG_NUMA */
904 static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
907 #endif /* CONFIG_TMPFS */
909 static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
910 struct shmem_inode_info *info, pgoff_t index)
912 return swapin_readahead(swap, gfp, NULL, 0);
915 static inline struct page *shmem_alloc_page(gfp_t gfp,
916 struct shmem_inode_info *info, pgoff_t index)
918 return alloc_page(gfp);
920 #endif /* CONFIG_NUMA */
922 #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
923 static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
930 * When a page is moved from swapcache to shmem filecache (either by the
931 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
932 * shmem_unuse_inode()), it may have been read in earlier from swap, in
933 * ignorance of the mapping it belongs to. If that mapping has special
934 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
935 * we may need to copy to a suitable page before moving to filecache.
937 * In a future release, this may well be extended to respect cpuset and
938 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
939 * but for now it is a simple matter of zone.
941 static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
943 return page_zonenum(page) > gfp_zone(gfp);
946 static int shmem_replace_page(struct page **pagep, gfp_t gfp,
947 struct shmem_inode_info *info, pgoff_t index)
949 struct page *oldpage, *newpage;
950 struct address_space *swap_mapping;
955 swap_index = page_private(oldpage);
956 swap_mapping = page_mapping(oldpage);
959 * We have arrived here because our zones are constrained, so don't
960 * limit chance of success by further cpuset and node constraints.
962 gfp &= ~GFP_CONSTRAINT_MASK;
963 newpage = shmem_alloc_page(gfp, info, index);
966 VM_BUG_ON(shmem_should_replace_page(newpage, gfp));
969 page_cache_get(newpage);
970 copy_highpage(newpage, oldpage);
972 VM_BUG_ON(!PageLocked(oldpage));
973 __set_page_locked(newpage);
974 VM_BUG_ON(!PageUptodate(oldpage));
975 SetPageUptodate(newpage);
976 VM_BUG_ON(!PageSwapBacked(oldpage));
977 SetPageSwapBacked(newpage);
978 VM_BUG_ON(!swap_index);
979 set_page_private(newpage, swap_index);
980 VM_BUG_ON(!PageSwapCache(oldpage));
981 SetPageSwapCache(newpage);
984 * Our caller will very soon move newpage out of swapcache, but it's
985 * a nice clean interface for us to replace oldpage by newpage there.
987 spin_lock_irq(&swap_mapping->tree_lock);
988 error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
990 __inc_zone_page_state(newpage, NR_FILE_PAGES);
991 __dec_zone_page_state(oldpage, NR_FILE_PAGES);
992 spin_unlock_irq(&swap_mapping->tree_lock);
995 mem_cgroup_replace_page_cache(oldpage, newpage);
996 lru_cache_add_anon(newpage);
998 ClearPageSwapCache(oldpage);
999 set_page_private(oldpage, 0);
1001 unlock_page(oldpage);
1002 page_cache_release(oldpage);
1003 page_cache_release(oldpage);
1008 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
1010 * If we allocate a new one we do not mark it dirty. That's up to the
1011 * vm. If we swap it in we mark it dirty since we also free the swap
1012 * entry since a page cannot live in both the swap and page cache
1014 static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
1015 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
1017 struct address_space *mapping = inode->i_mapping;
1018 struct shmem_inode_info *info;
1019 struct shmem_sb_info *sbinfo;
1026 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
1030 page = find_lock_page(mapping, index);
1031 if (radix_tree_exceptional_entry(page)) {
1032 swap = radix_to_swp_entry(page);
1036 if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
1037 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1042 /* fallocated page? */
1043 if (page && !PageUptodate(page)) {
1044 if (sgp != SGP_READ)
1047 page_cache_release(page);
1050 if (page || (sgp == SGP_READ && !swap.val)) {
1056 * Fast cache lookup did not find it:
1057 * bring it back from swap or allocate.
1059 info = SHMEM_I(inode);
1060 sbinfo = SHMEM_SB(inode->i_sb);
1063 /* Look it up and read it in.. */
1064 page = lookup_swap_cache(swap);
1066 /* here we actually do the io */
1068 *fault_type |= VM_FAULT_MAJOR;
1069 page = shmem_swapin(swap, gfp, info, index);
1076 /* We have to do this with page locked to prevent races */
1078 if (!PageSwapCache(page) || page->mapping) {
1079 error = -EEXIST; /* try again */
1082 if (!PageUptodate(page)) {
1086 wait_on_page_writeback(page);
1088 if (shmem_should_replace_page(page, gfp)) {
1089 error = shmem_replace_page(&page, gfp, info, index);
1094 error = mem_cgroup_cache_charge(page, current->mm,
1095 gfp & GFP_RECLAIM_MASK);
1097 error = shmem_add_to_page_cache(page, mapping, index,
1098 gfp, swp_to_radix_entry(swap));
1102 spin_lock(&info->lock);
1104 shmem_recalc_inode(inode);
1105 spin_unlock(&info->lock);
1107 delete_from_swap_cache(page);
1108 set_page_dirty(page);
1112 if (shmem_acct_block(info->flags)) {
1116 if (sbinfo->max_blocks) {
1117 if (percpu_counter_compare(&sbinfo->used_blocks,
1118 sbinfo->max_blocks) >= 0) {
1122 percpu_counter_inc(&sbinfo->used_blocks);
1125 page = shmem_alloc_page(gfp, info, index);
1131 SetPageSwapBacked(page);
1132 __set_page_locked(page);
1133 error = mem_cgroup_cache_charge(page, current->mm,
1134 gfp & GFP_RECLAIM_MASK);
1136 error = shmem_add_to_page_cache(page, mapping, index,
1140 lru_cache_add_anon(page);
1142 spin_lock(&info->lock);
1144 inode->i_blocks += BLOCKS_PER_PAGE;
1145 shmem_recalc_inode(inode);
1146 spin_unlock(&info->lock);
1150 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1152 if (sgp == SGP_FALLOC)
1156 * Let SGP_WRITE caller clear ends if write does not fill page;
1157 * but SGP_FALLOC on a page fallocated earlier must initialize
1158 * it now, lest undo on failure cancel our earlier guarantee.
1160 if (sgp != SGP_WRITE) {
1161 clear_highpage(page);
1162 flush_dcache_page(page);
1163 SetPageUptodate(page);
1165 if (sgp == SGP_DIRTY)
1166 set_page_dirty(page);
1169 /* Perhaps the file has been truncated since we checked */
1170 if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
1171 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1185 info = SHMEM_I(inode);
1186 ClearPageDirty(page);
1187 delete_from_page_cache(page);
1188 spin_lock(&info->lock);
1190 inode->i_blocks -= BLOCKS_PER_PAGE;
1191 spin_unlock(&info->lock);
1193 sbinfo = SHMEM_SB(inode->i_sb);
1194 if (sbinfo->max_blocks)
1195 percpu_counter_add(&sbinfo->used_blocks, -1);
1197 shmem_unacct_blocks(info->flags, 1);
1199 if (swap.val && error != -EINVAL) {
1200 struct page *test = find_get_page(mapping, index);
1201 if (test && !radix_tree_exceptional_entry(test))
1202 page_cache_release(test);
1203 /* Have another try if the entry has changed */
1204 if (test != swp_to_radix_entry(swap))
1209 page_cache_release(page);
1211 if (error == -ENOSPC && !once++) {
1212 info = SHMEM_I(inode);
1213 spin_lock(&info->lock);
1214 shmem_recalc_inode(inode);
1215 spin_unlock(&info->lock);
1218 if (error == -EEXIST)
1223 static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1225 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1227 int ret = VM_FAULT_LOCKED;
1229 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1231 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
1233 if (ret & VM_FAULT_MAJOR) {
1234 count_vm_event(PGMAJFAULT);
1235 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1241 static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
1243 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1244 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
1247 static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1250 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1253 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1254 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
1258 int shmem_lock(struct file *file, int lock, struct user_struct *user)
1260 struct inode *inode = file->f_path.dentry->d_inode;
1261 struct shmem_inode_info *info = SHMEM_I(inode);
1262 int retval = -ENOMEM;
1264 spin_lock(&info->lock);
1265 if (lock && !(info->flags & VM_LOCKED)) {
1266 if (!user_shm_lock(inode->i_size, user))
1268 info->flags |= VM_LOCKED;
1269 mapping_set_unevictable(file->f_mapping);
1271 if (!lock && (info->flags & VM_LOCKED) && user) {
1272 user_shm_unlock(inode->i_size, user);
1273 info->flags &= ~VM_LOCKED;
1274 mapping_clear_unevictable(file->f_mapping);
1279 spin_unlock(&info->lock);
1283 static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1285 file_accessed(file);
1286 vma->vm_ops = &shmem_vm_ops;
1287 vma->vm_flags |= VM_CAN_NONLINEAR;
1291 static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1292 umode_t mode, dev_t dev, unsigned long flags)
1294 struct inode *inode;
1295 struct shmem_inode_info *info;
1296 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1298 if (shmem_reserve_inode(sb))
1301 inode = new_inode(sb);
1303 inode->i_ino = get_next_ino();
1304 inode_init_owner(inode, dir, mode);
1305 inode->i_blocks = 0;
1306 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1307 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1308 inode->i_generation = get_seconds();
1309 info = SHMEM_I(inode);
1310 memset(info, 0, (char *)inode - (char *)info);
1311 spin_lock_init(&info->lock);
1312 info->flags = flags & VM_NORESERVE;
1313 INIT_LIST_HEAD(&info->swaplist);
1314 INIT_LIST_HEAD(&info->xattr_list);
1315 cache_no_acl(inode);
1317 switch (mode & S_IFMT) {
1319 inode->i_op = &shmem_special_inode_operations;
1320 init_special_inode(inode, mode, dev);
1323 inode->i_mapping->a_ops = &shmem_aops;
1324 inode->i_op = &shmem_inode_operations;
1325 inode->i_fop = &shmem_file_operations;
1326 mpol_shared_policy_init(&info->policy,
1327 shmem_get_sbmpol(sbinfo));
1331 /* Some things misbehave if size == 0 on a directory */
1332 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1333 inode->i_op = &shmem_dir_inode_operations;
1334 inode->i_fop = &simple_dir_operations;
1338 * Must not load anything in the rbtree,
1339 * mpol_free_shared_policy will not be called.
1341 mpol_shared_policy_init(&info->policy, NULL);
1345 shmem_free_inode(sb);
1350 static const struct inode_operations shmem_symlink_inode_operations;
1351 static const struct inode_operations shmem_short_symlink_operations;
1353 #ifdef CONFIG_TMPFS_XATTR
1354 static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
1356 #define shmem_initxattrs NULL
1360 shmem_write_begin(struct file *file, struct address_space *mapping,
1361 loff_t pos, unsigned len, unsigned flags,
1362 struct page **pagep, void **fsdata)
1364 struct inode *inode = mapping->host;
1365 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1366 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1370 shmem_write_end(struct file *file, struct address_space *mapping,
1371 loff_t pos, unsigned len, unsigned copied,
1372 struct page *page, void *fsdata)
1374 struct inode *inode = mapping->host;
1376 if (pos + copied > inode->i_size)
1377 i_size_write(inode, pos + copied);
1379 if (!PageUptodate(page)) {
1380 if (copied < PAGE_CACHE_SIZE) {
1381 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1382 zero_user_segments(page, 0, from,
1383 from + copied, PAGE_CACHE_SIZE);
1385 SetPageUptodate(page);
1387 set_page_dirty(page);
1389 page_cache_release(page);
1394 static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1396 struct inode *inode = filp->f_path.dentry->d_inode;
1397 struct address_space *mapping = inode->i_mapping;
1399 unsigned long offset;
1400 enum sgp_type sgp = SGP_READ;
1403 * Might this read be for a stacking filesystem? Then when reading
1404 * holes of a sparse file, we actually need to allocate those pages,
1405 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1407 if (segment_eq(get_fs(), KERNEL_DS))
1410 index = *ppos >> PAGE_CACHE_SHIFT;
1411 offset = *ppos & ~PAGE_CACHE_MASK;
1414 struct page *page = NULL;
1416 unsigned long nr, ret;
1417 loff_t i_size = i_size_read(inode);
1419 end_index = i_size >> PAGE_CACHE_SHIFT;
1420 if (index > end_index)
1422 if (index == end_index) {
1423 nr = i_size & ~PAGE_CACHE_MASK;
1428 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
1430 if (desc->error == -EINVAL)
1438 * We must evaluate after, since reads (unlike writes)
1439 * are called without i_mutex protection against truncate
1441 nr = PAGE_CACHE_SIZE;
1442 i_size = i_size_read(inode);
1443 end_index = i_size >> PAGE_CACHE_SHIFT;
1444 if (index == end_index) {
1445 nr = i_size & ~PAGE_CACHE_MASK;
1448 page_cache_release(page);
1456 * If users can be writing to this page using arbitrary
1457 * virtual addresses, take care about potential aliasing
1458 * before reading the page on the kernel side.
1460 if (mapping_writably_mapped(mapping))
1461 flush_dcache_page(page);
1463 * Mark the page accessed if we read the beginning.
1466 mark_page_accessed(page);
1468 page = ZERO_PAGE(0);
1469 page_cache_get(page);
1473 * Ok, we have the page, and it's up-to-date, so
1474 * now we can copy it to user space...
1476 * The actor routine returns how many bytes were actually used..
1477 * NOTE! This may not be the same as how much of a user buffer
1478 * we filled up (we may be padding etc), so we can only update
1479 * "pos" here (the actor routine has to update the user buffer
1480 * pointers and the remaining count).
1482 ret = actor(desc, page, offset, nr);
1484 index += offset >> PAGE_CACHE_SHIFT;
1485 offset &= ~PAGE_CACHE_MASK;
1487 page_cache_release(page);
1488 if (ret != nr || !desc->count)
1494 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1495 file_accessed(filp);
1498 static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1499 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
1501 struct file *filp = iocb->ki_filp;
1505 loff_t *ppos = &iocb->ki_pos;
1507 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1511 for (seg = 0; seg < nr_segs; seg++) {
1512 read_descriptor_t desc;
1515 desc.arg.buf = iov[seg].iov_base;
1516 desc.count = iov[seg].iov_len;
1517 if (desc.count == 0)
1520 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1521 retval += desc.written;
1523 retval = retval ?: desc.error;
1532 static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1533 struct pipe_inode_info *pipe, size_t len,
1536 struct address_space *mapping = in->f_mapping;
1537 struct inode *inode = mapping->host;
1538 unsigned int loff, nr_pages, req_pages;
1539 struct page *pages[PIPE_DEF_BUFFERS];
1540 struct partial_page partial[PIPE_DEF_BUFFERS];
1542 pgoff_t index, end_index;
1545 struct splice_pipe_desc spd = {
1549 .ops = &page_cache_pipe_buf_ops,
1550 .spd_release = spd_release_page,
1553 isize = i_size_read(inode);
1554 if (unlikely(*ppos >= isize))
1557 left = isize - *ppos;
1558 if (unlikely(left < len))
1561 if (splice_grow_spd(pipe, &spd))
1564 index = *ppos >> PAGE_CACHE_SHIFT;
1565 loff = *ppos & ~PAGE_CACHE_MASK;
1566 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1567 nr_pages = min(req_pages, pipe->buffers);
1569 spd.nr_pages = find_get_pages_contig(mapping, index,
1570 nr_pages, spd.pages);
1571 index += spd.nr_pages;
1574 while (spd.nr_pages < nr_pages) {
1575 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1579 spd.pages[spd.nr_pages++] = page;
1583 index = *ppos >> PAGE_CACHE_SHIFT;
1584 nr_pages = spd.nr_pages;
1587 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1588 unsigned int this_len;
1593 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1594 page = spd.pages[page_nr];
1596 if (!PageUptodate(page) || page->mapping != mapping) {
1597 error = shmem_getpage(inode, index, &page,
1602 page_cache_release(spd.pages[page_nr]);
1603 spd.pages[page_nr] = page;
1606 isize = i_size_read(inode);
1607 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1608 if (unlikely(!isize || index > end_index))
1611 if (end_index == index) {
1614 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1618 this_len = min(this_len, plen - loff);
1622 spd.partial[page_nr].offset = loff;
1623 spd.partial[page_nr].len = this_len;
1630 while (page_nr < nr_pages)
1631 page_cache_release(spd.pages[page_nr++]);
1634 error = splice_to_pipe(pipe, &spd);
1636 splice_shrink_spd(pipe, &spd);
1645 static long shmem_fallocate(struct file *file, int mode, loff_t offset,
1648 struct inode *inode = file->f_path.dentry->d_inode;
1649 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
1650 pgoff_t start, index, end;
1653 mutex_lock(&inode->i_mutex);
1655 if (mode & FALLOC_FL_PUNCH_HOLE) {
1656 struct address_space *mapping = file->f_mapping;
1657 loff_t unmap_start = round_up(offset, PAGE_SIZE);
1658 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
1660 if ((u64)unmap_end > (u64)unmap_start)
1661 unmap_mapping_range(mapping, unmap_start,
1662 1 + unmap_end - unmap_start, 0);
1663 shmem_truncate_range(inode, offset, offset + len - 1);
1664 /* No need to unmap again: hole-punching leaves COWed pages */
1669 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
1670 error = inode_newsize_ok(inode, offset + len);
1674 start = offset >> PAGE_CACHE_SHIFT;
1675 end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1676 /* Try to avoid a swapstorm if len is impossible to satisfy */
1677 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
1682 for (index = start; index < end; index++) {
1686 * Good, the fallocate(2) manpage permits EINTR: we may have
1687 * been interrupted because we are using up too much memory.
1689 if (signal_pending(current))
1692 error = shmem_getpage(inode, index, &page, SGP_FALLOC,
1695 /* Remove the !PageUptodate pages we added */
1696 shmem_undo_range(inode,
1697 (loff_t)start << PAGE_CACHE_SHIFT,
1698 (loff_t)index << PAGE_CACHE_SHIFT, true);
1703 * If !PageUptodate, leave it that way so that freeable pages
1704 * can be recognized if we need to rollback on error later.
1705 * But set_page_dirty so that memory pressure will swap rather
1706 * than free the pages we are allocating (and SGP_CACHE pages
1707 * might still be clean: we now need to mark those dirty too).
1709 set_page_dirty(page);
1711 page_cache_release(page);
1715 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
1716 i_size_write(inode, offset + len);
1718 inode->i_ctime = CURRENT_TIME;
1720 mutex_unlock(&inode->i_mutex);
1724 static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1726 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1728 buf->f_type = TMPFS_MAGIC;
1729 buf->f_bsize = PAGE_CACHE_SIZE;
1730 buf->f_namelen = NAME_MAX;
1731 if (sbinfo->max_blocks) {
1732 buf->f_blocks = sbinfo->max_blocks;
1734 buf->f_bfree = sbinfo->max_blocks -
1735 percpu_counter_sum(&sbinfo->used_blocks);
1737 if (sbinfo->max_inodes) {
1738 buf->f_files = sbinfo->max_inodes;
1739 buf->f_ffree = sbinfo->free_inodes;
1741 /* else leave those fields 0 like simple_statfs */
1746 * File creation. Allocate an inode, and we're done..
1749 shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1751 struct inode *inode;
1752 int error = -ENOSPC;
1754 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
1756 error = security_inode_init_security(inode, dir,
1758 shmem_initxattrs, NULL);
1760 if (error != -EOPNOTSUPP) {
1765 #ifdef CONFIG_TMPFS_POSIX_ACL
1766 error = generic_acl_init(inode, dir);
1774 dir->i_size += BOGO_DIRENT_SIZE;
1775 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1776 d_instantiate(dentry, inode);
1777 dget(dentry); /* Extra count - pin the dentry in core */
1782 static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1786 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1792 static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1793 struct nameidata *nd)
1795 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1801 static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1803 struct inode *inode = old_dentry->d_inode;
1807 * No ordinary (disk based) filesystem counts links as inodes;
1808 * but each new link needs a new dentry, pinning lowmem, and
1809 * tmpfs dentries cannot be pruned until they are unlinked.
1811 ret = shmem_reserve_inode(inode->i_sb);
1815 dir->i_size += BOGO_DIRENT_SIZE;
1816 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1818 ihold(inode); /* New dentry reference */
1819 dget(dentry); /* Extra pinning count for the created dentry */
1820 d_instantiate(dentry, inode);
1825 static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1827 struct inode *inode = dentry->d_inode;
1829 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1830 shmem_free_inode(inode->i_sb);
1832 dir->i_size -= BOGO_DIRENT_SIZE;
1833 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1835 dput(dentry); /* Undo the count from "create" - this does all the work */
1839 static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1841 if (!simple_empty(dentry))
1844 drop_nlink(dentry->d_inode);
1846 return shmem_unlink(dir, dentry);
1850 * The VFS layer already does all the dentry stuff for rename,
1851 * we just have to decrement the usage count for the target if
1852 * it exists so that the VFS layer correctly free's it when it
1855 static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1857 struct inode *inode = old_dentry->d_inode;
1858 int they_are_dirs = S_ISDIR(inode->i_mode);
1860 if (!simple_empty(new_dentry))
1863 if (new_dentry->d_inode) {
1864 (void) shmem_unlink(new_dir, new_dentry);
1866 drop_nlink(old_dir);
1867 } else if (they_are_dirs) {
1868 drop_nlink(old_dir);
1872 old_dir->i_size -= BOGO_DIRENT_SIZE;
1873 new_dir->i_size += BOGO_DIRENT_SIZE;
1874 old_dir->i_ctime = old_dir->i_mtime =
1875 new_dir->i_ctime = new_dir->i_mtime =
1876 inode->i_ctime = CURRENT_TIME;
1880 static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1884 struct inode *inode;
1887 struct shmem_inode_info *info;
1889 len = strlen(symname) + 1;
1890 if (len > PAGE_CACHE_SIZE)
1891 return -ENAMETOOLONG;
1893 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
1897 error = security_inode_init_security(inode, dir, &dentry->d_name,
1898 shmem_initxattrs, NULL);
1900 if (error != -EOPNOTSUPP) {
1907 info = SHMEM_I(inode);
1908 inode->i_size = len-1;
1909 if (len <= SHORT_SYMLINK_LEN) {
1910 info->symlink = kmemdup(symname, len, GFP_KERNEL);
1911 if (!info->symlink) {
1915 inode->i_op = &shmem_short_symlink_operations;
1917 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1922 inode->i_mapping->a_ops = &shmem_aops;
1923 inode->i_op = &shmem_symlink_inode_operations;
1924 kaddr = kmap_atomic(page);
1925 memcpy(kaddr, symname, len);
1926 kunmap_atomic(kaddr);
1927 SetPageUptodate(page);
1928 set_page_dirty(page);
1930 page_cache_release(page);
1932 dir->i_size += BOGO_DIRENT_SIZE;
1933 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1934 d_instantiate(dentry, inode);
1939 static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
1941 nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
1945 static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1947 struct page *page = NULL;
1948 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1949 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
1955 static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1957 if (!IS_ERR(nd_get_link(nd))) {
1958 struct page *page = cookie;
1960 mark_page_accessed(page);
1961 page_cache_release(page);
1965 #ifdef CONFIG_TMPFS_XATTR
1967 * Superblocks without xattr inode operations may get some security.* xattr
1968 * support from the LSM "for free". As soon as we have any other xattrs
1969 * like ACLs, we also need to implement the security.* handlers at
1970 * filesystem level, though.
1974 * Allocate new xattr and copy in the value; but leave the name to callers.
1976 static struct shmem_xattr *shmem_xattr_alloc(const void *value, size_t size)
1978 struct shmem_xattr *new_xattr;
1982 len = sizeof(*new_xattr) + size;
1983 if (len <= sizeof(*new_xattr))
1986 new_xattr = kmalloc(len, GFP_KERNEL);
1990 new_xattr->size = size;
1991 memcpy(new_xattr->value, value, size);
1996 * Callback for security_inode_init_security() for acquiring xattrs.
1998 static int shmem_initxattrs(struct inode *inode,
1999 const struct xattr *xattr_array,
2002 struct shmem_inode_info *info = SHMEM_I(inode);
2003 const struct xattr *xattr;
2004 struct shmem_xattr *new_xattr;
2007 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
2008 new_xattr = shmem_xattr_alloc(xattr->value, xattr->value_len);
2012 len = strlen(xattr->name) + 1;
2013 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
2015 if (!new_xattr->name) {
2020 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
2021 XATTR_SECURITY_PREFIX_LEN);
2022 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
2025 spin_lock(&info->lock);
2026 list_add(&new_xattr->list, &info->xattr_list);
2027 spin_unlock(&info->lock);
2033 static int shmem_xattr_get(struct dentry *dentry, const char *name,
2034 void *buffer, size_t size)
2036 struct shmem_inode_info *info;
2037 struct shmem_xattr *xattr;
2040 info = SHMEM_I(dentry->d_inode);
2042 spin_lock(&info->lock);
2043 list_for_each_entry(xattr, &info->xattr_list, list) {
2044 if (strcmp(name, xattr->name))
2049 if (size < xattr->size)
2052 memcpy(buffer, xattr->value, xattr->size);
2056 spin_unlock(&info->lock);
2060 static int shmem_xattr_set(struct inode *inode, const char *name,
2061 const void *value, size_t size, int flags)
2063 struct shmem_inode_info *info = SHMEM_I(inode);
2064 struct shmem_xattr *xattr;
2065 struct shmem_xattr *new_xattr = NULL;
2068 /* value == NULL means remove */
2070 new_xattr = shmem_xattr_alloc(value, size);
2074 new_xattr->name = kstrdup(name, GFP_KERNEL);
2075 if (!new_xattr->name) {
2081 spin_lock(&info->lock);
2082 list_for_each_entry(xattr, &info->xattr_list, list) {
2083 if (!strcmp(name, xattr->name)) {
2084 if (flags & XATTR_CREATE) {
2087 } else if (new_xattr) {
2088 list_replace(&xattr->list, &new_xattr->list);
2090 list_del(&xattr->list);
2095 if (flags & XATTR_REPLACE) {
2099 list_add(&new_xattr->list, &info->xattr_list);
2103 spin_unlock(&info->lock);
2110 static const struct xattr_handler *shmem_xattr_handlers[] = {
2111 #ifdef CONFIG_TMPFS_POSIX_ACL
2112 &generic_acl_access_handler,
2113 &generic_acl_default_handler,
2118 static int shmem_xattr_validate(const char *name)
2120 struct { const char *prefix; size_t len; } arr[] = {
2121 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
2122 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
2126 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2127 size_t preflen = arr[i].len;
2128 if (strncmp(name, arr[i].prefix, preflen) == 0) {
2137 static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
2138 void *buffer, size_t size)
2143 * If this is a request for a synthetic attribute in the system.*
2144 * namespace use the generic infrastructure to resolve a handler
2145 * for it via sb->s_xattr.
2147 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2148 return generic_getxattr(dentry, name, buffer, size);
2150 err = shmem_xattr_validate(name);
2154 return shmem_xattr_get(dentry, name, buffer, size);
2157 static int shmem_setxattr(struct dentry *dentry, const char *name,
2158 const void *value, size_t size, int flags)
2163 * If this is a request for a synthetic attribute in the system.*
2164 * namespace use the generic infrastructure to resolve a handler
2165 * for it via sb->s_xattr.
2167 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2168 return generic_setxattr(dentry, name, value, size, flags);
2170 err = shmem_xattr_validate(name);
2175 value = ""; /* empty EA, do not remove */
2177 return shmem_xattr_set(dentry->d_inode, name, value, size, flags);
2181 static int shmem_removexattr(struct dentry *dentry, const char *name)
2186 * If this is a request for a synthetic attribute in the system.*
2187 * namespace use the generic infrastructure to resolve a handler
2188 * for it via sb->s_xattr.
2190 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2191 return generic_removexattr(dentry, name);
2193 err = shmem_xattr_validate(name);
2197 return shmem_xattr_set(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
2200 static bool xattr_is_trusted(const char *name)
2202 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
2205 static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2207 bool trusted = capable(CAP_SYS_ADMIN);
2208 struct shmem_xattr *xattr;
2209 struct shmem_inode_info *info;
2212 info = SHMEM_I(dentry->d_inode);
2214 spin_lock(&info->lock);
2215 list_for_each_entry(xattr, &info->xattr_list, list) {
2218 /* skip "trusted." attributes for unprivileged callers */
2219 if (!trusted && xattr_is_trusted(xattr->name))
2222 len = strlen(xattr->name) + 1;
2229 memcpy(buffer, xattr->name, len);
2233 spin_unlock(&info->lock);
2237 #endif /* CONFIG_TMPFS_XATTR */
2239 static const struct inode_operations shmem_short_symlink_operations = {
2240 .readlink = generic_readlink,
2241 .follow_link = shmem_follow_short_symlink,
2242 #ifdef CONFIG_TMPFS_XATTR
2243 .setxattr = shmem_setxattr,
2244 .getxattr = shmem_getxattr,
2245 .listxattr = shmem_listxattr,
2246 .removexattr = shmem_removexattr,
2250 static const struct inode_operations shmem_symlink_inode_operations = {
2251 .readlink = generic_readlink,
2252 .follow_link = shmem_follow_link,
2253 .put_link = shmem_put_link,
2254 #ifdef CONFIG_TMPFS_XATTR
2255 .setxattr = shmem_setxattr,
2256 .getxattr = shmem_getxattr,
2257 .listxattr = shmem_listxattr,
2258 .removexattr = shmem_removexattr,
2262 static struct dentry *shmem_get_parent(struct dentry *child)
2264 return ERR_PTR(-ESTALE);
2267 static int shmem_match(struct inode *ino, void *vfh)
2271 inum = (inum << 32) | fh[1];
2272 return ino->i_ino == inum && fh[0] == ino->i_generation;
2275 static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2276 struct fid *fid, int fh_len, int fh_type)
2278 struct inode *inode;
2279 struct dentry *dentry = NULL;
2280 u64 inum = fid->raw[2];
2281 inum = (inum << 32) | fid->raw[1];
2286 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2287 shmem_match, fid->raw);
2289 dentry = d_find_alias(inode);
2296 static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2299 struct inode *inode = dentry->d_inode;
2306 if (inode_unhashed(inode)) {
2307 /* Unfortunately insert_inode_hash is not idempotent,
2308 * so as we hash inodes here rather than at creation
2309 * time, we need a lock to ensure we only try
2312 static DEFINE_SPINLOCK(lock);
2314 if (inode_unhashed(inode))
2315 __insert_inode_hash(inode,
2316 inode->i_ino + inode->i_generation);
2320 fh[0] = inode->i_generation;
2321 fh[1] = inode->i_ino;
2322 fh[2] = ((__u64)inode->i_ino) >> 32;
2328 static const struct export_operations shmem_export_ops = {
2329 .get_parent = shmem_get_parent,
2330 .encode_fh = shmem_encode_fh,
2331 .fh_to_dentry = shmem_fh_to_dentry,
2334 static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2337 char *this_char, *value, *rest;
2341 while (options != NULL) {
2342 this_char = options;
2345 * NUL-terminate this option: unfortunately,
2346 * mount options form a comma-separated list,
2347 * but mpol's nodelist may also contain commas.
2349 options = strchr(options, ',');
2350 if (options == NULL)
2353 if (!isdigit(*options)) {
2360 if ((value = strchr(this_char,'=')) != NULL) {
2364 "tmpfs: No value for mount option '%s'\n",
2369 if (!strcmp(this_char,"size")) {
2370 unsigned long long size;
2371 size = memparse(value,&rest);
2373 size <<= PAGE_SHIFT;
2374 size *= totalram_pages;
2380 sbinfo->max_blocks =
2381 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
2382 } else if (!strcmp(this_char,"nr_blocks")) {
2383 sbinfo->max_blocks = memparse(value, &rest);
2386 } else if (!strcmp(this_char,"nr_inodes")) {
2387 sbinfo->max_inodes = memparse(value, &rest);
2390 } else if (!strcmp(this_char,"mode")) {
2393 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
2396 } else if (!strcmp(this_char,"uid")) {
2399 uid = simple_strtoul(value, &rest, 0);
2402 sbinfo->uid = make_kuid(current_user_ns(), uid);
2403 if (!uid_valid(sbinfo->uid))
2405 } else if (!strcmp(this_char,"gid")) {
2408 gid = simple_strtoul(value, &rest, 0);
2411 sbinfo->gid = make_kgid(current_user_ns(), gid);
2412 if (!gid_valid(sbinfo->gid))
2414 } else if (!strcmp(this_char,"mpol")) {
2415 if (mpol_parse_str(value, &sbinfo->mpol, 1))
2418 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2426 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2432 static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2434 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2435 struct shmem_sb_info config = *sbinfo;
2436 unsigned long inodes;
2437 int error = -EINVAL;
2439 if (shmem_parse_options(data, &config, true))
2442 spin_lock(&sbinfo->stat_lock);
2443 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
2444 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
2446 if (config.max_inodes < inodes)
2449 * Those tests disallow limited->unlimited while any are in use;
2450 * but we must separately disallow unlimited->limited, because
2451 * in that case we have no record of how much is already in use.
2453 if (config.max_blocks && !sbinfo->max_blocks)
2455 if (config.max_inodes && !sbinfo->max_inodes)
2459 sbinfo->max_blocks = config.max_blocks;
2460 sbinfo->max_inodes = config.max_inodes;
2461 sbinfo->free_inodes = config.max_inodes - inodes;
2463 mpol_put(sbinfo->mpol);
2464 sbinfo->mpol = config.mpol; /* transfers initial ref */
2466 spin_unlock(&sbinfo->stat_lock);
2470 static int shmem_show_options(struct seq_file *seq, struct dentry *root)
2472 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
2474 if (sbinfo->max_blocks != shmem_default_max_blocks())
2475 seq_printf(seq, ",size=%luk",
2476 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2477 if (sbinfo->max_inodes != shmem_default_max_inodes())
2478 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2479 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2480 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
2481 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
2482 seq_printf(seq, ",uid=%u",
2483 from_kuid_munged(&init_user_ns, sbinfo->uid));
2484 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
2485 seq_printf(seq, ",gid=%u",
2486 from_kgid_munged(&init_user_ns, sbinfo->gid));
2487 shmem_show_mpol(seq, sbinfo->mpol);
2490 #endif /* CONFIG_TMPFS */
2492 static void shmem_put_super(struct super_block *sb)
2494 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2496 percpu_counter_destroy(&sbinfo->used_blocks);
2498 sb->s_fs_info = NULL;
2501 int shmem_fill_super(struct super_block *sb, void *data, int silent)
2503 struct inode *inode;
2504 struct shmem_sb_info *sbinfo;
2507 /* Round up to L1_CACHE_BYTES to resist false sharing */
2508 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
2509 L1_CACHE_BYTES), GFP_KERNEL);
2513 sbinfo->mode = S_IRWXUGO | S_ISVTX;
2514 sbinfo->uid = current_fsuid();
2515 sbinfo->gid = current_fsgid();
2516 sb->s_fs_info = sbinfo;
2520 * Per default we only allow half of the physical ram per
2521 * tmpfs instance, limiting inodes to one per page of lowmem;
2522 * but the internal instance is left unlimited.
2524 if (!(sb->s_flags & MS_NOUSER)) {
2525 sbinfo->max_blocks = shmem_default_max_blocks();
2526 sbinfo->max_inodes = shmem_default_max_inodes();
2527 if (shmem_parse_options(data, sbinfo, false)) {
2532 sb->s_export_op = &shmem_export_ops;
2533 sb->s_flags |= MS_NOSEC;
2535 sb->s_flags |= MS_NOUSER;
2538 spin_lock_init(&sbinfo->stat_lock);
2539 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2541 sbinfo->free_inodes = sbinfo->max_inodes;
2543 sb->s_maxbytes = MAX_LFS_FILESIZE;
2544 sb->s_blocksize = PAGE_CACHE_SIZE;
2545 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2546 sb->s_magic = TMPFS_MAGIC;
2547 sb->s_op = &shmem_ops;
2548 sb->s_time_gran = 1;
2549 #ifdef CONFIG_TMPFS_XATTR
2550 sb->s_xattr = shmem_xattr_handlers;
2552 #ifdef CONFIG_TMPFS_POSIX_ACL
2553 sb->s_flags |= MS_POSIXACL;
2556 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
2559 inode->i_uid = sbinfo->uid;
2560 inode->i_gid = sbinfo->gid;
2561 sb->s_root = d_make_root(inode);
2567 shmem_put_super(sb);
2571 static struct kmem_cache *shmem_inode_cachep;
2573 static struct inode *shmem_alloc_inode(struct super_block *sb)
2575 struct shmem_inode_info *info;
2576 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2579 return &info->vfs_inode;
2582 static void shmem_destroy_callback(struct rcu_head *head)
2584 struct inode *inode = container_of(head, struct inode, i_rcu);
2585 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2588 static void shmem_destroy_inode(struct inode *inode)
2590 if (S_ISREG(inode->i_mode))
2591 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2592 call_rcu(&inode->i_rcu, shmem_destroy_callback);
2595 static void shmem_init_inode(void *foo)
2597 struct shmem_inode_info *info = foo;
2598 inode_init_once(&info->vfs_inode);
2601 static int shmem_init_inodecache(void)
2603 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2604 sizeof(struct shmem_inode_info),
2605 0, SLAB_PANIC, shmem_init_inode);
2609 static void shmem_destroy_inodecache(void)
2611 kmem_cache_destroy(shmem_inode_cachep);
2614 static const struct address_space_operations shmem_aops = {
2615 .writepage = shmem_writepage,
2616 .set_page_dirty = __set_page_dirty_no_writeback,
2618 .write_begin = shmem_write_begin,
2619 .write_end = shmem_write_end,
2621 .migratepage = migrate_page,
2622 .error_remove_page = generic_error_remove_page,
2625 static const struct file_operations shmem_file_operations = {
2628 .llseek = generic_file_llseek,
2629 .read = do_sync_read,
2630 .write = do_sync_write,
2631 .aio_read = shmem_file_aio_read,
2632 .aio_write = generic_file_aio_write,
2633 .fsync = noop_fsync,
2634 .splice_read = shmem_file_splice_read,
2635 .splice_write = generic_file_splice_write,
2636 .fallocate = shmem_fallocate,
2640 static const struct inode_operations shmem_inode_operations = {
2641 .setattr = shmem_setattr,
2642 #ifdef CONFIG_TMPFS_XATTR
2643 .setxattr = shmem_setxattr,
2644 .getxattr = shmem_getxattr,
2645 .listxattr = shmem_listxattr,
2646 .removexattr = shmem_removexattr,
2650 static const struct inode_operations shmem_dir_inode_operations = {
2652 .create = shmem_create,
2653 .lookup = simple_lookup,
2655 .unlink = shmem_unlink,
2656 .symlink = shmem_symlink,
2657 .mkdir = shmem_mkdir,
2658 .rmdir = shmem_rmdir,
2659 .mknod = shmem_mknod,
2660 .rename = shmem_rename,
2662 #ifdef CONFIG_TMPFS_XATTR
2663 .setxattr = shmem_setxattr,
2664 .getxattr = shmem_getxattr,
2665 .listxattr = shmem_listxattr,
2666 .removexattr = shmem_removexattr,
2668 #ifdef CONFIG_TMPFS_POSIX_ACL
2669 .setattr = shmem_setattr,
2673 static const struct inode_operations shmem_special_inode_operations = {
2674 #ifdef CONFIG_TMPFS_XATTR
2675 .setxattr = shmem_setxattr,
2676 .getxattr = shmem_getxattr,
2677 .listxattr = shmem_listxattr,
2678 .removexattr = shmem_removexattr,
2680 #ifdef CONFIG_TMPFS_POSIX_ACL
2681 .setattr = shmem_setattr,
2685 static const struct super_operations shmem_ops = {
2686 .alloc_inode = shmem_alloc_inode,
2687 .destroy_inode = shmem_destroy_inode,
2689 .statfs = shmem_statfs,
2690 .remount_fs = shmem_remount_fs,
2691 .show_options = shmem_show_options,
2693 .evict_inode = shmem_evict_inode,
2694 .drop_inode = generic_delete_inode,
2695 .put_super = shmem_put_super,
2698 static const struct vm_operations_struct shmem_vm_ops = {
2699 .fault = shmem_fault,
2701 .set_policy = shmem_set_policy,
2702 .get_policy = shmem_get_policy,
2706 static struct dentry *shmem_mount(struct file_system_type *fs_type,
2707 int flags, const char *dev_name, void *data)
2709 return mount_nodev(fs_type, flags, data, shmem_fill_super);
2712 static struct file_system_type shmem_fs_type = {
2713 .owner = THIS_MODULE,
2715 .mount = shmem_mount,
2716 .kill_sb = kill_litter_super,
2719 int __init shmem_init(void)
2723 error = bdi_init(&shmem_backing_dev_info);
2727 error = shmem_init_inodecache();
2731 error = register_filesystem(&shmem_fs_type);
2733 printk(KERN_ERR "Could not register tmpfs\n");
2737 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2738 shmem_fs_type.name, NULL);
2739 if (IS_ERR(shm_mnt)) {
2740 error = PTR_ERR(shm_mnt);
2741 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2747 unregister_filesystem(&shmem_fs_type);
2749 shmem_destroy_inodecache();
2751 bdi_destroy(&shmem_backing_dev_info);
2753 shm_mnt = ERR_PTR(error);
2757 #else /* !CONFIG_SHMEM */
2760 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2762 * This is intended for small system where the benefits of the full
2763 * shmem code (swap-backed and resource-limited) are outweighed by
2764 * their complexity. On systems without swap this code should be
2765 * effectively equivalent, but much lighter weight.
2768 #include <linux/ramfs.h>
2770 static struct file_system_type shmem_fs_type = {
2772 .mount = ramfs_mount,
2773 .kill_sb = kill_litter_super,
2776 int __init shmem_init(void)
2778 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
2780 shm_mnt = kern_mount(&shmem_fs_type);
2781 BUG_ON(IS_ERR(shm_mnt));
2786 int shmem_unuse(swp_entry_t swap, struct page *page)
2791 int shmem_lock(struct file *file, int lock, struct user_struct *user)
2796 void shmem_unlock_mapping(struct address_space *mapping)
2800 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
2802 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
2804 EXPORT_SYMBOL_GPL(shmem_truncate_range);
2806 #define shmem_vm_ops generic_file_vm_ops
2807 #define shmem_file_operations ramfs_file_operations
2808 #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
2809 #define shmem_acct_size(flags, size) 0
2810 #define shmem_unacct_size(flags, size) do {} while (0)
2812 #endif /* CONFIG_SHMEM */
2817 * shmem_file_setup - get an unlinked file living in tmpfs
2818 * @name: name for dentry (to be seen in /proc/<pid>/maps
2819 * @size: size to be set for the file
2820 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
2822 struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
2826 struct inode *inode;
2828 struct dentry *root;
2831 if (IS_ERR(shm_mnt))
2832 return (void *)shm_mnt;
2834 if (size < 0 || size > MAX_LFS_FILESIZE)
2835 return ERR_PTR(-EINVAL);
2837 if (shmem_acct_size(flags, size))
2838 return ERR_PTR(-ENOMEM);
2842 this.len = strlen(name);
2843 this.hash = 0; /* will go */
2844 root = shm_mnt->mnt_root;
2845 path.dentry = d_alloc(root, &this);
2848 path.mnt = mntget(shm_mnt);
2851 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
2855 d_instantiate(path.dentry, inode);
2856 inode->i_size = size;
2857 clear_nlink(inode); /* It is unlinked */
2859 error = ramfs_nommu_expand_for_mapping(inode, size);
2865 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
2866 &shmem_file_operations);
2875 shmem_unacct_size(flags, size);
2876 return ERR_PTR(error);
2878 EXPORT_SYMBOL_GPL(shmem_file_setup);
2881 * shmem_zero_setup - setup a shared anonymous mapping
2882 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2884 int shmem_zero_setup(struct vm_area_struct *vma)
2887 loff_t size = vma->vm_end - vma->vm_start;
2889 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2891 return PTR_ERR(file);
2895 vma->vm_file = file;
2896 vma->vm_ops = &shmem_vm_ops;
2897 vma->vm_flags |= VM_CAN_NONLINEAR;
2902 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2903 * @mapping: the page's address_space
2904 * @index: the page index
2905 * @gfp: the page allocator flags to use if allocating
2907 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2908 * with any new page allocations done using the specified allocation flags.
2909 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2910 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2911 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2913 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2914 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
2916 struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2917 pgoff_t index, gfp_t gfp)
2920 struct inode *inode = mapping->host;
2924 BUG_ON(mapping->a_ops != &shmem_aops);
2925 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2927 page = ERR_PTR(error);
2933 * The tiny !SHMEM case uses ramfs without swap
2935 return read_cache_page_gfp(mapping, index, gfp);
2938 EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);