blob: 4439b7d55819c7ae849f561b09abc6c0ef0ea60f [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 Mortonb76db732008-02-08 04:21:49 -080090#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080091static unsigned long shmem_default_max_blocks(void)
92{
93 return totalram_pages / 2;
94}
95
96static unsigned long shmem_default_max_inodes(void)
97{
98 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
99}
Andrew Mortonb76db732008-02-08 04:21:49 -0800100#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800101
Hugh Dickins68da9f02011-07-25 17:12:34 -0700102static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
103 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
104
105static inline int shmem_getpage(struct inode *inode, pgoff_t index,
106 struct page **pagep, enum sgp_type sgp, int *fault_type)
107{
108 return shmem_getpage_gfp(inode, index, pagep, sgp,
109 mapping_gfp_mask(inode->i_mapping), fault_type);
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
113{
114 return sb->s_fs_info;
115}
116
117/*
118 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
119 * for shared memory and for shared anonymous (/dev/zero) mappings
120 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
121 * consistent with the pre-accounting of private mappings ...
122 */
123static inline int shmem_acct_size(unsigned long flags, loff_t size)
124{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000125 return (flags & VM_NORESERVE) ?
126 0 : security_vm_enough_memory_kern(VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static inline void shmem_unacct_size(unsigned long flags, loff_t size)
130{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000131 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 vm_unacct_memory(VM_ACCT(size));
133}
134
135/*
136 * ... whereas tmpfs objects are accounted incrementally as
137 * pages are allocated, in order to allow huge sparse files.
138 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
139 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
140 */
141static inline int shmem_acct_block(unsigned long flags)
142{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000143 return (flags & VM_NORESERVE) ?
144 security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147static inline void shmem_unacct_blocks(unsigned long flags, long pages)
148{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000149 if (flags & VM_NORESERVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
151}
152
Hugh Dickins759b9772007-03-05 00:30:28 -0800153static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700154static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800155static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800156static const struct inode_operations shmem_inode_operations;
157static const struct inode_operations shmem_dir_inode_operations;
158static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400159static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700161static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 .ra_pages = 0, /* No readahead */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700163 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164};
165
166static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800167static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169static void shmem_free_blocks(struct inode *inode, long pages)
170{
171 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700172 if (sbinfo->max_blocks) {
Tim Chen7e496292010-08-09 17:19:05 -0700173 percpu_counter_add(&sbinfo->used_blocks, -pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 inode->i_blocks -= pages*BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176}
177
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800178static int shmem_reserve_inode(struct super_block *sb)
179{
180 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
181 if (sbinfo->max_inodes) {
182 spin_lock(&sbinfo->stat_lock);
183 if (!sbinfo->free_inodes) {
184 spin_unlock(&sbinfo->stat_lock);
185 return -ENOSPC;
186 }
187 sbinfo->free_inodes--;
188 spin_unlock(&sbinfo->stat_lock);
189 }
190 return 0;
191}
192
193static void shmem_free_inode(struct super_block *sb)
194{
195 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
196 if (sbinfo->max_inodes) {
197 spin_lock(&sbinfo->stat_lock);
198 sbinfo->free_inodes++;
199 spin_unlock(&sbinfo->stat_lock);
200 }
201}
202
Randy Dunlap46711812008-03-19 17:00:41 -0700203/**
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 Dickins7a5d0fb2011-08-03 16:21:22 -0700241/*
242 * Replace item expected in radix tree by a new item, while holding tree lock.
243 */
244static int shmem_radix_tree_replace(struct address_space *mapping,
245 pgoff_t index, void *expected, void *replacement)
246{
247 void **pslot;
248 void *item = NULL;
249
250 VM_BUG_ON(!expected);
251 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
252 if (pslot)
253 item = radix_tree_deref_slot_protected(pslot,
254 &mapping->tree_lock);
255 if (item != expected)
256 return -ENOENT;
257 if (replacement)
258 radix_tree_replace_slot(pslot, replacement);
259 else
260 radix_tree_delete(&mapping->page_tree, index);
261 return 0;
262}
263
264/*
265 * Like find_get_pages, but collecting swap entries as well as pages.
266 */
267static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
268 pgoff_t start, unsigned int nr_pages,
269 struct page **pages, pgoff_t *indices)
270{
271 unsigned int i;
272 unsigned int ret;
273 unsigned int nr_found;
274
275 rcu_read_lock();
276restart:
277 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
278 (void ***)pages, indices, start, nr_pages);
279 ret = 0;
280 for (i = 0; i < nr_found; i++) {
281 struct page *page;
282repeat:
283 page = radix_tree_deref_slot((void **)pages[i]);
284 if (unlikely(!page))
285 continue;
286 if (radix_tree_exception(page)) {
287 if (radix_tree_exceptional_entry(page))
288 goto export;
289 /* radix_tree_deref_retry(page) */
290 goto restart;
291 }
292 if (!page_cache_get_speculative(page))
293 goto repeat;
294
295 /* Has the page moved? */
296 if (unlikely(page != *((void **)pages[i]))) {
297 page_cache_release(page);
298 goto repeat;
299 }
300export:
301 indices[ret] = indices[i];
302 pages[ret] = page;
303 ret++;
304 }
305 if (unlikely(!ret && nr_found))
306 goto restart;
307 rcu_read_unlock();
308 return ret;
309}
310
311/*
312 * Remove swap entry from radix tree, free the swap and its page cache.
313 */
314static int shmem_free_swap(struct address_space *mapping,
315 pgoff_t index, void *radswap)
316{
317 int error;
318
319 spin_lock_irq(&mapping->tree_lock);
320 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
321 spin_unlock_irq(&mapping->tree_lock);
322 if (!error)
323 free_swap_and_cache(radix_to_swp_entry(radswap));
324 return error;
325}
326
327/*
328 * Pagevec may contain swap entries, so shuffle up pages before releasing.
329 */
330static void shmem_pagevec_release(struct pagevec *pvec)
331{
332 int i, j;
333
334 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
335 struct page *page = pvec->pages[i];
336 if (!radix_tree_exceptional_entry(page))
337 pvec->pages[j++] = page;
338 }
339 pvec->nr = j;
340 pagevec_release(pvec);
341}
342
343/*
344 * Remove range of pages and swap entries from radix tree, and free them.
345 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700346void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700348 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700350 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700351 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700352 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700353 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700354 pgoff_t indices[PAGEVEC_SIZE];
355 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700356 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700357 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700359 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
360
361 pagevec_init(&pvec, 0);
362 index = start;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700363 while (index <= end) {
364 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
365 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
366 pvec.pages, indices);
367 if (!pvec.nr)
368 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700369 mem_cgroup_uncharge_start();
370 for (i = 0; i < pagevec_count(&pvec); i++) {
371 struct page *page = pvec.pages[i];
372
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700373 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700374 if (index > end)
375 break;
376
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700377 if (radix_tree_exceptional_entry(page)) {
378 nr_swaps_freed += !shmem_free_swap(mapping,
379 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700380 continue;
381 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700382
383 if (!trylock_page(page))
384 continue;
385 if (page->mapping == mapping) {
386 VM_BUG_ON(PageWriteback(page));
387 truncate_inode_page(mapping, page);
388 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700389 unlock_page(page);
390 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700391 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700392 mem_cgroup_uncharge_end();
393 cond_resched();
394 index++;
395 }
396
397 if (partial) {
398 struct page *page = NULL;
399 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
400 if (page) {
401 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
402 set_page_dirty(page);
403 unlock_page(page);
404 page_cache_release(page);
405 }
406 }
407
408 index = start;
409 for ( ; ; ) {
410 cond_resched();
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700411 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
412 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
413 pvec.pages, indices);
414 if (!pvec.nr) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700415 if (index == start)
416 break;
417 index = start;
418 continue;
419 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700420 if (index == start && indices[0] > end) {
421 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700422 break;
423 }
424 mem_cgroup_uncharge_start();
425 for (i = 0; i < pagevec_count(&pvec); i++) {
426 struct page *page = pvec.pages[i];
427
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700428 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700429 if (index > end)
430 break;
431
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700432 if (radix_tree_exceptional_entry(page)) {
433 nr_swaps_freed += !shmem_free_swap(mapping,
434 index, page);
435 continue;
436 }
437
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700438 lock_page(page);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700439 if (page->mapping == mapping) {
440 VM_BUG_ON(PageWriteback(page));
441 truncate_inode_page(mapping, page);
442 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700443 unlock_page(page);
444 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700445 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700446 mem_cgroup_uncharge_end();
447 index++;
448 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 spin_lock(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700451 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 shmem_recalc_inode(inode);
453 spin_unlock(&info->lock);
454
Hugh Dickins285b2c42011-08-03 16:21:20 -0700455 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700457EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Hugh Dickins94c1e622011-06-27 16:18:03 -0700459static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int error;
463
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200464 error = inode_change_ok(inode, attr);
465 if (error)
466 return error;
467
Hugh Dickins94c1e622011-06-27 16:18:03 -0700468 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
469 loff_t oldsize = inode->i_size;
470 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000471
Hugh Dickins94c1e622011-06-27 16:18:03 -0700472 if (newsize != oldsize) {
473 i_size_write(inode, newsize);
474 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
475 }
476 if (newsize < oldsize) {
477 loff_t holebegin = round_up(newsize, PAGE_SIZE);
478 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
479 shmem_truncate_range(inode, newsize, (loff_t)-1);
480 /* unmap again to remove racily COWed private pages */
481 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200485 setattr_copy(inode, attr);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700486#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200487 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100488 error = generic_acl_chmod(inode);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700489#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return error;
491}
492
Al Viro1f895f72010-06-05 19:10:41 -0400493static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct shmem_inode_info *info = SHMEM_I(inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700496 struct shmem_xattr *xattr, *nxattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000498 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 shmem_unacct_size(info->flags, inode->i_size);
500 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000501 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800503 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800505 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 }
Eric Parisb09e0fa2011-05-24 17:12:39 -0700508
509 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
510 kfree(xattr->name);
511 kfree(xattr);
512 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700513 BUG_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800514 shmem_free_inode(inode->i_sb);
Al Viro1f895f72010-06-05 19:10:41 -0400515 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700518static int shmem_unuse_inode(struct shmem_inode_info *info,
519 swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700521 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700522 pgoff_t index;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800523 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700525 for (index = 0; index < SHMEM_NR_DIRECT; index++)
526 if (shmem_get_swap(info, index).val == swap.val)
Hugh Dickins285b2c42011-08-03 16:21:20 -0700527 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return 0;
529found:
Hugh Dickins285b2c42011-08-03 16:21:20 -0700530 spin_lock(&info->lock);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700531 if (shmem_get_swap(info, index).val != swap.val) {
Hugh Dickins285b2c42011-08-03 16:21:20 -0700532 spin_unlock(&info->lock);
533 return 0;
534 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800535
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800536 /*
537 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400538 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800539 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700540 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800541 */
542 if (shmem_swaplist.next != &info->swaplist)
543 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800544
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800545 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700546 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
547 * but also to hold up shmem_evict_inode(): so inode cannot be freed
548 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800549 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700550 error = add_to_page_cache_locked(page, mapping, index, GFP_NOWAIT);
Hugh Dickins778dd892011-05-11 15:13:37 -0700551 /* which does mem_cgroup_uncharge_cache_page on error */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700552
Hugh Dickins48f170f2011-07-25 17:12:37 -0700553 if (error != -ENOMEM) {
Hugh Dickins73b12622008-02-04 22:28:50 -0800554 delete_from_swap_cache(page);
555 set_page_dirty(page);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700556 shmem_put_swap(info, index, (swp_entry_t){0});
Hugh Dickins285b2c42011-08-03 16:21:20 -0700557 info->swapped--;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700558 swap_free(swap);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800559 error = 1; /* not an error, but entry was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_unlock(&info->lock);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800562 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/*
566 * shmem_unuse() search for an eventually swapped out shmem page.
567 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700568int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700570 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 struct shmem_inode_info *info;
572 int found = 0;
Hugh Dickins778dd892011-05-11 15:13:37 -0700573 int error;
574
575 /*
576 * Charge page using GFP_KERNEL while we can wait, before taking
577 * the shmem_swaplist_mutex which might hold up shmem_writepage().
578 * Charged back to the user (not to caller) when swap account is used.
579 * add_to_page_cache() will be called with GFP_NOWAIT.
580 */
581 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
582 if (error)
583 goto out;
584 /*
585 * Try to preload while we can wait, to not make a habit of
586 * draining atomic reserves; but don't latch on to this cpu,
587 * it's okay if sometimes we get rescheduled after this.
588 */
589 error = radix_tree_preload(GFP_KERNEL);
590 if (error)
591 goto uncharge;
592 radix_tree_preload_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800594 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700595 list_for_each_safe(this, next, &shmem_swaplist) {
596 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700597 if (!info->swapped) {
598 spin_lock(&info->lock);
599 if (!info->swapped)
600 list_del_init(&info->swaplist);
601 spin_unlock(&info->lock);
602 }
603 if (info->swapped)
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700604 found = shmem_unuse_inode(info, swap, page);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800605 cond_resched();
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800606 if (found)
Hugh Dickins778dd892011-05-11 15:13:37 -0700607 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800609 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -0700610
611uncharge:
612 if (!found)
613 mem_cgroup_uncharge_cache_page(page);
614 if (found < 0)
615 error = found;
616out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800617 unlock_page(page);
618 page_cache_release(page);
Hugh Dickins778dd892011-05-11 15:13:37 -0700619 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622/*
623 * Move the page from the page cache to the swap cache.
624 */
625static int shmem_writepage(struct page *page, struct writeback_control *wbc)
626{
627 struct shmem_inode_info *info;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700628 swp_entry_t swap, oswap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct address_space *mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700630 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct inode *inode;
632
633 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 mapping = page->mapping;
635 index = page->index;
636 inode = mapping->host;
637 info = SHMEM_I(inode);
638 if (info->flags & VM_LOCKED)
639 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800640 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 goto redirty;
642
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800643 /*
644 * shmem_backing_dev_info's capabilities prevent regular writeback or
645 * sync from ever calling shmem_writepage; but a stacking filesystem
Hugh Dickins48f170f2011-07-25 17:12:37 -0700646 * might use ->writepage of its underlying filesystem, in which case
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800647 * tmpfs should write out to swap only in response to memory pressure,
Hugh Dickins48f170f2011-07-25 17:12:37 -0700648 * and not for the writeback threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800649 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700650 if (!wbc->for_reclaim) {
651 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
652 goto redirty;
653 }
Hugh Dickins285b2c42011-08-03 16:21:20 -0700654
655 /*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700656 * Disable even the toy swapping implementation, while we convert
657 * functions one by one to having swap entries in the radix tree.
Hugh Dickins285b2c42011-08-03 16:21:20 -0700658 */
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700659 if (index < ULONG_MAX)
Hugh Dickins285b2c42011-08-03 16:21:20 -0700660 goto redirty;
661
Hugh Dickins48f170f2011-07-25 17:12:37 -0700662 swap = get_swap_page();
663 if (!swap.val)
664 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800665
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700666 /*
667 * Add inode to shmem_unuse()'s list of swapped-out inodes,
668 * if it's not already there. Do it now because we cannot take
669 * mutex while holding spinlock, and must do so before the page
670 * is moved to swap cache, when its pagelock no longer protects
671 * the inode from eviction. But don't unlock the mutex until
672 * we've taken the spinlock, because shmem_unuse_inode() will
673 * prune a !swapped inode from the swaplist under both locks.
674 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700675 mutex_lock(&shmem_swaplist_mutex);
676 if (list_empty(&info->swaplist))
677 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 spin_lock(&info->lock);
Hugh Dickins48f170f2011-07-25 17:12:37 -0700680 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700681
Hugh Dickins285b2c42011-08-03 16:21:20 -0700682 oswap = shmem_get_swap(info, index);
683 if (oswap.val) {
Hugh Dickins48f170f2011-07-25 17:12:37 -0700684 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700685 free_swap_and_cache(oswap);
686 shmem_put_swap(info, index, (swp_entry_t){0});
687 info->swapped--;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800688 }
689 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Hugh Dickins48f170f2011-07-25 17:12:37 -0700691 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Minchan Kim4c73b1b2011-03-22 16:32:40 -0700692 delete_from_page_cache(page);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700693 shmem_put_swap(info, index, swap);
694 info->swapped++;
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800695 swap_shmem_alloc(swap);
Hugh Dickins826267c2011-05-28 13:14:09 -0700696 spin_unlock(&info->lock);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800697 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -0700698 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return 0;
700 }
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 spin_unlock(&info->lock);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700703 swapcache_free(swap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704redirty:
705 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800706 if (wbc->for_reclaim)
707 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
708 unlock_page(page);
709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712#ifdef CONFIG_NUMA
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800713#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700714static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800715{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700716 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800717
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700718 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700719 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800720
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700721 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800722
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700723 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800724}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700725
726static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
727{
728 struct mempolicy *mpol = NULL;
729 if (sbinfo->mpol) {
730 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
731 mpol = sbinfo->mpol;
732 mpol_get(mpol);
733 spin_unlock(&sbinfo->stat_lock);
734 }
735 return mpol;
736}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800737#endif /* CONFIG_TMPFS */
738
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700739static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
740 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700742 struct mempolicy mpol, *spol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct vm_area_struct pvma;
744
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700745 spol = mpol_cond_copy(&mpol,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700746 mpol_shared_policy_lookup(&info->policy, index));
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* Create a pseudo vma that just contains the policy */
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800749 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700750 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800751 pvma.vm_ops = NULL;
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700752 pvma.vm_policy = spol;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700753 return swapin_readahead(swap, gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Hugh Dickins02098fe2008-02-04 22:28:42 -0800756static struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700757 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct vm_area_struct pvma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800761 /* Create a pseudo vma that just contains the policy */
762 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700763 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800764 pvma.vm_ops = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700765 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700766
767 /*
768 * alloc_page_vma() will drop the shared policy reference
769 */
770 return alloc_page_vma(gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800772#else /* !CONFIG_NUMA */
773#ifdef CONFIG_TMPFS
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700774static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800775{
776}
777#endif /* CONFIG_TMPFS */
778
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700779static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
780 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700782 return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Hugh Dickins02098fe2008-02-04 22:28:42 -0800785static inline struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700786 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Hugh Dickinse84e2e12007-11-28 18:55:10 +0000788 return alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800790#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700792#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
793static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
794{
795 return NULL;
796}
797#endif
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799/*
Hugh Dickins68da9f02011-07-25 17:12:34 -0700800 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 *
802 * If we allocate a new one we do not mark it dirty. That's up to the
803 * vm. If we swap it in we mark it dirty since we also free the swap
804 * entry since a page cannot live in both the swap and page cache
805 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700806static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Hugh Dickins68da9f02011-07-25 17:12:34 -0700807 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 struct address_space *mapping = inode->i_mapping;
810 struct shmem_inode_info *info = SHMEM_I(inode);
811 struct shmem_sb_info *sbinfo;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700812 struct page *page;
Shaohua Liff36b8012010-08-09 17:19:06 -0700813 struct page *prealloc_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 swp_entry_t swap;
815 int error;
816
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700817 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819repeat:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700820 page = find_lock_page(mapping, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700821 if (page) {
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800822 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700823 * Once we can get the page lock, it must be uptodate:
824 * if there were an error in reading back from swap,
825 * the page would not be inserted into the filecache.
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800826 */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700827 BUG_ON(!PageUptodate(page));
828 goto done;
829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700832 * Try to preload while we can wait, to not make a habit of
833 * draining atomic reserves; but don't latch on to this cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700835 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
836 if (error)
837 goto out;
838 radix_tree_preload_end();
839
840 if (sgp != SGP_READ && !prealloc_page) {
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700841 prealloc_page = shmem_alloc_page(gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700842 if (prealloc_page) {
843 SetPageSwapBacked(prealloc_page);
844 if (mem_cgroup_cache_charge(prealloc_page,
845 current->mm, GFP_KERNEL)) {
846 page_cache_release(prealloc_page);
847 prealloc_page = NULL;
Shaohua Liff36b8012010-08-09 17:19:06 -0700848 }
849 }
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 spin_lock(&info->lock);
853 shmem_recalc_inode(inode);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700854 swap = shmem_get_swap(info, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (swap.val) {
856 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700857 page = lookup_swap_cache(swap);
858 if (!page) {
Christoph Lameterf8891e52006-06-30 01:55:45 -0700859 spin_unlock(&info->lock);
Ying Han456f9982011-05-26 16:25:38 -0700860 /* here we actually do the io */
Hugh Dickins68da9f02011-07-25 17:12:34 -0700861 if (fault_type)
862 *fault_type |= VM_FAULT_MAJOR;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700863 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700864 if (!page) {
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700865 swp_entry_t nswap = shmem_get_swap(info, index);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700866 if (nswap.val == swap.val) {
867 error = -ENOMEM;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700868 goto out;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto repeat;
871 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700872 wait_on_page_locked(page);
873 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 goto repeat;
875 }
876
877 /* We have to do this with page locked to prevent races */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700878 if (!trylock_page(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700880 wait_on_page_locked(page);
881 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 goto repeat;
883 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700884 if (PageWriteback(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700886 wait_on_page_writeback(page);
887 unlock_page(page);
888 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 goto repeat;
890 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700891 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700893 unlock_page(page);
894 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 error = -EIO;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700896 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
Hugh Dickins27ab7002011-07-25 17:12:36 -0700899 error = add_to_page_cache_locked(page, mapping,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700900 index, GFP_NOWAIT);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700901 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 spin_unlock(&info->lock);
Hugh Dickins82369552008-02-07 00:14:22 -0800903 if (error == -ENOMEM) {
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -0700904 /*
905 * reclaim from proper memory cgroup and
906 * call memcg's OOM if needed.
907 */
908 error = mem_cgroup_shmem_charge_fallback(
Hugh Dickins27ab7002011-07-25 17:12:36 -0700909 page, current->mm, gfp);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -0800910 if (error) {
Hugh Dickins27ab7002011-07-25 17:12:36 -0700911 unlock_page(page);
912 page_cache_release(page);
913 goto out;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -0800914 }
Hugh Dickins82369552008-02-07 00:14:22 -0800915 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700916 unlock_page(page);
917 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 goto repeat;
919 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700920
Hugh Dickins27ab7002011-07-25 17:12:36 -0700921 delete_from_swap_cache(page);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700922 shmem_put_swap(info, index, (swp_entry_t){0});
Hugh Dickins285b2c42011-08-03 16:21:20 -0700923 info->swapped--;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700924 spin_unlock(&info->lock);
925 set_page_dirty(page);
926 swap_free(swap);
927
928 } else if (sgp == SGP_READ) {
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700929 page = find_get_page(mapping, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700930 if (page && !trylock_page(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700932 wait_on_page_locked(page);
933 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 goto repeat;
935 }
936 spin_unlock(&info->lock);
Hugh Dickinse83c32e2011-07-25 17:12:35 -0700937
938 } else if (prealloc_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700940 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -0700941 if (percpu_counter_compare(&sbinfo->used_blocks,
942 sbinfo->max_blocks) >= 0 ||
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700943 shmem_acct_block(info->flags))
944 goto nospace;
Tim Chen7e496292010-08-09 17:19:05 -0700945 percpu_counter_inc(&sbinfo->used_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 inode->i_blocks += BLOCKS_PER_PAGE;
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700947 } else if (shmem_acct_block(info->flags))
948 goto nospace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Hugh Dickins27ab7002011-07-25 17:12:36 -0700950 page = prealloc_page;
951 prealloc_page = NULL;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700952
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700953 swap = shmem_get_swap(info, index);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700954 if (swap.val)
Hugh Dickins27ab7002011-07-25 17:12:36 -0700955 mem_cgroup_uncharge_cache_page(page);
956 else
Hugh Dickins285b2c42011-08-03 16:21:20 -0700957 error = add_to_page_cache_lru(page, mapping,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700958 index, GFP_NOWAIT);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700959 /*
960 * At add_to_page_cache_lru() failure,
961 * uncharge will be done automatically.
962 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700963 if (swap.val || error) {
Hugh Dickins27ab7002011-07-25 17:12:36 -0700964 shmem_unacct_blocks(info->flags, 1);
965 shmem_free_blocks(inode, 1);
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700966 spin_unlock(&info->lock);
967 page_cache_release(page);
968 goto repeat;
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 info->alloced++;
972 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700973 clear_highpage(page);
974 flush_dcache_page(page);
975 SetPageUptodate(page);
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -0800976 if (sgp == SGP_DIRTY)
Hugh Dickins27ab7002011-07-25 17:12:36 -0700977 set_page_dirty(page);
978
Hugh Dickinse83c32e2011-07-25 17:12:35 -0700979 } else {
980 spin_unlock(&info->lock);
981 error = -ENOMEM;
982 goto out;
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984done:
Hugh Dickins27ab7002011-07-25 17:12:36 -0700985 *pagep = page;
Shaohua Liff36b8012010-08-09 17:19:06 -0700986 error = 0;
Shaohua Liff36b8012010-08-09 17:19:06 -0700987out:
988 if (prealloc_page) {
989 mem_cgroup_uncharge_cache_page(prealloc_page);
990 page_cache_release(prealloc_page);
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return error;
Nick Piggind00806b2007-07-19 01:46:57 -0700993
Hugh Dickins27d54b32008-02-04 22:28:43 -0800994nospace:
Nick Piggind0217ac2007-07-19 01:47:03 -0700995 /*
996 * Perhaps the page was brought in from swap between find_lock_page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 * and taking info->lock? We allow for that at add_to_page_cache_lru,
Nick Piggin83c54072007-07-19 01:47:05 -0700998 * but must also avoid reporting a spurious ENOSPC while working on a
Hugh Dickins9276aad2011-07-25 17:12:34 -0700999 * full tmpfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001001 page = find_get_page(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001003 if (page) {
1004 page_cache_release(page);
1005 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001006 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001007 error = -ENOSPC;
Hugh Dickinse83c32e2011-07-25 17:12:35 -07001008 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1012{
1013 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1014 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001015 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1018 return VM_FAULT_SIGBUS;
1019
1020 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1021 if (error)
1022 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001023
Ying Han456f9982011-05-26 16:25:38 -07001024 if (ret & VM_FAULT_MAJOR) {
1025 count_vm_event(PGMAJFAULT);
1026 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1027 }
Hugh Dickins68da9f02011-07-25 17:12:34 -07001028 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001032static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001034 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1035 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001038static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1039 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001041 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1042 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001044 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1045 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047#endif
1048
1049int shmem_lock(struct file *file, int lock, struct user_struct *user)
1050{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001051 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 struct shmem_inode_info *info = SHMEM_I(inode);
1053 int retval = -ENOMEM;
1054
1055 spin_lock(&info->lock);
1056 if (lock && !(info->flags & VM_LOCKED)) {
1057 if (!user_shm_lock(inode->i_size, user))
1058 goto out_nomem;
1059 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001060 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062 if (!lock && (info->flags & VM_LOCKED) && user) {
1063 user_shm_unlock(inode->i_size, user);
1064 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001065 mapping_clear_unevictable(file->f_mapping);
1066 scan_mapping_unevictable_pages(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070out_nomem:
1071 spin_unlock(&info->lock);
1072 return retval;
1073}
1074
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001075static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 file_accessed(file);
1078 vma->vm_ops = &shmem_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001079 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return 0;
1081}
1082
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001083static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1084 int mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 struct inode *inode;
1087 struct shmem_inode_info *info;
1088 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1089
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001090 if (shmem_reserve_inode(sb))
1091 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 inode = new_inode(sb);
1094 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001095 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001096 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1099 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001100 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 info = SHMEM_I(inode);
1102 memset(info, 0, (char *)inode - (char *)info);
1103 spin_lock_init(&info->lock);
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001104 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 INIT_LIST_HEAD(&info->swaplist);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001106 INIT_LIST_HEAD(&info->xattr_list);
Al Viro72c04902009-06-24 16:58:48 -04001107 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 switch (mode & S_IFMT) {
1110 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001111 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 init_special_inode(inode, mode, dev);
1113 break;
1114 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001115 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 inode->i_op = &shmem_inode_operations;
1117 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001118 mpol_shared_policy_init(&info->policy,
1119 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 break;
1121 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001122 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /* Some things misbehave if size == 0 on a directory */
1124 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1125 inode->i_op = &shmem_dir_inode_operations;
1126 inode->i_fop = &simple_dir_operations;
1127 break;
1128 case S_IFLNK:
1129 /*
1130 * Must not load anything in the rbtree,
1131 * mpol_free_shared_policy will not be called.
1132 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001133 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
1135 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001136 } else
1137 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return inode;
1139}
1140
1141#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001142static const struct inode_operations shmem_symlink_inode_operations;
1143static const struct inode_operations shmem_symlink_inline_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001146shmem_write_begin(struct file *file, struct address_space *mapping,
1147 loff_t pos, unsigned len, unsigned flags,
1148 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Nick Piggin800d15a2007-10-16 01:25:03 -07001150 struct inode *inode = mapping->host;
1151 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin800d15a2007-10-16 01:25:03 -07001152 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1153}
1154
1155static int
1156shmem_write_end(struct file *file, struct address_space *mapping,
1157 loff_t pos, unsigned len, unsigned copied,
1158 struct page *page, void *fsdata)
1159{
1160 struct inode *inode = mapping->host;
1161
Hugh Dickinsd3602442008-02-04 22:28:44 -08001162 if (pos + copied > inode->i_size)
1163 i_size_write(inode, pos + copied);
1164
Nick Piggin800d15a2007-10-16 01:25:03 -07001165 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001166 unlock_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07001167 page_cache_release(page);
1168
Nick Piggin800d15a2007-10-16 01:25:03 -07001169 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1173{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001174 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001176 pgoff_t index;
1177 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001178 enum sgp_type sgp = SGP_READ;
1179
1180 /*
1181 * Might this read be for a stacking filesystem? Then when reading
1182 * holes of a sparse file, we actually need to allocate those pages,
1183 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1184 */
1185 if (segment_eq(get_fs(), KERNEL_DS))
1186 sgp = SGP_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 index = *ppos >> PAGE_CACHE_SHIFT;
1189 offset = *ppos & ~PAGE_CACHE_MASK;
1190
1191 for (;;) {
1192 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001193 pgoff_t end_index;
1194 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 loff_t i_size = i_size_read(inode);
1196
1197 end_index = i_size >> PAGE_CACHE_SHIFT;
1198 if (index > end_index)
1199 break;
1200 if (index == end_index) {
1201 nr = i_size & ~PAGE_CACHE_MASK;
1202 if (nr <= offset)
1203 break;
1204 }
1205
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001206 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (desc->error) {
1208 if (desc->error == -EINVAL)
1209 desc->error = 0;
1210 break;
1211 }
Hugh Dickinsd3602442008-02-04 22:28:44 -08001212 if (page)
1213 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /*
1216 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001217 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
1219 nr = PAGE_CACHE_SIZE;
1220 i_size = i_size_read(inode);
1221 end_index = i_size >> PAGE_CACHE_SHIFT;
1222 if (index == end_index) {
1223 nr = i_size & ~PAGE_CACHE_MASK;
1224 if (nr <= offset) {
1225 if (page)
1226 page_cache_release(page);
1227 break;
1228 }
1229 }
1230 nr -= offset;
1231
1232 if (page) {
1233 /*
1234 * If users can be writing to this page using arbitrary
1235 * virtual addresses, take care about potential aliasing
1236 * before reading the page on the kernel side.
1237 */
1238 if (mapping_writably_mapped(mapping))
1239 flush_dcache_page(page);
1240 /*
1241 * Mark the page accessed if we read the beginning.
1242 */
1243 if (!offset)
1244 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07001245 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 page = ZERO_PAGE(0);
Nick Pigginb5810032005-10-29 18:16:12 -07001247 page_cache_get(page);
1248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /*
1251 * Ok, we have the page, and it's up-to-date, so
1252 * now we can copy it to user space...
1253 *
1254 * The actor routine returns how many bytes were actually used..
1255 * NOTE! This may not be the same as how much of a user buffer
1256 * we filled up (we may be padding etc), so we can only update
1257 * "pos" here (the actor routine has to update the user buffer
1258 * pointers and the remaining count).
1259 */
1260 ret = actor(desc, page, offset, nr);
1261 offset += ret;
1262 index += offset >> PAGE_CACHE_SHIFT;
1263 offset &= ~PAGE_CACHE_MASK;
1264
1265 page_cache_release(page);
1266 if (ret != nr || !desc->count)
1267 break;
1268
1269 cond_resched();
1270 }
1271
1272 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1273 file_accessed(filp);
1274}
1275
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001276static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1277 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001279 struct file *filp = iocb->ki_filp;
1280 ssize_t retval;
1281 unsigned long seg;
1282 size_t count;
1283 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001285 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1286 if (retval)
1287 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001289 for (seg = 0; seg < nr_segs; seg++) {
1290 read_descriptor_t desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001292 desc.written = 0;
1293 desc.arg.buf = iov[seg].iov_base;
1294 desc.count = iov[seg].iov_len;
1295 if (desc.count == 0)
1296 continue;
1297 desc.error = 0;
1298 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1299 retval += desc.written;
1300 if (desc.error) {
1301 retval = retval ?: desc.error;
1302 break;
1303 }
1304 if (desc.count > 0)
1305 break;
1306 }
1307 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Hugh Dickins708e3502011-07-25 17:12:32 -07001310static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1311 struct pipe_inode_info *pipe, size_t len,
1312 unsigned int flags)
1313{
1314 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001315 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07001316 unsigned int loff, nr_pages, req_pages;
1317 struct page *pages[PIPE_DEF_BUFFERS];
1318 struct partial_page partial[PIPE_DEF_BUFFERS];
1319 struct page *page;
1320 pgoff_t index, end_index;
1321 loff_t isize, left;
1322 int error, page_nr;
1323 struct splice_pipe_desc spd = {
1324 .pages = pages,
1325 .partial = partial,
1326 .flags = flags,
1327 .ops = &page_cache_pipe_buf_ops,
1328 .spd_release = spd_release_page,
1329 };
1330
Hugh Dickins71f0e072011-07-25 17:12:33 -07001331 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001332 if (unlikely(*ppos >= isize))
1333 return 0;
1334
1335 left = isize - *ppos;
1336 if (unlikely(left < len))
1337 len = left;
1338
1339 if (splice_grow_spd(pipe, &spd))
1340 return -ENOMEM;
1341
1342 index = *ppos >> PAGE_CACHE_SHIFT;
1343 loff = *ppos & ~PAGE_CACHE_MASK;
1344 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1345 nr_pages = min(req_pages, pipe->buffers);
1346
Hugh Dickins708e3502011-07-25 17:12:32 -07001347 spd.nr_pages = find_get_pages_contig(mapping, index,
1348 nr_pages, spd.pages);
1349 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07001350 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001351
Hugh Dickins708e3502011-07-25 17:12:32 -07001352 while (spd.nr_pages < nr_pages) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001353 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1354 if (error)
1355 break;
1356 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07001357 spd.pages[spd.nr_pages++] = page;
1358 index++;
1359 }
1360
Hugh Dickins708e3502011-07-25 17:12:32 -07001361 index = *ppos >> PAGE_CACHE_SHIFT;
1362 nr_pages = spd.nr_pages;
1363 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001364
Hugh Dickins708e3502011-07-25 17:12:32 -07001365 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1366 unsigned int this_len;
1367
1368 if (!len)
1369 break;
1370
Hugh Dickins708e3502011-07-25 17:12:32 -07001371 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1372 page = spd.pages[page_nr];
1373
Hugh Dickins71f0e072011-07-25 17:12:33 -07001374 if (!PageUptodate(page) || page->mapping != mapping) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001375 error = shmem_getpage(inode, index, &page,
1376 SGP_CACHE, NULL);
1377 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07001378 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001379 unlock_page(page);
1380 page_cache_release(spd.pages[page_nr]);
1381 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07001382 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07001383
1384 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001385 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1386 if (unlikely(!isize || index > end_index))
1387 break;
1388
Hugh Dickins708e3502011-07-25 17:12:32 -07001389 if (end_index == index) {
1390 unsigned int plen;
1391
Hugh Dickins708e3502011-07-25 17:12:32 -07001392 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1393 if (plen <= loff)
1394 break;
1395
Hugh Dickins708e3502011-07-25 17:12:32 -07001396 this_len = min(this_len, plen - loff);
1397 len = this_len;
1398 }
1399
1400 spd.partial[page_nr].offset = loff;
1401 spd.partial[page_nr].len = this_len;
1402 len -= this_len;
1403 loff = 0;
1404 spd.nr_pages++;
1405 index++;
1406 }
1407
Hugh Dickins708e3502011-07-25 17:12:32 -07001408 while (page_nr < nr_pages)
1409 page_cache_release(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07001410
1411 if (spd.nr_pages)
1412 error = splice_to_pipe(pipe, &spd);
1413
1414 splice_shrink_spd(pipe, &spd);
1415
1416 if (error > 0) {
1417 *ppos += error;
1418 file_accessed(in);
1419 }
1420 return error;
1421}
1422
David Howells726c3342006-06-23 02:02:58 -07001423static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
David Howells726c3342006-06-23 02:02:58 -07001425 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 buf->f_type = TMPFS_MAGIC;
1428 buf->f_bsize = PAGE_CACHE_SIZE;
1429 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001430 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001432 buf->f_bavail =
1433 buf->f_bfree = sbinfo->max_blocks -
1434 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001435 }
1436 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 buf->f_files = sbinfo->max_inodes;
1438 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440 /* else leave those fields 0 like simple_statfs */
1441 return 0;
1442}
1443
1444/*
1445 * File creation. Allocate an inode, and we're done..
1446 */
1447static int
1448shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1449{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001450 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int error = -ENOSPC;
1452
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001453 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (inode) {
Eric Paris2a7dba32011-02-01 11:05:39 -05001455 error = security_inode_init_security(inode, dir,
1456 &dentry->d_name, NULL,
1457 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001458 if (error) {
1459 if (error != -EOPNOTSUPP) {
1460 iput(inode);
1461 return error;
1462 }
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001463 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001464#ifdef CONFIG_TMPFS_POSIX_ACL
1465 error = generic_acl_init(inode, dir);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001466 if (error) {
1467 iput(inode);
1468 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001469 }
Al Viro718deb62009-12-16 19:35:36 -05001470#else
1471 error = 0;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001472#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 dir->i_size += BOGO_DIRENT_SIZE;
1474 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1475 d_instantiate(dentry, inode);
1476 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478 return error;
1479}
1480
1481static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1482{
1483 int error;
1484
1485 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1486 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07001487 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return 0;
1489}
1490
1491static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1492 struct nameidata *nd)
1493{
1494 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1495}
1496
1497/*
1498 * Link a file..
1499 */
1500static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1501{
1502 struct inode *inode = old_dentry->d_inode;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001503 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 /*
1506 * No ordinary (disk based) filesystem counts links as inodes;
1507 * but each new link needs a new dentry, pinning lowmem, and
1508 * tmpfs dentries cannot be pruned until they are unlinked.
1509 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001510 ret = shmem_reserve_inode(inode->i_sb);
1511 if (ret)
1512 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 dir->i_size += BOGO_DIRENT_SIZE;
1515 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07001516 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04001517 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 dget(dentry); /* Extra pinning count for the created dentry */
1519 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001520out:
1521 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
1524static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1525{
1526 struct inode *inode = dentry->d_inode;
1527
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001528 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1529 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 dir->i_size -= BOGO_DIRENT_SIZE;
1532 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001533 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 dput(dentry); /* Undo the count from "create" - this does all the work */
1535 return 0;
1536}
1537
1538static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1539{
1540 if (!simple_empty(dentry))
1541 return -ENOTEMPTY;
1542
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001543 drop_nlink(dentry->d_inode);
1544 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return shmem_unlink(dir, dentry);
1546}
1547
1548/*
1549 * The VFS layer already does all the dentry stuff for rename,
1550 * we just have to decrement the usage count for the target if
1551 * it exists so that the VFS layer correctly free's it when it
1552 * gets overwritten.
1553 */
1554static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1555{
1556 struct inode *inode = old_dentry->d_inode;
1557 int they_are_dirs = S_ISDIR(inode->i_mode);
1558
1559 if (!simple_empty(new_dentry))
1560 return -ENOTEMPTY;
1561
1562 if (new_dentry->d_inode) {
1563 (void) shmem_unlink(new_dir, new_dentry);
1564 if (they_are_dirs)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001565 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001567 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07001568 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570
1571 old_dir->i_size -= BOGO_DIRENT_SIZE;
1572 new_dir->i_size += BOGO_DIRENT_SIZE;
1573 old_dir->i_ctime = old_dir->i_mtime =
1574 new_dir->i_ctime = new_dir->i_mtime =
1575 inode->i_ctime = CURRENT_TIME;
1576 return 0;
1577}
1578
1579static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1580{
1581 int error;
1582 int len;
1583 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07001584 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 char *kaddr;
1586 struct shmem_inode_info *info;
1587
1588 len = strlen(symname) + 1;
1589 if (len > PAGE_CACHE_SIZE)
1590 return -ENAMETOOLONG;
1591
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001592 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (!inode)
1594 return -ENOSPC;
1595
Eric Paris2a7dba32011-02-01 11:05:39 -05001596 error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
1597 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001598 if (error) {
1599 if (error != -EOPNOTSUPP) {
1600 iput(inode);
1601 return error;
1602 }
1603 error = 0;
1604 }
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 info = SHMEM_I(inode);
1607 inode->i_size = len-1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07001608 if (len <= SHMEM_SYMLINK_INLINE_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /* do it inline */
Eric Parisb09e0fa2011-05-24 17:12:39 -07001610 memcpy(info->inline_symlink, symname, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 inode->i_op = &shmem_symlink_inline_operations;
1612 } else {
1613 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1614 if (error) {
1615 iput(inode);
1616 return error;
1617 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07001618 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 inode->i_op = &shmem_symlink_inode_operations;
1620 kaddr = kmap_atomic(page, KM_USER0);
1621 memcpy(kaddr, symname, len);
1622 kunmap_atomic(kaddr, KM_USER0);
1623 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001624 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 page_cache_release(page);
1626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 dir->i_size += BOGO_DIRENT_SIZE;
1628 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1629 d_instantiate(dentry, inode);
1630 dget(dentry);
1631 return 0;
1632}
1633
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001634static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Eric Parisb09e0fa2011-05-24 17:12:39 -07001636 nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001637 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001640static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001643 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1644 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
Hugh Dickinsd3602442008-02-04 22:28:44 -08001645 if (page)
1646 unlock_page(page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001647 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001650static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 if (!IS_ERR(nd_get_link(nd))) {
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001653 struct page *page = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 kunmap(page);
1655 mark_page_accessed(page);
1656 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
1658}
1659
Eric Parisb09e0fa2011-05-24 17:12:39 -07001660#ifdef CONFIG_TMPFS_XATTR
1661/*
1662 * Superblocks without xattr inode operations may get some security.* xattr
1663 * support from the LSM "for free". As soon as we have any other xattrs
1664 * like ACLs, we also need to implement the security.* handlers at
1665 * filesystem level, though.
1666 */
1667
1668static int shmem_xattr_get(struct dentry *dentry, const char *name,
1669 void *buffer, size_t size)
1670{
1671 struct shmem_inode_info *info;
1672 struct shmem_xattr *xattr;
1673 int ret = -ENODATA;
1674
1675 info = SHMEM_I(dentry->d_inode);
1676
1677 spin_lock(&info->lock);
1678 list_for_each_entry(xattr, &info->xattr_list, list) {
1679 if (strcmp(name, xattr->name))
1680 continue;
1681
1682 ret = xattr->size;
1683 if (buffer) {
1684 if (size < xattr->size)
1685 ret = -ERANGE;
1686 else
1687 memcpy(buffer, xattr->value, xattr->size);
1688 }
1689 break;
1690 }
1691 spin_unlock(&info->lock);
1692 return ret;
1693}
1694
1695static int shmem_xattr_set(struct dentry *dentry, const char *name,
1696 const void *value, size_t size, int flags)
1697{
1698 struct inode *inode = dentry->d_inode;
1699 struct shmem_inode_info *info = SHMEM_I(inode);
1700 struct shmem_xattr *xattr;
1701 struct shmem_xattr *new_xattr = NULL;
1702 size_t len;
1703 int err = 0;
1704
1705 /* value == NULL means remove */
1706 if (value) {
1707 /* wrap around? */
1708 len = sizeof(*new_xattr) + size;
1709 if (len <= sizeof(*new_xattr))
1710 return -ENOMEM;
1711
1712 new_xattr = kmalloc(len, GFP_KERNEL);
1713 if (!new_xattr)
1714 return -ENOMEM;
1715
1716 new_xattr->name = kstrdup(name, GFP_KERNEL);
1717 if (!new_xattr->name) {
1718 kfree(new_xattr);
1719 return -ENOMEM;
1720 }
1721
1722 new_xattr->size = size;
1723 memcpy(new_xattr->value, value, size);
1724 }
1725
1726 spin_lock(&info->lock);
1727 list_for_each_entry(xattr, &info->xattr_list, list) {
1728 if (!strcmp(name, xattr->name)) {
1729 if (flags & XATTR_CREATE) {
1730 xattr = new_xattr;
1731 err = -EEXIST;
1732 } else if (new_xattr) {
1733 list_replace(&xattr->list, &new_xattr->list);
1734 } else {
1735 list_del(&xattr->list);
1736 }
1737 goto out;
1738 }
1739 }
1740 if (flags & XATTR_REPLACE) {
1741 xattr = new_xattr;
1742 err = -ENODATA;
1743 } else {
1744 list_add(&new_xattr->list, &info->xattr_list);
1745 xattr = NULL;
1746 }
1747out:
1748 spin_unlock(&info->lock);
1749 if (xattr)
1750 kfree(xattr->name);
1751 kfree(xattr);
1752 return err;
1753}
1754
Eric Parisb09e0fa2011-05-24 17:12:39 -07001755static const struct xattr_handler *shmem_xattr_handlers[] = {
1756#ifdef CONFIG_TMPFS_POSIX_ACL
1757 &generic_acl_access_handler,
1758 &generic_acl_default_handler,
1759#endif
1760 NULL
1761};
1762
1763static int shmem_xattr_validate(const char *name)
1764{
1765 struct { const char *prefix; size_t len; } arr[] = {
1766 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1767 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1768 };
1769 int i;
1770
1771 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1772 size_t preflen = arr[i].len;
1773 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1774 if (!name[preflen])
1775 return -EINVAL;
1776 return 0;
1777 }
1778 }
1779 return -EOPNOTSUPP;
1780}
1781
1782static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1783 void *buffer, size_t size)
1784{
1785 int err;
1786
1787 /*
1788 * If this is a request for a synthetic attribute in the system.*
1789 * namespace use the generic infrastructure to resolve a handler
1790 * for it via sb->s_xattr.
1791 */
1792 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1793 return generic_getxattr(dentry, name, buffer, size);
1794
1795 err = shmem_xattr_validate(name);
1796 if (err)
1797 return err;
1798
1799 return shmem_xattr_get(dentry, name, buffer, size);
1800}
1801
1802static int shmem_setxattr(struct dentry *dentry, const char *name,
1803 const void *value, size_t size, int flags)
1804{
1805 int err;
1806
1807 /*
1808 * If this is a request for a synthetic attribute in the system.*
1809 * namespace use the generic infrastructure to resolve a handler
1810 * for it via sb->s_xattr.
1811 */
1812 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1813 return generic_setxattr(dentry, name, value, size, flags);
1814
1815 err = shmem_xattr_validate(name);
1816 if (err)
1817 return err;
1818
1819 if (size == 0)
1820 value = ""; /* empty EA, do not remove */
1821
1822 return shmem_xattr_set(dentry, name, value, size, flags);
1823
1824}
1825
1826static int shmem_removexattr(struct dentry *dentry, const char *name)
1827{
1828 int err;
1829
1830 /*
1831 * If this is a request for a synthetic attribute in the system.*
1832 * namespace use the generic infrastructure to resolve a handler
1833 * for it via sb->s_xattr.
1834 */
1835 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1836 return generic_removexattr(dentry, name);
1837
1838 err = shmem_xattr_validate(name);
1839 if (err)
1840 return err;
1841
1842 return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
1843}
1844
1845static bool xattr_is_trusted(const char *name)
1846{
1847 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1848}
1849
1850static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
1851{
1852 bool trusted = capable(CAP_SYS_ADMIN);
1853 struct shmem_xattr *xattr;
1854 struct shmem_inode_info *info;
1855 size_t used = 0;
1856
1857 info = SHMEM_I(dentry->d_inode);
1858
1859 spin_lock(&info->lock);
1860 list_for_each_entry(xattr, &info->xattr_list, list) {
1861 size_t len;
1862
1863 /* skip "trusted." attributes for unprivileged callers */
1864 if (!trusted && xattr_is_trusted(xattr->name))
1865 continue;
1866
1867 len = strlen(xattr->name) + 1;
1868 used += len;
1869 if (buffer) {
1870 if (size < used) {
1871 used = -ERANGE;
1872 break;
1873 }
1874 memcpy(buffer, xattr->name, len);
1875 buffer += len;
1876 }
1877 }
1878 spin_unlock(&info->lock);
1879
1880 return used;
1881}
1882#endif /* CONFIG_TMPFS_XATTR */
1883
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001884static const struct inode_operations shmem_symlink_inline_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 .readlink = generic_readlink,
1886 .follow_link = shmem_follow_link_inline,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001887#ifdef CONFIG_TMPFS_XATTR
1888 .setxattr = shmem_setxattr,
1889 .getxattr = shmem_getxattr,
1890 .listxattr = shmem_listxattr,
1891 .removexattr = shmem_removexattr,
1892#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893};
1894
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001895static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 .readlink = generic_readlink,
1897 .follow_link = shmem_follow_link,
1898 .put_link = shmem_put_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001899#ifdef CONFIG_TMPFS_XATTR
1900 .setxattr = shmem_setxattr,
1901 .getxattr = shmem_getxattr,
1902 .listxattr = shmem_listxattr,
1903 .removexattr = shmem_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001904#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07001905};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001906
David M. Grimes91828a42006-10-17 00:09:45 -07001907static struct dentry *shmem_get_parent(struct dentry *child)
1908{
1909 return ERR_PTR(-ESTALE);
1910}
1911
1912static int shmem_match(struct inode *ino, void *vfh)
1913{
1914 __u32 *fh = vfh;
1915 __u64 inum = fh[2];
1916 inum = (inum << 32) | fh[1];
1917 return ino->i_ino == inum && fh[0] == ino->i_generation;
1918}
1919
Christoph Hellwig480b1162007-10-21 16:42:13 -07001920static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
1921 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07001922{
David M. Grimes91828a42006-10-17 00:09:45 -07001923 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07001924 struct dentry *dentry = NULL;
1925 u64 inum = fid->raw[2];
1926 inum = (inum << 32) | fid->raw[1];
David M. Grimes91828a42006-10-17 00:09:45 -07001927
Christoph Hellwig480b1162007-10-21 16:42:13 -07001928 if (fh_len < 3)
1929 return NULL;
1930
1931 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
1932 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07001933 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07001934 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07001935 iput(inode);
1936 }
1937
Christoph Hellwig480b1162007-10-21 16:42:13 -07001938 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07001939}
1940
1941static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
1942 int connectable)
1943{
1944 struct inode *inode = dentry->d_inode;
1945
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301946 if (*len < 3) {
1947 *len = 3;
David M. Grimes91828a42006-10-17 00:09:45 -07001948 return 255;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301949 }
David M. Grimes91828a42006-10-17 00:09:45 -07001950
Al Viro1d3382cb2010-10-23 15:19:20 -04001951 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07001952 /* Unfortunately insert_inode_hash is not idempotent,
1953 * so as we hash inodes here rather than at creation
1954 * time, we need a lock to ensure we only try
1955 * to do it once
1956 */
1957 static DEFINE_SPINLOCK(lock);
1958 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04001959 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07001960 __insert_inode_hash(inode,
1961 inode->i_ino + inode->i_generation);
1962 spin_unlock(&lock);
1963 }
1964
1965 fh[0] = inode->i_generation;
1966 fh[1] = inode->i_ino;
1967 fh[2] = ((__u64)inode->i_ino) >> 32;
1968
1969 *len = 3;
1970 return 1;
1971}
1972
Christoph Hellwig39655162007-10-21 16:42:17 -07001973static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07001974 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07001975 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07001976 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07001977};
1978
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001979static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
1980 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 char *this_char, *value, *rest;
1983
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00001984 while (options != NULL) {
1985 this_char = options;
1986 for (;;) {
1987 /*
1988 * NUL-terminate this option: unfortunately,
1989 * mount options form a comma-separated list,
1990 * but mpol's nodelist may also contain commas.
1991 */
1992 options = strchr(options, ',');
1993 if (options == NULL)
1994 break;
1995 options++;
1996 if (!isdigit(*options)) {
1997 options[-1] = '\0';
1998 break;
1999 }
2000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (!*this_char)
2002 continue;
2003 if ((value = strchr(this_char,'=')) != NULL) {
2004 *value++ = 0;
2005 } else {
2006 printk(KERN_ERR
2007 "tmpfs: No value for mount option '%s'\n",
2008 this_char);
2009 return 1;
2010 }
2011
2012 if (!strcmp(this_char,"size")) {
2013 unsigned long long size;
2014 size = memparse(value,&rest);
2015 if (*rest == '%') {
2016 size <<= PAGE_SHIFT;
2017 size *= totalram_pages;
2018 do_div(size, 100);
2019 rest++;
2020 }
2021 if (*rest)
2022 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002023 sbinfo->max_blocks =
2024 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002026 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (*rest)
2028 goto bad_val;
2029 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002030 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (*rest)
2032 goto bad_val;
2033 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002034 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002036 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (*rest)
2038 goto bad_val;
2039 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002040 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002042 sbinfo->uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (*rest)
2044 goto bad_val;
2045 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002046 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002048 sbinfo->gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (*rest)
2050 goto bad_val;
Robin Holt7339ff82006-01-14 13:20:48 -08002051 } else if (!strcmp(this_char,"mpol")) {
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002052 if (mpol_parse_str(value, &sbinfo->mpol, 1))
Robin Holt7339ff82006-01-14 13:20:48 -08002053 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 } else {
2055 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2056 this_char);
2057 return 1;
2058 }
2059 }
2060 return 0;
2061
2062bad_val:
2063 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2064 value, this_char);
2065 return 1;
2066
2067}
2068
2069static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2070{
2071 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002072 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002073 unsigned long inodes;
2074 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002076 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002077 return error;
2078
2079 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002080 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07002081 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002082 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002083 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002084 goto out;
2085 /*
2086 * Those tests also disallow limited->unlimited while any are in
2087 * use, so i_blocks will always be zero when max_blocks is zero;
2088 * but we must separately disallow unlimited->limited, because
2089 * in that case we have no record of how much is already in use.
2090 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002091 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002092 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002093 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002094 goto out;
2095
2096 error = 0;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002097 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002098 sbinfo->max_inodes = config.max_inodes;
2099 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002100
2101 mpol_put(sbinfo->mpol);
2102 sbinfo->mpol = config.mpol; /* transfers initial ref */
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002103out:
2104 spin_unlock(&sbinfo->stat_lock);
2105 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002107
2108static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2109{
2110 struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2111
2112 if (sbinfo->max_blocks != shmem_default_max_blocks())
2113 seq_printf(seq, ",size=%luk",
2114 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2115 if (sbinfo->max_inodes != shmem_default_max_inodes())
2116 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2117 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2118 seq_printf(seq, ",mode=%03o", sbinfo->mode);
2119 if (sbinfo->uid != 0)
2120 seq_printf(seq, ",uid=%u", sbinfo->uid);
2121 if (sbinfo->gid != 0)
2122 seq_printf(seq, ",gid=%u", sbinfo->gid);
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002123 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002124 return 0;
2125}
2126#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128static void shmem_put_super(struct super_block *sb)
2129{
Hugh Dickins602586a2010-08-17 15:23:56 -07002130 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2131
2132 percpu_counter_destroy(&sbinfo->used_blocks);
2133 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 sb->s_fs_info = NULL;
2135}
2136
Kay Sievers2b2af542009-04-30 15:23:42 +02002137int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 struct inode *inode;
2140 struct dentry *root;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002141 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002142 int err = -ENOMEM;
2143
2144 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07002145 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002146 L1_CACHE_BYTES), GFP_KERNEL);
2147 if (!sbinfo)
2148 return -ENOMEM;
2149
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002150 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11002151 sbinfo->uid = current_fsuid();
2152 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002153 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002155#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 /*
2157 * Per default we only allow half of the physical ram per
2158 * tmpfs instance, limiting inodes to one per page of lowmem;
2159 * but the internal instance is left unlimited.
2160 */
2161 if (!(sb->s_flags & MS_NOUSER)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002162 sbinfo->max_blocks = shmem_default_max_blocks();
2163 sbinfo->max_inodes = shmem_default_max_inodes();
2164 if (shmem_parse_options(data, sbinfo, false)) {
2165 err = -EINVAL;
2166 goto failed;
2167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
David M. Grimes91828a42006-10-17 00:09:45 -07002169 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170#else
2171 sb->s_flags |= MS_NOUSER;
2172#endif
2173
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002174 spin_lock_init(&sbinfo->stat_lock);
Hugh Dickins602586a2010-08-17 15:23:56 -07002175 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2176 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002177 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002178
Hugh Dickins285b2c42011-08-03 16:21:20 -07002179 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 sb->s_blocksize = PAGE_CACHE_SIZE;
2181 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2182 sb->s_magic = TMPFS_MAGIC;
2183 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01002184 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002185#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002186 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002187#endif
2188#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002189 sb->s_flags |= MS_POSIXACL;
2190#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002191
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002192 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (!inode)
2194 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002195 inode->i_uid = sbinfo->uid;
2196 inode->i_gid = sbinfo->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 root = d_alloc_root(inode);
2198 if (!root)
2199 goto failed_iput;
2200 sb->s_root = root;
2201 return 0;
2202
2203failed_iput:
2204 iput(inode);
2205failed:
2206 shmem_put_super(sb);
2207 return err;
2208}
2209
Pekka Enbergfcc234f2006-03-22 00:08:13 -08002210static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212static struct inode *shmem_alloc_inode(struct super_block *sb)
2213{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002214 struct shmem_inode_info *info;
2215 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2216 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002218 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219}
2220
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002221static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002222{
2223 struct inode *inode = container_of(head, struct inode, i_rcu);
2224 INIT_LIST_HEAD(&inode->i_dentry);
2225 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2226}
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228static void shmem_destroy_inode(struct inode *inode)
2229{
2230 if ((inode->i_mode & S_IFMT) == S_IFREG) {
2231 /* only struct inode is valid if it's an inline symlink */
2232 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2233 }
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002234 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002237static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002239 struct shmem_inode_info *info = foo;
2240 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002243static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
2245 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2246 sizeof(struct shmem_inode_info),
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002247 0, SLAB_PANIC, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return 0;
2249}
2250
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002251static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002253 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002256static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08002258 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07002260 .write_begin = shmem_write_begin,
2261 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262#endif
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07002263 .migratepage = migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +02002264 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265};
2266
Helge Deller15ad7cd2006-12-06 20:40:36 -08002267static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 .mmap = shmem_mmap,
2269#ifdef CONFIG_TMPFS
2270 .llseek = generic_file_llseek,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002271 .read = do_sync_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002272 .write = do_sync_write,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002273 .aio_read = shmem_file_aio_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002274 .aio_write = generic_file_aio_write,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02002275 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07002276 .splice_read = shmem_file_splice_read,
Hugh Dickinsae9764162007-06-04 10:00:39 +02002277 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278#endif
2279};
2280
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002281static const struct inode_operations shmem_inode_operations = {
Hugh Dickins94c1e622011-06-27 16:18:03 -07002282 .setattr = shmem_setattr,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08002283 .truncate_range = shmem_truncate_range,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002284#ifdef CONFIG_TMPFS_XATTR
2285 .setxattr = shmem_setxattr,
2286 .getxattr = shmem_getxattr,
2287 .listxattr = shmem_listxattr,
2288 .removexattr = shmem_removexattr,
2289#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290};
2291
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002292static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293#ifdef CONFIG_TMPFS
2294 .create = shmem_create,
2295 .lookup = simple_lookup,
2296 .link = shmem_link,
2297 .unlink = shmem_unlink,
2298 .symlink = shmem_symlink,
2299 .mkdir = shmem_mkdir,
2300 .rmdir = shmem_rmdir,
2301 .mknod = shmem_mknod,
2302 .rename = shmem_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002304#ifdef CONFIG_TMPFS_XATTR
2305 .setxattr = shmem_setxattr,
2306 .getxattr = shmem_getxattr,
2307 .listxattr = shmem_listxattr,
2308 .removexattr = shmem_removexattr,
2309#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002310#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002311 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002312#endif
2313};
2314
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002315static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07002316#ifdef CONFIG_TMPFS_XATTR
2317 .setxattr = shmem_setxattr,
2318 .getxattr = shmem_getxattr,
2319 .listxattr = shmem_listxattr,
2320 .removexattr = shmem_removexattr,
2321#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002322#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002323 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002324#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325};
2326
Hugh Dickins759b9772007-03-05 00:30:28 -08002327static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 .alloc_inode = shmem_alloc_inode,
2329 .destroy_inode = shmem_destroy_inode,
2330#ifdef CONFIG_TMPFS
2331 .statfs = shmem_statfs,
2332 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002333 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334#endif
Al Viro1f895f72010-06-05 19:10:41 -04002335 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 .drop_inode = generic_delete_inode,
2337 .put_super = shmem_put_super,
2338};
2339
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002340static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002341 .fault = shmem_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342#ifdef CONFIG_NUMA
2343 .set_policy = shmem_set_policy,
2344 .get_policy = shmem_get_policy,
2345#endif
2346};
2347
Al Viro3c26ff62010-07-25 11:46:36 +04002348static struct dentry *shmem_mount(struct file_system_type *fs_type,
2349 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Al Viro3c26ff62010-07-25 11:46:36 +04002351 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002354static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 .owner = THIS_MODULE,
2356 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002357 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 .kill_sb = kill_litter_super,
2359};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002361int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
2363 int error;
2364
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002365 error = bdi_init(&shmem_backing_dev_info);
2366 if (error)
2367 goto out4;
2368
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002369 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (error)
2371 goto out3;
2372
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002373 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (error) {
2375 printk(KERN_ERR "Could not register tmpfs\n");
2376 goto out2;
2377 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07002378
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002379 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2380 shmem_fs_type.name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (IS_ERR(shm_mnt)) {
2382 error = PTR_ERR(shm_mnt);
2383 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2384 goto out1;
2385 }
2386 return 0;
2387
2388out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002389 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002391 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392out3:
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002393 bdi_destroy(&shmem_backing_dev_info);
2394out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 shm_mnt = ERR_PTR(error);
2396 return error;
2397}
Matt Mackall853ac432009-01-06 14:40:20 -08002398
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002399#ifdef CONFIG_CGROUP_MEM_RES_CTLR
2400/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002401 * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002402 * @inode: the inode to be searched
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002403 * @index: the page offset to be searched
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002404 * @pagep: the pointer for the found page to be stored
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002405 * @swapp: the pointer for the found swap entry to be stored
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002406 *
2407 * If a page is found, refcount of it is incremented. Callers should handle
2408 * these refcount.
2409 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002410void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index,
2411 struct page **pagep, swp_entry_t *swapp)
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002412{
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002413 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002414 struct page *page = NULL;
2415 swp_entry_t swap = {0};
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002416
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002417 if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002418 goto out;
2419
2420 spin_lock(&info->lock);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002421#ifdef CONFIG_SWAP
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002422 swap = shmem_get_swap(info, index);
2423 if (swap.val)
2424 page = find_get_page(&swapper_space, swap.val);
Hugh Dickins285b2c42011-08-03 16:21:20 -07002425 else
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002426#endif
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002427 page = find_get_page(inode->i_mapping, index);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002428 spin_unlock(&info->lock);
2429out:
2430 *pagep = page;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002431 *swapp = swap;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002432}
2433#endif
2434
Matt Mackall853ac432009-01-06 14:40:20 -08002435#else /* !CONFIG_SHMEM */
2436
2437/*
2438 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2439 *
2440 * This is intended for small system where the benefits of the full
2441 * shmem code (swap-backed and resource-limited) are outweighed by
2442 * their complexity. On systems without swap this code should be
2443 * effectively equivalent, but much lighter weight.
2444 */
2445
2446#include <linux/ramfs.h>
2447
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002448static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08002449 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002450 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08002451 .kill_sb = kill_litter_super,
2452};
2453
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002454int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08002455{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002456 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08002457
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002458 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08002459 BUG_ON(IS_ERR(shm_mnt));
2460
2461 return 0;
2462}
2463
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002464int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08002465{
2466 return 0;
2467}
2468
Hugh Dickins3f96b792009-09-21 17:03:37 -07002469int shmem_lock(struct file *file, int lock, struct user_struct *user)
2470{
2471 return 0;
2472}
2473
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002474void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07002475{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002476 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07002477}
2478EXPORT_SYMBOL_GPL(shmem_truncate_range);
2479
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002480#ifdef CONFIG_CGROUP_MEM_RES_CTLR
2481/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002482 * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002483 * @inode: the inode to be searched
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002484 * @index: the page offset to be searched
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002485 * @pagep: the pointer for the found page to be stored
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002486 * @swapp: the pointer for the found swap entry to be stored
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002487 *
2488 * If a page is found, refcount of it is incremented. Callers should handle
2489 * these refcount.
2490 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002491void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index,
2492 struct page **pagep, swp_entry_t *swapp)
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002493{
2494 struct page *page = NULL;
2495
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002496 if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002497 goto out;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002498 page = find_get_page(inode->i_mapping, index);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002499out:
2500 *pagep = page;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002501 *swapp = (swp_entry_t){0};
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002502}
2503#endif
2504
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002505#define shmem_vm_ops generic_file_vm_ops
2506#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002507#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002508#define shmem_acct_size(flags, size) 0
2509#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08002510
2511#endif /* CONFIG_SHMEM */
2512
2513/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Randy Dunlap46711812008-03-19 17:00:41 -07002515/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 * shmem_file_setup - get an unlinked file living in tmpfs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 * @name: name for dentry (to be seen in /proc/<pid>/maps
2518 * @size: size to be set for the file
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002519 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 */
Sergei Trofimovich168f5ac2009-06-16 15:33:02 -07002521struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
2523 int error;
2524 struct file *file;
2525 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04002526 struct path path;
2527 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 struct qstr this;
2529
2530 if (IS_ERR(shm_mnt))
2531 return (void *)shm_mnt;
2532
Hugh Dickins285b2c42011-08-03 16:21:20 -07002533 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return ERR_PTR(-EINVAL);
2535
2536 if (shmem_acct_size(flags, size))
2537 return ERR_PTR(-ENOMEM);
2538
2539 error = -ENOMEM;
2540 this.name = name;
2541 this.len = strlen(name);
2542 this.hash = 0; /* will go */
2543 root = shm_mnt->mnt_root;
Al Viro2c48b9c2009-08-09 00:52:35 +04002544 path.dentry = d_alloc(root, &this);
2545 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 goto put_memory;
Al Viro2c48b9c2009-08-09 00:52:35 +04002547 path.mnt = mntget(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 error = -ENOSPC;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002550 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if (!inode)
Al Viro4b42af82009-08-05 18:25:56 +04002552 goto put_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Al Viro2c48b9c2009-08-09 00:52:35 +04002554 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 inode->i_size = size;
2556 inode->i_nlink = 0; /* It is unlinked */
Matt Mackall853ac432009-01-06 14:40:20 -08002557#ifndef CONFIG_MMU
2558 error = ramfs_nommu_expand_for_mapping(inode, size);
2559 if (error)
Al Viro4b42af82009-08-05 18:25:56 +04002560 goto put_dentry;
Matt Mackall853ac432009-01-06 14:40:20 -08002561#endif
Al Viro4b42af82009-08-05 18:25:56 +04002562
2563 error = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04002564 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04002565 &shmem_file_operations);
2566 if (!file)
2567 goto put_dentry;
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return file;
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571put_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04002572 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573put_memory:
2574 shmem_unacct_size(flags, size);
2575 return ERR_PTR(error);
2576}
Keith Packard395e0dd2008-06-20 00:08:06 -07002577EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Randy Dunlap46711812008-03-19 17:00:41 -07002579/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2582 */
2583int shmem_zero_setup(struct vm_area_struct *vma)
2584{
2585 struct file *file;
2586 loff_t size = vma->vm_end - vma->vm_start;
2587
2588 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2589 if (IS_ERR(file))
2590 return PTR_ERR(file);
2591
2592 if (vma->vm_file)
2593 fput(vma->vm_file);
2594 vma->vm_file = file;
2595 vma->vm_ops = &shmem_vm_ops;
Hugh Dickinsbee4c36a2011-03-22 16:33:43 -07002596 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return 0;
2598}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002599
2600/**
2601 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2602 * @mapping: the page's address_space
2603 * @index: the page index
2604 * @gfp: the page allocator flags to use if allocating
2605 *
2606 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2607 * with any new page allocations done using the specified allocation flags.
2608 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2609 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2610 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2611 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07002612 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2613 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002614 */
2615struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2616 pgoff_t index, gfp_t gfp)
2617{
Hugh Dickins68da9f02011-07-25 17:12:34 -07002618#ifdef CONFIG_SHMEM
2619 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002620 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07002621 int error;
2622
2623 BUG_ON(mapping->a_ops != &shmem_aops);
2624 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2625 if (error)
2626 page = ERR_PTR(error);
2627 else
2628 unlock_page(page);
2629 return page;
2630#else
2631 /*
2632 * The tiny !SHMEM case uses ramfs without swap
2633 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002634 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002635#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002636}
2637EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);