blob: 174f97188e8a665dbfab40b506b3646f6fc8acdb [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/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700265 * Like add_to_page_cache_locked, but error if expected item has gone.
266 */
267static int shmem_add_to_page_cache(struct page *page,
268 struct address_space *mapping,
269 pgoff_t index, gfp_t gfp, void *expected)
270{
271 int error;
272
273 VM_BUG_ON(!PageLocked(page));
274 VM_BUG_ON(!PageSwapBacked(page));
275
276 error = mem_cgroup_cache_charge(page, current->mm,
277 gfp & GFP_RECLAIM_MASK);
278 if (error)
279 goto out;
280 if (!expected)
281 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
282 if (!error) {
283 page_cache_get(page);
284 page->mapping = mapping;
285 page->index = index;
286
287 spin_lock_irq(&mapping->tree_lock);
288 if (!expected)
289 error = radix_tree_insert(&mapping->page_tree,
290 index, page);
291 else
292 error = shmem_radix_tree_replace(mapping, index,
293 expected, page);
294 if (!error) {
295 mapping->nrpages++;
296 __inc_zone_page_state(page, NR_FILE_PAGES);
297 __inc_zone_page_state(page, NR_SHMEM);
298 spin_unlock_irq(&mapping->tree_lock);
299 } else {
300 page->mapping = NULL;
301 spin_unlock_irq(&mapping->tree_lock);
302 page_cache_release(page);
303 }
304 if (!expected)
305 radix_tree_preload_end();
306 }
307 if (error)
308 mem_cgroup_uncharge_cache_page(page);
309out:
310 return error;
311}
312
313/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700314 * Like find_get_pages, but collecting swap entries as well as pages.
315 */
316static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
317 pgoff_t start, unsigned int nr_pages,
318 struct page **pages, pgoff_t *indices)
319{
320 unsigned int i;
321 unsigned int ret;
322 unsigned int nr_found;
323
324 rcu_read_lock();
325restart:
326 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
327 (void ***)pages, indices, start, nr_pages);
328 ret = 0;
329 for (i = 0; i < nr_found; i++) {
330 struct page *page;
331repeat:
332 page = radix_tree_deref_slot((void **)pages[i]);
333 if (unlikely(!page))
334 continue;
335 if (radix_tree_exception(page)) {
336 if (radix_tree_exceptional_entry(page))
337 goto export;
338 /* radix_tree_deref_retry(page) */
339 goto restart;
340 }
341 if (!page_cache_get_speculative(page))
342 goto repeat;
343
344 /* Has the page moved? */
345 if (unlikely(page != *((void **)pages[i]))) {
346 page_cache_release(page);
347 goto repeat;
348 }
349export:
350 indices[ret] = indices[i];
351 pages[ret] = page;
352 ret++;
353 }
354 if (unlikely(!ret && nr_found))
355 goto restart;
356 rcu_read_unlock();
357 return ret;
358}
359
360/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700361 * Lockless lookup of swap entry in radix tree, avoiding refcount on pages.
362 */
363static pgoff_t shmem_find_swap(struct address_space *mapping, void *radswap)
364{
365 void **slots[PAGEVEC_SIZE];
366 pgoff_t indices[PAGEVEC_SIZE];
367 unsigned int nr_found;
368
369restart:
370 nr_found = 1;
371 indices[0] = -1;
372 while (nr_found) {
373 pgoff_t index = indices[nr_found - 1] + 1;
374 unsigned int i;
375
376 rcu_read_lock();
377 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
378 slots, indices, index, PAGEVEC_SIZE);
379 for (i = 0; i < nr_found; i++) {
380 void *item = radix_tree_deref_slot(slots[i]);
381 if (radix_tree_deref_retry(item)) {
382 rcu_read_unlock();
383 goto restart;
384 }
385 if (item == radswap) {
386 rcu_read_unlock();
387 return indices[i];
388 }
389 }
390 rcu_read_unlock();
391 cond_resched();
392 }
393 return -1;
394}
395
396/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700397 * Remove swap entry from radix tree, free the swap and its page cache.
398 */
399static int shmem_free_swap(struct address_space *mapping,
400 pgoff_t index, void *radswap)
401{
402 int error;
403
404 spin_lock_irq(&mapping->tree_lock);
405 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
406 spin_unlock_irq(&mapping->tree_lock);
407 if (!error)
408 free_swap_and_cache(radix_to_swp_entry(radswap));
409 return error;
410}
411
412/*
413 * Pagevec may contain swap entries, so shuffle up pages before releasing.
414 */
415static void shmem_pagevec_release(struct pagevec *pvec)
416{
417 int i, j;
418
419 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
420 struct page *page = pvec->pages[i];
421 if (!radix_tree_exceptional_entry(page))
422 pvec->pages[j++] = page;
423 }
424 pvec->nr = j;
425 pagevec_release(pvec);
426}
427
428/*
429 * Remove range of pages and swap entries from radix tree, and free them.
430 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700431void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700433 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700435 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700436 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700437 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700438 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700439 pgoff_t indices[PAGEVEC_SIZE];
440 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700441 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700442 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700444 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
445
446 pagevec_init(&pvec, 0);
447 index = start;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700448 while (index <= end) {
449 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
450 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
451 pvec.pages, indices);
452 if (!pvec.nr)
453 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700454 mem_cgroup_uncharge_start();
455 for (i = 0; i < pagevec_count(&pvec); i++) {
456 struct page *page = pvec.pages[i];
457
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700458 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700459 if (index > end)
460 break;
461
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700462 if (radix_tree_exceptional_entry(page)) {
463 nr_swaps_freed += !shmem_free_swap(mapping,
464 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700465 continue;
466 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700467
468 if (!trylock_page(page))
469 continue;
470 if (page->mapping == mapping) {
471 VM_BUG_ON(PageWriteback(page));
472 truncate_inode_page(mapping, page);
473 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700474 unlock_page(page);
475 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700476 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700477 mem_cgroup_uncharge_end();
478 cond_resched();
479 index++;
480 }
481
482 if (partial) {
483 struct page *page = NULL;
484 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
485 if (page) {
486 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
487 set_page_dirty(page);
488 unlock_page(page);
489 page_cache_release(page);
490 }
491 }
492
493 index = start;
494 for ( ; ; ) {
495 cond_resched();
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700496 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
497 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
498 pvec.pages, indices);
499 if (!pvec.nr) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700500 if (index == start)
501 break;
502 index = start;
503 continue;
504 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700505 if (index == start && indices[0] > end) {
506 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700507 break;
508 }
509 mem_cgroup_uncharge_start();
510 for (i = 0; i < pagevec_count(&pvec); i++) {
511 struct page *page = pvec.pages[i];
512
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700513 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700514 if (index > end)
515 break;
516
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700517 if (radix_tree_exceptional_entry(page)) {
518 nr_swaps_freed += !shmem_free_swap(mapping,
519 index, page);
520 continue;
521 }
522
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700523 lock_page(page);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700524 if (page->mapping == mapping) {
525 VM_BUG_ON(PageWriteback(page));
526 truncate_inode_page(mapping, page);
527 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700528 unlock_page(page);
529 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700530 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700531 mem_cgroup_uncharge_end();
532 index++;
533 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 spin_lock(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700536 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 shmem_recalc_inode(inode);
538 spin_unlock(&info->lock);
539
Hugh Dickins285b2c42011-08-03 16:21:20 -0700540 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700542EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Hugh Dickins94c1e622011-06-27 16:18:03 -0700544static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 int error;
548
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200549 error = inode_change_ok(inode, attr);
550 if (error)
551 return error;
552
Hugh Dickins94c1e622011-06-27 16:18:03 -0700553 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
554 loff_t oldsize = inode->i_size;
555 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000556
Hugh Dickins94c1e622011-06-27 16:18:03 -0700557 if (newsize != oldsize) {
558 i_size_write(inode, newsize);
559 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
560 }
561 if (newsize < oldsize) {
562 loff_t holebegin = round_up(newsize, PAGE_SIZE);
563 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
564 shmem_truncate_range(inode, newsize, (loff_t)-1);
565 /* unmap again to remove racily COWed private pages */
566 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200570 setattr_copy(inode, attr);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700571#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200572 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100573 error = generic_acl_chmod(inode);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700574#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return error;
576}
577
Al Viro1f895f72010-06-05 19:10:41 -0400578static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct shmem_inode_info *info = SHMEM_I(inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700581 struct shmem_xattr *xattr, *nxattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000583 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 shmem_unacct_size(info->flags, inode->i_size);
585 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000586 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800588 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800590 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592 }
Eric Parisb09e0fa2011-05-24 17:12:39 -0700593
594 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
595 kfree(xattr->name);
596 kfree(xattr);
597 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700598 BUG_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800599 shmem_free_inode(inode->i_sb);
Al Viro1f895f72010-06-05 19:10:41 -0400600 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700603/*
604 * If swap found in inode, free it and move page from swapcache to filecache.
605 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700606static int shmem_unuse_inode(struct shmem_inode_info *info,
607 swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700609 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700610 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700611 pgoff_t index;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800612 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700614 radswap = swp_to_radix_entry(swap);
615 index = shmem_find_swap(mapping, radswap);
616 if (index == -1)
Hugh Dickins285b2c42011-08-03 16:21:20 -0700617 return 0;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800618
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800619 /*
620 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400621 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800622 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700623 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800624 */
625 if (shmem_swaplist.next != &info->swaplist)
626 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800627
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800628 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700629 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
630 * but also to hold up shmem_evict_inode(): so inode cannot be freed
631 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800632 */
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700633 error = shmem_add_to_page_cache(page, mapping, index,
634 GFP_NOWAIT, radswap);
Hugh Dickins778dd892011-05-11 15:13:37 -0700635 /* which does mem_cgroup_uncharge_cache_page on error */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700636
Hugh Dickins48f170f2011-07-25 17:12:37 -0700637 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700638 /*
639 * Truncation and eviction use free_swap_and_cache(), which
640 * only does trylock page: if we raced, best clean up here.
641 */
Hugh Dickins73b12622008-02-04 22:28:50 -0800642 delete_from_swap_cache(page);
643 set_page_dirty(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700644 if (!error) {
645 spin_lock(&info->lock);
646 info->swapped--;
647 spin_unlock(&info->lock);
648 swap_free(swap);
649 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800650 error = 1; /* not an error, but entry was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800652 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700656 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700658int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700660 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct shmem_inode_info *info;
662 int found = 0;
Hugh Dickins778dd892011-05-11 15:13:37 -0700663 int error;
664
665 /*
666 * Charge page using GFP_KERNEL while we can wait, before taking
667 * the shmem_swaplist_mutex which might hold up shmem_writepage().
668 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700669 * shmem_add_to_page_cache() will be called with GFP_NOWAIT.
Hugh Dickins778dd892011-05-11 15:13:37 -0700670 */
671 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
672 if (error)
673 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700674 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800676 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700677 list_for_each_safe(this, next, &shmem_swaplist) {
678 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700679 if (!info->swapped) {
680 spin_lock(&info->lock);
681 if (!info->swapped)
682 list_del_init(&info->swaplist);
683 spin_unlock(&info->lock);
684 }
685 if (info->swapped)
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700686 found = shmem_unuse_inode(info, swap, page);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800687 cond_resched();
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800688 if (found)
Hugh Dickins778dd892011-05-11 15:13:37 -0700689 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800691 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -0700692
Hugh Dickins778dd892011-05-11 15:13:37 -0700693 if (!found)
694 mem_cgroup_uncharge_cache_page(page);
695 if (found < 0)
696 error = found;
697out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800698 unlock_page(page);
699 page_cache_release(page);
Hugh Dickins778dd892011-05-11 15:13:37 -0700700 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703/*
704 * Move the page from the page cache to the swap cache.
705 */
706static int shmem_writepage(struct page *page, struct writeback_control *wbc)
707{
708 struct shmem_inode_info *info;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700709 swp_entry_t swap, oswap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 struct address_space *mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700711 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct inode *inode;
713
714 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 mapping = page->mapping;
716 index = page->index;
717 inode = mapping->host;
718 info = SHMEM_I(inode);
719 if (info->flags & VM_LOCKED)
720 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800721 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 goto redirty;
723
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800724 /*
725 * shmem_backing_dev_info's capabilities prevent regular writeback or
726 * sync from ever calling shmem_writepage; but a stacking filesystem
Hugh Dickins48f170f2011-07-25 17:12:37 -0700727 * might use ->writepage of its underlying filesystem, in which case
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800728 * tmpfs should write out to swap only in response to memory pressure,
Hugh Dickins48f170f2011-07-25 17:12:37 -0700729 * and not for the writeback threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800730 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700731 if (!wbc->for_reclaim) {
732 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
733 goto redirty;
734 }
Hugh Dickins285b2c42011-08-03 16:21:20 -0700735
736 /*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700737 * Disable even the toy swapping implementation, while we convert
738 * functions one by one to having swap entries in the radix tree.
Hugh Dickins285b2c42011-08-03 16:21:20 -0700739 */
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700740 if (index < ULONG_MAX)
Hugh Dickins285b2c42011-08-03 16:21:20 -0700741 goto redirty;
742
Hugh Dickins48f170f2011-07-25 17:12:37 -0700743 swap = get_swap_page();
744 if (!swap.val)
745 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800746
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700747 /*
748 * Add inode to shmem_unuse()'s list of swapped-out inodes,
749 * if it's not already there. Do it now because we cannot take
750 * mutex while holding spinlock, and must do so before the page
751 * is moved to swap cache, when its pagelock no longer protects
752 * the inode from eviction. But don't unlock the mutex until
753 * we've taken the spinlock, because shmem_unuse_inode() will
754 * prune a !swapped inode from the swaplist under both locks.
755 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700756 mutex_lock(&shmem_swaplist_mutex);
757 if (list_empty(&info->swaplist))
758 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 spin_lock(&info->lock);
Hugh Dickins48f170f2011-07-25 17:12:37 -0700761 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700762
Hugh Dickins285b2c42011-08-03 16:21:20 -0700763 oswap = shmem_get_swap(info, index);
764 if (oswap.val) {
Hugh Dickins48f170f2011-07-25 17:12:37 -0700765 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700766 free_swap_and_cache(oswap);
767 shmem_put_swap(info, index, (swp_entry_t){0});
768 info->swapped--;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800769 }
770 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Hugh Dickins48f170f2011-07-25 17:12:37 -0700772 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Minchan Kim4c73b1b2011-03-22 16:32:40 -0700773 delete_from_page_cache(page);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700774 shmem_put_swap(info, index, swap);
775 info->swapped++;
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800776 swap_shmem_alloc(swap);
Hugh Dickins826267c2011-05-28 13:14:09 -0700777 spin_unlock(&info->lock);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800778 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -0700779 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return 0;
781 }
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 spin_unlock(&info->lock);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700784 swapcache_free(swap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785redirty:
786 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800787 if (wbc->for_reclaim)
788 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
789 unlock_page(page);
790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793#ifdef CONFIG_NUMA
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800794#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700795static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800796{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700797 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800798
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700799 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700800 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800801
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700802 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800803
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700804 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800805}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700806
807static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
808{
809 struct mempolicy *mpol = NULL;
810 if (sbinfo->mpol) {
811 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
812 mpol = sbinfo->mpol;
813 mpol_get(mpol);
814 spin_unlock(&sbinfo->stat_lock);
815 }
816 return mpol;
817}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800818#endif /* CONFIG_TMPFS */
819
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700820static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
821 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700823 struct mempolicy mpol, *spol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 struct vm_area_struct pvma;
825
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700826 spol = mpol_cond_copy(&mpol,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700827 mpol_shared_policy_lookup(&info->policy, index));
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* Create a pseudo vma that just contains the policy */
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800830 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700831 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800832 pvma.vm_ops = NULL;
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700833 pvma.vm_policy = spol;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700834 return swapin_readahead(swap, gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Hugh Dickins02098fe2008-02-04 22:28:42 -0800837static struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700838 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct vm_area_struct pvma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800842 /* Create a pseudo vma that just contains the policy */
843 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700844 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800845 pvma.vm_ops = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700846 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700847
848 /*
849 * alloc_page_vma() will drop the shared policy reference
850 */
851 return alloc_page_vma(gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800853#else /* !CONFIG_NUMA */
854#ifdef CONFIG_TMPFS
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700855static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800856{
857}
858#endif /* CONFIG_TMPFS */
859
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700860static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
861 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700863 return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Hugh Dickins02098fe2008-02-04 22:28:42 -0800866static inline struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700867 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Hugh Dickinse84e2e12007-11-28 18:55:10 +0000869 return alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800871#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700873#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
874static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
875{
876 return NULL;
877}
878#endif
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/*
Hugh Dickins68da9f02011-07-25 17:12:34 -0700881 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 *
883 * If we allocate a new one we do not mark it dirty. That's up to the
884 * vm. If we swap it in we mark it dirty since we also free the swap
885 * entry since a page cannot live in both the swap and page cache
886 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700887static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Hugh Dickins68da9f02011-07-25 17:12:34 -0700888 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 struct address_space *mapping = inode->i_mapping;
891 struct shmem_inode_info *info = SHMEM_I(inode);
892 struct shmem_sb_info *sbinfo;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700893 struct page *page;
Shaohua Liff36b8012010-08-09 17:19:06 -0700894 struct page *prealloc_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 swp_entry_t swap;
896 int error;
897
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700898 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900repeat:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700901 page = find_lock_page(mapping, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700902 if (page) {
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800903 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700904 * Once we can get the page lock, it must be uptodate:
905 * if there were an error in reading back from swap,
906 * the page would not be inserted into the filecache.
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800907 */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700908 BUG_ON(!PageUptodate(page));
909 goto done;
910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700913 * Try to preload while we can wait, to not make a habit of
914 * draining atomic reserves; but don't latch on to this cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700916 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
917 if (error)
918 goto out;
919 radix_tree_preload_end();
920
921 if (sgp != SGP_READ && !prealloc_page) {
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700922 prealloc_page = shmem_alloc_page(gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700923 if (prealloc_page) {
924 SetPageSwapBacked(prealloc_page);
925 if (mem_cgroup_cache_charge(prealloc_page,
926 current->mm, GFP_KERNEL)) {
927 page_cache_release(prealloc_page);
928 prealloc_page = NULL;
Shaohua Liff36b8012010-08-09 17:19:06 -0700929 }
930 }
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 spin_lock(&info->lock);
934 shmem_recalc_inode(inode);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700935 swap = shmem_get_swap(info, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (swap.val) {
937 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700938 page = lookup_swap_cache(swap);
939 if (!page) {
Christoph Lameterf8891e52006-06-30 01:55:45 -0700940 spin_unlock(&info->lock);
Ying Han456f9982011-05-26 16:25:38 -0700941 /* here we actually do the io */
Hugh Dickins68da9f02011-07-25 17:12:34 -0700942 if (fault_type)
943 *fault_type |= VM_FAULT_MAJOR;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700944 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700945 if (!page) {
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700946 swp_entry_t nswap = shmem_get_swap(info, index);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700947 if (nswap.val == swap.val) {
948 error = -ENOMEM;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700949 goto out;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 goto repeat;
952 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700953 wait_on_page_locked(page);
954 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 goto repeat;
956 }
957
958 /* We have to do this with page locked to prevent races */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700959 if (!trylock_page(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700961 wait_on_page_locked(page);
962 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 goto repeat;
964 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700965 if (PageWriteback(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700967 wait_on_page_writeback(page);
968 unlock_page(page);
969 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 goto repeat;
971 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700972 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700974 unlock_page(page);
975 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 error = -EIO;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
Hugh Dickins27ab7002011-07-25 17:12:36 -0700980 error = add_to_page_cache_locked(page, mapping,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700981 index, GFP_NOWAIT);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700982 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 spin_unlock(&info->lock);
Hugh Dickins82369552008-02-07 00:14:22 -0800984 if (error == -ENOMEM) {
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -0700985 /*
986 * reclaim from proper memory cgroup and
987 * call memcg's OOM if needed.
988 */
989 error = mem_cgroup_shmem_charge_fallback(
Hugh Dickins27ab7002011-07-25 17:12:36 -0700990 page, current->mm, gfp);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -0800991 if (error) {
Hugh Dickins27ab7002011-07-25 17:12:36 -0700992 unlock_page(page);
993 page_cache_release(page);
994 goto out;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -0800995 }
Hugh Dickins82369552008-02-07 00:14:22 -0800996 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700997 unlock_page(page);
998 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 goto repeat;
1000 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001001
Hugh Dickins27ab7002011-07-25 17:12:36 -07001002 delete_from_swap_cache(page);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001003 shmem_put_swap(info, index, (swp_entry_t){0});
Hugh Dickins285b2c42011-08-03 16:21:20 -07001004 info->swapped--;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001005 spin_unlock(&info->lock);
1006 set_page_dirty(page);
1007 swap_free(swap);
1008
1009 } else if (sgp == SGP_READ) {
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001010 page = find_get_page(mapping, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001011 if (page && !trylock_page(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001013 wait_on_page_locked(page);
1014 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 goto repeat;
1016 }
1017 spin_unlock(&info->lock);
Hugh Dickinse83c32e2011-07-25 17:12:35 -07001018
1019 } else if (prealloc_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001021 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -07001022 if (percpu_counter_compare(&sbinfo->used_blocks,
1023 sbinfo->max_blocks) >= 0 ||
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001024 shmem_acct_block(info->flags))
1025 goto nospace;
Tim Chen7e496292010-08-09 17:19:05 -07001026 percpu_counter_inc(&sbinfo->used_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 inode->i_blocks += BLOCKS_PER_PAGE;
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001028 } else if (shmem_acct_block(info->flags))
1029 goto nospace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Hugh Dickins27ab7002011-07-25 17:12:36 -07001031 page = prealloc_page;
1032 prealloc_page = NULL;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001033
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001034 swap = shmem_get_swap(info, index);
Hugh Dickins285b2c42011-08-03 16:21:20 -07001035 if (swap.val)
Hugh Dickins27ab7002011-07-25 17:12:36 -07001036 mem_cgroup_uncharge_cache_page(page);
1037 else
Hugh Dickins285b2c42011-08-03 16:21:20 -07001038 error = add_to_page_cache_lru(page, mapping,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001039 index, GFP_NOWAIT);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001040 /*
1041 * At add_to_page_cache_lru() failure,
1042 * uncharge will be done automatically.
1043 */
Hugh Dickins285b2c42011-08-03 16:21:20 -07001044 if (swap.val || error) {
Hugh Dickins27ab7002011-07-25 17:12:36 -07001045 shmem_unacct_blocks(info->flags, 1);
1046 shmem_free_blocks(inode, 1);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001047 spin_unlock(&info->lock);
1048 page_cache_release(page);
1049 goto repeat;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 info->alloced++;
1053 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001054 clear_highpage(page);
1055 flush_dcache_page(page);
1056 SetPageUptodate(page);
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001057 if (sgp == SGP_DIRTY)
Hugh Dickins27ab7002011-07-25 17:12:36 -07001058 set_page_dirty(page);
1059
Hugh Dickinse83c32e2011-07-25 17:12:35 -07001060 } else {
1061 spin_unlock(&info->lock);
1062 error = -ENOMEM;
1063 goto out;
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065done:
Hugh Dickins27ab7002011-07-25 17:12:36 -07001066 *pagep = page;
Shaohua Liff36b8012010-08-09 17:19:06 -07001067 error = 0;
Shaohua Liff36b8012010-08-09 17:19:06 -07001068out:
1069 if (prealloc_page) {
1070 mem_cgroup_uncharge_cache_page(prealloc_page);
1071 page_cache_release(prealloc_page);
1072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return error;
Nick Piggind00806b2007-07-19 01:46:57 -07001074
Hugh Dickins27d54b32008-02-04 22:28:43 -08001075nospace:
Nick Piggind0217ac2007-07-19 01:47:03 -07001076 /*
1077 * Perhaps the page was brought in from swap between find_lock_page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 * and taking info->lock? We allow for that at add_to_page_cache_lru,
Nick Piggin83c54072007-07-19 01:47:05 -07001079 * but must also avoid reporting a spurious ENOSPC while working on a
Hugh Dickins9276aad2011-07-25 17:12:34 -07001080 * full tmpfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001082 page = find_get_page(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001084 if (page) {
1085 page_cache_release(page);
1086 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001087 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001088 error = -ENOSPC;
Hugh Dickinse83c32e2011-07-25 17:12:35 -07001089 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1093{
1094 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1095 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001096 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1099 return VM_FAULT_SIGBUS;
1100
1101 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1102 if (error)
1103 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001104
Ying Han456f9982011-05-26 16:25:38 -07001105 if (ret & VM_FAULT_MAJOR) {
1106 count_vm_event(PGMAJFAULT);
1107 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1108 }
Hugh Dickins68da9f02011-07-25 17:12:34 -07001109 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001113static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001115 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1116 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001119static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1120 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001122 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1123 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001125 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1126 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128#endif
1129
1130int shmem_lock(struct file *file, int lock, struct user_struct *user)
1131{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001132 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 struct shmem_inode_info *info = SHMEM_I(inode);
1134 int retval = -ENOMEM;
1135
1136 spin_lock(&info->lock);
1137 if (lock && !(info->flags & VM_LOCKED)) {
1138 if (!user_shm_lock(inode->i_size, user))
1139 goto out_nomem;
1140 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001141 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143 if (!lock && (info->flags & VM_LOCKED) && user) {
1144 user_shm_unlock(inode->i_size, user);
1145 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001146 mapping_clear_unevictable(file->f_mapping);
1147 scan_mapping_unevictable_pages(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151out_nomem:
1152 spin_unlock(&info->lock);
1153 return retval;
1154}
1155
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001156static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 file_accessed(file);
1159 vma->vm_ops = &shmem_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001160 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return 0;
1162}
1163
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001164static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1165 int mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 struct inode *inode;
1168 struct shmem_inode_info *info;
1169 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1170
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001171 if (shmem_reserve_inode(sb))
1172 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 inode = new_inode(sb);
1175 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001176 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001177 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1180 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001181 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 info = SHMEM_I(inode);
1183 memset(info, 0, (char *)inode - (char *)info);
1184 spin_lock_init(&info->lock);
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001185 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 INIT_LIST_HEAD(&info->swaplist);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001187 INIT_LIST_HEAD(&info->xattr_list);
Al Viro72c04902009-06-24 16:58:48 -04001188 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 switch (mode & S_IFMT) {
1191 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001192 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 init_special_inode(inode, mode, dev);
1194 break;
1195 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001196 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 inode->i_op = &shmem_inode_operations;
1198 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001199 mpol_shared_policy_init(&info->policy,
1200 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 break;
1202 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001203 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /* Some things misbehave if size == 0 on a directory */
1205 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1206 inode->i_op = &shmem_dir_inode_operations;
1207 inode->i_fop = &simple_dir_operations;
1208 break;
1209 case S_IFLNK:
1210 /*
1211 * Must not load anything in the rbtree,
1212 * mpol_free_shared_policy will not be called.
1213 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001214 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 break;
1216 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001217 } else
1218 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return inode;
1220}
1221
1222#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001223static const struct inode_operations shmem_symlink_inode_operations;
1224static const struct inode_operations shmem_symlink_inline_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001227shmem_write_begin(struct file *file, struct address_space *mapping,
1228 loff_t pos, unsigned len, unsigned flags,
1229 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Nick Piggin800d15a2007-10-16 01:25:03 -07001231 struct inode *inode = mapping->host;
1232 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin800d15a2007-10-16 01:25:03 -07001233 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1234}
1235
1236static int
1237shmem_write_end(struct file *file, struct address_space *mapping,
1238 loff_t pos, unsigned len, unsigned copied,
1239 struct page *page, void *fsdata)
1240{
1241 struct inode *inode = mapping->host;
1242
Hugh Dickinsd3602442008-02-04 22:28:44 -08001243 if (pos + copied > inode->i_size)
1244 i_size_write(inode, pos + copied);
1245
Nick Piggin800d15a2007-10-16 01:25:03 -07001246 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001247 unlock_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07001248 page_cache_release(page);
1249
Nick Piggin800d15a2007-10-16 01:25:03 -07001250 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1254{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001255 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001257 pgoff_t index;
1258 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001259 enum sgp_type sgp = SGP_READ;
1260
1261 /*
1262 * Might this read be for a stacking filesystem? Then when reading
1263 * holes of a sparse file, we actually need to allocate those pages,
1264 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1265 */
1266 if (segment_eq(get_fs(), KERNEL_DS))
1267 sgp = SGP_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 index = *ppos >> PAGE_CACHE_SHIFT;
1270 offset = *ppos & ~PAGE_CACHE_MASK;
1271
1272 for (;;) {
1273 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001274 pgoff_t end_index;
1275 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 loff_t i_size = i_size_read(inode);
1277
1278 end_index = i_size >> PAGE_CACHE_SHIFT;
1279 if (index > end_index)
1280 break;
1281 if (index == end_index) {
1282 nr = i_size & ~PAGE_CACHE_MASK;
1283 if (nr <= offset)
1284 break;
1285 }
1286
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001287 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (desc->error) {
1289 if (desc->error == -EINVAL)
1290 desc->error = 0;
1291 break;
1292 }
Hugh Dickinsd3602442008-02-04 22:28:44 -08001293 if (page)
1294 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 /*
1297 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001298 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 */
1300 nr = PAGE_CACHE_SIZE;
1301 i_size = i_size_read(inode);
1302 end_index = i_size >> PAGE_CACHE_SHIFT;
1303 if (index == end_index) {
1304 nr = i_size & ~PAGE_CACHE_MASK;
1305 if (nr <= offset) {
1306 if (page)
1307 page_cache_release(page);
1308 break;
1309 }
1310 }
1311 nr -= offset;
1312
1313 if (page) {
1314 /*
1315 * If users can be writing to this page using arbitrary
1316 * virtual addresses, take care about potential aliasing
1317 * before reading the page on the kernel side.
1318 */
1319 if (mapping_writably_mapped(mapping))
1320 flush_dcache_page(page);
1321 /*
1322 * Mark the page accessed if we read the beginning.
1323 */
1324 if (!offset)
1325 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07001326 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 page = ZERO_PAGE(0);
Nick Pigginb5810032005-10-29 18:16:12 -07001328 page_cache_get(page);
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 /*
1332 * Ok, we have the page, and it's up-to-date, so
1333 * now we can copy it to user space...
1334 *
1335 * The actor routine returns how many bytes were actually used..
1336 * NOTE! This may not be the same as how much of a user buffer
1337 * we filled up (we may be padding etc), so we can only update
1338 * "pos" here (the actor routine has to update the user buffer
1339 * pointers and the remaining count).
1340 */
1341 ret = actor(desc, page, offset, nr);
1342 offset += ret;
1343 index += offset >> PAGE_CACHE_SHIFT;
1344 offset &= ~PAGE_CACHE_MASK;
1345
1346 page_cache_release(page);
1347 if (ret != nr || !desc->count)
1348 break;
1349
1350 cond_resched();
1351 }
1352
1353 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1354 file_accessed(filp);
1355}
1356
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001357static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1358 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001360 struct file *filp = iocb->ki_filp;
1361 ssize_t retval;
1362 unsigned long seg;
1363 size_t count;
1364 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001366 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1367 if (retval)
1368 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001370 for (seg = 0; seg < nr_segs; seg++) {
1371 read_descriptor_t desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001373 desc.written = 0;
1374 desc.arg.buf = iov[seg].iov_base;
1375 desc.count = iov[seg].iov_len;
1376 if (desc.count == 0)
1377 continue;
1378 desc.error = 0;
1379 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1380 retval += desc.written;
1381 if (desc.error) {
1382 retval = retval ?: desc.error;
1383 break;
1384 }
1385 if (desc.count > 0)
1386 break;
1387 }
1388 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Hugh Dickins708e3502011-07-25 17:12:32 -07001391static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1392 struct pipe_inode_info *pipe, size_t len,
1393 unsigned int flags)
1394{
1395 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001396 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07001397 unsigned int loff, nr_pages, req_pages;
1398 struct page *pages[PIPE_DEF_BUFFERS];
1399 struct partial_page partial[PIPE_DEF_BUFFERS];
1400 struct page *page;
1401 pgoff_t index, end_index;
1402 loff_t isize, left;
1403 int error, page_nr;
1404 struct splice_pipe_desc spd = {
1405 .pages = pages,
1406 .partial = partial,
1407 .flags = flags,
1408 .ops = &page_cache_pipe_buf_ops,
1409 .spd_release = spd_release_page,
1410 };
1411
Hugh Dickins71f0e072011-07-25 17:12:33 -07001412 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001413 if (unlikely(*ppos >= isize))
1414 return 0;
1415
1416 left = isize - *ppos;
1417 if (unlikely(left < len))
1418 len = left;
1419
1420 if (splice_grow_spd(pipe, &spd))
1421 return -ENOMEM;
1422
1423 index = *ppos >> PAGE_CACHE_SHIFT;
1424 loff = *ppos & ~PAGE_CACHE_MASK;
1425 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1426 nr_pages = min(req_pages, pipe->buffers);
1427
Hugh Dickins708e3502011-07-25 17:12:32 -07001428 spd.nr_pages = find_get_pages_contig(mapping, index,
1429 nr_pages, spd.pages);
1430 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07001431 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001432
Hugh Dickins708e3502011-07-25 17:12:32 -07001433 while (spd.nr_pages < nr_pages) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001434 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1435 if (error)
1436 break;
1437 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07001438 spd.pages[spd.nr_pages++] = page;
1439 index++;
1440 }
1441
Hugh Dickins708e3502011-07-25 17:12:32 -07001442 index = *ppos >> PAGE_CACHE_SHIFT;
1443 nr_pages = spd.nr_pages;
1444 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001445
Hugh Dickins708e3502011-07-25 17:12:32 -07001446 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1447 unsigned int this_len;
1448
1449 if (!len)
1450 break;
1451
Hugh Dickins708e3502011-07-25 17:12:32 -07001452 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1453 page = spd.pages[page_nr];
1454
Hugh Dickins71f0e072011-07-25 17:12:33 -07001455 if (!PageUptodate(page) || page->mapping != mapping) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001456 error = shmem_getpage(inode, index, &page,
1457 SGP_CACHE, NULL);
1458 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07001459 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001460 unlock_page(page);
1461 page_cache_release(spd.pages[page_nr]);
1462 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07001463 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07001464
1465 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001466 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1467 if (unlikely(!isize || index > end_index))
1468 break;
1469
Hugh Dickins708e3502011-07-25 17:12:32 -07001470 if (end_index == index) {
1471 unsigned int plen;
1472
Hugh Dickins708e3502011-07-25 17:12:32 -07001473 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1474 if (plen <= loff)
1475 break;
1476
Hugh Dickins708e3502011-07-25 17:12:32 -07001477 this_len = min(this_len, plen - loff);
1478 len = this_len;
1479 }
1480
1481 spd.partial[page_nr].offset = loff;
1482 spd.partial[page_nr].len = this_len;
1483 len -= this_len;
1484 loff = 0;
1485 spd.nr_pages++;
1486 index++;
1487 }
1488
Hugh Dickins708e3502011-07-25 17:12:32 -07001489 while (page_nr < nr_pages)
1490 page_cache_release(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07001491
1492 if (spd.nr_pages)
1493 error = splice_to_pipe(pipe, &spd);
1494
1495 splice_shrink_spd(pipe, &spd);
1496
1497 if (error > 0) {
1498 *ppos += error;
1499 file_accessed(in);
1500 }
1501 return error;
1502}
1503
David Howells726c3342006-06-23 02:02:58 -07001504static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
David Howells726c3342006-06-23 02:02:58 -07001506 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 buf->f_type = TMPFS_MAGIC;
1509 buf->f_bsize = PAGE_CACHE_SIZE;
1510 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001511 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001513 buf->f_bavail =
1514 buf->f_bfree = sbinfo->max_blocks -
1515 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001516 }
1517 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 buf->f_files = sbinfo->max_inodes;
1519 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521 /* else leave those fields 0 like simple_statfs */
1522 return 0;
1523}
1524
1525/*
1526 * File creation. Allocate an inode, and we're done..
1527 */
1528static int
1529shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1530{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001531 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 int error = -ENOSPC;
1533
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001534 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (inode) {
Eric Paris2a7dba32011-02-01 11:05:39 -05001536 error = security_inode_init_security(inode, dir,
1537 &dentry->d_name, NULL,
1538 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001539 if (error) {
1540 if (error != -EOPNOTSUPP) {
1541 iput(inode);
1542 return error;
1543 }
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001544 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001545#ifdef CONFIG_TMPFS_POSIX_ACL
1546 error = generic_acl_init(inode, dir);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001547 if (error) {
1548 iput(inode);
1549 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001550 }
Al Viro718deb62009-12-16 19:35:36 -05001551#else
1552 error = 0;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001553#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 dir->i_size += BOGO_DIRENT_SIZE;
1555 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1556 d_instantiate(dentry, inode);
1557 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559 return error;
1560}
1561
1562static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1563{
1564 int error;
1565
1566 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1567 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07001568 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return 0;
1570}
1571
1572static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1573 struct nameidata *nd)
1574{
1575 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1576}
1577
1578/*
1579 * Link a file..
1580 */
1581static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1582{
1583 struct inode *inode = old_dentry->d_inode;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001584 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 /*
1587 * No ordinary (disk based) filesystem counts links as inodes;
1588 * but each new link needs a new dentry, pinning lowmem, and
1589 * tmpfs dentries cannot be pruned until they are unlinked.
1590 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001591 ret = shmem_reserve_inode(inode->i_sb);
1592 if (ret)
1593 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 dir->i_size += BOGO_DIRENT_SIZE;
1596 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07001597 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04001598 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 dget(dentry); /* Extra pinning count for the created dentry */
1600 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001601out:
1602 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
1605static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1606{
1607 struct inode *inode = dentry->d_inode;
1608
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001609 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1610 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 dir->i_size -= BOGO_DIRENT_SIZE;
1613 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001614 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 dput(dentry); /* Undo the count from "create" - this does all the work */
1616 return 0;
1617}
1618
1619static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1620{
1621 if (!simple_empty(dentry))
1622 return -ENOTEMPTY;
1623
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001624 drop_nlink(dentry->d_inode);
1625 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return shmem_unlink(dir, dentry);
1627}
1628
1629/*
1630 * The VFS layer already does all the dentry stuff for rename,
1631 * we just have to decrement the usage count for the target if
1632 * it exists so that the VFS layer correctly free's it when it
1633 * gets overwritten.
1634 */
1635static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1636{
1637 struct inode *inode = old_dentry->d_inode;
1638 int they_are_dirs = S_ISDIR(inode->i_mode);
1639
1640 if (!simple_empty(new_dentry))
1641 return -ENOTEMPTY;
1642
1643 if (new_dentry->d_inode) {
1644 (void) shmem_unlink(new_dir, new_dentry);
1645 if (they_are_dirs)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001646 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001648 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07001649 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651
1652 old_dir->i_size -= BOGO_DIRENT_SIZE;
1653 new_dir->i_size += BOGO_DIRENT_SIZE;
1654 old_dir->i_ctime = old_dir->i_mtime =
1655 new_dir->i_ctime = new_dir->i_mtime =
1656 inode->i_ctime = CURRENT_TIME;
1657 return 0;
1658}
1659
1660static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1661{
1662 int error;
1663 int len;
1664 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07001665 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 char *kaddr;
1667 struct shmem_inode_info *info;
1668
1669 len = strlen(symname) + 1;
1670 if (len > PAGE_CACHE_SIZE)
1671 return -ENAMETOOLONG;
1672
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001673 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (!inode)
1675 return -ENOSPC;
1676
Eric Paris2a7dba32011-02-01 11:05:39 -05001677 error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
1678 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001679 if (error) {
1680 if (error != -EOPNOTSUPP) {
1681 iput(inode);
1682 return error;
1683 }
1684 error = 0;
1685 }
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 info = SHMEM_I(inode);
1688 inode->i_size = len-1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07001689 if (len <= SHMEM_SYMLINK_INLINE_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 /* do it inline */
Eric Parisb09e0fa2011-05-24 17:12:39 -07001691 memcpy(info->inline_symlink, symname, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 inode->i_op = &shmem_symlink_inline_operations;
1693 } else {
1694 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1695 if (error) {
1696 iput(inode);
1697 return error;
1698 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07001699 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 inode->i_op = &shmem_symlink_inode_operations;
1701 kaddr = kmap_atomic(page, KM_USER0);
1702 memcpy(kaddr, symname, len);
1703 kunmap_atomic(kaddr, KM_USER0);
1704 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001705 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 page_cache_release(page);
1707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 dir->i_size += BOGO_DIRENT_SIZE;
1709 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1710 d_instantiate(dentry, inode);
1711 dget(dentry);
1712 return 0;
1713}
1714
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001715static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Eric Parisb09e0fa2011-05-24 17:12:39 -07001717 nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001718 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001721static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001724 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1725 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
Hugh Dickinsd3602442008-02-04 22:28:44 -08001726 if (page)
1727 unlock_page(page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001728 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001731static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
1733 if (!IS_ERR(nd_get_link(nd))) {
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001734 struct page *page = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 kunmap(page);
1736 mark_page_accessed(page);
1737 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
1739}
1740
Eric Parisb09e0fa2011-05-24 17:12:39 -07001741#ifdef CONFIG_TMPFS_XATTR
1742/*
1743 * Superblocks without xattr inode operations may get some security.* xattr
1744 * support from the LSM "for free". As soon as we have any other xattrs
1745 * like ACLs, we also need to implement the security.* handlers at
1746 * filesystem level, though.
1747 */
1748
1749static int shmem_xattr_get(struct dentry *dentry, const char *name,
1750 void *buffer, size_t size)
1751{
1752 struct shmem_inode_info *info;
1753 struct shmem_xattr *xattr;
1754 int ret = -ENODATA;
1755
1756 info = SHMEM_I(dentry->d_inode);
1757
1758 spin_lock(&info->lock);
1759 list_for_each_entry(xattr, &info->xattr_list, list) {
1760 if (strcmp(name, xattr->name))
1761 continue;
1762
1763 ret = xattr->size;
1764 if (buffer) {
1765 if (size < xattr->size)
1766 ret = -ERANGE;
1767 else
1768 memcpy(buffer, xattr->value, xattr->size);
1769 }
1770 break;
1771 }
1772 spin_unlock(&info->lock);
1773 return ret;
1774}
1775
1776static int shmem_xattr_set(struct dentry *dentry, const char *name,
1777 const void *value, size_t size, int flags)
1778{
1779 struct inode *inode = dentry->d_inode;
1780 struct shmem_inode_info *info = SHMEM_I(inode);
1781 struct shmem_xattr *xattr;
1782 struct shmem_xattr *new_xattr = NULL;
1783 size_t len;
1784 int err = 0;
1785
1786 /* value == NULL means remove */
1787 if (value) {
1788 /* wrap around? */
1789 len = sizeof(*new_xattr) + size;
1790 if (len <= sizeof(*new_xattr))
1791 return -ENOMEM;
1792
1793 new_xattr = kmalloc(len, GFP_KERNEL);
1794 if (!new_xattr)
1795 return -ENOMEM;
1796
1797 new_xattr->name = kstrdup(name, GFP_KERNEL);
1798 if (!new_xattr->name) {
1799 kfree(new_xattr);
1800 return -ENOMEM;
1801 }
1802
1803 new_xattr->size = size;
1804 memcpy(new_xattr->value, value, size);
1805 }
1806
1807 spin_lock(&info->lock);
1808 list_for_each_entry(xattr, &info->xattr_list, list) {
1809 if (!strcmp(name, xattr->name)) {
1810 if (flags & XATTR_CREATE) {
1811 xattr = new_xattr;
1812 err = -EEXIST;
1813 } else if (new_xattr) {
1814 list_replace(&xattr->list, &new_xattr->list);
1815 } else {
1816 list_del(&xattr->list);
1817 }
1818 goto out;
1819 }
1820 }
1821 if (flags & XATTR_REPLACE) {
1822 xattr = new_xattr;
1823 err = -ENODATA;
1824 } else {
1825 list_add(&new_xattr->list, &info->xattr_list);
1826 xattr = NULL;
1827 }
1828out:
1829 spin_unlock(&info->lock);
1830 if (xattr)
1831 kfree(xattr->name);
1832 kfree(xattr);
1833 return err;
1834}
1835
Eric Parisb09e0fa2011-05-24 17:12:39 -07001836static const struct xattr_handler *shmem_xattr_handlers[] = {
1837#ifdef CONFIG_TMPFS_POSIX_ACL
1838 &generic_acl_access_handler,
1839 &generic_acl_default_handler,
1840#endif
1841 NULL
1842};
1843
1844static int shmem_xattr_validate(const char *name)
1845{
1846 struct { const char *prefix; size_t len; } arr[] = {
1847 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1848 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1849 };
1850 int i;
1851
1852 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1853 size_t preflen = arr[i].len;
1854 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1855 if (!name[preflen])
1856 return -EINVAL;
1857 return 0;
1858 }
1859 }
1860 return -EOPNOTSUPP;
1861}
1862
1863static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1864 void *buffer, size_t size)
1865{
1866 int err;
1867
1868 /*
1869 * If this is a request for a synthetic attribute in the system.*
1870 * namespace use the generic infrastructure to resolve a handler
1871 * for it via sb->s_xattr.
1872 */
1873 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1874 return generic_getxattr(dentry, name, buffer, size);
1875
1876 err = shmem_xattr_validate(name);
1877 if (err)
1878 return err;
1879
1880 return shmem_xattr_get(dentry, name, buffer, size);
1881}
1882
1883static int shmem_setxattr(struct dentry *dentry, const char *name,
1884 const void *value, size_t size, int flags)
1885{
1886 int err;
1887
1888 /*
1889 * If this is a request for a synthetic attribute in the system.*
1890 * namespace use the generic infrastructure to resolve a handler
1891 * for it via sb->s_xattr.
1892 */
1893 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1894 return generic_setxattr(dentry, name, value, size, flags);
1895
1896 err = shmem_xattr_validate(name);
1897 if (err)
1898 return err;
1899
1900 if (size == 0)
1901 value = ""; /* empty EA, do not remove */
1902
1903 return shmem_xattr_set(dentry, name, value, size, flags);
1904
1905}
1906
1907static int shmem_removexattr(struct dentry *dentry, const char *name)
1908{
1909 int err;
1910
1911 /*
1912 * If this is a request for a synthetic attribute in the system.*
1913 * namespace use the generic infrastructure to resolve a handler
1914 * for it via sb->s_xattr.
1915 */
1916 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1917 return generic_removexattr(dentry, name);
1918
1919 err = shmem_xattr_validate(name);
1920 if (err)
1921 return err;
1922
1923 return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
1924}
1925
1926static bool xattr_is_trusted(const char *name)
1927{
1928 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1929}
1930
1931static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
1932{
1933 bool trusted = capable(CAP_SYS_ADMIN);
1934 struct shmem_xattr *xattr;
1935 struct shmem_inode_info *info;
1936 size_t used = 0;
1937
1938 info = SHMEM_I(dentry->d_inode);
1939
1940 spin_lock(&info->lock);
1941 list_for_each_entry(xattr, &info->xattr_list, list) {
1942 size_t len;
1943
1944 /* skip "trusted." attributes for unprivileged callers */
1945 if (!trusted && xattr_is_trusted(xattr->name))
1946 continue;
1947
1948 len = strlen(xattr->name) + 1;
1949 used += len;
1950 if (buffer) {
1951 if (size < used) {
1952 used = -ERANGE;
1953 break;
1954 }
1955 memcpy(buffer, xattr->name, len);
1956 buffer += len;
1957 }
1958 }
1959 spin_unlock(&info->lock);
1960
1961 return used;
1962}
1963#endif /* CONFIG_TMPFS_XATTR */
1964
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001965static const struct inode_operations shmem_symlink_inline_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 .readlink = generic_readlink,
1967 .follow_link = shmem_follow_link_inline,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001968#ifdef CONFIG_TMPFS_XATTR
1969 .setxattr = shmem_setxattr,
1970 .getxattr = shmem_getxattr,
1971 .listxattr = shmem_listxattr,
1972 .removexattr = shmem_removexattr,
1973#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974};
1975
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001976static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 .readlink = generic_readlink,
1978 .follow_link = shmem_follow_link,
1979 .put_link = shmem_put_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001980#ifdef CONFIG_TMPFS_XATTR
1981 .setxattr = shmem_setxattr,
1982 .getxattr = shmem_getxattr,
1983 .listxattr = shmem_listxattr,
1984 .removexattr = shmem_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001985#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07001986};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001987
David M. Grimes91828a42006-10-17 00:09:45 -07001988static struct dentry *shmem_get_parent(struct dentry *child)
1989{
1990 return ERR_PTR(-ESTALE);
1991}
1992
1993static int shmem_match(struct inode *ino, void *vfh)
1994{
1995 __u32 *fh = vfh;
1996 __u64 inum = fh[2];
1997 inum = (inum << 32) | fh[1];
1998 return ino->i_ino == inum && fh[0] == ino->i_generation;
1999}
2000
Christoph Hellwig480b1162007-10-21 16:42:13 -07002001static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2002 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07002003{
David M. Grimes91828a42006-10-17 00:09:45 -07002004 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07002005 struct dentry *dentry = NULL;
2006 u64 inum = fid->raw[2];
2007 inum = (inum << 32) | fid->raw[1];
David M. Grimes91828a42006-10-17 00:09:45 -07002008
Christoph Hellwig480b1162007-10-21 16:42:13 -07002009 if (fh_len < 3)
2010 return NULL;
2011
2012 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2013 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07002014 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07002015 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07002016 iput(inode);
2017 }
2018
Christoph Hellwig480b1162007-10-21 16:42:13 -07002019 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07002020}
2021
2022static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2023 int connectable)
2024{
2025 struct inode *inode = dentry->d_inode;
2026
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302027 if (*len < 3) {
2028 *len = 3;
David M. Grimes91828a42006-10-17 00:09:45 -07002029 return 255;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302030 }
David M. Grimes91828a42006-10-17 00:09:45 -07002031
Al Viro1d3382cb2010-10-23 15:19:20 -04002032 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07002033 /* Unfortunately insert_inode_hash is not idempotent,
2034 * so as we hash inodes here rather than at creation
2035 * time, we need a lock to ensure we only try
2036 * to do it once
2037 */
2038 static DEFINE_SPINLOCK(lock);
2039 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04002040 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07002041 __insert_inode_hash(inode,
2042 inode->i_ino + inode->i_generation);
2043 spin_unlock(&lock);
2044 }
2045
2046 fh[0] = inode->i_generation;
2047 fh[1] = inode->i_ino;
2048 fh[2] = ((__u64)inode->i_ino) >> 32;
2049
2050 *len = 3;
2051 return 1;
2052}
2053
Christoph Hellwig39655162007-10-21 16:42:17 -07002054static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07002055 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07002056 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07002057 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07002058};
2059
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002060static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2061 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 char *this_char, *value, *rest;
2064
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00002065 while (options != NULL) {
2066 this_char = options;
2067 for (;;) {
2068 /*
2069 * NUL-terminate this option: unfortunately,
2070 * mount options form a comma-separated list,
2071 * but mpol's nodelist may also contain commas.
2072 */
2073 options = strchr(options, ',');
2074 if (options == NULL)
2075 break;
2076 options++;
2077 if (!isdigit(*options)) {
2078 options[-1] = '\0';
2079 break;
2080 }
2081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (!*this_char)
2083 continue;
2084 if ((value = strchr(this_char,'=')) != NULL) {
2085 *value++ = 0;
2086 } else {
2087 printk(KERN_ERR
2088 "tmpfs: No value for mount option '%s'\n",
2089 this_char);
2090 return 1;
2091 }
2092
2093 if (!strcmp(this_char,"size")) {
2094 unsigned long long size;
2095 size = memparse(value,&rest);
2096 if (*rest == '%') {
2097 size <<= PAGE_SHIFT;
2098 size *= totalram_pages;
2099 do_div(size, 100);
2100 rest++;
2101 }
2102 if (*rest)
2103 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002104 sbinfo->max_blocks =
2105 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002107 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (*rest)
2109 goto bad_val;
2110 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002111 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (*rest)
2113 goto bad_val;
2114 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002115 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002117 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (*rest)
2119 goto bad_val;
2120 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002121 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002123 sbinfo->uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (*rest)
2125 goto bad_val;
2126 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002127 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002129 sbinfo->gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (*rest)
2131 goto bad_val;
Robin Holt7339ff82006-01-14 13:20:48 -08002132 } else if (!strcmp(this_char,"mpol")) {
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002133 if (mpol_parse_str(value, &sbinfo->mpol, 1))
Robin Holt7339ff82006-01-14 13:20:48 -08002134 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 } else {
2136 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2137 this_char);
2138 return 1;
2139 }
2140 }
2141 return 0;
2142
2143bad_val:
2144 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2145 value, this_char);
2146 return 1;
2147
2148}
2149
2150static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2151{
2152 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002153 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002154 unsigned long inodes;
2155 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002157 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002158 return error;
2159
2160 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002161 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07002162 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002163 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002164 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002165 goto out;
2166 /*
2167 * Those tests also disallow limited->unlimited while any are in
2168 * use, so i_blocks will always be zero when max_blocks is zero;
2169 * but we must separately disallow unlimited->limited, because
2170 * in that case we have no record of how much is already in use.
2171 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002172 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002173 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002174 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002175 goto out;
2176
2177 error = 0;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002178 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002179 sbinfo->max_inodes = config.max_inodes;
2180 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002181
2182 mpol_put(sbinfo->mpol);
2183 sbinfo->mpol = config.mpol; /* transfers initial ref */
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002184out:
2185 spin_unlock(&sbinfo->stat_lock);
2186 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002188
2189static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2190{
2191 struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2192
2193 if (sbinfo->max_blocks != shmem_default_max_blocks())
2194 seq_printf(seq, ",size=%luk",
2195 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2196 if (sbinfo->max_inodes != shmem_default_max_inodes())
2197 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2198 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2199 seq_printf(seq, ",mode=%03o", sbinfo->mode);
2200 if (sbinfo->uid != 0)
2201 seq_printf(seq, ",uid=%u", sbinfo->uid);
2202 if (sbinfo->gid != 0)
2203 seq_printf(seq, ",gid=%u", sbinfo->gid);
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002204 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002205 return 0;
2206}
2207#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209static void shmem_put_super(struct super_block *sb)
2210{
Hugh Dickins602586a2010-08-17 15:23:56 -07002211 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2212
2213 percpu_counter_destroy(&sbinfo->used_blocks);
2214 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 sb->s_fs_info = NULL;
2216}
2217
Kay Sievers2b2af542009-04-30 15:23:42 +02002218int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
2220 struct inode *inode;
2221 struct dentry *root;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002222 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002223 int err = -ENOMEM;
2224
2225 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07002226 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002227 L1_CACHE_BYTES), GFP_KERNEL);
2228 if (!sbinfo)
2229 return -ENOMEM;
2230
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002231 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11002232 sbinfo->uid = current_fsuid();
2233 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002234 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002236#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 /*
2238 * Per default we only allow half of the physical ram per
2239 * tmpfs instance, limiting inodes to one per page of lowmem;
2240 * but the internal instance is left unlimited.
2241 */
2242 if (!(sb->s_flags & MS_NOUSER)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002243 sbinfo->max_blocks = shmem_default_max_blocks();
2244 sbinfo->max_inodes = shmem_default_max_inodes();
2245 if (shmem_parse_options(data, sbinfo, false)) {
2246 err = -EINVAL;
2247 goto failed;
2248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
David M. Grimes91828a42006-10-17 00:09:45 -07002250 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251#else
2252 sb->s_flags |= MS_NOUSER;
2253#endif
2254
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002255 spin_lock_init(&sbinfo->stat_lock);
Hugh Dickins602586a2010-08-17 15:23:56 -07002256 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2257 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002258 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002259
Hugh Dickins285b2c42011-08-03 16:21:20 -07002260 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 sb->s_blocksize = PAGE_CACHE_SIZE;
2262 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2263 sb->s_magic = TMPFS_MAGIC;
2264 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01002265 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002266#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002267 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002268#endif
2269#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002270 sb->s_flags |= MS_POSIXACL;
2271#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002272
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002273 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (!inode)
2275 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002276 inode->i_uid = sbinfo->uid;
2277 inode->i_gid = sbinfo->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 root = d_alloc_root(inode);
2279 if (!root)
2280 goto failed_iput;
2281 sb->s_root = root;
2282 return 0;
2283
2284failed_iput:
2285 iput(inode);
2286failed:
2287 shmem_put_super(sb);
2288 return err;
2289}
2290
Pekka Enbergfcc234f2006-03-22 00:08:13 -08002291static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293static struct inode *shmem_alloc_inode(struct super_block *sb)
2294{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002295 struct shmem_inode_info *info;
2296 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2297 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002299 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002302static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002303{
2304 struct inode *inode = container_of(head, struct inode, i_rcu);
2305 INIT_LIST_HEAD(&inode->i_dentry);
2306 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2307}
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309static void shmem_destroy_inode(struct inode *inode)
2310{
2311 if ((inode->i_mode & S_IFMT) == S_IFREG) {
2312 /* only struct inode is valid if it's an inline symlink */
2313 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2314 }
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002315 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002318static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002320 struct shmem_inode_info *info = foo;
2321 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002324static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
2326 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2327 sizeof(struct shmem_inode_info),
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002328 0, SLAB_PANIC, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return 0;
2330}
2331
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002332static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002334 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002337static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08002339 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07002341 .write_begin = shmem_write_begin,
2342 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343#endif
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07002344 .migratepage = migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +02002345 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346};
2347
Helge Deller15ad7cd2006-12-06 20:40:36 -08002348static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 .mmap = shmem_mmap,
2350#ifdef CONFIG_TMPFS
2351 .llseek = generic_file_llseek,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002352 .read = do_sync_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002353 .write = do_sync_write,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002354 .aio_read = shmem_file_aio_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002355 .aio_write = generic_file_aio_write,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02002356 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07002357 .splice_read = shmem_file_splice_read,
Hugh Dickinsae9764162007-06-04 10:00:39 +02002358 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359#endif
2360};
2361
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002362static const struct inode_operations shmem_inode_operations = {
Hugh Dickins94c1e622011-06-27 16:18:03 -07002363 .setattr = shmem_setattr,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08002364 .truncate_range = shmem_truncate_range,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002365#ifdef CONFIG_TMPFS_XATTR
2366 .setxattr = shmem_setxattr,
2367 .getxattr = shmem_getxattr,
2368 .listxattr = shmem_listxattr,
2369 .removexattr = shmem_removexattr,
2370#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371};
2372
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002373static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374#ifdef CONFIG_TMPFS
2375 .create = shmem_create,
2376 .lookup = simple_lookup,
2377 .link = shmem_link,
2378 .unlink = shmem_unlink,
2379 .symlink = shmem_symlink,
2380 .mkdir = shmem_mkdir,
2381 .rmdir = shmem_rmdir,
2382 .mknod = shmem_mknod,
2383 .rename = shmem_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002385#ifdef CONFIG_TMPFS_XATTR
2386 .setxattr = shmem_setxattr,
2387 .getxattr = shmem_getxattr,
2388 .listxattr = shmem_listxattr,
2389 .removexattr = shmem_removexattr,
2390#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002391#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002392 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002393#endif
2394};
2395
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002396static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07002397#ifdef CONFIG_TMPFS_XATTR
2398 .setxattr = shmem_setxattr,
2399 .getxattr = shmem_getxattr,
2400 .listxattr = shmem_listxattr,
2401 .removexattr = shmem_removexattr,
2402#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002403#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002404 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002405#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406};
2407
Hugh Dickins759b9772007-03-05 00:30:28 -08002408static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 .alloc_inode = shmem_alloc_inode,
2410 .destroy_inode = shmem_destroy_inode,
2411#ifdef CONFIG_TMPFS
2412 .statfs = shmem_statfs,
2413 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002414 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415#endif
Al Viro1f895f72010-06-05 19:10:41 -04002416 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 .drop_inode = generic_delete_inode,
2418 .put_super = shmem_put_super,
2419};
2420
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002421static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002422 .fault = shmem_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423#ifdef CONFIG_NUMA
2424 .set_policy = shmem_set_policy,
2425 .get_policy = shmem_get_policy,
2426#endif
2427};
2428
Al Viro3c26ff62010-07-25 11:46:36 +04002429static struct dentry *shmem_mount(struct file_system_type *fs_type,
2430 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Al Viro3c26ff62010-07-25 11:46:36 +04002432 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002435static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 .owner = THIS_MODULE,
2437 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002438 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 .kill_sb = kill_litter_super,
2440};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002442int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
2444 int error;
2445
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002446 error = bdi_init(&shmem_backing_dev_info);
2447 if (error)
2448 goto out4;
2449
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002450 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (error)
2452 goto out3;
2453
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002454 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (error) {
2456 printk(KERN_ERR "Could not register tmpfs\n");
2457 goto out2;
2458 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07002459
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002460 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2461 shmem_fs_type.name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (IS_ERR(shm_mnt)) {
2463 error = PTR_ERR(shm_mnt);
2464 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2465 goto out1;
2466 }
2467 return 0;
2468
2469out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002470 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002472 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473out3:
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002474 bdi_destroy(&shmem_backing_dev_info);
2475out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 shm_mnt = ERR_PTR(error);
2477 return error;
2478}
Matt Mackall853ac432009-01-06 14:40:20 -08002479
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{
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002494 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002495 struct page *page = NULL;
2496 swp_entry_t swap = {0};
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002497
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002498 if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002499 goto out;
2500
2501 spin_lock(&info->lock);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002502#ifdef CONFIG_SWAP
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002503 swap = shmem_get_swap(info, index);
2504 if (swap.val)
2505 page = find_get_page(&swapper_space, swap.val);
Hugh Dickins285b2c42011-08-03 16:21:20 -07002506 else
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002507#endif
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002508 page = find_get_page(inode->i_mapping, index);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002509 spin_unlock(&info->lock);
2510out:
2511 *pagep = page;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002512 *swapp = swap;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002513}
2514#endif
2515
Matt Mackall853ac432009-01-06 14:40:20 -08002516#else /* !CONFIG_SHMEM */
2517
2518/*
2519 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2520 *
2521 * This is intended for small system where the benefits of the full
2522 * shmem code (swap-backed and resource-limited) are outweighed by
2523 * their complexity. On systems without swap this code should be
2524 * effectively equivalent, but much lighter weight.
2525 */
2526
2527#include <linux/ramfs.h>
2528
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002529static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08002530 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002531 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08002532 .kill_sb = kill_litter_super,
2533};
2534
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002535int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08002536{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002537 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08002538
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002539 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08002540 BUG_ON(IS_ERR(shm_mnt));
2541
2542 return 0;
2543}
2544
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002545int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08002546{
2547 return 0;
2548}
2549
Hugh Dickins3f96b792009-09-21 17:03:37 -07002550int shmem_lock(struct file *file, int lock, struct user_struct *user)
2551{
2552 return 0;
2553}
2554
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002555void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07002556{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002557 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07002558}
2559EXPORT_SYMBOL_GPL(shmem_truncate_range);
2560
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002561#ifdef CONFIG_CGROUP_MEM_RES_CTLR
2562/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002563 * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002564 * @inode: the inode to be searched
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002565 * @index: the page offset to be searched
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002566 * @pagep: the pointer for the found page to be stored
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002567 * @swapp: the pointer for the found swap entry to be stored
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002568 *
2569 * If a page is found, refcount of it is incremented. Callers should handle
2570 * these refcount.
2571 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002572void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index,
2573 struct page **pagep, swp_entry_t *swapp)
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002574{
2575 struct page *page = NULL;
2576
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002577 if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002578 goto out;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002579 page = find_get_page(inode->i_mapping, index);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002580out:
2581 *pagep = page;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002582 *swapp = (swp_entry_t){0};
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002583}
2584#endif
2585
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002586#define shmem_vm_ops generic_file_vm_ops
2587#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002588#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002589#define shmem_acct_size(flags, size) 0
2590#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08002591
2592#endif /* CONFIG_SHMEM */
2593
2594/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Randy Dunlap46711812008-03-19 17:00:41 -07002596/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 * shmem_file_setup - get an unlinked file living in tmpfs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 * @name: name for dentry (to be seen in /proc/<pid>/maps
2599 * @size: size to be set for the file
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002600 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 */
Sergei Trofimovich168f5ac2009-06-16 15:33:02 -07002602struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 int error;
2605 struct file *file;
2606 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04002607 struct path path;
2608 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 struct qstr this;
2610
2611 if (IS_ERR(shm_mnt))
2612 return (void *)shm_mnt;
2613
Hugh Dickins285b2c42011-08-03 16:21:20 -07002614 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return ERR_PTR(-EINVAL);
2616
2617 if (shmem_acct_size(flags, size))
2618 return ERR_PTR(-ENOMEM);
2619
2620 error = -ENOMEM;
2621 this.name = name;
2622 this.len = strlen(name);
2623 this.hash = 0; /* will go */
2624 root = shm_mnt->mnt_root;
Al Viro2c48b9c2009-08-09 00:52:35 +04002625 path.dentry = d_alloc(root, &this);
2626 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 goto put_memory;
Al Viro2c48b9c2009-08-09 00:52:35 +04002628 path.mnt = mntget(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 error = -ENOSPC;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002631 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 if (!inode)
Al Viro4b42af82009-08-05 18:25:56 +04002633 goto put_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Al Viro2c48b9c2009-08-09 00:52:35 +04002635 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 inode->i_size = size;
2637 inode->i_nlink = 0; /* It is unlinked */
Matt Mackall853ac432009-01-06 14:40:20 -08002638#ifndef CONFIG_MMU
2639 error = ramfs_nommu_expand_for_mapping(inode, size);
2640 if (error)
Al Viro4b42af82009-08-05 18:25:56 +04002641 goto put_dentry;
Matt Mackall853ac432009-01-06 14:40:20 -08002642#endif
Al Viro4b42af82009-08-05 18:25:56 +04002643
2644 error = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04002645 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04002646 &shmem_file_operations);
2647 if (!file)
2648 goto put_dentry;
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 return file;
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652put_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04002653 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654put_memory:
2655 shmem_unacct_size(flags, size);
2656 return ERR_PTR(error);
2657}
Keith Packard395e0dd2008-06-20 00:08:06 -07002658EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Randy Dunlap46711812008-03-19 17:00:41 -07002660/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2663 */
2664int shmem_zero_setup(struct vm_area_struct *vma)
2665{
2666 struct file *file;
2667 loff_t size = vma->vm_end - vma->vm_start;
2668
2669 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2670 if (IS_ERR(file))
2671 return PTR_ERR(file);
2672
2673 if (vma->vm_file)
2674 fput(vma->vm_file);
2675 vma->vm_file = file;
2676 vma->vm_ops = &shmem_vm_ops;
Hugh Dickinsbee4c36a2011-03-22 16:33:43 -07002677 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return 0;
2679}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002680
2681/**
2682 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2683 * @mapping: the page's address_space
2684 * @index: the page index
2685 * @gfp: the page allocator flags to use if allocating
2686 *
2687 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2688 * with any new page allocations done using the specified allocation flags.
2689 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2690 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2691 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2692 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07002693 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2694 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002695 */
2696struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2697 pgoff_t index, gfp_t gfp)
2698{
Hugh Dickins68da9f02011-07-25 17:12:34 -07002699#ifdef CONFIG_SHMEM
2700 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002701 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07002702 int error;
2703
2704 BUG_ON(mapping->a_ops != &shmem_aops);
2705 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2706 if (error)
2707 page = ERR_PTR(error);
2708 else
2709 unlock_page(page);
2710 return page;
2711#else
2712 /*
2713 * The tiny !SHMEM case uses ramfs without swap
2714 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002715 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002716#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002717}
2718EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);