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