xref: /openbmc/linux/mm/shmem.c (revision f6b3ec238d12c8cc6cc71490c6e3127988460349)
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/config.h>
271da177e4SLinus Torvalds #include <linux/module.h>
281da177e4SLinus Torvalds #include <linux/init.h>
291da177e4SLinus Torvalds #include <linux/devfs_fs_kernel.h>
301da177e4SLinus Torvalds #include <linux/fs.h>
311da177e4SLinus Torvalds #include <linux/mm.h>
321da177e4SLinus Torvalds #include <linux/mman.h>
331da177e4SLinus Torvalds #include <linux/file.h>
341da177e4SLinus Torvalds #include <linux/swap.h>
351da177e4SLinus Torvalds #include <linux/pagemap.h>
361da177e4SLinus Torvalds #include <linux/string.h>
371da177e4SLinus Torvalds #include <linux/slab.h>
381da177e4SLinus Torvalds #include <linux/backing-dev.h>
391da177e4SLinus Torvalds #include <linux/shmem_fs.h>
401da177e4SLinus Torvalds #include <linux/mount.h>
411da177e4SLinus Torvalds #include <linux/writeback.h>
421da177e4SLinus Torvalds #include <linux/vfs.h>
431da177e4SLinus Torvalds #include <linux/blkdev.h>
441da177e4SLinus Torvalds #include <linux/security.h>
451da177e4SLinus Torvalds #include <linux/swapops.h>
461da177e4SLinus Torvalds #include <linux/mempolicy.h>
471da177e4SLinus Torvalds #include <linux/namei.h>
481da177e4SLinus Torvalds #include <asm/uaccess.h>
491da177e4SLinus Torvalds #include <asm/div64.h>
501da177e4SLinus Torvalds #include <asm/pgtable.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /* This magic number is used in glibc for posix shared memory */
531da177e4SLinus Torvalds #define TMPFS_MAGIC	0x01021994
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
561da177e4SLinus Torvalds #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
571da177e4SLinus Torvalds #define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #define SHMEM_MAX_INDEX  (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
601da177e4SLinus Torvalds #define SHMEM_MAX_BYTES  ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
651da177e4SLinus Torvalds #define SHMEM_PAGEIN	 VM_READ
661da177e4SLinus Torvalds #define SHMEM_TRUNCATE	 VM_WRITE
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* Definition to limit shmem_truncate's steps between cond_rescheds */
691da177e4SLinus Torvalds #define LATENCY_LIMIT	 64
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */
721da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
751da177e4SLinus Torvalds enum sgp_type {
761da177e4SLinus Torvalds 	SGP_QUICK,	/* don't try more than file page cache lookup */
771da177e4SLinus Torvalds 	SGP_READ,	/* don't exceed i_size, don't allocate page */
781da177e4SLinus Torvalds 	SGP_CACHE,	/* don't exceed i_size, may allocate page */
791da177e4SLinus Torvalds 	SGP_WRITE,	/* may exceed i_size, may allocate page */
801da177e4SLinus Torvalds };
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx,
831da177e4SLinus Torvalds 			 struct page **pagep, enum sgp_type sgp, int *type);
841da177e4SLinus Torvalds 
856daa0e28SAl Viro static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	/*
881da177e4SLinus Torvalds 	 * The above definition of ENTRIES_PER_PAGE, and the use of
891da177e4SLinus Torvalds 	 * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
901da177e4SLinus Torvalds 	 * might be reconsidered if it ever diverges from PAGE_SIZE.
911da177e4SLinus Torvalds 	 */
921da177e4SLinus Torvalds 	return alloc_pages(gfp_mask, PAGE_CACHE_SHIFT-PAGE_SHIFT);
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static inline void shmem_dir_free(struct page *page)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	__free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static struct page **shmem_dir_map(struct page *page)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	return (struct page **)kmap_atomic(page, KM_USER0);
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds static inline void shmem_dir_unmap(struct page **dir)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	kunmap_atomic(dir, KM_USER0);
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds static swp_entry_t *shmem_swp_map(struct page *page)
1111da177e4SLinus Torvalds {
1121da177e4SLinus Torvalds 	return (swp_entry_t *)kmap_atomic(page, KM_USER1);
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds static inline void shmem_swp_balance_unmap(void)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	/*
1181da177e4SLinus Torvalds 	 * When passing a pointer to an i_direct entry, to code which
1191da177e4SLinus Torvalds 	 * also handles indirect entries and so will shmem_swp_unmap,
1201da177e4SLinus Torvalds 	 * we must arrange for the preempt count to remain in balance.
1211da177e4SLinus Torvalds 	 * What kmap_atomic of a lowmem page does depends on config
1221da177e4SLinus Torvalds 	 * and architecture, so pretend to kmap_atomic some lowmem page.
1231da177e4SLinus Torvalds 	 */
1241da177e4SLinus Torvalds 	(void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static inline void shmem_swp_unmap(swp_entry_t *entry)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	kunmap_atomic(entry, KM_USER1);
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	return sb->s_fs_info;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /*
1381da177e4SLinus Torvalds  * shmem_file_setup pre-accounts the whole fixed size of a VM object,
1391da177e4SLinus Torvalds  * for shared memory and for shared anonymous (/dev/zero) mappings
1401da177e4SLinus Torvalds  * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
1411da177e4SLinus Torvalds  * consistent with the pre-accounting of private mappings ...
1421da177e4SLinus Torvalds  */
1431da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	return (flags & VM_ACCOUNT)?
1461da177e4SLinus Torvalds 		security_vm_enough_memory(VM_ACCT(size)): 0;
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	if (flags & VM_ACCOUNT)
1521da177e4SLinus Torvalds 		vm_unacct_memory(VM_ACCT(size));
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds /*
1561da177e4SLinus Torvalds  * ... whereas tmpfs objects are accounted incrementally as
1571da177e4SLinus Torvalds  * pages are allocated, in order to allow huge sparse files.
1581da177e4SLinus Torvalds  * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
1591da177e4SLinus Torvalds  * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
1601da177e4SLinus Torvalds  */
1611da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	return (flags & VM_ACCOUNT)?
1641da177e4SLinus Torvalds 		0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	if (!(flags & VM_ACCOUNT))
1701da177e4SLinus Torvalds 		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds static struct super_operations shmem_ops;
1741da177e4SLinus Torvalds static struct address_space_operations shmem_aops;
1751da177e4SLinus Torvalds static struct file_operations shmem_file_operations;
1761da177e4SLinus Torvalds static struct inode_operations shmem_inode_operations;
1771da177e4SLinus Torvalds static struct inode_operations shmem_dir_inode_operations;
1781da177e4SLinus Torvalds static struct vm_operations_struct shmem_vm_ops;
1791da177e4SLinus Torvalds 
1806c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info  __read_mostly = {
1811da177e4SLinus Torvalds 	.ra_pages	= 0,	/* No readahead */
1821da177e4SLinus Torvalds 	.capabilities	= BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
1831da177e4SLinus Torvalds 	.unplug_io_fn	= default_unplug_io_fn,
1841da177e4SLinus Torvalds };
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist);
1871da177e4SLinus Torvalds static DEFINE_SPINLOCK(shmem_swaplist_lock);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds static void shmem_free_blocks(struct inode *inode, long pages)
1901da177e4SLinus Torvalds {
1911da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
1920edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
1931da177e4SLinus Torvalds 		spin_lock(&sbinfo->stat_lock);
1941da177e4SLinus Torvalds 		sbinfo->free_blocks += pages;
1951da177e4SLinus Torvalds 		inode->i_blocks -= pages*BLOCKS_PER_PAGE;
1961da177e4SLinus Torvalds 		spin_unlock(&sbinfo->stat_lock);
1971da177e4SLinus Torvalds 	}
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds /*
2011da177e4SLinus Torvalds  * shmem_recalc_inode - recalculate the size of an inode
2021da177e4SLinus Torvalds  *
2031da177e4SLinus Torvalds  * @inode: inode to recalc
2041da177e4SLinus Torvalds  *
2051da177e4SLinus Torvalds  * We have to calculate the free blocks since the mm can drop
2061da177e4SLinus Torvalds  * undirtied hole pages behind our back.
2071da177e4SLinus Torvalds  *
2081da177e4SLinus Torvalds  * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
2091da177e4SLinus Torvalds  * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
2101da177e4SLinus Torvalds  *
2111da177e4SLinus Torvalds  * It has to be called with the spinlock held.
2121da177e4SLinus Torvalds  */
2131da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
2161da177e4SLinus Torvalds 	long freed;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
2191da177e4SLinus Torvalds 	if (freed > 0) {
2201da177e4SLinus Torvalds 		info->alloced -= freed;
2211da177e4SLinus Torvalds 		shmem_unacct_blocks(info->flags, freed);
2221da177e4SLinus Torvalds 		shmem_free_blocks(inode, freed);
2231da177e4SLinus Torvalds 	}
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds /*
2271da177e4SLinus Torvalds  * shmem_swp_entry - find the swap vector position in the info structure
2281da177e4SLinus Torvalds  *
2291da177e4SLinus Torvalds  * @info:  info structure for the inode
2301da177e4SLinus Torvalds  * @index: index of the page to find
2311da177e4SLinus Torvalds  * @page:  optional page to add to the structure. Has to be preset to
2321da177e4SLinus Torvalds  *         all zeros
2331da177e4SLinus Torvalds  *
2341da177e4SLinus Torvalds  * If there is no space allocated yet it will return NULL when
2351da177e4SLinus Torvalds  * page is NULL, else it will use the page for the needed block,
2361da177e4SLinus Torvalds  * setting it to NULL on return to indicate that it has been used.
2371da177e4SLinus Torvalds  *
2381da177e4SLinus Torvalds  * The swap vector is organized the following way:
2391da177e4SLinus Torvalds  *
2401da177e4SLinus Torvalds  * There are SHMEM_NR_DIRECT entries directly stored in the
2411da177e4SLinus Torvalds  * shmem_inode_info structure. So small files do not need an addional
2421da177e4SLinus Torvalds  * allocation.
2431da177e4SLinus Torvalds  *
2441da177e4SLinus Torvalds  * For pages with index > SHMEM_NR_DIRECT there is the pointer
2451da177e4SLinus Torvalds  * i_indirect which points to a page which holds in the first half
2461da177e4SLinus Torvalds  * doubly indirect blocks, in the second half triple indirect blocks:
2471da177e4SLinus Torvalds  *
2481da177e4SLinus Torvalds  * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
2491da177e4SLinus Torvalds  * following layout (for SHMEM_NR_DIRECT == 16):
2501da177e4SLinus Torvalds  *
2511da177e4SLinus Torvalds  * i_indirect -> dir --> 16-19
2521da177e4SLinus Torvalds  * 	      |	     +-> 20-23
2531da177e4SLinus Torvalds  * 	      |
2541da177e4SLinus Torvalds  * 	      +-->dir2 --> 24-27
2551da177e4SLinus Torvalds  * 	      |	       +-> 28-31
2561da177e4SLinus Torvalds  * 	      |	       +-> 32-35
2571da177e4SLinus Torvalds  * 	      |	       +-> 36-39
2581da177e4SLinus Torvalds  * 	      |
2591da177e4SLinus Torvalds  * 	      +-->dir3 --> 40-43
2601da177e4SLinus Torvalds  * 	       	       +-> 44-47
2611da177e4SLinus Torvalds  * 	      	       +-> 48-51
2621da177e4SLinus Torvalds  * 	      	       +-> 52-55
2631da177e4SLinus Torvalds  */
2641da177e4SLinus Torvalds static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	unsigned long offset;
2671da177e4SLinus Torvalds 	struct page **dir;
2681da177e4SLinus Torvalds 	struct page *subdir;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	if (index < SHMEM_NR_DIRECT) {
2711da177e4SLinus Torvalds 		shmem_swp_balance_unmap();
2721da177e4SLinus Torvalds 		return info->i_direct+index;
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 	if (!info->i_indirect) {
2751da177e4SLinus Torvalds 		if (page) {
2761da177e4SLinus Torvalds 			info->i_indirect = *page;
2771da177e4SLinus Torvalds 			*page = NULL;
2781da177e4SLinus Torvalds 		}
2791da177e4SLinus Torvalds 		return NULL;			/* need another page */
2801da177e4SLinus Torvalds 	}
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	index -= SHMEM_NR_DIRECT;
2831da177e4SLinus Torvalds 	offset = index % ENTRIES_PER_PAGE;
2841da177e4SLinus Torvalds 	index /= ENTRIES_PER_PAGE;
2851da177e4SLinus Torvalds 	dir = shmem_dir_map(info->i_indirect);
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	if (index >= ENTRIES_PER_PAGE/2) {
2881da177e4SLinus Torvalds 		index -= ENTRIES_PER_PAGE/2;
2891da177e4SLinus Torvalds 		dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
2901da177e4SLinus Torvalds 		index %= ENTRIES_PER_PAGE;
2911da177e4SLinus Torvalds 		subdir = *dir;
2921da177e4SLinus Torvalds 		if (!subdir) {
2931da177e4SLinus Torvalds 			if (page) {
2941da177e4SLinus Torvalds 				*dir = *page;
2951da177e4SLinus Torvalds 				*page = NULL;
2961da177e4SLinus Torvalds 			}
2971da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
2981da177e4SLinus Torvalds 			return NULL;		/* need another page */
2991da177e4SLinus Torvalds 		}
3001da177e4SLinus Torvalds 		shmem_dir_unmap(dir);
3011da177e4SLinus Torvalds 		dir = shmem_dir_map(subdir);
3021da177e4SLinus Torvalds 	}
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	dir += index;
3051da177e4SLinus Torvalds 	subdir = *dir;
3061da177e4SLinus Torvalds 	if (!subdir) {
3071da177e4SLinus Torvalds 		if (!page || !(subdir = *page)) {
3081da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
3091da177e4SLinus Torvalds 			return NULL;		/* need a page */
3101da177e4SLinus Torvalds 		}
3111da177e4SLinus Torvalds 		*dir = subdir;
3121da177e4SLinus Torvalds 		*page = NULL;
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 	shmem_dir_unmap(dir);
3151da177e4SLinus Torvalds 	return shmem_swp_map(subdir) + offset;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
3191da177e4SLinus Torvalds {
3201da177e4SLinus Torvalds 	long incdec = value? 1: -1;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	entry->val = value;
3231da177e4SLinus Torvalds 	info->swapped += incdec;
3244c21e2f2SHugh Dickins 	if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
3254c21e2f2SHugh Dickins 		struct page *page = kmap_atomic_to_page(entry);
3264c21e2f2SHugh Dickins 		set_page_private(page, page_private(page) + incdec);
3274c21e2f2SHugh Dickins 	}
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds /*
3311da177e4SLinus Torvalds  * shmem_swp_alloc - get the position of the swap entry for the page.
3321da177e4SLinus Torvalds  *                   If it does not exist allocate the entry.
3331da177e4SLinus Torvalds  *
3341da177e4SLinus Torvalds  * @info:	info structure for the inode
3351da177e4SLinus Torvalds  * @index:	index of the page to find
3361da177e4SLinus Torvalds  * @sgp:	check and recheck i_size? skip allocation?
3371da177e4SLinus Torvalds  */
3381da177e4SLinus Torvalds static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	struct inode *inode = &info->vfs_inode;
3411da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3421da177e4SLinus Torvalds 	struct page *page = NULL;
3431da177e4SLinus Torvalds 	swp_entry_t *entry;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	if (sgp != SGP_WRITE &&
3461da177e4SLinus Torvalds 	    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
3471da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	while (!(entry = shmem_swp_entry(info, index, &page))) {
3501da177e4SLinus Torvalds 		if (sgp == SGP_READ)
3511da177e4SLinus Torvalds 			return shmem_swp_map(ZERO_PAGE(0));
3521da177e4SLinus Torvalds 		/*
3531da177e4SLinus Torvalds 		 * Test free_blocks against 1 not 0, since we have 1 data
3541da177e4SLinus Torvalds 		 * page (and perhaps indirect index pages) yet to allocate:
3551da177e4SLinus Torvalds 		 * a waste to allocate index if we cannot allocate data.
3561da177e4SLinus Torvalds 		 */
3570edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
3581da177e4SLinus Torvalds 			spin_lock(&sbinfo->stat_lock);
3591da177e4SLinus Torvalds 			if (sbinfo->free_blocks <= 1) {
3601da177e4SLinus Torvalds 				spin_unlock(&sbinfo->stat_lock);
3611da177e4SLinus Torvalds 				return ERR_PTR(-ENOSPC);
3621da177e4SLinus Torvalds 			}
3631da177e4SLinus Torvalds 			sbinfo->free_blocks--;
3641da177e4SLinus Torvalds 			inode->i_blocks += BLOCKS_PER_PAGE;
3651da177e4SLinus Torvalds 			spin_unlock(&sbinfo->stat_lock);
3661da177e4SLinus Torvalds 		}
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 		spin_unlock(&info->lock);
3691da177e4SLinus Torvalds 		page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping) | __GFP_ZERO);
3704c21e2f2SHugh Dickins 		if (page)
3714c21e2f2SHugh Dickins 			set_page_private(page, 0);
3721da177e4SLinus Torvalds 		spin_lock(&info->lock);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 		if (!page) {
3751da177e4SLinus Torvalds 			shmem_free_blocks(inode, 1);
3761da177e4SLinus Torvalds 			return ERR_PTR(-ENOMEM);
3771da177e4SLinus Torvalds 		}
3781da177e4SLinus Torvalds 		if (sgp != SGP_WRITE &&
3791da177e4SLinus Torvalds 		    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
3801da177e4SLinus Torvalds 			entry = ERR_PTR(-EINVAL);
3811da177e4SLinus Torvalds 			break;
3821da177e4SLinus Torvalds 		}
3831da177e4SLinus Torvalds 		if (info->next_index <= index)
3841da177e4SLinus Torvalds 			info->next_index = index + 1;
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 	if (page) {
3871da177e4SLinus Torvalds 		/* another task gave its page, or truncated the file */
3881da177e4SLinus Torvalds 		shmem_free_blocks(inode, 1);
3891da177e4SLinus Torvalds 		shmem_dir_free(page);
3901da177e4SLinus Torvalds 	}
3911da177e4SLinus Torvalds 	if (info->next_index <= index && !IS_ERR(entry))
3921da177e4SLinus Torvalds 		info->next_index = index + 1;
3931da177e4SLinus Torvalds 	return entry;
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds /*
3971da177e4SLinus Torvalds  * shmem_free_swp - free some swap entries in a directory
3981da177e4SLinus Torvalds  *
3991da177e4SLinus Torvalds  * @dir:   pointer to the directory
4001da177e4SLinus Torvalds  * @edir:  pointer after last entry of the directory
4011da177e4SLinus Torvalds  */
4021da177e4SLinus Torvalds static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	swp_entry_t *ptr;
4051da177e4SLinus Torvalds 	int freed = 0;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	for (ptr = dir; ptr < edir; ptr++) {
4081da177e4SLinus Torvalds 		if (ptr->val) {
4091da177e4SLinus Torvalds 			free_swap_and_cache(*ptr);
4101da177e4SLinus Torvalds 			*ptr = (swp_entry_t){0};
4111da177e4SLinus Torvalds 			freed++;
4121da177e4SLinus Torvalds 		}
4131da177e4SLinus Torvalds 	}
4141da177e4SLinus Torvalds 	return freed;
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds static int shmem_map_and_free_swp(struct page *subdir,
4181da177e4SLinus Torvalds 		int offset, int limit, struct page ***dir)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds 	swp_entry_t *ptr;
4211da177e4SLinus Torvalds 	int freed = 0;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	ptr = shmem_swp_map(subdir);
4241da177e4SLinus Torvalds 	for (; offset < limit; offset += LATENCY_LIMIT) {
4251da177e4SLinus Torvalds 		int size = limit - offset;
4261da177e4SLinus Torvalds 		if (size > LATENCY_LIMIT)
4271da177e4SLinus Torvalds 			size = LATENCY_LIMIT;
4281da177e4SLinus Torvalds 		freed += shmem_free_swp(ptr+offset, ptr+offset+size);
4291da177e4SLinus Torvalds 		if (need_resched()) {
4301da177e4SLinus Torvalds 			shmem_swp_unmap(ptr);
4311da177e4SLinus Torvalds 			if (*dir) {
4321da177e4SLinus Torvalds 				shmem_dir_unmap(*dir);
4331da177e4SLinus Torvalds 				*dir = NULL;
4341da177e4SLinus Torvalds 			}
4351da177e4SLinus Torvalds 			cond_resched();
4361da177e4SLinus Torvalds 			ptr = shmem_swp_map(subdir);
4371da177e4SLinus Torvalds 		}
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 	shmem_swp_unmap(ptr);
4401da177e4SLinus Torvalds 	return freed;
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds static void shmem_free_pages(struct list_head *next)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds 	struct page *page;
4461da177e4SLinus Torvalds 	int freed = 0;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	do {
4491da177e4SLinus Torvalds 		page = container_of(next, struct page, lru);
4501da177e4SLinus Torvalds 		next = next->next;
4511da177e4SLinus Torvalds 		shmem_dir_free(page);
4521da177e4SLinus Torvalds 		freed++;
4531da177e4SLinus Torvalds 		if (freed >= LATENCY_LIMIT) {
4541da177e4SLinus Torvalds 			cond_resched();
4551da177e4SLinus Torvalds 			freed = 0;
4561da177e4SLinus Torvalds 		}
4571da177e4SLinus Torvalds 	} while (next);
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
460*f6b3ec23SBadari Pulavarty static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
4631da177e4SLinus Torvalds 	unsigned long idx;
4641da177e4SLinus Torvalds 	unsigned long size;
4651da177e4SLinus Torvalds 	unsigned long limit;
4661da177e4SLinus Torvalds 	unsigned long stage;
4671da177e4SLinus Torvalds 	unsigned long diroff;
4681da177e4SLinus Torvalds 	struct page **dir;
4691da177e4SLinus Torvalds 	struct page *topdir;
4701da177e4SLinus Torvalds 	struct page *middir;
4711da177e4SLinus Torvalds 	struct page *subdir;
4721da177e4SLinus Torvalds 	swp_entry_t *ptr;
4731da177e4SLinus Torvalds 	LIST_HEAD(pages_to_free);
4741da177e4SLinus Torvalds 	long nr_pages_to_free = 0;
4751da177e4SLinus Torvalds 	long nr_swaps_freed = 0;
4761da177e4SLinus Torvalds 	int offset;
4771da177e4SLinus Torvalds 	int freed;
478*f6b3ec23SBadari Pulavarty 	int punch_hole = 0;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
481*f6b3ec23SBadari Pulavarty 	idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4821da177e4SLinus Torvalds 	if (idx >= info->next_index)
4831da177e4SLinus Torvalds 		return;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	spin_lock(&info->lock);
4861da177e4SLinus Torvalds 	info->flags |= SHMEM_TRUNCATE;
487*f6b3ec23SBadari Pulavarty 	if (likely(end == (loff_t) -1)) {
4881da177e4SLinus Torvalds 		limit = info->next_index;
4891da177e4SLinus Torvalds 		info->next_index = idx;
490*f6b3ec23SBadari Pulavarty 	} else {
491*f6b3ec23SBadari Pulavarty 		limit = (end + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
492*f6b3ec23SBadari Pulavarty 		if (limit > info->next_index)
493*f6b3ec23SBadari Pulavarty 			limit = info->next_index;
494*f6b3ec23SBadari Pulavarty 		punch_hole = 1;
495*f6b3ec23SBadari Pulavarty 	}
496*f6b3ec23SBadari Pulavarty 
4971da177e4SLinus Torvalds 	topdir = info->i_indirect;
498*f6b3ec23SBadari Pulavarty 	if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
4991da177e4SLinus Torvalds 		info->i_indirect = NULL;
5001da177e4SLinus Torvalds 		nr_pages_to_free++;
5011da177e4SLinus Torvalds 		list_add(&topdir->lru, &pages_to_free);
5021da177e4SLinus Torvalds 	}
5031da177e4SLinus Torvalds 	spin_unlock(&info->lock);
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	if (info->swapped && idx < SHMEM_NR_DIRECT) {
5061da177e4SLinus Torvalds 		ptr = info->i_direct;
5071da177e4SLinus Torvalds 		size = limit;
5081da177e4SLinus Torvalds 		if (size > SHMEM_NR_DIRECT)
5091da177e4SLinus Torvalds 			size = SHMEM_NR_DIRECT;
5101da177e4SLinus Torvalds 		nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size);
5111da177e4SLinus Torvalds 	}
5121da177e4SLinus Torvalds 	if (!topdir)
5131da177e4SLinus Torvalds 		goto done2;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	BUG_ON(limit <= SHMEM_NR_DIRECT);
5161da177e4SLinus Torvalds 	limit -= SHMEM_NR_DIRECT;
5171da177e4SLinus Torvalds 	idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
5181da177e4SLinus Torvalds 	offset = idx % ENTRIES_PER_PAGE;
5191da177e4SLinus Torvalds 	idx -= offset;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	dir = shmem_dir_map(topdir);
5221da177e4SLinus Torvalds 	stage = ENTRIES_PER_PAGEPAGE/2;
5231da177e4SLinus Torvalds 	if (idx < ENTRIES_PER_PAGEPAGE/2) {
5241da177e4SLinus Torvalds 		middir = topdir;
5251da177e4SLinus Torvalds 		diroff = idx/ENTRIES_PER_PAGE;
5261da177e4SLinus Torvalds 	} else {
5271da177e4SLinus Torvalds 		dir += ENTRIES_PER_PAGE/2;
5281da177e4SLinus Torvalds 		dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
5291da177e4SLinus Torvalds 		while (stage <= idx)
5301da177e4SLinus Torvalds 			stage += ENTRIES_PER_PAGEPAGE;
5311da177e4SLinus Torvalds 		middir = *dir;
5321da177e4SLinus Torvalds 		if (*dir) {
5331da177e4SLinus Torvalds 			diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
5341da177e4SLinus Torvalds 				ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
5351da177e4SLinus Torvalds 			if (!diroff && !offset) {
5361da177e4SLinus Torvalds 				*dir = NULL;
5371da177e4SLinus Torvalds 				nr_pages_to_free++;
5381da177e4SLinus Torvalds 				list_add(&middir->lru, &pages_to_free);
5391da177e4SLinus Torvalds 			}
5401da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
5411da177e4SLinus Torvalds 			dir = shmem_dir_map(middir);
5421da177e4SLinus Torvalds 		} else {
5431da177e4SLinus Torvalds 			diroff = 0;
5441da177e4SLinus Torvalds 			offset = 0;
5451da177e4SLinus Torvalds 			idx = stage;
5461da177e4SLinus Torvalds 		}
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
5501da177e4SLinus Torvalds 		if (unlikely(idx == stage)) {
5511da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
5521da177e4SLinus Torvalds 			dir = shmem_dir_map(topdir) +
5531da177e4SLinus Torvalds 			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
5541da177e4SLinus Torvalds 			while (!*dir) {
5551da177e4SLinus Torvalds 				dir++;
5561da177e4SLinus Torvalds 				idx += ENTRIES_PER_PAGEPAGE;
5571da177e4SLinus Torvalds 				if (idx >= limit)
5581da177e4SLinus Torvalds 					goto done1;
5591da177e4SLinus Torvalds 			}
5601da177e4SLinus Torvalds 			stage = idx + ENTRIES_PER_PAGEPAGE;
5611da177e4SLinus Torvalds 			middir = *dir;
5621da177e4SLinus Torvalds 			*dir = NULL;
5631da177e4SLinus Torvalds 			nr_pages_to_free++;
5641da177e4SLinus Torvalds 			list_add(&middir->lru, &pages_to_free);
5651da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
5661da177e4SLinus Torvalds 			cond_resched();
5671da177e4SLinus Torvalds 			dir = shmem_dir_map(middir);
5681da177e4SLinus Torvalds 			diroff = 0;
5691da177e4SLinus Torvalds 		}
5701da177e4SLinus Torvalds 		subdir = dir[diroff];
5714c21e2f2SHugh Dickins 		if (subdir && page_private(subdir)) {
5721da177e4SLinus Torvalds 			size = limit - idx;
5731da177e4SLinus Torvalds 			if (size > ENTRIES_PER_PAGE)
5741da177e4SLinus Torvalds 				size = ENTRIES_PER_PAGE;
5751da177e4SLinus Torvalds 			freed = shmem_map_and_free_swp(subdir,
5761da177e4SLinus Torvalds 						offset, size, &dir);
5771da177e4SLinus Torvalds 			if (!dir)
5781da177e4SLinus Torvalds 				dir = shmem_dir_map(middir);
5791da177e4SLinus Torvalds 			nr_swaps_freed += freed;
5801da177e4SLinus Torvalds 			if (offset)
5811da177e4SLinus Torvalds 				spin_lock(&info->lock);
5824c21e2f2SHugh Dickins 			set_page_private(subdir, page_private(subdir) - freed);
5831da177e4SLinus Torvalds 			if (offset)
5841da177e4SLinus Torvalds 				spin_unlock(&info->lock);
585*f6b3ec23SBadari Pulavarty 			if (!punch_hole)
5864c21e2f2SHugh Dickins 				BUG_ON(page_private(subdir) > offset);
5871da177e4SLinus Torvalds 		}
5881da177e4SLinus Torvalds 		if (offset)
5891da177e4SLinus Torvalds 			offset = 0;
590*f6b3ec23SBadari Pulavarty 		else if (subdir && !page_private(subdir)) {
5911da177e4SLinus Torvalds 			dir[diroff] = NULL;
5921da177e4SLinus Torvalds 			nr_pages_to_free++;
5931da177e4SLinus Torvalds 			list_add(&subdir->lru, &pages_to_free);
5941da177e4SLinus Torvalds 		}
5951da177e4SLinus Torvalds 	}
5961da177e4SLinus Torvalds done1:
5971da177e4SLinus Torvalds 	shmem_dir_unmap(dir);
5981da177e4SLinus Torvalds done2:
5991da177e4SLinus Torvalds 	if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
6001da177e4SLinus Torvalds 		/*
6011da177e4SLinus Torvalds 		 * Call truncate_inode_pages again: racing shmem_unuse_inode
6021da177e4SLinus Torvalds 		 * may have swizzled a page in from swap since vmtruncate or
6031da177e4SLinus Torvalds 		 * generic_delete_inode did it, before we lowered next_index.
6041da177e4SLinus Torvalds 		 * Also, though shmem_getpage checks i_size before adding to
6051da177e4SLinus Torvalds 		 * cache, no recheck after: so fix the narrow window there too.
6061da177e4SLinus Torvalds 		 */
607*f6b3ec23SBadari Pulavarty 		truncate_inode_pages_range(inode->i_mapping, start, end);
6081da177e4SLinus Torvalds 	}
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	spin_lock(&info->lock);
6111da177e4SLinus Torvalds 	info->flags &= ~SHMEM_TRUNCATE;
6121da177e4SLinus Torvalds 	info->swapped -= nr_swaps_freed;
6131da177e4SLinus Torvalds 	if (nr_pages_to_free)
6141da177e4SLinus Torvalds 		shmem_free_blocks(inode, nr_pages_to_free);
6151da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
6161da177e4SLinus Torvalds 	spin_unlock(&info->lock);
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	/*
6191da177e4SLinus Torvalds 	 * Empty swap vector directory pages to be freed?
6201da177e4SLinus Torvalds 	 */
6211da177e4SLinus Torvalds 	if (!list_empty(&pages_to_free)) {
6221da177e4SLinus Torvalds 		pages_to_free.prev->next = NULL;
6231da177e4SLinus Torvalds 		shmem_free_pages(pages_to_free.next);
6241da177e4SLinus Torvalds 	}
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds 
627*f6b3ec23SBadari Pulavarty static void shmem_truncate(struct inode *inode)
628*f6b3ec23SBadari Pulavarty {
629*f6b3ec23SBadari Pulavarty 	shmem_truncate_range(inode, inode->i_size, (loff_t)-1);
630*f6b3ec23SBadari Pulavarty }
631*f6b3ec23SBadari Pulavarty 
6321da177e4SLinus Torvalds static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
6331da177e4SLinus Torvalds {
6341da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
6351da177e4SLinus Torvalds 	struct page *page = NULL;
6361da177e4SLinus Torvalds 	int error;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_SIZE) {
6391da177e4SLinus Torvalds 		if (attr->ia_size < inode->i_size) {
6401da177e4SLinus Torvalds 			/*
6411da177e4SLinus Torvalds 			 * If truncating down to a partial page, then
6421da177e4SLinus Torvalds 			 * if that page is already allocated, hold it
6431da177e4SLinus Torvalds 			 * in memory until the truncation is over, so
6441da177e4SLinus Torvalds 			 * truncate_partial_page cannnot miss it were
6451da177e4SLinus Torvalds 			 * it assigned to swap.
6461da177e4SLinus Torvalds 			 */
6471da177e4SLinus Torvalds 			if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
6481da177e4SLinus Torvalds 				(void) shmem_getpage(inode,
6491da177e4SLinus Torvalds 					attr->ia_size>>PAGE_CACHE_SHIFT,
6501da177e4SLinus Torvalds 						&page, SGP_READ, NULL);
6511da177e4SLinus Torvalds 			}
6521da177e4SLinus Torvalds 			/*
6531da177e4SLinus Torvalds 			 * Reset SHMEM_PAGEIN flag so that shmem_truncate can
6541da177e4SLinus Torvalds 			 * detect if any pages might have been added to cache
6551da177e4SLinus Torvalds 			 * after truncate_inode_pages.  But we needn't bother
6561da177e4SLinus Torvalds 			 * if it's being fully truncated to zero-length: the
6571da177e4SLinus Torvalds 			 * nrpages check is efficient enough in that case.
6581da177e4SLinus Torvalds 			 */
6591da177e4SLinus Torvalds 			if (attr->ia_size) {
6601da177e4SLinus Torvalds 				struct shmem_inode_info *info = SHMEM_I(inode);
6611da177e4SLinus Torvalds 				spin_lock(&info->lock);
6621da177e4SLinus Torvalds 				info->flags &= ~SHMEM_PAGEIN;
6631da177e4SLinus Torvalds 				spin_unlock(&info->lock);
6641da177e4SLinus Torvalds 			}
6651da177e4SLinus Torvalds 		}
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	error = inode_change_ok(inode, attr);
6691da177e4SLinus Torvalds 	if (!error)
6701da177e4SLinus Torvalds 		error = inode_setattr(inode, attr);
6711da177e4SLinus Torvalds 	if (page)
6721da177e4SLinus Torvalds 		page_cache_release(page);
6731da177e4SLinus Torvalds 	return error;
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds static void shmem_delete_inode(struct inode *inode)
6771da177e4SLinus Torvalds {
6781da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
6791da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	if (inode->i_op->truncate == shmem_truncate) {
682fef26658SMark Fasheh 		truncate_inode_pages(inode->i_mapping, 0);
6831da177e4SLinus Torvalds 		shmem_unacct_size(info->flags, inode->i_size);
6841da177e4SLinus Torvalds 		inode->i_size = 0;
6851da177e4SLinus Torvalds 		shmem_truncate(inode);
6861da177e4SLinus Torvalds 		if (!list_empty(&info->swaplist)) {
6871da177e4SLinus Torvalds 			spin_lock(&shmem_swaplist_lock);
6881da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
6891da177e4SLinus Torvalds 			spin_unlock(&shmem_swaplist_lock);
6901da177e4SLinus Torvalds 		}
6911da177e4SLinus Torvalds 	}
6921da177e4SLinus Torvalds 	BUG_ON(inode->i_blocks);
6930edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
6941da177e4SLinus Torvalds 		spin_lock(&sbinfo->stat_lock);
6951da177e4SLinus Torvalds 		sbinfo->free_inodes++;
6961da177e4SLinus Torvalds 		spin_unlock(&sbinfo->stat_lock);
6971da177e4SLinus Torvalds 	}
6981da177e4SLinus Torvalds 	clear_inode(inode);
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	swp_entry_t *ptr;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	for (ptr = dir; ptr < edir; ptr++) {
7061da177e4SLinus Torvalds 		if (ptr->val == entry.val)
7071da177e4SLinus Torvalds 			return ptr - dir;
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 	return -1;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
7131da177e4SLinus Torvalds {
7141da177e4SLinus Torvalds 	struct inode *inode;
7151da177e4SLinus Torvalds 	unsigned long idx;
7161da177e4SLinus Torvalds 	unsigned long size;
7171da177e4SLinus Torvalds 	unsigned long limit;
7181da177e4SLinus Torvalds 	unsigned long stage;
7191da177e4SLinus Torvalds 	struct page **dir;
7201da177e4SLinus Torvalds 	struct page *subdir;
7211da177e4SLinus Torvalds 	swp_entry_t *ptr;
7221da177e4SLinus Torvalds 	int offset;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	idx = 0;
7251da177e4SLinus Torvalds 	ptr = info->i_direct;
7261da177e4SLinus Torvalds 	spin_lock(&info->lock);
7271da177e4SLinus Torvalds 	limit = info->next_index;
7281da177e4SLinus Torvalds 	size = limit;
7291da177e4SLinus Torvalds 	if (size > SHMEM_NR_DIRECT)
7301da177e4SLinus Torvalds 		size = SHMEM_NR_DIRECT;
7311da177e4SLinus Torvalds 	offset = shmem_find_swp(entry, ptr, ptr+size);
7321da177e4SLinus Torvalds 	if (offset >= 0) {
7331da177e4SLinus Torvalds 		shmem_swp_balance_unmap();
7341da177e4SLinus Torvalds 		goto found;
7351da177e4SLinus Torvalds 	}
7361da177e4SLinus Torvalds 	if (!info->i_indirect)
7371da177e4SLinus Torvalds 		goto lost2;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	dir = shmem_dir_map(info->i_indirect);
7401da177e4SLinus Torvalds 	stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
7431da177e4SLinus Torvalds 		if (unlikely(idx == stage)) {
7441da177e4SLinus Torvalds 			shmem_dir_unmap(dir-1);
7451da177e4SLinus Torvalds 			dir = shmem_dir_map(info->i_indirect) +
7461da177e4SLinus Torvalds 			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
7471da177e4SLinus Torvalds 			while (!*dir) {
7481da177e4SLinus Torvalds 				dir++;
7491da177e4SLinus Torvalds 				idx += ENTRIES_PER_PAGEPAGE;
7501da177e4SLinus Torvalds 				if (idx >= limit)
7511da177e4SLinus Torvalds 					goto lost1;
7521da177e4SLinus Torvalds 			}
7531da177e4SLinus Torvalds 			stage = idx + ENTRIES_PER_PAGEPAGE;
7541da177e4SLinus Torvalds 			subdir = *dir;
7551da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
7561da177e4SLinus Torvalds 			dir = shmem_dir_map(subdir);
7571da177e4SLinus Torvalds 		}
7581da177e4SLinus Torvalds 		subdir = *dir;
7594c21e2f2SHugh Dickins 		if (subdir && page_private(subdir)) {
7601da177e4SLinus Torvalds 			ptr = shmem_swp_map(subdir);
7611da177e4SLinus Torvalds 			size = limit - idx;
7621da177e4SLinus Torvalds 			if (size > ENTRIES_PER_PAGE)
7631da177e4SLinus Torvalds 				size = ENTRIES_PER_PAGE;
7641da177e4SLinus Torvalds 			offset = shmem_find_swp(entry, ptr, ptr+size);
7651da177e4SLinus Torvalds 			if (offset >= 0) {
7661da177e4SLinus Torvalds 				shmem_dir_unmap(dir);
7671da177e4SLinus Torvalds 				goto found;
7681da177e4SLinus Torvalds 			}
7691da177e4SLinus Torvalds 			shmem_swp_unmap(ptr);
7701da177e4SLinus Torvalds 		}
7711da177e4SLinus Torvalds 	}
7721da177e4SLinus Torvalds lost1:
7731da177e4SLinus Torvalds 	shmem_dir_unmap(dir-1);
7741da177e4SLinus Torvalds lost2:
7751da177e4SLinus Torvalds 	spin_unlock(&info->lock);
7761da177e4SLinus Torvalds 	return 0;
7771da177e4SLinus Torvalds found:
7781da177e4SLinus Torvalds 	idx += offset;
7791da177e4SLinus Torvalds 	inode = &info->vfs_inode;
7801da177e4SLinus Torvalds 	if (move_from_swap_cache(page, idx, inode->i_mapping) == 0) {
7811da177e4SLinus Torvalds 		info->flags |= SHMEM_PAGEIN;
7821da177e4SLinus Torvalds 		shmem_swp_set(info, ptr + offset, 0);
7831da177e4SLinus Torvalds 	}
7841da177e4SLinus Torvalds 	shmem_swp_unmap(ptr);
7851da177e4SLinus Torvalds 	spin_unlock(&info->lock);
7861da177e4SLinus Torvalds 	/*
7871da177e4SLinus Torvalds 	 * Decrement swap count even when the entry is left behind:
7881da177e4SLinus Torvalds 	 * try_to_unuse will skip over mms, then reincrement count.
7891da177e4SLinus Torvalds 	 */
7901da177e4SLinus Torvalds 	swap_free(entry);
7911da177e4SLinus Torvalds 	return 1;
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds /*
7951da177e4SLinus Torvalds  * shmem_unuse() search for an eventually swapped out shmem page.
7961da177e4SLinus Torvalds  */
7971da177e4SLinus Torvalds int shmem_unuse(swp_entry_t entry, struct page *page)
7981da177e4SLinus Torvalds {
7991da177e4SLinus Torvalds 	struct list_head *p, *next;
8001da177e4SLinus Torvalds 	struct shmem_inode_info *info;
8011da177e4SLinus Torvalds 	int found = 0;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	spin_lock(&shmem_swaplist_lock);
8041da177e4SLinus Torvalds 	list_for_each_safe(p, next, &shmem_swaplist) {
8051da177e4SLinus Torvalds 		info = list_entry(p, struct shmem_inode_info, swaplist);
8061da177e4SLinus Torvalds 		if (!info->swapped)
8071da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
8081da177e4SLinus Torvalds 		else if (shmem_unuse_inode(info, entry, page)) {
8091da177e4SLinus Torvalds 			/* move head to start search for next from here */
8101da177e4SLinus Torvalds 			list_move_tail(&shmem_swaplist, &info->swaplist);
8111da177e4SLinus Torvalds 			found = 1;
8121da177e4SLinus Torvalds 			break;
8131da177e4SLinus Torvalds 		}
8141da177e4SLinus Torvalds 	}
8151da177e4SLinus Torvalds 	spin_unlock(&shmem_swaplist_lock);
8161da177e4SLinus Torvalds 	return found;
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds /*
8201da177e4SLinus Torvalds  * Move the page from the page cache to the swap cache.
8211da177e4SLinus Torvalds  */
8221da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc)
8231da177e4SLinus Torvalds {
8241da177e4SLinus Torvalds 	struct shmem_inode_info *info;
8251da177e4SLinus Torvalds 	swp_entry_t *entry, swap;
8261da177e4SLinus Torvalds 	struct address_space *mapping;
8271da177e4SLinus Torvalds 	unsigned long index;
8281da177e4SLinus Torvalds 	struct inode *inode;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
8311da177e4SLinus Torvalds 	BUG_ON(page_mapped(page));
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	mapping = page->mapping;
8341da177e4SLinus Torvalds 	index = page->index;
8351da177e4SLinus Torvalds 	inode = mapping->host;
8361da177e4SLinus Torvalds 	info = SHMEM_I(inode);
8371da177e4SLinus Torvalds 	if (info->flags & VM_LOCKED)
8381da177e4SLinus Torvalds 		goto redirty;
8391da177e4SLinus Torvalds 	swap = get_swap_page();
8401da177e4SLinus Torvalds 	if (!swap.val)
8411da177e4SLinus Torvalds 		goto redirty;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	spin_lock(&info->lock);
8441da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
8451da177e4SLinus Torvalds 	if (index >= info->next_index) {
8461da177e4SLinus Torvalds 		BUG_ON(!(info->flags & SHMEM_TRUNCATE));
8471da177e4SLinus Torvalds 		goto unlock;
8481da177e4SLinus Torvalds 	}
8491da177e4SLinus Torvalds 	entry = shmem_swp_entry(info, index, NULL);
8501da177e4SLinus Torvalds 	BUG_ON(!entry);
8511da177e4SLinus Torvalds 	BUG_ON(entry->val);
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	if (move_to_swap_cache(page, swap) == 0) {
8541da177e4SLinus Torvalds 		shmem_swp_set(info, entry, swap.val);
8551da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
8561da177e4SLinus Torvalds 		spin_unlock(&info->lock);
8571da177e4SLinus Torvalds 		if (list_empty(&info->swaplist)) {
8581da177e4SLinus Torvalds 			spin_lock(&shmem_swaplist_lock);
8591da177e4SLinus Torvalds 			/* move instead of add in case we're racing */
8601da177e4SLinus Torvalds 			list_move_tail(&info->swaplist, &shmem_swaplist);
8611da177e4SLinus Torvalds 			spin_unlock(&shmem_swaplist_lock);
8621da177e4SLinus Torvalds 		}
8631da177e4SLinus Torvalds 		unlock_page(page);
8641da177e4SLinus Torvalds 		return 0;
8651da177e4SLinus Torvalds 	}
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	shmem_swp_unmap(entry);
8681da177e4SLinus Torvalds unlock:
8691da177e4SLinus Torvalds 	spin_unlock(&info->lock);
8701da177e4SLinus Torvalds 	swap_free(swap);
8711da177e4SLinus Torvalds redirty:
8721da177e4SLinus Torvalds 	set_page_dirty(page);
873994fc28cSZach Brown 	return AOP_WRITEPAGE_ACTIVATE;	/* Return with the page locked */
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds #ifdef CONFIG_NUMA
8771da177e4SLinus Torvalds static struct page *shmem_swapin_async(struct shared_policy *p,
8781da177e4SLinus Torvalds 				       swp_entry_t entry, unsigned long idx)
8791da177e4SLinus Torvalds {
8801da177e4SLinus Torvalds 	struct page *page;
8811da177e4SLinus Torvalds 	struct vm_area_struct pvma;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	/* Create a pseudo vma that just contains the policy */
8841da177e4SLinus Torvalds 	memset(&pvma, 0, sizeof(struct vm_area_struct));
8851da177e4SLinus Torvalds 	pvma.vm_end = PAGE_SIZE;
8861da177e4SLinus Torvalds 	pvma.vm_pgoff = idx;
8871da177e4SLinus Torvalds 	pvma.vm_policy = mpol_shared_policy_lookup(p, idx);
8881da177e4SLinus Torvalds 	page = read_swap_cache_async(entry, &pvma, 0);
8891da177e4SLinus Torvalds 	mpol_free(pvma.vm_policy);
8901da177e4SLinus Torvalds 	return page;
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds struct page *shmem_swapin(struct shmem_inode_info *info, swp_entry_t entry,
8941da177e4SLinus Torvalds 			  unsigned long idx)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds 	struct shared_policy *p = &info->policy;
8971da177e4SLinus Torvalds 	int i, num;
8981da177e4SLinus Torvalds 	struct page *page;
8991da177e4SLinus Torvalds 	unsigned long offset;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	num = valid_swaphandles(entry, &offset);
9021da177e4SLinus Torvalds 	for (i = 0; i < num; offset++, i++) {
9031da177e4SLinus Torvalds 		page = shmem_swapin_async(p,
9041da177e4SLinus Torvalds 				swp_entry(swp_type(entry), offset), idx);
9051da177e4SLinus Torvalds 		if (!page)
9061da177e4SLinus Torvalds 			break;
9071da177e4SLinus Torvalds 		page_cache_release(page);
9081da177e4SLinus Torvalds 	}
9091da177e4SLinus Torvalds 	lru_add_drain();	/* Push any new pages onto the LRU now */
9101da177e4SLinus Torvalds 	return shmem_swapin_async(p, entry, idx);
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds static struct page *
9146daa0e28SAl Viro shmem_alloc_page(gfp_t gfp, struct shmem_inode_info *info,
9151da177e4SLinus Torvalds 		 unsigned long idx)
9161da177e4SLinus Torvalds {
9171da177e4SLinus Torvalds 	struct vm_area_struct pvma;
9181da177e4SLinus Torvalds 	struct page *page;
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	memset(&pvma, 0, sizeof(struct vm_area_struct));
9211da177e4SLinus Torvalds 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
9221da177e4SLinus Torvalds 	pvma.vm_pgoff = idx;
9231da177e4SLinus Torvalds 	pvma.vm_end = PAGE_SIZE;
9241da177e4SLinus Torvalds 	page = alloc_page_vma(gfp | __GFP_ZERO, &pvma, 0);
9251da177e4SLinus Torvalds 	mpol_free(pvma.vm_policy);
9261da177e4SLinus Torvalds 	return page;
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds #else
9291da177e4SLinus Torvalds static inline struct page *
9301da177e4SLinus Torvalds shmem_swapin(struct shmem_inode_info *info,swp_entry_t entry,unsigned long idx)
9311da177e4SLinus Torvalds {
9321da177e4SLinus Torvalds 	swapin_readahead(entry, 0, NULL);
9331da177e4SLinus Torvalds 	return read_swap_cache_async(entry, NULL, 0);
9341da177e4SLinus Torvalds }
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds static inline struct page *
937dd0fc66fSAl Viro shmem_alloc_page(gfp_t gfp,struct shmem_inode_info *info, unsigned long idx)
9381da177e4SLinus Torvalds {
9391da177e4SLinus Torvalds 	return alloc_page(gfp | __GFP_ZERO);
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds #endif
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds /*
9441da177e4SLinus Torvalds  * shmem_getpage - either get the page from swap or allocate a new one
9451da177e4SLinus Torvalds  *
9461da177e4SLinus Torvalds  * If we allocate a new one we do not mark it dirty. That's up to the
9471da177e4SLinus Torvalds  * vm. If we swap it in we mark it dirty since we also free the swap
9481da177e4SLinus Torvalds  * entry since a page cannot live in both the swap and page cache
9491da177e4SLinus Torvalds  */
9501da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx,
9511da177e4SLinus Torvalds 			struct page **pagep, enum sgp_type sgp, int *type)
9521da177e4SLinus Torvalds {
9531da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
9541da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
9551da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo;
9561da177e4SLinus Torvalds 	struct page *filepage = *pagep;
9571da177e4SLinus Torvalds 	struct page *swappage;
9581da177e4SLinus Torvalds 	swp_entry_t *entry;
9591da177e4SLinus Torvalds 	swp_entry_t swap;
9601da177e4SLinus Torvalds 	int error;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	if (idx >= SHMEM_MAX_INDEX)
9631da177e4SLinus Torvalds 		return -EFBIG;
9641da177e4SLinus Torvalds 	/*
9651da177e4SLinus Torvalds 	 * Normally, filepage is NULL on entry, and either found
9661da177e4SLinus Torvalds 	 * uptodate immediately, or allocated and zeroed, or read
9671da177e4SLinus Torvalds 	 * in under swappage, which is then assigned to filepage.
9681da177e4SLinus Torvalds 	 * But shmem_prepare_write passes in a locked filepage,
9691da177e4SLinus Torvalds 	 * which may be found not uptodate by other callers too,
9701da177e4SLinus Torvalds 	 * and may need to be copied from the swappage read in.
9711da177e4SLinus Torvalds 	 */
9721da177e4SLinus Torvalds repeat:
9731da177e4SLinus Torvalds 	if (!filepage)
9741da177e4SLinus Torvalds 		filepage = find_lock_page(mapping, idx);
9751da177e4SLinus Torvalds 	if (filepage && PageUptodate(filepage))
9761da177e4SLinus Torvalds 		goto done;
9771da177e4SLinus Torvalds 	error = 0;
9781da177e4SLinus Torvalds 	if (sgp == SGP_QUICK)
9791da177e4SLinus Torvalds 		goto failed;
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	spin_lock(&info->lock);
9821da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
9831da177e4SLinus Torvalds 	entry = shmem_swp_alloc(info, idx, sgp);
9841da177e4SLinus Torvalds 	if (IS_ERR(entry)) {
9851da177e4SLinus Torvalds 		spin_unlock(&info->lock);
9861da177e4SLinus Torvalds 		error = PTR_ERR(entry);
9871da177e4SLinus Torvalds 		goto failed;
9881da177e4SLinus Torvalds 	}
9891da177e4SLinus Torvalds 	swap = *entry;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	if (swap.val) {
9921da177e4SLinus Torvalds 		/* Look it up and read it in.. */
9931da177e4SLinus Torvalds 		swappage = lookup_swap_cache(swap);
9941da177e4SLinus Torvalds 		if (!swappage) {
9951da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
9961da177e4SLinus Torvalds 			spin_unlock(&info->lock);
9971da177e4SLinus Torvalds 			/* here we actually do the io */
9981da177e4SLinus Torvalds 			if (type && *type == VM_FAULT_MINOR) {
9991da177e4SLinus Torvalds 				inc_page_state(pgmajfault);
10001da177e4SLinus Torvalds 				*type = VM_FAULT_MAJOR;
10011da177e4SLinus Torvalds 			}
10021da177e4SLinus Torvalds 			swappage = shmem_swapin(info, swap, idx);
10031da177e4SLinus Torvalds 			if (!swappage) {
10041da177e4SLinus Torvalds 				spin_lock(&info->lock);
10051da177e4SLinus Torvalds 				entry = shmem_swp_alloc(info, idx, sgp);
10061da177e4SLinus Torvalds 				if (IS_ERR(entry))
10071da177e4SLinus Torvalds 					error = PTR_ERR(entry);
10081da177e4SLinus Torvalds 				else {
10091da177e4SLinus Torvalds 					if (entry->val == swap.val)
10101da177e4SLinus Torvalds 						error = -ENOMEM;
10111da177e4SLinus Torvalds 					shmem_swp_unmap(entry);
10121da177e4SLinus Torvalds 				}
10131da177e4SLinus Torvalds 				spin_unlock(&info->lock);
10141da177e4SLinus Torvalds 				if (error)
10151da177e4SLinus Torvalds 					goto failed;
10161da177e4SLinus Torvalds 				goto repeat;
10171da177e4SLinus Torvalds 			}
10181da177e4SLinus Torvalds 			wait_on_page_locked(swappage);
10191da177e4SLinus Torvalds 			page_cache_release(swappage);
10201da177e4SLinus Torvalds 			goto repeat;
10211da177e4SLinus Torvalds 		}
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 		/* We have to do this with page locked to prevent races */
10241da177e4SLinus Torvalds 		if (TestSetPageLocked(swappage)) {
10251da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
10261da177e4SLinus Torvalds 			spin_unlock(&info->lock);
10271da177e4SLinus Torvalds 			wait_on_page_locked(swappage);
10281da177e4SLinus Torvalds 			page_cache_release(swappage);
10291da177e4SLinus Torvalds 			goto repeat;
10301da177e4SLinus Torvalds 		}
10311da177e4SLinus Torvalds 		if (PageWriteback(swappage)) {
10321da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
10331da177e4SLinus Torvalds 			spin_unlock(&info->lock);
10341da177e4SLinus Torvalds 			wait_on_page_writeback(swappage);
10351da177e4SLinus Torvalds 			unlock_page(swappage);
10361da177e4SLinus Torvalds 			page_cache_release(swappage);
10371da177e4SLinus Torvalds 			goto repeat;
10381da177e4SLinus Torvalds 		}
10391da177e4SLinus Torvalds 		if (!PageUptodate(swappage)) {
10401da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
10411da177e4SLinus Torvalds 			spin_unlock(&info->lock);
10421da177e4SLinus Torvalds 			unlock_page(swappage);
10431da177e4SLinus Torvalds 			page_cache_release(swappage);
10441da177e4SLinus Torvalds 			error = -EIO;
10451da177e4SLinus Torvalds 			goto failed;
10461da177e4SLinus Torvalds 		}
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 		if (filepage) {
10491da177e4SLinus Torvalds 			shmem_swp_set(info, entry, 0);
10501da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
10511da177e4SLinus Torvalds 			delete_from_swap_cache(swappage);
10521da177e4SLinus Torvalds 			spin_unlock(&info->lock);
10531da177e4SLinus Torvalds 			copy_highpage(filepage, swappage);
10541da177e4SLinus Torvalds 			unlock_page(swappage);
10551da177e4SLinus Torvalds 			page_cache_release(swappage);
10561da177e4SLinus Torvalds 			flush_dcache_page(filepage);
10571da177e4SLinus Torvalds 			SetPageUptodate(filepage);
10581da177e4SLinus Torvalds 			set_page_dirty(filepage);
10591da177e4SLinus Torvalds 			swap_free(swap);
10601da177e4SLinus Torvalds 		} else if (!(error = move_from_swap_cache(
10611da177e4SLinus Torvalds 				swappage, idx, mapping))) {
10621da177e4SLinus Torvalds 			info->flags |= SHMEM_PAGEIN;
10631da177e4SLinus Torvalds 			shmem_swp_set(info, entry, 0);
10641da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
10651da177e4SLinus Torvalds 			spin_unlock(&info->lock);
10661da177e4SLinus Torvalds 			filepage = swappage;
10671da177e4SLinus Torvalds 			swap_free(swap);
10681da177e4SLinus Torvalds 		} else {
10691da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
10701da177e4SLinus Torvalds 			spin_unlock(&info->lock);
10711da177e4SLinus Torvalds 			unlock_page(swappage);
10721da177e4SLinus Torvalds 			page_cache_release(swappage);
10731da177e4SLinus Torvalds 			if (error == -ENOMEM) {
10741da177e4SLinus Torvalds 				/* let kswapd refresh zone for GFP_ATOMICs */
10751da177e4SLinus Torvalds 				blk_congestion_wait(WRITE, HZ/50);
10761da177e4SLinus Torvalds 			}
10771da177e4SLinus Torvalds 			goto repeat;
10781da177e4SLinus Torvalds 		}
10791da177e4SLinus Torvalds 	} else if (sgp == SGP_READ && !filepage) {
10801da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
10811da177e4SLinus Torvalds 		filepage = find_get_page(mapping, idx);
10821da177e4SLinus Torvalds 		if (filepage &&
10831da177e4SLinus Torvalds 		    (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
10841da177e4SLinus Torvalds 			spin_unlock(&info->lock);
10851da177e4SLinus Torvalds 			wait_on_page_locked(filepage);
10861da177e4SLinus Torvalds 			page_cache_release(filepage);
10871da177e4SLinus Torvalds 			filepage = NULL;
10881da177e4SLinus Torvalds 			goto repeat;
10891da177e4SLinus Torvalds 		}
10901da177e4SLinus Torvalds 		spin_unlock(&info->lock);
10911da177e4SLinus Torvalds 	} else {
10921da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
10931da177e4SLinus Torvalds 		sbinfo = SHMEM_SB(inode->i_sb);
10940edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
10951da177e4SLinus Torvalds 			spin_lock(&sbinfo->stat_lock);
10961da177e4SLinus Torvalds 			if (sbinfo->free_blocks == 0 ||
10971da177e4SLinus Torvalds 			    shmem_acct_block(info->flags)) {
10981da177e4SLinus Torvalds 				spin_unlock(&sbinfo->stat_lock);
10991da177e4SLinus Torvalds 				spin_unlock(&info->lock);
11001da177e4SLinus Torvalds 				error = -ENOSPC;
11011da177e4SLinus Torvalds 				goto failed;
11021da177e4SLinus Torvalds 			}
11031da177e4SLinus Torvalds 			sbinfo->free_blocks--;
11041da177e4SLinus Torvalds 			inode->i_blocks += BLOCKS_PER_PAGE;
11051da177e4SLinus Torvalds 			spin_unlock(&sbinfo->stat_lock);
11061da177e4SLinus Torvalds 		} else if (shmem_acct_block(info->flags)) {
11071da177e4SLinus Torvalds 			spin_unlock(&info->lock);
11081da177e4SLinus Torvalds 			error = -ENOSPC;
11091da177e4SLinus Torvalds 			goto failed;
11101da177e4SLinus Torvalds 		}
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 		if (!filepage) {
11131da177e4SLinus Torvalds 			spin_unlock(&info->lock);
11141da177e4SLinus Torvalds 			filepage = shmem_alloc_page(mapping_gfp_mask(mapping),
11151da177e4SLinus Torvalds 						    info,
11161da177e4SLinus Torvalds 						    idx);
11171da177e4SLinus Torvalds 			if (!filepage) {
11181da177e4SLinus Torvalds 				shmem_unacct_blocks(info->flags, 1);
11191da177e4SLinus Torvalds 				shmem_free_blocks(inode, 1);
11201da177e4SLinus Torvalds 				error = -ENOMEM;
11211da177e4SLinus Torvalds 				goto failed;
11221da177e4SLinus Torvalds 			}
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 			spin_lock(&info->lock);
11251da177e4SLinus Torvalds 			entry = shmem_swp_alloc(info, idx, sgp);
11261da177e4SLinus Torvalds 			if (IS_ERR(entry))
11271da177e4SLinus Torvalds 				error = PTR_ERR(entry);
11281da177e4SLinus Torvalds 			else {
11291da177e4SLinus Torvalds 				swap = *entry;
11301da177e4SLinus Torvalds 				shmem_swp_unmap(entry);
11311da177e4SLinus Torvalds 			}
11321da177e4SLinus Torvalds 			if (error || swap.val || 0 != add_to_page_cache_lru(
11331da177e4SLinus Torvalds 					filepage, mapping, idx, GFP_ATOMIC)) {
11341da177e4SLinus Torvalds 				spin_unlock(&info->lock);
11351da177e4SLinus Torvalds 				page_cache_release(filepage);
11361da177e4SLinus Torvalds 				shmem_unacct_blocks(info->flags, 1);
11371da177e4SLinus Torvalds 				shmem_free_blocks(inode, 1);
11381da177e4SLinus Torvalds 				filepage = NULL;
11391da177e4SLinus Torvalds 				if (error)
11401da177e4SLinus Torvalds 					goto failed;
11411da177e4SLinus Torvalds 				goto repeat;
11421da177e4SLinus Torvalds 			}
11431da177e4SLinus Torvalds 			info->flags |= SHMEM_PAGEIN;
11441da177e4SLinus Torvalds 		}
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 		info->alloced++;
11471da177e4SLinus Torvalds 		spin_unlock(&info->lock);
11481da177e4SLinus Torvalds 		flush_dcache_page(filepage);
11491da177e4SLinus Torvalds 		SetPageUptodate(filepage);
11501da177e4SLinus Torvalds 	}
11511da177e4SLinus Torvalds done:
11521da177e4SLinus Torvalds 	if (*pagep != filepage) {
11531da177e4SLinus Torvalds 		unlock_page(filepage);
11541da177e4SLinus Torvalds 		*pagep = filepage;
11551da177e4SLinus Torvalds 	}
11561da177e4SLinus Torvalds 	return 0;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds failed:
11591da177e4SLinus Torvalds 	if (*pagep != filepage) {
11601da177e4SLinus Torvalds 		unlock_page(filepage);
11611da177e4SLinus Torvalds 		page_cache_release(filepage);
11621da177e4SLinus Torvalds 	}
11631da177e4SLinus Torvalds 	return error;
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds struct page *shmem_nopage(struct vm_area_struct *vma, unsigned long address, int *type)
11671da177e4SLinus Torvalds {
11681da177e4SLinus Torvalds 	struct inode *inode = vma->vm_file->f_dentry->d_inode;
11691da177e4SLinus Torvalds 	struct page *page = NULL;
11701da177e4SLinus Torvalds 	unsigned long idx;
11711da177e4SLinus Torvalds 	int error;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	idx = (address - vma->vm_start) >> PAGE_SHIFT;
11741da177e4SLinus Torvalds 	idx += vma->vm_pgoff;
11751da177e4SLinus Torvalds 	idx >>= PAGE_CACHE_SHIFT - PAGE_SHIFT;
11761da177e4SLinus Torvalds 	if (((loff_t) idx << PAGE_CACHE_SHIFT) >= i_size_read(inode))
11771da177e4SLinus Torvalds 		return NOPAGE_SIGBUS;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	error = shmem_getpage(inode, idx, &page, SGP_CACHE, type);
11801da177e4SLinus Torvalds 	if (error)
11811da177e4SLinus Torvalds 		return (error == -ENOMEM)? NOPAGE_OOM: NOPAGE_SIGBUS;
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	mark_page_accessed(page);
11841da177e4SLinus Torvalds 	return page;
11851da177e4SLinus Torvalds }
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds static int shmem_populate(struct vm_area_struct *vma,
11881da177e4SLinus Torvalds 	unsigned long addr, unsigned long len,
11891da177e4SLinus Torvalds 	pgprot_t prot, unsigned long pgoff, int nonblock)
11901da177e4SLinus Torvalds {
11911da177e4SLinus Torvalds 	struct inode *inode = vma->vm_file->f_dentry->d_inode;
11921da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
11931da177e4SLinus Torvalds 	enum sgp_type sgp = nonblock? SGP_QUICK: SGP_CACHE;
11941da177e4SLinus Torvalds 	unsigned long size;
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
11971da177e4SLinus Torvalds 	if (pgoff >= size || pgoff + (len >> PAGE_SHIFT) > size)
11981da177e4SLinus Torvalds 		return -EINVAL;
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	while ((long) len > 0) {
12011da177e4SLinus Torvalds 		struct page *page = NULL;
12021da177e4SLinus Torvalds 		int err;
12031da177e4SLinus Torvalds 		/*
12041da177e4SLinus Torvalds 		 * Will need changing if PAGE_CACHE_SIZE != PAGE_SIZE
12051da177e4SLinus Torvalds 		 */
12061da177e4SLinus Torvalds 		err = shmem_getpage(inode, pgoff, &page, sgp, NULL);
12071da177e4SLinus Torvalds 		if (err)
12081da177e4SLinus Torvalds 			return err;
1209d44ed4f8SPaolo 'Blaisorblade' Giarrusso 		/* Page may still be null, but only if nonblock was set. */
12101da177e4SLinus Torvalds 		if (page) {
12111da177e4SLinus Torvalds 			mark_page_accessed(page);
12121da177e4SLinus Torvalds 			err = install_page(mm, vma, addr, page, prot);
12131da177e4SLinus Torvalds 			if (err) {
12141da177e4SLinus Torvalds 				page_cache_release(page);
12151da177e4SLinus Torvalds 				return err;
12161da177e4SLinus Torvalds 			}
121765500d23SHugh Dickins 		} else if (vma->vm_flags & VM_NONLINEAR) {
1218d44ed4f8SPaolo 'Blaisorblade' Giarrusso 			/* No page was found just because we can't read it in
1219d44ed4f8SPaolo 'Blaisorblade' Giarrusso 			 * now (being here implies nonblock != 0), but the page
1220d44ed4f8SPaolo 'Blaisorblade' Giarrusso 			 * may exist, so set the PTE to fault it in later. */
12211da177e4SLinus Torvalds     			err = install_file_pte(mm, vma, addr, pgoff, prot);
12221da177e4SLinus Torvalds 			if (err)
12231da177e4SLinus Torvalds 	    			return err;
12241da177e4SLinus Torvalds 		}
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 		len -= PAGE_SIZE;
12271da177e4SLinus Torvalds 		addr += PAGE_SIZE;
12281da177e4SLinus Torvalds 		pgoff++;
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 	return 0;
12311da177e4SLinus Torvalds }
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds #ifdef CONFIG_NUMA
12341da177e4SLinus Torvalds int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
12351da177e4SLinus Torvalds {
12361da177e4SLinus Torvalds 	struct inode *i = vma->vm_file->f_dentry->d_inode;
12371da177e4SLinus Torvalds 	return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
12381da177e4SLinus Torvalds }
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds struct mempolicy *
12411da177e4SLinus Torvalds shmem_get_policy(struct vm_area_struct *vma, unsigned long addr)
12421da177e4SLinus Torvalds {
12431da177e4SLinus Torvalds 	struct inode *i = vma->vm_file->f_dentry->d_inode;
12441da177e4SLinus Torvalds 	unsigned long idx;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
12471da177e4SLinus Torvalds 	return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
12481da177e4SLinus Torvalds }
12491da177e4SLinus Torvalds #endif
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user)
12521da177e4SLinus Torvalds {
12531da177e4SLinus Torvalds 	struct inode *inode = file->f_dentry->d_inode;
12541da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
12551da177e4SLinus Torvalds 	int retval = -ENOMEM;
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 	spin_lock(&info->lock);
12581da177e4SLinus Torvalds 	if (lock && !(info->flags & VM_LOCKED)) {
12591da177e4SLinus Torvalds 		if (!user_shm_lock(inode->i_size, user))
12601da177e4SLinus Torvalds 			goto out_nomem;
12611da177e4SLinus Torvalds 		info->flags |= VM_LOCKED;
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds 	if (!lock && (info->flags & VM_LOCKED) && user) {
12641da177e4SLinus Torvalds 		user_shm_unlock(inode->i_size, user);
12651da177e4SLinus Torvalds 		info->flags &= ~VM_LOCKED;
12661da177e4SLinus Torvalds 	}
12671da177e4SLinus Torvalds 	retval = 0;
12681da177e4SLinus Torvalds out_nomem:
12691da177e4SLinus Torvalds 	spin_unlock(&info->lock);
12701da177e4SLinus Torvalds 	return retval;
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
12741da177e4SLinus Torvalds {
12751da177e4SLinus Torvalds 	file_accessed(file);
12761da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
12771da177e4SLinus Torvalds 	return 0;
12781da177e4SLinus Torvalds }
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds static struct inode *
12811da177e4SLinus Torvalds shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
12821da177e4SLinus Torvalds {
12831da177e4SLinus Torvalds 	struct inode *inode;
12841da177e4SLinus Torvalds 	struct shmem_inode_info *info;
12851da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
12861da177e4SLinus Torvalds 
12870edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
12881da177e4SLinus Torvalds 		spin_lock(&sbinfo->stat_lock);
12891da177e4SLinus Torvalds 		if (!sbinfo->free_inodes) {
12901da177e4SLinus Torvalds 			spin_unlock(&sbinfo->stat_lock);
12911da177e4SLinus Torvalds 			return NULL;
12921da177e4SLinus Torvalds 		}
12931da177e4SLinus Torvalds 		sbinfo->free_inodes--;
12941da177e4SLinus Torvalds 		spin_unlock(&sbinfo->stat_lock);
12951da177e4SLinus Torvalds 	}
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 	inode = new_inode(sb);
12981da177e4SLinus Torvalds 	if (inode) {
12991da177e4SLinus Torvalds 		inode->i_mode = mode;
13001da177e4SLinus Torvalds 		inode->i_uid = current->fsuid;
13011da177e4SLinus Torvalds 		inode->i_gid = current->fsgid;
13021da177e4SLinus Torvalds 		inode->i_blksize = PAGE_CACHE_SIZE;
13031da177e4SLinus Torvalds 		inode->i_blocks = 0;
13041da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &shmem_aops;
13051da177e4SLinus Torvalds 		inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
13061da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
13071da177e4SLinus Torvalds 		info = SHMEM_I(inode);
13081da177e4SLinus Torvalds 		memset(info, 0, (char *)inode - (char *)info);
13091da177e4SLinus Torvalds 		spin_lock_init(&info->lock);
13101da177e4SLinus Torvalds 		INIT_LIST_HEAD(&info->swaplist);
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
13131da177e4SLinus Torvalds 		default:
13141da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
13151da177e4SLinus Torvalds 			break;
13161da177e4SLinus Torvalds 		case S_IFREG:
13171da177e4SLinus Torvalds 			inode->i_op = &shmem_inode_operations;
13181da177e4SLinus Torvalds 			inode->i_fop = &shmem_file_operations;
13191da177e4SLinus Torvalds 			mpol_shared_policy_init(&info->policy);
13201da177e4SLinus Torvalds 			break;
13211da177e4SLinus Torvalds 		case S_IFDIR:
13221da177e4SLinus Torvalds 			inode->i_nlink++;
13231da177e4SLinus Torvalds 			/* Some things misbehave if size == 0 on a directory */
13241da177e4SLinus Torvalds 			inode->i_size = 2 * BOGO_DIRENT_SIZE;
13251da177e4SLinus Torvalds 			inode->i_op = &shmem_dir_inode_operations;
13261da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
13271da177e4SLinus Torvalds 			break;
13281da177e4SLinus Torvalds 		case S_IFLNK:
13291da177e4SLinus Torvalds 			/*
13301da177e4SLinus Torvalds 			 * Must not load anything in the rbtree,
13311da177e4SLinus Torvalds 			 * mpol_free_shared_policy will not be called.
13321da177e4SLinus Torvalds 			 */
13331da177e4SLinus Torvalds 			mpol_shared_policy_init(&info->policy);
13341da177e4SLinus Torvalds 			break;
13351da177e4SLinus Torvalds 		}
13360edd73b3SHugh Dickins 	} else if (sbinfo->max_inodes) {
13371da177e4SLinus Torvalds 		spin_lock(&sbinfo->stat_lock);
13381da177e4SLinus Torvalds 		sbinfo->free_inodes++;
13391da177e4SLinus Torvalds 		spin_unlock(&sbinfo->stat_lock);
13401da177e4SLinus Torvalds 	}
13411da177e4SLinus Torvalds 	return inode;
13421da177e4SLinus Torvalds }
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
13451da177e4SLinus Torvalds static struct inode_operations shmem_symlink_inode_operations;
13461da177e4SLinus Torvalds static struct inode_operations shmem_symlink_inline_operations;
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds /*
13491da177e4SLinus Torvalds  * Normally tmpfs makes no use of shmem_prepare_write, but it
13501da177e4SLinus Torvalds  * lets a tmpfs file be used read-write below the loop driver.
13511da177e4SLinus Torvalds  */
13521da177e4SLinus Torvalds static int
13531da177e4SLinus Torvalds shmem_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
13541da177e4SLinus Torvalds {
13551da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
13561da177e4SLinus Torvalds 	return shmem_getpage(inode, page->index, &page, SGP_WRITE, NULL);
13571da177e4SLinus Torvalds }
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds static ssize_t
13601da177e4SLinus Torvalds shmem_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
13611da177e4SLinus Torvalds {
13621da177e4SLinus Torvalds 	struct inode	*inode = file->f_dentry->d_inode;
13631da177e4SLinus Torvalds 	loff_t		pos;
13641da177e4SLinus Torvalds 	unsigned long	written;
13651da177e4SLinus Torvalds 	ssize_t		err;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	if ((ssize_t) count < 0)
13681da177e4SLinus Torvalds 		return -EINVAL;
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, buf, count))
13711da177e4SLinus Torvalds 		return -EFAULT;
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 	down(&inode->i_sem);
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 	pos = *ppos;
13761da177e4SLinus Torvalds 	written = 0;
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	err = generic_write_checks(file, &pos, &count, 0);
13791da177e4SLinus Torvalds 	if (err || !count)
13801da177e4SLinus Torvalds 		goto out;
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	err = remove_suid(file->f_dentry);
13831da177e4SLinus Torvalds 	if (err)
13841da177e4SLinus Torvalds 		goto out;
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	do {
13891da177e4SLinus Torvalds 		struct page *page = NULL;
13901da177e4SLinus Torvalds 		unsigned long bytes, index, offset;
13911da177e4SLinus Torvalds 		char *kaddr;
13921da177e4SLinus Torvalds 		int left;
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 		offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
13951da177e4SLinus Torvalds 		index = pos >> PAGE_CACHE_SHIFT;
13961da177e4SLinus Torvalds 		bytes = PAGE_CACHE_SIZE - offset;
13971da177e4SLinus Torvalds 		if (bytes > count)
13981da177e4SLinus Torvalds 			bytes = count;
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 		/*
14011da177e4SLinus Torvalds 		 * We don't hold page lock across copy from user -
14021da177e4SLinus Torvalds 		 * what would it guard against? - so no deadlock here.
14031da177e4SLinus Torvalds 		 * But it still may be a good idea to prefault below.
14041da177e4SLinus Torvalds 		 */
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 		err = shmem_getpage(inode, index, &page, SGP_WRITE, NULL);
14071da177e4SLinus Torvalds 		if (err)
14081da177e4SLinus Torvalds 			break;
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 		left = bytes;
14111da177e4SLinus Torvalds 		if (PageHighMem(page)) {
14121da177e4SLinus Torvalds 			volatile unsigned char dummy;
14131da177e4SLinus Torvalds 			__get_user(dummy, buf);
14141da177e4SLinus Torvalds 			__get_user(dummy, buf + bytes - 1);
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 			kaddr = kmap_atomic(page, KM_USER0);
14171da177e4SLinus Torvalds 			left = __copy_from_user_inatomic(kaddr + offset,
14181da177e4SLinus Torvalds 							buf, bytes);
14191da177e4SLinus Torvalds 			kunmap_atomic(kaddr, KM_USER0);
14201da177e4SLinus Torvalds 		}
14211da177e4SLinus Torvalds 		if (left) {
14221da177e4SLinus Torvalds 			kaddr = kmap(page);
14231da177e4SLinus Torvalds 			left = __copy_from_user(kaddr + offset, buf, bytes);
14241da177e4SLinus Torvalds 			kunmap(page);
14251da177e4SLinus Torvalds 		}
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 		written += bytes;
14281da177e4SLinus Torvalds 		count -= bytes;
14291da177e4SLinus Torvalds 		pos += bytes;
14301da177e4SLinus Torvalds 		buf += bytes;
14311da177e4SLinus Torvalds 		if (pos > inode->i_size)
14321da177e4SLinus Torvalds 			i_size_write(inode, pos);
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 		flush_dcache_page(page);
14351da177e4SLinus Torvalds 		set_page_dirty(page);
14361da177e4SLinus Torvalds 		mark_page_accessed(page);
14371da177e4SLinus Torvalds 		page_cache_release(page);
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds 		if (left) {
14401da177e4SLinus Torvalds 			pos -= left;
14411da177e4SLinus Torvalds 			written -= left;
14421da177e4SLinus Torvalds 			err = -EFAULT;
14431da177e4SLinus Torvalds 			break;
14441da177e4SLinus Torvalds 		}
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 		/*
14471da177e4SLinus Torvalds 		 * Our dirty pages are not counted in nr_dirty,
14481da177e4SLinus Torvalds 		 * and we do not attempt to balance dirty pages.
14491da177e4SLinus Torvalds 		 */
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 		cond_resched();
14521da177e4SLinus Torvalds 	} while (count);
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	*ppos = pos;
14551da177e4SLinus Torvalds 	if (written)
14561da177e4SLinus Torvalds 		err = written;
14571da177e4SLinus Torvalds out:
14581da177e4SLinus Torvalds 	up(&inode->i_sem);
14591da177e4SLinus Torvalds 	return err;
14601da177e4SLinus Torvalds }
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
14631da177e4SLinus Torvalds {
14641da177e4SLinus Torvalds 	struct inode *inode = filp->f_dentry->d_inode;
14651da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
14661da177e4SLinus Torvalds 	unsigned long index, offset;
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	index = *ppos >> PAGE_CACHE_SHIFT;
14691da177e4SLinus Torvalds 	offset = *ppos & ~PAGE_CACHE_MASK;
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 	for (;;) {
14721da177e4SLinus Torvalds 		struct page *page = NULL;
14731da177e4SLinus Torvalds 		unsigned long end_index, nr, ret;
14741da177e4SLinus Torvalds 		loff_t i_size = i_size_read(inode);
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
14771da177e4SLinus Torvalds 		if (index > end_index)
14781da177e4SLinus Torvalds 			break;
14791da177e4SLinus Torvalds 		if (index == end_index) {
14801da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
14811da177e4SLinus Torvalds 			if (nr <= offset)
14821da177e4SLinus Torvalds 				break;
14831da177e4SLinus Torvalds 		}
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 		desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
14861da177e4SLinus Torvalds 		if (desc->error) {
14871da177e4SLinus Torvalds 			if (desc->error == -EINVAL)
14881da177e4SLinus Torvalds 				desc->error = 0;
14891da177e4SLinus Torvalds 			break;
14901da177e4SLinus Torvalds 		}
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 		/*
14931da177e4SLinus Torvalds 		 * We must evaluate after, since reads (unlike writes)
14941da177e4SLinus Torvalds 		 * are called without i_sem protection against truncate
14951da177e4SLinus Torvalds 		 */
14961da177e4SLinus Torvalds 		nr = PAGE_CACHE_SIZE;
14971da177e4SLinus Torvalds 		i_size = i_size_read(inode);
14981da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
14991da177e4SLinus Torvalds 		if (index == end_index) {
15001da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
15011da177e4SLinus Torvalds 			if (nr <= offset) {
15021da177e4SLinus Torvalds 				if (page)
15031da177e4SLinus Torvalds 					page_cache_release(page);
15041da177e4SLinus Torvalds 				break;
15051da177e4SLinus Torvalds 			}
15061da177e4SLinus Torvalds 		}
15071da177e4SLinus Torvalds 		nr -= offset;
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 		if (page) {
15101da177e4SLinus Torvalds 			/*
15111da177e4SLinus Torvalds 			 * If users can be writing to this page using arbitrary
15121da177e4SLinus Torvalds 			 * virtual addresses, take care about potential aliasing
15131da177e4SLinus Torvalds 			 * before reading the page on the kernel side.
15141da177e4SLinus Torvalds 			 */
15151da177e4SLinus Torvalds 			if (mapping_writably_mapped(mapping))
15161da177e4SLinus Torvalds 				flush_dcache_page(page);
15171da177e4SLinus Torvalds 			/*
15181da177e4SLinus Torvalds 			 * Mark the page accessed if we read the beginning.
15191da177e4SLinus Torvalds 			 */
15201da177e4SLinus Torvalds 			if (!offset)
15211da177e4SLinus Torvalds 				mark_page_accessed(page);
1522b5810039SNick Piggin 		} else {
15231da177e4SLinus Torvalds 			page = ZERO_PAGE(0);
1524b5810039SNick Piggin 			page_cache_get(page);
1525b5810039SNick Piggin 		}
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 		/*
15281da177e4SLinus Torvalds 		 * Ok, we have the page, and it's up-to-date, so
15291da177e4SLinus Torvalds 		 * now we can copy it to user space...
15301da177e4SLinus Torvalds 		 *
15311da177e4SLinus Torvalds 		 * The actor routine returns how many bytes were actually used..
15321da177e4SLinus Torvalds 		 * NOTE! This may not be the same as how much of a user buffer
15331da177e4SLinus Torvalds 		 * we filled up (we may be padding etc), so we can only update
15341da177e4SLinus Torvalds 		 * "pos" here (the actor routine has to update the user buffer
15351da177e4SLinus Torvalds 		 * pointers and the remaining count).
15361da177e4SLinus Torvalds 		 */
15371da177e4SLinus Torvalds 		ret = actor(desc, page, offset, nr);
15381da177e4SLinus Torvalds 		offset += ret;
15391da177e4SLinus Torvalds 		index += offset >> PAGE_CACHE_SHIFT;
15401da177e4SLinus Torvalds 		offset &= ~PAGE_CACHE_MASK;
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 		page_cache_release(page);
15431da177e4SLinus Torvalds 		if (ret != nr || !desc->count)
15441da177e4SLinus Torvalds 			break;
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 		cond_resched();
15471da177e4SLinus Torvalds 	}
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
15501da177e4SLinus Torvalds 	file_accessed(filp);
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
15541da177e4SLinus Torvalds {
15551da177e4SLinus Torvalds 	read_descriptor_t desc;
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds 	if ((ssize_t) count < 0)
15581da177e4SLinus Torvalds 		return -EINVAL;
15591da177e4SLinus Torvalds 	if (!access_ok(VERIFY_WRITE, buf, count))
15601da177e4SLinus Torvalds 		return -EFAULT;
15611da177e4SLinus Torvalds 	if (!count)
15621da177e4SLinus Torvalds 		return 0;
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds 	desc.written = 0;
15651da177e4SLinus Torvalds 	desc.count = count;
15661da177e4SLinus Torvalds 	desc.arg.buf = buf;
15671da177e4SLinus Torvalds 	desc.error = 0;
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	do_shmem_file_read(filp, ppos, &desc, file_read_actor);
15701da177e4SLinus Torvalds 	if (desc.written)
15711da177e4SLinus Torvalds 		return desc.written;
15721da177e4SLinus Torvalds 	return desc.error;
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds static ssize_t shmem_file_sendfile(struct file *in_file, loff_t *ppos,
15761da177e4SLinus Torvalds 			 size_t count, read_actor_t actor, void *target)
15771da177e4SLinus Torvalds {
15781da177e4SLinus Torvalds 	read_descriptor_t desc;
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	if (!count)
15811da177e4SLinus Torvalds 		return 0;
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	desc.written = 0;
15841da177e4SLinus Torvalds 	desc.count = count;
15851da177e4SLinus Torvalds 	desc.arg.data = target;
15861da177e4SLinus Torvalds 	desc.error = 0;
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds 	do_shmem_file_read(in_file, ppos, &desc, actor);
15891da177e4SLinus Torvalds 	if (desc.written)
15901da177e4SLinus Torvalds 		return desc.written;
15911da177e4SLinus Torvalds 	return desc.error;
15921da177e4SLinus Torvalds }
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds static int shmem_statfs(struct super_block *sb, struct kstatfs *buf)
15951da177e4SLinus Torvalds {
15961da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	buf->f_type = TMPFS_MAGIC;
15991da177e4SLinus Torvalds 	buf->f_bsize = PAGE_CACHE_SIZE;
16001da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
16011da177e4SLinus Torvalds 	spin_lock(&sbinfo->stat_lock);
16020edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
16031da177e4SLinus Torvalds 		buf->f_blocks = sbinfo->max_blocks;
16041da177e4SLinus Torvalds 		buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
16050edd73b3SHugh Dickins 	}
16060edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
16071da177e4SLinus Torvalds 		buf->f_files = sbinfo->max_inodes;
16081da177e4SLinus Torvalds 		buf->f_ffree = sbinfo->free_inodes;
16091da177e4SLinus Torvalds 	}
16101da177e4SLinus Torvalds 	/* else leave those fields 0 like simple_statfs */
16110edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
16121da177e4SLinus Torvalds 	return 0;
16131da177e4SLinus Torvalds }
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds /*
16161da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
16171da177e4SLinus Torvalds  */
16181da177e4SLinus Torvalds static int
16191da177e4SLinus Torvalds shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
16201da177e4SLinus Torvalds {
16211da177e4SLinus Torvalds 	struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
16221da177e4SLinus Torvalds 	int error = -ENOSPC;
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	if (inode) {
1625570bc1c2SStephen Smalley 		error = security_inode_init_security(inode, dir, NULL, NULL,
1626570bc1c2SStephen Smalley 						     NULL);
1627570bc1c2SStephen Smalley 		if (error) {
1628570bc1c2SStephen Smalley 			if (error != -EOPNOTSUPP) {
1629570bc1c2SStephen Smalley 				iput(inode);
1630570bc1c2SStephen Smalley 				return error;
1631570bc1c2SStephen Smalley 			}
1632570bc1c2SStephen Smalley 			error = 0;
1633570bc1c2SStephen Smalley 		}
16341da177e4SLinus Torvalds 		if (dir->i_mode & S_ISGID) {
16351da177e4SLinus Torvalds 			inode->i_gid = dir->i_gid;
16361da177e4SLinus Torvalds 			if (S_ISDIR(mode))
16371da177e4SLinus Torvalds 				inode->i_mode |= S_ISGID;
16381da177e4SLinus Torvalds 		}
16391da177e4SLinus Torvalds 		dir->i_size += BOGO_DIRENT_SIZE;
16401da177e4SLinus Torvalds 		dir->i_ctime = dir->i_mtime = CURRENT_TIME;
16411da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
16421da177e4SLinus Torvalds 		dget(dentry); /* Extra count - pin the dentry in core */
16431da177e4SLinus Torvalds 	}
16441da177e4SLinus Torvalds 	return error;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
16481da177e4SLinus Torvalds {
16491da177e4SLinus Torvalds 	int error;
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 	if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
16521da177e4SLinus Torvalds 		return error;
16531da177e4SLinus Torvalds 	dir->i_nlink++;
16541da177e4SLinus Torvalds 	return 0;
16551da177e4SLinus Torvalds }
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
16581da177e4SLinus Torvalds 		struct nameidata *nd)
16591da177e4SLinus Torvalds {
16601da177e4SLinus Torvalds 	return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
16611da177e4SLinus Torvalds }
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds /*
16641da177e4SLinus Torvalds  * Link a file..
16651da177e4SLinus Torvalds  */
16661da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
16671da177e4SLinus Torvalds {
16681da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
16691da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	/*
16721da177e4SLinus Torvalds 	 * No ordinary (disk based) filesystem counts links as inodes;
16731da177e4SLinus Torvalds 	 * but each new link needs a new dentry, pinning lowmem, and
16741da177e4SLinus Torvalds 	 * tmpfs dentries cannot be pruned until they are unlinked.
16751da177e4SLinus Torvalds 	 */
16760edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
16771da177e4SLinus Torvalds 		spin_lock(&sbinfo->stat_lock);
16781da177e4SLinus Torvalds 		if (!sbinfo->free_inodes) {
16791da177e4SLinus Torvalds 			spin_unlock(&sbinfo->stat_lock);
16801da177e4SLinus Torvalds 			return -ENOSPC;
16811da177e4SLinus Torvalds 		}
16821da177e4SLinus Torvalds 		sbinfo->free_inodes--;
16831da177e4SLinus Torvalds 		spin_unlock(&sbinfo->stat_lock);
16841da177e4SLinus Torvalds 	}
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
16871da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
16881da177e4SLinus Torvalds 	inode->i_nlink++;
16891da177e4SLinus Torvalds 	atomic_inc(&inode->i_count);	/* New dentry reference */
16901da177e4SLinus Torvalds 	dget(dentry);		/* Extra pinning count for the created dentry */
16911da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
16921da177e4SLinus Torvalds 	return 0;
16931da177e4SLinus Torvalds }
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry)
16961da177e4SLinus Torvalds {
16971da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 	if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) {
17001da177e4SLinus Torvalds 		struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
17010edd73b3SHugh Dickins 		if (sbinfo->max_inodes) {
17021da177e4SLinus Torvalds 			spin_lock(&sbinfo->stat_lock);
17031da177e4SLinus Torvalds 			sbinfo->free_inodes++;
17041da177e4SLinus Torvalds 			spin_unlock(&sbinfo->stat_lock);
17051da177e4SLinus Torvalds 		}
17061da177e4SLinus Torvalds 	}
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 	dir->i_size -= BOGO_DIRENT_SIZE;
17091da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
17101da177e4SLinus Torvalds 	inode->i_nlink--;
17111da177e4SLinus Torvalds 	dput(dentry);	/* Undo the count from "create" - this does all the work */
17121da177e4SLinus Torvalds 	return 0;
17131da177e4SLinus Torvalds }
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
17161da177e4SLinus Torvalds {
17171da177e4SLinus Torvalds 	if (!simple_empty(dentry))
17181da177e4SLinus Torvalds 		return -ENOTEMPTY;
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	dir->i_nlink--;
17211da177e4SLinus Torvalds 	return shmem_unlink(dir, dentry);
17221da177e4SLinus Torvalds }
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds /*
17251da177e4SLinus Torvalds  * The VFS layer already does all the dentry stuff for rename,
17261da177e4SLinus Torvalds  * we just have to decrement the usage count for the target if
17271da177e4SLinus Torvalds  * it exists so that the VFS layer correctly free's it when it
17281da177e4SLinus Torvalds  * gets overwritten.
17291da177e4SLinus Torvalds  */
17301da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
17311da177e4SLinus Torvalds {
17321da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
17331da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(inode->i_mode);
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
17361da177e4SLinus Torvalds 		return -ENOTEMPTY;
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
17391da177e4SLinus Torvalds 		(void) shmem_unlink(new_dir, new_dentry);
17401da177e4SLinus Torvalds 		if (they_are_dirs)
17411da177e4SLinus Torvalds 			old_dir->i_nlink--;
17421da177e4SLinus Torvalds 	} else if (they_are_dirs) {
17431da177e4SLinus Torvalds 		old_dir->i_nlink--;
17441da177e4SLinus Torvalds 		new_dir->i_nlink++;
17451da177e4SLinus Torvalds 	}
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds 	old_dir->i_size -= BOGO_DIRENT_SIZE;
17481da177e4SLinus Torvalds 	new_dir->i_size += BOGO_DIRENT_SIZE;
17491da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime =
17501da177e4SLinus Torvalds 	new_dir->i_ctime = new_dir->i_mtime =
17511da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME;
17521da177e4SLinus Torvalds 	return 0;
17531da177e4SLinus Torvalds }
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17561da177e4SLinus Torvalds {
17571da177e4SLinus Torvalds 	int error;
17581da177e4SLinus Torvalds 	int len;
17591da177e4SLinus Torvalds 	struct inode *inode;
17601da177e4SLinus Torvalds 	struct page *page = NULL;
17611da177e4SLinus Torvalds 	char *kaddr;
17621da177e4SLinus Torvalds 	struct shmem_inode_info *info;
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 	len = strlen(symname) + 1;
17651da177e4SLinus Torvalds 	if (len > PAGE_CACHE_SIZE)
17661da177e4SLinus Torvalds 		return -ENAMETOOLONG;
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
17691da177e4SLinus Torvalds 	if (!inode)
17701da177e4SLinus Torvalds 		return -ENOSPC;
17711da177e4SLinus Torvalds 
1772570bc1c2SStephen Smalley 	error = security_inode_init_security(inode, dir, NULL, NULL,
1773570bc1c2SStephen Smalley 					     NULL);
1774570bc1c2SStephen Smalley 	if (error) {
1775570bc1c2SStephen Smalley 		if (error != -EOPNOTSUPP) {
1776570bc1c2SStephen Smalley 			iput(inode);
1777570bc1c2SStephen Smalley 			return error;
1778570bc1c2SStephen Smalley 		}
1779570bc1c2SStephen Smalley 		error = 0;
1780570bc1c2SStephen Smalley 	}
1781570bc1c2SStephen Smalley 
17821da177e4SLinus Torvalds 	info = SHMEM_I(inode);
17831da177e4SLinus Torvalds 	inode->i_size = len-1;
17841da177e4SLinus Torvalds 	if (len <= (char *)inode - (char *)info) {
17851da177e4SLinus Torvalds 		/* do it inline */
17861da177e4SLinus Torvalds 		memcpy(info, symname, len);
17871da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inline_operations;
17881da177e4SLinus Torvalds 	} else {
17891da177e4SLinus Torvalds 		error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
17901da177e4SLinus Torvalds 		if (error) {
17911da177e4SLinus Torvalds 			iput(inode);
17921da177e4SLinus Torvalds 			return error;
17931da177e4SLinus Torvalds 		}
17941da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inode_operations;
17951da177e4SLinus Torvalds 		kaddr = kmap_atomic(page, KM_USER0);
17961da177e4SLinus Torvalds 		memcpy(kaddr, symname, len);
17971da177e4SLinus Torvalds 		kunmap_atomic(kaddr, KM_USER0);
17981da177e4SLinus Torvalds 		set_page_dirty(page);
17991da177e4SLinus Torvalds 		page_cache_release(page);
18001da177e4SLinus Torvalds 	}
18011da177e4SLinus Torvalds 	if (dir->i_mode & S_ISGID)
18021da177e4SLinus Torvalds 		inode->i_gid = dir->i_gid;
18031da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
18041da177e4SLinus Torvalds 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
18051da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
18061da177e4SLinus Torvalds 	dget(dentry);
18071da177e4SLinus Torvalds 	return 0;
18081da177e4SLinus Torvalds }
18091da177e4SLinus Torvalds 
1810cc314eefSLinus Torvalds static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
18111da177e4SLinus Torvalds {
18121da177e4SLinus Torvalds 	nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
1813cc314eefSLinus Torvalds 	return NULL;
18141da177e4SLinus Torvalds }
18151da177e4SLinus Torvalds 
1816cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
18171da177e4SLinus Torvalds {
18181da177e4SLinus Torvalds 	struct page *page = NULL;
18191da177e4SLinus Torvalds 	int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
18201da177e4SLinus Torvalds 	nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
1821cc314eefSLinus Torvalds 	return page;
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds 
1824cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
18251da177e4SLinus Torvalds {
18261da177e4SLinus Torvalds 	if (!IS_ERR(nd_get_link(nd))) {
1827cc314eefSLinus Torvalds 		struct page *page = cookie;
18281da177e4SLinus Torvalds 		kunmap(page);
18291da177e4SLinus Torvalds 		mark_page_accessed(page);
18301da177e4SLinus Torvalds 		page_cache_release(page);
18311da177e4SLinus Torvalds 	}
18321da177e4SLinus Torvalds }
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds static struct inode_operations shmem_symlink_inline_operations = {
18351da177e4SLinus Torvalds 	.readlink	= generic_readlink,
18361da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link_inline,
18371da177e4SLinus Torvalds };
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds static struct inode_operations shmem_symlink_inode_operations = {
18401da177e4SLinus Torvalds 	.truncate	= shmem_truncate,
18411da177e4SLinus Torvalds 	.readlink	= generic_readlink,
18421da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link,
18431da177e4SLinus Torvalds 	.put_link	= shmem_put_link,
18441da177e4SLinus Torvalds };
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds static int shmem_parse_options(char *options, int *mode, uid_t *uid, gid_t *gid, unsigned long *blocks, unsigned long *inodes)
18471da177e4SLinus Torvalds {
18481da177e4SLinus Torvalds 	char *this_char, *value, *rest;
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 	while ((this_char = strsep(&options, ",")) != NULL) {
18511da177e4SLinus Torvalds 		if (!*this_char)
18521da177e4SLinus Torvalds 			continue;
18531da177e4SLinus Torvalds 		if ((value = strchr(this_char,'=')) != NULL) {
18541da177e4SLinus Torvalds 			*value++ = 0;
18551da177e4SLinus Torvalds 		} else {
18561da177e4SLinus Torvalds 			printk(KERN_ERR
18571da177e4SLinus Torvalds 			    "tmpfs: No value for mount option '%s'\n",
18581da177e4SLinus Torvalds 			    this_char);
18591da177e4SLinus Torvalds 			return 1;
18601da177e4SLinus Torvalds 		}
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 		if (!strcmp(this_char,"size")) {
18631da177e4SLinus Torvalds 			unsigned long long size;
18641da177e4SLinus Torvalds 			size = memparse(value,&rest);
18651da177e4SLinus Torvalds 			if (*rest == '%') {
18661da177e4SLinus Torvalds 				size <<= PAGE_SHIFT;
18671da177e4SLinus Torvalds 				size *= totalram_pages;
18681da177e4SLinus Torvalds 				do_div(size, 100);
18691da177e4SLinus Torvalds 				rest++;
18701da177e4SLinus Torvalds 			}
18711da177e4SLinus Torvalds 			if (*rest)
18721da177e4SLinus Torvalds 				goto bad_val;
18731da177e4SLinus Torvalds 			*blocks = size >> PAGE_CACHE_SHIFT;
18741da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_blocks")) {
18751da177e4SLinus Torvalds 			*blocks = memparse(value,&rest);
18761da177e4SLinus Torvalds 			if (*rest)
18771da177e4SLinus Torvalds 				goto bad_val;
18781da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_inodes")) {
18791da177e4SLinus Torvalds 			*inodes = memparse(value,&rest);
18801da177e4SLinus Torvalds 			if (*rest)
18811da177e4SLinus Torvalds 				goto bad_val;
18821da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"mode")) {
18831da177e4SLinus Torvalds 			if (!mode)
18841da177e4SLinus Torvalds 				continue;
18851da177e4SLinus Torvalds 			*mode = simple_strtoul(value,&rest,8);
18861da177e4SLinus Torvalds 			if (*rest)
18871da177e4SLinus Torvalds 				goto bad_val;
18881da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"uid")) {
18891da177e4SLinus Torvalds 			if (!uid)
18901da177e4SLinus Torvalds 				continue;
18911da177e4SLinus Torvalds 			*uid = simple_strtoul(value,&rest,0);
18921da177e4SLinus Torvalds 			if (*rest)
18931da177e4SLinus Torvalds 				goto bad_val;
18941da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"gid")) {
18951da177e4SLinus Torvalds 			if (!gid)
18961da177e4SLinus Torvalds 				continue;
18971da177e4SLinus Torvalds 			*gid = simple_strtoul(value,&rest,0);
18981da177e4SLinus Torvalds 			if (*rest)
18991da177e4SLinus Torvalds 				goto bad_val;
19001da177e4SLinus Torvalds 		} else {
19011da177e4SLinus Torvalds 			printk(KERN_ERR "tmpfs: Bad mount option %s\n",
19021da177e4SLinus Torvalds 			       this_char);
19031da177e4SLinus Torvalds 			return 1;
19041da177e4SLinus Torvalds 		}
19051da177e4SLinus Torvalds 	}
19061da177e4SLinus Torvalds 	return 0;
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds bad_val:
19091da177e4SLinus Torvalds 	printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
19101da177e4SLinus Torvalds 	       value, this_char);
19111da177e4SLinus Torvalds 	return 1;
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds }
19141da177e4SLinus Torvalds 
19151da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
19161da177e4SLinus Torvalds {
19171da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
19180edd73b3SHugh Dickins 	unsigned long max_blocks = sbinfo->max_blocks;
19190edd73b3SHugh Dickins 	unsigned long max_inodes = sbinfo->max_inodes;
19200edd73b3SHugh Dickins 	unsigned long blocks;
19210edd73b3SHugh Dickins 	unsigned long inodes;
19220edd73b3SHugh Dickins 	int error = -EINVAL;
19231da177e4SLinus Torvalds 
19240edd73b3SHugh Dickins 	if (shmem_parse_options(data, NULL, NULL, NULL,
19250edd73b3SHugh Dickins 				&max_blocks, &max_inodes))
19260edd73b3SHugh Dickins 		return error;
19270edd73b3SHugh Dickins 
19280edd73b3SHugh Dickins 	spin_lock(&sbinfo->stat_lock);
19290edd73b3SHugh Dickins 	blocks = sbinfo->max_blocks - sbinfo->free_blocks;
19300edd73b3SHugh Dickins 	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
19310edd73b3SHugh Dickins 	if (max_blocks < blocks)
19320edd73b3SHugh Dickins 		goto out;
19330edd73b3SHugh Dickins 	if (max_inodes < inodes)
19340edd73b3SHugh Dickins 		goto out;
19350edd73b3SHugh Dickins 	/*
19360edd73b3SHugh Dickins 	 * Those tests also disallow limited->unlimited while any are in
19370edd73b3SHugh Dickins 	 * use, so i_blocks will always be zero when max_blocks is zero;
19380edd73b3SHugh Dickins 	 * but we must separately disallow unlimited->limited, because
19390edd73b3SHugh Dickins 	 * in that case we have no record of how much is already in use.
19400edd73b3SHugh Dickins 	 */
19410edd73b3SHugh Dickins 	if (max_blocks && !sbinfo->max_blocks)
19420edd73b3SHugh Dickins 		goto out;
19430edd73b3SHugh Dickins 	if (max_inodes && !sbinfo->max_inodes)
19440edd73b3SHugh Dickins 		goto out;
19450edd73b3SHugh Dickins 
19460edd73b3SHugh Dickins 	error = 0;
19470edd73b3SHugh Dickins 	sbinfo->max_blocks  = max_blocks;
19480edd73b3SHugh Dickins 	sbinfo->free_blocks = max_blocks - blocks;
19490edd73b3SHugh Dickins 	sbinfo->max_inodes  = max_inodes;
19500edd73b3SHugh Dickins 	sbinfo->free_inodes = max_inodes - inodes;
19510edd73b3SHugh Dickins out:
19520edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
19530edd73b3SHugh Dickins 	return error;
19541da177e4SLinus Torvalds }
19551da177e4SLinus Torvalds #endif
19561da177e4SLinus Torvalds 
19571da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb)
19581da177e4SLinus Torvalds {
19591da177e4SLinus Torvalds 	kfree(sb->s_fs_info);
19601da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
19611da177e4SLinus Torvalds }
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds static int shmem_fill_super(struct super_block *sb,
19641da177e4SLinus Torvalds 			    void *data, int silent)
19651da177e4SLinus Torvalds {
19661da177e4SLinus Torvalds 	struct inode *inode;
19671da177e4SLinus Torvalds 	struct dentry *root;
19681da177e4SLinus Torvalds 	int mode   = S_IRWXUGO | S_ISVTX;
19691da177e4SLinus Torvalds 	uid_t uid = current->fsuid;
19701da177e4SLinus Torvalds 	gid_t gid = current->fsgid;
19711da177e4SLinus Torvalds 	int err = -ENOMEM;
19720edd73b3SHugh Dickins 	struct shmem_sb_info *sbinfo;
19731da177e4SLinus Torvalds 	unsigned long blocks = 0;
19741da177e4SLinus Torvalds 	unsigned long inodes = 0;
19751da177e4SLinus Torvalds 
19760edd73b3SHugh Dickins #ifdef CONFIG_TMPFS
19771da177e4SLinus Torvalds 	/*
19781da177e4SLinus Torvalds 	 * Per default we only allow half of the physical ram per
19791da177e4SLinus Torvalds 	 * tmpfs instance, limiting inodes to one per page of lowmem;
19801da177e4SLinus Torvalds 	 * but the internal instance is left unlimited.
19811da177e4SLinus Torvalds 	 */
19821da177e4SLinus Torvalds 	if (!(sb->s_flags & MS_NOUSER)) {
19831da177e4SLinus Torvalds 		blocks = totalram_pages / 2;
19841da177e4SLinus Torvalds 		inodes = totalram_pages - totalhigh_pages;
19851da177e4SLinus Torvalds 		if (inodes > blocks)
19861da177e4SLinus Torvalds 			inodes = blocks;
19870edd73b3SHugh Dickins 		if (shmem_parse_options(data, &mode, &uid, &gid,
19880edd73b3SHugh Dickins 					&blocks, &inodes))
19891da177e4SLinus Torvalds 			return -EINVAL;
19901da177e4SLinus Torvalds 	}
19910edd73b3SHugh Dickins #else
19920edd73b3SHugh Dickins 	sb->s_flags |= MS_NOUSER;
19930edd73b3SHugh Dickins #endif
19941da177e4SLinus Torvalds 
19950edd73b3SHugh Dickins 	/* Round up to L1_CACHE_BYTES to resist false sharing */
19960edd73b3SHugh Dickins 	sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
19970edd73b3SHugh Dickins 				L1_CACHE_BYTES), GFP_KERNEL);
19981da177e4SLinus Torvalds 	if (!sbinfo)
19991da177e4SLinus Torvalds 		return -ENOMEM;
20000edd73b3SHugh Dickins 
20011da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
20021da177e4SLinus Torvalds 	sbinfo->max_blocks = blocks;
20031da177e4SLinus Torvalds 	sbinfo->free_blocks = blocks;
20041da177e4SLinus Torvalds 	sbinfo->max_inodes = inodes;
20051da177e4SLinus Torvalds 	sbinfo->free_inodes = inodes;
20061da177e4SLinus Torvalds 
20070edd73b3SHugh Dickins 	sb->s_fs_info = sbinfo;
20081da177e4SLinus Torvalds 	sb->s_maxbytes = SHMEM_MAX_BYTES;
20091da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
20101da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
20111da177e4SLinus Torvalds 	sb->s_magic = TMPFS_MAGIC;
20121da177e4SLinus Torvalds 	sb->s_op = &shmem_ops;
20130edd73b3SHugh Dickins 
20141da177e4SLinus Torvalds 	inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
20151da177e4SLinus Torvalds 	if (!inode)
20161da177e4SLinus Torvalds 		goto failed;
20171da177e4SLinus Torvalds 	inode->i_uid = uid;
20181da177e4SLinus Torvalds 	inode->i_gid = gid;
20191da177e4SLinus Torvalds 	root = d_alloc_root(inode);
20201da177e4SLinus Torvalds 	if (!root)
20211da177e4SLinus Torvalds 		goto failed_iput;
20221da177e4SLinus Torvalds 	sb->s_root = root;
20231da177e4SLinus Torvalds 	return 0;
20241da177e4SLinus Torvalds 
20251da177e4SLinus Torvalds failed_iput:
20261da177e4SLinus Torvalds 	iput(inode);
20271da177e4SLinus Torvalds failed:
20281da177e4SLinus Torvalds 	shmem_put_super(sb);
20291da177e4SLinus Torvalds 	return err;
20301da177e4SLinus Torvalds }
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds static kmem_cache_t *shmem_inode_cachep;
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb)
20351da177e4SLinus Torvalds {
20361da177e4SLinus Torvalds 	struct shmem_inode_info *p;
20371da177e4SLinus Torvalds 	p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, SLAB_KERNEL);
20381da177e4SLinus Torvalds 	if (!p)
20391da177e4SLinus Torvalds 		return NULL;
20401da177e4SLinus Torvalds 	return &p->vfs_inode;
20411da177e4SLinus Torvalds }
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode)
20441da177e4SLinus Torvalds {
20451da177e4SLinus Torvalds 	if ((inode->i_mode & S_IFMT) == S_IFREG) {
20461da177e4SLinus Torvalds 		/* only struct inode is valid if it's an inline symlink */
20471da177e4SLinus Torvalds 		mpol_free_shared_policy(&SHMEM_I(inode)->policy);
20481da177e4SLinus Torvalds 	}
20491da177e4SLinus Torvalds 	kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
20501da177e4SLinus Torvalds }
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
20531da177e4SLinus Torvalds {
20541da177e4SLinus Torvalds 	struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
20571da177e4SLinus Torvalds 	    SLAB_CTOR_CONSTRUCTOR) {
20581da177e4SLinus Torvalds 		inode_init_once(&p->vfs_inode);
20591da177e4SLinus Torvalds 	}
20601da177e4SLinus Torvalds }
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds static int init_inodecache(void)
20631da177e4SLinus Torvalds {
20641da177e4SLinus Torvalds 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
20651da177e4SLinus Torvalds 				sizeof(struct shmem_inode_info),
20661da177e4SLinus Torvalds 				0, 0, init_once, NULL);
20671da177e4SLinus Torvalds 	if (shmem_inode_cachep == NULL)
20681da177e4SLinus Torvalds 		return -ENOMEM;
20691da177e4SLinus Torvalds 	return 0;
20701da177e4SLinus Torvalds }
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds static void destroy_inodecache(void)
20731da177e4SLinus Torvalds {
20741da177e4SLinus Torvalds 	if (kmem_cache_destroy(shmem_inode_cachep))
20751da177e4SLinus Torvalds 		printk(KERN_INFO "shmem_inode_cache: not all structures were freed\n");
20761da177e4SLinus Torvalds }
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds static struct address_space_operations shmem_aops = {
20791da177e4SLinus Torvalds 	.writepage	= shmem_writepage,
20801da177e4SLinus Torvalds 	.set_page_dirty	= __set_page_dirty_nobuffers,
20811da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
20821da177e4SLinus Torvalds 	.prepare_write	= shmem_prepare_write,
20831da177e4SLinus Torvalds 	.commit_write	= simple_commit_write,
20841da177e4SLinus Torvalds #endif
20851da177e4SLinus Torvalds };
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds static struct file_operations shmem_file_operations = {
20881da177e4SLinus Torvalds 	.mmap		= shmem_mmap,
20891da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
20901da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
20911da177e4SLinus Torvalds 	.read		= shmem_file_read,
20921da177e4SLinus Torvalds 	.write		= shmem_file_write,
20931da177e4SLinus Torvalds 	.fsync		= simple_sync_file,
20941da177e4SLinus Torvalds 	.sendfile	= shmem_file_sendfile,
20951da177e4SLinus Torvalds #endif
20961da177e4SLinus Torvalds };
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds static struct inode_operations shmem_inode_operations = {
20991da177e4SLinus Torvalds 	.truncate	= shmem_truncate,
21001da177e4SLinus Torvalds 	.setattr	= shmem_notify_change,
2101*f6b3ec23SBadari Pulavarty 	.truncate_range	= shmem_truncate_range,
21021da177e4SLinus Torvalds };
21031da177e4SLinus Torvalds 
21041da177e4SLinus Torvalds static struct inode_operations shmem_dir_inode_operations = {
21051da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
21061da177e4SLinus Torvalds 	.create		= shmem_create,
21071da177e4SLinus Torvalds 	.lookup		= simple_lookup,
21081da177e4SLinus Torvalds 	.link		= shmem_link,
21091da177e4SLinus Torvalds 	.unlink		= shmem_unlink,
21101da177e4SLinus Torvalds 	.symlink	= shmem_symlink,
21111da177e4SLinus Torvalds 	.mkdir		= shmem_mkdir,
21121da177e4SLinus Torvalds 	.rmdir		= shmem_rmdir,
21131da177e4SLinus Torvalds 	.mknod		= shmem_mknod,
21141da177e4SLinus Torvalds 	.rename		= shmem_rename,
21151da177e4SLinus Torvalds #endif
21161da177e4SLinus Torvalds };
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds static struct super_operations shmem_ops = {
21191da177e4SLinus Torvalds 	.alloc_inode	= shmem_alloc_inode,
21201da177e4SLinus Torvalds 	.destroy_inode	= shmem_destroy_inode,
21211da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
21221da177e4SLinus Torvalds 	.statfs		= shmem_statfs,
21231da177e4SLinus Torvalds 	.remount_fs	= shmem_remount_fs,
21241da177e4SLinus Torvalds #endif
21251da177e4SLinus Torvalds 	.delete_inode	= shmem_delete_inode,
21261da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
21271da177e4SLinus Torvalds 	.put_super	= shmem_put_super,
21281da177e4SLinus Torvalds };
21291da177e4SLinus Torvalds 
21301da177e4SLinus Torvalds static struct vm_operations_struct shmem_vm_ops = {
21311da177e4SLinus Torvalds 	.nopage		= shmem_nopage,
21321da177e4SLinus Torvalds 	.populate	= shmem_populate,
21331da177e4SLinus Torvalds #ifdef CONFIG_NUMA
21341da177e4SLinus Torvalds 	.set_policy     = shmem_set_policy,
21351da177e4SLinus Torvalds 	.get_policy     = shmem_get_policy,
21361da177e4SLinus Torvalds #endif
21371da177e4SLinus Torvalds };
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds static struct super_block *shmem_get_sb(struct file_system_type *fs_type,
21411da177e4SLinus Torvalds 	int flags, const char *dev_name, void *data)
21421da177e4SLinus Torvalds {
21431da177e4SLinus Torvalds 	return get_sb_nodev(fs_type, flags, data, shmem_fill_super);
21441da177e4SLinus Torvalds }
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds static struct file_system_type tmpfs_fs_type = {
21471da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
21481da177e4SLinus Torvalds 	.name		= "tmpfs",
21491da177e4SLinus Torvalds 	.get_sb		= shmem_get_sb,
21501da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
21511da177e4SLinus Torvalds };
21521da177e4SLinus Torvalds static struct vfsmount *shm_mnt;
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds static int __init init_tmpfs(void)
21551da177e4SLinus Torvalds {
21561da177e4SLinus Torvalds 	int error;
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds 	error = init_inodecache();
21591da177e4SLinus Torvalds 	if (error)
21601da177e4SLinus Torvalds 		goto out3;
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 	error = register_filesystem(&tmpfs_fs_type);
21631da177e4SLinus Torvalds 	if (error) {
21641da177e4SLinus Torvalds 		printk(KERN_ERR "Could not register tmpfs\n");
21651da177e4SLinus Torvalds 		goto out2;
21661da177e4SLinus Torvalds 	}
21671da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
21681da177e4SLinus Torvalds 	devfs_mk_dir("shm");
21691da177e4SLinus Torvalds #endif
21701da177e4SLinus Torvalds 	shm_mnt = do_kern_mount(tmpfs_fs_type.name, MS_NOUSER,
21711da177e4SLinus Torvalds 				tmpfs_fs_type.name, NULL);
21721da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt)) {
21731da177e4SLinus Torvalds 		error = PTR_ERR(shm_mnt);
21741da177e4SLinus Torvalds 		printk(KERN_ERR "Could not kern_mount tmpfs\n");
21751da177e4SLinus Torvalds 		goto out1;
21761da177e4SLinus Torvalds 	}
21771da177e4SLinus Torvalds 	return 0;
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds out1:
21801da177e4SLinus Torvalds 	unregister_filesystem(&tmpfs_fs_type);
21811da177e4SLinus Torvalds out2:
21821da177e4SLinus Torvalds 	destroy_inodecache();
21831da177e4SLinus Torvalds out3:
21841da177e4SLinus Torvalds 	shm_mnt = ERR_PTR(error);
21851da177e4SLinus Torvalds 	return error;
21861da177e4SLinus Torvalds }
21871da177e4SLinus Torvalds module_init(init_tmpfs)
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds /*
21901da177e4SLinus Torvalds  * shmem_file_setup - get an unlinked file living in tmpfs
21911da177e4SLinus Torvalds  *
21921da177e4SLinus Torvalds  * @name: name for dentry (to be seen in /proc/<pid>/maps
21931da177e4SLinus Torvalds  * @size: size to be set for the file
21941da177e4SLinus Torvalds  *
21951da177e4SLinus Torvalds  */
21961da177e4SLinus Torvalds struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
21971da177e4SLinus Torvalds {
21981da177e4SLinus Torvalds 	int error;
21991da177e4SLinus Torvalds 	struct file *file;
22001da177e4SLinus Torvalds 	struct inode *inode;
22011da177e4SLinus Torvalds 	struct dentry *dentry, *root;
22021da177e4SLinus Torvalds 	struct qstr this;
22031da177e4SLinus Torvalds 
22041da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt))
22051da177e4SLinus Torvalds 		return (void *)shm_mnt;
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds 	if (size < 0 || size > SHMEM_MAX_BYTES)
22081da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 	if (shmem_acct_size(flags, size))
22111da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 	error = -ENOMEM;
22141da177e4SLinus Torvalds 	this.name = name;
22151da177e4SLinus Torvalds 	this.len = strlen(name);
22161da177e4SLinus Torvalds 	this.hash = 0; /* will go */
22171da177e4SLinus Torvalds 	root = shm_mnt->mnt_root;
22181da177e4SLinus Torvalds 	dentry = d_alloc(root, &this);
22191da177e4SLinus Torvalds 	if (!dentry)
22201da177e4SLinus Torvalds 		goto put_memory;
22211da177e4SLinus Torvalds 
22221da177e4SLinus Torvalds 	error = -ENFILE;
22231da177e4SLinus Torvalds 	file = get_empty_filp();
22241da177e4SLinus Torvalds 	if (!file)
22251da177e4SLinus Torvalds 		goto put_dentry;
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds 	error = -ENOSPC;
22281da177e4SLinus Torvalds 	inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
22291da177e4SLinus Torvalds 	if (!inode)
22301da177e4SLinus Torvalds 		goto close_file;
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 	SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
22331da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
22341da177e4SLinus Torvalds 	inode->i_size = size;
22351da177e4SLinus Torvalds 	inode->i_nlink = 0;	/* It is unlinked */
22361da177e4SLinus Torvalds 	file->f_vfsmnt = mntget(shm_mnt);
22371da177e4SLinus Torvalds 	file->f_dentry = dentry;
22381da177e4SLinus Torvalds 	file->f_mapping = inode->i_mapping;
22391da177e4SLinus Torvalds 	file->f_op = &shmem_file_operations;
22401da177e4SLinus Torvalds 	file->f_mode = FMODE_WRITE | FMODE_READ;
22411da177e4SLinus Torvalds 	return file;
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds close_file:
22441da177e4SLinus Torvalds 	put_filp(file);
22451da177e4SLinus Torvalds put_dentry:
22461da177e4SLinus Torvalds 	dput(dentry);
22471da177e4SLinus Torvalds put_memory:
22481da177e4SLinus Torvalds 	shmem_unacct_size(flags, size);
22491da177e4SLinus Torvalds 	return ERR_PTR(error);
22501da177e4SLinus Torvalds }
22511da177e4SLinus Torvalds 
22521da177e4SLinus Torvalds /*
22531da177e4SLinus Torvalds  * shmem_zero_setup - setup a shared anonymous mapping
22541da177e4SLinus Torvalds  *
22551da177e4SLinus Torvalds  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
22561da177e4SLinus Torvalds  */
22571da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma)
22581da177e4SLinus Torvalds {
22591da177e4SLinus Torvalds 	struct file *file;
22601da177e4SLinus Torvalds 	loff_t size = vma->vm_end - vma->vm_start;
22611da177e4SLinus Torvalds 
22621da177e4SLinus Torvalds 	file = shmem_file_setup("dev/zero", size, vma->vm_flags);
22631da177e4SLinus Torvalds 	if (IS_ERR(file))
22641da177e4SLinus Torvalds 		return PTR_ERR(file);
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds 	if (vma->vm_file)
22671da177e4SLinus Torvalds 		fput(vma->vm_file);
22681da177e4SLinus Torvalds 	vma->vm_file = file;
22691da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
22701da177e4SLinus Torvalds 	return 0;
22711da177e4SLinus Torvalds }
2272