2 * Resizable virtual memory filesystem for Linux.
4 * Copyright (C) 2000 Linus Torvalds.
6 * 2000-2001 Christoph Rohland
9 * Copyright (C) 2002-2005 Hugh Dickins.
10 * Copyright (C) 2002-2005 VERITAS Software Corporation.
11 * Copyright (C) 2004 Andi Kleen, SuSE Labs
13 * Extended attribute support for tmpfs:
14 * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
15 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
18 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
20 * This file is released under the GPL.
24 #include <linux/init.h>
25 #include <linux/vfs.h>
26 #include <linux/mount.h>
27 #include <linux/pagemap.h>
28 #include <linux/file.h>
30 #include <linux/module.h>
31 #include <linux/percpu_counter.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/splice.h>
55 #include <linux/security.h>
56 #include <linux/swapops.h>
57 #include <linux/mempolicy.h>
58 #include <linux/namei.h>
59 #include <linux/ctype.h>
60 #include <linux/migrate.h>
61 #include <linux/highmem.h>
62 #include <linux/seq_file.h>
63 #include <linux/magic.h>
65 #include <asm/uaccess.h>
66 #include <asm/div64.h>
67 #include <asm/pgtable.h>
69 #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
70 #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
72 /* Pretend that each entry is of this size in directory's i_size */
73 #define BOGO_DIRENT_SIZE 20
76 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
77 char *name; /* xattr name */
82 /* Flag allocation requirements to shmem_getpage */
84 SGP_READ, /* don't exceed i_size, don't allocate page */
85 SGP_CACHE, /* don't exceed i_size, may allocate page */
86 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
87 SGP_WRITE, /* may exceed i_size, may allocate page */
91 static unsigned long shmem_default_max_blocks(void)
93 return totalram_pages / 2;
96 static unsigned long shmem_default_max_inodes(void)
98 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
102 static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
103 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
105 static inline int shmem_getpage(struct inode *inode, pgoff_t index,
106 struct page **pagep, enum sgp_type sgp, int *fault_type)
108 return shmem_getpage_gfp(inode, index, pagep, sgp,
109 mapping_gfp_mask(inode->i_mapping), fault_type);
112 static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
114 return sb->s_fs_info;
118 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
119 * for shared memory and for shared anonymous (/dev/zero) mappings
120 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
121 * consistent with the pre-accounting of private mappings ...
123 static inline int shmem_acct_size(unsigned long flags, loff_t size)
125 return (flags & VM_NORESERVE) ?
126 0 : security_vm_enough_memory_kern(VM_ACCT(size));
129 static inline void shmem_unacct_size(unsigned long flags, loff_t size)
131 if (!(flags & VM_NORESERVE))
132 vm_unacct_memory(VM_ACCT(size));
136 * ... whereas tmpfs objects are accounted incrementally as
137 * pages are allocated, in order to allow huge sparse files.
138 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
139 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
141 static inline int shmem_acct_block(unsigned long flags)
143 return (flags & VM_NORESERVE) ?
144 security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
147 static inline void shmem_unacct_blocks(unsigned long flags, long pages)
149 if (flags & VM_NORESERVE)
150 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
153 static const struct super_operations shmem_ops;
154 static const struct address_space_operations shmem_aops;
155 static const struct file_operations shmem_file_operations;
156 static const struct inode_operations shmem_inode_operations;
157 static const struct inode_operations shmem_dir_inode_operations;
158 static const struct inode_operations shmem_special_inode_operations;
159 static const struct vm_operations_struct shmem_vm_ops;
161 static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
162 .ra_pages = 0, /* No readahead */
163 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
166 static LIST_HEAD(shmem_swaplist);
167 static DEFINE_MUTEX(shmem_swaplist_mutex);
169 static void shmem_free_blocks(struct inode *inode, long pages)
171 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
172 if (sbinfo->max_blocks) {
173 percpu_counter_add(&sbinfo->used_blocks, -pages);
174 inode->i_blocks -= pages*BLOCKS_PER_PAGE;
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 size 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 info->alloced -= freed;
223 shmem_unacct_blocks(info->flags, freed);
224 shmem_free_blocks(inode, freed);
228 static void shmem_put_swap(struct shmem_inode_info *info, pgoff_t index,
231 if (index < SHMEM_NR_DIRECT)
232 info->i_direct[index] = swap;
235 static swp_entry_t shmem_get_swap(struct shmem_inode_info *info, pgoff_t index)
237 return (index < SHMEM_NR_DIRECT) ?
238 info->i_direct[index] : (swp_entry_t){0};
241 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
243 struct address_space *mapping = inode->i_mapping;
244 struct shmem_inode_info *info = SHMEM_I(inode);
245 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
246 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
250 truncate_inode_pages_range(mapping, lstart, lend);
252 if (end > SHMEM_NR_DIRECT)
253 end = SHMEM_NR_DIRECT;
255 spin_lock(&info->lock);
256 for (index = start; index < end; index++) {
257 swap = shmem_get_swap(info, index);
259 free_swap_and_cache(swap);
260 shmem_put_swap(info, index, (swp_entry_t){0});
265 if (mapping->nrpages) {
266 spin_unlock(&info->lock);
268 * A page may have meanwhile sneaked in from swap.
270 truncate_inode_pages_range(mapping, lstart, lend);
271 spin_lock(&info->lock);
274 shmem_recalc_inode(inode);
275 spin_unlock(&info->lock);
277 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
279 EXPORT_SYMBOL_GPL(shmem_truncate_range);
281 static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
283 struct inode *inode = dentry->d_inode;
286 error = inode_change_ok(inode, attr);
290 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
291 loff_t oldsize = inode->i_size;
292 loff_t newsize = attr->ia_size;
293 struct page *page = NULL;
295 if (newsize < oldsize) {
297 * If truncating down to a partial page, then
298 * if that page is already allocated, hold it
299 * in memory until the truncation is over, so
300 * truncate_partial_page cannot miss it were
301 * it assigned to swap.
303 if (newsize & (PAGE_CACHE_SIZE-1)) {
304 (void) shmem_getpage(inode,
305 newsize >> PAGE_CACHE_SHIFT,
306 &page, SGP_READ, NULL);
311 if (newsize != oldsize) {
312 i_size_write(inode, newsize);
313 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
315 if (newsize < oldsize) {
316 loff_t holebegin = round_up(newsize, PAGE_SIZE);
317 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
318 shmem_truncate_range(inode, newsize, (loff_t)-1);
319 /* unmap again to remove racily COWed private pages */
320 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
323 page_cache_release(page);
326 setattr_copy(inode, attr);
327 #ifdef CONFIG_TMPFS_POSIX_ACL
328 if (attr->ia_valid & ATTR_MODE)
329 error = generic_acl_chmod(inode);
334 static void shmem_evict_inode(struct inode *inode)
336 struct shmem_inode_info *info = SHMEM_I(inode);
337 struct shmem_xattr *xattr, *nxattr;
339 if (inode->i_mapping->a_ops == &shmem_aops) {
340 shmem_unacct_size(info->flags, inode->i_size);
342 shmem_truncate_range(inode, 0, (loff_t)-1);
343 if (!list_empty(&info->swaplist)) {
344 mutex_lock(&shmem_swaplist_mutex);
345 list_del_init(&info->swaplist);
346 mutex_unlock(&shmem_swaplist_mutex);
350 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
354 BUG_ON(inode->i_blocks);
355 shmem_free_inode(inode->i_sb);
356 end_writeback(inode);
359 static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
361 struct address_space *mapping = info->vfs_inode.i_mapping;
365 for (idx = 0; idx < SHMEM_NR_DIRECT; idx++)
366 if (shmem_get_swap(info, idx).val == entry.val)
370 spin_lock(&info->lock);
371 if (shmem_get_swap(info, idx).val != entry.val) {
372 spin_unlock(&info->lock);
377 * Move _head_ to start search for next from here.
378 * But be careful: shmem_evict_inode checks list_empty without taking
379 * mutex, and there's an instant in list_move_tail when info->swaplist
380 * would appear empty, if it were the only one on shmem_swaplist.
382 if (shmem_swaplist.next != &info->swaplist)
383 list_move_tail(&shmem_swaplist, &info->swaplist);
386 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
387 * but also to hold up shmem_evict_inode(): so inode cannot be freed
388 * beneath us (pagelock doesn't help until the page is in pagecache).
390 error = add_to_page_cache_locked(page, mapping, idx, GFP_NOWAIT);
391 /* which does mem_cgroup_uncharge_cache_page on error */
393 if (error != -ENOMEM) {
394 delete_from_swap_cache(page);
395 set_page_dirty(page);
396 shmem_put_swap(info, idx, (swp_entry_t){0});
399 error = 1; /* not an error, but entry was found */
401 spin_unlock(&info->lock);
406 * shmem_unuse() search for an eventually swapped out shmem page.
408 int shmem_unuse(swp_entry_t entry, struct page *page)
410 struct list_head *p, *next;
411 struct shmem_inode_info *info;
416 * Charge page using GFP_KERNEL while we can wait, before taking
417 * the shmem_swaplist_mutex which might hold up shmem_writepage().
418 * Charged back to the user (not to caller) when swap account is used.
419 * add_to_page_cache() will be called with GFP_NOWAIT.
421 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
425 * Try to preload while we can wait, to not make a habit of
426 * draining atomic reserves; but don't latch on to this cpu,
427 * it's okay if sometimes we get rescheduled after this.
429 error = radix_tree_preload(GFP_KERNEL);
432 radix_tree_preload_end();
434 mutex_lock(&shmem_swaplist_mutex);
435 list_for_each_safe(p, next, &shmem_swaplist) {
436 info = list_entry(p, struct shmem_inode_info, swaplist);
437 if (!info->swapped) {
438 spin_lock(&info->lock);
440 list_del_init(&info->swaplist);
441 spin_unlock(&info->lock);
444 found = shmem_unuse_inode(info, entry, page);
449 mutex_unlock(&shmem_swaplist_mutex);
453 mem_cgroup_uncharge_cache_page(page);
458 page_cache_release(page);
463 * Move the page from the page cache to the swap cache.
465 static int shmem_writepage(struct page *page, struct writeback_control *wbc)
467 struct shmem_inode_info *info;
468 swp_entry_t swap, oswap;
469 struct address_space *mapping;
473 BUG_ON(!PageLocked(page));
474 mapping = page->mapping;
476 inode = mapping->host;
477 info = SHMEM_I(inode);
478 if (info->flags & VM_LOCKED)
480 if (!total_swap_pages)
484 * shmem_backing_dev_info's capabilities prevent regular writeback or
485 * sync from ever calling shmem_writepage; but a stacking filesystem
486 * might use ->writepage of its underlying filesystem, in which case
487 * tmpfs should write out to swap only in response to memory pressure,
488 * and not for the writeback threads or sync.
490 if (!wbc->for_reclaim) {
491 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
496 * Just for this patch, we have a toy implementation,
497 * which can swap out only the first SHMEM_NR_DIRECT pages:
498 * for simple demonstration of where we need to think about swap.
500 if (index >= SHMEM_NR_DIRECT)
503 swap = get_swap_page();
508 * Add inode to shmem_unuse()'s list of swapped-out inodes,
509 * if it's not already there. Do it now because we cannot take
510 * mutex while holding spinlock, and must do so before the page
511 * is moved to swap cache, when its pagelock no longer protects
512 * the inode from eviction. But don't unlock the mutex until
513 * we've taken the spinlock, because shmem_unuse_inode() will
514 * prune a !swapped inode from the swaplist under both locks.
516 mutex_lock(&shmem_swaplist_mutex);
517 if (list_empty(&info->swaplist))
518 list_add_tail(&info->swaplist, &shmem_swaplist);
520 spin_lock(&info->lock);
521 mutex_unlock(&shmem_swaplist_mutex);
523 oswap = shmem_get_swap(info, index);
525 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
526 free_swap_and_cache(oswap);
527 shmem_put_swap(info, index, (swp_entry_t){0});
530 shmem_recalc_inode(inode);
532 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
533 delete_from_page_cache(page);
534 shmem_put_swap(info, index, swap);
536 swap_shmem_alloc(swap);
537 spin_unlock(&info->lock);
538 BUG_ON(page_mapped(page));
539 swap_writepage(page, wbc);
543 spin_unlock(&info->lock);
544 swapcache_free(swap, NULL);
546 set_page_dirty(page);
547 if (wbc->for_reclaim)
548 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
555 static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
559 if (!mpol || mpol->mode == MPOL_DEFAULT)
560 return; /* show nothing */
562 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
564 seq_printf(seq, ",mpol=%s", buffer);
567 static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
569 struct mempolicy *mpol = NULL;
571 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
574 spin_unlock(&sbinfo->stat_lock);
578 #endif /* CONFIG_TMPFS */
580 static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
581 struct shmem_inode_info *info, unsigned long idx)
583 struct mempolicy mpol, *spol;
584 struct vm_area_struct pvma;
587 spol = mpol_cond_copy(&mpol,
588 mpol_shared_policy_lookup(&info->policy, idx));
590 /* Create a pseudo vma that just contains the policy */
594 pvma.vm_policy = spol;
595 page = swapin_readahead(entry, gfp, &pvma, 0);
599 static struct page *shmem_alloc_page(gfp_t gfp,
600 struct shmem_inode_info *info, unsigned long idx)
602 struct vm_area_struct pvma;
604 /* Create a pseudo vma that just contains the policy */
608 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
611 * alloc_page_vma() will drop the shared policy reference
613 return alloc_page_vma(gfp, &pvma, 0);
615 #else /* !CONFIG_NUMA */
617 static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *p)
620 #endif /* CONFIG_TMPFS */
622 static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
623 struct shmem_inode_info *info, unsigned long idx)
625 return swapin_readahead(entry, gfp, NULL, 0);
628 static inline struct page *shmem_alloc_page(gfp_t gfp,
629 struct shmem_inode_info *info, unsigned long idx)
631 return alloc_page(gfp);
633 #endif /* CONFIG_NUMA */
635 #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
636 static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
643 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
645 * If we allocate a new one we do not mark it dirty. That's up to the
646 * vm. If we swap it in we mark it dirty since we also free the swap
647 * entry since a page cannot live in both the swap and page cache
649 static int shmem_getpage_gfp(struct inode *inode, pgoff_t idx,
650 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
652 struct address_space *mapping = inode->i_mapping;
653 struct shmem_inode_info *info = SHMEM_I(inode);
654 struct shmem_sb_info *sbinfo;
656 struct page *prealloc_page = NULL;
660 if (idx > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
663 page = find_lock_page(mapping, idx);
666 * Once we can get the page lock, it must be uptodate:
667 * if there were an error in reading back from swap,
668 * the page would not be inserted into the filecache.
670 BUG_ON(!PageUptodate(page));
675 * Try to preload while we can wait, to not make a habit of
676 * draining atomic reserves; but don't latch on to this cpu.
678 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
681 radix_tree_preload_end();
683 if (sgp != SGP_READ && !prealloc_page) {
684 prealloc_page = shmem_alloc_page(gfp, info, idx);
686 SetPageSwapBacked(prealloc_page);
687 if (mem_cgroup_cache_charge(prealloc_page,
688 current->mm, GFP_KERNEL)) {
689 page_cache_release(prealloc_page);
690 prealloc_page = NULL;
695 spin_lock(&info->lock);
696 shmem_recalc_inode(inode);
697 swap = shmem_get_swap(info, idx);
699 /* Look it up and read it in.. */
700 page = lookup_swap_cache(swap);
702 spin_unlock(&info->lock);
703 /* here we actually do the io */
705 *fault_type |= VM_FAULT_MAJOR;
706 page = shmem_swapin(swap, gfp, info, idx);
708 swp_entry_t nswap = shmem_get_swap(info, idx);
709 if (nswap.val == swap.val) {
715 wait_on_page_locked(page);
716 page_cache_release(page);
720 /* We have to do this with page locked to prevent races */
721 if (!trylock_page(page)) {
722 spin_unlock(&info->lock);
723 wait_on_page_locked(page);
724 page_cache_release(page);
727 if (PageWriteback(page)) {
728 spin_unlock(&info->lock);
729 wait_on_page_writeback(page);
731 page_cache_release(page);
734 if (!PageUptodate(page)) {
735 spin_unlock(&info->lock);
737 page_cache_release(page);
742 error = add_to_page_cache_locked(page, mapping,
745 spin_unlock(&info->lock);
746 if (error == -ENOMEM) {
748 * reclaim from proper memory cgroup and
749 * call memcg's OOM if needed.
751 error = mem_cgroup_shmem_charge_fallback(
752 page, current->mm, gfp);
755 page_cache_release(page);
760 page_cache_release(page);
764 delete_from_swap_cache(page);
765 shmem_put_swap(info, idx, (swp_entry_t){0});
767 spin_unlock(&info->lock);
768 set_page_dirty(page);
771 } else if (sgp == SGP_READ) {
772 page = find_get_page(mapping, idx);
773 if (page && !trylock_page(page)) {
774 spin_unlock(&info->lock);
775 wait_on_page_locked(page);
776 page_cache_release(page);
779 spin_unlock(&info->lock);
781 } else if (prealloc_page) {
782 sbinfo = SHMEM_SB(inode->i_sb);
783 if (sbinfo->max_blocks) {
784 if (percpu_counter_compare(&sbinfo->used_blocks,
785 sbinfo->max_blocks) >= 0 ||
786 shmem_acct_block(info->flags))
788 percpu_counter_inc(&sbinfo->used_blocks);
789 inode->i_blocks += BLOCKS_PER_PAGE;
790 } else if (shmem_acct_block(info->flags))
793 page = prealloc_page;
794 prealloc_page = NULL;
796 swap = shmem_get_swap(info, idx);
798 mem_cgroup_uncharge_cache_page(page);
800 error = add_to_page_cache_lru(page, mapping,
803 * At add_to_page_cache_lru() failure,
804 * uncharge will be done automatically.
806 if (swap.val || error) {
807 shmem_unacct_blocks(info->flags, 1);
808 shmem_free_blocks(inode, 1);
809 spin_unlock(&info->lock);
810 page_cache_release(page);
815 spin_unlock(&info->lock);
816 clear_highpage(page);
817 flush_dcache_page(page);
818 SetPageUptodate(page);
819 if (sgp == SGP_DIRTY)
820 set_page_dirty(page);
823 spin_unlock(&info->lock);
832 mem_cgroup_uncharge_cache_page(prealloc_page);
833 page_cache_release(prealloc_page);
839 * Perhaps the page was brought in from swap between find_lock_page
840 * and taking info->lock? We allow for that at add_to_page_cache_lru,
841 * but must also avoid reporting a spurious ENOSPC while working on a
844 page = find_get_page(mapping, idx);
845 spin_unlock(&info->lock);
847 page_cache_release(page);
854 static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
856 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
858 int ret = VM_FAULT_LOCKED;
860 if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
861 return VM_FAULT_SIGBUS;
863 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
865 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
867 if (ret & VM_FAULT_MAJOR) {
868 count_vm_event(PGMAJFAULT);
869 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
875 static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
877 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
878 return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
881 static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
884 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
887 idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
888 return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
892 int shmem_lock(struct file *file, int lock, struct user_struct *user)
894 struct inode *inode = file->f_path.dentry->d_inode;
895 struct shmem_inode_info *info = SHMEM_I(inode);
896 int retval = -ENOMEM;
898 spin_lock(&info->lock);
899 if (lock && !(info->flags & VM_LOCKED)) {
900 if (!user_shm_lock(inode->i_size, user))
902 info->flags |= VM_LOCKED;
903 mapping_set_unevictable(file->f_mapping);
905 if (!lock && (info->flags & VM_LOCKED) && user) {
906 user_shm_unlock(inode->i_size, user);
907 info->flags &= ~VM_LOCKED;
908 mapping_clear_unevictable(file->f_mapping);
909 scan_mapping_unevictable_pages(file->f_mapping);
914 spin_unlock(&info->lock);
918 static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
921 vma->vm_ops = &shmem_vm_ops;
922 vma->vm_flags |= VM_CAN_NONLINEAR;
926 static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
927 int mode, dev_t dev, unsigned long flags)
930 struct shmem_inode_info *info;
931 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
933 if (shmem_reserve_inode(sb))
936 inode = new_inode(sb);
938 inode->i_ino = get_next_ino();
939 inode_init_owner(inode, dir, mode);
941 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
942 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
943 inode->i_generation = get_seconds();
944 info = SHMEM_I(inode);
945 memset(info, 0, (char *)inode - (char *)info);
946 spin_lock_init(&info->lock);
947 info->flags = flags & VM_NORESERVE;
948 INIT_LIST_HEAD(&info->swaplist);
949 INIT_LIST_HEAD(&info->xattr_list);
952 switch (mode & S_IFMT) {
954 inode->i_op = &shmem_special_inode_operations;
955 init_special_inode(inode, mode, dev);
958 inode->i_mapping->a_ops = &shmem_aops;
959 inode->i_op = &shmem_inode_operations;
960 inode->i_fop = &shmem_file_operations;
961 mpol_shared_policy_init(&info->policy,
962 shmem_get_sbmpol(sbinfo));
966 /* Some things misbehave if size == 0 on a directory */
967 inode->i_size = 2 * BOGO_DIRENT_SIZE;
968 inode->i_op = &shmem_dir_inode_operations;
969 inode->i_fop = &simple_dir_operations;
973 * Must not load anything in the rbtree,
974 * mpol_free_shared_policy will not be called.
976 mpol_shared_policy_init(&info->policy, NULL);
980 shmem_free_inode(sb);
985 static const struct inode_operations shmem_symlink_inode_operations;
986 static const struct inode_operations shmem_symlink_inline_operations;
989 shmem_write_begin(struct file *file, struct address_space *mapping,
990 loff_t pos, unsigned len, unsigned flags,
991 struct page **pagep, void **fsdata)
993 struct inode *inode = mapping->host;
994 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
995 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
999 shmem_write_end(struct file *file, struct address_space *mapping,
1000 loff_t pos, unsigned len, unsigned copied,
1001 struct page *page, void *fsdata)
1003 struct inode *inode = mapping->host;
1005 if (pos + copied > inode->i_size)
1006 i_size_write(inode, pos + copied);
1008 set_page_dirty(page);
1010 page_cache_release(page);
1015 static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1017 struct inode *inode = filp->f_path.dentry->d_inode;
1018 struct address_space *mapping = inode->i_mapping;
1019 unsigned long index, offset;
1020 enum sgp_type sgp = SGP_READ;
1023 * Might this read be for a stacking filesystem? Then when reading
1024 * holes of a sparse file, we actually need to allocate those pages,
1025 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1027 if (segment_eq(get_fs(), KERNEL_DS))
1030 index = *ppos >> PAGE_CACHE_SHIFT;
1031 offset = *ppos & ~PAGE_CACHE_MASK;
1034 struct page *page = NULL;
1035 unsigned long end_index, nr, ret;
1036 loff_t i_size = i_size_read(inode);
1038 end_index = i_size >> PAGE_CACHE_SHIFT;
1039 if (index > end_index)
1041 if (index == end_index) {
1042 nr = i_size & ~PAGE_CACHE_MASK;
1047 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
1049 if (desc->error == -EINVAL)
1057 * We must evaluate after, since reads (unlike writes)
1058 * are called without i_mutex protection against truncate
1060 nr = PAGE_CACHE_SIZE;
1061 i_size = i_size_read(inode);
1062 end_index = i_size >> PAGE_CACHE_SHIFT;
1063 if (index == end_index) {
1064 nr = i_size & ~PAGE_CACHE_MASK;
1067 page_cache_release(page);
1075 * If users can be writing to this page using arbitrary
1076 * virtual addresses, take care about potential aliasing
1077 * before reading the page on the kernel side.
1079 if (mapping_writably_mapped(mapping))
1080 flush_dcache_page(page);
1082 * Mark the page accessed if we read the beginning.
1085 mark_page_accessed(page);
1087 page = ZERO_PAGE(0);
1088 page_cache_get(page);
1092 * Ok, we have the page, and it's up-to-date, so
1093 * now we can copy it to user space...
1095 * The actor routine returns how many bytes were actually used..
1096 * NOTE! This may not be the same as how much of a user buffer
1097 * we filled up (we may be padding etc), so we can only update
1098 * "pos" here (the actor routine has to update the user buffer
1099 * pointers and the remaining count).
1101 ret = actor(desc, page, offset, nr);
1103 index += offset >> PAGE_CACHE_SHIFT;
1104 offset &= ~PAGE_CACHE_MASK;
1106 page_cache_release(page);
1107 if (ret != nr || !desc->count)
1113 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1114 file_accessed(filp);
1117 static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1118 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
1120 struct file *filp = iocb->ki_filp;
1124 loff_t *ppos = &iocb->ki_pos;
1126 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1130 for (seg = 0; seg < nr_segs; seg++) {
1131 read_descriptor_t desc;
1134 desc.arg.buf = iov[seg].iov_base;
1135 desc.count = iov[seg].iov_len;
1136 if (desc.count == 0)
1139 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1140 retval += desc.written;
1142 retval = retval ?: desc.error;
1151 static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1152 struct pipe_inode_info *pipe, size_t len,
1155 struct address_space *mapping = in->f_mapping;
1156 struct inode *inode = mapping->host;
1157 unsigned int loff, nr_pages, req_pages;
1158 struct page *pages[PIPE_DEF_BUFFERS];
1159 struct partial_page partial[PIPE_DEF_BUFFERS];
1161 pgoff_t index, end_index;
1164 struct splice_pipe_desc spd = {
1168 .ops = &page_cache_pipe_buf_ops,
1169 .spd_release = spd_release_page,
1172 isize = i_size_read(inode);
1173 if (unlikely(*ppos >= isize))
1176 left = isize - *ppos;
1177 if (unlikely(left < len))
1180 if (splice_grow_spd(pipe, &spd))
1183 index = *ppos >> PAGE_CACHE_SHIFT;
1184 loff = *ppos & ~PAGE_CACHE_MASK;
1185 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1186 nr_pages = min(req_pages, pipe->buffers);
1188 spd.nr_pages = find_get_pages_contig(mapping, index,
1189 nr_pages, spd.pages);
1190 index += spd.nr_pages;
1193 while (spd.nr_pages < nr_pages) {
1194 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1198 spd.pages[spd.nr_pages++] = page;
1202 index = *ppos >> PAGE_CACHE_SHIFT;
1203 nr_pages = spd.nr_pages;
1206 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1207 unsigned int this_len;
1212 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1213 page = spd.pages[page_nr];
1215 if (!PageUptodate(page) || page->mapping != mapping) {
1216 error = shmem_getpage(inode, index, &page,
1221 page_cache_release(spd.pages[page_nr]);
1222 spd.pages[page_nr] = page;
1225 isize = i_size_read(inode);
1226 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1227 if (unlikely(!isize || index > end_index))
1230 if (end_index == index) {
1233 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1237 this_len = min(this_len, plen - loff);
1241 spd.partial[page_nr].offset = loff;
1242 spd.partial[page_nr].len = this_len;
1249 while (page_nr < nr_pages)
1250 page_cache_release(spd.pages[page_nr++]);
1253 error = splice_to_pipe(pipe, &spd);
1255 splice_shrink_spd(pipe, &spd);
1264 static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1266 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1268 buf->f_type = TMPFS_MAGIC;
1269 buf->f_bsize = PAGE_CACHE_SIZE;
1270 buf->f_namelen = NAME_MAX;
1271 if (sbinfo->max_blocks) {
1272 buf->f_blocks = sbinfo->max_blocks;
1273 buf->f_bavail = buf->f_bfree =
1274 sbinfo->max_blocks - percpu_counter_sum(&sbinfo->used_blocks);
1276 if (sbinfo->max_inodes) {
1277 buf->f_files = sbinfo->max_inodes;
1278 buf->f_ffree = sbinfo->free_inodes;
1280 /* else leave those fields 0 like simple_statfs */
1285 * File creation. Allocate an inode, and we're done..
1288 shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1290 struct inode *inode;
1291 int error = -ENOSPC;
1293 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
1295 error = security_inode_init_security(inode, dir,
1296 &dentry->d_name, NULL,
1299 if (error != -EOPNOTSUPP) {
1304 #ifdef CONFIG_TMPFS_POSIX_ACL
1305 error = generic_acl_init(inode, dir);
1313 dir->i_size += BOGO_DIRENT_SIZE;
1314 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1315 d_instantiate(dentry, inode);
1316 dget(dentry); /* Extra count - pin the dentry in core */
1321 static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1325 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1331 static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1332 struct nameidata *nd)
1334 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1340 static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1342 struct inode *inode = old_dentry->d_inode;
1346 * No ordinary (disk based) filesystem counts links as inodes;
1347 * but each new link needs a new dentry, pinning lowmem, and
1348 * tmpfs dentries cannot be pruned until they are unlinked.
1350 ret = shmem_reserve_inode(inode->i_sb);
1354 dir->i_size += BOGO_DIRENT_SIZE;
1355 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1357 ihold(inode); /* New dentry reference */
1358 dget(dentry); /* Extra pinning count for the created dentry */
1359 d_instantiate(dentry, inode);
1364 static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1366 struct inode *inode = dentry->d_inode;
1368 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1369 shmem_free_inode(inode->i_sb);
1371 dir->i_size -= BOGO_DIRENT_SIZE;
1372 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1374 dput(dentry); /* Undo the count from "create" - this does all the work */
1378 static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1380 if (!simple_empty(dentry))
1383 drop_nlink(dentry->d_inode);
1385 return shmem_unlink(dir, dentry);
1389 * The VFS layer already does all the dentry stuff for rename,
1390 * we just have to decrement the usage count for the target if
1391 * it exists so that the VFS layer correctly free's it when it
1394 static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1396 struct inode *inode = old_dentry->d_inode;
1397 int they_are_dirs = S_ISDIR(inode->i_mode);
1399 if (!simple_empty(new_dentry))
1402 if (new_dentry->d_inode) {
1403 (void) shmem_unlink(new_dir, new_dentry);
1405 drop_nlink(old_dir);
1406 } else if (they_are_dirs) {
1407 drop_nlink(old_dir);
1411 old_dir->i_size -= BOGO_DIRENT_SIZE;
1412 new_dir->i_size += BOGO_DIRENT_SIZE;
1413 old_dir->i_ctime = old_dir->i_mtime =
1414 new_dir->i_ctime = new_dir->i_mtime =
1415 inode->i_ctime = CURRENT_TIME;
1419 static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1423 struct inode *inode;
1426 struct shmem_inode_info *info;
1428 len = strlen(symname) + 1;
1429 if (len > PAGE_CACHE_SIZE)
1430 return -ENAMETOOLONG;
1432 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
1436 error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
1439 if (error != -EOPNOTSUPP) {
1446 info = SHMEM_I(inode);
1447 inode->i_size = len-1;
1448 if (len <= SHMEM_SYMLINK_INLINE_LEN) {
1450 memcpy(info->inline_symlink, symname, len);
1451 inode->i_op = &shmem_symlink_inline_operations;
1453 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1458 inode->i_mapping->a_ops = &shmem_aops;
1459 inode->i_op = &shmem_symlink_inode_operations;
1460 kaddr = kmap_atomic(page, KM_USER0);
1461 memcpy(kaddr, symname, len);
1462 kunmap_atomic(kaddr, KM_USER0);
1463 set_page_dirty(page);
1465 page_cache_release(page);
1467 dir->i_size += BOGO_DIRENT_SIZE;
1468 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1469 d_instantiate(dentry, inode);
1474 static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
1476 nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
1480 static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1482 struct page *page = NULL;
1483 int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1484 nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
1490 static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1492 if (!IS_ERR(nd_get_link(nd))) {
1493 struct page *page = cookie;
1495 mark_page_accessed(page);
1496 page_cache_release(page);
1500 #ifdef CONFIG_TMPFS_XATTR
1502 * Superblocks without xattr inode operations may get some security.* xattr
1503 * support from the LSM "for free". As soon as we have any other xattrs
1504 * like ACLs, we also need to implement the security.* handlers at
1505 * filesystem level, though.
1508 static int shmem_xattr_get(struct dentry *dentry, const char *name,
1509 void *buffer, size_t size)
1511 struct shmem_inode_info *info;
1512 struct shmem_xattr *xattr;
1515 info = SHMEM_I(dentry->d_inode);
1517 spin_lock(&info->lock);
1518 list_for_each_entry(xattr, &info->xattr_list, list) {
1519 if (strcmp(name, xattr->name))
1524 if (size < xattr->size)
1527 memcpy(buffer, xattr->value, xattr->size);
1531 spin_unlock(&info->lock);
1535 static int shmem_xattr_set(struct dentry *dentry, const char *name,
1536 const void *value, size_t size, int flags)
1538 struct inode *inode = dentry->d_inode;
1539 struct shmem_inode_info *info = SHMEM_I(inode);
1540 struct shmem_xattr *xattr;
1541 struct shmem_xattr *new_xattr = NULL;
1545 /* value == NULL means remove */
1548 len = sizeof(*new_xattr) + size;
1549 if (len <= sizeof(*new_xattr))
1552 new_xattr = kmalloc(len, GFP_KERNEL);
1556 new_xattr->name = kstrdup(name, GFP_KERNEL);
1557 if (!new_xattr->name) {
1562 new_xattr->size = size;
1563 memcpy(new_xattr->value, value, size);
1566 spin_lock(&info->lock);
1567 list_for_each_entry(xattr, &info->xattr_list, list) {
1568 if (!strcmp(name, xattr->name)) {
1569 if (flags & XATTR_CREATE) {
1572 } else if (new_xattr) {
1573 list_replace(&xattr->list, &new_xattr->list);
1575 list_del(&xattr->list);
1580 if (flags & XATTR_REPLACE) {
1584 list_add(&new_xattr->list, &info->xattr_list);
1588 spin_unlock(&info->lock);
1596 static const struct xattr_handler *shmem_xattr_handlers[] = {
1597 #ifdef CONFIG_TMPFS_POSIX_ACL
1598 &generic_acl_access_handler,
1599 &generic_acl_default_handler,
1604 static int shmem_xattr_validate(const char *name)
1606 struct { const char *prefix; size_t len; } arr[] = {
1607 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1608 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1612 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1613 size_t preflen = arr[i].len;
1614 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1623 static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1624 void *buffer, size_t size)
1629 * If this is a request for a synthetic attribute in the system.*
1630 * namespace use the generic infrastructure to resolve a handler
1631 * for it via sb->s_xattr.
1633 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1634 return generic_getxattr(dentry, name, buffer, size);
1636 err = shmem_xattr_validate(name);
1640 return shmem_xattr_get(dentry, name, buffer, size);
1643 static int shmem_setxattr(struct dentry *dentry, const char *name,
1644 const void *value, size_t size, int flags)
1649 * If this is a request for a synthetic attribute in the system.*
1650 * namespace use the generic infrastructure to resolve a handler
1651 * for it via sb->s_xattr.
1653 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1654 return generic_setxattr(dentry, name, value, size, flags);
1656 err = shmem_xattr_validate(name);
1661 value = ""; /* empty EA, do not remove */
1663 return shmem_xattr_set(dentry, name, value, size, flags);
1667 static int shmem_removexattr(struct dentry *dentry, const char *name)
1672 * If this is a request for a synthetic attribute in the system.*
1673 * namespace use the generic infrastructure to resolve a handler
1674 * for it via sb->s_xattr.
1676 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1677 return generic_removexattr(dentry, name);
1679 err = shmem_xattr_validate(name);
1683 return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
1686 static bool xattr_is_trusted(const char *name)
1688 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1691 static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
1693 bool trusted = capable(CAP_SYS_ADMIN);
1694 struct shmem_xattr *xattr;
1695 struct shmem_inode_info *info;
1698 info = SHMEM_I(dentry->d_inode);
1700 spin_lock(&info->lock);
1701 list_for_each_entry(xattr, &info->xattr_list, list) {
1704 /* skip "trusted." attributes for unprivileged callers */
1705 if (!trusted && xattr_is_trusted(xattr->name))
1708 len = strlen(xattr->name) + 1;
1715 memcpy(buffer, xattr->name, len);
1719 spin_unlock(&info->lock);
1723 #endif /* CONFIG_TMPFS_XATTR */
1725 static const struct inode_operations shmem_symlink_inline_operations = {
1726 .readlink = generic_readlink,
1727 .follow_link = shmem_follow_link_inline,
1728 #ifdef CONFIG_TMPFS_XATTR
1729 .setxattr = shmem_setxattr,
1730 .getxattr = shmem_getxattr,
1731 .listxattr = shmem_listxattr,
1732 .removexattr = shmem_removexattr,
1736 static const struct inode_operations shmem_symlink_inode_operations = {
1737 .readlink = generic_readlink,
1738 .follow_link = shmem_follow_link,
1739 .put_link = shmem_put_link,
1740 #ifdef CONFIG_TMPFS_XATTR
1741 .setxattr = shmem_setxattr,
1742 .getxattr = shmem_getxattr,
1743 .listxattr = shmem_listxattr,
1744 .removexattr = shmem_removexattr,
1748 static struct dentry *shmem_get_parent(struct dentry *child)
1750 return ERR_PTR(-ESTALE);
1753 static int shmem_match(struct inode *ino, void *vfh)
1757 inum = (inum << 32) | fh[1];
1758 return ino->i_ino == inum && fh[0] == ino->i_generation;
1761 static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
1762 struct fid *fid, int fh_len, int fh_type)
1764 struct inode *inode;
1765 struct dentry *dentry = NULL;
1766 u64 inum = fid->raw[2];
1767 inum = (inum << 32) | fid->raw[1];
1772 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
1773 shmem_match, fid->raw);
1775 dentry = d_find_alias(inode);
1782 static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
1785 struct inode *inode = dentry->d_inode;
1792 if (inode_unhashed(inode)) {
1793 /* Unfortunately insert_inode_hash is not idempotent,
1794 * so as we hash inodes here rather than at creation
1795 * time, we need a lock to ensure we only try
1798 static DEFINE_SPINLOCK(lock);
1800 if (inode_unhashed(inode))
1801 __insert_inode_hash(inode,
1802 inode->i_ino + inode->i_generation);
1806 fh[0] = inode->i_generation;
1807 fh[1] = inode->i_ino;
1808 fh[2] = ((__u64)inode->i_ino) >> 32;
1814 static const struct export_operations shmem_export_ops = {
1815 .get_parent = shmem_get_parent,
1816 .encode_fh = shmem_encode_fh,
1817 .fh_to_dentry = shmem_fh_to_dentry,
1820 static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
1823 char *this_char, *value, *rest;
1825 while (options != NULL) {
1826 this_char = options;
1829 * NUL-terminate this option: unfortunately,
1830 * mount options form a comma-separated list,
1831 * but mpol's nodelist may also contain commas.
1833 options = strchr(options, ',');
1834 if (options == NULL)
1837 if (!isdigit(*options)) {
1844 if ((value = strchr(this_char,'=')) != NULL) {
1848 "tmpfs: No value for mount option '%s'\n",
1853 if (!strcmp(this_char,"size")) {
1854 unsigned long long size;
1855 size = memparse(value,&rest);
1857 size <<= PAGE_SHIFT;
1858 size *= totalram_pages;
1864 sbinfo->max_blocks =
1865 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
1866 } else if (!strcmp(this_char,"nr_blocks")) {
1867 sbinfo->max_blocks = memparse(value, &rest);
1870 } else if (!strcmp(this_char,"nr_inodes")) {
1871 sbinfo->max_inodes = memparse(value, &rest);
1874 } else if (!strcmp(this_char,"mode")) {
1877 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
1880 } else if (!strcmp(this_char,"uid")) {
1883 sbinfo->uid = simple_strtoul(value, &rest, 0);
1886 } else if (!strcmp(this_char,"gid")) {
1889 sbinfo->gid = simple_strtoul(value, &rest, 0);
1892 } else if (!strcmp(this_char,"mpol")) {
1893 if (mpol_parse_str(value, &sbinfo->mpol, 1))
1896 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
1904 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
1910 static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
1912 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1913 struct shmem_sb_info config = *sbinfo;
1914 unsigned long inodes;
1915 int error = -EINVAL;
1917 if (shmem_parse_options(data, &config, true))
1920 spin_lock(&sbinfo->stat_lock);
1921 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
1922 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
1924 if (config.max_inodes < inodes)
1927 * Those tests also disallow limited->unlimited while any are in
1928 * use, so i_blocks will always be zero when max_blocks is zero;
1929 * but we must separately disallow unlimited->limited, because
1930 * in that case we have no record of how much is already in use.
1932 if (config.max_blocks && !sbinfo->max_blocks)
1934 if (config.max_inodes && !sbinfo->max_inodes)
1938 sbinfo->max_blocks = config.max_blocks;
1939 sbinfo->max_inodes = config.max_inodes;
1940 sbinfo->free_inodes = config.max_inodes - inodes;
1942 mpol_put(sbinfo->mpol);
1943 sbinfo->mpol = config.mpol; /* transfers initial ref */
1945 spin_unlock(&sbinfo->stat_lock);
1949 static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
1951 struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
1953 if (sbinfo->max_blocks != shmem_default_max_blocks())
1954 seq_printf(seq, ",size=%luk",
1955 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
1956 if (sbinfo->max_inodes != shmem_default_max_inodes())
1957 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
1958 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
1959 seq_printf(seq, ",mode=%03o", sbinfo->mode);
1960 if (sbinfo->uid != 0)
1961 seq_printf(seq, ",uid=%u", sbinfo->uid);
1962 if (sbinfo->gid != 0)
1963 seq_printf(seq, ",gid=%u", sbinfo->gid);
1964 shmem_show_mpol(seq, sbinfo->mpol);
1967 #endif /* CONFIG_TMPFS */
1969 static void shmem_put_super(struct super_block *sb)
1971 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1973 percpu_counter_destroy(&sbinfo->used_blocks);
1975 sb->s_fs_info = NULL;
1978 int shmem_fill_super(struct super_block *sb, void *data, int silent)
1980 struct inode *inode;
1981 struct dentry *root;
1982 struct shmem_sb_info *sbinfo;
1985 /* Round up to L1_CACHE_BYTES to resist false sharing */
1986 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
1987 L1_CACHE_BYTES), GFP_KERNEL);
1991 sbinfo->mode = S_IRWXUGO | S_ISVTX;
1992 sbinfo->uid = current_fsuid();
1993 sbinfo->gid = current_fsgid();
1994 sb->s_fs_info = sbinfo;
1998 * Per default we only allow half of the physical ram per
1999 * tmpfs instance, limiting inodes to one per page of lowmem;
2000 * but the internal instance is left unlimited.
2002 if (!(sb->s_flags & MS_NOUSER)) {
2003 sbinfo->max_blocks = shmem_default_max_blocks();
2004 sbinfo->max_inodes = shmem_default_max_inodes();
2005 if (shmem_parse_options(data, sbinfo, false)) {
2010 sb->s_export_op = &shmem_export_ops;
2012 sb->s_flags |= MS_NOUSER;
2015 spin_lock_init(&sbinfo->stat_lock);
2016 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2018 sbinfo->free_inodes = sbinfo->max_inodes;
2020 sb->s_maxbytes = MAX_LFS_FILESIZE;
2021 sb->s_blocksize = PAGE_CACHE_SIZE;
2022 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2023 sb->s_magic = TMPFS_MAGIC;
2024 sb->s_op = &shmem_ops;
2025 sb->s_time_gran = 1;
2026 #ifdef CONFIG_TMPFS_XATTR
2027 sb->s_xattr = shmem_xattr_handlers;
2029 #ifdef CONFIG_TMPFS_POSIX_ACL
2030 sb->s_flags |= MS_POSIXACL;
2033 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
2036 inode->i_uid = sbinfo->uid;
2037 inode->i_gid = sbinfo->gid;
2038 root = d_alloc_root(inode);
2047 shmem_put_super(sb);
2051 static struct kmem_cache *shmem_inode_cachep;
2053 static struct inode *shmem_alloc_inode(struct super_block *sb)
2055 struct shmem_inode_info *p;
2056 p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2059 return &p->vfs_inode;
2062 static void shmem_i_callback(struct rcu_head *head)
2064 struct inode *inode = container_of(head, struct inode, i_rcu);
2065 INIT_LIST_HEAD(&inode->i_dentry);
2066 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2069 static void shmem_destroy_inode(struct inode *inode)
2071 if ((inode->i_mode & S_IFMT) == S_IFREG) {
2072 /* only struct inode is valid if it's an inline symlink */
2073 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2075 call_rcu(&inode->i_rcu, shmem_i_callback);
2078 static void init_once(void *foo)
2080 struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
2082 inode_init_once(&p->vfs_inode);
2085 static int init_inodecache(void)
2087 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2088 sizeof(struct shmem_inode_info),
2089 0, SLAB_PANIC, init_once);
2093 static void destroy_inodecache(void)
2095 kmem_cache_destroy(shmem_inode_cachep);
2098 static const struct address_space_operations shmem_aops = {
2099 .writepage = shmem_writepage,
2100 .set_page_dirty = __set_page_dirty_no_writeback,
2102 .write_begin = shmem_write_begin,
2103 .write_end = shmem_write_end,
2105 .migratepage = migrate_page,
2106 .error_remove_page = generic_error_remove_page,
2109 static const struct file_operations shmem_file_operations = {
2112 .llseek = generic_file_llseek,
2113 .read = do_sync_read,
2114 .write = do_sync_write,
2115 .aio_read = shmem_file_aio_read,
2116 .aio_write = generic_file_aio_write,
2117 .fsync = noop_fsync,
2118 .splice_read = shmem_file_splice_read,
2119 .splice_write = generic_file_splice_write,
2123 static const struct inode_operations shmem_inode_operations = {
2124 .setattr = shmem_setattr,
2125 .truncate_range = shmem_truncate_range,
2126 #ifdef CONFIG_TMPFS_XATTR
2127 .setxattr = shmem_setxattr,
2128 .getxattr = shmem_getxattr,
2129 .listxattr = shmem_listxattr,
2130 .removexattr = shmem_removexattr,
2134 static const struct inode_operations shmem_dir_inode_operations = {
2136 .create = shmem_create,
2137 .lookup = simple_lookup,
2139 .unlink = shmem_unlink,
2140 .symlink = shmem_symlink,
2141 .mkdir = shmem_mkdir,
2142 .rmdir = shmem_rmdir,
2143 .mknod = shmem_mknod,
2144 .rename = shmem_rename,
2146 #ifdef CONFIG_TMPFS_XATTR
2147 .setxattr = shmem_setxattr,
2148 .getxattr = shmem_getxattr,
2149 .listxattr = shmem_listxattr,
2150 .removexattr = shmem_removexattr,
2152 #ifdef CONFIG_TMPFS_POSIX_ACL
2153 .setattr = shmem_setattr,
2157 static const struct inode_operations shmem_special_inode_operations = {
2158 #ifdef CONFIG_TMPFS_XATTR
2159 .setxattr = shmem_setxattr,
2160 .getxattr = shmem_getxattr,
2161 .listxattr = shmem_listxattr,
2162 .removexattr = shmem_removexattr,
2164 #ifdef CONFIG_TMPFS_POSIX_ACL
2165 .setattr = shmem_setattr,
2169 static const struct super_operations shmem_ops = {
2170 .alloc_inode = shmem_alloc_inode,
2171 .destroy_inode = shmem_destroy_inode,
2173 .statfs = shmem_statfs,
2174 .remount_fs = shmem_remount_fs,
2175 .show_options = shmem_show_options,
2177 .evict_inode = shmem_evict_inode,
2178 .drop_inode = generic_delete_inode,
2179 .put_super = shmem_put_super,
2182 static const struct vm_operations_struct shmem_vm_ops = {
2183 .fault = shmem_fault,
2185 .set_policy = shmem_set_policy,
2186 .get_policy = shmem_get_policy,
2191 static struct dentry *shmem_mount(struct file_system_type *fs_type,
2192 int flags, const char *dev_name, void *data)
2194 return mount_nodev(fs_type, flags, data, shmem_fill_super);
2197 static struct file_system_type tmpfs_fs_type = {
2198 .owner = THIS_MODULE,
2200 .mount = shmem_mount,
2201 .kill_sb = kill_litter_super,
2204 int __init init_tmpfs(void)
2208 error = bdi_init(&shmem_backing_dev_info);
2212 error = init_inodecache();
2216 error = register_filesystem(&tmpfs_fs_type);
2218 printk(KERN_ERR "Could not register tmpfs\n");
2222 shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
2223 tmpfs_fs_type.name, NULL);
2224 if (IS_ERR(shm_mnt)) {
2225 error = PTR_ERR(shm_mnt);
2226 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2232 unregister_filesystem(&tmpfs_fs_type);
2234 destroy_inodecache();
2236 bdi_destroy(&shmem_backing_dev_info);
2238 shm_mnt = ERR_PTR(error);
2242 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
2244 * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
2245 * @inode: the inode to be searched
2246 * @pgoff: the offset to be searched
2247 * @pagep: the pointer for the found page to be stored
2248 * @ent: the pointer for the found swap entry to be stored
2250 * If a page is found, refcount of it is incremented. Callers should handle
2253 void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
2254 struct page **pagep, swp_entry_t *ent)
2256 swp_entry_t entry = { .val = 0 };
2257 struct page *page = NULL;
2258 struct shmem_inode_info *info = SHMEM_I(inode);
2260 if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
2263 spin_lock(&info->lock);
2265 entry = shmem_get_swap(info, pgoff);
2267 page = find_get_page(&swapper_space, entry.val);
2270 page = find_get_page(inode->i_mapping, pgoff);
2271 spin_unlock(&info->lock);
2278 #else /* !CONFIG_SHMEM */
2281 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2283 * This is intended for small system where the benefits of the full
2284 * shmem code (swap-backed and resource-limited) are outweighed by
2285 * their complexity. On systems without swap this code should be
2286 * effectively equivalent, but much lighter weight.
2289 #include <linux/ramfs.h>
2291 static struct file_system_type tmpfs_fs_type = {
2293 .mount = ramfs_mount,
2294 .kill_sb = kill_litter_super,
2297 int __init init_tmpfs(void)
2299 BUG_ON(register_filesystem(&tmpfs_fs_type) != 0);
2301 shm_mnt = kern_mount(&tmpfs_fs_type);
2302 BUG_ON(IS_ERR(shm_mnt));
2307 int shmem_unuse(swp_entry_t entry, struct page *page)
2312 int shmem_lock(struct file *file, int lock, struct user_struct *user)
2317 void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
2319 truncate_inode_pages_range(inode->i_mapping, start, end);
2321 EXPORT_SYMBOL_GPL(shmem_truncate_range);
2323 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
2325 * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
2326 * @inode: the inode to be searched
2327 * @pgoff: the offset to be searched
2328 * @pagep: the pointer for the found page to be stored
2329 * @ent: the pointer for the found swap entry to be stored
2331 * If a page is found, refcount of it is incremented. Callers should handle
2334 void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
2335 struct page **pagep, swp_entry_t *ent)
2337 struct page *page = NULL;
2339 if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
2341 page = find_get_page(inode->i_mapping, pgoff);
2344 *ent = (swp_entry_t){ .val = 0 };
2348 #define shmem_vm_ops generic_file_vm_ops
2349 #define shmem_file_operations ramfs_file_operations
2350 #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
2351 #define shmem_acct_size(flags, size) 0
2352 #define shmem_unacct_size(flags, size) do {} while (0)
2354 #endif /* CONFIG_SHMEM */
2359 * shmem_file_setup - get an unlinked file living in tmpfs
2360 * @name: name for dentry (to be seen in /proc/<pid>/maps
2361 * @size: size to be set for the file
2362 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
2364 struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
2368 struct inode *inode;
2370 struct dentry *root;
2373 if (IS_ERR(shm_mnt))
2374 return (void *)shm_mnt;
2376 if (size < 0 || size > MAX_LFS_FILESIZE)
2377 return ERR_PTR(-EINVAL);
2379 if (shmem_acct_size(flags, size))
2380 return ERR_PTR(-ENOMEM);
2384 this.len = strlen(name);
2385 this.hash = 0; /* will go */
2386 root = shm_mnt->mnt_root;
2387 path.dentry = d_alloc(root, &this);
2390 path.mnt = mntget(shm_mnt);
2393 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
2397 d_instantiate(path.dentry, inode);
2398 inode->i_size = size;
2399 inode->i_nlink = 0; /* It is unlinked */
2401 error = ramfs_nommu_expand_for_mapping(inode, size);
2407 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
2408 &shmem_file_operations);
2417 shmem_unacct_size(flags, size);
2418 return ERR_PTR(error);
2420 EXPORT_SYMBOL_GPL(shmem_file_setup);
2423 * shmem_zero_setup - setup a shared anonymous mapping
2424 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2426 int shmem_zero_setup(struct vm_area_struct *vma)
2429 loff_t size = vma->vm_end - vma->vm_start;
2431 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2433 return PTR_ERR(file);
2437 vma->vm_file = file;
2438 vma->vm_ops = &shmem_vm_ops;
2439 vma->vm_flags |= VM_CAN_NONLINEAR;
2444 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2445 * @mapping: the page's address_space
2446 * @index: the page index
2447 * @gfp: the page allocator flags to use if allocating
2449 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2450 * with any new page allocations done using the specified allocation flags.
2451 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2452 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2453 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2455 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2456 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
2458 struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2459 pgoff_t index, gfp_t gfp)
2462 struct inode *inode = mapping->host;
2466 BUG_ON(mapping->a_ops != &shmem_aops);
2467 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2469 page = ERR_PTR(error);
2475 * The tiny !SHMEM case uses ramfs without swap
2477 return read_cache_page_gfp(mapping, index, gfp);
2480 EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);