xref: /openbmc/linux/mm/shmem.c (revision 9183df25fe7b194563db3fec6dc3202a5855839c)
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>
34a27bb332SKent Overstreet #include <linux/aio.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>
69*9183df25SDavid Herrmann #include <linux/syscalls.h>
7040e041a2SDavid Herrmann #include <linux/fcntl.h>
71*9183df25SDavid 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 
1946c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info  __read_mostly = {
1951da177e4SLinus Torvalds 	.ra_pages	= 0,	/* No readahead */
1964f98a2feSRik van Riel 	.capabilities	= BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
1971da177e4SLinus Torvalds };
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist);
200cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex);
2011da177e4SLinus Torvalds 
2025b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb)
2035b04c689SPavel Emelyanov {
2045b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2055b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2065b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2075b04c689SPavel Emelyanov 		if (!sbinfo->free_inodes) {
2085b04c689SPavel Emelyanov 			spin_unlock(&sbinfo->stat_lock);
2095b04c689SPavel Emelyanov 			return -ENOSPC;
2105b04c689SPavel Emelyanov 		}
2115b04c689SPavel Emelyanov 		sbinfo->free_inodes--;
2125b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2135b04c689SPavel Emelyanov 	}
2145b04c689SPavel Emelyanov 	return 0;
2155b04c689SPavel Emelyanov }
2165b04c689SPavel Emelyanov 
2175b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb)
2185b04c689SPavel Emelyanov {
2195b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2205b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2215b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2225b04c689SPavel Emelyanov 		sbinfo->free_inodes++;
2235b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2245b04c689SPavel Emelyanov 	}
2255b04c689SPavel Emelyanov }
2265b04c689SPavel Emelyanov 
22746711810SRandy Dunlap /**
22841ffe5d5SHugh Dickins  * shmem_recalc_inode - recalculate the block usage of an inode
2291da177e4SLinus Torvalds  * @inode: inode to recalc
2301da177e4SLinus Torvalds  *
2311da177e4SLinus Torvalds  * We have to calculate the free blocks since the mm can drop
2321da177e4SLinus Torvalds  * undirtied hole pages behind our back.
2331da177e4SLinus Torvalds  *
2341da177e4SLinus Torvalds  * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
2351da177e4SLinus Torvalds  * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
2361da177e4SLinus Torvalds  *
2371da177e4SLinus Torvalds  * It has to be called with the spinlock held.
2381da177e4SLinus Torvalds  */
2391da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode)
2401da177e4SLinus Torvalds {
2411da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
2421da177e4SLinus Torvalds 	long freed;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
2451da177e4SLinus Torvalds 	if (freed > 0) {
24654af6042SHugh Dickins 		struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
24754af6042SHugh Dickins 		if (sbinfo->max_blocks)
24854af6042SHugh Dickins 			percpu_counter_add(&sbinfo->used_blocks, -freed);
2491da177e4SLinus Torvalds 		info->alloced -= freed;
25054af6042SHugh Dickins 		inode->i_blocks -= freed * BLOCKS_PER_PAGE;
2511da177e4SLinus Torvalds 		shmem_unacct_blocks(info->flags, freed);
2521da177e4SLinus Torvalds 	}
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2557a5d0fbbSHugh Dickins /*
2567a5d0fbbSHugh Dickins  * Replace item expected in radix tree by a new item, while holding tree lock.
2577a5d0fbbSHugh Dickins  */
2587a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping,
2597a5d0fbbSHugh Dickins 			pgoff_t index, void *expected, void *replacement)
2607a5d0fbbSHugh Dickins {
2617a5d0fbbSHugh Dickins 	void **pslot;
2626dbaf22cSJohannes Weiner 	void *item;
2637a5d0fbbSHugh Dickins 
2647a5d0fbbSHugh Dickins 	VM_BUG_ON(!expected);
2656dbaf22cSJohannes Weiner 	VM_BUG_ON(!replacement);
2667a5d0fbbSHugh Dickins 	pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
2676dbaf22cSJohannes Weiner 	if (!pslot)
2686dbaf22cSJohannes Weiner 		return -ENOENT;
2696dbaf22cSJohannes Weiner 	item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock);
2707a5d0fbbSHugh Dickins 	if (item != expected)
2717a5d0fbbSHugh Dickins 		return -ENOENT;
2727a5d0fbbSHugh Dickins 	radix_tree_replace_slot(pslot, replacement);
2737a5d0fbbSHugh Dickins 	return 0;
2747a5d0fbbSHugh Dickins }
2757a5d0fbbSHugh Dickins 
2767a5d0fbbSHugh Dickins /*
277d1899228SHugh Dickins  * Sometimes, before we decide whether to proceed or to fail, we must check
278d1899228SHugh Dickins  * that an entry was not already brought back from swap by a racing thread.
279d1899228SHugh Dickins  *
280d1899228SHugh Dickins  * Checking page is not enough: by the time a SwapCache page is locked, it
281d1899228SHugh Dickins  * might be reused, and again be SwapCache, using the same swap as before.
282d1899228SHugh Dickins  */
283d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping,
284d1899228SHugh Dickins 			       pgoff_t index, swp_entry_t swap)
285d1899228SHugh Dickins {
286d1899228SHugh Dickins 	void *item;
287d1899228SHugh Dickins 
288d1899228SHugh Dickins 	rcu_read_lock();
289d1899228SHugh Dickins 	item = radix_tree_lookup(&mapping->page_tree, index);
290d1899228SHugh Dickins 	rcu_read_unlock();
291d1899228SHugh Dickins 	return item == swp_to_radix_entry(swap);
292d1899228SHugh Dickins }
293d1899228SHugh Dickins 
294d1899228SHugh Dickins /*
29546f65ec1SHugh Dickins  * Like add_to_page_cache_locked, but error if expected item has gone.
29646f65ec1SHugh Dickins  */
29746f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page,
29846f65ec1SHugh Dickins 				   struct address_space *mapping,
299fed400a1SWang Sheng-Hui 				   pgoff_t index, void *expected)
30046f65ec1SHugh Dickins {
301b065b432SHugh Dickins 	int error;
30246f65ec1SHugh Dickins 
303309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageLocked(page), page);
304309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
30546f65ec1SHugh Dickins 
30646f65ec1SHugh Dickins 	page_cache_get(page);
30746f65ec1SHugh Dickins 	page->mapping = mapping;
30846f65ec1SHugh Dickins 	page->index = index;
30946f65ec1SHugh Dickins 
31046f65ec1SHugh Dickins 	spin_lock_irq(&mapping->tree_lock);
31146f65ec1SHugh Dickins 	if (!expected)
312b065b432SHugh Dickins 		error = radix_tree_insert(&mapping->page_tree, index, page);
31346f65ec1SHugh Dickins 	else
314b065b432SHugh Dickins 		error = shmem_radix_tree_replace(mapping, index, expected,
315b065b432SHugh Dickins 								 page);
31646f65ec1SHugh Dickins 	if (!error) {
31746f65ec1SHugh Dickins 		mapping->nrpages++;
31846f65ec1SHugh Dickins 		__inc_zone_page_state(page, NR_FILE_PAGES);
31946f65ec1SHugh Dickins 		__inc_zone_page_state(page, NR_SHMEM);
32046f65ec1SHugh Dickins 		spin_unlock_irq(&mapping->tree_lock);
32146f65ec1SHugh Dickins 	} else {
32246f65ec1SHugh Dickins 		page->mapping = NULL;
32346f65ec1SHugh Dickins 		spin_unlock_irq(&mapping->tree_lock);
32446f65ec1SHugh Dickins 		page_cache_release(page);
32546f65ec1SHugh Dickins 	}
32646f65ec1SHugh Dickins 	return error;
32746f65ec1SHugh Dickins }
32846f65ec1SHugh Dickins 
32946f65ec1SHugh Dickins /*
3306922c0c7SHugh Dickins  * Like delete_from_page_cache, but substitutes swap for page.
3316922c0c7SHugh Dickins  */
3326922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap)
3336922c0c7SHugh Dickins {
3346922c0c7SHugh Dickins 	struct address_space *mapping = page->mapping;
3356922c0c7SHugh Dickins 	int error;
3366922c0c7SHugh Dickins 
3376922c0c7SHugh Dickins 	spin_lock_irq(&mapping->tree_lock);
3386922c0c7SHugh Dickins 	error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
3396922c0c7SHugh Dickins 	page->mapping = NULL;
3406922c0c7SHugh Dickins 	mapping->nrpages--;
3416922c0c7SHugh Dickins 	__dec_zone_page_state(page, NR_FILE_PAGES);
3426922c0c7SHugh Dickins 	__dec_zone_page_state(page, NR_SHMEM);
3436922c0c7SHugh Dickins 	spin_unlock_irq(&mapping->tree_lock);
3446922c0c7SHugh Dickins 	page_cache_release(page);
3456922c0c7SHugh Dickins 	BUG_ON(error);
3466922c0c7SHugh Dickins }
3476922c0c7SHugh Dickins 
3486922c0c7SHugh Dickins /*
3497a5d0fbbSHugh Dickins  * Remove swap entry from radix tree, free the swap and its page cache.
3507a5d0fbbSHugh Dickins  */
3517a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping,
3527a5d0fbbSHugh Dickins 			   pgoff_t index, void *radswap)
3537a5d0fbbSHugh Dickins {
3546dbaf22cSJohannes Weiner 	void *old;
3557a5d0fbbSHugh Dickins 
3567a5d0fbbSHugh Dickins 	spin_lock_irq(&mapping->tree_lock);
3576dbaf22cSJohannes Weiner 	old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
3587a5d0fbbSHugh Dickins 	spin_unlock_irq(&mapping->tree_lock);
3596dbaf22cSJohannes Weiner 	if (old != radswap)
3606dbaf22cSJohannes Weiner 		return -ENOENT;
3617a5d0fbbSHugh Dickins 	free_swap_and_cache(radix_to_swp_entry(radswap));
3626dbaf22cSJohannes Weiner 	return 0;
3637a5d0fbbSHugh Dickins }
3647a5d0fbbSHugh Dickins 
3657a5d0fbbSHugh Dickins /*
36624513264SHugh Dickins  * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
36724513264SHugh Dickins  */
36824513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping)
36924513264SHugh Dickins {
37024513264SHugh Dickins 	struct pagevec pvec;
37124513264SHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
37224513264SHugh Dickins 	pgoff_t index = 0;
37324513264SHugh Dickins 
37424513264SHugh Dickins 	pagevec_init(&pvec, 0);
37524513264SHugh Dickins 	/*
37624513264SHugh Dickins 	 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
37724513264SHugh Dickins 	 */
37824513264SHugh Dickins 	while (!mapping_unevictable(mapping)) {
37924513264SHugh Dickins 		/*
38024513264SHugh Dickins 		 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
38124513264SHugh Dickins 		 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
38224513264SHugh Dickins 		 */
3830cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
38424513264SHugh Dickins 					   PAGEVEC_SIZE, pvec.pages, indices);
38524513264SHugh Dickins 		if (!pvec.nr)
38624513264SHugh Dickins 			break;
38724513264SHugh Dickins 		index = indices[pvec.nr - 1] + 1;
3880cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
38924513264SHugh Dickins 		check_move_unevictable_pages(pvec.pages, pvec.nr);
39024513264SHugh Dickins 		pagevec_release(&pvec);
39124513264SHugh Dickins 		cond_resched();
39224513264SHugh Dickins 	}
3937a5d0fbbSHugh Dickins }
3947a5d0fbbSHugh Dickins 
3957a5d0fbbSHugh Dickins /*
3967a5d0fbbSHugh Dickins  * Remove range of pages and swap entries from radix tree, and free them.
3971635f6a7SHugh Dickins  * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
3987a5d0fbbSHugh Dickins  */
3991635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
4001635f6a7SHugh Dickins 								 bool unfalloc)
4011da177e4SLinus Torvalds {
402285b2c4fSHugh Dickins 	struct address_space *mapping = inode->i_mapping;
4031da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
404285b2c4fSHugh Dickins 	pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
40583e4fa9cSHugh Dickins 	pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
40683e4fa9cSHugh Dickins 	unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
40783e4fa9cSHugh Dickins 	unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
408bda97eabSHugh Dickins 	struct pagevec pvec;
4097a5d0fbbSHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
4107a5d0fbbSHugh Dickins 	long nr_swaps_freed = 0;
411285b2c4fSHugh Dickins 	pgoff_t index;
412bda97eabSHugh Dickins 	int i;
4131da177e4SLinus Torvalds 
41483e4fa9cSHugh Dickins 	if (lend == -1)
41583e4fa9cSHugh Dickins 		end = -1;	/* unsigned, so actually very big */
416bda97eabSHugh Dickins 
417bda97eabSHugh Dickins 	pagevec_init(&pvec, 0);
418bda97eabSHugh Dickins 	index = start;
41983e4fa9cSHugh Dickins 	while (index < end) {
4200cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
42183e4fa9cSHugh Dickins 			min(end - index, (pgoff_t)PAGEVEC_SIZE),
4227a5d0fbbSHugh Dickins 			pvec.pages, indices);
4237a5d0fbbSHugh Dickins 		if (!pvec.nr)
4247a5d0fbbSHugh Dickins 			break;
425bda97eabSHugh Dickins 		for (i = 0; i < pagevec_count(&pvec); i++) {
426bda97eabSHugh Dickins 			struct page *page = pvec.pages[i];
427bda97eabSHugh Dickins 
4287a5d0fbbSHugh Dickins 			index = indices[i];
42983e4fa9cSHugh Dickins 			if (index >= end)
430bda97eabSHugh Dickins 				break;
431bda97eabSHugh Dickins 
4327a5d0fbbSHugh Dickins 			if (radix_tree_exceptional_entry(page)) {
4331635f6a7SHugh Dickins 				if (unfalloc)
4341635f6a7SHugh Dickins 					continue;
4357a5d0fbbSHugh Dickins 				nr_swaps_freed += !shmem_free_swap(mapping,
4367a5d0fbbSHugh Dickins 								index, page);
4377a5d0fbbSHugh Dickins 				continue;
4387a5d0fbbSHugh Dickins 			}
4397a5d0fbbSHugh Dickins 
440bda97eabSHugh Dickins 			if (!trylock_page(page))
441bda97eabSHugh Dickins 				continue;
4421635f6a7SHugh Dickins 			if (!unfalloc || !PageUptodate(page)) {
4437a5d0fbbSHugh Dickins 				if (page->mapping == mapping) {
444309381feSSasha Levin 					VM_BUG_ON_PAGE(PageWriteback(page), page);
445bda97eabSHugh Dickins 					truncate_inode_page(mapping, page);
4467a5d0fbbSHugh Dickins 				}
4471635f6a7SHugh Dickins 			}
448bda97eabSHugh Dickins 			unlock_page(page);
449bda97eabSHugh Dickins 		}
4500cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
45124513264SHugh Dickins 		pagevec_release(&pvec);
452bda97eabSHugh Dickins 		cond_resched();
453bda97eabSHugh Dickins 		index++;
454bda97eabSHugh Dickins 	}
455bda97eabSHugh Dickins 
45683e4fa9cSHugh Dickins 	if (partial_start) {
457bda97eabSHugh Dickins 		struct page *page = NULL;
458bda97eabSHugh Dickins 		shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
459bda97eabSHugh Dickins 		if (page) {
46083e4fa9cSHugh Dickins 			unsigned int top = PAGE_CACHE_SIZE;
46183e4fa9cSHugh Dickins 			if (start > end) {
46283e4fa9cSHugh Dickins 				top = partial_end;
46383e4fa9cSHugh Dickins 				partial_end = 0;
46483e4fa9cSHugh Dickins 			}
46583e4fa9cSHugh Dickins 			zero_user_segment(page, partial_start, top);
466bda97eabSHugh Dickins 			set_page_dirty(page);
467bda97eabSHugh Dickins 			unlock_page(page);
468bda97eabSHugh Dickins 			page_cache_release(page);
469bda97eabSHugh Dickins 		}
470bda97eabSHugh Dickins 	}
47183e4fa9cSHugh Dickins 	if (partial_end) {
47283e4fa9cSHugh Dickins 		struct page *page = NULL;
47383e4fa9cSHugh Dickins 		shmem_getpage(inode, end, &page, SGP_READ, NULL);
47483e4fa9cSHugh Dickins 		if (page) {
47583e4fa9cSHugh Dickins 			zero_user_segment(page, 0, partial_end);
47683e4fa9cSHugh Dickins 			set_page_dirty(page);
47783e4fa9cSHugh Dickins 			unlock_page(page);
47883e4fa9cSHugh Dickins 			page_cache_release(page);
47983e4fa9cSHugh Dickins 		}
48083e4fa9cSHugh Dickins 	}
48183e4fa9cSHugh Dickins 	if (start >= end)
48283e4fa9cSHugh Dickins 		return;
483bda97eabSHugh Dickins 
484bda97eabSHugh Dickins 	index = start;
485b1a36650SHugh Dickins 	while (index < end) {
486bda97eabSHugh Dickins 		cond_resched();
4870cd6144aSJohannes Weiner 
4880cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
48983e4fa9cSHugh Dickins 				min(end - index, (pgoff_t)PAGEVEC_SIZE),
4907a5d0fbbSHugh Dickins 				pvec.pages, indices);
4917a5d0fbbSHugh Dickins 		if (!pvec.nr) {
492b1a36650SHugh Dickins 			/* If all gone or hole-punch or unfalloc, we're done */
493b1a36650SHugh Dickins 			if (index == start || end != -1)
494bda97eabSHugh Dickins 				break;
495b1a36650SHugh Dickins 			/* But if truncating, restart to make sure all gone */
496bda97eabSHugh Dickins 			index = start;
497bda97eabSHugh Dickins 			continue;
498bda97eabSHugh Dickins 		}
499bda97eabSHugh Dickins 		for (i = 0; i < pagevec_count(&pvec); i++) {
500bda97eabSHugh Dickins 			struct page *page = pvec.pages[i];
501bda97eabSHugh Dickins 
5027a5d0fbbSHugh Dickins 			index = indices[i];
50383e4fa9cSHugh Dickins 			if (index >= end)
504bda97eabSHugh Dickins 				break;
505bda97eabSHugh Dickins 
5067a5d0fbbSHugh Dickins 			if (radix_tree_exceptional_entry(page)) {
5071635f6a7SHugh Dickins 				if (unfalloc)
5081635f6a7SHugh Dickins 					continue;
509b1a36650SHugh Dickins 				if (shmem_free_swap(mapping, index, page)) {
510b1a36650SHugh Dickins 					/* Swap was replaced by page: retry */
511b1a36650SHugh Dickins 					index--;
512b1a36650SHugh Dickins 					break;
513b1a36650SHugh Dickins 				}
514b1a36650SHugh Dickins 				nr_swaps_freed++;
5157a5d0fbbSHugh Dickins 				continue;
5167a5d0fbbSHugh Dickins 			}
5177a5d0fbbSHugh Dickins 
518bda97eabSHugh Dickins 			lock_page(page);
5191635f6a7SHugh Dickins 			if (!unfalloc || !PageUptodate(page)) {
5207a5d0fbbSHugh Dickins 				if (page->mapping == mapping) {
521309381feSSasha Levin 					VM_BUG_ON_PAGE(PageWriteback(page), page);
522bda97eabSHugh Dickins 					truncate_inode_page(mapping, page);
523b1a36650SHugh Dickins 				} else {
524b1a36650SHugh Dickins 					/* Page was replaced by swap: retry */
525b1a36650SHugh Dickins 					unlock_page(page);
526b1a36650SHugh Dickins 					index--;
527b1a36650SHugh Dickins 					break;
5287a5d0fbbSHugh Dickins 				}
5291635f6a7SHugh Dickins 			}
530bda97eabSHugh Dickins 			unlock_page(page);
531bda97eabSHugh Dickins 		}
5320cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
53324513264SHugh Dickins 		pagevec_release(&pvec);
534bda97eabSHugh Dickins 		index++;
535bda97eabSHugh Dickins 	}
53694c1e62dSHugh Dickins 
5371da177e4SLinus Torvalds 	spin_lock(&info->lock);
5387a5d0fbbSHugh Dickins 	info->swapped -= nr_swaps_freed;
5391da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
5401da177e4SLinus Torvalds 	spin_unlock(&info->lock);
5411635f6a7SHugh Dickins }
5421da177e4SLinus Torvalds 
5431635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
5441635f6a7SHugh Dickins {
5451635f6a7SHugh Dickins 	shmem_undo_range(inode, lstart, lend, false);
546285b2c4fSHugh Dickins 	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
5471da177e4SLinus Torvalds }
54894c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
5491da177e4SLinus Torvalds 
55094c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
5511da177e4SLinus Torvalds {
5521da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
55340e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
5541da177e4SLinus Torvalds 	int error;
5551da177e4SLinus Torvalds 
556db78b877SChristoph Hellwig 	error = inode_change_ok(inode, attr);
557db78b877SChristoph Hellwig 	if (error)
558db78b877SChristoph Hellwig 		return error;
559db78b877SChristoph Hellwig 
56094c1e62dSHugh Dickins 	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
56194c1e62dSHugh Dickins 		loff_t oldsize = inode->i_size;
56294c1e62dSHugh Dickins 		loff_t newsize = attr->ia_size;
5633889e6e7Snpiggin@suse.de 
56440e041a2SDavid Herrmann 		/* protected by i_mutex */
56540e041a2SDavid Herrmann 		if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
56640e041a2SDavid Herrmann 		    (newsize > oldsize && (info->seals & F_SEAL_GROW)))
56740e041a2SDavid Herrmann 			return -EPERM;
56840e041a2SDavid Herrmann 
56994c1e62dSHugh Dickins 		if (newsize != oldsize) {
57077142517SKonstantin Khlebnikov 			error = shmem_reacct_size(SHMEM_I(inode)->flags,
57177142517SKonstantin Khlebnikov 					oldsize, newsize);
57277142517SKonstantin Khlebnikov 			if (error)
57377142517SKonstantin Khlebnikov 				return error;
57494c1e62dSHugh Dickins 			i_size_write(inode, newsize);
57594c1e62dSHugh Dickins 			inode->i_ctime = inode->i_mtime = CURRENT_TIME;
57694c1e62dSHugh Dickins 		}
57794c1e62dSHugh Dickins 		if (newsize < oldsize) {
57894c1e62dSHugh Dickins 			loff_t holebegin = round_up(newsize, PAGE_SIZE);
57994c1e62dSHugh Dickins 			unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
58094c1e62dSHugh Dickins 			shmem_truncate_range(inode, newsize, (loff_t)-1);
58194c1e62dSHugh Dickins 			/* unmap again to remove racily COWed private pages */
58294c1e62dSHugh Dickins 			unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
58394c1e62dSHugh Dickins 		}
5841da177e4SLinus Torvalds 	}
5851da177e4SLinus Torvalds 
5866a1a90adSChristoph Hellwig 	setattr_copy(inode, attr);
587db78b877SChristoph Hellwig 	if (attr->ia_valid & ATTR_MODE)
588feda821eSChristoph Hellwig 		error = posix_acl_chmod(inode, inode->i_mode);
5891da177e4SLinus Torvalds 	return error;
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds 
5921f895f75SAl Viro static void shmem_evict_inode(struct inode *inode)
5931da177e4SLinus Torvalds {
5941da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
5951da177e4SLinus Torvalds 
5963889e6e7Snpiggin@suse.de 	if (inode->i_mapping->a_ops == &shmem_aops) {
5971da177e4SLinus Torvalds 		shmem_unacct_size(info->flags, inode->i_size);
5981da177e4SLinus Torvalds 		inode->i_size = 0;
5993889e6e7Snpiggin@suse.de 		shmem_truncate_range(inode, 0, (loff_t)-1);
6001da177e4SLinus Torvalds 		if (!list_empty(&info->swaplist)) {
601cb5f7b9aSHugh Dickins 			mutex_lock(&shmem_swaplist_mutex);
6021da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
603cb5f7b9aSHugh Dickins 			mutex_unlock(&shmem_swaplist_mutex);
6041da177e4SLinus Torvalds 		}
60569f07ec9SHugh Dickins 	} else
60669f07ec9SHugh Dickins 		kfree(info->symlink);
607b09e0fa4SEric Paris 
60838f38657SAristeu Rozanski 	simple_xattrs_free(&info->xattrs);
6090f3c42f5SHugh Dickins 	WARN_ON(inode->i_blocks);
6105b04c689SPavel Emelyanov 	shmem_free_inode(inode->i_sb);
611dbd5768fSJan Kara 	clear_inode(inode);
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
61446f65ec1SHugh Dickins /*
61546f65ec1SHugh Dickins  * If swap found in inode, free it and move page from swapcache to filecache.
61646f65ec1SHugh Dickins  */
61741ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info,
618bde05d1cSHugh Dickins 			     swp_entry_t swap, struct page **pagep)
6191da177e4SLinus Torvalds {
620285b2c4fSHugh Dickins 	struct address_space *mapping = info->vfs_inode.i_mapping;
62146f65ec1SHugh Dickins 	void *radswap;
62241ffe5d5SHugh Dickins 	pgoff_t index;
623bde05d1cSHugh Dickins 	gfp_t gfp;
624bde05d1cSHugh Dickins 	int error = 0;
6251da177e4SLinus Torvalds 
62646f65ec1SHugh Dickins 	radswap = swp_to_radix_entry(swap);
627e504f3fdSHugh Dickins 	index = radix_tree_locate_item(&mapping->page_tree, radswap);
62846f65ec1SHugh Dickins 	if (index == -1)
62900501b53SJohannes Weiner 		return -EAGAIN;	/* tell shmem_unuse we found nothing */
6302e0e26c7SHugh Dickins 
6311b1b32f2SHugh Dickins 	/*
6321b1b32f2SHugh Dickins 	 * Move _head_ to start search for next from here.
6331f895f75SAl Viro 	 * But be careful: shmem_evict_inode checks list_empty without taking
6341b1b32f2SHugh Dickins 	 * mutex, and there's an instant in list_move_tail when info->swaplist
635285b2c4fSHugh Dickins 	 * would appear empty, if it were the only one on shmem_swaplist.
6361b1b32f2SHugh Dickins 	 */
6371b1b32f2SHugh Dickins 	if (shmem_swaplist.next != &info->swaplist)
6382e0e26c7SHugh Dickins 		list_move_tail(&shmem_swaplist, &info->swaplist);
6392e0e26c7SHugh Dickins 
640bde05d1cSHugh Dickins 	gfp = mapping_gfp_mask(mapping);
641bde05d1cSHugh Dickins 	if (shmem_should_replace_page(*pagep, gfp)) {
642bde05d1cSHugh Dickins 		mutex_unlock(&shmem_swaplist_mutex);
643bde05d1cSHugh Dickins 		error = shmem_replace_page(pagep, gfp, info, index);
644bde05d1cSHugh Dickins 		mutex_lock(&shmem_swaplist_mutex);
645bde05d1cSHugh Dickins 		/*
646bde05d1cSHugh Dickins 		 * We needed to drop mutex to make that restrictive page
6470142ef6cSHugh Dickins 		 * allocation, but the inode might have been freed while we
6480142ef6cSHugh Dickins 		 * dropped it: although a racing shmem_evict_inode() cannot
6490142ef6cSHugh Dickins 		 * complete without emptying the radix_tree, our page lock
6500142ef6cSHugh Dickins 		 * on this swapcache page is not enough to prevent that -
6510142ef6cSHugh Dickins 		 * free_swap_and_cache() of our swap entry will only
6520142ef6cSHugh Dickins 		 * trylock_page(), removing swap from radix_tree whatever.
6530142ef6cSHugh Dickins 		 *
6540142ef6cSHugh Dickins 		 * We must not proceed to shmem_add_to_page_cache() if the
6550142ef6cSHugh Dickins 		 * inode has been freed, but of course we cannot rely on
6560142ef6cSHugh Dickins 		 * inode or mapping or info to check that.  However, we can
6570142ef6cSHugh Dickins 		 * safely check if our swap entry is still in use (and here
6580142ef6cSHugh Dickins 		 * it can't have got reused for another page): if it's still
6590142ef6cSHugh Dickins 		 * in use, then the inode cannot have been freed yet, and we
6600142ef6cSHugh Dickins 		 * can safely proceed (if it's no longer in use, that tells
6610142ef6cSHugh Dickins 		 * nothing about the inode, but we don't need to unuse swap).
662bde05d1cSHugh Dickins 		 */
663bde05d1cSHugh Dickins 		if (!page_swapcount(*pagep))
664bde05d1cSHugh Dickins 			error = -ENOENT;
665bde05d1cSHugh Dickins 	}
666bde05d1cSHugh Dickins 
667d13d1443SKAMEZAWA Hiroyuki 	/*
668778dd893SHugh Dickins 	 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
669778dd893SHugh Dickins 	 * but also to hold up shmem_evict_inode(): so inode cannot be freed
670778dd893SHugh Dickins 	 * beneath us (pagelock doesn't help until the page is in pagecache).
671d13d1443SKAMEZAWA Hiroyuki 	 */
672bde05d1cSHugh Dickins 	if (!error)
673bde05d1cSHugh Dickins 		error = shmem_add_to_page_cache(*pagep, mapping, index,
674fed400a1SWang Sheng-Hui 						radswap);
67548f170fbSHugh Dickins 	if (error != -ENOMEM) {
67646f65ec1SHugh Dickins 		/*
67746f65ec1SHugh Dickins 		 * Truncation and eviction use free_swap_and_cache(), which
67846f65ec1SHugh Dickins 		 * only does trylock page: if we raced, best clean up here.
67946f65ec1SHugh Dickins 		 */
680bde05d1cSHugh Dickins 		delete_from_swap_cache(*pagep);
681bde05d1cSHugh Dickins 		set_page_dirty(*pagep);
68246f65ec1SHugh Dickins 		if (!error) {
68346f65ec1SHugh Dickins 			spin_lock(&info->lock);
684285b2c4fSHugh Dickins 			info->swapped--;
68546f65ec1SHugh Dickins 			spin_unlock(&info->lock);
68641ffe5d5SHugh Dickins 			swap_free(swap);
68746f65ec1SHugh Dickins 		}
6881da177e4SLinus Torvalds 	}
6892e0e26c7SHugh Dickins 	return error;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds /*
69346f65ec1SHugh Dickins  * Search through swapped inodes to find and replace swap by page.
6941da177e4SLinus Torvalds  */
69541ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page)
6961da177e4SLinus Torvalds {
69741ffe5d5SHugh Dickins 	struct list_head *this, *next;
6981da177e4SLinus Torvalds 	struct shmem_inode_info *info;
69900501b53SJohannes Weiner 	struct mem_cgroup *memcg;
700bde05d1cSHugh Dickins 	int error = 0;
701bde05d1cSHugh Dickins 
702bde05d1cSHugh Dickins 	/*
703bde05d1cSHugh Dickins 	 * There's a faint possibility that swap page was replaced before
7040142ef6cSHugh Dickins 	 * caller locked it: caller will come back later with the right page.
705bde05d1cSHugh Dickins 	 */
7060142ef6cSHugh Dickins 	if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
707bde05d1cSHugh Dickins 		goto out;
708778dd893SHugh Dickins 
709778dd893SHugh Dickins 	/*
710778dd893SHugh Dickins 	 * Charge page using GFP_KERNEL while we can wait, before taking
711778dd893SHugh Dickins 	 * the shmem_swaplist_mutex which might hold up shmem_writepage().
712778dd893SHugh Dickins 	 * Charged back to the user (not to caller) when swap account is used.
713778dd893SHugh Dickins 	 */
71400501b53SJohannes Weiner 	error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg);
715778dd893SHugh Dickins 	if (error)
716778dd893SHugh Dickins 		goto out;
71746f65ec1SHugh Dickins 	/* No radix_tree_preload: swap entry keeps a place for page in tree */
71800501b53SJohannes Weiner 	error = -EAGAIN;
7191da177e4SLinus Torvalds 
720cb5f7b9aSHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
72141ffe5d5SHugh Dickins 	list_for_each_safe(this, next, &shmem_swaplist) {
72241ffe5d5SHugh Dickins 		info = list_entry(this, struct shmem_inode_info, swaplist);
723285b2c4fSHugh Dickins 		if (info->swapped)
72400501b53SJohannes Weiner 			error = shmem_unuse_inode(info, swap, &page);
7256922c0c7SHugh Dickins 		else
7266922c0c7SHugh Dickins 			list_del_init(&info->swaplist);
727cb5f7b9aSHugh Dickins 		cond_resched();
72800501b53SJohannes Weiner 		if (error != -EAGAIN)
729778dd893SHugh Dickins 			break;
73000501b53SJohannes Weiner 		/* found nothing in this: move on to search the next */
7311da177e4SLinus Torvalds 	}
732cb5f7b9aSHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
733778dd893SHugh Dickins 
73400501b53SJohannes Weiner 	if (error) {
73500501b53SJohannes Weiner 		if (error != -ENOMEM)
73600501b53SJohannes Weiner 			error = 0;
73700501b53SJohannes Weiner 		mem_cgroup_cancel_charge(page, memcg);
73800501b53SJohannes Weiner 	} else
73900501b53SJohannes Weiner 		mem_cgroup_commit_charge(page, memcg, true);
740778dd893SHugh Dickins out:
741aaa46865SHugh Dickins 	unlock_page(page);
742aaa46865SHugh Dickins 	page_cache_release(page);
743778dd893SHugh Dickins 	return error;
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds /*
7471da177e4SLinus Torvalds  * Move the page from the page cache to the swap cache.
7481da177e4SLinus Torvalds  */
7491da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	struct shmem_inode_info *info;
7521da177e4SLinus Torvalds 	struct address_space *mapping;
7531da177e4SLinus Torvalds 	struct inode *inode;
7546922c0c7SHugh Dickins 	swp_entry_t swap;
7556922c0c7SHugh Dickins 	pgoff_t index;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
7581da177e4SLinus Torvalds 	mapping = page->mapping;
7591da177e4SLinus Torvalds 	index = page->index;
7601da177e4SLinus Torvalds 	inode = mapping->host;
7611da177e4SLinus Torvalds 	info = SHMEM_I(inode);
7621da177e4SLinus Torvalds 	if (info->flags & VM_LOCKED)
7631da177e4SLinus Torvalds 		goto redirty;
764d9fe526aSHugh Dickins 	if (!total_swap_pages)
7651da177e4SLinus Torvalds 		goto redirty;
7661da177e4SLinus Torvalds 
767d9fe526aSHugh Dickins 	/*
768d9fe526aSHugh Dickins 	 * shmem_backing_dev_info's capabilities prevent regular writeback or
769d9fe526aSHugh Dickins 	 * sync from ever calling shmem_writepage; but a stacking filesystem
77048f170fbSHugh Dickins 	 * might use ->writepage of its underlying filesystem, in which case
771d9fe526aSHugh Dickins 	 * tmpfs should write out to swap only in response to memory pressure,
77248f170fbSHugh Dickins 	 * and not for the writeback threads or sync.
773d9fe526aSHugh Dickins 	 */
77448f170fbSHugh Dickins 	if (!wbc->for_reclaim) {
77548f170fbSHugh Dickins 		WARN_ON_ONCE(1);	/* Still happens? Tell us about it! */
77648f170fbSHugh Dickins 		goto redirty;
77748f170fbSHugh Dickins 	}
7781635f6a7SHugh Dickins 
7791635f6a7SHugh Dickins 	/*
7801635f6a7SHugh Dickins 	 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
7811635f6a7SHugh Dickins 	 * value into swapfile.c, the only way we can correctly account for a
7821635f6a7SHugh Dickins 	 * fallocated page arriving here is now to initialize it and write it.
7831aac1400SHugh Dickins 	 *
7841aac1400SHugh Dickins 	 * That's okay for a page already fallocated earlier, but if we have
7851aac1400SHugh Dickins 	 * not yet completed the fallocation, then (a) we want to keep track
7861aac1400SHugh Dickins 	 * of this page in case we have to undo it, and (b) it may not be a
7871aac1400SHugh Dickins 	 * good idea to continue anyway, once we're pushing into swap.  So
7881aac1400SHugh Dickins 	 * reactivate the page, and let shmem_fallocate() quit when too many.
7891635f6a7SHugh Dickins 	 */
7901635f6a7SHugh Dickins 	if (!PageUptodate(page)) {
7911aac1400SHugh Dickins 		if (inode->i_private) {
7921aac1400SHugh Dickins 			struct shmem_falloc *shmem_falloc;
7931aac1400SHugh Dickins 			spin_lock(&inode->i_lock);
7941aac1400SHugh Dickins 			shmem_falloc = inode->i_private;
7951aac1400SHugh Dickins 			if (shmem_falloc &&
7968e205f77SHugh Dickins 			    !shmem_falloc->waitq &&
7971aac1400SHugh Dickins 			    index >= shmem_falloc->start &&
7981aac1400SHugh Dickins 			    index < shmem_falloc->next)
7991aac1400SHugh Dickins 				shmem_falloc->nr_unswapped++;
8001aac1400SHugh Dickins 			else
8011aac1400SHugh Dickins 				shmem_falloc = NULL;
8021aac1400SHugh Dickins 			spin_unlock(&inode->i_lock);
8031aac1400SHugh Dickins 			if (shmem_falloc)
8041aac1400SHugh Dickins 				goto redirty;
8051aac1400SHugh Dickins 		}
8061635f6a7SHugh Dickins 		clear_highpage(page);
8071635f6a7SHugh Dickins 		flush_dcache_page(page);
8081635f6a7SHugh Dickins 		SetPageUptodate(page);
8091635f6a7SHugh Dickins 	}
8101635f6a7SHugh Dickins 
811d9fe526aSHugh Dickins 	swap = get_swap_page();
81248f170fbSHugh Dickins 	if (!swap.val)
81348f170fbSHugh Dickins 		goto redirty;
814d9fe526aSHugh Dickins 
815b1dea800SHugh Dickins 	/*
816b1dea800SHugh Dickins 	 * Add inode to shmem_unuse()'s list of swapped-out inodes,
8176922c0c7SHugh Dickins 	 * if it's not already there.  Do it now before the page is
8186922c0c7SHugh Dickins 	 * moved to swap cache, when its pagelock no longer protects
819b1dea800SHugh Dickins 	 * the inode from eviction.  But don't unlock the mutex until
8206922c0c7SHugh Dickins 	 * we've incremented swapped, because shmem_unuse_inode() will
8216922c0c7SHugh Dickins 	 * prune a !swapped inode from the swaplist under this mutex.
822b1dea800SHugh Dickins 	 */
823b1dea800SHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
82405bf86b4SHugh Dickins 	if (list_empty(&info->swaplist))
82505bf86b4SHugh Dickins 		list_add_tail(&info->swaplist, &shmem_swaplist);
826b1dea800SHugh Dickins 
82748f170fbSHugh Dickins 	if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
828aaa46865SHugh Dickins 		swap_shmem_alloc(swap);
8296922c0c7SHugh Dickins 		shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
8306922c0c7SHugh Dickins 
8316922c0c7SHugh Dickins 		spin_lock(&info->lock);
8326922c0c7SHugh Dickins 		info->swapped++;
8336922c0c7SHugh Dickins 		shmem_recalc_inode(inode);
834826267cfSHugh Dickins 		spin_unlock(&info->lock);
8356922c0c7SHugh Dickins 
8366922c0c7SHugh Dickins 		mutex_unlock(&shmem_swaplist_mutex);
837d9fe526aSHugh Dickins 		BUG_ON(page_mapped(page));
8389fab5619SHugh Dickins 		swap_writepage(page, wbc);
8391da177e4SLinus Torvalds 		return 0;
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 
8426922c0c7SHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
8430a31bc97SJohannes Weiner 	swapcache_free(swap);
8441da177e4SLinus Torvalds redirty:
8451da177e4SLinus Torvalds 	set_page_dirty(page);
846d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
847d9fe526aSHugh Dickins 		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
848d9fe526aSHugh Dickins 	unlock_page(page);
849d9fe526aSHugh Dickins 	return 0;
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds #ifdef CONFIG_NUMA
853680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
85471fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
855680d794bSakpm@linux-foundation.org {
856680d794bSakpm@linux-foundation.org 	char buffer[64];
857680d794bSakpm@linux-foundation.org 
85871fe804bSLee Schermerhorn 	if (!mpol || mpol->mode == MPOL_DEFAULT)
859095f1fc4SLee Schermerhorn 		return;		/* show nothing */
860095f1fc4SLee Schermerhorn 
861a7a88b23SHugh Dickins 	mpol_to_str(buffer, sizeof(buffer), mpol);
862095f1fc4SLee Schermerhorn 
863095f1fc4SLee Schermerhorn 	seq_printf(seq, ",mpol=%s", buffer);
864680d794bSakpm@linux-foundation.org }
86571fe804bSLee Schermerhorn 
86671fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
86771fe804bSLee Schermerhorn {
86871fe804bSLee Schermerhorn 	struct mempolicy *mpol = NULL;
86971fe804bSLee Schermerhorn 	if (sbinfo->mpol) {
87071fe804bSLee Schermerhorn 		spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
87171fe804bSLee Schermerhorn 		mpol = sbinfo->mpol;
87271fe804bSLee Schermerhorn 		mpol_get(mpol);
87371fe804bSLee Schermerhorn 		spin_unlock(&sbinfo->stat_lock);
87471fe804bSLee Schermerhorn 	}
87571fe804bSLee Schermerhorn 	return mpol;
87671fe804bSLee Schermerhorn }
877680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
878680d794bSakpm@linux-foundation.org 
87941ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
88041ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds 	struct vm_area_struct pvma;
88318a2f371SMel Gorman 	struct page *page;
8841da177e4SLinus Torvalds 
885c4cc6d07SHugh Dickins 	/* Create a pseudo vma that just contains the policy */
886c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
88709c231cbSNathan Zimmer 	/* Bias interleave by inode number to distribute better across nodes */
88809c231cbSNathan Zimmer 	pvma.vm_pgoff = index + info->vfs_inode.i_ino;
889c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
89041ffe5d5SHugh Dickins 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
89152cd3b07SLee Schermerhorn 
89218a2f371SMel Gorman 	page = swapin_readahead(swap, gfp, &pvma, 0);
89318a2f371SMel Gorman 
89418a2f371SMel Gorman 	/* Drop reference taken by mpol_shared_policy_lookup() */
89518a2f371SMel Gorman 	mpol_cond_put(pvma.vm_policy);
89618a2f371SMel Gorman 
89718a2f371SMel Gorman 	return page;
89818a2f371SMel Gorman }
89918a2f371SMel Gorman 
90018a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp,
90118a2f371SMel Gorman 			struct shmem_inode_info *info, pgoff_t index)
90218a2f371SMel Gorman {
90318a2f371SMel Gorman 	struct vm_area_struct pvma;
90418a2f371SMel Gorman 	struct page *page;
90518a2f371SMel Gorman 
90618a2f371SMel Gorman 	/* Create a pseudo vma that just contains the policy */
90718a2f371SMel Gorman 	pvma.vm_start = 0;
90818a2f371SMel Gorman 	/* Bias interleave by inode number to distribute better across nodes */
90918a2f371SMel Gorman 	pvma.vm_pgoff = index + info->vfs_inode.i_ino;
91018a2f371SMel Gorman 	pvma.vm_ops = NULL;
91118a2f371SMel Gorman 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
91218a2f371SMel Gorman 
91318a2f371SMel Gorman 	page = alloc_page_vma(gfp, &pvma, 0);
91418a2f371SMel Gorman 
91518a2f371SMel Gorman 	/* Drop reference taken by mpol_shared_policy_lookup() */
91618a2f371SMel Gorman 	mpol_cond_put(pvma.vm_policy);
91718a2f371SMel Gorman 
91818a2f371SMel Gorman 	return page;
9191da177e4SLinus Torvalds }
920680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */
921680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
92241ffe5d5SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
923680d794bSakpm@linux-foundation.org {
924680d794bSakpm@linux-foundation.org }
925680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
926680d794bSakpm@linux-foundation.org 
92741ffe5d5SHugh Dickins static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
92841ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
9291da177e4SLinus Torvalds {
93041ffe5d5SHugh Dickins 	return swapin_readahead(swap, gfp, NULL, 0);
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
93302098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp,
93441ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
9351da177e4SLinus Torvalds {
936e84e2e13SHugh Dickins 	return alloc_page(gfp);
9371da177e4SLinus Torvalds }
938680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */
9391da177e4SLinus Torvalds 
94071fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
94171fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
94271fe804bSLee Schermerhorn {
94371fe804bSLee Schermerhorn 	return NULL;
94471fe804bSLee Schermerhorn }
94571fe804bSLee Schermerhorn #endif
94671fe804bSLee Schermerhorn 
9471da177e4SLinus Torvalds /*
948bde05d1cSHugh Dickins  * When a page is moved from swapcache to shmem filecache (either by the
949bde05d1cSHugh Dickins  * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
950bde05d1cSHugh Dickins  * shmem_unuse_inode()), it may have been read in earlier from swap, in
951bde05d1cSHugh Dickins  * ignorance of the mapping it belongs to.  If that mapping has special
952bde05d1cSHugh Dickins  * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
953bde05d1cSHugh Dickins  * we may need to copy to a suitable page before moving to filecache.
954bde05d1cSHugh Dickins  *
955bde05d1cSHugh Dickins  * In a future release, this may well be extended to respect cpuset and
956bde05d1cSHugh Dickins  * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
957bde05d1cSHugh Dickins  * but for now it is a simple matter of zone.
958bde05d1cSHugh Dickins  */
959bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
960bde05d1cSHugh Dickins {
961bde05d1cSHugh Dickins 	return page_zonenum(page) > gfp_zone(gfp);
962bde05d1cSHugh Dickins }
963bde05d1cSHugh Dickins 
964bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp,
965bde05d1cSHugh Dickins 				struct shmem_inode_info *info, pgoff_t index)
966bde05d1cSHugh Dickins {
967bde05d1cSHugh Dickins 	struct page *oldpage, *newpage;
968bde05d1cSHugh Dickins 	struct address_space *swap_mapping;
969bde05d1cSHugh Dickins 	pgoff_t swap_index;
970bde05d1cSHugh Dickins 	int error;
971bde05d1cSHugh Dickins 
972bde05d1cSHugh Dickins 	oldpage = *pagep;
973bde05d1cSHugh Dickins 	swap_index = page_private(oldpage);
974bde05d1cSHugh Dickins 	swap_mapping = page_mapping(oldpage);
975bde05d1cSHugh Dickins 
976bde05d1cSHugh Dickins 	/*
977bde05d1cSHugh Dickins 	 * We have arrived here because our zones are constrained, so don't
978bde05d1cSHugh Dickins 	 * limit chance of success by further cpuset and node constraints.
979bde05d1cSHugh Dickins 	 */
980bde05d1cSHugh Dickins 	gfp &= ~GFP_CONSTRAINT_MASK;
981bde05d1cSHugh Dickins 	newpage = shmem_alloc_page(gfp, info, index);
982bde05d1cSHugh Dickins 	if (!newpage)
983bde05d1cSHugh Dickins 		return -ENOMEM;
984bde05d1cSHugh Dickins 
985bde05d1cSHugh Dickins 	page_cache_get(newpage);
986bde05d1cSHugh Dickins 	copy_highpage(newpage, oldpage);
9870142ef6cSHugh Dickins 	flush_dcache_page(newpage);
988bde05d1cSHugh Dickins 
989bde05d1cSHugh Dickins 	__set_page_locked(newpage);
990bde05d1cSHugh Dickins 	SetPageUptodate(newpage);
991bde05d1cSHugh Dickins 	SetPageSwapBacked(newpage);
992bde05d1cSHugh Dickins 	set_page_private(newpage, swap_index);
993bde05d1cSHugh Dickins 	SetPageSwapCache(newpage);
994bde05d1cSHugh Dickins 
995bde05d1cSHugh Dickins 	/*
996bde05d1cSHugh Dickins 	 * Our caller will very soon move newpage out of swapcache, but it's
997bde05d1cSHugh Dickins 	 * a nice clean interface for us to replace oldpage by newpage there.
998bde05d1cSHugh Dickins 	 */
999bde05d1cSHugh Dickins 	spin_lock_irq(&swap_mapping->tree_lock);
1000bde05d1cSHugh Dickins 	error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
1001bde05d1cSHugh Dickins 								   newpage);
10020142ef6cSHugh Dickins 	if (!error) {
1003bde05d1cSHugh Dickins 		__inc_zone_page_state(newpage, NR_FILE_PAGES);
1004bde05d1cSHugh Dickins 		__dec_zone_page_state(oldpage, NR_FILE_PAGES);
10050142ef6cSHugh Dickins 	}
1006bde05d1cSHugh Dickins 	spin_unlock_irq(&swap_mapping->tree_lock);
1007bde05d1cSHugh Dickins 
10080142ef6cSHugh Dickins 	if (unlikely(error)) {
10090142ef6cSHugh Dickins 		/*
10100142ef6cSHugh Dickins 		 * Is this possible?  I think not, now that our callers check
10110142ef6cSHugh Dickins 		 * both PageSwapCache and page_private after getting page lock;
10120142ef6cSHugh Dickins 		 * but be defensive.  Reverse old to newpage for clear and free.
10130142ef6cSHugh Dickins 		 */
10140142ef6cSHugh Dickins 		oldpage = newpage;
10150142ef6cSHugh Dickins 	} else {
10160a31bc97SJohannes Weiner 		mem_cgroup_migrate(oldpage, newpage, false);
1017bde05d1cSHugh Dickins 		lru_cache_add_anon(newpage);
10180142ef6cSHugh Dickins 		*pagep = newpage;
10190142ef6cSHugh Dickins 	}
1020bde05d1cSHugh Dickins 
1021bde05d1cSHugh Dickins 	ClearPageSwapCache(oldpage);
1022bde05d1cSHugh Dickins 	set_page_private(oldpage, 0);
1023bde05d1cSHugh Dickins 
1024bde05d1cSHugh Dickins 	unlock_page(oldpage);
1025bde05d1cSHugh Dickins 	page_cache_release(oldpage);
1026bde05d1cSHugh Dickins 	page_cache_release(oldpage);
10270142ef6cSHugh Dickins 	return error;
1028bde05d1cSHugh Dickins }
1029bde05d1cSHugh Dickins 
1030bde05d1cSHugh Dickins /*
103168da9f05SHugh Dickins  * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
10321da177e4SLinus Torvalds  *
10331da177e4SLinus Torvalds  * If we allocate a new one we do not mark it dirty. That's up to the
10341da177e4SLinus Torvalds  * vm. If we swap it in we mark it dirty since we also free the swap
10351da177e4SLinus Torvalds  * entry since a page cannot live in both the swap and page cache
10361da177e4SLinus Torvalds  */
103741ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
103868da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
10391da177e4SLinus Torvalds {
10401da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
104154af6042SHugh Dickins 	struct shmem_inode_info *info;
10421da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo;
104300501b53SJohannes Weiner 	struct mem_cgroup *memcg;
104427ab7006SHugh Dickins 	struct page *page;
10451da177e4SLinus Torvalds 	swp_entry_t swap;
10461da177e4SLinus Torvalds 	int error;
104754af6042SHugh Dickins 	int once = 0;
10481635f6a7SHugh Dickins 	int alloced = 0;
10491da177e4SLinus Torvalds 
105041ffe5d5SHugh Dickins 	if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
10511da177e4SLinus Torvalds 		return -EFBIG;
10521da177e4SLinus Torvalds repeat:
105354af6042SHugh Dickins 	swap.val = 0;
10540cd6144aSJohannes Weiner 	page = find_lock_entry(mapping, index);
105554af6042SHugh Dickins 	if (radix_tree_exceptional_entry(page)) {
105654af6042SHugh Dickins 		swap = radix_to_swp_entry(page);
105754af6042SHugh Dickins 		page = NULL;
105854af6042SHugh Dickins 	}
105954af6042SHugh Dickins 
10601635f6a7SHugh Dickins 	if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
106154af6042SHugh Dickins 	    ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
106254af6042SHugh Dickins 		error = -EINVAL;
106354af6042SHugh Dickins 		goto failed;
106454af6042SHugh Dickins 	}
106554af6042SHugh Dickins 
106666d2f4d2SHugh Dickins 	if (page && sgp == SGP_WRITE)
106766d2f4d2SHugh Dickins 		mark_page_accessed(page);
106866d2f4d2SHugh Dickins 
10691635f6a7SHugh Dickins 	/* fallocated page? */
10701635f6a7SHugh Dickins 	if (page && !PageUptodate(page)) {
10711635f6a7SHugh Dickins 		if (sgp != SGP_READ)
10721635f6a7SHugh Dickins 			goto clear;
10731635f6a7SHugh Dickins 		unlock_page(page);
10741635f6a7SHugh Dickins 		page_cache_release(page);
10751635f6a7SHugh Dickins 		page = NULL;
10761635f6a7SHugh Dickins 	}
107754af6042SHugh Dickins 	if (page || (sgp == SGP_READ && !swap.val)) {
107854af6042SHugh Dickins 		*pagep = page;
107954af6042SHugh Dickins 		return 0;
108027ab7006SHugh Dickins 	}
108127ab7006SHugh Dickins 
1082b409f9fcSHugh Dickins 	/*
108354af6042SHugh Dickins 	 * Fast cache lookup did not find it:
108454af6042SHugh Dickins 	 * bring it back from swap or allocate.
1085b409f9fcSHugh Dickins 	 */
108654af6042SHugh Dickins 	info = SHMEM_I(inode);
108754af6042SHugh Dickins 	sbinfo = SHMEM_SB(inode->i_sb);
108827ab7006SHugh Dickins 
10891da177e4SLinus Torvalds 	if (swap.val) {
10901da177e4SLinus Torvalds 		/* Look it up and read it in.. */
109127ab7006SHugh Dickins 		page = lookup_swap_cache(swap);
109227ab7006SHugh Dickins 		if (!page) {
1093456f998eSYing Han 			/* here we actually do the io */
109468da9f05SHugh Dickins 			if (fault_type)
109568da9f05SHugh Dickins 				*fault_type |= VM_FAULT_MAJOR;
109641ffe5d5SHugh Dickins 			page = shmem_swapin(swap, gfp, info, index);
109727ab7006SHugh Dickins 			if (!page) {
10981da177e4SLinus Torvalds 				error = -ENOMEM;
109954af6042SHugh Dickins 				goto failed;
1100285b2c4fSHugh Dickins 			}
11011da177e4SLinus Torvalds 		}
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 		/* We have to do this with page locked to prevent races */
110454af6042SHugh Dickins 		lock_page(page);
11050142ef6cSHugh Dickins 		if (!PageSwapCache(page) || page_private(page) != swap.val ||
1106d1899228SHugh Dickins 		    !shmem_confirm_swap(mapping, index, swap)) {
1107bde05d1cSHugh Dickins 			error = -EEXIST;	/* try again */
1108d1899228SHugh Dickins 			goto unlock;
1109bde05d1cSHugh Dickins 		}
111027ab7006SHugh Dickins 		if (!PageUptodate(page)) {
11111da177e4SLinus Torvalds 			error = -EIO;
111254af6042SHugh Dickins 			goto failed;
111354af6042SHugh Dickins 		}
111454af6042SHugh Dickins 		wait_on_page_writeback(page);
111554af6042SHugh Dickins 
1116bde05d1cSHugh Dickins 		if (shmem_should_replace_page(page, gfp)) {
1117bde05d1cSHugh Dickins 			error = shmem_replace_page(&page, gfp, info, index);
1118bde05d1cSHugh Dickins 			if (error)
111954af6042SHugh Dickins 				goto failed;
11201da177e4SLinus Torvalds 		}
11211da177e4SLinus Torvalds 
112200501b53SJohannes Weiner 		error = mem_cgroup_try_charge(page, current->mm, gfp, &memcg);
1123d1899228SHugh Dickins 		if (!error) {
112454af6042SHugh Dickins 			error = shmem_add_to_page_cache(page, mapping, index,
1125fed400a1SWang Sheng-Hui 						swp_to_radix_entry(swap));
1126215c02bcSHugh Dickins 			/*
1127215c02bcSHugh Dickins 			 * We already confirmed swap under page lock, and make
1128215c02bcSHugh Dickins 			 * no memory allocation here, so usually no possibility
1129215c02bcSHugh Dickins 			 * of error; but free_swap_and_cache() only trylocks a
1130215c02bcSHugh Dickins 			 * page, so it is just possible that the entry has been
1131215c02bcSHugh Dickins 			 * truncated or holepunched since swap was confirmed.
1132215c02bcSHugh Dickins 			 * shmem_undo_range() will have done some of the
1133215c02bcSHugh Dickins 			 * unaccounting, now delete_from_swap_cache() will do
1134215c02bcSHugh Dickins 			 * the rest (including mem_cgroup_uncharge_swapcache).
1135215c02bcSHugh Dickins 			 * Reset swap.val? No, leave it so "failed" goes back to
1136215c02bcSHugh Dickins 			 * "repeat": reading a hole and writing should succeed.
1137215c02bcSHugh Dickins 			 */
113800501b53SJohannes Weiner 			if (error) {
113900501b53SJohannes Weiner 				mem_cgroup_cancel_charge(page, memcg);
1140215c02bcSHugh Dickins 				delete_from_swap_cache(page);
1141d1899228SHugh Dickins 			}
114200501b53SJohannes Weiner 		}
114354af6042SHugh Dickins 		if (error)
114454af6042SHugh Dickins 			goto failed;
114554af6042SHugh Dickins 
114600501b53SJohannes Weiner 		mem_cgroup_commit_charge(page, memcg, true);
114700501b53SJohannes Weiner 
114854af6042SHugh Dickins 		spin_lock(&info->lock);
114954af6042SHugh Dickins 		info->swapped--;
115054af6042SHugh Dickins 		shmem_recalc_inode(inode);
11511da177e4SLinus Torvalds 		spin_unlock(&info->lock);
115227ab7006SHugh Dickins 
115366d2f4d2SHugh Dickins 		if (sgp == SGP_WRITE)
115466d2f4d2SHugh Dickins 			mark_page_accessed(page);
115566d2f4d2SHugh Dickins 
115627ab7006SHugh Dickins 		delete_from_swap_cache(page);
115727ab7006SHugh Dickins 		set_page_dirty(page);
115827ab7006SHugh Dickins 		swap_free(swap);
115927ab7006SHugh Dickins 
116054af6042SHugh Dickins 	} else {
116154af6042SHugh Dickins 		if (shmem_acct_block(info->flags)) {
116254af6042SHugh Dickins 			error = -ENOSPC;
116354af6042SHugh Dickins 			goto failed;
11641da177e4SLinus Torvalds 		}
11650edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
1166fc5da22aSHugh Dickins 			if (percpu_counter_compare(&sbinfo->used_blocks,
116754af6042SHugh Dickins 						sbinfo->max_blocks) >= 0) {
116854af6042SHugh Dickins 				error = -ENOSPC;
116954af6042SHugh Dickins 				goto unacct;
117054af6042SHugh Dickins 			}
11717e496299STim Chen 			percpu_counter_inc(&sbinfo->used_blocks);
117259a16eadSHugh Dickins 		}
11731da177e4SLinus Torvalds 
117454af6042SHugh Dickins 		page = shmem_alloc_page(gfp, info, index);
117554af6042SHugh Dickins 		if (!page) {
117654af6042SHugh Dickins 			error = -ENOMEM;
117754af6042SHugh Dickins 			goto decused;
117854af6042SHugh Dickins 		}
117954af6042SHugh Dickins 
118007a42788SMel Gorman 		__SetPageSwapBacked(page);
118154af6042SHugh Dickins 		__set_page_locked(page);
118266d2f4d2SHugh Dickins 		if (sgp == SGP_WRITE)
1183eb39d618SHugh Dickins 			__SetPageReferenced(page);
118466d2f4d2SHugh Dickins 
118500501b53SJohannes Weiner 		error = mem_cgroup_try_charge(page, current->mm, gfp, &memcg);
118654af6042SHugh Dickins 		if (error)
118754af6042SHugh Dickins 			goto decused;
11885e4c0d97SJan Kara 		error = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK);
1189b065b432SHugh Dickins 		if (!error) {
1190b065b432SHugh Dickins 			error = shmem_add_to_page_cache(page, mapping, index,
1191fed400a1SWang Sheng-Hui 							NULL);
1192b065b432SHugh Dickins 			radix_tree_preload_end();
1193b065b432SHugh Dickins 		}
1194b065b432SHugh Dickins 		if (error) {
119500501b53SJohannes Weiner 			mem_cgroup_cancel_charge(page, memcg);
1196b065b432SHugh Dickins 			goto decused;
1197b065b432SHugh Dickins 		}
119800501b53SJohannes Weiner 		mem_cgroup_commit_charge(page, memcg, false);
119954af6042SHugh Dickins 		lru_cache_add_anon(page);
120054af6042SHugh Dickins 
120154af6042SHugh Dickins 		spin_lock(&info->lock);
12021da177e4SLinus Torvalds 		info->alloced++;
120354af6042SHugh Dickins 		inode->i_blocks += BLOCKS_PER_PAGE;
120454af6042SHugh Dickins 		shmem_recalc_inode(inode);
120559a16eadSHugh Dickins 		spin_unlock(&info->lock);
12061635f6a7SHugh Dickins 		alloced = true;
120754af6042SHugh Dickins 
1208ec9516fbSHugh Dickins 		/*
12091635f6a7SHugh Dickins 		 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
12101635f6a7SHugh Dickins 		 */
12111635f6a7SHugh Dickins 		if (sgp == SGP_FALLOC)
12121635f6a7SHugh Dickins 			sgp = SGP_WRITE;
12131635f6a7SHugh Dickins clear:
12141635f6a7SHugh Dickins 		/*
12151635f6a7SHugh Dickins 		 * Let SGP_WRITE caller clear ends if write does not fill page;
12161635f6a7SHugh Dickins 		 * but SGP_FALLOC on a page fallocated earlier must initialize
12171635f6a7SHugh Dickins 		 * it now, lest undo on failure cancel our earlier guarantee.
1218ec9516fbSHugh Dickins 		 */
1219ec9516fbSHugh Dickins 		if (sgp != SGP_WRITE) {
122027ab7006SHugh Dickins 			clear_highpage(page);
122127ab7006SHugh Dickins 			flush_dcache_page(page);
122227ab7006SHugh Dickins 			SetPageUptodate(page);
1223ec9516fbSHugh Dickins 		}
1224a0ee5ec5SHugh Dickins 		if (sgp == SGP_DIRTY)
122527ab7006SHugh Dickins 			set_page_dirty(page);
12261da177e4SLinus Torvalds 	}
1227bde05d1cSHugh Dickins 
122854af6042SHugh Dickins 	/* Perhaps the file has been truncated since we checked */
12291635f6a7SHugh Dickins 	if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
123054af6042SHugh Dickins 	    ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
123154af6042SHugh Dickins 		error = -EINVAL;
12321635f6a7SHugh Dickins 		if (alloced)
123354af6042SHugh Dickins 			goto trunc;
12341635f6a7SHugh Dickins 		else
12351635f6a7SHugh Dickins 			goto failed;
1236ff36b801SShaohua Li 	}
123754af6042SHugh Dickins 	*pagep = page;
123854af6042SHugh Dickins 	return 0;
1239d00806b1SNick Piggin 
1240d0217ac0SNick Piggin 	/*
124154af6042SHugh Dickins 	 * Error recovery.
12421da177e4SLinus Torvalds 	 */
124354af6042SHugh Dickins trunc:
12441635f6a7SHugh Dickins 	info = SHMEM_I(inode);
124554af6042SHugh Dickins 	ClearPageDirty(page);
124654af6042SHugh Dickins 	delete_from_page_cache(page);
124754af6042SHugh Dickins 	spin_lock(&info->lock);
124854af6042SHugh Dickins 	info->alloced--;
124954af6042SHugh Dickins 	inode->i_blocks -= BLOCKS_PER_PAGE;
12501da177e4SLinus Torvalds 	spin_unlock(&info->lock);
125154af6042SHugh Dickins decused:
12521635f6a7SHugh Dickins 	sbinfo = SHMEM_SB(inode->i_sb);
125354af6042SHugh Dickins 	if (sbinfo->max_blocks)
125454af6042SHugh Dickins 		percpu_counter_add(&sbinfo->used_blocks, -1);
125554af6042SHugh Dickins unacct:
125654af6042SHugh Dickins 	shmem_unacct_blocks(info->flags, 1);
125754af6042SHugh Dickins failed:
1258d1899228SHugh Dickins 	if (swap.val && error != -EINVAL &&
1259d1899228SHugh Dickins 	    !shmem_confirm_swap(mapping, index, swap))
126054af6042SHugh Dickins 		error = -EEXIST;
1261d1899228SHugh Dickins unlock:
126227ab7006SHugh Dickins 	if (page) {
126354af6042SHugh Dickins 		unlock_page(page);
12641da177e4SLinus Torvalds 		page_cache_release(page);
126554af6042SHugh Dickins 	}
126654af6042SHugh Dickins 	if (error == -ENOSPC && !once++) {
126754af6042SHugh Dickins 		info = SHMEM_I(inode);
126854af6042SHugh Dickins 		spin_lock(&info->lock);
126954af6042SHugh Dickins 		shmem_recalc_inode(inode);
127054af6042SHugh Dickins 		spin_unlock(&info->lock);
12711da177e4SLinus Torvalds 		goto repeat;
1272d8dc74f2SAdrian Bunk 	}
1273d1899228SHugh Dickins 	if (error == -EEXIST)	/* from above or from radix_tree_insert */
127454af6042SHugh Dickins 		goto repeat;
127554af6042SHugh Dickins 	return error;
12761da177e4SLinus Torvalds }
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
12791da177e4SLinus Torvalds {
1280496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
12811da177e4SLinus Torvalds 	int error;
128268da9f05SHugh Dickins 	int ret = VM_FAULT_LOCKED;
12831da177e4SLinus Torvalds 
1284f00cdc6dSHugh Dickins 	/*
1285f00cdc6dSHugh Dickins 	 * Trinity finds that probing a hole which tmpfs is punching can
1286f00cdc6dSHugh Dickins 	 * prevent the hole-punch from ever completing: which in turn
1287f00cdc6dSHugh Dickins 	 * locks writers out with its hold on i_mutex.  So refrain from
12888e205f77SHugh Dickins 	 * faulting pages into the hole while it's being punched.  Although
12898e205f77SHugh Dickins 	 * shmem_undo_range() does remove the additions, it may be unable to
12908e205f77SHugh Dickins 	 * keep up, as each new page needs its own unmap_mapping_range() call,
12918e205f77SHugh Dickins 	 * and the i_mmap tree grows ever slower to scan if new vmas are added.
12928e205f77SHugh Dickins 	 *
12938e205f77SHugh Dickins 	 * It does not matter if we sometimes reach this check just before the
12948e205f77SHugh Dickins 	 * hole-punch begins, so that one fault then races with the punch:
12958e205f77SHugh Dickins 	 * we just need to make racing faults a rare case.
12968e205f77SHugh Dickins 	 *
12978e205f77SHugh Dickins 	 * The implementation below would be much simpler if we just used a
12988e205f77SHugh Dickins 	 * standard mutex or completion: but we cannot take i_mutex in fault,
12998e205f77SHugh Dickins 	 * and bloating every shmem inode for this unlikely case would be sad.
1300f00cdc6dSHugh Dickins 	 */
1301f00cdc6dSHugh Dickins 	if (unlikely(inode->i_private)) {
1302f00cdc6dSHugh Dickins 		struct shmem_falloc *shmem_falloc;
1303f00cdc6dSHugh Dickins 
1304f00cdc6dSHugh Dickins 		spin_lock(&inode->i_lock);
1305f00cdc6dSHugh Dickins 		shmem_falloc = inode->i_private;
13068e205f77SHugh Dickins 		if (shmem_falloc &&
13078e205f77SHugh Dickins 		    shmem_falloc->waitq &&
13088e205f77SHugh Dickins 		    vmf->pgoff >= shmem_falloc->start &&
13098e205f77SHugh Dickins 		    vmf->pgoff < shmem_falloc->next) {
13108e205f77SHugh Dickins 			wait_queue_head_t *shmem_falloc_waitq;
13118e205f77SHugh Dickins 			DEFINE_WAIT(shmem_fault_wait);
13128e205f77SHugh Dickins 
13138e205f77SHugh Dickins 			ret = VM_FAULT_NOPAGE;
1314f00cdc6dSHugh Dickins 			if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1315f00cdc6dSHugh Dickins 			   !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
13168e205f77SHugh Dickins 				/* It's polite to up mmap_sem if we can */
1317f00cdc6dSHugh Dickins 				up_read(&vma->vm_mm->mmap_sem);
13188e205f77SHugh Dickins 				ret = VM_FAULT_RETRY;
1319f00cdc6dSHugh Dickins 			}
13208e205f77SHugh Dickins 
13218e205f77SHugh Dickins 			shmem_falloc_waitq = shmem_falloc->waitq;
13228e205f77SHugh Dickins 			prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
13238e205f77SHugh Dickins 					TASK_UNINTERRUPTIBLE);
13248e205f77SHugh Dickins 			spin_unlock(&inode->i_lock);
13258e205f77SHugh Dickins 			schedule();
13268e205f77SHugh Dickins 
13278e205f77SHugh Dickins 			/*
13288e205f77SHugh Dickins 			 * shmem_falloc_waitq points into the shmem_fallocate()
13298e205f77SHugh Dickins 			 * stack of the hole-punching task: shmem_falloc_waitq
13308e205f77SHugh Dickins 			 * is usually invalid by the time we reach here, but
13318e205f77SHugh Dickins 			 * finish_wait() does not dereference it in that case;
13328e205f77SHugh Dickins 			 * though i_lock needed lest racing with wake_up_all().
13338e205f77SHugh Dickins 			 */
13348e205f77SHugh Dickins 			spin_lock(&inode->i_lock);
13358e205f77SHugh Dickins 			finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
13368e205f77SHugh Dickins 			spin_unlock(&inode->i_lock);
13378e205f77SHugh Dickins 			return ret;
1338f00cdc6dSHugh Dickins 		}
13398e205f77SHugh Dickins 		spin_unlock(&inode->i_lock);
1340f00cdc6dSHugh Dickins 	}
1341f00cdc6dSHugh Dickins 
13421da177e4SLinus Torvalds 	error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
13431da177e4SLinus Torvalds 	if (error)
13441da177e4SLinus Torvalds 		return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
134568da9f05SHugh Dickins 
1346456f998eSYing Han 	if (ret & VM_FAULT_MAJOR) {
1347456f998eSYing Han 		count_vm_event(PGMAJFAULT);
1348456f998eSYing Han 		mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1349456f998eSYing Han 	}
135068da9f05SHugh Dickins 	return ret;
13511da177e4SLinus Torvalds }
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds #ifdef CONFIG_NUMA
135441ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
13551da177e4SLinus Torvalds {
1356496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
135741ffe5d5SHugh Dickins 	return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
13581da177e4SLinus Torvalds }
13591da177e4SLinus Torvalds 
1360d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1361d8dc74f2SAdrian Bunk 					  unsigned long addr)
13621da177e4SLinus Torvalds {
1363496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
136441ffe5d5SHugh Dickins 	pgoff_t index;
13651da177e4SLinus Torvalds 
136641ffe5d5SHugh Dickins 	index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
136741ffe5d5SHugh Dickins 	return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds #endif
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user)
13721da177e4SLinus Torvalds {
1373496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
13741da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
13751da177e4SLinus Torvalds 	int retval = -ENOMEM;
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 	spin_lock(&info->lock);
13781da177e4SLinus Torvalds 	if (lock && !(info->flags & VM_LOCKED)) {
13791da177e4SLinus Torvalds 		if (!user_shm_lock(inode->i_size, user))
13801da177e4SLinus Torvalds 			goto out_nomem;
13811da177e4SLinus Torvalds 		info->flags |= VM_LOCKED;
138289e004eaSLee Schermerhorn 		mapping_set_unevictable(file->f_mapping);
13831da177e4SLinus Torvalds 	}
13841da177e4SLinus Torvalds 	if (!lock && (info->flags & VM_LOCKED) && user) {
13851da177e4SLinus Torvalds 		user_shm_unlock(inode->i_size, user);
13861da177e4SLinus Torvalds 		info->flags &= ~VM_LOCKED;
138789e004eaSLee Schermerhorn 		mapping_clear_unevictable(file->f_mapping);
13881da177e4SLinus Torvalds 	}
13891da177e4SLinus Torvalds 	retval = 0;
139089e004eaSLee Schermerhorn 
13911da177e4SLinus Torvalds out_nomem:
13921da177e4SLinus Torvalds 	spin_unlock(&info->lock);
13931da177e4SLinus Torvalds 	return retval;
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds 
13969b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
13971da177e4SLinus Torvalds {
13981da177e4SLinus Torvalds 	file_accessed(file);
13991da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
14001da177e4SLinus Torvalds 	return 0;
14011da177e4SLinus Torvalds }
14021da177e4SLinus Torvalds 
1403454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
140409208d15SAl Viro 				     umode_t mode, dev_t dev, unsigned long flags)
14051da177e4SLinus Torvalds {
14061da177e4SLinus Torvalds 	struct inode *inode;
14071da177e4SLinus Torvalds 	struct shmem_inode_info *info;
14081da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
14091da177e4SLinus Torvalds 
14105b04c689SPavel Emelyanov 	if (shmem_reserve_inode(sb))
14111da177e4SLinus Torvalds 		return NULL;
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds 	inode = new_inode(sb);
14141da177e4SLinus Torvalds 	if (inode) {
141585fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
1416454abafeSDmitry Monakhov 		inode_init_owner(inode, dir, mode);
14171da177e4SLinus Torvalds 		inode->i_blocks = 0;
14181da177e4SLinus Torvalds 		inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
14191da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
142091828a40SDavid M. Grimes 		inode->i_generation = get_seconds();
14211da177e4SLinus Torvalds 		info = SHMEM_I(inode);
14221da177e4SLinus Torvalds 		memset(info, 0, (char *)inode - (char *)info);
14231da177e4SLinus Torvalds 		spin_lock_init(&info->lock);
142440e041a2SDavid Herrmann 		info->seals = F_SEAL_SEAL;
14250b0a0806SHugh Dickins 		info->flags = flags & VM_NORESERVE;
14261da177e4SLinus Torvalds 		INIT_LIST_HEAD(&info->swaplist);
142738f38657SAristeu Rozanski 		simple_xattrs_init(&info->xattrs);
142872c04902SAl Viro 		cache_no_acl(inode);
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
14311da177e4SLinus Torvalds 		default:
143239f0247dSAndreas Gruenbacher 			inode->i_op = &shmem_special_inode_operations;
14331da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
14341da177e4SLinus Torvalds 			break;
14351da177e4SLinus Torvalds 		case S_IFREG:
143614fcc23fSHugh Dickins 			inode->i_mapping->a_ops = &shmem_aops;
14371da177e4SLinus Torvalds 			inode->i_op = &shmem_inode_operations;
14381da177e4SLinus Torvalds 			inode->i_fop = &shmem_file_operations;
143971fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy,
144071fe804bSLee Schermerhorn 						 shmem_get_sbmpol(sbinfo));
14411da177e4SLinus Torvalds 			break;
14421da177e4SLinus Torvalds 		case S_IFDIR:
1443d8c76e6fSDave Hansen 			inc_nlink(inode);
14441da177e4SLinus Torvalds 			/* Some things misbehave if size == 0 on a directory */
14451da177e4SLinus Torvalds 			inode->i_size = 2 * BOGO_DIRENT_SIZE;
14461da177e4SLinus Torvalds 			inode->i_op = &shmem_dir_inode_operations;
14471da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
14481da177e4SLinus Torvalds 			break;
14491da177e4SLinus Torvalds 		case S_IFLNK:
14501da177e4SLinus Torvalds 			/*
14511da177e4SLinus Torvalds 			 * Must not load anything in the rbtree,
14521da177e4SLinus Torvalds 			 * mpol_free_shared_policy will not be called.
14531da177e4SLinus Torvalds 			 */
145471fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy, NULL);
14551da177e4SLinus Torvalds 			break;
14561da177e4SLinus Torvalds 		}
14575b04c689SPavel Emelyanov 	} else
14585b04c689SPavel Emelyanov 		shmem_free_inode(sb);
14591da177e4SLinus Torvalds 	return inode;
14601da177e4SLinus Torvalds }
14611da177e4SLinus Torvalds 
14620cd6144aSJohannes Weiner bool shmem_mapping(struct address_space *mapping)
14630cd6144aSJohannes Weiner {
14640cd6144aSJohannes Weiner 	return mapping->backing_dev_info == &shmem_backing_dev_info;
14650cd6144aSJohannes Weiner }
14660cd6144aSJohannes Weiner 
14671da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
146892e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations;
146969f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations;
14701da177e4SLinus Torvalds 
14716d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR
14726d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
14736d9d88d0SJarkko Sakkinen #else
14746d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL
14756d9d88d0SJarkko Sakkinen #endif
14766d9d88d0SJarkko Sakkinen 
14771da177e4SLinus Torvalds static int
1478800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping,
1479800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
1480800d15a5SNick Piggin 			struct page **pagep, void **fsdata)
14811da177e4SLinus Torvalds {
1482800d15a5SNick Piggin 	struct inode *inode = mapping->host;
148340e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
1484800d15a5SNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
148540e041a2SDavid Herrmann 
148640e041a2SDavid Herrmann 	/* i_mutex is held by caller */
148740e041a2SDavid Herrmann 	if (unlikely(info->seals)) {
148840e041a2SDavid Herrmann 		if (info->seals & F_SEAL_WRITE)
148940e041a2SDavid Herrmann 			return -EPERM;
149040e041a2SDavid Herrmann 		if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
149140e041a2SDavid Herrmann 			return -EPERM;
149240e041a2SDavid Herrmann 	}
149340e041a2SDavid Herrmann 
149466d2f4d2SHugh Dickins 	return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1495800d15a5SNick Piggin }
1496800d15a5SNick Piggin 
1497800d15a5SNick Piggin static int
1498800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping,
1499800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
1500800d15a5SNick Piggin 			struct page *page, void *fsdata)
1501800d15a5SNick Piggin {
1502800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1503800d15a5SNick Piggin 
1504800d15a5SNick Piggin 	if (pos + copied > inode->i_size)
1505800d15a5SNick Piggin 		i_size_write(inode, pos + copied);
1506800d15a5SNick Piggin 
1507ec9516fbSHugh Dickins 	if (!PageUptodate(page)) {
1508ec9516fbSHugh Dickins 		if (copied < PAGE_CACHE_SIZE) {
1509ec9516fbSHugh Dickins 			unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1510ec9516fbSHugh Dickins 			zero_user_segments(page, 0, from,
1511ec9516fbSHugh Dickins 					from + copied, PAGE_CACHE_SIZE);
1512ec9516fbSHugh Dickins 		}
1513ec9516fbSHugh Dickins 		SetPageUptodate(page);
1514ec9516fbSHugh Dickins 	}
1515d3602444SHugh Dickins 	set_page_dirty(page);
15166746aff7SWu Fengguang 	unlock_page(page);
1517d3602444SHugh Dickins 	page_cache_release(page);
1518d3602444SHugh Dickins 
1519800d15a5SNick Piggin 	return copied;
15201da177e4SLinus Torvalds }
15211da177e4SLinus Torvalds 
15222ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
15231da177e4SLinus Torvalds {
15246e58e79dSAl Viro 	struct file *file = iocb->ki_filp;
15256e58e79dSAl Viro 	struct inode *inode = file_inode(file);
15261da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
152741ffe5d5SHugh Dickins 	pgoff_t index;
152841ffe5d5SHugh Dickins 	unsigned long offset;
1529a0ee5ec5SHugh Dickins 	enum sgp_type sgp = SGP_READ;
1530f7c1d074SGeert Uytterhoeven 	int error = 0;
1531cb66a7a1SAl Viro 	ssize_t retval = 0;
15326e58e79dSAl Viro 	loff_t *ppos = &iocb->ki_pos;
1533a0ee5ec5SHugh Dickins 
1534a0ee5ec5SHugh Dickins 	/*
1535a0ee5ec5SHugh Dickins 	 * Might this read be for a stacking filesystem?  Then when reading
1536a0ee5ec5SHugh Dickins 	 * holes of a sparse file, we actually need to allocate those pages,
1537a0ee5ec5SHugh Dickins 	 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1538a0ee5ec5SHugh Dickins 	 */
1539a0ee5ec5SHugh Dickins 	if (segment_eq(get_fs(), KERNEL_DS))
1540a0ee5ec5SHugh Dickins 		sgp = SGP_DIRTY;
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 	index = *ppos >> PAGE_CACHE_SHIFT;
15431da177e4SLinus Torvalds 	offset = *ppos & ~PAGE_CACHE_MASK;
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 	for (;;) {
15461da177e4SLinus Torvalds 		struct page *page = NULL;
154741ffe5d5SHugh Dickins 		pgoff_t end_index;
154841ffe5d5SHugh Dickins 		unsigned long nr, ret;
15491da177e4SLinus Torvalds 		loff_t i_size = i_size_read(inode);
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
15521da177e4SLinus Torvalds 		if (index > end_index)
15531da177e4SLinus Torvalds 			break;
15541da177e4SLinus Torvalds 		if (index == end_index) {
15551da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
15561da177e4SLinus Torvalds 			if (nr <= offset)
15571da177e4SLinus Torvalds 				break;
15581da177e4SLinus Torvalds 		}
15591da177e4SLinus Torvalds 
15606e58e79dSAl Viro 		error = shmem_getpage(inode, index, &page, sgp, NULL);
15616e58e79dSAl Viro 		if (error) {
15626e58e79dSAl Viro 			if (error == -EINVAL)
15636e58e79dSAl Viro 				error = 0;
15641da177e4SLinus Torvalds 			break;
15651da177e4SLinus Torvalds 		}
1566d3602444SHugh Dickins 		if (page)
1567d3602444SHugh Dickins 			unlock_page(page);
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 		/*
15701da177e4SLinus Torvalds 		 * We must evaluate after, since reads (unlike writes)
15711b1dcc1bSJes Sorensen 		 * are called without i_mutex protection against truncate
15721da177e4SLinus Torvalds 		 */
15731da177e4SLinus Torvalds 		nr = PAGE_CACHE_SIZE;
15741da177e4SLinus Torvalds 		i_size = i_size_read(inode);
15751da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
15761da177e4SLinus Torvalds 		if (index == end_index) {
15771da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
15781da177e4SLinus Torvalds 			if (nr <= offset) {
15791da177e4SLinus Torvalds 				if (page)
15801da177e4SLinus Torvalds 					page_cache_release(page);
15811da177e4SLinus Torvalds 				break;
15821da177e4SLinus Torvalds 			}
15831da177e4SLinus Torvalds 		}
15841da177e4SLinus Torvalds 		nr -= offset;
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 		if (page) {
15871da177e4SLinus Torvalds 			/*
15881da177e4SLinus Torvalds 			 * If users can be writing to this page using arbitrary
15891da177e4SLinus Torvalds 			 * virtual addresses, take care about potential aliasing
15901da177e4SLinus Torvalds 			 * before reading the page on the kernel side.
15911da177e4SLinus Torvalds 			 */
15921da177e4SLinus Torvalds 			if (mapping_writably_mapped(mapping))
15931da177e4SLinus Torvalds 				flush_dcache_page(page);
15941da177e4SLinus Torvalds 			/*
15951da177e4SLinus Torvalds 			 * Mark the page accessed if we read the beginning.
15961da177e4SLinus Torvalds 			 */
15971da177e4SLinus Torvalds 			if (!offset)
15981da177e4SLinus Torvalds 				mark_page_accessed(page);
1599b5810039SNick Piggin 		} else {
16001da177e4SLinus Torvalds 			page = ZERO_PAGE(0);
1601b5810039SNick Piggin 			page_cache_get(page);
1602b5810039SNick Piggin 		}
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 		/*
16051da177e4SLinus Torvalds 		 * Ok, we have the page, and it's up-to-date, so
16061da177e4SLinus Torvalds 		 * now we can copy it to user space...
16071da177e4SLinus Torvalds 		 */
16082ba5bbedSAl Viro 		ret = copy_page_to_iter(page, offset, nr, to);
16096e58e79dSAl Viro 		retval += ret;
16101da177e4SLinus Torvalds 		offset += ret;
16111da177e4SLinus Torvalds 		index += offset >> PAGE_CACHE_SHIFT;
16121da177e4SLinus Torvalds 		offset &= ~PAGE_CACHE_MASK;
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 		page_cache_release(page);
16152ba5bbedSAl Viro 		if (!iov_iter_count(to))
16161da177e4SLinus Torvalds 			break;
16176e58e79dSAl Viro 		if (ret < nr) {
16186e58e79dSAl Viro 			error = -EFAULT;
16196e58e79dSAl Viro 			break;
16206e58e79dSAl Viro 		}
16211da177e4SLinus Torvalds 		cond_resched();
16221da177e4SLinus Torvalds 	}
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
16256e58e79dSAl Viro 	file_accessed(file);
16266e58e79dSAl Viro 	return retval ? retval : error;
16271da177e4SLinus Torvalds }
16281da177e4SLinus Torvalds 
1629708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1630708e3508SHugh Dickins 				struct pipe_inode_info *pipe, size_t len,
1631708e3508SHugh Dickins 				unsigned int flags)
1632708e3508SHugh Dickins {
1633708e3508SHugh Dickins 	struct address_space *mapping = in->f_mapping;
163471f0e07aSHugh Dickins 	struct inode *inode = mapping->host;
1635708e3508SHugh Dickins 	unsigned int loff, nr_pages, req_pages;
1636708e3508SHugh Dickins 	struct page *pages[PIPE_DEF_BUFFERS];
1637708e3508SHugh Dickins 	struct partial_page partial[PIPE_DEF_BUFFERS];
1638708e3508SHugh Dickins 	struct page *page;
1639708e3508SHugh Dickins 	pgoff_t index, end_index;
1640708e3508SHugh Dickins 	loff_t isize, left;
1641708e3508SHugh Dickins 	int error, page_nr;
1642708e3508SHugh Dickins 	struct splice_pipe_desc spd = {
1643708e3508SHugh Dickins 		.pages = pages,
1644708e3508SHugh Dickins 		.partial = partial,
1645047fe360SEric Dumazet 		.nr_pages_max = PIPE_DEF_BUFFERS,
1646708e3508SHugh Dickins 		.flags = flags,
1647708e3508SHugh Dickins 		.ops = &page_cache_pipe_buf_ops,
1648708e3508SHugh Dickins 		.spd_release = spd_release_page,
1649708e3508SHugh Dickins 	};
1650708e3508SHugh Dickins 
165171f0e07aSHugh Dickins 	isize = i_size_read(inode);
1652708e3508SHugh Dickins 	if (unlikely(*ppos >= isize))
1653708e3508SHugh Dickins 		return 0;
1654708e3508SHugh Dickins 
1655708e3508SHugh Dickins 	left = isize - *ppos;
1656708e3508SHugh Dickins 	if (unlikely(left < len))
1657708e3508SHugh Dickins 		len = left;
1658708e3508SHugh Dickins 
1659708e3508SHugh Dickins 	if (splice_grow_spd(pipe, &spd))
1660708e3508SHugh Dickins 		return -ENOMEM;
1661708e3508SHugh Dickins 
1662708e3508SHugh Dickins 	index = *ppos >> PAGE_CACHE_SHIFT;
1663708e3508SHugh Dickins 	loff = *ppos & ~PAGE_CACHE_MASK;
1664708e3508SHugh Dickins 	req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1665a786c06dSAl Viro 	nr_pages = min(req_pages, spd.nr_pages_max);
1666708e3508SHugh Dickins 
1667708e3508SHugh Dickins 	spd.nr_pages = find_get_pages_contig(mapping, index,
1668708e3508SHugh Dickins 						nr_pages, spd.pages);
1669708e3508SHugh Dickins 	index += spd.nr_pages;
1670708e3508SHugh Dickins 	error = 0;
167171f0e07aSHugh Dickins 
1672708e3508SHugh Dickins 	while (spd.nr_pages < nr_pages) {
167371f0e07aSHugh Dickins 		error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
167471f0e07aSHugh Dickins 		if (error)
1675708e3508SHugh Dickins 			break;
1676708e3508SHugh Dickins 		unlock_page(page);
1677708e3508SHugh Dickins 		spd.pages[spd.nr_pages++] = page;
1678708e3508SHugh Dickins 		index++;
1679708e3508SHugh Dickins 	}
1680708e3508SHugh Dickins 
1681708e3508SHugh Dickins 	index = *ppos >> PAGE_CACHE_SHIFT;
1682708e3508SHugh Dickins 	nr_pages = spd.nr_pages;
1683708e3508SHugh Dickins 	spd.nr_pages = 0;
168471f0e07aSHugh Dickins 
1685708e3508SHugh Dickins 	for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1686708e3508SHugh Dickins 		unsigned int this_len;
1687708e3508SHugh Dickins 
1688708e3508SHugh Dickins 		if (!len)
1689708e3508SHugh Dickins 			break;
1690708e3508SHugh Dickins 
1691708e3508SHugh Dickins 		this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1692708e3508SHugh Dickins 		page = spd.pages[page_nr];
1693708e3508SHugh Dickins 
169471f0e07aSHugh Dickins 		if (!PageUptodate(page) || page->mapping != mapping) {
169571f0e07aSHugh Dickins 			error = shmem_getpage(inode, index, &page,
169671f0e07aSHugh Dickins 							SGP_CACHE, NULL);
169771f0e07aSHugh Dickins 			if (error)
1698708e3508SHugh Dickins 				break;
169971f0e07aSHugh Dickins 			unlock_page(page);
1700708e3508SHugh Dickins 			page_cache_release(spd.pages[page_nr]);
1701708e3508SHugh Dickins 			spd.pages[page_nr] = page;
1702708e3508SHugh Dickins 		}
1703708e3508SHugh Dickins 
170471f0e07aSHugh Dickins 		isize = i_size_read(inode);
1705708e3508SHugh Dickins 		end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1706708e3508SHugh Dickins 		if (unlikely(!isize || index > end_index))
1707708e3508SHugh Dickins 			break;
1708708e3508SHugh Dickins 
1709708e3508SHugh Dickins 		if (end_index == index) {
1710708e3508SHugh Dickins 			unsigned int plen;
1711708e3508SHugh Dickins 
1712708e3508SHugh Dickins 			plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1713708e3508SHugh Dickins 			if (plen <= loff)
1714708e3508SHugh Dickins 				break;
1715708e3508SHugh Dickins 
1716708e3508SHugh Dickins 			this_len = min(this_len, plen - loff);
1717708e3508SHugh Dickins 			len = this_len;
1718708e3508SHugh Dickins 		}
1719708e3508SHugh Dickins 
1720708e3508SHugh Dickins 		spd.partial[page_nr].offset = loff;
1721708e3508SHugh Dickins 		spd.partial[page_nr].len = this_len;
1722708e3508SHugh Dickins 		len -= this_len;
1723708e3508SHugh Dickins 		loff = 0;
1724708e3508SHugh Dickins 		spd.nr_pages++;
1725708e3508SHugh Dickins 		index++;
1726708e3508SHugh Dickins 	}
1727708e3508SHugh Dickins 
1728708e3508SHugh Dickins 	while (page_nr < nr_pages)
1729708e3508SHugh Dickins 		page_cache_release(spd.pages[page_nr++]);
1730708e3508SHugh Dickins 
1731708e3508SHugh Dickins 	if (spd.nr_pages)
1732708e3508SHugh Dickins 		error = splice_to_pipe(pipe, &spd);
1733708e3508SHugh Dickins 
1734047fe360SEric Dumazet 	splice_shrink_spd(&spd);
1735708e3508SHugh Dickins 
1736708e3508SHugh Dickins 	if (error > 0) {
1737708e3508SHugh Dickins 		*ppos += error;
1738708e3508SHugh Dickins 		file_accessed(in);
1739708e3508SHugh Dickins 	}
1740708e3508SHugh Dickins 	return error;
1741708e3508SHugh Dickins }
1742708e3508SHugh Dickins 
1743220f2ac9SHugh Dickins /*
1744220f2ac9SHugh Dickins  * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
1745220f2ac9SHugh Dickins  */
1746220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
1747965c8e59SAndrew Morton 				    pgoff_t index, pgoff_t end, int whence)
1748220f2ac9SHugh Dickins {
1749220f2ac9SHugh Dickins 	struct page *page;
1750220f2ac9SHugh Dickins 	struct pagevec pvec;
1751220f2ac9SHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
1752220f2ac9SHugh Dickins 	bool done = false;
1753220f2ac9SHugh Dickins 	int i;
1754220f2ac9SHugh Dickins 
1755220f2ac9SHugh Dickins 	pagevec_init(&pvec, 0);
1756220f2ac9SHugh Dickins 	pvec.nr = 1;		/* start small: we may be there already */
1757220f2ac9SHugh Dickins 	while (!done) {
17580cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
1759220f2ac9SHugh Dickins 					pvec.nr, pvec.pages, indices);
1760220f2ac9SHugh Dickins 		if (!pvec.nr) {
1761965c8e59SAndrew Morton 			if (whence == SEEK_DATA)
1762220f2ac9SHugh Dickins 				index = end;
1763220f2ac9SHugh Dickins 			break;
1764220f2ac9SHugh Dickins 		}
1765220f2ac9SHugh Dickins 		for (i = 0; i < pvec.nr; i++, index++) {
1766220f2ac9SHugh Dickins 			if (index < indices[i]) {
1767965c8e59SAndrew Morton 				if (whence == SEEK_HOLE) {
1768220f2ac9SHugh Dickins 					done = true;
1769220f2ac9SHugh Dickins 					break;
1770220f2ac9SHugh Dickins 				}
1771220f2ac9SHugh Dickins 				index = indices[i];
1772220f2ac9SHugh Dickins 			}
1773220f2ac9SHugh Dickins 			page = pvec.pages[i];
1774220f2ac9SHugh Dickins 			if (page && !radix_tree_exceptional_entry(page)) {
1775220f2ac9SHugh Dickins 				if (!PageUptodate(page))
1776220f2ac9SHugh Dickins 					page = NULL;
1777220f2ac9SHugh Dickins 			}
1778220f2ac9SHugh Dickins 			if (index >= end ||
1779965c8e59SAndrew Morton 			    (page && whence == SEEK_DATA) ||
1780965c8e59SAndrew Morton 			    (!page && whence == SEEK_HOLE)) {
1781220f2ac9SHugh Dickins 				done = true;
1782220f2ac9SHugh Dickins 				break;
1783220f2ac9SHugh Dickins 			}
1784220f2ac9SHugh Dickins 		}
17850cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
1786220f2ac9SHugh Dickins 		pagevec_release(&pvec);
1787220f2ac9SHugh Dickins 		pvec.nr = PAGEVEC_SIZE;
1788220f2ac9SHugh Dickins 		cond_resched();
1789220f2ac9SHugh Dickins 	}
1790220f2ac9SHugh Dickins 	return index;
1791220f2ac9SHugh Dickins }
1792220f2ac9SHugh Dickins 
1793965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
1794220f2ac9SHugh Dickins {
1795220f2ac9SHugh Dickins 	struct address_space *mapping = file->f_mapping;
1796220f2ac9SHugh Dickins 	struct inode *inode = mapping->host;
1797220f2ac9SHugh Dickins 	pgoff_t start, end;
1798220f2ac9SHugh Dickins 	loff_t new_offset;
1799220f2ac9SHugh Dickins 
1800965c8e59SAndrew Morton 	if (whence != SEEK_DATA && whence != SEEK_HOLE)
1801965c8e59SAndrew Morton 		return generic_file_llseek_size(file, offset, whence,
1802220f2ac9SHugh Dickins 					MAX_LFS_FILESIZE, i_size_read(inode));
1803220f2ac9SHugh Dickins 	mutex_lock(&inode->i_mutex);
1804220f2ac9SHugh Dickins 	/* We're holding i_mutex so we can access i_size directly */
1805220f2ac9SHugh Dickins 
1806220f2ac9SHugh Dickins 	if (offset < 0)
1807220f2ac9SHugh Dickins 		offset = -EINVAL;
1808220f2ac9SHugh Dickins 	else if (offset >= inode->i_size)
1809220f2ac9SHugh Dickins 		offset = -ENXIO;
1810220f2ac9SHugh Dickins 	else {
1811220f2ac9SHugh Dickins 		start = offset >> PAGE_CACHE_SHIFT;
1812220f2ac9SHugh Dickins 		end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1813965c8e59SAndrew Morton 		new_offset = shmem_seek_hole_data(mapping, start, end, whence);
1814220f2ac9SHugh Dickins 		new_offset <<= PAGE_CACHE_SHIFT;
1815220f2ac9SHugh Dickins 		if (new_offset > offset) {
1816220f2ac9SHugh Dickins 			if (new_offset < inode->i_size)
1817220f2ac9SHugh Dickins 				offset = new_offset;
1818965c8e59SAndrew Morton 			else if (whence == SEEK_DATA)
1819220f2ac9SHugh Dickins 				offset = -ENXIO;
1820220f2ac9SHugh Dickins 			else
1821220f2ac9SHugh Dickins 				offset = inode->i_size;
1822220f2ac9SHugh Dickins 		}
1823220f2ac9SHugh Dickins 	}
1824220f2ac9SHugh Dickins 
1825387aae6fSHugh Dickins 	if (offset >= 0)
182646a1c2c7SJie Liu 		offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
1827220f2ac9SHugh Dickins 	mutex_unlock(&inode->i_mutex);
1828220f2ac9SHugh Dickins 	return offset;
1829220f2ac9SHugh Dickins }
1830220f2ac9SHugh Dickins 
183140e041a2SDavid Herrmann static int shmem_wait_for_pins(struct address_space *mapping)
183240e041a2SDavid Herrmann {
183340e041a2SDavid Herrmann 	return 0;
183440e041a2SDavid Herrmann }
183540e041a2SDavid Herrmann 
183640e041a2SDavid Herrmann #define F_ALL_SEALS (F_SEAL_SEAL | \
183740e041a2SDavid Herrmann 		     F_SEAL_SHRINK | \
183840e041a2SDavid Herrmann 		     F_SEAL_GROW | \
183940e041a2SDavid Herrmann 		     F_SEAL_WRITE)
184040e041a2SDavid Herrmann 
184140e041a2SDavid Herrmann int shmem_add_seals(struct file *file, unsigned int seals)
184240e041a2SDavid Herrmann {
184340e041a2SDavid Herrmann 	struct inode *inode = file_inode(file);
184440e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
184540e041a2SDavid Herrmann 	int error;
184640e041a2SDavid Herrmann 
184740e041a2SDavid Herrmann 	/*
184840e041a2SDavid Herrmann 	 * SEALING
184940e041a2SDavid Herrmann 	 * Sealing allows multiple parties to share a shmem-file but restrict
185040e041a2SDavid Herrmann 	 * access to a specific subset of file operations. Seals can only be
185140e041a2SDavid Herrmann 	 * added, but never removed. This way, mutually untrusted parties can
185240e041a2SDavid Herrmann 	 * share common memory regions with a well-defined policy. A malicious
185340e041a2SDavid Herrmann 	 * peer can thus never perform unwanted operations on a shared object.
185440e041a2SDavid Herrmann 	 *
185540e041a2SDavid Herrmann 	 * Seals are only supported on special shmem-files and always affect
185640e041a2SDavid Herrmann 	 * the whole underlying inode. Once a seal is set, it may prevent some
185740e041a2SDavid Herrmann 	 * kinds of access to the file. Currently, the following seals are
185840e041a2SDavid Herrmann 	 * defined:
185940e041a2SDavid Herrmann 	 *   SEAL_SEAL: Prevent further seals from being set on this file
186040e041a2SDavid Herrmann 	 *   SEAL_SHRINK: Prevent the file from shrinking
186140e041a2SDavid Herrmann 	 *   SEAL_GROW: Prevent the file from growing
186240e041a2SDavid Herrmann 	 *   SEAL_WRITE: Prevent write access to the file
186340e041a2SDavid Herrmann 	 *
186440e041a2SDavid Herrmann 	 * As we don't require any trust relationship between two parties, we
186540e041a2SDavid Herrmann 	 * must prevent seals from being removed. Therefore, sealing a file
186640e041a2SDavid Herrmann 	 * only adds a given set of seals to the file, it never touches
186740e041a2SDavid Herrmann 	 * existing seals. Furthermore, the "setting seals"-operation can be
186840e041a2SDavid Herrmann 	 * sealed itself, which basically prevents any further seal from being
186940e041a2SDavid Herrmann 	 * added.
187040e041a2SDavid Herrmann 	 *
187140e041a2SDavid Herrmann 	 * Semantics of sealing are only defined on volatile files. Only
187240e041a2SDavid Herrmann 	 * anonymous shmem files support sealing. More importantly, seals are
187340e041a2SDavid Herrmann 	 * never written to disk. Therefore, there's no plan to support it on
187440e041a2SDavid Herrmann 	 * other file types.
187540e041a2SDavid Herrmann 	 */
187640e041a2SDavid Herrmann 
187740e041a2SDavid Herrmann 	if (file->f_op != &shmem_file_operations)
187840e041a2SDavid Herrmann 		return -EINVAL;
187940e041a2SDavid Herrmann 	if (!(file->f_mode & FMODE_WRITE))
188040e041a2SDavid Herrmann 		return -EPERM;
188140e041a2SDavid Herrmann 	if (seals & ~(unsigned int)F_ALL_SEALS)
188240e041a2SDavid Herrmann 		return -EINVAL;
188340e041a2SDavid Herrmann 
188440e041a2SDavid Herrmann 	mutex_lock(&inode->i_mutex);
188540e041a2SDavid Herrmann 
188640e041a2SDavid Herrmann 	if (info->seals & F_SEAL_SEAL) {
188740e041a2SDavid Herrmann 		error = -EPERM;
188840e041a2SDavid Herrmann 		goto unlock;
188940e041a2SDavid Herrmann 	}
189040e041a2SDavid Herrmann 
189140e041a2SDavid Herrmann 	if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
189240e041a2SDavid Herrmann 		error = mapping_deny_writable(file->f_mapping);
189340e041a2SDavid Herrmann 		if (error)
189440e041a2SDavid Herrmann 			goto unlock;
189540e041a2SDavid Herrmann 
189640e041a2SDavid Herrmann 		error = shmem_wait_for_pins(file->f_mapping);
189740e041a2SDavid Herrmann 		if (error) {
189840e041a2SDavid Herrmann 			mapping_allow_writable(file->f_mapping);
189940e041a2SDavid Herrmann 			goto unlock;
190040e041a2SDavid Herrmann 		}
190140e041a2SDavid Herrmann 	}
190240e041a2SDavid Herrmann 
190340e041a2SDavid Herrmann 	info->seals |= seals;
190440e041a2SDavid Herrmann 	error = 0;
190540e041a2SDavid Herrmann 
190640e041a2SDavid Herrmann unlock:
190740e041a2SDavid Herrmann 	mutex_unlock(&inode->i_mutex);
190840e041a2SDavid Herrmann 	return error;
190940e041a2SDavid Herrmann }
191040e041a2SDavid Herrmann EXPORT_SYMBOL_GPL(shmem_add_seals);
191140e041a2SDavid Herrmann 
191240e041a2SDavid Herrmann int shmem_get_seals(struct file *file)
191340e041a2SDavid Herrmann {
191440e041a2SDavid Herrmann 	if (file->f_op != &shmem_file_operations)
191540e041a2SDavid Herrmann 		return -EINVAL;
191640e041a2SDavid Herrmann 
191740e041a2SDavid Herrmann 	return SHMEM_I(file_inode(file))->seals;
191840e041a2SDavid Herrmann }
191940e041a2SDavid Herrmann EXPORT_SYMBOL_GPL(shmem_get_seals);
192040e041a2SDavid Herrmann 
192140e041a2SDavid Herrmann long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
192240e041a2SDavid Herrmann {
192340e041a2SDavid Herrmann 	long error;
192440e041a2SDavid Herrmann 
192540e041a2SDavid Herrmann 	switch (cmd) {
192640e041a2SDavid Herrmann 	case F_ADD_SEALS:
192740e041a2SDavid Herrmann 		/* disallow upper 32bit */
192840e041a2SDavid Herrmann 		if (arg > UINT_MAX)
192940e041a2SDavid Herrmann 			return -EINVAL;
193040e041a2SDavid Herrmann 
193140e041a2SDavid Herrmann 		error = shmem_add_seals(file, arg);
193240e041a2SDavid Herrmann 		break;
193340e041a2SDavid Herrmann 	case F_GET_SEALS:
193440e041a2SDavid Herrmann 		error = shmem_get_seals(file);
193540e041a2SDavid Herrmann 		break;
193640e041a2SDavid Herrmann 	default:
193740e041a2SDavid Herrmann 		error = -EINVAL;
193840e041a2SDavid Herrmann 		break;
193940e041a2SDavid Herrmann 	}
194040e041a2SDavid Herrmann 
194140e041a2SDavid Herrmann 	return error;
194240e041a2SDavid Herrmann }
194340e041a2SDavid Herrmann 
194483e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset,
194583e4fa9cSHugh Dickins 							 loff_t len)
194683e4fa9cSHugh Dickins {
1947496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
1948e2d12e22SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
194940e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
19501aac1400SHugh Dickins 	struct shmem_falloc shmem_falloc;
1951e2d12e22SHugh Dickins 	pgoff_t start, index, end;
1952e2d12e22SHugh Dickins 	int error;
195383e4fa9cSHugh Dickins 
195413ace4d0SHugh Dickins 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
195513ace4d0SHugh Dickins 		return -EOPNOTSUPP;
195613ace4d0SHugh Dickins 
195783e4fa9cSHugh Dickins 	mutex_lock(&inode->i_mutex);
195883e4fa9cSHugh Dickins 
195983e4fa9cSHugh Dickins 	if (mode & FALLOC_FL_PUNCH_HOLE) {
196083e4fa9cSHugh Dickins 		struct address_space *mapping = file->f_mapping;
196183e4fa9cSHugh Dickins 		loff_t unmap_start = round_up(offset, PAGE_SIZE);
196283e4fa9cSHugh Dickins 		loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
19638e205f77SHugh Dickins 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
196483e4fa9cSHugh Dickins 
196540e041a2SDavid Herrmann 		/* protected by i_mutex */
196640e041a2SDavid Herrmann 		if (info->seals & F_SEAL_WRITE) {
196740e041a2SDavid Herrmann 			error = -EPERM;
196840e041a2SDavid Herrmann 			goto out;
196940e041a2SDavid Herrmann 		}
197040e041a2SDavid Herrmann 
19718e205f77SHugh Dickins 		shmem_falloc.waitq = &shmem_falloc_waitq;
1972f00cdc6dSHugh Dickins 		shmem_falloc.start = unmap_start >> PAGE_SHIFT;
1973f00cdc6dSHugh Dickins 		shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
1974f00cdc6dSHugh Dickins 		spin_lock(&inode->i_lock);
1975f00cdc6dSHugh Dickins 		inode->i_private = &shmem_falloc;
1976f00cdc6dSHugh Dickins 		spin_unlock(&inode->i_lock);
1977f00cdc6dSHugh Dickins 
197883e4fa9cSHugh Dickins 		if ((u64)unmap_end > (u64)unmap_start)
197983e4fa9cSHugh Dickins 			unmap_mapping_range(mapping, unmap_start,
198083e4fa9cSHugh Dickins 					    1 + unmap_end - unmap_start, 0);
198183e4fa9cSHugh Dickins 		shmem_truncate_range(inode, offset, offset + len - 1);
198283e4fa9cSHugh Dickins 		/* No need to unmap again: hole-punching leaves COWed pages */
19838e205f77SHugh Dickins 
19848e205f77SHugh Dickins 		spin_lock(&inode->i_lock);
19858e205f77SHugh Dickins 		inode->i_private = NULL;
19868e205f77SHugh Dickins 		wake_up_all(&shmem_falloc_waitq);
19878e205f77SHugh Dickins 		spin_unlock(&inode->i_lock);
198883e4fa9cSHugh Dickins 		error = 0;
19898e205f77SHugh Dickins 		goto out;
199083e4fa9cSHugh Dickins 	}
199183e4fa9cSHugh Dickins 
1992e2d12e22SHugh Dickins 	/* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
1993e2d12e22SHugh Dickins 	error = inode_newsize_ok(inode, offset + len);
1994e2d12e22SHugh Dickins 	if (error)
1995e2d12e22SHugh Dickins 		goto out;
1996e2d12e22SHugh Dickins 
199740e041a2SDavid Herrmann 	if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
199840e041a2SDavid Herrmann 		error = -EPERM;
199940e041a2SDavid Herrmann 		goto out;
200040e041a2SDavid Herrmann 	}
200140e041a2SDavid Herrmann 
2002e2d12e22SHugh Dickins 	start = offset >> PAGE_CACHE_SHIFT;
2003e2d12e22SHugh Dickins 	end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2004e2d12e22SHugh Dickins 	/* Try to avoid a swapstorm if len is impossible to satisfy */
2005e2d12e22SHugh Dickins 	if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2006e2d12e22SHugh Dickins 		error = -ENOSPC;
2007e2d12e22SHugh Dickins 		goto out;
2008e2d12e22SHugh Dickins 	}
2009e2d12e22SHugh Dickins 
20108e205f77SHugh Dickins 	shmem_falloc.waitq = NULL;
20111aac1400SHugh Dickins 	shmem_falloc.start = start;
20121aac1400SHugh Dickins 	shmem_falloc.next  = start;
20131aac1400SHugh Dickins 	shmem_falloc.nr_falloced = 0;
20141aac1400SHugh Dickins 	shmem_falloc.nr_unswapped = 0;
20151aac1400SHugh Dickins 	spin_lock(&inode->i_lock);
20161aac1400SHugh Dickins 	inode->i_private = &shmem_falloc;
20171aac1400SHugh Dickins 	spin_unlock(&inode->i_lock);
20181aac1400SHugh Dickins 
2019e2d12e22SHugh Dickins 	for (index = start; index < end; index++) {
2020e2d12e22SHugh Dickins 		struct page *page;
2021e2d12e22SHugh Dickins 
2022e2d12e22SHugh Dickins 		/*
2023e2d12e22SHugh Dickins 		 * Good, the fallocate(2) manpage permits EINTR: we may have
2024e2d12e22SHugh Dickins 		 * been interrupted because we are using up too much memory.
2025e2d12e22SHugh Dickins 		 */
2026e2d12e22SHugh Dickins 		if (signal_pending(current))
2027e2d12e22SHugh Dickins 			error = -EINTR;
20281aac1400SHugh Dickins 		else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
20291aac1400SHugh Dickins 			error = -ENOMEM;
2030e2d12e22SHugh Dickins 		else
20311635f6a7SHugh Dickins 			error = shmem_getpage(inode, index, &page, SGP_FALLOC,
2032e2d12e22SHugh Dickins 									NULL);
2033e2d12e22SHugh Dickins 		if (error) {
20341635f6a7SHugh Dickins 			/* Remove the !PageUptodate pages we added */
20351635f6a7SHugh Dickins 			shmem_undo_range(inode,
20361635f6a7SHugh Dickins 				(loff_t)start << PAGE_CACHE_SHIFT,
20371635f6a7SHugh Dickins 				(loff_t)index << PAGE_CACHE_SHIFT, true);
20381aac1400SHugh Dickins 			goto undone;
2039e2d12e22SHugh Dickins 		}
2040e2d12e22SHugh Dickins 
2041e2d12e22SHugh Dickins 		/*
20421aac1400SHugh Dickins 		 * Inform shmem_writepage() how far we have reached.
20431aac1400SHugh Dickins 		 * No need for lock or barrier: we have the page lock.
20441aac1400SHugh Dickins 		 */
20451aac1400SHugh Dickins 		shmem_falloc.next++;
20461aac1400SHugh Dickins 		if (!PageUptodate(page))
20471aac1400SHugh Dickins 			shmem_falloc.nr_falloced++;
20481aac1400SHugh Dickins 
20491aac1400SHugh Dickins 		/*
20501635f6a7SHugh Dickins 		 * If !PageUptodate, leave it that way so that freeable pages
20511635f6a7SHugh Dickins 		 * can be recognized if we need to rollback on error later.
20521635f6a7SHugh Dickins 		 * But set_page_dirty so that memory pressure will swap rather
2053e2d12e22SHugh Dickins 		 * than free the pages we are allocating (and SGP_CACHE pages
2054e2d12e22SHugh Dickins 		 * might still be clean: we now need to mark those dirty too).
2055e2d12e22SHugh Dickins 		 */
2056e2d12e22SHugh Dickins 		set_page_dirty(page);
2057e2d12e22SHugh Dickins 		unlock_page(page);
2058e2d12e22SHugh Dickins 		page_cache_release(page);
2059e2d12e22SHugh Dickins 		cond_resched();
2060e2d12e22SHugh Dickins 	}
2061e2d12e22SHugh Dickins 
2062e2d12e22SHugh Dickins 	if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2063e2d12e22SHugh Dickins 		i_size_write(inode, offset + len);
2064e2d12e22SHugh Dickins 	inode->i_ctime = CURRENT_TIME;
20651aac1400SHugh Dickins undone:
20661aac1400SHugh Dickins 	spin_lock(&inode->i_lock);
20671aac1400SHugh Dickins 	inode->i_private = NULL;
20681aac1400SHugh Dickins 	spin_unlock(&inode->i_lock);
2069e2d12e22SHugh Dickins out:
207083e4fa9cSHugh Dickins 	mutex_unlock(&inode->i_mutex);
207183e4fa9cSHugh Dickins 	return error;
207283e4fa9cSHugh Dickins }
207383e4fa9cSHugh Dickins 
2074726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
20751da177e4SLinus Torvalds {
2076726c3342SDavid Howells 	struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	buf->f_type = TMPFS_MAGIC;
20791da177e4SLinus Torvalds 	buf->f_bsize = PAGE_CACHE_SIZE;
20801da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
20810edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
20821da177e4SLinus Torvalds 		buf->f_blocks = sbinfo->max_blocks;
208341ffe5d5SHugh Dickins 		buf->f_bavail =
208441ffe5d5SHugh Dickins 		buf->f_bfree  = sbinfo->max_blocks -
208541ffe5d5SHugh Dickins 				percpu_counter_sum(&sbinfo->used_blocks);
20860edd73b3SHugh Dickins 	}
20870edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
20881da177e4SLinus Torvalds 		buf->f_files = sbinfo->max_inodes;
20891da177e4SLinus Torvalds 		buf->f_ffree = sbinfo->free_inodes;
20901da177e4SLinus Torvalds 	}
20911da177e4SLinus Torvalds 	/* else leave those fields 0 like simple_statfs */
20921da177e4SLinus Torvalds 	return 0;
20931da177e4SLinus Torvalds }
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds /*
20961da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
20971da177e4SLinus Torvalds  */
20981da177e4SLinus Torvalds static int
20991a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
21001da177e4SLinus Torvalds {
21010b0a0806SHugh Dickins 	struct inode *inode;
21021da177e4SLinus Torvalds 	int error = -ENOSPC;
21031da177e4SLinus Torvalds 
2104454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
21051da177e4SLinus Torvalds 	if (inode) {
2106feda821eSChristoph Hellwig 		error = simple_acl_create(dir, inode);
2107feda821eSChristoph Hellwig 		if (error)
2108feda821eSChristoph Hellwig 			goto out_iput;
21092a7dba39SEric Paris 		error = security_inode_init_security(inode, dir,
21109d8f13baSMimi Zohar 						     &dentry->d_name,
21116d9d88d0SJarkko Sakkinen 						     shmem_initxattrs, NULL);
2112feda821eSChristoph Hellwig 		if (error && error != -EOPNOTSUPP)
2113feda821eSChristoph Hellwig 			goto out_iput;
211437ec43cdSMimi Zohar 
2115718deb6bSAl Viro 		error = 0;
21161da177e4SLinus Torvalds 		dir->i_size += BOGO_DIRENT_SIZE;
21171da177e4SLinus Torvalds 		dir->i_ctime = dir->i_mtime = CURRENT_TIME;
21181da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
21191da177e4SLinus Torvalds 		dget(dentry); /* Extra count - pin the dentry in core */
21201da177e4SLinus Torvalds 	}
21211da177e4SLinus Torvalds 	return error;
2122feda821eSChristoph Hellwig out_iput:
2123feda821eSChristoph Hellwig 	iput(inode);
2124feda821eSChristoph Hellwig 	return error;
21251da177e4SLinus Torvalds }
21261da177e4SLinus Torvalds 
212760545d0dSAl Viro static int
212860545d0dSAl Viro shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
212960545d0dSAl Viro {
213060545d0dSAl Viro 	struct inode *inode;
213160545d0dSAl Viro 	int error = -ENOSPC;
213260545d0dSAl Viro 
213360545d0dSAl Viro 	inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
213460545d0dSAl Viro 	if (inode) {
213560545d0dSAl Viro 		error = security_inode_init_security(inode, dir,
213660545d0dSAl Viro 						     NULL,
213760545d0dSAl Viro 						     shmem_initxattrs, NULL);
2138feda821eSChristoph Hellwig 		if (error && error != -EOPNOTSUPP)
2139feda821eSChristoph Hellwig 			goto out_iput;
2140feda821eSChristoph Hellwig 		error = simple_acl_create(dir, inode);
2141feda821eSChristoph Hellwig 		if (error)
2142feda821eSChristoph Hellwig 			goto out_iput;
214360545d0dSAl Viro 		d_tmpfile(dentry, inode);
214460545d0dSAl Viro 	}
214560545d0dSAl Viro 	return error;
2146feda821eSChristoph Hellwig out_iput:
2147feda821eSChristoph Hellwig 	iput(inode);
2148feda821eSChristoph Hellwig 	return error;
214960545d0dSAl Viro }
215060545d0dSAl Viro 
215118bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
21521da177e4SLinus Torvalds {
21531da177e4SLinus Torvalds 	int error;
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds 	if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
21561da177e4SLinus Torvalds 		return error;
2157d8c76e6fSDave Hansen 	inc_nlink(dir);
21581da177e4SLinus Torvalds 	return 0;
21591da177e4SLinus Torvalds }
21601da177e4SLinus Torvalds 
21614acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2162ebfc3b49SAl Viro 		bool excl)
21631da177e4SLinus Torvalds {
21641da177e4SLinus Torvalds 	return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
21651da177e4SLinus Torvalds }
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds /*
21681da177e4SLinus Torvalds  * Link a file..
21691da177e4SLinus Torvalds  */
21701da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
21711da177e4SLinus Torvalds {
21721da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
21735b04c689SPavel Emelyanov 	int ret;
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds 	/*
21761da177e4SLinus Torvalds 	 * No ordinary (disk based) filesystem counts links as inodes;
21771da177e4SLinus Torvalds 	 * but each new link needs a new dentry, pinning lowmem, and
21781da177e4SLinus Torvalds 	 * tmpfs dentries cannot be pruned until they are unlinked.
21791da177e4SLinus Torvalds 	 */
21805b04c689SPavel Emelyanov 	ret = shmem_reserve_inode(inode->i_sb);
21815b04c689SPavel Emelyanov 	if (ret)
21825b04c689SPavel Emelyanov 		goto out;
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
21851da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2186d8c76e6fSDave Hansen 	inc_nlink(inode);
21877de9c6eeSAl Viro 	ihold(inode);	/* New dentry reference */
21881da177e4SLinus Torvalds 	dget(dentry);		/* Extra pinning count for the created dentry */
21891da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
21905b04c689SPavel Emelyanov out:
21915b04c689SPavel Emelyanov 	return ret;
21921da177e4SLinus Torvalds }
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry)
21951da177e4SLinus Torvalds {
21961da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
21971da177e4SLinus Torvalds 
21985b04c689SPavel Emelyanov 	if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
21995b04c689SPavel Emelyanov 		shmem_free_inode(inode->i_sb);
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 	dir->i_size -= BOGO_DIRENT_SIZE;
22021da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
22039a53c3a7SDave Hansen 	drop_nlink(inode);
22041da177e4SLinus Torvalds 	dput(dentry);	/* Undo the count from "create" - this does all the work */
22051da177e4SLinus Torvalds 	return 0;
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
22091da177e4SLinus Torvalds {
22101da177e4SLinus Torvalds 	if (!simple_empty(dentry))
22111da177e4SLinus Torvalds 		return -ENOTEMPTY;
22121da177e4SLinus Torvalds 
22139a53c3a7SDave Hansen 	drop_nlink(dentry->d_inode);
22149a53c3a7SDave Hansen 	drop_nlink(dir);
22151da177e4SLinus Torvalds 	return shmem_unlink(dir, dentry);
22161da177e4SLinus Torvalds }
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds /*
22191da177e4SLinus Torvalds  * The VFS layer already does all the dentry stuff for rename,
22201da177e4SLinus Torvalds  * we just have to decrement the usage count for the target if
22211da177e4SLinus Torvalds  * it exists so that the VFS layer correctly free's it when it
22221da177e4SLinus Torvalds  * gets overwritten.
22231da177e4SLinus Torvalds  */
22241da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
22251da177e4SLinus Torvalds {
22261da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
22271da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(inode->i_mode);
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
22301da177e4SLinus Torvalds 		return -ENOTEMPTY;
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
22331da177e4SLinus Torvalds 		(void) shmem_unlink(new_dir, new_dentry);
22341da177e4SLinus Torvalds 		if (they_are_dirs)
22359a53c3a7SDave Hansen 			drop_nlink(old_dir);
22361da177e4SLinus Torvalds 	} else if (they_are_dirs) {
22379a53c3a7SDave Hansen 		drop_nlink(old_dir);
2238d8c76e6fSDave Hansen 		inc_nlink(new_dir);
22391da177e4SLinus Torvalds 	}
22401da177e4SLinus Torvalds 
22411da177e4SLinus Torvalds 	old_dir->i_size -= BOGO_DIRENT_SIZE;
22421da177e4SLinus Torvalds 	new_dir->i_size += BOGO_DIRENT_SIZE;
22431da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime =
22441da177e4SLinus Torvalds 	new_dir->i_ctime = new_dir->i_mtime =
22451da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME;
22461da177e4SLinus Torvalds 	return 0;
22471da177e4SLinus Torvalds }
22481da177e4SLinus Torvalds 
22491da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
22501da177e4SLinus Torvalds {
22511da177e4SLinus Torvalds 	int error;
22521da177e4SLinus Torvalds 	int len;
22531da177e4SLinus Torvalds 	struct inode *inode;
22549276aad6SHugh Dickins 	struct page *page;
22551da177e4SLinus Torvalds 	char *kaddr;
22561da177e4SLinus Torvalds 	struct shmem_inode_info *info;
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds 	len = strlen(symname) + 1;
22591da177e4SLinus Torvalds 	if (len > PAGE_CACHE_SIZE)
22601da177e4SLinus Torvalds 		return -ENAMETOOLONG;
22611da177e4SLinus Torvalds 
2262454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
22631da177e4SLinus Torvalds 	if (!inode)
22641da177e4SLinus Torvalds 		return -ENOSPC;
22651da177e4SLinus Torvalds 
22669d8f13baSMimi Zohar 	error = security_inode_init_security(inode, dir, &dentry->d_name,
22676d9d88d0SJarkko Sakkinen 					     shmem_initxattrs, NULL);
2268570bc1c2SStephen Smalley 	if (error) {
2269570bc1c2SStephen Smalley 		if (error != -EOPNOTSUPP) {
2270570bc1c2SStephen Smalley 			iput(inode);
2271570bc1c2SStephen Smalley 			return error;
2272570bc1c2SStephen Smalley 		}
2273570bc1c2SStephen Smalley 		error = 0;
2274570bc1c2SStephen Smalley 	}
2275570bc1c2SStephen Smalley 
22761da177e4SLinus Torvalds 	info = SHMEM_I(inode);
22771da177e4SLinus Torvalds 	inode->i_size = len-1;
227869f07ec9SHugh Dickins 	if (len <= SHORT_SYMLINK_LEN) {
227969f07ec9SHugh Dickins 		info->symlink = kmemdup(symname, len, GFP_KERNEL);
228069f07ec9SHugh Dickins 		if (!info->symlink) {
228169f07ec9SHugh Dickins 			iput(inode);
228269f07ec9SHugh Dickins 			return -ENOMEM;
228369f07ec9SHugh Dickins 		}
228469f07ec9SHugh Dickins 		inode->i_op = &shmem_short_symlink_operations;
22851da177e4SLinus Torvalds 	} else {
22861da177e4SLinus Torvalds 		error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
22871da177e4SLinus Torvalds 		if (error) {
22881da177e4SLinus Torvalds 			iput(inode);
22891da177e4SLinus Torvalds 			return error;
22901da177e4SLinus Torvalds 		}
229114fcc23fSHugh Dickins 		inode->i_mapping->a_ops = &shmem_aops;
22921da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inode_operations;
22939b04c5feSCong Wang 		kaddr = kmap_atomic(page);
22941da177e4SLinus Torvalds 		memcpy(kaddr, symname, len);
22959b04c5feSCong Wang 		kunmap_atomic(kaddr);
2296ec9516fbSHugh Dickins 		SetPageUptodate(page);
22971da177e4SLinus Torvalds 		set_page_dirty(page);
22986746aff7SWu Fengguang 		unlock_page(page);
22991da177e4SLinus Torvalds 		page_cache_release(page);
23001da177e4SLinus Torvalds 	}
23011da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
23021da177e4SLinus Torvalds 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
23031da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
23041da177e4SLinus Torvalds 	dget(dentry);
23051da177e4SLinus Torvalds 	return 0;
23061da177e4SLinus Torvalds }
23071da177e4SLinus Torvalds 
230869f07ec9SHugh Dickins static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
23091da177e4SLinus Torvalds {
231069f07ec9SHugh Dickins 	nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
2311cc314eefSLinus Torvalds 	return NULL;
23121da177e4SLinus Torvalds }
23131da177e4SLinus Torvalds 
2314cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
23151da177e4SLinus Torvalds {
23161da177e4SLinus Torvalds 	struct page *page = NULL;
231741ffe5d5SHugh Dickins 	int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
231841ffe5d5SHugh Dickins 	nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
2319d3602444SHugh Dickins 	if (page)
2320d3602444SHugh Dickins 		unlock_page(page);
2321cc314eefSLinus Torvalds 	return page;
23221da177e4SLinus Torvalds }
23231da177e4SLinus Torvalds 
2324cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
23251da177e4SLinus Torvalds {
23261da177e4SLinus Torvalds 	if (!IS_ERR(nd_get_link(nd))) {
2327cc314eefSLinus Torvalds 		struct page *page = cookie;
23281da177e4SLinus Torvalds 		kunmap(page);
23291da177e4SLinus Torvalds 		mark_page_accessed(page);
23301da177e4SLinus Torvalds 		page_cache_release(page);
23311da177e4SLinus Torvalds 	}
23321da177e4SLinus Torvalds }
23331da177e4SLinus Torvalds 
2334b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2335b09e0fa4SEric Paris /*
2336b09e0fa4SEric Paris  * Superblocks without xattr inode operations may get some security.* xattr
2337b09e0fa4SEric Paris  * support from the LSM "for free". As soon as we have any other xattrs
2338b09e0fa4SEric Paris  * like ACLs, we also need to implement the security.* handlers at
2339b09e0fa4SEric Paris  * filesystem level, though.
2340b09e0fa4SEric Paris  */
2341b09e0fa4SEric Paris 
23426d9d88d0SJarkko Sakkinen /*
23436d9d88d0SJarkko Sakkinen  * Callback for security_inode_init_security() for acquiring xattrs.
23446d9d88d0SJarkko Sakkinen  */
23456d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode,
23466d9d88d0SJarkko Sakkinen 			    const struct xattr *xattr_array,
23476d9d88d0SJarkko Sakkinen 			    void *fs_info)
23486d9d88d0SJarkko Sakkinen {
23496d9d88d0SJarkko Sakkinen 	struct shmem_inode_info *info = SHMEM_I(inode);
23506d9d88d0SJarkko Sakkinen 	const struct xattr *xattr;
235138f38657SAristeu Rozanski 	struct simple_xattr *new_xattr;
23526d9d88d0SJarkko Sakkinen 	size_t len;
23536d9d88d0SJarkko Sakkinen 
23546d9d88d0SJarkko Sakkinen 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
235538f38657SAristeu Rozanski 		new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
23566d9d88d0SJarkko Sakkinen 		if (!new_xattr)
23576d9d88d0SJarkko Sakkinen 			return -ENOMEM;
23586d9d88d0SJarkko Sakkinen 
23596d9d88d0SJarkko Sakkinen 		len = strlen(xattr->name) + 1;
23606d9d88d0SJarkko Sakkinen 		new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
23616d9d88d0SJarkko Sakkinen 					  GFP_KERNEL);
23626d9d88d0SJarkko Sakkinen 		if (!new_xattr->name) {
23636d9d88d0SJarkko Sakkinen 			kfree(new_xattr);
23646d9d88d0SJarkko Sakkinen 			return -ENOMEM;
23656d9d88d0SJarkko Sakkinen 		}
23666d9d88d0SJarkko Sakkinen 
23676d9d88d0SJarkko Sakkinen 		memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
23686d9d88d0SJarkko Sakkinen 		       XATTR_SECURITY_PREFIX_LEN);
23696d9d88d0SJarkko Sakkinen 		memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
23706d9d88d0SJarkko Sakkinen 		       xattr->name, len);
23716d9d88d0SJarkko Sakkinen 
237238f38657SAristeu Rozanski 		simple_xattr_list_add(&info->xattrs, new_xattr);
23736d9d88d0SJarkko Sakkinen 	}
23746d9d88d0SJarkko Sakkinen 
23756d9d88d0SJarkko Sakkinen 	return 0;
23766d9d88d0SJarkko Sakkinen }
23776d9d88d0SJarkko Sakkinen 
2378b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = {
2379b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
2380feda821eSChristoph Hellwig 	&posix_acl_access_xattr_handler,
2381feda821eSChristoph Hellwig 	&posix_acl_default_xattr_handler,
2382b09e0fa4SEric Paris #endif
2383b09e0fa4SEric Paris 	NULL
2384b09e0fa4SEric Paris };
2385b09e0fa4SEric Paris 
2386b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name)
2387b09e0fa4SEric Paris {
2388b09e0fa4SEric Paris 	struct { const char *prefix; size_t len; } arr[] = {
2389b09e0fa4SEric Paris 		{ XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
2390b09e0fa4SEric Paris 		{ XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
2391b09e0fa4SEric Paris 	};
2392b09e0fa4SEric Paris 	int i;
2393b09e0fa4SEric Paris 
2394b09e0fa4SEric Paris 	for (i = 0; i < ARRAY_SIZE(arr); i++) {
2395b09e0fa4SEric Paris 		size_t preflen = arr[i].len;
2396b09e0fa4SEric Paris 		if (strncmp(name, arr[i].prefix, preflen) == 0) {
2397b09e0fa4SEric Paris 			if (!name[preflen])
2398b09e0fa4SEric Paris 				return -EINVAL;
2399b09e0fa4SEric Paris 			return 0;
2400b09e0fa4SEric Paris 		}
2401b09e0fa4SEric Paris 	}
2402b09e0fa4SEric Paris 	return -EOPNOTSUPP;
2403b09e0fa4SEric Paris }
2404b09e0fa4SEric Paris 
2405b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
2406b09e0fa4SEric Paris 			      void *buffer, size_t size)
2407b09e0fa4SEric Paris {
240838f38657SAristeu Rozanski 	struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
2409b09e0fa4SEric Paris 	int err;
2410b09e0fa4SEric Paris 
2411b09e0fa4SEric Paris 	/*
2412b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2413b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2414b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2415b09e0fa4SEric Paris 	 */
2416b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2417b09e0fa4SEric Paris 		return generic_getxattr(dentry, name, buffer, size);
2418b09e0fa4SEric Paris 
2419b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2420b09e0fa4SEric Paris 	if (err)
2421b09e0fa4SEric Paris 		return err;
2422b09e0fa4SEric Paris 
242338f38657SAristeu Rozanski 	return simple_xattr_get(&info->xattrs, name, buffer, size);
2424b09e0fa4SEric Paris }
2425b09e0fa4SEric Paris 
2426b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name,
2427b09e0fa4SEric Paris 			  const void *value, size_t size, int flags)
2428b09e0fa4SEric Paris {
242938f38657SAristeu Rozanski 	struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
2430b09e0fa4SEric Paris 	int err;
2431b09e0fa4SEric Paris 
2432b09e0fa4SEric Paris 	/*
2433b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2434b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2435b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2436b09e0fa4SEric Paris 	 */
2437b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2438b09e0fa4SEric Paris 		return generic_setxattr(dentry, name, value, size, flags);
2439b09e0fa4SEric Paris 
2440b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2441b09e0fa4SEric Paris 	if (err)
2442b09e0fa4SEric Paris 		return err;
2443b09e0fa4SEric Paris 
244438f38657SAristeu Rozanski 	return simple_xattr_set(&info->xattrs, name, value, size, flags);
2445b09e0fa4SEric Paris }
2446b09e0fa4SEric Paris 
2447b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name)
2448b09e0fa4SEric Paris {
244938f38657SAristeu Rozanski 	struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
2450b09e0fa4SEric Paris 	int err;
2451b09e0fa4SEric Paris 
2452b09e0fa4SEric Paris 	/*
2453b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2454b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2455b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2456b09e0fa4SEric Paris 	 */
2457b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2458b09e0fa4SEric Paris 		return generic_removexattr(dentry, name);
2459b09e0fa4SEric Paris 
2460b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2461b09e0fa4SEric Paris 	if (err)
2462b09e0fa4SEric Paris 		return err;
2463b09e0fa4SEric Paris 
246438f38657SAristeu Rozanski 	return simple_xattr_remove(&info->xattrs, name);
2465b09e0fa4SEric Paris }
2466b09e0fa4SEric Paris 
2467b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2468b09e0fa4SEric Paris {
246938f38657SAristeu Rozanski 	struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
247038f38657SAristeu Rozanski 	return simple_xattr_list(&info->xattrs, buffer, size);
2471b09e0fa4SEric Paris }
2472b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */
2473b09e0fa4SEric Paris 
247469f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = {
24751da177e4SLinus Torvalds 	.readlink	= generic_readlink,
247669f07ec9SHugh Dickins 	.follow_link	= shmem_follow_short_symlink,
2477b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2478b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2479b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2480b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2481b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2482b09e0fa4SEric Paris #endif
24831da177e4SLinus Torvalds };
24841da177e4SLinus Torvalds 
248592e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = {
24861da177e4SLinus Torvalds 	.readlink	= generic_readlink,
24871da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link,
24881da177e4SLinus Torvalds 	.put_link	= shmem_put_link,
2489b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2490b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2491b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2492b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2493b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
249439f0247dSAndreas Gruenbacher #endif
2495b09e0fa4SEric Paris };
249639f0247dSAndreas Gruenbacher 
249791828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child)
249891828a40SDavid M. Grimes {
249991828a40SDavid M. Grimes 	return ERR_PTR(-ESTALE);
250091828a40SDavid M. Grimes }
250191828a40SDavid M. Grimes 
250291828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh)
250391828a40SDavid M. Grimes {
250491828a40SDavid M. Grimes 	__u32 *fh = vfh;
250591828a40SDavid M. Grimes 	__u64 inum = fh[2];
250691828a40SDavid M. Grimes 	inum = (inum << 32) | fh[1];
250791828a40SDavid M. Grimes 	return ino->i_ino == inum && fh[0] == ino->i_generation;
250891828a40SDavid M. Grimes }
250991828a40SDavid M. Grimes 
2510480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2511480b116cSChristoph Hellwig 		struct fid *fid, int fh_len, int fh_type)
251291828a40SDavid M. Grimes {
251391828a40SDavid M. Grimes 	struct inode *inode;
2514480b116cSChristoph Hellwig 	struct dentry *dentry = NULL;
251535c2a7f4SHugh Dickins 	u64 inum;
251691828a40SDavid M. Grimes 
2517480b116cSChristoph Hellwig 	if (fh_len < 3)
2518480b116cSChristoph Hellwig 		return NULL;
2519480b116cSChristoph Hellwig 
252035c2a7f4SHugh Dickins 	inum = fid->raw[2];
252135c2a7f4SHugh Dickins 	inum = (inum << 32) | fid->raw[1];
252235c2a7f4SHugh Dickins 
2523480b116cSChristoph Hellwig 	inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2524480b116cSChristoph Hellwig 			shmem_match, fid->raw);
252591828a40SDavid M. Grimes 	if (inode) {
2526480b116cSChristoph Hellwig 		dentry = d_find_alias(inode);
252791828a40SDavid M. Grimes 		iput(inode);
252891828a40SDavid M. Grimes 	}
252991828a40SDavid M. Grimes 
2530480b116cSChristoph Hellwig 	return dentry;
253191828a40SDavid M. Grimes }
253291828a40SDavid M. Grimes 
2533b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
2534b0b0382bSAl Viro 				struct inode *parent)
253591828a40SDavid M. Grimes {
25365fe0c237SAneesh Kumar K.V 	if (*len < 3) {
25375fe0c237SAneesh Kumar K.V 		*len = 3;
253894e07a75SNamjae Jeon 		return FILEID_INVALID;
25395fe0c237SAneesh Kumar K.V 	}
254091828a40SDavid M. Grimes 
25411d3382cbSAl Viro 	if (inode_unhashed(inode)) {
254291828a40SDavid M. Grimes 		/* Unfortunately insert_inode_hash is not idempotent,
254391828a40SDavid M. Grimes 		 * so as we hash inodes here rather than at creation
254491828a40SDavid M. Grimes 		 * time, we need a lock to ensure we only try
254591828a40SDavid M. Grimes 		 * to do it once
254691828a40SDavid M. Grimes 		 */
254791828a40SDavid M. Grimes 		static DEFINE_SPINLOCK(lock);
254891828a40SDavid M. Grimes 		spin_lock(&lock);
25491d3382cbSAl Viro 		if (inode_unhashed(inode))
255091828a40SDavid M. Grimes 			__insert_inode_hash(inode,
255191828a40SDavid M. Grimes 					    inode->i_ino + inode->i_generation);
255291828a40SDavid M. Grimes 		spin_unlock(&lock);
255391828a40SDavid M. Grimes 	}
255491828a40SDavid M. Grimes 
255591828a40SDavid M. Grimes 	fh[0] = inode->i_generation;
255691828a40SDavid M. Grimes 	fh[1] = inode->i_ino;
255791828a40SDavid M. Grimes 	fh[2] = ((__u64)inode->i_ino) >> 32;
255891828a40SDavid M. Grimes 
255991828a40SDavid M. Grimes 	*len = 3;
256091828a40SDavid M. Grimes 	return 1;
256191828a40SDavid M. Grimes }
256291828a40SDavid M. Grimes 
256339655164SChristoph Hellwig static const struct export_operations shmem_export_ops = {
256491828a40SDavid M. Grimes 	.get_parent     = shmem_get_parent,
256591828a40SDavid M. Grimes 	.encode_fh      = shmem_encode_fh,
2566480b116cSChristoph Hellwig 	.fh_to_dentry	= shmem_fh_to_dentry,
256791828a40SDavid M. Grimes };
256891828a40SDavid M. Grimes 
2569680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2570680d794bSakpm@linux-foundation.org 			       bool remount)
25711da177e4SLinus Torvalds {
25721da177e4SLinus Torvalds 	char *this_char, *value, *rest;
257349cd0a5cSGreg Thelen 	struct mempolicy *mpol = NULL;
25748751e039SEric W. Biederman 	uid_t uid;
25758751e039SEric W. Biederman 	gid_t gid;
25761da177e4SLinus Torvalds 
2577b00dc3adSHugh Dickins 	while (options != NULL) {
2578b00dc3adSHugh Dickins 		this_char = options;
2579b00dc3adSHugh Dickins 		for (;;) {
2580b00dc3adSHugh Dickins 			/*
2581b00dc3adSHugh Dickins 			 * NUL-terminate this option: unfortunately,
2582b00dc3adSHugh Dickins 			 * mount options form a comma-separated list,
2583b00dc3adSHugh Dickins 			 * but mpol's nodelist may also contain commas.
2584b00dc3adSHugh Dickins 			 */
2585b00dc3adSHugh Dickins 			options = strchr(options, ',');
2586b00dc3adSHugh Dickins 			if (options == NULL)
2587b00dc3adSHugh Dickins 				break;
2588b00dc3adSHugh Dickins 			options++;
2589b00dc3adSHugh Dickins 			if (!isdigit(*options)) {
2590b00dc3adSHugh Dickins 				options[-1] = '\0';
2591b00dc3adSHugh Dickins 				break;
2592b00dc3adSHugh Dickins 			}
2593b00dc3adSHugh Dickins 		}
25941da177e4SLinus Torvalds 		if (!*this_char)
25951da177e4SLinus Torvalds 			continue;
25961da177e4SLinus Torvalds 		if ((value = strchr(this_char,'=')) != NULL) {
25971da177e4SLinus Torvalds 			*value++ = 0;
25981da177e4SLinus Torvalds 		} else {
25991da177e4SLinus Torvalds 			printk(KERN_ERR
26001da177e4SLinus Torvalds 			    "tmpfs: No value for mount option '%s'\n",
26011da177e4SLinus Torvalds 			    this_char);
260249cd0a5cSGreg Thelen 			goto error;
26031da177e4SLinus Torvalds 		}
26041da177e4SLinus Torvalds 
26051da177e4SLinus Torvalds 		if (!strcmp(this_char,"size")) {
26061da177e4SLinus Torvalds 			unsigned long long size;
26071da177e4SLinus Torvalds 			size = memparse(value,&rest);
26081da177e4SLinus Torvalds 			if (*rest == '%') {
26091da177e4SLinus Torvalds 				size <<= PAGE_SHIFT;
26101da177e4SLinus Torvalds 				size *= totalram_pages;
26111da177e4SLinus Torvalds 				do_div(size, 100);
26121da177e4SLinus Torvalds 				rest++;
26131da177e4SLinus Torvalds 			}
26141da177e4SLinus Torvalds 			if (*rest)
26151da177e4SLinus Torvalds 				goto bad_val;
2616680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks =
2617680d794bSakpm@linux-foundation.org 				DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
26181da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_blocks")) {
2619680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks = memparse(value, &rest);
26201da177e4SLinus Torvalds 			if (*rest)
26211da177e4SLinus Torvalds 				goto bad_val;
26221da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_inodes")) {
2623680d794bSakpm@linux-foundation.org 			sbinfo->max_inodes = memparse(value, &rest);
26241da177e4SLinus Torvalds 			if (*rest)
26251da177e4SLinus Torvalds 				goto bad_val;
26261da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"mode")) {
2627680d794bSakpm@linux-foundation.org 			if (remount)
26281da177e4SLinus Torvalds 				continue;
2629680d794bSakpm@linux-foundation.org 			sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
26301da177e4SLinus Torvalds 			if (*rest)
26311da177e4SLinus Torvalds 				goto bad_val;
26321da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"uid")) {
2633680d794bSakpm@linux-foundation.org 			if (remount)
26341da177e4SLinus Torvalds 				continue;
26358751e039SEric W. Biederman 			uid = simple_strtoul(value, &rest, 0);
26361da177e4SLinus Torvalds 			if (*rest)
26371da177e4SLinus Torvalds 				goto bad_val;
26388751e039SEric W. Biederman 			sbinfo->uid = make_kuid(current_user_ns(), uid);
26398751e039SEric W. Biederman 			if (!uid_valid(sbinfo->uid))
26408751e039SEric W. Biederman 				goto bad_val;
26411da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"gid")) {
2642680d794bSakpm@linux-foundation.org 			if (remount)
26431da177e4SLinus Torvalds 				continue;
26448751e039SEric W. Biederman 			gid = simple_strtoul(value, &rest, 0);
26451da177e4SLinus Torvalds 			if (*rest)
26461da177e4SLinus Torvalds 				goto bad_val;
26478751e039SEric W. Biederman 			sbinfo->gid = make_kgid(current_user_ns(), gid);
26488751e039SEric W. Biederman 			if (!gid_valid(sbinfo->gid))
26498751e039SEric W. Biederman 				goto bad_val;
26507339ff83SRobin Holt 		} else if (!strcmp(this_char,"mpol")) {
265149cd0a5cSGreg Thelen 			mpol_put(mpol);
265249cd0a5cSGreg Thelen 			mpol = NULL;
265349cd0a5cSGreg Thelen 			if (mpol_parse_str(value, &mpol))
26547339ff83SRobin Holt 				goto bad_val;
26551da177e4SLinus Torvalds 		} else {
26561da177e4SLinus Torvalds 			printk(KERN_ERR "tmpfs: Bad mount option %s\n",
26571da177e4SLinus Torvalds 			       this_char);
265849cd0a5cSGreg Thelen 			goto error;
26591da177e4SLinus Torvalds 		}
26601da177e4SLinus Torvalds 	}
266149cd0a5cSGreg Thelen 	sbinfo->mpol = mpol;
26621da177e4SLinus Torvalds 	return 0;
26631da177e4SLinus Torvalds 
26641da177e4SLinus Torvalds bad_val:
26651da177e4SLinus Torvalds 	printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
26661da177e4SLinus Torvalds 	       value, this_char);
266749cd0a5cSGreg Thelen error:
266849cd0a5cSGreg Thelen 	mpol_put(mpol);
26691da177e4SLinus Torvalds 	return 1;
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds }
26721da177e4SLinus Torvalds 
26731da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
26741da177e4SLinus Torvalds {
26751da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2676680d794bSakpm@linux-foundation.org 	struct shmem_sb_info config = *sbinfo;
26770edd73b3SHugh Dickins 	unsigned long inodes;
26780edd73b3SHugh Dickins 	int error = -EINVAL;
26791da177e4SLinus Torvalds 
26805f00110fSGreg Thelen 	config.mpol = NULL;
2681680d794bSakpm@linux-foundation.org 	if (shmem_parse_options(data, &config, true))
26820edd73b3SHugh Dickins 		return error;
26830edd73b3SHugh Dickins 
26840edd73b3SHugh Dickins 	spin_lock(&sbinfo->stat_lock);
26850edd73b3SHugh Dickins 	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
26867e496299STim Chen 	if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
26870edd73b3SHugh Dickins 		goto out;
2688680d794bSakpm@linux-foundation.org 	if (config.max_inodes < inodes)
26890edd73b3SHugh Dickins 		goto out;
26900edd73b3SHugh Dickins 	/*
269154af6042SHugh Dickins 	 * Those tests disallow limited->unlimited while any are in use;
26920edd73b3SHugh Dickins 	 * but we must separately disallow unlimited->limited, because
26930edd73b3SHugh Dickins 	 * in that case we have no record of how much is already in use.
26940edd73b3SHugh Dickins 	 */
2695680d794bSakpm@linux-foundation.org 	if (config.max_blocks && !sbinfo->max_blocks)
26960edd73b3SHugh Dickins 		goto out;
2697680d794bSakpm@linux-foundation.org 	if (config.max_inodes && !sbinfo->max_inodes)
26980edd73b3SHugh Dickins 		goto out;
26990edd73b3SHugh Dickins 
27000edd73b3SHugh Dickins 	error = 0;
2701680d794bSakpm@linux-foundation.org 	sbinfo->max_blocks  = config.max_blocks;
2702680d794bSakpm@linux-foundation.org 	sbinfo->max_inodes  = config.max_inodes;
2703680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = config.max_inodes - inodes;
270471fe804bSLee Schermerhorn 
27055f00110fSGreg Thelen 	/*
27065f00110fSGreg Thelen 	 * Preserve previous mempolicy unless mpol remount option was specified.
27075f00110fSGreg Thelen 	 */
27085f00110fSGreg Thelen 	if (config.mpol) {
270971fe804bSLee Schermerhorn 		mpol_put(sbinfo->mpol);
271071fe804bSLee Schermerhorn 		sbinfo->mpol = config.mpol;	/* transfers initial ref */
27115f00110fSGreg Thelen 	}
27120edd73b3SHugh Dickins out:
27130edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
27140edd73b3SHugh Dickins 	return error;
27151da177e4SLinus Torvalds }
2716680d794bSakpm@linux-foundation.org 
271734c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root)
2718680d794bSakpm@linux-foundation.org {
271934c80b1dSAl Viro 	struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
2720680d794bSakpm@linux-foundation.org 
2721680d794bSakpm@linux-foundation.org 	if (sbinfo->max_blocks != shmem_default_max_blocks())
2722680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",size=%luk",
2723680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2724680d794bSakpm@linux-foundation.org 	if (sbinfo->max_inodes != shmem_default_max_inodes())
2725680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2726680d794bSakpm@linux-foundation.org 	if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
272709208d15SAl Viro 		seq_printf(seq, ",mode=%03ho", sbinfo->mode);
27288751e039SEric W. Biederman 	if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
27298751e039SEric W. Biederman 		seq_printf(seq, ",uid=%u",
27308751e039SEric W. Biederman 				from_kuid_munged(&init_user_ns, sbinfo->uid));
27318751e039SEric W. Biederman 	if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
27328751e039SEric W. Biederman 		seq_printf(seq, ",gid=%u",
27338751e039SEric W. Biederman 				from_kgid_munged(&init_user_ns, sbinfo->gid));
273471fe804bSLee Schermerhorn 	shmem_show_mpol(seq, sbinfo->mpol);
2735680d794bSakpm@linux-foundation.org 	return 0;
2736680d794bSakpm@linux-foundation.org }
2737*9183df25SDavid Herrmann 
2738*9183df25SDavid Herrmann #define MFD_NAME_PREFIX "memfd:"
2739*9183df25SDavid Herrmann #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
2740*9183df25SDavid Herrmann #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
2741*9183df25SDavid Herrmann 
2742*9183df25SDavid Herrmann #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
2743*9183df25SDavid Herrmann 
2744*9183df25SDavid Herrmann SYSCALL_DEFINE2(memfd_create,
2745*9183df25SDavid Herrmann 		const char __user *, uname,
2746*9183df25SDavid Herrmann 		unsigned int, flags)
2747*9183df25SDavid Herrmann {
2748*9183df25SDavid Herrmann 	struct shmem_inode_info *info;
2749*9183df25SDavid Herrmann 	struct file *file;
2750*9183df25SDavid Herrmann 	int fd, error;
2751*9183df25SDavid Herrmann 	char *name;
2752*9183df25SDavid Herrmann 	long len;
2753*9183df25SDavid Herrmann 
2754*9183df25SDavid Herrmann 	if (flags & ~(unsigned int)MFD_ALL_FLAGS)
2755*9183df25SDavid Herrmann 		return -EINVAL;
2756*9183df25SDavid Herrmann 
2757*9183df25SDavid Herrmann 	/* length includes terminating zero */
2758*9183df25SDavid Herrmann 	len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
2759*9183df25SDavid Herrmann 	if (len <= 0)
2760*9183df25SDavid Herrmann 		return -EFAULT;
2761*9183df25SDavid Herrmann 	if (len > MFD_NAME_MAX_LEN + 1)
2762*9183df25SDavid Herrmann 		return -EINVAL;
2763*9183df25SDavid Herrmann 
2764*9183df25SDavid Herrmann 	name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
2765*9183df25SDavid Herrmann 	if (!name)
2766*9183df25SDavid Herrmann 		return -ENOMEM;
2767*9183df25SDavid Herrmann 
2768*9183df25SDavid Herrmann 	strcpy(name, MFD_NAME_PREFIX);
2769*9183df25SDavid Herrmann 	if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
2770*9183df25SDavid Herrmann 		error = -EFAULT;
2771*9183df25SDavid Herrmann 		goto err_name;
2772*9183df25SDavid Herrmann 	}
2773*9183df25SDavid Herrmann 
2774*9183df25SDavid Herrmann 	/* terminating-zero may have changed after strnlen_user() returned */
2775*9183df25SDavid Herrmann 	if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
2776*9183df25SDavid Herrmann 		error = -EFAULT;
2777*9183df25SDavid Herrmann 		goto err_name;
2778*9183df25SDavid Herrmann 	}
2779*9183df25SDavid Herrmann 
2780*9183df25SDavid Herrmann 	fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
2781*9183df25SDavid Herrmann 	if (fd < 0) {
2782*9183df25SDavid Herrmann 		error = fd;
2783*9183df25SDavid Herrmann 		goto err_name;
2784*9183df25SDavid Herrmann 	}
2785*9183df25SDavid Herrmann 
2786*9183df25SDavid Herrmann 	file = shmem_file_setup(name, 0, VM_NORESERVE);
2787*9183df25SDavid Herrmann 	if (IS_ERR(file)) {
2788*9183df25SDavid Herrmann 		error = PTR_ERR(file);
2789*9183df25SDavid Herrmann 		goto err_fd;
2790*9183df25SDavid Herrmann 	}
2791*9183df25SDavid Herrmann 	info = SHMEM_I(file_inode(file));
2792*9183df25SDavid Herrmann 	file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
2793*9183df25SDavid Herrmann 	file->f_flags |= O_RDWR | O_LARGEFILE;
2794*9183df25SDavid Herrmann 	if (flags & MFD_ALLOW_SEALING)
2795*9183df25SDavid Herrmann 		info->seals &= ~F_SEAL_SEAL;
2796*9183df25SDavid Herrmann 
2797*9183df25SDavid Herrmann 	fd_install(fd, file);
2798*9183df25SDavid Herrmann 	kfree(name);
2799*9183df25SDavid Herrmann 	return fd;
2800*9183df25SDavid Herrmann 
2801*9183df25SDavid Herrmann err_fd:
2802*9183df25SDavid Herrmann 	put_unused_fd(fd);
2803*9183df25SDavid Herrmann err_name:
2804*9183df25SDavid Herrmann 	kfree(name);
2805*9183df25SDavid Herrmann 	return error;
2806*9183df25SDavid Herrmann }
2807*9183df25SDavid Herrmann 
2808680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
28091da177e4SLinus Torvalds 
28101da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb)
28111da177e4SLinus Torvalds {
2812602586a8SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2813602586a8SHugh Dickins 
2814602586a8SHugh Dickins 	percpu_counter_destroy(&sbinfo->used_blocks);
281549cd0a5cSGreg Thelen 	mpol_put(sbinfo->mpol);
2816602586a8SHugh Dickins 	kfree(sbinfo);
28171da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
28181da177e4SLinus Torvalds }
28191da177e4SLinus Torvalds 
28202b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent)
28211da177e4SLinus Torvalds {
28221da177e4SLinus Torvalds 	struct inode *inode;
28230edd73b3SHugh Dickins 	struct shmem_sb_info *sbinfo;
2824680d794bSakpm@linux-foundation.org 	int err = -ENOMEM;
2825680d794bSakpm@linux-foundation.org 
2826680d794bSakpm@linux-foundation.org 	/* Round up to L1_CACHE_BYTES to resist false sharing */
2827425fbf04SPekka Enberg 	sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
2828680d794bSakpm@linux-foundation.org 				L1_CACHE_BYTES), GFP_KERNEL);
2829680d794bSakpm@linux-foundation.org 	if (!sbinfo)
2830680d794bSakpm@linux-foundation.org 		return -ENOMEM;
2831680d794bSakpm@linux-foundation.org 
2832680d794bSakpm@linux-foundation.org 	sbinfo->mode = S_IRWXUGO | S_ISVTX;
283376aac0e9SDavid Howells 	sbinfo->uid = current_fsuid();
283476aac0e9SDavid Howells 	sbinfo->gid = current_fsgid();
2835680d794bSakpm@linux-foundation.org 	sb->s_fs_info = sbinfo;
28361da177e4SLinus Torvalds 
28370edd73b3SHugh Dickins #ifdef CONFIG_TMPFS
28381da177e4SLinus Torvalds 	/*
28391da177e4SLinus Torvalds 	 * Per default we only allow half of the physical ram per
28401da177e4SLinus Torvalds 	 * tmpfs instance, limiting inodes to one per page of lowmem;
28411da177e4SLinus Torvalds 	 * but the internal instance is left unlimited.
28421da177e4SLinus Torvalds 	 */
2843ca4e0519SAl Viro 	if (!(sb->s_flags & MS_KERNMOUNT)) {
2844680d794bSakpm@linux-foundation.org 		sbinfo->max_blocks = shmem_default_max_blocks();
2845680d794bSakpm@linux-foundation.org 		sbinfo->max_inodes = shmem_default_max_inodes();
2846680d794bSakpm@linux-foundation.org 		if (shmem_parse_options(data, sbinfo, false)) {
2847680d794bSakpm@linux-foundation.org 			err = -EINVAL;
2848680d794bSakpm@linux-foundation.org 			goto failed;
2849680d794bSakpm@linux-foundation.org 		}
2850ca4e0519SAl Viro 	} else {
2851ca4e0519SAl Viro 		sb->s_flags |= MS_NOUSER;
28521da177e4SLinus Torvalds 	}
285391828a40SDavid M. Grimes 	sb->s_export_op = &shmem_export_ops;
28542f6e38f3SHugh Dickins 	sb->s_flags |= MS_NOSEC;
28550edd73b3SHugh Dickins #else
28560edd73b3SHugh Dickins 	sb->s_flags |= MS_NOUSER;
28570edd73b3SHugh Dickins #endif
28581da177e4SLinus Torvalds 
28591da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
2860602586a8SHugh Dickins 	if (percpu_counter_init(&sbinfo->used_blocks, 0))
2861602586a8SHugh Dickins 		goto failed;
2862680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = sbinfo->max_inodes;
28631da177e4SLinus Torvalds 
2864285b2c4fSHugh Dickins 	sb->s_maxbytes = MAX_LFS_FILESIZE;
28651da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
28661da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
28671da177e4SLinus Torvalds 	sb->s_magic = TMPFS_MAGIC;
28681da177e4SLinus Torvalds 	sb->s_op = &shmem_ops;
2869cfd95a9cSRobin H. Johnson 	sb->s_time_gran = 1;
2870b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
287139f0247dSAndreas Gruenbacher 	sb->s_xattr = shmem_xattr_handlers;
2872b09e0fa4SEric Paris #endif
2873b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
287439f0247dSAndreas Gruenbacher 	sb->s_flags |= MS_POSIXACL;
287539f0247dSAndreas Gruenbacher #endif
28760edd73b3SHugh Dickins 
2877454abafeSDmitry Monakhov 	inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
28781da177e4SLinus Torvalds 	if (!inode)
28791da177e4SLinus Torvalds 		goto failed;
2880680d794bSakpm@linux-foundation.org 	inode->i_uid = sbinfo->uid;
2881680d794bSakpm@linux-foundation.org 	inode->i_gid = sbinfo->gid;
2882318ceed0SAl Viro 	sb->s_root = d_make_root(inode);
2883318ceed0SAl Viro 	if (!sb->s_root)
288448fde701SAl Viro 		goto failed;
28851da177e4SLinus Torvalds 	return 0;
28861da177e4SLinus Torvalds 
28871da177e4SLinus Torvalds failed:
28881da177e4SLinus Torvalds 	shmem_put_super(sb);
28891da177e4SLinus Torvalds 	return err;
28901da177e4SLinus Torvalds }
28911da177e4SLinus Torvalds 
2892fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep;
28931da177e4SLinus Torvalds 
28941da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb)
28951da177e4SLinus Torvalds {
289641ffe5d5SHugh Dickins 	struct shmem_inode_info *info;
289741ffe5d5SHugh Dickins 	info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
289841ffe5d5SHugh Dickins 	if (!info)
28991da177e4SLinus Torvalds 		return NULL;
290041ffe5d5SHugh Dickins 	return &info->vfs_inode;
29011da177e4SLinus Torvalds }
29021da177e4SLinus Torvalds 
290341ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head)
2904fa0d7e3dSNick Piggin {
2905fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
2906fa0d7e3dSNick Piggin 	kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2907fa0d7e3dSNick Piggin }
2908fa0d7e3dSNick Piggin 
29091da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode)
29101da177e4SLinus Torvalds {
291109208d15SAl Viro 	if (S_ISREG(inode->i_mode))
29121da177e4SLinus Torvalds 		mpol_free_shared_policy(&SHMEM_I(inode)->policy);
291341ffe5d5SHugh Dickins 	call_rcu(&inode->i_rcu, shmem_destroy_callback);
29141da177e4SLinus Torvalds }
29151da177e4SLinus Torvalds 
291641ffe5d5SHugh Dickins static void shmem_init_inode(void *foo)
29171da177e4SLinus Torvalds {
291841ffe5d5SHugh Dickins 	struct shmem_inode_info *info = foo;
291941ffe5d5SHugh Dickins 	inode_init_once(&info->vfs_inode);
29201da177e4SLinus Torvalds }
29211da177e4SLinus Torvalds 
292241ffe5d5SHugh Dickins static int shmem_init_inodecache(void)
29231da177e4SLinus Torvalds {
29241da177e4SLinus Torvalds 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
29251da177e4SLinus Torvalds 				sizeof(struct shmem_inode_info),
292641ffe5d5SHugh Dickins 				0, SLAB_PANIC, shmem_init_inode);
29271da177e4SLinus Torvalds 	return 0;
29281da177e4SLinus Torvalds }
29291da177e4SLinus Torvalds 
293041ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void)
29311da177e4SLinus Torvalds {
29321a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(shmem_inode_cachep);
29331da177e4SLinus Torvalds }
29341da177e4SLinus Torvalds 
2935f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = {
29361da177e4SLinus Torvalds 	.writepage	= shmem_writepage,
293776719325SKen Chen 	.set_page_dirty	= __set_page_dirty_no_writeback,
29381da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
2939800d15a5SNick Piggin 	.write_begin	= shmem_write_begin,
2940800d15a5SNick Piggin 	.write_end	= shmem_write_end,
29411da177e4SLinus Torvalds #endif
2942304dbdb7SLee Schermerhorn 	.migratepage	= migrate_page,
2943aa261f54SAndi Kleen 	.error_remove_page = generic_error_remove_page,
29441da177e4SLinus Torvalds };
29451da177e4SLinus Torvalds 
294615ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = {
29471da177e4SLinus Torvalds 	.mmap		= shmem_mmap,
29481da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
2949220f2ac9SHugh Dickins 	.llseek		= shmem_file_llseek,
29502ba5bbedSAl Viro 	.read		= new_sync_read,
29518174202bSAl Viro 	.write		= new_sync_write,
29522ba5bbedSAl Viro 	.read_iter	= shmem_file_read_iter,
29538174202bSAl Viro 	.write_iter	= generic_file_write_iter,
29541b061d92SChristoph Hellwig 	.fsync		= noop_fsync,
2955708e3508SHugh Dickins 	.splice_read	= shmem_file_splice_read,
2956f6cb85d0SAl Viro 	.splice_write	= iter_file_splice_write,
295783e4fa9cSHugh Dickins 	.fallocate	= shmem_fallocate,
29581da177e4SLinus Torvalds #endif
29591da177e4SLinus Torvalds };
29601da177e4SLinus Torvalds 
296192e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = {
296294c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
2963b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2964b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2965b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2966b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2967b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2968feda821eSChristoph Hellwig 	.set_acl	= simple_set_acl,
2969b09e0fa4SEric Paris #endif
29701da177e4SLinus Torvalds };
29711da177e4SLinus Torvalds 
297292e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = {
29731da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
29741da177e4SLinus Torvalds 	.create		= shmem_create,
29751da177e4SLinus Torvalds 	.lookup		= simple_lookup,
29761da177e4SLinus Torvalds 	.link		= shmem_link,
29771da177e4SLinus Torvalds 	.unlink		= shmem_unlink,
29781da177e4SLinus Torvalds 	.symlink	= shmem_symlink,
29791da177e4SLinus Torvalds 	.mkdir		= shmem_mkdir,
29801da177e4SLinus Torvalds 	.rmdir		= shmem_rmdir,
29811da177e4SLinus Torvalds 	.mknod		= shmem_mknod,
29821da177e4SLinus Torvalds 	.rename		= shmem_rename,
298360545d0dSAl Viro 	.tmpfile	= shmem_tmpfile,
29841da177e4SLinus Torvalds #endif
2985b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2986b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2987b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2988b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2989b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2990b09e0fa4SEric Paris #endif
299139f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
299294c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
2993feda821eSChristoph Hellwig 	.set_acl	= simple_set_acl,
299439f0247dSAndreas Gruenbacher #endif
299539f0247dSAndreas Gruenbacher };
299639f0247dSAndreas Gruenbacher 
299792e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = {
2998b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2999b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
3000b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
3001b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
3002b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
3003b09e0fa4SEric Paris #endif
300439f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
300594c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
3006feda821eSChristoph Hellwig 	.set_acl	= simple_set_acl,
300739f0247dSAndreas Gruenbacher #endif
30081da177e4SLinus Torvalds };
30091da177e4SLinus Torvalds 
3010759b9775SHugh Dickins static const struct super_operations shmem_ops = {
30111da177e4SLinus Torvalds 	.alloc_inode	= shmem_alloc_inode,
30121da177e4SLinus Torvalds 	.destroy_inode	= shmem_destroy_inode,
30131da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
30141da177e4SLinus Torvalds 	.statfs		= shmem_statfs,
30151da177e4SLinus Torvalds 	.remount_fs	= shmem_remount_fs,
3016680d794bSakpm@linux-foundation.org 	.show_options	= shmem_show_options,
30171da177e4SLinus Torvalds #endif
30181f895f75SAl Viro 	.evict_inode	= shmem_evict_inode,
30191da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
30201da177e4SLinus Torvalds 	.put_super	= shmem_put_super,
30211da177e4SLinus Torvalds };
30221da177e4SLinus Torvalds 
3023f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = {
302454cb8821SNick Piggin 	.fault		= shmem_fault,
3025d7c17551SNing Qu 	.map_pages	= filemap_map_pages,
30261da177e4SLinus Torvalds #ifdef CONFIG_NUMA
30271da177e4SLinus Torvalds 	.set_policy     = shmem_set_policy,
30281da177e4SLinus Torvalds 	.get_policy     = shmem_get_policy,
30291da177e4SLinus Torvalds #endif
30300b173bc4SKonstantin Khlebnikov 	.remap_pages	= generic_file_remap_pages,
30311da177e4SLinus Torvalds };
30321da177e4SLinus Torvalds 
30333c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type,
30343c26ff6eSAl Viro 	int flags, const char *dev_name, void *data)
30351da177e4SLinus Torvalds {
30363c26ff6eSAl Viro 	return mount_nodev(fs_type, flags, data, shmem_fill_super);
30371da177e4SLinus Torvalds }
30381da177e4SLinus Torvalds 
303941ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = {
30401da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
30411da177e4SLinus Torvalds 	.name		= "tmpfs",
30423c26ff6eSAl Viro 	.mount		= shmem_mount,
30431da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
30442b8576cbSEric W. Biederman 	.fs_flags	= FS_USERNS_MOUNT,
30451da177e4SLinus Torvalds };
30461da177e4SLinus Torvalds 
304741ffe5d5SHugh Dickins int __init shmem_init(void)
30481da177e4SLinus Torvalds {
30491da177e4SLinus Torvalds 	int error;
30501da177e4SLinus Torvalds 
305116203a7aSRob Landley 	/* If rootfs called this, don't re-init */
305216203a7aSRob Landley 	if (shmem_inode_cachep)
305316203a7aSRob Landley 		return 0;
305416203a7aSRob Landley 
3055e0bf68ddSPeter Zijlstra 	error = bdi_init(&shmem_backing_dev_info);
3056e0bf68ddSPeter Zijlstra 	if (error)
3057e0bf68ddSPeter Zijlstra 		goto out4;
3058e0bf68ddSPeter Zijlstra 
305941ffe5d5SHugh Dickins 	error = shmem_init_inodecache();
30601da177e4SLinus Torvalds 	if (error)
30611da177e4SLinus Torvalds 		goto out3;
30621da177e4SLinus Torvalds 
306341ffe5d5SHugh Dickins 	error = register_filesystem(&shmem_fs_type);
30641da177e4SLinus Torvalds 	if (error) {
30651da177e4SLinus Torvalds 		printk(KERN_ERR "Could not register tmpfs\n");
30661da177e4SLinus Torvalds 		goto out2;
30671da177e4SLinus Torvalds 	}
306895dc112aSGreg Kroah-Hartman 
3069ca4e0519SAl Viro 	shm_mnt = kern_mount(&shmem_fs_type);
30701da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt)) {
30711da177e4SLinus Torvalds 		error = PTR_ERR(shm_mnt);
30721da177e4SLinus Torvalds 		printk(KERN_ERR "Could not kern_mount tmpfs\n");
30731da177e4SLinus Torvalds 		goto out1;
30741da177e4SLinus Torvalds 	}
30751da177e4SLinus Torvalds 	return 0;
30761da177e4SLinus Torvalds 
30771da177e4SLinus Torvalds out1:
307841ffe5d5SHugh Dickins 	unregister_filesystem(&shmem_fs_type);
30791da177e4SLinus Torvalds out2:
308041ffe5d5SHugh Dickins 	shmem_destroy_inodecache();
30811da177e4SLinus Torvalds out3:
3082e0bf68ddSPeter Zijlstra 	bdi_destroy(&shmem_backing_dev_info);
3083e0bf68ddSPeter Zijlstra out4:
30841da177e4SLinus Torvalds 	shm_mnt = ERR_PTR(error);
30851da177e4SLinus Torvalds 	return error;
30861da177e4SLinus Torvalds }
3087853ac43aSMatt Mackall 
3088853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */
3089853ac43aSMatt Mackall 
3090853ac43aSMatt Mackall /*
3091853ac43aSMatt Mackall  * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3092853ac43aSMatt Mackall  *
3093853ac43aSMatt Mackall  * This is intended for small system where the benefits of the full
3094853ac43aSMatt Mackall  * shmem code (swap-backed and resource-limited) are outweighed by
3095853ac43aSMatt Mackall  * their complexity. On systems without swap this code should be
3096853ac43aSMatt Mackall  * effectively equivalent, but much lighter weight.
3097853ac43aSMatt Mackall  */
3098853ac43aSMatt Mackall 
309941ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = {
3100853ac43aSMatt Mackall 	.name		= "tmpfs",
31013c26ff6eSAl Viro 	.mount		= ramfs_mount,
3102853ac43aSMatt Mackall 	.kill_sb	= kill_litter_super,
31032b8576cbSEric W. Biederman 	.fs_flags	= FS_USERNS_MOUNT,
3104853ac43aSMatt Mackall };
3105853ac43aSMatt Mackall 
310641ffe5d5SHugh Dickins int __init shmem_init(void)
3107853ac43aSMatt Mackall {
310841ffe5d5SHugh Dickins 	BUG_ON(register_filesystem(&shmem_fs_type) != 0);
3109853ac43aSMatt Mackall 
311041ffe5d5SHugh Dickins 	shm_mnt = kern_mount(&shmem_fs_type);
3111853ac43aSMatt Mackall 	BUG_ON(IS_ERR(shm_mnt));
3112853ac43aSMatt Mackall 
3113853ac43aSMatt Mackall 	return 0;
3114853ac43aSMatt Mackall }
3115853ac43aSMatt Mackall 
311641ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page)
3117853ac43aSMatt Mackall {
3118853ac43aSMatt Mackall 	return 0;
3119853ac43aSMatt Mackall }
3120853ac43aSMatt Mackall 
31213f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user)
31223f96b79aSHugh Dickins {
31233f96b79aSHugh Dickins 	return 0;
31243f96b79aSHugh Dickins }
31253f96b79aSHugh Dickins 
312624513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping)
312724513264SHugh Dickins {
312824513264SHugh Dickins }
312924513264SHugh Dickins 
313041ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
313194c1e62dSHugh Dickins {
313241ffe5d5SHugh Dickins 	truncate_inode_pages_range(inode->i_mapping, lstart, lend);
313394c1e62dSHugh Dickins }
313494c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
313594c1e62dSHugh Dickins 
3136853ac43aSMatt Mackall #define shmem_vm_ops				generic_file_vm_ops
31370b0a0806SHugh Dickins #define shmem_file_operations			ramfs_file_operations
3138454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags)	ramfs_get_inode(sb, dir, mode, dev)
31390b0a0806SHugh Dickins #define shmem_acct_size(flags, size)		0
31400b0a0806SHugh Dickins #define shmem_unacct_size(flags, size)		do {} while (0)
3141853ac43aSMatt Mackall 
3142853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */
3143853ac43aSMatt Mackall 
3144853ac43aSMatt Mackall /* common code */
31451da177e4SLinus Torvalds 
31463451538aSAl Viro static struct dentry_operations anon_ops = {
3147118b2302SAl Viro 	.d_dname = simple_dname
31483451538aSAl Viro };
31493451538aSAl Viro 
3150c7277090SEric Paris static struct file *__shmem_file_setup(const char *name, loff_t size,
3151c7277090SEric Paris 				       unsigned long flags, unsigned int i_flags)
31521da177e4SLinus Torvalds {
31536b4d0b27SAl Viro 	struct file *res;
31541da177e4SLinus Torvalds 	struct inode *inode;
31552c48b9c4SAl Viro 	struct path path;
31563451538aSAl Viro 	struct super_block *sb;
31571da177e4SLinus Torvalds 	struct qstr this;
31581da177e4SLinus Torvalds 
31591da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt))
31606b4d0b27SAl Viro 		return ERR_CAST(shm_mnt);
31611da177e4SLinus Torvalds 
3162285b2c4fSHugh Dickins 	if (size < 0 || size > MAX_LFS_FILESIZE)
31631da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
31641da177e4SLinus Torvalds 
31651da177e4SLinus Torvalds 	if (shmem_acct_size(flags, size))
31661da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
31671da177e4SLinus Torvalds 
31686b4d0b27SAl Viro 	res = ERR_PTR(-ENOMEM);
31691da177e4SLinus Torvalds 	this.name = name;
31701da177e4SLinus Torvalds 	this.len = strlen(name);
31711da177e4SLinus Torvalds 	this.hash = 0; /* will go */
31723451538aSAl Viro 	sb = shm_mnt->mnt_sb;
317366ee4b88SKonstantin Khlebnikov 	path.mnt = mntget(shm_mnt);
31743451538aSAl Viro 	path.dentry = d_alloc_pseudo(sb, &this);
31752c48b9c4SAl Viro 	if (!path.dentry)
31761da177e4SLinus Torvalds 		goto put_memory;
31773451538aSAl Viro 	d_set_d_op(path.dentry, &anon_ops);
31781da177e4SLinus Torvalds 
31796b4d0b27SAl Viro 	res = ERR_PTR(-ENOSPC);
31803451538aSAl Viro 	inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
31811da177e4SLinus Torvalds 	if (!inode)
318266ee4b88SKonstantin Khlebnikov 		goto put_memory;
31831da177e4SLinus Torvalds 
3184c7277090SEric Paris 	inode->i_flags |= i_flags;
31852c48b9c4SAl Viro 	d_instantiate(path.dentry, inode);
31861da177e4SLinus Torvalds 	inode->i_size = size;
31876d6b77f1SMiklos Szeredi 	clear_nlink(inode);	/* It is unlinked */
318826567cdbSAl Viro 	res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
318926567cdbSAl Viro 	if (IS_ERR(res))
319066ee4b88SKonstantin Khlebnikov 		goto put_path;
31914b42af81SAl Viro 
31926b4d0b27SAl Viro 	res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
31934b42af81SAl Viro 		  &shmem_file_operations);
31946b4d0b27SAl Viro 	if (IS_ERR(res))
319566ee4b88SKonstantin Khlebnikov 		goto put_path;
31964b42af81SAl Viro 
31976b4d0b27SAl Viro 	return res;
31981da177e4SLinus Torvalds 
31991da177e4SLinus Torvalds put_memory:
32001da177e4SLinus Torvalds 	shmem_unacct_size(flags, size);
320166ee4b88SKonstantin Khlebnikov put_path:
320266ee4b88SKonstantin Khlebnikov 	path_put(&path);
32036b4d0b27SAl Viro 	return res;
32041da177e4SLinus Torvalds }
3205c7277090SEric Paris 
3206c7277090SEric Paris /**
3207c7277090SEric Paris  * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3208c7277090SEric Paris  * 	kernel internal.  There will be NO LSM permission checks against the
3209c7277090SEric Paris  * 	underlying inode.  So users of this interface must do LSM checks at a
3210c7277090SEric Paris  * 	higher layer.  The one user is the big_key implementation.  LSM checks
3211c7277090SEric Paris  * 	are provided at the key level rather than the inode level.
3212c7277090SEric Paris  * @name: name for dentry (to be seen in /proc/<pid>/maps
3213c7277090SEric Paris  * @size: size to be set for the file
3214c7277090SEric Paris  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3215c7277090SEric Paris  */
3216c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3217c7277090SEric Paris {
3218c7277090SEric Paris 	return __shmem_file_setup(name, size, flags, S_PRIVATE);
3219c7277090SEric Paris }
3220c7277090SEric Paris 
3221c7277090SEric Paris /**
3222c7277090SEric Paris  * shmem_file_setup - get an unlinked file living in tmpfs
3223c7277090SEric Paris  * @name: name for dentry (to be seen in /proc/<pid>/maps
3224c7277090SEric Paris  * @size: size to be set for the file
3225c7277090SEric Paris  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3226c7277090SEric Paris  */
3227c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3228c7277090SEric Paris {
3229c7277090SEric Paris 	return __shmem_file_setup(name, size, flags, 0);
3230c7277090SEric Paris }
3231395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup);
32321da177e4SLinus Torvalds 
323346711810SRandy Dunlap /**
32341da177e4SLinus Torvalds  * shmem_zero_setup - setup a shared anonymous mapping
32351da177e4SLinus Torvalds  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
32361da177e4SLinus Torvalds  */
32371da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma)
32381da177e4SLinus Torvalds {
32391da177e4SLinus Torvalds 	struct file *file;
32401da177e4SLinus Torvalds 	loff_t size = vma->vm_end - vma->vm_start;
32411da177e4SLinus Torvalds 
32421da177e4SLinus Torvalds 	file = shmem_file_setup("dev/zero", size, vma->vm_flags);
32431da177e4SLinus Torvalds 	if (IS_ERR(file))
32441da177e4SLinus Torvalds 		return PTR_ERR(file);
32451da177e4SLinus Torvalds 
32461da177e4SLinus Torvalds 	if (vma->vm_file)
32471da177e4SLinus Torvalds 		fput(vma->vm_file);
32481da177e4SLinus Torvalds 	vma->vm_file = file;
32491da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
32501da177e4SLinus Torvalds 	return 0;
32511da177e4SLinus Torvalds }
3252d9d90e5eSHugh Dickins 
3253d9d90e5eSHugh Dickins /**
3254d9d90e5eSHugh Dickins  * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3255d9d90e5eSHugh Dickins  * @mapping:	the page's address_space
3256d9d90e5eSHugh Dickins  * @index:	the page index
3257d9d90e5eSHugh Dickins  * @gfp:	the page allocator flags to use if allocating
3258d9d90e5eSHugh Dickins  *
3259d9d90e5eSHugh Dickins  * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3260d9d90e5eSHugh Dickins  * with any new page allocations done using the specified allocation flags.
3261d9d90e5eSHugh Dickins  * But read_cache_page_gfp() uses the ->readpage() method: which does not
3262d9d90e5eSHugh Dickins  * suit tmpfs, since it may have pages in swapcache, and needs to find those
3263d9d90e5eSHugh Dickins  * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
3264d9d90e5eSHugh Dickins  *
326568da9f05SHugh Dickins  * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
326668da9f05SHugh Dickins  * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
3267d9d90e5eSHugh Dickins  */
3268d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
3269d9d90e5eSHugh Dickins 					 pgoff_t index, gfp_t gfp)
3270d9d90e5eSHugh Dickins {
327168da9f05SHugh Dickins #ifdef CONFIG_SHMEM
327268da9f05SHugh Dickins 	struct inode *inode = mapping->host;
32739276aad6SHugh Dickins 	struct page *page;
327468da9f05SHugh Dickins 	int error;
327568da9f05SHugh Dickins 
327668da9f05SHugh Dickins 	BUG_ON(mapping->a_ops != &shmem_aops);
327768da9f05SHugh Dickins 	error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
327868da9f05SHugh Dickins 	if (error)
327968da9f05SHugh Dickins 		page = ERR_PTR(error);
328068da9f05SHugh Dickins 	else
328168da9f05SHugh Dickins 		unlock_page(page);
328268da9f05SHugh Dickins 	return page;
328368da9f05SHugh Dickins #else
328468da9f05SHugh Dickins 	/*
328568da9f05SHugh Dickins 	 * The tiny !SHMEM case uses ramfs without swap
328668da9f05SHugh Dickins 	 */
3287d9d90e5eSHugh Dickins 	return read_cache_page_gfp(mapping, index, gfp);
328868da9f05SHugh Dickins #endif
3289d9d90e5eSHugh Dickins }
3290d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);
3291