xref: /openbmc/linux/mm/shmem.c (revision b0b0382bb4904965a9e9fca77ad87514dfda0d1c)
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>
56708e3508SHugh Dickins #include <linux/splice.h>
571da177e4SLinus Torvalds #include <linux/security.h>
581da177e4SLinus Torvalds #include <linux/swapops.h>
591da177e4SLinus Torvalds #include <linux/mempolicy.h>
601da177e4SLinus Torvalds #include <linux/namei.h>
61b00dc3adSHugh Dickins #include <linux/ctype.h>
62304dbdb7SLee Schermerhorn #include <linux/migrate.h>
63c1f60a5aSChristoph Lameter #include <linux/highmem.h>
64680d794bSakpm@linux-foundation.org #include <linux/seq_file.h>
6592562927SMimi Zohar #include <linux/magic.h>
66304dbdb7SLee Schermerhorn 
671da177e4SLinus Torvalds #include <asm/uaccess.h>
681da177e4SLinus Torvalds #include <asm/pgtable.h>
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
711da177e4SLinus Torvalds #define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */
741da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20
751da177e4SLinus Torvalds 
7669f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
7769f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128
7869f07ec9SHugh Dickins 
79b09e0fa4SEric Paris struct shmem_xattr {
80b09e0fa4SEric Paris 	struct list_head list;	/* anchored by shmem_inode_info->xattr_list */
81b09e0fa4SEric Paris 	char *name;		/* xattr name */
82b09e0fa4SEric Paris 	size_t size;
83b09e0fa4SEric Paris 	char value[0];
84b09e0fa4SEric Paris };
85b09e0fa4SEric Paris 
86285b2c4fSHugh Dickins /* Flag allocation requirements to shmem_getpage */
871da177e4SLinus Torvalds enum sgp_type {
881da177e4SLinus Torvalds 	SGP_READ,	/* don't exceed i_size, don't allocate page */
891da177e4SLinus Torvalds 	SGP_CACHE,	/* don't exceed i_size, may allocate page */
90a0ee5ec5SHugh Dickins 	SGP_DIRTY,	/* like SGP_CACHE, but set new page dirty */
911da177e4SLinus Torvalds 	SGP_WRITE,	/* may exceed i_size, may allocate page */
921da177e4SLinus Torvalds };
931da177e4SLinus Torvalds 
94b76db735SAndrew Morton #ifdef CONFIG_TMPFS
95680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void)
96680d794bSakpm@linux-foundation.org {
97680d794bSakpm@linux-foundation.org 	return totalram_pages / 2;
98680d794bSakpm@linux-foundation.org }
99680d794bSakpm@linux-foundation.org 
100680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void)
101680d794bSakpm@linux-foundation.org {
102680d794bSakpm@linux-foundation.org 	return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
103680d794bSakpm@linux-foundation.org }
104b76db735SAndrew Morton #endif
105680d794bSakpm@linux-foundation.org 
10668da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
10768da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
10868da9f05SHugh Dickins 
10968da9f05SHugh Dickins static inline int shmem_getpage(struct inode *inode, pgoff_t index,
11068da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, int *fault_type)
11168da9f05SHugh Dickins {
11268da9f05SHugh Dickins 	return shmem_getpage_gfp(inode, index, pagep, sgp,
11368da9f05SHugh Dickins 			mapping_gfp_mask(inode->i_mapping), fault_type);
11468da9f05SHugh Dickins }
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	return sb->s_fs_info;
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds /*
1221da177e4SLinus Torvalds  * shmem_file_setup pre-accounts the whole fixed size of a VM object,
1231da177e4SLinus Torvalds  * for shared memory and for shared anonymous (/dev/zero) mappings
1241da177e4SLinus Torvalds  * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
1251da177e4SLinus Torvalds  * consistent with the pre-accounting of private mappings ...
1261da177e4SLinus Torvalds  */
1271da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size)
1281da177e4SLinus Torvalds {
1290b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
130191c5424SAl Viro 		0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size)
1341da177e4SLinus Torvalds {
1350b0a0806SHugh Dickins 	if (!(flags & VM_NORESERVE))
1361da177e4SLinus Torvalds 		vm_unacct_memory(VM_ACCT(size));
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds  * ... whereas tmpfs objects are accounted incrementally as
1411da177e4SLinus Torvalds  * pages are allocated, in order to allow huge sparse files.
1421da177e4SLinus Torvalds  * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
1431da177e4SLinus Torvalds  * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
1441da177e4SLinus Torvalds  */
1451da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags)
1461da177e4SLinus Torvalds {
1470b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
148191c5424SAl Viro 		security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages)
1521da177e4SLinus Torvalds {
1530b0a0806SHugh Dickins 	if (flags & VM_NORESERVE)
1541da177e4SLinus Torvalds 		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
157759b9775SHugh Dickins static const struct super_operations shmem_ops;
158f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops;
15915ad7cdcSHelge Deller static const struct file_operations shmem_file_operations;
16092e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations;
16192e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations;
16292e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations;
163f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops;
1641da177e4SLinus Torvalds 
1656c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info  __read_mostly = {
1661da177e4SLinus Torvalds 	.ra_pages	= 0,	/* No readahead */
1674f98a2feSRik van Riel 	.capabilities	= BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
1681da177e4SLinus Torvalds };
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist);
171cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex);
1721da177e4SLinus Torvalds 
1735b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb)
1745b04c689SPavel Emelyanov {
1755b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1765b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
1775b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
1785b04c689SPavel Emelyanov 		if (!sbinfo->free_inodes) {
1795b04c689SPavel Emelyanov 			spin_unlock(&sbinfo->stat_lock);
1805b04c689SPavel Emelyanov 			return -ENOSPC;
1815b04c689SPavel Emelyanov 		}
1825b04c689SPavel Emelyanov 		sbinfo->free_inodes--;
1835b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
1845b04c689SPavel Emelyanov 	}
1855b04c689SPavel Emelyanov 	return 0;
1865b04c689SPavel Emelyanov }
1875b04c689SPavel Emelyanov 
1885b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb)
1895b04c689SPavel Emelyanov {
1905b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1915b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
1925b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
1935b04c689SPavel Emelyanov 		sbinfo->free_inodes++;
1945b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
1955b04c689SPavel Emelyanov 	}
1965b04c689SPavel Emelyanov }
1975b04c689SPavel Emelyanov 
19846711810SRandy Dunlap /**
19941ffe5d5SHugh Dickins  * shmem_recalc_inode - recalculate the block usage of an inode
2001da177e4SLinus Torvalds  * @inode: inode to recalc
2011da177e4SLinus Torvalds  *
2021da177e4SLinus Torvalds  * We have to calculate the free blocks since the mm can drop
2031da177e4SLinus Torvalds  * undirtied hole pages behind our back.
2041da177e4SLinus Torvalds  *
2051da177e4SLinus Torvalds  * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
2061da177e4SLinus Torvalds  * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
2071da177e4SLinus Torvalds  *
2081da177e4SLinus Torvalds  * It has to be called with the spinlock held.
2091da177e4SLinus Torvalds  */
2101da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
2131da177e4SLinus Torvalds 	long freed;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
2161da177e4SLinus Torvalds 	if (freed > 0) {
21754af6042SHugh Dickins 		struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
21854af6042SHugh Dickins 		if (sbinfo->max_blocks)
21954af6042SHugh Dickins 			percpu_counter_add(&sbinfo->used_blocks, -freed);
2201da177e4SLinus Torvalds 		info->alloced -= freed;
22154af6042SHugh Dickins 		inode->i_blocks -= freed * BLOCKS_PER_PAGE;
2221da177e4SLinus Torvalds 		shmem_unacct_blocks(info->flags, freed);
2231da177e4SLinus Torvalds 	}
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
2267a5d0fbbSHugh Dickins /*
2277a5d0fbbSHugh Dickins  * Replace item expected in radix tree by a new item, while holding tree lock.
2287a5d0fbbSHugh Dickins  */
2297a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping,
2307a5d0fbbSHugh Dickins 			pgoff_t index, void *expected, void *replacement)
2317a5d0fbbSHugh Dickins {
2327a5d0fbbSHugh Dickins 	void **pslot;
2337a5d0fbbSHugh Dickins 	void *item = NULL;
2347a5d0fbbSHugh Dickins 
2357a5d0fbbSHugh Dickins 	VM_BUG_ON(!expected);
2367a5d0fbbSHugh Dickins 	pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
2377a5d0fbbSHugh Dickins 	if (pslot)
2387a5d0fbbSHugh Dickins 		item = radix_tree_deref_slot_protected(pslot,
2397a5d0fbbSHugh Dickins 							&mapping->tree_lock);
2407a5d0fbbSHugh Dickins 	if (item != expected)
2417a5d0fbbSHugh Dickins 		return -ENOENT;
2427a5d0fbbSHugh Dickins 	if (replacement)
2437a5d0fbbSHugh Dickins 		radix_tree_replace_slot(pslot, replacement);
2447a5d0fbbSHugh Dickins 	else
2457a5d0fbbSHugh Dickins 		radix_tree_delete(&mapping->page_tree, index);
2467a5d0fbbSHugh Dickins 	return 0;
2477a5d0fbbSHugh Dickins }
2487a5d0fbbSHugh Dickins 
2497a5d0fbbSHugh Dickins /*
25046f65ec1SHugh Dickins  * Like add_to_page_cache_locked, but error if expected item has gone.
25146f65ec1SHugh Dickins  */
25246f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page,
25346f65ec1SHugh Dickins 				   struct address_space *mapping,
25446f65ec1SHugh Dickins 				   pgoff_t index, gfp_t gfp, void *expected)
25546f65ec1SHugh Dickins {
256aa3b1895SHugh Dickins 	int error = 0;
25746f65ec1SHugh Dickins 
25846f65ec1SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
25946f65ec1SHugh Dickins 	VM_BUG_ON(!PageSwapBacked(page));
26046f65ec1SHugh Dickins 
26146f65ec1SHugh Dickins 	if (!expected)
26246f65ec1SHugh Dickins 		error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
26346f65ec1SHugh Dickins 	if (!error) {
26446f65ec1SHugh Dickins 		page_cache_get(page);
26546f65ec1SHugh Dickins 		page->mapping = mapping;
26646f65ec1SHugh Dickins 		page->index = index;
26746f65ec1SHugh Dickins 
26846f65ec1SHugh Dickins 		spin_lock_irq(&mapping->tree_lock);
26946f65ec1SHugh Dickins 		if (!expected)
27046f65ec1SHugh Dickins 			error = radix_tree_insert(&mapping->page_tree,
27146f65ec1SHugh Dickins 							index, page);
27246f65ec1SHugh Dickins 		else
27346f65ec1SHugh Dickins 			error = shmem_radix_tree_replace(mapping, index,
27446f65ec1SHugh Dickins 							expected, page);
27546f65ec1SHugh Dickins 		if (!error) {
27646f65ec1SHugh Dickins 			mapping->nrpages++;
27746f65ec1SHugh Dickins 			__inc_zone_page_state(page, NR_FILE_PAGES);
27846f65ec1SHugh Dickins 			__inc_zone_page_state(page, NR_SHMEM);
27946f65ec1SHugh Dickins 			spin_unlock_irq(&mapping->tree_lock);
28046f65ec1SHugh Dickins 		} else {
28146f65ec1SHugh Dickins 			page->mapping = NULL;
28246f65ec1SHugh Dickins 			spin_unlock_irq(&mapping->tree_lock);
28346f65ec1SHugh Dickins 			page_cache_release(page);
28446f65ec1SHugh Dickins 		}
28546f65ec1SHugh Dickins 		if (!expected)
28646f65ec1SHugh Dickins 			radix_tree_preload_end();
28746f65ec1SHugh Dickins 	}
28846f65ec1SHugh Dickins 	if (error)
28946f65ec1SHugh Dickins 		mem_cgroup_uncharge_cache_page(page);
29046f65ec1SHugh Dickins 	return error;
29146f65ec1SHugh Dickins }
29246f65ec1SHugh Dickins 
29346f65ec1SHugh Dickins /*
2946922c0c7SHugh Dickins  * Like delete_from_page_cache, but substitutes swap for page.
2956922c0c7SHugh Dickins  */
2966922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap)
2976922c0c7SHugh Dickins {
2986922c0c7SHugh Dickins 	struct address_space *mapping = page->mapping;
2996922c0c7SHugh Dickins 	int error;
3006922c0c7SHugh Dickins 
3016922c0c7SHugh Dickins 	spin_lock_irq(&mapping->tree_lock);
3026922c0c7SHugh Dickins 	error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
3036922c0c7SHugh Dickins 	page->mapping = NULL;
3046922c0c7SHugh Dickins 	mapping->nrpages--;
3056922c0c7SHugh Dickins 	__dec_zone_page_state(page, NR_FILE_PAGES);
3066922c0c7SHugh Dickins 	__dec_zone_page_state(page, NR_SHMEM);
3076922c0c7SHugh Dickins 	spin_unlock_irq(&mapping->tree_lock);
3086922c0c7SHugh Dickins 	page_cache_release(page);
3096922c0c7SHugh Dickins 	BUG_ON(error);
3106922c0c7SHugh Dickins }
3116922c0c7SHugh Dickins 
3126922c0c7SHugh Dickins /*
3137a5d0fbbSHugh Dickins  * Like find_get_pages, but collecting swap entries as well as pages.
3147a5d0fbbSHugh Dickins  */
3157a5d0fbbSHugh Dickins static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
3167a5d0fbbSHugh Dickins 					pgoff_t start, unsigned int nr_pages,
3177a5d0fbbSHugh Dickins 					struct page **pages, pgoff_t *indices)
3187a5d0fbbSHugh Dickins {
3197a5d0fbbSHugh Dickins 	unsigned int i;
3207a5d0fbbSHugh Dickins 	unsigned int ret;
3217a5d0fbbSHugh Dickins 	unsigned int nr_found;
3227a5d0fbbSHugh Dickins 
3237a5d0fbbSHugh Dickins 	rcu_read_lock();
3247a5d0fbbSHugh Dickins restart:
3257a5d0fbbSHugh Dickins 	nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
3267a5d0fbbSHugh Dickins 				(void ***)pages, indices, start, nr_pages);
3277a5d0fbbSHugh Dickins 	ret = 0;
3287a5d0fbbSHugh Dickins 	for (i = 0; i < nr_found; i++) {
3297a5d0fbbSHugh Dickins 		struct page *page;
3307a5d0fbbSHugh Dickins repeat:
3317a5d0fbbSHugh Dickins 		page = radix_tree_deref_slot((void **)pages[i]);
3327a5d0fbbSHugh Dickins 		if (unlikely(!page))
3337a5d0fbbSHugh Dickins 			continue;
3347a5d0fbbSHugh Dickins 		if (radix_tree_exception(page)) {
3358079b1c8SHugh Dickins 			if (radix_tree_deref_retry(page))
3367a5d0fbbSHugh Dickins 				goto restart;
3378079b1c8SHugh Dickins 			/*
3388079b1c8SHugh Dickins 			 * Otherwise, we must be storing a swap entry
3398079b1c8SHugh Dickins 			 * here as an exceptional entry: so return it
3408079b1c8SHugh Dickins 			 * without attempting to raise page count.
3418079b1c8SHugh Dickins 			 */
3428079b1c8SHugh Dickins 			goto export;
3437a5d0fbbSHugh Dickins 		}
3447a5d0fbbSHugh Dickins 		if (!page_cache_get_speculative(page))
3457a5d0fbbSHugh Dickins 			goto repeat;
3467a5d0fbbSHugh Dickins 
3477a5d0fbbSHugh Dickins 		/* Has the page moved? */
3487a5d0fbbSHugh Dickins 		if (unlikely(page != *((void **)pages[i]))) {
3497a5d0fbbSHugh Dickins 			page_cache_release(page);
3507a5d0fbbSHugh Dickins 			goto repeat;
3517a5d0fbbSHugh Dickins 		}
3527a5d0fbbSHugh Dickins export:
3537a5d0fbbSHugh Dickins 		indices[ret] = indices[i];
3547a5d0fbbSHugh Dickins 		pages[ret] = page;
3557a5d0fbbSHugh Dickins 		ret++;
3567a5d0fbbSHugh Dickins 	}
3577a5d0fbbSHugh Dickins 	if (unlikely(!ret && nr_found))
3587a5d0fbbSHugh Dickins 		goto restart;
3597a5d0fbbSHugh Dickins 	rcu_read_unlock();
3607a5d0fbbSHugh Dickins 	return ret;
3617a5d0fbbSHugh Dickins }
3627a5d0fbbSHugh Dickins 
3637a5d0fbbSHugh Dickins /*
3647a5d0fbbSHugh Dickins  * Remove swap entry from radix tree, free the swap and its page cache.
3657a5d0fbbSHugh Dickins  */
3667a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping,
3677a5d0fbbSHugh Dickins 			   pgoff_t index, void *radswap)
3687a5d0fbbSHugh Dickins {
3697a5d0fbbSHugh Dickins 	int error;
3707a5d0fbbSHugh Dickins 
3717a5d0fbbSHugh Dickins 	spin_lock_irq(&mapping->tree_lock);
3727a5d0fbbSHugh Dickins 	error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
3737a5d0fbbSHugh Dickins 	spin_unlock_irq(&mapping->tree_lock);
3747a5d0fbbSHugh Dickins 	if (!error)
3757a5d0fbbSHugh Dickins 		free_swap_and_cache(radix_to_swp_entry(radswap));
3767a5d0fbbSHugh Dickins 	return error;
3777a5d0fbbSHugh Dickins }
3787a5d0fbbSHugh Dickins 
3797a5d0fbbSHugh Dickins /*
3807a5d0fbbSHugh Dickins  * Pagevec may contain swap entries, so shuffle up pages before releasing.
3817a5d0fbbSHugh Dickins  */
38224513264SHugh Dickins static void shmem_deswap_pagevec(struct pagevec *pvec)
3837a5d0fbbSHugh Dickins {
3847a5d0fbbSHugh Dickins 	int i, j;
3857a5d0fbbSHugh Dickins 
3867a5d0fbbSHugh Dickins 	for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
3877a5d0fbbSHugh Dickins 		struct page *page = pvec->pages[i];
3887a5d0fbbSHugh Dickins 		if (!radix_tree_exceptional_entry(page))
3897a5d0fbbSHugh Dickins 			pvec->pages[j++] = page;
3907a5d0fbbSHugh Dickins 	}
3917a5d0fbbSHugh Dickins 	pvec->nr = j;
39224513264SHugh Dickins }
39324513264SHugh Dickins 
39424513264SHugh Dickins /*
39524513264SHugh Dickins  * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
39624513264SHugh Dickins  */
39724513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping)
39824513264SHugh Dickins {
39924513264SHugh Dickins 	struct pagevec pvec;
40024513264SHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
40124513264SHugh Dickins 	pgoff_t index = 0;
40224513264SHugh Dickins 
40324513264SHugh Dickins 	pagevec_init(&pvec, 0);
40424513264SHugh Dickins 	/*
40524513264SHugh Dickins 	 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
40624513264SHugh Dickins 	 */
40724513264SHugh Dickins 	while (!mapping_unevictable(mapping)) {
40824513264SHugh Dickins 		/*
40924513264SHugh Dickins 		 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
41024513264SHugh Dickins 		 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
41124513264SHugh Dickins 		 */
41224513264SHugh Dickins 		pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
41324513264SHugh Dickins 					PAGEVEC_SIZE, pvec.pages, indices);
41424513264SHugh Dickins 		if (!pvec.nr)
41524513264SHugh Dickins 			break;
41624513264SHugh Dickins 		index = indices[pvec.nr - 1] + 1;
41724513264SHugh Dickins 		shmem_deswap_pagevec(&pvec);
41824513264SHugh Dickins 		check_move_unevictable_pages(pvec.pages, pvec.nr);
41924513264SHugh Dickins 		pagevec_release(&pvec);
42024513264SHugh Dickins 		cond_resched();
42124513264SHugh Dickins 	}
4227a5d0fbbSHugh Dickins }
4237a5d0fbbSHugh Dickins 
4247a5d0fbbSHugh Dickins /*
4257a5d0fbbSHugh Dickins  * Remove range of pages and swap entries from radix tree, and free them.
4267a5d0fbbSHugh Dickins  */
427285b2c4fSHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
4281da177e4SLinus Torvalds {
429285b2c4fSHugh Dickins 	struct address_space *mapping = inode->i_mapping;
4301da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
431285b2c4fSHugh Dickins 	pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
432bda97eabSHugh Dickins 	unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
433285b2c4fSHugh Dickins 	pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
434bda97eabSHugh Dickins 	struct pagevec pvec;
4357a5d0fbbSHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
4367a5d0fbbSHugh Dickins 	long nr_swaps_freed = 0;
437285b2c4fSHugh Dickins 	pgoff_t index;
438bda97eabSHugh Dickins 	int i;
4391da177e4SLinus Torvalds 
440bda97eabSHugh Dickins 	BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
441bda97eabSHugh Dickins 
442bda97eabSHugh Dickins 	pagevec_init(&pvec, 0);
443bda97eabSHugh Dickins 	index = start;
4447a5d0fbbSHugh Dickins 	while (index <= end) {
4457a5d0fbbSHugh Dickins 		pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
4467a5d0fbbSHugh Dickins 			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
4477a5d0fbbSHugh Dickins 							pvec.pages, indices);
4487a5d0fbbSHugh Dickins 		if (!pvec.nr)
4497a5d0fbbSHugh Dickins 			break;
450bda97eabSHugh Dickins 		mem_cgroup_uncharge_start();
451bda97eabSHugh Dickins 		for (i = 0; i < pagevec_count(&pvec); i++) {
452bda97eabSHugh Dickins 			struct page *page = pvec.pages[i];
453bda97eabSHugh Dickins 
4547a5d0fbbSHugh Dickins 			index = indices[i];
455bda97eabSHugh Dickins 			if (index > end)
456bda97eabSHugh Dickins 				break;
457bda97eabSHugh Dickins 
4587a5d0fbbSHugh Dickins 			if (radix_tree_exceptional_entry(page)) {
4597a5d0fbbSHugh Dickins 				nr_swaps_freed += !shmem_free_swap(mapping,
4607a5d0fbbSHugh Dickins 								index, page);
4617a5d0fbbSHugh Dickins 				continue;
4627a5d0fbbSHugh Dickins 			}
4637a5d0fbbSHugh Dickins 
464bda97eabSHugh Dickins 			if (!trylock_page(page))
465bda97eabSHugh Dickins 				continue;
4667a5d0fbbSHugh Dickins 			if (page->mapping == mapping) {
4677a5d0fbbSHugh Dickins 				VM_BUG_ON(PageWriteback(page));
468bda97eabSHugh Dickins 				truncate_inode_page(mapping, page);
4697a5d0fbbSHugh Dickins 			}
470bda97eabSHugh Dickins 			unlock_page(page);
471bda97eabSHugh Dickins 		}
47224513264SHugh Dickins 		shmem_deswap_pagevec(&pvec);
47324513264SHugh Dickins 		pagevec_release(&pvec);
474bda97eabSHugh Dickins 		mem_cgroup_uncharge_end();
475bda97eabSHugh Dickins 		cond_resched();
476bda97eabSHugh Dickins 		index++;
477bda97eabSHugh Dickins 	}
478bda97eabSHugh Dickins 
479bda97eabSHugh Dickins 	if (partial) {
480bda97eabSHugh Dickins 		struct page *page = NULL;
481bda97eabSHugh Dickins 		shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
482bda97eabSHugh Dickins 		if (page) {
483bda97eabSHugh Dickins 			zero_user_segment(page, partial, PAGE_CACHE_SIZE);
484bda97eabSHugh Dickins 			set_page_dirty(page);
485bda97eabSHugh Dickins 			unlock_page(page);
486bda97eabSHugh Dickins 			page_cache_release(page);
487bda97eabSHugh Dickins 		}
488bda97eabSHugh Dickins 	}
489bda97eabSHugh Dickins 
490bda97eabSHugh Dickins 	index = start;
491bda97eabSHugh Dickins 	for ( ; ; ) {
492bda97eabSHugh Dickins 		cond_resched();
4937a5d0fbbSHugh Dickins 		pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
4947a5d0fbbSHugh Dickins 			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
4957a5d0fbbSHugh Dickins 							pvec.pages, indices);
4967a5d0fbbSHugh Dickins 		if (!pvec.nr) {
497bda97eabSHugh Dickins 			if (index == start)
498bda97eabSHugh Dickins 				break;
499bda97eabSHugh Dickins 			index = start;
500bda97eabSHugh Dickins 			continue;
501bda97eabSHugh Dickins 		}
5027a5d0fbbSHugh Dickins 		if (index == start && indices[0] > end) {
50324513264SHugh Dickins 			shmem_deswap_pagevec(&pvec);
50424513264SHugh Dickins 			pagevec_release(&pvec);
505bda97eabSHugh Dickins 			break;
506bda97eabSHugh Dickins 		}
507bda97eabSHugh Dickins 		mem_cgroup_uncharge_start();
508bda97eabSHugh Dickins 		for (i = 0; i < pagevec_count(&pvec); i++) {
509bda97eabSHugh Dickins 			struct page *page = pvec.pages[i];
510bda97eabSHugh Dickins 
5117a5d0fbbSHugh Dickins 			index = indices[i];
512bda97eabSHugh Dickins 			if (index > end)
513bda97eabSHugh Dickins 				break;
514bda97eabSHugh Dickins 
5157a5d0fbbSHugh Dickins 			if (radix_tree_exceptional_entry(page)) {
5167a5d0fbbSHugh Dickins 				nr_swaps_freed += !shmem_free_swap(mapping,
5177a5d0fbbSHugh Dickins 								index, page);
5187a5d0fbbSHugh Dickins 				continue;
5197a5d0fbbSHugh Dickins 			}
5207a5d0fbbSHugh Dickins 
521bda97eabSHugh Dickins 			lock_page(page);
5227a5d0fbbSHugh Dickins 			if (page->mapping == mapping) {
5237a5d0fbbSHugh Dickins 				VM_BUG_ON(PageWriteback(page));
524bda97eabSHugh Dickins 				truncate_inode_page(mapping, page);
5257a5d0fbbSHugh Dickins 			}
526bda97eabSHugh Dickins 			unlock_page(page);
527bda97eabSHugh Dickins 		}
52824513264SHugh Dickins 		shmem_deswap_pagevec(&pvec);
52924513264SHugh Dickins 		pagevec_release(&pvec);
530bda97eabSHugh Dickins 		mem_cgroup_uncharge_end();
531bda97eabSHugh Dickins 		index++;
532bda97eabSHugh Dickins 	}
53394c1e62dSHugh Dickins 
5341da177e4SLinus Torvalds 	spin_lock(&info->lock);
5357a5d0fbbSHugh Dickins 	info->swapped -= nr_swaps_freed;
5361da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
5371da177e4SLinus Torvalds 	spin_unlock(&info->lock);
5381da177e4SLinus Torvalds 
539285b2c4fSHugh Dickins 	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
5401da177e4SLinus Torvalds }
54194c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
5421da177e4SLinus Torvalds 
54394c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
5441da177e4SLinus Torvalds {
5451da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5461da177e4SLinus Torvalds 	int error;
5471da177e4SLinus Torvalds 
548db78b877SChristoph Hellwig 	error = inode_change_ok(inode, attr);
549db78b877SChristoph Hellwig 	if (error)
550db78b877SChristoph Hellwig 		return error;
551db78b877SChristoph Hellwig 
55294c1e62dSHugh Dickins 	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
55394c1e62dSHugh Dickins 		loff_t oldsize = inode->i_size;
55494c1e62dSHugh Dickins 		loff_t newsize = attr->ia_size;
5553889e6e7Snpiggin@suse.de 
55694c1e62dSHugh Dickins 		if (newsize != oldsize) {
55794c1e62dSHugh Dickins 			i_size_write(inode, newsize);
55894c1e62dSHugh Dickins 			inode->i_ctime = inode->i_mtime = CURRENT_TIME;
55994c1e62dSHugh Dickins 		}
56094c1e62dSHugh Dickins 		if (newsize < oldsize) {
56194c1e62dSHugh Dickins 			loff_t holebegin = round_up(newsize, PAGE_SIZE);
56294c1e62dSHugh Dickins 			unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
56394c1e62dSHugh Dickins 			shmem_truncate_range(inode, newsize, (loff_t)-1);
56494c1e62dSHugh Dickins 			/* unmap again to remove racily COWed private pages */
56594c1e62dSHugh Dickins 			unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
56694c1e62dSHugh Dickins 		}
5671da177e4SLinus Torvalds 	}
5681da177e4SLinus Torvalds 
5696a1a90adSChristoph Hellwig 	setattr_copy(inode, attr);
57039f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
571db78b877SChristoph Hellwig 	if (attr->ia_valid & ATTR_MODE)
5721c7c474cSChristoph Hellwig 		error = generic_acl_chmod(inode);
57339f0247dSAndreas Gruenbacher #endif
5741da177e4SLinus Torvalds 	return error;
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds 
5771f895f75SAl Viro static void shmem_evict_inode(struct inode *inode)
5781da177e4SLinus Torvalds {
5791da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
580b09e0fa4SEric Paris 	struct shmem_xattr *xattr, *nxattr;
5811da177e4SLinus Torvalds 
5823889e6e7Snpiggin@suse.de 	if (inode->i_mapping->a_ops == &shmem_aops) {
5831da177e4SLinus Torvalds 		shmem_unacct_size(info->flags, inode->i_size);
5841da177e4SLinus Torvalds 		inode->i_size = 0;
5853889e6e7Snpiggin@suse.de 		shmem_truncate_range(inode, 0, (loff_t)-1);
5861da177e4SLinus Torvalds 		if (!list_empty(&info->swaplist)) {
587cb5f7b9aSHugh Dickins 			mutex_lock(&shmem_swaplist_mutex);
5881da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
589cb5f7b9aSHugh Dickins 			mutex_unlock(&shmem_swaplist_mutex);
5901da177e4SLinus Torvalds 		}
59169f07ec9SHugh Dickins 	} else
59269f07ec9SHugh Dickins 		kfree(info->symlink);
593b09e0fa4SEric Paris 
594b09e0fa4SEric Paris 	list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
595b09e0fa4SEric Paris 		kfree(xattr->name);
596b09e0fa4SEric Paris 		kfree(xattr);
597b09e0fa4SEric Paris 	}
5981da177e4SLinus Torvalds 	BUG_ON(inode->i_blocks);
5995b04c689SPavel Emelyanov 	shmem_free_inode(inode->i_sb);
600dbd5768fSJan Kara 	clear_inode(inode);
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
60346f65ec1SHugh Dickins /*
60446f65ec1SHugh Dickins  * If swap found in inode, free it and move page from swapcache to filecache.
60546f65ec1SHugh Dickins  */
60641ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info,
60741ffe5d5SHugh Dickins 			     swp_entry_t swap, struct page *page)
6081da177e4SLinus Torvalds {
609285b2c4fSHugh Dickins 	struct address_space *mapping = info->vfs_inode.i_mapping;
61046f65ec1SHugh Dickins 	void *radswap;
61141ffe5d5SHugh Dickins 	pgoff_t index;
612d9fe526aSHugh Dickins 	int error;
6131da177e4SLinus Torvalds 
61446f65ec1SHugh Dickins 	radswap = swp_to_radix_entry(swap);
615e504f3fdSHugh Dickins 	index = radix_tree_locate_item(&mapping->page_tree, radswap);
61646f65ec1SHugh Dickins 	if (index == -1)
6171da177e4SLinus Torvalds 		return 0;
6182e0e26c7SHugh Dickins 
6191b1b32f2SHugh Dickins 	/*
6201b1b32f2SHugh Dickins 	 * Move _head_ to start search for next from here.
6211f895f75SAl Viro 	 * But be careful: shmem_evict_inode checks list_empty without taking
6221b1b32f2SHugh Dickins 	 * mutex, and there's an instant in list_move_tail when info->swaplist
623285b2c4fSHugh Dickins 	 * would appear empty, if it were the only one on shmem_swaplist.
6241b1b32f2SHugh Dickins 	 */
6251b1b32f2SHugh Dickins 	if (shmem_swaplist.next != &info->swaplist)
6262e0e26c7SHugh Dickins 		list_move_tail(&shmem_swaplist, &info->swaplist);
6272e0e26c7SHugh Dickins 
628d13d1443SKAMEZAWA Hiroyuki 	/*
629778dd893SHugh Dickins 	 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
630778dd893SHugh Dickins 	 * but also to hold up shmem_evict_inode(): so inode cannot be freed
631778dd893SHugh Dickins 	 * beneath us (pagelock doesn't help until the page is in pagecache).
632d13d1443SKAMEZAWA Hiroyuki 	 */
63346f65ec1SHugh Dickins 	error = shmem_add_to_page_cache(page, mapping, index,
63446f65ec1SHugh Dickins 						GFP_NOWAIT, radswap);
635778dd893SHugh Dickins 	/* which does mem_cgroup_uncharge_cache_page on error */
63669029cd5SKAMEZAWA Hiroyuki 
63748f170fbSHugh Dickins 	if (error != -ENOMEM) {
63846f65ec1SHugh Dickins 		/*
63946f65ec1SHugh Dickins 		 * Truncation and eviction use free_swap_and_cache(), which
64046f65ec1SHugh Dickins 		 * only does trylock page: if we raced, best clean up here.
64146f65ec1SHugh Dickins 		 */
64273b1262fSHugh Dickins 		delete_from_swap_cache(page);
64373b1262fSHugh Dickins 		set_page_dirty(page);
64446f65ec1SHugh Dickins 		if (!error) {
64546f65ec1SHugh Dickins 			spin_lock(&info->lock);
646285b2c4fSHugh Dickins 			info->swapped--;
64746f65ec1SHugh Dickins 			spin_unlock(&info->lock);
64841ffe5d5SHugh Dickins 			swap_free(swap);
64946f65ec1SHugh Dickins 		}
6502e0e26c7SHugh Dickins 		error = 1;	/* not an error, but entry was found */
6511da177e4SLinus Torvalds 	}
6522e0e26c7SHugh Dickins 	return error;
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds /*
65646f65ec1SHugh Dickins  * Search through swapped inodes to find and replace swap by page.
6571da177e4SLinus Torvalds  */
65841ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page)
6591da177e4SLinus Torvalds {
66041ffe5d5SHugh Dickins 	struct list_head *this, *next;
6611da177e4SLinus Torvalds 	struct shmem_inode_info *info;
6621da177e4SLinus Torvalds 	int found = 0;
663778dd893SHugh Dickins 	int error;
664778dd893SHugh Dickins 
665778dd893SHugh Dickins 	/*
666778dd893SHugh Dickins 	 * Charge page using GFP_KERNEL while we can wait, before taking
667778dd893SHugh Dickins 	 * the shmem_swaplist_mutex which might hold up shmem_writepage().
668778dd893SHugh Dickins 	 * Charged back to the user (not to caller) when swap account is used.
669778dd893SHugh Dickins 	 */
670778dd893SHugh Dickins 	error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
671778dd893SHugh Dickins 	if (error)
672778dd893SHugh Dickins 		goto out;
67346f65ec1SHugh Dickins 	/* No radix_tree_preload: swap entry keeps a place for page in tree */
6741da177e4SLinus Torvalds 
675cb5f7b9aSHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
67641ffe5d5SHugh Dickins 	list_for_each_safe(this, next, &shmem_swaplist) {
67741ffe5d5SHugh Dickins 		info = list_entry(this, struct shmem_inode_info, swaplist);
678285b2c4fSHugh Dickins 		if (info->swapped)
67941ffe5d5SHugh Dickins 			found = shmem_unuse_inode(info, swap, page);
6806922c0c7SHugh Dickins 		else
6816922c0c7SHugh Dickins 			list_del_init(&info->swaplist);
682cb5f7b9aSHugh Dickins 		cond_resched();
6832e0e26c7SHugh Dickins 		if (found)
684778dd893SHugh Dickins 			break;
6851da177e4SLinus Torvalds 	}
686cb5f7b9aSHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
687778dd893SHugh Dickins 
688778dd893SHugh Dickins 	if (!found)
689778dd893SHugh Dickins 		mem_cgroup_uncharge_cache_page(page);
690778dd893SHugh Dickins 	if (found < 0)
691778dd893SHugh Dickins 		error = found;
692778dd893SHugh Dickins out:
693aaa46865SHugh Dickins 	unlock_page(page);
694aaa46865SHugh Dickins 	page_cache_release(page);
695778dd893SHugh Dickins 	return error;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds /*
6991da177e4SLinus Torvalds  * Move the page from the page cache to the swap cache.
7001da177e4SLinus Torvalds  */
7011da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	struct shmem_inode_info *info;
7041da177e4SLinus Torvalds 	struct address_space *mapping;
7051da177e4SLinus Torvalds 	struct inode *inode;
7066922c0c7SHugh Dickins 	swp_entry_t swap;
7076922c0c7SHugh Dickins 	pgoff_t index;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
7101da177e4SLinus Torvalds 	mapping = page->mapping;
7111da177e4SLinus Torvalds 	index = page->index;
7121da177e4SLinus Torvalds 	inode = mapping->host;
7131da177e4SLinus Torvalds 	info = SHMEM_I(inode);
7141da177e4SLinus Torvalds 	if (info->flags & VM_LOCKED)
7151da177e4SLinus Torvalds 		goto redirty;
716d9fe526aSHugh Dickins 	if (!total_swap_pages)
7171da177e4SLinus Torvalds 		goto redirty;
7181da177e4SLinus Torvalds 
719d9fe526aSHugh Dickins 	/*
720d9fe526aSHugh Dickins 	 * shmem_backing_dev_info's capabilities prevent regular writeback or
721d9fe526aSHugh Dickins 	 * sync from ever calling shmem_writepage; but a stacking filesystem
72248f170fbSHugh Dickins 	 * might use ->writepage of its underlying filesystem, in which case
723d9fe526aSHugh Dickins 	 * tmpfs should write out to swap only in response to memory pressure,
72448f170fbSHugh Dickins 	 * and not for the writeback threads or sync.
725d9fe526aSHugh Dickins 	 */
72648f170fbSHugh Dickins 	if (!wbc->for_reclaim) {
72748f170fbSHugh Dickins 		WARN_ON_ONCE(1);	/* Still happens? Tell us about it! */
72848f170fbSHugh Dickins 		goto redirty;
72948f170fbSHugh Dickins 	}
730d9fe526aSHugh Dickins 	swap = get_swap_page();
73148f170fbSHugh Dickins 	if (!swap.val)
73248f170fbSHugh Dickins 		goto redirty;
733d9fe526aSHugh Dickins 
734b1dea800SHugh Dickins 	/*
735b1dea800SHugh Dickins 	 * Add inode to shmem_unuse()'s list of swapped-out inodes,
7366922c0c7SHugh Dickins 	 * if it's not already there.  Do it now before the page is
7376922c0c7SHugh Dickins 	 * moved to swap cache, when its pagelock no longer protects
738b1dea800SHugh Dickins 	 * the inode from eviction.  But don't unlock the mutex until
7396922c0c7SHugh Dickins 	 * we've incremented swapped, because shmem_unuse_inode() will
7406922c0c7SHugh Dickins 	 * prune a !swapped inode from the swaplist under this mutex.
741b1dea800SHugh Dickins 	 */
742b1dea800SHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
74305bf86b4SHugh Dickins 	if (list_empty(&info->swaplist))
74405bf86b4SHugh Dickins 		list_add_tail(&info->swaplist, &shmem_swaplist);
745b1dea800SHugh Dickins 
74648f170fbSHugh Dickins 	if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
747aaa46865SHugh Dickins 		swap_shmem_alloc(swap);
7486922c0c7SHugh Dickins 		shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
7496922c0c7SHugh Dickins 
7506922c0c7SHugh Dickins 		spin_lock(&info->lock);
7516922c0c7SHugh Dickins 		info->swapped++;
7526922c0c7SHugh Dickins 		shmem_recalc_inode(inode);
753826267cfSHugh Dickins 		spin_unlock(&info->lock);
7546922c0c7SHugh Dickins 
7556922c0c7SHugh Dickins 		mutex_unlock(&shmem_swaplist_mutex);
756d9fe526aSHugh Dickins 		BUG_ON(page_mapped(page));
7579fab5619SHugh Dickins 		swap_writepage(page, wbc);
7581da177e4SLinus Torvalds 		return 0;
7591da177e4SLinus Torvalds 	}
7601da177e4SLinus Torvalds 
7616922c0c7SHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
762cb4b86baSKAMEZAWA Hiroyuki 	swapcache_free(swap, NULL);
7631da177e4SLinus Torvalds redirty:
7641da177e4SLinus Torvalds 	set_page_dirty(page);
765d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
766d9fe526aSHugh Dickins 		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
767d9fe526aSHugh Dickins 	unlock_page(page);
768d9fe526aSHugh Dickins 	return 0;
7691da177e4SLinus Torvalds }
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds #ifdef CONFIG_NUMA
772680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
77371fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
774680d794bSakpm@linux-foundation.org {
775680d794bSakpm@linux-foundation.org 	char buffer[64];
776680d794bSakpm@linux-foundation.org 
77771fe804bSLee Schermerhorn 	if (!mpol || mpol->mode == MPOL_DEFAULT)
778095f1fc4SLee Schermerhorn 		return;		/* show nothing */
779095f1fc4SLee Schermerhorn 
78071fe804bSLee Schermerhorn 	mpol_to_str(buffer, sizeof(buffer), mpol, 1);
781095f1fc4SLee Schermerhorn 
782095f1fc4SLee Schermerhorn 	seq_printf(seq, ",mpol=%s", buffer);
783680d794bSakpm@linux-foundation.org }
78471fe804bSLee Schermerhorn 
78571fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
78671fe804bSLee Schermerhorn {
78771fe804bSLee Schermerhorn 	struct mempolicy *mpol = NULL;
78871fe804bSLee Schermerhorn 	if (sbinfo->mpol) {
78971fe804bSLee Schermerhorn 		spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
79071fe804bSLee Schermerhorn 		mpol = sbinfo->mpol;
79171fe804bSLee Schermerhorn 		mpol_get(mpol);
79271fe804bSLee Schermerhorn 		spin_unlock(&sbinfo->stat_lock);
79371fe804bSLee Schermerhorn 	}
79471fe804bSLee Schermerhorn 	return mpol;
79571fe804bSLee Schermerhorn }
796680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
797680d794bSakpm@linux-foundation.org 
79841ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
79941ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
8001da177e4SLinus Torvalds {
80152cd3b07SLee Schermerhorn 	struct mempolicy mpol, *spol;
8021da177e4SLinus Torvalds 	struct vm_area_struct pvma;
8031da177e4SLinus Torvalds 
80452cd3b07SLee Schermerhorn 	spol = mpol_cond_copy(&mpol,
80541ffe5d5SHugh Dickins 			mpol_shared_policy_lookup(&info->policy, index));
80652cd3b07SLee Schermerhorn 
8071da177e4SLinus Torvalds 	/* Create a pseudo vma that just contains the policy */
808c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
80941ffe5d5SHugh Dickins 	pvma.vm_pgoff = index;
810c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
81152cd3b07SLee Schermerhorn 	pvma.vm_policy = spol;
81241ffe5d5SHugh Dickins 	return swapin_readahead(swap, gfp, &pvma, 0);
8131da177e4SLinus Torvalds }
8141da177e4SLinus Torvalds 
81502098feaSHugh Dickins static struct page *shmem_alloc_page(gfp_t gfp,
81641ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
8171da177e4SLinus Torvalds {
8181da177e4SLinus Torvalds 	struct vm_area_struct pvma;
8191da177e4SLinus Torvalds 
820c4cc6d07SHugh Dickins 	/* Create a pseudo vma that just contains the policy */
821c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
82241ffe5d5SHugh Dickins 	pvma.vm_pgoff = index;
823c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
82441ffe5d5SHugh Dickins 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
82552cd3b07SLee Schermerhorn 
82652cd3b07SLee Schermerhorn 	/*
82752cd3b07SLee Schermerhorn 	 * alloc_page_vma() will drop the shared policy reference
82852cd3b07SLee Schermerhorn 	 */
82952cd3b07SLee Schermerhorn 	return alloc_page_vma(gfp, &pvma, 0);
8301da177e4SLinus Torvalds }
831680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */
832680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
83341ffe5d5SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
834680d794bSakpm@linux-foundation.org {
835680d794bSakpm@linux-foundation.org }
836680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
837680d794bSakpm@linux-foundation.org 
83841ffe5d5SHugh Dickins static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
83941ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
8401da177e4SLinus Torvalds {
84141ffe5d5SHugh Dickins 	return swapin_readahead(swap, gfp, NULL, 0);
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
84402098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp,
84541ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
8461da177e4SLinus Torvalds {
847e84e2e13SHugh Dickins 	return alloc_page(gfp);
8481da177e4SLinus Torvalds }
849680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */
8501da177e4SLinus Torvalds 
85171fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
85271fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
85371fe804bSLee Schermerhorn {
85471fe804bSLee Schermerhorn 	return NULL;
85571fe804bSLee Schermerhorn }
85671fe804bSLee Schermerhorn #endif
85771fe804bSLee Schermerhorn 
8581da177e4SLinus Torvalds /*
85968da9f05SHugh Dickins  * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
8601da177e4SLinus Torvalds  *
8611da177e4SLinus Torvalds  * If we allocate a new one we do not mark it dirty. That's up to the
8621da177e4SLinus Torvalds  * vm. If we swap it in we mark it dirty since we also free the swap
8631da177e4SLinus Torvalds  * entry since a page cannot live in both the swap and page cache
8641da177e4SLinus Torvalds  */
86541ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
86668da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
8671da177e4SLinus Torvalds {
8681da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
86954af6042SHugh Dickins 	struct shmem_inode_info *info;
8701da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo;
87127ab7006SHugh Dickins 	struct page *page;
8721da177e4SLinus Torvalds 	swp_entry_t swap;
8731da177e4SLinus Torvalds 	int error;
87454af6042SHugh Dickins 	int once = 0;
8751da177e4SLinus Torvalds 
87641ffe5d5SHugh Dickins 	if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
8771da177e4SLinus Torvalds 		return -EFBIG;
8781da177e4SLinus Torvalds repeat:
87954af6042SHugh Dickins 	swap.val = 0;
88041ffe5d5SHugh Dickins 	page = find_lock_page(mapping, index);
88154af6042SHugh Dickins 	if (radix_tree_exceptional_entry(page)) {
88254af6042SHugh Dickins 		swap = radix_to_swp_entry(page);
88354af6042SHugh Dickins 		page = NULL;
88454af6042SHugh Dickins 	}
88554af6042SHugh Dickins 
88654af6042SHugh Dickins 	if (sgp != SGP_WRITE &&
88754af6042SHugh Dickins 	    ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
88854af6042SHugh Dickins 		error = -EINVAL;
88954af6042SHugh Dickins 		goto failed;
89054af6042SHugh Dickins 	}
89154af6042SHugh Dickins 
89254af6042SHugh Dickins 	if (page || (sgp == SGP_READ && !swap.val)) {
89327ab7006SHugh Dickins 		/*
89427ab7006SHugh Dickins 		 * Once we can get the page lock, it must be uptodate:
89527ab7006SHugh Dickins 		 * if there were an error in reading back from swap,
89627ab7006SHugh Dickins 		 * the page would not be inserted into the filecache.
89727ab7006SHugh Dickins 		 */
89854af6042SHugh Dickins 		BUG_ON(page && !PageUptodate(page));
89954af6042SHugh Dickins 		*pagep = page;
90054af6042SHugh Dickins 		return 0;
90127ab7006SHugh Dickins 	}
90227ab7006SHugh Dickins 
903b409f9fcSHugh Dickins 	/*
90454af6042SHugh Dickins 	 * Fast cache lookup did not find it:
90554af6042SHugh Dickins 	 * bring it back from swap or allocate.
906b409f9fcSHugh Dickins 	 */
90754af6042SHugh Dickins 	info = SHMEM_I(inode);
90854af6042SHugh Dickins 	sbinfo = SHMEM_SB(inode->i_sb);
90927ab7006SHugh Dickins 
9101da177e4SLinus Torvalds 	if (swap.val) {
9111da177e4SLinus Torvalds 		/* Look it up and read it in.. */
91227ab7006SHugh Dickins 		page = lookup_swap_cache(swap);
91327ab7006SHugh Dickins 		if (!page) {
914456f998eSYing Han 			/* here we actually do the io */
91568da9f05SHugh Dickins 			if (fault_type)
91668da9f05SHugh Dickins 				*fault_type |= VM_FAULT_MAJOR;
91741ffe5d5SHugh Dickins 			page = shmem_swapin(swap, gfp, info, index);
91827ab7006SHugh Dickins 			if (!page) {
9191da177e4SLinus Torvalds 				error = -ENOMEM;
92054af6042SHugh Dickins 				goto failed;
921285b2c4fSHugh Dickins 			}
9221da177e4SLinus Torvalds 		}
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 		/* We have to do this with page locked to prevent races */
92554af6042SHugh Dickins 		lock_page(page);
92627ab7006SHugh Dickins 		if (!PageUptodate(page)) {
9271da177e4SLinus Torvalds 			error = -EIO;
92854af6042SHugh Dickins 			goto failed;
92954af6042SHugh Dickins 		}
93054af6042SHugh Dickins 		wait_on_page_writeback(page);
93154af6042SHugh Dickins 
93254af6042SHugh Dickins 		/* Someone may have already done it for us */
93354af6042SHugh Dickins 		if (page->mapping) {
93454af6042SHugh Dickins 			if (page->mapping == mapping &&
93554af6042SHugh Dickins 			    page->index == index)
93654af6042SHugh Dickins 				goto done;
93754af6042SHugh Dickins 			error = -EEXIST;
93854af6042SHugh Dickins 			goto failed;
9391da177e4SLinus Torvalds 		}
9401da177e4SLinus Torvalds 
941aa3b1895SHugh Dickins 		error = mem_cgroup_cache_charge(page, current->mm,
942aa3b1895SHugh Dickins 						gfp & GFP_RECLAIM_MASK);
943aa3b1895SHugh Dickins 		if (!error)
94454af6042SHugh Dickins 			error = shmem_add_to_page_cache(page, mapping, index,
94554af6042SHugh Dickins 						gfp, swp_to_radix_entry(swap));
94654af6042SHugh Dickins 		if (error)
94754af6042SHugh Dickins 			goto failed;
94854af6042SHugh Dickins 
94954af6042SHugh Dickins 		spin_lock(&info->lock);
95054af6042SHugh Dickins 		info->swapped--;
95154af6042SHugh Dickins 		shmem_recalc_inode(inode);
9521da177e4SLinus Torvalds 		spin_unlock(&info->lock);
95327ab7006SHugh Dickins 
95427ab7006SHugh Dickins 		delete_from_swap_cache(page);
95527ab7006SHugh Dickins 		set_page_dirty(page);
95627ab7006SHugh Dickins 		swap_free(swap);
95727ab7006SHugh Dickins 
95854af6042SHugh Dickins 	} else {
95954af6042SHugh Dickins 		if (shmem_acct_block(info->flags)) {
96054af6042SHugh Dickins 			error = -ENOSPC;
96154af6042SHugh Dickins 			goto failed;
9621da177e4SLinus Torvalds 		}
9630edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
964fc5da22aSHugh Dickins 			if (percpu_counter_compare(&sbinfo->used_blocks,
96554af6042SHugh Dickins 						sbinfo->max_blocks) >= 0) {
96654af6042SHugh Dickins 				error = -ENOSPC;
96754af6042SHugh Dickins 				goto unacct;
96854af6042SHugh Dickins 			}
9697e496299STim Chen 			percpu_counter_inc(&sbinfo->used_blocks);
97059a16eadSHugh Dickins 		}
9711da177e4SLinus Torvalds 
97254af6042SHugh Dickins 		page = shmem_alloc_page(gfp, info, index);
97354af6042SHugh Dickins 		if (!page) {
97454af6042SHugh Dickins 			error = -ENOMEM;
97554af6042SHugh Dickins 			goto decused;
97654af6042SHugh Dickins 		}
97754af6042SHugh Dickins 
97854af6042SHugh Dickins 		SetPageSwapBacked(page);
97954af6042SHugh Dickins 		__set_page_locked(page);
980aa3b1895SHugh Dickins 		error = mem_cgroup_cache_charge(page, current->mm,
981aa3b1895SHugh Dickins 						gfp & GFP_RECLAIM_MASK);
982aa3b1895SHugh Dickins 		if (!error)
98354af6042SHugh Dickins 			error = shmem_add_to_page_cache(page, mapping, index,
98454af6042SHugh Dickins 						gfp, NULL);
98554af6042SHugh Dickins 		if (error)
98654af6042SHugh Dickins 			goto decused;
98754af6042SHugh Dickins 		lru_cache_add_anon(page);
98854af6042SHugh Dickins 
98954af6042SHugh Dickins 		spin_lock(&info->lock);
9901da177e4SLinus Torvalds 		info->alloced++;
99154af6042SHugh Dickins 		inode->i_blocks += BLOCKS_PER_PAGE;
99254af6042SHugh Dickins 		shmem_recalc_inode(inode);
99359a16eadSHugh Dickins 		spin_unlock(&info->lock);
99454af6042SHugh Dickins 
99527ab7006SHugh Dickins 		clear_highpage(page);
99627ab7006SHugh Dickins 		flush_dcache_page(page);
99727ab7006SHugh Dickins 		SetPageUptodate(page);
998a0ee5ec5SHugh Dickins 		if (sgp == SGP_DIRTY)
99927ab7006SHugh Dickins 			set_page_dirty(page);
10001da177e4SLinus Torvalds 	}
10011da177e4SLinus Torvalds done:
100254af6042SHugh Dickins 	/* Perhaps the file has been truncated since we checked */
100354af6042SHugh Dickins 	if (sgp != SGP_WRITE &&
100454af6042SHugh Dickins 	    ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
100554af6042SHugh Dickins 		error = -EINVAL;
100654af6042SHugh Dickins 		goto trunc;
1007ff36b801SShaohua Li 	}
100854af6042SHugh Dickins 	*pagep = page;
100954af6042SHugh Dickins 	return 0;
1010d00806b1SNick Piggin 
1011d0217ac0SNick Piggin 	/*
101254af6042SHugh Dickins 	 * Error recovery.
10131da177e4SLinus Torvalds 	 */
101454af6042SHugh Dickins trunc:
101554af6042SHugh Dickins 	ClearPageDirty(page);
101654af6042SHugh Dickins 	delete_from_page_cache(page);
101754af6042SHugh Dickins 	spin_lock(&info->lock);
101854af6042SHugh Dickins 	info->alloced--;
101954af6042SHugh Dickins 	inode->i_blocks -= BLOCKS_PER_PAGE;
10201da177e4SLinus Torvalds 	spin_unlock(&info->lock);
102154af6042SHugh Dickins decused:
102254af6042SHugh Dickins 	if (sbinfo->max_blocks)
102354af6042SHugh Dickins 		percpu_counter_add(&sbinfo->used_blocks, -1);
102454af6042SHugh Dickins unacct:
102554af6042SHugh Dickins 	shmem_unacct_blocks(info->flags, 1);
102654af6042SHugh Dickins failed:
102754af6042SHugh Dickins 	if (swap.val && error != -EINVAL) {
102854af6042SHugh Dickins 		struct page *test = find_get_page(mapping, index);
102954af6042SHugh Dickins 		if (test && !radix_tree_exceptional_entry(test))
103054af6042SHugh Dickins 			page_cache_release(test);
103154af6042SHugh Dickins 		/* Have another try if the entry has changed */
103254af6042SHugh Dickins 		if (test != swp_to_radix_entry(swap))
103354af6042SHugh Dickins 			error = -EEXIST;
103454af6042SHugh Dickins 	}
103527ab7006SHugh Dickins 	if (page) {
103654af6042SHugh Dickins 		unlock_page(page);
10371da177e4SLinus Torvalds 		page_cache_release(page);
103854af6042SHugh Dickins 	}
103954af6042SHugh Dickins 	if (error == -ENOSPC && !once++) {
104054af6042SHugh Dickins 		info = SHMEM_I(inode);
104154af6042SHugh Dickins 		spin_lock(&info->lock);
104254af6042SHugh Dickins 		shmem_recalc_inode(inode);
104354af6042SHugh Dickins 		spin_unlock(&info->lock);
10441da177e4SLinus Torvalds 		goto repeat;
1045d8dc74f2SAdrian Bunk 	}
104654af6042SHugh Dickins 	if (error == -EEXIST)
104754af6042SHugh Dickins 		goto repeat;
104854af6042SHugh Dickins 	return error;
10491da177e4SLinus Torvalds }
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
10521da177e4SLinus Torvalds {
10531da177e4SLinus Torvalds 	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
10541da177e4SLinus Torvalds 	int error;
105568da9f05SHugh Dickins 	int ret = VM_FAULT_LOCKED;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
10581da177e4SLinus Torvalds 	if (error)
10591da177e4SLinus Torvalds 		return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
106068da9f05SHugh Dickins 
1061456f998eSYing Han 	if (ret & VM_FAULT_MAJOR) {
1062456f998eSYing Han 		count_vm_event(PGMAJFAULT);
1063456f998eSYing Han 		mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1064456f998eSYing Han 	}
106568da9f05SHugh Dickins 	return ret;
10661da177e4SLinus Torvalds }
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds #ifdef CONFIG_NUMA
106941ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
10701da177e4SLinus Torvalds {
107141ffe5d5SHugh Dickins 	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
107241ffe5d5SHugh Dickins 	return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
10731da177e4SLinus Torvalds }
10741da177e4SLinus Torvalds 
1075d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1076d8dc74f2SAdrian Bunk 					  unsigned long addr)
10771da177e4SLinus Torvalds {
107841ffe5d5SHugh Dickins 	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
107941ffe5d5SHugh Dickins 	pgoff_t index;
10801da177e4SLinus Torvalds 
108141ffe5d5SHugh Dickins 	index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
108241ffe5d5SHugh Dickins 	return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
10831da177e4SLinus Torvalds }
10841da177e4SLinus Torvalds #endif
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user)
10871da177e4SLinus Torvalds {
1088d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
10891da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
10901da177e4SLinus Torvalds 	int retval = -ENOMEM;
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	spin_lock(&info->lock);
10931da177e4SLinus Torvalds 	if (lock && !(info->flags & VM_LOCKED)) {
10941da177e4SLinus Torvalds 		if (!user_shm_lock(inode->i_size, user))
10951da177e4SLinus Torvalds 			goto out_nomem;
10961da177e4SLinus Torvalds 		info->flags |= VM_LOCKED;
109789e004eaSLee Schermerhorn 		mapping_set_unevictable(file->f_mapping);
10981da177e4SLinus Torvalds 	}
10991da177e4SLinus Torvalds 	if (!lock && (info->flags & VM_LOCKED) && user) {
11001da177e4SLinus Torvalds 		user_shm_unlock(inode->i_size, user);
11011da177e4SLinus Torvalds 		info->flags &= ~VM_LOCKED;
110289e004eaSLee Schermerhorn 		mapping_clear_unevictable(file->f_mapping);
11031da177e4SLinus Torvalds 	}
11041da177e4SLinus Torvalds 	retval = 0;
110589e004eaSLee Schermerhorn 
11061da177e4SLinus Torvalds out_nomem:
11071da177e4SLinus Torvalds 	spin_unlock(&info->lock);
11081da177e4SLinus Torvalds 	return retval;
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
11119b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
11121da177e4SLinus Torvalds {
11131da177e4SLinus Torvalds 	file_accessed(file);
11141da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
1115d0217ac0SNick Piggin 	vma->vm_flags |= VM_CAN_NONLINEAR;
11161da177e4SLinus Torvalds 	return 0;
11171da177e4SLinus Torvalds }
11181da177e4SLinus Torvalds 
1119454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
112009208d15SAl Viro 				     umode_t mode, dev_t dev, unsigned long flags)
11211da177e4SLinus Torvalds {
11221da177e4SLinus Torvalds 	struct inode *inode;
11231da177e4SLinus Torvalds 	struct shmem_inode_info *info;
11241da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
11251da177e4SLinus Torvalds 
11265b04c689SPavel Emelyanov 	if (shmem_reserve_inode(sb))
11271da177e4SLinus Torvalds 		return NULL;
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	inode = new_inode(sb);
11301da177e4SLinus Torvalds 	if (inode) {
113185fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
1132454abafeSDmitry Monakhov 		inode_init_owner(inode, dir, mode);
11331da177e4SLinus Torvalds 		inode->i_blocks = 0;
11341da177e4SLinus Torvalds 		inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
11351da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
113691828a40SDavid M. Grimes 		inode->i_generation = get_seconds();
11371da177e4SLinus Torvalds 		info = SHMEM_I(inode);
11381da177e4SLinus Torvalds 		memset(info, 0, (char *)inode - (char *)info);
11391da177e4SLinus Torvalds 		spin_lock_init(&info->lock);
11400b0a0806SHugh Dickins 		info->flags = flags & VM_NORESERVE;
11411da177e4SLinus Torvalds 		INIT_LIST_HEAD(&info->swaplist);
1142b09e0fa4SEric Paris 		INIT_LIST_HEAD(&info->xattr_list);
114372c04902SAl Viro 		cache_no_acl(inode);
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
11461da177e4SLinus Torvalds 		default:
114739f0247dSAndreas Gruenbacher 			inode->i_op = &shmem_special_inode_operations;
11481da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
11491da177e4SLinus Torvalds 			break;
11501da177e4SLinus Torvalds 		case S_IFREG:
115114fcc23fSHugh Dickins 			inode->i_mapping->a_ops = &shmem_aops;
11521da177e4SLinus Torvalds 			inode->i_op = &shmem_inode_operations;
11531da177e4SLinus Torvalds 			inode->i_fop = &shmem_file_operations;
115471fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy,
115571fe804bSLee Schermerhorn 						 shmem_get_sbmpol(sbinfo));
11561da177e4SLinus Torvalds 			break;
11571da177e4SLinus Torvalds 		case S_IFDIR:
1158d8c76e6fSDave Hansen 			inc_nlink(inode);
11591da177e4SLinus Torvalds 			/* Some things misbehave if size == 0 on a directory */
11601da177e4SLinus Torvalds 			inode->i_size = 2 * BOGO_DIRENT_SIZE;
11611da177e4SLinus Torvalds 			inode->i_op = &shmem_dir_inode_operations;
11621da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
11631da177e4SLinus Torvalds 			break;
11641da177e4SLinus Torvalds 		case S_IFLNK:
11651da177e4SLinus Torvalds 			/*
11661da177e4SLinus Torvalds 			 * Must not load anything in the rbtree,
11671da177e4SLinus Torvalds 			 * mpol_free_shared_policy will not be called.
11681da177e4SLinus Torvalds 			 */
116971fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy, NULL);
11701da177e4SLinus Torvalds 			break;
11711da177e4SLinus Torvalds 		}
11725b04c689SPavel Emelyanov 	} else
11735b04c689SPavel Emelyanov 		shmem_free_inode(sb);
11741da177e4SLinus Torvalds 	return inode;
11751da177e4SLinus Torvalds }
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
117892e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations;
117969f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations;
11801da177e4SLinus Torvalds 
11816d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR
11826d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
11836d9d88d0SJarkko Sakkinen #else
11846d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL
11856d9d88d0SJarkko Sakkinen #endif
11866d9d88d0SJarkko Sakkinen 
11871da177e4SLinus Torvalds static int
1188800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping,
1189800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
1190800d15a5SNick Piggin 			struct page **pagep, void **fsdata)
11911da177e4SLinus Torvalds {
1192800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1193800d15a5SNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1194800d15a5SNick Piggin 	return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1195800d15a5SNick Piggin }
1196800d15a5SNick Piggin 
1197800d15a5SNick Piggin static int
1198800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping,
1199800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
1200800d15a5SNick Piggin 			struct page *page, void *fsdata)
1201800d15a5SNick Piggin {
1202800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1203800d15a5SNick Piggin 
1204800d15a5SNick Piggin 	if (pos + copied > inode->i_size)
1205800d15a5SNick Piggin 		i_size_write(inode, pos + copied);
1206800d15a5SNick Piggin 
1207d3602444SHugh Dickins 	set_page_dirty(page);
12086746aff7SWu Fengguang 	unlock_page(page);
1209d3602444SHugh Dickins 	page_cache_release(page);
1210d3602444SHugh Dickins 
1211800d15a5SNick Piggin 	return copied;
12121da177e4SLinus Torvalds }
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
12151da177e4SLinus Torvalds {
1216d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
12171da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
121841ffe5d5SHugh Dickins 	pgoff_t index;
121941ffe5d5SHugh Dickins 	unsigned long offset;
1220a0ee5ec5SHugh Dickins 	enum sgp_type sgp = SGP_READ;
1221a0ee5ec5SHugh Dickins 
1222a0ee5ec5SHugh Dickins 	/*
1223a0ee5ec5SHugh Dickins 	 * Might this read be for a stacking filesystem?  Then when reading
1224a0ee5ec5SHugh Dickins 	 * holes of a sparse file, we actually need to allocate those pages,
1225a0ee5ec5SHugh Dickins 	 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1226a0ee5ec5SHugh Dickins 	 */
1227a0ee5ec5SHugh Dickins 	if (segment_eq(get_fs(), KERNEL_DS))
1228a0ee5ec5SHugh Dickins 		sgp = SGP_DIRTY;
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	index = *ppos >> PAGE_CACHE_SHIFT;
12311da177e4SLinus Torvalds 	offset = *ppos & ~PAGE_CACHE_MASK;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	for (;;) {
12341da177e4SLinus Torvalds 		struct page *page = NULL;
123541ffe5d5SHugh Dickins 		pgoff_t end_index;
123641ffe5d5SHugh Dickins 		unsigned long nr, ret;
12371da177e4SLinus Torvalds 		loff_t i_size = i_size_read(inode);
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
12401da177e4SLinus Torvalds 		if (index > end_index)
12411da177e4SLinus Torvalds 			break;
12421da177e4SLinus Torvalds 		if (index == end_index) {
12431da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
12441da177e4SLinus Torvalds 			if (nr <= offset)
12451da177e4SLinus Torvalds 				break;
12461da177e4SLinus Torvalds 		}
12471da177e4SLinus Torvalds 
1248a0ee5ec5SHugh Dickins 		desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
12491da177e4SLinus Torvalds 		if (desc->error) {
12501da177e4SLinus Torvalds 			if (desc->error == -EINVAL)
12511da177e4SLinus Torvalds 				desc->error = 0;
12521da177e4SLinus Torvalds 			break;
12531da177e4SLinus Torvalds 		}
1254d3602444SHugh Dickins 		if (page)
1255d3602444SHugh Dickins 			unlock_page(page);
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 		/*
12581da177e4SLinus Torvalds 		 * We must evaluate after, since reads (unlike writes)
12591b1dcc1bSJes Sorensen 		 * are called without i_mutex protection against truncate
12601da177e4SLinus Torvalds 		 */
12611da177e4SLinus Torvalds 		nr = PAGE_CACHE_SIZE;
12621da177e4SLinus Torvalds 		i_size = i_size_read(inode);
12631da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
12641da177e4SLinus Torvalds 		if (index == end_index) {
12651da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
12661da177e4SLinus Torvalds 			if (nr <= offset) {
12671da177e4SLinus Torvalds 				if (page)
12681da177e4SLinus Torvalds 					page_cache_release(page);
12691da177e4SLinus Torvalds 				break;
12701da177e4SLinus Torvalds 			}
12711da177e4SLinus Torvalds 		}
12721da177e4SLinus Torvalds 		nr -= offset;
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 		if (page) {
12751da177e4SLinus Torvalds 			/*
12761da177e4SLinus Torvalds 			 * If users can be writing to this page using arbitrary
12771da177e4SLinus Torvalds 			 * virtual addresses, take care about potential aliasing
12781da177e4SLinus Torvalds 			 * before reading the page on the kernel side.
12791da177e4SLinus Torvalds 			 */
12801da177e4SLinus Torvalds 			if (mapping_writably_mapped(mapping))
12811da177e4SLinus Torvalds 				flush_dcache_page(page);
12821da177e4SLinus Torvalds 			/*
12831da177e4SLinus Torvalds 			 * Mark the page accessed if we read the beginning.
12841da177e4SLinus Torvalds 			 */
12851da177e4SLinus Torvalds 			if (!offset)
12861da177e4SLinus Torvalds 				mark_page_accessed(page);
1287b5810039SNick Piggin 		} else {
12881da177e4SLinus Torvalds 			page = ZERO_PAGE(0);
1289b5810039SNick Piggin 			page_cache_get(page);
1290b5810039SNick Piggin 		}
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 		/*
12931da177e4SLinus Torvalds 		 * Ok, we have the page, and it's up-to-date, so
12941da177e4SLinus Torvalds 		 * now we can copy it to user space...
12951da177e4SLinus Torvalds 		 *
12961da177e4SLinus Torvalds 		 * The actor routine returns how many bytes were actually used..
12971da177e4SLinus Torvalds 		 * NOTE! This may not be the same as how much of a user buffer
12981da177e4SLinus Torvalds 		 * we filled up (we may be padding etc), so we can only update
12991da177e4SLinus Torvalds 		 * "pos" here (the actor routine has to update the user buffer
13001da177e4SLinus Torvalds 		 * pointers and the remaining count).
13011da177e4SLinus Torvalds 		 */
13021da177e4SLinus Torvalds 		ret = actor(desc, page, offset, nr);
13031da177e4SLinus Torvalds 		offset += ret;
13041da177e4SLinus Torvalds 		index += offset >> PAGE_CACHE_SHIFT;
13051da177e4SLinus Torvalds 		offset &= ~PAGE_CACHE_MASK;
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 		page_cache_release(page);
13081da177e4SLinus Torvalds 		if (ret != nr || !desc->count)
13091da177e4SLinus Torvalds 			break;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 		cond_resched();
13121da177e4SLinus Torvalds 	}
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
13151da177e4SLinus Torvalds 	file_accessed(filp);
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
1318bcd78e49SHugh Dickins static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1319bcd78e49SHugh Dickins 		const struct iovec *iov, unsigned long nr_segs, loff_t pos)
13201da177e4SLinus Torvalds {
1321bcd78e49SHugh Dickins 	struct file *filp = iocb->ki_filp;
1322bcd78e49SHugh Dickins 	ssize_t retval;
1323bcd78e49SHugh Dickins 	unsigned long seg;
1324bcd78e49SHugh Dickins 	size_t count;
1325bcd78e49SHugh Dickins 	loff_t *ppos = &iocb->ki_pos;
1326bcd78e49SHugh Dickins 
1327bcd78e49SHugh Dickins 	retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1328bcd78e49SHugh Dickins 	if (retval)
1329bcd78e49SHugh Dickins 		return retval;
1330bcd78e49SHugh Dickins 
1331bcd78e49SHugh Dickins 	for (seg = 0; seg < nr_segs; seg++) {
13321da177e4SLinus Torvalds 		read_descriptor_t desc;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 		desc.written = 0;
1335bcd78e49SHugh Dickins 		desc.arg.buf = iov[seg].iov_base;
1336bcd78e49SHugh Dickins 		desc.count = iov[seg].iov_len;
1337bcd78e49SHugh Dickins 		if (desc.count == 0)
1338bcd78e49SHugh Dickins 			continue;
13391da177e4SLinus Torvalds 		desc.error = 0;
13401da177e4SLinus Torvalds 		do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1341bcd78e49SHugh Dickins 		retval += desc.written;
1342bcd78e49SHugh Dickins 		if (desc.error) {
1343bcd78e49SHugh Dickins 			retval = retval ?: desc.error;
1344bcd78e49SHugh Dickins 			break;
1345bcd78e49SHugh Dickins 		}
1346bcd78e49SHugh Dickins 		if (desc.count > 0)
1347bcd78e49SHugh Dickins 			break;
1348bcd78e49SHugh Dickins 	}
1349bcd78e49SHugh Dickins 	return retval;
13501da177e4SLinus Torvalds }
13511da177e4SLinus Torvalds 
1352708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1353708e3508SHugh Dickins 				struct pipe_inode_info *pipe, size_t len,
1354708e3508SHugh Dickins 				unsigned int flags)
1355708e3508SHugh Dickins {
1356708e3508SHugh Dickins 	struct address_space *mapping = in->f_mapping;
135771f0e07aSHugh Dickins 	struct inode *inode = mapping->host;
1358708e3508SHugh Dickins 	unsigned int loff, nr_pages, req_pages;
1359708e3508SHugh Dickins 	struct page *pages[PIPE_DEF_BUFFERS];
1360708e3508SHugh Dickins 	struct partial_page partial[PIPE_DEF_BUFFERS];
1361708e3508SHugh Dickins 	struct page *page;
1362708e3508SHugh Dickins 	pgoff_t index, end_index;
1363708e3508SHugh Dickins 	loff_t isize, left;
1364708e3508SHugh Dickins 	int error, page_nr;
1365708e3508SHugh Dickins 	struct splice_pipe_desc spd = {
1366708e3508SHugh Dickins 		.pages = pages,
1367708e3508SHugh Dickins 		.partial = partial,
1368708e3508SHugh Dickins 		.flags = flags,
1369708e3508SHugh Dickins 		.ops = &page_cache_pipe_buf_ops,
1370708e3508SHugh Dickins 		.spd_release = spd_release_page,
1371708e3508SHugh Dickins 	};
1372708e3508SHugh Dickins 
137371f0e07aSHugh Dickins 	isize = i_size_read(inode);
1374708e3508SHugh Dickins 	if (unlikely(*ppos >= isize))
1375708e3508SHugh Dickins 		return 0;
1376708e3508SHugh Dickins 
1377708e3508SHugh Dickins 	left = isize - *ppos;
1378708e3508SHugh Dickins 	if (unlikely(left < len))
1379708e3508SHugh Dickins 		len = left;
1380708e3508SHugh Dickins 
1381708e3508SHugh Dickins 	if (splice_grow_spd(pipe, &spd))
1382708e3508SHugh Dickins 		return -ENOMEM;
1383708e3508SHugh Dickins 
1384708e3508SHugh Dickins 	index = *ppos >> PAGE_CACHE_SHIFT;
1385708e3508SHugh Dickins 	loff = *ppos & ~PAGE_CACHE_MASK;
1386708e3508SHugh Dickins 	req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1387708e3508SHugh Dickins 	nr_pages = min(req_pages, pipe->buffers);
1388708e3508SHugh Dickins 
1389708e3508SHugh Dickins 	spd.nr_pages = find_get_pages_contig(mapping, index,
1390708e3508SHugh Dickins 						nr_pages, spd.pages);
1391708e3508SHugh Dickins 	index += spd.nr_pages;
1392708e3508SHugh Dickins 	error = 0;
139371f0e07aSHugh Dickins 
1394708e3508SHugh Dickins 	while (spd.nr_pages < nr_pages) {
139571f0e07aSHugh Dickins 		error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
139671f0e07aSHugh Dickins 		if (error)
1397708e3508SHugh Dickins 			break;
1398708e3508SHugh Dickins 		unlock_page(page);
1399708e3508SHugh Dickins 		spd.pages[spd.nr_pages++] = page;
1400708e3508SHugh Dickins 		index++;
1401708e3508SHugh Dickins 	}
1402708e3508SHugh Dickins 
1403708e3508SHugh Dickins 	index = *ppos >> PAGE_CACHE_SHIFT;
1404708e3508SHugh Dickins 	nr_pages = spd.nr_pages;
1405708e3508SHugh Dickins 	spd.nr_pages = 0;
140671f0e07aSHugh Dickins 
1407708e3508SHugh Dickins 	for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1408708e3508SHugh Dickins 		unsigned int this_len;
1409708e3508SHugh Dickins 
1410708e3508SHugh Dickins 		if (!len)
1411708e3508SHugh Dickins 			break;
1412708e3508SHugh Dickins 
1413708e3508SHugh Dickins 		this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1414708e3508SHugh Dickins 		page = spd.pages[page_nr];
1415708e3508SHugh Dickins 
141671f0e07aSHugh Dickins 		if (!PageUptodate(page) || page->mapping != mapping) {
141771f0e07aSHugh Dickins 			error = shmem_getpage(inode, index, &page,
141871f0e07aSHugh Dickins 							SGP_CACHE, NULL);
141971f0e07aSHugh Dickins 			if (error)
1420708e3508SHugh Dickins 				break;
142171f0e07aSHugh Dickins 			unlock_page(page);
1422708e3508SHugh Dickins 			page_cache_release(spd.pages[page_nr]);
1423708e3508SHugh Dickins 			spd.pages[page_nr] = page;
1424708e3508SHugh Dickins 		}
1425708e3508SHugh Dickins 
142671f0e07aSHugh Dickins 		isize = i_size_read(inode);
1427708e3508SHugh Dickins 		end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1428708e3508SHugh Dickins 		if (unlikely(!isize || index > end_index))
1429708e3508SHugh Dickins 			break;
1430708e3508SHugh Dickins 
1431708e3508SHugh Dickins 		if (end_index == index) {
1432708e3508SHugh Dickins 			unsigned int plen;
1433708e3508SHugh Dickins 
1434708e3508SHugh Dickins 			plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1435708e3508SHugh Dickins 			if (plen <= loff)
1436708e3508SHugh Dickins 				break;
1437708e3508SHugh Dickins 
1438708e3508SHugh Dickins 			this_len = min(this_len, plen - loff);
1439708e3508SHugh Dickins 			len = this_len;
1440708e3508SHugh Dickins 		}
1441708e3508SHugh Dickins 
1442708e3508SHugh Dickins 		spd.partial[page_nr].offset = loff;
1443708e3508SHugh Dickins 		spd.partial[page_nr].len = this_len;
1444708e3508SHugh Dickins 		len -= this_len;
1445708e3508SHugh Dickins 		loff = 0;
1446708e3508SHugh Dickins 		spd.nr_pages++;
1447708e3508SHugh Dickins 		index++;
1448708e3508SHugh Dickins 	}
1449708e3508SHugh Dickins 
1450708e3508SHugh Dickins 	while (page_nr < nr_pages)
1451708e3508SHugh Dickins 		page_cache_release(spd.pages[page_nr++]);
1452708e3508SHugh Dickins 
1453708e3508SHugh Dickins 	if (spd.nr_pages)
1454708e3508SHugh Dickins 		error = splice_to_pipe(pipe, &spd);
1455708e3508SHugh Dickins 
1456708e3508SHugh Dickins 	splice_shrink_spd(pipe, &spd);
1457708e3508SHugh Dickins 
1458708e3508SHugh Dickins 	if (error > 0) {
1459708e3508SHugh Dickins 		*ppos += error;
1460708e3508SHugh Dickins 		file_accessed(in);
1461708e3508SHugh Dickins 	}
1462708e3508SHugh Dickins 	return error;
1463708e3508SHugh Dickins }
1464708e3508SHugh Dickins 
1465726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
14661da177e4SLinus Torvalds {
1467726c3342SDavid Howells 	struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	buf->f_type = TMPFS_MAGIC;
14701da177e4SLinus Torvalds 	buf->f_bsize = PAGE_CACHE_SIZE;
14711da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
14720edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
14731da177e4SLinus Torvalds 		buf->f_blocks = sbinfo->max_blocks;
147441ffe5d5SHugh Dickins 		buf->f_bavail =
147541ffe5d5SHugh Dickins 		buf->f_bfree  = sbinfo->max_blocks -
147641ffe5d5SHugh Dickins 				percpu_counter_sum(&sbinfo->used_blocks);
14770edd73b3SHugh Dickins 	}
14780edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
14791da177e4SLinus Torvalds 		buf->f_files = sbinfo->max_inodes;
14801da177e4SLinus Torvalds 		buf->f_ffree = sbinfo->free_inodes;
14811da177e4SLinus Torvalds 	}
14821da177e4SLinus Torvalds 	/* else leave those fields 0 like simple_statfs */
14831da177e4SLinus Torvalds 	return 0;
14841da177e4SLinus Torvalds }
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds /*
14871da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
14881da177e4SLinus Torvalds  */
14891da177e4SLinus Torvalds static int
14901a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
14911da177e4SLinus Torvalds {
14920b0a0806SHugh Dickins 	struct inode *inode;
14931da177e4SLinus Torvalds 	int error = -ENOSPC;
14941da177e4SLinus Torvalds 
1495454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
14961da177e4SLinus Torvalds 	if (inode) {
14972a7dba39SEric Paris 		error = security_inode_init_security(inode, dir,
14989d8f13baSMimi Zohar 						     &dentry->d_name,
14996d9d88d0SJarkko Sakkinen 						     shmem_initxattrs, NULL);
1500570bc1c2SStephen Smalley 		if (error) {
1501570bc1c2SStephen Smalley 			if (error != -EOPNOTSUPP) {
1502570bc1c2SStephen Smalley 				iput(inode);
1503570bc1c2SStephen Smalley 				return error;
1504570bc1c2SStephen Smalley 			}
150539f0247dSAndreas Gruenbacher 		}
15061c7c474cSChristoph Hellwig #ifdef CONFIG_TMPFS_POSIX_ACL
15071c7c474cSChristoph Hellwig 		error = generic_acl_init(inode, dir);
150839f0247dSAndreas Gruenbacher 		if (error) {
150939f0247dSAndreas Gruenbacher 			iput(inode);
151039f0247dSAndreas Gruenbacher 			return error;
1511570bc1c2SStephen Smalley 		}
1512718deb6bSAl Viro #else
1513718deb6bSAl Viro 		error = 0;
15141c7c474cSChristoph Hellwig #endif
15151da177e4SLinus Torvalds 		dir->i_size += BOGO_DIRENT_SIZE;
15161da177e4SLinus Torvalds 		dir->i_ctime = dir->i_mtime = CURRENT_TIME;
15171da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
15181da177e4SLinus Torvalds 		dget(dentry); /* Extra count - pin the dentry in core */
15191da177e4SLinus Torvalds 	}
15201da177e4SLinus Torvalds 	return error;
15211da177e4SLinus Torvalds }
15221da177e4SLinus Torvalds 
152318bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
15241da177e4SLinus Torvalds {
15251da177e4SLinus Torvalds 	int error;
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
15281da177e4SLinus Torvalds 		return error;
1529d8c76e6fSDave Hansen 	inc_nlink(dir);
15301da177e4SLinus Torvalds 	return 0;
15311da177e4SLinus Torvalds }
15321da177e4SLinus Torvalds 
15334acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
15341da177e4SLinus Torvalds 		struct nameidata *nd)
15351da177e4SLinus Torvalds {
15361da177e4SLinus Torvalds 	return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
15371da177e4SLinus Torvalds }
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds /*
15401da177e4SLinus Torvalds  * Link a file..
15411da177e4SLinus Torvalds  */
15421da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
15431da177e4SLinus Torvalds {
15441da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
15455b04c689SPavel Emelyanov 	int ret;
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	/*
15481da177e4SLinus Torvalds 	 * No ordinary (disk based) filesystem counts links as inodes;
15491da177e4SLinus Torvalds 	 * but each new link needs a new dentry, pinning lowmem, and
15501da177e4SLinus Torvalds 	 * tmpfs dentries cannot be pruned until they are unlinked.
15511da177e4SLinus Torvalds 	 */
15525b04c689SPavel Emelyanov 	ret = shmem_reserve_inode(inode->i_sb);
15535b04c689SPavel Emelyanov 	if (ret)
15545b04c689SPavel Emelyanov 		goto out;
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
15571da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1558d8c76e6fSDave Hansen 	inc_nlink(inode);
15597de9c6eeSAl Viro 	ihold(inode);	/* New dentry reference */
15601da177e4SLinus Torvalds 	dget(dentry);		/* Extra pinning count for the created dentry */
15611da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
15625b04c689SPavel Emelyanov out:
15635b04c689SPavel Emelyanov 	return ret;
15641da177e4SLinus Torvalds }
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry)
15671da177e4SLinus Torvalds {
15681da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
15691da177e4SLinus Torvalds 
15705b04c689SPavel Emelyanov 	if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
15715b04c689SPavel Emelyanov 		shmem_free_inode(inode->i_sb);
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	dir->i_size -= BOGO_DIRENT_SIZE;
15741da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
15759a53c3a7SDave Hansen 	drop_nlink(inode);
15761da177e4SLinus Torvalds 	dput(dentry);	/* Undo the count from "create" - this does all the work */
15771da177e4SLinus Torvalds 	return 0;
15781da177e4SLinus Torvalds }
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
15811da177e4SLinus Torvalds {
15821da177e4SLinus Torvalds 	if (!simple_empty(dentry))
15831da177e4SLinus Torvalds 		return -ENOTEMPTY;
15841da177e4SLinus Torvalds 
15859a53c3a7SDave Hansen 	drop_nlink(dentry->d_inode);
15869a53c3a7SDave Hansen 	drop_nlink(dir);
15871da177e4SLinus Torvalds 	return shmem_unlink(dir, dentry);
15881da177e4SLinus Torvalds }
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds /*
15911da177e4SLinus Torvalds  * The VFS layer already does all the dentry stuff for rename,
15921da177e4SLinus Torvalds  * we just have to decrement the usage count for the target if
15931da177e4SLinus Torvalds  * it exists so that the VFS layer correctly free's it when it
15941da177e4SLinus Torvalds  * gets overwritten.
15951da177e4SLinus Torvalds  */
15961da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
15971da177e4SLinus Torvalds {
15981da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
15991da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(inode->i_mode);
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
16021da177e4SLinus Torvalds 		return -ENOTEMPTY;
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
16051da177e4SLinus Torvalds 		(void) shmem_unlink(new_dir, new_dentry);
16061da177e4SLinus Torvalds 		if (they_are_dirs)
16079a53c3a7SDave Hansen 			drop_nlink(old_dir);
16081da177e4SLinus Torvalds 	} else if (they_are_dirs) {
16099a53c3a7SDave Hansen 		drop_nlink(old_dir);
1610d8c76e6fSDave Hansen 		inc_nlink(new_dir);
16111da177e4SLinus Torvalds 	}
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds 	old_dir->i_size -= BOGO_DIRENT_SIZE;
16141da177e4SLinus Torvalds 	new_dir->i_size += BOGO_DIRENT_SIZE;
16151da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime =
16161da177e4SLinus Torvalds 	new_dir->i_ctime = new_dir->i_mtime =
16171da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME;
16181da177e4SLinus Torvalds 	return 0;
16191da177e4SLinus Torvalds }
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
16221da177e4SLinus Torvalds {
16231da177e4SLinus Torvalds 	int error;
16241da177e4SLinus Torvalds 	int len;
16251da177e4SLinus Torvalds 	struct inode *inode;
16269276aad6SHugh Dickins 	struct page *page;
16271da177e4SLinus Torvalds 	char *kaddr;
16281da177e4SLinus Torvalds 	struct shmem_inode_info *info;
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 	len = strlen(symname) + 1;
16311da177e4SLinus Torvalds 	if (len > PAGE_CACHE_SIZE)
16321da177e4SLinus Torvalds 		return -ENAMETOOLONG;
16331da177e4SLinus Torvalds 
1634454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
16351da177e4SLinus Torvalds 	if (!inode)
16361da177e4SLinus Torvalds 		return -ENOSPC;
16371da177e4SLinus Torvalds 
16389d8f13baSMimi Zohar 	error = security_inode_init_security(inode, dir, &dentry->d_name,
16396d9d88d0SJarkko Sakkinen 					     shmem_initxattrs, NULL);
1640570bc1c2SStephen Smalley 	if (error) {
1641570bc1c2SStephen Smalley 		if (error != -EOPNOTSUPP) {
1642570bc1c2SStephen Smalley 			iput(inode);
1643570bc1c2SStephen Smalley 			return error;
1644570bc1c2SStephen Smalley 		}
1645570bc1c2SStephen Smalley 		error = 0;
1646570bc1c2SStephen Smalley 	}
1647570bc1c2SStephen Smalley 
16481da177e4SLinus Torvalds 	info = SHMEM_I(inode);
16491da177e4SLinus Torvalds 	inode->i_size = len-1;
165069f07ec9SHugh Dickins 	if (len <= SHORT_SYMLINK_LEN) {
165169f07ec9SHugh Dickins 		info->symlink = kmemdup(symname, len, GFP_KERNEL);
165269f07ec9SHugh Dickins 		if (!info->symlink) {
165369f07ec9SHugh Dickins 			iput(inode);
165469f07ec9SHugh Dickins 			return -ENOMEM;
165569f07ec9SHugh Dickins 		}
165669f07ec9SHugh Dickins 		inode->i_op = &shmem_short_symlink_operations;
16571da177e4SLinus Torvalds 	} else {
16581da177e4SLinus Torvalds 		error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
16591da177e4SLinus Torvalds 		if (error) {
16601da177e4SLinus Torvalds 			iput(inode);
16611da177e4SLinus Torvalds 			return error;
16621da177e4SLinus Torvalds 		}
166314fcc23fSHugh Dickins 		inode->i_mapping->a_ops = &shmem_aops;
16641da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inode_operations;
16659b04c5feSCong Wang 		kaddr = kmap_atomic(page);
16661da177e4SLinus Torvalds 		memcpy(kaddr, symname, len);
16679b04c5feSCong Wang 		kunmap_atomic(kaddr);
16681da177e4SLinus Torvalds 		set_page_dirty(page);
16696746aff7SWu Fengguang 		unlock_page(page);
16701da177e4SLinus Torvalds 		page_cache_release(page);
16711da177e4SLinus Torvalds 	}
16721da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
16731da177e4SLinus Torvalds 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
16741da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
16751da177e4SLinus Torvalds 	dget(dentry);
16761da177e4SLinus Torvalds 	return 0;
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds 
167969f07ec9SHugh Dickins static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
16801da177e4SLinus Torvalds {
168169f07ec9SHugh Dickins 	nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
1682cc314eefSLinus Torvalds 	return NULL;
16831da177e4SLinus Torvalds }
16841da177e4SLinus Torvalds 
1685cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
16861da177e4SLinus Torvalds {
16871da177e4SLinus Torvalds 	struct page *page = NULL;
168841ffe5d5SHugh Dickins 	int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
168941ffe5d5SHugh Dickins 	nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
1690d3602444SHugh Dickins 	if (page)
1691d3602444SHugh Dickins 		unlock_page(page);
1692cc314eefSLinus Torvalds 	return page;
16931da177e4SLinus Torvalds }
16941da177e4SLinus Torvalds 
1695cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
16961da177e4SLinus Torvalds {
16971da177e4SLinus Torvalds 	if (!IS_ERR(nd_get_link(nd))) {
1698cc314eefSLinus Torvalds 		struct page *page = cookie;
16991da177e4SLinus Torvalds 		kunmap(page);
17001da177e4SLinus Torvalds 		mark_page_accessed(page);
17011da177e4SLinus Torvalds 		page_cache_release(page);
17021da177e4SLinus Torvalds 	}
17031da177e4SLinus Torvalds }
17041da177e4SLinus Torvalds 
1705b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
1706b09e0fa4SEric Paris /*
1707b09e0fa4SEric Paris  * Superblocks without xattr inode operations may get some security.* xattr
1708b09e0fa4SEric Paris  * support from the LSM "for free". As soon as we have any other xattrs
1709b09e0fa4SEric Paris  * like ACLs, we also need to implement the security.* handlers at
1710b09e0fa4SEric Paris  * filesystem level, though.
1711b09e0fa4SEric Paris  */
1712b09e0fa4SEric Paris 
17136d9d88d0SJarkko Sakkinen /*
17146d9d88d0SJarkko Sakkinen  * Allocate new xattr and copy in the value; but leave the name to callers.
17156d9d88d0SJarkko Sakkinen  */
17166d9d88d0SJarkko Sakkinen static struct shmem_xattr *shmem_xattr_alloc(const void *value, size_t size)
17176d9d88d0SJarkko Sakkinen {
17186d9d88d0SJarkko Sakkinen 	struct shmem_xattr *new_xattr;
17196d9d88d0SJarkko Sakkinen 	size_t len;
17206d9d88d0SJarkko Sakkinen 
17216d9d88d0SJarkko Sakkinen 	/* wrap around? */
17226d9d88d0SJarkko Sakkinen 	len = sizeof(*new_xattr) + size;
17236d9d88d0SJarkko Sakkinen 	if (len <= sizeof(*new_xattr))
17246d9d88d0SJarkko Sakkinen 		return NULL;
17256d9d88d0SJarkko Sakkinen 
17266d9d88d0SJarkko Sakkinen 	new_xattr = kmalloc(len, GFP_KERNEL);
17276d9d88d0SJarkko Sakkinen 	if (!new_xattr)
17286d9d88d0SJarkko Sakkinen 		return NULL;
17296d9d88d0SJarkko Sakkinen 
17306d9d88d0SJarkko Sakkinen 	new_xattr->size = size;
17316d9d88d0SJarkko Sakkinen 	memcpy(new_xattr->value, value, size);
17326d9d88d0SJarkko Sakkinen 	return new_xattr;
17336d9d88d0SJarkko Sakkinen }
17346d9d88d0SJarkko Sakkinen 
17356d9d88d0SJarkko Sakkinen /*
17366d9d88d0SJarkko Sakkinen  * Callback for security_inode_init_security() for acquiring xattrs.
17376d9d88d0SJarkko Sakkinen  */
17386d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode,
17396d9d88d0SJarkko Sakkinen 			    const struct xattr *xattr_array,
17406d9d88d0SJarkko Sakkinen 			    void *fs_info)
17416d9d88d0SJarkko Sakkinen {
17426d9d88d0SJarkko Sakkinen 	struct shmem_inode_info *info = SHMEM_I(inode);
17436d9d88d0SJarkko Sakkinen 	const struct xattr *xattr;
17446d9d88d0SJarkko Sakkinen 	struct shmem_xattr *new_xattr;
17456d9d88d0SJarkko Sakkinen 	size_t len;
17466d9d88d0SJarkko Sakkinen 
17476d9d88d0SJarkko Sakkinen 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
17486d9d88d0SJarkko Sakkinen 		new_xattr = shmem_xattr_alloc(xattr->value, xattr->value_len);
17496d9d88d0SJarkko Sakkinen 		if (!new_xattr)
17506d9d88d0SJarkko Sakkinen 			return -ENOMEM;
17516d9d88d0SJarkko Sakkinen 
17526d9d88d0SJarkko Sakkinen 		len = strlen(xattr->name) + 1;
17536d9d88d0SJarkko Sakkinen 		new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
17546d9d88d0SJarkko Sakkinen 					  GFP_KERNEL);
17556d9d88d0SJarkko Sakkinen 		if (!new_xattr->name) {
17566d9d88d0SJarkko Sakkinen 			kfree(new_xattr);
17576d9d88d0SJarkko Sakkinen 			return -ENOMEM;
17586d9d88d0SJarkko Sakkinen 		}
17596d9d88d0SJarkko Sakkinen 
17606d9d88d0SJarkko Sakkinen 		memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
17616d9d88d0SJarkko Sakkinen 		       XATTR_SECURITY_PREFIX_LEN);
17626d9d88d0SJarkko Sakkinen 		memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
17636d9d88d0SJarkko Sakkinen 		       xattr->name, len);
17646d9d88d0SJarkko Sakkinen 
17656d9d88d0SJarkko Sakkinen 		spin_lock(&info->lock);
17666d9d88d0SJarkko Sakkinen 		list_add(&new_xattr->list, &info->xattr_list);
17676d9d88d0SJarkko Sakkinen 		spin_unlock(&info->lock);
17686d9d88d0SJarkko Sakkinen 	}
17696d9d88d0SJarkko Sakkinen 
17706d9d88d0SJarkko Sakkinen 	return 0;
17716d9d88d0SJarkko Sakkinen }
17726d9d88d0SJarkko Sakkinen 
1773b09e0fa4SEric Paris static int shmem_xattr_get(struct dentry *dentry, const char *name,
1774b09e0fa4SEric Paris 			   void *buffer, size_t size)
1775b09e0fa4SEric Paris {
1776b09e0fa4SEric Paris 	struct shmem_inode_info *info;
1777b09e0fa4SEric Paris 	struct shmem_xattr *xattr;
1778b09e0fa4SEric Paris 	int ret = -ENODATA;
1779b09e0fa4SEric Paris 
1780b09e0fa4SEric Paris 	info = SHMEM_I(dentry->d_inode);
1781b09e0fa4SEric Paris 
1782b09e0fa4SEric Paris 	spin_lock(&info->lock);
1783b09e0fa4SEric Paris 	list_for_each_entry(xattr, &info->xattr_list, list) {
1784b09e0fa4SEric Paris 		if (strcmp(name, xattr->name))
1785b09e0fa4SEric Paris 			continue;
1786b09e0fa4SEric Paris 
1787b09e0fa4SEric Paris 		ret = xattr->size;
1788b09e0fa4SEric Paris 		if (buffer) {
1789b09e0fa4SEric Paris 			if (size < xattr->size)
1790b09e0fa4SEric Paris 				ret = -ERANGE;
1791b09e0fa4SEric Paris 			else
1792b09e0fa4SEric Paris 				memcpy(buffer, xattr->value, xattr->size);
1793b09e0fa4SEric Paris 		}
1794b09e0fa4SEric Paris 		break;
1795b09e0fa4SEric Paris 	}
1796b09e0fa4SEric Paris 	spin_unlock(&info->lock);
1797b09e0fa4SEric Paris 	return ret;
1798b09e0fa4SEric Paris }
1799b09e0fa4SEric Paris 
18006d9d88d0SJarkko Sakkinen static int shmem_xattr_set(struct inode *inode, const char *name,
1801b09e0fa4SEric Paris 			   const void *value, size_t size, int flags)
1802b09e0fa4SEric Paris {
1803b09e0fa4SEric Paris 	struct shmem_inode_info *info = SHMEM_I(inode);
1804b09e0fa4SEric Paris 	struct shmem_xattr *xattr;
1805b09e0fa4SEric Paris 	struct shmem_xattr *new_xattr = NULL;
1806b09e0fa4SEric Paris 	int err = 0;
1807b09e0fa4SEric Paris 
1808b09e0fa4SEric Paris 	/* value == NULL means remove */
1809b09e0fa4SEric Paris 	if (value) {
18106d9d88d0SJarkko Sakkinen 		new_xattr = shmem_xattr_alloc(value, size);
1811b09e0fa4SEric Paris 		if (!new_xattr)
1812b09e0fa4SEric Paris 			return -ENOMEM;
1813b09e0fa4SEric Paris 
1814b09e0fa4SEric Paris 		new_xattr->name = kstrdup(name, GFP_KERNEL);
1815b09e0fa4SEric Paris 		if (!new_xattr->name) {
1816b09e0fa4SEric Paris 			kfree(new_xattr);
1817b09e0fa4SEric Paris 			return -ENOMEM;
1818b09e0fa4SEric Paris 		}
1819b09e0fa4SEric Paris 	}
1820b09e0fa4SEric Paris 
1821b09e0fa4SEric Paris 	spin_lock(&info->lock);
1822b09e0fa4SEric Paris 	list_for_each_entry(xattr, &info->xattr_list, list) {
1823b09e0fa4SEric Paris 		if (!strcmp(name, xattr->name)) {
1824b09e0fa4SEric Paris 			if (flags & XATTR_CREATE) {
1825b09e0fa4SEric Paris 				xattr = new_xattr;
1826b09e0fa4SEric Paris 				err = -EEXIST;
1827b09e0fa4SEric Paris 			} else if (new_xattr) {
1828b09e0fa4SEric Paris 				list_replace(&xattr->list, &new_xattr->list);
1829b09e0fa4SEric Paris 			} else {
1830b09e0fa4SEric Paris 				list_del(&xattr->list);
1831b09e0fa4SEric Paris 			}
1832b09e0fa4SEric Paris 			goto out;
1833b09e0fa4SEric Paris 		}
1834b09e0fa4SEric Paris 	}
1835b09e0fa4SEric Paris 	if (flags & XATTR_REPLACE) {
1836b09e0fa4SEric Paris 		xattr = new_xattr;
1837b09e0fa4SEric Paris 		err = -ENODATA;
1838b09e0fa4SEric Paris 	} else {
1839b09e0fa4SEric Paris 		list_add(&new_xattr->list, &info->xattr_list);
1840b09e0fa4SEric Paris 		xattr = NULL;
1841b09e0fa4SEric Paris 	}
1842b09e0fa4SEric Paris out:
1843b09e0fa4SEric Paris 	spin_unlock(&info->lock);
1844b09e0fa4SEric Paris 	if (xattr)
1845b09e0fa4SEric Paris 		kfree(xattr->name);
1846b09e0fa4SEric Paris 	kfree(xattr);
1847b09e0fa4SEric Paris 	return err;
1848b09e0fa4SEric Paris }
1849b09e0fa4SEric Paris 
1850b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = {
1851b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
1852b09e0fa4SEric Paris 	&generic_acl_access_handler,
1853b09e0fa4SEric Paris 	&generic_acl_default_handler,
1854b09e0fa4SEric Paris #endif
1855b09e0fa4SEric Paris 	NULL
1856b09e0fa4SEric Paris };
1857b09e0fa4SEric Paris 
1858b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name)
1859b09e0fa4SEric Paris {
1860b09e0fa4SEric Paris 	struct { const char *prefix; size_t len; } arr[] = {
1861b09e0fa4SEric Paris 		{ XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1862b09e0fa4SEric Paris 		{ XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1863b09e0fa4SEric Paris 	};
1864b09e0fa4SEric Paris 	int i;
1865b09e0fa4SEric Paris 
1866b09e0fa4SEric Paris 	for (i = 0; i < ARRAY_SIZE(arr); i++) {
1867b09e0fa4SEric Paris 		size_t preflen = arr[i].len;
1868b09e0fa4SEric Paris 		if (strncmp(name, arr[i].prefix, preflen) == 0) {
1869b09e0fa4SEric Paris 			if (!name[preflen])
1870b09e0fa4SEric Paris 				return -EINVAL;
1871b09e0fa4SEric Paris 			return 0;
1872b09e0fa4SEric Paris 		}
1873b09e0fa4SEric Paris 	}
1874b09e0fa4SEric Paris 	return -EOPNOTSUPP;
1875b09e0fa4SEric Paris }
1876b09e0fa4SEric Paris 
1877b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1878b09e0fa4SEric Paris 			      void *buffer, size_t size)
1879b09e0fa4SEric Paris {
1880b09e0fa4SEric Paris 	int err;
1881b09e0fa4SEric Paris 
1882b09e0fa4SEric Paris 	/*
1883b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
1884b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
1885b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
1886b09e0fa4SEric Paris 	 */
1887b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1888b09e0fa4SEric Paris 		return generic_getxattr(dentry, name, buffer, size);
1889b09e0fa4SEric Paris 
1890b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
1891b09e0fa4SEric Paris 	if (err)
1892b09e0fa4SEric Paris 		return err;
1893b09e0fa4SEric Paris 
1894b09e0fa4SEric Paris 	return shmem_xattr_get(dentry, name, buffer, size);
1895b09e0fa4SEric Paris }
1896b09e0fa4SEric Paris 
1897b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name,
1898b09e0fa4SEric Paris 			  const void *value, size_t size, int flags)
1899b09e0fa4SEric Paris {
1900b09e0fa4SEric Paris 	int err;
1901b09e0fa4SEric Paris 
1902b09e0fa4SEric Paris 	/*
1903b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
1904b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
1905b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
1906b09e0fa4SEric Paris 	 */
1907b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1908b09e0fa4SEric Paris 		return generic_setxattr(dentry, name, value, size, flags);
1909b09e0fa4SEric Paris 
1910b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
1911b09e0fa4SEric Paris 	if (err)
1912b09e0fa4SEric Paris 		return err;
1913b09e0fa4SEric Paris 
1914b09e0fa4SEric Paris 	if (size == 0)
1915b09e0fa4SEric Paris 		value = "";  /* empty EA, do not remove */
1916b09e0fa4SEric Paris 
19176d9d88d0SJarkko Sakkinen 	return shmem_xattr_set(dentry->d_inode, name, value, size, flags);
1918b09e0fa4SEric Paris 
1919b09e0fa4SEric Paris }
1920b09e0fa4SEric Paris 
1921b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name)
1922b09e0fa4SEric Paris {
1923b09e0fa4SEric Paris 	int err;
1924b09e0fa4SEric Paris 
1925b09e0fa4SEric Paris 	/*
1926b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
1927b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
1928b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
1929b09e0fa4SEric Paris 	 */
1930b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1931b09e0fa4SEric Paris 		return generic_removexattr(dentry, name);
1932b09e0fa4SEric Paris 
1933b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
1934b09e0fa4SEric Paris 	if (err)
1935b09e0fa4SEric Paris 		return err;
1936b09e0fa4SEric Paris 
19376d9d88d0SJarkko Sakkinen 	return shmem_xattr_set(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
1938b09e0fa4SEric Paris }
1939b09e0fa4SEric Paris 
1940b09e0fa4SEric Paris static bool xattr_is_trusted(const char *name)
1941b09e0fa4SEric Paris {
1942b09e0fa4SEric Paris 	return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1943b09e0fa4SEric Paris }
1944b09e0fa4SEric Paris 
1945b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
1946b09e0fa4SEric Paris {
1947b09e0fa4SEric Paris 	bool trusted = capable(CAP_SYS_ADMIN);
1948b09e0fa4SEric Paris 	struct shmem_xattr *xattr;
1949b09e0fa4SEric Paris 	struct shmem_inode_info *info;
1950b09e0fa4SEric Paris 	size_t used = 0;
1951b09e0fa4SEric Paris 
1952b09e0fa4SEric Paris 	info = SHMEM_I(dentry->d_inode);
1953b09e0fa4SEric Paris 
1954b09e0fa4SEric Paris 	spin_lock(&info->lock);
1955b09e0fa4SEric Paris 	list_for_each_entry(xattr, &info->xattr_list, list) {
1956b09e0fa4SEric Paris 		size_t len;
1957b09e0fa4SEric Paris 
1958b09e0fa4SEric Paris 		/* skip "trusted." attributes for unprivileged callers */
1959b09e0fa4SEric Paris 		if (!trusted && xattr_is_trusted(xattr->name))
1960b09e0fa4SEric Paris 			continue;
1961b09e0fa4SEric Paris 
1962b09e0fa4SEric Paris 		len = strlen(xattr->name) + 1;
1963b09e0fa4SEric Paris 		used += len;
1964b09e0fa4SEric Paris 		if (buffer) {
1965b09e0fa4SEric Paris 			if (size < used) {
1966b09e0fa4SEric Paris 				used = -ERANGE;
1967b09e0fa4SEric Paris 				break;
1968b09e0fa4SEric Paris 			}
1969b09e0fa4SEric Paris 			memcpy(buffer, xattr->name, len);
1970b09e0fa4SEric Paris 			buffer += len;
1971b09e0fa4SEric Paris 		}
1972b09e0fa4SEric Paris 	}
1973b09e0fa4SEric Paris 	spin_unlock(&info->lock);
1974b09e0fa4SEric Paris 
1975b09e0fa4SEric Paris 	return used;
1976b09e0fa4SEric Paris }
1977b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */
1978b09e0fa4SEric Paris 
197969f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = {
19801da177e4SLinus Torvalds 	.readlink	= generic_readlink,
198169f07ec9SHugh Dickins 	.follow_link	= shmem_follow_short_symlink,
1982b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
1983b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
1984b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
1985b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
1986b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
1987b09e0fa4SEric Paris #endif
19881da177e4SLinus Torvalds };
19891da177e4SLinus Torvalds 
199092e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = {
19911da177e4SLinus Torvalds 	.readlink	= generic_readlink,
19921da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link,
19931da177e4SLinus Torvalds 	.put_link	= shmem_put_link,
1994b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
1995b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
1996b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
1997b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
1998b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
199939f0247dSAndreas Gruenbacher #endif
2000b09e0fa4SEric Paris };
200139f0247dSAndreas Gruenbacher 
200291828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child)
200391828a40SDavid M. Grimes {
200491828a40SDavid M. Grimes 	return ERR_PTR(-ESTALE);
200591828a40SDavid M. Grimes }
200691828a40SDavid M. Grimes 
200791828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh)
200891828a40SDavid M. Grimes {
200991828a40SDavid M. Grimes 	__u32 *fh = vfh;
201091828a40SDavid M. Grimes 	__u64 inum = fh[2];
201191828a40SDavid M. Grimes 	inum = (inum << 32) | fh[1];
201291828a40SDavid M. Grimes 	return ino->i_ino == inum && fh[0] == ino->i_generation;
201391828a40SDavid M. Grimes }
201491828a40SDavid M. Grimes 
2015480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2016480b116cSChristoph Hellwig 		struct fid *fid, int fh_len, int fh_type)
201791828a40SDavid M. Grimes {
201891828a40SDavid M. Grimes 	struct inode *inode;
2019480b116cSChristoph Hellwig 	struct dentry *dentry = NULL;
2020480b116cSChristoph Hellwig 	u64 inum = fid->raw[2];
2021480b116cSChristoph Hellwig 	inum = (inum << 32) | fid->raw[1];
202291828a40SDavid M. Grimes 
2023480b116cSChristoph Hellwig 	if (fh_len < 3)
2024480b116cSChristoph Hellwig 		return NULL;
2025480b116cSChristoph Hellwig 
2026480b116cSChristoph Hellwig 	inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2027480b116cSChristoph Hellwig 			shmem_match, fid->raw);
202891828a40SDavid M. Grimes 	if (inode) {
2029480b116cSChristoph Hellwig 		dentry = d_find_alias(inode);
203091828a40SDavid M. Grimes 		iput(inode);
203191828a40SDavid M. Grimes 	}
203291828a40SDavid M. Grimes 
2033480b116cSChristoph Hellwig 	return dentry;
203491828a40SDavid M. Grimes }
203591828a40SDavid M. Grimes 
2036*b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
2037*b0b0382bSAl Viro 				struct inode *parent)
203891828a40SDavid M. Grimes {
20395fe0c237SAneesh Kumar K.V 	if (*len < 3) {
20405fe0c237SAneesh Kumar K.V 		*len = 3;
204191828a40SDavid M. Grimes 		return 255;
20425fe0c237SAneesh Kumar K.V 	}
204391828a40SDavid M. Grimes 
20441d3382cbSAl Viro 	if (inode_unhashed(inode)) {
204591828a40SDavid M. Grimes 		/* Unfortunately insert_inode_hash is not idempotent,
204691828a40SDavid M. Grimes 		 * so as we hash inodes here rather than at creation
204791828a40SDavid M. Grimes 		 * time, we need a lock to ensure we only try
204891828a40SDavid M. Grimes 		 * to do it once
204991828a40SDavid M. Grimes 		 */
205091828a40SDavid M. Grimes 		static DEFINE_SPINLOCK(lock);
205191828a40SDavid M. Grimes 		spin_lock(&lock);
20521d3382cbSAl Viro 		if (inode_unhashed(inode))
205391828a40SDavid M. Grimes 			__insert_inode_hash(inode,
205491828a40SDavid M. Grimes 					    inode->i_ino + inode->i_generation);
205591828a40SDavid M. Grimes 		spin_unlock(&lock);
205691828a40SDavid M. Grimes 	}
205791828a40SDavid M. Grimes 
205891828a40SDavid M. Grimes 	fh[0] = inode->i_generation;
205991828a40SDavid M. Grimes 	fh[1] = inode->i_ino;
206091828a40SDavid M. Grimes 	fh[2] = ((__u64)inode->i_ino) >> 32;
206191828a40SDavid M. Grimes 
206291828a40SDavid M. Grimes 	*len = 3;
206391828a40SDavid M. Grimes 	return 1;
206491828a40SDavid M. Grimes }
206591828a40SDavid M. Grimes 
206639655164SChristoph Hellwig static const struct export_operations shmem_export_ops = {
206791828a40SDavid M. Grimes 	.get_parent     = shmem_get_parent,
206891828a40SDavid M. Grimes 	.encode_fh      = shmem_encode_fh,
2069480b116cSChristoph Hellwig 	.fh_to_dentry	= shmem_fh_to_dentry,
207091828a40SDavid M. Grimes };
207191828a40SDavid M. Grimes 
2072680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2073680d794bSakpm@linux-foundation.org 			       bool remount)
20741da177e4SLinus Torvalds {
20751da177e4SLinus Torvalds 	char *this_char, *value, *rest;
20768751e039SEric W. Biederman 	uid_t uid;
20778751e039SEric W. Biederman 	gid_t gid;
20781da177e4SLinus Torvalds 
2079b00dc3adSHugh Dickins 	while (options != NULL) {
2080b00dc3adSHugh Dickins 		this_char = options;
2081b00dc3adSHugh Dickins 		for (;;) {
2082b00dc3adSHugh Dickins 			/*
2083b00dc3adSHugh Dickins 			 * NUL-terminate this option: unfortunately,
2084b00dc3adSHugh Dickins 			 * mount options form a comma-separated list,
2085b00dc3adSHugh Dickins 			 * but mpol's nodelist may also contain commas.
2086b00dc3adSHugh Dickins 			 */
2087b00dc3adSHugh Dickins 			options = strchr(options, ',');
2088b00dc3adSHugh Dickins 			if (options == NULL)
2089b00dc3adSHugh Dickins 				break;
2090b00dc3adSHugh Dickins 			options++;
2091b00dc3adSHugh Dickins 			if (!isdigit(*options)) {
2092b00dc3adSHugh Dickins 				options[-1] = '\0';
2093b00dc3adSHugh Dickins 				break;
2094b00dc3adSHugh Dickins 			}
2095b00dc3adSHugh Dickins 		}
20961da177e4SLinus Torvalds 		if (!*this_char)
20971da177e4SLinus Torvalds 			continue;
20981da177e4SLinus Torvalds 		if ((value = strchr(this_char,'=')) != NULL) {
20991da177e4SLinus Torvalds 			*value++ = 0;
21001da177e4SLinus Torvalds 		} else {
21011da177e4SLinus Torvalds 			printk(KERN_ERR
21021da177e4SLinus Torvalds 			    "tmpfs: No value for mount option '%s'\n",
21031da177e4SLinus Torvalds 			    this_char);
21041da177e4SLinus Torvalds 			return 1;
21051da177e4SLinus Torvalds 		}
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds 		if (!strcmp(this_char,"size")) {
21081da177e4SLinus Torvalds 			unsigned long long size;
21091da177e4SLinus Torvalds 			size = memparse(value,&rest);
21101da177e4SLinus Torvalds 			if (*rest == '%') {
21111da177e4SLinus Torvalds 				size <<= PAGE_SHIFT;
21121da177e4SLinus Torvalds 				size *= totalram_pages;
21131da177e4SLinus Torvalds 				do_div(size, 100);
21141da177e4SLinus Torvalds 				rest++;
21151da177e4SLinus Torvalds 			}
21161da177e4SLinus Torvalds 			if (*rest)
21171da177e4SLinus Torvalds 				goto bad_val;
2118680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks =
2119680d794bSakpm@linux-foundation.org 				DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
21201da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_blocks")) {
2121680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks = memparse(value, &rest);
21221da177e4SLinus Torvalds 			if (*rest)
21231da177e4SLinus Torvalds 				goto bad_val;
21241da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_inodes")) {
2125680d794bSakpm@linux-foundation.org 			sbinfo->max_inodes = memparse(value, &rest);
21261da177e4SLinus Torvalds 			if (*rest)
21271da177e4SLinus Torvalds 				goto bad_val;
21281da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"mode")) {
2129680d794bSakpm@linux-foundation.org 			if (remount)
21301da177e4SLinus Torvalds 				continue;
2131680d794bSakpm@linux-foundation.org 			sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
21321da177e4SLinus Torvalds 			if (*rest)
21331da177e4SLinus Torvalds 				goto bad_val;
21341da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"uid")) {
2135680d794bSakpm@linux-foundation.org 			if (remount)
21361da177e4SLinus Torvalds 				continue;
21378751e039SEric W. Biederman 			uid = simple_strtoul(value, &rest, 0);
21381da177e4SLinus Torvalds 			if (*rest)
21391da177e4SLinus Torvalds 				goto bad_val;
21408751e039SEric W. Biederman 			sbinfo->uid = make_kuid(current_user_ns(), uid);
21418751e039SEric W. Biederman 			if (!uid_valid(sbinfo->uid))
21428751e039SEric W. Biederman 				goto bad_val;
21431da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"gid")) {
2144680d794bSakpm@linux-foundation.org 			if (remount)
21451da177e4SLinus Torvalds 				continue;
21468751e039SEric W. Biederman 			gid = simple_strtoul(value, &rest, 0);
21471da177e4SLinus Torvalds 			if (*rest)
21481da177e4SLinus Torvalds 				goto bad_val;
21498751e039SEric W. Biederman 			sbinfo->gid = make_kgid(current_user_ns(), gid);
21508751e039SEric W. Biederman 			if (!gid_valid(sbinfo->gid))
21518751e039SEric W. Biederman 				goto bad_val;
21527339ff83SRobin Holt 		} else if (!strcmp(this_char,"mpol")) {
215371fe804bSLee Schermerhorn 			if (mpol_parse_str(value, &sbinfo->mpol, 1))
21547339ff83SRobin Holt 				goto bad_val;
21551da177e4SLinus Torvalds 		} else {
21561da177e4SLinus Torvalds 			printk(KERN_ERR "tmpfs: Bad mount option %s\n",
21571da177e4SLinus Torvalds 			       this_char);
21581da177e4SLinus Torvalds 			return 1;
21591da177e4SLinus Torvalds 		}
21601da177e4SLinus Torvalds 	}
21611da177e4SLinus Torvalds 	return 0;
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds bad_val:
21641da177e4SLinus Torvalds 	printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
21651da177e4SLinus Torvalds 	       value, this_char);
21661da177e4SLinus Torvalds 	return 1;
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds }
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
21711da177e4SLinus Torvalds {
21721da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2173680d794bSakpm@linux-foundation.org 	struct shmem_sb_info config = *sbinfo;
21740edd73b3SHugh Dickins 	unsigned long inodes;
21750edd73b3SHugh Dickins 	int error = -EINVAL;
21761da177e4SLinus Torvalds 
2177680d794bSakpm@linux-foundation.org 	if (shmem_parse_options(data, &config, true))
21780edd73b3SHugh Dickins 		return error;
21790edd73b3SHugh Dickins 
21800edd73b3SHugh Dickins 	spin_lock(&sbinfo->stat_lock);
21810edd73b3SHugh Dickins 	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
21827e496299STim Chen 	if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
21830edd73b3SHugh Dickins 		goto out;
2184680d794bSakpm@linux-foundation.org 	if (config.max_inodes < inodes)
21850edd73b3SHugh Dickins 		goto out;
21860edd73b3SHugh Dickins 	/*
218754af6042SHugh Dickins 	 * Those tests disallow limited->unlimited while any are in use;
21880edd73b3SHugh Dickins 	 * but we must separately disallow unlimited->limited, because
21890edd73b3SHugh Dickins 	 * in that case we have no record of how much is already in use.
21900edd73b3SHugh Dickins 	 */
2191680d794bSakpm@linux-foundation.org 	if (config.max_blocks && !sbinfo->max_blocks)
21920edd73b3SHugh Dickins 		goto out;
2193680d794bSakpm@linux-foundation.org 	if (config.max_inodes && !sbinfo->max_inodes)
21940edd73b3SHugh Dickins 		goto out;
21950edd73b3SHugh Dickins 
21960edd73b3SHugh Dickins 	error = 0;
2197680d794bSakpm@linux-foundation.org 	sbinfo->max_blocks  = config.max_blocks;
2198680d794bSakpm@linux-foundation.org 	sbinfo->max_inodes  = config.max_inodes;
2199680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = config.max_inodes - inodes;
220071fe804bSLee Schermerhorn 
220171fe804bSLee Schermerhorn 	mpol_put(sbinfo->mpol);
220271fe804bSLee Schermerhorn 	sbinfo->mpol        = config.mpol;	/* transfers initial ref */
22030edd73b3SHugh Dickins out:
22040edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
22050edd73b3SHugh Dickins 	return error;
22061da177e4SLinus Torvalds }
2207680d794bSakpm@linux-foundation.org 
220834c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root)
2209680d794bSakpm@linux-foundation.org {
221034c80b1dSAl Viro 	struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
2211680d794bSakpm@linux-foundation.org 
2212680d794bSakpm@linux-foundation.org 	if (sbinfo->max_blocks != shmem_default_max_blocks())
2213680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",size=%luk",
2214680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2215680d794bSakpm@linux-foundation.org 	if (sbinfo->max_inodes != shmem_default_max_inodes())
2216680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2217680d794bSakpm@linux-foundation.org 	if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
221809208d15SAl Viro 		seq_printf(seq, ",mode=%03ho", sbinfo->mode);
22198751e039SEric W. Biederman 	if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
22208751e039SEric W. Biederman 		seq_printf(seq, ",uid=%u",
22218751e039SEric W. Biederman 				from_kuid_munged(&init_user_ns, sbinfo->uid));
22228751e039SEric W. Biederman 	if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
22238751e039SEric W. Biederman 		seq_printf(seq, ",gid=%u",
22248751e039SEric W. Biederman 				from_kgid_munged(&init_user_ns, sbinfo->gid));
222571fe804bSLee Schermerhorn 	shmem_show_mpol(seq, sbinfo->mpol);
2226680d794bSakpm@linux-foundation.org 	return 0;
2227680d794bSakpm@linux-foundation.org }
2228680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb)
22311da177e4SLinus Torvalds {
2232602586a8SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2233602586a8SHugh Dickins 
2234602586a8SHugh Dickins 	percpu_counter_destroy(&sbinfo->used_blocks);
2235602586a8SHugh Dickins 	kfree(sbinfo);
22361da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
22371da177e4SLinus Torvalds }
22381da177e4SLinus Torvalds 
22392b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent)
22401da177e4SLinus Torvalds {
22411da177e4SLinus Torvalds 	struct inode *inode;
22420edd73b3SHugh Dickins 	struct shmem_sb_info *sbinfo;
2243680d794bSakpm@linux-foundation.org 	int err = -ENOMEM;
2244680d794bSakpm@linux-foundation.org 
2245680d794bSakpm@linux-foundation.org 	/* Round up to L1_CACHE_BYTES to resist false sharing */
2246425fbf04SPekka Enberg 	sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
2247680d794bSakpm@linux-foundation.org 				L1_CACHE_BYTES), GFP_KERNEL);
2248680d794bSakpm@linux-foundation.org 	if (!sbinfo)
2249680d794bSakpm@linux-foundation.org 		return -ENOMEM;
2250680d794bSakpm@linux-foundation.org 
2251680d794bSakpm@linux-foundation.org 	sbinfo->mode = S_IRWXUGO | S_ISVTX;
225276aac0e9SDavid Howells 	sbinfo->uid = current_fsuid();
225376aac0e9SDavid Howells 	sbinfo->gid = current_fsgid();
2254680d794bSakpm@linux-foundation.org 	sb->s_fs_info = sbinfo;
22551da177e4SLinus Torvalds 
22560edd73b3SHugh Dickins #ifdef CONFIG_TMPFS
22571da177e4SLinus Torvalds 	/*
22581da177e4SLinus Torvalds 	 * Per default we only allow half of the physical ram per
22591da177e4SLinus Torvalds 	 * tmpfs instance, limiting inodes to one per page of lowmem;
22601da177e4SLinus Torvalds 	 * but the internal instance is left unlimited.
22611da177e4SLinus Torvalds 	 */
22621da177e4SLinus Torvalds 	if (!(sb->s_flags & MS_NOUSER)) {
2263680d794bSakpm@linux-foundation.org 		sbinfo->max_blocks = shmem_default_max_blocks();
2264680d794bSakpm@linux-foundation.org 		sbinfo->max_inodes = shmem_default_max_inodes();
2265680d794bSakpm@linux-foundation.org 		if (shmem_parse_options(data, sbinfo, false)) {
2266680d794bSakpm@linux-foundation.org 			err = -EINVAL;
2267680d794bSakpm@linux-foundation.org 			goto failed;
2268680d794bSakpm@linux-foundation.org 		}
22691da177e4SLinus Torvalds 	}
227091828a40SDavid M. Grimes 	sb->s_export_op = &shmem_export_ops;
22710edd73b3SHugh Dickins #else
22720edd73b3SHugh Dickins 	sb->s_flags |= MS_NOUSER;
22730edd73b3SHugh Dickins #endif
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
2276602586a8SHugh Dickins 	if (percpu_counter_init(&sbinfo->used_blocks, 0))
2277602586a8SHugh Dickins 		goto failed;
2278680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = sbinfo->max_inodes;
22791da177e4SLinus Torvalds 
2280285b2c4fSHugh Dickins 	sb->s_maxbytes = MAX_LFS_FILESIZE;
22811da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
22821da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
22831da177e4SLinus Torvalds 	sb->s_magic = TMPFS_MAGIC;
22841da177e4SLinus Torvalds 	sb->s_op = &shmem_ops;
2285cfd95a9cSRobin H. Johnson 	sb->s_time_gran = 1;
2286b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
228739f0247dSAndreas Gruenbacher 	sb->s_xattr = shmem_xattr_handlers;
2288b09e0fa4SEric Paris #endif
2289b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
229039f0247dSAndreas Gruenbacher 	sb->s_flags |= MS_POSIXACL;
229139f0247dSAndreas Gruenbacher #endif
22920edd73b3SHugh Dickins 
2293454abafeSDmitry Monakhov 	inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
22941da177e4SLinus Torvalds 	if (!inode)
22951da177e4SLinus Torvalds 		goto failed;
2296680d794bSakpm@linux-foundation.org 	inode->i_uid = sbinfo->uid;
2297680d794bSakpm@linux-foundation.org 	inode->i_gid = sbinfo->gid;
2298318ceed0SAl Viro 	sb->s_root = d_make_root(inode);
2299318ceed0SAl Viro 	if (!sb->s_root)
230048fde701SAl Viro 		goto failed;
23011da177e4SLinus Torvalds 	return 0;
23021da177e4SLinus Torvalds 
23031da177e4SLinus Torvalds failed:
23041da177e4SLinus Torvalds 	shmem_put_super(sb);
23051da177e4SLinus Torvalds 	return err;
23061da177e4SLinus Torvalds }
23071da177e4SLinus Torvalds 
2308fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep;
23091da177e4SLinus Torvalds 
23101da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb)
23111da177e4SLinus Torvalds {
231241ffe5d5SHugh Dickins 	struct shmem_inode_info *info;
231341ffe5d5SHugh Dickins 	info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
231441ffe5d5SHugh Dickins 	if (!info)
23151da177e4SLinus Torvalds 		return NULL;
231641ffe5d5SHugh Dickins 	return &info->vfs_inode;
23171da177e4SLinus Torvalds }
23181da177e4SLinus Torvalds 
231941ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head)
2320fa0d7e3dSNick Piggin {
2321fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
2322fa0d7e3dSNick Piggin 	kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2323fa0d7e3dSNick Piggin }
2324fa0d7e3dSNick Piggin 
23251da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode)
23261da177e4SLinus Torvalds {
232709208d15SAl Viro 	if (S_ISREG(inode->i_mode))
23281da177e4SLinus Torvalds 		mpol_free_shared_policy(&SHMEM_I(inode)->policy);
232941ffe5d5SHugh Dickins 	call_rcu(&inode->i_rcu, shmem_destroy_callback);
23301da177e4SLinus Torvalds }
23311da177e4SLinus Torvalds 
233241ffe5d5SHugh Dickins static void shmem_init_inode(void *foo)
23331da177e4SLinus Torvalds {
233441ffe5d5SHugh Dickins 	struct shmem_inode_info *info = foo;
233541ffe5d5SHugh Dickins 	inode_init_once(&info->vfs_inode);
23361da177e4SLinus Torvalds }
23371da177e4SLinus Torvalds 
233841ffe5d5SHugh Dickins static int shmem_init_inodecache(void)
23391da177e4SLinus Torvalds {
23401da177e4SLinus Torvalds 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
23411da177e4SLinus Torvalds 				sizeof(struct shmem_inode_info),
234241ffe5d5SHugh Dickins 				0, SLAB_PANIC, shmem_init_inode);
23431da177e4SLinus Torvalds 	return 0;
23441da177e4SLinus Torvalds }
23451da177e4SLinus Torvalds 
234641ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void)
23471da177e4SLinus Torvalds {
23481a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(shmem_inode_cachep);
23491da177e4SLinus Torvalds }
23501da177e4SLinus Torvalds 
2351f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = {
23521da177e4SLinus Torvalds 	.writepage	= shmem_writepage,
235376719325SKen Chen 	.set_page_dirty	= __set_page_dirty_no_writeback,
23541da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
2355800d15a5SNick Piggin 	.write_begin	= shmem_write_begin,
2356800d15a5SNick Piggin 	.write_end	= shmem_write_end,
23571da177e4SLinus Torvalds #endif
2358304dbdb7SLee Schermerhorn 	.migratepage	= migrate_page,
2359aa261f54SAndi Kleen 	.error_remove_page = generic_error_remove_page,
23601da177e4SLinus Torvalds };
23611da177e4SLinus Torvalds 
236215ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = {
23631da177e4SLinus Torvalds 	.mmap		= shmem_mmap,
23641da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
23651da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
2366bcd78e49SHugh Dickins 	.read		= do_sync_read,
23675402b976SHugh Dickins 	.write		= do_sync_write,
2368bcd78e49SHugh Dickins 	.aio_read	= shmem_file_aio_read,
23695402b976SHugh Dickins 	.aio_write	= generic_file_aio_write,
23701b061d92SChristoph Hellwig 	.fsync		= noop_fsync,
2371708e3508SHugh Dickins 	.splice_read	= shmem_file_splice_read,
2372ae976416SHugh Dickins 	.splice_write	= generic_file_splice_write,
23731da177e4SLinus Torvalds #endif
23741da177e4SLinus Torvalds };
23751da177e4SLinus Torvalds 
237692e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = {
237794c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
2378f6b3ec23SBadari Pulavarty 	.truncate_range	= shmem_truncate_range,
2379b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2380b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2381b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2382b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2383b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2384b09e0fa4SEric Paris #endif
23851da177e4SLinus Torvalds };
23861da177e4SLinus Torvalds 
238792e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = {
23881da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
23891da177e4SLinus Torvalds 	.create		= shmem_create,
23901da177e4SLinus Torvalds 	.lookup		= simple_lookup,
23911da177e4SLinus Torvalds 	.link		= shmem_link,
23921da177e4SLinus Torvalds 	.unlink		= shmem_unlink,
23931da177e4SLinus Torvalds 	.symlink	= shmem_symlink,
23941da177e4SLinus Torvalds 	.mkdir		= shmem_mkdir,
23951da177e4SLinus Torvalds 	.rmdir		= shmem_rmdir,
23961da177e4SLinus Torvalds 	.mknod		= shmem_mknod,
23971da177e4SLinus Torvalds 	.rename		= shmem_rename,
23981da177e4SLinus Torvalds #endif
2399b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2400b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2401b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2402b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2403b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2404b09e0fa4SEric Paris #endif
240539f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
240694c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
240739f0247dSAndreas Gruenbacher #endif
240839f0247dSAndreas Gruenbacher };
240939f0247dSAndreas Gruenbacher 
241092e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = {
2411b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2412b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2413b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2414b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2415b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2416b09e0fa4SEric Paris #endif
241739f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
241894c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
241939f0247dSAndreas Gruenbacher #endif
24201da177e4SLinus Torvalds };
24211da177e4SLinus Torvalds 
2422759b9775SHugh Dickins static const struct super_operations shmem_ops = {
24231da177e4SLinus Torvalds 	.alloc_inode	= shmem_alloc_inode,
24241da177e4SLinus Torvalds 	.destroy_inode	= shmem_destroy_inode,
24251da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
24261da177e4SLinus Torvalds 	.statfs		= shmem_statfs,
24271da177e4SLinus Torvalds 	.remount_fs	= shmem_remount_fs,
2428680d794bSakpm@linux-foundation.org 	.show_options	= shmem_show_options,
24291da177e4SLinus Torvalds #endif
24301f895f75SAl Viro 	.evict_inode	= shmem_evict_inode,
24311da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
24321da177e4SLinus Torvalds 	.put_super	= shmem_put_super,
24331da177e4SLinus Torvalds };
24341da177e4SLinus Torvalds 
2435f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = {
243654cb8821SNick Piggin 	.fault		= shmem_fault,
24371da177e4SLinus Torvalds #ifdef CONFIG_NUMA
24381da177e4SLinus Torvalds 	.set_policy     = shmem_set_policy,
24391da177e4SLinus Torvalds 	.get_policy     = shmem_get_policy,
24401da177e4SLinus Torvalds #endif
24411da177e4SLinus Torvalds };
24421da177e4SLinus Torvalds 
24433c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type,
24443c26ff6eSAl Viro 	int flags, const char *dev_name, void *data)
24451da177e4SLinus Torvalds {
24463c26ff6eSAl Viro 	return mount_nodev(fs_type, flags, data, shmem_fill_super);
24471da177e4SLinus Torvalds }
24481da177e4SLinus Torvalds 
244941ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = {
24501da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
24511da177e4SLinus Torvalds 	.name		= "tmpfs",
24523c26ff6eSAl Viro 	.mount		= shmem_mount,
24531da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
24541da177e4SLinus Torvalds };
24551da177e4SLinus Torvalds 
245641ffe5d5SHugh Dickins int __init shmem_init(void)
24571da177e4SLinus Torvalds {
24581da177e4SLinus Torvalds 	int error;
24591da177e4SLinus Torvalds 
2460e0bf68ddSPeter Zijlstra 	error = bdi_init(&shmem_backing_dev_info);
2461e0bf68ddSPeter Zijlstra 	if (error)
2462e0bf68ddSPeter Zijlstra 		goto out4;
2463e0bf68ddSPeter Zijlstra 
246441ffe5d5SHugh Dickins 	error = shmem_init_inodecache();
24651da177e4SLinus Torvalds 	if (error)
24661da177e4SLinus Torvalds 		goto out3;
24671da177e4SLinus Torvalds 
246841ffe5d5SHugh Dickins 	error = register_filesystem(&shmem_fs_type);
24691da177e4SLinus Torvalds 	if (error) {
24701da177e4SLinus Torvalds 		printk(KERN_ERR "Could not register tmpfs\n");
24711da177e4SLinus Torvalds 		goto out2;
24721da177e4SLinus Torvalds 	}
247395dc112aSGreg Kroah-Hartman 
247441ffe5d5SHugh Dickins 	shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
247541ffe5d5SHugh Dickins 				 shmem_fs_type.name, NULL);
24761da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt)) {
24771da177e4SLinus Torvalds 		error = PTR_ERR(shm_mnt);
24781da177e4SLinus Torvalds 		printk(KERN_ERR "Could not kern_mount tmpfs\n");
24791da177e4SLinus Torvalds 		goto out1;
24801da177e4SLinus Torvalds 	}
24811da177e4SLinus Torvalds 	return 0;
24821da177e4SLinus Torvalds 
24831da177e4SLinus Torvalds out1:
248441ffe5d5SHugh Dickins 	unregister_filesystem(&shmem_fs_type);
24851da177e4SLinus Torvalds out2:
248641ffe5d5SHugh Dickins 	shmem_destroy_inodecache();
24871da177e4SLinus Torvalds out3:
2488e0bf68ddSPeter Zijlstra 	bdi_destroy(&shmem_backing_dev_info);
2489e0bf68ddSPeter Zijlstra out4:
24901da177e4SLinus Torvalds 	shm_mnt = ERR_PTR(error);
24911da177e4SLinus Torvalds 	return error;
24921da177e4SLinus Torvalds }
2493853ac43aSMatt Mackall 
2494853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */
2495853ac43aSMatt Mackall 
2496853ac43aSMatt Mackall /*
2497853ac43aSMatt Mackall  * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2498853ac43aSMatt Mackall  *
2499853ac43aSMatt Mackall  * This is intended for small system where the benefits of the full
2500853ac43aSMatt Mackall  * shmem code (swap-backed and resource-limited) are outweighed by
2501853ac43aSMatt Mackall  * their complexity. On systems without swap this code should be
2502853ac43aSMatt Mackall  * effectively equivalent, but much lighter weight.
2503853ac43aSMatt Mackall  */
2504853ac43aSMatt Mackall 
2505853ac43aSMatt Mackall #include <linux/ramfs.h>
2506853ac43aSMatt Mackall 
250741ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = {
2508853ac43aSMatt Mackall 	.name		= "tmpfs",
25093c26ff6eSAl Viro 	.mount		= ramfs_mount,
2510853ac43aSMatt Mackall 	.kill_sb	= kill_litter_super,
2511853ac43aSMatt Mackall };
2512853ac43aSMatt Mackall 
251341ffe5d5SHugh Dickins int __init shmem_init(void)
2514853ac43aSMatt Mackall {
251541ffe5d5SHugh Dickins 	BUG_ON(register_filesystem(&shmem_fs_type) != 0);
2516853ac43aSMatt Mackall 
251741ffe5d5SHugh Dickins 	shm_mnt = kern_mount(&shmem_fs_type);
2518853ac43aSMatt Mackall 	BUG_ON(IS_ERR(shm_mnt));
2519853ac43aSMatt Mackall 
2520853ac43aSMatt Mackall 	return 0;
2521853ac43aSMatt Mackall }
2522853ac43aSMatt Mackall 
252341ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page)
2524853ac43aSMatt Mackall {
2525853ac43aSMatt Mackall 	return 0;
2526853ac43aSMatt Mackall }
2527853ac43aSMatt Mackall 
25283f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user)
25293f96b79aSHugh Dickins {
25303f96b79aSHugh Dickins 	return 0;
25313f96b79aSHugh Dickins }
25323f96b79aSHugh Dickins 
253324513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping)
253424513264SHugh Dickins {
253524513264SHugh Dickins }
253624513264SHugh Dickins 
253741ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
253894c1e62dSHugh Dickins {
253941ffe5d5SHugh Dickins 	truncate_inode_pages_range(inode->i_mapping, lstart, lend);
254094c1e62dSHugh Dickins }
254194c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
254294c1e62dSHugh Dickins 
2543853ac43aSMatt Mackall #define shmem_vm_ops				generic_file_vm_ops
25440b0a0806SHugh Dickins #define shmem_file_operations			ramfs_file_operations
2545454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags)	ramfs_get_inode(sb, dir, mode, dev)
25460b0a0806SHugh Dickins #define shmem_acct_size(flags, size)		0
25470b0a0806SHugh Dickins #define shmem_unacct_size(flags, size)		do {} while (0)
2548853ac43aSMatt Mackall 
2549853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */
2550853ac43aSMatt Mackall 
2551853ac43aSMatt Mackall /* common code */
25521da177e4SLinus Torvalds 
255346711810SRandy Dunlap /**
25541da177e4SLinus Torvalds  * shmem_file_setup - get an unlinked file living in tmpfs
25551da177e4SLinus Torvalds  * @name: name for dentry (to be seen in /proc/<pid>/maps
25561da177e4SLinus Torvalds  * @size: size to be set for the file
25570b0a0806SHugh Dickins  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
25581da177e4SLinus Torvalds  */
2559168f5ac6SSergei Trofimovich struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
25601da177e4SLinus Torvalds {
25611da177e4SLinus Torvalds 	int error;
25621da177e4SLinus Torvalds 	struct file *file;
25631da177e4SLinus Torvalds 	struct inode *inode;
25642c48b9c4SAl Viro 	struct path path;
25652c48b9c4SAl Viro 	struct dentry *root;
25661da177e4SLinus Torvalds 	struct qstr this;
25671da177e4SLinus Torvalds 
25681da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt))
25691da177e4SLinus Torvalds 		return (void *)shm_mnt;
25701da177e4SLinus Torvalds 
2571285b2c4fSHugh Dickins 	if (size < 0 || size > MAX_LFS_FILESIZE)
25721da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
25731da177e4SLinus Torvalds 
25741da177e4SLinus Torvalds 	if (shmem_acct_size(flags, size))
25751da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
25761da177e4SLinus Torvalds 
25771da177e4SLinus Torvalds 	error = -ENOMEM;
25781da177e4SLinus Torvalds 	this.name = name;
25791da177e4SLinus Torvalds 	this.len = strlen(name);
25801da177e4SLinus Torvalds 	this.hash = 0; /* will go */
25811da177e4SLinus Torvalds 	root = shm_mnt->mnt_root;
25822c48b9c4SAl Viro 	path.dentry = d_alloc(root, &this);
25832c48b9c4SAl Viro 	if (!path.dentry)
25841da177e4SLinus Torvalds 		goto put_memory;
25852c48b9c4SAl Viro 	path.mnt = mntget(shm_mnt);
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds 	error = -ENOSPC;
2588454abafeSDmitry Monakhov 	inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
25891da177e4SLinus Torvalds 	if (!inode)
25904b42af81SAl Viro 		goto put_dentry;
25911da177e4SLinus Torvalds 
25922c48b9c4SAl Viro 	d_instantiate(path.dentry, inode);
25931da177e4SLinus Torvalds 	inode->i_size = size;
25946d6b77f1SMiklos Szeredi 	clear_nlink(inode);	/* It is unlinked */
2595853ac43aSMatt Mackall #ifndef CONFIG_MMU
2596853ac43aSMatt Mackall 	error = ramfs_nommu_expand_for_mapping(inode, size);
2597853ac43aSMatt Mackall 	if (error)
25984b42af81SAl Viro 		goto put_dentry;
2599853ac43aSMatt Mackall #endif
26004b42af81SAl Viro 
26014b42af81SAl Viro 	error = -ENFILE;
26022c48b9c4SAl Viro 	file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
26034b42af81SAl Viro 		  &shmem_file_operations);
26044b42af81SAl Viro 	if (!file)
26054b42af81SAl Viro 		goto put_dentry;
26064b42af81SAl Viro 
26071da177e4SLinus Torvalds 	return file;
26081da177e4SLinus Torvalds 
26091da177e4SLinus Torvalds put_dentry:
26102c48b9c4SAl Viro 	path_put(&path);
26111da177e4SLinus Torvalds put_memory:
26121da177e4SLinus Torvalds 	shmem_unacct_size(flags, size);
26131da177e4SLinus Torvalds 	return ERR_PTR(error);
26141da177e4SLinus Torvalds }
2615395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup);
26161da177e4SLinus Torvalds 
261746711810SRandy Dunlap /**
26181da177e4SLinus Torvalds  * shmem_zero_setup - setup a shared anonymous mapping
26191da177e4SLinus Torvalds  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
26201da177e4SLinus Torvalds  */
26211da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma)
26221da177e4SLinus Torvalds {
26231da177e4SLinus Torvalds 	struct file *file;
26241da177e4SLinus Torvalds 	loff_t size = vma->vm_end - vma->vm_start;
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 	file = shmem_file_setup("dev/zero", size, vma->vm_flags);
26271da177e4SLinus Torvalds 	if (IS_ERR(file))
26281da177e4SLinus Torvalds 		return PTR_ERR(file);
26291da177e4SLinus Torvalds 
26301da177e4SLinus Torvalds 	if (vma->vm_file)
26311da177e4SLinus Torvalds 		fput(vma->vm_file);
26321da177e4SLinus Torvalds 	vma->vm_file = file;
26331da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
2634bee4c36aSHugh Dickins 	vma->vm_flags |= VM_CAN_NONLINEAR;
26351da177e4SLinus Torvalds 	return 0;
26361da177e4SLinus Torvalds }
2637d9d90e5eSHugh Dickins 
2638d9d90e5eSHugh Dickins /**
2639d9d90e5eSHugh Dickins  * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2640d9d90e5eSHugh Dickins  * @mapping:	the page's address_space
2641d9d90e5eSHugh Dickins  * @index:	the page index
2642d9d90e5eSHugh Dickins  * @gfp:	the page allocator flags to use if allocating
2643d9d90e5eSHugh Dickins  *
2644d9d90e5eSHugh Dickins  * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2645d9d90e5eSHugh Dickins  * with any new page allocations done using the specified allocation flags.
2646d9d90e5eSHugh Dickins  * But read_cache_page_gfp() uses the ->readpage() method: which does not
2647d9d90e5eSHugh Dickins  * suit tmpfs, since it may have pages in swapcache, and needs to find those
2648d9d90e5eSHugh Dickins  * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2649d9d90e5eSHugh Dickins  *
265068da9f05SHugh Dickins  * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
265168da9f05SHugh Dickins  * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
2652d9d90e5eSHugh Dickins  */
2653d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2654d9d90e5eSHugh Dickins 					 pgoff_t index, gfp_t gfp)
2655d9d90e5eSHugh Dickins {
265668da9f05SHugh Dickins #ifdef CONFIG_SHMEM
265768da9f05SHugh Dickins 	struct inode *inode = mapping->host;
26589276aad6SHugh Dickins 	struct page *page;
265968da9f05SHugh Dickins 	int error;
266068da9f05SHugh Dickins 
266168da9f05SHugh Dickins 	BUG_ON(mapping->a_ops != &shmem_aops);
266268da9f05SHugh Dickins 	error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
266368da9f05SHugh Dickins 	if (error)
266468da9f05SHugh Dickins 		page = ERR_PTR(error);
266568da9f05SHugh Dickins 	else
266668da9f05SHugh Dickins 		unlock_page(page);
266768da9f05SHugh Dickins 	return page;
266868da9f05SHugh Dickins #else
266968da9f05SHugh Dickins 	/*
267068da9f05SHugh Dickins 	 * The tiny !SHMEM case uses ramfs without swap
267168da9f05SHugh Dickins 	 */
2672d9d90e5eSHugh Dickins 	return read_cache_page_gfp(mapping, index, gfp);
267368da9f05SHugh Dickins #endif
2674d9d90e5eSHugh Dickins }
2675d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);
2676