xref: /openbmc/linux/mm/shmem.c (revision d189922862e03ce6c7adc1e99d3b94e632dc8e89)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Resizable virtual memory filesystem for Linux.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2000 Linus Torvalds.
51da177e4SLinus Torvalds  *		 2000 Transmeta Corp.
61da177e4SLinus Torvalds  *		 2000-2001 Christoph Rohland
71da177e4SLinus Torvalds  *		 2000-2001 SAP AG
81da177e4SLinus Torvalds  *		 2002 Red Hat Inc.
96922c0c7SHugh Dickins  * Copyright (C) 2002-2011 Hugh Dickins.
106922c0c7SHugh Dickins  * Copyright (C) 2011 Google Inc.
110edd73b3SHugh Dickins  * Copyright (C) 2002-2005 VERITAS Software Corporation.
121da177e4SLinus Torvalds  * Copyright (C) 2004 Andi Kleen, SuSE Labs
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * Extended attribute support for tmpfs:
151da177e4SLinus Torvalds  * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
161da177e4SLinus Torvalds  * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
171da177e4SLinus Torvalds  *
18853ac43aSMatt Mackall  * tiny-shmem:
19853ac43aSMatt Mackall  * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
20853ac43aSMatt Mackall  *
211da177e4SLinus Torvalds  * This file is released under the GPL.
221da177e4SLinus Torvalds  */
231da177e4SLinus Torvalds 
24853ac43aSMatt Mackall #include <linux/fs.h>
25853ac43aSMatt Mackall #include <linux/init.h>
26853ac43aSMatt Mackall #include <linux/vfs.h>
27853ac43aSMatt Mackall #include <linux/mount.h>
28caefba17SHugh Dickins #include <linux/pagemap.h>
29853ac43aSMatt Mackall #include <linux/file.h>
30853ac43aSMatt Mackall #include <linux/mm.h>
31b95f1b31SPaul Gortmaker #include <linux/export.h>
32853ac43aSMatt Mackall #include <linux/swap.h>
33853ac43aSMatt Mackall 
34853ac43aSMatt Mackall static struct vfsmount *shm_mnt;
35853ac43aSMatt Mackall 
36853ac43aSMatt Mackall #ifdef CONFIG_SHMEM
371da177e4SLinus Torvalds /*
381da177e4SLinus Torvalds  * This virtual memory filesystem is heavily based on the ramfs. It
391da177e4SLinus Torvalds  * extends ramfs by the ability to use swap and honor resource limits
401da177e4SLinus Torvalds  * which makes it a completely usable filesystem.
411da177e4SLinus Torvalds  */
421da177e4SLinus Torvalds 
4339f0247dSAndreas Gruenbacher #include <linux/xattr.h>
44a5694255SChristoph Hellwig #include <linux/exportfs.h>
451c7c474cSChristoph Hellwig #include <linux/posix_acl.h>
4639f0247dSAndreas Gruenbacher #include <linux/generic_acl.h>
471da177e4SLinus Torvalds #include <linux/mman.h>
481da177e4SLinus Torvalds #include <linux/string.h>
491da177e4SLinus Torvalds #include <linux/slab.h>
501da177e4SLinus Torvalds #include <linux/backing-dev.h>
511da177e4SLinus Torvalds #include <linux/shmem_fs.h>
521da177e4SLinus Torvalds #include <linux/writeback.h>
531da177e4SLinus Torvalds #include <linux/blkdev.h>
54bda97eabSHugh Dickins #include <linux/pagevec.h>
5541ffe5d5SHugh Dickins #include <linux/percpu_counter.h>
5683e4fa9cSHugh Dickins #include <linux/falloc.h>
57708e3508SHugh Dickins #include <linux/splice.h>
581da177e4SLinus Torvalds #include <linux/security.h>
591da177e4SLinus Torvalds #include <linux/swapops.h>
601da177e4SLinus Torvalds #include <linux/mempolicy.h>
611da177e4SLinus Torvalds #include <linux/namei.h>
62b00dc3adSHugh Dickins #include <linux/ctype.h>
63304dbdb7SLee Schermerhorn #include <linux/migrate.h>
64c1f60a5aSChristoph Lameter #include <linux/highmem.h>
65680d794bSakpm@linux-foundation.org #include <linux/seq_file.h>
6692562927SMimi Zohar #include <linux/magic.h>
67304dbdb7SLee Schermerhorn 
681da177e4SLinus Torvalds #include <asm/uaccess.h>
691da177e4SLinus Torvalds #include <asm/pgtable.h>
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds #define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
721da177e4SLinus Torvalds #define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */
751da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20
761da177e4SLinus Torvalds 
7769f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
7869f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128
7969f07ec9SHugh Dickins 
80b09e0fa4SEric Paris struct shmem_xattr {
81b09e0fa4SEric Paris 	struct list_head list;	/* anchored by shmem_inode_info->xattr_list */
82b09e0fa4SEric Paris 	char *name;		/* xattr name */
83b09e0fa4SEric Paris 	size_t size;
84b09e0fa4SEric Paris 	char value[0];
85b09e0fa4SEric Paris };
86b09e0fa4SEric Paris 
871aac1400SHugh Dickins /*
881aac1400SHugh Dickins  * shmem_fallocate and shmem_writepage communicate via inode->i_private
891aac1400SHugh Dickins  * (with i_mutex making sure that it has only one user at a time):
901aac1400SHugh Dickins  * we would prefer not to enlarge the shmem inode just for that.
911aac1400SHugh Dickins  */
921aac1400SHugh Dickins struct shmem_falloc {
931aac1400SHugh Dickins 	pgoff_t start;		/* start of range currently being fallocated */
941aac1400SHugh Dickins 	pgoff_t next;		/* the next page offset to be fallocated */
951aac1400SHugh Dickins 	pgoff_t nr_falloced;	/* how many new pages have been fallocated */
961aac1400SHugh Dickins 	pgoff_t nr_unswapped;	/* how often writepage refused to swap out */
971aac1400SHugh Dickins };
981aac1400SHugh Dickins 
99285b2c4fSHugh Dickins /* Flag allocation requirements to shmem_getpage */
1001da177e4SLinus Torvalds enum sgp_type {
1011da177e4SLinus Torvalds 	SGP_READ,	/* don't exceed i_size, don't allocate page */
1021da177e4SLinus Torvalds 	SGP_CACHE,	/* don't exceed i_size, may allocate page */
103a0ee5ec5SHugh Dickins 	SGP_DIRTY,	/* like SGP_CACHE, but set new page dirty */
1041635f6a7SHugh Dickins 	SGP_WRITE,	/* may exceed i_size, may allocate !Uptodate page */
1051635f6a7SHugh Dickins 	SGP_FALLOC,	/* like SGP_WRITE, but make existing page Uptodate */
1061da177e4SLinus Torvalds };
1071da177e4SLinus Torvalds 
108b76db735SAndrew Morton #ifdef CONFIG_TMPFS
109680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void)
110680d794bSakpm@linux-foundation.org {
111680d794bSakpm@linux-foundation.org 	return totalram_pages / 2;
112680d794bSakpm@linux-foundation.org }
113680d794bSakpm@linux-foundation.org 
114680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void)
115680d794bSakpm@linux-foundation.org {
116680d794bSakpm@linux-foundation.org 	return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
117680d794bSakpm@linux-foundation.org }
118b76db735SAndrew Morton #endif
119680d794bSakpm@linux-foundation.org 
120bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
121bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp,
122bde05d1cSHugh Dickins 				struct shmem_inode_info *info, pgoff_t index);
12368da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
12468da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
12568da9f05SHugh Dickins 
12668da9f05SHugh Dickins static inline int shmem_getpage(struct inode *inode, pgoff_t index,
12768da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, int *fault_type)
12868da9f05SHugh Dickins {
12968da9f05SHugh Dickins 	return shmem_getpage_gfp(inode, index, pagep, sgp,
13068da9f05SHugh Dickins 			mapping_gfp_mask(inode->i_mapping), fault_type);
13168da9f05SHugh Dickins }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	return sb->s_fs_info;
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /*
1391da177e4SLinus Torvalds  * shmem_file_setup pre-accounts the whole fixed size of a VM object,
1401da177e4SLinus Torvalds  * for shared memory and for shared anonymous (/dev/zero) mappings
1411da177e4SLinus Torvalds  * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
1421da177e4SLinus Torvalds  * consistent with the pre-accounting of private mappings ...
1431da177e4SLinus Torvalds  */
1441da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size)
1451da177e4SLinus Torvalds {
1460b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
147191c5424SAl Viro 		0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size)
1511da177e4SLinus Torvalds {
1520b0a0806SHugh Dickins 	if (!(flags & VM_NORESERVE))
1531da177e4SLinus Torvalds 		vm_unacct_memory(VM_ACCT(size));
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds /*
1571da177e4SLinus Torvalds  * ... whereas tmpfs objects are accounted incrementally as
1581da177e4SLinus Torvalds  * pages are allocated, in order to allow huge sparse files.
1591da177e4SLinus Torvalds  * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
1601da177e4SLinus Torvalds  * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
1611da177e4SLinus Torvalds  */
1621da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags)
1631da177e4SLinus Torvalds {
1640b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
165191c5424SAl Viro 		security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages)
1691da177e4SLinus Torvalds {
1700b0a0806SHugh Dickins 	if (flags & VM_NORESERVE)
1711da177e4SLinus Torvalds 		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
174759b9775SHugh Dickins static const struct super_operations shmem_ops;
175f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops;
17615ad7cdcSHelge Deller static const struct file_operations shmem_file_operations;
17792e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations;
17892e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations;
17992e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations;
180f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops;
1811da177e4SLinus Torvalds 
1826c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info  __read_mostly = {
1831da177e4SLinus Torvalds 	.ra_pages	= 0,	/* No readahead */
1844f98a2feSRik van Riel 	.capabilities	= BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
1851da177e4SLinus Torvalds };
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist);
188cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex);
1891da177e4SLinus Torvalds 
1905b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb)
1915b04c689SPavel Emelyanov {
1925b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1935b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
1945b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
1955b04c689SPavel Emelyanov 		if (!sbinfo->free_inodes) {
1965b04c689SPavel Emelyanov 			spin_unlock(&sbinfo->stat_lock);
1975b04c689SPavel Emelyanov 			return -ENOSPC;
1985b04c689SPavel Emelyanov 		}
1995b04c689SPavel Emelyanov 		sbinfo->free_inodes--;
2005b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2015b04c689SPavel Emelyanov 	}
2025b04c689SPavel Emelyanov 	return 0;
2035b04c689SPavel Emelyanov }
2045b04c689SPavel Emelyanov 
2055b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb)
2065b04c689SPavel Emelyanov {
2075b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2085b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2095b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2105b04c689SPavel Emelyanov 		sbinfo->free_inodes++;
2115b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2125b04c689SPavel Emelyanov 	}
2135b04c689SPavel Emelyanov }
2145b04c689SPavel Emelyanov 
21546711810SRandy Dunlap /**
21641ffe5d5SHugh Dickins  * shmem_recalc_inode - recalculate the block usage of an inode
2171da177e4SLinus Torvalds  * @inode: inode to recalc
2181da177e4SLinus Torvalds  *
2191da177e4SLinus Torvalds  * We have to calculate the free blocks since the mm can drop
2201da177e4SLinus Torvalds  * undirtied hole pages behind our back.
2211da177e4SLinus Torvalds  *
2221da177e4SLinus Torvalds  * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
2231da177e4SLinus Torvalds  * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
2241da177e4SLinus Torvalds  *
2251da177e4SLinus Torvalds  * It has to be called with the spinlock held.
2261da177e4SLinus Torvalds  */
2271da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode)
2281da177e4SLinus Torvalds {
2291da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
2301da177e4SLinus Torvalds 	long freed;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
2331da177e4SLinus Torvalds 	if (freed > 0) {
23454af6042SHugh Dickins 		struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
23554af6042SHugh Dickins 		if (sbinfo->max_blocks)
23654af6042SHugh Dickins 			percpu_counter_add(&sbinfo->used_blocks, -freed);
2371da177e4SLinus Torvalds 		info->alloced -= freed;
23854af6042SHugh Dickins 		inode->i_blocks -= freed * BLOCKS_PER_PAGE;
2391da177e4SLinus Torvalds 		shmem_unacct_blocks(info->flags, freed);
2401da177e4SLinus Torvalds 	}
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2437a5d0fbbSHugh Dickins /*
2447a5d0fbbSHugh Dickins  * Replace item expected in radix tree by a new item, while holding tree lock.
2457a5d0fbbSHugh Dickins  */
2467a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping,
2477a5d0fbbSHugh Dickins 			pgoff_t index, void *expected, void *replacement)
2487a5d0fbbSHugh Dickins {
2497a5d0fbbSHugh Dickins 	void **pslot;
2507a5d0fbbSHugh Dickins 	void *item = NULL;
2517a5d0fbbSHugh Dickins 
2527a5d0fbbSHugh Dickins 	VM_BUG_ON(!expected);
2537a5d0fbbSHugh Dickins 	pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
2547a5d0fbbSHugh Dickins 	if (pslot)
2557a5d0fbbSHugh Dickins 		item = radix_tree_deref_slot_protected(pslot,
2567a5d0fbbSHugh Dickins 							&mapping->tree_lock);
2577a5d0fbbSHugh Dickins 	if (item != expected)
2587a5d0fbbSHugh Dickins 		return -ENOENT;
2597a5d0fbbSHugh Dickins 	if (replacement)
2607a5d0fbbSHugh Dickins 		radix_tree_replace_slot(pslot, replacement);
2617a5d0fbbSHugh Dickins 	else
2627a5d0fbbSHugh Dickins 		radix_tree_delete(&mapping->page_tree, index);
2637a5d0fbbSHugh Dickins 	return 0;
2647a5d0fbbSHugh Dickins }
2657a5d0fbbSHugh Dickins 
2667a5d0fbbSHugh Dickins /*
267*d1899228SHugh Dickins  * Sometimes, before we decide whether to proceed or to fail, we must check
268*d1899228SHugh Dickins  * that an entry was not already brought back from swap by a racing thread.
269*d1899228SHugh Dickins  *
270*d1899228SHugh Dickins  * Checking page is not enough: by the time a SwapCache page is locked, it
271*d1899228SHugh Dickins  * might be reused, and again be SwapCache, using the same swap as before.
272*d1899228SHugh Dickins  */
273*d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping,
274*d1899228SHugh Dickins 			       pgoff_t index, swp_entry_t swap)
275*d1899228SHugh Dickins {
276*d1899228SHugh Dickins 	void *item;
277*d1899228SHugh Dickins 
278*d1899228SHugh Dickins 	rcu_read_lock();
279*d1899228SHugh Dickins 	item = radix_tree_lookup(&mapping->page_tree, index);
280*d1899228SHugh Dickins 	rcu_read_unlock();
281*d1899228SHugh Dickins 	return item == swp_to_radix_entry(swap);
282*d1899228SHugh Dickins }
283*d1899228SHugh Dickins 
284*d1899228SHugh Dickins /*
28546f65ec1SHugh Dickins  * Like add_to_page_cache_locked, but error if expected item has gone.
28646f65ec1SHugh Dickins  */
28746f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page,
28846f65ec1SHugh Dickins 				   struct address_space *mapping,
28946f65ec1SHugh Dickins 				   pgoff_t index, gfp_t gfp, void *expected)
29046f65ec1SHugh Dickins {
291aa3b1895SHugh Dickins 	int error = 0;
29246f65ec1SHugh Dickins 
29346f65ec1SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
29446f65ec1SHugh Dickins 	VM_BUG_ON(!PageSwapBacked(page));
29546f65ec1SHugh Dickins 
29646f65ec1SHugh Dickins 	if (!expected)
29746f65ec1SHugh Dickins 		error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
29846f65ec1SHugh Dickins 	if (!error) {
29946f65ec1SHugh Dickins 		page_cache_get(page);
30046f65ec1SHugh Dickins 		page->mapping = mapping;
30146f65ec1SHugh Dickins 		page->index = index;
30246f65ec1SHugh Dickins 
30346f65ec1SHugh Dickins 		spin_lock_irq(&mapping->tree_lock);
30446f65ec1SHugh Dickins 		if (!expected)
30546f65ec1SHugh Dickins 			error = radix_tree_insert(&mapping->page_tree,
30646f65ec1SHugh Dickins 							index, page);
30746f65ec1SHugh Dickins 		else
30846f65ec1SHugh Dickins 			error = shmem_radix_tree_replace(mapping, index,
30946f65ec1SHugh Dickins 							expected, page);
31046f65ec1SHugh Dickins 		if (!error) {
31146f65ec1SHugh Dickins 			mapping->nrpages++;
31246f65ec1SHugh Dickins 			__inc_zone_page_state(page, NR_FILE_PAGES);
31346f65ec1SHugh Dickins 			__inc_zone_page_state(page, NR_SHMEM);
31446f65ec1SHugh Dickins 			spin_unlock_irq(&mapping->tree_lock);
31546f65ec1SHugh Dickins 		} else {
31646f65ec1SHugh Dickins 			page->mapping = NULL;
31746f65ec1SHugh Dickins 			spin_unlock_irq(&mapping->tree_lock);
31846f65ec1SHugh Dickins 			page_cache_release(page);
31946f65ec1SHugh Dickins 		}
32046f65ec1SHugh Dickins 		if (!expected)
32146f65ec1SHugh Dickins 			radix_tree_preload_end();
32246f65ec1SHugh Dickins 	}
32346f65ec1SHugh Dickins 	if (error)
32446f65ec1SHugh Dickins 		mem_cgroup_uncharge_cache_page(page);
32546f65ec1SHugh Dickins 	return error;
32646f65ec1SHugh Dickins }
32746f65ec1SHugh Dickins 
32846f65ec1SHugh Dickins /*
3296922c0c7SHugh Dickins  * Like delete_from_page_cache, but substitutes swap for page.
3306922c0c7SHugh Dickins  */
3316922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap)
3326922c0c7SHugh Dickins {
3336922c0c7SHugh Dickins 	struct address_space *mapping = page->mapping;
3346922c0c7SHugh Dickins 	int error;
3356922c0c7SHugh Dickins 
3366922c0c7SHugh Dickins 	spin_lock_irq(&mapping->tree_lock);
3376922c0c7SHugh Dickins 	error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
3386922c0c7SHugh Dickins 	page->mapping = NULL;
3396922c0c7SHugh Dickins 	mapping->nrpages--;
3406922c0c7SHugh Dickins 	__dec_zone_page_state(page, NR_FILE_PAGES);
3416922c0c7SHugh Dickins 	__dec_zone_page_state(page, NR_SHMEM);
3426922c0c7SHugh Dickins 	spin_unlock_irq(&mapping->tree_lock);
3436922c0c7SHugh Dickins 	page_cache_release(page);
3446922c0c7SHugh Dickins 	BUG_ON(error);
3456922c0c7SHugh Dickins }
3466922c0c7SHugh Dickins 
3476922c0c7SHugh Dickins /*
3487a5d0fbbSHugh Dickins  * Like find_get_pages, but collecting swap entries as well as pages.
3497a5d0fbbSHugh Dickins  */
3507a5d0fbbSHugh Dickins static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
3517a5d0fbbSHugh Dickins 					pgoff_t start, unsigned int nr_pages,
3527a5d0fbbSHugh Dickins 					struct page **pages, pgoff_t *indices)
3537a5d0fbbSHugh Dickins {
3547a5d0fbbSHugh Dickins 	unsigned int i;
3557a5d0fbbSHugh Dickins 	unsigned int ret;
3567a5d0fbbSHugh Dickins 	unsigned int nr_found;
3577a5d0fbbSHugh Dickins 
3587a5d0fbbSHugh Dickins 	rcu_read_lock();
3597a5d0fbbSHugh Dickins restart:
3607a5d0fbbSHugh Dickins 	nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
3617a5d0fbbSHugh Dickins 				(void ***)pages, indices, start, nr_pages);
3627a5d0fbbSHugh Dickins 	ret = 0;
3637a5d0fbbSHugh Dickins 	for (i = 0; i < nr_found; i++) {
3647a5d0fbbSHugh Dickins 		struct page *page;
3657a5d0fbbSHugh Dickins repeat:
3667a5d0fbbSHugh Dickins 		page = radix_tree_deref_slot((void **)pages[i]);
3677a5d0fbbSHugh Dickins 		if (unlikely(!page))
3687a5d0fbbSHugh Dickins 			continue;
3697a5d0fbbSHugh Dickins 		if (radix_tree_exception(page)) {
3708079b1c8SHugh Dickins 			if (radix_tree_deref_retry(page))
3717a5d0fbbSHugh Dickins 				goto restart;
3728079b1c8SHugh Dickins 			/*
3738079b1c8SHugh Dickins 			 * Otherwise, we must be storing a swap entry
3748079b1c8SHugh Dickins 			 * here as an exceptional entry: so return it
3758079b1c8SHugh Dickins 			 * without attempting to raise page count.
3768079b1c8SHugh Dickins 			 */
3778079b1c8SHugh Dickins 			goto export;
3787a5d0fbbSHugh Dickins 		}
3797a5d0fbbSHugh Dickins 		if (!page_cache_get_speculative(page))
3807a5d0fbbSHugh Dickins 			goto repeat;
3817a5d0fbbSHugh Dickins 
3827a5d0fbbSHugh Dickins 		/* Has the page moved? */
3837a5d0fbbSHugh Dickins 		if (unlikely(page != *((void **)pages[i]))) {
3847a5d0fbbSHugh Dickins 			page_cache_release(page);
3857a5d0fbbSHugh Dickins 			goto repeat;
3867a5d0fbbSHugh Dickins 		}
3877a5d0fbbSHugh Dickins export:
3887a5d0fbbSHugh Dickins 		indices[ret] = indices[i];
3897a5d0fbbSHugh Dickins 		pages[ret] = page;
3907a5d0fbbSHugh Dickins 		ret++;
3917a5d0fbbSHugh Dickins 	}
3927a5d0fbbSHugh Dickins 	if (unlikely(!ret && nr_found))
3937a5d0fbbSHugh Dickins 		goto restart;
3947a5d0fbbSHugh Dickins 	rcu_read_unlock();
3957a5d0fbbSHugh Dickins 	return ret;
3967a5d0fbbSHugh Dickins }
3977a5d0fbbSHugh Dickins 
3987a5d0fbbSHugh Dickins /*
3997a5d0fbbSHugh Dickins  * Remove swap entry from radix tree, free the swap and its page cache.
4007a5d0fbbSHugh Dickins  */
4017a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping,
4027a5d0fbbSHugh Dickins 			   pgoff_t index, void *radswap)
4037a5d0fbbSHugh Dickins {
4047a5d0fbbSHugh Dickins 	int error;
4057a5d0fbbSHugh Dickins 
4067a5d0fbbSHugh Dickins 	spin_lock_irq(&mapping->tree_lock);
4077a5d0fbbSHugh Dickins 	error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
4087a5d0fbbSHugh Dickins 	spin_unlock_irq(&mapping->tree_lock);
4097a5d0fbbSHugh Dickins 	if (!error)
4107a5d0fbbSHugh Dickins 		free_swap_and_cache(radix_to_swp_entry(radswap));
4117a5d0fbbSHugh Dickins 	return error;
4127a5d0fbbSHugh Dickins }
4137a5d0fbbSHugh Dickins 
4147a5d0fbbSHugh Dickins /*
4157a5d0fbbSHugh Dickins  * Pagevec may contain swap entries, so shuffle up pages before releasing.
4167a5d0fbbSHugh Dickins  */
41724513264SHugh Dickins static void shmem_deswap_pagevec(struct pagevec *pvec)
4187a5d0fbbSHugh Dickins {
4197a5d0fbbSHugh Dickins 	int i, j;
4207a5d0fbbSHugh Dickins 
4217a5d0fbbSHugh Dickins 	for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
4227a5d0fbbSHugh Dickins 		struct page *page = pvec->pages[i];
4237a5d0fbbSHugh Dickins 		if (!radix_tree_exceptional_entry(page))
4247a5d0fbbSHugh Dickins 			pvec->pages[j++] = page;
4257a5d0fbbSHugh Dickins 	}
4267a5d0fbbSHugh Dickins 	pvec->nr = j;
42724513264SHugh Dickins }
42824513264SHugh Dickins 
42924513264SHugh Dickins /*
43024513264SHugh Dickins  * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
43124513264SHugh Dickins  */
43224513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping)
43324513264SHugh Dickins {
43424513264SHugh Dickins 	struct pagevec pvec;
43524513264SHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
43624513264SHugh Dickins 	pgoff_t index = 0;
43724513264SHugh Dickins 
43824513264SHugh Dickins 	pagevec_init(&pvec, 0);
43924513264SHugh Dickins 	/*
44024513264SHugh Dickins 	 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
44124513264SHugh Dickins 	 */
44224513264SHugh Dickins 	while (!mapping_unevictable(mapping)) {
44324513264SHugh Dickins 		/*
44424513264SHugh Dickins 		 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
44524513264SHugh Dickins 		 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
44624513264SHugh Dickins 		 */
44724513264SHugh Dickins 		pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
44824513264SHugh Dickins 					PAGEVEC_SIZE, pvec.pages, indices);
44924513264SHugh Dickins 		if (!pvec.nr)
45024513264SHugh Dickins 			break;
45124513264SHugh Dickins 		index = indices[pvec.nr - 1] + 1;
45224513264SHugh Dickins 		shmem_deswap_pagevec(&pvec);
45324513264SHugh Dickins 		check_move_unevictable_pages(pvec.pages, pvec.nr);
45424513264SHugh Dickins 		pagevec_release(&pvec);
45524513264SHugh Dickins 		cond_resched();
45624513264SHugh Dickins 	}
4577a5d0fbbSHugh Dickins }
4587a5d0fbbSHugh Dickins 
4597a5d0fbbSHugh Dickins /*
4607a5d0fbbSHugh Dickins  * Remove range of pages and swap entries from radix tree, and free them.
4611635f6a7SHugh Dickins  * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
4627a5d0fbbSHugh Dickins  */
4631635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
4641635f6a7SHugh Dickins 								 bool unfalloc)
4651da177e4SLinus Torvalds {
466285b2c4fSHugh Dickins 	struct address_space *mapping = inode->i_mapping;
4671da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
468285b2c4fSHugh Dickins 	pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
46983e4fa9cSHugh Dickins 	pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
47083e4fa9cSHugh Dickins 	unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
47183e4fa9cSHugh Dickins 	unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
472bda97eabSHugh Dickins 	struct pagevec pvec;
4737a5d0fbbSHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
4747a5d0fbbSHugh Dickins 	long nr_swaps_freed = 0;
475285b2c4fSHugh Dickins 	pgoff_t index;
476bda97eabSHugh Dickins 	int i;
4771da177e4SLinus Torvalds 
47883e4fa9cSHugh Dickins 	if (lend == -1)
47983e4fa9cSHugh Dickins 		end = -1;	/* unsigned, so actually very big */
480bda97eabSHugh Dickins 
481bda97eabSHugh Dickins 	pagevec_init(&pvec, 0);
482bda97eabSHugh Dickins 	index = start;
48383e4fa9cSHugh Dickins 	while (index < end) {
4847a5d0fbbSHugh Dickins 		pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
48583e4fa9cSHugh Dickins 				min(end - index, (pgoff_t)PAGEVEC_SIZE),
4867a5d0fbbSHugh Dickins 							pvec.pages, indices);
4877a5d0fbbSHugh Dickins 		if (!pvec.nr)
4887a5d0fbbSHugh Dickins 			break;
489bda97eabSHugh Dickins 		mem_cgroup_uncharge_start();
490bda97eabSHugh Dickins 		for (i = 0; i < pagevec_count(&pvec); i++) {
491bda97eabSHugh Dickins 			struct page *page = pvec.pages[i];
492bda97eabSHugh Dickins 
4937a5d0fbbSHugh Dickins 			index = indices[i];
49483e4fa9cSHugh Dickins 			if (index >= end)
495bda97eabSHugh Dickins 				break;
496bda97eabSHugh Dickins 
4977a5d0fbbSHugh Dickins 			if (radix_tree_exceptional_entry(page)) {
4981635f6a7SHugh Dickins 				if (unfalloc)
4991635f6a7SHugh Dickins 					continue;
5007a5d0fbbSHugh Dickins 				nr_swaps_freed += !shmem_free_swap(mapping,
5017a5d0fbbSHugh Dickins 								index, page);
5027a5d0fbbSHugh Dickins 				continue;
5037a5d0fbbSHugh Dickins 			}
5047a5d0fbbSHugh Dickins 
505bda97eabSHugh Dickins 			if (!trylock_page(page))
506bda97eabSHugh Dickins 				continue;
5071635f6a7SHugh Dickins 			if (!unfalloc || !PageUptodate(page)) {
5087a5d0fbbSHugh Dickins 				if (page->mapping == mapping) {
5097a5d0fbbSHugh Dickins 					VM_BUG_ON(PageWriteback(page));
510bda97eabSHugh Dickins 					truncate_inode_page(mapping, page);
5117a5d0fbbSHugh Dickins 				}
5121635f6a7SHugh Dickins 			}
513bda97eabSHugh Dickins 			unlock_page(page);
514bda97eabSHugh Dickins 		}
51524513264SHugh Dickins 		shmem_deswap_pagevec(&pvec);
51624513264SHugh Dickins 		pagevec_release(&pvec);
517bda97eabSHugh Dickins 		mem_cgroup_uncharge_end();
518bda97eabSHugh Dickins 		cond_resched();
519bda97eabSHugh Dickins 		index++;
520bda97eabSHugh Dickins 	}
521bda97eabSHugh Dickins 
52283e4fa9cSHugh Dickins 	if (partial_start) {
523bda97eabSHugh Dickins 		struct page *page = NULL;
524bda97eabSHugh Dickins 		shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
525bda97eabSHugh Dickins 		if (page) {
52683e4fa9cSHugh Dickins 			unsigned int top = PAGE_CACHE_SIZE;
52783e4fa9cSHugh Dickins 			if (start > end) {
52883e4fa9cSHugh Dickins 				top = partial_end;
52983e4fa9cSHugh Dickins 				partial_end = 0;
53083e4fa9cSHugh Dickins 			}
53183e4fa9cSHugh Dickins 			zero_user_segment(page, partial_start, top);
532bda97eabSHugh Dickins 			set_page_dirty(page);
533bda97eabSHugh Dickins 			unlock_page(page);
534bda97eabSHugh Dickins 			page_cache_release(page);
535bda97eabSHugh Dickins 		}
536bda97eabSHugh Dickins 	}
53783e4fa9cSHugh Dickins 	if (partial_end) {
53883e4fa9cSHugh Dickins 		struct page *page = NULL;
53983e4fa9cSHugh Dickins 		shmem_getpage(inode, end, &page, SGP_READ, NULL);
54083e4fa9cSHugh Dickins 		if (page) {
54183e4fa9cSHugh Dickins 			zero_user_segment(page, 0, partial_end);
54283e4fa9cSHugh Dickins 			set_page_dirty(page);
54383e4fa9cSHugh Dickins 			unlock_page(page);
54483e4fa9cSHugh Dickins 			page_cache_release(page);
54583e4fa9cSHugh Dickins 		}
54683e4fa9cSHugh Dickins 	}
54783e4fa9cSHugh Dickins 	if (start >= end)
54883e4fa9cSHugh Dickins 		return;
549bda97eabSHugh Dickins 
550bda97eabSHugh Dickins 	index = start;
551bda97eabSHugh Dickins 	for ( ; ; ) {
552bda97eabSHugh Dickins 		cond_resched();
5537a5d0fbbSHugh Dickins 		pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
55483e4fa9cSHugh Dickins 				min(end - index, (pgoff_t)PAGEVEC_SIZE),
5557a5d0fbbSHugh Dickins 							pvec.pages, indices);
5567a5d0fbbSHugh Dickins 		if (!pvec.nr) {
5571635f6a7SHugh Dickins 			if (index == start || unfalloc)
558bda97eabSHugh Dickins 				break;
559bda97eabSHugh Dickins 			index = start;
560bda97eabSHugh Dickins 			continue;
561bda97eabSHugh Dickins 		}
5621635f6a7SHugh Dickins 		if ((index == start || unfalloc) && indices[0] >= end) {
56324513264SHugh Dickins 			shmem_deswap_pagevec(&pvec);
56424513264SHugh Dickins 			pagevec_release(&pvec);
565bda97eabSHugh Dickins 			break;
566bda97eabSHugh Dickins 		}
567bda97eabSHugh Dickins 		mem_cgroup_uncharge_start();
568bda97eabSHugh Dickins 		for (i = 0; i < pagevec_count(&pvec); i++) {
569bda97eabSHugh Dickins 			struct page *page = pvec.pages[i];
570bda97eabSHugh Dickins 
5717a5d0fbbSHugh Dickins 			index = indices[i];
57283e4fa9cSHugh Dickins 			if (index >= end)
573bda97eabSHugh Dickins 				break;
574bda97eabSHugh Dickins 
5757a5d0fbbSHugh Dickins 			if (radix_tree_exceptional_entry(page)) {
5761635f6a7SHugh Dickins 				if (unfalloc)
5771635f6a7SHugh Dickins 					continue;
5787a5d0fbbSHugh Dickins 				nr_swaps_freed += !shmem_free_swap(mapping,
5797a5d0fbbSHugh Dickins 								index, page);
5807a5d0fbbSHugh Dickins 				continue;
5817a5d0fbbSHugh Dickins 			}
5827a5d0fbbSHugh Dickins 
583bda97eabSHugh Dickins 			lock_page(page);
5841635f6a7SHugh Dickins 			if (!unfalloc || !PageUptodate(page)) {
5857a5d0fbbSHugh Dickins 				if (page->mapping == mapping) {
5867a5d0fbbSHugh Dickins 					VM_BUG_ON(PageWriteback(page));
587bda97eabSHugh Dickins 					truncate_inode_page(mapping, page);
5887a5d0fbbSHugh Dickins 				}
5891635f6a7SHugh Dickins 			}
590bda97eabSHugh Dickins 			unlock_page(page);
591bda97eabSHugh Dickins 		}
59224513264SHugh Dickins 		shmem_deswap_pagevec(&pvec);
59324513264SHugh Dickins 		pagevec_release(&pvec);
594bda97eabSHugh Dickins 		mem_cgroup_uncharge_end();
595bda97eabSHugh Dickins 		index++;
596bda97eabSHugh Dickins 	}
59794c1e62dSHugh Dickins 
5981da177e4SLinus Torvalds 	spin_lock(&info->lock);
5997a5d0fbbSHugh Dickins 	info->swapped -= nr_swaps_freed;
6001da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
6011da177e4SLinus Torvalds 	spin_unlock(&info->lock);
6021635f6a7SHugh Dickins }
6031da177e4SLinus Torvalds 
6041635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
6051635f6a7SHugh Dickins {
6061635f6a7SHugh Dickins 	shmem_undo_range(inode, lstart, lend, false);
607285b2c4fSHugh Dickins 	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
6081da177e4SLinus Torvalds }
60994c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
6101da177e4SLinus Torvalds 
61194c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
6141da177e4SLinus Torvalds 	int error;
6151da177e4SLinus Torvalds 
616db78b877SChristoph Hellwig 	error = inode_change_ok(inode, attr);
617db78b877SChristoph Hellwig 	if (error)
618db78b877SChristoph Hellwig 		return error;
619db78b877SChristoph Hellwig 
62094c1e62dSHugh Dickins 	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
62194c1e62dSHugh Dickins 		loff_t oldsize = inode->i_size;
62294c1e62dSHugh Dickins 		loff_t newsize = attr->ia_size;
6233889e6e7Snpiggin@suse.de 
62494c1e62dSHugh Dickins 		if (newsize != oldsize) {
62594c1e62dSHugh Dickins 			i_size_write(inode, newsize);
62694c1e62dSHugh Dickins 			inode->i_ctime = inode->i_mtime = CURRENT_TIME;
62794c1e62dSHugh Dickins 		}
62894c1e62dSHugh Dickins 		if (newsize < oldsize) {
62994c1e62dSHugh Dickins 			loff_t holebegin = round_up(newsize, PAGE_SIZE);
63094c1e62dSHugh Dickins 			unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
63194c1e62dSHugh Dickins 			shmem_truncate_range(inode, newsize, (loff_t)-1);
63294c1e62dSHugh Dickins 			/* unmap again to remove racily COWed private pages */
63394c1e62dSHugh Dickins 			unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
63494c1e62dSHugh Dickins 		}
6351da177e4SLinus Torvalds 	}
6361da177e4SLinus Torvalds 
6376a1a90adSChristoph Hellwig 	setattr_copy(inode, attr);
63839f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
639db78b877SChristoph Hellwig 	if (attr->ia_valid & ATTR_MODE)
6401c7c474cSChristoph Hellwig 		error = generic_acl_chmod(inode);
64139f0247dSAndreas Gruenbacher #endif
6421da177e4SLinus Torvalds 	return error;
6431da177e4SLinus Torvalds }
6441da177e4SLinus Torvalds 
6451f895f75SAl Viro static void shmem_evict_inode(struct inode *inode)
6461da177e4SLinus Torvalds {
6471da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
648b09e0fa4SEric Paris 	struct shmem_xattr *xattr, *nxattr;
6491da177e4SLinus Torvalds 
6503889e6e7Snpiggin@suse.de 	if (inode->i_mapping->a_ops == &shmem_aops) {
6511da177e4SLinus Torvalds 		shmem_unacct_size(info->flags, inode->i_size);
6521da177e4SLinus Torvalds 		inode->i_size = 0;
6533889e6e7Snpiggin@suse.de 		shmem_truncate_range(inode, 0, (loff_t)-1);
6541da177e4SLinus Torvalds 		if (!list_empty(&info->swaplist)) {
655cb5f7b9aSHugh Dickins 			mutex_lock(&shmem_swaplist_mutex);
6561da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
657cb5f7b9aSHugh Dickins 			mutex_unlock(&shmem_swaplist_mutex);
6581da177e4SLinus Torvalds 		}
65969f07ec9SHugh Dickins 	} else
66069f07ec9SHugh Dickins 		kfree(info->symlink);
661b09e0fa4SEric Paris 
662b09e0fa4SEric Paris 	list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
663b09e0fa4SEric Paris 		kfree(xattr->name);
664b09e0fa4SEric Paris 		kfree(xattr);
665b09e0fa4SEric Paris 	}
6661da177e4SLinus Torvalds 	BUG_ON(inode->i_blocks);
6675b04c689SPavel Emelyanov 	shmem_free_inode(inode->i_sb);
668dbd5768fSJan Kara 	clear_inode(inode);
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
67146f65ec1SHugh Dickins /*
67246f65ec1SHugh Dickins  * If swap found in inode, free it and move page from swapcache to filecache.
67346f65ec1SHugh Dickins  */
67441ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info,
675bde05d1cSHugh Dickins 			     swp_entry_t swap, struct page **pagep)
6761da177e4SLinus Torvalds {
677285b2c4fSHugh Dickins 	struct address_space *mapping = info->vfs_inode.i_mapping;
67846f65ec1SHugh Dickins 	void *radswap;
67941ffe5d5SHugh Dickins 	pgoff_t index;
680bde05d1cSHugh Dickins 	gfp_t gfp;
681bde05d1cSHugh Dickins 	int error = 0;
6821da177e4SLinus Torvalds 
68346f65ec1SHugh Dickins 	radswap = swp_to_radix_entry(swap);
684e504f3fdSHugh Dickins 	index = radix_tree_locate_item(&mapping->page_tree, radswap);
68546f65ec1SHugh Dickins 	if (index == -1)
6861da177e4SLinus Torvalds 		return 0;
6872e0e26c7SHugh Dickins 
6881b1b32f2SHugh Dickins 	/*
6891b1b32f2SHugh Dickins 	 * Move _head_ to start search for next from here.
6901f895f75SAl Viro 	 * But be careful: shmem_evict_inode checks list_empty without taking
6911b1b32f2SHugh Dickins 	 * mutex, and there's an instant in list_move_tail when info->swaplist
692285b2c4fSHugh Dickins 	 * would appear empty, if it were the only one on shmem_swaplist.
6931b1b32f2SHugh Dickins 	 */
6941b1b32f2SHugh Dickins 	if (shmem_swaplist.next != &info->swaplist)
6952e0e26c7SHugh Dickins 		list_move_tail(&shmem_swaplist, &info->swaplist);
6962e0e26c7SHugh Dickins 
697bde05d1cSHugh Dickins 	gfp = mapping_gfp_mask(mapping);
698bde05d1cSHugh Dickins 	if (shmem_should_replace_page(*pagep, gfp)) {
699bde05d1cSHugh Dickins 		mutex_unlock(&shmem_swaplist_mutex);
700bde05d1cSHugh Dickins 		error = shmem_replace_page(pagep, gfp, info, index);
701bde05d1cSHugh Dickins 		mutex_lock(&shmem_swaplist_mutex);
702bde05d1cSHugh Dickins 		/*
703bde05d1cSHugh Dickins 		 * We needed to drop mutex to make that restrictive page
7040142ef6cSHugh Dickins 		 * allocation, but the inode might have been freed while we
7050142ef6cSHugh Dickins 		 * dropped it: although a racing shmem_evict_inode() cannot
7060142ef6cSHugh Dickins 		 * complete without emptying the radix_tree, our page lock
7070142ef6cSHugh Dickins 		 * on this swapcache page is not enough to prevent that -
7080142ef6cSHugh Dickins 		 * free_swap_and_cache() of our swap entry will only
7090142ef6cSHugh Dickins 		 * trylock_page(), removing swap from radix_tree whatever.
7100142ef6cSHugh Dickins 		 *
7110142ef6cSHugh Dickins 		 * We must not proceed to shmem_add_to_page_cache() if the
7120142ef6cSHugh Dickins 		 * inode has been freed, but of course we cannot rely on
7130142ef6cSHugh Dickins 		 * inode or mapping or info to check that.  However, we can
7140142ef6cSHugh Dickins 		 * safely check if our swap entry is still in use (and here
7150142ef6cSHugh Dickins 		 * it can't have got reused for another page): if it's still
7160142ef6cSHugh Dickins 		 * in use, then the inode cannot have been freed yet, and we
7170142ef6cSHugh Dickins 		 * can safely proceed (if it's no longer in use, that tells
7180142ef6cSHugh Dickins 		 * nothing about the inode, but we don't need to unuse swap).
719bde05d1cSHugh Dickins 		 */
720bde05d1cSHugh Dickins 		if (!page_swapcount(*pagep))
721bde05d1cSHugh Dickins 			error = -ENOENT;
722bde05d1cSHugh Dickins 	}
723bde05d1cSHugh Dickins 
724d13d1443SKAMEZAWA Hiroyuki 	/*
725778dd893SHugh Dickins 	 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
726778dd893SHugh Dickins 	 * but also to hold up shmem_evict_inode(): so inode cannot be freed
727778dd893SHugh Dickins 	 * beneath us (pagelock doesn't help until the page is in pagecache).
728d13d1443SKAMEZAWA Hiroyuki 	 */
729bde05d1cSHugh Dickins 	if (!error)
730bde05d1cSHugh Dickins 		error = shmem_add_to_page_cache(*pagep, mapping, index,
73146f65ec1SHugh Dickins 						GFP_NOWAIT, radswap);
73248f170fbSHugh Dickins 	if (error != -ENOMEM) {
73346f65ec1SHugh Dickins 		/*
73446f65ec1SHugh Dickins 		 * Truncation and eviction use free_swap_and_cache(), which
73546f65ec1SHugh Dickins 		 * only does trylock page: if we raced, best clean up here.
73646f65ec1SHugh Dickins 		 */
737bde05d1cSHugh Dickins 		delete_from_swap_cache(*pagep);
738bde05d1cSHugh Dickins 		set_page_dirty(*pagep);
73946f65ec1SHugh Dickins 		if (!error) {
74046f65ec1SHugh Dickins 			spin_lock(&info->lock);
741285b2c4fSHugh Dickins 			info->swapped--;
74246f65ec1SHugh Dickins 			spin_unlock(&info->lock);
74341ffe5d5SHugh Dickins 			swap_free(swap);
74446f65ec1SHugh Dickins 		}
7452e0e26c7SHugh Dickins 		error = 1;	/* not an error, but entry was found */
7461da177e4SLinus Torvalds 	}
7472e0e26c7SHugh Dickins 	return error;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds /*
75146f65ec1SHugh Dickins  * Search through swapped inodes to find and replace swap by page.
7521da177e4SLinus Torvalds  */
75341ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page)
7541da177e4SLinus Torvalds {
75541ffe5d5SHugh Dickins 	struct list_head *this, *next;
7561da177e4SLinus Torvalds 	struct shmem_inode_info *info;
7571da177e4SLinus Torvalds 	int found = 0;
758bde05d1cSHugh Dickins 	int error = 0;
759bde05d1cSHugh Dickins 
760bde05d1cSHugh Dickins 	/*
761bde05d1cSHugh Dickins 	 * There's a faint possibility that swap page was replaced before
7620142ef6cSHugh Dickins 	 * caller locked it: caller will come back later with the right page.
763bde05d1cSHugh Dickins 	 */
7640142ef6cSHugh Dickins 	if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
765bde05d1cSHugh Dickins 		goto out;
766778dd893SHugh Dickins 
767778dd893SHugh Dickins 	/*
768778dd893SHugh Dickins 	 * Charge page using GFP_KERNEL while we can wait, before taking
769778dd893SHugh Dickins 	 * the shmem_swaplist_mutex which might hold up shmem_writepage().
770778dd893SHugh Dickins 	 * Charged back to the user (not to caller) when swap account is used.
771778dd893SHugh Dickins 	 */
772778dd893SHugh Dickins 	error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
773778dd893SHugh Dickins 	if (error)
774778dd893SHugh Dickins 		goto out;
77546f65ec1SHugh Dickins 	/* No radix_tree_preload: swap entry keeps a place for page in tree */
7761da177e4SLinus Torvalds 
777cb5f7b9aSHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
77841ffe5d5SHugh Dickins 	list_for_each_safe(this, next, &shmem_swaplist) {
77941ffe5d5SHugh Dickins 		info = list_entry(this, struct shmem_inode_info, swaplist);
780285b2c4fSHugh Dickins 		if (info->swapped)
781bde05d1cSHugh Dickins 			found = shmem_unuse_inode(info, swap, &page);
7826922c0c7SHugh Dickins 		else
7836922c0c7SHugh Dickins 			list_del_init(&info->swaplist);
784cb5f7b9aSHugh Dickins 		cond_resched();
7852e0e26c7SHugh Dickins 		if (found)
786778dd893SHugh Dickins 			break;
7871da177e4SLinus Torvalds 	}
788cb5f7b9aSHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
789778dd893SHugh Dickins 
790778dd893SHugh Dickins 	if (found < 0)
791778dd893SHugh Dickins 		error = found;
792778dd893SHugh Dickins out:
793aaa46865SHugh Dickins 	unlock_page(page);
794aaa46865SHugh Dickins 	page_cache_release(page);
795778dd893SHugh Dickins 	return error;
7961da177e4SLinus Torvalds }
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds /*
7991da177e4SLinus Torvalds  * Move the page from the page cache to the swap cache.
8001da177e4SLinus Torvalds  */
8011da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc)
8021da177e4SLinus Torvalds {
8031da177e4SLinus Torvalds 	struct shmem_inode_info *info;
8041da177e4SLinus Torvalds 	struct address_space *mapping;
8051da177e4SLinus Torvalds 	struct inode *inode;
8066922c0c7SHugh Dickins 	swp_entry_t swap;
8076922c0c7SHugh Dickins 	pgoff_t index;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
8101da177e4SLinus Torvalds 	mapping = page->mapping;
8111da177e4SLinus Torvalds 	index = page->index;
8121da177e4SLinus Torvalds 	inode = mapping->host;
8131da177e4SLinus Torvalds 	info = SHMEM_I(inode);
8141da177e4SLinus Torvalds 	if (info->flags & VM_LOCKED)
8151da177e4SLinus Torvalds 		goto redirty;
816d9fe526aSHugh Dickins 	if (!total_swap_pages)
8171da177e4SLinus Torvalds 		goto redirty;
8181da177e4SLinus Torvalds 
819d9fe526aSHugh Dickins 	/*
820d9fe526aSHugh Dickins 	 * shmem_backing_dev_info's capabilities prevent regular writeback or
821d9fe526aSHugh Dickins 	 * sync from ever calling shmem_writepage; but a stacking filesystem
82248f170fbSHugh Dickins 	 * might use ->writepage of its underlying filesystem, in which case
823d9fe526aSHugh Dickins 	 * tmpfs should write out to swap only in response to memory pressure,
82448f170fbSHugh Dickins 	 * and not for the writeback threads or sync.
825d9fe526aSHugh Dickins 	 */
82648f170fbSHugh Dickins 	if (!wbc->for_reclaim) {
82748f170fbSHugh Dickins 		WARN_ON_ONCE(1);	/* Still happens? Tell us about it! */
82848f170fbSHugh Dickins 		goto redirty;
82948f170fbSHugh Dickins 	}
8301635f6a7SHugh Dickins 
8311635f6a7SHugh Dickins 	/*
8321635f6a7SHugh Dickins 	 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
8331635f6a7SHugh Dickins 	 * value into swapfile.c, the only way we can correctly account for a
8341635f6a7SHugh Dickins 	 * fallocated page arriving here is now to initialize it and write it.
8351aac1400SHugh Dickins 	 *
8361aac1400SHugh Dickins 	 * That's okay for a page already fallocated earlier, but if we have
8371aac1400SHugh Dickins 	 * not yet completed the fallocation, then (a) we want to keep track
8381aac1400SHugh Dickins 	 * of this page in case we have to undo it, and (b) it may not be a
8391aac1400SHugh Dickins 	 * good idea to continue anyway, once we're pushing into swap.  So
8401aac1400SHugh Dickins 	 * reactivate the page, and let shmem_fallocate() quit when too many.
8411635f6a7SHugh Dickins 	 */
8421635f6a7SHugh Dickins 	if (!PageUptodate(page)) {
8431aac1400SHugh Dickins 		if (inode->i_private) {
8441aac1400SHugh Dickins 			struct shmem_falloc *shmem_falloc;
8451aac1400SHugh Dickins 			spin_lock(&inode->i_lock);
8461aac1400SHugh Dickins 			shmem_falloc = inode->i_private;
8471aac1400SHugh Dickins 			if (shmem_falloc &&
8481aac1400SHugh Dickins 			    index >= shmem_falloc->start &&
8491aac1400SHugh Dickins 			    index < shmem_falloc->next)
8501aac1400SHugh Dickins 				shmem_falloc->nr_unswapped++;
8511aac1400SHugh Dickins 			else
8521aac1400SHugh Dickins 				shmem_falloc = NULL;
8531aac1400SHugh Dickins 			spin_unlock(&inode->i_lock);
8541aac1400SHugh Dickins 			if (shmem_falloc)
8551aac1400SHugh Dickins 				goto redirty;
8561aac1400SHugh Dickins 		}
8571635f6a7SHugh Dickins 		clear_highpage(page);
8581635f6a7SHugh Dickins 		flush_dcache_page(page);
8591635f6a7SHugh Dickins 		SetPageUptodate(page);
8601635f6a7SHugh Dickins 	}
8611635f6a7SHugh Dickins 
862d9fe526aSHugh Dickins 	swap = get_swap_page();
86348f170fbSHugh Dickins 	if (!swap.val)
86448f170fbSHugh Dickins 		goto redirty;
865d9fe526aSHugh Dickins 
866b1dea800SHugh Dickins 	/*
867b1dea800SHugh Dickins 	 * Add inode to shmem_unuse()'s list of swapped-out inodes,
8686922c0c7SHugh Dickins 	 * if it's not already there.  Do it now before the page is
8696922c0c7SHugh Dickins 	 * moved to swap cache, when its pagelock no longer protects
870b1dea800SHugh Dickins 	 * the inode from eviction.  But don't unlock the mutex until
8716922c0c7SHugh Dickins 	 * we've incremented swapped, because shmem_unuse_inode() will
8726922c0c7SHugh Dickins 	 * prune a !swapped inode from the swaplist under this mutex.
873b1dea800SHugh Dickins 	 */
874b1dea800SHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
87505bf86b4SHugh Dickins 	if (list_empty(&info->swaplist))
87605bf86b4SHugh Dickins 		list_add_tail(&info->swaplist, &shmem_swaplist);
877b1dea800SHugh Dickins 
87848f170fbSHugh Dickins 	if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
879aaa46865SHugh Dickins 		swap_shmem_alloc(swap);
8806922c0c7SHugh Dickins 		shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
8816922c0c7SHugh Dickins 
8826922c0c7SHugh Dickins 		spin_lock(&info->lock);
8836922c0c7SHugh Dickins 		info->swapped++;
8846922c0c7SHugh Dickins 		shmem_recalc_inode(inode);
885826267cfSHugh Dickins 		spin_unlock(&info->lock);
8866922c0c7SHugh Dickins 
8876922c0c7SHugh Dickins 		mutex_unlock(&shmem_swaplist_mutex);
888d9fe526aSHugh Dickins 		BUG_ON(page_mapped(page));
8899fab5619SHugh Dickins 		swap_writepage(page, wbc);
8901da177e4SLinus Torvalds 		return 0;
8911da177e4SLinus Torvalds 	}
8921da177e4SLinus Torvalds 
8936922c0c7SHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
894cb4b86baSKAMEZAWA Hiroyuki 	swapcache_free(swap, NULL);
8951da177e4SLinus Torvalds redirty:
8961da177e4SLinus Torvalds 	set_page_dirty(page);
897d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
898d9fe526aSHugh Dickins 		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
899d9fe526aSHugh Dickins 	unlock_page(page);
900d9fe526aSHugh Dickins 	return 0;
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds #ifdef CONFIG_NUMA
904680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
90571fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
906680d794bSakpm@linux-foundation.org {
907680d794bSakpm@linux-foundation.org 	char buffer[64];
908680d794bSakpm@linux-foundation.org 
90971fe804bSLee Schermerhorn 	if (!mpol || mpol->mode == MPOL_DEFAULT)
910095f1fc4SLee Schermerhorn 		return;		/* show nothing */
911095f1fc4SLee Schermerhorn 
91271fe804bSLee Schermerhorn 	mpol_to_str(buffer, sizeof(buffer), mpol, 1);
913095f1fc4SLee Schermerhorn 
914095f1fc4SLee Schermerhorn 	seq_printf(seq, ",mpol=%s", buffer);
915680d794bSakpm@linux-foundation.org }
91671fe804bSLee Schermerhorn 
91771fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
91871fe804bSLee Schermerhorn {
91971fe804bSLee Schermerhorn 	struct mempolicy *mpol = NULL;
92071fe804bSLee Schermerhorn 	if (sbinfo->mpol) {
92171fe804bSLee Schermerhorn 		spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
92271fe804bSLee Schermerhorn 		mpol = sbinfo->mpol;
92371fe804bSLee Schermerhorn 		mpol_get(mpol);
92471fe804bSLee Schermerhorn 		spin_unlock(&sbinfo->stat_lock);
92571fe804bSLee Schermerhorn 	}
92671fe804bSLee Schermerhorn 	return mpol;
92771fe804bSLee Schermerhorn }
928680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
929680d794bSakpm@linux-foundation.org 
93041ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
93141ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
9321da177e4SLinus Torvalds {
93352cd3b07SLee Schermerhorn 	struct mempolicy mpol, *spol;
9341da177e4SLinus Torvalds 	struct vm_area_struct pvma;
9351da177e4SLinus Torvalds 
93652cd3b07SLee Schermerhorn 	spol = mpol_cond_copy(&mpol,
93741ffe5d5SHugh Dickins 			mpol_shared_policy_lookup(&info->policy, index));
93852cd3b07SLee Schermerhorn 
9391da177e4SLinus Torvalds 	/* Create a pseudo vma that just contains the policy */
940c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
94141ffe5d5SHugh Dickins 	pvma.vm_pgoff = index;
942c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
94352cd3b07SLee Schermerhorn 	pvma.vm_policy = spol;
94441ffe5d5SHugh Dickins 	return swapin_readahead(swap, gfp, &pvma, 0);
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds 
94702098feaSHugh Dickins static struct page *shmem_alloc_page(gfp_t gfp,
94841ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
9491da177e4SLinus Torvalds {
9501da177e4SLinus Torvalds 	struct vm_area_struct pvma;
9511da177e4SLinus Torvalds 
952c4cc6d07SHugh Dickins 	/* Create a pseudo vma that just contains the policy */
953c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
95441ffe5d5SHugh Dickins 	pvma.vm_pgoff = index;
955c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
95641ffe5d5SHugh Dickins 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
95752cd3b07SLee Schermerhorn 
95852cd3b07SLee Schermerhorn 	/*
95952cd3b07SLee Schermerhorn 	 * alloc_page_vma() will drop the shared policy reference
96052cd3b07SLee Schermerhorn 	 */
96152cd3b07SLee Schermerhorn 	return alloc_page_vma(gfp, &pvma, 0);
9621da177e4SLinus Torvalds }
963680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */
964680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
96541ffe5d5SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
966680d794bSakpm@linux-foundation.org {
967680d794bSakpm@linux-foundation.org }
968680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
969680d794bSakpm@linux-foundation.org 
97041ffe5d5SHugh Dickins static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
97141ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
9721da177e4SLinus Torvalds {
97341ffe5d5SHugh Dickins 	return swapin_readahead(swap, gfp, NULL, 0);
9741da177e4SLinus Torvalds }
9751da177e4SLinus Torvalds 
97602098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp,
97741ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
9781da177e4SLinus Torvalds {
979e84e2e13SHugh Dickins 	return alloc_page(gfp);
9801da177e4SLinus Torvalds }
981680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */
9821da177e4SLinus Torvalds 
98371fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
98471fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
98571fe804bSLee Schermerhorn {
98671fe804bSLee Schermerhorn 	return NULL;
98771fe804bSLee Schermerhorn }
98871fe804bSLee Schermerhorn #endif
98971fe804bSLee Schermerhorn 
9901da177e4SLinus Torvalds /*
991bde05d1cSHugh Dickins  * When a page is moved from swapcache to shmem filecache (either by the
992bde05d1cSHugh Dickins  * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
993bde05d1cSHugh Dickins  * shmem_unuse_inode()), it may have been read in earlier from swap, in
994bde05d1cSHugh Dickins  * ignorance of the mapping it belongs to.  If that mapping has special
995bde05d1cSHugh Dickins  * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
996bde05d1cSHugh Dickins  * we may need to copy to a suitable page before moving to filecache.
997bde05d1cSHugh Dickins  *
998bde05d1cSHugh Dickins  * In a future release, this may well be extended to respect cpuset and
999bde05d1cSHugh Dickins  * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1000bde05d1cSHugh Dickins  * but for now it is a simple matter of zone.
1001bde05d1cSHugh Dickins  */
1002bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1003bde05d1cSHugh Dickins {
1004bde05d1cSHugh Dickins 	return page_zonenum(page) > gfp_zone(gfp);
1005bde05d1cSHugh Dickins }
1006bde05d1cSHugh Dickins 
1007bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1008bde05d1cSHugh Dickins 				struct shmem_inode_info *info, pgoff_t index)
1009bde05d1cSHugh Dickins {
1010bde05d1cSHugh Dickins 	struct page *oldpage, *newpage;
1011bde05d1cSHugh Dickins 	struct address_space *swap_mapping;
1012bde05d1cSHugh Dickins 	pgoff_t swap_index;
1013bde05d1cSHugh Dickins 	int error;
1014bde05d1cSHugh Dickins 
1015bde05d1cSHugh Dickins 	oldpage = *pagep;
1016bde05d1cSHugh Dickins 	swap_index = page_private(oldpage);
1017bde05d1cSHugh Dickins 	swap_mapping = page_mapping(oldpage);
1018bde05d1cSHugh Dickins 
1019bde05d1cSHugh Dickins 	/*
1020bde05d1cSHugh Dickins 	 * We have arrived here because our zones are constrained, so don't
1021bde05d1cSHugh Dickins 	 * limit chance of success by further cpuset and node constraints.
1022bde05d1cSHugh Dickins 	 */
1023bde05d1cSHugh Dickins 	gfp &= ~GFP_CONSTRAINT_MASK;
1024bde05d1cSHugh Dickins 	newpage = shmem_alloc_page(gfp, info, index);
1025bde05d1cSHugh Dickins 	if (!newpage)
1026bde05d1cSHugh Dickins 		return -ENOMEM;
1027bde05d1cSHugh Dickins 
1028bde05d1cSHugh Dickins 	page_cache_get(newpage);
1029bde05d1cSHugh Dickins 	copy_highpage(newpage, oldpage);
10300142ef6cSHugh Dickins 	flush_dcache_page(newpage);
1031bde05d1cSHugh Dickins 
1032bde05d1cSHugh Dickins 	__set_page_locked(newpage);
1033bde05d1cSHugh Dickins 	SetPageUptodate(newpage);
1034bde05d1cSHugh Dickins 	SetPageSwapBacked(newpage);
1035bde05d1cSHugh Dickins 	set_page_private(newpage, swap_index);
1036bde05d1cSHugh Dickins 	SetPageSwapCache(newpage);
1037bde05d1cSHugh Dickins 
1038bde05d1cSHugh Dickins 	/*
1039bde05d1cSHugh Dickins 	 * Our caller will very soon move newpage out of swapcache, but it's
1040bde05d1cSHugh Dickins 	 * a nice clean interface for us to replace oldpage by newpage there.
1041bde05d1cSHugh Dickins 	 */
1042bde05d1cSHugh Dickins 	spin_lock_irq(&swap_mapping->tree_lock);
1043bde05d1cSHugh Dickins 	error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
1044bde05d1cSHugh Dickins 								   newpage);
10450142ef6cSHugh Dickins 	if (!error) {
1046bde05d1cSHugh Dickins 		__inc_zone_page_state(newpage, NR_FILE_PAGES);
1047bde05d1cSHugh Dickins 		__dec_zone_page_state(oldpage, NR_FILE_PAGES);
10480142ef6cSHugh Dickins 	}
1049bde05d1cSHugh Dickins 	spin_unlock_irq(&swap_mapping->tree_lock);
1050bde05d1cSHugh Dickins 
10510142ef6cSHugh Dickins 	if (unlikely(error)) {
10520142ef6cSHugh Dickins 		/*
10530142ef6cSHugh Dickins 		 * Is this possible?  I think not, now that our callers check
10540142ef6cSHugh Dickins 		 * both PageSwapCache and page_private after getting page lock;
10550142ef6cSHugh Dickins 		 * but be defensive.  Reverse old to newpage for clear and free.
10560142ef6cSHugh Dickins 		 */
10570142ef6cSHugh Dickins 		oldpage = newpage;
10580142ef6cSHugh Dickins 	} else {
1059bde05d1cSHugh Dickins 		mem_cgroup_replace_page_cache(oldpage, newpage);
1060bde05d1cSHugh Dickins 		lru_cache_add_anon(newpage);
10610142ef6cSHugh Dickins 		*pagep = newpage;
10620142ef6cSHugh Dickins 	}
1063bde05d1cSHugh Dickins 
1064bde05d1cSHugh Dickins 	ClearPageSwapCache(oldpage);
1065bde05d1cSHugh Dickins 	set_page_private(oldpage, 0);
1066bde05d1cSHugh Dickins 
1067bde05d1cSHugh Dickins 	unlock_page(oldpage);
1068bde05d1cSHugh Dickins 	page_cache_release(oldpage);
1069bde05d1cSHugh Dickins 	page_cache_release(oldpage);
10700142ef6cSHugh Dickins 	return error;
1071bde05d1cSHugh Dickins }
1072bde05d1cSHugh Dickins 
1073bde05d1cSHugh Dickins /*
107468da9f05SHugh Dickins  * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
10751da177e4SLinus Torvalds  *
10761da177e4SLinus Torvalds  * If we allocate a new one we do not mark it dirty. That's up to the
10771da177e4SLinus Torvalds  * vm. If we swap it in we mark it dirty since we also free the swap
10781da177e4SLinus Torvalds  * entry since a page cannot live in both the swap and page cache
10791da177e4SLinus Torvalds  */
108041ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
108168da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
10821da177e4SLinus Torvalds {
10831da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
108454af6042SHugh Dickins 	struct shmem_inode_info *info;
10851da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo;
108627ab7006SHugh Dickins 	struct page *page;
10871da177e4SLinus Torvalds 	swp_entry_t swap;
10881da177e4SLinus Torvalds 	int error;
108954af6042SHugh Dickins 	int once = 0;
10901635f6a7SHugh Dickins 	int alloced = 0;
10911da177e4SLinus Torvalds 
109241ffe5d5SHugh Dickins 	if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
10931da177e4SLinus Torvalds 		return -EFBIG;
10941da177e4SLinus Torvalds repeat:
109554af6042SHugh Dickins 	swap.val = 0;
109641ffe5d5SHugh Dickins 	page = find_lock_page(mapping, index);
109754af6042SHugh Dickins 	if (radix_tree_exceptional_entry(page)) {
109854af6042SHugh Dickins 		swap = radix_to_swp_entry(page);
109954af6042SHugh Dickins 		page = NULL;
110054af6042SHugh Dickins 	}
110154af6042SHugh Dickins 
11021635f6a7SHugh Dickins 	if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
110354af6042SHugh Dickins 	    ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
110454af6042SHugh Dickins 		error = -EINVAL;
110554af6042SHugh Dickins 		goto failed;
110654af6042SHugh Dickins 	}
110754af6042SHugh Dickins 
11081635f6a7SHugh Dickins 	/* fallocated page? */
11091635f6a7SHugh Dickins 	if (page && !PageUptodate(page)) {
11101635f6a7SHugh Dickins 		if (sgp != SGP_READ)
11111635f6a7SHugh Dickins 			goto clear;
11121635f6a7SHugh Dickins 		unlock_page(page);
11131635f6a7SHugh Dickins 		page_cache_release(page);
11141635f6a7SHugh Dickins 		page = NULL;
11151635f6a7SHugh Dickins 	}
111654af6042SHugh Dickins 	if (page || (sgp == SGP_READ && !swap.val)) {
111754af6042SHugh Dickins 		*pagep = page;
111854af6042SHugh Dickins 		return 0;
111927ab7006SHugh Dickins 	}
112027ab7006SHugh Dickins 
1121b409f9fcSHugh Dickins 	/*
112254af6042SHugh Dickins 	 * Fast cache lookup did not find it:
112354af6042SHugh Dickins 	 * bring it back from swap or allocate.
1124b409f9fcSHugh Dickins 	 */
112554af6042SHugh Dickins 	info = SHMEM_I(inode);
112654af6042SHugh Dickins 	sbinfo = SHMEM_SB(inode->i_sb);
112727ab7006SHugh Dickins 
11281da177e4SLinus Torvalds 	if (swap.val) {
11291da177e4SLinus Torvalds 		/* Look it up and read it in.. */
113027ab7006SHugh Dickins 		page = lookup_swap_cache(swap);
113127ab7006SHugh Dickins 		if (!page) {
1132456f998eSYing Han 			/* here we actually do the io */
113368da9f05SHugh Dickins 			if (fault_type)
113468da9f05SHugh Dickins 				*fault_type |= VM_FAULT_MAJOR;
113541ffe5d5SHugh Dickins 			page = shmem_swapin(swap, gfp, info, index);
113627ab7006SHugh Dickins 			if (!page) {
11371da177e4SLinus Torvalds 				error = -ENOMEM;
113854af6042SHugh Dickins 				goto failed;
1139285b2c4fSHugh Dickins 			}
11401da177e4SLinus Torvalds 		}
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 		/* We have to do this with page locked to prevent races */
114354af6042SHugh Dickins 		lock_page(page);
11440142ef6cSHugh Dickins 		if (!PageSwapCache(page) || page_private(page) != swap.val ||
1145*d1899228SHugh Dickins 		    !shmem_confirm_swap(mapping, index, swap)) {
1146bde05d1cSHugh Dickins 			error = -EEXIST;	/* try again */
1147*d1899228SHugh Dickins 			goto unlock;
1148bde05d1cSHugh Dickins 		}
114927ab7006SHugh Dickins 		if (!PageUptodate(page)) {
11501da177e4SLinus Torvalds 			error = -EIO;
115154af6042SHugh Dickins 			goto failed;
115254af6042SHugh Dickins 		}
115354af6042SHugh Dickins 		wait_on_page_writeback(page);
115454af6042SHugh Dickins 
1155bde05d1cSHugh Dickins 		if (shmem_should_replace_page(page, gfp)) {
1156bde05d1cSHugh Dickins 			error = shmem_replace_page(&page, gfp, info, index);
1157bde05d1cSHugh Dickins 			if (error)
115854af6042SHugh Dickins 				goto failed;
11591da177e4SLinus Torvalds 		}
11601da177e4SLinus Torvalds 
1161aa3b1895SHugh Dickins 		error = mem_cgroup_cache_charge(page, current->mm,
1162aa3b1895SHugh Dickins 						gfp & GFP_RECLAIM_MASK);
1163*d1899228SHugh Dickins 		if (!error) {
116454af6042SHugh Dickins 			error = shmem_add_to_page_cache(page, mapping, index,
116554af6042SHugh Dickins 						gfp, swp_to_radix_entry(swap));
1166*d1899228SHugh Dickins 			/* We already confirmed swap, and make no allocation */
1167*d1899228SHugh Dickins 			VM_BUG_ON(error);
1168*d1899228SHugh Dickins 		}
116954af6042SHugh Dickins 		if (error)
117054af6042SHugh Dickins 			goto failed;
117154af6042SHugh Dickins 
117254af6042SHugh Dickins 		spin_lock(&info->lock);
117354af6042SHugh Dickins 		info->swapped--;
117454af6042SHugh Dickins 		shmem_recalc_inode(inode);
11751da177e4SLinus Torvalds 		spin_unlock(&info->lock);
117627ab7006SHugh Dickins 
117727ab7006SHugh Dickins 		delete_from_swap_cache(page);
117827ab7006SHugh Dickins 		set_page_dirty(page);
117927ab7006SHugh Dickins 		swap_free(swap);
118027ab7006SHugh Dickins 
118154af6042SHugh Dickins 	} else {
118254af6042SHugh Dickins 		if (shmem_acct_block(info->flags)) {
118354af6042SHugh Dickins 			error = -ENOSPC;
118454af6042SHugh Dickins 			goto failed;
11851da177e4SLinus Torvalds 		}
11860edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
1187fc5da22aSHugh Dickins 			if (percpu_counter_compare(&sbinfo->used_blocks,
118854af6042SHugh Dickins 						sbinfo->max_blocks) >= 0) {
118954af6042SHugh Dickins 				error = -ENOSPC;
119054af6042SHugh Dickins 				goto unacct;
119154af6042SHugh Dickins 			}
11927e496299STim Chen 			percpu_counter_inc(&sbinfo->used_blocks);
119359a16eadSHugh Dickins 		}
11941da177e4SLinus Torvalds 
119554af6042SHugh Dickins 		page = shmem_alloc_page(gfp, info, index);
119654af6042SHugh Dickins 		if (!page) {
119754af6042SHugh Dickins 			error = -ENOMEM;
119854af6042SHugh Dickins 			goto decused;
119954af6042SHugh Dickins 		}
120054af6042SHugh Dickins 
120154af6042SHugh Dickins 		SetPageSwapBacked(page);
120254af6042SHugh Dickins 		__set_page_locked(page);
1203aa3b1895SHugh Dickins 		error = mem_cgroup_cache_charge(page, current->mm,
1204aa3b1895SHugh Dickins 						gfp & GFP_RECLAIM_MASK);
1205aa3b1895SHugh Dickins 		if (!error)
120654af6042SHugh Dickins 			error = shmem_add_to_page_cache(page, mapping, index,
120754af6042SHugh Dickins 						gfp, NULL);
120854af6042SHugh Dickins 		if (error)
120954af6042SHugh Dickins 			goto decused;
121054af6042SHugh Dickins 		lru_cache_add_anon(page);
121154af6042SHugh Dickins 
121254af6042SHugh Dickins 		spin_lock(&info->lock);
12131da177e4SLinus Torvalds 		info->alloced++;
121454af6042SHugh Dickins 		inode->i_blocks += BLOCKS_PER_PAGE;
121554af6042SHugh Dickins 		shmem_recalc_inode(inode);
121659a16eadSHugh Dickins 		spin_unlock(&info->lock);
12171635f6a7SHugh Dickins 		alloced = true;
121854af6042SHugh Dickins 
1219ec9516fbSHugh Dickins 		/*
12201635f6a7SHugh Dickins 		 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
12211635f6a7SHugh Dickins 		 */
12221635f6a7SHugh Dickins 		if (sgp == SGP_FALLOC)
12231635f6a7SHugh Dickins 			sgp = SGP_WRITE;
12241635f6a7SHugh Dickins clear:
12251635f6a7SHugh Dickins 		/*
12261635f6a7SHugh Dickins 		 * Let SGP_WRITE caller clear ends if write does not fill page;
12271635f6a7SHugh Dickins 		 * but SGP_FALLOC on a page fallocated earlier must initialize
12281635f6a7SHugh Dickins 		 * it now, lest undo on failure cancel our earlier guarantee.
1229ec9516fbSHugh Dickins 		 */
1230ec9516fbSHugh Dickins 		if (sgp != SGP_WRITE) {
123127ab7006SHugh Dickins 			clear_highpage(page);
123227ab7006SHugh Dickins 			flush_dcache_page(page);
123327ab7006SHugh Dickins 			SetPageUptodate(page);
1234ec9516fbSHugh Dickins 		}
1235a0ee5ec5SHugh Dickins 		if (sgp == SGP_DIRTY)
123627ab7006SHugh Dickins 			set_page_dirty(page);
12371da177e4SLinus Torvalds 	}
1238bde05d1cSHugh Dickins 
123954af6042SHugh Dickins 	/* Perhaps the file has been truncated since we checked */
12401635f6a7SHugh Dickins 	if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
124154af6042SHugh Dickins 	    ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
124254af6042SHugh Dickins 		error = -EINVAL;
12431635f6a7SHugh Dickins 		if (alloced)
124454af6042SHugh Dickins 			goto trunc;
12451635f6a7SHugh Dickins 		else
12461635f6a7SHugh Dickins 			goto failed;
1247ff36b801SShaohua Li 	}
124854af6042SHugh Dickins 	*pagep = page;
124954af6042SHugh Dickins 	return 0;
1250d00806b1SNick Piggin 
1251d0217ac0SNick Piggin 	/*
125254af6042SHugh Dickins 	 * Error recovery.
12531da177e4SLinus Torvalds 	 */
125454af6042SHugh Dickins trunc:
12551635f6a7SHugh Dickins 	info = SHMEM_I(inode);
125654af6042SHugh Dickins 	ClearPageDirty(page);
125754af6042SHugh Dickins 	delete_from_page_cache(page);
125854af6042SHugh Dickins 	spin_lock(&info->lock);
125954af6042SHugh Dickins 	info->alloced--;
126054af6042SHugh Dickins 	inode->i_blocks -= BLOCKS_PER_PAGE;
12611da177e4SLinus Torvalds 	spin_unlock(&info->lock);
126254af6042SHugh Dickins decused:
12631635f6a7SHugh Dickins 	sbinfo = SHMEM_SB(inode->i_sb);
126454af6042SHugh Dickins 	if (sbinfo->max_blocks)
126554af6042SHugh Dickins 		percpu_counter_add(&sbinfo->used_blocks, -1);
126654af6042SHugh Dickins unacct:
126754af6042SHugh Dickins 	shmem_unacct_blocks(info->flags, 1);
126854af6042SHugh Dickins failed:
1269*d1899228SHugh Dickins 	if (swap.val && error != -EINVAL &&
1270*d1899228SHugh Dickins 	    !shmem_confirm_swap(mapping, index, swap))
127154af6042SHugh Dickins 		error = -EEXIST;
1272*d1899228SHugh Dickins unlock:
127327ab7006SHugh Dickins 	if (page) {
127454af6042SHugh Dickins 		unlock_page(page);
12751da177e4SLinus Torvalds 		page_cache_release(page);
127654af6042SHugh Dickins 	}
127754af6042SHugh Dickins 	if (error == -ENOSPC && !once++) {
127854af6042SHugh Dickins 		info = SHMEM_I(inode);
127954af6042SHugh Dickins 		spin_lock(&info->lock);
128054af6042SHugh Dickins 		shmem_recalc_inode(inode);
128154af6042SHugh Dickins 		spin_unlock(&info->lock);
12821da177e4SLinus Torvalds 		goto repeat;
1283d8dc74f2SAdrian Bunk 	}
1284*d1899228SHugh Dickins 	if (error == -EEXIST)	/* from above or from radix_tree_insert */
128554af6042SHugh Dickins 		goto repeat;
128654af6042SHugh Dickins 	return error;
12871da177e4SLinus Torvalds }
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
12901da177e4SLinus Torvalds {
12911da177e4SLinus Torvalds 	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
12921da177e4SLinus Torvalds 	int error;
129368da9f05SHugh Dickins 	int ret = VM_FAULT_LOCKED;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
12961da177e4SLinus Torvalds 	if (error)
12971da177e4SLinus Torvalds 		return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
129868da9f05SHugh Dickins 
1299456f998eSYing Han 	if (ret & VM_FAULT_MAJOR) {
1300456f998eSYing Han 		count_vm_event(PGMAJFAULT);
1301456f998eSYing Han 		mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1302456f998eSYing Han 	}
130368da9f05SHugh Dickins 	return ret;
13041da177e4SLinus Torvalds }
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds #ifdef CONFIG_NUMA
130741ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
13081da177e4SLinus Torvalds {
130941ffe5d5SHugh Dickins 	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
131041ffe5d5SHugh Dickins 	return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds 
1313d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1314d8dc74f2SAdrian Bunk 					  unsigned long addr)
13151da177e4SLinus Torvalds {
131641ffe5d5SHugh Dickins 	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
131741ffe5d5SHugh Dickins 	pgoff_t index;
13181da177e4SLinus Torvalds 
131941ffe5d5SHugh Dickins 	index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
132041ffe5d5SHugh Dickins 	return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds #endif
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user)
13251da177e4SLinus Torvalds {
1326d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
13271da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
13281da177e4SLinus Torvalds 	int retval = -ENOMEM;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 	spin_lock(&info->lock);
13311da177e4SLinus Torvalds 	if (lock && !(info->flags & VM_LOCKED)) {
13321da177e4SLinus Torvalds 		if (!user_shm_lock(inode->i_size, user))
13331da177e4SLinus Torvalds 			goto out_nomem;
13341da177e4SLinus Torvalds 		info->flags |= VM_LOCKED;
133589e004eaSLee Schermerhorn 		mapping_set_unevictable(file->f_mapping);
13361da177e4SLinus Torvalds 	}
13371da177e4SLinus Torvalds 	if (!lock && (info->flags & VM_LOCKED) && user) {
13381da177e4SLinus Torvalds 		user_shm_unlock(inode->i_size, user);
13391da177e4SLinus Torvalds 		info->flags &= ~VM_LOCKED;
134089e004eaSLee Schermerhorn 		mapping_clear_unevictable(file->f_mapping);
13411da177e4SLinus Torvalds 	}
13421da177e4SLinus Torvalds 	retval = 0;
134389e004eaSLee Schermerhorn 
13441da177e4SLinus Torvalds out_nomem:
13451da177e4SLinus Torvalds 	spin_unlock(&info->lock);
13461da177e4SLinus Torvalds 	return retval;
13471da177e4SLinus Torvalds }
13481da177e4SLinus Torvalds 
13499b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
13501da177e4SLinus Torvalds {
13511da177e4SLinus Torvalds 	file_accessed(file);
13521da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
1353d0217ac0SNick Piggin 	vma->vm_flags |= VM_CAN_NONLINEAR;
13541da177e4SLinus Torvalds 	return 0;
13551da177e4SLinus Torvalds }
13561da177e4SLinus Torvalds 
1357454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
135809208d15SAl Viro 				     umode_t mode, dev_t dev, unsigned long flags)
13591da177e4SLinus Torvalds {
13601da177e4SLinus Torvalds 	struct inode *inode;
13611da177e4SLinus Torvalds 	struct shmem_inode_info *info;
13621da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
13631da177e4SLinus Torvalds 
13645b04c689SPavel Emelyanov 	if (shmem_reserve_inode(sb))
13651da177e4SLinus Torvalds 		return NULL;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	inode = new_inode(sb);
13681da177e4SLinus Torvalds 	if (inode) {
136985fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
1370454abafeSDmitry Monakhov 		inode_init_owner(inode, dir, mode);
13711da177e4SLinus Torvalds 		inode->i_blocks = 0;
13721da177e4SLinus Torvalds 		inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
13731da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
137491828a40SDavid M. Grimes 		inode->i_generation = get_seconds();
13751da177e4SLinus Torvalds 		info = SHMEM_I(inode);
13761da177e4SLinus Torvalds 		memset(info, 0, (char *)inode - (char *)info);
13771da177e4SLinus Torvalds 		spin_lock_init(&info->lock);
13780b0a0806SHugh Dickins 		info->flags = flags & VM_NORESERVE;
13791da177e4SLinus Torvalds 		INIT_LIST_HEAD(&info->swaplist);
1380b09e0fa4SEric Paris 		INIT_LIST_HEAD(&info->xattr_list);
138172c04902SAl Viro 		cache_no_acl(inode);
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
13841da177e4SLinus Torvalds 		default:
138539f0247dSAndreas Gruenbacher 			inode->i_op = &shmem_special_inode_operations;
13861da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
13871da177e4SLinus Torvalds 			break;
13881da177e4SLinus Torvalds 		case S_IFREG:
138914fcc23fSHugh Dickins 			inode->i_mapping->a_ops = &shmem_aops;
13901da177e4SLinus Torvalds 			inode->i_op = &shmem_inode_operations;
13911da177e4SLinus Torvalds 			inode->i_fop = &shmem_file_operations;
139271fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy,
139371fe804bSLee Schermerhorn 						 shmem_get_sbmpol(sbinfo));
13941da177e4SLinus Torvalds 			break;
13951da177e4SLinus Torvalds 		case S_IFDIR:
1396d8c76e6fSDave Hansen 			inc_nlink(inode);
13971da177e4SLinus Torvalds 			/* Some things misbehave if size == 0 on a directory */
13981da177e4SLinus Torvalds 			inode->i_size = 2 * BOGO_DIRENT_SIZE;
13991da177e4SLinus Torvalds 			inode->i_op = &shmem_dir_inode_operations;
14001da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
14011da177e4SLinus Torvalds 			break;
14021da177e4SLinus Torvalds 		case S_IFLNK:
14031da177e4SLinus Torvalds 			/*
14041da177e4SLinus Torvalds 			 * Must not load anything in the rbtree,
14051da177e4SLinus Torvalds 			 * mpol_free_shared_policy will not be called.
14061da177e4SLinus Torvalds 			 */
140771fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy, NULL);
14081da177e4SLinus Torvalds 			break;
14091da177e4SLinus Torvalds 		}
14105b04c689SPavel Emelyanov 	} else
14115b04c689SPavel Emelyanov 		shmem_free_inode(sb);
14121da177e4SLinus Torvalds 	return inode;
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
141692e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations;
141769f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations;
14181da177e4SLinus Torvalds 
14196d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR
14206d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
14216d9d88d0SJarkko Sakkinen #else
14226d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL
14236d9d88d0SJarkko Sakkinen #endif
14246d9d88d0SJarkko Sakkinen 
14251da177e4SLinus Torvalds static int
1426800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping,
1427800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
1428800d15a5SNick Piggin 			struct page **pagep, void **fsdata)
14291da177e4SLinus Torvalds {
1430800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1431800d15a5SNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1432800d15a5SNick Piggin 	return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1433800d15a5SNick Piggin }
1434800d15a5SNick Piggin 
1435800d15a5SNick Piggin static int
1436800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping,
1437800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
1438800d15a5SNick Piggin 			struct page *page, void *fsdata)
1439800d15a5SNick Piggin {
1440800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1441800d15a5SNick Piggin 
1442800d15a5SNick Piggin 	if (pos + copied > inode->i_size)
1443800d15a5SNick Piggin 		i_size_write(inode, pos + copied);
1444800d15a5SNick Piggin 
1445ec9516fbSHugh Dickins 	if (!PageUptodate(page)) {
1446ec9516fbSHugh Dickins 		if (copied < PAGE_CACHE_SIZE) {
1447ec9516fbSHugh Dickins 			unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1448ec9516fbSHugh Dickins 			zero_user_segments(page, 0, from,
1449ec9516fbSHugh Dickins 					from + copied, PAGE_CACHE_SIZE);
1450ec9516fbSHugh Dickins 		}
1451ec9516fbSHugh Dickins 		SetPageUptodate(page);
1452ec9516fbSHugh Dickins 	}
1453d3602444SHugh Dickins 	set_page_dirty(page);
14546746aff7SWu Fengguang 	unlock_page(page);
1455d3602444SHugh Dickins 	page_cache_release(page);
1456d3602444SHugh Dickins 
1457800d15a5SNick Piggin 	return copied;
14581da177e4SLinus Torvalds }
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
14611da177e4SLinus Torvalds {
1462d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
14631da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
146441ffe5d5SHugh Dickins 	pgoff_t index;
146541ffe5d5SHugh Dickins 	unsigned long offset;
1466a0ee5ec5SHugh Dickins 	enum sgp_type sgp = SGP_READ;
1467a0ee5ec5SHugh Dickins 
1468a0ee5ec5SHugh Dickins 	/*
1469a0ee5ec5SHugh Dickins 	 * Might this read be for a stacking filesystem?  Then when reading
1470a0ee5ec5SHugh Dickins 	 * holes of a sparse file, we actually need to allocate those pages,
1471a0ee5ec5SHugh Dickins 	 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1472a0ee5ec5SHugh Dickins 	 */
1473a0ee5ec5SHugh Dickins 	if (segment_eq(get_fs(), KERNEL_DS))
1474a0ee5ec5SHugh Dickins 		sgp = SGP_DIRTY;
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	index = *ppos >> PAGE_CACHE_SHIFT;
14771da177e4SLinus Torvalds 	offset = *ppos & ~PAGE_CACHE_MASK;
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 	for (;;) {
14801da177e4SLinus Torvalds 		struct page *page = NULL;
148141ffe5d5SHugh Dickins 		pgoff_t end_index;
148241ffe5d5SHugh Dickins 		unsigned long nr, ret;
14831da177e4SLinus Torvalds 		loff_t i_size = i_size_read(inode);
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
14861da177e4SLinus Torvalds 		if (index > end_index)
14871da177e4SLinus Torvalds 			break;
14881da177e4SLinus Torvalds 		if (index == end_index) {
14891da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
14901da177e4SLinus Torvalds 			if (nr <= offset)
14911da177e4SLinus Torvalds 				break;
14921da177e4SLinus Torvalds 		}
14931da177e4SLinus Torvalds 
1494a0ee5ec5SHugh Dickins 		desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
14951da177e4SLinus Torvalds 		if (desc->error) {
14961da177e4SLinus Torvalds 			if (desc->error == -EINVAL)
14971da177e4SLinus Torvalds 				desc->error = 0;
14981da177e4SLinus Torvalds 			break;
14991da177e4SLinus Torvalds 		}
1500d3602444SHugh Dickins 		if (page)
1501d3602444SHugh Dickins 			unlock_page(page);
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 		/*
15041da177e4SLinus Torvalds 		 * We must evaluate after, since reads (unlike writes)
15051b1dcc1bSJes Sorensen 		 * are called without i_mutex protection against truncate
15061da177e4SLinus Torvalds 		 */
15071da177e4SLinus Torvalds 		nr = PAGE_CACHE_SIZE;
15081da177e4SLinus Torvalds 		i_size = i_size_read(inode);
15091da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
15101da177e4SLinus Torvalds 		if (index == end_index) {
15111da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
15121da177e4SLinus Torvalds 			if (nr <= offset) {
15131da177e4SLinus Torvalds 				if (page)
15141da177e4SLinus Torvalds 					page_cache_release(page);
15151da177e4SLinus Torvalds 				break;
15161da177e4SLinus Torvalds 			}
15171da177e4SLinus Torvalds 		}
15181da177e4SLinus Torvalds 		nr -= offset;
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 		if (page) {
15211da177e4SLinus Torvalds 			/*
15221da177e4SLinus Torvalds 			 * If users can be writing to this page using arbitrary
15231da177e4SLinus Torvalds 			 * virtual addresses, take care about potential aliasing
15241da177e4SLinus Torvalds 			 * before reading the page on the kernel side.
15251da177e4SLinus Torvalds 			 */
15261da177e4SLinus Torvalds 			if (mapping_writably_mapped(mapping))
15271da177e4SLinus Torvalds 				flush_dcache_page(page);
15281da177e4SLinus Torvalds 			/*
15291da177e4SLinus Torvalds 			 * Mark the page accessed if we read the beginning.
15301da177e4SLinus Torvalds 			 */
15311da177e4SLinus Torvalds 			if (!offset)
15321da177e4SLinus Torvalds 				mark_page_accessed(page);
1533b5810039SNick Piggin 		} else {
15341da177e4SLinus Torvalds 			page = ZERO_PAGE(0);
1535b5810039SNick Piggin 			page_cache_get(page);
1536b5810039SNick Piggin 		}
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 		/*
15391da177e4SLinus Torvalds 		 * Ok, we have the page, and it's up-to-date, so
15401da177e4SLinus Torvalds 		 * now we can copy it to user space...
15411da177e4SLinus Torvalds 		 *
15421da177e4SLinus Torvalds 		 * The actor routine returns how many bytes were actually used..
15431da177e4SLinus Torvalds 		 * NOTE! This may not be the same as how much of a user buffer
15441da177e4SLinus Torvalds 		 * we filled up (we may be padding etc), so we can only update
15451da177e4SLinus Torvalds 		 * "pos" here (the actor routine has to update the user buffer
15461da177e4SLinus Torvalds 		 * pointers and the remaining count).
15471da177e4SLinus Torvalds 		 */
15481da177e4SLinus Torvalds 		ret = actor(desc, page, offset, nr);
15491da177e4SLinus Torvalds 		offset += ret;
15501da177e4SLinus Torvalds 		index += offset >> PAGE_CACHE_SHIFT;
15511da177e4SLinus Torvalds 		offset &= ~PAGE_CACHE_MASK;
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds 		page_cache_release(page);
15541da177e4SLinus Torvalds 		if (ret != nr || !desc->count)
15551da177e4SLinus Torvalds 			break;
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds 		cond_resched();
15581da177e4SLinus Torvalds 	}
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
15611da177e4SLinus Torvalds 	file_accessed(filp);
15621da177e4SLinus Torvalds }
15631da177e4SLinus Torvalds 
1564bcd78e49SHugh Dickins static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1565bcd78e49SHugh Dickins 		const struct iovec *iov, unsigned long nr_segs, loff_t pos)
15661da177e4SLinus Torvalds {
1567bcd78e49SHugh Dickins 	struct file *filp = iocb->ki_filp;
1568bcd78e49SHugh Dickins 	ssize_t retval;
1569bcd78e49SHugh Dickins 	unsigned long seg;
1570bcd78e49SHugh Dickins 	size_t count;
1571bcd78e49SHugh Dickins 	loff_t *ppos = &iocb->ki_pos;
1572bcd78e49SHugh Dickins 
1573bcd78e49SHugh Dickins 	retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1574bcd78e49SHugh Dickins 	if (retval)
1575bcd78e49SHugh Dickins 		return retval;
1576bcd78e49SHugh Dickins 
1577bcd78e49SHugh Dickins 	for (seg = 0; seg < nr_segs; seg++) {
15781da177e4SLinus Torvalds 		read_descriptor_t desc;
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 		desc.written = 0;
1581bcd78e49SHugh Dickins 		desc.arg.buf = iov[seg].iov_base;
1582bcd78e49SHugh Dickins 		desc.count = iov[seg].iov_len;
1583bcd78e49SHugh Dickins 		if (desc.count == 0)
1584bcd78e49SHugh Dickins 			continue;
15851da177e4SLinus Torvalds 		desc.error = 0;
15861da177e4SLinus Torvalds 		do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1587bcd78e49SHugh Dickins 		retval += desc.written;
1588bcd78e49SHugh Dickins 		if (desc.error) {
1589bcd78e49SHugh Dickins 			retval = retval ?: desc.error;
1590bcd78e49SHugh Dickins 			break;
1591bcd78e49SHugh Dickins 		}
1592bcd78e49SHugh Dickins 		if (desc.count > 0)
1593bcd78e49SHugh Dickins 			break;
1594bcd78e49SHugh Dickins 	}
1595bcd78e49SHugh Dickins 	return retval;
15961da177e4SLinus Torvalds }
15971da177e4SLinus Torvalds 
1598708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1599708e3508SHugh Dickins 				struct pipe_inode_info *pipe, size_t len,
1600708e3508SHugh Dickins 				unsigned int flags)
1601708e3508SHugh Dickins {
1602708e3508SHugh Dickins 	struct address_space *mapping = in->f_mapping;
160371f0e07aSHugh Dickins 	struct inode *inode = mapping->host;
1604708e3508SHugh Dickins 	unsigned int loff, nr_pages, req_pages;
1605708e3508SHugh Dickins 	struct page *pages[PIPE_DEF_BUFFERS];
1606708e3508SHugh Dickins 	struct partial_page partial[PIPE_DEF_BUFFERS];
1607708e3508SHugh Dickins 	struct page *page;
1608708e3508SHugh Dickins 	pgoff_t index, end_index;
1609708e3508SHugh Dickins 	loff_t isize, left;
1610708e3508SHugh Dickins 	int error, page_nr;
1611708e3508SHugh Dickins 	struct splice_pipe_desc spd = {
1612708e3508SHugh Dickins 		.pages = pages,
1613708e3508SHugh Dickins 		.partial = partial,
1614047fe360SEric Dumazet 		.nr_pages_max = PIPE_DEF_BUFFERS,
1615708e3508SHugh Dickins 		.flags = flags,
1616708e3508SHugh Dickins 		.ops = &page_cache_pipe_buf_ops,
1617708e3508SHugh Dickins 		.spd_release = spd_release_page,
1618708e3508SHugh Dickins 	};
1619708e3508SHugh Dickins 
162071f0e07aSHugh Dickins 	isize = i_size_read(inode);
1621708e3508SHugh Dickins 	if (unlikely(*ppos >= isize))
1622708e3508SHugh Dickins 		return 0;
1623708e3508SHugh Dickins 
1624708e3508SHugh Dickins 	left = isize - *ppos;
1625708e3508SHugh Dickins 	if (unlikely(left < len))
1626708e3508SHugh Dickins 		len = left;
1627708e3508SHugh Dickins 
1628708e3508SHugh Dickins 	if (splice_grow_spd(pipe, &spd))
1629708e3508SHugh Dickins 		return -ENOMEM;
1630708e3508SHugh Dickins 
1631708e3508SHugh Dickins 	index = *ppos >> PAGE_CACHE_SHIFT;
1632708e3508SHugh Dickins 	loff = *ppos & ~PAGE_CACHE_MASK;
1633708e3508SHugh Dickins 	req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1634708e3508SHugh Dickins 	nr_pages = min(req_pages, pipe->buffers);
1635708e3508SHugh Dickins 
1636708e3508SHugh Dickins 	spd.nr_pages = find_get_pages_contig(mapping, index,
1637708e3508SHugh Dickins 						nr_pages, spd.pages);
1638708e3508SHugh Dickins 	index += spd.nr_pages;
1639708e3508SHugh Dickins 	error = 0;
164071f0e07aSHugh Dickins 
1641708e3508SHugh Dickins 	while (spd.nr_pages < nr_pages) {
164271f0e07aSHugh Dickins 		error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
164371f0e07aSHugh Dickins 		if (error)
1644708e3508SHugh Dickins 			break;
1645708e3508SHugh Dickins 		unlock_page(page);
1646708e3508SHugh Dickins 		spd.pages[spd.nr_pages++] = page;
1647708e3508SHugh Dickins 		index++;
1648708e3508SHugh Dickins 	}
1649708e3508SHugh Dickins 
1650708e3508SHugh Dickins 	index = *ppos >> PAGE_CACHE_SHIFT;
1651708e3508SHugh Dickins 	nr_pages = spd.nr_pages;
1652708e3508SHugh Dickins 	spd.nr_pages = 0;
165371f0e07aSHugh Dickins 
1654708e3508SHugh Dickins 	for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1655708e3508SHugh Dickins 		unsigned int this_len;
1656708e3508SHugh Dickins 
1657708e3508SHugh Dickins 		if (!len)
1658708e3508SHugh Dickins 			break;
1659708e3508SHugh Dickins 
1660708e3508SHugh Dickins 		this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1661708e3508SHugh Dickins 		page = spd.pages[page_nr];
1662708e3508SHugh Dickins 
166371f0e07aSHugh Dickins 		if (!PageUptodate(page) || page->mapping != mapping) {
166471f0e07aSHugh Dickins 			error = shmem_getpage(inode, index, &page,
166571f0e07aSHugh Dickins 							SGP_CACHE, NULL);
166671f0e07aSHugh Dickins 			if (error)
1667708e3508SHugh Dickins 				break;
166871f0e07aSHugh Dickins 			unlock_page(page);
1669708e3508SHugh Dickins 			page_cache_release(spd.pages[page_nr]);
1670708e3508SHugh Dickins 			spd.pages[page_nr] = page;
1671708e3508SHugh Dickins 		}
1672708e3508SHugh Dickins 
167371f0e07aSHugh Dickins 		isize = i_size_read(inode);
1674708e3508SHugh Dickins 		end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1675708e3508SHugh Dickins 		if (unlikely(!isize || index > end_index))
1676708e3508SHugh Dickins 			break;
1677708e3508SHugh Dickins 
1678708e3508SHugh Dickins 		if (end_index == index) {
1679708e3508SHugh Dickins 			unsigned int plen;
1680708e3508SHugh Dickins 
1681708e3508SHugh Dickins 			plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1682708e3508SHugh Dickins 			if (plen <= loff)
1683708e3508SHugh Dickins 				break;
1684708e3508SHugh Dickins 
1685708e3508SHugh Dickins 			this_len = min(this_len, plen - loff);
1686708e3508SHugh Dickins 			len = this_len;
1687708e3508SHugh Dickins 		}
1688708e3508SHugh Dickins 
1689708e3508SHugh Dickins 		spd.partial[page_nr].offset = loff;
1690708e3508SHugh Dickins 		spd.partial[page_nr].len = this_len;
1691708e3508SHugh Dickins 		len -= this_len;
1692708e3508SHugh Dickins 		loff = 0;
1693708e3508SHugh Dickins 		spd.nr_pages++;
1694708e3508SHugh Dickins 		index++;
1695708e3508SHugh Dickins 	}
1696708e3508SHugh Dickins 
1697708e3508SHugh Dickins 	while (page_nr < nr_pages)
1698708e3508SHugh Dickins 		page_cache_release(spd.pages[page_nr++]);
1699708e3508SHugh Dickins 
1700708e3508SHugh Dickins 	if (spd.nr_pages)
1701708e3508SHugh Dickins 		error = splice_to_pipe(pipe, &spd);
1702708e3508SHugh Dickins 
1703047fe360SEric Dumazet 	splice_shrink_spd(&spd);
1704708e3508SHugh Dickins 
1705708e3508SHugh Dickins 	if (error > 0) {
1706708e3508SHugh Dickins 		*ppos += error;
1707708e3508SHugh Dickins 		file_accessed(in);
1708708e3508SHugh Dickins 	}
1709708e3508SHugh Dickins 	return error;
1710708e3508SHugh Dickins }
1711708e3508SHugh Dickins 
171283e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset,
171383e4fa9cSHugh Dickins 							 loff_t len)
171483e4fa9cSHugh Dickins {
171583e4fa9cSHugh Dickins 	struct inode *inode = file->f_path.dentry->d_inode;
1716e2d12e22SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
17171aac1400SHugh Dickins 	struct shmem_falloc shmem_falloc;
1718e2d12e22SHugh Dickins 	pgoff_t start, index, end;
1719e2d12e22SHugh Dickins 	int error;
172083e4fa9cSHugh Dickins 
172183e4fa9cSHugh Dickins 	mutex_lock(&inode->i_mutex);
172283e4fa9cSHugh Dickins 
172383e4fa9cSHugh Dickins 	if (mode & FALLOC_FL_PUNCH_HOLE) {
172483e4fa9cSHugh Dickins 		struct address_space *mapping = file->f_mapping;
172583e4fa9cSHugh Dickins 		loff_t unmap_start = round_up(offset, PAGE_SIZE);
172683e4fa9cSHugh Dickins 		loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
172783e4fa9cSHugh Dickins 
172883e4fa9cSHugh Dickins 		if ((u64)unmap_end > (u64)unmap_start)
172983e4fa9cSHugh Dickins 			unmap_mapping_range(mapping, unmap_start,
173083e4fa9cSHugh Dickins 					    1 + unmap_end - unmap_start, 0);
173183e4fa9cSHugh Dickins 		shmem_truncate_range(inode, offset, offset + len - 1);
173283e4fa9cSHugh Dickins 		/* No need to unmap again: hole-punching leaves COWed pages */
173383e4fa9cSHugh Dickins 		error = 0;
1734e2d12e22SHugh Dickins 		goto out;
173583e4fa9cSHugh Dickins 	}
173683e4fa9cSHugh Dickins 
1737e2d12e22SHugh Dickins 	/* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
1738e2d12e22SHugh Dickins 	error = inode_newsize_ok(inode, offset + len);
1739e2d12e22SHugh Dickins 	if (error)
1740e2d12e22SHugh Dickins 		goto out;
1741e2d12e22SHugh Dickins 
1742e2d12e22SHugh Dickins 	start = offset >> PAGE_CACHE_SHIFT;
1743e2d12e22SHugh Dickins 	end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1744e2d12e22SHugh Dickins 	/* Try to avoid a swapstorm if len is impossible to satisfy */
1745e2d12e22SHugh Dickins 	if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
1746e2d12e22SHugh Dickins 		error = -ENOSPC;
1747e2d12e22SHugh Dickins 		goto out;
1748e2d12e22SHugh Dickins 	}
1749e2d12e22SHugh Dickins 
17501aac1400SHugh Dickins 	shmem_falloc.start = start;
17511aac1400SHugh Dickins 	shmem_falloc.next  = start;
17521aac1400SHugh Dickins 	shmem_falloc.nr_falloced = 0;
17531aac1400SHugh Dickins 	shmem_falloc.nr_unswapped = 0;
17541aac1400SHugh Dickins 	spin_lock(&inode->i_lock);
17551aac1400SHugh Dickins 	inode->i_private = &shmem_falloc;
17561aac1400SHugh Dickins 	spin_unlock(&inode->i_lock);
17571aac1400SHugh Dickins 
1758e2d12e22SHugh Dickins 	for (index = start; index < end; index++) {
1759e2d12e22SHugh Dickins 		struct page *page;
1760e2d12e22SHugh Dickins 
1761e2d12e22SHugh Dickins 		/*
1762e2d12e22SHugh Dickins 		 * Good, the fallocate(2) manpage permits EINTR: we may have
1763e2d12e22SHugh Dickins 		 * been interrupted because we are using up too much memory.
1764e2d12e22SHugh Dickins 		 */
1765e2d12e22SHugh Dickins 		if (signal_pending(current))
1766e2d12e22SHugh Dickins 			error = -EINTR;
17671aac1400SHugh Dickins 		else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
17681aac1400SHugh Dickins 			error = -ENOMEM;
1769e2d12e22SHugh Dickins 		else
17701635f6a7SHugh Dickins 			error = shmem_getpage(inode, index, &page, SGP_FALLOC,
1771e2d12e22SHugh Dickins 									NULL);
1772e2d12e22SHugh Dickins 		if (error) {
17731635f6a7SHugh Dickins 			/* Remove the !PageUptodate pages we added */
17741635f6a7SHugh Dickins 			shmem_undo_range(inode,
17751635f6a7SHugh Dickins 				(loff_t)start << PAGE_CACHE_SHIFT,
17761635f6a7SHugh Dickins 				(loff_t)index << PAGE_CACHE_SHIFT, true);
17771aac1400SHugh Dickins 			goto undone;
1778e2d12e22SHugh Dickins 		}
1779e2d12e22SHugh Dickins 
1780e2d12e22SHugh Dickins 		/*
17811aac1400SHugh Dickins 		 * Inform shmem_writepage() how far we have reached.
17821aac1400SHugh Dickins 		 * No need for lock or barrier: we have the page lock.
17831aac1400SHugh Dickins 		 */
17841aac1400SHugh Dickins 		shmem_falloc.next++;
17851aac1400SHugh Dickins 		if (!PageUptodate(page))
17861aac1400SHugh Dickins 			shmem_falloc.nr_falloced++;
17871aac1400SHugh Dickins 
17881aac1400SHugh Dickins 		/*
17891635f6a7SHugh Dickins 		 * If !PageUptodate, leave it that way so that freeable pages
17901635f6a7SHugh Dickins 		 * can be recognized if we need to rollback on error later.
17911635f6a7SHugh Dickins 		 * But set_page_dirty so that memory pressure will swap rather
1792e2d12e22SHugh Dickins 		 * than free the pages we are allocating (and SGP_CACHE pages
1793e2d12e22SHugh Dickins 		 * might still be clean: we now need to mark those dirty too).
1794e2d12e22SHugh Dickins 		 */
1795e2d12e22SHugh Dickins 		set_page_dirty(page);
1796e2d12e22SHugh Dickins 		unlock_page(page);
1797e2d12e22SHugh Dickins 		page_cache_release(page);
1798e2d12e22SHugh Dickins 		cond_resched();
1799e2d12e22SHugh Dickins 	}
1800e2d12e22SHugh Dickins 
1801e2d12e22SHugh Dickins 	if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
1802e2d12e22SHugh Dickins 		i_size_write(inode, offset + len);
1803e2d12e22SHugh Dickins 	inode->i_ctime = CURRENT_TIME;
18041aac1400SHugh Dickins undone:
18051aac1400SHugh Dickins 	spin_lock(&inode->i_lock);
18061aac1400SHugh Dickins 	inode->i_private = NULL;
18071aac1400SHugh Dickins 	spin_unlock(&inode->i_lock);
1808e2d12e22SHugh Dickins out:
180983e4fa9cSHugh Dickins 	mutex_unlock(&inode->i_mutex);
181083e4fa9cSHugh Dickins 	return error;
181183e4fa9cSHugh Dickins }
181283e4fa9cSHugh Dickins 
1813726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
18141da177e4SLinus Torvalds {
1815726c3342SDavid Howells 	struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds 	buf->f_type = TMPFS_MAGIC;
18181da177e4SLinus Torvalds 	buf->f_bsize = PAGE_CACHE_SIZE;
18191da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
18200edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
18211da177e4SLinus Torvalds 		buf->f_blocks = sbinfo->max_blocks;
182241ffe5d5SHugh Dickins 		buf->f_bavail =
182341ffe5d5SHugh Dickins 		buf->f_bfree  = sbinfo->max_blocks -
182441ffe5d5SHugh Dickins 				percpu_counter_sum(&sbinfo->used_blocks);
18250edd73b3SHugh Dickins 	}
18260edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
18271da177e4SLinus Torvalds 		buf->f_files = sbinfo->max_inodes;
18281da177e4SLinus Torvalds 		buf->f_ffree = sbinfo->free_inodes;
18291da177e4SLinus Torvalds 	}
18301da177e4SLinus Torvalds 	/* else leave those fields 0 like simple_statfs */
18311da177e4SLinus Torvalds 	return 0;
18321da177e4SLinus Torvalds }
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds /*
18351da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
18361da177e4SLinus Torvalds  */
18371da177e4SLinus Torvalds static int
18381a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
18391da177e4SLinus Torvalds {
18400b0a0806SHugh Dickins 	struct inode *inode;
18411da177e4SLinus Torvalds 	int error = -ENOSPC;
18421da177e4SLinus Torvalds 
1843454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
18441da177e4SLinus Torvalds 	if (inode) {
18452a7dba39SEric Paris 		error = security_inode_init_security(inode, dir,
18469d8f13baSMimi Zohar 						     &dentry->d_name,
18476d9d88d0SJarkko Sakkinen 						     shmem_initxattrs, NULL);
1848570bc1c2SStephen Smalley 		if (error) {
1849570bc1c2SStephen Smalley 			if (error != -EOPNOTSUPP) {
1850570bc1c2SStephen Smalley 				iput(inode);
1851570bc1c2SStephen Smalley 				return error;
1852570bc1c2SStephen Smalley 			}
185339f0247dSAndreas Gruenbacher 		}
18541c7c474cSChristoph Hellwig #ifdef CONFIG_TMPFS_POSIX_ACL
18551c7c474cSChristoph Hellwig 		error = generic_acl_init(inode, dir);
185639f0247dSAndreas Gruenbacher 		if (error) {
185739f0247dSAndreas Gruenbacher 			iput(inode);
185839f0247dSAndreas Gruenbacher 			return error;
1859570bc1c2SStephen Smalley 		}
1860718deb6bSAl Viro #else
1861718deb6bSAl Viro 		error = 0;
18621c7c474cSChristoph Hellwig #endif
18631da177e4SLinus Torvalds 		dir->i_size += BOGO_DIRENT_SIZE;
18641da177e4SLinus Torvalds 		dir->i_ctime = dir->i_mtime = CURRENT_TIME;
18651da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
18661da177e4SLinus Torvalds 		dget(dentry); /* Extra count - pin the dentry in core */
18671da177e4SLinus Torvalds 	}
18681da177e4SLinus Torvalds 	return error;
18691da177e4SLinus Torvalds }
18701da177e4SLinus Torvalds 
187118bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
18721da177e4SLinus Torvalds {
18731da177e4SLinus Torvalds 	int error;
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds 	if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
18761da177e4SLinus Torvalds 		return error;
1877d8c76e6fSDave Hansen 	inc_nlink(dir);
18781da177e4SLinus Torvalds 	return 0;
18791da177e4SLinus Torvalds }
18801da177e4SLinus Torvalds 
18814acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
18821da177e4SLinus Torvalds 		struct nameidata *nd)
18831da177e4SLinus Torvalds {
18841da177e4SLinus Torvalds 	return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
18851da177e4SLinus Torvalds }
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds /*
18881da177e4SLinus Torvalds  * Link a file..
18891da177e4SLinus Torvalds  */
18901da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
18911da177e4SLinus Torvalds {
18921da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
18935b04c689SPavel Emelyanov 	int ret;
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	/*
18961da177e4SLinus Torvalds 	 * No ordinary (disk based) filesystem counts links as inodes;
18971da177e4SLinus Torvalds 	 * but each new link needs a new dentry, pinning lowmem, and
18981da177e4SLinus Torvalds 	 * tmpfs dentries cannot be pruned until they are unlinked.
18991da177e4SLinus Torvalds 	 */
19005b04c689SPavel Emelyanov 	ret = shmem_reserve_inode(inode->i_sb);
19015b04c689SPavel Emelyanov 	if (ret)
19025b04c689SPavel Emelyanov 		goto out;
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
19051da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1906d8c76e6fSDave Hansen 	inc_nlink(inode);
19077de9c6eeSAl Viro 	ihold(inode);	/* New dentry reference */
19081da177e4SLinus Torvalds 	dget(dentry);		/* Extra pinning count for the created dentry */
19091da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
19105b04c689SPavel Emelyanov out:
19115b04c689SPavel Emelyanov 	return ret;
19121da177e4SLinus Torvalds }
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry)
19151da177e4SLinus Torvalds {
19161da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19171da177e4SLinus Torvalds 
19185b04c689SPavel Emelyanov 	if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
19195b04c689SPavel Emelyanov 		shmem_free_inode(inode->i_sb);
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 	dir->i_size -= BOGO_DIRENT_SIZE;
19221da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
19239a53c3a7SDave Hansen 	drop_nlink(inode);
19241da177e4SLinus Torvalds 	dput(dentry);	/* Undo the count from "create" - this does all the work */
19251da177e4SLinus Torvalds 	return 0;
19261da177e4SLinus Torvalds }
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
19291da177e4SLinus Torvalds {
19301da177e4SLinus Torvalds 	if (!simple_empty(dentry))
19311da177e4SLinus Torvalds 		return -ENOTEMPTY;
19321da177e4SLinus Torvalds 
19339a53c3a7SDave Hansen 	drop_nlink(dentry->d_inode);
19349a53c3a7SDave Hansen 	drop_nlink(dir);
19351da177e4SLinus Torvalds 	return shmem_unlink(dir, dentry);
19361da177e4SLinus Torvalds }
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds /*
19391da177e4SLinus Torvalds  * The VFS layer already does all the dentry stuff for rename,
19401da177e4SLinus Torvalds  * we just have to decrement the usage count for the target if
19411da177e4SLinus Torvalds  * it exists so that the VFS layer correctly free's it when it
19421da177e4SLinus Torvalds  * gets overwritten.
19431da177e4SLinus Torvalds  */
19441da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
19451da177e4SLinus Torvalds {
19461da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
19471da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(inode->i_mode);
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
19501da177e4SLinus Torvalds 		return -ENOTEMPTY;
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
19531da177e4SLinus Torvalds 		(void) shmem_unlink(new_dir, new_dentry);
19541da177e4SLinus Torvalds 		if (they_are_dirs)
19559a53c3a7SDave Hansen 			drop_nlink(old_dir);
19561da177e4SLinus Torvalds 	} else if (they_are_dirs) {
19579a53c3a7SDave Hansen 		drop_nlink(old_dir);
1958d8c76e6fSDave Hansen 		inc_nlink(new_dir);
19591da177e4SLinus Torvalds 	}
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	old_dir->i_size -= BOGO_DIRENT_SIZE;
19621da177e4SLinus Torvalds 	new_dir->i_size += BOGO_DIRENT_SIZE;
19631da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime =
19641da177e4SLinus Torvalds 	new_dir->i_ctime = new_dir->i_mtime =
19651da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME;
19661da177e4SLinus Torvalds 	return 0;
19671da177e4SLinus Torvalds }
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
19701da177e4SLinus Torvalds {
19711da177e4SLinus Torvalds 	int error;
19721da177e4SLinus Torvalds 	int len;
19731da177e4SLinus Torvalds 	struct inode *inode;
19749276aad6SHugh Dickins 	struct page *page;
19751da177e4SLinus Torvalds 	char *kaddr;
19761da177e4SLinus Torvalds 	struct shmem_inode_info *info;
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds 	len = strlen(symname) + 1;
19791da177e4SLinus Torvalds 	if (len > PAGE_CACHE_SIZE)
19801da177e4SLinus Torvalds 		return -ENAMETOOLONG;
19811da177e4SLinus Torvalds 
1982454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
19831da177e4SLinus Torvalds 	if (!inode)
19841da177e4SLinus Torvalds 		return -ENOSPC;
19851da177e4SLinus Torvalds 
19869d8f13baSMimi Zohar 	error = security_inode_init_security(inode, dir, &dentry->d_name,
19876d9d88d0SJarkko Sakkinen 					     shmem_initxattrs, NULL);
1988570bc1c2SStephen Smalley 	if (error) {
1989570bc1c2SStephen Smalley 		if (error != -EOPNOTSUPP) {
1990570bc1c2SStephen Smalley 			iput(inode);
1991570bc1c2SStephen Smalley 			return error;
1992570bc1c2SStephen Smalley 		}
1993570bc1c2SStephen Smalley 		error = 0;
1994570bc1c2SStephen Smalley 	}
1995570bc1c2SStephen Smalley 
19961da177e4SLinus Torvalds 	info = SHMEM_I(inode);
19971da177e4SLinus Torvalds 	inode->i_size = len-1;
199869f07ec9SHugh Dickins 	if (len <= SHORT_SYMLINK_LEN) {
199969f07ec9SHugh Dickins 		info->symlink = kmemdup(symname, len, GFP_KERNEL);
200069f07ec9SHugh Dickins 		if (!info->symlink) {
200169f07ec9SHugh Dickins 			iput(inode);
200269f07ec9SHugh Dickins 			return -ENOMEM;
200369f07ec9SHugh Dickins 		}
200469f07ec9SHugh Dickins 		inode->i_op = &shmem_short_symlink_operations;
20051da177e4SLinus Torvalds 	} else {
20061da177e4SLinus Torvalds 		error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
20071da177e4SLinus Torvalds 		if (error) {
20081da177e4SLinus Torvalds 			iput(inode);
20091da177e4SLinus Torvalds 			return error;
20101da177e4SLinus Torvalds 		}
201114fcc23fSHugh Dickins 		inode->i_mapping->a_ops = &shmem_aops;
20121da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inode_operations;
20139b04c5feSCong Wang 		kaddr = kmap_atomic(page);
20141da177e4SLinus Torvalds 		memcpy(kaddr, symname, len);
20159b04c5feSCong Wang 		kunmap_atomic(kaddr);
2016ec9516fbSHugh Dickins 		SetPageUptodate(page);
20171da177e4SLinus Torvalds 		set_page_dirty(page);
20186746aff7SWu Fengguang 		unlock_page(page);
20191da177e4SLinus Torvalds 		page_cache_release(page);
20201da177e4SLinus Torvalds 	}
20211da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
20221da177e4SLinus Torvalds 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
20231da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
20241da177e4SLinus Torvalds 	dget(dentry);
20251da177e4SLinus Torvalds 	return 0;
20261da177e4SLinus Torvalds }
20271da177e4SLinus Torvalds 
202869f07ec9SHugh Dickins static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
20291da177e4SLinus Torvalds {
203069f07ec9SHugh Dickins 	nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
2031cc314eefSLinus Torvalds 	return NULL;
20321da177e4SLinus Torvalds }
20331da177e4SLinus Torvalds 
2034cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
20351da177e4SLinus Torvalds {
20361da177e4SLinus Torvalds 	struct page *page = NULL;
203741ffe5d5SHugh Dickins 	int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
203841ffe5d5SHugh Dickins 	nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
2039d3602444SHugh Dickins 	if (page)
2040d3602444SHugh Dickins 		unlock_page(page);
2041cc314eefSLinus Torvalds 	return page;
20421da177e4SLinus Torvalds }
20431da177e4SLinus Torvalds 
2044cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
20451da177e4SLinus Torvalds {
20461da177e4SLinus Torvalds 	if (!IS_ERR(nd_get_link(nd))) {
2047cc314eefSLinus Torvalds 		struct page *page = cookie;
20481da177e4SLinus Torvalds 		kunmap(page);
20491da177e4SLinus Torvalds 		mark_page_accessed(page);
20501da177e4SLinus Torvalds 		page_cache_release(page);
20511da177e4SLinus Torvalds 	}
20521da177e4SLinus Torvalds }
20531da177e4SLinus Torvalds 
2054b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2055b09e0fa4SEric Paris /*
2056b09e0fa4SEric Paris  * Superblocks without xattr inode operations may get some security.* xattr
2057b09e0fa4SEric Paris  * support from the LSM "for free". As soon as we have any other xattrs
2058b09e0fa4SEric Paris  * like ACLs, we also need to implement the security.* handlers at
2059b09e0fa4SEric Paris  * filesystem level, though.
2060b09e0fa4SEric Paris  */
2061b09e0fa4SEric Paris 
20626d9d88d0SJarkko Sakkinen /*
20636d9d88d0SJarkko Sakkinen  * Allocate new xattr and copy in the value; but leave the name to callers.
20646d9d88d0SJarkko Sakkinen  */
20656d9d88d0SJarkko Sakkinen static struct shmem_xattr *shmem_xattr_alloc(const void *value, size_t size)
20666d9d88d0SJarkko Sakkinen {
20676d9d88d0SJarkko Sakkinen 	struct shmem_xattr *new_xattr;
20686d9d88d0SJarkko Sakkinen 	size_t len;
20696d9d88d0SJarkko Sakkinen 
20706d9d88d0SJarkko Sakkinen 	/* wrap around? */
20716d9d88d0SJarkko Sakkinen 	len = sizeof(*new_xattr) + size;
20726d9d88d0SJarkko Sakkinen 	if (len <= sizeof(*new_xattr))
20736d9d88d0SJarkko Sakkinen 		return NULL;
20746d9d88d0SJarkko Sakkinen 
20756d9d88d0SJarkko Sakkinen 	new_xattr = kmalloc(len, GFP_KERNEL);
20766d9d88d0SJarkko Sakkinen 	if (!new_xattr)
20776d9d88d0SJarkko Sakkinen 		return NULL;
20786d9d88d0SJarkko Sakkinen 
20796d9d88d0SJarkko Sakkinen 	new_xattr->size = size;
20806d9d88d0SJarkko Sakkinen 	memcpy(new_xattr->value, value, size);
20816d9d88d0SJarkko Sakkinen 	return new_xattr;
20826d9d88d0SJarkko Sakkinen }
20836d9d88d0SJarkko Sakkinen 
20846d9d88d0SJarkko Sakkinen /*
20856d9d88d0SJarkko Sakkinen  * Callback for security_inode_init_security() for acquiring xattrs.
20866d9d88d0SJarkko Sakkinen  */
20876d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode,
20886d9d88d0SJarkko Sakkinen 			    const struct xattr *xattr_array,
20896d9d88d0SJarkko Sakkinen 			    void *fs_info)
20906d9d88d0SJarkko Sakkinen {
20916d9d88d0SJarkko Sakkinen 	struct shmem_inode_info *info = SHMEM_I(inode);
20926d9d88d0SJarkko Sakkinen 	const struct xattr *xattr;
20936d9d88d0SJarkko Sakkinen 	struct shmem_xattr *new_xattr;
20946d9d88d0SJarkko Sakkinen 	size_t len;
20956d9d88d0SJarkko Sakkinen 
20966d9d88d0SJarkko Sakkinen 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
20976d9d88d0SJarkko Sakkinen 		new_xattr = shmem_xattr_alloc(xattr->value, xattr->value_len);
20986d9d88d0SJarkko Sakkinen 		if (!new_xattr)
20996d9d88d0SJarkko Sakkinen 			return -ENOMEM;
21006d9d88d0SJarkko Sakkinen 
21016d9d88d0SJarkko Sakkinen 		len = strlen(xattr->name) + 1;
21026d9d88d0SJarkko Sakkinen 		new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
21036d9d88d0SJarkko Sakkinen 					  GFP_KERNEL);
21046d9d88d0SJarkko Sakkinen 		if (!new_xattr->name) {
21056d9d88d0SJarkko Sakkinen 			kfree(new_xattr);
21066d9d88d0SJarkko Sakkinen 			return -ENOMEM;
21076d9d88d0SJarkko Sakkinen 		}
21086d9d88d0SJarkko Sakkinen 
21096d9d88d0SJarkko Sakkinen 		memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
21106d9d88d0SJarkko Sakkinen 		       XATTR_SECURITY_PREFIX_LEN);
21116d9d88d0SJarkko Sakkinen 		memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
21126d9d88d0SJarkko Sakkinen 		       xattr->name, len);
21136d9d88d0SJarkko Sakkinen 
21146d9d88d0SJarkko Sakkinen 		spin_lock(&info->lock);
21156d9d88d0SJarkko Sakkinen 		list_add(&new_xattr->list, &info->xattr_list);
21166d9d88d0SJarkko Sakkinen 		spin_unlock(&info->lock);
21176d9d88d0SJarkko Sakkinen 	}
21186d9d88d0SJarkko Sakkinen 
21196d9d88d0SJarkko Sakkinen 	return 0;
21206d9d88d0SJarkko Sakkinen }
21216d9d88d0SJarkko Sakkinen 
2122b09e0fa4SEric Paris static int shmem_xattr_get(struct dentry *dentry, const char *name,
2123b09e0fa4SEric Paris 			   void *buffer, size_t size)
2124b09e0fa4SEric Paris {
2125b09e0fa4SEric Paris 	struct shmem_inode_info *info;
2126b09e0fa4SEric Paris 	struct shmem_xattr *xattr;
2127b09e0fa4SEric Paris 	int ret = -ENODATA;
2128b09e0fa4SEric Paris 
2129b09e0fa4SEric Paris 	info = SHMEM_I(dentry->d_inode);
2130b09e0fa4SEric Paris 
2131b09e0fa4SEric Paris 	spin_lock(&info->lock);
2132b09e0fa4SEric Paris 	list_for_each_entry(xattr, &info->xattr_list, list) {
2133b09e0fa4SEric Paris 		if (strcmp(name, xattr->name))
2134b09e0fa4SEric Paris 			continue;
2135b09e0fa4SEric Paris 
2136b09e0fa4SEric Paris 		ret = xattr->size;
2137b09e0fa4SEric Paris 		if (buffer) {
2138b09e0fa4SEric Paris 			if (size < xattr->size)
2139b09e0fa4SEric Paris 				ret = -ERANGE;
2140b09e0fa4SEric Paris 			else
2141b09e0fa4SEric Paris 				memcpy(buffer, xattr->value, xattr->size);
2142b09e0fa4SEric Paris 		}
2143b09e0fa4SEric Paris 		break;
2144b09e0fa4SEric Paris 	}
2145b09e0fa4SEric Paris 	spin_unlock(&info->lock);
2146b09e0fa4SEric Paris 	return ret;
2147b09e0fa4SEric Paris }
2148b09e0fa4SEric Paris 
21496d9d88d0SJarkko Sakkinen static int shmem_xattr_set(struct inode *inode, const char *name,
2150b09e0fa4SEric Paris 			   const void *value, size_t size, int flags)
2151b09e0fa4SEric Paris {
2152b09e0fa4SEric Paris 	struct shmem_inode_info *info = SHMEM_I(inode);
2153b09e0fa4SEric Paris 	struct shmem_xattr *xattr;
2154b09e0fa4SEric Paris 	struct shmem_xattr *new_xattr = NULL;
2155b09e0fa4SEric Paris 	int err = 0;
2156b09e0fa4SEric Paris 
2157b09e0fa4SEric Paris 	/* value == NULL means remove */
2158b09e0fa4SEric Paris 	if (value) {
21596d9d88d0SJarkko Sakkinen 		new_xattr = shmem_xattr_alloc(value, size);
2160b09e0fa4SEric Paris 		if (!new_xattr)
2161b09e0fa4SEric Paris 			return -ENOMEM;
2162b09e0fa4SEric Paris 
2163b09e0fa4SEric Paris 		new_xattr->name = kstrdup(name, GFP_KERNEL);
2164b09e0fa4SEric Paris 		if (!new_xattr->name) {
2165b09e0fa4SEric Paris 			kfree(new_xattr);
2166b09e0fa4SEric Paris 			return -ENOMEM;
2167b09e0fa4SEric Paris 		}
2168b09e0fa4SEric Paris 	}
2169b09e0fa4SEric Paris 
2170b09e0fa4SEric Paris 	spin_lock(&info->lock);
2171b09e0fa4SEric Paris 	list_for_each_entry(xattr, &info->xattr_list, list) {
2172b09e0fa4SEric Paris 		if (!strcmp(name, xattr->name)) {
2173b09e0fa4SEric Paris 			if (flags & XATTR_CREATE) {
2174b09e0fa4SEric Paris 				xattr = new_xattr;
2175b09e0fa4SEric Paris 				err = -EEXIST;
2176b09e0fa4SEric Paris 			} else if (new_xattr) {
2177b09e0fa4SEric Paris 				list_replace(&xattr->list, &new_xattr->list);
2178b09e0fa4SEric Paris 			} else {
2179b09e0fa4SEric Paris 				list_del(&xattr->list);
2180b09e0fa4SEric Paris 			}
2181b09e0fa4SEric Paris 			goto out;
2182b09e0fa4SEric Paris 		}
2183b09e0fa4SEric Paris 	}
2184b09e0fa4SEric Paris 	if (flags & XATTR_REPLACE) {
2185b09e0fa4SEric Paris 		xattr = new_xattr;
2186b09e0fa4SEric Paris 		err = -ENODATA;
2187b09e0fa4SEric Paris 	} else {
2188b09e0fa4SEric Paris 		list_add(&new_xattr->list, &info->xattr_list);
2189b09e0fa4SEric Paris 		xattr = NULL;
2190b09e0fa4SEric Paris 	}
2191b09e0fa4SEric Paris out:
2192b09e0fa4SEric Paris 	spin_unlock(&info->lock);
2193b09e0fa4SEric Paris 	if (xattr)
2194b09e0fa4SEric Paris 		kfree(xattr->name);
2195b09e0fa4SEric Paris 	kfree(xattr);
2196b09e0fa4SEric Paris 	return err;
2197b09e0fa4SEric Paris }
2198b09e0fa4SEric Paris 
2199b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = {
2200b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
2201b09e0fa4SEric Paris 	&generic_acl_access_handler,
2202b09e0fa4SEric Paris 	&generic_acl_default_handler,
2203b09e0fa4SEric Paris #endif
2204b09e0fa4SEric Paris 	NULL
2205b09e0fa4SEric Paris };
2206b09e0fa4SEric Paris 
2207b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name)
2208b09e0fa4SEric Paris {
2209b09e0fa4SEric Paris 	struct { const char *prefix; size_t len; } arr[] = {
2210b09e0fa4SEric Paris 		{ XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
2211b09e0fa4SEric Paris 		{ XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
2212b09e0fa4SEric Paris 	};
2213b09e0fa4SEric Paris 	int i;
2214b09e0fa4SEric Paris 
2215b09e0fa4SEric Paris 	for (i = 0; i < ARRAY_SIZE(arr); i++) {
2216b09e0fa4SEric Paris 		size_t preflen = arr[i].len;
2217b09e0fa4SEric Paris 		if (strncmp(name, arr[i].prefix, preflen) == 0) {
2218b09e0fa4SEric Paris 			if (!name[preflen])
2219b09e0fa4SEric Paris 				return -EINVAL;
2220b09e0fa4SEric Paris 			return 0;
2221b09e0fa4SEric Paris 		}
2222b09e0fa4SEric Paris 	}
2223b09e0fa4SEric Paris 	return -EOPNOTSUPP;
2224b09e0fa4SEric Paris }
2225b09e0fa4SEric Paris 
2226b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
2227b09e0fa4SEric Paris 			      void *buffer, size_t size)
2228b09e0fa4SEric Paris {
2229b09e0fa4SEric Paris 	int err;
2230b09e0fa4SEric Paris 
2231b09e0fa4SEric Paris 	/*
2232b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2233b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2234b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2235b09e0fa4SEric Paris 	 */
2236b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2237b09e0fa4SEric Paris 		return generic_getxattr(dentry, name, buffer, size);
2238b09e0fa4SEric Paris 
2239b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2240b09e0fa4SEric Paris 	if (err)
2241b09e0fa4SEric Paris 		return err;
2242b09e0fa4SEric Paris 
2243b09e0fa4SEric Paris 	return shmem_xattr_get(dentry, name, buffer, size);
2244b09e0fa4SEric Paris }
2245b09e0fa4SEric Paris 
2246b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name,
2247b09e0fa4SEric Paris 			  const void *value, size_t size, int flags)
2248b09e0fa4SEric Paris {
2249b09e0fa4SEric Paris 	int err;
2250b09e0fa4SEric Paris 
2251b09e0fa4SEric Paris 	/*
2252b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2253b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2254b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2255b09e0fa4SEric Paris 	 */
2256b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2257b09e0fa4SEric Paris 		return generic_setxattr(dentry, name, value, size, flags);
2258b09e0fa4SEric Paris 
2259b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2260b09e0fa4SEric Paris 	if (err)
2261b09e0fa4SEric Paris 		return err;
2262b09e0fa4SEric Paris 
2263b09e0fa4SEric Paris 	if (size == 0)
2264b09e0fa4SEric Paris 		value = "";  /* empty EA, do not remove */
2265b09e0fa4SEric Paris 
22666d9d88d0SJarkko Sakkinen 	return shmem_xattr_set(dentry->d_inode, name, value, size, flags);
2267b09e0fa4SEric Paris 
2268b09e0fa4SEric Paris }
2269b09e0fa4SEric Paris 
2270b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name)
2271b09e0fa4SEric Paris {
2272b09e0fa4SEric Paris 	int err;
2273b09e0fa4SEric Paris 
2274b09e0fa4SEric Paris 	/*
2275b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2276b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2277b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2278b09e0fa4SEric Paris 	 */
2279b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2280b09e0fa4SEric Paris 		return generic_removexattr(dentry, name);
2281b09e0fa4SEric Paris 
2282b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2283b09e0fa4SEric Paris 	if (err)
2284b09e0fa4SEric Paris 		return err;
2285b09e0fa4SEric Paris 
22866d9d88d0SJarkko Sakkinen 	return shmem_xattr_set(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
2287b09e0fa4SEric Paris }
2288b09e0fa4SEric Paris 
2289b09e0fa4SEric Paris static bool xattr_is_trusted(const char *name)
2290b09e0fa4SEric Paris {
2291b09e0fa4SEric Paris 	return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
2292b09e0fa4SEric Paris }
2293b09e0fa4SEric Paris 
2294b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2295b09e0fa4SEric Paris {
2296b09e0fa4SEric Paris 	bool trusted = capable(CAP_SYS_ADMIN);
2297b09e0fa4SEric Paris 	struct shmem_xattr *xattr;
2298b09e0fa4SEric Paris 	struct shmem_inode_info *info;
2299b09e0fa4SEric Paris 	size_t used = 0;
2300b09e0fa4SEric Paris 
2301b09e0fa4SEric Paris 	info = SHMEM_I(dentry->d_inode);
2302b09e0fa4SEric Paris 
2303b09e0fa4SEric Paris 	spin_lock(&info->lock);
2304b09e0fa4SEric Paris 	list_for_each_entry(xattr, &info->xattr_list, list) {
2305b09e0fa4SEric Paris 		size_t len;
2306b09e0fa4SEric Paris 
2307b09e0fa4SEric Paris 		/* skip "trusted." attributes for unprivileged callers */
2308b09e0fa4SEric Paris 		if (!trusted && xattr_is_trusted(xattr->name))
2309b09e0fa4SEric Paris 			continue;
2310b09e0fa4SEric Paris 
2311b09e0fa4SEric Paris 		len = strlen(xattr->name) + 1;
2312b09e0fa4SEric Paris 		used += len;
2313b09e0fa4SEric Paris 		if (buffer) {
2314b09e0fa4SEric Paris 			if (size < used) {
2315b09e0fa4SEric Paris 				used = -ERANGE;
2316b09e0fa4SEric Paris 				break;
2317b09e0fa4SEric Paris 			}
2318b09e0fa4SEric Paris 			memcpy(buffer, xattr->name, len);
2319b09e0fa4SEric Paris 			buffer += len;
2320b09e0fa4SEric Paris 		}
2321b09e0fa4SEric Paris 	}
2322b09e0fa4SEric Paris 	spin_unlock(&info->lock);
2323b09e0fa4SEric Paris 
2324b09e0fa4SEric Paris 	return used;
2325b09e0fa4SEric Paris }
2326b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */
2327b09e0fa4SEric Paris 
232869f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = {
23291da177e4SLinus Torvalds 	.readlink	= generic_readlink,
233069f07ec9SHugh Dickins 	.follow_link	= shmem_follow_short_symlink,
2331b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2332b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2333b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2334b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2335b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2336b09e0fa4SEric Paris #endif
23371da177e4SLinus Torvalds };
23381da177e4SLinus Torvalds 
233992e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = {
23401da177e4SLinus Torvalds 	.readlink	= generic_readlink,
23411da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link,
23421da177e4SLinus Torvalds 	.put_link	= shmem_put_link,
2343b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2344b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2345b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2346b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2347b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
234839f0247dSAndreas Gruenbacher #endif
2349b09e0fa4SEric Paris };
235039f0247dSAndreas Gruenbacher 
235191828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child)
235291828a40SDavid M. Grimes {
235391828a40SDavid M. Grimes 	return ERR_PTR(-ESTALE);
235491828a40SDavid M. Grimes }
235591828a40SDavid M. Grimes 
235691828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh)
235791828a40SDavid M. Grimes {
235891828a40SDavid M. Grimes 	__u32 *fh = vfh;
235991828a40SDavid M. Grimes 	__u64 inum = fh[2];
236091828a40SDavid M. Grimes 	inum = (inum << 32) | fh[1];
236191828a40SDavid M. Grimes 	return ino->i_ino == inum && fh[0] == ino->i_generation;
236291828a40SDavid M. Grimes }
236391828a40SDavid M. Grimes 
2364480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2365480b116cSChristoph Hellwig 		struct fid *fid, int fh_len, int fh_type)
236691828a40SDavid M. Grimes {
236791828a40SDavid M. Grimes 	struct inode *inode;
2368480b116cSChristoph Hellwig 	struct dentry *dentry = NULL;
2369480b116cSChristoph Hellwig 	u64 inum = fid->raw[2];
2370480b116cSChristoph Hellwig 	inum = (inum << 32) | fid->raw[1];
237191828a40SDavid M. Grimes 
2372480b116cSChristoph Hellwig 	if (fh_len < 3)
2373480b116cSChristoph Hellwig 		return NULL;
2374480b116cSChristoph Hellwig 
2375480b116cSChristoph Hellwig 	inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2376480b116cSChristoph Hellwig 			shmem_match, fid->raw);
237791828a40SDavid M. Grimes 	if (inode) {
2378480b116cSChristoph Hellwig 		dentry = d_find_alias(inode);
237991828a40SDavid M. Grimes 		iput(inode);
238091828a40SDavid M. Grimes 	}
238191828a40SDavid M. Grimes 
2382480b116cSChristoph Hellwig 	return dentry;
238391828a40SDavid M. Grimes }
238491828a40SDavid M. Grimes 
2385b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
2386b0b0382bSAl Viro 				struct inode *parent)
238791828a40SDavid M. Grimes {
23885fe0c237SAneesh Kumar K.V 	if (*len < 3) {
23895fe0c237SAneesh Kumar K.V 		*len = 3;
239091828a40SDavid M. Grimes 		return 255;
23915fe0c237SAneesh Kumar K.V 	}
239291828a40SDavid M. Grimes 
23931d3382cbSAl Viro 	if (inode_unhashed(inode)) {
239491828a40SDavid M. Grimes 		/* Unfortunately insert_inode_hash is not idempotent,
239591828a40SDavid M. Grimes 		 * so as we hash inodes here rather than at creation
239691828a40SDavid M. Grimes 		 * time, we need a lock to ensure we only try
239791828a40SDavid M. Grimes 		 * to do it once
239891828a40SDavid M. Grimes 		 */
239991828a40SDavid M. Grimes 		static DEFINE_SPINLOCK(lock);
240091828a40SDavid M. Grimes 		spin_lock(&lock);
24011d3382cbSAl Viro 		if (inode_unhashed(inode))
240291828a40SDavid M. Grimes 			__insert_inode_hash(inode,
240391828a40SDavid M. Grimes 					    inode->i_ino + inode->i_generation);
240491828a40SDavid M. Grimes 		spin_unlock(&lock);
240591828a40SDavid M. Grimes 	}
240691828a40SDavid M. Grimes 
240791828a40SDavid M. Grimes 	fh[0] = inode->i_generation;
240891828a40SDavid M. Grimes 	fh[1] = inode->i_ino;
240991828a40SDavid M. Grimes 	fh[2] = ((__u64)inode->i_ino) >> 32;
241091828a40SDavid M. Grimes 
241191828a40SDavid M. Grimes 	*len = 3;
241291828a40SDavid M. Grimes 	return 1;
241391828a40SDavid M. Grimes }
241491828a40SDavid M. Grimes 
241539655164SChristoph Hellwig static const struct export_operations shmem_export_ops = {
241691828a40SDavid M. Grimes 	.get_parent     = shmem_get_parent,
241791828a40SDavid M. Grimes 	.encode_fh      = shmem_encode_fh,
2418480b116cSChristoph Hellwig 	.fh_to_dentry	= shmem_fh_to_dentry,
241991828a40SDavid M. Grimes };
242091828a40SDavid M. Grimes 
2421680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2422680d794bSakpm@linux-foundation.org 			       bool remount)
24231da177e4SLinus Torvalds {
24241da177e4SLinus Torvalds 	char *this_char, *value, *rest;
24258751e039SEric W. Biederman 	uid_t uid;
24268751e039SEric W. Biederman 	gid_t gid;
24271da177e4SLinus Torvalds 
2428b00dc3adSHugh Dickins 	while (options != NULL) {
2429b00dc3adSHugh Dickins 		this_char = options;
2430b00dc3adSHugh Dickins 		for (;;) {
2431b00dc3adSHugh Dickins 			/*
2432b00dc3adSHugh Dickins 			 * NUL-terminate this option: unfortunately,
2433b00dc3adSHugh Dickins 			 * mount options form a comma-separated list,
2434b00dc3adSHugh Dickins 			 * but mpol's nodelist may also contain commas.
2435b00dc3adSHugh Dickins 			 */
2436b00dc3adSHugh Dickins 			options = strchr(options, ',');
2437b00dc3adSHugh Dickins 			if (options == NULL)
2438b00dc3adSHugh Dickins 				break;
2439b00dc3adSHugh Dickins 			options++;
2440b00dc3adSHugh Dickins 			if (!isdigit(*options)) {
2441b00dc3adSHugh Dickins 				options[-1] = '\0';
2442b00dc3adSHugh Dickins 				break;
2443b00dc3adSHugh Dickins 			}
2444b00dc3adSHugh Dickins 		}
24451da177e4SLinus Torvalds 		if (!*this_char)
24461da177e4SLinus Torvalds 			continue;
24471da177e4SLinus Torvalds 		if ((value = strchr(this_char,'=')) != NULL) {
24481da177e4SLinus Torvalds 			*value++ = 0;
24491da177e4SLinus Torvalds 		} else {
24501da177e4SLinus Torvalds 			printk(KERN_ERR
24511da177e4SLinus Torvalds 			    "tmpfs: No value for mount option '%s'\n",
24521da177e4SLinus Torvalds 			    this_char);
24531da177e4SLinus Torvalds 			return 1;
24541da177e4SLinus Torvalds 		}
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds 		if (!strcmp(this_char,"size")) {
24571da177e4SLinus Torvalds 			unsigned long long size;
24581da177e4SLinus Torvalds 			size = memparse(value,&rest);
24591da177e4SLinus Torvalds 			if (*rest == '%') {
24601da177e4SLinus Torvalds 				size <<= PAGE_SHIFT;
24611da177e4SLinus Torvalds 				size *= totalram_pages;
24621da177e4SLinus Torvalds 				do_div(size, 100);
24631da177e4SLinus Torvalds 				rest++;
24641da177e4SLinus Torvalds 			}
24651da177e4SLinus Torvalds 			if (*rest)
24661da177e4SLinus Torvalds 				goto bad_val;
2467680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks =
2468680d794bSakpm@linux-foundation.org 				DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
24691da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_blocks")) {
2470680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks = memparse(value, &rest);
24711da177e4SLinus Torvalds 			if (*rest)
24721da177e4SLinus Torvalds 				goto bad_val;
24731da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_inodes")) {
2474680d794bSakpm@linux-foundation.org 			sbinfo->max_inodes = memparse(value, &rest);
24751da177e4SLinus Torvalds 			if (*rest)
24761da177e4SLinus Torvalds 				goto bad_val;
24771da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"mode")) {
2478680d794bSakpm@linux-foundation.org 			if (remount)
24791da177e4SLinus Torvalds 				continue;
2480680d794bSakpm@linux-foundation.org 			sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
24811da177e4SLinus Torvalds 			if (*rest)
24821da177e4SLinus Torvalds 				goto bad_val;
24831da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"uid")) {
2484680d794bSakpm@linux-foundation.org 			if (remount)
24851da177e4SLinus Torvalds 				continue;
24868751e039SEric W. Biederman 			uid = simple_strtoul(value, &rest, 0);
24871da177e4SLinus Torvalds 			if (*rest)
24881da177e4SLinus Torvalds 				goto bad_val;
24898751e039SEric W. Biederman 			sbinfo->uid = make_kuid(current_user_ns(), uid);
24908751e039SEric W. Biederman 			if (!uid_valid(sbinfo->uid))
24918751e039SEric W. Biederman 				goto bad_val;
24921da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"gid")) {
2493680d794bSakpm@linux-foundation.org 			if (remount)
24941da177e4SLinus Torvalds 				continue;
24958751e039SEric W. Biederman 			gid = simple_strtoul(value, &rest, 0);
24961da177e4SLinus Torvalds 			if (*rest)
24971da177e4SLinus Torvalds 				goto bad_val;
24988751e039SEric W. Biederman 			sbinfo->gid = make_kgid(current_user_ns(), gid);
24998751e039SEric W. Biederman 			if (!gid_valid(sbinfo->gid))
25008751e039SEric W. Biederman 				goto bad_val;
25017339ff83SRobin Holt 		} else if (!strcmp(this_char,"mpol")) {
250271fe804bSLee Schermerhorn 			if (mpol_parse_str(value, &sbinfo->mpol, 1))
25037339ff83SRobin Holt 				goto bad_val;
25041da177e4SLinus Torvalds 		} else {
25051da177e4SLinus Torvalds 			printk(KERN_ERR "tmpfs: Bad mount option %s\n",
25061da177e4SLinus Torvalds 			       this_char);
25071da177e4SLinus Torvalds 			return 1;
25081da177e4SLinus Torvalds 		}
25091da177e4SLinus Torvalds 	}
25101da177e4SLinus Torvalds 	return 0;
25111da177e4SLinus Torvalds 
25121da177e4SLinus Torvalds bad_val:
25131da177e4SLinus Torvalds 	printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
25141da177e4SLinus Torvalds 	       value, this_char);
25151da177e4SLinus Torvalds 	return 1;
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds }
25181da177e4SLinus Torvalds 
25191da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
25201da177e4SLinus Torvalds {
25211da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2522680d794bSakpm@linux-foundation.org 	struct shmem_sb_info config = *sbinfo;
25230edd73b3SHugh Dickins 	unsigned long inodes;
25240edd73b3SHugh Dickins 	int error = -EINVAL;
25251da177e4SLinus Torvalds 
2526680d794bSakpm@linux-foundation.org 	if (shmem_parse_options(data, &config, true))
25270edd73b3SHugh Dickins 		return error;
25280edd73b3SHugh Dickins 
25290edd73b3SHugh Dickins 	spin_lock(&sbinfo->stat_lock);
25300edd73b3SHugh Dickins 	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
25317e496299STim Chen 	if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
25320edd73b3SHugh Dickins 		goto out;
2533680d794bSakpm@linux-foundation.org 	if (config.max_inodes < inodes)
25340edd73b3SHugh Dickins 		goto out;
25350edd73b3SHugh Dickins 	/*
253654af6042SHugh Dickins 	 * Those tests disallow limited->unlimited while any are in use;
25370edd73b3SHugh Dickins 	 * but we must separately disallow unlimited->limited, because
25380edd73b3SHugh Dickins 	 * in that case we have no record of how much is already in use.
25390edd73b3SHugh Dickins 	 */
2540680d794bSakpm@linux-foundation.org 	if (config.max_blocks && !sbinfo->max_blocks)
25410edd73b3SHugh Dickins 		goto out;
2542680d794bSakpm@linux-foundation.org 	if (config.max_inodes && !sbinfo->max_inodes)
25430edd73b3SHugh Dickins 		goto out;
25440edd73b3SHugh Dickins 
25450edd73b3SHugh Dickins 	error = 0;
2546680d794bSakpm@linux-foundation.org 	sbinfo->max_blocks  = config.max_blocks;
2547680d794bSakpm@linux-foundation.org 	sbinfo->max_inodes  = config.max_inodes;
2548680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = config.max_inodes - inodes;
254971fe804bSLee Schermerhorn 
255071fe804bSLee Schermerhorn 	mpol_put(sbinfo->mpol);
255171fe804bSLee Schermerhorn 	sbinfo->mpol        = config.mpol;	/* transfers initial ref */
25520edd73b3SHugh Dickins out:
25530edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
25540edd73b3SHugh Dickins 	return error;
25551da177e4SLinus Torvalds }
2556680d794bSakpm@linux-foundation.org 
255734c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root)
2558680d794bSakpm@linux-foundation.org {
255934c80b1dSAl Viro 	struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
2560680d794bSakpm@linux-foundation.org 
2561680d794bSakpm@linux-foundation.org 	if (sbinfo->max_blocks != shmem_default_max_blocks())
2562680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",size=%luk",
2563680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2564680d794bSakpm@linux-foundation.org 	if (sbinfo->max_inodes != shmem_default_max_inodes())
2565680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2566680d794bSakpm@linux-foundation.org 	if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
256709208d15SAl Viro 		seq_printf(seq, ",mode=%03ho", sbinfo->mode);
25688751e039SEric W. Biederman 	if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
25698751e039SEric W. Biederman 		seq_printf(seq, ",uid=%u",
25708751e039SEric W. Biederman 				from_kuid_munged(&init_user_ns, sbinfo->uid));
25718751e039SEric W. Biederman 	if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
25728751e039SEric W. Biederman 		seq_printf(seq, ",gid=%u",
25738751e039SEric W. Biederman 				from_kgid_munged(&init_user_ns, sbinfo->gid));
257471fe804bSLee Schermerhorn 	shmem_show_mpol(seq, sbinfo->mpol);
2575680d794bSakpm@linux-foundation.org 	return 0;
2576680d794bSakpm@linux-foundation.org }
2577680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
25781da177e4SLinus Torvalds 
25791da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb)
25801da177e4SLinus Torvalds {
2581602586a8SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2582602586a8SHugh Dickins 
2583602586a8SHugh Dickins 	percpu_counter_destroy(&sbinfo->used_blocks);
2584602586a8SHugh Dickins 	kfree(sbinfo);
25851da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
25861da177e4SLinus Torvalds }
25871da177e4SLinus Torvalds 
25882b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent)
25891da177e4SLinus Torvalds {
25901da177e4SLinus Torvalds 	struct inode *inode;
25910edd73b3SHugh Dickins 	struct shmem_sb_info *sbinfo;
2592680d794bSakpm@linux-foundation.org 	int err = -ENOMEM;
2593680d794bSakpm@linux-foundation.org 
2594680d794bSakpm@linux-foundation.org 	/* Round up to L1_CACHE_BYTES to resist false sharing */
2595425fbf04SPekka Enberg 	sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
2596680d794bSakpm@linux-foundation.org 				L1_CACHE_BYTES), GFP_KERNEL);
2597680d794bSakpm@linux-foundation.org 	if (!sbinfo)
2598680d794bSakpm@linux-foundation.org 		return -ENOMEM;
2599680d794bSakpm@linux-foundation.org 
2600680d794bSakpm@linux-foundation.org 	sbinfo->mode = S_IRWXUGO | S_ISVTX;
260176aac0e9SDavid Howells 	sbinfo->uid = current_fsuid();
260276aac0e9SDavid Howells 	sbinfo->gid = current_fsgid();
2603680d794bSakpm@linux-foundation.org 	sb->s_fs_info = sbinfo;
26041da177e4SLinus Torvalds 
26050edd73b3SHugh Dickins #ifdef CONFIG_TMPFS
26061da177e4SLinus Torvalds 	/*
26071da177e4SLinus Torvalds 	 * Per default we only allow half of the physical ram per
26081da177e4SLinus Torvalds 	 * tmpfs instance, limiting inodes to one per page of lowmem;
26091da177e4SLinus Torvalds 	 * but the internal instance is left unlimited.
26101da177e4SLinus Torvalds 	 */
26111da177e4SLinus Torvalds 	if (!(sb->s_flags & MS_NOUSER)) {
2612680d794bSakpm@linux-foundation.org 		sbinfo->max_blocks = shmem_default_max_blocks();
2613680d794bSakpm@linux-foundation.org 		sbinfo->max_inodes = shmem_default_max_inodes();
2614680d794bSakpm@linux-foundation.org 		if (shmem_parse_options(data, sbinfo, false)) {
2615680d794bSakpm@linux-foundation.org 			err = -EINVAL;
2616680d794bSakpm@linux-foundation.org 			goto failed;
2617680d794bSakpm@linux-foundation.org 		}
26181da177e4SLinus Torvalds 	}
261991828a40SDavid M. Grimes 	sb->s_export_op = &shmem_export_ops;
26202f6e38f3SHugh Dickins 	sb->s_flags |= MS_NOSEC;
26210edd73b3SHugh Dickins #else
26220edd73b3SHugh Dickins 	sb->s_flags |= MS_NOUSER;
26230edd73b3SHugh Dickins #endif
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
2626602586a8SHugh Dickins 	if (percpu_counter_init(&sbinfo->used_blocks, 0))
2627602586a8SHugh Dickins 		goto failed;
2628680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = sbinfo->max_inodes;
26291da177e4SLinus Torvalds 
2630285b2c4fSHugh Dickins 	sb->s_maxbytes = MAX_LFS_FILESIZE;
26311da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
26321da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
26331da177e4SLinus Torvalds 	sb->s_magic = TMPFS_MAGIC;
26341da177e4SLinus Torvalds 	sb->s_op = &shmem_ops;
2635cfd95a9cSRobin H. Johnson 	sb->s_time_gran = 1;
2636b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
263739f0247dSAndreas Gruenbacher 	sb->s_xattr = shmem_xattr_handlers;
2638b09e0fa4SEric Paris #endif
2639b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
264039f0247dSAndreas Gruenbacher 	sb->s_flags |= MS_POSIXACL;
264139f0247dSAndreas Gruenbacher #endif
26420edd73b3SHugh Dickins 
2643454abafeSDmitry Monakhov 	inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
26441da177e4SLinus Torvalds 	if (!inode)
26451da177e4SLinus Torvalds 		goto failed;
2646680d794bSakpm@linux-foundation.org 	inode->i_uid = sbinfo->uid;
2647680d794bSakpm@linux-foundation.org 	inode->i_gid = sbinfo->gid;
2648318ceed0SAl Viro 	sb->s_root = d_make_root(inode);
2649318ceed0SAl Viro 	if (!sb->s_root)
265048fde701SAl Viro 		goto failed;
26511da177e4SLinus Torvalds 	return 0;
26521da177e4SLinus Torvalds 
26531da177e4SLinus Torvalds failed:
26541da177e4SLinus Torvalds 	shmem_put_super(sb);
26551da177e4SLinus Torvalds 	return err;
26561da177e4SLinus Torvalds }
26571da177e4SLinus Torvalds 
2658fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep;
26591da177e4SLinus Torvalds 
26601da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb)
26611da177e4SLinus Torvalds {
266241ffe5d5SHugh Dickins 	struct shmem_inode_info *info;
266341ffe5d5SHugh Dickins 	info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
266441ffe5d5SHugh Dickins 	if (!info)
26651da177e4SLinus Torvalds 		return NULL;
266641ffe5d5SHugh Dickins 	return &info->vfs_inode;
26671da177e4SLinus Torvalds }
26681da177e4SLinus Torvalds 
266941ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head)
2670fa0d7e3dSNick Piggin {
2671fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
2672fa0d7e3dSNick Piggin 	kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2673fa0d7e3dSNick Piggin }
2674fa0d7e3dSNick Piggin 
26751da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode)
26761da177e4SLinus Torvalds {
267709208d15SAl Viro 	if (S_ISREG(inode->i_mode))
26781da177e4SLinus Torvalds 		mpol_free_shared_policy(&SHMEM_I(inode)->policy);
267941ffe5d5SHugh Dickins 	call_rcu(&inode->i_rcu, shmem_destroy_callback);
26801da177e4SLinus Torvalds }
26811da177e4SLinus Torvalds 
268241ffe5d5SHugh Dickins static void shmem_init_inode(void *foo)
26831da177e4SLinus Torvalds {
268441ffe5d5SHugh Dickins 	struct shmem_inode_info *info = foo;
268541ffe5d5SHugh Dickins 	inode_init_once(&info->vfs_inode);
26861da177e4SLinus Torvalds }
26871da177e4SLinus Torvalds 
268841ffe5d5SHugh Dickins static int shmem_init_inodecache(void)
26891da177e4SLinus Torvalds {
26901da177e4SLinus Torvalds 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
26911da177e4SLinus Torvalds 				sizeof(struct shmem_inode_info),
269241ffe5d5SHugh Dickins 				0, SLAB_PANIC, shmem_init_inode);
26931da177e4SLinus Torvalds 	return 0;
26941da177e4SLinus Torvalds }
26951da177e4SLinus Torvalds 
269641ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void)
26971da177e4SLinus Torvalds {
26981a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(shmem_inode_cachep);
26991da177e4SLinus Torvalds }
27001da177e4SLinus Torvalds 
2701f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = {
27021da177e4SLinus Torvalds 	.writepage	= shmem_writepage,
270376719325SKen Chen 	.set_page_dirty	= __set_page_dirty_no_writeback,
27041da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
2705800d15a5SNick Piggin 	.write_begin	= shmem_write_begin,
2706800d15a5SNick Piggin 	.write_end	= shmem_write_end,
27071da177e4SLinus Torvalds #endif
2708304dbdb7SLee Schermerhorn 	.migratepage	= migrate_page,
2709aa261f54SAndi Kleen 	.error_remove_page = generic_error_remove_page,
27101da177e4SLinus Torvalds };
27111da177e4SLinus Torvalds 
271215ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = {
27131da177e4SLinus Torvalds 	.mmap		= shmem_mmap,
27141da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
2715f21f8062SHugh Dickins 	.llseek		= generic_file_llseek,
2716bcd78e49SHugh Dickins 	.read		= do_sync_read,
27175402b976SHugh Dickins 	.write		= do_sync_write,
2718bcd78e49SHugh Dickins 	.aio_read	= shmem_file_aio_read,
27195402b976SHugh Dickins 	.aio_write	= generic_file_aio_write,
27201b061d92SChristoph Hellwig 	.fsync		= noop_fsync,
2721708e3508SHugh Dickins 	.splice_read	= shmem_file_splice_read,
2722ae976416SHugh Dickins 	.splice_write	= generic_file_splice_write,
272383e4fa9cSHugh Dickins 	.fallocate	= shmem_fallocate,
27241da177e4SLinus Torvalds #endif
27251da177e4SLinus Torvalds };
27261da177e4SLinus Torvalds 
272792e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = {
272894c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
2729b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2730b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2731b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2732b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2733b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2734b09e0fa4SEric Paris #endif
27351da177e4SLinus Torvalds };
27361da177e4SLinus Torvalds 
273792e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = {
27381da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
27391da177e4SLinus Torvalds 	.create		= shmem_create,
27401da177e4SLinus Torvalds 	.lookup		= simple_lookup,
27411da177e4SLinus Torvalds 	.link		= shmem_link,
27421da177e4SLinus Torvalds 	.unlink		= shmem_unlink,
27431da177e4SLinus Torvalds 	.symlink	= shmem_symlink,
27441da177e4SLinus Torvalds 	.mkdir		= shmem_mkdir,
27451da177e4SLinus Torvalds 	.rmdir		= shmem_rmdir,
27461da177e4SLinus Torvalds 	.mknod		= shmem_mknod,
27471da177e4SLinus Torvalds 	.rename		= shmem_rename,
27481da177e4SLinus Torvalds #endif
2749b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2750b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2751b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2752b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2753b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2754b09e0fa4SEric Paris #endif
275539f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
275694c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
275739f0247dSAndreas Gruenbacher #endif
275839f0247dSAndreas Gruenbacher };
275939f0247dSAndreas Gruenbacher 
276092e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = {
2761b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2762b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2763b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2764b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2765b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2766b09e0fa4SEric Paris #endif
276739f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
276894c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
276939f0247dSAndreas Gruenbacher #endif
27701da177e4SLinus Torvalds };
27711da177e4SLinus Torvalds 
2772759b9775SHugh Dickins static const struct super_operations shmem_ops = {
27731da177e4SLinus Torvalds 	.alloc_inode	= shmem_alloc_inode,
27741da177e4SLinus Torvalds 	.destroy_inode	= shmem_destroy_inode,
27751da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
27761da177e4SLinus Torvalds 	.statfs		= shmem_statfs,
27771da177e4SLinus Torvalds 	.remount_fs	= shmem_remount_fs,
2778680d794bSakpm@linux-foundation.org 	.show_options	= shmem_show_options,
27791da177e4SLinus Torvalds #endif
27801f895f75SAl Viro 	.evict_inode	= shmem_evict_inode,
27811da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
27821da177e4SLinus Torvalds 	.put_super	= shmem_put_super,
27831da177e4SLinus Torvalds };
27841da177e4SLinus Torvalds 
2785f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = {
278654cb8821SNick Piggin 	.fault		= shmem_fault,
27871da177e4SLinus Torvalds #ifdef CONFIG_NUMA
27881da177e4SLinus Torvalds 	.set_policy     = shmem_set_policy,
27891da177e4SLinus Torvalds 	.get_policy     = shmem_get_policy,
27901da177e4SLinus Torvalds #endif
27911da177e4SLinus Torvalds };
27921da177e4SLinus Torvalds 
27933c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type,
27943c26ff6eSAl Viro 	int flags, const char *dev_name, void *data)
27951da177e4SLinus Torvalds {
27963c26ff6eSAl Viro 	return mount_nodev(fs_type, flags, data, shmem_fill_super);
27971da177e4SLinus Torvalds }
27981da177e4SLinus Torvalds 
279941ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = {
28001da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
28011da177e4SLinus Torvalds 	.name		= "tmpfs",
28023c26ff6eSAl Viro 	.mount		= shmem_mount,
28031da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
28041da177e4SLinus Torvalds };
28051da177e4SLinus Torvalds 
280641ffe5d5SHugh Dickins int __init shmem_init(void)
28071da177e4SLinus Torvalds {
28081da177e4SLinus Torvalds 	int error;
28091da177e4SLinus Torvalds 
2810e0bf68ddSPeter Zijlstra 	error = bdi_init(&shmem_backing_dev_info);
2811e0bf68ddSPeter Zijlstra 	if (error)
2812e0bf68ddSPeter Zijlstra 		goto out4;
2813e0bf68ddSPeter Zijlstra 
281441ffe5d5SHugh Dickins 	error = shmem_init_inodecache();
28151da177e4SLinus Torvalds 	if (error)
28161da177e4SLinus Torvalds 		goto out3;
28171da177e4SLinus Torvalds 
281841ffe5d5SHugh Dickins 	error = register_filesystem(&shmem_fs_type);
28191da177e4SLinus Torvalds 	if (error) {
28201da177e4SLinus Torvalds 		printk(KERN_ERR "Could not register tmpfs\n");
28211da177e4SLinus Torvalds 		goto out2;
28221da177e4SLinus Torvalds 	}
282395dc112aSGreg Kroah-Hartman 
282441ffe5d5SHugh Dickins 	shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
282541ffe5d5SHugh Dickins 				 shmem_fs_type.name, NULL);
28261da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt)) {
28271da177e4SLinus Torvalds 		error = PTR_ERR(shm_mnt);
28281da177e4SLinus Torvalds 		printk(KERN_ERR "Could not kern_mount tmpfs\n");
28291da177e4SLinus Torvalds 		goto out1;
28301da177e4SLinus Torvalds 	}
28311da177e4SLinus Torvalds 	return 0;
28321da177e4SLinus Torvalds 
28331da177e4SLinus Torvalds out1:
283441ffe5d5SHugh Dickins 	unregister_filesystem(&shmem_fs_type);
28351da177e4SLinus Torvalds out2:
283641ffe5d5SHugh Dickins 	shmem_destroy_inodecache();
28371da177e4SLinus Torvalds out3:
2838e0bf68ddSPeter Zijlstra 	bdi_destroy(&shmem_backing_dev_info);
2839e0bf68ddSPeter Zijlstra out4:
28401da177e4SLinus Torvalds 	shm_mnt = ERR_PTR(error);
28411da177e4SLinus Torvalds 	return error;
28421da177e4SLinus Torvalds }
2843853ac43aSMatt Mackall 
2844853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */
2845853ac43aSMatt Mackall 
2846853ac43aSMatt Mackall /*
2847853ac43aSMatt Mackall  * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2848853ac43aSMatt Mackall  *
2849853ac43aSMatt Mackall  * This is intended for small system where the benefits of the full
2850853ac43aSMatt Mackall  * shmem code (swap-backed and resource-limited) are outweighed by
2851853ac43aSMatt Mackall  * their complexity. On systems without swap this code should be
2852853ac43aSMatt Mackall  * effectively equivalent, but much lighter weight.
2853853ac43aSMatt Mackall  */
2854853ac43aSMatt Mackall 
2855853ac43aSMatt Mackall #include <linux/ramfs.h>
2856853ac43aSMatt Mackall 
285741ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = {
2858853ac43aSMatt Mackall 	.name		= "tmpfs",
28593c26ff6eSAl Viro 	.mount		= ramfs_mount,
2860853ac43aSMatt Mackall 	.kill_sb	= kill_litter_super,
2861853ac43aSMatt Mackall };
2862853ac43aSMatt Mackall 
286341ffe5d5SHugh Dickins int __init shmem_init(void)
2864853ac43aSMatt Mackall {
286541ffe5d5SHugh Dickins 	BUG_ON(register_filesystem(&shmem_fs_type) != 0);
2866853ac43aSMatt Mackall 
286741ffe5d5SHugh Dickins 	shm_mnt = kern_mount(&shmem_fs_type);
2868853ac43aSMatt Mackall 	BUG_ON(IS_ERR(shm_mnt));
2869853ac43aSMatt Mackall 
2870853ac43aSMatt Mackall 	return 0;
2871853ac43aSMatt Mackall }
2872853ac43aSMatt Mackall 
287341ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page)
2874853ac43aSMatt Mackall {
2875853ac43aSMatt Mackall 	return 0;
2876853ac43aSMatt Mackall }
2877853ac43aSMatt Mackall 
28783f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user)
28793f96b79aSHugh Dickins {
28803f96b79aSHugh Dickins 	return 0;
28813f96b79aSHugh Dickins }
28823f96b79aSHugh Dickins 
288324513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping)
288424513264SHugh Dickins {
288524513264SHugh Dickins }
288624513264SHugh Dickins 
288741ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
288894c1e62dSHugh Dickins {
288941ffe5d5SHugh Dickins 	truncate_inode_pages_range(inode->i_mapping, lstart, lend);
289094c1e62dSHugh Dickins }
289194c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
289294c1e62dSHugh Dickins 
2893853ac43aSMatt Mackall #define shmem_vm_ops				generic_file_vm_ops
28940b0a0806SHugh Dickins #define shmem_file_operations			ramfs_file_operations
2895454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags)	ramfs_get_inode(sb, dir, mode, dev)
28960b0a0806SHugh Dickins #define shmem_acct_size(flags, size)		0
28970b0a0806SHugh Dickins #define shmem_unacct_size(flags, size)		do {} while (0)
2898853ac43aSMatt Mackall 
2899853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */
2900853ac43aSMatt Mackall 
2901853ac43aSMatt Mackall /* common code */
29021da177e4SLinus Torvalds 
290346711810SRandy Dunlap /**
29041da177e4SLinus Torvalds  * shmem_file_setup - get an unlinked file living in tmpfs
29051da177e4SLinus Torvalds  * @name: name for dentry (to be seen in /proc/<pid>/maps
29061da177e4SLinus Torvalds  * @size: size to be set for the file
29070b0a0806SHugh Dickins  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
29081da177e4SLinus Torvalds  */
2909168f5ac6SSergei Trofimovich struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
29101da177e4SLinus Torvalds {
29111da177e4SLinus Torvalds 	int error;
29121da177e4SLinus Torvalds 	struct file *file;
29131da177e4SLinus Torvalds 	struct inode *inode;
29142c48b9c4SAl Viro 	struct path path;
29152c48b9c4SAl Viro 	struct dentry *root;
29161da177e4SLinus Torvalds 	struct qstr this;
29171da177e4SLinus Torvalds 
29181da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt))
29191da177e4SLinus Torvalds 		return (void *)shm_mnt;
29201da177e4SLinus Torvalds 
2921285b2c4fSHugh Dickins 	if (size < 0 || size > MAX_LFS_FILESIZE)
29221da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
29231da177e4SLinus Torvalds 
29241da177e4SLinus Torvalds 	if (shmem_acct_size(flags, size))
29251da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
29261da177e4SLinus Torvalds 
29271da177e4SLinus Torvalds 	error = -ENOMEM;
29281da177e4SLinus Torvalds 	this.name = name;
29291da177e4SLinus Torvalds 	this.len = strlen(name);
29301da177e4SLinus Torvalds 	this.hash = 0; /* will go */
29311da177e4SLinus Torvalds 	root = shm_mnt->mnt_root;
29322c48b9c4SAl Viro 	path.dentry = d_alloc(root, &this);
29332c48b9c4SAl Viro 	if (!path.dentry)
29341da177e4SLinus Torvalds 		goto put_memory;
29352c48b9c4SAl Viro 	path.mnt = mntget(shm_mnt);
29361da177e4SLinus Torvalds 
29371da177e4SLinus Torvalds 	error = -ENOSPC;
2938454abafeSDmitry Monakhov 	inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
29391da177e4SLinus Torvalds 	if (!inode)
29404b42af81SAl Viro 		goto put_dentry;
29411da177e4SLinus Torvalds 
29422c48b9c4SAl Viro 	d_instantiate(path.dentry, inode);
29431da177e4SLinus Torvalds 	inode->i_size = size;
29446d6b77f1SMiklos Szeredi 	clear_nlink(inode);	/* It is unlinked */
2945853ac43aSMatt Mackall #ifndef CONFIG_MMU
2946853ac43aSMatt Mackall 	error = ramfs_nommu_expand_for_mapping(inode, size);
2947853ac43aSMatt Mackall 	if (error)
29484b42af81SAl Viro 		goto put_dentry;
2949853ac43aSMatt Mackall #endif
29504b42af81SAl Viro 
29514b42af81SAl Viro 	error = -ENFILE;
29522c48b9c4SAl Viro 	file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
29534b42af81SAl Viro 		  &shmem_file_operations);
29544b42af81SAl Viro 	if (!file)
29554b42af81SAl Viro 		goto put_dentry;
29564b42af81SAl Viro 
29571da177e4SLinus Torvalds 	return file;
29581da177e4SLinus Torvalds 
29591da177e4SLinus Torvalds put_dentry:
29602c48b9c4SAl Viro 	path_put(&path);
29611da177e4SLinus Torvalds put_memory:
29621da177e4SLinus Torvalds 	shmem_unacct_size(flags, size);
29631da177e4SLinus Torvalds 	return ERR_PTR(error);
29641da177e4SLinus Torvalds }
2965395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup);
29661da177e4SLinus Torvalds 
296746711810SRandy Dunlap /**
29681da177e4SLinus Torvalds  * shmem_zero_setup - setup a shared anonymous mapping
29691da177e4SLinus Torvalds  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
29701da177e4SLinus Torvalds  */
29711da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma)
29721da177e4SLinus Torvalds {
29731da177e4SLinus Torvalds 	struct file *file;
29741da177e4SLinus Torvalds 	loff_t size = vma->vm_end - vma->vm_start;
29751da177e4SLinus Torvalds 
29761da177e4SLinus Torvalds 	file = shmem_file_setup("dev/zero", size, vma->vm_flags);
29771da177e4SLinus Torvalds 	if (IS_ERR(file))
29781da177e4SLinus Torvalds 		return PTR_ERR(file);
29791da177e4SLinus Torvalds 
29801da177e4SLinus Torvalds 	if (vma->vm_file)
29811da177e4SLinus Torvalds 		fput(vma->vm_file);
29821da177e4SLinus Torvalds 	vma->vm_file = file;
29831da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
2984bee4c36aSHugh Dickins 	vma->vm_flags |= VM_CAN_NONLINEAR;
29851da177e4SLinus Torvalds 	return 0;
29861da177e4SLinus Torvalds }
2987d9d90e5eSHugh Dickins 
2988d9d90e5eSHugh Dickins /**
2989d9d90e5eSHugh Dickins  * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2990d9d90e5eSHugh Dickins  * @mapping:	the page's address_space
2991d9d90e5eSHugh Dickins  * @index:	the page index
2992d9d90e5eSHugh Dickins  * @gfp:	the page allocator flags to use if allocating
2993d9d90e5eSHugh Dickins  *
2994d9d90e5eSHugh Dickins  * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2995d9d90e5eSHugh Dickins  * with any new page allocations done using the specified allocation flags.
2996d9d90e5eSHugh Dickins  * But read_cache_page_gfp() uses the ->readpage() method: which does not
2997d9d90e5eSHugh Dickins  * suit tmpfs, since it may have pages in swapcache, and needs to find those
2998d9d90e5eSHugh Dickins  * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2999d9d90e5eSHugh Dickins  *
300068da9f05SHugh Dickins  * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
300168da9f05SHugh Dickins  * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
3002d9d90e5eSHugh Dickins  */
3003d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
3004d9d90e5eSHugh Dickins 					 pgoff_t index, gfp_t gfp)
3005d9d90e5eSHugh Dickins {
300668da9f05SHugh Dickins #ifdef CONFIG_SHMEM
300768da9f05SHugh Dickins 	struct inode *inode = mapping->host;
30089276aad6SHugh Dickins 	struct page *page;
300968da9f05SHugh Dickins 	int error;
301068da9f05SHugh Dickins 
301168da9f05SHugh Dickins 	BUG_ON(mapping->a_ops != &shmem_aops);
301268da9f05SHugh Dickins 	error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
301368da9f05SHugh Dickins 	if (error)
301468da9f05SHugh Dickins 		page = ERR_PTR(error);
301568da9f05SHugh Dickins 	else
301668da9f05SHugh Dickins 		unlock_page(page);
301768da9f05SHugh Dickins 	return page;
301868da9f05SHugh Dickins #else
301968da9f05SHugh Dickins 	/*
302068da9f05SHugh Dickins 	 * The tiny !SHMEM case uses ramfs without swap
302168da9f05SHugh Dickins 	 */
3022d9d90e5eSHugh Dickins 	return read_cache_page_gfp(mapping, index, gfp);
302368da9f05SHugh Dickins #endif
3024d9d90e5eSHugh Dickins }
3025d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);
3026