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