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/swap.h>
33 static struct vfsmount *shm_mnt;
37 * This virtual memory filesystem is heavily based on the ramfs. It
38 * extends ramfs by the ability to use swap and honor resource limits
39 * which makes it a completely usable filesystem.
42 #include <linux/xattr.h>
43 #include <linux/exportfs.h>
44 #include <linux/posix_acl.h>
45 #include <linux/generic_acl.h>
46 #include <linux/mman.h>
47 #include <linux/string.h>
48 #include <linux/slab.h>
49 #include <linux/backing-dev.h>
50 #include <linux/shmem_fs.h>
51 #include <linux/writeback.h>
52 #include <linux/blkdev.h>
53 #include <linux/pagevec.h>
54 #include <linux/percpu_counter.h>
55 #include <linux/splice.h>
56 #include <linux/security.h>
57 #include <linux/swapops.h>
58 #include <linux/mempolicy.h>
59 #include <linux/namei.h>
60 #include <linux/ctype.h>
61 #include <linux/migrate.h>
62 #include <linux/highmem.h>
63 #include <linux/seq_file.h>
64 #include <linux/magic.h>
66 #include <asm/uaccess.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 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 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};
242 * Replace item expected in radix tree by a new item, while holding tree lock.
244 static int shmem_radix_tree_replace(struct address_space *mapping,
245 pgoff_t index, void *expected, void *replacement)
250 VM_BUG_ON(!expected);
251 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
253 item = radix_tree_deref_slot_protected(pslot,
254 &mapping->tree_lock);
255 if (item != expected)
258 radix_tree_replace_slot(pslot, replacement);
260 radix_tree_delete(&mapping->page_tree, index);
265 * Like add_to_page_cache_locked, but error if expected item has gone.
267 static int shmem_add_to_page_cache(struct page *page,
268 struct address_space *mapping,
269 pgoff_t index, gfp_t gfp, void *expected)
273 VM_BUG_ON(!PageLocked(page));
274 VM_BUG_ON(!PageSwapBacked(page));
276 error = mem_cgroup_cache_charge(page, current->mm,
277 gfp & GFP_RECLAIM_MASK);
281 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
283 page_cache_get(page);
284 page->mapping = mapping;
287 spin_lock_irq(&mapping->tree_lock);
289 error = radix_tree_insert(&mapping->page_tree,
292 error = shmem_radix_tree_replace(mapping, index,
296 __inc_zone_page_state(page, NR_FILE_PAGES);
297 __inc_zone_page_state(page, NR_SHMEM);
298 spin_unlock_irq(&mapping->tree_lock);
300 page->mapping = NULL;
301 spin_unlock_irq(&mapping->tree_lock);
302 page_cache_release(page);
305 radix_tree_preload_end();
308 mem_cgroup_uncharge_cache_page(page);
314 * Like find_get_pages, but collecting swap entries as well as pages.
316 static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
317 pgoff_t start, unsigned int nr_pages,
318 struct page **pages, pgoff_t *indices)
322 unsigned int nr_found;
326 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
327 (void ***)pages, indices, start, nr_pages);
329 for (i = 0; i < nr_found; i++) {
332 page = radix_tree_deref_slot((void **)pages[i]);
335 if (radix_tree_exception(page)) {
336 if (radix_tree_exceptional_entry(page))
338 /* radix_tree_deref_retry(page) */
341 if (!page_cache_get_speculative(page))
344 /* Has the page moved? */
345 if (unlikely(page != *((void **)pages[i]))) {
346 page_cache_release(page);
350 indices[ret] = indices[i];
354 if (unlikely(!ret && nr_found))
361 * Lockless lookup of swap entry in radix tree, avoiding refcount on pages.
363 static pgoff_t shmem_find_swap(struct address_space *mapping, void *radswap)
365 void **slots[PAGEVEC_SIZE];
366 pgoff_t indices[PAGEVEC_SIZE];
367 unsigned int nr_found;
373 pgoff_t index = indices[nr_found - 1] + 1;
377 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
378 slots, indices, index, PAGEVEC_SIZE);
379 for (i = 0; i < nr_found; i++) {
380 void *item = radix_tree_deref_slot(slots[i]);
381 if (radix_tree_deref_retry(item)) {
385 if (item == radswap) {
397 * Remove swap entry from radix tree, free the swap and its page cache.
399 static int shmem_free_swap(struct address_space *mapping,
400 pgoff_t index, void *radswap)
404 spin_lock_irq(&mapping->tree_lock);
405 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
406 spin_unlock_irq(&mapping->tree_lock);
408 free_swap_and_cache(radix_to_swp_entry(radswap));
413 * Pagevec may contain swap entries, so shuffle up pages before releasing.
415 static void shmem_pagevec_release(struct pagevec *pvec)
419 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
420 struct page *page = pvec->pages[i];
421 if (!radix_tree_exceptional_entry(page))
422 pvec->pages[j++] = page;
425 pagevec_release(pvec);
429 * Remove range of pages and swap entries from radix tree, and free them.
431 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
433 struct address_space *mapping = inode->i_mapping;
434 struct shmem_inode_info *info = SHMEM_I(inode);
435 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
436 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
437 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
439 pgoff_t indices[PAGEVEC_SIZE];
440 long nr_swaps_freed = 0;
444 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
446 pagevec_init(&pvec, 0);
448 while (index <= end) {
449 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
450 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
451 pvec.pages, indices);
454 mem_cgroup_uncharge_start();
455 for (i = 0; i < pagevec_count(&pvec); i++) {
456 struct page *page = pvec.pages[i];
462 if (radix_tree_exceptional_entry(page)) {
463 nr_swaps_freed += !shmem_free_swap(mapping,
468 if (!trylock_page(page))
470 if (page->mapping == mapping) {
471 VM_BUG_ON(PageWriteback(page));
472 truncate_inode_page(mapping, page);
476 shmem_pagevec_release(&pvec);
477 mem_cgroup_uncharge_end();
483 struct page *page = NULL;
484 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
486 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
487 set_page_dirty(page);
489 page_cache_release(page);
496 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
497 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
498 pvec.pages, indices);
505 if (index == start && indices[0] > end) {
506 shmem_pagevec_release(&pvec);
509 mem_cgroup_uncharge_start();
510 for (i = 0; i < pagevec_count(&pvec); i++) {
511 struct page *page = pvec.pages[i];
517 if (radix_tree_exceptional_entry(page)) {
518 nr_swaps_freed += !shmem_free_swap(mapping,
524 if (page->mapping == mapping) {
525 VM_BUG_ON(PageWriteback(page));
526 truncate_inode_page(mapping, page);
530 shmem_pagevec_release(&pvec);
531 mem_cgroup_uncharge_end();
535 spin_lock(&info->lock);
536 info->swapped -= nr_swaps_freed;
537 shmem_recalc_inode(inode);
538 spin_unlock(&info->lock);
540 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
542 EXPORT_SYMBOL_GPL(shmem_truncate_range);
544 static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
546 struct inode *inode = dentry->d_inode;
549 error = inode_change_ok(inode, attr);
553 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
554 loff_t oldsize = inode->i_size;
555 loff_t newsize = attr->ia_size;
557 if (newsize != oldsize) {
558 i_size_write(inode, newsize);
559 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
561 if (newsize < oldsize) {
562 loff_t holebegin = round_up(newsize, PAGE_SIZE);
563 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
564 shmem_truncate_range(inode, newsize, (loff_t)-1);
565 /* unmap again to remove racily COWed private pages */
566 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
570 setattr_copy(inode, attr);
571 #ifdef CONFIG_TMPFS_POSIX_ACL
572 if (attr->ia_valid & ATTR_MODE)
573 error = generic_acl_chmod(inode);
578 static void shmem_evict_inode(struct inode *inode)
580 struct shmem_inode_info *info = SHMEM_I(inode);
581 struct shmem_xattr *xattr, *nxattr;
583 if (inode->i_mapping->a_ops == &shmem_aops) {
584 shmem_unacct_size(info->flags, inode->i_size);
586 shmem_truncate_range(inode, 0, (loff_t)-1);
587 if (!list_empty(&info->swaplist)) {
588 mutex_lock(&shmem_swaplist_mutex);
589 list_del_init(&info->swaplist);
590 mutex_unlock(&shmem_swaplist_mutex);
594 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
598 BUG_ON(inode->i_blocks);
599 shmem_free_inode(inode->i_sb);
600 end_writeback(inode);
604 * If swap found in inode, free it and move page from swapcache to filecache.
606 static int shmem_unuse_inode(struct shmem_inode_info *info,
607 swp_entry_t swap, struct page *page)
609 struct address_space *mapping = info->vfs_inode.i_mapping;
614 radswap = swp_to_radix_entry(swap);
615 index = shmem_find_swap(mapping, radswap);
620 * Move _head_ to start search for next from here.
621 * But be careful: shmem_evict_inode checks list_empty without taking
622 * mutex, and there's an instant in list_move_tail when info->swaplist
623 * would appear empty, if it were the only one on shmem_swaplist.
625 if (shmem_swaplist.next != &info->swaplist)
626 list_move_tail(&shmem_swaplist, &info->swaplist);
629 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
630 * but also to hold up shmem_evict_inode(): so inode cannot be freed
631 * beneath us (pagelock doesn't help until the page is in pagecache).
633 error = shmem_add_to_page_cache(page, mapping, index,
634 GFP_NOWAIT, radswap);
635 /* which does mem_cgroup_uncharge_cache_page on error */
637 if (error != -ENOMEM) {
639 * Truncation and eviction use free_swap_and_cache(), which
640 * only does trylock page: if we raced, best clean up here.
642 delete_from_swap_cache(page);
643 set_page_dirty(page);
645 spin_lock(&info->lock);
647 spin_unlock(&info->lock);
650 error = 1; /* not an error, but entry was found */
656 * Search through swapped inodes to find and replace swap by page.
658 int shmem_unuse(swp_entry_t swap, struct page *page)
660 struct list_head *this, *next;
661 struct shmem_inode_info *info;
666 * Charge page using GFP_KERNEL while we can wait, before taking
667 * the shmem_swaplist_mutex which might hold up shmem_writepage().
668 * Charged back to the user (not to caller) when swap account is used.
669 * shmem_add_to_page_cache() will be called with GFP_NOWAIT.
671 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
674 /* No radix_tree_preload: swap entry keeps a place for page in tree */
676 mutex_lock(&shmem_swaplist_mutex);
677 list_for_each_safe(this, next, &shmem_swaplist) {
678 info = list_entry(this, struct shmem_inode_info, swaplist);
679 if (!info->swapped) {
680 spin_lock(&info->lock);
682 list_del_init(&info->swaplist);
683 spin_unlock(&info->lock);
686 found = shmem_unuse_inode(info, swap, page);
691 mutex_unlock(&shmem_swaplist_mutex);
694 mem_cgroup_uncharge_cache_page(page);
699 page_cache_release(page);
704 * Move the page from the page cache to the swap cache.
706 static int shmem_writepage(struct page *page, struct writeback_control *wbc)
708 struct shmem_inode_info *info;
709 swp_entry_t swap, oswap;
710 struct address_space *mapping;
714 BUG_ON(!PageLocked(page));
715 mapping = page->mapping;
717 inode = mapping->host;
718 info = SHMEM_I(inode);
719 if (info->flags & VM_LOCKED)
721 if (!total_swap_pages)
725 * shmem_backing_dev_info's capabilities prevent regular writeback or
726 * sync from ever calling shmem_writepage; but a stacking filesystem
727 * might use ->writepage of its underlying filesystem, in which case
728 * tmpfs should write out to swap only in response to memory pressure,
729 * and not for the writeback threads or sync.
731 if (!wbc->for_reclaim) {
732 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
737 * Disable even the toy swapping implementation, while we convert
738 * functions one by one to having swap entries in the radix tree.
740 if (index < ULONG_MAX)
743 swap = get_swap_page();
748 * Add inode to shmem_unuse()'s list of swapped-out inodes,
749 * if it's not already there. Do it now because we cannot take
750 * mutex while holding spinlock, and must do so before the page
751 * is moved to swap cache, when its pagelock no longer protects
752 * the inode from eviction. But don't unlock the mutex until
753 * we've taken the spinlock, because shmem_unuse_inode() will
754 * prune a !swapped inode from the swaplist under both locks.
756 mutex_lock(&shmem_swaplist_mutex);
757 if (list_empty(&info->swaplist))
758 list_add_tail(&info->swaplist, &shmem_swaplist);
760 spin_lock(&info->lock);
761 mutex_unlock(&shmem_swaplist_mutex);
763 oswap = shmem_get_swap(info, index);
765 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
766 free_swap_and_cache(oswap);
767 shmem_put_swap(info, index, (swp_entry_t){0});
770 shmem_recalc_inode(inode);
772 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
773 delete_from_page_cache(page);
774 shmem_put_swap(info, index, swap);
776 swap_shmem_alloc(swap);
777 spin_unlock(&info->lock);
778 BUG_ON(page_mapped(page));
779 swap_writepage(page, wbc);
783 spin_unlock(&info->lock);
784 swapcache_free(swap, NULL);
786 set_page_dirty(page);
787 if (wbc->for_reclaim)
788 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
795 static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
799 if (!mpol || mpol->mode == MPOL_DEFAULT)
800 return; /* show nothing */
802 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
804 seq_printf(seq, ",mpol=%s", buffer);
807 static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
809 struct mempolicy *mpol = NULL;
811 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
814 spin_unlock(&sbinfo->stat_lock);
818 #endif /* CONFIG_TMPFS */
820 static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
821 struct shmem_inode_info *info, pgoff_t index)
823 struct mempolicy mpol, *spol;
824 struct vm_area_struct pvma;
826 spol = mpol_cond_copy(&mpol,
827 mpol_shared_policy_lookup(&info->policy, index));
829 /* Create a pseudo vma that just contains the policy */
831 pvma.vm_pgoff = index;
833 pvma.vm_policy = spol;
834 return swapin_readahead(swap, gfp, &pvma, 0);
837 static struct page *shmem_alloc_page(gfp_t gfp,
838 struct shmem_inode_info *info, pgoff_t index)
840 struct vm_area_struct pvma;
842 /* Create a pseudo vma that just contains the policy */
844 pvma.vm_pgoff = index;
846 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
849 * alloc_page_vma() will drop the shared policy reference
851 return alloc_page_vma(gfp, &pvma, 0);
853 #else /* !CONFIG_NUMA */
855 static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
858 #endif /* CONFIG_TMPFS */
860 static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
861 struct shmem_inode_info *info, pgoff_t index)
863 return swapin_readahead(swap, gfp, NULL, 0);
866 static inline struct page *shmem_alloc_page(gfp_t gfp,
867 struct shmem_inode_info *info, pgoff_t index)
869 return alloc_page(gfp);
871 #endif /* CONFIG_NUMA */
873 #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
874 static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
881 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
883 * If we allocate a new one we do not mark it dirty. That's up to the
884 * vm. If we swap it in we mark it dirty since we also free the swap
885 * entry since a page cannot live in both the swap and page cache
887 static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
888 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
890 struct address_space *mapping = inode->i_mapping;
891 struct shmem_inode_info *info = SHMEM_I(inode);
892 struct shmem_sb_info *sbinfo;
894 struct page *prealloc_page = NULL;
898 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
901 page = find_lock_page(mapping, index);
904 * Once we can get the page lock, it must be uptodate:
905 * if there were an error in reading back from swap,
906 * the page would not be inserted into the filecache.
908 BUG_ON(!PageUptodate(page));
913 * Try to preload while we can wait, to not make a habit of
914 * draining atomic reserves; but don't latch on to this cpu.
916 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
919 radix_tree_preload_end();
921 if (sgp != SGP_READ && !prealloc_page) {
922 prealloc_page = shmem_alloc_page(gfp, info, index);
924 SetPageSwapBacked(prealloc_page);
925 if (mem_cgroup_cache_charge(prealloc_page,
926 current->mm, GFP_KERNEL)) {
927 page_cache_release(prealloc_page);
928 prealloc_page = NULL;
933 spin_lock(&info->lock);
934 shmem_recalc_inode(inode);
935 swap = shmem_get_swap(info, index);
937 /* Look it up and read it in.. */
938 page = lookup_swap_cache(swap);
940 spin_unlock(&info->lock);
941 /* here we actually do the io */
943 *fault_type |= VM_FAULT_MAJOR;
944 page = shmem_swapin(swap, gfp, info, index);
946 swp_entry_t nswap = shmem_get_swap(info, index);
947 if (nswap.val == swap.val) {
953 wait_on_page_locked(page);
954 page_cache_release(page);
958 /* We have to do this with page locked to prevent races */
959 if (!trylock_page(page)) {
960 spin_unlock(&info->lock);
961 wait_on_page_locked(page);
962 page_cache_release(page);
965 if (PageWriteback(page)) {
966 spin_unlock(&info->lock);
967 wait_on_page_writeback(page);
969 page_cache_release(page);
972 if (!PageUptodate(page)) {
973 spin_unlock(&info->lock);
975 page_cache_release(page);
980 error = add_to_page_cache_locked(page, mapping,
983 spin_unlock(&info->lock);
984 if (error == -ENOMEM) {
986 * reclaim from proper memory cgroup and
987 * call memcg's OOM if needed.
989 error = mem_cgroup_shmem_charge_fallback(
990 page, current->mm, gfp);
993 page_cache_release(page);
998 page_cache_release(page);
1002 delete_from_swap_cache(page);
1003 shmem_put_swap(info, index, (swp_entry_t){0});
1005 spin_unlock(&info->lock);
1006 set_page_dirty(page);
1009 } else if (sgp == SGP_READ) {
1010 page = find_get_page(mapping, index);
1011 if (page && !trylock_page(page)) {
1012 spin_unlock(&info->lock);
1013 wait_on_page_locked(page);
1014 page_cache_release(page);
1017 spin_unlock(&info->lock);
1019 } else if (prealloc_page) {
1020 sbinfo = SHMEM_SB(inode->i_sb);
1021 if (sbinfo->max_blocks) {
1022 if (percpu_counter_compare(&sbinfo->used_blocks,
1023 sbinfo->max_blocks) >= 0 ||
1024 shmem_acct_block(info->flags))
1026 percpu_counter_inc(&sbinfo->used_blocks);
1027 inode->i_blocks += BLOCKS_PER_PAGE;
1028 } else if (shmem_acct_block(info->flags))
1031 page = prealloc_page;
1032 prealloc_page = NULL;
1034 swap = shmem_get_swap(info, index);
1036 mem_cgroup_uncharge_cache_page(page);
1038 error = add_to_page_cache_lru(page, mapping,
1041 * At add_to_page_cache_lru() failure,
1042 * uncharge will be done automatically.
1044 if (swap.val || error) {
1045 shmem_unacct_blocks(info->flags, 1);
1046 shmem_free_blocks(inode, 1);
1047 spin_unlock(&info->lock);
1048 page_cache_release(page);
1053 spin_unlock(&info->lock);
1054 clear_highpage(page);
1055 flush_dcache_page(page);
1056 SetPageUptodate(page);
1057 if (sgp == SGP_DIRTY)
1058 set_page_dirty(page);
1061 spin_unlock(&info->lock);
1069 if (prealloc_page) {
1070 mem_cgroup_uncharge_cache_page(prealloc_page);
1071 page_cache_release(prealloc_page);
1077 * Perhaps the page was brought in from swap between find_lock_page
1078 * and taking info->lock? We allow for that at add_to_page_cache_lru,
1079 * but must also avoid reporting a spurious ENOSPC while working on a
1082 page = find_get_page(mapping, index);
1083 spin_unlock(&info->lock);
1085 page_cache_release(page);
1092 static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1094 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1096 int ret = VM_FAULT_LOCKED;
1098 if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1099 return VM_FAULT_SIGBUS;
1101 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1103 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
1105 if (ret & VM_FAULT_MAJOR) {
1106 count_vm_event(PGMAJFAULT);
1107 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1113 static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
1115 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1116 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
1119 static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1122 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1125 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1126 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
1130 int shmem_lock(struct file *file, int lock, struct user_struct *user)
1132 struct inode *inode = file->f_path.dentry->d_inode;
1133 struct shmem_inode_info *info = SHMEM_I(inode);
1134 int retval = -ENOMEM;
1136 spin_lock(&info->lock);
1137 if (lock && !(info->flags & VM_LOCKED)) {
1138 if (!user_shm_lock(inode->i_size, user))
1140 info->flags |= VM_LOCKED;
1141 mapping_set_unevictable(file->f_mapping);
1143 if (!lock && (info->flags & VM_LOCKED) && user) {
1144 user_shm_unlock(inode->i_size, user);
1145 info->flags &= ~VM_LOCKED;
1146 mapping_clear_unevictable(file->f_mapping);
1147 scan_mapping_unevictable_pages(file->f_mapping);
1152 spin_unlock(&info->lock);
1156 static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1158 file_accessed(file);
1159 vma->vm_ops = &shmem_vm_ops;
1160 vma->vm_flags |= VM_CAN_NONLINEAR;
1164 static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1165 int mode, dev_t dev, unsigned long flags)
1167 struct inode *inode;
1168 struct shmem_inode_info *info;
1169 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1171 if (shmem_reserve_inode(sb))
1174 inode = new_inode(sb);
1176 inode->i_ino = get_next_ino();
1177 inode_init_owner(inode, dir, mode);
1178 inode->i_blocks = 0;
1179 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1180 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1181 inode->i_generation = get_seconds();
1182 info = SHMEM_I(inode);
1183 memset(info, 0, (char *)inode - (char *)info);
1184 spin_lock_init(&info->lock);
1185 info->flags = flags & VM_NORESERVE;
1186 INIT_LIST_HEAD(&info->swaplist);
1187 INIT_LIST_HEAD(&info->xattr_list);
1188 cache_no_acl(inode);
1190 switch (mode & S_IFMT) {
1192 inode->i_op = &shmem_special_inode_operations;
1193 init_special_inode(inode, mode, dev);
1196 inode->i_mapping->a_ops = &shmem_aops;
1197 inode->i_op = &shmem_inode_operations;
1198 inode->i_fop = &shmem_file_operations;
1199 mpol_shared_policy_init(&info->policy,
1200 shmem_get_sbmpol(sbinfo));
1204 /* Some things misbehave if size == 0 on a directory */
1205 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1206 inode->i_op = &shmem_dir_inode_operations;
1207 inode->i_fop = &simple_dir_operations;
1211 * Must not load anything in the rbtree,
1212 * mpol_free_shared_policy will not be called.
1214 mpol_shared_policy_init(&info->policy, NULL);
1218 shmem_free_inode(sb);
1223 static const struct inode_operations shmem_symlink_inode_operations;
1224 static const struct inode_operations shmem_symlink_inline_operations;
1227 shmem_write_begin(struct file *file, struct address_space *mapping,
1228 loff_t pos, unsigned len, unsigned flags,
1229 struct page **pagep, void **fsdata)
1231 struct inode *inode = mapping->host;
1232 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1233 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1237 shmem_write_end(struct file *file, struct address_space *mapping,
1238 loff_t pos, unsigned len, unsigned copied,
1239 struct page *page, void *fsdata)
1241 struct inode *inode = mapping->host;
1243 if (pos + copied > inode->i_size)
1244 i_size_write(inode, pos + copied);
1246 set_page_dirty(page);
1248 page_cache_release(page);
1253 static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1255 struct inode *inode = filp->f_path.dentry->d_inode;
1256 struct address_space *mapping = inode->i_mapping;
1258 unsigned long offset;
1259 enum sgp_type sgp = SGP_READ;
1262 * Might this read be for a stacking filesystem? Then when reading
1263 * holes of a sparse file, we actually need to allocate those pages,
1264 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1266 if (segment_eq(get_fs(), KERNEL_DS))
1269 index = *ppos >> PAGE_CACHE_SHIFT;
1270 offset = *ppos & ~PAGE_CACHE_MASK;
1273 struct page *page = NULL;
1275 unsigned long nr, ret;
1276 loff_t i_size = i_size_read(inode);
1278 end_index = i_size >> PAGE_CACHE_SHIFT;
1279 if (index > end_index)
1281 if (index == end_index) {
1282 nr = i_size & ~PAGE_CACHE_MASK;
1287 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
1289 if (desc->error == -EINVAL)
1297 * We must evaluate after, since reads (unlike writes)
1298 * are called without i_mutex protection against truncate
1300 nr = PAGE_CACHE_SIZE;
1301 i_size = i_size_read(inode);
1302 end_index = i_size >> PAGE_CACHE_SHIFT;
1303 if (index == end_index) {
1304 nr = i_size & ~PAGE_CACHE_MASK;
1307 page_cache_release(page);
1315 * If users can be writing to this page using arbitrary
1316 * virtual addresses, take care about potential aliasing
1317 * before reading the page on the kernel side.
1319 if (mapping_writably_mapped(mapping))
1320 flush_dcache_page(page);
1322 * Mark the page accessed if we read the beginning.
1325 mark_page_accessed(page);
1327 page = ZERO_PAGE(0);
1328 page_cache_get(page);
1332 * Ok, we have the page, and it's up-to-date, so
1333 * now we can copy it to user space...
1335 * The actor routine returns how many bytes were actually used..
1336 * NOTE! This may not be the same as how much of a user buffer
1337 * we filled up (we may be padding etc), so we can only update
1338 * "pos" here (the actor routine has to update the user buffer
1339 * pointers and the remaining count).
1341 ret = actor(desc, page, offset, nr);
1343 index += offset >> PAGE_CACHE_SHIFT;
1344 offset &= ~PAGE_CACHE_MASK;
1346 page_cache_release(page);
1347 if (ret != nr || !desc->count)
1353 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1354 file_accessed(filp);
1357 static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1358 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
1360 struct file *filp = iocb->ki_filp;
1364 loff_t *ppos = &iocb->ki_pos;
1366 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1370 for (seg = 0; seg < nr_segs; seg++) {
1371 read_descriptor_t desc;
1374 desc.arg.buf = iov[seg].iov_base;
1375 desc.count = iov[seg].iov_len;
1376 if (desc.count == 0)
1379 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1380 retval += desc.written;
1382 retval = retval ?: desc.error;
1391 static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1392 struct pipe_inode_info *pipe, size_t len,
1395 struct address_space *mapping = in->f_mapping;
1396 struct inode *inode = mapping->host;
1397 unsigned int loff, nr_pages, req_pages;
1398 struct page *pages[PIPE_DEF_BUFFERS];
1399 struct partial_page partial[PIPE_DEF_BUFFERS];
1401 pgoff_t index, end_index;
1404 struct splice_pipe_desc spd = {
1408 .ops = &page_cache_pipe_buf_ops,
1409 .spd_release = spd_release_page,
1412 isize = i_size_read(inode);
1413 if (unlikely(*ppos >= isize))
1416 left = isize - *ppos;
1417 if (unlikely(left < len))
1420 if (splice_grow_spd(pipe, &spd))
1423 index = *ppos >> PAGE_CACHE_SHIFT;
1424 loff = *ppos & ~PAGE_CACHE_MASK;
1425 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1426 nr_pages = min(req_pages, pipe->buffers);
1428 spd.nr_pages = find_get_pages_contig(mapping, index,
1429 nr_pages, spd.pages);
1430 index += spd.nr_pages;
1433 while (spd.nr_pages < nr_pages) {
1434 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1438 spd.pages[spd.nr_pages++] = page;
1442 index = *ppos >> PAGE_CACHE_SHIFT;
1443 nr_pages = spd.nr_pages;
1446 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1447 unsigned int this_len;
1452 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1453 page = spd.pages[page_nr];
1455 if (!PageUptodate(page) || page->mapping != mapping) {
1456 error = shmem_getpage(inode, index, &page,
1461 page_cache_release(spd.pages[page_nr]);
1462 spd.pages[page_nr] = page;
1465 isize = i_size_read(inode);
1466 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1467 if (unlikely(!isize || index > end_index))
1470 if (end_index == index) {
1473 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1477 this_len = min(this_len, plen - loff);
1481 spd.partial[page_nr].offset = loff;
1482 spd.partial[page_nr].len = this_len;
1489 while (page_nr < nr_pages)
1490 page_cache_release(spd.pages[page_nr++]);
1493 error = splice_to_pipe(pipe, &spd);
1495 splice_shrink_spd(pipe, &spd);
1504 static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1506 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1508 buf->f_type = TMPFS_MAGIC;
1509 buf->f_bsize = PAGE_CACHE_SIZE;
1510 buf->f_namelen = NAME_MAX;
1511 if (sbinfo->max_blocks) {
1512 buf->f_blocks = sbinfo->max_blocks;
1514 buf->f_bfree = sbinfo->max_blocks -
1515 percpu_counter_sum(&sbinfo->used_blocks);
1517 if (sbinfo->max_inodes) {
1518 buf->f_files = sbinfo->max_inodes;
1519 buf->f_ffree = sbinfo->free_inodes;
1521 /* else leave those fields 0 like simple_statfs */
1526 * File creation. Allocate an inode, and we're done..
1529 shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1531 struct inode *inode;
1532 int error = -ENOSPC;
1534 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
1536 error = security_inode_init_security(inode, dir,
1537 &dentry->d_name, NULL,
1540 if (error != -EOPNOTSUPP) {
1545 #ifdef CONFIG_TMPFS_POSIX_ACL
1546 error = generic_acl_init(inode, dir);
1554 dir->i_size += BOGO_DIRENT_SIZE;
1555 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1556 d_instantiate(dentry, inode);
1557 dget(dentry); /* Extra count - pin the dentry in core */
1562 static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1566 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1572 static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1573 struct nameidata *nd)
1575 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1581 static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1583 struct inode *inode = old_dentry->d_inode;
1587 * No ordinary (disk based) filesystem counts links as inodes;
1588 * but each new link needs a new dentry, pinning lowmem, and
1589 * tmpfs dentries cannot be pruned until they are unlinked.
1591 ret = shmem_reserve_inode(inode->i_sb);
1595 dir->i_size += BOGO_DIRENT_SIZE;
1596 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1598 ihold(inode); /* New dentry reference */
1599 dget(dentry); /* Extra pinning count for the created dentry */
1600 d_instantiate(dentry, inode);
1605 static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1607 struct inode *inode = dentry->d_inode;
1609 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1610 shmem_free_inode(inode->i_sb);
1612 dir->i_size -= BOGO_DIRENT_SIZE;
1613 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1615 dput(dentry); /* Undo the count from "create" - this does all the work */
1619 static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1621 if (!simple_empty(dentry))
1624 drop_nlink(dentry->d_inode);
1626 return shmem_unlink(dir, dentry);
1630 * The VFS layer already does all the dentry stuff for rename,
1631 * we just have to decrement the usage count for the target if
1632 * it exists so that the VFS layer correctly free's it when it
1635 static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1637 struct inode *inode = old_dentry->d_inode;
1638 int they_are_dirs = S_ISDIR(inode->i_mode);
1640 if (!simple_empty(new_dentry))
1643 if (new_dentry->d_inode) {
1644 (void) shmem_unlink(new_dir, new_dentry);
1646 drop_nlink(old_dir);
1647 } else if (they_are_dirs) {
1648 drop_nlink(old_dir);
1652 old_dir->i_size -= BOGO_DIRENT_SIZE;
1653 new_dir->i_size += BOGO_DIRENT_SIZE;
1654 old_dir->i_ctime = old_dir->i_mtime =
1655 new_dir->i_ctime = new_dir->i_mtime =
1656 inode->i_ctime = CURRENT_TIME;
1660 static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1664 struct inode *inode;
1667 struct shmem_inode_info *info;
1669 len = strlen(symname) + 1;
1670 if (len > PAGE_CACHE_SIZE)
1671 return -ENAMETOOLONG;
1673 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
1677 error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
1680 if (error != -EOPNOTSUPP) {
1687 info = SHMEM_I(inode);
1688 inode->i_size = len-1;
1689 if (len <= SHMEM_SYMLINK_INLINE_LEN) {
1691 memcpy(info->inline_symlink, symname, len);
1692 inode->i_op = &shmem_symlink_inline_operations;
1694 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1699 inode->i_mapping->a_ops = &shmem_aops;
1700 inode->i_op = &shmem_symlink_inode_operations;
1701 kaddr = kmap_atomic(page, KM_USER0);
1702 memcpy(kaddr, symname, len);
1703 kunmap_atomic(kaddr, KM_USER0);
1704 set_page_dirty(page);
1706 page_cache_release(page);
1708 dir->i_size += BOGO_DIRENT_SIZE;
1709 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1710 d_instantiate(dentry, inode);
1715 static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
1717 nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
1721 static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1723 struct page *page = NULL;
1724 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1725 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
1731 static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1733 if (!IS_ERR(nd_get_link(nd))) {
1734 struct page *page = cookie;
1736 mark_page_accessed(page);
1737 page_cache_release(page);
1741 #ifdef CONFIG_TMPFS_XATTR
1743 * Superblocks without xattr inode operations may get some security.* xattr
1744 * support from the LSM "for free". As soon as we have any other xattrs
1745 * like ACLs, we also need to implement the security.* handlers at
1746 * filesystem level, though.
1749 static int shmem_xattr_get(struct dentry *dentry, const char *name,
1750 void *buffer, size_t size)
1752 struct shmem_inode_info *info;
1753 struct shmem_xattr *xattr;
1756 info = SHMEM_I(dentry->d_inode);
1758 spin_lock(&info->lock);
1759 list_for_each_entry(xattr, &info->xattr_list, list) {
1760 if (strcmp(name, xattr->name))
1765 if (size < xattr->size)
1768 memcpy(buffer, xattr->value, xattr->size);
1772 spin_unlock(&info->lock);
1776 static int shmem_xattr_set(struct dentry *dentry, const char *name,
1777 const void *value, size_t size, int flags)
1779 struct inode *inode = dentry->d_inode;
1780 struct shmem_inode_info *info = SHMEM_I(inode);
1781 struct shmem_xattr *xattr;
1782 struct shmem_xattr *new_xattr = NULL;
1786 /* value == NULL means remove */
1789 len = sizeof(*new_xattr) + size;
1790 if (len <= sizeof(*new_xattr))
1793 new_xattr = kmalloc(len, GFP_KERNEL);
1797 new_xattr->name = kstrdup(name, GFP_KERNEL);
1798 if (!new_xattr->name) {
1803 new_xattr->size = size;
1804 memcpy(new_xattr->value, value, size);
1807 spin_lock(&info->lock);
1808 list_for_each_entry(xattr, &info->xattr_list, list) {
1809 if (!strcmp(name, xattr->name)) {
1810 if (flags & XATTR_CREATE) {
1813 } else if (new_xattr) {
1814 list_replace(&xattr->list, &new_xattr->list);
1816 list_del(&xattr->list);
1821 if (flags & XATTR_REPLACE) {
1825 list_add(&new_xattr->list, &info->xattr_list);
1829 spin_unlock(&info->lock);
1836 static const struct xattr_handler *shmem_xattr_handlers[] = {
1837 #ifdef CONFIG_TMPFS_POSIX_ACL
1838 &generic_acl_access_handler,
1839 &generic_acl_default_handler,
1844 static int shmem_xattr_validate(const char *name)
1846 struct { const char *prefix; size_t len; } arr[] = {
1847 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1848 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1852 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1853 size_t preflen = arr[i].len;
1854 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1863 static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1864 void *buffer, size_t size)
1869 * If this is a request for a synthetic attribute in the system.*
1870 * namespace use the generic infrastructure to resolve a handler
1871 * for it via sb->s_xattr.
1873 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1874 return generic_getxattr(dentry, name, buffer, size);
1876 err = shmem_xattr_validate(name);
1880 return shmem_xattr_get(dentry, name, buffer, size);
1883 static int shmem_setxattr(struct dentry *dentry, const char *name,
1884 const void *value, size_t size, int flags)
1889 * If this is a request for a synthetic attribute in the system.*
1890 * namespace use the generic infrastructure to resolve a handler
1891 * for it via sb->s_xattr.
1893 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1894 return generic_setxattr(dentry, name, value, size, flags);
1896 err = shmem_xattr_validate(name);
1901 value = ""; /* empty EA, do not remove */
1903 return shmem_xattr_set(dentry, name, value, size, flags);
1907 static int shmem_removexattr(struct dentry *dentry, const char *name)
1912 * If this is a request for a synthetic attribute in the system.*
1913 * namespace use the generic infrastructure to resolve a handler
1914 * for it via sb->s_xattr.
1916 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1917 return generic_removexattr(dentry, name);
1919 err = shmem_xattr_validate(name);
1923 return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
1926 static bool xattr_is_trusted(const char *name)
1928 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1931 static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
1933 bool trusted = capable(CAP_SYS_ADMIN);
1934 struct shmem_xattr *xattr;
1935 struct shmem_inode_info *info;
1938 info = SHMEM_I(dentry->d_inode);
1940 spin_lock(&info->lock);
1941 list_for_each_entry(xattr, &info->xattr_list, list) {
1944 /* skip "trusted." attributes for unprivileged callers */
1945 if (!trusted && xattr_is_trusted(xattr->name))
1948 len = strlen(xattr->name) + 1;
1955 memcpy(buffer, xattr->name, len);
1959 spin_unlock(&info->lock);
1963 #endif /* CONFIG_TMPFS_XATTR */
1965 static const struct inode_operations shmem_symlink_inline_operations = {
1966 .readlink = generic_readlink,
1967 .follow_link = shmem_follow_link_inline,
1968 #ifdef CONFIG_TMPFS_XATTR
1969 .setxattr = shmem_setxattr,
1970 .getxattr = shmem_getxattr,
1971 .listxattr = shmem_listxattr,
1972 .removexattr = shmem_removexattr,
1976 static const struct inode_operations shmem_symlink_inode_operations = {
1977 .readlink = generic_readlink,
1978 .follow_link = shmem_follow_link,
1979 .put_link = shmem_put_link,
1980 #ifdef CONFIG_TMPFS_XATTR
1981 .setxattr = shmem_setxattr,
1982 .getxattr = shmem_getxattr,
1983 .listxattr = shmem_listxattr,
1984 .removexattr = shmem_removexattr,
1988 static struct dentry *shmem_get_parent(struct dentry *child)
1990 return ERR_PTR(-ESTALE);
1993 static int shmem_match(struct inode *ino, void *vfh)
1997 inum = (inum << 32) | fh[1];
1998 return ino->i_ino == inum && fh[0] == ino->i_generation;
2001 static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2002 struct fid *fid, int fh_len, int fh_type)
2004 struct inode *inode;
2005 struct dentry *dentry = NULL;
2006 u64 inum = fid->raw[2];
2007 inum = (inum << 32) | fid->raw[1];
2012 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2013 shmem_match, fid->raw);
2015 dentry = d_find_alias(inode);
2022 static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2025 struct inode *inode = dentry->d_inode;
2032 if (inode_unhashed(inode)) {
2033 /* Unfortunately insert_inode_hash is not idempotent,
2034 * so as we hash inodes here rather than at creation
2035 * time, we need a lock to ensure we only try
2038 static DEFINE_SPINLOCK(lock);
2040 if (inode_unhashed(inode))
2041 __insert_inode_hash(inode,
2042 inode->i_ino + inode->i_generation);
2046 fh[0] = inode->i_generation;
2047 fh[1] = inode->i_ino;
2048 fh[2] = ((__u64)inode->i_ino) >> 32;
2054 static const struct export_operations shmem_export_ops = {
2055 .get_parent = shmem_get_parent,
2056 .encode_fh = shmem_encode_fh,
2057 .fh_to_dentry = shmem_fh_to_dentry,
2060 static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2063 char *this_char, *value, *rest;
2065 while (options != NULL) {
2066 this_char = options;
2069 * NUL-terminate this option: unfortunately,
2070 * mount options form a comma-separated list,
2071 * but mpol's nodelist may also contain commas.
2073 options = strchr(options, ',');
2074 if (options == NULL)
2077 if (!isdigit(*options)) {
2084 if ((value = strchr(this_char,'=')) != NULL) {
2088 "tmpfs: No value for mount option '%s'\n",
2093 if (!strcmp(this_char,"size")) {
2094 unsigned long long size;
2095 size = memparse(value,&rest);
2097 size <<= PAGE_SHIFT;
2098 size *= totalram_pages;
2104 sbinfo->max_blocks =
2105 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
2106 } else if (!strcmp(this_char,"nr_blocks")) {
2107 sbinfo->max_blocks = memparse(value, &rest);
2110 } else if (!strcmp(this_char,"nr_inodes")) {
2111 sbinfo->max_inodes = memparse(value, &rest);
2114 } else if (!strcmp(this_char,"mode")) {
2117 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
2120 } else if (!strcmp(this_char,"uid")) {
2123 sbinfo->uid = simple_strtoul(value, &rest, 0);
2126 } else if (!strcmp(this_char,"gid")) {
2129 sbinfo->gid = simple_strtoul(value, &rest, 0);
2132 } else if (!strcmp(this_char,"mpol")) {
2133 if (mpol_parse_str(value, &sbinfo->mpol, 1))
2136 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2144 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2150 static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2152 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2153 struct shmem_sb_info config = *sbinfo;
2154 unsigned long inodes;
2155 int error = -EINVAL;
2157 if (shmem_parse_options(data, &config, true))
2160 spin_lock(&sbinfo->stat_lock);
2161 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
2162 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
2164 if (config.max_inodes < inodes)
2167 * Those tests also disallow limited->unlimited while any are in
2168 * use, so i_blocks will always be zero when max_blocks is zero;
2169 * but we must separately disallow unlimited->limited, because
2170 * in that case we have no record of how much is already in use.
2172 if (config.max_blocks && !sbinfo->max_blocks)
2174 if (config.max_inodes && !sbinfo->max_inodes)
2178 sbinfo->max_blocks = config.max_blocks;
2179 sbinfo->max_inodes = config.max_inodes;
2180 sbinfo->free_inodes = config.max_inodes - inodes;
2182 mpol_put(sbinfo->mpol);
2183 sbinfo->mpol = config.mpol; /* transfers initial ref */
2185 spin_unlock(&sbinfo->stat_lock);
2189 static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2191 struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2193 if (sbinfo->max_blocks != shmem_default_max_blocks())
2194 seq_printf(seq, ",size=%luk",
2195 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2196 if (sbinfo->max_inodes != shmem_default_max_inodes())
2197 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2198 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2199 seq_printf(seq, ",mode=%03o", sbinfo->mode);
2200 if (sbinfo->uid != 0)
2201 seq_printf(seq, ",uid=%u", sbinfo->uid);
2202 if (sbinfo->gid != 0)
2203 seq_printf(seq, ",gid=%u", sbinfo->gid);
2204 shmem_show_mpol(seq, sbinfo->mpol);
2207 #endif /* CONFIG_TMPFS */
2209 static void shmem_put_super(struct super_block *sb)
2211 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2213 percpu_counter_destroy(&sbinfo->used_blocks);
2215 sb->s_fs_info = NULL;
2218 int shmem_fill_super(struct super_block *sb, void *data, int silent)
2220 struct inode *inode;
2221 struct dentry *root;
2222 struct shmem_sb_info *sbinfo;
2225 /* Round up to L1_CACHE_BYTES to resist false sharing */
2226 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
2227 L1_CACHE_BYTES), GFP_KERNEL);
2231 sbinfo->mode = S_IRWXUGO | S_ISVTX;
2232 sbinfo->uid = current_fsuid();
2233 sbinfo->gid = current_fsgid();
2234 sb->s_fs_info = sbinfo;
2238 * Per default we only allow half of the physical ram per
2239 * tmpfs instance, limiting inodes to one per page of lowmem;
2240 * but the internal instance is left unlimited.
2242 if (!(sb->s_flags & MS_NOUSER)) {
2243 sbinfo->max_blocks = shmem_default_max_blocks();
2244 sbinfo->max_inodes = shmem_default_max_inodes();
2245 if (shmem_parse_options(data, sbinfo, false)) {
2250 sb->s_export_op = &shmem_export_ops;
2252 sb->s_flags |= MS_NOUSER;
2255 spin_lock_init(&sbinfo->stat_lock);
2256 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2258 sbinfo->free_inodes = sbinfo->max_inodes;
2260 sb->s_maxbytes = MAX_LFS_FILESIZE;
2261 sb->s_blocksize = PAGE_CACHE_SIZE;
2262 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2263 sb->s_magic = TMPFS_MAGIC;
2264 sb->s_op = &shmem_ops;
2265 sb->s_time_gran = 1;
2266 #ifdef CONFIG_TMPFS_XATTR
2267 sb->s_xattr = shmem_xattr_handlers;
2269 #ifdef CONFIG_TMPFS_POSIX_ACL
2270 sb->s_flags |= MS_POSIXACL;
2273 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
2276 inode->i_uid = sbinfo->uid;
2277 inode->i_gid = sbinfo->gid;
2278 root = d_alloc_root(inode);
2287 shmem_put_super(sb);
2291 static struct kmem_cache *shmem_inode_cachep;
2293 static struct inode *shmem_alloc_inode(struct super_block *sb)
2295 struct shmem_inode_info *info;
2296 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2299 return &info->vfs_inode;
2302 static void shmem_destroy_callback(struct rcu_head *head)
2304 struct inode *inode = container_of(head, struct inode, i_rcu);
2305 INIT_LIST_HEAD(&inode->i_dentry);
2306 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2309 static void shmem_destroy_inode(struct inode *inode)
2311 if ((inode->i_mode & S_IFMT) == S_IFREG) {
2312 /* only struct inode is valid if it's an inline symlink */
2313 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2315 call_rcu(&inode->i_rcu, shmem_destroy_callback);
2318 static void shmem_init_inode(void *foo)
2320 struct shmem_inode_info *info = foo;
2321 inode_init_once(&info->vfs_inode);
2324 static int shmem_init_inodecache(void)
2326 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2327 sizeof(struct shmem_inode_info),
2328 0, SLAB_PANIC, shmem_init_inode);
2332 static void shmem_destroy_inodecache(void)
2334 kmem_cache_destroy(shmem_inode_cachep);
2337 static const struct address_space_operations shmem_aops = {
2338 .writepage = shmem_writepage,
2339 .set_page_dirty = __set_page_dirty_no_writeback,
2341 .write_begin = shmem_write_begin,
2342 .write_end = shmem_write_end,
2344 .migratepage = migrate_page,
2345 .error_remove_page = generic_error_remove_page,
2348 static const struct file_operations shmem_file_operations = {
2351 .llseek = generic_file_llseek,
2352 .read = do_sync_read,
2353 .write = do_sync_write,
2354 .aio_read = shmem_file_aio_read,
2355 .aio_write = generic_file_aio_write,
2356 .fsync = noop_fsync,
2357 .splice_read = shmem_file_splice_read,
2358 .splice_write = generic_file_splice_write,
2362 static const struct inode_operations shmem_inode_operations = {
2363 .setattr = shmem_setattr,
2364 .truncate_range = shmem_truncate_range,
2365 #ifdef CONFIG_TMPFS_XATTR
2366 .setxattr = shmem_setxattr,
2367 .getxattr = shmem_getxattr,
2368 .listxattr = shmem_listxattr,
2369 .removexattr = shmem_removexattr,
2373 static const struct inode_operations shmem_dir_inode_operations = {
2375 .create = shmem_create,
2376 .lookup = simple_lookup,
2378 .unlink = shmem_unlink,
2379 .symlink = shmem_symlink,
2380 .mkdir = shmem_mkdir,
2381 .rmdir = shmem_rmdir,
2382 .mknod = shmem_mknod,
2383 .rename = shmem_rename,
2385 #ifdef CONFIG_TMPFS_XATTR
2386 .setxattr = shmem_setxattr,
2387 .getxattr = shmem_getxattr,
2388 .listxattr = shmem_listxattr,
2389 .removexattr = shmem_removexattr,
2391 #ifdef CONFIG_TMPFS_POSIX_ACL
2392 .setattr = shmem_setattr,
2396 static const struct inode_operations shmem_special_inode_operations = {
2397 #ifdef CONFIG_TMPFS_XATTR
2398 .setxattr = shmem_setxattr,
2399 .getxattr = shmem_getxattr,
2400 .listxattr = shmem_listxattr,
2401 .removexattr = shmem_removexattr,
2403 #ifdef CONFIG_TMPFS_POSIX_ACL
2404 .setattr = shmem_setattr,
2408 static const struct super_operations shmem_ops = {
2409 .alloc_inode = shmem_alloc_inode,
2410 .destroy_inode = shmem_destroy_inode,
2412 .statfs = shmem_statfs,
2413 .remount_fs = shmem_remount_fs,
2414 .show_options = shmem_show_options,
2416 .evict_inode = shmem_evict_inode,
2417 .drop_inode = generic_delete_inode,
2418 .put_super = shmem_put_super,
2421 static const struct vm_operations_struct shmem_vm_ops = {
2422 .fault = shmem_fault,
2424 .set_policy = shmem_set_policy,
2425 .get_policy = shmem_get_policy,
2429 static struct dentry *shmem_mount(struct file_system_type *fs_type,
2430 int flags, const char *dev_name, void *data)
2432 return mount_nodev(fs_type, flags, data, shmem_fill_super);
2435 static struct file_system_type shmem_fs_type = {
2436 .owner = THIS_MODULE,
2438 .mount = shmem_mount,
2439 .kill_sb = kill_litter_super,
2442 int __init shmem_init(void)
2446 error = bdi_init(&shmem_backing_dev_info);
2450 error = shmem_init_inodecache();
2454 error = register_filesystem(&shmem_fs_type);
2456 printk(KERN_ERR "Could not register tmpfs\n");
2460 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2461 shmem_fs_type.name, NULL);
2462 if (IS_ERR(shm_mnt)) {
2463 error = PTR_ERR(shm_mnt);
2464 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2470 unregister_filesystem(&shmem_fs_type);
2472 shmem_destroy_inodecache();
2474 bdi_destroy(&shmem_backing_dev_info);
2476 shm_mnt = ERR_PTR(error);
2480 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
2482 * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file
2483 * @inode: the inode to be searched
2484 * @index: the page offset to be searched
2485 * @pagep: the pointer for the found page to be stored
2486 * @swapp: the pointer for the found swap entry to be stored
2488 * If a page is found, refcount of it is incremented. Callers should handle
2491 void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index,
2492 struct page **pagep, swp_entry_t *swapp)
2494 struct shmem_inode_info *info = SHMEM_I(inode);
2495 struct page *page = NULL;
2496 swp_entry_t swap = {0};
2498 if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
2501 spin_lock(&info->lock);
2503 swap = shmem_get_swap(info, index);
2505 page = find_get_page(&swapper_space, swap.val);
2508 page = find_get_page(inode->i_mapping, index);
2509 spin_unlock(&info->lock);
2516 #else /* !CONFIG_SHMEM */
2519 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2521 * This is intended for small system where the benefits of the full
2522 * shmem code (swap-backed and resource-limited) are outweighed by
2523 * their complexity. On systems without swap this code should be
2524 * effectively equivalent, but much lighter weight.
2527 #include <linux/ramfs.h>
2529 static struct file_system_type shmem_fs_type = {
2531 .mount = ramfs_mount,
2532 .kill_sb = kill_litter_super,
2535 int __init shmem_init(void)
2537 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
2539 shm_mnt = kern_mount(&shmem_fs_type);
2540 BUG_ON(IS_ERR(shm_mnt));
2545 int shmem_unuse(swp_entry_t swap, struct page *page)
2550 int shmem_lock(struct file *file, int lock, struct user_struct *user)
2555 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
2557 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
2559 EXPORT_SYMBOL_GPL(shmem_truncate_range);
2561 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
2563 * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file
2564 * @inode: the inode to be searched
2565 * @index: the page offset to be searched
2566 * @pagep: the pointer for the found page to be stored
2567 * @swapp: the pointer for the found swap entry to be stored
2569 * If a page is found, refcount of it is incremented. Callers should handle
2572 void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index,
2573 struct page **pagep, swp_entry_t *swapp)
2575 struct page *page = NULL;
2577 if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
2579 page = find_get_page(inode->i_mapping, index);
2582 *swapp = (swp_entry_t){0};
2586 #define shmem_vm_ops generic_file_vm_ops
2587 #define shmem_file_operations ramfs_file_operations
2588 #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
2589 #define shmem_acct_size(flags, size) 0
2590 #define shmem_unacct_size(flags, size) do {} while (0)
2592 #endif /* CONFIG_SHMEM */
2597 * shmem_file_setup - get an unlinked file living in tmpfs
2598 * @name: name for dentry (to be seen in /proc/<pid>/maps
2599 * @size: size to be set for the file
2600 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
2602 struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
2606 struct inode *inode;
2608 struct dentry *root;
2611 if (IS_ERR(shm_mnt))
2612 return (void *)shm_mnt;
2614 if (size < 0 || size > MAX_LFS_FILESIZE)
2615 return ERR_PTR(-EINVAL);
2617 if (shmem_acct_size(flags, size))
2618 return ERR_PTR(-ENOMEM);
2622 this.len = strlen(name);
2623 this.hash = 0; /* will go */
2624 root = shm_mnt->mnt_root;
2625 path.dentry = d_alloc(root, &this);
2628 path.mnt = mntget(shm_mnt);
2631 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
2635 d_instantiate(path.dentry, inode);
2636 inode->i_size = size;
2637 inode->i_nlink = 0; /* It is unlinked */
2639 error = ramfs_nommu_expand_for_mapping(inode, size);
2645 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
2646 &shmem_file_operations);
2655 shmem_unacct_size(flags, size);
2656 return ERR_PTR(error);
2658 EXPORT_SYMBOL_GPL(shmem_file_setup);
2661 * shmem_zero_setup - setup a shared anonymous mapping
2662 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2664 int shmem_zero_setup(struct vm_area_struct *vma)
2667 loff_t size = vma->vm_end - vma->vm_start;
2669 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2671 return PTR_ERR(file);
2675 vma->vm_file = file;
2676 vma->vm_ops = &shmem_vm_ops;
2677 vma->vm_flags |= VM_CAN_NONLINEAR;
2682 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2683 * @mapping: the page's address_space
2684 * @index: the page index
2685 * @gfp: the page allocator flags to use if allocating
2687 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2688 * with any new page allocations done using the specified allocation flags.
2689 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2690 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2691 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2693 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2694 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
2696 struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2697 pgoff_t index, gfp_t gfp)
2700 struct inode *inode = mapping->host;
2704 BUG_ON(mapping->a_ops != &shmem_aops);
2705 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2707 page = ERR_PTR(error);
2713 * The tiny !SHMEM case uses ramfs without swap
2715 return read_cache_page_gfp(mapping, index, gfp);
2718 EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);