xref: /openbmc/linux/mm/shmem.c (revision 71fe804b6d56d6a7aed680e096901434cef6a2c3)
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  *
171da177e4SLinus Torvalds  * This file is released under the GPL.
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds /*
211da177e4SLinus Torvalds  * This virtual memory filesystem is heavily based on the ramfs. It
221da177e4SLinus Torvalds  * extends ramfs by the ability to use swap and honor resource limits
231da177e4SLinus Torvalds  * which makes it a completely usable filesystem.
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <linux/module.h>
271da177e4SLinus Torvalds #include <linux/init.h>
281da177e4SLinus Torvalds #include <linux/fs.h>
2939f0247dSAndreas Gruenbacher #include <linux/xattr.h>
30a5694255SChristoph Hellwig #include <linux/exportfs.h>
3139f0247dSAndreas Gruenbacher #include <linux/generic_acl.h>
321da177e4SLinus Torvalds #include <linux/mm.h>
331da177e4SLinus Torvalds #include <linux/mman.h>
341da177e4SLinus Torvalds #include <linux/file.h>
351da177e4SLinus Torvalds #include <linux/swap.h>
361da177e4SLinus Torvalds #include <linux/pagemap.h>
371da177e4SLinus Torvalds #include <linux/string.h>
381da177e4SLinus Torvalds #include <linux/slab.h>
391da177e4SLinus Torvalds #include <linux/backing-dev.h>
401da177e4SLinus Torvalds #include <linux/shmem_fs.h>
411da177e4SLinus Torvalds #include <linux/mount.h>
421da177e4SLinus Torvalds #include <linux/writeback.h>
431da177e4SLinus Torvalds #include <linux/vfs.h>
441da177e4SLinus Torvalds #include <linux/blkdev.h>
451da177e4SLinus Torvalds #include <linux/security.h>
461da177e4SLinus Torvalds #include <linux/swapops.h>
471da177e4SLinus Torvalds #include <linux/mempolicy.h>
481da177e4SLinus Torvalds #include <linux/namei.h>
49b00dc3adSHugh Dickins #include <linux/ctype.h>
50304dbdb7SLee Schermerhorn #include <linux/migrate.h>
51c1f60a5aSChristoph Lameter #include <linux/highmem.h>
52680d794bSakpm@linux-foundation.org #include <linux/seq_file.h>
53304dbdb7SLee Schermerhorn 
541da177e4SLinus Torvalds #include <asm/uaccess.h>
551da177e4SLinus Torvalds #include <asm/div64.h>
561da177e4SLinus Torvalds #include <asm/pgtable.h>
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds /* This magic number is used in glibc for posix shared memory */
591da177e4SLinus Torvalds #define TMPFS_MAGIC	0x01021994
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
621da177e4SLinus Torvalds #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
631da177e4SLinus Torvalds #define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #define SHMEM_MAX_INDEX  (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
661da177e4SLinus Torvalds #define SHMEM_MAX_BYTES  ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
711da177e4SLinus Torvalds #define SHMEM_PAGEIN	 VM_READ
721da177e4SLinus Torvalds #define SHMEM_TRUNCATE	 VM_WRITE
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* Definition to limit shmem_truncate's steps between cond_rescheds */
751da177e4SLinus Torvalds #define LATENCY_LIMIT	 64
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */
781da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
811da177e4SLinus Torvalds enum sgp_type {
821da177e4SLinus Torvalds 	SGP_READ,	/* don't exceed i_size, don't allocate page */
831da177e4SLinus Torvalds 	SGP_CACHE,	/* don't exceed i_size, may allocate page */
84a0ee5ec5SHugh Dickins 	SGP_DIRTY,	/* like SGP_CACHE, but set new page dirty */
851da177e4SLinus Torvalds 	SGP_WRITE,	/* may exceed i_size, may allocate page */
861da177e4SLinus Torvalds };
871da177e4SLinus Torvalds 
88b76db735SAndrew Morton #ifdef CONFIG_TMPFS
89680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void)
90680d794bSakpm@linux-foundation.org {
91680d794bSakpm@linux-foundation.org 	return totalram_pages / 2;
92680d794bSakpm@linux-foundation.org }
93680d794bSakpm@linux-foundation.org 
94680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void)
95680d794bSakpm@linux-foundation.org {
96680d794bSakpm@linux-foundation.org 	return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
97680d794bSakpm@linux-foundation.org }
98b76db735SAndrew Morton #endif
99680d794bSakpm@linux-foundation.org 
1001da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx,
1011da177e4SLinus Torvalds 			 struct page **pagep, enum sgp_type sgp, int *type);
1021da177e4SLinus Torvalds 
1036daa0e28SAl Viro static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	/*
1061da177e4SLinus Torvalds 	 * The above definition of ENTRIES_PER_PAGE, and the use of
1071da177e4SLinus Torvalds 	 * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
1081da177e4SLinus Torvalds 	 * might be reconsidered if it ever diverges from PAGE_SIZE.
109769848c0SMel Gorman 	 *
110e12ba74dSMel Gorman 	 * Mobility flags are masked out as swap vectors cannot move
1111da177e4SLinus Torvalds 	 */
112e12ba74dSMel Gorman 	return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
113769848c0SMel Gorman 				PAGE_CACHE_SHIFT-PAGE_SHIFT);
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds static inline void shmem_dir_free(struct page *page)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	__free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds static struct page **shmem_dir_map(struct page *page)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	return (struct page **)kmap_atomic(page, KM_USER0);
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds static inline void shmem_dir_unmap(struct page **dir)
1271da177e4SLinus Torvalds {
1281da177e4SLinus Torvalds 	kunmap_atomic(dir, KM_USER0);
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds static swp_entry_t *shmem_swp_map(struct page *page)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	return (swp_entry_t *)kmap_atomic(page, KM_USER1);
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds static inline void shmem_swp_balance_unmap(void)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	/*
1391da177e4SLinus Torvalds 	 * When passing a pointer to an i_direct entry, to code which
1401da177e4SLinus Torvalds 	 * also handles indirect entries and so will shmem_swp_unmap,
1411da177e4SLinus Torvalds 	 * we must arrange for the preempt count to remain in balance.
1421da177e4SLinus Torvalds 	 * What kmap_atomic of a lowmem page does depends on config
1431da177e4SLinus Torvalds 	 * and architecture, so pretend to kmap_atomic some lowmem page.
1441da177e4SLinus Torvalds 	 */
1451da177e4SLinus Torvalds 	(void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds static inline void shmem_swp_unmap(swp_entry_t *entry)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	kunmap_atomic(entry, KM_USER1);
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds 	return sb->s_fs_info;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds /*
1591da177e4SLinus Torvalds  * shmem_file_setup pre-accounts the whole fixed size of a VM object,
1601da177e4SLinus Torvalds  * for shared memory and for shared anonymous (/dev/zero) mappings
1611da177e4SLinus Torvalds  * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
1621da177e4SLinus Torvalds  * consistent with the pre-accounting of private mappings ...
1631da177e4SLinus Torvalds  */
1641da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size)
1651da177e4SLinus Torvalds {
1661da177e4SLinus Torvalds 	return (flags & VM_ACCOUNT)?
1671da177e4SLinus Torvalds 		security_vm_enough_memory(VM_ACCT(size)): 0;
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	if (flags & VM_ACCOUNT)
1731da177e4SLinus Torvalds 		vm_unacct_memory(VM_ACCT(size));
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds /*
1771da177e4SLinus Torvalds  * ... whereas tmpfs objects are accounted incrementally as
1781da177e4SLinus Torvalds  * pages are allocated, in order to allow huge sparse files.
1791da177e4SLinus Torvalds  * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
1801da177e4SLinus Torvalds  * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
1811da177e4SLinus Torvalds  */
1821da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	return (flags & VM_ACCOUNT)?
1851da177e4SLinus Torvalds 		0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	if (!(flags & VM_ACCOUNT))
1911da177e4SLinus Torvalds 		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
194759b9775SHugh Dickins static const struct super_operations shmem_ops;
195f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops;
19615ad7cdcSHelge Deller static const struct file_operations shmem_file_operations;
19792e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations;
19892e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations;
19992e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations;
2001da177e4SLinus Torvalds static struct vm_operations_struct shmem_vm_ops;
2011da177e4SLinus Torvalds 
2026c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info  __read_mostly = {
2031da177e4SLinus Torvalds 	.ra_pages	= 0,	/* No readahead */
2041da177e4SLinus Torvalds 	.capabilities	= BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
2051da177e4SLinus Torvalds 	.unplug_io_fn	= default_unplug_io_fn,
2061da177e4SLinus Torvalds };
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist);
209cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex);
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds static void shmem_free_blocks(struct inode *inode, long pages)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
2140edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
2151da177e4SLinus Torvalds 		spin_lock(&sbinfo->stat_lock);
2161da177e4SLinus Torvalds 		sbinfo->free_blocks += pages;
2171da177e4SLinus Torvalds 		inode->i_blocks -= pages*BLOCKS_PER_PAGE;
2181da177e4SLinus Torvalds 		spin_unlock(&sbinfo->stat_lock);
2191da177e4SLinus Torvalds 	}
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2225b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb)
2235b04c689SPavel Emelyanov {
2245b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2255b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2265b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2275b04c689SPavel Emelyanov 		if (!sbinfo->free_inodes) {
2285b04c689SPavel Emelyanov 			spin_unlock(&sbinfo->stat_lock);
2295b04c689SPavel Emelyanov 			return -ENOSPC;
2305b04c689SPavel Emelyanov 		}
2315b04c689SPavel Emelyanov 		sbinfo->free_inodes--;
2325b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2335b04c689SPavel Emelyanov 	}
2345b04c689SPavel Emelyanov 	return 0;
2355b04c689SPavel Emelyanov }
2365b04c689SPavel Emelyanov 
2375b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb)
2385b04c689SPavel Emelyanov {
2395b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2405b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2415b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2425b04c689SPavel Emelyanov 		sbinfo->free_inodes++;
2435b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2445b04c689SPavel Emelyanov 	}
2455b04c689SPavel Emelyanov }
2465b04c689SPavel Emelyanov 
24746711810SRandy Dunlap /**
2481da177e4SLinus Torvalds  * shmem_recalc_inode - recalculate the size of an inode
2491da177e4SLinus Torvalds  * @inode: inode to recalc
2501da177e4SLinus Torvalds  *
2511da177e4SLinus Torvalds  * We have to calculate the free blocks since the mm can drop
2521da177e4SLinus Torvalds  * undirtied hole pages behind our back.
2531da177e4SLinus Torvalds  *
2541da177e4SLinus Torvalds  * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
2551da177e4SLinus Torvalds  * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
2561da177e4SLinus Torvalds  *
2571da177e4SLinus Torvalds  * It has to be called with the spinlock held.
2581da177e4SLinus Torvalds  */
2591da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
2621da177e4SLinus Torvalds 	long freed;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
2651da177e4SLinus Torvalds 	if (freed > 0) {
2661da177e4SLinus Torvalds 		info->alloced -= freed;
2671da177e4SLinus Torvalds 		shmem_unacct_blocks(info->flags, freed);
2681da177e4SLinus Torvalds 		shmem_free_blocks(inode, freed);
2691da177e4SLinus Torvalds 	}
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
27246711810SRandy Dunlap /**
2731da177e4SLinus Torvalds  * shmem_swp_entry - find the swap vector position in the info structure
2741da177e4SLinus Torvalds  * @info:  info structure for the inode
2751da177e4SLinus Torvalds  * @index: index of the page to find
2761da177e4SLinus Torvalds  * @page:  optional page to add to the structure. Has to be preset to
2771da177e4SLinus Torvalds  *         all zeros
2781da177e4SLinus Torvalds  *
2791da177e4SLinus Torvalds  * If there is no space allocated yet it will return NULL when
2801da177e4SLinus Torvalds  * page is NULL, else it will use the page for the needed block,
2811da177e4SLinus Torvalds  * setting it to NULL on return to indicate that it has been used.
2821da177e4SLinus Torvalds  *
2831da177e4SLinus Torvalds  * The swap vector is organized the following way:
2841da177e4SLinus Torvalds  *
2851da177e4SLinus Torvalds  * There are SHMEM_NR_DIRECT entries directly stored in the
2861da177e4SLinus Torvalds  * shmem_inode_info structure. So small files do not need an addional
2871da177e4SLinus Torvalds  * allocation.
2881da177e4SLinus Torvalds  *
2891da177e4SLinus Torvalds  * For pages with index > SHMEM_NR_DIRECT there is the pointer
2901da177e4SLinus Torvalds  * i_indirect which points to a page which holds in the first half
2911da177e4SLinus Torvalds  * doubly indirect blocks, in the second half triple indirect blocks:
2921da177e4SLinus Torvalds  *
2931da177e4SLinus Torvalds  * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
2941da177e4SLinus Torvalds  * following layout (for SHMEM_NR_DIRECT == 16):
2951da177e4SLinus Torvalds  *
2961da177e4SLinus Torvalds  * i_indirect -> dir --> 16-19
2971da177e4SLinus Torvalds  * 	      |	     +-> 20-23
2981da177e4SLinus Torvalds  * 	      |
2991da177e4SLinus Torvalds  * 	      +-->dir2 --> 24-27
3001da177e4SLinus Torvalds  * 	      |	       +-> 28-31
3011da177e4SLinus Torvalds  * 	      |	       +-> 32-35
3021da177e4SLinus Torvalds  * 	      |	       +-> 36-39
3031da177e4SLinus Torvalds  * 	      |
3041da177e4SLinus Torvalds  * 	      +-->dir3 --> 40-43
3051da177e4SLinus Torvalds  * 	       	       +-> 44-47
3061da177e4SLinus Torvalds  * 	      	       +-> 48-51
3071da177e4SLinus Torvalds  * 	      	       +-> 52-55
3081da177e4SLinus Torvalds  */
3091da177e4SLinus Torvalds static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
3101da177e4SLinus Torvalds {
3111da177e4SLinus Torvalds 	unsigned long offset;
3121da177e4SLinus Torvalds 	struct page **dir;
3131da177e4SLinus Torvalds 	struct page *subdir;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	if (index < SHMEM_NR_DIRECT) {
3161da177e4SLinus Torvalds 		shmem_swp_balance_unmap();
3171da177e4SLinus Torvalds 		return info->i_direct+index;
3181da177e4SLinus Torvalds 	}
3191da177e4SLinus Torvalds 	if (!info->i_indirect) {
3201da177e4SLinus Torvalds 		if (page) {
3211da177e4SLinus Torvalds 			info->i_indirect = *page;
3221da177e4SLinus Torvalds 			*page = NULL;
3231da177e4SLinus Torvalds 		}
3241da177e4SLinus Torvalds 		return NULL;			/* need another page */
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	index -= SHMEM_NR_DIRECT;
3281da177e4SLinus Torvalds 	offset = index % ENTRIES_PER_PAGE;
3291da177e4SLinus Torvalds 	index /= ENTRIES_PER_PAGE;
3301da177e4SLinus Torvalds 	dir = shmem_dir_map(info->i_indirect);
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	if (index >= ENTRIES_PER_PAGE/2) {
3331da177e4SLinus Torvalds 		index -= ENTRIES_PER_PAGE/2;
3341da177e4SLinus Torvalds 		dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
3351da177e4SLinus Torvalds 		index %= ENTRIES_PER_PAGE;
3361da177e4SLinus Torvalds 		subdir = *dir;
3371da177e4SLinus Torvalds 		if (!subdir) {
3381da177e4SLinus Torvalds 			if (page) {
3391da177e4SLinus Torvalds 				*dir = *page;
3401da177e4SLinus Torvalds 				*page = NULL;
3411da177e4SLinus Torvalds 			}
3421da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
3431da177e4SLinus Torvalds 			return NULL;		/* need another page */
3441da177e4SLinus Torvalds 		}
3451da177e4SLinus Torvalds 		shmem_dir_unmap(dir);
3461da177e4SLinus Torvalds 		dir = shmem_dir_map(subdir);
3471da177e4SLinus Torvalds 	}
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	dir += index;
3501da177e4SLinus Torvalds 	subdir = *dir;
3511da177e4SLinus Torvalds 	if (!subdir) {
3521da177e4SLinus Torvalds 		if (!page || !(subdir = *page)) {
3531da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
3541da177e4SLinus Torvalds 			return NULL;		/* need a page */
3551da177e4SLinus Torvalds 		}
3561da177e4SLinus Torvalds 		*dir = subdir;
3571da177e4SLinus Torvalds 		*page = NULL;
3581da177e4SLinus Torvalds 	}
3591da177e4SLinus Torvalds 	shmem_dir_unmap(dir);
3601da177e4SLinus Torvalds 	return shmem_swp_map(subdir) + offset;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
3641da177e4SLinus Torvalds {
3651da177e4SLinus Torvalds 	long incdec = value? 1: -1;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	entry->val = value;
3681da177e4SLinus Torvalds 	info->swapped += incdec;
3694c21e2f2SHugh Dickins 	if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
3704c21e2f2SHugh Dickins 		struct page *page = kmap_atomic_to_page(entry);
3714c21e2f2SHugh Dickins 		set_page_private(page, page_private(page) + incdec);
3724c21e2f2SHugh Dickins 	}
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
37546711810SRandy Dunlap /**
3761da177e4SLinus Torvalds  * shmem_swp_alloc - get the position of the swap entry for the page.
3771da177e4SLinus Torvalds  * @info:	info structure for the inode
3781da177e4SLinus Torvalds  * @index:	index of the page to find
3791da177e4SLinus Torvalds  * @sgp:	check and recheck i_size? skip allocation?
38046711810SRandy Dunlap  *
38146711810SRandy Dunlap  * If the entry does not exist, allocate it.
3821da177e4SLinus Torvalds  */
3831da177e4SLinus Torvalds static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	struct inode *inode = &info->vfs_inode;
3861da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3871da177e4SLinus Torvalds 	struct page *page = NULL;
3881da177e4SLinus Torvalds 	swp_entry_t *entry;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	if (sgp != SGP_WRITE &&
3911da177e4SLinus Torvalds 	    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
3921da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	while (!(entry = shmem_swp_entry(info, index, &page))) {
3951da177e4SLinus Torvalds 		if (sgp == SGP_READ)
3961da177e4SLinus Torvalds 			return shmem_swp_map(ZERO_PAGE(0));
3971da177e4SLinus Torvalds 		/*
3981da177e4SLinus Torvalds 		 * Test free_blocks against 1 not 0, since we have 1 data
3991da177e4SLinus Torvalds 		 * page (and perhaps indirect index pages) yet to allocate:
4001da177e4SLinus Torvalds 		 * a waste to allocate index if we cannot allocate data.
4011da177e4SLinus Torvalds 		 */
4020edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
4031da177e4SLinus Torvalds 			spin_lock(&sbinfo->stat_lock);
4041da177e4SLinus Torvalds 			if (sbinfo->free_blocks <= 1) {
4051da177e4SLinus Torvalds 				spin_unlock(&sbinfo->stat_lock);
4061da177e4SLinus Torvalds 				return ERR_PTR(-ENOSPC);
4071da177e4SLinus Torvalds 			}
4081da177e4SLinus Torvalds 			sbinfo->free_blocks--;
4091da177e4SLinus Torvalds 			inode->i_blocks += BLOCKS_PER_PAGE;
4101da177e4SLinus Torvalds 			spin_unlock(&sbinfo->stat_lock);
4111da177e4SLinus Torvalds 		}
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 		spin_unlock(&info->lock);
414769848c0SMel Gorman 		page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping));
4154c21e2f2SHugh Dickins 		if (page)
4164c21e2f2SHugh Dickins 			set_page_private(page, 0);
4171da177e4SLinus Torvalds 		spin_lock(&info->lock);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 		if (!page) {
4201da177e4SLinus Torvalds 			shmem_free_blocks(inode, 1);
4211da177e4SLinus Torvalds 			return ERR_PTR(-ENOMEM);
4221da177e4SLinus Torvalds 		}
4231da177e4SLinus Torvalds 		if (sgp != SGP_WRITE &&
4241da177e4SLinus Torvalds 		    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
4251da177e4SLinus Torvalds 			entry = ERR_PTR(-EINVAL);
4261da177e4SLinus Torvalds 			break;
4271da177e4SLinus Torvalds 		}
4281da177e4SLinus Torvalds 		if (info->next_index <= index)
4291da177e4SLinus Torvalds 			info->next_index = index + 1;
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 	if (page) {
4321da177e4SLinus Torvalds 		/* another task gave its page, or truncated the file */
4331da177e4SLinus Torvalds 		shmem_free_blocks(inode, 1);
4341da177e4SLinus Torvalds 		shmem_dir_free(page);
4351da177e4SLinus Torvalds 	}
4361da177e4SLinus Torvalds 	if (info->next_index <= index && !IS_ERR(entry))
4371da177e4SLinus Torvalds 		info->next_index = index + 1;
4381da177e4SLinus Torvalds 	return entry;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
44146711810SRandy Dunlap /**
4421da177e4SLinus Torvalds  * shmem_free_swp - free some swap entries in a directory
4431da177e4SLinus Torvalds  * @dir:        pointer to the directory
4441da177e4SLinus Torvalds  * @edir:       pointer after last entry of the directory
4451ae70006SHugh Dickins  * @punch_lock: pointer to spinlock when needed for the holepunch case
4461da177e4SLinus Torvalds  */
4471ae70006SHugh Dickins static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
4481ae70006SHugh Dickins 						spinlock_t *punch_lock)
4491da177e4SLinus Torvalds {
4501ae70006SHugh Dickins 	spinlock_t *punch_unlock = NULL;
4511da177e4SLinus Torvalds 	swp_entry_t *ptr;
4521da177e4SLinus Torvalds 	int freed = 0;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	for (ptr = dir; ptr < edir; ptr++) {
4551da177e4SLinus Torvalds 		if (ptr->val) {
4561ae70006SHugh Dickins 			if (unlikely(punch_lock)) {
4571ae70006SHugh Dickins 				punch_unlock = punch_lock;
4581ae70006SHugh Dickins 				punch_lock = NULL;
4591ae70006SHugh Dickins 				spin_lock(punch_unlock);
4601ae70006SHugh Dickins 				if (!ptr->val)
4611ae70006SHugh Dickins 					continue;
4621ae70006SHugh Dickins 			}
4631da177e4SLinus Torvalds 			free_swap_and_cache(*ptr);
4641da177e4SLinus Torvalds 			*ptr = (swp_entry_t){0};
4651da177e4SLinus Torvalds 			freed++;
4661da177e4SLinus Torvalds 		}
4671da177e4SLinus Torvalds 	}
4681ae70006SHugh Dickins 	if (punch_unlock)
4691ae70006SHugh Dickins 		spin_unlock(punch_unlock);
4701da177e4SLinus Torvalds 	return freed;
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
4731ae70006SHugh Dickins static int shmem_map_and_free_swp(struct page *subdir, int offset,
4741ae70006SHugh Dickins 		int limit, struct page ***dir, spinlock_t *punch_lock)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds 	swp_entry_t *ptr;
4771da177e4SLinus Torvalds 	int freed = 0;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	ptr = shmem_swp_map(subdir);
4801da177e4SLinus Torvalds 	for (; offset < limit; offset += LATENCY_LIMIT) {
4811da177e4SLinus Torvalds 		int size = limit - offset;
4821da177e4SLinus Torvalds 		if (size > LATENCY_LIMIT)
4831da177e4SLinus Torvalds 			size = LATENCY_LIMIT;
4841ae70006SHugh Dickins 		freed += shmem_free_swp(ptr+offset, ptr+offset+size,
4851ae70006SHugh Dickins 							punch_lock);
4861da177e4SLinus Torvalds 		if (need_resched()) {
4871da177e4SLinus Torvalds 			shmem_swp_unmap(ptr);
4881da177e4SLinus Torvalds 			if (*dir) {
4891da177e4SLinus Torvalds 				shmem_dir_unmap(*dir);
4901da177e4SLinus Torvalds 				*dir = NULL;
4911da177e4SLinus Torvalds 			}
4921da177e4SLinus Torvalds 			cond_resched();
4931da177e4SLinus Torvalds 			ptr = shmem_swp_map(subdir);
4941da177e4SLinus Torvalds 		}
4951da177e4SLinus Torvalds 	}
4961da177e4SLinus Torvalds 	shmem_swp_unmap(ptr);
4971da177e4SLinus Torvalds 	return freed;
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds static void shmem_free_pages(struct list_head *next)
5011da177e4SLinus Torvalds {
5021da177e4SLinus Torvalds 	struct page *page;
5031da177e4SLinus Torvalds 	int freed = 0;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	do {
5061da177e4SLinus Torvalds 		page = container_of(next, struct page, lru);
5071da177e4SLinus Torvalds 		next = next->next;
5081da177e4SLinus Torvalds 		shmem_dir_free(page);
5091da177e4SLinus Torvalds 		freed++;
5101da177e4SLinus Torvalds 		if (freed >= LATENCY_LIMIT) {
5111da177e4SLinus Torvalds 			cond_resched();
5121da177e4SLinus Torvalds 			freed = 0;
5131da177e4SLinus Torvalds 		}
5141da177e4SLinus Torvalds 	} while (next);
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds 
517f6b3ec23SBadari Pulavarty static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
5181da177e4SLinus Torvalds {
5191da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
5201da177e4SLinus Torvalds 	unsigned long idx;
5211da177e4SLinus Torvalds 	unsigned long size;
5221da177e4SLinus Torvalds 	unsigned long limit;
5231da177e4SLinus Torvalds 	unsigned long stage;
5241da177e4SLinus Torvalds 	unsigned long diroff;
5251da177e4SLinus Torvalds 	struct page **dir;
5261da177e4SLinus Torvalds 	struct page *topdir;
5271da177e4SLinus Torvalds 	struct page *middir;
5281da177e4SLinus Torvalds 	struct page *subdir;
5291da177e4SLinus Torvalds 	swp_entry_t *ptr;
5301da177e4SLinus Torvalds 	LIST_HEAD(pages_to_free);
5311da177e4SLinus Torvalds 	long nr_pages_to_free = 0;
5321da177e4SLinus Torvalds 	long nr_swaps_freed = 0;
5331da177e4SLinus Torvalds 	int offset;
5341da177e4SLinus Torvalds 	int freed;
535a2646d1eSHugh Dickins 	int punch_hole;
5361ae70006SHugh Dickins 	spinlock_t *needs_lock;
5371ae70006SHugh Dickins 	spinlock_t *punch_lock;
538a2646d1eSHugh Dickins 	unsigned long upper_limit;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
541f6b3ec23SBadari Pulavarty 	idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
5421da177e4SLinus Torvalds 	if (idx >= info->next_index)
5431da177e4SLinus Torvalds 		return;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	spin_lock(&info->lock);
5461da177e4SLinus Torvalds 	info->flags |= SHMEM_TRUNCATE;
547f6b3ec23SBadari Pulavarty 	if (likely(end == (loff_t) -1)) {
5481da177e4SLinus Torvalds 		limit = info->next_index;
549a2646d1eSHugh Dickins 		upper_limit = SHMEM_MAX_INDEX;
5501da177e4SLinus Torvalds 		info->next_index = idx;
5511ae70006SHugh Dickins 		needs_lock = NULL;
552a2646d1eSHugh Dickins 		punch_hole = 0;
553f6b3ec23SBadari Pulavarty 	} else {
554a2646d1eSHugh Dickins 		if (end + 1 >= inode->i_size) {	/* we may free a little more */
555a2646d1eSHugh Dickins 			limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
556a2646d1eSHugh Dickins 							PAGE_CACHE_SHIFT;
557a2646d1eSHugh Dickins 			upper_limit = SHMEM_MAX_INDEX;
558a2646d1eSHugh Dickins 		} else {
559a2646d1eSHugh Dickins 			limit = (end + 1) >> PAGE_CACHE_SHIFT;
560a2646d1eSHugh Dickins 			upper_limit = limit;
561a2646d1eSHugh Dickins 		}
5621ae70006SHugh Dickins 		needs_lock = &info->lock;
563f6b3ec23SBadari Pulavarty 		punch_hole = 1;
564f6b3ec23SBadari Pulavarty 	}
565f6b3ec23SBadari Pulavarty 
5661da177e4SLinus Torvalds 	topdir = info->i_indirect;
567f6b3ec23SBadari Pulavarty 	if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
5681da177e4SLinus Torvalds 		info->i_indirect = NULL;
5691da177e4SLinus Torvalds 		nr_pages_to_free++;
5701da177e4SLinus Torvalds 		list_add(&topdir->lru, &pages_to_free);
5711da177e4SLinus Torvalds 	}
5721da177e4SLinus Torvalds 	spin_unlock(&info->lock);
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	if (info->swapped && idx < SHMEM_NR_DIRECT) {
5751da177e4SLinus Torvalds 		ptr = info->i_direct;
5761da177e4SLinus Torvalds 		size = limit;
5771da177e4SLinus Torvalds 		if (size > SHMEM_NR_DIRECT)
5781da177e4SLinus Torvalds 			size = SHMEM_NR_DIRECT;
5791ae70006SHugh Dickins 		nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
5801da177e4SLinus Torvalds 	}
58192a3d03aSBadari Pulavarty 
58292a3d03aSBadari Pulavarty 	/*
58392a3d03aSBadari Pulavarty 	 * If there are no indirect blocks or we are punching a hole
58492a3d03aSBadari Pulavarty 	 * below indirect blocks, nothing to be done.
58592a3d03aSBadari Pulavarty 	 */
586a2646d1eSHugh Dickins 	if (!topdir || limit <= SHMEM_NR_DIRECT)
5871da177e4SLinus Torvalds 		goto done2;
5881da177e4SLinus Torvalds 
5891ae70006SHugh Dickins 	/*
5901ae70006SHugh Dickins 	 * The truncation case has already dropped info->lock, and we're safe
5911ae70006SHugh Dickins 	 * because i_size and next_index have already been lowered, preventing
5921ae70006SHugh Dickins 	 * access beyond.  But in the punch_hole case, we still need to take
5931ae70006SHugh Dickins 	 * the lock when updating the swap directory, because there might be
5941ae70006SHugh Dickins 	 * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
5951ae70006SHugh Dickins 	 * shmem_writepage.  However, whenever we find we can remove a whole
5961ae70006SHugh Dickins 	 * directory page (not at the misaligned start or end of the range),
5971ae70006SHugh Dickins 	 * we first NULLify its pointer in the level above, and then have no
5981ae70006SHugh Dickins 	 * need to take the lock when updating its contents: needs_lock and
5991ae70006SHugh Dickins 	 * punch_lock (either pointing to info->lock or NULL) manage this.
6001ae70006SHugh Dickins 	 */
6011ae70006SHugh Dickins 
602a2646d1eSHugh Dickins 	upper_limit -= SHMEM_NR_DIRECT;
6031da177e4SLinus Torvalds 	limit -= SHMEM_NR_DIRECT;
6041da177e4SLinus Torvalds 	idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
6051da177e4SLinus Torvalds 	offset = idx % ENTRIES_PER_PAGE;
6061da177e4SLinus Torvalds 	idx -= offset;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	dir = shmem_dir_map(topdir);
6091da177e4SLinus Torvalds 	stage = ENTRIES_PER_PAGEPAGE/2;
6101da177e4SLinus Torvalds 	if (idx < ENTRIES_PER_PAGEPAGE/2) {
6111da177e4SLinus Torvalds 		middir = topdir;
6121da177e4SLinus Torvalds 		diroff = idx/ENTRIES_PER_PAGE;
6131da177e4SLinus Torvalds 	} else {
6141da177e4SLinus Torvalds 		dir += ENTRIES_PER_PAGE/2;
6151da177e4SLinus Torvalds 		dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
6161da177e4SLinus Torvalds 		while (stage <= idx)
6171da177e4SLinus Torvalds 			stage += ENTRIES_PER_PAGEPAGE;
6181da177e4SLinus Torvalds 		middir = *dir;
6191da177e4SLinus Torvalds 		if (*dir) {
6201da177e4SLinus Torvalds 			diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
6211da177e4SLinus Torvalds 				ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
622a2646d1eSHugh Dickins 			if (!diroff && !offset && upper_limit >= stage) {
6231ae70006SHugh Dickins 				if (needs_lock) {
6241ae70006SHugh Dickins 					spin_lock(needs_lock);
6251ae70006SHugh Dickins 					*dir = NULL;
6261ae70006SHugh Dickins 					spin_unlock(needs_lock);
6271ae70006SHugh Dickins 					needs_lock = NULL;
6281ae70006SHugh Dickins 				} else
6291da177e4SLinus Torvalds 					*dir = NULL;
6301da177e4SLinus Torvalds 				nr_pages_to_free++;
6311da177e4SLinus Torvalds 				list_add(&middir->lru, &pages_to_free);
6321da177e4SLinus Torvalds 			}
6331da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6341da177e4SLinus Torvalds 			dir = shmem_dir_map(middir);
6351da177e4SLinus Torvalds 		} else {
6361da177e4SLinus Torvalds 			diroff = 0;
6371da177e4SLinus Torvalds 			offset = 0;
6381da177e4SLinus Torvalds 			idx = stage;
6391da177e4SLinus Torvalds 		}
6401da177e4SLinus Torvalds 	}
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
6431da177e4SLinus Torvalds 		if (unlikely(idx == stage)) {
6441da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6451da177e4SLinus Torvalds 			dir = shmem_dir_map(topdir) +
6461da177e4SLinus Torvalds 			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
6471da177e4SLinus Torvalds 			while (!*dir) {
6481da177e4SLinus Torvalds 				dir++;
6491da177e4SLinus Torvalds 				idx += ENTRIES_PER_PAGEPAGE;
6501da177e4SLinus Torvalds 				if (idx >= limit)
6511da177e4SLinus Torvalds 					goto done1;
6521da177e4SLinus Torvalds 			}
6531da177e4SLinus Torvalds 			stage = idx + ENTRIES_PER_PAGEPAGE;
6541da177e4SLinus Torvalds 			middir = *dir;
6551ae70006SHugh Dickins 			if (punch_hole)
6561ae70006SHugh Dickins 				needs_lock = &info->lock;
657a2646d1eSHugh Dickins 			if (upper_limit >= stage) {
6581ae70006SHugh Dickins 				if (needs_lock) {
6591ae70006SHugh Dickins 					spin_lock(needs_lock);
6601ae70006SHugh Dickins 					*dir = NULL;
6611ae70006SHugh Dickins 					spin_unlock(needs_lock);
6621ae70006SHugh Dickins 					needs_lock = NULL;
6631ae70006SHugh Dickins 				} else
6641da177e4SLinus Torvalds 					*dir = NULL;
6651da177e4SLinus Torvalds 				nr_pages_to_free++;
6661da177e4SLinus Torvalds 				list_add(&middir->lru, &pages_to_free);
667a2646d1eSHugh Dickins 			}
6681da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6691da177e4SLinus Torvalds 			cond_resched();
6701da177e4SLinus Torvalds 			dir = shmem_dir_map(middir);
6711da177e4SLinus Torvalds 			diroff = 0;
6721da177e4SLinus Torvalds 		}
6731ae70006SHugh Dickins 		punch_lock = needs_lock;
6741da177e4SLinus Torvalds 		subdir = dir[diroff];
6751ae70006SHugh Dickins 		if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
6761ae70006SHugh Dickins 			if (needs_lock) {
6771ae70006SHugh Dickins 				spin_lock(needs_lock);
6781ae70006SHugh Dickins 				dir[diroff] = NULL;
6791ae70006SHugh Dickins 				spin_unlock(needs_lock);
6801ae70006SHugh Dickins 				punch_lock = NULL;
6811ae70006SHugh Dickins 			} else
6821da177e4SLinus Torvalds 				dir[diroff] = NULL;
6831da177e4SLinus Torvalds 			nr_pages_to_free++;
6841da177e4SLinus Torvalds 			list_add(&subdir->lru, &pages_to_free);
6851da177e4SLinus Torvalds 		}
6861ae70006SHugh Dickins 		if (subdir && page_private(subdir) /* has swap entries */) {
6871ae70006SHugh Dickins 			size = limit - idx;
6881ae70006SHugh Dickins 			if (size > ENTRIES_PER_PAGE)
6891ae70006SHugh Dickins 				size = ENTRIES_PER_PAGE;
6901ae70006SHugh Dickins 			freed = shmem_map_and_free_swp(subdir,
6911ae70006SHugh Dickins 					offset, size, &dir, punch_lock);
6921ae70006SHugh Dickins 			if (!dir)
6931ae70006SHugh Dickins 				dir = shmem_dir_map(middir);
6941ae70006SHugh Dickins 			nr_swaps_freed += freed;
6951ae70006SHugh Dickins 			if (offset || punch_lock) {
6961ae70006SHugh Dickins 				spin_lock(&info->lock);
6971ae70006SHugh Dickins 				set_page_private(subdir,
6981ae70006SHugh Dickins 					page_private(subdir) - freed);
6991ae70006SHugh Dickins 				spin_unlock(&info->lock);
7001ae70006SHugh Dickins 			} else
7011ae70006SHugh Dickins 				BUG_ON(page_private(subdir) != freed);
7021ae70006SHugh Dickins 		}
7031ae70006SHugh Dickins 		offset = 0;
7041da177e4SLinus Torvalds 	}
7051da177e4SLinus Torvalds done1:
7061da177e4SLinus Torvalds 	shmem_dir_unmap(dir);
7071da177e4SLinus Torvalds done2:
7081da177e4SLinus Torvalds 	if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
7091da177e4SLinus Torvalds 		/*
7101da177e4SLinus Torvalds 		 * Call truncate_inode_pages again: racing shmem_unuse_inode
7111da177e4SLinus Torvalds 		 * may have swizzled a page in from swap since vmtruncate or
7121da177e4SLinus Torvalds 		 * generic_delete_inode did it, before we lowered next_index.
7131da177e4SLinus Torvalds 		 * Also, though shmem_getpage checks i_size before adding to
7141da177e4SLinus Torvalds 		 * cache, no recheck after: so fix the narrow window there too.
71516a10019SHugh Dickins 		 *
71616a10019SHugh Dickins 		 * Recalling truncate_inode_pages_range and unmap_mapping_range
71716a10019SHugh Dickins 		 * every time for punch_hole (which never got a chance to clear
71816a10019SHugh Dickins 		 * SHMEM_PAGEIN at the start of vmtruncate_range) is expensive,
71916a10019SHugh Dickins 		 * yet hardly ever necessary: try to optimize them out later.
7201da177e4SLinus Torvalds 		 */
721f6b3ec23SBadari Pulavarty 		truncate_inode_pages_range(inode->i_mapping, start, end);
72216a10019SHugh Dickins 		if (punch_hole)
72316a10019SHugh Dickins 			unmap_mapping_range(inode->i_mapping, start,
72416a10019SHugh Dickins 							end - start, 1);
7251da177e4SLinus Torvalds 	}
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	spin_lock(&info->lock);
7281da177e4SLinus Torvalds 	info->flags &= ~SHMEM_TRUNCATE;
7291da177e4SLinus Torvalds 	info->swapped -= nr_swaps_freed;
7301da177e4SLinus Torvalds 	if (nr_pages_to_free)
7311da177e4SLinus Torvalds 		shmem_free_blocks(inode, nr_pages_to_free);
7321da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
7331da177e4SLinus Torvalds 	spin_unlock(&info->lock);
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	/*
7361da177e4SLinus Torvalds 	 * Empty swap vector directory pages to be freed?
7371da177e4SLinus Torvalds 	 */
7381da177e4SLinus Torvalds 	if (!list_empty(&pages_to_free)) {
7391da177e4SLinus Torvalds 		pages_to_free.prev->next = NULL;
7401da177e4SLinus Torvalds 		shmem_free_pages(pages_to_free.next);
7411da177e4SLinus Torvalds 	}
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
744f6b3ec23SBadari Pulavarty static void shmem_truncate(struct inode *inode)
745f6b3ec23SBadari Pulavarty {
746f6b3ec23SBadari Pulavarty 	shmem_truncate_range(inode, inode->i_size, (loff_t)-1);
747f6b3ec23SBadari Pulavarty }
748f6b3ec23SBadari Pulavarty 
7491da177e4SLinus Torvalds static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
7521da177e4SLinus Torvalds 	struct page *page = NULL;
7531da177e4SLinus Torvalds 	int error;
7541da177e4SLinus Torvalds 
75539f0247dSAndreas Gruenbacher 	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
7561da177e4SLinus Torvalds 		if (attr->ia_size < inode->i_size) {
7571da177e4SLinus Torvalds 			/*
7581da177e4SLinus Torvalds 			 * If truncating down to a partial page, then
7591da177e4SLinus Torvalds 			 * if that page is already allocated, hold it
7601da177e4SLinus Torvalds 			 * in memory until the truncation is over, so
7611da177e4SLinus Torvalds 			 * truncate_partial_page cannnot miss it were
7621da177e4SLinus Torvalds 			 * it assigned to swap.
7631da177e4SLinus Torvalds 			 */
7641da177e4SLinus Torvalds 			if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
7651da177e4SLinus Torvalds 				(void) shmem_getpage(inode,
7661da177e4SLinus Torvalds 					attr->ia_size>>PAGE_CACHE_SHIFT,
7671da177e4SLinus Torvalds 						&page, SGP_READ, NULL);
768d3602444SHugh Dickins 				if (page)
769d3602444SHugh Dickins 					unlock_page(page);
7701da177e4SLinus Torvalds 			}
7711da177e4SLinus Torvalds 			/*
7721da177e4SLinus Torvalds 			 * Reset SHMEM_PAGEIN flag so that shmem_truncate can
7731da177e4SLinus Torvalds 			 * detect if any pages might have been added to cache
7741da177e4SLinus Torvalds 			 * after truncate_inode_pages.  But we needn't bother
7751da177e4SLinus Torvalds 			 * if it's being fully truncated to zero-length: the
7761da177e4SLinus Torvalds 			 * nrpages check is efficient enough in that case.
7771da177e4SLinus Torvalds 			 */
7781da177e4SLinus Torvalds 			if (attr->ia_size) {
7791da177e4SLinus Torvalds 				struct shmem_inode_info *info = SHMEM_I(inode);
7801da177e4SLinus Torvalds 				spin_lock(&info->lock);
7811da177e4SLinus Torvalds 				info->flags &= ~SHMEM_PAGEIN;
7821da177e4SLinus Torvalds 				spin_unlock(&info->lock);
7831da177e4SLinus Torvalds 			}
7841da177e4SLinus Torvalds 		}
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	error = inode_change_ok(inode, attr);
7881da177e4SLinus Torvalds 	if (!error)
7891da177e4SLinus Torvalds 		error = inode_setattr(inode, attr);
79039f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
79139f0247dSAndreas Gruenbacher 	if (!error && (attr->ia_valid & ATTR_MODE))
79239f0247dSAndreas Gruenbacher 		error = generic_acl_chmod(inode, &shmem_acl_ops);
79339f0247dSAndreas Gruenbacher #endif
7941da177e4SLinus Torvalds 	if (page)
7951da177e4SLinus Torvalds 		page_cache_release(page);
7961da177e4SLinus Torvalds 	return error;
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds static void shmem_delete_inode(struct inode *inode)
8001da177e4SLinus Torvalds {
8011da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	if (inode->i_op->truncate == shmem_truncate) {
804fef26658SMark Fasheh 		truncate_inode_pages(inode->i_mapping, 0);
8051da177e4SLinus Torvalds 		shmem_unacct_size(info->flags, inode->i_size);
8061da177e4SLinus Torvalds 		inode->i_size = 0;
8071da177e4SLinus Torvalds 		shmem_truncate(inode);
8081da177e4SLinus Torvalds 		if (!list_empty(&info->swaplist)) {
809cb5f7b9aSHugh Dickins 			mutex_lock(&shmem_swaplist_mutex);
8101da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
811cb5f7b9aSHugh Dickins 			mutex_unlock(&shmem_swaplist_mutex);
8121da177e4SLinus Torvalds 		}
8131da177e4SLinus Torvalds 	}
8141da177e4SLinus Torvalds 	BUG_ON(inode->i_blocks);
8155b04c689SPavel Emelyanov 	shmem_free_inode(inode->i_sb);
8161da177e4SLinus Torvalds 	clear_inode(inode);
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
8201da177e4SLinus Torvalds {
8211da177e4SLinus Torvalds 	swp_entry_t *ptr;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	for (ptr = dir; ptr < edir; ptr++) {
8241da177e4SLinus Torvalds 		if (ptr->val == entry.val)
8251da177e4SLinus Torvalds 			return ptr - dir;
8261da177e4SLinus Torvalds 	}
8271da177e4SLinus Torvalds 	return -1;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
8311da177e4SLinus Torvalds {
8321da177e4SLinus Torvalds 	struct inode *inode;
8331da177e4SLinus Torvalds 	unsigned long idx;
8341da177e4SLinus Torvalds 	unsigned long size;
8351da177e4SLinus Torvalds 	unsigned long limit;
8361da177e4SLinus Torvalds 	unsigned long stage;
8371da177e4SLinus Torvalds 	struct page **dir;
8381da177e4SLinus Torvalds 	struct page *subdir;
8391da177e4SLinus Torvalds 	swp_entry_t *ptr;
8401da177e4SLinus Torvalds 	int offset;
841d9fe526aSHugh Dickins 	int error;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	idx = 0;
8441da177e4SLinus Torvalds 	ptr = info->i_direct;
8451da177e4SLinus Torvalds 	spin_lock(&info->lock);
8461b1b32f2SHugh Dickins 	if (!info->swapped) {
8471b1b32f2SHugh Dickins 		list_del_init(&info->swaplist);
8481b1b32f2SHugh Dickins 		goto lost2;
8491b1b32f2SHugh Dickins 	}
8501da177e4SLinus Torvalds 	limit = info->next_index;
8511da177e4SLinus Torvalds 	size = limit;
8521da177e4SLinus Torvalds 	if (size > SHMEM_NR_DIRECT)
8531da177e4SLinus Torvalds 		size = SHMEM_NR_DIRECT;
8541da177e4SLinus Torvalds 	offset = shmem_find_swp(entry, ptr, ptr+size);
8552e0e26c7SHugh Dickins 	if (offset >= 0)
8561da177e4SLinus Torvalds 		goto found;
8571da177e4SLinus Torvalds 	if (!info->i_indirect)
8581da177e4SLinus Torvalds 		goto lost2;
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	dir = shmem_dir_map(info->i_indirect);
8611da177e4SLinus Torvalds 	stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
8641da177e4SLinus Torvalds 		if (unlikely(idx == stage)) {
8651da177e4SLinus Torvalds 			shmem_dir_unmap(dir-1);
866cb5f7b9aSHugh Dickins 			if (cond_resched_lock(&info->lock)) {
867cb5f7b9aSHugh Dickins 				/* check it has not been truncated */
868cb5f7b9aSHugh Dickins 				if (limit > info->next_index) {
869cb5f7b9aSHugh Dickins 					limit = info->next_index;
870cb5f7b9aSHugh Dickins 					if (idx >= limit)
871cb5f7b9aSHugh Dickins 						goto lost2;
872cb5f7b9aSHugh Dickins 				}
873cb5f7b9aSHugh Dickins 			}
8741da177e4SLinus Torvalds 			dir = shmem_dir_map(info->i_indirect) +
8751da177e4SLinus Torvalds 			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
8761da177e4SLinus Torvalds 			while (!*dir) {
8771da177e4SLinus Torvalds 				dir++;
8781da177e4SLinus Torvalds 				idx += ENTRIES_PER_PAGEPAGE;
8791da177e4SLinus Torvalds 				if (idx >= limit)
8801da177e4SLinus Torvalds 					goto lost1;
8811da177e4SLinus Torvalds 			}
8821da177e4SLinus Torvalds 			stage = idx + ENTRIES_PER_PAGEPAGE;
8831da177e4SLinus Torvalds 			subdir = *dir;
8841da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
8851da177e4SLinus Torvalds 			dir = shmem_dir_map(subdir);
8861da177e4SLinus Torvalds 		}
8871da177e4SLinus Torvalds 		subdir = *dir;
8884c21e2f2SHugh Dickins 		if (subdir && page_private(subdir)) {
8891da177e4SLinus Torvalds 			ptr = shmem_swp_map(subdir);
8901da177e4SLinus Torvalds 			size = limit - idx;
8911da177e4SLinus Torvalds 			if (size > ENTRIES_PER_PAGE)
8921da177e4SLinus Torvalds 				size = ENTRIES_PER_PAGE;
8931da177e4SLinus Torvalds 			offset = shmem_find_swp(entry, ptr, ptr+size);
8942e0e26c7SHugh Dickins 			shmem_swp_unmap(ptr);
8951da177e4SLinus Torvalds 			if (offset >= 0) {
8961da177e4SLinus Torvalds 				shmem_dir_unmap(dir);
8971da177e4SLinus Torvalds 				goto found;
8981da177e4SLinus Torvalds 			}
8991da177e4SLinus Torvalds 		}
9001da177e4SLinus Torvalds 	}
9011da177e4SLinus Torvalds lost1:
9021da177e4SLinus Torvalds 	shmem_dir_unmap(dir-1);
9031da177e4SLinus Torvalds lost2:
9041da177e4SLinus Torvalds 	spin_unlock(&info->lock);
9051da177e4SLinus Torvalds 	return 0;
9061da177e4SLinus Torvalds found:
9071da177e4SLinus Torvalds 	idx += offset;
9082e0e26c7SHugh Dickins 	inode = igrab(&info->vfs_inode);
9092e0e26c7SHugh Dickins 	spin_unlock(&info->lock);
9102e0e26c7SHugh Dickins 
9111b1b32f2SHugh Dickins 	/*
9121b1b32f2SHugh Dickins 	 * Move _head_ to start search for next from here.
9131b1b32f2SHugh Dickins 	 * But be careful: shmem_delete_inode checks list_empty without taking
9141b1b32f2SHugh Dickins 	 * mutex, and there's an instant in list_move_tail when info->swaplist
9151b1b32f2SHugh Dickins 	 * would appear empty, if it were the only one on shmem_swaplist.  We
9161b1b32f2SHugh Dickins 	 * could avoid doing it if inode NULL; or use this minor optimization.
9171b1b32f2SHugh Dickins 	 */
9181b1b32f2SHugh Dickins 	if (shmem_swaplist.next != &info->swaplist)
9192e0e26c7SHugh Dickins 		list_move_tail(&shmem_swaplist, &info->swaplist);
9202e0e26c7SHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
9212e0e26c7SHugh Dickins 
9222e0e26c7SHugh Dickins 	error = 1;
9232e0e26c7SHugh Dickins 	if (!inode)
9242e0e26c7SHugh Dickins 		goto out;
92582369553SHugh Dickins 	/* Precharge page while we can wait, compensate afterwards */
92682369553SHugh Dickins 	error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
927b409f9fcSHugh Dickins 	if (error)
928b409f9fcSHugh Dickins 		goto out;
92982369553SHugh Dickins 	error = radix_tree_preload(GFP_KERNEL);
93082369553SHugh Dickins 	if (error)
93182369553SHugh Dickins 		goto uncharge;
932b409f9fcSHugh Dickins 	error = 1;
9332e0e26c7SHugh Dickins 
9342e0e26c7SHugh Dickins 	spin_lock(&info->lock);
9352e0e26c7SHugh Dickins 	ptr = shmem_swp_entry(info, idx, NULL);
9362e0e26c7SHugh Dickins 	if (ptr && ptr->val == entry.val)
9372e0e26c7SHugh Dickins 		error = add_to_page_cache(page, inode->i_mapping,
938b409f9fcSHugh Dickins 						idx, GFP_NOWAIT);
939d9fe526aSHugh Dickins 	if (error == -EEXIST) {
940d9fe526aSHugh Dickins 		struct page *filepage = find_get_page(inode->i_mapping, idx);
9412e0e26c7SHugh Dickins 		error = 1;
942d9fe526aSHugh Dickins 		if (filepage) {
943d9fe526aSHugh Dickins 			/*
944d9fe526aSHugh Dickins 			 * There might be a more uptodate page coming down
945d9fe526aSHugh Dickins 			 * from a stacked writepage: forget our swappage if so.
946d9fe526aSHugh Dickins 			 */
947d9fe526aSHugh Dickins 			if (PageUptodate(filepage))
948d9fe526aSHugh Dickins 				error = 0;
949d9fe526aSHugh Dickins 			page_cache_release(filepage);
950d9fe526aSHugh Dickins 		}
951d9fe526aSHugh Dickins 	}
952d9fe526aSHugh Dickins 	if (!error) {
95373b1262fSHugh Dickins 		delete_from_swap_cache(page);
95473b1262fSHugh Dickins 		set_page_dirty(page);
9551da177e4SLinus Torvalds 		info->flags |= SHMEM_PAGEIN;
9562e0e26c7SHugh Dickins 		shmem_swp_set(info, ptr, 0);
9572e0e26c7SHugh Dickins 		swap_free(entry);
9582e0e26c7SHugh Dickins 		error = 1;	/* not an error, but entry was found */
9591da177e4SLinus Torvalds 	}
9602e0e26c7SHugh Dickins 	if (ptr)
9611da177e4SLinus Torvalds 		shmem_swp_unmap(ptr);
9621da177e4SLinus Torvalds 	spin_unlock(&info->lock);
963b409f9fcSHugh Dickins 	radix_tree_preload_end();
96482369553SHugh Dickins uncharge:
96582369553SHugh Dickins 	mem_cgroup_uncharge_page(page);
9662e0e26c7SHugh Dickins out:
9672e0e26c7SHugh Dickins 	unlock_page(page);
9682e0e26c7SHugh Dickins 	page_cache_release(page);
9692e0e26c7SHugh Dickins 	iput(inode);		/* allows for NULL */
9702e0e26c7SHugh Dickins 	return error;
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds /*
9741da177e4SLinus Torvalds  * shmem_unuse() search for an eventually swapped out shmem page.
9751da177e4SLinus Torvalds  */
9761da177e4SLinus Torvalds int shmem_unuse(swp_entry_t entry, struct page *page)
9771da177e4SLinus Torvalds {
9781da177e4SLinus Torvalds 	struct list_head *p, *next;
9791da177e4SLinus Torvalds 	struct shmem_inode_info *info;
9801da177e4SLinus Torvalds 	int found = 0;
9811da177e4SLinus Torvalds 
982cb5f7b9aSHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
9831da177e4SLinus Torvalds 	list_for_each_safe(p, next, &shmem_swaplist) {
9841da177e4SLinus Torvalds 		info = list_entry(p, struct shmem_inode_info, swaplist);
9852e0e26c7SHugh Dickins 		found = shmem_unuse_inode(info, entry, page);
986cb5f7b9aSHugh Dickins 		cond_resched();
9872e0e26c7SHugh Dickins 		if (found)
9882e0e26c7SHugh Dickins 			goto out;
9891da177e4SLinus Torvalds 	}
990cb5f7b9aSHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
9912e0e26c7SHugh Dickins out:	return found;	/* 0 or 1 or -ENOMEM */
9921da177e4SLinus Torvalds }
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds /*
9951da177e4SLinus Torvalds  * Move the page from the page cache to the swap cache.
9961da177e4SLinus Torvalds  */
9971da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc)
9981da177e4SLinus Torvalds {
9991da177e4SLinus Torvalds 	struct shmem_inode_info *info;
10001da177e4SLinus Torvalds 	swp_entry_t *entry, swap;
10011da177e4SLinus Torvalds 	struct address_space *mapping;
10021da177e4SLinus Torvalds 	unsigned long index;
10031da177e4SLinus Torvalds 	struct inode *inode;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
10061da177e4SLinus Torvalds 	mapping = page->mapping;
10071da177e4SLinus Torvalds 	index = page->index;
10081da177e4SLinus Torvalds 	inode = mapping->host;
10091da177e4SLinus Torvalds 	info = SHMEM_I(inode);
10101da177e4SLinus Torvalds 	if (info->flags & VM_LOCKED)
10111da177e4SLinus Torvalds 		goto redirty;
1012d9fe526aSHugh Dickins 	if (!total_swap_pages)
10131da177e4SLinus Torvalds 		goto redirty;
10141da177e4SLinus Torvalds 
1015d9fe526aSHugh Dickins 	/*
1016d9fe526aSHugh Dickins 	 * shmem_backing_dev_info's capabilities prevent regular writeback or
1017d9fe526aSHugh Dickins 	 * sync from ever calling shmem_writepage; but a stacking filesystem
1018d9fe526aSHugh Dickins 	 * may use the ->writepage of its underlying filesystem, in which case
1019d9fe526aSHugh Dickins 	 * tmpfs should write out to swap only in response to memory pressure,
1020d9fe526aSHugh Dickins 	 * and not for pdflush or sync.  However, in those cases, we do still
1021d9fe526aSHugh Dickins 	 * want to check if there's a redundant swappage to be discarded.
1022d9fe526aSHugh Dickins 	 */
1023d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
1024d9fe526aSHugh Dickins 		swap = get_swap_page();
1025d9fe526aSHugh Dickins 	else
1026d9fe526aSHugh Dickins 		swap.val = 0;
1027d9fe526aSHugh Dickins 
10281da177e4SLinus Torvalds 	spin_lock(&info->lock);
10291da177e4SLinus Torvalds 	if (index >= info->next_index) {
10301da177e4SLinus Torvalds 		BUG_ON(!(info->flags & SHMEM_TRUNCATE));
10311da177e4SLinus Torvalds 		goto unlock;
10321da177e4SLinus Torvalds 	}
10331da177e4SLinus Torvalds 	entry = shmem_swp_entry(info, index, NULL);
1034d9fe526aSHugh Dickins 	if (entry->val) {
1035d9fe526aSHugh Dickins 		/*
1036d9fe526aSHugh Dickins 		 * The more uptodate page coming down from a stacked
1037d9fe526aSHugh Dickins 		 * writepage should replace our old swappage.
1038d9fe526aSHugh Dickins 		 */
1039d9fe526aSHugh Dickins 		free_swap_and_cache(*entry);
1040d9fe526aSHugh Dickins 		shmem_swp_set(info, entry, 0);
1041d9fe526aSHugh Dickins 	}
1042d9fe526aSHugh Dickins 	shmem_recalc_inode(inode);
10431da177e4SLinus Torvalds 
1044d9fe526aSHugh Dickins 	if (swap.val && add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
104573b1262fSHugh Dickins 		remove_from_page_cache(page);
10461da177e4SLinus Torvalds 		shmem_swp_set(info, entry, swap.val);
10471da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
10481b1b32f2SHugh Dickins 		if (list_empty(&info->swaplist))
10491b1b32f2SHugh Dickins 			inode = igrab(inode);
10501b1b32f2SHugh Dickins 		else
10511b1b32f2SHugh Dickins 			inode = NULL;
10521da177e4SLinus Torvalds 		spin_unlock(&info->lock);
105373b1262fSHugh Dickins 		swap_duplicate(swap);
1054d9fe526aSHugh Dickins 		BUG_ON(page_mapped(page));
105573b1262fSHugh Dickins 		page_cache_release(page);	/* pagecache ref */
105673b1262fSHugh Dickins 		set_page_dirty(page);
10571da177e4SLinus Torvalds 		unlock_page(page);
10581b1b32f2SHugh Dickins 		if (inode) {
10591b1b32f2SHugh Dickins 			mutex_lock(&shmem_swaplist_mutex);
10601b1b32f2SHugh Dickins 			/* move instead of add in case we're racing */
10611b1b32f2SHugh Dickins 			list_move_tail(&info->swaplist, &shmem_swaplist);
10621b1b32f2SHugh Dickins 			mutex_unlock(&shmem_swaplist_mutex);
10631b1b32f2SHugh Dickins 			iput(inode);
10641b1b32f2SHugh Dickins 		}
10651da177e4SLinus Torvalds 		return 0;
10661da177e4SLinus Torvalds 	}
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	shmem_swp_unmap(entry);
10691da177e4SLinus Torvalds unlock:
10701da177e4SLinus Torvalds 	spin_unlock(&info->lock);
10711da177e4SLinus Torvalds 	swap_free(swap);
10721da177e4SLinus Torvalds redirty:
10731da177e4SLinus Torvalds 	set_page_dirty(page);
1074d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
1075d9fe526aSHugh Dickins 		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
1076d9fe526aSHugh Dickins 	unlock_page(page);
1077d9fe526aSHugh Dickins 	return 0;
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1081680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
1082*71fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1083680d794bSakpm@linux-foundation.org {
1084680d794bSakpm@linux-foundation.org 	char buffer[64];
1085680d794bSakpm@linux-foundation.org 
1086*71fe804bSLee Schermerhorn 	if (!mpol || mpol->mode == MPOL_DEFAULT)
1087095f1fc4SLee Schermerhorn 		return;		/* show nothing */
1088095f1fc4SLee Schermerhorn 
1089*71fe804bSLee Schermerhorn 	mpol_to_str(buffer, sizeof(buffer), mpol, 1);
1090095f1fc4SLee Schermerhorn 
1091095f1fc4SLee Schermerhorn 	seq_printf(seq, ",mpol=%s", buffer);
1092680d794bSakpm@linux-foundation.org }
1093*71fe804bSLee Schermerhorn 
1094*71fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1095*71fe804bSLee Schermerhorn {
1096*71fe804bSLee Schermerhorn 	struct mempolicy *mpol = NULL;
1097*71fe804bSLee Schermerhorn 	if (sbinfo->mpol) {
1098*71fe804bSLee Schermerhorn 		spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
1099*71fe804bSLee Schermerhorn 		mpol = sbinfo->mpol;
1100*71fe804bSLee Schermerhorn 		mpol_get(mpol);
1101*71fe804bSLee Schermerhorn 		spin_unlock(&sbinfo->stat_lock);
1102*71fe804bSLee Schermerhorn 	}
1103*71fe804bSLee Schermerhorn 	return mpol;
1104*71fe804bSLee Schermerhorn }
1105680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
1106680d794bSakpm@linux-foundation.org 
110702098feaSHugh Dickins static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
110802098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11091da177e4SLinus Torvalds {
111052cd3b07SLee Schermerhorn 	struct mempolicy mpol, *spol;
11111da177e4SLinus Torvalds 	struct vm_area_struct pvma;
1112c4cc6d07SHugh Dickins 	struct page *page;
11131da177e4SLinus Torvalds 
111452cd3b07SLee Schermerhorn 	spol = mpol_cond_copy(&mpol,
111552cd3b07SLee Schermerhorn 				mpol_shared_policy_lookup(&info->policy, idx));
111652cd3b07SLee Schermerhorn 
11171da177e4SLinus Torvalds 	/* Create a pseudo vma that just contains the policy */
1118c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
11191da177e4SLinus Torvalds 	pvma.vm_pgoff = idx;
1120c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
112152cd3b07SLee Schermerhorn 	pvma.vm_policy = spol;
112202098feaSHugh Dickins 	page = swapin_readahead(entry, gfp, &pvma, 0);
11231da177e4SLinus Torvalds 	return page;
11241da177e4SLinus Torvalds }
11251da177e4SLinus Torvalds 
112602098feaSHugh Dickins static struct page *shmem_alloc_page(gfp_t gfp,
112702098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11281da177e4SLinus Torvalds {
11291da177e4SLinus Torvalds 	struct vm_area_struct pvma;
11301da177e4SLinus Torvalds 
1131c4cc6d07SHugh Dickins 	/* Create a pseudo vma that just contains the policy */
1132c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
11331da177e4SLinus Torvalds 	pvma.vm_pgoff = idx;
1134c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
1135c4cc6d07SHugh Dickins 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
113652cd3b07SLee Schermerhorn 
113752cd3b07SLee Schermerhorn 	/*
113852cd3b07SLee Schermerhorn 	 * alloc_page_vma() will drop the shared policy reference
113952cd3b07SLee Schermerhorn 	 */
114052cd3b07SLee Schermerhorn 	return alloc_page_vma(gfp, &pvma, 0);
11411da177e4SLinus Torvalds }
1142680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */
1143680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
1144*71fe804bSLee Schermerhorn static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *p)
1145680d794bSakpm@linux-foundation.org {
1146680d794bSakpm@linux-foundation.org }
1147680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
1148680d794bSakpm@linux-foundation.org 
114902098feaSHugh Dickins static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
115002098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11511da177e4SLinus Torvalds {
115202098feaSHugh Dickins 	return swapin_readahead(entry, gfp, NULL, 0);
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
115502098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp,
115602098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11571da177e4SLinus Torvalds {
1158e84e2e13SHugh Dickins 	return alloc_page(gfp);
11591da177e4SLinus Torvalds }
1160680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */
11611da177e4SLinus Torvalds 
1162*71fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
1163*71fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1164*71fe804bSLee Schermerhorn {
1165*71fe804bSLee Schermerhorn 	return NULL;
1166*71fe804bSLee Schermerhorn }
1167*71fe804bSLee Schermerhorn #endif
1168*71fe804bSLee Schermerhorn 
11691da177e4SLinus Torvalds /*
11701da177e4SLinus Torvalds  * shmem_getpage - either get the page from swap or allocate a new one
11711da177e4SLinus Torvalds  *
11721da177e4SLinus Torvalds  * If we allocate a new one we do not mark it dirty. That's up to the
11731da177e4SLinus Torvalds  * vm. If we swap it in we mark it dirty since we also free the swap
11741da177e4SLinus Torvalds  * entry since a page cannot live in both the swap and page cache
11751da177e4SLinus Torvalds  */
11761da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx,
11771da177e4SLinus Torvalds 			struct page **pagep, enum sgp_type sgp, int *type)
11781da177e4SLinus Torvalds {
11791da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
11801da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
11811da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo;
11821da177e4SLinus Torvalds 	struct page *filepage = *pagep;
11831da177e4SLinus Torvalds 	struct page *swappage;
11841da177e4SLinus Torvalds 	swp_entry_t *entry;
11851da177e4SLinus Torvalds 	swp_entry_t swap;
118602098feaSHugh Dickins 	gfp_t gfp;
11871da177e4SLinus Torvalds 	int error;
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	if (idx >= SHMEM_MAX_INDEX)
11901da177e4SLinus Torvalds 		return -EFBIG;
119154cb8821SNick Piggin 
119254cb8821SNick Piggin 	if (type)
119383c54070SNick Piggin 		*type = 0;
119454cb8821SNick Piggin 
11951da177e4SLinus Torvalds 	/*
11961da177e4SLinus Torvalds 	 * Normally, filepage is NULL on entry, and either found
11971da177e4SLinus Torvalds 	 * uptodate immediately, or allocated and zeroed, or read
11981da177e4SLinus Torvalds 	 * in under swappage, which is then assigned to filepage.
11995402b976SHugh Dickins 	 * But shmem_readpage (required for splice) passes in a locked
1200ae976416SHugh Dickins 	 * filepage, which may be found not uptodate by other callers
1201ae976416SHugh Dickins 	 * too, and may need to be copied from the swappage read in.
12021da177e4SLinus Torvalds 	 */
12031da177e4SLinus Torvalds repeat:
12041da177e4SLinus Torvalds 	if (!filepage)
12051da177e4SLinus Torvalds 		filepage = find_lock_page(mapping, idx);
12061da177e4SLinus Torvalds 	if (filepage && PageUptodate(filepage))
12071da177e4SLinus Torvalds 		goto done;
12081da177e4SLinus Torvalds 	error = 0;
120902098feaSHugh Dickins 	gfp = mapping_gfp_mask(mapping);
1210b409f9fcSHugh Dickins 	if (!filepage) {
1211b409f9fcSHugh Dickins 		/*
1212b409f9fcSHugh Dickins 		 * Try to preload while we can wait, to not make a habit of
1213b409f9fcSHugh Dickins 		 * draining atomic reserves; but don't latch on to this cpu.
1214b409f9fcSHugh Dickins 		 */
1215b409f9fcSHugh Dickins 		error = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
1216b409f9fcSHugh Dickins 		if (error)
1217b409f9fcSHugh Dickins 			goto failed;
1218b409f9fcSHugh Dickins 		radix_tree_preload_end();
1219b409f9fcSHugh Dickins 	}
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	spin_lock(&info->lock);
12221da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
12231da177e4SLinus Torvalds 	entry = shmem_swp_alloc(info, idx, sgp);
12241da177e4SLinus Torvalds 	if (IS_ERR(entry)) {
12251da177e4SLinus Torvalds 		spin_unlock(&info->lock);
12261da177e4SLinus Torvalds 		error = PTR_ERR(entry);
12271da177e4SLinus Torvalds 		goto failed;
12281da177e4SLinus Torvalds 	}
12291da177e4SLinus Torvalds 	swap = *entry;
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	if (swap.val) {
12321da177e4SLinus Torvalds 		/* Look it up and read it in.. */
12331da177e4SLinus Torvalds 		swappage = lookup_swap_cache(swap);
12341da177e4SLinus Torvalds 		if (!swappage) {
12351da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
12361da177e4SLinus Torvalds 			/* here we actually do the io */
123783c54070SNick Piggin 			if (type && !(*type & VM_FAULT_MAJOR)) {
1238f8891e5eSChristoph Lameter 				__count_vm_event(PGMAJFAULT);
123983c54070SNick Piggin 				*type |= VM_FAULT_MAJOR;
12401da177e4SLinus Torvalds 			}
1241f8891e5eSChristoph Lameter 			spin_unlock(&info->lock);
124202098feaSHugh Dickins 			swappage = shmem_swapin(swap, gfp, info, idx);
12431da177e4SLinus Torvalds 			if (!swappage) {
12441da177e4SLinus Torvalds 				spin_lock(&info->lock);
12451da177e4SLinus Torvalds 				entry = shmem_swp_alloc(info, idx, sgp);
12461da177e4SLinus Torvalds 				if (IS_ERR(entry))
12471da177e4SLinus Torvalds 					error = PTR_ERR(entry);
12481da177e4SLinus Torvalds 				else {
12491da177e4SLinus Torvalds 					if (entry->val == swap.val)
12501da177e4SLinus Torvalds 						error = -ENOMEM;
12511da177e4SLinus Torvalds 					shmem_swp_unmap(entry);
12521da177e4SLinus Torvalds 				}
12531da177e4SLinus Torvalds 				spin_unlock(&info->lock);
12541da177e4SLinus Torvalds 				if (error)
12551da177e4SLinus Torvalds 					goto failed;
12561da177e4SLinus Torvalds 				goto repeat;
12571da177e4SLinus Torvalds 			}
12581da177e4SLinus Torvalds 			wait_on_page_locked(swappage);
12591da177e4SLinus Torvalds 			page_cache_release(swappage);
12601da177e4SLinus Torvalds 			goto repeat;
12611da177e4SLinus Torvalds 		}
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 		/* We have to do this with page locked to prevent races */
12641da177e4SLinus Torvalds 		if (TestSetPageLocked(swappage)) {
12651da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
12661da177e4SLinus Torvalds 			spin_unlock(&info->lock);
12671da177e4SLinus Torvalds 			wait_on_page_locked(swappage);
12681da177e4SLinus Torvalds 			page_cache_release(swappage);
12691da177e4SLinus Torvalds 			goto repeat;
12701da177e4SLinus Torvalds 		}
12711da177e4SLinus Torvalds 		if (PageWriteback(swappage)) {
12721da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
12731da177e4SLinus Torvalds 			spin_unlock(&info->lock);
12741da177e4SLinus Torvalds 			wait_on_page_writeback(swappage);
12751da177e4SLinus Torvalds 			unlock_page(swappage);
12761da177e4SLinus Torvalds 			page_cache_release(swappage);
12771da177e4SLinus Torvalds 			goto repeat;
12781da177e4SLinus Torvalds 		}
12791da177e4SLinus Torvalds 		if (!PageUptodate(swappage)) {
12801da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
12811da177e4SLinus Torvalds 			spin_unlock(&info->lock);
12821da177e4SLinus Torvalds 			unlock_page(swappage);
12831da177e4SLinus Torvalds 			page_cache_release(swappage);
12841da177e4SLinus Torvalds 			error = -EIO;
12851da177e4SLinus Torvalds 			goto failed;
12861da177e4SLinus Torvalds 		}
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 		if (filepage) {
12891da177e4SLinus Torvalds 			shmem_swp_set(info, entry, 0);
12901da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
12911da177e4SLinus Torvalds 			delete_from_swap_cache(swappage);
12921da177e4SLinus Torvalds 			spin_unlock(&info->lock);
12931da177e4SLinus Torvalds 			copy_highpage(filepage, swappage);
12941da177e4SLinus Torvalds 			unlock_page(swappage);
12951da177e4SLinus Torvalds 			page_cache_release(swappage);
12961da177e4SLinus Torvalds 			flush_dcache_page(filepage);
12971da177e4SLinus Torvalds 			SetPageUptodate(filepage);
12981da177e4SLinus Torvalds 			set_page_dirty(filepage);
12991da177e4SLinus Torvalds 			swap_free(swap);
130073b1262fSHugh Dickins 		} else if (!(error = add_to_page_cache(
1301b409f9fcSHugh Dickins 				swappage, mapping, idx, GFP_NOWAIT))) {
13021da177e4SLinus Torvalds 			info->flags |= SHMEM_PAGEIN;
13031da177e4SLinus Torvalds 			shmem_swp_set(info, entry, 0);
13041da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
130573b1262fSHugh Dickins 			delete_from_swap_cache(swappage);
13061da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13071da177e4SLinus Torvalds 			filepage = swappage;
130873b1262fSHugh Dickins 			set_page_dirty(filepage);
13091da177e4SLinus Torvalds 			swap_free(swap);
13101da177e4SLinus Torvalds 		} else {
13111da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13121da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13131da177e4SLinus Torvalds 			unlock_page(swappage);
131482369553SHugh Dickins 			if (error == -ENOMEM) {
131582369553SHugh Dickins 				/* allow reclaim from this memory cgroup */
13167e924aafSHugh Dickins 				error = mem_cgroup_cache_charge(swappage,
131782369553SHugh Dickins 					current->mm, gfp & ~__GFP_HIGHMEM);
13187e924aafSHugh Dickins 				if (error) {
13197e924aafSHugh Dickins 					page_cache_release(swappage);
132082369553SHugh Dickins 					goto failed;
132182369553SHugh Dickins 				}
13227e924aafSHugh Dickins 				mem_cgroup_uncharge_page(swappage);
13237e924aafSHugh Dickins 			}
13247e924aafSHugh Dickins 			page_cache_release(swappage);
13251da177e4SLinus Torvalds 			goto repeat;
13261da177e4SLinus Torvalds 		}
13271da177e4SLinus Torvalds 	} else if (sgp == SGP_READ && !filepage) {
13281da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
13291da177e4SLinus Torvalds 		filepage = find_get_page(mapping, idx);
13301da177e4SLinus Torvalds 		if (filepage &&
13311da177e4SLinus Torvalds 		    (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
13321da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13331da177e4SLinus Torvalds 			wait_on_page_locked(filepage);
13341da177e4SLinus Torvalds 			page_cache_release(filepage);
13351da177e4SLinus Torvalds 			filepage = NULL;
13361da177e4SLinus Torvalds 			goto repeat;
13371da177e4SLinus Torvalds 		}
13381da177e4SLinus Torvalds 		spin_unlock(&info->lock);
13391da177e4SLinus Torvalds 	} else {
13401da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
13411da177e4SLinus Torvalds 		sbinfo = SHMEM_SB(inode->i_sb);
13420edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
13431da177e4SLinus Torvalds 			spin_lock(&sbinfo->stat_lock);
13441da177e4SLinus Torvalds 			if (sbinfo->free_blocks == 0 ||
13451da177e4SLinus Torvalds 			    shmem_acct_block(info->flags)) {
13461da177e4SLinus Torvalds 				spin_unlock(&sbinfo->stat_lock);
13471da177e4SLinus Torvalds 				spin_unlock(&info->lock);
13481da177e4SLinus Torvalds 				error = -ENOSPC;
13491da177e4SLinus Torvalds 				goto failed;
13501da177e4SLinus Torvalds 			}
13511da177e4SLinus Torvalds 			sbinfo->free_blocks--;
13521da177e4SLinus Torvalds 			inode->i_blocks += BLOCKS_PER_PAGE;
13531da177e4SLinus Torvalds 			spin_unlock(&sbinfo->stat_lock);
13541da177e4SLinus Torvalds 		} else if (shmem_acct_block(info->flags)) {
13551da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13561da177e4SLinus Torvalds 			error = -ENOSPC;
13571da177e4SLinus Torvalds 			goto failed;
13581da177e4SLinus Torvalds 		}
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 		if (!filepage) {
13611da177e4SLinus Torvalds 			spin_unlock(&info->lock);
136202098feaSHugh Dickins 			filepage = shmem_alloc_page(gfp, info, idx);
13631da177e4SLinus Torvalds 			if (!filepage) {
13641da177e4SLinus Torvalds 				shmem_unacct_blocks(info->flags, 1);
13651da177e4SLinus Torvalds 				shmem_free_blocks(inode, 1);
13661da177e4SLinus Torvalds 				error = -ENOMEM;
13671da177e4SLinus Torvalds 				goto failed;
13681da177e4SLinus Torvalds 			}
13691da177e4SLinus Torvalds 
137082369553SHugh Dickins 			/* Precharge page while we can wait, compensate after */
137182369553SHugh Dickins 			error = mem_cgroup_cache_charge(filepage, current->mm,
137282369553SHugh Dickins 							gfp & ~__GFP_HIGHMEM);
137382369553SHugh Dickins 			if (error) {
137482369553SHugh Dickins 				page_cache_release(filepage);
137582369553SHugh Dickins 				shmem_unacct_blocks(info->flags, 1);
137682369553SHugh Dickins 				shmem_free_blocks(inode, 1);
137782369553SHugh Dickins 				filepage = NULL;
137882369553SHugh Dickins 				goto failed;
137982369553SHugh Dickins 			}
138082369553SHugh Dickins 
13811da177e4SLinus Torvalds 			spin_lock(&info->lock);
13821da177e4SLinus Torvalds 			entry = shmem_swp_alloc(info, idx, sgp);
13831da177e4SLinus Torvalds 			if (IS_ERR(entry))
13841da177e4SLinus Torvalds 				error = PTR_ERR(entry);
13851da177e4SLinus Torvalds 			else {
13861da177e4SLinus Torvalds 				swap = *entry;
13871da177e4SLinus Torvalds 				shmem_swp_unmap(entry);
13881da177e4SLinus Torvalds 			}
13891da177e4SLinus Torvalds 			if (error || swap.val || 0 != add_to_page_cache_lru(
1390b409f9fcSHugh Dickins 					filepage, mapping, idx, GFP_NOWAIT)) {
13911da177e4SLinus Torvalds 				spin_unlock(&info->lock);
139282369553SHugh Dickins 				mem_cgroup_uncharge_page(filepage);
13931da177e4SLinus Torvalds 				page_cache_release(filepage);
13941da177e4SLinus Torvalds 				shmem_unacct_blocks(info->flags, 1);
13951da177e4SLinus Torvalds 				shmem_free_blocks(inode, 1);
13961da177e4SLinus Torvalds 				filepage = NULL;
13971da177e4SLinus Torvalds 				if (error)
13981da177e4SLinus Torvalds 					goto failed;
13991da177e4SLinus Torvalds 				goto repeat;
14001da177e4SLinus Torvalds 			}
140182369553SHugh Dickins 			mem_cgroup_uncharge_page(filepage);
14021da177e4SLinus Torvalds 			info->flags |= SHMEM_PAGEIN;
14031da177e4SLinus Torvalds 		}
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 		info->alloced++;
14061da177e4SLinus Torvalds 		spin_unlock(&info->lock);
1407e84e2e13SHugh Dickins 		clear_highpage(filepage);
14081da177e4SLinus Torvalds 		flush_dcache_page(filepage);
14091da177e4SLinus Torvalds 		SetPageUptodate(filepage);
1410a0ee5ec5SHugh Dickins 		if (sgp == SGP_DIRTY)
1411a0ee5ec5SHugh Dickins 			set_page_dirty(filepage);
14121da177e4SLinus Torvalds 	}
14131da177e4SLinus Torvalds done:
14141da177e4SLinus Torvalds 	*pagep = filepage;
14151da177e4SLinus Torvalds 	return 0;
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds failed:
14181da177e4SLinus Torvalds 	if (*pagep != filepage) {
14191da177e4SLinus Torvalds 		unlock_page(filepage);
14201da177e4SLinus Torvalds 		page_cache_release(filepage);
14211da177e4SLinus Torvalds 	}
14221da177e4SLinus Torvalds 	return error;
14231da177e4SLinus Torvalds }
14241da177e4SLinus Torvalds 
1425d0217ac0SNick Piggin static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
14261da177e4SLinus Torvalds {
1427d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
14281da177e4SLinus Torvalds 	int error;
1429d0217ac0SNick Piggin 	int ret;
14301da177e4SLinus Torvalds 
1431d0217ac0SNick Piggin 	if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1432d0217ac0SNick Piggin 		return VM_FAULT_SIGBUS;
1433d00806b1SNick Piggin 
143427d54b39SHugh Dickins 	error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1435d0217ac0SNick Piggin 	if (error)
1436d0217ac0SNick Piggin 		return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
14371da177e4SLinus Torvalds 
1438d0217ac0SNick Piggin 	mark_page_accessed(vmf->page);
143983c54070SNick Piggin 	return ret | VM_FAULT_LOCKED;
14401da177e4SLinus Torvalds }
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1443d8dc74f2SAdrian Bunk static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
14441da177e4SLinus Torvalds {
1445d3ac7f89SJosef "Jeff" Sipek 	struct inode *i = vma->vm_file->f_path.dentry->d_inode;
14461da177e4SLinus Torvalds 	return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds 
1449d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1450d8dc74f2SAdrian Bunk 					  unsigned long addr)
14511da177e4SLinus Torvalds {
1452d3ac7f89SJosef "Jeff" Sipek 	struct inode *i = vma->vm_file->f_path.dentry->d_inode;
14531da177e4SLinus Torvalds 	unsigned long idx;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
14561da177e4SLinus Torvalds 	return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
14571da177e4SLinus Torvalds }
14581da177e4SLinus Torvalds #endif
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user)
14611da177e4SLinus Torvalds {
1462d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
14631da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
14641da177e4SLinus Torvalds 	int retval = -ENOMEM;
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 	spin_lock(&info->lock);
14671da177e4SLinus Torvalds 	if (lock && !(info->flags & VM_LOCKED)) {
14681da177e4SLinus Torvalds 		if (!user_shm_lock(inode->i_size, user))
14691da177e4SLinus Torvalds 			goto out_nomem;
14701da177e4SLinus Torvalds 		info->flags |= VM_LOCKED;
14711da177e4SLinus Torvalds 	}
14721da177e4SLinus Torvalds 	if (!lock && (info->flags & VM_LOCKED) && user) {
14731da177e4SLinus Torvalds 		user_shm_unlock(inode->i_size, user);
14741da177e4SLinus Torvalds 		info->flags &= ~VM_LOCKED;
14751da177e4SLinus Torvalds 	}
14761da177e4SLinus Torvalds 	retval = 0;
14771da177e4SLinus Torvalds out_nomem:
14781da177e4SLinus Torvalds 	spin_unlock(&info->lock);
14791da177e4SLinus Torvalds 	return retval;
14801da177e4SLinus Torvalds }
14811da177e4SLinus Torvalds 
14829b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
14831da177e4SLinus Torvalds {
14841da177e4SLinus Torvalds 	file_accessed(file);
14851da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
1486d0217ac0SNick Piggin 	vma->vm_flags |= VM_CAN_NONLINEAR;
14871da177e4SLinus Torvalds 	return 0;
14881da177e4SLinus Torvalds }
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds static struct inode *
14911da177e4SLinus Torvalds shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
14921da177e4SLinus Torvalds {
14931da177e4SLinus Torvalds 	struct inode *inode;
14941da177e4SLinus Torvalds 	struct shmem_inode_info *info;
14951da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
14961da177e4SLinus Torvalds 
14975b04c689SPavel Emelyanov 	if (shmem_reserve_inode(sb))
14981da177e4SLinus Torvalds 		return NULL;
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 	inode = new_inode(sb);
15011da177e4SLinus Torvalds 	if (inode) {
15021da177e4SLinus Torvalds 		inode->i_mode = mode;
15031da177e4SLinus Torvalds 		inode->i_uid = current->fsuid;
15041da177e4SLinus Torvalds 		inode->i_gid = current->fsgid;
15051da177e4SLinus Torvalds 		inode->i_blocks = 0;
15061da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &shmem_aops;
15071da177e4SLinus Torvalds 		inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
15081da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
150991828a40SDavid M. Grimes 		inode->i_generation = get_seconds();
15101da177e4SLinus Torvalds 		info = SHMEM_I(inode);
15111da177e4SLinus Torvalds 		memset(info, 0, (char *)inode - (char *)info);
15121da177e4SLinus Torvalds 		spin_lock_init(&info->lock);
15131da177e4SLinus Torvalds 		INIT_LIST_HEAD(&info->swaplist);
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
15161da177e4SLinus Torvalds 		default:
151739f0247dSAndreas Gruenbacher 			inode->i_op = &shmem_special_inode_operations;
15181da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
15191da177e4SLinus Torvalds 			break;
15201da177e4SLinus Torvalds 		case S_IFREG:
15211da177e4SLinus Torvalds 			inode->i_op = &shmem_inode_operations;
15221da177e4SLinus Torvalds 			inode->i_fop = &shmem_file_operations;
1523*71fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy,
1524*71fe804bSLee Schermerhorn 						 shmem_get_sbmpol(sbinfo));
15251da177e4SLinus Torvalds 			break;
15261da177e4SLinus Torvalds 		case S_IFDIR:
1527d8c76e6fSDave Hansen 			inc_nlink(inode);
15281da177e4SLinus Torvalds 			/* Some things misbehave if size == 0 on a directory */
15291da177e4SLinus Torvalds 			inode->i_size = 2 * BOGO_DIRENT_SIZE;
15301da177e4SLinus Torvalds 			inode->i_op = &shmem_dir_inode_operations;
15311da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
15321da177e4SLinus Torvalds 			break;
15331da177e4SLinus Torvalds 		case S_IFLNK:
15341da177e4SLinus Torvalds 			/*
15351da177e4SLinus Torvalds 			 * Must not load anything in the rbtree,
15361da177e4SLinus Torvalds 			 * mpol_free_shared_policy will not be called.
15371da177e4SLinus Torvalds 			 */
1538*71fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy, NULL);
15391da177e4SLinus Torvalds 			break;
15401da177e4SLinus Torvalds 		}
15415b04c689SPavel Emelyanov 	} else
15425b04c689SPavel Emelyanov 		shmem_free_inode(sb);
15431da177e4SLinus Torvalds 	return inode;
15441da177e4SLinus Torvalds }
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
154792e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations;
154892e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds /*
1551800d15a5SNick Piggin  * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
1552ae976416SHugh Dickins  * but providing them allows a tmpfs file to be used for splice, sendfile, and
1553ae976416SHugh Dickins  * below the loop driver, in the generic fashion that many filesystems support.
15541da177e4SLinus Torvalds  */
1555ae976416SHugh Dickins static int shmem_readpage(struct file *file, struct page *page)
1556ae976416SHugh Dickins {
1557ae976416SHugh Dickins 	struct inode *inode = page->mapping->host;
1558ae976416SHugh Dickins 	int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
1559ae976416SHugh Dickins 	unlock_page(page);
1560ae976416SHugh Dickins 	return error;
1561ae976416SHugh Dickins }
1562ae976416SHugh Dickins 
15631da177e4SLinus Torvalds static int
1564800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping,
1565800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
1566800d15a5SNick Piggin 			struct page **pagep, void **fsdata)
15671da177e4SLinus Torvalds {
1568800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1569800d15a5SNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1570800d15a5SNick Piggin 	*pagep = NULL;
1571800d15a5SNick Piggin 	return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1572800d15a5SNick Piggin }
1573800d15a5SNick Piggin 
1574800d15a5SNick Piggin static int
1575800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping,
1576800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
1577800d15a5SNick Piggin 			struct page *page, void *fsdata)
1578800d15a5SNick Piggin {
1579800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1580800d15a5SNick Piggin 
1581800d15a5SNick Piggin 	if (pos + copied > inode->i_size)
1582800d15a5SNick Piggin 		i_size_write(inode, pos + copied);
1583800d15a5SNick Piggin 
1584d3602444SHugh Dickins 	unlock_page(page);
1585d3602444SHugh Dickins 	set_page_dirty(page);
1586d3602444SHugh Dickins 	page_cache_release(page);
1587d3602444SHugh Dickins 
1588800d15a5SNick Piggin 	return copied;
15891da177e4SLinus Torvalds }
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
15921da177e4SLinus Torvalds {
1593d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
15941da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
15951da177e4SLinus Torvalds 	unsigned long index, offset;
1596a0ee5ec5SHugh Dickins 	enum sgp_type sgp = SGP_READ;
1597a0ee5ec5SHugh Dickins 
1598a0ee5ec5SHugh Dickins 	/*
1599a0ee5ec5SHugh Dickins 	 * Might this read be for a stacking filesystem?  Then when reading
1600a0ee5ec5SHugh Dickins 	 * holes of a sparse file, we actually need to allocate those pages,
1601a0ee5ec5SHugh Dickins 	 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1602a0ee5ec5SHugh Dickins 	 */
1603a0ee5ec5SHugh Dickins 	if (segment_eq(get_fs(), KERNEL_DS))
1604a0ee5ec5SHugh Dickins 		sgp = SGP_DIRTY;
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	index = *ppos >> PAGE_CACHE_SHIFT;
16071da177e4SLinus Torvalds 	offset = *ppos & ~PAGE_CACHE_MASK;
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 	for (;;) {
16101da177e4SLinus Torvalds 		struct page *page = NULL;
16111da177e4SLinus Torvalds 		unsigned long end_index, nr, ret;
16121da177e4SLinus Torvalds 		loff_t i_size = i_size_read(inode);
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
16151da177e4SLinus Torvalds 		if (index > end_index)
16161da177e4SLinus Torvalds 			break;
16171da177e4SLinus Torvalds 		if (index == end_index) {
16181da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
16191da177e4SLinus Torvalds 			if (nr <= offset)
16201da177e4SLinus Torvalds 				break;
16211da177e4SLinus Torvalds 		}
16221da177e4SLinus Torvalds 
1623a0ee5ec5SHugh Dickins 		desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
16241da177e4SLinus Torvalds 		if (desc->error) {
16251da177e4SLinus Torvalds 			if (desc->error == -EINVAL)
16261da177e4SLinus Torvalds 				desc->error = 0;
16271da177e4SLinus Torvalds 			break;
16281da177e4SLinus Torvalds 		}
1629d3602444SHugh Dickins 		if (page)
1630d3602444SHugh Dickins 			unlock_page(page);
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 		/*
16331da177e4SLinus Torvalds 		 * We must evaluate after, since reads (unlike writes)
16341b1dcc1bSJes Sorensen 		 * are called without i_mutex protection against truncate
16351da177e4SLinus Torvalds 		 */
16361da177e4SLinus Torvalds 		nr = PAGE_CACHE_SIZE;
16371da177e4SLinus Torvalds 		i_size = i_size_read(inode);
16381da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
16391da177e4SLinus Torvalds 		if (index == end_index) {
16401da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
16411da177e4SLinus Torvalds 			if (nr <= offset) {
16421da177e4SLinus Torvalds 				if (page)
16431da177e4SLinus Torvalds 					page_cache_release(page);
16441da177e4SLinus Torvalds 				break;
16451da177e4SLinus Torvalds 			}
16461da177e4SLinus Torvalds 		}
16471da177e4SLinus Torvalds 		nr -= offset;
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds 		if (page) {
16501da177e4SLinus Torvalds 			/*
16511da177e4SLinus Torvalds 			 * If users can be writing to this page using arbitrary
16521da177e4SLinus Torvalds 			 * virtual addresses, take care about potential aliasing
16531da177e4SLinus Torvalds 			 * before reading the page on the kernel side.
16541da177e4SLinus Torvalds 			 */
16551da177e4SLinus Torvalds 			if (mapping_writably_mapped(mapping))
16561da177e4SLinus Torvalds 				flush_dcache_page(page);
16571da177e4SLinus Torvalds 			/*
16581da177e4SLinus Torvalds 			 * Mark the page accessed if we read the beginning.
16591da177e4SLinus Torvalds 			 */
16601da177e4SLinus Torvalds 			if (!offset)
16611da177e4SLinus Torvalds 				mark_page_accessed(page);
1662b5810039SNick Piggin 		} else {
16631da177e4SLinus Torvalds 			page = ZERO_PAGE(0);
1664b5810039SNick Piggin 			page_cache_get(page);
1665b5810039SNick Piggin 		}
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 		/*
16681da177e4SLinus Torvalds 		 * Ok, we have the page, and it's up-to-date, so
16691da177e4SLinus Torvalds 		 * now we can copy it to user space...
16701da177e4SLinus Torvalds 		 *
16711da177e4SLinus Torvalds 		 * The actor routine returns how many bytes were actually used..
16721da177e4SLinus Torvalds 		 * NOTE! This may not be the same as how much of a user buffer
16731da177e4SLinus Torvalds 		 * we filled up (we may be padding etc), so we can only update
16741da177e4SLinus Torvalds 		 * "pos" here (the actor routine has to update the user buffer
16751da177e4SLinus Torvalds 		 * pointers and the remaining count).
16761da177e4SLinus Torvalds 		 */
16771da177e4SLinus Torvalds 		ret = actor(desc, page, offset, nr);
16781da177e4SLinus Torvalds 		offset += ret;
16791da177e4SLinus Torvalds 		index += offset >> PAGE_CACHE_SHIFT;
16801da177e4SLinus Torvalds 		offset &= ~PAGE_CACHE_MASK;
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 		page_cache_release(page);
16831da177e4SLinus Torvalds 		if (ret != nr || !desc->count)
16841da177e4SLinus Torvalds 			break;
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 		cond_resched();
16871da177e4SLinus Torvalds 	}
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
16901da177e4SLinus Torvalds 	file_accessed(filp);
16911da177e4SLinus Torvalds }
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
16941da177e4SLinus Torvalds {
16951da177e4SLinus Torvalds 	read_descriptor_t desc;
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 	if ((ssize_t) count < 0)
16981da177e4SLinus Torvalds 		return -EINVAL;
16991da177e4SLinus Torvalds 	if (!access_ok(VERIFY_WRITE, buf, count))
17001da177e4SLinus Torvalds 		return -EFAULT;
17011da177e4SLinus Torvalds 	if (!count)
17021da177e4SLinus Torvalds 		return 0;
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds 	desc.written = 0;
17051da177e4SLinus Torvalds 	desc.count = count;
17061da177e4SLinus Torvalds 	desc.arg.buf = buf;
17071da177e4SLinus Torvalds 	desc.error = 0;
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 	do_shmem_file_read(filp, ppos, &desc, file_read_actor);
17101da177e4SLinus Torvalds 	if (desc.written)
17111da177e4SLinus Torvalds 		return desc.written;
17121da177e4SLinus Torvalds 	return desc.error;
17131da177e4SLinus Torvalds }
17141da177e4SLinus Torvalds 
1715726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
17161da177e4SLinus Torvalds {
1717726c3342SDavid Howells 	struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 	buf->f_type = TMPFS_MAGIC;
17201da177e4SLinus Torvalds 	buf->f_bsize = PAGE_CACHE_SIZE;
17211da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
17221da177e4SLinus Torvalds 	spin_lock(&sbinfo->stat_lock);
17230edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
17241da177e4SLinus Torvalds 		buf->f_blocks = sbinfo->max_blocks;
17251da177e4SLinus Torvalds 		buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
17260edd73b3SHugh Dickins 	}
17270edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
17281da177e4SLinus Torvalds 		buf->f_files = sbinfo->max_inodes;
17291da177e4SLinus Torvalds 		buf->f_ffree = sbinfo->free_inodes;
17301da177e4SLinus Torvalds 	}
17311da177e4SLinus Torvalds 	/* else leave those fields 0 like simple_statfs */
17320edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
17331da177e4SLinus Torvalds 	return 0;
17341da177e4SLinus Torvalds }
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds /*
17371da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
17381da177e4SLinus Torvalds  */
17391da177e4SLinus Torvalds static int
17401da177e4SLinus Torvalds shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
17411da177e4SLinus Torvalds {
17421da177e4SLinus Torvalds 	struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
17431da177e4SLinus Torvalds 	int error = -ENOSPC;
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds 	if (inode) {
1746570bc1c2SStephen Smalley 		error = security_inode_init_security(inode, dir, NULL, NULL,
1747570bc1c2SStephen Smalley 						     NULL);
1748570bc1c2SStephen Smalley 		if (error) {
1749570bc1c2SStephen Smalley 			if (error != -EOPNOTSUPP) {
1750570bc1c2SStephen Smalley 				iput(inode);
1751570bc1c2SStephen Smalley 				return error;
1752570bc1c2SStephen Smalley 			}
175339f0247dSAndreas Gruenbacher 		}
175439f0247dSAndreas Gruenbacher 		error = shmem_acl_init(inode, dir);
175539f0247dSAndreas Gruenbacher 		if (error) {
175639f0247dSAndreas Gruenbacher 			iput(inode);
175739f0247dSAndreas Gruenbacher 			return error;
1758570bc1c2SStephen Smalley 		}
17591da177e4SLinus Torvalds 		if (dir->i_mode & S_ISGID) {
17601da177e4SLinus Torvalds 			inode->i_gid = dir->i_gid;
17611da177e4SLinus Torvalds 			if (S_ISDIR(mode))
17621da177e4SLinus Torvalds 				inode->i_mode |= S_ISGID;
17631da177e4SLinus Torvalds 		}
17641da177e4SLinus Torvalds 		dir->i_size += BOGO_DIRENT_SIZE;
17651da177e4SLinus Torvalds 		dir->i_ctime = dir->i_mtime = CURRENT_TIME;
17661da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
17671da177e4SLinus Torvalds 		dget(dentry); /* Extra count - pin the dentry in core */
17681da177e4SLinus Torvalds 	}
17691da177e4SLinus Torvalds 	return error;
17701da177e4SLinus Torvalds }
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
17731da177e4SLinus Torvalds {
17741da177e4SLinus Torvalds 	int error;
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
17771da177e4SLinus Torvalds 		return error;
1778d8c76e6fSDave Hansen 	inc_nlink(dir);
17791da177e4SLinus Torvalds 	return 0;
17801da177e4SLinus Torvalds }
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
17831da177e4SLinus Torvalds 		struct nameidata *nd)
17841da177e4SLinus Torvalds {
17851da177e4SLinus Torvalds 	return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
17861da177e4SLinus Torvalds }
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds /*
17891da177e4SLinus Torvalds  * Link a file..
17901da177e4SLinus Torvalds  */
17911da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
17921da177e4SLinus Torvalds {
17931da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
17945b04c689SPavel Emelyanov 	int ret;
17951da177e4SLinus Torvalds 
17961da177e4SLinus Torvalds 	/*
17971da177e4SLinus Torvalds 	 * No ordinary (disk based) filesystem counts links as inodes;
17981da177e4SLinus Torvalds 	 * but each new link needs a new dentry, pinning lowmem, and
17991da177e4SLinus Torvalds 	 * tmpfs dentries cannot be pruned until they are unlinked.
18001da177e4SLinus Torvalds 	 */
18015b04c689SPavel Emelyanov 	ret = shmem_reserve_inode(inode->i_sb);
18025b04c689SPavel Emelyanov 	if (ret)
18035b04c689SPavel Emelyanov 		goto out;
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
18061da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1807d8c76e6fSDave Hansen 	inc_nlink(inode);
18081da177e4SLinus Torvalds 	atomic_inc(&inode->i_count);	/* New dentry reference */
18091da177e4SLinus Torvalds 	dget(dentry);		/* Extra pinning count for the created dentry */
18101da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
18115b04c689SPavel Emelyanov out:
18125b04c689SPavel Emelyanov 	return ret;
18131da177e4SLinus Torvalds }
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry)
18161da177e4SLinus Torvalds {
18171da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
18181da177e4SLinus Torvalds 
18195b04c689SPavel Emelyanov 	if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
18205b04c689SPavel Emelyanov 		shmem_free_inode(inode->i_sb);
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 	dir->i_size -= BOGO_DIRENT_SIZE;
18231da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
18249a53c3a7SDave Hansen 	drop_nlink(inode);
18251da177e4SLinus Torvalds 	dput(dentry);	/* Undo the count from "create" - this does all the work */
18261da177e4SLinus Torvalds 	return 0;
18271da177e4SLinus Torvalds }
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
18301da177e4SLinus Torvalds {
18311da177e4SLinus Torvalds 	if (!simple_empty(dentry))
18321da177e4SLinus Torvalds 		return -ENOTEMPTY;
18331da177e4SLinus Torvalds 
18349a53c3a7SDave Hansen 	drop_nlink(dentry->d_inode);
18359a53c3a7SDave Hansen 	drop_nlink(dir);
18361da177e4SLinus Torvalds 	return shmem_unlink(dir, dentry);
18371da177e4SLinus Torvalds }
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds /*
18401da177e4SLinus Torvalds  * The VFS layer already does all the dentry stuff for rename,
18411da177e4SLinus Torvalds  * we just have to decrement the usage count for the target if
18421da177e4SLinus Torvalds  * it exists so that the VFS layer correctly free's it when it
18431da177e4SLinus Torvalds  * gets overwritten.
18441da177e4SLinus Torvalds  */
18451da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
18461da177e4SLinus Torvalds {
18471da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
18481da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(inode->i_mode);
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
18511da177e4SLinus Torvalds 		return -ENOTEMPTY;
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
18541da177e4SLinus Torvalds 		(void) shmem_unlink(new_dir, new_dentry);
18551da177e4SLinus Torvalds 		if (they_are_dirs)
18569a53c3a7SDave Hansen 			drop_nlink(old_dir);
18571da177e4SLinus Torvalds 	} else if (they_are_dirs) {
18589a53c3a7SDave Hansen 		drop_nlink(old_dir);
1859d8c76e6fSDave Hansen 		inc_nlink(new_dir);
18601da177e4SLinus Torvalds 	}
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 	old_dir->i_size -= BOGO_DIRENT_SIZE;
18631da177e4SLinus Torvalds 	new_dir->i_size += BOGO_DIRENT_SIZE;
18641da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime =
18651da177e4SLinus Torvalds 	new_dir->i_ctime = new_dir->i_mtime =
18661da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME;
18671da177e4SLinus Torvalds 	return 0;
18681da177e4SLinus Torvalds }
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
18711da177e4SLinus Torvalds {
18721da177e4SLinus Torvalds 	int error;
18731da177e4SLinus Torvalds 	int len;
18741da177e4SLinus Torvalds 	struct inode *inode;
18751da177e4SLinus Torvalds 	struct page *page = NULL;
18761da177e4SLinus Torvalds 	char *kaddr;
18771da177e4SLinus Torvalds 	struct shmem_inode_info *info;
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds 	len = strlen(symname) + 1;
18801da177e4SLinus Torvalds 	if (len > PAGE_CACHE_SIZE)
18811da177e4SLinus Torvalds 		return -ENAMETOOLONG;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 	inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
18841da177e4SLinus Torvalds 	if (!inode)
18851da177e4SLinus Torvalds 		return -ENOSPC;
18861da177e4SLinus Torvalds 
1887570bc1c2SStephen Smalley 	error = security_inode_init_security(inode, dir, NULL, NULL,
1888570bc1c2SStephen Smalley 					     NULL);
1889570bc1c2SStephen Smalley 	if (error) {
1890570bc1c2SStephen Smalley 		if (error != -EOPNOTSUPP) {
1891570bc1c2SStephen Smalley 			iput(inode);
1892570bc1c2SStephen Smalley 			return error;
1893570bc1c2SStephen Smalley 		}
1894570bc1c2SStephen Smalley 		error = 0;
1895570bc1c2SStephen Smalley 	}
1896570bc1c2SStephen Smalley 
18971da177e4SLinus Torvalds 	info = SHMEM_I(inode);
18981da177e4SLinus Torvalds 	inode->i_size = len-1;
18991da177e4SLinus Torvalds 	if (len <= (char *)inode - (char *)info) {
19001da177e4SLinus Torvalds 		/* do it inline */
19011da177e4SLinus Torvalds 		memcpy(info, symname, len);
19021da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inline_operations;
19031da177e4SLinus Torvalds 	} else {
19041da177e4SLinus Torvalds 		error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
19051da177e4SLinus Torvalds 		if (error) {
19061da177e4SLinus Torvalds 			iput(inode);
19071da177e4SLinus Torvalds 			return error;
19081da177e4SLinus Torvalds 		}
1909d3602444SHugh Dickins 		unlock_page(page);
19101da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inode_operations;
19111da177e4SLinus Torvalds 		kaddr = kmap_atomic(page, KM_USER0);
19121da177e4SLinus Torvalds 		memcpy(kaddr, symname, len);
19131da177e4SLinus Torvalds 		kunmap_atomic(kaddr, KM_USER0);
19141da177e4SLinus Torvalds 		set_page_dirty(page);
19151da177e4SLinus Torvalds 		page_cache_release(page);
19161da177e4SLinus Torvalds 	}
19171da177e4SLinus Torvalds 	if (dir->i_mode & S_ISGID)
19181da177e4SLinus Torvalds 		inode->i_gid = dir->i_gid;
19191da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
19201da177e4SLinus Torvalds 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
19211da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
19221da177e4SLinus Torvalds 	dget(dentry);
19231da177e4SLinus Torvalds 	return 0;
19241da177e4SLinus Torvalds }
19251da177e4SLinus Torvalds 
1926cc314eefSLinus Torvalds static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
19271da177e4SLinus Torvalds {
19281da177e4SLinus Torvalds 	nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
1929cc314eefSLinus Torvalds 	return NULL;
19301da177e4SLinus Torvalds }
19311da177e4SLinus Torvalds 
1932cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
19331da177e4SLinus Torvalds {
19341da177e4SLinus Torvalds 	struct page *page = NULL;
19351da177e4SLinus Torvalds 	int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
19361da177e4SLinus Torvalds 	nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
1937d3602444SHugh Dickins 	if (page)
1938d3602444SHugh Dickins 		unlock_page(page);
1939cc314eefSLinus Torvalds 	return page;
19401da177e4SLinus Torvalds }
19411da177e4SLinus Torvalds 
1942cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
19431da177e4SLinus Torvalds {
19441da177e4SLinus Torvalds 	if (!IS_ERR(nd_get_link(nd))) {
1945cc314eefSLinus Torvalds 		struct page *page = cookie;
19461da177e4SLinus Torvalds 		kunmap(page);
19471da177e4SLinus Torvalds 		mark_page_accessed(page);
19481da177e4SLinus Torvalds 		page_cache_release(page);
19491da177e4SLinus Torvalds 	}
19501da177e4SLinus Torvalds }
19511da177e4SLinus Torvalds 
195292e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations = {
19531da177e4SLinus Torvalds 	.readlink	= generic_readlink,
19541da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link_inline,
19551da177e4SLinus Torvalds };
19561da177e4SLinus Torvalds 
195792e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = {
19581da177e4SLinus Torvalds 	.truncate	= shmem_truncate,
19591da177e4SLinus Torvalds 	.readlink	= generic_readlink,
19601da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link,
19611da177e4SLinus Torvalds 	.put_link	= shmem_put_link,
19621da177e4SLinus Torvalds };
19631da177e4SLinus Torvalds 
196439f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
196546711810SRandy Dunlap /*
196639f0247dSAndreas Gruenbacher  * Superblocks without xattr inode operations will get security.* xattr
196739f0247dSAndreas Gruenbacher  * support from the VFS "for free". As soon as we have any other xattrs
196839f0247dSAndreas Gruenbacher  * like ACLs, we also need to implement the security.* handlers at
196939f0247dSAndreas Gruenbacher  * filesystem level, though.
197039f0247dSAndreas Gruenbacher  */
197139f0247dSAndreas Gruenbacher 
197239f0247dSAndreas Gruenbacher static size_t shmem_xattr_security_list(struct inode *inode, char *list,
197339f0247dSAndreas Gruenbacher 					size_t list_len, const char *name,
197439f0247dSAndreas Gruenbacher 					size_t name_len)
197539f0247dSAndreas Gruenbacher {
197639f0247dSAndreas Gruenbacher 	return security_inode_listsecurity(inode, list, list_len);
197739f0247dSAndreas Gruenbacher }
197839f0247dSAndreas Gruenbacher 
197939f0247dSAndreas Gruenbacher static int shmem_xattr_security_get(struct inode *inode, const char *name,
198039f0247dSAndreas Gruenbacher 				    void *buffer, size_t size)
198139f0247dSAndreas Gruenbacher {
198239f0247dSAndreas Gruenbacher 	if (strcmp(name, "") == 0)
198339f0247dSAndreas Gruenbacher 		return -EINVAL;
198442492594SDavid P. Quigley 	return xattr_getsecurity(inode, name, buffer, size);
198539f0247dSAndreas Gruenbacher }
198639f0247dSAndreas Gruenbacher 
198739f0247dSAndreas Gruenbacher static int shmem_xattr_security_set(struct inode *inode, const char *name,
198839f0247dSAndreas Gruenbacher 				    const void *value, size_t size, int flags)
198939f0247dSAndreas Gruenbacher {
199039f0247dSAndreas Gruenbacher 	if (strcmp(name, "") == 0)
199139f0247dSAndreas Gruenbacher 		return -EINVAL;
199239f0247dSAndreas Gruenbacher 	return security_inode_setsecurity(inode, name, value, size, flags);
199339f0247dSAndreas Gruenbacher }
199439f0247dSAndreas Gruenbacher 
19951f370a23SAdrian Bunk static struct xattr_handler shmem_xattr_security_handler = {
199639f0247dSAndreas Gruenbacher 	.prefix = XATTR_SECURITY_PREFIX,
199739f0247dSAndreas Gruenbacher 	.list   = shmem_xattr_security_list,
199839f0247dSAndreas Gruenbacher 	.get    = shmem_xattr_security_get,
199939f0247dSAndreas Gruenbacher 	.set    = shmem_xattr_security_set,
200039f0247dSAndreas Gruenbacher };
200139f0247dSAndreas Gruenbacher 
200239f0247dSAndreas Gruenbacher static struct xattr_handler *shmem_xattr_handlers[] = {
200339f0247dSAndreas Gruenbacher 	&shmem_xattr_acl_access_handler,
200439f0247dSAndreas Gruenbacher 	&shmem_xattr_acl_default_handler,
200539f0247dSAndreas Gruenbacher 	&shmem_xattr_security_handler,
200639f0247dSAndreas Gruenbacher 	NULL
200739f0247dSAndreas Gruenbacher };
200839f0247dSAndreas Gruenbacher #endif
200939f0247dSAndreas Gruenbacher 
201091828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child)
201191828a40SDavid M. Grimes {
201291828a40SDavid M. Grimes 	return ERR_PTR(-ESTALE);
201391828a40SDavid M. Grimes }
201491828a40SDavid M. Grimes 
201591828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh)
201691828a40SDavid M. Grimes {
201791828a40SDavid M. Grimes 	__u32 *fh = vfh;
201891828a40SDavid M. Grimes 	__u64 inum = fh[2];
201991828a40SDavid M. Grimes 	inum = (inum << 32) | fh[1];
202091828a40SDavid M. Grimes 	return ino->i_ino == inum && fh[0] == ino->i_generation;
202191828a40SDavid M. Grimes }
202291828a40SDavid M. Grimes 
2023480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2024480b116cSChristoph Hellwig 		struct fid *fid, int fh_len, int fh_type)
202591828a40SDavid M. Grimes {
202691828a40SDavid M. Grimes 	struct inode *inode;
2027480b116cSChristoph Hellwig 	struct dentry *dentry = NULL;
2028480b116cSChristoph Hellwig 	u64 inum = fid->raw[2];
2029480b116cSChristoph Hellwig 	inum = (inum << 32) | fid->raw[1];
203091828a40SDavid M. Grimes 
2031480b116cSChristoph Hellwig 	if (fh_len < 3)
2032480b116cSChristoph Hellwig 		return NULL;
2033480b116cSChristoph Hellwig 
2034480b116cSChristoph Hellwig 	inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2035480b116cSChristoph Hellwig 			shmem_match, fid->raw);
203691828a40SDavid M. Grimes 	if (inode) {
2037480b116cSChristoph Hellwig 		dentry = d_find_alias(inode);
203891828a40SDavid M. Grimes 		iput(inode);
203991828a40SDavid M. Grimes 	}
204091828a40SDavid M. Grimes 
2041480b116cSChristoph Hellwig 	return dentry;
204291828a40SDavid M. Grimes }
204391828a40SDavid M. Grimes 
204491828a40SDavid M. Grimes static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
204591828a40SDavid M. Grimes 				int connectable)
204691828a40SDavid M. Grimes {
204791828a40SDavid M. Grimes 	struct inode *inode = dentry->d_inode;
204891828a40SDavid M. Grimes 
204991828a40SDavid M. Grimes 	if (*len < 3)
205091828a40SDavid M. Grimes 		return 255;
205191828a40SDavid M. Grimes 
205291828a40SDavid M. Grimes 	if (hlist_unhashed(&inode->i_hash)) {
205391828a40SDavid M. Grimes 		/* Unfortunately insert_inode_hash is not idempotent,
205491828a40SDavid M. Grimes 		 * so as we hash inodes here rather than at creation
205591828a40SDavid M. Grimes 		 * time, we need a lock to ensure we only try
205691828a40SDavid M. Grimes 		 * to do it once
205791828a40SDavid M. Grimes 		 */
205891828a40SDavid M. Grimes 		static DEFINE_SPINLOCK(lock);
205991828a40SDavid M. Grimes 		spin_lock(&lock);
206091828a40SDavid M. Grimes 		if (hlist_unhashed(&inode->i_hash))
206191828a40SDavid M. Grimes 			__insert_inode_hash(inode,
206291828a40SDavid M. Grimes 					    inode->i_ino + inode->i_generation);
206391828a40SDavid M. Grimes 		spin_unlock(&lock);
206491828a40SDavid M. Grimes 	}
206591828a40SDavid M. Grimes 
206691828a40SDavid M. Grimes 	fh[0] = inode->i_generation;
206791828a40SDavid M. Grimes 	fh[1] = inode->i_ino;
206891828a40SDavid M. Grimes 	fh[2] = ((__u64)inode->i_ino) >> 32;
206991828a40SDavid M. Grimes 
207091828a40SDavid M. Grimes 	*len = 3;
207191828a40SDavid M. Grimes 	return 1;
207291828a40SDavid M. Grimes }
207391828a40SDavid M. Grimes 
207439655164SChristoph Hellwig static const struct export_operations shmem_export_ops = {
207591828a40SDavid M. Grimes 	.get_parent     = shmem_get_parent,
207691828a40SDavid M. Grimes 	.encode_fh      = shmem_encode_fh,
2077480b116cSChristoph Hellwig 	.fh_to_dentry	= shmem_fh_to_dentry,
207891828a40SDavid M. Grimes };
207991828a40SDavid M. Grimes 
2080680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2081680d794bSakpm@linux-foundation.org 			       bool remount)
20821da177e4SLinus Torvalds {
20831da177e4SLinus Torvalds 	char *this_char, *value, *rest;
20841da177e4SLinus Torvalds 
2085b00dc3adSHugh Dickins 	while (options != NULL) {
2086b00dc3adSHugh Dickins 		this_char = options;
2087b00dc3adSHugh Dickins 		for (;;) {
2088b00dc3adSHugh Dickins 			/*
2089b00dc3adSHugh Dickins 			 * NUL-terminate this option: unfortunately,
2090b00dc3adSHugh Dickins 			 * mount options form a comma-separated list,
2091b00dc3adSHugh Dickins 			 * but mpol's nodelist may also contain commas.
2092b00dc3adSHugh Dickins 			 */
2093b00dc3adSHugh Dickins 			options = strchr(options, ',');
2094b00dc3adSHugh Dickins 			if (options == NULL)
2095b00dc3adSHugh Dickins 				break;
2096b00dc3adSHugh Dickins 			options++;
2097b00dc3adSHugh Dickins 			if (!isdigit(*options)) {
2098b00dc3adSHugh Dickins 				options[-1] = '\0';
2099b00dc3adSHugh Dickins 				break;
2100b00dc3adSHugh Dickins 			}
2101b00dc3adSHugh Dickins 		}
21021da177e4SLinus Torvalds 		if (!*this_char)
21031da177e4SLinus Torvalds 			continue;
21041da177e4SLinus Torvalds 		if ((value = strchr(this_char,'=')) != NULL) {
21051da177e4SLinus Torvalds 			*value++ = 0;
21061da177e4SLinus Torvalds 		} else {
21071da177e4SLinus Torvalds 			printk(KERN_ERR
21081da177e4SLinus Torvalds 			    "tmpfs: No value for mount option '%s'\n",
21091da177e4SLinus Torvalds 			    this_char);
21101da177e4SLinus Torvalds 			return 1;
21111da177e4SLinus Torvalds 		}
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds 		if (!strcmp(this_char,"size")) {
21141da177e4SLinus Torvalds 			unsigned long long size;
21151da177e4SLinus Torvalds 			size = memparse(value,&rest);
21161da177e4SLinus Torvalds 			if (*rest == '%') {
21171da177e4SLinus Torvalds 				size <<= PAGE_SHIFT;
21181da177e4SLinus Torvalds 				size *= totalram_pages;
21191da177e4SLinus Torvalds 				do_div(size, 100);
21201da177e4SLinus Torvalds 				rest++;
21211da177e4SLinus Torvalds 			}
21221da177e4SLinus Torvalds 			if (*rest)
21231da177e4SLinus Torvalds 				goto bad_val;
2124680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks =
2125680d794bSakpm@linux-foundation.org 				DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
21261da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_blocks")) {
2127680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks = memparse(value, &rest);
21281da177e4SLinus Torvalds 			if (*rest)
21291da177e4SLinus Torvalds 				goto bad_val;
21301da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_inodes")) {
2131680d794bSakpm@linux-foundation.org 			sbinfo->max_inodes = memparse(value, &rest);
21321da177e4SLinus Torvalds 			if (*rest)
21331da177e4SLinus Torvalds 				goto bad_val;
21341da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"mode")) {
2135680d794bSakpm@linux-foundation.org 			if (remount)
21361da177e4SLinus Torvalds 				continue;
2137680d794bSakpm@linux-foundation.org 			sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
21381da177e4SLinus Torvalds 			if (*rest)
21391da177e4SLinus Torvalds 				goto bad_val;
21401da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"uid")) {
2141680d794bSakpm@linux-foundation.org 			if (remount)
21421da177e4SLinus Torvalds 				continue;
2143680d794bSakpm@linux-foundation.org 			sbinfo->uid = simple_strtoul(value, &rest, 0);
21441da177e4SLinus Torvalds 			if (*rest)
21451da177e4SLinus Torvalds 				goto bad_val;
21461da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"gid")) {
2147680d794bSakpm@linux-foundation.org 			if (remount)
21481da177e4SLinus Torvalds 				continue;
2149680d794bSakpm@linux-foundation.org 			sbinfo->gid = simple_strtoul(value, &rest, 0);
21501da177e4SLinus Torvalds 			if (*rest)
21511da177e4SLinus Torvalds 				goto bad_val;
21527339ff83SRobin Holt 		} else if (!strcmp(this_char,"mpol")) {
2153*71fe804bSLee Schermerhorn 			if (mpol_parse_str(value, &sbinfo->mpol, 1))
21547339ff83SRobin Holt 				goto bad_val;
21551da177e4SLinus Torvalds 		} else {
21561da177e4SLinus Torvalds 			printk(KERN_ERR "tmpfs: Bad mount option %s\n",
21571da177e4SLinus Torvalds 			       this_char);
21581da177e4SLinus Torvalds 			return 1;
21591da177e4SLinus Torvalds 		}
21601da177e4SLinus Torvalds 	}
21611da177e4SLinus Torvalds 	return 0;
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds bad_val:
21641da177e4SLinus Torvalds 	printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
21651da177e4SLinus Torvalds 	       value, this_char);
21661da177e4SLinus Torvalds 	return 1;
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds }
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
21711da177e4SLinus Torvalds {
21721da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2173680d794bSakpm@linux-foundation.org 	struct shmem_sb_info config = *sbinfo;
21740edd73b3SHugh Dickins 	unsigned long blocks;
21750edd73b3SHugh Dickins 	unsigned long inodes;
21760edd73b3SHugh Dickins 	int error = -EINVAL;
21771da177e4SLinus Torvalds 
2178680d794bSakpm@linux-foundation.org 	if (shmem_parse_options(data, &config, true))
21790edd73b3SHugh Dickins 		return error;
21800edd73b3SHugh Dickins 
21810edd73b3SHugh Dickins 	spin_lock(&sbinfo->stat_lock);
21820edd73b3SHugh Dickins 	blocks = sbinfo->max_blocks - sbinfo->free_blocks;
21830edd73b3SHugh Dickins 	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
2184680d794bSakpm@linux-foundation.org 	if (config.max_blocks < blocks)
21850edd73b3SHugh Dickins 		goto out;
2186680d794bSakpm@linux-foundation.org 	if (config.max_inodes < inodes)
21870edd73b3SHugh Dickins 		goto out;
21880edd73b3SHugh Dickins 	/*
21890edd73b3SHugh Dickins 	 * Those tests also disallow limited->unlimited while any are in
21900edd73b3SHugh Dickins 	 * use, so i_blocks will always be zero when max_blocks is zero;
21910edd73b3SHugh Dickins 	 * but we must separately disallow unlimited->limited, because
21920edd73b3SHugh Dickins 	 * in that case we have no record of how much is already in use.
21930edd73b3SHugh Dickins 	 */
2194680d794bSakpm@linux-foundation.org 	if (config.max_blocks && !sbinfo->max_blocks)
21950edd73b3SHugh Dickins 		goto out;
2196680d794bSakpm@linux-foundation.org 	if (config.max_inodes && !sbinfo->max_inodes)
21970edd73b3SHugh Dickins 		goto out;
21980edd73b3SHugh Dickins 
21990edd73b3SHugh Dickins 	error = 0;
2200680d794bSakpm@linux-foundation.org 	sbinfo->max_blocks  = config.max_blocks;
2201680d794bSakpm@linux-foundation.org 	sbinfo->free_blocks = config.max_blocks - blocks;
2202680d794bSakpm@linux-foundation.org 	sbinfo->max_inodes  = config.max_inodes;
2203680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = config.max_inodes - inodes;
2204*71fe804bSLee Schermerhorn 
2205*71fe804bSLee Schermerhorn 	mpol_put(sbinfo->mpol);
2206*71fe804bSLee Schermerhorn 	sbinfo->mpol        = config.mpol;	/* transfers initial ref */
22070edd73b3SHugh Dickins out:
22080edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
22090edd73b3SHugh Dickins 	return error;
22101da177e4SLinus Torvalds }
2211680d794bSakpm@linux-foundation.org 
2212680d794bSakpm@linux-foundation.org static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2213680d794bSakpm@linux-foundation.org {
2214680d794bSakpm@linux-foundation.org 	struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2215680d794bSakpm@linux-foundation.org 
2216680d794bSakpm@linux-foundation.org 	if (sbinfo->max_blocks != shmem_default_max_blocks())
2217680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",size=%luk",
2218680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2219680d794bSakpm@linux-foundation.org 	if (sbinfo->max_inodes != shmem_default_max_inodes())
2220680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2221680d794bSakpm@linux-foundation.org 	if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2222680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",mode=%03o", sbinfo->mode);
2223680d794bSakpm@linux-foundation.org 	if (sbinfo->uid != 0)
2224680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",uid=%u", sbinfo->uid);
2225680d794bSakpm@linux-foundation.org 	if (sbinfo->gid != 0)
2226680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",gid=%u", sbinfo->gid);
2227*71fe804bSLee Schermerhorn 	shmem_show_mpol(seq, sbinfo->mpol);
2228680d794bSakpm@linux-foundation.org 	return 0;
2229680d794bSakpm@linux-foundation.org }
2230680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb)
22331da177e4SLinus Torvalds {
22341da177e4SLinus Torvalds 	kfree(sb->s_fs_info);
22351da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
22361da177e4SLinus Torvalds }
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds static int shmem_fill_super(struct super_block *sb,
22391da177e4SLinus Torvalds 			    void *data, int silent)
22401da177e4SLinus Torvalds {
22411da177e4SLinus Torvalds 	struct inode *inode;
22421da177e4SLinus Torvalds 	struct dentry *root;
22430edd73b3SHugh Dickins 	struct shmem_sb_info *sbinfo;
2244680d794bSakpm@linux-foundation.org 	int err = -ENOMEM;
2245680d794bSakpm@linux-foundation.org 
2246680d794bSakpm@linux-foundation.org 	/* Round up to L1_CACHE_BYTES to resist false sharing */
2247680d794bSakpm@linux-foundation.org 	sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
2248680d794bSakpm@linux-foundation.org 				L1_CACHE_BYTES), GFP_KERNEL);
2249680d794bSakpm@linux-foundation.org 	if (!sbinfo)
2250680d794bSakpm@linux-foundation.org 		return -ENOMEM;
2251680d794bSakpm@linux-foundation.org 
2252680d794bSakpm@linux-foundation.org 	sbinfo->max_blocks = 0;
2253680d794bSakpm@linux-foundation.org 	sbinfo->max_inodes = 0;
2254680d794bSakpm@linux-foundation.org 	sbinfo->mode = S_IRWXUGO | S_ISVTX;
2255680d794bSakpm@linux-foundation.org 	sbinfo->uid = current->fsuid;
2256680d794bSakpm@linux-foundation.org 	sbinfo->gid = current->fsgid;
2257*71fe804bSLee Schermerhorn 	sbinfo->mpol = NULL;
2258680d794bSakpm@linux-foundation.org 	sb->s_fs_info = sbinfo;
22591da177e4SLinus Torvalds 
22600edd73b3SHugh Dickins #ifdef CONFIG_TMPFS
22611da177e4SLinus Torvalds 	/*
22621da177e4SLinus Torvalds 	 * Per default we only allow half of the physical ram per
22631da177e4SLinus Torvalds 	 * tmpfs instance, limiting inodes to one per page of lowmem;
22641da177e4SLinus Torvalds 	 * but the internal instance is left unlimited.
22651da177e4SLinus Torvalds 	 */
22661da177e4SLinus Torvalds 	if (!(sb->s_flags & MS_NOUSER)) {
2267680d794bSakpm@linux-foundation.org 		sbinfo->max_blocks = shmem_default_max_blocks();
2268680d794bSakpm@linux-foundation.org 		sbinfo->max_inodes = shmem_default_max_inodes();
2269680d794bSakpm@linux-foundation.org 		if (shmem_parse_options(data, sbinfo, false)) {
2270680d794bSakpm@linux-foundation.org 			err = -EINVAL;
2271680d794bSakpm@linux-foundation.org 			goto failed;
2272680d794bSakpm@linux-foundation.org 		}
22731da177e4SLinus Torvalds 	}
227491828a40SDavid M. Grimes 	sb->s_export_op = &shmem_export_ops;
22750edd73b3SHugh Dickins #else
22760edd73b3SHugh Dickins 	sb->s_flags |= MS_NOUSER;
22770edd73b3SHugh Dickins #endif
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
2280680d794bSakpm@linux-foundation.org 	sbinfo->free_blocks = sbinfo->max_blocks;
2281680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = sbinfo->max_inodes;
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds 	sb->s_maxbytes = SHMEM_MAX_BYTES;
22841da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
22851da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
22861da177e4SLinus Torvalds 	sb->s_magic = TMPFS_MAGIC;
22871da177e4SLinus Torvalds 	sb->s_op = &shmem_ops;
2288cfd95a9cSRobin H. Johnson 	sb->s_time_gran = 1;
228939f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
229039f0247dSAndreas Gruenbacher 	sb->s_xattr = shmem_xattr_handlers;
229139f0247dSAndreas Gruenbacher 	sb->s_flags |= MS_POSIXACL;
229239f0247dSAndreas Gruenbacher #endif
22930edd73b3SHugh Dickins 
2294680d794bSakpm@linux-foundation.org 	inode = shmem_get_inode(sb, S_IFDIR | sbinfo->mode, 0);
22951da177e4SLinus Torvalds 	if (!inode)
22961da177e4SLinus Torvalds 		goto failed;
2297680d794bSakpm@linux-foundation.org 	inode->i_uid = sbinfo->uid;
2298680d794bSakpm@linux-foundation.org 	inode->i_gid = sbinfo->gid;
22991da177e4SLinus Torvalds 	root = d_alloc_root(inode);
23001da177e4SLinus Torvalds 	if (!root)
23011da177e4SLinus Torvalds 		goto failed_iput;
23021da177e4SLinus Torvalds 	sb->s_root = root;
23031da177e4SLinus Torvalds 	return 0;
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds failed_iput:
23061da177e4SLinus Torvalds 	iput(inode);
23071da177e4SLinus Torvalds failed:
23081da177e4SLinus Torvalds 	shmem_put_super(sb);
23091da177e4SLinus Torvalds 	return err;
23101da177e4SLinus Torvalds }
23111da177e4SLinus Torvalds 
2312fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep;
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb)
23151da177e4SLinus Torvalds {
23161da177e4SLinus Torvalds 	struct shmem_inode_info *p;
2317e94b1766SChristoph Lameter 	p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
23181da177e4SLinus Torvalds 	if (!p)
23191da177e4SLinus Torvalds 		return NULL;
23201da177e4SLinus Torvalds 	return &p->vfs_inode;
23211da177e4SLinus Torvalds }
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode)
23241da177e4SLinus Torvalds {
23251da177e4SLinus Torvalds 	if ((inode->i_mode & S_IFMT) == S_IFREG) {
23261da177e4SLinus Torvalds 		/* only struct inode is valid if it's an inline symlink */
23271da177e4SLinus Torvalds 		mpol_free_shared_policy(&SHMEM_I(inode)->policy);
23281da177e4SLinus Torvalds 	}
232939f0247dSAndreas Gruenbacher 	shmem_acl_destroy_inode(inode);
23301da177e4SLinus Torvalds 	kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
23311da177e4SLinus Torvalds }
23321da177e4SLinus Torvalds 
23334ba9b9d0SChristoph Lameter static void init_once(struct kmem_cache *cachep, void *foo)
23341da177e4SLinus Torvalds {
23351da177e4SLinus Torvalds 	struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds 	inode_init_once(&p->vfs_inode);
233839f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
233939f0247dSAndreas Gruenbacher 	p->i_acl = NULL;
234039f0247dSAndreas Gruenbacher 	p->i_default_acl = NULL;
234139f0247dSAndreas Gruenbacher #endif
23421da177e4SLinus Torvalds }
23431da177e4SLinus Torvalds 
23441da177e4SLinus Torvalds static int init_inodecache(void)
23451da177e4SLinus Torvalds {
23461da177e4SLinus Torvalds 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
23471da177e4SLinus Torvalds 				sizeof(struct shmem_inode_info),
2348040b5c6fSAlexey Dobriyan 				0, SLAB_PANIC, init_once);
23491da177e4SLinus Torvalds 	return 0;
23501da177e4SLinus Torvalds }
23511da177e4SLinus Torvalds 
23521da177e4SLinus Torvalds static void destroy_inodecache(void)
23531da177e4SLinus Torvalds {
23541a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(shmem_inode_cachep);
23551da177e4SLinus Torvalds }
23561da177e4SLinus Torvalds 
2357f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = {
23581da177e4SLinus Torvalds 	.writepage	= shmem_writepage,
235976719325SKen Chen 	.set_page_dirty	= __set_page_dirty_no_writeback,
23601da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
2361ae976416SHugh Dickins 	.readpage	= shmem_readpage,
2362800d15a5SNick Piggin 	.write_begin	= shmem_write_begin,
2363800d15a5SNick Piggin 	.write_end	= shmem_write_end,
23641da177e4SLinus Torvalds #endif
2365304dbdb7SLee Schermerhorn 	.migratepage	= migrate_page,
23661da177e4SLinus Torvalds };
23671da177e4SLinus Torvalds 
236815ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = {
23691da177e4SLinus Torvalds 	.mmap		= shmem_mmap,
23701da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
23711da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
23721da177e4SLinus Torvalds 	.read		= shmem_file_read,
23735402b976SHugh Dickins 	.write		= do_sync_write,
23745402b976SHugh Dickins 	.aio_write	= generic_file_aio_write,
23751da177e4SLinus Torvalds 	.fsync		= simple_sync_file,
2376ae976416SHugh Dickins 	.splice_read	= generic_file_splice_read,
2377ae976416SHugh Dickins 	.splice_write	= generic_file_splice_write,
23781da177e4SLinus Torvalds #endif
23791da177e4SLinus Torvalds };
23801da177e4SLinus Torvalds 
238192e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = {
23821da177e4SLinus Torvalds 	.truncate	= shmem_truncate,
23831da177e4SLinus Torvalds 	.setattr	= shmem_notify_change,
2384f6b3ec23SBadari Pulavarty 	.truncate_range	= shmem_truncate_range,
238539f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
238639f0247dSAndreas Gruenbacher 	.setxattr	= generic_setxattr,
238739f0247dSAndreas Gruenbacher 	.getxattr	= generic_getxattr,
238839f0247dSAndreas Gruenbacher 	.listxattr	= generic_listxattr,
238939f0247dSAndreas Gruenbacher 	.removexattr	= generic_removexattr,
239039f0247dSAndreas Gruenbacher 	.permission	= shmem_permission,
239139f0247dSAndreas Gruenbacher #endif
239239f0247dSAndreas Gruenbacher 
23931da177e4SLinus Torvalds };
23941da177e4SLinus Torvalds 
239592e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = {
23961da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
23971da177e4SLinus Torvalds 	.create		= shmem_create,
23981da177e4SLinus Torvalds 	.lookup		= simple_lookup,
23991da177e4SLinus Torvalds 	.link		= shmem_link,
24001da177e4SLinus Torvalds 	.unlink		= shmem_unlink,
24011da177e4SLinus Torvalds 	.symlink	= shmem_symlink,
24021da177e4SLinus Torvalds 	.mkdir		= shmem_mkdir,
24031da177e4SLinus Torvalds 	.rmdir		= shmem_rmdir,
24041da177e4SLinus Torvalds 	.mknod		= shmem_mknod,
24051da177e4SLinus Torvalds 	.rename		= shmem_rename,
24061da177e4SLinus Torvalds #endif
240739f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
240839f0247dSAndreas Gruenbacher 	.setattr	= shmem_notify_change,
240939f0247dSAndreas Gruenbacher 	.setxattr	= generic_setxattr,
241039f0247dSAndreas Gruenbacher 	.getxattr	= generic_getxattr,
241139f0247dSAndreas Gruenbacher 	.listxattr	= generic_listxattr,
241239f0247dSAndreas Gruenbacher 	.removexattr	= generic_removexattr,
241339f0247dSAndreas Gruenbacher 	.permission	= shmem_permission,
241439f0247dSAndreas Gruenbacher #endif
241539f0247dSAndreas Gruenbacher };
241639f0247dSAndreas Gruenbacher 
241792e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = {
241839f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
241939f0247dSAndreas Gruenbacher 	.setattr	= shmem_notify_change,
242039f0247dSAndreas Gruenbacher 	.setxattr	= generic_setxattr,
242139f0247dSAndreas Gruenbacher 	.getxattr	= generic_getxattr,
242239f0247dSAndreas Gruenbacher 	.listxattr	= generic_listxattr,
242339f0247dSAndreas Gruenbacher 	.removexattr	= generic_removexattr,
242439f0247dSAndreas Gruenbacher 	.permission	= shmem_permission,
242539f0247dSAndreas Gruenbacher #endif
24261da177e4SLinus Torvalds };
24271da177e4SLinus Torvalds 
2428759b9775SHugh Dickins static const struct super_operations shmem_ops = {
24291da177e4SLinus Torvalds 	.alloc_inode	= shmem_alloc_inode,
24301da177e4SLinus Torvalds 	.destroy_inode	= shmem_destroy_inode,
24311da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
24321da177e4SLinus Torvalds 	.statfs		= shmem_statfs,
24331da177e4SLinus Torvalds 	.remount_fs	= shmem_remount_fs,
2434680d794bSakpm@linux-foundation.org 	.show_options	= shmem_show_options,
24351da177e4SLinus Torvalds #endif
24361da177e4SLinus Torvalds 	.delete_inode	= shmem_delete_inode,
24371da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
24381da177e4SLinus Torvalds 	.put_super	= shmem_put_super,
24391da177e4SLinus Torvalds };
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds static struct vm_operations_struct shmem_vm_ops = {
244254cb8821SNick Piggin 	.fault		= shmem_fault,
24431da177e4SLinus Torvalds #ifdef CONFIG_NUMA
24441da177e4SLinus Torvalds 	.set_policy     = shmem_set_policy,
24451da177e4SLinus Torvalds 	.get_policy     = shmem_get_policy,
24461da177e4SLinus Torvalds #endif
24471da177e4SLinus Torvalds };
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds 
2450454e2398SDavid Howells static int shmem_get_sb(struct file_system_type *fs_type,
2451454e2398SDavid Howells 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
24521da177e4SLinus Torvalds {
2453454e2398SDavid Howells 	return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt);
24541da177e4SLinus Torvalds }
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds static struct file_system_type tmpfs_fs_type = {
24571da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
24581da177e4SLinus Torvalds 	.name		= "tmpfs",
24591da177e4SLinus Torvalds 	.get_sb		= shmem_get_sb,
24601da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
24611da177e4SLinus Torvalds };
24621da177e4SLinus Torvalds static struct vfsmount *shm_mnt;
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds static int __init init_tmpfs(void)
24651da177e4SLinus Torvalds {
24661da177e4SLinus Torvalds 	int error;
24671da177e4SLinus Torvalds 
2468e0bf68ddSPeter Zijlstra 	error = bdi_init(&shmem_backing_dev_info);
2469e0bf68ddSPeter Zijlstra 	if (error)
2470e0bf68ddSPeter Zijlstra 		goto out4;
2471e0bf68ddSPeter Zijlstra 
24721da177e4SLinus Torvalds 	error = init_inodecache();
24731da177e4SLinus Torvalds 	if (error)
24741da177e4SLinus Torvalds 		goto out3;
24751da177e4SLinus Torvalds 
24761da177e4SLinus Torvalds 	error = register_filesystem(&tmpfs_fs_type);
24771da177e4SLinus Torvalds 	if (error) {
24781da177e4SLinus Torvalds 		printk(KERN_ERR "Could not register tmpfs\n");
24791da177e4SLinus Torvalds 		goto out2;
24801da177e4SLinus Torvalds 	}
248195dc112aSGreg Kroah-Hartman 
24821f5ce9e9STrond Myklebust 	shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
24831da177e4SLinus Torvalds 				tmpfs_fs_type.name, NULL);
24841da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt)) {
24851da177e4SLinus Torvalds 		error = PTR_ERR(shm_mnt);
24861da177e4SLinus Torvalds 		printk(KERN_ERR "Could not kern_mount tmpfs\n");
24871da177e4SLinus Torvalds 		goto out1;
24881da177e4SLinus Torvalds 	}
24891da177e4SLinus Torvalds 	return 0;
24901da177e4SLinus Torvalds 
24911da177e4SLinus Torvalds out1:
24921da177e4SLinus Torvalds 	unregister_filesystem(&tmpfs_fs_type);
24931da177e4SLinus Torvalds out2:
24941da177e4SLinus Torvalds 	destroy_inodecache();
24951da177e4SLinus Torvalds out3:
2496e0bf68ddSPeter Zijlstra 	bdi_destroy(&shmem_backing_dev_info);
2497e0bf68ddSPeter Zijlstra out4:
24981da177e4SLinus Torvalds 	shm_mnt = ERR_PTR(error);
24991da177e4SLinus Torvalds 	return error;
25001da177e4SLinus Torvalds }
25011da177e4SLinus Torvalds module_init(init_tmpfs)
25021da177e4SLinus Torvalds 
250346711810SRandy Dunlap /**
25041da177e4SLinus Torvalds  * shmem_file_setup - get an unlinked file living in tmpfs
25051da177e4SLinus Torvalds  * @name: name for dentry (to be seen in /proc/<pid>/maps
25061da177e4SLinus Torvalds  * @size: size to be set for the file
250746711810SRandy Dunlap  * @flags: vm_flags
25081da177e4SLinus Torvalds  */
25091da177e4SLinus Torvalds struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
25101da177e4SLinus Torvalds {
25111da177e4SLinus Torvalds 	int error;
25121da177e4SLinus Torvalds 	struct file *file;
25131da177e4SLinus Torvalds 	struct inode *inode;
25141da177e4SLinus Torvalds 	struct dentry *dentry, *root;
25151da177e4SLinus Torvalds 	struct qstr this;
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt))
25181da177e4SLinus Torvalds 		return (void *)shm_mnt;
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds 	if (size < 0 || size > SHMEM_MAX_BYTES)
25211da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds 	if (shmem_acct_size(flags, size))
25241da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds 	error = -ENOMEM;
25271da177e4SLinus Torvalds 	this.name = name;
25281da177e4SLinus Torvalds 	this.len = strlen(name);
25291da177e4SLinus Torvalds 	this.hash = 0; /* will go */
25301da177e4SLinus Torvalds 	root = shm_mnt->mnt_root;
25311da177e4SLinus Torvalds 	dentry = d_alloc(root, &this);
25321da177e4SLinus Torvalds 	if (!dentry)
25331da177e4SLinus Torvalds 		goto put_memory;
25341da177e4SLinus Torvalds 
25351da177e4SLinus Torvalds 	error = -ENFILE;
25361da177e4SLinus Torvalds 	file = get_empty_filp();
25371da177e4SLinus Torvalds 	if (!file)
25381da177e4SLinus Torvalds 		goto put_dentry;
25391da177e4SLinus Torvalds 
25401da177e4SLinus Torvalds 	error = -ENOSPC;
25411da177e4SLinus Torvalds 	inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
25421da177e4SLinus Torvalds 	if (!inode)
25431da177e4SLinus Torvalds 		goto close_file;
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 	SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
25461da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
25471da177e4SLinus Torvalds 	inode->i_size = size;
25481da177e4SLinus Torvalds 	inode->i_nlink = 0;	/* It is unlinked */
2549ce8d2cdfSDave Hansen 	init_file(file, shm_mnt, dentry, FMODE_WRITE | FMODE_READ,
2550ce8d2cdfSDave Hansen 			&shmem_file_operations);
25511da177e4SLinus Torvalds 	return file;
25521da177e4SLinus Torvalds 
25531da177e4SLinus Torvalds close_file:
25541da177e4SLinus Torvalds 	put_filp(file);
25551da177e4SLinus Torvalds put_dentry:
25561da177e4SLinus Torvalds 	dput(dentry);
25571da177e4SLinus Torvalds put_memory:
25581da177e4SLinus Torvalds 	shmem_unacct_size(flags, size);
25591da177e4SLinus Torvalds 	return ERR_PTR(error);
25601da177e4SLinus Torvalds }
25611da177e4SLinus Torvalds 
256246711810SRandy Dunlap /**
25631da177e4SLinus Torvalds  * shmem_zero_setup - setup a shared anonymous mapping
25641da177e4SLinus Torvalds  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
25651da177e4SLinus Torvalds  */
25661da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma)
25671da177e4SLinus Torvalds {
25681da177e4SLinus Torvalds 	struct file *file;
25691da177e4SLinus Torvalds 	loff_t size = vma->vm_end - vma->vm_start;
25701da177e4SLinus Torvalds 
25711da177e4SLinus Torvalds 	file = shmem_file_setup("dev/zero", size, vma->vm_flags);
25721da177e4SLinus Torvalds 	if (IS_ERR(file))
25731da177e4SLinus Torvalds 		return PTR_ERR(file);
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds 	if (vma->vm_file)
25761da177e4SLinus Torvalds 		fput(vma->vm_file);
25771da177e4SLinus Torvalds 	vma->vm_file = file;
25781da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
25791da177e4SLinus Torvalds 	return 0;
25801da177e4SLinus Torvalds }
2581