swapoff: scan ptes preemptibly
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / mm / shmem.c
CommitLineData
1da177e4
LT
1/*
2 * Resizable virtual memory filesystem for Linux.
3 *
4 * Copyright (C) 2000 Linus Torvalds.
5 * 2000 Transmeta Corp.
6 * 2000-2001 Christoph Rohland
7 * 2000-2001 SAP AG
8 * 2002 Red Hat Inc.
0edd73b3
HD
9 * Copyright (C) 2002-2005 Hugh Dickins.
10 * Copyright (C) 2002-2005 VERITAS Software Corporation.
1da177e4
LT
11 * Copyright (C) 2004 Andi Kleen, SuSE Labs
12 *
13 * Extended attribute support for tmpfs:
14 * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
15 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
16 *
17 * This file is released under the GPL.
18 */
19
20/*
21 * This virtual memory filesystem is heavily based on the ramfs. It
22 * extends ramfs by the ability to use swap and honor resource limits
23 * which makes it a completely usable filesystem.
24 */
25
1da177e4
LT
26#include <linux/module.h>
27#include <linux/init.h>
1da177e4 28#include <linux/fs.h>
39f0247d 29#include <linux/xattr.h>
a5694255 30#include <linux/exportfs.h>
39f0247d 31#include <linux/generic_acl.h>
1da177e4
LT
32#include <linux/mm.h>
33#include <linux/mman.h>
34#include <linux/file.h>
35#include <linux/swap.h>
36#include <linux/pagemap.h>
37#include <linux/string.h>
38#include <linux/slab.h>
39#include <linux/backing-dev.h>
40#include <linux/shmem_fs.h>
41#include <linux/mount.h>
42#include <linux/writeback.h>
43#include <linux/vfs.h>
44#include <linux/blkdev.h>
45#include <linux/security.h>
46#include <linux/swapops.h>
47#include <linux/mempolicy.h>
48#include <linux/namei.h>
b00dc3ad 49#include <linux/ctype.h>
304dbdb7 50#include <linux/migrate.h>
c1f60a5a 51#include <linux/highmem.h>
304dbdb7 52
1da177e4
LT
53#include <asm/uaccess.h>
54#include <asm/div64.h>
55#include <asm/pgtable.h>
56
57/* This magic number is used in glibc for posix shared memory */
58#define TMPFS_MAGIC 0x01021994
59
60#define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
61#define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
62#define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
63
64#define SHMEM_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
65#define SHMEM_MAX_BYTES ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
66
67#define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
68
69/* info->flags needs VM_flags to handle pagein/truncate races efficiently */
70#define SHMEM_PAGEIN VM_READ
71#define SHMEM_TRUNCATE VM_WRITE
72
73/* Definition to limit shmem_truncate's steps between cond_rescheds */
74#define LATENCY_LIMIT 64
75
76/* Pretend that each entry is of this size in directory's i_size */
77#define BOGO_DIRENT_SIZE 20
78
1da177e4
LT
79/* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
80enum sgp_type {
1da177e4
LT
81 SGP_READ, /* don't exceed i_size, don't allocate page */
82 SGP_CACHE, /* don't exceed i_size, may allocate page */
83 SGP_WRITE, /* may exceed i_size, may allocate page */
84};
85
86static int shmem_getpage(struct inode *inode, unsigned long idx,
87 struct page **pagep, enum sgp_type sgp, int *type);
88
6daa0e28 89static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
1da177e4
LT
90{
91 /*
92 * The above definition of ENTRIES_PER_PAGE, and the use of
93 * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
94 * might be reconsidered if it ever diverges from PAGE_SIZE.
769848c0 95 *
e12ba74d 96 * Mobility flags are masked out as swap vectors cannot move
1da177e4 97 */
e12ba74d 98 return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
769848c0 99 PAGE_CACHE_SHIFT-PAGE_SHIFT);
1da177e4
LT
100}
101
102static inline void shmem_dir_free(struct page *page)
103{
104 __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
105}
106
107static struct page **shmem_dir_map(struct page *page)
108{
109 return (struct page **)kmap_atomic(page, KM_USER0);
110}
111
112static inline void shmem_dir_unmap(struct page **dir)
113{
114 kunmap_atomic(dir, KM_USER0);
115}
116
117static swp_entry_t *shmem_swp_map(struct page *page)
118{
119 return (swp_entry_t *)kmap_atomic(page, KM_USER1);
120}
121
122static inline void shmem_swp_balance_unmap(void)
123{
124 /*
125 * When passing a pointer to an i_direct entry, to code which
126 * also handles indirect entries and so will shmem_swp_unmap,
127 * we must arrange for the preempt count to remain in balance.
128 * What kmap_atomic of a lowmem page does depends on config
129 * and architecture, so pretend to kmap_atomic some lowmem page.
130 */
131 (void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
132}
133
134static inline void shmem_swp_unmap(swp_entry_t *entry)
135{
136 kunmap_atomic(entry, KM_USER1);
137}
138
139static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
140{
141 return sb->s_fs_info;
142}
143
144/*
145 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
146 * for shared memory and for shared anonymous (/dev/zero) mappings
147 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
148 * consistent with the pre-accounting of private mappings ...
149 */
150static inline int shmem_acct_size(unsigned long flags, loff_t size)
151{
152 return (flags & VM_ACCOUNT)?
153 security_vm_enough_memory(VM_ACCT(size)): 0;
154}
155
156static inline void shmem_unacct_size(unsigned long flags, loff_t size)
157{
158 if (flags & VM_ACCOUNT)
159 vm_unacct_memory(VM_ACCT(size));
160}
161
162/*
163 * ... whereas tmpfs objects are accounted incrementally as
164 * pages are allocated, in order to allow huge sparse files.
165 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
166 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
167 */
168static inline int shmem_acct_block(unsigned long flags)
169{
170 return (flags & VM_ACCOUNT)?
171 0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
172}
173
174static inline void shmem_unacct_blocks(unsigned long flags, long pages)
175{
176 if (!(flags & VM_ACCOUNT))
177 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
178}
179
759b9775 180static const struct super_operations shmem_ops;
f5e54d6e 181static const struct address_space_operations shmem_aops;
15ad7cdc 182static const struct file_operations shmem_file_operations;
92e1d5be
AV
183static const struct inode_operations shmem_inode_operations;
184static const struct inode_operations shmem_dir_inode_operations;
185static const struct inode_operations shmem_special_inode_operations;
1da177e4
LT
186static struct vm_operations_struct shmem_vm_ops;
187
6c231b7b 188static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
1da177e4
LT
189 .ra_pages = 0, /* No readahead */
190 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
191 .unplug_io_fn = default_unplug_io_fn,
192};
193
194static LIST_HEAD(shmem_swaplist);
195static DEFINE_SPINLOCK(shmem_swaplist_lock);
196
197static void shmem_free_blocks(struct inode *inode, long pages)
198{
199 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
0edd73b3 200 if (sbinfo->max_blocks) {
1da177e4
LT
201 spin_lock(&sbinfo->stat_lock);
202 sbinfo->free_blocks += pages;
203 inode->i_blocks -= pages*BLOCKS_PER_PAGE;
204 spin_unlock(&sbinfo->stat_lock);
205 }
206}
207
208/*
209 * shmem_recalc_inode - recalculate the size of an inode
210 *
211 * @inode: inode to recalc
212 *
213 * We have to calculate the free blocks since the mm can drop
214 * undirtied hole pages behind our back.
215 *
216 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
217 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
218 *
219 * It has to be called with the spinlock held.
220 */
221static void shmem_recalc_inode(struct inode *inode)
222{
223 struct shmem_inode_info *info = SHMEM_I(inode);
224 long freed;
225
226 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
227 if (freed > 0) {
228 info->alloced -= freed;
229 shmem_unacct_blocks(info->flags, freed);
230 shmem_free_blocks(inode, freed);
231 }
232}
233
234/*
235 * shmem_swp_entry - find the swap vector position in the info structure
236 *
237 * @info: info structure for the inode
238 * @index: index of the page to find
239 * @page: optional page to add to the structure. Has to be preset to
240 * all zeros
241 *
242 * If there is no space allocated yet it will return NULL when
243 * page is NULL, else it will use the page for the needed block,
244 * setting it to NULL on return to indicate that it has been used.
245 *
246 * The swap vector is organized the following way:
247 *
248 * There are SHMEM_NR_DIRECT entries directly stored in the
249 * shmem_inode_info structure. So small files do not need an addional
250 * allocation.
251 *
252 * For pages with index > SHMEM_NR_DIRECT there is the pointer
253 * i_indirect which points to a page which holds in the first half
254 * doubly indirect blocks, in the second half triple indirect blocks:
255 *
256 * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
257 * following layout (for SHMEM_NR_DIRECT == 16):
258 *
259 * i_indirect -> dir --> 16-19
260 * | +-> 20-23
261 * |
262 * +-->dir2 --> 24-27
263 * | +-> 28-31
264 * | +-> 32-35
265 * | +-> 36-39
266 * |
267 * +-->dir3 --> 40-43
268 * +-> 44-47
269 * +-> 48-51
270 * +-> 52-55
271 */
272static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
273{
274 unsigned long offset;
275 struct page **dir;
276 struct page *subdir;
277
278 if (index < SHMEM_NR_DIRECT) {
279 shmem_swp_balance_unmap();
280 return info->i_direct+index;
281 }
282 if (!info->i_indirect) {
283 if (page) {
284 info->i_indirect = *page;
285 *page = NULL;
286 }
287 return NULL; /* need another page */
288 }
289
290 index -= SHMEM_NR_DIRECT;
291 offset = index % ENTRIES_PER_PAGE;
292 index /= ENTRIES_PER_PAGE;
293 dir = shmem_dir_map(info->i_indirect);
294
295 if (index >= ENTRIES_PER_PAGE/2) {
296 index -= ENTRIES_PER_PAGE/2;
297 dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
298 index %= ENTRIES_PER_PAGE;
299 subdir = *dir;
300 if (!subdir) {
301 if (page) {
302 *dir = *page;
303 *page = NULL;
304 }
305 shmem_dir_unmap(dir);
306 return NULL; /* need another page */
307 }
308 shmem_dir_unmap(dir);
309 dir = shmem_dir_map(subdir);
310 }
311
312 dir += index;
313 subdir = *dir;
314 if (!subdir) {
315 if (!page || !(subdir = *page)) {
316 shmem_dir_unmap(dir);
317 return NULL; /* need a page */
318 }
319 *dir = subdir;
320 *page = NULL;
321 }
322 shmem_dir_unmap(dir);
323 return shmem_swp_map(subdir) + offset;
324}
325
326static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
327{
328 long incdec = value? 1: -1;
329
330 entry->val = value;
331 info->swapped += incdec;
4c21e2f2
HD
332 if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
333 struct page *page = kmap_atomic_to_page(entry);
334 set_page_private(page, page_private(page) + incdec);
335 }
1da177e4
LT
336}
337
338/*
339 * shmem_swp_alloc - get the position of the swap entry for the page.
340 * If it does not exist allocate the entry.
341 *
342 * @info: info structure for the inode
343 * @index: index of the page to find
344 * @sgp: check and recheck i_size? skip allocation?
345 */
346static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
347{
348 struct inode *inode = &info->vfs_inode;
349 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
350 struct page *page = NULL;
351 swp_entry_t *entry;
352
353 if (sgp != SGP_WRITE &&
354 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
355 return ERR_PTR(-EINVAL);
356
357 while (!(entry = shmem_swp_entry(info, index, &page))) {
358 if (sgp == SGP_READ)
359 return shmem_swp_map(ZERO_PAGE(0));
360 /*
361 * Test free_blocks against 1 not 0, since we have 1 data
362 * page (and perhaps indirect index pages) yet to allocate:
363 * a waste to allocate index if we cannot allocate data.
364 */
0edd73b3 365 if (sbinfo->max_blocks) {
1da177e4
LT
366 spin_lock(&sbinfo->stat_lock);
367 if (sbinfo->free_blocks <= 1) {
368 spin_unlock(&sbinfo->stat_lock);
369 return ERR_PTR(-ENOSPC);
370 }
371 sbinfo->free_blocks--;
372 inode->i_blocks += BLOCKS_PER_PAGE;
373 spin_unlock(&sbinfo->stat_lock);
374 }
375
376 spin_unlock(&info->lock);
769848c0 377 page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping));
4c21e2f2
HD
378 if (page)
379 set_page_private(page, 0);
1da177e4
LT
380 spin_lock(&info->lock);
381
382 if (!page) {
383 shmem_free_blocks(inode, 1);
384 return ERR_PTR(-ENOMEM);
385 }
386 if (sgp != SGP_WRITE &&
387 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
388 entry = ERR_PTR(-EINVAL);
389 break;
390 }
391 if (info->next_index <= index)
392 info->next_index = index + 1;
393 }
394 if (page) {
395 /* another task gave its page, or truncated the file */
396 shmem_free_blocks(inode, 1);
397 shmem_dir_free(page);
398 }
399 if (info->next_index <= index && !IS_ERR(entry))
400 info->next_index = index + 1;
401 return entry;
402}
403
404/*
405 * shmem_free_swp - free some swap entries in a directory
406 *
1ae70006
HD
407 * @dir: pointer to the directory
408 * @edir: pointer after last entry of the directory
409 * @punch_lock: pointer to spinlock when needed for the holepunch case
1da177e4 410 */
1ae70006
HD
411static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
412 spinlock_t *punch_lock)
1da177e4 413{
1ae70006 414 spinlock_t *punch_unlock = NULL;
1da177e4
LT
415 swp_entry_t *ptr;
416 int freed = 0;
417
418 for (ptr = dir; ptr < edir; ptr++) {
419 if (ptr->val) {
1ae70006
HD
420 if (unlikely(punch_lock)) {
421 punch_unlock = punch_lock;
422 punch_lock = NULL;
423 spin_lock(punch_unlock);
424 if (!ptr->val)
425 continue;
426 }
1da177e4
LT
427 free_swap_and_cache(*ptr);
428 *ptr = (swp_entry_t){0};
429 freed++;
430 }
431 }
1ae70006
HD
432 if (punch_unlock)
433 spin_unlock(punch_unlock);
1da177e4
LT
434 return freed;
435}
436
1ae70006
HD
437static int shmem_map_and_free_swp(struct page *subdir, int offset,
438 int limit, struct page ***dir, spinlock_t *punch_lock)
1da177e4
LT
439{
440 swp_entry_t *ptr;
441 int freed = 0;
442
443 ptr = shmem_swp_map(subdir);
444 for (; offset < limit; offset += LATENCY_LIMIT) {
445 int size = limit - offset;
446 if (size > LATENCY_LIMIT)
447 size = LATENCY_LIMIT;
1ae70006
HD
448 freed += shmem_free_swp(ptr+offset, ptr+offset+size,
449 punch_lock);
1da177e4
LT
450 if (need_resched()) {
451 shmem_swp_unmap(ptr);
452 if (*dir) {
453 shmem_dir_unmap(*dir);
454 *dir = NULL;
455 }
456 cond_resched();
457 ptr = shmem_swp_map(subdir);
458 }
459 }
460 shmem_swp_unmap(ptr);
461 return freed;
462}
463
464static void shmem_free_pages(struct list_head *next)
465{
466 struct page *page;
467 int freed = 0;
468
469 do {
470 page = container_of(next, struct page, lru);
471 next = next->next;
472 shmem_dir_free(page);
473 freed++;
474 if (freed >= LATENCY_LIMIT) {
475 cond_resched();
476 freed = 0;
477 }
478 } while (next);
479}
480
f6b3ec23 481static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
1da177e4
LT
482{
483 struct shmem_inode_info *info = SHMEM_I(inode);
484 unsigned long idx;
485 unsigned long size;
486 unsigned long limit;
487 unsigned long stage;
488 unsigned long diroff;
489 struct page **dir;
490 struct page *topdir;
491 struct page *middir;
492 struct page *subdir;
493 swp_entry_t *ptr;
494 LIST_HEAD(pages_to_free);
495 long nr_pages_to_free = 0;
496 long nr_swaps_freed = 0;
497 int offset;
498 int freed;
a2646d1e 499 int punch_hole;
1ae70006
HD
500 spinlock_t *needs_lock;
501 spinlock_t *punch_lock;
a2646d1e 502 unsigned long upper_limit;
1da177e4
LT
503
504 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
f6b3ec23 505 idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1da177e4
LT
506 if (idx >= info->next_index)
507 return;
508
509 spin_lock(&info->lock);
510 info->flags |= SHMEM_TRUNCATE;
f6b3ec23
BP
511 if (likely(end == (loff_t) -1)) {
512 limit = info->next_index;
a2646d1e 513 upper_limit = SHMEM_MAX_INDEX;
f6b3ec23 514 info->next_index = idx;
1ae70006 515 needs_lock = NULL;
a2646d1e 516 punch_hole = 0;
f6b3ec23 517 } else {
a2646d1e
HD
518 if (end + 1 >= inode->i_size) { /* we may free a little more */
519 limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
520 PAGE_CACHE_SHIFT;
521 upper_limit = SHMEM_MAX_INDEX;
522 } else {
523 limit = (end + 1) >> PAGE_CACHE_SHIFT;
524 upper_limit = limit;
525 }
1ae70006 526 needs_lock = &info->lock;
f6b3ec23
BP
527 punch_hole = 1;
528 }
529
1da177e4 530 topdir = info->i_indirect;
f6b3ec23 531 if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
1da177e4
LT
532 info->i_indirect = NULL;
533 nr_pages_to_free++;
534 list_add(&topdir->lru, &pages_to_free);
535 }
536 spin_unlock(&info->lock);
537
538 if (info->swapped && idx < SHMEM_NR_DIRECT) {
539 ptr = info->i_direct;
540 size = limit;
541 if (size > SHMEM_NR_DIRECT)
542 size = SHMEM_NR_DIRECT;
1ae70006 543 nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
1da177e4 544 }
92a3d03a
BP
545
546 /*
547 * If there are no indirect blocks or we are punching a hole
548 * below indirect blocks, nothing to be done.
549 */
a2646d1e 550 if (!topdir || limit <= SHMEM_NR_DIRECT)
1da177e4
LT
551 goto done2;
552
1ae70006
HD
553 /*
554 * The truncation case has already dropped info->lock, and we're safe
555 * because i_size and next_index have already been lowered, preventing
556 * access beyond. But in the punch_hole case, we still need to take
557 * the lock when updating the swap directory, because there might be
558 * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
559 * shmem_writepage. However, whenever we find we can remove a whole
560 * directory page (not at the misaligned start or end of the range),
561 * we first NULLify its pointer in the level above, and then have no
562 * need to take the lock when updating its contents: needs_lock and
563 * punch_lock (either pointing to info->lock or NULL) manage this.
564 */
565
a2646d1e 566 upper_limit -= SHMEM_NR_DIRECT;
1da177e4
LT
567 limit -= SHMEM_NR_DIRECT;
568 idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
569 offset = idx % ENTRIES_PER_PAGE;
570 idx -= offset;
571
572 dir = shmem_dir_map(topdir);
573 stage = ENTRIES_PER_PAGEPAGE/2;
574 if (idx < ENTRIES_PER_PAGEPAGE/2) {
575 middir = topdir;
576 diroff = idx/ENTRIES_PER_PAGE;
577 } else {
578 dir += ENTRIES_PER_PAGE/2;
579 dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
580 while (stage <= idx)
581 stage += ENTRIES_PER_PAGEPAGE;
582 middir = *dir;
583 if (*dir) {
584 diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
585 ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
a2646d1e 586 if (!diroff && !offset && upper_limit >= stage) {
1ae70006
HD
587 if (needs_lock) {
588 spin_lock(needs_lock);
589 *dir = NULL;
590 spin_unlock(needs_lock);
591 needs_lock = NULL;
592 } else
593 *dir = NULL;
1da177e4
LT
594 nr_pages_to_free++;
595 list_add(&middir->lru, &pages_to_free);
596 }
597 shmem_dir_unmap(dir);
598 dir = shmem_dir_map(middir);
599 } else {
600 diroff = 0;
601 offset = 0;
602 idx = stage;
603 }
604 }
605
606 for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
607 if (unlikely(idx == stage)) {
608 shmem_dir_unmap(dir);
609 dir = shmem_dir_map(topdir) +
610 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
611 while (!*dir) {
612 dir++;
613 idx += ENTRIES_PER_PAGEPAGE;
614 if (idx >= limit)
615 goto done1;
616 }
617 stage = idx + ENTRIES_PER_PAGEPAGE;
618 middir = *dir;
1ae70006
HD
619 if (punch_hole)
620 needs_lock = &info->lock;
a2646d1e 621 if (upper_limit >= stage) {
1ae70006
HD
622 if (needs_lock) {
623 spin_lock(needs_lock);
624 *dir = NULL;
625 spin_unlock(needs_lock);
626 needs_lock = NULL;
627 } else
628 *dir = NULL;
a2646d1e
HD
629 nr_pages_to_free++;
630 list_add(&middir->lru, &pages_to_free);
631 }
1da177e4
LT
632 shmem_dir_unmap(dir);
633 cond_resched();
634 dir = shmem_dir_map(middir);
635 diroff = 0;
636 }
1ae70006 637 punch_lock = needs_lock;
1da177e4 638 subdir = dir[diroff];
1ae70006
HD
639 if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
640 if (needs_lock) {
641 spin_lock(needs_lock);
642 dir[diroff] = NULL;
643 spin_unlock(needs_lock);
644 punch_lock = NULL;
645 } else
646 dir[diroff] = NULL;
647 nr_pages_to_free++;
648 list_add(&subdir->lru, &pages_to_free);
649 }
650 if (subdir && page_private(subdir) /* has swap entries */) {
1da177e4
LT
651 size = limit - idx;
652 if (size > ENTRIES_PER_PAGE)
653 size = ENTRIES_PER_PAGE;
654 freed = shmem_map_and_free_swp(subdir,
1ae70006 655 offset, size, &dir, punch_lock);
1da177e4
LT
656 if (!dir)
657 dir = shmem_dir_map(middir);
658 nr_swaps_freed += freed;
1ae70006 659 if (offset || punch_lock) {
1da177e4 660 spin_lock(&info->lock);
1ae70006
HD
661 set_page_private(subdir,
662 page_private(subdir) - freed);
1da177e4 663 spin_unlock(&info->lock);
1ae70006
HD
664 } else
665 BUG_ON(page_private(subdir) != freed);
1da177e4 666 }
1ae70006 667 offset = 0;
1da177e4
LT
668 }
669done1:
670 shmem_dir_unmap(dir);
671done2:
672 if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
673 /*
674 * Call truncate_inode_pages again: racing shmem_unuse_inode
675 * may have swizzled a page in from swap since vmtruncate or
676 * generic_delete_inode did it, before we lowered next_index.
677 * Also, though shmem_getpage checks i_size before adding to
678 * cache, no recheck after: so fix the narrow window there too.
16a10019
HD
679 *
680 * Recalling truncate_inode_pages_range and unmap_mapping_range
681 * every time for punch_hole (which never got a chance to clear
682 * SHMEM_PAGEIN at the start of vmtruncate_range) is expensive,
683 * yet hardly ever necessary: try to optimize them out later.
1da177e4 684 */
f6b3ec23 685 truncate_inode_pages_range(inode->i_mapping, start, end);
16a10019
HD
686 if (punch_hole)
687 unmap_mapping_range(inode->i_mapping, start,
688 end - start, 1);
1da177e4
LT
689 }
690
691 spin_lock(&info->lock);
692 info->flags &= ~SHMEM_TRUNCATE;
693 info->swapped -= nr_swaps_freed;
694 if (nr_pages_to_free)
695 shmem_free_blocks(inode, nr_pages_to_free);
696 shmem_recalc_inode(inode);
697 spin_unlock(&info->lock);
698
699 /*
700 * Empty swap vector directory pages to be freed?
701 */
702 if (!list_empty(&pages_to_free)) {
703 pages_to_free.prev->next = NULL;
704 shmem_free_pages(pages_to_free.next);
705 }
706}
707
f6b3ec23
BP
708static void shmem_truncate(struct inode *inode)
709{
710 shmem_truncate_range(inode, inode->i_size, (loff_t)-1);
711}
712
1da177e4
LT
713static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
714{
715 struct inode *inode = dentry->d_inode;
716 struct page *page = NULL;
717 int error;
718
39f0247d 719 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
1da177e4
LT
720 if (attr->ia_size < inode->i_size) {
721 /*
722 * If truncating down to a partial page, then
723 * if that page is already allocated, hold it
724 * in memory until the truncation is over, so
725 * truncate_partial_page cannnot miss it were
726 * it assigned to swap.
727 */
728 if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
729 (void) shmem_getpage(inode,
730 attr->ia_size>>PAGE_CACHE_SHIFT,
731 &page, SGP_READ, NULL);
d3602444
HD
732 if (page)
733 unlock_page(page);
1da177e4
LT
734 }
735 /*
736 * Reset SHMEM_PAGEIN flag so that shmem_truncate can
737 * detect if any pages might have been added to cache
738 * after truncate_inode_pages. But we needn't bother
739 * if it's being fully truncated to zero-length: the
740 * nrpages check is efficient enough in that case.
741 */
742 if (attr->ia_size) {
743 struct shmem_inode_info *info = SHMEM_I(inode);
744 spin_lock(&info->lock);
745 info->flags &= ~SHMEM_PAGEIN;
746 spin_unlock(&info->lock);
747 }
748 }
749 }
750
751 error = inode_change_ok(inode, attr);
752 if (!error)
753 error = inode_setattr(inode, attr);
39f0247d
AG
754#ifdef CONFIG_TMPFS_POSIX_ACL
755 if (!error && (attr->ia_valid & ATTR_MODE))
756 error = generic_acl_chmod(inode, &shmem_acl_ops);
757#endif
1da177e4
LT
758 if (page)
759 page_cache_release(page);
760 return error;
761}
762
763static void shmem_delete_inode(struct inode *inode)
764{
765 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
766 struct shmem_inode_info *info = SHMEM_I(inode);
767
768 if (inode->i_op->truncate == shmem_truncate) {
fef26658 769 truncate_inode_pages(inode->i_mapping, 0);
1da177e4
LT
770 shmem_unacct_size(info->flags, inode->i_size);
771 inode->i_size = 0;
772 shmem_truncate(inode);
773 if (!list_empty(&info->swaplist)) {
774 spin_lock(&shmem_swaplist_lock);
775 list_del_init(&info->swaplist);
776 spin_unlock(&shmem_swaplist_lock);
777 }
778 }
0edd73b3
HD
779 BUG_ON(inode->i_blocks);
780 if (sbinfo->max_inodes) {
1da177e4
LT
781 spin_lock(&sbinfo->stat_lock);
782 sbinfo->free_inodes++;
783 spin_unlock(&sbinfo->stat_lock);
784 }
785 clear_inode(inode);
786}
787
788static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
789{
790 swp_entry_t *ptr;
791
792 for (ptr = dir; ptr < edir; ptr++) {
793 if (ptr->val == entry.val)
794 return ptr - dir;
795 }
796 return -1;
797}
798
799static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
800{
801 struct inode *inode;
802 unsigned long idx;
803 unsigned long size;
804 unsigned long limit;
805 unsigned long stage;
806 struct page **dir;
807 struct page *subdir;
808 swp_entry_t *ptr;
809 int offset;
810
811 idx = 0;
812 ptr = info->i_direct;
813 spin_lock(&info->lock);
814 limit = info->next_index;
815 size = limit;
816 if (size > SHMEM_NR_DIRECT)
817 size = SHMEM_NR_DIRECT;
818 offset = shmem_find_swp(entry, ptr, ptr+size);
819 if (offset >= 0) {
820 shmem_swp_balance_unmap();
821 goto found;
822 }
823 if (!info->i_indirect)
824 goto lost2;
825
826 dir = shmem_dir_map(info->i_indirect);
827 stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
828
829 for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
830 if (unlikely(idx == stage)) {
831 shmem_dir_unmap(dir-1);
832 dir = shmem_dir_map(info->i_indirect) +
833 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
834 while (!*dir) {
835 dir++;
836 idx += ENTRIES_PER_PAGEPAGE;
837 if (idx >= limit)
838 goto lost1;
839 }
840 stage = idx + ENTRIES_PER_PAGEPAGE;
841 subdir = *dir;
842 shmem_dir_unmap(dir);
843 dir = shmem_dir_map(subdir);
844 }
845 subdir = *dir;
4c21e2f2 846 if (subdir && page_private(subdir)) {
1da177e4
LT
847 ptr = shmem_swp_map(subdir);
848 size = limit - idx;
849 if (size > ENTRIES_PER_PAGE)
850 size = ENTRIES_PER_PAGE;
851 offset = shmem_find_swp(entry, ptr, ptr+size);
852 if (offset >= 0) {
853 shmem_dir_unmap(dir);
854 goto found;
855 }
856 shmem_swp_unmap(ptr);
857 }
858 }
859lost1:
860 shmem_dir_unmap(dir-1);
861lost2:
862 spin_unlock(&info->lock);
863 return 0;
864found:
865 idx += offset;
866 inode = &info->vfs_inode;
867 if (move_from_swap_cache(page, idx, inode->i_mapping) == 0) {
868 info->flags |= SHMEM_PAGEIN;
869 shmem_swp_set(info, ptr + offset, 0);
870 }
871 shmem_swp_unmap(ptr);
872 spin_unlock(&info->lock);
873 /*
874 * Decrement swap count even when the entry is left behind:
875 * try_to_unuse will skip over mms, then reincrement count.
876 */
877 swap_free(entry);
878 return 1;
879}
880
881/*
882 * shmem_unuse() search for an eventually swapped out shmem page.
883 */
884int shmem_unuse(swp_entry_t entry, struct page *page)
885{
886 struct list_head *p, *next;
887 struct shmem_inode_info *info;
888 int found = 0;
889
890 spin_lock(&shmem_swaplist_lock);
891 list_for_each_safe(p, next, &shmem_swaplist) {
892 info = list_entry(p, struct shmem_inode_info, swaplist);
893 if (!info->swapped)
894 list_del_init(&info->swaplist);
895 else if (shmem_unuse_inode(info, entry, page)) {
896 /* move head to start search for next from here */
897 list_move_tail(&shmem_swaplist, &info->swaplist);
898 found = 1;
899 break;
900 }
901 }
902 spin_unlock(&shmem_swaplist_lock);
903 return found;
904}
905
906/*
907 * Move the page from the page cache to the swap cache.
908 */
909static int shmem_writepage(struct page *page, struct writeback_control *wbc)
910{
911 struct shmem_inode_info *info;
912 swp_entry_t *entry, swap;
913 struct address_space *mapping;
914 unsigned long index;
915 struct inode *inode;
916
917 BUG_ON(!PageLocked(page));
487e9bf2
HD
918 /*
919 * shmem_backing_dev_info's capabilities prevent regular writeback or
920 * sync from ever calling shmem_writepage; but a stacking filesystem
921 * may use the ->writepage of its underlying filesystem, in which case
922 * we want to do nothing when that underlying filesystem is tmpfs
923 * (writing out to swap is useful as a response to memory pressure, but
924 * of no use to stabilize the data) - just redirty the page, unlock it
925 * and claim success in this case. AOP_WRITEPAGE_ACTIVATE, and the
926 * page_mapped check below, must be avoided unless we're in reclaim.
927 */
928 if (!wbc->for_reclaim) {
929 set_page_dirty(page);
930 unlock_page(page);
931 return 0;
932 }
1da177e4
LT
933 BUG_ON(page_mapped(page));
934
935 mapping = page->mapping;
936 index = page->index;
937 inode = mapping->host;
938 info = SHMEM_I(inode);
939 if (info->flags & VM_LOCKED)
940 goto redirty;
941 swap = get_swap_page();
942 if (!swap.val)
943 goto redirty;
944
945 spin_lock(&info->lock);
946 shmem_recalc_inode(inode);
947 if (index >= info->next_index) {
948 BUG_ON(!(info->flags & SHMEM_TRUNCATE));
949 goto unlock;
950 }
951 entry = shmem_swp_entry(info, index, NULL);
952 BUG_ON(!entry);
953 BUG_ON(entry->val);
954
955 if (move_to_swap_cache(page, swap) == 0) {
956 shmem_swp_set(info, entry, swap.val);
957 shmem_swp_unmap(entry);
958 spin_unlock(&info->lock);
959 if (list_empty(&info->swaplist)) {
960 spin_lock(&shmem_swaplist_lock);
961 /* move instead of add in case we're racing */
962 list_move_tail(&info->swaplist, &shmem_swaplist);
963 spin_unlock(&shmem_swaplist_lock);
964 }
965 unlock_page(page);
966 return 0;
967 }
968
969 shmem_swp_unmap(entry);
970unlock:
971 spin_unlock(&info->lock);
972 swap_free(swap);
973redirty:
974 set_page_dirty(page);
994fc28c 975 return AOP_WRITEPAGE_ACTIVATE; /* Return with the page locked */
1da177e4
LT
976}
977
978#ifdef CONFIG_NUMA
d15c023b 979static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes)
b00dc3ad
HD
980{
981 char *nodelist = strchr(value, ':');
982 int err = 1;
983
984 if (nodelist) {
985 /* NUL-terminate policy string */
986 *nodelist++ = '\0';
987 if (nodelist_parse(nodelist, *policy_nodes))
988 goto out;
37b07e41 989 if (!nodes_subset(*policy_nodes, node_states[N_HIGH_MEMORY]))
a210906c 990 goto out;
b00dc3ad
HD
991 }
992 if (!strcmp(value, "default")) {
993 *policy = MPOL_DEFAULT;
994 /* Don't allow a nodelist */
995 if (!nodelist)
996 err = 0;
997 } else if (!strcmp(value, "prefer")) {
998 *policy = MPOL_PREFERRED;
999 /* Insist on a nodelist of one node only */
1000 if (nodelist) {
1001 char *rest = nodelist;
1002 while (isdigit(*rest))
1003 rest++;
1004 if (!*rest)
1005 err = 0;
1006 }
1007 } else if (!strcmp(value, "bind")) {
1008 *policy = MPOL_BIND;
1009 /* Insist on a nodelist */
1010 if (nodelist)
1011 err = 0;
1012 } else if (!strcmp(value, "interleave")) {
1013 *policy = MPOL_INTERLEAVE;
37b07e41
LS
1014 /*
1015 * Default to online nodes with memory if no nodelist
1016 */
b00dc3ad 1017 if (!nodelist)
37b07e41 1018 *policy_nodes = node_states[N_HIGH_MEMORY];
b00dc3ad
HD
1019 err = 0;
1020 }
1021out:
1022 /* Restore string for error message */
1023 if (nodelist)
1024 *--nodelist = ':';
1025 return err;
1026}
1027
02098fea
HD
1028static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
1029 struct shmem_inode_info *info, unsigned long idx)
1da177e4 1030{
1da177e4 1031 struct vm_area_struct pvma;
c4cc6d07 1032 struct page *page;
1da177e4
LT
1033
1034 /* Create a pseudo vma that just contains the policy */
c4cc6d07 1035 pvma.vm_start = 0;
1da177e4 1036 pvma.vm_pgoff = idx;
c4cc6d07
HD
1037 pvma.vm_ops = NULL;
1038 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
02098fea 1039 page = swapin_readahead(entry, gfp, &pvma, 0);
1da177e4
LT
1040 mpol_free(pvma.vm_policy);
1041 return page;
1042}
1043
02098fea
HD
1044static struct page *shmem_alloc_page(gfp_t gfp,
1045 struct shmem_inode_info *info, unsigned long idx)
1da177e4
LT
1046{
1047 struct vm_area_struct pvma;
1048 struct page *page;
1049
c4cc6d07
HD
1050 /* Create a pseudo vma that just contains the policy */
1051 pvma.vm_start = 0;
1da177e4 1052 pvma.vm_pgoff = idx;
c4cc6d07
HD
1053 pvma.vm_ops = NULL;
1054 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
e84e2e13 1055 page = alloc_page_vma(gfp, &pvma, 0);
1da177e4
LT
1056 mpol_free(pvma.vm_policy);
1057 return page;
1058}
1059#else
37b07e41
LS
1060static inline int shmem_parse_mpol(char *value, int *policy,
1061 nodemask_t *policy_nodes)
b00dc3ad
HD
1062{
1063 return 1;
1064}
1065
02098fea
HD
1066static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
1067 struct shmem_inode_info *info, unsigned long idx)
1da177e4 1068{
02098fea 1069 return swapin_readahead(entry, gfp, NULL, 0);
1da177e4
LT
1070}
1071
02098fea
HD
1072static inline struct page *shmem_alloc_page(gfp_t gfp,
1073 struct shmem_inode_info *info, unsigned long idx)
1da177e4 1074{
e84e2e13 1075 return alloc_page(gfp);
1da177e4
LT
1076}
1077#endif
1078
1079/*
1080 * shmem_getpage - either get the page from swap or allocate a new one
1081 *
1082 * If we allocate a new one we do not mark it dirty. That's up to the
1083 * vm. If we swap it in we mark it dirty since we also free the swap
1084 * entry since a page cannot live in both the swap and page cache
1085 */
1086static int shmem_getpage(struct inode *inode, unsigned long idx,
1087 struct page **pagep, enum sgp_type sgp, int *type)
1088{
1089 struct address_space *mapping = inode->i_mapping;
1090 struct shmem_inode_info *info = SHMEM_I(inode);
1091 struct shmem_sb_info *sbinfo;
1092 struct page *filepage = *pagep;
1093 struct page *swappage;
1094 swp_entry_t *entry;
1095 swp_entry_t swap;
02098fea 1096 gfp_t gfp;
1da177e4
LT
1097 int error;
1098
1099 if (idx >= SHMEM_MAX_INDEX)
1100 return -EFBIG;
54cb8821
NP
1101
1102 if (type)
83c54070 1103 *type = 0;
54cb8821 1104
1da177e4
LT
1105 /*
1106 * Normally, filepage is NULL on entry, and either found
1107 * uptodate immediately, or allocated and zeroed, or read
1108 * in under swappage, which is then assigned to filepage.
5402b976 1109 * But shmem_readpage (required for splice) passes in a locked
ae976416
HD
1110 * filepage, which may be found not uptodate by other callers
1111 * too, and may need to be copied from the swappage read in.
1da177e4
LT
1112 */
1113repeat:
1114 if (!filepage)
1115 filepage = find_lock_page(mapping, idx);
1116 if (filepage && PageUptodate(filepage))
1117 goto done;
1118 error = 0;
02098fea 1119 gfp = mapping_gfp_mask(mapping);
1da177e4
LT
1120
1121 spin_lock(&info->lock);
1122 shmem_recalc_inode(inode);
1123 entry = shmem_swp_alloc(info, idx, sgp);
1124 if (IS_ERR(entry)) {
1125 spin_unlock(&info->lock);
1126 error = PTR_ERR(entry);
1127 goto failed;
1128 }
1129 swap = *entry;
1130
1131 if (swap.val) {
1132 /* Look it up and read it in.. */
1133 swappage = lookup_swap_cache(swap);
1134 if (!swappage) {
1135 shmem_swp_unmap(entry);
1da177e4 1136 /* here we actually do the io */
83c54070 1137 if (type && !(*type & VM_FAULT_MAJOR)) {
f8891e5e 1138 __count_vm_event(PGMAJFAULT);
83c54070 1139 *type |= VM_FAULT_MAJOR;
1da177e4 1140 }
f8891e5e 1141 spin_unlock(&info->lock);
02098fea 1142 swappage = shmem_swapin(swap, gfp, info, idx);
1da177e4
LT
1143 if (!swappage) {
1144 spin_lock(&info->lock);
1145 entry = shmem_swp_alloc(info, idx, sgp);
1146 if (IS_ERR(entry))
1147 error = PTR_ERR(entry);
1148 else {
1149 if (entry->val == swap.val)
1150 error = -ENOMEM;
1151 shmem_swp_unmap(entry);
1152 }
1153 spin_unlock(&info->lock);
1154 if (error)
1155 goto failed;
1156 goto repeat;
1157 }
1158 wait_on_page_locked(swappage);
1159 page_cache_release(swappage);
1160 goto repeat;
1161 }
1162
1163 /* We have to do this with page locked to prevent races */
1164 if (TestSetPageLocked(swappage)) {
1165 shmem_swp_unmap(entry);
1166 spin_unlock(&info->lock);
1167 wait_on_page_locked(swappage);
1168 page_cache_release(swappage);
1169 goto repeat;
1170 }
1171 if (PageWriteback(swappage)) {
1172 shmem_swp_unmap(entry);
1173 spin_unlock(&info->lock);
1174 wait_on_page_writeback(swappage);
1175 unlock_page(swappage);
1176 page_cache_release(swappage);
1177 goto repeat;
1178 }
1179 if (!PageUptodate(swappage)) {
1180 shmem_swp_unmap(entry);
1181 spin_unlock(&info->lock);
1182 unlock_page(swappage);
1183 page_cache_release(swappage);
1184 error = -EIO;
1185 goto failed;
1186 }
1187
1188 if (filepage) {
1189 shmem_swp_set(info, entry, 0);
1190 shmem_swp_unmap(entry);
1191 delete_from_swap_cache(swappage);
1192 spin_unlock(&info->lock);
1193 copy_highpage(filepage, swappage);
1194 unlock_page(swappage);
1195 page_cache_release(swappage);
1196 flush_dcache_page(filepage);
1197 SetPageUptodate(filepage);
1198 set_page_dirty(filepage);
1199 swap_free(swap);
1200 } else if (!(error = move_from_swap_cache(
1201 swappage, idx, mapping))) {
1202 info->flags |= SHMEM_PAGEIN;
1203 shmem_swp_set(info, entry, 0);
1204 shmem_swp_unmap(entry);
1205 spin_unlock(&info->lock);
1206 filepage = swappage;
1207 swap_free(swap);
1208 } else {
1209 shmem_swp_unmap(entry);
1210 spin_unlock(&info->lock);
1211 unlock_page(swappage);
1212 page_cache_release(swappage);
1213 if (error == -ENOMEM) {
1214 /* let kswapd refresh zone for GFP_ATOMICs */
3fcfab16 1215 congestion_wait(WRITE, HZ/50);
1da177e4
LT
1216 }
1217 goto repeat;
1218 }
1219 } else if (sgp == SGP_READ && !filepage) {
1220 shmem_swp_unmap(entry);
1221 filepage = find_get_page(mapping, idx);
1222 if (filepage &&
1223 (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
1224 spin_unlock(&info->lock);
1225 wait_on_page_locked(filepage);
1226 page_cache_release(filepage);
1227 filepage = NULL;
1228 goto repeat;
1229 }
1230 spin_unlock(&info->lock);
1231 } else {
1232 shmem_swp_unmap(entry);
1233 sbinfo = SHMEM_SB(inode->i_sb);
0edd73b3 1234 if (sbinfo->max_blocks) {
1da177e4
LT
1235 spin_lock(&sbinfo->stat_lock);
1236 if (sbinfo->free_blocks == 0 ||
1237 shmem_acct_block(info->flags)) {
1238 spin_unlock(&sbinfo->stat_lock);
1239 spin_unlock(&info->lock);
1240 error = -ENOSPC;
1241 goto failed;
1242 }
1243 sbinfo->free_blocks--;
1244 inode->i_blocks += BLOCKS_PER_PAGE;
1245 spin_unlock(&sbinfo->stat_lock);
1246 } else if (shmem_acct_block(info->flags)) {
1247 spin_unlock(&info->lock);
1248 error = -ENOSPC;
1249 goto failed;
1250 }
1251
1252 if (!filepage) {
1253 spin_unlock(&info->lock);
02098fea 1254 filepage = shmem_alloc_page(gfp, info, idx);
1da177e4
LT
1255 if (!filepage) {
1256 shmem_unacct_blocks(info->flags, 1);
1257 shmem_free_blocks(inode, 1);
1258 error = -ENOMEM;
1259 goto failed;
1260 }
1261
1262 spin_lock(&info->lock);
1263 entry = shmem_swp_alloc(info, idx, sgp);
1264 if (IS_ERR(entry))
1265 error = PTR_ERR(entry);
1266 else {
1267 swap = *entry;
1268 shmem_swp_unmap(entry);
1269 }
1270 if (error || swap.val || 0 != add_to_page_cache_lru(
1271 filepage, mapping, idx, GFP_ATOMIC)) {
1272 spin_unlock(&info->lock);
1273 page_cache_release(filepage);
1274 shmem_unacct_blocks(info->flags, 1);
1275 shmem_free_blocks(inode, 1);
1276 filepage = NULL;
1277 if (error)
1278 goto failed;
1279 goto repeat;
1280 }
1281 info->flags |= SHMEM_PAGEIN;
1282 }
1283
1284 info->alloced++;
1285 spin_unlock(&info->lock);
e84e2e13 1286 clear_highpage(filepage);
1da177e4
LT
1287 flush_dcache_page(filepage);
1288 SetPageUptodate(filepage);
1289 }
1290done:
d3602444 1291 *pagep = filepage;
1da177e4
LT
1292 return 0;
1293
1294failed:
1295 if (*pagep != filepage) {
1296 unlock_page(filepage);
1297 page_cache_release(filepage);
1298 }
1299 return error;
1300}
1301
d0217ac0 1302static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1da177e4 1303{
d3ac7f89 1304 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1da177e4 1305 int error;
d0217ac0 1306 int ret;
1da177e4 1307
d0217ac0
NP
1308 if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1309 return VM_FAULT_SIGBUS;
d00806b1 1310
27d54b39 1311 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
d0217ac0
NP
1312 if (error)
1313 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
1da177e4 1314
d0217ac0 1315 mark_page_accessed(vmf->page);
83c54070 1316 return ret | VM_FAULT_LOCKED;
1da177e4
LT
1317}
1318
1da177e4 1319#ifdef CONFIG_NUMA
d8dc74f2 1320static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
1da177e4 1321{
d3ac7f89 1322 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
1da177e4
LT
1323 return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
1324}
1325
d8dc74f2
AB
1326static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1327 unsigned long addr)
1da177e4 1328{
d3ac7f89 1329 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
1da177e4
LT
1330 unsigned long idx;
1331
1332 idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1333 return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
1334}
1335#endif
1336
1337int shmem_lock(struct file *file, int lock, struct user_struct *user)
1338{
d3ac7f89 1339 struct inode *inode = file->f_path.dentry->d_inode;
1da177e4
LT
1340 struct shmem_inode_info *info = SHMEM_I(inode);
1341 int retval = -ENOMEM;
1342
1343 spin_lock(&info->lock);
1344 if (lock && !(info->flags & VM_LOCKED)) {
1345 if (!user_shm_lock(inode->i_size, user))
1346 goto out_nomem;
1347 info->flags |= VM_LOCKED;
1348 }
1349 if (!lock && (info->flags & VM_LOCKED) && user) {
1350 user_shm_unlock(inode->i_size, user);
1351 info->flags &= ~VM_LOCKED;
1352 }
1353 retval = 0;
1354out_nomem:
1355 spin_unlock(&info->lock);
1356 return retval;
1357}
1358
9b83a6a8 1359static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1da177e4
LT
1360{
1361 file_accessed(file);
1362 vma->vm_ops = &shmem_vm_ops;
d0217ac0 1363 vma->vm_flags |= VM_CAN_NONLINEAR;
1da177e4
LT
1364 return 0;
1365}
1366
1367static struct inode *
1368shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
1369{
1370 struct inode *inode;
1371 struct shmem_inode_info *info;
1372 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1373
0edd73b3 1374 if (sbinfo->max_inodes) {
1da177e4
LT
1375 spin_lock(&sbinfo->stat_lock);
1376 if (!sbinfo->free_inodes) {
1377 spin_unlock(&sbinfo->stat_lock);
1378 return NULL;
1379 }
1380 sbinfo->free_inodes--;
1381 spin_unlock(&sbinfo->stat_lock);
1382 }
1383
1384 inode = new_inode(sb);
1385 if (inode) {
1386 inode->i_mode = mode;
1387 inode->i_uid = current->fsuid;
1388 inode->i_gid = current->fsgid;
1da177e4
LT
1389 inode->i_blocks = 0;
1390 inode->i_mapping->a_ops = &shmem_aops;
1391 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1392 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
91828a40 1393 inode->i_generation = get_seconds();
1da177e4
LT
1394 info = SHMEM_I(inode);
1395 memset(info, 0, (char *)inode - (char *)info);
1396 spin_lock_init(&info->lock);
1397 INIT_LIST_HEAD(&info->swaplist);
1398
1399 switch (mode & S_IFMT) {
1400 default:
39f0247d 1401 inode->i_op = &shmem_special_inode_operations;
1da177e4
LT
1402 init_special_inode(inode, mode, dev);
1403 break;
1404 case S_IFREG:
1405 inode->i_op = &shmem_inode_operations;
1406 inode->i_fop = &shmem_file_operations;
7339ff83
RH
1407 mpol_shared_policy_init(&info->policy, sbinfo->policy,
1408 &sbinfo->policy_nodes);
1da177e4
LT
1409 break;
1410 case S_IFDIR:
d8c76e6f 1411 inc_nlink(inode);
1da177e4
LT
1412 /* Some things misbehave if size == 0 on a directory */
1413 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1414 inode->i_op = &shmem_dir_inode_operations;
1415 inode->i_fop = &simple_dir_operations;
1416 break;
1417 case S_IFLNK:
1418 /*
1419 * Must not load anything in the rbtree,
1420 * mpol_free_shared_policy will not be called.
1421 */
7339ff83
RH
1422 mpol_shared_policy_init(&info->policy, MPOL_DEFAULT,
1423 NULL);
1da177e4
LT
1424 break;
1425 }
0edd73b3 1426 } else if (sbinfo->max_inodes) {
1da177e4
LT
1427 spin_lock(&sbinfo->stat_lock);
1428 sbinfo->free_inodes++;
1429 spin_unlock(&sbinfo->stat_lock);
1430 }
1431 return inode;
1432}
1433
1434#ifdef CONFIG_TMPFS
92e1d5be
AV
1435static const struct inode_operations shmem_symlink_inode_operations;
1436static const struct inode_operations shmem_symlink_inline_operations;
1da177e4
LT
1437
1438/*
800d15a5 1439 * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
ae976416
HD
1440 * but providing them allows a tmpfs file to be used for splice, sendfile, and
1441 * below the loop driver, in the generic fashion that many filesystems support.
1da177e4 1442 */
ae976416
HD
1443static int shmem_readpage(struct file *file, struct page *page)
1444{
1445 struct inode *inode = page->mapping->host;
1446 int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
1447 unlock_page(page);
1448 return error;
1449}
1450
1da177e4 1451static int
800d15a5
NP
1452shmem_write_begin(struct file *file, struct address_space *mapping,
1453 loff_t pos, unsigned len, unsigned flags,
1454 struct page **pagep, void **fsdata)
1da177e4 1455{
800d15a5
NP
1456 struct inode *inode = mapping->host;
1457 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1458 *pagep = NULL;
1459 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1460}
1461
1462static int
1463shmem_write_end(struct file *file, struct address_space *mapping,
1464 loff_t pos, unsigned len, unsigned copied,
1465 struct page *page, void *fsdata)
1466{
1467 struct inode *inode = mapping->host;
1468
d3602444
HD
1469 if (pos + copied > inode->i_size)
1470 i_size_write(inode, pos + copied);
1471
1472 unlock_page(page);
800d15a5
NP
1473 set_page_dirty(page);
1474 page_cache_release(page);
1475
800d15a5 1476 return copied;
1da177e4
LT
1477}
1478
1da177e4
LT
1479static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1480{
d3ac7f89 1481 struct inode *inode = filp->f_path.dentry->d_inode;
1da177e4
LT
1482 struct address_space *mapping = inode->i_mapping;
1483 unsigned long index, offset;
1484
1485 index = *ppos >> PAGE_CACHE_SHIFT;
1486 offset = *ppos & ~PAGE_CACHE_MASK;
1487
1488 for (;;) {
1489 struct page *page = NULL;
1490 unsigned long end_index, nr, ret;
1491 loff_t i_size = i_size_read(inode);
1492
1493 end_index = i_size >> PAGE_CACHE_SHIFT;
1494 if (index > end_index)
1495 break;
1496 if (index == end_index) {
1497 nr = i_size & ~PAGE_CACHE_MASK;
1498 if (nr <= offset)
1499 break;
1500 }
1501
1502 desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
1503 if (desc->error) {
1504 if (desc->error == -EINVAL)
1505 desc->error = 0;
1506 break;
1507 }
d3602444
HD
1508 if (page)
1509 unlock_page(page);
1da177e4
LT
1510
1511 /*
1512 * We must evaluate after, since reads (unlike writes)
1b1dcc1b 1513 * are called without i_mutex protection against truncate
1da177e4
LT
1514 */
1515 nr = PAGE_CACHE_SIZE;
1516 i_size = i_size_read(inode);
1517 end_index = i_size >> PAGE_CACHE_SHIFT;
1518 if (index == end_index) {
1519 nr = i_size & ~PAGE_CACHE_MASK;
1520 if (nr <= offset) {
1521 if (page)
1522 page_cache_release(page);
1523 break;
1524 }
1525 }
1526 nr -= offset;
1527
1528 if (page) {
1529 /*
1530 * If users can be writing to this page using arbitrary
1531 * virtual addresses, take care about potential aliasing
1532 * before reading the page on the kernel side.
1533 */
1534 if (mapping_writably_mapped(mapping))
1535 flush_dcache_page(page);
1536 /*
1537 * Mark the page accessed if we read the beginning.
1538 */
1539 if (!offset)
1540 mark_page_accessed(page);
b5810039 1541 } else {
1da177e4 1542 page = ZERO_PAGE(0);
b5810039
NP
1543 page_cache_get(page);
1544 }
1da177e4
LT
1545
1546 /*
1547 * Ok, we have the page, and it's up-to-date, so
1548 * now we can copy it to user space...
1549 *
1550 * The actor routine returns how many bytes were actually used..
1551 * NOTE! This may not be the same as how much of a user buffer
1552 * we filled up (we may be padding etc), so we can only update
1553 * "pos" here (the actor routine has to update the user buffer
1554 * pointers and the remaining count).
1555 */
1556 ret = actor(desc, page, offset, nr);
1557 offset += ret;
1558 index += offset >> PAGE_CACHE_SHIFT;
1559 offset &= ~PAGE_CACHE_MASK;
1560
1561 page_cache_release(page);
1562 if (ret != nr || !desc->count)
1563 break;
1564
1565 cond_resched();
1566 }
1567
1568 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1569 file_accessed(filp);
1570}
1571
1572static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1573{
1574 read_descriptor_t desc;
1575
1576 if ((ssize_t) count < 0)
1577 return -EINVAL;
1578 if (!access_ok(VERIFY_WRITE, buf, count))
1579 return -EFAULT;
1580 if (!count)
1581 return 0;
1582
1583 desc.written = 0;
1584 desc.count = count;
1585 desc.arg.buf = buf;
1586 desc.error = 0;
1587
1588 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1589 if (desc.written)
1590 return desc.written;
1591 return desc.error;
1592}
1593
726c3342 1594static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 1595{
726c3342 1596 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1da177e4
LT
1597
1598 buf->f_type = TMPFS_MAGIC;
1599 buf->f_bsize = PAGE_CACHE_SIZE;
1600 buf->f_namelen = NAME_MAX;
0edd73b3
HD
1601 spin_lock(&sbinfo->stat_lock);
1602 if (sbinfo->max_blocks) {
1da177e4
LT
1603 buf->f_blocks = sbinfo->max_blocks;
1604 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
0edd73b3
HD
1605 }
1606 if (sbinfo->max_inodes) {
1da177e4
LT
1607 buf->f_files = sbinfo->max_inodes;
1608 buf->f_ffree = sbinfo->free_inodes;
1da177e4
LT
1609 }
1610 /* else leave those fields 0 like simple_statfs */
0edd73b3 1611 spin_unlock(&sbinfo->stat_lock);
1da177e4
LT
1612 return 0;
1613}
1614
1615/*
1616 * File creation. Allocate an inode, and we're done..
1617 */
1618static int
1619shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1620{
1621 struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
1622 int error = -ENOSPC;
1623
1624 if (inode) {
570bc1c2
SS
1625 error = security_inode_init_security(inode, dir, NULL, NULL,
1626 NULL);
1627 if (error) {
1628 if (error != -EOPNOTSUPP) {
1629 iput(inode);
1630 return error;
1631 }
39f0247d
AG
1632 }
1633 error = shmem_acl_init(inode, dir);
1634 if (error) {
1635 iput(inode);
1636 return error;
570bc1c2 1637 }
1da177e4
LT
1638 if (dir->i_mode & S_ISGID) {
1639 inode->i_gid = dir->i_gid;
1640 if (S_ISDIR(mode))
1641 inode->i_mode |= S_ISGID;
1642 }
1643 dir->i_size += BOGO_DIRENT_SIZE;
1644 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1645 d_instantiate(dentry, inode);
1646 dget(dentry); /* Extra count - pin the dentry in core */
1da177e4
LT
1647 }
1648 return error;
1649}
1650
1651static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1652{
1653 int error;
1654
1655 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1656 return error;
d8c76e6f 1657 inc_nlink(dir);
1da177e4
LT
1658 return 0;
1659}
1660
1661static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1662 struct nameidata *nd)
1663{
1664 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1665}
1666
1667/*
1668 * Link a file..
1669 */
1670static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1671{
1672 struct inode *inode = old_dentry->d_inode;
1673 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
1674
1675 /*
1676 * No ordinary (disk based) filesystem counts links as inodes;
1677 * but each new link needs a new dentry, pinning lowmem, and
1678 * tmpfs dentries cannot be pruned until they are unlinked.
1679 */
0edd73b3 1680 if (sbinfo->max_inodes) {
1da177e4
LT
1681 spin_lock(&sbinfo->stat_lock);
1682 if (!sbinfo->free_inodes) {
1683 spin_unlock(&sbinfo->stat_lock);
1684 return -ENOSPC;
1685 }
1686 sbinfo->free_inodes--;
1687 spin_unlock(&sbinfo->stat_lock);
1688 }
1689
1690 dir->i_size += BOGO_DIRENT_SIZE;
1691 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
d8c76e6f 1692 inc_nlink(inode);
1da177e4
LT
1693 atomic_inc(&inode->i_count); /* New dentry reference */
1694 dget(dentry); /* Extra pinning count for the created dentry */
1695 d_instantiate(dentry, inode);
1696 return 0;
1697}
1698
1699static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1700{
1701 struct inode *inode = dentry->d_inode;
1702
1703 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) {
1704 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
0edd73b3 1705 if (sbinfo->max_inodes) {
1da177e4
LT
1706 spin_lock(&sbinfo->stat_lock);
1707 sbinfo->free_inodes++;
1708 spin_unlock(&sbinfo->stat_lock);
1709 }
1710 }
1711
1712 dir->i_size -= BOGO_DIRENT_SIZE;
1713 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
9a53c3a7 1714 drop_nlink(inode);
1da177e4
LT
1715 dput(dentry); /* Undo the count from "create" - this does all the work */
1716 return 0;
1717}
1718
1719static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1720{
1721 if (!simple_empty(dentry))
1722 return -ENOTEMPTY;
1723
9a53c3a7
DH
1724 drop_nlink(dentry->d_inode);
1725 drop_nlink(dir);
1da177e4
LT
1726 return shmem_unlink(dir, dentry);
1727}
1728
1729/*
1730 * The VFS layer already does all the dentry stuff for rename,
1731 * we just have to decrement the usage count for the target if
1732 * it exists so that the VFS layer correctly free's it when it
1733 * gets overwritten.
1734 */
1735static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1736{
1737 struct inode *inode = old_dentry->d_inode;
1738 int they_are_dirs = S_ISDIR(inode->i_mode);
1739
1740 if (!simple_empty(new_dentry))
1741 return -ENOTEMPTY;
1742
1743 if (new_dentry->d_inode) {
1744 (void) shmem_unlink(new_dir, new_dentry);
1745 if (they_are_dirs)
9a53c3a7 1746 drop_nlink(old_dir);
1da177e4 1747 } else if (they_are_dirs) {
9a53c3a7 1748 drop_nlink(old_dir);
d8c76e6f 1749 inc_nlink(new_dir);
1da177e4
LT
1750 }
1751
1752 old_dir->i_size -= BOGO_DIRENT_SIZE;
1753 new_dir->i_size += BOGO_DIRENT_SIZE;
1754 old_dir->i_ctime = old_dir->i_mtime =
1755 new_dir->i_ctime = new_dir->i_mtime =
1756 inode->i_ctime = CURRENT_TIME;
1757 return 0;
1758}
1759
1760static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1761{
1762 int error;
1763 int len;
1764 struct inode *inode;
1765 struct page *page = NULL;
1766 char *kaddr;
1767 struct shmem_inode_info *info;
1768
1769 len = strlen(symname) + 1;
1770 if (len > PAGE_CACHE_SIZE)
1771 return -ENAMETOOLONG;
1772
1773 inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
1774 if (!inode)
1775 return -ENOSPC;
1776
570bc1c2
SS
1777 error = security_inode_init_security(inode, dir, NULL, NULL,
1778 NULL);
1779 if (error) {
1780 if (error != -EOPNOTSUPP) {
1781 iput(inode);
1782 return error;
1783 }
1784 error = 0;
1785 }
1786
1da177e4
LT
1787 info = SHMEM_I(inode);
1788 inode->i_size = len-1;
1789 if (len <= (char *)inode - (char *)info) {
1790 /* do it inline */
1791 memcpy(info, symname, len);
1792 inode->i_op = &shmem_symlink_inline_operations;
1793 } else {
1794 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1795 if (error) {
1796 iput(inode);
1797 return error;
1798 }
d3602444 1799 unlock_page(page);
1da177e4
LT
1800 inode->i_op = &shmem_symlink_inode_operations;
1801 kaddr = kmap_atomic(page, KM_USER0);
1802 memcpy(kaddr, symname, len);
1803 kunmap_atomic(kaddr, KM_USER0);
1804 set_page_dirty(page);
1805 page_cache_release(page);
1806 }
1807 if (dir->i_mode & S_ISGID)
1808 inode->i_gid = dir->i_gid;
1809 dir->i_size += BOGO_DIRENT_SIZE;
1810 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1811 d_instantiate(dentry, inode);
1812 dget(dentry);
1813 return 0;
1814}
1815
cc314eef 1816static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
1817{
1818 nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
cc314eef 1819 return NULL;
1da177e4
LT
1820}
1821
cc314eef 1822static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
1823{
1824 struct page *page = NULL;
1825 int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1826 nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
d3602444
HD
1827 if (page)
1828 unlock_page(page);
cc314eef 1829 return page;
1da177e4
LT
1830}
1831
cc314eef 1832static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4
LT
1833{
1834 if (!IS_ERR(nd_get_link(nd))) {
cc314eef 1835 struct page *page = cookie;
1da177e4
LT
1836 kunmap(page);
1837 mark_page_accessed(page);
1838 page_cache_release(page);
1da177e4
LT
1839 }
1840}
1841
92e1d5be 1842static const struct inode_operations shmem_symlink_inline_operations = {
1da177e4
LT
1843 .readlink = generic_readlink,
1844 .follow_link = shmem_follow_link_inline,
1da177e4
LT
1845};
1846
92e1d5be 1847static const struct inode_operations shmem_symlink_inode_operations = {
1da177e4
LT
1848 .truncate = shmem_truncate,
1849 .readlink = generic_readlink,
1850 .follow_link = shmem_follow_link,
1851 .put_link = shmem_put_link,
1da177e4
LT
1852};
1853
39f0247d
AG
1854#ifdef CONFIG_TMPFS_POSIX_ACL
1855/**
1856 * Superblocks without xattr inode operations will get security.* xattr
1857 * support from the VFS "for free". As soon as we have any other xattrs
1858 * like ACLs, we also need to implement the security.* handlers at
1859 * filesystem level, though.
1860 */
1861
1862static size_t shmem_xattr_security_list(struct inode *inode, char *list,
1863 size_t list_len, const char *name,
1864 size_t name_len)
1865{
1866 return security_inode_listsecurity(inode, list, list_len);
1867}
1868
1869static int shmem_xattr_security_get(struct inode *inode, const char *name,
1870 void *buffer, size_t size)
1871{
1872 if (strcmp(name, "") == 0)
1873 return -EINVAL;
1874 return security_inode_getsecurity(inode, name, buffer, size,
1875 -EOPNOTSUPP);
1876}
1877
1878static int shmem_xattr_security_set(struct inode *inode, const char *name,
1879 const void *value, size_t size, int flags)
1880{
1881 if (strcmp(name, "") == 0)
1882 return -EINVAL;
1883 return security_inode_setsecurity(inode, name, value, size, flags);
1884}
1885
1f370a23 1886static struct xattr_handler shmem_xattr_security_handler = {
39f0247d
AG
1887 .prefix = XATTR_SECURITY_PREFIX,
1888 .list = shmem_xattr_security_list,
1889 .get = shmem_xattr_security_get,
1890 .set = shmem_xattr_security_set,
1891};
1892
1893static struct xattr_handler *shmem_xattr_handlers[] = {
1894 &shmem_xattr_acl_access_handler,
1895 &shmem_xattr_acl_default_handler,
1896 &shmem_xattr_security_handler,
1897 NULL
1898};
1899#endif
1900
91828a40
DG
1901static struct dentry *shmem_get_parent(struct dentry *child)
1902{
1903 return ERR_PTR(-ESTALE);
1904}
1905
1906static int shmem_match(struct inode *ino, void *vfh)
1907{
1908 __u32 *fh = vfh;
1909 __u64 inum = fh[2];
1910 inum = (inum << 32) | fh[1];
1911 return ino->i_ino == inum && fh[0] == ino->i_generation;
1912}
1913
480b116c
CH
1914static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
1915 struct fid *fid, int fh_len, int fh_type)
91828a40 1916{
91828a40 1917 struct inode *inode;
480b116c
CH
1918 struct dentry *dentry = NULL;
1919 u64 inum = fid->raw[2];
1920 inum = (inum << 32) | fid->raw[1];
1921
1922 if (fh_len < 3)
1923 return NULL;
91828a40 1924
480b116c
CH
1925 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
1926 shmem_match, fid->raw);
91828a40 1927 if (inode) {
480b116c 1928 dentry = d_find_alias(inode);
91828a40
DG
1929 iput(inode);
1930 }
1931
480b116c 1932 return dentry;
91828a40
DG
1933}
1934
1935static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
1936 int connectable)
1937{
1938 struct inode *inode = dentry->d_inode;
1939
1940 if (*len < 3)
1941 return 255;
1942
1943 if (hlist_unhashed(&inode->i_hash)) {
1944 /* Unfortunately insert_inode_hash is not idempotent,
1945 * so as we hash inodes here rather than at creation
1946 * time, we need a lock to ensure we only try
1947 * to do it once
1948 */
1949 static DEFINE_SPINLOCK(lock);
1950 spin_lock(&lock);
1951 if (hlist_unhashed(&inode->i_hash))
1952 __insert_inode_hash(inode,
1953 inode->i_ino + inode->i_generation);
1954 spin_unlock(&lock);
1955 }
1956
1957 fh[0] = inode->i_generation;
1958 fh[1] = inode->i_ino;
1959 fh[2] = ((__u64)inode->i_ino) >> 32;
1960
1961 *len = 3;
1962 return 1;
1963}
1964
39655164 1965static const struct export_operations shmem_export_ops = {
91828a40 1966 .get_parent = shmem_get_parent,
91828a40 1967 .encode_fh = shmem_encode_fh,
480b116c 1968 .fh_to_dentry = shmem_fh_to_dentry,
91828a40
DG
1969};
1970
7339ff83
RH
1971static int shmem_parse_options(char *options, int *mode, uid_t *uid,
1972 gid_t *gid, unsigned long *blocks, unsigned long *inodes,
1973 int *policy, nodemask_t *policy_nodes)
1da177e4
LT
1974{
1975 char *this_char, *value, *rest;
1976
b00dc3ad
HD
1977 while (options != NULL) {
1978 this_char = options;
1979 for (;;) {
1980 /*
1981 * NUL-terminate this option: unfortunately,
1982 * mount options form a comma-separated list,
1983 * but mpol's nodelist may also contain commas.
1984 */
1985 options = strchr(options, ',');
1986 if (options == NULL)
1987 break;
1988 options++;
1989 if (!isdigit(*options)) {
1990 options[-1] = '\0';
1991 break;
1992 }
1993 }
1da177e4
LT
1994 if (!*this_char)
1995 continue;
1996 if ((value = strchr(this_char,'=')) != NULL) {
1997 *value++ = 0;
1998 } else {
1999 printk(KERN_ERR
2000 "tmpfs: No value for mount option '%s'\n",
2001 this_char);
2002 return 1;
2003 }
2004
2005 if (!strcmp(this_char,"size")) {
2006 unsigned long long size;
2007 size = memparse(value,&rest);
2008 if (*rest == '%') {
2009 size <<= PAGE_SHIFT;
2010 size *= totalram_pages;
2011 do_div(size, 100);
2012 rest++;
2013 }
2014 if (*rest)
2015 goto bad_val;
2016 *blocks = size >> PAGE_CACHE_SHIFT;
2017 } else if (!strcmp(this_char,"nr_blocks")) {
2018 *blocks = memparse(value,&rest);
2019 if (*rest)
2020 goto bad_val;
2021 } else if (!strcmp(this_char,"nr_inodes")) {
2022 *inodes = memparse(value,&rest);
2023 if (*rest)
2024 goto bad_val;
2025 } else if (!strcmp(this_char,"mode")) {
2026 if (!mode)
2027 continue;
2028 *mode = simple_strtoul(value,&rest,8);
2029 if (*rest)
2030 goto bad_val;
2031 } else if (!strcmp(this_char,"uid")) {
2032 if (!uid)
2033 continue;
2034 *uid = simple_strtoul(value,&rest,0);
2035 if (*rest)
2036 goto bad_val;
2037 } else if (!strcmp(this_char,"gid")) {
2038 if (!gid)
2039 continue;
2040 *gid = simple_strtoul(value,&rest,0);
2041 if (*rest)
2042 goto bad_val;
7339ff83 2043 } else if (!strcmp(this_char,"mpol")) {
b00dc3ad 2044 if (shmem_parse_mpol(value,policy,policy_nodes))
7339ff83 2045 goto bad_val;
1da177e4
LT
2046 } else {
2047 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2048 this_char);
2049 return 1;
2050 }
2051 }
2052 return 0;
2053
2054bad_val:
2055 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2056 value, this_char);
2057 return 1;
2058
2059}
2060
2061static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2062{
2063 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
0edd73b3
HD
2064 unsigned long max_blocks = sbinfo->max_blocks;
2065 unsigned long max_inodes = sbinfo->max_inodes;
7339ff83
RH
2066 int policy = sbinfo->policy;
2067 nodemask_t policy_nodes = sbinfo->policy_nodes;
0edd73b3
HD
2068 unsigned long blocks;
2069 unsigned long inodes;
2070 int error = -EINVAL;
2071
7339ff83
RH
2072 if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks,
2073 &max_inodes, &policy, &policy_nodes))
0edd73b3 2074 return error;
1da177e4 2075
0edd73b3
HD
2076 spin_lock(&sbinfo->stat_lock);
2077 blocks = sbinfo->max_blocks - sbinfo->free_blocks;
2078 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
2079 if (max_blocks < blocks)
2080 goto out;
2081 if (max_inodes < inodes)
2082 goto out;
2083 /*
2084 * Those tests also disallow limited->unlimited while any are in
2085 * use, so i_blocks will always be zero when max_blocks is zero;
2086 * but we must separately disallow unlimited->limited, because
2087 * in that case we have no record of how much is already in use.
2088 */
2089 if (max_blocks && !sbinfo->max_blocks)
2090 goto out;
2091 if (max_inodes && !sbinfo->max_inodes)
2092 goto out;
2093
2094 error = 0;
2095 sbinfo->max_blocks = max_blocks;
2096 sbinfo->free_blocks = max_blocks - blocks;
2097 sbinfo->max_inodes = max_inodes;
2098 sbinfo->free_inodes = max_inodes - inodes;
7339ff83
RH
2099 sbinfo->policy = policy;
2100 sbinfo->policy_nodes = policy_nodes;
0edd73b3
HD
2101out:
2102 spin_unlock(&sbinfo->stat_lock);
2103 return error;
1da177e4
LT
2104}
2105#endif
2106
2107static void shmem_put_super(struct super_block *sb)
2108{
2109 kfree(sb->s_fs_info);
2110 sb->s_fs_info = NULL;
2111}
2112
1da177e4
LT
2113static int shmem_fill_super(struct super_block *sb,
2114 void *data, int silent)
2115{
2116 struct inode *inode;
2117 struct dentry *root;
2118 int mode = S_IRWXUGO | S_ISVTX;
2119 uid_t uid = current->fsuid;
2120 gid_t gid = current->fsgid;
2121 int err = -ENOMEM;
0edd73b3 2122 struct shmem_sb_info *sbinfo;
1da177e4
LT
2123 unsigned long blocks = 0;
2124 unsigned long inodes = 0;
7339ff83 2125 int policy = MPOL_DEFAULT;
37b07e41 2126 nodemask_t policy_nodes = node_states[N_HIGH_MEMORY];
1da177e4 2127
0edd73b3 2128#ifdef CONFIG_TMPFS
1da177e4
LT
2129 /*
2130 * Per default we only allow half of the physical ram per
2131 * tmpfs instance, limiting inodes to one per page of lowmem;
2132 * but the internal instance is left unlimited.
2133 */
2134 if (!(sb->s_flags & MS_NOUSER)) {
2135 blocks = totalram_pages / 2;
2136 inodes = totalram_pages - totalhigh_pages;
2137 if (inodes > blocks)
2138 inodes = blocks;
7339ff83
RH
2139 if (shmem_parse_options(data, &mode, &uid, &gid, &blocks,
2140 &inodes, &policy, &policy_nodes))
1da177e4
LT
2141 return -EINVAL;
2142 }
91828a40 2143 sb->s_export_op = &shmem_export_ops;
1da177e4
LT
2144#else
2145 sb->s_flags |= MS_NOUSER;
2146#endif
2147
0edd73b3
HD
2148 /* Round up to L1_CACHE_BYTES to resist false sharing */
2149 sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
2150 L1_CACHE_BYTES), GFP_KERNEL);
2151 if (!sbinfo)
2152 return -ENOMEM;
2153
2154 spin_lock_init(&sbinfo->stat_lock);
2155 sbinfo->max_blocks = blocks;
2156 sbinfo->free_blocks = blocks;
2157 sbinfo->max_inodes = inodes;
2158 sbinfo->free_inodes = inodes;
7339ff83
RH
2159 sbinfo->policy = policy;
2160 sbinfo->policy_nodes = policy_nodes;
0edd73b3
HD
2161
2162 sb->s_fs_info = sbinfo;
1da177e4
LT
2163 sb->s_maxbytes = SHMEM_MAX_BYTES;
2164 sb->s_blocksize = PAGE_CACHE_SIZE;
2165 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2166 sb->s_magic = TMPFS_MAGIC;
2167 sb->s_op = &shmem_ops;
cfd95a9c 2168 sb->s_time_gran = 1;
39f0247d
AG
2169#ifdef CONFIG_TMPFS_POSIX_ACL
2170 sb->s_xattr = shmem_xattr_handlers;
2171 sb->s_flags |= MS_POSIXACL;
2172#endif
0edd73b3 2173
1da177e4
LT
2174 inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
2175 if (!inode)
2176 goto failed;
2177 inode->i_uid = uid;
2178 inode->i_gid = gid;
2179 root = d_alloc_root(inode);
2180 if (!root)
2181 goto failed_iput;
2182 sb->s_root = root;
2183 return 0;
2184
2185failed_iput:
2186 iput(inode);
2187failed:
2188 shmem_put_super(sb);
2189 return err;
2190}
2191
fcc234f8 2192static struct kmem_cache *shmem_inode_cachep;
1da177e4
LT
2193
2194static struct inode *shmem_alloc_inode(struct super_block *sb)
2195{
2196 struct shmem_inode_info *p;
e94b1766 2197 p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
1da177e4
LT
2198 if (!p)
2199 return NULL;
2200 return &p->vfs_inode;
2201}
2202
2203static void shmem_destroy_inode(struct inode *inode)
2204{
2205 if ((inode->i_mode & S_IFMT) == S_IFREG) {
2206 /* only struct inode is valid if it's an inline symlink */
2207 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2208 }
39f0247d 2209 shmem_acl_destroy_inode(inode);
1da177e4
LT
2210 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2211}
2212
4ba9b9d0 2213static void init_once(struct kmem_cache *cachep, void *foo)
1da177e4
LT
2214{
2215 struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
2216
a35afb83 2217 inode_init_once(&p->vfs_inode);
39f0247d 2218#ifdef CONFIG_TMPFS_POSIX_ACL
a35afb83
CL
2219 p->i_acl = NULL;
2220 p->i_default_acl = NULL;
39f0247d 2221#endif
1da177e4
LT
2222}
2223
2224static int init_inodecache(void)
2225{
2226 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2227 sizeof(struct shmem_inode_info),
040b5c6f 2228 0, SLAB_PANIC, init_once);
1da177e4
LT
2229 return 0;
2230}
2231
2232static void destroy_inodecache(void)
2233{
1a1d92c1 2234 kmem_cache_destroy(shmem_inode_cachep);
1da177e4
LT
2235}
2236
f5e54d6e 2237static const struct address_space_operations shmem_aops = {
1da177e4 2238 .writepage = shmem_writepage,
76719325 2239 .set_page_dirty = __set_page_dirty_no_writeback,
1da177e4 2240#ifdef CONFIG_TMPFS
ae976416 2241 .readpage = shmem_readpage,
800d15a5
NP
2242 .write_begin = shmem_write_begin,
2243 .write_end = shmem_write_end,
1da177e4 2244#endif
304dbdb7 2245 .migratepage = migrate_page,
1da177e4
LT
2246};
2247
15ad7cdc 2248static const struct file_operations shmem_file_operations = {
1da177e4
LT
2249 .mmap = shmem_mmap,
2250#ifdef CONFIG_TMPFS
2251 .llseek = generic_file_llseek,
2252 .read = shmem_file_read,
5402b976
HD
2253 .write = do_sync_write,
2254 .aio_write = generic_file_aio_write,
1da177e4 2255 .fsync = simple_sync_file,
ae976416
HD
2256 .splice_read = generic_file_splice_read,
2257 .splice_write = generic_file_splice_write,
1da177e4
LT
2258#endif
2259};
2260
92e1d5be 2261static const struct inode_operations shmem_inode_operations = {
1da177e4
LT
2262 .truncate = shmem_truncate,
2263 .setattr = shmem_notify_change,
f6b3ec23 2264 .truncate_range = shmem_truncate_range,
39f0247d
AG
2265#ifdef CONFIG_TMPFS_POSIX_ACL
2266 .setxattr = generic_setxattr,
2267 .getxattr = generic_getxattr,
2268 .listxattr = generic_listxattr,
2269 .removexattr = generic_removexattr,
2270 .permission = shmem_permission,
2271#endif
2272
1da177e4
LT
2273};
2274
92e1d5be 2275static const struct inode_operations shmem_dir_inode_operations = {
1da177e4
LT
2276#ifdef CONFIG_TMPFS
2277 .create = shmem_create,
2278 .lookup = simple_lookup,
2279 .link = shmem_link,
2280 .unlink = shmem_unlink,
2281 .symlink = shmem_symlink,
2282 .mkdir = shmem_mkdir,
2283 .rmdir = shmem_rmdir,
2284 .mknod = shmem_mknod,
2285 .rename = shmem_rename,
1da177e4 2286#endif
39f0247d
AG
2287#ifdef CONFIG_TMPFS_POSIX_ACL
2288 .setattr = shmem_notify_change,
2289 .setxattr = generic_setxattr,
2290 .getxattr = generic_getxattr,
2291 .listxattr = generic_listxattr,
2292 .removexattr = generic_removexattr,
2293 .permission = shmem_permission,
2294#endif
2295};
2296
92e1d5be 2297static const struct inode_operations shmem_special_inode_operations = {
39f0247d
AG
2298#ifdef CONFIG_TMPFS_POSIX_ACL
2299 .setattr = shmem_notify_change,
2300 .setxattr = generic_setxattr,
2301 .getxattr = generic_getxattr,
2302 .listxattr = generic_listxattr,
2303 .removexattr = generic_removexattr,
2304 .permission = shmem_permission,
2305#endif
1da177e4
LT
2306};
2307
759b9775 2308static const struct super_operations shmem_ops = {
1da177e4
LT
2309 .alloc_inode = shmem_alloc_inode,
2310 .destroy_inode = shmem_destroy_inode,
2311#ifdef CONFIG_TMPFS
2312 .statfs = shmem_statfs,
2313 .remount_fs = shmem_remount_fs,
2314#endif
2315 .delete_inode = shmem_delete_inode,
2316 .drop_inode = generic_delete_inode,
2317 .put_super = shmem_put_super,
2318};
2319
2320static struct vm_operations_struct shmem_vm_ops = {
54cb8821 2321 .fault = shmem_fault,
1da177e4
LT
2322#ifdef CONFIG_NUMA
2323 .set_policy = shmem_set_policy,
2324 .get_policy = shmem_get_policy,
2325#endif
2326};
2327
2328
454e2398
DH
2329static int shmem_get_sb(struct file_system_type *fs_type,
2330 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1da177e4 2331{
454e2398 2332 return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt);
1da177e4
LT
2333}
2334
2335static struct file_system_type tmpfs_fs_type = {
2336 .owner = THIS_MODULE,
2337 .name = "tmpfs",
2338 .get_sb = shmem_get_sb,
2339 .kill_sb = kill_litter_super,
2340};
2341static struct vfsmount *shm_mnt;
2342
2343static int __init init_tmpfs(void)
2344{
2345 int error;
2346
e0bf68dd
PZ
2347 error = bdi_init(&shmem_backing_dev_info);
2348 if (error)
2349 goto out4;
2350
1da177e4
LT
2351 error = init_inodecache();
2352 if (error)
2353 goto out3;
2354
2355 error = register_filesystem(&tmpfs_fs_type);
2356 if (error) {
2357 printk(KERN_ERR "Could not register tmpfs\n");
2358 goto out2;
2359 }
95dc112a 2360
1f5ce9e9 2361 shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
1da177e4
LT
2362 tmpfs_fs_type.name, NULL);
2363 if (IS_ERR(shm_mnt)) {
2364 error = PTR_ERR(shm_mnt);
2365 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2366 goto out1;
2367 }
2368 return 0;
2369
2370out1:
2371 unregister_filesystem(&tmpfs_fs_type);
2372out2:
2373 destroy_inodecache();
2374out3:
e0bf68dd
PZ
2375 bdi_destroy(&shmem_backing_dev_info);
2376out4:
1da177e4
LT
2377 shm_mnt = ERR_PTR(error);
2378 return error;
2379}
2380module_init(init_tmpfs)
2381
2382/*
2383 * shmem_file_setup - get an unlinked file living in tmpfs
2384 *
2385 * @name: name for dentry (to be seen in /proc/<pid>/maps
2386 * @size: size to be set for the file
2387 *
2388 */
2389struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
2390{
2391 int error;
2392 struct file *file;
2393 struct inode *inode;
2394 struct dentry *dentry, *root;
2395 struct qstr this;
2396
2397 if (IS_ERR(shm_mnt))
2398 return (void *)shm_mnt;
2399
2400 if (size < 0 || size > SHMEM_MAX_BYTES)
2401 return ERR_PTR(-EINVAL);
2402
2403 if (shmem_acct_size(flags, size))
2404 return ERR_PTR(-ENOMEM);
2405
2406 error = -ENOMEM;
2407 this.name = name;
2408 this.len = strlen(name);
2409 this.hash = 0; /* will go */
2410 root = shm_mnt->mnt_root;
2411 dentry = d_alloc(root, &this);
2412 if (!dentry)
2413 goto put_memory;
2414
2415 error = -ENFILE;
2416 file = get_empty_filp();
2417 if (!file)
2418 goto put_dentry;
2419
2420 error = -ENOSPC;
2421 inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
2422 if (!inode)
2423 goto close_file;
2424
2425 SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
2426 d_instantiate(dentry, inode);
2427 inode->i_size = size;
2428 inode->i_nlink = 0; /* It is unlinked */
ce8d2cdf
DH
2429 init_file(file, shm_mnt, dentry, FMODE_WRITE | FMODE_READ,
2430 &shmem_file_operations);
1da177e4
LT
2431 return file;
2432
2433close_file:
2434 put_filp(file);
2435put_dentry:
2436 dput(dentry);
2437put_memory:
2438 shmem_unacct_size(flags, size);
2439 return ERR_PTR(error);
2440}
2441
2442/*
2443 * shmem_zero_setup - setup a shared anonymous mapping
2444 *
2445 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2446 */
2447int shmem_zero_setup(struct vm_area_struct *vma)
2448{
2449 struct file *file;
2450 loff_t size = vma->vm_end - vma->vm_start;
2451
2452 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2453 if (IS_ERR(file))
2454 return PTR_ERR(file);
2455
2456 if (vma->vm_file)
2457 fput(vma->vm_file);
2458 vma->vm_file = file;
2459 vma->vm_ops = &shmem_vm_ops;
2460 return 0;
2461}