tmpfs: undo fallocation on failure
[GitHub/mt8127/android_kernel_alcatel_ttab.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.
6922c0c7
HD
9 * Copyright (C) 2002-2011 Hugh Dickins.
10 * Copyright (C) 2011 Google Inc.
0edd73b3 11 * Copyright (C) 2002-2005 VERITAS Software Corporation.
1da177e4
LT
12 * Copyright (C) 2004 Andi Kleen, SuSE Labs
13 *
14 * Extended attribute support for tmpfs:
15 * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
16 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
17 *
853ac43a
MM
18 * tiny-shmem:
19 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
20 *
1da177e4
LT
21 * This file is released under the GPL.
22 */
23
853ac43a
MM
24#include <linux/fs.h>
25#include <linux/init.h>
26#include <linux/vfs.h>
27#include <linux/mount.h>
caefba17 28#include <linux/pagemap.h>
853ac43a
MM
29#include <linux/file.h>
30#include <linux/mm.h>
b95f1b31 31#include <linux/export.h>
853ac43a
MM
32#include <linux/swap.h>
33
34static struct vfsmount *shm_mnt;
35
36#ifdef CONFIG_SHMEM
1da177e4
LT
37/*
38 * This virtual memory filesystem is heavily based on the ramfs. It
39 * extends ramfs by the ability to use swap and honor resource limits
40 * which makes it a completely usable filesystem.
41 */
42
39f0247d 43#include <linux/xattr.h>
a5694255 44#include <linux/exportfs.h>
1c7c474c 45#include <linux/posix_acl.h>
39f0247d 46#include <linux/generic_acl.h>
1da177e4 47#include <linux/mman.h>
1da177e4
LT
48#include <linux/string.h>
49#include <linux/slab.h>
50#include <linux/backing-dev.h>
51#include <linux/shmem_fs.h>
1da177e4 52#include <linux/writeback.h>
1da177e4 53#include <linux/blkdev.h>
bda97eab 54#include <linux/pagevec.h>
41ffe5d5 55#include <linux/percpu_counter.h>
83e4fa9c 56#include <linux/falloc.h>
708e3508 57#include <linux/splice.h>
1da177e4
LT
58#include <linux/security.h>
59#include <linux/swapops.h>
60#include <linux/mempolicy.h>
61#include <linux/namei.h>
b00dc3ad 62#include <linux/ctype.h>
304dbdb7 63#include <linux/migrate.h>
c1f60a5a 64#include <linux/highmem.h>
680d794b 65#include <linux/seq_file.h>
92562927 66#include <linux/magic.h>
304dbdb7 67
1da177e4 68#include <asm/uaccess.h>
1da177e4
LT
69#include <asm/pgtable.h>
70
caefba17 71#define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
1da177e4
LT
72#define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
73
1da177e4
LT
74/* Pretend that each entry is of this size in directory's i_size */
75#define BOGO_DIRENT_SIZE 20
76
69f07ec9
HD
77/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
78#define SHORT_SYMLINK_LEN 128
79
b09e0fa4
EP
80struct shmem_xattr {
81 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
82 char *name; /* xattr name */
83 size_t size;
84 char value[0];
85};
86
285b2c4f 87/* Flag allocation requirements to shmem_getpage */
1da177e4 88enum sgp_type {
1da177e4
LT
89 SGP_READ, /* don't exceed i_size, don't allocate page */
90 SGP_CACHE, /* don't exceed i_size, may allocate page */
a0ee5ec5 91 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
1635f6a7
HD
92 SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
93 SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
1da177e4
LT
94};
95
b76db735 96#ifdef CONFIG_TMPFS
680d794b 97static unsigned long shmem_default_max_blocks(void)
98{
99 return totalram_pages / 2;
100}
101
102static unsigned long shmem_default_max_inodes(void)
103{
104 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
105}
b76db735 106#endif
680d794b 107
bde05d1c
HD
108static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
109static int shmem_replace_page(struct page **pagep, gfp_t gfp,
110 struct shmem_inode_info *info, pgoff_t index);
68da9f05
HD
111static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
112 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
113
114static inline int shmem_getpage(struct inode *inode, pgoff_t index,
115 struct page **pagep, enum sgp_type sgp, int *fault_type)
116{
117 return shmem_getpage_gfp(inode, index, pagep, sgp,
118 mapping_gfp_mask(inode->i_mapping), fault_type);
119}
1da177e4 120
1da177e4
LT
121static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
122{
123 return sb->s_fs_info;
124}
125
126/*
127 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
128 * for shared memory and for shared anonymous (/dev/zero) mappings
129 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
130 * consistent with the pre-accounting of private mappings ...
131 */
132static inline int shmem_acct_size(unsigned long flags, loff_t size)
133{
0b0a0806 134 return (flags & VM_NORESERVE) ?
191c5424 135 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
1da177e4
LT
136}
137
138static inline void shmem_unacct_size(unsigned long flags, loff_t size)
139{
0b0a0806 140 if (!(flags & VM_NORESERVE))
1da177e4
LT
141 vm_unacct_memory(VM_ACCT(size));
142}
143
144/*
145 * ... whereas tmpfs objects are accounted incrementally as
146 * pages are allocated, in order to allow huge sparse files.
147 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
148 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
149 */
150static inline int shmem_acct_block(unsigned long flags)
151{
0b0a0806 152 return (flags & VM_NORESERVE) ?
191c5424 153 security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
1da177e4
LT
154}
155
156static inline void shmem_unacct_blocks(unsigned long flags, long pages)
157{
0b0a0806 158 if (flags & VM_NORESERVE)
1da177e4
LT
159 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
160}
161
759b9775 162static const struct super_operations shmem_ops;
f5e54d6e 163static const struct address_space_operations shmem_aops;
15ad7cdc 164static const struct file_operations shmem_file_operations;
92e1d5be
AV
165static const struct inode_operations shmem_inode_operations;
166static const struct inode_operations shmem_dir_inode_operations;
167static const struct inode_operations shmem_special_inode_operations;
f0f37e2f 168static const struct vm_operations_struct shmem_vm_ops;
1da177e4 169
6c231b7b 170static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
1da177e4 171 .ra_pages = 0, /* No readahead */
4f98a2fe 172 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
1da177e4
LT
173};
174
175static LIST_HEAD(shmem_swaplist);
cb5f7b9a 176static DEFINE_MUTEX(shmem_swaplist_mutex);
1da177e4 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) {
54af6042
HD
222 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
223 if (sbinfo->max_blocks)
224 percpu_counter_add(&sbinfo->used_blocks, -freed);
1da177e4 225 info->alloced -= freed;
54af6042 226 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
1da177e4 227 shmem_unacct_blocks(info->flags, freed);
1da177e4
LT
228 }
229}
230
7a5d0fbb
HD
231/*
232 * Replace item expected in radix tree by a new item, while holding tree lock.
233 */
234static int shmem_radix_tree_replace(struct address_space *mapping,
235 pgoff_t index, void *expected, void *replacement)
236{
237 void **pslot;
238 void *item = NULL;
239
240 VM_BUG_ON(!expected);
241 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
242 if (pslot)
243 item = radix_tree_deref_slot_protected(pslot,
244 &mapping->tree_lock);
245 if (item != expected)
246 return -ENOENT;
247 if (replacement)
248 radix_tree_replace_slot(pslot, replacement);
249 else
250 radix_tree_delete(&mapping->page_tree, index);
251 return 0;
252}
253
46f65ec1
HD
254/*
255 * Like add_to_page_cache_locked, but error if expected item has gone.
256 */
257static int shmem_add_to_page_cache(struct page *page,
258 struct address_space *mapping,
259 pgoff_t index, gfp_t gfp, void *expected)
260{
aa3b1895 261 int error = 0;
46f65ec1
HD
262
263 VM_BUG_ON(!PageLocked(page));
264 VM_BUG_ON(!PageSwapBacked(page));
265
46f65ec1
HD
266 if (!expected)
267 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
268 if (!error) {
269 page_cache_get(page);
270 page->mapping = mapping;
271 page->index = index;
272
273 spin_lock_irq(&mapping->tree_lock);
274 if (!expected)
275 error = radix_tree_insert(&mapping->page_tree,
276 index, page);
277 else
278 error = shmem_radix_tree_replace(mapping, index,
279 expected, page);
280 if (!error) {
281 mapping->nrpages++;
282 __inc_zone_page_state(page, NR_FILE_PAGES);
283 __inc_zone_page_state(page, NR_SHMEM);
284 spin_unlock_irq(&mapping->tree_lock);
285 } else {
286 page->mapping = NULL;
287 spin_unlock_irq(&mapping->tree_lock);
288 page_cache_release(page);
289 }
290 if (!expected)
291 radix_tree_preload_end();
292 }
293 if (error)
294 mem_cgroup_uncharge_cache_page(page);
46f65ec1
HD
295 return error;
296}
297
6922c0c7
HD
298/*
299 * Like delete_from_page_cache, but substitutes swap for page.
300 */
301static void shmem_delete_from_page_cache(struct page *page, void *radswap)
302{
303 struct address_space *mapping = page->mapping;
304 int error;
305
306 spin_lock_irq(&mapping->tree_lock);
307 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
308 page->mapping = NULL;
309 mapping->nrpages--;
310 __dec_zone_page_state(page, NR_FILE_PAGES);
311 __dec_zone_page_state(page, NR_SHMEM);
312 spin_unlock_irq(&mapping->tree_lock);
313 page_cache_release(page);
314 BUG_ON(error);
315}
316
7a5d0fbb
HD
317/*
318 * Like find_get_pages, but collecting swap entries as well as pages.
319 */
320static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
321 pgoff_t start, unsigned int nr_pages,
322 struct page **pages, pgoff_t *indices)
323{
324 unsigned int i;
325 unsigned int ret;
326 unsigned int nr_found;
327
328 rcu_read_lock();
329restart:
330 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
331 (void ***)pages, indices, start, nr_pages);
332 ret = 0;
333 for (i = 0; i < nr_found; i++) {
334 struct page *page;
335repeat:
336 page = radix_tree_deref_slot((void **)pages[i]);
337 if (unlikely(!page))
338 continue;
339 if (radix_tree_exception(page)) {
8079b1c8
HD
340 if (radix_tree_deref_retry(page))
341 goto restart;
342 /*
343 * Otherwise, we must be storing a swap entry
344 * here as an exceptional entry: so return it
345 * without attempting to raise page count.
346 */
347 goto export;
7a5d0fbb
HD
348 }
349 if (!page_cache_get_speculative(page))
350 goto repeat;
351
352 /* Has the page moved? */
353 if (unlikely(page != *((void **)pages[i]))) {
354 page_cache_release(page);
355 goto repeat;
356 }
357export:
358 indices[ret] = indices[i];
359 pages[ret] = page;
360 ret++;
361 }
362 if (unlikely(!ret && nr_found))
363 goto restart;
364 rcu_read_unlock();
365 return ret;
366}
367
368/*
369 * Remove swap entry from radix tree, free the swap and its page cache.
370 */
371static int shmem_free_swap(struct address_space *mapping,
372 pgoff_t index, void *radswap)
373{
374 int error;
375
376 spin_lock_irq(&mapping->tree_lock);
377 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
378 spin_unlock_irq(&mapping->tree_lock);
379 if (!error)
380 free_swap_and_cache(radix_to_swp_entry(radswap));
381 return error;
382}
383
384/*
385 * Pagevec may contain swap entries, so shuffle up pages before releasing.
386 */
24513264 387static void shmem_deswap_pagevec(struct pagevec *pvec)
7a5d0fbb
HD
388{
389 int i, j;
390
391 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
392 struct page *page = pvec->pages[i];
393 if (!radix_tree_exceptional_entry(page))
394 pvec->pages[j++] = page;
395 }
396 pvec->nr = j;
24513264
HD
397}
398
399/*
400 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
401 */
402void shmem_unlock_mapping(struct address_space *mapping)
403{
404 struct pagevec pvec;
405 pgoff_t indices[PAGEVEC_SIZE];
406 pgoff_t index = 0;
407
408 pagevec_init(&pvec, 0);
409 /*
410 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
411 */
412 while (!mapping_unevictable(mapping)) {
413 /*
414 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
415 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
416 */
417 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
418 PAGEVEC_SIZE, pvec.pages, indices);
419 if (!pvec.nr)
420 break;
421 index = indices[pvec.nr - 1] + 1;
422 shmem_deswap_pagevec(&pvec);
423 check_move_unevictable_pages(pvec.pages, pvec.nr);
424 pagevec_release(&pvec);
425 cond_resched();
426 }
7a5d0fbb
HD
427}
428
429/*
430 * Remove range of pages and swap entries from radix tree, and free them.
1635f6a7 431 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
7a5d0fbb 432 */
1635f6a7
HD
433static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
434 bool unfalloc)
1da177e4 435{
285b2c4f 436 struct address_space *mapping = inode->i_mapping;
1da177e4 437 struct shmem_inode_info *info = SHMEM_I(inode);
285b2c4f 438 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
83e4fa9c
HD
439 pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
440 unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
441 unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
bda97eab 442 struct pagevec pvec;
7a5d0fbb
HD
443 pgoff_t indices[PAGEVEC_SIZE];
444 long nr_swaps_freed = 0;
285b2c4f 445 pgoff_t index;
bda97eab
HD
446 int i;
447
83e4fa9c
HD
448 if (lend == -1)
449 end = -1; /* unsigned, so actually very big */
bda97eab
HD
450
451 pagevec_init(&pvec, 0);
452 index = start;
83e4fa9c 453 while (index < end) {
7a5d0fbb 454 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
83e4fa9c 455 min(end - index, (pgoff_t)PAGEVEC_SIZE),
7a5d0fbb
HD
456 pvec.pages, indices);
457 if (!pvec.nr)
458 break;
bda97eab
HD
459 mem_cgroup_uncharge_start();
460 for (i = 0; i < pagevec_count(&pvec); i++) {
461 struct page *page = pvec.pages[i];
462
7a5d0fbb 463 index = indices[i];
83e4fa9c 464 if (index >= end)
bda97eab
HD
465 break;
466
7a5d0fbb 467 if (radix_tree_exceptional_entry(page)) {
1635f6a7
HD
468 if (unfalloc)
469 continue;
7a5d0fbb
HD
470 nr_swaps_freed += !shmem_free_swap(mapping,
471 index, page);
bda97eab 472 continue;
7a5d0fbb
HD
473 }
474
475 if (!trylock_page(page))
bda97eab 476 continue;
1635f6a7
HD
477 if (!unfalloc || !PageUptodate(page)) {
478 if (page->mapping == mapping) {
479 VM_BUG_ON(PageWriteback(page));
480 truncate_inode_page(mapping, page);
481 }
bda97eab 482 }
bda97eab
HD
483 unlock_page(page);
484 }
24513264
HD
485 shmem_deswap_pagevec(&pvec);
486 pagevec_release(&pvec);
bda97eab
HD
487 mem_cgroup_uncharge_end();
488 cond_resched();
489 index++;
490 }
1da177e4 491
83e4fa9c 492 if (partial_start) {
bda97eab
HD
493 struct page *page = NULL;
494 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
495 if (page) {
83e4fa9c
HD
496 unsigned int top = PAGE_CACHE_SIZE;
497 if (start > end) {
498 top = partial_end;
499 partial_end = 0;
500 }
501 zero_user_segment(page, partial_start, top);
502 set_page_dirty(page);
503 unlock_page(page);
504 page_cache_release(page);
505 }
506 }
507 if (partial_end) {
508 struct page *page = NULL;
509 shmem_getpage(inode, end, &page, SGP_READ, NULL);
510 if (page) {
511 zero_user_segment(page, 0, partial_end);
bda97eab
HD
512 set_page_dirty(page);
513 unlock_page(page);
514 page_cache_release(page);
515 }
516 }
83e4fa9c
HD
517 if (start >= end)
518 return;
bda97eab
HD
519
520 index = start;
521 for ( ; ; ) {
522 cond_resched();
7a5d0fbb 523 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
83e4fa9c 524 min(end - index, (pgoff_t)PAGEVEC_SIZE),
7a5d0fbb
HD
525 pvec.pages, indices);
526 if (!pvec.nr) {
1635f6a7 527 if (index == start || unfalloc)
bda97eab
HD
528 break;
529 index = start;
530 continue;
531 }
1635f6a7 532 if ((index == start || unfalloc) && indices[0] >= end) {
24513264
HD
533 shmem_deswap_pagevec(&pvec);
534 pagevec_release(&pvec);
bda97eab
HD
535 break;
536 }
537 mem_cgroup_uncharge_start();
538 for (i = 0; i < pagevec_count(&pvec); i++) {
539 struct page *page = pvec.pages[i];
540
7a5d0fbb 541 index = indices[i];
83e4fa9c 542 if (index >= end)
bda97eab
HD
543 break;
544
7a5d0fbb 545 if (radix_tree_exceptional_entry(page)) {
1635f6a7
HD
546 if (unfalloc)
547 continue;
7a5d0fbb
HD
548 nr_swaps_freed += !shmem_free_swap(mapping,
549 index, page);
550 continue;
551 }
552
bda97eab 553 lock_page(page);
1635f6a7
HD
554 if (!unfalloc || !PageUptodate(page)) {
555 if (page->mapping == mapping) {
556 VM_BUG_ON(PageWriteback(page));
557 truncate_inode_page(mapping, page);
558 }
7a5d0fbb 559 }
bda97eab
HD
560 unlock_page(page);
561 }
24513264
HD
562 shmem_deswap_pagevec(&pvec);
563 pagevec_release(&pvec);
bda97eab
HD
564 mem_cgroup_uncharge_end();
565 index++;
566 }
94c1e62d 567
1da177e4 568 spin_lock(&info->lock);
7a5d0fbb 569 info->swapped -= nr_swaps_freed;
1da177e4
LT
570 shmem_recalc_inode(inode);
571 spin_unlock(&info->lock);
1635f6a7 572}
1da177e4 573
1635f6a7
HD
574void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
575{
576 shmem_undo_range(inode, lstart, lend, false);
285b2c4f 577 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1da177e4 578}
94c1e62d 579EXPORT_SYMBOL_GPL(shmem_truncate_range);
1da177e4 580
94c1e62d 581static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
1da177e4
LT
582{
583 struct inode *inode = dentry->d_inode;
1da177e4
LT
584 int error;
585
db78b877
CH
586 error = inode_change_ok(inode, attr);
587 if (error)
588 return error;
589
94c1e62d
HD
590 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
591 loff_t oldsize = inode->i_size;
592 loff_t newsize = attr->ia_size;
3889e6e7 593
94c1e62d
HD
594 if (newsize != oldsize) {
595 i_size_write(inode, newsize);
596 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
597 }
598 if (newsize < oldsize) {
599 loff_t holebegin = round_up(newsize, PAGE_SIZE);
600 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
601 shmem_truncate_range(inode, newsize, (loff_t)-1);
602 /* unmap again to remove racily COWed private pages */
603 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
604 }
1da177e4
LT
605 }
606
db78b877 607 setattr_copy(inode, attr);
39f0247d 608#ifdef CONFIG_TMPFS_POSIX_ACL
db78b877 609 if (attr->ia_valid & ATTR_MODE)
1c7c474c 610 error = generic_acl_chmod(inode);
39f0247d 611#endif
1da177e4
LT
612 return error;
613}
614
1f895f75 615static void shmem_evict_inode(struct inode *inode)
1da177e4 616{
1da177e4 617 struct shmem_inode_info *info = SHMEM_I(inode);
b09e0fa4 618 struct shmem_xattr *xattr, *nxattr;
1da177e4 619
3889e6e7 620 if (inode->i_mapping->a_ops == &shmem_aops) {
1da177e4
LT
621 shmem_unacct_size(info->flags, inode->i_size);
622 inode->i_size = 0;
3889e6e7 623 shmem_truncate_range(inode, 0, (loff_t)-1);
1da177e4 624 if (!list_empty(&info->swaplist)) {
cb5f7b9a 625 mutex_lock(&shmem_swaplist_mutex);
1da177e4 626 list_del_init(&info->swaplist);
cb5f7b9a 627 mutex_unlock(&shmem_swaplist_mutex);
1da177e4 628 }
69f07ec9
HD
629 } else
630 kfree(info->symlink);
b09e0fa4
EP
631
632 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
633 kfree(xattr->name);
634 kfree(xattr);
635 }
0edd73b3 636 BUG_ON(inode->i_blocks);
5b04c689 637 shmem_free_inode(inode->i_sb);
dbd5768f 638 clear_inode(inode);
1da177e4
LT
639}
640
46f65ec1
HD
641/*
642 * If swap found in inode, free it and move page from swapcache to filecache.
643 */
41ffe5d5 644static int shmem_unuse_inode(struct shmem_inode_info *info,
bde05d1c 645 swp_entry_t swap, struct page **pagep)
1da177e4 646{
285b2c4f 647 struct address_space *mapping = info->vfs_inode.i_mapping;
46f65ec1 648 void *radswap;
41ffe5d5 649 pgoff_t index;
bde05d1c
HD
650 gfp_t gfp;
651 int error = 0;
1da177e4 652
46f65ec1 653 radswap = swp_to_radix_entry(swap);
e504f3fd 654 index = radix_tree_locate_item(&mapping->page_tree, radswap);
46f65ec1 655 if (index == -1)
285b2c4f 656 return 0;
2e0e26c7 657
1b1b32f2
HD
658 /*
659 * Move _head_ to start search for next from here.
1f895f75 660 * But be careful: shmem_evict_inode checks list_empty without taking
1b1b32f2 661 * mutex, and there's an instant in list_move_tail when info->swaplist
285b2c4f 662 * would appear empty, if it were the only one on shmem_swaplist.
1b1b32f2
HD
663 */
664 if (shmem_swaplist.next != &info->swaplist)
665 list_move_tail(&shmem_swaplist, &info->swaplist);
2e0e26c7 666
bde05d1c
HD
667 gfp = mapping_gfp_mask(mapping);
668 if (shmem_should_replace_page(*pagep, gfp)) {
669 mutex_unlock(&shmem_swaplist_mutex);
670 error = shmem_replace_page(pagep, gfp, info, index);
671 mutex_lock(&shmem_swaplist_mutex);
672 /*
673 * We needed to drop mutex to make that restrictive page
674 * allocation; but the inode might already be freed by now,
675 * and we cannot refer to inode or mapping or info to check.
676 * However, we do hold page lock on the PageSwapCache page,
677 * so can check if that still has our reference remaining.
678 */
679 if (!page_swapcount(*pagep))
680 error = -ENOENT;
681 }
682
d13d1443 683 /*
778dd893
HD
684 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
685 * but also to hold up shmem_evict_inode(): so inode cannot be freed
686 * beneath us (pagelock doesn't help until the page is in pagecache).
d13d1443 687 */
bde05d1c
HD
688 if (!error)
689 error = shmem_add_to_page_cache(*pagep, mapping, index,
46f65ec1 690 GFP_NOWAIT, radswap);
48f170fb 691 if (error != -ENOMEM) {
46f65ec1
HD
692 /*
693 * Truncation and eviction use free_swap_and_cache(), which
694 * only does trylock page: if we raced, best clean up here.
695 */
bde05d1c
HD
696 delete_from_swap_cache(*pagep);
697 set_page_dirty(*pagep);
46f65ec1
HD
698 if (!error) {
699 spin_lock(&info->lock);
700 info->swapped--;
701 spin_unlock(&info->lock);
702 swap_free(swap);
703 }
2e0e26c7 704 error = 1; /* not an error, but entry was found */
1da177e4 705 }
2e0e26c7 706 return error;
1da177e4
LT
707}
708
709/*
46f65ec1 710 * Search through swapped inodes to find and replace swap by page.
1da177e4 711 */
41ffe5d5 712int shmem_unuse(swp_entry_t swap, struct page *page)
1da177e4 713{
41ffe5d5 714 struct list_head *this, *next;
1da177e4
LT
715 struct shmem_inode_info *info;
716 int found = 0;
bde05d1c
HD
717 int error = 0;
718
719 /*
720 * There's a faint possibility that swap page was replaced before
721 * caller locked it: it will come back later with the right page.
722 */
723 if (unlikely(!PageSwapCache(page)))
724 goto out;
778dd893
HD
725
726 /*
727 * Charge page using GFP_KERNEL while we can wait, before taking
728 * the shmem_swaplist_mutex which might hold up shmem_writepage().
729 * Charged back to the user (not to caller) when swap account is used.
778dd893
HD
730 */
731 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
732 if (error)
733 goto out;
46f65ec1 734 /* No radix_tree_preload: swap entry keeps a place for page in tree */
1da177e4 735
cb5f7b9a 736 mutex_lock(&shmem_swaplist_mutex);
41ffe5d5
HD
737 list_for_each_safe(this, next, &shmem_swaplist) {
738 info = list_entry(this, struct shmem_inode_info, swaplist);
285b2c4f 739 if (info->swapped)
bde05d1c 740 found = shmem_unuse_inode(info, swap, &page);
6922c0c7
HD
741 else
742 list_del_init(&info->swaplist);
cb5f7b9a 743 cond_resched();
2e0e26c7 744 if (found)
778dd893 745 break;
1da177e4 746 }
cb5f7b9a 747 mutex_unlock(&shmem_swaplist_mutex);
778dd893 748
778dd893
HD
749 if (found < 0)
750 error = found;
751out:
aaa46865
HD
752 unlock_page(page);
753 page_cache_release(page);
778dd893 754 return error;
1da177e4
LT
755}
756
757/*
758 * Move the page from the page cache to the swap cache.
759 */
760static int shmem_writepage(struct page *page, struct writeback_control *wbc)
761{
762 struct shmem_inode_info *info;
1da177e4 763 struct address_space *mapping;
1da177e4 764 struct inode *inode;
6922c0c7
HD
765 swp_entry_t swap;
766 pgoff_t index;
1da177e4
LT
767
768 BUG_ON(!PageLocked(page));
1da177e4
LT
769 mapping = page->mapping;
770 index = page->index;
771 inode = mapping->host;
772 info = SHMEM_I(inode);
773 if (info->flags & VM_LOCKED)
774 goto redirty;
d9fe526a 775 if (!total_swap_pages)
1da177e4
LT
776 goto redirty;
777
d9fe526a
HD
778 /*
779 * shmem_backing_dev_info's capabilities prevent regular writeback or
780 * sync from ever calling shmem_writepage; but a stacking filesystem
48f170fb 781 * might use ->writepage of its underlying filesystem, in which case
d9fe526a 782 * tmpfs should write out to swap only in response to memory pressure,
48f170fb 783 * and not for the writeback threads or sync.
d9fe526a 784 */
48f170fb
HD
785 if (!wbc->for_reclaim) {
786 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
787 goto redirty;
788 }
1635f6a7
HD
789
790 /*
791 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
792 * value into swapfile.c, the only way we can correctly account for a
793 * fallocated page arriving here is now to initialize it and write it.
794 */
795 if (!PageUptodate(page)) {
796 clear_highpage(page);
797 flush_dcache_page(page);
798 SetPageUptodate(page);
799 }
800
48f170fb
HD
801 swap = get_swap_page();
802 if (!swap.val)
803 goto redirty;
d9fe526a 804
b1dea800
HD
805 /*
806 * Add inode to shmem_unuse()'s list of swapped-out inodes,
6922c0c7
HD
807 * if it's not already there. Do it now before the page is
808 * moved to swap cache, when its pagelock no longer protects
b1dea800 809 * the inode from eviction. But don't unlock the mutex until
6922c0c7
HD
810 * we've incremented swapped, because shmem_unuse_inode() will
811 * prune a !swapped inode from the swaplist under this mutex.
b1dea800 812 */
48f170fb
HD
813 mutex_lock(&shmem_swaplist_mutex);
814 if (list_empty(&info->swaplist))
815 list_add_tail(&info->swaplist, &shmem_swaplist);
b1dea800 816
48f170fb 817 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
aaa46865 818 swap_shmem_alloc(swap);
6922c0c7
HD
819 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
820
821 spin_lock(&info->lock);
822 info->swapped++;
823 shmem_recalc_inode(inode);
826267cf 824 spin_unlock(&info->lock);
6922c0c7
HD
825
826 mutex_unlock(&shmem_swaplist_mutex);
d9fe526a 827 BUG_ON(page_mapped(page));
9fab5619 828 swap_writepage(page, wbc);
1da177e4
LT
829 return 0;
830 }
831
6922c0c7 832 mutex_unlock(&shmem_swaplist_mutex);
cb4b86ba 833 swapcache_free(swap, NULL);
1da177e4
LT
834redirty:
835 set_page_dirty(page);
d9fe526a
HD
836 if (wbc->for_reclaim)
837 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
838 unlock_page(page);
839 return 0;
1da177e4
LT
840}
841
842#ifdef CONFIG_NUMA
680d794b 843#ifdef CONFIG_TMPFS
71fe804b 844static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
680d794b 845{
095f1fc4 846 char buffer[64];
680d794b 847
71fe804b 848 if (!mpol || mpol->mode == MPOL_DEFAULT)
095f1fc4 849 return; /* show nothing */
680d794b 850
71fe804b 851 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
095f1fc4
LS
852
853 seq_printf(seq, ",mpol=%s", buffer);
680d794b 854}
71fe804b
LS
855
856static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
857{
858 struct mempolicy *mpol = NULL;
859 if (sbinfo->mpol) {
860 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
861 mpol = sbinfo->mpol;
862 mpol_get(mpol);
863 spin_unlock(&sbinfo->stat_lock);
864 }
865 return mpol;
866}
680d794b 867#endif /* CONFIG_TMPFS */
868
41ffe5d5
HD
869static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
870 struct shmem_inode_info *info, pgoff_t index)
1da177e4 871{
52cd3b07 872 struct mempolicy mpol, *spol;
1da177e4
LT
873 struct vm_area_struct pvma;
874
52cd3b07 875 spol = mpol_cond_copy(&mpol,
41ffe5d5 876 mpol_shared_policy_lookup(&info->policy, index));
52cd3b07 877
1da177e4 878 /* Create a pseudo vma that just contains the policy */
c4cc6d07 879 pvma.vm_start = 0;
41ffe5d5 880 pvma.vm_pgoff = index;
c4cc6d07 881 pvma.vm_ops = NULL;
52cd3b07 882 pvma.vm_policy = spol;
41ffe5d5 883 return swapin_readahead(swap, gfp, &pvma, 0);
1da177e4
LT
884}
885
02098fea 886static struct page *shmem_alloc_page(gfp_t gfp,
41ffe5d5 887 struct shmem_inode_info *info, pgoff_t index)
1da177e4
LT
888{
889 struct vm_area_struct pvma;
1da177e4 890
c4cc6d07
HD
891 /* Create a pseudo vma that just contains the policy */
892 pvma.vm_start = 0;
41ffe5d5 893 pvma.vm_pgoff = index;
c4cc6d07 894 pvma.vm_ops = NULL;
41ffe5d5 895 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
52cd3b07
LS
896
897 /*
898 * alloc_page_vma() will drop the shared policy reference
899 */
900 return alloc_page_vma(gfp, &pvma, 0);
1da177e4 901}
680d794b 902#else /* !CONFIG_NUMA */
903#ifdef CONFIG_TMPFS
41ffe5d5 904static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
680d794b 905{
906}
907#endif /* CONFIG_TMPFS */
908
41ffe5d5
HD
909static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
910 struct shmem_inode_info *info, pgoff_t index)
1da177e4 911{
41ffe5d5 912 return swapin_readahead(swap, gfp, NULL, 0);
1da177e4
LT
913}
914
02098fea 915static inline struct page *shmem_alloc_page(gfp_t gfp,
41ffe5d5 916 struct shmem_inode_info *info, pgoff_t index)
1da177e4 917{
e84e2e13 918 return alloc_page(gfp);
1da177e4 919}
680d794b 920#endif /* CONFIG_NUMA */
1da177e4 921
71fe804b
LS
922#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
923static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
924{
925 return NULL;
926}
927#endif
928
bde05d1c
HD
929/*
930 * When a page is moved from swapcache to shmem filecache (either by the
931 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
932 * shmem_unuse_inode()), it may have been read in earlier from swap, in
933 * ignorance of the mapping it belongs to. If that mapping has special
934 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
935 * we may need to copy to a suitable page before moving to filecache.
936 *
937 * In a future release, this may well be extended to respect cpuset and
938 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
939 * but for now it is a simple matter of zone.
940 */
941static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
942{
943 return page_zonenum(page) > gfp_zone(gfp);
944}
945
946static int shmem_replace_page(struct page **pagep, gfp_t gfp,
947 struct shmem_inode_info *info, pgoff_t index)
948{
949 struct page *oldpage, *newpage;
950 struct address_space *swap_mapping;
951 pgoff_t swap_index;
952 int error;
953
954 oldpage = *pagep;
955 swap_index = page_private(oldpage);
956 swap_mapping = page_mapping(oldpage);
957
958 /*
959 * We have arrived here because our zones are constrained, so don't
960 * limit chance of success by further cpuset and node constraints.
961 */
962 gfp &= ~GFP_CONSTRAINT_MASK;
963 newpage = shmem_alloc_page(gfp, info, index);
964 if (!newpage)
965 return -ENOMEM;
966 VM_BUG_ON(shmem_should_replace_page(newpage, gfp));
967
968 *pagep = newpage;
969 page_cache_get(newpage);
970 copy_highpage(newpage, oldpage);
971
972 VM_BUG_ON(!PageLocked(oldpage));
973 __set_page_locked(newpage);
974 VM_BUG_ON(!PageUptodate(oldpage));
975 SetPageUptodate(newpage);
976 VM_BUG_ON(!PageSwapBacked(oldpage));
977 SetPageSwapBacked(newpage);
978 VM_BUG_ON(!swap_index);
979 set_page_private(newpage, swap_index);
980 VM_BUG_ON(!PageSwapCache(oldpage));
981 SetPageSwapCache(newpage);
982
983 /*
984 * Our caller will very soon move newpage out of swapcache, but it's
985 * a nice clean interface for us to replace oldpage by newpage there.
986 */
987 spin_lock_irq(&swap_mapping->tree_lock);
988 error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
989 newpage);
990 __inc_zone_page_state(newpage, NR_FILE_PAGES);
991 __dec_zone_page_state(oldpage, NR_FILE_PAGES);
992 spin_unlock_irq(&swap_mapping->tree_lock);
993 BUG_ON(error);
994
995 mem_cgroup_replace_page_cache(oldpage, newpage);
996 lru_cache_add_anon(newpage);
997
998 ClearPageSwapCache(oldpage);
999 set_page_private(oldpage, 0);
1000
1001 unlock_page(oldpage);
1002 page_cache_release(oldpage);
1003 page_cache_release(oldpage);
1004 return 0;
1005}
1006
1da177e4 1007/*
68da9f05 1008 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
1da177e4
LT
1009 *
1010 * If we allocate a new one we do not mark it dirty. That's up to the
1011 * vm. If we swap it in we mark it dirty since we also free the swap
1012 * entry since a page cannot live in both the swap and page cache
1013 */
41ffe5d5 1014static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
68da9f05 1015 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
1da177e4
LT
1016{
1017 struct address_space *mapping = inode->i_mapping;
54af6042 1018 struct shmem_inode_info *info;
1da177e4 1019 struct shmem_sb_info *sbinfo;
27ab7006 1020 struct page *page;
1da177e4
LT
1021 swp_entry_t swap;
1022 int error;
54af6042 1023 int once = 0;
1635f6a7 1024 int alloced = 0;
1da177e4 1025
41ffe5d5 1026 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
1da177e4 1027 return -EFBIG;
1da177e4 1028repeat:
54af6042 1029 swap.val = 0;
41ffe5d5 1030 page = find_lock_page(mapping, index);
54af6042
HD
1031 if (radix_tree_exceptional_entry(page)) {
1032 swap = radix_to_swp_entry(page);
1033 page = NULL;
1034 }
1035
1635f6a7 1036 if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
54af6042
HD
1037 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1038 error = -EINVAL;
1039 goto failed;
1040 }
1041
1635f6a7
HD
1042 /* fallocated page? */
1043 if (page && !PageUptodate(page)) {
1044 if (sgp != SGP_READ)
1045 goto clear;
1046 unlock_page(page);
1047 page_cache_release(page);
1048 page = NULL;
1049 }
54af6042 1050 if (page || (sgp == SGP_READ && !swap.val)) {
54af6042
HD
1051 *pagep = page;
1052 return 0;
27ab7006
HD
1053 }
1054
1055 /*
54af6042
HD
1056 * Fast cache lookup did not find it:
1057 * bring it back from swap or allocate.
27ab7006 1058 */
54af6042
HD
1059 info = SHMEM_I(inode);
1060 sbinfo = SHMEM_SB(inode->i_sb);
1da177e4 1061
1da177e4
LT
1062 if (swap.val) {
1063 /* Look it up and read it in.. */
27ab7006
HD
1064 page = lookup_swap_cache(swap);
1065 if (!page) {
1da177e4 1066 /* here we actually do the io */
68da9f05
HD
1067 if (fault_type)
1068 *fault_type |= VM_FAULT_MAJOR;
41ffe5d5 1069 page = shmem_swapin(swap, gfp, info, index);
27ab7006 1070 if (!page) {
54af6042
HD
1071 error = -ENOMEM;
1072 goto failed;
1da177e4 1073 }
1da177e4
LT
1074 }
1075
1076 /* We have to do this with page locked to prevent races */
54af6042 1077 lock_page(page);
bde05d1c
HD
1078 if (!PageSwapCache(page) || page->mapping) {
1079 error = -EEXIST; /* try again */
1080 goto failed;
1081 }
27ab7006 1082 if (!PageUptodate(page)) {
1da177e4 1083 error = -EIO;
54af6042 1084 goto failed;
1da177e4 1085 }
54af6042
HD
1086 wait_on_page_writeback(page);
1087
bde05d1c
HD
1088 if (shmem_should_replace_page(page, gfp)) {
1089 error = shmem_replace_page(&page, gfp, info, index);
1090 if (error)
1091 goto failed;
1da177e4 1092 }
27ab7006 1093
aa3b1895
HD
1094 error = mem_cgroup_cache_charge(page, current->mm,
1095 gfp & GFP_RECLAIM_MASK);
1096 if (!error)
1097 error = shmem_add_to_page_cache(page, mapping, index,
1098 gfp, swp_to_radix_entry(swap));
54af6042
HD
1099 if (error)
1100 goto failed;
1101
1102 spin_lock(&info->lock);
285b2c4f 1103 info->swapped--;
54af6042 1104 shmem_recalc_inode(inode);
27ab7006 1105 spin_unlock(&info->lock);
54af6042
HD
1106
1107 delete_from_swap_cache(page);
27ab7006
HD
1108 set_page_dirty(page);
1109 swap_free(swap);
1110
54af6042
HD
1111 } else {
1112 if (shmem_acct_block(info->flags)) {
1113 error = -ENOSPC;
1114 goto failed;
1da177e4 1115 }
0edd73b3 1116 if (sbinfo->max_blocks) {
fc5da22a 1117 if (percpu_counter_compare(&sbinfo->used_blocks,
54af6042
HD
1118 sbinfo->max_blocks) >= 0) {
1119 error = -ENOSPC;
1120 goto unacct;
1121 }
7e496299 1122 percpu_counter_inc(&sbinfo->used_blocks);
54af6042 1123 }
1da177e4 1124
54af6042
HD
1125 page = shmem_alloc_page(gfp, info, index);
1126 if (!page) {
1127 error = -ENOMEM;
1128 goto decused;
1da177e4
LT
1129 }
1130
54af6042
HD
1131 SetPageSwapBacked(page);
1132 __set_page_locked(page);
aa3b1895
HD
1133 error = mem_cgroup_cache_charge(page, current->mm,
1134 gfp & GFP_RECLAIM_MASK);
1135 if (!error)
1136 error = shmem_add_to_page_cache(page, mapping, index,
1137 gfp, NULL);
54af6042
HD
1138 if (error)
1139 goto decused;
1140 lru_cache_add_anon(page);
1141
1142 spin_lock(&info->lock);
1da177e4 1143 info->alloced++;
54af6042
HD
1144 inode->i_blocks += BLOCKS_PER_PAGE;
1145 shmem_recalc_inode(inode);
1da177e4 1146 spin_unlock(&info->lock);
1635f6a7 1147 alloced = true;
54af6042 1148
ec9516fb 1149 /*
1635f6a7
HD
1150 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1151 */
1152 if (sgp == SGP_FALLOC)
1153 sgp = SGP_WRITE;
1154clear:
1155 /*
1156 * Let SGP_WRITE caller clear ends if write does not fill page;
1157 * but SGP_FALLOC on a page fallocated earlier must initialize
1158 * it now, lest undo on failure cancel our earlier guarantee.
ec9516fb
HD
1159 */
1160 if (sgp != SGP_WRITE) {
1161 clear_highpage(page);
1162 flush_dcache_page(page);
1163 SetPageUptodate(page);
1164 }
a0ee5ec5 1165 if (sgp == SGP_DIRTY)
27ab7006 1166 set_page_dirty(page);
1da177e4 1167 }
bde05d1c 1168
54af6042 1169 /* Perhaps the file has been truncated since we checked */
1635f6a7 1170 if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
54af6042
HD
1171 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1172 error = -EINVAL;
1635f6a7
HD
1173 if (alloced)
1174 goto trunc;
1175 else
1176 goto failed;
e83c32e8 1177 }
54af6042
HD
1178 *pagep = page;
1179 return 0;
1da177e4 1180
59a16ead 1181 /*
54af6042 1182 * Error recovery.
59a16ead 1183 */
54af6042 1184trunc:
1635f6a7 1185 info = SHMEM_I(inode);
54af6042
HD
1186 ClearPageDirty(page);
1187 delete_from_page_cache(page);
1188 spin_lock(&info->lock);
1189 info->alloced--;
1190 inode->i_blocks -= BLOCKS_PER_PAGE;
59a16ead 1191 spin_unlock(&info->lock);
54af6042 1192decused:
1635f6a7 1193 sbinfo = SHMEM_SB(inode->i_sb);
54af6042
HD
1194 if (sbinfo->max_blocks)
1195 percpu_counter_add(&sbinfo->used_blocks, -1);
1196unacct:
1197 shmem_unacct_blocks(info->flags, 1);
1198failed:
1199 if (swap.val && error != -EINVAL) {
1200 struct page *test = find_get_page(mapping, index);
1201 if (test && !radix_tree_exceptional_entry(test))
1202 page_cache_release(test);
1203 /* Have another try if the entry has changed */
1204 if (test != swp_to_radix_entry(swap))
1205 error = -EEXIST;
1206 }
27ab7006 1207 if (page) {
54af6042 1208 unlock_page(page);
27ab7006 1209 page_cache_release(page);
54af6042
HD
1210 }
1211 if (error == -ENOSPC && !once++) {
1212 info = SHMEM_I(inode);
1213 spin_lock(&info->lock);
1214 shmem_recalc_inode(inode);
1215 spin_unlock(&info->lock);
27ab7006 1216 goto repeat;
ff36b801 1217 }
54af6042
HD
1218 if (error == -EEXIST)
1219 goto repeat;
1220 return error;
1da177e4
LT
1221}
1222
d0217ac0 1223static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1da177e4 1224{
d3ac7f89 1225 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1da177e4 1226 int error;
68da9f05 1227 int ret = VM_FAULT_LOCKED;
1da177e4 1228
27d54b39 1229 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
d0217ac0
NP
1230 if (error)
1231 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
68da9f05 1232
456f998e
YH
1233 if (ret & VM_FAULT_MAJOR) {
1234 count_vm_event(PGMAJFAULT);
1235 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1236 }
68da9f05 1237 return ret;
1da177e4
LT
1238}
1239
1da177e4 1240#ifdef CONFIG_NUMA
41ffe5d5 1241static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
1da177e4 1242{
41ffe5d5
HD
1243 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1244 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
1da177e4
LT
1245}
1246
d8dc74f2
AB
1247static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1248 unsigned long addr)
1da177e4 1249{
41ffe5d5
HD
1250 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1251 pgoff_t index;
1da177e4 1252
41ffe5d5
HD
1253 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1254 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
1da177e4
LT
1255}
1256#endif
1257
1258int shmem_lock(struct file *file, int lock, struct user_struct *user)
1259{
d3ac7f89 1260 struct inode *inode = file->f_path.dentry->d_inode;
1da177e4
LT
1261 struct shmem_inode_info *info = SHMEM_I(inode);
1262 int retval = -ENOMEM;
1263
1264 spin_lock(&info->lock);
1265 if (lock && !(info->flags & VM_LOCKED)) {
1266 if (!user_shm_lock(inode->i_size, user))
1267 goto out_nomem;
1268 info->flags |= VM_LOCKED;
89e004ea 1269 mapping_set_unevictable(file->f_mapping);
1da177e4
LT
1270 }
1271 if (!lock && (info->flags & VM_LOCKED) && user) {
1272 user_shm_unlock(inode->i_size, user);
1273 info->flags &= ~VM_LOCKED;
89e004ea 1274 mapping_clear_unevictable(file->f_mapping);
1da177e4
LT
1275 }
1276 retval = 0;
89e004ea 1277
1da177e4
LT
1278out_nomem:
1279 spin_unlock(&info->lock);
1280 return retval;
1281}
1282
9b83a6a8 1283static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1da177e4
LT
1284{
1285 file_accessed(file);
1286 vma->vm_ops = &shmem_vm_ops;
d0217ac0 1287 vma->vm_flags |= VM_CAN_NONLINEAR;
1da177e4
LT
1288 return 0;
1289}
1290
454abafe 1291static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
09208d15 1292 umode_t mode, dev_t dev, unsigned long flags)
1da177e4
LT
1293{
1294 struct inode *inode;
1295 struct shmem_inode_info *info;
1296 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1297
5b04c689
PE
1298 if (shmem_reserve_inode(sb))
1299 return NULL;
1da177e4
LT
1300
1301 inode = new_inode(sb);
1302 if (inode) {
85fe4025 1303 inode->i_ino = get_next_ino();
454abafe 1304 inode_init_owner(inode, dir, mode);
1da177e4 1305 inode->i_blocks = 0;
1da177e4
LT
1306 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1307 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
91828a40 1308 inode->i_generation = get_seconds();
1da177e4
LT
1309 info = SHMEM_I(inode);
1310 memset(info, 0, (char *)inode - (char *)info);
1311 spin_lock_init(&info->lock);
0b0a0806 1312 info->flags = flags & VM_NORESERVE;
1da177e4 1313 INIT_LIST_HEAD(&info->swaplist);
b09e0fa4 1314 INIT_LIST_HEAD(&info->xattr_list);
72c04902 1315 cache_no_acl(inode);
1da177e4
LT
1316
1317 switch (mode & S_IFMT) {
1318 default:
39f0247d 1319 inode->i_op = &shmem_special_inode_operations;
1da177e4
LT
1320 init_special_inode(inode, mode, dev);
1321 break;
1322 case S_IFREG:
14fcc23f 1323 inode->i_mapping->a_ops = &shmem_aops;
1da177e4
LT
1324 inode->i_op = &shmem_inode_operations;
1325 inode->i_fop = &shmem_file_operations;
71fe804b
LS
1326 mpol_shared_policy_init(&info->policy,
1327 shmem_get_sbmpol(sbinfo));
1da177e4
LT
1328 break;
1329 case S_IFDIR:
d8c76e6f 1330 inc_nlink(inode);
1da177e4
LT
1331 /* Some things misbehave if size == 0 on a directory */
1332 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1333 inode->i_op = &shmem_dir_inode_operations;
1334 inode->i_fop = &simple_dir_operations;
1335 break;
1336 case S_IFLNK:
1337 /*
1338 * Must not load anything in the rbtree,
1339 * mpol_free_shared_policy will not be called.
1340 */
71fe804b 1341 mpol_shared_policy_init(&info->policy, NULL);
1da177e4
LT
1342 break;
1343 }
5b04c689
PE
1344 } else
1345 shmem_free_inode(sb);
1da177e4
LT
1346 return inode;
1347}
1348
1349#ifdef CONFIG_TMPFS
92e1d5be 1350static const struct inode_operations shmem_symlink_inode_operations;
69f07ec9 1351static const struct inode_operations shmem_short_symlink_operations;
1da177e4 1352
6d9d88d0
JS
1353#ifdef CONFIG_TMPFS_XATTR
1354static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
1355#else
1356#define shmem_initxattrs NULL
1357#endif
1358
1da177e4 1359static int
800d15a5
NP
1360shmem_write_begin(struct file *file, struct address_space *mapping,
1361 loff_t pos, unsigned len, unsigned flags,
1362 struct page **pagep, void **fsdata)
1da177e4 1363{
800d15a5
NP
1364 struct inode *inode = mapping->host;
1365 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
800d15a5
NP
1366 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1367}
1368
1369static int
1370shmem_write_end(struct file *file, struct address_space *mapping,
1371 loff_t pos, unsigned len, unsigned copied,
1372 struct page *page, void *fsdata)
1373{
1374 struct inode *inode = mapping->host;
1375
d3602444
HD
1376 if (pos + copied > inode->i_size)
1377 i_size_write(inode, pos + copied);
1378
ec9516fb
HD
1379 if (!PageUptodate(page)) {
1380 if (copied < PAGE_CACHE_SIZE) {
1381 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1382 zero_user_segments(page, 0, from,
1383 from + copied, PAGE_CACHE_SIZE);
1384 }
1385 SetPageUptodate(page);
1386 }
800d15a5 1387 set_page_dirty(page);
6746aff7 1388 unlock_page(page);
800d15a5
NP
1389 page_cache_release(page);
1390
800d15a5 1391 return copied;
1da177e4
LT
1392}
1393
1da177e4
LT
1394static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1395{
d3ac7f89 1396 struct inode *inode = filp->f_path.dentry->d_inode;
1da177e4 1397 struct address_space *mapping = inode->i_mapping;
41ffe5d5
HD
1398 pgoff_t index;
1399 unsigned long offset;
a0ee5ec5
HD
1400 enum sgp_type sgp = SGP_READ;
1401
1402 /*
1403 * Might this read be for a stacking filesystem? Then when reading
1404 * holes of a sparse file, we actually need to allocate those pages,
1405 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1406 */
1407 if (segment_eq(get_fs(), KERNEL_DS))
1408 sgp = SGP_DIRTY;
1da177e4
LT
1409
1410 index = *ppos >> PAGE_CACHE_SHIFT;
1411 offset = *ppos & ~PAGE_CACHE_MASK;
1412
1413 for (;;) {
1414 struct page *page = NULL;
41ffe5d5
HD
1415 pgoff_t end_index;
1416 unsigned long nr, ret;
1da177e4
LT
1417 loff_t i_size = i_size_read(inode);
1418
1419 end_index = i_size >> PAGE_CACHE_SHIFT;
1420 if (index > end_index)
1421 break;
1422 if (index == end_index) {
1423 nr = i_size & ~PAGE_CACHE_MASK;
1424 if (nr <= offset)
1425 break;
1426 }
1427
a0ee5ec5 1428 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
1da177e4
LT
1429 if (desc->error) {
1430 if (desc->error == -EINVAL)
1431 desc->error = 0;
1432 break;
1433 }
d3602444
HD
1434 if (page)
1435 unlock_page(page);
1da177e4
LT
1436
1437 /*
1438 * We must evaluate after, since reads (unlike writes)
1b1dcc1b 1439 * are called without i_mutex protection against truncate
1da177e4
LT
1440 */
1441 nr = PAGE_CACHE_SIZE;
1442 i_size = i_size_read(inode);
1443 end_index = i_size >> PAGE_CACHE_SHIFT;
1444 if (index == end_index) {
1445 nr = i_size & ~PAGE_CACHE_MASK;
1446 if (nr <= offset) {
1447 if (page)
1448 page_cache_release(page);
1449 break;
1450 }
1451 }
1452 nr -= offset;
1453
1454 if (page) {
1455 /*
1456 * If users can be writing to this page using arbitrary
1457 * virtual addresses, take care about potential aliasing
1458 * before reading the page on the kernel side.
1459 */
1460 if (mapping_writably_mapped(mapping))
1461 flush_dcache_page(page);
1462 /*
1463 * Mark the page accessed if we read the beginning.
1464 */
1465 if (!offset)
1466 mark_page_accessed(page);
b5810039 1467 } else {
1da177e4 1468 page = ZERO_PAGE(0);
b5810039
NP
1469 page_cache_get(page);
1470 }
1da177e4
LT
1471
1472 /*
1473 * Ok, we have the page, and it's up-to-date, so
1474 * now we can copy it to user space...
1475 *
1476 * The actor routine returns how many bytes were actually used..
1477 * NOTE! This may not be the same as how much of a user buffer
1478 * we filled up (we may be padding etc), so we can only update
1479 * "pos" here (the actor routine has to update the user buffer
1480 * pointers and the remaining count).
1481 */
1482 ret = actor(desc, page, offset, nr);
1483 offset += ret;
1484 index += offset >> PAGE_CACHE_SHIFT;
1485 offset &= ~PAGE_CACHE_MASK;
1486
1487 page_cache_release(page);
1488 if (ret != nr || !desc->count)
1489 break;
1490
1491 cond_resched();
1492 }
1493
1494 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1495 file_accessed(filp);
1496}
1497
bcd78e49
HD
1498static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1499 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
1500{
1501 struct file *filp = iocb->ki_filp;
1502 ssize_t retval;
1503 unsigned long seg;
1504 size_t count;
1505 loff_t *ppos = &iocb->ki_pos;
1506
1507 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1508 if (retval)
1509 return retval;
1510
1511 for (seg = 0; seg < nr_segs; seg++) {
1512 read_descriptor_t desc;
1513
1514 desc.written = 0;
1515 desc.arg.buf = iov[seg].iov_base;
1516 desc.count = iov[seg].iov_len;
1517 if (desc.count == 0)
1518 continue;
1519 desc.error = 0;
1520 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1521 retval += desc.written;
1522 if (desc.error) {
1523 retval = retval ?: desc.error;
1524 break;
1525 }
1526 if (desc.count > 0)
1527 break;
1528 }
1529 return retval;
1da177e4
LT
1530}
1531
708e3508
HD
1532static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1533 struct pipe_inode_info *pipe, size_t len,
1534 unsigned int flags)
1535{
1536 struct address_space *mapping = in->f_mapping;
71f0e07a 1537 struct inode *inode = mapping->host;
708e3508
HD
1538 unsigned int loff, nr_pages, req_pages;
1539 struct page *pages[PIPE_DEF_BUFFERS];
1540 struct partial_page partial[PIPE_DEF_BUFFERS];
1541 struct page *page;
1542 pgoff_t index, end_index;
1543 loff_t isize, left;
1544 int error, page_nr;
1545 struct splice_pipe_desc spd = {
1546 .pages = pages,
1547 .partial = partial,
1548 .flags = flags,
1549 .ops = &page_cache_pipe_buf_ops,
1550 .spd_release = spd_release_page,
1551 };
1552
71f0e07a 1553 isize = i_size_read(inode);
708e3508
HD
1554 if (unlikely(*ppos >= isize))
1555 return 0;
1556
1557 left = isize - *ppos;
1558 if (unlikely(left < len))
1559 len = left;
1560
1561 if (splice_grow_spd(pipe, &spd))
1562 return -ENOMEM;
1563
1564 index = *ppos >> PAGE_CACHE_SHIFT;
1565 loff = *ppos & ~PAGE_CACHE_MASK;
1566 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1567 nr_pages = min(req_pages, pipe->buffers);
1568
708e3508
HD
1569 spd.nr_pages = find_get_pages_contig(mapping, index,
1570 nr_pages, spd.pages);
1571 index += spd.nr_pages;
708e3508 1572 error = 0;
708e3508 1573
71f0e07a 1574 while (spd.nr_pages < nr_pages) {
71f0e07a
HD
1575 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1576 if (error)
1577 break;
1578 unlock_page(page);
708e3508
HD
1579 spd.pages[spd.nr_pages++] = page;
1580 index++;
1581 }
1582
708e3508
HD
1583 index = *ppos >> PAGE_CACHE_SHIFT;
1584 nr_pages = spd.nr_pages;
1585 spd.nr_pages = 0;
71f0e07a 1586
708e3508
HD
1587 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1588 unsigned int this_len;
1589
1590 if (!len)
1591 break;
1592
708e3508
HD
1593 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1594 page = spd.pages[page_nr];
1595
71f0e07a 1596 if (!PageUptodate(page) || page->mapping != mapping) {
71f0e07a
HD
1597 error = shmem_getpage(inode, index, &page,
1598 SGP_CACHE, NULL);
1599 if (error)
708e3508 1600 break;
71f0e07a
HD
1601 unlock_page(page);
1602 page_cache_release(spd.pages[page_nr]);
1603 spd.pages[page_nr] = page;
708e3508 1604 }
71f0e07a
HD
1605
1606 isize = i_size_read(inode);
708e3508
HD
1607 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1608 if (unlikely(!isize || index > end_index))
1609 break;
1610
708e3508
HD
1611 if (end_index == index) {
1612 unsigned int plen;
1613
708e3508
HD
1614 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1615 if (plen <= loff)
1616 break;
1617
708e3508
HD
1618 this_len = min(this_len, plen - loff);
1619 len = this_len;
1620 }
1621
1622 spd.partial[page_nr].offset = loff;
1623 spd.partial[page_nr].len = this_len;
1624 len -= this_len;
1625 loff = 0;
1626 spd.nr_pages++;
1627 index++;
1628 }
1629
708e3508
HD
1630 while (page_nr < nr_pages)
1631 page_cache_release(spd.pages[page_nr++]);
708e3508
HD
1632
1633 if (spd.nr_pages)
1634 error = splice_to_pipe(pipe, &spd);
1635
1636 splice_shrink_spd(pipe, &spd);
1637
1638 if (error > 0) {
1639 *ppos += error;
1640 file_accessed(in);
1641 }
1642 return error;
1643}
1644
83e4fa9c
HD
1645static long shmem_fallocate(struct file *file, int mode, loff_t offset,
1646 loff_t len)
1647{
1648 struct inode *inode = file->f_path.dentry->d_inode;
e2d12e22
HD
1649 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
1650 pgoff_t start, index, end;
1651 int error;
83e4fa9c
HD
1652
1653 mutex_lock(&inode->i_mutex);
1654
1655 if (mode & FALLOC_FL_PUNCH_HOLE) {
1656 struct address_space *mapping = file->f_mapping;
1657 loff_t unmap_start = round_up(offset, PAGE_SIZE);
1658 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
1659
1660 if ((u64)unmap_end > (u64)unmap_start)
1661 unmap_mapping_range(mapping, unmap_start,
1662 1 + unmap_end - unmap_start, 0);
1663 shmem_truncate_range(inode, offset, offset + len - 1);
1664 /* No need to unmap again: hole-punching leaves COWed pages */
1665 error = 0;
e2d12e22
HD
1666 goto out;
1667 }
1668
1669 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
1670 error = inode_newsize_ok(inode, offset + len);
1671 if (error)
1672 goto out;
1673
1674 start = offset >> PAGE_CACHE_SHIFT;
1675 end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1676 /* Try to avoid a swapstorm if len is impossible to satisfy */
1677 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
1678 error = -ENOSPC;
1679 goto out;
83e4fa9c
HD
1680 }
1681
e2d12e22
HD
1682 for (index = start; index < end; index++) {
1683 struct page *page;
1684
1685 /*
1686 * Good, the fallocate(2) manpage permits EINTR: we may have
1687 * been interrupted because we are using up too much memory.
1688 */
1689 if (signal_pending(current))
1690 error = -EINTR;
1691 else
1635f6a7 1692 error = shmem_getpage(inode, index, &page, SGP_FALLOC,
e2d12e22
HD
1693 NULL);
1694 if (error) {
1635f6a7
HD
1695 /* Remove the !PageUptodate pages we added */
1696 shmem_undo_range(inode,
1697 (loff_t)start << PAGE_CACHE_SHIFT,
1698 (loff_t)index << PAGE_CACHE_SHIFT, true);
e2d12e22
HD
1699 goto ctime;
1700 }
1701
e2d12e22 1702 /*
1635f6a7
HD
1703 * If !PageUptodate, leave it that way so that freeable pages
1704 * can be recognized if we need to rollback on error later.
1705 * But set_page_dirty so that memory pressure will swap rather
e2d12e22
HD
1706 * than free the pages we are allocating (and SGP_CACHE pages
1707 * might still be clean: we now need to mark those dirty too).
1708 */
1709 set_page_dirty(page);
1710 unlock_page(page);
1711 page_cache_release(page);
1712 cond_resched();
1713 }
1714
1715 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
1716 i_size_write(inode, offset + len);
1717ctime:
1718 inode->i_ctime = CURRENT_TIME;
1719out:
83e4fa9c
HD
1720 mutex_unlock(&inode->i_mutex);
1721 return error;
1722}
1723
726c3342 1724static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 1725{
726c3342 1726 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1da177e4
LT
1727
1728 buf->f_type = TMPFS_MAGIC;
1729 buf->f_bsize = PAGE_CACHE_SIZE;
1730 buf->f_namelen = NAME_MAX;
0edd73b3 1731 if (sbinfo->max_blocks) {
1da177e4 1732 buf->f_blocks = sbinfo->max_blocks;
41ffe5d5
HD
1733 buf->f_bavail =
1734 buf->f_bfree = sbinfo->max_blocks -
1735 percpu_counter_sum(&sbinfo->used_blocks);
0edd73b3
HD
1736 }
1737 if (sbinfo->max_inodes) {
1da177e4
LT
1738 buf->f_files = sbinfo->max_inodes;
1739 buf->f_ffree = sbinfo->free_inodes;
1da177e4
LT
1740 }
1741 /* else leave those fields 0 like simple_statfs */
1742 return 0;
1743}
1744
1745/*
1746 * File creation. Allocate an inode, and we're done..
1747 */
1748static int
1a67aafb 1749shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1da177e4 1750{
0b0a0806 1751 struct inode *inode;
1da177e4
LT
1752 int error = -ENOSPC;
1753
454abafe 1754 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
1da177e4 1755 if (inode) {
2a7dba39 1756 error = security_inode_init_security(inode, dir,
9d8f13ba 1757 &dentry->d_name,
6d9d88d0 1758 shmem_initxattrs, NULL);
570bc1c2
SS
1759 if (error) {
1760 if (error != -EOPNOTSUPP) {
1761 iput(inode);
1762 return error;
1763 }
39f0247d 1764 }
1c7c474c
CH
1765#ifdef CONFIG_TMPFS_POSIX_ACL
1766 error = generic_acl_init(inode, dir);
39f0247d
AG
1767 if (error) {
1768 iput(inode);
1769 return error;
570bc1c2 1770 }
718deb6b
AV
1771#else
1772 error = 0;
1c7c474c 1773#endif
1da177e4
LT
1774 dir->i_size += BOGO_DIRENT_SIZE;
1775 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1776 d_instantiate(dentry, inode);
1777 dget(dentry); /* Extra count - pin the dentry in core */
1da177e4
LT
1778 }
1779 return error;
1780}
1781
18bb1db3 1782static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4
LT
1783{
1784 int error;
1785
1786 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1787 return error;
d8c76e6f 1788 inc_nlink(dir);
1da177e4
LT
1789 return 0;
1790}
1791
4acdaf27 1792static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1da177e4
LT
1793 struct nameidata *nd)
1794{
1795 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1796}
1797
1798/*
1799 * Link a file..
1800 */
1801static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1802{
1803 struct inode *inode = old_dentry->d_inode;
5b04c689 1804 int ret;
1da177e4
LT
1805
1806 /*
1807 * No ordinary (disk based) filesystem counts links as inodes;
1808 * but each new link needs a new dentry, pinning lowmem, and
1809 * tmpfs dentries cannot be pruned until they are unlinked.
1810 */
5b04c689
PE
1811 ret = shmem_reserve_inode(inode->i_sb);
1812 if (ret)
1813 goto out;
1da177e4
LT
1814
1815 dir->i_size += BOGO_DIRENT_SIZE;
1816 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
d8c76e6f 1817 inc_nlink(inode);
7de9c6ee 1818 ihold(inode); /* New dentry reference */
1da177e4
LT
1819 dget(dentry); /* Extra pinning count for the created dentry */
1820 d_instantiate(dentry, inode);
5b04c689
PE
1821out:
1822 return ret;
1da177e4
LT
1823}
1824
1825static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1826{
1827 struct inode *inode = dentry->d_inode;
1828
5b04c689
PE
1829 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1830 shmem_free_inode(inode->i_sb);
1da177e4
LT
1831
1832 dir->i_size -= BOGO_DIRENT_SIZE;
1833 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
9a53c3a7 1834 drop_nlink(inode);
1da177e4
LT
1835 dput(dentry); /* Undo the count from "create" - this does all the work */
1836 return 0;
1837}
1838
1839static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1840{
1841 if (!simple_empty(dentry))
1842 return -ENOTEMPTY;
1843
9a53c3a7
DH
1844 drop_nlink(dentry->d_inode);
1845 drop_nlink(dir);
1da177e4
LT
1846 return shmem_unlink(dir, dentry);
1847}
1848
1849/*
1850 * The VFS layer already does all the dentry stuff for rename,
1851 * we just have to decrement the usage count for the target if
1852 * it exists so that the VFS layer correctly free's it when it
1853 * gets overwritten.
1854 */
1855static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1856{
1857 struct inode *inode = old_dentry->d_inode;
1858 int they_are_dirs = S_ISDIR(inode->i_mode);
1859
1860 if (!simple_empty(new_dentry))
1861 return -ENOTEMPTY;
1862
1863 if (new_dentry->d_inode) {
1864 (void) shmem_unlink(new_dir, new_dentry);
1865 if (they_are_dirs)
9a53c3a7 1866 drop_nlink(old_dir);
1da177e4 1867 } else if (they_are_dirs) {
9a53c3a7 1868 drop_nlink(old_dir);
d8c76e6f 1869 inc_nlink(new_dir);
1da177e4
LT
1870 }
1871
1872 old_dir->i_size -= BOGO_DIRENT_SIZE;
1873 new_dir->i_size += BOGO_DIRENT_SIZE;
1874 old_dir->i_ctime = old_dir->i_mtime =
1875 new_dir->i_ctime = new_dir->i_mtime =
1876 inode->i_ctime = CURRENT_TIME;
1877 return 0;
1878}
1879
1880static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1881{
1882 int error;
1883 int len;
1884 struct inode *inode;
9276aad6 1885 struct page *page;
1da177e4
LT
1886 char *kaddr;
1887 struct shmem_inode_info *info;
1888
1889 len = strlen(symname) + 1;
1890 if (len > PAGE_CACHE_SIZE)
1891 return -ENAMETOOLONG;
1892
454abafe 1893 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
1da177e4
LT
1894 if (!inode)
1895 return -ENOSPC;
1896
9d8f13ba 1897 error = security_inode_init_security(inode, dir, &dentry->d_name,
6d9d88d0 1898 shmem_initxattrs, NULL);
570bc1c2
SS
1899 if (error) {
1900 if (error != -EOPNOTSUPP) {
1901 iput(inode);
1902 return error;
1903 }
1904 error = 0;
1905 }
1906
1da177e4
LT
1907 info = SHMEM_I(inode);
1908 inode->i_size = len-1;
69f07ec9
HD
1909 if (len <= SHORT_SYMLINK_LEN) {
1910 info->symlink = kmemdup(symname, len, GFP_KERNEL);
1911 if (!info->symlink) {
1912 iput(inode);
1913 return -ENOMEM;
1914 }
1915 inode->i_op = &shmem_short_symlink_operations;
1da177e4
LT
1916 } else {
1917 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1918 if (error) {
1919 iput(inode);
1920 return error;
1921 }
14fcc23f 1922 inode->i_mapping->a_ops = &shmem_aops;
1da177e4 1923 inode->i_op = &shmem_symlink_inode_operations;
9b04c5fe 1924 kaddr = kmap_atomic(page);
1da177e4 1925 memcpy(kaddr, symname, len);
9b04c5fe 1926 kunmap_atomic(kaddr);
ec9516fb 1927 SetPageUptodate(page);
1da177e4 1928 set_page_dirty(page);
6746aff7 1929 unlock_page(page);
1da177e4
LT
1930 page_cache_release(page);
1931 }
1da177e4
LT
1932 dir->i_size += BOGO_DIRENT_SIZE;
1933 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1934 d_instantiate(dentry, inode);
1935 dget(dentry);
1936 return 0;
1937}
1938
69f07ec9 1939static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
1da177e4 1940{
69f07ec9 1941 nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
cc314eef 1942 return NULL;
1da177e4
LT
1943}
1944
cc314eef 1945static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
1946{
1947 struct page *page = NULL;
41ffe5d5
HD
1948 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1949 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
d3602444
HD
1950 if (page)
1951 unlock_page(page);
cc314eef 1952 return page;
1da177e4
LT
1953}
1954
cc314eef 1955static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4
LT
1956{
1957 if (!IS_ERR(nd_get_link(nd))) {
cc314eef 1958 struct page *page = cookie;
1da177e4
LT
1959 kunmap(page);
1960 mark_page_accessed(page);
1961 page_cache_release(page);
1da177e4
LT
1962 }
1963}
1964
b09e0fa4 1965#ifdef CONFIG_TMPFS_XATTR
46711810 1966/*
b09e0fa4
EP
1967 * Superblocks without xattr inode operations may get some security.* xattr
1968 * support from the LSM "for free". As soon as we have any other xattrs
39f0247d
AG
1969 * like ACLs, we also need to implement the security.* handlers at
1970 * filesystem level, though.
1971 */
1972
6d9d88d0
JS
1973/*
1974 * Allocate new xattr and copy in the value; but leave the name to callers.
1975 */
1976static struct shmem_xattr *shmem_xattr_alloc(const void *value, size_t size)
1977{
1978 struct shmem_xattr *new_xattr;
1979 size_t len;
1980
1981 /* wrap around? */
1982 len = sizeof(*new_xattr) + size;
1983 if (len <= sizeof(*new_xattr))
1984 return NULL;
1985
1986 new_xattr = kmalloc(len, GFP_KERNEL);
1987 if (!new_xattr)
1988 return NULL;
1989
1990 new_xattr->size = size;
1991 memcpy(new_xattr->value, value, size);
1992 return new_xattr;
1993}
1994
1995/*
1996 * Callback for security_inode_init_security() for acquiring xattrs.
1997 */
1998static int shmem_initxattrs(struct inode *inode,
1999 const struct xattr *xattr_array,
2000 void *fs_info)
2001{
2002 struct shmem_inode_info *info = SHMEM_I(inode);
2003 const struct xattr *xattr;
2004 struct shmem_xattr *new_xattr;
2005 size_t len;
2006
2007 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
2008 new_xattr = shmem_xattr_alloc(xattr->value, xattr->value_len);
2009 if (!new_xattr)
2010 return -ENOMEM;
2011
2012 len = strlen(xattr->name) + 1;
2013 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
2014 GFP_KERNEL);
2015 if (!new_xattr->name) {
2016 kfree(new_xattr);
2017 return -ENOMEM;
2018 }
2019
2020 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
2021 XATTR_SECURITY_PREFIX_LEN);
2022 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
2023 xattr->name, len);
2024
2025 spin_lock(&info->lock);
2026 list_add(&new_xattr->list, &info->xattr_list);
2027 spin_unlock(&info->lock);
2028 }
2029
2030 return 0;
2031}
2032
b09e0fa4
EP
2033static int shmem_xattr_get(struct dentry *dentry, const char *name,
2034 void *buffer, size_t size)
39f0247d 2035{
b09e0fa4
EP
2036 struct shmem_inode_info *info;
2037 struct shmem_xattr *xattr;
2038 int ret = -ENODATA;
39f0247d 2039
b09e0fa4
EP
2040 info = SHMEM_I(dentry->d_inode);
2041
2042 spin_lock(&info->lock);
2043 list_for_each_entry(xattr, &info->xattr_list, list) {
2044 if (strcmp(name, xattr->name))
2045 continue;
2046
2047 ret = xattr->size;
2048 if (buffer) {
2049 if (size < xattr->size)
2050 ret = -ERANGE;
2051 else
2052 memcpy(buffer, xattr->value, xattr->size);
2053 }
2054 break;
2055 }
2056 spin_unlock(&info->lock);
2057 return ret;
39f0247d
AG
2058}
2059
6d9d88d0 2060static int shmem_xattr_set(struct inode *inode, const char *name,
b09e0fa4 2061 const void *value, size_t size, int flags)
39f0247d 2062{
b09e0fa4
EP
2063 struct shmem_inode_info *info = SHMEM_I(inode);
2064 struct shmem_xattr *xattr;
2065 struct shmem_xattr *new_xattr = NULL;
b09e0fa4
EP
2066 int err = 0;
2067
2068 /* value == NULL means remove */
2069 if (value) {
6d9d88d0 2070 new_xattr = shmem_xattr_alloc(value, size);
b09e0fa4
EP
2071 if (!new_xattr)
2072 return -ENOMEM;
2073
2074 new_xattr->name = kstrdup(name, GFP_KERNEL);
2075 if (!new_xattr->name) {
2076 kfree(new_xattr);
2077 return -ENOMEM;
2078 }
b09e0fa4
EP
2079 }
2080
2081 spin_lock(&info->lock);
2082 list_for_each_entry(xattr, &info->xattr_list, list) {
2083 if (!strcmp(name, xattr->name)) {
2084 if (flags & XATTR_CREATE) {
2085 xattr = new_xattr;
2086 err = -EEXIST;
2087 } else if (new_xattr) {
2088 list_replace(&xattr->list, &new_xattr->list);
2089 } else {
2090 list_del(&xattr->list);
2091 }
2092 goto out;
2093 }
2094 }
2095 if (flags & XATTR_REPLACE) {
2096 xattr = new_xattr;
2097 err = -ENODATA;
2098 } else {
2099 list_add(&new_xattr->list, &info->xattr_list);
2100 xattr = NULL;
2101 }
2102out:
2103 spin_unlock(&info->lock);
2104 if (xattr)
2105 kfree(xattr->name);
2106 kfree(xattr);
2107 return err;
39f0247d
AG
2108}
2109
bb435453 2110static const struct xattr_handler *shmem_xattr_handlers[] = {
b09e0fa4 2111#ifdef CONFIG_TMPFS_POSIX_ACL
1c7c474c
CH
2112 &generic_acl_access_handler,
2113 &generic_acl_default_handler,
b09e0fa4 2114#endif
39f0247d
AG
2115 NULL
2116};
b09e0fa4
EP
2117
2118static int shmem_xattr_validate(const char *name)
2119{
2120 struct { const char *prefix; size_t len; } arr[] = {
2121 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
2122 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
2123 };
2124 int i;
2125
2126 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2127 size_t preflen = arr[i].len;
2128 if (strncmp(name, arr[i].prefix, preflen) == 0) {
2129 if (!name[preflen])
2130 return -EINVAL;
2131 return 0;
2132 }
2133 }
2134 return -EOPNOTSUPP;
2135}
2136
2137static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
2138 void *buffer, size_t size)
2139{
2140 int err;
2141
2142 /*
2143 * If this is a request for a synthetic attribute in the system.*
2144 * namespace use the generic infrastructure to resolve a handler
2145 * for it via sb->s_xattr.
2146 */
2147 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2148 return generic_getxattr(dentry, name, buffer, size);
2149
2150 err = shmem_xattr_validate(name);
2151 if (err)
2152 return err;
2153
2154 return shmem_xattr_get(dentry, name, buffer, size);
2155}
2156
2157static int shmem_setxattr(struct dentry *dentry, const char *name,
2158 const void *value, size_t size, int flags)
2159{
2160 int err;
2161
2162 /*
2163 * If this is a request for a synthetic attribute in the system.*
2164 * namespace use the generic infrastructure to resolve a handler
2165 * for it via sb->s_xattr.
2166 */
2167 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2168 return generic_setxattr(dentry, name, value, size, flags);
2169
2170 err = shmem_xattr_validate(name);
2171 if (err)
2172 return err;
2173
2174 if (size == 0)
2175 value = ""; /* empty EA, do not remove */
2176
6d9d88d0 2177 return shmem_xattr_set(dentry->d_inode, name, value, size, flags);
b09e0fa4
EP
2178
2179}
2180
2181static int shmem_removexattr(struct dentry *dentry, const char *name)
2182{
2183 int err;
2184
2185 /*
2186 * If this is a request for a synthetic attribute in the system.*
2187 * namespace use the generic infrastructure to resolve a handler
2188 * for it via sb->s_xattr.
2189 */
2190 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2191 return generic_removexattr(dentry, name);
2192
2193 err = shmem_xattr_validate(name);
2194 if (err)
2195 return err;
2196
6d9d88d0 2197 return shmem_xattr_set(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
b09e0fa4
EP
2198}
2199
2200static bool xattr_is_trusted(const char *name)
2201{
2202 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
2203}
2204
2205static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2206{
2207 bool trusted = capable(CAP_SYS_ADMIN);
2208 struct shmem_xattr *xattr;
2209 struct shmem_inode_info *info;
2210 size_t used = 0;
2211
2212 info = SHMEM_I(dentry->d_inode);
2213
2214 spin_lock(&info->lock);
2215 list_for_each_entry(xattr, &info->xattr_list, list) {
2216 size_t len;
2217
2218 /* skip "trusted." attributes for unprivileged callers */
2219 if (!trusted && xattr_is_trusted(xattr->name))
2220 continue;
2221
2222 len = strlen(xattr->name) + 1;
2223 used += len;
2224 if (buffer) {
2225 if (size < used) {
2226 used = -ERANGE;
2227 break;
2228 }
2229 memcpy(buffer, xattr->name, len);
2230 buffer += len;
2231 }
2232 }
2233 spin_unlock(&info->lock);
2234
2235 return used;
2236}
2237#endif /* CONFIG_TMPFS_XATTR */
2238
69f07ec9 2239static const struct inode_operations shmem_short_symlink_operations = {
b09e0fa4 2240 .readlink = generic_readlink,
69f07ec9 2241 .follow_link = shmem_follow_short_symlink,
b09e0fa4
EP
2242#ifdef CONFIG_TMPFS_XATTR
2243 .setxattr = shmem_setxattr,
2244 .getxattr = shmem_getxattr,
2245 .listxattr = shmem_listxattr,
2246 .removexattr = shmem_removexattr,
2247#endif
2248};
2249
2250static const struct inode_operations shmem_symlink_inode_operations = {
2251 .readlink = generic_readlink,
2252 .follow_link = shmem_follow_link,
2253 .put_link = shmem_put_link,
2254#ifdef CONFIG_TMPFS_XATTR
2255 .setxattr = shmem_setxattr,
2256 .getxattr = shmem_getxattr,
2257 .listxattr = shmem_listxattr,
2258 .removexattr = shmem_removexattr,
39f0247d 2259#endif
b09e0fa4 2260};
39f0247d 2261
91828a40
DG
2262static struct dentry *shmem_get_parent(struct dentry *child)
2263{
2264 return ERR_PTR(-ESTALE);
2265}
2266
2267static int shmem_match(struct inode *ino, void *vfh)
2268{
2269 __u32 *fh = vfh;
2270 __u64 inum = fh[2];
2271 inum = (inum << 32) | fh[1];
2272 return ino->i_ino == inum && fh[0] == ino->i_generation;
2273}
2274
480b116c
CH
2275static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2276 struct fid *fid, int fh_len, int fh_type)
91828a40 2277{
91828a40 2278 struct inode *inode;
480b116c
CH
2279 struct dentry *dentry = NULL;
2280 u64 inum = fid->raw[2];
2281 inum = (inum << 32) | fid->raw[1];
2282
2283 if (fh_len < 3)
2284 return NULL;
91828a40 2285
480b116c
CH
2286 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2287 shmem_match, fid->raw);
91828a40 2288 if (inode) {
480b116c 2289 dentry = d_find_alias(inode);
91828a40
DG
2290 iput(inode);
2291 }
2292
480b116c 2293 return dentry;
91828a40
DG
2294}
2295
2296static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2297 int connectable)
2298{
2299 struct inode *inode = dentry->d_inode;
2300
5fe0c237
AK
2301 if (*len < 3) {
2302 *len = 3;
91828a40 2303 return 255;
5fe0c237 2304 }
91828a40 2305
1d3382cb 2306 if (inode_unhashed(inode)) {
91828a40
DG
2307 /* Unfortunately insert_inode_hash is not idempotent,
2308 * so as we hash inodes here rather than at creation
2309 * time, we need a lock to ensure we only try
2310 * to do it once
2311 */
2312 static DEFINE_SPINLOCK(lock);
2313 spin_lock(&lock);
1d3382cb 2314 if (inode_unhashed(inode))
91828a40
DG
2315 __insert_inode_hash(inode,
2316 inode->i_ino + inode->i_generation);
2317 spin_unlock(&lock);
2318 }
2319
2320 fh[0] = inode->i_generation;
2321 fh[1] = inode->i_ino;
2322 fh[2] = ((__u64)inode->i_ino) >> 32;
2323
2324 *len = 3;
2325 return 1;
2326}
2327
39655164 2328static const struct export_operations shmem_export_ops = {
91828a40 2329 .get_parent = shmem_get_parent,
91828a40 2330 .encode_fh = shmem_encode_fh,
480b116c 2331 .fh_to_dentry = shmem_fh_to_dentry,
91828a40
DG
2332};
2333
680d794b 2334static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2335 bool remount)
1da177e4
LT
2336{
2337 char *this_char, *value, *rest;
8751e039
EB
2338 uid_t uid;
2339 gid_t gid;
1da177e4 2340
b00dc3ad
HD
2341 while (options != NULL) {
2342 this_char = options;
2343 for (;;) {
2344 /*
2345 * NUL-terminate this option: unfortunately,
2346 * mount options form a comma-separated list,
2347 * but mpol's nodelist may also contain commas.
2348 */
2349 options = strchr(options, ',');
2350 if (options == NULL)
2351 break;
2352 options++;
2353 if (!isdigit(*options)) {
2354 options[-1] = '\0';
2355 break;
2356 }
2357 }
1da177e4
LT
2358 if (!*this_char)
2359 continue;
2360 if ((value = strchr(this_char,'=')) != NULL) {
2361 *value++ = 0;
2362 } else {
2363 printk(KERN_ERR
2364 "tmpfs: No value for mount option '%s'\n",
2365 this_char);
2366 return 1;
2367 }
2368
2369 if (!strcmp(this_char,"size")) {
2370 unsigned long long size;
2371 size = memparse(value,&rest);
2372 if (*rest == '%') {
2373 size <<= PAGE_SHIFT;
2374 size *= totalram_pages;
2375 do_div(size, 100);
2376 rest++;
2377 }
2378 if (*rest)
2379 goto bad_val;
680d794b 2380 sbinfo->max_blocks =
2381 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
1da177e4 2382 } else if (!strcmp(this_char,"nr_blocks")) {
680d794b 2383 sbinfo->max_blocks = memparse(value, &rest);
1da177e4
LT
2384 if (*rest)
2385 goto bad_val;
2386 } else if (!strcmp(this_char,"nr_inodes")) {
680d794b 2387 sbinfo->max_inodes = memparse(value, &rest);
1da177e4
LT
2388 if (*rest)
2389 goto bad_val;
2390 } else if (!strcmp(this_char,"mode")) {
680d794b 2391 if (remount)
1da177e4 2392 continue;
680d794b 2393 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
1da177e4
LT
2394 if (*rest)
2395 goto bad_val;
2396 } else if (!strcmp(this_char,"uid")) {
680d794b 2397 if (remount)
1da177e4 2398 continue;
8751e039 2399 uid = simple_strtoul(value, &rest, 0);
1da177e4
LT
2400 if (*rest)
2401 goto bad_val;
8751e039
EB
2402 sbinfo->uid = make_kuid(current_user_ns(), uid);
2403 if (!uid_valid(sbinfo->uid))
2404 goto bad_val;
1da177e4 2405 } else if (!strcmp(this_char,"gid")) {
680d794b 2406 if (remount)
1da177e4 2407 continue;
8751e039 2408 gid = simple_strtoul(value, &rest, 0);
1da177e4
LT
2409 if (*rest)
2410 goto bad_val;
8751e039
EB
2411 sbinfo->gid = make_kgid(current_user_ns(), gid);
2412 if (!gid_valid(sbinfo->gid))
2413 goto bad_val;
7339ff83 2414 } else if (!strcmp(this_char,"mpol")) {
71fe804b 2415 if (mpol_parse_str(value, &sbinfo->mpol, 1))
7339ff83 2416 goto bad_val;
1da177e4
LT
2417 } else {
2418 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2419 this_char);
2420 return 1;
2421 }
2422 }
2423 return 0;
2424
2425bad_val:
2426 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2427 value, this_char);
2428 return 1;
2429
2430}
2431
2432static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2433{
2434 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
680d794b 2435 struct shmem_sb_info config = *sbinfo;
0edd73b3
HD
2436 unsigned long inodes;
2437 int error = -EINVAL;
2438
680d794b 2439 if (shmem_parse_options(data, &config, true))
0edd73b3 2440 return error;
1da177e4 2441
0edd73b3 2442 spin_lock(&sbinfo->stat_lock);
0edd73b3 2443 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
7e496299 2444 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
0edd73b3 2445 goto out;
680d794b 2446 if (config.max_inodes < inodes)
0edd73b3
HD
2447 goto out;
2448 /*
54af6042 2449 * Those tests disallow limited->unlimited while any are in use;
0edd73b3
HD
2450 * but we must separately disallow unlimited->limited, because
2451 * in that case we have no record of how much is already in use.
2452 */
680d794b 2453 if (config.max_blocks && !sbinfo->max_blocks)
0edd73b3 2454 goto out;
680d794b 2455 if (config.max_inodes && !sbinfo->max_inodes)
0edd73b3
HD
2456 goto out;
2457
2458 error = 0;
680d794b 2459 sbinfo->max_blocks = config.max_blocks;
680d794b 2460 sbinfo->max_inodes = config.max_inodes;
2461 sbinfo->free_inodes = config.max_inodes - inodes;
71fe804b
LS
2462
2463 mpol_put(sbinfo->mpol);
2464 sbinfo->mpol = config.mpol; /* transfers initial ref */
0edd73b3
HD
2465out:
2466 spin_unlock(&sbinfo->stat_lock);
2467 return error;
1da177e4 2468}
680d794b 2469
34c80b1d 2470static int shmem_show_options(struct seq_file *seq, struct dentry *root)
680d794b 2471{
34c80b1d 2472 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
680d794b 2473
2474 if (sbinfo->max_blocks != shmem_default_max_blocks())
2475 seq_printf(seq, ",size=%luk",
2476 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2477 if (sbinfo->max_inodes != shmem_default_max_inodes())
2478 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2479 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
09208d15 2480 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
8751e039
EB
2481 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
2482 seq_printf(seq, ",uid=%u",
2483 from_kuid_munged(&init_user_ns, sbinfo->uid));
2484 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
2485 seq_printf(seq, ",gid=%u",
2486 from_kgid_munged(&init_user_ns, sbinfo->gid));
71fe804b 2487 shmem_show_mpol(seq, sbinfo->mpol);
680d794b 2488 return 0;
2489}
2490#endif /* CONFIG_TMPFS */
1da177e4
LT
2491
2492static void shmem_put_super(struct super_block *sb)
2493{
602586a8
HD
2494 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2495
2496 percpu_counter_destroy(&sbinfo->used_blocks);
2497 kfree(sbinfo);
1da177e4
LT
2498 sb->s_fs_info = NULL;
2499}
2500
2b2af54a 2501int shmem_fill_super(struct super_block *sb, void *data, int silent)
1da177e4
LT
2502{
2503 struct inode *inode;
0edd73b3 2504 struct shmem_sb_info *sbinfo;
680d794b 2505 int err = -ENOMEM;
2506
2507 /* Round up to L1_CACHE_BYTES to resist false sharing */
425fbf04 2508 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
680d794b 2509 L1_CACHE_BYTES), GFP_KERNEL);
2510 if (!sbinfo)
2511 return -ENOMEM;
2512
680d794b 2513 sbinfo->mode = S_IRWXUGO | S_ISVTX;
76aac0e9
DH
2514 sbinfo->uid = current_fsuid();
2515 sbinfo->gid = current_fsgid();
680d794b 2516 sb->s_fs_info = sbinfo;
1da177e4 2517
0edd73b3 2518#ifdef CONFIG_TMPFS
1da177e4
LT
2519 /*
2520 * Per default we only allow half of the physical ram per
2521 * tmpfs instance, limiting inodes to one per page of lowmem;
2522 * but the internal instance is left unlimited.
2523 */
2524 if (!(sb->s_flags & MS_NOUSER)) {
680d794b 2525 sbinfo->max_blocks = shmem_default_max_blocks();
2526 sbinfo->max_inodes = shmem_default_max_inodes();
2527 if (shmem_parse_options(data, sbinfo, false)) {
2528 err = -EINVAL;
2529 goto failed;
2530 }
1da177e4 2531 }
91828a40 2532 sb->s_export_op = &shmem_export_ops;
2f6e38f3 2533 sb->s_flags |= MS_NOSEC;
1da177e4
LT
2534#else
2535 sb->s_flags |= MS_NOUSER;
2536#endif
2537
0edd73b3 2538 spin_lock_init(&sbinfo->stat_lock);
602586a8
HD
2539 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2540 goto failed;
680d794b 2541 sbinfo->free_inodes = sbinfo->max_inodes;
0edd73b3 2542
285b2c4f 2543 sb->s_maxbytes = MAX_LFS_FILESIZE;
1da177e4
LT
2544 sb->s_blocksize = PAGE_CACHE_SIZE;
2545 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2546 sb->s_magic = TMPFS_MAGIC;
2547 sb->s_op = &shmem_ops;
cfd95a9c 2548 sb->s_time_gran = 1;
b09e0fa4 2549#ifdef CONFIG_TMPFS_XATTR
39f0247d 2550 sb->s_xattr = shmem_xattr_handlers;
b09e0fa4
EP
2551#endif
2552#ifdef CONFIG_TMPFS_POSIX_ACL
39f0247d
AG
2553 sb->s_flags |= MS_POSIXACL;
2554#endif
0edd73b3 2555
454abafe 2556 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
1da177e4
LT
2557 if (!inode)
2558 goto failed;
680d794b 2559 inode->i_uid = sbinfo->uid;
2560 inode->i_gid = sbinfo->gid;
318ceed0
AV
2561 sb->s_root = d_make_root(inode);
2562 if (!sb->s_root)
48fde701 2563 goto failed;
1da177e4
LT
2564 return 0;
2565
1da177e4
LT
2566failed:
2567 shmem_put_super(sb);
2568 return err;
2569}
2570
fcc234f8 2571static struct kmem_cache *shmem_inode_cachep;
1da177e4
LT
2572
2573static struct inode *shmem_alloc_inode(struct super_block *sb)
2574{
41ffe5d5
HD
2575 struct shmem_inode_info *info;
2576 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2577 if (!info)
1da177e4 2578 return NULL;
41ffe5d5 2579 return &info->vfs_inode;
1da177e4
LT
2580}
2581
41ffe5d5 2582static void shmem_destroy_callback(struct rcu_head *head)
fa0d7e3d
NP
2583{
2584 struct inode *inode = container_of(head, struct inode, i_rcu);
fa0d7e3d
NP
2585 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2586}
2587
1da177e4
LT
2588static void shmem_destroy_inode(struct inode *inode)
2589{
09208d15 2590 if (S_ISREG(inode->i_mode))
1da177e4 2591 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
41ffe5d5 2592 call_rcu(&inode->i_rcu, shmem_destroy_callback);
1da177e4
LT
2593}
2594
41ffe5d5 2595static void shmem_init_inode(void *foo)
1da177e4 2596{
41ffe5d5
HD
2597 struct shmem_inode_info *info = foo;
2598 inode_init_once(&info->vfs_inode);
1da177e4
LT
2599}
2600
41ffe5d5 2601static int shmem_init_inodecache(void)
1da177e4
LT
2602{
2603 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2604 sizeof(struct shmem_inode_info),
41ffe5d5 2605 0, SLAB_PANIC, shmem_init_inode);
1da177e4
LT
2606 return 0;
2607}
2608
41ffe5d5 2609static void shmem_destroy_inodecache(void)
1da177e4 2610{
1a1d92c1 2611 kmem_cache_destroy(shmem_inode_cachep);
1da177e4
LT
2612}
2613
f5e54d6e 2614static const struct address_space_operations shmem_aops = {
1da177e4 2615 .writepage = shmem_writepage,
76719325 2616 .set_page_dirty = __set_page_dirty_no_writeback,
1da177e4 2617#ifdef CONFIG_TMPFS
800d15a5
NP
2618 .write_begin = shmem_write_begin,
2619 .write_end = shmem_write_end,
1da177e4 2620#endif
304dbdb7 2621 .migratepage = migrate_page,
aa261f54 2622 .error_remove_page = generic_error_remove_page,
1da177e4
LT
2623};
2624
15ad7cdc 2625static const struct file_operations shmem_file_operations = {
1da177e4
LT
2626 .mmap = shmem_mmap,
2627#ifdef CONFIG_TMPFS
2628 .llseek = generic_file_llseek,
bcd78e49 2629 .read = do_sync_read,
5402b976 2630 .write = do_sync_write,
bcd78e49 2631 .aio_read = shmem_file_aio_read,
5402b976 2632 .aio_write = generic_file_aio_write,
1b061d92 2633 .fsync = noop_fsync,
708e3508 2634 .splice_read = shmem_file_splice_read,
ae976416 2635 .splice_write = generic_file_splice_write,
83e4fa9c 2636 .fallocate = shmem_fallocate,
1da177e4
LT
2637#endif
2638};
2639
92e1d5be 2640static const struct inode_operations shmem_inode_operations = {
94c1e62d 2641 .setattr = shmem_setattr,
b09e0fa4
EP
2642#ifdef CONFIG_TMPFS_XATTR
2643 .setxattr = shmem_setxattr,
2644 .getxattr = shmem_getxattr,
2645 .listxattr = shmem_listxattr,
2646 .removexattr = shmem_removexattr,
2647#endif
1da177e4
LT
2648};
2649
92e1d5be 2650static const struct inode_operations shmem_dir_inode_operations = {
1da177e4
LT
2651#ifdef CONFIG_TMPFS
2652 .create = shmem_create,
2653 .lookup = simple_lookup,
2654 .link = shmem_link,
2655 .unlink = shmem_unlink,
2656 .symlink = shmem_symlink,
2657 .mkdir = shmem_mkdir,
2658 .rmdir = shmem_rmdir,
2659 .mknod = shmem_mknod,
2660 .rename = shmem_rename,
1da177e4 2661#endif
b09e0fa4
EP
2662#ifdef CONFIG_TMPFS_XATTR
2663 .setxattr = shmem_setxattr,
2664 .getxattr = shmem_getxattr,
2665 .listxattr = shmem_listxattr,
2666 .removexattr = shmem_removexattr,
2667#endif
39f0247d 2668#ifdef CONFIG_TMPFS_POSIX_ACL
94c1e62d 2669 .setattr = shmem_setattr,
39f0247d
AG
2670#endif
2671};
2672
92e1d5be 2673static const struct inode_operations shmem_special_inode_operations = {
b09e0fa4
EP
2674#ifdef CONFIG_TMPFS_XATTR
2675 .setxattr = shmem_setxattr,
2676 .getxattr = shmem_getxattr,
2677 .listxattr = shmem_listxattr,
2678 .removexattr = shmem_removexattr,
2679#endif
39f0247d 2680#ifdef CONFIG_TMPFS_POSIX_ACL
94c1e62d 2681 .setattr = shmem_setattr,
39f0247d 2682#endif
1da177e4
LT
2683};
2684
759b9775 2685static const struct super_operations shmem_ops = {
1da177e4
LT
2686 .alloc_inode = shmem_alloc_inode,
2687 .destroy_inode = shmem_destroy_inode,
2688#ifdef CONFIG_TMPFS
2689 .statfs = shmem_statfs,
2690 .remount_fs = shmem_remount_fs,
680d794b 2691 .show_options = shmem_show_options,
1da177e4 2692#endif
1f895f75 2693 .evict_inode = shmem_evict_inode,
1da177e4
LT
2694 .drop_inode = generic_delete_inode,
2695 .put_super = shmem_put_super,
2696};
2697
f0f37e2f 2698static const struct vm_operations_struct shmem_vm_ops = {
54cb8821 2699 .fault = shmem_fault,
1da177e4
LT
2700#ifdef CONFIG_NUMA
2701 .set_policy = shmem_set_policy,
2702 .get_policy = shmem_get_policy,
2703#endif
2704};
2705
3c26ff6e
AV
2706static struct dentry *shmem_mount(struct file_system_type *fs_type,
2707 int flags, const char *dev_name, void *data)
1da177e4 2708{
3c26ff6e 2709 return mount_nodev(fs_type, flags, data, shmem_fill_super);
1da177e4
LT
2710}
2711
41ffe5d5 2712static struct file_system_type shmem_fs_type = {
1da177e4
LT
2713 .owner = THIS_MODULE,
2714 .name = "tmpfs",
3c26ff6e 2715 .mount = shmem_mount,
1da177e4
LT
2716 .kill_sb = kill_litter_super,
2717};
1da177e4 2718
41ffe5d5 2719int __init shmem_init(void)
1da177e4
LT
2720{
2721 int error;
2722
e0bf68dd
PZ
2723 error = bdi_init(&shmem_backing_dev_info);
2724 if (error)
2725 goto out4;
2726
41ffe5d5 2727 error = shmem_init_inodecache();
1da177e4
LT
2728 if (error)
2729 goto out3;
2730
41ffe5d5 2731 error = register_filesystem(&shmem_fs_type);
1da177e4
LT
2732 if (error) {
2733 printk(KERN_ERR "Could not register tmpfs\n");
2734 goto out2;
2735 }
95dc112a 2736
41ffe5d5
HD
2737 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2738 shmem_fs_type.name, NULL);
1da177e4
LT
2739 if (IS_ERR(shm_mnt)) {
2740 error = PTR_ERR(shm_mnt);
2741 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2742 goto out1;
2743 }
2744 return 0;
2745
2746out1:
41ffe5d5 2747 unregister_filesystem(&shmem_fs_type);
1da177e4 2748out2:
41ffe5d5 2749 shmem_destroy_inodecache();
1da177e4 2750out3:
e0bf68dd
PZ
2751 bdi_destroy(&shmem_backing_dev_info);
2752out4:
1da177e4
LT
2753 shm_mnt = ERR_PTR(error);
2754 return error;
2755}
853ac43a
MM
2756
2757#else /* !CONFIG_SHMEM */
2758
2759/*
2760 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2761 *
2762 * This is intended for small system where the benefits of the full
2763 * shmem code (swap-backed and resource-limited) are outweighed by
2764 * their complexity. On systems without swap this code should be
2765 * effectively equivalent, but much lighter weight.
2766 */
2767
2768#include <linux/ramfs.h>
2769
41ffe5d5 2770static struct file_system_type shmem_fs_type = {
853ac43a 2771 .name = "tmpfs",
3c26ff6e 2772 .mount = ramfs_mount,
853ac43a
MM
2773 .kill_sb = kill_litter_super,
2774};
2775
41ffe5d5 2776int __init shmem_init(void)
853ac43a 2777{
41ffe5d5 2778 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
853ac43a 2779
41ffe5d5 2780 shm_mnt = kern_mount(&shmem_fs_type);
853ac43a
MM
2781 BUG_ON(IS_ERR(shm_mnt));
2782
2783 return 0;
2784}
2785
41ffe5d5 2786int shmem_unuse(swp_entry_t swap, struct page *page)
853ac43a
MM
2787{
2788 return 0;
2789}
2790
3f96b79a
HD
2791int shmem_lock(struct file *file, int lock, struct user_struct *user)
2792{
2793 return 0;
2794}
2795
24513264
HD
2796void shmem_unlock_mapping(struct address_space *mapping)
2797{
2798}
2799
41ffe5d5 2800void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
94c1e62d 2801{
41ffe5d5 2802 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
94c1e62d
HD
2803}
2804EXPORT_SYMBOL_GPL(shmem_truncate_range);
2805
0b0a0806
HD
2806#define shmem_vm_ops generic_file_vm_ops
2807#define shmem_file_operations ramfs_file_operations
454abafe 2808#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
0b0a0806
HD
2809#define shmem_acct_size(flags, size) 0
2810#define shmem_unacct_size(flags, size) do {} while (0)
853ac43a
MM
2811
2812#endif /* CONFIG_SHMEM */
2813
2814/* common code */
1da177e4 2815
46711810 2816/**
1da177e4 2817 * shmem_file_setup - get an unlinked file living in tmpfs
1da177e4
LT
2818 * @name: name for dentry (to be seen in /proc/<pid>/maps
2819 * @size: size to be set for the file
0b0a0806 2820 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
1da177e4 2821 */
168f5ac6 2822struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
1da177e4
LT
2823{
2824 int error;
2825 struct file *file;
2826 struct inode *inode;
2c48b9c4
AV
2827 struct path path;
2828 struct dentry *root;
1da177e4
LT
2829 struct qstr this;
2830
2831 if (IS_ERR(shm_mnt))
2832 return (void *)shm_mnt;
2833
285b2c4f 2834 if (size < 0 || size > MAX_LFS_FILESIZE)
1da177e4
LT
2835 return ERR_PTR(-EINVAL);
2836
2837 if (shmem_acct_size(flags, size))
2838 return ERR_PTR(-ENOMEM);
2839
2840 error = -ENOMEM;
2841 this.name = name;
2842 this.len = strlen(name);
2843 this.hash = 0; /* will go */
2844 root = shm_mnt->mnt_root;
2c48b9c4
AV
2845 path.dentry = d_alloc(root, &this);
2846 if (!path.dentry)
1da177e4 2847 goto put_memory;
2c48b9c4 2848 path.mnt = mntget(shm_mnt);
1da177e4 2849
1da177e4 2850 error = -ENOSPC;
454abafe 2851 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
1da177e4 2852 if (!inode)
4b42af81 2853 goto put_dentry;
1da177e4 2854
2c48b9c4 2855 d_instantiate(path.dentry, inode);
1da177e4 2856 inode->i_size = size;
6d6b77f1 2857 clear_nlink(inode); /* It is unlinked */
853ac43a
MM
2858#ifndef CONFIG_MMU
2859 error = ramfs_nommu_expand_for_mapping(inode, size);
2860 if (error)
4b42af81 2861 goto put_dentry;
853ac43a 2862#endif
4b42af81
AV
2863
2864 error = -ENFILE;
2c48b9c4 2865 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
4b42af81
AV
2866 &shmem_file_operations);
2867 if (!file)
2868 goto put_dentry;
2869
1da177e4
LT
2870 return file;
2871
1da177e4 2872put_dentry:
2c48b9c4 2873 path_put(&path);
1da177e4
LT
2874put_memory:
2875 shmem_unacct_size(flags, size);
2876 return ERR_PTR(error);
2877}
395e0ddc 2878EXPORT_SYMBOL_GPL(shmem_file_setup);
1da177e4 2879
46711810 2880/**
1da177e4 2881 * shmem_zero_setup - setup a shared anonymous mapping
1da177e4
LT
2882 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2883 */
2884int shmem_zero_setup(struct vm_area_struct *vma)
2885{
2886 struct file *file;
2887 loff_t size = vma->vm_end - vma->vm_start;
2888
2889 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2890 if (IS_ERR(file))
2891 return PTR_ERR(file);
2892
2893 if (vma->vm_file)
2894 fput(vma->vm_file);
2895 vma->vm_file = file;
2896 vma->vm_ops = &shmem_vm_ops;
bee4c36a 2897 vma->vm_flags |= VM_CAN_NONLINEAR;
1da177e4
LT
2898 return 0;
2899}
d9d90e5e
HD
2900
2901/**
2902 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2903 * @mapping: the page's address_space
2904 * @index: the page index
2905 * @gfp: the page allocator flags to use if allocating
2906 *
2907 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2908 * with any new page allocations done using the specified allocation flags.
2909 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2910 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2911 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2912 *
68da9f05
HD
2913 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2914 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
d9d90e5e
HD
2915 */
2916struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2917 pgoff_t index, gfp_t gfp)
2918{
68da9f05
HD
2919#ifdef CONFIG_SHMEM
2920 struct inode *inode = mapping->host;
9276aad6 2921 struct page *page;
68da9f05
HD
2922 int error;
2923
2924 BUG_ON(mapping->a_ops != &shmem_aops);
2925 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2926 if (error)
2927 page = ERR_PTR(error);
2928 else
2929 unlock_page(page);
2930 return page;
2931#else
2932 /*
2933 * The tiny !SHMEM case uses ramfs without swap
2934 */
d9d90e5e 2935 return read_cache_page_gfp(mapping, index, gfp);
68da9f05 2936#endif
d9d90e5e
HD
2937}
2938EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);