xref: /openbmc/linux/mm/shmem.c (revision d9fe526a83b84edc9c5ff217a00c896bfc20b2ce)
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>
52304dbdb7SLee Schermerhorn 
531da177e4SLinus Torvalds #include <asm/uaccess.h>
541da177e4SLinus Torvalds #include <asm/div64.h>
551da177e4SLinus Torvalds #include <asm/pgtable.h>
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /* This magic number is used in glibc for posix shared memory */
581da177e4SLinus Torvalds #define TMPFS_MAGIC	0x01021994
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
611da177e4SLinus Torvalds #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
621da177e4SLinus Torvalds #define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #define SHMEM_MAX_INDEX  (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
651da177e4SLinus Torvalds #define SHMEM_MAX_BYTES  ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
701da177e4SLinus Torvalds #define SHMEM_PAGEIN	 VM_READ
711da177e4SLinus Torvalds #define SHMEM_TRUNCATE	 VM_WRITE
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* Definition to limit shmem_truncate's steps between cond_rescheds */
741da177e4SLinus Torvalds #define LATENCY_LIMIT	 64
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */
771da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
801da177e4SLinus Torvalds enum sgp_type {
811da177e4SLinus Torvalds 	SGP_READ,	/* don't exceed i_size, don't allocate page */
821da177e4SLinus Torvalds 	SGP_CACHE,	/* don't exceed i_size, may allocate page */
831da177e4SLinus Torvalds 	SGP_WRITE,	/* may exceed i_size, may allocate page */
841da177e4SLinus Torvalds };
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx,
871da177e4SLinus Torvalds 			 struct page **pagep, enum sgp_type sgp, int *type);
881da177e4SLinus Torvalds 
896daa0e28SAl Viro static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	/*
921da177e4SLinus Torvalds 	 * The above definition of ENTRIES_PER_PAGE, and the use of
931da177e4SLinus Torvalds 	 * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
941da177e4SLinus Torvalds 	 * might be reconsidered if it ever diverges from PAGE_SIZE.
95769848c0SMel Gorman 	 *
96e12ba74dSMel Gorman 	 * Mobility flags are masked out as swap vectors cannot move
971da177e4SLinus Torvalds 	 */
98e12ba74dSMel Gorman 	return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
99769848c0SMel Gorman 				PAGE_CACHE_SHIFT-PAGE_SHIFT);
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds static inline void shmem_dir_free(struct page *page)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	__free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds static struct page **shmem_dir_map(struct page *page)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	return (struct page **)kmap_atomic(page, KM_USER0);
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds static inline void shmem_dir_unmap(struct page **dir)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	kunmap_atomic(dir, KM_USER0);
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds static swp_entry_t *shmem_swp_map(struct page *page)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	return (swp_entry_t *)kmap_atomic(page, KM_USER1);
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds static inline void shmem_swp_balance_unmap(void)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	/*
1251da177e4SLinus Torvalds 	 * When passing a pointer to an i_direct entry, to code which
1261da177e4SLinus Torvalds 	 * also handles indirect entries and so will shmem_swp_unmap,
1271da177e4SLinus Torvalds 	 * we must arrange for the preempt count to remain in balance.
1281da177e4SLinus Torvalds 	 * What kmap_atomic of a lowmem page does depends on config
1291da177e4SLinus Torvalds 	 * and architecture, so pretend to kmap_atomic some lowmem page.
1301da177e4SLinus Torvalds 	 */
1311da177e4SLinus Torvalds 	(void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds static inline void shmem_swp_unmap(swp_entry_t *entry)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	kunmap_atomic(entry, KM_USER1);
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	return sb->s_fs_info;
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds /*
1451da177e4SLinus Torvalds  * shmem_file_setup pre-accounts the whole fixed size of a VM object,
1461da177e4SLinus Torvalds  * for shared memory and for shared anonymous (/dev/zero) mappings
1471da177e4SLinus Torvalds  * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
1481da177e4SLinus Torvalds  * consistent with the pre-accounting of private mappings ...
1491da177e4SLinus Torvalds  */
1501da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	return (flags & VM_ACCOUNT)?
1531da177e4SLinus Torvalds 		security_vm_enough_memory(VM_ACCT(size)): 0;
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	if (flags & VM_ACCOUNT)
1591da177e4SLinus Torvalds 		vm_unacct_memory(VM_ACCT(size));
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds /*
1631da177e4SLinus Torvalds  * ... whereas tmpfs objects are accounted incrementally as
1641da177e4SLinus Torvalds  * pages are allocated, in order to allow huge sparse files.
1651da177e4SLinus Torvalds  * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
1661da177e4SLinus Torvalds  * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
1671da177e4SLinus Torvalds  */
1681da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags)
1691da177e4SLinus Torvalds {
1701da177e4SLinus Torvalds 	return (flags & VM_ACCOUNT)?
1711da177e4SLinus Torvalds 		0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	if (!(flags & VM_ACCOUNT))
1771da177e4SLinus Torvalds 		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
180759b9775SHugh Dickins static const struct super_operations shmem_ops;
181f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops;
18215ad7cdcSHelge Deller static const struct file_operations shmem_file_operations;
18392e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations;
18492e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations;
18592e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations;
1861da177e4SLinus Torvalds static struct vm_operations_struct shmem_vm_ops;
1871da177e4SLinus Torvalds 
1886c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info  __read_mostly = {
1891da177e4SLinus Torvalds 	.ra_pages	= 0,	/* No readahead */
1901da177e4SLinus Torvalds 	.capabilities	= BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
1911da177e4SLinus Torvalds 	.unplug_io_fn	= default_unplug_io_fn,
1921da177e4SLinus Torvalds };
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist);
1951da177e4SLinus Torvalds static DEFINE_SPINLOCK(shmem_swaplist_lock);
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds static void shmem_free_blocks(struct inode *inode, long pages)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
2000edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
2011da177e4SLinus Torvalds 		spin_lock(&sbinfo->stat_lock);
2021da177e4SLinus Torvalds 		sbinfo->free_blocks += pages;
2031da177e4SLinus Torvalds 		inode->i_blocks -= pages*BLOCKS_PER_PAGE;
2041da177e4SLinus Torvalds 		spin_unlock(&sbinfo->stat_lock);
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2085b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb)
2095b04c689SPavel Emelyanov {
2105b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2115b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2125b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2135b04c689SPavel Emelyanov 		if (!sbinfo->free_inodes) {
2145b04c689SPavel Emelyanov 			spin_unlock(&sbinfo->stat_lock);
2155b04c689SPavel Emelyanov 			return -ENOSPC;
2165b04c689SPavel Emelyanov 		}
2175b04c689SPavel Emelyanov 		sbinfo->free_inodes--;
2185b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2195b04c689SPavel Emelyanov 	}
2205b04c689SPavel Emelyanov 	return 0;
2215b04c689SPavel Emelyanov }
2225b04c689SPavel Emelyanov 
2235b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb)
2245b04c689SPavel Emelyanov {
2255b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2265b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2275b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2285b04c689SPavel Emelyanov 		sbinfo->free_inodes++;
2295b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2305b04c689SPavel Emelyanov 	}
2315b04c689SPavel Emelyanov }
2325b04c689SPavel Emelyanov 
2331da177e4SLinus Torvalds /*
2341da177e4SLinus Torvalds  * shmem_recalc_inode - recalculate the size of an inode
2351da177e4SLinus Torvalds  *
2361da177e4SLinus Torvalds  * @inode: inode to recalc
2371da177e4SLinus Torvalds  *
2381da177e4SLinus Torvalds  * We have to calculate the free blocks since the mm can drop
2391da177e4SLinus Torvalds  * undirtied hole pages behind our back.
2401da177e4SLinus Torvalds  *
2411da177e4SLinus Torvalds  * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
2421da177e4SLinus Torvalds  * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
2431da177e4SLinus Torvalds  *
2441da177e4SLinus Torvalds  * It has to be called with the spinlock held.
2451da177e4SLinus Torvalds  */
2461da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode)
2471da177e4SLinus Torvalds {
2481da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
2491da177e4SLinus Torvalds 	long freed;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
2521da177e4SLinus Torvalds 	if (freed > 0) {
2531da177e4SLinus Torvalds 		info->alloced -= freed;
2541da177e4SLinus Torvalds 		shmem_unacct_blocks(info->flags, freed);
2551da177e4SLinus Torvalds 		shmem_free_blocks(inode, freed);
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds /*
2601da177e4SLinus Torvalds  * shmem_swp_entry - find the swap vector position in the info structure
2611da177e4SLinus Torvalds  *
2621da177e4SLinus Torvalds  * @info:  info structure for the inode
2631da177e4SLinus Torvalds  * @index: index of the page to find
2641da177e4SLinus Torvalds  * @page:  optional page to add to the structure. Has to be preset to
2651da177e4SLinus Torvalds  *         all zeros
2661da177e4SLinus Torvalds  *
2671da177e4SLinus Torvalds  * If there is no space allocated yet it will return NULL when
2681da177e4SLinus Torvalds  * page is NULL, else it will use the page for the needed block,
2691da177e4SLinus Torvalds  * setting it to NULL on return to indicate that it has been used.
2701da177e4SLinus Torvalds  *
2711da177e4SLinus Torvalds  * The swap vector is organized the following way:
2721da177e4SLinus Torvalds  *
2731da177e4SLinus Torvalds  * There are SHMEM_NR_DIRECT entries directly stored in the
2741da177e4SLinus Torvalds  * shmem_inode_info structure. So small files do not need an addional
2751da177e4SLinus Torvalds  * allocation.
2761da177e4SLinus Torvalds  *
2771da177e4SLinus Torvalds  * For pages with index > SHMEM_NR_DIRECT there is the pointer
2781da177e4SLinus Torvalds  * i_indirect which points to a page which holds in the first half
2791da177e4SLinus Torvalds  * doubly indirect blocks, in the second half triple indirect blocks:
2801da177e4SLinus Torvalds  *
2811da177e4SLinus Torvalds  * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
2821da177e4SLinus Torvalds  * following layout (for SHMEM_NR_DIRECT == 16):
2831da177e4SLinus Torvalds  *
2841da177e4SLinus Torvalds  * i_indirect -> dir --> 16-19
2851da177e4SLinus Torvalds  * 	      |	     +-> 20-23
2861da177e4SLinus Torvalds  * 	      |
2871da177e4SLinus Torvalds  * 	      +-->dir2 --> 24-27
2881da177e4SLinus Torvalds  * 	      |	       +-> 28-31
2891da177e4SLinus Torvalds  * 	      |	       +-> 32-35
2901da177e4SLinus Torvalds  * 	      |	       +-> 36-39
2911da177e4SLinus Torvalds  * 	      |
2921da177e4SLinus Torvalds  * 	      +-->dir3 --> 40-43
2931da177e4SLinus Torvalds  * 	       	       +-> 44-47
2941da177e4SLinus Torvalds  * 	      	       +-> 48-51
2951da177e4SLinus Torvalds  * 	      	       +-> 52-55
2961da177e4SLinus Torvalds  */
2971da177e4SLinus Torvalds static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	unsigned long offset;
3001da177e4SLinus Torvalds 	struct page **dir;
3011da177e4SLinus Torvalds 	struct page *subdir;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	if (index < SHMEM_NR_DIRECT) {
3041da177e4SLinus Torvalds 		shmem_swp_balance_unmap();
3051da177e4SLinus Torvalds 		return info->i_direct+index;
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds 	if (!info->i_indirect) {
3081da177e4SLinus Torvalds 		if (page) {
3091da177e4SLinus Torvalds 			info->i_indirect = *page;
3101da177e4SLinus Torvalds 			*page = NULL;
3111da177e4SLinus Torvalds 		}
3121da177e4SLinus Torvalds 		return NULL;			/* need another page */
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	index -= SHMEM_NR_DIRECT;
3161da177e4SLinus Torvalds 	offset = index % ENTRIES_PER_PAGE;
3171da177e4SLinus Torvalds 	index /= ENTRIES_PER_PAGE;
3181da177e4SLinus Torvalds 	dir = shmem_dir_map(info->i_indirect);
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	if (index >= ENTRIES_PER_PAGE/2) {
3211da177e4SLinus Torvalds 		index -= ENTRIES_PER_PAGE/2;
3221da177e4SLinus Torvalds 		dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
3231da177e4SLinus Torvalds 		index %= ENTRIES_PER_PAGE;
3241da177e4SLinus Torvalds 		subdir = *dir;
3251da177e4SLinus Torvalds 		if (!subdir) {
3261da177e4SLinus Torvalds 			if (page) {
3271da177e4SLinus Torvalds 				*dir = *page;
3281da177e4SLinus Torvalds 				*page = NULL;
3291da177e4SLinus Torvalds 			}
3301da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
3311da177e4SLinus Torvalds 			return NULL;		/* need another page */
3321da177e4SLinus Torvalds 		}
3331da177e4SLinus Torvalds 		shmem_dir_unmap(dir);
3341da177e4SLinus Torvalds 		dir = shmem_dir_map(subdir);
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	dir += index;
3381da177e4SLinus Torvalds 	subdir = *dir;
3391da177e4SLinus Torvalds 	if (!subdir) {
3401da177e4SLinus Torvalds 		if (!page || !(subdir = *page)) {
3411da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
3421da177e4SLinus Torvalds 			return NULL;		/* need a page */
3431da177e4SLinus Torvalds 		}
3441da177e4SLinus Torvalds 		*dir = subdir;
3451da177e4SLinus Torvalds 		*page = NULL;
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 	shmem_dir_unmap(dir);
3481da177e4SLinus Torvalds 	return shmem_swp_map(subdir) + offset;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	long incdec = value? 1: -1;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	entry->val = value;
3561da177e4SLinus Torvalds 	info->swapped += incdec;
3574c21e2f2SHugh Dickins 	if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
3584c21e2f2SHugh Dickins 		struct page *page = kmap_atomic_to_page(entry);
3594c21e2f2SHugh Dickins 		set_page_private(page, page_private(page) + incdec);
3604c21e2f2SHugh Dickins 	}
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds /*
3641da177e4SLinus Torvalds  * shmem_swp_alloc - get the position of the swap entry for the page.
3651da177e4SLinus Torvalds  *                   If it does not exist allocate the entry.
3661da177e4SLinus Torvalds  *
3671da177e4SLinus Torvalds  * @info:	info structure for the inode
3681da177e4SLinus Torvalds  * @index:	index of the page to find
3691da177e4SLinus Torvalds  * @sgp:	check and recheck i_size? skip allocation?
3701da177e4SLinus Torvalds  */
3711da177e4SLinus Torvalds static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
3721da177e4SLinus Torvalds {
3731da177e4SLinus Torvalds 	struct inode *inode = &info->vfs_inode;
3741da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3751da177e4SLinus Torvalds 	struct page *page = NULL;
3761da177e4SLinus Torvalds 	swp_entry_t *entry;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	if (sgp != SGP_WRITE &&
3791da177e4SLinus Torvalds 	    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
3801da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	while (!(entry = shmem_swp_entry(info, index, &page))) {
3831da177e4SLinus Torvalds 		if (sgp == SGP_READ)
3841da177e4SLinus Torvalds 			return shmem_swp_map(ZERO_PAGE(0));
3851da177e4SLinus Torvalds 		/*
3861da177e4SLinus Torvalds 		 * Test free_blocks against 1 not 0, since we have 1 data
3871da177e4SLinus Torvalds 		 * page (and perhaps indirect index pages) yet to allocate:
3881da177e4SLinus Torvalds 		 * a waste to allocate index if we cannot allocate data.
3891da177e4SLinus Torvalds 		 */
3900edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
3911da177e4SLinus Torvalds 			spin_lock(&sbinfo->stat_lock);
3921da177e4SLinus Torvalds 			if (sbinfo->free_blocks <= 1) {
3931da177e4SLinus Torvalds 				spin_unlock(&sbinfo->stat_lock);
3941da177e4SLinus Torvalds 				return ERR_PTR(-ENOSPC);
3951da177e4SLinus Torvalds 			}
3961da177e4SLinus Torvalds 			sbinfo->free_blocks--;
3971da177e4SLinus Torvalds 			inode->i_blocks += BLOCKS_PER_PAGE;
3981da177e4SLinus Torvalds 			spin_unlock(&sbinfo->stat_lock);
3991da177e4SLinus Torvalds 		}
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 		spin_unlock(&info->lock);
402769848c0SMel Gorman 		page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping));
4034c21e2f2SHugh Dickins 		if (page)
4044c21e2f2SHugh Dickins 			set_page_private(page, 0);
4051da177e4SLinus Torvalds 		spin_lock(&info->lock);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 		if (!page) {
4081da177e4SLinus Torvalds 			shmem_free_blocks(inode, 1);
4091da177e4SLinus Torvalds 			return ERR_PTR(-ENOMEM);
4101da177e4SLinus Torvalds 		}
4111da177e4SLinus Torvalds 		if (sgp != SGP_WRITE &&
4121da177e4SLinus Torvalds 		    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
4131da177e4SLinus Torvalds 			entry = ERR_PTR(-EINVAL);
4141da177e4SLinus Torvalds 			break;
4151da177e4SLinus Torvalds 		}
4161da177e4SLinus Torvalds 		if (info->next_index <= index)
4171da177e4SLinus Torvalds 			info->next_index = index + 1;
4181da177e4SLinus Torvalds 	}
4191da177e4SLinus Torvalds 	if (page) {
4201da177e4SLinus Torvalds 		/* another task gave its page, or truncated the file */
4211da177e4SLinus Torvalds 		shmem_free_blocks(inode, 1);
4221da177e4SLinus Torvalds 		shmem_dir_free(page);
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 	if (info->next_index <= index && !IS_ERR(entry))
4251da177e4SLinus Torvalds 		info->next_index = index + 1;
4261da177e4SLinus Torvalds 	return entry;
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds /*
4301da177e4SLinus Torvalds  * shmem_free_swp - free some swap entries in a directory
4311da177e4SLinus Torvalds  *
4321da177e4SLinus Torvalds  * @dir:        pointer to the directory
4331da177e4SLinus Torvalds  * @edir:       pointer after last entry of the directory
4341ae70006SHugh Dickins  * @punch_lock: pointer to spinlock when needed for the holepunch case
4351da177e4SLinus Torvalds  */
4361ae70006SHugh Dickins static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
4371ae70006SHugh Dickins 						spinlock_t *punch_lock)
4381da177e4SLinus Torvalds {
4391ae70006SHugh Dickins 	spinlock_t *punch_unlock = NULL;
4401da177e4SLinus Torvalds 	swp_entry_t *ptr;
4411da177e4SLinus Torvalds 	int freed = 0;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	for (ptr = dir; ptr < edir; ptr++) {
4441da177e4SLinus Torvalds 		if (ptr->val) {
4451ae70006SHugh Dickins 			if (unlikely(punch_lock)) {
4461ae70006SHugh Dickins 				punch_unlock = punch_lock;
4471ae70006SHugh Dickins 				punch_lock = NULL;
4481ae70006SHugh Dickins 				spin_lock(punch_unlock);
4491ae70006SHugh Dickins 				if (!ptr->val)
4501ae70006SHugh Dickins 					continue;
4511ae70006SHugh Dickins 			}
4521da177e4SLinus Torvalds 			free_swap_and_cache(*ptr);
4531da177e4SLinus Torvalds 			*ptr = (swp_entry_t){0};
4541da177e4SLinus Torvalds 			freed++;
4551da177e4SLinus Torvalds 		}
4561da177e4SLinus Torvalds 	}
4571ae70006SHugh Dickins 	if (punch_unlock)
4581ae70006SHugh Dickins 		spin_unlock(punch_unlock);
4591da177e4SLinus Torvalds 	return freed;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621ae70006SHugh Dickins static int shmem_map_and_free_swp(struct page *subdir, int offset,
4631ae70006SHugh Dickins 		int limit, struct page ***dir, spinlock_t *punch_lock)
4641da177e4SLinus Torvalds {
4651da177e4SLinus Torvalds 	swp_entry_t *ptr;
4661da177e4SLinus Torvalds 	int freed = 0;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	ptr = shmem_swp_map(subdir);
4691da177e4SLinus Torvalds 	for (; offset < limit; offset += LATENCY_LIMIT) {
4701da177e4SLinus Torvalds 		int size = limit - offset;
4711da177e4SLinus Torvalds 		if (size > LATENCY_LIMIT)
4721da177e4SLinus Torvalds 			size = LATENCY_LIMIT;
4731ae70006SHugh Dickins 		freed += shmem_free_swp(ptr+offset, ptr+offset+size,
4741ae70006SHugh Dickins 							punch_lock);
4751da177e4SLinus Torvalds 		if (need_resched()) {
4761da177e4SLinus Torvalds 			shmem_swp_unmap(ptr);
4771da177e4SLinus Torvalds 			if (*dir) {
4781da177e4SLinus Torvalds 				shmem_dir_unmap(*dir);
4791da177e4SLinus Torvalds 				*dir = NULL;
4801da177e4SLinus Torvalds 			}
4811da177e4SLinus Torvalds 			cond_resched();
4821da177e4SLinus Torvalds 			ptr = shmem_swp_map(subdir);
4831da177e4SLinus Torvalds 		}
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds 	shmem_swp_unmap(ptr);
4861da177e4SLinus Torvalds 	return freed;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds static void shmem_free_pages(struct list_head *next)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	struct page *page;
4921da177e4SLinus Torvalds 	int freed = 0;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	do {
4951da177e4SLinus Torvalds 		page = container_of(next, struct page, lru);
4961da177e4SLinus Torvalds 		next = next->next;
4971da177e4SLinus Torvalds 		shmem_dir_free(page);
4981da177e4SLinus Torvalds 		freed++;
4991da177e4SLinus Torvalds 		if (freed >= LATENCY_LIMIT) {
5001da177e4SLinus Torvalds 			cond_resched();
5011da177e4SLinus Torvalds 			freed = 0;
5021da177e4SLinus Torvalds 		}
5031da177e4SLinus Torvalds 	} while (next);
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
506f6b3ec23SBadari Pulavarty static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
5071da177e4SLinus Torvalds {
5081da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
5091da177e4SLinus Torvalds 	unsigned long idx;
5101da177e4SLinus Torvalds 	unsigned long size;
5111da177e4SLinus Torvalds 	unsigned long limit;
5121da177e4SLinus Torvalds 	unsigned long stage;
5131da177e4SLinus Torvalds 	unsigned long diroff;
5141da177e4SLinus Torvalds 	struct page **dir;
5151da177e4SLinus Torvalds 	struct page *topdir;
5161da177e4SLinus Torvalds 	struct page *middir;
5171da177e4SLinus Torvalds 	struct page *subdir;
5181da177e4SLinus Torvalds 	swp_entry_t *ptr;
5191da177e4SLinus Torvalds 	LIST_HEAD(pages_to_free);
5201da177e4SLinus Torvalds 	long nr_pages_to_free = 0;
5211da177e4SLinus Torvalds 	long nr_swaps_freed = 0;
5221da177e4SLinus Torvalds 	int offset;
5231da177e4SLinus Torvalds 	int freed;
524a2646d1eSHugh Dickins 	int punch_hole;
5251ae70006SHugh Dickins 	spinlock_t *needs_lock;
5261ae70006SHugh Dickins 	spinlock_t *punch_lock;
527a2646d1eSHugh Dickins 	unsigned long upper_limit;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
530f6b3ec23SBadari Pulavarty 	idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
5311da177e4SLinus Torvalds 	if (idx >= info->next_index)
5321da177e4SLinus Torvalds 		return;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	spin_lock(&info->lock);
5351da177e4SLinus Torvalds 	info->flags |= SHMEM_TRUNCATE;
536f6b3ec23SBadari Pulavarty 	if (likely(end == (loff_t) -1)) {
5371da177e4SLinus Torvalds 		limit = info->next_index;
538a2646d1eSHugh Dickins 		upper_limit = SHMEM_MAX_INDEX;
5391da177e4SLinus Torvalds 		info->next_index = idx;
5401ae70006SHugh Dickins 		needs_lock = NULL;
541a2646d1eSHugh Dickins 		punch_hole = 0;
542f6b3ec23SBadari Pulavarty 	} else {
543a2646d1eSHugh Dickins 		if (end + 1 >= inode->i_size) {	/* we may free a little more */
544a2646d1eSHugh Dickins 			limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
545a2646d1eSHugh Dickins 							PAGE_CACHE_SHIFT;
546a2646d1eSHugh Dickins 			upper_limit = SHMEM_MAX_INDEX;
547a2646d1eSHugh Dickins 		} else {
548a2646d1eSHugh Dickins 			limit = (end + 1) >> PAGE_CACHE_SHIFT;
549a2646d1eSHugh Dickins 			upper_limit = limit;
550a2646d1eSHugh Dickins 		}
5511ae70006SHugh Dickins 		needs_lock = &info->lock;
552f6b3ec23SBadari Pulavarty 		punch_hole = 1;
553f6b3ec23SBadari Pulavarty 	}
554f6b3ec23SBadari Pulavarty 
5551da177e4SLinus Torvalds 	topdir = info->i_indirect;
556f6b3ec23SBadari Pulavarty 	if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
5571da177e4SLinus Torvalds 		info->i_indirect = NULL;
5581da177e4SLinus Torvalds 		nr_pages_to_free++;
5591da177e4SLinus Torvalds 		list_add(&topdir->lru, &pages_to_free);
5601da177e4SLinus Torvalds 	}
5611da177e4SLinus Torvalds 	spin_unlock(&info->lock);
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	if (info->swapped && idx < SHMEM_NR_DIRECT) {
5641da177e4SLinus Torvalds 		ptr = info->i_direct;
5651da177e4SLinus Torvalds 		size = limit;
5661da177e4SLinus Torvalds 		if (size > SHMEM_NR_DIRECT)
5671da177e4SLinus Torvalds 			size = SHMEM_NR_DIRECT;
5681ae70006SHugh Dickins 		nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
5691da177e4SLinus Torvalds 	}
57092a3d03aSBadari Pulavarty 
57192a3d03aSBadari Pulavarty 	/*
57292a3d03aSBadari Pulavarty 	 * If there are no indirect blocks or we are punching a hole
57392a3d03aSBadari Pulavarty 	 * below indirect blocks, nothing to be done.
57492a3d03aSBadari Pulavarty 	 */
575a2646d1eSHugh Dickins 	if (!topdir || limit <= SHMEM_NR_DIRECT)
5761da177e4SLinus Torvalds 		goto done2;
5771da177e4SLinus Torvalds 
5781ae70006SHugh Dickins 	/*
5791ae70006SHugh Dickins 	 * The truncation case has already dropped info->lock, and we're safe
5801ae70006SHugh Dickins 	 * because i_size and next_index have already been lowered, preventing
5811ae70006SHugh Dickins 	 * access beyond.  But in the punch_hole case, we still need to take
5821ae70006SHugh Dickins 	 * the lock when updating the swap directory, because there might be
5831ae70006SHugh Dickins 	 * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
5841ae70006SHugh Dickins 	 * shmem_writepage.  However, whenever we find we can remove a whole
5851ae70006SHugh Dickins 	 * directory page (not at the misaligned start or end of the range),
5861ae70006SHugh Dickins 	 * we first NULLify its pointer in the level above, and then have no
5871ae70006SHugh Dickins 	 * need to take the lock when updating its contents: needs_lock and
5881ae70006SHugh Dickins 	 * punch_lock (either pointing to info->lock or NULL) manage this.
5891ae70006SHugh Dickins 	 */
5901ae70006SHugh Dickins 
591a2646d1eSHugh Dickins 	upper_limit -= SHMEM_NR_DIRECT;
5921da177e4SLinus Torvalds 	limit -= SHMEM_NR_DIRECT;
5931da177e4SLinus Torvalds 	idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
5941da177e4SLinus Torvalds 	offset = idx % ENTRIES_PER_PAGE;
5951da177e4SLinus Torvalds 	idx -= offset;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	dir = shmem_dir_map(topdir);
5981da177e4SLinus Torvalds 	stage = ENTRIES_PER_PAGEPAGE/2;
5991da177e4SLinus Torvalds 	if (idx < ENTRIES_PER_PAGEPAGE/2) {
6001da177e4SLinus Torvalds 		middir = topdir;
6011da177e4SLinus Torvalds 		diroff = idx/ENTRIES_PER_PAGE;
6021da177e4SLinus Torvalds 	} else {
6031da177e4SLinus Torvalds 		dir += ENTRIES_PER_PAGE/2;
6041da177e4SLinus Torvalds 		dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
6051da177e4SLinus Torvalds 		while (stage <= idx)
6061da177e4SLinus Torvalds 			stage += ENTRIES_PER_PAGEPAGE;
6071da177e4SLinus Torvalds 		middir = *dir;
6081da177e4SLinus Torvalds 		if (*dir) {
6091da177e4SLinus Torvalds 			diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
6101da177e4SLinus Torvalds 				ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
611a2646d1eSHugh Dickins 			if (!diroff && !offset && upper_limit >= stage) {
6121ae70006SHugh Dickins 				if (needs_lock) {
6131ae70006SHugh Dickins 					spin_lock(needs_lock);
6141ae70006SHugh Dickins 					*dir = NULL;
6151ae70006SHugh Dickins 					spin_unlock(needs_lock);
6161ae70006SHugh Dickins 					needs_lock = NULL;
6171ae70006SHugh Dickins 				} else
6181da177e4SLinus Torvalds 					*dir = NULL;
6191da177e4SLinus Torvalds 				nr_pages_to_free++;
6201da177e4SLinus Torvalds 				list_add(&middir->lru, &pages_to_free);
6211da177e4SLinus Torvalds 			}
6221da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6231da177e4SLinus Torvalds 			dir = shmem_dir_map(middir);
6241da177e4SLinus Torvalds 		} else {
6251da177e4SLinus Torvalds 			diroff = 0;
6261da177e4SLinus Torvalds 			offset = 0;
6271da177e4SLinus Torvalds 			idx = stage;
6281da177e4SLinus Torvalds 		}
6291da177e4SLinus Torvalds 	}
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
6321da177e4SLinus Torvalds 		if (unlikely(idx == stage)) {
6331da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6341da177e4SLinus Torvalds 			dir = shmem_dir_map(topdir) +
6351da177e4SLinus Torvalds 			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
6361da177e4SLinus Torvalds 			while (!*dir) {
6371da177e4SLinus Torvalds 				dir++;
6381da177e4SLinus Torvalds 				idx += ENTRIES_PER_PAGEPAGE;
6391da177e4SLinus Torvalds 				if (idx >= limit)
6401da177e4SLinus Torvalds 					goto done1;
6411da177e4SLinus Torvalds 			}
6421da177e4SLinus Torvalds 			stage = idx + ENTRIES_PER_PAGEPAGE;
6431da177e4SLinus Torvalds 			middir = *dir;
6441ae70006SHugh Dickins 			if (punch_hole)
6451ae70006SHugh Dickins 				needs_lock = &info->lock;
646a2646d1eSHugh Dickins 			if (upper_limit >= stage) {
6471ae70006SHugh Dickins 				if (needs_lock) {
6481ae70006SHugh Dickins 					spin_lock(needs_lock);
6491ae70006SHugh Dickins 					*dir = NULL;
6501ae70006SHugh Dickins 					spin_unlock(needs_lock);
6511ae70006SHugh Dickins 					needs_lock = NULL;
6521ae70006SHugh Dickins 				} else
6531da177e4SLinus Torvalds 					*dir = NULL;
6541da177e4SLinus Torvalds 				nr_pages_to_free++;
6551da177e4SLinus Torvalds 				list_add(&middir->lru, &pages_to_free);
656a2646d1eSHugh Dickins 			}
6571da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6581da177e4SLinus Torvalds 			cond_resched();
6591da177e4SLinus Torvalds 			dir = shmem_dir_map(middir);
6601da177e4SLinus Torvalds 			diroff = 0;
6611da177e4SLinus Torvalds 		}
6621ae70006SHugh Dickins 		punch_lock = needs_lock;
6631da177e4SLinus Torvalds 		subdir = dir[diroff];
6641ae70006SHugh Dickins 		if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
6651ae70006SHugh Dickins 			if (needs_lock) {
6661ae70006SHugh Dickins 				spin_lock(needs_lock);
6671ae70006SHugh Dickins 				dir[diroff] = NULL;
6681ae70006SHugh Dickins 				spin_unlock(needs_lock);
6691ae70006SHugh Dickins 				punch_lock = NULL;
6701ae70006SHugh Dickins 			} else
6711da177e4SLinus Torvalds 				dir[diroff] = NULL;
6721da177e4SLinus Torvalds 			nr_pages_to_free++;
6731da177e4SLinus Torvalds 			list_add(&subdir->lru, &pages_to_free);
6741da177e4SLinus Torvalds 		}
6751ae70006SHugh Dickins 		if (subdir && page_private(subdir) /* has swap entries */) {
6761ae70006SHugh Dickins 			size = limit - idx;
6771ae70006SHugh Dickins 			if (size > ENTRIES_PER_PAGE)
6781ae70006SHugh Dickins 				size = ENTRIES_PER_PAGE;
6791ae70006SHugh Dickins 			freed = shmem_map_and_free_swp(subdir,
6801ae70006SHugh Dickins 					offset, size, &dir, punch_lock);
6811ae70006SHugh Dickins 			if (!dir)
6821ae70006SHugh Dickins 				dir = shmem_dir_map(middir);
6831ae70006SHugh Dickins 			nr_swaps_freed += freed;
6841ae70006SHugh Dickins 			if (offset || punch_lock) {
6851ae70006SHugh Dickins 				spin_lock(&info->lock);
6861ae70006SHugh Dickins 				set_page_private(subdir,
6871ae70006SHugh Dickins 					page_private(subdir) - freed);
6881ae70006SHugh Dickins 				spin_unlock(&info->lock);
6891ae70006SHugh Dickins 			} else
6901ae70006SHugh Dickins 				BUG_ON(page_private(subdir) != freed);
6911ae70006SHugh Dickins 		}
6921ae70006SHugh Dickins 		offset = 0;
6931da177e4SLinus Torvalds 	}
6941da177e4SLinus Torvalds done1:
6951da177e4SLinus Torvalds 	shmem_dir_unmap(dir);
6961da177e4SLinus Torvalds done2:
6971da177e4SLinus Torvalds 	if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
6981da177e4SLinus Torvalds 		/*
6991da177e4SLinus Torvalds 		 * Call truncate_inode_pages again: racing shmem_unuse_inode
7001da177e4SLinus Torvalds 		 * may have swizzled a page in from swap since vmtruncate or
7011da177e4SLinus Torvalds 		 * generic_delete_inode did it, before we lowered next_index.
7021da177e4SLinus Torvalds 		 * Also, though shmem_getpage checks i_size before adding to
7031da177e4SLinus Torvalds 		 * cache, no recheck after: so fix the narrow window there too.
70416a10019SHugh Dickins 		 *
70516a10019SHugh Dickins 		 * Recalling truncate_inode_pages_range and unmap_mapping_range
70616a10019SHugh Dickins 		 * every time for punch_hole (which never got a chance to clear
70716a10019SHugh Dickins 		 * SHMEM_PAGEIN at the start of vmtruncate_range) is expensive,
70816a10019SHugh Dickins 		 * yet hardly ever necessary: try to optimize them out later.
7091da177e4SLinus Torvalds 		 */
710f6b3ec23SBadari Pulavarty 		truncate_inode_pages_range(inode->i_mapping, start, end);
71116a10019SHugh Dickins 		if (punch_hole)
71216a10019SHugh Dickins 			unmap_mapping_range(inode->i_mapping, start,
71316a10019SHugh Dickins 							end - start, 1);
7141da177e4SLinus Torvalds 	}
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	spin_lock(&info->lock);
7171da177e4SLinus Torvalds 	info->flags &= ~SHMEM_TRUNCATE;
7181da177e4SLinus Torvalds 	info->swapped -= nr_swaps_freed;
7191da177e4SLinus Torvalds 	if (nr_pages_to_free)
7201da177e4SLinus Torvalds 		shmem_free_blocks(inode, nr_pages_to_free);
7211da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
7221da177e4SLinus Torvalds 	spin_unlock(&info->lock);
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	/*
7251da177e4SLinus Torvalds 	 * Empty swap vector directory pages to be freed?
7261da177e4SLinus Torvalds 	 */
7271da177e4SLinus Torvalds 	if (!list_empty(&pages_to_free)) {
7281da177e4SLinus Torvalds 		pages_to_free.prev->next = NULL;
7291da177e4SLinus Torvalds 		shmem_free_pages(pages_to_free.next);
7301da177e4SLinus Torvalds 	}
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
733f6b3ec23SBadari Pulavarty static void shmem_truncate(struct inode *inode)
734f6b3ec23SBadari Pulavarty {
735f6b3ec23SBadari Pulavarty 	shmem_truncate_range(inode, inode->i_size, (loff_t)-1);
736f6b3ec23SBadari Pulavarty }
737f6b3ec23SBadari Pulavarty 
7381da177e4SLinus Torvalds static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
7391da177e4SLinus Torvalds {
7401da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
7411da177e4SLinus Torvalds 	struct page *page = NULL;
7421da177e4SLinus Torvalds 	int error;
7431da177e4SLinus Torvalds 
74439f0247dSAndreas Gruenbacher 	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
7451da177e4SLinus Torvalds 		if (attr->ia_size < inode->i_size) {
7461da177e4SLinus Torvalds 			/*
7471da177e4SLinus Torvalds 			 * If truncating down to a partial page, then
7481da177e4SLinus Torvalds 			 * if that page is already allocated, hold it
7491da177e4SLinus Torvalds 			 * in memory until the truncation is over, so
7501da177e4SLinus Torvalds 			 * truncate_partial_page cannnot miss it were
7511da177e4SLinus Torvalds 			 * it assigned to swap.
7521da177e4SLinus Torvalds 			 */
7531da177e4SLinus Torvalds 			if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
7541da177e4SLinus Torvalds 				(void) shmem_getpage(inode,
7551da177e4SLinus Torvalds 					attr->ia_size>>PAGE_CACHE_SHIFT,
7561da177e4SLinus Torvalds 						&page, SGP_READ, NULL);
757d3602444SHugh Dickins 				if (page)
758d3602444SHugh Dickins 					unlock_page(page);
7591da177e4SLinus Torvalds 			}
7601da177e4SLinus Torvalds 			/*
7611da177e4SLinus Torvalds 			 * Reset SHMEM_PAGEIN flag so that shmem_truncate can
7621da177e4SLinus Torvalds 			 * detect if any pages might have been added to cache
7631da177e4SLinus Torvalds 			 * after truncate_inode_pages.  But we needn't bother
7641da177e4SLinus Torvalds 			 * if it's being fully truncated to zero-length: the
7651da177e4SLinus Torvalds 			 * nrpages check is efficient enough in that case.
7661da177e4SLinus Torvalds 			 */
7671da177e4SLinus Torvalds 			if (attr->ia_size) {
7681da177e4SLinus Torvalds 				struct shmem_inode_info *info = SHMEM_I(inode);
7691da177e4SLinus Torvalds 				spin_lock(&info->lock);
7701da177e4SLinus Torvalds 				info->flags &= ~SHMEM_PAGEIN;
7711da177e4SLinus Torvalds 				spin_unlock(&info->lock);
7721da177e4SLinus Torvalds 			}
7731da177e4SLinus Torvalds 		}
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	error = inode_change_ok(inode, attr);
7771da177e4SLinus Torvalds 	if (!error)
7781da177e4SLinus Torvalds 		error = inode_setattr(inode, attr);
77939f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
78039f0247dSAndreas Gruenbacher 	if (!error && (attr->ia_valid & ATTR_MODE))
78139f0247dSAndreas Gruenbacher 		error = generic_acl_chmod(inode, &shmem_acl_ops);
78239f0247dSAndreas Gruenbacher #endif
7831da177e4SLinus Torvalds 	if (page)
7841da177e4SLinus Torvalds 		page_cache_release(page);
7851da177e4SLinus Torvalds 	return error;
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds static void shmem_delete_inode(struct inode *inode)
7891da177e4SLinus Torvalds {
7901da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	if (inode->i_op->truncate == shmem_truncate) {
793fef26658SMark Fasheh 		truncate_inode_pages(inode->i_mapping, 0);
7941da177e4SLinus Torvalds 		shmem_unacct_size(info->flags, inode->i_size);
7951da177e4SLinus Torvalds 		inode->i_size = 0;
7961da177e4SLinus Torvalds 		shmem_truncate(inode);
7971da177e4SLinus Torvalds 		if (!list_empty(&info->swaplist)) {
7981da177e4SLinus Torvalds 			spin_lock(&shmem_swaplist_lock);
7991da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
8001da177e4SLinus Torvalds 			spin_unlock(&shmem_swaplist_lock);
8011da177e4SLinus Torvalds 		}
8021da177e4SLinus Torvalds 	}
8031da177e4SLinus Torvalds 	BUG_ON(inode->i_blocks);
8045b04c689SPavel Emelyanov 	shmem_free_inode(inode->i_sb);
8051da177e4SLinus Torvalds 	clear_inode(inode);
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
8091da177e4SLinus Torvalds {
8101da177e4SLinus Torvalds 	swp_entry_t *ptr;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	for (ptr = dir; ptr < edir; ptr++) {
8131da177e4SLinus Torvalds 		if (ptr->val == entry.val)
8141da177e4SLinus Torvalds 			return ptr - dir;
8151da177e4SLinus Torvalds 	}
8161da177e4SLinus Torvalds 	return -1;
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
8201da177e4SLinus Torvalds {
8211da177e4SLinus Torvalds 	struct inode *inode;
8221da177e4SLinus Torvalds 	unsigned long idx;
8231da177e4SLinus Torvalds 	unsigned long size;
8241da177e4SLinus Torvalds 	unsigned long limit;
8251da177e4SLinus Torvalds 	unsigned long stage;
8261da177e4SLinus Torvalds 	struct page **dir;
8271da177e4SLinus Torvalds 	struct page *subdir;
8281da177e4SLinus Torvalds 	swp_entry_t *ptr;
8291da177e4SLinus Torvalds 	int offset;
830*d9fe526aSHugh Dickins 	int error;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	idx = 0;
8331da177e4SLinus Torvalds 	ptr = info->i_direct;
8341da177e4SLinus Torvalds 	spin_lock(&info->lock);
8351da177e4SLinus Torvalds 	limit = info->next_index;
8361da177e4SLinus Torvalds 	size = limit;
8371da177e4SLinus Torvalds 	if (size > SHMEM_NR_DIRECT)
8381da177e4SLinus Torvalds 		size = SHMEM_NR_DIRECT;
8391da177e4SLinus Torvalds 	offset = shmem_find_swp(entry, ptr, ptr+size);
8401da177e4SLinus Torvalds 	if (offset >= 0) {
8411da177e4SLinus Torvalds 		shmem_swp_balance_unmap();
8421da177e4SLinus Torvalds 		goto found;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 	if (!info->i_indirect)
8451da177e4SLinus Torvalds 		goto lost2;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	dir = shmem_dir_map(info->i_indirect);
8481da177e4SLinus Torvalds 	stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
8511da177e4SLinus Torvalds 		if (unlikely(idx == stage)) {
8521da177e4SLinus Torvalds 			shmem_dir_unmap(dir-1);
8531da177e4SLinus Torvalds 			dir = shmem_dir_map(info->i_indirect) +
8541da177e4SLinus Torvalds 			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
8551da177e4SLinus Torvalds 			while (!*dir) {
8561da177e4SLinus Torvalds 				dir++;
8571da177e4SLinus Torvalds 				idx += ENTRIES_PER_PAGEPAGE;
8581da177e4SLinus Torvalds 				if (idx >= limit)
8591da177e4SLinus Torvalds 					goto lost1;
8601da177e4SLinus Torvalds 			}
8611da177e4SLinus Torvalds 			stage = idx + ENTRIES_PER_PAGEPAGE;
8621da177e4SLinus Torvalds 			subdir = *dir;
8631da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
8641da177e4SLinus Torvalds 			dir = shmem_dir_map(subdir);
8651da177e4SLinus Torvalds 		}
8661da177e4SLinus Torvalds 		subdir = *dir;
8674c21e2f2SHugh Dickins 		if (subdir && page_private(subdir)) {
8681da177e4SLinus Torvalds 			ptr = shmem_swp_map(subdir);
8691da177e4SLinus Torvalds 			size = limit - idx;
8701da177e4SLinus Torvalds 			if (size > ENTRIES_PER_PAGE)
8711da177e4SLinus Torvalds 				size = ENTRIES_PER_PAGE;
8721da177e4SLinus Torvalds 			offset = shmem_find_swp(entry, ptr, ptr+size);
8731da177e4SLinus Torvalds 			if (offset >= 0) {
8741da177e4SLinus Torvalds 				shmem_dir_unmap(dir);
8751da177e4SLinus Torvalds 				goto found;
8761da177e4SLinus Torvalds 			}
8771da177e4SLinus Torvalds 			shmem_swp_unmap(ptr);
8781da177e4SLinus Torvalds 		}
8791da177e4SLinus Torvalds 	}
8801da177e4SLinus Torvalds lost1:
8811da177e4SLinus Torvalds 	shmem_dir_unmap(dir-1);
8821da177e4SLinus Torvalds lost2:
8831da177e4SLinus Torvalds 	spin_unlock(&info->lock);
8841da177e4SLinus Torvalds 	return 0;
8851da177e4SLinus Torvalds found:
8861da177e4SLinus Torvalds 	idx += offset;
8871da177e4SLinus Torvalds 	inode = &info->vfs_inode;
888*d9fe526aSHugh Dickins 	error = add_to_page_cache(page, inode->i_mapping, idx, GFP_ATOMIC);
889*d9fe526aSHugh Dickins 	if (error == -EEXIST) {
890*d9fe526aSHugh Dickins 		struct page *filepage = find_get_page(inode->i_mapping, idx);
891*d9fe526aSHugh Dickins 		if (filepage) {
892*d9fe526aSHugh Dickins 			/*
893*d9fe526aSHugh Dickins 			 * There might be a more uptodate page coming down
894*d9fe526aSHugh Dickins 			 * from a stacked writepage: forget our swappage if so.
895*d9fe526aSHugh Dickins 			 */
896*d9fe526aSHugh Dickins 			if (PageUptodate(filepage))
897*d9fe526aSHugh Dickins 				error = 0;
898*d9fe526aSHugh Dickins 			page_cache_release(filepage);
899*d9fe526aSHugh Dickins 		}
900*d9fe526aSHugh Dickins 	}
901*d9fe526aSHugh Dickins 	if (!error) {
90273b1262fSHugh Dickins 		delete_from_swap_cache(page);
90373b1262fSHugh Dickins 		set_page_dirty(page);
9041da177e4SLinus Torvalds 		info->flags |= SHMEM_PAGEIN;
9051da177e4SLinus Torvalds 		shmem_swp_set(info, ptr + offset, 0);
9061da177e4SLinus Torvalds 	}
9071da177e4SLinus Torvalds 	shmem_swp_unmap(ptr);
9081da177e4SLinus Torvalds 	spin_unlock(&info->lock);
9091da177e4SLinus Torvalds 	/*
9101da177e4SLinus Torvalds 	 * Decrement swap count even when the entry is left behind:
9111da177e4SLinus Torvalds 	 * try_to_unuse will skip over mms, then reincrement count.
9121da177e4SLinus Torvalds 	 */
9131da177e4SLinus Torvalds 	swap_free(entry);
9141da177e4SLinus Torvalds 	return 1;
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds /*
9181da177e4SLinus Torvalds  * shmem_unuse() search for an eventually swapped out shmem page.
9191da177e4SLinus Torvalds  */
9201da177e4SLinus Torvalds int shmem_unuse(swp_entry_t entry, struct page *page)
9211da177e4SLinus Torvalds {
9221da177e4SLinus Torvalds 	struct list_head *p, *next;
9231da177e4SLinus Torvalds 	struct shmem_inode_info *info;
9241da177e4SLinus Torvalds 	int found = 0;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	spin_lock(&shmem_swaplist_lock);
9271da177e4SLinus Torvalds 	list_for_each_safe(p, next, &shmem_swaplist) {
9281da177e4SLinus Torvalds 		info = list_entry(p, struct shmem_inode_info, swaplist);
9291da177e4SLinus Torvalds 		if (!info->swapped)
9301da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
9311da177e4SLinus Torvalds 		else if (shmem_unuse_inode(info, entry, page)) {
9321da177e4SLinus Torvalds 			/* move head to start search for next from here */
9331da177e4SLinus Torvalds 			list_move_tail(&shmem_swaplist, &info->swaplist);
9341da177e4SLinus Torvalds 			found = 1;
9351da177e4SLinus Torvalds 			break;
9361da177e4SLinus Torvalds 		}
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 	spin_unlock(&shmem_swaplist_lock);
9391da177e4SLinus Torvalds 	return found;
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds /*
9431da177e4SLinus Torvalds  * Move the page from the page cache to the swap cache.
9441da177e4SLinus Torvalds  */
9451da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc)
9461da177e4SLinus Torvalds {
9471da177e4SLinus Torvalds 	struct shmem_inode_info *info;
9481da177e4SLinus Torvalds 	swp_entry_t *entry, swap;
9491da177e4SLinus Torvalds 	struct address_space *mapping;
9501da177e4SLinus Torvalds 	unsigned long index;
9511da177e4SLinus Torvalds 	struct inode *inode;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
9541da177e4SLinus Torvalds 	mapping = page->mapping;
9551da177e4SLinus Torvalds 	index = page->index;
9561da177e4SLinus Torvalds 	inode = mapping->host;
9571da177e4SLinus Torvalds 	info = SHMEM_I(inode);
9581da177e4SLinus Torvalds 	if (info->flags & VM_LOCKED)
9591da177e4SLinus Torvalds 		goto redirty;
960*d9fe526aSHugh Dickins 	if (!total_swap_pages)
9611da177e4SLinus Torvalds 		goto redirty;
9621da177e4SLinus Torvalds 
963*d9fe526aSHugh Dickins 	/*
964*d9fe526aSHugh Dickins 	 * shmem_backing_dev_info's capabilities prevent regular writeback or
965*d9fe526aSHugh Dickins 	 * sync from ever calling shmem_writepage; but a stacking filesystem
966*d9fe526aSHugh Dickins 	 * may use the ->writepage of its underlying filesystem, in which case
967*d9fe526aSHugh Dickins 	 * tmpfs should write out to swap only in response to memory pressure,
968*d9fe526aSHugh Dickins 	 * and not for pdflush or sync.  However, in those cases, we do still
969*d9fe526aSHugh Dickins 	 * want to check if there's a redundant swappage to be discarded.
970*d9fe526aSHugh Dickins 	 */
971*d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
972*d9fe526aSHugh Dickins 		swap = get_swap_page();
973*d9fe526aSHugh Dickins 	else
974*d9fe526aSHugh Dickins 		swap.val = 0;
975*d9fe526aSHugh Dickins 
9761da177e4SLinus Torvalds 	spin_lock(&info->lock);
9771da177e4SLinus Torvalds 	if (index >= info->next_index) {
9781da177e4SLinus Torvalds 		BUG_ON(!(info->flags & SHMEM_TRUNCATE));
9791da177e4SLinus Torvalds 		goto unlock;
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds 	entry = shmem_swp_entry(info, index, NULL);
982*d9fe526aSHugh Dickins 	if (entry->val) {
983*d9fe526aSHugh Dickins 		/*
984*d9fe526aSHugh Dickins 		 * The more uptodate page coming down from a stacked
985*d9fe526aSHugh Dickins 		 * writepage should replace our old swappage.
986*d9fe526aSHugh Dickins 		 */
987*d9fe526aSHugh Dickins 		free_swap_and_cache(*entry);
988*d9fe526aSHugh Dickins 		shmem_swp_set(info, entry, 0);
989*d9fe526aSHugh Dickins 	}
990*d9fe526aSHugh Dickins 	shmem_recalc_inode(inode);
9911da177e4SLinus Torvalds 
992*d9fe526aSHugh Dickins 	if (swap.val && add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
99373b1262fSHugh Dickins 		remove_from_page_cache(page);
9941da177e4SLinus Torvalds 		shmem_swp_set(info, entry, swap.val);
9951da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
9961da177e4SLinus Torvalds 		spin_unlock(&info->lock);
9971da177e4SLinus Torvalds 		if (list_empty(&info->swaplist)) {
9981da177e4SLinus Torvalds 			spin_lock(&shmem_swaplist_lock);
9991da177e4SLinus Torvalds 			/* move instead of add in case we're racing */
10001da177e4SLinus Torvalds 			list_move_tail(&info->swaplist, &shmem_swaplist);
10011da177e4SLinus Torvalds 			spin_unlock(&shmem_swaplist_lock);
10021da177e4SLinus Torvalds 		}
100373b1262fSHugh Dickins 		swap_duplicate(swap);
1004*d9fe526aSHugh Dickins 		BUG_ON(page_mapped(page));
100573b1262fSHugh Dickins 		page_cache_release(page);	/* pagecache ref */
100673b1262fSHugh Dickins 		set_page_dirty(page);
10071da177e4SLinus Torvalds 		unlock_page(page);
10081da177e4SLinus Torvalds 		return 0;
10091da177e4SLinus Torvalds 	}
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	shmem_swp_unmap(entry);
10121da177e4SLinus Torvalds unlock:
10131da177e4SLinus Torvalds 	spin_unlock(&info->lock);
10141da177e4SLinus Torvalds 	swap_free(swap);
10151da177e4SLinus Torvalds redirty:
10161da177e4SLinus Torvalds 	set_page_dirty(page);
1017*d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
1018*d9fe526aSHugh Dickins 		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
1019*d9fe526aSHugh Dickins 	unlock_page(page);
1020*d9fe526aSHugh Dickins 	return 0;
10211da177e4SLinus Torvalds }
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1024d15c023bSHugh Dickins static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes)
1025b00dc3adSHugh Dickins {
1026b00dc3adSHugh Dickins 	char *nodelist = strchr(value, ':');
1027b00dc3adSHugh Dickins 	int err = 1;
1028b00dc3adSHugh Dickins 
1029b00dc3adSHugh Dickins 	if (nodelist) {
1030b00dc3adSHugh Dickins 		/* NUL-terminate policy string */
1031b00dc3adSHugh Dickins 		*nodelist++ = '\0';
1032b00dc3adSHugh Dickins 		if (nodelist_parse(nodelist, *policy_nodes))
1033b00dc3adSHugh Dickins 			goto out;
103437b07e41SLee Schermerhorn 		if (!nodes_subset(*policy_nodes, node_states[N_HIGH_MEMORY]))
1035a210906cSHugh Dickins 			goto out;
1036b00dc3adSHugh Dickins 	}
1037b00dc3adSHugh Dickins 	if (!strcmp(value, "default")) {
1038b00dc3adSHugh Dickins 		*policy = MPOL_DEFAULT;
1039b00dc3adSHugh Dickins 		/* Don't allow a nodelist */
1040b00dc3adSHugh Dickins 		if (!nodelist)
1041b00dc3adSHugh Dickins 			err = 0;
1042b00dc3adSHugh Dickins 	} else if (!strcmp(value, "prefer")) {
1043b00dc3adSHugh Dickins 		*policy = MPOL_PREFERRED;
1044b00dc3adSHugh Dickins 		/* Insist on a nodelist of one node only */
1045b00dc3adSHugh Dickins 		if (nodelist) {
1046b00dc3adSHugh Dickins 			char *rest = nodelist;
1047b00dc3adSHugh Dickins 			while (isdigit(*rest))
1048b00dc3adSHugh Dickins 				rest++;
1049b00dc3adSHugh Dickins 			if (!*rest)
1050b00dc3adSHugh Dickins 				err = 0;
1051b00dc3adSHugh Dickins 		}
1052b00dc3adSHugh Dickins 	} else if (!strcmp(value, "bind")) {
1053b00dc3adSHugh Dickins 		*policy = MPOL_BIND;
1054b00dc3adSHugh Dickins 		/* Insist on a nodelist */
1055b00dc3adSHugh Dickins 		if (nodelist)
1056b00dc3adSHugh Dickins 			err = 0;
1057b00dc3adSHugh Dickins 	} else if (!strcmp(value, "interleave")) {
1058b00dc3adSHugh Dickins 		*policy = MPOL_INTERLEAVE;
105937b07e41SLee Schermerhorn 		/*
106037b07e41SLee Schermerhorn 		 * Default to online nodes with memory if no nodelist
106137b07e41SLee Schermerhorn 		 */
1062b00dc3adSHugh Dickins 		if (!nodelist)
106337b07e41SLee Schermerhorn 			*policy_nodes = node_states[N_HIGH_MEMORY];
1064b00dc3adSHugh Dickins 		err = 0;
1065b00dc3adSHugh Dickins 	}
1066b00dc3adSHugh Dickins out:
1067b00dc3adSHugh Dickins 	/* Restore string for error message */
1068b00dc3adSHugh Dickins 	if (nodelist)
1069b00dc3adSHugh Dickins 		*--nodelist = ':';
1070b00dc3adSHugh Dickins 	return err;
1071b00dc3adSHugh Dickins }
1072b00dc3adSHugh Dickins 
107302098feaSHugh Dickins static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
107402098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	struct vm_area_struct pvma;
1077c4cc6d07SHugh Dickins 	struct page *page;
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	/* Create a pseudo vma that just contains the policy */
1080c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
10811da177e4SLinus Torvalds 	pvma.vm_pgoff = idx;
1082c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
1083c4cc6d07SHugh Dickins 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
108402098feaSHugh Dickins 	page = swapin_readahead(entry, gfp, &pvma, 0);
10851da177e4SLinus Torvalds 	mpol_free(pvma.vm_policy);
10861da177e4SLinus Torvalds 	return page;
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
108902098feaSHugh Dickins static struct page *shmem_alloc_page(gfp_t gfp,
109002098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
10911da177e4SLinus Torvalds {
10921da177e4SLinus Torvalds 	struct vm_area_struct pvma;
10931da177e4SLinus Torvalds 	struct page *page;
10941da177e4SLinus Torvalds 
1095c4cc6d07SHugh Dickins 	/* Create a pseudo vma that just contains the policy */
1096c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
10971da177e4SLinus Torvalds 	pvma.vm_pgoff = idx;
1098c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
1099c4cc6d07SHugh Dickins 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
1100e84e2e13SHugh Dickins 	page = alloc_page_vma(gfp, &pvma, 0);
11011da177e4SLinus Torvalds 	mpol_free(pvma.vm_policy);
11021da177e4SLinus Torvalds 	return page;
11031da177e4SLinus Torvalds }
11041da177e4SLinus Torvalds #else
110537b07e41SLee Schermerhorn static inline int shmem_parse_mpol(char *value, int *policy,
110637b07e41SLee Schermerhorn 						nodemask_t *policy_nodes)
1107b00dc3adSHugh Dickins {
1108b00dc3adSHugh Dickins 	return 1;
1109b00dc3adSHugh Dickins }
1110b00dc3adSHugh Dickins 
111102098feaSHugh Dickins static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
111202098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11131da177e4SLinus Torvalds {
111402098feaSHugh Dickins 	return swapin_readahead(entry, gfp, NULL, 0);
11151da177e4SLinus Torvalds }
11161da177e4SLinus Torvalds 
111702098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp,
111802098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11191da177e4SLinus Torvalds {
1120e84e2e13SHugh Dickins 	return alloc_page(gfp);
11211da177e4SLinus Torvalds }
11221da177e4SLinus Torvalds #endif
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds /*
11251da177e4SLinus Torvalds  * shmem_getpage - either get the page from swap or allocate a new one
11261da177e4SLinus Torvalds  *
11271da177e4SLinus Torvalds  * If we allocate a new one we do not mark it dirty. That's up to the
11281da177e4SLinus Torvalds  * vm. If we swap it in we mark it dirty since we also free the swap
11291da177e4SLinus Torvalds  * entry since a page cannot live in both the swap and page cache
11301da177e4SLinus Torvalds  */
11311da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx,
11321da177e4SLinus Torvalds 			struct page **pagep, enum sgp_type sgp, int *type)
11331da177e4SLinus Torvalds {
11341da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
11351da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
11361da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo;
11371da177e4SLinus Torvalds 	struct page *filepage = *pagep;
11381da177e4SLinus Torvalds 	struct page *swappage;
11391da177e4SLinus Torvalds 	swp_entry_t *entry;
11401da177e4SLinus Torvalds 	swp_entry_t swap;
114102098feaSHugh Dickins 	gfp_t gfp;
11421da177e4SLinus Torvalds 	int error;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	if (idx >= SHMEM_MAX_INDEX)
11451da177e4SLinus Torvalds 		return -EFBIG;
114654cb8821SNick Piggin 
114754cb8821SNick Piggin 	if (type)
114883c54070SNick Piggin 		*type = 0;
114954cb8821SNick Piggin 
11501da177e4SLinus Torvalds 	/*
11511da177e4SLinus Torvalds 	 * Normally, filepage is NULL on entry, and either found
11521da177e4SLinus Torvalds 	 * uptodate immediately, or allocated and zeroed, or read
11531da177e4SLinus Torvalds 	 * in under swappage, which is then assigned to filepage.
11545402b976SHugh Dickins 	 * But shmem_readpage (required for splice) passes in a locked
1155ae976416SHugh Dickins 	 * filepage, which may be found not uptodate by other callers
1156ae976416SHugh Dickins 	 * too, and may need to be copied from the swappage read in.
11571da177e4SLinus Torvalds 	 */
11581da177e4SLinus Torvalds repeat:
11591da177e4SLinus Torvalds 	if (!filepage)
11601da177e4SLinus Torvalds 		filepage = find_lock_page(mapping, idx);
11611da177e4SLinus Torvalds 	if (filepage && PageUptodate(filepage))
11621da177e4SLinus Torvalds 		goto done;
11631da177e4SLinus Torvalds 	error = 0;
116402098feaSHugh Dickins 	gfp = mapping_gfp_mask(mapping);
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 	spin_lock(&info->lock);
11671da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
11681da177e4SLinus Torvalds 	entry = shmem_swp_alloc(info, idx, sgp);
11691da177e4SLinus Torvalds 	if (IS_ERR(entry)) {
11701da177e4SLinus Torvalds 		spin_unlock(&info->lock);
11711da177e4SLinus Torvalds 		error = PTR_ERR(entry);
11721da177e4SLinus Torvalds 		goto failed;
11731da177e4SLinus Torvalds 	}
11741da177e4SLinus Torvalds 	swap = *entry;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	if (swap.val) {
11771da177e4SLinus Torvalds 		/* Look it up and read it in.. */
11781da177e4SLinus Torvalds 		swappage = lookup_swap_cache(swap);
11791da177e4SLinus Torvalds 		if (!swappage) {
11801da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
11811da177e4SLinus Torvalds 			/* here we actually do the io */
118283c54070SNick Piggin 			if (type && !(*type & VM_FAULT_MAJOR)) {
1183f8891e5eSChristoph Lameter 				__count_vm_event(PGMAJFAULT);
118483c54070SNick Piggin 				*type |= VM_FAULT_MAJOR;
11851da177e4SLinus Torvalds 			}
1186f8891e5eSChristoph Lameter 			spin_unlock(&info->lock);
118702098feaSHugh Dickins 			swappage = shmem_swapin(swap, gfp, info, idx);
11881da177e4SLinus Torvalds 			if (!swappage) {
11891da177e4SLinus Torvalds 				spin_lock(&info->lock);
11901da177e4SLinus Torvalds 				entry = shmem_swp_alloc(info, idx, sgp);
11911da177e4SLinus Torvalds 				if (IS_ERR(entry))
11921da177e4SLinus Torvalds 					error = PTR_ERR(entry);
11931da177e4SLinus Torvalds 				else {
11941da177e4SLinus Torvalds 					if (entry->val == swap.val)
11951da177e4SLinus Torvalds 						error = -ENOMEM;
11961da177e4SLinus Torvalds 					shmem_swp_unmap(entry);
11971da177e4SLinus Torvalds 				}
11981da177e4SLinus Torvalds 				spin_unlock(&info->lock);
11991da177e4SLinus Torvalds 				if (error)
12001da177e4SLinus Torvalds 					goto failed;
12011da177e4SLinus Torvalds 				goto repeat;
12021da177e4SLinus Torvalds 			}
12031da177e4SLinus Torvalds 			wait_on_page_locked(swappage);
12041da177e4SLinus Torvalds 			page_cache_release(swappage);
12051da177e4SLinus Torvalds 			goto repeat;
12061da177e4SLinus Torvalds 		}
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 		/* We have to do this with page locked to prevent races */
12091da177e4SLinus Torvalds 		if (TestSetPageLocked(swappage)) {
12101da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
12111da177e4SLinus Torvalds 			spin_unlock(&info->lock);
12121da177e4SLinus Torvalds 			wait_on_page_locked(swappage);
12131da177e4SLinus Torvalds 			page_cache_release(swappage);
12141da177e4SLinus Torvalds 			goto repeat;
12151da177e4SLinus Torvalds 		}
12161da177e4SLinus Torvalds 		if (PageWriteback(swappage)) {
12171da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
12181da177e4SLinus Torvalds 			spin_unlock(&info->lock);
12191da177e4SLinus Torvalds 			wait_on_page_writeback(swappage);
12201da177e4SLinus Torvalds 			unlock_page(swappage);
12211da177e4SLinus Torvalds 			page_cache_release(swappage);
12221da177e4SLinus Torvalds 			goto repeat;
12231da177e4SLinus Torvalds 		}
12241da177e4SLinus Torvalds 		if (!PageUptodate(swappage)) {
12251da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
12261da177e4SLinus Torvalds 			spin_unlock(&info->lock);
12271da177e4SLinus Torvalds 			unlock_page(swappage);
12281da177e4SLinus Torvalds 			page_cache_release(swappage);
12291da177e4SLinus Torvalds 			error = -EIO;
12301da177e4SLinus Torvalds 			goto failed;
12311da177e4SLinus Torvalds 		}
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 		if (filepage) {
12341da177e4SLinus Torvalds 			shmem_swp_set(info, entry, 0);
12351da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
12361da177e4SLinus Torvalds 			delete_from_swap_cache(swappage);
12371da177e4SLinus Torvalds 			spin_unlock(&info->lock);
12381da177e4SLinus Torvalds 			copy_highpage(filepage, swappage);
12391da177e4SLinus Torvalds 			unlock_page(swappage);
12401da177e4SLinus Torvalds 			page_cache_release(swappage);
12411da177e4SLinus Torvalds 			flush_dcache_page(filepage);
12421da177e4SLinus Torvalds 			SetPageUptodate(filepage);
12431da177e4SLinus Torvalds 			set_page_dirty(filepage);
12441da177e4SLinus Torvalds 			swap_free(swap);
124573b1262fSHugh Dickins 		} else if (!(error = add_to_page_cache(
124673b1262fSHugh Dickins 				swappage, mapping, idx, GFP_ATOMIC))) {
12471da177e4SLinus Torvalds 			info->flags |= SHMEM_PAGEIN;
12481da177e4SLinus Torvalds 			shmem_swp_set(info, entry, 0);
12491da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
125073b1262fSHugh Dickins 			delete_from_swap_cache(swappage);
12511da177e4SLinus Torvalds 			spin_unlock(&info->lock);
12521da177e4SLinus Torvalds 			filepage = swappage;
125373b1262fSHugh Dickins 			set_page_dirty(filepage);
12541da177e4SLinus Torvalds 			swap_free(swap);
12551da177e4SLinus Torvalds 		} else {
12561da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
12571da177e4SLinus Torvalds 			spin_unlock(&info->lock);
12581da177e4SLinus Torvalds 			unlock_page(swappage);
12591da177e4SLinus Torvalds 			page_cache_release(swappage);
12601da177e4SLinus Torvalds 			if (error == -ENOMEM) {
12611da177e4SLinus Torvalds 				/* let kswapd refresh zone for GFP_ATOMICs */
12623fcfab16SAndrew Morton 				congestion_wait(WRITE, HZ/50);
12631da177e4SLinus Torvalds 			}
12641da177e4SLinus Torvalds 			goto repeat;
12651da177e4SLinus Torvalds 		}
12661da177e4SLinus Torvalds 	} else if (sgp == SGP_READ && !filepage) {
12671da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
12681da177e4SLinus Torvalds 		filepage = find_get_page(mapping, idx);
12691da177e4SLinus Torvalds 		if (filepage &&
12701da177e4SLinus Torvalds 		    (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
12711da177e4SLinus Torvalds 			spin_unlock(&info->lock);
12721da177e4SLinus Torvalds 			wait_on_page_locked(filepage);
12731da177e4SLinus Torvalds 			page_cache_release(filepage);
12741da177e4SLinus Torvalds 			filepage = NULL;
12751da177e4SLinus Torvalds 			goto repeat;
12761da177e4SLinus Torvalds 		}
12771da177e4SLinus Torvalds 		spin_unlock(&info->lock);
12781da177e4SLinus Torvalds 	} else {
12791da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
12801da177e4SLinus Torvalds 		sbinfo = SHMEM_SB(inode->i_sb);
12810edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
12821da177e4SLinus Torvalds 			spin_lock(&sbinfo->stat_lock);
12831da177e4SLinus Torvalds 			if (sbinfo->free_blocks == 0 ||
12841da177e4SLinus Torvalds 			    shmem_acct_block(info->flags)) {
12851da177e4SLinus Torvalds 				spin_unlock(&sbinfo->stat_lock);
12861da177e4SLinus Torvalds 				spin_unlock(&info->lock);
12871da177e4SLinus Torvalds 				error = -ENOSPC;
12881da177e4SLinus Torvalds 				goto failed;
12891da177e4SLinus Torvalds 			}
12901da177e4SLinus Torvalds 			sbinfo->free_blocks--;
12911da177e4SLinus Torvalds 			inode->i_blocks += BLOCKS_PER_PAGE;
12921da177e4SLinus Torvalds 			spin_unlock(&sbinfo->stat_lock);
12931da177e4SLinus Torvalds 		} else if (shmem_acct_block(info->flags)) {
12941da177e4SLinus Torvalds 			spin_unlock(&info->lock);
12951da177e4SLinus Torvalds 			error = -ENOSPC;
12961da177e4SLinus Torvalds 			goto failed;
12971da177e4SLinus Torvalds 		}
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 		if (!filepage) {
13001da177e4SLinus Torvalds 			spin_unlock(&info->lock);
130102098feaSHugh Dickins 			filepage = shmem_alloc_page(gfp, info, idx);
13021da177e4SLinus Torvalds 			if (!filepage) {
13031da177e4SLinus Torvalds 				shmem_unacct_blocks(info->flags, 1);
13041da177e4SLinus Torvalds 				shmem_free_blocks(inode, 1);
13051da177e4SLinus Torvalds 				error = -ENOMEM;
13061da177e4SLinus Torvalds 				goto failed;
13071da177e4SLinus Torvalds 			}
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 			spin_lock(&info->lock);
13101da177e4SLinus Torvalds 			entry = shmem_swp_alloc(info, idx, sgp);
13111da177e4SLinus Torvalds 			if (IS_ERR(entry))
13121da177e4SLinus Torvalds 				error = PTR_ERR(entry);
13131da177e4SLinus Torvalds 			else {
13141da177e4SLinus Torvalds 				swap = *entry;
13151da177e4SLinus Torvalds 				shmem_swp_unmap(entry);
13161da177e4SLinus Torvalds 			}
13171da177e4SLinus Torvalds 			if (error || swap.val || 0 != add_to_page_cache_lru(
13181da177e4SLinus Torvalds 					filepage, mapping, idx, GFP_ATOMIC)) {
13191da177e4SLinus Torvalds 				spin_unlock(&info->lock);
13201da177e4SLinus Torvalds 				page_cache_release(filepage);
13211da177e4SLinus Torvalds 				shmem_unacct_blocks(info->flags, 1);
13221da177e4SLinus Torvalds 				shmem_free_blocks(inode, 1);
13231da177e4SLinus Torvalds 				filepage = NULL;
13241da177e4SLinus Torvalds 				if (error)
13251da177e4SLinus Torvalds 					goto failed;
13261da177e4SLinus Torvalds 				goto repeat;
13271da177e4SLinus Torvalds 			}
13281da177e4SLinus Torvalds 			info->flags |= SHMEM_PAGEIN;
13291da177e4SLinus Torvalds 		}
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 		info->alloced++;
13321da177e4SLinus Torvalds 		spin_unlock(&info->lock);
1333e84e2e13SHugh Dickins 		clear_highpage(filepage);
13341da177e4SLinus Torvalds 		flush_dcache_page(filepage);
13351da177e4SLinus Torvalds 		SetPageUptodate(filepage);
13361da177e4SLinus Torvalds 	}
13371da177e4SLinus Torvalds done:
13381da177e4SLinus Torvalds 	*pagep = filepage;
13391da177e4SLinus Torvalds 	return 0;
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds failed:
13421da177e4SLinus Torvalds 	if (*pagep != filepage) {
13431da177e4SLinus Torvalds 		unlock_page(filepage);
13441da177e4SLinus Torvalds 		page_cache_release(filepage);
13451da177e4SLinus Torvalds 	}
13461da177e4SLinus Torvalds 	return error;
13471da177e4SLinus Torvalds }
13481da177e4SLinus Torvalds 
1349d0217ac0SNick Piggin static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
13501da177e4SLinus Torvalds {
1351d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
13521da177e4SLinus Torvalds 	int error;
1353d0217ac0SNick Piggin 	int ret;
13541da177e4SLinus Torvalds 
1355d0217ac0SNick Piggin 	if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1356d0217ac0SNick Piggin 		return VM_FAULT_SIGBUS;
1357d00806b1SNick Piggin 
135827d54b39SHugh Dickins 	error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1359d0217ac0SNick Piggin 	if (error)
1360d0217ac0SNick Piggin 		return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
13611da177e4SLinus Torvalds 
1362d0217ac0SNick Piggin 	mark_page_accessed(vmf->page);
136383c54070SNick Piggin 	return ret | VM_FAULT_LOCKED;
13641da177e4SLinus Torvalds }
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1367d8dc74f2SAdrian Bunk static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
13681da177e4SLinus Torvalds {
1369d3ac7f89SJosef "Jeff" Sipek 	struct inode *i = vma->vm_file->f_path.dentry->d_inode;
13701da177e4SLinus Torvalds 	return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
13711da177e4SLinus Torvalds }
13721da177e4SLinus Torvalds 
1373d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1374d8dc74f2SAdrian Bunk 					  unsigned long addr)
13751da177e4SLinus Torvalds {
1376d3ac7f89SJosef "Jeff" Sipek 	struct inode *i = vma->vm_file->f_path.dentry->d_inode;
13771da177e4SLinus Torvalds 	unsigned long idx;
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
13801da177e4SLinus Torvalds 	return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
13811da177e4SLinus Torvalds }
13821da177e4SLinus Torvalds #endif
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user)
13851da177e4SLinus Torvalds {
1386d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
13871da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
13881da177e4SLinus Torvalds 	int retval = -ENOMEM;
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	spin_lock(&info->lock);
13911da177e4SLinus Torvalds 	if (lock && !(info->flags & VM_LOCKED)) {
13921da177e4SLinus Torvalds 		if (!user_shm_lock(inode->i_size, user))
13931da177e4SLinus Torvalds 			goto out_nomem;
13941da177e4SLinus Torvalds 		info->flags |= VM_LOCKED;
13951da177e4SLinus Torvalds 	}
13961da177e4SLinus Torvalds 	if (!lock && (info->flags & VM_LOCKED) && user) {
13971da177e4SLinus Torvalds 		user_shm_unlock(inode->i_size, user);
13981da177e4SLinus Torvalds 		info->flags &= ~VM_LOCKED;
13991da177e4SLinus Torvalds 	}
14001da177e4SLinus Torvalds 	retval = 0;
14011da177e4SLinus Torvalds out_nomem:
14021da177e4SLinus Torvalds 	spin_unlock(&info->lock);
14031da177e4SLinus Torvalds 	return retval;
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds 
14069b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
14071da177e4SLinus Torvalds {
14081da177e4SLinus Torvalds 	file_accessed(file);
14091da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
1410d0217ac0SNick Piggin 	vma->vm_flags |= VM_CAN_NONLINEAR;
14111da177e4SLinus Torvalds 	return 0;
14121da177e4SLinus Torvalds }
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds static struct inode *
14151da177e4SLinus Torvalds shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
14161da177e4SLinus Torvalds {
14171da177e4SLinus Torvalds 	struct inode *inode;
14181da177e4SLinus Torvalds 	struct shmem_inode_info *info;
14191da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
14201da177e4SLinus Torvalds 
14215b04c689SPavel Emelyanov 	if (shmem_reserve_inode(sb))
14221da177e4SLinus Torvalds 		return NULL;
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 	inode = new_inode(sb);
14251da177e4SLinus Torvalds 	if (inode) {
14261da177e4SLinus Torvalds 		inode->i_mode = mode;
14271da177e4SLinus Torvalds 		inode->i_uid = current->fsuid;
14281da177e4SLinus Torvalds 		inode->i_gid = current->fsgid;
14291da177e4SLinus Torvalds 		inode->i_blocks = 0;
14301da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &shmem_aops;
14311da177e4SLinus Torvalds 		inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
14321da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
143391828a40SDavid M. Grimes 		inode->i_generation = get_seconds();
14341da177e4SLinus Torvalds 		info = SHMEM_I(inode);
14351da177e4SLinus Torvalds 		memset(info, 0, (char *)inode - (char *)info);
14361da177e4SLinus Torvalds 		spin_lock_init(&info->lock);
14371da177e4SLinus Torvalds 		INIT_LIST_HEAD(&info->swaplist);
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
14401da177e4SLinus Torvalds 		default:
144139f0247dSAndreas Gruenbacher 			inode->i_op = &shmem_special_inode_operations;
14421da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
14431da177e4SLinus Torvalds 			break;
14441da177e4SLinus Torvalds 		case S_IFREG:
14451da177e4SLinus Torvalds 			inode->i_op = &shmem_inode_operations;
14461da177e4SLinus Torvalds 			inode->i_fop = &shmem_file_operations;
14477339ff83SRobin Holt 			mpol_shared_policy_init(&info->policy, sbinfo->policy,
14487339ff83SRobin Holt 							&sbinfo->policy_nodes);
14491da177e4SLinus Torvalds 			break;
14501da177e4SLinus Torvalds 		case S_IFDIR:
1451d8c76e6fSDave Hansen 			inc_nlink(inode);
14521da177e4SLinus Torvalds 			/* Some things misbehave if size == 0 on a directory */
14531da177e4SLinus Torvalds 			inode->i_size = 2 * BOGO_DIRENT_SIZE;
14541da177e4SLinus Torvalds 			inode->i_op = &shmem_dir_inode_operations;
14551da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
14561da177e4SLinus Torvalds 			break;
14571da177e4SLinus Torvalds 		case S_IFLNK:
14581da177e4SLinus Torvalds 			/*
14591da177e4SLinus Torvalds 			 * Must not load anything in the rbtree,
14601da177e4SLinus Torvalds 			 * mpol_free_shared_policy will not be called.
14611da177e4SLinus Torvalds 			 */
14627339ff83SRobin Holt 			mpol_shared_policy_init(&info->policy, MPOL_DEFAULT,
14637339ff83SRobin Holt 						NULL);
14641da177e4SLinus Torvalds 			break;
14651da177e4SLinus Torvalds 		}
14665b04c689SPavel Emelyanov 	} else
14675b04c689SPavel Emelyanov 		shmem_free_inode(sb);
14681da177e4SLinus Torvalds 	return inode;
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
147292e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations;
147392e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations;
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds /*
1476800d15a5SNick Piggin  * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
1477ae976416SHugh Dickins  * but providing them allows a tmpfs file to be used for splice, sendfile, and
1478ae976416SHugh Dickins  * below the loop driver, in the generic fashion that many filesystems support.
14791da177e4SLinus Torvalds  */
1480ae976416SHugh Dickins static int shmem_readpage(struct file *file, struct page *page)
1481ae976416SHugh Dickins {
1482ae976416SHugh Dickins 	struct inode *inode = page->mapping->host;
1483ae976416SHugh Dickins 	int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
1484ae976416SHugh Dickins 	unlock_page(page);
1485ae976416SHugh Dickins 	return error;
1486ae976416SHugh Dickins }
1487ae976416SHugh Dickins 
14881da177e4SLinus Torvalds static int
1489800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping,
1490800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
1491800d15a5SNick Piggin 			struct page **pagep, void **fsdata)
14921da177e4SLinus Torvalds {
1493800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1494800d15a5SNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1495800d15a5SNick Piggin 	*pagep = NULL;
1496800d15a5SNick Piggin 	return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1497800d15a5SNick Piggin }
1498800d15a5SNick Piggin 
1499800d15a5SNick Piggin static int
1500800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping,
1501800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
1502800d15a5SNick Piggin 			struct page *page, void *fsdata)
1503800d15a5SNick Piggin {
1504800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1505800d15a5SNick Piggin 
1506800d15a5SNick Piggin 	if (pos + copied > inode->i_size)
1507800d15a5SNick Piggin 		i_size_write(inode, pos + copied);
1508800d15a5SNick Piggin 
1509d3602444SHugh Dickins 	unlock_page(page);
1510d3602444SHugh Dickins 	set_page_dirty(page);
1511d3602444SHugh Dickins 	page_cache_release(page);
1512d3602444SHugh Dickins 
1513800d15a5SNick Piggin 	return copied;
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
15171da177e4SLinus Torvalds {
1518d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
15191da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
15201da177e4SLinus Torvalds 	unsigned long index, offset;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	index = *ppos >> PAGE_CACHE_SHIFT;
15231da177e4SLinus Torvalds 	offset = *ppos & ~PAGE_CACHE_MASK;
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	for (;;) {
15261da177e4SLinus Torvalds 		struct page *page = NULL;
15271da177e4SLinus Torvalds 		unsigned long end_index, nr, ret;
15281da177e4SLinus Torvalds 		loff_t i_size = i_size_read(inode);
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
15311da177e4SLinus Torvalds 		if (index > end_index)
15321da177e4SLinus Torvalds 			break;
15331da177e4SLinus Torvalds 		if (index == end_index) {
15341da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
15351da177e4SLinus Torvalds 			if (nr <= offset)
15361da177e4SLinus Torvalds 				break;
15371da177e4SLinus Torvalds 		}
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds 		desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
15401da177e4SLinus Torvalds 		if (desc->error) {
15411da177e4SLinus Torvalds 			if (desc->error == -EINVAL)
15421da177e4SLinus Torvalds 				desc->error = 0;
15431da177e4SLinus Torvalds 			break;
15441da177e4SLinus Torvalds 		}
1545d3602444SHugh Dickins 		if (page)
1546d3602444SHugh Dickins 			unlock_page(page);
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 		/*
15491da177e4SLinus Torvalds 		 * We must evaluate after, since reads (unlike writes)
15501b1dcc1bSJes Sorensen 		 * are called without i_mutex protection against truncate
15511da177e4SLinus Torvalds 		 */
15521da177e4SLinus Torvalds 		nr = PAGE_CACHE_SIZE;
15531da177e4SLinus Torvalds 		i_size = i_size_read(inode);
15541da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
15551da177e4SLinus Torvalds 		if (index == end_index) {
15561da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
15571da177e4SLinus Torvalds 			if (nr <= offset) {
15581da177e4SLinus Torvalds 				if (page)
15591da177e4SLinus Torvalds 					page_cache_release(page);
15601da177e4SLinus Torvalds 				break;
15611da177e4SLinus Torvalds 			}
15621da177e4SLinus Torvalds 		}
15631da177e4SLinus Torvalds 		nr -= offset;
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 		if (page) {
15661da177e4SLinus Torvalds 			/*
15671da177e4SLinus Torvalds 			 * If users can be writing to this page using arbitrary
15681da177e4SLinus Torvalds 			 * virtual addresses, take care about potential aliasing
15691da177e4SLinus Torvalds 			 * before reading the page on the kernel side.
15701da177e4SLinus Torvalds 			 */
15711da177e4SLinus Torvalds 			if (mapping_writably_mapped(mapping))
15721da177e4SLinus Torvalds 				flush_dcache_page(page);
15731da177e4SLinus Torvalds 			/*
15741da177e4SLinus Torvalds 			 * Mark the page accessed if we read the beginning.
15751da177e4SLinus Torvalds 			 */
15761da177e4SLinus Torvalds 			if (!offset)
15771da177e4SLinus Torvalds 				mark_page_accessed(page);
1578b5810039SNick Piggin 		} else {
15791da177e4SLinus Torvalds 			page = ZERO_PAGE(0);
1580b5810039SNick Piggin 			page_cache_get(page);
1581b5810039SNick Piggin 		}
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 		/*
15841da177e4SLinus Torvalds 		 * Ok, we have the page, and it's up-to-date, so
15851da177e4SLinus Torvalds 		 * now we can copy it to user space...
15861da177e4SLinus Torvalds 		 *
15871da177e4SLinus Torvalds 		 * The actor routine returns how many bytes were actually used..
15881da177e4SLinus Torvalds 		 * NOTE! This may not be the same as how much of a user buffer
15891da177e4SLinus Torvalds 		 * we filled up (we may be padding etc), so we can only update
15901da177e4SLinus Torvalds 		 * "pos" here (the actor routine has to update the user buffer
15911da177e4SLinus Torvalds 		 * pointers and the remaining count).
15921da177e4SLinus Torvalds 		 */
15931da177e4SLinus Torvalds 		ret = actor(desc, page, offset, nr);
15941da177e4SLinus Torvalds 		offset += ret;
15951da177e4SLinus Torvalds 		index += offset >> PAGE_CACHE_SHIFT;
15961da177e4SLinus Torvalds 		offset &= ~PAGE_CACHE_MASK;
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 		page_cache_release(page);
15991da177e4SLinus Torvalds 		if (ret != nr || !desc->count)
16001da177e4SLinus Torvalds 			break;
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 		cond_resched();
16031da177e4SLinus Torvalds 	}
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
16061da177e4SLinus Torvalds 	file_accessed(filp);
16071da177e4SLinus Torvalds }
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
16101da177e4SLinus Torvalds {
16111da177e4SLinus Torvalds 	read_descriptor_t desc;
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds 	if ((ssize_t) count < 0)
16141da177e4SLinus Torvalds 		return -EINVAL;
16151da177e4SLinus Torvalds 	if (!access_ok(VERIFY_WRITE, buf, count))
16161da177e4SLinus Torvalds 		return -EFAULT;
16171da177e4SLinus Torvalds 	if (!count)
16181da177e4SLinus Torvalds 		return 0;
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 	desc.written = 0;
16211da177e4SLinus Torvalds 	desc.count = count;
16221da177e4SLinus Torvalds 	desc.arg.buf = buf;
16231da177e4SLinus Torvalds 	desc.error = 0;
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds 	do_shmem_file_read(filp, ppos, &desc, file_read_actor);
16261da177e4SLinus Torvalds 	if (desc.written)
16271da177e4SLinus Torvalds 		return desc.written;
16281da177e4SLinus Torvalds 	return desc.error;
16291da177e4SLinus Torvalds }
16301da177e4SLinus Torvalds 
1631726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
16321da177e4SLinus Torvalds {
1633726c3342SDavid Howells 	struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 	buf->f_type = TMPFS_MAGIC;
16361da177e4SLinus Torvalds 	buf->f_bsize = PAGE_CACHE_SIZE;
16371da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
16381da177e4SLinus Torvalds 	spin_lock(&sbinfo->stat_lock);
16390edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
16401da177e4SLinus Torvalds 		buf->f_blocks = sbinfo->max_blocks;
16411da177e4SLinus Torvalds 		buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
16420edd73b3SHugh Dickins 	}
16430edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
16441da177e4SLinus Torvalds 		buf->f_files = sbinfo->max_inodes;
16451da177e4SLinus Torvalds 		buf->f_ffree = sbinfo->free_inodes;
16461da177e4SLinus Torvalds 	}
16471da177e4SLinus Torvalds 	/* else leave those fields 0 like simple_statfs */
16480edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
16491da177e4SLinus Torvalds 	return 0;
16501da177e4SLinus Torvalds }
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds /*
16531da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
16541da177e4SLinus Torvalds  */
16551da177e4SLinus Torvalds static int
16561da177e4SLinus Torvalds shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
16571da177e4SLinus Torvalds {
16581da177e4SLinus Torvalds 	struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
16591da177e4SLinus Torvalds 	int error = -ENOSPC;
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 	if (inode) {
1662570bc1c2SStephen Smalley 		error = security_inode_init_security(inode, dir, NULL, NULL,
1663570bc1c2SStephen Smalley 						     NULL);
1664570bc1c2SStephen Smalley 		if (error) {
1665570bc1c2SStephen Smalley 			if (error != -EOPNOTSUPP) {
1666570bc1c2SStephen Smalley 				iput(inode);
1667570bc1c2SStephen Smalley 				return error;
1668570bc1c2SStephen Smalley 			}
166939f0247dSAndreas Gruenbacher 		}
167039f0247dSAndreas Gruenbacher 		error = shmem_acl_init(inode, dir);
167139f0247dSAndreas Gruenbacher 		if (error) {
167239f0247dSAndreas Gruenbacher 			iput(inode);
167339f0247dSAndreas Gruenbacher 			return error;
1674570bc1c2SStephen Smalley 		}
16751da177e4SLinus Torvalds 		if (dir->i_mode & S_ISGID) {
16761da177e4SLinus Torvalds 			inode->i_gid = dir->i_gid;
16771da177e4SLinus Torvalds 			if (S_ISDIR(mode))
16781da177e4SLinus Torvalds 				inode->i_mode |= S_ISGID;
16791da177e4SLinus Torvalds 		}
16801da177e4SLinus Torvalds 		dir->i_size += BOGO_DIRENT_SIZE;
16811da177e4SLinus Torvalds 		dir->i_ctime = dir->i_mtime = CURRENT_TIME;
16821da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
16831da177e4SLinus Torvalds 		dget(dentry); /* Extra count - pin the dentry in core */
16841da177e4SLinus Torvalds 	}
16851da177e4SLinus Torvalds 	return error;
16861da177e4SLinus Torvalds }
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
16891da177e4SLinus Torvalds {
16901da177e4SLinus Torvalds 	int error;
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
16931da177e4SLinus Torvalds 		return error;
1694d8c76e6fSDave Hansen 	inc_nlink(dir);
16951da177e4SLinus Torvalds 	return 0;
16961da177e4SLinus Torvalds }
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
16991da177e4SLinus Torvalds 		struct nameidata *nd)
17001da177e4SLinus Torvalds {
17011da177e4SLinus Torvalds 	return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
17021da177e4SLinus Torvalds }
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds /*
17051da177e4SLinus Torvalds  * Link a file..
17061da177e4SLinus Torvalds  */
17071da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
17081da177e4SLinus Torvalds {
17091da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
17105b04c689SPavel Emelyanov 	int ret;
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	/*
17131da177e4SLinus Torvalds 	 * No ordinary (disk based) filesystem counts links as inodes;
17141da177e4SLinus Torvalds 	 * but each new link needs a new dentry, pinning lowmem, and
17151da177e4SLinus Torvalds 	 * tmpfs dentries cannot be pruned until they are unlinked.
17161da177e4SLinus Torvalds 	 */
17175b04c689SPavel Emelyanov 	ret = shmem_reserve_inode(inode->i_sb);
17185b04c689SPavel Emelyanov 	if (ret)
17195b04c689SPavel Emelyanov 		goto out;
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
17221da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1723d8c76e6fSDave Hansen 	inc_nlink(inode);
17241da177e4SLinus Torvalds 	atomic_inc(&inode->i_count);	/* New dentry reference */
17251da177e4SLinus Torvalds 	dget(dentry);		/* Extra pinning count for the created dentry */
17261da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
17275b04c689SPavel Emelyanov out:
17285b04c689SPavel Emelyanov 	return ret;
17291da177e4SLinus Torvalds }
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry)
17321da177e4SLinus Torvalds {
17331da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
17341da177e4SLinus Torvalds 
17355b04c689SPavel Emelyanov 	if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
17365b04c689SPavel Emelyanov 		shmem_free_inode(inode->i_sb);
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	dir->i_size -= BOGO_DIRENT_SIZE;
17391da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
17409a53c3a7SDave Hansen 	drop_nlink(inode);
17411da177e4SLinus Torvalds 	dput(dentry);	/* Undo the count from "create" - this does all the work */
17421da177e4SLinus Torvalds 	return 0;
17431da177e4SLinus Torvalds }
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
17461da177e4SLinus Torvalds {
17471da177e4SLinus Torvalds 	if (!simple_empty(dentry))
17481da177e4SLinus Torvalds 		return -ENOTEMPTY;
17491da177e4SLinus Torvalds 
17509a53c3a7SDave Hansen 	drop_nlink(dentry->d_inode);
17519a53c3a7SDave Hansen 	drop_nlink(dir);
17521da177e4SLinus Torvalds 	return shmem_unlink(dir, dentry);
17531da177e4SLinus Torvalds }
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds /*
17561da177e4SLinus Torvalds  * The VFS layer already does all the dentry stuff for rename,
17571da177e4SLinus Torvalds  * we just have to decrement the usage count for the target if
17581da177e4SLinus Torvalds  * it exists so that the VFS layer correctly free's it when it
17591da177e4SLinus Torvalds  * gets overwritten.
17601da177e4SLinus Torvalds  */
17611da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
17621da177e4SLinus Torvalds {
17631da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
17641da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(inode->i_mode);
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
17671da177e4SLinus Torvalds 		return -ENOTEMPTY;
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
17701da177e4SLinus Torvalds 		(void) shmem_unlink(new_dir, new_dentry);
17711da177e4SLinus Torvalds 		if (they_are_dirs)
17729a53c3a7SDave Hansen 			drop_nlink(old_dir);
17731da177e4SLinus Torvalds 	} else if (they_are_dirs) {
17749a53c3a7SDave Hansen 		drop_nlink(old_dir);
1775d8c76e6fSDave Hansen 		inc_nlink(new_dir);
17761da177e4SLinus Torvalds 	}
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 	old_dir->i_size -= BOGO_DIRENT_SIZE;
17791da177e4SLinus Torvalds 	new_dir->i_size += BOGO_DIRENT_SIZE;
17801da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime =
17811da177e4SLinus Torvalds 	new_dir->i_ctime = new_dir->i_mtime =
17821da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME;
17831da177e4SLinus Torvalds 	return 0;
17841da177e4SLinus Torvalds }
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17871da177e4SLinus Torvalds {
17881da177e4SLinus Torvalds 	int error;
17891da177e4SLinus Torvalds 	int len;
17901da177e4SLinus Torvalds 	struct inode *inode;
17911da177e4SLinus Torvalds 	struct page *page = NULL;
17921da177e4SLinus Torvalds 	char *kaddr;
17931da177e4SLinus Torvalds 	struct shmem_inode_info *info;
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 	len = strlen(symname) + 1;
17961da177e4SLinus Torvalds 	if (len > PAGE_CACHE_SIZE)
17971da177e4SLinus Torvalds 		return -ENAMETOOLONG;
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 	inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
18001da177e4SLinus Torvalds 	if (!inode)
18011da177e4SLinus Torvalds 		return -ENOSPC;
18021da177e4SLinus Torvalds 
1803570bc1c2SStephen Smalley 	error = security_inode_init_security(inode, dir, NULL, NULL,
1804570bc1c2SStephen Smalley 					     NULL);
1805570bc1c2SStephen Smalley 	if (error) {
1806570bc1c2SStephen Smalley 		if (error != -EOPNOTSUPP) {
1807570bc1c2SStephen Smalley 			iput(inode);
1808570bc1c2SStephen Smalley 			return error;
1809570bc1c2SStephen Smalley 		}
1810570bc1c2SStephen Smalley 		error = 0;
1811570bc1c2SStephen Smalley 	}
1812570bc1c2SStephen Smalley 
18131da177e4SLinus Torvalds 	info = SHMEM_I(inode);
18141da177e4SLinus Torvalds 	inode->i_size = len-1;
18151da177e4SLinus Torvalds 	if (len <= (char *)inode - (char *)info) {
18161da177e4SLinus Torvalds 		/* do it inline */
18171da177e4SLinus Torvalds 		memcpy(info, symname, len);
18181da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inline_operations;
18191da177e4SLinus Torvalds 	} else {
18201da177e4SLinus Torvalds 		error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
18211da177e4SLinus Torvalds 		if (error) {
18221da177e4SLinus Torvalds 			iput(inode);
18231da177e4SLinus Torvalds 			return error;
18241da177e4SLinus Torvalds 		}
1825d3602444SHugh Dickins 		unlock_page(page);
18261da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inode_operations;
18271da177e4SLinus Torvalds 		kaddr = kmap_atomic(page, KM_USER0);
18281da177e4SLinus Torvalds 		memcpy(kaddr, symname, len);
18291da177e4SLinus Torvalds 		kunmap_atomic(kaddr, KM_USER0);
18301da177e4SLinus Torvalds 		set_page_dirty(page);
18311da177e4SLinus Torvalds 		page_cache_release(page);
18321da177e4SLinus Torvalds 	}
18331da177e4SLinus Torvalds 	if (dir->i_mode & S_ISGID)
18341da177e4SLinus Torvalds 		inode->i_gid = dir->i_gid;
18351da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
18361da177e4SLinus Torvalds 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
18371da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
18381da177e4SLinus Torvalds 	dget(dentry);
18391da177e4SLinus Torvalds 	return 0;
18401da177e4SLinus Torvalds }
18411da177e4SLinus Torvalds 
1842cc314eefSLinus Torvalds static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
18431da177e4SLinus Torvalds {
18441da177e4SLinus Torvalds 	nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
1845cc314eefSLinus Torvalds 	return NULL;
18461da177e4SLinus Torvalds }
18471da177e4SLinus Torvalds 
1848cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
18491da177e4SLinus Torvalds {
18501da177e4SLinus Torvalds 	struct page *page = NULL;
18511da177e4SLinus Torvalds 	int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
18521da177e4SLinus Torvalds 	nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
1853d3602444SHugh Dickins 	if (page)
1854d3602444SHugh Dickins 		unlock_page(page);
1855cc314eefSLinus Torvalds 	return page;
18561da177e4SLinus Torvalds }
18571da177e4SLinus Torvalds 
1858cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
18591da177e4SLinus Torvalds {
18601da177e4SLinus Torvalds 	if (!IS_ERR(nd_get_link(nd))) {
1861cc314eefSLinus Torvalds 		struct page *page = cookie;
18621da177e4SLinus Torvalds 		kunmap(page);
18631da177e4SLinus Torvalds 		mark_page_accessed(page);
18641da177e4SLinus Torvalds 		page_cache_release(page);
18651da177e4SLinus Torvalds 	}
18661da177e4SLinus Torvalds }
18671da177e4SLinus Torvalds 
186892e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations = {
18691da177e4SLinus Torvalds 	.readlink	= generic_readlink,
18701da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link_inline,
18711da177e4SLinus Torvalds };
18721da177e4SLinus Torvalds 
187392e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = {
18741da177e4SLinus Torvalds 	.truncate	= shmem_truncate,
18751da177e4SLinus Torvalds 	.readlink	= generic_readlink,
18761da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link,
18771da177e4SLinus Torvalds 	.put_link	= shmem_put_link,
18781da177e4SLinus Torvalds };
18791da177e4SLinus Torvalds 
188039f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
188139f0247dSAndreas Gruenbacher /**
188239f0247dSAndreas Gruenbacher  * Superblocks without xattr inode operations will get security.* xattr
188339f0247dSAndreas Gruenbacher  * support from the VFS "for free". As soon as we have any other xattrs
188439f0247dSAndreas Gruenbacher  * like ACLs, we also need to implement the security.* handlers at
188539f0247dSAndreas Gruenbacher  * filesystem level, though.
188639f0247dSAndreas Gruenbacher  */
188739f0247dSAndreas Gruenbacher 
188839f0247dSAndreas Gruenbacher static size_t shmem_xattr_security_list(struct inode *inode, char *list,
188939f0247dSAndreas Gruenbacher 					size_t list_len, const char *name,
189039f0247dSAndreas Gruenbacher 					size_t name_len)
189139f0247dSAndreas Gruenbacher {
189239f0247dSAndreas Gruenbacher 	return security_inode_listsecurity(inode, list, list_len);
189339f0247dSAndreas Gruenbacher }
189439f0247dSAndreas Gruenbacher 
189539f0247dSAndreas Gruenbacher static int shmem_xattr_security_get(struct inode *inode, const char *name,
189639f0247dSAndreas Gruenbacher 				    void *buffer, size_t size)
189739f0247dSAndreas Gruenbacher {
189839f0247dSAndreas Gruenbacher 	if (strcmp(name, "") == 0)
189939f0247dSAndreas Gruenbacher 		return -EINVAL;
190039f0247dSAndreas Gruenbacher 	return security_inode_getsecurity(inode, name, buffer, size,
190139f0247dSAndreas Gruenbacher 					  -EOPNOTSUPP);
190239f0247dSAndreas Gruenbacher }
190339f0247dSAndreas Gruenbacher 
190439f0247dSAndreas Gruenbacher static int shmem_xattr_security_set(struct inode *inode, const char *name,
190539f0247dSAndreas Gruenbacher 				    const void *value, size_t size, int flags)
190639f0247dSAndreas Gruenbacher {
190739f0247dSAndreas Gruenbacher 	if (strcmp(name, "") == 0)
190839f0247dSAndreas Gruenbacher 		return -EINVAL;
190939f0247dSAndreas Gruenbacher 	return security_inode_setsecurity(inode, name, value, size, flags);
191039f0247dSAndreas Gruenbacher }
191139f0247dSAndreas Gruenbacher 
19121f370a23SAdrian Bunk static struct xattr_handler shmem_xattr_security_handler = {
191339f0247dSAndreas Gruenbacher 	.prefix = XATTR_SECURITY_PREFIX,
191439f0247dSAndreas Gruenbacher 	.list   = shmem_xattr_security_list,
191539f0247dSAndreas Gruenbacher 	.get    = shmem_xattr_security_get,
191639f0247dSAndreas Gruenbacher 	.set    = shmem_xattr_security_set,
191739f0247dSAndreas Gruenbacher };
191839f0247dSAndreas Gruenbacher 
191939f0247dSAndreas Gruenbacher static struct xattr_handler *shmem_xattr_handlers[] = {
192039f0247dSAndreas Gruenbacher 	&shmem_xattr_acl_access_handler,
192139f0247dSAndreas Gruenbacher 	&shmem_xattr_acl_default_handler,
192239f0247dSAndreas Gruenbacher 	&shmem_xattr_security_handler,
192339f0247dSAndreas Gruenbacher 	NULL
192439f0247dSAndreas Gruenbacher };
192539f0247dSAndreas Gruenbacher #endif
192639f0247dSAndreas Gruenbacher 
192791828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child)
192891828a40SDavid M. Grimes {
192991828a40SDavid M. Grimes 	return ERR_PTR(-ESTALE);
193091828a40SDavid M. Grimes }
193191828a40SDavid M. Grimes 
193291828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh)
193391828a40SDavid M. Grimes {
193491828a40SDavid M. Grimes 	__u32 *fh = vfh;
193591828a40SDavid M. Grimes 	__u64 inum = fh[2];
193691828a40SDavid M. Grimes 	inum = (inum << 32) | fh[1];
193791828a40SDavid M. Grimes 	return ino->i_ino == inum && fh[0] == ino->i_generation;
193891828a40SDavid M. Grimes }
193991828a40SDavid M. Grimes 
1940480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
1941480b116cSChristoph Hellwig 		struct fid *fid, int fh_len, int fh_type)
194291828a40SDavid M. Grimes {
194391828a40SDavid M. Grimes 	struct inode *inode;
1944480b116cSChristoph Hellwig 	struct dentry *dentry = NULL;
1945480b116cSChristoph Hellwig 	u64 inum = fid->raw[2];
1946480b116cSChristoph Hellwig 	inum = (inum << 32) | fid->raw[1];
194791828a40SDavid M. Grimes 
1948480b116cSChristoph Hellwig 	if (fh_len < 3)
1949480b116cSChristoph Hellwig 		return NULL;
1950480b116cSChristoph Hellwig 
1951480b116cSChristoph Hellwig 	inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
1952480b116cSChristoph Hellwig 			shmem_match, fid->raw);
195391828a40SDavid M. Grimes 	if (inode) {
1954480b116cSChristoph Hellwig 		dentry = d_find_alias(inode);
195591828a40SDavid M. Grimes 		iput(inode);
195691828a40SDavid M. Grimes 	}
195791828a40SDavid M. Grimes 
1958480b116cSChristoph Hellwig 	return dentry;
195991828a40SDavid M. Grimes }
196091828a40SDavid M. Grimes 
196191828a40SDavid M. Grimes static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
196291828a40SDavid M. Grimes 				int connectable)
196391828a40SDavid M. Grimes {
196491828a40SDavid M. Grimes 	struct inode *inode = dentry->d_inode;
196591828a40SDavid M. Grimes 
196691828a40SDavid M. Grimes 	if (*len < 3)
196791828a40SDavid M. Grimes 		return 255;
196891828a40SDavid M. Grimes 
196991828a40SDavid M. Grimes 	if (hlist_unhashed(&inode->i_hash)) {
197091828a40SDavid M. Grimes 		/* Unfortunately insert_inode_hash is not idempotent,
197191828a40SDavid M. Grimes 		 * so as we hash inodes here rather than at creation
197291828a40SDavid M. Grimes 		 * time, we need a lock to ensure we only try
197391828a40SDavid M. Grimes 		 * to do it once
197491828a40SDavid M. Grimes 		 */
197591828a40SDavid M. Grimes 		static DEFINE_SPINLOCK(lock);
197691828a40SDavid M. Grimes 		spin_lock(&lock);
197791828a40SDavid M. Grimes 		if (hlist_unhashed(&inode->i_hash))
197891828a40SDavid M. Grimes 			__insert_inode_hash(inode,
197991828a40SDavid M. Grimes 					    inode->i_ino + inode->i_generation);
198091828a40SDavid M. Grimes 		spin_unlock(&lock);
198191828a40SDavid M. Grimes 	}
198291828a40SDavid M. Grimes 
198391828a40SDavid M. Grimes 	fh[0] = inode->i_generation;
198491828a40SDavid M. Grimes 	fh[1] = inode->i_ino;
198591828a40SDavid M. Grimes 	fh[2] = ((__u64)inode->i_ino) >> 32;
198691828a40SDavid M. Grimes 
198791828a40SDavid M. Grimes 	*len = 3;
198891828a40SDavid M. Grimes 	return 1;
198991828a40SDavid M. Grimes }
199091828a40SDavid M. Grimes 
199139655164SChristoph Hellwig static const struct export_operations shmem_export_ops = {
199291828a40SDavid M. Grimes 	.get_parent     = shmem_get_parent,
199391828a40SDavid M. Grimes 	.encode_fh      = shmem_encode_fh,
1994480b116cSChristoph Hellwig 	.fh_to_dentry	= shmem_fh_to_dentry,
199591828a40SDavid M. Grimes };
199691828a40SDavid M. Grimes 
19977339ff83SRobin Holt static int shmem_parse_options(char *options, int *mode, uid_t *uid,
19987339ff83SRobin Holt 	gid_t *gid, unsigned long *blocks, unsigned long *inodes,
19997339ff83SRobin Holt 	int *policy, nodemask_t *policy_nodes)
20001da177e4SLinus Torvalds {
20011da177e4SLinus Torvalds 	char *this_char, *value, *rest;
20021da177e4SLinus Torvalds 
2003b00dc3adSHugh Dickins 	while (options != NULL) {
2004b00dc3adSHugh Dickins 		this_char = options;
2005b00dc3adSHugh Dickins 		for (;;) {
2006b00dc3adSHugh Dickins 			/*
2007b00dc3adSHugh Dickins 			 * NUL-terminate this option: unfortunately,
2008b00dc3adSHugh Dickins 			 * mount options form a comma-separated list,
2009b00dc3adSHugh Dickins 			 * but mpol's nodelist may also contain commas.
2010b00dc3adSHugh Dickins 			 */
2011b00dc3adSHugh Dickins 			options = strchr(options, ',');
2012b00dc3adSHugh Dickins 			if (options == NULL)
2013b00dc3adSHugh Dickins 				break;
2014b00dc3adSHugh Dickins 			options++;
2015b00dc3adSHugh Dickins 			if (!isdigit(*options)) {
2016b00dc3adSHugh Dickins 				options[-1] = '\0';
2017b00dc3adSHugh Dickins 				break;
2018b00dc3adSHugh Dickins 			}
2019b00dc3adSHugh Dickins 		}
20201da177e4SLinus Torvalds 		if (!*this_char)
20211da177e4SLinus Torvalds 			continue;
20221da177e4SLinus Torvalds 		if ((value = strchr(this_char,'=')) != NULL) {
20231da177e4SLinus Torvalds 			*value++ = 0;
20241da177e4SLinus Torvalds 		} else {
20251da177e4SLinus Torvalds 			printk(KERN_ERR
20261da177e4SLinus Torvalds 			    "tmpfs: No value for mount option '%s'\n",
20271da177e4SLinus Torvalds 			    this_char);
20281da177e4SLinus Torvalds 			return 1;
20291da177e4SLinus Torvalds 		}
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds 		if (!strcmp(this_char,"size")) {
20321da177e4SLinus Torvalds 			unsigned long long size;
20331da177e4SLinus Torvalds 			size = memparse(value,&rest);
20341da177e4SLinus Torvalds 			if (*rest == '%') {
20351da177e4SLinus Torvalds 				size <<= PAGE_SHIFT;
20361da177e4SLinus Torvalds 				size *= totalram_pages;
20371da177e4SLinus Torvalds 				do_div(size, 100);
20381da177e4SLinus Torvalds 				rest++;
20391da177e4SLinus Torvalds 			}
20401da177e4SLinus Torvalds 			if (*rest)
20411da177e4SLinus Torvalds 				goto bad_val;
2042818db359SMichael Marineau 			*blocks = DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
20431da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_blocks")) {
20441da177e4SLinus Torvalds 			*blocks = memparse(value,&rest);
20451da177e4SLinus Torvalds 			if (*rest)
20461da177e4SLinus Torvalds 				goto bad_val;
20471da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_inodes")) {
20481da177e4SLinus Torvalds 			*inodes = memparse(value,&rest);
20491da177e4SLinus Torvalds 			if (*rest)
20501da177e4SLinus Torvalds 				goto bad_val;
20511da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"mode")) {
20521da177e4SLinus Torvalds 			if (!mode)
20531da177e4SLinus Torvalds 				continue;
20541da177e4SLinus Torvalds 			*mode = simple_strtoul(value,&rest,8);
20551da177e4SLinus Torvalds 			if (*rest)
20561da177e4SLinus Torvalds 				goto bad_val;
20571da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"uid")) {
20581da177e4SLinus Torvalds 			if (!uid)
20591da177e4SLinus Torvalds 				continue;
20601da177e4SLinus Torvalds 			*uid = simple_strtoul(value,&rest,0);
20611da177e4SLinus Torvalds 			if (*rest)
20621da177e4SLinus Torvalds 				goto bad_val;
20631da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"gid")) {
20641da177e4SLinus Torvalds 			if (!gid)
20651da177e4SLinus Torvalds 				continue;
20661da177e4SLinus Torvalds 			*gid = simple_strtoul(value,&rest,0);
20671da177e4SLinus Torvalds 			if (*rest)
20681da177e4SLinus Torvalds 				goto bad_val;
20697339ff83SRobin Holt 		} else if (!strcmp(this_char,"mpol")) {
2070b00dc3adSHugh Dickins 			if (shmem_parse_mpol(value,policy,policy_nodes))
20717339ff83SRobin Holt 				goto bad_val;
20721da177e4SLinus Torvalds 		} else {
20731da177e4SLinus Torvalds 			printk(KERN_ERR "tmpfs: Bad mount option %s\n",
20741da177e4SLinus Torvalds 			       this_char);
20751da177e4SLinus Torvalds 			return 1;
20761da177e4SLinus Torvalds 		}
20771da177e4SLinus Torvalds 	}
20781da177e4SLinus Torvalds 	return 0;
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds bad_val:
20811da177e4SLinus Torvalds 	printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
20821da177e4SLinus Torvalds 	       value, this_char);
20831da177e4SLinus Torvalds 	return 1;
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds }
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
20881da177e4SLinus Torvalds {
20891da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
20900edd73b3SHugh Dickins 	unsigned long max_blocks = sbinfo->max_blocks;
20910edd73b3SHugh Dickins 	unsigned long max_inodes = sbinfo->max_inodes;
20927339ff83SRobin Holt 	int policy = sbinfo->policy;
20937339ff83SRobin Holt 	nodemask_t policy_nodes = sbinfo->policy_nodes;
20940edd73b3SHugh Dickins 	unsigned long blocks;
20950edd73b3SHugh Dickins 	unsigned long inodes;
20960edd73b3SHugh Dickins 	int error = -EINVAL;
20971da177e4SLinus Torvalds 
20987339ff83SRobin Holt 	if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks,
20997339ff83SRobin Holt 				&max_inodes, &policy, &policy_nodes))
21000edd73b3SHugh Dickins 		return error;
21010edd73b3SHugh Dickins 
21020edd73b3SHugh Dickins 	spin_lock(&sbinfo->stat_lock);
21030edd73b3SHugh Dickins 	blocks = sbinfo->max_blocks - sbinfo->free_blocks;
21040edd73b3SHugh Dickins 	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
21050edd73b3SHugh Dickins 	if (max_blocks < blocks)
21060edd73b3SHugh Dickins 		goto out;
21070edd73b3SHugh Dickins 	if (max_inodes < inodes)
21080edd73b3SHugh Dickins 		goto out;
21090edd73b3SHugh Dickins 	/*
21100edd73b3SHugh Dickins 	 * Those tests also disallow limited->unlimited while any are in
21110edd73b3SHugh Dickins 	 * use, so i_blocks will always be zero when max_blocks is zero;
21120edd73b3SHugh Dickins 	 * but we must separately disallow unlimited->limited, because
21130edd73b3SHugh Dickins 	 * in that case we have no record of how much is already in use.
21140edd73b3SHugh Dickins 	 */
21150edd73b3SHugh Dickins 	if (max_blocks && !sbinfo->max_blocks)
21160edd73b3SHugh Dickins 		goto out;
21170edd73b3SHugh Dickins 	if (max_inodes && !sbinfo->max_inodes)
21180edd73b3SHugh Dickins 		goto out;
21190edd73b3SHugh Dickins 
21200edd73b3SHugh Dickins 	error = 0;
21210edd73b3SHugh Dickins 	sbinfo->max_blocks  = max_blocks;
21220edd73b3SHugh Dickins 	sbinfo->free_blocks = max_blocks - blocks;
21230edd73b3SHugh Dickins 	sbinfo->max_inodes  = max_inodes;
21240edd73b3SHugh Dickins 	sbinfo->free_inodes = max_inodes - inodes;
21257339ff83SRobin Holt 	sbinfo->policy = policy;
21267339ff83SRobin Holt 	sbinfo->policy_nodes = policy_nodes;
21270edd73b3SHugh Dickins out:
21280edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
21290edd73b3SHugh Dickins 	return error;
21301da177e4SLinus Torvalds }
21311da177e4SLinus Torvalds #endif
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb)
21341da177e4SLinus Torvalds {
21351da177e4SLinus Torvalds 	kfree(sb->s_fs_info);
21361da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
21371da177e4SLinus Torvalds }
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds static int shmem_fill_super(struct super_block *sb,
21401da177e4SLinus Torvalds 			    void *data, int silent)
21411da177e4SLinus Torvalds {
21421da177e4SLinus Torvalds 	struct inode *inode;
21431da177e4SLinus Torvalds 	struct dentry *root;
21441da177e4SLinus Torvalds 	int mode   = S_IRWXUGO | S_ISVTX;
21451da177e4SLinus Torvalds 	uid_t uid = current->fsuid;
21461da177e4SLinus Torvalds 	gid_t gid = current->fsgid;
21471da177e4SLinus Torvalds 	int err = -ENOMEM;
21480edd73b3SHugh Dickins 	struct shmem_sb_info *sbinfo;
21491da177e4SLinus Torvalds 	unsigned long blocks = 0;
21501da177e4SLinus Torvalds 	unsigned long inodes = 0;
21517339ff83SRobin Holt 	int policy = MPOL_DEFAULT;
215237b07e41SLee Schermerhorn 	nodemask_t policy_nodes = node_states[N_HIGH_MEMORY];
21531da177e4SLinus Torvalds 
21540edd73b3SHugh Dickins #ifdef CONFIG_TMPFS
21551da177e4SLinus Torvalds 	/*
21561da177e4SLinus Torvalds 	 * Per default we only allow half of the physical ram per
21571da177e4SLinus Torvalds 	 * tmpfs instance, limiting inodes to one per page of lowmem;
21581da177e4SLinus Torvalds 	 * but the internal instance is left unlimited.
21591da177e4SLinus Torvalds 	 */
21601da177e4SLinus Torvalds 	if (!(sb->s_flags & MS_NOUSER)) {
21611da177e4SLinus Torvalds 		blocks = totalram_pages / 2;
21621da177e4SLinus Torvalds 		inodes = totalram_pages - totalhigh_pages;
21631da177e4SLinus Torvalds 		if (inodes > blocks)
21641da177e4SLinus Torvalds 			inodes = blocks;
21657339ff83SRobin Holt 		if (shmem_parse_options(data, &mode, &uid, &gid, &blocks,
21667339ff83SRobin Holt 					&inodes, &policy, &policy_nodes))
21671da177e4SLinus Torvalds 			return -EINVAL;
21681da177e4SLinus Torvalds 	}
216991828a40SDavid M. Grimes 	sb->s_export_op = &shmem_export_ops;
21700edd73b3SHugh Dickins #else
21710edd73b3SHugh Dickins 	sb->s_flags |= MS_NOUSER;
21720edd73b3SHugh Dickins #endif
21731da177e4SLinus Torvalds 
21740edd73b3SHugh Dickins 	/* Round up to L1_CACHE_BYTES to resist false sharing */
21750edd73b3SHugh Dickins 	sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
21760edd73b3SHugh Dickins 				L1_CACHE_BYTES), GFP_KERNEL);
21771da177e4SLinus Torvalds 	if (!sbinfo)
21781da177e4SLinus Torvalds 		return -ENOMEM;
21790edd73b3SHugh Dickins 
21801da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
21811da177e4SLinus Torvalds 	sbinfo->max_blocks = blocks;
21821da177e4SLinus Torvalds 	sbinfo->free_blocks = blocks;
21831da177e4SLinus Torvalds 	sbinfo->max_inodes = inodes;
21841da177e4SLinus Torvalds 	sbinfo->free_inodes = inodes;
21857339ff83SRobin Holt 	sbinfo->policy = policy;
21867339ff83SRobin Holt 	sbinfo->policy_nodes = policy_nodes;
21871da177e4SLinus Torvalds 
21880edd73b3SHugh Dickins 	sb->s_fs_info = sbinfo;
21891da177e4SLinus Torvalds 	sb->s_maxbytes = SHMEM_MAX_BYTES;
21901da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
21911da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
21921da177e4SLinus Torvalds 	sb->s_magic = TMPFS_MAGIC;
21931da177e4SLinus Torvalds 	sb->s_op = &shmem_ops;
2194cfd95a9cSRobin H. Johnson 	sb->s_time_gran = 1;
219539f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
219639f0247dSAndreas Gruenbacher 	sb->s_xattr = shmem_xattr_handlers;
219739f0247dSAndreas Gruenbacher 	sb->s_flags |= MS_POSIXACL;
219839f0247dSAndreas Gruenbacher #endif
21990edd73b3SHugh Dickins 
22001da177e4SLinus Torvalds 	inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
22011da177e4SLinus Torvalds 	if (!inode)
22021da177e4SLinus Torvalds 		goto failed;
22031da177e4SLinus Torvalds 	inode->i_uid = uid;
22041da177e4SLinus Torvalds 	inode->i_gid = gid;
22051da177e4SLinus Torvalds 	root = d_alloc_root(inode);
22061da177e4SLinus Torvalds 	if (!root)
22071da177e4SLinus Torvalds 		goto failed_iput;
22081da177e4SLinus Torvalds 	sb->s_root = root;
22091da177e4SLinus Torvalds 	return 0;
22101da177e4SLinus Torvalds 
22111da177e4SLinus Torvalds failed_iput:
22121da177e4SLinus Torvalds 	iput(inode);
22131da177e4SLinus Torvalds failed:
22141da177e4SLinus Torvalds 	shmem_put_super(sb);
22151da177e4SLinus Torvalds 	return err;
22161da177e4SLinus Torvalds }
22171da177e4SLinus Torvalds 
2218fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep;
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb)
22211da177e4SLinus Torvalds {
22221da177e4SLinus Torvalds 	struct shmem_inode_info *p;
2223e94b1766SChristoph Lameter 	p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
22241da177e4SLinus Torvalds 	if (!p)
22251da177e4SLinus Torvalds 		return NULL;
22261da177e4SLinus Torvalds 	return &p->vfs_inode;
22271da177e4SLinus Torvalds }
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode)
22301da177e4SLinus Torvalds {
22311da177e4SLinus Torvalds 	if ((inode->i_mode & S_IFMT) == S_IFREG) {
22321da177e4SLinus Torvalds 		/* only struct inode is valid if it's an inline symlink */
22331da177e4SLinus Torvalds 		mpol_free_shared_policy(&SHMEM_I(inode)->policy);
22341da177e4SLinus Torvalds 	}
223539f0247dSAndreas Gruenbacher 	shmem_acl_destroy_inode(inode);
22361da177e4SLinus Torvalds 	kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
22371da177e4SLinus Torvalds }
22381da177e4SLinus Torvalds 
22394ba9b9d0SChristoph Lameter static void init_once(struct kmem_cache *cachep, void *foo)
22401da177e4SLinus Torvalds {
22411da177e4SLinus Torvalds 	struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds 	inode_init_once(&p->vfs_inode);
224439f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
224539f0247dSAndreas Gruenbacher 	p->i_acl = NULL;
224639f0247dSAndreas Gruenbacher 	p->i_default_acl = NULL;
224739f0247dSAndreas Gruenbacher #endif
22481da177e4SLinus Torvalds }
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds static int init_inodecache(void)
22511da177e4SLinus Torvalds {
22521da177e4SLinus Torvalds 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
22531da177e4SLinus Torvalds 				sizeof(struct shmem_inode_info),
2254040b5c6fSAlexey Dobriyan 				0, SLAB_PANIC, init_once);
22551da177e4SLinus Torvalds 	return 0;
22561da177e4SLinus Torvalds }
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds static void destroy_inodecache(void)
22591da177e4SLinus Torvalds {
22601a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(shmem_inode_cachep);
22611da177e4SLinus Torvalds }
22621da177e4SLinus Torvalds 
2263f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = {
22641da177e4SLinus Torvalds 	.writepage	= shmem_writepage,
226576719325SKen Chen 	.set_page_dirty	= __set_page_dirty_no_writeback,
22661da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
2267ae976416SHugh Dickins 	.readpage	= shmem_readpage,
2268800d15a5SNick Piggin 	.write_begin	= shmem_write_begin,
2269800d15a5SNick Piggin 	.write_end	= shmem_write_end,
22701da177e4SLinus Torvalds #endif
2271304dbdb7SLee Schermerhorn 	.migratepage	= migrate_page,
22721da177e4SLinus Torvalds };
22731da177e4SLinus Torvalds 
227415ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = {
22751da177e4SLinus Torvalds 	.mmap		= shmem_mmap,
22761da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
22771da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
22781da177e4SLinus Torvalds 	.read		= shmem_file_read,
22795402b976SHugh Dickins 	.write		= do_sync_write,
22805402b976SHugh Dickins 	.aio_write	= generic_file_aio_write,
22811da177e4SLinus Torvalds 	.fsync		= simple_sync_file,
2282ae976416SHugh Dickins 	.splice_read	= generic_file_splice_read,
2283ae976416SHugh Dickins 	.splice_write	= generic_file_splice_write,
22841da177e4SLinus Torvalds #endif
22851da177e4SLinus Torvalds };
22861da177e4SLinus Torvalds 
228792e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = {
22881da177e4SLinus Torvalds 	.truncate	= shmem_truncate,
22891da177e4SLinus Torvalds 	.setattr	= shmem_notify_change,
2290f6b3ec23SBadari Pulavarty 	.truncate_range	= shmem_truncate_range,
229139f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
229239f0247dSAndreas Gruenbacher 	.setxattr	= generic_setxattr,
229339f0247dSAndreas Gruenbacher 	.getxattr	= generic_getxattr,
229439f0247dSAndreas Gruenbacher 	.listxattr	= generic_listxattr,
229539f0247dSAndreas Gruenbacher 	.removexattr	= generic_removexattr,
229639f0247dSAndreas Gruenbacher 	.permission	= shmem_permission,
229739f0247dSAndreas Gruenbacher #endif
229839f0247dSAndreas Gruenbacher 
22991da177e4SLinus Torvalds };
23001da177e4SLinus Torvalds 
230192e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = {
23021da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
23031da177e4SLinus Torvalds 	.create		= shmem_create,
23041da177e4SLinus Torvalds 	.lookup		= simple_lookup,
23051da177e4SLinus Torvalds 	.link		= shmem_link,
23061da177e4SLinus Torvalds 	.unlink		= shmem_unlink,
23071da177e4SLinus Torvalds 	.symlink	= shmem_symlink,
23081da177e4SLinus Torvalds 	.mkdir		= shmem_mkdir,
23091da177e4SLinus Torvalds 	.rmdir		= shmem_rmdir,
23101da177e4SLinus Torvalds 	.mknod		= shmem_mknod,
23111da177e4SLinus Torvalds 	.rename		= shmem_rename,
23121da177e4SLinus Torvalds #endif
231339f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
231439f0247dSAndreas Gruenbacher 	.setattr	= shmem_notify_change,
231539f0247dSAndreas Gruenbacher 	.setxattr	= generic_setxattr,
231639f0247dSAndreas Gruenbacher 	.getxattr	= generic_getxattr,
231739f0247dSAndreas Gruenbacher 	.listxattr	= generic_listxattr,
231839f0247dSAndreas Gruenbacher 	.removexattr	= generic_removexattr,
231939f0247dSAndreas Gruenbacher 	.permission	= shmem_permission,
232039f0247dSAndreas Gruenbacher #endif
232139f0247dSAndreas Gruenbacher };
232239f0247dSAndreas Gruenbacher 
232392e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = {
232439f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
232539f0247dSAndreas Gruenbacher 	.setattr	= shmem_notify_change,
232639f0247dSAndreas Gruenbacher 	.setxattr	= generic_setxattr,
232739f0247dSAndreas Gruenbacher 	.getxattr	= generic_getxattr,
232839f0247dSAndreas Gruenbacher 	.listxattr	= generic_listxattr,
232939f0247dSAndreas Gruenbacher 	.removexattr	= generic_removexattr,
233039f0247dSAndreas Gruenbacher 	.permission	= shmem_permission,
233139f0247dSAndreas Gruenbacher #endif
23321da177e4SLinus Torvalds };
23331da177e4SLinus Torvalds 
2334759b9775SHugh Dickins static const struct super_operations shmem_ops = {
23351da177e4SLinus Torvalds 	.alloc_inode	= shmem_alloc_inode,
23361da177e4SLinus Torvalds 	.destroy_inode	= shmem_destroy_inode,
23371da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
23381da177e4SLinus Torvalds 	.statfs		= shmem_statfs,
23391da177e4SLinus Torvalds 	.remount_fs	= shmem_remount_fs,
23401da177e4SLinus Torvalds #endif
23411da177e4SLinus Torvalds 	.delete_inode	= shmem_delete_inode,
23421da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
23431da177e4SLinus Torvalds 	.put_super	= shmem_put_super,
23441da177e4SLinus Torvalds };
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds static struct vm_operations_struct shmem_vm_ops = {
234754cb8821SNick Piggin 	.fault		= shmem_fault,
23481da177e4SLinus Torvalds #ifdef CONFIG_NUMA
23491da177e4SLinus Torvalds 	.set_policy     = shmem_set_policy,
23501da177e4SLinus Torvalds 	.get_policy     = shmem_get_policy,
23511da177e4SLinus Torvalds #endif
23521da177e4SLinus Torvalds };
23531da177e4SLinus Torvalds 
23541da177e4SLinus Torvalds 
2355454e2398SDavid Howells static int shmem_get_sb(struct file_system_type *fs_type,
2356454e2398SDavid Howells 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
23571da177e4SLinus Torvalds {
2358454e2398SDavid Howells 	return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt);
23591da177e4SLinus Torvalds }
23601da177e4SLinus Torvalds 
23611da177e4SLinus Torvalds static struct file_system_type tmpfs_fs_type = {
23621da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
23631da177e4SLinus Torvalds 	.name		= "tmpfs",
23641da177e4SLinus Torvalds 	.get_sb		= shmem_get_sb,
23651da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
23661da177e4SLinus Torvalds };
23671da177e4SLinus Torvalds static struct vfsmount *shm_mnt;
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds static int __init init_tmpfs(void)
23701da177e4SLinus Torvalds {
23711da177e4SLinus Torvalds 	int error;
23721da177e4SLinus Torvalds 
2373e0bf68ddSPeter Zijlstra 	error = bdi_init(&shmem_backing_dev_info);
2374e0bf68ddSPeter Zijlstra 	if (error)
2375e0bf68ddSPeter Zijlstra 		goto out4;
2376e0bf68ddSPeter Zijlstra 
23771da177e4SLinus Torvalds 	error = init_inodecache();
23781da177e4SLinus Torvalds 	if (error)
23791da177e4SLinus Torvalds 		goto out3;
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds 	error = register_filesystem(&tmpfs_fs_type);
23821da177e4SLinus Torvalds 	if (error) {
23831da177e4SLinus Torvalds 		printk(KERN_ERR "Could not register tmpfs\n");
23841da177e4SLinus Torvalds 		goto out2;
23851da177e4SLinus Torvalds 	}
238695dc112aSGreg Kroah-Hartman 
23871f5ce9e9STrond Myklebust 	shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
23881da177e4SLinus Torvalds 				tmpfs_fs_type.name, NULL);
23891da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt)) {
23901da177e4SLinus Torvalds 		error = PTR_ERR(shm_mnt);
23911da177e4SLinus Torvalds 		printk(KERN_ERR "Could not kern_mount tmpfs\n");
23921da177e4SLinus Torvalds 		goto out1;
23931da177e4SLinus Torvalds 	}
23941da177e4SLinus Torvalds 	return 0;
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds out1:
23971da177e4SLinus Torvalds 	unregister_filesystem(&tmpfs_fs_type);
23981da177e4SLinus Torvalds out2:
23991da177e4SLinus Torvalds 	destroy_inodecache();
24001da177e4SLinus Torvalds out3:
2401e0bf68ddSPeter Zijlstra 	bdi_destroy(&shmem_backing_dev_info);
2402e0bf68ddSPeter Zijlstra out4:
24031da177e4SLinus Torvalds 	shm_mnt = ERR_PTR(error);
24041da177e4SLinus Torvalds 	return error;
24051da177e4SLinus Torvalds }
24061da177e4SLinus Torvalds module_init(init_tmpfs)
24071da177e4SLinus Torvalds 
24081da177e4SLinus Torvalds /*
24091da177e4SLinus Torvalds  * shmem_file_setup - get an unlinked file living in tmpfs
24101da177e4SLinus Torvalds  *
24111da177e4SLinus Torvalds  * @name: name for dentry (to be seen in /proc/<pid>/maps
24121da177e4SLinus Torvalds  * @size: size to be set for the file
24131da177e4SLinus Torvalds  *
24141da177e4SLinus Torvalds  */
24151da177e4SLinus Torvalds struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
24161da177e4SLinus Torvalds {
24171da177e4SLinus Torvalds 	int error;
24181da177e4SLinus Torvalds 	struct file *file;
24191da177e4SLinus Torvalds 	struct inode *inode;
24201da177e4SLinus Torvalds 	struct dentry *dentry, *root;
24211da177e4SLinus Torvalds 	struct qstr this;
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt))
24241da177e4SLinus Torvalds 		return (void *)shm_mnt;
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds 	if (size < 0 || size > SHMEM_MAX_BYTES)
24271da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
24281da177e4SLinus Torvalds 
24291da177e4SLinus Torvalds 	if (shmem_acct_size(flags, size))
24301da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds 	error = -ENOMEM;
24331da177e4SLinus Torvalds 	this.name = name;
24341da177e4SLinus Torvalds 	this.len = strlen(name);
24351da177e4SLinus Torvalds 	this.hash = 0; /* will go */
24361da177e4SLinus Torvalds 	root = shm_mnt->mnt_root;
24371da177e4SLinus Torvalds 	dentry = d_alloc(root, &this);
24381da177e4SLinus Torvalds 	if (!dentry)
24391da177e4SLinus Torvalds 		goto put_memory;
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 	error = -ENFILE;
24421da177e4SLinus Torvalds 	file = get_empty_filp();
24431da177e4SLinus Torvalds 	if (!file)
24441da177e4SLinus Torvalds 		goto put_dentry;
24451da177e4SLinus Torvalds 
24461da177e4SLinus Torvalds 	error = -ENOSPC;
24471da177e4SLinus Torvalds 	inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
24481da177e4SLinus Torvalds 	if (!inode)
24491da177e4SLinus Torvalds 		goto close_file;
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 	SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
24521da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
24531da177e4SLinus Torvalds 	inode->i_size = size;
24541da177e4SLinus Torvalds 	inode->i_nlink = 0;	/* It is unlinked */
2455ce8d2cdfSDave Hansen 	init_file(file, shm_mnt, dentry, FMODE_WRITE | FMODE_READ,
2456ce8d2cdfSDave Hansen 			&shmem_file_operations);
24571da177e4SLinus Torvalds 	return file;
24581da177e4SLinus Torvalds 
24591da177e4SLinus Torvalds close_file:
24601da177e4SLinus Torvalds 	put_filp(file);
24611da177e4SLinus Torvalds put_dentry:
24621da177e4SLinus Torvalds 	dput(dentry);
24631da177e4SLinus Torvalds put_memory:
24641da177e4SLinus Torvalds 	shmem_unacct_size(flags, size);
24651da177e4SLinus Torvalds 	return ERR_PTR(error);
24661da177e4SLinus Torvalds }
24671da177e4SLinus Torvalds 
24681da177e4SLinus Torvalds /*
24691da177e4SLinus Torvalds  * shmem_zero_setup - setup a shared anonymous mapping
24701da177e4SLinus Torvalds  *
24711da177e4SLinus Torvalds  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
24721da177e4SLinus Torvalds  */
24731da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma)
24741da177e4SLinus Torvalds {
24751da177e4SLinus Torvalds 	struct file *file;
24761da177e4SLinus Torvalds 	loff_t size = vma->vm_end - vma->vm_start;
24771da177e4SLinus Torvalds 
24781da177e4SLinus Torvalds 	file = shmem_file_setup("dev/zero", size, vma->vm_flags);
24791da177e4SLinus Torvalds 	if (IS_ERR(file))
24801da177e4SLinus Torvalds 		return PTR_ERR(file);
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds 	if (vma->vm_file)
24831da177e4SLinus Torvalds 		fput(vma->vm_file);
24841da177e4SLinus Torvalds 	vma->vm_file = file;
24851da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
24861da177e4SLinus Torvalds 	return 0;
24871da177e4SLinus Torvalds }
2488