nc-p

Untitled

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