blob: e101c211ed1fe89207577c80ff2263192a91c579 [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>
31#include <linux/swap.h>
32
33static struct vfsmount *shm_mnt;
34
35#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * This virtual memory filesystem is heavily based on the ramfs. It
38 * extends ramfs by the ability to use swap and honor resource limits
39 * which makes it a completely usable filesystem.
40 */
41
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070042#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070043#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010044#include <linux/posix_acl.h>
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070045#include <linux/generic_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/string.h>
48#include <linux/slab.h>
49#include <linux/backing-dev.h>
50#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070053#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070054#include <linux/percpu_counter.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070055#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/security.h>
57#include <linux/swapops.h>
58#include <linux/mempolicy.h>
59#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000060#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070061#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070062#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080063#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040064#include <linux/magic.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#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 Mortonb76db7352008-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 Mortonb76db7352008-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 Dobriyanf0f37e2f2009-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/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700204 * shmem_recalc_inode - recalculate the block usage 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;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700246 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700247 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700248 struct pagevec pvec;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700249 pgoff_t index;
250 swp_entry_t swap;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700251 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700253 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
254
255 pagevec_init(&pvec, 0);
256 index = start;
257 while (index <= end && pagevec_lookup(&pvec, mapping, index,
258 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
259 mem_cgroup_uncharge_start();
260 for (i = 0; i < pagevec_count(&pvec); i++) {
261 struct page *page = pvec.pages[i];
262
263 /* We rely upon deletion not changing page->index */
264 index = page->index;
265 if (index > end)
266 break;
267
268 if (!trylock_page(page))
269 continue;
270 WARN_ON(page->index != index);
271 if (PageWriteback(page)) {
272 unlock_page(page);
273 continue;
274 }
275 truncate_inode_page(mapping, page);
276 unlock_page(page);
277 }
278 pagevec_release(&pvec);
279 mem_cgroup_uncharge_end();
280 cond_resched();
281 index++;
282 }
283
284 if (partial) {
285 struct page *page = NULL;
286 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
287 if (page) {
288 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
289 set_page_dirty(page);
290 unlock_page(page);
291 page_cache_release(page);
292 }
293 }
294
295 index = start;
296 for ( ; ; ) {
297 cond_resched();
298 if (!pagevec_lookup(&pvec, mapping, index,
299 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
300 if (index == start)
301 break;
302 index = start;
303 continue;
304 }
305 if (index == start && pvec.pages[0]->index > end) {
306 pagevec_release(&pvec);
307 break;
308 }
309 mem_cgroup_uncharge_start();
310 for (i = 0; i < pagevec_count(&pvec); i++) {
311 struct page *page = pvec.pages[i];
312
313 /* We rely upon deletion not changing page->index */
314 index = page->index;
315 if (index > end)
316 break;
317
318 lock_page(page);
319 WARN_ON(page->index != index);
320 wait_on_page_writeback(page);
321 truncate_inode_page(mapping, page);
322 unlock_page(page);
323 }
324 pagevec_release(&pvec);
325 mem_cgroup_uncharge_end();
326 index++;
327 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700328
Hugh Dickins285b2c42011-08-03 16:21:20 -0700329 if (end > SHMEM_NR_DIRECT)
330 end = SHMEM_NR_DIRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 spin_lock(&info->lock);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700333 for (index = start; index < end; index++) {
334 swap = shmem_get_swap(info, index);
335 if (swap.val) {
336 free_swap_and_cache(swap);
337 shmem_put_swap(info, index, (swp_entry_t){0});
338 info->swapped--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 }
341
Hugh Dickins285b2c42011-08-03 16:21:20 -0700342 if (mapping->nrpages) {
343 spin_unlock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /*
Hugh Dickins285b2c42011-08-03 16:21:20 -0700345 * A page may have meanwhile sneaked in from swap.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700347 truncate_inode_pages_range(mapping, lstart, lend);
348 spin_lock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 shmem_recalc_inode(inode);
352 spin_unlock(&info->lock);
353
Hugh Dickins285b2c42011-08-03 16:21:20 -0700354 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700356EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Hugh Dickins94c1e622011-06-27 16:18:03 -0700358static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 int error;
362
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200363 error = inode_change_ok(inode, attr);
364 if (error)
365 return error;
366
Hugh Dickins94c1e622011-06-27 16:18:03 -0700367 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
368 loff_t oldsize = inode->i_size;
369 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000370
Hugh Dickins94c1e622011-06-27 16:18:03 -0700371 if (newsize != oldsize) {
372 i_size_write(inode, newsize);
373 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
374 }
375 if (newsize < oldsize) {
376 loff_t holebegin = round_up(newsize, PAGE_SIZE);
377 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
378 shmem_truncate_range(inode, newsize, (loff_t)-1);
379 /* unmap again to remove racily COWed private pages */
380 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200384 setattr_copy(inode, attr);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700385#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200386 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100387 error = generic_acl_chmod(inode);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700388#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return error;
390}
391
Al Viro1f895f72010-06-05 19:10:41 -0400392static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct shmem_inode_info *info = SHMEM_I(inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700395 struct shmem_xattr *xattr, *nxattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000397 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 shmem_unacct_size(info->flags, inode->i_size);
399 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000400 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800402 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800404 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406 }
Eric Parisb09e0fa2011-05-24 17:12:39 -0700407
408 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
409 kfree(xattr->name);
410 kfree(xattr);
411 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700412 BUG_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800413 shmem_free_inode(inode->i_sb);
Al Viro1f895f72010-06-05 19:10:41 -0400414 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700417static int shmem_unuse_inode(struct shmem_inode_info *info,
418 swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700420 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700421 pgoff_t index;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800422 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700424 for (index = 0; index < SHMEM_NR_DIRECT; index++)
425 if (shmem_get_swap(info, index).val == swap.val)
Hugh Dickins285b2c42011-08-03 16:21:20 -0700426 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 0;
428found:
Hugh Dickins285b2c42011-08-03 16:21:20 -0700429 spin_lock(&info->lock);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700430 if (shmem_get_swap(info, index).val != swap.val) {
Hugh Dickins285b2c42011-08-03 16:21:20 -0700431 spin_unlock(&info->lock);
432 return 0;
433 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800434
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800435 /*
436 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400437 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800438 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700439 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800440 */
441 if (shmem_swaplist.next != &info->swaplist)
442 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800443
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800444 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700445 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
446 * but also to hold up shmem_evict_inode(): so inode cannot be freed
447 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800448 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700449 error = add_to_page_cache_locked(page, mapping, index, GFP_NOWAIT);
Hugh Dickins778dd892011-05-11 15:13:37 -0700450 /* which does mem_cgroup_uncharge_cache_page on error */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700451
Hugh Dickins48f170f2011-07-25 17:12:37 -0700452 if (error != -ENOMEM) {
Hugh Dickins73b12622008-02-04 22:28:50 -0800453 delete_from_swap_cache(page);
454 set_page_dirty(page);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700455 shmem_put_swap(info, index, (swp_entry_t){0});
Hugh Dickins285b2c42011-08-03 16:21:20 -0700456 info->swapped--;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700457 swap_free(swap);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800458 error = 1; /* not an error, but entry was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 spin_unlock(&info->lock);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800461 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464/*
465 * shmem_unuse() search for an eventually swapped out shmem page.
466 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700467int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700469 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct shmem_inode_info *info;
471 int found = 0;
Hugh Dickins778dd892011-05-11 15:13:37 -0700472 int error;
473
474 /*
475 * Charge page using GFP_KERNEL while we can wait, before taking
476 * the shmem_swaplist_mutex which might hold up shmem_writepage().
477 * Charged back to the user (not to caller) when swap account is used.
478 * add_to_page_cache() will be called with GFP_NOWAIT.
479 */
480 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
481 if (error)
482 goto out;
483 /*
484 * Try to preload while we can wait, to not make a habit of
485 * draining atomic reserves; but don't latch on to this cpu,
486 * it's okay if sometimes we get rescheduled after this.
487 */
488 error = radix_tree_preload(GFP_KERNEL);
489 if (error)
490 goto uncharge;
491 radix_tree_preload_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800493 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700494 list_for_each_safe(this, next, &shmem_swaplist) {
495 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700496 if (!info->swapped) {
497 spin_lock(&info->lock);
498 if (!info->swapped)
499 list_del_init(&info->swaplist);
500 spin_unlock(&info->lock);
501 }
502 if (info->swapped)
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700503 found = shmem_unuse_inode(info, swap, page);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800504 cond_resched();
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800505 if (found)
Hugh Dickins778dd892011-05-11 15:13:37 -0700506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800508 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -0700509
510uncharge:
511 if (!found)
512 mem_cgroup_uncharge_cache_page(page);
513 if (found < 0)
514 error = found;
515out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800516 unlock_page(page);
517 page_cache_release(page);
Hugh Dickins778dd892011-05-11 15:13:37 -0700518 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521/*
522 * Move the page from the page cache to the swap cache.
523 */
524static int shmem_writepage(struct page *page, struct writeback_control *wbc)
525{
526 struct shmem_inode_info *info;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700527 swp_entry_t swap, oswap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct address_space *mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700529 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct inode *inode;
531
532 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 mapping = page->mapping;
534 index = page->index;
535 inode = mapping->host;
536 info = SHMEM_I(inode);
537 if (info->flags & VM_LOCKED)
538 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800539 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto redirty;
541
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800542 /*
543 * shmem_backing_dev_info's capabilities prevent regular writeback or
544 * sync from ever calling shmem_writepage; but a stacking filesystem
Hugh Dickins48f170f2011-07-25 17:12:37 -0700545 * might use ->writepage of its underlying filesystem, in which case
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800546 * tmpfs should write out to swap only in response to memory pressure,
Hugh Dickins48f170f2011-07-25 17:12:37 -0700547 * and not for the writeback threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800548 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700549 if (!wbc->for_reclaim) {
550 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
551 goto redirty;
552 }
Hugh Dickins285b2c42011-08-03 16:21:20 -0700553
554 /*
555 * Just for this patch, we have a toy implementation,
556 * which can swap out only the first SHMEM_NR_DIRECT pages:
557 * for simple demonstration of where we need to think about swap.
558 */
559 if (index >= SHMEM_NR_DIRECT)
560 goto redirty;
561
Hugh Dickins48f170f2011-07-25 17:12:37 -0700562 swap = get_swap_page();
563 if (!swap.val)
564 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800565
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700566 /*
567 * Add inode to shmem_unuse()'s list of swapped-out inodes,
568 * if it's not already there. Do it now because we cannot take
569 * mutex while holding spinlock, and must do so before the page
570 * is moved to swap cache, when its pagelock no longer protects
571 * the inode from eviction. But don't unlock the mutex until
572 * we've taken the spinlock, because shmem_unuse_inode() will
573 * prune a !swapped inode from the swaplist under both locks.
574 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700575 mutex_lock(&shmem_swaplist_mutex);
576 if (list_empty(&info->swaplist))
577 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 spin_lock(&info->lock);
Hugh Dickins48f170f2011-07-25 17:12:37 -0700580 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700581
Hugh Dickins285b2c42011-08-03 16:21:20 -0700582 oswap = shmem_get_swap(info, index);
583 if (oswap.val) {
Hugh Dickins48f170f2011-07-25 17:12:37 -0700584 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700585 free_swap_and_cache(oswap);
586 shmem_put_swap(info, index, (swp_entry_t){0});
587 info->swapped--;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800588 }
589 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Hugh Dickins48f170f2011-07-25 17:12:37 -0700591 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Minchan Kim4c73b1b2011-03-22 16:32:40 -0700592 delete_from_page_cache(page);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700593 shmem_put_swap(info, index, swap);
594 info->swapped++;
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800595 swap_shmem_alloc(swap);
Hugh Dickins826267c2011-05-28 13:14:09 -0700596 spin_unlock(&info->lock);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800597 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -0700598 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return 0;
600 }
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 spin_unlock(&info->lock);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700603 swapcache_free(swap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604redirty:
605 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800606 if (wbc->for_reclaim)
607 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
608 unlock_page(page);
609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612#ifdef CONFIG_NUMA
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800613#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700614static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800615{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700616 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800617
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700618 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700619 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800620
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700621 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800622
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700623 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800624}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700625
626static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
627{
628 struct mempolicy *mpol = NULL;
629 if (sbinfo->mpol) {
630 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
631 mpol = sbinfo->mpol;
632 mpol_get(mpol);
633 spin_unlock(&sbinfo->stat_lock);
634 }
635 return mpol;
636}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800637#endif /* CONFIG_TMPFS */
638
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700639static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
640 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700642 struct mempolicy mpol, *spol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct vm_area_struct pvma;
644
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700645 spol = mpol_cond_copy(&mpol,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700646 mpol_shared_policy_lookup(&info->policy, index));
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* Create a pseudo vma that just contains the policy */
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800649 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700650 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800651 pvma.vm_ops = NULL;
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700652 pvma.vm_policy = spol;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700653 return swapin_readahead(swap, gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Hugh Dickins02098fe2008-02-04 22:28:42 -0800656static struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700657 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct vm_area_struct pvma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800661 /* Create a pseudo vma that just contains the policy */
662 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700663 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800664 pvma.vm_ops = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700665 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700666
667 /*
668 * alloc_page_vma() will drop the shared policy reference
669 */
670 return alloc_page_vma(gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800672#else /* !CONFIG_NUMA */
673#ifdef CONFIG_TMPFS
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700674static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800675{
676}
677#endif /* CONFIG_TMPFS */
678
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700679static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
680 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700682 return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Hugh Dickins02098fe2008-02-04 22:28:42 -0800685static inline struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700686 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Hugh Dickinse84e2e12007-11-28 18:55:10 +0000688 return alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800690#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700692#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
693static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
694{
695 return NULL;
696}
697#endif
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/*
Hugh Dickins68da9f02011-07-25 17:12:34 -0700700 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 *
702 * If we allocate a new one we do not mark it dirty. That's up to the
703 * vm. If we swap it in we mark it dirty since we also free the swap
704 * entry since a page cannot live in both the swap and page cache
705 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700706static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Hugh Dickins68da9f02011-07-25 17:12:34 -0700707 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 struct address_space *mapping = inode->i_mapping;
710 struct shmem_inode_info *info = SHMEM_I(inode);
711 struct shmem_sb_info *sbinfo;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700712 struct page *page;
Shaohua Liff36b8012010-08-09 17:19:06 -0700713 struct page *prealloc_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 swp_entry_t swap;
715 int error;
716
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700717 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719repeat:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700720 page = find_lock_page(mapping, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700721 if (page) {
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800722 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700723 * Once we can get the page lock, it must be uptodate:
724 * if there were an error in reading back from swap,
725 * the page would not be inserted into the filecache.
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800726 */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700727 BUG_ON(!PageUptodate(page));
728 goto done;
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700732 * Try to preload while we can wait, to not make a habit of
733 * draining atomic reserves; but don't latch on to this cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700735 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
736 if (error)
737 goto out;
738 radix_tree_preload_end();
739
740 if (sgp != SGP_READ && !prealloc_page) {
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700741 prealloc_page = shmem_alloc_page(gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700742 if (prealloc_page) {
743 SetPageSwapBacked(prealloc_page);
744 if (mem_cgroup_cache_charge(prealloc_page,
745 current->mm, GFP_KERNEL)) {
746 page_cache_release(prealloc_page);
747 prealloc_page = NULL;
Shaohua Liff36b8012010-08-09 17:19:06 -0700748 }
749 }
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 spin_lock(&info->lock);
753 shmem_recalc_inode(inode);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700754 swap = shmem_get_swap(info, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (swap.val) {
756 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700757 page = lookup_swap_cache(swap);
758 if (!page) {
Christoph Lameterf8891e52006-06-30 01:55:45 -0700759 spin_unlock(&info->lock);
Ying Han456f9982011-05-26 16:25:38 -0700760 /* here we actually do the io */
Hugh Dickins68da9f02011-07-25 17:12:34 -0700761 if (fault_type)
762 *fault_type |= VM_FAULT_MAJOR;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700763 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700764 if (!page) {
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700765 swp_entry_t nswap = shmem_get_swap(info, index);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700766 if (nswap.val == swap.val) {
767 error = -ENOMEM;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700768 goto out;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 goto repeat;
771 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700772 wait_on_page_locked(page);
773 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 goto repeat;
775 }
776
777 /* We have to do this with page locked to prevent races */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700778 if (!trylock_page(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700780 wait_on_page_locked(page);
781 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 goto repeat;
783 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700784 if (PageWriteback(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700786 wait_on_page_writeback(page);
787 unlock_page(page);
788 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 goto repeat;
790 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700791 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700793 unlock_page(page);
794 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 error = -EIO;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700796 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
Hugh Dickins27ab7002011-07-25 17:12:36 -0700799 error = add_to_page_cache_locked(page, mapping,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700800 index, GFP_NOWAIT);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700801 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 spin_unlock(&info->lock);
Hugh Dickins82369552008-02-07 00:14:22 -0800803 if (error == -ENOMEM) {
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -0700804 /*
805 * reclaim from proper memory cgroup and
806 * call memcg's OOM if needed.
807 */
808 error = mem_cgroup_shmem_charge_fallback(
Hugh Dickins27ab7002011-07-25 17:12:36 -0700809 page, current->mm, gfp);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -0800810 if (error) {
Hugh Dickins27ab7002011-07-25 17:12:36 -0700811 unlock_page(page);
812 page_cache_release(page);
813 goto out;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -0800814 }
Hugh Dickins82369552008-02-07 00:14:22 -0800815 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700816 unlock_page(page);
817 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto repeat;
819 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700820
Hugh Dickins27ab7002011-07-25 17:12:36 -0700821 delete_from_swap_cache(page);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700822 shmem_put_swap(info, index, (swp_entry_t){0});
Hugh Dickins285b2c42011-08-03 16:21:20 -0700823 info->swapped--;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700824 spin_unlock(&info->lock);
825 set_page_dirty(page);
826 swap_free(swap);
827
828 } else if (sgp == SGP_READ) {
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700829 page = find_get_page(mapping, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700830 if (page && !trylock_page(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700832 wait_on_page_locked(page);
833 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 goto repeat;
835 }
836 spin_unlock(&info->lock);
Hugh Dickinse83c32e2011-07-25 17:12:35 -0700837
838 } else if (prealloc_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700840 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -0700841 if (percpu_counter_compare(&sbinfo->used_blocks,
842 sbinfo->max_blocks) >= 0 ||
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700843 shmem_acct_block(info->flags))
844 goto nospace;
Tim Chen7e496292010-08-09 17:19:05 -0700845 percpu_counter_inc(&sbinfo->used_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 inode->i_blocks += BLOCKS_PER_PAGE;
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700847 } else if (shmem_acct_block(info->flags))
848 goto nospace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Hugh Dickins27ab7002011-07-25 17:12:36 -0700850 page = prealloc_page;
851 prealloc_page = NULL;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700852
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700853 swap = shmem_get_swap(info, index);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700854 if (swap.val)
Hugh Dickins27ab7002011-07-25 17:12:36 -0700855 mem_cgroup_uncharge_cache_page(page);
856 else
Hugh Dickins285b2c42011-08-03 16:21:20 -0700857 error = add_to_page_cache_lru(page, mapping,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700858 index, GFP_NOWAIT);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700859 /*
860 * At add_to_page_cache_lru() failure,
861 * uncharge will be done automatically.
862 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700863 if (swap.val || error) {
Hugh Dickins27ab7002011-07-25 17:12:36 -0700864 shmem_unacct_blocks(info->flags, 1);
865 shmem_free_blocks(inode, 1);
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700866 spin_unlock(&info->lock);
867 page_cache_release(page);
868 goto repeat;
869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 info->alloced++;
872 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700873 clear_highpage(page);
874 flush_dcache_page(page);
875 SetPageUptodate(page);
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -0800876 if (sgp == SGP_DIRTY)
Hugh Dickins27ab7002011-07-25 17:12:36 -0700877 set_page_dirty(page);
878
Hugh Dickinse83c32e2011-07-25 17:12:35 -0700879 } else {
880 spin_unlock(&info->lock);
881 error = -ENOMEM;
882 goto out;
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884done:
Hugh Dickins27ab7002011-07-25 17:12:36 -0700885 *pagep = page;
Shaohua Liff36b8012010-08-09 17:19:06 -0700886 error = 0;
Shaohua Liff36b8012010-08-09 17:19:06 -0700887out:
888 if (prealloc_page) {
889 mem_cgroup_uncharge_cache_page(prealloc_page);
890 page_cache_release(prealloc_page);
891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return error;
Nick Piggind00806b2007-07-19 01:46:57 -0700893
Hugh Dickins27d54b32008-02-04 22:28:43 -0800894nospace:
Nick Piggind0217ac2007-07-19 01:47:03 -0700895 /*
896 * Perhaps the page was brought in from swap between find_lock_page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * and taking info->lock? We allow for that at add_to_page_cache_lru,
Nick Piggin83c54072007-07-19 01:47:05 -0700898 * but must also avoid reporting a spurious ENOSPC while working on a
Hugh Dickins9276aad2011-07-25 17:12:34 -0700899 * full tmpfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700901 page = find_get_page(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700903 if (page) {
904 page_cache_release(page);
905 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -0800906 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700907 error = -ENOSPC;
Hugh Dickinse83c32e2011-07-25 17:12:35 -0700908 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
912{
913 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
914 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -0700915 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
918 return VM_FAULT_SIGBUS;
919
920 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
921 if (error)
922 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700923
Ying Han456f9982011-05-26 16:25:38 -0700924 if (ret & VM_FAULT_MAJOR) {
925 count_vm_event(PGMAJFAULT);
926 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
927 }
Hugh Dickins68da9f02011-07-25 17:12:34 -0700928 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
931#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700932static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700934 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
935 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Adrian Bunkd8dc74f2007-10-16 01:26:26 -0700938static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
939 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700941 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
942 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700944 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
945 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947#endif
948
949int shmem_lock(struct file *file, int lock, struct user_struct *user)
950{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -0800951 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct shmem_inode_info *info = SHMEM_I(inode);
953 int retval = -ENOMEM;
954
955 spin_lock(&info->lock);
956 if (lock && !(info->flags & VM_LOCKED)) {
957 if (!user_shm_lock(inode->i_size, user))
958 goto out_nomem;
959 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -0700960 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962 if (!lock && (info->flags & VM_LOCKED) && user) {
963 user_shm_unlock(inode->i_size, user);
964 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -0700965 mapping_clear_unevictable(file->f_mapping);
966 scan_mapping_unevictable_pages(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -0700969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970out_nomem:
971 spin_unlock(&info->lock);
972 return retval;
973}
974
Adrian Bunk9b83a6a2007-02-28 20:11:03 -0800975static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 file_accessed(file);
978 vma->vm_ops = &shmem_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -0700979 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return 0;
981}
982
Dmitry Monakhov454abaf2010-03-04 17:32:18 +0300983static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
984 int mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct inode *inode;
987 struct shmem_inode_info *info;
988 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
989
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800990 if (shmem_reserve_inode(sb))
991 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 inode = new_inode(sb);
994 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400995 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +0300996 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
999 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001000 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 info = SHMEM_I(inode);
1002 memset(info, 0, (char *)inode - (char *)info);
1003 spin_lock_init(&info->lock);
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001004 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 INIT_LIST_HEAD(&info->swaplist);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001006 INIT_LIST_HEAD(&info->xattr_list);
Al Viro72c04902009-06-24 16:58:48 -04001007 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 switch (mode & S_IFMT) {
1010 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001011 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 init_special_inode(inode, mode, dev);
1013 break;
1014 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001015 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 inode->i_op = &shmem_inode_operations;
1017 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001018 mpol_shared_policy_init(&info->policy,
1019 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 break;
1021 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001022 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /* Some things misbehave if size == 0 on a directory */
1024 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1025 inode->i_op = &shmem_dir_inode_operations;
1026 inode->i_fop = &simple_dir_operations;
1027 break;
1028 case S_IFLNK:
1029 /*
1030 * Must not load anything in the rbtree,
1031 * mpol_free_shared_policy will not be called.
1032 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001033 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 break;
1035 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001036 } else
1037 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return inode;
1039}
1040
1041#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001042static const struct inode_operations shmem_symlink_inode_operations;
1043static const struct inode_operations shmem_symlink_inline_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001046shmem_write_begin(struct file *file, struct address_space *mapping,
1047 loff_t pos, unsigned len, unsigned flags,
1048 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Nick Piggin800d15a2007-10-16 01:25:03 -07001050 struct inode *inode = mapping->host;
1051 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin800d15a2007-10-16 01:25:03 -07001052 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1053}
1054
1055static int
1056shmem_write_end(struct file *file, struct address_space *mapping,
1057 loff_t pos, unsigned len, unsigned copied,
1058 struct page *page, void *fsdata)
1059{
1060 struct inode *inode = mapping->host;
1061
Hugh Dickinsd3602442008-02-04 22:28:44 -08001062 if (pos + copied > inode->i_size)
1063 i_size_write(inode, pos + copied);
1064
Nick Piggin800d15a2007-10-16 01:25:03 -07001065 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001066 unlock_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07001067 page_cache_release(page);
1068
Nick Piggin800d15a2007-10-16 01:25:03 -07001069 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1073{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001074 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001076 pgoff_t index;
1077 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001078 enum sgp_type sgp = SGP_READ;
1079
1080 /*
1081 * Might this read be for a stacking filesystem? Then when reading
1082 * holes of a sparse file, we actually need to allocate those pages,
1083 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1084 */
1085 if (segment_eq(get_fs(), KERNEL_DS))
1086 sgp = SGP_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 index = *ppos >> PAGE_CACHE_SHIFT;
1089 offset = *ppos & ~PAGE_CACHE_MASK;
1090
1091 for (;;) {
1092 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001093 pgoff_t end_index;
1094 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 loff_t i_size = i_size_read(inode);
1096
1097 end_index = i_size >> PAGE_CACHE_SHIFT;
1098 if (index > end_index)
1099 break;
1100 if (index == end_index) {
1101 nr = i_size & ~PAGE_CACHE_MASK;
1102 if (nr <= offset)
1103 break;
1104 }
1105
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001106 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (desc->error) {
1108 if (desc->error == -EINVAL)
1109 desc->error = 0;
1110 break;
1111 }
Hugh Dickinsd3602442008-02-04 22:28:44 -08001112 if (page)
1113 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /*
1116 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001117 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
1119 nr = PAGE_CACHE_SIZE;
1120 i_size = i_size_read(inode);
1121 end_index = i_size >> PAGE_CACHE_SHIFT;
1122 if (index == end_index) {
1123 nr = i_size & ~PAGE_CACHE_MASK;
1124 if (nr <= offset) {
1125 if (page)
1126 page_cache_release(page);
1127 break;
1128 }
1129 }
1130 nr -= offset;
1131
1132 if (page) {
1133 /*
1134 * If users can be writing to this page using arbitrary
1135 * virtual addresses, take care about potential aliasing
1136 * before reading the page on the kernel side.
1137 */
1138 if (mapping_writably_mapped(mapping))
1139 flush_dcache_page(page);
1140 /*
1141 * Mark the page accessed if we read the beginning.
1142 */
1143 if (!offset)
1144 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07001145 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 page = ZERO_PAGE(0);
Nick Pigginb5810032005-10-29 18:16:12 -07001147 page_cache_get(page);
1148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 /*
1151 * Ok, we have the page, and it's up-to-date, so
1152 * now we can copy it to user space...
1153 *
1154 * The actor routine returns how many bytes were actually used..
1155 * NOTE! This may not be the same as how much of a user buffer
1156 * we filled up (we may be padding etc), so we can only update
1157 * "pos" here (the actor routine has to update the user buffer
1158 * pointers and the remaining count).
1159 */
1160 ret = actor(desc, page, offset, nr);
1161 offset += ret;
1162 index += offset >> PAGE_CACHE_SHIFT;
1163 offset &= ~PAGE_CACHE_MASK;
1164
1165 page_cache_release(page);
1166 if (ret != nr || !desc->count)
1167 break;
1168
1169 cond_resched();
1170 }
1171
1172 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1173 file_accessed(filp);
1174}
1175
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001176static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1177 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001179 struct file *filp = iocb->ki_filp;
1180 ssize_t retval;
1181 unsigned long seg;
1182 size_t count;
1183 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001185 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1186 if (retval)
1187 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001189 for (seg = 0; seg < nr_segs; seg++) {
1190 read_descriptor_t desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001192 desc.written = 0;
1193 desc.arg.buf = iov[seg].iov_base;
1194 desc.count = iov[seg].iov_len;
1195 if (desc.count == 0)
1196 continue;
1197 desc.error = 0;
1198 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1199 retval += desc.written;
1200 if (desc.error) {
1201 retval = retval ?: desc.error;
1202 break;
1203 }
1204 if (desc.count > 0)
1205 break;
1206 }
1207 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Hugh Dickins708e3502011-07-25 17:12:32 -07001210static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1211 struct pipe_inode_info *pipe, size_t len,
1212 unsigned int flags)
1213{
1214 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001215 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07001216 unsigned int loff, nr_pages, req_pages;
1217 struct page *pages[PIPE_DEF_BUFFERS];
1218 struct partial_page partial[PIPE_DEF_BUFFERS];
1219 struct page *page;
1220 pgoff_t index, end_index;
1221 loff_t isize, left;
1222 int error, page_nr;
1223 struct splice_pipe_desc spd = {
1224 .pages = pages,
1225 .partial = partial,
1226 .flags = flags,
1227 .ops = &page_cache_pipe_buf_ops,
1228 .spd_release = spd_release_page,
1229 };
1230
Hugh Dickins71f0e072011-07-25 17:12:33 -07001231 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001232 if (unlikely(*ppos >= isize))
1233 return 0;
1234
1235 left = isize - *ppos;
1236 if (unlikely(left < len))
1237 len = left;
1238
1239 if (splice_grow_spd(pipe, &spd))
1240 return -ENOMEM;
1241
1242 index = *ppos >> PAGE_CACHE_SHIFT;
1243 loff = *ppos & ~PAGE_CACHE_MASK;
1244 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1245 nr_pages = min(req_pages, pipe->buffers);
1246
Hugh Dickins708e3502011-07-25 17:12:32 -07001247 spd.nr_pages = find_get_pages_contig(mapping, index,
1248 nr_pages, spd.pages);
1249 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07001250 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001251
Hugh Dickins708e3502011-07-25 17:12:32 -07001252 while (spd.nr_pages < nr_pages) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001253 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1254 if (error)
1255 break;
1256 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07001257 spd.pages[spd.nr_pages++] = page;
1258 index++;
1259 }
1260
Hugh Dickins708e3502011-07-25 17:12:32 -07001261 index = *ppos >> PAGE_CACHE_SHIFT;
1262 nr_pages = spd.nr_pages;
1263 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001264
Hugh Dickins708e3502011-07-25 17:12:32 -07001265 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1266 unsigned int this_len;
1267
1268 if (!len)
1269 break;
1270
Hugh Dickins708e3502011-07-25 17:12:32 -07001271 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1272 page = spd.pages[page_nr];
1273
Hugh Dickins71f0e072011-07-25 17:12:33 -07001274 if (!PageUptodate(page) || page->mapping != mapping) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001275 error = shmem_getpage(inode, index, &page,
1276 SGP_CACHE, NULL);
1277 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07001278 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001279 unlock_page(page);
1280 page_cache_release(spd.pages[page_nr]);
1281 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07001282 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07001283
1284 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001285 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1286 if (unlikely(!isize || index > end_index))
1287 break;
1288
Hugh Dickins708e3502011-07-25 17:12:32 -07001289 if (end_index == index) {
1290 unsigned int plen;
1291
Hugh Dickins708e3502011-07-25 17:12:32 -07001292 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1293 if (plen <= loff)
1294 break;
1295
Hugh Dickins708e3502011-07-25 17:12:32 -07001296 this_len = min(this_len, plen - loff);
1297 len = this_len;
1298 }
1299
1300 spd.partial[page_nr].offset = loff;
1301 spd.partial[page_nr].len = this_len;
1302 len -= this_len;
1303 loff = 0;
1304 spd.nr_pages++;
1305 index++;
1306 }
1307
Hugh Dickins708e3502011-07-25 17:12:32 -07001308 while (page_nr < nr_pages)
1309 page_cache_release(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07001310
1311 if (spd.nr_pages)
1312 error = splice_to_pipe(pipe, &spd);
1313
1314 splice_shrink_spd(pipe, &spd);
1315
1316 if (error > 0) {
1317 *ppos += error;
1318 file_accessed(in);
1319 }
1320 return error;
1321}
1322
David Howells726c3342006-06-23 02:02:58 -07001323static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
David Howells726c3342006-06-23 02:02:58 -07001325 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 buf->f_type = TMPFS_MAGIC;
1328 buf->f_bsize = PAGE_CACHE_SIZE;
1329 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001330 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001332 buf->f_bavail =
1333 buf->f_bfree = sbinfo->max_blocks -
1334 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001335 }
1336 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 buf->f_files = sbinfo->max_inodes;
1338 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340 /* else leave those fields 0 like simple_statfs */
1341 return 0;
1342}
1343
1344/*
1345 * File creation. Allocate an inode, and we're done..
1346 */
1347static int
1348shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1349{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001350 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int error = -ENOSPC;
1352
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001353 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (inode) {
Eric Paris2a7dba32011-02-01 11:05:39 -05001355 error = security_inode_init_security(inode, dir,
1356 &dentry->d_name, NULL,
1357 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001358 if (error) {
1359 if (error != -EOPNOTSUPP) {
1360 iput(inode);
1361 return error;
1362 }
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001363 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001364#ifdef CONFIG_TMPFS_POSIX_ACL
1365 error = generic_acl_init(inode, dir);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001366 if (error) {
1367 iput(inode);
1368 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001369 }
Al Viro718deb62009-12-16 19:35:36 -05001370#else
1371 error = 0;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001372#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 dir->i_size += BOGO_DIRENT_SIZE;
1374 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1375 d_instantiate(dentry, inode);
1376 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378 return error;
1379}
1380
1381static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1382{
1383 int error;
1384
1385 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1386 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07001387 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return 0;
1389}
1390
1391static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1392 struct nameidata *nd)
1393{
1394 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1395}
1396
1397/*
1398 * Link a file..
1399 */
1400static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1401{
1402 struct inode *inode = old_dentry->d_inode;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001403 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 /*
1406 * No ordinary (disk based) filesystem counts links as inodes;
1407 * but each new link needs a new dentry, pinning lowmem, and
1408 * tmpfs dentries cannot be pruned until they are unlinked.
1409 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001410 ret = shmem_reserve_inode(inode->i_sb);
1411 if (ret)
1412 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 dir->i_size += BOGO_DIRENT_SIZE;
1415 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07001416 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04001417 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 dget(dentry); /* Extra pinning count for the created dentry */
1419 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001420out:
1421 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1425{
1426 struct inode *inode = dentry->d_inode;
1427
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001428 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1429 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 dir->i_size -= BOGO_DIRENT_SIZE;
1432 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001433 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 dput(dentry); /* Undo the count from "create" - this does all the work */
1435 return 0;
1436}
1437
1438static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1439{
1440 if (!simple_empty(dentry))
1441 return -ENOTEMPTY;
1442
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001443 drop_nlink(dentry->d_inode);
1444 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return shmem_unlink(dir, dentry);
1446}
1447
1448/*
1449 * The VFS layer already does all the dentry stuff for rename,
1450 * we just have to decrement the usage count for the target if
1451 * it exists so that the VFS layer correctly free's it when it
1452 * gets overwritten.
1453 */
1454static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1455{
1456 struct inode *inode = old_dentry->d_inode;
1457 int they_are_dirs = S_ISDIR(inode->i_mode);
1458
1459 if (!simple_empty(new_dentry))
1460 return -ENOTEMPTY;
1461
1462 if (new_dentry->d_inode) {
1463 (void) shmem_unlink(new_dir, new_dentry);
1464 if (they_are_dirs)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001465 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001467 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07001468 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470
1471 old_dir->i_size -= BOGO_DIRENT_SIZE;
1472 new_dir->i_size += BOGO_DIRENT_SIZE;
1473 old_dir->i_ctime = old_dir->i_mtime =
1474 new_dir->i_ctime = new_dir->i_mtime =
1475 inode->i_ctime = CURRENT_TIME;
1476 return 0;
1477}
1478
1479static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1480{
1481 int error;
1482 int len;
1483 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07001484 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 char *kaddr;
1486 struct shmem_inode_info *info;
1487
1488 len = strlen(symname) + 1;
1489 if (len > PAGE_CACHE_SIZE)
1490 return -ENAMETOOLONG;
1491
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001492 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (!inode)
1494 return -ENOSPC;
1495
Eric Paris2a7dba32011-02-01 11:05:39 -05001496 error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
1497 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001498 if (error) {
1499 if (error != -EOPNOTSUPP) {
1500 iput(inode);
1501 return error;
1502 }
1503 error = 0;
1504 }
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 info = SHMEM_I(inode);
1507 inode->i_size = len-1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07001508 if (len <= SHMEM_SYMLINK_INLINE_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 /* do it inline */
Eric Parisb09e0fa2011-05-24 17:12:39 -07001510 memcpy(info->inline_symlink, symname, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 inode->i_op = &shmem_symlink_inline_operations;
1512 } else {
1513 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1514 if (error) {
1515 iput(inode);
1516 return error;
1517 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07001518 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 inode->i_op = &shmem_symlink_inode_operations;
1520 kaddr = kmap_atomic(page, KM_USER0);
1521 memcpy(kaddr, symname, len);
1522 kunmap_atomic(kaddr, KM_USER0);
1523 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001524 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 page_cache_release(page);
1526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 dir->i_size += BOGO_DIRENT_SIZE;
1528 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1529 d_instantiate(dentry, inode);
1530 dget(dentry);
1531 return 0;
1532}
1533
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001534static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Eric Parisb09e0fa2011-05-24 17:12:39 -07001536 nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001537 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001540static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001543 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1544 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
Hugh Dickinsd3602442008-02-04 22:28:44 -08001545 if (page)
1546 unlock_page(page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001547 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001550static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 if (!IS_ERR(nd_get_link(nd))) {
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001553 struct page *page = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 kunmap(page);
1555 mark_page_accessed(page);
1556 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558}
1559
Eric Parisb09e0fa2011-05-24 17:12:39 -07001560#ifdef CONFIG_TMPFS_XATTR
1561/*
1562 * Superblocks without xattr inode operations may get some security.* xattr
1563 * support from the LSM "for free". As soon as we have any other xattrs
1564 * like ACLs, we also need to implement the security.* handlers at
1565 * filesystem level, though.
1566 */
1567
1568static int shmem_xattr_get(struct dentry *dentry, const char *name,
1569 void *buffer, size_t size)
1570{
1571 struct shmem_inode_info *info;
1572 struct shmem_xattr *xattr;
1573 int ret = -ENODATA;
1574
1575 info = SHMEM_I(dentry->d_inode);
1576
1577 spin_lock(&info->lock);
1578 list_for_each_entry(xattr, &info->xattr_list, list) {
1579 if (strcmp(name, xattr->name))
1580 continue;
1581
1582 ret = xattr->size;
1583 if (buffer) {
1584 if (size < xattr->size)
1585 ret = -ERANGE;
1586 else
1587 memcpy(buffer, xattr->value, xattr->size);
1588 }
1589 break;
1590 }
1591 spin_unlock(&info->lock);
1592 return ret;
1593}
1594
1595static int shmem_xattr_set(struct dentry *dentry, const char *name,
1596 const void *value, size_t size, int flags)
1597{
1598 struct inode *inode = dentry->d_inode;
1599 struct shmem_inode_info *info = SHMEM_I(inode);
1600 struct shmem_xattr *xattr;
1601 struct shmem_xattr *new_xattr = NULL;
1602 size_t len;
1603 int err = 0;
1604
1605 /* value == NULL means remove */
1606 if (value) {
1607 /* wrap around? */
1608 len = sizeof(*new_xattr) + size;
1609 if (len <= sizeof(*new_xattr))
1610 return -ENOMEM;
1611
1612 new_xattr = kmalloc(len, GFP_KERNEL);
1613 if (!new_xattr)
1614 return -ENOMEM;
1615
1616 new_xattr->name = kstrdup(name, GFP_KERNEL);
1617 if (!new_xattr->name) {
1618 kfree(new_xattr);
1619 return -ENOMEM;
1620 }
1621
1622 new_xattr->size = size;
1623 memcpy(new_xattr->value, value, size);
1624 }
1625
1626 spin_lock(&info->lock);
1627 list_for_each_entry(xattr, &info->xattr_list, list) {
1628 if (!strcmp(name, xattr->name)) {
1629 if (flags & XATTR_CREATE) {
1630 xattr = new_xattr;
1631 err = -EEXIST;
1632 } else if (new_xattr) {
1633 list_replace(&xattr->list, &new_xattr->list);
1634 } else {
1635 list_del(&xattr->list);
1636 }
1637 goto out;
1638 }
1639 }
1640 if (flags & XATTR_REPLACE) {
1641 xattr = new_xattr;
1642 err = -ENODATA;
1643 } else {
1644 list_add(&new_xattr->list, &info->xattr_list);
1645 xattr = NULL;
1646 }
1647out:
1648 spin_unlock(&info->lock);
1649 if (xattr)
1650 kfree(xattr->name);
1651 kfree(xattr);
1652 return err;
1653}
1654
Eric Parisb09e0fa2011-05-24 17:12:39 -07001655static const struct xattr_handler *shmem_xattr_handlers[] = {
1656#ifdef CONFIG_TMPFS_POSIX_ACL
1657 &generic_acl_access_handler,
1658 &generic_acl_default_handler,
1659#endif
1660 NULL
1661};
1662
1663static int shmem_xattr_validate(const char *name)
1664{
1665 struct { const char *prefix; size_t len; } arr[] = {
1666 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1667 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1668 };
1669 int i;
1670
1671 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1672 size_t preflen = arr[i].len;
1673 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1674 if (!name[preflen])
1675 return -EINVAL;
1676 return 0;
1677 }
1678 }
1679 return -EOPNOTSUPP;
1680}
1681
1682static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1683 void *buffer, size_t size)
1684{
1685 int err;
1686
1687 /*
1688 * If this is a request for a synthetic attribute in the system.*
1689 * namespace use the generic infrastructure to resolve a handler
1690 * for it via sb->s_xattr.
1691 */
1692 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1693 return generic_getxattr(dentry, name, buffer, size);
1694
1695 err = shmem_xattr_validate(name);
1696 if (err)
1697 return err;
1698
1699 return shmem_xattr_get(dentry, name, buffer, size);
1700}
1701
1702static int shmem_setxattr(struct dentry *dentry, const char *name,
1703 const void *value, size_t size, int flags)
1704{
1705 int err;
1706
1707 /*
1708 * If this is a request for a synthetic attribute in the system.*
1709 * namespace use the generic infrastructure to resolve a handler
1710 * for it via sb->s_xattr.
1711 */
1712 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1713 return generic_setxattr(dentry, name, value, size, flags);
1714
1715 err = shmem_xattr_validate(name);
1716 if (err)
1717 return err;
1718
1719 if (size == 0)
1720 value = ""; /* empty EA, do not remove */
1721
1722 return shmem_xattr_set(dentry, name, value, size, flags);
1723
1724}
1725
1726static int shmem_removexattr(struct dentry *dentry, const char *name)
1727{
1728 int err;
1729
1730 /*
1731 * If this is a request for a synthetic attribute in the system.*
1732 * namespace use the generic infrastructure to resolve a handler
1733 * for it via sb->s_xattr.
1734 */
1735 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1736 return generic_removexattr(dentry, name);
1737
1738 err = shmem_xattr_validate(name);
1739 if (err)
1740 return err;
1741
1742 return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
1743}
1744
1745static bool xattr_is_trusted(const char *name)
1746{
1747 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1748}
1749
1750static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
1751{
1752 bool trusted = capable(CAP_SYS_ADMIN);
1753 struct shmem_xattr *xattr;
1754 struct shmem_inode_info *info;
1755 size_t used = 0;
1756
1757 info = SHMEM_I(dentry->d_inode);
1758
1759 spin_lock(&info->lock);
1760 list_for_each_entry(xattr, &info->xattr_list, list) {
1761 size_t len;
1762
1763 /* skip "trusted." attributes for unprivileged callers */
1764 if (!trusted && xattr_is_trusted(xattr->name))
1765 continue;
1766
1767 len = strlen(xattr->name) + 1;
1768 used += len;
1769 if (buffer) {
1770 if (size < used) {
1771 used = -ERANGE;
1772 break;
1773 }
1774 memcpy(buffer, xattr->name, len);
1775 buffer += len;
1776 }
1777 }
1778 spin_unlock(&info->lock);
1779
1780 return used;
1781}
1782#endif /* CONFIG_TMPFS_XATTR */
1783
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001784static const struct inode_operations shmem_symlink_inline_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 .readlink = generic_readlink,
1786 .follow_link = shmem_follow_link_inline,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001787#ifdef CONFIG_TMPFS_XATTR
1788 .setxattr = shmem_setxattr,
1789 .getxattr = shmem_getxattr,
1790 .listxattr = shmem_listxattr,
1791 .removexattr = shmem_removexattr,
1792#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793};
1794
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001795static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 .readlink = generic_readlink,
1797 .follow_link = shmem_follow_link,
1798 .put_link = shmem_put_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001799#ifdef CONFIG_TMPFS_XATTR
1800 .setxattr = shmem_setxattr,
1801 .getxattr = shmem_getxattr,
1802 .listxattr = shmem_listxattr,
1803 .removexattr = shmem_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001804#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07001805};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001806
David M. Grimes91828a42006-10-17 00:09:45 -07001807static struct dentry *shmem_get_parent(struct dentry *child)
1808{
1809 return ERR_PTR(-ESTALE);
1810}
1811
1812static int shmem_match(struct inode *ino, void *vfh)
1813{
1814 __u32 *fh = vfh;
1815 __u64 inum = fh[2];
1816 inum = (inum << 32) | fh[1];
1817 return ino->i_ino == inum && fh[0] == ino->i_generation;
1818}
1819
Christoph Hellwig480b1162007-10-21 16:42:13 -07001820static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
1821 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07001822{
David M. Grimes91828a42006-10-17 00:09:45 -07001823 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07001824 struct dentry *dentry = NULL;
1825 u64 inum = fid->raw[2];
1826 inum = (inum << 32) | fid->raw[1];
David M. Grimes91828a42006-10-17 00:09:45 -07001827
Christoph Hellwig480b1162007-10-21 16:42:13 -07001828 if (fh_len < 3)
1829 return NULL;
1830
1831 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
1832 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07001833 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07001834 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07001835 iput(inode);
1836 }
1837
Christoph Hellwig480b1162007-10-21 16:42:13 -07001838 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07001839}
1840
1841static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
1842 int connectable)
1843{
1844 struct inode *inode = dentry->d_inode;
1845
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301846 if (*len < 3) {
1847 *len = 3;
David M. Grimes91828a42006-10-17 00:09:45 -07001848 return 255;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301849 }
David M. Grimes91828a42006-10-17 00:09:45 -07001850
Al Viro1d3382cb2010-10-23 15:19:20 -04001851 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07001852 /* Unfortunately insert_inode_hash is not idempotent,
1853 * so as we hash inodes here rather than at creation
1854 * time, we need a lock to ensure we only try
1855 * to do it once
1856 */
1857 static DEFINE_SPINLOCK(lock);
1858 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04001859 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07001860 __insert_inode_hash(inode,
1861 inode->i_ino + inode->i_generation);
1862 spin_unlock(&lock);
1863 }
1864
1865 fh[0] = inode->i_generation;
1866 fh[1] = inode->i_ino;
1867 fh[2] = ((__u64)inode->i_ino) >> 32;
1868
1869 *len = 3;
1870 return 1;
1871}
1872
Christoph Hellwig39655162007-10-21 16:42:17 -07001873static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07001874 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07001875 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07001876 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07001877};
1878
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001879static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
1880 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 char *this_char, *value, *rest;
1883
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00001884 while (options != NULL) {
1885 this_char = options;
1886 for (;;) {
1887 /*
1888 * NUL-terminate this option: unfortunately,
1889 * mount options form a comma-separated list,
1890 * but mpol's nodelist may also contain commas.
1891 */
1892 options = strchr(options, ',');
1893 if (options == NULL)
1894 break;
1895 options++;
1896 if (!isdigit(*options)) {
1897 options[-1] = '\0';
1898 break;
1899 }
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (!*this_char)
1902 continue;
1903 if ((value = strchr(this_char,'=')) != NULL) {
1904 *value++ = 0;
1905 } else {
1906 printk(KERN_ERR
1907 "tmpfs: No value for mount option '%s'\n",
1908 this_char);
1909 return 1;
1910 }
1911
1912 if (!strcmp(this_char,"size")) {
1913 unsigned long long size;
1914 size = memparse(value,&rest);
1915 if (*rest == '%') {
1916 size <<= PAGE_SHIFT;
1917 size *= totalram_pages;
1918 do_div(size, 100);
1919 rest++;
1920 }
1921 if (*rest)
1922 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001923 sbinfo->max_blocks =
1924 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001926 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (*rest)
1928 goto bad_val;
1929 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001930 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (*rest)
1932 goto bad_val;
1933 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001934 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001936 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (*rest)
1938 goto bad_val;
1939 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001940 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001942 sbinfo->uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (*rest)
1944 goto bad_val;
1945 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001946 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001948 sbinfo->gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (*rest)
1950 goto bad_val;
Robin Holt7339ff82006-01-14 13:20:48 -08001951 } else if (!strcmp(this_char,"mpol")) {
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001952 if (mpol_parse_str(value, &sbinfo->mpol, 1))
Robin Holt7339ff82006-01-14 13:20:48 -08001953 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 } else {
1955 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
1956 this_char);
1957 return 1;
1958 }
1959 }
1960 return 0;
1961
1962bad_val:
1963 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
1964 value, this_char);
1965 return 1;
1966
1967}
1968
1969static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
1970{
1971 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001972 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001973 unsigned long inodes;
1974 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001976 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001977 return error;
1978
1979 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001980 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07001981 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001982 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001983 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001984 goto out;
1985 /*
1986 * Those tests also disallow limited->unlimited while any are in
1987 * use, so i_blocks will always be zero when max_blocks is zero;
1988 * but we must separately disallow unlimited->limited, because
1989 * in that case we have no record of how much is already in use.
1990 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001991 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001992 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001993 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001994 goto out;
1995
1996 error = 0;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001997 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001998 sbinfo->max_inodes = config.max_inodes;
1999 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002000
2001 mpol_put(sbinfo->mpol);
2002 sbinfo->mpol = config.mpol; /* transfers initial ref */
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002003out:
2004 spin_unlock(&sbinfo->stat_lock);
2005 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002007
2008static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2009{
2010 struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2011
2012 if (sbinfo->max_blocks != shmem_default_max_blocks())
2013 seq_printf(seq, ",size=%luk",
2014 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2015 if (sbinfo->max_inodes != shmem_default_max_inodes())
2016 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2017 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2018 seq_printf(seq, ",mode=%03o", sbinfo->mode);
2019 if (sbinfo->uid != 0)
2020 seq_printf(seq, ",uid=%u", sbinfo->uid);
2021 if (sbinfo->gid != 0)
2022 seq_printf(seq, ",gid=%u", sbinfo->gid);
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002023 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002024 return 0;
2025}
2026#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028static void shmem_put_super(struct super_block *sb)
2029{
Hugh Dickins602586a2010-08-17 15:23:56 -07002030 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2031
2032 percpu_counter_destroy(&sbinfo->used_blocks);
2033 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 sb->s_fs_info = NULL;
2035}
2036
Kay Sievers2b2af542009-04-30 15:23:42 +02002037int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
2039 struct inode *inode;
2040 struct dentry *root;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002041 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002042 int err = -ENOMEM;
2043
2044 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07002045 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002046 L1_CACHE_BYTES), GFP_KERNEL);
2047 if (!sbinfo)
2048 return -ENOMEM;
2049
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002050 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11002051 sbinfo->uid = current_fsuid();
2052 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002053 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002055#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /*
2057 * Per default we only allow half of the physical ram per
2058 * tmpfs instance, limiting inodes to one per page of lowmem;
2059 * but the internal instance is left unlimited.
2060 */
2061 if (!(sb->s_flags & MS_NOUSER)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002062 sbinfo->max_blocks = shmem_default_max_blocks();
2063 sbinfo->max_inodes = shmem_default_max_inodes();
2064 if (shmem_parse_options(data, sbinfo, false)) {
2065 err = -EINVAL;
2066 goto failed;
2067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
David M. Grimes91828a42006-10-17 00:09:45 -07002069 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070#else
2071 sb->s_flags |= MS_NOUSER;
2072#endif
2073
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002074 spin_lock_init(&sbinfo->stat_lock);
Hugh Dickins602586a2010-08-17 15:23:56 -07002075 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2076 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002077 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002078
Hugh Dickins285b2c42011-08-03 16:21:20 -07002079 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 sb->s_blocksize = PAGE_CACHE_SIZE;
2081 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2082 sb->s_magic = TMPFS_MAGIC;
2083 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01002084 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002085#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002086 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002087#endif
2088#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002089 sb->s_flags |= MS_POSIXACL;
2090#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002091
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002092 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (!inode)
2094 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002095 inode->i_uid = sbinfo->uid;
2096 inode->i_gid = sbinfo->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 root = d_alloc_root(inode);
2098 if (!root)
2099 goto failed_iput;
2100 sb->s_root = root;
2101 return 0;
2102
2103failed_iput:
2104 iput(inode);
2105failed:
2106 shmem_put_super(sb);
2107 return err;
2108}
2109
Pekka Enbergfcc234f2006-03-22 00:08:13 -08002110static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112static struct inode *shmem_alloc_inode(struct super_block *sb)
2113{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002114 struct shmem_inode_info *info;
2115 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2116 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002118 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002121static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002122{
2123 struct inode *inode = container_of(head, struct inode, i_rcu);
2124 INIT_LIST_HEAD(&inode->i_dentry);
2125 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2126}
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128static void shmem_destroy_inode(struct inode *inode)
2129{
2130 if ((inode->i_mode & S_IFMT) == S_IFREG) {
2131 /* only struct inode is valid if it's an inline symlink */
2132 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2133 }
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002134 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002137static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002139 struct shmem_inode_info *info = foo;
2140 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002143static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2146 sizeof(struct shmem_inode_info),
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002147 0, SLAB_PANIC, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return 0;
2149}
2150
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002151static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002153 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002156static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08002158 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07002160 .write_begin = shmem_write_begin,
2161 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162#endif
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07002163 .migratepage = migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +02002164 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165};
2166
Helge Deller15ad7cd2006-12-06 20:40:36 -08002167static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 .mmap = shmem_mmap,
2169#ifdef CONFIG_TMPFS
2170 .llseek = generic_file_llseek,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002171 .read = do_sync_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002172 .write = do_sync_write,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002173 .aio_read = shmem_file_aio_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002174 .aio_write = generic_file_aio_write,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02002175 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07002176 .splice_read = shmem_file_splice_read,
Hugh Dickinsae9764162007-06-04 10:00:39 +02002177 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178#endif
2179};
2180
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002181static const struct inode_operations shmem_inode_operations = {
Hugh Dickins94c1e622011-06-27 16:18:03 -07002182 .setattr = shmem_setattr,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08002183 .truncate_range = shmem_truncate_range,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002184#ifdef CONFIG_TMPFS_XATTR
2185 .setxattr = shmem_setxattr,
2186 .getxattr = shmem_getxattr,
2187 .listxattr = shmem_listxattr,
2188 .removexattr = shmem_removexattr,
2189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190};
2191
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002192static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193#ifdef CONFIG_TMPFS
2194 .create = shmem_create,
2195 .lookup = simple_lookup,
2196 .link = shmem_link,
2197 .unlink = shmem_unlink,
2198 .symlink = shmem_symlink,
2199 .mkdir = shmem_mkdir,
2200 .rmdir = shmem_rmdir,
2201 .mknod = shmem_mknod,
2202 .rename = shmem_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002204#ifdef CONFIG_TMPFS_XATTR
2205 .setxattr = shmem_setxattr,
2206 .getxattr = shmem_getxattr,
2207 .listxattr = shmem_listxattr,
2208 .removexattr = shmem_removexattr,
2209#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002210#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002211 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002212#endif
2213};
2214
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002215static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07002216#ifdef CONFIG_TMPFS_XATTR
2217 .setxattr = shmem_setxattr,
2218 .getxattr = shmem_getxattr,
2219 .listxattr = shmem_listxattr,
2220 .removexattr = shmem_removexattr,
2221#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002222#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002223 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002224#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225};
2226
Hugh Dickins759b9772007-03-05 00:30:28 -08002227static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 .alloc_inode = shmem_alloc_inode,
2229 .destroy_inode = shmem_destroy_inode,
2230#ifdef CONFIG_TMPFS
2231 .statfs = shmem_statfs,
2232 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002233 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234#endif
Al Viro1f895f72010-06-05 19:10:41 -04002235 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 .drop_inode = generic_delete_inode,
2237 .put_super = shmem_put_super,
2238};
2239
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002240static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002241 .fault = shmem_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242#ifdef CONFIG_NUMA
2243 .set_policy = shmem_set_policy,
2244 .get_policy = shmem_get_policy,
2245#endif
2246};
2247
Al Viro3c26ff62010-07-25 11:46:36 +04002248static struct dentry *shmem_mount(struct file_system_type *fs_type,
2249 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Al Viro3c26ff62010-07-25 11:46:36 +04002251 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002254static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 .owner = THIS_MODULE,
2256 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002257 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 .kill_sb = kill_litter_super,
2259};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002261int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 int error;
2264
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002265 error = bdi_init(&shmem_backing_dev_info);
2266 if (error)
2267 goto out4;
2268
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002269 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (error)
2271 goto out3;
2272
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002273 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (error) {
2275 printk(KERN_ERR "Could not register tmpfs\n");
2276 goto out2;
2277 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07002278
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002279 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2280 shmem_fs_type.name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (IS_ERR(shm_mnt)) {
2282 error = PTR_ERR(shm_mnt);
2283 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2284 goto out1;
2285 }
2286 return 0;
2287
2288out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002289 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002291 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292out3:
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002293 bdi_destroy(&shmem_backing_dev_info);
2294out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 shm_mnt = ERR_PTR(error);
2296 return error;
2297}
Matt Mackall853ac432009-01-06 14:40:20 -08002298
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002299#ifdef CONFIG_CGROUP_MEM_RES_CTLR
2300/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002301 * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002302 * @inode: the inode to be searched
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002303 * @index: the page offset to be searched
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002304 * @pagep: the pointer for the found page to be stored
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002305 * @swapp: the pointer for the found swap entry to be stored
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002306 *
2307 * If a page is found, refcount of it is incremented. Callers should handle
2308 * these refcount.
2309 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002310void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index,
2311 struct page **pagep, swp_entry_t *swapp)
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002312{
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002313 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002314 struct page *page = NULL;
2315 swp_entry_t swap = {0};
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002316
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002317 if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002318 goto out;
2319
2320 spin_lock(&info->lock);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002321#ifdef CONFIG_SWAP
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002322 swap = shmem_get_swap(info, index);
2323 if (swap.val)
2324 page = find_get_page(&swapper_space, swap.val);
Hugh Dickins285b2c42011-08-03 16:21:20 -07002325 else
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002326#endif
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002327 page = find_get_page(inode->i_mapping, index);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002328 spin_unlock(&info->lock);
2329out:
2330 *pagep = page;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002331 *swapp = swap;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002332}
2333#endif
2334
Matt Mackall853ac432009-01-06 14:40:20 -08002335#else /* !CONFIG_SHMEM */
2336
2337/*
2338 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2339 *
2340 * This is intended for small system where the benefits of the full
2341 * shmem code (swap-backed and resource-limited) are outweighed by
2342 * their complexity. On systems without swap this code should be
2343 * effectively equivalent, but much lighter weight.
2344 */
2345
2346#include <linux/ramfs.h>
2347
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002348static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08002349 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002350 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08002351 .kill_sb = kill_litter_super,
2352};
2353
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002354int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08002355{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002356 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08002357
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002358 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08002359 BUG_ON(IS_ERR(shm_mnt));
2360
2361 return 0;
2362}
2363
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002364int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08002365{
2366 return 0;
2367}
2368
Hugh Dickins3f96b792009-09-21 17:03:37 -07002369int shmem_lock(struct file *file, int lock, struct user_struct *user)
2370{
2371 return 0;
2372}
2373
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002374void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07002375{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002376 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07002377}
2378EXPORT_SYMBOL_GPL(shmem_truncate_range);
2379
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002380#ifdef CONFIG_CGROUP_MEM_RES_CTLR
2381/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002382 * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002383 * @inode: the inode to be searched
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002384 * @index: the page offset to be searched
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002385 * @pagep: the pointer for the found page to be stored
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002386 * @swapp: the pointer for the found swap entry to be stored
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002387 *
2388 * If a page is found, refcount of it is incremented. Callers should handle
2389 * these refcount.
2390 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002391void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index,
2392 struct page **pagep, swp_entry_t *swapp)
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002393{
2394 struct page *page = NULL;
2395
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002396 if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002397 goto out;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002398 page = find_get_page(inode->i_mapping, index);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002399out:
2400 *pagep = page;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002401 *swapp = (swp_entry_t){0};
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002402}
2403#endif
2404
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002405#define shmem_vm_ops generic_file_vm_ops
2406#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002407#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002408#define shmem_acct_size(flags, size) 0
2409#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08002410
2411#endif /* CONFIG_SHMEM */
2412
2413/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Randy Dunlap46711812008-03-19 17:00:41 -07002415/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 * shmem_file_setup - get an unlinked file living in tmpfs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 * @name: name for dentry (to be seen in /proc/<pid>/maps
2418 * @size: size to be set for the file
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002419 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 */
Sergei Trofimovich168f5ac2009-06-16 15:33:02 -07002421struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
2423 int error;
2424 struct file *file;
2425 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04002426 struct path path;
2427 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 struct qstr this;
2429
2430 if (IS_ERR(shm_mnt))
2431 return (void *)shm_mnt;
2432
Hugh Dickins285b2c42011-08-03 16:21:20 -07002433 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 return ERR_PTR(-EINVAL);
2435
2436 if (shmem_acct_size(flags, size))
2437 return ERR_PTR(-ENOMEM);
2438
2439 error = -ENOMEM;
2440 this.name = name;
2441 this.len = strlen(name);
2442 this.hash = 0; /* will go */
2443 root = shm_mnt->mnt_root;
Al Viro2c48b9c2009-08-09 00:52:35 +04002444 path.dentry = d_alloc(root, &this);
2445 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 goto put_memory;
Al Viro2c48b9c2009-08-09 00:52:35 +04002447 path.mnt = mntget(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 error = -ENOSPC;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002450 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (!inode)
Al Viro4b42af82009-08-05 18:25:56 +04002452 goto put_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Al Viro2c48b9c2009-08-09 00:52:35 +04002454 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 inode->i_size = size;
2456 inode->i_nlink = 0; /* It is unlinked */
Matt Mackall853ac432009-01-06 14:40:20 -08002457#ifndef CONFIG_MMU
2458 error = ramfs_nommu_expand_for_mapping(inode, size);
2459 if (error)
Al Viro4b42af82009-08-05 18:25:56 +04002460 goto put_dentry;
Matt Mackall853ac432009-01-06 14:40:20 -08002461#endif
Al Viro4b42af82009-08-05 18:25:56 +04002462
2463 error = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04002464 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04002465 &shmem_file_operations);
2466 if (!file)
2467 goto put_dentry;
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 return file;
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471put_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04002472 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473put_memory:
2474 shmem_unacct_size(flags, size);
2475 return ERR_PTR(error);
2476}
Keith Packard395e0dd2008-06-20 00:08:06 -07002477EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Randy Dunlap46711812008-03-19 17:00:41 -07002479/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2482 */
2483int shmem_zero_setup(struct vm_area_struct *vma)
2484{
2485 struct file *file;
2486 loff_t size = vma->vm_end - vma->vm_start;
2487
2488 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2489 if (IS_ERR(file))
2490 return PTR_ERR(file);
2491
2492 if (vma->vm_file)
2493 fput(vma->vm_file);
2494 vma->vm_file = file;
2495 vma->vm_ops = &shmem_vm_ops;
Hugh Dickinsbee4c36a2011-03-22 16:33:43 -07002496 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return 0;
2498}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002499
2500/**
2501 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2502 * @mapping: the page's address_space
2503 * @index: the page index
2504 * @gfp: the page allocator flags to use if allocating
2505 *
2506 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2507 * with any new page allocations done using the specified allocation flags.
2508 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2509 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2510 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2511 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07002512 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2513 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002514 */
2515struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2516 pgoff_t index, gfp_t gfp)
2517{
Hugh Dickins68da9f02011-07-25 17:12:34 -07002518#ifdef CONFIG_SHMEM
2519 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002520 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07002521 int error;
2522
2523 BUG_ON(mapping->a_ops != &shmem_aops);
2524 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2525 if (error)
2526 page = ERR_PTR(error);
2527 else
2528 unlock_page(page);
2529 return page;
2530#else
2531 /*
2532 * The tiny !SHMEM case uses ramfs without swap
2533 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002534 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002535#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002536}
2537EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);