xref: /openbmc/linux/mm/shmem.c (revision b09e0fa4b4ea66266058eead43350bd7d55fec67)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Resizable virtual memory filesystem for Linux.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2000 Linus Torvalds.
51da177e4SLinus Torvalds  *		 2000 Transmeta Corp.
61da177e4SLinus Torvalds  *		 2000-2001 Christoph Rohland
71da177e4SLinus Torvalds  *		 2000-2001 SAP AG
81da177e4SLinus Torvalds  *		 2002 Red Hat Inc.
90edd73b3SHugh Dickins  * Copyright (C) 2002-2005 Hugh Dickins.
100edd73b3SHugh Dickins  * Copyright (C) 2002-2005 VERITAS Software Corporation.
111da177e4SLinus Torvalds  * Copyright (C) 2004 Andi Kleen, SuSE Labs
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * Extended attribute support for tmpfs:
141da177e4SLinus Torvalds  * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
151da177e4SLinus Torvalds  * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
161da177e4SLinus Torvalds  *
17853ac43aSMatt Mackall  * tiny-shmem:
18853ac43aSMatt Mackall  * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
19853ac43aSMatt Mackall  *
201da177e4SLinus Torvalds  * This file is released under the GPL.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
23853ac43aSMatt Mackall #include <linux/fs.h>
24853ac43aSMatt Mackall #include <linux/init.h>
25853ac43aSMatt Mackall #include <linux/vfs.h>
26853ac43aSMatt Mackall #include <linux/mount.h>
27caefba17SHugh Dickins #include <linux/pagemap.h>
28853ac43aSMatt Mackall #include <linux/file.h>
29853ac43aSMatt Mackall #include <linux/mm.h>
30853ac43aSMatt Mackall #include <linux/module.h>
317e496299STim Chen #include <linux/percpu_counter.h>
32853ac43aSMatt Mackall #include <linux/swap.h>
33853ac43aSMatt Mackall 
34853ac43aSMatt Mackall static struct vfsmount *shm_mnt;
35853ac43aSMatt Mackall 
36853ac43aSMatt Mackall #ifdef CONFIG_SHMEM
371da177e4SLinus Torvalds /*
381da177e4SLinus Torvalds  * This virtual memory filesystem is heavily based on the ramfs. It
391da177e4SLinus Torvalds  * extends ramfs by the ability to use swap and honor resource limits
401da177e4SLinus Torvalds  * which makes it a completely usable filesystem.
411da177e4SLinus Torvalds  */
421da177e4SLinus Torvalds 
4339f0247dSAndreas Gruenbacher #include <linux/xattr.h>
44a5694255SChristoph Hellwig #include <linux/exportfs.h>
451c7c474cSChristoph Hellwig #include <linux/posix_acl.h>
4639f0247dSAndreas Gruenbacher #include <linux/generic_acl.h>
471da177e4SLinus Torvalds #include <linux/mman.h>
481da177e4SLinus Torvalds #include <linux/string.h>
491da177e4SLinus Torvalds #include <linux/slab.h>
501da177e4SLinus Torvalds #include <linux/backing-dev.h>
511da177e4SLinus Torvalds #include <linux/shmem_fs.h>
521da177e4SLinus Torvalds #include <linux/writeback.h>
531da177e4SLinus Torvalds #include <linux/blkdev.h>
541da177e4SLinus Torvalds #include <linux/security.h>
551da177e4SLinus Torvalds #include <linux/swapops.h>
561da177e4SLinus Torvalds #include <linux/mempolicy.h>
571da177e4SLinus Torvalds #include <linux/namei.h>
58b00dc3adSHugh Dickins #include <linux/ctype.h>
59304dbdb7SLee Schermerhorn #include <linux/migrate.h>
60c1f60a5aSChristoph Lameter #include <linux/highmem.h>
61680d794bSakpm@linux-foundation.org #include <linux/seq_file.h>
6292562927SMimi Zohar #include <linux/magic.h>
63304dbdb7SLee Schermerhorn 
641da177e4SLinus Torvalds #include <asm/uaccess.h>
651da177e4SLinus Torvalds #include <asm/div64.h>
661da177e4SLinus Torvalds #include <asm/pgtable.h>
671da177e4SLinus Torvalds 
68caefba17SHugh Dickins /*
69caefba17SHugh Dickins  * The maximum size of a shmem/tmpfs file is limited by the maximum size of
70caefba17SHugh Dickins  * its triple-indirect swap vector - see illustration at shmem_swp_entry().
71caefba17SHugh Dickins  *
72caefba17SHugh Dickins  * With 4kB page size, maximum file size is just over 2TB on a 32-bit kernel,
73caefba17SHugh Dickins  * but one eighth of that on a 64-bit kernel.  With 8kB page size, maximum
74caefba17SHugh Dickins  * file size is just over 4TB on a 64-bit kernel, but 16TB on a 32-bit kernel,
75caefba17SHugh Dickins  * MAX_LFS_FILESIZE being then more restrictive than swap vector layout.
76caefba17SHugh Dickins  *
77caefba17SHugh Dickins  * We use / and * instead of shifts in the definitions below, so that the swap
78caefba17SHugh Dickins  * vector can be tested with small even values (e.g. 20) for ENTRIES_PER_PAGE.
79caefba17SHugh Dickins  */
801da177e4SLinus Torvalds #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
8161609d01SYuri Tikhonov #define ENTRIES_PER_PAGEPAGE ((unsigned long long)ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
82caefba17SHugh Dickins 
83caefba17SHugh Dickins #define SHMSWP_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
84caefba17SHugh Dickins #define SHMSWP_MAX_BYTES (SHMSWP_MAX_INDEX << PAGE_CACHE_SHIFT)
85caefba17SHugh Dickins 
86caefba17SHugh Dickins #define SHMEM_MAX_BYTES  min_t(unsigned long long, SHMSWP_MAX_BYTES, MAX_LFS_FILESIZE)
87caefba17SHugh Dickins #define SHMEM_MAX_INDEX  ((unsigned long)((SHMEM_MAX_BYTES+1) >> PAGE_CACHE_SHIFT))
88caefba17SHugh Dickins 
891da177e4SLinus Torvalds #define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
901da177e4SLinus Torvalds #define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
931da177e4SLinus Torvalds #define SHMEM_PAGEIN	 VM_READ
941da177e4SLinus Torvalds #define SHMEM_TRUNCATE	 VM_WRITE
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /* Definition to limit shmem_truncate's steps between cond_rescheds */
971da177e4SLinus Torvalds #define LATENCY_LIMIT	 64
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */
1001da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20
1011da177e4SLinus Torvalds 
102*b09e0fa4SEric Paris struct shmem_xattr {
103*b09e0fa4SEric Paris 	struct list_head list;	/* anchored by shmem_inode_info->xattr_list */
104*b09e0fa4SEric Paris 	char *name;		/* xattr name */
105*b09e0fa4SEric Paris 	size_t size;
106*b09e0fa4SEric Paris 	char value[0];
107*b09e0fa4SEric Paris };
108*b09e0fa4SEric Paris 
1091da177e4SLinus Torvalds /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
1101da177e4SLinus Torvalds enum sgp_type {
1111da177e4SLinus Torvalds 	SGP_READ,	/* don't exceed i_size, don't allocate page */
1121da177e4SLinus Torvalds 	SGP_CACHE,	/* don't exceed i_size, may allocate page */
113a0ee5ec5SHugh Dickins 	SGP_DIRTY,	/* like SGP_CACHE, but set new page dirty */
1141da177e4SLinus Torvalds 	SGP_WRITE,	/* may exceed i_size, may allocate page */
1151da177e4SLinus Torvalds };
1161da177e4SLinus Torvalds 
117b76db735SAndrew Morton #ifdef CONFIG_TMPFS
118680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void)
119680d794bSakpm@linux-foundation.org {
120680d794bSakpm@linux-foundation.org 	return totalram_pages / 2;
121680d794bSakpm@linux-foundation.org }
122680d794bSakpm@linux-foundation.org 
123680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void)
124680d794bSakpm@linux-foundation.org {
125680d794bSakpm@linux-foundation.org 	return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
126680d794bSakpm@linux-foundation.org }
127b76db735SAndrew Morton #endif
128680d794bSakpm@linux-foundation.org 
1291da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx,
1301da177e4SLinus Torvalds 			 struct page **pagep, enum sgp_type sgp, int *type);
1311da177e4SLinus Torvalds 
1326daa0e28SAl Viro static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	/*
1351da177e4SLinus Torvalds 	 * The above definition of ENTRIES_PER_PAGE, and the use of
1361da177e4SLinus Torvalds 	 * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
1371da177e4SLinus Torvalds 	 * might be reconsidered if it ever diverges from PAGE_SIZE.
138769848c0SMel Gorman 	 *
139e12ba74dSMel Gorman 	 * Mobility flags are masked out as swap vectors cannot move
1401da177e4SLinus Torvalds 	 */
141e12ba74dSMel Gorman 	return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
142769848c0SMel Gorman 				PAGE_CACHE_SHIFT-PAGE_SHIFT);
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds static inline void shmem_dir_free(struct page *page)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	__free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static struct page **shmem_dir_map(struct page *page)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	return (struct page **)kmap_atomic(page, KM_USER0);
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds static inline void shmem_dir_unmap(struct page **dir)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	kunmap_atomic(dir, KM_USER0);
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds static swp_entry_t *shmem_swp_map(struct page *page)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	return (swp_entry_t *)kmap_atomic(page, KM_USER1);
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds static inline void shmem_swp_balance_unmap(void)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	/*
1681da177e4SLinus Torvalds 	 * When passing a pointer to an i_direct entry, to code which
1691da177e4SLinus Torvalds 	 * also handles indirect entries and so will shmem_swp_unmap,
1701da177e4SLinus Torvalds 	 * we must arrange for the preempt count to remain in balance.
1711da177e4SLinus Torvalds 	 * What kmap_atomic of a lowmem page does depends on config
1721da177e4SLinus Torvalds 	 * and architecture, so pretend to kmap_atomic some lowmem page.
1731da177e4SLinus Torvalds 	 */
1741da177e4SLinus Torvalds 	(void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds static inline void shmem_swp_unmap(swp_entry_t *entry)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	kunmap_atomic(entry, KM_USER1);
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	return sb->s_fs_info;
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds /*
1881da177e4SLinus Torvalds  * shmem_file_setup pre-accounts the whole fixed size of a VM object,
1891da177e4SLinus Torvalds  * for shared memory and for shared anonymous (/dev/zero) mappings
1901da177e4SLinus Torvalds  * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
1911da177e4SLinus Torvalds  * consistent with the pre-accounting of private mappings ...
1921da177e4SLinus Torvalds  */
1931da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size)
1941da177e4SLinus Torvalds {
1950b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
1960b0a0806SHugh Dickins 		0 : security_vm_enough_memory_kern(VM_ACCT(size));
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size)
2001da177e4SLinus Torvalds {
2010b0a0806SHugh Dickins 	if (!(flags & VM_NORESERVE))
2021da177e4SLinus Torvalds 		vm_unacct_memory(VM_ACCT(size));
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /*
2061da177e4SLinus Torvalds  * ... whereas tmpfs objects are accounted incrementally as
2071da177e4SLinus Torvalds  * pages are allocated, in order to allow huge sparse files.
2081da177e4SLinus Torvalds  * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
2091da177e4SLinus Torvalds  * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
2101da177e4SLinus Torvalds  */
2111da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags)
2121da177e4SLinus Torvalds {
2130b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
2140b0a0806SHugh Dickins 		security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages)
2181da177e4SLinus Torvalds {
2190b0a0806SHugh Dickins 	if (flags & VM_NORESERVE)
2201da177e4SLinus Torvalds 		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
223759b9775SHugh Dickins static const struct super_operations shmem_ops;
224f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops;
22515ad7cdcSHelge Deller static const struct file_operations shmem_file_operations;
22692e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations;
22792e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations;
22892e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations;
229f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops;
2301da177e4SLinus Torvalds 
2316c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info  __read_mostly = {
2321da177e4SLinus Torvalds 	.ra_pages	= 0,	/* No readahead */
2334f98a2feSRik van Riel 	.capabilities	= BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
2341da177e4SLinus Torvalds };
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist);
237cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex);
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds static void shmem_free_blocks(struct inode *inode, long pages)
2401da177e4SLinus Torvalds {
2411da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
2420edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
2437e496299STim Chen 		percpu_counter_add(&sbinfo->used_blocks, -pages);
2447e496299STim Chen 		spin_lock(&inode->i_lock);
2451da177e4SLinus Torvalds 		inode->i_blocks -= pages*BLOCKS_PER_PAGE;
2467e496299STim Chen 		spin_unlock(&inode->i_lock);
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2505b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb)
2515b04c689SPavel Emelyanov {
2525b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2535b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2545b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2555b04c689SPavel Emelyanov 		if (!sbinfo->free_inodes) {
2565b04c689SPavel Emelyanov 			spin_unlock(&sbinfo->stat_lock);
2575b04c689SPavel Emelyanov 			return -ENOSPC;
2585b04c689SPavel Emelyanov 		}
2595b04c689SPavel Emelyanov 		sbinfo->free_inodes--;
2605b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2615b04c689SPavel Emelyanov 	}
2625b04c689SPavel Emelyanov 	return 0;
2635b04c689SPavel Emelyanov }
2645b04c689SPavel Emelyanov 
2655b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb)
2665b04c689SPavel Emelyanov {
2675b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2685b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2695b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2705b04c689SPavel Emelyanov 		sbinfo->free_inodes++;
2715b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2725b04c689SPavel Emelyanov 	}
2735b04c689SPavel Emelyanov }
2745b04c689SPavel Emelyanov 
27546711810SRandy Dunlap /**
2761da177e4SLinus Torvalds  * shmem_recalc_inode - recalculate the size of an inode
2771da177e4SLinus Torvalds  * @inode: inode to recalc
2781da177e4SLinus Torvalds  *
2791da177e4SLinus Torvalds  * We have to calculate the free blocks since the mm can drop
2801da177e4SLinus Torvalds  * undirtied hole pages behind our back.
2811da177e4SLinus Torvalds  *
2821da177e4SLinus Torvalds  * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
2831da177e4SLinus Torvalds  * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
2841da177e4SLinus Torvalds  *
2851da177e4SLinus Torvalds  * It has to be called with the spinlock held.
2861da177e4SLinus Torvalds  */
2871da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode)
2881da177e4SLinus Torvalds {
2891da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
2901da177e4SLinus Torvalds 	long freed;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
2931da177e4SLinus Torvalds 	if (freed > 0) {
2941da177e4SLinus Torvalds 		info->alloced -= freed;
2951da177e4SLinus Torvalds 		shmem_unacct_blocks(info->flags, freed);
2961da177e4SLinus Torvalds 		shmem_free_blocks(inode, freed);
2971da177e4SLinus Torvalds 	}
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
30046711810SRandy Dunlap /**
3011da177e4SLinus Torvalds  * shmem_swp_entry - find the swap vector position in the info structure
3021da177e4SLinus Torvalds  * @info:  info structure for the inode
3031da177e4SLinus Torvalds  * @index: index of the page to find
3041da177e4SLinus Torvalds  * @page:  optional page to add to the structure. Has to be preset to
3051da177e4SLinus Torvalds  *         all zeros
3061da177e4SLinus Torvalds  *
3071da177e4SLinus Torvalds  * If there is no space allocated yet it will return NULL when
3081da177e4SLinus Torvalds  * page is NULL, else it will use the page for the needed block,
3091da177e4SLinus Torvalds  * setting it to NULL on return to indicate that it has been used.
3101da177e4SLinus Torvalds  *
3111da177e4SLinus Torvalds  * The swap vector is organized the following way:
3121da177e4SLinus Torvalds  *
3131da177e4SLinus Torvalds  * There are SHMEM_NR_DIRECT entries directly stored in the
3141da177e4SLinus Torvalds  * shmem_inode_info structure. So small files do not need an addional
3151da177e4SLinus Torvalds  * allocation.
3161da177e4SLinus Torvalds  *
3171da177e4SLinus Torvalds  * For pages with index > SHMEM_NR_DIRECT there is the pointer
3181da177e4SLinus Torvalds  * i_indirect which points to a page which holds in the first half
3191da177e4SLinus Torvalds  * doubly indirect blocks, in the second half triple indirect blocks:
3201da177e4SLinus Torvalds  *
3211da177e4SLinus Torvalds  * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
3221da177e4SLinus Torvalds  * following layout (for SHMEM_NR_DIRECT == 16):
3231da177e4SLinus Torvalds  *
3241da177e4SLinus Torvalds  * i_indirect -> dir --> 16-19
3251da177e4SLinus Torvalds  * 	      |	     +-> 20-23
3261da177e4SLinus Torvalds  * 	      |
3271da177e4SLinus Torvalds  * 	      +-->dir2 --> 24-27
3281da177e4SLinus Torvalds  * 	      |	       +-> 28-31
3291da177e4SLinus Torvalds  * 	      |	       +-> 32-35
3301da177e4SLinus Torvalds  * 	      |	       +-> 36-39
3311da177e4SLinus Torvalds  * 	      |
3321da177e4SLinus Torvalds  * 	      +-->dir3 --> 40-43
3331da177e4SLinus Torvalds  * 	       	       +-> 44-47
3341da177e4SLinus Torvalds  * 	      	       +-> 48-51
3351da177e4SLinus Torvalds  * 	      	       +-> 52-55
3361da177e4SLinus Torvalds  */
3371da177e4SLinus Torvalds static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	unsigned long offset;
3401da177e4SLinus Torvalds 	struct page **dir;
3411da177e4SLinus Torvalds 	struct page *subdir;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	if (index < SHMEM_NR_DIRECT) {
3441da177e4SLinus Torvalds 		shmem_swp_balance_unmap();
3451da177e4SLinus Torvalds 		return info->i_direct+index;
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 	if (!info->i_indirect) {
3481da177e4SLinus Torvalds 		if (page) {
3491da177e4SLinus Torvalds 			info->i_indirect = *page;
3501da177e4SLinus Torvalds 			*page = NULL;
3511da177e4SLinus Torvalds 		}
3521da177e4SLinus Torvalds 		return NULL;			/* need another page */
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	index -= SHMEM_NR_DIRECT;
3561da177e4SLinus Torvalds 	offset = index % ENTRIES_PER_PAGE;
3571da177e4SLinus Torvalds 	index /= ENTRIES_PER_PAGE;
3581da177e4SLinus Torvalds 	dir = shmem_dir_map(info->i_indirect);
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	if (index >= ENTRIES_PER_PAGE/2) {
3611da177e4SLinus Torvalds 		index -= ENTRIES_PER_PAGE/2;
3621da177e4SLinus Torvalds 		dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
3631da177e4SLinus Torvalds 		index %= ENTRIES_PER_PAGE;
3641da177e4SLinus Torvalds 		subdir = *dir;
3651da177e4SLinus Torvalds 		if (!subdir) {
3661da177e4SLinus Torvalds 			if (page) {
3671da177e4SLinus Torvalds 				*dir = *page;
3681da177e4SLinus Torvalds 				*page = NULL;
3691da177e4SLinus Torvalds 			}
3701da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
3711da177e4SLinus Torvalds 			return NULL;		/* need another page */
3721da177e4SLinus Torvalds 		}
3731da177e4SLinus Torvalds 		shmem_dir_unmap(dir);
3741da177e4SLinus Torvalds 		dir = shmem_dir_map(subdir);
3751da177e4SLinus Torvalds 	}
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	dir += index;
3781da177e4SLinus Torvalds 	subdir = *dir;
3791da177e4SLinus Torvalds 	if (!subdir) {
3801da177e4SLinus Torvalds 		if (!page || !(subdir = *page)) {
3811da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
3821da177e4SLinus Torvalds 			return NULL;		/* need a page */
3831da177e4SLinus Torvalds 		}
3841da177e4SLinus Torvalds 		*dir = subdir;
3851da177e4SLinus Torvalds 		*page = NULL;
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 	shmem_dir_unmap(dir);
3881da177e4SLinus Torvalds 	return shmem_swp_map(subdir) + offset;
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	long incdec = value? 1: -1;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	entry->val = value;
3961da177e4SLinus Torvalds 	info->swapped += incdec;
3974c21e2f2SHugh Dickins 	if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
3984c21e2f2SHugh Dickins 		struct page *page = kmap_atomic_to_page(entry);
3994c21e2f2SHugh Dickins 		set_page_private(page, page_private(page) + incdec);
4004c21e2f2SHugh Dickins 	}
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
40346711810SRandy Dunlap /**
4041da177e4SLinus Torvalds  * shmem_swp_alloc - get the position of the swap entry for the page.
4051da177e4SLinus Torvalds  * @info:	info structure for the inode
4061da177e4SLinus Torvalds  * @index:	index of the page to find
4071da177e4SLinus Torvalds  * @sgp:	check and recheck i_size? skip allocation?
40846711810SRandy Dunlap  *
40946711810SRandy Dunlap  * If the entry does not exist, allocate it.
4101da177e4SLinus Torvalds  */
4111da177e4SLinus Torvalds static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	struct inode *inode = &info->vfs_inode;
4141da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
4151da177e4SLinus Torvalds 	struct page *page = NULL;
4161da177e4SLinus Torvalds 	swp_entry_t *entry;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	if (sgp != SGP_WRITE &&
4191da177e4SLinus Torvalds 	    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
4201da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	while (!(entry = shmem_swp_entry(info, index, &page))) {
4231da177e4SLinus Torvalds 		if (sgp == SGP_READ)
4241da177e4SLinus Torvalds 			return shmem_swp_map(ZERO_PAGE(0));
4251da177e4SLinus Torvalds 		/*
4267e496299STim Chen 		 * Test used_blocks against 1 less max_blocks, since we have 1 data
4271da177e4SLinus Torvalds 		 * page (and perhaps indirect index pages) yet to allocate:
4281da177e4SLinus Torvalds 		 * a waste to allocate index if we cannot allocate data.
4291da177e4SLinus Torvalds 		 */
4300edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
431fc5da22aSHugh Dickins 			if (percpu_counter_compare(&sbinfo->used_blocks,
432fc5da22aSHugh Dickins 						sbinfo->max_blocks - 1) >= 0)
4331da177e4SLinus Torvalds 				return ERR_PTR(-ENOSPC);
4347e496299STim Chen 			percpu_counter_inc(&sbinfo->used_blocks);
4357e496299STim Chen 			spin_lock(&inode->i_lock);
4361da177e4SLinus Torvalds 			inode->i_blocks += BLOCKS_PER_PAGE;
4377e496299STim Chen 			spin_unlock(&inode->i_lock);
4381da177e4SLinus Torvalds 		}
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 		spin_unlock(&info->lock);
441769848c0SMel Gorman 		page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping));
4421da177e4SLinus Torvalds 		spin_lock(&info->lock);
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 		if (!page) {
4451da177e4SLinus Torvalds 			shmem_free_blocks(inode, 1);
4461da177e4SLinus Torvalds 			return ERR_PTR(-ENOMEM);
4471da177e4SLinus Torvalds 		}
4481da177e4SLinus Torvalds 		if (sgp != SGP_WRITE &&
4491da177e4SLinus Torvalds 		    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
4501da177e4SLinus Torvalds 			entry = ERR_PTR(-EINVAL);
4511da177e4SLinus Torvalds 			break;
4521da177e4SLinus Torvalds 		}
4531da177e4SLinus Torvalds 		if (info->next_index <= index)
4541da177e4SLinus Torvalds 			info->next_index = index + 1;
4551da177e4SLinus Torvalds 	}
4561da177e4SLinus Torvalds 	if (page) {
4571da177e4SLinus Torvalds 		/* another task gave its page, or truncated the file */
4581da177e4SLinus Torvalds 		shmem_free_blocks(inode, 1);
4591da177e4SLinus Torvalds 		shmem_dir_free(page);
4601da177e4SLinus Torvalds 	}
4611da177e4SLinus Torvalds 	if (info->next_index <= index && !IS_ERR(entry))
4621da177e4SLinus Torvalds 		info->next_index = index + 1;
4631da177e4SLinus Torvalds 	return entry;
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
46646711810SRandy Dunlap /**
4671da177e4SLinus Torvalds  * shmem_free_swp - free some swap entries in a directory
4681da177e4SLinus Torvalds  * @dir:        pointer to the directory
4691da177e4SLinus Torvalds  * @edir:       pointer after last entry of the directory
4701ae70006SHugh Dickins  * @punch_lock: pointer to spinlock when needed for the holepunch case
4711da177e4SLinus Torvalds  */
4721ae70006SHugh Dickins static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
4731ae70006SHugh Dickins 						spinlock_t *punch_lock)
4741da177e4SLinus Torvalds {
4751ae70006SHugh Dickins 	spinlock_t *punch_unlock = NULL;
4761da177e4SLinus Torvalds 	swp_entry_t *ptr;
4771da177e4SLinus Torvalds 	int freed = 0;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	for (ptr = dir; ptr < edir; ptr++) {
4801da177e4SLinus Torvalds 		if (ptr->val) {
4811ae70006SHugh Dickins 			if (unlikely(punch_lock)) {
4821ae70006SHugh Dickins 				punch_unlock = punch_lock;
4831ae70006SHugh Dickins 				punch_lock = NULL;
4841ae70006SHugh Dickins 				spin_lock(punch_unlock);
4851ae70006SHugh Dickins 				if (!ptr->val)
4861ae70006SHugh Dickins 					continue;
4871ae70006SHugh Dickins 			}
4881da177e4SLinus Torvalds 			free_swap_and_cache(*ptr);
4891da177e4SLinus Torvalds 			*ptr = (swp_entry_t){0};
4901da177e4SLinus Torvalds 			freed++;
4911da177e4SLinus Torvalds 		}
4921da177e4SLinus Torvalds 	}
4931ae70006SHugh Dickins 	if (punch_unlock)
4941ae70006SHugh Dickins 		spin_unlock(punch_unlock);
4951da177e4SLinus Torvalds 	return freed;
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981ae70006SHugh Dickins static int shmem_map_and_free_swp(struct page *subdir, int offset,
4991ae70006SHugh Dickins 		int limit, struct page ***dir, spinlock_t *punch_lock)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	swp_entry_t *ptr;
5021da177e4SLinus Torvalds 	int freed = 0;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	ptr = shmem_swp_map(subdir);
5051da177e4SLinus Torvalds 	for (; offset < limit; offset += LATENCY_LIMIT) {
5061da177e4SLinus Torvalds 		int size = limit - offset;
5071da177e4SLinus Torvalds 		if (size > LATENCY_LIMIT)
5081da177e4SLinus Torvalds 			size = LATENCY_LIMIT;
5091ae70006SHugh Dickins 		freed += shmem_free_swp(ptr+offset, ptr+offset+size,
5101ae70006SHugh Dickins 							punch_lock);
5111da177e4SLinus Torvalds 		if (need_resched()) {
5121da177e4SLinus Torvalds 			shmem_swp_unmap(ptr);
5131da177e4SLinus Torvalds 			if (*dir) {
5141da177e4SLinus Torvalds 				shmem_dir_unmap(*dir);
5151da177e4SLinus Torvalds 				*dir = NULL;
5161da177e4SLinus Torvalds 			}
5171da177e4SLinus Torvalds 			cond_resched();
5181da177e4SLinus Torvalds 			ptr = shmem_swp_map(subdir);
5191da177e4SLinus Torvalds 		}
5201da177e4SLinus Torvalds 	}
5211da177e4SLinus Torvalds 	shmem_swp_unmap(ptr);
5221da177e4SLinus Torvalds 	return freed;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds static void shmem_free_pages(struct list_head *next)
5261da177e4SLinus Torvalds {
5271da177e4SLinus Torvalds 	struct page *page;
5281da177e4SLinus Torvalds 	int freed = 0;
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	do {
5311da177e4SLinus Torvalds 		page = container_of(next, struct page, lru);
5321da177e4SLinus Torvalds 		next = next->next;
5331da177e4SLinus Torvalds 		shmem_dir_free(page);
5341da177e4SLinus Torvalds 		freed++;
5351da177e4SLinus Torvalds 		if (freed >= LATENCY_LIMIT) {
5361da177e4SLinus Torvalds 			cond_resched();
5371da177e4SLinus Torvalds 			freed = 0;
5381da177e4SLinus Torvalds 		}
5391da177e4SLinus Torvalds 	} while (next);
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
542f6b3ec23SBadari Pulavarty static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
5431da177e4SLinus Torvalds {
5441da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
5451da177e4SLinus Torvalds 	unsigned long idx;
5461da177e4SLinus Torvalds 	unsigned long size;
5471da177e4SLinus Torvalds 	unsigned long limit;
5481da177e4SLinus Torvalds 	unsigned long stage;
5491da177e4SLinus Torvalds 	unsigned long diroff;
5501da177e4SLinus Torvalds 	struct page **dir;
5511da177e4SLinus Torvalds 	struct page *topdir;
5521da177e4SLinus Torvalds 	struct page *middir;
5531da177e4SLinus Torvalds 	struct page *subdir;
5541da177e4SLinus Torvalds 	swp_entry_t *ptr;
5551da177e4SLinus Torvalds 	LIST_HEAD(pages_to_free);
5561da177e4SLinus Torvalds 	long nr_pages_to_free = 0;
5571da177e4SLinus Torvalds 	long nr_swaps_freed = 0;
5581da177e4SLinus Torvalds 	int offset;
5591da177e4SLinus Torvalds 	int freed;
560a2646d1eSHugh Dickins 	int punch_hole;
5611ae70006SHugh Dickins 	spinlock_t *needs_lock;
5621ae70006SHugh Dickins 	spinlock_t *punch_lock;
563a2646d1eSHugh Dickins 	unsigned long upper_limit;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
566f6b3ec23SBadari Pulavarty 	idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
5671da177e4SLinus Torvalds 	if (idx >= info->next_index)
5681da177e4SLinus Torvalds 		return;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	spin_lock(&info->lock);
5711da177e4SLinus Torvalds 	info->flags |= SHMEM_TRUNCATE;
572f6b3ec23SBadari Pulavarty 	if (likely(end == (loff_t) -1)) {
5731da177e4SLinus Torvalds 		limit = info->next_index;
574a2646d1eSHugh Dickins 		upper_limit = SHMEM_MAX_INDEX;
5751da177e4SLinus Torvalds 		info->next_index = idx;
5761ae70006SHugh Dickins 		needs_lock = NULL;
577a2646d1eSHugh Dickins 		punch_hole = 0;
578f6b3ec23SBadari Pulavarty 	} else {
579a2646d1eSHugh Dickins 		if (end + 1 >= inode->i_size) {	/* we may free a little more */
580a2646d1eSHugh Dickins 			limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
581a2646d1eSHugh Dickins 							PAGE_CACHE_SHIFT;
582a2646d1eSHugh Dickins 			upper_limit = SHMEM_MAX_INDEX;
583a2646d1eSHugh Dickins 		} else {
584a2646d1eSHugh Dickins 			limit = (end + 1) >> PAGE_CACHE_SHIFT;
585a2646d1eSHugh Dickins 			upper_limit = limit;
586a2646d1eSHugh Dickins 		}
5871ae70006SHugh Dickins 		needs_lock = &info->lock;
588f6b3ec23SBadari Pulavarty 		punch_hole = 1;
589f6b3ec23SBadari Pulavarty 	}
590f6b3ec23SBadari Pulavarty 
5911da177e4SLinus Torvalds 	topdir = info->i_indirect;
592f6b3ec23SBadari Pulavarty 	if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
5931da177e4SLinus Torvalds 		info->i_indirect = NULL;
5941da177e4SLinus Torvalds 		nr_pages_to_free++;
5951da177e4SLinus Torvalds 		list_add(&topdir->lru, &pages_to_free);
5961da177e4SLinus Torvalds 	}
5971da177e4SLinus Torvalds 	spin_unlock(&info->lock);
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	if (info->swapped && idx < SHMEM_NR_DIRECT) {
6001da177e4SLinus Torvalds 		ptr = info->i_direct;
6011da177e4SLinus Torvalds 		size = limit;
6021da177e4SLinus Torvalds 		if (size > SHMEM_NR_DIRECT)
6031da177e4SLinus Torvalds 			size = SHMEM_NR_DIRECT;
6041ae70006SHugh Dickins 		nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
6051da177e4SLinus Torvalds 	}
60692a3d03aSBadari Pulavarty 
60792a3d03aSBadari Pulavarty 	/*
60892a3d03aSBadari Pulavarty 	 * If there are no indirect blocks or we are punching a hole
60992a3d03aSBadari Pulavarty 	 * below indirect blocks, nothing to be done.
61092a3d03aSBadari Pulavarty 	 */
611a2646d1eSHugh Dickins 	if (!topdir || limit <= SHMEM_NR_DIRECT)
6121da177e4SLinus Torvalds 		goto done2;
6131da177e4SLinus Torvalds 
6141ae70006SHugh Dickins 	/*
6151ae70006SHugh Dickins 	 * The truncation case has already dropped info->lock, and we're safe
6161ae70006SHugh Dickins 	 * because i_size and next_index have already been lowered, preventing
6171ae70006SHugh Dickins 	 * access beyond.  But in the punch_hole case, we still need to take
6181ae70006SHugh Dickins 	 * the lock when updating the swap directory, because there might be
6191ae70006SHugh Dickins 	 * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
6201ae70006SHugh Dickins 	 * shmem_writepage.  However, whenever we find we can remove a whole
6211ae70006SHugh Dickins 	 * directory page (not at the misaligned start or end of the range),
6221ae70006SHugh Dickins 	 * we first NULLify its pointer in the level above, and then have no
6231ae70006SHugh Dickins 	 * need to take the lock when updating its contents: needs_lock and
6241ae70006SHugh Dickins 	 * punch_lock (either pointing to info->lock or NULL) manage this.
6251ae70006SHugh Dickins 	 */
6261ae70006SHugh Dickins 
627a2646d1eSHugh Dickins 	upper_limit -= SHMEM_NR_DIRECT;
6281da177e4SLinus Torvalds 	limit -= SHMEM_NR_DIRECT;
6291da177e4SLinus Torvalds 	idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
6301da177e4SLinus Torvalds 	offset = idx % ENTRIES_PER_PAGE;
6311da177e4SLinus Torvalds 	idx -= offset;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	dir = shmem_dir_map(topdir);
6341da177e4SLinus Torvalds 	stage = ENTRIES_PER_PAGEPAGE/2;
6351da177e4SLinus Torvalds 	if (idx < ENTRIES_PER_PAGEPAGE/2) {
6361da177e4SLinus Torvalds 		middir = topdir;
6371da177e4SLinus Torvalds 		diroff = idx/ENTRIES_PER_PAGE;
6381da177e4SLinus Torvalds 	} else {
6391da177e4SLinus Torvalds 		dir += ENTRIES_PER_PAGE/2;
6401da177e4SLinus Torvalds 		dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
6411da177e4SLinus Torvalds 		while (stage <= idx)
6421da177e4SLinus Torvalds 			stage += ENTRIES_PER_PAGEPAGE;
6431da177e4SLinus Torvalds 		middir = *dir;
6441da177e4SLinus Torvalds 		if (*dir) {
6451da177e4SLinus Torvalds 			diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
6461da177e4SLinus Torvalds 				ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
647a2646d1eSHugh Dickins 			if (!diroff && !offset && upper_limit >= stage) {
6481ae70006SHugh Dickins 				if (needs_lock) {
6491ae70006SHugh Dickins 					spin_lock(needs_lock);
6501ae70006SHugh Dickins 					*dir = NULL;
6511ae70006SHugh Dickins 					spin_unlock(needs_lock);
6521ae70006SHugh Dickins 					needs_lock = NULL;
6531ae70006SHugh Dickins 				} else
6541da177e4SLinus Torvalds 					*dir = NULL;
6551da177e4SLinus Torvalds 				nr_pages_to_free++;
6561da177e4SLinus Torvalds 				list_add(&middir->lru, &pages_to_free);
6571da177e4SLinus Torvalds 			}
6581da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6591da177e4SLinus Torvalds 			dir = shmem_dir_map(middir);
6601da177e4SLinus Torvalds 		} else {
6611da177e4SLinus Torvalds 			diroff = 0;
6621da177e4SLinus Torvalds 			offset = 0;
6631da177e4SLinus Torvalds 			idx = stage;
6641da177e4SLinus Torvalds 		}
6651da177e4SLinus Torvalds 	}
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
6681da177e4SLinus Torvalds 		if (unlikely(idx == stage)) {
6691da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6701da177e4SLinus Torvalds 			dir = shmem_dir_map(topdir) +
6711da177e4SLinus Torvalds 			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
6721da177e4SLinus Torvalds 			while (!*dir) {
6731da177e4SLinus Torvalds 				dir++;
6741da177e4SLinus Torvalds 				idx += ENTRIES_PER_PAGEPAGE;
6751da177e4SLinus Torvalds 				if (idx >= limit)
6761da177e4SLinus Torvalds 					goto done1;
6771da177e4SLinus Torvalds 			}
6781da177e4SLinus Torvalds 			stage = idx + ENTRIES_PER_PAGEPAGE;
6791da177e4SLinus Torvalds 			middir = *dir;
6801ae70006SHugh Dickins 			if (punch_hole)
6811ae70006SHugh Dickins 				needs_lock = &info->lock;
682a2646d1eSHugh Dickins 			if (upper_limit >= stage) {
6831ae70006SHugh Dickins 				if (needs_lock) {
6841ae70006SHugh Dickins 					spin_lock(needs_lock);
6851ae70006SHugh Dickins 					*dir = NULL;
6861ae70006SHugh Dickins 					spin_unlock(needs_lock);
6871ae70006SHugh Dickins 					needs_lock = NULL;
6881ae70006SHugh Dickins 				} else
6891da177e4SLinus Torvalds 					*dir = NULL;
6901da177e4SLinus Torvalds 				nr_pages_to_free++;
6911da177e4SLinus Torvalds 				list_add(&middir->lru, &pages_to_free);
692a2646d1eSHugh Dickins 			}
6931da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6941da177e4SLinus Torvalds 			cond_resched();
6951da177e4SLinus Torvalds 			dir = shmem_dir_map(middir);
6961da177e4SLinus Torvalds 			diroff = 0;
6971da177e4SLinus Torvalds 		}
6981ae70006SHugh Dickins 		punch_lock = needs_lock;
6991da177e4SLinus Torvalds 		subdir = dir[diroff];
7001ae70006SHugh Dickins 		if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
7011ae70006SHugh Dickins 			if (needs_lock) {
7021ae70006SHugh Dickins 				spin_lock(needs_lock);
7031ae70006SHugh Dickins 				dir[diroff] = NULL;
7041ae70006SHugh Dickins 				spin_unlock(needs_lock);
7051ae70006SHugh Dickins 				punch_lock = NULL;
7061ae70006SHugh Dickins 			} else
7071da177e4SLinus Torvalds 				dir[diroff] = NULL;
7081da177e4SLinus Torvalds 			nr_pages_to_free++;
7091da177e4SLinus Torvalds 			list_add(&subdir->lru, &pages_to_free);
7101da177e4SLinus Torvalds 		}
7111ae70006SHugh Dickins 		if (subdir && page_private(subdir) /* has swap entries */) {
7121ae70006SHugh Dickins 			size = limit - idx;
7131ae70006SHugh Dickins 			if (size > ENTRIES_PER_PAGE)
7141ae70006SHugh Dickins 				size = ENTRIES_PER_PAGE;
7151ae70006SHugh Dickins 			freed = shmem_map_and_free_swp(subdir,
7161ae70006SHugh Dickins 					offset, size, &dir, punch_lock);
7171ae70006SHugh Dickins 			if (!dir)
7181ae70006SHugh Dickins 				dir = shmem_dir_map(middir);
7191ae70006SHugh Dickins 			nr_swaps_freed += freed;
7201ae70006SHugh Dickins 			if (offset || punch_lock) {
7211ae70006SHugh Dickins 				spin_lock(&info->lock);
7221ae70006SHugh Dickins 				set_page_private(subdir,
7231ae70006SHugh Dickins 					page_private(subdir) - freed);
7241ae70006SHugh Dickins 				spin_unlock(&info->lock);
7251ae70006SHugh Dickins 			} else
7261ae70006SHugh Dickins 				BUG_ON(page_private(subdir) != freed);
7271ae70006SHugh Dickins 		}
7281ae70006SHugh Dickins 		offset = 0;
7291da177e4SLinus Torvalds 	}
7301da177e4SLinus Torvalds done1:
7311da177e4SLinus Torvalds 	shmem_dir_unmap(dir);
7321da177e4SLinus Torvalds done2:
7331da177e4SLinus Torvalds 	if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
7341da177e4SLinus Torvalds 		/*
7351da177e4SLinus Torvalds 		 * Call truncate_inode_pages again: racing shmem_unuse_inode
7363889e6e7Snpiggin@suse.de 		 * may have swizzled a page in from swap since
7373889e6e7Snpiggin@suse.de 		 * truncate_pagecache or generic_delete_inode did it, before we
7383889e6e7Snpiggin@suse.de 		 * lowered next_index.  Also, though shmem_getpage checks
7393889e6e7Snpiggin@suse.de 		 * i_size before adding to cache, no recheck after: so fix the
7403889e6e7Snpiggin@suse.de 		 * narrow window there too.
74116a10019SHugh Dickins 		 *
74216a10019SHugh Dickins 		 * Recalling truncate_inode_pages_range and unmap_mapping_range
74316a10019SHugh Dickins 		 * every time for punch_hole (which never got a chance to clear
74416a10019SHugh Dickins 		 * SHMEM_PAGEIN at the start of vmtruncate_range) is expensive,
74516a10019SHugh Dickins 		 * yet hardly ever necessary: try to optimize them out later.
7461da177e4SLinus Torvalds 		 */
747f6b3ec23SBadari Pulavarty 		truncate_inode_pages_range(inode->i_mapping, start, end);
74816a10019SHugh Dickins 		if (punch_hole)
74916a10019SHugh Dickins 			unmap_mapping_range(inode->i_mapping, start,
75016a10019SHugh Dickins 							end - start, 1);
7511da177e4SLinus Torvalds 	}
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	spin_lock(&info->lock);
7541da177e4SLinus Torvalds 	info->flags &= ~SHMEM_TRUNCATE;
7551da177e4SLinus Torvalds 	info->swapped -= nr_swaps_freed;
7561da177e4SLinus Torvalds 	if (nr_pages_to_free)
7571da177e4SLinus Torvalds 		shmem_free_blocks(inode, nr_pages_to_free);
7581da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
7591da177e4SLinus Torvalds 	spin_unlock(&info->lock);
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	/*
7621da177e4SLinus Torvalds 	 * Empty swap vector directory pages to be freed?
7631da177e4SLinus Torvalds 	 */
7641da177e4SLinus Torvalds 	if (!list_empty(&pages_to_free)) {
7651da177e4SLinus Torvalds 		pages_to_free.prev->next = NULL;
7661da177e4SLinus Torvalds 		shmem_free_pages(pages_to_free.next);
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
7711da177e4SLinus Torvalds {
7721da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
773af5a30d8SNick Piggin 	loff_t newsize = attr->ia_size;
7741da177e4SLinus Torvalds 	int error;
7751da177e4SLinus Torvalds 
776db78b877SChristoph Hellwig 	error = inode_change_ok(inode, attr);
777db78b877SChristoph Hellwig 	if (error)
778db78b877SChristoph Hellwig 		return error;
779db78b877SChristoph Hellwig 
780af5a30d8SNick Piggin 	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)
781af5a30d8SNick Piggin 					&& newsize != inode->i_size) {
7823889e6e7Snpiggin@suse.de 		struct page *page = NULL;
7833889e6e7Snpiggin@suse.de 
7843889e6e7Snpiggin@suse.de 		if (newsize < inode->i_size) {
7851da177e4SLinus Torvalds 			/*
7861da177e4SLinus Torvalds 			 * If truncating down to a partial page, then
7871da177e4SLinus Torvalds 			 * if that page is already allocated, hold it
7881da177e4SLinus Torvalds 			 * in memory until the truncation is over, so
789ae0e47f0SJustin P. Mattock 			 * truncate_partial_page cannot miss it were
7901da177e4SLinus Torvalds 			 * it assigned to swap.
7911da177e4SLinus Torvalds 			 */
7923889e6e7Snpiggin@suse.de 			if (newsize & (PAGE_CACHE_SIZE-1)) {
7931da177e4SLinus Torvalds 				(void) shmem_getpage(inode,
7943889e6e7Snpiggin@suse.de 					newsize >> PAGE_CACHE_SHIFT,
7951da177e4SLinus Torvalds 						&page, SGP_READ, NULL);
796d3602444SHugh Dickins 				if (page)
797d3602444SHugh Dickins 					unlock_page(page);
7981da177e4SLinus Torvalds 			}
7991da177e4SLinus Torvalds 			/*
8001da177e4SLinus Torvalds 			 * Reset SHMEM_PAGEIN flag so that shmem_truncate can
8011da177e4SLinus Torvalds 			 * detect if any pages might have been added to cache
8021da177e4SLinus Torvalds 			 * after truncate_inode_pages.  But we needn't bother
8031da177e4SLinus Torvalds 			 * if it's being fully truncated to zero-length: the
8041da177e4SLinus Torvalds 			 * nrpages check is efficient enough in that case.
8051da177e4SLinus Torvalds 			 */
8063889e6e7Snpiggin@suse.de 			if (newsize) {
8071da177e4SLinus Torvalds 				struct shmem_inode_info *info = SHMEM_I(inode);
8081da177e4SLinus Torvalds 				spin_lock(&info->lock);
8091da177e4SLinus Torvalds 				info->flags &= ~SHMEM_PAGEIN;
8101da177e4SLinus Torvalds 				spin_unlock(&info->lock);
8111da177e4SLinus Torvalds 			}
8121da177e4SLinus Torvalds 		}
8133889e6e7Snpiggin@suse.de 
8142c27c65eSChristoph Hellwig 		/* XXX(truncate): truncate_setsize should be called last */
8152c27c65eSChristoph Hellwig 		truncate_setsize(inode, newsize);
8163889e6e7Snpiggin@suse.de 		if (page)
8173889e6e7Snpiggin@suse.de 			page_cache_release(page);
8183889e6e7Snpiggin@suse.de 		shmem_truncate_range(inode, newsize, (loff_t)-1);
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds 
8216a1a90adSChristoph Hellwig 	setattr_copy(inode, attr);
82239f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
823db78b877SChristoph Hellwig 	if (attr->ia_valid & ATTR_MODE)
8241c7c474cSChristoph Hellwig 		error = generic_acl_chmod(inode);
82539f0247dSAndreas Gruenbacher #endif
8261da177e4SLinus Torvalds 	return error;
8271da177e4SLinus Torvalds }
8281da177e4SLinus Torvalds 
8291f895f75SAl Viro static void shmem_evict_inode(struct inode *inode)
8301da177e4SLinus Torvalds {
8311da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
832*b09e0fa4SEric Paris 	struct shmem_xattr *xattr, *nxattr;
8331da177e4SLinus Torvalds 
8343889e6e7Snpiggin@suse.de 	if (inode->i_mapping->a_ops == &shmem_aops) {
835fef26658SMark Fasheh 		truncate_inode_pages(inode->i_mapping, 0);
8361da177e4SLinus Torvalds 		shmem_unacct_size(info->flags, inode->i_size);
8371da177e4SLinus Torvalds 		inode->i_size = 0;
8383889e6e7Snpiggin@suse.de 		shmem_truncate_range(inode, 0, (loff_t)-1);
8391da177e4SLinus Torvalds 		if (!list_empty(&info->swaplist)) {
840cb5f7b9aSHugh Dickins 			mutex_lock(&shmem_swaplist_mutex);
8411da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
842cb5f7b9aSHugh Dickins 			mutex_unlock(&shmem_swaplist_mutex);
8431da177e4SLinus Torvalds 		}
8441da177e4SLinus Torvalds 	}
845*b09e0fa4SEric Paris 
846*b09e0fa4SEric Paris 	list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
847*b09e0fa4SEric Paris 		kfree(xattr->name);
848*b09e0fa4SEric Paris 		kfree(xattr);
849*b09e0fa4SEric Paris 	}
8501da177e4SLinus Torvalds 	BUG_ON(inode->i_blocks);
8515b04c689SPavel Emelyanov 	shmem_free_inode(inode->i_sb);
8521f895f75SAl Viro 	end_writeback(inode);
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
8561da177e4SLinus Torvalds {
8571da177e4SLinus Torvalds 	swp_entry_t *ptr;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	for (ptr = dir; ptr < edir; ptr++) {
8601da177e4SLinus Torvalds 		if (ptr->val == entry.val)
8611da177e4SLinus Torvalds 			return ptr - dir;
8621da177e4SLinus Torvalds 	}
8631da177e4SLinus Torvalds 	return -1;
8641da177e4SLinus Torvalds }
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
8671da177e4SLinus Torvalds {
868778dd893SHugh Dickins 	struct address_space *mapping;
8691da177e4SLinus Torvalds 	unsigned long idx;
8701da177e4SLinus Torvalds 	unsigned long size;
8711da177e4SLinus Torvalds 	unsigned long limit;
8721da177e4SLinus Torvalds 	unsigned long stage;
8731da177e4SLinus Torvalds 	struct page **dir;
8741da177e4SLinus Torvalds 	struct page *subdir;
8751da177e4SLinus Torvalds 	swp_entry_t *ptr;
8761da177e4SLinus Torvalds 	int offset;
877d9fe526aSHugh Dickins 	int error;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	idx = 0;
8801da177e4SLinus Torvalds 	ptr = info->i_direct;
8811da177e4SLinus Torvalds 	spin_lock(&info->lock);
8821b1b32f2SHugh Dickins 	if (!info->swapped) {
8831b1b32f2SHugh Dickins 		list_del_init(&info->swaplist);
8841b1b32f2SHugh Dickins 		goto lost2;
8851b1b32f2SHugh Dickins 	}
8861da177e4SLinus Torvalds 	limit = info->next_index;
8871da177e4SLinus Torvalds 	size = limit;
8881da177e4SLinus Torvalds 	if (size > SHMEM_NR_DIRECT)
8891da177e4SLinus Torvalds 		size = SHMEM_NR_DIRECT;
8901da177e4SLinus Torvalds 	offset = shmem_find_swp(entry, ptr, ptr+size);
891778dd893SHugh Dickins 	if (offset >= 0) {
892778dd893SHugh Dickins 		shmem_swp_balance_unmap();
8931da177e4SLinus Torvalds 		goto found;
894778dd893SHugh Dickins 	}
8951da177e4SLinus Torvalds 	if (!info->i_indirect)
8961da177e4SLinus Torvalds 		goto lost2;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	dir = shmem_dir_map(info->i_indirect);
8991da177e4SLinus Torvalds 	stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
9021da177e4SLinus Torvalds 		if (unlikely(idx == stage)) {
9031da177e4SLinus Torvalds 			shmem_dir_unmap(dir-1);
904cb5f7b9aSHugh Dickins 			if (cond_resched_lock(&info->lock)) {
905cb5f7b9aSHugh Dickins 				/* check it has not been truncated */
906cb5f7b9aSHugh Dickins 				if (limit > info->next_index) {
907cb5f7b9aSHugh Dickins 					limit = info->next_index;
908cb5f7b9aSHugh Dickins 					if (idx >= limit)
909cb5f7b9aSHugh Dickins 						goto lost2;
910cb5f7b9aSHugh Dickins 				}
911cb5f7b9aSHugh Dickins 			}
9121da177e4SLinus Torvalds 			dir = shmem_dir_map(info->i_indirect) +
9131da177e4SLinus Torvalds 			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
9141da177e4SLinus Torvalds 			while (!*dir) {
9151da177e4SLinus Torvalds 				dir++;
9161da177e4SLinus Torvalds 				idx += ENTRIES_PER_PAGEPAGE;
9171da177e4SLinus Torvalds 				if (idx >= limit)
9181da177e4SLinus Torvalds 					goto lost1;
9191da177e4SLinus Torvalds 			}
9201da177e4SLinus Torvalds 			stage = idx + ENTRIES_PER_PAGEPAGE;
9211da177e4SLinus Torvalds 			subdir = *dir;
9221da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
9231da177e4SLinus Torvalds 			dir = shmem_dir_map(subdir);
9241da177e4SLinus Torvalds 		}
9251da177e4SLinus Torvalds 		subdir = *dir;
9264c21e2f2SHugh Dickins 		if (subdir && page_private(subdir)) {
9271da177e4SLinus Torvalds 			ptr = shmem_swp_map(subdir);
9281da177e4SLinus Torvalds 			size = limit - idx;
9291da177e4SLinus Torvalds 			if (size > ENTRIES_PER_PAGE)
9301da177e4SLinus Torvalds 				size = ENTRIES_PER_PAGE;
9311da177e4SLinus Torvalds 			offset = shmem_find_swp(entry, ptr, ptr+size);
932e6c9366bSHugh Dickins 			shmem_swp_unmap(ptr);
9331da177e4SLinus Torvalds 			if (offset >= 0) {
9341da177e4SLinus Torvalds 				shmem_dir_unmap(dir);
935e6c9366bSHugh Dickins 				ptr = shmem_swp_map(subdir);
9361da177e4SLinus Torvalds 				goto found;
9371da177e4SLinus Torvalds 			}
9381da177e4SLinus Torvalds 		}
9391da177e4SLinus Torvalds 	}
9401da177e4SLinus Torvalds lost1:
9411da177e4SLinus Torvalds 	shmem_dir_unmap(dir-1);
9421da177e4SLinus Torvalds lost2:
9431da177e4SLinus Torvalds 	spin_unlock(&info->lock);
9441da177e4SLinus Torvalds 	return 0;
9451da177e4SLinus Torvalds found:
9461da177e4SLinus Torvalds 	idx += offset;
947778dd893SHugh Dickins 	ptr += offset;
9482e0e26c7SHugh Dickins 
9491b1b32f2SHugh Dickins 	/*
9501b1b32f2SHugh Dickins 	 * Move _head_ to start search for next from here.
9511f895f75SAl Viro 	 * But be careful: shmem_evict_inode checks list_empty without taking
9521b1b32f2SHugh Dickins 	 * mutex, and there's an instant in list_move_tail when info->swaplist
9531b1b32f2SHugh Dickins 	 * would appear empty, if it were the only one on shmem_swaplist.  We
9541b1b32f2SHugh Dickins 	 * could avoid doing it if inode NULL; or use this minor optimization.
9551b1b32f2SHugh Dickins 	 */
9561b1b32f2SHugh Dickins 	if (shmem_swaplist.next != &info->swaplist)
9572e0e26c7SHugh Dickins 		list_move_tail(&shmem_swaplist, &info->swaplist);
9582e0e26c7SHugh Dickins 
959d13d1443SKAMEZAWA Hiroyuki 	/*
960778dd893SHugh Dickins 	 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
961778dd893SHugh Dickins 	 * but also to hold up shmem_evict_inode(): so inode cannot be freed
962778dd893SHugh Dickins 	 * beneath us (pagelock doesn't help until the page is in pagecache).
963d13d1443SKAMEZAWA Hiroyuki 	 */
964778dd893SHugh Dickins 	mapping = info->vfs_inode.i_mapping;
965778dd893SHugh Dickins 	error = add_to_page_cache_locked(page, mapping, idx, GFP_NOWAIT);
966778dd893SHugh Dickins 	/* which does mem_cgroup_uncharge_cache_page on error */
96769029cd5SKAMEZAWA Hiroyuki 
968d9fe526aSHugh Dickins 	if (error == -EEXIST) {
969778dd893SHugh Dickins 		struct page *filepage = find_get_page(mapping, idx);
9702e0e26c7SHugh Dickins 		error = 1;
971d9fe526aSHugh Dickins 		if (filepage) {
972d9fe526aSHugh Dickins 			/*
973d9fe526aSHugh Dickins 			 * There might be a more uptodate page coming down
974d9fe526aSHugh Dickins 			 * from a stacked writepage: forget our swappage if so.
975d9fe526aSHugh Dickins 			 */
976d9fe526aSHugh Dickins 			if (PageUptodate(filepage))
977d9fe526aSHugh Dickins 				error = 0;
978d9fe526aSHugh Dickins 			page_cache_release(filepage);
979d9fe526aSHugh Dickins 		}
980d9fe526aSHugh Dickins 	}
981d9fe526aSHugh Dickins 	if (!error) {
98273b1262fSHugh Dickins 		delete_from_swap_cache(page);
98373b1262fSHugh Dickins 		set_page_dirty(page);
9841da177e4SLinus Torvalds 		info->flags |= SHMEM_PAGEIN;
9852e0e26c7SHugh Dickins 		shmem_swp_set(info, ptr, 0);
9862e0e26c7SHugh Dickins 		swap_free(entry);
9872e0e26c7SHugh Dickins 		error = 1;	/* not an error, but entry was found */
9881da177e4SLinus Torvalds 	}
9891da177e4SLinus Torvalds 	shmem_swp_unmap(ptr);
9901da177e4SLinus Torvalds 	spin_unlock(&info->lock);
9912e0e26c7SHugh Dickins 	return error;
9921da177e4SLinus Torvalds }
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds /*
9951da177e4SLinus Torvalds  * shmem_unuse() search for an eventually swapped out shmem page.
9961da177e4SLinus Torvalds  */
9971da177e4SLinus Torvalds int shmem_unuse(swp_entry_t entry, struct page *page)
9981da177e4SLinus Torvalds {
9991da177e4SLinus Torvalds 	struct list_head *p, *next;
10001da177e4SLinus Torvalds 	struct shmem_inode_info *info;
10011da177e4SLinus Torvalds 	int found = 0;
1002778dd893SHugh Dickins 	int error;
1003778dd893SHugh Dickins 
1004778dd893SHugh Dickins 	/*
1005778dd893SHugh Dickins 	 * Charge page using GFP_KERNEL while we can wait, before taking
1006778dd893SHugh Dickins 	 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1007778dd893SHugh Dickins 	 * Charged back to the user (not to caller) when swap account is used.
1008778dd893SHugh Dickins 	 * add_to_page_cache() will be called with GFP_NOWAIT.
1009778dd893SHugh Dickins 	 */
1010778dd893SHugh Dickins 	error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
1011778dd893SHugh Dickins 	if (error)
1012778dd893SHugh Dickins 		goto out;
1013778dd893SHugh Dickins 	/*
1014778dd893SHugh Dickins 	 * Try to preload while we can wait, to not make a habit of
1015778dd893SHugh Dickins 	 * draining atomic reserves; but don't latch on to this cpu,
1016778dd893SHugh Dickins 	 * it's okay if sometimes we get rescheduled after this.
1017778dd893SHugh Dickins 	 */
1018778dd893SHugh Dickins 	error = radix_tree_preload(GFP_KERNEL);
1019778dd893SHugh Dickins 	if (error)
1020778dd893SHugh Dickins 		goto uncharge;
1021778dd893SHugh Dickins 	radix_tree_preload_end();
10221da177e4SLinus Torvalds 
1023cb5f7b9aSHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
10241da177e4SLinus Torvalds 	list_for_each_safe(p, next, &shmem_swaplist) {
10251da177e4SLinus Torvalds 		info = list_entry(p, struct shmem_inode_info, swaplist);
10262e0e26c7SHugh Dickins 		found = shmem_unuse_inode(info, entry, page);
1027cb5f7b9aSHugh Dickins 		cond_resched();
10282e0e26c7SHugh Dickins 		if (found)
1029778dd893SHugh Dickins 			break;
10301da177e4SLinus Torvalds 	}
1031cb5f7b9aSHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
1032778dd893SHugh Dickins 
1033778dd893SHugh Dickins uncharge:
1034778dd893SHugh Dickins 	if (!found)
1035778dd893SHugh Dickins 		mem_cgroup_uncharge_cache_page(page);
1036778dd893SHugh Dickins 	if (found < 0)
1037778dd893SHugh Dickins 		error = found;
1038778dd893SHugh Dickins out:
1039aaa46865SHugh Dickins 	unlock_page(page);
1040aaa46865SHugh Dickins 	page_cache_release(page);
1041778dd893SHugh Dickins 	return error;
10421da177e4SLinus Torvalds }
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds /*
10451da177e4SLinus Torvalds  * Move the page from the page cache to the swap cache.
10461da177e4SLinus Torvalds  */
10471da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc)
10481da177e4SLinus Torvalds {
10491da177e4SLinus Torvalds 	struct shmem_inode_info *info;
10501da177e4SLinus Torvalds 	swp_entry_t *entry, swap;
10511da177e4SLinus Torvalds 	struct address_space *mapping;
10521da177e4SLinus Torvalds 	unsigned long index;
10531da177e4SLinus Torvalds 	struct inode *inode;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
10561da177e4SLinus Torvalds 	mapping = page->mapping;
10571da177e4SLinus Torvalds 	index = page->index;
10581da177e4SLinus Torvalds 	inode = mapping->host;
10591da177e4SLinus Torvalds 	info = SHMEM_I(inode);
10601da177e4SLinus Torvalds 	if (info->flags & VM_LOCKED)
10611da177e4SLinus Torvalds 		goto redirty;
1062d9fe526aSHugh Dickins 	if (!total_swap_pages)
10631da177e4SLinus Torvalds 		goto redirty;
10641da177e4SLinus Torvalds 
1065d9fe526aSHugh Dickins 	/*
1066d9fe526aSHugh Dickins 	 * shmem_backing_dev_info's capabilities prevent regular writeback or
1067d9fe526aSHugh Dickins 	 * sync from ever calling shmem_writepage; but a stacking filesystem
1068d9fe526aSHugh Dickins 	 * may use the ->writepage of its underlying filesystem, in which case
1069d9fe526aSHugh Dickins 	 * tmpfs should write out to swap only in response to memory pressure,
10705b0830cbSJens Axboe 	 * and not for the writeback threads or sync.  However, in those cases,
10715b0830cbSJens Axboe 	 * we do still want to check if there's a redundant swappage to be
10725b0830cbSJens Axboe 	 * discarded.
1073d9fe526aSHugh Dickins 	 */
1074d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
1075d9fe526aSHugh Dickins 		swap = get_swap_page();
1076d9fe526aSHugh Dickins 	else
1077d9fe526aSHugh Dickins 		swap.val = 0;
1078d9fe526aSHugh Dickins 
1079b1dea800SHugh Dickins 	/*
1080b1dea800SHugh Dickins 	 * Add inode to shmem_unuse()'s list of swapped-out inodes,
1081b1dea800SHugh Dickins 	 * if it's not already there.  Do it now because we cannot take
1082b1dea800SHugh Dickins 	 * mutex while holding spinlock, and must do so before the page
1083b1dea800SHugh Dickins 	 * is moved to swap cache, when its pagelock no longer protects
1084b1dea800SHugh Dickins 	 * the inode from eviction.  But don't unlock the mutex until
1085b1dea800SHugh Dickins 	 * we've taken the spinlock, because shmem_unuse_inode() will
1086b1dea800SHugh Dickins 	 * prune a !swapped inode from the swaplist under both locks.
1087b1dea800SHugh Dickins 	 */
108805bf86b4SHugh Dickins 	if (swap.val) {
1089b1dea800SHugh Dickins 		mutex_lock(&shmem_swaplist_mutex);
109005bf86b4SHugh Dickins 		if (list_empty(&info->swaplist))
109105bf86b4SHugh Dickins 			list_add_tail(&info->swaplist, &shmem_swaplist);
1092b1dea800SHugh Dickins 	}
1093b1dea800SHugh Dickins 
10941da177e4SLinus Torvalds 	spin_lock(&info->lock);
109505bf86b4SHugh Dickins 	if (swap.val)
1096b1dea800SHugh Dickins 		mutex_unlock(&shmem_swaplist_mutex);
1097b1dea800SHugh Dickins 
10981da177e4SLinus Torvalds 	if (index >= info->next_index) {
10991da177e4SLinus Torvalds 		BUG_ON(!(info->flags & SHMEM_TRUNCATE));
11001da177e4SLinus Torvalds 		goto unlock;
11011da177e4SLinus Torvalds 	}
11021da177e4SLinus Torvalds 	entry = shmem_swp_entry(info, index, NULL);
1103d9fe526aSHugh Dickins 	if (entry->val) {
1104d9fe526aSHugh Dickins 		/*
1105d9fe526aSHugh Dickins 		 * The more uptodate page coming down from a stacked
1106d9fe526aSHugh Dickins 		 * writepage should replace our old swappage.
1107d9fe526aSHugh Dickins 		 */
1108d9fe526aSHugh Dickins 		free_swap_and_cache(*entry);
1109d9fe526aSHugh Dickins 		shmem_swp_set(info, entry, 0);
1110d9fe526aSHugh Dickins 	}
1111d9fe526aSHugh Dickins 	shmem_recalc_inode(inode);
11121da177e4SLinus Torvalds 
1113d9fe526aSHugh Dickins 	if (swap.val && add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
11144c73b1bcSMinchan Kim 		delete_from_page_cache(page);
11151da177e4SLinus Torvalds 		shmem_swp_set(info, entry, swap.val);
11161da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
11171da177e4SLinus Torvalds 		spin_unlock(&info->lock);
1118aaa46865SHugh Dickins 		swap_shmem_alloc(swap);
1119d9fe526aSHugh Dickins 		BUG_ON(page_mapped(page));
11209fab5619SHugh Dickins 		swap_writepage(page, wbc);
11211da177e4SLinus Torvalds 		return 0;
11221da177e4SLinus Torvalds 	}
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 	shmem_swp_unmap(entry);
11251da177e4SLinus Torvalds unlock:
11261da177e4SLinus Torvalds 	spin_unlock(&info->lock);
11272ca4532aSDaisuke Nishimura 	/*
11282ca4532aSDaisuke Nishimura 	 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
11292ca4532aSDaisuke Nishimura 	 * clear SWAP_HAS_CACHE flag.
11302ca4532aSDaisuke Nishimura 	 */
1131cb4b86baSKAMEZAWA Hiroyuki 	swapcache_free(swap, NULL);
11321da177e4SLinus Torvalds redirty:
11331da177e4SLinus Torvalds 	set_page_dirty(page);
1134d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
1135d9fe526aSHugh Dickins 		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
1136d9fe526aSHugh Dickins 	unlock_page(page);
1137d9fe526aSHugh Dickins 	return 0;
11381da177e4SLinus Torvalds }
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1141680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
114271fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1143680d794bSakpm@linux-foundation.org {
1144680d794bSakpm@linux-foundation.org 	char buffer[64];
1145680d794bSakpm@linux-foundation.org 
114671fe804bSLee Schermerhorn 	if (!mpol || mpol->mode == MPOL_DEFAULT)
1147095f1fc4SLee Schermerhorn 		return;		/* show nothing */
1148095f1fc4SLee Schermerhorn 
114971fe804bSLee Schermerhorn 	mpol_to_str(buffer, sizeof(buffer), mpol, 1);
1150095f1fc4SLee Schermerhorn 
1151095f1fc4SLee Schermerhorn 	seq_printf(seq, ",mpol=%s", buffer);
1152680d794bSakpm@linux-foundation.org }
115371fe804bSLee Schermerhorn 
115471fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
115571fe804bSLee Schermerhorn {
115671fe804bSLee Schermerhorn 	struct mempolicy *mpol = NULL;
115771fe804bSLee Schermerhorn 	if (sbinfo->mpol) {
115871fe804bSLee Schermerhorn 		spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
115971fe804bSLee Schermerhorn 		mpol = sbinfo->mpol;
116071fe804bSLee Schermerhorn 		mpol_get(mpol);
116171fe804bSLee Schermerhorn 		spin_unlock(&sbinfo->stat_lock);
116271fe804bSLee Schermerhorn 	}
116371fe804bSLee Schermerhorn 	return mpol;
116471fe804bSLee Schermerhorn }
1165680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
1166680d794bSakpm@linux-foundation.org 
116702098feaSHugh Dickins static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
116802098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11691da177e4SLinus Torvalds {
117052cd3b07SLee Schermerhorn 	struct mempolicy mpol, *spol;
11711da177e4SLinus Torvalds 	struct vm_area_struct pvma;
1172c4cc6d07SHugh Dickins 	struct page *page;
11731da177e4SLinus Torvalds 
117452cd3b07SLee Schermerhorn 	spol = mpol_cond_copy(&mpol,
117552cd3b07SLee Schermerhorn 				mpol_shared_policy_lookup(&info->policy, idx));
117652cd3b07SLee Schermerhorn 
11771da177e4SLinus Torvalds 	/* Create a pseudo vma that just contains the policy */
1178c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
11791da177e4SLinus Torvalds 	pvma.vm_pgoff = idx;
1180c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
118152cd3b07SLee Schermerhorn 	pvma.vm_policy = spol;
118202098feaSHugh Dickins 	page = swapin_readahead(entry, gfp, &pvma, 0);
11831da177e4SLinus Torvalds 	return page;
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
118602098feaSHugh Dickins static struct page *shmem_alloc_page(gfp_t gfp,
118702098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11881da177e4SLinus Torvalds {
11891da177e4SLinus Torvalds 	struct vm_area_struct pvma;
11901da177e4SLinus Torvalds 
1191c4cc6d07SHugh Dickins 	/* Create a pseudo vma that just contains the policy */
1192c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
11931da177e4SLinus Torvalds 	pvma.vm_pgoff = idx;
1194c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
1195c4cc6d07SHugh Dickins 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
119652cd3b07SLee Schermerhorn 
119752cd3b07SLee Schermerhorn 	/*
119852cd3b07SLee Schermerhorn 	 * alloc_page_vma() will drop the shared policy reference
119952cd3b07SLee Schermerhorn 	 */
120052cd3b07SLee Schermerhorn 	return alloc_page_vma(gfp, &pvma, 0);
12011da177e4SLinus Torvalds }
1202680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */
1203680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
120471fe804bSLee Schermerhorn static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *p)
1205680d794bSakpm@linux-foundation.org {
1206680d794bSakpm@linux-foundation.org }
1207680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
1208680d794bSakpm@linux-foundation.org 
120902098feaSHugh Dickins static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
121002098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
12111da177e4SLinus Torvalds {
121202098feaSHugh Dickins 	return swapin_readahead(entry, gfp, NULL, 0);
12131da177e4SLinus Torvalds }
12141da177e4SLinus Torvalds 
121502098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp,
121602098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
12171da177e4SLinus Torvalds {
1218e84e2e13SHugh Dickins 	return alloc_page(gfp);
12191da177e4SLinus Torvalds }
1220680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */
12211da177e4SLinus Torvalds 
122271fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
122371fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
122471fe804bSLee Schermerhorn {
122571fe804bSLee Schermerhorn 	return NULL;
122671fe804bSLee Schermerhorn }
122771fe804bSLee Schermerhorn #endif
122871fe804bSLee Schermerhorn 
12291da177e4SLinus Torvalds /*
12301da177e4SLinus Torvalds  * shmem_getpage - either get the page from swap or allocate a new one
12311da177e4SLinus Torvalds  *
12321da177e4SLinus Torvalds  * If we allocate a new one we do not mark it dirty. That's up to the
12331da177e4SLinus Torvalds  * vm. If we swap it in we mark it dirty since we also free the swap
12341da177e4SLinus Torvalds  * entry since a page cannot live in both the swap and page cache
12351da177e4SLinus Torvalds  */
12361da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx,
12371da177e4SLinus Torvalds 			struct page **pagep, enum sgp_type sgp, int *type)
12381da177e4SLinus Torvalds {
12391da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
12401da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
12411da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo;
12421da177e4SLinus Torvalds 	struct page *filepage = *pagep;
12431da177e4SLinus Torvalds 	struct page *swappage;
1244ff36b801SShaohua Li 	struct page *prealloc_page = NULL;
12451da177e4SLinus Torvalds 	swp_entry_t *entry;
12461da177e4SLinus Torvalds 	swp_entry_t swap;
124702098feaSHugh Dickins 	gfp_t gfp;
12481da177e4SLinus Torvalds 	int error;
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	if (idx >= SHMEM_MAX_INDEX)
12511da177e4SLinus Torvalds 		return -EFBIG;
125254cb8821SNick Piggin 
125354cb8821SNick Piggin 	if (type)
125483c54070SNick Piggin 		*type = 0;
125554cb8821SNick Piggin 
12561da177e4SLinus Torvalds 	/*
12571da177e4SLinus Torvalds 	 * Normally, filepage is NULL on entry, and either found
12581da177e4SLinus Torvalds 	 * uptodate immediately, or allocated and zeroed, or read
12591da177e4SLinus Torvalds 	 * in under swappage, which is then assigned to filepage.
12605402b976SHugh Dickins 	 * But shmem_readpage (required for splice) passes in a locked
1261ae976416SHugh Dickins 	 * filepage, which may be found not uptodate by other callers
1262ae976416SHugh Dickins 	 * too, and may need to be copied from the swappage read in.
12631da177e4SLinus Torvalds 	 */
12641da177e4SLinus Torvalds repeat:
12651da177e4SLinus Torvalds 	if (!filepage)
12661da177e4SLinus Torvalds 		filepage = find_lock_page(mapping, idx);
12671da177e4SLinus Torvalds 	if (filepage && PageUptodate(filepage))
12681da177e4SLinus Torvalds 		goto done;
126902098feaSHugh Dickins 	gfp = mapping_gfp_mask(mapping);
1270b409f9fcSHugh Dickins 	if (!filepage) {
1271b409f9fcSHugh Dickins 		/*
1272b409f9fcSHugh Dickins 		 * Try to preload while we can wait, to not make a habit of
1273b409f9fcSHugh Dickins 		 * draining atomic reserves; but don't latch on to this cpu.
1274b409f9fcSHugh Dickins 		 */
1275b409f9fcSHugh Dickins 		error = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
1276b409f9fcSHugh Dickins 		if (error)
1277b409f9fcSHugh Dickins 			goto failed;
1278b409f9fcSHugh Dickins 		radix_tree_preload_end();
1279ff36b801SShaohua Li 		if (sgp != SGP_READ && !prealloc_page) {
1280ff36b801SShaohua Li 			/* We don't care if this fails */
1281ff36b801SShaohua Li 			prealloc_page = shmem_alloc_page(gfp, info, idx);
1282ff36b801SShaohua Li 			if (prealloc_page) {
1283ff36b801SShaohua Li 				if (mem_cgroup_cache_charge(prealloc_page,
1284ff36b801SShaohua Li 						current->mm, GFP_KERNEL)) {
1285ff36b801SShaohua Li 					page_cache_release(prealloc_page);
1286ff36b801SShaohua Li 					prealloc_page = NULL;
1287b409f9fcSHugh Dickins 				}
1288ff36b801SShaohua Li 			}
1289ff36b801SShaohua Li 		}
1290ff36b801SShaohua Li 	}
1291ff36b801SShaohua Li 	error = 0;
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	spin_lock(&info->lock);
12941da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
12951da177e4SLinus Torvalds 	entry = shmem_swp_alloc(info, idx, sgp);
12961da177e4SLinus Torvalds 	if (IS_ERR(entry)) {
12971da177e4SLinus Torvalds 		spin_unlock(&info->lock);
12981da177e4SLinus Torvalds 		error = PTR_ERR(entry);
12991da177e4SLinus Torvalds 		goto failed;
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds 	swap = *entry;
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 	if (swap.val) {
13041da177e4SLinus Torvalds 		/* Look it up and read it in.. */
13051da177e4SLinus Torvalds 		swappage = lookup_swap_cache(swap);
13061da177e4SLinus Torvalds 		if (!swappage) {
13071da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13081da177e4SLinus Torvalds 			/* here we actually do the io */
130983c54070SNick Piggin 			if (type && !(*type & VM_FAULT_MAJOR)) {
1310f8891e5eSChristoph Lameter 				__count_vm_event(PGMAJFAULT);
131183c54070SNick Piggin 				*type |= VM_FAULT_MAJOR;
13121da177e4SLinus Torvalds 			}
1313f8891e5eSChristoph Lameter 			spin_unlock(&info->lock);
131402098feaSHugh Dickins 			swappage = shmem_swapin(swap, gfp, info, idx);
13151da177e4SLinus Torvalds 			if (!swappage) {
13161da177e4SLinus Torvalds 				spin_lock(&info->lock);
13171da177e4SLinus Torvalds 				entry = shmem_swp_alloc(info, idx, sgp);
13181da177e4SLinus Torvalds 				if (IS_ERR(entry))
13191da177e4SLinus Torvalds 					error = PTR_ERR(entry);
13201da177e4SLinus Torvalds 				else {
13211da177e4SLinus Torvalds 					if (entry->val == swap.val)
13221da177e4SLinus Torvalds 						error = -ENOMEM;
13231da177e4SLinus Torvalds 					shmem_swp_unmap(entry);
13241da177e4SLinus Torvalds 				}
13251da177e4SLinus Torvalds 				spin_unlock(&info->lock);
13261da177e4SLinus Torvalds 				if (error)
13271da177e4SLinus Torvalds 					goto failed;
13281da177e4SLinus Torvalds 				goto repeat;
13291da177e4SLinus Torvalds 			}
13301da177e4SLinus Torvalds 			wait_on_page_locked(swappage);
13311da177e4SLinus Torvalds 			page_cache_release(swappage);
13321da177e4SLinus Torvalds 			goto repeat;
13331da177e4SLinus Torvalds 		}
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 		/* We have to do this with page locked to prevent races */
1336529ae9aaSNick Piggin 		if (!trylock_page(swappage)) {
13371da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13381da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13391da177e4SLinus Torvalds 			wait_on_page_locked(swappage);
13401da177e4SLinus Torvalds 			page_cache_release(swappage);
13411da177e4SLinus Torvalds 			goto repeat;
13421da177e4SLinus Torvalds 		}
13431da177e4SLinus Torvalds 		if (PageWriteback(swappage)) {
13441da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13451da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13461da177e4SLinus Torvalds 			wait_on_page_writeback(swappage);
13471da177e4SLinus Torvalds 			unlock_page(swappage);
13481da177e4SLinus Torvalds 			page_cache_release(swappage);
13491da177e4SLinus Torvalds 			goto repeat;
13501da177e4SLinus Torvalds 		}
13511da177e4SLinus Torvalds 		if (!PageUptodate(swappage)) {
13521da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13531da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13541da177e4SLinus Torvalds 			unlock_page(swappage);
13551da177e4SLinus Torvalds 			page_cache_release(swappage);
13561da177e4SLinus Torvalds 			error = -EIO;
13571da177e4SLinus Torvalds 			goto failed;
13581da177e4SLinus Torvalds 		}
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 		if (filepage) {
13611da177e4SLinus Torvalds 			shmem_swp_set(info, entry, 0);
13621da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13631da177e4SLinus Torvalds 			delete_from_swap_cache(swappage);
13641da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13651da177e4SLinus Torvalds 			copy_highpage(filepage, swappage);
13661da177e4SLinus Torvalds 			unlock_page(swappage);
13671da177e4SLinus Torvalds 			page_cache_release(swappage);
13681da177e4SLinus Torvalds 			flush_dcache_page(filepage);
13691da177e4SLinus Torvalds 			SetPageUptodate(filepage);
13701da177e4SLinus Torvalds 			set_page_dirty(filepage);
13711da177e4SLinus Torvalds 			swap_free(swap);
1372e286781dSNick Piggin 		} else if (!(error = add_to_page_cache_locked(swappage, mapping,
1373e286781dSNick Piggin 					idx, GFP_NOWAIT))) {
13741da177e4SLinus Torvalds 			info->flags |= SHMEM_PAGEIN;
13751da177e4SLinus Torvalds 			shmem_swp_set(info, entry, 0);
13761da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
137773b1262fSHugh Dickins 			delete_from_swap_cache(swappage);
13781da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13791da177e4SLinus Torvalds 			filepage = swappage;
138073b1262fSHugh Dickins 			set_page_dirty(filepage);
13811da177e4SLinus Torvalds 			swap_free(swap);
13821da177e4SLinus Torvalds 		} else {
13831da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13841da177e4SLinus Torvalds 			spin_unlock(&info->lock);
138582369553SHugh Dickins 			if (error == -ENOMEM) {
1386ae3abae6SDaisuke Nishimura 				/*
1387ae3abae6SDaisuke Nishimura 				 * reclaim from proper memory cgroup and
1388ae3abae6SDaisuke Nishimura 				 * call memcg's OOM if needed.
1389ae3abae6SDaisuke Nishimura 				 */
1390ae3abae6SDaisuke Nishimura 				error = mem_cgroup_shmem_charge_fallback(
1391ae3abae6SDaisuke Nishimura 								swappage,
1392b5a84319SKAMEZAWA Hiroyuki 								current->mm,
1393c9b0ed51SKAMEZAWA Hiroyuki 								gfp);
1394b5a84319SKAMEZAWA Hiroyuki 				if (error) {
1395b5a84319SKAMEZAWA Hiroyuki 					unlock_page(swappage);
1396b5a84319SKAMEZAWA Hiroyuki 					page_cache_release(swappage);
139782369553SHugh Dickins 					goto failed;
139882369553SHugh Dickins 				}
1399b5a84319SKAMEZAWA Hiroyuki 			}
1400b5a84319SKAMEZAWA Hiroyuki 			unlock_page(swappage);
1401b5a84319SKAMEZAWA Hiroyuki 			page_cache_release(swappage);
14021da177e4SLinus Torvalds 			goto repeat;
14031da177e4SLinus Torvalds 		}
14041da177e4SLinus Torvalds 	} else if (sgp == SGP_READ && !filepage) {
14051da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
14061da177e4SLinus Torvalds 		filepage = find_get_page(mapping, idx);
14071da177e4SLinus Torvalds 		if (filepage &&
1408529ae9aaSNick Piggin 		    (!PageUptodate(filepage) || !trylock_page(filepage))) {
14091da177e4SLinus Torvalds 			spin_unlock(&info->lock);
14101da177e4SLinus Torvalds 			wait_on_page_locked(filepage);
14111da177e4SLinus Torvalds 			page_cache_release(filepage);
14121da177e4SLinus Torvalds 			filepage = NULL;
14131da177e4SLinus Torvalds 			goto repeat;
14141da177e4SLinus Torvalds 		}
14151da177e4SLinus Torvalds 		spin_unlock(&info->lock);
14161da177e4SLinus Torvalds 	} else {
14171da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
14181da177e4SLinus Torvalds 		sbinfo = SHMEM_SB(inode->i_sb);
14190edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
1420fc5da22aSHugh Dickins 			if (percpu_counter_compare(&sbinfo->used_blocks,
1421fc5da22aSHugh Dickins 						sbinfo->max_blocks) >= 0 ||
142259a16eadSHugh Dickins 			    shmem_acct_block(info->flags))
142359a16eadSHugh Dickins 				goto nospace;
14247e496299STim Chen 			percpu_counter_inc(&sbinfo->used_blocks);
14257e496299STim Chen 			spin_lock(&inode->i_lock);
14261da177e4SLinus Torvalds 			inode->i_blocks += BLOCKS_PER_PAGE;
14277e496299STim Chen 			spin_unlock(&inode->i_lock);
142859a16eadSHugh Dickins 		} else if (shmem_acct_block(info->flags))
142959a16eadSHugh Dickins 			goto nospace;
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds 		if (!filepage) {
143269029cd5SKAMEZAWA Hiroyuki 			int ret;
143369029cd5SKAMEZAWA Hiroyuki 
1434ff36b801SShaohua Li 			if (!prealloc_page) {
14351da177e4SLinus Torvalds 				spin_unlock(&info->lock);
143602098feaSHugh Dickins 				filepage = shmem_alloc_page(gfp, info, idx);
14371da177e4SLinus Torvalds 				if (!filepage) {
14381da177e4SLinus Torvalds 					shmem_unacct_blocks(info->flags, 1);
14391da177e4SLinus Torvalds 					shmem_free_blocks(inode, 1);
14401da177e4SLinus Torvalds 					error = -ENOMEM;
14411da177e4SLinus Torvalds 					goto failed;
14421da177e4SLinus Torvalds 				}
1443b2e18538SRik van Riel 				SetPageSwapBacked(filepage);
14441da177e4SLinus Torvalds 
1445ff36b801SShaohua Li 				/*
1446ff36b801SShaohua Li 				 * Precharge page while we can wait, compensate
1447ff36b801SShaohua Li 				 * after
1448ff36b801SShaohua Li 				 */
1449ff36b801SShaohua Li 				error = mem_cgroup_cache_charge(filepage,
1450ff36b801SShaohua Li 					current->mm, GFP_KERNEL);
145182369553SHugh Dickins 				if (error) {
145282369553SHugh Dickins 					page_cache_release(filepage);
145382369553SHugh Dickins 					shmem_unacct_blocks(info->flags, 1);
145482369553SHugh Dickins 					shmem_free_blocks(inode, 1);
145582369553SHugh Dickins 					filepage = NULL;
145682369553SHugh Dickins 					goto failed;
145782369553SHugh Dickins 				}
145882369553SHugh Dickins 
14591da177e4SLinus Torvalds 				spin_lock(&info->lock);
1460ff36b801SShaohua Li 			} else {
1461ff36b801SShaohua Li 				filepage = prealloc_page;
1462ff36b801SShaohua Li 				prealloc_page = NULL;
1463ff36b801SShaohua Li 				SetPageSwapBacked(filepage);
1464ff36b801SShaohua Li 			}
1465ff36b801SShaohua Li 
14661da177e4SLinus Torvalds 			entry = shmem_swp_alloc(info, idx, sgp);
14671da177e4SLinus Torvalds 			if (IS_ERR(entry))
14681da177e4SLinus Torvalds 				error = PTR_ERR(entry);
14691da177e4SLinus Torvalds 			else {
14701da177e4SLinus Torvalds 				swap = *entry;
14711da177e4SLinus Torvalds 				shmem_swp_unmap(entry);
14721da177e4SLinus Torvalds 			}
147369029cd5SKAMEZAWA Hiroyuki 			ret = error || swap.val;
147469029cd5SKAMEZAWA Hiroyuki 			if (ret)
147569029cd5SKAMEZAWA Hiroyuki 				mem_cgroup_uncharge_cache_page(filepage);
147669029cd5SKAMEZAWA Hiroyuki 			else
147769029cd5SKAMEZAWA Hiroyuki 				ret = add_to_page_cache_lru(filepage, mapping,
147869029cd5SKAMEZAWA Hiroyuki 						idx, GFP_NOWAIT);
147969029cd5SKAMEZAWA Hiroyuki 			/*
148069029cd5SKAMEZAWA Hiroyuki 			 * At add_to_page_cache_lru() failure, uncharge will
148169029cd5SKAMEZAWA Hiroyuki 			 * be done automatically.
148269029cd5SKAMEZAWA Hiroyuki 			 */
148369029cd5SKAMEZAWA Hiroyuki 			if (ret) {
14841da177e4SLinus Torvalds 				spin_unlock(&info->lock);
14851da177e4SLinus Torvalds 				page_cache_release(filepage);
14861da177e4SLinus Torvalds 				shmem_unacct_blocks(info->flags, 1);
14871da177e4SLinus Torvalds 				shmem_free_blocks(inode, 1);
14881da177e4SLinus Torvalds 				filepage = NULL;
14891da177e4SLinus Torvalds 				if (error)
14901da177e4SLinus Torvalds 					goto failed;
14911da177e4SLinus Torvalds 				goto repeat;
14921da177e4SLinus Torvalds 			}
14931da177e4SLinus Torvalds 			info->flags |= SHMEM_PAGEIN;
14941da177e4SLinus Torvalds 		}
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds 		info->alloced++;
14971da177e4SLinus Torvalds 		spin_unlock(&info->lock);
1498e84e2e13SHugh Dickins 		clear_highpage(filepage);
14991da177e4SLinus Torvalds 		flush_dcache_page(filepage);
15001da177e4SLinus Torvalds 		SetPageUptodate(filepage);
1501a0ee5ec5SHugh Dickins 		if (sgp == SGP_DIRTY)
1502a0ee5ec5SHugh Dickins 			set_page_dirty(filepage);
15031da177e4SLinus Torvalds 	}
15041da177e4SLinus Torvalds done:
15051da177e4SLinus Torvalds 	*pagep = filepage;
1506ff36b801SShaohua Li 	error = 0;
1507ff36b801SShaohua Li 	goto out;
15081da177e4SLinus Torvalds 
150959a16eadSHugh Dickins nospace:
151059a16eadSHugh Dickins 	/*
151159a16eadSHugh Dickins 	 * Perhaps the page was brought in from swap between find_lock_page
151259a16eadSHugh Dickins 	 * and taking info->lock?  We allow for that at add_to_page_cache_lru,
151359a16eadSHugh Dickins 	 * but must also avoid reporting a spurious ENOSPC while working on a
151459a16eadSHugh Dickins 	 * full tmpfs.  (When filepage has been passed in to shmem_getpage, it
151559a16eadSHugh Dickins 	 * is already in page cache, which prevents this race from occurring.)
151659a16eadSHugh Dickins 	 */
151759a16eadSHugh Dickins 	if (!filepage) {
151859a16eadSHugh Dickins 		struct page *page = find_get_page(mapping, idx);
151959a16eadSHugh Dickins 		if (page) {
152059a16eadSHugh Dickins 			spin_unlock(&info->lock);
152159a16eadSHugh Dickins 			page_cache_release(page);
152259a16eadSHugh Dickins 			goto repeat;
152359a16eadSHugh Dickins 		}
152459a16eadSHugh Dickins 	}
152559a16eadSHugh Dickins 	spin_unlock(&info->lock);
152659a16eadSHugh Dickins 	error = -ENOSPC;
15271da177e4SLinus Torvalds failed:
15281da177e4SLinus Torvalds 	if (*pagep != filepage) {
15291da177e4SLinus Torvalds 		unlock_page(filepage);
15301da177e4SLinus Torvalds 		page_cache_release(filepage);
15311da177e4SLinus Torvalds 	}
1532ff36b801SShaohua Li out:
1533ff36b801SShaohua Li 	if (prealloc_page) {
1534ff36b801SShaohua Li 		mem_cgroup_uncharge_cache_page(prealloc_page);
1535ff36b801SShaohua Li 		page_cache_release(prealloc_page);
1536ff36b801SShaohua Li 	}
15371da177e4SLinus Torvalds 	return error;
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds 
1540d0217ac0SNick Piggin static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
15411da177e4SLinus Torvalds {
1542d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
15431da177e4SLinus Torvalds 	int error;
1544d0217ac0SNick Piggin 	int ret;
15451da177e4SLinus Torvalds 
1546d0217ac0SNick Piggin 	if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1547d0217ac0SNick Piggin 		return VM_FAULT_SIGBUS;
1548d00806b1SNick Piggin 
154927d54b39SHugh Dickins 	error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1550d0217ac0SNick Piggin 	if (error)
1551d0217ac0SNick Piggin 		return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
15521da177e4SLinus Torvalds 
155383c54070SNick Piggin 	return ret | VM_FAULT_LOCKED;
15541da177e4SLinus Torvalds }
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1557d8dc74f2SAdrian Bunk static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
15581da177e4SLinus Torvalds {
1559d3ac7f89SJosef "Jeff" Sipek 	struct inode *i = vma->vm_file->f_path.dentry->d_inode;
15601da177e4SLinus Torvalds 	return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
15611da177e4SLinus Torvalds }
15621da177e4SLinus Torvalds 
1563d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1564d8dc74f2SAdrian Bunk 					  unsigned long addr)
15651da177e4SLinus Torvalds {
1566d3ac7f89SJosef "Jeff" Sipek 	struct inode *i = vma->vm_file->f_path.dentry->d_inode;
15671da177e4SLinus Torvalds 	unsigned long idx;
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
15701da177e4SLinus Torvalds 	return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
15711da177e4SLinus Torvalds }
15721da177e4SLinus Torvalds #endif
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user)
15751da177e4SLinus Torvalds {
1576d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
15771da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
15781da177e4SLinus Torvalds 	int retval = -ENOMEM;
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	spin_lock(&info->lock);
15811da177e4SLinus Torvalds 	if (lock && !(info->flags & VM_LOCKED)) {
15821da177e4SLinus Torvalds 		if (!user_shm_lock(inode->i_size, user))
15831da177e4SLinus Torvalds 			goto out_nomem;
15841da177e4SLinus Torvalds 		info->flags |= VM_LOCKED;
158589e004eaSLee Schermerhorn 		mapping_set_unevictable(file->f_mapping);
15861da177e4SLinus Torvalds 	}
15871da177e4SLinus Torvalds 	if (!lock && (info->flags & VM_LOCKED) && user) {
15881da177e4SLinus Torvalds 		user_shm_unlock(inode->i_size, user);
15891da177e4SLinus Torvalds 		info->flags &= ~VM_LOCKED;
159089e004eaSLee Schermerhorn 		mapping_clear_unevictable(file->f_mapping);
159189e004eaSLee Schermerhorn 		scan_mapping_unevictable_pages(file->f_mapping);
15921da177e4SLinus Torvalds 	}
15931da177e4SLinus Torvalds 	retval = 0;
159489e004eaSLee Schermerhorn 
15951da177e4SLinus Torvalds out_nomem:
15961da177e4SLinus Torvalds 	spin_unlock(&info->lock);
15971da177e4SLinus Torvalds 	return retval;
15981da177e4SLinus Torvalds }
15991da177e4SLinus Torvalds 
16009b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
16011da177e4SLinus Torvalds {
16021da177e4SLinus Torvalds 	file_accessed(file);
16031da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
1604d0217ac0SNick Piggin 	vma->vm_flags |= VM_CAN_NONLINEAR;
16051da177e4SLinus Torvalds 	return 0;
16061da177e4SLinus Torvalds }
16071da177e4SLinus Torvalds 
1608454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1609454abafeSDmitry Monakhov 				     int mode, dev_t dev, unsigned long flags)
16101da177e4SLinus Torvalds {
16111da177e4SLinus Torvalds 	struct inode *inode;
16121da177e4SLinus Torvalds 	struct shmem_inode_info *info;
16131da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
16141da177e4SLinus Torvalds 
16155b04c689SPavel Emelyanov 	if (shmem_reserve_inode(sb))
16161da177e4SLinus Torvalds 		return NULL;
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 	inode = new_inode(sb);
16191da177e4SLinus Torvalds 	if (inode) {
162085fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
1621454abafeSDmitry Monakhov 		inode_init_owner(inode, dir, mode);
16221da177e4SLinus Torvalds 		inode->i_blocks = 0;
16231da177e4SLinus Torvalds 		inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
16241da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
162591828a40SDavid M. Grimes 		inode->i_generation = get_seconds();
16261da177e4SLinus Torvalds 		info = SHMEM_I(inode);
16271da177e4SLinus Torvalds 		memset(info, 0, (char *)inode - (char *)info);
16281da177e4SLinus Torvalds 		spin_lock_init(&info->lock);
16290b0a0806SHugh Dickins 		info->flags = flags & VM_NORESERVE;
16301da177e4SLinus Torvalds 		INIT_LIST_HEAD(&info->swaplist);
1631*b09e0fa4SEric Paris 		INIT_LIST_HEAD(&info->xattr_list);
163272c04902SAl Viro 		cache_no_acl(inode);
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
16351da177e4SLinus Torvalds 		default:
163639f0247dSAndreas Gruenbacher 			inode->i_op = &shmem_special_inode_operations;
16371da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
16381da177e4SLinus Torvalds 			break;
16391da177e4SLinus Torvalds 		case S_IFREG:
164014fcc23fSHugh Dickins 			inode->i_mapping->a_ops = &shmem_aops;
16411da177e4SLinus Torvalds 			inode->i_op = &shmem_inode_operations;
16421da177e4SLinus Torvalds 			inode->i_fop = &shmem_file_operations;
164371fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy,
164471fe804bSLee Schermerhorn 						 shmem_get_sbmpol(sbinfo));
16451da177e4SLinus Torvalds 			break;
16461da177e4SLinus Torvalds 		case S_IFDIR:
1647d8c76e6fSDave Hansen 			inc_nlink(inode);
16481da177e4SLinus Torvalds 			/* Some things misbehave if size == 0 on a directory */
16491da177e4SLinus Torvalds 			inode->i_size = 2 * BOGO_DIRENT_SIZE;
16501da177e4SLinus Torvalds 			inode->i_op = &shmem_dir_inode_operations;
16511da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
16521da177e4SLinus Torvalds 			break;
16531da177e4SLinus Torvalds 		case S_IFLNK:
16541da177e4SLinus Torvalds 			/*
16551da177e4SLinus Torvalds 			 * Must not load anything in the rbtree,
16561da177e4SLinus Torvalds 			 * mpol_free_shared_policy will not be called.
16571da177e4SLinus Torvalds 			 */
165871fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy, NULL);
16591da177e4SLinus Torvalds 			break;
16601da177e4SLinus Torvalds 		}
16615b04c689SPavel Emelyanov 	} else
16625b04c689SPavel Emelyanov 		shmem_free_inode(sb);
16631da177e4SLinus Torvalds 	return inode;
16641da177e4SLinus Torvalds }
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
166792e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations;
166892e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations;
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds /*
1671800d15a5SNick Piggin  * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
1672ae976416SHugh Dickins  * but providing them allows a tmpfs file to be used for splice, sendfile, and
1673ae976416SHugh Dickins  * below the loop driver, in the generic fashion that many filesystems support.
16741da177e4SLinus Torvalds  */
1675ae976416SHugh Dickins static int shmem_readpage(struct file *file, struct page *page)
1676ae976416SHugh Dickins {
1677ae976416SHugh Dickins 	struct inode *inode = page->mapping->host;
1678ae976416SHugh Dickins 	int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
1679ae976416SHugh Dickins 	unlock_page(page);
1680ae976416SHugh Dickins 	return error;
1681ae976416SHugh Dickins }
1682ae976416SHugh Dickins 
16831da177e4SLinus Torvalds static int
1684800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping,
1685800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
1686800d15a5SNick Piggin 			struct page **pagep, void **fsdata)
16871da177e4SLinus Torvalds {
1688800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1689800d15a5SNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1690800d15a5SNick Piggin 	*pagep = NULL;
1691800d15a5SNick Piggin 	return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1692800d15a5SNick Piggin }
1693800d15a5SNick Piggin 
1694800d15a5SNick Piggin static int
1695800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping,
1696800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
1697800d15a5SNick Piggin 			struct page *page, void *fsdata)
1698800d15a5SNick Piggin {
1699800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1700800d15a5SNick Piggin 
1701800d15a5SNick Piggin 	if (pos + copied > inode->i_size)
1702800d15a5SNick Piggin 		i_size_write(inode, pos + copied);
1703800d15a5SNick Piggin 
1704d3602444SHugh Dickins 	set_page_dirty(page);
17056746aff7SWu Fengguang 	unlock_page(page);
1706d3602444SHugh Dickins 	page_cache_release(page);
1707d3602444SHugh Dickins 
1708800d15a5SNick Piggin 	return copied;
17091da177e4SLinus Torvalds }
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
17121da177e4SLinus Torvalds {
1713d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
17141da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
17151da177e4SLinus Torvalds 	unsigned long index, offset;
1716a0ee5ec5SHugh Dickins 	enum sgp_type sgp = SGP_READ;
1717a0ee5ec5SHugh Dickins 
1718a0ee5ec5SHugh Dickins 	/*
1719a0ee5ec5SHugh Dickins 	 * Might this read be for a stacking filesystem?  Then when reading
1720a0ee5ec5SHugh Dickins 	 * holes of a sparse file, we actually need to allocate those pages,
1721a0ee5ec5SHugh Dickins 	 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1722a0ee5ec5SHugh Dickins 	 */
1723a0ee5ec5SHugh Dickins 	if (segment_eq(get_fs(), KERNEL_DS))
1724a0ee5ec5SHugh Dickins 		sgp = SGP_DIRTY;
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	index = *ppos >> PAGE_CACHE_SHIFT;
17271da177e4SLinus Torvalds 	offset = *ppos & ~PAGE_CACHE_MASK;
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds 	for (;;) {
17301da177e4SLinus Torvalds 		struct page *page = NULL;
17311da177e4SLinus Torvalds 		unsigned long end_index, nr, ret;
17321da177e4SLinus Torvalds 		loff_t i_size = i_size_read(inode);
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
17351da177e4SLinus Torvalds 		if (index > end_index)
17361da177e4SLinus Torvalds 			break;
17371da177e4SLinus Torvalds 		if (index == end_index) {
17381da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
17391da177e4SLinus Torvalds 			if (nr <= offset)
17401da177e4SLinus Torvalds 				break;
17411da177e4SLinus Torvalds 		}
17421da177e4SLinus Torvalds 
1743a0ee5ec5SHugh Dickins 		desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
17441da177e4SLinus Torvalds 		if (desc->error) {
17451da177e4SLinus Torvalds 			if (desc->error == -EINVAL)
17461da177e4SLinus Torvalds 				desc->error = 0;
17471da177e4SLinus Torvalds 			break;
17481da177e4SLinus Torvalds 		}
1749d3602444SHugh Dickins 		if (page)
1750d3602444SHugh Dickins 			unlock_page(page);
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 		/*
17531da177e4SLinus Torvalds 		 * We must evaluate after, since reads (unlike writes)
17541b1dcc1bSJes Sorensen 		 * are called without i_mutex protection against truncate
17551da177e4SLinus Torvalds 		 */
17561da177e4SLinus Torvalds 		nr = PAGE_CACHE_SIZE;
17571da177e4SLinus Torvalds 		i_size = i_size_read(inode);
17581da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
17591da177e4SLinus Torvalds 		if (index == end_index) {
17601da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
17611da177e4SLinus Torvalds 			if (nr <= offset) {
17621da177e4SLinus Torvalds 				if (page)
17631da177e4SLinus Torvalds 					page_cache_release(page);
17641da177e4SLinus Torvalds 				break;
17651da177e4SLinus Torvalds 			}
17661da177e4SLinus Torvalds 		}
17671da177e4SLinus Torvalds 		nr -= offset;
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 		if (page) {
17701da177e4SLinus Torvalds 			/*
17711da177e4SLinus Torvalds 			 * If users can be writing to this page using arbitrary
17721da177e4SLinus Torvalds 			 * virtual addresses, take care about potential aliasing
17731da177e4SLinus Torvalds 			 * before reading the page on the kernel side.
17741da177e4SLinus Torvalds 			 */
17751da177e4SLinus Torvalds 			if (mapping_writably_mapped(mapping))
17761da177e4SLinus Torvalds 				flush_dcache_page(page);
17771da177e4SLinus Torvalds 			/*
17781da177e4SLinus Torvalds 			 * Mark the page accessed if we read the beginning.
17791da177e4SLinus Torvalds 			 */
17801da177e4SLinus Torvalds 			if (!offset)
17811da177e4SLinus Torvalds 				mark_page_accessed(page);
1782b5810039SNick Piggin 		} else {
17831da177e4SLinus Torvalds 			page = ZERO_PAGE(0);
1784b5810039SNick Piggin 			page_cache_get(page);
1785b5810039SNick Piggin 		}
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 		/*
17881da177e4SLinus Torvalds 		 * Ok, we have the page, and it's up-to-date, so
17891da177e4SLinus Torvalds 		 * now we can copy it to user space...
17901da177e4SLinus Torvalds 		 *
17911da177e4SLinus Torvalds 		 * The actor routine returns how many bytes were actually used..
17921da177e4SLinus Torvalds 		 * NOTE! This may not be the same as how much of a user buffer
17931da177e4SLinus Torvalds 		 * we filled up (we may be padding etc), so we can only update
17941da177e4SLinus Torvalds 		 * "pos" here (the actor routine has to update the user buffer
17951da177e4SLinus Torvalds 		 * pointers and the remaining count).
17961da177e4SLinus Torvalds 		 */
17971da177e4SLinus Torvalds 		ret = actor(desc, page, offset, nr);
17981da177e4SLinus Torvalds 		offset += ret;
17991da177e4SLinus Torvalds 		index += offset >> PAGE_CACHE_SHIFT;
18001da177e4SLinus Torvalds 		offset &= ~PAGE_CACHE_MASK;
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 		page_cache_release(page);
18031da177e4SLinus Torvalds 		if (ret != nr || !desc->count)
18041da177e4SLinus Torvalds 			break;
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 		cond_resched();
18071da177e4SLinus Torvalds 	}
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 	*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
18101da177e4SLinus Torvalds 	file_accessed(filp);
18111da177e4SLinus Torvalds }
18121da177e4SLinus Torvalds 
1813bcd78e49SHugh Dickins static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1814bcd78e49SHugh Dickins 		const struct iovec *iov, unsigned long nr_segs, loff_t pos)
18151da177e4SLinus Torvalds {
1816bcd78e49SHugh Dickins 	struct file *filp = iocb->ki_filp;
1817bcd78e49SHugh Dickins 	ssize_t retval;
1818bcd78e49SHugh Dickins 	unsigned long seg;
1819bcd78e49SHugh Dickins 	size_t count;
1820bcd78e49SHugh Dickins 	loff_t *ppos = &iocb->ki_pos;
1821bcd78e49SHugh Dickins 
1822bcd78e49SHugh Dickins 	retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1823bcd78e49SHugh Dickins 	if (retval)
1824bcd78e49SHugh Dickins 		return retval;
1825bcd78e49SHugh Dickins 
1826bcd78e49SHugh Dickins 	for (seg = 0; seg < nr_segs; seg++) {
18271da177e4SLinus Torvalds 		read_descriptor_t desc;
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 		desc.written = 0;
1830bcd78e49SHugh Dickins 		desc.arg.buf = iov[seg].iov_base;
1831bcd78e49SHugh Dickins 		desc.count = iov[seg].iov_len;
1832bcd78e49SHugh Dickins 		if (desc.count == 0)
1833bcd78e49SHugh Dickins 			continue;
18341da177e4SLinus Torvalds 		desc.error = 0;
18351da177e4SLinus Torvalds 		do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1836bcd78e49SHugh Dickins 		retval += desc.written;
1837bcd78e49SHugh Dickins 		if (desc.error) {
1838bcd78e49SHugh Dickins 			retval = retval ?: desc.error;
1839bcd78e49SHugh Dickins 			break;
1840bcd78e49SHugh Dickins 		}
1841bcd78e49SHugh Dickins 		if (desc.count > 0)
1842bcd78e49SHugh Dickins 			break;
1843bcd78e49SHugh Dickins 	}
1844bcd78e49SHugh Dickins 	return retval;
18451da177e4SLinus Torvalds }
18461da177e4SLinus Torvalds 
1847726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
18481da177e4SLinus Torvalds {
1849726c3342SDavid Howells 	struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 	buf->f_type = TMPFS_MAGIC;
18521da177e4SLinus Torvalds 	buf->f_bsize = PAGE_CACHE_SIZE;
18531da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
18540edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
18551da177e4SLinus Torvalds 		buf->f_blocks = sbinfo->max_blocks;
18567e496299STim Chen 		buf->f_bavail = buf->f_bfree =
18577e496299STim Chen 				sbinfo->max_blocks - percpu_counter_sum(&sbinfo->used_blocks);
18580edd73b3SHugh Dickins 	}
18590edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
18601da177e4SLinus Torvalds 		buf->f_files = sbinfo->max_inodes;
18611da177e4SLinus Torvalds 		buf->f_ffree = sbinfo->free_inodes;
18621da177e4SLinus Torvalds 	}
18631da177e4SLinus Torvalds 	/* else leave those fields 0 like simple_statfs */
18641da177e4SLinus Torvalds 	return 0;
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds /*
18681da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
18691da177e4SLinus Torvalds  */
18701da177e4SLinus Torvalds static int
18711da177e4SLinus Torvalds shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
18721da177e4SLinus Torvalds {
18730b0a0806SHugh Dickins 	struct inode *inode;
18741da177e4SLinus Torvalds 	int error = -ENOSPC;
18751da177e4SLinus Torvalds 
1876454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
18771da177e4SLinus Torvalds 	if (inode) {
18782a7dba39SEric Paris 		error = security_inode_init_security(inode, dir,
18792a7dba39SEric Paris 						     &dentry->d_name, NULL,
18802a7dba39SEric Paris 						     NULL, NULL);
1881570bc1c2SStephen Smalley 		if (error) {
1882570bc1c2SStephen Smalley 			if (error != -EOPNOTSUPP) {
1883570bc1c2SStephen Smalley 				iput(inode);
1884570bc1c2SStephen Smalley 				return error;
1885570bc1c2SStephen Smalley 			}
188639f0247dSAndreas Gruenbacher 		}
18871c7c474cSChristoph Hellwig #ifdef CONFIG_TMPFS_POSIX_ACL
18881c7c474cSChristoph Hellwig 		error = generic_acl_init(inode, dir);
188939f0247dSAndreas Gruenbacher 		if (error) {
189039f0247dSAndreas Gruenbacher 			iput(inode);
189139f0247dSAndreas Gruenbacher 			return error;
1892570bc1c2SStephen Smalley 		}
1893718deb6bSAl Viro #else
1894718deb6bSAl Viro 		error = 0;
18951c7c474cSChristoph Hellwig #endif
18961da177e4SLinus Torvalds 		dir->i_size += BOGO_DIRENT_SIZE;
18971da177e4SLinus Torvalds 		dir->i_ctime = dir->i_mtime = CURRENT_TIME;
18981da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
18991da177e4SLinus Torvalds 		dget(dentry); /* Extra count - pin the dentry in core */
19001da177e4SLinus Torvalds 	}
19011da177e4SLinus Torvalds 	return error;
19021da177e4SLinus Torvalds }
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
19051da177e4SLinus Torvalds {
19061da177e4SLinus Torvalds 	int error;
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds 	if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
19091da177e4SLinus Torvalds 		return error;
1910d8c76e6fSDave Hansen 	inc_nlink(dir);
19111da177e4SLinus Torvalds 	return 0;
19121da177e4SLinus Torvalds }
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
19151da177e4SLinus Torvalds 		struct nameidata *nd)
19161da177e4SLinus Torvalds {
19171da177e4SLinus Torvalds 	return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
19181da177e4SLinus Torvalds }
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds /*
19211da177e4SLinus Torvalds  * Link a file..
19221da177e4SLinus Torvalds  */
19231da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
19241da177e4SLinus Torvalds {
19251da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
19265b04c689SPavel Emelyanov 	int ret;
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds 	/*
19291da177e4SLinus Torvalds 	 * No ordinary (disk based) filesystem counts links as inodes;
19301da177e4SLinus Torvalds 	 * but each new link needs a new dentry, pinning lowmem, and
19311da177e4SLinus Torvalds 	 * tmpfs dentries cannot be pruned until they are unlinked.
19321da177e4SLinus Torvalds 	 */
19335b04c689SPavel Emelyanov 	ret = shmem_reserve_inode(inode->i_sb);
19345b04c689SPavel Emelyanov 	if (ret)
19355b04c689SPavel Emelyanov 		goto out;
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
19381da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1939d8c76e6fSDave Hansen 	inc_nlink(inode);
19407de9c6eeSAl Viro 	ihold(inode);	/* New dentry reference */
19411da177e4SLinus Torvalds 	dget(dentry);		/* Extra pinning count for the created dentry */
19421da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
19435b04c689SPavel Emelyanov out:
19445b04c689SPavel Emelyanov 	return ret;
19451da177e4SLinus Torvalds }
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry)
19481da177e4SLinus Torvalds {
19491da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19501da177e4SLinus Torvalds 
19515b04c689SPavel Emelyanov 	if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
19525b04c689SPavel Emelyanov 		shmem_free_inode(inode->i_sb);
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	dir->i_size -= BOGO_DIRENT_SIZE;
19551da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
19569a53c3a7SDave Hansen 	drop_nlink(inode);
19571da177e4SLinus Torvalds 	dput(dentry);	/* Undo the count from "create" - this does all the work */
19581da177e4SLinus Torvalds 	return 0;
19591da177e4SLinus Torvalds }
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
19621da177e4SLinus Torvalds {
19631da177e4SLinus Torvalds 	if (!simple_empty(dentry))
19641da177e4SLinus Torvalds 		return -ENOTEMPTY;
19651da177e4SLinus Torvalds 
19669a53c3a7SDave Hansen 	drop_nlink(dentry->d_inode);
19679a53c3a7SDave Hansen 	drop_nlink(dir);
19681da177e4SLinus Torvalds 	return shmem_unlink(dir, dentry);
19691da177e4SLinus Torvalds }
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds /*
19721da177e4SLinus Torvalds  * The VFS layer already does all the dentry stuff for rename,
19731da177e4SLinus Torvalds  * we just have to decrement the usage count for the target if
19741da177e4SLinus Torvalds  * it exists so that the VFS layer correctly free's it when it
19751da177e4SLinus Torvalds  * gets overwritten.
19761da177e4SLinus Torvalds  */
19771da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
19781da177e4SLinus Torvalds {
19791da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
19801da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(inode->i_mode);
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
19831da177e4SLinus Torvalds 		return -ENOTEMPTY;
19841da177e4SLinus Torvalds 
19851da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
19861da177e4SLinus Torvalds 		(void) shmem_unlink(new_dir, new_dentry);
19871da177e4SLinus Torvalds 		if (they_are_dirs)
19889a53c3a7SDave Hansen 			drop_nlink(old_dir);
19891da177e4SLinus Torvalds 	} else if (they_are_dirs) {
19909a53c3a7SDave Hansen 		drop_nlink(old_dir);
1991d8c76e6fSDave Hansen 		inc_nlink(new_dir);
19921da177e4SLinus Torvalds 	}
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	old_dir->i_size -= BOGO_DIRENT_SIZE;
19951da177e4SLinus Torvalds 	new_dir->i_size += BOGO_DIRENT_SIZE;
19961da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime =
19971da177e4SLinus Torvalds 	new_dir->i_ctime = new_dir->i_mtime =
19981da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME;
19991da177e4SLinus Torvalds 	return 0;
20001da177e4SLinus Torvalds }
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20031da177e4SLinus Torvalds {
20041da177e4SLinus Torvalds 	int error;
20051da177e4SLinus Torvalds 	int len;
20061da177e4SLinus Torvalds 	struct inode *inode;
20071da177e4SLinus Torvalds 	struct page *page = NULL;
20081da177e4SLinus Torvalds 	char *kaddr;
20091da177e4SLinus Torvalds 	struct shmem_inode_info *info;
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	len = strlen(symname) + 1;
20121da177e4SLinus Torvalds 	if (len > PAGE_CACHE_SIZE)
20131da177e4SLinus Torvalds 		return -ENAMETOOLONG;
20141da177e4SLinus Torvalds 
2015454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
20161da177e4SLinus Torvalds 	if (!inode)
20171da177e4SLinus Torvalds 		return -ENOSPC;
20181da177e4SLinus Torvalds 
20192a7dba39SEric Paris 	error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
20202a7dba39SEric Paris 					     NULL, NULL);
2021570bc1c2SStephen Smalley 	if (error) {
2022570bc1c2SStephen Smalley 		if (error != -EOPNOTSUPP) {
2023570bc1c2SStephen Smalley 			iput(inode);
2024570bc1c2SStephen Smalley 			return error;
2025570bc1c2SStephen Smalley 		}
2026570bc1c2SStephen Smalley 		error = 0;
2027570bc1c2SStephen Smalley 	}
2028570bc1c2SStephen Smalley 
20291da177e4SLinus Torvalds 	info = SHMEM_I(inode);
20301da177e4SLinus Torvalds 	inode->i_size = len-1;
2031*b09e0fa4SEric Paris 	if (len <= SHMEM_SYMLINK_INLINE_LEN) {
20321da177e4SLinus Torvalds 		/* do it inline */
2033*b09e0fa4SEric Paris 		memcpy(info->inline_symlink, symname, len);
20341da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inline_operations;
20351da177e4SLinus Torvalds 	} else {
20361da177e4SLinus Torvalds 		error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
20371da177e4SLinus Torvalds 		if (error) {
20381da177e4SLinus Torvalds 			iput(inode);
20391da177e4SLinus Torvalds 			return error;
20401da177e4SLinus Torvalds 		}
204114fcc23fSHugh Dickins 		inode->i_mapping->a_ops = &shmem_aops;
20421da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inode_operations;
20431da177e4SLinus Torvalds 		kaddr = kmap_atomic(page, KM_USER0);
20441da177e4SLinus Torvalds 		memcpy(kaddr, symname, len);
20451da177e4SLinus Torvalds 		kunmap_atomic(kaddr, KM_USER0);
20461da177e4SLinus Torvalds 		set_page_dirty(page);
20476746aff7SWu Fengguang 		unlock_page(page);
20481da177e4SLinus Torvalds 		page_cache_release(page);
20491da177e4SLinus Torvalds 	}
20501da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
20511da177e4SLinus Torvalds 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
20521da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
20531da177e4SLinus Torvalds 	dget(dentry);
20541da177e4SLinus Torvalds 	return 0;
20551da177e4SLinus Torvalds }
20561da177e4SLinus Torvalds 
2057cc314eefSLinus Torvalds static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
20581da177e4SLinus Torvalds {
2059*b09e0fa4SEric Paris 	nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
2060cc314eefSLinus Torvalds 	return NULL;
20611da177e4SLinus Torvalds }
20621da177e4SLinus Torvalds 
2063cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
20641da177e4SLinus Torvalds {
20651da177e4SLinus Torvalds 	struct page *page = NULL;
20661da177e4SLinus Torvalds 	int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
20671da177e4SLinus Torvalds 	nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
2068d3602444SHugh Dickins 	if (page)
2069d3602444SHugh Dickins 		unlock_page(page);
2070cc314eefSLinus Torvalds 	return page;
20711da177e4SLinus Torvalds }
20721da177e4SLinus Torvalds 
2073cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
20741da177e4SLinus Torvalds {
20751da177e4SLinus Torvalds 	if (!IS_ERR(nd_get_link(nd))) {
2076cc314eefSLinus Torvalds 		struct page *page = cookie;
20771da177e4SLinus Torvalds 		kunmap(page);
20781da177e4SLinus Torvalds 		mark_page_accessed(page);
20791da177e4SLinus Torvalds 		page_cache_release(page);
20801da177e4SLinus Torvalds 	}
20811da177e4SLinus Torvalds }
20821da177e4SLinus Torvalds 
2083*b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2084*b09e0fa4SEric Paris /*
2085*b09e0fa4SEric Paris  * Superblocks without xattr inode operations may get some security.* xattr
2086*b09e0fa4SEric Paris  * support from the LSM "for free". As soon as we have any other xattrs
2087*b09e0fa4SEric Paris  * like ACLs, we also need to implement the security.* handlers at
2088*b09e0fa4SEric Paris  * filesystem level, though.
2089*b09e0fa4SEric Paris  */
2090*b09e0fa4SEric Paris 
2091*b09e0fa4SEric Paris static int shmem_xattr_get(struct dentry *dentry, const char *name,
2092*b09e0fa4SEric Paris 			   void *buffer, size_t size)
2093*b09e0fa4SEric Paris {
2094*b09e0fa4SEric Paris 	struct shmem_inode_info *info;
2095*b09e0fa4SEric Paris 	struct shmem_xattr *xattr;
2096*b09e0fa4SEric Paris 	int ret = -ENODATA;
2097*b09e0fa4SEric Paris 
2098*b09e0fa4SEric Paris 	info = SHMEM_I(dentry->d_inode);
2099*b09e0fa4SEric Paris 
2100*b09e0fa4SEric Paris 	spin_lock(&info->lock);
2101*b09e0fa4SEric Paris 	list_for_each_entry(xattr, &info->xattr_list, list) {
2102*b09e0fa4SEric Paris 		if (strcmp(name, xattr->name))
2103*b09e0fa4SEric Paris 			continue;
2104*b09e0fa4SEric Paris 
2105*b09e0fa4SEric Paris 		ret = xattr->size;
2106*b09e0fa4SEric Paris 		if (buffer) {
2107*b09e0fa4SEric Paris 			if (size < xattr->size)
2108*b09e0fa4SEric Paris 				ret = -ERANGE;
2109*b09e0fa4SEric Paris 			else
2110*b09e0fa4SEric Paris 				memcpy(buffer, xattr->value, xattr->size);
2111*b09e0fa4SEric Paris 		}
2112*b09e0fa4SEric Paris 		break;
2113*b09e0fa4SEric Paris 	}
2114*b09e0fa4SEric Paris 	spin_unlock(&info->lock);
2115*b09e0fa4SEric Paris 	return ret;
2116*b09e0fa4SEric Paris }
2117*b09e0fa4SEric Paris 
2118*b09e0fa4SEric Paris static int shmem_xattr_set(struct dentry *dentry, const char *name,
2119*b09e0fa4SEric Paris 			   const void *value, size_t size, int flags)
2120*b09e0fa4SEric Paris {
2121*b09e0fa4SEric Paris 	struct inode *inode = dentry->d_inode;
2122*b09e0fa4SEric Paris 	struct shmem_inode_info *info = SHMEM_I(inode);
2123*b09e0fa4SEric Paris 	struct shmem_xattr *xattr;
2124*b09e0fa4SEric Paris 	struct shmem_xattr *new_xattr = NULL;
2125*b09e0fa4SEric Paris 	size_t len;
2126*b09e0fa4SEric Paris 	int err = 0;
2127*b09e0fa4SEric Paris 
2128*b09e0fa4SEric Paris 	/* value == NULL means remove */
2129*b09e0fa4SEric Paris 	if (value) {
2130*b09e0fa4SEric Paris 		/* wrap around? */
2131*b09e0fa4SEric Paris 		len = sizeof(*new_xattr) + size;
2132*b09e0fa4SEric Paris 		if (len <= sizeof(*new_xattr))
2133*b09e0fa4SEric Paris 			return -ENOMEM;
2134*b09e0fa4SEric Paris 
2135*b09e0fa4SEric Paris 		new_xattr = kmalloc(len, GFP_KERNEL);
2136*b09e0fa4SEric Paris 		if (!new_xattr)
2137*b09e0fa4SEric Paris 			return -ENOMEM;
2138*b09e0fa4SEric Paris 
2139*b09e0fa4SEric Paris 		new_xattr->name = kstrdup(name, GFP_KERNEL);
2140*b09e0fa4SEric Paris 		if (!new_xattr->name) {
2141*b09e0fa4SEric Paris 			kfree(new_xattr);
2142*b09e0fa4SEric Paris 			return -ENOMEM;
2143*b09e0fa4SEric Paris 		}
2144*b09e0fa4SEric Paris 
2145*b09e0fa4SEric Paris 		new_xattr->size = size;
2146*b09e0fa4SEric Paris 		memcpy(new_xattr->value, value, size);
2147*b09e0fa4SEric Paris 	}
2148*b09e0fa4SEric Paris 
2149*b09e0fa4SEric Paris 	spin_lock(&info->lock);
2150*b09e0fa4SEric Paris 	list_for_each_entry(xattr, &info->xattr_list, list) {
2151*b09e0fa4SEric Paris 		if (!strcmp(name, xattr->name)) {
2152*b09e0fa4SEric Paris 			if (flags & XATTR_CREATE) {
2153*b09e0fa4SEric Paris 				xattr = new_xattr;
2154*b09e0fa4SEric Paris 				err = -EEXIST;
2155*b09e0fa4SEric Paris 			} else if (new_xattr) {
2156*b09e0fa4SEric Paris 				list_replace(&xattr->list, &new_xattr->list);
2157*b09e0fa4SEric Paris 			} else {
2158*b09e0fa4SEric Paris 				list_del(&xattr->list);
2159*b09e0fa4SEric Paris 			}
2160*b09e0fa4SEric Paris 			goto out;
2161*b09e0fa4SEric Paris 		}
2162*b09e0fa4SEric Paris 	}
2163*b09e0fa4SEric Paris 	if (flags & XATTR_REPLACE) {
2164*b09e0fa4SEric Paris 		xattr = new_xattr;
2165*b09e0fa4SEric Paris 		err = -ENODATA;
2166*b09e0fa4SEric Paris 	} else {
2167*b09e0fa4SEric Paris 		list_add(&new_xattr->list, &info->xattr_list);
2168*b09e0fa4SEric Paris 		xattr = NULL;
2169*b09e0fa4SEric Paris 	}
2170*b09e0fa4SEric Paris out:
2171*b09e0fa4SEric Paris 	spin_unlock(&info->lock);
2172*b09e0fa4SEric Paris 	if (xattr)
2173*b09e0fa4SEric Paris 		kfree(xattr->name);
2174*b09e0fa4SEric Paris 	kfree(xattr);
2175*b09e0fa4SEric Paris 	return err;
2176*b09e0fa4SEric Paris }
2177*b09e0fa4SEric Paris 
2178*b09e0fa4SEric Paris 
2179*b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = {
2180*b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
2181*b09e0fa4SEric Paris 	&generic_acl_access_handler,
2182*b09e0fa4SEric Paris 	&generic_acl_default_handler,
2183*b09e0fa4SEric Paris #endif
2184*b09e0fa4SEric Paris 	NULL
2185*b09e0fa4SEric Paris };
2186*b09e0fa4SEric Paris 
2187*b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name)
2188*b09e0fa4SEric Paris {
2189*b09e0fa4SEric Paris 	struct { const char *prefix; size_t len; } arr[] = {
2190*b09e0fa4SEric Paris 		{ XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
2191*b09e0fa4SEric Paris 		{ XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
2192*b09e0fa4SEric Paris 	};
2193*b09e0fa4SEric Paris 	int i;
2194*b09e0fa4SEric Paris 
2195*b09e0fa4SEric Paris 	for (i = 0; i < ARRAY_SIZE(arr); i++) {
2196*b09e0fa4SEric Paris 		size_t preflen = arr[i].len;
2197*b09e0fa4SEric Paris 		if (strncmp(name, arr[i].prefix, preflen) == 0) {
2198*b09e0fa4SEric Paris 			if (!name[preflen])
2199*b09e0fa4SEric Paris 				return -EINVAL;
2200*b09e0fa4SEric Paris 			return 0;
2201*b09e0fa4SEric Paris 		}
2202*b09e0fa4SEric Paris 	}
2203*b09e0fa4SEric Paris 	return -EOPNOTSUPP;
2204*b09e0fa4SEric Paris }
2205*b09e0fa4SEric Paris 
2206*b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
2207*b09e0fa4SEric Paris 			      void *buffer, size_t size)
2208*b09e0fa4SEric Paris {
2209*b09e0fa4SEric Paris 	int err;
2210*b09e0fa4SEric Paris 
2211*b09e0fa4SEric Paris 	/*
2212*b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2213*b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2214*b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2215*b09e0fa4SEric Paris 	 */
2216*b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2217*b09e0fa4SEric Paris 		return generic_getxattr(dentry, name, buffer, size);
2218*b09e0fa4SEric Paris 
2219*b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2220*b09e0fa4SEric Paris 	if (err)
2221*b09e0fa4SEric Paris 		return err;
2222*b09e0fa4SEric Paris 
2223*b09e0fa4SEric Paris 	return shmem_xattr_get(dentry, name, buffer, size);
2224*b09e0fa4SEric Paris }
2225*b09e0fa4SEric Paris 
2226*b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name,
2227*b09e0fa4SEric Paris 			  const void *value, size_t size, int flags)
2228*b09e0fa4SEric Paris {
2229*b09e0fa4SEric Paris 	int err;
2230*b09e0fa4SEric Paris 
2231*b09e0fa4SEric Paris 	/*
2232*b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2233*b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2234*b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2235*b09e0fa4SEric Paris 	 */
2236*b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2237*b09e0fa4SEric Paris 		return generic_setxattr(dentry, name, value, size, flags);
2238*b09e0fa4SEric Paris 
2239*b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2240*b09e0fa4SEric Paris 	if (err)
2241*b09e0fa4SEric Paris 		return err;
2242*b09e0fa4SEric Paris 
2243*b09e0fa4SEric Paris 	if (size == 0)
2244*b09e0fa4SEric Paris 		value = "";  /* empty EA, do not remove */
2245*b09e0fa4SEric Paris 
2246*b09e0fa4SEric Paris 	return shmem_xattr_set(dentry, name, value, size, flags);
2247*b09e0fa4SEric Paris 
2248*b09e0fa4SEric Paris }
2249*b09e0fa4SEric Paris 
2250*b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name)
2251*b09e0fa4SEric Paris {
2252*b09e0fa4SEric Paris 	int err;
2253*b09e0fa4SEric Paris 
2254*b09e0fa4SEric Paris 	/*
2255*b09e0fa4SEric Paris 	 * If this is a request for a synthetic attribute in the system.*
2256*b09e0fa4SEric Paris 	 * namespace use the generic infrastructure to resolve a handler
2257*b09e0fa4SEric Paris 	 * for it via sb->s_xattr.
2258*b09e0fa4SEric Paris 	 */
2259*b09e0fa4SEric Paris 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2260*b09e0fa4SEric Paris 		return generic_removexattr(dentry, name);
2261*b09e0fa4SEric Paris 
2262*b09e0fa4SEric Paris 	err = shmem_xattr_validate(name);
2263*b09e0fa4SEric Paris 	if (err)
2264*b09e0fa4SEric Paris 		return err;
2265*b09e0fa4SEric Paris 
2266*b09e0fa4SEric Paris 	return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
2267*b09e0fa4SEric Paris }
2268*b09e0fa4SEric Paris 
2269*b09e0fa4SEric Paris static bool xattr_is_trusted(const char *name)
2270*b09e0fa4SEric Paris {
2271*b09e0fa4SEric Paris 	return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
2272*b09e0fa4SEric Paris }
2273*b09e0fa4SEric Paris 
2274*b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2275*b09e0fa4SEric Paris {
2276*b09e0fa4SEric Paris 	bool trusted = capable(CAP_SYS_ADMIN);
2277*b09e0fa4SEric Paris 	struct shmem_xattr *xattr;
2278*b09e0fa4SEric Paris 	struct shmem_inode_info *info;
2279*b09e0fa4SEric Paris 	size_t used = 0;
2280*b09e0fa4SEric Paris 
2281*b09e0fa4SEric Paris 	info = SHMEM_I(dentry->d_inode);
2282*b09e0fa4SEric Paris 
2283*b09e0fa4SEric Paris 	spin_lock(&info->lock);
2284*b09e0fa4SEric Paris 	list_for_each_entry(xattr, &info->xattr_list, list) {
2285*b09e0fa4SEric Paris 		size_t len;
2286*b09e0fa4SEric Paris 
2287*b09e0fa4SEric Paris 		/* skip "trusted." attributes for unprivileged callers */
2288*b09e0fa4SEric Paris 		if (!trusted && xattr_is_trusted(xattr->name))
2289*b09e0fa4SEric Paris 			continue;
2290*b09e0fa4SEric Paris 
2291*b09e0fa4SEric Paris 		len = strlen(xattr->name) + 1;
2292*b09e0fa4SEric Paris 		used += len;
2293*b09e0fa4SEric Paris 		if (buffer) {
2294*b09e0fa4SEric Paris 			if (size < used) {
2295*b09e0fa4SEric Paris 				used = -ERANGE;
2296*b09e0fa4SEric Paris 				break;
2297*b09e0fa4SEric Paris 			}
2298*b09e0fa4SEric Paris 			memcpy(buffer, xattr->name, len);
2299*b09e0fa4SEric Paris 			buffer += len;
2300*b09e0fa4SEric Paris 		}
2301*b09e0fa4SEric Paris 	}
2302*b09e0fa4SEric Paris 	spin_unlock(&info->lock);
2303*b09e0fa4SEric Paris 
2304*b09e0fa4SEric Paris 	return used;
2305*b09e0fa4SEric Paris }
2306*b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */
2307*b09e0fa4SEric Paris 
230892e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations = {
23091da177e4SLinus Torvalds 	.readlink	= generic_readlink,
23101da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link_inline,
2311*b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2312*b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2313*b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2314*b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2315*b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2316*b09e0fa4SEric Paris #endif
23171da177e4SLinus Torvalds };
23181da177e4SLinus Torvalds 
231992e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = {
23201da177e4SLinus Torvalds 	.readlink	= generic_readlink,
23211da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link,
23221da177e4SLinus Torvalds 	.put_link	= shmem_put_link,
2323*b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2324*b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2325*b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2326*b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2327*b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
232839f0247dSAndreas Gruenbacher #endif
2329*b09e0fa4SEric Paris };
233039f0247dSAndreas Gruenbacher 
233191828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child)
233291828a40SDavid M. Grimes {
233391828a40SDavid M. Grimes 	return ERR_PTR(-ESTALE);
233491828a40SDavid M. Grimes }
233591828a40SDavid M. Grimes 
233691828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh)
233791828a40SDavid M. Grimes {
233891828a40SDavid M. Grimes 	__u32 *fh = vfh;
233991828a40SDavid M. Grimes 	__u64 inum = fh[2];
234091828a40SDavid M. Grimes 	inum = (inum << 32) | fh[1];
234191828a40SDavid M. Grimes 	return ino->i_ino == inum && fh[0] == ino->i_generation;
234291828a40SDavid M. Grimes }
234391828a40SDavid M. Grimes 
2344480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2345480b116cSChristoph Hellwig 		struct fid *fid, int fh_len, int fh_type)
234691828a40SDavid M. Grimes {
234791828a40SDavid M. Grimes 	struct inode *inode;
2348480b116cSChristoph Hellwig 	struct dentry *dentry = NULL;
2349480b116cSChristoph Hellwig 	u64 inum = fid->raw[2];
2350480b116cSChristoph Hellwig 	inum = (inum << 32) | fid->raw[1];
235191828a40SDavid M. Grimes 
2352480b116cSChristoph Hellwig 	if (fh_len < 3)
2353480b116cSChristoph Hellwig 		return NULL;
2354480b116cSChristoph Hellwig 
2355480b116cSChristoph Hellwig 	inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2356480b116cSChristoph Hellwig 			shmem_match, fid->raw);
235791828a40SDavid M. Grimes 	if (inode) {
2358480b116cSChristoph Hellwig 		dentry = d_find_alias(inode);
235991828a40SDavid M. Grimes 		iput(inode);
236091828a40SDavid M. Grimes 	}
236191828a40SDavid M. Grimes 
2362480b116cSChristoph Hellwig 	return dentry;
236391828a40SDavid M. Grimes }
236491828a40SDavid M. Grimes 
236591828a40SDavid M. Grimes static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
236691828a40SDavid M. Grimes 				int connectable)
236791828a40SDavid M. Grimes {
236891828a40SDavid M. Grimes 	struct inode *inode = dentry->d_inode;
236991828a40SDavid M. Grimes 
23705fe0c237SAneesh Kumar K.V 	if (*len < 3) {
23715fe0c237SAneesh Kumar K.V 		*len = 3;
237291828a40SDavid M. Grimes 		return 255;
23735fe0c237SAneesh Kumar K.V 	}
237491828a40SDavid M. Grimes 
23751d3382cbSAl Viro 	if (inode_unhashed(inode)) {
237691828a40SDavid M. Grimes 		/* Unfortunately insert_inode_hash is not idempotent,
237791828a40SDavid M. Grimes 		 * so as we hash inodes here rather than at creation
237891828a40SDavid M. Grimes 		 * time, we need a lock to ensure we only try
237991828a40SDavid M. Grimes 		 * to do it once
238091828a40SDavid M. Grimes 		 */
238191828a40SDavid M. Grimes 		static DEFINE_SPINLOCK(lock);
238291828a40SDavid M. Grimes 		spin_lock(&lock);
23831d3382cbSAl Viro 		if (inode_unhashed(inode))
238491828a40SDavid M. Grimes 			__insert_inode_hash(inode,
238591828a40SDavid M. Grimes 					    inode->i_ino + inode->i_generation);
238691828a40SDavid M. Grimes 		spin_unlock(&lock);
238791828a40SDavid M. Grimes 	}
238891828a40SDavid M. Grimes 
238991828a40SDavid M. Grimes 	fh[0] = inode->i_generation;
239091828a40SDavid M. Grimes 	fh[1] = inode->i_ino;
239191828a40SDavid M. Grimes 	fh[2] = ((__u64)inode->i_ino) >> 32;
239291828a40SDavid M. Grimes 
239391828a40SDavid M. Grimes 	*len = 3;
239491828a40SDavid M. Grimes 	return 1;
239591828a40SDavid M. Grimes }
239691828a40SDavid M. Grimes 
239739655164SChristoph Hellwig static const struct export_operations shmem_export_ops = {
239891828a40SDavid M. Grimes 	.get_parent     = shmem_get_parent,
239991828a40SDavid M. Grimes 	.encode_fh      = shmem_encode_fh,
2400480b116cSChristoph Hellwig 	.fh_to_dentry	= shmem_fh_to_dentry,
240191828a40SDavid M. Grimes };
240291828a40SDavid M. Grimes 
2403680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2404680d794bSakpm@linux-foundation.org 			       bool remount)
24051da177e4SLinus Torvalds {
24061da177e4SLinus Torvalds 	char *this_char, *value, *rest;
24071da177e4SLinus Torvalds 
2408b00dc3adSHugh Dickins 	while (options != NULL) {
2409b00dc3adSHugh Dickins 		this_char = options;
2410b00dc3adSHugh Dickins 		for (;;) {
2411b00dc3adSHugh Dickins 			/*
2412b00dc3adSHugh Dickins 			 * NUL-terminate this option: unfortunately,
2413b00dc3adSHugh Dickins 			 * mount options form a comma-separated list,
2414b00dc3adSHugh Dickins 			 * but mpol's nodelist may also contain commas.
2415b00dc3adSHugh Dickins 			 */
2416b00dc3adSHugh Dickins 			options = strchr(options, ',');
2417b00dc3adSHugh Dickins 			if (options == NULL)
2418b00dc3adSHugh Dickins 				break;
2419b00dc3adSHugh Dickins 			options++;
2420b00dc3adSHugh Dickins 			if (!isdigit(*options)) {
2421b00dc3adSHugh Dickins 				options[-1] = '\0';
2422b00dc3adSHugh Dickins 				break;
2423b00dc3adSHugh Dickins 			}
2424b00dc3adSHugh Dickins 		}
24251da177e4SLinus Torvalds 		if (!*this_char)
24261da177e4SLinus Torvalds 			continue;
24271da177e4SLinus Torvalds 		if ((value = strchr(this_char,'=')) != NULL) {
24281da177e4SLinus Torvalds 			*value++ = 0;
24291da177e4SLinus Torvalds 		} else {
24301da177e4SLinus Torvalds 			printk(KERN_ERR
24311da177e4SLinus Torvalds 			    "tmpfs: No value for mount option '%s'\n",
24321da177e4SLinus Torvalds 			    this_char);
24331da177e4SLinus Torvalds 			return 1;
24341da177e4SLinus Torvalds 		}
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds 		if (!strcmp(this_char,"size")) {
24371da177e4SLinus Torvalds 			unsigned long long size;
24381da177e4SLinus Torvalds 			size = memparse(value,&rest);
24391da177e4SLinus Torvalds 			if (*rest == '%') {
24401da177e4SLinus Torvalds 				size <<= PAGE_SHIFT;
24411da177e4SLinus Torvalds 				size *= totalram_pages;
24421da177e4SLinus Torvalds 				do_div(size, 100);
24431da177e4SLinus Torvalds 				rest++;
24441da177e4SLinus Torvalds 			}
24451da177e4SLinus Torvalds 			if (*rest)
24461da177e4SLinus Torvalds 				goto bad_val;
2447680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks =
2448680d794bSakpm@linux-foundation.org 				DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
24491da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_blocks")) {
2450680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks = memparse(value, &rest);
24511da177e4SLinus Torvalds 			if (*rest)
24521da177e4SLinus Torvalds 				goto bad_val;
24531da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_inodes")) {
2454680d794bSakpm@linux-foundation.org 			sbinfo->max_inodes = memparse(value, &rest);
24551da177e4SLinus Torvalds 			if (*rest)
24561da177e4SLinus Torvalds 				goto bad_val;
24571da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"mode")) {
2458680d794bSakpm@linux-foundation.org 			if (remount)
24591da177e4SLinus Torvalds 				continue;
2460680d794bSakpm@linux-foundation.org 			sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
24611da177e4SLinus Torvalds 			if (*rest)
24621da177e4SLinus Torvalds 				goto bad_val;
24631da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"uid")) {
2464680d794bSakpm@linux-foundation.org 			if (remount)
24651da177e4SLinus Torvalds 				continue;
2466680d794bSakpm@linux-foundation.org 			sbinfo->uid = simple_strtoul(value, &rest, 0);
24671da177e4SLinus Torvalds 			if (*rest)
24681da177e4SLinus Torvalds 				goto bad_val;
24691da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"gid")) {
2470680d794bSakpm@linux-foundation.org 			if (remount)
24711da177e4SLinus Torvalds 				continue;
2472680d794bSakpm@linux-foundation.org 			sbinfo->gid = simple_strtoul(value, &rest, 0);
24731da177e4SLinus Torvalds 			if (*rest)
24741da177e4SLinus Torvalds 				goto bad_val;
24757339ff83SRobin Holt 		} else if (!strcmp(this_char,"mpol")) {
247671fe804bSLee Schermerhorn 			if (mpol_parse_str(value, &sbinfo->mpol, 1))
24777339ff83SRobin Holt 				goto bad_val;
24781da177e4SLinus Torvalds 		} else {
24791da177e4SLinus Torvalds 			printk(KERN_ERR "tmpfs: Bad mount option %s\n",
24801da177e4SLinus Torvalds 			       this_char);
24811da177e4SLinus Torvalds 			return 1;
24821da177e4SLinus Torvalds 		}
24831da177e4SLinus Torvalds 	}
24841da177e4SLinus Torvalds 	return 0;
24851da177e4SLinus Torvalds 
24861da177e4SLinus Torvalds bad_val:
24871da177e4SLinus Torvalds 	printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
24881da177e4SLinus Torvalds 	       value, this_char);
24891da177e4SLinus Torvalds 	return 1;
24901da177e4SLinus Torvalds 
24911da177e4SLinus Torvalds }
24921da177e4SLinus Torvalds 
24931da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
24941da177e4SLinus Torvalds {
24951da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2496680d794bSakpm@linux-foundation.org 	struct shmem_sb_info config = *sbinfo;
24970edd73b3SHugh Dickins 	unsigned long inodes;
24980edd73b3SHugh Dickins 	int error = -EINVAL;
24991da177e4SLinus Torvalds 
2500680d794bSakpm@linux-foundation.org 	if (shmem_parse_options(data, &config, true))
25010edd73b3SHugh Dickins 		return error;
25020edd73b3SHugh Dickins 
25030edd73b3SHugh Dickins 	spin_lock(&sbinfo->stat_lock);
25040edd73b3SHugh Dickins 	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
25057e496299STim Chen 	if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
25060edd73b3SHugh Dickins 		goto out;
2507680d794bSakpm@linux-foundation.org 	if (config.max_inodes < inodes)
25080edd73b3SHugh Dickins 		goto out;
25090edd73b3SHugh Dickins 	/*
25100edd73b3SHugh Dickins 	 * Those tests also disallow limited->unlimited while any are in
25110edd73b3SHugh Dickins 	 * use, so i_blocks will always be zero when max_blocks is zero;
25120edd73b3SHugh Dickins 	 * but we must separately disallow unlimited->limited, because
25130edd73b3SHugh Dickins 	 * in that case we have no record of how much is already in use.
25140edd73b3SHugh Dickins 	 */
2515680d794bSakpm@linux-foundation.org 	if (config.max_blocks && !sbinfo->max_blocks)
25160edd73b3SHugh Dickins 		goto out;
2517680d794bSakpm@linux-foundation.org 	if (config.max_inodes && !sbinfo->max_inodes)
25180edd73b3SHugh Dickins 		goto out;
25190edd73b3SHugh Dickins 
25200edd73b3SHugh Dickins 	error = 0;
2521680d794bSakpm@linux-foundation.org 	sbinfo->max_blocks  = config.max_blocks;
2522680d794bSakpm@linux-foundation.org 	sbinfo->max_inodes  = config.max_inodes;
2523680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = config.max_inodes - inodes;
252471fe804bSLee Schermerhorn 
252571fe804bSLee Schermerhorn 	mpol_put(sbinfo->mpol);
252671fe804bSLee Schermerhorn 	sbinfo->mpol        = config.mpol;	/* transfers initial ref */
25270edd73b3SHugh Dickins out:
25280edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
25290edd73b3SHugh Dickins 	return error;
25301da177e4SLinus Torvalds }
2531680d794bSakpm@linux-foundation.org 
2532680d794bSakpm@linux-foundation.org static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2533680d794bSakpm@linux-foundation.org {
2534680d794bSakpm@linux-foundation.org 	struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2535680d794bSakpm@linux-foundation.org 
2536680d794bSakpm@linux-foundation.org 	if (sbinfo->max_blocks != shmem_default_max_blocks())
2537680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",size=%luk",
2538680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2539680d794bSakpm@linux-foundation.org 	if (sbinfo->max_inodes != shmem_default_max_inodes())
2540680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2541680d794bSakpm@linux-foundation.org 	if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2542680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",mode=%03o", sbinfo->mode);
2543680d794bSakpm@linux-foundation.org 	if (sbinfo->uid != 0)
2544680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",uid=%u", sbinfo->uid);
2545680d794bSakpm@linux-foundation.org 	if (sbinfo->gid != 0)
2546680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",gid=%u", sbinfo->gid);
254771fe804bSLee Schermerhorn 	shmem_show_mpol(seq, sbinfo->mpol);
2548680d794bSakpm@linux-foundation.org 	return 0;
2549680d794bSakpm@linux-foundation.org }
2550680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb)
25531da177e4SLinus Torvalds {
2554602586a8SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2555602586a8SHugh Dickins 
2556602586a8SHugh Dickins 	percpu_counter_destroy(&sbinfo->used_blocks);
2557602586a8SHugh Dickins 	kfree(sbinfo);
25581da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
25591da177e4SLinus Torvalds }
25601da177e4SLinus Torvalds 
25612b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent)
25621da177e4SLinus Torvalds {
25631da177e4SLinus Torvalds 	struct inode *inode;
25641da177e4SLinus Torvalds 	struct dentry *root;
25650edd73b3SHugh Dickins 	struct shmem_sb_info *sbinfo;
2566680d794bSakpm@linux-foundation.org 	int err = -ENOMEM;
2567680d794bSakpm@linux-foundation.org 
2568680d794bSakpm@linux-foundation.org 	/* Round up to L1_CACHE_BYTES to resist false sharing */
2569425fbf04SPekka Enberg 	sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
2570680d794bSakpm@linux-foundation.org 				L1_CACHE_BYTES), GFP_KERNEL);
2571680d794bSakpm@linux-foundation.org 	if (!sbinfo)
2572680d794bSakpm@linux-foundation.org 		return -ENOMEM;
2573680d794bSakpm@linux-foundation.org 
2574680d794bSakpm@linux-foundation.org 	sbinfo->mode = S_IRWXUGO | S_ISVTX;
257576aac0e9SDavid Howells 	sbinfo->uid = current_fsuid();
257676aac0e9SDavid Howells 	sbinfo->gid = current_fsgid();
2577680d794bSakpm@linux-foundation.org 	sb->s_fs_info = sbinfo;
25781da177e4SLinus Torvalds 
25790edd73b3SHugh Dickins #ifdef CONFIG_TMPFS
25801da177e4SLinus Torvalds 	/*
25811da177e4SLinus Torvalds 	 * Per default we only allow half of the physical ram per
25821da177e4SLinus Torvalds 	 * tmpfs instance, limiting inodes to one per page of lowmem;
25831da177e4SLinus Torvalds 	 * but the internal instance is left unlimited.
25841da177e4SLinus Torvalds 	 */
25851da177e4SLinus Torvalds 	if (!(sb->s_flags & MS_NOUSER)) {
2586680d794bSakpm@linux-foundation.org 		sbinfo->max_blocks = shmem_default_max_blocks();
2587680d794bSakpm@linux-foundation.org 		sbinfo->max_inodes = shmem_default_max_inodes();
2588680d794bSakpm@linux-foundation.org 		if (shmem_parse_options(data, sbinfo, false)) {
2589680d794bSakpm@linux-foundation.org 			err = -EINVAL;
2590680d794bSakpm@linux-foundation.org 			goto failed;
2591680d794bSakpm@linux-foundation.org 		}
25921da177e4SLinus Torvalds 	}
259391828a40SDavid M. Grimes 	sb->s_export_op = &shmem_export_ops;
25940edd73b3SHugh Dickins #else
25950edd73b3SHugh Dickins 	sb->s_flags |= MS_NOUSER;
25960edd73b3SHugh Dickins #endif
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
2599602586a8SHugh Dickins 	if (percpu_counter_init(&sbinfo->used_blocks, 0))
2600602586a8SHugh Dickins 		goto failed;
2601680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = sbinfo->max_inodes;
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds 	sb->s_maxbytes = SHMEM_MAX_BYTES;
26041da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
26051da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
26061da177e4SLinus Torvalds 	sb->s_magic = TMPFS_MAGIC;
26071da177e4SLinus Torvalds 	sb->s_op = &shmem_ops;
2608cfd95a9cSRobin H. Johnson 	sb->s_time_gran = 1;
2609*b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
261039f0247dSAndreas Gruenbacher 	sb->s_xattr = shmem_xattr_handlers;
2611*b09e0fa4SEric Paris #endif
2612*b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL
261339f0247dSAndreas Gruenbacher 	sb->s_flags |= MS_POSIXACL;
261439f0247dSAndreas Gruenbacher #endif
26150edd73b3SHugh Dickins 
2616454abafeSDmitry Monakhov 	inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
26171da177e4SLinus Torvalds 	if (!inode)
26181da177e4SLinus Torvalds 		goto failed;
2619680d794bSakpm@linux-foundation.org 	inode->i_uid = sbinfo->uid;
2620680d794bSakpm@linux-foundation.org 	inode->i_gid = sbinfo->gid;
26211da177e4SLinus Torvalds 	root = d_alloc_root(inode);
26221da177e4SLinus Torvalds 	if (!root)
26231da177e4SLinus Torvalds 		goto failed_iput;
26241da177e4SLinus Torvalds 	sb->s_root = root;
26251da177e4SLinus Torvalds 	return 0;
26261da177e4SLinus Torvalds 
26271da177e4SLinus Torvalds failed_iput:
26281da177e4SLinus Torvalds 	iput(inode);
26291da177e4SLinus Torvalds failed:
26301da177e4SLinus Torvalds 	shmem_put_super(sb);
26311da177e4SLinus Torvalds 	return err;
26321da177e4SLinus Torvalds }
26331da177e4SLinus Torvalds 
2634fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep;
26351da177e4SLinus Torvalds 
26361da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb)
26371da177e4SLinus Torvalds {
26381da177e4SLinus Torvalds 	struct shmem_inode_info *p;
2639e94b1766SChristoph Lameter 	p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
26401da177e4SLinus Torvalds 	if (!p)
26411da177e4SLinus Torvalds 		return NULL;
26421da177e4SLinus Torvalds 	return &p->vfs_inode;
26431da177e4SLinus Torvalds }
26441da177e4SLinus Torvalds 
2645fa0d7e3dSNick Piggin static void shmem_i_callback(struct rcu_head *head)
2646fa0d7e3dSNick Piggin {
2647fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
2648fa0d7e3dSNick Piggin 	INIT_LIST_HEAD(&inode->i_dentry);
2649fa0d7e3dSNick Piggin 	kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2650fa0d7e3dSNick Piggin }
2651fa0d7e3dSNick Piggin 
26521da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode)
26531da177e4SLinus Torvalds {
26541da177e4SLinus Torvalds 	if ((inode->i_mode & S_IFMT) == S_IFREG) {
26551da177e4SLinus Torvalds 		/* only struct inode is valid if it's an inline symlink */
26561da177e4SLinus Torvalds 		mpol_free_shared_policy(&SHMEM_I(inode)->policy);
26571da177e4SLinus Torvalds 	}
2658fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, shmem_i_callback);
26591da177e4SLinus Torvalds }
26601da177e4SLinus Torvalds 
266151cc5068SAlexey Dobriyan static void init_once(void *foo)
26621da177e4SLinus Torvalds {
26631da177e4SLinus Torvalds 	struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds 	inode_init_once(&p->vfs_inode);
26661da177e4SLinus Torvalds }
26671da177e4SLinus Torvalds 
26681da177e4SLinus Torvalds static int init_inodecache(void)
26691da177e4SLinus Torvalds {
26701da177e4SLinus Torvalds 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
26711da177e4SLinus Torvalds 				sizeof(struct shmem_inode_info),
2672040b5c6fSAlexey Dobriyan 				0, SLAB_PANIC, init_once);
26731da177e4SLinus Torvalds 	return 0;
26741da177e4SLinus Torvalds }
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds static void destroy_inodecache(void)
26771da177e4SLinus Torvalds {
26781a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(shmem_inode_cachep);
26791da177e4SLinus Torvalds }
26801da177e4SLinus Torvalds 
2681f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = {
26821da177e4SLinus Torvalds 	.writepage	= shmem_writepage,
268376719325SKen Chen 	.set_page_dirty	= __set_page_dirty_no_writeback,
26841da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
2685ae976416SHugh Dickins 	.readpage	= shmem_readpage,
2686800d15a5SNick Piggin 	.write_begin	= shmem_write_begin,
2687800d15a5SNick Piggin 	.write_end	= shmem_write_end,
26881da177e4SLinus Torvalds #endif
2689304dbdb7SLee Schermerhorn 	.migratepage	= migrate_page,
2690aa261f54SAndi Kleen 	.error_remove_page = generic_error_remove_page,
26911da177e4SLinus Torvalds };
26921da177e4SLinus Torvalds 
269315ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = {
26941da177e4SLinus Torvalds 	.mmap		= shmem_mmap,
26951da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
26961da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
2697bcd78e49SHugh Dickins 	.read		= do_sync_read,
26985402b976SHugh Dickins 	.write		= do_sync_write,
2699bcd78e49SHugh Dickins 	.aio_read	= shmem_file_aio_read,
27005402b976SHugh Dickins 	.aio_write	= generic_file_aio_write,
27011b061d92SChristoph Hellwig 	.fsync		= noop_fsync,
2702ae976416SHugh Dickins 	.splice_read	= generic_file_splice_read,
2703ae976416SHugh Dickins 	.splice_write	= generic_file_splice_write,
27041da177e4SLinus Torvalds #endif
27051da177e4SLinus Torvalds };
27061da177e4SLinus Torvalds 
270792e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = {
27081da177e4SLinus Torvalds 	.setattr	= shmem_notify_change,
2709f6b3ec23SBadari Pulavarty 	.truncate_range	= shmem_truncate_range,
2710*b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2711*b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2712*b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2713*b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2714*b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2715*b09e0fa4SEric Paris #endif
271639f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
27171c7c474cSChristoph Hellwig 	.check_acl	= generic_check_acl,
271839f0247dSAndreas Gruenbacher #endif
271939f0247dSAndreas Gruenbacher 
27201da177e4SLinus Torvalds };
27211da177e4SLinus Torvalds 
272292e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = {
27231da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
27241da177e4SLinus Torvalds 	.create		= shmem_create,
27251da177e4SLinus Torvalds 	.lookup		= simple_lookup,
27261da177e4SLinus Torvalds 	.link		= shmem_link,
27271da177e4SLinus Torvalds 	.unlink		= shmem_unlink,
27281da177e4SLinus Torvalds 	.symlink	= shmem_symlink,
27291da177e4SLinus Torvalds 	.mkdir		= shmem_mkdir,
27301da177e4SLinus Torvalds 	.rmdir		= shmem_rmdir,
27311da177e4SLinus Torvalds 	.mknod		= shmem_mknod,
27321da177e4SLinus Torvalds 	.rename		= shmem_rename,
27331da177e4SLinus Torvalds #endif
2734*b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2735*b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2736*b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2737*b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2738*b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2739*b09e0fa4SEric Paris #endif
274039f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
274139f0247dSAndreas Gruenbacher 	.setattr	= shmem_notify_change,
27421c7c474cSChristoph Hellwig 	.check_acl	= generic_check_acl,
274339f0247dSAndreas Gruenbacher #endif
274439f0247dSAndreas Gruenbacher };
274539f0247dSAndreas Gruenbacher 
274692e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = {
2747*b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR
2748*b09e0fa4SEric Paris 	.setxattr	= shmem_setxattr,
2749*b09e0fa4SEric Paris 	.getxattr	= shmem_getxattr,
2750*b09e0fa4SEric Paris 	.listxattr	= shmem_listxattr,
2751*b09e0fa4SEric Paris 	.removexattr	= shmem_removexattr,
2752*b09e0fa4SEric Paris #endif
275339f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
275439f0247dSAndreas Gruenbacher 	.setattr	= shmem_notify_change,
27551c7c474cSChristoph Hellwig 	.check_acl	= generic_check_acl,
275639f0247dSAndreas Gruenbacher #endif
27571da177e4SLinus Torvalds };
27581da177e4SLinus Torvalds 
2759759b9775SHugh Dickins static const struct super_operations shmem_ops = {
27601da177e4SLinus Torvalds 	.alloc_inode	= shmem_alloc_inode,
27611da177e4SLinus Torvalds 	.destroy_inode	= shmem_destroy_inode,
27621da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
27631da177e4SLinus Torvalds 	.statfs		= shmem_statfs,
27641da177e4SLinus Torvalds 	.remount_fs	= shmem_remount_fs,
2765680d794bSakpm@linux-foundation.org 	.show_options	= shmem_show_options,
27661da177e4SLinus Torvalds #endif
27671f895f75SAl Viro 	.evict_inode	= shmem_evict_inode,
27681da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
27691da177e4SLinus Torvalds 	.put_super	= shmem_put_super,
27701da177e4SLinus Torvalds };
27711da177e4SLinus Torvalds 
2772f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = {
277354cb8821SNick Piggin 	.fault		= shmem_fault,
27741da177e4SLinus Torvalds #ifdef CONFIG_NUMA
27751da177e4SLinus Torvalds 	.set_policy     = shmem_set_policy,
27761da177e4SLinus Torvalds 	.get_policy     = shmem_get_policy,
27771da177e4SLinus Torvalds #endif
27781da177e4SLinus Torvalds };
27791da177e4SLinus Torvalds 
27801da177e4SLinus Torvalds 
27813c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type,
27823c26ff6eSAl Viro 	int flags, const char *dev_name, void *data)
27831da177e4SLinus Torvalds {
27843c26ff6eSAl Viro 	return mount_nodev(fs_type, flags, data, shmem_fill_super);
27851da177e4SLinus Torvalds }
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds static struct file_system_type tmpfs_fs_type = {
27881da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
27891da177e4SLinus Torvalds 	.name		= "tmpfs",
27903c26ff6eSAl Viro 	.mount		= shmem_mount,
27911da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
27921da177e4SLinus Torvalds };
27931da177e4SLinus Torvalds 
27942b2af54aSKay Sievers int __init init_tmpfs(void)
27951da177e4SLinus Torvalds {
27961da177e4SLinus Torvalds 	int error;
27971da177e4SLinus Torvalds 
2798e0bf68ddSPeter Zijlstra 	error = bdi_init(&shmem_backing_dev_info);
2799e0bf68ddSPeter Zijlstra 	if (error)
2800e0bf68ddSPeter Zijlstra 		goto out4;
2801e0bf68ddSPeter Zijlstra 
28021da177e4SLinus Torvalds 	error = init_inodecache();
28031da177e4SLinus Torvalds 	if (error)
28041da177e4SLinus Torvalds 		goto out3;
28051da177e4SLinus Torvalds 
28061da177e4SLinus Torvalds 	error = register_filesystem(&tmpfs_fs_type);
28071da177e4SLinus Torvalds 	if (error) {
28081da177e4SLinus Torvalds 		printk(KERN_ERR "Could not register tmpfs\n");
28091da177e4SLinus Torvalds 		goto out2;
28101da177e4SLinus Torvalds 	}
281195dc112aSGreg Kroah-Hartman 
28121f5ce9e9STrond Myklebust 	shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
28131da177e4SLinus Torvalds 				tmpfs_fs_type.name, NULL);
28141da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt)) {
28151da177e4SLinus Torvalds 		error = PTR_ERR(shm_mnt);
28161da177e4SLinus Torvalds 		printk(KERN_ERR "Could not kern_mount tmpfs\n");
28171da177e4SLinus Torvalds 		goto out1;
28181da177e4SLinus Torvalds 	}
28191da177e4SLinus Torvalds 	return 0;
28201da177e4SLinus Torvalds 
28211da177e4SLinus Torvalds out1:
28221da177e4SLinus Torvalds 	unregister_filesystem(&tmpfs_fs_type);
28231da177e4SLinus Torvalds out2:
28241da177e4SLinus Torvalds 	destroy_inodecache();
28251da177e4SLinus Torvalds out3:
2826e0bf68ddSPeter Zijlstra 	bdi_destroy(&shmem_backing_dev_info);
2827e0bf68ddSPeter Zijlstra out4:
28281da177e4SLinus Torvalds 	shm_mnt = ERR_PTR(error);
28291da177e4SLinus Torvalds 	return error;
28301da177e4SLinus Torvalds }
2831853ac43aSMatt Mackall 
283287946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR
283387946a72SDaisuke Nishimura /**
283487946a72SDaisuke Nishimura  * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
283587946a72SDaisuke Nishimura  * @inode: the inode to be searched
283687946a72SDaisuke Nishimura  * @pgoff: the offset to be searched
283787946a72SDaisuke Nishimura  * @pagep: the pointer for the found page to be stored
283887946a72SDaisuke Nishimura  * @ent: the pointer for the found swap entry to be stored
283987946a72SDaisuke Nishimura  *
284087946a72SDaisuke Nishimura  * If a page is found, refcount of it is incremented. Callers should handle
284187946a72SDaisuke Nishimura  * these refcount.
284287946a72SDaisuke Nishimura  */
284387946a72SDaisuke Nishimura void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
284487946a72SDaisuke Nishimura 					struct page **pagep, swp_entry_t *ent)
284587946a72SDaisuke Nishimura {
284687946a72SDaisuke Nishimura 	swp_entry_t entry = { .val = 0 }, *ptr;
284787946a72SDaisuke Nishimura 	struct page *page = NULL;
284887946a72SDaisuke Nishimura 	struct shmem_inode_info *info = SHMEM_I(inode);
284987946a72SDaisuke Nishimura 
285087946a72SDaisuke Nishimura 	if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
285187946a72SDaisuke Nishimura 		goto out;
285287946a72SDaisuke Nishimura 
285387946a72SDaisuke Nishimura 	spin_lock(&info->lock);
285487946a72SDaisuke Nishimura 	ptr = shmem_swp_entry(info, pgoff, NULL);
285587946a72SDaisuke Nishimura #ifdef CONFIG_SWAP
285687946a72SDaisuke Nishimura 	if (ptr && ptr->val) {
285787946a72SDaisuke Nishimura 		entry.val = ptr->val;
285887946a72SDaisuke Nishimura 		page = find_get_page(&swapper_space, entry.val);
285987946a72SDaisuke Nishimura 	} else
286087946a72SDaisuke Nishimura #endif
286187946a72SDaisuke Nishimura 		page = find_get_page(inode->i_mapping, pgoff);
286287946a72SDaisuke Nishimura 	if (ptr)
286387946a72SDaisuke Nishimura 		shmem_swp_unmap(ptr);
286487946a72SDaisuke Nishimura 	spin_unlock(&info->lock);
286587946a72SDaisuke Nishimura out:
286687946a72SDaisuke Nishimura 	*pagep = page;
286787946a72SDaisuke Nishimura 	*ent = entry;
286887946a72SDaisuke Nishimura }
286987946a72SDaisuke Nishimura #endif
287087946a72SDaisuke Nishimura 
2871853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */
2872853ac43aSMatt Mackall 
2873853ac43aSMatt Mackall /*
2874853ac43aSMatt Mackall  * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2875853ac43aSMatt Mackall  *
2876853ac43aSMatt Mackall  * This is intended for small system where the benefits of the full
2877853ac43aSMatt Mackall  * shmem code (swap-backed and resource-limited) are outweighed by
2878853ac43aSMatt Mackall  * their complexity. On systems without swap this code should be
2879853ac43aSMatt Mackall  * effectively equivalent, but much lighter weight.
2880853ac43aSMatt Mackall  */
2881853ac43aSMatt Mackall 
2882853ac43aSMatt Mackall #include <linux/ramfs.h>
2883853ac43aSMatt Mackall 
2884853ac43aSMatt Mackall static struct file_system_type tmpfs_fs_type = {
2885853ac43aSMatt Mackall 	.name		= "tmpfs",
28863c26ff6eSAl Viro 	.mount		= ramfs_mount,
2887853ac43aSMatt Mackall 	.kill_sb	= kill_litter_super,
2888853ac43aSMatt Mackall };
2889853ac43aSMatt Mackall 
28902b2af54aSKay Sievers int __init init_tmpfs(void)
2891853ac43aSMatt Mackall {
2892853ac43aSMatt Mackall 	BUG_ON(register_filesystem(&tmpfs_fs_type) != 0);
2893853ac43aSMatt Mackall 
2894853ac43aSMatt Mackall 	shm_mnt = kern_mount(&tmpfs_fs_type);
2895853ac43aSMatt Mackall 	BUG_ON(IS_ERR(shm_mnt));
2896853ac43aSMatt Mackall 
2897853ac43aSMatt Mackall 	return 0;
2898853ac43aSMatt Mackall }
2899853ac43aSMatt Mackall 
2900853ac43aSMatt Mackall int shmem_unuse(swp_entry_t entry, struct page *page)
2901853ac43aSMatt Mackall {
2902853ac43aSMatt Mackall 	return 0;
2903853ac43aSMatt Mackall }
2904853ac43aSMatt Mackall 
29053f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user)
29063f96b79aSHugh Dickins {
29073f96b79aSHugh Dickins 	return 0;
29083f96b79aSHugh Dickins }
29093f96b79aSHugh Dickins 
291087946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR
291187946a72SDaisuke Nishimura /**
291287946a72SDaisuke Nishimura  * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
291387946a72SDaisuke Nishimura  * @inode: the inode to be searched
291487946a72SDaisuke Nishimura  * @pgoff: the offset to be searched
291587946a72SDaisuke Nishimura  * @pagep: the pointer for the found page to be stored
291687946a72SDaisuke Nishimura  * @ent: the pointer for the found swap entry to be stored
291787946a72SDaisuke Nishimura  *
291887946a72SDaisuke Nishimura  * If a page is found, refcount of it is incremented. Callers should handle
291987946a72SDaisuke Nishimura  * these refcount.
292087946a72SDaisuke Nishimura  */
292187946a72SDaisuke Nishimura void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
292287946a72SDaisuke Nishimura 					struct page **pagep, swp_entry_t *ent)
292387946a72SDaisuke Nishimura {
292487946a72SDaisuke Nishimura 	struct page *page = NULL;
292587946a72SDaisuke Nishimura 
292687946a72SDaisuke Nishimura 	if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
292787946a72SDaisuke Nishimura 		goto out;
292887946a72SDaisuke Nishimura 	page = find_get_page(inode->i_mapping, pgoff);
292987946a72SDaisuke Nishimura out:
293087946a72SDaisuke Nishimura 	*pagep = page;
293187946a72SDaisuke Nishimura 	*ent = (swp_entry_t){ .val = 0 };
293287946a72SDaisuke Nishimura }
293387946a72SDaisuke Nishimura #endif
293487946a72SDaisuke Nishimura 
2935853ac43aSMatt Mackall #define shmem_vm_ops				generic_file_vm_ops
29360b0a0806SHugh Dickins #define shmem_file_operations			ramfs_file_operations
2937454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags)	ramfs_get_inode(sb, dir, mode, dev)
29380b0a0806SHugh Dickins #define shmem_acct_size(flags, size)		0
29390b0a0806SHugh Dickins #define shmem_unacct_size(flags, size)		do {} while (0)
2940caefba17SHugh Dickins #define SHMEM_MAX_BYTES				MAX_LFS_FILESIZE
2941853ac43aSMatt Mackall 
2942853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */
2943853ac43aSMatt Mackall 
2944853ac43aSMatt Mackall /* common code */
29451da177e4SLinus Torvalds 
294646711810SRandy Dunlap /**
29471da177e4SLinus Torvalds  * shmem_file_setup - get an unlinked file living in tmpfs
29481da177e4SLinus Torvalds  * @name: name for dentry (to be seen in /proc/<pid>/maps
29491da177e4SLinus Torvalds  * @size: size to be set for the file
29500b0a0806SHugh Dickins  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
29511da177e4SLinus Torvalds  */
2952168f5ac6SSergei Trofimovich struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
29531da177e4SLinus Torvalds {
29541da177e4SLinus Torvalds 	int error;
29551da177e4SLinus Torvalds 	struct file *file;
29561da177e4SLinus Torvalds 	struct inode *inode;
29572c48b9c4SAl Viro 	struct path path;
29582c48b9c4SAl Viro 	struct dentry *root;
29591da177e4SLinus Torvalds 	struct qstr this;
29601da177e4SLinus Torvalds 
29611da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt))
29621da177e4SLinus Torvalds 		return (void *)shm_mnt;
29631da177e4SLinus Torvalds 
29641da177e4SLinus Torvalds 	if (size < 0 || size > SHMEM_MAX_BYTES)
29651da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
29661da177e4SLinus Torvalds 
29671da177e4SLinus Torvalds 	if (shmem_acct_size(flags, size))
29681da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
29691da177e4SLinus Torvalds 
29701da177e4SLinus Torvalds 	error = -ENOMEM;
29711da177e4SLinus Torvalds 	this.name = name;
29721da177e4SLinus Torvalds 	this.len = strlen(name);
29731da177e4SLinus Torvalds 	this.hash = 0; /* will go */
29741da177e4SLinus Torvalds 	root = shm_mnt->mnt_root;
29752c48b9c4SAl Viro 	path.dentry = d_alloc(root, &this);
29762c48b9c4SAl Viro 	if (!path.dentry)
29771da177e4SLinus Torvalds 		goto put_memory;
29782c48b9c4SAl Viro 	path.mnt = mntget(shm_mnt);
29791da177e4SLinus Torvalds 
29801da177e4SLinus Torvalds 	error = -ENOSPC;
2981454abafeSDmitry Monakhov 	inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
29821da177e4SLinus Torvalds 	if (!inode)
29834b42af81SAl Viro 		goto put_dentry;
29841da177e4SLinus Torvalds 
29852c48b9c4SAl Viro 	d_instantiate(path.dentry, inode);
29861da177e4SLinus Torvalds 	inode->i_size = size;
29871da177e4SLinus Torvalds 	inode->i_nlink = 0;	/* It is unlinked */
2988853ac43aSMatt Mackall #ifndef CONFIG_MMU
2989853ac43aSMatt Mackall 	error = ramfs_nommu_expand_for_mapping(inode, size);
2990853ac43aSMatt Mackall 	if (error)
29914b42af81SAl Viro 		goto put_dentry;
2992853ac43aSMatt Mackall #endif
29934b42af81SAl Viro 
29944b42af81SAl Viro 	error = -ENFILE;
29952c48b9c4SAl Viro 	file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
29964b42af81SAl Viro 		  &shmem_file_operations);
29974b42af81SAl Viro 	if (!file)
29984b42af81SAl Viro 		goto put_dentry;
29994b42af81SAl Viro 
30001da177e4SLinus Torvalds 	return file;
30011da177e4SLinus Torvalds 
30021da177e4SLinus Torvalds put_dentry:
30032c48b9c4SAl Viro 	path_put(&path);
30041da177e4SLinus Torvalds put_memory:
30051da177e4SLinus Torvalds 	shmem_unacct_size(flags, size);
30061da177e4SLinus Torvalds 	return ERR_PTR(error);
30071da177e4SLinus Torvalds }
3008395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup);
30091da177e4SLinus Torvalds 
301046711810SRandy Dunlap /**
30111da177e4SLinus Torvalds  * shmem_zero_setup - setup a shared anonymous mapping
30121da177e4SLinus Torvalds  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
30131da177e4SLinus Torvalds  */
30141da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma)
30151da177e4SLinus Torvalds {
30161da177e4SLinus Torvalds 	struct file *file;
30171da177e4SLinus Torvalds 	loff_t size = vma->vm_end - vma->vm_start;
30181da177e4SLinus Torvalds 
30191da177e4SLinus Torvalds 	file = shmem_file_setup("dev/zero", size, vma->vm_flags);
30201da177e4SLinus Torvalds 	if (IS_ERR(file))
30211da177e4SLinus Torvalds 		return PTR_ERR(file);
30221da177e4SLinus Torvalds 
30231da177e4SLinus Torvalds 	if (vma->vm_file)
30241da177e4SLinus Torvalds 		fput(vma->vm_file);
30251da177e4SLinus Torvalds 	vma->vm_file = file;
30261da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
3027bee4c36aSHugh Dickins 	vma->vm_flags |= VM_CAN_NONLINEAR;
30281da177e4SLinus Torvalds 	return 0;
30291da177e4SLinus Torvalds }
3030