2 * Resizable virtual memory filesystem for Linux.
4 * Copyright (C) 2000 Linus Torvalds.
6 * 2000-2001 Christoph Rohland
9 * Copyright (C) 2002-2011 Hugh Dickins.
10 * Copyright (C) 2011 Google Inc.
11 * Copyright (C) 2002-2005 VERITAS Software Corporation.
12 * Copyright (C) 2004 Andi Kleen, SuSE Labs
14 * Extended attribute support for tmpfs:
15 * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
16 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
19 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
21 * This file is released under the GPL.
25 #include <linux/init.h>
26 #include <linux/vfs.h>
27 #include <linux/mount.h>
28 #include <linux/pagemap.h>
29 #include <linux/file.h>
31 #include <linux/export.h>
32 #include <linux/swap.h>
34 static struct vfsmount *shm_mnt;
38 * This virtual memory filesystem is heavily based on the ramfs. It
39 * extends ramfs by the ability to use swap and honor resource limits
40 * which makes it a completely usable filesystem.
43 #include <linux/xattr.h>
44 #include <linux/exportfs.h>
45 #include <linux/posix_acl.h>
46 #include <linux/generic_acl.h>
47 #include <linux/mman.h>
48 #include <linux/string.h>
49 #include <linux/slab.h>
50 #include <linux/backing-dev.h>
51 #include <linux/shmem_fs.h>
52 #include <linux/writeback.h>
53 #include <linux/blkdev.h>
54 #include <linux/pagevec.h>
55 #include <linux/percpu_counter.h>
56 #include <linux/splice.h>
57 #include <linux/security.h>
58 #include <linux/swapops.h>
59 #include <linux/mempolicy.h>
60 #include <linux/namei.h>
61 #include <linux/ctype.h>
62 #include <linux/migrate.h>
63 #include <linux/highmem.h>
64 #include <linux/seq_file.h>
65 #include <linux/magic.h>
67 #include <asm/uaccess.h>
68 #include <asm/pgtable.h>
70 #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
71 #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
73 /* Pretend that each entry is of this size in directory's i_size */
74 #define BOGO_DIRENT_SIZE 20
76 /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
77 #define SHORT_SYMLINK_LEN 128
80 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
81 char *name; /* xattr name */
86 /* Flag allocation requirements to shmem_getpage */
88 SGP_READ, /* don't exceed i_size, don't allocate page */
89 SGP_CACHE, /* don't exceed i_size, may allocate page */
90 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
91 SGP_WRITE, /* may exceed i_size, may allocate page */
95 static unsigned long shmem_default_max_blocks(void)
97 return totalram_pages / 2;
100 static unsigned long shmem_default_max_inodes(void)
102 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
106 static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
107 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
109 static inline int shmem_getpage(struct inode *inode, pgoff_t index,
110 struct page **pagep, enum sgp_type sgp, int *fault_type)
112 return shmem_getpage_gfp(inode, index, pagep, sgp,
113 mapping_gfp_mask(inode->i_mapping), fault_type);
116 static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
118 return sb->s_fs_info;
122 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
123 * for shared memory and for shared anonymous (/dev/zero) mappings
124 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
125 * consistent with the pre-accounting of private mappings ...
127 static inline int shmem_acct_size(unsigned long flags, loff_t size)
129 return (flags & VM_NORESERVE) ?
130 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
133 static inline void shmem_unacct_size(unsigned long flags, loff_t size)
135 if (!(flags & VM_NORESERVE))
136 vm_unacct_memory(VM_ACCT(size));
140 * ... whereas tmpfs objects are accounted incrementally as
141 * pages are allocated, in order to allow huge sparse files.
142 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
143 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
145 static inline int shmem_acct_block(unsigned long flags)
147 return (flags & VM_NORESERVE) ?
148 security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
151 static inline void shmem_unacct_blocks(unsigned long flags, long pages)
153 if (flags & VM_NORESERVE)
154 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
157 static const struct super_operations shmem_ops;
158 static const struct address_space_operations shmem_aops;
159 static const struct file_operations shmem_file_operations;
160 static const struct inode_operations shmem_inode_operations;
161 static const struct inode_operations shmem_dir_inode_operations;
162 static const struct inode_operations shmem_special_inode_operations;
163 static const struct vm_operations_struct shmem_vm_ops;
165 static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
166 .ra_pages = 0, /* No readahead */
167 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
170 static LIST_HEAD(shmem_swaplist);
171 static DEFINE_MUTEX(shmem_swaplist_mutex);
173 static int shmem_reserve_inode(struct super_block *sb)
175 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
176 if (sbinfo->max_inodes) {
177 spin_lock(&sbinfo->stat_lock);
178 if (!sbinfo->free_inodes) {
179 spin_unlock(&sbinfo->stat_lock);
182 sbinfo->free_inodes--;
183 spin_unlock(&sbinfo->stat_lock);
188 static void shmem_free_inode(struct super_block *sb)
190 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
191 if (sbinfo->max_inodes) {
192 spin_lock(&sbinfo->stat_lock);
193 sbinfo->free_inodes++;
194 spin_unlock(&sbinfo->stat_lock);
199 * shmem_recalc_inode - recalculate the block usage of an inode
200 * @inode: inode to recalc
202 * We have to calculate the free blocks since the mm can drop
203 * undirtied hole pages behind our back.
205 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
206 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
208 * It has to be called with the spinlock held.
210 static void shmem_recalc_inode(struct inode *inode)
212 struct shmem_inode_info *info = SHMEM_I(inode);
215 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
217 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
218 if (sbinfo->max_blocks)
219 percpu_counter_add(&sbinfo->used_blocks, -freed);
220 info->alloced -= freed;
221 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
222 shmem_unacct_blocks(info->flags, freed);
227 * Replace item expected in radix tree by a new item, while holding tree lock.
229 static int shmem_radix_tree_replace(struct address_space *mapping,
230 pgoff_t index, void *expected, void *replacement)
235 VM_BUG_ON(!expected);
236 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
238 item = radix_tree_deref_slot_protected(pslot,
239 &mapping->tree_lock);
240 if (item != expected)
243 radix_tree_replace_slot(pslot, replacement);
245 radix_tree_delete(&mapping->page_tree, index);
250 * Like add_to_page_cache_locked, but error if expected item has gone.
252 static int shmem_add_to_page_cache(struct page *page,
253 struct address_space *mapping,
254 pgoff_t index, gfp_t gfp, void *expected)
258 VM_BUG_ON(!PageLocked(page));
259 VM_BUG_ON(!PageSwapBacked(page));
262 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
264 page_cache_get(page);
265 page->mapping = mapping;
268 spin_lock_irq(&mapping->tree_lock);
270 error = radix_tree_insert(&mapping->page_tree,
273 error = shmem_radix_tree_replace(mapping, index,
277 __inc_zone_page_state(page, NR_FILE_PAGES);
278 __inc_zone_page_state(page, NR_SHMEM);
279 spin_unlock_irq(&mapping->tree_lock);
281 page->mapping = NULL;
282 spin_unlock_irq(&mapping->tree_lock);
283 page_cache_release(page);
286 radix_tree_preload_end();
289 mem_cgroup_uncharge_cache_page(page);
294 * Like delete_from_page_cache, but substitutes swap for page.
296 static void shmem_delete_from_page_cache(struct page *page, void *radswap)
298 struct address_space *mapping = page->mapping;
301 spin_lock_irq(&mapping->tree_lock);
302 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
303 page->mapping = NULL;
305 __dec_zone_page_state(page, NR_FILE_PAGES);
306 __dec_zone_page_state(page, NR_SHMEM);
307 spin_unlock_irq(&mapping->tree_lock);
308 page_cache_release(page);
313 * Like find_get_pages, but collecting swap entries as well as pages.
315 static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
316 pgoff_t start, unsigned int nr_pages,
317 struct page **pages, pgoff_t *indices)
321 unsigned int nr_found;
325 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
326 (void ***)pages, indices, start, nr_pages);
328 for (i = 0; i < nr_found; i++) {
331 page = radix_tree_deref_slot((void **)pages[i]);
334 if (radix_tree_exception(page)) {
335 if (radix_tree_deref_retry(page))
338 * Otherwise, we must be storing a swap entry
339 * here as an exceptional entry: so return it
340 * without attempting to raise page count.
344 if (!page_cache_get_speculative(page))
347 /* Has the page moved? */
348 if (unlikely(page != *((void **)pages[i]))) {
349 page_cache_release(page);
353 indices[ret] = indices[i];
357 if (unlikely(!ret && nr_found))
364 * Remove swap entry from radix tree, free the swap and its page cache.
366 static int shmem_free_swap(struct address_space *mapping,
367 pgoff_t index, void *radswap)
371 spin_lock_irq(&mapping->tree_lock);
372 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
373 spin_unlock_irq(&mapping->tree_lock);
375 free_swap_and_cache(radix_to_swp_entry(radswap));
380 * Pagevec may contain swap entries, so shuffle up pages before releasing.
382 static void shmem_deswap_pagevec(struct pagevec *pvec)
386 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
387 struct page *page = pvec->pages[i];
388 if (!radix_tree_exceptional_entry(page))
389 pvec->pages[j++] = page;
395 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
397 void shmem_unlock_mapping(struct address_space *mapping)
400 pgoff_t indices[PAGEVEC_SIZE];
403 pagevec_init(&pvec, 0);
405 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
407 while (!mapping_unevictable(mapping)) {
409 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
410 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
412 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
413 PAGEVEC_SIZE, pvec.pages, indices);
416 index = indices[pvec.nr - 1] + 1;
417 shmem_deswap_pagevec(&pvec);
418 check_move_unevictable_pages(pvec.pages, pvec.nr);
419 pagevec_release(&pvec);
425 * Remove range of pages and swap entries from radix tree, and free them.
427 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
429 struct address_space *mapping = inode->i_mapping;
430 struct shmem_inode_info *info = SHMEM_I(inode);
431 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
432 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
433 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
435 pgoff_t indices[PAGEVEC_SIZE];
436 long nr_swaps_freed = 0;
440 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
442 pagevec_init(&pvec, 0);
444 while (index <= end) {
445 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
446 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
447 pvec.pages, indices);
450 mem_cgroup_uncharge_start();
451 for (i = 0; i < pagevec_count(&pvec); i++) {
452 struct page *page = pvec.pages[i];
458 if (radix_tree_exceptional_entry(page)) {
459 nr_swaps_freed += !shmem_free_swap(mapping,
464 if (!trylock_page(page))
466 if (page->mapping == mapping) {
467 VM_BUG_ON(PageWriteback(page));
468 truncate_inode_page(mapping, page);
472 shmem_deswap_pagevec(&pvec);
473 pagevec_release(&pvec);
474 mem_cgroup_uncharge_end();
480 struct page *page = NULL;
481 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
483 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
484 set_page_dirty(page);
486 page_cache_release(page);
493 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
494 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
495 pvec.pages, indices);
502 if (index == start && indices[0] > end) {
503 shmem_deswap_pagevec(&pvec);
504 pagevec_release(&pvec);
507 mem_cgroup_uncharge_start();
508 for (i = 0; i < pagevec_count(&pvec); i++) {
509 struct page *page = pvec.pages[i];
515 if (radix_tree_exceptional_entry(page)) {
516 nr_swaps_freed += !shmem_free_swap(mapping,
522 if (page->mapping == mapping) {
523 VM_BUG_ON(PageWriteback(page));
524 truncate_inode_page(mapping, page);
528 shmem_deswap_pagevec(&pvec);
529 pagevec_release(&pvec);
530 mem_cgroup_uncharge_end();
534 spin_lock(&info->lock);
535 info->swapped -= nr_swaps_freed;
536 shmem_recalc_inode(inode);
537 spin_unlock(&info->lock);
539 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
541 EXPORT_SYMBOL_GPL(shmem_truncate_range);
543 static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
545 struct inode *inode = dentry->d_inode;
548 error = inode_change_ok(inode, attr);
552 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
553 loff_t oldsize = inode->i_size;
554 loff_t newsize = attr->ia_size;
556 if (newsize != oldsize) {
557 i_size_write(inode, newsize);
558 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
560 if (newsize < oldsize) {
561 loff_t holebegin = round_up(newsize, PAGE_SIZE);
562 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
563 shmem_truncate_range(inode, newsize, (loff_t)-1);
564 /* unmap again to remove racily COWed private pages */
565 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
569 setattr_copy(inode, attr);
570 #ifdef CONFIG_TMPFS_POSIX_ACL
571 if (attr->ia_valid & ATTR_MODE)
572 error = generic_acl_chmod(inode);
577 static void shmem_evict_inode(struct inode *inode)
579 struct shmem_inode_info *info = SHMEM_I(inode);
580 struct shmem_xattr *xattr, *nxattr;
582 if (inode->i_mapping->a_ops == &shmem_aops) {
583 shmem_unacct_size(info->flags, inode->i_size);
585 shmem_truncate_range(inode, 0, (loff_t)-1);
586 if (!list_empty(&info->swaplist)) {
587 mutex_lock(&shmem_swaplist_mutex);
588 list_del_init(&info->swaplist);
589 mutex_unlock(&shmem_swaplist_mutex);
592 kfree(info->symlink);
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 = radix_tree_locate_item(&mapping->page_tree, 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.
670 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
673 /* No radix_tree_preload: swap entry keeps a place for page in tree */
675 mutex_lock(&shmem_swaplist_mutex);
676 list_for_each_safe(this, next, &shmem_swaplist) {
677 info = list_entry(this, struct shmem_inode_info, swaplist);
679 found = shmem_unuse_inode(info, swap, page);
681 list_del_init(&info->swaplist);
686 mutex_unlock(&shmem_swaplist_mutex);
689 mem_cgroup_uncharge_cache_page(page);
694 page_cache_release(page);
699 * Move the page from the page cache to the swap cache.
701 static int shmem_writepage(struct page *page, struct writeback_control *wbc)
703 struct shmem_inode_info *info;
704 struct address_space *mapping;
709 BUG_ON(!PageLocked(page));
710 mapping = page->mapping;
712 inode = mapping->host;
713 info = SHMEM_I(inode);
714 if (info->flags & VM_LOCKED)
716 if (!total_swap_pages)
720 * shmem_backing_dev_info's capabilities prevent regular writeback or
721 * sync from ever calling shmem_writepage; but a stacking filesystem
722 * might use ->writepage of its underlying filesystem, in which case
723 * tmpfs should write out to swap only in response to memory pressure,
724 * and not for the writeback threads or sync.
726 if (!wbc->for_reclaim) {
727 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
730 swap = get_swap_page();
735 * Add inode to shmem_unuse()'s list of swapped-out inodes,
736 * if it's not already there. Do it now before the page is
737 * moved to swap cache, when its pagelock no longer protects
738 * the inode from eviction. But don't unlock the mutex until
739 * we've incremented swapped, because shmem_unuse_inode() will
740 * prune a !swapped inode from the swaplist under this mutex.
742 mutex_lock(&shmem_swaplist_mutex);
743 if (list_empty(&info->swaplist))
744 list_add_tail(&info->swaplist, &shmem_swaplist);
746 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
747 swap_shmem_alloc(swap);
748 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
750 spin_lock(&info->lock);
752 shmem_recalc_inode(inode);
753 spin_unlock(&info->lock);
755 mutex_unlock(&shmem_swaplist_mutex);
756 BUG_ON(page_mapped(page));
757 swap_writepage(page, wbc);
761 mutex_unlock(&shmem_swaplist_mutex);
762 swapcache_free(swap, NULL);
764 set_page_dirty(page);
765 if (wbc->for_reclaim)
766 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
772 * Some intel GPUs can't use those pages in the GTT, which results in
773 * graphics corruption. Sadly, it's impossible to prevent usage of those
774 * pages in the intel allocator.
776 * Instead, we test for those areas here and leak the corresponding pages.
778 * Some day, when the intel GPU memory is not backed by shmem any more,
779 * we'll be able to come up with a solution which is contained in i915.
781 static bool i915_usable_page(struct page *page)
783 dma_addr_t addr = page_to_phys(page);
785 if (unlikely((addr < 1 * 1024 * 1024) ||
786 (addr == 0x20050000) ||
787 (addr == 0x20110000) ||
788 (addr == 0x20130000) ||
789 (addr == 0x20138000) ||
790 (addr == 0x40004000)))
798 static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
802 if (!mpol || mpol->mode == MPOL_DEFAULT)
803 return; /* show nothing */
805 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
807 seq_printf(seq, ",mpol=%s", buffer);
810 static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
812 struct mempolicy *mpol = NULL;
814 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
817 spin_unlock(&sbinfo->stat_lock);
821 #endif /* CONFIG_TMPFS */
823 static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
824 struct shmem_inode_info *info, pgoff_t index)
826 struct mempolicy mpol, *spol;
827 struct vm_area_struct pvma;
829 spol = mpol_cond_copy(&mpol,
830 mpol_shared_policy_lookup(&info->policy, index));
832 /* Create a pseudo vma that just contains the policy */
834 pvma.vm_pgoff = index;
836 pvma.vm_policy = spol;
837 return swapin_readahead(swap, gfp, &pvma, 0);
840 static struct page *shmem_alloc_page(gfp_t gfp,
841 struct shmem_inode_info *info, pgoff_t index)
843 struct vm_area_struct pvma;
846 /* Create a pseudo vma that just contains the policy */
848 pvma.vm_pgoff = index;
850 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
853 * alloc_page_vma() will drop the shared policy reference
856 page = alloc_page_vma(gfp, &pvma, 0);
857 } while (page && !i915_usable_page(page));
861 #else /* !CONFIG_NUMA */
863 static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
866 #endif /* CONFIG_TMPFS */
868 static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
869 struct shmem_inode_info *info, pgoff_t index)
871 return swapin_readahead(swap, gfp, NULL, 0);
874 static inline struct page *shmem_alloc_page(gfp_t gfp,
875 struct shmem_inode_info *info, pgoff_t index)
879 page = alloc_page(gfp);
880 } while (page && !i915_usable_page(page));
884 #endif /* CONFIG_NUMA */
886 #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
887 static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
894 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
896 * If we allocate a new one we do not mark it dirty. That's up to the
897 * vm. If we swap it in we mark it dirty since we also free the swap
898 * entry since a page cannot live in both the swap and page cache
900 static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
901 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
903 struct address_space *mapping = inode->i_mapping;
904 struct shmem_inode_info *info;
905 struct shmem_sb_info *sbinfo;
911 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
915 page = find_lock_page(mapping, index);
916 if (radix_tree_exceptional_entry(page)) {
917 swap = radix_to_swp_entry(page);
921 if (sgp != SGP_WRITE &&
922 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
927 if (page || (sgp == SGP_READ && !swap.val)) {
929 * Once we can get the page lock, it must be uptodate:
930 * if there were an error in reading back from swap,
931 * the page would not be inserted into the filecache.
933 BUG_ON(page && !PageUptodate(page));
939 * Fast cache lookup did not find it:
940 * bring it back from swap or allocate.
942 info = SHMEM_I(inode);
943 sbinfo = SHMEM_SB(inode->i_sb);
946 /* Look it up and read it in.. */
947 page = lookup_swap_cache(swap);
949 /* here we actually do the io */
951 *fault_type |= VM_FAULT_MAJOR;
952 page = shmem_swapin(swap, gfp, info, index);
959 /* We have to do this with page locked to prevent races */
961 if (!PageUptodate(page)) {
965 wait_on_page_writeback(page);
967 /* Someone may have already done it for us */
969 if (page->mapping == mapping &&
970 page->index == index)
976 error = mem_cgroup_cache_charge(page, current->mm,
977 gfp & GFP_RECLAIM_MASK);
979 error = shmem_add_to_page_cache(page, mapping, index,
980 gfp, swp_to_radix_entry(swap));
984 spin_lock(&info->lock);
986 shmem_recalc_inode(inode);
987 spin_unlock(&info->lock);
989 delete_from_swap_cache(page);
990 set_page_dirty(page);
994 if (shmem_acct_block(info->flags)) {
998 if (sbinfo->max_blocks) {
999 if (percpu_counter_compare(&sbinfo->used_blocks,
1000 sbinfo->max_blocks) >= 0) {
1004 percpu_counter_inc(&sbinfo->used_blocks);
1007 page = shmem_alloc_page(gfp, info, index);
1013 SetPageSwapBacked(page);
1014 __set_page_locked(page);
1015 error = mem_cgroup_cache_charge(page, current->mm,
1016 gfp & GFP_RECLAIM_MASK);
1018 error = shmem_add_to_page_cache(page, mapping, index,
1022 lru_cache_add_anon(page);
1024 spin_lock(&info->lock);
1026 inode->i_blocks += BLOCKS_PER_PAGE;
1027 shmem_recalc_inode(inode);
1028 spin_unlock(&info->lock);
1030 clear_highpage(page);
1031 flush_dcache_page(page);
1032 SetPageUptodate(page);
1033 if (sgp == SGP_DIRTY)
1034 set_page_dirty(page);
1037 /* Perhaps the file has been truncated since we checked */
1038 if (sgp != SGP_WRITE &&
1039 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1050 ClearPageDirty(page);
1051 delete_from_page_cache(page);
1052 spin_lock(&info->lock);
1054 inode->i_blocks -= BLOCKS_PER_PAGE;
1055 spin_unlock(&info->lock);
1057 if (sbinfo->max_blocks)
1058 percpu_counter_add(&sbinfo->used_blocks, -1);
1060 shmem_unacct_blocks(info->flags, 1);
1062 if (swap.val && error != -EINVAL) {
1063 struct page *test = find_get_page(mapping, index);
1064 if (test && !radix_tree_exceptional_entry(test))
1065 page_cache_release(test);
1066 /* Have another try if the entry has changed */
1067 if (test != swp_to_radix_entry(swap))
1072 page_cache_release(page);
1074 if (error == -ENOSPC && !once++) {
1075 info = SHMEM_I(inode);
1076 spin_lock(&info->lock);
1077 shmem_recalc_inode(inode);
1078 spin_unlock(&info->lock);
1081 if (error == -EEXIST)
1086 static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1088 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1090 int ret = VM_FAULT_LOCKED;
1092 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1094 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
1096 if (ret & VM_FAULT_MAJOR) {
1097 count_vm_event(PGMAJFAULT);
1098 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1104 static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
1106 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1107 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
1110 static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1113 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1116 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1117 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
1121 int shmem_lock(struct file *file, int lock, struct user_struct *user)
1123 struct inode *inode = file->f_path.dentry->d_inode;
1124 struct shmem_inode_info *info = SHMEM_I(inode);
1125 int retval = -ENOMEM;
1127 spin_lock(&info->lock);
1128 if (lock && !(info->flags & VM_LOCKED)) {
1129 if (!user_shm_lock(inode->i_size, user))
1131 info->flags |= VM_LOCKED;
1132 mapping_set_unevictable(file->f_mapping);
1134 if (!lock && (info->flags & VM_LOCKED) && user) {
1135 user_shm_unlock(inode->i_size, user);
1136 info->flags &= ~VM_LOCKED;
1137 mapping_clear_unevictable(file->f_mapping);
1142 spin_unlock(&info->lock);
1146 static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1148 file_accessed(file);
1149 vma->vm_ops = &shmem_vm_ops;
1150 vma->vm_flags |= VM_CAN_NONLINEAR;
1154 static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1155 umode_t mode, dev_t dev, unsigned long flags)
1157 struct inode *inode;
1158 struct shmem_inode_info *info;
1159 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1161 if (shmem_reserve_inode(sb))
1164 inode = new_inode(sb);
1166 inode->i_ino = get_next_ino();
1167 inode_init_owner(inode, dir, mode);
1168 inode->i_blocks = 0;
1169 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1170 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1171 inode->i_generation = get_seconds();
1172 info = SHMEM_I(inode);
1173 memset(info, 0, (char *)inode - (char *)info);
1174 spin_lock_init(&info->lock);
1175 info->flags = flags & VM_NORESERVE;
1176 INIT_LIST_HEAD(&info->swaplist);
1177 INIT_LIST_HEAD(&info->xattr_list);
1178 cache_no_acl(inode);
1180 switch (mode & S_IFMT) {
1182 inode->i_op = &shmem_special_inode_operations;
1183 init_special_inode(inode, mode, dev);
1186 inode->i_mapping->a_ops = &shmem_aops;
1187 inode->i_op = &shmem_inode_operations;
1188 inode->i_fop = &shmem_file_operations;
1189 mpol_shared_policy_init(&info->policy,
1190 shmem_get_sbmpol(sbinfo));
1194 /* Some things misbehave if size == 0 on a directory */
1195 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1196 inode->i_op = &shmem_dir_inode_operations;
1197 inode->i_fop = &simple_dir_operations;
1201 * Must not load anything in the rbtree,
1202 * mpol_free_shared_policy will not be called.
1204 mpol_shared_policy_init(&info->policy, NULL);
1208 shmem_free_inode(sb);
1213 static const struct inode_operations shmem_symlink_inode_operations;
1214 static const struct inode_operations shmem_short_symlink_operations;
1216 #ifdef CONFIG_TMPFS_XATTR
1217 static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
1219 #define shmem_initxattrs NULL
1223 shmem_write_begin(struct file *file, struct address_space *mapping,
1224 loff_t pos, unsigned len, unsigned flags,
1225 struct page **pagep, void **fsdata)
1227 struct inode *inode = mapping->host;
1228 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1229 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1233 shmem_write_end(struct file *file, struct address_space *mapping,
1234 loff_t pos, unsigned len, unsigned copied,
1235 struct page *page, void *fsdata)
1237 struct inode *inode = mapping->host;
1239 if (pos + copied > inode->i_size)
1240 i_size_write(inode, pos + copied);
1242 set_page_dirty(page);
1244 page_cache_release(page);
1249 static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1251 struct inode *inode = filp->f_path.dentry->d_inode;
1252 struct address_space *mapping = inode->i_mapping;
1254 unsigned long offset;
1255 enum sgp_type sgp = SGP_READ;
1258 * Might this read be for a stacking filesystem? Then when reading
1259 * holes of a sparse file, we actually need to allocate those pages,
1260 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1262 if (segment_eq(get_fs(), KERNEL_DS))
1265 index = *ppos >> PAGE_CACHE_SHIFT;
1266 offset = *ppos & ~PAGE_CACHE_MASK;
1269 struct page *page = NULL;
1271 unsigned long nr, ret;
1272 loff_t i_size = i_size_read(inode);
1274 end_index = i_size >> PAGE_CACHE_SHIFT;
1275 if (index > end_index)
1277 if (index == end_index) {
1278 nr = i_size & ~PAGE_CACHE_MASK;
1283 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
1285 if (desc->error == -EINVAL)
1293 * We must evaluate after, since reads (unlike writes)
1294 * are called without i_mutex protection against truncate
1296 nr = PAGE_CACHE_SIZE;
1297 i_size = i_size_read(inode);
1298 end_index = i_size >> PAGE_CACHE_SHIFT;
1299 if (index == end_index) {
1300 nr = i_size & ~PAGE_CACHE_MASK;
1303 page_cache_release(page);
1311 * If users can be writing to this page using arbitrary
1312 * virtual addresses, take care about potential aliasing
1313 * before reading the page on the kernel side.
1315 if (mapping_writably_mapped(mapping))
1316 flush_dcache_page(page);
1318 * Mark the page accessed if we read the beginning.
1321 mark_page_accessed(page);
1323 page = ZERO_PAGE(0);
1324 page_cache_get(page);
1328 * Ok, we have the page, and it's up-to-date, so
1329 * now we can copy it to user space...
1331 * The actor routine returns how many bytes were actually used..
1332 * NOTE! This may not be the same as how much of a user buffer
1333 * we filled up (we may be padding etc), so we can only update
1334 * "pos" here (the actor routine has to update the user buffer
1335 * pointers and the remaining count).
1337 ret = actor(desc, page, offset, nr);
1339 index += offset >> PAGE_CACHE_SHIFT;
1340 offset &= ~PAGE_CACHE_MASK;
1342 page_cache_release(page);
1343 if (ret != nr || !desc->count)
1349 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1350 file_accessed(filp);
1353 static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1354 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
1356 struct file *filp = iocb->ki_filp;
1360 loff_t *ppos = &iocb->ki_pos;
1362 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1366 for (seg = 0; seg < nr_segs; seg++) {
1367 read_descriptor_t desc;
1370 desc.arg.buf = iov[seg].iov_base;
1371 desc.count = iov[seg].iov_len;
1372 if (desc.count == 0)
1375 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1376 retval += desc.written;
1378 retval = retval ?: desc.error;
1387 static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1388 struct pipe_inode_info *pipe, size_t len,
1391 struct address_space *mapping = in->f_mapping;
1392 struct inode *inode = mapping->host;
1393 unsigned int loff, nr_pages, req_pages;
1394 struct page *pages[PIPE_DEF_BUFFERS];
1395 struct partial_page partial[PIPE_DEF_BUFFERS];
1397 pgoff_t index, end_index;
1400 struct splice_pipe_desc spd = {
1404 .ops = &page_cache_pipe_buf_ops,
1405 .spd_release = spd_release_page,
1408 isize = i_size_read(inode);
1409 if (unlikely(*ppos >= isize))
1412 left = isize - *ppos;
1413 if (unlikely(left < len))
1416 if (splice_grow_spd(pipe, &spd))
1419 index = *ppos >> PAGE_CACHE_SHIFT;
1420 loff = *ppos & ~PAGE_CACHE_MASK;
1421 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1422 nr_pages = min(req_pages, pipe->buffers);
1424 spd.nr_pages = find_get_pages_contig(mapping, index,
1425 nr_pages, spd.pages);
1426 index += spd.nr_pages;
1429 while (spd.nr_pages < nr_pages) {
1430 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1434 spd.pages[spd.nr_pages++] = page;
1438 index = *ppos >> PAGE_CACHE_SHIFT;
1439 nr_pages = spd.nr_pages;
1442 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1443 unsigned int this_len;
1448 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1449 page = spd.pages[page_nr];
1451 if (!PageUptodate(page) || page->mapping != mapping) {
1452 error = shmem_getpage(inode, index, &page,
1457 page_cache_release(spd.pages[page_nr]);
1458 spd.pages[page_nr] = page;
1461 isize = i_size_read(inode);
1462 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1463 if (unlikely(!isize || index > end_index))
1466 if (end_index == index) {
1469 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1473 this_len = min(this_len, plen - loff);
1477 spd.partial[page_nr].offset = loff;
1478 spd.partial[page_nr].len = this_len;
1485 while (page_nr < nr_pages)
1486 page_cache_release(spd.pages[page_nr++]);
1489 error = splice_to_pipe(pipe, &spd);
1491 splice_shrink_spd(pipe, &spd);
1500 static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1502 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1504 buf->f_type = TMPFS_MAGIC;
1505 buf->f_bsize = PAGE_CACHE_SIZE;
1506 buf->f_namelen = NAME_MAX;
1507 if (sbinfo->max_blocks) {
1508 buf->f_blocks = sbinfo->max_blocks;
1510 buf->f_bfree = sbinfo->max_blocks -
1511 percpu_counter_sum(&sbinfo->used_blocks);
1513 if (sbinfo->max_inodes) {
1514 buf->f_files = sbinfo->max_inodes;
1515 buf->f_ffree = sbinfo->free_inodes;
1517 /* else leave those fields 0 like simple_statfs */
1522 * File creation. Allocate an inode, and we're done..
1525 shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1527 struct inode *inode;
1528 int error = -ENOSPC;
1530 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
1532 error = security_inode_init_security(inode, dir,
1534 shmem_initxattrs, NULL);
1536 if (error != -EOPNOTSUPP) {
1541 #ifdef CONFIG_TMPFS_POSIX_ACL
1542 error = generic_acl_init(inode, dir);
1550 dir->i_size += BOGO_DIRENT_SIZE;
1551 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1552 d_instantiate(dentry, inode);
1553 dget(dentry); /* Extra count - pin the dentry in core */
1558 static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1562 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1568 static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1569 struct nameidata *nd)
1571 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1577 static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1579 struct inode *inode = old_dentry->d_inode;
1583 * No ordinary (disk based) filesystem counts links as inodes;
1584 * but each new link needs a new dentry, pinning lowmem, and
1585 * tmpfs dentries cannot be pruned until they are unlinked.
1587 ret = shmem_reserve_inode(inode->i_sb);
1591 dir->i_size += BOGO_DIRENT_SIZE;
1592 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1594 ihold(inode); /* New dentry reference */
1595 dget(dentry); /* Extra pinning count for the created dentry */
1596 d_instantiate(dentry, inode);
1601 static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1603 struct inode *inode = dentry->d_inode;
1605 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1606 shmem_free_inode(inode->i_sb);
1608 dir->i_size -= BOGO_DIRENT_SIZE;
1609 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1611 dput(dentry); /* Undo the count from "create" - this does all the work */
1615 static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1617 if (!simple_empty(dentry))
1620 drop_nlink(dentry->d_inode);
1622 return shmem_unlink(dir, dentry);
1626 * The VFS layer already does all the dentry stuff for rename,
1627 * we just have to decrement the usage count for the target if
1628 * it exists so that the VFS layer correctly free's it when it
1631 static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1633 struct inode *inode = old_dentry->d_inode;
1634 int they_are_dirs = S_ISDIR(inode->i_mode);
1636 if (!simple_empty(new_dentry))
1639 if (new_dentry->d_inode) {
1640 (void) shmem_unlink(new_dir, new_dentry);
1642 drop_nlink(old_dir);
1643 } else if (they_are_dirs) {
1644 drop_nlink(old_dir);
1648 old_dir->i_size -= BOGO_DIRENT_SIZE;
1649 new_dir->i_size += BOGO_DIRENT_SIZE;
1650 old_dir->i_ctime = old_dir->i_mtime =
1651 new_dir->i_ctime = new_dir->i_mtime =
1652 inode->i_ctime = CURRENT_TIME;
1656 static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1660 struct inode *inode;
1663 struct shmem_inode_info *info;
1665 len = strlen(symname) + 1;
1666 if (len > PAGE_CACHE_SIZE)
1667 return -ENAMETOOLONG;
1669 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
1673 error = security_inode_init_security(inode, dir, &dentry->d_name,
1674 shmem_initxattrs, NULL);
1676 if (error != -EOPNOTSUPP) {
1683 info = SHMEM_I(inode);
1684 inode->i_size = len-1;
1685 if (len <= SHORT_SYMLINK_LEN) {
1686 info->symlink = kmemdup(symname, len, GFP_KERNEL);
1687 if (!info->symlink) {
1691 inode->i_op = &shmem_short_symlink_operations;
1693 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1698 inode->i_mapping->a_ops = &shmem_aops;
1699 inode->i_op = &shmem_symlink_inode_operations;
1700 kaddr = kmap_atomic(page);
1701 memcpy(kaddr, symname, len);
1702 kunmap_atomic(kaddr);
1703 set_page_dirty(page);
1705 page_cache_release(page);
1707 dir->i_size += BOGO_DIRENT_SIZE;
1708 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1709 d_instantiate(dentry, inode);
1714 static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
1716 nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
1720 static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1722 struct page *page = NULL;
1723 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1724 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
1730 static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1732 if (!IS_ERR(nd_get_link(nd))) {
1733 struct page *page = cookie;
1735 mark_page_accessed(page);
1736 page_cache_release(page);
1740 #ifdef CONFIG_TMPFS_XATTR
1742 * Superblocks without xattr inode operations may get some security.* xattr
1743 * support from the LSM "for free". As soon as we have any other xattrs
1744 * like ACLs, we also need to implement the security.* handlers at
1745 * filesystem level, though.
1749 * Allocate new xattr and copy in the value; but leave the name to callers.
1751 static struct shmem_xattr *shmem_xattr_alloc(const void *value, size_t size)
1753 struct shmem_xattr *new_xattr;
1757 len = sizeof(*new_xattr) + size;
1758 if (len <= sizeof(*new_xattr))
1761 new_xattr = kmalloc(len, GFP_KERNEL);
1765 new_xattr->size = size;
1766 memcpy(new_xattr->value, value, size);
1771 * Callback for security_inode_init_security() for acquiring xattrs.
1773 static int shmem_initxattrs(struct inode *inode,
1774 const struct xattr *xattr_array,
1777 struct shmem_inode_info *info = SHMEM_I(inode);
1778 const struct xattr *xattr;
1779 struct shmem_xattr *new_xattr;
1782 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
1783 new_xattr = shmem_xattr_alloc(xattr->value, xattr->value_len);
1787 len = strlen(xattr->name) + 1;
1788 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
1790 if (!new_xattr->name) {
1795 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
1796 XATTR_SECURITY_PREFIX_LEN);
1797 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
1800 spin_lock(&info->lock);
1801 list_add(&new_xattr->list, &info->xattr_list);
1802 spin_unlock(&info->lock);
1808 static int shmem_xattr_get(struct dentry *dentry, const char *name,
1809 void *buffer, size_t size)
1811 struct shmem_inode_info *info;
1812 struct shmem_xattr *xattr;
1815 info = SHMEM_I(dentry->d_inode);
1817 spin_lock(&info->lock);
1818 list_for_each_entry(xattr, &info->xattr_list, list) {
1819 if (strcmp(name, xattr->name))
1824 if (size < xattr->size)
1827 memcpy(buffer, xattr->value, xattr->size);
1831 spin_unlock(&info->lock);
1835 static int shmem_xattr_set(struct inode *inode, const char *name,
1836 const void *value, size_t size, int flags)
1838 struct shmem_inode_info *info = SHMEM_I(inode);
1839 struct shmem_xattr *xattr;
1840 struct shmem_xattr *new_xattr = NULL;
1843 /* value == NULL means remove */
1845 new_xattr = shmem_xattr_alloc(value, size);
1849 new_xattr->name = kstrdup(name, GFP_KERNEL);
1850 if (!new_xattr->name) {
1856 spin_lock(&info->lock);
1857 list_for_each_entry(xattr, &info->xattr_list, list) {
1858 if (!strcmp(name, xattr->name)) {
1859 if (flags & XATTR_CREATE) {
1862 } else if (new_xattr) {
1863 list_replace(&xattr->list, &new_xattr->list);
1865 list_del(&xattr->list);
1870 if (flags & XATTR_REPLACE) {
1874 list_add(&new_xattr->list, &info->xattr_list);
1878 spin_unlock(&info->lock);
1885 static const struct xattr_handler *shmem_xattr_handlers[] = {
1886 #ifdef CONFIG_TMPFS_POSIX_ACL
1887 &generic_acl_access_handler,
1888 &generic_acl_default_handler,
1893 static int shmem_xattr_validate(const char *name)
1895 struct { const char *prefix; size_t len; } arr[] = {
1896 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1897 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1901 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1902 size_t preflen = arr[i].len;
1903 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1912 static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1913 void *buffer, size_t size)
1918 * If this is a request for a synthetic attribute in the system.*
1919 * namespace use the generic infrastructure to resolve a handler
1920 * for it via sb->s_xattr.
1922 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1923 return generic_getxattr(dentry, name, buffer, size);
1925 err = shmem_xattr_validate(name);
1929 return shmem_xattr_get(dentry, name, buffer, size);
1932 static int shmem_setxattr(struct dentry *dentry, const char *name,
1933 const void *value, size_t size, int flags)
1938 * If this is a request for a synthetic attribute in the system.*
1939 * namespace use the generic infrastructure to resolve a handler
1940 * for it via sb->s_xattr.
1942 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1943 return generic_setxattr(dentry, name, value, size, flags);
1945 err = shmem_xattr_validate(name);
1950 value = ""; /* empty EA, do not remove */
1952 return shmem_xattr_set(dentry->d_inode, name, value, size, flags);
1956 static int shmem_removexattr(struct dentry *dentry, const char *name)
1961 * If this is a request for a synthetic attribute in the system.*
1962 * namespace use the generic infrastructure to resolve a handler
1963 * for it via sb->s_xattr.
1965 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1966 return generic_removexattr(dentry, name);
1968 err = shmem_xattr_validate(name);
1972 return shmem_xattr_set(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
1975 static bool xattr_is_trusted(const char *name)
1977 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1980 static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
1982 bool trusted = capable(CAP_SYS_ADMIN);
1983 struct shmem_xattr *xattr;
1984 struct shmem_inode_info *info;
1987 info = SHMEM_I(dentry->d_inode);
1989 spin_lock(&info->lock);
1990 list_for_each_entry(xattr, &info->xattr_list, list) {
1993 /* skip "trusted." attributes for unprivileged callers */
1994 if (!trusted && xattr_is_trusted(xattr->name))
1997 len = strlen(xattr->name) + 1;
2004 memcpy(buffer, xattr->name, len);
2008 spin_unlock(&info->lock);
2012 #endif /* CONFIG_TMPFS_XATTR */
2014 static const struct inode_operations shmem_short_symlink_operations = {
2015 .readlink = generic_readlink,
2016 .follow_link = shmem_follow_short_symlink,
2017 #ifdef CONFIG_TMPFS_XATTR
2018 .setxattr = shmem_setxattr,
2019 .getxattr = shmem_getxattr,
2020 .listxattr = shmem_listxattr,
2021 .removexattr = shmem_removexattr,
2025 static const struct inode_operations shmem_symlink_inode_operations = {
2026 .readlink = generic_readlink,
2027 .follow_link = shmem_follow_link,
2028 .put_link = shmem_put_link,
2029 #ifdef CONFIG_TMPFS_XATTR
2030 .setxattr = shmem_setxattr,
2031 .getxattr = shmem_getxattr,
2032 .listxattr = shmem_listxattr,
2033 .removexattr = shmem_removexattr,
2037 static struct dentry *shmem_get_parent(struct dentry *child)
2039 return ERR_PTR(-ESTALE);
2042 static int shmem_match(struct inode *ino, void *vfh)
2046 inum = (inum << 32) | fh[1];
2047 return ino->i_ino == inum && fh[0] == ino->i_generation;
2050 static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2051 struct fid *fid, int fh_len, int fh_type)
2053 struct inode *inode;
2054 struct dentry *dentry = NULL;
2055 u64 inum = fid->raw[2];
2056 inum = (inum << 32) | fid->raw[1];
2061 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2062 shmem_match, fid->raw);
2064 dentry = d_find_alias(inode);
2071 static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2074 struct inode *inode = dentry->d_inode;
2081 if (inode_unhashed(inode)) {
2082 /* Unfortunately insert_inode_hash is not idempotent,
2083 * so as we hash inodes here rather than at creation
2084 * time, we need a lock to ensure we only try
2087 static DEFINE_SPINLOCK(lock);
2089 if (inode_unhashed(inode))
2090 __insert_inode_hash(inode,
2091 inode->i_ino + inode->i_generation);
2095 fh[0] = inode->i_generation;
2096 fh[1] = inode->i_ino;
2097 fh[2] = ((__u64)inode->i_ino) >> 32;
2103 static const struct export_operations shmem_export_ops = {
2104 .get_parent = shmem_get_parent,
2105 .encode_fh = shmem_encode_fh,
2106 .fh_to_dentry = shmem_fh_to_dentry,
2109 static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2112 char *this_char, *value, *rest;
2114 while (options != NULL) {
2115 this_char = options;
2118 * NUL-terminate this option: unfortunately,
2119 * mount options form a comma-separated list,
2120 * but mpol's nodelist may also contain commas.
2122 options = strchr(options, ',');
2123 if (options == NULL)
2126 if (!isdigit(*options)) {
2133 if ((value = strchr(this_char,'=')) != NULL) {
2137 "tmpfs: No value for mount option '%s'\n",
2142 if (!strcmp(this_char,"size")) {
2143 unsigned long long size;
2144 size = memparse(value,&rest);
2146 size <<= PAGE_SHIFT;
2147 size *= totalram_pages;
2153 sbinfo->max_blocks =
2154 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
2155 } else if (!strcmp(this_char,"nr_blocks")) {
2156 sbinfo->max_blocks = memparse(value, &rest);
2159 } else if (!strcmp(this_char,"nr_inodes")) {
2160 sbinfo->max_inodes = memparse(value, &rest);
2163 } else if (!strcmp(this_char,"mode")) {
2166 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
2169 } else if (!strcmp(this_char,"uid")) {
2172 sbinfo->uid = simple_strtoul(value, &rest, 0);
2175 } else if (!strcmp(this_char,"gid")) {
2178 sbinfo->gid = simple_strtoul(value, &rest, 0);
2181 } else if (!strcmp(this_char,"mpol")) {
2182 if (mpol_parse_str(value, &sbinfo->mpol, 1))
2185 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2193 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2199 static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2201 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2202 struct shmem_sb_info config = *sbinfo;
2203 unsigned long inodes;
2204 int error = -EINVAL;
2206 if (shmem_parse_options(data, &config, true))
2209 spin_lock(&sbinfo->stat_lock);
2210 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
2211 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
2213 if (config.max_inodes < inodes)
2216 * Those tests disallow limited->unlimited while any are in use;
2217 * but we must separately disallow unlimited->limited, because
2218 * in that case we have no record of how much is already in use.
2220 if (config.max_blocks && !sbinfo->max_blocks)
2222 if (config.max_inodes && !sbinfo->max_inodes)
2226 sbinfo->max_blocks = config.max_blocks;
2227 sbinfo->max_inodes = config.max_inodes;
2228 sbinfo->free_inodes = config.max_inodes - inodes;
2230 mpol_put(sbinfo->mpol);
2231 sbinfo->mpol = config.mpol; /* transfers initial ref */
2233 spin_unlock(&sbinfo->stat_lock);
2237 static int shmem_show_options(struct seq_file *seq, struct dentry *root)
2239 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
2241 if (sbinfo->max_blocks != shmem_default_max_blocks())
2242 seq_printf(seq, ",size=%luk",
2243 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2244 if (sbinfo->max_inodes != shmem_default_max_inodes())
2245 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2246 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2247 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
2248 if (sbinfo->uid != 0)
2249 seq_printf(seq, ",uid=%u", sbinfo->uid);
2250 if (sbinfo->gid != 0)
2251 seq_printf(seq, ",gid=%u", sbinfo->gid);
2252 shmem_show_mpol(seq, sbinfo->mpol);
2255 #endif /* CONFIG_TMPFS */
2257 static void shmem_put_super(struct super_block *sb)
2259 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2261 percpu_counter_destroy(&sbinfo->used_blocks);
2263 sb->s_fs_info = NULL;
2266 int shmem_fill_super(struct super_block *sb, void *data, int silent)
2268 struct inode *inode;
2269 struct shmem_sb_info *sbinfo;
2272 /* Round up to L1_CACHE_BYTES to resist false sharing */
2273 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
2274 L1_CACHE_BYTES), GFP_KERNEL);
2278 sbinfo->mode = S_IRWXUGO | S_ISVTX;
2279 sbinfo->uid = current_fsuid();
2280 sbinfo->gid = current_fsgid();
2281 sb->s_fs_info = sbinfo;
2285 * Per default we only allow half of the physical ram per
2286 * tmpfs instance, limiting inodes to one per page of lowmem;
2287 * but the internal instance is left unlimited.
2289 if (!(sb->s_flags & MS_NOUSER)) {
2290 sbinfo->max_blocks = shmem_default_max_blocks();
2291 sbinfo->max_inodes = shmem_default_max_inodes();
2292 if (shmem_parse_options(data, sbinfo, false)) {
2297 sb->s_export_op = &shmem_export_ops;
2299 sb->s_flags |= MS_NOUSER;
2302 spin_lock_init(&sbinfo->stat_lock);
2303 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2305 sbinfo->free_inodes = sbinfo->max_inodes;
2307 sb->s_maxbytes = MAX_LFS_FILESIZE;
2308 sb->s_blocksize = PAGE_CACHE_SIZE;
2309 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2310 sb->s_magic = TMPFS_MAGIC;
2311 sb->s_op = &shmem_ops;
2312 sb->s_time_gran = 1;
2313 #ifdef CONFIG_TMPFS_XATTR
2314 sb->s_xattr = shmem_xattr_handlers;
2316 #ifdef CONFIG_TMPFS_POSIX_ACL
2317 sb->s_flags |= MS_POSIXACL;
2320 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
2323 inode->i_uid = sbinfo->uid;
2324 inode->i_gid = sbinfo->gid;
2325 sb->s_root = d_make_root(inode);
2331 shmem_put_super(sb);
2335 static struct kmem_cache *shmem_inode_cachep;
2337 static struct inode *shmem_alloc_inode(struct super_block *sb)
2339 struct shmem_inode_info *info;
2340 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2343 return &info->vfs_inode;
2346 static void shmem_destroy_callback(struct rcu_head *head)
2348 struct inode *inode = container_of(head, struct inode, i_rcu);
2349 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2352 static void shmem_destroy_inode(struct inode *inode)
2354 if (S_ISREG(inode->i_mode))
2355 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2356 call_rcu(&inode->i_rcu, shmem_destroy_callback);
2359 static void shmem_init_inode(void *foo)
2361 struct shmem_inode_info *info = foo;
2362 inode_init_once(&info->vfs_inode);
2365 static int shmem_init_inodecache(void)
2367 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2368 sizeof(struct shmem_inode_info),
2369 0, SLAB_PANIC, shmem_init_inode);
2373 static void shmem_destroy_inodecache(void)
2375 kmem_cache_destroy(shmem_inode_cachep);
2378 static const struct address_space_operations shmem_aops = {
2379 .writepage = shmem_writepage,
2380 .set_page_dirty = __set_page_dirty_no_writeback,
2382 .write_begin = shmem_write_begin,
2383 .write_end = shmem_write_end,
2385 .migratepage = migrate_page,
2386 .error_remove_page = generic_error_remove_page,
2389 static const struct file_operations shmem_file_operations = {
2392 .llseek = generic_file_llseek,
2393 .read = do_sync_read,
2394 .write = do_sync_write,
2395 .aio_read = shmem_file_aio_read,
2396 .aio_write = generic_file_aio_write,
2397 .fsync = noop_fsync,
2398 .splice_read = shmem_file_splice_read,
2399 .splice_write = generic_file_splice_write,
2403 static const struct inode_operations shmem_inode_operations = {
2404 .setattr = shmem_setattr,
2405 .truncate_range = shmem_truncate_range,
2406 #ifdef CONFIG_TMPFS_XATTR
2407 .setxattr = shmem_setxattr,
2408 .getxattr = shmem_getxattr,
2409 .listxattr = shmem_listxattr,
2410 .removexattr = shmem_removexattr,
2414 static const struct inode_operations shmem_dir_inode_operations = {
2416 .create = shmem_create,
2417 .lookup = simple_lookup,
2419 .unlink = shmem_unlink,
2420 .symlink = shmem_symlink,
2421 .mkdir = shmem_mkdir,
2422 .rmdir = shmem_rmdir,
2423 .mknod = shmem_mknod,
2424 .rename = shmem_rename,
2426 #ifdef CONFIG_TMPFS_XATTR
2427 .setxattr = shmem_setxattr,
2428 .getxattr = shmem_getxattr,
2429 .listxattr = shmem_listxattr,
2430 .removexattr = shmem_removexattr,
2432 #ifdef CONFIG_TMPFS_POSIX_ACL
2433 .setattr = shmem_setattr,
2437 static const struct inode_operations shmem_special_inode_operations = {
2438 #ifdef CONFIG_TMPFS_XATTR
2439 .setxattr = shmem_setxattr,
2440 .getxattr = shmem_getxattr,
2441 .listxattr = shmem_listxattr,
2442 .removexattr = shmem_removexattr,
2444 #ifdef CONFIG_TMPFS_POSIX_ACL
2445 .setattr = shmem_setattr,
2449 static const struct super_operations shmem_ops = {
2450 .alloc_inode = shmem_alloc_inode,
2451 .destroy_inode = shmem_destroy_inode,
2453 .statfs = shmem_statfs,
2454 .remount_fs = shmem_remount_fs,
2455 .show_options = shmem_show_options,
2457 .evict_inode = shmem_evict_inode,
2458 .drop_inode = generic_delete_inode,
2459 .put_super = shmem_put_super,
2462 static const struct vm_operations_struct shmem_vm_ops = {
2463 .fault = shmem_fault,
2465 .set_policy = shmem_set_policy,
2466 .get_policy = shmem_get_policy,
2470 static struct dentry *shmem_mount(struct file_system_type *fs_type,
2471 int flags, const char *dev_name, void *data)
2473 return mount_nodev(fs_type, flags, data, shmem_fill_super);
2476 static struct file_system_type shmem_fs_type = {
2477 .owner = THIS_MODULE,
2479 .mount = shmem_mount,
2480 .kill_sb = kill_litter_super,
2483 int __init shmem_init(void)
2487 error = bdi_init(&shmem_backing_dev_info);
2491 error = shmem_init_inodecache();
2495 error = register_filesystem(&shmem_fs_type);
2497 printk(KERN_ERR "Could not register tmpfs\n");
2501 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2502 shmem_fs_type.name, NULL);
2503 if (IS_ERR(shm_mnt)) {
2504 error = PTR_ERR(shm_mnt);
2505 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2511 unregister_filesystem(&shmem_fs_type);
2513 shmem_destroy_inodecache();
2515 bdi_destroy(&shmem_backing_dev_info);
2517 shm_mnt = ERR_PTR(error);
2521 #else /* !CONFIG_SHMEM */
2524 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2526 * This is intended for small system where the benefits of the full
2527 * shmem code (swap-backed and resource-limited) are outweighed by
2528 * their complexity. On systems without swap this code should be
2529 * effectively equivalent, but much lighter weight.
2532 #include <linux/ramfs.h>
2534 static struct file_system_type shmem_fs_type = {
2536 .mount = ramfs_mount,
2537 .kill_sb = kill_litter_super,
2540 int __init shmem_init(void)
2542 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
2544 shm_mnt = kern_mount(&shmem_fs_type);
2545 BUG_ON(IS_ERR(shm_mnt));
2550 int shmem_unuse(swp_entry_t swap, struct page *page)
2555 int shmem_lock(struct file *file, int lock, struct user_struct *user)
2560 void shmem_unlock_mapping(struct address_space *mapping)
2564 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
2566 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
2568 EXPORT_SYMBOL_GPL(shmem_truncate_range);
2570 #define shmem_vm_ops generic_file_vm_ops
2571 #define shmem_file_operations ramfs_file_operations
2572 #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
2573 #define shmem_acct_size(flags, size) 0
2574 #define shmem_unacct_size(flags, size) do {} while (0)
2576 #endif /* CONFIG_SHMEM */
2581 * shmem_file_setup - get an unlinked file living in tmpfs
2582 * @name: name for dentry (to be seen in /proc/<pid>/maps
2583 * @size: size to be set for the file
2584 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
2586 struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
2590 struct inode *inode;
2592 struct dentry *root;
2595 if (IS_ERR(shm_mnt))
2596 return (void *)shm_mnt;
2598 if (size < 0 || size > MAX_LFS_FILESIZE)
2599 return ERR_PTR(-EINVAL);
2601 if (shmem_acct_size(flags, size))
2602 return ERR_PTR(-ENOMEM);
2606 this.len = strlen(name);
2607 this.hash = 0; /* will go */
2608 root = shm_mnt->mnt_root;
2609 path.dentry = d_alloc(root, &this);
2612 path.mnt = mntget(shm_mnt);
2615 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
2619 d_instantiate(path.dentry, inode);
2620 inode->i_size = size;
2621 clear_nlink(inode); /* It is unlinked */
2623 error = ramfs_nommu_expand_for_mapping(inode, size);
2629 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
2630 &shmem_file_operations);
2639 shmem_unacct_size(flags, size);
2640 return ERR_PTR(error);
2642 EXPORT_SYMBOL_GPL(shmem_file_setup);
2645 * shmem_zero_setup - setup a shared anonymous mapping
2646 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2648 int shmem_zero_setup(struct vm_area_struct *vma)
2651 loff_t size = vma->vm_end - vma->vm_start;
2653 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2655 return PTR_ERR(file);
2659 vma->vm_file = file;
2660 vma->vm_ops = &shmem_vm_ops;
2661 vma->vm_flags |= VM_CAN_NONLINEAR;
2666 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2667 * @mapping: the page's address_space
2668 * @index: the page index
2669 * @gfp: the page allocator flags to use if allocating
2671 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2672 * with any new page allocations done using the specified allocation flags.
2673 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2674 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2675 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2677 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2678 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
2680 struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2681 pgoff_t index, gfp_t gfp)
2684 struct inode *inode = mapping->host;
2688 BUG_ON(mapping->a_ops != &shmem_aops);
2689 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2691 page = ERR_PTR(error);
2697 * The tiny !SHMEM case uses ramfs without swap
2699 return read_cache_page_gfp(mapping, index, gfp);
2702 EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);