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