BACKPORT: mm: Add an F_SEAL_FUTURE_WRITE seal to memfd
[GitHub/LineageOS/android_kernel_samsung_universal7580.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>
250297ed 28#include <linux/ramfs.h>
caefba17 29#include <linux/pagemap.h>
853ac43a
MM
30#include <linux/file.h>
31#include <linux/mm.h>
b95f1b31 32#include <linux/export.h>
853ac43a 33#include <linux/swap.h>
a27bb332 34#include <linux/aio.h>
853ac43a
MM
35
36static struct vfsmount *shm_mnt;
37
38#ifdef CONFIG_SHMEM
1da177e4
LT
39/*
40 * This virtual memory filesystem is heavily based on the ramfs. It
41 * extends ramfs by the ability to use swap and honor resource limits
42 * which makes it a completely usable filesystem.
43 */
44
39f0247d 45#include <linux/xattr.h>
a5694255 46#include <linux/exportfs.h>
1c7c474c 47#include <linux/posix_acl.h>
39f0247d 48#include <linux/generic_acl.h>
1da177e4 49#include <linux/mman.h>
1da177e4
LT
50#include <linux/string.h>
51#include <linux/slab.h>
52#include <linux/backing-dev.h>
53#include <linux/shmem_fs.h>
1da177e4 54#include <linux/writeback.h>
1da177e4 55#include <linux/blkdev.h>
bda97eab 56#include <linux/pagevec.h>
41ffe5d5 57#include <linux/percpu_counter.h>
83e4fa9c 58#include <linux/falloc.h>
708e3508 59#include <linux/splice.h>
1da177e4
LT
60#include <linux/security.h>
61#include <linux/swapops.h>
62#include <linux/mempolicy.h>
63#include <linux/namei.h>
b00dc3ad 64#include <linux/ctype.h>
304dbdb7 65#include <linux/migrate.h>
c1f60a5a 66#include <linux/highmem.h>
680d794b 67#include <linux/seq_file.h>
8a09920f 68#include <linux/syscalls.h>
92562927 69#include <linux/magic.h>
2194d4de 70#include <linux/fcntl.h>
8a09920f 71#include <uapi/linux/memfd.h>
304dbdb7 72
1da177e4 73#include <asm/uaccess.h>
1da177e4
LT
74#include <asm/pgtable.h>
75
caefba17 76#define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
1da177e4
LT
77#define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
78
1da177e4
LT
79/* Pretend that each entry is of this size in directory's i_size */
80#define BOGO_DIRENT_SIZE 20
81
69f07ec9
HD
82/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
83#define SHORT_SYMLINK_LEN 128
84
1aac1400 85/*
1ccc3ffa
HD
86 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
87 * inode->i_private (with i_mutex making sure that it has only one user at
88 * a time): we would prefer not to enlarge the shmem inode just for that.
1aac1400
HD
89 */
90struct shmem_falloc {
887675c9 91 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
1aac1400
HD
92 pgoff_t start; /* start of range currently being fallocated */
93 pgoff_t next; /* the next page offset to be fallocated */
94 pgoff_t nr_falloced; /* how many new pages have been fallocated */
95 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
96};
97
285b2c4f 98/* Flag allocation requirements to shmem_getpage */
1da177e4 99enum sgp_type {
1da177e4
LT
100 SGP_READ, /* don't exceed i_size, don't allocate page */
101 SGP_CACHE, /* don't exceed i_size, may allocate page */
a0ee5ec5 102 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
1635f6a7
HD
103 SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
104 SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
1da177e4
LT
105};
106
b76db735 107#ifdef CONFIG_TMPFS
680d794b 108static unsigned long shmem_default_max_blocks(void)
109{
110 return totalram_pages / 2;
111}
112
113static unsigned long shmem_default_max_inodes(void)
114{
115 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
116}
b76db735 117#endif
680d794b 118
bde05d1c
HD
119static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
120static int shmem_replace_page(struct page **pagep, gfp_t gfp,
121 struct shmem_inode_info *info, pgoff_t index);
68da9f05
HD
122static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
123 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
124
125static inline int shmem_getpage(struct inode *inode, pgoff_t index,
126 struct page **pagep, enum sgp_type sgp, int *fault_type)
127{
128 return shmem_getpage_gfp(inode, index, pagep, sgp,
129 mapping_gfp_mask(inode->i_mapping), fault_type);
130}
1da177e4 131
1da177e4
LT
132static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
133{
134 return sb->s_fs_info;
135}
136
137/*
138 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
139 * for shared memory and for shared anonymous (/dev/zero) mappings
140 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
141 * consistent with the pre-accounting of private mappings ...
142 */
143static inline int shmem_acct_size(unsigned long flags, loff_t size)
144{
0b0a0806 145 return (flags & VM_NORESERVE) ?
191c5424 146 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
1da177e4
LT
147}
148
149static inline void shmem_unacct_size(unsigned long flags, loff_t size)
150{
0b0a0806 151 if (!(flags & VM_NORESERVE))
1da177e4
LT
152 vm_unacct_memory(VM_ACCT(size));
153}
154
155/*
156 * ... whereas tmpfs objects are accounted incrementally as
157 * pages are allocated, in order to allow huge sparse files.
158 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
159 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
160 */
161static inline int shmem_acct_block(unsigned long flags)
162{
0b0a0806 163 return (flags & VM_NORESERVE) ?
191c5424 164 security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
1da177e4
LT
165}
166
167static inline void shmem_unacct_blocks(unsigned long flags, long pages)
168{
0b0a0806 169 if (flags & VM_NORESERVE)
1da177e4
LT
170 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
171}
172
759b9775 173static const struct super_operations shmem_ops;
f5e54d6e 174static const struct address_space_operations shmem_aops;
15ad7cdc 175static const struct file_operations shmem_file_operations;
92e1d5be
AV
176static const struct inode_operations shmem_inode_operations;
177static const struct inode_operations shmem_dir_inode_operations;
178static const struct inode_operations shmem_special_inode_operations;
f0f37e2f 179static const struct vm_operations_struct shmem_vm_ops;
1da177e4 180
6c231b7b 181static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
1da177e4 182 .ra_pages = 0, /* No readahead */
4f98a2fe 183 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
1da177e4
LT
184};
185
186static LIST_HEAD(shmem_swaplist);
cb5f7b9a 187static DEFINE_MUTEX(shmem_swaplist_mutex);
1da177e4 188
5b04c689
PE
189static int shmem_reserve_inode(struct super_block *sb)
190{
191 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
192 if (sbinfo->max_inodes) {
193 spin_lock(&sbinfo->stat_lock);
194 if (!sbinfo->free_inodes) {
195 spin_unlock(&sbinfo->stat_lock);
196 return -ENOSPC;
197 }
198 sbinfo->free_inodes--;
199 spin_unlock(&sbinfo->stat_lock);
200 }
201 return 0;
202}
203
204static void shmem_free_inode(struct super_block *sb)
205{
206 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
207 if (sbinfo->max_inodes) {
208 spin_lock(&sbinfo->stat_lock);
209 sbinfo->free_inodes++;
210 spin_unlock(&sbinfo->stat_lock);
211 }
212}
213
46711810 214/**
41ffe5d5 215 * shmem_recalc_inode - recalculate the block usage of an inode
1da177e4
LT
216 * @inode: inode to recalc
217 *
218 * We have to calculate the free blocks since the mm can drop
219 * undirtied hole pages behind our back.
220 *
221 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
222 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
223 *
224 * It has to be called with the spinlock held.
225 */
226static void shmem_recalc_inode(struct inode *inode)
227{
228 struct shmem_inode_info *info = SHMEM_I(inode);
229 long freed;
230
231 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
232 if (freed > 0) {
54af6042
HD
233 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
234 if (sbinfo->max_blocks)
235 percpu_counter_add(&sbinfo->used_blocks, -freed);
1da177e4 236 info->alloced -= freed;
54af6042 237 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
1da177e4 238 shmem_unacct_blocks(info->flags, freed);
1da177e4
LT
239 }
240}
241
7a5d0fbb
HD
242/*
243 * Replace item expected in radix tree by a new item, while holding tree lock.
244 */
245static int shmem_radix_tree_replace(struct address_space *mapping,
246 pgoff_t index, void *expected, void *replacement)
247{
248 void **pslot;
249 void *item = NULL;
250
251 VM_BUG_ON(!expected);
252 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
253 if (pslot)
254 item = radix_tree_deref_slot_protected(pslot,
255 &mapping->tree_lock);
256 if (item != expected)
257 return -ENOENT;
258 if (replacement)
259 radix_tree_replace_slot(pslot, replacement);
260 else
261 radix_tree_delete(&mapping->page_tree, index);
262 return 0;
263}
264
d1899228
HD
265/*
266 * Sometimes, before we decide whether to proceed or to fail, we must check
267 * that an entry was not already brought back from swap by a racing thread.
268 *
269 * Checking page is not enough: by the time a SwapCache page is locked, it
270 * might be reused, and again be SwapCache, using the same swap as before.
271 */
272static bool shmem_confirm_swap(struct address_space *mapping,
273 pgoff_t index, swp_entry_t swap)
274{
275 void *item;
276
277 rcu_read_lock();
278 item = radix_tree_lookup(&mapping->page_tree, index);
279 rcu_read_unlock();
280 return item == swp_to_radix_entry(swap);
281}
282
46f65ec1
HD
283/*
284 * Like add_to_page_cache_locked, but error if expected item has gone.
285 */
286static int shmem_add_to_page_cache(struct page *page,
287 struct address_space *mapping,
288 pgoff_t index, gfp_t gfp, void *expected)
289{
b065b432 290 int error;
46f65ec1
HD
291
292 VM_BUG_ON(!PageLocked(page));
293 VM_BUG_ON(!PageSwapBacked(page));
294
b065b432
HD
295 page_cache_get(page);
296 page->mapping = mapping;
297 page->index = index;
298
299 spin_lock_irq(&mapping->tree_lock);
46f65ec1 300 if (!expected)
b065b432
HD
301 error = radix_tree_insert(&mapping->page_tree, index, page);
302 else
303 error = shmem_radix_tree_replace(mapping, index, expected,
304 page);
46f65ec1 305 if (!error) {
b065b432
HD
306 mapping->nrpages++;
307 __inc_zone_page_state(page, NR_FILE_PAGES);
308 __inc_zone_page_state(page, NR_SHMEM);
309 spin_unlock_irq(&mapping->tree_lock);
310 } else {
311 page->mapping = NULL;
312 spin_unlock_irq(&mapping->tree_lock);
313 page_cache_release(page);
46f65ec1 314 }
46f65ec1
HD
315 return error;
316}
317
6922c0c7
HD
318/*
319 * Like delete_from_page_cache, but substitutes swap for page.
320 */
321static void shmem_delete_from_page_cache(struct page *page, void *radswap)
322{
323 struct address_space *mapping = page->mapping;
324 int error;
325
326 spin_lock_irq(&mapping->tree_lock);
327 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
328 page->mapping = NULL;
329 mapping->nrpages--;
330 __dec_zone_page_state(page, NR_FILE_PAGES);
331 __dec_zone_page_state(page, NR_SHMEM);
332 spin_unlock_irq(&mapping->tree_lock);
333 page_cache_release(page);
334 BUG_ON(error);
335}
336
7a5d0fbb
HD
337/*
338 * Like find_get_pages, but collecting swap entries as well as pages.
339 */
340static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
341 pgoff_t start, unsigned int nr_pages,
342 struct page **pages, pgoff_t *indices)
343{
860f2759
JW
344 void **slot;
345 unsigned int ret = 0;
346 struct radix_tree_iter iter;
347
348 if (!nr_pages)
349 return 0;
7a5d0fbb
HD
350
351 rcu_read_lock();
352restart:
860f2759 353 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
7a5d0fbb
HD
354 struct page *page;
355repeat:
860f2759 356 page = radix_tree_deref_slot(slot);
7a5d0fbb
HD
357 if (unlikely(!page))
358 continue;
359 if (radix_tree_exception(page)) {
8079b1c8
HD
360 if (radix_tree_deref_retry(page))
361 goto restart;
362 /*
363 * Otherwise, we must be storing a swap entry
364 * here as an exceptional entry: so return it
365 * without attempting to raise page count.
366 */
367 goto export;
7a5d0fbb
HD
368 }
369 if (!page_cache_get_speculative(page))
370 goto repeat;
371
372 /* Has the page moved? */
860f2759 373 if (unlikely(page != *slot)) {
7a5d0fbb
HD
374 page_cache_release(page);
375 goto repeat;
376 }
377export:
860f2759 378 indices[ret] = iter.index;
7a5d0fbb 379 pages[ret] = page;
860f2759
JW
380 if (++ret == nr_pages)
381 break;
7a5d0fbb 382 }
7a5d0fbb
HD
383 rcu_read_unlock();
384 return ret;
385}
386
387/*
388 * Remove swap entry from radix tree, free the swap and its page cache.
389 */
390static int shmem_free_swap(struct address_space *mapping,
391 pgoff_t index, void *radswap)
392{
393 int error;
394
395 spin_lock_irq(&mapping->tree_lock);
396 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
397 spin_unlock_irq(&mapping->tree_lock);
398 if (!error)
399 free_swap_and_cache(radix_to_swp_entry(radswap));
400 return error;
401}
402
403/*
404 * Pagevec may contain swap entries, so shuffle up pages before releasing.
405 */
24513264 406static void shmem_deswap_pagevec(struct pagevec *pvec)
7a5d0fbb
HD
407{
408 int i, j;
409
410 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
411 struct page *page = pvec->pages[i];
412 if (!radix_tree_exceptional_entry(page))
413 pvec->pages[j++] = page;
414 }
415 pvec->nr = j;
24513264
HD
416}
417
418/*
419 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
420 */
421void shmem_unlock_mapping(struct address_space *mapping)
422{
423 struct pagevec pvec;
424 pgoff_t indices[PAGEVEC_SIZE];
425 pgoff_t index = 0;
426
427 pagevec_init(&pvec, 0);
428 /*
429 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
430 */
431 while (!mapping_unevictable(mapping)) {
432 /*
433 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
434 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
435 */
436 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
437 PAGEVEC_SIZE, pvec.pages, indices);
438 if (!pvec.nr)
439 break;
440 index = indices[pvec.nr - 1] + 1;
441 shmem_deswap_pagevec(&pvec);
442 check_move_unevictable_pages(pvec.pages, pvec.nr);
443 pagevec_release(&pvec);
444 cond_resched();
445 }
7a5d0fbb
HD
446}
447
448/*
449 * Remove range of pages and swap entries from radix tree, and free them.
1635f6a7 450 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
7a5d0fbb 451 */
1635f6a7
HD
452static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
453 bool unfalloc)
1da177e4 454{
285b2c4f 455 struct address_space *mapping = inode->i_mapping;
1da177e4 456 struct shmem_inode_info *info = SHMEM_I(inode);
285b2c4f 457 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
83e4fa9c
HD
458 pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
459 unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
460 unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
bda97eab 461 struct pagevec pvec;
7a5d0fbb
HD
462 pgoff_t indices[PAGEVEC_SIZE];
463 long nr_swaps_freed = 0;
285b2c4f 464 pgoff_t index;
bda97eab
HD
465 int i;
466
83e4fa9c
HD
467 if (lend == -1)
468 end = -1; /* unsigned, so actually very big */
bda97eab
HD
469
470 pagevec_init(&pvec, 0);
471 index = start;
83e4fa9c 472 while (index < end) {
7a5d0fbb 473 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
83e4fa9c 474 min(end - index, (pgoff_t)PAGEVEC_SIZE),
7a5d0fbb
HD
475 pvec.pages, indices);
476 if (!pvec.nr)
477 break;
bda97eab
HD
478 mem_cgroup_uncharge_start();
479 for (i = 0; i < pagevec_count(&pvec); i++) {
480 struct page *page = pvec.pages[i];
481
7a5d0fbb 482 index = indices[i];
83e4fa9c 483 if (index >= end)
bda97eab
HD
484 break;
485
7a5d0fbb 486 if (radix_tree_exceptional_entry(page)) {
1635f6a7
HD
487 if (unfalloc)
488 continue;
3c2a0909
S
489 if (shmem_free_swap(mapping, index, page)) {
490 /* Swap was replaced by page: retry */
491 index--;
492 break;
493 }
494 nr_swaps_freed++;
bda97eab 495 continue;
7a5d0fbb
HD
496 }
497
498 if (!trylock_page(page))
bda97eab 499 continue;
1635f6a7
HD
500 if (!unfalloc || !PageUptodate(page)) {
501 if (page->mapping == mapping) {
502 VM_BUG_ON(PageWriteback(page));
503 truncate_inode_page(mapping, page);
3c2a0909
S
504 } else {
505 /* Page was replaced by swap: retry */
506 unlock_page(page);
507 index--;
508 break;
1635f6a7 509 }
bda97eab 510 }
bda97eab
HD
511 unlock_page(page);
512 }
24513264
HD
513 shmem_deswap_pagevec(&pvec);
514 pagevec_release(&pvec);
bda97eab
HD
515 mem_cgroup_uncharge_end();
516 cond_resched();
517 index++;
518 }
1da177e4 519
83e4fa9c 520 if (partial_start) {
bda97eab
HD
521 struct page *page = NULL;
522 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
523 if (page) {
83e4fa9c
HD
524 unsigned int top = PAGE_CACHE_SIZE;
525 if (start > end) {
526 top = partial_end;
527 partial_end = 0;
528 }
529 zero_user_segment(page, partial_start, top);
530 set_page_dirty(page);
531 unlock_page(page);
532 page_cache_release(page);
533 }
534 }
535 if (partial_end) {
536 struct page *page = NULL;
537 shmem_getpage(inode, end, &page, SGP_READ, NULL);
538 if (page) {
539 zero_user_segment(page, 0, partial_end);
bda97eab
HD
540 set_page_dirty(page);
541 unlock_page(page);
542 page_cache_release(page);
543 }
544 }
83e4fa9c
HD
545 if (start >= end)
546 return;
bda97eab
HD
547
548 index = start;
7dc7fb43 549 while (index < end) {
bda97eab 550 cond_resched();
7a5d0fbb 551 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
83e4fa9c 552 min(end - index, (pgoff_t)PAGEVEC_SIZE),
7a5d0fbb
HD
553 pvec.pages, indices);
554 if (!pvec.nr) {
7dc7fb43
HD
555 /* If all gone or hole-punch or unfalloc, we're done */
556 if (index == start || end != -1)
bda97eab 557 break;
7dc7fb43 558 /* But if truncating, restart to make sure all gone */
bda97eab
HD
559 index = start;
560 continue;
561 }
bda97eab
HD
562 mem_cgroup_uncharge_start();
563 for (i = 0; i < pagevec_count(&pvec); i++) {
564 struct page *page = pvec.pages[i];
565
7a5d0fbb 566 index = indices[i];
83e4fa9c 567 if (index >= end)
bda97eab
HD
568 break;
569
7a5d0fbb 570 if (radix_tree_exceptional_entry(page)) {
1635f6a7
HD
571 if (unfalloc)
572 continue;
7dc7fb43
HD
573 if (shmem_free_swap(mapping, index, page)) {
574 /* Swap was replaced by page: retry */
575 index--;
576 break;
577 }
578 nr_swaps_freed++;
7a5d0fbb
HD
579 continue;
580 }
581
bda97eab 582 lock_page(page);
1635f6a7
HD
583 if (!unfalloc || !PageUptodate(page)) {
584 if (page->mapping == mapping) {
585 VM_BUG_ON(PageWriteback(page));
586 truncate_inode_page(mapping, page);
7dc7fb43
HD
587 } else {
588 /* Page was replaced by swap: retry */
589 unlock_page(page);
590 index--;
591 break;
1635f6a7 592 }
7a5d0fbb 593 }
bda97eab
HD
594 unlock_page(page);
595 }
24513264
HD
596 shmem_deswap_pagevec(&pvec);
597 pagevec_release(&pvec);
bda97eab
HD
598 mem_cgroup_uncharge_end();
599 index++;
600 }
94c1e62d 601
1da177e4 602 spin_lock(&info->lock);
7a5d0fbb 603 info->swapped -= nr_swaps_freed;
1da177e4
LT
604 shmem_recalc_inode(inode);
605 spin_unlock(&info->lock);
1635f6a7 606}
1da177e4 607
1635f6a7
HD
608void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
609{
610 shmem_undo_range(inode, lstart, lend, false);
285b2c4f 611 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1da177e4 612}
94c1e62d 613EXPORT_SYMBOL_GPL(shmem_truncate_range);
1da177e4 614
94c1e62d 615static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
1da177e4
LT
616{
617 struct inode *inode = dentry->d_inode;
2194d4de 618 struct shmem_inode_info *info = SHMEM_I(inode);
1da177e4
LT
619 int error;
620
db78b877
CH
621 error = inode_change_ok(inode, attr);
622 if (error)
623 return error;
624
94c1e62d
HD
625 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
626 loff_t oldsize = inode->i_size;
627 loff_t newsize = attr->ia_size;
3889e6e7 628
2194d4de
DH
629 /* protected by i_mutex */
630 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
631 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
632 return -EPERM;
633
94c1e62d
HD
634 if (newsize != oldsize) {
635 i_size_write(inode, newsize);
636 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
637 }
638 if (newsize < oldsize) {
639 loff_t holebegin = round_up(newsize, PAGE_SIZE);
640 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
641 shmem_truncate_range(inode, newsize, (loff_t)-1);
642 /* unmap again to remove racily COWed private pages */
643 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
644 }
1da177e4
LT
645 }
646
db78b877 647 setattr_copy(inode, attr);
39f0247d 648#ifdef CONFIG_TMPFS_POSIX_ACL
db78b877 649 if (attr->ia_valid & ATTR_MODE)
1c7c474c 650 error = generic_acl_chmod(inode);
39f0247d 651#endif
1da177e4
LT
652 return error;
653}
654
1f895f75 655static void shmem_evict_inode(struct inode *inode)
1da177e4 656{
1da177e4
LT
657 struct shmem_inode_info *info = SHMEM_I(inode);
658
3889e6e7 659 if (inode->i_mapping->a_ops == &shmem_aops) {
1da177e4
LT
660 shmem_unacct_size(info->flags, inode->i_size);
661 inode->i_size = 0;
3889e6e7 662 shmem_truncate_range(inode, 0, (loff_t)-1);
1da177e4 663 if (!list_empty(&info->swaplist)) {
cb5f7b9a 664 mutex_lock(&shmem_swaplist_mutex);
1da177e4 665 list_del_init(&info->swaplist);
cb5f7b9a 666 mutex_unlock(&shmem_swaplist_mutex);
1da177e4 667 }
69f07ec9
HD
668 } else
669 kfree(info->symlink);
b09e0fa4 670
38f38657 671 simple_xattrs_free(&info->xattrs);
0f3c42f5 672 WARN_ON(inode->i_blocks);
5b04c689 673 shmem_free_inode(inode->i_sb);
dbd5768f 674 clear_inode(inode);
1da177e4
LT
675}
676
46f65ec1
HD
677/*
678 * If swap found in inode, free it and move page from swapcache to filecache.
679 */
41ffe5d5 680static int shmem_unuse_inode(struct shmem_inode_info *info,
bde05d1c 681 swp_entry_t swap, struct page **pagep)
1da177e4 682{
285b2c4f 683 struct address_space *mapping = info->vfs_inode.i_mapping;
46f65ec1 684 void *radswap;
41ffe5d5 685 pgoff_t index;
bde05d1c
HD
686 gfp_t gfp;
687 int error = 0;
1da177e4 688
46f65ec1 689 radswap = swp_to_radix_entry(swap);
e504f3fd 690 index = radix_tree_locate_item(&mapping->page_tree, radswap);
46f65ec1 691 if (index == -1)
285b2c4f 692 return 0;
2e0e26c7 693
1b1b32f2
HD
694 /*
695 * Move _head_ to start search for next from here.
1f895f75 696 * But be careful: shmem_evict_inode checks list_empty without taking
1b1b32f2 697 * mutex, and there's an instant in list_move_tail when info->swaplist
285b2c4f 698 * would appear empty, if it were the only one on shmem_swaplist.
1b1b32f2
HD
699 */
700 if (shmem_swaplist.next != &info->swaplist)
701 list_move_tail(&shmem_swaplist, &info->swaplist);
2e0e26c7 702
bde05d1c
HD
703 gfp = mapping_gfp_mask(mapping);
704 if (shmem_should_replace_page(*pagep, gfp)) {
705 mutex_unlock(&shmem_swaplist_mutex);
706 error = shmem_replace_page(pagep, gfp, info, index);
707 mutex_lock(&shmem_swaplist_mutex);
708 /*
709 * We needed to drop mutex to make that restrictive page
0142ef6c
HD
710 * allocation, but the inode might have been freed while we
711 * dropped it: although a racing shmem_evict_inode() cannot
712 * complete without emptying the radix_tree, our page lock
713 * on this swapcache page is not enough to prevent that -
714 * free_swap_and_cache() of our swap entry will only
715 * trylock_page(), removing swap from radix_tree whatever.
716 *
717 * We must not proceed to shmem_add_to_page_cache() if the
718 * inode has been freed, but of course we cannot rely on
719 * inode or mapping or info to check that. However, we can
720 * safely check if our swap entry is still in use (and here
721 * it can't have got reused for another page): if it's still
722 * in use, then the inode cannot have been freed yet, and we
723 * can safely proceed (if it's no longer in use, that tells
724 * nothing about the inode, but we don't need to unuse swap).
bde05d1c
HD
725 */
726 if (!page_swapcount(*pagep))
727 error = -ENOENT;
728 }
729
d13d1443 730 /*
778dd893
HD
731 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
732 * but also to hold up shmem_evict_inode(): so inode cannot be freed
733 * beneath us (pagelock doesn't help until the page is in pagecache).
d13d1443 734 */
bde05d1c
HD
735 if (!error)
736 error = shmem_add_to_page_cache(*pagep, mapping, index,
46f65ec1 737 GFP_NOWAIT, radswap);
48f170fb 738 if (error != -ENOMEM) {
46f65ec1
HD
739 /*
740 * Truncation and eviction use free_swap_and_cache(), which
741 * only does trylock page: if we raced, best clean up here.
742 */
bde05d1c
HD
743 delete_from_swap_cache(*pagep);
744 set_page_dirty(*pagep);
46f65ec1
HD
745 if (!error) {
746 spin_lock(&info->lock);
747 info->swapped--;
748 spin_unlock(&info->lock);
749 swap_free(swap);
750 }
2e0e26c7 751 error = 1; /* not an error, but entry was found */
1da177e4 752 }
2e0e26c7 753 return error;
1da177e4
LT
754}
755
756/*
46f65ec1 757 * Search through swapped inodes to find and replace swap by page.
1da177e4 758 */
41ffe5d5 759int shmem_unuse(swp_entry_t swap, struct page *page)
1da177e4 760{
41ffe5d5 761 struct list_head *this, *next;
1da177e4
LT
762 struct shmem_inode_info *info;
763 int found = 0;
bde05d1c
HD
764 int error = 0;
765
766 /*
767 * There's a faint possibility that swap page was replaced before
0142ef6c 768 * caller locked it: caller will come back later with the right page.
bde05d1c 769 */
0142ef6c 770 if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
bde05d1c 771 goto out;
778dd893
HD
772
773 /*
774 * Charge page using GFP_KERNEL while we can wait, before taking
775 * the shmem_swaplist_mutex which might hold up shmem_writepage().
776 * Charged back to the user (not to caller) when swap account is used.
778dd893
HD
777 */
778 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
779 if (error)
780 goto out;
46f65ec1 781 /* No radix_tree_preload: swap entry keeps a place for page in tree */
1da177e4 782
cb5f7b9a 783 mutex_lock(&shmem_swaplist_mutex);
41ffe5d5
HD
784 list_for_each_safe(this, next, &shmem_swaplist) {
785 info = list_entry(this, struct shmem_inode_info, swaplist);
285b2c4f 786 if (info->swapped)
bde05d1c 787 found = shmem_unuse_inode(info, swap, &page);
6922c0c7
HD
788 else
789 list_del_init(&info->swaplist);
cb5f7b9a 790 cond_resched();
2e0e26c7 791 if (found)
778dd893 792 break;
1da177e4 793 }
cb5f7b9a 794 mutex_unlock(&shmem_swaplist_mutex);
778dd893 795
778dd893
HD
796 if (found < 0)
797 error = found;
798out:
aaa46865
HD
799 unlock_page(page);
800 page_cache_release(page);
778dd893 801 return error;
1da177e4
LT
802}
803
804/*
805 * Move the page from the page cache to the swap cache.
806 */
807static int shmem_writepage(struct page *page, struct writeback_control *wbc)
808{
809 struct shmem_inode_info *info;
1da177e4 810 struct address_space *mapping;
1da177e4 811 struct inode *inode;
6922c0c7
HD
812 swp_entry_t swap;
813 pgoff_t index;
1da177e4
LT
814
815 BUG_ON(!PageLocked(page));
1da177e4
LT
816 mapping = page->mapping;
817 index = page->index;
818 inode = mapping->host;
819 info = SHMEM_I(inode);
820 if (info->flags & VM_LOCKED)
821 goto redirty;
d9fe526a 822 if (!total_swap_pages)
1da177e4
LT
823 goto redirty;
824
d9fe526a
HD
825 /*
826 * shmem_backing_dev_info's capabilities prevent regular writeback or
827 * sync from ever calling shmem_writepage; but a stacking filesystem
48f170fb 828 * might use ->writepage of its underlying filesystem, in which case
d9fe526a 829 * tmpfs should write out to swap only in response to memory pressure,
48f170fb 830 * and not for the writeback threads or sync.
d9fe526a 831 */
48f170fb
HD
832 if (!wbc->for_reclaim) {
833 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
834 goto redirty;
835 }
1635f6a7
HD
836
837 /*
838 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
839 * value into swapfile.c, the only way we can correctly account for a
840 * fallocated page arriving here is now to initialize it and write it.
1aac1400
HD
841 *
842 * That's okay for a page already fallocated earlier, but if we have
843 * not yet completed the fallocation, then (a) we want to keep track
844 * of this page in case we have to undo it, and (b) it may not be a
845 * good idea to continue anyway, once we're pushing into swap. So
846 * reactivate the page, and let shmem_fallocate() quit when too many.
1635f6a7
HD
847 */
848 if (!PageUptodate(page)) {
1aac1400
HD
849 if (inode->i_private) {
850 struct shmem_falloc *shmem_falloc;
851 spin_lock(&inode->i_lock);
852 shmem_falloc = inode->i_private;
853 if (shmem_falloc &&
887675c9 854 !shmem_falloc->waitq &&
1aac1400
HD
855 index >= shmem_falloc->start &&
856 index < shmem_falloc->next)
857 shmem_falloc->nr_unswapped++;
858 else
859 shmem_falloc = NULL;
860 spin_unlock(&inode->i_lock);
861 if (shmem_falloc)
862 goto redirty;
863 }
1635f6a7
HD
864 clear_highpage(page);
865 flush_dcache_page(page);
866 SetPageUptodate(page);
867 }
868
48f170fb
HD
869 swap = get_swap_page();
870 if (!swap.val)
871 goto redirty;
d9fe526a 872
b1dea800
HD
873 /*
874 * Add inode to shmem_unuse()'s list of swapped-out inodes,
6922c0c7
HD
875 * if it's not already there. Do it now before the page is
876 * moved to swap cache, when its pagelock no longer protects
b1dea800 877 * the inode from eviction. But don't unlock the mutex until
6922c0c7
HD
878 * we've incremented swapped, because shmem_unuse_inode() will
879 * prune a !swapped inode from the swaplist under this mutex.
b1dea800 880 */
48f170fb
HD
881 mutex_lock(&shmem_swaplist_mutex);
882 if (list_empty(&info->swaplist))
883 list_add_tail(&info->swaplist, &shmem_swaplist);
b1dea800 884
48f170fb 885 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
aaa46865 886 swap_shmem_alloc(swap);
6922c0c7
HD
887 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
888
889 spin_lock(&info->lock);
890 info->swapped++;
891 shmem_recalc_inode(inode);
826267cf 892 spin_unlock(&info->lock);
6922c0c7
HD
893
894 mutex_unlock(&shmem_swaplist_mutex);
d9fe526a 895 BUG_ON(page_mapped(page));
9fab5619 896 swap_writepage(page, wbc);
1da177e4
LT
897 return 0;
898 }
899
6922c0c7 900 mutex_unlock(&shmem_swaplist_mutex);
cb4b86ba 901 swapcache_free(swap, NULL);
1da177e4
LT
902redirty:
903 set_page_dirty(page);
d9fe526a
HD
904 if (wbc->for_reclaim)
905 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
906 unlock_page(page);
907 return 0;
1da177e4
LT
908}
909
910#ifdef CONFIG_NUMA
680d794b 911#ifdef CONFIG_TMPFS
71fe804b 912static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
680d794b 913{
095f1fc4 914 char buffer[64];
680d794b 915
71fe804b 916 if (!mpol || mpol->mode == MPOL_DEFAULT)
095f1fc4 917 return; /* show nothing */
680d794b 918
a7a88b23 919 mpol_to_str(buffer, sizeof(buffer), mpol);
095f1fc4
LS
920
921 seq_printf(seq, ",mpol=%s", buffer);
680d794b 922}
71fe804b
LS
923
924static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
925{
926 struct mempolicy *mpol = NULL;
927 if (sbinfo->mpol) {
928 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
929 mpol = sbinfo->mpol;
930 mpol_get(mpol);
931 spin_unlock(&sbinfo->stat_lock);
932 }
933 return mpol;
934}
680d794b 935#endif /* CONFIG_TMPFS */
936
41ffe5d5
HD
937static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
938 struct shmem_inode_info *info, pgoff_t index)
1da177e4 939{
1da177e4 940 struct vm_area_struct pvma;
18a2f371 941 struct page *page;
52cd3b07 942
1da177e4 943 /* Create a pseudo vma that just contains the policy */
c4cc6d07 944 pvma.vm_start = 0;
09c231cb
NZ
945 /* Bias interleave by inode number to distribute better across nodes */
946 pvma.vm_pgoff = index + info->vfs_inode.i_ino;
c4cc6d07 947 pvma.vm_ops = NULL;
18a2f371
MG
948 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
949
950 page = swapin_readahead(swap, gfp, &pvma, 0);
951
952 /* Drop reference taken by mpol_shared_policy_lookup() */
953 mpol_cond_put(pvma.vm_policy);
954
955 return page;
1da177e4
LT
956}
957
02098fea 958static struct page *shmem_alloc_page(gfp_t gfp,
41ffe5d5 959 struct shmem_inode_info *info, pgoff_t index)
1da177e4
LT
960{
961 struct vm_area_struct pvma;
18a2f371 962 struct page *page;
1da177e4 963
c4cc6d07
HD
964 /* Create a pseudo vma that just contains the policy */
965 pvma.vm_start = 0;
09c231cb
NZ
966 /* Bias interleave by inode number to distribute better across nodes */
967 pvma.vm_pgoff = index + info->vfs_inode.i_ino;
c4cc6d07 968 pvma.vm_ops = NULL;
41ffe5d5 969 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
52cd3b07 970
18a2f371 971 page = alloc_page_vma(gfp, &pvma, 0);
3c2a0909
S
972 if (page && is_cma_pageblock(page)) {
973 __free_page(page);
974 page = alloc_pages(gfp & ~__GFP_MOVABLE, 0);
975 }
18a2f371
MG
976
977 /* Drop reference taken by mpol_shared_policy_lookup() */
978 mpol_cond_put(pvma.vm_policy);
979
980 return page;
1da177e4 981}
680d794b 982#else /* !CONFIG_NUMA */
983#ifdef CONFIG_TMPFS
41ffe5d5 984static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
680d794b 985{
986}
987#endif /* CONFIG_TMPFS */
988
41ffe5d5
HD
989static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
990 struct shmem_inode_info *info, pgoff_t index)
1da177e4 991{
41ffe5d5 992 return swapin_readahead(swap, gfp, NULL, 0);
1da177e4
LT
993}
994
02098fea 995static inline struct page *shmem_alloc_page(gfp_t gfp,
41ffe5d5 996 struct shmem_inode_info *info, pgoff_t index)
1da177e4 997{
e84e2e13 998 return alloc_page(gfp);
1da177e4 999}
680d794b 1000#endif /* CONFIG_NUMA */
1da177e4 1001
71fe804b
LS
1002#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
1003static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1004{
1005 return NULL;
1006}
1007#endif
1008
bde05d1c
HD
1009/*
1010 * When a page is moved from swapcache to shmem filecache (either by the
1011 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1012 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1013 * ignorance of the mapping it belongs to. If that mapping has special
1014 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1015 * we may need to copy to a suitable page before moving to filecache.
1016 *
1017 * In a future release, this may well be extended to respect cpuset and
1018 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1019 * but for now it is a simple matter of zone.
1020 */
1021static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1022{
1023 return page_zonenum(page) > gfp_zone(gfp);
1024}
1025
1026static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1027 struct shmem_inode_info *info, pgoff_t index)
1028{
1029 struct page *oldpage, *newpage;
1030 struct address_space *swap_mapping;
1031 pgoff_t swap_index;
1032 int error;
1033
1034 oldpage = *pagep;
1035 swap_index = page_private(oldpage);
1036 swap_mapping = page_mapping(oldpage);
1037
1038 /*
1039 * We have arrived here because our zones are constrained, so don't
1040 * limit chance of success by further cpuset and node constraints.
1041 */
1042 gfp &= ~GFP_CONSTRAINT_MASK;
1043 newpage = shmem_alloc_page(gfp, info, index);
1044 if (!newpage)
1045 return -ENOMEM;
bde05d1c 1046
bde05d1c
HD
1047 page_cache_get(newpage);
1048 copy_highpage(newpage, oldpage);
0142ef6c 1049 flush_dcache_page(newpage);
bde05d1c 1050
bde05d1c 1051 __set_page_locked(newpage);
bde05d1c 1052 SetPageUptodate(newpage);
bde05d1c 1053 SetPageSwapBacked(newpage);
bde05d1c 1054 set_page_private(newpage, swap_index);
bde05d1c
HD
1055 SetPageSwapCache(newpage);
1056
1057 /*
1058 * Our caller will very soon move newpage out of swapcache, but it's
1059 * a nice clean interface for us to replace oldpage by newpage there.
1060 */
1061 spin_lock_irq(&swap_mapping->tree_lock);
1062 error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
1063 newpage);
0142ef6c
HD
1064 if (!error) {
1065 __inc_zone_page_state(newpage, NR_FILE_PAGES);
1066 __dec_zone_page_state(oldpage, NR_FILE_PAGES);
1067 }
bde05d1c 1068 spin_unlock_irq(&swap_mapping->tree_lock);
bde05d1c 1069
0142ef6c
HD
1070 if (unlikely(error)) {
1071 /*
1072 * Is this possible? I think not, now that our callers check
1073 * both PageSwapCache and page_private after getting page lock;
1074 * but be defensive. Reverse old to newpage for clear and free.
1075 */
1076 oldpage = newpage;
1077 } else {
1078 mem_cgroup_replace_page_cache(oldpage, newpage);
1079 lru_cache_add_anon(newpage);
1080 *pagep = newpage;
1081 }
bde05d1c
HD
1082
1083 ClearPageSwapCache(oldpage);
1084 set_page_private(oldpage, 0);
1085
1086 unlock_page(oldpage);
1087 page_cache_release(oldpage);
1088 page_cache_release(oldpage);
0142ef6c 1089 return error;
bde05d1c
HD
1090}
1091
1da177e4 1092/*
68da9f05 1093 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
1da177e4
LT
1094 *
1095 * If we allocate a new one we do not mark it dirty. That's up to the
1096 * vm. If we swap it in we mark it dirty since we also free the swap
1097 * entry since a page cannot live in both the swap and page cache
1098 */
41ffe5d5 1099static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
68da9f05 1100 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
1da177e4
LT
1101{
1102 struct address_space *mapping = inode->i_mapping;
54af6042 1103 struct shmem_inode_info *info;
1da177e4 1104 struct shmem_sb_info *sbinfo;
27ab7006 1105 struct page *page;
1da177e4
LT
1106 swp_entry_t swap;
1107 int error;
54af6042 1108 int once = 0;
1635f6a7 1109 int alloced = 0;
1da177e4 1110
41ffe5d5 1111 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
1da177e4 1112 return -EFBIG;
1da177e4 1113repeat:
54af6042 1114 swap.val = 0;
41ffe5d5 1115 page = find_lock_page(mapping, index);
54af6042
HD
1116 if (radix_tree_exceptional_entry(page)) {
1117 swap = radix_to_swp_entry(page);
1118 page = NULL;
1119 }
1120
1635f6a7 1121 if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
54af6042
HD
1122 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1123 error = -EINVAL;
1124 goto failed;
1125 }
1126
1635f6a7
HD
1127 /* fallocated page? */
1128 if (page && !PageUptodate(page)) {
1129 if (sgp != SGP_READ)
1130 goto clear;
1131 unlock_page(page);
1132 page_cache_release(page);
1133 page = NULL;
1134 }
54af6042 1135 if (page || (sgp == SGP_READ && !swap.val)) {
54af6042
HD
1136 *pagep = page;
1137 return 0;
27ab7006
HD
1138 }
1139
1140 /*
54af6042
HD
1141 * Fast cache lookup did not find it:
1142 * bring it back from swap or allocate.
27ab7006 1143 */
54af6042
HD
1144 info = SHMEM_I(inode);
1145 sbinfo = SHMEM_SB(inode->i_sb);
1da177e4 1146
1da177e4
LT
1147 if (swap.val) {
1148 /* Look it up and read it in.. */
27ab7006
HD
1149 page = lookup_swap_cache(swap);
1150 if (!page) {
1da177e4 1151 /* here we actually do the io */
68da9f05
HD
1152 if (fault_type)
1153 *fault_type |= VM_FAULT_MAJOR;
41ffe5d5 1154 page = shmem_swapin(swap, gfp, info, index);
27ab7006 1155 if (!page) {
54af6042
HD
1156 error = -ENOMEM;
1157 goto failed;
1da177e4 1158 }
1da177e4
LT
1159 }
1160
1161 /* We have to do this with page locked to prevent races */
54af6042 1162 lock_page(page);
0142ef6c 1163 if (!PageSwapCache(page) || page_private(page) != swap.val ||
d1899228 1164 !shmem_confirm_swap(mapping, index, swap)) {
bde05d1c 1165 error = -EEXIST; /* try again */
d1899228 1166 goto unlock;
bde05d1c 1167 }
27ab7006 1168 if (!PageUptodate(page)) {
1da177e4 1169 error = -EIO;
54af6042 1170 goto failed;
1da177e4 1171 }
54af6042
HD
1172 wait_on_page_writeback(page);
1173
bde05d1c
HD
1174 if (shmem_should_replace_page(page, gfp)) {
1175 error = shmem_replace_page(&page, gfp, info, index);
1176 if (error)
1177 goto failed;
1da177e4 1178 }
27ab7006 1179
aa3b1895
HD
1180 error = mem_cgroup_cache_charge(page, current->mm,
1181 gfp & GFP_RECLAIM_MASK);
d1899228 1182 if (!error) {
aa3b1895
HD
1183 error = shmem_add_to_page_cache(page, mapping, index,
1184 gfp, swp_to_radix_entry(swap));
215c02bc
HD
1185 /*
1186 * We already confirmed swap under page lock, and make
1187 * no memory allocation here, so usually no possibility
1188 * of error; but free_swap_and_cache() only trylocks a
1189 * page, so it is just possible that the entry has been
1190 * truncated or holepunched since swap was confirmed.
1191 * shmem_undo_range() will have done some of the
1192 * unaccounting, now delete_from_swap_cache() will do
1193 * the rest (including mem_cgroup_uncharge_swapcache).
1194 * Reset swap.val? No, leave it so "failed" goes back to
1195 * "repeat": reading a hole and writing should succeed.
1196 */
1197 if (error)
1198 delete_from_swap_cache(page);
d1899228 1199 }
54af6042
HD
1200 if (error)
1201 goto failed;
1202
1203 spin_lock(&info->lock);
285b2c4f 1204 info->swapped--;
54af6042 1205 shmem_recalc_inode(inode);
27ab7006 1206 spin_unlock(&info->lock);
54af6042
HD
1207
1208 delete_from_swap_cache(page);
27ab7006
HD
1209 set_page_dirty(page);
1210 swap_free(swap);
1211
54af6042
HD
1212 } else {
1213 if (shmem_acct_block(info->flags)) {
1214 error = -ENOSPC;
1215 goto failed;
1da177e4 1216 }
0edd73b3 1217 if (sbinfo->max_blocks) {
fc5da22a 1218 if (percpu_counter_compare(&sbinfo->used_blocks,
54af6042
HD
1219 sbinfo->max_blocks) >= 0) {
1220 error = -ENOSPC;
1221 goto unacct;
1222 }
7e496299 1223 percpu_counter_inc(&sbinfo->used_blocks);
54af6042 1224 }
1da177e4 1225
54af6042
HD
1226 page = shmem_alloc_page(gfp, info, index);
1227 if (!page) {
1228 error = -ENOMEM;
1229 goto decused;
1da177e4
LT
1230 }
1231
54af6042
HD
1232 SetPageSwapBacked(page);
1233 __set_page_locked(page);
aa3b1895
HD
1234 error = mem_cgroup_cache_charge(page, current->mm,
1235 gfp & GFP_RECLAIM_MASK);
54af6042
HD
1236 if (error)
1237 goto decused;
b065b432
HD
1238 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
1239 if (!error) {
1240 error = shmem_add_to_page_cache(page, mapping, index,
1241 gfp, NULL);
1242 radix_tree_preload_end();
1243 }
1244 if (error) {
1245 mem_cgroup_uncharge_cache_page(page);
1246 goto decused;
1247 }
54af6042
HD
1248 lru_cache_add_anon(page);
1249
1250 spin_lock(&info->lock);
1da177e4 1251 info->alloced++;
54af6042
HD
1252 inode->i_blocks += BLOCKS_PER_PAGE;
1253 shmem_recalc_inode(inode);
1da177e4 1254 spin_unlock(&info->lock);
1635f6a7 1255 alloced = true;
54af6042 1256
ec9516fb 1257 /*
1635f6a7
HD
1258 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1259 */
1260 if (sgp == SGP_FALLOC)
1261 sgp = SGP_WRITE;
1262clear:
1263 /*
1264 * Let SGP_WRITE caller clear ends if write does not fill page;
1265 * but SGP_FALLOC on a page fallocated earlier must initialize
1266 * it now, lest undo on failure cancel our earlier guarantee.
ec9516fb
HD
1267 */
1268 if (sgp != SGP_WRITE) {
1269 clear_highpage(page);
1270 flush_dcache_page(page);
1271 SetPageUptodate(page);
1272 }
a0ee5ec5 1273 if (sgp == SGP_DIRTY)
27ab7006 1274 set_page_dirty(page);
1da177e4 1275 }
bde05d1c 1276
54af6042 1277 /* Perhaps the file has been truncated since we checked */
1635f6a7 1278 if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
54af6042
HD
1279 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1280 error = -EINVAL;
1635f6a7
HD
1281 if (alloced)
1282 goto trunc;
1283 else
1284 goto failed;
e83c32e8 1285 }
54af6042
HD
1286 *pagep = page;
1287 return 0;
1da177e4 1288
59a16ead 1289 /*
54af6042 1290 * Error recovery.
59a16ead 1291 */
54af6042 1292trunc:
1635f6a7 1293 info = SHMEM_I(inode);
54af6042
HD
1294 ClearPageDirty(page);
1295 delete_from_page_cache(page);
1296 spin_lock(&info->lock);
1297 info->alloced--;
1298 inode->i_blocks -= BLOCKS_PER_PAGE;
59a16ead 1299 spin_unlock(&info->lock);
54af6042 1300decused:
1635f6a7 1301 sbinfo = SHMEM_SB(inode->i_sb);
54af6042
HD
1302 if (sbinfo->max_blocks)
1303 percpu_counter_add(&sbinfo->used_blocks, -1);
1304unacct:
1305 shmem_unacct_blocks(info->flags, 1);
1306failed:
d1899228
HD
1307 if (swap.val && error != -EINVAL &&
1308 !shmem_confirm_swap(mapping, index, swap))
1309 error = -EEXIST;
1310unlock:
27ab7006 1311 if (page) {
54af6042 1312 unlock_page(page);
27ab7006 1313 page_cache_release(page);
54af6042
HD
1314 }
1315 if (error == -ENOSPC && !once++) {
1316 info = SHMEM_I(inode);
1317 spin_lock(&info->lock);
1318 shmem_recalc_inode(inode);
1319 spin_unlock(&info->lock);
27ab7006 1320 goto repeat;
ff36b801 1321 }
d1899228 1322 if (error == -EEXIST) /* from above or from radix_tree_insert */
54af6042
HD
1323 goto repeat;
1324 return error;
1da177e4
LT
1325}
1326
d0217ac0 1327static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1da177e4 1328{
496ad9aa 1329 struct inode *inode = file_inode(vma->vm_file);
1da177e4 1330 int error;
68da9f05 1331 int ret = VM_FAULT_LOCKED;
1da177e4 1332
1ccc3ffa
HD
1333 /*
1334 * Trinity finds that probing a hole which tmpfs is punching can
1335 * prevent the hole-punch from ever completing: which in turn
1336 * locks writers out with its hold on i_mutex. So refrain from
887675c9
HD
1337 * faulting pages into the hole while it's being punched. Although
1338 * shmem_undo_range() does remove the additions, it may be unable to
1339 * keep up, as each new page needs its own unmap_mapping_range() call,
1340 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1341 *
1342 * It does not matter if we sometimes reach this check just before the
1343 * hole-punch begins, so that one fault then races with the punch:
1344 * we just need to make racing faults a rare case.
1345 *
1346 * The implementation below would be much simpler if we just used a
1347 * standard mutex or completion: but we cannot take i_mutex in fault,
1348 * and bloating every shmem inode for this unlikely case would be sad.
1ccc3ffa
HD
1349 */
1350 if (unlikely(inode->i_private)) {
1351 struct shmem_falloc *shmem_falloc;
1352
1353 spin_lock(&inode->i_lock);
1354 shmem_falloc = inode->i_private;
887675c9
HD
1355 if (shmem_falloc &&
1356 shmem_falloc->waitq &&
1357 vmf->pgoff >= shmem_falloc->start &&
1358 vmf->pgoff < shmem_falloc->next) {
1359 wait_queue_head_t *shmem_falloc_waitq;
1360 DEFINE_WAIT(shmem_fault_wait);
1361
1362 ret = VM_FAULT_NOPAGE;
1ccc3ffa
HD
1363 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1364 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
887675c9 1365 /* It's polite to up mmap_sem if we can */
1ccc3ffa 1366 up_read(&vma->vm_mm->mmap_sem);
887675c9 1367 ret = VM_FAULT_RETRY;
1ccc3ffa 1368 }
887675c9
HD
1369
1370 shmem_falloc_waitq = shmem_falloc->waitq;
1371 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
1372 TASK_UNINTERRUPTIBLE);
1373 spin_unlock(&inode->i_lock);
1374 schedule();
1375
1376 /*
1377 * shmem_falloc_waitq points into the shmem_fallocate()
1378 * stack of the hole-punching task: shmem_falloc_waitq
1379 * is usually invalid by the time we reach here, but
1380 * finish_wait() does not dereference it in that case;
1381 * though i_lock needed lest racing with wake_up_all().
1382 */
1383 spin_lock(&inode->i_lock);
1384 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
1385 spin_unlock(&inode->i_lock);
1386 return ret;
1ccc3ffa 1387 }
887675c9 1388 spin_unlock(&inode->i_lock);
1ccc3ffa
HD
1389 }
1390
27d54b39 1391 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
d0217ac0
NP
1392 if (error)
1393 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
68da9f05 1394
456f998e
YH
1395 if (ret & VM_FAULT_MAJOR) {
1396 count_vm_event(PGMAJFAULT);
1397 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1398 }
68da9f05 1399 return ret;
1da177e4
LT
1400}
1401
1da177e4 1402#ifdef CONFIG_NUMA
41ffe5d5 1403static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
1da177e4 1404{
496ad9aa 1405 struct inode *inode = file_inode(vma->vm_file);
41ffe5d5 1406 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
1da177e4
LT
1407}
1408
d8dc74f2
AB
1409static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1410 unsigned long addr)
1da177e4 1411{
496ad9aa 1412 struct inode *inode = file_inode(vma->vm_file);
41ffe5d5 1413 pgoff_t index;
1da177e4 1414
41ffe5d5
HD
1415 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1416 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
1da177e4
LT
1417}
1418#endif
1419
1420int shmem_lock(struct file *file, int lock, struct user_struct *user)
1421{
496ad9aa 1422 struct inode *inode = file_inode(file);
1da177e4
LT
1423 struct shmem_inode_info *info = SHMEM_I(inode);
1424 int retval = -ENOMEM;
1425
1426 spin_lock(&info->lock);
1427 if (lock && !(info->flags & VM_LOCKED)) {
1428 if (!user_shm_lock(inode->i_size, user))
1429 goto out_nomem;
1430 info->flags |= VM_LOCKED;
89e004ea 1431 mapping_set_unevictable(file->f_mapping);
1da177e4
LT
1432 }
1433 if (!lock && (info->flags & VM_LOCKED) && user) {
1434 user_shm_unlock(inode->i_size, user);
1435 info->flags &= ~VM_LOCKED;
89e004ea 1436 mapping_clear_unevictable(file->f_mapping);
1da177e4
LT
1437 }
1438 retval = 0;
89e004ea 1439
1da177e4
LT
1440out_nomem:
1441 spin_unlock(&info->lock);
1442 return retval;
1443}
1444
9b83a6a8 1445static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1da177e4 1446{
e970ac1f
JF
1447 struct inode *inode = file->f_path.dentry->d_inode;
1448 struct shmem_inode_info *info = SHMEM_I(inode);
1449
1450 if (info->seals & F_SEAL_FUTURE_WRITE) {
1451 /*
1452 * New PROT_WRITE and MAP_SHARED mmaps are not allowed when
1453 * "future write" seal active.
1454 */
1455 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
1456 return -EPERM;
1457
1458 /*
1459 * Since the F_SEAL_FUTURE_WRITE seals allow for a MAP_SHARED
1460 * read-only mapping, take care to not allow mprotect to revert
1461 * protections.
1462 */
1463 vma->vm_flags &= ~(VM_MAYWRITE);
1464 }
1465
1da177e4
LT
1466 file_accessed(file);
1467 vma->vm_ops = &shmem_vm_ops;
1468 return 0;
1469}
1470
454abafe 1471static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
09208d15 1472 umode_t mode, dev_t dev, unsigned long flags)
1da177e4
LT
1473{
1474 struct inode *inode;
1475 struct shmem_inode_info *info;
1476 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1477
5b04c689
PE
1478 if (shmem_reserve_inode(sb))
1479 return NULL;
1da177e4
LT
1480
1481 inode = new_inode(sb);
1482 if (inode) {
85fe4025 1483 inode->i_ino = get_next_ino();
454abafe 1484 inode_init_owner(inode, dir, mode);
1da177e4 1485 inode->i_blocks = 0;
1da177e4
LT
1486 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1487 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
91828a40 1488 inode->i_generation = get_seconds();
1da177e4
LT
1489 info = SHMEM_I(inode);
1490 memset(info, 0, (char *)inode - (char *)info);
1491 spin_lock_init(&info->lock);
2194d4de 1492 info->seals = F_SEAL_SEAL;
0b0a0806 1493 info->flags = flags & VM_NORESERVE;
1da177e4 1494 INIT_LIST_HEAD(&info->swaplist);
38f38657 1495 simple_xattrs_init(&info->xattrs);
72c04902 1496 cache_no_acl(inode);
1da177e4
LT
1497
1498 switch (mode & S_IFMT) {
1499 default:
39f0247d 1500 inode->i_op = &shmem_special_inode_operations;
1da177e4
LT
1501 init_special_inode(inode, mode, dev);
1502 break;
1503 case S_IFREG:
14fcc23f 1504 inode->i_mapping->a_ops = &shmem_aops;
1da177e4
LT
1505 inode->i_op = &shmem_inode_operations;
1506 inode->i_fop = &shmem_file_operations;
71fe804b
LS
1507 mpol_shared_policy_init(&info->policy,
1508 shmem_get_sbmpol(sbinfo));
1da177e4
LT
1509 break;
1510 case S_IFDIR:
d8c76e6f 1511 inc_nlink(inode);
1da177e4
LT
1512 /* Some things misbehave if size == 0 on a directory */
1513 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1514 inode->i_op = &shmem_dir_inode_operations;
1515 inode->i_fop = &simple_dir_operations;
1516 break;
1517 case S_IFLNK:
1518 /*
1519 * Must not load anything in the rbtree,
1520 * mpol_free_shared_policy will not be called.
1521 */
71fe804b 1522 mpol_shared_policy_init(&info->policy, NULL);
1da177e4
LT
1523 break;
1524 }
5b04c689
PE
1525 } else
1526 shmem_free_inode(sb);
1da177e4
LT
1527 return inode;
1528}
1529
1530#ifdef CONFIG_TMPFS
92e1d5be 1531static const struct inode_operations shmem_symlink_inode_operations;
69f07ec9 1532static const struct inode_operations shmem_short_symlink_operations;
1da177e4 1533
6d9d88d0
JS
1534#ifdef CONFIG_TMPFS_XATTR
1535static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
1536#else
1537#define shmem_initxattrs NULL
1538#endif
1539
1da177e4 1540static int
800d15a5
NP
1541shmem_write_begin(struct file *file, struct address_space *mapping,
1542 loff_t pos, unsigned len, unsigned flags,
1543 struct page **pagep, void **fsdata)
1da177e4 1544{
800d15a5 1545 struct inode *inode = mapping->host;
2194d4de 1546 struct shmem_inode_info *info = SHMEM_I(inode);
800d15a5 1547 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2194d4de
DH
1548
1549 /* i_mutex is held by caller */
e970ac1f
JF
1550 if (unlikely(info->seals & (F_SEAL_GROW |
1551 F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) {
1552 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))
2194d4de
DH
1553 return -EPERM;
1554 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
1555 return -EPERM;
1556 }
1557
800d15a5
NP
1558 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1559}
1560
1561static int
1562shmem_write_end(struct file *file, struct address_space *mapping,
1563 loff_t pos, unsigned len, unsigned copied,
1564 struct page *page, void *fsdata)
1565{
1566 struct inode *inode = mapping->host;
1567
d3602444
HD
1568 if (pos + copied > inode->i_size)
1569 i_size_write(inode, pos + copied);
1570
ec9516fb
HD
1571 if (!PageUptodate(page)) {
1572 if (copied < PAGE_CACHE_SIZE) {
1573 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1574 zero_user_segments(page, 0, from,
1575 from + copied, PAGE_CACHE_SIZE);
1576 }
1577 SetPageUptodate(page);
1578 }
800d15a5 1579 set_page_dirty(page);
6746aff7 1580 unlock_page(page);
800d15a5
NP
1581 page_cache_release(page);
1582
800d15a5 1583 return copied;
1da177e4
LT
1584}
1585
1da177e4
LT
1586static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1587{
496ad9aa 1588 struct inode *inode = file_inode(filp);
1da177e4 1589 struct address_space *mapping = inode->i_mapping;
41ffe5d5
HD
1590 pgoff_t index;
1591 unsigned long offset;
a0ee5ec5
HD
1592 enum sgp_type sgp = SGP_READ;
1593
1594 /*
1595 * Might this read be for a stacking filesystem? Then when reading
1596 * holes of a sparse file, we actually need to allocate those pages,
1597 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1598 */
1599 if (segment_eq(get_fs(), KERNEL_DS))
1600 sgp = SGP_DIRTY;
1da177e4
LT
1601
1602 index = *ppos >> PAGE_CACHE_SHIFT;
1603 offset = *ppos & ~PAGE_CACHE_MASK;
1604
1605 for (;;) {
1606 struct page *page = NULL;
41ffe5d5
HD
1607 pgoff_t end_index;
1608 unsigned long nr, ret;
1da177e4
LT
1609 loff_t i_size = i_size_read(inode);
1610
1611 end_index = i_size >> PAGE_CACHE_SHIFT;
1612 if (index > end_index)
1613 break;
1614 if (index == end_index) {
1615 nr = i_size & ~PAGE_CACHE_MASK;
1616 if (nr <= offset)
1617 break;
1618 }
1619
a0ee5ec5 1620 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
1da177e4
LT
1621 if (desc->error) {
1622 if (desc->error == -EINVAL)
1623 desc->error = 0;
1624 break;
1625 }
d3602444
HD
1626 if (page)
1627 unlock_page(page);
1da177e4
LT
1628
1629 /*
1630 * We must evaluate after, since reads (unlike writes)
1b1dcc1b 1631 * are called without i_mutex protection against truncate
1da177e4
LT
1632 */
1633 nr = PAGE_CACHE_SIZE;
1634 i_size = i_size_read(inode);
1635 end_index = i_size >> PAGE_CACHE_SHIFT;
1636 if (index == end_index) {
1637 nr = i_size & ~PAGE_CACHE_MASK;
1638 if (nr <= offset) {
1639 if (page)
1640 page_cache_release(page);
1641 break;
1642 }
1643 }
1644 nr -= offset;
1645
1646 if (page) {
1647 /*
1648 * If users can be writing to this page using arbitrary
1649 * virtual addresses, take care about potential aliasing
1650 * before reading the page on the kernel side.
1651 */
1652 if (mapping_writably_mapped(mapping))
1653 flush_dcache_page(page);
1654 /*
1655 * Mark the page accessed if we read the beginning.
1656 */
1657 if (!offset)
1658 mark_page_accessed(page);
b5810039 1659 } else {
1da177e4 1660 page = ZERO_PAGE(0);
b5810039
NP
1661 page_cache_get(page);
1662 }
1da177e4
LT
1663
1664 /*
1665 * Ok, we have the page, and it's up-to-date, so
1666 * now we can copy it to user space...
1667 *
1668 * The actor routine returns how many bytes were actually used..
1669 * NOTE! This may not be the same as how much of a user buffer
1670 * we filled up (we may be padding etc), so we can only update
1671 * "pos" here (the actor routine has to update the user buffer
1672 * pointers and the remaining count).
1673 */
1674 ret = actor(desc, page, offset, nr);
1675 offset += ret;
1676 index += offset >> PAGE_CACHE_SHIFT;
1677 offset &= ~PAGE_CACHE_MASK;
1678
1679 page_cache_release(page);
1680 if (ret != nr || !desc->count)
1681 break;
1682
1683 cond_resched();
1684 }
1685
1686 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1687 file_accessed(filp);
1688}
1689
bcd78e49
HD
1690static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1691 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
1692{
1693 struct file *filp = iocb->ki_filp;
1694 ssize_t retval;
1695 unsigned long seg;
1696 size_t count;
1697 loff_t *ppos = &iocb->ki_pos;
1698
1699 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1700 if (retval)
1701 return retval;
1702
1703 for (seg = 0; seg < nr_segs; seg++) {
1704 read_descriptor_t desc;
1705
1706 desc.written = 0;
1707 desc.arg.buf = iov[seg].iov_base;
1708 desc.count = iov[seg].iov_len;
1709 if (desc.count == 0)
1710 continue;
1711 desc.error = 0;
1712 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1713 retval += desc.written;
1714 if (desc.error) {
1715 retval = retval ?: desc.error;
1716 break;
1717 }
1718 if (desc.count > 0)
1719 break;
1720 }
1721 return retval;
1da177e4
LT
1722}
1723
708e3508
HD
1724static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1725 struct pipe_inode_info *pipe, size_t len,
1726 unsigned int flags)
1727{
1728 struct address_space *mapping = in->f_mapping;
71f0e07a 1729 struct inode *inode = mapping->host;
708e3508
HD
1730 unsigned int loff, nr_pages, req_pages;
1731 struct page *pages[PIPE_DEF_BUFFERS];
1732 struct partial_page partial[PIPE_DEF_BUFFERS];
1733 struct page *page;
1734 pgoff_t index, end_index;
1735 loff_t isize, left;
1736 int error, page_nr;
1737 struct splice_pipe_desc spd = {
1738 .pages = pages,
1739 .partial = partial,
047fe360 1740 .nr_pages_max = PIPE_DEF_BUFFERS,
708e3508
HD
1741 .flags = flags,
1742 .ops = &page_cache_pipe_buf_ops,
1743 .spd_release = spd_release_page,
1744 };
1745
71f0e07a 1746 isize = i_size_read(inode);
708e3508
HD
1747 if (unlikely(*ppos >= isize))
1748 return 0;
1749
1750 left = isize - *ppos;
1751 if (unlikely(left < len))
1752 len = left;
1753
1754 if (splice_grow_spd(pipe, &spd))
1755 return -ENOMEM;
1756
1757 index = *ppos >> PAGE_CACHE_SHIFT;
1758 loff = *ppos & ~PAGE_CACHE_MASK;
1759 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1760 nr_pages = min(req_pages, pipe->buffers);
1761
708e3508
HD
1762 spd.nr_pages = find_get_pages_contig(mapping, index,
1763 nr_pages, spd.pages);
1764 index += spd.nr_pages;
708e3508 1765 error = 0;
708e3508 1766
71f0e07a 1767 while (spd.nr_pages < nr_pages) {
71f0e07a
HD
1768 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1769 if (error)
1770 break;
1771 unlock_page(page);
708e3508
HD
1772 spd.pages[spd.nr_pages++] = page;
1773 index++;
1774 }
1775
708e3508
HD
1776 index = *ppos >> PAGE_CACHE_SHIFT;
1777 nr_pages = spd.nr_pages;
1778 spd.nr_pages = 0;
71f0e07a 1779
708e3508
HD
1780 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1781 unsigned int this_len;
1782
1783 if (!len)
1784 break;
1785
708e3508
HD
1786 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1787 page = spd.pages[page_nr];
1788
71f0e07a 1789 if (!PageUptodate(page) || page->mapping != mapping) {
71f0e07a
HD
1790 error = shmem_getpage(inode, index, &page,
1791 SGP_CACHE, NULL);
1792 if (error)
708e3508 1793 break;
71f0e07a
HD
1794 unlock_page(page);
1795 page_cache_release(spd.pages[page_nr]);
1796 spd.pages[page_nr] = page;
708e3508 1797 }
71f0e07a
HD
1798
1799 isize = i_size_read(inode);
708e3508
HD
1800 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1801 if (unlikely(!isize || index > end_index))
1802 break;
1803
708e3508
HD
1804 if (end_index == index) {
1805 unsigned int plen;
1806
708e3508
HD
1807 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1808 if (plen <= loff)
1809 break;
1810
708e3508
HD
1811 this_len = min(this_len, plen - loff);
1812 len = this_len;
1813 }
1814
1815 spd.partial[page_nr].offset = loff;
1816 spd.partial[page_nr].len = this_len;
1817 len -= this_len;
1818 loff = 0;
1819 spd.nr_pages++;
1820 index++;
1821 }
1822
708e3508
HD
1823 while (page_nr < nr_pages)
1824 page_cache_release(spd.pages[page_nr++]);
708e3508
HD
1825
1826 if (spd.nr_pages)
1827 error = splice_to_pipe(pipe, &spd);
1828
047fe360 1829 splice_shrink_spd(&spd);
708e3508
HD
1830
1831 if (error > 0) {
1832 *ppos += error;
1833 file_accessed(in);
1834 }
1835 return error;
1836}
1837
220f2ac9
HD
1838/*
1839 * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
1840 */
1841static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
965c8e59 1842 pgoff_t index, pgoff_t end, int whence)
220f2ac9
HD
1843{
1844 struct page *page;
1845 struct pagevec pvec;
1846 pgoff_t indices[PAGEVEC_SIZE];
1847 bool done = false;
1848 int i;
1849
1850 pagevec_init(&pvec, 0);
1851 pvec.nr = 1; /* start small: we may be there already */
1852 while (!done) {
1853 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
1854 pvec.nr, pvec.pages, indices);
1855 if (!pvec.nr) {
965c8e59 1856 if (whence == SEEK_DATA)
220f2ac9
HD
1857 index = end;
1858 break;
1859 }
1860 for (i = 0; i < pvec.nr; i++, index++) {
1861 if (index < indices[i]) {
965c8e59 1862 if (whence == SEEK_HOLE) {
220f2ac9
HD
1863 done = true;
1864 break;
1865 }
1866 index = indices[i];
1867 }
1868 page = pvec.pages[i];
1869 if (page && !radix_tree_exceptional_entry(page)) {
1870 if (!PageUptodate(page))
1871 page = NULL;
1872 }
1873 if (index >= end ||
965c8e59
AM
1874 (page && whence == SEEK_DATA) ||
1875 (!page && whence == SEEK_HOLE)) {
220f2ac9
HD
1876 done = true;
1877 break;
1878 }
1879 }
1880 shmem_deswap_pagevec(&pvec);
1881 pagevec_release(&pvec);
1882 pvec.nr = PAGEVEC_SIZE;
1883 cond_resched();
1884 }
1885 return index;
1886}
1887
965c8e59 1888static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
220f2ac9
HD
1889{
1890 struct address_space *mapping = file->f_mapping;
1891 struct inode *inode = mapping->host;
1892 pgoff_t start, end;
1893 loff_t new_offset;
1894
965c8e59
AM
1895 if (whence != SEEK_DATA && whence != SEEK_HOLE)
1896 return generic_file_llseek_size(file, offset, whence,
220f2ac9
HD
1897 MAX_LFS_FILESIZE, i_size_read(inode));
1898 mutex_lock(&inode->i_mutex);
1899 /* We're holding i_mutex so we can access i_size directly */
1900
1901 if (offset < 0)
1902 offset = -EINVAL;
1903 else if (offset >= inode->i_size)
1904 offset = -ENXIO;
1905 else {
1906 start = offset >> PAGE_CACHE_SHIFT;
1907 end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
965c8e59 1908 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
220f2ac9
HD
1909 new_offset <<= PAGE_CACHE_SHIFT;
1910 if (new_offset > offset) {
1911 if (new_offset < inode->i_size)
1912 offset = new_offset;
965c8e59 1913 else if (whence == SEEK_DATA)
220f2ac9
HD
1914 offset = -ENXIO;
1915 else
1916 offset = inode->i_size;
1917 }
1918 }
1919
1920 if (offset >= 0 && offset != file->f_pos) {
1921 file->f_pos = offset;
1922 file->f_version = 0;
1923 }
1924 mutex_unlock(&inode->i_mutex);
1925 return offset;
1926}
1927
32476920
DH
1928/*
1929 * We need a tag: a new tag would expand every radix_tree_node by 8 bytes,
1930 * so reuse a tag which we firmly believe is never set or cleared on shmem.
1931 */
1932#define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE
1933#define LAST_SCAN 4 /* about 150ms max */
1934
1935static void shmem_tag_pins(struct address_space *mapping)
1936{
1937 struct radix_tree_iter iter;
1938 void **slot;
1939 pgoff_t start;
1940 struct page *page;
1941
1942 lru_add_drain();
1943 start = 0;
1944 rcu_read_lock();
1945
1946restart:
1947 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
1948 page = radix_tree_deref_slot(slot);
1949 if (!page || radix_tree_exception(page)) {
1950 if (radix_tree_deref_retry(page))
1951 goto restart;
1952 } else if (page_count(page) - page_mapcount(page) > 1) {
1953 spin_lock_irq(&mapping->tree_lock);
1954 radix_tree_tag_set(&mapping->page_tree, iter.index,
1955 SHMEM_TAG_PINNED);
1956 spin_unlock_irq(&mapping->tree_lock);
1957 }
1958
1959 if (need_resched()) {
def6d5b3
ADR
1960 rcu_read_unlock();
1961 rcu_read_lock();
32476920
DH
1962 start = iter.index + 1;
1963 goto restart;
1964 }
1965 }
1966 rcu_read_unlock();
1967}
1968
1969/*
1970 * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
1971 * via get_user_pages(), drivers might have some pending I/O without any active
1972 * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
1973 * and see whether it has an elevated ref-count. If so, we tag them and wait for
1974 * them to be dropped.
1975 * The caller must guarantee that no new user will acquire writable references
1976 * to those pages to avoid races.
1977 */
2194d4de
DH
1978static int shmem_wait_for_pins(struct address_space *mapping)
1979{
32476920
DH
1980 struct radix_tree_iter iter;
1981 void **slot;
1982 pgoff_t start;
1983 struct page *page;
1984 int error, scan;
1985
1986 shmem_tag_pins(mapping);
1987
1988 error = 0;
1989 for (scan = 0; scan <= LAST_SCAN; scan++) {
1990 if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED))
1991 break;
1992
1993 if (!scan)
1994 lru_add_drain_all();
1995 else if (schedule_timeout_killable((HZ << scan) / 200))
1996 scan = LAST_SCAN;
1997
1998 start = 0;
1999 rcu_read_lock();
2000restart:
2001 radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter,
2002 start, SHMEM_TAG_PINNED) {
2003
2004 page = radix_tree_deref_slot(slot);
2005 if (radix_tree_exception(page)) {
2006 if (radix_tree_deref_retry(page))
2007 goto restart;
2008
2009 page = NULL;
2010 }
2011
2012 if (page &&
2013 page_count(page) - page_mapcount(page) != 1) {
2014 if (scan < LAST_SCAN)
2015 goto continue_resched;
2016
2017 /*
2018 * On the last scan, we clean up all those tags
2019 * we inserted; but make a note that we still
2020 * found pages pinned.
2021 */
2022 error = -EBUSY;
2023 }
2024
2025 spin_lock_irq(&mapping->tree_lock);
2026 radix_tree_tag_clear(&mapping->page_tree,
2027 iter.index, SHMEM_TAG_PINNED);
2028 spin_unlock_irq(&mapping->tree_lock);
2029continue_resched:
2030 if (need_resched()) {
def6d5b3
ADR
2031 rcu_read_unlock();
2032 rcu_read_lock();
32476920
DH
2033 start = iter.index + 1;
2034 goto restart;
2035 }
2036 }
2037 rcu_read_unlock();
2038 }
2039
2040 return error;
2194d4de
DH
2041}
2042
2043#define F_ALL_SEALS (F_SEAL_SEAL | \
2044 F_SEAL_SHRINK | \
2045 F_SEAL_GROW | \
e970ac1f
JF
2046 F_SEAL_WRITE | \
2047 F_SEAL_FUTURE_WRITE)
2194d4de
DH
2048
2049int shmem_add_seals(struct file *file, unsigned int seals)
2050{
2051 struct inode *inode = file_inode(file);
2052 struct shmem_inode_info *info = SHMEM_I(inode);
2053 int error;
2054
2055 /*
2056 * SEALING
2057 * Sealing allows multiple parties to share a shmem-file but restrict
2058 * access to a specific subset of file operations. Seals can only be
2059 * added, but never removed. This way, mutually untrusted parties can
2060 * share common memory regions with a well-defined policy. A malicious
2061 * peer can thus never perform unwanted operations on a shared object.
2062 *
2063 * Seals are only supported on special shmem-files and always affect
2064 * the whole underlying inode. Once a seal is set, it may prevent some
2065 * kinds of access to the file. Currently, the following seals are
2066 * defined:
2067 * SEAL_SEAL: Prevent further seals from being set on this file
2068 * SEAL_SHRINK: Prevent the file from shrinking
2069 * SEAL_GROW: Prevent the file from growing
2070 * SEAL_WRITE: Prevent write access to the file
2071 *
2072 * As we don't require any trust relationship between two parties, we
2073 * must prevent seals from being removed. Therefore, sealing a file
2074 * only adds a given set of seals to the file, it never touches
2075 * existing seals. Furthermore, the "setting seals"-operation can be
2076 * sealed itself, which basically prevents any further seal from being
2077 * added.
2078 *
2079 * Semantics of sealing are only defined on volatile files. Only
2080 * anonymous shmem files support sealing. More importantly, seals are
2081 * never written to disk. Therefore, there's no plan to support it on
2082 * other file types.
2083 */
2084
2085 if (file->f_op != &shmem_file_operations)
2086 return -EINVAL;
2087 if (!(file->f_mode & FMODE_WRITE))
2088 return -EPERM;
2089 if (seals & ~(unsigned int)F_ALL_SEALS)
2090 return -EINVAL;
2091
2092 mutex_lock(&inode->i_mutex);
2093
2094 if (info->seals & F_SEAL_SEAL) {
2095 error = -EPERM;
2096 goto unlock;
2097 }
2098
2099 if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
2100 error = mapping_deny_writable(file->f_mapping);
2101 if (error)
2102 goto unlock;
2103
2104 error = shmem_wait_for_pins(file->f_mapping);
2105 if (error) {
2106 mapping_allow_writable(file->f_mapping);
2107 goto unlock;
2108 }
2109 }
2110
2111 info->seals |= seals;
2112 error = 0;
2113
2114unlock:
2115 mutex_unlock(&inode->i_mutex);
2116 return error;
2117}
2118EXPORT_SYMBOL_GPL(shmem_add_seals);
2119
2120int shmem_get_seals(struct file *file)
2121{
2122 if (file->f_op != &shmem_file_operations)
2123 return -EINVAL;
2124
2125 return SHMEM_I(file_inode(file))->seals;
2126}
2127EXPORT_SYMBOL_GPL(shmem_get_seals);
2128
2129long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
2130{
2131 long error;
2132
2133 switch (cmd) {
2134 case F_ADD_SEALS:
2135 /* disallow upper 32bit */
2136 if (arg > UINT_MAX)
2137 return -EINVAL;
2138
2139 error = shmem_add_seals(file, arg);
2140 break;
2141 case F_GET_SEALS:
2142 error = shmem_get_seals(file);
2143 break;
2144 default:
2145 error = -EINVAL;
2146 break;
2147 }
2148
2149 return error;
2150}
2151
83e4fa9c
HD
2152static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2153 loff_t len)
2154{
496ad9aa 2155 struct inode *inode = file_inode(file);
e2d12e22 2156 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
2194d4de 2157 struct shmem_inode_info *info = SHMEM_I(inode);
1aac1400 2158 struct shmem_falloc shmem_falloc;
e2d12e22
HD
2159 pgoff_t start, index, end;
2160 int error;
83e4fa9c
HD
2161
2162 mutex_lock(&inode->i_mutex);
2163
2164 if (mode & FALLOC_FL_PUNCH_HOLE) {
2165 struct address_space *mapping = file->f_mapping;
2166 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2167 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
887675c9 2168 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
83e4fa9c 2169
2194d4de 2170 /* protected by i_mutex */
e970ac1f 2171 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
2194d4de
DH
2172 error = -EPERM;
2173 goto out;
2174 }
2175
887675c9 2176 shmem_falloc.waitq = &shmem_falloc_waitq;
1ccc3ffa
HD
2177 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2178 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2179 spin_lock(&inode->i_lock);
2180 inode->i_private = &shmem_falloc;
2181 spin_unlock(&inode->i_lock);
2182
83e4fa9c
HD
2183 if ((u64)unmap_end > (u64)unmap_start)
2184 unmap_mapping_range(mapping, unmap_start,
2185 1 + unmap_end - unmap_start, 0);
2186 shmem_truncate_range(inode, offset, offset + len - 1);
2187 /* No need to unmap again: hole-punching leaves COWed pages */
887675c9
HD
2188
2189 spin_lock(&inode->i_lock);
2190 inode->i_private = NULL;
2191 wake_up_all(&shmem_falloc_waitq);
2192 spin_unlock(&inode->i_lock);
83e4fa9c 2193 error = 0;
887675c9 2194 goto out;
e2d12e22
HD
2195 }
2196
2197 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2198 error = inode_newsize_ok(inode, offset + len);
2199 if (error)
2200 goto out;
2201
2194d4de
DH
2202 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2203 error = -EPERM;
2204 goto out;
2205 }
2206
e2d12e22
HD
2207 start = offset >> PAGE_CACHE_SHIFT;
2208 end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2209 /* Try to avoid a swapstorm if len is impossible to satisfy */
2210 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2211 error = -ENOSPC;
2212 goto out;
83e4fa9c
HD
2213 }
2214
887675c9 2215 shmem_falloc.waitq = NULL;
1aac1400
HD
2216 shmem_falloc.start = start;
2217 shmem_falloc.next = start;
2218 shmem_falloc.nr_falloced = 0;
2219 shmem_falloc.nr_unswapped = 0;
2220 spin_lock(&inode->i_lock);
2221 inode->i_private = &shmem_falloc;
2222 spin_unlock(&inode->i_lock);
2223
e2d12e22
HD
2224 for (index = start; index < end; index++) {
2225 struct page *page;
2226
2227 /*
2228 * Good, the fallocate(2) manpage permits EINTR: we may have
2229 * been interrupted because we are using up too much memory.
2230 */
2231 if (signal_pending(current))
2232 error = -EINTR;
1aac1400
HD
2233 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2234 error = -ENOMEM;
e2d12e22 2235 else
1635f6a7 2236 error = shmem_getpage(inode, index, &page, SGP_FALLOC,
e2d12e22
HD
2237 NULL);
2238 if (error) {
1635f6a7 2239 /* Remove the !PageUptodate pages we added */
42a22cd4
HD
2240 if (index > start) {
2241 shmem_undo_range(inode,
2242 (loff_t)start << PAGE_CACHE_SHIFT,
2243 ((loff_t)index << PAGE_CACHE_SHIFT) - 1, true);
2244 }
1aac1400 2245 goto undone;
e2d12e22
HD
2246 }
2247
1aac1400
HD
2248 /*
2249 * Inform shmem_writepage() how far we have reached.
2250 * No need for lock or barrier: we have the page lock.
2251 */
2252 shmem_falloc.next++;
2253 if (!PageUptodate(page))
2254 shmem_falloc.nr_falloced++;
2255
e2d12e22 2256 /*
1635f6a7
HD
2257 * If !PageUptodate, leave it that way so that freeable pages
2258 * can be recognized if we need to rollback on error later.
2259 * But set_page_dirty so that memory pressure will swap rather
e2d12e22
HD
2260 * than free the pages we are allocating (and SGP_CACHE pages
2261 * might still be clean: we now need to mark those dirty too).
2262 */
2263 set_page_dirty(page);
2264 unlock_page(page);
2265 page_cache_release(page);
2266 cond_resched();
2267 }
2268
2269 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2270 i_size_write(inode, offset + len);
e2d12e22 2271 inode->i_ctime = CURRENT_TIME;
1aac1400
HD
2272undone:
2273 spin_lock(&inode->i_lock);
2274 inode->i_private = NULL;
2275 spin_unlock(&inode->i_lock);
e2d12e22 2276out:
83e4fa9c
HD
2277 mutex_unlock(&inode->i_mutex);
2278 return error;
2279}
2280
726c3342 2281static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 2282{
726c3342 2283 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1da177e4
LT
2284
2285 buf->f_type = TMPFS_MAGIC;
2286 buf->f_bsize = PAGE_CACHE_SIZE;
2287 buf->f_namelen = NAME_MAX;
0edd73b3 2288 if (sbinfo->max_blocks) {
1da177e4 2289 buf->f_blocks = sbinfo->max_blocks;
41ffe5d5
HD
2290 buf->f_bavail =
2291 buf->f_bfree = sbinfo->max_blocks -
2292 percpu_counter_sum(&sbinfo->used_blocks);
0edd73b3
HD
2293 }
2294 if (sbinfo->max_inodes) {
1da177e4
LT
2295 buf->f_files = sbinfo->max_inodes;
2296 buf->f_ffree = sbinfo->free_inodes;
1da177e4
LT
2297 }
2298 /* else leave those fields 0 like simple_statfs */
2299 return 0;
2300}
2301
2302/*
2303 * File creation. Allocate an inode, and we're done..
2304 */
2305static int
1a67aafb 2306shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1da177e4 2307{
0b0a0806 2308 struct inode *inode;
1da177e4
LT
2309 int error = -ENOSPC;
2310
454abafe 2311 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
1da177e4 2312 if (inode) {
2a7dba39 2313 error = security_inode_init_security(inode, dir,
9d8f13ba 2314 &dentry->d_name,
6d9d88d0 2315 shmem_initxattrs, NULL);
570bc1c2
SS
2316 if (error) {
2317 if (error != -EOPNOTSUPP) {
2318 iput(inode);
2319 return error;
2320 }
39f0247d 2321 }
1c7c474c
CH
2322#ifdef CONFIG_TMPFS_POSIX_ACL
2323 error = generic_acl_init(inode, dir);
39f0247d
AG
2324 if (error) {
2325 iput(inode);
2326 return error;
570bc1c2 2327 }
718deb6b
AV
2328#else
2329 error = 0;
1c7c474c 2330#endif
1da177e4
LT
2331 dir->i_size += BOGO_DIRENT_SIZE;
2332 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2333 d_instantiate(dentry, inode);
2334 dget(dentry); /* Extra count - pin the dentry in core */
1da177e4
LT
2335 }
2336 return error;
2337}
2338
aa8cc8ee
AV
2339static int
2340shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2341{
2342 struct inode *inode;
2343 int error = -ENOSPC;
2344
2345 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2346 if (inode) {
2347 error = security_inode_init_security(inode, dir,
2348 NULL,
2349 shmem_initxattrs, NULL);
2350 if (error) {
2351 if (error != -EOPNOTSUPP) {
2352 iput(inode);
2353 return error;
2354 }
2355 }
2356#ifdef CONFIG_TMPFS_POSIX_ACL
2357 error = generic_acl_init(inode, dir);
2358 if (error) {
2359 iput(inode);
2360 return error;
2361 }
2362#else
2363 error = 0;
2364#endif
2365 d_tmpfile(dentry, inode);
2366 }
2367 return error;
2368}
2369
18bb1db3 2370static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4
LT
2371{
2372 int error;
2373
2374 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2375 return error;
d8c76e6f 2376 inc_nlink(dir);
1da177e4
LT
2377 return 0;
2378}
2379
4acdaf27 2380static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
ebfc3b49 2381 bool excl)
1da177e4
LT
2382{
2383 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2384}
2385
2386/*
2387 * Link a file..
2388 */
2389static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2390{
2391 struct inode *inode = old_dentry->d_inode;
5b04c689 2392 int ret;
1da177e4
LT
2393
2394 /*
2395 * No ordinary (disk based) filesystem counts links as inodes;
2396 * but each new link needs a new dentry, pinning lowmem, and
2397 * tmpfs dentries cannot be pruned until they are unlinked.
2398 */
5b04c689
PE
2399 ret = shmem_reserve_inode(inode->i_sb);
2400 if (ret)
2401 goto out;
1da177e4
LT
2402
2403 dir->i_size += BOGO_DIRENT_SIZE;
2404 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
d8c76e6f 2405 inc_nlink(inode);
7de9c6ee 2406 ihold(inode); /* New dentry reference */
1da177e4
LT
2407 dget(dentry); /* Extra pinning count for the created dentry */
2408 d_instantiate(dentry, inode);
5b04c689
PE
2409out:
2410 return ret;
1da177e4
LT
2411}
2412
2413static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2414{
2415 struct inode *inode = dentry->d_inode;
2416
5b04c689
PE
2417 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2418 shmem_free_inode(inode->i_sb);
1da177e4
LT
2419
2420 dir->i_size -= BOGO_DIRENT_SIZE;
2421 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
9a53c3a7 2422 drop_nlink(inode);
1da177e4
LT
2423 dput(dentry); /* Undo the count from "create" - this does all the work */
2424 return 0;
2425}
2426
2427static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2428{
2429 if (!simple_empty(dentry))
2430 return -ENOTEMPTY;
2431
9a53c3a7
DH
2432 drop_nlink(dentry->d_inode);
2433 drop_nlink(dir);
1da177e4
LT
2434 return shmem_unlink(dir, dentry);
2435}
2436
2437/*
2438 * The VFS layer already does all the dentry stuff for rename,
2439 * we just have to decrement the usage count for the target if
2440 * it exists so that the VFS layer correctly free's it when it
2441 * gets overwritten.
2442 */
2443static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2444{
2445 struct inode *inode = old_dentry->d_inode;
2446 int they_are_dirs = S_ISDIR(inode->i_mode);
2447
2448 if (!simple_empty(new_dentry))
2449 return -ENOTEMPTY;
2450
2451 if (new_dentry->d_inode) {
2452 (void) shmem_unlink(new_dir, new_dentry);
474740b9
MS
2453 if (they_are_dirs) {
2454 drop_nlink(new_dentry->d_inode);
9a53c3a7 2455 drop_nlink(old_dir);
474740b9 2456 }
1da177e4 2457 } else if (they_are_dirs) {
9a53c3a7 2458 drop_nlink(old_dir);
d8c76e6f 2459 inc_nlink(new_dir);
1da177e4
LT
2460 }
2461
2462 old_dir->i_size -= BOGO_DIRENT_SIZE;
2463 new_dir->i_size += BOGO_DIRENT_SIZE;
2464 old_dir->i_ctime = old_dir->i_mtime =
2465 new_dir->i_ctime = new_dir->i_mtime =
2466 inode->i_ctime = CURRENT_TIME;
2467 return 0;
2468}
2469
2470static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
2471{
2472 int error;
2473 int len;
2474 struct inode *inode;
9276aad6 2475 struct page *page;
1da177e4
LT
2476 char *kaddr;
2477 struct shmem_inode_info *info;
2478
2479 len = strlen(symname) + 1;
2480 if (len > PAGE_CACHE_SIZE)
2481 return -ENAMETOOLONG;
2482
454abafe 2483 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
1da177e4
LT
2484 if (!inode)
2485 return -ENOSPC;
2486
9d8f13ba 2487 error = security_inode_init_security(inode, dir, &dentry->d_name,
6d9d88d0 2488 shmem_initxattrs, NULL);
570bc1c2
SS
2489 if (error) {
2490 if (error != -EOPNOTSUPP) {
2491 iput(inode);
2492 return error;
2493 }
2494 error = 0;
2495 }
2496
1da177e4
LT
2497 info = SHMEM_I(inode);
2498 inode->i_size = len-1;
69f07ec9
HD
2499 if (len <= SHORT_SYMLINK_LEN) {
2500 info->symlink = kmemdup(symname, len, GFP_KERNEL);
2501 if (!info->symlink) {
2502 iput(inode);
2503 return -ENOMEM;
2504 }
2505 inode->i_op = &shmem_short_symlink_operations;
1da177e4
LT
2506 } else {
2507 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
2508 if (error) {
2509 iput(inode);
2510 return error;
2511 }
14fcc23f 2512 inode->i_mapping->a_ops = &shmem_aops;
1da177e4 2513 inode->i_op = &shmem_symlink_inode_operations;
9b04c5fe 2514 kaddr = kmap_atomic(page);
1da177e4 2515 memcpy(kaddr, symname, len);
9b04c5fe 2516 kunmap_atomic(kaddr);
ec9516fb 2517 SetPageUptodate(page);
1da177e4 2518 set_page_dirty(page);
6746aff7 2519 unlock_page(page);
1da177e4
LT
2520 page_cache_release(page);
2521 }
1da177e4
LT
2522 dir->i_size += BOGO_DIRENT_SIZE;
2523 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2524 d_instantiate(dentry, inode);
2525 dget(dentry);
2526 return 0;
2527}
2528
69f07ec9 2529static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
1da177e4 2530{
69f07ec9 2531 nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
cc314eef 2532 return NULL;
1da177e4
LT
2533}
2534
cc314eef 2535static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
2536{
2537 struct page *page = NULL;
41ffe5d5
HD
2538 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
2539 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
d3602444
HD
2540 if (page)
2541 unlock_page(page);
cc314eef 2542 return page;
1da177e4
LT
2543}
2544
cc314eef 2545static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4
LT
2546{
2547 if (!IS_ERR(nd_get_link(nd))) {
cc314eef 2548 struct page *page = cookie;
1da177e4
LT
2549 kunmap(page);
2550 mark_page_accessed(page);
2551 page_cache_release(page);
1da177e4
LT
2552 }
2553}
2554
b09e0fa4 2555#ifdef CONFIG_TMPFS_XATTR
46711810 2556/*
b09e0fa4
EP
2557 * Superblocks without xattr inode operations may get some security.* xattr
2558 * support from the LSM "for free". As soon as we have any other xattrs
39f0247d
AG
2559 * like ACLs, we also need to implement the security.* handlers at
2560 * filesystem level, though.
2561 */
2562
6d9d88d0
JS
2563/*
2564 * Callback for security_inode_init_security() for acquiring xattrs.
2565 */
2566static int shmem_initxattrs(struct inode *inode,
2567 const struct xattr *xattr_array,
2568 void *fs_info)
2569{
2570 struct shmem_inode_info *info = SHMEM_I(inode);
2571 const struct xattr *xattr;
38f38657 2572 struct simple_xattr *new_xattr;
6d9d88d0
JS
2573 size_t len;
2574
2575 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
38f38657 2576 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
6d9d88d0
JS
2577 if (!new_xattr)
2578 return -ENOMEM;
2579
2580 len = strlen(xattr->name) + 1;
2581 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
2582 GFP_KERNEL);
2583 if (!new_xattr->name) {
2584 kfree(new_xattr);
2585 return -ENOMEM;
2586 }
2587
2588 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
2589 XATTR_SECURITY_PREFIX_LEN);
2590 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
2591 xattr->name, len);
2592
38f38657 2593 simple_xattr_list_add(&info->xattrs, new_xattr);
6d9d88d0
JS
2594 }
2595
2596 return 0;
2597}
2598
bb435453 2599static const struct xattr_handler *shmem_xattr_handlers[] = {
b09e0fa4 2600#ifdef CONFIG_TMPFS_POSIX_ACL
1c7c474c
CH
2601 &generic_acl_access_handler,
2602 &generic_acl_default_handler,
b09e0fa4 2603#endif
39f0247d
AG
2604 NULL
2605};
b09e0fa4
EP
2606
2607static int shmem_xattr_validate(const char *name)
2608{
2609 struct { const char *prefix; size_t len; } arr[] = {
2610 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
2611 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
2612 };
2613 int i;
2614
2615 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2616 size_t preflen = arr[i].len;
2617 if (strncmp(name, arr[i].prefix, preflen) == 0) {
2618 if (!name[preflen])
2619 return -EINVAL;
2620 return 0;
2621 }
2622 }
2623 return -EOPNOTSUPP;
2624}
2625
2626static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
2627 void *buffer, size_t size)
2628{
38f38657 2629 struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
b09e0fa4
EP
2630 int err;
2631
2632 /*
2633 * If this is a request for a synthetic attribute in the system.*
2634 * namespace use the generic infrastructure to resolve a handler
2635 * for it via sb->s_xattr.
2636 */
2637 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2638 return generic_getxattr(dentry, name, buffer, size);
2639
2640 err = shmem_xattr_validate(name);
2641 if (err)
2642 return err;
2643
38f38657 2644 return simple_xattr_get(&info->xattrs, name, buffer, size);
b09e0fa4
EP
2645}
2646
2647static int shmem_setxattr(struct dentry *dentry, const char *name,
2648 const void *value, size_t size, int flags)
2649{
38f38657 2650 struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
b09e0fa4
EP
2651 int err;
2652
2653 /*
2654 * If this is a request for a synthetic attribute in the system.*
2655 * namespace use the generic infrastructure to resolve a handler
2656 * for it via sb->s_xattr.
2657 */
2658 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2659 return generic_setxattr(dentry, name, value, size, flags);
2660
2661 err = shmem_xattr_validate(name);
2662 if (err)
2663 return err;
2664
38f38657 2665 return simple_xattr_set(&info->xattrs, name, value, size, flags);
b09e0fa4
EP
2666}
2667
2668static int shmem_removexattr(struct dentry *dentry, const char *name)
2669{
38f38657 2670 struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
b09e0fa4
EP
2671 int err;
2672
2673 /*
2674 * If this is a request for a synthetic attribute in the system.*
2675 * namespace use the generic infrastructure to resolve a handler
2676 * for it via sb->s_xattr.
2677 */
2678 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2679 return generic_removexattr(dentry, name);
2680
2681 err = shmem_xattr_validate(name);
2682 if (err)
2683 return err;
2684
38f38657 2685 return simple_xattr_remove(&info->xattrs, name);
b09e0fa4
EP
2686}
2687
2688static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2689{
38f38657
AR
2690 struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
2691 return simple_xattr_list(&info->xattrs, buffer, size);
b09e0fa4
EP
2692}
2693#endif /* CONFIG_TMPFS_XATTR */
2694
69f07ec9 2695static const struct inode_operations shmem_short_symlink_operations = {
b09e0fa4 2696 .readlink = generic_readlink,
69f07ec9 2697 .follow_link = shmem_follow_short_symlink,
b09e0fa4
EP
2698#ifdef CONFIG_TMPFS_XATTR
2699 .setxattr = shmem_setxattr,
2700 .getxattr = shmem_getxattr,
2701 .listxattr = shmem_listxattr,
2702 .removexattr = shmem_removexattr,
2703#endif
2704};
2705
2706static const struct inode_operations shmem_symlink_inode_operations = {
2707 .readlink = generic_readlink,
2708 .follow_link = shmem_follow_link,
2709 .put_link = shmem_put_link,
2710#ifdef CONFIG_TMPFS_XATTR
2711 .setxattr = shmem_setxattr,
2712 .getxattr = shmem_getxattr,
2713 .listxattr = shmem_listxattr,
2714 .removexattr = shmem_removexattr,
39f0247d 2715#endif
b09e0fa4 2716};
39f0247d 2717
91828a40
DG
2718static struct dentry *shmem_get_parent(struct dentry *child)
2719{
2720 return ERR_PTR(-ESTALE);
2721}
2722
2723static int shmem_match(struct inode *ino, void *vfh)
2724{
2725 __u32 *fh = vfh;
2726 __u64 inum = fh[2];
2727 inum = (inum << 32) | fh[1];
2728 return ino->i_ino == inum && fh[0] == ino->i_generation;
2729}
2730
480b116c
CH
2731static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2732 struct fid *fid, int fh_len, int fh_type)
91828a40 2733{
91828a40 2734 struct inode *inode;
480b116c 2735 struct dentry *dentry = NULL;
35c2a7f4 2736 u64 inum;
480b116c
CH
2737
2738 if (fh_len < 3)
2739 return NULL;
91828a40 2740
35c2a7f4
HD
2741 inum = fid->raw[2];
2742 inum = (inum << 32) | fid->raw[1];
2743
480b116c
CH
2744 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2745 shmem_match, fid->raw);
91828a40 2746 if (inode) {
480b116c 2747 dentry = d_find_alias(inode);
91828a40
DG
2748 iput(inode);
2749 }
2750
480b116c 2751 return dentry;
91828a40
DG
2752}
2753
b0b0382b
AV
2754static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
2755 struct inode *parent)
91828a40 2756{
5fe0c237
AK
2757 if (*len < 3) {
2758 *len = 3;
94e07a75 2759 return FILEID_INVALID;
5fe0c237 2760 }
91828a40 2761
1d3382cb 2762 if (inode_unhashed(inode)) {
91828a40
DG
2763 /* Unfortunately insert_inode_hash is not idempotent,
2764 * so as we hash inodes here rather than at creation
2765 * time, we need a lock to ensure we only try
2766 * to do it once
2767 */
2768 static DEFINE_SPINLOCK(lock);
2769 spin_lock(&lock);
1d3382cb 2770 if (inode_unhashed(inode))
91828a40
DG
2771 __insert_inode_hash(inode,
2772 inode->i_ino + inode->i_generation);
2773 spin_unlock(&lock);
2774 }
2775
2776 fh[0] = inode->i_generation;
2777 fh[1] = inode->i_ino;
2778 fh[2] = ((__u64)inode->i_ino) >> 32;
2779
2780 *len = 3;
2781 return 1;
2782}
2783
39655164 2784static const struct export_operations shmem_export_ops = {
91828a40 2785 .get_parent = shmem_get_parent,
91828a40 2786 .encode_fh = shmem_encode_fh,
480b116c 2787 .fh_to_dentry = shmem_fh_to_dentry,
91828a40
DG
2788};
2789
680d794b 2790static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2791 bool remount)
1da177e4
LT
2792{
2793 char *this_char, *value, *rest;
49cd0a5c 2794 struct mempolicy *mpol = NULL;
8751e039
EB
2795 uid_t uid;
2796 gid_t gid;
1da177e4 2797
b00dc3ad
HD
2798 while (options != NULL) {
2799 this_char = options;
2800 for (;;) {
2801 /*
2802 * NUL-terminate this option: unfortunately,
2803 * mount options form a comma-separated list,
2804 * but mpol's nodelist may also contain commas.
2805 */
2806 options = strchr(options, ',');
2807 if (options == NULL)
2808 break;
2809 options++;
2810 if (!isdigit(*options)) {
2811 options[-1] = '\0';
2812 break;
2813 }
2814 }
1da177e4
LT
2815 if (!*this_char)
2816 continue;
2817 if ((value = strchr(this_char,'=')) != NULL) {
2818 *value++ = 0;
2819 } else {
2820 printk(KERN_ERR
2821 "tmpfs: No value for mount option '%s'\n",
2822 this_char);
49cd0a5c 2823 goto error;
1da177e4
LT
2824 }
2825
2826 if (!strcmp(this_char,"size")) {
2827 unsigned long long size;
2828 size = memparse(value,&rest);
2829 if (*rest == '%') {
2830 size <<= PAGE_SHIFT;
2831 size *= totalram_pages;
2832 do_div(size, 100);
2833 rest++;
2834 }
2835 if (*rest)
2836 goto bad_val;
680d794b 2837 sbinfo->max_blocks =
2838 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
1da177e4 2839 } else if (!strcmp(this_char,"nr_blocks")) {
680d794b 2840 sbinfo->max_blocks = memparse(value, &rest);
1da177e4
LT
2841 if (*rest)
2842 goto bad_val;
2843 } else if (!strcmp(this_char,"nr_inodes")) {
680d794b 2844 sbinfo->max_inodes = memparse(value, &rest);
1da177e4
LT
2845 if (*rest)
2846 goto bad_val;
2847 } else if (!strcmp(this_char,"mode")) {
680d794b 2848 if (remount)
1da177e4 2849 continue;
680d794b 2850 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
1da177e4
LT
2851 if (*rest)
2852 goto bad_val;
2853 } else if (!strcmp(this_char,"uid")) {
680d794b 2854 if (remount)
1da177e4 2855 continue;
8751e039 2856 uid = simple_strtoul(value, &rest, 0);
1da177e4
LT
2857 if (*rest)
2858 goto bad_val;
8751e039
EB
2859 sbinfo->uid = make_kuid(current_user_ns(), uid);
2860 if (!uid_valid(sbinfo->uid))
2861 goto bad_val;
1da177e4 2862 } else if (!strcmp(this_char,"gid")) {
680d794b 2863 if (remount)
1da177e4 2864 continue;
8751e039 2865 gid = simple_strtoul(value, &rest, 0);
1da177e4
LT
2866 if (*rest)
2867 goto bad_val;
8751e039
EB
2868 sbinfo->gid = make_kgid(current_user_ns(), gid);
2869 if (!gid_valid(sbinfo->gid))
2870 goto bad_val;
7339ff83 2871 } else if (!strcmp(this_char,"mpol")) {
49cd0a5c
GT
2872 mpol_put(mpol);
2873 mpol = NULL;
2874 if (mpol_parse_str(value, &mpol))
7339ff83 2875 goto bad_val;
1da177e4
LT
2876 } else {
2877 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2878 this_char);
49cd0a5c 2879 goto error;
1da177e4
LT
2880 }
2881 }
49cd0a5c 2882 sbinfo->mpol = mpol;
1da177e4
LT
2883 return 0;
2884
2885bad_val:
2886 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2887 value, this_char);
49cd0a5c
GT
2888error:
2889 mpol_put(mpol);
1da177e4
LT
2890 return 1;
2891
2892}
2893
2894static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2895{
2896 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
680d794b 2897 struct shmem_sb_info config = *sbinfo;
0edd73b3
HD
2898 unsigned long inodes;
2899 int error = -EINVAL;
2900
5f00110f 2901 config.mpol = NULL;
680d794b 2902 if (shmem_parse_options(data, &config, true))
0edd73b3 2903 return error;
1da177e4 2904
0edd73b3 2905 spin_lock(&sbinfo->stat_lock);
0edd73b3 2906 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
7e496299 2907 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
0edd73b3 2908 goto out;
680d794b 2909 if (config.max_inodes < inodes)
0edd73b3
HD
2910 goto out;
2911 /*
54af6042 2912 * Those tests disallow limited->unlimited while any are in use;
0edd73b3
HD
2913 * but we must separately disallow unlimited->limited, because
2914 * in that case we have no record of how much is already in use.
2915 */
680d794b 2916 if (config.max_blocks && !sbinfo->max_blocks)
0edd73b3 2917 goto out;
680d794b 2918 if (config.max_inodes && !sbinfo->max_inodes)
0edd73b3
HD
2919 goto out;
2920
2921 error = 0;
680d794b 2922 sbinfo->max_blocks = config.max_blocks;
680d794b 2923 sbinfo->max_inodes = config.max_inodes;
2924 sbinfo->free_inodes = config.max_inodes - inodes;
71fe804b 2925
5f00110f
GT
2926 /*
2927 * Preserve previous mempolicy unless mpol remount option was specified.
2928 */
2929 if (config.mpol) {
2930 mpol_put(sbinfo->mpol);
2931 sbinfo->mpol = config.mpol; /* transfers initial ref */
2932 }
0edd73b3
HD
2933out:
2934 spin_unlock(&sbinfo->stat_lock);
2935 return error;
1da177e4 2936}
680d794b 2937
34c80b1d 2938static int shmem_show_options(struct seq_file *seq, struct dentry *root)
680d794b 2939{
34c80b1d 2940 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
680d794b 2941
2942 if (sbinfo->max_blocks != shmem_default_max_blocks())
2943 seq_printf(seq, ",size=%luk",
2944 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2945 if (sbinfo->max_inodes != shmem_default_max_inodes())
2946 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2947 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
09208d15 2948 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
8751e039
EB
2949 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
2950 seq_printf(seq, ",uid=%u",
2951 from_kuid_munged(&init_user_ns, sbinfo->uid));
2952 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
2953 seq_printf(seq, ",gid=%u",
2954 from_kgid_munged(&init_user_ns, sbinfo->gid));
71fe804b 2955 shmem_show_mpol(seq, sbinfo->mpol);
680d794b 2956 return 0;
2957}
8a09920f
DH
2958
2959#define MFD_NAME_PREFIX "memfd:"
2960#define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
2961#define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
2962
2963#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
2964
2965SYSCALL_DEFINE2(memfd_create,
2966 const char __user *, uname,
2967 unsigned int, flags)
2968{
2969 struct shmem_inode_info *info;
2970 struct file *file;
2971 int fd, error;
2972 char *name;
2973 long len;
2974
2975 if (flags & ~(unsigned int)MFD_ALL_FLAGS)
2976 return -EINVAL;
2977
2978 /* length includes terminating zero */
2979 len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
2980 if (len <= 0)
2981 return -EFAULT;
2982 if (len > MFD_NAME_MAX_LEN + 1)
2983 return -EINVAL;
2984
2985 name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
2986 if (!name)
2987 return -ENOMEM;
2988
2989 strcpy(name, MFD_NAME_PREFIX);
2990 if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
2991 error = -EFAULT;
2992 goto err_name;
2993 }
2994
2995 /* terminating-zero may have changed after strnlen_user() returned */
2996 if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
2997 error = -EFAULT;
2998 goto err_name;
2999 }
3000
3001 fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
3002 if (fd < 0) {
3003 error = fd;
3004 goto err_name;
3005 }
3006
3007 file = shmem_file_setup(name, 0, VM_NORESERVE);
3008 if (IS_ERR(file)) {
3009 error = PTR_ERR(file);
3010 goto err_fd;
3011 }
3012 info = SHMEM_I(file->f_path.dentry->d_inode);
3013 file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
3014 file->f_flags |= O_RDWR | O_LARGEFILE;
3015
3016 fd_install(fd, file);
3017 kfree(name);
3018 return fd;
3019
3020err_fd:
3021 put_unused_fd(fd);
3022err_name:
3023 kfree(name);
3024 return error;
3025}
3026
680d794b 3027#endif /* CONFIG_TMPFS */
1da177e4
LT
3028
3029static void shmem_put_super(struct super_block *sb)
3030{
602586a8
HD
3031 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3032
3033 percpu_counter_destroy(&sbinfo->used_blocks);
49cd0a5c 3034 mpol_put(sbinfo->mpol);
602586a8 3035 kfree(sbinfo);
1da177e4
LT
3036 sb->s_fs_info = NULL;
3037}
3038
2b2af54a 3039int shmem_fill_super(struct super_block *sb, void *data, int silent)
1da177e4
LT
3040{
3041 struct inode *inode;
0edd73b3 3042 struct shmem_sb_info *sbinfo;
680d794b 3043 int err = -ENOMEM;
3044
3045 /* Round up to L1_CACHE_BYTES to resist false sharing */
425fbf04 3046 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
680d794b 3047 L1_CACHE_BYTES), GFP_KERNEL);
3048 if (!sbinfo)
3049 return -ENOMEM;
3050
680d794b 3051 sbinfo->mode = S_IRWXUGO | S_ISVTX;
76aac0e9
DH
3052 sbinfo->uid = current_fsuid();
3053 sbinfo->gid = current_fsgid();
680d794b 3054 sb->s_fs_info = sbinfo;
1da177e4 3055
0edd73b3 3056#ifdef CONFIG_TMPFS
1da177e4
LT
3057 /*
3058 * Per default we only allow half of the physical ram per
3059 * tmpfs instance, limiting inodes to one per page of lowmem;
3060 * but the internal instance is left unlimited.
3061 */
3062 if (!(sb->s_flags & MS_NOUSER)) {
680d794b 3063 sbinfo->max_blocks = shmem_default_max_blocks();
3064 sbinfo->max_inodes = shmem_default_max_inodes();
3065 if (shmem_parse_options(data, sbinfo, false)) {
3066 err = -EINVAL;
3067 goto failed;
3068 }
1da177e4 3069 }
91828a40 3070 sb->s_export_op = &shmem_export_ops;
2f6e38f3 3071 sb->s_flags |= MS_NOSEC;
1da177e4
LT
3072#else
3073 sb->s_flags |= MS_NOUSER;
3074#endif
3075
0edd73b3 3076 spin_lock_init(&sbinfo->stat_lock);
602586a8
HD
3077 if (percpu_counter_init(&sbinfo->used_blocks, 0))
3078 goto failed;
680d794b 3079 sbinfo->free_inodes = sbinfo->max_inodes;
0edd73b3 3080
285b2c4f 3081 sb->s_maxbytes = MAX_LFS_FILESIZE;
1da177e4
LT
3082 sb->s_blocksize = PAGE_CACHE_SIZE;
3083 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
3084 sb->s_magic = TMPFS_MAGIC;
3085 sb->s_op = &shmem_ops;
cfd95a9c 3086 sb->s_time_gran = 1;
b09e0fa4 3087#ifdef CONFIG_TMPFS_XATTR
39f0247d 3088 sb->s_xattr = shmem_xattr_handlers;
b09e0fa4
EP
3089#endif
3090#ifdef CONFIG_TMPFS_POSIX_ACL
39f0247d
AG
3091 sb->s_flags |= MS_POSIXACL;
3092#endif
0edd73b3 3093
454abafe 3094 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
1da177e4
LT
3095 if (!inode)
3096 goto failed;
680d794b 3097 inode->i_uid = sbinfo->uid;
3098 inode->i_gid = sbinfo->gid;
318ceed0
AV
3099 sb->s_root = d_make_root(inode);
3100 if (!sb->s_root)
48fde701 3101 goto failed;
1da177e4
LT
3102 return 0;
3103
1da177e4
LT
3104failed:
3105 shmem_put_super(sb);
3106 return err;
3107}
3108
fcc234f8 3109static struct kmem_cache *shmem_inode_cachep;
1da177e4
LT
3110
3111static struct inode *shmem_alloc_inode(struct super_block *sb)
3112{
41ffe5d5
HD
3113 struct shmem_inode_info *info;
3114 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3115 if (!info)
1da177e4 3116 return NULL;
41ffe5d5 3117 return &info->vfs_inode;
1da177e4
LT
3118}
3119
41ffe5d5 3120static void shmem_destroy_callback(struct rcu_head *head)
fa0d7e3d
NP
3121{
3122 struct inode *inode = container_of(head, struct inode, i_rcu);
fa0d7e3d
NP
3123 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3124}
3125
1da177e4
LT
3126static void shmem_destroy_inode(struct inode *inode)
3127{
09208d15 3128 if (S_ISREG(inode->i_mode))
1da177e4 3129 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
41ffe5d5 3130 call_rcu(&inode->i_rcu, shmem_destroy_callback);
1da177e4
LT
3131}
3132
41ffe5d5 3133static void shmem_init_inode(void *foo)
1da177e4 3134{
41ffe5d5
HD
3135 struct shmem_inode_info *info = foo;
3136 inode_init_once(&info->vfs_inode);
1da177e4
LT
3137}
3138
41ffe5d5 3139static int shmem_init_inodecache(void)
1da177e4
LT
3140{
3141 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3142 sizeof(struct shmem_inode_info),
41ffe5d5 3143 0, SLAB_PANIC, shmem_init_inode);
1da177e4
LT
3144 return 0;
3145}
3146
41ffe5d5 3147static void shmem_destroy_inodecache(void)
1da177e4 3148{
1a1d92c1 3149 kmem_cache_destroy(shmem_inode_cachep);
1da177e4
LT
3150}
3151
f5e54d6e 3152static const struct address_space_operations shmem_aops = {
1da177e4 3153 .writepage = shmem_writepage,
76719325 3154 .set_page_dirty = __set_page_dirty_no_writeback,
1da177e4 3155#ifdef CONFIG_TMPFS
800d15a5
NP
3156 .write_begin = shmem_write_begin,
3157 .write_end = shmem_write_end,
1da177e4 3158#endif
304dbdb7 3159 .migratepage = migrate_page,
aa261f54 3160 .error_remove_page = generic_error_remove_page,
1da177e4
LT
3161};
3162
15ad7cdc 3163static const struct file_operations shmem_file_operations = {
1da177e4
LT
3164 .mmap = shmem_mmap,
3165#ifdef CONFIG_TMPFS
220f2ac9 3166 .llseek = shmem_file_llseek,
bcd78e49 3167 .read = do_sync_read,
5402b976 3168 .write = do_sync_write,
bcd78e49 3169 .aio_read = shmem_file_aio_read,
5402b976 3170 .aio_write = generic_file_aio_write,
1b061d92 3171 .fsync = noop_fsync,
708e3508 3172 .splice_read = shmem_file_splice_read,
ae976416 3173 .splice_write = generic_file_splice_write,
83e4fa9c 3174 .fallocate = shmem_fallocate,
1da177e4
LT
3175#endif
3176};
3177
92e1d5be 3178static const struct inode_operations shmem_inode_operations = {
94c1e62d 3179 .setattr = shmem_setattr,
b09e0fa4
EP
3180#ifdef CONFIG_TMPFS_XATTR
3181 .setxattr = shmem_setxattr,
3182 .getxattr = shmem_getxattr,
3183 .listxattr = shmem_listxattr,
3184 .removexattr = shmem_removexattr,
3185#endif
1da177e4
LT
3186};
3187
92e1d5be 3188static const struct inode_operations shmem_dir_inode_operations = {
1da177e4
LT
3189#ifdef CONFIG_TMPFS
3190 .create = shmem_create,
3191 .lookup = simple_lookup,
3192 .link = shmem_link,
3193 .unlink = shmem_unlink,
3194 .symlink = shmem_symlink,
3195 .mkdir = shmem_mkdir,
3196 .rmdir = shmem_rmdir,
3197 .mknod = shmem_mknod,
3198 .rename = shmem_rename,
aa8cc8ee 3199 .tmpfile = shmem_tmpfile,
1da177e4 3200#endif
b09e0fa4
EP
3201#ifdef CONFIG_TMPFS_XATTR
3202 .setxattr = shmem_setxattr,
3203 .getxattr = shmem_getxattr,
3204 .listxattr = shmem_listxattr,
3205 .removexattr = shmem_removexattr,
3206#endif
39f0247d 3207#ifdef CONFIG_TMPFS_POSIX_ACL
94c1e62d 3208 .setattr = shmem_setattr,
39f0247d
AG
3209#endif
3210};
3211
92e1d5be 3212static const struct inode_operations shmem_special_inode_operations = {
b09e0fa4
EP
3213#ifdef CONFIG_TMPFS_XATTR
3214 .setxattr = shmem_setxattr,
3215 .getxattr = shmem_getxattr,
3216 .listxattr = shmem_listxattr,
3217 .removexattr = shmem_removexattr,
3218#endif
39f0247d 3219#ifdef CONFIG_TMPFS_POSIX_ACL
94c1e62d 3220 .setattr = shmem_setattr,
39f0247d 3221#endif
1da177e4
LT
3222};
3223
759b9775 3224static const struct super_operations shmem_ops = {
1da177e4
LT
3225 .alloc_inode = shmem_alloc_inode,
3226 .destroy_inode = shmem_destroy_inode,
3227#ifdef CONFIG_TMPFS
3228 .statfs = shmem_statfs,
3229 .remount_fs = shmem_remount_fs,
680d794b 3230 .show_options = shmem_show_options,
1da177e4 3231#endif
1f895f75 3232 .evict_inode = shmem_evict_inode,
1da177e4
LT
3233 .drop_inode = generic_delete_inode,
3234 .put_super = shmem_put_super,
3235};
3236
f0f37e2f 3237static const struct vm_operations_struct shmem_vm_ops = {
54cb8821 3238 .fault = shmem_fault,
1da177e4
LT
3239#ifdef CONFIG_NUMA
3240 .set_policy = shmem_set_policy,
3241 .get_policy = shmem_get_policy,
3242#endif
0b173bc4 3243 .remap_pages = generic_file_remap_pages,
1da177e4
LT
3244};
3245
3c26ff6e
AV
3246static struct dentry *shmem_mount(struct file_system_type *fs_type,
3247 int flags, const char *dev_name, void *data)
1da177e4 3248{
3c26ff6e 3249 return mount_nodev(fs_type, flags, data, shmem_fill_super);
1da177e4
LT
3250}
3251
41ffe5d5 3252static struct file_system_type shmem_fs_type = {
1da177e4
LT
3253 .owner = THIS_MODULE,
3254 .name = "tmpfs",
3c26ff6e 3255 .mount = shmem_mount,
1da177e4 3256 .kill_sb = kill_litter_super,
2b8576cb 3257 .fs_flags = FS_USERNS_MOUNT,
1da177e4 3258};
1da177e4 3259
41ffe5d5 3260int __init shmem_init(void)
1da177e4
LT
3261{
3262 int error;
3263
e0bf68dd
PZ
3264 error = bdi_init(&shmem_backing_dev_info);
3265 if (error)
3266 goto out4;
3267
41ffe5d5 3268 error = shmem_init_inodecache();
1da177e4
LT
3269 if (error)
3270 goto out3;
3271
41ffe5d5 3272 error = register_filesystem(&shmem_fs_type);
1da177e4
LT
3273 if (error) {
3274 printk(KERN_ERR "Could not register tmpfs\n");
3275 goto out2;
3276 }
95dc112a 3277
41ffe5d5
HD
3278 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
3279 shmem_fs_type.name, NULL);
1da177e4
LT
3280 if (IS_ERR(shm_mnt)) {
3281 error = PTR_ERR(shm_mnt);
3282 printk(KERN_ERR "Could not kern_mount tmpfs\n");
3283 goto out1;
3284 }
3285 return 0;
3286
3287out1:
41ffe5d5 3288 unregister_filesystem(&shmem_fs_type);
1da177e4 3289out2:
41ffe5d5 3290 shmem_destroy_inodecache();
1da177e4 3291out3:
e0bf68dd
PZ
3292 bdi_destroy(&shmem_backing_dev_info);
3293out4:
1da177e4
LT
3294 shm_mnt = ERR_PTR(error);
3295 return error;
3296}
853ac43a
MM
3297
3298#else /* !CONFIG_SHMEM */
3299
3300/*
3301 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3302 *
3303 * This is intended for small system where the benefits of the full
3304 * shmem code (swap-backed and resource-limited) are outweighed by
3305 * their complexity. On systems without swap this code should be
3306 * effectively equivalent, but much lighter weight.
3307 */
3308
41ffe5d5 3309static struct file_system_type shmem_fs_type = {
853ac43a 3310 .name = "tmpfs",
3c26ff6e 3311 .mount = ramfs_mount,
853ac43a 3312 .kill_sb = kill_litter_super,
2b8576cb 3313 .fs_flags = FS_USERNS_MOUNT,
853ac43a
MM
3314};
3315
41ffe5d5 3316int __init shmem_init(void)
853ac43a 3317{
41ffe5d5 3318 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
853ac43a 3319
41ffe5d5 3320 shm_mnt = kern_mount(&shmem_fs_type);
853ac43a
MM
3321 BUG_ON(IS_ERR(shm_mnt));
3322
3323 return 0;
3324}
3325
41ffe5d5 3326int shmem_unuse(swp_entry_t swap, struct page *page)
853ac43a
MM
3327{
3328 return 0;
3329}
3330
3f96b79a
HD
3331int shmem_lock(struct file *file, int lock, struct user_struct *user)
3332{
3333 return 0;
3334}
3335
24513264
HD
3336void shmem_unlock_mapping(struct address_space *mapping)
3337{
3338}
3339
41ffe5d5 3340void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
94c1e62d 3341{
41ffe5d5 3342 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
94c1e62d
HD
3343}
3344EXPORT_SYMBOL_GPL(shmem_truncate_range);
3345
0b0a0806
HD
3346#define shmem_vm_ops generic_file_vm_ops
3347#define shmem_file_operations ramfs_file_operations
454abafe 3348#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
0b0a0806
HD
3349#define shmem_acct_size(flags, size) 0
3350#define shmem_unacct_size(flags, size) do {} while (0)
853ac43a
MM
3351
3352#endif /* CONFIG_SHMEM */
3353
3354/* common code */
1da177e4 3355
3451538a 3356static struct dentry_operations anon_ops = {
ad4c3cc4 3357 .d_dname = simple_dname
3451538a
AV
3358};
3359
46711810 3360/**
1da177e4 3361 * shmem_file_setup - get an unlinked file living in tmpfs
1da177e4
LT
3362 * @name: name for dentry (to be seen in /proc/<pid>/maps
3363 * @size: size to be set for the file
0b0a0806 3364 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
1da177e4 3365 */
168f5ac6 3366struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
1da177e4 3367{
6b4d0b27 3368 struct file *res;
1da177e4 3369 struct inode *inode;
2c48b9c4 3370 struct path path;
3451538a 3371 struct super_block *sb;
1da177e4
LT
3372 struct qstr this;
3373
3374 if (IS_ERR(shm_mnt))
6b4d0b27 3375 return ERR_CAST(shm_mnt);
1da177e4 3376
285b2c4f 3377 if (size < 0 || size > MAX_LFS_FILESIZE)
1da177e4
LT
3378 return ERR_PTR(-EINVAL);
3379
3380 if (shmem_acct_size(flags, size))
3381 return ERR_PTR(-ENOMEM);
3382
6b4d0b27 3383 res = ERR_PTR(-ENOMEM);
1da177e4
LT
3384 this.name = name;
3385 this.len = strlen(name);
3386 this.hash = 0; /* will go */
3451538a
AV
3387 sb = shm_mnt->mnt_sb;
3388 path.dentry = d_alloc_pseudo(sb, &this);
2c48b9c4 3389 if (!path.dentry)
1da177e4 3390 goto put_memory;
3451538a 3391 d_set_d_op(path.dentry, &anon_ops);
2c48b9c4 3392 path.mnt = mntget(shm_mnt);
1da177e4 3393
6b4d0b27 3394 res = ERR_PTR(-ENOSPC);
3451538a 3395 inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
1da177e4 3396 if (!inode)
4b42af81 3397 goto put_dentry;
1da177e4 3398
2c48b9c4 3399 d_instantiate(path.dentry, inode);
1da177e4 3400 inode->i_size = size;
6d6b77f1 3401 clear_nlink(inode); /* It is unlinked */
26567cdb
AV
3402 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
3403 if (IS_ERR(res))
4b42af81 3404 goto put_dentry;
4b42af81 3405
6b4d0b27 3406 res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
4b42af81 3407 &shmem_file_operations);
6b4d0b27 3408 if (IS_ERR(res))
4b42af81
AV
3409 goto put_dentry;
3410
6b4d0b27 3411 return res;
1da177e4 3412
1da177e4 3413put_dentry:
2c48b9c4 3414 path_put(&path);
1da177e4
LT
3415put_memory:
3416 shmem_unacct_size(flags, size);
6b4d0b27 3417 return res;
1da177e4 3418}
395e0ddc 3419EXPORT_SYMBOL_GPL(shmem_file_setup);
1da177e4 3420
3c2a0909
S
3421void shmem_set_file(struct vm_area_struct *vma, struct file *file)
3422{
3423 if (vma->vm_file)
3424 fput(vma->vm_file);
3425 vma->vm_file = file;
3426 vma->vm_ops = &shmem_vm_ops;
3427}
3428
46711810 3429/**
1da177e4 3430 * shmem_zero_setup - setup a shared anonymous mapping
1da177e4
LT
3431 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
3432 */
3433int shmem_zero_setup(struct vm_area_struct *vma)
3434{
3435 struct file *file;
3436 loff_t size = vma->vm_end - vma->vm_start;
3437
3438 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
3439 if (IS_ERR(file))
3440 return PTR_ERR(file);
3441
3c2a0909 3442 shmem_set_file(vma, file);
1da177e4
LT
3443 return 0;
3444}
d9d90e5e
HD
3445
3446/**
3447 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3448 * @mapping: the page's address_space
3449 * @index: the page index
3450 * @gfp: the page allocator flags to use if allocating
3451 *
3452 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3453 * with any new page allocations done using the specified allocation flags.
3454 * But read_cache_page_gfp() uses the ->readpage() method: which does not
3455 * suit tmpfs, since it may have pages in swapcache, and needs to find those
3456 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
3457 *
68da9f05
HD
3458 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
3459 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
d9d90e5e
HD
3460 */
3461struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
3462 pgoff_t index, gfp_t gfp)
3463{
68da9f05
HD
3464#ifdef CONFIG_SHMEM
3465 struct inode *inode = mapping->host;
9276aad6 3466 struct page *page;
68da9f05
HD
3467 int error;
3468
3469 BUG_ON(mapping->a_ops != &shmem_aops);
3470 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
3471 if (error)
3472 page = ERR_PTR(error);
3473 else
3474 unlock_page(page);
3475 return page;
3476#else
3477 /*
3478 * The tiny !SHMEM case uses ramfs without swap
3479 */
d9d90e5e 3480 return read_cache_page_gfp(mapping, index, gfp);
68da9f05 3481#endif
d9d90e5e
HD
3482}
3483EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);