xref: /openbmc/linux/mm/shmem.c (revision 5955102c9984fa081b2d570cfac75c97eecf8f3b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Resizable virtual memory filesystem for Linux.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2000 Linus Torvalds.
51da177e4SLinus Torvalds  *		 2000 Transmeta Corp.
61da177e4SLinus Torvalds  *		 2000-2001 Christoph Rohland
71da177e4SLinus Torvalds  *		 2000-2001 SAP AG
81da177e4SLinus Torvalds  *		 2002 Red Hat Inc.
96922c0c7SHugh Dickins  * Copyright (C) 2002-2011 Hugh Dickins.
106922c0c7SHugh Dickins  * Copyright (C) 2011 Google Inc.
110edd73b3SHugh Dickins  * Copyright (C) 2002-2005 VERITAS Software Corporation.
121da177e4SLinus Torvalds  * Copyright (C) 2004 Andi Kleen, SuSE Labs
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * Extended attribute support for tmpfs:
151da177e4SLinus Torvalds  * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
161da177e4SLinus Torvalds  * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
171da177e4SLinus Torvalds  *
18853ac43aSMatt Mackall  * tiny-shmem:
19853ac43aSMatt Mackall  * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
20853ac43aSMatt Mackall  *
211da177e4SLinus Torvalds  * This file is released under the GPL.
221da177e4SLinus Torvalds  */
231da177e4SLinus Torvalds 
24853ac43aSMatt Mackall #include <linux/fs.h>
25853ac43aSMatt Mackall #include <linux/init.h>
26853ac43aSMatt Mackall #include <linux/vfs.h>
27853ac43aSMatt Mackall #include <linux/mount.h>
28250297edSAndrew Morton #include <linux/ramfs.h>
29caefba17SHugh Dickins #include <linux/pagemap.h>
30853ac43aSMatt Mackall #include <linux/file.h>
31853ac43aSMatt Mackall #include <linux/mm.h>
32b95f1b31SPaul Gortmaker #include <linux/export.h>
33853ac43aSMatt Mackall #include <linux/swap.h>
34e2e40f2cSChristoph Hellwig #include <linux/uio.h>
35853ac43aSMatt Mackall 
36853ac43aSMatt Mackall static struct vfsmount *shm_mnt;
37853ac43aSMatt Mackall 
38853ac43aSMatt Mackall #ifdef CONFIG_SHMEM
391da177e4SLinus Torvalds /*
401da177e4SLinus Torvalds  * This virtual memory filesystem is heavily based on the ramfs. It
411da177e4SLinus Torvalds  * extends ramfs by the ability to use swap and honor resource limits
421da177e4SLinus Torvalds  * which makes it a completely usable filesystem.
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds 
4539f0247dSAndreas Gruenbacher #include <linux/xattr.h>
46a5694255SChristoph Hellwig #include <linux/exportfs.h>
471c7c474cSChristoph Hellwig #include <linux/posix_acl.h>
48feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h>
491da177e4SLinus Torvalds #include <linux/mman.h>
501da177e4SLinus Torvalds #include <linux/string.h>
511da177e4SLinus Torvalds #include <linux/slab.h>
521da177e4SLinus Torvalds #include <linux/backing-dev.h>
531da177e4SLinus Torvalds #include <linux/shmem_fs.h>
541da177e4SLinus Torvalds #include <linux/writeback.h>
551da177e4SLinus Torvalds #include <linux/blkdev.h>
56bda97eabSHugh Dickins #include <linux/pagevec.h>
5741ffe5d5SHugh Dickins #include <linux/percpu_counter.h>
5883e4fa9cSHugh Dickins #include <linux/falloc.h>
59708e3508SHugh Dickins #include <linux/splice.h>
601da177e4SLinus Torvalds #include <linux/security.h>
611da177e4SLinus Torvalds #include <linux/swapops.h>
621da177e4SLinus Torvalds #include <linux/mempolicy.h>
631da177e4SLinus Torvalds #include <linux/namei.h>
64b00dc3adSHugh Dickins #include <linux/ctype.h>
65304dbdb7SLee Schermerhorn #include <linux/migrate.h>
66c1f60a5aSChristoph Lameter #include <linux/highmem.h>
67680d794bSakpm@linux-foundation.org #include <linux/seq_file.h>
6892562927SMimi Zohar #include <linux/magic.h>
699183df25SDavid Herrmann #include <linux/syscalls.h>
7040e041a2SDavid Herrmann #include <linux/fcntl.h>
719183df25SDavid Herrmann #include <uapi/linux/memfd.h>
72304dbdb7SLee Schermerhorn 
731da177e4SLinus Torvalds #include <asm/uaccess.h>
741da177e4SLinus Torvalds #include <asm/pgtable.h>
751da177e4SLinus Torvalds 
76dd56b046SMel Gorman #include "internal.h"
77dd56b046SMel Gorman 
781da177e4SLinus Torvalds #define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
791da177e4SLinus Torvalds #define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */
821da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20
831da177e4SLinus Torvalds 
8469f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
8569f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128
8669f07ec9SHugh Dickins 
871aac1400SHugh Dickins /*
88f00cdc6dSHugh Dickins  * shmem_fallocate communicates with shmem_fault or shmem_writepage via
89f00cdc6dSHugh Dickins  * inode->i_private (with i_mutex making sure that it has only one user at
90f00cdc6dSHugh Dickins  * a time): we would prefer not to enlarge the shmem inode just for that.
911aac1400SHugh Dickins  */
921aac1400SHugh Dickins struct shmem_falloc {
938e205f77SHugh Dickins 	wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
941aac1400SHugh Dickins 	pgoff_t start;		/* start of range currently being fallocated */
951aac1400SHugh Dickins 	pgoff_t next;		/* the next page offset to be fallocated */
961aac1400SHugh Dickins 	pgoff_t nr_falloced;	/* how many new pages have been fallocated */
971aac1400SHugh Dickins 	pgoff_t nr_unswapped;	/* how often writepage refused to swap out */
981aac1400SHugh Dickins };
991aac1400SHugh Dickins 
100285b2c4fSHugh Dickins /* Flag allocation requirements to shmem_getpage */
1011da177e4SLinus Torvalds enum sgp_type {
1021da177e4SLinus Torvalds 	SGP_READ,	/* don't exceed i_size, don't allocate page */
1031da177e4SLinus Torvalds 	SGP_CACHE,	/* don't exceed i_size, may allocate page */
104a0ee5ec5SHugh Dickins 	SGP_DIRTY,	/* like SGP_CACHE, but set new page dirty */
1051635f6a7SHugh Dickins 	SGP_WRITE,	/* may exceed i_size, may allocate !Uptodate page */
1061635f6a7SHugh Dickins 	SGP_FALLOC,	/* like SGP_WRITE, but make existing page Uptodate */
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds 
109b76db735SAndrew Morton #ifdef CONFIG_TMPFS
110680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void)
111680d794bSakpm@linux-foundation.org {
112680d794bSakpm@linux-foundation.org 	return totalram_pages / 2;
113680d794bSakpm@linux-foundation.org }
114680d794bSakpm@linux-foundation.org 
115680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void)
116680d794bSakpm@linux-foundation.org {
117680d794bSakpm@linux-foundation.org 	return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
118680d794bSakpm@linux-foundation.org }
119b76db735SAndrew Morton #endif
120680d794bSakpm@linux-foundation.org 
121bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
122bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp,
123bde05d1cSHugh Dickins 				struct shmem_inode_info *info, pgoff_t index);
12468da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
12568da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
12668da9f05SHugh Dickins 
12768da9f05SHugh Dickins static inline int shmem_getpage(struct inode *inode, pgoff_t index,
12868da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, int *fault_type)
12968da9f05SHugh Dickins {
13068da9f05SHugh Dickins 	return shmem_getpage_gfp(inode, index, pagep, sgp,
13168da9f05SHugh Dickins 			mapping_gfp_mask(inode->i_mapping), fault_type);
13268da9f05SHugh Dickins }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	return sb->s_fs_info;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds  * shmem_file_setup pre-accounts the whole fixed size of a VM object,
1411da177e4SLinus Torvalds  * for shared memory and for shared anonymous (/dev/zero) mappings
1421da177e4SLinus Torvalds  * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
1431da177e4SLinus Torvalds  * consistent with the pre-accounting of private mappings ...
1441da177e4SLinus Torvalds  */
1451da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size)
1461da177e4SLinus Torvalds {
1470b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
148191c5424SAl Viro 		0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size)
1521da177e4SLinus Torvalds {
1530b0a0806SHugh Dickins 	if (!(flags & VM_NORESERVE))
1541da177e4SLinus Torvalds 		vm_unacct_memory(VM_ACCT(size));
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
15777142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags,
15877142517SKonstantin Khlebnikov 		loff_t oldsize, loff_t newsize)
15977142517SKonstantin Khlebnikov {
16077142517SKonstantin Khlebnikov 	if (!(flags & VM_NORESERVE)) {
16177142517SKonstantin Khlebnikov 		if (VM_ACCT(newsize) > VM_ACCT(oldsize))
16277142517SKonstantin Khlebnikov 			return security_vm_enough_memory_mm(current->mm,
16377142517SKonstantin Khlebnikov 					VM_ACCT(newsize) - VM_ACCT(oldsize));
16477142517SKonstantin Khlebnikov 		else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
16577142517SKonstantin Khlebnikov 			vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
16677142517SKonstantin Khlebnikov 	}
16777142517SKonstantin Khlebnikov 	return 0;
16877142517SKonstantin Khlebnikov }
16977142517SKonstantin Khlebnikov 
1701da177e4SLinus Torvalds /*
1711da177e4SLinus Torvalds  * ... whereas tmpfs objects are accounted incrementally as
1721da177e4SLinus Torvalds  * pages are allocated, in order to allow huge sparse files.
1731da177e4SLinus Torvalds  * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
1741da177e4SLinus Torvalds  * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
1751da177e4SLinus Torvalds  */
1761da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags)
1771da177e4SLinus Torvalds {
1780b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
179191c5424SAl Viro 		security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages)
1831da177e4SLinus Torvalds {
1840b0a0806SHugh Dickins 	if (flags & VM_NORESERVE)
1851da177e4SLinus Torvalds 		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
188759b9775SHugh Dickins static const struct super_operations shmem_ops;
189f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops;
19015ad7cdcSHelge Deller static const struct file_operations shmem_file_operations;
19192e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations;
19292e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations;
19392e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations;
194f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist);
197cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex);
1981da177e4SLinus Torvalds 
1995b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb)
2005b04c689SPavel Emelyanov {
2015b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2025b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2035b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2045b04c689SPavel Emelyanov 		if (!sbinfo->free_inodes) {
2055b04c689SPavel Emelyanov 			spin_unlock(&sbinfo->stat_lock);
2065b04c689SPavel Emelyanov 			return -ENOSPC;
2075b04c689SPavel Emelyanov 		}
2085b04c689SPavel Emelyanov 		sbinfo->free_inodes--;
2095b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2105b04c689SPavel Emelyanov 	}
2115b04c689SPavel Emelyanov 	return 0;
2125b04c689SPavel Emelyanov }
2135b04c689SPavel Emelyanov 
2145b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb)
2155b04c689SPavel Emelyanov {
2165b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2175b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2185b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2195b04c689SPavel Emelyanov 		sbinfo->free_inodes++;
2205b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2215b04c689SPavel Emelyanov 	}
2225b04c689SPavel Emelyanov }
2235b04c689SPavel Emelyanov 
22446711810SRandy Dunlap /**
22541ffe5d5SHugh Dickins  * shmem_recalc_inode - recalculate the block usage of an inode
2261da177e4SLinus Torvalds  * @inode: inode to recalc
2271da177e4SLinus Torvalds  *
2281da177e4SLinus Torvalds  * We have to calculate the free blocks since the mm can drop
2291da177e4SLinus Torvalds  * undirtied hole pages behind our back.
2301da177e4SLinus Torvalds  *
2311da177e4SLinus Torvalds  * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
2321da177e4SLinus Torvalds  * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
2331da177e4SLinus Torvalds  *
2341da177e4SLinus Torvalds  * It has to be called with the spinlock held.
2351da177e4SLinus Torvalds  */
2361da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
2391da177e4SLinus Torvalds 	long freed;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
2421da177e4SLinus Torvalds 	if (freed > 0) {
24354af6042SHugh Dickins 		struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
24454af6042SHugh Dickins 		if (sbinfo->max_blocks)
24554af6042SHugh Dickins 			percpu_counter_add(&sbinfo->used_blocks, -freed);
2461da177e4SLinus Torvalds 		info->alloced -= freed;
24754af6042SHugh Dickins 		inode->i_blocks -= freed * BLOCKS_PER_PAGE;
2481da177e4SLinus Torvalds 		shmem_unacct_blocks(info->flags, freed);
2491da177e4SLinus Torvalds 	}
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
2527a5d0fbbSHugh Dickins /*
2537a5d0fbbSHugh Dickins  * Replace item expected in radix tree by a new item, while holding tree lock.
2547a5d0fbbSHugh Dickins  */
2557a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping,
2567a5d0fbbSHugh Dickins 			pgoff_t index, void *expected, void *replacement)
2577a5d0fbbSHugh Dickins {
2587a5d0fbbSHugh Dickins 	void **pslot;
2596dbaf22cSJohannes Weiner 	void *item;
2607a5d0fbbSHugh Dickins 
2617a5d0fbbSHugh Dickins 	VM_BUG_ON(!expected);
2626dbaf22cSJohannes Weiner 	VM_BUG_ON(!replacement);
2637a5d0fbbSHugh Dickins 	pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
2646dbaf22cSJohannes Weiner 	if (!pslot)
2656dbaf22cSJohannes Weiner 		return -ENOENT;
2666dbaf22cSJohannes Weiner 	item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock);
2677a5d0fbbSHugh Dickins 	if (item != expected)
2687a5d0fbbSHugh Dickins 		return -ENOENT;
2697a5d0fbbSHugh Dickins 	radix_tree_replace_slot(pslot, replacement);
2707a5d0fbbSHugh Dickins 	return 0;
2717a5d0fbbSHugh Dickins }
2727a5d0fbbSHugh Dickins 
2737a5d0fbbSHugh Dickins /*
274d1899228SHugh Dickins  * Sometimes, before we decide whether to proceed or to fail, we must check
275d1899228SHugh Dickins  * that an entry was not already brought back from swap by a racing thread.
276d1899228SHugh Dickins  *
277d1899228SHugh Dickins  * Checking page is not enough: by the time a SwapCache page is locked, it
278d1899228SHugh Dickins  * might be reused, and again be SwapCache, using the same swap as before.
279d1899228SHugh Dickins  */
280d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping,
281d1899228SHugh Dickins 			       pgoff_t index, swp_entry_t swap)
282d1899228SHugh Dickins {
283d1899228SHugh Dickins 	void *item;
284d1899228SHugh Dickins 
285d1899228SHugh Dickins 	rcu_read_lock();
286d1899228SHugh Dickins 	item = radix_tree_lookup(&mapping->page_tree, index);
287d1899228SHugh Dickins 	rcu_read_unlock();
288d1899228SHugh Dickins 	return item == swp_to_radix_entry(swap);
289d1899228SHugh Dickins }
290d1899228SHugh Dickins 
291d1899228SHugh Dickins /*
29246f65ec1SHugh Dickins  * Like add_to_page_cache_locked, but error if expected item has gone.
29346f65ec1SHugh Dickins  */
29446f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page,
29546f65ec1SHugh Dickins 				   struct address_space *mapping,
296fed400a1SWang Sheng-Hui 				   pgoff_t index, void *expected)
29746f65ec1SHugh Dickins {
298b065b432SHugh Dickins 	int error;
29946f65ec1SHugh Dickins 
300309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageLocked(page), page);
301309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
30246f65ec1SHugh Dickins 
30346f65ec1SHugh Dickins 	page_cache_get(page);
30446f65ec1SHugh Dickins 	page->mapping = mapping;
30546f65ec1SHugh Dickins 	page->index = index;
30646f65ec1SHugh Dickins 
30746f65ec1SHugh Dickins 	spin_lock_irq(&mapping->tree_lock);
30846f65ec1SHugh Dickins 	if (!expected)
309b065b432SHugh Dickins 		error = radix_tree_insert(&mapping->page_tree, index, page);
31046f65ec1SHugh Dickins 	else
311b065b432SHugh Dickins 		error = shmem_radix_tree_replace(mapping, index, expected,
312b065b432SHugh Dickins 								 page);
31346f65ec1SHugh Dickins 	if (!error) {
31446f65ec1SHugh Dickins 		mapping->nrpages++;
31546f65ec1SHugh Dickins 		__inc_zone_page_state(page, NR_FILE_PAGES);
31646f65ec1SHugh Dickins 		__inc_zone_page_state(page, NR_SHMEM);
31746f65ec1SHugh Dickins 		spin_unlock_irq(&mapping->tree_lock);
31846f65ec1SHugh Dickins 	} else {
31946f65ec1SHugh Dickins 		page->mapping = NULL;
32046f65ec1SHugh Dickins 		spin_unlock_irq(&mapping->tree_lock);
32146f65ec1SHugh Dickins 		page_cache_release(page);
32246f65ec1SHugh Dickins 	}
32346f65ec1SHugh Dickins 	return error;
32446f65ec1SHugh Dickins }
32546f65ec1SHugh Dickins 
32646f65ec1SHugh Dickins /*
3276922c0c7SHugh Dickins  * Like delete_from_page_cache, but substitutes swap for page.
3286922c0c7SHugh Dickins  */
3296922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap)
3306922c0c7SHugh Dickins {
3316922c0c7SHugh Dickins 	struct address_space *mapping = page->mapping;
3326922c0c7SHugh Dickins 	int error;
3336922c0c7SHugh Dickins 
3346922c0c7SHugh Dickins 	spin_lock_irq(&mapping->tree_lock);
3356922c0c7SHugh Dickins 	error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
3366922c0c7SHugh Dickins 	page->mapping = NULL;
3376922c0c7SHugh Dickins 	mapping->nrpages--;
3386922c0c7SHugh Dickins 	__dec_zone_page_state(page, NR_FILE_PAGES);
3396922c0c7SHugh Dickins 	__dec_zone_page_state(page, NR_SHMEM);
3406922c0c7SHugh Dickins 	spin_unlock_irq(&mapping->tree_lock);
3416922c0c7SHugh Dickins 	page_cache_release(page);
3426922c0c7SHugh Dickins 	BUG_ON(error);
3436922c0c7SHugh Dickins }
3446922c0c7SHugh Dickins 
3456922c0c7SHugh Dickins /*
3467a5d0fbbSHugh Dickins  * Remove swap entry from radix tree, free the swap and its page cache.
3477a5d0fbbSHugh Dickins  */
3487a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping,
3497a5d0fbbSHugh Dickins 			   pgoff_t index, void *radswap)
3507a5d0fbbSHugh Dickins {
3516dbaf22cSJohannes Weiner 	void *old;
3527a5d0fbbSHugh Dickins 
3537a5d0fbbSHugh Dickins 	spin_lock_irq(&mapping->tree_lock);
3546dbaf22cSJohannes Weiner 	old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
3557a5d0fbbSHugh Dickins 	spin_unlock_irq(&mapping->tree_lock);
3566dbaf22cSJohannes Weiner 	if (old != radswap)
3576dbaf22cSJohannes Weiner 		return -ENOENT;
3587a5d0fbbSHugh Dickins 	free_swap_and_cache(radix_to_swp_entry(radswap));
3596dbaf22cSJohannes Weiner 	return 0;
3607a5d0fbbSHugh Dickins }
3617a5d0fbbSHugh Dickins 
3627a5d0fbbSHugh Dickins /*
3636a15a370SVlastimil Babka  * Determine (in bytes) how many of the shmem object's pages mapped by the
36448131e03SVlastimil Babka  * given offsets are swapped out.
3656a15a370SVlastimil Babka  *
3666a15a370SVlastimil Babka  * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU,
3676a15a370SVlastimil Babka  * as long as the inode doesn't go away and racy results are not a problem.
3686a15a370SVlastimil Babka  */
36948131e03SVlastimil Babka unsigned long shmem_partial_swap_usage(struct address_space *mapping,
37048131e03SVlastimil Babka 						pgoff_t start, pgoff_t end)
3716a15a370SVlastimil Babka {
3726a15a370SVlastimil Babka 	struct radix_tree_iter iter;
3736a15a370SVlastimil Babka 	void **slot;
3746a15a370SVlastimil Babka 	struct page *page;
37548131e03SVlastimil Babka 	unsigned long swapped = 0;
3766a15a370SVlastimil Babka 
3776a15a370SVlastimil Babka 	rcu_read_lock();
3786a15a370SVlastimil Babka 
3796a15a370SVlastimil Babka restart:
3806a15a370SVlastimil Babka 	radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
3816a15a370SVlastimil Babka 		if (iter.index >= end)
3826a15a370SVlastimil Babka 			break;
3836a15a370SVlastimil Babka 
3846a15a370SVlastimil Babka 		page = radix_tree_deref_slot(slot);
3856a15a370SVlastimil Babka 
3866a15a370SVlastimil Babka 		/*
3876a15a370SVlastimil Babka 		 * This should only be possible to happen at index 0, so we
3886a15a370SVlastimil Babka 		 * don't need to reset the counter, nor do we risk infinite
3896a15a370SVlastimil Babka 		 * restarts.
3906a15a370SVlastimil Babka 		 */
3916a15a370SVlastimil Babka 		if (radix_tree_deref_retry(page))
3926a15a370SVlastimil Babka 			goto restart;
3936a15a370SVlastimil Babka 
3946a15a370SVlastimil Babka 		if (radix_tree_exceptional_entry(page))
3956a15a370SVlastimil Babka 			swapped++;
3966a15a370SVlastimil Babka 
3976a15a370SVlastimil Babka 		if (need_resched()) {
3986a15a370SVlastimil Babka 			cond_resched_rcu();
3996a15a370SVlastimil Babka 			start = iter.index + 1;
4006a15a370SVlastimil Babka 			goto restart;
4016a15a370SVlastimil Babka 		}
4026a15a370SVlastimil Babka 	}
4036a15a370SVlastimil Babka 
4046a15a370SVlastimil Babka 	rcu_read_unlock();
4056a15a370SVlastimil Babka 
4066a15a370SVlastimil Babka 	return swapped << PAGE_SHIFT;
4076a15a370SVlastimil Babka }
4086a15a370SVlastimil Babka 
4096a15a370SVlastimil Babka /*
41048131e03SVlastimil Babka  * Determine (in bytes) how many of the shmem object's pages mapped by the
41148131e03SVlastimil Babka  * given vma is swapped out.
41248131e03SVlastimil Babka  *
41348131e03SVlastimil Babka  * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU,
41448131e03SVlastimil Babka  * as long as the inode doesn't go away and racy results are not a problem.
41548131e03SVlastimil Babka  */
41648131e03SVlastimil Babka unsigned long shmem_swap_usage(struct vm_area_struct *vma)
41748131e03SVlastimil Babka {
41848131e03SVlastimil Babka 	struct inode *inode = file_inode(vma->vm_file);
41948131e03SVlastimil Babka 	struct shmem_inode_info *info = SHMEM_I(inode);
42048131e03SVlastimil Babka 	struct address_space *mapping = inode->i_mapping;
42148131e03SVlastimil Babka 	unsigned long swapped;
42248131e03SVlastimil Babka 
42348131e03SVlastimil Babka 	/* Be careful as we don't hold info->lock */
42448131e03SVlastimil Babka 	swapped = READ_ONCE(info->swapped);
42548131e03SVlastimil Babka 
42648131e03SVlastimil Babka 	/*
42748131e03SVlastimil Babka 	 * The easier cases are when the shmem object has nothing in swap, or
42848131e03SVlastimil Babka 	 * the vma maps it whole. Then we can simply use the stats that we
42948131e03SVlastimil Babka 	 * already track.
43048131e03SVlastimil Babka 	 */
43148131e03SVlastimil Babka 	if (!swapped)
43248131e03SVlastimil Babka 		return 0;
43348131e03SVlastimil Babka 
43448131e03SVlastimil Babka 	if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
43548131e03SVlastimil Babka 		return swapped << PAGE_SHIFT;
43648131e03SVlastimil Babka 
43748131e03SVlastimil Babka 	/* Here comes the more involved part */
43848131e03SVlastimil Babka 	return shmem_partial_swap_usage(mapping,
43948131e03SVlastimil Babka 			linear_page_index(vma, vma->vm_start),
44048131e03SVlastimil Babka 			linear_page_index(vma, vma->vm_end));
44148131e03SVlastimil Babka }
44248131e03SVlastimil Babka 
44348131e03SVlastimil Babka /*
44424513264SHugh Dickins  * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
44524513264SHugh Dickins  */
44624513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping)
44724513264SHugh Dickins {
44824513264SHugh Dickins 	struct pagevec pvec;
44924513264SHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
45024513264SHugh Dickins 	pgoff_t index = 0;
45124513264SHugh Dickins 
45224513264SHugh Dickins 	pagevec_init(&pvec, 0);
45324513264SHugh Dickins 	/*
45424513264SHugh Dickins 	 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
45524513264SHugh Dickins 	 */
45624513264SHugh Dickins 	while (!mapping_unevictable(mapping)) {
45724513264SHugh Dickins 		/*
45824513264SHugh Dickins 		 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
45924513264SHugh Dickins 		 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
46024513264SHugh Dickins 		 */
4610cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
46224513264SHugh Dickins 					   PAGEVEC_SIZE, pvec.pages, indices);
46324513264SHugh Dickins 		if (!pvec.nr)
46424513264SHugh Dickins 			break;
46524513264SHugh Dickins 		index = indices[pvec.nr - 1] + 1;
4660cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
46724513264SHugh Dickins 		check_move_unevictable_pages(pvec.pages, pvec.nr);
46824513264SHugh Dickins 		pagevec_release(&pvec);
46924513264SHugh Dickins 		cond_resched();
47024513264SHugh Dickins 	}
4717a5d0fbbSHugh Dickins }
4727a5d0fbbSHugh Dickins 
4737a5d0fbbSHugh Dickins /*
4747a5d0fbbSHugh Dickins  * Remove range of pages and swap entries from radix tree, and free them.
4751635f6a7SHugh Dickins  * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
4767a5d0fbbSHugh Dickins  */
4771635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
4781635f6a7SHugh Dickins 								 bool unfalloc)
4791da177e4SLinus Torvalds {
480285b2c4fSHugh Dickins 	struct address_space *mapping = inode->i_mapping;
4811da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
482285b2c4fSHugh Dickins 	pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
48383e4fa9cSHugh Dickins 	pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
48483e4fa9cSHugh Dickins 	unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
48583e4fa9cSHugh Dickins 	unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
486bda97eabSHugh Dickins 	struct pagevec pvec;
4877a5d0fbbSHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
4887a5d0fbbSHugh Dickins 	long nr_swaps_freed = 0;
489285b2c4fSHugh Dickins 	pgoff_t index;
490bda97eabSHugh Dickins 	int i;
4911da177e4SLinus Torvalds 
49283e4fa9cSHugh Dickins 	if (lend == -1)
49383e4fa9cSHugh Dickins 		end = -1;	/* unsigned, so actually very big */
494bda97eabSHugh Dickins 
495bda97eabSHugh Dickins 	pagevec_init(&pvec, 0);
496bda97eabSHugh Dickins 	index = start;
49783e4fa9cSHugh Dickins 	while (index < end) {
4980cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
49983e4fa9cSHugh Dickins 			min(end - index, (pgoff_t)PAGEVEC_SIZE),
5007a5d0fbbSHugh Dickins 			pvec.pages, indices);
5017a5d0fbbSHugh Dickins 		if (!pvec.nr)
5027a5d0fbbSHugh Dickins 			break;
503bda97eabSHugh Dickins 		for (i = 0; i < pagevec_count(&pvec); i++) {
504bda97eabSHugh Dickins 			struct page *page = pvec.pages[i];
505bda97eabSHugh Dickins 
5067a5d0fbbSHugh Dickins 			index = indices[i];
50783e4fa9cSHugh Dickins 			if (index >= end)
508bda97eabSHugh Dickins 				break;
509bda97eabSHugh Dickins 
5107a5d0fbbSHugh Dickins 			if (radix_tree_exceptional_entry(page)) {
5111635f6a7SHugh Dickins 				if (unfalloc)
5121635f6a7SHugh Dickins 					continue;
5137a5d0fbbSHugh Dickins 				nr_swaps_freed += !shmem_free_swap(mapping,
5147a5d0fbbSHugh Dickins 								index, page);
5157a5d0fbbSHugh Dickins 				continue;
5167a5d0fbbSHugh Dickins 			}
5177a5d0fbbSHugh Dickins 
518bda97eabSHugh Dickins 			if (!trylock_page(page))
519bda97eabSHugh Dickins 				continue;
5201635f6a7SHugh Dickins 			if (!unfalloc || !PageUptodate(page)) {
5217a5d0fbbSHugh Dickins 				if (page->mapping == mapping) {
522309381feSSasha Levin 					VM_BUG_ON_PAGE(PageWriteback(page), page);
523bda97eabSHugh Dickins 					truncate_inode_page(mapping, page);
5247a5d0fbbSHugh Dickins 				}
5251635f6a7SHugh Dickins 			}
526bda97eabSHugh Dickins 			unlock_page(page);
527bda97eabSHugh Dickins 		}
5280cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
52924513264SHugh Dickins 		pagevec_release(&pvec);
530bda97eabSHugh Dickins 		cond_resched();
531bda97eabSHugh Dickins 		index++;
532bda97eabSHugh Dickins 	}
533bda97eabSHugh Dickins 
53483e4fa9cSHugh Dickins 	if (partial_start) {
535bda97eabSHugh Dickins 		struct page *page = NULL;
536bda97eabSHugh Dickins 		shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
537bda97eabSHugh Dickins 		if (page) {
53883e4fa9cSHugh Dickins 			unsigned int top = PAGE_CACHE_SIZE;
53983e4fa9cSHugh Dickins 			if (start > end) {
54083e4fa9cSHugh Dickins 				top = partial_end;
54183e4fa9cSHugh Dickins 				partial_end = 0;
54283e4fa9cSHugh Dickins 			}
54383e4fa9cSHugh Dickins 			zero_user_segment(page, partial_start, top);
544bda97eabSHugh Dickins 			set_page_dirty(page);
545bda97eabSHugh Dickins 			unlock_page(page);
546bda97eabSHugh Dickins 			page_cache_release(page);
547bda97eabSHugh Dickins 		}
548bda97eabSHugh Dickins 	}
54983e4fa9cSHugh Dickins 	if (partial_end) {
55083e4fa9cSHugh Dickins 		struct page *page = NULL;
55183e4fa9cSHugh Dickins 		shmem_getpage(inode, end, &page, SGP_READ, NULL);
55283e4fa9cSHugh Dickins 		if (page) {
55383e4fa9cSHugh Dickins 			zero_user_segment(page, 0, partial_end);
55483e4fa9cSHugh Dickins 			set_page_dirty(page);
55583e4fa9cSHugh Dickins 			unlock_page(page);
55683e4fa9cSHugh Dickins 			page_cache_release(page);
55783e4fa9cSHugh Dickins 		}
55883e4fa9cSHugh Dickins 	}
55983e4fa9cSHugh Dickins 	if (start >= end)
56083e4fa9cSHugh Dickins 		return;
561bda97eabSHugh Dickins 
562bda97eabSHugh Dickins 	index = start;
563b1a36650SHugh Dickins 	while (index < end) {
564bda97eabSHugh Dickins 		cond_resched();
5650cd6144aSJohannes Weiner 
5660cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
56783e4fa9cSHugh Dickins 				min(end - index, (pgoff_t)PAGEVEC_SIZE),
5687a5d0fbbSHugh Dickins 				pvec.pages, indices);
5697a5d0fbbSHugh Dickins 		if (!pvec.nr) {
570b1a36650SHugh Dickins 			/* If all gone or hole-punch or unfalloc, we're done */
571b1a36650SHugh Dickins 			if (index == start || end != -1)
572bda97eabSHugh Dickins 				break;
573b1a36650SHugh Dickins 			/* But if truncating, restart to make sure all gone */
574bda97eabSHugh Dickins 			index = start;
575bda97eabSHugh Dickins 			continue;
576bda97eabSHugh Dickins 		}
577bda97eabSHugh Dickins 		for (i = 0; i < pagevec_count(&pvec); i++) {
578bda97eabSHugh Dickins 			struct page *page = pvec.pages[i];
579bda97eabSHugh Dickins 
5807a5d0fbbSHugh Dickins 			index = indices[i];
58183e4fa9cSHugh Dickins 			if (index >= end)
582bda97eabSHugh Dickins 				break;
583bda97eabSHugh Dickins 
5847a5d0fbbSHugh Dickins 			if (radix_tree_exceptional_entry(page)) {
5851635f6a7SHugh Dickins 				if (unfalloc)
5861635f6a7SHugh Dickins 					continue;
587b1a36650SHugh Dickins 				if (shmem_free_swap(mapping, index, page)) {
588b1a36650SHugh Dickins 					/* Swap was replaced by page: retry */
589b1a36650SHugh Dickins 					index--;
590b1a36650SHugh Dickins 					break;
591b1a36650SHugh Dickins 				}
592b1a36650SHugh Dickins 				nr_swaps_freed++;
5937a5d0fbbSHugh Dickins 				continue;
5947a5d0fbbSHugh Dickins 			}
5957a5d0fbbSHugh Dickins 
596bda97eabSHugh Dickins 			lock_page(page);
5971635f6a7SHugh Dickins 			if (!unfalloc || !PageUptodate(page)) {
5987a5d0fbbSHugh Dickins 				if (page->mapping == mapping) {
599309381feSSasha Levin 					VM_BUG_ON_PAGE(PageWriteback(page), page);
600bda97eabSHugh Dickins 					truncate_inode_page(mapping, page);
601b1a36650SHugh Dickins 				} else {
602b1a36650SHugh Dickins 					/* Page was replaced by swap: retry */
603b1a36650SHugh Dickins 					unlock_page(page);
604b1a36650SHugh Dickins 					index--;
605b1a36650SHugh Dickins 					break;
6067a5d0fbbSHugh Dickins 				}
6071635f6a7SHugh Dickins 			}
608bda97eabSHugh Dickins 			unlock_page(page);
609bda97eabSHugh Dickins 		}
6100cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
61124513264SHugh Dickins 		pagevec_release(&pvec);
612bda97eabSHugh Dickins 		index++;
613bda97eabSHugh Dickins 	}
61494c1e62dSHugh Dickins 
6151da177e4SLinus Torvalds 	spin_lock(&info->lock);
6167a5d0fbbSHugh Dickins 	info->swapped -= nr_swaps_freed;
6171da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
6181da177e4SLinus Torvalds 	spin_unlock(&info->lock);
6191635f6a7SHugh Dickins }
6201da177e4SLinus Torvalds 
6211635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
6221635f6a7SHugh Dickins {
6231635f6a7SHugh Dickins 	shmem_undo_range(inode, lstart, lend, false);
624285b2c4fSHugh Dickins 	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
6251da177e4SLinus Torvalds }
62694c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
6271da177e4SLinus Torvalds 
62844a30220SYu Zhao static int shmem_getattr(struct vfsmount *mnt, struct dentry *dentry,
62944a30220SYu Zhao 			 struct kstat *stat)
63044a30220SYu Zhao {
63144a30220SYu Zhao 	struct inode *inode = dentry->d_inode;
63244a30220SYu Zhao 	struct shmem_inode_info *info = SHMEM_I(inode);
63344a30220SYu Zhao 
634d0424c42SHugh Dickins 	if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
63544a30220SYu Zhao 		spin_lock(&info->lock);
63644a30220SYu Zhao 		shmem_recalc_inode(inode);
63744a30220SYu Zhao 		spin_unlock(&info->lock);
638d0424c42SHugh Dickins 	}
63944a30220SYu Zhao 	generic_fillattr(inode, stat);
64044a30220SYu Zhao 	return 0;
64144a30220SYu Zhao }
64244a30220SYu Zhao 
64394c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
6441da177e4SLinus Torvalds {
64575c3cfa8SDavid Howells 	struct inode *inode = d_inode(dentry);
64640e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
6471da177e4SLinus Torvalds 	int error;
6481da177e4SLinus Torvalds 
649db78b877SChristoph Hellwig 	error = inode_change_ok(inode, attr);
650db78b877SChristoph Hellwig 	if (error)
651db78b877SChristoph Hellwig 		return error;
652db78b877SChristoph Hellwig 
65394c1e62dSHugh Dickins 	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
65494c1e62dSHugh Dickins 		loff_t oldsize = inode->i_size;
65594c1e62dSHugh Dickins 		loff_t newsize = attr->ia_size;
6563889e6e7Snpiggin@suse.de 
65740e041a2SDavid Herrmann 		/* protected by i_mutex */
65840e041a2SDavid Herrmann 		if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
65940e041a2SDavid Herrmann 		    (newsize > oldsize && (info->seals & F_SEAL_GROW)))
66040e041a2SDavid Herrmann 			return -EPERM;
66140e041a2SDavid Herrmann 
66294c1e62dSHugh Dickins 		if (newsize != oldsize) {
66377142517SKonstantin Khlebnikov 			error = shmem_reacct_size(SHMEM_I(inode)->flags,
66477142517SKonstantin Khlebnikov 					oldsize, newsize);
66577142517SKonstantin Khlebnikov 			if (error)
66677142517SKonstantin Khlebnikov 				return error;
66794c1e62dSHugh Dickins 			i_size_write(inode, newsize);
66894c1e62dSHugh Dickins 			inode->i_ctime = inode->i_mtime = CURRENT_TIME;
66994c1e62dSHugh Dickins 		}
670afa2db2fSJosef Bacik 		if (newsize <= oldsize) {
67194c1e62dSHugh Dickins 			loff_t holebegin = round_up(newsize, PAGE_SIZE);
672d0424c42SHugh Dickins 			if (oldsize > holebegin)
673d0424c42SHugh Dickins 				unmap_mapping_range(inode->i_mapping,
674d0424c42SHugh Dickins 							holebegin, 0, 1);
675d0424c42SHugh Dickins 			if (info->alloced)
676d0424c42SHugh Dickins 				shmem_truncate_range(inode,
677d0424c42SHugh Dickins 							newsize, (loff_t)-1);
67894c1e62dSHugh Dickins 			/* unmap again to remove racily COWed private pages */
679d0424c42SHugh Dickins 			if (oldsize > holebegin)
680d0424c42SHugh Dickins 				unmap_mapping_range(inode->i_mapping,
681d0424c42SHugh Dickins 							holebegin, 0, 1);
68294c1e62dSHugh Dickins 		}
6831da177e4SLinus Torvalds 	}
6841da177e4SLinus Torvalds 
6856a1a90adSChristoph Hellwig 	setattr_copy(inode, attr);
686db78b877SChristoph Hellwig 	if (attr->ia_valid & ATTR_MODE)
687feda821eSChristoph Hellwig 		error = posix_acl_chmod(inode, inode->i_mode);
6881da177e4SLinus Torvalds 	return error;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
6911f895f75SAl Viro static void shmem_evict_inode(struct inode *inode)
6921da177e4SLinus Torvalds {
6931da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
6941da177e4SLinus Torvalds 
6953889e6e7Snpiggin@suse.de 	if (inode->i_mapping->a_ops == &shmem_aops) {
6961da177e4SLinus Torvalds 		shmem_unacct_size(info->flags, inode->i_size);
6971da177e4SLinus Torvalds 		inode->i_size = 0;
6983889e6e7Snpiggin@suse.de 		shmem_truncate_range(inode, 0, (loff_t)-1);
6991da177e4SLinus Torvalds 		if (!list_empty(&info->swaplist)) {
700cb5f7b9aSHugh Dickins 			mutex_lock(&shmem_swaplist_mutex);
7011da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
702cb5f7b9aSHugh Dickins 			mutex_unlock(&shmem_swaplist_mutex);
7031da177e4SLinus Torvalds 		}
70469f07ec9SHugh Dickins 	} else
70569f07ec9SHugh Dickins 		kfree(info->symlink);
706b09e0fa4SEric Paris 
70738f38657SAristeu Rozanski 	simple_xattrs_free(&info->xattrs);
7080f3c42f5SHugh Dickins 	WARN_ON(inode->i_blocks);
7095b04c689SPavel Emelyanov 	shmem_free_inode(inode->i_sb);
710dbd5768fSJan Kara 	clear_inode(inode);
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
71346f65ec1SHugh Dickins /*
71446f65ec1SHugh Dickins  * If swap found in inode, free it and move page from swapcache to filecache.
71546f65ec1SHugh Dickins  */
71641ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info,
717bde05d1cSHugh Dickins 			     swp_entry_t swap, struct page **pagep)
7181da177e4SLinus Torvalds {
719285b2c4fSHugh Dickins 	struct address_space *mapping = info->vfs_inode.i_mapping;
72046f65ec1SHugh Dickins 	void *radswap;
72141ffe5d5SHugh Dickins 	pgoff_t index;
722bde05d1cSHugh Dickins 	gfp_t gfp;
723bde05d1cSHugh Dickins 	int error = 0;
7241da177e4SLinus Torvalds 
72546f65ec1SHugh Dickins 	radswap = swp_to_radix_entry(swap);
726e504f3fdSHugh Dickins 	index = radix_tree_locate_item(&mapping->page_tree, radswap);
72746f65ec1SHugh Dickins 	if (index == -1)
72800501b53SJohannes Weiner 		return -EAGAIN;	/* tell shmem_unuse we found nothing */
7292e0e26c7SHugh Dickins 
7301b1b32f2SHugh Dickins 	/*
7311b1b32f2SHugh Dickins 	 * Move _head_ to start search for next from here.
7321f895f75SAl Viro 	 * But be careful: shmem_evict_inode checks list_empty without taking
7331b1b32f2SHugh Dickins 	 * mutex, and there's an instant in list_move_tail when info->swaplist
734285b2c4fSHugh Dickins 	 * would appear empty, if it were the only one on shmem_swaplist.
7351b1b32f2SHugh Dickins 	 */
7361b1b32f2SHugh Dickins 	if (shmem_swaplist.next != &info->swaplist)
7372e0e26c7SHugh Dickins 		list_move_tail(&shmem_swaplist, &info->swaplist);
7382e0e26c7SHugh Dickins 
739bde05d1cSHugh Dickins 	gfp = mapping_gfp_mask(mapping);
740bde05d1cSHugh Dickins 	if (shmem_should_replace_page(*pagep, gfp)) {
741bde05d1cSHugh Dickins 		mutex_unlock(&shmem_swaplist_mutex);
742bde05d1cSHugh Dickins 		error = shmem_replace_page(pagep, gfp, info, index);
743bde05d1cSHugh Dickins 		mutex_lock(&shmem_swaplist_mutex);
744bde05d1cSHugh Dickins 		/*
745bde05d1cSHugh Dickins 		 * We needed to drop mutex to make that restrictive page
7460142ef6cSHugh Dickins 		 * allocation, but the inode might have been freed while we
7470142ef6cSHugh Dickins 		 * dropped it: although a racing shmem_evict_inode() cannot
7480142ef6cSHugh Dickins 		 * complete without emptying the radix_tree, our page lock
7490142ef6cSHugh Dickins 		 * on this swapcache page is not enough to prevent that -
7500142ef6cSHugh Dickins 		 * free_swap_and_cache() of our swap entry will only
7510142ef6cSHugh Dickins 		 * trylock_page(), removing swap from radix_tree whatever.
7520142ef6cSHugh Dickins 		 *
7530142ef6cSHugh Dickins 		 * We must not proceed to shmem_add_to_page_cache() if the
7540142ef6cSHugh Dickins 		 * inode has been freed, but of course we cannot rely on
7550142ef6cSHugh Dickins 		 * inode or mapping or info to check that.  However, we can
7560142ef6cSHugh Dickins 		 * safely check if our swap entry is still in use (and here
7570142ef6cSHugh Dickins 		 * it can't have got reused for another page): if it's still
7580142ef6cSHugh Dickins 		 * in use, then the inode cannot have been freed yet, and we
7590142ef6cSHugh Dickins 		 * can safely proceed (if it's no longer in use, that tells
7600142ef6cSHugh Dickins 		 * nothing about the inode, but we don't need to unuse swap).
761bde05d1cSHugh Dickins 		 */
762bde05d1cSHugh Dickins 		if (!page_swapcount(*pagep))
763bde05d1cSHugh Dickins 			error = -ENOENT;
764bde05d1cSHugh Dickins 	}
765bde05d1cSHugh Dickins 
766d13d1443SKAMEZAWA Hiroyuki 	/*
767778dd893SHugh Dickins 	 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
768778dd893SHugh Dickins 	 * but also to hold up shmem_evict_inode(): so inode cannot be freed
769778dd893SHugh Dickins 	 * beneath us (pagelock doesn't help until the page is in pagecache).
770d13d1443SKAMEZAWA Hiroyuki 	 */
771bde05d1cSHugh Dickins 	if (!error)
772bde05d1cSHugh Dickins 		error = shmem_add_to_page_cache(*pagep, mapping, index,
773fed400a1SWang Sheng-Hui 						radswap);
77448f170fbSHugh Dickins 	if (error != -ENOMEM) {
77546f65ec1SHugh Dickins 		/*
77646f65ec1SHugh Dickins 		 * Truncation and eviction use free_swap_and_cache(), which
77746f65ec1SHugh Dickins 		 * only does trylock page: if we raced, best clean up here.
77846f65ec1SHugh Dickins 		 */
779bde05d1cSHugh Dickins 		delete_from_swap_cache(*pagep);
780bde05d1cSHugh Dickins 		set_page_dirty(*pagep);
78146f65ec1SHugh Dickins 		if (!error) {
78246f65ec1SHugh Dickins 			spin_lock(&info->lock);
783285b2c4fSHugh Dickins 			info->swapped--;
78446f65ec1SHugh Dickins 			spin_unlock(&info->lock);
78541ffe5d5SHugh Dickins 			swap_free(swap);
78646f65ec1SHugh Dickins 		}
7871da177e4SLinus Torvalds 	}
7882e0e26c7SHugh Dickins 	return error;
7891da177e4SLinus Torvalds }
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds /*
79246f65ec1SHugh Dickins  * Search through swapped inodes to find and replace swap by page.
7931da177e4SLinus Torvalds  */
79441ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page)
7951da177e4SLinus Torvalds {
79641ffe5d5SHugh Dickins 	struct list_head *this, *next;
7971da177e4SLinus Torvalds 	struct shmem_inode_info *info;
79800501b53SJohannes Weiner 	struct mem_cgroup *memcg;
799bde05d1cSHugh Dickins 	int error = 0;
800bde05d1cSHugh Dickins 
801bde05d1cSHugh Dickins 	/*
802bde05d1cSHugh Dickins 	 * There's a faint possibility that swap page was replaced before
8030142ef6cSHugh Dickins 	 * caller locked it: caller will come back later with the right page.
804bde05d1cSHugh Dickins 	 */
8050142ef6cSHugh Dickins 	if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
806bde05d1cSHugh Dickins 		goto out;
807778dd893SHugh Dickins 
808778dd893SHugh Dickins 	/*
809778dd893SHugh Dickins 	 * Charge page using GFP_KERNEL while we can wait, before taking
810778dd893SHugh Dickins 	 * the shmem_swaplist_mutex which might hold up shmem_writepage().
811778dd893SHugh Dickins 	 * Charged back to the user (not to caller) when swap account is used.
812778dd893SHugh Dickins 	 */
813f627c2f5SKirill A. Shutemov 	error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg,
814f627c2f5SKirill A. Shutemov 			false);
815778dd893SHugh Dickins 	if (error)
816778dd893SHugh Dickins 		goto out;
81746f65ec1SHugh Dickins 	/* No radix_tree_preload: swap entry keeps a place for page in tree */
81800501b53SJohannes Weiner 	error = -EAGAIN;
8191da177e4SLinus Torvalds 
820cb5f7b9aSHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
82141ffe5d5SHugh Dickins 	list_for_each_safe(this, next, &shmem_swaplist) {
82241ffe5d5SHugh Dickins 		info = list_entry(this, struct shmem_inode_info, swaplist);
823285b2c4fSHugh Dickins 		if (info->swapped)
82400501b53SJohannes Weiner 			error = shmem_unuse_inode(info, swap, &page);
8256922c0c7SHugh Dickins 		else
8266922c0c7SHugh Dickins 			list_del_init(&info->swaplist);
827cb5f7b9aSHugh Dickins 		cond_resched();
82800501b53SJohannes Weiner 		if (error != -EAGAIN)
829778dd893SHugh Dickins 			break;
83000501b53SJohannes Weiner 		/* found nothing in this: move on to search the next */
8311da177e4SLinus Torvalds 	}
832cb5f7b9aSHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
833778dd893SHugh Dickins 
83400501b53SJohannes Weiner 	if (error) {
83500501b53SJohannes Weiner 		if (error != -ENOMEM)
83600501b53SJohannes Weiner 			error = 0;
837f627c2f5SKirill A. Shutemov 		mem_cgroup_cancel_charge(page, memcg, false);
83800501b53SJohannes Weiner 	} else
839f627c2f5SKirill A. Shutemov 		mem_cgroup_commit_charge(page, memcg, true, false);
840778dd893SHugh Dickins out:
841aaa46865SHugh Dickins 	unlock_page(page);
842aaa46865SHugh Dickins 	page_cache_release(page);
843778dd893SHugh Dickins 	return error;
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds /*
8471da177e4SLinus Torvalds  * Move the page from the page cache to the swap cache.
8481da177e4SLinus Torvalds  */
8491da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc)
8501da177e4SLinus Torvalds {
8511da177e4SLinus Torvalds 	struct shmem_inode_info *info;
8521da177e4SLinus Torvalds 	struct address_space *mapping;
8531da177e4SLinus Torvalds 	struct inode *inode;
8546922c0c7SHugh Dickins 	swp_entry_t swap;
8556922c0c7SHugh Dickins 	pgoff_t index;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
8581da177e4SLinus Torvalds 	mapping = page->mapping;
8591da177e4SLinus Torvalds 	index = page->index;
8601da177e4SLinus Torvalds 	inode = mapping->host;
8611da177e4SLinus Torvalds 	info = SHMEM_I(inode);
8621da177e4SLinus Torvalds 	if (info->flags & VM_LOCKED)
8631da177e4SLinus Torvalds 		goto redirty;
864d9fe526aSHugh Dickins 	if (!total_swap_pages)
8651da177e4SLinus Torvalds 		goto redirty;
8661da177e4SLinus Torvalds 
867d9fe526aSHugh Dickins 	/*
86897b713baSChristoph Hellwig 	 * Our capabilities prevent regular writeback or sync from ever calling
86997b713baSChristoph Hellwig 	 * shmem_writepage; but a stacking filesystem might use ->writepage of
87097b713baSChristoph Hellwig 	 * its underlying filesystem, in which case tmpfs should write out to
87197b713baSChristoph Hellwig 	 * swap only in response to memory pressure, and not for the writeback
87297b713baSChristoph Hellwig 	 * threads or sync.
873d9fe526aSHugh Dickins 	 */
87448f170fbSHugh Dickins 	if (!wbc->for_reclaim) {
87548f170fbSHugh Dickins 		WARN_ON_ONCE(1);	/* Still happens? Tell us about it! */
87648f170fbSHugh Dickins 		goto redirty;
87748f170fbSHugh Dickins 	}
8781635f6a7SHugh Dickins 
8791635f6a7SHugh Dickins 	/*
8801635f6a7SHugh Dickins 	 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
8811635f6a7SHugh Dickins 	 * value into swapfile.c, the only way we can correctly account for a
8821635f6a7SHugh Dickins 	 * fallocated page arriving here is now to initialize it and write it.
8831aac1400SHugh Dickins 	 *
8841aac1400SHugh Dickins 	 * That's okay for a page already fallocated earlier, but if we have
8851aac1400SHugh Dickins 	 * not yet completed the fallocation, then (a) we want to keep track
8861aac1400SHugh Dickins 	 * of this page in case we have to undo it, and (b) it may not be a
8871aac1400SHugh Dickins 	 * good idea to continue anyway, once we're pushing into swap.  So
8881aac1400SHugh Dickins 	 * reactivate the page, and let shmem_fallocate() quit when too many.
8891635f6a7SHugh Dickins 	 */
8901635f6a7SHugh Dickins 	if (!PageUptodate(page)) {
8911aac1400SHugh Dickins 		if (inode->i_private) {
8921aac1400SHugh Dickins 			struct shmem_falloc *shmem_falloc;
8931aac1400SHugh Dickins 			spin_lock(&inode->i_lock);
8941aac1400SHugh Dickins 			shmem_falloc = inode->i_private;
8951aac1400SHugh Dickins 			if (shmem_falloc &&
8968e205f77SHugh Dickins 			    !shmem_falloc->waitq &&
8971aac1400SHugh Dickins 			    index >= shmem_falloc->start &&
8981aac1400SHugh Dickins 			    index < shmem_falloc->next)
8991aac1400SHugh Dickins 				shmem_falloc->nr_unswapped++;
9001aac1400SHugh Dickins 			else
9011aac1400SHugh Dickins 				shmem_falloc = NULL;
9021aac1400SHugh Dickins 			spin_unlock(&inode->i_lock);
9031aac1400SHugh Dickins 			if (shmem_falloc)
9041aac1400SHugh Dickins 				goto redirty;
9051aac1400SHugh Dickins 		}
9061635f6a7SHugh Dickins 		clear_highpage(page);
9071635f6a7SHugh Dickins 		flush_dcache_page(page);
9081635f6a7SHugh Dickins 		SetPageUptodate(page);
9091635f6a7SHugh Dickins 	}
9101635f6a7SHugh Dickins 
911d9fe526aSHugh Dickins 	swap = get_swap_page();
91248f170fbSHugh Dickins 	if (!swap.val)
91348f170fbSHugh Dickins 		goto redirty;
914d9fe526aSHugh Dickins 
91537e84351SVladimir Davydov 	if (mem_cgroup_try_charge_swap(page, swap))
91637e84351SVladimir Davydov 		goto free_swap;
91737e84351SVladimir Davydov 
918b1dea800SHugh Dickins 	/*
919b1dea800SHugh Dickins 	 * Add inode to shmem_unuse()'s list of swapped-out inodes,
9206922c0c7SHugh Dickins 	 * if it's not already there.  Do it now before the page is
9216922c0c7SHugh Dickins 	 * moved to swap cache, when its pagelock no longer protects
922b1dea800SHugh Dickins 	 * the inode from eviction.  But don't unlock the mutex until
9236922c0c7SHugh Dickins 	 * we've incremented swapped, because shmem_unuse_inode() will
9246922c0c7SHugh Dickins 	 * prune a !swapped inode from the swaplist under this mutex.
925b1dea800SHugh Dickins 	 */
926b1dea800SHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
92705bf86b4SHugh Dickins 	if (list_empty(&info->swaplist))
92805bf86b4SHugh Dickins 		list_add_tail(&info->swaplist, &shmem_swaplist);
929b1dea800SHugh Dickins 
93048f170fbSHugh Dickins 	if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
931267a4c76SHugh Dickins 		spin_lock(&info->lock);
932267a4c76SHugh Dickins 		shmem_recalc_inode(inode);
933267a4c76SHugh Dickins 		info->swapped++;
934267a4c76SHugh Dickins 		spin_unlock(&info->lock);
935267a4c76SHugh Dickins 
936aaa46865SHugh Dickins 		swap_shmem_alloc(swap);
9376922c0c7SHugh Dickins 		shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
9386922c0c7SHugh Dickins 
9396922c0c7SHugh Dickins 		mutex_unlock(&shmem_swaplist_mutex);
940d9fe526aSHugh Dickins 		BUG_ON(page_mapped(page));
9419fab5619SHugh Dickins 		swap_writepage(page, wbc);
9421da177e4SLinus Torvalds 		return 0;
9431da177e4SLinus Torvalds 	}
9441da177e4SLinus Torvalds 
9456922c0c7SHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
94637e84351SVladimir Davydov free_swap:
9470a31bc97SJohannes Weiner 	swapcache_free(swap);
9481da177e4SLinus Torvalds redirty:
9491da177e4SLinus Torvalds 	set_page_dirty(page);
950d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
951d9fe526aSHugh Dickins 		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
952d9fe526aSHugh Dickins 	unlock_page(page);
953d9fe526aSHugh Dickins 	return 0;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds #ifdef CONFIG_NUMA
957680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
95871fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
959680d794bSakpm@linux-foundation.org {
960680d794bSakpm@linux-foundation.org 	char buffer[64];
961680d794bSakpm@linux-foundation.org 
96271fe804bSLee Schermerhorn 	if (!mpol || mpol->mode == MPOL_DEFAULT)
963095f1fc4SLee Schermerhorn 		return;		/* show nothing */
964095f1fc4SLee Schermerhorn 
965a7a88b23SHugh Dickins 	mpol_to_str(buffer, sizeof(buffer), mpol);
966095f1fc4SLee Schermerhorn 
967095f1fc4SLee Schermerhorn 	seq_printf(seq, ",mpol=%s", buffer);
968680d794bSakpm@linux-foundation.org }
96971fe804bSLee Schermerhorn 
97071fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
97171fe804bSLee Schermerhorn {
97271fe804bSLee Schermerhorn 	struct mempolicy *mpol = NULL;
97371fe804bSLee Schermerhorn 	if (sbinfo->mpol) {
97471fe804bSLee Schermerhorn 		spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
97571fe804bSLee Schermerhorn 		mpol = sbinfo->mpol;
97671fe804bSLee Schermerhorn 		mpol_get(mpol);
97771fe804bSLee Schermerhorn 		spin_unlock(&sbinfo->stat_lock);
97871fe804bSLee Schermerhorn 	}
97971fe804bSLee Schermerhorn 	return mpol;
98071fe804bSLee Schermerhorn }
981680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
982680d794bSakpm@linux-foundation.org 
98341ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
98441ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
9851da177e4SLinus Torvalds {
9861da177e4SLinus Torvalds 	struct vm_area_struct pvma;
98718a2f371SMel Gorman 	struct page *page;
9881da177e4SLinus Torvalds 
989c4cc6d07SHugh Dickins 	/* Create a pseudo vma that just contains the policy */
990c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
99109c231cbSNathan Zimmer 	/* Bias interleave by inode number to distribute better across nodes */
99209c231cbSNathan Zimmer 	pvma.vm_pgoff = index + info->vfs_inode.i_ino;
993c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
99441ffe5d5SHugh Dickins 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
99552cd3b07SLee Schermerhorn 
99618a2f371SMel Gorman 	page = swapin_readahead(swap, gfp, &pvma, 0);
99718a2f371SMel Gorman 
99818a2f371SMel Gorman 	/* Drop reference taken by mpol_shared_policy_lookup() */
99918a2f371SMel Gorman 	mpol_cond_put(pvma.vm_policy);
100018a2f371SMel Gorman 
100118a2f371SMel Gorman 	return page;
100218a2f371SMel Gorman }
100318a2f371SMel Gorman 
100418a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp,
100518a2f371SMel Gorman 			struct shmem_inode_info *info, pgoff_t index)
100618a2f371SMel Gorman {
100718a2f371SMel Gorman 	struct vm_area_struct pvma;
100818a2f371SMel Gorman 	struct page *page;
100918a2f371SMel Gorman 
101018a2f371SMel Gorman 	/* Create a pseudo vma that just contains the policy */
101118a2f371SMel Gorman 	pvma.vm_start = 0;
101218a2f371SMel Gorman 	/* Bias interleave by inode number to distribute better across nodes */
101318a2f371SMel Gorman 	pvma.vm_pgoff = index + info->vfs_inode.i_ino;
101418a2f371SMel Gorman 	pvma.vm_ops = NULL;
101518a2f371SMel Gorman 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
101618a2f371SMel Gorman 
101718a2f371SMel Gorman 	page = alloc_page_vma(gfp, &pvma, 0);
101818a2f371SMel Gorman 
101918a2f371SMel Gorman 	/* Drop reference taken by mpol_shared_policy_lookup() */
102018a2f371SMel Gorman 	mpol_cond_put(pvma.vm_policy);
102118a2f371SMel Gorman 
102218a2f371SMel Gorman 	return page;
10231da177e4SLinus Torvalds }
1024680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */
1025680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
102641ffe5d5SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1027680d794bSakpm@linux-foundation.org {
1028680d794bSakpm@linux-foundation.org }
1029680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
1030680d794bSakpm@linux-foundation.org 
103141ffe5d5SHugh Dickins static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
103241ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
10331da177e4SLinus Torvalds {
103441ffe5d5SHugh Dickins 	return swapin_readahead(swap, gfp, NULL, 0);
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
103702098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp,
103841ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
10391da177e4SLinus Torvalds {
1040e84e2e13SHugh Dickins 	return alloc_page(gfp);
10411da177e4SLinus Torvalds }
1042680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */
10431da177e4SLinus Torvalds 
104471fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
104571fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
104671fe804bSLee Schermerhorn {
104771fe804bSLee Schermerhorn 	return NULL;
104871fe804bSLee Schermerhorn }
104971fe804bSLee Schermerhorn #endif
105071fe804bSLee Schermerhorn 
10511da177e4SLinus Torvalds /*
1052bde05d1cSHugh Dickins  * When a page is moved from swapcache to shmem filecache (either by the
1053bde05d1cSHugh Dickins  * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1054bde05d1cSHugh Dickins  * shmem_unuse_inode()), it may have been read in earlier from swap, in
1055bde05d1cSHugh Dickins  * ignorance of the mapping it belongs to.  If that mapping has special
1056bde05d1cSHugh Dickins  * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1057bde05d1cSHugh Dickins  * we may need to copy to a suitable page before moving to filecache.
1058bde05d1cSHugh Dickins  *
1059bde05d1cSHugh Dickins  * In a future release, this may well be extended to respect cpuset and
1060bde05d1cSHugh Dickins  * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1061bde05d1cSHugh Dickins  * but for now it is a simple matter of zone.
1062bde05d1cSHugh Dickins  */
1063bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1064bde05d1cSHugh Dickins {
1065bde05d1cSHugh Dickins 	return page_zonenum(page) > gfp_zone(gfp);
1066bde05d1cSHugh Dickins }
1067bde05d1cSHugh Dickins 
1068bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1069bde05d1cSHugh Dickins 				struct shmem_inode_info *info, pgoff_t index)
1070bde05d1cSHugh Dickins {
1071bde05d1cSHugh Dickins 	struct page *oldpage, *newpage;
1072bde05d1cSHugh Dickins 	struct address_space *swap_mapping;
1073bde05d1cSHugh Dickins 	pgoff_t swap_index;
1074bde05d1cSHugh Dickins 	int error;
1075bde05d1cSHugh Dickins 
1076bde05d1cSHugh Dickins 	oldpage = *pagep;
1077bde05d1cSHugh Dickins 	swap_index = page_private(oldpage);
1078bde05d1cSHugh Dickins 	swap_mapping = page_mapping(oldpage);
1079bde05d1cSHugh Dickins 
1080bde05d1cSHugh Dickins 	/*
1081bde05d1cSHugh Dickins 	 * We have arrived here because our zones are constrained, so don't
1082bde05d1cSHugh Dickins 	 * limit chance of success by further cpuset and node constraints.
1083bde05d1cSHugh Dickins 	 */
1084bde05d1cSHugh Dickins 	gfp &= ~GFP_CONSTRAINT_MASK;
1085bde05d1cSHugh Dickins 	newpage = shmem_alloc_page(gfp, info, index);
1086bde05d1cSHugh Dickins 	if (!newpage)
1087bde05d1cSHugh Dickins 		return -ENOMEM;
1088bde05d1cSHugh Dickins 
1089bde05d1cSHugh Dickins 	page_cache_get(newpage);
1090bde05d1cSHugh Dickins 	copy_highpage(newpage, oldpage);
10910142ef6cSHugh Dickins 	flush_dcache_page(newpage);
1092bde05d1cSHugh Dickins 
109348c935adSKirill A. Shutemov 	__SetPageLocked(newpage);
1094bde05d1cSHugh Dickins 	SetPageUptodate(newpage);
1095bde05d1cSHugh Dickins 	SetPageSwapBacked(newpage);
1096bde05d1cSHugh Dickins 	set_page_private(newpage, swap_index);
1097bde05d1cSHugh Dickins 	SetPageSwapCache(newpage);
1098bde05d1cSHugh Dickins 
1099bde05d1cSHugh Dickins 	/*
1100bde05d1cSHugh Dickins 	 * Our caller will very soon move newpage out of swapcache, but it's
1101bde05d1cSHugh Dickins 	 * a nice clean interface for us to replace oldpage by newpage there.
1102bde05d1cSHugh Dickins 	 */
1103bde05d1cSHugh Dickins 	spin_lock_irq(&swap_mapping->tree_lock);
1104bde05d1cSHugh Dickins 	error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
1105bde05d1cSHugh Dickins 								   newpage);
11060142ef6cSHugh Dickins 	if (!error) {
1107bde05d1cSHugh Dickins 		__inc_zone_page_state(newpage, NR_FILE_PAGES);
1108bde05d1cSHugh Dickins 		__dec_zone_page_state(oldpage, NR_FILE_PAGES);
11090142ef6cSHugh Dickins 	}
1110bde05d1cSHugh Dickins 	spin_unlock_irq(&swap_mapping->tree_lock);
1111bde05d1cSHugh Dickins 
11120142ef6cSHugh Dickins 	if (unlikely(error)) {
11130142ef6cSHugh Dickins 		/*
11140142ef6cSHugh Dickins 		 * Is this possible?  I think not, now that our callers check
11150142ef6cSHugh Dickins 		 * both PageSwapCache and page_private after getting page lock;
11160142ef6cSHugh Dickins 		 * but be defensive.  Reverse old to newpage for clear and free.
11170142ef6cSHugh Dickins 		 */
11180142ef6cSHugh Dickins 		oldpage = newpage;
11190142ef6cSHugh Dickins 	} else {
112045637babSHugh Dickins 		mem_cgroup_replace_page(oldpage, newpage);
1121bde05d1cSHugh Dickins 		lru_cache_add_anon(newpage);
11220142ef6cSHugh Dickins 		*pagep = newpage;
11230142ef6cSHugh Dickins 	}
1124bde05d1cSHugh Dickins 
1125bde05d1cSHugh Dickins 	ClearPageSwapCache(oldpage);
1126bde05d1cSHugh Dickins 	set_page_private(oldpage, 0);
1127bde05d1cSHugh Dickins 
1128bde05d1cSHugh Dickins 	unlock_page(oldpage);
1129bde05d1cSHugh Dickins 	page_cache_release(oldpage);
1130bde05d1cSHugh Dickins 	page_cache_release(oldpage);
11310142ef6cSHugh Dickins 	return error;
1132bde05d1cSHugh Dickins }
1133bde05d1cSHugh Dickins 
1134bde05d1cSHugh Dickins /*
113568da9f05SHugh Dickins  * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
11361da177e4SLinus Torvalds  *
11371da177e4SLinus Torvalds  * If we allocate a new one we do not mark it dirty. That's up to the
11381da177e4SLinus Torvalds  * vm. If we swap it in we mark it dirty since we also free the swap
11391da177e4SLinus Torvalds  * entry since a page cannot live in both the swap and page cache
11401da177e4SLinus Torvalds  */
114141ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
114268da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
11431da177e4SLinus Torvalds {
11441da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
114554af6042SHugh Dickins 	struct shmem_inode_info *info;
11461da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo;
114700501b53SJohannes Weiner 	struct mem_cgroup *memcg;
114827ab7006SHugh Dickins 	struct page *page;
11491da177e4SLinus Torvalds 	swp_entry_t swap;
11501da177e4SLinus Torvalds 	int error;
115154af6042SHugh Dickins 	int once = 0;
11521635f6a7SHugh Dickins 	int alloced = 0;
11531da177e4SLinus Torvalds 
115441ffe5d5SHugh Dickins 	if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
11551da177e4SLinus Torvalds 		return -EFBIG;
11561da177e4SLinus Torvalds repeat:
115754af6042SHugh Dickins 	swap.val = 0;
11580cd6144aSJohannes Weiner 	page = find_lock_entry(mapping, index);
115954af6042SHugh Dickins 	if (radix_tree_exceptional_entry(page)) {
116054af6042SHugh Dickins 		swap = radix_to_swp_entry(page);
116154af6042SHugh Dickins 		page = NULL;
116254af6042SHugh Dickins 	}
116354af6042SHugh Dickins 
11641635f6a7SHugh Dickins 	if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
116554af6042SHugh Dickins 	    ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
116654af6042SHugh Dickins 		error = -EINVAL;
1167267a4c76SHugh Dickins 		goto unlock;
116854af6042SHugh Dickins 	}
116954af6042SHugh Dickins 
117066d2f4d2SHugh Dickins 	if (page && sgp == SGP_WRITE)
117166d2f4d2SHugh Dickins 		mark_page_accessed(page);
117266d2f4d2SHugh Dickins 
11731635f6a7SHugh Dickins 	/* fallocated page? */
11741635f6a7SHugh Dickins 	if (page && !PageUptodate(page)) {
11751635f6a7SHugh Dickins 		if (sgp != SGP_READ)
11761635f6a7SHugh Dickins 			goto clear;
11771635f6a7SHugh Dickins 		unlock_page(page);
11781635f6a7SHugh Dickins 		page_cache_release(page);
11791635f6a7SHugh Dickins 		page = NULL;
11801635f6a7SHugh Dickins 	}
118154af6042SHugh Dickins 	if (page || (sgp == SGP_READ && !swap.val)) {
118254af6042SHugh Dickins 		*pagep = page;
118354af6042SHugh Dickins 		return 0;
118427ab7006SHugh Dickins 	}
118527ab7006SHugh Dickins 
1186b409f9fcSHugh Dickins 	/*
118754af6042SHugh Dickins 	 * Fast cache lookup did not find it:
118854af6042SHugh Dickins 	 * bring it back from swap or allocate.
1189b409f9fcSHugh Dickins 	 */
119054af6042SHugh Dickins 	info = SHMEM_I(inode);
119154af6042SHugh Dickins 	sbinfo = SHMEM_SB(inode->i_sb);
119227ab7006SHugh Dickins 
11931da177e4SLinus Torvalds 	if (swap.val) {
11941da177e4SLinus Torvalds 		/* Look it up and read it in.. */
119527ab7006SHugh Dickins 		page = lookup_swap_cache(swap);
119627ab7006SHugh Dickins 		if (!page) {
1197456f998eSYing Han 			/* here we actually do the io */
119868da9f05SHugh Dickins 			if (fault_type)
119968da9f05SHugh Dickins 				*fault_type |= VM_FAULT_MAJOR;
120041ffe5d5SHugh Dickins 			page = shmem_swapin(swap, gfp, info, index);
120127ab7006SHugh Dickins 			if (!page) {
12021da177e4SLinus Torvalds 				error = -ENOMEM;
120354af6042SHugh Dickins 				goto failed;
1204285b2c4fSHugh Dickins 			}
12051da177e4SLinus Torvalds 		}
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 		/* We have to do this with page locked to prevent races */
120854af6042SHugh Dickins 		lock_page(page);
12090142ef6cSHugh Dickins 		if (!PageSwapCache(page) || page_private(page) != swap.val ||
1210d1899228SHugh Dickins 		    !shmem_confirm_swap(mapping, index, swap)) {
1211bde05d1cSHugh Dickins 			error = -EEXIST;	/* try again */
1212d1899228SHugh Dickins 			goto unlock;
1213bde05d1cSHugh Dickins 		}
121427ab7006SHugh Dickins 		if (!PageUptodate(page)) {
12151da177e4SLinus Torvalds 			error = -EIO;
121654af6042SHugh Dickins 			goto failed;
121754af6042SHugh Dickins 		}
121854af6042SHugh Dickins 		wait_on_page_writeback(page);
121954af6042SHugh Dickins 
1220bde05d1cSHugh Dickins 		if (shmem_should_replace_page(page, gfp)) {
1221bde05d1cSHugh Dickins 			error = shmem_replace_page(&page, gfp, info, index);
1222bde05d1cSHugh Dickins 			if (error)
122354af6042SHugh Dickins 				goto failed;
12241da177e4SLinus Torvalds 		}
12251da177e4SLinus Torvalds 
1226f627c2f5SKirill A. Shutemov 		error = mem_cgroup_try_charge(page, current->mm, gfp, &memcg,
1227f627c2f5SKirill A. Shutemov 				false);
1228d1899228SHugh Dickins 		if (!error) {
122954af6042SHugh Dickins 			error = shmem_add_to_page_cache(page, mapping, index,
1230fed400a1SWang Sheng-Hui 						swp_to_radix_entry(swap));
1231215c02bcSHugh Dickins 			/*
1232215c02bcSHugh Dickins 			 * We already confirmed swap under page lock, and make
1233215c02bcSHugh Dickins 			 * no memory allocation here, so usually no possibility
1234215c02bcSHugh Dickins 			 * of error; but free_swap_and_cache() only trylocks a
1235215c02bcSHugh Dickins 			 * page, so it is just possible that the entry has been
1236215c02bcSHugh Dickins 			 * truncated or holepunched since swap was confirmed.
1237215c02bcSHugh Dickins 			 * shmem_undo_range() will have done some of the
1238215c02bcSHugh Dickins 			 * unaccounting, now delete_from_swap_cache() will do
123993aa7d95SVladimir Davydov 			 * the rest.
1240215c02bcSHugh Dickins 			 * Reset swap.val? No, leave it so "failed" goes back to
1241215c02bcSHugh Dickins 			 * "repeat": reading a hole and writing should succeed.
1242215c02bcSHugh Dickins 			 */
124300501b53SJohannes Weiner 			if (error) {
1244f627c2f5SKirill A. Shutemov 				mem_cgroup_cancel_charge(page, memcg, false);
1245215c02bcSHugh Dickins 				delete_from_swap_cache(page);
1246d1899228SHugh Dickins 			}
124700501b53SJohannes Weiner 		}
124854af6042SHugh Dickins 		if (error)
124954af6042SHugh Dickins 			goto failed;
125054af6042SHugh Dickins 
1251f627c2f5SKirill A. Shutemov 		mem_cgroup_commit_charge(page, memcg, true, false);
125200501b53SJohannes Weiner 
125354af6042SHugh Dickins 		spin_lock(&info->lock);
125454af6042SHugh Dickins 		info->swapped--;
125554af6042SHugh Dickins 		shmem_recalc_inode(inode);
12561da177e4SLinus Torvalds 		spin_unlock(&info->lock);
125727ab7006SHugh Dickins 
125866d2f4d2SHugh Dickins 		if (sgp == SGP_WRITE)
125966d2f4d2SHugh Dickins 			mark_page_accessed(page);
126066d2f4d2SHugh Dickins 
126127ab7006SHugh Dickins 		delete_from_swap_cache(page);
126227ab7006SHugh Dickins 		set_page_dirty(page);
126327ab7006SHugh Dickins 		swap_free(swap);
126427ab7006SHugh Dickins 
126554af6042SHugh Dickins 	} else {
126654af6042SHugh Dickins 		if (shmem_acct_block(info->flags)) {
126754af6042SHugh Dickins 			error = -ENOSPC;
126854af6042SHugh Dickins 			goto failed;
12691da177e4SLinus Torvalds 		}
12700edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
1271fc5da22aSHugh Dickins 			if (percpu_counter_compare(&sbinfo->used_blocks,
127254af6042SHugh Dickins 						sbinfo->max_blocks) >= 0) {
127354af6042SHugh Dickins 				error = -ENOSPC;
127454af6042SHugh Dickins 				goto unacct;
127554af6042SHugh Dickins 			}
12767e496299STim Chen 			percpu_counter_inc(&sbinfo->used_blocks);
127759a16eadSHugh Dickins 		}
12781da177e4SLinus Torvalds 
127954af6042SHugh Dickins 		page = shmem_alloc_page(gfp, info, index);
128054af6042SHugh Dickins 		if (!page) {
128154af6042SHugh Dickins 			error = -ENOMEM;
128254af6042SHugh Dickins 			goto decused;
128354af6042SHugh Dickins 		}
128454af6042SHugh Dickins 
128507a42788SMel Gorman 		__SetPageSwapBacked(page);
128648c935adSKirill A. Shutemov 		__SetPageLocked(page);
128766d2f4d2SHugh Dickins 		if (sgp == SGP_WRITE)
1288eb39d618SHugh Dickins 			__SetPageReferenced(page);
128966d2f4d2SHugh Dickins 
1290f627c2f5SKirill A. Shutemov 		error = mem_cgroup_try_charge(page, current->mm, gfp, &memcg,
1291f627c2f5SKirill A. Shutemov 				false);
129254af6042SHugh Dickins 		if (error)
129354af6042SHugh Dickins 			goto decused;
12945e4c0d97SJan Kara 		error = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK);
1295b065b432SHugh Dickins 		if (!error) {
1296b065b432SHugh Dickins 			error = shmem_add_to_page_cache(page, mapping, index,
1297fed400a1SWang Sheng-Hui 							NULL);
1298b065b432SHugh Dickins 			radix_tree_preload_end();
1299b065b432SHugh Dickins 		}
1300b065b432SHugh Dickins 		if (error) {
1301f627c2f5SKirill A. Shutemov 			mem_cgroup_cancel_charge(page, memcg, false);
1302b065b432SHugh Dickins 			goto decused;
1303b065b432SHugh Dickins 		}
1304f627c2f5SKirill A. Shutemov 		mem_cgroup_commit_charge(page, memcg, false, false);
130554af6042SHugh Dickins 		lru_cache_add_anon(page);
130654af6042SHugh Dickins 
130754af6042SHugh Dickins 		spin_lock(&info->lock);
13081da177e4SLinus Torvalds 		info->alloced++;
130954af6042SHugh Dickins 		inode->i_blocks += BLOCKS_PER_PAGE;
131054af6042SHugh Dickins 		shmem_recalc_inode(inode);
131159a16eadSHugh Dickins 		spin_unlock(&info->lock);
13121635f6a7SHugh Dickins 		alloced = true;
131354af6042SHugh Dickins 
1314ec9516fbSHugh Dickins 		/*
13151635f6a7SHugh Dickins 		 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
13161635f6a7SHugh Dickins 		 */
13171635f6a7SHugh Dickins 		if (sgp == SGP_FALLOC)
13181635f6a7SHugh Dickins 			sgp = SGP_WRITE;
13191635f6a7SHugh Dickins clear:
13201635f6a7SHugh Dickins 		/*
13211635f6a7SHugh Dickins 		 * Let SGP_WRITE caller clear ends if write does not fill page;
13221635f6a7SHugh Dickins 		 * but SGP_FALLOC on a page fallocated earlier must initialize
13231635f6a7SHugh Dickins 		 * it now, lest undo on failure cancel our earlier guarantee.
1324ec9516fbSHugh Dickins 		 */
1325ec9516fbSHugh Dickins 		if (sgp != SGP_WRITE) {
132627ab7006SHugh Dickins 			clear_highpage(page);
132727ab7006SHugh Dickins 			flush_dcache_page(page);
132827ab7006SHugh Dickins 			SetPageUptodate(page);
1329ec9516fbSHugh Dickins 		}
1330a0ee5ec5SHugh Dickins 		if (sgp == SGP_DIRTY)
133127ab7006SHugh Dickins 			set_page_dirty(page);
13321da177e4SLinus Torvalds 	}
1333bde05d1cSHugh Dickins 
133454af6042SHugh Dickins 	/* Perhaps the file has been truncated since we checked */
13351635f6a7SHugh Dickins 	if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
133654af6042SHugh Dickins 	    ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1337267a4c76SHugh Dickins 		if (alloced) {
1338267a4c76SHugh Dickins 			ClearPageDirty(page);
1339267a4c76SHugh Dickins 			delete_from_page_cache(page);
1340267a4c76SHugh Dickins 			spin_lock(&info->lock);
1341267a4c76SHugh Dickins 			shmem_recalc_inode(inode);
1342267a4c76SHugh Dickins 			spin_unlock(&info->lock);
1343267a4c76SHugh Dickins 		}
134454af6042SHugh Dickins 		error = -EINVAL;
1345267a4c76SHugh Dickins 		goto unlock;
1346ff36b801SShaohua Li 	}
134754af6042SHugh Dickins 	*pagep = page;
134854af6042SHugh Dickins 	return 0;
1349d00806b1SNick Piggin 
1350d0217ac0SNick Piggin 	/*
135154af6042SHugh Dickins 	 * Error recovery.
13521da177e4SLinus Torvalds 	 */
135354af6042SHugh Dickins decused:
135454af6042SHugh Dickins 	if (sbinfo->max_blocks)
135554af6042SHugh Dickins 		percpu_counter_add(&sbinfo->used_blocks, -1);
135654af6042SHugh Dickins unacct:
135754af6042SHugh Dickins 	shmem_unacct_blocks(info->flags, 1);
135854af6042SHugh Dickins failed:
1359267a4c76SHugh Dickins 	if (swap.val && !shmem_confirm_swap(mapping, index, swap))
136054af6042SHugh Dickins 		error = -EEXIST;
1361d1899228SHugh Dickins unlock:
136227ab7006SHugh Dickins 	if (page) {
136354af6042SHugh Dickins 		unlock_page(page);
13641da177e4SLinus Torvalds 		page_cache_release(page);
136554af6042SHugh Dickins 	}
136654af6042SHugh Dickins 	if (error == -ENOSPC && !once++) {
136754af6042SHugh Dickins 		info = SHMEM_I(inode);
136854af6042SHugh Dickins 		spin_lock(&info->lock);
136954af6042SHugh Dickins 		shmem_recalc_inode(inode);
137054af6042SHugh Dickins 		spin_unlock(&info->lock);
13711da177e4SLinus Torvalds 		goto repeat;
1372d8dc74f2SAdrian Bunk 	}
1373d1899228SHugh Dickins 	if (error == -EEXIST)	/* from above or from radix_tree_insert */
137454af6042SHugh Dickins 		goto repeat;
137554af6042SHugh Dickins 	return error;
13761da177e4SLinus Torvalds }
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
13791da177e4SLinus Torvalds {
1380496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
13811da177e4SLinus Torvalds 	int error;
138268da9f05SHugh Dickins 	int ret = VM_FAULT_LOCKED;
13831da177e4SLinus Torvalds 
1384f00cdc6dSHugh Dickins 	/*
1385f00cdc6dSHugh Dickins 	 * Trinity finds that probing a hole which tmpfs is punching can
1386f00cdc6dSHugh Dickins 	 * prevent the hole-punch from ever completing: which in turn
1387f00cdc6dSHugh Dickins 	 * locks writers out with its hold on i_mutex.  So refrain from
13888e205f77SHugh Dickins 	 * faulting pages into the hole while it's being punched.  Although
13898e205f77SHugh Dickins 	 * shmem_undo_range() does remove the additions, it may be unable to
13908e205f77SHugh Dickins 	 * keep up, as each new page needs its own unmap_mapping_range() call,
13918e205f77SHugh Dickins 	 * and the i_mmap tree grows ever slower to scan if new vmas are added.
13928e205f77SHugh Dickins 	 *
13938e205f77SHugh Dickins 	 * It does not matter if we sometimes reach this check just before the
13948e205f77SHugh Dickins 	 * hole-punch begins, so that one fault then races with the punch:
13958e205f77SHugh Dickins 	 * we just need to make racing faults a rare case.
13968e205f77SHugh Dickins 	 *
13978e205f77SHugh Dickins 	 * The implementation below would be much simpler if we just used a
13988e205f77SHugh Dickins 	 * standard mutex or completion: but we cannot take i_mutex in fault,
13998e205f77SHugh Dickins 	 * and bloating every shmem inode for this unlikely case would be sad.
1400f00cdc6dSHugh Dickins 	 */
1401f00cdc6dSHugh Dickins 	if (unlikely(inode->i_private)) {
1402f00cdc6dSHugh Dickins 		struct shmem_falloc *shmem_falloc;
1403f00cdc6dSHugh Dickins 
1404f00cdc6dSHugh Dickins 		spin_lock(&inode->i_lock);
1405f00cdc6dSHugh Dickins 		shmem_falloc = inode->i_private;
14068e205f77SHugh Dickins 		if (shmem_falloc &&
14078e205f77SHugh Dickins 		    shmem_falloc->waitq &&
14088e205f77SHugh Dickins 		    vmf->pgoff >= shmem_falloc->start &&
14098e205f77SHugh Dickins 		    vmf->pgoff < shmem_falloc->next) {
14108e205f77SHugh Dickins 			wait_queue_head_t *shmem_falloc_waitq;
14118e205f77SHugh Dickins 			DEFINE_WAIT(shmem_fault_wait);
14128e205f77SHugh Dickins 
14138e205f77SHugh Dickins 			ret = VM_FAULT_NOPAGE;
1414f00cdc6dSHugh Dickins 			if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1415f00cdc6dSHugh Dickins 			   !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
14168e205f77SHugh Dickins 				/* It's polite to up mmap_sem if we can */
1417f00cdc6dSHugh Dickins 				up_read(&vma->vm_mm->mmap_sem);
14188e205f77SHugh Dickins 				ret = VM_FAULT_RETRY;
1419f00cdc6dSHugh Dickins 			}
14208e205f77SHugh Dickins 
14218e205f77SHugh Dickins 			shmem_falloc_waitq = shmem_falloc->waitq;
14228e205f77SHugh Dickins 			prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
14238e205f77SHugh Dickins 					TASK_UNINTERRUPTIBLE);
14248e205f77SHugh Dickins 			spin_unlock(&inode->i_lock);
14258e205f77SHugh Dickins 			schedule();
14268e205f77SHugh Dickins 
14278e205f77SHugh Dickins 			/*
14288e205f77SHugh Dickins 			 * shmem_falloc_waitq points into the shmem_fallocate()
14298e205f77SHugh Dickins 			 * stack of the hole-punching task: shmem_falloc_waitq
14308e205f77SHugh Dickins 			 * is usually invalid by the time we reach here, but
14318e205f77SHugh Dickins 			 * finish_wait() does not dereference it in that case;
14328e205f77SHugh Dickins 			 * though i_lock needed lest racing with wake_up_all().
14338e205f77SHugh Dickins 			 */
14348e205f77SHugh Dickins 			spin_lock(&inode->i_lock);
14358e205f77SHugh Dickins 			finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
14368e205f77SHugh Dickins 			spin_unlock(&inode->i_lock);
14378e205f77SHugh Dickins 			return ret;
1438f00cdc6dSHugh Dickins 		}
14398e205f77SHugh Dickins 		spin_unlock(&inode->i_lock);
1440f00cdc6dSHugh Dickins 	}
1441f00cdc6dSHugh Dickins 
14421da177e4SLinus Torvalds 	error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
14431da177e4SLinus Torvalds 	if (error)
14441da177e4SLinus Torvalds 		return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
144568da9f05SHugh Dickins 
1446456f998eSYing Han 	if (ret & VM_FAULT_MAJOR) {
1447456f998eSYing Han 		count_vm_event(PGMAJFAULT);
1448456f998eSYing Han 		mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1449456f998eSYing Han 	}
145068da9f05SHugh Dickins 	return ret;
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds #ifdef CONFIG_NUMA
145441ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
14551da177e4SLinus Torvalds {
1456496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
145741ffe5d5SHugh Dickins 	return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
14581da177e4SLinus Torvalds }
14591da177e4SLinus Torvalds 
1460d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1461d8dc74f2SAdrian Bunk 					  unsigned long addr)
14621da177e4SLinus Torvalds {
1463496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
146441ffe5d5SHugh Dickins 	pgoff_t index;
14651da177e4SLinus Torvalds 
146641ffe5d5SHugh Dickins 	index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
146741ffe5d5SHugh Dickins 	return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
14681da177e4SLinus Torvalds }
14691da177e4SLinus Torvalds #endif
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user)
14721da177e4SLinus Torvalds {
1473496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
14741da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
14751da177e4SLinus Torvalds 	int retval = -ENOMEM;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	spin_lock(&info->lock);
14781da177e4SLinus Torvalds 	if (lock && !(info->flags & VM_LOCKED)) {
14791da177e4SLinus Torvalds 		if (!user_shm_lock(inode->i_size, user))
14801da177e4SLinus Torvalds 			goto out_nomem;
14811da177e4SLinus Torvalds 		info->flags |= VM_LOCKED;
148289e004eaSLee Schermerhorn 		mapping_set_unevictable(file->f_mapping);
14831da177e4SLinus Torvalds 	}
14841da177e4SLinus Torvalds 	if (!lock && (info->flags & VM_LOCKED) && user) {
14851da177e4SLinus Torvalds 		user_shm_unlock(inode->i_size, user);
14861da177e4SLinus Torvalds 		info->flags &= ~VM_LOCKED;
148789e004eaSLee Schermerhorn 		mapping_clear_unevictable(file->f_mapping);
14881da177e4SLinus Torvalds 	}
14891da177e4SLinus Torvalds 	retval = 0;
149089e004eaSLee Schermerhorn 
14911da177e4SLinus Torvalds out_nomem:
14921da177e4SLinus Torvalds 	spin_unlock(&info->lock);
14931da177e4SLinus Torvalds 	return retval;
14941da177e4SLinus Torvalds }
14951da177e4SLinus Torvalds 
14969b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
14971da177e4SLinus Torvalds {
14981da177e4SLinus Torvalds 	file_accessed(file);
14991da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
15001da177e4SLinus Torvalds 	return 0;
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds 
1503454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
150409208d15SAl Viro 				     umode_t mode, dev_t dev, unsigned long flags)
15051da177e4SLinus Torvalds {
15061da177e4SLinus Torvalds 	struct inode *inode;
15071da177e4SLinus Torvalds 	struct shmem_inode_info *info;
15081da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
15091da177e4SLinus Torvalds 
15105b04c689SPavel Emelyanov 	if (shmem_reserve_inode(sb))
15111da177e4SLinus Torvalds 		return NULL;
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 	inode = new_inode(sb);
15141da177e4SLinus Torvalds 	if (inode) {
151585fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
1516454abafeSDmitry Monakhov 		inode_init_owner(inode, dir, mode);
15171da177e4SLinus Torvalds 		inode->i_blocks = 0;
15181da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
151991828a40SDavid M. Grimes 		inode->i_generation = get_seconds();
15201da177e4SLinus Torvalds 		info = SHMEM_I(inode);
15211da177e4SLinus Torvalds 		memset(info, 0, (char *)inode - (char *)info);
15221da177e4SLinus Torvalds 		spin_lock_init(&info->lock);
152340e041a2SDavid Herrmann 		info->seals = F_SEAL_SEAL;
15240b0a0806SHugh Dickins 		info->flags = flags & VM_NORESERVE;
15251da177e4SLinus Torvalds 		INIT_LIST_HEAD(&info->swaplist);
152638f38657SAristeu Rozanski 		simple_xattrs_init(&info->xattrs);
152772c04902SAl Viro 		cache_no_acl(inode);
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
15301da177e4SLinus Torvalds 		default:
153139f0247dSAndreas Gruenbacher 			inode->i_op = &shmem_special_inode_operations;
15321da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
15331da177e4SLinus Torvalds 			break;
15341da177e4SLinus Torvalds 		case S_IFREG:
153514fcc23fSHugh Dickins 			inode->i_mapping->a_ops = &shmem_aops;
15361da177e4SLinus Torvalds 			inode->i_op = &shmem_inode_operations;
15371da177e4SLinus Torvalds 			inode->i_fop = &shmem_file_operations;
153871fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy,
153971fe804bSLee Schermerhorn 						 shmem_get_sbmpol(sbinfo));
15401da177e4SLinus Torvalds 			break;
15411da177e4SLinus Torvalds 		case S_IFDIR:
1542d8c76e6fSDave Hansen 			inc_nlink(inode);
15431da177e4SLinus Torvalds 			/* Some things misbehave if size == 0 on a directory */
15441da177e4SLinus Torvalds 			inode->i_size = 2 * BOGO_DIRENT_SIZE;
15451da177e4SLinus Torvalds 			inode->i_op = &shmem_dir_inode_operations;
15461da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
15471da177e4SLinus Torvalds 			break;
15481da177e4SLinus Torvalds 		case S_IFLNK:
15491da177e4SLinus Torvalds 			/*
15501da177e4SLinus Torvalds 			 * Must not load anything in the rbtree,
15511da177e4SLinus Torvalds 			 * mpol_free_shared_policy will not be called.
15521da177e4SLinus Torvalds 			 */
155371fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy, NULL);
15541da177e4SLinus Torvalds 			break;
15551da177e4SLinus Torvalds 		}
15565b04c689SPavel Emelyanov 	} else
15575b04c689SPavel Emelyanov 		shmem_free_inode(sb);
15581da177e4SLinus Torvalds 	return inode;
15591da177e4SLinus Torvalds }
15601da177e4SLinus Torvalds 
15610cd6144aSJohannes Weiner bool shmem_mapping(struct address_space *mapping)
15620cd6144aSJohannes Weiner {
1563f0774d88SSasha Levin 	if (!mapping->host)
1564f0774d88SSasha Levin 		return false;
1565f0774d88SSasha Levin 
156697b713baSChristoph Hellwig 	return mapping->host->i_sb->s_op == &shmem_ops;
15670cd6144aSJohannes Weiner }
15680cd6144aSJohannes Weiner 
15691da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
157092e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations;
157169f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations;
15721da177e4SLinus Torvalds 
15736d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR
15746d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
15756d9d88d0SJarkko Sakkinen #else
15766d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL
15776d9d88d0SJarkko Sakkinen #endif
15786d9d88d0SJarkko Sakkinen 
15791da177e4SLinus Torvalds static int
1580800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping,
1581800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
1582800d15a5SNick Piggin 			struct page **pagep, void **fsdata)
15831da177e4SLinus Torvalds {
1584800d15a5SNick Piggin 	struct inode *inode = mapping->host;
158540e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
1586800d15a5SNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
158740e041a2SDavid Herrmann 
158840e041a2SDavid Herrmann 	/* i_mutex is held by caller */
158940e041a2SDavid Herrmann 	if (unlikely(info->seals)) {
159040e041a2SDavid Herrmann 		if (info->seals & F_SEAL_WRITE)
159140e041a2SDavid Herrmann 			return -EPERM;
159240e041a2SDavid Herrmann 		if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
159340e041a2SDavid Herrmann 			return -EPERM;
159440e041a2SDavid Herrmann 	}
159540e041a2SDavid Herrmann 
159666d2f4d2SHugh Dickins 	return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1597800d15a5SNick Piggin }
1598800d15a5SNick Piggin 
1599800d15a5SNick Piggin static int
1600800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping,
1601800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
1602800d15a5SNick Piggin 			struct page *page, void *fsdata)
1603800d15a5SNick Piggin {
1604800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1605800d15a5SNick Piggin 
1606800d15a5SNick Piggin 	if (pos + copied > inode->i_size)
1607800d15a5SNick Piggin 		i_size_write(inode, pos + copied);
1608800d15a5SNick Piggin 
1609ec9516fbSHugh Dickins 	if (!PageUptodate(page)) {
1610ec9516fbSHugh Dickins 		if (copied < PAGE_CACHE_SIZE) {
1611ec9516fbSHugh Dickins 			unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1612ec9516fbSHugh Dickins 			zero_user_segments(page, 0, from,
1613ec9516fbSHugh Dickins 					from + copied, PAGE_CACHE_SIZE);
1614ec9516fbSHugh Dickins 		}
1615ec9516fbSHugh Dickins 		SetPageUptodate(page);
1616ec9516fbSHugh Dickins 	}
1617d3602444SHugh Dickins 	set_page_dirty(page);
16186746aff7SWu Fengguang 	unlock_page(page);
1619d3602444SHugh Dickins 	page_cache_release(page);
1620d3602444SHugh Dickins 
1621800d15a5SNick Piggin 	return copied;
16221da177e4SLinus Torvalds }
16231da177e4SLinus Torvalds 
16242ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
16251da177e4SLinus Torvalds {
16266e58e79dSAl Viro 	struct file *file = iocb->ki_filp;
16276e58e79dSAl Viro 	struct inode *inode = file_inode(file);
16281da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
162941ffe5d5SHugh Dickins 	pgoff_t index;
163041ffe5d5SHugh Dickins 	unsigned long offset;
1631a0ee5ec5SHugh Dickins 	enum sgp_type sgp = SGP_READ;
1632f7c1d074SGeert Uytterhoeven 	int error = 0;
1633cb66a7a1SAl Viro 	ssize_t retval = 0;
16346e58e79dSAl Viro 	loff_t *ppos = &iocb->ki_pos;
1635a0ee5ec5SHugh Dickins 
1636a0ee5ec5SHugh Dickins 	/*
1637a0ee5ec5SHugh Dickins 	 * Might this read be for a stacking filesystem?  Then when reading
1638a0ee5ec5SHugh Dickins 	 * holes of a sparse file, we actually need to allocate those pages,
1639a0ee5ec5SHugh Dickins 	 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1640a0ee5ec5SHugh Dickins 	 */
1641777eda2cSAl Viro 	if (!iter_is_iovec(to))
1642a0ee5ec5SHugh Dickins 		sgp = SGP_DIRTY;
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 	index = *ppos >> PAGE_CACHE_SHIFT;
16451da177e4SLinus Torvalds 	offset = *ppos & ~PAGE_CACHE_MASK;
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 	for (;;) {
16481da177e4SLinus Torvalds 		struct page *page = NULL;
164941ffe5d5SHugh Dickins 		pgoff_t end_index;
165041ffe5d5SHugh Dickins 		unsigned long nr, ret;
16511da177e4SLinus Torvalds 		loff_t i_size = i_size_read(inode);
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
16541da177e4SLinus Torvalds 		if (index > end_index)
16551da177e4SLinus Torvalds 			break;
16561da177e4SLinus Torvalds 		if (index == end_index) {
16571da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
16581da177e4SLinus Torvalds 			if (nr <= offset)
16591da177e4SLinus Torvalds 				break;
16601da177e4SLinus Torvalds 		}
16611da177e4SLinus Torvalds 
16626e58e79dSAl Viro 		error = shmem_getpage(inode, index, &page, sgp, NULL);
16636e58e79dSAl Viro 		if (error) {
16646e58e79dSAl Viro 			if (error == -EINVAL)
16656e58e79dSAl Viro 				error = 0;
16661da177e4SLinus Torvalds 			break;
16671da177e4SLinus Torvalds 		}
1668d3602444SHugh Dickins 		if (page)
1669d3602444SHugh Dickins 			unlock_page(page);
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 		/*
16721da177e4SLinus Torvalds 		 * We must evaluate after, since reads (unlike writes)
16731b1dcc1bSJes Sorensen 		 * are called without i_mutex protection against truncate
16741da177e4SLinus Torvalds 		 */
16751da177e4SLinus Torvalds 		nr = PAGE_CACHE_SIZE;
16761da177e4SLinus Torvalds 		i_size = i_size_read(inode);
16771da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
16781da177e4SLinus Torvalds 		if (index == end_index) {
16791da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
16801da177e4SLinus Torvalds 			if (nr <= offset) {
16811da177e4SLinus Torvalds 				if (page)
16821da177e4SLinus Torvalds 					page_cache_release(page);
16831da177e4SLinus Torvalds 				break;
16841da177e4SLinus Torvalds 			}
16851da177e4SLinus Torvalds 		}
16861da177e4SLinus Torvalds 		nr -= offset;
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds 		if (page) {
16891da177e4SLinus Torvalds 			/*
16901da177e4SLinus Torvalds 			 * If users can be writing to this page using arbitrary
16911da177e4SLinus Torvalds 			 * virtual addresses, take care about potential aliasing
16921da177e4SLinus Torvalds 			 * before reading the page on the kernel side.
16931da177e4SLinus Torvalds 			 */
16941da177e4SLinus Torvalds 			if (mapping_writably_mapped(mapping))
16951da177e4SLinus Torvalds 				flush_dcache_page(page);
16961da177e4SLinus Torvalds 			/*
16971da177e4SLinus Torvalds 			 * Mark the page accessed if we read the beginning.
16981da177e4SLinus Torvalds 			 */
16991da177e4SLinus Torvalds 			if (!offset)
17001da177e4SLinus Torvalds 				mark_page_accessed(page);
1701b5810039SNick Piggin 		} else {
17021da177e4SLinus Torvalds 			page = ZERO_PAGE(0);
1703b5810039SNick Piggin 			page_cache_get(page);
1704b5810039SNick Piggin 		}
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds 		/*
17071da177e4SLinus Torvalds 		 * Ok, we have the page, and it's up-to-date, so
17081da177e4SLinus Torvalds 		 * now we can copy it to user space...
17091da177e4SLinus Torvalds 		 */
17102ba5bbedSAl Viro 		ret = copy_page_to_iter(page, offset, nr, to);
17116e58e79dSAl Viro 		retval += ret;
17121da177e4SLinus Torvalds 		offset += ret;
17131da177e4SLinus Torvalds 		index += offset >> PAGE_CACHE_SHIFT;
17141da177e4SLinus Torvalds 		offset &= ~PAGE_CACHE_MASK;
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds 		page_cache_release(page);
17172ba5bbedSAl Viro 		if (!iov_iter_count(to))
17181da177e4SLinus Torvalds 			break;
17196e58e79dSAl Viro 		if (ret < nr) {
17206e58e79dSAl Viro 			error = -EFAULT;
17216e58e79dSAl Viro 			break;
17226e58e79dSAl Viro 		}
17231da177e4SLinus Torvalds 		cond_resched();
17241da177e4SLinus Torvalds 	}
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
17276e58e79dSAl Viro 	file_accessed(file);
17286e58e79dSAl Viro 	return retval ? retval : error;
17291da177e4SLinus Torvalds }
17301da177e4SLinus Torvalds 
1731708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1732708e3508SHugh Dickins 				struct pipe_inode_info *pipe, size_t len,
1733708e3508SHugh Dickins 				unsigned int flags)
1734708e3508SHugh Dickins {
1735708e3508SHugh Dickins 	struct address_space *mapping = in->f_mapping;
173671f0e07aSHugh Dickins 	struct inode *inode = mapping->host;
1737708e3508SHugh Dickins 	unsigned int loff, nr_pages, req_pages;
1738708e3508SHugh Dickins 	struct page *pages[PIPE_DEF_BUFFERS];
1739708e3508SHugh Dickins 	struct partial_page partial[PIPE_DEF_BUFFERS];
1740708e3508SHugh Dickins 	struct page *page;
1741708e3508SHugh Dickins 	pgoff_t index, end_index;
1742708e3508SHugh Dickins 	loff_t isize, left;
1743708e3508SHugh Dickins 	int error, page_nr;
1744708e3508SHugh Dickins 	struct splice_pipe_desc spd = {
1745708e3508SHugh Dickins 		.pages = pages,
1746708e3508SHugh Dickins 		.partial = partial,
1747047fe360SEric Dumazet 		.nr_pages_max = PIPE_DEF_BUFFERS,
1748708e3508SHugh Dickins 		.flags = flags,
1749708e3508SHugh Dickins 		.ops = &page_cache_pipe_buf_ops,
1750708e3508SHugh Dickins 		.spd_release = spd_release_page,
1751708e3508SHugh Dickins 	};
1752708e3508SHugh Dickins 
175371f0e07aSHugh Dickins 	isize = i_size_read(inode);
1754708e3508SHugh Dickins 	if (unlikely(*ppos >= isize))
1755708e3508SHugh Dickins 		return 0;
1756708e3508SHugh Dickins 
1757708e3508SHugh Dickins 	left = isize - *ppos;
1758708e3508SHugh Dickins 	if (unlikely(left < len))
1759708e3508SHugh Dickins 		len = left;
1760708e3508SHugh Dickins 
1761708e3508SHugh Dickins 	if (splice_grow_spd(pipe, &spd))
1762708e3508SHugh Dickins 		return -ENOMEM;
1763708e3508SHugh Dickins 
1764708e3508SHugh Dickins 	index = *ppos >> PAGE_CACHE_SHIFT;
1765708e3508SHugh Dickins 	loff = *ppos & ~PAGE_CACHE_MASK;
1766708e3508SHugh Dickins 	req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1767a786c06dSAl Viro 	nr_pages = min(req_pages, spd.nr_pages_max);
1768708e3508SHugh Dickins 
1769708e3508SHugh Dickins 	spd.nr_pages = find_get_pages_contig(mapping, index,
1770708e3508SHugh Dickins 						nr_pages, spd.pages);
1771708e3508SHugh Dickins 	index += spd.nr_pages;
1772708e3508SHugh Dickins 	error = 0;
177371f0e07aSHugh Dickins 
1774708e3508SHugh Dickins 	while (spd.nr_pages < nr_pages) {
177571f0e07aSHugh Dickins 		error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
177671f0e07aSHugh Dickins 		if (error)
1777708e3508SHugh Dickins 			break;
1778708e3508SHugh Dickins 		unlock_page(page);
1779708e3508SHugh Dickins 		spd.pages[spd.nr_pages++] = page;
1780708e3508SHugh Dickins 		index++;
1781708e3508SHugh Dickins 	}
1782708e3508SHugh Dickins 
1783708e3508SHugh Dickins 	index = *ppos >> PAGE_CACHE_SHIFT;
1784708e3508SHugh Dickins 	nr_pages = spd.nr_pages;
1785708e3508SHugh Dickins 	spd.nr_pages = 0;
178671f0e07aSHugh Dickins 
1787708e3508SHugh Dickins 	for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1788708e3508SHugh Dickins 		unsigned int this_len;
1789708e3508SHugh Dickins 
1790708e3508SHugh Dickins 		if (!len)
1791708e3508SHugh Dickins 			break;
1792708e3508SHugh Dickins 
1793708e3508SHugh Dickins 		this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1794708e3508SHugh Dickins 		page = spd.pages[page_nr];
1795708e3508SHugh Dickins 
179671f0e07aSHugh Dickins 		if (!PageUptodate(page) || page->mapping != mapping) {
179771f0e07aSHugh Dickins 			error = shmem_getpage(inode, index, &page,
179871f0e07aSHugh Dickins 							SGP_CACHE, NULL);
179971f0e07aSHugh Dickins 			if (error)
1800708e3508SHugh Dickins 				break;
180171f0e07aSHugh Dickins 			unlock_page(page);
1802708e3508SHugh Dickins 			page_cache_release(spd.pages[page_nr]);
1803708e3508SHugh Dickins 			spd.pages[page_nr] = page;
1804708e3508SHugh Dickins 		}
1805708e3508SHugh Dickins 
180671f0e07aSHugh Dickins 		isize = i_size_read(inode);
1807708e3508SHugh Dickins 		end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1808708e3508SHugh Dickins 		if (unlikely(!isize || index > end_index))
1809708e3508SHugh Dickins 			break;
1810708e3508SHugh Dickins 
1811708e3508SHugh Dickins 		if (end_index == index) {
1812708e3508SHugh Dickins 			unsigned int plen;
1813708e3508SHugh Dickins 
1814708e3508SHugh Dickins 			plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1815708e3508SHugh Dickins 			if (plen <= loff)
1816708e3508SHugh Dickins 				break;
1817708e3508SHugh Dickins 
1818708e3508SHugh Dickins 			this_len = min(this_len, plen - loff);
1819708e3508SHugh Dickins 			len = this_len;
1820708e3508SHugh Dickins 		}
1821708e3508SHugh Dickins 
1822708e3508SHugh Dickins 		spd.partial[page_nr].offset = loff;
1823708e3508SHugh Dickins 		spd.partial[page_nr].len = this_len;
1824708e3508SHugh Dickins 		len -= this_len;
1825708e3508SHugh Dickins 		loff = 0;
1826708e3508SHugh Dickins 		spd.nr_pages++;
1827708e3508SHugh Dickins 		index++;
1828708e3508SHugh Dickins 	}
1829708e3508SHugh Dickins 
1830708e3508SHugh Dickins 	while (page_nr < nr_pages)
1831708e3508SHugh Dickins 		page_cache_release(spd.pages[page_nr++]);
1832708e3508SHugh Dickins 
1833708e3508SHugh Dickins 	if (spd.nr_pages)
1834708e3508SHugh Dickins 		error = splice_to_pipe(pipe, &spd);
1835708e3508SHugh Dickins 
1836047fe360SEric Dumazet 	splice_shrink_spd(&spd);
1837708e3508SHugh Dickins 
1838708e3508SHugh Dickins 	if (error > 0) {
1839708e3508SHugh Dickins 		*ppos += error;
1840708e3508SHugh Dickins 		file_accessed(in);
1841708e3508SHugh Dickins 	}
1842708e3508SHugh Dickins 	return error;
1843708e3508SHugh Dickins }
1844708e3508SHugh Dickins 
1845220f2ac9SHugh Dickins /*
1846220f2ac9SHugh Dickins  * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
1847220f2ac9SHugh Dickins  */
1848220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
1849965c8e59SAndrew Morton 				    pgoff_t index, pgoff_t end, int whence)
1850220f2ac9SHugh Dickins {
1851220f2ac9SHugh Dickins 	struct page *page;
1852220f2ac9SHugh Dickins 	struct pagevec pvec;
1853220f2ac9SHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
1854220f2ac9SHugh Dickins 	bool done = false;
1855220f2ac9SHugh Dickins 	int i;
1856220f2ac9SHugh Dickins 
1857220f2ac9SHugh Dickins 	pagevec_init(&pvec, 0);
1858220f2ac9SHugh Dickins 	pvec.nr = 1;		/* start small: we may be there already */
1859220f2ac9SHugh Dickins 	while (!done) {
18600cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
1861220f2ac9SHugh Dickins 					pvec.nr, pvec.pages, indices);
1862220f2ac9SHugh Dickins 		if (!pvec.nr) {
1863965c8e59SAndrew Morton 			if (whence == SEEK_DATA)
1864220f2ac9SHugh Dickins 				index = end;
1865220f2ac9SHugh Dickins 			break;
1866220f2ac9SHugh Dickins 		}
1867220f2ac9SHugh Dickins 		for (i = 0; i < pvec.nr; i++, index++) {
1868220f2ac9SHugh Dickins 			if (index < indices[i]) {
1869965c8e59SAndrew Morton 				if (whence == SEEK_HOLE) {
1870220f2ac9SHugh Dickins 					done = true;
1871220f2ac9SHugh Dickins 					break;
1872220f2ac9SHugh Dickins 				}
1873220f2ac9SHugh Dickins 				index = indices[i];
1874220f2ac9SHugh Dickins 			}
1875220f2ac9SHugh Dickins 			page = pvec.pages[i];
1876220f2ac9SHugh Dickins 			if (page && !radix_tree_exceptional_entry(page)) {
1877220f2ac9SHugh Dickins 				if (!PageUptodate(page))
1878220f2ac9SHugh Dickins 					page = NULL;
1879220f2ac9SHugh Dickins 			}
1880220f2ac9SHugh Dickins 			if (index >= end ||
1881965c8e59SAndrew Morton 			    (page && whence == SEEK_DATA) ||
1882965c8e59SAndrew Morton 			    (!page && whence == SEEK_HOLE)) {
1883220f2ac9SHugh Dickins 				done = true;
1884220f2ac9SHugh Dickins 				break;
1885220f2ac9SHugh Dickins 			}
1886220f2ac9SHugh Dickins 		}
18870cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
1888220f2ac9SHugh Dickins 		pagevec_release(&pvec);
1889220f2ac9SHugh Dickins 		pvec.nr = PAGEVEC_SIZE;
1890220f2ac9SHugh Dickins 		cond_resched();
1891220f2ac9SHugh Dickins 	}
1892220f2ac9SHugh Dickins 	return index;
1893220f2ac9SHugh Dickins }
1894220f2ac9SHugh Dickins 
1895965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
1896220f2ac9SHugh Dickins {
1897220f2ac9SHugh Dickins 	struct address_space *mapping = file->f_mapping;
1898220f2ac9SHugh Dickins 	struct inode *inode = mapping->host;
1899220f2ac9SHugh Dickins 	pgoff_t start, end;
1900220f2ac9SHugh Dickins 	loff_t new_offset;
1901220f2ac9SHugh Dickins 
1902965c8e59SAndrew Morton 	if (whence != SEEK_DATA && whence != SEEK_HOLE)
1903965c8e59SAndrew Morton 		return generic_file_llseek_size(file, offset, whence,
1904220f2ac9SHugh Dickins 					MAX_LFS_FILESIZE, i_size_read(inode));
1905*5955102cSAl Viro 	inode_lock(inode);
1906220f2ac9SHugh Dickins 	/* We're holding i_mutex so we can access i_size directly */
1907220f2ac9SHugh Dickins 
1908220f2ac9SHugh Dickins 	if (offset < 0)
1909220f2ac9SHugh Dickins 		offset = -EINVAL;
1910220f2ac9SHugh Dickins 	else if (offset >= inode->i_size)
1911220f2ac9SHugh Dickins 		offset = -ENXIO;
1912220f2ac9SHugh Dickins 	else {
1913220f2ac9SHugh Dickins 		start = offset >> PAGE_CACHE_SHIFT;
1914220f2ac9SHugh Dickins 		end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1915965c8e59SAndrew Morton 		new_offset = shmem_seek_hole_data(mapping, start, end, whence);
1916220f2ac9SHugh Dickins 		new_offset <<= PAGE_CACHE_SHIFT;
1917220f2ac9SHugh Dickins 		if (new_offset > offset) {
1918220f2ac9SHugh Dickins 			if (new_offset < inode->i_size)
1919220f2ac9SHugh Dickins 				offset = new_offset;
1920965c8e59SAndrew Morton 			else if (whence == SEEK_DATA)
1921220f2ac9SHugh Dickins 				offset = -ENXIO;
1922220f2ac9SHugh Dickins 			else
1923220f2ac9SHugh Dickins 				offset = inode->i_size;
1924220f2ac9SHugh Dickins 		}
1925220f2ac9SHugh Dickins 	}
1926220f2ac9SHugh Dickins 
1927387aae6fSHugh Dickins 	if (offset >= 0)
192846a1c2c7SJie Liu 		offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
1929*5955102cSAl Viro 	inode_unlock(inode);
1930220f2ac9SHugh Dickins 	return offset;
1931220f2ac9SHugh Dickins }
1932220f2ac9SHugh Dickins 
193305f65b5cSDavid Herrmann /*
193405f65b5cSDavid Herrmann  * We need a tag: a new tag would expand every radix_tree_node by 8 bytes,
193505f65b5cSDavid Herrmann  * so reuse a tag which we firmly believe is never set or cleared on shmem.
193605f65b5cSDavid Herrmann  */
193705f65b5cSDavid Herrmann #define SHMEM_TAG_PINNED        PAGECACHE_TAG_TOWRITE
193805f65b5cSDavid Herrmann #define LAST_SCAN               4       /* about 150ms max */
193905f65b5cSDavid Herrmann 
194005f65b5cSDavid Herrmann static void shmem_tag_pins(struct address_space *mapping)
194105f65b5cSDavid Herrmann {
194205f65b5cSDavid Herrmann 	struct radix_tree_iter iter;
194305f65b5cSDavid Herrmann 	void **slot;
194405f65b5cSDavid Herrmann 	pgoff_t start;
194505f65b5cSDavid Herrmann 	struct page *page;
194605f65b5cSDavid Herrmann 
194705f65b5cSDavid Herrmann 	lru_add_drain();
194805f65b5cSDavid Herrmann 	start = 0;
194905f65b5cSDavid Herrmann 	rcu_read_lock();
195005f65b5cSDavid Herrmann 
195105f65b5cSDavid Herrmann restart:
195205f65b5cSDavid Herrmann 	radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
195305f65b5cSDavid Herrmann 		page = radix_tree_deref_slot(slot);
195405f65b5cSDavid Herrmann 		if (!page || radix_tree_exception(page)) {
195505f65b5cSDavid Herrmann 			if (radix_tree_deref_retry(page))
195605f65b5cSDavid Herrmann 				goto restart;
195705f65b5cSDavid Herrmann 		} else if (page_count(page) - page_mapcount(page) > 1) {
195805f65b5cSDavid Herrmann 			spin_lock_irq(&mapping->tree_lock);
195905f65b5cSDavid Herrmann 			radix_tree_tag_set(&mapping->page_tree, iter.index,
196005f65b5cSDavid Herrmann 					   SHMEM_TAG_PINNED);
196105f65b5cSDavid Herrmann 			spin_unlock_irq(&mapping->tree_lock);
196205f65b5cSDavid Herrmann 		}
196305f65b5cSDavid Herrmann 
196405f65b5cSDavid Herrmann 		if (need_resched()) {
196505f65b5cSDavid Herrmann 			cond_resched_rcu();
196605f65b5cSDavid Herrmann 			start = iter.index + 1;
196705f65b5cSDavid Herrmann 			goto restart;
196805f65b5cSDavid Herrmann 		}
196905f65b5cSDavid Herrmann 	}
197005f65b5cSDavid Herrmann 	rcu_read_unlock();
197105f65b5cSDavid Herrmann }
197205f65b5cSDavid Herrmann 
197305f65b5cSDavid Herrmann /*
197405f65b5cSDavid Herrmann  * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
197505f65b5cSDavid Herrmann  * via get_user_pages(), drivers might have some pending I/O without any active
197605f65b5cSDavid Herrmann  * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
197705f65b5cSDavid Herrmann  * and see whether it has an elevated ref-count. If so, we tag them and wait for
197805f65b5cSDavid Herrmann  * them to be dropped.
197905f65b5cSDavid Herrmann  * The caller must guarantee that no new user will acquire writable references
198005f65b5cSDavid Herrmann  * to those pages to avoid races.
198105f65b5cSDavid Herrmann  */
198240e041a2SDavid Herrmann static int shmem_wait_for_pins(struct address_space *mapping)
198340e041a2SDavid Herrmann {
198405f65b5cSDavid Herrmann 	struct radix_tree_iter iter;
198505f65b5cSDavid Herrmann 	void **slot;
198605f65b5cSDavid Herrmann 	pgoff_t start;
198705f65b5cSDavid Herrmann 	struct page *page;
198805f65b5cSDavid Herrmann 	int error, scan;
198905f65b5cSDavid Herrmann 
199005f65b5cSDavid Herrmann 	shmem_tag_pins(mapping);
199105f65b5cSDavid Herrmann 
199205f65b5cSDavid Herrmann 	error = 0;
199305f65b5cSDavid Herrmann 	for (scan = 0; scan <= LAST_SCAN; scan++) {
199405f65b5cSDavid Herrmann 		if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED))
199505f65b5cSDavid Herrmann 			break;
199605f65b5cSDavid Herrmann 
199705f65b5cSDavid Herrmann 		if (!scan)
199805f65b5cSDavid Herrmann 			lru_add_drain_all();
199905f65b5cSDavid Herrmann 		else if (schedule_timeout_killable((HZ << scan) / 200))
200005f65b5cSDavid Herrmann 			scan = LAST_SCAN;
200105f65b5cSDavid Herrmann 
200205f65b5cSDavid Herrmann 		start = 0;
200305f65b5cSDavid Herrmann 		rcu_read_lock();
200405f65b5cSDavid Herrmann restart:
200505f65b5cSDavid Herrmann 		radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter,
200605f65b5cSDavid Herrmann 					   start, SHMEM_TAG_PINNED) {
200705f65b5cSDavid Herrmann 
200805f65b5cSDavid Herrmann 			page = radix_tree_deref_slot(slot);
200905f65b5cSDavid Herrmann 			if (radix_tree_exception(page)) {
201005f65b5cSDavid Herrmann 				if (radix_tree_deref_retry(page))
201105f65b5cSDavid Herrmann 					goto restart;
201205f65b5cSDavid Herrmann 
201305f65b5cSDavid Herrmann 				page = NULL;
201405f65b5cSDavid Herrmann 			}
201505f65b5cSDavid Herrmann 
201605f65b5cSDavid Herrmann 			if (page &&
201705f65b5cSDavid Herrmann 			    page_count(page) - page_mapcount(page) != 1) {
201805f65b5cSDavid Herrmann 				if (scan < LAST_SCAN)
201905f65b5cSDavid Herrmann 					goto continue_resched;
202005f65b5cSDavid Herrmann 
202105f65b5cSDavid Herrmann 				/*
202205f65b5cSDavid Herrmann 				 * On the last scan, we clean up all those tags
202305f65b5cSDavid Herrmann 				 * we inserted; but make a note that we still
202405f65b5cSDavid Herrmann 				 * found pages pinned.
202505f65b5cSDavid Herrmann 				 */
202605f65b5cSDavid Herrmann 				error = -EBUSY;
202705f65b5cSDavid Herrmann 			}
202805f65b5cSDavid Herrmann 
202905f65b5cSDavid Herrmann 			spin_lock_irq(&mapping->tree_lock);
203005f65b5cSDavid Herrmann 			radix_tree_tag_clear(&mapping->page_tree,
203105f65b5cSDavid Herrmann 					     iter.index, SHMEM_TAG_PINNED);
203205f65b5cSDavid Herrmann 			spin_unlock_irq(&mapping->tree_lock);
203305f65b5cSDavid Herrmann continue_resched:
203405f65b5cSDavid Herrmann 			if (need_resched()) {
203505f65b5cSDavid Herrmann 				cond_resched_rcu();
203605f65b5cSDavid Herrmann 				start = iter.index + 1;
203705f65b5cSDavid Herrmann 				goto restart;
203805f65b5cSDavid Herrmann 			}
203905f65b5cSDavid Herrmann 		}
204005f65b5cSDavid Herrmann 		rcu_read_unlock();
204105f65b5cSDavid Herrmann 	}
204205f65b5cSDavid Herrmann 
204305f65b5cSDavid Herrmann 	return error;
204440e041a2SDavid Herrmann }
204540e041a2SDavid Herrmann 
204640e041a2SDavid Herrmann #define F_ALL_SEALS (F_SEAL_SEAL | \
204740e041a2SDavid Herrmann 		     F_SEAL_SHRINK | \
204840e041a2SDavid Herrmann 		     F_SEAL_GROW | \
204940e041a2SDavid Herrmann 		     F_SEAL_WRITE)
205040e041a2SDavid Herrmann 
205140e041a2SDavid Herrmann int shmem_add_seals(struct file *file, unsigned int seals)
205240e041a2SDavid Herrmann {
205340e041a2SDavid Herrmann 	struct inode *inode = file_inode(file);
205440e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
205540e041a2SDavid Herrmann 	int error;
205640e041a2SDavid Herrmann 
205740e041a2SDavid Herrmann 	/*
205840e041a2SDavid Herrmann 	 * SEALING
205940e041a2SDavid Herrmann 	 * Sealing allows multiple parties to share a shmem-file but restrict
206040e041a2SDavid Herrmann 	 * access to a specific subset of file operations. Seals can only be
206140e041a2SDavid Herrmann 	 * added, but never removed. This way, mutually untrusted parties can
206240e041a2SDavid Herrmann 	 * share common memory regions with a well-defined policy. A malicious
206340e041a2SDavid Herrmann 	 * peer can thus never perform unwanted operations on a shared object.
206440e041a2SDavid Herrmann 	 *
206540e041a2SDavid Herrmann 	 * Seals are only supported on special shmem-files and always affect
206640e041a2SDavid Herrmann 	 * the whole underlying inode. Once a seal is set, it may prevent some
206740e041a2SDavid Herrmann 	 * kinds of access to the file. Currently, the following seals are
206840e041a2SDavid Herrmann 	 * defined:
206940e041a2SDavid Herrmann 	 *   SEAL_SEAL: Prevent further seals from being set on this file
207040e041a2SDavid Herrmann 	 *   SEAL_SHRINK: Prevent the file from shrinking
207140e041a2SDavid Herrmann 	 *   SEAL_GROW: Prevent the file from growing
207240e041a2SDavid Herrmann 	 *   SEAL_WRITE: Prevent write access to the file
207340e041a2SDavid Herrmann 	 *
207440e041a2SDavid Herrmann 	 * As we don't require any trust relationship between two parties, we
207540e041a2SDavid Herrmann 	 * must prevent seals from being removed. Therefore, sealing a file
207640e041a2SDavid Herrmann 	 * only adds a given set of seals to the file, it never touches
207740e041a2SDavid Herrmann 	 * existing seals. Furthermore, the "setting seals"-operation can be
207840e041a2SDavid Herrmann 	 * sealed itself, which basically prevents any further seal from being
207940e041a2SDavid Herrmann 	 * added.
208040e041a2SDavid Herrmann 	 *
208140e041a2SDavid Herrmann 	 * Semantics of sealing are only defined on volatile files. Only
208240e041a2SDavid Herrmann 	 * anonymous shmem files support sealing. More importantly, seals are
208340e041a2SDavid Herrmann 	 * never written to disk. Therefore, there's no plan to support it on
208440e041a2SDavid Herrmann 	 * other file types.
208540e041a2SDavid Herrmann 	 */
208640e041a2SDavid Herrmann 
208740e041a2SDavid Herrmann 	if (file->f_op != &shmem_file_operations)
208840e041a2SDavid Herrmann 		return -EINVAL;
208940e041a2SDavid Herrmann 	if (!(file->f_mode & FMODE_WRITE))
209040e041a2SDavid Herrmann 		return -EPERM;
209140e041a2SDavid Herrmann 	if (seals & ~(unsigned int)F_ALL_SEALS)
209240e041a2SDavid Herrmann 		return -EINVAL;
209340e041a2SDavid Herrmann 
2094*5955102cSAl Viro 	inode_lock(inode);
209540e041a2SDavid Herrmann 
209640e041a2SDavid Herrmann 	if (info->seals & F_SEAL_SEAL) {
209740e041a2SDavid Herrmann 		error = -EPERM;
209840e041a2SDavid Herrmann 		goto unlock;
209940e041a2SDavid Herrmann 	}
210040e041a2SDavid Herrmann 
210140e041a2SDavid Herrmann 	if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
210240e041a2SDavid Herrmann 		error = mapping_deny_writable(file->f_mapping);
210340e041a2SDavid Herrmann 		if (error)
210440e041a2SDavid Herrmann 			goto unlock;
210540e041a2SDavid Herrmann 
210640e041a2SDavid Herrmann 		error = shmem_wait_for_pins(file->f_mapping);
210740e041a2SDavid Herrmann 		if (error) {
210840e041a2SDavid Herrmann 			mapping_allow_writable(file->f_mapping);
210940e041a2SDavid Herrmann 			goto unlock;
211040e041a2SDavid Herrmann 		}
211140e041a2SDavid Herrmann 	}
211240e041a2SDavid Herrmann 
211340e041a2SDavid Herrmann 	info->seals |= seals;
211440e041a2SDavid Herrmann 	error = 0;
211540e041a2SDavid Herrmann 
211640e041a2SDavid Herrmann unlock:
2117*5955102cSAl Viro 	inode_unlock(inode);
211840e041a2SDavid Herrmann 	return error;
211940e041a2SDavid Herrmann }
212040e041a2SDavid Herrmann EXPORT_SYMBOL_GPL(shmem_add_seals);
212140e041a2SDavid Herrmann 
212240e041a2SDavid Herrmann int shmem_get_seals(struct file *file)
212340e041a2SDavid Herrmann {
212440e041a2SDavid Herrmann 	if (file->f_op != &shmem_file_operations)
212540e041a2SDavid Herrmann 		return -EINVAL;
212640e041a2SDavid Herrmann 
212740e041a2SDavid Herrmann 	return SHMEM_I(file_inode(file))->seals;
212840e041a2SDavid Herrmann }
212940e041a2SDavid Herrmann EXPORT_SYMBOL_GPL(shmem_get_seals);
213040e041a2SDavid Herrmann 
213140e041a2SDavid Herrmann long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
213240e041a2SDavid Herrmann {
213340e041a2SDavid Herrmann 	long error;
213440e041a2SDavid Herrmann 
213540e041a2SDavid Herrmann 	switch (cmd) {
213640e041a2SDavid Herrmann 	case F_ADD_SEALS:
213740e041a2SDavid Herrmann 		/* disallow upper 32bit */
213840e041a2SDavid Herrmann 		if (arg > UINT_MAX)
213940e041a2SDavid Herrmann 			return -EINVAL;
214040e041a2SDavid Herrmann 
214140e041a2SDavid Herrmann 		error = shmem_add_seals(file, arg);
214240e041a2SDavid Herrmann 		break;
214340e041a2SDavid Herrmann 	case F_GET_SEALS:
214440e041a2SDavid Herrmann 		error = shmem_get_seals(file);
214540e041a2SDavid Herrmann 		break;
214640e041a2SDavid Herrmann 	default:
214740e041a2SDavid Herrmann 		error = -EINVAL;
214840e041a2SDavid Herrmann 		break;
214940e041a2SDavid Herrmann 	}
215040e041a2SDavid Herrmann 
215140e041a2SDavid Herrmann 	return error;
215240e041a2SDavid Herrmann }
215340e041a2SDavid Herrmann 
215483e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset,
215583e4fa9cSHugh Dickins 							 loff_t len)
215683e4fa9cSHugh Dickins {
2157496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2158e2d12e22SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
215940e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
21601aac1400SHugh Dickins 	struct shmem_falloc shmem_falloc;
2161e2d12e22SHugh Dickins 	pgoff_t start, index, end;
2162e2d12e22SHugh Dickins 	int error;
216383e4fa9cSHugh Dickins 
216413ace4d0SHugh Dickins 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
216513ace4d0SHugh Dickins 		return -EOPNOTSUPP;
216613ace4d0SHugh Dickins 
2167*5955102cSAl Viro 	inode_lock(inode);
216883e4fa9cSHugh Dickins 
216983e4fa9cSHugh Dickins 	if (mode & FALLOC_FL_PUNCH_HOLE) {
217083e4fa9cSHugh Dickins 		struct address_space *mapping = file->f_mapping;
217183e4fa9cSHugh Dickins 		loff_t unmap_start = round_up(offset, PAGE_SIZE);
217283e4fa9cSHugh Dickins 		loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
21738e205f77SHugh Dickins 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
217483e4fa9cSHugh Dickins 
217540e041a2SDavid Herrmann 		/* protected by i_mutex */
217640e041a2SDavid Herrmann 		if (info->seals & F_SEAL_WRITE) {
217740e041a2SDavid Herrmann 			error = -EPERM;
217840e041a2SDavid Herrmann 			goto out;
217940e041a2SDavid Herrmann 		}
218040e041a2SDavid Herrmann 
21818e205f77SHugh Dickins 		shmem_falloc.waitq = &shmem_falloc_waitq;
2182f00cdc6dSHugh Dickins 		shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2183f00cdc6dSHugh Dickins 		shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2184f00cdc6dSHugh Dickins 		spin_lock(&inode->i_lock);
2185f00cdc6dSHugh Dickins 		inode->i_private = &shmem_falloc;
2186f00cdc6dSHugh Dickins 		spin_unlock(&inode->i_lock);
2187f00cdc6dSHugh Dickins 
218883e4fa9cSHugh Dickins 		if ((u64)unmap_end > (u64)unmap_start)
218983e4fa9cSHugh Dickins 			unmap_mapping_range(mapping, unmap_start,
219083e4fa9cSHugh Dickins 					    1 + unmap_end - unmap_start, 0);
219183e4fa9cSHugh Dickins 		shmem_truncate_range(inode, offset, offset + len - 1);
219283e4fa9cSHugh Dickins 		/* No need to unmap again: hole-punching leaves COWed pages */
21938e205f77SHugh Dickins 
21948e205f77SHugh Dickins 		spin_lock(&inode->i_lock);
21958e205f77SHugh Dickins 		inode->i_private = NULL;
21968e205f77SHugh Dickins 		wake_up_all(&shmem_falloc_waitq);
21978e205f77SHugh Dickins 		spin_unlock(&inode->i_lock);
219883e4fa9cSHugh Dickins 		error = 0;
21998e205f77SHugh Dickins 		goto out;
220083e4fa9cSHugh Dickins 	}
220183e4fa9cSHugh Dickins 
2202e2d12e22SHugh Dickins 	/* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2203e2d12e22SHugh Dickins 	error = inode_newsize_ok(inode, offset + len);
2204e2d12e22SHugh Dickins 	if (error)
2205e2d12e22SHugh Dickins 		goto out;
2206e2d12e22SHugh Dickins 
220740e041a2SDavid Herrmann 	if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
220840e041a2SDavid Herrmann 		error = -EPERM;
220940e041a2SDavid Herrmann 		goto out;
221040e041a2SDavid Herrmann 	}
221140e041a2SDavid Herrmann 
2212e2d12e22SHugh Dickins 	start = offset >> PAGE_CACHE_SHIFT;
2213e2d12e22SHugh Dickins 	end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2214e2d12e22SHugh Dickins 	/* Try to avoid a swapstorm if len is impossible to satisfy */
2215e2d12e22SHugh Dickins 	if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2216e2d12e22SHugh Dickins 		error = -ENOSPC;
2217e2d12e22SHugh Dickins 		goto out;
2218e2d12e22SHugh Dickins 	}
2219e2d12e22SHugh Dickins 
22208e205f77SHugh Dickins 	shmem_falloc.waitq = NULL;
22211aac1400SHugh Dickins 	shmem_falloc.start = start;
22221aac1400SHugh Dickins 	shmem_falloc.next  = start;
22231aac1400SHugh Dickins 	shmem_falloc.nr_falloced = 0;
22241aac1400SHugh Dickins 	shmem_falloc.nr_unswapped = 0;
22251aac1400SHugh Dickins 	spin_lock(&inode->i_lock);
22261aac1400SHugh Dickins 	inode->i_private = &shmem_falloc;
22271aac1400SHugh Dickins 	spin_unlock(&inode->i_lock);
22281aac1400SHugh Dickins 
2229e2d12e22SHugh Dickins 	for (index = start; index < end; index++) {
2230e2d12e22SHugh Dickins 		struct page *page;
2231e2d12e22SHugh Dickins 
2232e2d12e22SHugh Dickins 		/*
2233e2d12e22SHugh Dickins 		 * Good, the fallocate(2) manpage permits EINTR: we may have
2234e2d12e22SHugh Dickins 		 * been interrupted because we are using up too much memory.
2235e2d12e22SHugh Dickins 		 */
2236e2d12e22SHugh Dickins 		if (signal_pending(current))
2237e2d12e22SHugh Dickins 			error = -EINTR;
22381aac1400SHugh Dickins 		else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
22391aac1400SHugh Dickins 			error = -ENOMEM;
2240e2d12e22SHugh Dickins 		else
22411635f6a7SHugh Dickins 			error = shmem_getpage(inode, index, &page, SGP_FALLOC,
2242e2d12e22SHugh Dickins 									NULL);
2243e2d12e22SHugh Dickins 		if (error) {
22441635f6a7SHugh Dickins 			/* Remove the !PageUptodate pages we added */
22451635f6a7SHugh Dickins 			shmem_undo_range(inode,
22461635f6a7SHugh Dickins 				(loff_t)start << PAGE_CACHE_SHIFT,
22471635f6a7SHugh Dickins 				(loff_t)index << PAGE_CACHE_SHIFT, true);
22481aac1400SHugh Dickins 			goto undone;
2249e2d12e22SHugh Dickins 		}
2250e2d12e22SHugh Dickins 
2251e2d12e22SHugh Dickins 		/*
22521aac1400SHugh Dickins 		 * Inform shmem_writepage() how far we have reached.
22531aac1400SHugh Dickins 		 * No need for lock or barrier: we have the page lock.
22541aac1400SHugh Dickins 		 */
22551aac1400SHugh Dickins 		shmem_falloc.next++;
22561aac1400SHugh Dickins 		if (!PageUptodate(page))
22571aac1400SHugh Dickins 			shmem_falloc.nr_falloced++;
22581aac1400SHugh Dickins 
22591aac1400SHugh Dickins 		/*
22601635f6a7SHugh Dickins 		 * If !PageUptodate, leave it that way so that freeable pages
22611635f6a7SHugh Dickins 		 * can be recognized if we need to rollback on error later.
22621635f6a7SHugh Dickins 		 * But set_page_dirty so that memory pressure will swap rather
2263e2d12e22SHugh Dickins 		 * than free the pages we are allocating (and SGP_CACHE pages
2264e2d12e22SHugh Dickins 		 * might still be clean: we now need to mark those dirty too).
2265e2d12e22SHugh Dickins 		 */
2266e2d12e22SHugh Dickins 		set_page_dirty(page);
2267e2d12e22SHugh Dickins 		unlock_page(page);
2268e2d12e22SHugh Dickins 		page_cache_release(page);
2269e2d12e22SHugh Dickins 		cond_resched();
2270e2d12e22SHugh Dickins 	}
2271e2d12e22SHugh Dickins 
2272e2d12e22SHugh Dickins 	if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2273e2d12e22SHugh Dickins 		i_size_write(inode, offset + len);
2274e2d12e22SHugh Dickins 	inode->i_ctime = CURRENT_TIME;
22751aac1400SHugh Dickins undone:
22761aac1400SHugh Dickins 	spin_lock(&inode->i_lock);
22771aac1400SHugh Dickins 	inode->i_private = NULL;
22781aac1400SHugh Dickins 	spin_unlock(&inode->i_lock);
2279e2d12e22SHugh Dickins out:
2280*5955102cSAl Viro 	inode_unlock(inode);
228183e4fa9cSHugh Dickins 	return error;
228283e4fa9cSHugh Dickins }
228383e4fa9cSHugh Dickins 
2284726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
22851da177e4SLinus Torvalds {
2286726c3342SDavid Howells 	struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 	buf->f_type = TMPFS_MAGIC;
22891da177e4SLinus Torvalds 	buf->f_bsize = PAGE_CACHE_SIZE;
22901da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
22910edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
22921da177e4SLinus Torvalds 		buf->f_blocks = sbinfo->max_blocks;
229341ffe5d5SHugh Dickins 		buf->f_bavail =
229441ffe5d5SHugh Dickins 		buf->f_bfree  = sbinfo->max_blocks -
229541ffe5d5SHugh Dickins 				percpu_counter_sum(&sbinfo->used_blocks);
22960edd73b3SHugh Dickins 	}
22970edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
22981da177e4SLinus Torvalds 		buf->f_files = sbinfo->max_inodes;
22991da177e4SLinus Torvalds 		buf->f_ffree = sbinfo->free_inodes;
23001da177e4SLinus Torvalds 	}
23011da177e4SLinus Torvalds 	/* else leave those fields 0 like simple_statfs */
23021da177e4SLinus Torvalds 	return 0;
23031da177e4SLinus Torvalds }
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds /*
23061da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
23071da177e4SLinus Torvalds  */
23081da177e4SLinus Torvalds static int
23091a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
23101da177e4SLinus Torvalds {
23110b0a0806SHugh Dickins 	struct inode *inode;
23121da177e4SLinus Torvalds 	int error = -ENOSPC;
23131da177e4SLinus Torvalds 
2314454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
23151da177e4SLinus Torvalds 	if (inode) {
2316feda821eSChristoph Hellwig 		error = simple_acl_create(dir, inode);
2317feda821eSChristoph Hellwig 		if (error)
2318feda821eSChristoph Hellwig 			goto out_iput;
23192a7dba39SEric Paris 		error = security_inode_init_security(inode, dir,
23209d8f13baSMimi Zohar 						     &dentry->d_name,
23216d9d88d0SJarkko Sakkinen 						     shmem_initxattrs, NULL);
2322feda821eSChristoph Hellwig 		if (error && error != -EOPNOTSUPP)
2323feda821eSChristoph Hellwig 			goto out_iput;
232437ec43cdSMimi Zohar 
2325718deb6bSAl Viro 		error = 0;
23261da177e4SLinus Torvalds 		dir->i_size += BOGO_DIRENT_SIZE;
23271da177e4SLinus Torvalds 		dir->i_ctime = dir->i_mtime = CURRENT_TIME;
23281da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
23291da177e4SLinus Torvalds 		dget(dentry); /* Extra count - pin the dentry in core */
23301da177e4SLinus Torvalds 	}
23311da177e4SLinus Torvalds 	return error;
2332feda821eSChristoph Hellwig out_iput:
2333feda821eSChristoph Hellwig 	iput(inode);
2334feda821eSChristoph Hellwig 	return error;
23351da177e4SLinus Torvalds }
23361da177e4SLinus Torvalds 
233760545d0dSAl Viro static int
233860545d0dSAl Viro shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
233960545d0dSAl Viro {
234060545d0dSAl Viro 	struct inode *inode;
234160545d0dSAl Viro 	int error = -ENOSPC;
234260545d0dSAl Viro 
234360545d0dSAl Viro 	inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
234460545d0dSAl Viro 	if (inode) {
234560545d0dSAl Viro 		error = security_inode_init_security(inode, dir,
234660545d0dSAl Viro 						     NULL,
234760545d0dSAl Viro 						     shmem_initxattrs, NULL);
2348feda821eSChristoph Hellwig 		if (error && error != -EOPNOTSUPP)
2349feda821eSChristoph Hellwig 			goto out_iput;
2350feda821eSChristoph Hellwig 		error = simple_acl_create(dir, inode);
2351feda821eSChristoph Hellwig 		if (error)
2352feda821eSChristoph Hellwig 			goto out_iput;
235360545d0dSAl Viro 		d_tmpfile(dentry, inode);
235460545d0dSAl Viro 	}
235560545d0dSAl Viro 	return error;
2356feda821eSChristoph Hellwig out_iput:
2357feda821eSChristoph Hellwig 	iput(inode);
2358feda821eSChristoph Hellwig 	return error;
235960545d0dSAl Viro }
236060545d0dSAl Viro 
236118bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
23621da177e4SLinus Torvalds {
23631da177e4SLinus Torvalds 	int error;
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds 	if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
23661da177e4SLinus Torvalds 		return error;
2367d8c76e6fSDave Hansen 	inc_nlink(dir);
23681da177e4SLinus Torvalds 	return 0;
23691da177e4SLinus Torvalds }
23701da177e4SLinus Torvalds 
23714acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2372ebfc3b49SAl Viro 		bool excl)
23731da177e4SLinus Torvalds {
23741da177e4SLinus Torvalds 	return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
23751da177e4SLinus Torvalds }
23761da177e4SLinus Torvalds 
23771da177e4SLinus Torvalds /*
23781da177e4SLinus Torvalds  * Link a file..
23791da177e4SLinus Torvalds  */
23801da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
23811da177e4SLinus Torvalds {
238275c3cfa8SDavid Howells 	struct inode *inode = d_inode(old_dentry);
23835b04c689SPavel Emelyanov 	int ret;
23841da177e4SLinus Torvalds 
23851da177e4SLinus Torvalds 	/*
23861da177e4SLinus Torvalds 	 * No ordinary (disk based) filesystem counts links as inodes;
23871da177e4SLinus Torvalds 	 * but each new link needs a new dentry, pinning lowmem, and
23881da177e4SLinus Torvalds 	 * tmpfs dentries cannot be pruned until they are unlinked.
23891da177e4SLinus Torvalds 	 */
23905b04c689SPavel Emelyanov 	ret = shmem_reserve_inode(inode->i_sb);
23915b04c689SPavel Emelyanov 	if (ret)
23925b04c689SPavel Emelyanov 		goto out;
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
23951da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2396d8c76e6fSDave Hansen 	inc_nlink(inode);
23977de9c6eeSAl Viro 	ihold(inode);	/* New dentry reference */
23981da177e4SLinus Torvalds 	dget(dentry);		/* Extra pinning count for the created dentry */
23991da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
24005b04c689SPavel Emelyanov out:
24015b04c689SPavel Emelyanov 	return ret;
24021da177e4SLinus Torvalds }
24031da177e4SLinus Torvalds 
24041da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry)
24051da177e4SLinus Torvalds {
240675c3cfa8SDavid Howells 	struct inode *inode = d_inode(dentry);
24071da177e4SLinus Torvalds 
24085b04c689SPavel Emelyanov 	if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
24095b04c689SPavel Emelyanov 		shmem_free_inode(inode->i_sb);
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds 	dir->i_size -= BOGO_DIRENT_SIZE;
24121da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
24139a53c3a7SDave Hansen 	drop_nlink(inode);
24141da177e4SLinus Torvalds 	dput(dentry);	/* Undo the count from "create" - this does all the work */
24151da177e4SLinus Torvalds 	return 0;
24161da177e4SLinus Torvalds }
24171da177e4SLinus Torvalds 
24181da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
24191da177e4SLinus Torvalds {
24201da177e4SLinus Torvalds 	if (!simple_empty(dentry))
24211da177e4SLinus Torvalds 		return -ENOTEMPTY;
24221da177e4SLinus Torvalds 
242375c3cfa8SDavid Howells 	drop_nlink(d_inode(dentry));
24249a53c3a7SDave Hansen 	drop_nlink(dir);
24251da177e4SLinus Torvalds 	return shmem_unlink(dir, dentry);
24261da177e4SLinus Torvalds }
24271da177e4SLinus Torvalds 
242837456771SMiklos Szeredi static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
242937456771SMiklos Szeredi {
2430e36cb0b8SDavid Howells 	bool old_is_dir = d_is_dir(old_dentry);
2431e36cb0b8SDavid Howells 	bool new_is_dir = d_is_dir(new_dentry);
243237456771SMiklos Szeredi 
243337456771SMiklos Szeredi 	if (old_dir != new_dir && old_is_dir != new_is_dir) {
243437456771SMiklos Szeredi 		if (old_is_dir) {
243537456771SMiklos Szeredi 			drop_nlink(old_dir);
243637456771SMiklos Szeredi 			inc_nlink(new_dir);
243737456771SMiklos Szeredi 		} else {
243837456771SMiklos Szeredi 			drop_nlink(new_dir);
243937456771SMiklos Szeredi 			inc_nlink(old_dir);
244037456771SMiklos Szeredi 		}
244137456771SMiklos Szeredi 	}
244237456771SMiklos Szeredi 	old_dir->i_ctime = old_dir->i_mtime =
244337456771SMiklos Szeredi 	new_dir->i_ctime = new_dir->i_mtime =
244475c3cfa8SDavid Howells 	d_inode(old_dentry)->i_ctime =
244575c3cfa8SDavid Howells 	d_inode(new_dentry)->i_ctime = CURRENT_TIME;
244637456771SMiklos Szeredi 
244737456771SMiklos Szeredi 	return 0;
244837456771SMiklos Szeredi }
244937456771SMiklos Szeredi 
245046fdb794SMiklos Szeredi static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
245146fdb794SMiklos Szeredi {
245246fdb794SMiklos Szeredi 	struct dentry *whiteout;
245346fdb794SMiklos Szeredi 	int error;
245446fdb794SMiklos Szeredi 
245546fdb794SMiklos Szeredi 	whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
245646fdb794SMiklos Szeredi 	if (!whiteout)
245746fdb794SMiklos Szeredi 		return -ENOMEM;
245846fdb794SMiklos Szeredi 
245946fdb794SMiklos Szeredi 	error = shmem_mknod(old_dir, whiteout,
246046fdb794SMiklos Szeredi 			    S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
246146fdb794SMiklos Szeredi 	dput(whiteout);
246246fdb794SMiklos Szeredi 	if (error)
246346fdb794SMiklos Szeredi 		return error;
246446fdb794SMiklos Szeredi 
246546fdb794SMiklos Szeredi 	/*
246646fdb794SMiklos Szeredi 	 * Cheat and hash the whiteout while the old dentry is still in
246746fdb794SMiklos Szeredi 	 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
246846fdb794SMiklos Szeredi 	 *
246946fdb794SMiklos Szeredi 	 * d_lookup() will consistently find one of them at this point,
247046fdb794SMiklos Szeredi 	 * not sure which one, but that isn't even important.
247146fdb794SMiklos Szeredi 	 */
247246fdb794SMiklos Szeredi 	d_rehash(whiteout);
247346fdb794SMiklos Szeredi 	return 0;
247446fdb794SMiklos Szeredi }
247546fdb794SMiklos Szeredi 
24761da177e4SLinus Torvalds /*
24771da177e4SLinus Torvalds  * The VFS layer already does all the dentry stuff for rename,
24781da177e4SLinus Torvalds  * we just have to decrement the usage count for the target if
24791da177e4SLinus Torvalds  * it exists so that the VFS layer correctly free's it when it
24801da177e4SLinus Torvalds  * gets overwritten.
24811da177e4SLinus Torvalds  */
24823b69ff51SMiklos Szeredi static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags)
24831da177e4SLinus Torvalds {
248475c3cfa8SDavid Howells 	struct inode *inode = d_inode(old_dentry);
24851da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(inode->i_mode);
24861da177e4SLinus Torvalds 
248746fdb794SMiklos Szeredi 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
24883b69ff51SMiklos Szeredi 		return -EINVAL;
24893b69ff51SMiklos Szeredi 
249037456771SMiklos Szeredi 	if (flags & RENAME_EXCHANGE)
249137456771SMiklos Szeredi 		return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
249237456771SMiklos Szeredi 
24931da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
24941da177e4SLinus Torvalds 		return -ENOTEMPTY;
24951da177e4SLinus Torvalds 
249646fdb794SMiklos Szeredi 	if (flags & RENAME_WHITEOUT) {
249746fdb794SMiklos Szeredi 		int error;
249846fdb794SMiklos Szeredi 
249946fdb794SMiklos Szeredi 		error = shmem_whiteout(old_dir, old_dentry);
250046fdb794SMiklos Szeredi 		if (error)
250146fdb794SMiklos Szeredi 			return error;
250246fdb794SMiklos Szeredi 	}
250346fdb794SMiklos Szeredi 
250475c3cfa8SDavid Howells 	if (d_really_is_positive(new_dentry)) {
25051da177e4SLinus Torvalds 		(void) shmem_unlink(new_dir, new_dentry);
2506b928095bSMiklos Szeredi 		if (they_are_dirs) {
250775c3cfa8SDavid Howells 			drop_nlink(d_inode(new_dentry));
25089a53c3a7SDave Hansen 			drop_nlink(old_dir);
2509b928095bSMiklos Szeredi 		}
25101da177e4SLinus Torvalds 	} else if (they_are_dirs) {
25119a53c3a7SDave Hansen 		drop_nlink(old_dir);
2512d8c76e6fSDave Hansen 		inc_nlink(new_dir);
25131da177e4SLinus Torvalds 	}
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds 	old_dir->i_size -= BOGO_DIRENT_SIZE;
25161da177e4SLinus Torvalds 	new_dir->i_size += BOGO_DIRENT_SIZE;
25171da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime =
25181da177e4SLinus Torvalds 	new_dir->i_ctime = new_dir->i_mtime =
25191da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME;
25201da177e4SLinus Torvalds 	return 0;
25211da177e4SLinus Torvalds }
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
25241da177e4SLinus Torvalds {
25251da177e4SLinus Torvalds 	int error;
25261da177e4SLinus Torvalds 	int len;
25271da177e4SLinus Torvalds 	struct inode *inode;
25289276aad6SHugh Dickins 	struct page *page;
25291da177e4SLinus Torvalds 	struct shmem_inode_info *info;
25301da177e4SLinus Torvalds 
25311da177e4SLinus Torvalds 	len = strlen(symname) + 1;
25321da177e4SLinus Torvalds 	if (len > PAGE_CACHE_SIZE)
25331da177e4SLinus Torvalds 		return -ENAMETOOLONG;
25341da177e4SLinus Torvalds 
2535454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
25361da177e4SLinus Torvalds 	if (!inode)
25371da177e4SLinus Torvalds 		return -ENOSPC;
25381da177e4SLinus Torvalds 
25399d8f13baSMimi Zohar 	error = security_inode_init_security(inode, dir, &dentry->d_name,
25406d9d88d0SJarkko Sakkinen 					     shmem_initxattrs, NULL);
2541570bc1c2SStephen Smalley 	if (error) {
2542570bc1c2SStephen Smalley 		if (error != -EOPNOTSUPP) {
2543570bc1c2SStephen Smalley 			iput(inode);
2544570bc1c2SStephen Smalley 			return error;
2545570bc1c2SStephen Smalley 		}
2546570bc1c2SStephen Smalley 		error = 0;
2547570bc1c2SStephen Smalley 	}
2548570bc1c2SStephen Smalley 
25491da177e4SLinus Torvalds 	info = SHMEM_I(inode);
25501da177e4SLinus Torvalds 	inode->i_size = len-1;
255169f07ec9SHugh Dickins 	if (len <= SHORT_SYMLINK_LEN) {
255269f07ec9SHugh Dickins 		info->symlink = kmemdup(symname, len, GFP_KERNEL);
255369f07ec9SHugh Dickins 		if (!info->symlink) {
255469f07ec9SHugh Dickins 			iput(inode);
255569f07ec9SHugh Dickins 			return -ENOMEM;
255669f07ec9SHugh Dickins 		}
255769f07ec9SHugh Dickins 		inode->i_op = &shmem_short_symlink_operations;
255860380f19SAl Viro 		inode->i_link = info->symlink;
25591da177e4SLinus Torvalds 	} else {
2560e8ecde25SAl Viro 		inode_nohighmem(inode);
25611da177e4SLinus Torvalds 		error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
25621da177e4SLinus Torvalds 		if (error) {
25631da177e4SLinus Torvalds 			iput(inode);
25641da177e4SLinus Torvalds 			return error;
25651da177e4SLinus Torvalds 		}
256614fcc23fSHugh Dickins 		inode->i_mapping->a_ops = &shmem_aops;
25671da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inode_operations;
256821fc61c7SAl Viro 		memcpy(page_address(page), symname, len);
2569ec9516fbSHugh Dickins 		SetPageUptodate(page);
25701da177e4SLinus Torvalds 		set_page_dirty(page);
25716746aff7SWu Fengguang 		unlock_page(page);
25721da177e4SLinus Torvalds 		page_cache_release(page);
25731da177e4SLinus Torvalds 	}
25741da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
25751da177e4SLinus Torvalds 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
25761da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
25771da177e4SLinus Torvalds 	dget(dentry);
25781da177e4SLinus Torvalds 	return 0;
25791da177e4SLinus Torvalds }
25801da177e4SLinus Torvalds 
2581fceef393SAl Viro static void shmem_put_link(void *arg)
2582fceef393SAl Viro {
2583fceef393SAl Viro 	mark_page_accessed(arg);
2584fceef393SAl Viro 	put_page(arg);
2585fceef393SAl Viro }
2586fceef393SAl Viro 
25876b255391SAl Viro static const char *shmem_get_link(struct dentry *dentry,
2588fceef393SAl Viro 				  struct inode *inode,
2589fceef393SAl Viro 				  struct delayed_call *done)
25901da177e4SLinus Torvalds {
25911da177e4SLinus Torvalds 	struct page *page = NULL;
25926b255391SAl Viro 	int error;
25936a6c9904SAl Viro 	if (!dentry) {
25946a6c9904SAl Viro 		page = find_get_page(inode->i_mapping, 0);
25956a6c9904SAl Viro 		if (!page)
25966b255391SAl Viro 			return ERR_PTR(-ECHILD);
25976a6c9904SAl Viro 		if (!PageUptodate(page)) {
25986a6c9904SAl Viro 			put_page(page);
25996a6c9904SAl Viro 			return ERR_PTR(-ECHILD);
26006a6c9904SAl Viro 		}
26016a6c9904SAl Viro 	} else {
26026b255391SAl Viro 		error = shmem_getpage(inode, 0, &page, SGP_READ, NULL);
2603680baacbSAl Viro 		if (error)
2604680baacbSAl Viro 			return ERR_PTR(error);
2605d3602444SHugh Dickins 		unlock_page(page);
26061da177e4SLinus Torvalds 	}
2607fceef393SAl Viro 	set_delayed_call(done, shmem_put_link, page);
260821fc61c7SAl Viro 	return page_address(page);
26091da177e4SLinus Torvalds }
26101da177e4SLinus Torvalds 
2611b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2612b09e0fa4SEric Paris /*
2613b09e0fa4SEric Paris  * Superblocks without xattr inode operations may get some security.* xattr
2614b09e0fa4SEric Paris  * support from the LSM "for free". As soon as we have any other xattrs
2615b09e0fa4SEric Paris  * like ACLs, we also need to implement the security.* handlers at
2616b09e0fa4SEric Paris  * filesystem level, though.
2617b09e0fa4SEric Paris  */
2618b09e0fa4SEric Paris 
26196d9d88d0SJarkko Sakkinen /*
26206d9d88d0SJarkko Sakkinen  * Callback for security_inode_init_security() for acquiring xattrs.
26216d9d88d0SJarkko Sakkinen  */
26226d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode,
26236d9d88d0SJarkko Sakkinen 			    const struct xattr *xattr_array,
26246d9d88d0SJarkko Sakkinen 			    void *fs_info)
26256d9d88d0SJarkko Sakkinen {
26266d9d88d0SJarkko Sakkinen 	struct shmem_inode_info *info = SHMEM_I(inode);
26276d9d88d0SJarkko Sakkinen 	const struct xattr *xattr;
262838f38657SAristeu Rozanski 	struct simple_xattr *new_xattr;
26296d9d88d0SJarkko Sakkinen 	size_t len;
26306d9d88d0SJarkko Sakkinen 
26316d9d88d0SJarkko Sakkinen 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
263238f38657SAristeu Rozanski 		new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
26336d9d88d0SJarkko Sakkinen 		if (!new_xattr)
26346d9d88d0SJarkko Sakkinen 			return -ENOMEM;
26356d9d88d0SJarkko Sakkinen 
26366d9d88d0SJarkko Sakkinen 		len = strlen(xattr->name) + 1;
26376d9d88d0SJarkko Sakkinen 		new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
26386d9d88d0SJarkko Sakkinen 					  GFP_KERNEL);
26396d9d88d0SJarkko Sakkinen 		if (!new_xattr->name) {
26406d9d88d0SJarkko Sakkinen 			kfree(new_xattr);
26416d9d88d0SJarkko Sakkinen 			return -ENOMEM;
26426d9d88d0SJarkko Sakkinen 		}
26436d9d88d0SJarkko Sakkinen 
26446d9d88d0SJarkko Sakkinen 		memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
26456d9d88d0SJarkko Sakkinen 		       XATTR_SECURITY_PREFIX_LEN);
26466d9d88d0SJarkko Sakkinen 		memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
26476d9d88d0SJarkko Sakkinen 		       xattr->name, len);
26486d9d88d0SJarkko Sakkinen 
264938f38657SAristeu Rozanski 		simple_xattr_list_add(&info->xattrs, new_xattr);
26506d9d88d0SJarkko Sakkinen 	}
26516d9d88d0SJarkko Sakkinen 
26526d9d88d0SJarkko Sakkinen 	return 0;
26536d9d88d0SJarkko Sakkinen }
26546d9d88d0SJarkko Sakkinen 
2655aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_get(const struct xattr_handler *handler,
2656aa7c5241SAndreas Gruenbacher 				   struct dentry *dentry, const char *name,
2657aa7c5241SAndreas Gruenbacher 				   void *buffer, size_t size)
2658aa7c5241SAndreas Gruenbacher {
2659aa7c5241SAndreas Gruenbacher 	struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
2660aa7c5241SAndreas Gruenbacher 
2661aa7c5241SAndreas Gruenbacher 	name = xattr_full_name(handler, name);
2662aa7c5241SAndreas Gruenbacher 	return simple_xattr_get(&info->xattrs, name, buffer, size);
2663aa7c5241SAndreas Gruenbacher }
2664aa7c5241SAndreas Gruenbacher 
2665aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_set(const struct xattr_handler *handler,
2666aa7c5241SAndreas Gruenbacher 				   struct dentry *dentry, const char *name,
2667aa7c5241SAndreas Gruenbacher 				   const void *value, size_t size, int flags)
2668aa7c5241SAndreas Gruenbacher {
2669aa7c5241SAndreas Gruenbacher 	struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
2670aa7c5241SAndreas Gruenbacher 
2671aa7c5241SAndreas Gruenbacher 	name = xattr_full_name(handler, name);
2672aa7c5241SAndreas Gruenbacher 	return simple_xattr_set(&info->xattrs, name, value, size, flags);
2673aa7c5241SAndreas Gruenbacher }
2674aa7c5241SAndreas Gruenbacher 
2675aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_security_xattr_handler = {
2676aa7c5241SAndreas Gruenbacher 	.prefix = XATTR_SECURITY_PREFIX,
2677aa7c5241SAndreas Gruenbacher 	.get = shmem_xattr_handler_get,
2678aa7c5241SAndreas Gruenbacher 	.set = shmem_xattr_handler_set,
2679aa7c5241SAndreas Gruenbacher };
2680aa7c5241SAndreas Gruenbacher 
2681aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_trusted_xattr_handler = {
2682aa7c5241SAndreas Gruenbacher 	.prefix = XATTR_TRUSTED_PREFIX,
2683aa7c5241SAndreas Gruenbacher 	.get = shmem_xattr_handler_get,
2684aa7c5241SAndreas Gruenbacher 	.set = shmem_xattr_handler_set,
2685aa7c5241SAndreas Gruenbacher };
2686aa7c5241SAndreas Gruenbacher 
2687b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = {
2688b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
2689feda821eSChristoph Hellwig 	&posix_acl_access_xattr_handler,
2690feda821eSChristoph Hellwig 	&posix_acl_default_xattr_handler,
2691b09e0fa4SEric Paris #endif
2692aa7c5241SAndreas Gruenbacher 	&shmem_security_xattr_handler,
2693aa7c5241SAndreas Gruenbacher 	&shmem_trusted_xattr_handler,
2694b09e0fa4SEric Paris 	NULL
2695b09e0fa4SEric Paris };
2696b09e0fa4SEric Paris 
2697b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2698b09e0fa4SEric Paris {
269975c3cfa8SDavid Howells 	struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
2700786534b9SAndreas Gruenbacher 	return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
2701b09e0fa4SEric Paris }
2702b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */
2703b09e0fa4SEric Paris 
270469f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = {
27051da177e4SLinus Torvalds 	.readlink	= generic_readlink,
27066b255391SAl Viro 	.get_link	= simple_get_link,
2707b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2708aa7c5241SAndreas Gruenbacher 	.setxattr	= generic_setxattr,
2709aa7c5241SAndreas Gruenbacher 	.getxattr	= generic_getxattr,
2710b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2711aa7c5241SAndreas Gruenbacher 	.removexattr	= generic_removexattr,
2712b09e0fa4SEric Paris #endif
27131da177e4SLinus Torvalds };
27141da177e4SLinus Torvalds 
271592e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = {
27161da177e4SLinus Torvalds 	.readlink	= generic_readlink,
27176b255391SAl Viro 	.get_link	= shmem_get_link,
2718b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2719aa7c5241SAndreas Gruenbacher 	.setxattr	= generic_setxattr,
2720aa7c5241SAndreas Gruenbacher 	.getxattr	= generic_getxattr,
2721b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2722aa7c5241SAndreas Gruenbacher 	.removexattr	= generic_removexattr,
272339f0247dSAndreas Gruenbacher #endif
2724b09e0fa4SEric Paris };
272539f0247dSAndreas Gruenbacher 
272691828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child)
272791828a40SDavid M. Grimes {
272891828a40SDavid M. Grimes 	return ERR_PTR(-ESTALE);
272991828a40SDavid M. Grimes }
273091828a40SDavid M. Grimes 
273191828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh)
273291828a40SDavid M. Grimes {
273391828a40SDavid M. Grimes 	__u32 *fh = vfh;
273491828a40SDavid M. Grimes 	__u64 inum = fh[2];
273591828a40SDavid M. Grimes 	inum = (inum << 32) | fh[1];
273691828a40SDavid M. Grimes 	return ino->i_ino == inum && fh[0] == ino->i_generation;
273791828a40SDavid M. Grimes }
273891828a40SDavid M. Grimes 
2739480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2740480b116cSChristoph Hellwig 		struct fid *fid, int fh_len, int fh_type)
274191828a40SDavid M. Grimes {
274291828a40SDavid M. Grimes 	struct inode *inode;
2743480b116cSChristoph Hellwig 	struct dentry *dentry = NULL;
274435c2a7f4SHugh Dickins 	u64 inum;
274591828a40SDavid M. Grimes 
2746480b116cSChristoph Hellwig 	if (fh_len < 3)
2747480b116cSChristoph Hellwig 		return NULL;
2748480b116cSChristoph Hellwig 
274935c2a7f4SHugh Dickins 	inum = fid->raw[2];
275035c2a7f4SHugh Dickins 	inum = (inum << 32) | fid->raw[1];
275135c2a7f4SHugh Dickins 
2752480b116cSChristoph Hellwig 	inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2753480b116cSChristoph Hellwig 			shmem_match, fid->raw);
275491828a40SDavid M. Grimes 	if (inode) {
2755480b116cSChristoph Hellwig 		dentry = d_find_alias(inode);
275691828a40SDavid M. Grimes 		iput(inode);
275791828a40SDavid M. Grimes 	}
275891828a40SDavid M. Grimes 
2759480b116cSChristoph Hellwig 	return dentry;
276091828a40SDavid M. Grimes }
276191828a40SDavid M. Grimes 
2762b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
2763b0b0382bSAl Viro 				struct inode *parent)
276491828a40SDavid M. Grimes {
27655fe0c237SAneesh Kumar K.V 	if (*len < 3) {
27665fe0c237SAneesh Kumar K.V 		*len = 3;
276794e07a75SNamjae Jeon 		return FILEID_INVALID;
27685fe0c237SAneesh Kumar K.V 	}
276991828a40SDavid M. Grimes 
27701d3382cbSAl Viro 	if (inode_unhashed(inode)) {
277191828a40SDavid M. Grimes 		/* Unfortunately insert_inode_hash is not idempotent,
277291828a40SDavid M. Grimes 		 * so as we hash inodes here rather than at creation
277391828a40SDavid M. Grimes 		 * time, we need a lock to ensure we only try
277491828a40SDavid M. Grimes 		 * to do it once
277591828a40SDavid M. Grimes 		 */
277691828a40SDavid M. Grimes 		static DEFINE_SPINLOCK(lock);
277791828a40SDavid M. Grimes 		spin_lock(&lock);
27781d3382cbSAl Viro 		if (inode_unhashed(inode))
277991828a40SDavid M. Grimes 			__insert_inode_hash(inode,
278091828a40SDavid M. Grimes 					    inode->i_ino + inode->i_generation);
278191828a40SDavid M. Grimes 		spin_unlock(&lock);
278291828a40SDavid M. Grimes 	}
278391828a40SDavid M. Grimes 
278491828a40SDavid M. Grimes 	fh[0] = inode->i_generation;
278591828a40SDavid M. Grimes 	fh[1] = inode->i_ino;
278691828a40SDavid M. Grimes 	fh[2] = ((__u64)inode->i_ino) >> 32;
278791828a40SDavid M. Grimes 
278891828a40SDavid M. Grimes 	*len = 3;
278991828a40SDavid M. Grimes 	return 1;
279091828a40SDavid M. Grimes }
279191828a40SDavid M. Grimes 
279239655164SChristoph Hellwig static const struct export_operations shmem_export_ops = {
279391828a40SDavid M. Grimes 	.get_parent     = shmem_get_parent,
279491828a40SDavid M. Grimes 	.encode_fh      = shmem_encode_fh,
2795480b116cSChristoph Hellwig 	.fh_to_dentry	= shmem_fh_to_dentry,
279691828a40SDavid M. Grimes };
279791828a40SDavid M. Grimes 
2798680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2799680d794bSakpm@linux-foundation.org 			       bool remount)
28001da177e4SLinus Torvalds {
28011da177e4SLinus Torvalds 	char *this_char, *value, *rest;
280249cd0a5cSGreg Thelen 	struct mempolicy *mpol = NULL;
28038751e039SEric W. Biederman 	uid_t uid;
28048751e039SEric W. Biederman 	gid_t gid;
28051da177e4SLinus Torvalds 
2806b00dc3adSHugh Dickins 	while (options != NULL) {
2807b00dc3adSHugh Dickins 		this_char = options;
2808b00dc3adSHugh Dickins 		for (;;) {
2809b00dc3adSHugh Dickins 			/*
2810b00dc3adSHugh Dickins 			 * NUL-terminate this option: unfortunately,
2811b00dc3adSHugh Dickins 			 * mount options form a comma-separated list,
2812b00dc3adSHugh Dickins 			 * but mpol's nodelist may also contain commas.
2813b00dc3adSHugh Dickins 			 */
2814b00dc3adSHugh Dickins 			options = strchr(options, ',');
2815b00dc3adSHugh Dickins 			if (options == NULL)
2816b00dc3adSHugh Dickins 				break;
2817b00dc3adSHugh Dickins 			options++;
2818b00dc3adSHugh Dickins 			if (!isdigit(*options)) {
2819b00dc3adSHugh Dickins 				options[-1] = '\0';
2820b00dc3adSHugh Dickins 				break;
2821b00dc3adSHugh Dickins 			}
2822b00dc3adSHugh Dickins 		}
28231da177e4SLinus Torvalds 		if (!*this_char)
28241da177e4SLinus Torvalds 			continue;
28251da177e4SLinus Torvalds 		if ((value = strchr(this_char,'=')) != NULL) {
28261da177e4SLinus Torvalds 			*value++ = 0;
28271da177e4SLinus Torvalds 		} else {
28281da177e4SLinus Torvalds 			printk(KERN_ERR
28291da177e4SLinus Torvalds 			    "tmpfs: No value for mount option '%s'\n",
28301da177e4SLinus Torvalds 			    this_char);
283149cd0a5cSGreg Thelen 			goto error;
28321da177e4SLinus Torvalds 		}
28331da177e4SLinus Torvalds 
28341da177e4SLinus Torvalds 		if (!strcmp(this_char,"size")) {
28351da177e4SLinus Torvalds 			unsigned long long size;
28361da177e4SLinus Torvalds 			size = memparse(value,&rest);
28371da177e4SLinus Torvalds 			if (*rest == '%') {
28381da177e4SLinus Torvalds 				size <<= PAGE_SHIFT;
28391da177e4SLinus Torvalds 				size *= totalram_pages;
28401da177e4SLinus Torvalds 				do_div(size, 100);
28411da177e4SLinus Torvalds 				rest++;
28421da177e4SLinus Torvalds 			}
28431da177e4SLinus Torvalds 			if (*rest)
28441da177e4SLinus Torvalds 				goto bad_val;
2845680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks =
2846680d794bSakpm@linux-foundation.org 				DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
28471da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_blocks")) {
2848680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks = memparse(value, &rest);
28491da177e4SLinus Torvalds 			if (*rest)
28501da177e4SLinus Torvalds 				goto bad_val;
28511da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_inodes")) {
2852680d794bSakpm@linux-foundation.org 			sbinfo->max_inodes = memparse(value, &rest);
28531da177e4SLinus Torvalds 			if (*rest)
28541da177e4SLinus Torvalds 				goto bad_val;
28551da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"mode")) {
2856680d794bSakpm@linux-foundation.org 			if (remount)
28571da177e4SLinus Torvalds 				continue;
2858680d794bSakpm@linux-foundation.org 			sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
28591da177e4SLinus Torvalds 			if (*rest)
28601da177e4SLinus Torvalds 				goto bad_val;
28611da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"uid")) {
2862680d794bSakpm@linux-foundation.org 			if (remount)
28631da177e4SLinus Torvalds 				continue;
28648751e039SEric W. Biederman 			uid = simple_strtoul(value, &rest, 0);
28651da177e4SLinus Torvalds 			if (*rest)
28661da177e4SLinus Torvalds 				goto bad_val;
28678751e039SEric W. Biederman 			sbinfo->uid = make_kuid(current_user_ns(), uid);
28688751e039SEric W. Biederman 			if (!uid_valid(sbinfo->uid))
28698751e039SEric W. Biederman 				goto bad_val;
28701da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"gid")) {
2871680d794bSakpm@linux-foundation.org 			if (remount)
28721da177e4SLinus Torvalds 				continue;
28738751e039SEric W. Biederman 			gid = simple_strtoul(value, &rest, 0);
28741da177e4SLinus Torvalds 			if (*rest)
28751da177e4SLinus Torvalds 				goto bad_val;
28768751e039SEric W. Biederman 			sbinfo->gid = make_kgid(current_user_ns(), gid);
28778751e039SEric W. Biederman 			if (!gid_valid(sbinfo->gid))
28788751e039SEric W. Biederman 				goto bad_val;
28797339ff83SRobin Holt 		} else if (!strcmp(this_char,"mpol")) {
288049cd0a5cSGreg Thelen 			mpol_put(mpol);
288149cd0a5cSGreg Thelen 			mpol = NULL;
288249cd0a5cSGreg Thelen 			if (mpol_parse_str(value, &mpol))
28837339ff83SRobin Holt 				goto bad_val;
28841da177e4SLinus Torvalds 		} else {
28851da177e4SLinus Torvalds 			printk(KERN_ERR "tmpfs: Bad mount option %s\n",
28861da177e4SLinus Torvalds 			       this_char);
288749cd0a5cSGreg Thelen 			goto error;
28881da177e4SLinus Torvalds 		}
28891da177e4SLinus Torvalds 	}
289049cd0a5cSGreg Thelen 	sbinfo->mpol = mpol;
28911da177e4SLinus Torvalds 	return 0;
28921da177e4SLinus Torvalds 
28931da177e4SLinus Torvalds bad_val:
28941da177e4SLinus Torvalds 	printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
28951da177e4SLinus Torvalds 	       value, this_char);
289649cd0a5cSGreg Thelen error:
289749cd0a5cSGreg Thelen 	mpol_put(mpol);
28981da177e4SLinus Torvalds 	return 1;
28991da177e4SLinus Torvalds 
29001da177e4SLinus Torvalds }
29011da177e4SLinus Torvalds 
29021da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
29031da177e4SLinus Torvalds {
29041da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2905680d794bSakpm@linux-foundation.org 	struct shmem_sb_info config = *sbinfo;
29060edd73b3SHugh Dickins 	unsigned long inodes;
29070edd73b3SHugh Dickins 	int error = -EINVAL;
29081da177e4SLinus Torvalds 
29095f00110fSGreg Thelen 	config.mpol = NULL;
2910680d794bSakpm@linux-foundation.org 	if (shmem_parse_options(data, &config, true))
29110edd73b3SHugh Dickins 		return error;
29120edd73b3SHugh Dickins 
29130edd73b3SHugh Dickins 	spin_lock(&sbinfo->stat_lock);
29140edd73b3SHugh Dickins 	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
29157e496299STim Chen 	if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
29160edd73b3SHugh Dickins 		goto out;
2917680d794bSakpm@linux-foundation.org 	if (config.max_inodes < inodes)
29180edd73b3SHugh Dickins 		goto out;
29190edd73b3SHugh Dickins 	/*
292054af6042SHugh Dickins 	 * Those tests disallow limited->unlimited while any are in use;
29210edd73b3SHugh Dickins 	 * but we must separately disallow unlimited->limited, because
29220edd73b3SHugh Dickins 	 * in that case we have no record of how much is already in use.
29230edd73b3SHugh Dickins 	 */
2924680d794bSakpm@linux-foundation.org 	if (config.max_blocks && !sbinfo->max_blocks)
29250edd73b3SHugh Dickins 		goto out;
2926680d794bSakpm@linux-foundation.org 	if (config.max_inodes && !sbinfo->max_inodes)
29270edd73b3SHugh Dickins 		goto out;
29280edd73b3SHugh Dickins 
29290edd73b3SHugh Dickins 	error = 0;
2930680d794bSakpm@linux-foundation.org 	sbinfo->max_blocks  = config.max_blocks;
2931680d794bSakpm@linux-foundation.org 	sbinfo->max_inodes  = config.max_inodes;
2932680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = config.max_inodes - inodes;
293371fe804bSLee Schermerhorn 
29345f00110fSGreg Thelen 	/*
29355f00110fSGreg Thelen 	 * Preserve previous mempolicy unless mpol remount option was specified.
29365f00110fSGreg Thelen 	 */
29375f00110fSGreg Thelen 	if (config.mpol) {
293871fe804bSLee Schermerhorn 		mpol_put(sbinfo->mpol);
293971fe804bSLee Schermerhorn 		sbinfo->mpol = config.mpol;	/* transfers initial ref */
29405f00110fSGreg Thelen 	}
29410edd73b3SHugh Dickins out:
29420edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
29430edd73b3SHugh Dickins 	return error;
29441da177e4SLinus Torvalds }
2945680d794bSakpm@linux-foundation.org 
294634c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root)
2947680d794bSakpm@linux-foundation.org {
294834c80b1dSAl Viro 	struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
2949680d794bSakpm@linux-foundation.org 
2950680d794bSakpm@linux-foundation.org 	if (sbinfo->max_blocks != shmem_default_max_blocks())
2951680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",size=%luk",
2952680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2953680d794bSakpm@linux-foundation.org 	if (sbinfo->max_inodes != shmem_default_max_inodes())
2954680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2955680d794bSakpm@linux-foundation.org 	if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
295609208d15SAl Viro 		seq_printf(seq, ",mode=%03ho", sbinfo->mode);
29578751e039SEric W. Biederman 	if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
29588751e039SEric W. Biederman 		seq_printf(seq, ",uid=%u",
29598751e039SEric W. Biederman 				from_kuid_munged(&init_user_ns, sbinfo->uid));
29608751e039SEric W. Biederman 	if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
29618751e039SEric W. Biederman 		seq_printf(seq, ",gid=%u",
29628751e039SEric W. Biederman 				from_kgid_munged(&init_user_ns, sbinfo->gid));
296371fe804bSLee Schermerhorn 	shmem_show_mpol(seq, sbinfo->mpol);
2964680d794bSakpm@linux-foundation.org 	return 0;
2965680d794bSakpm@linux-foundation.org }
29669183df25SDavid Herrmann 
29679183df25SDavid Herrmann #define MFD_NAME_PREFIX "memfd:"
29689183df25SDavid Herrmann #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
29699183df25SDavid Herrmann #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
29709183df25SDavid Herrmann 
29719183df25SDavid Herrmann #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
29729183df25SDavid Herrmann 
29739183df25SDavid Herrmann SYSCALL_DEFINE2(memfd_create,
29749183df25SDavid Herrmann 		const char __user *, uname,
29759183df25SDavid Herrmann 		unsigned int, flags)
29769183df25SDavid Herrmann {
29779183df25SDavid Herrmann 	struct shmem_inode_info *info;
29789183df25SDavid Herrmann 	struct file *file;
29799183df25SDavid Herrmann 	int fd, error;
29809183df25SDavid Herrmann 	char *name;
29819183df25SDavid Herrmann 	long len;
29829183df25SDavid Herrmann 
29839183df25SDavid Herrmann 	if (flags & ~(unsigned int)MFD_ALL_FLAGS)
29849183df25SDavid Herrmann 		return -EINVAL;
29859183df25SDavid Herrmann 
29869183df25SDavid Herrmann 	/* length includes terminating zero */
29879183df25SDavid Herrmann 	len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
29889183df25SDavid Herrmann 	if (len <= 0)
29899183df25SDavid Herrmann 		return -EFAULT;
29909183df25SDavid Herrmann 	if (len > MFD_NAME_MAX_LEN + 1)
29919183df25SDavid Herrmann 		return -EINVAL;
29929183df25SDavid Herrmann 
29939183df25SDavid Herrmann 	name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
29949183df25SDavid Herrmann 	if (!name)
29959183df25SDavid Herrmann 		return -ENOMEM;
29969183df25SDavid Herrmann 
29979183df25SDavid Herrmann 	strcpy(name, MFD_NAME_PREFIX);
29989183df25SDavid Herrmann 	if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
29999183df25SDavid Herrmann 		error = -EFAULT;
30009183df25SDavid Herrmann 		goto err_name;
30019183df25SDavid Herrmann 	}
30029183df25SDavid Herrmann 
30039183df25SDavid Herrmann 	/* terminating-zero may have changed after strnlen_user() returned */
30049183df25SDavid Herrmann 	if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
30059183df25SDavid Herrmann 		error = -EFAULT;
30069183df25SDavid Herrmann 		goto err_name;
30079183df25SDavid Herrmann 	}
30089183df25SDavid Herrmann 
30099183df25SDavid Herrmann 	fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
30109183df25SDavid Herrmann 	if (fd < 0) {
30119183df25SDavid Herrmann 		error = fd;
30129183df25SDavid Herrmann 		goto err_name;
30139183df25SDavid Herrmann 	}
30149183df25SDavid Herrmann 
30159183df25SDavid Herrmann 	file = shmem_file_setup(name, 0, VM_NORESERVE);
30169183df25SDavid Herrmann 	if (IS_ERR(file)) {
30179183df25SDavid Herrmann 		error = PTR_ERR(file);
30189183df25SDavid Herrmann 		goto err_fd;
30199183df25SDavid Herrmann 	}
30209183df25SDavid Herrmann 	info = SHMEM_I(file_inode(file));
30219183df25SDavid Herrmann 	file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
30229183df25SDavid Herrmann 	file->f_flags |= O_RDWR | O_LARGEFILE;
30239183df25SDavid Herrmann 	if (flags & MFD_ALLOW_SEALING)
30249183df25SDavid Herrmann 		info->seals &= ~F_SEAL_SEAL;
30259183df25SDavid Herrmann 
30269183df25SDavid Herrmann 	fd_install(fd, file);
30279183df25SDavid Herrmann 	kfree(name);
30289183df25SDavid Herrmann 	return fd;
30299183df25SDavid Herrmann 
30309183df25SDavid Herrmann err_fd:
30319183df25SDavid Herrmann 	put_unused_fd(fd);
30329183df25SDavid Herrmann err_name:
30339183df25SDavid Herrmann 	kfree(name);
30349183df25SDavid Herrmann 	return error;
30359183df25SDavid Herrmann }
30369183df25SDavid Herrmann 
3037680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
30381da177e4SLinus Torvalds 
30391da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb)
30401da177e4SLinus Torvalds {
3041602586a8SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3042602586a8SHugh Dickins 
3043602586a8SHugh Dickins 	percpu_counter_destroy(&sbinfo->used_blocks);
304449cd0a5cSGreg Thelen 	mpol_put(sbinfo->mpol);
3045602586a8SHugh Dickins 	kfree(sbinfo);
30461da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
30471da177e4SLinus Torvalds }
30481da177e4SLinus Torvalds 
30492b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent)
30501da177e4SLinus Torvalds {
30511da177e4SLinus Torvalds 	struct inode *inode;
30520edd73b3SHugh Dickins 	struct shmem_sb_info *sbinfo;
3053680d794bSakpm@linux-foundation.org 	int err = -ENOMEM;
3054680d794bSakpm@linux-foundation.org 
3055680d794bSakpm@linux-foundation.org 	/* Round up to L1_CACHE_BYTES to resist false sharing */
3056425fbf04SPekka Enberg 	sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
3057680d794bSakpm@linux-foundation.org 				L1_CACHE_BYTES), GFP_KERNEL);
3058680d794bSakpm@linux-foundation.org 	if (!sbinfo)
3059680d794bSakpm@linux-foundation.org 		return -ENOMEM;
3060680d794bSakpm@linux-foundation.org 
3061680d794bSakpm@linux-foundation.org 	sbinfo->mode = S_IRWXUGO | S_ISVTX;
306276aac0e9SDavid Howells 	sbinfo->uid = current_fsuid();
306376aac0e9SDavid Howells 	sbinfo->gid = current_fsgid();
3064680d794bSakpm@linux-foundation.org 	sb->s_fs_info = sbinfo;
30651da177e4SLinus Torvalds 
30660edd73b3SHugh Dickins #ifdef CONFIG_TMPFS
30671da177e4SLinus Torvalds 	/*
30681da177e4SLinus Torvalds 	 * Per default we only allow half of the physical ram per
30691da177e4SLinus Torvalds 	 * tmpfs instance, limiting inodes to one per page of lowmem;
30701da177e4SLinus Torvalds 	 * but the internal instance is left unlimited.
30711da177e4SLinus Torvalds 	 */
3072ca4e0519SAl Viro 	if (!(sb->s_flags & MS_KERNMOUNT)) {
3073680d794bSakpm@linux-foundation.org 		sbinfo->max_blocks = shmem_default_max_blocks();
3074680d794bSakpm@linux-foundation.org 		sbinfo->max_inodes = shmem_default_max_inodes();
3075680d794bSakpm@linux-foundation.org 		if (shmem_parse_options(data, sbinfo, false)) {
3076680d794bSakpm@linux-foundation.org 			err = -EINVAL;
3077680d794bSakpm@linux-foundation.org 			goto failed;
3078680d794bSakpm@linux-foundation.org 		}
3079ca4e0519SAl Viro 	} else {
3080ca4e0519SAl Viro 		sb->s_flags |= MS_NOUSER;
30811da177e4SLinus Torvalds 	}
308291828a40SDavid M. Grimes 	sb->s_export_op = &shmem_export_ops;
30832f6e38f3SHugh Dickins 	sb->s_flags |= MS_NOSEC;
30840edd73b3SHugh Dickins #else
30850edd73b3SHugh Dickins 	sb->s_flags |= MS_NOUSER;
30860edd73b3SHugh Dickins #endif
30871da177e4SLinus Torvalds 
30881da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
3089908c7f19STejun Heo 	if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
3090602586a8SHugh Dickins 		goto failed;
3091680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = sbinfo->max_inodes;
30921da177e4SLinus Torvalds 
3093285b2c4fSHugh Dickins 	sb->s_maxbytes = MAX_LFS_FILESIZE;
30941da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
30951da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
30961da177e4SLinus Torvalds 	sb->s_magic = TMPFS_MAGIC;
30971da177e4SLinus Torvalds 	sb->s_op = &shmem_ops;
3098cfd95a9cSRobin H. Johnson 	sb->s_time_gran = 1;
3099b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
310039f0247dSAndreas Gruenbacher 	sb->s_xattr = shmem_xattr_handlers;
3101b09e0fa4SEric Paris #endif
3102b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
310339f0247dSAndreas Gruenbacher 	sb->s_flags |= MS_POSIXACL;
310439f0247dSAndreas Gruenbacher #endif
31050edd73b3SHugh Dickins 
3106454abafeSDmitry Monakhov 	inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
31071da177e4SLinus Torvalds 	if (!inode)
31081da177e4SLinus Torvalds 		goto failed;
3109680d794bSakpm@linux-foundation.org 	inode->i_uid = sbinfo->uid;
3110680d794bSakpm@linux-foundation.org 	inode->i_gid = sbinfo->gid;
3111318ceed0SAl Viro 	sb->s_root = d_make_root(inode);
3112318ceed0SAl Viro 	if (!sb->s_root)
311348fde701SAl Viro 		goto failed;
31141da177e4SLinus Torvalds 	return 0;
31151da177e4SLinus Torvalds 
31161da177e4SLinus Torvalds failed:
31171da177e4SLinus Torvalds 	shmem_put_super(sb);
31181da177e4SLinus Torvalds 	return err;
31191da177e4SLinus Torvalds }
31201da177e4SLinus Torvalds 
3121fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep;
31221da177e4SLinus Torvalds 
31231da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb)
31241da177e4SLinus Torvalds {
312541ffe5d5SHugh Dickins 	struct shmem_inode_info *info;
312641ffe5d5SHugh Dickins 	info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
312741ffe5d5SHugh Dickins 	if (!info)
31281da177e4SLinus Torvalds 		return NULL;
312941ffe5d5SHugh Dickins 	return &info->vfs_inode;
31301da177e4SLinus Torvalds }
31311da177e4SLinus Torvalds 
313241ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head)
3133fa0d7e3dSNick Piggin {
3134fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
3135fa0d7e3dSNick Piggin 	kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3136fa0d7e3dSNick Piggin }
3137fa0d7e3dSNick Piggin 
31381da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode)
31391da177e4SLinus Torvalds {
314009208d15SAl Viro 	if (S_ISREG(inode->i_mode))
31411da177e4SLinus Torvalds 		mpol_free_shared_policy(&SHMEM_I(inode)->policy);
314241ffe5d5SHugh Dickins 	call_rcu(&inode->i_rcu, shmem_destroy_callback);
31431da177e4SLinus Torvalds }
31441da177e4SLinus Torvalds 
314541ffe5d5SHugh Dickins static void shmem_init_inode(void *foo)
31461da177e4SLinus Torvalds {
314741ffe5d5SHugh Dickins 	struct shmem_inode_info *info = foo;
314841ffe5d5SHugh Dickins 	inode_init_once(&info->vfs_inode);
31491da177e4SLinus Torvalds }
31501da177e4SLinus Torvalds 
315141ffe5d5SHugh Dickins static int shmem_init_inodecache(void)
31521da177e4SLinus Torvalds {
31531da177e4SLinus Torvalds 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
31541da177e4SLinus Torvalds 				sizeof(struct shmem_inode_info),
31555d097056SVladimir Davydov 				0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
31561da177e4SLinus Torvalds 	return 0;
31571da177e4SLinus Torvalds }
31581da177e4SLinus Torvalds 
315941ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void)
31601da177e4SLinus Torvalds {
31611a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(shmem_inode_cachep);
31621da177e4SLinus Torvalds }
31631da177e4SLinus Torvalds 
3164f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = {
31651da177e4SLinus Torvalds 	.writepage	= shmem_writepage,
316676719325SKen Chen 	.set_page_dirty	= __set_page_dirty_no_writeback,
31671da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
3168800d15a5SNick Piggin 	.write_begin	= shmem_write_begin,
3169800d15a5SNick Piggin 	.write_end	= shmem_write_end,
31701da177e4SLinus Torvalds #endif
31711c93923cSAndrew Morton #ifdef CONFIG_MIGRATION
3172304dbdb7SLee Schermerhorn 	.migratepage	= migrate_page,
31731c93923cSAndrew Morton #endif
3174aa261f54SAndi Kleen 	.error_remove_page = generic_error_remove_page,
31751da177e4SLinus Torvalds };
31761da177e4SLinus Torvalds 
317715ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = {
31781da177e4SLinus Torvalds 	.mmap		= shmem_mmap,
31791da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
3180220f2ac9SHugh Dickins 	.llseek		= shmem_file_llseek,
31812ba5bbedSAl Viro 	.read_iter	= shmem_file_read_iter,
31828174202bSAl Viro 	.write_iter	= generic_file_write_iter,
31831b061d92SChristoph Hellwig 	.fsync		= noop_fsync,
3184708e3508SHugh Dickins 	.splice_read	= shmem_file_splice_read,
3185f6cb85d0SAl Viro 	.splice_write	= iter_file_splice_write,
318683e4fa9cSHugh Dickins 	.fallocate	= shmem_fallocate,
31871da177e4SLinus Torvalds #endif
31881da177e4SLinus Torvalds };
31891da177e4SLinus Torvalds 
319092e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = {
319144a30220SYu Zhao 	.getattr	= shmem_getattr,
319294c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
3193b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
3194aa7c5241SAndreas Gruenbacher 	.setxattr	= generic_setxattr,
3195aa7c5241SAndreas Gruenbacher 	.getxattr	= generic_getxattr,
3196b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
3197aa7c5241SAndreas Gruenbacher 	.removexattr	= generic_removexattr,
3198feda821eSChristoph Hellwig 	.set_acl	= simple_set_acl,
3199b09e0fa4SEric Paris #endif
32001da177e4SLinus Torvalds };
32011da177e4SLinus Torvalds 
320292e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = {
32031da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
32041da177e4SLinus Torvalds 	.create		= shmem_create,
32051da177e4SLinus Torvalds 	.lookup		= simple_lookup,
32061da177e4SLinus Torvalds 	.link		= shmem_link,
32071da177e4SLinus Torvalds 	.unlink		= shmem_unlink,
32081da177e4SLinus Torvalds 	.symlink	= shmem_symlink,
32091da177e4SLinus Torvalds 	.mkdir		= shmem_mkdir,
32101da177e4SLinus Torvalds 	.rmdir		= shmem_rmdir,
32111da177e4SLinus Torvalds 	.mknod		= shmem_mknod,
32123b69ff51SMiklos Szeredi 	.rename2	= shmem_rename2,
321360545d0dSAl Viro 	.tmpfile	= shmem_tmpfile,
32141da177e4SLinus Torvalds #endif
3215b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
3216aa7c5241SAndreas Gruenbacher 	.setxattr	= generic_setxattr,
3217aa7c5241SAndreas Gruenbacher 	.getxattr	= generic_getxattr,
3218b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
3219aa7c5241SAndreas Gruenbacher 	.removexattr	= generic_removexattr,
3220b09e0fa4SEric Paris #endif
322139f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
322294c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
3223feda821eSChristoph Hellwig 	.set_acl	= simple_set_acl,
322439f0247dSAndreas Gruenbacher #endif
322539f0247dSAndreas Gruenbacher };
322639f0247dSAndreas Gruenbacher 
322792e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = {
3228b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
3229aa7c5241SAndreas Gruenbacher 	.setxattr	= generic_setxattr,
3230aa7c5241SAndreas Gruenbacher 	.getxattr	= generic_getxattr,
3231b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
3232aa7c5241SAndreas Gruenbacher 	.removexattr	= generic_removexattr,
3233b09e0fa4SEric Paris #endif
323439f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
323594c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
3236feda821eSChristoph Hellwig 	.set_acl	= simple_set_acl,
323739f0247dSAndreas Gruenbacher #endif
32381da177e4SLinus Torvalds };
32391da177e4SLinus Torvalds 
3240759b9775SHugh Dickins static const struct super_operations shmem_ops = {
32411da177e4SLinus Torvalds 	.alloc_inode	= shmem_alloc_inode,
32421da177e4SLinus Torvalds 	.destroy_inode	= shmem_destroy_inode,
32431da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
32441da177e4SLinus Torvalds 	.statfs		= shmem_statfs,
32451da177e4SLinus Torvalds 	.remount_fs	= shmem_remount_fs,
3246680d794bSakpm@linux-foundation.org 	.show_options	= shmem_show_options,
32471da177e4SLinus Torvalds #endif
32481f895f75SAl Viro 	.evict_inode	= shmem_evict_inode,
32491da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
32501da177e4SLinus Torvalds 	.put_super	= shmem_put_super,
32511da177e4SLinus Torvalds };
32521da177e4SLinus Torvalds 
3253f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = {
325454cb8821SNick Piggin 	.fault		= shmem_fault,
3255d7c17551SNing Qu 	.map_pages	= filemap_map_pages,
32561da177e4SLinus Torvalds #ifdef CONFIG_NUMA
32571da177e4SLinus Torvalds 	.set_policy     = shmem_set_policy,
32581da177e4SLinus Torvalds 	.get_policy     = shmem_get_policy,
32591da177e4SLinus Torvalds #endif
32601da177e4SLinus Torvalds };
32611da177e4SLinus Torvalds 
32623c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type,
32633c26ff6eSAl Viro 	int flags, const char *dev_name, void *data)
32641da177e4SLinus Torvalds {
32653c26ff6eSAl Viro 	return mount_nodev(fs_type, flags, data, shmem_fill_super);
32661da177e4SLinus Torvalds }
32671da177e4SLinus Torvalds 
326841ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = {
32691da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
32701da177e4SLinus Torvalds 	.name		= "tmpfs",
32713c26ff6eSAl Viro 	.mount		= shmem_mount,
32721da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
32732b8576cbSEric W. Biederman 	.fs_flags	= FS_USERNS_MOUNT,
32741da177e4SLinus Torvalds };
32751da177e4SLinus Torvalds 
327641ffe5d5SHugh Dickins int __init shmem_init(void)
32771da177e4SLinus Torvalds {
32781da177e4SLinus Torvalds 	int error;
32791da177e4SLinus Torvalds 
328016203a7aSRob Landley 	/* If rootfs called this, don't re-init */
328116203a7aSRob Landley 	if (shmem_inode_cachep)
328216203a7aSRob Landley 		return 0;
328316203a7aSRob Landley 
328441ffe5d5SHugh Dickins 	error = shmem_init_inodecache();
32851da177e4SLinus Torvalds 	if (error)
32861da177e4SLinus Torvalds 		goto out3;
32871da177e4SLinus Torvalds 
328841ffe5d5SHugh Dickins 	error = register_filesystem(&shmem_fs_type);
32891da177e4SLinus Torvalds 	if (error) {
32901da177e4SLinus Torvalds 		printk(KERN_ERR "Could not register tmpfs\n");
32911da177e4SLinus Torvalds 		goto out2;
32921da177e4SLinus Torvalds 	}
329395dc112aSGreg Kroah-Hartman 
3294ca4e0519SAl Viro 	shm_mnt = kern_mount(&shmem_fs_type);
32951da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt)) {
32961da177e4SLinus Torvalds 		error = PTR_ERR(shm_mnt);
32971da177e4SLinus Torvalds 		printk(KERN_ERR "Could not kern_mount tmpfs\n");
32981da177e4SLinus Torvalds 		goto out1;
32991da177e4SLinus Torvalds 	}
33001da177e4SLinus Torvalds 	return 0;
33011da177e4SLinus Torvalds 
33021da177e4SLinus Torvalds out1:
330341ffe5d5SHugh Dickins 	unregister_filesystem(&shmem_fs_type);
33041da177e4SLinus Torvalds out2:
330541ffe5d5SHugh Dickins 	shmem_destroy_inodecache();
33061da177e4SLinus Torvalds out3:
33071da177e4SLinus Torvalds 	shm_mnt = ERR_PTR(error);
33081da177e4SLinus Torvalds 	return error;
33091da177e4SLinus Torvalds }
3310853ac43aSMatt Mackall 
3311853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */
3312853ac43aSMatt Mackall 
3313853ac43aSMatt Mackall /*
3314853ac43aSMatt Mackall  * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3315853ac43aSMatt Mackall  *
3316853ac43aSMatt Mackall  * This is intended for small system where the benefits of the full
3317853ac43aSMatt Mackall  * shmem code (swap-backed and resource-limited) are outweighed by
3318853ac43aSMatt Mackall  * their complexity. On systems without swap this code should be
3319853ac43aSMatt Mackall  * effectively equivalent, but much lighter weight.
3320853ac43aSMatt Mackall  */
3321853ac43aSMatt Mackall 
332241ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = {
3323853ac43aSMatt Mackall 	.name		= "tmpfs",
33243c26ff6eSAl Viro 	.mount		= ramfs_mount,
3325853ac43aSMatt Mackall 	.kill_sb	= kill_litter_super,
33262b8576cbSEric W. Biederman 	.fs_flags	= FS_USERNS_MOUNT,
3327853ac43aSMatt Mackall };
3328853ac43aSMatt Mackall 
332941ffe5d5SHugh Dickins int __init shmem_init(void)
3330853ac43aSMatt Mackall {
333141ffe5d5SHugh Dickins 	BUG_ON(register_filesystem(&shmem_fs_type) != 0);
3332853ac43aSMatt Mackall 
333341ffe5d5SHugh Dickins 	shm_mnt = kern_mount(&shmem_fs_type);
3334853ac43aSMatt Mackall 	BUG_ON(IS_ERR(shm_mnt));
3335853ac43aSMatt Mackall 
3336853ac43aSMatt Mackall 	return 0;
3337853ac43aSMatt Mackall }
3338853ac43aSMatt Mackall 
333941ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page)
3340853ac43aSMatt Mackall {
3341853ac43aSMatt Mackall 	return 0;
3342853ac43aSMatt Mackall }
3343853ac43aSMatt Mackall 
33443f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user)
33453f96b79aSHugh Dickins {
33463f96b79aSHugh Dickins 	return 0;
33473f96b79aSHugh Dickins }
33483f96b79aSHugh Dickins 
334924513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping)
335024513264SHugh Dickins {
335124513264SHugh Dickins }
335224513264SHugh Dickins 
335341ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
335494c1e62dSHugh Dickins {
335541ffe5d5SHugh Dickins 	truncate_inode_pages_range(inode->i_mapping, lstart, lend);
335694c1e62dSHugh Dickins }
335794c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
335894c1e62dSHugh Dickins 
3359853ac43aSMatt Mackall #define shmem_vm_ops				generic_file_vm_ops
33600b0a0806SHugh Dickins #define shmem_file_operations			ramfs_file_operations
3361454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags)	ramfs_get_inode(sb, dir, mode, dev)
33620b0a0806SHugh Dickins #define shmem_acct_size(flags, size)		0
33630b0a0806SHugh Dickins #define shmem_unacct_size(flags, size)		do {} while (0)
3364853ac43aSMatt Mackall 
3365853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */
3366853ac43aSMatt Mackall 
3367853ac43aSMatt Mackall /* common code */
33681da177e4SLinus Torvalds 
33693451538aSAl Viro static struct dentry_operations anon_ops = {
3370118b2302SAl Viro 	.d_dname = simple_dname
33713451538aSAl Viro };
33723451538aSAl Viro 
3373c7277090SEric Paris static struct file *__shmem_file_setup(const char *name, loff_t size,
3374c7277090SEric Paris 				       unsigned long flags, unsigned int i_flags)
33751da177e4SLinus Torvalds {
33766b4d0b27SAl Viro 	struct file *res;
33771da177e4SLinus Torvalds 	struct inode *inode;
33782c48b9c4SAl Viro 	struct path path;
33793451538aSAl Viro 	struct super_block *sb;
33801da177e4SLinus Torvalds 	struct qstr this;
33811da177e4SLinus Torvalds 
33821da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt))
33836b4d0b27SAl Viro 		return ERR_CAST(shm_mnt);
33841da177e4SLinus Torvalds 
3385285b2c4fSHugh Dickins 	if (size < 0 || size > MAX_LFS_FILESIZE)
33861da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
33871da177e4SLinus Torvalds 
33881da177e4SLinus Torvalds 	if (shmem_acct_size(flags, size))
33891da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
33901da177e4SLinus Torvalds 
33916b4d0b27SAl Viro 	res = ERR_PTR(-ENOMEM);
33921da177e4SLinus Torvalds 	this.name = name;
33931da177e4SLinus Torvalds 	this.len = strlen(name);
33941da177e4SLinus Torvalds 	this.hash = 0; /* will go */
33953451538aSAl Viro 	sb = shm_mnt->mnt_sb;
339666ee4b88SKonstantin Khlebnikov 	path.mnt = mntget(shm_mnt);
33973451538aSAl Viro 	path.dentry = d_alloc_pseudo(sb, &this);
33982c48b9c4SAl Viro 	if (!path.dentry)
33991da177e4SLinus Torvalds 		goto put_memory;
34003451538aSAl Viro 	d_set_d_op(path.dentry, &anon_ops);
34011da177e4SLinus Torvalds 
34026b4d0b27SAl Viro 	res = ERR_PTR(-ENOSPC);
34033451538aSAl Viro 	inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
34041da177e4SLinus Torvalds 	if (!inode)
340566ee4b88SKonstantin Khlebnikov 		goto put_memory;
34061da177e4SLinus Torvalds 
3407c7277090SEric Paris 	inode->i_flags |= i_flags;
34082c48b9c4SAl Viro 	d_instantiate(path.dentry, inode);
34091da177e4SLinus Torvalds 	inode->i_size = size;
34106d6b77f1SMiklos Szeredi 	clear_nlink(inode);	/* It is unlinked */
341126567cdbSAl Viro 	res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
341226567cdbSAl Viro 	if (IS_ERR(res))
341366ee4b88SKonstantin Khlebnikov 		goto put_path;
34144b42af81SAl Viro 
34156b4d0b27SAl Viro 	res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
34164b42af81SAl Viro 		  &shmem_file_operations);
34176b4d0b27SAl Viro 	if (IS_ERR(res))
341866ee4b88SKonstantin Khlebnikov 		goto put_path;
34194b42af81SAl Viro 
34206b4d0b27SAl Viro 	return res;
34211da177e4SLinus Torvalds 
34221da177e4SLinus Torvalds put_memory:
34231da177e4SLinus Torvalds 	shmem_unacct_size(flags, size);
342466ee4b88SKonstantin Khlebnikov put_path:
342566ee4b88SKonstantin Khlebnikov 	path_put(&path);
34266b4d0b27SAl Viro 	return res;
34271da177e4SLinus Torvalds }
3428c7277090SEric Paris 
3429c7277090SEric Paris /**
3430c7277090SEric Paris  * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3431c7277090SEric Paris  * 	kernel internal.  There will be NO LSM permission checks against the
3432c7277090SEric Paris  * 	underlying inode.  So users of this interface must do LSM checks at a
3433e1832f29SStephen Smalley  *	higher layer.  The users are the big_key and shm implementations.  LSM
3434e1832f29SStephen Smalley  *	checks are provided at the key or shm level rather than the inode.
3435c7277090SEric Paris  * @name: name for dentry (to be seen in /proc/<pid>/maps
3436c7277090SEric Paris  * @size: size to be set for the file
3437c7277090SEric Paris  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3438c7277090SEric Paris  */
3439c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3440c7277090SEric Paris {
3441c7277090SEric Paris 	return __shmem_file_setup(name, size, flags, S_PRIVATE);
3442c7277090SEric Paris }
3443c7277090SEric Paris 
3444c7277090SEric Paris /**
3445c7277090SEric Paris  * shmem_file_setup - get an unlinked file living in tmpfs
3446c7277090SEric Paris  * @name: name for dentry (to be seen in /proc/<pid>/maps
3447c7277090SEric Paris  * @size: size to be set for the file
3448c7277090SEric Paris  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3449c7277090SEric Paris  */
3450c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3451c7277090SEric Paris {
3452c7277090SEric Paris 	return __shmem_file_setup(name, size, flags, 0);
3453c7277090SEric Paris }
3454395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup);
34551da177e4SLinus Torvalds 
345646711810SRandy Dunlap /**
34571da177e4SLinus Torvalds  * shmem_zero_setup - setup a shared anonymous mapping
34581da177e4SLinus Torvalds  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
34591da177e4SLinus Torvalds  */
34601da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma)
34611da177e4SLinus Torvalds {
34621da177e4SLinus Torvalds 	struct file *file;
34631da177e4SLinus Torvalds 	loff_t size = vma->vm_end - vma->vm_start;
34641da177e4SLinus Torvalds 
346566fc1303SHugh Dickins 	/*
346666fc1303SHugh Dickins 	 * Cloning a new file under mmap_sem leads to a lock ordering conflict
346766fc1303SHugh Dickins 	 * between XFS directory reading and selinux: since this file is only
346866fc1303SHugh Dickins 	 * accessible to the user through its mapping, use S_PRIVATE flag to
346966fc1303SHugh Dickins 	 * bypass file security, in the same way as shmem_kernel_file_setup().
347066fc1303SHugh Dickins 	 */
347166fc1303SHugh Dickins 	file = __shmem_file_setup("dev/zero", size, vma->vm_flags, S_PRIVATE);
34721da177e4SLinus Torvalds 	if (IS_ERR(file))
34731da177e4SLinus Torvalds 		return PTR_ERR(file);
34741da177e4SLinus Torvalds 
34751da177e4SLinus Torvalds 	if (vma->vm_file)
34761da177e4SLinus Torvalds 		fput(vma->vm_file);
34771da177e4SLinus Torvalds 	vma->vm_file = file;
34781da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
34791da177e4SLinus Torvalds 	return 0;
34801da177e4SLinus Torvalds }
3481d9d90e5eSHugh Dickins 
3482d9d90e5eSHugh Dickins /**
3483d9d90e5eSHugh Dickins  * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3484d9d90e5eSHugh Dickins  * @mapping:	the page's address_space
3485d9d90e5eSHugh Dickins  * @index:	the page index
3486d9d90e5eSHugh Dickins  * @gfp:	the page allocator flags to use if allocating
3487d9d90e5eSHugh Dickins  *
3488d9d90e5eSHugh Dickins  * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3489d9d90e5eSHugh Dickins  * with any new page allocations done using the specified allocation flags.
3490d9d90e5eSHugh Dickins  * But read_cache_page_gfp() uses the ->readpage() method: which does not
3491d9d90e5eSHugh Dickins  * suit tmpfs, since it may have pages in swapcache, and needs to find those
3492d9d90e5eSHugh Dickins  * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
3493d9d90e5eSHugh Dickins  *
349468da9f05SHugh Dickins  * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
349568da9f05SHugh Dickins  * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
3496d9d90e5eSHugh Dickins  */
3497d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
3498d9d90e5eSHugh Dickins 					 pgoff_t index, gfp_t gfp)
3499d9d90e5eSHugh Dickins {
350068da9f05SHugh Dickins #ifdef CONFIG_SHMEM
350168da9f05SHugh Dickins 	struct inode *inode = mapping->host;
35029276aad6SHugh Dickins 	struct page *page;
350368da9f05SHugh Dickins 	int error;
350468da9f05SHugh Dickins 
350568da9f05SHugh Dickins 	BUG_ON(mapping->a_ops != &shmem_aops);
350668da9f05SHugh Dickins 	error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
350768da9f05SHugh Dickins 	if (error)
350868da9f05SHugh Dickins 		page = ERR_PTR(error);
350968da9f05SHugh Dickins 	else
351068da9f05SHugh Dickins 		unlock_page(page);
351168da9f05SHugh Dickins 	return page;
351268da9f05SHugh Dickins #else
351368da9f05SHugh Dickins 	/*
351468da9f05SHugh Dickins 	 * The tiny !SHMEM case uses ramfs without swap
351568da9f05SHugh Dickins 	 */
3516d9d90e5eSHugh Dickins 	return read_cache_page_gfp(mapping, index, gfp);
351768da9f05SHugh Dickins #endif
3518d9d90e5eSHugh Dickins }
3519d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);
3520