xref: /openbmc/linux/mm/shmem.c (revision e83c32e8f92724a06a22a3b42f3afc07db93e131)
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.
90edd73b3SHugh Dickins  * Copyright (C) 2002-2005 Hugh Dickins.
100edd73b3SHugh Dickins  * Copyright (C) 2002-2005 VERITAS Software Corporation.
111da177e4SLinus Torvalds  * Copyright (C) 2004 Andi Kleen, SuSE Labs
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * Extended attribute support for tmpfs:
141da177e4SLinus Torvalds  * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
151da177e4SLinus Torvalds  * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
161da177e4SLinus Torvalds  *
17853ac43aSMatt Mackall  * tiny-shmem:
18853ac43aSMatt Mackall  * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
19853ac43aSMatt Mackall  *
201da177e4SLinus Torvalds  * This file is released under the GPL.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
23853ac43aSMatt Mackall #include <linux/fs.h>
24853ac43aSMatt Mackall #include <linux/init.h>
25853ac43aSMatt Mackall #include <linux/vfs.h>
26853ac43aSMatt Mackall #include <linux/mount.h>
27caefba17SHugh Dickins #include <linux/pagemap.h>
28853ac43aSMatt Mackall #include <linux/file.h>
29853ac43aSMatt Mackall #include <linux/mm.h>
30853ac43aSMatt Mackall #include <linux/module.h>
317e496299STim Chen #include <linux/percpu_counter.h>
32853ac43aSMatt Mackall #include <linux/swap.h>
33853ac43aSMatt Mackall 
34853ac43aSMatt Mackall static struct vfsmount *shm_mnt;
35853ac43aSMatt Mackall 
36853ac43aSMatt Mackall #ifdef CONFIG_SHMEM
371da177e4SLinus Torvalds /*
381da177e4SLinus Torvalds  * This virtual memory filesystem is heavily based on the ramfs. It
391da177e4SLinus Torvalds  * extends ramfs by the ability to use swap and honor resource limits
401da177e4SLinus Torvalds  * which makes it a completely usable filesystem.
411da177e4SLinus Torvalds  */
421da177e4SLinus Torvalds 
4339f0247dSAndreas Gruenbacher #include <linux/xattr.h>
44a5694255SChristoph Hellwig #include <linux/exportfs.h>
451c7c474cSChristoph Hellwig #include <linux/posix_acl.h>
4639f0247dSAndreas Gruenbacher #include <linux/generic_acl.h>
471da177e4SLinus Torvalds #include <linux/mman.h>
481da177e4SLinus Torvalds #include <linux/string.h>
491da177e4SLinus Torvalds #include <linux/slab.h>
501da177e4SLinus Torvalds #include <linux/backing-dev.h>
511da177e4SLinus Torvalds #include <linux/shmem_fs.h>
521da177e4SLinus Torvalds #include <linux/writeback.h>
531da177e4SLinus Torvalds #include <linux/blkdev.h>
54708e3508SHugh Dickins #include <linux/splice.h>
551da177e4SLinus Torvalds #include <linux/security.h>
561da177e4SLinus Torvalds #include <linux/swapops.h>
571da177e4SLinus Torvalds #include <linux/mempolicy.h>
581da177e4SLinus Torvalds #include <linux/namei.h>
59b00dc3adSHugh Dickins #include <linux/ctype.h>
60304dbdb7SLee Schermerhorn #include <linux/migrate.h>
61c1f60a5aSChristoph Lameter #include <linux/highmem.h>
62680d794bSakpm@linux-foundation.org #include <linux/seq_file.h>
6392562927SMimi Zohar #include <linux/magic.h>
64304dbdb7SLee Schermerhorn 
651da177e4SLinus Torvalds #include <asm/uaccess.h>
661da177e4SLinus Torvalds #include <asm/div64.h>
671da177e4SLinus Torvalds #include <asm/pgtable.h>
681da177e4SLinus Torvalds 
69caefba17SHugh Dickins /*
70caefba17SHugh Dickins  * The maximum size of a shmem/tmpfs file is limited by the maximum size of
71caefba17SHugh Dickins  * its triple-indirect swap vector - see illustration at shmem_swp_entry().
72caefba17SHugh Dickins  *
73caefba17SHugh Dickins  * With 4kB page size, maximum file size is just over 2TB on a 32-bit kernel,
74caefba17SHugh Dickins  * but one eighth of that on a 64-bit kernel.  With 8kB page size, maximum
75caefba17SHugh Dickins  * file size is just over 4TB on a 64-bit kernel, but 16TB on a 32-bit kernel,
76caefba17SHugh Dickins  * MAX_LFS_FILESIZE being then more restrictive than swap vector layout.
77caefba17SHugh Dickins  *
78caefba17SHugh Dickins  * We use / and * instead of shifts in the definitions below, so that the swap
79caefba17SHugh Dickins  * vector can be tested with small even values (e.g. 20) for ENTRIES_PER_PAGE.
80caefba17SHugh Dickins  */
811da177e4SLinus Torvalds #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
8261609d01SYuri Tikhonov #define ENTRIES_PER_PAGEPAGE ((unsigned long long)ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
83caefba17SHugh Dickins 
84caefba17SHugh Dickins #define SHMSWP_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
85caefba17SHugh Dickins #define SHMSWP_MAX_BYTES (SHMSWP_MAX_INDEX << PAGE_CACHE_SHIFT)
86caefba17SHugh Dickins 
87caefba17SHugh Dickins #define SHMEM_MAX_BYTES  min_t(unsigned long long, SHMSWP_MAX_BYTES, MAX_LFS_FILESIZE)
88caefba17SHugh Dickins #define SHMEM_MAX_INDEX  ((unsigned long)((SHMEM_MAX_BYTES+1) >> PAGE_CACHE_SHIFT))
89caefba17SHugh Dickins 
901da177e4SLinus Torvalds #define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
911da177e4SLinus Torvalds #define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
941da177e4SLinus Torvalds #define SHMEM_PAGEIN	 VM_READ
951da177e4SLinus Torvalds #define SHMEM_TRUNCATE	 VM_WRITE
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds /* Definition to limit shmem_truncate's steps between cond_rescheds */
981da177e4SLinus Torvalds #define LATENCY_LIMIT	 64
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */
1011da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20
1021da177e4SLinus Torvalds 
103b09e0fa4SEric Paris struct shmem_xattr {
104b09e0fa4SEric Paris 	struct list_head list;	/* anchored by shmem_inode_info->xattr_list */
105b09e0fa4SEric Paris 	char *name;		/* xattr name */
106b09e0fa4SEric Paris 	size_t size;
107b09e0fa4SEric Paris 	char value[0];
108b09e0fa4SEric Paris };
109b09e0fa4SEric Paris 
1101da177e4SLinus Torvalds /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
1111da177e4SLinus Torvalds enum sgp_type {
1121da177e4SLinus Torvalds 	SGP_READ,	/* don't exceed i_size, don't allocate page */
1131da177e4SLinus Torvalds 	SGP_CACHE,	/* don't exceed i_size, may allocate page */
114a0ee5ec5SHugh Dickins 	SGP_DIRTY,	/* like SGP_CACHE, but set new page dirty */
1151da177e4SLinus Torvalds 	SGP_WRITE,	/* may exceed i_size, may allocate page */
1161da177e4SLinus Torvalds };
1171da177e4SLinus Torvalds 
118b76db735SAndrew Morton #ifdef CONFIG_TMPFS
119680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void)
120680d794bSakpm@linux-foundation.org {
121680d794bSakpm@linux-foundation.org 	return totalram_pages / 2;
122680d794bSakpm@linux-foundation.org }
123680d794bSakpm@linux-foundation.org 
124680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void)
125680d794bSakpm@linux-foundation.org {
126680d794bSakpm@linux-foundation.org 	return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
127680d794bSakpm@linux-foundation.org }
128b76db735SAndrew Morton #endif
129680d794bSakpm@linux-foundation.org 
13068da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
13168da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
13268da9f05SHugh Dickins 
13368da9f05SHugh Dickins static inline int shmem_getpage(struct inode *inode, pgoff_t index,
13468da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, int *fault_type)
13568da9f05SHugh Dickins {
13668da9f05SHugh Dickins 	return shmem_getpage_gfp(inode, index, pagep, sgp,
13768da9f05SHugh Dickins 			mapping_gfp_mask(inode->i_mapping), fault_type);
13868da9f05SHugh Dickins }
1391da177e4SLinus Torvalds 
1406daa0e28SAl Viro static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	/*
1431da177e4SLinus Torvalds 	 * The above definition of ENTRIES_PER_PAGE, and the use of
1441da177e4SLinus Torvalds 	 * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
1451da177e4SLinus Torvalds 	 * might be reconsidered if it ever diverges from PAGE_SIZE.
146769848c0SMel Gorman 	 *
147e12ba74dSMel Gorman 	 * Mobility flags are masked out as swap vectors cannot move
1481da177e4SLinus Torvalds 	 */
149e12ba74dSMel Gorman 	return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
150769848c0SMel Gorman 				PAGE_CACHE_SHIFT-PAGE_SHIFT);
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds static inline void shmem_dir_free(struct page *page)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds 	__free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds static struct page **shmem_dir_map(struct page *page)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	return (struct page **)kmap_atomic(page, KM_USER0);
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds static inline void shmem_dir_unmap(struct page **dir)
1641da177e4SLinus Torvalds {
1651da177e4SLinus Torvalds 	kunmap_atomic(dir, KM_USER0);
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds static swp_entry_t *shmem_swp_map(struct page *page)
1691da177e4SLinus Torvalds {
1701da177e4SLinus Torvalds 	return (swp_entry_t *)kmap_atomic(page, KM_USER1);
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds static inline void shmem_swp_balance_unmap(void)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	/*
1761da177e4SLinus Torvalds 	 * When passing a pointer to an i_direct entry, to code which
1771da177e4SLinus Torvalds 	 * also handles indirect entries and so will shmem_swp_unmap,
1781da177e4SLinus Torvalds 	 * we must arrange for the preempt count to remain in balance.
1791da177e4SLinus Torvalds 	 * What kmap_atomic of a lowmem page does depends on config
1801da177e4SLinus Torvalds 	 * and architecture, so pretend to kmap_atomic some lowmem page.
1811da177e4SLinus Torvalds 	 */
1821da177e4SLinus Torvalds 	(void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds static inline void shmem_swp_unmap(swp_entry_t *entry)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds 	kunmap_atomic(entry, KM_USER1);
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	return sb->s_fs_info;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds /*
1961da177e4SLinus Torvalds  * shmem_file_setup pre-accounts the whole fixed size of a VM object,
1971da177e4SLinus Torvalds  * for shared memory and for shared anonymous (/dev/zero) mappings
1981da177e4SLinus Torvalds  * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
1991da177e4SLinus Torvalds  * consistent with the pre-accounting of private mappings ...
2001da177e4SLinus Torvalds  */
2011da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size)
2021da177e4SLinus Torvalds {
2030b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
2040b0a0806SHugh Dickins 		0 : security_vm_enough_memory_kern(VM_ACCT(size));
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size)
2081da177e4SLinus Torvalds {
2090b0a0806SHugh Dickins 	if (!(flags & VM_NORESERVE))
2101da177e4SLinus Torvalds 		vm_unacct_memory(VM_ACCT(size));
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds /*
2141da177e4SLinus Torvalds  * ... whereas tmpfs objects are accounted incrementally as
2151da177e4SLinus Torvalds  * pages are allocated, in order to allow huge sparse files.
2161da177e4SLinus Torvalds  * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
2171da177e4SLinus Torvalds  * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
2181da177e4SLinus Torvalds  */
2191da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags)
2201da177e4SLinus Torvalds {
2210b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
2220b0a0806SHugh Dickins 		security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages)
2261da177e4SLinus Torvalds {
2270b0a0806SHugh Dickins 	if (flags & VM_NORESERVE)
2281da177e4SLinus Torvalds 		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
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;
2381da177e4SLinus Torvalds 
2396c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info  __read_mostly = {
2401da177e4SLinus Torvalds 	.ra_pages	= 0,	/* No readahead */
2414f98a2feSRik van Riel 	.capabilities	= BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
2421da177e4SLinus Torvalds };
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist);
245cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds static void shmem_free_blocks(struct inode *inode, long pages)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
2500edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
2517e496299STim Chen 		percpu_counter_add(&sbinfo->used_blocks, -pages);
2521da177e4SLinus Torvalds 		inode->i_blocks -= pages*BLOCKS_PER_PAGE;
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
2565b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb)
2575b04c689SPavel Emelyanov {
2585b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2595b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2605b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2615b04c689SPavel Emelyanov 		if (!sbinfo->free_inodes) {
2625b04c689SPavel Emelyanov 			spin_unlock(&sbinfo->stat_lock);
2635b04c689SPavel Emelyanov 			return -ENOSPC;
2645b04c689SPavel Emelyanov 		}
2655b04c689SPavel Emelyanov 		sbinfo->free_inodes--;
2665b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2675b04c689SPavel Emelyanov 	}
2685b04c689SPavel Emelyanov 	return 0;
2695b04c689SPavel Emelyanov }
2705b04c689SPavel Emelyanov 
2715b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb)
2725b04c689SPavel Emelyanov {
2735b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2745b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2755b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2765b04c689SPavel Emelyanov 		sbinfo->free_inodes++;
2775b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2785b04c689SPavel Emelyanov 	}
2795b04c689SPavel Emelyanov }
2805b04c689SPavel Emelyanov 
28146711810SRandy Dunlap /**
2821da177e4SLinus Torvalds  * shmem_recalc_inode - recalculate the size of an inode
2831da177e4SLinus Torvalds  * @inode: inode to recalc
2841da177e4SLinus Torvalds  *
2851da177e4SLinus Torvalds  * We have to calculate the free blocks since the mm can drop
2861da177e4SLinus Torvalds  * undirtied hole pages behind our back.
2871da177e4SLinus Torvalds  *
2881da177e4SLinus Torvalds  * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
2891da177e4SLinus Torvalds  * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
2901da177e4SLinus Torvalds  *
2911da177e4SLinus Torvalds  * It has to be called with the spinlock held.
2921da177e4SLinus Torvalds  */
2931da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode)
2941da177e4SLinus Torvalds {
2951da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
2961da177e4SLinus Torvalds 	long freed;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
2991da177e4SLinus Torvalds 	if (freed > 0) {
3001da177e4SLinus Torvalds 		info->alloced -= freed;
3011da177e4SLinus Torvalds 		shmem_unacct_blocks(info->flags, freed);
3021da177e4SLinus Torvalds 		shmem_free_blocks(inode, freed);
3031da177e4SLinus Torvalds 	}
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
30646711810SRandy Dunlap /**
3071da177e4SLinus Torvalds  * shmem_swp_entry - find the swap vector position in the info structure
3081da177e4SLinus Torvalds  * @info:  info structure for the inode
3091da177e4SLinus Torvalds  * @index: index of the page to find
3101da177e4SLinus Torvalds  * @page:  optional page to add to the structure. Has to be preset to
3111da177e4SLinus Torvalds  *         all zeros
3121da177e4SLinus Torvalds  *
3131da177e4SLinus Torvalds  * If there is no space allocated yet it will return NULL when
3141da177e4SLinus Torvalds  * page is NULL, else it will use the page for the needed block,
3151da177e4SLinus Torvalds  * setting it to NULL on return to indicate that it has been used.
3161da177e4SLinus Torvalds  *
3171da177e4SLinus Torvalds  * The swap vector is organized the following way:
3181da177e4SLinus Torvalds  *
3191da177e4SLinus Torvalds  * There are SHMEM_NR_DIRECT entries directly stored in the
3201da177e4SLinus Torvalds  * shmem_inode_info structure. So small files do not need an addional
3211da177e4SLinus Torvalds  * allocation.
3221da177e4SLinus Torvalds  *
3231da177e4SLinus Torvalds  * For pages with index > SHMEM_NR_DIRECT there is the pointer
3241da177e4SLinus Torvalds  * i_indirect which points to a page which holds in the first half
3251da177e4SLinus Torvalds  * doubly indirect blocks, in the second half triple indirect blocks:
3261da177e4SLinus Torvalds  *
3271da177e4SLinus Torvalds  * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
3281da177e4SLinus Torvalds  * following layout (for SHMEM_NR_DIRECT == 16):
3291da177e4SLinus Torvalds  *
3301da177e4SLinus Torvalds  * i_indirect -> dir --> 16-19
3311da177e4SLinus Torvalds  * 	      |	     +-> 20-23
3321da177e4SLinus Torvalds  * 	      |
3331da177e4SLinus Torvalds  * 	      +-->dir2 --> 24-27
3341da177e4SLinus Torvalds  * 	      |	       +-> 28-31
3351da177e4SLinus Torvalds  * 	      |	       +-> 32-35
3361da177e4SLinus Torvalds  * 	      |	       +-> 36-39
3371da177e4SLinus Torvalds  * 	      |
3381da177e4SLinus Torvalds  * 	      +-->dir3 --> 40-43
3391da177e4SLinus Torvalds  * 	       	       +-> 44-47
3401da177e4SLinus Torvalds  * 	      	       +-> 48-51
3411da177e4SLinus Torvalds  * 	      	       +-> 52-55
3421da177e4SLinus Torvalds  */
3431da177e4SLinus Torvalds static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds 	unsigned long offset;
3461da177e4SLinus Torvalds 	struct page **dir;
3471da177e4SLinus Torvalds 	struct page *subdir;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	if (index < SHMEM_NR_DIRECT) {
3501da177e4SLinus Torvalds 		shmem_swp_balance_unmap();
3511da177e4SLinus Torvalds 		return info->i_direct+index;
3521da177e4SLinus Torvalds 	}
3531da177e4SLinus Torvalds 	if (!info->i_indirect) {
3541da177e4SLinus Torvalds 		if (page) {
3551da177e4SLinus Torvalds 			info->i_indirect = *page;
3561da177e4SLinus Torvalds 			*page = NULL;
3571da177e4SLinus Torvalds 		}
3581da177e4SLinus Torvalds 		return NULL;			/* need another page */
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	index -= SHMEM_NR_DIRECT;
3621da177e4SLinus Torvalds 	offset = index % ENTRIES_PER_PAGE;
3631da177e4SLinus Torvalds 	index /= ENTRIES_PER_PAGE;
3641da177e4SLinus Torvalds 	dir = shmem_dir_map(info->i_indirect);
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	if (index >= ENTRIES_PER_PAGE/2) {
3671da177e4SLinus Torvalds 		index -= ENTRIES_PER_PAGE/2;
3681da177e4SLinus Torvalds 		dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
3691da177e4SLinus Torvalds 		index %= ENTRIES_PER_PAGE;
3701da177e4SLinus Torvalds 		subdir = *dir;
3711da177e4SLinus Torvalds 		if (!subdir) {
3721da177e4SLinus Torvalds 			if (page) {
3731da177e4SLinus Torvalds 				*dir = *page;
3741da177e4SLinus Torvalds 				*page = NULL;
3751da177e4SLinus Torvalds 			}
3761da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
3771da177e4SLinus Torvalds 			return NULL;		/* need another page */
3781da177e4SLinus Torvalds 		}
3791da177e4SLinus Torvalds 		shmem_dir_unmap(dir);
3801da177e4SLinus Torvalds 		dir = shmem_dir_map(subdir);
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	dir += index;
3841da177e4SLinus Torvalds 	subdir = *dir;
3851da177e4SLinus Torvalds 	if (!subdir) {
3861da177e4SLinus Torvalds 		if (!page || !(subdir = *page)) {
3871da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
3881da177e4SLinus Torvalds 			return NULL;		/* need a page */
3891da177e4SLinus Torvalds 		}
3901da177e4SLinus Torvalds 		*dir = subdir;
3911da177e4SLinus Torvalds 		*page = NULL;
3921da177e4SLinus Torvalds 	}
3931da177e4SLinus Torvalds 	shmem_dir_unmap(dir);
3941da177e4SLinus Torvalds 	return shmem_swp_map(subdir) + offset;
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	long incdec = value? 1: -1;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	entry->val = value;
4021da177e4SLinus Torvalds 	info->swapped += incdec;
4034c21e2f2SHugh Dickins 	if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
4044c21e2f2SHugh Dickins 		struct page *page = kmap_atomic_to_page(entry);
4054c21e2f2SHugh Dickins 		set_page_private(page, page_private(page) + incdec);
4064c21e2f2SHugh Dickins 	}
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
40946711810SRandy Dunlap /**
4101da177e4SLinus Torvalds  * shmem_swp_alloc - get the position of the swap entry for the page.
4111da177e4SLinus Torvalds  * @info:	info structure for the inode
4121da177e4SLinus Torvalds  * @index:	index of the page to find
4131da177e4SLinus Torvalds  * @sgp:	check and recheck i_size? skip allocation?
41468da9f05SHugh Dickins  * @gfp:	gfp mask to use for any page allocation
41546711810SRandy Dunlap  *
41646711810SRandy Dunlap  * If the entry does not exist, allocate it.
4171da177e4SLinus Torvalds  */
41868da9f05SHugh Dickins static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info,
41968da9f05SHugh Dickins 			unsigned long index, enum sgp_type sgp, gfp_t gfp)
4201da177e4SLinus Torvalds {
4211da177e4SLinus Torvalds 	struct inode *inode = &info->vfs_inode;
4221da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
4231da177e4SLinus Torvalds 	struct page *page = NULL;
4241da177e4SLinus Torvalds 	swp_entry_t *entry;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	if (sgp != SGP_WRITE &&
4271da177e4SLinus Torvalds 	    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
4281da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	while (!(entry = shmem_swp_entry(info, index, &page))) {
4311da177e4SLinus Torvalds 		if (sgp == SGP_READ)
4321da177e4SLinus Torvalds 			return shmem_swp_map(ZERO_PAGE(0));
4331da177e4SLinus Torvalds 		/*
4347e496299STim Chen 		 * Test used_blocks against 1 less max_blocks, since we have 1 data
4351da177e4SLinus Torvalds 		 * page (and perhaps indirect index pages) yet to allocate:
4361da177e4SLinus Torvalds 		 * a waste to allocate index if we cannot allocate data.
4371da177e4SLinus Torvalds 		 */
4380edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
439fc5da22aSHugh Dickins 			if (percpu_counter_compare(&sbinfo->used_blocks,
440fc5da22aSHugh Dickins 						sbinfo->max_blocks - 1) >= 0)
4411da177e4SLinus Torvalds 				return ERR_PTR(-ENOSPC);
4427e496299STim Chen 			percpu_counter_inc(&sbinfo->used_blocks);
4431da177e4SLinus Torvalds 			inode->i_blocks += BLOCKS_PER_PAGE;
4441da177e4SLinus Torvalds 		}
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 		spin_unlock(&info->lock);
44768da9f05SHugh Dickins 		page = shmem_dir_alloc(gfp);
4481da177e4SLinus Torvalds 		spin_lock(&info->lock);
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 		if (!page) {
4511da177e4SLinus Torvalds 			shmem_free_blocks(inode, 1);
4521da177e4SLinus Torvalds 			return ERR_PTR(-ENOMEM);
4531da177e4SLinus Torvalds 		}
4541da177e4SLinus Torvalds 		if (sgp != SGP_WRITE &&
4551da177e4SLinus Torvalds 		    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
4561da177e4SLinus Torvalds 			entry = ERR_PTR(-EINVAL);
4571da177e4SLinus Torvalds 			break;
4581da177e4SLinus Torvalds 		}
4591da177e4SLinus Torvalds 		if (info->next_index <= index)
4601da177e4SLinus Torvalds 			info->next_index = index + 1;
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds 	if (page) {
4631da177e4SLinus Torvalds 		/* another task gave its page, or truncated the file */
4641da177e4SLinus Torvalds 		shmem_free_blocks(inode, 1);
4651da177e4SLinus Torvalds 		shmem_dir_free(page);
4661da177e4SLinus Torvalds 	}
4671da177e4SLinus Torvalds 	if (info->next_index <= index && !IS_ERR(entry))
4681da177e4SLinus Torvalds 		info->next_index = index + 1;
4691da177e4SLinus Torvalds 	return entry;
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
47246711810SRandy Dunlap /**
4731da177e4SLinus Torvalds  * shmem_free_swp - free some swap entries in a directory
4741da177e4SLinus Torvalds  * @dir:        pointer to the directory
4751da177e4SLinus Torvalds  * @edir:       pointer after last entry of the directory
4761ae70006SHugh Dickins  * @punch_lock: pointer to spinlock when needed for the holepunch case
4771da177e4SLinus Torvalds  */
4781ae70006SHugh Dickins static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
4791ae70006SHugh Dickins 						spinlock_t *punch_lock)
4801da177e4SLinus Torvalds {
4811ae70006SHugh Dickins 	spinlock_t *punch_unlock = NULL;
4821da177e4SLinus Torvalds 	swp_entry_t *ptr;
4831da177e4SLinus Torvalds 	int freed = 0;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	for (ptr = dir; ptr < edir; ptr++) {
4861da177e4SLinus Torvalds 		if (ptr->val) {
4871ae70006SHugh Dickins 			if (unlikely(punch_lock)) {
4881ae70006SHugh Dickins 				punch_unlock = punch_lock;
4891ae70006SHugh Dickins 				punch_lock = NULL;
4901ae70006SHugh Dickins 				spin_lock(punch_unlock);
4911ae70006SHugh Dickins 				if (!ptr->val)
4921ae70006SHugh Dickins 					continue;
4931ae70006SHugh Dickins 			}
4941da177e4SLinus Torvalds 			free_swap_and_cache(*ptr);
4951da177e4SLinus Torvalds 			*ptr = (swp_entry_t){0};
4961da177e4SLinus Torvalds 			freed++;
4971da177e4SLinus Torvalds 		}
4981da177e4SLinus Torvalds 	}
4991ae70006SHugh Dickins 	if (punch_unlock)
5001ae70006SHugh Dickins 		spin_unlock(punch_unlock);
5011da177e4SLinus Torvalds 	return freed;
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds 
5041ae70006SHugh Dickins static int shmem_map_and_free_swp(struct page *subdir, int offset,
5051ae70006SHugh Dickins 		int limit, struct page ***dir, spinlock_t *punch_lock)
5061da177e4SLinus Torvalds {
5071da177e4SLinus Torvalds 	swp_entry_t *ptr;
5081da177e4SLinus Torvalds 	int freed = 0;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	ptr = shmem_swp_map(subdir);
5111da177e4SLinus Torvalds 	for (; offset < limit; offset += LATENCY_LIMIT) {
5121da177e4SLinus Torvalds 		int size = limit - offset;
5131da177e4SLinus Torvalds 		if (size > LATENCY_LIMIT)
5141da177e4SLinus Torvalds 			size = LATENCY_LIMIT;
5151ae70006SHugh Dickins 		freed += shmem_free_swp(ptr+offset, ptr+offset+size,
5161ae70006SHugh Dickins 							punch_lock);
5171da177e4SLinus Torvalds 		if (need_resched()) {
5181da177e4SLinus Torvalds 			shmem_swp_unmap(ptr);
5191da177e4SLinus Torvalds 			if (*dir) {
5201da177e4SLinus Torvalds 				shmem_dir_unmap(*dir);
5211da177e4SLinus Torvalds 				*dir = NULL;
5221da177e4SLinus Torvalds 			}
5231da177e4SLinus Torvalds 			cond_resched();
5241da177e4SLinus Torvalds 			ptr = shmem_swp_map(subdir);
5251da177e4SLinus Torvalds 		}
5261da177e4SLinus Torvalds 	}
5271da177e4SLinus Torvalds 	shmem_swp_unmap(ptr);
5281da177e4SLinus Torvalds 	return freed;
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds static void shmem_free_pages(struct list_head *next)
5321da177e4SLinus Torvalds {
5331da177e4SLinus Torvalds 	struct page *page;
5341da177e4SLinus Torvalds 	int freed = 0;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	do {
5371da177e4SLinus Torvalds 		page = container_of(next, struct page, lru);
5381da177e4SLinus Torvalds 		next = next->next;
5391da177e4SLinus Torvalds 		shmem_dir_free(page);
5401da177e4SLinus Torvalds 		freed++;
5411da177e4SLinus Torvalds 		if (freed >= LATENCY_LIMIT) {
5421da177e4SLinus Torvalds 			cond_resched();
5431da177e4SLinus Torvalds 			freed = 0;
5441da177e4SLinus Torvalds 		}
5451da177e4SLinus Torvalds 	} while (next);
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds 
54894c1e62dSHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
5491da177e4SLinus Torvalds {
5501da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
5511da177e4SLinus Torvalds 	unsigned long idx;
5521da177e4SLinus Torvalds 	unsigned long size;
5531da177e4SLinus Torvalds 	unsigned long limit;
5541da177e4SLinus Torvalds 	unsigned long stage;
5551da177e4SLinus Torvalds 	unsigned long diroff;
5561da177e4SLinus Torvalds 	struct page **dir;
5571da177e4SLinus Torvalds 	struct page *topdir;
5581da177e4SLinus Torvalds 	struct page *middir;
5591da177e4SLinus Torvalds 	struct page *subdir;
5601da177e4SLinus Torvalds 	swp_entry_t *ptr;
5611da177e4SLinus Torvalds 	LIST_HEAD(pages_to_free);
5621da177e4SLinus Torvalds 	long nr_pages_to_free = 0;
5631da177e4SLinus Torvalds 	long nr_swaps_freed = 0;
5641da177e4SLinus Torvalds 	int offset;
5651da177e4SLinus Torvalds 	int freed;
566a2646d1eSHugh Dickins 	int punch_hole;
5671ae70006SHugh Dickins 	spinlock_t *needs_lock;
5681ae70006SHugh Dickins 	spinlock_t *punch_lock;
569a2646d1eSHugh Dickins 	unsigned long upper_limit;
5701da177e4SLinus Torvalds 
57194c1e62dSHugh Dickins 	truncate_inode_pages_range(inode->i_mapping, start, end);
57294c1e62dSHugh Dickins 
5731da177e4SLinus Torvalds 	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
574f6b3ec23SBadari Pulavarty 	idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
5751da177e4SLinus Torvalds 	if (idx >= info->next_index)
5761da177e4SLinus Torvalds 		return;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	spin_lock(&info->lock);
5791da177e4SLinus Torvalds 	info->flags |= SHMEM_TRUNCATE;
580f6b3ec23SBadari Pulavarty 	if (likely(end == (loff_t) -1)) {
5811da177e4SLinus Torvalds 		limit = info->next_index;
582a2646d1eSHugh Dickins 		upper_limit = SHMEM_MAX_INDEX;
5831da177e4SLinus Torvalds 		info->next_index = idx;
5841ae70006SHugh Dickins 		needs_lock = NULL;
585a2646d1eSHugh Dickins 		punch_hole = 0;
586f6b3ec23SBadari Pulavarty 	} else {
587a2646d1eSHugh Dickins 		if (end + 1 >= inode->i_size) {	/* we may free a little more */
588a2646d1eSHugh Dickins 			limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
589a2646d1eSHugh Dickins 							PAGE_CACHE_SHIFT;
590a2646d1eSHugh Dickins 			upper_limit = SHMEM_MAX_INDEX;
591a2646d1eSHugh Dickins 		} else {
592a2646d1eSHugh Dickins 			limit = (end + 1) >> PAGE_CACHE_SHIFT;
593a2646d1eSHugh Dickins 			upper_limit = limit;
594a2646d1eSHugh Dickins 		}
5951ae70006SHugh Dickins 		needs_lock = &info->lock;
596f6b3ec23SBadari Pulavarty 		punch_hole = 1;
597f6b3ec23SBadari Pulavarty 	}
598f6b3ec23SBadari Pulavarty 
5991da177e4SLinus Torvalds 	topdir = info->i_indirect;
600f6b3ec23SBadari Pulavarty 	if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
6011da177e4SLinus Torvalds 		info->i_indirect = NULL;
6021da177e4SLinus Torvalds 		nr_pages_to_free++;
6031da177e4SLinus Torvalds 		list_add(&topdir->lru, &pages_to_free);
6041da177e4SLinus Torvalds 	}
6051da177e4SLinus Torvalds 	spin_unlock(&info->lock);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	if (info->swapped && idx < SHMEM_NR_DIRECT) {
6081da177e4SLinus Torvalds 		ptr = info->i_direct;
6091da177e4SLinus Torvalds 		size = limit;
6101da177e4SLinus Torvalds 		if (size > SHMEM_NR_DIRECT)
6111da177e4SLinus Torvalds 			size = SHMEM_NR_DIRECT;
6121ae70006SHugh Dickins 		nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
6131da177e4SLinus Torvalds 	}
61492a3d03aSBadari Pulavarty 
61592a3d03aSBadari Pulavarty 	/*
61692a3d03aSBadari Pulavarty 	 * If there are no indirect blocks or we are punching a hole
61792a3d03aSBadari Pulavarty 	 * below indirect blocks, nothing to be done.
61892a3d03aSBadari Pulavarty 	 */
619a2646d1eSHugh Dickins 	if (!topdir || limit <= SHMEM_NR_DIRECT)
6201da177e4SLinus Torvalds 		goto done2;
6211da177e4SLinus Torvalds 
6221ae70006SHugh Dickins 	/*
6231ae70006SHugh Dickins 	 * The truncation case has already dropped info->lock, and we're safe
6241ae70006SHugh Dickins 	 * because i_size and next_index have already been lowered, preventing
6251ae70006SHugh Dickins 	 * access beyond.  But in the punch_hole case, we still need to take
6261ae70006SHugh Dickins 	 * the lock when updating the swap directory, because there might be
6271ae70006SHugh Dickins 	 * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
6281ae70006SHugh Dickins 	 * shmem_writepage.  However, whenever we find we can remove a whole
6291ae70006SHugh Dickins 	 * directory page (not at the misaligned start or end of the range),
6301ae70006SHugh Dickins 	 * we first NULLify its pointer in the level above, and then have no
6311ae70006SHugh Dickins 	 * need to take the lock when updating its contents: needs_lock and
6321ae70006SHugh Dickins 	 * punch_lock (either pointing to info->lock or NULL) manage this.
6331ae70006SHugh Dickins 	 */
6341ae70006SHugh Dickins 
635a2646d1eSHugh Dickins 	upper_limit -= SHMEM_NR_DIRECT;
6361da177e4SLinus Torvalds 	limit -= SHMEM_NR_DIRECT;
6371da177e4SLinus Torvalds 	idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
6381da177e4SLinus Torvalds 	offset = idx % ENTRIES_PER_PAGE;
6391da177e4SLinus Torvalds 	idx -= offset;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	dir = shmem_dir_map(topdir);
6421da177e4SLinus Torvalds 	stage = ENTRIES_PER_PAGEPAGE/2;
6431da177e4SLinus Torvalds 	if (idx < ENTRIES_PER_PAGEPAGE/2) {
6441da177e4SLinus Torvalds 		middir = topdir;
6451da177e4SLinus Torvalds 		diroff = idx/ENTRIES_PER_PAGE;
6461da177e4SLinus Torvalds 	} else {
6471da177e4SLinus Torvalds 		dir += ENTRIES_PER_PAGE/2;
6481da177e4SLinus Torvalds 		dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
6491da177e4SLinus Torvalds 		while (stage <= idx)
6501da177e4SLinus Torvalds 			stage += ENTRIES_PER_PAGEPAGE;
6511da177e4SLinus Torvalds 		middir = *dir;
6521da177e4SLinus Torvalds 		if (*dir) {
6531da177e4SLinus Torvalds 			diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
6541da177e4SLinus Torvalds 				ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
655a2646d1eSHugh Dickins 			if (!diroff && !offset && upper_limit >= stage) {
6561ae70006SHugh Dickins 				if (needs_lock) {
6571ae70006SHugh Dickins 					spin_lock(needs_lock);
6581ae70006SHugh Dickins 					*dir = NULL;
6591ae70006SHugh Dickins 					spin_unlock(needs_lock);
6601ae70006SHugh Dickins 					needs_lock = NULL;
6611ae70006SHugh Dickins 				} else
6621da177e4SLinus Torvalds 					*dir = NULL;
6631da177e4SLinus Torvalds 				nr_pages_to_free++;
6641da177e4SLinus Torvalds 				list_add(&middir->lru, &pages_to_free);
6651da177e4SLinus Torvalds 			}
6661da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6671da177e4SLinus Torvalds 			dir = shmem_dir_map(middir);
6681da177e4SLinus Torvalds 		} else {
6691da177e4SLinus Torvalds 			diroff = 0;
6701da177e4SLinus Torvalds 			offset = 0;
6711da177e4SLinus Torvalds 			idx = stage;
6721da177e4SLinus Torvalds 		}
6731da177e4SLinus Torvalds 	}
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
6761da177e4SLinus Torvalds 		if (unlikely(idx == stage)) {
6771da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6781da177e4SLinus Torvalds 			dir = shmem_dir_map(topdir) +
6791da177e4SLinus Torvalds 			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
6801da177e4SLinus Torvalds 			while (!*dir) {
6811da177e4SLinus Torvalds 				dir++;
6821da177e4SLinus Torvalds 				idx += ENTRIES_PER_PAGEPAGE;
6831da177e4SLinus Torvalds 				if (idx >= limit)
6841da177e4SLinus Torvalds 					goto done1;
6851da177e4SLinus Torvalds 			}
6861da177e4SLinus Torvalds 			stage = idx + ENTRIES_PER_PAGEPAGE;
6871da177e4SLinus Torvalds 			middir = *dir;
6881ae70006SHugh Dickins 			if (punch_hole)
6891ae70006SHugh Dickins 				needs_lock = &info->lock;
690a2646d1eSHugh Dickins 			if (upper_limit >= stage) {
6911ae70006SHugh Dickins 				if (needs_lock) {
6921ae70006SHugh Dickins 					spin_lock(needs_lock);
6931ae70006SHugh Dickins 					*dir = NULL;
6941ae70006SHugh Dickins 					spin_unlock(needs_lock);
6951ae70006SHugh Dickins 					needs_lock = NULL;
6961ae70006SHugh Dickins 				} else
6971da177e4SLinus Torvalds 					*dir = NULL;
6981da177e4SLinus Torvalds 				nr_pages_to_free++;
6991da177e4SLinus Torvalds 				list_add(&middir->lru, &pages_to_free);
700a2646d1eSHugh Dickins 			}
7011da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
7021da177e4SLinus Torvalds 			cond_resched();
7031da177e4SLinus Torvalds 			dir = shmem_dir_map(middir);
7041da177e4SLinus Torvalds 			diroff = 0;
7051da177e4SLinus Torvalds 		}
7061ae70006SHugh Dickins 		punch_lock = needs_lock;
7071da177e4SLinus Torvalds 		subdir = dir[diroff];
7081ae70006SHugh Dickins 		if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
7091ae70006SHugh Dickins 			if (needs_lock) {
7101ae70006SHugh Dickins 				spin_lock(needs_lock);
7111ae70006SHugh Dickins 				dir[diroff] = NULL;
7121ae70006SHugh Dickins 				spin_unlock(needs_lock);
7131ae70006SHugh Dickins 				punch_lock = NULL;
7141ae70006SHugh Dickins 			} else
7151da177e4SLinus Torvalds 				dir[diroff] = NULL;
7161da177e4SLinus Torvalds 			nr_pages_to_free++;
7171da177e4SLinus Torvalds 			list_add(&subdir->lru, &pages_to_free);
7181da177e4SLinus Torvalds 		}
7191ae70006SHugh Dickins 		if (subdir && page_private(subdir) /* has swap entries */) {
7201ae70006SHugh Dickins 			size = limit - idx;
7211ae70006SHugh Dickins 			if (size > ENTRIES_PER_PAGE)
7221ae70006SHugh Dickins 				size = ENTRIES_PER_PAGE;
7231ae70006SHugh Dickins 			freed = shmem_map_and_free_swp(subdir,
7241ae70006SHugh Dickins 					offset, size, &dir, punch_lock);
7251ae70006SHugh Dickins 			if (!dir)
7261ae70006SHugh Dickins 				dir = shmem_dir_map(middir);
7271ae70006SHugh Dickins 			nr_swaps_freed += freed;
7281ae70006SHugh Dickins 			if (offset || punch_lock) {
7291ae70006SHugh Dickins 				spin_lock(&info->lock);
7301ae70006SHugh Dickins 				set_page_private(subdir,
7311ae70006SHugh Dickins 					page_private(subdir) - freed);
7321ae70006SHugh Dickins 				spin_unlock(&info->lock);
7331ae70006SHugh Dickins 			} else
7341ae70006SHugh Dickins 				BUG_ON(page_private(subdir) != freed);
7351ae70006SHugh Dickins 		}
7361ae70006SHugh Dickins 		offset = 0;
7371da177e4SLinus Torvalds 	}
7381da177e4SLinus Torvalds done1:
7391da177e4SLinus Torvalds 	shmem_dir_unmap(dir);
7401da177e4SLinus Torvalds done2:
7411da177e4SLinus Torvalds 	if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
7421da177e4SLinus Torvalds 		/*
7431da177e4SLinus Torvalds 		 * Call truncate_inode_pages again: racing shmem_unuse_inode
7443889e6e7Snpiggin@suse.de 		 * may have swizzled a page in from swap since
7453889e6e7Snpiggin@suse.de 		 * truncate_pagecache or generic_delete_inode did it, before we
7463889e6e7Snpiggin@suse.de 		 * lowered next_index.  Also, though shmem_getpage checks
7473889e6e7Snpiggin@suse.de 		 * i_size before adding to cache, no recheck after: so fix the
7483889e6e7Snpiggin@suse.de 		 * narrow window there too.
7491da177e4SLinus Torvalds 		 */
750f6b3ec23SBadari Pulavarty 		truncate_inode_pages_range(inode->i_mapping, start, end);
7511da177e4SLinus Torvalds 	}
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	spin_lock(&info->lock);
7541da177e4SLinus Torvalds 	info->flags &= ~SHMEM_TRUNCATE;
7551da177e4SLinus Torvalds 	info->swapped -= nr_swaps_freed;
7561da177e4SLinus Torvalds 	if (nr_pages_to_free)
7571da177e4SLinus Torvalds 		shmem_free_blocks(inode, nr_pages_to_free);
7581da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
7591da177e4SLinus Torvalds 	spin_unlock(&info->lock);
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	/*
7621da177e4SLinus Torvalds 	 * Empty swap vector directory pages to be freed?
7631da177e4SLinus Torvalds 	 */
7641da177e4SLinus Torvalds 	if (!list_empty(&pages_to_free)) {
7651da177e4SLinus Torvalds 		pages_to_free.prev->next = NULL;
7661da177e4SLinus Torvalds 		shmem_free_pages(pages_to_free.next);
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds }
76994c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
7701da177e4SLinus Torvalds 
77194c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
7741da177e4SLinus Torvalds 	int error;
7751da177e4SLinus Torvalds 
776db78b877SChristoph Hellwig 	error = inode_change_ok(inode, attr);
777db78b877SChristoph Hellwig 	if (error)
778db78b877SChristoph Hellwig 		return error;
779db78b877SChristoph Hellwig 
78094c1e62dSHugh Dickins 	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
78194c1e62dSHugh Dickins 		loff_t oldsize = inode->i_size;
78294c1e62dSHugh Dickins 		loff_t newsize = attr->ia_size;
7833889e6e7Snpiggin@suse.de 		struct page *page = NULL;
7843889e6e7Snpiggin@suse.de 
78594c1e62dSHugh Dickins 		if (newsize < oldsize) {
7861da177e4SLinus Torvalds 			/*
7871da177e4SLinus Torvalds 			 * If truncating down to a partial page, then
7881da177e4SLinus Torvalds 			 * if that page is already allocated, hold it
7891da177e4SLinus Torvalds 			 * in memory until the truncation is over, so
790ae0e47f0SJustin P. Mattock 			 * truncate_partial_page cannot miss it were
7911da177e4SLinus Torvalds 			 * it assigned to swap.
7921da177e4SLinus Torvalds 			 */
7933889e6e7Snpiggin@suse.de 			if (newsize & (PAGE_CACHE_SIZE-1)) {
7941da177e4SLinus Torvalds 				(void) shmem_getpage(inode,
7953889e6e7Snpiggin@suse.de 					newsize >> PAGE_CACHE_SHIFT,
7961da177e4SLinus Torvalds 						&page, SGP_READ, NULL);
797d3602444SHugh Dickins 				if (page)
798d3602444SHugh Dickins 					unlock_page(page);
7991da177e4SLinus Torvalds 			}
8001da177e4SLinus Torvalds 			/*
8011da177e4SLinus Torvalds 			 * Reset SHMEM_PAGEIN flag so that shmem_truncate can
8021da177e4SLinus Torvalds 			 * detect if any pages might have been added to cache
8031da177e4SLinus Torvalds 			 * after truncate_inode_pages.  But we needn't bother
8041da177e4SLinus Torvalds 			 * if it's being fully truncated to zero-length: the
8051da177e4SLinus Torvalds 			 * nrpages check is efficient enough in that case.
8061da177e4SLinus Torvalds 			 */
8073889e6e7Snpiggin@suse.de 			if (newsize) {
8081da177e4SLinus Torvalds 				struct shmem_inode_info *info = SHMEM_I(inode);
8091da177e4SLinus Torvalds 				spin_lock(&info->lock);
8101da177e4SLinus Torvalds 				info->flags &= ~SHMEM_PAGEIN;
8111da177e4SLinus Torvalds 				spin_unlock(&info->lock);
8121da177e4SLinus Torvalds 			}
8131da177e4SLinus Torvalds 		}
81494c1e62dSHugh Dickins 		if (newsize != oldsize) {
81594c1e62dSHugh Dickins 			i_size_write(inode, newsize);
81694c1e62dSHugh Dickins 			inode->i_ctime = inode->i_mtime = CURRENT_TIME;
81794c1e62dSHugh Dickins 		}
81894c1e62dSHugh Dickins 		if (newsize < oldsize) {
81994c1e62dSHugh Dickins 			loff_t holebegin = round_up(newsize, PAGE_SIZE);
82094c1e62dSHugh Dickins 			unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
82194c1e62dSHugh Dickins 			shmem_truncate_range(inode, newsize, (loff_t)-1);
82294c1e62dSHugh Dickins 			/* unmap again to remove racily COWed private pages */
82394c1e62dSHugh Dickins 			unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
82494c1e62dSHugh Dickins 		}
8253889e6e7Snpiggin@suse.de 		if (page)
8263889e6e7Snpiggin@suse.de 			page_cache_release(page);
8271da177e4SLinus Torvalds 	}
8281da177e4SLinus Torvalds 
8296a1a90adSChristoph Hellwig 	setattr_copy(inode, attr);
83039f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
831db78b877SChristoph Hellwig 	if (attr->ia_valid & ATTR_MODE)
8321c7c474cSChristoph Hellwig 		error = generic_acl_chmod(inode);
83339f0247dSAndreas Gruenbacher #endif
8341da177e4SLinus Torvalds 	return error;
8351da177e4SLinus Torvalds }
8361da177e4SLinus Torvalds 
8371f895f75SAl Viro static void shmem_evict_inode(struct inode *inode)
8381da177e4SLinus Torvalds {
8391da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
840b09e0fa4SEric Paris 	struct shmem_xattr *xattr, *nxattr;
8411da177e4SLinus Torvalds 
8423889e6e7Snpiggin@suse.de 	if (inode->i_mapping->a_ops == &shmem_aops) {
8431da177e4SLinus Torvalds 		shmem_unacct_size(info->flags, inode->i_size);
8441da177e4SLinus Torvalds 		inode->i_size = 0;
8453889e6e7Snpiggin@suse.de 		shmem_truncate_range(inode, 0, (loff_t)-1);
8461da177e4SLinus Torvalds 		if (!list_empty(&info->swaplist)) {
847cb5f7b9aSHugh Dickins 			mutex_lock(&shmem_swaplist_mutex);
8481da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
849cb5f7b9aSHugh Dickins 			mutex_unlock(&shmem_swaplist_mutex);
8501da177e4SLinus Torvalds 		}
8511da177e4SLinus Torvalds 	}
852b09e0fa4SEric Paris 
853b09e0fa4SEric Paris 	list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
854b09e0fa4SEric Paris 		kfree(xattr->name);
855b09e0fa4SEric Paris 		kfree(xattr);
856b09e0fa4SEric Paris 	}
8571da177e4SLinus Torvalds 	BUG_ON(inode->i_blocks);
8585b04c689SPavel Emelyanov 	shmem_free_inode(inode->i_sb);
8591f895f75SAl Viro 	end_writeback(inode);
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
8631da177e4SLinus Torvalds {
8641da177e4SLinus Torvalds 	swp_entry_t *ptr;
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	for (ptr = dir; ptr < edir; ptr++) {
8671da177e4SLinus Torvalds 		if (ptr->val == entry.val)
8681da177e4SLinus Torvalds 			return ptr - dir;
8691da177e4SLinus Torvalds 	}
8701da177e4SLinus Torvalds 	return -1;
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
8741da177e4SLinus Torvalds {
875778dd893SHugh Dickins 	struct address_space *mapping;
8761da177e4SLinus Torvalds 	unsigned long idx;
8771da177e4SLinus Torvalds 	unsigned long size;
8781da177e4SLinus Torvalds 	unsigned long limit;
8791da177e4SLinus Torvalds 	unsigned long stage;
8801da177e4SLinus Torvalds 	struct page **dir;
8811da177e4SLinus Torvalds 	struct page *subdir;
8821da177e4SLinus Torvalds 	swp_entry_t *ptr;
8831da177e4SLinus Torvalds 	int offset;
884d9fe526aSHugh Dickins 	int error;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	idx = 0;
8871da177e4SLinus Torvalds 	ptr = info->i_direct;
8881da177e4SLinus Torvalds 	spin_lock(&info->lock);
8891b1b32f2SHugh Dickins 	if (!info->swapped) {
8901b1b32f2SHugh Dickins 		list_del_init(&info->swaplist);
8911b1b32f2SHugh Dickins 		goto lost2;
8921b1b32f2SHugh Dickins 	}
8931da177e4SLinus Torvalds 	limit = info->next_index;
8941da177e4SLinus Torvalds 	size = limit;
8951da177e4SLinus Torvalds 	if (size > SHMEM_NR_DIRECT)
8961da177e4SLinus Torvalds 		size = SHMEM_NR_DIRECT;
8971da177e4SLinus Torvalds 	offset = shmem_find_swp(entry, ptr, ptr+size);
898778dd893SHugh Dickins 	if (offset >= 0) {
899778dd893SHugh Dickins 		shmem_swp_balance_unmap();
9001da177e4SLinus Torvalds 		goto found;
901778dd893SHugh Dickins 	}
9021da177e4SLinus Torvalds 	if (!info->i_indirect)
9031da177e4SLinus Torvalds 		goto lost2;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	dir = shmem_dir_map(info->i_indirect);
9061da177e4SLinus Torvalds 	stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
9091da177e4SLinus Torvalds 		if (unlikely(idx == stage)) {
9101da177e4SLinus Torvalds 			shmem_dir_unmap(dir-1);
911cb5f7b9aSHugh Dickins 			if (cond_resched_lock(&info->lock)) {
912cb5f7b9aSHugh Dickins 				/* check it has not been truncated */
913cb5f7b9aSHugh Dickins 				if (limit > info->next_index) {
914cb5f7b9aSHugh Dickins 					limit = info->next_index;
915cb5f7b9aSHugh Dickins 					if (idx >= limit)
916cb5f7b9aSHugh Dickins 						goto lost2;
917cb5f7b9aSHugh Dickins 				}
918cb5f7b9aSHugh Dickins 			}
9191da177e4SLinus Torvalds 			dir = shmem_dir_map(info->i_indirect) +
9201da177e4SLinus Torvalds 			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
9211da177e4SLinus Torvalds 			while (!*dir) {
9221da177e4SLinus Torvalds 				dir++;
9231da177e4SLinus Torvalds 				idx += ENTRIES_PER_PAGEPAGE;
9241da177e4SLinus Torvalds 				if (idx >= limit)
9251da177e4SLinus Torvalds 					goto lost1;
9261da177e4SLinus Torvalds 			}
9271da177e4SLinus Torvalds 			stage = idx + ENTRIES_PER_PAGEPAGE;
9281da177e4SLinus Torvalds 			subdir = *dir;
9291da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
9301da177e4SLinus Torvalds 			dir = shmem_dir_map(subdir);
9311da177e4SLinus Torvalds 		}
9321da177e4SLinus Torvalds 		subdir = *dir;
9334c21e2f2SHugh Dickins 		if (subdir && page_private(subdir)) {
9341da177e4SLinus Torvalds 			ptr = shmem_swp_map(subdir);
9351da177e4SLinus Torvalds 			size = limit - idx;
9361da177e4SLinus Torvalds 			if (size > ENTRIES_PER_PAGE)
9371da177e4SLinus Torvalds 				size = ENTRIES_PER_PAGE;
9381da177e4SLinus Torvalds 			offset = shmem_find_swp(entry, ptr, ptr+size);
939e6c9366bSHugh Dickins 			shmem_swp_unmap(ptr);
9401da177e4SLinus Torvalds 			if (offset >= 0) {
9411da177e4SLinus Torvalds 				shmem_dir_unmap(dir);
942e6c9366bSHugh Dickins 				ptr = shmem_swp_map(subdir);
9431da177e4SLinus Torvalds 				goto found;
9441da177e4SLinus Torvalds 			}
9451da177e4SLinus Torvalds 		}
9461da177e4SLinus Torvalds 	}
9471da177e4SLinus Torvalds lost1:
9481da177e4SLinus Torvalds 	shmem_dir_unmap(dir-1);
9491da177e4SLinus Torvalds lost2:
9501da177e4SLinus Torvalds 	spin_unlock(&info->lock);
9511da177e4SLinus Torvalds 	return 0;
9521da177e4SLinus Torvalds found:
9531da177e4SLinus Torvalds 	idx += offset;
954778dd893SHugh Dickins 	ptr += offset;
9552e0e26c7SHugh Dickins 
9561b1b32f2SHugh Dickins 	/*
9571b1b32f2SHugh Dickins 	 * Move _head_ to start search for next from here.
9581f895f75SAl Viro 	 * But be careful: shmem_evict_inode checks list_empty without taking
9591b1b32f2SHugh Dickins 	 * mutex, and there's an instant in list_move_tail when info->swaplist
9601b1b32f2SHugh Dickins 	 * would appear empty, if it were the only one on shmem_swaplist.  We
9611b1b32f2SHugh Dickins 	 * could avoid doing it if inode NULL; or use this minor optimization.
9621b1b32f2SHugh Dickins 	 */
9631b1b32f2SHugh Dickins 	if (shmem_swaplist.next != &info->swaplist)
9642e0e26c7SHugh Dickins 		list_move_tail(&shmem_swaplist, &info->swaplist);
9652e0e26c7SHugh Dickins 
966d13d1443SKAMEZAWA Hiroyuki 	/*
967778dd893SHugh Dickins 	 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
968778dd893SHugh Dickins 	 * but also to hold up shmem_evict_inode(): so inode cannot be freed
969778dd893SHugh Dickins 	 * beneath us (pagelock doesn't help until the page is in pagecache).
970d13d1443SKAMEZAWA Hiroyuki 	 */
971778dd893SHugh Dickins 	mapping = info->vfs_inode.i_mapping;
972778dd893SHugh Dickins 	error = add_to_page_cache_locked(page, mapping, idx, GFP_NOWAIT);
973778dd893SHugh Dickins 	/* which does mem_cgroup_uncharge_cache_page on error */
97469029cd5SKAMEZAWA Hiroyuki 
975d9fe526aSHugh Dickins 	if (error == -EEXIST) {
976778dd893SHugh Dickins 		struct page *filepage = find_get_page(mapping, idx);
9772e0e26c7SHugh Dickins 		error = 1;
978d9fe526aSHugh Dickins 		if (filepage) {
979d9fe526aSHugh Dickins 			/*
980d9fe526aSHugh Dickins 			 * There might be a more uptodate page coming down
981d9fe526aSHugh Dickins 			 * from a stacked writepage: forget our swappage if so.
982d9fe526aSHugh Dickins 			 */
983d9fe526aSHugh Dickins 			if (PageUptodate(filepage))
984d9fe526aSHugh Dickins 				error = 0;
985d9fe526aSHugh Dickins 			page_cache_release(filepage);
986d9fe526aSHugh Dickins 		}
987d9fe526aSHugh Dickins 	}
988d9fe526aSHugh Dickins 	if (!error) {
98973b1262fSHugh Dickins 		delete_from_swap_cache(page);
99073b1262fSHugh Dickins 		set_page_dirty(page);
9911da177e4SLinus Torvalds 		info->flags |= SHMEM_PAGEIN;
9922e0e26c7SHugh Dickins 		shmem_swp_set(info, ptr, 0);
9932e0e26c7SHugh Dickins 		swap_free(entry);
9942e0e26c7SHugh Dickins 		error = 1;	/* not an error, but entry was found */
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 	shmem_swp_unmap(ptr);
9971da177e4SLinus Torvalds 	spin_unlock(&info->lock);
9982e0e26c7SHugh Dickins 	return error;
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds /*
10021da177e4SLinus Torvalds  * shmem_unuse() search for an eventually swapped out shmem page.
10031da177e4SLinus Torvalds  */
10041da177e4SLinus Torvalds int shmem_unuse(swp_entry_t entry, struct page *page)
10051da177e4SLinus Torvalds {
10061da177e4SLinus Torvalds 	struct list_head *p, *next;
10071da177e4SLinus Torvalds 	struct shmem_inode_info *info;
10081da177e4SLinus Torvalds 	int found = 0;
1009778dd893SHugh Dickins 	int error;
1010778dd893SHugh Dickins 
1011778dd893SHugh Dickins 	/*
1012778dd893SHugh Dickins 	 * Charge page using GFP_KERNEL while we can wait, before taking
1013778dd893SHugh Dickins 	 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1014778dd893SHugh Dickins 	 * Charged back to the user (not to caller) when swap account is used.
1015778dd893SHugh Dickins 	 * add_to_page_cache() will be called with GFP_NOWAIT.
1016778dd893SHugh Dickins 	 */
1017778dd893SHugh Dickins 	error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
1018778dd893SHugh Dickins 	if (error)
1019778dd893SHugh Dickins 		goto out;
1020778dd893SHugh Dickins 	/*
1021778dd893SHugh Dickins 	 * Try to preload while we can wait, to not make a habit of
1022778dd893SHugh Dickins 	 * draining atomic reserves; but don't latch on to this cpu,
1023778dd893SHugh Dickins 	 * it's okay if sometimes we get rescheduled after this.
1024778dd893SHugh Dickins 	 */
1025778dd893SHugh Dickins 	error = radix_tree_preload(GFP_KERNEL);
1026778dd893SHugh Dickins 	if (error)
1027778dd893SHugh Dickins 		goto uncharge;
1028778dd893SHugh Dickins 	radix_tree_preload_end();
10291da177e4SLinus Torvalds 
1030cb5f7b9aSHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
10311da177e4SLinus Torvalds 	list_for_each_safe(p, next, &shmem_swaplist) {
10321da177e4SLinus Torvalds 		info = list_entry(p, struct shmem_inode_info, swaplist);
10332e0e26c7SHugh Dickins 		found = shmem_unuse_inode(info, entry, page);
1034cb5f7b9aSHugh Dickins 		cond_resched();
10352e0e26c7SHugh Dickins 		if (found)
1036778dd893SHugh Dickins 			break;
10371da177e4SLinus Torvalds 	}
1038cb5f7b9aSHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
1039778dd893SHugh Dickins 
1040778dd893SHugh Dickins uncharge:
1041778dd893SHugh Dickins 	if (!found)
1042778dd893SHugh Dickins 		mem_cgroup_uncharge_cache_page(page);
1043778dd893SHugh Dickins 	if (found < 0)
1044778dd893SHugh Dickins 		error = found;
1045778dd893SHugh Dickins out:
1046aaa46865SHugh Dickins 	unlock_page(page);
1047aaa46865SHugh Dickins 	page_cache_release(page);
1048778dd893SHugh Dickins 	return error;
10491da177e4SLinus Torvalds }
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds /*
10521da177e4SLinus Torvalds  * Move the page from the page cache to the swap cache.
10531da177e4SLinus Torvalds  */
10541da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc)
10551da177e4SLinus Torvalds {
10561da177e4SLinus Torvalds 	struct shmem_inode_info *info;
10571da177e4SLinus Torvalds 	swp_entry_t *entry, swap;
10581da177e4SLinus Torvalds 	struct address_space *mapping;
10591da177e4SLinus Torvalds 	unsigned long index;
10601da177e4SLinus Torvalds 	struct inode *inode;
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
10631da177e4SLinus Torvalds 	mapping = page->mapping;
10641da177e4SLinus Torvalds 	index = page->index;
10651da177e4SLinus Torvalds 	inode = mapping->host;
10661da177e4SLinus Torvalds 	info = SHMEM_I(inode);
10671da177e4SLinus Torvalds 	if (info->flags & VM_LOCKED)
10681da177e4SLinus Torvalds 		goto redirty;
1069d9fe526aSHugh Dickins 	if (!total_swap_pages)
10701da177e4SLinus Torvalds 		goto redirty;
10711da177e4SLinus Torvalds 
1072d9fe526aSHugh Dickins 	/*
1073d9fe526aSHugh Dickins 	 * shmem_backing_dev_info's capabilities prevent regular writeback or
1074d9fe526aSHugh Dickins 	 * sync from ever calling shmem_writepage; but a stacking filesystem
1075d9fe526aSHugh Dickins 	 * may use the ->writepage of its underlying filesystem, in which case
1076d9fe526aSHugh Dickins 	 * tmpfs should write out to swap only in response to memory pressure,
10775b0830cbSJens Axboe 	 * and not for the writeback threads or sync.  However, in those cases,
10785b0830cbSJens Axboe 	 * we do still want to check if there's a redundant swappage to be
10795b0830cbSJens Axboe 	 * discarded.
1080d9fe526aSHugh Dickins 	 */
1081d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
1082d9fe526aSHugh Dickins 		swap = get_swap_page();
1083d9fe526aSHugh Dickins 	else
1084d9fe526aSHugh Dickins 		swap.val = 0;
1085d9fe526aSHugh Dickins 
1086b1dea800SHugh Dickins 	/*
1087b1dea800SHugh Dickins 	 * Add inode to shmem_unuse()'s list of swapped-out inodes,
1088b1dea800SHugh Dickins 	 * if it's not already there.  Do it now because we cannot take
1089b1dea800SHugh Dickins 	 * mutex while holding spinlock, and must do so before the page
1090b1dea800SHugh Dickins 	 * is moved to swap cache, when its pagelock no longer protects
1091b1dea800SHugh Dickins 	 * the inode from eviction.  But don't unlock the mutex until
1092b1dea800SHugh Dickins 	 * we've taken the spinlock, because shmem_unuse_inode() will
1093b1dea800SHugh Dickins 	 * prune a !swapped inode from the swaplist under both locks.
1094b1dea800SHugh Dickins 	 */
109505bf86b4SHugh Dickins 	if (swap.val) {
1096b1dea800SHugh Dickins 		mutex_lock(&shmem_swaplist_mutex);
109705bf86b4SHugh Dickins 		if (list_empty(&info->swaplist))
109805bf86b4SHugh Dickins 			list_add_tail(&info->swaplist, &shmem_swaplist);
1099b1dea800SHugh Dickins 	}
1100b1dea800SHugh Dickins 
11011da177e4SLinus Torvalds 	spin_lock(&info->lock);
110205bf86b4SHugh Dickins 	if (swap.val)
1103b1dea800SHugh Dickins 		mutex_unlock(&shmem_swaplist_mutex);
1104b1dea800SHugh Dickins 
11051da177e4SLinus Torvalds 	if (index >= info->next_index) {
11061da177e4SLinus Torvalds 		BUG_ON(!(info->flags & SHMEM_TRUNCATE));
11071da177e4SLinus Torvalds 		goto unlock;
11081da177e4SLinus Torvalds 	}
11091da177e4SLinus Torvalds 	entry = shmem_swp_entry(info, index, NULL);
1110d9fe526aSHugh Dickins 	if (entry->val) {
1111d9fe526aSHugh Dickins 		/*
1112d9fe526aSHugh Dickins 		 * The more uptodate page coming down from a stacked
1113d9fe526aSHugh Dickins 		 * writepage should replace our old swappage.
1114d9fe526aSHugh Dickins 		 */
1115d9fe526aSHugh Dickins 		free_swap_and_cache(*entry);
1116d9fe526aSHugh Dickins 		shmem_swp_set(info, entry, 0);
1117d9fe526aSHugh Dickins 	}
1118d9fe526aSHugh Dickins 	shmem_recalc_inode(inode);
11191da177e4SLinus Torvalds 
1120d9fe526aSHugh Dickins 	if (swap.val && add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
11214c73b1bcSMinchan Kim 		delete_from_page_cache(page);
11221da177e4SLinus Torvalds 		shmem_swp_set(info, entry, swap.val);
11231da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
1124aaa46865SHugh Dickins 		swap_shmem_alloc(swap);
1125826267cfSHugh Dickins 		spin_unlock(&info->lock);
1126d9fe526aSHugh Dickins 		BUG_ON(page_mapped(page));
11279fab5619SHugh Dickins 		swap_writepage(page, wbc);
11281da177e4SLinus Torvalds 		return 0;
11291da177e4SLinus Torvalds 	}
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	shmem_swp_unmap(entry);
11321da177e4SLinus Torvalds unlock:
11331da177e4SLinus Torvalds 	spin_unlock(&info->lock);
11342ca4532aSDaisuke Nishimura 	/*
11352ca4532aSDaisuke Nishimura 	 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
11362ca4532aSDaisuke Nishimura 	 * clear SWAP_HAS_CACHE flag.
11372ca4532aSDaisuke Nishimura 	 */
1138cb4b86baSKAMEZAWA Hiroyuki 	swapcache_free(swap, NULL);
11391da177e4SLinus Torvalds redirty:
11401da177e4SLinus Torvalds 	set_page_dirty(page);
1141d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
1142d9fe526aSHugh Dickins 		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
1143d9fe526aSHugh Dickins 	unlock_page(page);
1144d9fe526aSHugh Dickins 	return 0;
11451da177e4SLinus Torvalds }
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1148680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
114971fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1150680d794bSakpm@linux-foundation.org {
1151680d794bSakpm@linux-foundation.org 	char buffer[64];
1152680d794bSakpm@linux-foundation.org 
115371fe804bSLee Schermerhorn 	if (!mpol || mpol->mode == MPOL_DEFAULT)
1154095f1fc4SLee Schermerhorn 		return;		/* show nothing */
1155095f1fc4SLee Schermerhorn 
115671fe804bSLee Schermerhorn 	mpol_to_str(buffer, sizeof(buffer), mpol, 1);
1157095f1fc4SLee Schermerhorn 
1158095f1fc4SLee Schermerhorn 	seq_printf(seq, ",mpol=%s", buffer);
1159680d794bSakpm@linux-foundation.org }
116071fe804bSLee Schermerhorn 
116171fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
116271fe804bSLee Schermerhorn {
116371fe804bSLee Schermerhorn 	struct mempolicy *mpol = NULL;
116471fe804bSLee Schermerhorn 	if (sbinfo->mpol) {
116571fe804bSLee Schermerhorn 		spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
116671fe804bSLee Schermerhorn 		mpol = sbinfo->mpol;
116771fe804bSLee Schermerhorn 		mpol_get(mpol);
116871fe804bSLee Schermerhorn 		spin_unlock(&sbinfo->stat_lock);
116971fe804bSLee Schermerhorn 	}
117071fe804bSLee Schermerhorn 	return mpol;
117171fe804bSLee Schermerhorn }
1172680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
1173680d794bSakpm@linux-foundation.org 
117402098feaSHugh Dickins static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
117502098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11761da177e4SLinus Torvalds {
117752cd3b07SLee Schermerhorn 	struct mempolicy mpol, *spol;
11781da177e4SLinus Torvalds 	struct vm_area_struct pvma;
1179c4cc6d07SHugh Dickins 	struct page *page;
11801da177e4SLinus Torvalds 
118152cd3b07SLee Schermerhorn 	spol = mpol_cond_copy(&mpol,
118252cd3b07SLee Schermerhorn 				mpol_shared_policy_lookup(&info->policy, idx));
118352cd3b07SLee Schermerhorn 
11841da177e4SLinus Torvalds 	/* Create a pseudo vma that just contains the policy */
1185c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
11861da177e4SLinus Torvalds 	pvma.vm_pgoff = idx;
1187c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
118852cd3b07SLee Schermerhorn 	pvma.vm_policy = spol;
118902098feaSHugh Dickins 	page = swapin_readahead(entry, gfp, &pvma, 0);
11901da177e4SLinus Torvalds 	return page;
11911da177e4SLinus Torvalds }
11921da177e4SLinus Torvalds 
119302098feaSHugh Dickins static struct page *shmem_alloc_page(gfp_t gfp,
119402098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11951da177e4SLinus Torvalds {
11961da177e4SLinus Torvalds 	struct vm_area_struct pvma;
11971da177e4SLinus Torvalds 
1198c4cc6d07SHugh Dickins 	/* Create a pseudo vma that just contains the policy */
1199c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
12001da177e4SLinus Torvalds 	pvma.vm_pgoff = idx;
1201c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
1202c4cc6d07SHugh Dickins 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
120352cd3b07SLee Schermerhorn 
120452cd3b07SLee Schermerhorn 	/*
120552cd3b07SLee Schermerhorn 	 * alloc_page_vma() will drop the shared policy reference
120652cd3b07SLee Schermerhorn 	 */
120752cd3b07SLee Schermerhorn 	return alloc_page_vma(gfp, &pvma, 0);
12081da177e4SLinus Torvalds }
1209680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */
1210680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
121171fe804bSLee Schermerhorn static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *p)
1212680d794bSakpm@linux-foundation.org {
1213680d794bSakpm@linux-foundation.org }
1214680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
1215680d794bSakpm@linux-foundation.org 
121602098feaSHugh Dickins static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
121702098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
12181da177e4SLinus Torvalds {
121902098feaSHugh Dickins 	return swapin_readahead(entry, gfp, NULL, 0);
12201da177e4SLinus Torvalds }
12211da177e4SLinus Torvalds 
122202098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp,
122302098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
12241da177e4SLinus Torvalds {
1225e84e2e13SHugh Dickins 	return alloc_page(gfp);
12261da177e4SLinus Torvalds }
1227680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */
12281da177e4SLinus Torvalds 
122971fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
123071fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
123171fe804bSLee Schermerhorn {
123271fe804bSLee Schermerhorn 	return NULL;
123371fe804bSLee Schermerhorn }
123471fe804bSLee Schermerhorn #endif
123571fe804bSLee Schermerhorn 
12361da177e4SLinus Torvalds /*
123768da9f05SHugh Dickins  * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
12381da177e4SLinus Torvalds  *
12391da177e4SLinus Torvalds  * If we allocate a new one we do not mark it dirty. That's up to the
12401da177e4SLinus Torvalds  * vm. If we swap it in we mark it dirty since we also free the swap
12411da177e4SLinus Torvalds  * entry since a page cannot live in both the swap and page cache
12421da177e4SLinus Torvalds  */
124368da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t idx,
124468da9f05SHugh Dickins 	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
12451da177e4SLinus Torvalds {
12461da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
12471da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
12481da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo;
12499276aad6SHugh Dickins 	struct page *filepage;
12501da177e4SLinus Torvalds 	struct page *swappage;
1251ff36b801SShaohua Li 	struct page *prealloc_page = NULL;
12521da177e4SLinus Torvalds 	swp_entry_t *entry;
12531da177e4SLinus Torvalds 	swp_entry_t swap;
12541da177e4SLinus Torvalds 	int error;
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	if (idx >= SHMEM_MAX_INDEX)
12571da177e4SLinus Torvalds 		return -EFBIG;
12581da177e4SLinus Torvalds repeat:
12591da177e4SLinus Torvalds 	filepage = find_lock_page(mapping, idx);
12601da177e4SLinus Torvalds 	if (filepage && PageUptodate(filepage))
12611da177e4SLinus Torvalds 		goto done;
1262b409f9fcSHugh Dickins 	if (!filepage) {
1263b409f9fcSHugh Dickins 		/*
1264b409f9fcSHugh Dickins 		 * Try to preload while we can wait, to not make a habit of
1265b409f9fcSHugh Dickins 		 * draining atomic reserves; but don't latch on to this cpu.
1266b409f9fcSHugh Dickins 		 */
126768da9f05SHugh Dickins 		error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
1268b409f9fcSHugh Dickins 		if (error)
1269b409f9fcSHugh Dickins 			goto failed;
1270b409f9fcSHugh Dickins 		radix_tree_preload_end();
1271ff36b801SShaohua Li 		if (sgp != SGP_READ && !prealloc_page) {
1272ff36b801SShaohua Li 			prealloc_page = shmem_alloc_page(gfp, info, idx);
1273ff36b801SShaohua Li 			if (prealloc_page) {
1274*e83c32e8SHugh Dickins 				SetPageSwapBacked(prealloc_page);
1275ff36b801SShaohua Li 				if (mem_cgroup_cache_charge(prealloc_page,
1276ff36b801SShaohua Li 						current->mm, GFP_KERNEL)) {
1277ff36b801SShaohua Li 					page_cache_release(prealloc_page);
1278ff36b801SShaohua Li 					prealloc_page = NULL;
1279b409f9fcSHugh Dickins 				}
1280ff36b801SShaohua Li 			}
1281ff36b801SShaohua Li 		}
1282ff36b801SShaohua Li 	}
1283ff36b801SShaohua Li 	error = 0;
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	spin_lock(&info->lock);
12861da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
128768da9f05SHugh Dickins 	entry = shmem_swp_alloc(info, idx, sgp, gfp);
12881da177e4SLinus Torvalds 	if (IS_ERR(entry)) {
12891da177e4SLinus Torvalds 		spin_unlock(&info->lock);
12901da177e4SLinus Torvalds 		error = PTR_ERR(entry);
12911da177e4SLinus Torvalds 		goto failed;
12921da177e4SLinus Torvalds 	}
12931da177e4SLinus Torvalds 	swap = *entry;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	if (swap.val) {
12961da177e4SLinus Torvalds 		/* Look it up and read it in.. */
12971da177e4SLinus Torvalds 		swappage = lookup_swap_cache(swap);
12981da177e4SLinus Torvalds 		if (!swappage) {
12991da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
1300f8891e5eSChristoph Lameter 			spin_unlock(&info->lock);
1301456f998eSYing Han 			/* here we actually do the io */
130268da9f05SHugh Dickins 			if (fault_type)
130368da9f05SHugh Dickins 				*fault_type |= VM_FAULT_MAJOR;
130402098feaSHugh Dickins 			swappage = shmem_swapin(swap, gfp, info, idx);
13051da177e4SLinus Torvalds 			if (!swappage) {
13061da177e4SLinus Torvalds 				spin_lock(&info->lock);
130768da9f05SHugh Dickins 				entry = shmem_swp_alloc(info, idx, sgp, gfp);
13081da177e4SLinus Torvalds 				if (IS_ERR(entry))
13091da177e4SLinus Torvalds 					error = PTR_ERR(entry);
13101da177e4SLinus Torvalds 				else {
13111da177e4SLinus Torvalds 					if (entry->val == swap.val)
13121da177e4SLinus Torvalds 						error = -ENOMEM;
13131da177e4SLinus Torvalds 					shmem_swp_unmap(entry);
13141da177e4SLinus Torvalds 				}
13151da177e4SLinus Torvalds 				spin_unlock(&info->lock);
13161da177e4SLinus Torvalds 				if (error)
13171da177e4SLinus Torvalds 					goto failed;
13181da177e4SLinus Torvalds 				goto repeat;
13191da177e4SLinus Torvalds 			}
13201da177e4SLinus Torvalds 			wait_on_page_locked(swappage);
13211da177e4SLinus Torvalds 			page_cache_release(swappage);
13221da177e4SLinus Torvalds 			goto repeat;
13231da177e4SLinus Torvalds 		}
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 		/* We have to do this with page locked to prevent races */
1326529ae9aaSNick Piggin 		if (!trylock_page(swappage)) {
13271da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13281da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13291da177e4SLinus Torvalds 			wait_on_page_locked(swappage);
13301da177e4SLinus Torvalds 			page_cache_release(swappage);
13311da177e4SLinus Torvalds 			goto repeat;
13321da177e4SLinus Torvalds 		}
13331da177e4SLinus Torvalds 		if (PageWriteback(swappage)) {
13341da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13351da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13361da177e4SLinus Torvalds 			wait_on_page_writeback(swappage);
13371da177e4SLinus Torvalds 			unlock_page(swappage);
13381da177e4SLinus Torvalds 			page_cache_release(swappage);
13391da177e4SLinus Torvalds 			goto repeat;
13401da177e4SLinus Torvalds 		}
13411da177e4SLinus Torvalds 		if (!PageUptodate(swappage)) {
13421da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13431da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13441da177e4SLinus Torvalds 			unlock_page(swappage);
13451da177e4SLinus Torvalds 			page_cache_release(swappage);
13461da177e4SLinus Torvalds 			error = -EIO;
13471da177e4SLinus Torvalds 			goto failed;
13481da177e4SLinus Torvalds 		}
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 		if (filepage) {
13511da177e4SLinus Torvalds 			shmem_swp_set(info, entry, 0);
13521da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13531da177e4SLinus Torvalds 			delete_from_swap_cache(swappage);
13541da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13551da177e4SLinus Torvalds 			copy_highpage(filepage, swappage);
13561da177e4SLinus Torvalds 			unlock_page(swappage);
13571da177e4SLinus Torvalds 			page_cache_release(swappage);
13581da177e4SLinus Torvalds 			flush_dcache_page(filepage);
13591da177e4SLinus Torvalds 			SetPageUptodate(filepage);
13601da177e4SLinus Torvalds 			set_page_dirty(filepage);
13611da177e4SLinus Torvalds 			swap_free(swap);
1362e286781dSNick Piggin 		} else if (!(error = add_to_page_cache_locked(swappage, mapping,
1363e286781dSNick Piggin 					idx, GFP_NOWAIT))) {
13641da177e4SLinus Torvalds 			info->flags |= SHMEM_PAGEIN;
13651da177e4SLinus Torvalds 			shmem_swp_set(info, entry, 0);
13661da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
136773b1262fSHugh Dickins 			delete_from_swap_cache(swappage);
13681da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13691da177e4SLinus Torvalds 			filepage = swappage;
137073b1262fSHugh Dickins 			set_page_dirty(filepage);
13711da177e4SLinus Torvalds 			swap_free(swap);
13721da177e4SLinus Torvalds 		} else {
13731da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13741da177e4SLinus Torvalds 			spin_unlock(&info->lock);
137582369553SHugh Dickins 			if (error == -ENOMEM) {
1376ae3abae6SDaisuke Nishimura 				/*
1377ae3abae6SDaisuke Nishimura 				 * reclaim from proper memory cgroup and
1378ae3abae6SDaisuke Nishimura 				 * call memcg's OOM if needed.
1379ae3abae6SDaisuke Nishimura 				 */
1380ae3abae6SDaisuke Nishimura 				error = mem_cgroup_shmem_charge_fallback(
1381ae3abae6SDaisuke Nishimura 								swappage,
1382b5a84319SKAMEZAWA Hiroyuki 								current->mm,
1383c9b0ed51SKAMEZAWA Hiroyuki 								gfp);
1384b5a84319SKAMEZAWA Hiroyuki 				if (error) {
1385b5a84319SKAMEZAWA Hiroyuki 					unlock_page(swappage);
1386b5a84319SKAMEZAWA Hiroyuki 					page_cache_release(swappage);
138782369553SHugh Dickins 					goto failed;
138882369553SHugh Dickins 				}
1389b5a84319SKAMEZAWA Hiroyuki 			}
1390b5a84319SKAMEZAWA Hiroyuki 			unlock_page(swappage);
1391b5a84319SKAMEZAWA Hiroyuki 			page_cache_release(swappage);
13921da177e4SLinus Torvalds 			goto repeat;
13931da177e4SLinus Torvalds 		}
13941da177e4SLinus Torvalds 	} else if (sgp == SGP_READ && !filepage) {
13951da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
13961da177e4SLinus Torvalds 		filepage = find_get_page(mapping, idx);
13971da177e4SLinus Torvalds 		if (filepage &&
1398529ae9aaSNick Piggin 		    (!PageUptodate(filepage) || !trylock_page(filepage))) {
13991da177e4SLinus Torvalds 			spin_unlock(&info->lock);
14001da177e4SLinus Torvalds 			wait_on_page_locked(filepage);
14011da177e4SLinus Torvalds 			page_cache_release(filepage);
14021da177e4SLinus Torvalds 			filepage = NULL;
14031da177e4SLinus Torvalds 			goto repeat;
14041da177e4SLinus Torvalds 		}
14051da177e4SLinus Torvalds 		spin_unlock(&info->lock);
1406*e83c32e8SHugh Dickins 
1407*e83c32e8SHugh Dickins 	} else if (prealloc_page) {
14081da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
14091da177e4SLinus Torvalds 		sbinfo = SHMEM_SB(inode->i_sb);
14100edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
1411fc5da22aSHugh Dickins 			if (percpu_counter_compare(&sbinfo->used_blocks,
1412fc5da22aSHugh Dickins 						sbinfo->max_blocks) >= 0 ||
141359a16eadSHugh Dickins 			    shmem_acct_block(info->flags))
141459a16eadSHugh Dickins 				goto nospace;
14157e496299STim Chen 			percpu_counter_inc(&sbinfo->used_blocks);
14161da177e4SLinus Torvalds 			inode->i_blocks += BLOCKS_PER_PAGE;
141759a16eadSHugh Dickins 		} else if (shmem_acct_block(info->flags))
141859a16eadSHugh Dickins 			goto nospace;
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 		if (!filepage) {
142169029cd5SKAMEZAWA Hiroyuki 			int ret;
142269029cd5SKAMEZAWA Hiroyuki 
1423ff36b801SShaohua Li 			filepage = prealloc_page;
1424ff36b801SShaohua Li 			prealloc_page = NULL;
1425ff36b801SShaohua Li 
142668da9f05SHugh Dickins 			entry = shmem_swp_alloc(info, idx, sgp, gfp);
14271da177e4SLinus Torvalds 			if (IS_ERR(entry))
14281da177e4SLinus Torvalds 				error = PTR_ERR(entry);
14291da177e4SLinus Torvalds 			else {
14301da177e4SLinus Torvalds 				swap = *entry;
14311da177e4SLinus Torvalds 				shmem_swp_unmap(entry);
14321da177e4SLinus Torvalds 			}
143369029cd5SKAMEZAWA Hiroyuki 			ret = error || swap.val;
143469029cd5SKAMEZAWA Hiroyuki 			if (ret)
143569029cd5SKAMEZAWA Hiroyuki 				mem_cgroup_uncharge_cache_page(filepage);
143669029cd5SKAMEZAWA Hiroyuki 			else
143769029cd5SKAMEZAWA Hiroyuki 				ret = add_to_page_cache_lru(filepage, mapping,
143869029cd5SKAMEZAWA Hiroyuki 						idx, GFP_NOWAIT);
143969029cd5SKAMEZAWA Hiroyuki 			/*
144069029cd5SKAMEZAWA Hiroyuki 			 * At add_to_page_cache_lru() failure, uncharge will
144169029cd5SKAMEZAWA Hiroyuki 			 * be done automatically.
144269029cd5SKAMEZAWA Hiroyuki 			 */
144369029cd5SKAMEZAWA Hiroyuki 			if (ret) {
14441da177e4SLinus Torvalds 				shmem_unacct_blocks(info->flags, 1);
14451da177e4SLinus Torvalds 				shmem_free_blocks(inode, 1);
1446d515afe8SHugh Dickins 				spin_unlock(&info->lock);
1447d515afe8SHugh Dickins 				page_cache_release(filepage);
14481da177e4SLinus Torvalds 				filepage = NULL;
14491da177e4SLinus Torvalds 				if (error)
14501da177e4SLinus Torvalds 					goto failed;
14511da177e4SLinus Torvalds 				goto repeat;
14521da177e4SLinus Torvalds 			}
14531da177e4SLinus Torvalds 			info->flags |= SHMEM_PAGEIN;
14541da177e4SLinus Torvalds 		}
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds 		info->alloced++;
14571da177e4SLinus Torvalds 		spin_unlock(&info->lock);
1458e84e2e13SHugh Dickins 		clear_highpage(filepage);
14591da177e4SLinus Torvalds 		flush_dcache_page(filepage);
14601da177e4SLinus Torvalds 		SetPageUptodate(filepage);
1461a0ee5ec5SHugh Dickins 		if (sgp == SGP_DIRTY)
1462a0ee5ec5SHugh Dickins 			set_page_dirty(filepage);
1463*e83c32e8SHugh Dickins 	} else {
1464*e83c32e8SHugh Dickins 		spin_unlock(&info->lock);
1465*e83c32e8SHugh Dickins 		error = -ENOMEM;
1466*e83c32e8SHugh Dickins 		goto out;
14671da177e4SLinus Torvalds 	}
14681da177e4SLinus Torvalds done:
14691da177e4SLinus Torvalds 	*pagep = filepage;
1470ff36b801SShaohua Li 	error = 0;
1471*e83c32e8SHugh Dickins out:
1472*e83c32e8SHugh Dickins 	if (prealloc_page) {
1473*e83c32e8SHugh Dickins 		mem_cgroup_uncharge_cache_page(prealloc_page);
1474*e83c32e8SHugh Dickins 		page_cache_release(prealloc_page);
1475*e83c32e8SHugh Dickins 	}
1476*e83c32e8SHugh Dickins 	return error;
14771da177e4SLinus Torvalds 
147859a16eadSHugh Dickins nospace:
147959a16eadSHugh Dickins 	/*
148059a16eadSHugh Dickins 	 * Perhaps the page was brought in from swap between find_lock_page
148159a16eadSHugh Dickins 	 * and taking info->lock?  We allow for that at add_to_page_cache_lru,
148259a16eadSHugh Dickins 	 * but must also avoid reporting a spurious ENOSPC while working on a
14839276aad6SHugh Dickins 	 * full tmpfs.
148459a16eadSHugh Dickins 	 */
148559a16eadSHugh Dickins 	if (!filepage) {
148659a16eadSHugh Dickins 		struct page *page = find_get_page(mapping, idx);
148759a16eadSHugh Dickins 		if (page) {
148859a16eadSHugh Dickins 			spin_unlock(&info->lock);
148959a16eadSHugh Dickins 			page_cache_release(page);
149059a16eadSHugh Dickins 			goto repeat;
149159a16eadSHugh Dickins 		}
149259a16eadSHugh Dickins 	}
149359a16eadSHugh Dickins 	spin_unlock(&info->lock);
149459a16eadSHugh Dickins 	error = -ENOSPC;
14951da177e4SLinus Torvalds failed:
14969276aad6SHugh Dickins 	if (filepage) {
14971da177e4SLinus Torvalds 		unlock_page(filepage);
14981da177e4SLinus Torvalds 		page_cache_release(filepage);
14991da177e4SLinus Torvalds 	}
1500*e83c32e8SHugh Dickins 	goto out;
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds 
1503d0217ac0SNick Piggin static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
15041da177e4SLinus Torvalds {
1505d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
15061da177e4SLinus Torvalds 	int error;
150768da9f05SHugh Dickins 	int ret = VM_FAULT_LOCKED;
15081da177e4SLinus Torvalds 
1509d0217ac0SNick Piggin 	if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1510d0217ac0SNick Piggin 		return VM_FAULT_SIGBUS;
1511d00806b1SNick Piggin 
151227d54b39SHugh Dickins 	error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1513d0217ac0SNick Piggin 	if (error)
1514d0217ac0SNick Piggin 		return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
151568da9f05SHugh Dickins 
1516456f998eSYing Han 	if (ret & VM_FAULT_MAJOR) {
1517456f998eSYing Han 		count_vm_event(PGMAJFAULT);
1518456f998eSYing Han 		mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1519456f998eSYing Han 	}
152068da9f05SHugh Dickins 	return ret;
15211da177e4SLinus Torvalds }
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1524d8dc74f2SAdrian Bunk static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
15251da177e4SLinus Torvalds {
1526d3ac7f89SJosef "Jeff" Sipek 	struct inode *i = vma->vm_file->f_path.dentry->d_inode;
15271da177e4SLinus Torvalds 	return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
1530d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1531d8dc74f2SAdrian Bunk 					  unsigned long addr)
15321da177e4SLinus Torvalds {
1533d3ac7f89SJosef "Jeff" Sipek 	struct inode *i = vma->vm_file->f_path.dentry->d_inode;
15341da177e4SLinus Torvalds 	unsigned long idx;
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds 	idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
15371da177e4SLinus Torvalds 	return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds #endif
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user)
15421da177e4SLinus Torvalds {
1543d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
15441da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
15451da177e4SLinus Torvalds 	int retval = -ENOMEM;
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	spin_lock(&info->lock);
15481da177e4SLinus Torvalds 	if (lock && !(info->flags & VM_LOCKED)) {
15491da177e4SLinus Torvalds 		if (!user_shm_lock(inode->i_size, user))
15501da177e4SLinus Torvalds 			goto out_nomem;
15511da177e4SLinus Torvalds 		info->flags |= VM_LOCKED;
155289e004eaSLee Schermerhorn 		mapping_set_unevictable(file->f_mapping);
15531da177e4SLinus Torvalds 	}
15541da177e4SLinus Torvalds 	if (!lock && (info->flags & VM_LOCKED) && user) {
15551da177e4SLinus Torvalds 		user_shm_unlock(inode->i_size, user);
15561da177e4SLinus Torvalds 		info->flags &= ~VM_LOCKED;
155789e004eaSLee Schermerhorn 		mapping_clear_unevictable(file->f_mapping);
155889e004eaSLee Schermerhorn 		scan_mapping_unevictable_pages(file->f_mapping);
15591da177e4SLinus Torvalds 	}
15601da177e4SLinus Torvalds 	retval = 0;
156189e004eaSLee Schermerhorn 
15621da177e4SLinus Torvalds out_nomem:
15631da177e4SLinus Torvalds 	spin_unlock(&info->lock);
15641da177e4SLinus Torvalds 	return retval;
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
15679b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
15681da177e4SLinus Torvalds {
15691da177e4SLinus Torvalds 	file_accessed(file);
15701da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
1571d0217ac0SNick Piggin 	vma->vm_flags |= VM_CAN_NONLINEAR;
15721da177e4SLinus Torvalds 	return 0;
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
1575454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1576454abafeSDmitry Monakhov 				     int mode, dev_t dev, unsigned long flags)
15771da177e4SLinus Torvalds {
15781da177e4SLinus Torvalds 	struct inode *inode;
15791da177e4SLinus Torvalds 	struct shmem_inode_info *info;
15801da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
15811da177e4SLinus Torvalds 
15825b04c689SPavel Emelyanov 	if (shmem_reserve_inode(sb))
15831da177e4SLinus Torvalds 		return NULL;
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds 	inode = new_inode(sb);
15861da177e4SLinus Torvalds 	if (inode) {
158785fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
1588454abafeSDmitry Monakhov 		inode_init_owner(inode, dir, mode);
15891da177e4SLinus Torvalds 		inode->i_blocks = 0;
15901da177e4SLinus Torvalds 		inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
15911da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
159291828a40SDavid M. Grimes 		inode->i_generation = get_seconds();
15931da177e4SLinus Torvalds 		info = SHMEM_I(inode);
15941da177e4SLinus Torvalds 		memset(info, 0, (char *)inode - (char *)info);
15951da177e4SLinus Torvalds 		spin_lock_init(&info->lock);
15960b0a0806SHugh Dickins 		info->flags = flags & VM_NORESERVE;
15971da177e4SLinus Torvalds 		INIT_LIST_HEAD(&info->swaplist);
1598b09e0fa4SEric Paris 		INIT_LIST_HEAD(&info->xattr_list);
159972c04902SAl Viro 		cache_no_acl(inode);
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
16021da177e4SLinus Torvalds 		default:
160339f0247dSAndreas Gruenbacher 			inode->i_op = &shmem_special_inode_operations;
16041da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
16051da177e4SLinus Torvalds 			break;
16061da177e4SLinus Torvalds 		case S_IFREG:
160714fcc23fSHugh Dickins 			inode->i_mapping->a_ops = &shmem_aops;
16081da177e4SLinus Torvalds 			inode->i_op = &shmem_inode_operations;
16091da177e4SLinus Torvalds 			inode->i_fop = &shmem_file_operations;
161071fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy,
161171fe804bSLee Schermerhorn 						 shmem_get_sbmpol(sbinfo));
16121da177e4SLinus Torvalds 			break;
16131da177e4SLinus Torvalds 		case S_IFDIR:
1614d8c76e6fSDave Hansen 			inc_nlink(inode);
16151da177e4SLinus Torvalds 			/* Some things misbehave if size == 0 on a directory */
16161da177e4SLinus Torvalds 			inode->i_size = 2 * BOGO_DIRENT_SIZE;
16171da177e4SLinus Torvalds 			inode->i_op = &shmem_dir_inode_operations;
16181da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
16191da177e4SLinus Torvalds 			break;
16201da177e4SLinus Torvalds 		case S_IFLNK:
16211da177e4SLinus Torvalds 			/*
16221da177e4SLinus Torvalds 			 * Must not load anything in the rbtree,
16231da177e4SLinus Torvalds 			 * mpol_free_shared_policy will not be called.
16241da177e4SLinus Torvalds 			 */
162571fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy, NULL);
16261da177e4SLinus Torvalds 			break;
16271da177e4SLinus Torvalds 		}
16285b04c689SPavel Emelyanov 	} else
16295b04c689SPavel Emelyanov 		shmem_free_inode(sb);
16301da177e4SLinus Torvalds 	return inode;
16311da177e4SLinus Torvalds }
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
163492e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations;
163592e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations;
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds static int
1638800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping,
1639800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
1640800d15a5SNick Piggin 			struct page **pagep, void **fsdata)
16411da177e4SLinus Torvalds {
1642800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1643800d15a5SNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1644800d15a5SNick Piggin 	return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1645800d15a5SNick Piggin }
1646800d15a5SNick Piggin 
1647800d15a5SNick Piggin static int
1648800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping,
1649800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
1650800d15a5SNick Piggin 			struct page *page, void *fsdata)
1651800d15a5SNick Piggin {
1652800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1653800d15a5SNick Piggin 
1654800d15a5SNick Piggin 	if (pos + copied > inode->i_size)
1655800d15a5SNick Piggin 		i_size_write(inode, pos + copied);
1656800d15a5SNick Piggin 
1657d3602444SHugh Dickins 	set_page_dirty(page);
16586746aff7SWu Fengguang 	unlock_page(page);
1659d3602444SHugh Dickins 	page_cache_release(page);
1660d3602444SHugh Dickins 
1661800d15a5SNick Piggin 	return copied;
16621da177e4SLinus Torvalds }
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
16651da177e4SLinus Torvalds {
1666d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
16671da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
16681da177e4SLinus Torvalds 	unsigned long index, offset;
1669a0ee5ec5SHugh Dickins 	enum sgp_type sgp = SGP_READ;
1670a0ee5ec5SHugh Dickins 
1671a0ee5ec5SHugh Dickins 	/*
1672a0ee5ec5SHugh Dickins 	 * Might this read be for a stacking filesystem?  Then when reading
1673a0ee5ec5SHugh Dickins 	 * holes of a sparse file, we actually need to allocate those pages,
1674a0ee5ec5SHugh Dickins 	 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1675a0ee5ec5SHugh Dickins 	 */
1676a0ee5ec5SHugh Dickins 	if (segment_eq(get_fs(), KERNEL_DS))
1677a0ee5ec5SHugh Dickins 		sgp = SGP_DIRTY;
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 	index = *ppos >> PAGE_CACHE_SHIFT;
16801da177e4SLinus Torvalds 	offset = *ppos & ~PAGE_CACHE_MASK;
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 	for (;;) {
16831da177e4SLinus Torvalds 		struct page *page = NULL;
16841da177e4SLinus Torvalds 		unsigned long end_index, nr, ret;
16851da177e4SLinus Torvalds 		loff_t i_size = i_size_read(inode);
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
16881da177e4SLinus Torvalds 		if (index > end_index)
16891da177e4SLinus Torvalds 			break;
16901da177e4SLinus Torvalds 		if (index == end_index) {
16911da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
16921da177e4SLinus Torvalds 			if (nr <= offset)
16931da177e4SLinus Torvalds 				break;
16941da177e4SLinus Torvalds 		}
16951da177e4SLinus Torvalds 
1696a0ee5ec5SHugh Dickins 		desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
16971da177e4SLinus Torvalds 		if (desc->error) {
16981da177e4SLinus Torvalds 			if (desc->error == -EINVAL)
16991da177e4SLinus Torvalds 				desc->error = 0;
17001da177e4SLinus Torvalds 			break;
17011da177e4SLinus Torvalds 		}
1702d3602444SHugh Dickins 		if (page)
1703d3602444SHugh Dickins 			unlock_page(page);
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 		/*
17061da177e4SLinus Torvalds 		 * We must evaluate after, since reads (unlike writes)
17071b1dcc1bSJes Sorensen 		 * are called without i_mutex protection against truncate
17081da177e4SLinus Torvalds 		 */
17091da177e4SLinus Torvalds 		nr = PAGE_CACHE_SIZE;
17101da177e4SLinus Torvalds 		i_size = i_size_read(inode);
17111da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
17121da177e4SLinus Torvalds 		if (index == end_index) {
17131da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
17141da177e4SLinus Torvalds 			if (nr <= offset) {
17151da177e4SLinus Torvalds 				if (page)
17161da177e4SLinus Torvalds 					page_cache_release(page);
17171da177e4SLinus Torvalds 				break;
17181da177e4SLinus Torvalds 			}
17191da177e4SLinus Torvalds 		}
17201da177e4SLinus Torvalds 		nr -= offset;
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 		if (page) {
17231da177e4SLinus Torvalds 			/*
17241da177e4SLinus Torvalds 			 * If users can be writing to this page using arbitrary
17251da177e4SLinus Torvalds 			 * virtual addresses, take care about potential aliasing
17261da177e4SLinus Torvalds 			 * before reading the page on the kernel side.
17271da177e4SLinus Torvalds 			 */
17281da177e4SLinus Torvalds 			if (mapping_writably_mapped(mapping))
17291da177e4SLinus Torvalds 				flush_dcache_page(page);
17301da177e4SLinus Torvalds 			/*
17311da177e4SLinus Torvalds 			 * Mark the page accessed if we read the beginning.
17321da177e4SLinus Torvalds 			 */
17331da177e4SLinus Torvalds 			if (!offset)
17341da177e4SLinus Torvalds 				mark_page_accessed(page);
1735b5810039SNick Piggin 		} else {
17361da177e4SLinus Torvalds 			page = ZERO_PAGE(0);
1737b5810039SNick Piggin 			page_cache_get(page);
1738b5810039SNick Piggin 		}
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds 		/*
17411da177e4SLinus Torvalds 		 * Ok, we have the page, and it's up-to-date, so
17421da177e4SLinus Torvalds 		 * now we can copy it to user space...
17431da177e4SLinus Torvalds 		 *
17441da177e4SLinus Torvalds 		 * The actor routine returns how many bytes were actually used..
17451da177e4SLinus Torvalds 		 * NOTE! This may not be the same as how much of a user buffer
17461da177e4SLinus Torvalds 		 * we filled up (we may be padding etc), so we can only update
17471da177e4SLinus Torvalds 		 * "pos" here (the actor routine has to update the user buffer
17481da177e4SLinus Torvalds 		 * pointers and the remaining count).
17491da177e4SLinus Torvalds 		 */
17501da177e4SLinus Torvalds 		ret = actor(desc, page, offset, nr);
17511da177e4SLinus Torvalds 		offset += ret;
17521da177e4SLinus Torvalds 		index += offset >> PAGE_CACHE_SHIFT;
17531da177e4SLinus Torvalds 		offset &= ~PAGE_CACHE_MASK;
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 		page_cache_release(page);
17561da177e4SLinus Torvalds 		if (ret != nr || !desc->count)
17571da177e4SLinus Torvalds 			break;
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds 		cond_resched();
17601da177e4SLinus Torvalds 	}
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 	*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
17631da177e4SLinus Torvalds 	file_accessed(filp);
17641da177e4SLinus Torvalds }
17651da177e4SLinus Torvalds 
1766bcd78e49SHugh Dickins static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1767bcd78e49SHugh Dickins 		const struct iovec *iov, unsigned long nr_segs, loff_t pos)
17681da177e4SLinus Torvalds {
1769bcd78e49SHugh Dickins 	struct file *filp = iocb->ki_filp;
1770bcd78e49SHugh Dickins 	ssize_t retval;
1771bcd78e49SHugh Dickins 	unsigned long seg;
1772bcd78e49SHugh Dickins 	size_t count;
1773bcd78e49SHugh Dickins 	loff_t *ppos = &iocb->ki_pos;
1774bcd78e49SHugh Dickins 
1775bcd78e49SHugh Dickins 	retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1776bcd78e49SHugh Dickins 	if (retval)
1777bcd78e49SHugh Dickins 		return retval;
1778bcd78e49SHugh Dickins 
1779bcd78e49SHugh Dickins 	for (seg = 0; seg < nr_segs; seg++) {
17801da177e4SLinus Torvalds 		read_descriptor_t desc;
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds 		desc.written = 0;
1783bcd78e49SHugh Dickins 		desc.arg.buf = iov[seg].iov_base;
1784bcd78e49SHugh Dickins 		desc.count = iov[seg].iov_len;
1785bcd78e49SHugh Dickins 		if (desc.count == 0)
1786bcd78e49SHugh Dickins 			continue;
17871da177e4SLinus Torvalds 		desc.error = 0;
17881da177e4SLinus Torvalds 		do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1789bcd78e49SHugh Dickins 		retval += desc.written;
1790bcd78e49SHugh Dickins 		if (desc.error) {
1791bcd78e49SHugh Dickins 			retval = retval ?: desc.error;
1792bcd78e49SHugh Dickins 			break;
1793bcd78e49SHugh Dickins 		}
1794bcd78e49SHugh Dickins 		if (desc.count > 0)
1795bcd78e49SHugh Dickins 			break;
1796bcd78e49SHugh Dickins 	}
1797bcd78e49SHugh Dickins 	return retval;
17981da177e4SLinus Torvalds }
17991da177e4SLinus Torvalds 
1800708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1801708e3508SHugh Dickins 				struct pipe_inode_info *pipe, size_t len,
1802708e3508SHugh Dickins 				unsigned int flags)
1803708e3508SHugh Dickins {
1804708e3508SHugh Dickins 	struct address_space *mapping = in->f_mapping;
180571f0e07aSHugh Dickins 	struct inode *inode = mapping->host;
1806708e3508SHugh Dickins 	unsigned int loff, nr_pages, req_pages;
1807708e3508SHugh Dickins 	struct page *pages[PIPE_DEF_BUFFERS];
1808708e3508SHugh Dickins 	struct partial_page partial[PIPE_DEF_BUFFERS];
1809708e3508SHugh Dickins 	struct page *page;
1810708e3508SHugh Dickins 	pgoff_t index, end_index;
1811708e3508SHugh Dickins 	loff_t isize, left;
1812708e3508SHugh Dickins 	int error, page_nr;
1813708e3508SHugh Dickins 	struct splice_pipe_desc spd = {
1814708e3508SHugh Dickins 		.pages = pages,
1815708e3508SHugh Dickins 		.partial = partial,
1816708e3508SHugh Dickins 		.flags = flags,
1817708e3508SHugh Dickins 		.ops = &page_cache_pipe_buf_ops,
1818708e3508SHugh Dickins 		.spd_release = spd_release_page,
1819708e3508SHugh Dickins 	};
1820708e3508SHugh Dickins 
182171f0e07aSHugh Dickins 	isize = i_size_read(inode);
1822708e3508SHugh Dickins 	if (unlikely(*ppos >= isize))
1823708e3508SHugh Dickins 		return 0;
1824708e3508SHugh Dickins 
1825708e3508SHugh Dickins 	left = isize - *ppos;
1826708e3508SHugh Dickins 	if (unlikely(left < len))
1827708e3508SHugh Dickins 		len = left;
1828708e3508SHugh Dickins 
1829708e3508SHugh Dickins 	if (splice_grow_spd(pipe, &spd))
1830708e3508SHugh Dickins 		return -ENOMEM;
1831708e3508SHugh Dickins 
1832708e3508SHugh Dickins 	index = *ppos >> PAGE_CACHE_SHIFT;
1833708e3508SHugh Dickins 	loff = *ppos & ~PAGE_CACHE_MASK;
1834708e3508SHugh Dickins 	req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1835708e3508SHugh Dickins 	nr_pages = min(req_pages, pipe->buffers);
1836708e3508SHugh Dickins 
1837708e3508SHugh Dickins 	spd.nr_pages = find_get_pages_contig(mapping, index,
1838708e3508SHugh Dickins 						nr_pages, spd.pages);
1839708e3508SHugh Dickins 	index += spd.nr_pages;
1840708e3508SHugh Dickins 	error = 0;
184171f0e07aSHugh Dickins 
1842708e3508SHugh Dickins 	while (spd.nr_pages < nr_pages) {
184371f0e07aSHugh Dickins 		error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
184471f0e07aSHugh Dickins 		if (error)
1845708e3508SHugh Dickins 			break;
1846708e3508SHugh Dickins 		unlock_page(page);
1847708e3508SHugh Dickins 		spd.pages[spd.nr_pages++] = page;
1848708e3508SHugh Dickins 		index++;
1849708e3508SHugh Dickins 	}
1850708e3508SHugh Dickins 
1851708e3508SHugh Dickins 	index = *ppos >> PAGE_CACHE_SHIFT;
1852708e3508SHugh Dickins 	nr_pages = spd.nr_pages;
1853708e3508SHugh Dickins 	spd.nr_pages = 0;
185471f0e07aSHugh Dickins 
1855708e3508SHugh Dickins 	for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1856708e3508SHugh Dickins 		unsigned int this_len;
1857708e3508SHugh Dickins 
1858708e3508SHugh Dickins 		if (!len)
1859708e3508SHugh Dickins 			break;
1860708e3508SHugh Dickins 
1861708e3508SHugh Dickins 		this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1862708e3508SHugh Dickins 		page = spd.pages[page_nr];
1863708e3508SHugh Dickins 
186471f0e07aSHugh Dickins 		if (!PageUptodate(page) || page->mapping != mapping) {
186571f0e07aSHugh Dickins 			error = shmem_getpage(inode, index, &page,
186671f0e07aSHugh Dickins 							SGP_CACHE, NULL);
186771f0e07aSHugh Dickins 			if (error)
1868708e3508SHugh Dickins 				break;
186971f0e07aSHugh Dickins 			unlock_page(page);
1870708e3508SHugh Dickins 			page_cache_release(spd.pages[page_nr]);
1871708e3508SHugh Dickins 			spd.pages[page_nr] = page;
1872708e3508SHugh Dickins 		}
1873708e3508SHugh Dickins 
187471f0e07aSHugh Dickins 		isize = i_size_read(inode);
1875708e3508SHugh Dickins 		end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1876708e3508SHugh Dickins 		if (unlikely(!isize || index > end_index))
1877708e3508SHugh Dickins 			break;
1878708e3508SHugh Dickins 
1879708e3508SHugh Dickins 		if (end_index == index) {
1880708e3508SHugh Dickins 			unsigned int plen;
1881708e3508SHugh Dickins 
1882708e3508SHugh Dickins 			plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1883708e3508SHugh Dickins 			if (plen <= loff)
1884708e3508SHugh Dickins 				break;
1885708e3508SHugh Dickins 
1886708e3508SHugh Dickins 			this_len = min(this_len, plen - loff);
1887708e3508SHugh Dickins 			len = this_len;
1888708e3508SHugh Dickins 		}
1889708e3508SHugh Dickins 
1890708e3508SHugh Dickins 		spd.partial[page_nr].offset = loff;
1891708e3508SHugh Dickins 		spd.partial[page_nr].len = this_len;
1892708e3508SHugh Dickins 		len -= this_len;
1893708e3508SHugh Dickins 		loff = 0;
1894708e3508SHugh Dickins 		spd.nr_pages++;
1895708e3508SHugh Dickins 		index++;
1896708e3508SHugh Dickins 	}
1897708e3508SHugh Dickins 
1898708e3508SHugh Dickins 	while (page_nr < nr_pages)
1899708e3508SHugh Dickins 		page_cache_release(spd.pages[page_nr++]);
1900708e3508SHugh Dickins 
1901708e3508SHugh Dickins 	if (spd.nr_pages)
1902708e3508SHugh Dickins 		error = splice_to_pipe(pipe, &spd);
1903708e3508SHugh Dickins 
1904708e3508SHugh Dickins 	splice_shrink_spd(pipe, &spd);
1905708e3508SHugh Dickins 
1906708e3508SHugh Dickins 	if (error > 0) {
1907708e3508SHugh Dickins 		*ppos += error;
1908708e3508SHugh Dickins 		file_accessed(in);
1909708e3508SHugh Dickins 	}
1910708e3508SHugh Dickins 	return error;
1911708e3508SHugh Dickins }
1912708e3508SHugh Dickins 
1913726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
19141da177e4SLinus Torvalds {
1915726c3342SDavid Howells 	struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds 	buf->f_type = TMPFS_MAGIC;
19181da177e4SLinus Torvalds 	buf->f_bsize = PAGE_CACHE_SIZE;
19191da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
19200edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
19211da177e4SLinus Torvalds 		buf->f_blocks = sbinfo->max_blocks;
19227e496299STim Chen 		buf->f_bavail = buf->f_bfree =
19237e496299STim Chen 				sbinfo->max_blocks - percpu_counter_sum(&sbinfo->used_blocks);
19240edd73b3SHugh Dickins 	}
19250edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
19261da177e4SLinus Torvalds 		buf->f_files = sbinfo->max_inodes;
19271da177e4SLinus Torvalds 		buf->f_ffree = sbinfo->free_inodes;
19281da177e4SLinus Torvalds 	}
19291da177e4SLinus Torvalds 	/* else leave those fields 0 like simple_statfs */
19301da177e4SLinus Torvalds 	return 0;
19311da177e4SLinus Torvalds }
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds /*
19341da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
19351da177e4SLinus Torvalds  */
19361da177e4SLinus Torvalds static int
19371da177e4SLinus Torvalds shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
19381da177e4SLinus Torvalds {
19390b0a0806SHugh Dickins 	struct inode *inode;
19401da177e4SLinus Torvalds 	int error = -ENOSPC;
19411da177e4SLinus Torvalds 
1942454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
19431da177e4SLinus Torvalds 	if (inode) {
19442a7dba39SEric Paris 		error = security_inode_init_security(inode, dir,
19452a7dba39SEric Paris 						     &dentry->d_name, NULL,
19462a7dba39SEric Paris 						     NULL, NULL);
1947570bc1c2SStephen Smalley 		if (error) {
1948570bc1c2SStephen Smalley 			if (error != -EOPNOTSUPP) {
1949570bc1c2SStephen Smalley 				iput(inode);
1950570bc1c2SStephen Smalley 				return error;
1951570bc1c2SStephen Smalley 			}
195239f0247dSAndreas Gruenbacher 		}
19531c7c474cSChristoph Hellwig #ifdef CONFIG_TMPFS_POSIX_ACL
19541c7c474cSChristoph Hellwig 		error = generic_acl_init(inode, dir);
195539f0247dSAndreas Gruenbacher 		if (error) {
195639f0247dSAndreas Gruenbacher 			iput(inode);
195739f0247dSAndreas Gruenbacher 			return error;
1958570bc1c2SStephen Smalley 		}
1959718deb6bSAl Viro #else
1960718deb6bSAl Viro 		error = 0;
19611c7c474cSChristoph Hellwig #endif
19621da177e4SLinus Torvalds 		dir->i_size += BOGO_DIRENT_SIZE;
19631da177e4SLinus Torvalds 		dir->i_ctime = dir->i_mtime = CURRENT_TIME;
19641da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
19651da177e4SLinus Torvalds 		dget(dentry); /* Extra count - pin the dentry in core */
19661da177e4SLinus Torvalds 	}
19671da177e4SLinus Torvalds 	return error;
19681da177e4SLinus Torvalds }
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
19711da177e4SLinus Torvalds {
19721da177e4SLinus Torvalds 	int error;
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 	if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
19751da177e4SLinus Torvalds 		return error;
1976d8c76e6fSDave Hansen 	inc_nlink(dir);
19771da177e4SLinus Torvalds 	return 0;
19781da177e4SLinus Torvalds }
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
19811da177e4SLinus Torvalds 		struct nameidata *nd)
19821da177e4SLinus Torvalds {
19831da177e4SLinus Torvalds 	return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
19841da177e4SLinus Torvalds }
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds /*
19871da177e4SLinus Torvalds  * Link a file..
19881da177e4SLinus Torvalds  */
19891da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
19901da177e4SLinus Torvalds {
19911da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
19925b04c689SPavel Emelyanov 	int ret;
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	/*
19951da177e4SLinus Torvalds 	 * No ordinary (disk based) filesystem counts links as inodes;
19961da177e4SLinus Torvalds 	 * but each new link needs a new dentry, pinning lowmem, and
19971da177e4SLinus Torvalds 	 * tmpfs dentries cannot be pruned until they are unlinked.
19981da177e4SLinus Torvalds 	 */
19995b04c689SPavel Emelyanov 	ret = shmem_reserve_inode(inode->i_sb);
20005b04c689SPavel Emelyanov 	if (ret)
20015b04c689SPavel Emelyanov 		goto out;
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
20041da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2005d8c76e6fSDave Hansen 	inc_nlink(inode);
20067de9c6eeSAl Viro 	ihold(inode);	/* New dentry reference */
20071da177e4SLinus Torvalds 	dget(dentry);		/* Extra pinning count for the created dentry */
20081da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
20095b04c689SPavel Emelyanov out:
20105b04c689SPavel Emelyanov 	return ret;
20111da177e4SLinus Torvalds }
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry)
20141da177e4SLinus Torvalds {
20151da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
20161da177e4SLinus Torvalds 
20175b04c689SPavel Emelyanov 	if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
20185b04c689SPavel Emelyanov 		shmem_free_inode(inode->i_sb);
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	dir->i_size -= BOGO_DIRENT_SIZE;
20211da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
20229a53c3a7SDave Hansen 	drop_nlink(inode);
20231da177e4SLinus Torvalds 	dput(dentry);	/* Undo the count from "create" - this does all the work */
20241da177e4SLinus Torvalds 	return 0;
20251da177e4SLinus Torvalds }
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
20281da177e4SLinus Torvalds {
20291da177e4SLinus Torvalds 	if (!simple_empty(dentry))
20301da177e4SLinus Torvalds 		return -ENOTEMPTY;
20311da177e4SLinus Torvalds 
20329a53c3a7SDave Hansen 	drop_nlink(dentry->d_inode);
20339a53c3a7SDave Hansen 	drop_nlink(dir);
20341da177e4SLinus Torvalds 	return shmem_unlink(dir, dentry);
20351da177e4SLinus Torvalds }
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds /*
20381da177e4SLinus Torvalds  * The VFS layer already does all the dentry stuff for rename,
20391da177e4SLinus Torvalds  * we just have to decrement the usage count for the target if
20401da177e4SLinus Torvalds  * it exists so that the VFS layer correctly free's it when it
20411da177e4SLinus Torvalds  * gets overwritten.
20421da177e4SLinus Torvalds  */
20431da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
20441da177e4SLinus Torvalds {
20451da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
20461da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(inode->i_mode);
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
20491da177e4SLinus Torvalds 		return -ENOTEMPTY;
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
20521da177e4SLinus Torvalds 		(void) shmem_unlink(new_dir, new_dentry);
20531da177e4SLinus Torvalds 		if (they_are_dirs)
20549a53c3a7SDave Hansen 			drop_nlink(old_dir);
20551da177e4SLinus Torvalds 	} else if (they_are_dirs) {
20569a53c3a7SDave Hansen 		drop_nlink(old_dir);
2057d8c76e6fSDave Hansen 		inc_nlink(new_dir);
20581da177e4SLinus Torvalds 	}
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds 	old_dir->i_size -= BOGO_DIRENT_SIZE;
20611da177e4SLinus Torvalds 	new_dir->i_size += BOGO_DIRENT_SIZE;
20621da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime =
20631da177e4SLinus Torvalds 	new_dir->i_ctime = new_dir->i_mtime =
20641da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME;
20651da177e4SLinus Torvalds 	return 0;
20661da177e4SLinus Torvalds }
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20691da177e4SLinus Torvalds {
20701da177e4SLinus Torvalds 	int error;
20711da177e4SLinus Torvalds 	int len;
20721da177e4SLinus Torvalds 	struct inode *inode;
20739276aad6SHugh Dickins 	struct page *page;
20741da177e4SLinus Torvalds 	char *kaddr;
20751da177e4SLinus Torvalds 	struct shmem_inode_info *info;
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 	len = strlen(symname) + 1;
20781da177e4SLinus Torvalds 	if (len > PAGE_CACHE_SIZE)
20791da177e4SLinus Torvalds 		return -ENAMETOOLONG;
20801da177e4SLinus Torvalds 
2081454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
20821da177e4SLinus Torvalds 	if (!inode)
20831da177e4SLinus Torvalds 		return -ENOSPC;
20841da177e4SLinus Torvalds 
20852a7dba39SEric Paris 	error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
20862a7dba39SEric Paris 					     NULL, NULL);
2087570bc1c2SStephen Smalley 	if (error) {
2088570bc1c2SStephen Smalley 		if (error != -EOPNOTSUPP) {
2089570bc1c2SStephen Smalley 			iput(inode);
2090570bc1c2SStephen Smalley 			return error;
2091570bc1c2SStephen Smalley 		}
2092570bc1c2SStephen Smalley 		error = 0;
2093570bc1c2SStephen Smalley 	}
2094570bc1c2SStephen Smalley 
20951da177e4SLinus Torvalds 	info = SHMEM_I(inode);
20961da177e4SLinus Torvalds 	inode->i_size = len-1;
2097b09e0fa4SEric Paris 	if (len <= SHMEM_SYMLINK_INLINE_LEN) {
20981da177e4SLinus Torvalds 		/* do it inline */
2099b09e0fa4SEric Paris 		memcpy(info->inline_symlink, symname, len);
21001da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inline_operations;
21011da177e4SLinus Torvalds 	} else {
21021da177e4SLinus Torvalds 		error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
21031da177e4SLinus Torvalds 		if (error) {
21041da177e4SLinus Torvalds 			iput(inode);
21051da177e4SLinus Torvalds 			return error;
21061da177e4SLinus Torvalds 		}
210714fcc23fSHugh Dickins 		inode->i_mapping->a_ops = &shmem_aops;
21081da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inode_operations;
21091da177e4SLinus Torvalds 		kaddr = kmap_atomic(page, KM_USER0);
21101da177e4SLinus Torvalds 		memcpy(kaddr, symname, len);
21111da177e4SLinus Torvalds 		kunmap_atomic(kaddr, KM_USER0);
21121da177e4SLinus Torvalds 		set_page_dirty(page);
21136746aff7SWu Fengguang 		unlock_page(page);
21141da177e4SLinus Torvalds 		page_cache_release(page);
21151da177e4SLinus Torvalds 	}
21161da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
21171da177e4SLinus Torvalds 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
21181da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
21191da177e4SLinus Torvalds 	dget(dentry);
21201da177e4SLinus Torvalds 	return 0;
21211da177e4SLinus Torvalds }
21221da177e4SLinus Torvalds 
2123cc314eefSLinus Torvalds static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
21241da177e4SLinus Torvalds {
2125b09e0fa4SEric Paris 	nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
2126cc314eefSLinus Torvalds 	return NULL;
21271da177e4SLinus Torvalds }
21281da177e4SLinus Torvalds 
2129cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
21301da177e4SLinus Torvalds {
21311da177e4SLinus Torvalds 	struct page *page = NULL;
21321da177e4SLinus Torvalds 	int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
21331da177e4SLinus Torvalds 	nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
2134d3602444SHugh Dickins 	if (page)
2135d3602444SHugh Dickins 		unlock_page(page);
2136cc314eefSLinus Torvalds 	return page;
21371da177e4SLinus Torvalds }
21381da177e4SLinus Torvalds 
2139cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
21401da177e4SLinus Torvalds {
21411da177e4SLinus Torvalds 	if (!IS_ERR(nd_get_link(nd))) {
2142cc314eefSLinus Torvalds 		struct page *page = cookie;
21431da177e4SLinus Torvalds 		kunmap(page);
21441da177e4SLinus Torvalds 		mark_page_accessed(page);
21451da177e4SLinus Torvalds 		page_cache_release(page);
21461da177e4SLinus Torvalds 	}
21471da177e4SLinus Torvalds }
21481da177e4SLinus Torvalds 
2149b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2150b09e0fa4SEric Paris /*
2151b09e0fa4SEric Paris  * Superblocks without xattr inode operations may get some security.* xattr
2152b09e0fa4SEric Paris  * support from the LSM "for free". As soon as we have any other xattrs
2153b09e0fa4SEric Paris  * like ACLs, we also need to implement the security.* handlers at
2154b09e0fa4SEric Paris  * filesystem level, though.
2155b09e0fa4SEric Paris  */
2156b09e0fa4SEric Paris 
2157b09e0fa4SEric Paris static int shmem_xattr_get(struct dentry *dentry, const char *name,
2158b09e0fa4SEric Paris 			   void *buffer, size_t size)
2159b09e0fa4SEric Paris {
2160b09e0fa4SEric Paris 	struct shmem_inode_info *info;
2161b09e0fa4SEric Paris 	struct shmem_xattr *xattr;
2162b09e0fa4SEric Paris 	int ret = -ENODATA;
2163b09e0fa4SEric Paris 
2164b09e0fa4SEric Paris 	info = SHMEM_I(dentry->d_inode);
2165b09e0fa4SEric Paris 
2166b09e0fa4SEric Paris 	spin_lock(&info->lock);
2167b09e0fa4SEric Paris 	list_for_each_entry(xattr, &info->xattr_list, list) {
2168b09e0fa4SEric Paris 		if (strcmp(name, xattr->name))
2169b09e0fa4SEric Paris 			continue;
2170b09e0fa4SEric Paris 
2171b09e0fa4SEric Paris 		ret = xattr->size;
2172b09e0fa4SEric Paris 		if (buffer) {
2173b09e0fa4SEric Paris 			if (size < xattr->size)
2174b09e0fa4SEric Paris 				ret = -ERANGE;
2175b09e0fa4SEric Paris 			else
2176b09e0fa4SEric Paris 				memcpy(buffer, xattr->value, xattr->size);
2177b09e0fa4SEric Paris 		}
2178b09e0fa4SEric Paris 		break;
2179b09e0fa4SEric Paris 	}
2180b09e0fa4SEric Paris 	spin_unlock(&info->lock);
2181b09e0fa4SEric Paris 	return ret;
2182b09e0fa4SEric Paris }
2183b09e0fa4SEric Paris 
2184b09e0fa4SEric Paris static int shmem_xattr_set(struct dentry *dentry, const char *name,
2185b09e0fa4SEric Paris 			   const void *value, size_t size, int flags)
2186b09e0fa4SEric Paris {
2187b09e0fa4SEric Paris 	struct inode *inode = dentry->d_inode;
2188b09e0fa4SEric Paris 	struct shmem_inode_info *info = SHMEM_I(inode);
2189b09e0fa4SEric Paris 	struct shmem_xattr *xattr;
2190b09e0fa4SEric Paris 	struct shmem_xattr *new_xattr = NULL;
2191b09e0fa4SEric Paris 	size_t len;
2192b09e0fa4SEric Paris 	int err = 0;
2193b09e0fa4SEric Paris 
2194b09e0fa4SEric Paris 	/* value == NULL means remove */
2195b09e0fa4SEric Paris 	if (value) {
2196b09e0fa4SEric Paris 		/* wrap around? */
2197b09e0fa4SEric Paris 		len = sizeof(*new_xattr) + size;
2198b09e0fa4SEric Paris 		if (len <= sizeof(*new_xattr))
2199b09e0fa4SEric Paris 			return -ENOMEM;
2200b09e0fa4SEric Paris 
2201b09e0fa4SEric Paris 		new_xattr = kmalloc(len, GFP_KERNEL);
2202b09e0fa4SEric Paris 		if (!new_xattr)
2203b09e0fa4SEric Paris 			return -ENOMEM;
2204b09e0fa4SEric Paris 
2205b09e0fa4SEric Paris 		new_xattr->name = kstrdup(name, GFP_KERNEL);
2206b09e0fa4SEric Paris 		if (!new_xattr->name) {
2207b09e0fa4SEric Paris 			kfree(new_xattr);
2208b09e0fa4SEric Paris 			return -ENOMEM;
2209b09e0fa4SEric Paris 		}
2210b09e0fa4SEric Paris 
2211b09e0fa4SEric Paris 		new_xattr->size = size;
2212b09e0fa4SEric Paris 		memcpy(new_xattr->value, value, size);
2213b09e0fa4SEric Paris 	}
2214b09e0fa4SEric Paris 
2215b09e0fa4SEric Paris 	spin_lock(&info->lock);
2216b09e0fa4SEric Paris 	list_for_each_entry(xattr, &info->xattr_list, list) {
2217b09e0fa4SEric Paris 		if (!strcmp(name, xattr->name)) {
2218b09e0fa4SEric Paris 			if (flags & XATTR_CREATE) {
2219b09e0fa4SEric Paris 				xattr = new_xattr;
2220b09e0fa4SEric Paris 				err = -EEXIST;
2221b09e0fa4SEric Paris 			} else if (new_xattr) {
2222b09e0fa4SEric Paris 				list_replace(&xattr->list, &new_xattr->list);
2223b09e0fa4SEric Paris 			} else {
2224b09e0fa4SEric Paris 				list_del(&xattr->list);
2225b09e0fa4SEric Paris 			}
2226b09e0fa4SEric Paris 			goto out;
2227b09e0fa4SEric Paris 		}
2228b09e0fa4SEric Paris 	}
2229b09e0fa4SEric Paris 	if (flags & XATTR_REPLACE) {
2230b09e0fa4SEric Paris 		xattr = new_xattr;
2231b09e0fa4SEric Paris 		err = -ENODATA;
2232b09e0fa4SEric Paris 	} else {
2233b09e0fa4SEric Paris 		list_add(&new_xattr->list, &info->xattr_list);
2234b09e0fa4SEric Paris 		xattr = NULL;
2235b09e0fa4SEric Paris 	}
2236b09e0fa4SEric Paris out:
2237b09e0fa4SEric Paris 	spin_unlock(&info->lock);
2238b09e0fa4SEric Paris 	if (xattr)
2239b09e0fa4SEric Paris 		kfree(xattr->name);
2240b09e0fa4SEric Paris 	kfree(xattr);
2241b09e0fa4SEric Paris 	return err;
2242b09e0fa4SEric Paris }
2243b09e0fa4SEric Paris 
2244b09e0fa4SEric Paris 
2245b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = {
2246b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
2247b09e0fa4SEric Paris 	&generic_acl_access_handler,
2248b09e0fa4SEric Paris 	&generic_acl_default_handler,
2249b09e0fa4SEric Paris #endif
2250b09e0fa4SEric Paris 	NULL
2251b09e0fa4SEric Paris };
2252b09e0fa4SEric Paris 
2253b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name)
2254b09e0fa4SEric Paris {
2255b09e0fa4SEric Paris 	struct { const char *prefix; size_t len; } arr[] = {
2256b09e0fa4SEric Paris 		{ XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
2257b09e0fa4SEric Paris 		{ XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
2258b09e0fa4SEric Paris 	};
2259b09e0fa4SEric Paris 	int i;
2260b09e0fa4SEric Paris 
2261b09e0fa4SEric Paris 	for (i = 0; i < ARRAY_SIZE(arr); i++) {
2262b09e0fa4SEric Paris 		size_t preflen = arr[i].len;
2263b09e0fa4SEric Paris 		if (strncmp(name, arr[i].prefix, preflen) == 0) {
2264b09e0fa4SEric Paris 			if (!name[preflen])
2265b09e0fa4SEric Paris 				return -EINVAL;
2266b09e0fa4SEric Paris 			return 0;
2267b09e0fa4SEric Paris 		}
2268b09e0fa4SEric Paris 	}
2269b09e0fa4SEric Paris 	return -EOPNOTSUPP;
2270b09e0fa4SEric Paris }
2271b09e0fa4SEric Paris 
2272b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
2273b09e0fa4SEric Paris 			      void *buffer, size_t size)
2274b09e0fa4SEric Paris {
2275b09e0fa4SEric Paris 	int err;
2276b09e0fa4SEric Paris 
2277b09e0fa4SEric Paris 	/*
2278b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2279b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2280b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2281b09e0fa4SEric Paris 	 */
2282b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2283b09e0fa4SEric Paris 		return generic_getxattr(dentry, name, buffer, size);
2284b09e0fa4SEric Paris 
2285b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2286b09e0fa4SEric Paris 	if (err)
2287b09e0fa4SEric Paris 		return err;
2288b09e0fa4SEric Paris 
2289b09e0fa4SEric Paris 	return shmem_xattr_get(dentry, name, buffer, size);
2290b09e0fa4SEric Paris }
2291b09e0fa4SEric Paris 
2292b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name,
2293b09e0fa4SEric Paris 			  const void *value, size_t size, int flags)
2294b09e0fa4SEric Paris {
2295b09e0fa4SEric Paris 	int err;
2296b09e0fa4SEric Paris 
2297b09e0fa4SEric Paris 	/*
2298b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2299b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2300b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2301b09e0fa4SEric Paris 	 */
2302b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2303b09e0fa4SEric Paris 		return generic_setxattr(dentry, name, value, size, flags);
2304b09e0fa4SEric Paris 
2305b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2306b09e0fa4SEric Paris 	if (err)
2307b09e0fa4SEric Paris 		return err;
2308b09e0fa4SEric Paris 
2309b09e0fa4SEric Paris 	if (size == 0)
2310b09e0fa4SEric Paris 		value = "";  /* empty EA, do not remove */
2311b09e0fa4SEric Paris 
2312b09e0fa4SEric Paris 	return shmem_xattr_set(dentry, name, value, size, flags);
2313b09e0fa4SEric Paris 
2314b09e0fa4SEric Paris }
2315b09e0fa4SEric Paris 
2316b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name)
2317b09e0fa4SEric Paris {
2318b09e0fa4SEric Paris 	int err;
2319b09e0fa4SEric Paris 
2320b09e0fa4SEric Paris 	/*
2321b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2322b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2323b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2324b09e0fa4SEric Paris 	 */
2325b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2326b09e0fa4SEric Paris 		return generic_removexattr(dentry, name);
2327b09e0fa4SEric Paris 
2328b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2329b09e0fa4SEric Paris 	if (err)
2330b09e0fa4SEric Paris 		return err;
2331b09e0fa4SEric Paris 
2332b09e0fa4SEric Paris 	return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
2333b09e0fa4SEric Paris }
2334b09e0fa4SEric Paris 
2335b09e0fa4SEric Paris static bool xattr_is_trusted(const char *name)
2336b09e0fa4SEric Paris {
2337b09e0fa4SEric Paris 	return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
2338b09e0fa4SEric Paris }
2339b09e0fa4SEric Paris 
2340b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2341b09e0fa4SEric Paris {
2342b09e0fa4SEric Paris 	bool trusted = capable(CAP_SYS_ADMIN);
2343b09e0fa4SEric Paris 	struct shmem_xattr *xattr;
2344b09e0fa4SEric Paris 	struct shmem_inode_info *info;
2345b09e0fa4SEric Paris 	size_t used = 0;
2346b09e0fa4SEric Paris 
2347b09e0fa4SEric Paris 	info = SHMEM_I(dentry->d_inode);
2348b09e0fa4SEric Paris 
2349b09e0fa4SEric Paris 	spin_lock(&info->lock);
2350b09e0fa4SEric Paris 	list_for_each_entry(xattr, &info->xattr_list, list) {
2351b09e0fa4SEric Paris 		size_t len;
2352b09e0fa4SEric Paris 
2353b09e0fa4SEric Paris 		/* skip "trusted." attributes for unprivileged callers */
2354b09e0fa4SEric Paris 		if (!trusted && xattr_is_trusted(xattr->name))
2355b09e0fa4SEric Paris 			continue;
2356b09e0fa4SEric Paris 
2357b09e0fa4SEric Paris 		len = strlen(xattr->name) + 1;
2358b09e0fa4SEric Paris 		used += len;
2359b09e0fa4SEric Paris 		if (buffer) {
2360b09e0fa4SEric Paris 			if (size < used) {
2361b09e0fa4SEric Paris 				used = -ERANGE;
2362b09e0fa4SEric Paris 				break;
2363b09e0fa4SEric Paris 			}
2364b09e0fa4SEric Paris 			memcpy(buffer, xattr->name, len);
2365b09e0fa4SEric Paris 			buffer += len;
2366b09e0fa4SEric Paris 		}
2367b09e0fa4SEric Paris 	}
2368b09e0fa4SEric Paris 	spin_unlock(&info->lock);
2369b09e0fa4SEric Paris 
2370b09e0fa4SEric Paris 	return used;
2371b09e0fa4SEric Paris }
2372b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */
2373b09e0fa4SEric Paris 
237492e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations = {
23751da177e4SLinus Torvalds 	.readlink	= generic_readlink,
23761da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link_inline,
2377b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2378b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2379b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2380b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2381b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2382b09e0fa4SEric Paris #endif
23831da177e4SLinus Torvalds };
23841da177e4SLinus Torvalds 
238592e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = {
23861da177e4SLinus Torvalds 	.readlink	= generic_readlink,
23871da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link,
23881da177e4SLinus Torvalds 	.put_link	= shmem_put_link,
2389b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2390b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2391b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2392b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2393b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
239439f0247dSAndreas Gruenbacher #endif
2395b09e0fa4SEric Paris };
239639f0247dSAndreas Gruenbacher 
239791828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child)
239891828a40SDavid M. Grimes {
239991828a40SDavid M. Grimes 	return ERR_PTR(-ESTALE);
240091828a40SDavid M. Grimes }
240191828a40SDavid M. Grimes 
240291828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh)
240391828a40SDavid M. Grimes {
240491828a40SDavid M. Grimes 	__u32 *fh = vfh;
240591828a40SDavid M. Grimes 	__u64 inum = fh[2];
240691828a40SDavid M. Grimes 	inum = (inum << 32) | fh[1];
240791828a40SDavid M. Grimes 	return ino->i_ino == inum && fh[0] == ino->i_generation;
240891828a40SDavid M. Grimes }
240991828a40SDavid M. Grimes 
2410480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2411480b116cSChristoph Hellwig 		struct fid *fid, int fh_len, int fh_type)
241291828a40SDavid M. Grimes {
241391828a40SDavid M. Grimes 	struct inode *inode;
2414480b116cSChristoph Hellwig 	struct dentry *dentry = NULL;
2415480b116cSChristoph Hellwig 	u64 inum = fid->raw[2];
2416480b116cSChristoph Hellwig 	inum = (inum << 32) | fid->raw[1];
241791828a40SDavid M. Grimes 
2418480b116cSChristoph Hellwig 	if (fh_len < 3)
2419480b116cSChristoph Hellwig 		return NULL;
2420480b116cSChristoph Hellwig 
2421480b116cSChristoph Hellwig 	inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2422480b116cSChristoph Hellwig 			shmem_match, fid->raw);
242391828a40SDavid M. Grimes 	if (inode) {
2424480b116cSChristoph Hellwig 		dentry = d_find_alias(inode);
242591828a40SDavid M. Grimes 		iput(inode);
242691828a40SDavid M. Grimes 	}
242791828a40SDavid M. Grimes 
2428480b116cSChristoph Hellwig 	return dentry;
242991828a40SDavid M. Grimes }
243091828a40SDavid M. Grimes 
243191828a40SDavid M. Grimes static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
243291828a40SDavid M. Grimes 				int connectable)
243391828a40SDavid M. Grimes {
243491828a40SDavid M. Grimes 	struct inode *inode = dentry->d_inode;
243591828a40SDavid M. Grimes 
24365fe0c237SAneesh Kumar K.V 	if (*len < 3) {
24375fe0c237SAneesh Kumar K.V 		*len = 3;
243891828a40SDavid M. Grimes 		return 255;
24395fe0c237SAneesh Kumar K.V 	}
244091828a40SDavid M. Grimes 
24411d3382cbSAl Viro 	if (inode_unhashed(inode)) {
244291828a40SDavid M. Grimes 		/* Unfortunately insert_inode_hash is not idempotent,
244391828a40SDavid M. Grimes 		 * so as we hash inodes here rather than at creation
244491828a40SDavid M. Grimes 		 * time, we need a lock to ensure we only try
244591828a40SDavid M. Grimes 		 * to do it once
244691828a40SDavid M. Grimes 		 */
244791828a40SDavid M. Grimes 		static DEFINE_SPINLOCK(lock);
244891828a40SDavid M. Grimes 		spin_lock(&lock);
24491d3382cbSAl Viro 		if (inode_unhashed(inode))
245091828a40SDavid M. Grimes 			__insert_inode_hash(inode,
245191828a40SDavid M. Grimes 					    inode->i_ino + inode->i_generation);
245291828a40SDavid M. Grimes 		spin_unlock(&lock);
245391828a40SDavid M. Grimes 	}
245491828a40SDavid M. Grimes 
245591828a40SDavid M. Grimes 	fh[0] = inode->i_generation;
245691828a40SDavid M. Grimes 	fh[1] = inode->i_ino;
245791828a40SDavid M. Grimes 	fh[2] = ((__u64)inode->i_ino) >> 32;
245891828a40SDavid M. Grimes 
245991828a40SDavid M. Grimes 	*len = 3;
246091828a40SDavid M. Grimes 	return 1;
246191828a40SDavid M. Grimes }
246291828a40SDavid M. Grimes 
246339655164SChristoph Hellwig static const struct export_operations shmem_export_ops = {
246491828a40SDavid M. Grimes 	.get_parent     = shmem_get_parent,
246591828a40SDavid M. Grimes 	.encode_fh      = shmem_encode_fh,
2466480b116cSChristoph Hellwig 	.fh_to_dentry	= shmem_fh_to_dentry,
246791828a40SDavid M. Grimes };
246891828a40SDavid M. Grimes 
2469680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2470680d794bSakpm@linux-foundation.org 			       bool remount)
24711da177e4SLinus Torvalds {
24721da177e4SLinus Torvalds 	char *this_char, *value, *rest;
24731da177e4SLinus Torvalds 
2474b00dc3adSHugh Dickins 	while (options != NULL) {
2475b00dc3adSHugh Dickins 		this_char = options;
2476b00dc3adSHugh Dickins 		for (;;) {
2477b00dc3adSHugh Dickins 			/*
2478b00dc3adSHugh Dickins 			 * NUL-terminate this option: unfortunately,
2479b00dc3adSHugh Dickins 			 * mount options form a comma-separated list,
2480b00dc3adSHugh Dickins 			 * but mpol's nodelist may also contain commas.
2481b00dc3adSHugh Dickins 			 */
2482b00dc3adSHugh Dickins 			options = strchr(options, ',');
2483b00dc3adSHugh Dickins 			if (options == NULL)
2484b00dc3adSHugh Dickins 				break;
2485b00dc3adSHugh Dickins 			options++;
2486b00dc3adSHugh Dickins 			if (!isdigit(*options)) {
2487b00dc3adSHugh Dickins 				options[-1] = '\0';
2488b00dc3adSHugh Dickins 				break;
2489b00dc3adSHugh Dickins 			}
2490b00dc3adSHugh Dickins 		}
24911da177e4SLinus Torvalds 		if (!*this_char)
24921da177e4SLinus Torvalds 			continue;
24931da177e4SLinus Torvalds 		if ((value = strchr(this_char,'=')) != NULL) {
24941da177e4SLinus Torvalds 			*value++ = 0;
24951da177e4SLinus Torvalds 		} else {
24961da177e4SLinus Torvalds 			printk(KERN_ERR
24971da177e4SLinus Torvalds 			    "tmpfs: No value for mount option '%s'\n",
24981da177e4SLinus Torvalds 			    this_char);
24991da177e4SLinus Torvalds 			return 1;
25001da177e4SLinus Torvalds 		}
25011da177e4SLinus Torvalds 
25021da177e4SLinus Torvalds 		if (!strcmp(this_char,"size")) {
25031da177e4SLinus Torvalds 			unsigned long long size;
25041da177e4SLinus Torvalds 			size = memparse(value,&rest);
25051da177e4SLinus Torvalds 			if (*rest == '%') {
25061da177e4SLinus Torvalds 				size <<= PAGE_SHIFT;
25071da177e4SLinus Torvalds 				size *= totalram_pages;
25081da177e4SLinus Torvalds 				do_div(size, 100);
25091da177e4SLinus Torvalds 				rest++;
25101da177e4SLinus Torvalds 			}
25111da177e4SLinus Torvalds 			if (*rest)
25121da177e4SLinus Torvalds 				goto bad_val;
2513680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks =
2514680d794bSakpm@linux-foundation.org 				DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
25151da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_blocks")) {
2516680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks = memparse(value, &rest);
25171da177e4SLinus Torvalds 			if (*rest)
25181da177e4SLinus Torvalds 				goto bad_val;
25191da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_inodes")) {
2520680d794bSakpm@linux-foundation.org 			sbinfo->max_inodes = memparse(value, &rest);
25211da177e4SLinus Torvalds 			if (*rest)
25221da177e4SLinus Torvalds 				goto bad_val;
25231da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"mode")) {
2524680d794bSakpm@linux-foundation.org 			if (remount)
25251da177e4SLinus Torvalds 				continue;
2526680d794bSakpm@linux-foundation.org 			sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
25271da177e4SLinus Torvalds 			if (*rest)
25281da177e4SLinus Torvalds 				goto bad_val;
25291da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"uid")) {
2530680d794bSakpm@linux-foundation.org 			if (remount)
25311da177e4SLinus Torvalds 				continue;
2532680d794bSakpm@linux-foundation.org 			sbinfo->uid = simple_strtoul(value, &rest, 0);
25331da177e4SLinus Torvalds 			if (*rest)
25341da177e4SLinus Torvalds 				goto bad_val;
25351da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"gid")) {
2536680d794bSakpm@linux-foundation.org 			if (remount)
25371da177e4SLinus Torvalds 				continue;
2538680d794bSakpm@linux-foundation.org 			sbinfo->gid = simple_strtoul(value, &rest, 0);
25391da177e4SLinus Torvalds 			if (*rest)
25401da177e4SLinus Torvalds 				goto bad_val;
25417339ff83SRobin Holt 		} else if (!strcmp(this_char,"mpol")) {
254271fe804bSLee Schermerhorn 			if (mpol_parse_str(value, &sbinfo->mpol, 1))
25437339ff83SRobin Holt 				goto bad_val;
25441da177e4SLinus Torvalds 		} else {
25451da177e4SLinus Torvalds 			printk(KERN_ERR "tmpfs: Bad mount option %s\n",
25461da177e4SLinus Torvalds 			       this_char);
25471da177e4SLinus Torvalds 			return 1;
25481da177e4SLinus Torvalds 		}
25491da177e4SLinus Torvalds 	}
25501da177e4SLinus Torvalds 	return 0;
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds bad_val:
25531da177e4SLinus Torvalds 	printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
25541da177e4SLinus Torvalds 	       value, this_char);
25551da177e4SLinus Torvalds 	return 1;
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds }
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
25601da177e4SLinus Torvalds {
25611da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2562680d794bSakpm@linux-foundation.org 	struct shmem_sb_info config = *sbinfo;
25630edd73b3SHugh Dickins 	unsigned long inodes;
25640edd73b3SHugh Dickins 	int error = -EINVAL;
25651da177e4SLinus Torvalds 
2566680d794bSakpm@linux-foundation.org 	if (shmem_parse_options(data, &config, true))
25670edd73b3SHugh Dickins 		return error;
25680edd73b3SHugh Dickins 
25690edd73b3SHugh Dickins 	spin_lock(&sbinfo->stat_lock);
25700edd73b3SHugh Dickins 	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
25717e496299STim Chen 	if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
25720edd73b3SHugh Dickins 		goto out;
2573680d794bSakpm@linux-foundation.org 	if (config.max_inodes < inodes)
25740edd73b3SHugh Dickins 		goto out;
25750edd73b3SHugh Dickins 	/*
25760edd73b3SHugh Dickins 	 * Those tests also disallow limited->unlimited while any are in
25770edd73b3SHugh Dickins 	 * use, so i_blocks will always be zero when max_blocks is zero;
25780edd73b3SHugh Dickins 	 * but we must separately disallow unlimited->limited, because
25790edd73b3SHugh Dickins 	 * in that case we have no record of how much is already in use.
25800edd73b3SHugh Dickins 	 */
2581680d794bSakpm@linux-foundation.org 	if (config.max_blocks && !sbinfo->max_blocks)
25820edd73b3SHugh Dickins 		goto out;
2583680d794bSakpm@linux-foundation.org 	if (config.max_inodes && !sbinfo->max_inodes)
25840edd73b3SHugh Dickins 		goto out;
25850edd73b3SHugh Dickins 
25860edd73b3SHugh Dickins 	error = 0;
2587680d794bSakpm@linux-foundation.org 	sbinfo->max_blocks  = config.max_blocks;
2588680d794bSakpm@linux-foundation.org 	sbinfo->max_inodes  = config.max_inodes;
2589680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = config.max_inodes - inodes;
259071fe804bSLee Schermerhorn 
259171fe804bSLee Schermerhorn 	mpol_put(sbinfo->mpol);
259271fe804bSLee Schermerhorn 	sbinfo->mpol        = config.mpol;	/* transfers initial ref */
25930edd73b3SHugh Dickins out:
25940edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
25950edd73b3SHugh Dickins 	return error;
25961da177e4SLinus Torvalds }
2597680d794bSakpm@linux-foundation.org 
2598680d794bSakpm@linux-foundation.org static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2599680d794bSakpm@linux-foundation.org {
2600680d794bSakpm@linux-foundation.org 	struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2601680d794bSakpm@linux-foundation.org 
2602680d794bSakpm@linux-foundation.org 	if (sbinfo->max_blocks != shmem_default_max_blocks())
2603680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",size=%luk",
2604680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2605680d794bSakpm@linux-foundation.org 	if (sbinfo->max_inodes != shmem_default_max_inodes())
2606680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2607680d794bSakpm@linux-foundation.org 	if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2608680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",mode=%03o", sbinfo->mode);
2609680d794bSakpm@linux-foundation.org 	if (sbinfo->uid != 0)
2610680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",uid=%u", sbinfo->uid);
2611680d794bSakpm@linux-foundation.org 	if (sbinfo->gid != 0)
2612680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",gid=%u", sbinfo->gid);
261371fe804bSLee Schermerhorn 	shmem_show_mpol(seq, sbinfo->mpol);
2614680d794bSakpm@linux-foundation.org 	return 0;
2615680d794bSakpm@linux-foundation.org }
2616680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
26171da177e4SLinus Torvalds 
26181da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb)
26191da177e4SLinus Torvalds {
2620602586a8SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2621602586a8SHugh Dickins 
2622602586a8SHugh Dickins 	percpu_counter_destroy(&sbinfo->used_blocks);
2623602586a8SHugh Dickins 	kfree(sbinfo);
26241da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
26251da177e4SLinus Torvalds }
26261da177e4SLinus Torvalds 
26272b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent)
26281da177e4SLinus Torvalds {
26291da177e4SLinus Torvalds 	struct inode *inode;
26301da177e4SLinus Torvalds 	struct dentry *root;
26310edd73b3SHugh Dickins 	struct shmem_sb_info *sbinfo;
2632680d794bSakpm@linux-foundation.org 	int err = -ENOMEM;
2633680d794bSakpm@linux-foundation.org 
2634680d794bSakpm@linux-foundation.org 	/* Round up to L1_CACHE_BYTES to resist false sharing */
2635425fbf04SPekka Enberg 	sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
2636680d794bSakpm@linux-foundation.org 				L1_CACHE_BYTES), GFP_KERNEL);
2637680d794bSakpm@linux-foundation.org 	if (!sbinfo)
2638680d794bSakpm@linux-foundation.org 		return -ENOMEM;
2639680d794bSakpm@linux-foundation.org 
2640680d794bSakpm@linux-foundation.org 	sbinfo->mode = S_IRWXUGO | S_ISVTX;
264176aac0e9SDavid Howells 	sbinfo->uid = current_fsuid();
264276aac0e9SDavid Howells 	sbinfo->gid = current_fsgid();
2643680d794bSakpm@linux-foundation.org 	sb->s_fs_info = sbinfo;
26441da177e4SLinus Torvalds 
26450edd73b3SHugh Dickins #ifdef CONFIG_TMPFS
26461da177e4SLinus Torvalds 	/*
26471da177e4SLinus Torvalds 	 * Per default we only allow half of the physical ram per
26481da177e4SLinus Torvalds 	 * tmpfs instance, limiting inodes to one per page of lowmem;
26491da177e4SLinus Torvalds 	 * but the internal instance is left unlimited.
26501da177e4SLinus Torvalds 	 */
26511da177e4SLinus Torvalds 	if (!(sb->s_flags & MS_NOUSER)) {
2652680d794bSakpm@linux-foundation.org 		sbinfo->max_blocks = shmem_default_max_blocks();
2653680d794bSakpm@linux-foundation.org 		sbinfo->max_inodes = shmem_default_max_inodes();
2654680d794bSakpm@linux-foundation.org 		if (shmem_parse_options(data, sbinfo, false)) {
2655680d794bSakpm@linux-foundation.org 			err = -EINVAL;
2656680d794bSakpm@linux-foundation.org 			goto failed;
2657680d794bSakpm@linux-foundation.org 		}
26581da177e4SLinus Torvalds 	}
265991828a40SDavid M. Grimes 	sb->s_export_op = &shmem_export_ops;
26600edd73b3SHugh Dickins #else
26610edd73b3SHugh Dickins 	sb->s_flags |= MS_NOUSER;
26620edd73b3SHugh Dickins #endif
26631da177e4SLinus Torvalds 
26641da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
2665602586a8SHugh Dickins 	if (percpu_counter_init(&sbinfo->used_blocks, 0))
2666602586a8SHugh Dickins 		goto failed;
2667680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = sbinfo->max_inodes;
26681da177e4SLinus Torvalds 
26691da177e4SLinus Torvalds 	sb->s_maxbytes = SHMEM_MAX_BYTES;
26701da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
26711da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
26721da177e4SLinus Torvalds 	sb->s_magic = TMPFS_MAGIC;
26731da177e4SLinus Torvalds 	sb->s_op = &shmem_ops;
2674cfd95a9cSRobin H. Johnson 	sb->s_time_gran = 1;
2675b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
267639f0247dSAndreas Gruenbacher 	sb->s_xattr = shmem_xattr_handlers;
2677b09e0fa4SEric Paris #endif
2678b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
267939f0247dSAndreas Gruenbacher 	sb->s_flags |= MS_POSIXACL;
268039f0247dSAndreas Gruenbacher #endif
26810edd73b3SHugh Dickins 
2682454abafeSDmitry Monakhov 	inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
26831da177e4SLinus Torvalds 	if (!inode)
26841da177e4SLinus Torvalds 		goto failed;
2685680d794bSakpm@linux-foundation.org 	inode->i_uid = sbinfo->uid;
2686680d794bSakpm@linux-foundation.org 	inode->i_gid = sbinfo->gid;
26871da177e4SLinus Torvalds 	root = d_alloc_root(inode);
26881da177e4SLinus Torvalds 	if (!root)
26891da177e4SLinus Torvalds 		goto failed_iput;
26901da177e4SLinus Torvalds 	sb->s_root = root;
26911da177e4SLinus Torvalds 	return 0;
26921da177e4SLinus Torvalds 
26931da177e4SLinus Torvalds failed_iput:
26941da177e4SLinus Torvalds 	iput(inode);
26951da177e4SLinus Torvalds failed:
26961da177e4SLinus Torvalds 	shmem_put_super(sb);
26971da177e4SLinus Torvalds 	return err;
26981da177e4SLinus Torvalds }
26991da177e4SLinus Torvalds 
2700fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep;
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb)
27031da177e4SLinus Torvalds {
27041da177e4SLinus Torvalds 	struct shmem_inode_info *p;
2705e94b1766SChristoph Lameter 	p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
27061da177e4SLinus Torvalds 	if (!p)
27071da177e4SLinus Torvalds 		return NULL;
27081da177e4SLinus Torvalds 	return &p->vfs_inode;
27091da177e4SLinus Torvalds }
27101da177e4SLinus Torvalds 
2711fa0d7e3dSNick Piggin static void shmem_i_callback(struct rcu_head *head)
2712fa0d7e3dSNick Piggin {
2713fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
2714fa0d7e3dSNick Piggin 	INIT_LIST_HEAD(&inode->i_dentry);
2715fa0d7e3dSNick Piggin 	kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2716fa0d7e3dSNick Piggin }
2717fa0d7e3dSNick Piggin 
27181da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode)
27191da177e4SLinus Torvalds {
27201da177e4SLinus Torvalds 	if ((inode->i_mode & S_IFMT) == S_IFREG) {
27211da177e4SLinus Torvalds 		/* only struct inode is valid if it's an inline symlink */
27221da177e4SLinus Torvalds 		mpol_free_shared_policy(&SHMEM_I(inode)->policy);
27231da177e4SLinus Torvalds 	}
2724fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, shmem_i_callback);
27251da177e4SLinus Torvalds }
27261da177e4SLinus Torvalds 
272751cc5068SAlexey Dobriyan static void init_once(void *foo)
27281da177e4SLinus Torvalds {
27291da177e4SLinus Torvalds 	struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds 	inode_init_once(&p->vfs_inode);
27321da177e4SLinus Torvalds }
27331da177e4SLinus Torvalds 
27341da177e4SLinus Torvalds static int init_inodecache(void)
27351da177e4SLinus Torvalds {
27361da177e4SLinus Torvalds 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
27371da177e4SLinus Torvalds 				sizeof(struct shmem_inode_info),
2738040b5c6fSAlexey Dobriyan 				0, SLAB_PANIC, init_once);
27391da177e4SLinus Torvalds 	return 0;
27401da177e4SLinus Torvalds }
27411da177e4SLinus Torvalds 
27421da177e4SLinus Torvalds static void destroy_inodecache(void)
27431da177e4SLinus Torvalds {
27441a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(shmem_inode_cachep);
27451da177e4SLinus Torvalds }
27461da177e4SLinus Torvalds 
2747f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = {
27481da177e4SLinus Torvalds 	.writepage	= shmem_writepage,
274976719325SKen Chen 	.set_page_dirty	= __set_page_dirty_no_writeback,
27501da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
2751800d15a5SNick Piggin 	.write_begin	= shmem_write_begin,
2752800d15a5SNick Piggin 	.write_end	= shmem_write_end,
27531da177e4SLinus Torvalds #endif
2754304dbdb7SLee Schermerhorn 	.migratepage	= migrate_page,
2755aa261f54SAndi Kleen 	.error_remove_page = generic_error_remove_page,
27561da177e4SLinus Torvalds };
27571da177e4SLinus Torvalds 
275815ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = {
27591da177e4SLinus Torvalds 	.mmap		= shmem_mmap,
27601da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
27611da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
2762bcd78e49SHugh Dickins 	.read		= do_sync_read,
27635402b976SHugh Dickins 	.write		= do_sync_write,
2764bcd78e49SHugh Dickins 	.aio_read	= shmem_file_aio_read,
27655402b976SHugh Dickins 	.aio_write	= generic_file_aio_write,
27661b061d92SChristoph Hellwig 	.fsync		= noop_fsync,
2767708e3508SHugh Dickins 	.splice_read	= shmem_file_splice_read,
2768ae976416SHugh Dickins 	.splice_write	= generic_file_splice_write,
27691da177e4SLinus Torvalds #endif
27701da177e4SLinus Torvalds };
27711da177e4SLinus Torvalds 
277292e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = {
277394c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
2774f6b3ec23SBadari Pulavarty 	.truncate_range	= shmem_truncate_range,
2775b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2776b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2777b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2778b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2779b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2780b09e0fa4SEric Paris #endif
278139f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
27821c7c474cSChristoph Hellwig 	.check_acl	= generic_check_acl,
278339f0247dSAndreas Gruenbacher #endif
278439f0247dSAndreas Gruenbacher 
27851da177e4SLinus Torvalds };
27861da177e4SLinus Torvalds 
278792e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = {
27881da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
27891da177e4SLinus Torvalds 	.create		= shmem_create,
27901da177e4SLinus Torvalds 	.lookup		= simple_lookup,
27911da177e4SLinus Torvalds 	.link		= shmem_link,
27921da177e4SLinus Torvalds 	.unlink		= shmem_unlink,
27931da177e4SLinus Torvalds 	.symlink	= shmem_symlink,
27941da177e4SLinus Torvalds 	.mkdir		= shmem_mkdir,
27951da177e4SLinus Torvalds 	.rmdir		= shmem_rmdir,
27961da177e4SLinus Torvalds 	.mknod		= shmem_mknod,
27971da177e4SLinus Torvalds 	.rename		= shmem_rename,
27981da177e4SLinus Torvalds #endif
2799b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2800b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2801b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2802b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2803b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2804b09e0fa4SEric Paris #endif
280539f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
280694c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
28071c7c474cSChristoph Hellwig 	.check_acl	= generic_check_acl,
280839f0247dSAndreas Gruenbacher #endif
280939f0247dSAndreas Gruenbacher };
281039f0247dSAndreas Gruenbacher 
281192e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = {
2812b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2813b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2814b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2815b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2816b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2817b09e0fa4SEric Paris #endif
281839f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
281994c1e62dSHugh Dickins 	.setattr	= shmem_setattr,
28201c7c474cSChristoph Hellwig 	.check_acl	= generic_check_acl,
282139f0247dSAndreas Gruenbacher #endif
28221da177e4SLinus Torvalds };
28231da177e4SLinus Torvalds 
2824759b9775SHugh Dickins static const struct super_operations shmem_ops = {
28251da177e4SLinus Torvalds 	.alloc_inode	= shmem_alloc_inode,
28261da177e4SLinus Torvalds 	.destroy_inode	= shmem_destroy_inode,
28271da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
28281da177e4SLinus Torvalds 	.statfs		= shmem_statfs,
28291da177e4SLinus Torvalds 	.remount_fs	= shmem_remount_fs,
2830680d794bSakpm@linux-foundation.org 	.show_options	= shmem_show_options,
28311da177e4SLinus Torvalds #endif
28321f895f75SAl Viro 	.evict_inode	= shmem_evict_inode,
28331da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
28341da177e4SLinus Torvalds 	.put_super	= shmem_put_super,
28351da177e4SLinus Torvalds };
28361da177e4SLinus Torvalds 
2837f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = {
283854cb8821SNick Piggin 	.fault		= shmem_fault,
28391da177e4SLinus Torvalds #ifdef CONFIG_NUMA
28401da177e4SLinus Torvalds 	.set_policy     = shmem_set_policy,
28411da177e4SLinus Torvalds 	.get_policy     = shmem_get_policy,
28421da177e4SLinus Torvalds #endif
28431da177e4SLinus Torvalds };
28441da177e4SLinus Torvalds 
28451da177e4SLinus Torvalds 
28463c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type,
28473c26ff6eSAl Viro 	int flags, const char *dev_name, void *data)
28481da177e4SLinus Torvalds {
28493c26ff6eSAl Viro 	return mount_nodev(fs_type, flags, data, shmem_fill_super);
28501da177e4SLinus Torvalds }
28511da177e4SLinus Torvalds 
28521da177e4SLinus Torvalds static struct file_system_type tmpfs_fs_type = {
28531da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
28541da177e4SLinus Torvalds 	.name		= "tmpfs",
28553c26ff6eSAl Viro 	.mount		= shmem_mount,
28561da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
28571da177e4SLinus Torvalds };
28581da177e4SLinus Torvalds 
28592b2af54aSKay Sievers int __init init_tmpfs(void)
28601da177e4SLinus Torvalds {
28611da177e4SLinus Torvalds 	int error;
28621da177e4SLinus Torvalds 
2863e0bf68ddSPeter Zijlstra 	error = bdi_init(&shmem_backing_dev_info);
2864e0bf68ddSPeter Zijlstra 	if (error)
2865e0bf68ddSPeter Zijlstra 		goto out4;
2866e0bf68ddSPeter Zijlstra 
28671da177e4SLinus Torvalds 	error = init_inodecache();
28681da177e4SLinus Torvalds 	if (error)
28691da177e4SLinus Torvalds 		goto out3;
28701da177e4SLinus Torvalds 
28711da177e4SLinus Torvalds 	error = register_filesystem(&tmpfs_fs_type);
28721da177e4SLinus Torvalds 	if (error) {
28731da177e4SLinus Torvalds 		printk(KERN_ERR "Could not register tmpfs\n");
28741da177e4SLinus Torvalds 		goto out2;
28751da177e4SLinus Torvalds 	}
287695dc112aSGreg Kroah-Hartman 
28771f5ce9e9STrond Myklebust 	shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
28781da177e4SLinus Torvalds 				tmpfs_fs_type.name, NULL);
28791da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt)) {
28801da177e4SLinus Torvalds 		error = PTR_ERR(shm_mnt);
28811da177e4SLinus Torvalds 		printk(KERN_ERR "Could not kern_mount tmpfs\n");
28821da177e4SLinus Torvalds 		goto out1;
28831da177e4SLinus Torvalds 	}
28841da177e4SLinus Torvalds 	return 0;
28851da177e4SLinus Torvalds 
28861da177e4SLinus Torvalds out1:
28871da177e4SLinus Torvalds 	unregister_filesystem(&tmpfs_fs_type);
28881da177e4SLinus Torvalds out2:
28891da177e4SLinus Torvalds 	destroy_inodecache();
28901da177e4SLinus Torvalds out3:
2891e0bf68ddSPeter Zijlstra 	bdi_destroy(&shmem_backing_dev_info);
2892e0bf68ddSPeter Zijlstra out4:
28931da177e4SLinus Torvalds 	shm_mnt = ERR_PTR(error);
28941da177e4SLinus Torvalds 	return error;
28951da177e4SLinus Torvalds }
2896853ac43aSMatt Mackall 
289787946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR
289887946a72SDaisuke Nishimura /**
289987946a72SDaisuke Nishimura  * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
290087946a72SDaisuke Nishimura  * @inode: the inode to be searched
290187946a72SDaisuke Nishimura  * @pgoff: the offset to be searched
290287946a72SDaisuke Nishimura  * @pagep: the pointer for the found page to be stored
290387946a72SDaisuke Nishimura  * @ent: the pointer for the found swap entry to be stored
290487946a72SDaisuke Nishimura  *
290587946a72SDaisuke Nishimura  * If a page is found, refcount of it is incremented. Callers should handle
290687946a72SDaisuke Nishimura  * these refcount.
290787946a72SDaisuke Nishimura  */
290887946a72SDaisuke Nishimura void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
290987946a72SDaisuke Nishimura 					struct page **pagep, swp_entry_t *ent)
291087946a72SDaisuke Nishimura {
291187946a72SDaisuke Nishimura 	swp_entry_t entry = { .val = 0 }, *ptr;
291287946a72SDaisuke Nishimura 	struct page *page = NULL;
291387946a72SDaisuke Nishimura 	struct shmem_inode_info *info = SHMEM_I(inode);
291487946a72SDaisuke Nishimura 
291587946a72SDaisuke Nishimura 	if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
291687946a72SDaisuke Nishimura 		goto out;
291787946a72SDaisuke Nishimura 
291887946a72SDaisuke Nishimura 	spin_lock(&info->lock);
291987946a72SDaisuke Nishimura 	ptr = shmem_swp_entry(info, pgoff, NULL);
292087946a72SDaisuke Nishimura #ifdef CONFIG_SWAP
292187946a72SDaisuke Nishimura 	if (ptr && ptr->val) {
292287946a72SDaisuke Nishimura 		entry.val = ptr->val;
292387946a72SDaisuke Nishimura 		page = find_get_page(&swapper_space, entry.val);
292487946a72SDaisuke Nishimura 	} else
292587946a72SDaisuke Nishimura #endif
292687946a72SDaisuke Nishimura 		page = find_get_page(inode->i_mapping, pgoff);
292787946a72SDaisuke Nishimura 	if (ptr)
292887946a72SDaisuke Nishimura 		shmem_swp_unmap(ptr);
292987946a72SDaisuke Nishimura 	spin_unlock(&info->lock);
293087946a72SDaisuke Nishimura out:
293187946a72SDaisuke Nishimura 	*pagep = page;
293287946a72SDaisuke Nishimura 	*ent = entry;
293387946a72SDaisuke Nishimura }
293487946a72SDaisuke Nishimura #endif
293587946a72SDaisuke Nishimura 
2936853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */
2937853ac43aSMatt Mackall 
2938853ac43aSMatt Mackall /*
2939853ac43aSMatt Mackall  * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2940853ac43aSMatt Mackall  *
2941853ac43aSMatt Mackall  * This is intended for small system where the benefits of the full
2942853ac43aSMatt Mackall  * shmem code (swap-backed and resource-limited) are outweighed by
2943853ac43aSMatt Mackall  * their complexity. On systems without swap this code should be
2944853ac43aSMatt Mackall  * effectively equivalent, but much lighter weight.
2945853ac43aSMatt Mackall  */
2946853ac43aSMatt Mackall 
2947853ac43aSMatt Mackall #include <linux/ramfs.h>
2948853ac43aSMatt Mackall 
2949853ac43aSMatt Mackall static struct file_system_type tmpfs_fs_type = {
2950853ac43aSMatt Mackall 	.name		= "tmpfs",
29513c26ff6eSAl Viro 	.mount		= ramfs_mount,
2952853ac43aSMatt Mackall 	.kill_sb	= kill_litter_super,
2953853ac43aSMatt Mackall };
2954853ac43aSMatt Mackall 
29552b2af54aSKay Sievers int __init init_tmpfs(void)
2956853ac43aSMatt Mackall {
2957853ac43aSMatt Mackall 	BUG_ON(register_filesystem(&tmpfs_fs_type) != 0);
2958853ac43aSMatt Mackall 
2959853ac43aSMatt Mackall 	shm_mnt = kern_mount(&tmpfs_fs_type);
2960853ac43aSMatt Mackall 	BUG_ON(IS_ERR(shm_mnt));
2961853ac43aSMatt Mackall 
2962853ac43aSMatt Mackall 	return 0;
2963853ac43aSMatt Mackall }
2964853ac43aSMatt Mackall 
2965853ac43aSMatt Mackall int shmem_unuse(swp_entry_t entry, struct page *page)
2966853ac43aSMatt Mackall {
2967853ac43aSMatt Mackall 	return 0;
2968853ac43aSMatt Mackall }
2969853ac43aSMatt Mackall 
29703f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user)
29713f96b79aSHugh Dickins {
29723f96b79aSHugh Dickins 	return 0;
29733f96b79aSHugh Dickins }
29743f96b79aSHugh Dickins 
297594c1e62dSHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
297694c1e62dSHugh Dickins {
297794c1e62dSHugh Dickins 	truncate_inode_pages_range(inode->i_mapping, start, end);
297894c1e62dSHugh Dickins }
297994c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range);
298094c1e62dSHugh Dickins 
298187946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR
298287946a72SDaisuke Nishimura /**
298387946a72SDaisuke Nishimura  * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
298487946a72SDaisuke Nishimura  * @inode: the inode to be searched
298587946a72SDaisuke Nishimura  * @pgoff: the offset to be searched
298687946a72SDaisuke Nishimura  * @pagep: the pointer for the found page to be stored
298787946a72SDaisuke Nishimura  * @ent: the pointer for the found swap entry to be stored
298887946a72SDaisuke Nishimura  *
298987946a72SDaisuke Nishimura  * If a page is found, refcount of it is incremented. Callers should handle
299087946a72SDaisuke Nishimura  * these refcount.
299187946a72SDaisuke Nishimura  */
299287946a72SDaisuke Nishimura void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
299387946a72SDaisuke Nishimura 					struct page **pagep, swp_entry_t *ent)
299487946a72SDaisuke Nishimura {
299587946a72SDaisuke Nishimura 	struct page *page = NULL;
299687946a72SDaisuke Nishimura 
299787946a72SDaisuke Nishimura 	if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
299887946a72SDaisuke Nishimura 		goto out;
299987946a72SDaisuke Nishimura 	page = find_get_page(inode->i_mapping, pgoff);
300087946a72SDaisuke Nishimura out:
300187946a72SDaisuke Nishimura 	*pagep = page;
300287946a72SDaisuke Nishimura 	*ent = (swp_entry_t){ .val = 0 };
300387946a72SDaisuke Nishimura }
300487946a72SDaisuke Nishimura #endif
300587946a72SDaisuke Nishimura 
3006853ac43aSMatt Mackall #define shmem_vm_ops				generic_file_vm_ops
30070b0a0806SHugh Dickins #define shmem_file_operations			ramfs_file_operations
3008454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags)	ramfs_get_inode(sb, dir, mode, dev)
30090b0a0806SHugh Dickins #define shmem_acct_size(flags, size)		0
30100b0a0806SHugh Dickins #define shmem_unacct_size(flags, size)		do {} while (0)
3011caefba17SHugh Dickins #define SHMEM_MAX_BYTES				MAX_LFS_FILESIZE
3012853ac43aSMatt Mackall 
3013853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */
3014853ac43aSMatt Mackall 
3015853ac43aSMatt Mackall /* common code */
30161da177e4SLinus Torvalds 
301746711810SRandy Dunlap /**
30181da177e4SLinus Torvalds  * shmem_file_setup - get an unlinked file living in tmpfs
30191da177e4SLinus Torvalds  * @name: name for dentry (to be seen in /proc/<pid>/maps
30201da177e4SLinus Torvalds  * @size: size to be set for the file
30210b0a0806SHugh Dickins  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
30221da177e4SLinus Torvalds  */
3023168f5ac6SSergei Trofimovich struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
30241da177e4SLinus Torvalds {
30251da177e4SLinus Torvalds 	int error;
30261da177e4SLinus Torvalds 	struct file *file;
30271da177e4SLinus Torvalds 	struct inode *inode;
30282c48b9c4SAl Viro 	struct path path;
30292c48b9c4SAl Viro 	struct dentry *root;
30301da177e4SLinus Torvalds 	struct qstr this;
30311da177e4SLinus Torvalds 
30321da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt))
30331da177e4SLinus Torvalds 		return (void *)shm_mnt;
30341da177e4SLinus Torvalds 
30351da177e4SLinus Torvalds 	if (size < 0 || size > SHMEM_MAX_BYTES)
30361da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
30371da177e4SLinus Torvalds 
30381da177e4SLinus Torvalds 	if (shmem_acct_size(flags, size))
30391da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
30401da177e4SLinus Torvalds 
30411da177e4SLinus Torvalds 	error = -ENOMEM;
30421da177e4SLinus Torvalds 	this.name = name;
30431da177e4SLinus Torvalds 	this.len = strlen(name);
30441da177e4SLinus Torvalds 	this.hash = 0; /* will go */
30451da177e4SLinus Torvalds 	root = shm_mnt->mnt_root;
30462c48b9c4SAl Viro 	path.dentry = d_alloc(root, &this);
30472c48b9c4SAl Viro 	if (!path.dentry)
30481da177e4SLinus Torvalds 		goto put_memory;
30492c48b9c4SAl Viro 	path.mnt = mntget(shm_mnt);
30501da177e4SLinus Torvalds 
30511da177e4SLinus Torvalds 	error = -ENOSPC;
3052454abafeSDmitry Monakhov 	inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
30531da177e4SLinus Torvalds 	if (!inode)
30544b42af81SAl Viro 		goto put_dentry;
30551da177e4SLinus Torvalds 
30562c48b9c4SAl Viro 	d_instantiate(path.dentry, inode);
30571da177e4SLinus Torvalds 	inode->i_size = size;
30581da177e4SLinus Torvalds 	inode->i_nlink = 0;	/* It is unlinked */
3059853ac43aSMatt Mackall #ifndef CONFIG_MMU
3060853ac43aSMatt Mackall 	error = ramfs_nommu_expand_for_mapping(inode, size);
3061853ac43aSMatt Mackall 	if (error)
30624b42af81SAl Viro 		goto put_dentry;
3063853ac43aSMatt Mackall #endif
30644b42af81SAl Viro 
30654b42af81SAl Viro 	error = -ENFILE;
30662c48b9c4SAl Viro 	file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
30674b42af81SAl Viro 		  &shmem_file_operations);
30684b42af81SAl Viro 	if (!file)
30694b42af81SAl Viro 		goto put_dentry;
30704b42af81SAl Viro 
30711da177e4SLinus Torvalds 	return file;
30721da177e4SLinus Torvalds 
30731da177e4SLinus Torvalds put_dentry:
30742c48b9c4SAl Viro 	path_put(&path);
30751da177e4SLinus Torvalds put_memory:
30761da177e4SLinus Torvalds 	shmem_unacct_size(flags, size);
30771da177e4SLinus Torvalds 	return ERR_PTR(error);
30781da177e4SLinus Torvalds }
3079395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup);
30801da177e4SLinus Torvalds 
308146711810SRandy Dunlap /**
30821da177e4SLinus Torvalds  * shmem_zero_setup - setup a shared anonymous mapping
30831da177e4SLinus Torvalds  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
30841da177e4SLinus Torvalds  */
30851da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma)
30861da177e4SLinus Torvalds {
30871da177e4SLinus Torvalds 	struct file *file;
30881da177e4SLinus Torvalds 	loff_t size = vma->vm_end - vma->vm_start;
30891da177e4SLinus Torvalds 
30901da177e4SLinus Torvalds 	file = shmem_file_setup("dev/zero", size, vma->vm_flags);
30911da177e4SLinus Torvalds 	if (IS_ERR(file))
30921da177e4SLinus Torvalds 		return PTR_ERR(file);
30931da177e4SLinus Torvalds 
30941da177e4SLinus Torvalds 	if (vma->vm_file)
30951da177e4SLinus Torvalds 		fput(vma->vm_file);
30961da177e4SLinus Torvalds 	vma->vm_file = file;
30971da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
3098bee4c36aSHugh Dickins 	vma->vm_flags |= VM_CAN_NONLINEAR;
30991da177e4SLinus Torvalds 	return 0;
31001da177e4SLinus Torvalds }
3101d9d90e5eSHugh Dickins 
3102d9d90e5eSHugh Dickins /**
3103d9d90e5eSHugh Dickins  * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3104d9d90e5eSHugh Dickins  * @mapping:	the page's address_space
3105d9d90e5eSHugh Dickins  * @index:	the page index
3106d9d90e5eSHugh Dickins  * @gfp:	the page allocator flags to use if allocating
3107d9d90e5eSHugh Dickins  *
3108d9d90e5eSHugh Dickins  * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3109d9d90e5eSHugh Dickins  * with any new page allocations done using the specified allocation flags.
3110d9d90e5eSHugh Dickins  * But read_cache_page_gfp() uses the ->readpage() method: which does not
3111d9d90e5eSHugh Dickins  * suit tmpfs, since it may have pages in swapcache, and needs to find those
3112d9d90e5eSHugh Dickins  * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
3113d9d90e5eSHugh Dickins  *
311468da9f05SHugh Dickins  * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
311568da9f05SHugh Dickins  * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
3116d9d90e5eSHugh Dickins  */
3117d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
3118d9d90e5eSHugh Dickins 					 pgoff_t index, gfp_t gfp)
3119d9d90e5eSHugh Dickins {
312068da9f05SHugh Dickins #ifdef CONFIG_SHMEM
312168da9f05SHugh Dickins 	struct inode *inode = mapping->host;
31229276aad6SHugh Dickins 	struct page *page;
312368da9f05SHugh Dickins 	int error;
312468da9f05SHugh Dickins 
312568da9f05SHugh Dickins 	BUG_ON(mapping->a_ops != &shmem_aops);
312668da9f05SHugh Dickins 	error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
312768da9f05SHugh Dickins 	if (error)
312868da9f05SHugh Dickins 		page = ERR_PTR(error);
312968da9f05SHugh Dickins 	else
313068da9f05SHugh Dickins 		unlock_page(page);
313168da9f05SHugh Dickins 	return page;
313268da9f05SHugh Dickins #else
313368da9f05SHugh Dickins 	/*
313468da9f05SHugh Dickins 	 * The tiny !SHMEM case uses ramfs without swap
313568da9f05SHugh Dickins 	 */
3136d9d90e5eSHugh Dickins 	return read_cache_page_gfp(mapping, index, gfp);
313768da9f05SHugh Dickins #endif
3138d9d90e5eSHugh Dickins }
3139d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);
3140