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 * vmtruncate_range() communicates with shmem_fault via
81 * inode->i_private (with i_mutex making sure that it has only one user at
82 * a time): we would prefer not to enlarge the shmem inode just for that.
85 pgoff_t start; /* start of range currently being fallocated */
86 pgoff_t next; /* the next page offset to be fallocated */
90 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
91 char *name; /* xattr name */
96 /* Flag allocation requirements to shmem_getpage */
98 SGP_READ, /* don't exceed i_size, don't allocate page */
99 SGP_CACHE, /* don't exceed i_size, may allocate page */
100 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
101 SGP_WRITE, /* may exceed i_size, may allocate page */
105 static unsigned long shmem_default_max_blocks(void)
107 return totalram_pages / 2;
110 static unsigned long shmem_default_max_inodes(void)
112 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
116 static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
117 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
119 static inline int shmem_getpage(struct inode *inode, pgoff_t index,
120 struct page **pagep, enum sgp_type sgp, int *fault_type)
122 return shmem_getpage_gfp(inode, index, pagep, sgp,
123 mapping_gfp_mask(inode->i_mapping), fault_type);
126 static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
128 return sb->s_fs_info;
132 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
133 * for shared memory and for shared anonymous (/dev/zero) mappings
134 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
135 * consistent with the pre-accounting of private mappings ...
137 static inline int shmem_acct_size(unsigned long flags, loff_t size)
139 return (flags & VM_NORESERVE) ?
140 0 : security_vm_enough_memory_kern(VM_ACCT(size));
143 static inline void shmem_unacct_size(unsigned long flags, loff_t size)
145 if (!(flags & VM_NORESERVE))
146 vm_unacct_memory(VM_ACCT(size));
150 * ... whereas tmpfs objects are accounted incrementally as
151 * pages are allocated, in order to allow huge sparse files.
152 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
153 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
155 static inline int shmem_acct_block(unsigned long flags)
157 return (flags & VM_NORESERVE) ?
158 security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
161 static inline void shmem_unacct_blocks(unsigned long flags, long pages)
163 if (flags & VM_NORESERVE)
164 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
167 static const struct super_operations shmem_ops;
168 static const struct address_space_operations shmem_aops;
169 static const struct file_operations shmem_file_operations;
170 static const struct inode_operations shmem_inode_operations;
171 static const struct inode_operations shmem_dir_inode_operations;
172 static const struct inode_operations shmem_special_inode_operations;
173 static const struct vm_operations_struct shmem_vm_ops;
175 static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
176 .ra_pages = 0, /* No readahead */
177 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
180 static LIST_HEAD(shmem_swaplist);
181 static DEFINE_MUTEX(shmem_swaplist_mutex);
183 static int shmem_reserve_inode(struct super_block *sb)
185 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
186 if (sbinfo->max_inodes) {
187 spin_lock(&sbinfo->stat_lock);
188 if (!sbinfo->free_inodes) {
189 spin_unlock(&sbinfo->stat_lock);
192 sbinfo->free_inodes--;
193 spin_unlock(&sbinfo->stat_lock);
198 static void shmem_free_inode(struct super_block *sb)
200 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
201 if (sbinfo->max_inodes) {
202 spin_lock(&sbinfo->stat_lock);
203 sbinfo->free_inodes++;
204 spin_unlock(&sbinfo->stat_lock);
209 * shmem_recalc_inode - recalculate the block usage of an inode
210 * @inode: inode to recalc
212 * We have to calculate the free blocks since the mm can drop
213 * undirtied hole pages behind our back.
215 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
216 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
218 * It has to be called with the spinlock held.
220 static void shmem_recalc_inode(struct inode *inode)
222 struct shmem_inode_info *info = SHMEM_I(inode);
225 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
227 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
228 if (sbinfo->max_blocks)
229 percpu_counter_add(&sbinfo->used_blocks, -freed);
230 info->alloced -= freed;
231 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
232 shmem_unacct_blocks(info->flags, freed);
237 * Replace item expected in radix tree by a new item, while holding tree lock.
239 static int shmem_radix_tree_replace(struct address_space *mapping,
240 pgoff_t index, void *expected, void *replacement)
245 VM_BUG_ON(!expected);
246 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
248 item = radix_tree_deref_slot_protected(pslot,
249 &mapping->tree_lock);
250 if (item != expected)
253 radix_tree_replace_slot(pslot, replacement);
255 radix_tree_delete(&mapping->page_tree, index);
260 * Like add_to_page_cache_locked, but error if expected item has gone.
262 static int shmem_add_to_page_cache(struct page *page,
263 struct address_space *mapping,
264 pgoff_t index, gfp_t gfp, void *expected)
268 VM_BUG_ON(!PageLocked(page));
269 VM_BUG_ON(!PageSwapBacked(page));
272 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
274 page_cache_get(page);
275 page->mapping = mapping;
278 spin_lock_irq(&mapping->tree_lock);
280 error = radix_tree_insert(&mapping->page_tree,
283 error = shmem_radix_tree_replace(mapping, index,
287 __inc_zone_page_state(page, NR_FILE_PAGES);
288 __inc_zone_page_state(page, NR_SHMEM);
289 spin_unlock_irq(&mapping->tree_lock);
291 page->mapping = NULL;
292 spin_unlock_irq(&mapping->tree_lock);
293 page_cache_release(page);
296 radix_tree_preload_end();
299 mem_cgroup_uncharge_cache_page(page);
304 * Like delete_from_page_cache, but substitutes swap for page.
306 static void shmem_delete_from_page_cache(struct page *page, void *radswap)
308 struct address_space *mapping = page->mapping;
311 spin_lock_irq(&mapping->tree_lock);
312 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
313 page->mapping = NULL;
315 __dec_zone_page_state(page, NR_FILE_PAGES);
316 __dec_zone_page_state(page, NR_SHMEM);
317 spin_unlock_irq(&mapping->tree_lock);
318 page_cache_release(page);
323 * Like find_get_pages, but collecting swap entries as well as pages.
325 static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
326 pgoff_t start, unsigned int nr_pages,
327 struct page **pages, pgoff_t *indices)
331 unsigned int nr_found;
335 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
336 (void ***)pages, indices, start, nr_pages);
338 for (i = 0; i < nr_found; i++) {
341 page = radix_tree_deref_slot((void **)pages[i]);
344 if (radix_tree_exception(page)) {
345 if (radix_tree_deref_retry(page))
348 * Otherwise, we must be storing a swap entry
349 * here as an exceptional entry: so return it
350 * without attempting to raise page count.
354 if (!page_cache_get_speculative(page))
357 /* Has the page moved? */
358 if (unlikely(page != *((void **)pages[i]))) {
359 page_cache_release(page);
363 indices[ret] = indices[i];
367 if (unlikely(!ret && nr_found))
374 * Remove swap entry from radix tree, free the swap and its page cache.
376 static int shmem_free_swap(struct address_space *mapping,
377 pgoff_t index, void *radswap)
381 spin_lock_irq(&mapping->tree_lock);
382 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
383 spin_unlock_irq(&mapping->tree_lock);
385 free_swap_and_cache(radix_to_swp_entry(radswap));
390 * Pagevec may contain swap entries, so shuffle up pages before releasing.
392 static void shmem_deswap_pagevec(struct pagevec *pvec)
396 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
397 struct page *page = pvec->pages[i];
398 if (!radix_tree_exceptional_entry(page))
399 pvec->pages[j++] = page;
405 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
407 void shmem_unlock_mapping(struct address_space *mapping)
410 pgoff_t indices[PAGEVEC_SIZE];
413 pagevec_init(&pvec, 0);
415 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
417 while (!mapping_unevictable(mapping)) {
419 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
420 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
422 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
423 PAGEVEC_SIZE, pvec.pages, indices);
426 index = indices[pvec.nr - 1] + 1;
427 shmem_deswap_pagevec(&pvec);
428 check_move_unevictable_pages(pvec.pages, pvec.nr);
429 pagevec_release(&pvec);
435 * Remove range of pages and swap entries from radix tree, and free them.
437 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
439 struct address_space *mapping = inode->i_mapping;
440 struct shmem_inode_info *info = SHMEM_I(inode);
441 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
442 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
443 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
445 pgoff_t indices[PAGEVEC_SIZE];
446 long nr_swaps_freed = 0;
450 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
452 pagevec_init(&pvec, 0);
454 while (index <= end) {
455 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
456 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
457 pvec.pages, indices);
460 mem_cgroup_uncharge_start();
461 for (i = 0; i < pagevec_count(&pvec); i++) {
462 struct page *page = pvec.pages[i];
468 if (radix_tree_exceptional_entry(page)) {
469 nr_swaps_freed += !shmem_free_swap(mapping,
474 if (!trylock_page(page))
476 if (page->mapping == mapping) {
477 VM_BUG_ON(PageWriteback(page));
478 truncate_inode_page(mapping, page);
482 shmem_deswap_pagevec(&pvec);
483 pagevec_release(&pvec);
484 mem_cgroup_uncharge_end();
490 struct page *page = NULL;
491 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
493 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
494 set_page_dirty(page);
496 page_cache_release(page);
503 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
504 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
505 pvec.pages, indices);
512 if (index == start && indices[0] > end) {
513 shmem_deswap_pagevec(&pvec);
514 pagevec_release(&pvec);
517 mem_cgroup_uncharge_start();
518 for (i = 0; i < pagevec_count(&pvec); i++) {
519 struct page *page = pvec.pages[i];
525 if (radix_tree_exceptional_entry(page)) {
526 nr_swaps_freed += !shmem_free_swap(mapping,
532 if (page->mapping == mapping) {
533 VM_BUG_ON(PageWriteback(page));
534 truncate_inode_page(mapping, page);
538 shmem_deswap_pagevec(&pvec);
539 pagevec_release(&pvec);
540 mem_cgroup_uncharge_end();
544 spin_lock(&info->lock);
545 info->swapped -= nr_swaps_freed;
546 shmem_recalc_inode(inode);
547 spin_unlock(&info->lock);
549 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
551 EXPORT_SYMBOL_GPL(shmem_truncate_range);
553 static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
555 struct inode *inode = dentry->d_inode;
558 error = inode_change_ok(inode, attr);
562 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
563 loff_t oldsize = inode->i_size;
564 loff_t newsize = attr->ia_size;
566 if (newsize != oldsize) {
567 i_size_write(inode, newsize);
568 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
570 if (newsize < oldsize) {
571 loff_t holebegin = round_up(newsize, PAGE_SIZE);
572 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
573 shmem_truncate_range(inode, newsize, (loff_t)-1);
574 /* unmap again to remove racily COWed private pages */
575 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
579 setattr_copy(inode, attr);
580 #ifdef CONFIG_TMPFS_POSIX_ACL
581 if (attr->ia_valid & ATTR_MODE)
582 error = generic_acl_chmod(inode);
587 static void shmem_evict_inode(struct inode *inode)
589 struct shmem_inode_info *info = SHMEM_I(inode);
590 struct shmem_xattr *xattr, *nxattr;
592 if (inode->i_mapping->a_ops == &shmem_aops) {
593 shmem_unacct_size(info->flags, inode->i_size);
595 shmem_truncate_range(inode, 0, (loff_t)-1);
596 if (!list_empty(&info->swaplist)) {
597 mutex_lock(&shmem_swaplist_mutex);
598 list_del_init(&info->swaplist);
599 mutex_unlock(&shmem_swaplist_mutex);
602 kfree(info->symlink);
604 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
608 WARN_ON(inode->i_blocks);
609 shmem_free_inode(inode->i_sb);
610 end_writeback(inode);
614 * If swap found in inode, free it and move page from swapcache to filecache.
616 static int shmem_unuse_inode(struct shmem_inode_info *info,
617 swp_entry_t swap, struct page *page)
619 struct address_space *mapping = info->vfs_inode.i_mapping;
624 radswap = swp_to_radix_entry(swap);
625 index = radix_tree_locate_item(&mapping->page_tree, radswap);
630 * Move _head_ to start search for next from here.
631 * But be careful: shmem_evict_inode checks list_empty without taking
632 * mutex, and there's an instant in list_move_tail when info->swaplist
633 * would appear empty, if it were the only one on shmem_swaplist.
635 if (shmem_swaplist.next != &info->swaplist)
636 list_move_tail(&shmem_swaplist, &info->swaplist);
639 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
640 * but also to hold up shmem_evict_inode(): so inode cannot be freed
641 * beneath us (pagelock doesn't help until the page is in pagecache).
643 error = shmem_add_to_page_cache(page, mapping, index,
644 GFP_NOWAIT, radswap);
645 /* which does mem_cgroup_uncharge_cache_page on error */
647 if (error != -ENOMEM) {
649 * Truncation and eviction use free_swap_and_cache(), which
650 * only does trylock page: if we raced, best clean up here.
652 delete_from_swap_cache(page);
653 set_page_dirty(page);
655 spin_lock(&info->lock);
657 spin_unlock(&info->lock);
660 error = 1; /* not an error, but entry was found */
666 * Search through swapped inodes to find and replace swap by page.
668 int shmem_unuse(swp_entry_t swap, struct page *page)
670 struct list_head *this, *next;
671 struct shmem_inode_info *info;
676 * Charge page using GFP_KERNEL while we can wait, before taking
677 * the shmem_swaplist_mutex which might hold up shmem_writepage().
678 * Charged back to the user (not to caller) when swap account is used.
680 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
683 /* No radix_tree_preload: swap entry keeps a place for page in tree */
685 mutex_lock(&shmem_swaplist_mutex);
686 list_for_each_safe(this, next, &shmem_swaplist) {
687 info = list_entry(this, struct shmem_inode_info, swaplist);
689 found = shmem_unuse_inode(info, swap, page);
691 list_del_init(&info->swaplist);
696 mutex_unlock(&shmem_swaplist_mutex);
699 mem_cgroup_uncharge_cache_page(page);
704 page_cache_release(page);
709 * Move the page from the page cache to the swap cache.
711 static int shmem_writepage(struct page *page, struct writeback_control *wbc)
713 struct shmem_inode_info *info;
714 struct address_space *mapping;
719 BUG_ON(!PageLocked(page));
720 mapping = page->mapping;
722 inode = mapping->host;
723 info = SHMEM_I(inode);
724 if (info->flags & VM_LOCKED)
726 if (!total_swap_pages)
730 * shmem_backing_dev_info's capabilities prevent regular writeback or
731 * sync from ever calling shmem_writepage; but a stacking filesystem
732 * might use ->writepage of its underlying filesystem, in which case
733 * tmpfs should write out to swap only in response to memory pressure,
734 * and not for the writeback threads or sync.
736 if (!wbc->for_reclaim) {
737 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
740 swap = get_swap_page();
745 * Add inode to shmem_unuse()'s list of swapped-out inodes,
746 * if it's not already there. Do it now before the page is
747 * moved to swap cache, when its pagelock no longer protects
748 * the inode from eviction. But don't unlock the mutex until
749 * we've incremented swapped, because shmem_unuse_inode() will
750 * prune a !swapped inode from the swaplist under this mutex.
752 mutex_lock(&shmem_swaplist_mutex);
753 if (list_empty(&info->swaplist))
754 list_add_tail(&info->swaplist, &shmem_swaplist);
756 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
757 swap_shmem_alloc(swap);
758 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
760 spin_lock(&info->lock);
762 shmem_recalc_inode(inode);
763 spin_unlock(&info->lock);
765 mutex_unlock(&shmem_swaplist_mutex);
766 BUG_ON(page_mapped(page));
767 swap_writepage(page, wbc);
771 mutex_unlock(&shmem_swaplist_mutex);
772 swapcache_free(swap, NULL);
774 set_page_dirty(page);
775 if (wbc->for_reclaim)
776 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
783 static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
787 if (!mpol || mpol->mode == MPOL_DEFAULT)
788 return; /* show nothing */
790 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
792 seq_printf(seq, ",mpol=%s", buffer);
795 static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
797 struct mempolicy *mpol = NULL;
799 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
802 spin_unlock(&sbinfo->stat_lock);
806 #endif /* CONFIG_TMPFS */
808 static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
809 struct shmem_inode_info *info, pgoff_t index)
811 struct vm_area_struct pvma;
814 /* Create a pseudo vma that just contains the policy */
816 pvma.vm_pgoff = index;
818 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
820 page = swapin_readahead(swap, gfp, &pvma, 0);
822 /* Drop reference taken by mpol_shared_policy_lookup() */
823 mpol_cond_put(pvma.vm_policy);
828 static struct page *shmem_alloc_page(gfp_t gfp,
829 struct shmem_inode_info *info, pgoff_t index)
831 struct vm_area_struct pvma;
834 /* Create a pseudo vma that just contains the policy */
836 pvma.vm_pgoff = index;
838 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
840 page = alloc_page_vma(gfp, &pvma, 0);
842 /* Drop reference taken by mpol_shared_policy_lookup() */
843 mpol_cond_put(pvma.vm_policy);
847 #else /* !CONFIG_NUMA */
849 static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
852 #endif /* CONFIG_TMPFS */
854 static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
855 struct shmem_inode_info *info, pgoff_t index)
857 return swapin_readahead(swap, gfp, NULL, 0);
860 static inline struct page *shmem_alloc_page(gfp_t gfp,
861 struct shmem_inode_info *info, pgoff_t index)
863 return alloc_page(gfp);
865 #endif /* CONFIG_NUMA */
867 #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
868 static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
875 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
877 * If we allocate a new one we do not mark it dirty. That's up to the
878 * vm. If we swap it in we mark it dirty since we also free the swap
879 * entry since a page cannot live in both the swap and page cache
881 static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
882 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
884 struct address_space *mapping = inode->i_mapping;
885 struct shmem_inode_info *info;
886 struct shmem_sb_info *sbinfo;
892 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
896 page = find_lock_page(mapping, index);
897 if (radix_tree_exceptional_entry(page)) {
898 swap = radix_to_swp_entry(page);
902 if (sgp != SGP_WRITE &&
903 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
908 if (page || (sgp == SGP_READ && !swap.val)) {
910 * Once we can get the page lock, it must be uptodate:
911 * if there were an error in reading back from swap,
912 * the page would not be inserted into the filecache.
914 BUG_ON(page && !PageUptodate(page));
920 * Fast cache lookup did not find it:
921 * bring it back from swap or allocate.
923 info = SHMEM_I(inode);
924 sbinfo = SHMEM_SB(inode->i_sb);
927 /* Look it up and read it in.. */
928 page = lookup_swap_cache(swap);
930 /* here we actually do the io */
932 *fault_type |= VM_FAULT_MAJOR;
933 page = shmem_swapin(swap, gfp, info, index);
940 /* We have to do this with page locked to prevent races */
942 if (!PageUptodate(page)) {
946 wait_on_page_writeback(page);
948 /* Someone may have already done it for us */
950 if (page->mapping == mapping &&
951 page->index == index)
957 error = mem_cgroup_cache_charge(page, current->mm,
958 gfp & GFP_RECLAIM_MASK);
960 error = shmem_add_to_page_cache(page, mapping, index,
961 gfp, swp_to_radix_entry(swap));
965 spin_lock(&info->lock);
967 shmem_recalc_inode(inode);
968 spin_unlock(&info->lock);
970 delete_from_swap_cache(page);
971 set_page_dirty(page);
975 if (shmem_acct_block(info->flags)) {
979 if (sbinfo->max_blocks) {
980 if (percpu_counter_compare(&sbinfo->used_blocks,
981 sbinfo->max_blocks) >= 0) {
985 percpu_counter_inc(&sbinfo->used_blocks);
988 page = shmem_alloc_page(gfp, info, index);
994 SetPageSwapBacked(page);
995 __set_page_locked(page);
996 error = mem_cgroup_cache_charge(page, current->mm,
997 gfp & GFP_RECLAIM_MASK);
999 error = shmem_add_to_page_cache(page, mapping, index,
1003 lru_cache_add_anon(page);
1005 spin_lock(&info->lock);
1007 inode->i_blocks += BLOCKS_PER_PAGE;
1008 shmem_recalc_inode(inode);
1009 spin_unlock(&info->lock);
1011 clear_highpage(page);
1012 flush_dcache_page(page);
1013 SetPageUptodate(page);
1014 if (sgp == SGP_DIRTY)
1015 set_page_dirty(page);
1018 /* Perhaps the file has been truncated since we checked */
1019 if (sgp != SGP_WRITE &&
1020 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1031 ClearPageDirty(page);
1032 delete_from_page_cache(page);
1033 spin_lock(&info->lock);
1035 inode->i_blocks -= BLOCKS_PER_PAGE;
1036 spin_unlock(&info->lock);
1038 if (sbinfo->max_blocks)
1039 percpu_counter_add(&sbinfo->used_blocks, -1);
1041 shmem_unacct_blocks(info->flags, 1);
1043 if (swap.val && error != -EINVAL) {
1044 struct page *test = find_get_page(mapping, index);
1045 if (test && !radix_tree_exceptional_entry(test))
1046 page_cache_release(test);
1047 /* Have another try if the entry has changed */
1048 if (test != swp_to_radix_entry(swap))
1053 page_cache_release(page);
1055 if (error == -ENOSPC && !once++) {
1056 info = SHMEM_I(inode);
1057 spin_lock(&info->lock);
1058 shmem_recalc_inode(inode);
1059 spin_unlock(&info->lock);
1062 if (error == -EEXIST)
1067 static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1069 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1071 int ret = VM_FAULT_LOCKED;
1074 * Trinity finds that probing a hole which tmpfs is punching can
1075 * prevent the hole-punch from ever completing: which in turn
1076 * locks writers out with its hold on i_mutex. So refrain from
1077 * faulting pages into the hole while it's being punched, and
1078 * wait on i_mutex to be released if vmf->flags permits.
1080 if (unlikely(inode->i_private)) {
1081 struct shmem_falloc *shmem_falloc;
1083 spin_lock(&inode->i_lock);
1084 shmem_falloc = inode->i_private;
1085 if (!shmem_falloc ||
1086 vmf->pgoff < shmem_falloc->start ||
1087 vmf->pgoff >= shmem_falloc->next)
1088 shmem_falloc = NULL;
1089 spin_unlock(&inode->i_lock);
1091 * i_lock has protected us from taking shmem_falloc seriously
1092 * once return from vmtruncate_range() went back up that stack.
1093 * i_lock does not serialize with i_mutex at all, but it does
1094 * not matter if sometimes we wait unnecessarily, or sometimes
1095 * miss out on waiting: we just need to make those cases rare.
1098 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1099 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
1100 up_read(&vma->vm_mm->mmap_sem);
1101 mutex_lock(&inode->i_mutex);
1102 mutex_unlock(&inode->i_mutex);
1103 return VM_FAULT_RETRY;
1105 /* cond_resched? Leave that to GUP or return to user */
1106 return VM_FAULT_NOPAGE;
1110 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1112 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
1114 if (ret & VM_FAULT_MAJOR) {
1115 count_vm_event(PGMAJFAULT);
1116 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1121 int vmtruncate_range(struct inode *inode, loff_t lstart, loff_t lend)
1124 * If the underlying filesystem is not going to provide
1125 * a way to truncate a range of blocks (punch a hole) -
1126 * we should return failure right now.
1127 * Only CONFIG_SHMEM shmem.c ever supported i_op->truncate_range().
1129 if (inode->i_op->truncate_range != shmem_truncate_range)
1132 mutex_lock(&inode->i_mutex);
1134 struct shmem_falloc shmem_falloc;
1135 struct address_space *mapping = inode->i_mapping;
1136 loff_t unmap_start = round_up(lstart, PAGE_SIZE);
1137 loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
1139 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
1140 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
1141 spin_lock(&inode->i_lock);
1142 inode->i_private = &shmem_falloc;
1143 spin_unlock(&inode->i_lock);
1145 if ((u64)unmap_end > (u64)unmap_start)
1146 unmap_mapping_range(mapping, unmap_start,
1147 1 + unmap_end - unmap_start, 0);
1148 shmem_truncate_range(inode, lstart, lend);
1149 /* No need to unmap again: hole-punching leaves COWed pages */
1151 spin_lock(&inode->i_lock);
1152 inode->i_private = NULL;
1153 spin_unlock(&inode->i_lock);
1155 mutex_unlock(&inode->i_mutex);
1160 static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
1162 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1163 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
1166 static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1169 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1172 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1173 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
1177 int shmem_lock(struct file *file, int lock, struct user_struct *user)
1179 struct inode *inode = file->f_path.dentry->d_inode;
1180 struct shmem_inode_info *info = SHMEM_I(inode);
1181 int retval = -ENOMEM;
1183 spin_lock(&info->lock);
1184 if (lock && !(info->flags & VM_LOCKED)) {
1185 if (!user_shm_lock(inode->i_size, user))
1187 info->flags |= VM_LOCKED;
1188 mapping_set_unevictable(file->f_mapping);
1190 if (!lock && (info->flags & VM_LOCKED) && user) {
1191 user_shm_unlock(inode->i_size, user);
1192 info->flags &= ~VM_LOCKED;
1193 mapping_clear_unevictable(file->f_mapping);
1198 spin_unlock(&info->lock);
1202 static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1204 file_accessed(file);
1205 vma->vm_ops = &shmem_vm_ops;
1206 vma->vm_flags |= VM_CAN_NONLINEAR;
1210 static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1211 int mode, dev_t dev, unsigned long flags)
1213 struct inode *inode;
1214 struct shmem_inode_info *info;
1215 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1217 if (shmem_reserve_inode(sb))
1220 inode = new_inode(sb);
1222 inode->i_ino = get_next_ino();
1223 inode_init_owner(inode, dir, mode);
1224 inode->i_blocks = 0;
1225 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1226 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1227 inode->i_generation = get_seconds();
1228 info = SHMEM_I(inode);
1229 memset(info, 0, (char *)inode - (char *)info);
1230 spin_lock_init(&info->lock);
1231 info->flags = flags & VM_NORESERVE;
1232 INIT_LIST_HEAD(&info->swaplist);
1233 INIT_LIST_HEAD(&info->xattr_list);
1234 cache_no_acl(inode);
1236 switch (mode & S_IFMT) {
1238 inode->i_op = &shmem_special_inode_operations;
1239 init_special_inode(inode, mode, dev);
1242 inode->i_mapping->a_ops = &shmem_aops;
1243 inode->i_op = &shmem_inode_operations;
1244 inode->i_fop = &shmem_file_operations;
1245 mpol_shared_policy_init(&info->policy,
1246 shmem_get_sbmpol(sbinfo));
1250 /* Some things misbehave if size == 0 on a directory */
1251 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1252 inode->i_op = &shmem_dir_inode_operations;
1253 inode->i_fop = &simple_dir_operations;
1257 * Must not load anything in the rbtree,
1258 * mpol_free_shared_policy will not be called.
1260 mpol_shared_policy_init(&info->policy, NULL);
1264 shmem_free_inode(sb);
1269 static const struct inode_operations shmem_symlink_inode_operations;
1270 static const struct inode_operations shmem_short_symlink_operations;
1273 shmem_write_begin(struct file *file, struct address_space *mapping,
1274 loff_t pos, unsigned len, unsigned flags,
1275 struct page **pagep, void **fsdata)
1277 struct inode *inode = mapping->host;
1278 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1279 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1283 shmem_write_end(struct file *file, struct address_space *mapping,
1284 loff_t pos, unsigned len, unsigned copied,
1285 struct page *page, void *fsdata)
1287 struct inode *inode = mapping->host;
1289 if (pos + copied > inode->i_size)
1290 i_size_write(inode, pos + copied);
1292 set_page_dirty(page);
1294 page_cache_release(page);
1299 static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1301 struct inode *inode = filp->f_path.dentry->d_inode;
1302 struct address_space *mapping = inode->i_mapping;
1304 unsigned long offset;
1305 enum sgp_type sgp = SGP_READ;
1308 * Might this read be for a stacking filesystem? Then when reading
1309 * holes of a sparse file, we actually need to allocate those pages,
1310 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1312 if (segment_eq(get_fs(), KERNEL_DS))
1315 index = *ppos >> PAGE_CACHE_SHIFT;
1316 offset = *ppos & ~PAGE_CACHE_MASK;
1319 struct page *page = NULL;
1321 unsigned long nr, ret;
1322 loff_t i_size = i_size_read(inode);
1324 end_index = i_size >> PAGE_CACHE_SHIFT;
1325 if (index > end_index)
1327 if (index == end_index) {
1328 nr = i_size & ~PAGE_CACHE_MASK;
1333 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
1335 if (desc->error == -EINVAL)
1343 * We must evaluate after, since reads (unlike writes)
1344 * are called without i_mutex protection against truncate
1346 nr = PAGE_CACHE_SIZE;
1347 i_size = i_size_read(inode);
1348 end_index = i_size >> PAGE_CACHE_SHIFT;
1349 if (index == end_index) {
1350 nr = i_size & ~PAGE_CACHE_MASK;
1353 page_cache_release(page);
1361 * If users can be writing to this page using arbitrary
1362 * virtual addresses, take care about potential aliasing
1363 * before reading the page on the kernel side.
1365 if (mapping_writably_mapped(mapping))
1366 flush_dcache_page(page);
1368 * Mark the page accessed if we read the beginning.
1371 mark_page_accessed(page);
1373 page = ZERO_PAGE(0);
1374 page_cache_get(page);
1378 * Ok, we have the page, and it's up-to-date, so
1379 * now we can copy it to user space...
1381 * The actor routine returns how many bytes were actually used..
1382 * NOTE! This may not be the same as how much of a user buffer
1383 * we filled up (we may be padding etc), so we can only update
1384 * "pos" here (the actor routine has to update the user buffer
1385 * pointers and the remaining count).
1387 ret = actor(desc, page, offset, nr);
1389 index += offset >> PAGE_CACHE_SHIFT;
1390 offset &= ~PAGE_CACHE_MASK;
1392 page_cache_release(page);
1393 if (ret != nr || !desc->count)
1399 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1400 file_accessed(filp);
1403 static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1404 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
1406 struct file *filp = iocb->ki_filp;
1410 loff_t *ppos = &iocb->ki_pos;
1412 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1416 for (seg = 0; seg < nr_segs; seg++) {
1417 read_descriptor_t desc;
1420 desc.arg.buf = iov[seg].iov_base;
1421 desc.count = iov[seg].iov_len;
1422 if (desc.count == 0)
1425 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1426 retval += desc.written;
1428 retval = retval ?: desc.error;
1437 static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1438 struct pipe_inode_info *pipe, size_t len,
1441 struct address_space *mapping = in->f_mapping;
1442 struct inode *inode = mapping->host;
1443 unsigned int loff, nr_pages, req_pages;
1444 struct page *pages[PIPE_DEF_BUFFERS];
1445 struct partial_page partial[PIPE_DEF_BUFFERS];
1447 pgoff_t index, end_index;
1450 struct splice_pipe_desc spd = {
1453 .nr_pages_max = PIPE_DEF_BUFFERS,
1455 .ops = &page_cache_pipe_buf_ops,
1456 .spd_release = spd_release_page,
1459 isize = i_size_read(inode);
1460 if (unlikely(*ppos >= isize))
1463 left = isize - *ppos;
1464 if (unlikely(left < len))
1467 if (splice_grow_spd(pipe, &spd))
1470 index = *ppos >> PAGE_CACHE_SHIFT;
1471 loff = *ppos & ~PAGE_CACHE_MASK;
1472 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1473 nr_pages = min(req_pages, pipe->buffers);
1475 spd.nr_pages = find_get_pages_contig(mapping, index,
1476 nr_pages, spd.pages);
1477 index += spd.nr_pages;
1480 while (spd.nr_pages < nr_pages) {
1481 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1485 spd.pages[spd.nr_pages++] = page;
1489 index = *ppos >> PAGE_CACHE_SHIFT;
1490 nr_pages = spd.nr_pages;
1493 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1494 unsigned int this_len;
1499 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1500 page = spd.pages[page_nr];
1502 if (!PageUptodate(page) || page->mapping != mapping) {
1503 error = shmem_getpage(inode, index, &page,
1508 page_cache_release(spd.pages[page_nr]);
1509 spd.pages[page_nr] = page;
1512 isize = i_size_read(inode);
1513 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1514 if (unlikely(!isize || index > end_index))
1517 if (end_index == index) {
1520 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1524 this_len = min(this_len, plen - loff);
1528 spd.partial[page_nr].offset = loff;
1529 spd.partial[page_nr].len = this_len;
1536 while (page_nr < nr_pages)
1537 page_cache_release(spd.pages[page_nr++]);
1540 error = splice_to_pipe(pipe, &spd);
1542 splice_shrink_spd(&spd);
1551 static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1553 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1555 buf->f_type = TMPFS_MAGIC;
1556 buf->f_bsize = PAGE_CACHE_SIZE;
1557 buf->f_namelen = NAME_MAX;
1558 if (sbinfo->max_blocks) {
1559 buf->f_blocks = sbinfo->max_blocks;
1561 buf->f_bfree = sbinfo->max_blocks -
1562 percpu_counter_sum(&sbinfo->used_blocks);
1564 if (sbinfo->max_inodes) {
1565 buf->f_files = sbinfo->max_inodes;
1566 buf->f_ffree = sbinfo->free_inodes;
1568 /* else leave those fields 0 like simple_statfs */
1573 * File creation. Allocate an inode, and we're done..
1576 shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1578 struct inode *inode;
1579 int error = -ENOSPC;
1581 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
1583 error = security_inode_init_security(inode, dir,
1587 if (error != -EOPNOTSUPP) {
1592 #ifdef CONFIG_TMPFS_POSIX_ACL
1593 error = generic_acl_init(inode, dir);
1601 dir->i_size += BOGO_DIRENT_SIZE;
1602 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1603 d_instantiate(dentry, inode);
1604 dget(dentry); /* Extra count - pin the dentry in core */
1609 static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1613 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1619 static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1620 struct nameidata *nd)
1622 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1628 static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1630 struct inode *inode = old_dentry->d_inode;
1634 * No ordinary (disk based) filesystem counts links as inodes;
1635 * but each new link needs a new dentry, pinning lowmem, and
1636 * tmpfs dentries cannot be pruned until they are unlinked.
1638 ret = shmem_reserve_inode(inode->i_sb);
1642 dir->i_size += BOGO_DIRENT_SIZE;
1643 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1645 ihold(inode); /* New dentry reference */
1646 dget(dentry); /* Extra pinning count for the created dentry */
1647 d_instantiate(dentry, inode);
1652 static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1654 struct inode *inode = dentry->d_inode;
1656 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1657 shmem_free_inode(inode->i_sb);
1659 dir->i_size -= BOGO_DIRENT_SIZE;
1660 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1662 dput(dentry); /* Undo the count from "create" - this does all the work */
1666 static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1668 if (!simple_empty(dentry))
1671 drop_nlink(dentry->d_inode);
1673 return shmem_unlink(dir, dentry);
1677 * The VFS layer already does all the dentry stuff for rename,
1678 * we just have to decrement the usage count for the target if
1679 * it exists so that the VFS layer correctly free's it when it
1682 static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1684 struct inode *inode = old_dentry->d_inode;
1685 int they_are_dirs = S_ISDIR(inode->i_mode);
1687 if (!simple_empty(new_dentry))
1690 if (new_dentry->d_inode) {
1691 (void) shmem_unlink(new_dir, new_dentry);
1693 drop_nlink(old_dir);
1694 } else if (they_are_dirs) {
1695 drop_nlink(old_dir);
1699 old_dir->i_size -= BOGO_DIRENT_SIZE;
1700 new_dir->i_size += BOGO_DIRENT_SIZE;
1701 old_dir->i_ctime = old_dir->i_mtime =
1702 new_dir->i_ctime = new_dir->i_mtime =
1703 inode->i_ctime = CURRENT_TIME;
1707 static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1711 struct inode *inode;
1714 struct shmem_inode_info *info;
1716 len = strlen(symname) + 1;
1717 if (len > PAGE_CACHE_SIZE)
1718 return -ENAMETOOLONG;
1720 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
1724 error = security_inode_init_security(inode, dir, &dentry->d_name,
1727 if (error != -EOPNOTSUPP) {
1734 info = SHMEM_I(inode);
1735 inode->i_size = len-1;
1736 if (len <= SHORT_SYMLINK_LEN) {
1737 info->symlink = kmemdup(symname, len, GFP_KERNEL);
1738 if (!info->symlink) {
1742 inode->i_op = &shmem_short_symlink_operations;
1744 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1749 inode->i_mapping->a_ops = &shmem_aops;
1750 inode->i_op = &shmem_symlink_inode_operations;
1751 kaddr = kmap_atomic(page, KM_USER0);
1752 memcpy(kaddr, symname, len);
1753 kunmap_atomic(kaddr, KM_USER0);
1754 set_page_dirty(page);
1756 page_cache_release(page);
1758 dir->i_size += BOGO_DIRENT_SIZE;
1759 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1760 d_instantiate(dentry, inode);
1765 static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
1767 nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
1771 static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1773 struct page *page = NULL;
1774 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1775 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
1781 static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1783 if (!IS_ERR(nd_get_link(nd))) {
1784 struct page *page = cookie;
1786 mark_page_accessed(page);
1787 page_cache_release(page);
1791 #ifdef CONFIG_TMPFS_XATTR
1793 * Superblocks without xattr inode operations may get some security.* xattr
1794 * support from the LSM "for free". As soon as we have any other xattrs
1795 * like ACLs, we also need to implement the security.* handlers at
1796 * filesystem level, though.
1799 static int shmem_xattr_get(struct dentry *dentry, const char *name,
1800 void *buffer, size_t size)
1802 struct shmem_inode_info *info;
1803 struct shmem_xattr *xattr;
1806 info = SHMEM_I(dentry->d_inode);
1808 spin_lock(&info->lock);
1809 list_for_each_entry(xattr, &info->xattr_list, list) {
1810 if (strcmp(name, xattr->name))
1815 if (size < xattr->size)
1818 memcpy(buffer, xattr->value, xattr->size);
1822 spin_unlock(&info->lock);
1826 static int shmem_xattr_set(struct dentry *dentry, const char *name,
1827 const void *value, size_t size, int flags)
1829 struct inode *inode = dentry->d_inode;
1830 struct shmem_inode_info *info = SHMEM_I(inode);
1831 struct shmem_xattr *xattr;
1832 struct shmem_xattr *new_xattr = NULL;
1836 /* value == NULL means remove */
1839 len = sizeof(*new_xattr) + size;
1840 if (len <= sizeof(*new_xattr))
1843 new_xattr = kmalloc(len, GFP_KERNEL);
1847 new_xattr->name = kstrdup(name, GFP_KERNEL);
1848 if (!new_xattr->name) {
1853 new_xattr->size = size;
1854 memcpy(new_xattr->value, value, size);
1857 spin_lock(&info->lock);
1858 list_for_each_entry(xattr, &info->xattr_list, list) {
1859 if (!strcmp(name, xattr->name)) {
1860 if (flags & XATTR_CREATE) {
1863 } else if (new_xattr) {
1864 list_replace(&xattr->list, &new_xattr->list);
1866 list_del(&xattr->list);
1871 if (flags & XATTR_REPLACE) {
1875 list_add(&new_xattr->list, &info->xattr_list);
1879 spin_unlock(&info->lock);
1886 static const struct xattr_handler *shmem_xattr_handlers[] = {
1887 #ifdef CONFIG_TMPFS_POSIX_ACL
1888 &generic_acl_access_handler,
1889 &generic_acl_default_handler,
1894 static int shmem_xattr_validate(const char *name)
1896 struct { const char *prefix; size_t len; } arr[] = {
1897 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1898 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1902 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1903 size_t preflen = arr[i].len;
1904 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1913 static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1914 void *buffer, size_t size)
1919 * If this is a request for a synthetic attribute in the system.*
1920 * namespace use the generic infrastructure to resolve a handler
1921 * for it via sb->s_xattr.
1923 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1924 return generic_getxattr(dentry, name, buffer, size);
1926 err = shmem_xattr_validate(name);
1930 return shmem_xattr_get(dentry, name, buffer, size);
1933 static int shmem_setxattr(struct dentry *dentry, const char *name,
1934 const void *value, size_t size, int flags)
1939 * If this is a request for a synthetic attribute in the system.*
1940 * namespace use the generic infrastructure to resolve a handler
1941 * for it via sb->s_xattr.
1943 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1944 return generic_setxattr(dentry, name, value, size, flags);
1946 err = shmem_xattr_validate(name);
1951 value = ""; /* empty EA, do not remove */
1953 return shmem_xattr_set(dentry, name, value, size, flags);
1957 static int shmem_removexattr(struct dentry *dentry, const char *name)
1962 * If this is a request for a synthetic attribute in the system.*
1963 * namespace use the generic infrastructure to resolve a handler
1964 * for it via sb->s_xattr.
1966 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1967 return generic_removexattr(dentry, name);
1969 err = shmem_xattr_validate(name);
1973 return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
1976 static bool xattr_is_trusted(const char *name)
1978 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1981 static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
1983 bool trusted = capable(CAP_SYS_ADMIN);
1984 struct shmem_xattr *xattr;
1985 struct shmem_inode_info *info;
1988 info = SHMEM_I(dentry->d_inode);
1990 spin_lock(&info->lock);
1991 list_for_each_entry(xattr, &info->xattr_list, list) {
1994 /* skip "trusted." attributes for unprivileged callers */
1995 if (!trusted && xattr_is_trusted(xattr->name))
1998 len = strlen(xattr->name) + 1;
2005 memcpy(buffer, xattr->name, len);
2009 spin_unlock(&info->lock);
2013 #endif /* CONFIG_TMPFS_XATTR */
2015 static const struct inode_operations shmem_short_symlink_operations = {
2016 .readlink = generic_readlink,
2017 .follow_link = shmem_follow_short_symlink,
2018 #ifdef CONFIG_TMPFS_XATTR
2019 .setxattr = shmem_setxattr,
2020 .getxattr = shmem_getxattr,
2021 .listxattr = shmem_listxattr,
2022 .removexattr = shmem_removexattr,
2026 static const struct inode_operations shmem_symlink_inode_operations = {
2027 .readlink = generic_readlink,
2028 .follow_link = shmem_follow_link,
2029 .put_link = shmem_put_link,
2030 #ifdef CONFIG_TMPFS_XATTR
2031 .setxattr = shmem_setxattr,
2032 .getxattr = shmem_getxattr,
2033 .listxattr = shmem_listxattr,
2034 .removexattr = shmem_removexattr,
2038 static struct dentry *shmem_get_parent(struct dentry *child)
2040 return ERR_PTR(-ESTALE);
2043 static int shmem_match(struct inode *ino, void *vfh)
2047 inum = (inum << 32) | fh[1];
2048 return ino->i_ino == inum && fh[0] == ino->i_generation;
2051 static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2052 struct fid *fid, int fh_len, int fh_type)
2054 struct inode *inode;
2055 struct dentry *dentry = NULL;
2062 inum = (inum << 32) | fid->raw[1];
2064 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2065 shmem_match, fid->raw);
2067 dentry = d_find_alias(inode);
2074 static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2077 struct inode *inode = dentry->d_inode;
2084 if (inode_unhashed(inode)) {
2085 /* Unfortunately insert_inode_hash is not idempotent,
2086 * so as we hash inodes here rather than at creation
2087 * time, we need a lock to ensure we only try
2090 static DEFINE_SPINLOCK(lock);
2092 if (inode_unhashed(inode))
2093 __insert_inode_hash(inode,
2094 inode->i_ino + inode->i_generation);
2098 fh[0] = inode->i_generation;
2099 fh[1] = inode->i_ino;
2100 fh[2] = ((__u64)inode->i_ino) >> 32;
2106 static const struct export_operations shmem_export_ops = {
2107 .get_parent = shmem_get_parent,
2108 .encode_fh = shmem_encode_fh,
2109 .fh_to_dentry = shmem_fh_to_dentry,
2112 static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2115 char *this_char, *value, *rest;
2117 while (options != NULL) {
2118 this_char = options;
2121 * NUL-terminate this option: unfortunately,
2122 * mount options form a comma-separated list,
2123 * but mpol's nodelist may also contain commas.
2125 options = strchr(options, ',');
2126 if (options == NULL)
2129 if (!isdigit(*options)) {
2136 if ((value = strchr(this_char,'=')) != NULL) {
2140 "tmpfs: No value for mount option '%s'\n",
2145 if (!strcmp(this_char,"size")) {
2146 unsigned long long size;
2147 size = memparse(value,&rest);
2149 size <<= PAGE_SHIFT;
2150 size *= totalram_pages;
2156 sbinfo->max_blocks =
2157 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
2158 } else if (!strcmp(this_char,"nr_blocks")) {
2159 sbinfo->max_blocks = memparse(value, &rest);
2162 } else if (!strcmp(this_char,"nr_inodes")) {
2163 sbinfo->max_inodes = memparse(value, &rest);
2166 } else if (!strcmp(this_char,"mode")) {
2169 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
2172 } else if (!strcmp(this_char,"uid")) {
2175 sbinfo->uid = simple_strtoul(value, &rest, 0);
2178 } else if (!strcmp(this_char,"gid")) {
2181 sbinfo->gid = simple_strtoul(value, &rest, 0);
2184 } else if (!strcmp(this_char,"mpol")) {
2185 if (mpol_parse_str(value, &sbinfo->mpol, 1))
2188 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2196 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2202 static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2204 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2205 struct shmem_sb_info config = *sbinfo;
2206 unsigned long inodes;
2207 int error = -EINVAL;
2210 if (shmem_parse_options(data, &config, true))
2213 spin_lock(&sbinfo->stat_lock);
2214 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
2215 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
2217 if (config.max_inodes < inodes)
2220 * Those tests disallow limited->unlimited while any are in use;
2221 * but we must separately disallow unlimited->limited, because
2222 * in that case we have no record of how much is already in use.
2224 if (config.max_blocks && !sbinfo->max_blocks)
2226 if (config.max_inodes && !sbinfo->max_inodes)
2230 sbinfo->max_blocks = config.max_blocks;
2231 sbinfo->max_inodes = config.max_inodes;
2232 sbinfo->free_inodes = config.max_inodes - inodes;
2235 * Preserve previous mempolicy unless mpol remount option was specified.
2238 mpol_put(sbinfo->mpol);
2239 sbinfo->mpol = config.mpol; /* transfers initial ref */
2242 spin_unlock(&sbinfo->stat_lock);
2246 static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2248 struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2250 if (sbinfo->max_blocks != shmem_default_max_blocks())
2251 seq_printf(seq, ",size=%luk",
2252 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2253 if (sbinfo->max_inodes != shmem_default_max_inodes())
2254 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2255 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2256 seq_printf(seq, ",mode=%03o", sbinfo->mode);
2257 if (sbinfo->uid != 0)
2258 seq_printf(seq, ",uid=%u", sbinfo->uid);
2259 if (sbinfo->gid != 0)
2260 seq_printf(seq, ",gid=%u", sbinfo->gid);
2261 shmem_show_mpol(seq, sbinfo->mpol);
2264 #endif /* CONFIG_TMPFS */
2266 static void shmem_put_super(struct super_block *sb)
2268 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2270 percpu_counter_destroy(&sbinfo->used_blocks);
2272 sb->s_fs_info = NULL;
2275 int shmem_fill_super(struct super_block *sb, void *data, int silent)
2277 struct inode *inode;
2278 struct dentry *root;
2279 struct shmem_sb_info *sbinfo;
2282 /* Round up to L1_CACHE_BYTES to resist false sharing */
2283 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
2284 L1_CACHE_BYTES), GFP_KERNEL);
2288 sbinfo->mode = S_IRWXUGO | S_ISVTX;
2289 sbinfo->uid = current_fsuid();
2290 sbinfo->gid = current_fsgid();
2291 sb->s_fs_info = sbinfo;
2295 * Per default we only allow half of the physical ram per
2296 * tmpfs instance, limiting inodes to one per page of lowmem;
2297 * but the internal instance is left unlimited.
2299 if (!(sb->s_flags & MS_NOUSER)) {
2300 sbinfo->max_blocks = shmem_default_max_blocks();
2301 sbinfo->max_inodes = shmem_default_max_inodes();
2302 if (shmem_parse_options(data, sbinfo, false)) {
2307 sb->s_export_op = &shmem_export_ops;
2309 sb->s_flags |= MS_NOUSER;
2312 spin_lock_init(&sbinfo->stat_lock);
2313 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2315 sbinfo->free_inodes = sbinfo->max_inodes;
2317 sb->s_maxbytes = MAX_LFS_FILESIZE;
2318 sb->s_blocksize = PAGE_CACHE_SIZE;
2319 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2320 sb->s_magic = TMPFS_MAGIC;
2321 sb->s_op = &shmem_ops;
2322 sb->s_time_gran = 1;
2323 #ifdef CONFIG_TMPFS_XATTR
2324 sb->s_xattr = shmem_xattr_handlers;
2326 #ifdef CONFIG_TMPFS_POSIX_ACL
2327 sb->s_flags |= MS_POSIXACL;
2330 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
2333 inode->i_uid = sbinfo->uid;
2334 inode->i_gid = sbinfo->gid;
2335 root = d_alloc_root(inode);
2344 shmem_put_super(sb);
2348 static struct kmem_cache *shmem_inode_cachep;
2350 static struct inode *shmem_alloc_inode(struct super_block *sb)
2352 struct shmem_inode_info *info;
2353 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2356 return &info->vfs_inode;
2359 static void shmem_destroy_callback(struct rcu_head *head)
2361 struct inode *inode = container_of(head, struct inode, i_rcu);
2362 INIT_LIST_HEAD(&inode->i_dentry);
2363 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2366 static void shmem_destroy_inode(struct inode *inode)
2368 if ((inode->i_mode & S_IFMT) == S_IFREG)
2369 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2370 call_rcu(&inode->i_rcu, shmem_destroy_callback);
2373 static void shmem_init_inode(void *foo)
2375 struct shmem_inode_info *info = foo;
2376 inode_init_once(&info->vfs_inode);
2379 static int shmem_init_inodecache(void)
2381 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2382 sizeof(struct shmem_inode_info),
2383 0, SLAB_PANIC, shmem_init_inode);
2387 static void shmem_destroy_inodecache(void)
2389 kmem_cache_destroy(shmem_inode_cachep);
2392 static const struct address_space_operations shmem_aops = {
2393 .writepage = shmem_writepage,
2394 .set_page_dirty = __set_page_dirty_no_writeback,
2396 .write_begin = shmem_write_begin,
2397 .write_end = shmem_write_end,
2399 .migratepage = migrate_page,
2400 .error_remove_page = generic_error_remove_page,
2403 static const struct file_operations shmem_file_operations = {
2406 .llseek = generic_file_llseek,
2407 .read = do_sync_read,
2408 .write = do_sync_write,
2409 .aio_read = shmem_file_aio_read,
2410 .aio_write = generic_file_aio_write,
2411 .fsync = noop_fsync,
2412 .splice_read = shmem_file_splice_read,
2413 .splice_write = generic_file_splice_write,
2417 static const struct inode_operations shmem_inode_operations = {
2418 .setattr = shmem_setattr,
2419 .truncate_range = shmem_truncate_range,
2420 #ifdef CONFIG_TMPFS_XATTR
2421 .setxattr = shmem_setxattr,
2422 .getxattr = shmem_getxattr,
2423 .listxattr = shmem_listxattr,
2424 .removexattr = shmem_removexattr,
2428 static const struct inode_operations shmem_dir_inode_operations = {
2430 .create = shmem_create,
2431 .lookup = simple_lookup,
2433 .unlink = shmem_unlink,
2434 .symlink = shmem_symlink,
2435 .mkdir = shmem_mkdir,
2436 .rmdir = shmem_rmdir,
2437 .mknod = shmem_mknod,
2438 .rename = shmem_rename,
2440 #ifdef CONFIG_TMPFS_XATTR
2441 .setxattr = shmem_setxattr,
2442 .getxattr = shmem_getxattr,
2443 .listxattr = shmem_listxattr,
2444 .removexattr = shmem_removexattr,
2446 #ifdef CONFIG_TMPFS_POSIX_ACL
2447 .setattr = shmem_setattr,
2451 static const struct inode_operations shmem_special_inode_operations = {
2452 #ifdef CONFIG_TMPFS_XATTR
2453 .setxattr = shmem_setxattr,
2454 .getxattr = shmem_getxattr,
2455 .listxattr = shmem_listxattr,
2456 .removexattr = shmem_removexattr,
2458 #ifdef CONFIG_TMPFS_POSIX_ACL
2459 .setattr = shmem_setattr,
2463 static const struct super_operations shmem_ops = {
2464 .alloc_inode = shmem_alloc_inode,
2465 .destroy_inode = shmem_destroy_inode,
2467 .statfs = shmem_statfs,
2468 .remount_fs = shmem_remount_fs,
2469 .show_options = shmem_show_options,
2471 .evict_inode = shmem_evict_inode,
2472 .drop_inode = generic_delete_inode,
2473 .put_super = shmem_put_super,
2476 static const struct vm_operations_struct shmem_vm_ops = {
2477 .fault = shmem_fault,
2479 .set_policy = shmem_set_policy,
2480 .get_policy = shmem_get_policy,
2484 static struct dentry *shmem_mount(struct file_system_type *fs_type,
2485 int flags, const char *dev_name, void *data)
2487 return mount_nodev(fs_type, flags, data, shmem_fill_super);
2490 static struct file_system_type shmem_fs_type = {
2491 .owner = THIS_MODULE,
2493 .mount = shmem_mount,
2494 .kill_sb = kill_litter_super,
2497 int __init shmem_init(void)
2501 error = bdi_init(&shmem_backing_dev_info);
2505 error = shmem_init_inodecache();
2509 error = register_filesystem(&shmem_fs_type);
2511 printk(KERN_ERR "Could not register tmpfs\n");
2515 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2516 shmem_fs_type.name, NULL);
2517 if (IS_ERR(shm_mnt)) {
2518 error = PTR_ERR(shm_mnt);
2519 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2525 unregister_filesystem(&shmem_fs_type);
2527 shmem_destroy_inodecache();
2529 bdi_destroy(&shmem_backing_dev_info);
2531 shm_mnt = ERR_PTR(error);
2535 #else /* !CONFIG_SHMEM */
2538 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2540 * This is intended for small system where the benefits of the full
2541 * shmem code (swap-backed and resource-limited) are outweighed by
2542 * their complexity. On systems without swap this code should be
2543 * effectively equivalent, but much lighter weight.
2546 #include <linux/ramfs.h>
2548 static struct file_system_type shmem_fs_type = {
2550 .mount = ramfs_mount,
2551 .kill_sb = kill_litter_super,
2554 int __init shmem_init(void)
2556 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
2558 shm_mnt = kern_mount(&shmem_fs_type);
2559 BUG_ON(IS_ERR(shm_mnt));
2564 int shmem_unuse(swp_entry_t swap, struct page *page)
2569 int shmem_lock(struct file *file, int lock, struct user_struct *user)
2574 void shmem_unlock_mapping(struct address_space *mapping)
2578 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
2580 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
2582 EXPORT_SYMBOL_GPL(shmem_truncate_range);
2584 int vmtruncate_range(struct inode *inode, loff_t lstart, loff_t lend)
2586 /* Only CONFIG_SHMEM shmem.c ever supported i_op->truncate_range(). */
2590 #define shmem_vm_ops generic_file_vm_ops
2591 #define shmem_file_operations ramfs_file_operations
2592 #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
2593 #define shmem_acct_size(flags, size) 0
2594 #define shmem_unacct_size(flags, size) do {} while (0)
2596 #endif /* CONFIG_SHMEM */
2601 * shmem_file_setup - get an unlinked file living in tmpfs
2602 * @name: name for dentry (to be seen in /proc/<pid>/maps
2603 * @size: size to be set for the file
2604 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
2606 struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
2610 struct inode *inode;
2612 struct dentry *root;
2615 if (IS_ERR(shm_mnt))
2616 return (void *)shm_mnt;
2618 if (size < 0 || size > MAX_LFS_FILESIZE)
2619 return ERR_PTR(-EINVAL);
2621 if (shmem_acct_size(flags, size))
2622 return ERR_PTR(-ENOMEM);
2626 this.len = strlen(name);
2627 this.hash = 0; /* will go */
2628 root = shm_mnt->mnt_root;
2629 path.dentry = d_alloc(root, &this);
2632 path.mnt = mntget(shm_mnt);
2635 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
2639 d_instantiate(path.dentry, inode);
2640 inode->i_size = size;
2641 clear_nlink(inode); /* It is unlinked */
2643 error = ramfs_nommu_expand_for_mapping(inode, size);
2649 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
2650 &shmem_file_operations);
2659 shmem_unacct_size(flags, size);
2660 return ERR_PTR(error);
2662 EXPORT_SYMBOL_GPL(shmem_file_setup);
2665 * shmem_zero_setup - setup a shared anonymous mapping
2666 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2668 int shmem_zero_setup(struct vm_area_struct *vma)
2671 loff_t size = vma->vm_end - vma->vm_start;
2673 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2675 return PTR_ERR(file);
2679 vma->vm_file = file;
2680 vma->vm_ops = &shmem_vm_ops;
2681 vma->vm_flags |= VM_CAN_NONLINEAR;
2686 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2687 * @mapping: the page's address_space
2688 * @index: the page index
2689 * @gfp: the page allocator flags to use if allocating
2691 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2692 * with any new page allocations done using the specified allocation flags.
2693 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2694 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2695 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2697 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2698 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
2700 struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2701 pgoff_t index, gfp_t gfp)
2704 struct inode *inode = mapping->host;
2708 BUG_ON(mapping->a_ops != &shmem_aops);
2709 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2711 page = ERR_PTR(error);
2717 * The tiny !SHMEM case uses ramfs without swap
2719 return read_cache_page_gfp(mapping, index, gfp);
2722 EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);