xref: /openbmc/linux/mm/shmem.c (revision c5bf121e4350a933bd431385e6fcb72a898ecc68)
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>
3246c9a946SArnd Bergmann #include <linux/random.h>
33174cd4b1SIngo Molnar #include <linux/sched/signal.h>
34b95f1b31SPaul Gortmaker #include <linux/export.h>
35853ac43aSMatt Mackall #include <linux/swap.h>
36e2e40f2cSChristoph Hellwig #include <linux/uio.h>
37f3f0e1d2SKirill A. Shutemov #include <linux/khugepaged.h>
38749df87bSMike Kravetz #include <linux/hugetlb.h>
39853ac43aSMatt Mackall 
4095cc09d6SAndrea Arcangeli #include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */
4195cc09d6SAndrea Arcangeli 
42853ac43aSMatt Mackall static struct vfsmount *shm_mnt;
43853ac43aSMatt Mackall 
44853ac43aSMatt Mackall #ifdef CONFIG_SHMEM
451da177e4SLinus Torvalds /*
461da177e4SLinus Torvalds  * This virtual memory filesystem is heavily based on the ramfs. It
471da177e4SLinus Torvalds  * extends ramfs by the ability to use swap and honor resource limits
481da177e4SLinus Torvalds  * which makes it a completely usable filesystem.
491da177e4SLinus Torvalds  */
501da177e4SLinus Torvalds 
5139f0247dSAndreas Gruenbacher #include <linux/xattr.h>
52a5694255SChristoph Hellwig #include <linux/exportfs.h>
531c7c474cSChristoph Hellwig #include <linux/posix_acl.h>
54feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h>
551da177e4SLinus Torvalds #include <linux/mman.h>
561da177e4SLinus Torvalds #include <linux/string.h>
571da177e4SLinus Torvalds #include <linux/slab.h>
581da177e4SLinus Torvalds #include <linux/backing-dev.h>
591da177e4SLinus Torvalds #include <linux/shmem_fs.h>
601da177e4SLinus Torvalds #include <linux/writeback.h>
611da177e4SLinus Torvalds #include <linux/blkdev.h>
62bda97eabSHugh Dickins #include <linux/pagevec.h>
6341ffe5d5SHugh Dickins #include <linux/percpu_counter.h>
6483e4fa9cSHugh Dickins #include <linux/falloc.h>
65708e3508SHugh Dickins #include <linux/splice.h>
661da177e4SLinus Torvalds #include <linux/security.h>
671da177e4SLinus Torvalds #include <linux/swapops.h>
681da177e4SLinus Torvalds #include <linux/mempolicy.h>
691da177e4SLinus Torvalds #include <linux/namei.h>
70b00dc3adSHugh Dickins #include <linux/ctype.h>
71304dbdb7SLee Schermerhorn #include <linux/migrate.h>
72c1f60a5aSChristoph Lameter #include <linux/highmem.h>
73680d794bSakpm@linux-foundation.org #include <linux/seq_file.h>
7492562927SMimi Zohar #include <linux/magic.h>
759183df25SDavid Herrmann #include <linux/syscalls.h>
7640e041a2SDavid Herrmann #include <linux/fcntl.h>
779183df25SDavid Herrmann #include <uapi/linux/memfd.h>
78cfda0526SMike Rapoport #include <linux/userfaultfd_k.h>
794c27fe4cSMike Rapoport #include <linux/rmap.h>
802b4db796SAmir Goldstein #include <linux/uuid.h>
81304dbdb7SLee Schermerhorn 
827c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
831da177e4SLinus Torvalds #include <asm/pgtable.h>
841da177e4SLinus Torvalds 
85dd56b046SMel Gorman #include "internal.h"
86dd56b046SMel Gorman 
8709cbfeafSKirill A. Shutemov #define BLOCKS_PER_PAGE  (PAGE_SIZE/512)
8809cbfeafSKirill A. Shutemov #define VM_ACCT(size)    (PAGE_ALIGN(size) >> PAGE_SHIFT)
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */
911da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20
921da177e4SLinus Torvalds 
9369f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
9469f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128
9569f07ec9SHugh Dickins 
961aac1400SHugh Dickins /*
97f00cdc6dSHugh Dickins  * shmem_fallocate communicates with shmem_fault or shmem_writepage via
98f00cdc6dSHugh Dickins  * inode->i_private (with i_mutex making sure that it has only one user at
99f00cdc6dSHugh Dickins  * a time): we would prefer not to enlarge the shmem inode just for that.
1001aac1400SHugh Dickins  */
1011aac1400SHugh Dickins struct shmem_falloc {
1028e205f77SHugh Dickins 	wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
1031aac1400SHugh Dickins 	pgoff_t start;		/* start of range currently being fallocated */
1041aac1400SHugh Dickins 	pgoff_t next;		/* the next page offset to be fallocated */
1051aac1400SHugh Dickins 	pgoff_t nr_falloced;	/* how many new pages have been fallocated */
1061aac1400SHugh Dickins 	pgoff_t nr_unswapped;	/* how often writepage refused to swap out */
1071aac1400SHugh Dickins };
1081aac1400SHugh Dickins 
109b76db735SAndrew Morton #ifdef CONFIG_TMPFS
110680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void)
111680d794bSakpm@linux-foundation.org {
112ca79b0c2SArun KS 	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 {
117ca79b0c2SArun KS 	unsigned long nr_pages = totalram_pages();
118ca79b0c2SArun KS 
119ca79b0c2SArun KS 	return min(nr_pages - totalhigh_pages(), nr_pages / 2);
120680d794bSakpm@linux-foundation.org }
121b76db735SAndrew Morton #endif
122680d794bSakpm@linux-foundation.org 
123bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
124bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp,
125bde05d1cSHugh Dickins 				struct shmem_inode_info *info, pgoff_t index);
126*c5bf121eSVineeth Remanan Pillai static int shmem_swapin_page(struct inode *inode, pgoff_t index,
127*c5bf121eSVineeth Remanan Pillai 			     struct page **pagep, enum sgp_type sgp,
128*c5bf121eSVineeth Remanan Pillai 			     gfp_t gfp, struct vm_area_struct *vma,
129*c5bf121eSVineeth Remanan Pillai 			     vm_fault_t *fault_type);
13068da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
1319e18eb29SAndres Lagar-Cavilla 		struct page **pagep, enum sgp_type sgp,
132cfda0526SMike Rapoport 		gfp_t gfp, struct vm_area_struct *vma,
1332b740303SSouptick Joarder 		struct vm_fault *vmf, vm_fault_t *fault_type);
13468da9f05SHugh Dickins 
135f3f0e1d2SKirill A. Shutemov int shmem_getpage(struct inode *inode, pgoff_t index,
1369e18eb29SAndres Lagar-Cavilla 		struct page **pagep, enum sgp_type sgp)
13768da9f05SHugh Dickins {
13868da9f05SHugh Dickins 	return shmem_getpage_gfp(inode, index, pagep, sgp,
139cfda0526SMike Rapoport 		mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL);
14068da9f05SHugh Dickins }
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	return sb->s_fs_info;
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds /*
1481da177e4SLinus Torvalds  * shmem_file_setup pre-accounts the whole fixed size of a VM object,
1491da177e4SLinus Torvalds  * for shared memory and for shared anonymous (/dev/zero) mappings
1501da177e4SLinus Torvalds  * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
1511da177e4SLinus Torvalds  * consistent with the pre-accounting of private mappings ...
1521da177e4SLinus Torvalds  */
1531da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size)
1541da177e4SLinus Torvalds {
1550b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
156191c5424SAl Viro 		0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size)
1601da177e4SLinus Torvalds {
1610b0a0806SHugh Dickins 	if (!(flags & VM_NORESERVE))
1621da177e4SLinus Torvalds 		vm_unacct_memory(VM_ACCT(size));
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
16577142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags,
16677142517SKonstantin Khlebnikov 		loff_t oldsize, loff_t newsize)
16777142517SKonstantin Khlebnikov {
16877142517SKonstantin Khlebnikov 	if (!(flags & VM_NORESERVE)) {
16977142517SKonstantin Khlebnikov 		if (VM_ACCT(newsize) > VM_ACCT(oldsize))
17077142517SKonstantin Khlebnikov 			return security_vm_enough_memory_mm(current->mm,
17177142517SKonstantin Khlebnikov 					VM_ACCT(newsize) - VM_ACCT(oldsize));
17277142517SKonstantin Khlebnikov 		else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
17377142517SKonstantin Khlebnikov 			vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
17477142517SKonstantin Khlebnikov 	}
17577142517SKonstantin Khlebnikov 	return 0;
17677142517SKonstantin Khlebnikov }
17777142517SKonstantin Khlebnikov 
1781da177e4SLinus Torvalds /*
1791da177e4SLinus Torvalds  * ... whereas tmpfs objects are accounted incrementally as
18075edd345SHugh Dickins  * pages are allocated, in order to allow large sparse files.
1811da177e4SLinus Torvalds  * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
1821da177e4SLinus Torvalds  * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
1831da177e4SLinus Torvalds  */
184800d8c63SKirill A. Shutemov static inline int shmem_acct_block(unsigned long flags, long pages)
1851da177e4SLinus Torvalds {
186800d8c63SKirill A. Shutemov 	if (!(flags & VM_NORESERVE))
187800d8c63SKirill A. Shutemov 		return 0;
188800d8c63SKirill A. Shutemov 
189800d8c63SKirill A. Shutemov 	return security_vm_enough_memory_mm(current->mm,
190800d8c63SKirill A. Shutemov 			pages * VM_ACCT(PAGE_SIZE));
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages)
1941da177e4SLinus Torvalds {
1950b0a0806SHugh Dickins 	if (flags & VM_NORESERVE)
19609cbfeafSKirill A. Shutemov 		vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
1990f079694SMike Rapoport static inline bool shmem_inode_acct_block(struct inode *inode, long pages)
2000f079694SMike Rapoport {
2010f079694SMike Rapoport 	struct shmem_inode_info *info = SHMEM_I(inode);
2020f079694SMike Rapoport 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
2030f079694SMike Rapoport 
2040f079694SMike Rapoport 	if (shmem_acct_block(info->flags, pages))
2050f079694SMike Rapoport 		return false;
2060f079694SMike Rapoport 
2070f079694SMike Rapoport 	if (sbinfo->max_blocks) {
2080f079694SMike Rapoport 		if (percpu_counter_compare(&sbinfo->used_blocks,
2090f079694SMike Rapoport 					   sbinfo->max_blocks - pages) > 0)
2100f079694SMike Rapoport 			goto unacct;
2110f079694SMike Rapoport 		percpu_counter_add(&sbinfo->used_blocks, pages);
2120f079694SMike Rapoport 	}
2130f079694SMike Rapoport 
2140f079694SMike Rapoport 	return true;
2150f079694SMike Rapoport 
2160f079694SMike Rapoport unacct:
2170f079694SMike Rapoport 	shmem_unacct_blocks(info->flags, pages);
2180f079694SMike Rapoport 	return false;
2190f079694SMike Rapoport }
2200f079694SMike Rapoport 
2210f079694SMike Rapoport static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages)
2220f079694SMike Rapoport {
2230f079694SMike Rapoport 	struct shmem_inode_info *info = SHMEM_I(inode);
2240f079694SMike Rapoport 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
2250f079694SMike Rapoport 
2260f079694SMike Rapoport 	if (sbinfo->max_blocks)
2270f079694SMike Rapoport 		percpu_counter_sub(&sbinfo->used_blocks, pages);
2280f079694SMike Rapoport 	shmem_unacct_blocks(info->flags, pages);
2290f079694SMike Rapoport }
2300f079694SMike Rapoport 
231759b9775SHugh Dickins static const struct super_operations shmem_ops;
232f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops;
23315ad7cdcSHelge Deller static const struct file_operations shmem_file_operations;
23492e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations;
23592e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations;
23692e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations;
237f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops;
238779750d2SKirill A. Shutemov static struct file_system_type shmem_fs_type;
2391da177e4SLinus Torvalds 
240b0506e48SMike Rapoport bool vma_is_shmem(struct vm_area_struct *vma)
241b0506e48SMike Rapoport {
242b0506e48SMike Rapoport 	return vma->vm_ops == &shmem_vm_ops;
243b0506e48SMike Rapoport }
244b0506e48SMike Rapoport 
2451da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist);
246cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex);
2471da177e4SLinus Torvalds 
2485b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb)
2495b04c689SPavel Emelyanov {
2505b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2515b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2525b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2535b04c689SPavel Emelyanov 		if (!sbinfo->free_inodes) {
2545b04c689SPavel Emelyanov 			spin_unlock(&sbinfo->stat_lock);
2555b04c689SPavel Emelyanov 			return -ENOSPC;
2565b04c689SPavel Emelyanov 		}
2575b04c689SPavel Emelyanov 		sbinfo->free_inodes--;
2585b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2595b04c689SPavel Emelyanov 	}
2605b04c689SPavel Emelyanov 	return 0;
2615b04c689SPavel Emelyanov }
2625b04c689SPavel Emelyanov 
2635b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb)
2645b04c689SPavel Emelyanov {
2655b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2665b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2675b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2685b04c689SPavel Emelyanov 		sbinfo->free_inodes++;
2695b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2705b04c689SPavel Emelyanov 	}
2715b04c689SPavel Emelyanov }
2725b04c689SPavel Emelyanov 
27346711810SRandy Dunlap /**
27441ffe5d5SHugh Dickins  * shmem_recalc_inode - recalculate the block usage of an inode
2751da177e4SLinus Torvalds  * @inode: inode to recalc
2761da177e4SLinus Torvalds  *
2771da177e4SLinus Torvalds  * We have to calculate the free blocks since the mm can drop
2781da177e4SLinus Torvalds  * undirtied hole pages behind our back.
2791da177e4SLinus Torvalds  *
2801da177e4SLinus Torvalds  * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
2811da177e4SLinus Torvalds  * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
2821da177e4SLinus Torvalds  *
2831da177e4SLinus Torvalds  * It has to be called with the spinlock held.
2841da177e4SLinus Torvalds  */
2851da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
2881da177e4SLinus Torvalds 	long freed;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
2911da177e4SLinus Torvalds 	if (freed > 0) {
2921da177e4SLinus Torvalds 		info->alloced -= freed;
29354af6042SHugh Dickins 		inode->i_blocks -= freed * BLOCKS_PER_PAGE;
2940f079694SMike Rapoport 		shmem_inode_unacct_blocks(inode, freed);
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
298800d8c63SKirill A. Shutemov bool shmem_charge(struct inode *inode, long pages)
299800d8c63SKirill A. Shutemov {
300800d8c63SKirill A. Shutemov 	struct shmem_inode_info *info = SHMEM_I(inode);
3014595ef88SKirill A. Shutemov 	unsigned long flags;
302800d8c63SKirill A. Shutemov 
3030f079694SMike Rapoport 	if (!shmem_inode_acct_block(inode, pages))
304800d8c63SKirill A. Shutemov 		return false;
305b1cc94abSMike Rapoport 
306aaa52e34SHugh Dickins 	/* nrpages adjustment first, then shmem_recalc_inode() when balanced */
307aaa52e34SHugh Dickins 	inode->i_mapping->nrpages += pages;
308aaa52e34SHugh Dickins 
3094595ef88SKirill A. Shutemov 	spin_lock_irqsave(&info->lock, flags);
310800d8c63SKirill A. Shutemov 	info->alloced += pages;
311800d8c63SKirill A. Shutemov 	inode->i_blocks += pages * BLOCKS_PER_PAGE;
312800d8c63SKirill A. Shutemov 	shmem_recalc_inode(inode);
3134595ef88SKirill A. Shutemov 	spin_unlock_irqrestore(&info->lock, flags);
314800d8c63SKirill A. Shutemov 
315800d8c63SKirill A. Shutemov 	return true;
316800d8c63SKirill A. Shutemov }
317800d8c63SKirill A. Shutemov 
318800d8c63SKirill A. Shutemov void shmem_uncharge(struct inode *inode, long pages)
319800d8c63SKirill A. Shutemov {
320800d8c63SKirill A. Shutemov 	struct shmem_inode_info *info = SHMEM_I(inode);
3214595ef88SKirill A. Shutemov 	unsigned long flags;
322800d8c63SKirill A. Shutemov 
323aaa52e34SHugh Dickins 	/* nrpages adjustment done by __delete_from_page_cache() or caller */
324aaa52e34SHugh Dickins 
3254595ef88SKirill A. Shutemov 	spin_lock_irqsave(&info->lock, flags);
326800d8c63SKirill A. Shutemov 	info->alloced -= pages;
327800d8c63SKirill A. Shutemov 	inode->i_blocks -= pages * BLOCKS_PER_PAGE;
328800d8c63SKirill A. Shutemov 	shmem_recalc_inode(inode);
3294595ef88SKirill A. Shutemov 	spin_unlock_irqrestore(&info->lock, flags);
330800d8c63SKirill A. Shutemov 
3310f079694SMike Rapoport 	shmem_inode_unacct_blocks(inode, pages);
332800d8c63SKirill A. Shutemov }
333800d8c63SKirill A. Shutemov 
3347a5d0fbbSHugh Dickins /*
33562f945b6SMatthew Wilcox  * Replace item expected in xarray by a new item, while holding xa_lock.
3367a5d0fbbSHugh Dickins  */
33762f945b6SMatthew Wilcox static int shmem_replace_entry(struct address_space *mapping,
3387a5d0fbbSHugh Dickins 			pgoff_t index, void *expected, void *replacement)
3397a5d0fbbSHugh Dickins {
34062f945b6SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, index);
3416dbaf22cSJohannes Weiner 	void *item;
3427a5d0fbbSHugh Dickins 
3437a5d0fbbSHugh Dickins 	VM_BUG_ON(!expected);
3446dbaf22cSJohannes Weiner 	VM_BUG_ON(!replacement);
34562f945b6SMatthew Wilcox 	item = xas_load(&xas);
3467a5d0fbbSHugh Dickins 	if (item != expected)
3477a5d0fbbSHugh Dickins 		return -ENOENT;
34862f945b6SMatthew Wilcox 	xas_store(&xas, replacement);
3497a5d0fbbSHugh Dickins 	return 0;
3507a5d0fbbSHugh Dickins }
3517a5d0fbbSHugh Dickins 
3527a5d0fbbSHugh Dickins /*
353d1899228SHugh Dickins  * Sometimes, before we decide whether to proceed or to fail, we must check
354d1899228SHugh Dickins  * that an entry was not already brought back from swap by a racing thread.
355d1899228SHugh Dickins  *
356d1899228SHugh Dickins  * Checking page is not enough: by the time a SwapCache page is locked, it
357d1899228SHugh Dickins  * might be reused, and again be SwapCache, using the same swap as before.
358d1899228SHugh Dickins  */
359d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping,
360d1899228SHugh Dickins 			       pgoff_t index, swp_entry_t swap)
361d1899228SHugh Dickins {
362a12831bfSMatthew Wilcox 	return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap);
363d1899228SHugh Dickins }
364d1899228SHugh Dickins 
365d1899228SHugh Dickins /*
3665a6e75f8SKirill A. Shutemov  * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
3675a6e75f8SKirill A. Shutemov  *
3685a6e75f8SKirill A. Shutemov  * SHMEM_HUGE_NEVER:
3695a6e75f8SKirill A. Shutemov  *	disables huge pages for the mount;
3705a6e75f8SKirill A. Shutemov  * SHMEM_HUGE_ALWAYS:
3715a6e75f8SKirill A. Shutemov  *	enables huge pages for the mount;
3725a6e75f8SKirill A. Shutemov  * SHMEM_HUGE_WITHIN_SIZE:
3735a6e75f8SKirill A. Shutemov  *	only allocate huge pages if the page will be fully within i_size,
3745a6e75f8SKirill A. Shutemov  *	also respect fadvise()/madvise() hints;
3755a6e75f8SKirill A. Shutemov  * SHMEM_HUGE_ADVISE:
3765a6e75f8SKirill A. Shutemov  *	only allocate huge pages if requested with fadvise()/madvise();
3775a6e75f8SKirill A. Shutemov  */
3785a6e75f8SKirill A. Shutemov 
3795a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_NEVER	0
3805a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ALWAYS	1
3815a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_WITHIN_SIZE	2
3825a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ADVISE	3
3835a6e75f8SKirill A. Shutemov 
3845a6e75f8SKirill A. Shutemov /*
3855a6e75f8SKirill A. Shutemov  * Special values.
3865a6e75f8SKirill A. Shutemov  * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
3875a6e75f8SKirill A. Shutemov  *
3885a6e75f8SKirill A. Shutemov  * SHMEM_HUGE_DENY:
3895a6e75f8SKirill A. Shutemov  *	disables huge on shm_mnt and all mounts, for emergency use;
3905a6e75f8SKirill A. Shutemov  * SHMEM_HUGE_FORCE:
3915a6e75f8SKirill A. Shutemov  *	enables huge on shm_mnt and all mounts, w/o needing option, for testing;
3925a6e75f8SKirill A. Shutemov  *
3935a6e75f8SKirill A. Shutemov  */
3945a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_DENY		(-1)
3955a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_FORCE	(-2)
3965a6e75f8SKirill A. Shutemov 
397e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3985a6e75f8SKirill A. Shutemov /* ifdef here to avoid bloating shmem.o when not necessary */
3995a6e75f8SKirill A. Shutemov 
4005b9c98f3SMike Kravetz static int shmem_huge __read_mostly;
4015a6e75f8SKirill A. Shutemov 
402f1f5929cSJérémy Lefaure #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
4035a6e75f8SKirill A. Shutemov static int shmem_parse_huge(const char *str)
4045a6e75f8SKirill A. Shutemov {
4055a6e75f8SKirill A. Shutemov 	if (!strcmp(str, "never"))
4065a6e75f8SKirill A. Shutemov 		return SHMEM_HUGE_NEVER;
4075a6e75f8SKirill A. Shutemov 	if (!strcmp(str, "always"))
4085a6e75f8SKirill A. Shutemov 		return SHMEM_HUGE_ALWAYS;
4095a6e75f8SKirill A. Shutemov 	if (!strcmp(str, "within_size"))
4105a6e75f8SKirill A. Shutemov 		return SHMEM_HUGE_WITHIN_SIZE;
4115a6e75f8SKirill A. Shutemov 	if (!strcmp(str, "advise"))
4125a6e75f8SKirill A. Shutemov 		return SHMEM_HUGE_ADVISE;
4135a6e75f8SKirill A. Shutemov 	if (!strcmp(str, "deny"))
4145a6e75f8SKirill A. Shutemov 		return SHMEM_HUGE_DENY;
4155a6e75f8SKirill A. Shutemov 	if (!strcmp(str, "force"))
4165a6e75f8SKirill A. Shutemov 		return SHMEM_HUGE_FORCE;
4175a6e75f8SKirill A. Shutemov 	return -EINVAL;
4185a6e75f8SKirill A. Shutemov }
4195a6e75f8SKirill A. Shutemov 
4205a6e75f8SKirill A. Shutemov static const char *shmem_format_huge(int huge)
4215a6e75f8SKirill A. Shutemov {
4225a6e75f8SKirill A. Shutemov 	switch (huge) {
4235a6e75f8SKirill A. Shutemov 	case SHMEM_HUGE_NEVER:
4245a6e75f8SKirill A. Shutemov 		return "never";
4255a6e75f8SKirill A. Shutemov 	case SHMEM_HUGE_ALWAYS:
4265a6e75f8SKirill A. Shutemov 		return "always";
4275a6e75f8SKirill A. Shutemov 	case SHMEM_HUGE_WITHIN_SIZE:
4285a6e75f8SKirill A. Shutemov 		return "within_size";
4295a6e75f8SKirill A. Shutemov 	case SHMEM_HUGE_ADVISE:
4305a6e75f8SKirill A. Shutemov 		return "advise";
4315a6e75f8SKirill A. Shutemov 	case SHMEM_HUGE_DENY:
4325a6e75f8SKirill A. Shutemov 		return "deny";
4335a6e75f8SKirill A. Shutemov 	case SHMEM_HUGE_FORCE:
4345a6e75f8SKirill A. Shutemov 		return "force";
4355a6e75f8SKirill A. Shutemov 	default:
4365a6e75f8SKirill A. Shutemov 		VM_BUG_ON(1);
4375a6e75f8SKirill A. Shutemov 		return "bad_val";
4385a6e75f8SKirill A. Shutemov 	}
4395a6e75f8SKirill A. Shutemov }
440f1f5929cSJérémy Lefaure #endif
4415a6e75f8SKirill A. Shutemov 
442779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
443779750d2SKirill A. Shutemov 		struct shrink_control *sc, unsigned long nr_to_split)
444779750d2SKirill A. Shutemov {
445779750d2SKirill A. Shutemov 	LIST_HEAD(list), *pos, *next;
446253fd0f0SKirill A. Shutemov 	LIST_HEAD(to_remove);
447779750d2SKirill A. Shutemov 	struct inode *inode;
448779750d2SKirill A. Shutemov 	struct shmem_inode_info *info;
449779750d2SKirill A. Shutemov 	struct page *page;
450779750d2SKirill A. Shutemov 	unsigned long batch = sc ? sc->nr_to_scan : 128;
451779750d2SKirill A. Shutemov 	int removed = 0, split = 0;
452779750d2SKirill A. Shutemov 
453779750d2SKirill A. Shutemov 	if (list_empty(&sbinfo->shrinklist))
454779750d2SKirill A. Shutemov 		return SHRINK_STOP;
455779750d2SKirill A. Shutemov 
456779750d2SKirill A. Shutemov 	spin_lock(&sbinfo->shrinklist_lock);
457779750d2SKirill A. Shutemov 	list_for_each_safe(pos, next, &sbinfo->shrinklist) {
458779750d2SKirill A. Shutemov 		info = list_entry(pos, struct shmem_inode_info, shrinklist);
459779750d2SKirill A. Shutemov 
460779750d2SKirill A. Shutemov 		/* pin the inode */
461779750d2SKirill A. Shutemov 		inode = igrab(&info->vfs_inode);
462779750d2SKirill A. Shutemov 
463779750d2SKirill A. Shutemov 		/* inode is about to be evicted */
464779750d2SKirill A. Shutemov 		if (!inode) {
465779750d2SKirill A. Shutemov 			list_del_init(&info->shrinklist);
466779750d2SKirill A. Shutemov 			removed++;
467779750d2SKirill A. Shutemov 			goto next;
468779750d2SKirill A. Shutemov 		}
469779750d2SKirill A. Shutemov 
470779750d2SKirill A. Shutemov 		/* Check if there's anything to gain */
471779750d2SKirill A. Shutemov 		if (round_up(inode->i_size, PAGE_SIZE) ==
472779750d2SKirill A. Shutemov 				round_up(inode->i_size, HPAGE_PMD_SIZE)) {
473253fd0f0SKirill A. Shutemov 			list_move(&info->shrinklist, &to_remove);
474779750d2SKirill A. Shutemov 			removed++;
475779750d2SKirill A. Shutemov 			goto next;
476779750d2SKirill A. Shutemov 		}
477779750d2SKirill A. Shutemov 
478779750d2SKirill A. Shutemov 		list_move(&info->shrinklist, &list);
479779750d2SKirill A. Shutemov next:
480779750d2SKirill A. Shutemov 		if (!--batch)
481779750d2SKirill A. Shutemov 			break;
482779750d2SKirill A. Shutemov 	}
483779750d2SKirill A. Shutemov 	spin_unlock(&sbinfo->shrinklist_lock);
484779750d2SKirill A. Shutemov 
485253fd0f0SKirill A. Shutemov 	list_for_each_safe(pos, next, &to_remove) {
486253fd0f0SKirill A. Shutemov 		info = list_entry(pos, struct shmem_inode_info, shrinklist);
487253fd0f0SKirill A. Shutemov 		inode = &info->vfs_inode;
488253fd0f0SKirill A. Shutemov 		list_del_init(&info->shrinklist);
489253fd0f0SKirill A. Shutemov 		iput(inode);
490253fd0f0SKirill A. Shutemov 	}
491253fd0f0SKirill A. Shutemov 
492779750d2SKirill A. Shutemov 	list_for_each_safe(pos, next, &list) {
493779750d2SKirill A. Shutemov 		int ret;
494779750d2SKirill A. Shutemov 
495779750d2SKirill A. Shutemov 		info = list_entry(pos, struct shmem_inode_info, shrinklist);
496779750d2SKirill A. Shutemov 		inode = &info->vfs_inode;
497779750d2SKirill A. Shutemov 
498b3cd54b2SKirill A. Shutemov 		if (nr_to_split && split >= nr_to_split)
499b3cd54b2SKirill A. Shutemov 			goto leave;
500779750d2SKirill A. Shutemov 
501b3cd54b2SKirill A. Shutemov 		page = find_get_page(inode->i_mapping,
502779750d2SKirill A. Shutemov 				(inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
503779750d2SKirill A. Shutemov 		if (!page)
504779750d2SKirill A. Shutemov 			goto drop;
505779750d2SKirill A. Shutemov 
506b3cd54b2SKirill A. Shutemov 		/* No huge page at the end of the file: nothing to split */
507779750d2SKirill A. Shutemov 		if (!PageTransHuge(page)) {
508779750d2SKirill A. Shutemov 			put_page(page);
509779750d2SKirill A. Shutemov 			goto drop;
510779750d2SKirill A. Shutemov 		}
511779750d2SKirill A. Shutemov 
512b3cd54b2SKirill A. Shutemov 		/*
513b3cd54b2SKirill A. Shutemov 		 * Leave the inode on the list if we failed to lock
514b3cd54b2SKirill A. Shutemov 		 * the page at this time.
515b3cd54b2SKirill A. Shutemov 		 *
516b3cd54b2SKirill A. Shutemov 		 * Waiting for the lock may lead to deadlock in the
517b3cd54b2SKirill A. Shutemov 		 * reclaim path.
518b3cd54b2SKirill A. Shutemov 		 */
519b3cd54b2SKirill A. Shutemov 		if (!trylock_page(page)) {
520b3cd54b2SKirill A. Shutemov 			put_page(page);
521b3cd54b2SKirill A. Shutemov 			goto leave;
522b3cd54b2SKirill A. Shutemov 		}
523b3cd54b2SKirill A. Shutemov 
524779750d2SKirill A. Shutemov 		ret = split_huge_page(page);
525779750d2SKirill A. Shutemov 		unlock_page(page);
526779750d2SKirill A. Shutemov 		put_page(page);
527779750d2SKirill A. Shutemov 
528b3cd54b2SKirill A. Shutemov 		/* If split failed leave the inode on the list */
529b3cd54b2SKirill A. Shutemov 		if (ret)
530b3cd54b2SKirill A. Shutemov 			goto leave;
531779750d2SKirill A. Shutemov 
532779750d2SKirill A. Shutemov 		split++;
533779750d2SKirill A. Shutemov drop:
534779750d2SKirill A. Shutemov 		list_del_init(&info->shrinklist);
535779750d2SKirill A. Shutemov 		removed++;
536b3cd54b2SKirill A. Shutemov leave:
537779750d2SKirill A. Shutemov 		iput(inode);
538779750d2SKirill A. Shutemov 	}
539779750d2SKirill A. Shutemov 
540779750d2SKirill A. Shutemov 	spin_lock(&sbinfo->shrinklist_lock);
541779750d2SKirill A. Shutemov 	list_splice_tail(&list, &sbinfo->shrinklist);
542779750d2SKirill A. Shutemov 	sbinfo->shrinklist_len -= removed;
543779750d2SKirill A. Shutemov 	spin_unlock(&sbinfo->shrinklist_lock);
544779750d2SKirill A. Shutemov 
545779750d2SKirill A. Shutemov 	return split;
546779750d2SKirill A. Shutemov }
547779750d2SKirill A. Shutemov 
548779750d2SKirill A. Shutemov static long shmem_unused_huge_scan(struct super_block *sb,
549779750d2SKirill A. Shutemov 		struct shrink_control *sc)
550779750d2SKirill A. Shutemov {
551779750d2SKirill A. Shutemov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
552779750d2SKirill A. Shutemov 
553779750d2SKirill A. Shutemov 	if (!READ_ONCE(sbinfo->shrinklist_len))
554779750d2SKirill A. Shutemov 		return SHRINK_STOP;
555779750d2SKirill A. Shutemov 
556779750d2SKirill A. Shutemov 	return shmem_unused_huge_shrink(sbinfo, sc, 0);
557779750d2SKirill A. Shutemov }
558779750d2SKirill A. Shutemov 
559779750d2SKirill A. Shutemov static long shmem_unused_huge_count(struct super_block *sb,
560779750d2SKirill A. Shutemov 		struct shrink_control *sc)
561779750d2SKirill A. Shutemov {
562779750d2SKirill A. Shutemov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
563779750d2SKirill A. Shutemov 	return READ_ONCE(sbinfo->shrinklist_len);
564779750d2SKirill A. Shutemov }
565e496cf3dSKirill A. Shutemov #else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */
5665a6e75f8SKirill A. Shutemov 
5675a6e75f8SKirill A. Shutemov #define shmem_huge SHMEM_HUGE_DENY
5685a6e75f8SKirill A. Shutemov 
569779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
570779750d2SKirill A. Shutemov 		struct shrink_control *sc, unsigned long nr_to_split)
571779750d2SKirill A. Shutemov {
572779750d2SKirill A. Shutemov 	return 0;
573779750d2SKirill A. Shutemov }
574e496cf3dSKirill A. Shutemov #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
5755a6e75f8SKirill A. Shutemov 
57689fdcd26SYang Shi static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo)
57789fdcd26SYang Shi {
57889fdcd26SYang Shi 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
57989fdcd26SYang Shi 	    (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) &&
58089fdcd26SYang Shi 	    shmem_huge != SHMEM_HUGE_DENY)
58189fdcd26SYang Shi 		return true;
58289fdcd26SYang Shi 	return false;
58389fdcd26SYang Shi }
58489fdcd26SYang Shi 
5855a6e75f8SKirill A. Shutemov /*
58646f65ec1SHugh Dickins  * Like add_to_page_cache_locked, but error if expected item has gone.
58746f65ec1SHugh Dickins  */
58846f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page,
58946f65ec1SHugh Dickins 				   struct address_space *mapping,
590552446a4SMatthew Wilcox 				   pgoff_t index, void *expected, gfp_t gfp)
59146f65ec1SHugh Dickins {
592552446a4SMatthew Wilcox 	XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page));
593552446a4SMatthew Wilcox 	unsigned long i = 0;
594552446a4SMatthew Wilcox 	unsigned long nr = 1UL << compound_order(page);
59546f65ec1SHugh Dickins 
596800d8c63SKirill A. Shutemov 	VM_BUG_ON_PAGE(PageTail(page), page);
597800d8c63SKirill A. Shutemov 	VM_BUG_ON_PAGE(index != round_down(index, nr), page);
598309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageLocked(page), page);
599309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
600800d8c63SKirill A. Shutemov 	VM_BUG_ON(expected && PageTransHuge(page));
60146f65ec1SHugh Dickins 
602800d8c63SKirill A. Shutemov 	page_ref_add(page, nr);
60346f65ec1SHugh Dickins 	page->mapping = mapping;
60446f65ec1SHugh Dickins 	page->index = index;
60546f65ec1SHugh Dickins 
606552446a4SMatthew Wilcox 	do {
607552446a4SMatthew Wilcox 		void *entry;
608552446a4SMatthew Wilcox 		xas_lock_irq(&xas);
609552446a4SMatthew Wilcox 		entry = xas_find_conflict(&xas);
610552446a4SMatthew Wilcox 		if (entry != expected)
611552446a4SMatthew Wilcox 			xas_set_err(&xas, -EEXIST);
612552446a4SMatthew Wilcox 		xas_create_range(&xas);
613552446a4SMatthew Wilcox 		if (xas_error(&xas))
614552446a4SMatthew Wilcox 			goto unlock;
615552446a4SMatthew Wilcox next:
616552446a4SMatthew Wilcox 		xas_store(&xas, page + i);
617552446a4SMatthew Wilcox 		if (++i < nr) {
618552446a4SMatthew Wilcox 			xas_next(&xas);
619552446a4SMatthew Wilcox 			goto next;
620552446a4SMatthew Wilcox 		}
621800d8c63SKirill A. Shutemov 		if (PageTransHuge(page)) {
622800d8c63SKirill A. Shutemov 			count_vm_event(THP_FILE_ALLOC);
62311fb9989SMel Gorman 			__inc_node_page_state(page, NR_SHMEM_THPS);
624552446a4SMatthew Wilcox 		}
625552446a4SMatthew Wilcox 		mapping->nrpages += nr;
62611fb9989SMel Gorman 		__mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
62711fb9989SMel Gorman 		__mod_node_page_state(page_pgdat(page), NR_SHMEM, nr);
628552446a4SMatthew Wilcox unlock:
629552446a4SMatthew Wilcox 		xas_unlock_irq(&xas);
630552446a4SMatthew Wilcox 	} while (xas_nomem(&xas, gfp));
631552446a4SMatthew Wilcox 
632552446a4SMatthew Wilcox 	if (xas_error(&xas)) {
63346f65ec1SHugh Dickins 		page->mapping = NULL;
634800d8c63SKirill A. Shutemov 		page_ref_sub(page, nr);
635552446a4SMatthew Wilcox 		return xas_error(&xas);
63646f65ec1SHugh Dickins 	}
637552446a4SMatthew Wilcox 
638552446a4SMatthew Wilcox 	return 0;
63946f65ec1SHugh Dickins }
64046f65ec1SHugh Dickins 
64146f65ec1SHugh Dickins /*
6426922c0c7SHugh Dickins  * Like delete_from_page_cache, but substitutes swap for page.
6436922c0c7SHugh Dickins  */
6446922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap)
6456922c0c7SHugh Dickins {
6466922c0c7SHugh Dickins 	struct address_space *mapping = page->mapping;
6476922c0c7SHugh Dickins 	int error;
6486922c0c7SHugh Dickins 
649800d8c63SKirill A. Shutemov 	VM_BUG_ON_PAGE(PageCompound(page), page);
650800d8c63SKirill A. Shutemov 
651b93b0163SMatthew Wilcox 	xa_lock_irq(&mapping->i_pages);
65262f945b6SMatthew Wilcox 	error = shmem_replace_entry(mapping, page->index, page, radswap);
6536922c0c7SHugh Dickins 	page->mapping = NULL;
6546922c0c7SHugh Dickins 	mapping->nrpages--;
65511fb9989SMel Gorman 	__dec_node_page_state(page, NR_FILE_PAGES);
65611fb9989SMel Gorman 	__dec_node_page_state(page, NR_SHMEM);
657b93b0163SMatthew Wilcox 	xa_unlock_irq(&mapping->i_pages);
65809cbfeafSKirill A. Shutemov 	put_page(page);
6596922c0c7SHugh Dickins 	BUG_ON(error);
6606922c0c7SHugh Dickins }
6616922c0c7SHugh Dickins 
6626922c0c7SHugh Dickins /*
663c121d3bbSMatthew Wilcox  * Remove swap entry from page cache, free the swap and its page cache.
6647a5d0fbbSHugh Dickins  */
6657a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping,
6667a5d0fbbSHugh Dickins 			   pgoff_t index, void *radswap)
6677a5d0fbbSHugh Dickins {
6686dbaf22cSJohannes Weiner 	void *old;
6697a5d0fbbSHugh Dickins 
67055f3f7eaSMatthew Wilcox 	old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0);
6716dbaf22cSJohannes Weiner 	if (old != radswap)
6726dbaf22cSJohannes Weiner 		return -ENOENT;
6737a5d0fbbSHugh Dickins 	free_swap_and_cache(radix_to_swp_entry(radswap));
6746dbaf22cSJohannes Weiner 	return 0;
6757a5d0fbbSHugh Dickins }
6767a5d0fbbSHugh Dickins 
6777a5d0fbbSHugh Dickins /*
6786a15a370SVlastimil Babka  * Determine (in bytes) how many of the shmem object's pages mapped by the
67948131e03SVlastimil Babka  * given offsets are swapped out.
6806a15a370SVlastimil Babka  *
681b93b0163SMatthew Wilcox  * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
6826a15a370SVlastimil Babka  * as long as the inode doesn't go away and racy results are not a problem.
6836a15a370SVlastimil Babka  */
68448131e03SVlastimil Babka unsigned long shmem_partial_swap_usage(struct address_space *mapping,
68548131e03SVlastimil Babka 						pgoff_t start, pgoff_t end)
6866a15a370SVlastimil Babka {
6877ae3424fSMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, start);
6886a15a370SVlastimil Babka 	struct page *page;
68948131e03SVlastimil Babka 	unsigned long swapped = 0;
6906a15a370SVlastimil Babka 
6916a15a370SVlastimil Babka 	rcu_read_lock();
6927ae3424fSMatthew Wilcox 	xas_for_each(&xas, page, end - 1) {
6937ae3424fSMatthew Wilcox 		if (xas_retry(&xas, page))
6942cf938aaSMatthew Wilcox 			continue;
6953159f943SMatthew Wilcox 		if (xa_is_value(page))
6966a15a370SVlastimil Babka 			swapped++;
6976a15a370SVlastimil Babka 
6986a15a370SVlastimil Babka 		if (need_resched()) {
6997ae3424fSMatthew Wilcox 			xas_pause(&xas);
7006a15a370SVlastimil Babka 			cond_resched_rcu();
7016a15a370SVlastimil Babka 		}
7026a15a370SVlastimil Babka 	}
7036a15a370SVlastimil Babka 
7046a15a370SVlastimil Babka 	rcu_read_unlock();
7056a15a370SVlastimil Babka 
7066a15a370SVlastimil Babka 	return swapped << PAGE_SHIFT;
7076a15a370SVlastimil Babka }
7086a15a370SVlastimil Babka 
7096a15a370SVlastimil Babka /*
71048131e03SVlastimil Babka  * Determine (in bytes) how many of the shmem object's pages mapped by the
71148131e03SVlastimil Babka  * given vma is swapped out.
71248131e03SVlastimil Babka  *
713b93b0163SMatthew Wilcox  * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
71448131e03SVlastimil Babka  * as long as the inode doesn't go away and racy results are not a problem.
71548131e03SVlastimil Babka  */
71648131e03SVlastimil Babka unsigned long shmem_swap_usage(struct vm_area_struct *vma)
71748131e03SVlastimil Babka {
71848131e03SVlastimil Babka 	struct inode *inode = file_inode(vma->vm_file);
71948131e03SVlastimil Babka 	struct shmem_inode_info *info = SHMEM_I(inode);
72048131e03SVlastimil Babka 	struct address_space *mapping = inode->i_mapping;
72148131e03SVlastimil Babka 	unsigned long swapped;
72248131e03SVlastimil Babka 
72348131e03SVlastimil Babka 	/* Be careful as we don't hold info->lock */
72448131e03SVlastimil Babka 	swapped = READ_ONCE(info->swapped);
72548131e03SVlastimil Babka 
72648131e03SVlastimil Babka 	/*
72748131e03SVlastimil Babka 	 * The easier cases are when the shmem object has nothing in swap, or
72848131e03SVlastimil Babka 	 * the vma maps it whole. Then we can simply use the stats that we
72948131e03SVlastimil Babka 	 * already track.
73048131e03SVlastimil Babka 	 */
73148131e03SVlastimil Babka 	if (!swapped)
73248131e03SVlastimil Babka 		return 0;
73348131e03SVlastimil Babka 
73448131e03SVlastimil Babka 	if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
73548131e03SVlastimil Babka 		return swapped << PAGE_SHIFT;
73648131e03SVlastimil Babka 
73748131e03SVlastimil Babka 	/* Here comes the more involved part */
73848131e03SVlastimil Babka 	return shmem_partial_swap_usage(mapping,
73948131e03SVlastimil Babka 			linear_page_index(vma, vma->vm_start),
74048131e03SVlastimil Babka 			linear_page_index(vma, vma->vm_end));
74148131e03SVlastimil Babka }
74248131e03SVlastimil Babka 
74348131e03SVlastimil Babka /*
74424513264SHugh Dickins  * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
74524513264SHugh Dickins  */
74624513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping)
74724513264SHugh Dickins {
74824513264SHugh Dickins 	struct pagevec pvec;
74924513264SHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
75024513264SHugh Dickins 	pgoff_t index = 0;
75124513264SHugh Dickins 
75286679820SMel Gorman 	pagevec_init(&pvec);
75324513264SHugh Dickins 	/*
75424513264SHugh Dickins 	 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
75524513264SHugh Dickins 	 */
75624513264SHugh Dickins 	while (!mapping_unevictable(mapping)) {
75724513264SHugh Dickins 		/*
75824513264SHugh Dickins 		 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
75924513264SHugh Dickins 		 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
76024513264SHugh Dickins 		 */
7610cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
76224513264SHugh Dickins 					   PAGEVEC_SIZE, pvec.pages, indices);
76324513264SHugh Dickins 		if (!pvec.nr)
76424513264SHugh Dickins 			break;
76524513264SHugh Dickins 		index = indices[pvec.nr - 1] + 1;
7660cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
76764e3d12fSKuo-Hsin Yang 		check_move_unevictable_pages(&pvec);
76824513264SHugh Dickins 		pagevec_release(&pvec);
76924513264SHugh Dickins 		cond_resched();
77024513264SHugh Dickins 	}
7717a5d0fbbSHugh Dickins }
7727a5d0fbbSHugh Dickins 
7737a5d0fbbSHugh Dickins /*
7747f4446eeSMatthew Wilcox  * Remove range of pages and swap entries from page cache, and free them.
7751635f6a7SHugh Dickins  * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
7767a5d0fbbSHugh Dickins  */
7771635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
7781635f6a7SHugh Dickins 								 bool unfalloc)
7791da177e4SLinus Torvalds {
780285b2c4fSHugh Dickins 	struct address_space *mapping = inode->i_mapping;
7811da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
78209cbfeafSKirill A. Shutemov 	pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
78309cbfeafSKirill A. Shutemov 	pgoff_t end = (lend + 1) >> PAGE_SHIFT;
78409cbfeafSKirill A. Shutemov 	unsigned int partial_start = lstart & (PAGE_SIZE - 1);
78509cbfeafSKirill A. Shutemov 	unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
786bda97eabSHugh Dickins 	struct pagevec pvec;
7877a5d0fbbSHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
7887a5d0fbbSHugh Dickins 	long nr_swaps_freed = 0;
789285b2c4fSHugh Dickins 	pgoff_t index;
790bda97eabSHugh Dickins 	int i;
7911da177e4SLinus Torvalds 
79283e4fa9cSHugh Dickins 	if (lend == -1)
79383e4fa9cSHugh Dickins 		end = -1;	/* unsigned, so actually very big */
794bda97eabSHugh Dickins 
79586679820SMel Gorman 	pagevec_init(&pvec);
796bda97eabSHugh Dickins 	index = start;
79783e4fa9cSHugh Dickins 	while (index < end) {
7980cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
79983e4fa9cSHugh Dickins 			min(end - index, (pgoff_t)PAGEVEC_SIZE),
8007a5d0fbbSHugh Dickins 			pvec.pages, indices);
8017a5d0fbbSHugh Dickins 		if (!pvec.nr)
8027a5d0fbbSHugh Dickins 			break;
803bda97eabSHugh Dickins 		for (i = 0; i < pagevec_count(&pvec); i++) {
804bda97eabSHugh Dickins 			struct page *page = pvec.pages[i];
805bda97eabSHugh Dickins 
8067a5d0fbbSHugh Dickins 			index = indices[i];
80783e4fa9cSHugh Dickins 			if (index >= end)
808bda97eabSHugh Dickins 				break;
809bda97eabSHugh Dickins 
8103159f943SMatthew Wilcox 			if (xa_is_value(page)) {
8111635f6a7SHugh Dickins 				if (unfalloc)
8121635f6a7SHugh Dickins 					continue;
8137a5d0fbbSHugh Dickins 				nr_swaps_freed += !shmem_free_swap(mapping,
8147a5d0fbbSHugh Dickins 								index, page);
8157a5d0fbbSHugh Dickins 				continue;
8167a5d0fbbSHugh Dickins 			}
8177a5d0fbbSHugh Dickins 
818800d8c63SKirill A. Shutemov 			VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
819800d8c63SKirill A. Shutemov 
820bda97eabSHugh Dickins 			if (!trylock_page(page))
821bda97eabSHugh Dickins 				continue;
822800d8c63SKirill A. Shutemov 
823800d8c63SKirill A. Shutemov 			if (PageTransTail(page)) {
824800d8c63SKirill A. Shutemov 				/* Middle of THP: zero out the page */
825800d8c63SKirill A. Shutemov 				clear_highpage(page);
826800d8c63SKirill A. Shutemov 				unlock_page(page);
827800d8c63SKirill A. Shutemov 				continue;
828800d8c63SKirill A. Shutemov 			} else if (PageTransHuge(page)) {
829800d8c63SKirill A. Shutemov 				if (index == round_down(end, HPAGE_PMD_NR)) {
830800d8c63SKirill A. Shutemov 					/*
831800d8c63SKirill A. Shutemov 					 * Range ends in the middle of THP:
832800d8c63SKirill A. Shutemov 					 * zero out the page
833800d8c63SKirill A. Shutemov 					 */
834800d8c63SKirill A. Shutemov 					clear_highpage(page);
835800d8c63SKirill A. Shutemov 					unlock_page(page);
836800d8c63SKirill A. Shutemov 					continue;
837800d8c63SKirill A. Shutemov 				}
838800d8c63SKirill A. Shutemov 				index += HPAGE_PMD_NR - 1;
839800d8c63SKirill A. Shutemov 				i += HPAGE_PMD_NR - 1;
840800d8c63SKirill A. Shutemov 			}
841800d8c63SKirill A. Shutemov 
8421635f6a7SHugh Dickins 			if (!unfalloc || !PageUptodate(page)) {
843800d8c63SKirill A. Shutemov 				VM_BUG_ON_PAGE(PageTail(page), page);
844800d8c63SKirill A. Shutemov 				if (page_mapping(page) == mapping) {
845309381feSSasha Levin 					VM_BUG_ON_PAGE(PageWriteback(page), page);
846bda97eabSHugh Dickins 					truncate_inode_page(mapping, page);
8477a5d0fbbSHugh Dickins 				}
8481635f6a7SHugh Dickins 			}
849bda97eabSHugh Dickins 			unlock_page(page);
850bda97eabSHugh Dickins 		}
8510cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
85224513264SHugh Dickins 		pagevec_release(&pvec);
853bda97eabSHugh Dickins 		cond_resched();
854bda97eabSHugh Dickins 		index++;
855bda97eabSHugh Dickins 	}
856bda97eabSHugh Dickins 
85783e4fa9cSHugh Dickins 	if (partial_start) {
858bda97eabSHugh Dickins 		struct page *page = NULL;
8599e18eb29SAndres Lagar-Cavilla 		shmem_getpage(inode, start - 1, &page, SGP_READ);
860bda97eabSHugh Dickins 		if (page) {
86109cbfeafSKirill A. Shutemov 			unsigned int top = PAGE_SIZE;
86283e4fa9cSHugh Dickins 			if (start > end) {
86383e4fa9cSHugh Dickins 				top = partial_end;
86483e4fa9cSHugh Dickins 				partial_end = 0;
86583e4fa9cSHugh Dickins 			}
86683e4fa9cSHugh Dickins 			zero_user_segment(page, partial_start, top);
867bda97eabSHugh Dickins 			set_page_dirty(page);
868bda97eabSHugh Dickins 			unlock_page(page);
86909cbfeafSKirill A. Shutemov 			put_page(page);
870bda97eabSHugh Dickins 		}
871bda97eabSHugh Dickins 	}
87283e4fa9cSHugh Dickins 	if (partial_end) {
87383e4fa9cSHugh Dickins 		struct page *page = NULL;
8749e18eb29SAndres Lagar-Cavilla 		shmem_getpage(inode, end, &page, SGP_READ);
87583e4fa9cSHugh Dickins 		if (page) {
87683e4fa9cSHugh Dickins 			zero_user_segment(page, 0, partial_end);
87783e4fa9cSHugh Dickins 			set_page_dirty(page);
87883e4fa9cSHugh Dickins 			unlock_page(page);
87909cbfeafSKirill A. Shutemov 			put_page(page);
88083e4fa9cSHugh Dickins 		}
88183e4fa9cSHugh Dickins 	}
88283e4fa9cSHugh Dickins 	if (start >= end)
88383e4fa9cSHugh Dickins 		return;
884bda97eabSHugh Dickins 
885bda97eabSHugh Dickins 	index = start;
886b1a36650SHugh Dickins 	while (index < end) {
887bda97eabSHugh Dickins 		cond_resched();
8880cd6144aSJohannes Weiner 
8890cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
89083e4fa9cSHugh Dickins 				min(end - index, (pgoff_t)PAGEVEC_SIZE),
8917a5d0fbbSHugh Dickins 				pvec.pages, indices);
8927a5d0fbbSHugh Dickins 		if (!pvec.nr) {
893b1a36650SHugh Dickins 			/* If all gone or hole-punch or unfalloc, we're done */
894b1a36650SHugh Dickins 			if (index == start || end != -1)
895bda97eabSHugh Dickins 				break;
896b1a36650SHugh Dickins 			/* But if truncating, restart to make sure all gone */
897bda97eabSHugh Dickins 			index = start;
898bda97eabSHugh Dickins 			continue;
899bda97eabSHugh Dickins 		}
900bda97eabSHugh Dickins 		for (i = 0; i < pagevec_count(&pvec); i++) {
901bda97eabSHugh Dickins 			struct page *page = pvec.pages[i];
902bda97eabSHugh Dickins 
9037a5d0fbbSHugh Dickins 			index = indices[i];
90483e4fa9cSHugh Dickins 			if (index >= end)
905bda97eabSHugh Dickins 				break;
906bda97eabSHugh Dickins 
9073159f943SMatthew Wilcox 			if (xa_is_value(page)) {
9081635f6a7SHugh Dickins 				if (unfalloc)
9091635f6a7SHugh Dickins 					continue;
910b1a36650SHugh Dickins 				if (shmem_free_swap(mapping, index, page)) {
911b1a36650SHugh Dickins 					/* Swap was replaced by page: retry */
912b1a36650SHugh Dickins 					index--;
913b1a36650SHugh Dickins 					break;
914b1a36650SHugh Dickins 				}
915b1a36650SHugh Dickins 				nr_swaps_freed++;
9167a5d0fbbSHugh Dickins 				continue;
9177a5d0fbbSHugh Dickins 			}
9187a5d0fbbSHugh Dickins 
919bda97eabSHugh Dickins 			lock_page(page);
920800d8c63SKirill A. Shutemov 
921800d8c63SKirill A. Shutemov 			if (PageTransTail(page)) {
922800d8c63SKirill A. Shutemov 				/* Middle of THP: zero out the page */
923800d8c63SKirill A. Shutemov 				clear_highpage(page);
924800d8c63SKirill A. Shutemov 				unlock_page(page);
925800d8c63SKirill A. Shutemov 				/*
926800d8c63SKirill A. Shutemov 				 * Partial thp truncate due 'start' in middle
927800d8c63SKirill A. Shutemov 				 * of THP: don't need to look on these pages
928800d8c63SKirill A. Shutemov 				 * again on !pvec.nr restart.
929800d8c63SKirill A. Shutemov 				 */
930800d8c63SKirill A. Shutemov 				if (index != round_down(end, HPAGE_PMD_NR))
931800d8c63SKirill A. Shutemov 					start++;
932800d8c63SKirill A. Shutemov 				continue;
933800d8c63SKirill A. Shutemov 			} else if (PageTransHuge(page)) {
934800d8c63SKirill A. Shutemov 				if (index == round_down(end, HPAGE_PMD_NR)) {
935800d8c63SKirill A. Shutemov 					/*
936800d8c63SKirill A. Shutemov 					 * Range ends in the middle of THP:
937800d8c63SKirill A. Shutemov 					 * zero out the page
938800d8c63SKirill A. Shutemov 					 */
939800d8c63SKirill A. Shutemov 					clear_highpage(page);
940800d8c63SKirill A. Shutemov 					unlock_page(page);
941800d8c63SKirill A. Shutemov 					continue;
942800d8c63SKirill A. Shutemov 				}
943800d8c63SKirill A. Shutemov 				index += HPAGE_PMD_NR - 1;
944800d8c63SKirill A. Shutemov 				i += HPAGE_PMD_NR - 1;
945800d8c63SKirill A. Shutemov 			}
946800d8c63SKirill A. Shutemov 
9471635f6a7SHugh Dickins 			if (!unfalloc || !PageUptodate(page)) {
948800d8c63SKirill A. Shutemov 				VM_BUG_ON_PAGE(PageTail(page), page);
949800d8c63SKirill A. Shutemov 				if (page_mapping(page) == mapping) {
950309381feSSasha Levin 					VM_BUG_ON_PAGE(PageWriteback(page), page);
951bda97eabSHugh Dickins 					truncate_inode_page(mapping, page);
952b1a36650SHugh Dickins 				} else {
953b1a36650SHugh Dickins 					/* Page was replaced by swap: retry */
954b1a36650SHugh Dickins 					unlock_page(page);
955b1a36650SHugh Dickins 					index--;
956b1a36650SHugh Dickins 					break;
9577a5d0fbbSHugh Dickins 				}
9581635f6a7SHugh Dickins 			}
959bda97eabSHugh Dickins 			unlock_page(page);
960bda97eabSHugh Dickins 		}
9610cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
96224513264SHugh Dickins 		pagevec_release(&pvec);
963bda97eabSHugh Dickins 		index++;
964bda97eabSHugh Dickins 	}
96594c1e62dSHugh Dickins 
9664595ef88SKirill A. Shutemov 	spin_lock_irq(&info->lock);
9677a5d0fbbSHugh Dickins 	info->swapped -= nr_swaps_freed;
9681da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
9694595ef88SKirill A. Shutemov 	spin_unlock_irq(&info->lock);
9701635f6a7SHugh Dickins }
9711da177e4SLinus Torvalds 
9721635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
9731635f6a7SHugh Dickins {
9741635f6a7SHugh Dickins 	shmem_undo_range(inode, lstart, lend, false);
975078cd827SDeepa Dinamani 	inode->i_ctime = inode->i_mtime = current_time(inode);
9761da177e4SLinus Torvalds }
97794c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
9781da177e4SLinus Torvalds 
979a528d35eSDavid Howells static int shmem_getattr(const struct path *path, struct kstat *stat,
980a528d35eSDavid Howells 			 u32 request_mask, unsigned int query_flags)
98144a30220SYu Zhao {
982a528d35eSDavid Howells 	struct inode *inode = path->dentry->d_inode;
98344a30220SYu Zhao 	struct shmem_inode_info *info = SHMEM_I(inode);
98489fdcd26SYang Shi 	struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb);
98544a30220SYu Zhao 
986d0424c42SHugh Dickins 	if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
9874595ef88SKirill A. Shutemov 		spin_lock_irq(&info->lock);
98844a30220SYu Zhao 		shmem_recalc_inode(inode);
9894595ef88SKirill A. Shutemov 		spin_unlock_irq(&info->lock);
990d0424c42SHugh Dickins 	}
99144a30220SYu Zhao 	generic_fillattr(inode, stat);
99289fdcd26SYang Shi 
99389fdcd26SYang Shi 	if (is_huge_enabled(sb_info))
99489fdcd26SYang Shi 		stat->blksize = HPAGE_PMD_SIZE;
99589fdcd26SYang Shi 
99644a30220SYu Zhao 	return 0;
99744a30220SYu Zhao }
99844a30220SYu Zhao 
99994c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
10001da177e4SLinus Torvalds {
100175c3cfa8SDavid Howells 	struct inode *inode = d_inode(dentry);
100240e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
1003779750d2SKirill A. Shutemov 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
10041da177e4SLinus Torvalds 	int error;
10051da177e4SLinus Torvalds 
100631051c85SJan Kara 	error = setattr_prepare(dentry, attr);
1007db78b877SChristoph Hellwig 	if (error)
1008db78b877SChristoph Hellwig 		return error;
1009db78b877SChristoph Hellwig 
101094c1e62dSHugh Dickins 	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
101194c1e62dSHugh Dickins 		loff_t oldsize = inode->i_size;
101294c1e62dSHugh Dickins 		loff_t newsize = attr->ia_size;
10133889e6e7Snpiggin@suse.de 
101440e041a2SDavid Herrmann 		/* protected by i_mutex */
101540e041a2SDavid Herrmann 		if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
101640e041a2SDavid Herrmann 		    (newsize > oldsize && (info->seals & F_SEAL_GROW)))
101740e041a2SDavid Herrmann 			return -EPERM;
101840e041a2SDavid Herrmann 
101994c1e62dSHugh Dickins 		if (newsize != oldsize) {
102077142517SKonstantin Khlebnikov 			error = shmem_reacct_size(SHMEM_I(inode)->flags,
102177142517SKonstantin Khlebnikov 					oldsize, newsize);
102277142517SKonstantin Khlebnikov 			if (error)
102377142517SKonstantin Khlebnikov 				return error;
102494c1e62dSHugh Dickins 			i_size_write(inode, newsize);
1025078cd827SDeepa Dinamani 			inode->i_ctime = inode->i_mtime = current_time(inode);
102694c1e62dSHugh Dickins 		}
1027afa2db2fSJosef Bacik 		if (newsize <= oldsize) {
102894c1e62dSHugh Dickins 			loff_t holebegin = round_up(newsize, PAGE_SIZE);
1029d0424c42SHugh Dickins 			if (oldsize > holebegin)
1030d0424c42SHugh Dickins 				unmap_mapping_range(inode->i_mapping,
1031d0424c42SHugh Dickins 							holebegin, 0, 1);
1032d0424c42SHugh Dickins 			if (info->alloced)
1033d0424c42SHugh Dickins 				shmem_truncate_range(inode,
1034d0424c42SHugh Dickins 							newsize, (loff_t)-1);
103594c1e62dSHugh Dickins 			/* unmap again to remove racily COWed private pages */
1036d0424c42SHugh Dickins 			if (oldsize > holebegin)
1037d0424c42SHugh Dickins 				unmap_mapping_range(inode->i_mapping,
1038d0424c42SHugh Dickins 							holebegin, 0, 1);
1039779750d2SKirill A. Shutemov 
1040779750d2SKirill A. Shutemov 			/*
1041779750d2SKirill A. Shutemov 			 * Part of the huge page can be beyond i_size: subject
1042779750d2SKirill A. Shutemov 			 * to shrink under memory pressure.
1043779750d2SKirill A. Shutemov 			 */
1044779750d2SKirill A. Shutemov 			if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
1045779750d2SKirill A. Shutemov 				spin_lock(&sbinfo->shrinklist_lock);
1046d041353dSCong Wang 				/*
1047d041353dSCong Wang 				 * _careful to defend against unlocked access to
1048d041353dSCong Wang 				 * ->shrink_list in shmem_unused_huge_shrink()
1049d041353dSCong Wang 				 */
1050d041353dSCong Wang 				if (list_empty_careful(&info->shrinklist)) {
1051779750d2SKirill A. Shutemov 					list_add_tail(&info->shrinklist,
1052779750d2SKirill A. Shutemov 							&sbinfo->shrinklist);
1053779750d2SKirill A. Shutemov 					sbinfo->shrinklist_len++;
1054779750d2SKirill A. Shutemov 				}
1055779750d2SKirill A. Shutemov 				spin_unlock(&sbinfo->shrinklist_lock);
1056779750d2SKirill A. Shutemov 			}
105794c1e62dSHugh Dickins 		}
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 
10606a1a90adSChristoph Hellwig 	setattr_copy(inode, attr);
1061db78b877SChristoph Hellwig 	if (attr->ia_valid & ATTR_MODE)
1062feda821eSChristoph Hellwig 		error = posix_acl_chmod(inode, inode->i_mode);
10631da177e4SLinus Torvalds 	return error;
10641da177e4SLinus Torvalds }
10651da177e4SLinus Torvalds 
10661f895f75SAl Viro static void shmem_evict_inode(struct inode *inode)
10671da177e4SLinus Torvalds {
10681da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
1069779750d2SKirill A. Shutemov 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
10701da177e4SLinus Torvalds 
10713889e6e7Snpiggin@suse.de 	if (inode->i_mapping->a_ops == &shmem_aops) {
10721da177e4SLinus Torvalds 		shmem_unacct_size(info->flags, inode->i_size);
10731da177e4SLinus Torvalds 		inode->i_size = 0;
10743889e6e7Snpiggin@suse.de 		shmem_truncate_range(inode, 0, (loff_t)-1);
1075779750d2SKirill A. Shutemov 		if (!list_empty(&info->shrinklist)) {
1076779750d2SKirill A. Shutemov 			spin_lock(&sbinfo->shrinklist_lock);
1077779750d2SKirill A. Shutemov 			if (!list_empty(&info->shrinklist)) {
1078779750d2SKirill A. Shutemov 				list_del_init(&info->shrinklist);
1079779750d2SKirill A. Shutemov 				sbinfo->shrinklist_len--;
1080779750d2SKirill A. Shutemov 			}
1081779750d2SKirill A. Shutemov 			spin_unlock(&sbinfo->shrinklist_lock);
1082779750d2SKirill A. Shutemov 		}
10831da177e4SLinus Torvalds 		if (!list_empty(&info->swaplist)) {
1084cb5f7b9aSHugh Dickins 			mutex_lock(&shmem_swaplist_mutex);
10851da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
1086cb5f7b9aSHugh Dickins 			mutex_unlock(&shmem_swaplist_mutex);
10871da177e4SLinus Torvalds 		}
10883ed47db3SAl Viro 	}
1089b09e0fa4SEric Paris 
109038f38657SAristeu Rozanski 	simple_xattrs_free(&info->xattrs);
10910f3c42f5SHugh Dickins 	WARN_ON(inode->i_blocks);
10925b04c689SPavel Emelyanov 	shmem_free_inode(inode->i_sb);
1093dbd5768fSJan Kara 	clear_inode(inode);
10941da177e4SLinus Torvalds }
10951da177e4SLinus Torvalds 
1096e21a2955SMatthew Wilcox static unsigned long find_swap_entry(struct xarray *xa, void *item)
1097478922e2SMatthew Wilcox {
1098e21a2955SMatthew Wilcox 	XA_STATE(xas, xa, 0);
1099478922e2SMatthew Wilcox 	unsigned int checked = 0;
1100e21a2955SMatthew Wilcox 	void *entry;
1101478922e2SMatthew Wilcox 
1102478922e2SMatthew Wilcox 	rcu_read_lock();
1103e21a2955SMatthew Wilcox 	xas_for_each(&xas, entry, ULONG_MAX) {
1104e21a2955SMatthew Wilcox 		if (xas_retry(&xas, entry))
11055b9c98f3SMike Kravetz 			continue;
1106e21a2955SMatthew Wilcox 		if (entry == item)
1107478922e2SMatthew Wilcox 			break;
1108478922e2SMatthew Wilcox 		checked++;
1109e21a2955SMatthew Wilcox 		if ((checked % XA_CHECK_SCHED) != 0)
1110478922e2SMatthew Wilcox 			continue;
1111e21a2955SMatthew Wilcox 		xas_pause(&xas);
1112478922e2SMatthew Wilcox 		cond_resched_rcu();
1113478922e2SMatthew Wilcox 	}
1114478922e2SMatthew Wilcox 	rcu_read_unlock();
1115e21a2955SMatthew Wilcox 
1116e21a2955SMatthew Wilcox 	return entry ? xas.xa_index : -1;
1117478922e2SMatthew Wilcox }
1118478922e2SMatthew Wilcox 
111946f65ec1SHugh Dickins /*
112046f65ec1SHugh Dickins  * If swap found in inode, free it and move page from swapcache to filecache.
112146f65ec1SHugh Dickins  */
112241ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info,
1123bde05d1cSHugh Dickins 			     swp_entry_t swap, struct page **pagep)
11241da177e4SLinus Torvalds {
1125285b2c4fSHugh Dickins 	struct address_space *mapping = info->vfs_inode.i_mapping;
112646f65ec1SHugh Dickins 	void *radswap;
112741ffe5d5SHugh Dickins 	pgoff_t index;
1128bde05d1cSHugh Dickins 	gfp_t gfp;
1129bde05d1cSHugh Dickins 	int error = 0;
11301da177e4SLinus Torvalds 
113146f65ec1SHugh Dickins 	radswap = swp_to_radix_entry(swap);
1132b93b0163SMatthew Wilcox 	index = find_swap_entry(&mapping->i_pages, radswap);
113346f65ec1SHugh Dickins 	if (index == -1)
113400501b53SJohannes Weiner 		return -EAGAIN;	/* tell shmem_unuse we found nothing */
11352e0e26c7SHugh Dickins 
11361b1b32f2SHugh Dickins 	/*
11371b1b32f2SHugh Dickins 	 * Move _head_ to start search for next from here.
11381f895f75SAl Viro 	 * But be careful: shmem_evict_inode checks list_empty without taking
11391b1b32f2SHugh Dickins 	 * mutex, and there's an instant in list_move_tail when info->swaplist
1140285b2c4fSHugh Dickins 	 * would appear empty, if it were the only one on shmem_swaplist.
11411b1b32f2SHugh Dickins 	 */
11421b1b32f2SHugh Dickins 	if (shmem_swaplist.next != &info->swaplist)
11432e0e26c7SHugh Dickins 		list_move_tail(&shmem_swaplist, &info->swaplist);
11442e0e26c7SHugh Dickins 
1145bde05d1cSHugh Dickins 	gfp = mapping_gfp_mask(mapping);
1146bde05d1cSHugh Dickins 	if (shmem_should_replace_page(*pagep, gfp)) {
1147bde05d1cSHugh Dickins 		mutex_unlock(&shmem_swaplist_mutex);
1148bde05d1cSHugh Dickins 		error = shmem_replace_page(pagep, gfp, info, index);
1149bde05d1cSHugh Dickins 		mutex_lock(&shmem_swaplist_mutex);
1150bde05d1cSHugh Dickins 		/*
1151bde05d1cSHugh Dickins 		 * We needed to drop mutex to make that restrictive page
11520142ef6cSHugh Dickins 		 * allocation, but the inode might have been freed while we
11530142ef6cSHugh Dickins 		 * dropped it: although a racing shmem_evict_inode() cannot
11547f4446eeSMatthew Wilcox 		 * complete without emptying the page cache, our page lock
11550142ef6cSHugh Dickins 		 * on this swapcache page is not enough to prevent that -
11560142ef6cSHugh Dickins 		 * free_swap_and_cache() of our swap entry will only
11577f4446eeSMatthew Wilcox 		 * trylock_page(), removing swap from page cache whatever.
11580142ef6cSHugh Dickins 		 *
11590142ef6cSHugh Dickins 		 * We must not proceed to shmem_add_to_page_cache() if the
11600142ef6cSHugh Dickins 		 * inode has been freed, but of course we cannot rely on
11610142ef6cSHugh Dickins 		 * inode or mapping or info to check that.  However, we can
11620142ef6cSHugh Dickins 		 * safely check if our swap entry is still in use (and here
11630142ef6cSHugh Dickins 		 * it can't have got reused for another page): if it's still
11640142ef6cSHugh Dickins 		 * in use, then the inode cannot have been freed yet, and we
11650142ef6cSHugh Dickins 		 * can safely proceed (if it's no longer in use, that tells
11660142ef6cSHugh Dickins 		 * nothing about the inode, but we don't need to unuse swap).
1167bde05d1cSHugh Dickins 		 */
1168bde05d1cSHugh Dickins 		if (!page_swapcount(*pagep))
1169bde05d1cSHugh Dickins 			error = -ENOENT;
1170bde05d1cSHugh Dickins 	}
1171bde05d1cSHugh Dickins 
1172d13d1443SKAMEZAWA Hiroyuki 	/*
1173778dd893SHugh Dickins 	 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
1174778dd893SHugh Dickins 	 * but also to hold up shmem_evict_inode(): so inode cannot be freed
1175778dd893SHugh Dickins 	 * beneath us (pagelock doesn't help until the page is in pagecache).
1176d13d1443SKAMEZAWA Hiroyuki 	 */
1177bde05d1cSHugh Dickins 	if (!error)
1178bde05d1cSHugh Dickins 		error = shmem_add_to_page_cache(*pagep, mapping, index,
1179552446a4SMatthew Wilcox 						radswap, gfp);
118048f170fbSHugh Dickins 	if (error != -ENOMEM) {
118146f65ec1SHugh Dickins 		/*
118246f65ec1SHugh Dickins 		 * Truncation and eviction use free_swap_and_cache(), which
118346f65ec1SHugh Dickins 		 * only does trylock page: if we raced, best clean up here.
118446f65ec1SHugh Dickins 		 */
1185bde05d1cSHugh Dickins 		delete_from_swap_cache(*pagep);
1186bde05d1cSHugh Dickins 		set_page_dirty(*pagep);
118746f65ec1SHugh Dickins 		if (!error) {
11884595ef88SKirill A. Shutemov 			spin_lock_irq(&info->lock);
1189285b2c4fSHugh Dickins 			info->swapped--;
11904595ef88SKirill A. Shutemov 			spin_unlock_irq(&info->lock);
119141ffe5d5SHugh Dickins 			swap_free(swap);
119246f65ec1SHugh Dickins 		}
11931da177e4SLinus Torvalds 	}
11942e0e26c7SHugh Dickins 	return error;
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds /*
119846f65ec1SHugh Dickins  * Search through swapped inodes to find and replace swap by page.
11991da177e4SLinus Torvalds  */
120041ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page)
12011da177e4SLinus Torvalds {
120241ffe5d5SHugh Dickins 	struct list_head *this, *next;
12031da177e4SLinus Torvalds 	struct shmem_inode_info *info;
120400501b53SJohannes Weiner 	struct mem_cgroup *memcg;
1205bde05d1cSHugh Dickins 	int error = 0;
1206bde05d1cSHugh Dickins 
1207bde05d1cSHugh Dickins 	/*
1208bde05d1cSHugh Dickins 	 * There's a faint possibility that swap page was replaced before
12090142ef6cSHugh Dickins 	 * caller locked it: caller will come back later with the right page.
1210bde05d1cSHugh Dickins 	 */
12110142ef6cSHugh Dickins 	if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
1212bde05d1cSHugh Dickins 		goto out;
1213778dd893SHugh Dickins 
1214778dd893SHugh Dickins 	/*
1215778dd893SHugh Dickins 	 * Charge page using GFP_KERNEL while we can wait, before taking
1216778dd893SHugh Dickins 	 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1217778dd893SHugh Dickins 	 * Charged back to the user (not to caller) when swap account is used.
1218778dd893SHugh Dickins 	 */
12192cf85583STejun Heo 	error = mem_cgroup_try_charge_delay(page, current->mm, GFP_KERNEL,
12202cf85583STejun Heo 					    &memcg, false);
1221778dd893SHugh Dickins 	if (error)
1222778dd893SHugh Dickins 		goto out;
12237f4446eeSMatthew Wilcox 	/* No memory allocation: swap entry occupies the slot for the page */
122400501b53SJohannes Weiner 	error = -EAGAIN;
12251da177e4SLinus Torvalds 
1226cb5f7b9aSHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
122741ffe5d5SHugh Dickins 	list_for_each_safe(this, next, &shmem_swaplist) {
122841ffe5d5SHugh Dickins 		info = list_entry(this, struct shmem_inode_info, swaplist);
1229285b2c4fSHugh Dickins 		if (info->swapped)
123000501b53SJohannes Weiner 			error = shmem_unuse_inode(info, swap, &page);
12316922c0c7SHugh Dickins 		else
12326922c0c7SHugh Dickins 			list_del_init(&info->swaplist);
1233cb5f7b9aSHugh Dickins 		cond_resched();
123400501b53SJohannes Weiner 		if (error != -EAGAIN)
1235778dd893SHugh Dickins 			break;
123600501b53SJohannes Weiner 		/* found nothing in this: move on to search the next */
12371da177e4SLinus Torvalds 	}
1238cb5f7b9aSHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
1239778dd893SHugh Dickins 
124000501b53SJohannes Weiner 	if (error) {
124100501b53SJohannes Weiner 		if (error != -ENOMEM)
124200501b53SJohannes Weiner 			error = 0;
1243f627c2f5SKirill A. Shutemov 		mem_cgroup_cancel_charge(page, memcg, false);
124400501b53SJohannes Weiner 	} else
1245f627c2f5SKirill A. Shutemov 		mem_cgroup_commit_charge(page, memcg, true, false);
1246778dd893SHugh Dickins out:
1247aaa46865SHugh Dickins 	unlock_page(page);
124809cbfeafSKirill A. Shutemov 	put_page(page);
1249778dd893SHugh Dickins 	return error;
12501da177e4SLinus Torvalds }
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds /*
12531da177e4SLinus Torvalds  * Move the page from the page cache to the swap cache.
12541da177e4SLinus Torvalds  */
12551da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc)
12561da177e4SLinus Torvalds {
12571da177e4SLinus Torvalds 	struct shmem_inode_info *info;
12581da177e4SLinus Torvalds 	struct address_space *mapping;
12591da177e4SLinus Torvalds 	struct inode *inode;
12606922c0c7SHugh Dickins 	swp_entry_t swap;
12616922c0c7SHugh Dickins 	pgoff_t index;
12621da177e4SLinus Torvalds 
1263800d8c63SKirill A. Shutemov 	VM_BUG_ON_PAGE(PageCompound(page), page);
12641da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
12651da177e4SLinus Torvalds 	mapping = page->mapping;
12661da177e4SLinus Torvalds 	index = page->index;
12671da177e4SLinus Torvalds 	inode = mapping->host;
12681da177e4SLinus Torvalds 	info = SHMEM_I(inode);
12691da177e4SLinus Torvalds 	if (info->flags & VM_LOCKED)
12701da177e4SLinus Torvalds 		goto redirty;
1271d9fe526aSHugh Dickins 	if (!total_swap_pages)
12721da177e4SLinus Torvalds 		goto redirty;
12731da177e4SLinus Torvalds 
1274d9fe526aSHugh Dickins 	/*
127597b713baSChristoph Hellwig 	 * Our capabilities prevent regular writeback or sync from ever calling
127697b713baSChristoph Hellwig 	 * shmem_writepage; but a stacking filesystem might use ->writepage of
127797b713baSChristoph Hellwig 	 * its underlying filesystem, in which case tmpfs should write out to
127897b713baSChristoph Hellwig 	 * swap only in response to memory pressure, and not for the writeback
127997b713baSChristoph Hellwig 	 * threads or sync.
1280d9fe526aSHugh Dickins 	 */
128148f170fbSHugh Dickins 	if (!wbc->for_reclaim) {
128248f170fbSHugh Dickins 		WARN_ON_ONCE(1);	/* Still happens? Tell us about it! */
128348f170fbSHugh Dickins 		goto redirty;
128448f170fbSHugh Dickins 	}
12851635f6a7SHugh Dickins 
12861635f6a7SHugh Dickins 	/*
12871635f6a7SHugh Dickins 	 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
12881635f6a7SHugh Dickins 	 * value into swapfile.c, the only way we can correctly account for a
12891635f6a7SHugh Dickins 	 * fallocated page arriving here is now to initialize it and write it.
12901aac1400SHugh Dickins 	 *
12911aac1400SHugh Dickins 	 * That's okay for a page already fallocated earlier, but if we have
12921aac1400SHugh Dickins 	 * not yet completed the fallocation, then (a) we want to keep track
12931aac1400SHugh Dickins 	 * of this page in case we have to undo it, and (b) it may not be a
12941aac1400SHugh Dickins 	 * good idea to continue anyway, once we're pushing into swap.  So
12951aac1400SHugh Dickins 	 * reactivate the page, and let shmem_fallocate() quit when too many.
12961635f6a7SHugh Dickins 	 */
12971635f6a7SHugh Dickins 	if (!PageUptodate(page)) {
12981aac1400SHugh Dickins 		if (inode->i_private) {
12991aac1400SHugh Dickins 			struct shmem_falloc *shmem_falloc;
13001aac1400SHugh Dickins 			spin_lock(&inode->i_lock);
13011aac1400SHugh Dickins 			shmem_falloc = inode->i_private;
13021aac1400SHugh Dickins 			if (shmem_falloc &&
13038e205f77SHugh Dickins 			    !shmem_falloc->waitq &&
13041aac1400SHugh Dickins 			    index >= shmem_falloc->start &&
13051aac1400SHugh Dickins 			    index < shmem_falloc->next)
13061aac1400SHugh Dickins 				shmem_falloc->nr_unswapped++;
13071aac1400SHugh Dickins 			else
13081aac1400SHugh Dickins 				shmem_falloc = NULL;
13091aac1400SHugh Dickins 			spin_unlock(&inode->i_lock);
13101aac1400SHugh Dickins 			if (shmem_falloc)
13111aac1400SHugh Dickins 				goto redirty;
13121aac1400SHugh Dickins 		}
13131635f6a7SHugh Dickins 		clear_highpage(page);
13141635f6a7SHugh Dickins 		flush_dcache_page(page);
13151635f6a7SHugh Dickins 		SetPageUptodate(page);
13161635f6a7SHugh Dickins 	}
13171635f6a7SHugh Dickins 
131838d8b4e6SHuang Ying 	swap = get_swap_page(page);
131948f170fbSHugh Dickins 	if (!swap.val)
132048f170fbSHugh Dickins 		goto redirty;
1321d9fe526aSHugh Dickins 
1322b1dea800SHugh Dickins 	/*
1323b1dea800SHugh Dickins 	 * Add inode to shmem_unuse()'s list of swapped-out inodes,
13246922c0c7SHugh Dickins 	 * if it's not already there.  Do it now before the page is
13256922c0c7SHugh Dickins 	 * moved to swap cache, when its pagelock no longer protects
1326b1dea800SHugh Dickins 	 * the inode from eviction.  But don't unlock the mutex until
13276922c0c7SHugh Dickins 	 * we've incremented swapped, because shmem_unuse_inode() will
13286922c0c7SHugh Dickins 	 * prune a !swapped inode from the swaplist under this mutex.
1329b1dea800SHugh Dickins 	 */
1330b1dea800SHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
133105bf86b4SHugh Dickins 	if (list_empty(&info->swaplist))
133205bf86b4SHugh Dickins 		list_add_tail(&info->swaplist, &shmem_swaplist);
1333b1dea800SHugh Dickins 
133448f170fbSHugh Dickins 	if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
13354595ef88SKirill A. Shutemov 		spin_lock_irq(&info->lock);
1336267a4c76SHugh Dickins 		shmem_recalc_inode(inode);
1337267a4c76SHugh Dickins 		info->swapped++;
13384595ef88SKirill A. Shutemov 		spin_unlock_irq(&info->lock);
1339267a4c76SHugh Dickins 
1340aaa46865SHugh Dickins 		swap_shmem_alloc(swap);
13416922c0c7SHugh Dickins 		shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
13426922c0c7SHugh Dickins 
13436922c0c7SHugh Dickins 		mutex_unlock(&shmem_swaplist_mutex);
1344d9fe526aSHugh Dickins 		BUG_ON(page_mapped(page));
13459fab5619SHugh Dickins 		swap_writepage(page, wbc);
13461da177e4SLinus Torvalds 		return 0;
13471da177e4SLinus Torvalds 	}
13481da177e4SLinus Torvalds 
13496922c0c7SHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
135075f6d6d2SMinchan Kim 	put_swap_page(page, swap);
13511da177e4SLinus Torvalds redirty:
13521da177e4SLinus Torvalds 	set_page_dirty(page);
1353d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
1354d9fe526aSHugh Dickins 		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
1355d9fe526aSHugh Dickins 	unlock_page(page);
1356d9fe526aSHugh Dickins 	return 0;
13571da177e4SLinus Torvalds }
13581da177e4SLinus Torvalds 
135975edd345SHugh Dickins #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
136071fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1361680d794bSakpm@linux-foundation.org {
1362680d794bSakpm@linux-foundation.org 	char buffer[64];
1363680d794bSakpm@linux-foundation.org 
136471fe804bSLee Schermerhorn 	if (!mpol || mpol->mode == MPOL_DEFAULT)
1365095f1fc4SLee Schermerhorn 		return;		/* show nothing */
1366095f1fc4SLee Schermerhorn 
1367a7a88b23SHugh Dickins 	mpol_to_str(buffer, sizeof(buffer), mpol);
1368095f1fc4SLee Schermerhorn 
1369095f1fc4SLee Schermerhorn 	seq_printf(seq, ",mpol=%s", buffer);
1370680d794bSakpm@linux-foundation.org }
137171fe804bSLee Schermerhorn 
137271fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
137371fe804bSLee Schermerhorn {
137471fe804bSLee Schermerhorn 	struct mempolicy *mpol = NULL;
137571fe804bSLee Schermerhorn 	if (sbinfo->mpol) {
137671fe804bSLee Schermerhorn 		spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
137771fe804bSLee Schermerhorn 		mpol = sbinfo->mpol;
137871fe804bSLee Schermerhorn 		mpol_get(mpol);
137971fe804bSLee Schermerhorn 		spin_unlock(&sbinfo->stat_lock);
138071fe804bSLee Schermerhorn 	}
138171fe804bSLee Schermerhorn 	return mpol;
138271fe804bSLee Schermerhorn }
138375edd345SHugh Dickins #else /* !CONFIG_NUMA || !CONFIG_TMPFS */
138475edd345SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
138575edd345SHugh Dickins {
138675edd345SHugh Dickins }
138775edd345SHugh Dickins static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
138875edd345SHugh Dickins {
138975edd345SHugh Dickins 	return NULL;
139075edd345SHugh Dickins }
139175edd345SHugh Dickins #endif /* CONFIG_NUMA && CONFIG_TMPFS */
139275edd345SHugh Dickins #ifndef CONFIG_NUMA
139375edd345SHugh Dickins #define vm_policy vm_private_data
139475edd345SHugh Dickins #endif
1395680d794bSakpm@linux-foundation.org 
1396800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1397800d8c63SKirill A. Shutemov 		struct shmem_inode_info *info, pgoff_t index)
1398800d8c63SKirill A. Shutemov {
1399800d8c63SKirill A. Shutemov 	/* Create a pseudo vma that just contains the policy */
14002c4541e2SKirill A. Shutemov 	vma_init(vma, NULL);
1401800d8c63SKirill A. Shutemov 	/* Bias interleave by inode number to distribute better across nodes */
1402800d8c63SKirill A. Shutemov 	vma->vm_pgoff = index + info->vfs_inode.i_ino;
1403800d8c63SKirill A. Shutemov 	vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1404800d8c63SKirill A. Shutemov }
1405800d8c63SKirill A. Shutemov 
1406800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1407800d8c63SKirill A. Shutemov {
1408800d8c63SKirill A. Shutemov 	/* Drop reference taken by mpol_shared_policy_lookup() */
1409800d8c63SKirill A. Shutemov 	mpol_cond_put(vma->vm_policy);
1410800d8c63SKirill A. Shutemov }
1411800d8c63SKirill A. Shutemov 
141241ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
141341ffe5d5SHugh Dickins 			struct shmem_inode_info *info, pgoff_t index)
14141da177e4SLinus Torvalds {
14151da177e4SLinus Torvalds 	struct vm_area_struct pvma;
141618a2f371SMel Gorman 	struct page *page;
1417e9e9b7ecSMinchan Kim 	struct vm_fault vmf;
14181da177e4SLinus Torvalds 
1419800d8c63SKirill A. Shutemov 	shmem_pseudo_vma_init(&pvma, info, index);
1420e9e9b7ecSMinchan Kim 	vmf.vma = &pvma;
1421e9e9b7ecSMinchan Kim 	vmf.address = 0;
1422e9e9b7ecSMinchan Kim 	page = swap_cluster_readahead(swap, gfp, &vmf);
1423800d8c63SKirill A. Shutemov 	shmem_pseudo_vma_destroy(&pvma);
142418a2f371SMel Gorman 
1425800d8c63SKirill A. Shutemov 	return page;
1426800d8c63SKirill A. Shutemov }
142718a2f371SMel Gorman 
1428800d8c63SKirill A. Shutemov static struct page *shmem_alloc_hugepage(gfp_t gfp,
1429800d8c63SKirill A. Shutemov 		struct shmem_inode_info *info, pgoff_t index)
1430800d8c63SKirill A. Shutemov {
1431800d8c63SKirill A. Shutemov 	struct vm_area_struct pvma;
14327b8d046fSMatthew Wilcox 	struct address_space *mapping = info->vfs_inode.i_mapping;
14337b8d046fSMatthew Wilcox 	pgoff_t hindex;
1434800d8c63SKirill A. Shutemov 	struct page *page;
1435800d8c63SKirill A. Shutemov 
1436e496cf3dSKirill A. Shutemov 	if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
1437800d8c63SKirill A. Shutemov 		return NULL;
1438800d8c63SKirill A. Shutemov 
14394620a06eSGeert Uytterhoeven 	hindex = round_down(index, HPAGE_PMD_NR);
14407b8d046fSMatthew Wilcox 	if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1,
14417b8d046fSMatthew Wilcox 								XA_PRESENT))
1442800d8c63SKirill A. Shutemov 		return NULL;
1443800d8c63SKirill A. Shutemov 
1444800d8c63SKirill A. Shutemov 	shmem_pseudo_vma_init(&pvma, info, hindex);
1445800d8c63SKirill A. Shutemov 	page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
1446356ff8a9SDavid Rientjes 			HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
1447800d8c63SKirill A. Shutemov 	shmem_pseudo_vma_destroy(&pvma);
1448800d8c63SKirill A. Shutemov 	if (page)
1449800d8c63SKirill A. Shutemov 		prep_transhuge_page(page);
145018a2f371SMel Gorman 	return page;
145118a2f371SMel Gorman }
145218a2f371SMel Gorman 
145318a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp,
145418a2f371SMel Gorman 			struct shmem_inode_info *info, pgoff_t index)
145518a2f371SMel Gorman {
145618a2f371SMel Gorman 	struct vm_area_struct pvma;
145718a2f371SMel Gorman 	struct page *page;
145818a2f371SMel Gorman 
1459800d8c63SKirill A. Shutemov 	shmem_pseudo_vma_init(&pvma, info, index);
1460800d8c63SKirill A. Shutemov 	page = alloc_page_vma(gfp, &pvma, 0);
1461800d8c63SKirill A. Shutemov 	shmem_pseudo_vma_destroy(&pvma);
146218a2f371SMel Gorman 
1463800d8c63SKirill A. Shutemov 	return page;
1464800d8c63SKirill A. Shutemov }
1465800d8c63SKirill A. Shutemov 
1466800d8c63SKirill A. Shutemov static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
14670f079694SMike Rapoport 		struct inode *inode,
1468800d8c63SKirill A. Shutemov 		pgoff_t index, bool huge)
1469800d8c63SKirill A. Shutemov {
14700f079694SMike Rapoport 	struct shmem_inode_info *info = SHMEM_I(inode);
1471800d8c63SKirill A. Shutemov 	struct page *page;
1472800d8c63SKirill A. Shutemov 	int nr;
1473800d8c63SKirill A. Shutemov 	int err = -ENOSPC;
1474800d8c63SKirill A. Shutemov 
1475e496cf3dSKirill A. Shutemov 	if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
1476800d8c63SKirill A. Shutemov 		huge = false;
1477800d8c63SKirill A. Shutemov 	nr = huge ? HPAGE_PMD_NR : 1;
1478800d8c63SKirill A. Shutemov 
14790f079694SMike Rapoport 	if (!shmem_inode_acct_block(inode, nr))
1480800d8c63SKirill A. Shutemov 		goto failed;
1481800d8c63SKirill A. Shutemov 
1482800d8c63SKirill A. Shutemov 	if (huge)
1483800d8c63SKirill A. Shutemov 		page = shmem_alloc_hugepage(gfp, info, index);
1484800d8c63SKirill A. Shutemov 	else
1485800d8c63SKirill A. Shutemov 		page = shmem_alloc_page(gfp, info, index);
148675edd345SHugh Dickins 	if (page) {
148775edd345SHugh Dickins 		__SetPageLocked(page);
148875edd345SHugh Dickins 		__SetPageSwapBacked(page);
1489800d8c63SKirill A. Shutemov 		return page;
149075edd345SHugh Dickins 	}
149118a2f371SMel Gorman 
1492800d8c63SKirill A. Shutemov 	err = -ENOMEM;
14930f079694SMike Rapoport 	shmem_inode_unacct_blocks(inode, nr);
1494800d8c63SKirill A. Shutemov failed:
1495800d8c63SKirill A. Shutemov 	return ERR_PTR(err);
14961da177e4SLinus Torvalds }
149771fe804bSLee Schermerhorn 
14981da177e4SLinus Torvalds /*
1499bde05d1cSHugh Dickins  * When a page is moved from swapcache to shmem filecache (either by the
1500bde05d1cSHugh Dickins  * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1501bde05d1cSHugh Dickins  * shmem_unuse_inode()), it may have been read in earlier from swap, in
1502bde05d1cSHugh Dickins  * ignorance of the mapping it belongs to.  If that mapping has special
1503bde05d1cSHugh Dickins  * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1504bde05d1cSHugh Dickins  * we may need to copy to a suitable page before moving to filecache.
1505bde05d1cSHugh Dickins  *
1506bde05d1cSHugh Dickins  * In a future release, this may well be extended to respect cpuset and
1507bde05d1cSHugh Dickins  * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1508bde05d1cSHugh Dickins  * but for now it is a simple matter of zone.
1509bde05d1cSHugh Dickins  */
1510bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1511bde05d1cSHugh Dickins {
1512bde05d1cSHugh Dickins 	return page_zonenum(page) > gfp_zone(gfp);
1513bde05d1cSHugh Dickins }
1514bde05d1cSHugh Dickins 
1515bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1516bde05d1cSHugh Dickins 				struct shmem_inode_info *info, pgoff_t index)
1517bde05d1cSHugh Dickins {
1518bde05d1cSHugh Dickins 	struct page *oldpage, *newpage;
1519bde05d1cSHugh Dickins 	struct address_space *swap_mapping;
1520c1cb20d4SYu Zhao 	swp_entry_t entry;
1521bde05d1cSHugh Dickins 	pgoff_t swap_index;
1522bde05d1cSHugh Dickins 	int error;
1523bde05d1cSHugh Dickins 
1524bde05d1cSHugh Dickins 	oldpage = *pagep;
1525c1cb20d4SYu Zhao 	entry.val = page_private(oldpage);
1526c1cb20d4SYu Zhao 	swap_index = swp_offset(entry);
1527bde05d1cSHugh Dickins 	swap_mapping = page_mapping(oldpage);
1528bde05d1cSHugh Dickins 
1529bde05d1cSHugh Dickins 	/*
1530bde05d1cSHugh Dickins 	 * We have arrived here because our zones are constrained, so don't
1531bde05d1cSHugh Dickins 	 * limit chance of success by further cpuset and node constraints.
1532bde05d1cSHugh Dickins 	 */
1533bde05d1cSHugh Dickins 	gfp &= ~GFP_CONSTRAINT_MASK;
1534bde05d1cSHugh Dickins 	newpage = shmem_alloc_page(gfp, info, index);
1535bde05d1cSHugh Dickins 	if (!newpage)
1536bde05d1cSHugh Dickins 		return -ENOMEM;
1537bde05d1cSHugh Dickins 
153809cbfeafSKirill A. Shutemov 	get_page(newpage);
1539bde05d1cSHugh Dickins 	copy_highpage(newpage, oldpage);
15400142ef6cSHugh Dickins 	flush_dcache_page(newpage);
1541bde05d1cSHugh Dickins 
15429956edf3SHugh Dickins 	__SetPageLocked(newpage);
15439956edf3SHugh Dickins 	__SetPageSwapBacked(newpage);
1544bde05d1cSHugh Dickins 	SetPageUptodate(newpage);
1545c1cb20d4SYu Zhao 	set_page_private(newpage, entry.val);
1546bde05d1cSHugh Dickins 	SetPageSwapCache(newpage);
1547bde05d1cSHugh Dickins 
1548bde05d1cSHugh Dickins 	/*
1549bde05d1cSHugh Dickins 	 * Our caller will very soon move newpage out of swapcache, but it's
1550bde05d1cSHugh Dickins 	 * a nice clean interface for us to replace oldpage by newpage there.
1551bde05d1cSHugh Dickins 	 */
1552b93b0163SMatthew Wilcox 	xa_lock_irq(&swap_mapping->i_pages);
155362f945b6SMatthew Wilcox 	error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage);
15540142ef6cSHugh Dickins 	if (!error) {
155511fb9989SMel Gorman 		__inc_node_page_state(newpage, NR_FILE_PAGES);
155611fb9989SMel Gorman 		__dec_node_page_state(oldpage, NR_FILE_PAGES);
15570142ef6cSHugh Dickins 	}
1558b93b0163SMatthew Wilcox 	xa_unlock_irq(&swap_mapping->i_pages);
1559bde05d1cSHugh Dickins 
15600142ef6cSHugh Dickins 	if (unlikely(error)) {
15610142ef6cSHugh Dickins 		/*
15620142ef6cSHugh Dickins 		 * Is this possible?  I think not, now that our callers check
15630142ef6cSHugh Dickins 		 * both PageSwapCache and page_private after getting page lock;
15640142ef6cSHugh Dickins 		 * but be defensive.  Reverse old to newpage for clear and free.
15650142ef6cSHugh Dickins 		 */
15660142ef6cSHugh Dickins 		oldpage = newpage;
15670142ef6cSHugh Dickins 	} else {
15686a93ca8fSJohannes Weiner 		mem_cgroup_migrate(oldpage, newpage);
1569bde05d1cSHugh Dickins 		lru_cache_add_anon(newpage);
15700142ef6cSHugh Dickins 		*pagep = newpage;
15710142ef6cSHugh Dickins 	}
1572bde05d1cSHugh Dickins 
1573bde05d1cSHugh Dickins 	ClearPageSwapCache(oldpage);
1574bde05d1cSHugh Dickins 	set_page_private(oldpage, 0);
1575bde05d1cSHugh Dickins 
1576bde05d1cSHugh Dickins 	unlock_page(oldpage);
157709cbfeafSKirill A. Shutemov 	put_page(oldpage);
157809cbfeafSKirill A. Shutemov 	put_page(oldpage);
15790142ef6cSHugh Dickins 	return error;
1580bde05d1cSHugh Dickins }
1581bde05d1cSHugh Dickins 
1582bde05d1cSHugh Dickins /*
1583*c5bf121eSVineeth Remanan Pillai  * Swap in the page pointed to by *pagep.
1584*c5bf121eSVineeth Remanan Pillai  * Caller has to make sure that *pagep contains a valid swapped page.
1585*c5bf121eSVineeth Remanan Pillai  * Returns 0 and the page in pagep if success. On failure, returns the
1586*c5bf121eSVineeth Remanan Pillai  * the error code and NULL in *pagep.
15871da177e4SLinus Torvalds  */
1588*c5bf121eSVineeth Remanan Pillai static int shmem_swapin_page(struct inode *inode, pgoff_t index,
1589*c5bf121eSVineeth Remanan Pillai 			     struct page **pagep, enum sgp_type sgp,
1590*c5bf121eSVineeth Remanan Pillai 			     gfp_t gfp, struct vm_area_struct *vma,
15912b740303SSouptick Joarder 			     vm_fault_t *fault_type)
15921da177e4SLinus Torvalds {
15931da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
159423f919d4SArnd Bergmann 	struct shmem_inode_info *info = SHMEM_I(inode);
1595*c5bf121eSVineeth Remanan Pillai 	struct mm_struct *charge_mm = vma ? vma->vm_mm : current->mm;
159600501b53SJohannes Weiner 	struct mem_cgroup *memcg;
159727ab7006SHugh Dickins 	struct page *page;
15981da177e4SLinus Torvalds 	swp_entry_t swap;
15991da177e4SLinus Torvalds 	int error;
16001da177e4SLinus Torvalds 
1601*c5bf121eSVineeth Remanan Pillai 	VM_BUG_ON(!*pagep || !xa_is_value(*pagep));
1602*c5bf121eSVineeth Remanan Pillai 	swap = radix_to_swp_entry(*pagep);
1603*c5bf121eSVineeth Remanan Pillai 	*pagep = NULL;
160454af6042SHugh Dickins 
16051da177e4SLinus Torvalds 	/* Look it up and read it in.. */
1606ec560175SHuang Ying 	page = lookup_swap_cache(swap, NULL, 0);
160727ab7006SHugh Dickins 	if (!page) {
16089e18eb29SAndres Lagar-Cavilla 		/* Or update major stats only when swapin succeeds?? */
16099e18eb29SAndres Lagar-Cavilla 		if (fault_type) {
161068da9f05SHugh Dickins 			*fault_type |= VM_FAULT_MAJOR;
16119e18eb29SAndres Lagar-Cavilla 			count_vm_event(PGMAJFAULT);
16122262185cSRoman Gushchin 			count_memcg_event_mm(charge_mm, PGMAJFAULT);
16139e18eb29SAndres Lagar-Cavilla 		}
16149e18eb29SAndres Lagar-Cavilla 		/* Here we actually start the io */
161541ffe5d5SHugh Dickins 		page = shmem_swapin(swap, gfp, info, index);
161627ab7006SHugh Dickins 		if (!page) {
16171da177e4SLinus Torvalds 			error = -ENOMEM;
161854af6042SHugh Dickins 			goto failed;
1619285b2c4fSHugh Dickins 		}
16201da177e4SLinus Torvalds 	}
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 	/* We have to do this with page locked to prevent races */
162354af6042SHugh Dickins 	lock_page(page);
16240142ef6cSHugh Dickins 	if (!PageSwapCache(page) || page_private(page) != swap.val ||
1625d1899228SHugh Dickins 	    !shmem_confirm_swap(mapping, index, swap)) {
1626*c5bf121eSVineeth Remanan Pillai 		error = -EEXIST;
1627d1899228SHugh Dickins 		goto unlock;
1628bde05d1cSHugh Dickins 	}
162927ab7006SHugh Dickins 	if (!PageUptodate(page)) {
16301da177e4SLinus Torvalds 		error = -EIO;
163154af6042SHugh Dickins 		goto failed;
163254af6042SHugh Dickins 	}
163354af6042SHugh Dickins 	wait_on_page_writeback(page);
163454af6042SHugh Dickins 
1635bde05d1cSHugh Dickins 	if (shmem_should_replace_page(page, gfp)) {
1636bde05d1cSHugh Dickins 		error = shmem_replace_page(&page, gfp, info, index);
1637bde05d1cSHugh Dickins 		if (error)
163854af6042SHugh Dickins 			goto failed;
16391da177e4SLinus Torvalds 	}
16401da177e4SLinus Torvalds 
16412cf85583STejun Heo 	error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
1642f627c2f5SKirill A. Shutemov 					    false);
1643d1899228SHugh Dickins 	if (!error) {
164454af6042SHugh Dickins 		error = shmem_add_to_page_cache(page, mapping, index,
1645552446a4SMatthew Wilcox 						swp_to_radix_entry(swap), gfp);
1646215c02bcSHugh Dickins 		/*
1647215c02bcSHugh Dickins 		 * We already confirmed swap under page lock, and make
1648215c02bcSHugh Dickins 		 * no memory allocation here, so usually no possibility
1649215c02bcSHugh Dickins 		 * of error; but free_swap_and_cache() only trylocks a
1650215c02bcSHugh Dickins 		 * page, so it is just possible that the entry has been
1651215c02bcSHugh Dickins 		 * truncated or holepunched since swap was confirmed.
1652215c02bcSHugh Dickins 		 * shmem_undo_range() will have done some of the
1653215c02bcSHugh Dickins 		 * unaccounting, now delete_from_swap_cache() will do
165493aa7d95SVladimir Davydov 		 * the rest.
1655215c02bcSHugh Dickins 		 */
165600501b53SJohannes Weiner 		if (error) {
1657f627c2f5SKirill A. Shutemov 			mem_cgroup_cancel_charge(page, memcg, false);
1658215c02bcSHugh Dickins 			delete_from_swap_cache(page);
1659d1899228SHugh Dickins 		}
166000501b53SJohannes Weiner 	}
166154af6042SHugh Dickins 	if (error)
166254af6042SHugh Dickins 		goto failed;
166354af6042SHugh Dickins 
1664f627c2f5SKirill A. Shutemov 	mem_cgroup_commit_charge(page, memcg, true, false);
166500501b53SJohannes Weiner 
16664595ef88SKirill A. Shutemov 	spin_lock_irq(&info->lock);
166754af6042SHugh Dickins 	info->swapped--;
166854af6042SHugh Dickins 	shmem_recalc_inode(inode);
16694595ef88SKirill A. Shutemov 	spin_unlock_irq(&info->lock);
167027ab7006SHugh Dickins 
167166d2f4d2SHugh Dickins 	if (sgp == SGP_WRITE)
167266d2f4d2SHugh Dickins 		mark_page_accessed(page);
167366d2f4d2SHugh Dickins 
167427ab7006SHugh Dickins 	delete_from_swap_cache(page);
167527ab7006SHugh Dickins 	set_page_dirty(page);
167627ab7006SHugh Dickins 	swap_free(swap);
167727ab7006SHugh Dickins 
1678*c5bf121eSVineeth Remanan Pillai 	*pagep = page;
1679*c5bf121eSVineeth Remanan Pillai 	return 0;
1680*c5bf121eSVineeth Remanan Pillai failed:
1681*c5bf121eSVineeth Remanan Pillai 	if (!shmem_confirm_swap(mapping, index, swap))
1682*c5bf121eSVineeth Remanan Pillai 		error = -EEXIST;
1683*c5bf121eSVineeth Remanan Pillai unlock:
1684*c5bf121eSVineeth Remanan Pillai 	if (page) {
1685*c5bf121eSVineeth Remanan Pillai 		unlock_page(page);
1686*c5bf121eSVineeth Remanan Pillai 		put_page(page);
1687*c5bf121eSVineeth Remanan Pillai 	}
1688*c5bf121eSVineeth Remanan Pillai 
1689*c5bf121eSVineeth Remanan Pillai 	return error;
1690*c5bf121eSVineeth Remanan Pillai }
1691*c5bf121eSVineeth Remanan Pillai 
1692*c5bf121eSVineeth Remanan Pillai /*
1693*c5bf121eSVineeth Remanan Pillai  * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
1694*c5bf121eSVineeth Remanan Pillai  *
1695*c5bf121eSVineeth Remanan Pillai  * If we allocate a new one we do not mark it dirty. That's up to the
1696*c5bf121eSVineeth Remanan Pillai  * vm. If we swap it in we mark it dirty since we also free the swap
1697*c5bf121eSVineeth Remanan Pillai  * entry since a page cannot live in both the swap and page cache.
1698*c5bf121eSVineeth Remanan Pillai  *
1699*c5bf121eSVineeth Remanan Pillai  * fault_mm and fault_type are only supplied by shmem_fault:
1700*c5bf121eSVineeth Remanan Pillai  * otherwise they are NULL.
1701*c5bf121eSVineeth Remanan Pillai  */
1702*c5bf121eSVineeth Remanan Pillai static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
1703*c5bf121eSVineeth Remanan Pillai 	struct page **pagep, enum sgp_type sgp, gfp_t gfp,
1704*c5bf121eSVineeth Remanan Pillai 	struct vm_area_struct *vma, struct vm_fault *vmf,
1705*c5bf121eSVineeth Remanan Pillai 			vm_fault_t *fault_type)
1706*c5bf121eSVineeth Remanan Pillai {
1707*c5bf121eSVineeth Remanan Pillai 	struct address_space *mapping = inode->i_mapping;
1708*c5bf121eSVineeth Remanan Pillai 	struct shmem_inode_info *info = SHMEM_I(inode);
1709*c5bf121eSVineeth Remanan Pillai 	struct shmem_sb_info *sbinfo;
1710*c5bf121eSVineeth Remanan Pillai 	struct mm_struct *charge_mm;
1711*c5bf121eSVineeth Remanan Pillai 	struct mem_cgroup *memcg;
1712*c5bf121eSVineeth Remanan Pillai 	struct page *page;
1713*c5bf121eSVineeth Remanan Pillai 	enum sgp_type sgp_huge = sgp;
1714*c5bf121eSVineeth Remanan Pillai 	pgoff_t hindex = index;
1715*c5bf121eSVineeth Remanan Pillai 	int error;
1716*c5bf121eSVineeth Remanan Pillai 	int once = 0;
1717*c5bf121eSVineeth Remanan Pillai 	int alloced = 0;
1718*c5bf121eSVineeth Remanan Pillai 
1719*c5bf121eSVineeth Remanan Pillai 	if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
1720*c5bf121eSVineeth Remanan Pillai 		return -EFBIG;
1721*c5bf121eSVineeth Remanan Pillai 	if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1722*c5bf121eSVineeth Remanan Pillai 		sgp = SGP_CACHE;
1723*c5bf121eSVineeth Remanan Pillai repeat:
1724*c5bf121eSVineeth Remanan Pillai 	if (sgp <= SGP_CACHE &&
1725*c5bf121eSVineeth Remanan Pillai 	    ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
1726*c5bf121eSVineeth Remanan Pillai 		return -EINVAL;
1727*c5bf121eSVineeth Remanan Pillai 	}
1728*c5bf121eSVineeth Remanan Pillai 
1729*c5bf121eSVineeth Remanan Pillai 	sbinfo = SHMEM_SB(inode->i_sb);
1730*c5bf121eSVineeth Remanan Pillai 	charge_mm = vma ? vma->vm_mm : current->mm;
1731*c5bf121eSVineeth Remanan Pillai 
1732*c5bf121eSVineeth Remanan Pillai 	page = find_lock_entry(mapping, index);
1733*c5bf121eSVineeth Remanan Pillai 	if (xa_is_value(page)) {
1734*c5bf121eSVineeth Remanan Pillai 		error = shmem_swapin_page(inode, index, &page,
1735*c5bf121eSVineeth Remanan Pillai 					  sgp, gfp, vma, fault_type);
1736*c5bf121eSVineeth Remanan Pillai 		if (error == -EEXIST)
1737*c5bf121eSVineeth Remanan Pillai 			goto repeat;
1738*c5bf121eSVineeth Remanan Pillai 
1739*c5bf121eSVineeth Remanan Pillai 		*pagep = page;
1740*c5bf121eSVineeth Remanan Pillai 		return error;
1741*c5bf121eSVineeth Remanan Pillai 	}
1742*c5bf121eSVineeth Remanan Pillai 
1743*c5bf121eSVineeth Remanan Pillai 	if (page && sgp == SGP_WRITE)
1744*c5bf121eSVineeth Remanan Pillai 		mark_page_accessed(page);
1745*c5bf121eSVineeth Remanan Pillai 
1746*c5bf121eSVineeth Remanan Pillai 	/* fallocated page? */
1747*c5bf121eSVineeth Remanan Pillai 	if (page && !PageUptodate(page)) {
1748*c5bf121eSVineeth Remanan Pillai 		if (sgp != SGP_READ)
1749*c5bf121eSVineeth Remanan Pillai 			goto clear;
1750*c5bf121eSVineeth Remanan Pillai 		unlock_page(page);
1751*c5bf121eSVineeth Remanan Pillai 		put_page(page);
1752*c5bf121eSVineeth Remanan Pillai 		page = NULL;
1753*c5bf121eSVineeth Remanan Pillai 	}
1754*c5bf121eSVineeth Remanan Pillai 	if (page || sgp == SGP_READ) {
1755*c5bf121eSVineeth Remanan Pillai 		*pagep = page;
1756*c5bf121eSVineeth Remanan Pillai 		return 0;
1757*c5bf121eSVineeth Remanan Pillai 	}
1758*c5bf121eSVineeth Remanan Pillai 
1759*c5bf121eSVineeth Remanan Pillai 	/*
1760*c5bf121eSVineeth Remanan Pillai 	 * Fast cache lookup did not find it:
1761*c5bf121eSVineeth Remanan Pillai 	 * bring it back from swap or allocate.
1762*c5bf121eSVineeth Remanan Pillai 	 */
1763*c5bf121eSVineeth Remanan Pillai 
1764cfda0526SMike Rapoport 	if (vma && userfaultfd_missing(vma)) {
1765cfda0526SMike Rapoport 		*fault_type = handle_userfault(vmf, VM_UFFD_MISSING);
1766cfda0526SMike Rapoport 		return 0;
1767cfda0526SMike Rapoport 	}
1768cfda0526SMike Rapoport 
1769800d8c63SKirill A. Shutemov 	/* shmem_symlink() */
1770800d8c63SKirill A. Shutemov 	if (mapping->a_ops != &shmem_aops)
1771800d8c63SKirill A. Shutemov 		goto alloc_nohuge;
1772657e3038SKirill A. Shutemov 	if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
1773800d8c63SKirill A. Shutemov 		goto alloc_nohuge;
1774800d8c63SKirill A. Shutemov 	if (shmem_huge == SHMEM_HUGE_FORCE)
1775800d8c63SKirill A. Shutemov 		goto alloc_huge;
1776800d8c63SKirill A. Shutemov 	switch (sbinfo->huge) {
1777800d8c63SKirill A. Shutemov 		loff_t i_size;
1778800d8c63SKirill A. Shutemov 		pgoff_t off;
1779800d8c63SKirill A. Shutemov 	case SHMEM_HUGE_NEVER:
1780800d8c63SKirill A. Shutemov 		goto alloc_nohuge;
1781800d8c63SKirill A. Shutemov 	case SHMEM_HUGE_WITHIN_SIZE:
1782800d8c63SKirill A. Shutemov 		off = round_up(index, HPAGE_PMD_NR);
1783800d8c63SKirill A. Shutemov 		i_size = round_up(i_size_read(inode), PAGE_SIZE);
1784800d8c63SKirill A. Shutemov 		if (i_size >= HPAGE_PMD_SIZE &&
1785800d8c63SKirill A. Shutemov 		    i_size >> PAGE_SHIFT >= off)
1786800d8c63SKirill A. Shutemov 			goto alloc_huge;
1787800d8c63SKirill A. Shutemov 		/* fallthrough */
1788800d8c63SKirill A. Shutemov 	case SHMEM_HUGE_ADVISE:
1789657e3038SKirill A. Shutemov 		if (sgp_huge == SGP_HUGE)
1790657e3038SKirill A. Shutemov 			goto alloc_huge;
1791657e3038SKirill A. Shutemov 		/* TODO: implement fadvise() hints */
1792800d8c63SKirill A. Shutemov 		goto alloc_nohuge;
179359a16eadSHugh Dickins 	}
17941da177e4SLinus Torvalds 
1795800d8c63SKirill A. Shutemov alloc_huge:
17960f079694SMike Rapoport 	page = shmem_alloc_and_acct_page(gfp, inode, index, true);
1797800d8c63SKirill A. Shutemov 	if (IS_ERR(page)) {
1798*c5bf121eSVineeth Remanan Pillai alloc_nohuge:
1799*c5bf121eSVineeth Remanan Pillai 		page = shmem_alloc_and_acct_page(gfp, inode,
1800800d8c63SKirill A. Shutemov 						 index, false);
180154af6042SHugh Dickins 	}
1802800d8c63SKirill A. Shutemov 	if (IS_ERR(page)) {
1803779750d2SKirill A. Shutemov 		int retry = 5;
1804*c5bf121eSVineeth Remanan Pillai 
1805800d8c63SKirill A. Shutemov 		error = PTR_ERR(page);
1806800d8c63SKirill A. Shutemov 		page = NULL;
1807779750d2SKirill A. Shutemov 		if (error != -ENOSPC)
1808*c5bf121eSVineeth Remanan Pillai 			goto unlock;
1809779750d2SKirill A. Shutemov 		/*
1810*c5bf121eSVineeth Remanan Pillai 		 * Try to reclaim some space by splitting a huge page
1811779750d2SKirill A. Shutemov 		 * beyond i_size on the filesystem.
1812779750d2SKirill A. Shutemov 		 */
1813779750d2SKirill A. Shutemov 		while (retry--) {
1814779750d2SKirill A. Shutemov 			int ret;
1815*c5bf121eSVineeth Remanan Pillai 
1816779750d2SKirill A. Shutemov 			ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
1817779750d2SKirill A. Shutemov 			if (ret == SHRINK_STOP)
1818779750d2SKirill A. Shutemov 				break;
1819779750d2SKirill A. Shutemov 			if (ret)
1820779750d2SKirill A. Shutemov 				goto alloc_nohuge;
1821779750d2SKirill A. Shutemov 		}
1822*c5bf121eSVineeth Remanan Pillai 		goto unlock;
1823800d8c63SKirill A. Shutemov 	}
1824800d8c63SKirill A. Shutemov 
1825800d8c63SKirill A. Shutemov 	if (PageTransHuge(page))
1826800d8c63SKirill A. Shutemov 		hindex = round_down(index, HPAGE_PMD_NR);
1827800d8c63SKirill A. Shutemov 	else
1828800d8c63SKirill A. Shutemov 		hindex = index;
1829800d8c63SKirill A. Shutemov 
183066d2f4d2SHugh Dickins 	if (sgp == SGP_WRITE)
1831eb39d618SHugh Dickins 		__SetPageReferenced(page);
183266d2f4d2SHugh Dickins 
18332cf85583STejun Heo 	error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
1834800d8c63SKirill A. Shutemov 					    PageTransHuge(page));
183554af6042SHugh Dickins 	if (error)
1836800d8c63SKirill A. Shutemov 		goto unacct;
1837800d8c63SKirill A. Shutemov 	error = shmem_add_to_page_cache(page, mapping, hindex,
1838552446a4SMatthew Wilcox 					NULL, gfp & GFP_RECLAIM_MASK);
1839b065b432SHugh Dickins 	if (error) {
1840800d8c63SKirill A. Shutemov 		mem_cgroup_cancel_charge(page, memcg,
1841800d8c63SKirill A. Shutemov 					 PageTransHuge(page));
1842800d8c63SKirill A. Shutemov 		goto unacct;
1843b065b432SHugh Dickins 	}
1844800d8c63SKirill A. Shutemov 	mem_cgroup_commit_charge(page, memcg, false,
1845800d8c63SKirill A. Shutemov 				 PageTransHuge(page));
184654af6042SHugh Dickins 	lru_cache_add_anon(page);
184754af6042SHugh Dickins 
18484595ef88SKirill A. Shutemov 	spin_lock_irq(&info->lock);
1849800d8c63SKirill A. Shutemov 	info->alloced += 1 << compound_order(page);
1850800d8c63SKirill A. Shutemov 	inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
185154af6042SHugh Dickins 	shmem_recalc_inode(inode);
18524595ef88SKirill A. Shutemov 	spin_unlock_irq(&info->lock);
18531635f6a7SHugh Dickins 	alloced = true;
185454af6042SHugh Dickins 
1855779750d2SKirill A. Shutemov 	if (PageTransHuge(page) &&
1856779750d2SKirill A. Shutemov 	    DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
1857779750d2SKirill A. Shutemov 			hindex + HPAGE_PMD_NR - 1) {
1858779750d2SKirill A. Shutemov 		/*
1859779750d2SKirill A. Shutemov 		 * Part of the huge page is beyond i_size: subject
1860779750d2SKirill A. Shutemov 		 * to shrink under memory pressure.
1861779750d2SKirill A. Shutemov 		 */
1862779750d2SKirill A. Shutemov 		spin_lock(&sbinfo->shrinklist_lock);
1863d041353dSCong Wang 		/*
1864d041353dSCong Wang 		 * _careful to defend against unlocked access to
1865d041353dSCong Wang 		 * ->shrink_list in shmem_unused_huge_shrink()
1866d041353dSCong Wang 		 */
1867d041353dSCong Wang 		if (list_empty_careful(&info->shrinklist)) {
1868779750d2SKirill A. Shutemov 			list_add_tail(&info->shrinklist,
1869779750d2SKirill A. Shutemov 				      &sbinfo->shrinklist);
1870779750d2SKirill A. Shutemov 			sbinfo->shrinklist_len++;
1871779750d2SKirill A. Shutemov 		}
1872779750d2SKirill A. Shutemov 		spin_unlock(&sbinfo->shrinklist_lock);
1873779750d2SKirill A. Shutemov 	}
1874779750d2SKirill A. Shutemov 
1875ec9516fbSHugh Dickins 	/*
18761635f6a7SHugh Dickins 	 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
18771635f6a7SHugh Dickins 	 */
18781635f6a7SHugh Dickins 	if (sgp == SGP_FALLOC)
18791635f6a7SHugh Dickins 		sgp = SGP_WRITE;
18801635f6a7SHugh Dickins clear:
18811635f6a7SHugh Dickins 	/*
18821635f6a7SHugh Dickins 	 * Let SGP_WRITE caller clear ends if write does not fill page;
18831635f6a7SHugh Dickins 	 * but SGP_FALLOC on a page fallocated earlier must initialize
18841635f6a7SHugh Dickins 	 * it now, lest undo on failure cancel our earlier guarantee.
1885ec9516fbSHugh Dickins 	 */
1886800d8c63SKirill A. Shutemov 	if (sgp != SGP_WRITE && !PageUptodate(page)) {
1887800d8c63SKirill A. Shutemov 		struct page *head = compound_head(page);
1888800d8c63SKirill A. Shutemov 		int i;
1889800d8c63SKirill A. Shutemov 
1890800d8c63SKirill A. Shutemov 		for (i = 0; i < (1 << compound_order(head)); i++) {
1891800d8c63SKirill A. Shutemov 			clear_highpage(head + i);
1892800d8c63SKirill A. Shutemov 			flush_dcache_page(head + i);
1893800d8c63SKirill A. Shutemov 		}
1894800d8c63SKirill A. Shutemov 		SetPageUptodate(head);
1895ec9516fbSHugh Dickins 	}
1896bde05d1cSHugh Dickins 
189754af6042SHugh Dickins 	/* Perhaps the file has been truncated since we checked */
189875edd345SHugh Dickins 	if (sgp <= SGP_CACHE &&
189909cbfeafSKirill A. Shutemov 	    ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
1900267a4c76SHugh Dickins 		if (alloced) {
1901267a4c76SHugh Dickins 			ClearPageDirty(page);
1902267a4c76SHugh Dickins 			delete_from_page_cache(page);
19034595ef88SKirill A. Shutemov 			spin_lock_irq(&info->lock);
1904267a4c76SHugh Dickins 			shmem_recalc_inode(inode);
19054595ef88SKirill A. Shutemov 			spin_unlock_irq(&info->lock);
1906267a4c76SHugh Dickins 		}
190754af6042SHugh Dickins 		error = -EINVAL;
1908267a4c76SHugh Dickins 		goto unlock;
1909ff36b801SShaohua Li 	}
1910800d8c63SKirill A. Shutemov 	*pagep = page + index - hindex;
191154af6042SHugh Dickins 	return 0;
1912d00806b1SNick Piggin 
1913d0217ac0SNick Piggin 	/*
191454af6042SHugh Dickins 	 * Error recovery.
19151da177e4SLinus Torvalds 	 */
191654af6042SHugh Dickins unacct:
19170f079694SMike Rapoport 	shmem_inode_unacct_blocks(inode, 1 << compound_order(page));
1918800d8c63SKirill A. Shutemov 
1919800d8c63SKirill A. Shutemov 	if (PageTransHuge(page)) {
1920800d8c63SKirill A. Shutemov 		unlock_page(page);
1921800d8c63SKirill A. Shutemov 		put_page(page);
1922800d8c63SKirill A. Shutemov 		goto alloc_nohuge;
1923800d8c63SKirill A. Shutemov 	}
1924d1899228SHugh Dickins unlock:
192527ab7006SHugh Dickins 	if (page) {
192654af6042SHugh Dickins 		unlock_page(page);
192709cbfeafSKirill A. Shutemov 		put_page(page);
192854af6042SHugh Dickins 	}
192954af6042SHugh Dickins 	if (error == -ENOSPC && !once++) {
19304595ef88SKirill A. Shutemov 		spin_lock_irq(&info->lock);
193154af6042SHugh Dickins 		shmem_recalc_inode(inode);
19324595ef88SKirill A. Shutemov 		spin_unlock_irq(&info->lock);
19331da177e4SLinus Torvalds 		goto repeat;
1934d8dc74f2SAdrian Bunk 	}
19357f4446eeSMatthew Wilcox 	if (error == -EEXIST)
193654af6042SHugh Dickins 		goto repeat;
193754af6042SHugh Dickins 	return error;
19381da177e4SLinus Torvalds }
19391da177e4SLinus Torvalds 
194010d20bd2SLinus Torvalds /*
194110d20bd2SLinus Torvalds  * This is like autoremove_wake_function, but it removes the wait queue
194210d20bd2SLinus Torvalds  * entry unconditionally - even if something else had already woken the
194310d20bd2SLinus Torvalds  * target.
194410d20bd2SLinus Torvalds  */
1945ac6424b9SIngo Molnar static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
194610d20bd2SLinus Torvalds {
194710d20bd2SLinus Torvalds 	int ret = default_wake_function(wait, mode, sync, key);
19482055da97SIngo Molnar 	list_del_init(&wait->entry);
194910d20bd2SLinus Torvalds 	return ret;
195010d20bd2SLinus Torvalds }
195110d20bd2SLinus Torvalds 
195220acce67SSouptick Joarder static vm_fault_t shmem_fault(struct vm_fault *vmf)
19531da177e4SLinus Torvalds {
195411bac800SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
1955496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
19569e18eb29SAndres Lagar-Cavilla 	gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
1957657e3038SKirill A. Shutemov 	enum sgp_type sgp;
195820acce67SSouptick Joarder 	int err;
195920acce67SSouptick Joarder 	vm_fault_t ret = VM_FAULT_LOCKED;
19601da177e4SLinus Torvalds 
1961f00cdc6dSHugh Dickins 	/*
1962f00cdc6dSHugh Dickins 	 * Trinity finds that probing a hole which tmpfs is punching can
1963f00cdc6dSHugh Dickins 	 * prevent the hole-punch from ever completing: which in turn
1964f00cdc6dSHugh Dickins 	 * locks writers out with its hold on i_mutex.  So refrain from
19658e205f77SHugh Dickins 	 * faulting pages into the hole while it's being punched.  Although
19668e205f77SHugh Dickins 	 * shmem_undo_range() does remove the additions, it may be unable to
19678e205f77SHugh Dickins 	 * keep up, as each new page needs its own unmap_mapping_range() call,
19688e205f77SHugh Dickins 	 * and the i_mmap tree grows ever slower to scan if new vmas are added.
19698e205f77SHugh Dickins 	 *
19708e205f77SHugh Dickins 	 * It does not matter if we sometimes reach this check just before the
19718e205f77SHugh Dickins 	 * hole-punch begins, so that one fault then races with the punch:
19728e205f77SHugh Dickins 	 * we just need to make racing faults a rare case.
19738e205f77SHugh Dickins 	 *
19748e205f77SHugh Dickins 	 * The implementation below would be much simpler if we just used a
19758e205f77SHugh Dickins 	 * standard mutex or completion: but we cannot take i_mutex in fault,
19768e205f77SHugh Dickins 	 * and bloating every shmem inode for this unlikely case would be sad.
1977f00cdc6dSHugh Dickins 	 */
1978f00cdc6dSHugh Dickins 	if (unlikely(inode->i_private)) {
1979f00cdc6dSHugh Dickins 		struct shmem_falloc *shmem_falloc;
1980f00cdc6dSHugh Dickins 
1981f00cdc6dSHugh Dickins 		spin_lock(&inode->i_lock);
1982f00cdc6dSHugh Dickins 		shmem_falloc = inode->i_private;
19838e205f77SHugh Dickins 		if (shmem_falloc &&
19848e205f77SHugh Dickins 		    shmem_falloc->waitq &&
19858e205f77SHugh Dickins 		    vmf->pgoff >= shmem_falloc->start &&
19868e205f77SHugh Dickins 		    vmf->pgoff < shmem_falloc->next) {
19878e205f77SHugh Dickins 			wait_queue_head_t *shmem_falloc_waitq;
198810d20bd2SLinus Torvalds 			DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
19898e205f77SHugh Dickins 
19908e205f77SHugh Dickins 			ret = VM_FAULT_NOPAGE;
1991f00cdc6dSHugh Dickins 			if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1992f00cdc6dSHugh Dickins 			   !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
19938e205f77SHugh Dickins 				/* It's polite to up mmap_sem if we can */
1994f00cdc6dSHugh Dickins 				up_read(&vma->vm_mm->mmap_sem);
19958e205f77SHugh Dickins 				ret = VM_FAULT_RETRY;
1996f00cdc6dSHugh Dickins 			}
19978e205f77SHugh Dickins 
19988e205f77SHugh Dickins 			shmem_falloc_waitq = shmem_falloc->waitq;
19998e205f77SHugh Dickins 			prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
20008e205f77SHugh Dickins 					TASK_UNINTERRUPTIBLE);
20018e205f77SHugh Dickins 			spin_unlock(&inode->i_lock);
20028e205f77SHugh Dickins 			schedule();
20038e205f77SHugh Dickins 
20048e205f77SHugh Dickins 			/*
20058e205f77SHugh Dickins 			 * shmem_falloc_waitq points into the shmem_fallocate()
20068e205f77SHugh Dickins 			 * stack of the hole-punching task: shmem_falloc_waitq
20078e205f77SHugh Dickins 			 * is usually invalid by the time we reach here, but
20088e205f77SHugh Dickins 			 * finish_wait() does not dereference it in that case;
20098e205f77SHugh Dickins 			 * though i_lock needed lest racing with wake_up_all().
20108e205f77SHugh Dickins 			 */
20118e205f77SHugh Dickins 			spin_lock(&inode->i_lock);
20128e205f77SHugh Dickins 			finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
20138e205f77SHugh Dickins 			spin_unlock(&inode->i_lock);
20148e205f77SHugh Dickins 			return ret;
2015f00cdc6dSHugh Dickins 		}
20168e205f77SHugh Dickins 		spin_unlock(&inode->i_lock);
2017f00cdc6dSHugh Dickins 	}
2018f00cdc6dSHugh Dickins 
2019657e3038SKirill A. Shutemov 	sgp = SGP_CACHE;
202018600332SMichal Hocko 
202118600332SMichal Hocko 	if ((vma->vm_flags & VM_NOHUGEPAGE) ||
202218600332SMichal Hocko 	    test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
2023657e3038SKirill A. Shutemov 		sgp = SGP_NOHUGE;
202418600332SMichal Hocko 	else if (vma->vm_flags & VM_HUGEPAGE)
202518600332SMichal Hocko 		sgp = SGP_HUGE;
2026657e3038SKirill A. Shutemov 
202720acce67SSouptick Joarder 	err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
2028cfda0526SMike Rapoport 				  gfp, vma, vmf, &ret);
202920acce67SSouptick Joarder 	if (err)
203020acce67SSouptick Joarder 		return vmf_error(err);
203168da9f05SHugh Dickins 	return ret;
20321da177e4SLinus Torvalds }
20331da177e4SLinus Torvalds 
2034c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file,
2035c01d5b30SHugh Dickins 				      unsigned long uaddr, unsigned long len,
2036c01d5b30SHugh Dickins 				      unsigned long pgoff, unsigned long flags)
2037c01d5b30SHugh Dickins {
2038c01d5b30SHugh Dickins 	unsigned long (*get_area)(struct file *,
2039c01d5b30SHugh Dickins 		unsigned long, unsigned long, unsigned long, unsigned long);
2040c01d5b30SHugh Dickins 	unsigned long addr;
2041c01d5b30SHugh Dickins 	unsigned long offset;
2042c01d5b30SHugh Dickins 	unsigned long inflated_len;
2043c01d5b30SHugh Dickins 	unsigned long inflated_addr;
2044c01d5b30SHugh Dickins 	unsigned long inflated_offset;
2045c01d5b30SHugh Dickins 
2046c01d5b30SHugh Dickins 	if (len > TASK_SIZE)
2047c01d5b30SHugh Dickins 		return -ENOMEM;
2048c01d5b30SHugh Dickins 
2049c01d5b30SHugh Dickins 	get_area = current->mm->get_unmapped_area;
2050c01d5b30SHugh Dickins 	addr = get_area(file, uaddr, len, pgoff, flags);
2051c01d5b30SHugh Dickins 
2052e496cf3dSKirill A. Shutemov 	if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
2053c01d5b30SHugh Dickins 		return addr;
2054c01d5b30SHugh Dickins 	if (IS_ERR_VALUE(addr))
2055c01d5b30SHugh Dickins 		return addr;
2056c01d5b30SHugh Dickins 	if (addr & ~PAGE_MASK)
2057c01d5b30SHugh Dickins 		return addr;
2058c01d5b30SHugh Dickins 	if (addr > TASK_SIZE - len)
2059c01d5b30SHugh Dickins 		return addr;
2060c01d5b30SHugh Dickins 
2061c01d5b30SHugh Dickins 	if (shmem_huge == SHMEM_HUGE_DENY)
2062c01d5b30SHugh Dickins 		return addr;
2063c01d5b30SHugh Dickins 	if (len < HPAGE_PMD_SIZE)
2064c01d5b30SHugh Dickins 		return addr;
2065c01d5b30SHugh Dickins 	if (flags & MAP_FIXED)
2066c01d5b30SHugh Dickins 		return addr;
2067c01d5b30SHugh Dickins 	/*
2068c01d5b30SHugh Dickins 	 * Our priority is to support MAP_SHARED mapped hugely;
2069c01d5b30SHugh Dickins 	 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
2070c01d5b30SHugh Dickins 	 * But if caller specified an address hint, respect that as before.
2071c01d5b30SHugh Dickins 	 */
2072c01d5b30SHugh Dickins 	if (uaddr)
2073c01d5b30SHugh Dickins 		return addr;
2074c01d5b30SHugh Dickins 
2075c01d5b30SHugh Dickins 	if (shmem_huge != SHMEM_HUGE_FORCE) {
2076c01d5b30SHugh Dickins 		struct super_block *sb;
2077c01d5b30SHugh Dickins 
2078c01d5b30SHugh Dickins 		if (file) {
2079c01d5b30SHugh Dickins 			VM_BUG_ON(file->f_op != &shmem_file_operations);
2080c01d5b30SHugh Dickins 			sb = file_inode(file)->i_sb;
2081c01d5b30SHugh Dickins 		} else {
2082c01d5b30SHugh Dickins 			/*
2083c01d5b30SHugh Dickins 			 * Called directly from mm/mmap.c, or drivers/char/mem.c
2084c01d5b30SHugh Dickins 			 * for "/dev/zero", to create a shared anonymous object.
2085c01d5b30SHugh Dickins 			 */
2086c01d5b30SHugh Dickins 			if (IS_ERR(shm_mnt))
2087c01d5b30SHugh Dickins 				return addr;
2088c01d5b30SHugh Dickins 			sb = shm_mnt->mnt_sb;
2089c01d5b30SHugh Dickins 		}
20903089bf61SToshi Kani 		if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
2091c01d5b30SHugh Dickins 			return addr;
2092c01d5b30SHugh Dickins 	}
2093c01d5b30SHugh Dickins 
2094c01d5b30SHugh Dickins 	offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
2095c01d5b30SHugh Dickins 	if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
2096c01d5b30SHugh Dickins 		return addr;
2097c01d5b30SHugh Dickins 	if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
2098c01d5b30SHugh Dickins 		return addr;
2099c01d5b30SHugh Dickins 
2100c01d5b30SHugh Dickins 	inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
2101c01d5b30SHugh Dickins 	if (inflated_len > TASK_SIZE)
2102c01d5b30SHugh Dickins 		return addr;
2103c01d5b30SHugh Dickins 	if (inflated_len < len)
2104c01d5b30SHugh Dickins 		return addr;
2105c01d5b30SHugh Dickins 
2106c01d5b30SHugh Dickins 	inflated_addr = get_area(NULL, 0, inflated_len, 0, flags);
2107c01d5b30SHugh Dickins 	if (IS_ERR_VALUE(inflated_addr))
2108c01d5b30SHugh Dickins 		return addr;
2109c01d5b30SHugh Dickins 	if (inflated_addr & ~PAGE_MASK)
2110c01d5b30SHugh Dickins 		return addr;
2111c01d5b30SHugh Dickins 
2112c01d5b30SHugh Dickins 	inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2113c01d5b30SHugh Dickins 	inflated_addr += offset - inflated_offset;
2114c01d5b30SHugh Dickins 	if (inflated_offset > offset)
2115c01d5b30SHugh Dickins 		inflated_addr += HPAGE_PMD_SIZE;
2116c01d5b30SHugh Dickins 
2117c01d5b30SHugh Dickins 	if (inflated_addr > TASK_SIZE - len)
2118c01d5b30SHugh Dickins 		return addr;
2119c01d5b30SHugh Dickins 	return inflated_addr;
2120c01d5b30SHugh Dickins }
2121c01d5b30SHugh Dickins 
21221da177e4SLinus Torvalds #ifdef CONFIG_NUMA
212341ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
21241da177e4SLinus Torvalds {
2125496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
212641ffe5d5SHugh Dickins 	return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
21271da177e4SLinus Torvalds }
21281da177e4SLinus Torvalds 
2129d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
2130d8dc74f2SAdrian Bunk 					  unsigned long addr)
21311da177e4SLinus Torvalds {
2132496ad9aaSAl Viro 	struct inode *inode = file_inode(vma->vm_file);
213341ffe5d5SHugh Dickins 	pgoff_t index;
21341da177e4SLinus Torvalds 
213541ffe5d5SHugh Dickins 	index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
213641ffe5d5SHugh Dickins 	return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
21371da177e4SLinus Torvalds }
21381da177e4SLinus Torvalds #endif
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user)
21411da177e4SLinus Torvalds {
2142496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
21431da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
21441da177e4SLinus Torvalds 	int retval = -ENOMEM;
21451da177e4SLinus Torvalds 
21464595ef88SKirill A. Shutemov 	spin_lock_irq(&info->lock);
21471da177e4SLinus Torvalds 	if (lock && !(info->flags & VM_LOCKED)) {
21481da177e4SLinus Torvalds 		if (!user_shm_lock(inode->i_size, user))
21491da177e4SLinus Torvalds 			goto out_nomem;
21501da177e4SLinus Torvalds 		info->flags |= VM_LOCKED;
215189e004eaSLee Schermerhorn 		mapping_set_unevictable(file->f_mapping);
21521da177e4SLinus Torvalds 	}
21531da177e4SLinus Torvalds 	if (!lock && (info->flags & VM_LOCKED) && user) {
21541da177e4SLinus Torvalds 		user_shm_unlock(inode->i_size, user);
21551da177e4SLinus Torvalds 		info->flags &= ~VM_LOCKED;
215689e004eaSLee Schermerhorn 		mapping_clear_unevictable(file->f_mapping);
21571da177e4SLinus Torvalds 	}
21581da177e4SLinus Torvalds 	retval = 0;
215989e004eaSLee Schermerhorn 
21601da177e4SLinus Torvalds out_nomem:
21614595ef88SKirill A. Shutemov 	spin_unlock_irq(&info->lock);
21621da177e4SLinus Torvalds 	return retval;
21631da177e4SLinus Torvalds }
21641da177e4SLinus Torvalds 
21659b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
21661da177e4SLinus Torvalds {
21671da177e4SLinus Torvalds 	file_accessed(file);
21681da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
2169e496cf3dSKirill A. Shutemov 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
2170f3f0e1d2SKirill A. Shutemov 			((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2171f3f0e1d2SKirill A. Shutemov 			(vma->vm_end & HPAGE_PMD_MASK)) {
2172f3f0e1d2SKirill A. Shutemov 		khugepaged_enter(vma, vma->vm_flags);
2173f3f0e1d2SKirill A. Shutemov 	}
21741da177e4SLinus Torvalds 	return 0;
21751da177e4SLinus Torvalds }
21761da177e4SLinus Torvalds 
2177454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
217809208d15SAl Viro 				     umode_t mode, dev_t dev, unsigned long flags)
21791da177e4SLinus Torvalds {
21801da177e4SLinus Torvalds 	struct inode *inode;
21811da177e4SLinus Torvalds 	struct shmem_inode_info *info;
21821da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
21831da177e4SLinus Torvalds 
21845b04c689SPavel Emelyanov 	if (shmem_reserve_inode(sb))
21851da177e4SLinus Torvalds 		return NULL;
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds 	inode = new_inode(sb);
21881da177e4SLinus Torvalds 	if (inode) {
218985fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
2190454abafeSDmitry Monakhov 		inode_init_owner(inode, dir, mode);
21911da177e4SLinus Torvalds 		inode->i_blocks = 0;
2192078cd827SDeepa Dinamani 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
219346c9a946SArnd Bergmann 		inode->i_generation = prandom_u32();
21941da177e4SLinus Torvalds 		info = SHMEM_I(inode);
21951da177e4SLinus Torvalds 		memset(info, 0, (char *)inode - (char *)info);
21961da177e4SLinus Torvalds 		spin_lock_init(&info->lock);
219740e041a2SDavid Herrmann 		info->seals = F_SEAL_SEAL;
21980b0a0806SHugh Dickins 		info->flags = flags & VM_NORESERVE;
2199779750d2SKirill A. Shutemov 		INIT_LIST_HEAD(&info->shrinklist);
22001da177e4SLinus Torvalds 		INIT_LIST_HEAD(&info->swaplist);
220138f38657SAristeu Rozanski 		simple_xattrs_init(&info->xattrs);
220272c04902SAl Viro 		cache_no_acl(inode);
22031da177e4SLinus Torvalds 
22041da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
22051da177e4SLinus Torvalds 		default:
220639f0247dSAndreas Gruenbacher 			inode->i_op = &shmem_special_inode_operations;
22071da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
22081da177e4SLinus Torvalds 			break;
22091da177e4SLinus Torvalds 		case S_IFREG:
221014fcc23fSHugh Dickins 			inode->i_mapping->a_ops = &shmem_aops;
22111da177e4SLinus Torvalds 			inode->i_op = &shmem_inode_operations;
22121da177e4SLinus Torvalds 			inode->i_fop = &shmem_file_operations;
221371fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy,
221471fe804bSLee Schermerhorn 						 shmem_get_sbmpol(sbinfo));
22151da177e4SLinus Torvalds 			break;
22161da177e4SLinus Torvalds 		case S_IFDIR:
2217d8c76e6fSDave Hansen 			inc_nlink(inode);
22181da177e4SLinus Torvalds 			/* Some things misbehave if size == 0 on a directory */
22191da177e4SLinus Torvalds 			inode->i_size = 2 * BOGO_DIRENT_SIZE;
22201da177e4SLinus Torvalds 			inode->i_op = &shmem_dir_inode_operations;
22211da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
22221da177e4SLinus Torvalds 			break;
22231da177e4SLinus Torvalds 		case S_IFLNK:
22241da177e4SLinus Torvalds 			/*
22251da177e4SLinus Torvalds 			 * Must not load anything in the rbtree,
22261da177e4SLinus Torvalds 			 * mpol_free_shared_policy will not be called.
22271da177e4SLinus Torvalds 			 */
222871fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy, NULL);
22291da177e4SLinus Torvalds 			break;
22301da177e4SLinus Torvalds 		}
2231b45d71fbSJoel Fernandes (Google) 
2232b45d71fbSJoel Fernandes (Google) 		lockdep_annotate_inode_mutex_key(inode);
22335b04c689SPavel Emelyanov 	} else
22345b04c689SPavel Emelyanov 		shmem_free_inode(sb);
22351da177e4SLinus Torvalds 	return inode;
22361da177e4SLinus Torvalds }
22371da177e4SLinus Torvalds 
22380cd6144aSJohannes Weiner bool shmem_mapping(struct address_space *mapping)
22390cd6144aSJohannes Weiner {
2240f8005451SHugh Dickins 	return mapping->a_ops == &shmem_aops;
22410cd6144aSJohannes Weiner }
22420cd6144aSJohannes Weiner 
22438d103963SMike Rapoport static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
22444c27fe4cSMike Rapoport 				  pmd_t *dst_pmd,
22454c27fe4cSMike Rapoport 				  struct vm_area_struct *dst_vma,
22464c27fe4cSMike Rapoport 				  unsigned long dst_addr,
22474c27fe4cSMike Rapoport 				  unsigned long src_addr,
22488d103963SMike Rapoport 				  bool zeropage,
22494c27fe4cSMike Rapoport 				  struct page **pagep)
22504c27fe4cSMike Rapoport {
22514c27fe4cSMike Rapoport 	struct inode *inode = file_inode(dst_vma->vm_file);
22524c27fe4cSMike Rapoport 	struct shmem_inode_info *info = SHMEM_I(inode);
22534c27fe4cSMike Rapoport 	struct address_space *mapping = inode->i_mapping;
22544c27fe4cSMike Rapoport 	gfp_t gfp = mapping_gfp_mask(mapping);
22554c27fe4cSMike Rapoport 	pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
22564c27fe4cSMike Rapoport 	struct mem_cgroup *memcg;
22574c27fe4cSMike Rapoport 	spinlock_t *ptl;
22584c27fe4cSMike Rapoport 	void *page_kaddr;
22594c27fe4cSMike Rapoport 	struct page *page;
22604c27fe4cSMike Rapoport 	pte_t _dst_pte, *dst_pte;
22614c27fe4cSMike Rapoport 	int ret;
2262e2a50c1fSAndrea Arcangeli 	pgoff_t offset, max_off;
22634c27fe4cSMike Rapoport 
22644c27fe4cSMike Rapoport 	ret = -ENOMEM;
22650f079694SMike Rapoport 	if (!shmem_inode_acct_block(inode, 1))
22664c27fe4cSMike Rapoport 		goto out;
22674c27fe4cSMike Rapoport 
2268cb658a45SAndrea Arcangeli 	if (!*pagep) {
22694c27fe4cSMike Rapoport 		page = shmem_alloc_page(gfp, info, pgoff);
22704c27fe4cSMike Rapoport 		if (!page)
22710f079694SMike Rapoport 			goto out_unacct_blocks;
22724c27fe4cSMike Rapoport 
22738d103963SMike Rapoport 		if (!zeropage) {	/* mcopy_atomic */
22744c27fe4cSMike Rapoport 			page_kaddr = kmap_atomic(page);
22758d103963SMike Rapoport 			ret = copy_from_user(page_kaddr,
22768d103963SMike Rapoport 					     (const void __user *)src_addr,
22774c27fe4cSMike Rapoport 					     PAGE_SIZE);
22784c27fe4cSMike Rapoport 			kunmap_atomic(page_kaddr);
22794c27fe4cSMike Rapoport 
22804c27fe4cSMike Rapoport 			/* fallback to copy_from_user outside mmap_sem */
22814c27fe4cSMike Rapoport 			if (unlikely(ret)) {
22824c27fe4cSMike Rapoport 				*pagep = page;
22830f079694SMike Rapoport 				shmem_inode_unacct_blocks(inode, 1);
22844c27fe4cSMike Rapoport 				/* don't free the page */
22859e368259SAndrea Arcangeli 				return -ENOENT;
22864c27fe4cSMike Rapoport 			}
22878d103963SMike Rapoport 		} else {		/* mfill_zeropage_atomic */
22888d103963SMike Rapoport 			clear_highpage(page);
22898d103963SMike Rapoport 		}
22904c27fe4cSMike Rapoport 	} else {
22914c27fe4cSMike Rapoport 		page = *pagep;
22924c27fe4cSMike Rapoport 		*pagep = NULL;
22934c27fe4cSMike Rapoport 	}
22944c27fe4cSMike Rapoport 
22959cc90c66SAndrea Arcangeli 	VM_BUG_ON(PageLocked(page) || PageSwapBacked(page));
22969cc90c66SAndrea Arcangeli 	__SetPageLocked(page);
22979cc90c66SAndrea Arcangeli 	__SetPageSwapBacked(page);
2298a425d358SAndrea Arcangeli 	__SetPageUptodate(page);
22999cc90c66SAndrea Arcangeli 
2300e2a50c1fSAndrea Arcangeli 	ret = -EFAULT;
2301e2a50c1fSAndrea Arcangeli 	offset = linear_page_index(dst_vma, dst_addr);
2302e2a50c1fSAndrea Arcangeli 	max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2303e2a50c1fSAndrea Arcangeli 	if (unlikely(offset >= max_off))
2304e2a50c1fSAndrea Arcangeli 		goto out_release;
2305e2a50c1fSAndrea Arcangeli 
23062cf85583STejun Heo 	ret = mem_cgroup_try_charge_delay(page, dst_mm, gfp, &memcg, false);
23074c27fe4cSMike Rapoport 	if (ret)
23084c27fe4cSMike Rapoport 		goto out_release;
23094c27fe4cSMike Rapoport 
2310552446a4SMatthew Wilcox 	ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL,
2311552446a4SMatthew Wilcox 						gfp & GFP_RECLAIM_MASK);
23124c27fe4cSMike Rapoport 	if (ret)
23134c27fe4cSMike Rapoport 		goto out_release_uncharge;
23144c27fe4cSMike Rapoport 
23154c27fe4cSMike Rapoport 	mem_cgroup_commit_charge(page, memcg, false, false);
23164c27fe4cSMike Rapoport 
23174c27fe4cSMike Rapoport 	_dst_pte = mk_pte(page, dst_vma->vm_page_prot);
23184c27fe4cSMike Rapoport 	if (dst_vma->vm_flags & VM_WRITE)
23194c27fe4cSMike Rapoport 		_dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
2320dcf7fe9dSAndrea Arcangeli 	else {
2321dcf7fe9dSAndrea Arcangeli 		/*
2322dcf7fe9dSAndrea Arcangeli 		 * We don't set the pte dirty if the vma has no
2323dcf7fe9dSAndrea Arcangeli 		 * VM_WRITE permission, so mark the page dirty or it
2324dcf7fe9dSAndrea Arcangeli 		 * could be freed from under us. We could do it
2325dcf7fe9dSAndrea Arcangeli 		 * unconditionally before unlock_page(), but doing it
2326dcf7fe9dSAndrea Arcangeli 		 * only if VM_WRITE is not set is faster.
2327dcf7fe9dSAndrea Arcangeli 		 */
2328dcf7fe9dSAndrea Arcangeli 		set_page_dirty(page);
2329dcf7fe9dSAndrea Arcangeli 	}
23304c27fe4cSMike Rapoport 
23314c27fe4cSMike Rapoport 	dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
2332e2a50c1fSAndrea Arcangeli 
2333e2a50c1fSAndrea Arcangeli 	ret = -EFAULT;
2334e2a50c1fSAndrea Arcangeli 	max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2335e2a50c1fSAndrea Arcangeli 	if (unlikely(offset >= max_off))
2336e2a50c1fSAndrea Arcangeli 		goto out_release_uncharge_unlock;
2337e2a50c1fSAndrea Arcangeli 
2338e2a50c1fSAndrea Arcangeli 	ret = -EEXIST;
23394c27fe4cSMike Rapoport 	if (!pte_none(*dst_pte))
23404c27fe4cSMike Rapoport 		goto out_release_uncharge_unlock;
23414c27fe4cSMike Rapoport 
23424c27fe4cSMike Rapoport 	lru_cache_add_anon(page);
23434c27fe4cSMike Rapoport 
23444c27fe4cSMike Rapoport 	spin_lock(&info->lock);
23454c27fe4cSMike Rapoport 	info->alloced++;
23464c27fe4cSMike Rapoport 	inode->i_blocks += BLOCKS_PER_PAGE;
23474c27fe4cSMike Rapoport 	shmem_recalc_inode(inode);
23484c27fe4cSMike Rapoport 	spin_unlock(&info->lock);
23494c27fe4cSMike Rapoport 
23504c27fe4cSMike Rapoport 	inc_mm_counter(dst_mm, mm_counter_file(page));
23514c27fe4cSMike Rapoport 	page_add_file_rmap(page, false);
23524c27fe4cSMike Rapoport 	set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
23534c27fe4cSMike Rapoport 
23544c27fe4cSMike Rapoport 	/* No need to invalidate - it was non-present before */
23554c27fe4cSMike Rapoport 	update_mmu_cache(dst_vma, dst_addr, dst_pte);
23564c27fe4cSMike Rapoport 	pte_unmap_unlock(dst_pte, ptl);
2357e2a50c1fSAndrea Arcangeli 	unlock_page(page);
23584c27fe4cSMike Rapoport 	ret = 0;
23594c27fe4cSMike Rapoport out:
23604c27fe4cSMike Rapoport 	return ret;
23614c27fe4cSMike Rapoport out_release_uncharge_unlock:
23624c27fe4cSMike Rapoport 	pte_unmap_unlock(dst_pte, ptl);
2363dcf7fe9dSAndrea Arcangeli 	ClearPageDirty(page);
2364e2a50c1fSAndrea Arcangeli 	delete_from_page_cache(page);
23654c27fe4cSMike Rapoport out_release_uncharge:
23664c27fe4cSMike Rapoport 	mem_cgroup_cancel_charge(page, memcg, false);
23674c27fe4cSMike Rapoport out_release:
23689cc90c66SAndrea Arcangeli 	unlock_page(page);
23694c27fe4cSMike Rapoport 	put_page(page);
23704c27fe4cSMike Rapoport out_unacct_blocks:
23710f079694SMike Rapoport 	shmem_inode_unacct_blocks(inode, 1);
23724c27fe4cSMike Rapoport 	goto out;
23734c27fe4cSMike Rapoport }
23744c27fe4cSMike Rapoport 
23758d103963SMike Rapoport int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm,
23768d103963SMike Rapoport 			   pmd_t *dst_pmd,
23778d103963SMike Rapoport 			   struct vm_area_struct *dst_vma,
23788d103963SMike Rapoport 			   unsigned long dst_addr,
23798d103963SMike Rapoport 			   unsigned long src_addr,
23808d103963SMike Rapoport 			   struct page **pagep)
23818d103963SMike Rapoport {
23828d103963SMike Rapoport 	return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
23838d103963SMike Rapoport 				      dst_addr, src_addr, false, pagep);
23848d103963SMike Rapoport }
23858d103963SMike Rapoport 
23868d103963SMike Rapoport int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm,
23878d103963SMike Rapoport 			     pmd_t *dst_pmd,
23888d103963SMike Rapoport 			     struct vm_area_struct *dst_vma,
23898d103963SMike Rapoport 			     unsigned long dst_addr)
23908d103963SMike Rapoport {
23918d103963SMike Rapoport 	struct page *page = NULL;
23928d103963SMike Rapoport 
23938d103963SMike Rapoport 	return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
23948d103963SMike Rapoport 				      dst_addr, 0, true, &page);
23958d103963SMike Rapoport }
23968d103963SMike Rapoport 
23971da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
239892e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations;
239969f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations;
24001da177e4SLinus Torvalds 
24016d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR
24026d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
24036d9d88d0SJarkko Sakkinen #else
24046d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL
24056d9d88d0SJarkko Sakkinen #endif
24066d9d88d0SJarkko Sakkinen 
24071da177e4SLinus Torvalds static int
2408800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping,
2409800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
2410800d15a5SNick Piggin 			struct page **pagep, void **fsdata)
24111da177e4SLinus Torvalds {
2412800d15a5SNick Piggin 	struct inode *inode = mapping->host;
241340e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
241409cbfeafSKirill A. Shutemov 	pgoff_t index = pos >> PAGE_SHIFT;
241540e041a2SDavid Herrmann 
241640e041a2SDavid Herrmann 	/* i_mutex is held by caller */
24173f472cc9SSteven Rostedt (VMware) 	if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) {
241840e041a2SDavid Herrmann 		if (info->seals & F_SEAL_WRITE)
241940e041a2SDavid Herrmann 			return -EPERM;
242040e041a2SDavid Herrmann 		if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
242140e041a2SDavid Herrmann 			return -EPERM;
242240e041a2SDavid Herrmann 	}
242340e041a2SDavid Herrmann 
24249e18eb29SAndres Lagar-Cavilla 	return shmem_getpage(inode, index, pagep, SGP_WRITE);
2425800d15a5SNick Piggin }
2426800d15a5SNick Piggin 
2427800d15a5SNick Piggin static int
2428800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping,
2429800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
2430800d15a5SNick Piggin 			struct page *page, void *fsdata)
2431800d15a5SNick Piggin {
2432800d15a5SNick Piggin 	struct inode *inode = mapping->host;
2433800d15a5SNick Piggin 
2434800d15a5SNick Piggin 	if (pos + copied > inode->i_size)
2435800d15a5SNick Piggin 		i_size_write(inode, pos + copied);
2436800d15a5SNick Piggin 
2437ec9516fbSHugh Dickins 	if (!PageUptodate(page)) {
2438800d8c63SKirill A. Shutemov 		struct page *head = compound_head(page);
2439800d8c63SKirill A. Shutemov 		if (PageTransCompound(page)) {
2440800d8c63SKirill A. Shutemov 			int i;
2441800d8c63SKirill A. Shutemov 
2442800d8c63SKirill A. Shutemov 			for (i = 0; i < HPAGE_PMD_NR; i++) {
2443800d8c63SKirill A. Shutemov 				if (head + i == page)
2444800d8c63SKirill A. Shutemov 					continue;
2445800d8c63SKirill A. Shutemov 				clear_highpage(head + i);
2446800d8c63SKirill A. Shutemov 				flush_dcache_page(head + i);
2447800d8c63SKirill A. Shutemov 			}
2448800d8c63SKirill A. Shutemov 		}
244909cbfeafSKirill A. Shutemov 		if (copied < PAGE_SIZE) {
245009cbfeafSKirill A. Shutemov 			unsigned from = pos & (PAGE_SIZE - 1);
2451ec9516fbSHugh Dickins 			zero_user_segments(page, 0, from,
245209cbfeafSKirill A. Shutemov 					from + copied, PAGE_SIZE);
2453ec9516fbSHugh Dickins 		}
2454800d8c63SKirill A. Shutemov 		SetPageUptodate(head);
2455ec9516fbSHugh Dickins 	}
2456d3602444SHugh Dickins 	set_page_dirty(page);
24576746aff7SWu Fengguang 	unlock_page(page);
245809cbfeafSKirill A. Shutemov 	put_page(page);
2459d3602444SHugh Dickins 
2460800d15a5SNick Piggin 	return copied;
24611da177e4SLinus Torvalds }
24621da177e4SLinus Torvalds 
24632ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
24641da177e4SLinus Torvalds {
24656e58e79dSAl Viro 	struct file *file = iocb->ki_filp;
24666e58e79dSAl Viro 	struct inode *inode = file_inode(file);
24671da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
246841ffe5d5SHugh Dickins 	pgoff_t index;
246941ffe5d5SHugh Dickins 	unsigned long offset;
2470a0ee5ec5SHugh Dickins 	enum sgp_type sgp = SGP_READ;
2471f7c1d074SGeert Uytterhoeven 	int error = 0;
2472cb66a7a1SAl Viro 	ssize_t retval = 0;
24736e58e79dSAl Viro 	loff_t *ppos = &iocb->ki_pos;
2474a0ee5ec5SHugh Dickins 
2475a0ee5ec5SHugh Dickins 	/*
2476a0ee5ec5SHugh Dickins 	 * Might this read be for a stacking filesystem?  Then when reading
2477a0ee5ec5SHugh Dickins 	 * holes of a sparse file, we actually need to allocate those pages,
2478a0ee5ec5SHugh Dickins 	 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2479a0ee5ec5SHugh Dickins 	 */
2480777eda2cSAl Viro 	if (!iter_is_iovec(to))
248175edd345SHugh Dickins 		sgp = SGP_CACHE;
24821da177e4SLinus Torvalds 
248309cbfeafSKirill A. Shutemov 	index = *ppos >> PAGE_SHIFT;
248409cbfeafSKirill A. Shutemov 	offset = *ppos & ~PAGE_MASK;
24851da177e4SLinus Torvalds 
24861da177e4SLinus Torvalds 	for (;;) {
24871da177e4SLinus Torvalds 		struct page *page = NULL;
248841ffe5d5SHugh Dickins 		pgoff_t end_index;
248941ffe5d5SHugh Dickins 		unsigned long nr, ret;
24901da177e4SLinus Torvalds 		loff_t i_size = i_size_read(inode);
24911da177e4SLinus Torvalds 
249209cbfeafSKirill A. Shutemov 		end_index = i_size >> PAGE_SHIFT;
24931da177e4SLinus Torvalds 		if (index > end_index)
24941da177e4SLinus Torvalds 			break;
24951da177e4SLinus Torvalds 		if (index == end_index) {
249609cbfeafSKirill A. Shutemov 			nr = i_size & ~PAGE_MASK;
24971da177e4SLinus Torvalds 			if (nr <= offset)
24981da177e4SLinus Torvalds 				break;
24991da177e4SLinus Torvalds 		}
25001da177e4SLinus Torvalds 
25019e18eb29SAndres Lagar-Cavilla 		error = shmem_getpage(inode, index, &page, sgp);
25026e58e79dSAl Viro 		if (error) {
25036e58e79dSAl Viro 			if (error == -EINVAL)
25046e58e79dSAl Viro 				error = 0;
25051da177e4SLinus Torvalds 			break;
25061da177e4SLinus Torvalds 		}
250775edd345SHugh Dickins 		if (page) {
250875edd345SHugh Dickins 			if (sgp == SGP_CACHE)
250975edd345SHugh Dickins 				set_page_dirty(page);
2510d3602444SHugh Dickins 			unlock_page(page);
251175edd345SHugh Dickins 		}
25121da177e4SLinus Torvalds 
25131da177e4SLinus Torvalds 		/*
25141da177e4SLinus Torvalds 		 * We must evaluate after, since reads (unlike writes)
25151b1dcc1bSJes Sorensen 		 * are called without i_mutex protection against truncate
25161da177e4SLinus Torvalds 		 */
251709cbfeafSKirill A. Shutemov 		nr = PAGE_SIZE;
25181da177e4SLinus Torvalds 		i_size = i_size_read(inode);
251909cbfeafSKirill A. Shutemov 		end_index = i_size >> PAGE_SHIFT;
25201da177e4SLinus Torvalds 		if (index == end_index) {
252109cbfeafSKirill A. Shutemov 			nr = i_size & ~PAGE_MASK;
25221da177e4SLinus Torvalds 			if (nr <= offset) {
25231da177e4SLinus Torvalds 				if (page)
252409cbfeafSKirill A. Shutemov 					put_page(page);
25251da177e4SLinus Torvalds 				break;
25261da177e4SLinus Torvalds 			}
25271da177e4SLinus Torvalds 		}
25281da177e4SLinus Torvalds 		nr -= offset;
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds 		if (page) {
25311da177e4SLinus Torvalds 			/*
25321da177e4SLinus Torvalds 			 * If users can be writing to this page using arbitrary
25331da177e4SLinus Torvalds 			 * virtual addresses, take care about potential aliasing
25341da177e4SLinus Torvalds 			 * before reading the page on the kernel side.
25351da177e4SLinus Torvalds 			 */
25361da177e4SLinus Torvalds 			if (mapping_writably_mapped(mapping))
25371da177e4SLinus Torvalds 				flush_dcache_page(page);
25381da177e4SLinus Torvalds 			/*
25391da177e4SLinus Torvalds 			 * Mark the page accessed if we read the beginning.
25401da177e4SLinus Torvalds 			 */
25411da177e4SLinus Torvalds 			if (!offset)
25421da177e4SLinus Torvalds 				mark_page_accessed(page);
2543b5810039SNick Piggin 		} else {
25441da177e4SLinus Torvalds 			page = ZERO_PAGE(0);
254509cbfeafSKirill A. Shutemov 			get_page(page);
2546b5810039SNick Piggin 		}
25471da177e4SLinus Torvalds 
25481da177e4SLinus Torvalds 		/*
25491da177e4SLinus Torvalds 		 * Ok, we have the page, and it's up-to-date, so
25501da177e4SLinus Torvalds 		 * now we can copy it to user space...
25511da177e4SLinus Torvalds 		 */
25522ba5bbedSAl Viro 		ret = copy_page_to_iter(page, offset, nr, to);
25536e58e79dSAl Viro 		retval += ret;
25541da177e4SLinus Torvalds 		offset += ret;
255509cbfeafSKirill A. Shutemov 		index += offset >> PAGE_SHIFT;
255609cbfeafSKirill A. Shutemov 		offset &= ~PAGE_MASK;
25571da177e4SLinus Torvalds 
255809cbfeafSKirill A. Shutemov 		put_page(page);
25592ba5bbedSAl Viro 		if (!iov_iter_count(to))
25601da177e4SLinus Torvalds 			break;
25616e58e79dSAl Viro 		if (ret < nr) {
25626e58e79dSAl Viro 			error = -EFAULT;
25636e58e79dSAl Viro 			break;
25646e58e79dSAl Viro 		}
25651da177e4SLinus Torvalds 		cond_resched();
25661da177e4SLinus Torvalds 	}
25671da177e4SLinus Torvalds 
256809cbfeafSKirill A. Shutemov 	*ppos = ((loff_t) index << PAGE_SHIFT) + offset;
25696e58e79dSAl Viro 	file_accessed(file);
25706e58e79dSAl Viro 	return retval ? retval : error;
25711da177e4SLinus Torvalds }
25721da177e4SLinus Torvalds 
2573220f2ac9SHugh Dickins /*
25747f4446eeSMatthew Wilcox  * llseek SEEK_DATA or SEEK_HOLE through the page cache.
2575220f2ac9SHugh Dickins  */
2576220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
2577965c8e59SAndrew Morton 				    pgoff_t index, pgoff_t end, int whence)
2578220f2ac9SHugh Dickins {
2579220f2ac9SHugh Dickins 	struct page *page;
2580220f2ac9SHugh Dickins 	struct pagevec pvec;
2581220f2ac9SHugh Dickins 	pgoff_t indices[PAGEVEC_SIZE];
2582220f2ac9SHugh Dickins 	bool done = false;
2583220f2ac9SHugh Dickins 	int i;
2584220f2ac9SHugh Dickins 
258586679820SMel Gorman 	pagevec_init(&pvec);
2586220f2ac9SHugh Dickins 	pvec.nr = 1;		/* start small: we may be there already */
2587220f2ac9SHugh Dickins 	while (!done) {
25880cd6144aSJohannes Weiner 		pvec.nr = find_get_entries(mapping, index,
2589220f2ac9SHugh Dickins 					pvec.nr, pvec.pages, indices);
2590220f2ac9SHugh Dickins 		if (!pvec.nr) {
2591965c8e59SAndrew Morton 			if (whence == SEEK_DATA)
2592220f2ac9SHugh Dickins 				index = end;
2593220f2ac9SHugh Dickins 			break;
2594220f2ac9SHugh Dickins 		}
2595220f2ac9SHugh Dickins 		for (i = 0; i < pvec.nr; i++, index++) {
2596220f2ac9SHugh Dickins 			if (index < indices[i]) {
2597965c8e59SAndrew Morton 				if (whence == SEEK_HOLE) {
2598220f2ac9SHugh Dickins 					done = true;
2599220f2ac9SHugh Dickins 					break;
2600220f2ac9SHugh Dickins 				}
2601220f2ac9SHugh Dickins 				index = indices[i];
2602220f2ac9SHugh Dickins 			}
2603220f2ac9SHugh Dickins 			page = pvec.pages[i];
26043159f943SMatthew Wilcox 			if (page && !xa_is_value(page)) {
2605220f2ac9SHugh Dickins 				if (!PageUptodate(page))
2606220f2ac9SHugh Dickins 					page = NULL;
2607220f2ac9SHugh Dickins 			}
2608220f2ac9SHugh Dickins 			if (index >= end ||
2609965c8e59SAndrew Morton 			    (page && whence == SEEK_DATA) ||
2610965c8e59SAndrew Morton 			    (!page && whence == SEEK_HOLE)) {
2611220f2ac9SHugh Dickins 				done = true;
2612220f2ac9SHugh Dickins 				break;
2613220f2ac9SHugh Dickins 			}
2614220f2ac9SHugh Dickins 		}
26150cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
2616220f2ac9SHugh Dickins 		pagevec_release(&pvec);
2617220f2ac9SHugh Dickins 		pvec.nr = PAGEVEC_SIZE;
2618220f2ac9SHugh Dickins 		cond_resched();
2619220f2ac9SHugh Dickins 	}
2620220f2ac9SHugh Dickins 	return index;
2621220f2ac9SHugh Dickins }
2622220f2ac9SHugh Dickins 
2623965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
2624220f2ac9SHugh Dickins {
2625220f2ac9SHugh Dickins 	struct address_space *mapping = file->f_mapping;
2626220f2ac9SHugh Dickins 	struct inode *inode = mapping->host;
2627220f2ac9SHugh Dickins 	pgoff_t start, end;
2628220f2ac9SHugh Dickins 	loff_t new_offset;
2629220f2ac9SHugh Dickins 
2630965c8e59SAndrew Morton 	if (whence != SEEK_DATA && whence != SEEK_HOLE)
2631965c8e59SAndrew Morton 		return generic_file_llseek_size(file, offset, whence,
2632220f2ac9SHugh Dickins 					MAX_LFS_FILESIZE, i_size_read(inode));
26335955102cSAl Viro 	inode_lock(inode);
2634220f2ac9SHugh Dickins 	/* We're holding i_mutex so we can access i_size directly */
2635220f2ac9SHugh Dickins 
26361a413646SYufen Yu 	if (offset < 0 || offset >= inode->i_size)
2637220f2ac9SHugh Dickins 		offset = -ENXIO;
2638220f2ac9SHugh Dickins 	else {
263909cbfeafSKirill A. Shutemov 		start = offset >> PAGE_SHIFT;
264009cbfeafSKirill A. Shutemov 		end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2641965c8e59SAndrew Morton 		new_offset = shmem_seek_hole_data(mapping, start, end, whence);
264209cbfeafSKirill A. Shutemov 		new_offset <<= PAGE_SHIFT;
2643220f2ac9SHugh Dickins 		if (new_offset > offset) {
2644220f2ac9SHugh Dickins 			if (new_offset < inode->i_size)
2645220f2ac9SHugh Dickins 				offset = new_offset;
2646965c8e59SAndrew Morton 			else if (whence == SEEK_DATA)
2647220f2ac9SHugh Dickins 				offset = -ENXIO;
2648220f2ac9SHugh Dickins 			else
2649220f2ac9SHugh Dickins 				offset = inode->i_size;
2650220f2ac9SHugh Dickins 		}
2651220f2ac9SHugh Dickins 	}
2652220f2ac9SHugh Dickins 
2653387aae6fSHugh Dickins 	if (offset >= 0)
265446a1c2c7SJie Liu 		offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
26555955102cSAl Viro 	inode_unlock(inode);
2656220f2ac9SHugh Dickins 	return offset;
2657220f2ac9SHugh Dickins }
2658220f2ac9SHugh Dickins 
265983e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset,
266083e4fa9cSHugh Dickins 							 loff_t len)
266183e4fa9cSHugh Dickins {
2662496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2663e2d12e22SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
266440e041a2SDavid Herrmann 	struct shmem_inode_info *info = SHMEM_I(inode);
26651aac1400SHugh Dickins 	struct shmem_falloc shmem_falloc;
2666e2d12e22SHugh Dickins 	pgoff_t start, index, end;
2667e2d12e22SHugh Dickins 	int error;
266883e4fa9cSHugh Dickins 
266913ace4d0SHugh Dickins 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
267013ace4d0SHugh Dickins 		return -EOPNOTSUPP;
267113ace4d0SHugh Dickins 
26725955102cSAl Viro 	inode_lock(inode);
267383e4fa9cSHugh Dickins 
267483e4fa9cSHugh Dickins 	if (mode & FALLOC_FL_PUNCH_HOLE) {
267583e4fa9cSHugh Dickins 		struct address_space *mapping = file->f_mapping;
267683e4fa9cSHugh Dickins 		loff_t unmap_start = round_up(offset, PAGE_SIZE);
267783e4fa9cSHugh Dickins 		loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
26788e205f77SHugh Dickins 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
267983e4fa9cSHugh Dickins 
268040e041a2SDavid Herrmann 		/* protected by i_mutex */
268140e041a2SDavid Herrmann 		if (info->seals & F_SEAL_WRITE) {
268240e041a2SDavid Herrmann 			error = -EPERM;
268340e041a2SDavid Herrmann 			goto out;
268440e041a2SDavid Herrmann 		}
268540e041a2SDavid Herrmann 
26868e205f77SHugh Dickins 		shmem_falloc.waitq = &shmem_falloc_waitq;
2687f00cdc6dSHugh Dickins 		shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2688f00cdc6dSHugh Dickins 		shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2689f00cdc6dSHugh Dickins 		spin_lock(&inode->i_lock);
2690f00cdc6dSHugh Dickins 		inode->i_private = &shmem_falloc;
2691f00cdc6dSHugh Dickins 		spin_unlock(&inode->i_lock);
2692f00cdc6dSHugh Dickins 
269383e4fa9cSHugh Dickins 		if ((u64)unmap_end > (u64)unmap_start)
269483e4fa9cSHugh Dickins 			unmap_mapping_range(mapping, unmap_start,
269583e4fa9cSHugh Dickins 					    1 + unmap_end - unmap_start, 0);
269683e4fa9cSHugh Dickins 		shmem_truncate_range(inode, offset, offset + len - 1);
269783e4fa9cSHugh Dickins 		/* No need to unmap again: hole-punching leaves COWed pages */
26988e205f77SHugh Dickins 
26998e205f77SHugh Dickins 		spin_lock(&inode->i_lock);
27008e205f77SHugh Dickins 		inode->i_private = NULL;
27018e205f77SHugh Dickins 		wake_up_all(&shmem_falloc_waitq);
27022055da97SIngo Molnar 		WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
27038e205f77SHugh Dickins 		spin_unlock(&inode->i_lock);
270483e4fa9cSHugh Dickins 		error = 0;
27058e205f77SHugh Dickins 		goto out;
270683e4fa9cSHugh Dickins 	}
270783e4fa9cSHugh Dickins 
2708e2d12e22SHugh Dickins 	/* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2709e2d12e22SHugh Dickins 	error = inode_newsize_ok(inode, offset + len);
2710e2d12e22SHugh Dickins 	if (error)
2711e2d12e22SHugh Dickins 		goto out;
2712e2d12e22SHugh Dickins 
271340e041a2SDavid Herrmann 	if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
271440e041a2SDavid Herrmann 		error = -EPERM;
271540e041a2SDavid Herrmann 		goto out;
271640e041a2SDavid Herrmann 	}
271740e041a2SDavid Herrmann 
271809cbfeafSKirill A. Shutemov 	start = offset >> PAGE_SHIFT;
271909cbfeafSKirill A. Shutemov 	end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
2720e2d12e22SHugh Dickins 	/* Try to avoid a swapstorm if len is impossible to satisfy */
2721e2d12e22SHugh Dickins 	if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2722e2d12e22SHugh Dickins 		error = -ENOSPC;
2723e2d12e22SHugh Dickins 		goto out;
2724e2d12e22SHugh Dickins 	}
2725e2d12e22SHugh Dickins 
27268e205f77SHugh Dickins 	shmem_falloc.waitq = NULL;
27271aac1400SHugh Dickins 	shmem_falloc.start = start;
27281aac1400SHugh Dickins 	shmem_falloc.next  = start;
27291aac1400SHugh Dickins 	shmem_falloc.nr_falloced = 0;
27301aac1400SHugh Dickins 	shmem_falloc.nr_unswapped = 0;
27311aac1400SHugh Dickins 	spin_lock(&inode->i_lock);
27321aac1400SHugh Dickins 	inode->i_private = &shmem_falloc;
27331aac1400SHugh Dickins 	spin_unlock(&inode->i_lock);
27341aac1400SHugh Dickins 
2735e2d12e22SHugh Dickins 	for (index = start; index < end; index++) {
2736e2d12e22SHugh Dickins 		struct page *page;
2737e2d12e22SHugh Dickins 
2738e2d12e22SHugh Dickins 		/*
2739e2d12e22SHugh Dickins 		 * Good, the fallocate(2) manpage permits EINTR: we may have
2740e2d12e22SHugh Dickins 		 * been interrupted because we are using up too much memory.
2741e2d12e22SHugh Dickins 		 */
2742e2d12e22SHugh Dickins 		if (signal_pending(current))
2743e2d12e22SHugh Dickins 			error = -EINTR;
27441aac1400SHugh Dickins 		else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
27451aac1400SHugh Dickins 			error = -ENOMEM;
2746e2d12e22SHugh Dickins 		else
27479e18eb29SAndres Lagar-Cavilla 			error = shmem_getpage(inode, index, &page, SGP_FALLOC);
2748e2d12e22SHugh Dickins 		if (error) {
27491635f6a7SHugh Dickins 			/* Remove the !PageUptodate pages we added */
27507f556567SHugh Dickins 			if (index > start) {
27511635f6a7SHugh Dickins 				shmem_undo_range(inode,
275209cbfeafSKirill A. Shutemov 				    (loff_t)start << PAGE_SHIFT,
2753b9b4bb26SAnthony Romano 				    ((loff_t)index << PAGE_SHIFT) - 1, true);
27547f556567SHugh Dickins 			}
27551aac1400SHugh Dickins 			goto undone;
2756e2d12e22SHugh Dickins 		}
2757e2d12e22SHugh Dickins 
2758e2d12e22SHugh Dickins 		/*
27591aac1400SHugh Dickins 		 * Inform shmem_writepage() how far we have reached.
27601aac1400SHugh Dickins 		 * No need for lock or barrier: we have the page lock.
27611aac1400SHugh Dickins 		 */
27621aac1400SHugh Dickins 		shmem_falloc.next++;
27631aac1400SHugh Dickins 		if (!PageUptodate(page))
27641aac1400SHugh Dickins 			shmem_falloc.nr_falloced++;
27651aac1400SHugh Dickins 
27661aac1400SHugh Dickins 		/*
27671635f6a7SHugh Dickins 		 * If !PageUptodate, leave it that way so that freeable pages
27681635f6a7SHugh Dickins 		 * can be recognized if we need to rollback on error later.
27691635f6a7SHugh Dickins 		 * But set_page_dirty so that memory pressure will swap rather
2770e2d12e22SHugh Dickins 		 * than free the pages we are allocating (and SGP_CACHE pages
2771e2d12e22SHugh Dickins 		 * might still be clean: we now need to mark those dirty too).
2772e2d12e22SHugh Dickins 		 */
2773e2d12e22SHugh Dickins 		set_page_dirty(page);
2774e2d12e22SHugh Dickins 		unlock_page(page);
277509cbfeafSKirill A. Shutemov 		put_page(page);
2776e2d12e22SHugh Dickins 		cond_resched();
2777e2d12e22SHugh Dickins 	}
2778e2d12e22SHugh Dickins 
2779e2d12e22SHugh Dickins 	if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2780e2d12e22SHugh Dickins 		i_size_write(inode, offset + len);
2781078cd827SDeepa Dinamani 	inode->i_ctime = current_time(inode);
27821aac1400SHugh Dickins undone:
27831aac1400SHugh Dickins 	spin_lock(&inode->i_lock);
27841aac1400SHugh Dickins 	inode->i_private = NULL;
27851aac1400SHugh Dickins 	spin_unlock(&inode->i_lock);
2786e2d12e22SHugh Dickins out:
27875955102cSAl Viro 	inode_unlock(inode);
278883e4fa9cSHugh Dickins 	return error;
278983e4fa9cSHugh Dickins }
279083e4fa9cSHugh Dickins 
2791726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
27921da177e4SLinus Torvalds {
2793726c3342SDavid Howells 	struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
27941da177e4SLinus Torvalds 
27951da177e4SLinus Torvalds 	buf->f_type = TMPFS_MAGIC;
279609cbfeafSKirill A. Shutemov 	buf->f_bsize = PAGE_SIZE;
27971da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
27980edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
27991da177e4SLinus Torvalds 		buf->f_blocks = sbinfo->max_blocks;
280041ffe5d5SHugh Dickins 		buf->f_bavail =
280141ffe5d5SHugh Dickins 		buf->f_bfree  = sbinfo->max_blocks -
280241ffe5d5SHugh Dickins 				percpu_counter_sum(&sbinfo->used_blocks);
28030edd73b3SHugh Dickins 	}
28040edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
28051da177e4SLinus Torvalds 		buf->f_files = sbinfo->max_inodes;
28061da177e4SLinus Torvalds 		buf->f_ffree = sbinfo->free_inodes;
28071da177e4SLinus Torvalds 	}
28081da177e4SLinus Torvalds 	/* else leave those fields 0 like simple_statfs */
28091da177e4SLinus Torvalds 	return 0;
28101da177e4SLinus Torvalds }
28111da177e4SLinus Torvalds 
28121da177e4SLinus Torvalds /*
28131da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
28141da177e4SLinus Torvalds  */
28151da177e4SLinus Torvalds static int
28161a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
28171da177e4SLinus Torvalds {
28180b0a0806SHugh Dickins 	struct inode *inode;
28191da177e4SLinus Torvalds 	int error = -ENOSPC;
28201da177e4SLinus Torvalds 
2821454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
28221da177e4SLinus Torvalds 	if (inode) {
2823feda821eSChristoph Hellwig 		error = simple_acl_create(dir, inode);
2824feda821eSChristoph Hellwig 		if (error)
2825feda821eSChristoph Hellwig 			goto out_iput;
28262a7dba39SEric Paris 		error = security_inode_init_security(inode, dir,
28279d8f13baSMimi Zohar 						     &dentry->d_name,
28286d9d88d0SJarkko Sakkinen 						     shmem_initxattrs, NULL);
2829feda821eSChristoph Hellwig 		if (error && error != -EOPNOTSUPP)
2830feda821eSChristoph Hellwig 			goto out_iput;
283137ec43cdSMimi Zohar 
2832718deb6bSAl Viro 		error = 0;
28331da177e4SLinus Torvalds 		dir->i_size += BOGO_DIRENT_SIZE;
2834078cd827SDeepa Dinamani 		dir->i_ctime = dir->i_mtime = current_time(dir);
28351da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
28361da177e4SLinus Torvalds 		dget(dentry); /* Extra count - pin the dentry in core */
28371da177e4SLinus Torvalds 	}
28381da177e4SLinus Torvalds 	return error;
2839feda821eSChristoph Hellwig out_iput:
2840feda821eSChristoph Hellwig 	iput(inode);
2841feda821eSChristoph Hellwig 	return error;
28421da177e4SLinus Torvalds }
28431da177e4SLinus Torvalds 
284460545d0dSAl Viro static int
284560545d0dSAl Viro shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
284660545d0dSAl Viro {
284760545d0dSAl Viro 	struct inode *inode;
284860545d0dSAl Viro 	int error = -ENOSPC;
284960545d0dSAl Viro 
285060545d0dSAl Viro 	inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
285160545d0dSAl Viro 	if (inode) {
285260545d0dSAl Viro 		error = security_inode_init_security(inode, dir,
285360545d0dSAl Viro 						     NULL,
285460545d0dSAl Viro 						     shmem_initxattrs, NULL);
2855feda821eSChristoph Hellwig 		if (error && error != -EOPNOTSUPP)
2856feda821eSChristoph Hellwig 			goto out_iput;
2857feda821eSChristoph Hellwig 		error = simple_acl_create(dir, inode);
2858feda821eSChristoph Hellwig 		if (error)
2859feda821eSChristoph Hellwig 			goto out_iput;
286060545d0dSAl Viro 		d_tmpfile(dentry, inode);
286160545d0dSAl Viro 	}
286260545d0dSAl Viro 	return error;
2863feda821eSChristoph Hellwig out_iput:
2864feda821eSChristoph Hellwig 	iput(inode);
2865feda821eSChristoph Hellwig 	return error;
286660545d0dSAl Viro }
286760545d0dSAl Viro 
286818bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
28691da177e4SLinus Torvalds {
28701da177e4SLinus Torvalds 	int error;
28711da177e4SLinus Torvalds 
28721da177e4SLinus Torvalds 	if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
28731da177e4SLinus Torvalds 		return error;
2874d8c76e6fSDave Hansen 	inc_nlink(dir);
28751da177e4SLinus Torvalds 	return 0;
28761da177e4SLinus Torvalds }
28771da177e4SLinus Torvalds 
28784acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2879ebfc3b49SAl Viro 		bool excl)
28801da177e4SLinus Torvalds {
28811da177e4SLinus Torvalds 	return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
28821da177e4SLinus Torvalds }
28831da177e4SLinus Torvalds 
28841da177e4SLinus Torvalds /*
28851da177e4SLinus Torvalds  * Link a file..
28861da177e4SLinus Torvalds  */
28871da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
28881da177e4SLinus Torvalds {
288975c3cfa8SDavid Howells 	struct inode *inode = d_inode(old_dentry);
289029b00e60SDarrick J. Wong 	int ret = 0;
28911da177e4SLinus Torvalds 
28921da177e4SLinus Torvalds 	/*
28931da177e4SLinus Torvalds 	 * No ordinary (disk based) filesystem counts links as inodes;
28941da177e4SLinus Torvalds 	 * but each new link needs a new dentry, pinning lowmem, and
28951da177e4SLinus Torvalds 	 * tmpfs dentries cannot be pruned until they are unlinked.
28961062af92SDarrick J. Wong 	 * But if an O_TMPFILE file is linked into the tmpfs, the
28971062af92SDarrick J. Wong 	 * first link must skip that, to get the accounting right.
28981da177e4SLinus Torvalds 	 */
28991062af92SDarrick J. Wong 	if (inode->i_nlink) {
29005b04c689SPavel Emelyanov 		ret = shmem_reserve_inode(inode->i_sb);
29015b04c689SPavel Emelyanov 		if (ret)
29025b04c689SPavel Emelyanov 			goto out;
29031062af92SDarrick J. Wong 	}
29041da177e4SLinus Torvalds 
29051da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
2906078cd827SDeepa Dinamani 	inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
2907d8c76e6fSDave Hansen 	inc_nlink(inode);
29087de9c6eeSAl Viro 	ihold(inode);	/* New dentry reference */
29091da177e4SLinus Torvalds 	dget(dentry);		/* Extra pinning count for the created dentry */
29101da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
29115b04c689SPavel Emelyanov out:
29125b04c689SPavel Emelyanov 	return ret;
29131da177e4SLinus Torvalds }
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry)
29161da177e4SLinus Torvalds {
291775c3cfa8SDavid Howells 	struct inode *inode = d_inode(dentry);
29181da177e4SLinus Torvalds 
29195b04c689SPavel Emelyanov 	if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
29205b04c689SPavel Emelyanov 		shmem_free_inode(inode->i_sb);
29211da177e4SLinus Torvalds 
29221da177e4SLinus Torvalds 	dir->i_size -= BOGO_DIRENT_SIZE;
2923078cd827SDeepa Dinamani 	inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
29249a53c3a7SDave Hansen 	drop_nlink(inode);
29251da177e4SLinus Torvalds 	dput(dentry);	/* Undo the count from "create" - this does all the work */
29261da177e4SLinus Torvalds 	return 0;
29271da177e4SLinus Torvalds }
29281da177e4SLinus Torvalds 
29291da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
29301da177e4SLinus Torvalds {
29311da177e4SLinus Torvalds 	if (!simple_empty(dentry))
29321da177e4SLinus Torvalds 		return -ENOTEMPTY;
29331da177e4SLinus Torvalds 
293475c3cfa8SDavid Howells 	drop_nlink(d_inode(dentry));
29359a53c3a7SDave Hansen 	drop_nlink(dir);
29361da177e4SLinus Torvalds 	return shmem_unlink(dir, dentry);
29371da177e4SLinus Torvalds }
29381da177e4SLinus Torvalds 
293937456771SMiklos Szeredi static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
294037456771SMiklos Szeredi {
2941e36cb0b8SDavid Howells 	bool old_is_dir = d_is_dir(old_dentry);
2942e36cb0b8SDavid Howells 	bool new_is_dir = d_is_dir(new_dentry);
294337456771SMiklos Szeredi 
294437456771SMiklos Szeredi 	if (old_dir != new_dir && old_is_dir != new_is_dir) {
294537456771SMiklos Szeredi 		if (old_is_dir) {
294637456771SMiklos Szeredi 			drop_nlink(old_dir);
294737456771SMiklos Szeredi 			inc_nlink(new_dir);
294837456771SMiklos Szeredi 		} else {
294937456771SMiklos Szeredi 			drop_nlink(new_dir);
295037456771SMiklos Szeredi 			inc_nlink(old_dir);
295137456771SMiklos Szeredi 		}
295237456771SMiklos Szeredi 	}
295337456771SMiklos Szeredi 	old_dir->i_ctime = old_dir->i_mtime =
295437456771SMiklos Szeredi 	new_dir->i_ctime = new_dir->i_mtime =
295575c3cfa8SDavid Howells 	d_inode(old_dentry)->i_ctime =
2956078cd827SDeepa Dinamani 	d_inode(new_dentry)->i_ctime = current_time(old_dir);
295737456771SMiklos Szeredi 
295837456771SMiklos Szeredi 	return 0;
295937456771SMiklos Szeredi }
296037456771SMiklos Szeredi 
296146fdb794SMiklos Szeredi static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
296246fdb794SMiklos Szeredi {
296346fdb794SMiklos Szeredi 	struct dentry *whiteout;
296446fdb794SMiklos Szeredi 	int error;
296546fdb794SMiklos Szeredi 
296646fdb794SMiklos Szeredi 	whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
296746fdb794SMiklos Szeredi 	if (!whiteout)
296846fdb794SMiklos Szeredi 		return -ENOMEM;
296946fdb794SMiklos Szeredi 
297046fdb794SMiklos Szeredi 	error = shmem_mknod(old_dir, whiteout,
297146fdb794SMiklos Szeredi 			    S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
297246fdb794SMiklos Szeredi 	dput(whiteout);
297346fdb794SMiklos Szeredi 	if (error)
297446fdb794SMiklos Szeredi 		return error;
297546fdb794SMiklos Szeredi 
297646fdb794SMiklos Szeredi 	/*
297746fdb794SMiklos Szeredi 	 * Cheat and hash the whiteout while the old dentry is still in
297846fdb794SMiklos Szeredi 	 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
297946fdb794SMiklos Szeredi 	 *
298046fdb794SMiklos Szeredi 	 * d_lookup() will consistently find one of them at this point,
298146fdb794SMiklos Szeredi 	 * not sure which one, but that isn't even important.
298246fdb794SMiklos Szeredi 	 */
298346fdb794SMiklos Szeredi 	d_rehash(whiteout);
298446fdb794SMiklos Szeredi 	return 0;
298546fdb794SMiklos Szeredi }
298646fdb794SMiklos Szeredi 
29871da177e4SLinus Torvalds /*
29881da177e4SLinus Torvalds  * The VFS layer already does all the dentry stuff for rename,
29891da177e4SLinus Torvalds  * we just have to decrement the usage count for the target if
29901da177e4SLinus Torvalds  * it exists so that the VFS layer correctly free's it when it
29911da177e4SLinus Torvalds  * gets overwritten.
29921da177e4SLinus Torvalds  */
29933b69ff51SMiklos Szeredi static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags)
29941da177e4SLinus Torvalds {
299575c3cfa8SDavid Howells 	struct inode *inode = d_inode(old_dentry);
29961da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(inode->i_mode);
29971da177e4SLinus Torvalds 
299846fdb794SMiklos Szeredi 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
29993b69ff51SMiklos Szeredi 		return -EINVAL;
30003b69ff51SMiklos Szeredi 
300137456771SMiklos Szeredi 	if (flags & RENAME_EXCHANGE)
300237456771SMiklos Szeredi 		return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
300337456771SMiklos Szeredi 
30041da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
30051da177e4SLinus Torvalds 		return -ENOTEMPTY;
30061da177e4SLinus Torvalds 
300746fdb794SMiklos Szeredi 	if (flags & RENAME_WHITEOUT) {
300846fdb794SMiklos Szeredi 		int error;
300946fdb794SMiklos Szeredi 
301046fdb794SMiklos Szeredi 		error = shmem_whiteout(old_dir, old_dentry);
301146fdb794SMiklos Szeredi 		if (error)
301246fdb794SMiklos Szeredi 			return error;
301346fdb794SMiklos Szeredi 	}
301446fdb794SMiklos Szeredi 
301575c3cfa8SDavid Howells 	if (d_really_is_positive(new_dentry)) {
30161da177e4SLinus Torvalds 		(void) shmem_unlink(new_dir, new_dentry);
3017b928095bSMiklos Szeredi 		if (they_are_dirs) {
301875c3cfa8SDavid Howells 			drop_nlink(d_inode(new_dentry));
30199a53c3a7SDave Hansen 			drop_nlink(old_dir);
3020b928095bSMiklos Szeredi 		}
30211da177e4SLinus Torvalds 	} else if (they_are_dirs) {
30229a53c3a7SDave Hansen 		drop_nlink(old_dir);
3023d8c76e6fSDave Hansen 		inc_nlink(new_dir);
30241da177e4SLinus Torvalds 	}
30251da177e4SLinus Torvalds 
30261da177e4SLinus Torvalds 	old_dir->i_size -= BOGO_DIRENT_SIZE;
30271da177e4SLinus Torvalds 	new_dir->i_size += BOGO_DIRENT_SIZE;
30281da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime =
30291da177e4SLinus Torvalds 	new_dir->i_ctime = new_dir->i_mtime =
3030078cd827SDeepa Dinamani 	inode->i_ctime = current_time(old_dir);
30311da177e4SLinus Torvalds 	return 0;
30321da177e4SLinus Torvalds }
30331da177e4SLinus Torvalds 
30341da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
30351da177e4SLinus Torvalds {
30361da177e4SLinus Torvalds 	int error;
30371da177e4SLinus Torvalds 	int len;
30381da177e4SLinus Torvalds 	struct inode *inode;
30399276aad6SHugh Dickins 	struct page *page;
30401da177e4SLinus Torvalds 
30411da177e4SLinus Torvalds 	len = strlen(symname) + 1;
304209cbfeafSKirill A. Shutemov 	if (len > PAGE_SIZE)
30431da177e4SLinus Torvalds 		return -ENAMETOOLONG;
30441da177e4SLinus Torvalds 
30450825a6f9SJoe Perches 	inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0,
30460825a6f9SJoe Perches 				VM_NORESERVE);
30471da177e4SLinus Torvalds 	if (!inode)
30481da177e4SLinus Torvalds 		return -ENOSPC;
30491da177e4SLinus Torvalds 
30509d8f13baSMimi Zohar 	error = security_inode_init_security(inode, dir, &dentry->d_name,
30516d9d88d0SJarkko Sakkinen 					     shmem_initxattrs, NULL);
3052570bc1c2SStephen Smalley 	if (error) {
3053570bc1c2SStephen Smalley 		if (error != -EOPNOTSUPP) {
3054570bc1c2SStephen Smalley 			iput(inode);
3055570bc1c2SStephen Smalley 			return error;
3056570bc1c2SStephen Smalley 		}
3057570bc1c2SStephen Smalley 		error = 0;
3058570bc1c2SStephen Smalley 	}
3059570bc1c2SStephen Smalley 
30601da177e4SLinus Torvalds 	inode->i_size = len-1;
306169f07ec9SHugh Dickins 	if (len <= SHORT_SYMLINK_LEN) {
30623ed47db3SAl Viro 		inode->i_link = kmemdup(symname, len, GFP_KERNEL);
30633ed47db3SAl Viro 		if (!inode->i_link) {
306469f07ec9SHugh Dickins 			iput(inode);
306569f07ec9SHugh Dickins 			return -ENOMEM;
306669f07ec9SHugh Dickins 		}
306769f07ec9SHugh Dickins 		inode->i_op = &shmem_short_symlink_operations;
30681da177e4SLinus Torvalds 	} else {
3069e8ecde25SAl Viro 		inode_nohighmem(inode);
30709e18eb29SAndres Lagar-Cavilla 		error = shmem_getpage(inode, 0, &page, SGP_WRITE);
30711da177e4SLinus Torvalds 		if (error) {
30721da177e4SLinus Torvalds 			iput(inode);
30731da177e4SLinus Torvalds 			return error;
30741da177e4SLinus Torvalds 		}
307514fcc23fSHugh Dickins 		inode->i_mapping->a_ops = &shmem_aops;
30761da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inode_operations;
307721fc61c7SAl Viro 		memcpy(page_address(page), symname, len);
3078ec9516fbSHugh Dickins 		SetPageUptodate(page);
30791da177e4SLinus Torvalds 		set_page_dirty(page);
30806746aff7SWu Fengguang 		unlock_page(page);
308109cbfeafSKirill A. Shutemov 		put_page(page);
30821da177e4SLinus Torvalds 	}
30831da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
3084078cd827SDeepa Dinamani 	dir->i_ctime = dir->i_mtime = current_time(dir);
30851da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
30861da177e4SLinus Torvalds 	dget(dentry);
30871da177e4SLinus Torvalds 	return 0;
30881da177e4SLinus Torvalds }
30891da177e4SLinus Torvalds 
3090fceef393SAl Viro static void shmem_put_link(void *arg)
3091fceef393SAl Viro {
3092fceef393SAl Viro 	mark_page_accessed(arg);
3093fceef393SAl Viro 	put_page(arg);
3094fceef393SAl Viro }
3095fceef393SAl Viro 
30966b255391SAl Viro static const char *shmem_get_link(struct dentry *dentry,
3097fceef393SAl Viro 				  struct inode *inode,
3098fceef393SAl Viro 				  struct delayed_call *done)
30991da177e4SLinus Torvalds {
31001da177e4SLinus Torvalds 	struct page *page = NULL;
31016b255391SAl Viro 	int error;
31026a6c9904SAl Viro 	if (!dentry) {
31036a6c9904SAl Viro 		page = find_get_page(inode->i_mapping, 0);
31046a6c9904SAl Viro 		if (!page)
31056b255391SAl Viro 			return ERR_PTR(-ECHILD);
31066a6c9904SAl Viro 		if (!PageUptodate(page)) {
31076a6c9904SAl Viro 			put_page(page);
31086a6c9904SAl Viro 			return ERR_PTR(-ECHILD);
31096a6c9904SAl Viro 		}
31106a6c9904SAl Viro 	} else {
31119e18eb29SAndres Lagar-Cavilla 		error = shmem_getpage(inode, 0, &page, SGP_READ);
3112680baacbSAl Viro 		if (error)
3113680baacbSAl Viro 			return ERR_PTR(error);
3114d3602444SHugh Dickins 		unlock_page(page);
31151da177e4SLinus Torvalds 	}
3116fceef393SAl Viro 	set_delayed_call(done, shmem_put_link, page);
311721fc61c7SAl Viro 	return page_address(page);
31181da177e4SLinus Torvalds }
31191da177e4SLinus Torvalds 
3120b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
3121b09e0fa4SEric Paris /*
3122b09e0fa4SEric Paris  * Superblocks without xattr inode operations may get some security.* xattr
3123b09e0fa4SEric Paris  * support from the LSM "for free". As soon as we have any other xattrs
3124b09e0fa4SEric Paris  * like ACLs, we also need to implement the security.* handlers at
3125b09e0fa4SEric Paris  * filesystem level, though.
3126b09e0fa4SEric Paris  */
3127b09e0fa4SEric Paris 
31286d9d88d0SJarkko Sakkinen /*
31296d9d88d0SJarkko Sakkinen  * Callback for security_inode_init_security() for acquiring xattrs.
31306d9d88d0SJarkko Sakkinen  */
31316d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode,
31326d9d88d0SJarkko Sakkinen 			    const struct xattr *xattr_array,
31336d9d88d0SJarkko Sakkinen 			    void *fs_info)
31346d9d88d0SJarkko Sakkinen {
31356d9d88d0SJarkko Sakkinen 	struct shmem_inode_info *info = SHMEM_I(inode);
31366d9d88d0SJarkko Sakkinen 	const struct xattr *xattr;
313738f38657SAristeu Rozanski 	struct simple_xattr *new_xattr;
31386d9d88d0SJarkko Sakkinen 	size_t len;
31396d9d88d0SJarkko Sakkinen 
31406d9d88d0SJarkko Sakkinen 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
314138f38657SAristeu Rozanski 		new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
31426d9d88d0SJarkko Sakkinen 		if (!new_xattr)
31436d9d88d0SJarkko Sakkinen 			return -ENOMEM;
31446d9d88d0SJarkko Sakkinen 
31456d9d88d0SJarkko Sakkinen 		len = strlen(xattr->name) + 1;
31466d9d88d0SJarkko Sakkinen 		new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
31476d9d88d0SJarkko Sakkinen 					  GFP_KERNEL);
31486d9d88d0SJarkko Sakkinen 		if (!new_xattr->name) {
31496d9d88d0SJarkko Sakkinen 			kfree(new_xattr);
31506d9d88d0SJarkko Sakkinen 			return -ENOMEM;
31516d9d88d0SJarkko Sakkinen 		}
31526d9d88d0SJarkko Sakkinen 
31536d9d88d0SJarkko Sakkinen 		memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
31546d9d88d0SJarkko Sakkinen 		       XATTR_SECURITY_PREFIX_LEN);
31556d9d88d0SJarkko Sakkinen 		memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
31566d9d88d0SJarkko Sakkinen 		       xattr->name, len);
31576d9d88d0SJarkko Sakkinen 
315838f38657SAristeu Rozanski 		simple_xattr_list_add(&info->xattrs, new_xattr);
31596d9d88d0SJarkko Sakkinen 	}
31606d9d88d0SJarkko Sakkinen 
31616d9d88d0SJarkko Sakkinen 	return 0;
31626d9d88d0SJarkko Sakkinen }
31636d9d88d0SJarkko Sakkinen 
3164aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_get(const struct xattr_handler *handler,
3165b296821aSAl Viro 				   struct dentry *unused, struct inode *inode,
3166b296821aSAl Viro 				   const char *name, void *buffer, size_t size)
3167aa7c5241SAndreas Gruenbacher {
3168b296821aSAl Viro 	struct shmem_inode_info *info = SHMEM_I(inode);
3169aa7c5241SAndreas Gruenbacher 
3170aa7c5241SAndreas Gruenbacher 	name = xattr_full_name(handler, name);
3171aa7c5241SAndreas Gruenbacher 	return simple_xattr_get(&info->xattrs, name, buffer, size);
3172aa7c5241SAndreas Gruenbacher }
3173aa7c5241SAndreas Gruenbacher 
3174aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_set(const struct xattr_handler *handler,
317559301226SAl Viro 				   struct dentry *unused, struct inode *inode,
317659301226SAl Viro 				   const char *name, const void *value,
317759301226SAl Viro 				   size_t size, int flags)
3178aa7c5241SAndreas Gruenbacher {
317959301226SAl Viro 	struct shmem_inode_info *info = SHMEM_I(inode);
3180aa7c5241SAndreas Gruenbacher 
3181aa7c5241SAndreas Gruenbacher 	name = xattr_full_name(handler, name);
3182aa7c5241SAndreas Gruenbacher 	return simple_xattr_set(&info->xattrs, name, value, size, flags);
3183aa7c5241SAndreas Gruenbacher }
3184aa7c5241SAndreas Gruenbacher 
3185aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_security_xattr_handler = {
3186aa7c5241SAndreas Gruenbacher 	.prefix = XATTR_SECURITY_PREFIX,
3187aa7c5241SAndreas Gruenbacher 	.get = shmem_xattr_handler_get,
3188aa7c5241SAndreas Gruenbacher 	.set = shmem_xattr_handler_set,
3189aa7c5241SAndreas Gruenbacher };
3190aa7c5241SAndreas Gruenbacher 
3191aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_trusted_xattr_handler = {
3192aa7c5241SAndreas Gruenbacher 	.prefix = XATTR_TRUSTED_PREFIX,
3193aa7c5241SAndreas Gruenbacher 	.get = shmem_xattr_handler_get,
3194aa7c5241SAndreas Gruenbacher 	.set = shmem_xattr_handler_set,
3195aa7c5241SAndreas Gruenbacher };
3196aa7c5241SAndreas Gruenbacher 
3197b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = {
3198b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
3199feda821eSChristoph Hellwig 	&posix_acl_access_xattr_handler,
3200feda821eSChristoph Hellwig 	&posix_acl_default_xattr_handler,
3201b09e0fa4SEric Paris #endif
3202aa7c5241SAndreas Gruenbacher 	&shmem_security_xattr_handler,
3203aa7c5241SAndreas Gruenbacher 	&shmem_trusted_xattr_handler,
3204b09e0fa4SEric Paris 	NULL
3205b09e0fa4SEric Paris };
3206b09e0fa4SEric Paris 
3207b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3208b09e0fa4SEric Paris {
320975c3cfa8SDavid Howells 	struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
3210786534b9SAndreas Gruenbacher 	return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
3211b09e0fa4SEric Paris }
3212b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */
3213b09e0fa4SEric Paris 
321469f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = {
32156b255391SAl Viro 	.get_link	= simple_get_link,
3216b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
3217b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
3218b09e0fa4SEric Paris #endif
32191da177e4SLinus Torvalds };
32201da177e4SLinus Torvalds 
322192e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = {
32226b255391SAl Viro 	.get_link	= shmem_get_link,
3223b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
3224b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
322539f0247dSAndreas Gruenbacher #endif
3226b09e0fa4SEric Paris };
322739f0247dSAndreas Gruenbacher 
322891828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child)
322991828a40SDavid M. Grimes {
323091828a40SDavid M. Grimes 	return ERR_PTR(-ESTALE);
323191828a40SDavid M. Grimes }
323291828a40SDavid M. Grimes 
323391828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh)
323491828a40SDavid M. Grimes {
323591828a40SDavid M. Grimes 	__u32 *fh = vfh;
323691828a40SDavid M. Grimes 	__u64 inum = fh[2];
323791828a40SDavid M. Grimes 	inum = (inum << 32) | fh[1];
323891828a40SDavid M. Grimes 	return ino->i_ino == inum && fh[0] == ino->i_generation;
323991828a40SDavid M. Grimes }
324091828a40SDavid M. Grimes 
324112ba780dSAmir Goldstein /* Find any alias of inode, but prefer a hashed alias */
324212ba780dSAmir Goldstein static struct dentry *shmem_find_alias(struct inode *inode)
324312ba780dSAmir Goldstein {
324412ba780dSAmir Goldstein 	struct dentry *alias = d_find_alias(inode);
324512ba780dSAmir Goldstein 
324612ba780dSAmir Goldstein 	return alias ?: d_find_any_alias(inode);
324712ba780dSAmir Goldstein }
324812ba780dSAmir Goldstein 
324912ba780dSAmir Goldstein 
3250480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3251480b116cSChristoph Hellwig 		struct fid *fid, int fh_len, int fh_type)
325291828a40SDavid M. Grimes {
325391828a40SDavid M. Grimes 	struct inode *inode;
3254480b116cSChristoph Hellwig 	struct dentry *dentry = NULL;
325535c2a7f4SHugh Dickins 	u64 inum;
325691828a40SDavid M. Grimes 
3257480b116cSChristoph Hellwig 	if (fh_len < 3)
3258480b116cSChristoph Hellwig 		return NULL;
3259480b116cSChristoph Hellwig 
326035c2a7f4SHugh Dickins 	inum = fid->raw[2];
326135c2a7f4SHugh Dickins 	inum = (inum << 32) | fid->raw[1];
326235c2a7f4SHugh Dickins 
3263480b116cSChristoph Hellwig 	inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3264480b116cSChristoph Hellwig 			shmem_match, fid->raw);
326591828a40SDavid M. Grimes 	if (inode) {
326612ba780dSAmir Goldstein 		dentry = shmem_find_alias(inode);
326791828a40SDavid M. Grimes 		iput(inode);
326891828a40SDavid M. Grimes 	}
326991828a40SDavid M. Grimes 
3270480b116cSChristoph Hellwig 	return dentry;
327191828a40SDavid M. Grimes }
327291828a40SDavid M. Grimes 
3273b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3274b0b0382bSAl Viro 				struct inode *parent)
327591828a40SDavid M. Grimes {
32765fe0c237SAneesh Kumar K.V 	if (*len < 3) {
32775fe0c237SAneesh Kumar K.V 		*len = 3;
327894e07a75SNamjae Jeon 		return FILEID_INVALID;
32795fe0c237SAneesh Kumar K.V 	}
328091828a40SDavid M. Grimes 
32811d3382cbSAl Viro 	if (inode_unhashed(inode)) {
328291828a40SDavid M. Grimes 		/* Unfortunately insert_inode_hash is not idempotent,
328391828a40SDavid M. Grimes 		 * so as we hash inodes here rather than at creation
328491828a40SDavid M. Grimes 		 * time, we need a lock to ensure we only try
328591828a40SDavid M. Grimes 		 * to do it once
328691828a40SDavid M. Grimes 		 */
328791828a40SDavid M. Grimes 		static DEFINE_SPINLOCK(lock);
328891828a40SDavid M. Grimes 		spin_lock(&lock);
32891d3382cbSAl Viro 		if (inode_unhashed(inode))
329091828a40SDavid M. Grimes 			__insert_inode_hash(inode,
329191828a40SDavid M. Grimes 					    inode->i_ino + inode->i_generation);
329291828a40SDavid M. Grimes 		spin_unlock(&lock);
329391828a40SDavid M. Grimes 	}
329491828a40SDavid M. Grimes 
329591828a40SDavid M. Grimes 	fh[0] = inode->i_generation;
329691828a40SDavid M. Grimes 	fh[1] = inode->i_ino;
329791828a40SDavid M. Grimes 	fh[2] = ((__u64)inode->i_ino) >> 32;
329891828a40SDavid M. Grimes 
329991828a40SDavid M. Grimes 	*len = 3;
330091828a40SDavid M. Grimes 	return 1;
330191828a40SDavid M. Grimes }
330291828a40SDavid M. Grimes 
330339655164SChristoph Hellwig static const struct export_operations shmem_export_ops = {
330491828a40SDavid M. Grimes 	.get_parent     = shmem_get_parent,
330591828a40SDavid M. Grimes 	.encode_fh      = shmem_encode_fh,
3306480b116cSChristoph Hellwig 	.fh_to_dentry	= shmem_fh_to_dentry,
330791828a40SDavid M. Grimes };
330891828a40SDavid M. Grimes 
3309680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
3310680d794bSakpm@linux-foundation.org 			       bool remount)
33111da177e4SLinus Torvalds {
33121da177e4SLinus Torvalds 	char *this_char, *value, *rest;
331349cd0a5cSGreg Thelen 	struct mempolicy *mpol = NULL;
33148751e039SEric W. Biederman 	uid_t uid;
33158751e039SEric W. Biederman 	gid_t gid;
33161da177e4SLinus Torvalds 
3317b00dc3adSHugh Dickins 	while (options != NULL) {
3318b00dc3adSHugh Dickins 		this_char = options;
3319b00dc3adSHugh Dickins 		for (;;) {
3320b00dc3adSHugh Dickins 			/*
3321b00dc3adSHugh Dickins 			 * NUL-terminate this option: unfortunately,
3322b00dc3adSHugh Dickins 			 * mount options form a comma-separated list,
3323b00dc3adSHugh Dickins 			 * but mpol's nodelist may also contain commas.
3324b00dc3adSHugh Dickins 			 */
3325b00dc3adSHugh Dickins 			options = strchr(options, ',');
3326b00dc3adSHugh Dickins 			if (options == NULL)
3327b00dc3adSHugh Dickins 				break;
3328b00dc3adSHugh Dickins 			options++;
3329b00dc3adSHugh Dickins 			if (!isdigit(*options)) {
3330b00dc3adSHugh Dickins 				options[-1] = '\0';
3331b00dc3adSHugh Dickins 				break;
3332b00dc3adSHugh Dickins 			}
3333b00dc3adSHugh Dickins 		}
33341da177e4SLinus Torvalds 		if (!*this_char)
33351da177e4SLinus Torvalds 			continue;
33361da177e4SLinus Torvalds 		if ((value = strchr(this_char,'=')) != NULL) {
33371da177e4SLinus Torvalds 			*value++ = 0;
33381da177e4SLinus Torvalds 		} else {
33391170532bSJoe Perches 			pr_err("tmpfs: No value for mount option '%s'\n",
33401da177e4SLinus Torvalds 			       this_char);
334149cd0a5cSGreg Thelen 			goto error;
33421da177e4SLinus Torvalds 		}
33431da177e4SLinus Torvalds 
33441da177e4SLinus Torvalds 		if (!strcmp(this_char,"size")) {
33451da177e4SLinus Torvalds 			unsigned long long size;
33461da177e4SLinus Torvalds 			size = memparse(value,&rest);
33471da177e4SLinus Torvalds 			if (*rest == '%') {
33481da177e4SLinus Torvalds 				size <<= PAGE_SHIFT;
3349ca79b0c2SArun KS 				size *= totalram_pages();
33501da177e4SLinus Torvalds 				do_div(size, 100);
33511da177e4SLinus Torvalds 				rest++;
33521da177e4SLinus Torvalds 			}
33531da177e4SLinus Torvalds 			if (*rest)
33541da177e4SLinus Torvalds 				goto bad_val;
3355680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks =
335609cbfeafSKirill A. Shutemov 				DIV_ROUND_UP(size, PAGE_SIZE);
33571da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_blocks")) {
3358680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks = memparse(value, &rest);
33591da177e4SLinus Torvalds 			if (*rest)
33601da177e4SLinus Torvalds 				goto bad_val;
33611da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_inodes")) {
3362680d794bSakpm@linux-foundation.org 			sbinfo->max_inodes = memparse(value, &rest);
33631da177e4SLinus Torvalds 			if (*rest)
33641da177e4SLinus Torvalds 				goto bad_val;
33651da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"mode")) {
3366680d794bSakpm@linux-foundation.org 			if (remount)
33671da177e4SLinus Torvalds 				continue;
3368680d794bSakpm@linux-foundation.org 			sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
33691da177e4SLinus Torvalds 			if (*rest)
33701da177e4SLinus Torvalds 				goto bad_val;
33711da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"uid")) {
3372680d794bSakpm@linux-foundation.org 			if (remount)
33731da177e4SLinus Torvalds 				continue;
33748751e039SEric W. Biederman 			uid = simple_strtoul(value, &rest, 0);
33751da177e4SLinus Torvalds 			if (*rest)
33761da177e4SLinus Torvalds 				goto bad_val;
33778751e039SEric W. Biederman 			sbinfo->uid = make_kuid(current_user_ns(), uid);
33788751e039SEric W. Biederman 			if (!uid_valid(sbinfo->uid))
33798751e039SEric W. Biederman 				goto bad_val;
33801da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"gid")) {
3381680d794bSakpm@linux-foundation.org 			if (remount)
33821da177e4SLinus Torvalds 				continue;
33838751e039SEric W. Biederman 			gid = simple_strtoul(value, &rest, 0);
33841da177e4SLinus Torvalds 			if (*rest)
33851da177e4SLinus Torvalds 				goto bad_val;
33868751e039SEric W. Biederman 			sbinfo->gid = make_kgid(current_user_ns(), gid);
33878751e039SEric W. Biederman 			if (!gid_valid(sbinfo->gid))
33888751e039SEric W. Biederman 				goto bad_val;
3389e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
33905a6e75f8SKirill A. Shutemov 		} else if (!strcmp(this_char, "huge")) {
33915a6e75f8SKirill A. Shutemov 			int huge;
33925a6e75f8SKirill A. Shutemov 			huge = shmem_parse_huge(value);
33935a6e75f8SKirill A. Shutemov 			if (huge < 0)
33945a6e75f8SKirill A. Shutemov 				goto bad_val;
33955a6e75f8SKirill A. Shutemov 			if (!has_transparent_hugepage() &&
33965a6e75f8SKirill A. Shutemov 					huge != SHMEM_HUGE_NEVER)
33975a6e75f8SKirill A. Shutemov 				goto bad_val;
33985a6e75f8SKirill A. Shutemov 			sbinfo->huge = huge;
33995a6e75f8SKirill A. Shutemov #endif
34005a6e75f8SKirill A. Shutemov #ifdef CONFIG_NUMA
34017339ff83SRobin Holt 		} else if (!strcmp(this_char,"mpol")) {
340249cd0a5cSGreg Thelen 			mpol_put(mpol);
340349cd0a5cSGreg Thelen 			mpol = NULL;
340449cd0a5cSGreg Thelen 			if (mpol_parse_str(value, &mpol))
34057339ff83SRobin Holt 				goto bad_val;
34065a6e75f8SKirill A. Shutemov #endif
34071da177e4SLinus Torvalds 		} else {
34081170532bSJoe Perches 			pr_err("tmpfs: Bad mount option %s\n", this_char);
340949cd0a5cSGreg Thelen 			goto error;
34101da177e4SLinus Torvalds 		}
34111da177e4SLinus Torvalds 	}
341249cd0a5cSGreg Thelen 	sbinfo->mpol = mpol;
34131da177e4SLinus Torvalds 	return 0;
34141da177e4SLinus Torvalds 
34151da177e4SLinus Torvalds bad_val:
34161170532bSJoe Perches 	pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
34171da177e4SLinus Torvalds 	       value, this_char);
341849cd0a5cSGreg Thelen error:
341949cd0a5cSGreg Thelen 	mpol_put(mpol);
34201da177e4SLinus Torvalds 	return 1;
34211da177e4SLinus Torvalds 
34221da177e4SLinus Torvalds }
34231da177e4SLinus Torvalds 
34241da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
34251da177e4SLinus Torvalds {
34261da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3427680d794bSakpm@linux-foundation.org 	struct shmem_sb_info config = *sbinfo;
34280edd73b3SHugh Dickins 	unsigned long inodes;
34290edd73b3SHugh Dickins 	int error = -EINVAL;
34301da177e4SLinus Torvalds 
34315f00110fSGreg Thelen 	config.mpol = NULL;
3432680d794bSakpm@linux-foundation.org 	if (shmem_parse_options(data, &config, true))
34330edd73b3SHugh Dickins 		return error;
34340edd73b3SHugh Dickins 
34350edd73b3SHugh Dickins 	spin_lock(&sbinfo->stat_lock);
34360edd73b3SHugh Dickins 	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
34377e496299STim Chen 	if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
34380edd73b3SHugh Dickins 		goto out;
3439680d794bSakpm@linux-foundation.org 	if (config.max_inodes < inodes)
34400edd73b3SHugh Dickins 		goto out;
34410edd73b3SHugh Dickins 	/*
344254af6042SHugh Dickins 	 * Those tests disallow limited->unlimited while any are in use;
34430edd73b3SHugh Dickins 	 * but we must separately disallow unlimited->limited, because
34440edd73b3SHugh Dickins 	 * in that case we have no record of how much is already in use.
34450edd73b3SHugh Dickins 	 */
3446680d794bSakpm@linux-foundation.org 	if (config.max_blocks && !sbinfo->max_blocks)
34470edd73b3SHugh Dickins 		goto out;
3448680d794bSakpm@linux-foundation.org 	if (config.max_inodes && !sbinfo->max_inodes)
34490edd73b3SHugh Dickins 		goto out;
34500edd73b3SHugh Dickins 
34510edd73b3SHugh Dickins 	error = 0;
34525a6e75f8SKirill A. Shutemov 	sbinfo->huge = config.huge;
3453680d794bSakpm@linux-foundation.org 	sbinfo->max_blocks  = config.max_blocks;
3454680d794bSakpm@linux-foundation.org 	sbinfo->max_inodes  = config.max_inodes;
3455680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = config.max_inodes - inodes;
345671fe804bSLee Schermerhorn 
34575f00110fSGreg Thelen 	/*
34585f00110fSGreg Thelen 	 * Preserve previous mempolicy unless mpol remount option was specified.
34595f00110fSGreg Thelen 	 */
34605f00110fSGreg Thelen 	if (config.mpol) {
346171fe804bSLee Schermerhorn 		mpol_put(sbinfo->mpol);
346271fe804bSLee Schermerhorn 		sbinfo->mpol = config.mpol;	/* transfers initial ref */
34635f00110fSGreg Thelen 	}
34640edd73b3SHugh Dickins out:
34650edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
34660edd73b3SHugh Dickins 	return error;
34671da177e4SLinus Torvalds }
3468680d794bSakpm@linux-foundation.org 
346934c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root)
3470680d794bSakpm@linux-foundation.org {
347134c80b1dSAl Viro 	struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
3472680d794bSakpm@linux-foundation.org 
3473680d794bSakpm@linux-foundation.org 	if (sbinfo->max_blocks != shmem_default_max_blocks())
3474680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",size=%luk",
347509cbfeafSKirill A. Shutemov 			sbinfo->max_blocks << (PAGE_SHIFT - 10));
3476680d794bSakpm@linux-foundation.org 	if (sbinfo->max_inodes != shmem_default_max_inodes())
3477680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
34780825a6f9SJoe Perches 	if (sbinfo->mode != (0777 | S_ISVTX))
347909208d15SAl Viro 		seq_printf(seq, ",mode=%03ho", sbinfo->mode);
34808751e039SEric W. Biederman 	if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
34818751e039SEric W. Biederman 		seq_printf(seq, ",uid=%u",
34828751e039SEric W. Biederman 				from_kuid_munged(&init_user_ns, sbinfo->uid));
34838751e039SEric W. Biederman 	if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
34848751e039SEric W. Biederman 		seq_printf(seq, ",gid=%u",
34858751e039SEric W. Biederman 				from_kgid_munged(&init_user_ns, sbinfo->gid));
3486e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
34875a6e75f8SKirill A. Shutemov 	/* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
34885a6e75f8SKirill A. Shutemov 	if (sbinfo->huge)
34895a6e75f8SKirill A. Shutemov 		seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
34905a6e75f8SKirill A. Shutemov #endif
349171fe804bSLee Schermerhorn 	shmem_show_mpol(seq, sbinfo->mpol);
3492680d794bSakpm@linux-foundation.org 	return 0;
3493680d794bSakpm@linux-foundation.org }
34949183df25SDavid Herrmann 
3495680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
34961da177e4SLinus Torvalds 
34971da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb)
34981da177e4SLinus Torvalds {
3499602586a8SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3500602586a8SHugh Dickins 
3501602586a8SHugh Dickins 	percpu_counter_destroy(&sbinfo->used_blocks);
350249cd0a5cSGreg Thelen 	mpol_put(sbinfo->mpol);
3503602586a8SHugh Dickins 	kfree(sbinfo);
35041da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
35051da177e4SLinus Torvalds }
35061da177e4SLinus Torvalds 
35072b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent)
35081da177e4SLinus Torvalds {
35091da177e4SLinus Torvalds 	struct inode *inode;
35100edd73b3SHugh Dickins 	struct shmem_sb_info *sbinfo;
3511680d794bSakpm@linux-foundation.org 	int err = -ENOMEM;
3512680d794bSakpm@linux-foundation.org 
3513680d794bSakpm@linux-foundation.org 	/* Round up to L1_CACHE_BYTES to resist false sharing */
3514425fbf04SPekka Enberg 	sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
3515680d794bSakpm@linux-foundation.org 				L1_CACHE_BYTES), GFP_KERNEL);
3516680d794bSakpm@linux-foundation.org 	if (!sbinfo)
3517680d794bSakpm@linux-foundation.org 		return -ENOMEM;
3518680d794bSakpm@linux-foundation.org 
35190825a6f9SJoe Perches 	sbinfo->mode = 0777 | S_ISVTX;
352076aac0e9SDavid Howells 	sbinfo->uid = current_fsuid();
352176aac0e9SDavid Howells 	sbinfo->gid = current_fsgid();
3522680d794bSakpm@linux-foundation.org 	sb->s_fs_info = sbinfo;
35231da177e4SLinus Torvalds 
35240edd73b3SHugh Dickins #ifdef CONFIG_TMPFS
35251da177e4SLinus Torvalds 	/*
35261da177e4SLinus Torvalds 	 * Per default we only allow half of the physical ram per
35271da177e4SLinus Torvalds 	 * tmpfs instance, limiting inodes to one per page of lowmem;
35281da177e4SLinus Torvalds 	 * but the internal instance is left unlimited.
35291da177e4SLinus Torvalds 	 */
35301751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_KERNMOUNT)) {
3531680d794bSakpm@linux-foundation.org 		sbinfo->max_blocks = shmem_default_max_blocks();
3532680d794bSakpm@linux-foundation.org 		sbinfo->max_inodes = shmem_default_max_inodes();
3533680d794bSakpm@linux-foundation.org 		if (shmem_parse_options(data, sbinfo, false)) {
3534680d794bSakpm@linux-foundation.org 			err = -EINVAL;
3535680d794bSakpm@linux-foundation.org 			goto failed;
3536680d794bSakpm@linux-foundation.org 		}
3537ca4e0519SAl Viro 	} else {
35381751e8a6SLinus Torvalds 		sb->s_flags |= SB_NOUSER;
35391da177e4SLinus Torvalds 	}
354091828a40SDavid M. Grimes 	sb->s_export_op = &shmem_export_ops;
35411751e8a6SLinus Torvalds 	sb->s_flags |= SB_NOSEC;
35420edd73b3SHugh Dickins #else
35431751e8a6SLinus Torvalds 	sb->s_flags |= SB_NOUSER;
35440edd73b3SHugh Dickins #endif
35451da177e4SLinus Torvalds 
35461da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
3547908c7f19STejun Heo 	if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
3548602586a8SHugh Dickins 		goto failed;
3549680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = sbinfo->max_inodes;
3550779750d2SKirill A. Shutemov 	spin_lock_init(&sbinfo->shrinklist_lock);
3551779750d2SKirill A. Shutemov 	INIT_LIST_HEAD(&sbinfo->shrinklist);
35521da177e4SLinus Torvalds 
3553285b2c4fSHugh Dickins 	sb->s_maxbytes = MAX_LFS_FILESIZE;
355409cbfeafSKirill A. Shutemov 	sb->s_blocksize = PAGE_SIZE;
355509cbfeafSKirill A. Shutemov 	sb->s_blocksize_bits = PAGE_SHIFT;
35561da177e4SLinus Torvalds 	sb->s_magic = TMPFS_MAGIC;
35571da177e4SLinus Torvalds 	sb->s_op = &shmem_ops;
3558cfd95a9cSRobin H. Johnson 	sb->s_time_gran = 1;
3559b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
356039f0247dSAndreas Gruenbacher 	sb->s_xattr = shmem_xattr_handlers;
3561b09e0fa4SEric Paris #endif
3562b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
35631751e8a6SLinus Torvalds 	sb->s_flags |= SB_POSIXACL;
356439f0247dSAndreas Gruenbacher #endif
35652b4db796SAmir Goldstein 	uuid_gen(&sb->s_uuid);
35660edd73b3SHugh Dickins 
3567454abafeSDmitry Monakhov 	inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
35681da177e4SLinus Torvalds 	if (!inode)
35691da177e4SLinus Torvalds 		goto failed;
3570680d794bSakpm@linux-foundation.org 	inode->i_uid = sbinfo->uid;
3571680d794bSakpm@linux-foundation.org 	inode->i_gid = sbinfo->gid;
3572318ceed0SAl Viro 	sb->s_root = d_make_root(inode);
3573318ceed0SAl Viro 	if (!sb->s_root)
357448fde701SAl Viro 		goto failed;
35751da177e4SLinus Torvalds 	return 0;
35761da177e4SLinus Torvalds 
35771da177e4SLinus Torvalds failed:
35781da177e4SLinus Torvalds 	shmem_put_super(sb);
35791da177e4SLinus Torvalds 	return err;
35801da177e4SLinus Torvalds }
35811da177e4SLinus Torvalds 
3582fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep;
35831da177e4SLinus Torvalds 
35841da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb)
35851da177e4SLinus Torvalds {
358641ffe5d5SHugh Dickins 	struct shmem_inode_info *info;
358741ffe5d5SHugh Dickins 	info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
358841ffe5d5SHugh Dickins 	if (!info)
35891da177e4SLinus Torvalds 		return NULL;
359041ffe5d5SHugh Dickins 	return &info->vfs_inode;
35911da177e4SLinus Torvalds }
35921da177e4SLinus Torvalds 
359341ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head)
3594fa0d7e3dSNick Piggin {
3595fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
359684e710daSAl Viro 	if (S_ISLNK(inode->i_mode))
35973ed47db3SAl Viro 		kfree(inode->i_link);
3598fa0d7e3dSNick Piggin 	kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3599fa0d7e3dSNick Piggin }
3600fa0d7e3dSNick Piggin 
36011da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode)
36021da177e4SLinus Torvalds {
360309208d15SAl Viro 	if (S_ISREG(inode->i_mode))
36041da177e4SLinus Torvalds 		mpol_free_shared_policy(&SHMEM_I(inode)->policy);
360541ffe5d5SHugh Dickins 	call_rcu(&inode->i_rcu, shmem_destroy_callback);
36061da177e4SLinus Torvalds }
36071da177e4SLinus Torvalds 
360841ffe5d5SHugh Dickins static void shmem_init_inode(void *foo)
36091da177e4SLinus Torvalds {
361041ffe5d5SHugh Dickins 	struct shmem_inode_info *info = foo;
361141ffe5d5SHugh Dickins 	inode_init_once(&info->vfs_inode);
36121da177e4SLinus Torvalds }
36131da177e4SLinus Torvalds 
36149a8ec03eSweiping zhang static void shmem_init_inodecache(void)
36151da177e4SLinus Torvalds {
36161da177e4SLinus Torvalds 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
36171da177e4SLinus Torvalds 				sizeof(struct shmem_inode_info),
36185d097056SVladimir Davydov 				0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
36191da177e4SLinus Torvalds }
36201da177e4SLinus Torvalds 
362141ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void)
36221da177e4SLinus Torvalds {
36231a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(shmem_inode_cachep);
36241da177e4SLinus Torvalds }
36251da177e4SLinus Torvalds 
3626f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = {
36271da177e4SLinus Torvalds 	.writepage	= shmem_writepage,
362876719325SKen Chen 	.set_page_dirty	= __set_page_dirty_no_writeback,
36291da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
3630800d15a5SNick Piggin 	.write_begin	= shmem_write_begin,
3631800d15a5SNick Piggin 	.write_end	= shmem_write_end,
36321da177e4SLinus Torvalds #endif
36331c93923cSAndrew Morton #ifdef CONFIG_MIGRATION
3634304dbdb7SLee Schermerhorn 	.migratepage	= migrate_page,
36351c93923cSAndrew Morton #endif
3636aa261f54SAndi Kleen 	.error_remove_page = generic_error_remove_page,
36371da177e4SLinus Torvalds };
36381da177e4SLinus Torvalds 
363915ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = {
36401da177e4SLinus Torvalds 	.mmap		= shmem_mmap,
3641c01d5b30SHugh Dickins 	.get_unmapped_area = shmem_get_unmapped_area,
36421da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
3643220f2ac9SHugh Dickins 	.llseek		= shmem_file_llseek,
36442ba5bbedSAl Viro 	.read_iter	= shmem_file_read_iter,
36458174202bSAl Viro 	.write_iter	= generic_file_write_iter,
36461b061d92SChristoph Hellwig 	.fsync		= noop_fsync,
364782c156f8SAl Viro 	.splice_read	= generic_file_splice_read,
3648f6cb85d0SAl Viro 	.splice_write	= iter_file_splice_write,
364983e4fa9cSHugh Dickins 	.fallocate	= shmem_fallocate,
36501da177e4SLinus Torvalds #endif
36511da177e4SLinus Torvalds };
36521da177e4SLinus Torvalds 
365392e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = {
365444a30220SYu Zhao 	.getattr	= shmem_getattr,
365594c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
3656b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
3657b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
3658feda821eSChristoph Hellwig 	.set_acl	= simple_set_acl,
3659b09e0fa4SEric Paris #endif
36601da177e4SLinus Torvalds };
36611da177e4SLinus Torvalds 
366292e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = {
36631da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
36641da177e4SLinus Torvalds 	.create		= shmem_create,
36651da177e4SLinus Torvalds 	.lookup		= simple_lookup,
36661da177e4SLinus Torvalds 	.link		= shmem_link,
36671da177e4SLinus Torvalds 	.unlink		= shmem_unlink,
36681da177e4SLinus Torvalds 	.symlink	= shmem_symlink,
36691da177e4SLinus Torvalds 	.mkdir		= shmem_mkdir,
36701da177e4SLinus Torvalds 	.rmdir		= shmem_rmdir,
36711da177e4SLinus Torvalds 	.mknod		= shmem_mknod,
36722773bf00SMiklos Szeredi 	.rename		= shmem_rename2,
367360545d0dSAl Viro 	.tmpfile	= shmem_tmpfile,
36741da177e4SLinus Torvalds #endif
3675b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
3676b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
3677b09e0fa4SEric Paris #endif
367839f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
367994c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
3680feda821eSChristoph Hellwig 	.set_acl	= simple_set_acl,
368139f0247dSAndreas Gruenbacher #endif
368239f0247dSAndreas Gruenbacher };
368339f0247dSAndreas Gruenbacher 
368492e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = {
3685b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
3686b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
3687b09e0fa4SEric Paris #endif
368839f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
368994c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
3690feda821eSChristoph Hellwig 	.set_acl	= simple_set_acl,
369139f0247dSAndreas Gruenbacher #endif
36921da177e4SLinus Torvalds };
36931da177e4SLinus Torvalds 
3694759b9775SHugh Dickins static const struct super_operations shmem_ops = {
36951da177e4SLinus Torvalds 	.alloc_inode	= shmem_alloc_inode,
36961da177e4SLinus Torvalds 	.destroy_inode	= shmem_destroy_inode,
36971da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
36981da177e4SLinus Torvalds 	.statfs		= shmem_statfs,
36991da177e4SLinus Torvalds 	.remount_fs	= shmem_remount_fs,
3700680d794bSakpm@linux-foundation.org 	.show_options	= shmem_show_options,
37011da177e4SLinus Torvalds #endif
37021f895f75SAl Viro 	.evict_inode	= shmem_evict_inode,
37031da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
37041da177e4SLinus Torvalds 	.put_super	= shmem_put_super,
3705779750d2SKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3706779750d2SKirill A. Shutemov 	.nr_cached_objects	= shmem_unused_huge_count,
3707779750d2SKirill A. Shutemov 	.free_cached_objects	= shmem_unused_huge_scan,
3708779750d2SKirill A. Shutemov #endif
37091da177e4SLinus Torvalds };
37101da177e4SLinus Torvalds 
3711f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = {
371254cb8821SNick Piggin 	.fault		= shmem_fault,
3713d7c17551SNing Qu 	.map_pages	= filemap_map_pages,
37141da177e4SLinus Torvalds #ifdef CONFIG_NUMA
37151da177e4SLinus Torvalds 	.set_policy     = shmem_set_policy,
37161da177e4SLinus Torvalds 	.get_policy     = shmem_get_policy,
37171da177e4SLinus Torvalds #endif
37181da177e4SLinus Torvalds };
37191da177e4SLinus Torvalds 
37203c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type,
37213c26ff6eSAl Viro 	int flags, const char *dev_name, void *data)
37221da177e4SLinus Torvalds {
37233c26ff6eSAl Viro 	return mount_nodev(fs_type, flags, data, shmem_fill_super);
37241da177e4SLinus Torvalds }
37251da177e4SLinus Torvalds 
372641ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = {
37271da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
37281da177e4SLinus Torvalds 	.name		= "tmpfs",
37293c26ff6eSAl Viro 	.mount		= shmem_mount,
37301da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
37312b8576cbSEric W. Biederman 	.fs_flags	= FS_USERNS_MOUNT,
37321da177e4SLinus Torvalds };
37331da177e4SLinus Torvalds 
373441ffe5d5SHugh Dickins int __init shmem_init(void)
37351da177e4SLinus Torvalds {
37361da177e4SLinus Torvalds 	int error;
37371da177e4SLinus Torvalds 
373816203a7aSRob Landley 	/* If rootfs called this, don't re-init */
373916203a7aSRob Landley 	if (shmem_inode_cachep)
374016203a7aSRob Landley 		return 0;
374116203a7aSRob Landley 
37429a8ec03eSweiping zhang 	shmem_init_inodecache();
37431da177e4SLinus Torvalds 
374441ffe5d5SHugh Dickins 	error = register_filesystem(&shmem_fs_type);
37451da177e4SLinus Torvalds 	if (error) {
37461170532bSJoe Perches 		pr_err("Could not register tmpfs\n");
37471da177e4SLinus Torvalds 		goto out2;
37481da177e4SLinus Torvalds 	}
374995dc112aSGreg Kroah-Hartman 
3750ca4e0519SAl Viro 	shm_mnt = kern_mount(&shmem_fs_type);
37511da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt)) {
37521da177e4SLinus Torvalds 		error = PTR_ERR(shm_mnt);
37531170532bSJoe Perches 		pr_err("Could not kern_mount tmpfs\n");
37541da177e4SLinus Torvalds 		goto out1;
37551da177e4SLinus Torvalds 	}
37565a6e75f8SKirill A. Shutemov 
3757e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3758435c0b87SKirill A. Shutemov 	if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
37595a6e75f8SKirill A. Shutemov 		SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
37605a6e75f8SKirill A. Shutemov 	else
37615a6e75f8SKirill A. Shutemov 		shmem_huge = 0; /* just in case it was patched */
37625a6e75f8SKirill A. Shutemov #endif
37631da177e4SLinus Torvalds 	return 0;
37641da177e4SLinus Torvalds 
37651da177e4SLinus Torvalds out1:
376641ffe5d5SHugh Dickins 	unregister_filesystem(&shmem_fs_type);
37671da177e4SLinus Torvalds out2:
376841ffe5d5SHugh Dickins 	shmem_destroy_inodecache();
37691da177e4SLinus Torvalds 	shm_mnt = ERR_PTR(error);
37701da177e4SLinus Torvalds 	return error;
37711da177e4SLinus Torvalds }
3772853ac43aSMatt Mackall 
3773e496cf3dSKirill A. Shutemov #if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS)
37745a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_show(struct kobject *kobj,
37755a6e75f8SKirill A. Shutemov 		struct kobj_attribute *attr, char *buf)
37765a6e75f8SKirill A. Shutemov {
37775a6e75f8SKirill A. Shutemov 	int values[] = {
37785a6e75f8SKirill A. Shutemov 		SHMEM_HUGE_ALWAYS,
37795a6e75f8SKirill A. Shutemov 		SHMEM_HUGE_WITHIN_SIZE,
37805a6e75f8SKirill A. Shutemov 		SHMEM_HUGE_ADVISE,
37815a6e75f8SKirill A. Shutemov 		SHMEM_HUGE_NEVER,
37825a6e75f8SKirill A. Shutemov 		SHMEM_HUGE_DENY,
37835a6e75f8SKirill A. Shutemov 		SHMEM_HUGE_FORCE,
37845a6e75f8SKirill A. Shutemov 	};
37855a6e75f8SKirill A. Shutemov 	int i, count;
37865a6e75f8SKirill A. Shutemov 
37875a6e75f8SKirill A. Shutemov 	for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
37885a6e75f8SKirill A. Shutemov 		const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
37895a6e75f8SKirill A. Shutemov 
37905a6e75f8SKirill A. Shutemov 		count += sprintf(buf + count, fmt,
37915a6e75f8SKirill A. Shutemov 				shmem_format_huge(values[i]));
37925a6e75f8SKirill A. Shutemov 	}
37935a6e75f8SKirill A. Shutemov 	buf[count - 1] = '\n';
37945a6e75f8SKirill A. Shutemov 	return count;
37955a6e75f8SKirill A. Shutemov }
37965a6e75f8SKirill A. Shutemov 
37975a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_store(struct kobject *kobj,
37985a6e75f8SKirill A. Shutemov 		struct kobj_attribute *attr, const char *buf, size_t count)
37995a6e75f8SKirill A. Shutemov {
38005a6e75f8SKirill A. Shutemov 	char tmp[16];
38015a6e75f8SKirill A. Shutemov 	int huge;
38025a6e75f8SKirill A. Shutemov 
38035a6e75f8SKirill A. Shutemov 	if (count + 1 > sizeof(tmp))
38045a6e75f8SKirill A. Shutemov 		return -EINVAL;
38055a6e75f8SKirill A. Shutemov 	memcpy(tmp, buf, count);
38065a6e75f8SKirill A. Shutemov 	tmp[count] = '\0';
38075a6e75f8SKirill A. Shutemov 	if (count && tmp[count - 1] == '\n')
38085a6e75f8SKirill A. Shutemov 		tmp[count - 1] = '\0';
38095a6e75f8SKirill A. Shutemov 
38105a6e75f8SKirill A. Shutemov 	huge = shmem_parse_huge(tmp);
38115a6e75f8SKirill A. Shutemov 	if (huge == -EINVAL)
38125a6e75f8SKirill A. Shutemov 		return -EINVAL;
38135a6e75f8SKirill A. Shutemov 	if (!has_transparent_hugepage() &&
38145a6e75f8SKirill A. Shutemov 			huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
38155a6e75f8SKirill A. Shutemov 		return -EINVAL;
38165a6e75f8SKirill A. Shutemov 
38175a6e75f8SKirill A. Shutemov 	shmem_huge = huge;
3818435c0b87SKirill A. Shutemov 	if (shmem_huge > SHMEM_HUGE_DENY)
38195a6e75f8SKirill A. Shutemov 		SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
38205a6e75f8SKirill A. Shutemov 	return count;
38215a6e75f8SKirill A. Shutemov }
38225a6e75f8SKirill A. Shutemov 
38235a6e75f8SKirill A. Shutemov struct kobj_attribute shmem_enabled_attr =
38245a6e75f8SKirill A. Shutemov 	__ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
38253b33719cSArnd Bergmann #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */
3826f3f0e1d2SKirill A. Shutemov 
38273b33719cSArnd Bergmann #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3828f3f0e1d2SKirill A. Shutemov bool shmem_huge_enabled(struct vm_area_struct *vma)
3829f3f0e1d2SKirill A. Shutemov {
3830f3f0e1d2SKirill A. Shutemov 	struct inode *inode = file_inode(vma->vm_file);
3831f3f0e1d2SKirill A. Shutemov 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3832f3f0e1d2SKirill A. Shutemov 	loff_t i_size;
3833f3f0e1d2SKirill A. Shutemov 	pgoff_t off;
3834f3f0e1d2SKirill A. Shutemov 
3835f3f0e1d2SKirill A. Shutemov 	if (shmem_huge == SHMEM_HUGE_FORCE)
3836f3f0e1d2SKirill A. Shutemov 		return true;
3837f3f0e1d2SKirill A. Shutemov 	if (shmem_huge == SHMEM_HUGE_DENY)
3838f3f0e1d2SKirill A. Shutemov 		return false;
3839f3f0e1d2SKirill A. Shutemov 	switch (sbinfo->huge) {
3840f3f0e1d2SKirill A. Shutemov 		case SHMEM_HUGE_NEVER:
3841f3f0e1d2SKirill A. Shutemov 			return false;
3842f3f0e1d2SKirill A. Shutemov 		case SHMEM_HUGE_ALWAYS:
3843f3f0e1d2SKirill A. Shutemov 			return true;
3844f3f0e1d2SKirill A. Shutemov 		case SHMEM_HUGE_WITHIN_SIZE:
3845f3f0e1d2SKirill A. Shutemov 			off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
3846f3f0e1d2SKirill A. Shutemov 			i_size = round_up(i_size_read(inode), PAGE_SIZE);
3847f3f0e1d2SKirill A. Shutemov 			if (i_size >= HPAGE_PMD_SIZE &&
3848f3f0e1d2SKirill A. Shutemov 					i_size >> PAGE_SHIFT >= off)
3849f3f0e1d2SKirill A. Shutemov 				return true;
3850c8402871SGustavo A. R. Silva 			/* fall through */
3851f3f0e1d2SKirill A. Shutemov 		case SHMEM_HUGE_ADVISE:
3852f3f0e1d2SKirill A. Shutemov 			/* TODO: implement fadvise() hints */
3853f3f0e1d2SKirill A. Shutemov 			return (vma->vm_flags & VM_HUGEPAGE);
3854f3f0e1d2SKirill A. Shutemov 		default:
3855f3f0e1d2SKirill A. Shutemov 			VM_BUG_ON(1);
3856f3f0e1d2SKirill A. Shutemov 			return false;
3857f3f0e1d2SKirill A. Shutemov 	}
3858f3f0e1d2SKirill A. Shutemov }
38593b33719cSArnd Bergmann #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
38605a6e75f8SKirill A. Shutemov 
3861853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */
3862853ac43aSMatt Mackall 
3863853ac43aSMatt Mackall /*
3864853ac43aSMatt Mackall  * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3865853ac43aSMatt Mackall  *
3866853ac43aSMatt Mackall  * This is intended for small system where the benefits of the full
3867853ac43aSMatt Mackall  * shmem code (swap-backed and resource-limited) are outweighed by
3868853ac43aSMatt Mackall  * their complexity. On systems without swap this code should be
3869853ac43aSMatt Mackall  * effectively equivalent, but much lighter weight.
3870853ac43aSMatt Mackall  */
3871853ac43aSMatt Mackall 
387241ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = {
3873853ac43aSMatt Mackall 	.name		= "tmpfs",
38743c26ff6eSAl Viro 	.mount		= ramfs_mount,
3875853ac43aSMatt Mackall 	.kill_sb	= kill_litter_super,
38762b8576cbSEric W. Biederman 	.fs_flags	= FS_USERNS_MOUNT,
3877853ac43aSMatt Mackall };
3878853ac43aSMatt Mackall 
387941ffe5d5SHugh Dickins int __init shmem_init(void)
3880853ac43aSMatt Mackall {
388141ffe5d5SHugh Dickins 	BUG_ON(register_filesystem(&shmem_fs_type) != 0);
3882853ac43aSMatt Mackall 
388341ffe5d5SHugh Dickins 	shm_mnt = kern_mount(&shmem_fs_type);
3884853ac43aSMatt Mackall 	BUG_ON(IS_ERR(shm_mnt));
3885853ac43aSMatt Mackall 
3886853ac43aSMatt Mackall 	return 0;
3887853ac43aSMatt Mackall }
3888853ac43aSMatt Mackall 
388941ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page)
3890853ac43aSMatt Mackall {
3891853ac43aSMatt Mackall 	return 0;
3892853ac43aSMatt Mackall }
3893853ac43aSMatt Mackall 
38943f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user)
38953f96b79aSHugh Dickins {
38963f96b79aSHugh Dickins 	return 0;
38973f96b79aSHugh Dickins }
38983f96b79aSHugh Dickins 
389924513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping)
390024513264SHugh Dickins {
390124513264SHugh Dickins }
390224513264SHugh Dickins 
3903c01d5b30SHugh Dickins #ifdef CONFIG_MMU
3904c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file,
3905c01d5b30SHugh Dickins 				      unsigned long addr, unsigned long len,
3906c01d5b30SHugh Dickins 				      unsigned long pgoff, unsigned long flags)
3907c01d5b30SHugh Dickins {
3908c01d5b30SHugh Dickins 	return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
3909c01d5b30SHugh Dickins }
3910c01d5b30SHugh Dickins #endif
3911c01d5b30SHugh Dickins 
391241ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
391394c1e62dSHugh Dickins {
391441ffe5d5SHugh Dickins 	truncate_inode_pages_range(inode->i_mapping, lstart, lend);
391594c1e62dSHugh Dickins }
391694c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
391794c1e62dSHugh Dickins 
3918853ac43aSMatt Mackall #define shmem_vm_ops				generic_file_vm_ops
39190b0a0806SHugh Dickins #define shmem_file_operations			ramfs_file_operations
3920454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags)	ramfs_get_inode(sb, dir, mode, dev)
39210b0a0806SHugh Dickins #define shmem_acct_size(flags, size)		0
39220b0a0806SHugh Dickins #define shmem_unacct_size(flags, size)		do {} while (0)
3923853ac43aSMatt Mackall 
3924853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */
3925853ac43aSMatt Mackall 
3926853ac43aSMatt Mackall /* common code */
39271da177e4SLinus Torvalds 
3928703321b6SMatthew Auld static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size,
3929c7277090SEric Paris 				       unsigned long flags, unsigned int i_flags)
39301da177e4SLinus Torvalds {
39311da177e4SLinus Torvalds 	struct inode *inode;
393293dec2daSAl Viro 	struct file *res;
39331da177e4SLinus Torvalds 
3934703321b6SMatthew Auld 	if (IS_ERR(mnt))
3935703321b6SMatthew Auld 		return ERR_CAST(mnt);
39361da177e4SLinus Torvalds 
3937285b2c4fSHugh Dickins 	if (size < 0 || size > MAX_LFS_FILESIZE)
39381da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
39391da177e4SLinus Torvalds 
39401da177e4SLinus Torvalds 	if (shmem_acct_size(flags, size))
39411da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
39421da177e4SLinus Torvalds 
394393dec2daSAl Viro 	inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0,
394493dec2daSAl Viro 				flags);
3945dac2d1f6SAl Viro 	if (unlikely(!inode)) {
3946dac2d1f6SAl Viro 		shmem_unacct_size(flags, size);
3947dac2d1f6SAl Viro 		return ERR_PTR(-ENOSPC);
3948dac2d1f6SAl Viro 	}
3949c7277090SEric Paris 	inode->i_flags |= i_flags;
39501da177e4SLinus Torvalds 	inode->i_size = size;
39516d6b77f1SMiklos Szeredi 	clear_nlink(inode);	/* It is unlinked */
395226567cdbSAl Viro 	res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
395393dec2daSAl Viro 	if (!IS_ERR(res))
395493dec2daSAl Viro 		res = alloc_file_pseudo(inode, mnt, name, O_RDWR,
39554b42af81SAl Viro 				&shmem_file_operations);
39566b4d0b27SAl Viro 	if (IS_ERR(res))
395793dec2daSAl Viro 		iput(inode);
39586b4d0b27SAl Viro 	return res;
39591da177e4SLinus Torvalds }
3960c7277090SEric Paris 
3961c7277090SEric Paris /**
3962c7277090SEric Paris  * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3963c7277090SEric Paris  * 	kernel internal.  There will be NO LSM permission checks against the
3964c7277090SEric Paris  * 	underlying inode.  So users of this interface must do LSM checks at a
3965e1832f29SStephen Smalley  *	higher layer.  The users are the big_key and shm implementations.  LSM
3966e1832f29SStephen Smalley  *	checks are provided at the key or shm level rather than the inode.
3967c7277090SEric Paris  * @name: name for dentry (to be seen in /proc/<pid>/maps
3968c7277090SEric Paris  * @size: size to be set for the file
3969c7277090SEric Paris  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3970c7277090SEric Paris  */
3971c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3972c7277090SEric Paris {
3973703321b6SMatthew Auld 	return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE);
3974c7277090SEric Paris }
3975c7277090SEric Paris 
3976c7277090SEric Paris /**
3977c7277090SEric Paris  * shmem_file_setup - get an unlinked file living in tmpfs
3978c7277090SEric Paris  * @name: name for dentry (to be seen in /proc/<pid>/maps
3979c7277090SEric Paris  * @size: size to be set for the file
3980c7277090SEric Paris  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3981c7277090SEric Paris  */
3982c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3983c7277090SEric Paris {
3984703321b6SMatthew Auld 	return __shmem_file_setup(shm_mnt, name, size, flags, 0);
3985c7277090SEric Paris }
3986395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup);
39871da177e4SLinus Torvalds 
398846711810SRandy Dunlap /**
3989703321b6SMatthew Auld  * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs
3990703321b6SMatthew Auld  * @mnt: the tmpfs mount where the file will be created
3991703321b6SMatthew Auld  * @name: name for dentry (to be seen in /proc/<pid>/maps
3992703321b6SMatthew Auld  * @size: size to be set for the file
3993703321b6SMatthew Auld  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3994703321b6SMatthew Auld  */
3995703321b6SMatthew Auld struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name,
3996703321b6SMatthew Auld 				       loff_t size, unsigned long flags)
3997703321b6SMatthew Auld {
3998703321b6SMatthew Auld 	return __shmem_file_setup(mnt, name, size, flags, 0);
3999703321b6SMatthew Auld }
4000703321b6SMatthew Auld EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt);
4001703321b6SMatthew Auld 
4002703321b6SMatthew Auld /**
40031da177e4SLinus Torvalds  * shmem_zero_setup - setup a shared anonymous mapping
40041da177e4SLinus Torvalds  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
40051da177e4SLinus Torvalds  */
40061da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma)
40071da177e4SLinus Torvalds {
40081da177e4SLinus Torvalds 	struct file *file;
40091da177e4SLinus Torvalds 	loff_t size = vma->vm_end - vma->vm_start;
40101da177e4SLinus Torvalds 
401166fc1303SHugh Dickins 	/*
401266fc1303SHugh Dickins 	 * Cloning a new file under mmap_sem leads to a lock ordering conflict
401366fc1303SHugh Dickins 	 * between XFS directory reading and selinux: since this file is only
401466fc1303SHugh Dickins 	 * accessible to the user through its mapping, use S_PRIVATE flag to
401566fc1303SHugh Dickins 	 * bypass file security, in the same way as shmem_kernel_file_setup().
401666fc1303SHugh Dickins 	 */
4017703321b6SMatthew Auld 	file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags);
40181da177e4SLinus Torvalds 	if (IS_ERR(file))
40191da177e4SLinus Torvalds 		return PTR_ERR(file);
40201da177e4SLinus Torvalds 
40211da177e4SLinus Torvalds 	if (vma->vm_file)
40221da177e4SLinus Torvalds 		fput(vma->vm_file);
40231da177e4SLinus Torvalds 	vma->vm_file = file;
40241da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
4025f3f0e1d2SKirill A. Shutemov 
4026e496cf3dSKirill A. Shutemov 	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
4027f3f0e1d2SKirill A. Shutemov 			((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
4028f3f0e1d2SKirill A. Shutemov 			(vma->vm_end & HPAGE_PMD_MASK)) {
4029f3f0e1d2SKirill A. Shutemov 		khugepaged_enter(vma, vma->vm_flags);
4030f3f0e1d2SKirill A. Shutemov 	}
4031f3f0e1d2SKirill A. Shutemov 
40321da177e4SLinus Torvalds 	return 0;
40331da177e4SLinus Torvalds }
4034d9d90e5eSHugh Dickins 
4035d9d90e5eSHugh Dickins /**
4036d9d90e5eSHugh Dickins  * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
4037d9d90e5eSHugh Dickins  * @mapping:	the page's address_space
4038d9d90e5eSHugh Dickins  * @index:	the page index
4039d9d90e5eSHugh Dickins  * @gfp:	the page allocator flags to use if allocating
4040d9d90e5eSHugh Dickins  *
4041d9d90e5eSHugh Dickins  * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
4042d9d90e5eSHugh Dickins  * with any new page allocations done using the specified allocation flags.
4043d9d90e5eSHugh Dickins  * But read_cache_page_gfp() uses the ->readpage() method: which does not
4044d9d90e5eSHugh Dickins  * suit tmpfs, since it may have pages in swapcache, and needs to find those
4045d9d90e5eSHugh Dickins  * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4046d9d90e5eSHugh Dickins  *
404768da9f05SHugh Dickins  * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
404868da9f05SHugh Dickins  * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
4049d9d90e5eSHugh Dickins  */
4050d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4051d9d90e5eSHugh Dickins 					 pgoff_t index, gfp_t gfp)
4052d9d90e5eSHugh Dickins {
405368da9f05SHugh Dickins #ifdef CONFIG_SHMEM
405468da9f05SHugh Dickins 	struct inode *inode = mapping->host;
40559276aad6SHugh Dickins 	struct page *page;
405668da9f05SHugh Dickins 	int error;
405768da9f05SHugh Dickins 
405868da9f05SHugh Dickins 	BUG_ON(mapping->a_ops != &shmem_aops);
40599e18eb29SAndres Lagar-Cavilla 	error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
4060cfda0526SMike Rapoport 				  gfp, NULL, NULL, NULL);
406168da9f05SHugh Dickins 	if (error)
406268da9f05SHugh Dickins 		page = ERR_PTR(error);
406368da9f05SHugh Dickins 	else
406468da9f05SHugh Dickins 		unlock_page(page);
406568da9f05SHugh Dickins 	return page;
406668da9f05SHugh Dickins #else
406768da9f05SHugh Dickins 	/*
406868da9f05SHugh Dickins 	 * The tiny !SHMEM case uses ramfs without swap
406968da9f05SHugh Dickins 	 */
4070d9d90e5eSHugh Dickins 	return read_cache_page_gfp(mapping, index, gfp);
407168da9f05SHugh Dickins #endif
4072d9d90e5eSHugh Dickins }
4073d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);
4074