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