xref: /openbmc/linux/mm/shmem.c (revision fa0d7e3de6d6fc5004ad9dea0dd6b286af8f03e9)
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 
1021da177e4SLinus Torvalds /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
1031da177e4SLinus Torvalds enum sgp_type {
1041da177e4SLinus Torvalds 	SGP_READ,	/* don't exceed i_size, don't allocate page */
1051da177e4SLinus Torvalds 	SGP_CACHE,	/* don't exceed i_size, may allocate page */
106a0ee5ec5SHugh Dickins 	SGP_DIRTY,	/* like SGP_CACHE, but set new page dirty */
1071da177e4SLinus Torvalds 	SGP_WRITE,	/* may exceed i_size, may allocate page */
1081da177e4SLinus Torvalds };
1091da177e4SLinus Torvalds 
110b76db735SAndrew Morton #ifdef CONFIG_TMPFS
111680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void)
112680d794bSakpm@linux-foundation.org {
113680d794bSakpm@linux-foundation.org 	return totalram_pages / 2;
114680d794bSakpm@linux-foundation.org }
115680d794bSakpm@linux-foundation.org 
116680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void)
117680d794bSakpm@linux-foundation.org {
118680d794bSakpm@linux-foundation.org 	return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
119680d794bSakpm@linux-foundation.org }
120b76db735SAndrew Morton #endif
121680d794bSakpm@linux-foundation.org 
1221da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx,
1231da177e4SLinus Torvalds 			 struct page **pagep, enum sgp_type sgp, int *type);
1241da177e4SLinus Torvalds 
1256daa0e28SAl Viro static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	/*
1281da177e4SLinus Torvalds 	 * The above definition of ENTRIES_PER_PAGE, and the use of
1291da177e4SLinus Torvalds 	 * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
1301da177e4SLinus Torvalds 	 * might be reconsidered if it ever diverges from PAGE_SIZE.
131769848c0SMel Gorman 	 *
132e12ba74dSMel Gorman 	 * Mobility flags are masked out as swap vectors cannot move
1331da177e4SLinus Torvalds 	 */
134e12ba74dSMel Gorman 	return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
135769848c0SMel Gorman 				PAGE_CACHE_SHIFT-PAGE_SHIFT);
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds static inline void shmem_dir_free(struct page *page)
1391da177e4SLinus Torvalds {
1401da177e4SLinus Torvalds 	__free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds static struct page **shmem_dir_map(struct page *page)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	return (struct page **)kmap_atomic(page, KM_USER0);
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds static inline void shmem_dir_unmap(struct page **dir)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	kunmap_atomic(dir, KM_USER0);
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds static swp_entry_t *shmem_swp_map(struct page *page)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds 	return (swp_entry_t *)kmap_atomic(page, KM_USER1);
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds static inline void shmem_swp_balance_unmap(void)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	/*
1611da177e4SLinus Torvalds 	 * When passing a pointer to an i_direct entry, to code which
1621da177e4SLinus Torvalds 	 * also handles indirect entries and so will shmem_swp_unmap,
1631da177e4SLinus Torvalds 	 * we must arrange for the preempt count to remain in balance.
1641da177e4SLinus Torvalds 	 * What kmap_atomic of a lowmem page does depends on config
1651da177e4SLinus Torvalds 	 * and architecture, so pretend to kmap_atomic some lowmem page.
1661da177e4SLinus Torvalds 	 */
1671da177e4SLinus Torvalds 	(void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds static inline void shmem_swp_unmap(swp_entry_t *entry)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	kunmap_atomic(entry, KM_USER1);
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	return sb->s_fs_info;
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds /*
1811da177e4SLinus Torvalds  * shmem_file_setup pre-accounts the whole fixed size of a VM object,
1821da177e4SLinus Torvalds  * for shared memory and for shared anonymous (/dev/zero) mappings
1831da177e4SLinus Torvalds  * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
1841da177e4SLinus Torvalds  * consistent with the pre-accounting of private mappings ...
1851da177e4SLinus Torvalds  */
1861da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size)
1871da177e4SLinus Torvalds {
1880b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
1890b0a0806SHugh Dickins 		0 : security_vm_enough_memory_kern(VM_ACCT(size));
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size)
1931da177e4SLinus Torvalds {
1940b0a0806SHugh Dickins 	if (!(flags & VM_NORESERVE))
1951da177e4SLinus Torvalds 		vm_unacct_memory(VM_ACCT(size));
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds /*
1991da177e4SLinus Torvalds  * ... whereas tmpfs objects are accounted incrementally as
2001da177e4SLinus Torvalds  * pages are allocated, in order to allow huge sparse files.
2011da177e4SLinus Torvalds  * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
2021da177e4SLinus Torvalds  * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
2031da177e4SLinus Torvalds  */
2041da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags)
2051da177e4SLinus Torvalds {
2060b0a0806SHugh Dickins 	return (flags & VM_NORESERVE) ?
2070b0a0806SHugh Dickins 		security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages)
2111da177e4SLinus Torvalds {
2120b0a0806SHugh Dickins 	if (flags & VM_NORESERVE)
2131da177e4SLinus Torvalds 		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
216759b9775SHugh Dickins static const struct super_operations shmem_ops;
217f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops;
21815ad7cdcSHelge Deller static const struct file_operations shmem_file_operations;
21992e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations;
22092e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations;
22192e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations;
222f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops;
2231da177e4SLinus Torvalds 
2246c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info  __read_mostly = {
2251da177e4SLinus Torvalds 	.ra_pages	= 0,	/* No readahead */
2264f98a2feSRik van Riel 	.capabilities	= BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
2271da177e4SLinus Torvalds 	.unplug_io_fn	= default_unplug_io_fn,
2281da177e4SLinus Torvalds };
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist);
231cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex);
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static void shmem_free_blocks(struct inode *inode, long pages)
2341da177e4SLinus Torvalds {
2351da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
2360edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
2377e496299STim Chen 		percpu_counter_add(&sbinfo->used_blocks, -pages);
2387e496299STim Chen 		spin_lock(&inode->i_lock);
2391da177e4SLinus Torvalds 		inode->i_blocks -= pages*BLOCKS_PER_PAGE;
2407e496299STim Chen 		spin_unlock(&inode->i_lock);
2411da177e4SLinus Torvalds 	}
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
2445b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb)
2455b04c689SPavel Emelyanov {
2465b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2475b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2485b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2495b04c689SPavel Emelyanov 		if (!sbinfo->free_inodes) {
2505b04c689SPavel Emelyanov 			spin_unlock(&sbinfo->stat_lock);
2515b04c689SPavel Emelyanov 			return -ENOSPC;
2525b04c689SPavel Emelyanov 		}
2535b04c689SPavel Emelyanov 		sbinfo->free_inodes--;
2545b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2555b04c689SPavel Emelyanov 	}
2565b04c689SPavel Emelyanov 	return 0;
2575b04c689SPavel Emelyanov }
2585b04c689SPavel Emelyanov 
2595b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb)
2605b04c689SPavel Emelyanov {
2615b04c689SPavel Emelyanov 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2625b04c689SPavel Emelyanov 	if (sbinfo->max_inodes) {
2635b04c689SPavel Emelyanov 		spin_lock(&sbinfo->stat_lock);
2645b04c689SPavel Emelyanov 		sbinfo->free_inodes++;
2655b04c689SPavel Emelyanov 		spin_unlock(&sbinfo->stat_lock);
2665b04c689SPavel Emelyanov 	}
2675b04c689SPavel Emelyanov }
2685b04c689SPavel Emelyanov 
26946711810SRandy Dunlap /**
2701da177e4SLinus Torvalds  * shmem_recalc_inode - recalculate the size of an inode
2711da177e4SLinus Torvalds  * @inode: inode to recalc
2721da177e4SLinus Torvalds  *
2731da177e4SLinus Torvalds  * We have to calculate the free blocks since the mm can drop
2741da177e4SLinus Torvalds  * undirtied hole pages behind our back.
2751da177e4SLinus Torvalds  *
2761da177e4SLinus Torvalds  * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
2771da177e4SLinus Torvalds  * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
2781da177e4SLinus Torvalds  *
2791da177e4SLinus Torvalds  * It has to be called with the spinlock held.
2801da177e4SLinus Torvalds  */
2811da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode)
2821da177e4SLinus Torvalds {
2831da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
2841da177e4SLinus Torvalds 	long freed;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
2871da177e4SLinus Torvalds 	if (freed > 0) {
2881da177e4SLinus Torvalds 		info->alloced -= freed;
2891da177e4SLinus Torvalds 		shmem_unacct_blocks(info->flags, freed);
2901da177e4SLinus Torvalds 		shmem_free_blocks(inode, freed);
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
29446711810SRandy Dunlap /**
2951da177e4SLinus Torvalds  * shmem_swp_entry - find the swap vector position in the info structure
2961da177e4SLinus Torvalds  * @info:  info structure for the inode
2971da177e4SLinus Torvalds  * @index: index of the page to find
2981da177e4SLinus Torvalds  * @page:  optional page to add to the structure. Has to be preset to
2991da177e4SLinus Torvalds  *         all zeros
3001da177e4SLinus Torvalds  *
3011da177e4SLinus Torvalds  * If there is no space allocated yet it will return NULL when
3021da177e4SLinus Torvalds  * page is NULL, else it will use the page for the needed block,
3031da177e4SLinus Torvalds  * setting it to NULL on return to indicate that it has been used.
3041da177e4SLinus Torvalds  *
3051da177e4SLinus Torvalds  * The swap vector is organized the following way:
3061da177e4SLinus Torvalds  *
3071da177e4SLinus Torvalds  * There are SHMEM_NR_DIRECT entries directly stored in the
3081da177e4SLinus Torvalds  * shmem_inode_info structure. So small files do not need an addional
3091da177e4SLinus Torvalds  * allocation.
3101da177e4SLinus Torvalds  *
3111da177e4SLinus Torvalds  * For pages with index > SHMEM_NR_DIRECT there is the pointer
3121da177e4SLinus Torvalds  * i_indirect which points to a page which holds in the first half
3131da177e4SLinus Torvalds  * doubly indirect blocks, in the second half triple indirect blocks:
3141da177e4SLinus Torvalds  *
3151da177e4SLinus Torvalds  * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
3161da177e4SLinus Torvalds  * following layout (for SHMEM_NR_DIRECT == 16):
3171da177e4SLinus Torvalds  *
3181da177e4SLinus Torvalds  * i_indirect -> dir --> 16-19
3191da177e4SLinus Torvalds  * 	      |	     +-> 20-23
3201da177e4SLinus Torvalds  * 	      |
3211da177e4SLinus Torvalds  * 	      +-->dir2 --> 24-27
3221da177e4SLinus Torvalds  * 	      |	       +-> 28-31
3231da177e4SLinus Torvalds  * 	      |	       +-> 32-35
3241da177e4SLinus Torvalds  * 	      |	       +-> 36-39
3251da177e4SLinus Torvalds  * 	      |
3261da177e4SLinus Torvalds  * 	      +-->dir3 --> 40-43
3271da177e4SLinus Torvalds  * 	       	       +-> 44-47
3281da177e4SLinus Torvalds  * 	      	       +-> 48-51
3291da177e4SLinus Torvalds  * 	      	       +-> 52-55
3301da177e4SLinus Torvalds  */
3311da177e4SLinus Torvalds static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	unsigned long offset;
3341da177e4SLinus Torvalds 	struct page **dir;
3351da177e4SLinus Torvalds 	struct page *subdir;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	if (index < SHMEM_NR_DIRECT) {
3381da177e4SLinus Torvalds 		shmem_swp_balance_unmap();
3391da177e4SLinus Torvalds 		return info->i_direct+index;
3401da177e4SLinus Torvalds 	}
3411da177e4SLinus Torvalds 	if (!info->i_indirect) {
3421da177e4SLinus Torvalds 		if (page) {
3431da177e4SLinus Torvalds 			info->i_indirect = *page;
3441da177e4SLinus Torvalds 			*page = NULL;
3451da177e4SLinus Torvalds 		}
3461da177e4SLinus Torvalds 		return NULL;			/* need another page */
3471da177e4SLinus Torvalds 	}
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	index -= SHMEM_NR_DIRECT;
3501da177e4SLinus Torvalds 	offset = index % ENTRIES_PER_PAGE;
3511da177e4SLinus Torvalds 	index /= ENTRIES_PER_PAGE;
3521da177e4SLinus Torvalds 	dir = shmem_dir_map(info->i_indirect);
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	if (index >= ENTRIES_PER_PAGE/2) {
3551da177e4SLinus Torvalds 		index -= ENTRIES_PER_PAGE/2;
3561da177e4SLinus Torvalds 		dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
3571da177e4SLinus Torvalds 		index %= ENTRIES_PER_PAGE;
3581da177e4SLinus Torvalds 		subdir = *dir;
3591da177e4SLinus Torvalds 		if (!subdir) {
3601da177e4SLinus Torvalds 			if (page) {
3611da177e4SLinus Torvalds 				*dir = *page;
3621da177e4SLinus Torvalds 				*page = NULL;
3631da177e4SLinus Torvalds 			}
3641da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
3651da177e4SLinus Torvalds 			return NULL;		/* need another page */
3661da177e4SLinus Torvalds 		}
3671da177e4SLinus Torvalds 		shmem_dir_unmap(dir);
3681da177e4SLinus Torvalds 		dir = shmem_dir_map(subdir);
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	dir += index;
3721da177e4SLinus Torvalds 	subdir = *dir;
3731da177e4SLinus Torvalds 	if (!subdir) {
3741da177e4SLinus Torvalds 		if (!page || !(subdir = *page)) {
3751da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
3761da177e4SLinus Torvalds 			return NULL;		/* need a page */
3771da177e4SLinus Torvalds 		}
3781da177e4SLinus Torvalds 		*dir = subdir;
3791da177e4SLinus Torvalds 		*page = NULL;
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 	shmem_dir_unmap(dir);
3821da177e4SLinus Torvalds 	return shmem_swp_map(subdir) + offset;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
3861da177e4SLinus Torvalds {
3871da177e4SLinus Torvalds 	long incdec = value? 1: -1;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	entry->val = value;
3901da177e4SLinus Torvalds 	info->swapped += incdec;
3914c21e2f2SHugh Dickins 	if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
3924c21e2f2SHugh Dickins 		struct page *page = kmap_atomic_to_page(entry);
3934c21e2f2SHugh Dickins 		set_page_private(page, page_private(page) + incdec);
3944c21e2f2SHugh Dickins 	}
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
39746711810SRandy Dunlap /**
3981da177e4SLinus Torvalds  * shmem_swp_alloc - get the position of the swap entry for the page.
3991da177e4SLinus Torvalds  * @info:	info structure for the inode
4001da177e4SLinus Torvalds  * @index:	index of the page to find
4011da177e4SLinus Torvalds  * @sgp:	check and recheck i_size? skip allocation?
40246711810SRandy Dunlap  *
40346711810SRandy Dunlap  * If the entry does not exist, allocate it.
4041da177e4SLinus Torvalds  */
4051da177e4SLinus Torvalds static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	struct inode *inode = &info->vfs_inode;
4081da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
4091da177e4SLinus Torvalds 	struct page *page = NULL;
4101da177e4SLinus Torvalds 	swp_entry_t *entry;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	if (sgp != SGP_WRITE &&
4131da177e4SLinus Torvalds 	    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
4141da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	while (!(entry = shmem_swp_entry(info, index, &page))) {
4171da177e4SLinus Torvalds 		if (sgp == SGP_READ)
4181da177e4SLinus Torvalds 			return shmem_swp_map(ZERO_PAGE(0));
4191da177e4SLinus Torvalds 		/*
4207e496299STim Chen 		 * Test used_blocks against 1 less max_blocks, since we have 1 data
4211da177e4SLinus Torvalds 		 * page (and perhaps indirect index pages) yet to allocate:
4221da177e4SLinus Torvalds 		 * a waste to allocate index if we cannot allocate data.
4231da177e4SLinus Torvalds 		 */
4240edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
4257e496299STim Chen 			if (percpu_counter_compare(&sbinfo->used_blocks, (sbinfo->max_blocks - 1)) > 0)
4261da177e4SLinus Torvalds 				return ERR_PTR(-ENOSPC);
4277e496299STim Chen 			percpu_counter_inc(&sbinfo->used_blocks);
4287e496299STim Chen 			spin_lock(&inode->i_lock);
4291da177e4SLinus Torvalds 			inode->i_blocks += BLOCKS_PER_PAGE;
4307e496299STim Chen 			spin_unlock(&inode->i_lock);
4311da177e4SLinus Torvalds 		}
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 		spin_unlock(&info->lock);
434769848c0SMel Gorman 		page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping));
4351da177e4SLinus Torvalds 		spin_lock(&info->lock);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 		if (!page) {
4381da177e4SLinus Torvalds 			shmem_free_blocks(inode, 1);
4391da177e4SLinus Torvalds 			return ERR_PTR(-ENOMEM);
4401da177e4SLinus Torvalds 		}
4411da177e4SLinus Torvalds 		if (sgp != SGP_WRITE &&
4421da177e4SLinus Torvalds 		    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
4431da177e4SLinus Torvalds 			entry = ERR_PTR(-EINVAL);
4441da177e4SLinus Torvalds 			break;
4451da177e4SLinus Torvalds 		}
4461da177e4SLinus Torvalds 		if (info->next_index <= index)
4471da177e4SLinus Torvalds 			info->next_index = index + 1;
4481da177e4SLinus Torvalds 	}
4491da177e4SLinus Torvalds 	if (page) {
4501da177e4SLinus Torvalds 		/* another task gave its page, or truncated the file */
4511da177e4SLinus Torvalds 		shmem_free_blocks(inode, 1);
4521da177e4SLinus Torvalds 		shmem_dir_free(page);
4531da177e4SLinus Torvalds 	}
4541da177e4SLinus Torvalds 	if (info->next_index <= index && !IS_ERR(entry))
4551da177e4SLinus Torvalds 		info->next_index = index + 1;
4561da177e4SLinus Torvalds 	return entry;
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
45946711810SRandy Dunlap /**
4601da177e4SLinus Torvalds  * shmem_free_swp - free some swap entries in a directory
4611da177e4SLinus Torvalds  * @dir:        pointer to the directory
4621da177e4SLinus Torvalds  * @edir:       pointer after last entry of the directory
4631ae70006SHugh Dickins  * @punch_lock: pointer to spinlock when needed for the holepunch case
4641da177e4SLinus Torvalds  */
4651ae70006SHugh Dickins static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
4661ae70006SHugh Dickins 						spinlock_t *punch_lock)
4671da177e4SLinus Torvalds {
4681ae70006SHugh Dickins 	spinlock_t *punch_unlock = NULL;
4691da177e4SLinus Torvalds 	swp_entry_t *ptr;
4701da177e4SLinus Torvalds 	int freed = 0;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	for (ptr = dir; ptr < edir; ptr++) {
4731da177e4SLinus Torvalds 		if (ptr->val) {
4741ae70006SHugh Dickins 			if (unlikely(punch_lock)) {
4751ae70006SHugh Dickins 				punch_unlock = punch_lock;
4761ae70006SHugh Dickins 				punch_lock = NULL;
4771ae70006SHugh Dickins 				spin_lock(punch_unlock);
4781ae70006SHugh Dickins 				if (!ptr->val)
4791ae70006SHugh Dickins 					continue;
4801ae70006SHugh Dickins 			}
4811da177e4SLinus Torvalds 			free_swap_and_cache(*ptr);
4821da177e4SLinus Torvalds 			*ptr = (swp_entry_t){0};
4831da177e4SLinus Torvalds 			freed++;
4841da177e4SLinus Torvalds 		}
4851da177e4SLinus Torvalds 	}
4861ae70006SHugh Dickins 	if (punch_unlock)
4871ae70006SHugh Dickins 		spin_unlock(punch_unlock);
4881da177e4SLinus Torvalds 	return freed;
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds 
4911ae70006SHugh Dickins static int shmem_map_and_free_swp(struct page *subdir, int offset,
4921ae70006SHugh Dickins 		int limit, struct page ***dir, spinlock_t *punch_lock)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	swp_entry_t *ptr;
4951da177e4SLinus Torvalds 	int freed = 0;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	ptr = shmem_swp_map(subdir);
4981da177e4SLinus Torvalds 	for (; offset < limit; offset += LATENCY_LIMIT) {
4991da177e4SLinus Torvalds 		int size = limit - offset;
5001da177e4SLinus Torvalds 		if (size > LATENCY_LIMIT)
5011da177e4SLinus Torvalds 			size = LATENCY_LIMIT;
5021ae70006SHugh Dickins 		freed += shmem_free_swp(ptr+offset, ptr+offset+size,
5031ae70006SHugh Dickins 							punch_lock);
5041da177e4SLinus Torvalds 		if (need_resched()) {
5051da177e4SLinus Torvalds 			shmem_swp_unmap(ptr);
5061da177e4SLinus Torvalds 			if (*dir) {
5071da177e4SLinus Torvalds 				shmem_dir_unmap(*dir);
5081da177e4SLinus Torvalds 				*dir = NULL;
5091da177e4SLinus Torvalds 			}
5101da177e4SLinus Torvalds 			cond_resched();
5111da177e4SLinus Torvalds 			ptr = shmem_swp_map(subdir);
5121da177e4SLinus Torvalds 		}
5131da177e4SLinus Torvalds 	}
5141da177e4SLinus Torvalds 	shmem_swp_unmap(ptr);
5151da177e4SLinus Torvalds 	return freed;
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds static void shmem_free_pages(struct list_head *next)
5191da177e4SLinus Torvalds {
5201da177e4SLinus Torvalds 	struct page *page;
5211da177e4SLinus Torvalds 	int freed = 0;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	do {
5241da177e4SLinus Torvalds 		page = container_of(next, struct page, lru);
5251da177e4SLinus Torvalds 		next = next->next;
5261da177e4SLinus Torvalds 		shmem_dir_free(page);
5271da177e4SLinus Torvalds 		freed++;
5281da177e4SLinus Torvalds 		if (freed >= LATENCY_LIMIT) {
5291da177e4SLinus Torvalds 			cond_resched();
5301da177e4SLinus Torvalds 			freed = 0;
5311da177e4SLinus Torvalds 		}
5321da177e4SLinus Torvalds 	} while (next);
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds 
535f6b3ec23SBadari Pulavarty static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
5361da177e4SLinus Torvalds {
5371da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
5381da177e4SLinus Torvalds 	unsigned long idx;
5391da177e4SLinus Torvalds 	unsigned long size;
5401da177e4SLinus Torvalds 	unsigned long limit;
5411da177e4SLinus Torvalds 	unsigned long stage;
5421da177e4SLinus Torvalds 	unsigned long diroff;
5431da177e4SLinus Torvalds 	struct page **dir;
5441da177e4SLinus Torvalds 	struct page *topdir;
5451da177e4SLinus Torvalds 	struct page *middir;
5461da177e4SLinus Torvalds 	struct page *subdir;
5471da177e4SLinus Torvalds 	swp_entry_t *ptr;
5481da177e4SLinus Torvalds 	LIST_HEAD(pages_to_free);
5491da177e4SLinus Torvalds 	long nr_pages_to_free = 0;
5501da177e4SLinus Torvalds 	long nr_swaps_freed = 0;
5511da177e4SLinus Torvalds 	int offset;
5521da177e4SLinus Torvalds 	int freed;
553a2646d1eSHugh Dickins 	int punch_hole;
5541ae70006SHugh Dickins 	spinlock_t *needs_lock;
5551ae70006SHugh Dickins 	spinlock_t *punch_lock;
556a2646d1eSHugh Dickins 	unsigned long upper_limit;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
559f6b3ec23SBadari Pulavarty 	idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
5601da177e4SLinus Torvalds 	if (idx >= info->next_index)
5611da177e4SLinus Torvalds 		return;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	spin_lock(&info->lock);
5641da177e4SLinus Torvalds 	info->flags |= SHMEM_TRUNCATE;
565f6b3ec23SBadari Pulavarty 	if (likely(end == (loff_t) -1)) {
5661da177e4SLinus Torvalds 		limit = info->next_index;
567a2646d1eSHugh Dickins 		upper_limit = SHMEM_MAX_INDEX;
5681da177e4SLinus Torvalds 		info->next_index = idx;
5691ae70006SHugh Dickins 		needs_lock = NULL;
570a2646d1eSHugh Dickins 		punch_hole = 0;
571f6b3ec23SBadari Pulavarty 	} else {
572a2646d1eSHugh Dickins 		if (end + 1 >= inode->i_size) {	/* we may free a little more */
573a2646d1eSHugh Dickins 			limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
574a2646d1eSHugh Dickins 							PAGE_CACHE_SHIFT;
575a2646d1eSHugh Dickins 			upper_limit = SHMEM_MAX_INDEX;
576a2646d1eSHugh Dickins 		} else {
577a2646d1eSHugh Dickins 			limit = (end + 1) >> PAGE_CACHE_SHIFT;
578a2646d1eSHugh Dickins 			upper_limit = limit;
579a2646d1eSHugh Dickins 		}
5801ae70006SHugh Dickins 		needs_lock = &info->lock;
581f6b3ec23SBadari Pulavarty 		punch_hole = 1;
582f6b3ec23SBadari Pulavarty 	}
583f6b3ec23SBadari Pulavarty 
5841da177e4SLinus Torvalds 	topdir = info->i_indirect;
585f6b3ec23SBadari Pulavarty 	if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
5861da177e4SLinus Torvalds 		info->i_indirect = NULL;
5871da177e4SLinus Torvalds 		nr_pages_to_free++;
5881da177e4SLinus Torvalds 		list_add(&topdir->lru, &pages_to_free);
5891da177e4SLinus Torvalds 	}
5901da177e4SLinus Torvalds 	spin_unlock(&info->lock);
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	if (info->swapped && idx < SHMEM_NR_DIRECT) {
5931da177e4SLinus Torvalds 		ptr = info->i_direct;
5941da177e4SLinus Torvalds 		size = limit;
5951da177e4SLinus Torvalds 		if (size > SHMEM_NR_DIRECT)
5961da177e4SLinus Torvalds 			size = SHMEM_NR_DIRECT;
5971ae70006SHugh Dickins 		nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
5981da177e4SLinus Torvalds 	}
59992a3d03aSBadari Pulavarty 
60092a3d03aSBadari Pulavarty 	/*
60192a3d03aSBadari Pulavarty 	 * If there are no indirect blocks or we are punching a hole
60292a3d03aSBadari Pulavarty 	 * below indirect blocks, nothing to be done.
60392a3d03aSBadari Pulavarty 	 */
604a2646d1eSHugh Dickins 	if (!topdir || limit <= SHMEM_NR_DIRECT)
6051da177e4SLinus Torvalds 		goto done2;
6061da177e4SLinus Torvalds 
6071ae70006SHugh Dickins 	/*
6081ae70006SHugh Dickins 	 * The truncation case has already dropped info->lock, and we're safe
6091ae70006SHugh Dickins 	 * because i_size and next_index have already been lowered, preventing
6101ae70006SHugh Dickins 	 * access beyond.  But in the punch_hole case, we still need to take
6111ae70006SHugh Dickins 	 * the lock when updating the swap directory, because there might be
6121ae70006SHugh Dickins 	 * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
6131ae70006SHugh Dickins 	 * shmem_writepage.  However, whenever we find we can remove a whole
6141ae70006SHugh Dickins 	 * directory page (not at the misaligned start or end of the range),
6151ae70006SHugh Dickins 	 * we first NULLify its pointer in the level above, and then have no
6161ae70006SHugh Dickins 	 * need to take the lock when updating its contents: needs_lock and
6171ae70006SHugh Dickins 	 * punch_lock (either pointing to info->lock or NULL) manage this.
6181ae70006SHugh Dickins 	 */
6191ae70006SHugh Dickins 
620a2646d1eSHugh Dickins 	upper_limit -= SHMEM_NR_DIRECT;
6211da177e4SLinus Torvalds 	limit -= SHMEM_NR_DIRECT;
6221da177e4SLinus Torvalds 	idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
6231da177e4SLinus Torvalds 	offset = idx % ENTRIES_PER_PAGE;
6241da177e4SLinus Torvalds 	idx -= offset;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	dir = shmem_dir_map(topdir);
6271da177e4SLinus Torvalds 	stage = ENTRIES_PER_PAGEPAGE/2;
6281da177e4SLinus Torvalds 	if (idx < ENTRIES_PER_PAGEPAGE/2) {
6291da177e4SLinus Torvalds 		middir = topdir;
6301da177e4SLinus Torvalds 		diroff = idx/ENTRIES_PER_PAGE;
6311da177e4SLinus Torvalds 	} else {
6321da177e4SLinus Torvalds 		dir += ENTRIES_PER_PAGE/2;
6331da177e4SLinus Torvalds 		dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
6341da177e4SLinus Torvalds 		while (stage <= idx)
6351da177e4SLinus Torvalds 			stage += ENTRIES_PER_PAGEPAGE;
6361da177e4SLinus Torvalds 		middir = *dir;
6371da177e4SLinus Torvalds 		if (*dir) {
6381da177e4SLinus Torvalds 			diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
6391da177e4SLinus Torvalds 				ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
640a2646d1eSHugh Dickins 			if (!diroff && !offset && upper_limit >= stage) {
6411ae70006SHugh Dickins 				if (needs_lock) {
6421ae70006SHugh Dickins 					spin_lock(needs_lock);
6431ae70006SHugh Dickins 					*dir = NULL;
6441ae70006SHugh Dickins 					spin_unlock(needs_lock);
6451ae70006SHugh Dickins 					needs_lock = NULL;
6461ae70006SHugh Dickins 				} else
6471da177e4SLinus Torvalds 					*dir = NULL;
6481da177e4SLinus Torvalds 				nr_pages_to_free++;
6491da177e4SLinus Torvalds 				list_add(&middir->lru, &pages_to_free);
6501da177e4SLinus Torvalds 			}
6511da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6521da177e4SLinus Torvalds 			dir = shmem_dir_map(middir);
6531da177e4SLinus Torvalds 		} else {
6541da177e4SLinus Torvalds 			diroff = 0;
6551da177e4SLinus Torvalds 			offset = 0;
6561da177e4SLinus Torvalds 			idx = stage;
6571da177e4SLinus Torvalds 		}
6581da177e4SLinus Torvalds 	}
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
6611da177e4SLinus Torvalds 		if (unlikely(idx == stage)) {
6621da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6631da177e4SLinus Torvalds 			dir = shmem_dir_map(topdir) +
6641da177e4SLinus Torvalds 			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
6651da177e4SLinus Torvalds 			while (!*dir) {
6661da177e4SLinus Torvalds 				dir++;
6671da177e4SLinus Torvalds 				idx += ENTRIES_PER_PAGEPAGE;
6681da177e4SLinus Torvalds 				if (idx >= limit)
6691da177e4SLinus Torvalds 					goto done1;
6701da177e4SLinus Torvalds 			}
6711da177e4SLinus Torvalds 			stage = idx + ENTRIES_PER_PAGEPAGE;
6721da177e4SLinus Torvalds 			middir = *dir;
6731ae70006SHugh Dickins 			if (punch_hole)
6741ae70006SHugh Dickins 				needs_lock = &info->lock;
675a2646d1eSHugh Dickins 			if (upper_limit >= stage) {
6761ae70006SHugh Dickins 				if (needs_lock) {
6771ae70006SHugh Dickins 					spin_lock(needs_lock);
6781ae70006SHugh Dickins 					*dir = NULL;
6791ae70006SHugh Dickins 					spin_unlock(needs_lock);
6801ae70006SHugh Dickins 					needs_lock = NULL;
6811ae70006SHugh Dickins 				} else
6821da177e4SLinus Torvalds 					*dir = NULL;
6831da177e4SLinus Torvalds 				nr_pages_to_free++;
6841da177e4SLinus Torvalds 				list_add(&middir->lru, &pages_to_free);
685a2646d1eSHugh Dickins 			}
6861da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
6871da177e4SLinus Torvalds 			cond_resched();
6881da177e4SLinus Torvalds 			dir = shmem_dir_map(middir);
6891da177e4SLinus Torvalds 			diroff = 0;
6901da177e4SLinus Torvalds 		}
6911ae70006SHugh Dickins 		punch_lock = needs_lock;
6921da177e4SLinus Torvalds 		subdir = dir[diroff];
6931ae70006SHugh Dickins 		if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
6941ae70006SHugh Dickins 			if (needs_lock) {
6951ae70006SHugh Dickins 				spin_lock(needs_lock);
6961ae70006SHugh Dickins 				dir[diroff] = NULL;
6971ae70006SHugh Dickins 				spin_unlock(needs_lock);
6981ae70006SHugh Dickins 				punch_lock = NULL;
6991ae70006SHugh Dickins 			} else
7001da177e4SLinus Torvalds 				dir[diroff] = NULL;
7011da177e4SLinus Torvalds 			nr_pages_to_free++;
7021da177e4SLinus Torvalds 			list_add(&subdir->lru, &pages_to_free);
7031da177e4SLinus Torvalds 		}
7041ae70006SHugh Dickins 		if (subdir && page_private(subdir) /* has swap entries */) {
7051ae70006SHugh Dickins 			size = limit - idx;
7061ae70006SHugh Dickins 			if (size > ENTRIES_PER_PAGE)
7071ae70006SHugh Dickins 				size = ENTRIES_PER_PAGE;
7081ae70006SHugh Dickins 			freed = shmem_map_and_free_swp(subdir,
7091ae70006SHugh Dickins 					offset, size, &dir, punch_lock);
7101ae70006SHugh Dickins 			if (!dir)
7111ae70006SHugh Dickins 				dir = shmem_dir_map(middir);
7121ae70006SHugh Dickins 			nr_swaps_freed += freed;
7131ae70006SHugh Dickins 			if (offset || punch_lock) {
7141ae70006SHugh Dickins 				spin_lock(&info->lock);
7151ae70006SHugh Dickins 				set_page_private(subdir,
7161ae70006SHugh Dickins 					page_private(subdir) - freed);
7171ae70006SHugh Dickins 				spin_unlock(&info->lock);
7181ae70006SHugh Dickins 			} else
7191ae70006SHugh Dickins 				BUG_ON(page_private(subdir) != freed);
7201ae70006SHugh Dickins 		}
7211ae70006SHugh Dickins 		offset = 0;
7221da177e4SLinus Torvalds 	}
7231da177e4SLinus Torvalds done1:
7241da177e4SLinus Torvalds 	shmem_dir_unmap(dir);
7251da177e4SLinus Torvalds done2:
7261da177e4SLinus Torvalds 	if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
7271da177e4SLinus Torvalds 		/*
7281da177e4SLinus Torvalds 		 * Call truncate_inode_pages again: racing shmem_unuse_inode
7293889e6e7Snpiggin@suse.de 		 * may have swizzled a page in from swap since
7303889e6e7Snpiggin@suse.de 		 * truncate_pagecache or generic_delete_inode did it, before we
7313889e6e7Snpiggin@suse.de 		 * lowered next_index.  Also, though shmem_getpage checks
7323889e6e7Snpiggin@suse.de 		 * i_size before adding to cache, no recheck after: so fix the
7333889e6e7Snpiggin@suse.de 		 * narrow window there too.
73416a10019SHugh Dickins 		 *
73516a10019SHugh Dickins 		 * Recalling truncate_inode_pages_range and unmap_mapping_range
73616a10019SHugh Dickins 		 * every time for punch_hole (which never got a chance to clear
73716a10019SHugh Dickins 		 * SHMEM_PAGEIN at the start of vmtruncate_range) is expensive,
73816a10019SHugh Dickins 		 * yet hardly ever necessary: try to optimize them out later.
7391da177e4SLinus Torvalds 		 */
740f6b3ec23SBadari Pulavarty 		truncate_inode_pages_range(inode->i_mapping, start, end);
74116a10019SHugh Dickins 		if (punch_hole)
74216a10019SHugh Dickins 			unmap_mapping_range(inode->i_mapping, start,
74316a10019SHugh Dickins 							end - start, 1);
7441da177e4SLinus Torvalds 	}
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	spin_lock(&info->lock);
7471da177e4SLinus Torvalds 	info->flags &= ~SHMEM_TRUNCATE;
7481da177e4SLinus Torvalds 	info->swapped -= nr_swaps_freed;
7491da177e4SLinus Torvalds 	if (nr_pages_to_free)
7501da177e4SLinus Torvalds 		shmem_free_blocks(inode, nr_pages_to_free);
7511da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
7521da177e4SLinus Torvalds 	spin_unlock(&info->lock);
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	/*
7551da177e4SLinus Torvalds 	 * Empty swap vector directory pages to be freed?
7561da177e4SLinus Torvalds 	 */
7571da177e4SLinus Torvalds 	if (!list_empty(&pages_to_free)) {
7581da177e4SLinus Torvalds 		pages_to_free.prev->next = NULL;
7591da177e4SLinus Torvalds 		shmem_free_pages(pages_to_free.next);
7601da177e4SLinus Torvalds 	}
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
7641da177e4SLinus Torvalds {
7651da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
766af5a30d8SNick Piggin 	loff_t newsize = attr->ia_size;
7671da177e4SLinus Torvalds 	int error;
7681da177e4SLinus Torvalds 
769db78b877SChristoph Hellwig 	error = inode_change_ok(inode, attr);
770db78b877SChristoph Hellwig 	if (error)
771db78b877SChristoph Hellwig 		return error;
772db78b877SChristoph Hellwig 
773af5a30d8SNick Piggin 	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)
774af5a30d8SNick Piggin 					&& newsize != inode->i_size) {
7753889e6e7Snpiggin@suse.de 		struct page *page = NULL;
7763889e6e7Snpiggin@suse.de 
7773889e6e7Snpiggin@suse.de 		if (newsize < inode->i_size) {
7781da177e4SLinus Torvalds 			/*
7791da177e4SLinus Torvalds 			 * If truncating down to a partial page, then
7801da177e4SLinus Torvalds 			 * if that page is already allocated, hold it
7811da177e4SLinus Torvalds 			 * in memory until the truncation is over, so
7821da177e4SLinus Torvalds 			 * truncate_partial_page cannnot miss it were
7831da177e4SLinus Torvalds 			 * it assigned to swap.
7841da177e4SLinus Torvalds 			 */
7853889e6e7Snpiggin@suse.de 			if (newsize & (PAGE_CACHE_SIZE-1)) {
7861da177e4SLinus Torvalds 				(void) shmem_getpage(inode,
7873889e6e7Snpiggin@suse.de 					newsize >> PAGE_CACHE_SHIFT,
7881da177e4SLinus Torvalds 						&page, SGP_READ, NULL);
789d3602444SHugh Dickins 				if (page)
790d3602444SHugh Dickins 					unlock_page(page);
7911da177e4SLinus Torvalds 			}
7921da177e4SLinus Torvalds 			/*
7931da177e4SLinus Torvalds 			 * Reset SHMEM_PAGEIN flag so that shmem_truncate can
7941da177e4SLinus Torvalds 			 * detect if any pages might have been added to cache
7951da177e4SLinus Torvalds 			 * after truncate_inode_pages.  But we needn't bother
7961da177e4SLinus Torvalds 			 * if it's being fully truncated to zero-length: the
7971da177e4SLinus Torvalds 			 * nrpages check is efficient enough in that case.
7981da177e4SLinus Torvalds 			 */
7993889e6e7Snpiggin@suse.de 			if (newsize) {
8001da177e4SLinus Torvalds 				struct shmem_inode_info *info = SHMEM_I(inode);
8011da177e4SLinus Torvalds 				spin_lock(&info->lock);
8021da177e4SLinus Torvalds 				info->flags &= ~SHMEM_PAGEIN;
8031da177e4SLinus Torvalds 				spin_unlock(&info->lock);
8041da177e4SLinus Torvalds 			}
8051da177e4SLinus Torvalds 		}
8063889e6e7Snpiggin@suse.de 
8072c27c65eSChristoph Hellwig 		/* XXX(truncate): truncate_setsize should be called last */
8082c27c65eSChristoph Hellwig 		truncate_setsize(inode, newsize);
8093889e6e7Snpiggin@suse.de 		if (page)
8103889e6e7Snpiggin@suse.de 			page_cache_release(page);
8113889e6e7Snpiggin@suse.de 		shmem_truncate_range(inode, newsize, (loff_t)-1);
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds 
8146a1a90adSChristoph Hellwig 	setattr_copy(inode, attr);
81539f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
816db78b877SChristoph Hellwig 	if (attr->ia_valid & ATTR_MODE)
8171c7c474cSChristoph Hellwig 		error = generic_acl_chmod(inode);
81839f0247dSAndreas Gruenbacher #endif
8191da177e4SLinus Torvalds 	return error;
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
8221f895f75SAl Viro static void shmem_evict_inode(struct inode *inode)
8231da177e4SLinus Torvalds {
8241da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
8251da177e4SLinus Torvalds 
8263889e6e7Snpiggin@suse.de 	if (inode->i_mapping->a_ops == &shmem_aops) {
827fef26658SMark Fasheh 		truncate_inode_pages(inode->i_mapping, 0);
8281da177e4SLinus Torvalds 		shmem_unacct_size(info->flags, inode->i_size);
8291da177e4SLinus Torvalds 		inode->i_size = 0;
8303889e6e7Snpiggin@suse.de 		shmem_truncate_range(inode, 0, (loff_t)-1);
8311da177e4SLinus Torvalds 		if (!list_empty(&info->swaplist)) {
832cb5f7b9aSHugh Dickins 			mutex_lock(&shmem_swaplist_mutex);
8331da177e4SLinus Torvalds 			list_del_init(&info->swaplist);
834cb5f7b9aSHugh Dickins 			mutex_unlock(&shmem_swaplist_mutex);
8351da177e4SLinus Torvalds 		}
8361da177e4SLinus Torvalds 	}
8371da177e4SLinus Torvalds 	BUG_ON(inode->i_blocks);
8385b04c689SPavel Emelyanov 	shmem_free_inode(inode->i_sb);
8391f895f75SAl Viro 	end_writeback(inode);
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
8431da177e4SLinus Torvalds {
8441da177e4SLinus Torvalds 	swp_entry_t *ptr;
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	for (ptr = dir; ptr < edir; ptr++) {
8471da177e4SLinus Torvalds 		if (ptr->val == entry.val)
8481da177e4SLinus Torvalds 			return ptr - dir;
8491da177e4SLinus Torvalds 	}
8501da177e4SLinus Torvalds 	return -1;
8511da177e4SLinus Torvalds }
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
8541da177e4SLinus Torvalds {
8551da177e4SLinus Torvalds 	struct inode *inode;
8561da177e4SLinus Torvalds 	unsigned long idx;
8571da177e4SLinus Torvalds 	unsigned long size;
8581da177e4SLinus Torvalds 	unsigned long limit;
8591da177e4SLinus Torvalds 	unsigned long stage;
8601da177e4SLinus Torvalds 	struct page **dir;
8611da177e4SLinus Torvalds 	struct page *subdir;
8621da177e4SLinus Torvalds 	swp_entry_t *ptr;
8631da177e4SLinus Torvalds 	int offset;
864d9fe526aSHugh Dickins 	int error;
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	idx = 0;
8671da177e4SLinus Torvalds 	ptr = info->i_direct;
8681da177e4SLinus Torvalds 	spin_lock(&info->lock);
8691b1b32f2SHugh Dickins 	if (!info->swapped) {
8701b1b32f2SHugh Dickins 		list_del_init(&info->swaplist);
8711b1b32f2SHugh Dickins 		goto lost2;
8721b1b32f2SHugh Dickins 	}
8731da177e4SLinus Torvalds 	limit = info->next_index;
8741da177e4SLinus Torvalds 	size = limit;
8751da177e4SLinus Torvalds 	if (size > SHMEM_NR_DIRECT)
8761da177e4SLinus Torvalds 		size = SHMEM_NR_DIRECT;
8771da177e4SLinus Torvalds 	offset = shmem_find_swp(entry, ptr, ptr+size);
8782e0e26c7SHugh Dickins 	if (offset >= 0)
8791da177e4SLinus Torvalds 		goto found;
8801da177e4SLinus Torvalds 	if (!info->i_indirect)
8811da177e4SLinus Torvalds 		goto lost2;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	dir = shmem_dir_map(info->i_indirect);
8841da177e4SLinus Torvalds 	stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
8871da177e4SLinus Torvalds 		if (unlikely(idx == stage)) {
8881da177e4SLinus Torvalds 			shmem_dir_unmap(dir-1);
889cb5f7b9aSHugh Dickins 			if (cond_resched_lock(&info->lock)) {
890cb5f7b9aSHugh Dickins 				/* check it has not been truncated */
891cb5f7b9aSHugh Dickins 				if (limit > info->next_index) {
892cb5f7b9aSHugh Dickins 					limit = info->next_index;
893cb5f7b9aSHugh Dickins 					if (idx >= limit)
894cb5f7b9aSHugh Dickins 						goto lost2;
895cb5f7b9aSHugh Dickins 				}
896cb5f7b9aSHugh Dickins 			}
8971da177e4SLinus Torvalds 			dir = shmem_dir_map(info->i_indirect) +
8981da177e4SLinus Torvalds 			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
8991da177e4SLinus Torvalds 			while (!*dir) {
9001da177e4SLinus Torvalds 				dir++;
9011da177e4SLinus Torvalds 				idx += ENTRIES_PER_PAGEPAGE;
9021da177e4SLinus Torvalds 				if (idx >= limit)
9031da177e4SLinus Torvalds 					goto lost1;
9041da177e4SLinus Torvalds 			}
9051da177e4SLinus Torvalds 			stage = idx + ENTRIES_PER_PAGEPAGE;
9061da177e4SLinus Torvalds 			subdir = *dir;
9071da177e4SLinus Torvalds 			shmem_dir_unmap(dir);
9081da177e4SLinus Torvalds 			dir = shmem_dir_map(subdir);
9091da177e4SLinus Torvalds 		}
9101da177e4SLinus Torvalds 		subdir = *dir;
9114c21e2f2SHugh Dickins 		if (subdir && page_private(subdir)) {
9121da177e4SLinus Torvalds 			ptr = shmem_swp_map(subdir);
9131da177e4SLinus Torvalds 			size = limit - idx;
9141da177e4SLinus Torvalds 			if (size > ENTRIES_PER_PAGE)
9151da177e4SLinus Torvalds 				size = ENTRIES_PER_PAGE;
9161da177e4SLinus Torvalds 			offset = shmem_find_swp(entry, ptr, ptr+size);
9172e0e26c7SHugh Dickins 			shmem_swp_unmap(ptr);
9181da177e4SLinus Torvalds 			if (offset >= 0) {
9191da177e4SLinus Torvalds 				shmem_dir_unmap(dir);
9201da177e4SLinus Torvalds 				goto found;
9211da177e4SLinus Torvalds 			}
9221da177e4SLinus Torvalds 		}
9231da177e4SLinus Torvalds 	}
9241da177e4SLinus Torvalds lost1:
9251da177e4SLinus Torvalds 	shmem_dir_unmap(dir-1);
9261da177e4SLinus Torvalds lost2:
9271da177e4SLinus Torvalds 	spin_unlock(&info->lock);
9281da177e4SLinus Torvalds 	return 0;
9291da177e4SLinus Torvalds found:
9301da177e4SLinus Torvalds 	idx += offset;
9312e0e26c7SHugh Dickins 	inode = igrab(&info->vfs_inode);
9322e0e26c7SHugh Dickins 	spin_unlock(&info->lock);
9332e0e26c7SHugh Dickins 
9341b1b32f2SHugh Dickins 	/*
9351b1b32f2SHugh Dickins 	 * Move _head_ to start search for next from here.
9361f895f75SAl Viro 	 * But be careful: shmem_evict_inode checks list_empty without taking
9371b1b32f2SHugh Dickins 	 * mutex, and there's an instant in list_move_tail when info->swaplist
9381b1b32f2SHugh Dickins 	 * would appear empty, if it were the only one on shmem_swaplist.  We
9391b1b32f2SHugh Dickins 	 * could avoid doing it if inode NULL; or use this minor optimization.
9401b1b32f2SHugh Dickins 	 */
9411b1b32f2SHugh Dickins 	if (shmem_swaplist.next != &info->swaplist)
9422e0e26c7SHugh Dickins 		list_move_tail(&shmem_swaplist, &info->swaplist);
9432e0e26c7SHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
9442e0e26c7SHugh Dickins 
9452e0e26c7SHugh Dickins 	error = 1;
9462e0e26c7SHugh Dickins 	if (!inode)
9472e0e26c7SHugh Dickins 		goto out;
948d13d1443SKAMEZAWA Hiroyuki 	/*
949b5a84319SKAMEZAWA Hiroyuki 	 * Charge page using GFP_KERNEL while we can wait.
950b5a84319SKAMEZAWA Hiroyuki 	 * Charged back to the user(not to caller) when swap account is used.
951b5a84319SKAMEZAWA Hiroyuki 	 * add_to_page_cache() will be called with GFP_NOWAIT.
952d13d1443SKAMEZAWA Hiroyuki 	 */
95382369553SHugh Dickins 	error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
954b409f9fcSHugh Dickins 	if (error)
955b409f9fcSHugh Dickins 		goto out;
95682369553SHugh Dickins 	error = radix_tree_preload(GFP_KERNEL);
95769029cd5SKAMEZAWA Hiroyuki 	if (error) {
95869029cd5SKAMEZAWA Hiroyuki 		mem_cgroup_uncharge_cache_page(page);
95969029cd5SKAMEZAWA Hiroyuki 		goto out;
96069029cd5SKAMEZAWA Hiroyuki 	}
961b409f9fcSHugh Dickins 	error = 1;
9622e0e26c7SHugh Dickins 
9632e0e26c7SHugh Dickins 	spin_lock(&info->lock);
9642e0e26c7SHugh Dickins 	ptr = shmem_swp_entry(info, idx, NULL);
96569029cd5SKAMEZAWA Hiroyuki 	if (ptr && ptr->val == entry.val) {
966e286781dSNick Piggin 		error = add_to_page_cache_locked(page, inode->i_mapping,
967b409f9fcSHugh Dickins 						idx, GFP_NOWAIT);
96869029cd5SKAMEZAWA Hiroyuki 		/* does mem_cgroup_uncharge_cache_page on error */
96969029cd5SKAMEZAWA Hiroyuki 	} else	/* we must compensate for our precharge above */
97069029cd5SKAMEZAWA Hiroyuki 		mem_cgroup_uncharge_cache_page(page);
97169029cd5SKAMEZAWA Hiroyuki 
972d9fe526aSHugh Dickins 	if (error == -EEXIST) {
973d9fe526aSHugh Dickins 		struct page *filepage = find_get_page(inode->i_mapping, idx);
9742e0e26c7SHugh Dickins 		error = 1;
975d9fe526aSHugh Dickins 		if (filepage) {
976d9fe526aSHugh Dickins 			/*
977d9fe526aSHugh Dickins 			 * There might be a more uptodate page coming down
978d9fe526aSHugh Dickins 			 * from a stacked writepage: forget our swappage if so.
979d9fe526aSHugh Dickins 			 */
980d9fe526aSHugh Dickins 			if (PageUptodate(filepage))
981d9fe526aSHugh Dickins 				error = 0;
982d9fe526aSHugh Dickins 			page_cache_release(filepage);
983d9fe526aSHugh Dickins 		}
984d9fe526aSHugh Dickins 	}
985d9fe526aSHugh Dickins 	if (!error) {
98673b1262fSHugh Dickins 		delete_from_swap_cache(page);
98773b1262fSHugh Dickins 		set_page_dirty(page);
9881da177e4SLinus Torvalds 		info->flags |= SHMEM_PAGEIN;
9892e0e26c7SHugh Dickins 		shmem_swp_set(info, ptr, 0);
9902e0e26c7SHugh Dickins 		swap_free(entry);
9912e0e26c7SHugh Dickins 		error = 1;	/* not an error, but entry was found */
9921da177e4SLinus Torvalds 	}
9932e0e26c7SHugh Dickins 	if (ptr)
9941da177e4SLinus Torvalds 		shmem_swp_unmap(ptr);
9951da177e4SLinus Torvalds 	spin_unlock(&info->lock);
996b409f9fcSHugh Dickins 	radix_tree_preload_end();
9972e0e26c7SHugh Dickins out:
9982e0e26c7SHugh Dickins 	unlock_page(page);
9992e0e26c7SHugh Dickins 	page_cache_release(page);
10002e0e26c7SHugh Dickins 	iput(inode);		/* allows for NULL */
10012e0e26c7SHugh Dickins 	return error;
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds /*
10051da177e4SLinus Torvalds  * shmem_unuse() search for an eventually swapped out shmem page.
10061da177e4SLinus Torvalds  */
10071da177e4SLinus Torvalds int shmem_unuse(swp_entry_t entry, struct page *page)
10081da177e4SLinus Torvalds {
10091da177e4SLinus Torvalds 	struct list_head *p, *next;
10101da177e4SLinus Torvalds 	struct shmem_inode_info *info;
10111da177e4SLinus Torvalds 	int found = 0;
10121da177e4SLinus Torvalds 
1013cb5f7b9aSHugh Dickins 	mutex_lock(&shmem_swaplist_mutex);
10141da177e4SLinus Torvalds 	list_for_each_safe(p, next, &shmem_swaplist) {
10151da177e4SLinus Torvalds 		info = list_entry(p, struct shmem_inode_info, swaplist);
10162e0e26c7SHugh Dickins 		found = shmem_unuse_inode(info, entry, page);
1017cb5f7b9aSHugh Dickins 		cond_resched();
10182e0e26c7SHugh Dickins 		if (found)
10192e0e26c7SHugh Dickins 			goto out;
10201da177e4SLinus Torvalds 	}
1021cb5f7b9aSHugh Dickins 	mutex_unlock(&shmem_swaplist_mutex);
1022aaa46865SHugh Dickins 	/*
1023aaa46865SHugh Dickins 	 * Can some race bring us here?  We've been holding page lock,
1024aaa46865SHugh Dickins 	 * so I think not; but would rather try again later than BUG()
1025aaa46865SHugh Dickins 	 */
1026aaa46865SHugh Dickins 	unlock_page(page);
1027aaa46865SHugh Dickins 	page_cache_release(page);
1028aaa46865SHugh Dickins out:
1029aaa46865SHugh Dickins 	return (found < 0) ? found : 0;
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds /*
10331da177e4SLinus Torvalds  * Move the page from the page cache to the swap cache.
10341da177e4SLinus Torvalds  */
10351da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	struct shmem_inode_info *info;
10381da177e4SLinus Torvalds 	swp_entry_t *entry, swap;
10391da177e4SLinus Torvalds 	struct address_space *mapping;
10401da177e4SLinus Torvalds 	unsigned long index;
10411da177e4SLinus Torvalds 	struct inode *inode;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
10441da177e4SLinus Torvalds 	mapping = page->mapping;
10451da177e4SLinus Torvalds 	index = page->index;
10461da177e4SLinus Torvalds 	inode = mapping->host;
10471da177e4SLinus Torvalds 	info = SHMEM_I(inode);
10481da177e4SLinus Torvalds 	if (info->flags & VM_LOCKED)
10491da177e4SLinus Torvalds 		goto redirty;
1050d9fe526aSHugh Dickins 	if (!total_swap_pages)
10511da177e4SLinus Torvalds 		goto redirty;
10521da177e4SLinus Torvalds 
1053d9fe526aSHugh Dickins 	/*
1054d9fe526aSHugh Dickins 	 * shmem_backing_dev_info's capabilities prevent regular writeback or
1055d9fe526aSHugh Dickins 	 * sync from ever calling shmem_writepage; but a stacking filesystem
1056d9fe526aSHugh Dickins 	 * may use the ->writepage of its underlying filesystem, in which case
1057d9fe526aSHugh Dickins 	 * tmpfs should write out to swap only in response to memory pressure,
10585b0830cbSJens Axboe 	 * and not for the writeback threads or sync.  However, in those cases,
10595b0830cbSJens Axboe 	 * we do still want to check if there's a redundant swappage to be
10605b0830cbSJens Axboe 	 * discarded.
1061d9fe526aSHugh Dickins 	 */
1062d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
1063d9fe526aSHugh Dickins 		swap = get_swap_page();
1064d9fe526aSHugh Dickins 	else
1065d9fe526aSHugh Dickins 		swap.val = 0;
1066d9fe526aSHugh Dickins 
10671da177e4SLinus Torvalds 	spin_lock(&info->lock);
10681da177e4SLinus Torvalds 	if (index >= info->next_index) {
10691da177e4SLinus Torvalds 		BUG_ON(!(info->flags & SHMEM_TRUNCATE));
10701da177e4SLinus Torvalds 		goto unlock;
10711da177e4SLinus Torvalds 	}
10721da177e4SLinus Torvalds 	entry = shmem_swp_entry(info, index, NULL);
1073d9fe526aSHugh Dickins 	if (entry->val) {
1074d9fe526aSHugh Dickins 		/*
1075d9fe526aSHugh Dickins 		 * The more uptodate page coming down from a stacked
1076d9fe526aSHugh Dickins 		 * writepage should replace our old swappage.
1077d9fe526aSHugh Dickins 		 */
1078d9fe526aSHugh Dickins 		free_swap_and_cache(*entry);
1079d9fe526aSHugh Dickins 		shmem_swp_set(info, entry, 0);
1080d9fe526aSHugh Dickins 	}
1081d9fe526aSHugh Dickins 	shmem_recalc_inode(inode);
10821da177e4SLinus Torvalds 
1083d9fe526aSHugh Dickins 	if (swap.val && add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
108473b1262fSHugh Dickins 		remove_from_page_cache(page);
10851da177e4SLinus Torvalds 		shmem_swp_set(info, entry, swap.val);
10861da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
10871b1b32f2SHugh Dickins 		if (list_empty(&info->swaplist))
10881b1b32f2SHugh Dickins 			inode = igrab(inode);
10891b1b32f2SHugh Dickins 		else
10901b1b32f2SHugh Dickins 			inode = NULL;
10911da177e4SLinus Torvalds 		spin_unlock(&info->lock);
1092aaa46865SHugh Dickins 		swap_shmem_alloc(swap);
1093d9fe526aSHugh Dickins 		BUG_ON(page_mapped(page));
109473b1262fSHugh Dickins 		page_cache_release(page);	/* pagecache ref */
10959fab5619SHugh Dickins 		swap_writepage(page, wbc);
10961b1b32f2SHugh Dickins 		if (inode) {
10971b1b32f2SHugh Dickins 			mutex_lock(&shmem_swaplist_mutex);
10981b1b32f2SHugh Dickins 			/* move instead of add in case we're racing */
10991b1b32f2SHugh Dickins 			list_move_tail(&info->swaplist, &shmem_swaplist);
11001b1b32f2SHugh Dickins 			mutex_unlock(&shmem_swaplist_mutex);
11011b1b32f2SHugh Dickins 			iput(inode);
11021b1b32f2SHugh Dickins 		}
11031da177e4SLinus Torvalds 		return 0;
11041da177e4SLinus Torvalds 	}
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	shmem_swp_unmap(entry);
11071da177e4SLinus Torvalds unlock:
11081da177e4SLinus Torvalds 	spin_unlock(&info->lock);
11092ca4532aSDaisuke Nishimura 	/*
11102ca4532aSDaisuke Nishimura 	 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
11112ca4532aSDaisuke Nishimura 	 * clear SWAP_HAS_CACHE flag.
11122ca4532aSDaisuke Nishimura 	 */
1113cb4b86baSKAMEZAWA Hiroyuki 	swapcache_free(swap, NULL);
11141da177e4SLinus Torvalds redirty:
11151da177e4SLinus Torvalds 	set_page_dirty(page);
1116d9fe526aSHugh Dickins 	if (wbc->for_reclaim)
1117d9fe526aSHugh Dickins 		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
1118d9fe526aSHugh Dickins 	unlock_page(page);
1119d9fe526aSHugh Dickins 	return 0;
11201da177e4SLinus Torvalds }
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1123680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
112471fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1125680d794bSakpm@linux-foundation.org {
1126680d794bSakpm@linux-foundation.org 	char buffer[64];
1127680d794bSakpm@linux-foundation.org 
112871fe804bSLee Schermerhorn 	if (!mpol || mpol->mode == MPOL_DEFAULT)
1129095f1fc4SLee Schermerhorn 		return;		/* show nothing */
1130095f1fc4SLee Schermerhorn 
113171fe804bSLee Schermerhorn 	mpol_to_str(buffer, sizeof(buffer), mpol, 1);
1132095f1fc4SLee Schermerhorn 
1133095f1fc4SLee Schermerhorn 	seq_printf(seq, ",mpol=%s", buffer);
1134680d794bSakpm@linux-foundation.org }
113571fe804bSLee Schermerhorn 
113671fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
113771fe804bSLee Schermerhorn {
113871fe804bSLee Schermerhorn 	struct mempolicy *mpol = NULL;
113971fe804bSLee Schermerhorn 	if (sbinfo->mpol) {
114071fe804bSLee Schermerhorn 		spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
114171fe804bSLee Schermerhorn 		mpol = sbinfo->mpol;
114271fe804bSLee Schermerhorn 		mpol_get(mpol);
114371fe804bSLee Schermerhorn 		spin_unlock(&sbinfo->stat_lock);
114471fe804bSLee Schermerhorn 	}
114571fe804bSLee Schermerhorn 	return mpol;
114671fe804bSLee Schermerhorn }
1147680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
1148680d794bSakpm@linux-foundation.org 
114902098feaSHugh Dickins static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
115002098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11511da177e4SLinus Torvalds {
115252cd3b07SLee Schermerhorn 	struct mempolicy mpol, *spol;
11531da177e4SLinus Torvalds 	struct vm_area_struct pvma;
1154c4cc6d07SHugh Dickins 	struct page *page;
11551da177e4SLinus Torvalds 
115652cd3b07SLee Schermerhorn 	spol = mpol_cond_copy(&mpol,
115752cd3b07SLee Schermerhorn 				mpol_shared_policy_lookup(&info->policy, idx));
115852cd3b07SLee Schermerhorn 
11591da177e4SLinus Torvalds 	/* Create a pseudo vma that just contains the policy */
1160c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
11611da177e4SLinus Torvalds 	pvma.vm_pgoff = idx;
1162c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
116352cd3b07SLee Schermerhorn 	pvma.vm_policy = spol;
116402098feaSHugh Dickins 	page = swapin_readahead(entry, gfp, &pvma, 0);
11651da177e4SLinus Torvalds 	return page;
11661da177e4SLinus Torvalds }
11671da177e4SLinus Torvalds 
116802098feaSHugh Dickins static struct page *shmem_alloc_page(gfp_t gfp,
116902098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11701da177e4SLinus Torvalds {
11711da177e4SLinus Torvalds 	struct vm_area_struct pvma;
11721da177e4SLinus Torvalds 
1173c4cc6d07SHugh Dickins 	/* Create a pseudo vma that just contains the policy */
1174c4cc6d07SHugh Dickins 	pvma.vm_start = 0;
11751da177e4SLinus Torvalds 	pvma.vm_pgoff = idx;
1176c4cc6d07SHugh Dickins 	pvma.vm_ops = NULL;
1177c4cc6d07SHugh Dickins 	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
117852cd3b07SLee Schermerhorn 
117952cd3b07SLee Schermerhorn 	/*
118052cd3b07SLee Schermerhorn 	 * alloc_page_vma() will drop the shared policy reference
118152cd3b07SLee Schermerhorn 	 */
118252cd3b07SLee Schermerhorn 	return alloc_page_vma(gfp, &pvma, 0);
11831da177e4SLinus Torvalds }
1184680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */
1185680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS
118671fe804bSLee Schermerhorn static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *p)
1187680d794bSakpm@linux-foundation.org {
1188680d794bSakpm@linux-foundation.org }
1189680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
1190680d794bSakpm@linux-foundation.org 
119102098feaSHugh Dickins static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
119202098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11931da177e4SLinus Torvalds {
119402098feaSHugh Dickins 	return swapin_readahead(entry, gfp, NULL, 0);
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
119702098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp,
119802098feaSHugh Dickins 			struct shmem_inode_info *info, unsigned long idx)
11991da177e4SLinus Torvalds {
1200e84e2e13SHugh Dickins 	return alloc_page(gfp);
12011da177e4SLinus Torvalds }
1202680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */
12031da177e4SLinus Torvalds 
120471fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
120571fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
120671fe804bSLee Schermerhorn {
120771fe804bSLee Schermerhorn 	return NULL;
120871fe804bSLee Schermerhorn }
120971fe804bSLee Schermerhorn #endif
121071fe804bSLee Schermerhorn 
12111da177e4SLinus Torvalds /*
12121da177e4SLinus Torvalds  * shmem_getpage - either get the page from swap or allocate a new one
12131da177e4SLinus Torvalds  *
12141da177e4SLinus Torvalds  * If we allocate a new one we do not mark it dirty. That's up to the
12151da177e4SLinus Torvalds  * vm. If we swap it in we mark it dirty since we also free the swap
12161da177e4SLinus Torvalds  * entry since a page cannot live in both the swap and page cache
12171da177e4SLinus Torvalds  */
12181da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx,
12191da177e4SLinus Torvalds 			struct page **pagep, enum sgp_type sgp, int *type)
12201da177e4SLinus Torvalds {
12211da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
12221da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
12231da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo;
12241da177e4SLinus Torvalds 	struct page *filepage = *pagep;
12251da177e4SLinus Torvalds 	struct page *swappage;
1226ff36b801SShaohua Li 	struct page *prealloc_page = NULL;
12271da177e4SLinus Torvalds 	swp_entry_t *entry;
12281da177e4SLinus Torvalds 	swp_entry_t swap;
122902098feaSHugh Dickins 	gfp_t gfp;
12301da177e4SLinus Torvalds 	int error;
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	if (idx >= SHMEM_MAX_INDEX)
12331da177e4SLinus Torvalds 		return -EFBIG;
123454cb8821SNick Piggin 
123554cb8821SNick Piggin 	if (type)
123683c54070SNick Piggin 		*type = 0;
123754cb8821SNick Piggin 
12381da177e4SLinus Torvalds 	/*
12391da177e4SLinus Torvalds 	 * Normally, filepage is NULL on entry, and either found
12401da177e4SLinus Torvalds 	 * uptodate immediately, or allocated and zeroed, or read
12411da177e4SLinus Torvalds 	 * in under swappage, which is then assigned to filepage.
12425402b976SHugh Dickins 	 * But shmem_readpage (required for splice) passes in a locked
1243ae976416SHugh Dickins 	 * filepage, which may be found not uptodate by other callers
1244ae976416SHugh Dickins 	 * too, and may need to be copied from the swappage read in.
12451da177e4SLinus Torvalds 	 */
12461da177e4SLinus Torvalds repeat:
12471da177e4SLinus Torvalds 	if (!filepage)
12481da177e4SLinus Torvalds 		filepage = find_lock_page(mapping, idx);
12491da177e4SLinus Torvalds 	if (filepage && PageUptodate(filepage))
12501da177e4SLinus Torvalds 		goto done;
125102098feaSHugh Dickins 	gfp = mapping_gfp_mask(mapping);
1252b409f9fcSHugh Dickins 	if (!filepage) {
1253b409f9fcSHugh Dickins 		/*
1254b409f9fcSHugh Dickins 		 * Try to preload while we can wait, to not make a habit of
1255b409f9fcSHugh Dickins 		 * draining atomic reserves; but don't latch on to this cpu.
1256b409f9fcSHugh Dickins 		 */
1257b409f9fcSHugh Dickins 		error = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
1258b409f9fcSHugh Dickins 		if (error)
1259b409f9fcSHugh Dickins 			goto failed;
1260b409f9fcSHugh Dickins 		radix_tree_preload_end();
1261ff36b801SShaohua Li 		if (sgp != SGP_READ && !prealloc_page) {
1262ff36b801SShaohua Li 			/* We don't care if this fails */
1263ff36b801SShaohua Li 			prealloc_page = shmem_alloc_page(gfp, info, idx);
1264ff36b801SShaohua Li 			if (prealloc_page) {
1265ff36b801SShaohua Li 				if (mem_cgroup_cache_charge(prealloc_page,
1266ff36b801SShaohua Li 						current->mm, GFP_KERNEL)) {
1267ff36b801SShaohua Li 					page_cache_release(prealloc_page);
1268ff36b801SShaohua Li 					prealloc_page = NULL;
1269b409f9fcSHugh Dickins 				}
1270ff36b801SShaohua Li 			}
1271ff36b801SShaohua Li 		}
1272ff36b801SShaohua Li 	}
1273ff36b801SShaohua Li 	error = 0;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	spin_lock(&info->lock);
12761da177e4SLinus Torvalds 	shmem_recalc_inode(inode);
12771da177e4SLinus Torvalds 	entry = shmem_swp_alloc(info, idx, sgp);
12781da177e4SLinus Torvalds 	if (IS_ERR(entry)) {
12791da177e4SLinus Torvalds 		spin_unlock(&info->lock);
12801da177e4SLinus Torvalds 		error = PTR_ERR(entry);
12811da177e4SLinus Torvalds 		goto failed;
12821da177e4SLinus Torvalds 	}
12831da177e4SLinus Torvalds 	swap = *entry;
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	if (swap.val) {
12861da177e4SLinus Torvalds 		/* Look it up and read it in.. */
12871da177e4SLinus Torvalds 		swappage = lookup_swap_cache(swap);
12881da177e4SLinus Torvalds 		if (!swappage) {
12891da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
12901da177e4SLinus Torvalds 			/* here we actually do the io */
129183c54070SNick Piggin 			if (type && !(*type & VM_FAULT_MAJOR)) {
1292f8891e5eSChristoph Lameter 				__count_vm_event(PGMAJFAULT);
129383c54070SNick Piggin 				*type |= VM_FAULT_MAJOR;
12941da177e4SLinus Torvalds 			}
1295f8891e5eSChristoph Lameter 			spin_unlock(&info->lock);
129602098feaSHugh Dickins 			swappage = shmem_swapin(swap, gfp, info, idx);
12971da177e4SLinus Torvalds 			if (!swappage) {
12981da177e4SLinus Torvalds 				spin_lock(&info->lock);
12991da177e4SLinus Torvalds 				entry = shmem_swp_alloc(info, idx, sgp);
13001da177e4SLinus Torvalds 				if (IS_ERR(entry))
13011da177e4SLinus Torvalds 					error = PTR_ERR(entry);
13021da177e4SLinus Torvalds 				else {
13031da177e4SLinus Torvalds 					if (entry->val == swap.val)
13041da177e4SLinus Torvalds 						error = -ENOMEM;
13051da177e4SLinus Torvalds 					shmem_swp_unmap(entry);
13061da177e4SLinus Torvalds 				}
13071da177e4SLinus Torvalds 				spin_unlock(&info->lock);
13081da177e4SLinus Torvalds 				if (error)
13091da177e4SLinus Torvalds 					goto failed;
13101da177e4SLinus Torvalds 				goto repeat;
13111da177e4SLinus Torvalds 			}
13121da177e4SLinus Torvalds 			wait_on_page_locked(swappage);
13131da177e4SLinus Torvalds 			page_cache_release(swappage);
13141da177e4SLinus Torvalds 			goto repeat;
13151da177e4SLinus Torvalds 		}
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 		/* We have to do this with page locked to prevent races */
1318529ae9aaSNick Piggin 		if (!trylock_page(swappage)) {
13191da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13201da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13211da177e4SLinus Torvalds 			wait_on_page_locked(swappage);
13221da177e4SLinus Torvalds 			page_cache_release(swappage);
13231da177e4SLinus Torvalds 			goto repeat;
13241da177e4SLinus Torvalds 		}
13251da177e4SLinus Torvalds 		if (PageWriteback(swappage)) {
13261da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13271da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13281da177e4SLinus Torvalds 			wait_on_page_writeback(swappage);
13291da177e4SLinus Torvalds 			unlock_page(swappage);
13301da177e4SLinus Torvalds 			page_cache_release(swappage);
13311da177e4SLinus Torvalds 			goto repeat;
13321da177e4SLinus Torvalds 		}
13331da177e4SLinus Torvalds 		if (!PageUptodate(swappage)) {
13341da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13351da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13361da177e4SLinus Torvalds 			unlock_page(swappage);
13371da177e4SLinus Torvalds 			page_cache_release(swappage);
13381da177e4SLinus Torvalds 			error = -EIO;
13391da177e4SLinus Torvalds 			goto failed;
13401da177e4SLinus Torvalds 		}
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 		if (filepage) {
13431da177e4SLinus Torvalds 			shmem_swp_set(info, entry, 0);
13441da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13451da177e4SLinus Torvalds 			delete_from_swap_cache(swappage);
13461da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13471da177e4SLinus Torvalds 			copy_highpage(filepage, swappage);
13481da177e4SLinus Torvalds 			unlock_page(swappage);
13491da177e4SLinus Torvalds 			page_cache_release(swappage);
13501da177e4SLinus Torvalds 			flush_dcache_page(filepage);
13511da177e4SLinus Torvalds 			SetPageUptodate(filepage);
13521da177e4SLinus Torvalds 			set_page_dirty(filepage);
13531da177e4SLinus Torvalds 			swap_free(swap);
1354e286781dSNick Piggin 		} else if (!(error = add_to_page_cache_locked(swappage, mapping,
1355e286781dSNick Piggin 					idx, GFP_NOWAIT))) {
13561da177e4SLinus Torvalds 			info->flags |= SHMEM_PAGEIN;
13571da177e4SLinus Torvalds 			shmem_swp_set(info, entry, 0);
13581da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
135973b1262fSHugh Dickins 			delete_from_swap_cache(swappage);
13601da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13611da177e4SLinus Torvalds 			filepage = swappage;
136273b1262fSHugh Dickins 			set_page_dirty(filepage);
13631da177e4SLinus Torvalds 			swap_free(swap);
13641da177e4SLinus Torvalds 		} else {
13651da177e4SLinus Torvalds 			shmem_swp_unmap(entry);
13661da177e4SLinus Torvalds 			spin_unlock(&info->lock);
136782369553SHugh Dickins 			if (error == -ENOMEM) {
1368ae3abae6SDaisuke Nishimura 				/*
1369ae3abae6SDaisuke Nishimura 				 * reclaim from proper memory cgroup and
1370ae3abae6SDaisuke Nishimura 				 * call memcg's OOM if needed.
1371ae3abae6SDaisuke Nishimura 				 */
1372ae3abae6SDaisuke Nishimura 				error = mem_cgroup_shmem_charge_fallback(
1373ae3abae6SDaisuke Nishimura 								swappage,
1374b5a84319SKAMEZAWA Hiroyuki 								current->mm,
1375c9b0ed51SKAMEZAWA Hiroyuki 								gfp);
1376b5a84319SKAMEZAWA Hiroyuki 				if (error) {
1377b5a84319SKAMEZAWA Hiroyuki 					unlock_page(swappage);
1378b5a84319SKAMEZAWA Hiroyuki 					page_cache_release(swappage);
137982369553SHugh Dickins 					goto failed;
138082369553SHugh Dickins 				}
1381b5a84319SKAMEZAWA Hiroyuki 			}
1382b5a84319SKAMEZAWA Hiroyuki 			unlock_page(swappage);
1383b5a84319SKAMEZAWA Hiroyuki 			page_cache_release(swappage);
13841da177e4SLinus Torvalds 			goto repeat;
13851da177e4SLinus Torvalds 		}
13861da177e4SLinus Torvalds 	} else if (sgp == SGP_READ && !filepage) {
13871da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
13881da177e4SLinus Torvalds 		filepage = find_get_page(mapping, idx);
13891da177e4SLinus Torvalds 		if (filepage &&
1390529ae9aaSNick Piggin 		    (!PageUptodate(filepage) || !trylock_page(filepage))) {
13911da177e4SLinus Torvalds 			spin_unlock(&info->lock);
13921da177e4SLinus Torvalds 			wait_on_page_locked(filepage);
13931da177e4SLinus Torvalds 			page_cache_release(filepage);
13941da177e4SLinus Torvalds 			filepage = NULL;
13951da177e4SLinus Torvalds 			goto repeat;
13961da177e4SLinus Torvalds 		}
13971da177e4SLinus Torvalds 		spin_unlock(&info->lock);
13981da177e4SLinus Torvalds 	} else {
13991da177e4SLinus Torvalds 		shmem_swp_unmap(entry);
14001da177e4SLinus Torvalds 		sbinfo = SHMEM_SB(inode->i_sb);
14010edd73b3SHugh Dickins 		if (sbinfo->max_blocks) {
14027e496299STim Chen 			if ((percpu_counter_compare(&sbinfo->used_blocks, sbinfo->max_blocks) > 0) ||
14031da177e4SLinus Torvalds 			    shmem_acct_block(info->flags)) {
14041da177e4SLinus Torvalds 				spin_unlock(&info->lock);
14051da177e4SLinus Torvalds 				error = -ENOSPC;
14061da177e4SLinus Torvalds 				goto failed;
14071da177e4SLinus Torvalds 			}
14087e496299STim Chen 			percpu_counter_inc(&sbinfo->used_blocks);
14097e496299STim Chen 			spin_lock(&inode->i_lock);
14101da177e4SLinus Torvalds 			inode->i_blocks += BLOCKS_PER_PAGE;
14117e496299STim Chen 			spin_unlock(&inode->i_lock);
14121da177e4SLinus Torvalds 		} else if (shmem_acct_block(info->flags)) {
14131da177e4SLinus Torvalds 			spin_unlock(&info->lock);
14141da177e4SLinus Torvalds 			error = -ENOSPC;
14151da177e4SLinus Torvalds 			goto failed;
14161da177e4SLinus Torvalds 		}
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 		if (!filepage) {
141969029cd5SKAMEZAWA Hiroyuki 			int ret;
142069029cd5SKAMEZAWA Hiroyuki 
1421ff36b801SShaohua Li 			if (!prealloc_page) {
14221da177e4SLinus Torvalds 				spin_unlock(&info->lock);
142302098feaSHugh Dickins 				filepage = shmem_alloc_page(gfp, info, idx);
14241da177e4SLinus Torvalds 				if (!filepage) {
14251da177e4SLinus Torvalds 					shmem_unacct_blocks(info->flags, 1);
14261da177e4SLinus Torvalds 					shmem_free_blocks(inode, 1);
14271da177e4SLinus Torvalds 					error = -ENOMEM;
14281da177e4SLinus Torvalds 					goto failed;
14291da177e4SLinus Torvalds 				}
1430b2e18538SRik van Riel 				SetPageSwapBacked(filepage);
14311da177e4SLinus Torvalds 
1432ff36b801SShaohua Li 				/*
1433ff36b801SShaohua Li 				 * Precharge page while we can wait, compensate
1434ff36b801SShaohua Li 				 * after
1435ff36b801SShaohua Li 				 */
1436ff36b801SShaohua Li 				error = mem_cgroup_cache_charge(filepage,
1437ff36b801SShaohua Li 					current->mm, GFP_KERNEL);
143882369553SHugh Dickins 				if (error) {
143982369553SHugh Dickins 					page_cache_release(filepage);
144082369553SHugh Dickins 					shmem_unacct_blocks(info->flags, 1);
144182369553SHugh Dickins 					shmem_free_blocks(inode, 1);
144282369553SHugh Dickins 					filepage = NULL;
144382369553SHugh Dickins 					goto failed;
144482369553SHugh Dickins 				}
144582369553SHugh Dickins 
14461da177e4SLinus Torvalds 				spin_lock(&info->lock);
1447ff36b801SShaohua Li 			} else {
1448ff36b801SShaohua Li 				filepage = prealloc_page;
1449ff36b801SShaohua Li 				prealloc_page = NULL;
1450ff36b801SShaohua Li 				SetPageSwapBacked(filepage);
1451ff36b801SShaohua Li 			}
1452ff36b801SShaohua Li 
14531da177e4SLinus Torvalds 			entry = shmem_swp_alloc(info, idx, sgp);
14541da177e4SLinus Torvalds 			if (IS_ERR(entry))
14551da177e4SLinus Torvalds 				error = PTR_ERR(entry);
14561da177e4SLinus Torvalds 			else {
14571da177e4SLinus Torvalds 				swap = *entry;
14581da177e4SLinus Torvalds 				shmem_swp_unmap(entry);
14591da177e4SLinus Torvalds 			}
146069029cd5SKAMEZAWA Hiroyuki 			ret = error || swap.val;
146169029cd5SKAMEZAWA Hiroyuki 			if (ret)
146269029cd5SKAMEZAWA Hiroyuki 				mem_cgroup_uncharge_cache_page(filepage);
146369029cd5SKAMEZAWA Hiroyuki 			else
146469029cd5SKAMEZAWA Hiroyuki 				ret = add_to_page_cache_lru(filepage, mapping,
146569029cd5SKAMEZAWA Hiroyuki 						idx, GFP_NOWAIT);
146669029cd5SKAMEZAWA Hiroyuki 			/*
146769029cd5SKAMEZAWA Hiroyuki 			 * At add_to_page_cache_lru() failure, uncharge will
146869029cd5SKAMEZAWA Hiroyuki 			 * be done automatically.
146969029cd5SKAMEZAWA Hiroyuki 			 */
147069029cd5SKAMEZAWA Hiroyuki 			if (ret) {
14711da177e4SLinus Torvalds 				spin_unlock(&info->lock);
14721da177e4SLinus Torvalds 				page_cache_release(filepage);
14731da177e4SLinus Torvalds 				shmem_unacct_blocks(info->flags, 1);
14741da177e4SLinus Torvalds 				shmem_free_blocks(inode, 1);
14751da177e4SLinus Torvalds 				filepage = NULL;
14761da177e4SLinus Torvalds 				if (error)
14771da177e4SLinus Torvalds 					goto failed;
14781da177e4SLinus Torvalds 				goto repeat;
14791da177e4SLinus Torvalds 			}
14801da177e4SLinus Torvalds 			info->flags |= SHMEM_PAGEIN;
14811da177e4SLinus Torvalds 		}
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 		info->alloced++;
14841da177e4SLinus Torvalds 		spin_unlock(&info->lock);
1485e84e2e13SHugh Dickins 		clear_highpage(filepage);
14861da177e4SLinus Torvalds 		flush_dcache_page(filepage);
14871da177e4SLinus Torvalds 		SetPageUptodate(filepage);
1488a0ee5ec5SHugh Dickins 		if (sgp == SGP_DIRTY)
1489a0ee5ec5SHugh Dickins 			set_page_dirty(filepage);
14901da177e4SLinus Torvalds 	}
14911da177e4SLinus Torvalds done:
14921da177e4SLinus Torvalds 	*pagep = filepage;
1493ff36b801SShaohua Li 	error = 0;
1494ff36b801SShaohua Li 	goto out;
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds failed:
14971da177e4SLinus Torvalds 	if (*pagep != filepage) {
14981da177e4SLinus Torvalds 		unlock_page(filepage);
14991da177e4SLinus Torvalds 		page_cache_release(filepage);
15001da177e4SLinus Torvalds 	}
1501ff36b801SShaohua Li out:
1502ff36b801SShaohua Li 	if (prealloc_page) {
1503ff36b801SShaohua Li 		mem_cgroup_uncharge_cache_page(prealloc_page);
1504ff36b801SShaohua Li 		page_cache_release(prealloc_page);
1505ff36b801SShaohua Li 	}
15061da177e4SLinus Torvalds 	return error;
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds 
1509d0217ac0SNick Piggin static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
15101da177e4SLinus Torvalds {
1511d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
15121da177e4SLinus Torvalds 	int error;
1513d0217ac0SNick Piggin 	int ret;
15141da177e4SLinus Torvalds 
1515d0217ac0SNick Piggin 	if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1516d0217ac0SNick Piggin 		return VM_FAULT_SIGBUS;
1517d00806b1SNick Piggin 
151827d54b39SHugh Dickins 	error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1519d0217ac0SNick Piggin 	if (error)
1520d0217ac0SNick Piggin 		return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
15211da177e4SLinus Torvalds 
152283c54070SNick Piggin 	return ret | VM_FAULT_LOCKED;
15231da177e4SLinus Torvalds }
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1526d8dc74f2SAdrian Bunk static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
15271da177e4SLinus Torvalds {
1528d3ac7f89SJosef "Jeff" Sipek 	struct inode *i = vma->vm_file->f_path.dentry->d_inode;
15291da177e4SLinus Torvalds 	return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
15301da177e4SLinus Torvalds }
15311da177e4SLinus Torvalds 
1532d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1533d8dc74f2SAdrian Bunk 					  unsigned long addr)
15341da177e4SLinus Torvalds {
1535d3ac7f89SJosef "Jeff" Sipek 	struct inode *i = vma->vm_file->f_path.dentry->d_inode;
15361da177e4SLinus Torvalds 	unsigned long idx;
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 	idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
15391da177e4SLinus Torvalds 	return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
15401da177e4SLinus Torvalds }
15411da177e4SLinus Torvalds #endif
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user)
15441da177e4SLinus Torvalds {
1545d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
15461da177e4SLinus Torvalds 	struct shmem_inode_info *info = SHMEM_I(inode);
15471da177e4SLinus Torvalds 	int retval = -ENOMEM;
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	spin_lock(&info->lock);
15501da177e4SLinus Torvalds 	if (lock && !(info->flags & VM_LOCKED)) {
15511da177e4SLinus Torvalds 		if (!user_shm_lock(inode->i_size, user))
15521da177e4SLinus Torvalds 			goto out_nomem;
15531da177e4SLinus Torvalds 		info->flags |= VM_LOCKED;
155489e004eaSLee Schermerhorn 		mapping_set_unevictable(file->f_mapping);
15551da177e4SLinus Torvalds 	}
15561da177e4SLinus Torvalds 	if (!lock && (info->flags & VM_LOCKED) && user) {
15571da177e4SLinus Torvalds 		user_shm_unlock(inode->i_size, user);
15581da177e4SLinus Torvalds 		info->flags &= ~VM_LOCKED;
155989e004eaSLee Schermerhorn 		mapping_clear_unevictable(file->f_mapping);
156089e004eaSLee Schermerhorn 		scan_mapping_unevictable_pages(file->f_mapping);
15611da177e4SLinus Torvalds 	}
15621da177e4SLinus Torvalds 	retval = 0;
156389e004eaSLee Schermerhorn 
15641da177e4SLinus Torvalds out_nomem:
15651da177e4SLinus Torvalds 	spin_unlock(&info->lock);
15661da177e4SLinus Torvalds 	return retval;
15671da177e4SLinus Torvalds }
15681da177e4SLinus Torvalds 
15699b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
15701da177e4SLinus Torvalds {
15711da177e4SLinus Torvalds 	file_accessed(file);
15721da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
1573d0217ac0SNick Piggin 	vma->vm_flags |= VM_CAN_NONLINEAR;
15741da177e4SLinus Torvalds 	return 0;
15751da177e4SLinus Torvalds }
15761da177e4SLinus Torvalds 
1577454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1578454abafeSDmitry Monakhov 				     int mode, dev_t dev, unsigned long flags)
15791da177e4SLinus Torvalds {
15801da177e4SLinus Torvalds 	struct inode *inode;
15811da177e4SLinus Torvalds 	struct shmem_inode_info *info;
15821da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
15831da177e4SLinus Torvalds 
15845b04c689SPavel Emelyanov 	if (shmem_reserve_inode(sb))
15851da177e4SLinus Torvalds 		return NULL;
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 	inode = new_inode(sb);
15881da177e4SLinus Torvalds 	if (inode) {
158985fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
1590454abafeSDmitry Monakhov 		inode_init_owner(inode, dir, mode);
15911da177e4SLinus Torvalds 		inode->i_blocks = 0;
15921da177e4SLinus Torvalds 		inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
15931da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
159491828a40SDavid M. Grimes 		inode->i_generation = get_seconds();
15951da177e4SLinus Torvalds 		info = SHMEM_I(inode);
15961da177e4SLinus Torvalds 		memset(info, 0, (char *)inode - (char *)info);
15971da177e4SLinus Torvalds 		spin_lock_init(&info->lock);
15980b0a0806SHugh Dickins 		info->flags = flags & VM_NORESERVE;
15991da177e4SLinus Torvalds 		INIT_LIST_HEAD(&info->swaplist);
160072c04902SAl Viro 		cache_no_acl(inode);
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
16031da177e4SLinus Torvalds 		default:
160439f0247dSAndreas Gruenbacher 			inode->i_op = &shmem_special_inode_operations;
16051da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
16061da177e4SLinus Torvalds 			break;
16071da177e4SLinus Torvalds 		case S_IFREG:
160814fcc23fSHugh Dickins 			inode->i_mapping->a_ops = &shmem_aops;
16091da177e4SLinus Torvalds 			inode->i_op = &shmem_inode_operations;
16101da177e4SLinus Torvalds 			inode->i_fop = &shmem_file_operations;
161171fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy,
161271fe804bSLee Schermerhorn 						 shmem_get_sbmpol(sbinfo));
16131da177e4SLinus Torvalds 			break;
16141da177e4SLinus Torvalds 		case S_IFDIR:
1615d8c76e6fSDave Hansen 			inc_nlink(inode);
16161da177e4SLinus Torvalds 			/* Some things misbehave if size == 0 on a directory */
16171da177e4SLinus Torvalds 			inode->i_size = 2 * BOGO_DIRENT_SIZE;
16181da177e4SLinus Torvalds 			inode->i_op = &shmem_dir_inode_operations;
16191da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
16201da177e4SLinus Torvalds 			break;
16211da177e4SLinus Torvalds 		case S_IFLNK:
16221da177e4SLinus Torvalds 			/*
16231da177e4SLinus Torvalds 			 * Must not load anything in the rbtree,
16241da177e4SLinus Torvalds 			 * mpol_free_shared_policy will not be called.
16251da177e4SLinus Torvalds 			 */
162671fe804bSLee Schermerhorn 			mpol_shared_policy_init(&info->policy, NULL);
16271da177e4SLinus Torvalds 			break;
16281da177e4SLinus Torvalds 		}
16295b04c689SPavel Emelyanov 	} else
16305b04c689SPavel Emelyanov 		shmem_free_inode(sb);
16311da177e4SLinus Torvalds 	return inode;
16321da177e4SLinus Torvalds }
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
163592e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations;
163692e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations;
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds /*
1639800d15a5SNick Piggin  * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
1640ae976416SHugh Dickins  * but providing them allows a tmpfs file to be used for splice, sendfile, and
1641ae976416SHugh Dickins  * below the loop driver, in the generic fashion that many filesystems support.
16421da177e4SLinus Torvalds  */
1643ae976416SHugh Dickins static int shmem_readpage(struct file *file, struct page *page)
1644ae976416SHugh Dickins {
1645ae976416SHugh Dickins 	struct inode *inode = page->mapping->host;
1646ae976416SHugh Dickins 	int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
1647ae976416SHugh Dickins 	unlock_page(page);
1648ae976416SHugh Dickins 	return error;
1649ae976416SHugh Dickins }
1650ae976416SHugh Dickins 
16511da177e4SLinus Torvalds static int
1652800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping,
1653800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
1654800d15a5SNick Piggin 			struct page **pagep, void **fsdata)
16551da177e4SLinus Torvalds {
1656800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1657800d15a5SNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1658800d15a5SNick Piggin 	*pagep = NULL;
1659800d15a5SNick Piggin 	return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1660800d15a5SNick Piggin }
1661800d15a5SNick Piggin 
1662800d15a5SNick Piggin static int
1663800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping,
1664800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
1665800d15a5SNick Piggin 			struct page *page, void *fsdata)
1666800d15a5SNick Piggin {
1667800d15a5SNick Piggin 	struct inode *inode = mapping->host;
1668800d15a5SNick Piggin 
1669800d15a5SNick Piggin 	if (pos + copied > inode->i_size)
1670800d15a5SNick Piggin 		i_size_write(inode, pos + copied);
1671800d15a5SNick Piggin 
1672d3602444SHugh Dickins 	set_page_dirty(page);
16736746aff7SWu Fengguang 	unlock_page(page);
1674d3602444SHugh Dickins 	page_cache_release(page);
1675d3602444SHugh Dickins 
1676800d15a5SNick Piggin 	return copied;
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
16801da177e4SLinus Torvalds {
1681d3ac7f89SJosef "Jeff" Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
16821da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
16831da177e4SLinus Torvalds 	unsigned long index, offset;
1684a0ee5ec5SHugh Dickins 	enum sgp_type sgp = SGP_READ;
1685a0ee5ec5SHugh Dickins 
1686a0ee5ec5SHugh Dickins 	/*
1687a0ee5ec5SHugh Dickins 	 * Might this read be for a stacking filesystem?  Then when reading
1688a0ee5ec5SHugh Dickins 	 * holes of a sparse file, we actually need to allocate those pages,
1689a0ee5ec5SHugh Dickins 	 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1690a0ee5ec5SHugh Dickins 	 */
1691a0ee5ec5SHugh Dickins 	if (segment_eq(get_fs(), KERNEL_DS))
1692a0ee5ec5SHugh Dickins 		sgp = SGP_DIRTY;
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	index = *ppos >> PAGE_CACHE_SHIFT;
16951da177e4SLinus Torvalds 	offset = *ppos & ~PAGE_CACHE_MASK;
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 	for (;;) {
16981da177e4SLinus Torvalds 		struct page *page = NULL;
16991da177e4SLinus Torvalds 		unsigned long end_index, nr, ret;
17001da177e4SLinus Torvalds 		loff_t i_size = i_size_read(inode);
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
17031da177e4SLinus Torvalds 		if (index > end_index)
17041da177e4SLinus Torvalds 			break;
17051da177e4SLinus Torvalds 		if (index == end_index) {
17061da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
17071da177e4SLinus Torvalds 			if (nr <= offset)
17081da177e4SLinus Torvalds 				break;
17091da177e4SLinus Torvalds 		}
17101da177e4SLinus Torvalds 
1711a0ee5ec5SHugh Dickins 		desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
17121da177e4SLinus Torvalds 		if (desc->error) {
17131da177e4SLinus Torvalds 			if (desc->error == -EINVAL)
17141da177e4SLinus Torvalds 				desc->error = 0;
17151da177e4SLinus Torvalds 			break;
17161da177e4SLinus Torvalds 		}
1717d3602444SHugh Dickins 		if (page)
1718d3602444SHugh Dickins 			unlock_page(page);
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 		/*
17211da177e4SLinus Torvalds 		 * We must evaluate after, since reads (unlike writes)
17221b1dcc1bSJes Sorensen 		 * are called without i_mutex protection against truncate
17231da177e4SLinus Torvalds 		 */
17241da177e4SLinus Torvalds 		nr = PAGE_CACHE_SIZE;
17251da177e4SLinus Torvalds 		i_size = i_size_read(inode);
17261da177e4SLinus Torvalds 		end_index = i_size >> PAGE_CACHE_SHIFT;
17271da177e4SLinus Torvalds 		if (index == end_index) {
17281da177e4SLinus Torvalds 			nr = i_size & ~PAGE_CACHE_MASK;
17291da177e4SLinus Torvalds 			if (nr <= offset) {
17301da177e4SLinus Torvalds 				if (page)
17311da177e4SLinus Torvalds 					page_cache_release(page);
17321da177e4SLinus Torvalds 				break;
17331da177e4SLinus Torvalds 			}
17341da177e4SLinus Torvalds 		}
17351da177e4SLinus Torvalds 		nr -= offset;
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 		if (page) {
17381da177e4SLinus Torvalds 			/*
17391da177e4SLinus Torvalds 			 * If users can be writing to this page using arbitrary
17401da177e4SLinus Torvalds 			 * virtual addresses, take care about potential aliasing
17411da177e4SLinus Torvalds 			 * before reading the page on the kernel side.
17421da177e4SLinus Torvalds 			 */
17431da177e4SLinus Torvalds 			if (mapping_writably_mapped(mapping))
17441da177e4SLinus Torvalds 				flush_dcache_page(page);
17451da177e4SLinus Torvalds 			/*
17461da177e4SLinus Torvalds 			 * Mark the page accessed if we read the beginning.
17471da177e4SLinus Torvalds 			 */
17481da177e4SLinus Torvalds 			if (!offset)
17491da177e4SLinus Torvalds 				mark_page_accessed(page);
1750b5810039SNick Piggin 		} else {
17511da177e4SLinus Torvalds 			page = ZERO_PAGE(0);
1752b5810039SNick Piggin 			page_cache_get(page);
1753b5810039SNick Piggin 		}
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 		/*
17561da177e4SLinus Torvalds 		 * Ok, we have the page, and it's up-to-date, so
17571da177e4SLinus Torvalds 		 * now we can copy it to user space...
17581da177e4SLinus Torvalds 		 *
17591da177e4SLinus Torvalds 		 * The actor routine returns how many bytes were actually used..
17601da177e4SLinus Torvalds 		 * NOTE! This may not be the same as how much of a user buffer
17611da177e4SLinus Torvalds 		 * we filled up (we may be padding etc), so we can only update
17621da177e4SLinus Torvalds 		 * "pos" here (the actor routine has to update the user buffer
17631da177e4SLinus Torvalds 		 * pointers and the remaining count).
17641da177e4SLinus Torvalds 		 */
17651da177e4SLinus Torvalds 		ret = actor(desc, page, offset, nr);
17661da177e4SLinus Torvalds 		offset += ret;
17671da177e4SLinus Torvalds 		index += offset >> PAGE_CACHE_SHIFT;
17681da177e4SLinus Torvalds 		offset &= ~PAGE_CACHE_MASK;
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 		page_cache_release(page);
17711da177e4SLinus Torvalds 		if (ret != nr || !desc->count)
17721da177e4SLinus Torvalds 			break;
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 		cond_resched();
17751da177e4SLinus Torvalds 	}
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
17781da177e4SLinus Torvalds 	file_accessed(filp);
17791da177e4SLinus Torvalds }
17801da177e4SLinus Torvalds 
1781bcd78e49SHugh Dickins static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1782bcd78e49SHugh Dickins 		const struct iovec *iov, unsigned long nr_segs, loff_t pos)
17831da177e4SLinus Torvalds {
1784bcd78e49SHugh Dickins 	struct file *filp = iocb->ki_filp;
1785bcd78e49SHugh Dickins 	ssize_t retval;
1786bcd78e49SHugh Dickins 	unsigned long seg;
1787bcd78e49SHugh Dickins 	size_t count;
1788bcd78e49SHugh Dickins 	loff_t *ppos = &iocb->ki_pos;
1789bcd78e49SHugh Dickins 
1790bcd78e49SHugh Dickins 	retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1791bcd78e49SHugh Dickins 	if (retval)
1792bcd78e49SHugh Dickins 		return retval;
1793bcd78e49SHugh Dickins 
1794bcd78e49SHugh Dickins 	for (seg = 0; seg < nr_segs; seg++) {
17951da177e4SLinus Torvalds 		read_descriptor_t desc;
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds 		desc.written = 0;
1798bcd78e49SHugh Dickins 		desc.arg.buf = iov[seg].iov_base;
1799bcd78e49SHugh Dickins 		desc.count = iov[seg].iov_len;
1800bcd78e49SHugh Dickins 		if (desc.count == 0)
1801bcd78e49SHugh Dickins 			continue;
18021da177e4SLinus Torvalds 		desc.error = 0;
18031da177e4SLinus Torvalds 		do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1804bcd78e49SHugh Dickins 		retval += desc.written;
1805bcd78e49SHugh Dickins 		if (desc.error) {
1806bcd78e49SHugh Dickins 			retval = retval ?: desc.error;
1807bcd78e49SHugh Dickins 			break;
1808bcd78e49SHugh Dickins 		}
1809bcd78e49SHugh Dickins 		if (desc.count > 0)
1810bcd78e49SHugh Dickins 			break;
1811bcd78e49SHugh Dickins 	}
1812bcd78e49SHugh Dickins 	return retval;
18131da177e4SLinus Torvalds }
18141da177e4SLinus Torvalds 
1815726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
18161da177e4SLinus Torvalds {
1817726c3342SDavid Howells 	struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds 	buf->f_type = TMPFS_MAGIC;
18201da177e4SLinus Torvalds 	buf->f_bsize = PAGE_CACHE_SIZE;
18211da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
18220edd73b3SHugh Dickins 	if (sbinfo->max_blocks) {
18231da177e4SLinus Torvalds 		buf->f_blocks = sbinfo->max_blocks;
18247e496299STim Chen 		buf->f_bavail = buf->f_bfree =
18257e496299STim Chen 				sbinfo->max_blocks - percpu_counter_sum(&sbinfo->used_blocks);
18260edd73b3SHugh Dickins 	}
18270edd73b3SHugh Dickins 	if (sbinfo->max_inodes) {
18281da177e4SLinus Torvalds 		buf->f_files = sbinfo->max_inodes;
18291da177e4SLinus Torvalds 		buf->f_ffree = sbinfo->free_inodes;
18301da177e4SLinus Torvalds 	}
18311da177e4SLinus Torvalds 	/* else leave those fields 0 like simple_statfs */
18321da177e4SLinus Torvalds 	return 0;
18331da177e4SLinus Torvalds }
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds /*
18361da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
18371da177e4SLinus Torvalds  */
18381da177e4SLinus Torvalds static int
18391da177e4SLinus Torvalds shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
18401da177e4SLinus Torvalds {
18410b0a0806SHugh Dickins 	struct inode *inode;
18421da177e4SLinus Torvalds 	int error = -ENOSPC;
18431da177e4SLinus Torvalds 
1844454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
18451da177e4SLinus Torvalds 	if (inode) {
1846570bc1c2SStephen Smalley 		error = security_inode_init_security(inode, dir, NULL, NULL,
1847570bc1c2SStephen Smalley 						     NULL);
1848570bc1c2SStephen Smalley 		if (error) {
1849570bc1c2SStephen Smalley 			if (error != -EOPNOTSUPP) {
1850570bc1c2SStephen Smalley 				iput(inode);
1851570bc1c2SStephen Smalley 				return error;
1852570bc1c2SStephen Smalley 			}
185339f0247dSAndreas Gruenbacher 		}
18541c7c474cSChristoph Hellwig #ifdef CONFIG_TMPFS_POSIX_ACL
18551c7c474cSChristoph Hellwig 		error = generic_acl_init(inode, dir);
185639f0247dSAndreas Gruenbacher 		if (error) {
185739f0247dSAndreas Gruenbacher 			iput(inode);
185839f0247dSAndreas Gruenbacher 			return error;
1859570bc1c2SStephen Smalley 		}
1860718deb6bSAl Viro #else
1861718deb6bSAl Viro 		error = 0;
18621c7c474cSChristoph Hellwig #endif
18631da177e4SLinus Torvalds 		dir->i_size += BOGO_DIRENT_SIZE;
18641da177e4SLinus Torvalds 		dir->i_ctime = dir->i_mtime = CURRENT_TIME;
18651da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
18661da177e4SLinus Torvalds 		dget(dentry); /* Extra count - pin the dentry in core */
18671da177e4SLinus Torvalds 	}
18681da177e4SLinus Torvalds 	return error;
18691da177e4SLinus Torvalds }
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
18721da177e4SLinus Torvalds {
18731da177e4SLinus Torvalds 	int error;
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds 	if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
18761da177e4SLinus Torvalds 		return error;
1877d8c76e6fSDave Hansen 	inc_nlink(dir);
18781da177e4SLinus Torvalds 	return 0;
18791da177e4SLinus Torvalds }
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
18821da177e4SLinus Torvalds 		struct nameidata *nd)
18831da177e4SLinus Torvalds {
18841da177e4SLinus Torvalds 	return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
18851da177e4SLinus Torvalds }
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds /*
18881da177e4SLinus Torvalds  * Link a file..
18891da177e4SLinus Torvalds  */
18901da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
18911da177e4SLinus Torvalds {
18921da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
18935b04c689SPavel Emelyanov 	int ret;
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	/*
18961da177e4SLinus Torvalds 	 * No ordinary (disk based) filesystem counts links as inodes;
18971da177e4SLinus Torvalds 	 * but each new link needs a new dentry, pinning lowmem, and
18981da177e4SLinus Torvalds 	 * tmpfs dentries cannot be pruned until they are unlinked.
18991da177e4SLinus Torvalds 	 */
19005b04c689SPavel Emelyanov 	ret = shmem_reserve_inode(inode->i_sb);
19015b04c689SPavel Emelyanov 	if (ret)
19025b04c689SPavel Emelyanov 		goto out;
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
19051da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1906d8c76e6fSDave Hansen 	inc_nlink(inode);
19077de9c6eeSAl Viro 	ihold(inode);	/* New dentry reference */
19081da177e4SLinus Torvalds 	dget(dentry);		/* Extra pinning count for the created dentry */
19091da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
19105b04c689SPavel Emelyanov out:
19115b04c689SPavel Emelyanov 	return ret;
19121da177e4SLinus Torvalds }
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry)
19151da177e4SLinus Torvalds {
19161da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19171da177e4SLinus Torvalds 
19185b04c689SPavel Emelyanov 	if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
19195b04c689SPavel Emelyanov 		shmem_free_inode(inode->i_sb);
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 	dir->i_size -= BOGO_DIRENT_SIZE;
19221da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
19239a53c3a7SDave Hansen 	drop_nlink(inode);
19241da177e4SLinus Torvalds 	dput(dentry);	/* Undo the count from "create" - this does all the work */
19251da177e4SLinus Torvalds 	return 0;
19261da177e4SLinus Torvalds }
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
19291da177e4SLinus Torvalds {
19301da177e4SLinus Torvalds 	if (!simple_empty(dentry))
19311da177e4SLinus Torvalds 		return -ENOTEMPTY;
19321da177e4SLinus Torvalds 
19339a53c3a7SDave Hansen 	drop_nlink(dentry->d_inode);
19349a53c3a7SDave Hansen 	drop_nlink(dir);
19351da177e4SLinus Torvalds 	return shmem_unlink(dir, dentry);
19361da177e4SLinus Torvalds }
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds /*
19391da177e4SLinus Torvalds  * The VFS layer already does all the dentry stuff for rename,
19401da177e4SLinus Torvalds  * we just have to decrement the usage count for the target if
19411da177e4SLinus Torvalds  * it exists so that the VFS layer correctly free's it when it
19421da177e4SLinus Torvalds  * gets overwritten.
19431da177e4SLinus Torvalds  */
19441da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
19451da177e4SLinus Torvalds {
19461da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
19471da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(inode->i_mode);
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
19501da177e4SLinus Torvalds 		return -ENOTEMPTY;
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
19531da177e4SLinus Torvalds 		(void) shmem_unlink(new_dir, new_dentry);
19541da177e4SLinus Torvalds 		if (they_are_dirs)
19559a53c3a7SDave Hansen 			drop_nlink(old_dir);
19561da177e4SLinus Torvalds 	} else if (they_are_dirs) {
19579a53c3a7SDave Hansen 		drop_nlink(old_dir);
1958d8c76e6fSDave Hansen 		inc_nlink(new_dir);
19591da177e4SLinus Torvalds 	}
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	old_dir->i_size -= BOGO_DIRENT_SIZE;
19621da177e4SLinus Torvalds 	new_dir->i_size += BOGO_DIRENT_SIZE;
19631da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime =
19641da177e4SLinus Torvalds 	new_dir->i_ctime = new_dir->i_mtime =
19651da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME;
19661da177e4SLinus Torvalds 	return 0;
19671da177e4SLinus Torvalds }
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
19701da177e4SLinus Torvalds {
19711da177e4SLinus Torvalds 	int error;
19721da177e4SLinus Torvalds 	int len;
19731da177e4SLinus Torvalds 	struct inode *inode;
19741da177e4SLinus Torvalds 	struct page *page = NULL;
19751da177e4SLinus Torvalds 	char *kaddr;
19761da177e4SLinus Torvalds 	struct shmem_inode_info *info;
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds 	len = strlen(symname) + 1;
19791da177e4SLinus Torvalds 	if (len > PAGE_CACHE_SIZE)
19801da177e4SLinus Torvalds 		return -ENAMETOOLONG;
19811da177e4SLinus Torvalds 
1982454abafeSDmitry Monakhov 	inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
19831da177e4SLinus Torvalds 	if (!inode)
19841da177e4SLinus Torvalds 		return -ENOSPC;
19851da177e4SLinus Torvalds 
1986570bc1c2SStephen Smalley 	error = security_inode_init_security(inode, dir, NULL, NULL,
1987570bc1c2SStephen Smalley 					     NULL);
1988570bc1c2SStephen Smalley 	if (error) {
1989570bc1c2SStephen Smalley 		if (error != -EOPNOTSUPP) {
1990570bc1c2SStephen Smalley 			iput(inode);
1991570bc1c2SStephen Smalley 			return error;
1992570bc1c2SStephen Smalley 		}
1993570bc1c2SStephen Smalley 		error = 0;
1994570bc1c2SStephen Smalley 	}
1995570bc1c2SStephen Smalley 
19961da177e4SLinus Torvalds 	info = SHMEM_I(inode);
19971da177e4SLinus Torvalds 	inode->i_size = len-1;
19981da177e4SLinus Torvalds 	if (len <= (char *)inode - (char *)info) {
19991da177e4SLinus Torvalds 		/* do it inline */
20001da177e4SLinus Torvalds 		memcpy(info, symname, len);
20011da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inline_operations;
20021da177e4SLinus Torvalds 	} else {
20031da177e4SLinus Torvalds 		error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
20041da177e4SLinus Torvalds 		if (error) {
20051da177e4SLinus Torvalds 			iput(inode);
20061da177e4SLinus Torvalds 			return error;
20071da177e4SLinus Torvalds 		}
200814fcc23fSHugh Dickins 		inode->i_mapping->a_ops = &shmem_aops;
20091da177e4SLinus Torvalds 		inode->i_op = &shmem_symlink_inode_operations;
20101da177e4SLinus Torvalds 		kaddr = kmap_atomic(page, KM_USER0);
20111da177e4SLinus Torvalds 		memcpy(kaddr, symname, len);
20121da177e4SLinus Torvalds 		kunmap_atomic(kaddr, KM_USER0);
20131da177e4SLinus Torvalds 		set_page_dirty(page);
20146746aff7SWu Fengguang 		unlock_page(page);
20151da177e4SLinus Torvalds 		page_cache_release(page);
20161da177e4SLinus Torvalds 	}
20171da177e4SLinus Torvalds 	dir->i_size += BOGO_DIRENT_SIZE;
20181da177e4SLinus Torvalds 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
20191da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
20201da177e4SLinus Torvalds 	dget(dentry);
20211da177e4SLinus Torvalds 	return 0;
20221da177e4SLinus Torvalds }
20231da177e4SLinus Torvalds 
2024cc314eefSLinus Torvalds static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
20251da177e4SLinus Torvalds {
20261da177e4SLinus Torvalds 	nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
2027cc314eefSLinus Torvalds 	return NULL;
20281da177e4SLinus Torvalds }
20291da177e4SLinus Torvalds 
2030cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
20311da177e4SLinus Torvalds {
20321da177e4SLinus Torvalds 	struct page *page = NULL;
20331da177e4SLinus Torvalds 	int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
20341da177e4SLinus Torvalds 	nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
2035d3602444SHugh Dickins 	if (page)
2036d3602444SHugh Dickins 		unlock_page(page);
2037cc314eefSLinus Torvalds 	return page;
20381da177e4SLinus Torvalds }
20391da177e4SLinus Torvalds 
2040cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
20411da177e4SLinus Torvalds {
20421da177e4SLinus Torvalds 	if (!IS_ERR(nd_get_link(nd))) {
2043cc314eefSLinus Torvalds 		struct page *page = cookie;
20441da177e4SLinus Torvalds 		kunmap(page);
20451da177e4SLinus Torvalds 		mark_page_accessed(page);
20461da177e4SLinus Torvalds 		page_cache_release(page);
20471da177e4SLinus Torvalds 	}
20481da177e4SLinus Torvalds }
20491da177e4SLinus Torvalds 
205092e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations = {
20511da177e4SLinus Torvalds 	.readlink	= generic_readlink,
20521da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link_inline,
20531da177e4SLinus Torvalds };
20541da177e4SLinus Torvalds 
205592e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = {
20561da177e4SLinus Torvalds 	.readlink	= generic_readlink,
20571da177e4SLinus Torvalds 	.follow_link	= shmem_follow_link,
20581da177e4SLinus Torvalds 	.put_link	= shmem_put_link,
20591da177e4SLinus Torvalds };
20601da177e4SLinus Torvalds 
206139f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
206246711810SRandy Dunlap /*
206339f0247dSAndreas Gruenbacher  * Superblocks without xattr inode operations will get security.* xattr
206439f0247dSAndreas Gruenbacher  * support from the VFS "for free". As soon as we have any other xattrs
206539f0247dSAndreas Gruenbacher  * like ACLs, we also need to implement the security.* handlers at
206639f0247dSAndreas Gruenbacher  * filesystem level, though.
206739f0247dSAndreas Gruenbacher  */
206839f0247dSAndreas Gruenbacher 
2069431547b3SChristoph Hellwig static size_t shmem_xattr_security_list(struct dentry *dentry, char *list,
207039f0247dSAndreas Gruenbacher 					size_t list_len, const char *name,
2071431547b3SChristoph Hellwig 					size_t name_len, int handler_flags)
207239f0247dSAndreas Gruenbacher {
2073431547b3SChristoph Hellwig 	return security_inode_listsecurity(dentry->d_inode, list, list_len);
207439f0247dSAndreas Gruenbacher }
207539f0247dSAndreas Gruenbacher 
2076431547b3SChristoph Hellwig static int shmem_xattr_security_get(struct dentry *dentry, const char *name,
2077431547b3SChristoph Hellwig 		void *buffer, size_t size, int handler_flags)
207839f0247dSAndreas Gruenbacher {
207939f0247dSAndreas Gruenbacher 	if (strcmp(name, "") == 0)
208039f0247dSAndreas Gruenbacher 		return -EINVAL;
2081431547b3SChristoph Hellwig 	return xattr_getsecurity(dentry->d_inode, name, buffer, size);
208239f0247dSAndreas Gruenbacher }
208339f0247dSAndreas Gruenbacher 
2084431547b3SChristoph Hellwig static int shmem_xattr_security_set(struct dentry *dentry, const char *name,
2085431547b3SChristoph Hellwig 		const void *value, size_t size, int flags, int handler_flags)
208639f0247dSAndreas Gruenbacher {
208739f0247dSAndreas Gruenbacher 	if (strcmp(name, "") == 0)
208839f0247dSAndreas Gruenbacher 		return -EINVAL;
2089431547b3SChristoph Hellwig 	return security_inode_setsecurity(dentry->d_inode, name, value,
2090431547b3SChristoph Hellwig 					  size, flags);
209139f0247dSAndreas Gruenbacher }
209239f0247dSAndreas Gruenbacher 
2093bb435453SStephen Hemminger static const struct xattr_handler shmem_xattr_security_handler = {
209439f0247dSAndreas Gruenbacher 	.prefix = XATTR_SECURITY_PREFIX,
209539f0247dSAndreas Gruenbacher 	.list   = shmem_xattr_security_list,
209639f0247dSAndreas Gruenbacher 	.get    = shmem_xattr_security_get,
209739f0247dSAndreas Gruenbacher 	.set    = shmem_xattr_security_set,
209839f0247dSAndreas Gruenbacher };
209939f0247dSAndreas Gruenbacher 
2100bb435453SStephen Hemminger static const struct xattr_handler *shmem_xattr_handlers[] = {
21011c7c474cSChristoph Hellwig 	&generic_acl_access_handler,
21021c7c474cSChristoph Hellwig 	&generic_acl_default_handler,
210339f0247dSAndreas Gruenbacher 	&shmem_xattr_security_handler,
210439f0247dSAndreas Gruenbacher 	NULL
210539f0247dSAndreas Gruenbacher };
210639f0247dSAndreas Gruenbacher #endif
210739f0247dSAndreas Gruenbacher 
210891828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child)
210991828a40SDavid M. Grimes {
211091828a40SDavid M. Grimes 	return ERR_PTR(-ESTALE);
211191828a40SDavid M. Grimes }
211291828a40SDavid M. Grimes 
211391828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh)
211491828a40SDavid M. Grimes {
211591828a40SDavid M. Grimes 	__u32 *fh = vfh;
211691828a40SDavid M. Grimes 	__u64 inum = fh[2];
211791828a40SDavid M. Grimes 	inum = (inum << 32) | fh[1];
211891828a40SDavid M. Grimes 	return ino->i_ino == inum && fh[0] == ino->i_generation;
211991828a40SDavid M. Grimes }
212091828a40SDavid M. Grimes 
2121480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2122480b116cSChristoph Hellwig 		struct fid *fid, int fh_len, int fh_type)
212391828a40SDavid M. Grimes {
212491828a40SDavid M. Grimes 	struct inode *inode;
2125480b116cSChristoph Hellwig 	struct dentry *dentry = NULL;
2126480b116cSChristoph Hellwig 	u64 inum = fid->raw[2];
2127480b116cSChristoph Hellwig 	inum = (inum << 32) | fid->raw[1];
212891828a40SDavid M. Grimes 
2129480b116cSChristoph Hellwig 	if (fh_len < 3)
2130480b116cSChristoph Hellwig 		return NULL;
2131480b116cSChristoph Hellwig 
2132480b116cSChristoph Hellwig 	inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2133480b116cSChristoph Hellwig 			shmem_match, fid->raw);
213491828a40SDavid M. Grimes 	if (inode) {
2135480b116cSChristoph Hellwig 		dentry = d_find_alias(inode);
213691828a40SDavid M. Grimes 		iput(inode);
213791828a40SDavid M. Grimes 	}
213891828a40SDavid M. Grimes 
2139480b116cSChristoph Hellwig 	return dentry;
214091828a40SDavid M. Grimes }
214191828a40SDavid M. Grimes 
214291828a40SDavid M. Grimes static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
214391828a40SDavid M. Grimes 				int connectable)
214491828a40SDavid M. Grimes {
214591828a40SDavid M. Grimes 	struct inode *inode = dentry->d_inode;
214691828a40SDavid M. Grimes 
214791828a40SDavid M. Grimes 	if (*len < 3)
214891828a40SDavid M. Grimes 		return 255;
214991828a40SDavid M. Grimes 
21501d3382cbSAl Viro 	if (inode_unhashed(inode)) {
215191828a40SDavid M. Grimes 		/* Unfortunately insert_inode_hash is not idempotent,
215291828a40SDavid M. Grimes 		 * so as we hash inodes here rather than at creation
215391828a40SDavid M. Grimes 		 * time, we need a lock to ensure we only try
215491828a40SDavid M. Grimes 		 * to do it once
215591828a40SDavid M. Grimes 		 */
215691828a40SDavid M. Grimes 		static DEFINE_SPINLOCK(lock);
215791828a40SDavid M. Grimes 		spin_lock(&lock);
21581d3382cbSAl Viro 		if (inode_unhashed(inode))
215991828a40SDavid M. Grimes 			__insert_inode_hash(inode,
216091828a40SDavid M. Grimes 					    inode->i_ino + inode->i_generation);
216191828a40SDavid M. Grimes 		spin_unlock(&lock);
216291828a40SDavid M. Grimes 	}
216391828a40SDavid M. Grimes 
216491828a40SDavid M. Grimes 	fh[0] = inode->i_generation;
216591828a40SDavid M. Grimes 	fh[1] = inode->i_ino;
216691828a40SDavid M. Grimes 	fh[2] = ((__u64)inode->i_ino) >> 32;
216791828a40SDavid M. Grimes 
216891828a40SDavid M. Grimes 	*len = 3;
216991828a40SDavid M. Grimes 	return 1;
217091828a40SDavid M. Grimes }
217191828a40SDavid M. Grimes 
217239655164SChristoph Hellwig static const struct export_operations shmem_export_ops = {
217391828a40SDavid M. Grimes 	.get_parent     = shmem_get_parent,
217491828a40SDavid M. Grimes 	.encode_fh      = shmem_encode_fh,
2175480b116cSChristoph Hellwig 	.fh_to_dentry	= shmem_fh_to_dentry,
217691828a40SDavid M. Grimes };
217791828a40SDavid M. Grimes 
2178680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2179680d794bSakpm@linux-foundation.org 			       bool remount)
21801da177e4SLinus Torvalds {
21811da177e4SLinus Torvalds 	char *this_char, *value, *rest;
21821da177e4SLinus Torvalds 
2183b00dc3adSHugh Dickins 	while (options != NULL) {
2184b00dc3adSHugh Dickins 		this_char = options;
2185b00dc3adSHugh Dickins 		for (;;) {
2186b00dc3adSHugh Dickins 			/*
2187b00dc3adSHugh Dickins 			 * NUL-terminate this option: unfortunately,
2188b00dc3adSHugh Dickins 			 * mount options form a comma-separated list,
2189b00dc3adSHugh Dickins 			 * but mpol's nodelist may also contain commas.
2190b00dc3adSHugh Dickins 			 */
2191b00dc3adSHugh Dickins 			options = strchr(options, ',');
2192b00dc3adSHugh Dickins 			if (options == NULL)
2193b00dc3adSHugh Dickins 				break;
2194b00dc3adSHugh Dickins 			options++;
2195b00dc3adSHugh Dickins 			if (!isdigit(*options)) {
2196b00dc3adSHugh Dickins 				options[-1] = '\0';
2197b00dc3adSHugh Dickins 				break;
2198b00dc3adSHugh Dickins 			}
2199b00dc3adSHugh Dickins 		}
22001da177e4SLinus Torvalds 		if (!*this_char)
22011da177e4SLinus Torvalds 			continue;
22021da177e4SLinus Torvalds 		if ((value = strchr(this_char,'=')) != NULL) {
22031da177e4SLinus Torvalds 			*value++ = 0;
22041da177e4SLinus Torvalds 		} else {
22051da177e4SLinus Torvalds 			printk(KERN_ERR
22061da177e4SLinus Torvalds 			    "tmpfs: No value for mount option '%s'\n",
22071da177e4SLinus Torvalds 			    this_char);
22081da177e4SLinus Torvalds 			return 1;
22091da177e4SLinus Torvalds 		}
22101da177e4SLinus Torvalds 
22111da177e4SLinus Torvalds 		if (!strcmp(this_char,"size")) {
22121da177e4SLinus Torvalds 			unsigned long long size;
22131da177e4SLinus Torvalds 			size = memparse(value,&rest);
22141da177e4SLinus Torvalds 			if (*rest == '%') {
22151da177e4SLinus Torvalds 				size <<= PAGE_SHIFT;
22161da177e4SLinus Torvalds 				size *= totalram_pages;
22171da177e4SLinus Torvalds 				do_div(size, 100);
22181da177e4SLinus Torvalds 				rest++;
22191da177e4SLinus Torvalds 			}
22201da177e4SLinus Torvalds 			if (*rest)
22211da177e4SLinus Torvalds 				goto bad_val;
2222680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks =
2223680d794bSakpm@linux-foundation.org 				DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
22241da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_blocks")) {
2225680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks = memparse(value, &rest);
22261da177e4SLinus Torvalds 			if (*rest)
22271da177e4SLinus Torvalds 				goto bad_val;
22281da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"nr_inodes")) {
2229680d794bSakpm@linux-foundation.org 			sbinfo->max_inodes = memparse(value, &rest);
22301da177e4SLinus Torvalds 			if (*rest)
22311da177e4SLinus Torvalds 				goto bad_val;
22321da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"mode")) {
2233680d794bSakpm@linux-foundation.org 			if (remount)
22341da177e4SLinus Torvalds 				continue;
2235680d794bSakpm@linux-foundation.org 			sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
22361da177e4SLinus Torvalds 			if (*rest)
22371da177e4SLinus Torvalds 				goto bad_val;
22381da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"uid")) {
2239680d794bSakpm@linux-foundation.org 			if (remount)
22401da177e4SLinus Torvalds 				continue;
2241680d794bSakpm@linux-foundation.org 			sbinfo->uid = simple_strtoul(value, &rest, 0);
22421da177e4SLinus Torvalds 			if (*rest)
22431da177e4SLinus Torvalds 				goto bad_val;
22441da177e4SLinus Torvalds 		} else if (!strcmp(this_char,"gid")) {
2245680d794bSakpm@linux-foundation.org 			if (remount)
22461da177e4SLinus Torvalds 				continue;
2247680d794bSakpm@linux-foundation.org 			sbinfo->gid = simple_strtoul(value, &rest, 0);
22481da177e4SLinus Torvalds 			if (*rest)
22491da177e4SLinus Torvalds 				goto bad_val;
22507339ff83SRobin Holt 		} else if (!strcmp(this_char,"mpol")) {
225171fe804bSLee Schermerhorn 			if (mpol_parse_str(value, &sbinfo->mpol, 1))
22527339ff83SRobin Holt 				goto bad_val;
22531da177e4SLinus Torvalds 		} else {
22541da177e4SLinus Torvalds 			printk(KERN_ERR "tmpfs: Bad mount option %s\n",
22551da177e4SLinus Torvalds 			       this_char);
22561da177e4SLinus Torvalds 			return 1;
22571da177e4SLinus Torvalds 		}
22581da177e4SLinus Torvalds 	}
22591da177e4SLinus Torvalds 	return 0;
22601da177e4SLinus Torvalds 
22611da177e4SLinus Torvalds bad_val:
22621da177e4SLinus Torvalds 	printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
22631da177e4SLinus Torvalds 	       value, this_char);
22641da177e4SLinus Torvalds 	return 1;
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds }
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
22691da177e4SLinus Torvalds {
22701da177e4SLinus Torvalds 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2271680d794bSakpm@linux-foundation.org 	struct shmem_sb_info config = *sbinfo;
22720edd73b3SHugh Dickins 	unsigned long inodes;
22730edd73b3SHugh Dickins 	int error = -EINVAL;
22741da177e4SLinus Torvalds 
2275680d794bSakpm@linux-foundation.org 	if (shmem_parse_options(data, &config, true))
22760edd73b3SHugh Dickins 		return error;
22770edd73b3SHugh Dickins 
22780edd73b3SHugh Dickins 	spin_lock(&sbinfo->stat_lock);
22790edd73b3SHugh Dickins 	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
22807e496299STim Chen 	if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
22810edd73b3SHugh Dickins 		goto out;
2282680d794bSakpm@linux-foundation.org 	if (config.max_inodes < inodes)
22830edd73b3SHugh Dickins 		goto out;
22840edd73b3SHugh Dickins 	/*
22850edd73b3SHugh Dickins 	 * Those tests also disallow limited->unlimited while any are in
22860edd73b3SHugh Dickins 	 * use, so i_blocks will always be zero when max_blocks is zero;
22870edd73b3SHugh Dickins 	 * but we must separately disallow unlimited->limited, because
22880edd73b3SHugh Dickins 	 * in that case we have no record of how much is already in use.
22890edd73b3SHugh Dickins 	 */
2290680d794bSakpm@linux-foundation.org 	if (config.max_blocks && !sbinfo->max_blocks)
22910edd73b3SHugh Dickins 		goto out;
2292680d794bSakpm@linux-foundation.org 	if (config.max_inodes && !sbinfo->max_inodes)
22930edd73b3SHugh Dickins 		goto out;
22940edd73b3SHugh Dickins 
22950edd73b3SHugh Dickins 	error = 0;
2296680d794bSakpm@linux-foundation.org 	sbinfo->max_blocks  = config.max_blocks;
2297680d794bSakpm@linux-foundation.org 	sbinfo->max_inodes  = config.max_inodes;
2298680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = config.max_inodes - inodes;
229971fe804bSLee Schermerhorn 
230071fe804bSLee Schermerhorn 	mpol_put(sbinfo->mpol);
230171fe804bSLee Schermerhorn 	sbinfo->mpol        = config.mpol;	/* transfers initial ref */
23020edd73b3SHugh Dickins out:
23030edd73b3SHugh Dickins 	spin_unlock(&sbinfo->stat_lock);
23040edd73b3SHugh Dickins 	return error;
23051da177e4SLinus Torvalds }
2306680d794bSakpm@linux-foundation.org 
2307680d794bSakpm@linux-foundation.org static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2308680d794bSakpm@linux-foundation.org {
2309680d794bSakpm@linux-foundation.org 	struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2310680d794bSakpm@linux-foundation.org 
2311680d794bSakpm@linux-foundation.org 	if (sbinfo->max_blocks != shmem_default_max_blocks())
2312680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",size=%luk",
2313680d794bSakpm@linux-foundation.org 			sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2314680d794bSakpm@linux-foundation.org 	if (sbinfo->max_inodes != shmem_default_max_inodes())
2315680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2316680d794bSakpm@linux-foundation.org 	if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2317680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",mode=%03o", sbinfo->mode);
2318680d794bSakpm@linux-foundation.org 	if (sbinfo->uid != 0)
2319680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",uid=%u", sbinfo->uid);
2320680d794bSakpm@linux-foundation.org 	if (sbinfo->gid != 0)
2321680d794bSakpm@linux-foundation.org 		seq_printf(seq, ",gid=%u", sbinfo->gid);
232271fe804bSLee Schermerhorn 	shmem_show_mpol(seq, sbinfo->mpol);
2323680d794bSakpm@linux-foundation.org 	return 0;
2324680d794bSakpm@linux-foundation.org }
2325680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */
23261da177e4SLinus Torvalds 
23271da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb)
23281da177e4SLinus Torvalds {
2329602586a8SHugh Dickins 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2330602586a8SHugh Dickins 
2331602586a8SHugh Dickins 	percpu_counter_destroy(&sbinfo->used_blocks);
2332602586a8SHugh Dickins 	kfree(sbinfo);
23331da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
23341da177e4SLinus Torvalds }
23351da177e4SLinus Torvalds 
23362b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent)
23371da177e4SLinus Torvalds {
23381da177e4SLinus Torvalds 	struct inode *inode;
23391da177e4SLinus Torvalds 	struct dentry *root;
23400edd73b3SHugh Dickins 	struct shmem_sb_info *sbinfo;
2341680d794bSakpm@linux-foundation.org 	int err = -ENOMEM;
2342680d794bSakpm@linux-foundation.org 
2343680d794bSakpm@linux-foundation.org 	/* Round up to L1_CACHE_BYTES to resist false sharing */
2344425fbf04SPekka Enberg 	sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
2345680d794bSakpm@linux-foundation.org 				L1_CACHE_BYTES), GFP_KERNEL);
2346680d794bSakpm@linux-foundation.org 	if (!sbinfo)
2347680d794bSakpm@linux-foundation.org 		return -ENOMEM;
2348680d794bSakpm@linux-foundation.org 
2349680d794bSakpm@linux-foundation.org 	sbinfo->mode = S_IRWXUGO | S_ISVTX;
235076aac0e9SDavid Howells 	sbinfo->uid = current_fsuid();
235176aac0e9SDavid Howells 	sbinfo->gid = current_fsgid();
2352680d794bSakpm@linux-foundation.org 	sb->s_fs_info = sbinfo;
23531da177e4SLinus Torvalds 
23540edd73b3SHugh Dickins #ifdef CONFIG_TMPFS
23551da177e4SLinus Torvalds 	/*
23561da177e4SLinus Torvalds 	 * Per default we only allow half of the physical ram per
23571da177e4SLinus Torvalds 	 * tmpfs instance, limiting inodes to one per page of lowmem;
23581da177e4SLinus Torvalds 	 * but the internal instance is left unlimited.
23591da177e4SLinus Torvalds 	 */
23601da177e4SLinus Torvalds 	if (!(sb->s_flags & MS_NOUSER)) {
2361680d794bSakpm@linux-foundation.org 		sbinfo->max_blocks = shmem_default_max_blocks();
2362680d794bSakpm@linux-foundation.org 		sbinfo->max_inodes = shmem_default_max_inodes();
2363680d794bSakpm@linux-foundation.org 		if (shmem_parse_options(data, sbinfo, false)) {
2364680d794bSakpm@linux-foundation.org 			err = -EINVAL;
2365680d794bSakpm@linux-foundation.org 			goto failed;
2366680d794bSakpm@linux-foundation.org 		}
23671da177e4SLinus Torvalds 	}
236891828a40SDavid M. Grimes 	sb->s_export_op = &shmem_export_ops;
23690edd73b3SHugh Dickins #else
23700edd73b3SHugh Dickins 	sb->s_flags |= MS_NOUSER;
23710edd73b3SHugh Dickins #endif
23721da177e4SLinus Torvalds 
23731da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
2374602586a8SHugh Dickins 	if (percpu_counter_init(&sbinfo->used_blocks, 0))
2375602586a8SHugh Dickins 		goto failed;
2376680d794bSakpm@linux-foundation.org 	sbinfo->free_inodes = sbinfo->max_inodes;
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds 	sb->s_maxbytes = SHMEM_MAX_BYTES;
23791da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
23801da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
23811da177e4SLinus Torvalds 	sb->s_magic = TMPFS_MAGIC;
23821da177e4SLinus Torvalds 	sb->s_op = &shmem_ops;
2383cfd95a9cSRobin H. Johnson 	sb->s_time_gran = 1;
238439f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
238539f0247dSAndreas Gruenbacher 	sb->s_xattr = shmem_xattr_handlers;
238639f0247dSAndreas Gruenbacher 	sb->s_flags |= MS_POSIXACL;
238739f0247dSAndreas Gruenbacher #endif
23880edd73b3SHugh Dickins 
2389454abafeSDmitry Monakhov 	inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
23901da177e4SLinus Torvalds 	if (!inode)
23911da177e4SLinus Torvalds 		goto failed;
2392680d794bSakpm@linux-foundation.org 	inode->i_uid = sbinfo->uid;
2393680d794bSakpm@linux-foundation.org 	inode->i_gid = sbinfo->gid;
23941da177e4SLinus Torvalds 	root = d_alloc_root(inode);
23951da177e4SLinus Torvalds 	if (!root)
23961da177e4SLinus Torvalds 		goto failed_iput;
23971da177e4SLinus Torvalds 	sb->s_root = root;
23981da177e4SLinus Torvalds 	return 0;
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds failed_iput:
24011da177e4SLinus Torvalds 	iput(inode);
24021da177e4SLinus Torvalds failed:
24031da177e4SLinus Torvalds 	shmem_put_super(sb);
24041da177e4SLinus Torvalds 	return err;
24051da177e4SLinus Torvalds }
24061da177e4SLinus Torvalds 
2407fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep;
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb)
24101da177e4SLinus Torvalds {
24111da177e4SLinus Torvalds 	struct shmem_inode_info *p;
2412e94b1766SChristoph Lameter 	p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
24131da177e4SLinus Torvalds 	if (!p)
24141da177e4SLinus Torvalds 		return NULL;
24151da177e4SLinus Torvalds 	return &p->vfs_inode;
24161da177e4SLinus Torvalds }
24171da177e4SLinus Torvalds 
2418*fa0d7e3dSNick Piggin static void shmem_i_callback(struct rcu_head *head)
2419*fa0d7e3dSNick Piggin {
2420*fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
2421*fa0d7e3dSNick Piggin 	INIT_LIST_HEAD(&inode->i_dentry);
2422*fa0d7e3dSNick Piggin 	kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2423*fa0d7e3dSNick Piggin }
2424*fa0d7e3dSNick Piggin 
24251da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode)
24261da177e4SLinus Torvalds {
24271da177e4SLinus Torvalds 	if ((inode->i_mode & S_IFMT) == S_IFREG) {
24281da177e4SLinus Torvalds 		/* only struct inode is valid if it's an inline symlink */
24291da177e4SLinus Torvalds 		mpol_free_shared_policy(&SHMEM_I(inode)->policy);
24301da177e4SLinus Torvalds 	}
2431*fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, shmem_i_callback);
24321da177e4SLinus Torvalds }
24331da177e4SLinus Torvalds 
243451cc5068SAlexey Dobriyan static void init_once(void *foo)
24351da177e4SLinus Torvalds {
24361da177e4SLinus Torvalds 	struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds 	inode_init_once(&p->vfs_inode);
24391da177e4SLinus Torvalds }
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds static int init_inodecache(void)
24421da177e4SLinus Torvalds {
24431da177e4SLinus Torvalds 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
24441da177e4SLinus Torvalds 				sizeof(struct shmem_inode_info),
2445040b5c6fSAlexey Dobriyan 				0, SLAB_PANIC, init_once);
24461da177e4SLinus Torvalds 	return 0;
24471da177e4SLinus Torvalds }
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds static void destroy_inodecache(void)
24501da177e4SLinus Torvalds {
24511a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(shmem_inode_cachep);
24521da177e4SLinus Torvalds }
24531da177e4SLinus Torvalds 
2454f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = {
24551da177e4SLinus Torvalds 	.writepage	= shmem_writepage,
245676719325SKen Chen 	.set_page_dirty	= __set_page_dirty_no_writeback,
24571da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
2458ae976416SHugh Dickins 	.readpage	= shmem_readpage,
2459800d15a5SNick Piggin 	.write_begin	= shmem_write_begin,
2460800d15a5SNick Piggin 	.write_end	= shmem_write_end,
24611da177e4SLinus Torvalds #endif
2462304dbdb7SLee Schermerhorn 	.migratepage	= migrate_page,
2463aa261f54SAndi Kleen 	.error_remove_page = generic_error_remove_page,
24641da177e4SLinus Torvalds };
24651da177e4SLinus Torvalds 
246615ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = {
24671da177e4SLinus Torvalds 	.mmap		= shmem_mmap,
24681da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
24691da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
2470bcd78e49SHugh Dickins 	.read		= do_sync_read,
24715402b976SHugh Dickins 	.write		= do_sync_write,
2472bcd78e49SHugh Dickins 	.aio_read	= shmem_file_aio_read,
24735402b976SHugh Dickins 	.aio_write	= generic_file_aio_write,
24741b061d92SChristoph Hellwig 	.fsync		= noop_fsync,
2475ae976416SHugh Dickins 	.splice_read	= generic_file_splice_read,
2476ae976416SHugh Dickins 	.splice_write	= generic_file_splice_write,
24771da177e4SLinus Torvalds #endif
24781da177e4SLinus Torvalds };
24791da177e4SLinus Torvalds 
248092e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = {
24811da177e4SLinus Torvalds 	.setattr	= shmem_notify_change,
2482f6b3ec23SBadari Pulavarty 	.truncate_range	= shmem_truncate_range,
248339f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
248439f0247dSAndreas Gruenbacher 	.setxattr	= generic_setxattr,
248539f0247dSAndreas Gruenbacher 	.getxattr	= generic_getxattr,
248639f0247dSAndreas Gruenbacher 	.listxattr	= generic_listxattr,
248739f0247dSAndreas Gruenbacher 	.removexattr	= generic_removexattr,
24881c7c474cSChristoph Hellwig 	.check_acl	= generic_check_acl,
248939f0247dSAndreas Gruenbacher #endif
249039f0247dSAndreas Gruenbacher 
24911da177e4SLinus Torvalds };
24921da177e4SLinus Torvalds 
249392e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = {
24941da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
24951da177e4SLinus Torvalds 	.create		= shmem_create,
24961da177e4SLinus Torvalds 	.lookup		= simple_lookup,
24971da177e4SLinus Torvalds 	.link		= shmem_link,
24981da177e4SLinus Torvalds 	.unlink		= shmem_unlink,
24991da177e4SLinus Torvalds 	.symlink	= shmem_symlink,
25001da177e4SLinus Torvalds 	.mkdir		= shmem_mkdir,
25011da177e4SLinus Torvalds 	.rmdir		= shmem_rmdir,
25021da177e4SLinus Torvalds 	.mknod		= shmem_mknod,
25031da177e4SLinus Torvalds 	.rename		= shmem_rename,
25041da177e4SLinus Torvalds #endif
250539f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
250639f0247dSAndreas Gruenbacher 	.setattr	= shmem_notify_change,
250739f0247dSAndreas Gruenbacher 	.setxattr	= generic_setxattr,
250839f0247dSAndreas Gruenbacher 	.getxattr	= generic_getxattr,
250939f0247dSAndreas Gruenbacher 	.listxattr	= generic_listxattr,
251039f0247dSAndreas Gruenbacher 	.removexattr	= generic_removexattr,
25111c7c474cSChristoph Hellwig 	.check_acl	= generic_check_acl,
251239f0247dSAndreas Gruenbacher #endif
251339f0247dSAndreas Gruenbacher };
251439f0247dSAndreas Gruenbacher 
251592e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = {
251639f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL
251739f0247dSAndreas Gruenbacher 	.setattr	= shmem_notify_change,
251839f0247dSAndreas Gruenbacher 	.setxattr	= generic_setxattr,
251939f0247dSAndreas Gruenbacher 	.getxattr	= generic_getxattr,
252039f0247dSAndreas Gruenbacher 	.listxattr	= generic_listxattr,
252139f0247dSAndreas Gruenbacher 	.removexattr	= generic_removexattr,
25221c7c474cSChristoph Hellwig 	.check_acl	= generic_check_acl,
252339f0247dSAndreas Gruenbacher #endif
25241da177e4SLinus Torvalds };
25251da177e4SLinus Torvalds 
2526759b9775SHugh Dickins static const struct super_operations shmem_ops = {
25271da177e4SLinus Torvalds 	.alloc_inode	= shmem_alloc_inode,
25281da177e4SLinus Torvalds 	.destroy_inode	= shmem_destroy_inode,
25291da177e4SLinus Torvalds #ifdef CONFIG_TMPFS
25301da177e4SLinus Torvalds 	.statfs		= shmem_statfs,
25311da177e4SLinus Torvalds 	.remount_fs	= shmem_remount_fs,
2532680d794bSakpm@linux-foundation.org 	.show_options	= shmem_show_options,
25331da177e4SLinus Torvalds #endif
25341f895f75SAl Viro 	.evict_inode	= shmem_evict_inode,
25351da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
25361da177e4SLinus Torvalds 	.put_super	= shmem_put_super,
25371da177e4SLinus Torvalds };
25381da177e4SLinus Torvalds 
2539f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = {
254054cb8821SNick Piggin 	.fault		= shmem_fault,
25411da177e4SLinus Torvalds #ifdef CONFIG_NUMA
25421da177e4SLinus Torvalds 	.set_policy     = shmem_set_policy,
25431da177e4SLinus Torvalds 	.get_policy     = shmem_get_policy,
25441da177e4SLinus Torvalds #endif
25451da177e4SLinus Torvalds };
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds 
25483c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type,
25493c26ff6eSAl Viro 	int flags, const char *dev_name, void *data)
25501da177e4SLinus Torvalds {
25513c26ff6eSAl Viro 	return mount_nodev(fs_type, flags, data, shmem_fill_super);
25521da177e4SLinus Torvalds }
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds static struct file_system_type tmpfs_fs_type = {
25551da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
25561da177e4SLinus Torvalds 	.name		= "tmpfs",
25573c26ff6eSAl Viro 	.mount		= shmem_mount,
25581da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
25591da177e4SLinus Torvalds };
25601da177e4SLinus Torvalds 
25612b2af54aSKay Sievers int __init init_tmpfs(void)
25621da177e4SLinus Torvalds {
25631da177e4SLinus Torvalds 	int error;
25641da177e4SLinus Torvalds 
2565e0bf68ddSPeter Zijlstra 	error = bdi_init(&shmem_backing_dev_info);
2566e0bf68ddSPeter Zijlstra 	if (error)
2567e0bf68ddSPeter Zijlstra 		goto out4;
2568e0bf68ddSPeter Zijlstra 
25691da177e4SLinus Torvalds 	error = init_inodecache();
25701da177e4SLinus Torvalds 	if (error)
25711da177e4SLinus Torvalds 		goto out3;
25721da177e4SLinus Torvalds 
25731da177e4SLinus Torvalds 	error = register_filesystem(&tmpfs_fs_type);
25741da177e4SLinus Torvalds 	if (error) {
25751da177e4SLinus Torvalds 		printk(KERN_ERR "Could not register tmpfs\n");
25761da177e4SLinus Torvalds 		goto out2;
25771da177e4SLinus Torvalds 	}
257895dc112aSGreg Kroah-Hartman 
25791f5ce9e9STrond Myklebust 	shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
25801da177e4SLinus Torvalds 				tmpfs_fs_type.name, NULL);
25811da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt)) {
25821da177e4SLinus Torvalds 		error = PTR_ERR(shm_mnt);
25831da177e4SLinus Torvalds 		printk(KERN_ERR "Could not kern_mount tmpfs\n");
25841da177e4SLinus Torvalds 		goto out1;
25851da177e4SLinus Torvalds 	}
25861da177e4SLinus Torvalds 	return 0;
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds out1:
25891da177e4SLinus Torvalds 	unregister_filesystem(&tmpfs_fs_type);
25901da177e4SLinus Torvalds out2:
25911da177e4SLinus Torvalds 	destroy_inodecache();
25921da177e4SLinus Torvalds out3:
2593e0bf68ddSPeter Zijlstra 	bdi_destroy(&shmem_backing_dev_info);
2594e0bf68ddSPeter Zijlstra out4:
25951da177e4SLinus Torvalds 	shm_mnt = ERR_PTR(error);
25961da177e4SLinus Torvalds 	return error;
25971da177e4SLinus Torvalds }
2598853ac43aSMatt Mackall 
259987946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR
260087946a72SDaisuke Nishimura /**
260187946a72SDaisuke Nishimura  * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
260287946a72SDaisuke Nishimura  * @inode: the inode to be searched
260387946a72SDaisuke Nishimura  * @pgoff: the offset to be searched
260487946a72SDaisuke Nishimura  * @pagep: the pointer for the found page to be stored
260587946a72SDaisuke Nishimura  * @ent: the pointer for the found swap entry to be stored
260687946a72SDaisuke Nishimura  *
260787946a72SDaisuke Nishimura  * If a page is found, refcount of it is incremented. Callers should handle
260887946a72SDaisuke Nishimura  * these refcount.
260987946a72SDaisuke Nishimura  */
261087946a72SDaisuke Nishimura void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
261187946a72SDaisuke Nishimura 					struct page **pagep, swp_entry_t *ent)
261287946a72SDaisuke Nishimura {
261387946a72SDaisuke Nishimura 	swp_entry_t entry = { .val = 0 }, *ptr;
261487946a72SDaisuke Nishimura 	struct page *page = NULL;
261587946a72SDaisuke Nishimura 	struct shmem_inode_info *info = SHMEM_I(inode);
261687946a72SDaisuke Nishimura 
261787946a72SDaisuke Nishimura 	if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
261887946a72SDaisuke Nishimura 		goto out;
261987946a72SDaisuke Nishimura 
262087946a72SDaisuke Nishimura 	spin_lock(&info->lock);
262187946a72SDaisuke Nishimura 	ptr = shmem_swp_entry(info, pgoff, NULL);
262287946a72SDaisuke Nishimura #ifdef CONFIG_SWAP
262387946a72SDaisuke Nishimura 	if (ptr && ptr->val) {
262487946a72SDaisuke Nishimura 		entry.val = ptr->val;
262587946a72SDaisuke Nishimura 		page = find_get_page(&swapper_space, entry.val);
262687946a72SDaisuke Nishimura 	} else
262787946a72SDaisuke Nishimura #endif
262887946a72SDaisuke Nishimura 		page = find_get_page(inode->i_mapping, pgoff);
262987946a72SDaisuke Nishimura 	if (ptr)
263087946a72SDaisuke Nishimura 		shmem_swp_unmap(ptr);
263187946a72SDaisuke Nishimura 	spin_unlock(&info->lock);
263287946a72SDaisuke Nishimura out:
263387946a72SDaisuke Nishimura 	*pagep = page;
263487946a72SDaisuke Nishimura 	*ent = entry;
263587946a72SDaisuke Nishimura }
263687946a72SDaisuke Nishimura #endif
263787946a72SDaisuke Nishimura 
2638853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */
2639853ac43aSMatt Mackall 
2640853ac43aSMatt Mackall /*
2641853ac43aSMatt Mackall  * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2642853ac43aSMatt Mackall  *
2643853ac43aSMatt Mackall  * This is intended for small system where the benefits of the full
2644853ac43aSMatt Mackall  * shmem code (swap-backed and resource-limited) are outweighed by
2645853ac43aSMatt Mackall  * their complexity. On systems without swap this code should be
2646853ac43aSMatt Mackall  * effectively equivalent, but much lighter weight.
2647853ac43aSMatt Mackall  */
2648853ac43aSMatt Mackall 
2649853ac43aSMatt Mackall #include <linux/ramfs.h>
2650853ac43aSMatt Mackall 
2651853ac43aSMatt Mackall static struct file_system_type tmpfs_fs_type = {
2652853ac43aSMatt Mackall 	.name		= "tmpfs",
26533c26ff6eSAl Viro 	.mount		= ramfs_mount,
2654853ac43aSMatt Mackall 	.kill_sb	= kill_litter_super,
2655853ac43aSMatt Mackall };
2656853ac43aSMatt Mackall 
26572b2af54aSKay Sievers int __init init_tmpfs(void)
2658853ac43aSMatt Mackall {
2659853ac43aSMatt Mackall 	BUG_ON(register_filesystem(&tmpfs_fs_type) != 0);
2660853ac43aSMatt Mackall 
2661853ac43aSMatt Mackall 	shm_mnt = kern_mount(&tmpfs_fs_type);
2662853ac43aSMatt Mackall 	BUG_ON(IS_ERR(shm_mnt));
2663853ac43aSMatt Mackall 
2664853ac43aSMatt Mackall 	return 0;
2665853ac43aSMatt Mackall }
2666853ac43aSMatt Mackall 
2667853ac43aSMatt Mackall int shmem_unuse(swp_entry_t entry, struct page *page)
2668853ac43aSMatt Mackall {
2669853ac43aSMatt Mackall 	return 0;
2670853ac43aSMatt Mackall }
2671853ac43aSMatt Mackall 
26723f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user)
26733f96b79aSHugh Dickins {
26743f96b79aSHugh Dickins 	return 0;
26753f96b79aSHugh Dickins }
26763f96b79aSHugh Dickins 
267787946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR
267887946a72SDaisuke Nishimura /**
267987946a72SDaisuke Nishimura  * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
268087946a72SDaisuke Nishimura  * @inode: the inode to be searched
268187946a72SDaisuke Nishimura  * @pgoff: the offset to be searched
268287946a72SDaisuke Nishimura  * @pagep: the pointer for the found page to be stored
268387946a72SDaisuke Nishimura  * @ent: the pointer for the found swap entry to be stored
268487946a72SDaisuke Nishimura  *
268587946a72SDaisuke Nishimura  * If a page is found, refcount of it is incremented. Callers should handle
268687946a72SDaisuke Nishimura  * these refcount.
268787946a72SDaisuke Nishimura  */
268887946a72SDaisuke Nishimura void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
268987946a72SDaisuke Nishimura 					struct page **pagep, swp_entry_t *ent)
269087946a72SDaisuke Nishimura {
269187946a72SDaisuke Nishimura 	struct page *page = NULL;
269287946a72SDaisuke Nishimura 
269387946a72SDaisuke Nishimura 	if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
269487946a72SDaisuke Nishimura 		goto out;
269587946a72SDaisuke Nishimura 	page = find_get_page(inode->i_mapping, pgoff);
269687946a72SDaisuke Nishimura out:
269787946a72SDaisuke Nishimura 	*pagep = page;
269887946a72SDaisuke Nishimura 	*ent = (swp_entry_t){ .val = 0 };
269987946a72SDaisuke Nishimura }
270087946a72SDaisuke Nishimura #endif
270187946a72SDaisuke Nishimura 
2702853ac43aSMatt Mackall #define shmem_vm_ops				generic_file_vm_ops
27030b0a0806SHugh Dickins #define shmem_file_operations			ramfs_file_operations
2704454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags)	ramfs_get_inode(sb, dir, mode, dev)
27050b0a0806SHugh Dickins #define shmem_acct_size(flags, size)		0
27060b0a0806SHugh Dickins #define shmem_unacct_size(flags, size)		do {} while (0)
2707caefba17SHugh Dickins #define SHMEM_MAX_BYTES				MAX_LFS_FILESIZE
2708853ac43aSMatt Mackall 
2709853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */
2710853ac43aSMatt Mackall 
2711853ac43aSMatt Mackall /* common code */
27121da177e4SLinus Torvalds 
271346711810SRandy Dunlap /**
27141da177e4SLinus Torvalds  * shmem_file_setup - get an unlinked file living in tmpfs
27151da177e4SLinus Torvalds  * @name: name for dentry (to be seen in /proc/<pid>/maps
27161da177e4SLinus Torvalds  * @size: size to be set for the file
27170b0a0806SHugh Dickins  * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
27181da177e4SLinus Torvalds  */
2719168f5ac6SSergei Trofimovich struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
27201da177e4SLinus Torvalds {
27211da177e4SLinus Torvalds 	int error;
27221da177e4SLinus Torvalds 	struct file *file;
27231da177e4SLinus Torvalds 	struct inode *inode;
27242c48b9c4SAl Viro 	struct path path;
27252c48b9c4SAl Viro 	struct dentry *root;
27261da177e4SLinus Torvalds 	struct qstr this;
27271da177e4SLinus Torvalds 
27281da177e4SLinus Torvalds 	if (IS_ERR(shm_mnt))
27291da177e4SLinus Torvalds 		return (void *)shm_mnt;
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds 	if (size < 0 || size > SHMEM_MAX_BYTES)
27321da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
27331da177e4SLinus Torvalds 
27341da177e4SLinus Torvalds 	if (shmem_acct_size(flags, size))
27351da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
27361da177e4SLinus Torvalds 
27371da177e4SLinus Torvalds 	error = -ENOMEM;
27381da177e4SLinus Torvalds 	this.name = name;
27391da177e4SLinus Torvalds 	this.len = strlen(name);
27401da177e4SLinus Torvalds 	this.hash = 0; /* will go */
27411da177e4SLinus Torvalds 	root = shm_mnt->mnt_root;
27422c48b9c4SAl Viro 	path.dentry = d_alloc(root, &this);
27432c48b9c4SAl Viro 	if (!path.dentry)
27441da177e4SLinus Torvalds 		goto put_memory;
27452c48b9c4SAl Viro 	path.mnt = mntget(shm_mnt);
27461da177e4SLinus Torvalds 
27471da177e4SLinus Torvalds 	error = -ENOSPC;
2748454abafeSDmitry Monakhov 	inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
27491da177e4SLinus Torvalds 	if (!inode)
27504b42af81SAl Viro 		goto put_dentry;
27511da177e4SLinus Torvalds 
27522c48b9c4SAl Viro 	d_instantiate(path.dentry, inode);
27531da177e4SLinus Torvalds 	inode->i_size = size;
27541da177e4SLinus Torvalds 	inode->i_nlink = 0;	/* It is unlinked */
2755853ac43aSMatt Mackall #ifndef CONFIG_MMU
2756853ac43aSMatt Mackall 	error = ramfs_nommu_expand_for_mapping(inode, size);
2757853ac43aSMatt Mackall 	if (error)
27584b42af81SAl Viro 		goto put_dentry;
2759853ac43aSMatt Mackall #endif
27604b42af81SAl Viro 
27614b42af81SAl Viro 	error = -ENFILE;
27622c48b9c4SAl Viro 	file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
27634b42af81SAl Viro 		  &shmem_file_operations);
27644b42af81SAl Viro 	if (!file)
27654b42af81SAl Viro 		goto put_dentry;
27664b42af81SAl Viro 
27671da177e4SLinus Torvalds 	return file;
27681da177e4SLinus Torvalds 
27691da177e4SLinus Torvalds put_dentry:
27702c48b9c4SAl Viro 	path_put(&path);
27711da177e4SLinus Torvalds put_memory:
27721da177e4SLinus Torvalds 	shmem_unacct_size(flags, size);
27731da177e4SLinus Torvalds 	return ERR_PTR(error);
27741da177e4SLinus Torvalds }
2775395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup);
27761da177e4SLinus Torvalds 
277746711810SRandy Dunlap /**
27781da177e4SLinus Torvalds  * shmem_zero_setup - setup a shared anonymous mapping
27791da177e4SLinus Torvalds  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
27801da177e4SLinus Torvalds  */
27811da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma)
27821da177e4SLinus Torvalds {
27831da177e4SLinus Torvalds 	struct file *file;
27841da177e4SLinus Torvalds 	loff_t size = vma->vm_end - vma->vm_start;
27851da177e4SLinus Torvalds 
27861da177e4SLinus Torvalds 	file = shmem_file_setup("dev/zero", size, vma->vm_flags);
27871da177e4SLinus Torvalds 	if (IS_ERR(file))
27881da177e4SLinus Torvalds 		return PTR_ERR(file);
27891da177e4SLinus Torvalds 
27901da177e4SLinus Torvalds 	if (vma->vm_file)
27911da177e4SLinus Torvalds 		fput(vma->vm_file);
27921da177e4SLinus Torvalds 	vma->vm_file = file;
27931da177e4SLinus Torvalds 	vma->vm_ops = &shmem_vm_ops;
27941da177e4SLinus Torvalds 	return 0;
27951da177e4SLinus Torvalds }
2796