xref: /openbmc/linux/mm/shmem.c (revision d0424c429f8e0555a337d71e0a13f2289c636ec9)
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>
28250297edSAndrew Morton #include <linux/ramfs.h>
29caefba17SHugh Dickins #include <linux/pagemap.h>
30853ac43aSMatt Mackall #include <linux/file.h>
31853ac43aSMatt Mackall #include <linux/mm.h>
32b95f1b31SPaul Gortmaker #include <linux/export.h>
33853ac43aSMatt Mackall #include <linux/swap.h>
34e2e40f2cSChristoph Hellwig #include <linux/uio.h>
35853ac43aSMatt Mackall 
36853ac43aSMatt Mackall static struct vfsmount *shm_mnt;
37853ac43aSMatt Mackall 
38853ac43aSMatt Mackall #ifdef CONFIG_SHMEM
391da177e4SLinus Torvalds /*
401da177e4SLinus Torvalds  * This virtual memory filesystem is heavily based on the ramfs. It
411da177e4SLinus Torvalds  * extends ramfs by the ability to use swap and honor resource limits
421da177e4SLinus Torvalds  * which makes it a completely usable filesystem.
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds 
4539f0247dSAndreas Gruenbacher #include <linux/xattr.h>
46a5694255SChristoph Hellwig #include <linux/exportfs.h>
471c7c474cSChristoph Hellwig #include <linux/posix_acl.h>
48feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h>
491da177e4SLinus Torvalds #include <linux/mman.h>
501da177e4SLinus Torvalds #include <linux/string.h>
511da177e4SLinus Torvalds #include <linux/slab.h>
521da177e4SLinus Torvalds #include <linux/backing-dev.h>
531da177e4SLinus Torvalds #include <linux/shmem_fs.h>
541da177e4SLinus Torvalds #include <linux/writeback.h>
551da177e4SLinus Torvalds #include <linux/blkdev.h>
56bda97eabSHugh Dickins #include <linux/pagevec.h>
5741ffe5d5SHugh Dickins #include <linux/percpu_counter.h>
5883e4fa9cSHugh Dickins #include <linux/falloc.h>
59708e3508SHugh Dickins #include <linux/splice.h>
601da177e4SLinus Torvalds #include <linux/security.h>
611da177e4SLinus Torvalds #include <linux/swapops.h>
621da177e4SLinus Torvalds #include <linux/mempolicy.h>
631da177e4SLinus Torvalds #include <linux/namei.h>
64b00dc3adSHugh Dickins #include <linux/ctype.h>
65304dbdb7SLee Schermerhorn #include <linux/migrate.h>
66c1f60a5aSChristoph Lameter #include <linux/highmem.h>
67680d794bSakpm@linux-foundation.org #include <linux/seq_file.h>
6892562927SMimi Zohar #include <linux/magic.h>
699183df25SDavid Herrmann #include <linux/syscalls.h>
7040e041a2SDavid Herrmann #include <linux/fcntl.h>
719183df25SDavid Herrmann #include <uapi/linux/memfd.h>
72304dbdb7SLee Schermerhorn 
731da177e4SLinus Torvalds #include <asm/uaccess.h>
741da177e4SLinus Torvalds #include <asm/pgtable.h>
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds #define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
771da177e4SLinus Torvalds #define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */
801da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20
811da177e4SLinus Torvalds 
8269f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
8369f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128
8469f07ec9SHugh Dickins 
851aac1400SHugh Dickins /*
86f00cdc6dSHugh Dickins  * shmem_fallocate communicates with shmem_fault or shmem_writepage via
87f00cdc6dSHugh Dickins  * inode->i_private (with i_mutex making sure that it has only one user at
88f00cdc6dSHugh Dickins  * a time): we would prefer not to enlarge the shmem inode just for that.
891aac1400SHugh Dickins  */
901aac1400SHugh Dickins struct shmem_falloc {
918e205f77SHugh Dickins 	wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
921aac1400SHugh Dickins 	pgoff_t start;		/* start of range currently being fallocated */
931aac1400SHugh Dickins 	pgoff_t next;		/* the next page offset to be fallocated */
941aac1400SHugh Dickins 	pgoff_t nr_falloced;	/* how many new pages have been fallocated */
951aac1400SHugh Dickins 	pgoff_t nr_unswapped;	/* how often writepage refused to swap out */
961aac1400SHugh Dickins };
971aac1400SHugh Dickins 
98285b2c4fSHugh Dickins /* Flag allocation requirements to shmem_getpage */
991da177e4SLinus Torvalds enum sgp_type {
1001da177e4SLinus Torvalds 	SGP_READ,	/* don't exceed i_size, don't allocate page */
1011da177e4SLinus Torvalds 	SGP_CACHE,	/* don't exceed i_size, may allocate page */
102a0ee5ec5SHugh Dickins 	SGP_DIRTY,	/* like SGP_CACHE, but set new page dirty */
1031635f6a7SHugh Dickins 	SGP_WRITE,	/* may exceed i_size, may allocate !Uptodate page */
1041635f6a7SHugh Dickins 	SGP_FALLOC,	/* like SGP_WRITE, but make existing page Uptodate */
1051da177e4SLinus Torvalds };
1061da177e4SLinus Torvalds 
107b76db735SAndrew Morton #ifdef CONFIG_TMPFS
108680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void)
109680d794bSakpm@linux-foundation.org {
110680d794bSakpm@linux-foundation.org 	return totalram_pages / 2;
111680d794bSakpm@linux-foundation.org }
112680d794bSakpm@linux-foundation.org 
113680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void)
114680d794bSakpm@linux-foundation.org {
115680d794bSakpm@linux-foundation.org 	return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
116680d794bSakpm@linux-foundation.org }
117b76db735SAndrew Morton #endif
118680d794bSakpm@linux-foundation.org 
119bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
120bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp,
121bde05d1cSHugh Dickins 				struct shmem_inode_info *info, pgoff_t index);
12268da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
12368da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
12468da9f05SHugh Dickins 
12568da9f05SHugh Dickins static inline int shmem_getpage(struct inode *inode, pgoff_t index,
12668da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, int *fault_type)
12768da9f05SHugh Dickins {
12868da9f05SHugh Dickins 	return shmem_getpage_gfp(inode, index, pagep, sgp,
12968da9f05SHugh Dickins 			mapping_gfp_mask(inode->i_mapping), fault_type);
13068da9f05SHugh Dickins }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	return sb->s_fs_info;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /*
1381da177e4SLinus Torvalds  * shmem_file_setup pre-accounts the whole fixed size of a VM object,
1391da177e4SLinus Torvalds  * for shared memory and for shared anonymous (/dev/zero) mappings
1401da177e4SLinus Torvalds  * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
1411da177e4SLinus Torvalds  * consistent with the pre-accounting of private mappings ...
1421da177e4SLinus Torvalds  */
1431da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size)
1441da177e4SLinus Torvalds {
1450b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
146191c5424SAl Viro 		0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size)
1501da177e4SLinus Torvalds {
1510b0a0806SHugh Dickins 	if (!(flags & VM_NORESERVE))
1521da177e4SLinus Torvalds 		vm_unacct_memory(VM_ACCT(size));
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
15577142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags,
15677142517SKonstantin Khlebnikov 		loff_t oldsize, loff_t newsize)
15777142517SKonstantin Khlebnikov {
15877142517SKonstantin Khlebnikov 	if (!(flags & VM_NORESERVE)) {
15977142517SKonstantin Khlebnikov 		if (VM_ACCT(newsize) > VM_ACCT(oldsize))
16077142517SKonstantin Khlebnikov 			return security_vm_enough_memory_mm(current->mm,
16177142517SKonstantin Khlebnikov 					VM_ACCT(newsize) - VM_ACCT(oldsize));
16277142517SKonstantin Khlebnikov 		else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
16377142517SKonstantin Khlebnikov 			vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
16477142517SKonstantin Khlebnikov 	}
16577142517SKonstantin Khlebnikov 	return 0;
16677142517SKonstantin Khlebnikov }
16777142517SKonstantin Khlebnikov 
1681da177e4SLinus Torvalds /*
1691da177e4SLinus Torvalds  * ... whereas tmpfs objects are accounted incrementally as
1701da177e4SLinus Torvalds  * pages are allocated, in order to allow huge sparse files.
1711da177e4SLinus Torvalds  * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
1721da177e4SLinus Torvalds  * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
1731da177e4SLinus Torvalds  */
1741da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags)
1751da177e4SLinus Torvalds {
1760b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
177191c5424SAl Viro 		security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages)
1811da177e4SLinus Torvalds {
1820b0a0806SHugh Dickins 	if (flags & VM_NORESERVE)
1831da177e4SLinus Torvalds 		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
186759b9775SHugh Dickins static const struct super_operations shmem_ops;
187f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops;
18815ad7cdcSHelge Deller static const struct file_operations shmem_file_operations;
18992e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations;
19092e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations;
19192e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations;
192f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist);
195cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex);
1961da177e4SLinus Torvalds 
1975b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb)
1985b04c689SPavel Emelyanov {
1995b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2005b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2015b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2025b04c689SPavel Emelyanov 		if (!sbinfo->free_inodes) {
2035b04c689SPavel Emelyanov 			spin_unlock(&sbinfo->stat_lock);
2045b04c689SPavel Emelyanov 			return -ENOSPC;
2055b04c689SPavel Emelyanov 		}
2065b04c689SPavel Emelyanov 		sbinfo->free_inodes--;
2075b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2085b04c689SPavel Emelyanov 	}
2095b04c689SPavel Emelyanov 	return 0;
2105b04c689SPavel Emelyanov }
2115b04c689SPavel Emelyanov 
2125b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb)
2135b04c689SPavel Emelyanov {
2145b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2155b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2165b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2175b04c689SPavel Emelyanov 		sbinfo->free_inodes++;
2185b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2195b04c689SPavel Emelyanov 	}
2205b04c689SPavel Emelyanov }
2215b04c689SPavel Emelyanov 
22246711810SRandy Dunlap /**
22341ffe5d5SHugh Dickins  * shmem_recalc_inode - recalculate the block usage of an inode
2241da177e4SLinus Torvalds  * @inode: inode to recalc
2251da177e4SLinus Torvalds  *
2261da177e4SLinus Torvalds  * We have to calculate the free blocks since the mm can drop
2271da177e4SLinus Torvalds  * undirtied hole pages behind our back.
2281da177e4SLinus Torvalds  *
2291da177e4SLinus Torvalds  * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
2301da177e4SLinus Torvalds  * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
2311da177e4SLinus Torvalds  *
2321da177e4SLinus Torvalds  * It has to be called with the spinlock held.
2331da177e4SLinus Torvalds  */
2341da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
2371da177e4SLinus Torvalds 	long freed;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
2401da177e4SLinus Torvalds 	if (freed > 0) {
24154af6042SHugh Dickins 		struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
24254af6042SHugh Dickins 		if (sbinfo->max_blocks)
24354af6042SHugh Dickins 			percpu_counter_add(&sbinfo->used_blocks, -freed);
2441da177e4SLinus Torvalds 		info->alloced -= freed;
24554af6042SHugh Dickins 		inode->i_blocks -= freed * BLOCKS_PER_PAGE;
2461da177e4SLinus Torvalds 		shmem_unacct_blocks(info->flags, freed);
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2507a5d0fbbSHugh Dickins /*
2517a5d0fbbSHugh Dickins  * Replace item expected in radix tree by a new item, while holding tree lock.
2527a5d0fbbSHugh Dickins  */
2537a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping,
2547a5d0fbbSHugh Dickins 			pgoff_t index, void *expected, void *replacement)
2557a5d0fbbSHugh Dickins {
2567a5d0fbbSHugh Dickins 	void **pslot;
2576dbaf22cSJohannes Weiner 	void *item;
2587a5d0fbbSHugh Dickins 
2597a5d0fbbSHugh Dickins 	VM_BUG_ON(!expected);
2606dbaf22cSJohannes Weiner 	VM_BUG_ON(!replacement);
2617a5d0fbbSHugh Dickins 	pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
2626dbaf22cSJohannes Weiner 	if (!pslot)
2636dbaf22cSJohannes Weiner 		return -ENOENT;
2646dbaf22cSJohannes Weiner 	item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock);
2657a5d0fbbSHugh Dickins 	if (item != expected)
2667a5d0fbbSHugh Dickins 		return -ENOENT;
2677a5d0fbbSHugh Dickins 	radix_tree_replace_slot(pslot, replacement);
2687a5d0fbbSHugh Dickins 	return 0;
2697a5d0fbbSHugh Dickins }
2707a5d0fbbSHugh Dickins 
2717a5d0fbbSHugh Dickins /*
272d1899228SHugh Dickins  * Sometimes, before we decide whether to proceed or to fail, we must check
273d1899228SHugh Dickins  * that an entry was not already brought back from swap by a racing thread.
274d1899228SHugh Dickins  *
275d1899228SHugh Dickins  * Checking page is not enough: by the time a SwapCache page is locked, it
276d1899228SHugh Dickins  * might be reused, and again be SwapCache, using the same swap as before.
277d1899228SHugh Dickins  */
278d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping,
279d1899228SHugh Dickins 			       pgoff_t index, swp_entry_t swap)
280d1899228SHugh Dickins {
281d1899228SHugh Dickins 	void *item;
282d1899228SHugh Dickins 
283d1899228SHugh Dickins 	rcu_read_lock();
284d1899228SHugh Dickins 	item = radix_tree_lookup(&mapping->page_tree, index);
285d1899228SHugh Dickins 	rcu_read_unlock();
286d1899228SHugh Dickins 	return item == swp_to_radix_entry(swap);
287d1899228SHugh Dickins }
288d1899228SHugh Dickins 
289d1899228SHugh Dickins /*
29046f65ec1SHugh Dickins  * Like add_to_page_cache_locked, but error if expected item has gone.
29146f65ec1SHugh Dickins  */
29246f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page,
29346f65ec1SHugh Dickins 				   struct address_space *mapping,
294fed400a1SWang Sheng-Hui 				   pgoff_t index, void *expected)
29546f65ec1SHugh Dickins {
296b065b432SHugh Dickins 	int error;
29746f65ec1SHugh Dickins 
298309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageLocked(page), page);
299309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
30046f65ec1SHugh Dickins 
30146f65ec1SHugh Dickins 	page_cache_get(page);
30246f65ec1SHugh Dickins 	page->mapping = mapping;
30346f65ec1SHugh Dickins 	page->index = index;
30446f65ec1SHugh Dickins 
30546f65ec1SHugh Dickins 	spin_lock_irq(&mapping->tree_lock);
30646f65ec1SHugh Dickins 	if (!expected)
307b065b432SHugh Dickins 		error = radix_tree_insert(&mapping->page_tree, index, page);
30846f65ec1SHugh Dickins 	else
309b065b432SHugh Dickins 		error = shmem_radix_tree_replace(mapping, index, expected,
310b065b432SHugh Dickins 								 page);
31146f65ec1SHugh Dickins 	if (!error) {
31246f65ec1SHugh Dickins 		mapping->nrpages++;
31346f65ec1SHugh Dickins 		__inc_zone_page_state(page, NR_FILE_PAGES);
31446f65ec1SHugh Dickins 		__inc_zone_page_state(page, NR_SHMEM);
31546f65ec1SHugh Dickins 		spin_unlock_irq(&mapping->tree_lock);
31646f65ec1SHugh Dickins 	} else {
31746f65ec1SHugh Dickins 		page->mapping = NULL;
31846f65ec1SHugh Dickins 		spin_unlock_irq(&mapping->tree_lock);
31946f65ec1SHugh Dickins 		page_cache_release(page);
32046f65ec1SHugh Dickins 	}
32146f65ec1SHugh Dickins 	return error;
32246f65ec1SHugh Dickins }
32346f65ec1SHugh Dickins 
32446f65ec1SHugh Dickins /*
3256922c0c7SHugh Dickins  * Like delete_from_page_cache, but substitutes swap for page.
3266922c0c7SHugh Dickins  */
3276922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap)
3286922c0c7SHugh Dickins {
3296922c0c7SHugh Dickins 	struct address_space *mapping = page->mapping;
3306922c0c7SHugh Dickins 	int error;
3316922c0c7SHugh Dickins 
3326922c0c7SHugh Dickins 	spin_lock_irq(&mapping->tree_lock);
3336922c0c7SHugh Dickins 	error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
3346922c0c7SHugh Dickins 	page->mapping = NULL;
3356922c0c7SHugh Dickins 	mapping->nrpages--;
3366922c0c7SHugh Dickins 	__dec_zone_page_state(page, NR_FILE_PAGES);
3376922c0c7SHugh Dickins 	__dec_zone_page_state(page, NR_SHMEM);
3386922c0c7SHugh Dickins 	spin_unlock_irq(&mapping->tree_lock);
3396922c0c7SHugh Dickins 	page_cache_release(page);
3406922c0c7SHugh Dickins 	BUG_ON(error);
3416922c0c7SHugh Dickins }
3426922c0c7SHugh Dickins 
3436922c0c7SHugh Dickins /*
3447a5d0fbbSHugh Dickins  * Remove swap entry from radix tree, free the swap and its page cache.
3457a5d0fbbSHugh Dickins  */
3467a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping,
3477a5d0fbbSHugh Dickins 			   pgoff_t index, void *radswap)
3487a5d0fbbSHugh Dickins {
3496dbaf22cSJohannes Weiner 	void *old;
3507a5d0fbbSHugh Dickins 
3517a5d0fbbSHugh Dickins 	spin_lock_irq(&mapping->tree_lock);
3526dbaf22cSJohannes Weiner 	old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
3537a5d0fbbSHugh Dickins 	spin_unlock_irq(&mapping->tree_lock);
3546dbaf22cSJohannes Weiner 	if (old != radswap)
3556dbaf22cSJohannes Weiner 		return -ENOENT;
3567a5d0fbbSHugh Dickins 	free_swap_and_cache(radix_to_swp_entry(radswap));
3576dbaf22cSJohannes Weiner 	return 0;
3587a5d0fbbSHugh Dickins }
3597a5d0fbbSHugh Dickins 
3607a5d0fbbSHugh Dickins /*
36124513264SHugh Dickins  * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
36224513264SHugh Dickins  */
36324513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping)
36424513264SHugh Dickins {
36524513264SHugh Dickins 	struct pagevec pvec;
36624513264SHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
36724513264SHugh Dickins 	pgoff_t index = 0;
36824513264SHugh Dickins 
36924513264SHugh Dickins 	pagevec_init(&pvec, 0);
37024513264SHugh Dickins 	/*
37124513264SHugh Dickins 	 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
37224513264SHugh Dickins 	 */
37324513264SHugh Dickins 	while (!mapping_unevictable(mapping)) {
37424513264SHugh Dickins 		/*
37524513264SHugh Dickins 		 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
37624513264SHugh Dickins 		 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
37724513264SHugh Dickins 		 */
3780cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
37924513264SHugh Dickins 					   PAGEVEC_SIZE, pvec.pages, indices);
38024513264SHugh Dickins 		if (!pvec.nr)
38124513264SHugh Dickins 			break;
38224513264SHugh Dickins 		index = indices[pvec.nr - 1] + 1;
3830cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
38424513264SHugh Dickins 		check_move_unevictable_pages(pvec.pages, pvec.nr);
38524513264SHugh Dickins 		pagevec_release(&pvec);
38624513264SHugh Dickins 		cond_resched();
38724513264SHugh Dickins 	}
3887a5d0fbbSHugh Dickins }
3897a5d0fbbSHugh Dickins 
3907a5d0fbbSHugh Dickins /*
3917a5d0fbbSHugh Dickins  * Remove range of pages and swap entries from radix tree, and free them.
3921635f6a7SHugh Dickins  * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
3937a5d0fbbSHugh Dickins  */
3941635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
3951635f6a7SHugh Dickins 								 bool unfalloc)
3961da177e4SLinus Torvalds {
397285b2c4fSHugh Dickins 	struct address_space *mapping = inode->i_mapping;
3981da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
399285b2c4fSHugh Dickins 	pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
40083e4fa9cSHugh Dickins 	pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
40183e4fa9cSHugh Dickins 	unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
40283e4fa9cSHugh Dickins 	unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
403bda97eabSHugh Dickins 	struct pagevec pvec;
4047a5d0fbbSHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
4057a5d0fbbSHugh Dickins 	long nr_swaps_freed = 0;
406285b2c4fSHugh Dickins 	pgoff_t index;
407bda97eabSHugh Dickins 	int i;
4081da177e4SLinus Torvalds 
40983e4fa9cSHugh Dickins 	if (lend == -1)
41083e4fa9cSHugh Dickins 		end = -1;	/* unsigned, so actually very big */
411bda97eabSHugh Dickins 
412bda97eabSHugh Dickins 	pagevec_init(&pvec, 0);
413bda97eabSHugh Dickins 	index = start;
41483e4fa9cSHugh Dickins 	while (index < end) {
4150cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
41683e4fa9cSHugh Dickins 			min(end - index, (pgoff_t)PAGEVEC_SIZE),
4177a5d0fbbSHugh Dickins 			pvec.pages, indices);
4187a5d0fbbSHugh Dickins 		if (!pvec.nr)
4197a5d0fbbSHugh Dickins 			break;
420bda97eabSHugh Dickins 		for (i = 0; i < pagevec_count(&pvec); i++) {
421bda97eabSHugh Dickins 			struct page *page = pvec.pages[i];
422bda97eabSHugh Dickins 
4237a5d0fbbSHugh Dickins 			index = indices[i];
42483e4fa9cSHugh Dickins 			if (index >= end)
425bda97eabSHugh Dickins 				break;
426bda97eabSHugh Dickins 
4277a5d0fbbSHugh Dickins 			if (radix_tree_exceptional_entry(page)) {
4281635f6a7SHugh Dickins 				if (unfalloc)
4291635f6a7SHugh Dickins 					continue;
4307a5d0fbbSHugh Dickins 				nr_swaps_freed += !shmem_free_swap(mapping,
4317a5d0fbbSHugh Dickins 								index, page);
4327a5d0fbbSHugh Dickins 				continue;
4337a5d0fbbSHugh Dickins 			}
4347a5d0fbbSHugh Dickins 
435bda97eabSHugh Dickins 			if (!trylock_page(page))
436bda97eabSHugh Dickins 				continue;
4371635f6a7SHugh Dickins 			if (!unfalloc || !PageUptodate(page)) {
4387a5d0fbbSHugh Dickins 				if (page->mapping == mapping) {
439309381feSSasha Levin 					VM_BUG_ON_PAGE(PageWriteback(page), page);
440bda97eabSHugh Dickins 					truncate_inode_page(mapping, page);
4417a5d0fbbSHugh Dickins 				}
4421635f6a7SHugh Dickins 			}
443bda97eabSHugh Dickins 			unlock_page(page);
444bda97eabSHugh Dickins 		}
4450cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
44624513264SHugh Dickins 		pagevec_release(&pvec);
447bda97eabSHugh Dickins 		cond_resched();
448bda97eabSHugh Dickins 		index++;
449bda97eabSHugh Dickins 	}
450bda97eabSHugh Dickins 
45183e4fa9cSHugh Dickins 	if (partial_start) {
452bda97eabSHugh Dickins 		struct page *page = NULL;
453bda97eabSHugh Dickins 		shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
454bda97eabSHugh Dickins 		if (page) {
45583e4fa9cSHugh Dickins 			unsigned int top = PAGE_CACHE_SIZE;
45683e4fa9cSHugh Dickins 			if (start > end) {
45783e4fa9cSHugh Dickins 				top = partial_end;
45883e4fa9cSHugh Dickins 				partial_end = 0;
45983e4fa9cSHugh Dickins 			}
46083e4fa9cSHugh Dickins 			zero_user_segment(page, partial_start, top);
461bda97eabSHugh Dickins 			set_page_dirty(page);
462bda97eabSHugh Dickins 			unlock_page(page);
463bda97eabSHugh Dickins 			page_cache_release(page);
464bda97eabSHugh Dickins 		}
465bda97eabSHugh Dickins 	}
46683e4fa9cSHugh Dickins 	if (partial_end) {
46783e4fa9cSHugh Dickins 		struct page *page = NULL;
46883e4fa9cSHugh Dickins 		shmem_getpage(inode, end, &page, SGP_READ, NULL);
46983e4fa9cSHugh Dickins 		if (page) {
47083e4fa9cSHugh Dickins 			zero_user_segment(page, 0, partial_end);
47183e4fa9cSHugh Dickins 			set_page_dirty(page);
47283e4fa9cSHugh Dickins 			unlock_page(page);
47383e4fa9cSHugh Dickins 			page_cache_release(page);
47483e4fa9cSHugh Dickins 		}
47583e4fa9cSHugh Dickins 	}
47683e4fa9cSHugh Dickins 	if (start >= end)
47783e4fa9cSHugh Dickins 		return;
478bda97eabSHugh Dickins 
479bda97eabSHugh Dickins 	index = start;
480b1a36650SHugh Dickins 	while (index < end) {
481bda97eabSHugh Dickins 		cond_resched();
4820cd6144aSJohannes Weiner 
4830cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
48483e4fa9cSHugh Dickins 				min(end - index, (pgoff_t)PAGEVEC_SIZE),
4857a5d0fbbSHugh Dickins 				pvec.pages, indices);
4867a5d0fbbSHugh Dickins 		if (!pvec.nr) {
487b1a36650SHugh Dickins 			/* If all gone or hole-punch or unfalloc, we're done */
488b1a36650SHugh Dickins 			if (index == start || end != -1)
489bda97eabSHugh Dickins 				break;
490b1a36650SHugh Dickins 			/* But if truncating, restart to make sure all gone */
491bda97eabSHugh Dickins 			index = start;
492bda97eabSHugh Dickins 			continue;
493bda97eabSHugh Dickins 		}
494bda97eabSHugh Dickins 		for (i = 0; i < pagevec_count(&pvec); i++) {
495bda97eabSHugh Dickins 			struct page *page = pvec.pages[i];
496bda97eabSHugh Dickins 
4977a5d0fbbSHugh Dickins 			index = indices[i];
49883e4fa9cSHugh Dickins 			if (index >= end)
499bda97eabSHugh Dickins 				break;
500bda97eabSHugh Dickins 
5017a5d0fbbSHugh Dickins 			if (radix_tree_exceptional_entry(page)) {
5021635f6a7SHugh Dickins 				if (unfalloc)
5031635f6a7SHugh Dickins 					continue;
504b1a36650SHugh Dickins 				if (shmem_free_swap(mapping, index, page)) {
505b1a36650SHugh Dickins 					/* Swap was replaced by page: retry */
506b1a36650SHugh Dickins 					index--;
507b1a36650SHugh Dickins 					break;
508b1a36650SHugh Dickins 				}
509b1a36650SHugh Dickins 				nr_swaps_freed++;
5107a5d0fbbSHugh Dickins 				continue;
5117a5d0fbbSHugh Dickins 			}
5127a5d0fbbSHugh Dickins 
513bda97eabSHugh Dickins 			lock_page(page);
5141635f6a7SHugh Dickins 			if (!unfalloc || !PageUptodate(page)) {
5157a5d0fbbSHugh Dickins 				if (page->mapping == mapping) {
516309381feSSasha Levin 					VM_BUG_ON_PAGE(PageWriteback(page), page);
517bda97eabSHugh Dickins 					truncate_inode_page(mapping, page);
518b1a36650SHugh Dickins 				} else {
519b1a36650SHugh Dickins 					/* Page was replaced by swap: retry */
520b1a36650SHugh Dickins 					unlock_page(page);
521b1a36650SHugh Dickins 					index--;
522b1a36650SHugh Dickins 					break;
5237a5d0fbbSHugh Dickins 				}
5241635f6a7SHugh Dickins 			}
525bda97eabSHugh Dickins 			unlock_page(page);
526bda97eabSHugh Dickins 		}
5270cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
52824513264SHugh Dickins 		pagevec_release(&pvec);
529bda97eabSHugh Dickins 		index++;
530bda97eabSHugh Dickins 	}
53194c1e62dSHugh Dickins 
5321da177e4SLinus Torvalds 	spin_lock(&info->lock);
5337a5d0fbbSHugh Dickins 	info->swapped -= nr_swaps_freed;
5341da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
5351da177e4SLinus Torvalds 	spin_unlock(&info->lock);
5361635f6a7SHugh Dickins }
5371da177e4SLinus Torvalds 
5381635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
5391635f6a7SHugh Dickins {
5401635f6a7SHugh Dickins 	shmem_undo_range(inode, lstart, lend, false);
541285b2c4fSHugh Dickins 	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
5421da177e4SLinus Torvalds }
54394c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
5441da177e4SLinus Torvalds 
54544a30220SYu Zhao static int shmem_getattr(struct vfsmount *mnt, struct dentry *dentry,
54644a30220SYu Zhao 			 struct kstat *stat)
54744a30220SYu Zhao {
54844a30220SYu Zhao 	struct inode *inode = dentry->d_inode;
54944a30220SYu Zhao 	struct shmem_inode_info *info = SHMEM_I(inode);
55044a30220SYu Zhao 
551*d0424c42SHugh Dickins 	if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
55244a30220SYu Zhao 		spin_lock(&info->lock);
55344a30220SYu Zhao 		shmem_recalc_inode(inode);
55444a30220SYu Zhao 		spin_unlock(&info->lock);
555*d0424c42SHugh Dickins 	}
55644a30220SYu Zhao 	generic_fillattr(inode, stat);
55744a30220SYu Zhao 	return 0;
55844a30220SYu Zhao }
55944a30220SYu Zhao 
56094c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
5611da177e4SLinus Torvalds {
56275c3cfa8SDavid Howells 	struct inode *inode = d_inode(dentry);
56340e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
5641da177e4SLinus Torvalds 	int error;
5651da177e4SLinus Torvalds 
566db78b877SChristoph Hellwig 	error = inode_change_ok(inode, attr);
567db78b877SChristoph Hellwig 	if (error)
568db78b877SChristoph Hellwig 		return error;
569db78b877SChristoph Hellwig 
57094c1e62dSHugh Dickins 	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
57194c1e62dSHugh Dickins 		loff_t oldsize = inode->i_size;
57294c1e62dSHugh Dickins 		loff_t newsize = attr->ia_size;
5733889e6e7Snpiggin@suse.de 
57440e041a2SDavid Herrmann 		/* protected by i_mutex */
57540e041a2SDavid Herrmann 		if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
57640e041a2SDavid Herrmann 		    (newsize > oldsize && (info->seals & F_SEAL_GROW)))
57740e041a2SDavid Herrmann 			return -EPERM;
57840e041a2SDavid Herrmann 
57994c1e62dSHugh Dickins 		if (newsize != oldsize) {
58077142517SKonstantin Khlebnikov 			error = shmem_reacct_size(SHMEM_I(inode)->flags,
58177142517SKonstantin Khlebnikov 					oldsize, newsize);
58277142517SKonstantin Khlebnikov 			if (error)
58377142517SKonstantin Khlebnikov 				return error;
58494c1e62dSHugh Dickins 			i_size_write(inode, newsize);
58594c1e62dSHugh Dickins 			inode->i_ctime = inode->i_mtime = CURRENT_TIME;
58694c1e62dSHugh Dickins 		}
587afa2db2fSJosef Bacik 		if (newsize <= oldsize) {
58894c1e62dSHugh Dickins 			loff_t holebegin = round_up(newsize, PAGE_SIZE);
589*d0424c42SHugh Dickins 			if (oldsize > holebegin)
590*d0424c42SHugh Dickins 				unmap_mapping_range(inode->i_mapping,
591*d0424c42SHugh Dickins 							holebegin, 0, 1);
592*d0424c42SHugh Dickins 			if (info->alloced)
593*d0424c42SHugh Dickins 				shmem_truncate_range(inode,
594*d0424c42SHugh Dickins 							newsize, (loff_t)-1);
59594c1e62dSHugh Dickins 			/* unmap again to remove racily COWed private pages */
596*d0424c42SHugh Dickins 			if (oldsize > holebegin)
597*d0424c42SHugh Dickins 				unmap_mapping_range(inode->i_mapping,
598*d0424c42SHugh Dickins 							holebegin, 0, 1);
59994c1e62dSHugh Dickins 		}
6001da177e4SLinus Torvalds 	}
6011da177e4SLinus Torvalds 
6026a1a90adSChristoph Hellwig 	setattr_copy(inode, attr);
603db78b877SChristoph Hellwig 	if (attr->ia_valid & ATTR_MODE)
604feda821eSChristoph Hellwig 		error = posix_acl_chmod(inode, inode->i_mode);
6051da177e4SLinus Torvalds 	return error;
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
6081f895f75SAl Viro static void shmem_evict_inode(struct inode *inode)
6091da177e4SLinus Torvalds {
6101da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
6111da177e4SLinus Torvalds 
6123889e6e7Snpiggin@suse.de 	if (inode->i_mapping->a_ops == &shmem_aops) {
6131da177e4SLinus Torvalds 		shmem_unacct_size(info->flags, inode->i_size);
6141da177e4SLinus Torvalds 		inode->i_size = 0;
6153889e6e7Snpiggin@suse.de 		shmem_truncate_range(inode, 0, (loff_t)-1);
6161da177e4SLinus Torvalds 		if (!list_empty(&info->swaplist)) {
617cb5f7b9aSHugh Dickins 			mutex_lock(&shmem_swaplist_mutex);
6181da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
619cb5f7b9aSHugh Dickins 			mutex_unlock(&shmem_swaplist_mutex);
6201da177e4SLinus Torvalds 		}
62169f07ec9SHugh Dickins 	} else
62269f07ec9SHugh Dickins 		kfree(info->symlink);
623b09e0fa4SEric Paris 
62438f38657SAristeu Rozanski 	simple_xattrs_free(&info->xattrs);
6250f3c42f5SHugh Dickins 	WARN_ON(inode->i_blocks);
6265b04c689SPavel Emelyanov 	shmem_free_inode(inode->i_sb);
627dbd5768fSJan Kara 	clear_inode(inode);
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
63046f65ec1SHugh Dickins /*
63146f65ec1SHugh Dickins  * If swap found in inode, free it and move page from swapcache to filecache.
63246f65ec1SHugh Dickins  */
63341ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info,
634bde05d1cSHugh Dickins 			     swp_entry_t swap, struct page **pagep)
6351da177e4SLinus Torvalds {
636285b2c4fSHugh Dickins 	struct address_space *mapping = info->vfs_inode.i_mapping;
63746f65ec1SHugh Dickins 	void *radswap;
63841ffe5d5SHugh Dickins 	pgoff_t index;
639bde05d1cSHugh Dickins 	gfp_t gfp;
640bde05d1cSHugh Dickins 	int error = 0;
6411da177e4SLinus Torvalds 
64246f65ec1SHugh Dickins 	radswap = swp_to_radix_entry(swap);
643e504f3fdSHugh Dickins 	index = radix_tree_locate_item(&mapping->page_tree, radswap);
64446f65ec1SHugh Dickins 	if (index == -1)
64500501b53SJohannes Weiner 		return -EAGAIN;	/* tell shmem_unuse we found nothing */
6462e0e26c7SHugh Dickins 
6471b1b32f2SHugh Dickins 	/*
6481b1b32f2SHugh Dickins 	 * Move _head_ to start search for next from here.
6491f895f75SAl Viro 	 * But be careful: shmem_evict_inode checks list_empty without taking
6501b1b32f2SHugh Dickins 	 * mutex, and there's an instant in list_move_tail when info->swaplist
651285b2c4fSHugh Dickins 	 * would appear empty, if it were the only one on shmem_swaplist.
6521b1b32f2SHugh Dickins 	 */
6531b1b32f2SHugh Dickins 	if (shmem_swaplist.next != &info->swaplist)
6542e0e26c7SHugh Dickins 		list_move_tail(&shmem_swaplist, &info->swaplist);
6552e0e26c7SHugh Dickins 
656bde05d1cSHugh Dickins 	gfp = mapping_gfp_mask(mapping);
657bde05d1cSHugh Dickins 	if (shmem_should_replace_page(*pagep, gfp)) {
658bde05d1cSHugh Dickins 		mutex_unlock(&shmem_swaplist_mutex);
659bde05d1cSHugh Dickins 		error = shmem_replace_page(pagep, gfp, info, index);
660bde05d1cSHugh Dickins 		mutex_lock(&shmem_swaplist_mutex);
661bde05d1cSHugh Dickins 		/*
662bde05d1cSHugh Dickins 		 * We needed to drop mutex to make that restrictive page
6630142ef6cSHugh Dickins 		 * allocation, but the inode might have been freed while we
6640142ef6cSHugh Dickins 		 * dropped it: although a racing shmem_evict_inode() cannot
6650142ef6cSHugh Dickins 		 * complete without emptying the radix_tree, our page lock
6660142ef6cSHugh Dickins 		 * on this swapcache page is not enough to prevent that -
6670142ef6cSHugh Dickins 		 * free_swap_and_cache() of our swap entry will only
6680142ef6cSHugh Dickins 		 * trylock_page(), removing swap from radix_tree whatever.
6690142ef6cSHugh Dickins 		 *
6700142ef6cSHugh Dickins 		 * We must not proceed to shmem_add_to_page_cache() if the
6710142ef6cSHugh Dickins 		 * inode has been freed, but of course we cannot rely on
6720142ef6cSHugh Dickins 		 * inode or mapping or info to check that.  However, we can
6730142ef6cSHugh Dickins 		 * safely check if our swap entry is still in use (and here
6740142ef6cSHugh Dickins 		 * it can't have got reused for another page): if it's still
6750142ef6cSHugh Dickins 		 * in use, then the inode cannot have been freed yet, and we
6760142ef6cSHugh Dickins 		 * can safely proceed (if it's no longer in use, that tells
6770142ef6cSHugh Dickins 		 * nothing about the inode, but we don't need to unuse swap).
678bde05d1cSHugh Dickins 		 */
679bde05d1cSHugh Dickins 		if (!page_swapcount(*pagep))
680bde05d1cSHugh Dickins 			error = -ENOENT;
681bde05d1cSHugh Dickins 	}
682bde05d1cSHugh Dickins 
683d13d1443SKAMEZAWA Hiroyuki 	/*
684778dd893SHugh Dickins 	 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
685778dd893SHugh Dickins 	 * but also to hold up shmem_evict_inode(): so inode cannot be freed
686778dd893SHugh Dickins 	 * beneath us (pagelock doesn't help until the page is in pagecache).
687d13d1443SKAMEZAWA Hiroyuki 	 */
688bde05d1cSHugh Dickins 	if (!error)
689bde05d1cSHugh Dickins 		error = shmem_add_to_page_cache(*pagep, mapping, index,
690fed400a1SWang Sheng-Hui 						radswap);
69148f170fbSHugh Dickins 	if (error != -ENOMEM) {
69246f65ec1SHugh Dickins 		/*
69346f65ec1SHugh Dickins 		 * Truncation and eviction use free_swap_and_cache(), which
69446f65ec1SHugh Dickins 		 * only does trylock page: if we raced, best clean up here.
69546f65ec1SHugh Dickins 		 */
696bde05d1cSHugh Dickins 		delete_from_swap_cache(*pagep);
697bde05d1cSHugh Dickins 		set_page_dirty(*pagep);
69846f65ec1SHugh Dickins 		if (!error) {
69946f65ec1SHugh Dickins 			spin_lock(&info->lock);
700285b2c4fSHugh Dickins 			info->swapped--;
70146f65ec1SHugh Dickins 			spin_unlock(&info->lock);
70241ffe5d5SHugh Dickins 			swap_free(swap);
70346f65ec1SHugh Dickins 		}
7041da177e4SLinus Torvalds 	}
7052e0e26c7SHugh Dickins 	return error;
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds /*
70946f65ec1SHugh Dickins  * Search through swapped inodes to find and replace swap by page.
7101da177e4SLinus Torvalds  */
71141ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page)
7121da177e4SLinus Torvalds {
71341ffe5d5SHugh Dickins 	struct list_head *this, *next;
7141da177e4SLinus Torvalds 	struct shmem_inode_info *info;
71500501b53SJohannes Weiner 	struct mem_cgroup *memcg;
716bde05d1cSHugh Dickins 	int error = 0;
717bde05d1cSHugh Dickins 
718bde05d1cSHugh Dickins 	/*
719bde05d1cSHugh Dickins 	 * There's a faint possibility that swap page was replaced before
7200142ef6cSHugh Dickins 	 * caller locked it: caller will come back later with the right page.
721bde05d1cSHugh Dickins 	 */
7220142ef6cSHugh Dickins 	if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
723bde05d1cSHugh Dickins 		goto out;
724778dd893SHugh Dickins 
725778dd893SHugh Dickins 	/*
726778dd893SHugh Dickins 	 * Charge page using GFP_KERNEL while we can wait, before taking
727778dd893SHugh Dickins 	 * the shmem_swaplist_mutex which might hold up shmem_writepage().
728778dd893SHugh Dickins 	 * Charged back to the user (not to caller) when swap account is used.
729778dd893SHugh Dickins 	 */
73000501b53SJohannes Weiner 	error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg);
731778dd893SHugh Dickins 	if (error)
732778dd893SHugh Dickins 		goto out;
73346f65ec1SHugh Dickins 	/* No radix_tree_preload: swap entry keeps a place for page in tree */
73400501b53SJohannes Weiner 	error = -EAGAIN;
7351da177e4SLinus Torvalds 
736cb5f7b9aSHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
73741ffe5d5SHugh Dickins 	list_for_each_safe(this, next, &shmem_swaplist) {
73841ffe5d5SHugh Dickins 		info = list_entry(this, struct shmem_inode_info, swaplist);
739285b2c4fSHugh Dickins 		if (info->swapped)
74000501b53SJohannes Weiner 			error = shmem_unuse_inode(info, swap, &page);
7416922c0c7SHugh Dickins 		else
7426922c0c7SHugh Dickins 			list_del_init(&info->swaplist);
743cb5f7b9aSHugh Dickins 		cond_resched();
74400501b53SJohannes Weiner 		if (error != -EAGAIN)
745778dd893SHugh Dickins 			break;
74600501b53SJohannes Weiner 		/* found nothing in this: move on to search the next */
7471da177e4SLinus Torvalds 	}
748cb5f7b9aSHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
749778dd893SHugh Dickins 
75000501b53SJohannes Weiner 	if (error) {
75100501b53SJohannes Weiner 		if (error != -ENOMEM)
75200501b53SJohannes Weiner 			error = 0;
75300501b53SJohannes Weiner 		mem_cgroup_cancel_charge(page, memcg);
75400501b53SJohannes Weiner 	} else
75500501b53SJohannes Weiner 		mem_cgroup_commit_charge(page, memcg, true);
756778dd893SHugh Dickins out:
757aaa46865SHugh Dickins 	unlock_page(page);
758aaa46865SHugh Dickins 	page_cache_release(page);
759778dd893SHugh Dickins 	return error;
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds /*
7631da177e4SLinus Torvalds  * Move the page from the page cache to the swap cache.
7641da177e4SLinus Torvalds  */
7651da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc)
7661da177e4SLinus Torvalds {
7671da177e4SLinus Torvalds 	struct shmem_inode_info *info;
7681da177e4SLinus Torvalds 	struct address_space *mapping;
7691da177e4SLinus Torvalds 	struct inode *inode;
7706922c0c7SHugh Dickins 	swp_entry_t swap;
7716922c0c7SHugh Dickins 	pgoff_t index;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
7741da177e4SLinus Torvalds 	mapping = page->mapping;
7751da177e4SLinus Torvalds 	index = page->index;
7761da177e4SLinus Torvalds 	inode = mapping->host;
7771da177e4SLinus Torvalds 	info = SHMEM_I(inode);
7781da177e4SLinus Torvalds 	if (info->flags & VM_LOCKED)
7791da177e4SLinus Torvalds 		goto redirty;
780d9fe526aSHugh Dickins 	if (!total_swap_pages)
7811da177e4SLinus Torvalds 		goto redirty;
7821da177e4SLinus Torvalds 
783d9fe526aSHugh Dickins 	/*
78497b713baSChristoph Hellwig 	 * Our capabilities prevent regular writeback or sync from ever calling
78597b713baSChristoph Hellwig 	 * shmem_writepage; but a stacking filesystem might use ->writepage of
78697b713baSChristoph Hellwig 	 * its underlying filesystem, in which case tmpfs should write out to
78797b713baSChristoph Hellwig 	 * swap only in response to memory pressure, and not for the writeback
78897b713baSChristoph Hellwig 	 * threads or sync.
789d9fe526aSHugh Dickins 	 */
79048f170fbSHugh Dickins 	if (!wbc->for_reclaim) {
79148f170fbSHugh Dickins 		WARN_ON_ONCE(1);	/* Still happens? Tell us about it! */
79248f170fbSHugh Dickins 		goto redirty;
79348f170fbSHugh Dickins 	}
7941635f6a7SHugh Dickins 
7951635f6a7SHugh Dickins 	/*
7961635f6a7SHugh Dickins 	 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
7971635f6a7SHugh Dickins 	 * value into swapfile.c, the only way we can correctly account for a
7981635f6a7SHugh Dickins 	 * fallocated page arriving here is now to initialize it and write it.
7991aac1400SHugh Dickins 	 *
8001aac1400SHugh Dickins 	 * That's okay for a page already fallocated earlier, but if we have
8011aac1400SHugh Dickins 	 * not yet completed the fallocation, then (a) we want to keep track
8021aac1400SHugh Dickins 	 * of this page in case we have to undo it, and (b) it may not be a
8031aac1400SHugh Dickins 	 * good idea to continue anyway, once we're pushing into swap.  So
8041aac1400SHugh Dickins 	 * reactivate the page, and let shmem_fallocate() quit when too many.
8051635f6a7SHugh Dickins 	 */
8061635f6a7SHugh Dickins 	if (!PageUptodate(page)) {
8071aac1400SHugh Dickins 		if (inode->i_private) {
8081aac1400SHugh Dickins 			struct shmem_falloc *shmem_falloc;
8091aac1400SHugh Dickins 			spin_lock(&inode->i_lock);
8101aac1400SHugh Dickins 			shmem_falloc = inode->i_private;
8111aac1400SHugh Dickins 			if (shmem_falloc &&
8128e205f77SHugh Dickins 			    !shmem_falloc->waitq &&
8131aac1400SHugh Dickins 			    index >= shmem_falloc->start &&
8141aac1400SHugh Dickins 			    index < shmem_falloc->next)
8151aac1400SHugh Dickins 				shmem_falloc->nr_unswapped++;
8161aac1400SHugh Dickins 			else
8171aac1400SHugh Dickins 				shmem_falloc = NULL;
8181aac1400SHugh Dickins 			spin_unlock(&inode->i_lock);
8191aac1400SHugh Dickins 			if (shmem_falloc)
8201aac1400SHugh Dickins 				goto redirty;
8211aac1400SHugh Dickins 		}
8221635f6a7SHugh Dickins 		clear_highpage(page);
8231635f6a7SHugh Dickins 		flush_dcache_page(page);
8241635f6a7SHugh Dickins 		SetPageUptodate(page);
8251635f6a7SHugh Dickins 	}
8261635f6a7SHugh Dickins 
827d9fe526aSHugh Dickins 	swap = get_swap_page();
82848f170fbSHugh Dickins 	if (!swap.val)
82948f170fbSHugh Dickins 		goto redirty;
830d9fe526aSHugh Dickins 
831b1dea800SHugh Dickins 	/*
832b1dea800SHugh Dickins 	 * Add inode to shmem_unuse()'s list of swapped-out inodes,
8336922c0c7SHugh Dickins 	 * if it's not already there.  Do it now before the page is
8346922c0c7SHugh Dickins 	 * moved to swap cache, when its pagelock no longer protects
835b1dea800SHugh Dickins 	 * the inode from eviction.  But don't unlock the mutex until
8366922c0c7SHugh Dickins 	 * we've incremented swapped, because shmem_unuse_inode() will
8376922c0c7SHugh Dickins 	 * prune a !swapped inode from the swaplist under this mutex.
838b1dea800SHugh Dickins 	 */
839b1dea800SHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
84005bf86b4SHugh Dickins 	if (list_empty(&info->swaplist))
84105bf86b4SHugh Dickins 		list_add_tail(&info->swaplist, &shmem_swaplist);
842b1dea800SHugh Dickins 
84348f170fbSHugh Dickins 	if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
844aaa46865SHugh Dickins 		swap_shmem_alloc(swap);
8456922c0c7SHugh Dickins 		shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
8466922c0c7SHugh Dickins 
8476922c0c7SHugh Dickins 		spin_lock(&info->lock);
8486922c0c7SHugh Dickins 		info->swapped++;
8496922c0c7SHugh Dickins 		shmem_recalc_inode(inode);
850826267cfSHugh Dickins 		spin_unlock(&info->lock);
8516922c0c7SHugh Dickins 
8526922c0c7SHugh Dickins 		mutex_unlock(&shmem_swaplist_mutex);
853d9fe526aSHugh Dickins 		BUG_ON(page_mapped(page));
8549fab5619SHugh Dickins 		swap_writepage(page, wbc);
8551da177e4SLinus Torvalds 		return 0;
8561da177e4SLinus Torvalds 	}
8571da177e4SLinus Torvalds 
8586922c0c7SHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
8590a31bc97SJohannes Weiner 	swapcache_free(swap);
8601da177e4SLinus Torvalds redirty:
8611da177e4SLinus Torvalds 	set_page_dirty(page);
862d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
863d9fe526aSHugh Dickins 		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
864d9fe526aSHugh Dickins 	unlock_page(page);
865d9fe526aSHugh Dickins 	return 0;
8661da177e4SLinus Torvalds }
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds #ifdef CONFIG_NUMA
869680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
87071fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
871680d794bSakpm@linux-foundation.org {
872680d794bSakpm@linux-foundation.org 	char buffer[64];
873680d794bSakpm@linux-foundation.org 
87471fe804bSLee Schermerhorn 	if (!mpol || mpol->mode == MPOL_DEFAULT)
875095f1fc4SLee Schermerhorn 		return;		/* show nothing */
876095f1fc4SLee Schermerhorn 
877a7a88b23SHugh Dickins 	mpol_to_str(buffer, sizeof(buffer), mpol);
878095f1fc4SLee Schermerhorn 
879095f1fc4SLee Schermerhorn 	seq_printf(seq, ",mpol=%s", buffer);
880680d794bSakpm@linux-foundation.org }
88171fe804bSLee Schermerhorn 
88271fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
88371fe804bSLee Schermerhorn {
88471fe804bSLee Schermerhorn 	struct mempolicy *mpol = NULL;
88571fe804bSLee Schermerhorn 	if (sbinfo->mpol) {
88671fe804bSLee Schermerhorn 		spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
88771fe804bSLee Schermerhorn 		mpol = sbinfo->mpol;
88871fe804bSLee Schermerhorn 		mpol_get(mpol);
88971fe804bSLee Schermerhorn 		spin_unlock(&sbinfo->stat_lock);
89071fe804bSLee Schermerhorn 	}
89171fe804bSLee Schermerhorn 	return mpol;
89271fe804bSLee Schermerhorn }
893680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
894680d794bSakpm@linux-foundation.org 
89541ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
89641ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
8971da177e4SLinus Torvalds {
8981da177e4SLinus Torvalds 	struct vm_area_struct pvma;
89918a2f371SMel Gorman 	struct page *page;
9001da177e4SLinus Torvalds 
901c4cc6d07SHugh Dickins 	/* Create a pseudo vma that just contains the policy */
902c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
90309c231cbSNathan Zimmer 	/* Bias interleave by inode number to distribute better across nodes */
90409c231cbSNathan Zimmer 	pvma.vm_pgoff = index + info->vfs_inode.i_ino;
905c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
90641ffe5d5SHugh Dickins 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
90752cd3b07SLee Schermerhorn 
90818a2f371SMel Gorman 	page = swapin_readahead(swap, gfp, &pvma, 0);
90918a2f371SMel Gorman 
91018a2f371SMel Gorman 	/* Drop reference taken by mpol_shared_policy_lookup() */
91118a2f371SMel Gorman 	mpol_cond_put(pvma.vm_policy);
91218a2f371SMel Gorman 
91318a2f371SMel Gorman 	return page;
91418a2f371SMel Gorman }
91518a2f371SMel Gorman 
91618a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp,
91718a2f371SMel Gorman 			struct shmem_inode_info *info, pgoff_t index)
91818a2f371SMel Gorman {
91918a2f371SMel Gorman 	struct vm_area_struct pvma;
92018a2f371SMel Gorman 	struct page *page;
92118a2f371SMel Gorman 
92218a2f371SMel Gorman 	/* Create a pseudo vma that just contains the policy */
92318a2f371SMel Gorman 	pvma.vm_start = 0;
92418a2f371SMel Gorman 	/* Bias interleave by inode number to distribute better across nodes */
92518a2f371SMel Gorman 	pvma.vm_pgoff = index + info->vfs_inode.i_ino;
92618a2f371SMel Gorman 	pvma.vm_ops = NULL;
92718a2f371SMel Gorman 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
92818a2f371SMel Gorman 
92918a2f371SMel Gorman 	page = alloc_page_vma(gfp, &pvma, 0);
93018a2f371SMel Gorman 
93118a2f371SMel Gorman 	/* Drop reference taken by mpol_shared_policy_lookup() */
93218a2f371SMel Gorman 	mpol_cond_put(pvma.vm_policy);
93318a2f371SMel Gorman 
93418a2f371SMel Gorman 	return page;
9351da177e4SLinus Torvalds }
936680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */
937680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
93841ffe5d5SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
939680d794bSakpm@linux-foundation.org {
940680d794bSakpm@linux-foundation.org }
941680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
942680d794bSakpm@linux-foundation.org 
94341ffe5d5SHugh Dickins static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
94441ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
9451da177e4SLinus Torvalds {
94641ffe5d5SHugh Dickins 	return swapin_readahead(swap, gfp, NULL, 0);
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
94902098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp,
95041ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
9511da177e4SLinus Torvalds {
952e84e2e13SHugh Dickins 	return alloc_page(gfp);
9531da177e4SLinus Torvalds }
954680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */
9551da177e4SLinus Torvalds 
95671fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
95771fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
95871fe804bSLee Schermerhorn {
95971fe804bSLee Schermerhorn 	return NULL;
96071fe804bSLee Schermerhorn }
96171fe804bSLee Schermerhorn #endif
96271fe804bSLee Schermerhorn 
9631da177e4SLinus Torvalds /*
964bde05d1cSHugh Dickins  * When a page is moved from swapcache to shmem filecache (either by the
965bde05d1cSHugh Dickins  * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
966bde05d1cSHugh Dickins  * shmem_unuse_inode()), it may have been read in earlier from swap, in
967bde05d1cSHugh Dickins  * ignorance of the mapping it belongs to.  If that mapping has special
968bde05d1cSHugh Dickins  * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
969bde05d1cSHugh Dickins  * we may need to copy to a suitable page before moving to filecache.
970bde05d1cSHugh Dickins  *
971bde05d1cSHugh Dickins  * In a future release, this may well be extended to respect cpuset and
972bde05d1cSHugh Dickins  * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
973bde05d1cSHugh Dickins  * but for now it is a simple matter of zone.
974bde05d1cSHugh Dickins  */
975bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
976bde05d1cSHugh Dickins {
977bde05d1cSHugh Dickins 	return page_zonenum(page) > gfp_zone(gfp);
978bde05d1cSHugh Dickins }
979bde05d1cSHugh Dickins 
980bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp,
981bde05d1cSHugh Dickins 				struct shmem_inode_info *info, pgoff_t index)
982bde05d1cSHugh Dickins {
983bde05d1cSHugh Dickins 	struct page *oldpage, *newpage;
984bde05d1cSHugh Dickins 	struct address_space *swap_mapping;
985bde05d1cSHugh Dickins 	pgoff_t swap_index;
986bde05d1cSHugh Dickins 	int error;
987bde05d1cSHugh Dickins 
988bde05d1cSHugh Dickins 	oldpage = *pagep;
989bde05d1cSHugh Dickins 	swap_index = page_private(oldpage);
990bde05d1cSHugh Dickins 	swap_mapping = page_mapping(oldpage);
991bde05d1cSHugh Dickins 
992bde05d1cSHugh Dickins 	/*
993bde05d1cSHugh Dickins 	 * We have arrived here because our zones are constrained, so don't
994bde05d1cSHugh Dickins 	 * limit chance of success by further cpuset and node constraints.
995bde05d1cSHugh Dickins 	 */
996bde05d1cSHugh Dickins 	gfp &= ~GFP_CONSTRAINT_MASK;
997bde05d1cSHugh Dickins 	newpage = shmem_alloc_page(gfp, info, index);
998bde05d1cSHugh Dickins 	if (!newpage)
999bde05d1cSHugh Dickins 		return -ENOMEM;
1000bde05d1cSHugh Dickins 
1001bde05d1cSHugh Dickins 	page_cache_get(newpage);
1002bde05d1cSHugh Dickins 	copy_highpage(newpage, oldpage);
10030142ef6cSHugh Dickins 	flush_dcache_page(newpage);
1004bde05d1cSHugh Dickins 
1005bde05d1cSHugh Dickins 	__set_page_locked(newpage);
1006bde05d1cSHugh Dickins 	SetPageUptodate(newpage);
1007bde05d1cSHugh Dickins 	SetPageSwapBacked(newpage);
1008bde05d1cSHugh Dickins 	set_page_private(newpage, swap_index);
1009bde05d1cSHugh Dickins 	SetPageSwapCache(newpage);
1010bde05d1cSHugh Dickins 
1011bde05d1cSHugh Dickins 	/*
1012bde05d1cSHugh Dickins 	 * Our caller will very soon move newpage out of swapcache, but it's
1013bde05d1cSHugh Dickins 	 * a nice clean interface for us to replace oldpage by newpage there.
1014bde05d1cSHugh Dickins 	 */
1015bde05d1cSHugh Dickins 	spin_lock_irq(&swap_mapping->tree_lock);
1016bde05d1cSHugh Dickins 	error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
1017bde05d1cSHugh Dickins 								   newpage);
10180142ef6cSHugh Dickins 	if (!error) {
1019bde05d1cSHugh Dickins 		__inc_zone_page_state(newpage, NR_FILE_PAGES);
1020bde05d1cSHugh Dickins 		__dec_zone_page_state(oldpage, NR_FILE_PAGES);
10210142ef6cSHugh Dickins 	}
1022bde05d1cSHugh Dickins 	spin_unlock_irq(&swap_mapping->tree_lock);
1023bde05d1cSHugh Dickins 
10240142ef6cSHugh Dickins 	if (unlikely(error)) {
10250142ef6cSHugh Dickins 		/*
10260142ef6cSHugh Dickins 		 * Is this possible?  I think not, now that our callers check
10270142ef6cSHugh Dickins 		 * both PageSwapCache and page_private after getting page lock;
10280142ef6cSHugh Dickins 		 * but be defensive.  Reverse old to newpage for clear and free.
10290142ef6cSHugh Dickins 		 */
10300142ef6cSHugh Dickins 		oldpage = newpage;
10310142ef6cSHugh Dickins 	} else {
103245637babSHugh Dickins 		mem_cgroup_replace_page(oldpage, newpage);
1033bde05d1cSHugh Dickins 		lru_cache_add_anon(newpage);
10340142ef6cSHugh Dickins 		*pagep = newpage;
10350142ef6cSHugh Dickins 	}
1036bde05d1cSHugh Dickins 
1037bde05d1cSHugh Dickins 	ClearPageSwapCache(oldpage);
1038bde05d1cSHugh Dickins 	set_page_private(oldpage, 0);
1039bde05d1cSHugh Dickins 
1040bde05d1cSHugh Dickins 	unlock_page(oldpage);
1041bde05d1cSHugh Dickins 	page_cache_release(oldpage);
1042bde05d1cSHugh Dickins 	page_cache_release(oldpage);
10430142ef6cSHugh Dickins 	return error;
1044bde05d1cSHugh Dickins }
1045bde05d1cSHugh Dickins 
1046bde05d1cSHugh Dickins /*
104768da9f05SHugh Dickins  * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
10481da177e4SLinus Torvalds  *
10491da177e4SLinus Torvalds  * If we allocate a new one we do not mark it dirty. That's up to the
10501da177e4SLinus Torvalds  * vm. If we swap it in we mark it dirty since we also free the swap
10511da177e4SLinus Torvalds  * entry since a page cannot live in both the swap and page cache
10521da177e4SLinus Torvalds  */
105341ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
105468da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
10551da177e4SLinus Torvalds {
10561da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
105754af6042SHugh Dickins 	struct shmem_inode_info *info;
10581da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo;
105900501b53SJohannes Weiner 	struct mem_cgroup *memcg;
106027ab7006SHugh Dickins 	struct page *page;
10611da177e4SLinus Torvalds 	swp_entry_t swap;
10621da177e4SLinus Torvalds 	int error;
106354af6042SHugh Dickins 	int once = 0;
10641635f6a7SHugh Dickins 	int alloced = 0;
10651da177e4SLinus Torvalds 
106641ffe5d5SHugh Dickins 	if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
10671da177e4SLinus Torvalds 		return -EFBIG;
10681da177e4SLinus Torvalds repeat:
106954af6042SHugh Dickins 	swap.val = 0;
10700cd6144aSJohannes Weiner 	page = find_lock_entry(mapping, index);
107154af6042SHugh Dickins 	if (radix_tree_exceptional_entry(page)) {
107254af6042SHugh Dickins 		swap = radix_to_swp_entry(page);
107354af6042SHugh Dickins 		page = NULL;
107454af6042SHugh Dickins 	}
107554af6042SHugh Dickins 
10761635f6a7SHugh Dickins 	if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
107754af6042SHugh Dickins 	    ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
107854af6042SHugh Dickins 		error = -EINVAL;
107954af6042SHugh Dickins 		goto failed;
108054af6042SHugh Dickins 	}
108154af6042SHugh Dickins 
108266d2f4d2SHugh Dickins 	if (page && sgp == SGP_WRITE)
108366d2f4d2SHugh Dickins 		mark_page_accessed(page);
108466d2f4d2SHugh Dickins 
10851635f6a7SHugh Dickins 	/* fallocated page? */
10861635f6a7SHugh Dickins 	if (page && !PageUptodate(page)) {
10871635f6a7SHugh Dickins 		if (sgp != SGP_READ)
10881635f6a7SHugh Dickins 			goto clear;
10891635f6a7SHugh Dickins 		unlock_page(page);
10901635f6a7SHugh Dickins 		page_cache_release(page);
10911635f6a7SHugh Dickins 		page = NULL;
10921635f6a7SHugh Dickins 	}
109354af6042SHugh Dickins 	if (page || (sgp == SGP_READ && !swap.val)) {
109454af6042SHugh Dickins 		*pagep = page;
109554af6042SHugh Dickins 		return 0;
109627ab7006SHugh Dickins 	}
109727ab7006SHugh Dickins 
1098b409f9fcSHugh Dickins 	/*
109954af6042SHugh Dickins 	 * Fast cache lookup did not find it:
110054af6042SHugh Dickins 	 * bring it back from swap or allocate.
1101b409f9fcSHugh Dickins 	 */
110254af6042SHugh Dickins 	info = SHMEM_I(inode);
110354af6042SHugh Dickins 	sbinfo = SHMEM_SB(inode->i_sb);
110427ab7006SHugh Dickins 
11051da177e4SLinus Torvalds 	if (swap.val) {
11061da177e4SLinus Torvalds 		/* Look it up and read it in.. */
110727ab7006SHugh Dickins 		page = lookup_swap_cache(swap);
110827ab7006SHugh Dickins 		if (!page) {
1109456f998eSYing Han 			/* here we actually do the io */
111068da9f05SHugh Dickins 			if (fault_type)
111168da9f05SHugh Dickins 				*fault_type |= VM_FAULT_MAJOR;
111241ffe5d5SHugh Dickins 			page = shmem_swapin(swap, gfp, info, index);
111327ab7006SHugh Dickins 			if (!page) {
11141da177e4SLinus Torvalds 				error = -ENOMEM;
111554af6042SHugh Dickins 				goto failed;
1116285b2c4fSHugh Dickins 			}
11171da177e4SLinus Torvalds 		}
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 		/* We have to do this with page locked to prevent races */
112054af6042SHugh Dickins 		lock_page(page);
11210142ef6cSHugh Dickins 		if (!PageSwapCache(page) || page_private(page) != swap.val ||
1122d1899228SHugh Dickins 		    !shmem_confirm_swap(mapping, index, swap)) {
1123bde05d1cSHugh Dickins 			error = -EEXIST;	/* try again */
1124d1899228SHugh Dickins 			goto unlock;
1125bde05d1cSHugh Dickins 		}
112627ab7006SHugh Dickins 		if (!PageUptodate(page)) {
11271da177e4SLinus Torvalds 			error = -EIO;
112854af6042SHugh Dickins 			goto failed;
112954af6042SHugh Dickins 		}
113054af6042SHugh Dickins 		wait_on_page_writeback(page);
113154af6042SHugh Dickins 
1132bde05d1cSHugh Dickins 		if (shmem_should_replace_page(page, gfp)) {
1133bde05d1cSHugh Dickins 			error = shmem_replace_page(&page, gfp, info, index);
1134bde05d1cSHugh Dickins 			if (error)
113554af6042SHugh Dickins 				goto failed;
11361da177e4SLinus Torvalds 		}
11371da177e4SLinus Torvalds 
113800501b53SJohannes Weiner 		error = mem_cgroup_try_charge(page, current->mm, gfp, &memcg);
1139d1899228SHugh Dickins 		if (!error) {
114054af6042SHugh Dickins 			error = shmem_add_to_page_cache(page, mapping, index,
1141fed400a1SWang Sheng-Hui 						swp_to_radix_entry(swap));
1142215c02bcSHugh Dickins 			/*
1143215c02bcSHugh Dickins 			 * We already confirmed swap under page lock, and make
1144215c02bcSHugh Dickins 			 * no memory allocation here, so usually no possibility
1145215c02bcSHugh Dickins 			 * of error; but free_swap_and_cache() only trylocks a
1146215c02bcSHugh Dickins 			 * page, so it is just possible that the entry has been
1147215c02bcSHugh Dickins 			 * truncated or holepunched since swap was confirmed.
1148215c02bcSHugh Dickins 			 * shmem_undo_range() will have done some of the
1149215c02bcSHugh Dickins 			 * unaccounting, now delete_from_swap_cache() will do
115093aa7d95SVladimir Davydov 			 * the rest.
1151215c02bcSHugh Dickins 			 * Reset swap.val? No, leave it so "failed" goes back to
1152215c02bcSHugh Dickins 			 * "repeat": reading a hole and writing should succeed.
1153215c02bcSHugh Dickins 			 */
115400501b53SJohannes Weiner 			if (error) {
115500501b53SJohannes Weiner 				mem_cgroup_cancel_charge(page, memcg);
1156215c02bcSHugh Dickins 				delete_from_swap_cache(page);
1157d1899228SHugh Dickins 			}
115800501b53SJohannes Weiner 		}
115954af6042SHugh Dickins 		if (error)
116054af6042SHugh Dickins 			goto failed;
116154af6042SHugh Dickins 
116200501b53SJohannes Weiner 		mem_cgroup_commit_charge(page, memcg, true);
116300501b53SJohannes Weiner 
116454af6042SHugh Dickins 		spin_lock(&info->lock);
116554af6042SHugh Dickins 		info->swapped--;
116654af6042SHugh Dickins 		shmem_recalc_inode(inode);
11671da177e4SLinus Torvalds 		spin_unlock(&info->lock);
116827ab7006SHugh Dickins 
116966d2f4d2SHugh Dickins 		if (sgp == SGP_WRITE)
117066d2f4d2SHugh Dickins 			mark_page_accessed(page);
117166d2f4d2SHugh Dickins 
117227ab7006SHugh Dickins 		delete_from_swap_cache(page);
117327ab7006SHugh Dickins 		set_page_dirty(page);
117427ab7006SHugh Dickins 		swap_free(swap);
117527ab7006SHugh Dickins 
117654af6042SHugh Dickins 	} else {
117754af6042SHugh Dickins 		if (shmem_acct_block(info->flags)) {
117854af6042SHugh Dickins 			error = -ENOSPC;
117954af6042SHugh Dickins 			goto failed;
11801da177e4SLinus Torvalds 		}
11810edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
1182fc5da22aSHugh Dickins 			if (percpu_counter_compare(&sbinfo->used_blocks,
118354af6042SHugh Dickins 						sbinfo->max_blocks) >= 0) {
118454af6042SHugh Dickins 				error = -ENOSPC;
118554af6042SHugh Dickins 				goto unacct;
118654af6042SHugh Dickins 			}
11877e496299STim Chen 			percpu_counter_inc(&sbinfo->used_blocks);
118859a16eadSHugh Dickins 		}
11891da177e4SLinus Torvalds 
119054af6042SHugh Dickins 		page = shmem_alloc_page(gfp, info, index);
119154af6042SHugh Dickins 		if (!page) {
119254af6042SHugh Dickins 			error = -ENOMEM;
119354af6042SHugh Dickins 			goto decused;
119454af6042SHugh Dickins 		}
119554af6042SHugh Dickins 
119607a42788SMel Gorman 		__SetPageSwapBacked(page);
119754af6042SHugh Dickins 		__set_page_locked(page);
119866d2f4d2SHugh Dickins 		if (sgp == SGP_WRITE)
1199eb39d618SHugh Dickins 			__SetPageReferenced(page);
120066d2f4d2SHugh Dickins 
120100501b53SJohannes Weiner 		error = mem_cgroup_try_charge(page, current->mm, gfp, &memcg);
120254af6042SHugh Dickins 		if (error)
120354af6042SHugh Dickins 			goto decused;
12045e4c0d97SJan Kara 		error = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK);
1205b065b432SHugh Dickins 		if (!error) {
1206b065b432SHugh Dickins 			error = shmem_add_to_page_cache(page, mapping, index,
1207fed400a1SWang Sheng-Hui 							NULL);
1208b065b432SHugh Dickins 			radix_tree_preload_end();
1209b065b432SHugh Dickins 		}
1210b065b432SHugh Dickins 		if (error) {
121100501b53SJohannes Weiner 			mem_cgroup_cancel_charge(page, memcg);
1212b065b432SHugh Dickins 			goto decused;
1213b065b432SHugh Dickins 		}
121400501b53SJohannes Weiner 		mem_cgroup_commit_charge(page, memcg, false);
121554af6042SHugh Dickins 		lru_cache_add_anon(page);
121654af6042SHugh Dickins 
121754af6042SHugh Dickins 		spin_lock(&info->lock);
12181da177e4SLinus Torvalds 		info->alloced++;
121954af6042SHugh Dickins 		inode->i_blocks += BLOCKS_PER_PAGE;
122054af6042SHugh Dickins 		shmem_recalc_inode(inode);
122159a16eadSHugh Dickins 		spin_unlock(&info->lock);
12221635f6a7SHugh Dickins 		alloced = true;
122354af6042SHugh Dickins 
1224ec9516fbSHugh Dickins 		/*
12251635f6a7SHugh Dickins 		 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
12261635f6a7SHugh Dickins 		 */
12271635f6a7SHugh Dickins 		if (sgp == SGP_FALLOC)
12281635f6a7SHugh Dickins 			sgp = SGP_WRITE;
12291635f6a7SHugh Dickins clear:
12301635f6a7SHugh Dickins 		/*
12311635f6a7SHugh Dickins 		 * Let SGP_WRITE caller clear ends if write does not fill page;
12321635f6a7SHugh Dickins 		 * but SGP_FALLOC on a page fallocated earlier must initialize
12331635f6a7SHugh Dickins 		 * it now, lest undo on failure cancel our earlier guarantee.
1234ec9516fbSHugh Dickins 		 */
1235ec9516fbSHugh Dickins 		if (sgp != SGP_WRITE) {
123627ab7006SHugh Dickins 			clear_highpage(page);
123727ab7006SHugh Dickins 			flush_dcache_page(page);
123827ab7006SHugh Dickins 			SetPageUptodate(page);
1239ec9516fbSHugh Dickins 		}
1240a0ee5ec5SHugh Dickins 		if (sgp == SGP_DIRTY)
124127ab7006SHugh Dickins 			set_page_dirty(page);
12421da177e4SLinus Torvalds 	}
1243bde05d1cSHugh Dickins 
124454af6042SHugh Dickins 	/* Perhaps the file has been truncated since we checked */
12451635f6a7SHugh Dickins 	if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
124654af6042SHugh Dickins 	    ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
124754af6042SHugh Dickins 		error = -EINVAL;
12481635f6a7SHugh Dickins 		if (alloced)
124954af6042SHugh Dickins 			goto trunc;
12501635f6a7SHugh Dickins 		else
12511635f6a7SHugh Dickins 			goto failed;
1252ff36b801SShaohua Li 	}
125354af6042SHugh Dickins 	*pagep = page;
125454af6042SHugh Dickins 	return 0;
1255d00806b1SNick Piggin 
1256d0217ac0SNick Piggin 	/*
125754af6042SHugh Dickins 	 * Error recovery.
12581da177e4SLinus Torvalds 	 */
125954af6042SHugh Dickins trunc:
12601635f6a7SHugh Dickins 	info = SHMEM_I(inode);
126154af6042SHugh Dickins 	ClearPageDirty(page);
126254af6042SHugh Dickins 	delete_from_page_cache(page);
126354af6042SHugh Dickins 	spin_lock(&info->lock);
126454af6042SHugh Dickins 	info->alloced--;
126554af6042SHugh Dickins 	inode->i_blocks -= BLOCKS_PER_PAGE;
12661da177e4SLinus Torvalds 	spin_unlock(&info->lock);
126754af6042SHugh Dickins decused:
12681635f6a7SHugh Dickins 	sbinfo = SHMEM_SB(inode->i_sb);
126954af6042SHugh Dickins 	if (sbinfo->max_blocks)
127054af6042SHugh Dickins 		percpu_counter_add(&sbinfo->used_blocks, -1);
127154af6042SHugh Dickins unacct:
127254af6042SHugh Dickins 	shmem_unacct_blocks(info->flags, 1);
127354af6042SHugh Dickins failed:
1274d1899228SHugh Dickins 	if (swap.val && error != -EINVAL &&
1275d1899228SHugh Dickins 	    !shmem_confirm_swap(mapping, index, swap))
127654af6042SHugh Dickins 		error = -EEXIST;
1277d1899228SHugh Dickins unlock:
127827ab7006SHugh Dickins 	if (page) {
127954af6042SHugh Dickins 		unlock_page(page);
12801da177e4SLinus Torvalds 		page_cache_release(page);
128154af6042SHugh Dickins 	}
128254af6042SHugh Dickins 	if (error == -ENOSPC && !once++) {
128354af6042SHugh Dickins 		info = SHMEM_I(inode);
128454af6042SHugh Dickins 		spin_lock(&info->lock);
128554af6042SHugh Dickins 		shmem_recalc_inode(inode);
128654af6042SHugh Dickins 		spin_unlock(&info->lock);
12871da177e4SLinus Torvalds 		goto repeat;
1288d8dc74f2SAdrian Bunk 	}
1289d1899228SHugh Dickins 	if (error == -EEXIST)	/* from above or from radix_tree_insert */
129054af6042SHugh Dickins 		goto repeat;
129154af6042SHugh Dickins 	return error;
12921da177e4SLinus Torvalds }
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
12951da177e4SLinus Torvalds {
1296496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
12971da177e4SLinus Torvalds 	int error;
129868da9f05SHugh Dickins 	int ret = VM_FAULT_LOCKED;
12991da177e4SLinus Torvalds 
1300f00cdc6dSHugh Dickins 	/*
1301f00cdc6dSHugh Dickins 	 * Trinity finds that probing a hole which tmpfs is punching can
1302f00cdc6dSHugh Dickins 	 * prevent the hole-punch from ever completing: which in turn
1303f00cdc6dSHugh Dickins 	 * locks writers out with its hold on i_mutex.  So refrain from
13048e205f77SHugh Dickins 	 * faulting pages into the hole while it's being punched.  Although
13058e205f77SHugh Dickins 	 * shmem_undo_range() does remove the additions, it may be unable to
13068e205f77SHugh Dickins 	 * keep up, as each new page needs its own unmap_mapping_range() call,
13078e205f77SHugh Dickins 	 * and the i_mmap tree grows ever slower to scan if new vmas are added.
13088e205f77SHugh Dickins 	 *
13098e205f77SHugh Dickins 	 * It does not matter if we sometimes reach this check just before the
13108e205f77SHugh Dickins 	 * hole-punch begins, so that one fault then races with the punch:
13118e205f77SHugh Dickins 	 * we just need to make racing faults a rare case.
13128e205f77SHugh Dickins 	 *
13138e205f77SHugh Dickins 	 * The implementation below would be much simpler if we just used a
13148e205f77SHugh Dickins 	 * standard mutex or completion: but we cannot take i_mutex in fault,
13158e205f77SHugh Dickins 	 * and bloating every shmem inode for this unlikely case would be sad.
1316f00cdc6dSHugh Dickins 	 */
1317f00cdc6dSHugh Dickins 	if (unlikely(inode->i_private)) {
1318f00cdc6dSHugh Dickins 		struct shmem_falloc *shmem_falloc;
1319f00cdc6dSHugh Dickins 
1320f00cdc6dSHugh Dickins 		spin_lock(&inode->i_lock);
1321f00cdc6dSHugh Dickins 		shmem_falloc = inode->i_private;
13228e205f77SHugh Dickins 		if (shmem_falloc &&
13238e205f77SHugh Dickins 		    shmem_falloc->waitq &&
13248e205f77SHugh Dickins 		    vmf->pgoff >= shmem_falloc->start &&
13258e205f77SHugh Dickins 		    vmf->pgoff < shmem_falloc->next) {
13268e205f77SHugh Dickins 			wait_queue_head_t *shmem_falloc_waitq;
13278e205f77SHugh Dickins 			DEFINE_WAIT(shmem_fault_wait);
13288e205f77SHugh Dickins 
13298e205f77SHugh Dickins 			ret = VM_FAULT_NOPAGE;
1330f00cdc6dSHugh Dickins 			if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1331f00cdc6dSHugh Dickins 			   !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
13328e205f77SHugh Dickins 				/* It's polite to up mmap_sem if we can */
1333f00cdc6dSHugh Dickins 				up_read(&vma->vm_mm->mmap_sem);
13348e205f77SHugh Dickins 				ret = VM_FAULT_RETRY;
1335f00cdc6dSHugh Dickins 			}
13368e205f77SHugh Dickins 
13378e205f77SHugh Dickins 			shmem_falloc_waitq = shmem_falloc->waitq;
13388e205f77SHugh Dickins 			prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
13398e205f77SHugh Dickins 					TASK_UNINTERRUPTIBLE);
13408e205f77SHugh Dickins 			spin_unlock(&inode->i_lock);
13418e205f77SHugh Dickins 			schedule();
13428e205f77SHugh Dickins 
13438e205f77SHugh Dickins 			/*
13448e205f77SHugh Dickins 			 * shmem_falloc_waitq points into the shmem_fallocate()
13458e205f77SHugh Dickins 			 * stack of the hole-punching task: shmem_falloc_waitq
13468e205f77SHugh Dickins 			 * is usually invalid by the time we reach here, but
13478e205f77SHugh Dickins 			 * finish_wait() does not dereference it in that case;
13488e205f77SHugh Dickins 			 * though i_lock needed lest racing with wake_up_all().
13498e205f77SHugh Dickins 			 */
13508e205f77SHugh Dickins 			spin_lock(&inode->i_lock);
13518e205f77SHugh Dickins 			finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
13528e205f77SHugh Dickins 			spin_unlock(&inode->i_lock);
13538e205f77SHugh Dickins 			return ret;
1354f00cdc6dSHugh Dickins 		}
13558e205f77SHugh Dickins 		spin_unlock(&inode->i_lock);
1356f00cdc6dSHugh Dickins 	}
1357f00cdc6dSHugh Dickins 
13581da177e4SLinus Torvalds 	error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
13591da177e4SLinus Torvalds 	if (error)
13601da177e4SLinus Torvalds 		return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
136168da9f05SHugh Dickins 
1362456f998eSYing Han 	if (ret & VM_FAULT_MAJOR) {
1363456f998eSYing Han 		count_vm_event(PGMAJFAULT);
1364456f998eSYing Han 		mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1365456f998eSYing Han 	}
136668da9f05SHugh Dickins 	return ret;
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds #ifdef CONFIG_NUMA
137041ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
13711da177e4SLinus Torvalds {
1372496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
137341ffe5d5SHugh Dickins 	return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
13741da177e4SLinus Torvalds }
13751da177e4SLinus Torvalds 
1376d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1377d8dc74f2SAdrian Bunk 					  unsigned long addr)
13781da177e4SLinus Torvalds {
1379496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
138041ffe5d5SHugh Dickins 	pgoff_t index;
13811da177e4SLinus Torvalds 
138241ffe5d5SHugh Dickins 	index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
138341ffe5d5SHugh Dickins 	return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
13841da177e4SLinus Torvalds }
13851da177e4SLinus Torvalds #endif
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user)
13881da177e4SLinus Torvalds {
1389496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
13901da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
13911da177e4SLinus Torvalds 	int retval = -ENOMEM;
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	spin_lock(&info->lock);
13941da177e4SLinus Torvalds 	if (lock && !(info->flags & VM_LOCKED)) {
13951da177e4SLinus Torvalds 		if (!user_shm_lock(inode->i_size, user))
13961da177e4SLinus Torvalds 			goto out_nomem;
13971da177e4SLinus Torvalds 		info->flags |= VM_LOCKED;
139889e004eaSLee Schermerhorn 		mapping_set_unevictable(file->f_mapping);
13991da177e4SLinus Torvalds 	}
14001da177e4SLinus Torvalds 	if (!lock && (info->flags & VM_LOCKED) && user) {
14011da177e4SLinus Torvalds 		user_shm_unlock(inode->i_size, user);
14021da177e4SLinus Torvalds 		info->flags &= ~VM_LOCKED;
140389e004eaSLee Schermerhorn 		mapping_clear_unevictable(file->f_mapping);
14041da177e4SLinus Torvalds 	}
14051da177e4SLinus Torvalds 	retval = 0;
140689e004eaSLee Schermerhorn 
14071da177e4SLinus Torvalds out_nomem:
14081da177e4SLinus Torvalds 	spin_unlock(&info->lock);
14091da177e4SLinus Torvalds 	return retval;
14101da177e4SLinus Torvalds }
14111da177e4SLinus Torvalds 
14129b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
14131da177e4SLinus Torvalds {
14141da177e4SLinus Torvalds 	file_accessed(file);
14151da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
14161da177e4SLinus Torvalds 	return 0;
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
1419454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
142009208d15SAl Viro 				     umode_t mode, dev_t dev, unsigned long flags)
14211da177e4SLinus Torvalds {
14221da177e4SLinus Torvalds 	struct inode *inode;
14231da177e4SLinus Torvalds 	struct shmem_inode_info *info;
14241da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
14251da177e4SLinus Torvalds 
14265b04c689SPavel Emelyanov 	if (shmem_reserve_inode(sb))
14271da177e4SLinus Torvalds 		return NULL;
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 	inode = new_inode(sb);
14301da177e4SLinus Torvalds 	if (inode) {
143185fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
1432454abafeSDmitry Monakhov 		inode_init_owner(inode, dir, mode);
14331da177e4SLinus Torvalds 		inode->i_blocks = 0;
14341da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
143591828a40SDavid M. Grimes 		inode->i_generation = get_seconds();
14361da177e4SLinus Torvalds 		info = SHMEM_I(inode);
14371da177e4SLinus Torvalds 		memset(info, 0, (char *)inode - (char *)info);
14381da177e4SLinus Torvalds 		spin_lock_init(&info->lock);
143940e041a2SDavid Herrmann 		info->seals = F_SEAL_SEAL;
14400b0a0806SHugh Dickins 		info->flags = flags & VM_NORESERVE;
14411da177e4SLinus Torvalds 		INIT_LIST_HEAD(&info->swaplist);
144238f38657SAristeu Rozanski 		simple_xattrs_init(&info->xattrs);
144372c04902SAl Viro 		cache_no_acl(inode);
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
14461da177e4SLinus Torvalds 		default:
144739f0247dSAndreas Gruenbacher 			inode->i_op = &shmem_special_inode_operations;
14481da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
14491da177e4SLinus Torvalds 			break;
14501da177e4SLinus Torvalds 		case S_IFREG:
145114fcc23fSHugh Dickins 			inode->i_mapping->a_ops = &shmem_aops;
14521da177e4SLinus Torvalds 			inode->i_op = &shmem_inode_operations;
14531da177e4SLinus Torvalds 			inode->i_fop = &shmem_file_operations;
145471fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy,
145571fe804bSLee Schermerhorn 						 shmem_get_sbmpol(sbinfo));
14561da177e4SLinus Torvalds 			break;
14571da177e4SLinus Torvalds 		case S_IFDIR:
1458d8c76e6fSDave Hansen 			inc_nlink(inode);
14591da177e4SLinus Torvalds 			/* Some things misbehave if size == 0 on a directory */
14601da177e4SLinus Torvalds 			inode->i_size = 2 * BOGO_DIRENT_SIZE;
14611da177e4SLinus Torvalds 			inode->i_op = &shmem_dir_inode_operations;
14621da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
14631da177e4SLinus Torvalds 			break;
14641da177e4SLinus Torvalds 		case S_IFLNK:
14651da177e4SLinus Torvalds 			/*
14661da177e4SLinus Torvalds 			 * Must not load anything in the rbtree,
14671da177e4SLinus Torvalds 			 * mpol_free_shared_policy will not be called.
14681da177e4SLinus Torvalds 			 */
146971fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy, NULL);
14701da177e4SLinus Torvalds 			break;
14711da177e4SLinus Torvalds 		}
14725b04c689SPavel Emelyanov 	} else
14735b04c689SPavel Emelyanov 		shmem_free_inode(sb);
14741da177e4SLinus Torvalds 	return inode;
14751da177e4SLinus Torvalds }
14761da177e4SLinus Torvalds 
14770cd6144aSJohannes Weiner bool shmem_mapping(struct address_space *mapping)
14780cd6144aSJohannes Weiner {
1479f0774d88SSasha Levin 	if (!mapping->host)
1480f0774d88SSasha Levin 		return false;
1481f0774d88SSasha Levin 
148297b713baSChristoph Hellwig 	return mapping->host->i_sb->s_op == &shmem_ops;
14830cd6144aSJohannes Weiner }
14840cd6144aSJohannes Weiner 
14851da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
148692e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations;
148769f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations;
14881da177e4SLinus Torvalds 
14896d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR
14906d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
14916d9d88d0SJarkko Sakkinen #else
14926d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL
14936d9d88d0SJarkko Sakkinen #endif
14946d9d88d0SJarkko Sakkinen 
14951da177e4SLinus Torvalds static int
1496800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping,
1497800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
1498800d15a5SNick Piggin 			struct page **pagep, void **fsdata)
14991da177e4SLinus Torvalds {
1500800d15a5SNick Piggin 	struct inode *inode = mapping->host;
150140e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
1502800d15a5SNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
150340e041a2SDavid Herrmann 
150440e041a2SDavid Herrmann 	/* i_mutex is held by caller */
150540e041a2SDavid Herrmann 	if (unlikely(info->seals)) {
150640e041a2SDavid Herrmann 		if (info->seals & F_SEAL_WRITE)
150740e041a2SDavid Herrmann 			return -EPERM;
150840e041a2SDavid Herrmann 		if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
150940e041a2SDavid Herrmann 			return -EPERM;
151040e041a2SDavid Herrmann 	}
151140e041a2SDavid Herrmann 
151266d2f4d2SHugh Dickins 	return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1513800d15a5SNick Piggin }
1514800d15a5SNick Piggin 
1515800d15a5SNick Piggin static int
1516800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping,
1517800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
1518800d15a5SNick Piggin 			struct page *page, void *fsdata)
1519800d15a5SNick Piggin {
1520800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1521800d15a5SNick Piggin 
1522800d15a5SNick Piggin 	if (pos + copied > inode->i_size)
1523800d15a5SNick Piggin 		i_size_write(inode, pos + copied);
1524800d15a5SNick Piggin 
1525ec9516fbSHugh Dickins 	if (!PageUptodate(page)) {
1526ec9516fbSHugh Dickins 		if (copied < PAGE_CACHE_SIZE) {
1527ec9516fbSHugh Dickins 			unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1528ec9516fbSHugh Dickins 			zero_user_segments(page, 0, from,
1529ec9516fbSHugh Dickins 					from + copied, PAGE_CACHE_SIZE);
1530ec9516fbSHugh Dickins 		}
1531ec9516fbSHugh Dickins 		SetPageUptodate(page);
1532ec9516fbSHugh Dickins 	}
1533d3602444SHugh Dickins 	set_page_dirty(page);
15346746aff7SWu Fengguang 	unlock_page(page);
1535d3602444SHugh Dickins 	page_cache_release(page);
1536d3602444SHugh Dickins 
1537800d15a5SNick Piggin 	return copied;
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds 
15402ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
15411da177e4SLinus Torvalds {
15426e58e79dSAl Viro 	struct file *file = iocb->ki_filp;
15436e58e79dSAl Viro 	struct inode *inode = file_inode(file);
15441da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
154541ffe5d5SHugh Dickins 	pgoff_t index;
154641ffe5d5SHugh Dickins 	unsigned long offset;
1547a0ee5ec5SHugh Dickins 	enum sgp_type sgp = SGP_READ;
1548f7c1d074SGeert Uytterhoeven 	int error = 0;
1549cb66a7a1SAl Viro 	ssize_t retval = 0;
15506e58e79dSAl Viro 	loff_t *ppos = &iocb->ki_pos;
1551a0ee5ec5SHugh Dickins 
1552a0ee5ec5SHugh Dickins 	/*
1553a0ee5ec5SHugh Dickins 	 * Might this read be for a stacking filesystem?  Then when reading
1554a0ee5ec5SHugh Dickins 	 * holes of a sparse file, we actually need to allocate those pages,
1555a0ee5ec5SHugh Dickins 	 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1556a0ee5ec5SHugh Dickins 	 */
1557777eda2cSAl Viro 	if (!iter_is_iovec(to))
1558a0ee5ec5SHugh Dickins 		sgp = SGP_DIRTY;
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	index = *ppos >> PAGE_CACHE_SHIFT;
15611da177e4SLinus Torvalds 	offset = *ppos & ~PAGE_CACHE_MASK;
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 	for (;;) {
15641da177e4SLinus Torvalds 		struct page *page = NULL;
156541ffe5d5SHugh Dickins 		pgoff_t end_index;
156641ffe5d5SHugh Dickins 		unsigned long nr, ret;
15671da177e4SLinus Torvalds 		loff_t i_size = i_size_read(inode);
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
15701da177e4SLinus Torvalds 		if (index > end_index)
15711da177e4SLinus Torvalds 			break;
15721da177e4SLinus Torvalds 		if (index == end_index) {
15731da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
15741da177e4SLinus Torvalds 			if (nr <= offset)
15751da177e4SLinus Torvalds 				break;
15761da177e4SLinus Torvalds 		}
15771da177e4SLinus Torvalds 
15786e58e79dSAl Viro 		error = shmem_getpage(inode, index, &page, sgp, NULL);
15796e58e79dSAl Viro 		if (error) {
15806e58e79dSAl Viro 			if (error == -EINVAL)
15816e58e79dSAl Viro 				error = 0;
15821da177e4SLinus Torvalds 			break;
15831da177e4SLinus Torvalds 		}
1584d3602444SHugh Dickins 		if (page)
1585d3602444SHugh Dickins 			unlock_page(page);
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 		/*
15881da177e4SLinus Torvalds 		 * We must evaluate after, since reads (unlike writes)
15891b1dcc1bSJes Sorensen 		 * are called without i_mutex protection against truncate
15901da177e4SLinus Torvalds 		 */
15911da177e4SLinus Torvalds 		nr = PAGE_CACHE_SIZE;
15921da177e4SLinus Torvalds 		i_size = i_size_read(inode);
15931da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
15941da177e4SLinus Torvalds 		if (index == end_index) {
15951da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
15961da177e4SLinus Torvalds 			if (nr <= offset) {
15971da177e4SLinus Torvalds 				if (page)
15981da177e4SLinus Torvalds 					page_cache_release(page);
15991da177e4SLinus Torvalds 				break;
16001da177e4SLinus Torvalds 			}
16011da177e4SLinus Torvalds 		}
16021da177e4SLinus Torvalds 		nr -= offset;
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 		if (page) {
16051da177e4SLinus Torvalds 			/*
16061da177e4SLinus Torvalds 			 * If users can be writing to this page using arbitrary
16071da177e4SLinus Torvalds 			 * virtual addresses, take care about potential aliasing
16081da177e4SLinus Torvalds 			 * before reading the page on the kernel side.
16091da177e4SLinus Torvalds 			 */
16101da177e4SLinus Torvalds 			if (mapping_writably_mapped(mapping))
16111da177e4SLinus Torvalds 				flush_dcache_page(page);
16121da177e4SLinus Torvalds 			/*
16131da177e4SLinus Torvalds 			 * Mark the page accessed if we read the beginning.
16141da177e4SLinus Torvalds 			 */
16151da177e4SLinus Torvalds 			if (!offset)
16161da177e4SLinus Torvalds 				mark_page_accessed(page);
1617b5810039SNick Piggin 		} else {
16181da177e4SLinus Torvalds 			page = ZERO_PAGE(0);
1619b5810039SNick Piggin 			page_cache_get(page);
1620b5810039SNick Piggin 		}
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 		/*
16231da177e4SLinus Torvalds 		 * Ok, we have the page, and it's up-to-date, so
16241da177e4SLinus Torvalds 		 * now we can copy it to user space...
16251da177e4SLinus Torvalds 		 */
16262ba5bbedSAl Viro 		ret = copy_page_to_iter(page, offset, nr, to);
16276e58e79dSAl Viro 		retval += ret;
16281da177e4SLinus Torvalds 		offset += ret;
16291da177e4SLinus Torvalds 		index += offset >> PAGE_CACHE_SHIFT;
16301da177e4SLinus Torvalds 		offset &= ~PAGE_CACHE_MASK;
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 		page_cache_release(page);
16332ba5bbedSAl Viro 		if (!iov_iter_count(to))
16341da177e4SLinus Torvalds 			break;
16356e58e79dSAl Viro 		if (ret < nr) {
16366e58e79dSAl Viro 			error = -EFAULT;
16376e58e79dSAl Viro 			break;
16386e58e79dSAl Viro 		}
16391da177e4SLinus Torvalds 		cond_resched();
16401da177e4SLinus Torvalds 	}
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds 	*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
16436e58e79dSAl Viro 	file_accessed(file);
16446e58e79dSAl Viro 	return retval ? retval : error;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
1647708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1648708e3508SHugh Dickins 				struct pipe_inode_info *pipe, size_t len,
1649708e3508SHugh Dickins 				unsigned int flags)
1650708e3508SHugh Dickins {
1651708e3508SHugh Dickins 	struct address_space *mapping = in->f_mapping;
165271f0e07aSHugh Dickins 	struct inode *inode = mapping->host;
1653708e3508SHugh Dickins 	unsigned int loff, nr_pages, req_pages;
1654708e3508SHugh Dickins 	struct page *pages[PIPE_DEF_BUFFERS];
1655708e3508SHugh Dickins 	struct partial_page partial[PIPE_DEF_BUFFERS];
1656708e3508SHugh Dickins 	struct page *page;
1657708e3508SHugh Dickins 	pgoff_t index, end_index;
1658708e3508SHugh Dickins 	loff_t isize, left;
1659708e3508SHugh Dickins 	int error, page_nr;
1660708e3508SHugh Dickins 	struct splice_pipe_desc spd = {
1661708e3508SHugh Dickins 		.pages = pages,
1662708e3508SHugh Dickins 		.partial = partial,
1663047fe360SEric Dumazet 		.nr_pages_max = PIPE_DEF_BUFFERS,
1664708e3508SHugh Dickins 		.flags = flags,
1665708e3508SHugh Dickins 		.ops = &page_cache_pipe_buf_ops,
1666708e3508SHugh Dickins 		.spd_release = spd_release_page,
1667708e3508SHugh Dickins 	};
1668708e3508SHugh Dickins 
166971f0e07aSHugh Dickins 	isize = i_size_read(inode);
1670708e3508SHugh Dickins 	if (unlikely(*ppos >= isize))
1671708e3508SHugh Dickins 		return 0;
1672708e3508SHugh Dickins 
1673708e3508SHugh Dickins 	left = isize - *ppos;
1674708e3508SHugh Dickins 	if (unlikely(left < len))
1675708e3508SHugh Dickins 		len = left;
1676708e3508SHugh Dickins 
1677708e3508SHugh Dickins 	if (splice_grow_spd(pipe, &spd))
1678708e3508SHugh Dickins 		return -ENOMEM;
1679708e3508SHugh Dickins 
1680708e3508SHugh Dickins 	index = *ppos >> PAGE_CACHE_SHIFT;
1681708e3508SHugh Dickins 	loff = *ppos & ~PAGE_CACHE_MASK;
1682708e3508SHugh Dickins 	req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1683a786c06dSAl Viro 	nr_pages = min(req_pages, spd.nr_pages_max);
1684708e3508SHugh Dickins 
1685708e3508SHugh Dickins 	spd.nr_pages = find_get_pages_contig(mapping, index,
1686708e3508SHugh Dickins 						nr_pages, spd.pages);
1687708e3508SHugh Dickins 	index += spd.nr_pages;
1688708e3508SHugh Dickins 	error = 0;
168971f0e07aSHugh Dickins 
1690708e3508SHugh Dickins 	while (spd.nr_pages < nr_pages) {
169171f0e07aSHugh Dickins 		error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
169271f0e07aSHugh Dickins 		if (error)
1693708e3508SHugh Dickins 			break;
1694708e3508SHugh Dickins 		unlock_page(page);
1695708e3508SHugh Dickins 		spd.pages[spd.nr_pages++] = page;
1696708e3508SHugh Dickins 		index++;
1697708e3508SHugh Dickins 	}
1698708e3508SHugh Dickins 
1699708e3508SHugh Dickins 	index = *ppos >> PAGE_CACHE_SHIFT;
1700708e3508SHugh Dickins 	nr_pages = spd.nr_pages;
1701708e3508SHugh Dickins 	spd.nr_pages = 0;
170271f0e07aSHugh Dickins 
1703708e3508SHugh Dickins 	for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1704708e3508SHugh Dickins 		unsigned int this_len;
1705708e3508SHugh Dickins 
1706708e3508SHugh Dickins 		if (!len)
1707708e3508SHugh Dickins 			break;
1708708e3508SHugh Dickins 
1709708e3508SHugh Dickins 		this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1710708e3508SHugh Dickins 		page = spd.pages[page_nr];
1711708e3508SHugh Dickins 
171271f0e07aSHugh Dickins 		if (!PageUptodate(page) || page->mapping != mapping) {
171371f0e07aSHugh Dickins 			error = shmem_getpage(inode, index, &page,
171471f0e07aSHugh Dickins 							SGP_CACHE, NULL);
171571f0e07aSHugh Dickins 			if (error)
1716708e3508SHugh Dickins 				break;
171771f0e07aSHugh Dickins 			unlock_page(page);
1718708e3508SHugh Dickins 			page_cache_release(spd.pages[page_nr]);
1719708e3508SHugh Dickins 			spd.pages[page_nr] = page;
1720708e3508SHugh Dickins 		}
1721708e3508SHugh Dickins 
172271f0e07aSHugh Dickins 		isize = i_size_read(inode);
1723708e3508SHugh Dickins 		end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1724708e3508SHugh Dickins 		if (unlikely(!isize || index > end_index))
1725708e3508SHugh Dickins 			break;
1726708e3508SHugh Dickins 
1727708e3508SHugh Dickins 		if (end_index == index) {
1728708e3508SHugh Dickins 			unsigned int plen;
1729708e3508SHugh Dickins 
1730708e3508SHugh Dickins 			plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1731708e3508SHugh Dickins 			if (plen <= loff)
1732708e3508SHugh Dickins 				break;
1733708e3508SHugh Dickins 
1734708e3508SHugh Dickins 			this_len = min(this_len, plen - loff);
1735708e3508SHugh Dickins 			len = this_len;
1736708e3508SHugh Dickins 		}
1737708e3508SHugh Dickins 
1738708e3508SHugh Dickins 		spd.partial[page_nr].offset = loff;
1739708e3508SHugh Dickins 		spd.partial[page_nr].len = this_len;
1740708e3508SHugh Dickins 		len -= this_len;
1741708e3508SHugh Dickins 		loff = 0;
1742708e3508SHugh Dickins 		spd.nr_pages++;
1743708e3508SHugh Dickins 		index++;
1744708e3508SHugh Dickins 	}
1745708e3508SHugh Dickins 
1746708e3508SHugh Dickins 	while (page_nr < nr_pages)
1747708e3508SHugh Dickins 		page_cache_release(spd.pages[page_nr++]);
1748708e3508SHugh Dickins 
1749708e3508SHugh Dickins 	if (spd.nr_pages)
1750708e3508SHugh Dickins 		error = splice_to_pipe(pipe, &spd);
1751708e3508SHugh Dickins 
1752047fe360SEric Dumazet 	splice_shrink_spd(&spd);
1753708e3508SHugh Dickins 
1754708e3508SHugh Dickins 	if (error > 0) {
1755708e3508SHugh Dickins 		*ppos += error;
1756708e3508SHugh Dickins 		file_accessed(in);
1757708e3508SHugh Dickins 	}
1758708e3508SHugh Dickins 	return error;
1759708e3508SHugh Dickins }
1760708e3508SHugh Dickins 
1761220f2ac9SHugh Dickins /*
1762220f2ac9SHugh Dickins  * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
1763220f2ac9SHugh Dickins  */
1764220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
1765965c8e59SAndrew Morton 				    pgoff_t index, pgoff_t end, int whence)
1766220f2ac9SHugh Dickins {
1767220f2ac9SHugh Dickins 	struct page *page;
1768220f2ac9SHugh Dickins 	struct pagevec pvec;
1769220f2ac9SHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
1770220f2ac9SHugh Dickins 	bool done = false;
1771220f2ac9SHugh Dickins 	int i;
1772220f2ac9SHugh Dickins 
1773220f2ac9SHugh Dickins 	pagevec_init(&pvec, 0);
1774220f2ac9SHugh Dickins 	pvec.nr = 1;		/* start small: we may be there already */
1775220f2ac9SHugh Dickins 	while (!done) {
17760cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
1777220f2ac9SHugh Dickins 					pvec.nr, pvec.pages, indices);
1778220f2ac9SHugh Dickins 		if (!pvec.nr) {
1779965c8e59SAndrew Morton 			if (whence == SEEK_DATA)
1780220f2ac9SHugh Dickins 				index = end;
1781220f2ac9SHugh Dickins 			break;
1782220f2ac9SHugh Dickins 		}
1783220f2ac9SHugh Dickins 		for (i = 0; i < pvec.nr; i++, index++) {
1784220f2ac9SHugh Dickins 			if (index < indices[i]) {
1785965c8e59SAndrew Morton 				if (whence == SEEK_HOLE) {
1786220f2ac9SHugh Dickins 					done = true;
1787220f2ac9SHugh Dickins 					break;
1788220f2ac9SHugh Dickins 				}
1789220f2ac9SHugh Dickins 				index = indices[i];
1790220f2ac9SHugh Dickins 			}
1791220f2ac9SHugh Dickins 			page = pvec.pages[i];
1792220f2ac9SHugh Dickins 			if (page && !radix_tree_exceptional_entry(page)) {
1793220f2ac9SHugh Dickins 				if (!PageUptodate(page))
1794220f2ac9SHugh Dickins 					page = NULL;
1795220f2ac9SHugh Dickins 			}
1796220f2ac9SHugh Dickins 			if (index >= end ||
1797965c8e59SAndrew Morton 			    (page && whence == SEEK_DATA) ||
1798965c8e59SAndrew Morton 			    (!page && whence == SEEK_HOLE)) {
1799220f2ac9SHugh Dickins 				done = true;
1800220f2ac9SHugh Dickins 				break;
1801220f2ac9SHugh Dickins 			}
1802220f2ac9SHugh Dickins 		}
18030cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
1804220f2ac9SHugh Dickins 		pagevec_release(&pvec);
1805220f2ac9SHugh Dickins 		pvec.nr = PAGEVEC_SIZE;
1806220f2ac9SHugh Dickins 		cond_resched();
1807220f2ac9SHugh Dickins 	}
1808220f2ac9SHugh Dickins 	return index;
1809220f2ac9SHugh Dickins }
1810220f2ac9SHugh Dickins 
1811965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
1812220f2ac9SHugh Dickins {
1813220f2ac9SHugh Dickins 	struct address_space *mapping = file->f_mapping;
1814220f2ac9SHugh Dickins 	struct inode *inode = mapping->host;
1815220f2ac9SHugh Dickins 	pgoff_t start, end;
1816220f2ac9SHugh Dickins 	loff_t new_offset;
1817220f2ac9SHugh Dickins 
1818965c8e59SAndrew Morton 	if (whence != SEEK_DATA && whence != SEEK_HOLE)
1819965c8e59SAndrew Morton 		return generic_file_llseek_size(file, offset, whence,
1820220f2ac9SHugh Dickins 					MAX_LFS_FILESIZE, i_size_read(inode));
1821220f2ac9SHugh Dickins 	mutex_lock(&inode->i_mutex);
1822220f2ac9SHugh Dickins 	/* We're holding i_mutex so we can access i_size directly */
1823220f2ac9SHugh Dickins 
1824220f2ac9SHugh Dickins 	if (offset < 0)
1825220f2ac9SHugh Dickins 		offset = -EINVAL;
1826220f2ac9SHugh Dickins 	else if (offset >= inode->i_size)
1827220f2ac9SHugh Dickins 		offset = -ENXIO;
1828220f2ac9SHugh Dickins 	else {
1829220f2ac9SHugh Dickins 		start = offset >> PAGE_CACHE_SHIFT;
1830220f2ac9SHugh Dickins 		end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1831965c8e59SAndrew Morton 		new_offset = shmem_seek_hole_data(mapping, start, end, whence);
1832220f2ac9SHugh Dickins 		new_offset <<= PAGE_CACHE_SHIFT;
1833220f2ac9SHugh Dickins 		if (new_offset > offset) {
1834220f2ac9SHugh Dickins 			if (new_offset < inode->i_size)
1835220f2ac9SHugh Dickins 				offset = new_offset;
1836965c8e59SAndrew Morton 			else if (whence == SEEK_DATA)
1837220f2ac9SHugh Dickins 				offset = -ENXIO;
1838220f2ac9SHugh Dickins 			else
1839220f2ac9SHugh Dickins 				offset = inode->i_size;
1840220f2ac9SHugh Dickins 		}
1841220f2ac9SHugh Dickins 	}
1842220f2ac9SHugh Dickins 
1843387aae6fSHugh Dickins 	if (offset >= 0)
184446a1c2c7SJie Liu 		offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
1845220f2ac9SHugh Dickins 	mutex_unlock(&inode->i_mutex);
1846220f2ac9SHugh Dickins 	return offset;
1847220f2ac9SHugh Dickins }
1848220f2ac9SHugh Dickins 
184905f65b5cSDavid Herrmann /*
185005f65b5cSDavid Herrmann  * We need a tag: a new tag would expand every radix_tree_node by 8 bytes,
185105f65b5cSDavid Herrmann  * so reuse a tag which we firmly believe is never set or cleared on shmem.
185205f65b5cSDavid Herrmann  */
185305f65b5cSDavid Herrmann #define SHMEM_TAG_PINNED        PAGECACHE_TAG_TOWRITE
185405f65b5cSDavid Herrmann #define LAST_SCAN               4       /* about 150ms max */
185505f65b5cSDavid Herrmann 
185605f65b5cSDavid Herrmann static void shmem_tag_pins(struct address_space *mapping)
185705f65b5cSDavid Herrmann {
185805f65b5cSDavid Herrmann 	struct radix_tree_iter iter;
185905f65b5cSDavid Herrmann 	void **slot;
186005f65b5cSDavid Herrmann 	pgoff_t start;
186105f65b5cSDavid Herrmann 	struct page *page;
186205f65b5cSDavid Herrmann 
186305f65b5cSDavid Herrmann 	lru_add_drain();
186405f65b5cSDavid Herrmann 	start = 0;
186505f65b5cSDavid Herrmann 	rcu_read_lock();
186605f65b5cSDavid Herrmann 
186705f65b5cSDavid Herrmann restart:
186805f65b5cSDavid Herrmann 	radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
186905f65b5cSDavid Herrmann 		page = radix_tree_deref_slot(slot);
187005f65b5cSDavid Herrmann 		if (!page || radix_tree_exception(page)) {
187105f65b5cSDavid Herrmann 			if (radix_tree_deref_retry(page))
187205f65b5cSDavid Herrmann 				goto restart;
187305f65b5cSDavid Herrmann 		} else if (page_count(page) - page_mapcount(page) > 1) {
187405f65b5cSDavid Herrmann 			spin_lock_irq(&mapping->tree_lock);
187505f65b5cSDavid Herrmann 			radix_tree_tag_set(&mapping->page_tree, iter.index,
187605f65b5cSDavid Herrmann 					   SHMEM_TAG_PINNED);
187705f65b5cSDavid Herrmann 			spin_unlock_irq(&mapping->tree_lock);
187805f65b5cSDavid Herrmann 		}
187905f65b5cSDavid Herrmann 
188005f65b5cSDavid Herrmann 		if (need_resched()) {
188105f65b5cSDavid Herrmann 			cond_resched_rcu();
188205f65b5cSDavid Herrmann 			start = iter.index + 1;
188305f65b5cSDavid Herrmann 			goto restart;
188405f65b5cSDavid Herrmann 		}
188505f65b5cSDavid Herrmann 	}
188605f65b5cSDavid Herrmann 	rcu_read_unlock();
188705f65b5cSDavid Herrmann }
188805f65b5cSDavid Herrmann 
188905f65b5cSDavid Herrmann /*
189005f65b5cSDavid Herrmann  * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
189105f65b5cSDavid Herrmann  * via get_user_pages(), drivers might have some pending I/O without any active
189205f65b5cSDavid Herrmann  * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
189305f65b5cSDavid Herrmann  * and see whether it has an elevated ref-count. If so, we tag them and wait for
189405f65b5cSDavid Herrmann  * them to be dropped.
189505f65b5cSDavid Herrmann  * The caller must guarantee that no new user will acquire writable references
189605f65b5cSDavid Herrmann  * to those pages to avoid races.
189705f65b5cSDavid Herrmann  */
189840e041a2SDavid Herrmann static int shmem_wait_for_pins(struct address_space *mapping)
189940e041a2SDavid Herrmann {
190005f65b5cSDavid Herrmann 	struct radix_tree_iter iter;
190105f65b5cSDavid Herrmann 	void **slot;
190205f65b5cSDavid Herrmann 	pgoff_t start;
190305f65b5cSDavid Herrmann 	struct page *page;
190405f65b5cSDavid Herrmann 	int error, scan;
190505f65b5cSDavid Herrmann 
190605f65b5cSDavid Herrmann 	shmem_tag_pins(mapping);
190705f65b5cSDavid Herrmann 
190805f65b5cSDavid Herrmann 	error = 0;
190905f65b5cSDavid Herrmann 	for (scan = 0; scan <= LAST_SCAN; scan++) {
191005f65b5cSDavid Herrmann 		if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED))
191105f65b5cSDavid Herrmann 			break;
191205f65b5cSDavid Herrmann 
191305f65b5cSDavid Herrmann 		if (!scan)
191405f65b5cSDavid Herrmann 			lru_add_drain_all();
191505f65b5cSDavid Herrmann 		else if (schedule_timeout_killable((HZ << scan) / 200))
191605f65b5cSDavid Herrmann 			scan = LAST_SCAN;
191705f65b5cSDavid Herrmann 
191805f65b5cSDavid Herrmann 		start = 0;
191905f65b5cSDavid Herrmann 		rcu_read_lock();
192005f65b5cSDavid Herrmann restart:
192105f65b5cSDavid Herrmann 		radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter,
192205f65b5cSDavid Herrmann 					   start, SHMEM_TAG_PINNED) {
192305f65b5cSDavid Herrmann 
192405f65b5cSDavid Herrmann 			page = radix_tree_deref_slot(slot);
192505f65b5cSDavid Herrmann 			if (radix_tree_exception(page)) {
192605f65b5cSDavid Herrmann 				if (radix_tree_deref_retry(page))
192705f65b5cSDavid Herrmann 					goto restart;
192805f65b5cSDavid Herrmann 
192905f65b5cSDavid Herrmann 				page = NULL;
193005f65b5cSDavid Herrmann 			}
193105f65b5cSDavid Herrmann 
193205f65b5cSDavid Herrmann 			if (page &&
193305f65b5cSDavid Herrmann 			    page_count(page) - page_mapcount(page) != 1) {
193405f65b5cSDavid Herrmann 				if (scan < LAST_SCAN)
193505f65b5cSDavid Herrmann 					goto continue_resched;
193605f65b5cSDavid Herrmann 
193705f65b5cSDavid Herrmann 				/*
193805f65b5cSDavid Herrmann 				 * On the last scan, we clean up all those tags
193905f65b5cSDavid Herrmann 				 * we inserted; but make a note that we still
194005f65b5cSDavid Herrmann 				 * found pages pinned.
194105f65b5cSDavid Herrmann 				 */
194205f65b5cSDavid Herrmann 				error = -EBUSY;
194305f65b5cSDavid Herrmann 			}
194405f65b5cSDavid Herrmann 
194505f65b5cSDavid Herrmann 			spin_lock_irq(&mapping->tree_lock);
194605f65b5cSDavid Herrmann 			radix_tree_tag_clear(&mapping->page_tree,
194705f65b5cSDavid Herrmann 					     iter.index, SHMEM_TAG_PINNED);
194805f65b5cSDavid Herrmann 			spin_unlock_irq(&mapping->tree_lock);
194905f65b5cSDavid Herrmann continue_resched:
195005f65b5cSDavid Herrmann 			if (need_resched()) {
195105f65b5cSDavid Herrmann 				cond_resched_rcu();
195205f65b5cSDavid Herrmann 				start = iter.index + 1;
195305f65b5cSDavid Herrmann 				goto restart;
195405f65b5cSDavid Herrmann 			}
195505f65b5cSDavid Herrmann 		}
195605f65b5cSDavid Herrmann 		rcu_read_unlock();
195705f65b5cSDavid Herrmann 	}
195805f65b5cSDavid Herrmann 
195905f65b5cSDavid Herrmann 	return error;
196040e041a2SDavid Herrmann }
196140e041a2SDavid Herrmann 
196240e041a2SDavid Herrmann #define F_ALL_SEALS (F_SEAL_SEAL | \
196340e041a2SDavid Herrmann 		     F_SEAL_SHRINK | \
196440e041a2SDavid Herrmann 		     F_SEAL_GROW | \
196540e041a2SDavid Herrmann 		     F_SEAL_WRITE)
196640e041a2SDavid Herrmann 
196740e041a2SDavid Herrmann int shmem_add_seals(struct file *file, unsigned int seals)
196840e041a2SDavid Herrmann {
196940e041a2SDavid Herrmann 	struct inode *inode = file_inode(file);
197040e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
197140e041a2SDavid Herrmann 	int error;
197240e041a2SDavid Herrmann 
197340e041a2SDavid Herrmann 	/*
197440e041a2SDavid Herrmann 	 * SEALING
197540e041a2SDavid Herrmann 	 * Sealing allows multiple parties to share a shmem-file but restrict
197640e041a2SDavid Herrmann 	 * access to a specific subset of file operations. Seals can only be
197740e041a2SDavid Herrmann 	 * added, but never removed. This way, mutually untrusted parties can
197840e041a2SDavid Herrmann 	 * share common memory regions with a well-defined policy. A malicious
197940e041a2SDavid Herrmann 	 * peer can thus never perform unwanted operations on a shared object.
198040e041a2SDavid Herrmann 	 *
198140e041a2SDavid Herrmann 	 * Seals are only supported on special shmem-files and always affect
198240e041a2SDavid Herrmann 	 * the whole underlying inode. Once a seal is set, it may prevent some
198340e041a2SDavid Herrmann 	 * kinds of access to the file. Currently, the following seals are
198440e041a2SDavid Herrmann 	 * defined:
198540e041a2SDavid Herrmann 	 *   SEAL_SEAL: Prevent further seals from being set on this file
198640e041a2SDavid Herrmann 	 *   SEAL_SHRINK: Prevent the file from shrinking
198740e041a2SDavid Herrmann 	 *   SEAL_GROW: Prevent the file from growing
198840e041a2SDavid Herrmann 	 *   SEAL_WRITE: Prevent write access to the file
198940e041a2SDavid Herrmann 	 *
199040e041a2SDavid Herrmann 	 * As we don't require any trust relationship between two parties, we
199140e041a2SDavid Herrmann 	 * must prevent seals from being removed. Therefore, sealing a file
199240e041a2SDavid Herrmann 	 * only adds a given set of seals to the file, it never touches
199340e041a2SDavid Herrmann 	 * existing seals. Furthermore, the "setting seals"-operation can be
199440e041a2SDavid Herrmann 	 * sealed itself, which basically prevents any further seal from being
199540e041a2SDavid Herrmann 	 * added.
199640e041a2SDavid Herrmann 	 *
199740e041a2SDavid Herrmann 	 * Semantics of sealing are only defined on volatile files. Only
199840e041a2SDavid Herrmann 	 * anonymous shmem files support sealing. More importantly, seals are
199940e041a2SDavid Herrmann 	 * never written to disk. Therefore, there's no plan to support it on
200040e041a2SDavid Herrmann 	 * other file types.
200140e041a2SDavid Herrmann 	 */
200240e041a2SDavid Herrmann 
200340e041a2SDavid Herrmann 	if (file->f_op != &shmem_file_operations)
200440e041a2SDavid Herrmann 		return -EINVAL;
200540e041a2SDavid Herrmann 	if (!(file->f_mode & FMODE_WRITE))
200640e041a2SDavid Herrmann 		return -EPERM;
200740e041a2SDavid Herrmann 	if (seals & ~(unsigned int)F_ALL_SEALS)
200840e041a2SDavid Herrmann 		return -EINVAL;
200940e041a2SDavid Herrmann 
201040e041a2SDavid Herrmann 	mutex_lock(&inode->i_mutex);
201140e041a2SDavid Herrmann 
201240e041a2SDavid Herrmann 	if (info->seals & F_SEAL_SEAL) {
201340e041a2SDavid Herrmann 		error = -EPERM;
201440e041a2SDavid Herrmann 		goto unlock;
201540e041a2SDavid Herrmann 	}
201640e041a2SDavid Herrmann 
201740e041a2SDavid Herrmann 	if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
201840e041a2SDavid Herrmann 		error = mapping_deny_writable(file->f_mapping);
201940e041a2SDavid Herrmann 		if (error)
202040e041a2SDavid Herrmann 			goto unlock;
202140e041a2SDavid Herrmann 
202240e041a2SDavid Herrmann 		error = shmem_wait_for_pins(file->f_mapping);
202340e041a2SDavid Herrmann 		if (error) {
202440e041a2SDavid Herrmann 			mapping_allow_writable(file->f_mapping);
202540e041a2SDavid Herrmann 			goto unlock;
202640e041a2SDavid Herrmann 		}
202740e041a2SDavid Herrmann 	}
202840e041a2SDavid Herrmann 
202940e041a2SDavid Herrmann 	info->seals |= seals;
203040e041a2SDavid Herrmann 	error = 0;
203140e041a2SDavid Herrmann 
203240e041a2SDavid Herrmann unlock:
203340e041a2SDavid Herrmann 	mutex_unlock(&inode->i_mutex);
203440e041a2SDavid Herrmann 	return error;
203540e041a2SDavid Herrmann }
203640e041a2SDavid Herrmann EXPORT_SYMBOL_GPL(shmem_add_seals);
203740e041a2SDavid Herrmann 
203840e041a2SDavid Herrmann int shmem_get_seals(struct file *file)
203940e041a2SDavid Herrmann {
204040e041a2SDavid Herrmann 	if (file->f_op != &shmem_file_operations)
204140e041a2SDavid Herrmann 		return -EINVAL;
204240e041a2SDavid Herrmann 
204340e041a2SDavid Herrmann 	return SHMEM_I(file_inode(file))->seals;
204440e041a2SDavid Herrmann }
204540e041a2SDavid Herrmann EXPORT_SYMBOL_GPL(shmem_get_seals);
204640e041a2SDavid Herrmann 
204740e041a2SDavid Herrmann long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
204840e041a2SDavid Herrmann {
204940e041a2SDavid Herrmann 	long error;
205040e041a2SDavid Herrmann 
205140e041a2SDavid Herrmann 	switch (cmd) {
205240e041a2SDavid Herrmann 	case F_ADD_SEALS:
205340e041a2SDavid Herrmann 		/* disallow upper 32bit */
205440e041a2SDavid Herrmann 		if (arg > UINT_MAX)
205540e041a2SDavid Herrmann 			return -EINVAL;
205640e041a2SDavid Herrmann 
205740e041a2SDavid Herrmann 		error = shmem_add_seals(file, arg);
205840e041a2SDavid Herrmann 		break;
205940e041a2SDavid Herrmann 	case F_GET_SEALS:
206040e041a2SDavid Herrmann 		error = shmem_get_seals(file);
206140e041a2SDavid Herrmann 		break;
206240e041a2SDavid Herrmann 	default:
206340e041a2SDavid Herrmann 		error = -EINVAL;
206440e041a2SDavid Herrmann 		break;
206540e041a2SDavid Herrmann 	}
206640e041a2SDavid Herrmann 
206740e041a2SDavid Herrmann 	return error;
206840e041a2SDavid Herrmann }
206940e041a2SDavid Herrmann 
207083e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset,
207183e4fa9cSHugh Dickins 							 loff_t len)
207283e4fa9cSHugh Dickins {
2073496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2074e2d12e22SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
207540e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
20761aac1400SHugh Dickins 	struct shmem_falloc shmem_falloc;
2077e2d12e22SHugh Dickins 	pgoff_t start, index, end;
2078e2d12e22SHugh Dickins 	int error;
207983e4fa9cSHugh Dickins 
208013ace4d0SHugh Dickins 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
208113ace4d0SHugh Dickins 		return -EOPNOTSUPP;
208213ace4d0SHugh Dickins 
208383e4fa9cSHugh Dickins 	mutex_lock(&inode->i_mutex);
208483e4fa9cSHugh Dickins 
208583e4fa9cSHugh Dickins 	if (mode & FALLOC_FL_PUNCH_HOLE) {
208683e4fa9cSHugh Dickins 		struct address_space *mapping = file->f_mapping;
208783e4fa9cSHugh Dickins 		loff_t unmap_start = round_up(offset, PAGE_SIZE);
208883e4fa9cSHugh Dickins 		loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
20898e205f77SHugh Dickins 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
209083e4fa9cSHugh Dickins 
209140e041a2SDavid Herrmann 		/* protected by i_mutex */
209240e041a2SDavid Herrmann 		if (info->seals & F_SEAL_WRITE) {
209340e041a2SDavid Herrmann 			error = -EPERM;
209440e041a2SDavid Herrmann 			goto out;
209540e041a2SDavid Herrmann 		}
209640e041a2SDavid Herrmann 
20978e205f77SHugh Dickins 		shmem_falloc.waitq = &shmem_falloc_waitq;
2098f00cdc6dSHugh Dickins 		shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2099f00cdc6dSHugh Dickins 		shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2100f00cdc6dSHugh Dickins 		spin_lock(&inode->i_lock);
2101f00cdc6dSHugh Dickins 		inode->i_private = &shmem_falloc;
2102f00cdc6dSHugh Dickins 		spin_unlock(&inode->i_lock);
2103f00cdc6dSHugh Dickins 
210483e4fa9cSHugh Dickins 		if ((u64)unmap_end > (u64)unmap_start)
210583e4fa9cSHugh Dickins 			unmap_mapping_range(mapping, unmap_start,
210683e4fa9cSHugh Dickins 					    1 + unmap_end - unmap_start, 0);
210783e4fa9cSHugh Dickins 		shmem_truncate_range(inode, offset, offset + len - 1);
210883e4fa9cSHugh Dickins 		/* No need to unmap again: hole-punching leaves COWed pages */
21098e205f77SHugh Dickins 
21108e205f77SHugh Dickins 		spin_lock(&inode->i_lock);
21118e205f77SHugh Dickins 		inode->i_private = NULL;
21128e205f77SHugh Dickins 		wake_up_all(&shmem_falloc_waitq);
21138e205f77SHugh Dickins 		spin_unlock(&inode->i_lock);
211483e4fa9cSHugh Dickins 		error = 0;
21158e205f77SHugh Dickins 		goto out;
211683e4fa9cSHugh Dickins 	}
211783e4fa9cSHugh Dickins 
2118e2d12e22SHugh Dickins 	/* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2119e2d12e22SHugh Dickins 	error = inode_newsize_ok(inode, offset + len);
2120e2d12e22SHugh Dickins 	if (error)
2121e2d12e22SHugh Dickins 		goto out;
2122e2d12e22SHugh Dickins 
212340e041a2SDavid Herrmann 	if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
212440e041a2SDavid Herrmann 		error = -EPERM;
212540e041a2SDavid Herrmann 		goto out;
212640e041a2SDavid Herrmann 	}
212740e041a2SDavid Herrmann 
2128e2d12e22SHugh Dickins 	start = offset >> PAGE_CACHE_SHIFT;
2129e2d12e22SHugh Dickins 	end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2130e2d12e22SHugh Dickins 	/* Try to avoid a swapstorm if len is impossible to satisfy */
2131e2d12e22SHugh Dickins 	if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2132e2d12e22SHugh Dickins 		error = -ENOSPC;
2133e2d12e22SHugh Dickins 		goto out;
2134e2d12e22SHugh Dickins 	}
2135e2d12e22SHugh Dickins 
21368e205f77SHugh Dickins 	shmem_falloc.waitq = NULL;
21371aac1400SHugh Dickins 	shmem_falloc.start = start;
21381aac1400SHugh Dickins 	shmem_falloc.next  = start;
21391aac1400SHugh Dickins 	shmem_falloc.nr_falloced = 0;
21401aac1400SHugh Dickins 	shmem_falloc.nr_unswapped = 0;
21411aac1400SHugh Dickins 	spin_lock(&inode->i_lock);
21421aac1400SHugh Dickins 	inode->i_private = &shmem_falloc;
21431aac1400SHugh Dickins 	spin_unlock(&inode->i_lock);
21441aac1400SHugh Dickins 
2145e2d12e22SHugh Dickins 	for (index = start; index < end; index++) {
2146e2d12e22SHugh Dickins 		struct page *page;
2147e2d12e22SHugh Dickins 
2148e2d12e22SHugh Dickins 		/*
2149e2d12e22SHugh Dickins 		 * Good, the fallocate(2) manpage permits EINTR: we may have
2150e2d12e22SHugh Dickins 		 * been interrupted because we are using up too much memory.
2151e2d12e22SHugh Dickins 		 */
2152e2d12e22SHugh Dickins 		if (signal_pending(current))
2153e2d12e22SHugh Dickins 			error = -EINTR;
21541aac1400SHugh Dickins 		else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
21551aac1400SHugh Dickins 			error = -ENOMEM;
2156e2d12e22SHugh Dickins 		else
21571635f6a7SHugh Dickins 			error = shmem_getpage(inode, index, &page, SGP_FALLOC,
2158e2d12e22SHugh Dickins 									NULL);
2159e2d12e22SHugh Dickins 		if (error) {
21601635f6a7SHugh Dickins 			/* Remove the !PageUptodate pages we added */
21611635f6a7SHugh Dickins 			shmem_undo_range(inode,
21621635f6a7SHugh Dickins 				(loff_t)start << PAGE_CACHE_SHIFT,
21631635f6a7SHugh Dickins 				(loff_t)index << PAGE_CACHE_SHIFT, true);
21641aac1400SHugh Dickins 			goto undone;
2165e2d12e22SHugh Dickins 		}
2166e2d12e22SHugh Dickins 
2167e2d12e22SHugh Dickins 		/*
21681aac1400SHugh Dickins 		 * Inform shmem_writepage() how far we have reached.
21691aac1400SHugh Dickins 		 * No need for lock or barrier: we have the page lock.
21701aac1400SHugh Dickins 		 */
21711aac1400SHugh Dickins 		shmem_falloc.next++;
21721aac1400SHugh Dickins 		if (!PageUptodate(page))
21731aac1400SHugh Dickins 			shmem_falloc.nr_falloced++;
21741aac1400SHugh Dickins 
21751aac1400SHugh Dickins 		/*
21761635f6a7SHugh Dickins 		 * If !PageUptodate, leave it that way so that freeable pages
21771635f6a7SHugh Dickins 		 * can be recognized if we need to rollback on error later.
21781635f6a7SHugh Dickins 		 * But set_page_dirty so that memory pressure will swap rather
2179e2d12e22SHugh Dickins 		 * than free the pages we are allocating (and SGP_CACHE pages
2180e2d12e22SHugh Dickins 		 * might still be clean: we now need to mark those dirty too).
2181e2d12e22SHugh Dickins 		 */
2182e2d12e22SHugh Dickins 		set_page_dirty(page);
2183e2d12e22SHugh Dickins 		unlock_page(page);
2184e2d12e22SHugh Dickins 		page_cache_release(page);
2185e2d12e22SHugh Dickins 		cond_resched();
2186e2d12e22SHugh Dickins 	}
2187e2d12e22SHugh Dickins 
2188e2d12e22SHugh Dickins 	if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2189e2d12e22SHugh Dickins 		i_size_write(inode, offset + len);
2190e2d12e22SHugh Dickins 	inode->i_ctime = CURRENT_TIME;
21911aac1400SHugh Dickins undone:
21921aac1400SHugh Dickins 	spin_lock(&inode->i_lock);
21931aac1400SHugh Dickins 	inode->i_private = NULL;
21941aac1400SHugh Dickins 	spin_unlock(&inode->i_lock);
2195e2d12e22SHugh Dickins out:
219683e4fa9cSHugh Dickins 	mutex_unlock(&inode->i_mutex);
219783e4fa9cSHugh Dickins 	return error;
219883e4fa9cSHugh Dickins }
219983e4fa9cSHugh Dickins 
2200726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
22011da177e4SLinus Torvalds {
2202726c3342SDavid Howells 	struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
22031da177e4SLinus Torvalds 
22041da177e4SLinus Torvalds 	buf->f_type = TMPFS_MAGIC;
22051da177e4SLinus Torvalds 	buf->f_bsize = PAGE_CACHE_SIZE;
22061da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
22070edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
22081da177e4SLinus Torvalds 		buf->f_blocks = sbinfo->max_blocks;
220941ffe5d5SHugh Dickins 		buf->f_bavail =
221041ffe5d5SHugh Dickins 		buf->f_bfree  = sbinfo->max_blocks -
221141ffe5d5SHugh Dickins 				percpu_counter_sum(&sbinfo->used_blocks);
22120edd73b3SHugh Dickins 	}
22130edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
22141da177e4SLinus Torvalds 		buf->f_files = sbinfo->max_inodes;
22151da177e4SLinus Torvalds 		buf->f_ffree = sbinfo->free_inodes;
22161da177e4SLinus Torvalds 	}
22171da177e4SLinus Torvalds 	/* else leave those fields 0 like simple_statfs */
22181da177e4SLinus Torvalds 	return 0;
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds /*
22221da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
22231da177e4SLinus Torvalds  */
22241da177e4SLinus Torvalds static int
22251a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
22261da177e4SLinus Torvalds {
22270b0a0806SHugh Dickins 	struct inode *inode;
22281da177e4SLinus Torvalds 	int error = -ENOSPC;
22291da177e4SLinus Torvalds 
2230454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
22311da177e4SLinus Torvalds 	if (inode) {
2232feda821eSChristoph Hellwig 		error = simple_acl_create(dir, inode);
2233feda821eSChristoph Hellwig 		if (error)
2234feda821eSChristoph Hellwig 			goto out_iput;
22352a7dba39SEric Paris 		error = security_inode_init_security(inode, dir,
22369d8f13baSMimi Zohar 						     &dentry->d_name,
22376d9d88d0SJarkko Sakkinen 						     shmem_initxattrs, NULL);
2238feda821eSChristoph Hellwig 		if (error && error != -EOPNOTSUPP)
2239feda821eSChristoph Hellwig 			goto out_iput;
224037ec43cdSMimi Zohar 
2241718deb6bSAl Viro 		error = 0;
22421da177e4SLinus Torvalds 		dir->i_size += BOGO_DIRENT_SIZE;
22431da177e4SLinus Torvalds 		dir->i_ctime = dir->i_mtime = CURRENT_TIME;
22441da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
22451da177e4SLinus Torvalds 		dget(dentry); /* Extra count - pin the dentry in core */
22461da177e4SLinus Torvalds 	}
22471da177e4SLinus Torvalds 	return error;
2248feda821eSChristoph Hellwig out_iput:
2249feda821eSChristoph Hellwig 	iput(inode);
2250feda821eSChristoph Hellwig 	return error;
22511da177e4SLinus Torvalds }
22521da177e4SLinus Torvalds 
225360545d0dSAl Viro static int
225460545d0dSAl Viro shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
225560545d0dSAl Viro {
225660545d0dSAl Viro 	struct inode *inode;
225760545d0dSAl Viro 	int error = -ENOSPC;
225860545d0dSAl Viro 
225960545d0dSAl Viro 	inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
226060545d0dSAl Viro 	if (inode) {
226160545d0dSAl Viro 		error = security_inode_init_security(inode, dir,
226260545d0dSAl Viro 						     NULL,
226360545d0dSAl Viro 						     shmem_initxattrs, NULL);
2264feda821eSChristoph Hellwig 		if (error && error != -EOPNOTSUPP)
2265feda821eSChristoph Hellwig 			goto out_iput;
2266feda821eSChristoph Hellwig 		error = simple_acl_create(dir, inode);
2267feda821eSChristoph Hellwig 		if (error)
2268feda821eSChristoph Hellwig 			goto out_iput;
226960545d0dSAl Viro 		d_tmpfile(dentry, inode);
227060545d0dSAl Viro 	}
227160545d0dSAl Viro 	return error;
2272feda821eSChristoph Hellwig out_iput:
2273feda821eSChristoph Hellwig 	iput(inode);
2274feda821eSChristoph Hellwig 	return error;
227560545d0dSAl Viro }
227660545d0dSAl Viro 
227718bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
22781da177e4SLinus Torvalds {
22791da177e4SLinus Torvalds 	int error;
22801da177e4SLinus Torvalds 
22811da177e4SLinus Torvalds 	if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
22821da177e4SLinus Torvalds 		return error;
2283d8c76e6fSDave Hansen 	inc_nlink(dir);
22841da177e4SLinus Torvalds 	return 0;
22851da177e4SLinus Torvalds }
22861da177e4SLinus Torvalds 
22874acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2288ebfc3b49SAl Viro 		bool excl)
22891da177e4SLinus Torvalds {
22901da177e4SLinus Torvalds 	return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
22911da177e4SLinus Torvalds }
22921da177e4SLinus Torvalds 
22931da177e4SLinus Torvalds /*
22941da177e4SLinus Torvalds  * Link a file..
22951da177e4SLinus Torvalds  */
22961da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
22971da177e4SLinus Torvalds {
229875c3cfa8SDavid Howells 	struct inode *inode = d_inode(old_dentry);
22995b04c689SPavel Emelyanov 	int ret;
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds 	/*
23021da177e4SLinus Torvalds 	 * No ordinary (disk based) filesystem counts links as inodes;
23031da177e4SLinus Torvalds 	 * but each new link needs a new dentry, pinning lowmem, and
23041da177e4SLinus Torvalds 	 * tmpfs dentries cannot be pruned until they are unlinked.
23051da177e4SLinus Torvalds 	 */
23065b04c689SPavel Emelyanov 	ret = shmem_reserve_inode(inode->i_sb);
23075b04c689SPavel Emelyanov 	if (ret)
23085b04c689SPavel Emelyanov 		goto out;
23091da177e4SLinus Torvalds 
23101da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
23111da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2312d8c76e6fSDave Hansen 	inc_nlink(inode);
23137de9c6eeSAl Viro 	ihold(inode);	/* New dentry reference */
23141da177e4SLinus Torvalds 	dget(dentry);		/* Extra pinning count for the created dentry */
23151da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
23165b04c689SPavel Emelyanov out:
23175b04c689SPavel Emelyanov 	return ret;
23181da177e4SLinus Torvalds }
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry)
23211da177e4SLinus Torvalds {
232275c3cfa8SDavid Howells 	struct inode *inode = d_inode(dentry);
23231da177e4SLinus Torvalds 
23245b04c689SPavel Emelyanov 	if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
23255b04c689SPavel Emelyanov 		shmem_free_inode(inode->i_sb);
23261da177e4SLinus Torvalds 
23271da177e4SLinus Torvalds 	dir->i_size -= BOGO_DIRENT_SIZE;
23281da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
23299a53c3a7SDave Hansen 	drop_nlink(inode);
23301da177e4SLinus Torvalds 	dput(dentry);	/* Undo the count from "create" - this does all the work */
23311da177e4SLinus Torvalds 	return 0;
23321da177e4SLinus Torvalds }
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
23351da177e4SLinus Torvalds {
23361da177e4SLinus Torvalds 	if (!simple_empty(dentry))
23371da177e4SLinus Torvalds 		return -ENOTEMPTY;
23381da177e4SLinus Torvalds 
233975c3cfa8SDavid Howells 	drop_nlink(d_inode(dentry));
23409a53c3a7SDave Hansen 	drop_nlink(dir);
23411da177e4SLinus Torvalds 	return shmem_unlink(dir, dentry);
23421da177e4SLinus Torvalds }
23431da177e4SLinus Torvalds 
234437456771SMiklos Szeredi static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
234537456771SMiklos Szeredi {
2346e36cb0b8SDavid Howells 	bool old_is_dir = d_is_dir(old_dentry);
2347e36cb0b8SDavid Howells 	bool new_is_dir = d_is_dir(new_dentry);
234837456771SMiklos Szeredi 
234937456771SMiklos Szeredi 	if (old_dir != new_dir && old_is_dir != new_is_dir) {
235037456771SMiklos Szeredi 		if (old_is_dir) {
235137456771SMiklos Szeredi 			drop_nlink(old_dir);
235237456771SMiklos Szeredi 			inc_nlink(new_dir);
235337456771SMiklos Szeredi 		} else {
235437456771SMiklos Szeredi 			drop_nlink(new_dir);
235537456771SMiklos Szeredi 			inc_nlink(old_dir);
235637456771SMiklos Szeredi 		}
235737456771SMiklos Szeredi 	}
235837456771SMiklos Szeredi 	old_dir->i_ctime = old_dir->i_mtime =
235937456771SMiklos Szeredi 	new_dir->i_ctime = new_dir->i_mtime =
236075c3cfa8SDavid Howells 	d_inode(old_dentry)->i_ctime =
236175c3cfa8SDavid Howells 	d_inode(new_dentry)->i_ctime = CURRENT_TIME;
236237456771SMiklos Szeredi 
236337456771SMiklos Szeredi 	return 0;
236437456771SMiklos Szeredi }
236537456771SMiklos Szeredi 
236646fdb794SMiklos Szeredi static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
236746fdb794SMiklos Szeredi {
236846fdb794SMiklos Szeredi 	struct dentry *whiteout;
236946fdb794SMiklos Szeredi 	int error;
237046fdb794SMiklos Szeredi 
237146fdb794SMiklos Szeredi 	whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
237246fdb794SMiklos Szeredi 	if (!whiteout)
237346fdb794SMiklos Szeredi 		return -ENOMEM;
237446fdb794SMiklos Szeredi 
237546fdb794SMiklos Szeredi 	error = shmem_mknod(old_dir, whiteout,
237646fdb794SMiklos Szeredi 			    S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
237746fdb794SMiklos Szeredi 	dput(whiteout);
237846fdb794SMiklos Szeredi 	if (error)
237946fdb794SMiklos Szeredi 		return error;
238046fdb794SMiklos Szeredi 
238146fdb794SMiklos Szeredi 	/*
238246fdb794SMiklos Szeredi 	 * Cheat and hash the whiteout while the old dentry is still in
238346fdb794SMiklos Szeredi 	 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
238446fdb794SMiklos Szeredi 	 *
238546fdb794SMiklos Szeredi 	 * d_lookup() will consistently find one of them at this point,
238646fdb794SMiklos Szeredi 	 * not sure which one, but that isn't even important.
238746fdb794SMiklos Szeredi 	 */
238846fdb794SMiklos Szeredi 	d_rehash(whiteout);
238946fdb794SMiklos Szeredi 	return 0;
239046fdb794SMiklos Szeredi }
239146fdb794SMiklos Szeredi 
23921da177e4SLinus Torvalds /*
23931da177e4SLinus Torvalds  * The VFS layer already does all the dentry stuff for rename,
23941da177e4SLinus Torvalds  * we just have to decrement the usage count for the target if
23951da177e4SLinus Torvalds  * it exists so that the VFS layer correctly free's it when it
23961da177e4SLinus Torvalds  * gets overwritten.
23971da177e4SLinus Torvalds  */
23983b69ff51SMiklos Szeredi static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags)
23991da177e4SLinus Torvalds {
240075c3cfa8SDavid Howells 	struct inode *inode = d_inode(old_dentry);
24011da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(inode->i_mode);
24021da177e4SLinus Torvalds 
240346fdb794SMiklos Szeredi 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
24043b69ff51SMiklos Szeredi 		return -EINVAL;
24053b69ff51SMiklos Szeredi 
240637456771SMiklos Szeredi 	if (flags & RENAME_EXCHANGE)
240737456771SMiklos Szeredi 		return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
240837456771SMiklos Szeredi 
24091da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
24101da177e4SLinus Torvalds 		return -ENOTEMPTY;
24111da177e4SLinus Torvalds 
241246fdb794SMiklos Szeredi 	if (flags & RENAME_WHITEOUT) {
241346fdb794SMiklos Szeredi 		int error;
241446fdb794SMiklos Szeredi 
241546fdb794SMiklos Szeredi 		error = shmem_whiteout(old_dir, old_dentry);
241646fdb794SMiklos Szeredi 		if (error)
241746fdb794SMiklos Szeredi 			return error;
241846fdb794SMiklos Szeredi 	}
241946fdb794SMiklos Szeredi 
242075c3cfa8SDavid Howells 	if (d_really_is_positive(new_dentry)) {
24211da177e4SLinus Torvalds 		(void) shmem_unlink(new_dir, new_dentry);
2422b928095bSMiklos Szeredi 		if (they_are_dirs) {
242375c3cfa8SDavid Howells 			drop_nlink(d_inode(new_dentry));
24249a53c3a7SDave Hansen 			drop_nlink(old_dir);
2425b928095bSMiklos Szeredi 		}
24261da177e4SLinus Torvalds 	} else if (they_are_dirs) {
24279a53c3a7SDave Hansen 		drop_nlink(old_dir);
2428d8c76e6fSDave Hansen 		inc_nlink(new_dir);
24291da177e4SLinus Torvalds 	}
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds 	old_dir->i_size -= BOGO_DIRENT_SIZE;
24321da177e4SLinus Torvalds 	new_dir->i_size += BOGO_DIRENT_SIZE;
24331da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime =
24341da177e4SLinus Torvalds 	new_dir->i_ctime = new_dir->i_mtime =
24351da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME;
24361da177e4SLinus Torvalds 	return 0;
24371da177e4SLinus Torvalds }
24381da177e4SLinus Torvalds 
24391da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
24401da177e4SLinus Torvalds {
24411da177e4SLinus Torvalds 	int error;
24421da177e4SLinus Torvalds 	int len;
24431da177e4SLinus Torvalds 	struct inode *inode;
24449276aad6SHugh Dickins 	struct page *page;
24451da177e4SLinus Torvalds 	char *kaddr;
24461da177e4SLinus Torvalds 	struct shmem_inode_info *info;
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds 	len = strlen(symname) + 1;
24491da177e4SLinus Torvalds 	if (len > PAGE_CACHE_SIZE)
24501da177e4SLinus Torvalds 		return -ENAMETOOLONG;
24511da177e4SLinus Torvalds 
2452454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
24531da177e4SLinus Torvalds 	if (!inode)
24541da177e4SLinus Torvalds 		return -ENOSPC;
24551da177e4SLinus Torvalds 
24569d8f13baSMimi Zohar 	error = security_inode_init_security(inode, dir, &dentry->d_name,
24576d9d88d0SJarkko Sakkinen 					     shmem_initxattrs, NULL);
2458570bc1c2SStephen Smalley 	if (error) {
2459570bc1c2SStephen Smalley 		if (error != -EOPNOTSUPP) {
2460570bc1c2SStephen Smalley 			iput(inode);
2461570bc1c2SStephen Smalley 			return error;
2462570bc1c2SStephen Smalley 		}
2463570bc1c2SStephen Smalley 		error = 0;
2464570bc1c2SStephen Smalley 	}
2465570bc1c2SStephen Smalley 
24661da177e4SLinus Torvalds 	info = SHMEM_I(inode);
24671da177e4SLinus Torvalds 	inode->i_size = len-1;
246869f07ec9SHugh Dickins 	if (len <= SHORT_SYMLINK_LEN) {
246969f07ec9SHugh Dickins 		info->symlink = kmemdup(symname, len, GFP_KERNEL);
247069f07ec9SHugh Dickins 		if (!info->symlink) {
247169f07ec9SHugh Dickins 			iput(inode);
247269f07ec9SHugh Dickins 			return -ENOMEM;
247369f07ec9SHugh Dickins 		}
247469f07ec9SHugh Dickins 		inode->i_op = &shmem_short_symlink_operations;
247560380f19SAl Viro 		inode->i_link = info->symlink;
24761da177e4SLinus Torvalds 	} else {
24771da177e4SLinus Torvalds 		error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
24781da177e4SLinus Torvalds 		if (error) {
24791da177e4SLinus Torvalds 			iput(inode);
24801da177e4SLinus Torvalds 			return error;
24811da177e4SLinus Torvalds 		}
248214fcc23fSHugh Dickins 		inode->i_mapping->a_ops = &shmem_aops;
24831da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inode_operations;
24849b04c5feSCong Wang 		kaddr = kmap_atomic(page);
24851da177e4SLinus Torvalds 		memcpy(kaddr, symname, len);
24869b04c5feSCong Wang 		kunmap_atomic(kaddr);
2487ec9516fbSHugh Dickins 		SetPageUptodate(page);
24881da177e4SLinus Torvalds 		set_page_dirty(page);
24896746aff7SWu Fengguang 		unlock_page(page);
24901da177e4SLinus Torvalds 		page_cache_release(page);
24911da177e4SLinus Torvalds 	}
24921da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
24931da177e4SLinus Torvalds 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
24941da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
24951da177e4SLinus Torvalds 	dget(dentry);
24961da177e4SLinus Torvalds 	return 0;
24971da177e4SLinus Torvalds }
24981da177e4SLinus Torvalds 
24996e77137bSAl Viro static const char *shmem_follow_link(struct dentry *dentry, void **cookie)
25001da177e4SLinus Torvalds {
25011da177e4SLinus Torvalds 	struct page *page = NULL;
250275c3cfa8SDavid Howells 	int error = shmem_getpage(d_inode(dentry), 0, &page, SGP_READ, NULL);
2503680baacbSAl Viro 	if (error)
2504680baacbSAl Viro 		return ERR_PTR(error);
2505d3602444SHugh Dickins 	unlock_page(page);
2506680baacbSAl Viro 	*cookie = page;
2507680baacbSAl Viro 	return kmap(page);
25081da177e4SLinus Torvalds }
25091da177e4SLinus Torvalds 
25105f2c4179SAl Viro static void shmem_put_link(struct inode *unused, void *cookie)
25111da177e4SLinus Torvalds {
2512cc314eefSLinus Torvalds 	struct page *page = cookie;
25131da177e4SLinus Torvalds 	kunmap(page);
25141da177e4SLinus Torvalds 	mark_page_accessed(page);
25151da177e4SLinus Torvalds 	page_cache_release(page);
25161da177e4SLinus Torvalds }
25171da177e4SLinus Torvalds 
2518b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2519b09e0fa4SEric Paris /*
2520b09e0fa4SEric Paris  * Superblocks without xattr inode operations may get some security.* xattr
2521b09e0fa4SEric Paris  * support from the LSM "for free". As soon as we have any other xattrs
2522b09e0fa4SEric Paris  * like ACLs, we also need to implement the security.* handlers at
2523b09e0fa4SEric Paris  * filesystem level, though.
2524b09e0fa4SEric Paris  */
2525b09e0fa4SEric Paris 
25266d9d88d0SJarkko Sakkinen /*
25276d9d88d0SJarkko Sakkinen  * Callback for security_inode_init_security() for acquiring xattrs.
25286d9d88d0SJarkko Sakkinen  */
25296d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode,
25306d9d88d0SJarkko Sakkinen 			    const struct xattr *xattr_array,
25316d9d88d0SJarkko Sakkinen 			    void *fs_info)
25326d9d88d0SJarkko Sakkinen {
25336d9d88d0SJarkko Sakkinen 	struct shmem_inode_info *info = SHMEM_I(inode);
25346d9d88d0SJarkko Sakkinen 	const struct xattr *xattr;
253538f38657SAristeu Rozanski 	struct simple_xattr *new_xattr;
25366d9d88d0SJarkko Sakkinen 	size_t len;
25376d9d88d0SJarkko Sakkinen 
25386d9d88d0SJarkko Sakkinen 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
253938f38657SAristeu Rozanski 		new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
25406d9d88d0SJarkko Sakkinen 		if (!new_xattr)
25416d9d88d0SJarkko Sakkinen 			return -ENOMEM;
25426d9d88d0SJarkko Sakkinen 
25436d9d88d0SJarkko Sakkinen 		len = strlen(xattr->name) + 1;
25446d9d88d0SJarkko Sakkinen 		new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
25456d9d88d0SJarkko Sakkinen 					  GFP_KERNEL);
25466d9d88d0SJarkko Sakkinen 		if (!new_xattr->name) {
25476d9d88d0SJarkko Sakkinen 			kfree(new_xattr);
25486d9d88d0SJarkko Sakkinen 			return -ENOMEM;
25496d9d88d0SJarkko Sakkinen 		}
25506d9d88d0SJarkko Sakkinen 
25516d9d88d0SJarkko Sakkinen 		memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
25526d9d88d0SJarkko Sakkinen 		       XATTR_SECURITY_PREFIX_LEN);
25536d9d88d0SJarkko Sakkinen 		memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
25546d9d88d0SJarkko Sakkinen 		       xattr->name, len);
25556d9d88d0SJarkko Sakkinen 
255638f38657SAristeu Rozanski 		simple_xattr_list_add(&info->xattrs, new_xattr);
25576d9d88d0SJarkko Sakkinen 	}
25586d9d88d0SJarkko Sakkinen 
25596d9d88d0SJarkko Sakkinen 	return 0;
25606d9d88d0SJarkko Sakkinen }
25616d9d88d0SJarkko Sakkinen 
2562b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = {
2563b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
2564feda821eSChristoph Hellwig 	&posix_acl_access_xattr_handler,
2565feda821eSChristoph Hellwig 	&posix_acl_default_xattr_handler,
2566b09e0fa4SEric Paris #endif
2567b09e0fa4SEric Paris 	NULL
2568b09e0fa4SEric Paris };
2569b09e0fa4SEric Paris 
2570b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name)
2571b09e0fa4SEric Paris {
2572b09e0fa4SEric Paris 	struct { const char *prefix; size_t len; } arr[] = {
2573b09e0fa4SEric Paris 		{ XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
2574b09e0fa4SEric Paris 		{ XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
2575b09e0fa4SEric Paris 	};
2576b09e0fa4SEric Paris 	int i;
2577b09e0fa4SEric Paris 
2578b09e0fa4SEric Paris 	for (i = 0; i < ARRAY_SIZE(arr); i++) {
2579b09e0fa4SEric Paris 		size_t preflen = arr[i].len;
2580b09e0fa4SEric Paris 		if (strncmp(name, arr[i].prefix, preflen) == 0) {
2581b09e0fa4SEric Paris 			if (!name[preflen])
2582b09e0fa4SEric Paris 				return -EINVAL;
2583b09e0fa4SEric Paris 			return 0;
2584b09e0fa4SEric Paris 		}
2585b09e0fa4SEric Paris 	}
2586b09e0fa4SEric Paris 	return -EOPNOTSUPP;
2587b09e0fa4SEric Paris }
2588b09e0fa4SEric Paris 
2589b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
2590b09e0fa4SEric Paris 			      void *buffer, size_t size)
2591b09e0fa4SEric Paris {
259275c3cfa8SDavid Howells 	struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
2593b09e0fa4SEric Paris 	int err;
2594b09e0fa4SEric Paris 
2595b09e0fa4SEric Paris 	/*
2596b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2597b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2598b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2599b09e0fa4SEric Paris 	 */
2600b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2601b09e0fa4SEric Paris 		return generic_getxattr(dentry, name, buffer, size);
2602b09e0fa4SEric Paris 
2603b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2604b09e0fa4SEric Paris 	if (err)
2605b09e0fa4SEric Paris 		return err;
2606b09e0fa4SEric Paris 
260738f38657SAristeu Rozanski 	return simple_xattr_get(&info->xattrs, name, buffer, size);
2608b09e0fa4SEric Paris }
2609b09e0fa4SEric Paris 
2610b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name,
2611b09e0fa4SEric Paris 			  const void *value, size_t size, int flags)
2612b09e0fa4SEric Paris {
261375c3cfa8SDavid Howells 	struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
2614b09e0fa4SEric Paris 	int err;
2615b09e0fa4SEric Paris 
2616b09e0fa4SEric Paris 	/*
2617b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2618b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2619b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2620b09e0fa4SEric Paris 	 */
2621b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2622b09e0fa4SEric Paris 		return generic_setxattr(dentry, name, value, size, flags);
2623b09e0fa4SEric Paris 
2624b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2625b09e0fa4SEric Paris 	if (err)
2626b09e0fa4SEric Paris 		return err;
2627b09e0fa4SEric Paris 
262838f38657SAristeu Rozanski 	return simple_xattr_set(&info->xattrs, name, value, size, flags);
2629b09e0fa4SEric Paris }
2630b09e0fa4SEric Paris 
2631b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name)
2632b09e0fa4SEric Paris {
263375c3cfa8SDavid Howells 	struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
2634b09e0fa4SEric Paris 	int err;
2635b09e0fa4SEric Paris 
2636b09e0fa4SEric Paris 	/*
2637b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2638b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2639b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2640b09e0fa4SEric Paris 	 */
2641b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2642b09e0fa4SEric Paris 		return generic_removexattr(dentry, name);
2643b09e0fa4SEric Paris 
2644b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2645b09e0fa4SEric Paris 	if (err)
2646b09e0fa4SEric Paris 		return err;
2647b09e0fa4SEric Paris 
264838f38657SAristeu Rozanski 	return simple_xattr_remove(&info->xattrs, name);
2649b09e0fa4SEric Paris }
2650b09e0fa4SEric Paris 
2651b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2652b09e0fa4SEric Paris {
265375c3cfa8SDavid Howells 	struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
265438f38657SAristeu Rozanski 	return simple_xattr_list(&info->xattrs, buffer, size);
2655b09e0fa4SEric Paris }
2656b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */
2657b09e0fa4SEric Paris 
265869f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = {
26591da177e4SLinus Torvalds 	.readlink	= generic_readlink,
266060380f19SAl Viro 	.follow_link	= simple_follow_link,
2661b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2662b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2663b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2664b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2665b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2666b09e0fa4SEric Paris #endif
26671da177e4SLinus Torvalds };
26681da177e4SLinus Torvalds 
266992e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = {
26701da177e4SLinus Torvalds 	.readlink	= generic_readlink,
26711da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link,
26721da177e4SLinus Torvalds 	.put_link	= shmem_put_link,
2673b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2674b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2675b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2676b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2677b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
267839f0247dSAndreas Gruenbacher #endif
2679b09e0fa4SEric Paris };
268039f0247dSAndreas Gruenbacher 
268191828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child)
268291828a40SDavid M. Grimes {
268391828a40SDavid M. Grimes 	return ERR_PTR(-ESTALE);
268491828a40SDavid M. Grimes }
268591828a40SDavid M. Grimes 
268691828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh)
268791828a40SDavid M. Grimes {
268891828a40SDavid M. Grimes 	__u32 *fh = vfh;
268991828a40SDavid M. Grimes 	__u64 inum = fh[2];
269091828a40SDavid M. Grimes 	inum = (inum << 32) | fh[1];
269191828a40SDavid M. Grimes 	return ino->i_ino == inum && fh[0] == ino->i_generation;
269291828a40SDavid M. Grimes }
269391828a40SDavid M. Grimes 
2694480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2695480b116cSChristoph Hellwig 		struct fid *fid, int fh_len, int fh_type)
269691828a40SDavid M. Grimes {
269791828a40SDavid M. Grimes 	struct inode *inode;
2698480b116cSChristoph Hellwig 	struct dentry *dentry = NULL;
269935c2a7f4SHugh Dickins 	u64 inum;
270091828a40SDavid M. Grimes 
2701480b116cSChristoph Hellwig 	if (fh_len < 3)
2702480b116cSChristoph Hellwig 		return NULL;
2703480b116cSChristoph Hellwig 
270435c2a7f4SHugh Dickins 	inum = fid->raw[2];
270535c2a7f4SHugh Dickins 	inum = (inum << 32) | fid->raw[1];
270635c2a7f4SHugh Dickins 
2707480b116cSChristoph Hellwig 	inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2708480b116cSChristoph Hellwig 			shmem_match, fid->raw);
270991828a40SDavid M. Grimes 	if (inode) {
2710480b116cSChristoph Hellwig 		dentry = d_find_alias(inode);
271191828a40SDavid M. Grimes 		iput(inode);
271291828a40SDavid M. Grimes 	}
271391828a40SDavid M. Grimes 
2714480b116cSChristoph Hellwig 	return dentry;
271591828a40SDavid M. Grimes }
271691828a40SDavid M. Grimes 
2717b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
2718b0b0382bSAl Viro 				struct inode *parent)
271991828a40SDavid M. Grimes {
27205fe0c237SAneesh Kumar K.V 	if (*len < 3) {
27215fe0c237SAneesh Kumar K.V 		*len = 3;
272294e07a75SNamjae Jeon 		return FILEID_INVALID;
27235fe0c237SAneesh Kumar K.V 	}
272491828a40SDavid M. Grimes 
27251d3382cbSAl Viro 	if (inode_unhashed(inode)) {
272691828a40SDavid M. Grimes 		/* Unfortunately insert_inode_hash is not idempotent,
272791828a40SDavid M. Grimes 		 * so as we hash inodes here rather than at creation
272891828a40SDavid M. Grimes 		 * time, we need a lock to ensure we only try
272991828a40SDavid M. Grimes 		 * to do it once
273091828a40SDavid M. Grimes 		 */
273191828a40SDavid M. Grimes 		static DEFINE_SPINLOCK(lock);
273291828a40SDavid M. Grimes 		spin_lock(&lock);
27331d3382cbSAl Viro 		if (inode_unhashed(inode))
273491828a40SDavid M. Grimes 			__insert_inode_hash(inode,
273591828a40SDavid M. Grimes 					    inode->i_ino + inode->i_generation);
273691828a40SDavid M. Grimes 		spin_unlock(&lock);
273791828a40SDavid M. Grimes 	}
273891828a40SDavid M. Grimes 
273991828a40SDavid M. Grimes 	fh[0] = inode->i_generation;
274091828a40SDavid M. Grimes 	fh[1] = inode->i_ino;
274191828a40SDavid M. Grimes 	fh[2] = ((__u64)inode->i_ino) >> 32;
274291828a40SDavid M. Grimes 
274391828a40SDavid M. Grimes 	*len = 3;
274491828a40SDavid M. Grimes 	return 1;
274591828a40SDavid M. Grimes }
274691828a40SDavid M. Grimes 
274739655164SChristoph Hellwig static const struct export_operations shmem_export_ops = {
274891828a40SDavid M. Grimes 	.get_parent     = shmem_get_parent,
274991828a40SDavid M. Grimes 	.encode_fh      = shmem_encode_fh,
2750480b116cSChristoph Hellwig 	.fh_to_dentry	= shmem_fh_to_dentry,
275191828a40SDavid M. Grimes };
275291828a40SDavid M. Grimes 
2753680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2754680d794bSakpm@linux-foundation.org 			       bool remount)
27551da177e4SLinus Torvalds {
27561da177e4SLinus Torvalds 	char *this_char, *value, *rest;
275749cd0a5cSGreg Thelen 	struct mempolicy *mpol = NULL;
27588751e039SEric W. Biederman 	uid_t uid;
27598751e039SEric W. Biederman 	gid_t gid;
27601da177e4SLinus Torvalds 
2761b00dc3adSHugh Dickins 	while (options != NULL) {
2762b00dc3adSHugh Dickins 		this_char = options;
2763b00dc3adSHugh Dickins 		for (;;) {
2764b00dc3adSHugh Dickins 			/*
2765b00dc3adSHugh Dickins 			 * NUL-terminate this option: unfortunately,
2766b00dc3adSHugh Dickins 			 * mount options form a comma-separated list,
2767b00dc3adSHugh Dickins 			 * but mpol's nodelist may also contain commas.
2768b00dc3adSHugh Dickins 			 */
2769b00dc3adSHugh Dickins 			options = strchr(options, ',');
2770b00dc3adSHugh Dickins 			if (options == NULL)
2771b00dc3adSHugh Dickins 				break;
2772b00dc3adSHugh Dickins 			options++;
2773b00dc3adSHugh Dickins 			if (!isdigit(*options)) {
2774b00dc3adSHugh Dickins 				options[-1] = '\0';
2775b00dc3adSHugh Dickins 				break;
2776b00dc3adSHugh Dickins 			}
2777b00dc3adSHugh Dickins 		}
27781da177e4SLinus Torvalds 		if (!*this_char)
27791da177e4SLinus Torvalds 			continue;
27801da177e4SLinus Torvalds 		if ((value = strchr(this_char,'=')) != NULL) {
27811da177e4SLinus Torvalds 			*value++ = 0;
27821da177e4SLinus Torvalds 		} else {
27831da177e4SLinus Torvalds 			printk(KERN_ERR
27841da177e4SLinus Torvalds 			    "tmpfs: No value for mount option '%s'\n",
27851da177e4SLinus Torvalds 			    this_char);
278649cd0a5cSGreg Thelen 			goto error;
27871da177e4SLinus Torvalds 		}
27881da177e4SLinus Torvalds 
27891da177e4SLinus Torvalds 		if (!strcmp(this_char,"size")) {
27901da177e4SLinus Torvalds 			unsigned long long size;
27911da177e4SLinus Torvalds 			size = memparse(value,&rest);
27921da177e4SLinus Torvalds 			if (*rest == '%') {
27931da177e4SLinus Torvalds 				size <<= PAGE_SHIFT;
27941da177e4SLinus Torvalds 				size *= totalram_pages;
27951da177e4SLinus Torvalds 				do_div(size, 100);
27961da177e4SLinus Torvalds 				rest++;
27971da177e4SLinus Torvalds 			}
27981da177e4SLinus Torvalds 			if (*rest)
27991da177e4SLinus Torvalds 				goto bad_val;
2800680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks =
2801680d794bSakpm@linux-foundation.org 				DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
28021da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_blocks")) {
2803680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks = memparse(value, &rest);
28041da177e4SLinus Torvalds 			if (*rest)
28051da177e4SLinus Torvalds 				goto bad_val;
28061da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_inodes")) {
2807680d794bSakpm@linux-foundation.org 			sbinfo->max_inodes = memparse(value, &rest);
28081da177e4SLinus Torvalds 			if (*rest)
28091da177e4SLinus Torvalds 				goto bad_val;
28101da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"mode")) {
2811680d794bSakpm@linux-foundation.org 			if (remount)
28121da177e4SLinus Torvalds 				continue;
2813680d794bSakpm@linux-foundation.org 			sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
28141da177e4SLinus Torvalds 			if (*rest)
28151da177e4SLinus Torvalds 				goto bad_val;
28161da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"uid")) {
2817680d794bSakpm@linux-foundation.org 			if (remount)
28181da177e4SLinus Torvalds 				continue;
28198751e039SEric W. Biederman 			uid = simple_strtoul(value, &rest, 0);
28201da177e4SLinus Torvalds 			if (*rest)
28211da177e4SLinus Torvalds 				goto bad_val;
28228751e039SEric W. Biederman 			sbinfo->uid = make_kuid(current_user_ns(), uid);
28238751e039SEric W. Biederman 			if (!uid_valid(sbinfo->uid))
28248751e039SEric W. Biederman 				goto bad_val;
28251da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"gid")) {
2826680d794bSakpm@linux-foundation.org 			if (remount)
28271da177e4SLinus Torvalds 				continue;
28288751e039SEric W. Biederman 			gid = simple_strtoul(value, &rest, 0);
28291da177e4SLinus Torvalds 			if (*rest)
28301da177e4SLinus Torvalds 				goto bad_val;
28318751e039SEric W. Biederman 			sbinfo->gid = make_kgid(current_user_ns(), gid);
28328751e039SEric W. Biederman 			if (!gid_valid(sbinfo->gid))
28338751e039SEric W. Biederman 				goto bad_val;
28347339ff83SRobin Holt 		} else if (!strcmp(this_char,"mpol")) {
283549cd0a5cSGreg Thelen 			mpol_put(mpol);
283649cd0a5cSGreg Thelen 			mpol = NULL;
283749cd0a5cSGreg Thelen 			if (mpol_parse_str(value, &mpol))
28387339ff83SRobin Holt 				goto bad_val;
28391da177e4SLinus Torvalds 		} else {
28401da177e4SLinus Torvalds 			printk(KERN_ERR "tmpfs: Bad mount option %s\n",
28411da177e4SLinus Torvalds 			       this_char);
284249cd0a5cSGreg Thelen 			goto error;
28431da177e4SLinus Torvalds 		}
28441da177e4SLinus Torvalds 	}
284549cd0a5cSGreg Thelen 	sbinfo->mpol = mpol;
28461da177e4SLinus Torvalds 	return 0;
28471da177e4SLinus Torvalds 
28481da177e4SLinus Torvalds bad_val:
28491da177e4SLinus Torvalds 	printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
28501da177e4SLinus Torvalds 	       value, this_char);
285149cd0a5cSGreg Thelen error:
285249cd0a5cSGreg Thelen 	mpol_put(mpol);
28531da177e4SLinus Torvalds 	return 1;
28541da177e4SLinus Torvalds 
28551da177e4SLinus Torvalds }
28561da177e4SLinus Torvalds 
28571da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
28581da177e4SLinus Torvalds {
28591da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2860680d794bSakpm@linux-foundation.org 	struct shmem_sb_info config = *sbinfo;
28610edd73b3SHugh Dickins 	unsigned long inodes;
28620edd73b3SHugh Dickins 	int error = -EINVAL;
28631da177e4SLinus Torvalds 
28645f00110fSGreg Thelen 	config.mpol = NULL;
2865680d794bSakpm@linux-foundation.org 	if (shmem_parse_options(data, &config, true))
28660edd73b3SHugh Dickins 		return error;
28670edd73b3SHugh Dickins 
28680edd73b3SHugh Dickins 	spin_lock(&sbinfo->stat_lock);
28690edd73b3SHugh Dickins 	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
28707e496299STim Chen 	if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
28710edd73b3SHugh Dickins 		goto out;
2872680d794bSakpm@linux-foundation.org 	if (config.max_inodes < inodes)
28730edd73b3SHugh Dickins 		goto out;
28740edd73b3SHugh Dickins 	/*
287554af6042SHugh Dickins 	 * Those tests disallow limited->unlimited while any are in use;
28760edd73b3SHugh Dickins 	 * but we must separately disallow unlimited->limited, because
28770edd73b3SHugh Dickins 	 * in that case we have no record of how much is already in use.
28780edd73b3SHugh Dickins 	 */
2879680d794bSakpm@linux-foundation.org 	if (config.max_blocks && !sbinfo->max_blocks)
28800edd73b3SHugh Dickins 		goto out;
2881680d794bSakpm@linux-foundation.org 	if (config.max_inodes && !sbinfo->max_inodes)
28820edd73b3SHugh Dickins 		goto out;
28830edd73b3SHugh Dickins 
28840edd73b3SHugh Dickins 	error = 0;
2885680d794bSakpm@linux-foundation.org 	sbinfo->max_blocks  = config.max_blocks;
2886680d794bSakpm@linux-foundation.org 	sbinfo->max_inodes  = config.max_inodes;
2887680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = config.max_inodes - inodes;
288871fe804bSLee Schermerhorn 
28895f00110fSGreg Thelen 	/*
28905f00110fSGreg Thelen 	 * Preserve previous mempolicy unless mpol remount option was specified.
28915f00110fSGreg Thelen 	 */
28925f00110fSGreg Thelen 	if (config.mpol) {
289371fe804bSLee Schermerhorn 		mpol_put(sbinfo->mpol);
289471fe804bSLee Schermerhorn 		sbinfo->mpol = config.mpol;	/* transfers initial ref */
28955f00110fSGreg Thelen 	}
28960edd73b3SHugh Dickins out:
28970edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
28980edd73b3SHugh Dickins 	return error;
28991da177e4SLinus Torvalds }
2900680d794bSakpm@linux-foundation.org 
290134c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root)
2902680d794bSakpm@linux-foundation.org {
290334c80b1dSAl Viro 	struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
2904680d794bSakpm@linux-foundation.org 
2905680d794bSakpm@linux-foundation.org 	if (sbinfo->max_blocks != shmem_default_max_blocks())
2906680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",size=%luk",
2907680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2908680d794bSakpm@linux-foundation.org 	if (sbinfo->max_inodes != shmem_default_max_inodes())
2909680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2910680d794bSakpm@linux-foundation.org 	if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
291109208d15SAl Viro 		seq_printf(seq, ",mode=%03ho", sbinfo->mode);
29128751e039SEric W. Biederman 	if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
29138751e039SEric W. Biederman 		seq_printf(seq, ",uid=%u",
29148751e039SEric W. Biederman 				from_kuid_munged(&init_user_ns, sbinfo->uid));
29158751e039SEric W. Biederman 	if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
29168751e039SEric W. Biederman 		seq_printf(seq, ",gid=%u",
29178751e039SEric W. Biederman 				from_kgid_munged(&init_user_ns, sbinfo->gid));
291871fe804bSLee Schermerhorn 	shmem_show_mpol(seq, sbinfo->mpol);
2919680d794bSakpm@linux-foundation.org 	return 0;
2920680d794bSakpm@linux-foundation.org }
29219183df25SDavid Herrmann 
29229183df25SDavid Herrmann #define MFD_NAME_PREFIX "memfd:"
29239183df25SDavid Herrmann #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
29249183df25SDavid Herrmann #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
29259183df25SDavid Herrmann 
29269183df25SDavid Herrmann #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
29279183df25SDavid Herrmann 
29289183df25SDavid Herrmann SYSCALL_DEFINE2(memfd_create,
29299183df25SDavid Herrmann 		const char __user *, uname,
29309183df25SDavid Herrmann 		unsigned int, flags)
29319183df25SDavid Herrmann {
29329183df25SDavid Herrmann 	struct shmem_inode_info *info;
29339183df25SDavid Herrmann 	struct file *file;
29349183df25SDavid Herrmann 	int fd, error;
29359183df25SDavid Herrmann 	char *name;
29369183df25SDavid Herrmann 	long len;
29379183df25SDavid Herrmann 
29389183df25SDavid Herrmann 	if (flags & ~(unsigned int)MFD_ALL_FLAGS)
29399183df25SDavid Herrmann 		return -EINVAL;
29409183df25SDavid Herrmann 
29419183df25SDavid Herrmann 	/* length includes terminating zero */
29429183df25SDavid Herrmann 	len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
29439183df25SDavid Herrmann 	if (len <= 0)
29449183df25SDavid Herrmann 		return -EFAULT;
29459183df25SDavid Herrmann 	if (len > MFD_NAME_MAX_LEN + 1)
29469183df25SDavid Herrmann 		return -EINVAL;
29479183df25SDavid Herrmann 
29489183df25SDavid Herrmann 	name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
29499183df25SDavid Herrmann 	if (!name)
29509183df25SDavid Herrmann 		return -ENOMEM;
29519183df25SDavid Herrmann 
29529183df25SDavid Herrmann 	strcpy(name, MFD_NAME_PREFIX);
29539183df25SDavid Herrmann 	if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
29549183df25SDavid Herrmann 		error = -EFAULT;
29559183df25SDavid Herrmann 		goto err_name;
29569183df25SDavid Herrmann 	}
29579183df25SDavid Herrmann 
29589183df25SDavid Herrmann 	/* terminating-zero may have changed after strnlen_user() returned */
29599183df25SDavid Herrmann 	if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
29609183df25SDavid Herrmann 		error = -EFAULT;
29619183df25SDavid Herrmann 		goto err_name;
29629183df25SDavid Herrmann 	}
29639183df25SDavid Herrmann 
29649183df25SDavid Herrmann 	fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
29659183df25SDavid Herrmann 	if (fd < 0) {
29669183df25SDavid Herrmann 		error = fd;
29679183df25SDavid Herrmann 		goto err_name;
29689183df25SDavid Herrmann 	}
29699183df25SDavid Herrmann 
29709183df25SDavid Herrmann 	file = shmem_file_setup(name, 0, VM_NORESERVE);
29719183df25SDavid Herrmann 	if (IS_ERR(file)) {
29729183df25SDavid Herrmann 		error = PTR_ERR(file);
29739183df25SDavid Herrmann 		goto err_fd;
29749183df25SDavid Herrmann 	}
29759183df25SDavid Herrmann 	info = SHMEM_I(file_inode(file));
29769183df25SDavid Herrmann 	file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
29779183df25SDavid Herrmann 	file->f_flags |= O_RDWR | O_LARGEFILE;
29789183df25SDavid Herrmann 	if (flags & MFD_ALLOW_SEALING)
29799183df25SDavid Herrmann 		info->seals &= ~F_SEAL_SEAL;
29809183df25SDavid Herrmann 
29819183df25SDavid Herrmann 	fd_install(fd, file);
29829183df25SDavid Herrmann 	kfree(name);
29839183df25SDavid Herrmann 	return fd;
29849183df25SDavid Herrmann 
29859183df25SDavid Herrmann err_fd:
29869183df25SDavid Herrmann 	put_unused_fd(fd);
29879183df25SDavid Herrmann err_name:
29889183df25SDavid Herrmann 	kfree(name);
29899183df25SDavid Herrmann 	return error;
29909183df25SDavid Herrmann }
29919183df25SDavid Herrmann 
2992680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
29931da177e4SLinus Torvalds 
29941da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb)
29951da177e4SLinus Torvalds {
2996602586a8SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2997602586a8SHugh Dickins 
2998602586a8SHugh Dickins 	percpu_counter_destroy(&sbinfo->used_blocks);
299949cd0a5cSGreg Thelen 	mpol_put(sbinfo->mpol);
3000602586a8SHugh Dickins 	kfree(sbinfo);
30011da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
30021da177e4SLinus Torvalds }
30031da177e4SLinus Torvalds 
30042b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent)
30051da177e4SLinus Torvalds {
30061da177e4SLinus Torvalds 	struct inode *inode;
30070edd73b3SHugh Dickins 	struct shmem_sb_info *sbinfo;
3008680d794bSakpm@linux-foundation.org 	int err = -ENOMEM;
3009680d794bSakpm@linux-foundation.org 
3010680d794bSakpm@linux-foundation.org 	/* Round up to L1_CACHE_BYTES to resist false sharing */
3011425fbf04SPekka Enberg 	sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
3012680d794bSakpm@linux-foundation.org 				L1_CACHE_BYTES), GFP_KERNEL);
3013680d794bSakpm@linux-foundation.org 	if (!sbinfo)
3014680d794bSakpm@linux-foundation.org 		return -ENOMEM;
3015680d794bSakpm@linux-foundation.org 
3016680d794bSakpm@linux-foundation.org 	sbinfo->mode = S_IRWXUGO | S_ISVTX;
301776aac0e9SDavid Howells 	sbinfo->uid = current_fsuid();
301876aac0e9SDavid Howells 	sbinfo->gid = current_fsgid();
3019680d794bSakpm@linux-foundation.org 	sb->s_fs_info = sbinfo;
30201da177e4SLinus Torvalds 
30210edd73b3SHugh Dickins #ifdef CONFIG_TMPFS
30221da177e4SLinus Torvalds 	/*
30231da177e4SLinus Torvalds 	 * Per default we only allow half of the physical ram per
30241da177e4SLinus Torvalds 	 * tmpfs instance, limiting inodes to one per page of lowmem;
30251da177e4SLinus Torvalds 	 * but the internal instance is left unlimited.
30261da177e4SLinus Torvalds 	 */
3027ca4e0519SAl Viro 	if (!(sb->s_flags & MS_KERNMOUNT)) {
3028680d794bSakpm@linux-foundation.org 		sbinfo->max_blocks = shmem_default_max_blocks();
3029680d794bSakpm@linux-foundation.org 		sbinfo->max_inodes = shmem_default_max_inodes();
3030680d794bSakpm@linux-foundation.org 		if (shmem_parse_options(data, sbinfo, false)) {
3031680d794bSakpm@linux-foundation.org 			err = -EINVAL;
3032680d794bSakpm@linux-foundation.org 			goto failed;
3033680d794bSakpm@linux-foundation.org 		}
3034ca4e0519SAl Viro 	} else {
3035ca4e0519SAl Viro 		sb->s_flags |= MS_NOUSER;
30361da177e4SLinus Torvalds 	}
303791828a40SDavid M. Grimes 	sb->s_export_op = &shmem_export_ops;
30382f6e38f3SHugh Dickins 	sb->s_flags |= MS_NOSEC;
30390edd73b3SHugh Dickins #else
30400edd73b3SHugh Dickins 	sb->s_flags |= MS_NOUSER;
30410edd73b3SHugh Dickins #endif
30421da177e4SLinus Torvalds 
30431da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
3044908c7f19STejun Heo 	if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
3045602586a8SHugh Dickins 		goto failed;
3046680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = sbinfo->max_inodes;
30471da177e4SLinus Torvalds 
3048285b2c4fSHugh Dickins 	sb->s_maxbytes = MAX_LFS_FILESIZE;
30491da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
30501da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
30511da177e4SLinus Torvalds 	sb->s_magic = TMPFS_MAGIC;
30521da177e4SLinus Torvalds 	sb->s_op = &shmem_ops;
3053cfd95a9cSRobin H. Johnson 	sb->s_time_gran = 1;
3054b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
305539f0247dSAndreas Gruenbacher 	sb->s_xattr = shmem_xattr_handlers;
3056b09e0fa4SEric Paris #endif
3057b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
305839f0247dSAndreas Gruenbacher 	sb->s_flags |= MS_POSIXACL;
305939f0247dSAndreas Gruenbacher #endif
30600edd73b3SHugh Dickins 
3061454abafeSDmitry Monakhov 	inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
30621da177e4SLinus Torvalds 	if (!inode)
30631da177e4SLinus Torvalds 		goto failed;
3064680d794bSakpm@linux-foundation.org 	inode->i_uid = sbinfo->uid;
3065680d794bSakpm@linux-foundation.org 	inode->i_gid = sbinfo->gid;
3066318ceed0SAl Viro 	sb->s_root = d_make_root(inode);
3067318ceed0SAl Viro 	if (!sb->s_root)
306848fde701SAl Viro 		goto failed;
30691da177e4SLinus Torvalds 	return 0;
30701da177e4SLinus Torvalds 
30711da177e4SLinus Torvalds failed:
30721da177e4SLinus Torvalds 	shmem_put_super(sb);
30731da177e4SLinus Torvalds 	return err;
30741da177e4SLinus Torvalds }
30751da177e4SLinus Torvalds 
3076fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep;
30771da177e4SLinus Torvalds 
30781da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb)
30791da177e4SLinus Torvalds {
308041ffe5d5SHugh Dickins 	struct shmem_inode_info *info;
308141ffe5d5SHugh Dickins 	info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
308241ffe5d5SHugh Dickins 	if (!info)
30831da177e4SLinus Torvalds 		return NULL;
308441ffe5d5SHugh Dickins 	return &info->vfs_inode;
30851da177e4SLinus Torvalds }
30861da177e4SLinus Torvalds 
308741ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head)
3088fa0d7e3dSNick Piggin {
3089fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
3090fa0d7e3dSNick Piggin 	kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3091fa0d7e3dSNick Piggin }
3092fa0d7e3dSNick Piggin 
30931da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode)
30941da177e4SLinus Torvalds {
309509208d15SAl Viro 	if (S_ISREG(inode->i_mode))
30961da177e4SLinus Torvalds 		mpol_free_shared_policy(&SHMEM_I(inode)->policy);
309741ffe5d5SHugh Dickins 	call_rcu(&inode->i_rcu, shmem_destroy_callback);
30981da177e4SLinus Torvalds }
30991da177e4SLinus Torvalds 
310041ffe5d5SHugh Dickins static void shmem_init_inode(void *foo)
31011da177e4SLinus Torvalds {
310241ffe5d5SHugh Dickins 	struct shmem_inode_info *info = foo;
310341ffe5d5SHugh Dickins 	inode_init_once(&info->vfs_inode);
31041da177e4SLinus Torvalds }
31051da177e4SLinus Torvalds 
310641ffe5d5SHugh Dickins static int shmem_init_inodecache(void)
31071da177e4SLinus Torvalds {
31081da177e4SLinus Torvalds 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
31091da177e4SLinus Torvalds 				sizeof(struct shmem_inode_info),
311041ffe5d5SHugh Dickins 				0, SLAB_PANIC, shmem_init_inode);
31111da177e4SLinus Torvalds 	return 0;
31121da177e4SLinus Torvalds }
31131da177e4SLinus Torvalds 
311441ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void)
31151da177e4SLinus Torvalds {
31161a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(shmem_inode_cachep);
31171da177e4SLinus Torvalds }
31181da177e4SLinus Torvalds 
3119f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = {
31201da177e4SLinus Torvalds 	.writepage	= shmem_writepage,
312176719325SKen Chen 	.set_page_dirty	= __set_page_dirty_no_writeback,
31221da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
3123800d15a5SNick Piggin 	.write_begin	= shmem_write_begin,
3124800d15a5SNick Piggin 	.write_end	= shmem_write_end,
31251da177e4SLinus Torvalds #endif
31261c93923cSAndrew Morton #ifdef CONFIG_MIGRATION
3127304dbdb7SLee Schermerhorn 	.migratepage	= migrate_page,
31281c93923cSAndrew Morton #endif
3129aa261f54SAndi Kleen 	.error_remove_page = generic_error_remove_page,
31301da177e4SLinus Torvalds };
31311da177e4SLinus Torvalds 
313215ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = {
31331da177e4SLinus Torvalds 	.mmap		= shmem_mmap,
31341da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
3135220f2ac9SHugh Dickins 	.llseek		= shmem_file_llseek,
31362ba5bbedSAl Viro 	.read_iter	= shmem_file_read_iter,
31378174202bSAl Viro 	.write_iter	= generic_file_write_iter,
31381b061d92SChristoph Hellwig 	.fsync		= noop_fsync,
3139708e3508SHugh Dickins 	.splice_read	= shmem_file_splice_read,
3140f6cb85d0SAl Viro 	.splice_write	= iter_file_splice_write,
314183e4fa9cSHugh Dickins 	.fallocate	= shmem_fallocate,
31421da177e4SLinus Torvalds #endif
31431da177e4SLinus Torvalds };
31441da177e4SLinus Torvalds 
314592e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = {
314644a30220SYu Zhao 	.getattr	= shmem_getattr,
314794c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
3148b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
3149b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
3150b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
3151b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
3152b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
3153feda821eSChristoph Hellwig 	.set_acl	= simple_set_acl,
3154b09e0fa4SEric Paris #endif
31551da177e4SLinus Torvalds };
31561da177e4SLinus Torvalds 
315792e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = {
31581da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
31591da177e4SLinus Torvalds 	.create		= shmem_create,
31601da177e4SLinus Torvalds 	.lookup		= simple_lookup,
31611da177e4SLinus Torvalds 	.link		= shmem_link,
31621da177e4SLinus Torvalds 	.unlink		= shmem_unlink,
31631da177e4SLinus Torvalds 	.symlink	= shmem_symlink,
31641da177e4SLinus Torvalds 	.mkdir		= shmem_mkdir,
31651da177e4SLinus Torvalds 	.rmdir		= shmem_rmdir,
31661da177e4SLinus Torvalds 	.mknod		= shmem_mknod,
31673b69ff51SMiklos Szeredi 	.rename2	= shmem_rename2,
316860545d0dSAl Viro 	.tmpfile	= shmem_tmpfile,
31691da177e4SLinus Torvalds #endif
3170b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
3171b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
3172b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
3173b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
3174b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
3175b09e0fa4SEric Paris #endif
317639f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
317794c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
3178feda821eSChristoph Hellwig 	.set_acl	= simple_set_acl,
317939f0247dSAndreas Gruenbacher #endif
318039f0247dSAndreas Gruenbacher };
318139f0247dSAndreas Gruenbacher 
318292e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = {
3183b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
3184b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
3185b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
3186b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
3187b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
3188b09e0fa4SEric Paris #endif
318939f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
319094c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
3191feda821eSChristoph Hellwig 	.set_acl	= simple_set_acl,
319239f0247dSAndreas Gruenbacher #endif
31931da177e4SLinus Torvalds };
31941da177e4SLinus Torvalds 
3195759b9775SHugh Dickins static const struct super_operations shmem_ops = {
31961da177e4SLinus Torvalds 	.alloc_inode	= shmem_alloc_inode,
31971da177e4SLinus Torvalds 	.destroy_inode	= shmem_destroy_inode,
31981da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
31991da177e4SLinus Torvalds 	.statfs		= shmem_statfs,
32001da177e4SLinus Torvalds 	.remount_fs	= shmem_remount_fs,
3201680d794bSakpm@linux-foundation.org 	.show_options	= shmem_show_options,
32021da177e4SLinus Torvalds #endif
32031f895f75SAl Viro 	.evict_inode	= shmem_evict_inode,
32041da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
32051da177e4SLinus Torvalds 	.put_super	= shmem_put_super,
32061da177e4SLinus Torvalds };
32071da177e4SLinus Torvalds 
3208f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = {
320954cb8821SNick Piggin 	.fault		= shmem_fault,
3210d7c17551SNing Qu 	.map_pages	= filemap_map_pages,
32111da177e4SLinus Torvalds #ifdef CONFIG_NUMA
32121da177e4SLinus Torvalds 	.set_policy     = shmem_set_policy,
32131da177e4SLinus Torvalds 	.get_policy     = shmem_get_policy,
32141da177e4SLinus Torvalds #endif
32151da177e4SLinus Torvalds };
32161da177e4SLinus Torvalds 
32173c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type,
32183c26ff6eSAl Viro 	int flags, const char *dev_name, void *data)
32191da177e4SLinus Torvalds {
32203c26ff6eSAl Viro 	return mount_nodev(fs_type, flags, data, shmem_fill_super);
32211da177e4SLinus Torvalds }
32221da177e4SLinus Torvalds 
322341ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = {
32241da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
32251da177e4SLinus Torvalds 	.name		= "tmpfs",
32263c26ff6eSAl Viro 	.mount		= shmem_mount,
32271da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
32282b8576cbSEric W. Biederman 	.fs_flags	= FS_USERNS_MOUNT,
32291da177e4SLinus Torvalds };
32301da177e4SLinus Torvalds 
323141ffe5d5SHugh Dickins int __init shmem_init(void)
32321da177e4SLinus Torvalds {
32331da177e4SLinus Torvalds 	int error;
32341da177e4SLinus Torvalds 
323516203a7aSRob Landley 	/* If rootfs called this, don't re-init */
323616203a7aSRob Landley 	if (shmem_inode_cachep)
323716203a7aSRob Landley 		return 0;
323816203a7aSRob Landley 
323941ffe5d5SHugh Dickins 	error = shmem_init_inodecache();
32401da177e4SLinus Torvalds 	if (error)
32411da177e4SLinus Torvalds 		goto out3;
32421da177e4SLinus Torvalds 
324341ffe5d5SHugh Dickins 	error = register_filesystem(&shmem_fs_type);
32441da177e4SLinus Torvalds 	if (error) {
32451da177e4SLinus Torvalds 		printk(KERN_ERR "Could not register tmpfs\n");
32461da177e4SLinus Torvalds 		goto out2;
32471da177e4SLinus Torvalds 	}
324895dc112aSGreg Kroah-Hartman 
3249ca4e0519SAl Viro 	shm_mnt = kern_mount(&shmem_fs_type);
32501da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt)) {
32511da177e4SLinus Torvalds 		error = PTR_ERR(shm_mnt);
32521da177e4SLinus Torvalds 		printk(KERN_ERR "Could not kern_mount tmpfs\n");
32531da177e4SLinus Torvalds 		goto out1;
32541da177e4SLinus Torvalds 	}
32551da177e4SLinus Torvalds 	return 0;
32561da177e4SLinus Torvalds 
32571da177e4SLinus Torvalds out1:
325841ffe5d5SHugh Dickins 	unregister_filesystem(&shmem_fs_type);
32591da177e4SLinus Torvalds out2:
326041ffe5d5SHugh Dickins 	shmem_destroy_inodecache();
32611da177e4SLinus Torvalds out3:
32621da177e4SLinus Torvalds 	shm_mnt = ERR_PTR(error);
32631da177e4SLinus Torvalds 	return error;
32641da177e4SLinus Torvalds }
3265853ac43aSMatt Mackall 
3266853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */
3267853ac43aSMatt Mackall 
3268853ac43aSMatt Mackall /*
3269853ac43aSMatt Mackall  * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3270853ac43aSMatt Mackall  *
3271853ac43aSMatt Mackall  * This is intended for small system where the benefits of the full
3272853ac43aSMatt Mackall  * shmem code (swap-backed and resource-limited) are outweighed by
3273853ac43aSMatt Mackall  * their complexity. On systems without swap this code should be
3274853ac43aSMatt Mackall  * effectively equivalent, but much lighter weight.
3275853ac43aSMatt Mackall  */
3276853ac43aSMatt Mackall 
327741ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = {
3278853ac43aSMatt Mackall 	.name		= "tmpfs",
32793c26ff6eSAl Viro 	.mount		= ramfs_mount,
3280853ac43aSMatt Mackall 	.kill_sb	= kill_litter_super,
32812b8576cbSEric W. Biederman 	.fs_flags	= FS_USERNS_MOUNT,
3282853ac43aSMatt Mackall };
3283853ac43aSMatt Mackall 
328441ffe5d5SHugh Dickins int __init shmem_init(void)
3285853ac43aSMatt Mackall {
328641ffe5d5SHugh Dickins 	BUG_ON(register_filesystem(&shmem_fs_type) != 0);
3287853ac43aSMatt Mackall 
328841ffe5d5SHugh Dickins 	shm_mnt = kern_mount(&shmem_fs_type);
3289853ac43aSMatt Mackall 	BUG_ON(IS_ERR(shm_mnt));
3290853ac43aSMatt Mackall 
3291853ac43aSMatt Mackall 	return 0;
3292853ac43aSMatt Mackall }
3293853ac43aSMatt Mackall 
329441ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page)
3295853ac43aSMatt Mackall {
3296853ac43aSMatt Mackall 	return 0;
3297853ac43aSMatt Mackall }
3298853ac43aSMatt Mackall 
32993f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user)
33003f96b79aSHugh Dickins {
33013f96b79aSHugh Dickins 	return 0;
33023f96b79aSHugh Dickins }
33033f96b79aSHugh Dickins 
330424513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping)
330524513264SHugh Dickins {
330624513264SHugh Dickins }
330724513264SHugh Dickins 
330841ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
330994c1e62dSHugh Dickins {
331041ffe5d5SHugh Dickins 	truncate_inode_pages_range(inode->i_mapping, lstart, lend);
331194c1e62dSHugh Dickins }
331294c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
331394c1e62dSHugh Dickins 
3314853ac43aSMatt Mackall #define shmem_vm_ops				generic_file_vm_ops
33150b0a0806SHugh Dickins #define shmem_file_operations			ramfs_file_operations
3316454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags)	ramfs_get_inode(sb, dir, mode, dev)
33170b0a0806SHugh Dickins #define shmem_acct_size(flags, size)		0
33180b0a0806SHugh Dickins #define shmem_unacct_size(flags, size)		do {} while (0)
3319853ac43aSMatt Mackall 
3320853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */
3321853ac43aSMatt Mackall 
3322853ac43aSMatt Mackall /* common code */
33231da177e4SLinus Torvalds 
33243451538aSAl Viro static struct dentry_operations anon_ops = {
3325118b2302SAl Viro 	.d_dname = simple_dname
33263451538aSAl Viro };
33273451538aSAl Viro 
3328c7277090SEric Paris static struct file *__shmem_file_setup(const char *name, loff_t size,
3329c7277090SEric Paris 				       unsigned long flags, unsigned int i_flags)
33301da177e4SLinus Torvalds {
33316b4d0b27SAl Viro 	struct file *res;
33321da177e4SLinus Torvalds 	struct inode *inode;
33332c48b9c4SAl Viro 	struct path path;
33343451538aSAl Viro 	struct super_block *sb;
33351da177e4SLinus Torvalds 	struct qstr this;
33361da177e4SLinus Torvalds 
33371da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt))
33386b4d0b27SAl Viro 		return ERR_CAST(shm_mnt);
33391da177e4SLinus Torvalds 
3340285b2c4fSHugh Dickins 	if (size < 0 || size > MAX_LFS_FILESIZE)
33411da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
33421da177e4SLinus Torvalds 
33431da177e4SLinus Torvalds 	if (shmem_acct_size(flags, size))
33441da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
33451da177e4SLinus Torvalds 
33466b4d0b27SAl Viro 	res = ERR_PTR(-ENOMEM);
33471da177e4SLinus Torvalds 	this.name = name;
33481da177e4SLinus Torvalds 	this.len = strlen(name);
33491da177e4SLinus Torvalds 	this.hash = 0; /* will go */
33503451538aSAl Viro 	sb = shm_mnt->mnt_sb;
335166ee4b88SKonstantin Khlebnikov 	path.mnt = mntget(shm_mnt);
33523451538aSAl Viro 	path.dentry = d_alloc_pseudo(sb, &this);
33532c48b9c4SAl Viro 	if (!path.dentry)
33541da177e4SLinus Torvalds 		goto put_memory;
33553451538aSAl Viro 	d_set_d_op(path.dentry, &anon_ops);
33561da177e4SLinus Torvalds 
33576b4d0b27SAl Viro 	res = ERR_PTR(-ENOSPC);
33583451538aSAl Viro 	inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
33591da177e4SLinus Torvalds 	if (!inode)
336066ee4b88SKonstantin Khlebnikov 		goto put_memory;
33611da177e4SLinus Torvalds 
3362c7277090SEric Paris 	inode->i_flags |= i_flags;
33632c48b9c4SAl Viro 	d_instantiate(path.dentry, inode);
33641da177e4SLinus Torvalds 	inode->i_size = size;
33656d6b77f1SMiklos Szeredi 	clear_nlink(inode);	/* It is unlinked */
336626567cdbSAl Viro 	res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
336726567cdbSAl Viro 	if (IS_ERR(res))
336866ee4b88SKonstantin Khlebnikov 		goto put_path;
33694b42af81SAl Viro 
33706b4d0b27SAl Viro 	res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
33714b42af81SAl Viro 		  &shmem_file_operations);
33726b4d0b27SAl Viro 	if (IS_ERR(res))
337366ee4b88SKonstantin Khlebnikov 		goto put_path;
33744b42af81SAl Viro 
33756b4d0b27SAl Viro 	return res;
33761da177e4SLinus Torvalds 
33771da177e4SLinus Torvalds put_memory:
33781da177e4SLinus Torvalds 	shmem_unacct_size(flags, size);
337966ee4b88SKonstantin Khlebnikov put_path:
338066ee4b88SKonstantin Khlebnikov 	path_put(&path);
33816b4d0b27SAl Viro 	return res;
33821da177e4SLinus Torvalds }
3383c7277090SEric Paris 
3384c7277090SEric Paris /**
3385c7277090SEric Paris  * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3386c7277090SEric Paris  * 	kernel internal.  There will be NO LSM permission checks against the
3387c7277090SEric Paris  * 	underlying inode.  So users of this interface must do LSM checks at a
3388e1832f29SStephen Smalley  *	higher layer.  The users are the big_key and shm implementations.  LSM
3389e1832f29SStephen Smalley  *	checks are provided at the key or shm level rather than the inode.
3390c7277090SEric Paris  * @name: name for dentry (to be seen in /proc/<pid>/maps
3391c7277090SEric Paris  * @size: size to be set for the file
3392c7277090SEric Paris  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3393c7277090SEric Paris  */
3394c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3395c7277090SEric Paris {
3396c7277090SEric Paris 	return __shmem_file_setup(name, size, flags, S_PRIVATE);
3397c7277090SEric Paris }
3398c7277090SEric Paris 
3399c7277090SEric Paris /**
3400c7277090SEric Paris  * shmem_file_setup - get an unlinked file living in tmpfs
3401c7277090SEric Paris  * @name: name for dentry (to be seen in /proc/<pid>/maps
3402c7277090SEric Paris  * @size: size to be set for the file
3403c7277090SEric Paris  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3404c7277090SEric Paris  */
3405c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3406c7277090SEric Paris {
3407c7277090SEric Paris 	return __shmem_file_setup(name, size, flags, 0);
3408c7277090SEric Paris }
3409395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup);
34101da177e4SLinus Torvalds 
341146711810SRandy Dunlap /**
34121da177e4SLinus Torvalds  * shmem_zero_setup - setup a shared anonymous mapping
34131da177e4SLinus Torvalds  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
34141da177e4SLinus Torvalds  */
34151da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma)
34161da177e4SLinus Torvalds {
34171da177e4SLinus Torvalds 	struct file *file;
34181da177e4SLinus Torvalds 	loff_t size = vma->vm_end - vma->vm_start;
34191da177e4SLinus Torvalds 
342066fc1303SHugh Dickins 	/*
342166fc1303SHugh Dickins 	 * Cloning a new file under mmap_sem leads to a lock ordering conflict
342266fc1303SHugh Dickins 	 * between XFS directory reading and selinux: since this file is only
342366fc1303SHugh Dickins 	 * accessible to the user through its mapping, use S_PRIVATE flag to
342466fc1303SHugh Dickins 	 * bypass file security, in the same way as shmem_kernel_file_setup().
342566fc1303SHugh Dickins 	 */
342666fc1303SHugh Dickins 	file = __shmem_file_setup("dev/zero", size, vma->vm_flags, S_PRIVATE);
34271da177e4SLinus Torvalds 	if (IS_ERR(file))
34281da177e4SLinus Torvalds 		return PTR_ERR(file);
34291da177e4SLinus Torvalds 
34301da177e4SLinus Torvalds 	if (vma->vm_file)
34311da177e4SLinus Torvalds 		fput(vma->vm_file);
34321da177e4SLinus Torvalds 	vma->vm_file = file;
34331da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
34341da177e4SLinus Torvalds 	return 0;
34351da177e4SLinus Torvalds }
3436d9d90e5eSHugh Dickins 
3437d9d90e5eSHugh Dickins /**
3438d9d90e5eSHugh Dickins  * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3439d9d90e5eSHugh Dickins  * @mapping:	the page's address_space
3440d9d90e5eSHugh Dickins  * @index:	the page index
3441d9d90e5eSHugh Dickins  * @gfp:	the page allocator flags to use if allocating
3442d9d90e5eSHugh Dickins  *
3443d9d90e5eSHugh Dickins  * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3444d9d90e5eSHugh Dickins  * with any new page allocations done using the specified allocation flags.
3445d9d90e5eSHugh Dickins  * But read_cache_page_gfp() uses the ->readpage() method: which does not
3446d9d90e5eSHugh Dickins  * suit tmpfs, since it may have pages in swapcache, and needs to find those
3447d9d90e5eSHugh Dickins  * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
3448d9d90e5eSHugh Dickins  *
344968da9f05SHugh Dickins  * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
345068da9f05SHugh Dickins  * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
3451d9d90e5eSHugh Dickins  */
3452d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
3453d9d90e5eSHugh Dickins 					 pgoff_t index, gfp_t gfp)
3454d9d90e5eSHugh Dickins {
345568da9f05SHugh Dickins #ifdef CONFIG_SHMEM
345668da9f05SHugh Dickins 	struct inode *inode = mapping->host;
34579276aad6SHugh Dickins 	struct page *page;
345868da9f05SHugh Dickins 	int error;
345968da9f05SHugh Dickins 
346068da9f05SHugh Dickins 	BUG_ON(mapping->a_ops != &shmem_aops);
346168da9f05SHugh Dickins 	error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
346268da9f05SHugh Dickins 	if (error)
346368da9f05SHugh Dickins 		page = ERR_PTR(error);
346468da9f05SHugh Dickins 	else
346568da9f05SHugh Dickins 		unlock_page(page);
346668da9f05SHugh Dickins 	return page;
346768da9f05SHugh Dickins #else
346868da9f05SHugh Dickins 	/*
346968da9f05SHugh Dickins 	 * The tiny !SHMEM case uses ramfs without swap
347068da9f05SHugh Dickins 	 */
3471d9d90e5eSHugh Dickins 	return read_cache_page_gfp(mapping, index, gfp);
347268da9f05SHugh Dickins #endif
3473d9d90e5eSHugh Dickins }
3474d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);
3475