]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - mm/shmem.c
tmpfs: convert shmem_unuse_inode to radix-swap
[mirror_ubuntu-zesty-kernel.git] / mm / shmem.c
CommitLineData
1da177e4
LT
1/*
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.
0edd73b3
HD
9 * Copyright (C) 2002-2005 Hugh Dickins.
10 * Copyright (C) 2002-2005 VERITAS Software Corporation.
1da177e4
LT
11 * 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 *
853ac43a
MM
17 * tiny-shmem:
18 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
19 *
1da177e4
LT
20 * This file is released under the GPL.
21 */
22
853ac43a
MM
23#include <linux/fs.h>
24#include <linux/init.h>
25#include <linux/vfs.h>
26#include <linux/mount.h>
caefba17 27#include <linux/pagemap.h>
853ac43a
MM
28#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
1da177e4
LT
36/*
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
39f0247d 42#include <linux/xattr.h>
a5694255 43#include <linux/exportfs.h>
1c7c474c 44#include <linux/posix_acl.h>
39f0247d 45#include <linux/generic_acl.h>
1da177e4 46#include <linux/mman.h>
1da177e4
LT
47#include <linux/string.h>
48#include <linux/slab.h>
49#include <linux/backing-dev.h>
50#include <linux/shmem_fs.h>
1da177e4 51#include <linux/writeback.h>
1da177e4 52#include <linux/blkdev.h>
bda97eab 53#include <linux/pagevec.h>
41ffe5d5 54#include <linux/percpu_counter.h>
708e3508 55#include <linux/splice.h>
1da177e4
LT
56#include <linux/security.h>
57#include <linux/swapops.h>
58#include <linux/mempolicy.h>
59#include <linux/namei.h>
b00dc3ad 60#include <linux/ctype.h>
304dbdb7 61#include <linux/migrate.h>
c1f60a5a 62#include <linux/highmem.h>
680d794b 63#include <linux/seq_file.h>
92562927 64#include <linux/magic.h>
304dbdb7 65
1da177e4 66#include <asm/uaccess.h>
1da177e4
LT
67#include <asm/pgtable.h>
68
caefba17 69#define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
1da177e4
LT
70#define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
71
1da177e4
LT
72/* Pretend that each entry is of this size in directory's i_size */
73#define BOGO_DIRENT_SIZE 20
74
b09e0fa4
EP
75struct 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
285b2c4f 82/* Flag allocation requirements to shmem_getpage */
1da177e4 83enum sgp_type {
1da177e4
LT
84 SGP_READ, /* don't exceed i_size, don't allocate page */
85 SGP_CACHE, /* don't exceed i_size, may allocate page */
a0ee5ec5 86 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
1da177e4
LT
87 SGP_WRITE, /* may exceed i_size, may allocate page */
88};
89
b76db735 90#ifdef CONFIG_TMPFS
680d794b
AM
91static 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}
b76db735 100#endif
680d794b 101
68da9f05
HD
102static 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}
1da177e4 111
1da177e4
LT
112static 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{
0b0a0806
HD
125 return (flags & VM_NORESERVE) ?
126 0 : security_vm_enough_memory_kern(VM_ACCT(size));
1da177e4
LT
127}
128
129static inline void shmem_unacct_size(unsigned long flags, loff_t size)
130{
0b0a0806 131 if (!(flags & VM_NORESERVE))
1da177e4
LT
132 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{
0b0a0806
HD
143 return (flags & VM_NORESERVE) ?
144 security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
1da177e4
LT
145}
146
147static inline void shmem_unacct_blocks(unsigned long flags, long pages)
148{
0b0a0806 149 if (flags & VM_NORESERVE)
1da177e4
LT
150 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
151}
152
759b9775 153static const struct super_operations shmem_ops;
f5e54d6e 154static const struct address_space_operations shmem_aops;
15ad7cdc 155static const struct file_operations shmem_file_operations;
92e1d5be
AV
156static 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;
f0f37e2f 159static const struct vm_operations_struct shmem_vm_ops;
1da177e4 160
6c231b7b 161static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
1da177e4 162 .ra_pages = 0, /* No readahead */
4f98a2fe 163 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
1da177e4
LT
164};
165
166static LIST_HEAD(shmem_swaplist);
cb5f7b9a 167static DEFINE_MUTEX(shmem_swaplist_mutex);
1da177e4
LT
168
169static void shmem_free_blocks(struct inode *inode, long pages)
170{
171 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
0edd73b3 172 if (sbinfo->max_blocks) {
7e496299 173 percpu_counter_add(&sbinfo->used_blocks, -pages);
1da177e4 174 inode->i_blocks -= pages*BLOCKS_PER_PAGE;
1da177e4
LT
175 }
176}
177
5b04c689
PE
178static 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
46711810 203/**
41ffe5d5 204 * shmem_recalc_inode - recalculate the block usage of an inode
1da177e4
LT
205 * @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
285b2c4f
HD
228static void shmem_put_swap(struct shmem_inode_info *info, pgoff_t index,
229 swp_entry_t swap)
1da177e4 230{
285b2c4f
HD
231 if (index < SHMEM_NR_DIRECT)
232 info->i_direct[index] = swap;
1da177e4
LT
233}
234
285b2c4f 235static swp_entry_t shmem_get_swap(struct shmem_inode_info *info, pgoff_t index)
1da177e4 236{
285b2c4f
HD
237 return (index < SHMEM_NR_DIRECT) ?
238 info->i_direct[index] : (swp_entry_t){0};
1da177e4
LT
239}
240
7a5d0fbb
HD
241/*
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
46f65ec1
HD
264/*
265 * 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
7a5d0fbb
HD
313/*
314 * 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
46f65ec1
HD
360/*
361 * 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
7a5d0fbb
HD
396/*
397 * 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 */
285b2c4f 431void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
1da177e4 432{
285b2c4f 433 struct address_space *mapping = inode->i_mapping;
1da177e4 434 struct shmem_inode_info *info = SHMEM_I(inode);
285b2c4f 435 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
bda97eab 436 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
285b2c4f 437 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
bda97eab 438 struct pagevec pvec;
7a5d0fbb
HD
439 pgoff_t indices[PAGEVEC_SIZE];
440 long nr_swaps_freed = 0;
285b2c4f 441 pgoff_t index;
bda97eab
HD
442 int i;
443
444 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
445
446 pagevec_init(&pvec, 0);
447 index = start;
7a5d0fbb
HD
448 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;
bda97eab
HD
454 mem_cgroup_uncharge_start();
455 for (i = 0; i < pagevec_count(&pvec); i++) {
456 struct page *page = pvec.pages[i];
457
7a5d0fbb 458 index = indices[i];
bda97eab
HD
459 if (index > end)
460 break;
461
7a5d0fbb
HD
462 if (radix_tree_exceptional_entry(page)) {
463 nr_swaps_freed += !shmem_free_swap(mapping,
464 index, page);
bda97eab 465 continue;
7a5d0fbb
HD
466 }
467
468 if (!trylock_page(page))
bda97eab 469 continue;
7a5d0fbb
HD
470 if (page->mapping == mapping) {
471 VM_BUG_ON(PageWriteback(page));
472 truncate_inode_page(mapping, page);
bda97eab 473 }
bda97eab
HD
474 unlock_page(page);
475 }
7a5d0fbb 476 shmem_pagevec_release(&pvec);
bda97eab
HD
477 mem_cgroup_uncharge_end();
478 cond_resched();
479 index++;
480 }
1da177e4 481
bda97eab
HD
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();
7a5d0fbb
HD
496 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) {
bda97eab
HD
500 if (index == start)
501 break;
502 index = start;
503 continue;
504 }
7a5d0fbb
HD
505 if (index == start && indices[0] > end) {
506 shmem_pagevec_release(&pvec);
bda97eab
HD
507 break;
508 }
509 mem_cgroup_uncharge_start();
510 for (i = 0; i < pagevec_count(&pvec); i++) {
511 struct page *page = pvec.pages[i];
512
7a5d0fbb 513 index = indices[i];
bda97eab
HD
514 if (index > end)
515 break;
516
7a5d0fbb
HD
517 if (radix_tree_exceptional_entry(page)) {
518 nr_swaps_freed += !shmem_free_swap(mapping,
519 index, page);
520 continue;
521 }
522
bda97eab 523 lock_page(page);
7a5d0fbb
HD
524 if (page->mapping == mapping) {
525 VM_BUG_ON(PageWriteback(page));
526 truncate_inode_page(mapping, page);
527 }
bda97eab
HD
528 unlock_page(page);
529 }
7a5d0fbb 530 shmem_pagevec_release(&pvec);
bda97eab
HD
531 mem_cgroup_uncharge_end();
532 index++;
533 }
94c1e62d 534
1da177e4 535 spin_lock(&info->lock);
7a5d0fbb 536 info->swapped -= nr_swaps_freed;
1da177e4
LT
537 shmem_recalc_inode(inode);
538 spin_unlock(&info->lock);
539
285b2c4f 540 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1da177e4 541}
94c1e62d 542EXPORT_SYMBOL_GPL(shmem_truncate_range);
1da177e4 543
94c1e62d 544static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
1da177e4
LT
545{
546 struct inode *inode = dentry->d_inode;
1da177e4
LT
547 int error;
548
db78b877
CH
549 error = inode_change_ok(inode, attr);
550 if (error)
551 return error;
552
94c1e62d
HD
553 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;
3889e6e7 556
94c1e62d
HD
557 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 }
1da177e4
LT
568 }
569
db78b877 570 setattr_copy(inode, attr);
39f0247d 571#ifdef CONFIG_TMPFS_POSIX_ACL
db78b877 572 if (attr->ia_valid & ATTR_MODE)
1c7c474c 573 error = generic_acl_chmod(inode);
39f0247d 574#endif
1da177e4
LT
575 return error;
576}
577
1f895f75 578static void shmem_evict_inode(struct inode *inode)
1da177e4 579{
1da177e4 580 struct shmem_inode_info *info = SHMEM_I(inode);
b09e0fa4 581 struct shmem_xattr *xattr, *nxattr;
1da177e4 582
3889e6e7 583 if (inode->i_mapping->a_ops == &shmem_aops) {
1da177e4
LT
584 shmem_unacct_size(info->flags, inode->i_size);
585 inode->i_size = 0;
3889e6e7 586 shmem_truncate_range(inode, 0, (loff_t)-1);
1da177e4 587 if (!list_empty(&info->swaplist)) {
cb5f7b9a 588 mutex_lock(&shmem_swaplist_mutex);
1da177e4 589 list_del_init(&info->swaplist);
cb5f7b9a 590 mutex_unlock(&shmem_swaplist_mutex);
1da177e4
LT
591 }
592 }
b09e0fa4
EP
593
594 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
595 kfree(xattr->name);
596 kfree(xattr);
597 }
0edd73b3 598 BUG_ON(inode->i_blocks);
5b04c689 599 shmem_free_inode(inode->i_sb);
1f895f75 600 end_writeback(inode);
1da177e4
LT
601}
602
46f65ec1
HD
603/*
604 * If swap found in inode, free it and move page from swapcache to filecache.
605 */
41ffe5d5
HD
606static int shmem_unuse_inode(struct shmem_inode_info *info,
607 swp_entry_t swap, struct page *page)
1da177e4 608{
285b2c4f 609 struct address_space *mapping = info->vfs_inode.i_mapping;
46f65ec1 610 void *radswap;
41ffe5d5 611 pgoff_t index;
d9fe526a 612 int error;
1da177e4 613
46f65ec1
HD
614 radswap = swp_to_radix_entry(swap);
615 index = shmem_find_swap(mapping, radswap);
616 if (index == -1)
285b2c4f 617 return 0;
2e0e26c7 618
1b1b32f2
HD
619 /*
620 * Move _head_ to start search for next from here.
1f895f75 621 * But be careful: shmem_evict_inode checks list_empty without taking
1b1b32f2 622 * mutex, and there's an instant in list_move_tail when info->swaplist
285b2c4f 623 * would appear empty, if it were the only one on shmem_swaplist.
1b1b32f2
HD
624 */
625 if (shmem_swaplist.next != &info->swaplist)
626 list_move_tail(&shmem_swaplist, &info->swaplist);
2e0e26c7 627
d13d1443 628 /*
778dd893
HD
629 * 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).
d13d1443 632 */
46f65ec1
HD
633 error = shmem_add_to_page_cache(page, mapping, index,
634 GFP_NOWAIT, radswap);
778dd893 635 /* which does mem_cgroup_uncharge_cache_page on error */
69029cd5 636
48f170fb 637 if (error != -ENOMEM) {
46f65ec1
HD
638 /*
639 * Truncation and eviction use free_swap_and_cache(), which
640 * only does trylock page: if we raced, best clean up here.
641 */
73b1262f
HD
642 delete_from_swap_cache(page);
643 set_page_dirty(page);
46f65ec1
HD
644 if (!error) {
645 spin_lock(&info->lock);
646 info->swapped--;
647 spin_unlock(&info->lock);
648 swap_free(swap);
649 }
2e0e26c7 650 error = 1; /* not an error, but entry was found */
1da177e4 651 }
2e0e26c7 652 return error;
1da177e4
LT
653}
654
655/*
46f65ec1 656 * Search through swapped inodes to find and replace swap by page.
1da177e4 657 */
41ffe5d5 658int shmem_unuse(swp_entry_t swap, struct page *page)
1da177e4 659{
41ffe5d5 660 struct list_head *this, *next;
1da177e4
LT
661 struct shmem_inode_info *info;
662 int found = 0;
778dd893
HD
663 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.
46f65ec1 669 * shmem_add_to_page_cache() will be called with GFP_NOWAIT.
778dd893
HD
670 */
671 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
672 if (error)
673 goto out;
46f65ec1 674 /* No radix_tree_preload: swap entry keeps a place for page in tree */
1da177e4 675
cb5f7b9a 676 mutex_lock(&shmem_swaplist_mutex);
41ffe5d5
HD
677 list_for_each_safe(this, next, &shmem_swaplist) {
678 info = list_entry(this, struct shmem_inode_info, swaplist);
285b2c4f
HD
679 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)
41ffe5d5 686 found = shmem_unuse_inode(info, swap, page);
cb5f7b9a 687 cond_resched();
2e0e26c7 688 if (found)
778dd893 689 break;
1da177e4 690 }
cb5f7b9a 691 mutex_unlock(&shmem_swaplist_mutex);
778dd893 692
778dd893
HD
693 if (!found)
694 mem_cgroup_uncharge_cache_page(page);
695 if (found < 0)
696 error = found;
697out:
aaa46865
HD
698 unlock_page(page);
699 page_cache_release(page);
778dd893 700 return error;
1da177e4
LT
701}
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;
285b2c4f 709 swp_entry_t swap, oswap;
1da177e4 710 struct address_space *mapping;
41ffe5d5 711 pgoff_t index;
1da177e4
LT
712 struct inode *inode;
713
714 BUG_ON(!PageLocked(page));
1da177e4
LT
715 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;
d9fe526a 721 if (!total_swap_pages)
1da177e4
LT
722 goto redirty;
723
d9fe526a
HD
724 /*
725 * shmem_backing_dev_info's capabilities prevent regular writeback or
726 * sync from ever calling shmem_writepage; but a stacking filesystem
48f170fb 727 * might use ->writepage of its underlying filesystem, in which case
d9fe526a 728 * tmpfs should write out to swap only in response to memory pressure,
48f170fb 729 * and not for the writeback threads or sync.
d9fe526a 730 */
48f170fb
HD
731 if (!wbc->for_reclaim) {
732 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
733 goto redirty;
734 }
285b2c4f
HD
735
736 /*
7a5d0fbb
HD
737 * Disable even the toy swapping implementation, while we convert
738 * functions one by one to having swap entries in the radix tree.
285b2c4f 739 */
7a5d0fbb 740 if (index < ULONG_MAX)
285b2c4f
HD
741 goto redirty;
742
48f170fb
HD
743 swap = get_swap_page();
744 if (!swap.val)
745 goto redirty;
d9fe526a 746
b1dea800
HD
747 /*
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 */
48f170fb
HD
756 mutex_lock(&shmem_swaplist_mutex);
757 if (list_empty(&info->swaplist))
758 list_add_tail(&info->swaplist, &shmem_swaplist);
b1dea800 759
1da177e4 760 spin_lock(&info->lock);
48f170fb 761 mutex_unlock(&shmem_swaplist_mutex);
b1dea800 762
285b2c4f
HD
763 oswap = shmem_get_swap(info, index);
764 if (oswap.val) {
48f170fb 765 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
285b2c4f
HD
766 free_swap_and_cache(oswap);
767 shmem_put_swap(info, index, (swp_entry_t){0});
768 info->swapped--;
d9fe526a
HD
769 }
770 shmem_recalc_inode(inode);
1da177e4 771
48f170fb 772 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
4c73b1bc 773 delete_from_page_cache(page);
285b2c4f
HD
774 shmem_put_swap(info, index, swap);
775 info->swapped++;
aaa46865 776 swap_shmem_alloc(swap);
826267cf 777 spin_unlock(&info->lock);
d9fe526a 778 BUG_ON(page_mapped(page));
9fab5619 779 swap_writepage(page, wbc);
1da177e4
LT
780 return 0;
781 }
782
1da177e4 783 spin_unlock(&info->lock);
cb4b86ba 784 swapcache_free(swap, NULL);
1da177e4
LT
785redirty:
786 set_page_dirty(page);
d9fe526a
HD
787 if (wbc->for_reclaim)
788 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
789 unlock_page(page);
790 return 0;
1da177e4
LT
791}
792
793#ifdef CONFIG_NUMA
680d794b 794#ifdef CONFIG_TMPFS
71fe804b 795static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
680d794b 796{
095f1fc4 797 char buffer[64];
680d794b 798
71fe804b 799 if (!mpol || mpol->mode == MPOL_DEFAULT)
095f1fc4 800 return; /* show nothing */
680d794b 801
71fe804b 802 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
095f1fc4
LS
803
804 seq_printf(seq, ",mpol=%s", buffer);
680d794b 805}
71fe804b
LS
806
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}
680d794b
AM
818#endif /* CONFIG_TMPFS */
819
41ffe5d5
HD
820static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
821 struct shmem_inode_info *info, pgoff_t index)
1da177e4 822{
52cd3b07 823 struct mempolicy mpol, *spol;
1da177e4
LT
824 struct vm_area_struct pvma;
825
52cd3b07 826 spol = mpol_cond_copy(&mpol,
41ffe5d5 827 mpol_shared_policy_lookup(&info->policy, index));
52cd3b07 828
1da177e4 829 /* Create a pseudo vma that just contains the policy */
c4cc6d07 830 pvma.vm_start = 0;
41ffe5d5 831 pvma.vm_pgoff = index;
c4cc6d07 832 pvma.vm_ops = NULL;
52cd3b07 833 pvma.vm_policy = spol;
41ffe5d5 834 return swapin_readahead(swap, gfp, &pvma, 0);
1da177e4
LT
835}
836
02098fea 837static struct page *shmem_alloc_page(gfp_t gfp,
41ffe5d5 838 struct shmem_inode_info *info, pgoff_t index)
1da177e4
LT
839{
840 struct vm_area_struct pvma;
1da177e4 841
c4cc6d07
HD
842 /* Create a pseudo vma that just contains the policy */
843 pvma.vm_start = 0;
41ffe5d5 844 pvma.vm_pgoff = index;
c4cc6d07 845 pvma.vm_ops = NULL;
41ffe5d5 846 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
52cd3b07
LS
847
848 /*
849 * alloc_page_vma() will drop the shared policy reference
850 */
851 return alloc_page_vma(gfp, &pvma, 0);
1da177e4 852}
680d794b
AM
853#else /* !CONFIG_NUMA */
854#ifdef CONFIG_TMPFS
41ffe5d5 855static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
680d794b
AM
856{
857}
858#endif /* CONFIG_TMPFS */
859
41ffe5d5
HD
860static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
861 struct shmem_inode_info *info, pgoff_t index)
1da177e4 862{
41ffe5d5 863 return swapin_readahead(swap, gfp, NULL, 0);
1da177e4
LT
864}
865
02098fea 866static inline struct page *shmem_alloc_page(gfp_t gfp,
41ffe5d5 867 struct shmem_inode_info *info, pgoff_t index)
1da177e4 868{
e84e2e13 869 return alloc_page(gfp);
1da177e4 870}
680d794b 871#endif /* CONFIG_NUMA */
1da177e4 872
71fe804b
LS
873#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
1da177e4 880/*
68da9f05 881 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
1da177e4
LT
882 *
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 */
41ffe5d5 887static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
68da9f05 888 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
1da177e4
LT
889{
890 struct address_space *mapping = inode->i_mapping;
891 struct shmem_inode_info *info = SHMEM_I(inode);
892 struct shmem_sb_info *sbinfo;
27ab7006 893 struct page *page;
ff36b801 894 struct page *prealloc_page = NULL;
1da177e4
LT
895 swp_entry_t swap;
896 int error;
897
41ffe5d5 898 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
1da177e4 899 return -EFBIG;
1da177e4 900repeat:
41ffe5d5 901 page = find_lock_page(mapping, index);
27ab7006 902 if (page) {
b409f9fc 903 /*
27ab7006
HD
904 * 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.
b409f9fc 907 */
27ab7006
HD
908 BUG_ON(!PageUptodate(page));
909 goto done;
910 }
911
912 /*
913 * 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.
915 */
916 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) {
41ffe5d5 922 prealloc_page = shmem_alloc_page(gfp, info, index);
27ab7006
HD
923 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;
ff36b801
SL
929 }
930 }
b409f9fc 931 }
1da177e4
LT
932
933 spin_lock(&info->lock);
934 shmem_recalc_inode(inode);
41ffe5d5 935 swap = shmem_get_swap(info, index);
1da177e4
LT
936 if (swap.val) {
937 /* Look it up and read it in.. */
27ab7006
HD
938 page = lookup_swap_cache(swap);
939 if (!page) {
456f998e 940 spin_unlock(&info->lock);
1da177e4 941 /* here we actually do the io */
68da9f05
HD
942 if (fault_type)
943 *fault_type |= VM_FAULT_MAJOR;
41ffe5d5 944 page = shmem_swapin(swap, gfp, info, index);
27ab7006 945 if (!page) {
41ffe5d5 946 swp_entry_t nswap = shmem_get_swap(info, index);
285b2c4f
HD
947 if (nswap.val == swap.val) {
948 error = -ENOMEM;
27ab7006 949 goto out;
285b2c4f 950 }
1da177e4
LT
951 goto repeat;
952 }
27ab7006
HD
953 wait_on_page_locked(page);
954 page_cache_release(page);
1da177e4
LT
955 goto repeat;
956 }
957
958 /* We have to do this with page locked to prevent races */
27ab7006 959 if (!trylock_page(page)) {
1da177e4 960 spin_unlock(&info->lock);
27ab7006
HD
961 wait_on_page_locked(page);
962 page_cache_release(page);
1da177e4
LT
963 goto repeat;
964 }
27ab7006 965 if (PageWriteback(page)) {
1da177e4 966 spin_unlock(&info->lock);
27ab7006
HD
967 wait_on_page_writeback(page);
968 unlock_page(page);
969 page_cache_release(page);
1da177e4
LT
970 goto repeat;
971 }
27ab7006 972 if (!PageUptodate(page)) {
1da177e4 973 spin_unlock(&info->lock);
27ab7006
HD
974 unlock_page(page);
975 page_cache_release(page);
1da177e4 976 error = -EIO;
27ab7006 977 goto out;
1da177e4
LT
978 }
979
27ab7006 980 error = add_to_page_cache_locked(page, mapping,
41ffe5d5 981 index, GFP_NOWAIT);
27ab7006 982 if (error) {
1da177e4 983 spin_unlock(&info->lock);
82369553 984 if (error == -ENOMEM) {
ae3abae6
DN
985 /*
986 * reclaim from proper memory cgroup and
987 * call memcg's OOM if needed.
988 */
989 error = mem_cgroup_shmem_charge_fallback(
27ab7006 990 page, current->mm, gfp);
b5a84319 991 if (error) {
27ab7006
HD
992 unlock_page(page);
993 page_cache_release(page);
994 goto out;
b5a84319 995 }
82369553 996 }
27ab7006
HD
997 unlock_page(page);
998 page_cache_release(page);
1da177e4
LT
999 goto repeat;
1000 }
27ab7006 1001
27ab7006 1002 delete_from_swap_cache(page);
41ffe5d5 1003 shmem_put_swap(info, index, (swp_entry_t){0});
285b2c4f 1004 info->swapped--;
27ab7006
HD
1005 spin_unlock(&info->lock);
1006 set_page_dirty(page);
1007 swap_free(swap);
1008
1009 } else if (sgp == SGP_READ) {
41ffe5d5 1010 page = find_get_page(mapping, index);
27ab7006 1011 if (page && !trylock_page(page)) {
1da177e4 1012 spin_unlock(&info->lock);
27ab7006
HD
1013 wait_on_page_locked(page);
1014 page_cache_release(page);
1da177e4
LT
1015 goto repeat;
1016 }
1017 spin_unlock(&info->lock);
e83c32e8
HD
1018
1019 } else if (prealloc_page) {
1da177e4 1020 sbinfo = SHMEM_SB(inode->i_sb);
0edd73b3 1021 if (sbinfo->max_blocks) {
fc5da22a
HD
1022 if (percpu_counter_compare(&sbinfo->used_blocks,
1023 sbinfo->max_blocks) >= 0 ||
59a16ead
HD
1024 shmem_acct_block(info->flags))
1025 goto nospace;
7e496299 1026 percpu_counter_inc(&sbinfo->used_blocks);
1da177e4 1027 inode->i_blocks += BLOCKS_PER_PAGE;
59a16ead
HD
1028 } else if (shmem_acct_block(info->flags))
1029 goto nospace;
1da177e4 1030
27ab7006
HD
1031 page = prealloc_page;
1032 prealloc_page = NULL;
1da177e4 1033
41ffe5d5 1034 swap = shmem_get_swap(info, index);
285b2c4f 1035 if (swap.val)
27ab7006
HD
1036 mem_cgroup_uncharge_cache_page(page);
1037 else
285b2c4f 1038 error = add_to_page_cache_lru(page, mapping,
41ffe5d5 1039 index, GFP_NOWAIT);
27ab7006
HD
1040 /*
1041 * At add_to_page_cache_lru() failure,
1042 * uncharge will be done automatically.
1043 */
285b2c4f 1044 if (swap.val || error) {
27ab7006
HD
1045 shmem_unacct_blocks(info->flags, 1);
1046 shmem_free_blocks(inode, 1);
1047 spin_unlock(&info->lock);
1048 page_cache_release(page);
27ab7006 1049 goto repeat;
1da177e4
LT
1050 }
1051
1052 info->alloced++;
1053 spin_unlock(&info->lock);
27ab7006
HD
1054 clear_highpage(page);
1055 flush_dcache_page(page);
1056 SetPageUptodate(page);
a0ee5ec5 1057 if (sgp == SGP_DIRTY)
27ab7006
HD
1058 set_page_dirty(page);
1059
e83c32e8
HD
1060 } else {
1061 spin_unlock(&info->lock);
1062 error = -ENOMEM;
1063 goto out;
1da177e4
LT
1064 }
1065done:
27ab7006 1066 *pagep = page;
ff36b801 1067 error = 0;
e83c32e8
HD
1068out:
1069 if (prealloc_page) {
1070 mem_cgroup_uncharge_cache_page(prealloc_page);
1071 page_cache_release(prealloc_page);
1072 }
1073 return error;
1da177e4 1074
59a16ead
HD
1075nospace:
1076 /*
1077 * Perhaps the page was brought in from swap between find_lock_page
1078 * and taking info->lock? We allow for that at add_to_page_cache_lru,
1079 * but must also avoid reporting a spurious ENOSPC while working on a
9276aad6 1080 * full tmpfs.
59a16ead 1081 */
41ffe5d5 1082 page = find_get_page(mapping, index);
59a16ead 1083 spin_unlock(&info->lock);
27ab7006
HD
1084 if (page) {
1085 page_cache_release(page);
1086 goto repeat;
ff36b801 1087 }
27ab7006 1088 error = -ENOSPC;
e83c32e8 1089 goto out;
1da177e4
LT
1090}
1091
d0217ac0 1092static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1da177e4 1093{
d3ac7f89 1094 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1da177e4 1095 int error;
68da9f05 1096 int ret = VM_FAULT_LOCKED;
1da177e4 1097
d0217ac0
NP
1098 if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1099 return VM_FAULT_SIGBUS;
d00806b1 1100
27d54b39 1101 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
d0217ac0
NP
1102 if (error)
1103 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
68da9f05 1104
456f998e
YH
1105 if (ret & VM_FAULT_MAJOR) {
1106 count_vm_event(PGMAJFAULT);
1107 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1108 }
68da9f05 1109 return ret;
1da177e4
LT
1110}
1111
1da177e4 1112#ifdef CONFIG_NUMA
41ffe5d5 1113static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
1da177e4 1114{
41ffe5d5
HD
1115 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1116 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
1da177e4
LT
1117}
1118
d8dc74f2
AB
1119static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1120 unsigned long addr)
1da177e4 1121{
41ffe5d5
HD
1122 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1123 pgoff_t index;
1da177e4 1124
41ffe5d5
HD
1125 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1126 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
1da177e4
LT
1127}
1128#endif
1129
1130int shmem_lock(struct file *file, int lock, struct user_struct *user)
1131{
d3ac7f89 1132 struct inode *inode = file->f_path.dentry->d_inode;
1da177e4
LT
1133 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;
89e004ea 1141 mapping_set_unevictable(file->f_mapping);
1da177e4
LT
1142 }
1143 if (!lock && (info->flags & VM_LOCKED) && user) {
1144 user_shm_unlock(inode->i_size, user);
1145 info->flags &= ~VM_LOCKED;
89e004ea
LS
1146 mapping_clear_unevictable(file->f_mapping);
1147 scan_mapping_unevictable_pages(file->f_mapping);
1da177e4
LT
1148 }
1149 retval = 0;
89e004ea 1150
1da177e4
LT
1151out_nomem:
1152 spin_unlock(&info->lock);
1153 return retval;
1154}
1155
9b83a6a8 1156static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1da177e4
LT
1157{
1158 file_accessed(file);
1159 vma->vm_ops = &shmem_vm_ops;
d0217ac0 1160 vma->vm_flags |= VM_CAN_NONLINEAR;
1da177e4
LT
1161 return 0;
1162}
1163
454abafe
DM
1164static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1165 int mode, dev_t dev, unsigned long flags)
1da177e4
LT
1166{
1167 struct inode *inode;
1168 struct shmem_inode_info *info;
1169 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1170
5b04c689
PE
1171 if (shmem_reserve_inode(sb))
1172 return NULL;
1da177e4
LT
1173
1174 inode = new_inode(sb);
1175 if (inode) {
85fe4025 1176 inode->i_ino = get_next_ino();
454abafe 1177 inode_init_owner(inode, dir, mode);
1da177e4 1178 inode->i_blocks = 0;
1da177e4
LT
1179 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1180 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
91828a40 1181 inode->i_generation = get_seconds();
1da177e4
LT
1182 info = SHMEM_I(inode);
1183 memset(info, 0, (char *)inode - (char *)info);
1184 spin_lock_init(&info->lock);
0b0a0806 1185 info->flags = flags & VM_NORESERVE;
1da177e4 1186 INIT_LIST_HEAD(&info->swaplist);
b09e0fa4 1187 INIT_LIST_HEAD(&info->xattr_list);
72c04902 1188 cache_no_acl(inode);
1da177e4
LT
1189
1190 switch (mode & S_IFMT) {
1191 default:
39f0247d 1192 inode->i_op = &shmem_special_inode_operations;
1da177e4
LT
1193 init_special_inode(inode, mode, dev);
1194 break;
1195 case S_IFREG:
14fcc23f 1196 inode->i_mapping->a_ops = &shmem_aops;
1da177e4
LT
1197 inode->i_op = &shmem_inode_operations;
1198 inode->i_fop = &shmem_file_operations;
71fe804b
LS
1199 mpol_shared_policy_init(&info->policy,
1200 shmem_get_sbmpol(sbinfo));
1da177e4
LT
1201 break;
1202 case S_IFDIR:
d8c76e6f 1203 inc_nlink(inode);
1da177e4
LT
1204 /* 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 */
71fe804b 1214 mpol_shared_policy_init(&info->policy, NULL);
1da177e4
LT
1215 break;
1216 }
5b04c689
PE
1217 } else
1218 shmem_free_inode(sb);
1da177e4
LT
1219 return inode;
1220}
1221
1222#ifdef CONFIG_TMPFS
92e1d5be
AV
1223static const struct inode_operations shmem_symlink_inode_operations;
1224static const struct inode_operations shmem_symlink_inline_operations;
1da177e4 1225
1da177e4 1226static int
800d15a5
NP
1227shmem_write_begin(struct file *file, struct address_space *mapping,
1228 loff_t pos, unsigned len, unsigned flags,
1229 struct page **pagep, void **fsdata)
1da177e4 1230{
800d15a5
NP
1231 struct inode *inode = mapping->host;
1232 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
800d15a5
NP
1233 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
d3602444
HD
1243 if (pos + copied > inode->i_size)
1244 i_size_write(inode, pos + copied);
1245
800d15a5 1246 set_page_dirty(page);
6746aff7 1247 unlock_page(page);
800d15a5
NP
1248 page_cache_release(page);
1249
800d15a5 1250 return copied;
1da177e4
LT
1251}
1252
1da177e4
LT
1253static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1254{
d3ac7f89 1255 struct inode *inode = filp->f_path.dentry->d_inode;
1da177e4 1256 struct address_space *mapping = inode->i_mapping;
41ffe5d5
HD
1257 pgoff_t index;
1258 unsigned long offset;
a0ee5ec5
HD
1259 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;
1da177e4
LT
1268
1269 index = *ppos >> PAGE_CACHE_SHIFT;
1270 offset = *ppos & ~PAGE_CACHE_MASK;
1271
1272 for (;;) {
1273 struct page *page = NULL;
41ffe5d5
HD
1274 pgoff_t end_index;
1275 unsigned long nr, ret;
1da177e4
LT
1276 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
a0ee5ec5 1287 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
1da177e4
LT
1288 if (desc->error) {
1289 if (desc->error == -EINVAL)
1290 desc->error = 0;
1291 break;
1292 }
d3602444
HD
1293 if (page)
1294 unlock_page(page);
1da177e4
LT
1295
1296 /*
1297 * We must evaluate after, since reads (unlike writes)
1b1dcc1b 1298 * are called without i_mutex protection against truncate
1da177e4
LT
1299 */
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);
b5810039 1326 } else {
1da177e4 1327 page = ZERO_PAGE(0);
b5810039
NP
1328 page_cache_get(page);
1329 }
1da177e4
LT
1330
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
bcd78e49
HD
1357static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1358 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
1359{
1360 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;
1365
1366 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1367 if (retval)
1368 return retval;
1369
1370 for (seg = 0; seg < nr_segs; seg++) {
1371 read_descriptor_t desc;
1372
1373 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;
1da177e4
LT
1389}
1390
708e3508
HD
1391static 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;
71f0e07a 1396 struct inode *inode = mapping->host;
708e3508
HD
1397 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
71f0e07a 1412 isize = i_size_read(inode);
708e3508
HD
1413 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
708e3508
HD
1428 spd.nr_pages = find_get_pages_contig(mapping, index,
1429 nr_pages, spd.pages);
1430 index += spd.nr_pages;
708e3508 1431 error = 0;
708e3508 1432
71f0e07a 1433 while (spd.nr_pages < nr_pages) {
71f0e07a
HD
1434 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1435 if (error)
1436 break;
1437 unlock_page(page);
708e3508
HD
1438 spd.pages[spd.nr_pages++] = page;
1439 index++;
1440 }
1441
708e3508
HD
1442 index = *ppos >> PAGE_CACHE_SHIFT;
1443 nr_pages = spd.nr_pages;
1444 spd.nr_pages = 0;
71f0e07a 1445
708e3508
HD
1446 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1447 unsigned int this_len;
1448
1449 if (!len)
1450 break;
1451
708e3508
HD
1452 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1453 page = spd.pages[page_nr];
1454
71f0e07a 1455 if (!PageUptodate(page) || page->mapping != mapping) {
71f0e07a
HD
1456 error = shmem_getpage(inode, index, &page,
1457 SGP_CACHE, NULL);
1458 if (error)
708e3508 1459 break;
71f0e07a
HD
1460 unlock_page(page);
1461 page_cache_release(spd.pages[page_nr]);
1462 spd.pages[page_nr] = page;
708e3508 1463 }
71f0e07a
HD
1464
1465 isize = i_size_read(inode);
708e3508
HD
1466 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1467 if (unlikely(!isize || index > end_index))
1468 break;
1469
708e3508
HD
1470 if (end_index == index) {
1471 unsigned int plen;
1472
708e3508
HD
1473 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1474 if (plen <= loff)
1475 break;
1476
708e3508
HD
1477 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
708e3508
HD
1489 while (page_nr < nr_pages)
1490 page_cache_release(spd.pages[page_nr++]);
708e3508
HD
1491
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
726c3342 1504static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 1505{
726c3342 1506 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1da177e4
LT
1507
1508 buf->f_type = TMPFS_MAGIC;
1509 buf->f_bsize = PAGE_CACHE_SIZE;
1510 buf->f_namelen = NAME_MAX;
0edd73b3 1511 if (sbinfo->max_blocks) {
1da177e4 1512 buf->f_blocks = sbinfo->max_blocks;
41ffe5d5
HD
1513 buf->f_bavail =
1514 buf->f_bfree = sbinfo->max_blocks -
1515 percpu_counter_sum(&sbinfo->used_blocks);
0edd73b3
HD
1516 }
1517 if (sbinfo->max_inodes) {
1da177e4
LT
1518 buf->f_files = sbinfo->max_inodes;
1519 buf->f_ffree = sbinfo->free_inodes;
1da177e4
LT
1520 }
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{
0b0a0806 1531 struct inode *inode;
1da177e4
LT
1532 int error = -ENOSPC;
1533
454abafe 1534 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
1da177e4 1535 if (inode) {
2a7dba39
EP
1536 error = security_inode_init_security(inode, dir,
1537 &dentry->d_name, NULL,
1538 NULL, NULL);
570bc1c2
SS
1539 if (error) {
1540 if (error != -EOPNOTSUPP) {
1541 iput(inode);
1542 return error;
1543 }
39f0247d 1544 }
1c7c474c
CH
1545#ifdef CONFIG_TMPFS_POSIX_ACL
1546 error = generic_acl_init(inode, dir);
39f0247d
AG
1547 if (error) {
1548 iput(inode);
1549 return error;
570bc1c2 1550 }
718deb6b
AV
1551#else
1552 error = 0;
1c7c474c 1553#endif
1da177e4
LT
1554 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 */
1da177e4
LT
1558 }
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;
d8c76e6f 1568 inc_nlink(dir);
1da177e4
LT
1569 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;
5b04c689 1584 int ret;
1da177e4
LT
1585
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 */
5b04c689
PE
1591 ret = shmem_reserve_inode(inode->i_sb);
1592 if (ret)
1593 goto out;
1da177e4
LT
1594
1595 dir->i_size += BOGO_DIRENT_SIZE;
1596 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
d8c76e6f 1597 inc_nlink(inode);
7de9c6ee 1598 ihold(inode); /* New dentry reference */
1da177e4
LT
1599 dget(dentry); /* Extra pinning count for the created dentry */
1600 d_instantiate(dentry, inode);
5b04c689
PE
1601out:
1602 return ret;
1da177e4
LT
1603}
1604
1605static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1606{
1607 struct inode *inode = dentry->d_inode;
1608
5b04c689
PE
1609 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1610 shmem_free_inode(inode->i_sb);
1da177e4
LT
1611
1612 dir->i_size -= BOGO_DIRENT_SIZE;
1613 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
9a53c3a7 1614 drop_nlink(inode);
1da177e4
LT
1615 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
9a53c3a7
DH
1624 drop_nlink(dentry->d_inode);
1625 drop_nlink(dir);
1da177e4
LT
1626 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)
9a53c3a7 1646 drop_nlink(old_dir);
1da177e4 1647 } else if (they_are_dirs) {
9a53c3a7 1648 drop_nlink(old_dir);
d8c76e6f 1649 inc_nlink(new_dir);
1da177e4
LT
1650 }
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;
9276aad6 1665 struct page *page;
1da177e4
LT
1666 char *kaddr;
1667 struct shmem_inode_info *info;
1668
1669 len = strlen(symname) + 1;
1670 if (len > PAGE_CACHE_SIZE)
1671 return -ENAMETOOLONG;
1672
454abafe 1673 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
1da177e4
LT
1674 if (!inode)
1675 return -ENOSPC;
1676
2a7dba39
EP
1677 error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
1678 NULL, NULL);
570bc1c2
SS
1679 if (error) {
1680 if (error != -EOPNOTSUPP) {
1681 iput(inode);
1682 return error;
1683 }
1684 error = 0;
1685 }
1686
1da177e4
LT
1687 info = SHMEM_I(inode);
1688 inode->i_size = len-1;
b09e0fa4 1689 if (len <= SHMEM_SYMLINK_INLINE_LEN) {
1da177e4 1690 /* do it inline */
b09e0fa4 1691 memcpy(info->inline_symlink, symname, len);
1da177e4
LT
1692 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 }
14fcc23f 1699 inode->i_mapping->a_ops = &shmem_aops;
1da177e4
LT
1700 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);
6746aff7 1705 unlock_page(page);
1da177e4
LT
1706 page_cache_release(page);
1707 }
1da177e4
LT
1708 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
cc314eef 1715static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
1da177e4 1716{
b09e0fa4 1717 nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
cc314eef 1718 return NULL;
1da177e4
LT
1719}
1720
cc314eef 1721static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
1722{
1723 struct page *page = NULL;
41ffe5d5
HD
1724 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1725 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
d3602444
HD
1726 if (page)
1727 unlock_page(page);
cc314eef 1728 return page;
1da177e4
LT
1729}
1730
cc314eef 1731static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4
LT
1732{
1733 if (!IS_ERR(nd_get_link(nd))) {
cc314eef 1734 struct page *page = cookie;
1da177e4
LT
1735 kunmap(page);
1736 mark_page_accessed(page);
1737 page_cache_release(page);
1da177e4
LT
1738 }
1739}
1740
b09e0fa4 1741#ifdef CONFIG_TMPFS_XATTR
46711810 1742/*
b09e0fa4
EP
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
39f0247d
AG
1745 * like ACLs, we also need to implement the security.* handlers at
1746 * filesystem level, though.
1747 */
1748
b09e0fa4
EP
1749static int shmem_xattr_get(struct dentry *dentry, const char *name,
1750 void *buffer, size_t size)
39f0247d 1751{
b09e0fa4
EP
1752 struct shmem_inode_info *info;
1753 struct shmem_xattr *xattr;
1754 int ret = -ENODATA;
39f0247d 1755
b09e0fa4
EP
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;
39f0247d
AG
1774}
1775
b09e0fa4
EP
1776static int shmem_xattr_set(struct dentry *dentry, const char *name,
1777 const void *value, size_t size, int flags)
39f0247d 1778{
b09e0fa4
EP
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;
39f0247d
AG
1834}
1835
bb435453 1836static const struct xattr_handler *shmem_xattr_handlers[] = {
b09e0fa4 1837#ifdef CONFIG_TMPFS_POSIX_ACL
1c7c474c
CH
1838 &generic_acl_access_handler,
1839 &generic_acl_default_handler,
b09e0fa4 1840#endif
39f0247d
AG
1841 NULL
1842};
b09e0fa4
EP
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
1965static const struct inode_operations shmem_symlink_inline_operations = {
1966 .readlink = generic_readlink,
1967 .follow_link = shmem_follow_link_inline,
1968#ifdef CONFIG_TMPFS_XATTR
1969 .setxattr = shmem_setxattr,
1970 .getxattr = shmem_getxattr,
1971 .listxattr = shmem_listxattr,
1972 .removexattr = shmem_removexattr,
1973#endif
1974};
1975
1976static const struct inode_operations shmem_symlink_inode_operations = {
1977 .readlink = generic_readlink,
1978 .follow_link = shmem_follow_link,
1979 .put_link = shmem_put_link,
1980#ifdef CONFIG_TMPFS_XATTR
1981 .setxattr = shmem_setxattr,
1982 .getxattr = shmem_getxattr,
1983 .listxattr = shmem_listxattr,
1984 .removexattr = shmem_removexattr,
39f0247d 1985#endif
b09e0fa4 1986};
39f0247d 1987
91828a40
DG
1988static 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
480b116c
CH
2001static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2002 struct fid *fid, int fh_len, int fh_type)
91828a40 2003{
91828a40 2004 struct inode *inode;
480b116c
CH
2005 struct dentry *dentry = NULL;
2006 u64 inum = fid->raw[2];
2007 inum = (inum << 32) | fid->raw[1];
2008
2009 if (fh_len < 3)
2010 return NULL;
91828a40 2011
480b116c
CH
2012 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2013 shmem_match, fid->raw);
91828a40 2014 if (inode) {
480b116c 2015 dentry = d_find_alias(inode);
91828a40
DG
2016 iput(inode);
2017 }
2018
480b116c 2019 return dentry;
91828a40
DG
2020}
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
5fe0c237
AK
2027 if (*len < 3) {
2028 *len = 3;
91828a40 2029 return 255;
5fe0c237 2030 }
91828a40 2031
1d3382cb 2032 if (inode_unhashed(inode)) {
91828a40
DG
2033 /* 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);
1d3382cb 2040 if (inode_unhashed(inode))
91828a40
DG
2041 __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
39655164 2054static const struct export_operations shmem_export_ops = {
91828a40 2055 .get_parent = shmem_get_parent,
91828a40 2056 .encode_fh = shmem_encode_fh,
480b116c 2057 .fh_to_dentry = shmem_fh_to_dentry,
91828a40
DG
2058};
2059
680d794b
AM
2060static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2061 bool remount)
1da177e4
LT
2062{
2063 char *this_char, *value, *rest;
2064
b00dc3ad
HD
2065 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 }
1da177e4
LT
2082 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;
680d794b
AM
2104 sbinfo->max_blocks =
2105 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
1da177e4 2106 } else if (!strcmp(this_char,"nr_blocks")) {
680d794b 2107 sbinfo->max_blocks = memparse(value, &rest);
1da177e4
LT
2108 if (*rest)
2109 goto bad_val;
2110 } else if (!strcmp(this_char,"nr_inodes")) {
680d794b 2111 sbinfo->max_inodes = memparse(value, &rest);
1da177e4
LT
2112 if (*rest)
2113 goto bad_val;
2114 } else if (!strcmp(this_char,"mode")) {
680d794b 2115 if (remount)
1da177e4 2116 continue;
680d794b 2117 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
1da177e4
LT
2118 if (*rest)
2119 goto bad_val;
2120 } else if (!strcmp(this_char,"uid")) {
680d794b 2121 if (remount)
1da177e4 2122 continue;
680d794b 2123 sbinfo->uid = simple_strtoul(value, &rest, 0);
1da177e4
LT
2124 if (*rest)
2125 goto bad_val;
2126 } else if (!strcmp(this_char,"gid")) {
680d794b 2127 if (remount)
1da177e4 2128 continue;
680d794b 2129 sbinfo->gid = simple_strtoul(value, &rest, 0);
1da177e4
LT
2130 if (*rest)
2131 goto bad_val;
7339ff83 2132 } else if (!strcmp(this_char,"mpol")) {
71fe804b 2133 if (mpol_parse_str(value, &sbinfo->mpol, 1))
7339ff83 2134 goto bad_val;
1da177e4
LT
2135 } 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);
680d794b 2153 struct shmem_sb_info config = *sbinfo;
0edd73b3
HD
2154 unsigned long inodes;
2155 int error = -EINVAL;
2156
680d794b 2157 if (shmem_parse_options(data, &config, true))
0edd73b3 2158 return error;
1da177e4 2159
0edd73b3 2160 spin_lock(&sbinfo->stat_lock);
0edd73b3 2161 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
7e496299 2162 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
0edd73b3 2163 goto out;
680d794b 2164 if (config.max_inodes < inodes)
0edd73b3
HD
2165 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 */
680d794b 2172 if (config.max_blocks && !sbinfo->max_blocks)
0edd73b3 2173 goto out;
680d794b 2174 if (config.max_inodes && !sbinfo->max_inodes)
0edd73b3
HD
2175 goto out;
2176
2177 error = 0;
680d794b 2178 sbinfo->max_blocks = config.max_blocks;
680d794b
AM
2179 sbinfo->max_inodes = config.max_inodes;
2180 sbinfo->free_inodes = config.max_inodes - inodes;
71fe804b
LS
2181
2182 mpol_put(sbinfo->mpol);
2183 sbinfo->mpol = config.mpol; /* transfers initial ref */
0edd73b3
HD
2184out:
2185 spin_unlock(&sbinfo->stat_lock);
2186 return error;
1da177e4 2187}
680d794b
AM
2188
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);
71fe804b 2204 shmem_show_mpol(seq, sbinfo->mpol);
680d794b
AM
2205 return 0;
2206}
2207#endif /* CONFIG_TMPFS */
1da177e4
LT
2208
2209static void shmem_put_super(struct super_block *sb)
2210{
602586a8
HD
2211 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2212
2213 percpu_counter_destroy(&sbinfo->used_blocks);
2214 kfree(sbinfo);
1da177e4
LT
2215 sb->s_fs_info = NULL;
2216}
2217
2b2af54a 2218int shmem_fill_super(struct super_block *sb, void *data, int silent)
1da177e4
LT
2219{
2220 struct inode *inode;
2221 struct dentry *root;
0edd73b3 2222 struct shmem_sb_info *sbinfo;
680d794b
AM
2223 int err = -ENOMEM;
2224
2225 /* Round up to L1_CACHE_BYTES to resist false sharing */
425fbf04 2226 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
680d794b
AM
2227 L1_CACHE_BYTES), GFP_KERNEL);
2228 if (!sbinfo)
2229 return -ENOMEM;
2230
680d794b 2231 sbinfo->mode = S_IRWXUGO | S_ISVTX;
76aac0e9
DH
2232 sbinfo->uid = current_fsuid();
2233 sbinfo->gid = current_fsgid();
680d794b 2234 sb->s_fs_info = sbinfo;
1da177e4 2235
0edd73b3 2236#ifdef CONFIG_TMPFS
1da177e4
LT
2237 /*
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)) {
680d794b
AM
2243 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 }
1da177e4 2249 }
91828a40 2250 sb->s_export_op = &shmem_export_ops;
1da177e4
LT
2251#else
2252 sb->s_flags |= MS_NOUSER;
2253#endif
2254
0edd73b3 2255 spin_lock_init(&sbinfo->stat_lock);
602586a8
HD
2256 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2257 goto failed;
680d794b 2258 sbinfo->free_inodes = sbinfo->max_inodes;
0edd73b3 2259
285b2c4f 2260 sb->s_maxbytes = MAX_LFS_FILESIZE;
1da177e4
LT
2261 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;
cfd95a9c 2265 sb->s_time_gran = 1;
b09e0fa4 2266#ifdef CONFIG_TMPFS_XATTR
39f0247d 2267 sb->s_xattr = shmem_xattr_handlers;
b09e0fa4
EP
2268#endif
2269#ifdef CONFIG_TMPFS_POSIX_ACL
39f0247d
AG
2270 sb->s_flags |= MS_POSIXACL;
2271#endif
0edd73b3 2272
454abafe 2273 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
1da177e4
LT
2274 if (!inode)
2275 goto failed;
680d794b
AM
2276 inode->i_uid = sbinfo->uid;
2277 inode->i_gid = sbinfo->gid;
1da177e4
LT
2278 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
fcc234f8 2291static struct kmem_cache *shmem_inode_cachep;
1da177e4
LT
2292
2293static struct inode *shmem_alloc_inode(struct super_block *sb)
2294{
41ffe5d5
HD
2295 struct shmem_inode_info *info;
2296 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2297 if (!info)
1da177e4 2298 return NULL;
41ffe5d5 2299 return &info->vfs_inode;
1da177e4
LT
2300}
2301
41ffe5d5 2302static void shmem_destroy_callback(struct rcu_head *head)
fa0d7e3d
NP
2303{
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
1da177e4
LT
2309static 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 }
41ffe5d5 2315 call_rcu(&inode->i_rcu, shmem_destroy_callback);
1da177e4
LT
2316}
2317
41ffe5d5 2318static void shmem_init_inode(void *foo)
1da177e4 2319{
41ffe5d5
HD
2320 struct shmem_inode_info *info = foo;
2321 inode_init_once(&info->vfs_inode);
1da177e4
LT
2322}
2323
41ffe5d5 2324static int shmem_init_inodecache(void)
1da177e4
LT
2325{
2326 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2327 sizeof(struct shmem_inode_info),
41ffe5d5 2328 0, SLAB_PANIC, shmem_init_inode);
1da177e4
LT
2329 return 0;
2330}
2331
41ffe5d5 2332static void shmem_destroy_inodecache(void)
1da177e4 2333{
1a1d92c1 2334 kmem_cache_destroy(shmem_inode_cachep);
1da177e4
LT
2335}
2336
f5e54d6e 2337static const struct address_space_operations shmem_aops = {
1da177e4 2338 .writepage = shmem_writepage,
76719325 2339 .set_page_dirty = __set_page_dirty_no_writeback,
1da177e4 2340#ifdef CONFIG_TMPFS
800d15a5
NP
2341 .write_begin = shmem_write_begin,
2342 .write_end = shmem_write_end,
1da177e4 2343#endif
304dbdb7 2344 .migratepage = migrate_page,
aa261f54 2345 .error_remove_page = generic_error_remove_page,
1da177e4
LT
2346};
2347
15ad7cdc 2348static const struct file_operations shmem_file_operations = {
1da177e4
LT
2349 .mmap = shmem_mmap,
2350#ifdef CONFIG_TMPFS
2351 .llseek = generic_file_llseek,
bcd78e49 2352 .read = do_sync_read,
5402b976 2353 .write = do_sync_write,
bcd78e49 2354 .aio_read = shmem_file_aio_read,
5402b976 2355 .aio_write = generic_file_aio_write,
1b061d92 2356 .fsync = noop_fsync,
708e3508 2357 .splice_read = shmem_file_splice_read,
ae976416 2358 .splice_write = generic_file_splice_write,
1da177e4
LT
2359#endif
2360};
2361
92e1d5be 2362static const struct inode_operations shmem_inode_operations = {
94c1e62d 2363 .setattr = shmem_setattr,
f6b3ec23 2364 .truncate_range = shmem_truncate_range,
b09e0fa4
EP
2365#ifdef CONFIG_TMPFS_XATTR
2366 .setxattr = shmem_setxattr,
2367 .getxattr = shmem_getxattr,
2368 .listxattr = shmem_listxattr,
2369 .removexattr = shmem_removexattr,
2370#endif
1da177e4
LT
2371};
2372
92e1d5be 2373static const struct inode_operations shmem_dir_inode_operations = {
1da177e4
LT
2374#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,
1da177e4 2384#endif
b09e0fa4
EP
2385#ifdef CONFIG_TMPFS_XATTR
2386 .setxattr = shmem_setxattr,
2387 .getxattr = shmem_getxattr,
2388 .listxattr = shmem_listxattr,
2389 .removexattr = shmem_removexattr,
2390#endif
39f0247d 2391#ifdef CONFIG_TMPFS_POSIX_ACL
94c1e62d 2392 .setattr = shmem_setattr,
39f0247d
AG
2393#endif
2394};
2395
92e1d5be 2396static const struct inode_operations shmem_special_inode_operations = {
b09e0fa4
EP
2397#ifdef CONFIG_TMPFS_XATTR
2398 .setxattr = shmem_setxattr,
2399 .getxattr = shmem_getxattr,
2400 .listxattr = shmem_listxattr,
2401 .removexattr = shmem_removexattr,
2402#endif
39f0247d 2403#ifdef CONFIG_TMPFS_POSIX_ACL
94c1e62d 2404 .setattr = shmem_setattr,
39f0247d 2405#endif
1da177e4
LT
2406};
2407
759b9775 2408static const struct super_operations shmem_ops = {
1da177e4
LT
2409 .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,
680d794b 2414 .show_options = shmem_show_options,
1da177e4 2415#endif
1f895f75 2416 .evict_inode = shmem_evict_inode,
1da177e4
LT
2417 .drop_inode = generic_delete_inode,
2418 .put_super = shmem_put_super,
2419};
2420
f0f37e2f 2421static const struct vm_operations_struct shmem_vm_ops = {
54cb8821 2422 .fault = shmem_fault,
1da177e4
LT
2423#ifdef CONFIG_NUMA
2424 .set_policy = shmem_set_policy,
2425 .get_policy = shmem_get_policy,
2426#endif
2427};
2428
3c26ff6e
AV
2429static struct dentry *shmem_mount(struct file_system_type *fs_type,
2430 int flags, const char *dev_name, void *data)
1da177e4 2431{
3c26ff6e 2432 return mount_nodev(fs_type, flags, data, shmem_fill_super);
1da177e4
LT
2433}
2434
41ffe5d5 2435static struct file_system_type shmem_fs_type = {
1da177e4
LT
2436 .owner = THIS_MODULE,
2437 .name = "tmpfs",
3c26ff6e 2438 .mount = shmem_mount,
1da177e4
LT
2439 .kill_sb = kill_litter_super,
2440};
1da177e4 2441
41ffe5d5 2442int __init shmem_init(void)
1da177e4
LT
2443{
2444 int error;
2445
e0bf68dd
PZ
2446 error = bdi_init(&shmem_backing_dev_info);
2447 if (error)
2448 goto out4;
2449
41ffe5d5 2450 error = shmem_init_inodecache();
1da177e4
LT
2451 if (error)
2452 goto out3;
2453
41ffe5d5 2454 error = register_filesystem(&shmem_fs_type);
1da177e4
LT
2455 if (error) {
2456 printk(KERN_ERR "Could not register tmpfs\n");
2457 goto out2;
2458 }
95dc112a 2459
41ffe5d5
HD
2460 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2461 shmem_fs_type.name, NULL);
1da177e4
LT
2462 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:
41ffe5d5 2470 unregister_filesystem(&shmem_fs_type);
1da177e4 2471out2:
41ffe5d5 2472 shmem_destroy_inodecache();
1da177e4 2473out3:
e0bf68dd
PZ
2474 bdi_destroy(&shmem_backing_dev_info);
2475out4:
1da177e4
LT
2476 shm_mnt = ERR_PTR(error);
2477 return error;
2478}
853ac43a 2479
87946a72
DN
2480#ifdef CONFIG_CGROUP_MEM_RES_CTLR
2481/**
41ffe5d5 2482 * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file
87946a72 2483 * @inode: the inode to be searched
41ffe5d5 2484 * @index: the page offset to be searched
87946a72 2485 * @pagep: the pointer for the found page to be stored
41ffe5d5 2486 * @swapp: the pointer for the found swap entry to be stored
87946a72
DN
2487 *
2488 * If a page is found, refcount of it is incremented. Callers should handle
2489 * these refcount.
2490 */
41ffe5d5
HD
2491void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index,
2492 struct page **pagep, swp_entry_t *swapp)
87946a72 2493{
87946a72 2494 struct shmem_inode_info *info = SHMEM_I(inode);
41ffe5d5
HD
2495 struct page *page = NULL;
2496 swp_entry_t swap = {0};
87946a72 2497
41ffe5d5 2498 if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
87946a72
DN
2499 goto out;
2500
2501 spin_lock(&info->lock);
87946a72 2502#ifdef CONFIG_SWAP
41ffe5d5
HD
2503 swap = shmem_get_swap(info, index);
2504 if (swap.val)
2505 page = find_get_page(&swapper_space, swap.val);
285b2c4f 2506 else
87946a72 2507#endif
41ffe5d5 2508 page = find_get_page(inode->i_mapping, index);
87946a72
DN
2509 spin_unlock(&info->lock);
2510out:
2511 *pagep = page;
41ffe5d5 2512 *swapp = swap;
87946a72
DN
2513}
2514#endif
2515
853ac43a
MM
2516#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
41ffe5d5 2529static struct file_system_type shmem_fs_type = {
853ac43a 2530 .name = "tmpfs",
3c26ff6e 2531 .mount = ramfs_mount,
853ac43a
MM
2532 .kill_sb = kill_litter_super,
2533};
2534
41ffe5d5 2535int __init shmem_init(void)
853ac43a 2536{
41ffe5d5 2537 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
853ac43a 2538
41ffe5d5 2539 shm_mnt = kern_mount(&shmem_fs_type);
853ac43a
MM
2540 BUG_ON(IS_ERR(shm_mnt));
2541
2542 return 0;
2543}
2544
41ffe5d5 2545int shmem_unuse(swp_entry_t swap, struct page *page)
853ac43a
MM
2546{
2547 return 0;
2548}
2549
3f96b79a
HD
2550int shmem_lock(struct file *file, int lock, struct user_struct *user)
2551{
2552 return 0;
2553}
2554
41ffe5d5 2555void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
94c1e62d 2556{
41ffe5d5 2557 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
94c1e62d
HD
2558}
2559EXPORT_SYMBOL_GPL(shmem_truncate_range);
2560
87946a72
DN
2561#ifdef CONFIG_CGROUP_MEM_RES_CTLR
2562/**
41ffe5d5 2563 * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file
87946a72 2564 * @inode: the inode to be searched
41ffe5d5 2565 * @index: the page offset to be searched
87946a72 2566 * @pagep: the pointer for the found page to be stored
41ffe5d5 2567 * @swapp: the pointer for the found swap entry to be stored
87946a72
DN
2568 *
2569 * If a page is found, refcount of it is incremented. Callers should handle
2570 * these refcount.
2571 */
41ffe5d5
HD
2572void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index,
2573 struct page **pagep, swp_entry_t *swapp)
87946a72
DN
2574{
2575 struct page *page = NULL;
2576
41ffe5d5 2577 if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
87946a72 2578 goto out;
41ffe5d5 2579 page = find_get_page(inode->i_mapping, index);
87946a72
DN
2580out:
2581 *pagep = page;
41ffe5d5 2582 *swapp = (swp_entry_t){0};
87946a72
DN
2583}
2584#endif
2585
0b0a0806
HD
2586#define shmem_vm_ops generic_file_vm_ops
2587#define shmem_file_operations ramfs_file_operations
454abafe 2588#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
0b0a0806
HD
2589#define shmem_acct_size(flags, size) 0
2590#define shmem_unacct_size(flags, size) do {} while (0)
853ac43a
MM
2591
2592#endif /* CONFIG_SHMEM */
2593
2594/* common code */
1da177e4 2595
46711810 2596/**
1da177e4 2597 * shmem_file_setup - get an unlinked file living in tmpfs
1da177e4
LT
2598 * @name: name for dentry (to be seen in /proc/<pid>/maps
2599 * @size: size to be set for the file
0b0a0806 2600 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
1da177e4 2601 */
168f5ac6 2602struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
1da177e4
LT
2603{
2604 int error;
2605 struct file *file;
2606 struct inode *inode;
2c48b9c4
AV
2607 struct path path;
2608 struct dentry *root;
1da177e4
LT
2609 struct qstr this;
2610
2611 if (IS_ERR(shm_mnt))
2612 return (void *)shm_mnt;
2613
285b2c4f 2614 if (size < 0 || size > MAX_LFS_FILESIZE)
1da177e4
LT
2615 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;
2c48b9c4
AV
2625 path.dentry = d_alloc(root, &this);
2626 if (!path.dentry)
1da177e4 2627 goto put_memory;
2c48b9c4 2628 path.mnt = mntget(shm_mnt);
1da177e4 2629
1da177e4 2630 error = -ENOSPC;
454abafe 2631 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
1da177e4 2632 if (!inode)
4b42af81 2633 goto put_dentry;
1da177e4 2634
2c48b9c4 2635 d_instantiate(path.dentry, inode);
1da177e4
LT
2636 inode->i_size = size;
2637 inode->i_nlink = 0; /* It is unlinked */
853ac43a
MM
2638#ifndef CONFIG_MMU
2639 error = ramfs_nommu_expand_for_mapping(inode, size);
2640 if (error)
4b42af81 2641 goto put_dentry;
853ac43a 2642#endif
4b42af81
AV
2643
2644 error = -ENFILE;
2c48b9c4 2645 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
4b42af81
AV
2646 &shmem_file_operations);
2647 if (!file)
2648 goto put_dentry;
2649
1da177e4
LT
2650 return file;
2651
1da177e4 2652put_dentry:
2c48b9c4 2653 path_put(&path);
1da177e4
LT
2654put_memory:
2655 shmem_unacct_size(flags, size);
2656 return ERR_PTR(error);
2657}
395e0ddc 2658EXPORT_SYMBOL_GPL(shmem_file_setup);
1da177e4 2659
46711810 2660/**
1da177e4 2661 * shmem_zero_setup - setup a shared anonymous mapping
1da177e4
LT
2662 * @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;
bee4c36a 2677 vma->vm_flags |= VM_CAN_NONLINEAR;
1da177e4
LT
2678 return 0;
2679}
d9d90e5e
HD
2680
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 *
68da9f05
HD
2693 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2694 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
d9d90e5e
HD
2695 */
2696struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2697 pgoff_t index, gfp_t gfp)
2698{
68da9f05
HD
2699#ifdef CONFIG_SHMEM
2700 struct inode *inode = mapping->host;
9276aad6 2701 struct page *page;
68da9f05
HD
2702 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 */
d9d90e5e 2715 return read_cache_page_gfp(mapping, index, gfp);
68da9f05 2716#endif
d9d90e5e
HD
2717}
2718EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);