blob: 5574b00ca771e8779fbd189d7b03c5ed8a8f48c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Resizable virtual memory filesystem for Linux.
3 *
4 * Copyright (C) 2000 Linus Torvalds.
5 * 2000 Transmeta Corp.
6 * 2000-2001 Christoph Rohland
7 * 2000-2001 SAP AG
8 * 2002 Red Hat Inc.
Hugh Dickins0edd73b2005-06-21 17:15:04 -07009 * Copyright (C) 2002-2005 Hugh Dickins.
10 * Copyright (C) 2002-2005 VERITAS Software Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Copyright (C) 2004 Andi Kleen, SuSE Labs
12 *
13 * Extended attribute support for tmpfs:
14 * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
15 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
16 *
Matt Mackall853ac432009-01-06 14:40:20 -080017 * tiny-shmem:
18 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
19 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * This file is released under the GPL.
21 */
22
Matt Mackall853ac432009-01-06 14:40:20 -080023#include <linux/fs.h>
24#include <linux/init.h>
25#include <linux/vfs.h>
26#include <linux/mount.h>
Hugh Dickinscaefba12009-04-13 14:40:12 -070027#include <linux/pagemap.h>
Matt Mackall853ac432009-01-06 14:40:20 -080028#include <linux/file.h>
29#include <linux/mm.h>
30#include <linux/module.h>
Tim Chen7e496292010-08-09 17:19:05 -070031#include <linux/percpu_counter.h>
Matt Mackall853ac432009-01-06 14:40:20 -080032#include <linux/swap.h>
33
34static struct vfsmount *shm_mnt;
35
36#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * This virtual memory filesystem is heavily based on the ramfs. It
39 * extends ramfs by the ability to use swap and honor resource limits
40 * which makes it a completely usable filesystem.
41 */
42
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070043#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070044#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010045#include <linux/posix_acl.h>
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070046#include <linux/generic_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/string.h>
49#include <linux/slab.h>
50#include <linux/backing-dev.h>
51#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/blkdev.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070054#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/security.h>
56#include <linux/swapops.h>
57#include <linux/mempolicy.h>
58#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000059#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070060#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070061#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080062#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040063#include <linux/magic.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <asm/uaccess.h>
66#include <asm/div64.h>
67#include <asm/pgtable.h>
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* Pretend that each entry is of this size in directory's i_size */
73#define BOGO_DIRENT_SIZE 20
74
Eric Parisb09e0fa2011-05-24 17:12:39 -070075struct shmem_xattr {
76 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
77 char *name; /* xattr name */
78 size_t size;
79 char value[0];
80};
81
Hugh Dickins285b2c42011-08-03 16:21:20 -070082/* Flag allocation requirements to shmem_getpage */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083enum sgp_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 SGP_READ, /* don't exceed i_size, don't allocate page */
85 SGP_CACHE, /* don't exceed i_size, may allocate page */
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -080086 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 SGP_WRITE, /* may exceed i_size, may allocate page */
88};
89
Andrew Mortonb76db732008-02-08 04:21:49 -080090#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080091static unsigned long shmem_default_max_blocks(void)
92{
93 return totalram_pages / 2;
94}
95
96static unsigned long shmem_default_max_inodes(void)
97{
98 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
99}
Andrew Mortonb76db732008-02-08 04:21:49 -0800100#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800101
Hugh Dickins68da9f02011-07-25 17:12:34 -0700102static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
103 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
104
105static inline int shmem_getpage(struct inode *inode, pgoff_t index,
106 struct page **pagep, enum sgp_type sgp, int *fault_type)
107{
108 return shmem_getpage_gfp(inode, index, pagep, sgp,
109 mapping_gfp_mask(inode->i_mapping), fault_type);
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
113{
114 return sb->s_fs_info;
115}
116
117/*
118 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
119 * for shared memory and for shared anonymous (/dev/zero) mappings
120 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
121 * consistent with the pre-accounting of private mappings ...
122 */
123static inline int shmem_acct_size(unsigned long flags, loff_t size)
124{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000125 return (flags & VM_NORESERVE) ?
126 0 : security_vm_enough_memory_kern(VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static inline void shmem_unacct_size(unsigned long flags, loff_t size)
130{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000131 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 vm_unacct_memory(VM_ACCT(size));
133}
134
135/*
136 * ... whereas tmpfs objects are accounted incrementally as
137 * pages are allocated, in order to allow huge sparse files.
138 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
139 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
140 */
141static inline int shmem_acct_block(unsigned long flags)
142{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000143 return (flags & VM_NORESERVE) ?
144 security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147static inline void shmem_unacct_blocks(unsigned long flags, long pages)
148{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000149 if (flags & VM_NORESERVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
151}
152
Hugh Dickins759b9772007-03-05 00:30:28 -0800153static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700154static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800155static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800156static const struct inode_operations shmem_inode_operations;
157static const struct inode_operations shmem_dir_inode_operations;
158static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400159static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700161static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 .ra_pages = 0, /* No readahead */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700163 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164};
165
166static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800167static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169static void shmem_free_blocks(struct inode *inode, long pages)
170{
171 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700172 if (sbinfo->max_blocks) {
Tim Chen7e496292010-08-09 17:19:05 -0700173 percpu_counter_add(&sbinfo->used_blocks, -pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 inode->i_blocks -= pages*BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176}
177
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800178static int shmem_reserve_inode(struct super_block *sb)
179{
180 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
181 if (sbinfo->max_inodes) {
182 spin_lock(&sbinfo->stat_lock);
183 if (!sbinfo->free_inodes) {
184 spin_unlock(&sbinfo->stat_lock);
185 return -ENOSPC;
186 }
187 sbinfo->free_inodes--;
188 spin_unlock(&sbinfo->stat_lock);
189 }
190 return 0;
191}
192
193static void shmem_free_inode(struct super_block *sb)
194{
195 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
196 if (sbinfo->max_inodes) {
197 spin_lock(&sbinfo->stat_lock);
198 sbinfo->free_inodes++;
199 spin_unlock(&sbinfo->stat_lock);
200 }
201}
202
Randy Dunlap46711812008-03-19 17:00:41 -0700203/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * shmem_recalc_inode - recalculate the size of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * @inode: inode to recalc
206 *
207 * We have to calculate the free blocks since the mm can drop
208 * undirtied hole pages behind our back.
209 *
210 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
211 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
212 *
213 * It has to be called with the spinlock held.
214 */
215static void shmem_recalc_inode(struct inode *inode)
216{
217 struct shmem_inode_info *info = SHMEM_I(inode);
218 long freed;
219
220 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
221 if (freed > 0) {
222 info->alloced -= freed;
223 shmem_unacct_blocks(info->flags, freed);
224 shmem_free_blocks(inode, freed);
225 }
226}
227
Hugh Dickins285b2c42011-08-03 16:21:20 -0700228static void shmem_put_swap(struct shmem_inode_info *info, pgoff_t index,
229 swp_entry_t swap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700231 if (index < SHMEM_NR_DIRECT)
232 info->i_direct[index] = swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Hugh Dickins285b2c42011-08-03 16:21:20 -0700235static swp_entry_t shmem_get_swap(struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700237 return (index < SHMEM_NR_DIRECT) ?
238 info->i_direct[index] : (swp_entry_t){0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Hugh Dickins285b2c42011-08-03 16:21:20 -0700241void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700243 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700245 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
246 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
247 pgoff_t index;
248 swp_entry_t swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Hugh Dickins285b2c42011-08-03 16:21:20 -0700250 truncate_inode_pages_range(mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -0700251
Hugh Dickins285b2c42011-08-03 16:21:20 -0700252 if (end > SHMEM_NR_DIRECT)
253 end = SHMEM_NR_DIRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 spin_lock(&info->lock);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700256 for (index = start; index < end; index++) {
257 swap = shmem_get_swap(info, index);
258 if (swap.val) {
259 free_swap_and_cache(swap);
260 shmem_put_swap(info, index, (swp_entry_t){0});
261 info->swapped--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263 }
264
Hugh Dickins285b2c42011-08-03 16:21:20 -0700265 if (mapping->nrpages) {
266 spin_unlock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /*
Hugh Dickins285b2c42011-08-03 16:21:20 -0700268 * A page may have meanwhile sneaked in from swap.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700270 truncate_inode_pages_range(mapping, lstart, lend);
271 spin_lock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 shmem_recalc_inode(inode);
275 spin_unlock(&info->lock);
276
Hugh Dickins285b2c42011-08-03 16:21:20 -0700277 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700279EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Hugh Dickins94c1e622011-06-27 16:18:03 -0700281static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 int error;
285
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200286 error = inode_change_ok(inode, attr);
287 if (error)
288 return error;
289
Hugh Dickins94c1e622011-06-27 16:18:03 -0700290 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
291 loff_t oldsize = inode->i_size;
292 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000293 struct page *page = NULL;
294
Hugh Dickins94c1e622011-06-27 16:18:03 -0700295 if (newsize < oldsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /*
297 * If truncating down to a partial page, then
298 * if that page is already allocated, hold it
299 * in memory until the truncation is over, so
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100300 * truncate_partial_page cannot miss it were
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 * it assigned to swap.
302 */
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000303 if (newsize & (PAGE_CACHE_SIZE-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 (void) shmem_getpage(inode,
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000305 newsize >> PAGE_CACHE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 &page, SGP_READ, NULL);
Hugh Dickinsd3602442008-02-04 22:28:44 -0800307 if (page)
308 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700311 if (newsize != oldsize) {
312 i_size_write(inode, newsize);
313 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
314 }
315 if (newsize < oldsize) {
316 loff_t holebegin = round_up(newsize, PAGE_SIZE);
317 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
318 shmem_truncate_range(inode, newsize, (loff_t)-1);
319 /* unmap again to remove racily COWed private pages */
320 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
321 }
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000322 if (page)
323 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200326 setattr_copy(inode, attr);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700327#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200328 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100329 error = generic_acl_chmod(inode);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700330#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return error;
332}
333
Al Viro1f895f72010-06-05 19:10:41 -0400334static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct shmem_inode_info *info = SHMEM_I(inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700337 struct shmem_xattr *xattr, *nxattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000339 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 shmem_unacct_size(info->flags, inode->i_size);
341 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000342 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800344 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800346 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 }
Eric Parisb09e0fa2011-05-24 17:12:39 -0700349
350 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
351 kfree(xattr->name);
352 kfree(xattr);
353 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700354 BUG_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800355 shmem_free_inode(inode->i_sb);
Al Viro1f895f72010-06-05 19:10:41 -0400356 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
360{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700361 struct address_space *mapping = info->vfs_inode.i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 unsigned long idx;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800363 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Hugh Dickins285b2c42011-08-03 16:21:20 -0700365 for (idx = 0; idx < SHMEM_NR_DIRECT; idx++)
366 if (shmem_get_swap(info, idx).val == entry.val)
367 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369found:
Hugh Dickins285b2c42011-08-03 16:21:20 -0700370 spin_lock(&info->lock);
371 if (shmem_get_swap(info, idx).val != entry.val) {
372 spin_unlock(&info->lock);
373 return 0;
374 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800375
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800376 /*
377 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400378 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800379 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700380 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800381 */
382 if (shmem_swaplist.next != &info->swaplist)
383 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800384
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800385 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700386 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
387 * but also to hold up shmem_evict_inode(): so inode cannot be freed
388 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800389 */
Hugh Dickins778dd892011-05-11 15:13:37 -0700390 error = add_to_page_cache_locked(page, mapping, idx, GFP_NOWAIT);
391 /* which does mem_cgroup_uncharge_cache_page on error */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700392
Hugh Dickins48f170f2011-07-25 17:12:37 -0700393 if (error != -ENOMEM) {
Hugh Dickins73b12622008-02-04 22:28:50 -0800394 delete_from_swap_cache(page);
395 set_page_dirty(page);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700396 shmem_put_swap(info, idx, (swp_entry_t){0});
397 info->swapped--;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800398 swap_free(entry);
399 error = 1; /* not an error, but entry was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 spin_unlock(&info->lock);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800402 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405/*
406 * shmem_unuse() search for an eventually swapped out shmem page.
407 */
408int shmem_unuse(swp_entry_t entry, struct page *page)
409{
410 struct list_head *p, *next;
411 struct shmem_inode_info *info;
412 int found = 0;
Hugh Dickins778dd892011-05-11 15:13:37 -0700413 int error;
414
415 /*
416 * Charge page using GFP_KERNEL while we can wait, before taking
417 * the shmem_swaplist_mutex which might hold up shmem_writepage().
418 * Charged back to the user (not to caller) when swap account is used.
419 * add_to_page_cache() will be called with GFP_NOWAIT.
420 */
421 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
422 if (error)
423 goto out;
424 /*
425 * Try to preload while we can wait, to not make a habit of
426 * draining atomic reserves; but don't latch on to this cpu,
427 * it's okay if sometimes we get rescheduled after this.
428 */
429 error = radix_tree_preload(GFP_KERNEL);
430 if (error)
431 goto uncharge;
432 radix_tree_preload_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800434 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 list_for_each_safe(p, next, &shmem_swaplist) {
436 info = list_entry(p, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700437 if (!info->swapped) {
438 spin_lock(&info->lock);
439 if (!info->swapped)
440 list_del_init(&info->swaplist);
441 spin_unlock(&info->lock);
442 }
443 if (info->swapped)
444 found = shmem_unuse_inode(info, entry, page);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800445 cond_resched();
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800446 if (found)
Hugh Dickins778dd892011-05-11 15:13:37 -0700447 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800449 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -0700450
451uncharge:
452 if (!found)
453 mem_cgroup_uncharge_cache_page(page);
454 if (found < 0)
455 error = found;
456out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800457 unlock_page(page);
458 page_cache_release(page);
Hugh Dickins778dd892011-05-11 15:13:37 -0700459 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462/*
463 * Move the page from the page cache to the swap cache.
464 */
465static int shmem_writepage(struct page *page, struct writeback_control *wbc)
466{
467 struct shmem_inode_info *info;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700468 swp_entry_t swap, oswap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct address_space *mapping;
470 unsigned long index;
471 struct inode *inode;
472
473 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 mapping = page->mapping;
475 index = page->index;
476 inode = mapping->host;
477 info = SHMEM_I(inode);
478 if (info->flags & VM_LOCKED)
479 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800480 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 goto redirty;
482
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800483 /*
484 * shmem_backing_dev_info's capabilities prevent regular writeback or
485 * sync from ever calling shmem_writepage; but a stacking filesystem
Hugh Dickins48f170f2011-07-25 17:12:37 -0700486 * might use ->writepage of its underlying filesystem, in which case
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800487 * tmpfs should write out to swap only in response to memory pressure,
Hugh Dickins48f170f2011-07-25 17:12:37 -0700488 * and not for the writeback threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800489 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700490 if (!wbc->for_reclaim) {
491 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
492 goto redirty;
493 }
Hugh Dickins285b2c42011-08-03 16:21:20 -0700494
495 /*
496 * Just for this patch, we have a toy implementation,
497 * which can swap out only the first SHMEM_NR_DIRECT pages:
498 * for simple demonstration of where we need to think about swap.
499 */
500 if (index >= SHMEM_NR_DIRECT)
501 goto redirty;
502
Hugh Dickins48f170f2011-07-25 17:12:37 -0700503 swap = get_swap_page();
504 if (!swap.val)
505 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800506
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700507 /*
508 * Add inode to shmem_unuse()'s list of swapped-out inodes,
509 * if it's not already there. Do it now because we cannot take
510 * mutex while holding spinlock, and must do so before the page
511 * is moved to swap cache, when its pagelock no longer protects
512 * the inode from eviction. But don't unlock the mutex until
513 * we've taken the spinlock, because shmem_unuse_inode() will
514 * prune a !swapped inode from the swaplist under both locks.
515 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700516 mutex_lock(&shmem_swaplist_mutex);
517 if (list_empty(&info->swaplist))
518 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 spin_lock(&info->lock);
Hugh Dickins48f170f2011-07-25 17:12:37 -0700521 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700522
Hugh Dickins285b2c42011-08-03 16:21:20 -0700523 oswap = shmem_get_swap(info, index);
524 if (oswap.val) {
Hugh Dickins48f170f2011-07-25 17:12:37 -0700525 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700526 free_swap_and_cache(oswap);
527 shmem_put_swap(info, index, (swp_entry_t){0});
528 info->swapped--;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800529 }
530 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Hugh Dickins48f170f2011-07-25 17:12:37 -0700532 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Minchan Kim4c73b1b2011-03-22 16:32:40 -0700533 delete_from_page_cache(page);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700534 shmem_put_swap(info, index, swap);
535 info->swapped++;
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800536 swap_shmem_alloc(swap);
Hugh Dickins826267c2011-05-28 13:14:09 -0700537 spin_unlock(&info->lock);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800538 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -0700539 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return 0;
541 }
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 spin_unlock(&info->lock);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700544 swapcache_free(swap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545redirty:
546 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800547 if (wbc->for_reclaim)
548 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
549 unlock_page(page);
550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553#ifdef CONFIG_NUMA
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800554#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700555static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800556{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700557 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800558
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700559 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700560 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800561
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700562 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800563
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700564 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800565}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700566
567static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
568{
569 struct mempolicy *mpol = NULL;
570 if (sbinfo->mpol) {
571 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
572 mpol = sbinfo->mpol;
573 mpol_get(mpol);
574 spin_unlock(&sbinfo->stat_lock);
575 }
576 return mpol;
577}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800578#endif /* CONFIG_TMPFS */
579
Hugh Dickins02098fe2008-02-04 22:28:42 -0800580static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
581 struct shmem_inode_info *info, unsigned long idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700583 struct mempolicy mpol, *spol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct vm_area_struct pvma;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800585 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700587 spol = mpol_cond_copy(&mpol,
588 mpol_shared_policy_lookup(&info->policy, idx));
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /* Create a pseudo vma that just contains the policy */
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800591 pvma.vm_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 pvma.vm_pgoff = idx;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800593 pvma.vm_ops = NULL;
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700594 pvma.vm_policy = spol;
Hugh Dickins02098fe2008-02-04 22:28:42 -0800595 page = swapin_readahead(entry, gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return page;
597}
598
Hugh Dickins02098fe2008-02-04 22:28:42 -0800599static struct page *shmem_alloc_page(gfp_t gfp,
600 struct shmem_inode_info *info, unsigned long idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 struct vm_area_struct pvma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800604 /* Create a pseudo vma that just contains the policy */
605 pvma.vm_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 pvma.vm_pgoff = idx;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800607 pvma.vm_ops = NULL;
608 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700609
610 /*
611 * alloc_page_vma() will drop the shared policy reference
612 */
613 return alloc_page_vma(gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800615#else /* !CONFIG_NUMA */
616#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700617static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *p)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800618{
619}
620#endif /* CONFIG_TMPFS */
621
Hugh Dickins02098fe2008-02-04 22:28:42 -0800622static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
623 struct shmem_inode_info *info, unsigned long idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Hugh Dickins02098fe2008-02-04 22:28:42 -0800625 return swapin_readahead(entry, gfp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Hugh Dickins02098fe2008-02-04 22:28:42 -0800628static inline struct page *shmem_alloc_page(gfp_t gfp,
629 struct shmem_inode_info *info, unsigned long idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Hugh Dickinse84e2e12007-11-28 18:55:10 +0000631 return alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800633#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700635#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
636static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
637{
638 return NULL;
639}
640#endif
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/*
Hugh Dickins68da9f02011-07-25 17:12:34 -0700643 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 *
645 * If we allocate a new one we do not mark it dirty. That's up to the
646 * vm. If we swap it in we mark it dirty since we also free the swap
647 * entry since a page cannot live in both the swap and page cache
648 */
Hugh Dickins68da9f02011-07-25 17:12:34 -0700649static int shmem_getpage_gfp(struct inode *inode, pgoff_t idx,
650 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 struct address_space *mapping = inode->i_mapping;
653 struct shmem_inode_info *info = SHMEM_I(inode);
654 struct shmem_sb_info *sbinfo;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700655 struct page *page;
Shaohua Liff36b8012010-08-09 17:19:06 -0700656 struct page *prealloc_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 swp_entry_t swap;
658 int error;
659
Hugh Dickins285b2c42011-08-03 16:21:20 -0700660 if (idx > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662repeat:
Hugh Dickins27ab7002011-07-25 17:12:36 -0700663 page = find_lock_page(mapping, idx);
664 if (page) {
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800665 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700666 * Once we can get the page lock, it must be uptodate:
667 * if there were an error in reading back from swap,
668 * the page would not be inserted into the filecache.
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800669 */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700670 BUG_ON(!PageUptodate(page));
671 goto done;
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700675 * Try to preload while we can wait, to not make a habit of
676 * draining atomic reserves; but don't latch on to this cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700678 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
679 if (error)
680 goto out;
681 radix_tree_preload_end();
682
683 if (sgp != SGP_READ && !prealloc_page) {
684 prealloc_page = shmem_alloc_page(gfp, info, idx);
685 if (prealloc_page) {
686 SetPageSwapBacked(prealloc_page);
687 if (mem_cgroup_cache_charge(prealloc_page,
688 current->mm, GFP_KERNEL)) {
689 page_cache_release(prealloc_page);
690 prealloc_page = NULL;
Shaohua Liff36b8012010-08-09 17:19:06 -0700691 }
692 }
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 spin_lock(&info->lock);
696 shmem_recalc_inode(inode);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700697 swap = shmem_get_swap(info, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (swap.val) {
699 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700700 page = lookup_swap_cache(swap);
701 if (!page) {
Christoph Lameterf8891e52006-06-30 01:55:45 -0700702 spin_unlock(&info->lock);
Ying Han456f9982011-05-26 16:25:38 -0700703 /* here we actually do the io */
Hugh Dickins68da9f02011-07-25 17:12:34 -0700704 if (fault_type)
705 *fault_type |= VM_FAULT_MAJOR;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700706 page = shmem_swapin(swap, gfp, info, idx);
707 if (!page) {
Hugh Dickins285b2c42011-08-03 16:21:20 -0700708 swp_entry_t nswap = shmem_get_swap(info, idx);
709 if (nswap.val == swap.val) {
710 error = -ENOMEM;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700711 goto out;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto repeat;
714 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700715 wait_on_page_locked(page);
716 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto repeat;
718 }
719
720 /* We have to do this with page locked to prevent races */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700721 if (!trylock_page(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700723 wait_on_page_locked(page);
724 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto repeat;
726 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700727 if (PageWriteback(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700729 wait_on_page_writeback(page);
730 unlock_page(page);
731 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 goto repeat;
733 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700734 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700736 unlock_page(page);
737 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 error = -EIO;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700739 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741
Hugh Dickins27ab7002011-07-25 17:12:36 -0700742 error = add_to_page_cache_locked(page, mapping,
743 idx, GFP_NOWAIT);
744 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 spin_unlock(&info->lock);
Hugh Dickins82369552008-02-07 00:14:22 -0800746 if (error == -ENOMEM) {
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -0700747 /*
748 * reclaim from proper memory cgroup and
749 * call memcg's OOM if needed.
750 */
751 error = mem_cgroup_shmem_charge_fallback(
Hugh Dickins27ab7002011-07-25 17:12:36 -0700752 page, current->mm, gfp);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -0800753 if (error) {
Hugh Dickins27ab7002011-07-25 17:12:36 -0700754 unlock_page(page);
755 page_cache_release(page);
756 goto out;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -0800757 }
Hugh Dickins82369552008-02-07 00:14:22 -0800758 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700759 unlock_page(page);
760 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 goto repeat;
762 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700763
Hugh Dickins27ab7002011-07-25 17:12:36 -0700764 delete_from_swap_cache(page);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700765 shmem_put_swap(info, idx, (swp_entry_t){0});
766 info->swapped--;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700767 spin_unlock(&info->lock);
768 set_page_dirty(page);
769 swap_free(swap);
770
771 } else if (sgp == SGP_READ) {
Hugh Dickins27ab7002011-07-25 17:12:36 -0700772 page = find_get_page(mapping, idx);
773 if (page && !trylock_page(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700775 wait_on_page_locked(page);
776 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 goto repeat;
778 }
779 spin_unlock(&info->lock);
Hugh Dickinse83c32e2011-07-25 17:12:35 -0700780
781 } else if (prealloc_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700783 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -0700784 if (percpu_counter_compare(&sbinfo->used_blocks,
785 sbinfo->max_blocks) >= 0 ||
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700786 shmem_acct_block(info->flags))
787 goto nospace;
Tim Chen7e496292010-08-09 17:19:05 -0700788 percpu_counter_inc(&sbinfo->used_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 inode->i_blocks += BLOCKS_PER_PAGE;
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700790 } else if (shmem_acct_block(info->flags))
791 goto nospace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Hugh Dickins27ab7002011-07-25 17:12:36 -0700793 page = prealloc_page;
794 prealloc_page = NULL;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700795
Hugh Dickins285b2c42011-08-03 16:21:20 -0700796 swap = shmem_get_swap(info, idx);
797 if (swap.val)
Hugh Dickins27ab7002011-07-25 17:12:36 -0700798 mem_cgroup_uncharge_cache_page(page);
799 else
Hugh Dickins285b2c42011-08-03 16:21:20 -0700800 error = add_to_page_cache_lru(page, mapping,
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700801 idx, GFP_NOWAIT);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700802 /*
803 * At add_to_page_cache_lru() failure,
804 * uncharge will be done automatically.
805 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700806 if (swap.val || error) {
Hugh Dickins27ab7002011-07-25 17:12:36 -0700807 shmem_unacct_blocks(info->flags, 1);
808 shmem_free_blocks(inode, 1);
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700809 spin_unlock(&info->lock);
810 page_cache_release(page);
811 goto repeat;
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 info->alloced++;
815 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700816 clear_highpage(page);
817 flush_dcache_page(page);
818 SetPageUptodate(page);
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -0800819 if (sgp == SGP_DIRTY)
Hugh Dickins27ab7002011-07-25 17:12:36 -0700820 set_page_dirty(page);
821
Hugh Dickinse83c32e2011-07-25 17:12:35 -0700822 } else {
823 spin_unlock(&info->lock);
824 error = -ENOMEM;
825 goto out;
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827done:
Hugh Dickins27ab7002011-07-25 17:12:36 -0700828 *pagep = page;
Shaohua Liff36b8012010-08-09 17:19:06 -0700829 error = 0;
Shaohua Liff36b8012010-08-09 17:19:06 -0700830out:
831 if (prealloc_page) {
832 mem_cgroup_uncharge_cache_page(prealloc_page);
833 page_cache_release(prealloc_page);
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return error;
Nick Piggind00806b2007-07-19 01:46:57 -0700836
Hugh Dickins27d54b32008-02-04 22:28:43 -0800837nospace:
Nick Piggind0217ac2007-07-19 01:47:03 -0700838 /*
839 * Perhaps the page was brought in from swap between find_lock_page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 * and taking info->lock? We allow for that at add_to_page_cache_lru,
Nick Piggin83c54072007-07-19 01:47:05 -0700841 * but must also avoid reporting a spurious ENOSPC while working on a
Hugh Dickins9276aad2011-07-25 17:12:34 -0700842 * full tmpfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700844 page = find_get_page(mapping, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700846 if (page) {
847 page_cache_release(page);
848 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -0800849 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700850 error = -ENOSPC;
Hugh Dickinse83c32e2011-07-25 17:12:35 -0700851 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
855{
856 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
857 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -0700858 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
861 return VM_FAULT_SIGBUS;
862
863 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
864 if (error)
865 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700866
Ying Han456f9982011-05-26 16:25:38 -0700867 if (ret & VM_FAULT_MAJOR) {
868 count_vm_event(PGMAJFAULT);
869 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
870 }
Hugh Dickins68da9f02011-07-25 17:12:34 -0700871 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874#ifdef CONFIG_NUMA
875static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
876{
877 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
878 return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
879}
880
Adrian Bunkd8dc74f2007-10-16 01:26:26 -0700881static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
882 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -0800884 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 unsigned long idx;
886
887 idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
888 return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
889}
890#endif
891
892int shmem_lock(struct file *file, int lock, struct user_struct *user)
893{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -0800894 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 struct shmem_inode_info *info = SHMEM_I(inode);
896 int retval = -ENOMEM;
897
898 spin_lock(&info->lock);
899 if (lock && !(info->flags & VM_LOCKED)) {
900 if (!user_shm_lock(inode->i_size, user))
901 goto out_nomem;
902 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -0700903 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905 if (!lock && (info->flags & VM_LOCKED) && user) {
906 user_shm_unlock(inode->i_size, user);
907 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -0700908 mapping_clear_unevictable(file->f_mapping);
909 scan_mapping_unevictable_pages(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -0700912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913out_nomem:
914 spin_unlock(&info->lock);
915 return retval;
916}
917
Adrian Bunk9b83a6a2007-02-28 20:11:03 -0800918static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 file_accessed(file);
921 vma->vm_ops = &shmem_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -0700922 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return 0;
924}
925
Dmitry Monakhov454abaf2010-03-04 17:32:18 +0300926static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
927 int mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 struct inode *inode;
930 struct shmem_inode_info *info;
931 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
932
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800933 if (shmem_reserve_inode(sb))
934 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 inode = new_inode(sb);
937 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400938 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +0300939 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
942 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -0700943 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 info = SHMEM_I(inode);
945 memset(info, 0, (char *)inode - (char *)info);
946 spin_lock_init(&info->lock);
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000947 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 INIT_LIST_HEAD(&info->swaplist);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700949 INIT_LIST_HEAD(&info->xattr_list);
Al Viro72c04902009-06-24 16:58:48 -0400950 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 switch (mode & S_IFMT) {
953 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700954 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 init_special_inode(inode, mode, dev);
956 break;
957 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -0700958 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 inode->i_op = &shmem_inode_operations;
960 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700961 mpol_shared_policy_init(&info->policy,
962 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 break;
964 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -0700965 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /* Some things misbehave if size == 0 on a directory */
967 inode->i_size = 2 * BOGO_DIRENT_SIZE;
968 inode->i_op = &shmem_dir_inode_operations;
969 inode->i_fop = &simple_dir_operations;
970 break;
971 case S_IFLNK:
972 /*
973 * Must not load anything in the rbtree,
974 * mpol_free_shared_policy will not be called.
975 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700976 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 break;
978 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800979 } else
980 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return inode;
982}
983
984#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800985static const struct inode_operations shmem_symlink_inode_operations;
986static const struct inode_operations shmem_symlink_inline_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988static int
Nick Piggin800d15a2007-10-16 01:25:03 -0700989shmem_write_begin(struct file *file, struct address_space *mapping,
990 loff_t pos, unsigned len, unsigned flags,
991 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Nick Piggin800d15a2007-10-16 01:25:03 -0700993 struct inode *inode = mapping->host;
994 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin800d15a2007-10-16 01:25:03 -0700995 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
996}
997
998static int
999shmem_write_end(struct file *file, struct address_space *mapping,
1000 loff_t pos, unsigned len, unsigned copied,
1001 struct page *page, void *fsdata)
1002{
1003 struct inode *inode = mapping->host;
1004
Hugh Dickinsd3602442008-02-04 22:28:44 -08001005 if (pos + copied > inode->i_size)
1006 i_size_write(inode, pos + copied);
1007
Nick Piggin800d15a2007-10-16 01:25:03 -07001008 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001009 unlock_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07001010 page_cache_release(page);
1011
Nick Piggin800d15a2007-10-16 01:25:03 -07001012 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1016{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001017 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 struct address_space *mapping = inode->i_mapping;
1019 unsigned long index, offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001020 enum sgp_type sgp = SGP_READ;
1021
1022 /*
1023 * Might this read be for a stacking filesystem? Then when reading
1024 * holes of a sparse file, we actually need to allocate those pages,
1025 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1026 */
1027 if (segment_eq(get_fs(), KERNEL_DS))
1028 sgp = SGP_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 index = *ppos >> PAGE_CACHE_SHIFT;
1031 offset = *ppos & ~PAGE_CACHE_MASK;
1032
1033 for (;;) {
1034 struct page *page = NULL;
1035 unsigned long end_index, nr, ret;
1036 loff_t i_size = i_size_read(inode);
1037
1038 end_index = i_size >> PAGE_CACHE_SHIFT;
1039 if (index > end_index)
1040 break;
1041 if (index == end_index) {
1042 nr = i_size & ~PAGE_CACHE_MASK;
1043 if (nr <= offset)
1044 break;
1045 }
1046
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001047 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (desc->error) {
1049 if (desc->error == -EINVAL)
1050 desc->error = 0;
1051 break;
1052 }
Hugh Dickinsd3602442008-02-04 22:28:44 -08001053 if (page)
1054 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 /*
1057 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001058 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 */
1060 nr = PAGE_CACHE_SIZE;
1061 i_size = i_size_read(inode);
1062 end_index = i_size >> PAGE_CACHE_SHIFT;
1063 if (index == end_index) {
1064 nr = i_size & ~PAGE_CACHE_MASK;
1065 if (nr <= offset) {
1066 if (page)
1067 page_cache_release(page);
1068 break;
1069 }
1070 }
1071 nr -= offset;
1072
1073 if (page) {
1074 /*
1075 * If users can be writing to this page using arbitrary
1076 * virtual addresses, take care about potential aliasing
1077 * before reading the page on the kernel side.
1078 */
1079 if (mapping_writably_mapped(mapping))
1080 flush_dcache_page(page);
1081 /*
1082 * Mark the page accessed if we read the beginning.
1083 */
1084 if (!offset)
1085 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07001086 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 page = ZERO_PAGE(0);
Nick Pigginb5810032005-10-29 18:16:12 -07001088 page_cache_get(page);
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 /*
1092 * Ok, we have the page, and it's up-to-date, so
1093 * now we can copy it to user space...
1094 *
1095 * The actor routine returns how many bytes were actually used..
1096 * NOTE! This may not be the same as how much of a user buffer
1097 * we filled up (we may be padding etc), so we can only update
1098 * "pos" here (the actor routine has to update the user buffer
1099 * pointers and the remaining count).
1100 */
1101 ret = actor(desc, page, offset, nr);
1102 offset += ret;
1103 index += offset >> PAGE_CACHE_SHIFT;
1104 offset &= ~PAGE_CACHE_MASK;
1105
1106 page_cache_release(page);
1107 if (ret != nr || !desc->count)
1108 break;
1109
1110 cond_resched();
1111 }
1112
1113 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1114 file_accessed(filp);
1115}
1116
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001117static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1118 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001120 struct file *filp = iocb->ki_filp;
1121 ssize_t retval;
1122 unsigned long seg;
1123 size_t count;
1124 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001126 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1127 if (retval)
1128 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001130 for (seg = 0; seg < nr_segs; seg++) {
1131 read_descriptor_t desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001133 desc.written = 0;
1134 desc.arg.buf = iov[seg].iov_base;
1135 desc.count = iov[seg].iov_len;
1136 if (desc.count == 0)
1137 continue;
1138 desc.error = 0;
1139 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1140 retval += desc.written;
1141 if (desc.error) {
1142 retval = retval ?: desc.error;
1143 break;
1144 }
1145 if (desc.count > 0)
1146 break;
1147 }
1148 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Hugh Dickins708e3502011-07-25 17:12:32 -07001151static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1152 struct pipe_inode_info *pipe, size_t len,
1153 unsigned int flags)
1154{
1155 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001156 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07001157 unsigned int loff, nr_pages, req_pages;
1158 struct page *pages[PIPE_DEF_BUFFERS];
1159 struct partial_page partial[PIPE_DEF_BUFFERS];
1160 struct page *page;
1161 pgoff_t index, end_index;
1162 loff_t isize, left;
1163 int error, page_nr;
1164 struct splice_pipe_desc spd = {
1165 .pages = pages,
1166 .partial = partial,
1167 .flags = flags,
1168 .ops = &page_cache_pipe_buf_ops,
1169 .spd_release = spd_release_page,
1170 };
1171
Hugh Dickins71f0e072011-07-25 17:12:33 -07001172 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001173 if (unlikely(*ppos >= isize))
1174 return 0;
1175
1176 left = isize - *ppos;
1177 if (unlikely(left < len))
1178 len = left;
1179
1180 if (splice_grow_spd(pipe, &spd))
1181 return -ENOMEM;
1182
1183 index = *ppos >> PAGE_CACHE_SHIFT;
1184 loff = *ppos & ~PAGE_CACHE_MASK;
1185 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1186 nr_pages = min(req_pages, pipe->buffers);
1187
Hugh Dickins708e3502011-07-25 17:12:32 -07001188 spd.nr_pages = find_get_pages_contig(mapping, index,
1189 nr_pages, spd.pages);
1190 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07001191 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001192
Hugh Dickins708e3502011-07-25 17:12:32 -07001193 while (spd.nr_pages < nr_pages) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001194 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1195 if (error)
1196 break;
1197 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07001198 spd.pages[spd.nr_pages++] = page;
1199 index++;
1200 }
1201
Hugh Dickins708e3502011-07-25 17:12:32 -07001202 index = *ppos >> PAGE_CACHE_SHIFT;
1203 nr_pages = spd.nr_pages;
1204 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001205
Hugh Dickins708e3502011-07-25 17:12:32 -07001206 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1207 unsigned int this_len;
1208
1209 if (!len)
1210 break;
1211
Hugh Dickins708e3502011-07-25 17:12:32 -07001212 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1213 page = spd.pages[page_nr];
1214
Hugh Dickins71f0e072011-07-25 17:12:33 -07001215 if (!PageUptodate(page) || page->mapping != mapping) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001216 error = shmem_getpage(inode, index, &page,
1217 SGP_CACHE, NULL);
1218 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07001219 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001220 unlock_page(page);
1221 page_cache_release(spd.pages[page_nr]);
1222 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07001223 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07001224
1225 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001226 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1227 if (unlikely(!isize || index > end_index))
1228 break;
1229
Hugh Dickins708e3502011-07-25 17:12:32 -07001230 if (end_index == index) {
1231 unsigned int plen;
1232
Hugh Dickins708e3502011-07-25 17:12:32 -07001233 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1234 if (plen <= loff)
1235 break;
1236
Hugh Dickins708e3502011-07-25 17:12:32 -07001237 this_len = min(this_len, plen - loff);
1238 len = this_len;
1239 }
1240
1241 spd.partial[page_nr].offset = loff;
1242 spd.partial[page_nr].len = this_len;
1243 len -= this_len;
1244 loff = 0;
1245 spd.nr_pages++;
1246 index++;
1247 }
1248
Hugh Dickins708e3502011-07-25 17:12:32 -07001249 while (page_nr < nr_pages)
1250 page_cache_release(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07001251
1252 if (spd.nr_pages)
1253 error = splice_to_pipe(pipe, &spd);
1254
1255 splice_shrink_spd(pipe, &spd);
1256
1257 if (error > 0) {
1258 *ppos += error;
1259 file_accessed(in);
1260 }
1261 return error;
1262}
1263
David Howells726c3342006-06-23 02:02:58 -07001264static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
David Howells726c3342006-06-23 02:02:58 -07001266 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 buf->f_type = TMPFS_MAGIC;
1269 buf->f_bsize = PAGE_CACHE_SIZE;
1270 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001271 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 buf->f_blocks = sbinfo->max_blocks;
Tim Chen7e496292010-08-09 17:19:05 -07001273 buf->f_bavail = buf->f_bfree =
1274 sbinfo->max_blocks - percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001275 }
1276 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 buf->f_files = sbinfo->max_inodes;
1278 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280 /* else leave those fields 0 like simple_statfs */
1281 return 0;
1282}
1283
1284/*
1285 * File creation. Allocate an inode, and we're done..
1286 */
1287static int
1288shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1289{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001290 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 int error = -ENOSPC;
1292
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001293 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (inode) {
Eric Paris2a7dba32011-02-01 11:05:39 -05001295 error = security_inode_init_security(inode, dir,
1296 &dentry->d_name, NULL,
1297 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001298 if (error) {
1299 if (error != -EOPNOTSUPP) {
1300 iput(inode);
1301 return error;
1302 }
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001303 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001304#ifdef CONFIG_TMPFS_POSIX_ACL
1305 error = generic_acl_init(inode, dir);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001306 if (error) {
1307 iput(inode);
1308 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001309 }
Al Viro718deb62009-12-16 19:35:36 -05001310#else
1311 error = 0;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001312#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 dir->i_size += BOGO_DIRENT_SIZE;
1314 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1315 d_instantiate(dentry, inode);
1316 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318 return error;
1319}
1320
1321static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1322{
1323 int error;
1324
1325 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1326 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07001327 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return 0;
1329}
1330
1331static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1332 struct nameidata *nd)
1333{
1334 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1335}
1336
1337/*
1338 * Link a file..
1339 */
1340static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1341{
1342 struct inode *inode = old_dentry->d_inode;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001343 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 /*
1346 * No ordinary (disk based) filesystem counts links as inodes;
1347 * but each new link needs a new dentry, pinning lowmem, and
1348 * tmpfs dentries cannot be pruned until they are unlinked.
1349 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001350 ret = shmem_reserve_inode(inode->i_sb);
1351 if (ret)
1352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 dir->i_size += BOGO_DIRENT_SIZE;
1355 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07001356 inc_nlink(inode);
Al Viro7de9c6e2010-10-23 11:11:40 -04001357 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 dget(dentry); /* Extra pinning count for the created dentry */
1359 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001360out:
1361 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
1364static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1365{
1366 struct inode *inode = dentry->d_inode;
1367
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001368 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1369 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 dir->i_size -= BOGO_DIRENT_SIZE;
1372 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001373 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 dput(dentry); /* Undo the count from "create" - this does all the work */
1375 return 0;
1376}
1377
1378static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1379{
1380 if (!simple_empty(dentry))
1381 return -ENOTEMPTY;
1382
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001383 drop_nlink(dentry->d_inode);
1384 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return shmem_unlink(dir, dentry);
1386}
1387
1388/*
1389 * The VFS layer already does all the dentry stuff for rename,
1390 * we just have to decrement the usage count for the target if
1391 * it exists so that the VFS layer correctly free's it when it
1392 * gets overwritten.
1393 */
1394static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1395{
1396 struct inode *inode = old_dentry->d_inode;
1397 int they_are_dirs = S_ISDIR(inode->i_mode);
1398
1399 if (!simple_empty(new_dentry))
1400 return -ENOTEMPTY;
1401
1402 if (new_dentry->d_inode) {
1403 (void) shmem_unlink(new_dir, new_dentry);
1404 if (they_are_dirs)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001405 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001407 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07001408 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
1411 old_dir->i_size -= BOGO_DIRENT_SIZE;
1412 new_dir->i_size += BOGO_DIRENT_SIZE;
1413 old_dir->i_ctime = old_dir->i_mtime =
1414 new_dir->i_ctime = new_dir->i_mtime =
1415 inode->i_ctime = CURRENT_TIME;
1416 return 0;
1417}
1418
1419static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1420{
1421 int error;
1422 int len;
1423 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07001424 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 char *kaddr;
1426 struct shmem_inode_info *info;
1427
1428 len = strlen(symname) + 1;
1429 if (len > PAGE_CACHE_SIZE)
1430 return -ENAMETOOLONG;
1431
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001432 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (!inode)
1434 return -ENOSPC;
1435
Eric Paris2a7dba32011-02-01 11:05:39 -05001436 error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
1437 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001438 if (error) {
1439 if (error != -EOPNOTSUPP) {
1440 iput(inode);
1441 return error;
1442 }
1443 error = 0;
1444 }
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 info = SHMEM_I(inode);
1447 inode->i_size = len-1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07001448 if (len <= SHMEM_SYMLINK_INLINE_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /* do it inline */
Eric Parisb09e0fa2011-05-24 17:12:39 -07001450 memcpy(info->inline_symlink, symname, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 inode->i_op = &shmem_symlink_inline_operations;
1452 } else {
1453 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1454 if (error) {
1455 iput(inode);
1456 return error;
1457 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07001458 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 inode->i_op = &shmem_symlink_inode_operations;
1460 kaddr = kmap_atomic(page, KM_USER0);
1461 memcpy(kaddr, symname, len);
1462 kunmap_atomic(kaddr, KM_USER0);
1463 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001464 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 page_cache_release(page);
1466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 dir->i_size += BOGO_DIRENT_SIZE;
1468 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1469 d_instantiate(dentry, inode);
1470 dget(dentry);
1471 return 0;
1472}
1473
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001474static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Eric Parisb09e0fa2011-05-24 17:12:39 -07001476 nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001477 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001480static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 struct page *page = NULL;
1483 int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1484 nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
Hugh Dickinsd3602442008-02-04 22:28:44 -08001485 if (page)
1486 unlock_page(page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001487 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001490static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 if (!IS_ERR(nd_get_link(nd))) {
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001493 struct page *page = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 kunmap(page);
1495 mark_page_accessed(page);
1496 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498}
1499
Eric Parisb09e0fa2011-05-24 17:12:39 -07001500#ifdef CONFIG_TMPFS_XATTR
1501/*
1502 * Superblocks without xattr inode operations may get some security.* xattr
1503 * support from the LSM "for free". As soon as we have any other xattrs
1504 * like ACLs, we also need to implement the security.* handlers at
1505 * filesystem level, though.
1506 */
1507
1508static int shmem_xattr_get(struct dentry *dentry, const char *name,
1509 void *buffer, size_t size)
1510{
1511 struct shmem_inode_info *info;
1512 struct shmem_xattr *xattr;
1513 int ret = -ENODATA;
1514
1515 info = SHMEM_I(dentry->d_inode);
1516
1517 spin_lock(&info->lock);
1518 list_for_each_entry(xattr, &info->xattr_list, list) {
1519 if (strcmp(name, xattr->name))
1520 continue;
1521
1522 ret = xattr->size;
1523 if (buffer) {
1524 if (size < xattr->size)
1525 ret = -ERANGE;
1526 else
1527 memcpy(buffer, xattr->value, xattr->size);
1528 }
1529 break;
1530 }
1531 spin_unlock(&info->lock);
1532 return ret;
1533}
1534
1535static int shmem_xattr_set(struct dentry *dentry, const char *name,
1536 const void *value, size_t size, int flags)
1537{
1538 struct inode *inode = dentry->d_inode;
1539 struct shmem_inode_info *info = SHMEM_I(inode);
1540 struct shmem_xattr *xattr;
1541 struct shmem_xattr *new_xattr = NULL;
1542 size_t len;
1543 int err = 0;
1544
1545 /* value == NULL means remove */
1546 if (value) {
1547 /* wrap around? */
1548 len = sizeof(*new_xattr) + size;
1549 if (len <= sizeof(*new_xattr))
1550 return -ENOMEM;
1551
1552 new_xattr = kmalloc(len, GFP_KERNEL);
1553 if (!new_xattr)
1554 return -ENOMEM;
1555
1556 new_xattr->name = kstrdup(name, GFP_KERNEL);
1557 if (!new_xattr->name) {
1558 kfree(new_xattr);
1559 return -ENOMEM;
1560 }
1561
1562 new_xattr->size = size;
1563 memcpy(new_xattr->value, value, size);
1564 }
1565
1566 spin_lock(&info->lock);
1567 list_for_each_entry(xattr, &info->xattr_list, list) {
1568 if (!strcmp(name, xattr->name)) {
1569 if (flags & XATTR_CREATE) {
1570 xattr = new_xattr;
1571 err = -EEXIST;
1572 } else if (new_xattr) {
1573 list_replace(&xattr->list, &new_xattr->list);
1574 } else {
1575 list_del(&xattr->list);
1576 }
1577 goto out;
1578 }
1579 }
1580 if (flags & XATTR_REPLACE) {
1581 xattr = new_xattr;
1582 err = -ENODATA;
1583 } else {
1584 list_add(&new_xattr->list, &info->xattr_list);
1585 xattr = NULL;
1586 }
1587out:
1588 spin_unlock(&info->lock);
1589 if (xattr)
1590 kfree(xattr->name);
1591 kfree(xattr);
1592 return err;
1593}
1594
1595
1596static const struct xattr_handler *shmem_xattr_handlers[] = {
1597#ifdef CONFIG_TMPFS_POSIX_ACL
1598 &generic_acl_access_handler,
1599 &generic_acl_default_handler,
1600#endif
1601 NULL
1602};
1603
1604static int shmem_xattr_validate(const char *name)
1605{
1606 struct { const char *prefix; size_t len; } arr[] = {
1607 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1608 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1609 };
1610 int i;
1611
1612 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1613 size_t preflen = arr[i].len;
1614 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1615 if (!name[preflen])
1616 return -EINVAL;
1617 return 0;
1618 }
1619 }
1620 return -EOPNOTSUPP;
1621}
1622
1623static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1624 void *buffer, size_t size)
1625{
1626 int err;
1627
1628 /*
1629 * If this is a request for a synthetic attribute in the system.*
1630 * namespace use the generic infrastructure to resolve a handler
1631 * for it via sb->s_xattr.
1632 */
1633 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1634 return generic_getxattr(dentry, name, buffer, size);
1635
1636 err = shmem_xattr_validate(name);
1637 if (err)
1638 return err;
1639
1640 return shmem_xattr_get(dentry, name, buffer, size);
1641}
1642
1643static int shmem_setxattr(struct dentry *dentry, const char *name,
1644 const void *value, size_t size, int flags)
1645{
1646 int err;
1647
1648 /*
1649 * If this is a request for a synthetic attribute in the system.*
1650 * namespace use the generic infrastructure to resolve a handler
1651 * for it via sb->s_xattr.
1652 */
1653 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1654 return generic_setxattr(dentry, name, value, size, flags);
1655
1656 err = shmem_xattr_validate(name);
1657 if (err)
1658 return err;
1659
1660 if (size == 0)
1661 value = ""; /* empty EA, do not remove */
1662
1663 return shmem_xattr_set(dentry, name, value, size, flags);
1664
1665}
1666
1667static int shmem_removexattr(struct dentry *dentry, const char *name)
1668{
1669 int err;
1670
1671 /*
1672 * If this is a request for a synthetic attribute in the system.*
1673 * namespace use the generic infrastructure to resolve a handler
1674 * for it via sb->s_xattr.
1675 */
1676 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1677 return generic_removexattr(dentry, name);
1678
1679 err = shmem_xattr_validate(name);
1680 if (err)
1681 return err;
1682
1683 return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
1684}
1685
1686static bool xattr_is_trusted(const char *name)
1687{
1688 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1689}
1690
1691static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
1692{
1693 bool trusted = capable(CAP_SYS_ADMIN);
1694 struct shmem_xattr *xattr;
1695 struct shmem_inode_info *info;
1696 size_t used = 0;
1697
1698 info = SHMEM_I(dentry->d_inode);
1699
1700 spin_lock(&info->lock);
1701 list_for_each_entry(xattr, &info->xattr_list, list) {
1702 size_t len;
1703
1704 /* skip "trusted." attributes for unprivileged callers */
1705 if (!trusted && xattr_is_trusted(xattr->name))
1706 continue;
1707
1708 len = strlen(xattr->name) + 1;
1709 used += len;
1710 if (buffer) {
1711 if (size < used) {
1712 used = -ERANGE;
1713 break;
1714 }
1715 memcpy(buffer, xattr->name, len);
1716 buffer += len;
1717 }
1718 }
1719 spin_unlock(&info->lock);
1720
1721 return used;
1722}
1723#endif /* CONFIG_TMPFS_XATTR */
1724
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001725static const struct inode_operations shmem_symlink_inline_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 .readlink = generic_readlink,
1727 .follow_link = shmem_follow_link_inline,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001728#ifdef CONFIG_TMPFS_XATTR
1729 .setxattr = shmem_setxattr,
1730 .getxattr = shmem_getxattr,
1731 .listxattr = shmem_listxattr,
1732 .removexattr = shmem_removexattr,
1733#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734};
1735
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001736static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 .readlink = generic_readlink,
1738 .follow_link = shmem_follow_link,
1739 .put_link = shmem_put_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001740#ifdef CONFIG_TMPFS_XATTR
1741 .setxattr = shmem_setxattr,
1742 .getxattr = shmem_getxattr,
1743 .listxattr = shmem_listxattr,
1744 .removexattr = shmem_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001745#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07001746};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001747
David M. Grimes91828a42006-10-17 00:09:45 -07001748static struct dentry *shmem_get_parent(struct dentry *child)
1749{
1750 return ERR_PTR(-ESTALE);
1751}
1752
1753static int shmem_match(struct inode *ino, void *vfh)
1754{
1755 __u32 *fh = vfh;
1756 __u64 inum = fh[2];
1757 inum = (inum << 32) | fh[1];
1758 return ino->i_ino == inum && fh[0] == ino->i_generation;
1759}
1760
Christoph Hellwig480b1162007-10-21 16:42:13 -07001761static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
1762 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07001763{
David M. Grimes91828a42006-10-17 00:09:45 -07001764 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07001765 struct dentry *dentry = NULL;
1766 u64 inum = fid->raw[2];
1767 inum = (inum << 32) | fid->raw[1];
David M. Grimes91828a42006-10-17 00:09:45 -07001768
Christoph Hellwig480b1162007-10-21 16:42:13 -07001769 if (fh_len < 3)
1770 return NULL;
1771
1772 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
1773 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07001774 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07001775 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07001776 iput(inode);
1777 }
1778
Christoph Hellwig480b1162007-10-21 16:42:13 -07001779 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07001780}
1781
1782static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
1783 int connectable)
1784{
1785 struct inode *inode = dentry->d_inode;
1786
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301787 if (*len < 3) {
1788 *len = 3;
David M. Grimes91828a42006-10-17 00:09:45 -07001789 return 255;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301790 }
David M. Grimes91828a42006-10-17 00:09:45 -07001791
Al Viro1d3382c2010-10-23 15:19:20 -04001792 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07001793 /* Unfortunately insert_inode_hash is not idempotent,
1794 * so as we hash inodes here rather than at creation
1795 * time, we need a lock to ensure we only try
1796 * to do it once
1797 */
1798 static DEFINE_SPINLOCK(lock);
1799 spin_lock(&lock);
Al Viro1d3382c2010-10-23 15:19:20 -04001800 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07001801 __insert_inode_hash(inode,
1802 inode->i_ino + inode->i_generation);
1803 spin_unlock(&lock);
1804 }
1805
1806 fh[0] = inode->i_generation;
1807 fh[1] = inode->i_ino;
1808 fh[2] = ((__u64)inode->i_ino) >> 32;
1809
1810 *len = 3;
1811 return 1;
1812}
1813
Christoph Hellwig39655162007-10-21 16:42:17 -07001814static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07001815 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07001816 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07001817 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07001818};
1819
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001820static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
1821 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
1823 char *this_char, *value, *rest;
1824
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00001825 while (options != NULL) {
1826 this_char = options;
1827 for (;;) {
1828 /*
1829 * NUL-terminate this option: unfortunately,
1830 * mount options form a comma-separated list,
1831 * but mpol's nodelist may also contain commas.
1832 */
1833 options = strchr(options, ',');
1834 if (options == NULL)
1835 break;
1836 options++;
1837 if (!isdigit(*options)) {
1838 options[-1] = '\0';
1839 break;
1840 }
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (!*this_char)
1843 continue;
1844 if ((value = strchr(this_char,'=')) != NULL) {
1845 *value++ = 0;
1846 } else {
1847 printk(KERN_ERR
1848 "tmpfs: No value for mount option '%s'\n",
1849 this_char);
1850 return 1;
1851 }
1852
1853 if (!strcmp(this_char,"size")) {
1854 unsigned long long size;
1855 size = memparse(value,&rest);
1856 if (*rest == '%') {
1857 size <<= PAGE_SHIFT;
1858 size *= totalram_pages;
1859 do_div(size, 100);
1860 rest++;
1861 }
1862 if (*rest)
1863 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001864 sbinfo->max_blocks =
1865 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001867 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (*rest)
1869 goto bad_val;
1870 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001871 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (*rest)
1873 goto bad_val;
1874 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001875 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001877 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (*rest)
1879 goto bad_val;
1880 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001881 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001883 sbinfo->uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (*rest)
1885 goto bad_val;
1886 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001887 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001889 sbinfo->gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (*rest)
1891 goto bad_val;
Robin Holt7339ff82006-01-14 13:20:48 -08001892 } else if (!strcmp(this_char,"mpol")) {
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001893 if (mpol_parse_str(value, &sbinfo->mpol, 1))
Robin Holt7339ff82006-01-14 13:20:48 -08001894 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 } else {
1896 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
1897 this_char);
1898 return 1;
1899 }
1900 }
1901 return 0;
1902
1903bad_val:
1904 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
1905 value, this_char);
1906 return 1;
1907
1908}
1909
1910static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
1911{
1912 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001913 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001914 unsigned long inodes;
1915 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001917 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001918 return error;
1919
1920 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001921 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07001922 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001923 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001924 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001925 goto out;
1926 /*
1927 * Those tests also disallow limited->unlimited while any are in
1928 * use, so i_blocks will always be zero when max_blocks is zero;
1929 * but we must separately disallow unlimited->limited, because
1930 * in that case we have no record of how much is already in use.
1931 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001932 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001933 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001934 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001935 goto out;
1936
1937 error = 0;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001938 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001939 sbinfo->max_inodes = config.max_inodes;
1940 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001941
1942 mpol_put(sbinfo->mpol);
1943 sbinfo->mpol = config.mpol; /* transfers initial ref */
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001944out:
1945 spin_unlock(&sbinfo->stat_lock);
1946 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001948
1949static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
1950{
1951 struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
1952
1953 if (sbinfo->max_blocks != shmem_default_max_blocks())
1954 seq_printf(seq, ",size=%luk",
1955 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
1956 if (sbinfo->max_inodes != shmem_default_max_inodes())
1957 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
1958 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
1959 seq_printf(seq, ",mode=%03o", sbinfo->mode);
1960 if (sbinfo->uid != 0)
1961 seq_printf(seq, ",uid=%u", sbinfo->uid);
1962 if (sbinfo->gid != 0)
1963 seq_printf(seq, ",gid=%u", sbinfo->gid);
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001964 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001965 return 0;
1966}
1967#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969static void shmem_put_super(struct super_block *sb)
1970{
Hugh Dickins602586a2010-08-17 15:23:56 -07001971 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1972
1973 percpu_counter_destroy(&sbinfo->used_blocks);
1974 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 sb->s_fs_info = NULL;
1976}
1977
Kay Sievers2b2af542009-04-30 15:23:42 +02001978int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 struct inode *inode;
1981 struct dentry *root;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001982 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001983 int err = -ENOMEM;
1984
1985 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07001986 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001987 L1_CACHE_BYTES), GFP_KERNEL);
1988 if (!sbinfo)
1989 return -ENOMEM;
1990
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001991 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11001992 sbinfo->uid = current_fsuid();
1993 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001994 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001996#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 /*
1998 * Per default we only allow half of the physical ram per
1999 * tmpfs instance, limiting inodes to one per page of lowmem;
2000 * but the internal instance is left unlimited.
2001 */
2002 if (!(sb->s_flags & MS_NOUSER)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002003 sbinfo->max_blocks = shmem_default_max_blocks();
2004 sbinfo->max_inodes = shmem_default_max_inodes();
2005 if (shmem_parse_options(data, sbinfo, false)) {
2006 err = -EINVAL;
2007 goto failed;
2008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
David M. Grimes91828a42006-10-17 00:09:45 -07002010 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011#else
2012 sb->s_flags |= MS_NOUSER;
2013#endif
2014
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002015 spin_lock_init(&sbinfo->stat_lock);
Hugh Dickins602586a2010-08-17 15:23:56 -07002016 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2017 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002018 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002019
Hugh Dickins285b2c42011-08-03 16:21:20 -07002020 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 sb->s_blocksize = PAGE_CACHE_SIZE;
2022 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2023 sb->s_magic = TMPFS_MAGIC;
2024 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01002025 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002026#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002027 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002028#endif
2029#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002030 sb->s_flags |= MS_POSIXACL;
2031#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002032
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002033 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (!inode)
2035 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002036 inode->i_uid = sbinfo->uid;
2037 inode->i_gid = sbinfo->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 root = d_alloc_root(inode);
2039 if (!root)
2040 goto failed_iput;
2041 sb->s_root = root;
2042 return 0;
2043
2044failed_iput:
2045 iput(inode);
2046failed:
2047 shmem_put_super(sb);
2048 return err;
2049}
2050
Pekka Enbergfcc234f2006-03-22 00:08:13 -08002051static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053static struct inode *shmem_alloc_inode(struct super_block *sb)
2054{
2055 struct shmem_inode_info *p;
Christoph Lametere94b1762006-12-06 20:33:17 -08002056 p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (!p)
2058 return NULL;
2059 return &p->vfs_inode;
2060}
2061
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002062static void shmem_i_callback(struct rcu_head *head)
2063{
2064 struct inode *inode = container_of(head, struct inode, i_rcu);
2065 INIT_LIST_HEAD(&inode->i_dentry);
2066 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2067}
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069static void shmem_destroy_inode(struct inode *inode)
2070{
2071 if ((inode->i_mode & S_IFMT) == S_IFREG) {
2072 /* only struct inode is valid if it's an inline symlink */
2073 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2074 }
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002075 call_rcu(&inode->i_rcu, shmem_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002078static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
2080 struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
2081
Christoph Lametera35afb82007-05-16 22:10:57 -07002082 inode_init_once(&p->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
2084
2085static int init_inodecache(void)
2086{
2087 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2088 sizeof(struct shmem_inode_info),
Alexey Dobriyan040b5c62007-10-16 23:26:10 -07002089 0, SLAB_PANIC, init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return 0;
2091}
2092
2093static void destroy_inodecache(void)
2094{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002095 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002098static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08002100 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07002102 .write_begin = shmem_write_begin,
2103 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104#endif
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07002105 .migratepage = migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +02002106 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107};
2108
Helge Deller15ad7cd2006-12-06 20:40:36 -08002109static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 .mmap = shmem_mmap,
2111#ifdef CONFIG_TMPFS
2112 .llseek = generic_file_llseek,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002113 .read = do_sync_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002114 .write = do_sync_write,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002115 .aio_read = shmem_file_aio_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002116 .aio_write = generic_file_aio_write,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02002117 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07002118 .splice_read = shmem_file_splice_read,
Hugh Dickinsae976412007-06-04 10:00:39 +02002119 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120#endif
2121};
2122
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002123static const struct inode_operations shmem_inode_operations = {
Hugh Dickins94c1e622011-06-27 16:18:03 -07002124 .setattr = shmem_setattr,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08002125 .truncate_range = shmem_truncate_range,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002126#ifdef CONFIG_TMPFS_XATTR
2127 .setxattr = shmem_setxattr,
2128 .getxattr = shmem_getxattr,
2129 .listxattr = shmem_listxattr,
2130 .removexattr = shmem_removexattr,
2131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132};
2133
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002134static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135#ifdef CONFIG_TMPFS
2136 .create = shmem_create,
2137 .lookup = simple_lookup,
2138 .link = shmem_link,
2139 .unlink = shmem_unlink,
2140 .symlink = shmem_symlink,
2141 .mkdir = shmem_mkdir,
2142 .rmdir = shmem_rmdir,
2143 .mknod = shmem_mknod,
2144 .rename = shmem_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002146#ifdef CONFIG_TMPFS_XATTR
2147 .setxattr = shmem_setxattr,
2148 .getxattr = shmem_getxattr,
2149 .listxattr = shmem_listxattr,
2150 .removexattr = shmem_removexattr,
2151#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002152#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002153 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002154#endif
2155};
2156
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002157static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07002158#ifdef CONFIG_TMPFS_XATTR
2159 .setxattr = shmem_setxattr,
2160 .getxattr = shmem_getxattr,
2161 .listxattr = shmem_listxattr,
2162 .removexattr = shmem_removexattr,
2163#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002164#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002165 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002166#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167};
2168
Hugh Dickins759b9772007-03-05 00:30:28 -08002169static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 .alloc_inode = shmem_alloc_inode,
2171 .destroy_inode = shmem_destroy_inode,
2172#ifdef CONFIG_TMPFS
2173 .statfs = shmem_statfs,
2174 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002175 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176#endif
Al Viro1f895f72010-06-05 19:10:41 -04002177 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 .drop_inode = generic_delete_inode,
2179 .put_super = shmem_put_super,
2180};
2181
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002182static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002183 .fault = shmem_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184#ifdef CONFIG_NUMA
2185 .set_policy = shmem_set_policy,
2186 .get_policy = shmem_get_policy,
2187#endif
2188};
2189
2190
Al Viro3c26ff62010-07-25 11:46:36 +04002191static struct dentry *shmem_mount(struct file_system_type *fs_type,
2192 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Al Viro3c26ff62010-07-25 11:46:36 +04002194 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
2197static struct file_system_type tmpfs_fs_type = {
2198 .owner = THIS_MODULE,
2199 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002200 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 .kill_sb = kill_litter_super,
2202};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Kay Sievers2b2af542009-04-30 15:23:42 +02002204int __init init_tmpfs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
2206 int error;
2207
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002208 error = bdi_init(&shmem_backing_dev_info);
2209 if (error)
2210 goto out4;
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 error = init_inodecache();
2213 if (error)
2214 goto out3;
2215
2216 error = register_filesystem(&tmpfs_fs_type);
2217 if (error) {
2218 printk(KERN_ERR "Could not register tmpfs\n");
2219 goto out2;
2220 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07002221
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -04002222 shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 tmpfs_fs_type.name, NULL);
2224 if (IS_ERR(shm_mnt)) {
2225 error = PTR_ERR(shm_mnt);
2226 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2227 goto out1;
2228 }
2229 return 0;
2230
2231out1:
2232 unregister_filesystem(&tmpfs_fs_type);
2233out2:
2234 destroy_inodecache();
2235out3:
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002236 bdi_destroy(&shmem_backing_dev_info);
2237out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 shm_mnt = ERR_PTR(error);
2239 return error;
2240}
Matt Mackall853ac432009-01-06 14:40:20 -08002241
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002242#ifdef CONFIG_CGROUP_MEM_RES_CTLR
2243/**
2244 * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
2245 * @inode: the inode to be searched
2246 * @pgoff: the offset to be searched
2247 * @pagep: the pointer for the found page to be stored
2248 * @ent: the pointer for the found swap entry to be stored
2249 *
2250 * If a page is found, refcount of it is incremented. Callers should handle
2251 * these refcount.
2252 */
2253void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
2254 struct page **pagep, swp_entry_t *ent)
2255{
Hugh Dickins285b2c42011-08-03 16:21:20 -07002256 swp_entry_t entry = { .val = 0 };
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002257 struct page *page = NULL;
2258 struct shmem_inode_info *info = SHMEM_I(inode);
2259
2260 if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
2261 goto out;
2262
2263 spin_lock(&info->lock);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002264#ifdef CONFIG_SWAP
Hugh Dickins285b2c42011-08-03 16:21:20 -07002265 entry = shmem_get_swap(info, pgoff);
2266 if (entry.val)
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002267 page = find_get_page(&swapper_space, entry.val);
Hugh Dickins285b2c42011-08-03 16:21:20 -07002268 else
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002269#endif
2270 page = find_get_page(inode->i_mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002271 spin_unlock(&info->lock);
2272out:
2273 *pagep = page;
2274 *ent = entry;
2275}
2276#endif
2277
Matt Mackall853ac432009-01-06 14:40:20 -08002278#else /* !CONFIG_SHMEM */
2279
2280/*
2281 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2282 *
2283 * This is intended for small system where the benefits of the full
2284 * shmem code (swap-backed and resource-limited) are outweighed by
2285 * their complexity. On systems without swap this code should be
2286 * effectively equivalent, but much lighter weight.
2287 */
2288
2289#include <linux/ramfs.h>
2290
2291static struct file_system_type tmpfs_fs_type = {
2292 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002293 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08002294 .kill_sb = kill_litter_super,
2295};
2296
Kay Sievers2b2af542009-04-30 15:23:42 +02002297int __init init_tmpfs(void)
Matt Mackall853ac432009-01-06 14:40:20 -08002298{
2299 BUG_ON(register_filesystem(&tmpfs_fs_type) != 0);
2300
2301 shm_mnt = kern_mount(&tmpfs_fs_type);
2302 BUG_ON(IS_ERR(shm_mnt));
2303
2304 return 0;
2305}
2306
2307int shmem_unuse(swp_entry_t entry, struct page *page)
2308{
2309 return 0;
2310}
2311
Hugh Dickins3f96b792009-09-21 17:03:37 -07002312int shmem_lock(struct file *file, int lock, struct user_struct *user)
2313{
2314 return 0;
2315}
2316
Hugh Dickins94c1e622011-06-27 16:18:03 -07002317void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
2318{
2319 truncate_inode_pages_range(inode->i_mapping, start, end);
2320}
2321EXPORT_SYMBOL_GPL(shmem_truncate_range);
2322
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002323#ifdef CONFIG_CGROUP_MEM_RES_CTLR
2324/**
2325 * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
2326 * @inode: the inode to be searched
2327 * @pgoff: the offset to be searched
2328 * @pagep: the pointer for the found page to be stored
2329 * @ent: the pointer for the found swap entry to be stored
2330 *
2331 * If a page is found, refcount of it is incremented. Callers should handle
2332 * these refcount.
2333 */
2334void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
2335 struct page **pagep, swp_entry_t *ent)
2336{
2337 struct page *page = NULL;
2338
2339 if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
2340 goto out;
2341 page = find_get_page(inode->i_mapping, pgoff);
2342out:
2343 *pagep = page;
2344 *ent = (swp_entry_t){ .val = 0 };
2345}
2346#endif
2347
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002348#define shmem_vm_ops generic_file_vm_ops
2349#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002350#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002351#define shmem_acct_size(flags, size) 0
2352#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08002353
2354#endif /* CONFIG_SHMEM */
2355
2356/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Randy Dunlap46711812008-03-19 17:00:41 -07002358/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 * shmem_file_setup - get an unlinked file living in tmpfs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 * @name: name for dentry (to be seen in /proc/<pid>/maps
2361 * @size: size to be set for the file
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002362 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 */
Sergei Trofimovich168f5ac2009-06-16 15:33:02 -07002364struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
2366 int error;
2367 struct file *file;
2368 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04002369 struct path path;
2370 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 struct qstr this;
2372
2373 if (IS_ERR(shm_mnt))
2374 return (void *)shm_mnt;
2375
Hugh Dickins285b2c42011-08-03 16:21:20 -07002376 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return ERR_PTR(-EINVAL);
2378
2379 if (shmem_acct_size(flags, size))
2380 return ERR_PTR(-ENOMEM);
2381
2382 error = -ENOMEM;
2383 this.name = name;
2384 this.len = strlen(name);
2385 this.hash = 0; /* will go */
2386 root = shm_mnt->mnt_root;
Al Viro2c48b9c2009-08-09 00:52:35 +04002387 path.dentry = d_alloc(root, &this);
2388 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 goto put_memory;
Al Viro2c48b9c2009-08-09 00:52:35 +04002390 path.mnt = mntget(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 error = -ENOSPC;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002393 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (!inode)
Al Viro4b42af82009-08-05 18:25:56 +04002395 goto put_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Al Viro2c48b9c2009-08-09 00:52:35 +04002397 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 inode->i_size = size;
2399 inode->i_nlink = 0; /* It is unlinked */
Matt Mackall853ac432009-01-06 14:40:20 -08002400#ifndef CONFIG_MMU
2401 error = ramfs_nommu_expand_for_mapping(inode, size);
2402 if (error)
Al Viro4b42af82009-08-05 18:25:56 +04002403 goto put_dentry;
Matt Mackall853ac432009-01-06 14:40:20 -08002404#endif
Al Viro4b42af82009-08-05 18:25:56 +04002405
2406 error = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04002407 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04002408 &shmem_file_operations);
2409 if (!file)
2410 goto put_dentry;
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return file;
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414put_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04002415 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416put_memory:
2417 shmem_unacct_size(flags, size);
2418 return ERR_PTR(error);
2419}
Keith Packard395e0dd2008-06-20 00:08:06 -07002420EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Randy Dunlap46711812008-03-19 17:00:41 -07002422/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2425 */
2426int shmem_zero_setup(struct vm_area_struct *vma)
2427{
2428 struct file *file;
2429 loff_t size = vma->vm_end - vma->vm_start;
2430
2431 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2432 if (IS_ERR(file))
2433 return PTR_ERR(file);
2434
2435 if (vma->vm_file)
2436 fput(vma->vm_file);
2437 vma->vm_file = file;
2438 vma->vm_ops = &shmem_vm_ops;
Hugh Dickinsbee4c362011-03-22 16:33:43 -07002439 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 return 0;
2441}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002442
2443/**
2444 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2445 * @mapping: the page's address_space
2446 * @index: the page index
2447 * @gfp: the page allocator flags to use if allocating
2448 *
2449 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2450 * with any new page allocations done using the specified allocation flags.
2451 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2452 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2453 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2454 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07002455 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2456 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002457 */
2458struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2459 pgoff_t index, gfp_t gfp)
2460{
Hugh Dickins68da9f02011-07-25 17:12:34 -07002461#ifdef CONFIG_SHMEM
2462 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002463 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07002464 int error;
2465
2466 BUG_ON(mapping->a_ops != &shmem_aops);
2467 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2468 if (error)
2469 page = ERR_PTR(error);
2470 else
2471 unlock_page(page);
2472 return page;
2473#else
2474 /*
2475 * The tiny !SHMEM case uses ramfs without swap
2476 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002477 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002478#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002479}
2480EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);