shmem.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  1. /*
  2. * Resizable virtual memory filesystem for Linux.
  3. *
  4. * Copyright (C) 2000 Linus Torvalds.
  5. * 2000 Transmeta Corp.
  6. * 2000-2001 Christoph Rohland
  7. * 2000-2001 SAP AG
  8. * 2002 Red Hat Inc.
  9. * Copyright (C) 2002-2005 Hugh Dickins.
  10. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  11. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  12. *
  13. * Extended attribute support for tmpfs:
  14. * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  15. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  16. *
  17. * tiny-shmem:
  18. * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
  19. *
  20. * This file is released under the GPL.
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/init.h>
  24. #include <linux/vfs.h>
  25. #include <linux/mount.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/file.h>
  28. #include <linux/mm.h>
  29. #include <linux/module.h>
  30. #include <linux/swap.h>
  31. static struct vfsmount *shm_mnt;
  32. #ifdef CONFIG_SHMEM
  33. /*
  34. * This virtual memory filesystem is heavily based on the ramfs. It
  35. * extends ramfs by the ability to use swap and honor resource limits
  36. * which makes it a completely usable filesystem.
  37. */
  38. #include <linux/xattr.h>
  39. #include <linux/exportfs.h>
  40. #include <linux/posix_acl.h>
  41. #include <linux/generic_acl.h>
  42. #include <linux/mman.h>
  43. #include <linux/string.h>
  44. #include <linux/slab.h>
  45. #include <linux/backing-dev.h>
  46. #include <linux/shmem_fs.h>
  47. #include <linux/writeback.h>
  48. #include <linux/blkdev.h>
  49. #include <linux/percpu_counter.h>
  50. #include <linux/splice.h>
  51. #include <linux/security.h>
  52. #include <linux/swapops.h>
  53. #include <linux/mempolicy.h>
  54. #include <linux/namei.h>
  55. #include <linux/ctype.h>
  56. #include <linux/migrate.h>
  57. #include <linux/highmem.h>
  58. #include <linux/seq_file.h>
  59. #include <linux/magic.h>
  60. #include <asm/uaccess.h>
  61. #include <asm/pgtable.h>
  62. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  63. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  64. /* Pretend that each entry is of this size in directory's i_size */
  65. #define BOGO_DIRENT_SIZE 20
  66. struct shmem_xattr {
  67. struct list_head list; /* anchored by shmem_inode_info->xattr_list */
  68. char *name; /* xattr name */
  69. size_t size;
  70. char value[0];
  71. };
  72. /* Flag allocation requirements to shmem_getpage */
  73. enum sgp_type {
  74. SGP_READ, /* don't exceed i_size, don't allocate page */
  75. SGP_CACHE, /* don't exceed i_size, may allocate page */
  76. SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
  77. SGP_WRITE, /* may exceed i_size, may allocate page */
  78. };
  79. #ifdef CONFIG_TMPFS
  80. static unsigned long shmem_default_max_blocks(void)
  81. {
  82. return totalram_pages / 2;
  83. }
  84. static unsigned long shmem_default_max_inodes(void)
  85. {
  86. return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
  87. }
  88. #endif
  89. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  90. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
  91. static inline int shmem_getpage(struct inode *inode, pgoff_t index,
  92. struct page **pagep, enum sgp_type sgp, int *fault_type)
  93. {
  94. return shmem_getpage_gfp(inode, index, pagep, sgp,
  95. mapping_gfp_mask(inode->i_mapping), fault_type);
  96. }
  97. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  98. {
  99. return sb->s_fs_info;
  100. }
  101. /*
  102. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  103. * for shared memory and for shared anonymous (/dev/zero) mappings
  104. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  105. * consistent with the pre-accounting of private mappings ...
  106. */
  107. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  108. {
  109. return (flags & VM_NORESERVE) ?
  110. 0 : security_vm_enough_memory_kern(VM_ACCT(size));
  111. }
  112. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  113. {
  114. if (!(flags & VM_NORESERVE))
  115. vm_unacct_memory(VM_ACCT(size));
  116. }
  117. /*
  118. * ... whereas tmpfs objects are accounted incrementally as
  119. * pages are allocated, in order to allow huge sparse files.
  120. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  121. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  122. */
  123. static inline int shmem_acct_block(unsigned long flags)
  124. {
  125. return (flags & VM_NORESERVE) ?
  126. security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
  127. }
  128. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  129. {
  130. if (flags & VM_NORESERVE)
  131. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  132. }
  133. static const struct super_operations shmem_ops;
  134. static const struct address_space_operations shmem_aops;
  135. static const struct file_operations shmem_file_operations;
  136. static const struct inode_operations shmem_inode_operations;
  137. static const struct inode_operations shmem_dir_inode_operations;
  138. static const struct inode_operations shmem_special_inode_operations;
  139. static const struct vm_operations_struct shmem_vm_ops;
  140. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  141. .ra_pages = 0, /* No readahead */
  142. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
  143. };
  144. static LIST_HEAD(shmem_swaplist);
  145. static DEFINE_MUTEX(shmem_swaplist_mutex);
  146. static void shmem_free_blocks(struct inode *inode, long pages)
  147. {
  148. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  149. if (sbinfo->max_blocks) {
  150. percpu_counter_add(&sbinfo->used_blocks, -pages);
  151. inode->i_blocks -= pages*BLOCKS_PER_PAGE;
  152. }
  153. }
  154. static int shmem_reserve_inode(struct super_block *sb)
  155. {
  156. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  157. if (sbinfo->max_inodes) {
  158. spin_lock(&sbinfo->stat_lock);
  159. if (!sbinfo->free_inodes) {
  160. spin_unlock(&sbinfo->stat_lock);
  161. return -ENOSPC;
  162. }
  163. sbinfo->free_inodes--;
  164. spin_unlock(&sbinfo->stat_lock);
  165. }
  166. return 0;
  167. }
  168. static void shmem_free_inode(struct super_block *sb)
  169. {
  170. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  171. if (sbinfo->max_inodes) {
  172. spin_lock(&sbinfo->stat_lock);
  173. sbinfo->free_inodes++;
  174. spin_unlock(&sbinfo->stat_lock);
  175. }
  176. }
  177. /**
  178. * shmem_recalc_inode - recalculate the block usage of an inode
  179. * @inode: inode to recalc
  180. *
  181. * We have to calculate the free blocks since the mm can drop
  182. * undirtied hole pages behind our back.
  183. *
  184. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  185. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  186. *
  187. * It has to be called with the spinlock held.
  188. */
  189. static void shmem_recalc_inode(struct inode *inode)
  190. {
  191. struct shmem_inode_info *info = SHMEM_I(inode);
  192. long freed;
  193. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  194. if (freed > 0) {
  195. info->alloced -= freed;
  196. shmem_unacct_blocks(info->flags, freed);
  197. shmem_free_blocks(inode, freed);
  198. }
  199. }
  200. static void shmem_put_swap(struct shmem_inode_info *info, pgoff_t index,
  201. swp_entry_t swap)
  202. {
  203. if (index < SHMEM_NR_DIRECT)
  204. info->i_direct[index] = swap;
  205. }
  206. static swp_entry_t shmem_get_swap(struct shmem_inode_info *info, pgoff_t index)
  207. {
  208. return (index < SHMEM_NR_DIRECT) ?
  209. info->i_direct[index] : (swp_entry_t){0};
  210. }
  211. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  212. {
  213. struct address_space *mapping = inode->i_mapping;
  214. struct shmem_inode_info *info = SHMEM_I(inode);
  215. pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  216. pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
  217. pgoff_t index;
  218. swp_entry_t swap;
  219. truncate_inode_pages_range(mapping, lstart, lend);
  220. if (end > SHMEM_NR_DIRECT)
  221. end = SHMEM_NR_DIRECT;
  222. spin_lock(&info->lock);
  223. for (index = start; index < end; index++) {
  224. swap = shmem_get_swap(info, index);
  225. if (swap.val) {
  226. free_swap_and_cache(swap);
  227. shmem_put_swap(info, index, (swp_entry_t){0});
  228. info->swapped--;
  229. }
  230. }
  231. if (mapping->nrpages) {
  232. spin_unlock(&info->lock);
  233. /*
  234. * A page may have meanwhile sneaked in from swap.
  235. */
  236. truncate_inode_pages_range(mapping, lstart, lend);
  237. spin_lock(&info->lock);
  238. }
  239. shmem_recalc_inode(inode);
  240. spin_unlock(&info->lock);
  241. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  242. }
  243. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  244. static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
  245. {
  246. struct inode *inode = dentry->d_inode;
  247. int error;
  248. error = inode_change_ok(inode, attr);
  249. if (error)
  250. return error;
  251. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  252. loff_t oldsize = inode->i_size;
  253. loff_t newsize = attr->ia_size;
  254. struct page *page = NULL;
  255. if (newsize < oldsize) {
  256. /*
  257. * If truncating down to a partial page, then
  258. * if that page is already allocated, hold it
  259. * in memory until the truncation is over, so
  260. * truncate_partial_page cannot miss it were
  261. * it assigned to swap.
  262. */
  263. if (newsize & (PAGE_CACHE_SIZE-1)) {
  264. (void) shmem_getpage(inode,
  265. newsize >> PAGE_CACHE_SHIFT,
  266. &page, SGP_READ, NULL);
  267. if (page)
  268. unlock_page(page);
  269. }
  270. }
  271. if (newsize != oldsize) {
  272. i_size_write(inode, newsize);
  273. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  274. }
  275. if (newsize < oldsize) {
  276. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  277. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  278. shmem_truncate_range(inode, newsize, (loff_t)-1);
  279. /* unmap again to remove racily COWed private pages */
  280. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  281. }
  282. if (page)
  283. page_cache_release(page);
  284. }
  285. setattr_copy(inode, attr);
  286. #ifdef CONFIG_TMPFS_POSIX_ACL
  287. if (attr->ia_valid & ATTR_MODE)
  288. error = generic_acl_chmod(inode);
  289. #endif
  290. return error;
  291. }
  292. static void shmem_evict_inode(struct inode *inode)
  293. {
  294. struct shmem_inode_info *info = SHMEM_I(inode);
  295. struct shmem_xattr *xattr, *nxattr;
  296. if (inode->i_mapping->a_ops == &shmem_aops) {
  297. shmem_unacct_size(info->flags, inode->i_size);
  298. inode->i_size = 0;
  299. shmem_truncate_range(inode, 0, (loff_t)-1);
  300. if (!list_empty(&info->swaplist)) {
  301. mutex_lock(&shmem_swaplist_mutex);
  302. list_del_init(&info->swaplist);
  303. mutex_unlock(&shmem_swaplist_mutex);
  304. }
  305. }
  306. list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
  307. kfree(xattr->name);
  308. kfree(xattr);
  309. }
  310. BUG_ON(inode->i_blocks);
  311. shmem_free_inode(inode->i_sb);
  312. end_writeback(inode);
  313. }
  314. static int shmem_unuse_inode(struct shmem_inode_info *info,
  315. swp_entry_t swap, struct page *page)
  316. {
  317. struct address_space *mapping = info->vfs_inode.i_mapping;
  318. pgoff_t index;
  319. int error;
  320. for (index = 0; index < SHMEM_NR_DIRECT; index++)
  321. if (shmem_get_swap(info, index).val == swap.val)
  322. goto found;
  323. return 0;
  324. found:
  325. spin_lock(&info->lock);
  326. if (shmem_get_swap(info, index).val != swap.val) {
  327. spin_unlock(&info->lock);
  328. return 0;
  329. }
  330. /*
  331. * Move _head_ to start search for next from here.
  332. * But be careful: shmem_evict_inode checks list_empty without taking
  333. * mutex, and there's an instant in list_move_tail when info->swaplist
  334. * would appear empty, if it were the only one on shmem_swaplist.
  335. */
  336. if (shmem_swaplist.next != &info->swaplist)
  337. list_move_tail(&shmem_swaplist, &info->swaplist);
  338. /*
  339. * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
  340. * but also to hold up shmem_evict_inode(): so inode cannot be freed
  341. * beneath us (pagelock doesn't help until the page is in pagecache).
  342. */
  343. error = add_to_page_cache_locked(page, mapping, index, GFP_NOWAIT);
  344. /* which does mem_cgroup_uncharge_cache_page on error */
  345. if (error != -ENOMEM) {
  346. delete_from_swap_cache(page);
  347. set_page_dirty(page);
  348. shmem_put_swap(info, index, (swp_entry_t){0});
  349. info->swapped--;
  350. swap_free(swap);
  351. error = 1; /* not an error, but entry was found */
  352. }
  353. spin_unlock(&info->lock);
  354. return error;
  355. }
  356. /*
  357. * shmem_unuse() search for an eventually swapped out shmem page.
  358. */
  359. int shmem_unuse(swp_entry_t swap, struct page *page)
  360. {
  361. struct list_head *this, *next;
  362. struct shmem_inode_info *info;
  363. int found = 0;
  364. int error;
  365. /*
  366. * Charge page using GFP_KERNEL while we can wait, before taking
  367. * the shmem_swaplist_mutex which might hold up shmem_writepage().
  368. * Charged back to the user (not to caller) when swap account is used.
  369. * add_to_page_cache() will be called with GFP_NOWAIT.
  370. */
  371. error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
  372. if (error)
  373. goto out;
  374. /*
  375. * Try to preload while we can wait, to not make a habit of
  376. * draining atomic reserves; but don't latch on to this cpu,
  377. * it's okay if sometimes we get rescheduled after this.
  378. */
  379. error = radix_tree_preload(GFP_KERNEL);
  380. if (error)
  381. goto uncharge;
  382. radix_tree_preload_end();
  383. mutex_lock(&shmem_swaplist_mutex);
  384. list_for_each_safe(this, next, &shmem_swaplist) {
  385. info = list_entry(this, struct shmem_inode_info, swaplist);
  386. if (!info->swapped) {
  387. spin_lock(&info->lock);
  388. if (!info->swapped)
  389. list_del_init(&info->swaplist);
  390. spin_unlock(&info->lock);
  391. }
  392. if (info->swapped)
  393. found = shmem_unuse_inode(info, swap, page);
  394. cond_resched();
  395. if (found)
  396. break;
  397. }
  398. mutex_unlock(&shmem_swaplist_mutex);
  399. uncharge:
  400. if (!found)
  401. mem_cgroup_uncharge_cache_page(page);
  402. if (found < 0)
  403. error = found;
  404. out:
  405. unlock_page(page);
  406. page_cache_release(page);
  407. return error;
  408. }
  409. /*
  410. * Move the page from the page cache to the swap cache.
  411. */
  412. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  413. {
  414. struct shmem_inode_info *info;
  415. swp_entry_t swap, oswap;
  416. struct address_space *mapping;
  417. pgoff_t index;
  418. struct inode *inode;
  419. BUG_ON(!PageLocked(page));
  420. mapping = page->mapping;
  421. index = page->index;
  422. inode = mapping->host;
  423. info = SHMEM_I(inode);
  424. if (info->flags & VM_LOCKED)
  425. goto redirty;
  426. if (!total_swap_pages)
  427. goto redirty;
  428. /*
  429. * shmem_backing_dev_info's capabilities prevent regular writeback or
  430. * sync from ever calling shmem_writepage; but a stacking filesystem
  431. * might use ->writepage of its underlying filesystem, in which case
  432. * tmpfs should write out to swap only in response to memory pressure,
  433. * and not for the writeback threads or sync.
  434. */
  435. if (!wbc->for_reclaim) {
  436. WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
  437. goto redirty;
  438. }
  439. /*
  440. * Just for this patch, we have a toy implementation,
  441. * which can swap out only the first SHMEM_NR_DIRECT pages:
  442. * for simple demonstration of where we need to think about swap.
  443. */
  444. if (index >= SHMEM_NR_DIRECT)
  445. goto redirty;
  446. swap = get_swap_page();
  447. if (!swap.val)
  448. goto redirty;
  449. /*
  450. * Add inode to shmem_unuse()'s list of swapped-out inodes,
  451. * if it's not already there. Do it now because we cannot take
  452. * mutex while holding spinlock, and must do so before the page
  453. * is moved to swap cache, when its pagelock no longer protects
  454. * the inode from eviction. But don't unlock the mutex until
  455. * we've taken the spinlock, because shmem_unuse_inode() will
  456. * prune a !swapped inode from the swaplist under both locks.
  457. */
  458. mutex_lock(&shmem_swaplist_mutex);
  459. if (list_empty(&info->swaplist))
  460. list_add_tail(&info->swaplist, &shmem_swaplist);
  461. spin_lock(&info->lock);
  462. mutex_unlock(&shmem_swaplist_mutex);
  463. oswap = shmem_get_swap(info, index);
  464. if (oswap.val) {
  465. WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
  466. free_swap_and_cache(oswap);
  467. shmem_put_swap(info, index, (swp_entry_t){0});
  468. info->swapped--;
  469. }
  470. shmem_recalc_inode(inode);
  471. if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
  472. delete_from_page_cache(page);
  473. shmem_put_swap(info, index, swap);
  474. info->swapped++;
  475. swap_shmem_alloc(swap);
  476. spin_unlock(&info->lock);
  477. BUG_ON(page_mapped(page));
  478. swap_writepage(page, wbc);
  479. return 0;
  480. }
  481. spin_unlock(&info->lock);
  482. swapcache_free(swap, NULL);
  483. redirty:
  484. set_page_dirty(page);
  485. if (wbc->for_reclaim)
  486. return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
  487. unlock_page(page);
  488. return 0;
  489. }
  490. #ifdef CONFIG_NUMA
  491. #ifdef CONFIG_TMPFS
  492. static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  493. {
  494. char buffer[64];
  495. if (!mpol || mpol->mode == MPOL_DEFAULT)
  496. return; /* show nothing */
  497. mpol_to_str(buffer, sizeof(buffer), mpol, 1);
  498. seq_printf(seq, ",mpol=%s", buffer);
  499. }
  500. static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  501. {
  502. struct mempolicy *mpol = NULL;
  503. if (sbinfo->mpol) {
  504. spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
  505. mpol = sbinfo->mpol;
  506. mpol_get(mpol);
  507. spin_unlock(&sbinfo->stat_lock);
  508. }
  509. return mpol;
  510. }
  511. #endif /* CONFIG_TMPFS */
  512. static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  513. struct shmem_inode_info *info, pgoff_t index)
  514. {
  515. struct mempolicy mpol, *spol;
  516. struct vm_area_struct pvma;
  517. spol = mpol_cond_copy(&mpol,
  518. mpol_shared_policy_lookup(&info->policy, index));
  519. /* Create a pseudo vma that just contains the policy */
  520. pvma.vm_start = 0;
  521. pvma.vm_pgoff = index;
  522. pvma.vm_ops = NULL;
  523. pvma.vm_policy = spol;
  524. return swapin_readahead(swap, gfp, &pvma, 0);
  525. }
  526. static struct page *shmem_alloc_page(gfp_t gfp,
  527. struct shmem_inode_info *info, pgoff_t index)
  528. {
  529. struct vm_area_struct pvma;
  530. /* Create a pseudo vma that just contains the policy */
  531. pvma.vm_start = 0;
  532. pvma.vm_pgoff = index;
  533. pvma.vm_ops = NULL;
  534. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  535. /*
  536. * alloc_page_vma() will drop the shared policy reference
  537. */
  538. return alloc_page_vma(gfp, &pvma, 0);
  539. }
  540. #else /* !CONFIG_NUMA */
  541. #ifdef CONFIG_TMPFS
  542. static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  543. {
  544. }
  545. #endif /* CONFIG_TMPFS */
  546. static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  547. struct shmem_inode_info *info, pgoff_t index)
  548. {
  549. return swapin_readahead(swap, gfp, NULL, 0);
  550. }
  551. static inline struct page *shmem_alloc_page(gfp_t gfp,
  552. struct shmem_inode_info *info, pgoff_t index)
  553. {
  554. return alloc_page(gfp);
  555. }
  556. #endif /* CONFIG_NUMA */
  557. #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
  558. static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  559. {
  560. return NULL;
  561. }
  562. #endif
  563. /*
  564. * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
  565. *
  566. * If we allocate a new one we do not mark it dirty. That's up to the
  567. * vm. If we swap it in we mark it dirty since we also free the swap
  568. * entry since a page cannot live in both the swap and page cache
  569. */
  570. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  571. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
  572. {
  573. struct address_space *mapping = inode->i_mapping;
  574. struct shmem_inode_info *info = SHMEM_I(inode);
  575. struct shmem_sb_info *sbinfo;
  576. struct page *page;
  577. struct page *prealloc_page = NULL;
  578. swp_entry_t swap;
  579. int error;
  580. if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
  581. return -EFBIG;
  582. repeat:
  583. page = find_lock_page(mapping, index);
  584. if (page) {
  585. /*
  586. * Once we can get the page lock, it must be uptodate:
  587. * if there were an error in reading back from swap,
  588. * the page would not be inserted into the filecache.
  589. */
  590. BUG_ON(!PageUptodate(page));
  591. goto done;
  592. }
  593. /*
  594. * Try to preload while we can wait, to not make a habit of
  595. * draining atomic reserves; but don't latch on to this cpu.
  596. */
  597. error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
  598. if (error)
  599. goto out;
  600. radix_tree_preload_end();
  601. if (sgp != SGP_READ && !prealloc_page) {
  602. prealloc_page = shmem_alloc_page(gfp, info, index);
  603. if (prealloc_page) {
  604. SetPageSwapBacked(prealloc_page);
  605. if (mem_cgroup_cache_charge(prealloc_page,
  606. current->mm, GFP_KERNEL)) {
  607. page_cache_release(prealloc_page);
  608. prealloc_page = NULL;
  609. }
  610. }
  611. }
  612. spin_lock(&info->lock);
  613. shmem_recalc_inode(inode);
  614. swap = shmem_get_swap(info, index);
  615. if (swap.val) {
  616. /* Look it up and read it in.. */
  617. page = lookup_swap_cache(swap);
  618. if (!page) {
  619. spin_unlock(&info->lock);
  620. /* here we actually do the io */
  621. if (fault_type)
  622. *fault_type |= VM_FAULT_MAJOR;
  623. page = shmem_swapin(swap, gfp, info, index);
  624. if (!page) {
  625. swp_entry_t nswap = shmem_get_swap(info, index);
  626. if (nswap.val == swap.val) {
  627. error = -ENOMEM;
  628. goto out;
  629. }
  630. goto repeat;
  631. }
  632. wait_on_page_locked(page);
  633. page_cache_release(page);
  634. goto repeat;
  635. }
  636. /* We have to do this with page locked to prevent races */
  637. if (!trylock_page(page)) {
  638. spin_unlock(&info->lock);
  639. wait_on_page_locked(page);
  640. page_cache_release(page);
  641. goto repeat;
  642. }
  643. if (PageWriteback(page)) {
  644. spin_unlock(&info->lock);
  645. wait_on_page_writeback(page);
  646. unlock_page(page);
  647. page_cache_release(page);
  648. goto repeat;
  649. }
  650. if (!PageUptodate(page)) {
  651. spin_unlock(&info->lock);
  652. unlock_page(page);
  653. page_cache_release(page);
  654. error = -EIO;
  655. goto out;
  656. }
  657. error = add_to_page_cache_locked(page, mapping,
  658. index, GFP_NOWAIT);
  659. if (error) {
  660. spin_unlock(&info->lock);
  661. if (error == -ENOMEM) {
  662. /*
  663. * reclaim from proper memory cgroup and
  664. * call memcg's OOM if needed.
  665. */
  666. error = mem_cgroup_shmem_charge_fallback(
  667. page, current->mm, gfp);
  668. if (error) {
  669. unlock_page(page);
  670. page_cache_release(page);
  671. goto out;
  672. }
  673. }
  674. unlock_page(page);
  675. page_cache_release(page);
  676. goto repeat;
  677. }
  678. delete_from_swap_cache(page);
  679. shmem_put_swap(info, index, (swp_entry_t){0});
  680. info->swapped--;
  681. spin_unlock(&info->lock);
  682. set_page_dirty(page);
  683. swap_free(swap);
  684. } else if (sgp == SGP_READ) {
  685. page = find_get_page(mapping, index);
  686. if (page && !trylock_page(page)) {
  687. spin_unlock(&info->lock);
  688. wait_on_page_locked(page);
  689. page_cache_release(page);
  690. goto repeat;
  691. }
  692. spin_unlock(&info->lock);
  693. } else if (prealloc_page) {
  694. sbinfo = SHMEM_SB(inode->i_sb);
  695. if (sbinfo->max_blocks) {
  696. if (percpu_counter_compare(&sbinfo->used_blocks,
  697. sbinfo->max_blocks) >= 0 ||
  698. shmem_acct_block(info->flags))
  699. goto nospace;
  700. percpu_counter_inc(&sbinfo->used_blocks);
  701. inode->i_blocks += BLOCKS_PER_PAGE;
  702. } else if (shmem_acct_block(info->flags))
  703. goto nospace;
  704. page = prealloc_page;
  705. prealloc_page = NULL;
  706. swap = shmem_get_swap(info, index);
  707. if (swap.val)
  708. mem_cgroup_uncharge_cache_page(page);
  709. else
  710. error = add_to_page_cache_lru(page, mapping,
  711. index, GFP_NOWAIT);
  712. /*
  713. * At add_to_page_cache_lru() failure,
  714. * uncharge will be done automatically.
  715. */
  716. if (swap.val || error) {
  717. shmem_unacct_blocks(info->flags, 1);
  718. shmem_free_blocks(inode, 1);
  719. spin_unlock(&info->lock);
  720. page_cache_release(page);
  721. goto repeat;
  722. }
  723. info->alloced++;
  724. spin_unlock(&info->lock);
  725. clear_highpage(page);
  726. flush_dcache_page(page);
  727. SetPageUptodate(page);
  728. if (sgp == SGP_DIRTY)
  729. set_page_dirty(page);
  730. } else {
  731. spin_unlock(&info->lock);
  732. error = -ENOMEM;
  733. goto out;
  734. }
  735. done:
  736. *pagep = page;
  737. error = 0;
  738. out:
  739. if (prealloc_page) {
  740. mem_cgroup_uncharge_cache_page(prealloc_page);
  741. page_cache_release(prealloc_page);
  742. }
  743. return error;
  744. nospace:
  745. /*
  746. * Perhaps the page was brought in from swap between find_lock_page
  747. * and taking info->lock? We allow for that at add_to_page_cache_lru,
  748. * but must also avoid reporting a spurious ENOSPC while working on a
  749. * full tmpfs.
  750. */
  751. page = find_get_page(mapping, index);
  752. spin_unlock(&info->lock);
  753. if (page) {
  754. page_cache_release(page);
  755. goto repeat;
  756. }
  757. error = -ENOSPC;
  758. goto out;
  759. }
  760. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  761. {
  762. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  763. int error;
  764. int ret = VM_FAULT_LOCKED;
  765. if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  766. return VM_FAULT_SIGBUS;
  767. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
  768. if (error)
  769. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  770. if (ret & VM_FAULT_MAJOR) {
  771. count_vm_event(PGMAJFAULT);
  772. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  773. }
  774. return ret;
  775. }
  776. #ifdef CONFIG_NUMA
  777. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
  778. {
  779. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  780. return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
  781. }
  782. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  783. unsigned long addr)
  784. {
  785. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  786. pgoff_t index;
  787. index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  788. return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
  789. }
  790. #endif
  791. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  792. {
  793. struct inode *inode = file->f_path.dentry->d_inode;
  794. struct shmem_inode_info *info = SHMEM_I(inode);
  795. int retval = -ENOMEM;
  796. spin_lock(&info->lock);
  797. if (lock && !(info->flags & VM_LOCKED)) {
  798. if (!user_shm_lock(inode->i_size, user))
  799. goto out_nomem;
  800. info->flags |= VM_LOCKED;
  801. mapping_set_unevictable(file->f_mapping);
  802. }
  803. if (!lock && (info->flags & VM_LOCKED) && user) {
  804. user_shm_unlock(inode->i_size, user);
  805. info->flags &= ~VM_LOCKED;
  806. mapping_clear_unevictable(file->f_mapping);
  807. scan_mapping_unevictable_pages(file->f_mapping);
  808. }
  809. retval = 0;
  810. out_nomem:
  811. spin_unlock(&info->lock);
  812. return retval;
  813. }
  814. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  815. {
  816. file_accessed(file);
  817. vma->vm_ops = &shmem_vm_ops;
  818. vma->vm_flags |= VM_CAN_NONLINEAR;
  819. return 0;
  820. }
  821. static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
  822. int mode, dev_t dev, unsigned long flags)
  823. {
  824. struct inode *inode;
  825. struct shmem_inode_info *info;
  826. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  827. if (shmem_reserve_inode(sb))
  828. return NULL;
  829. inode = new_inode(sb);
  830. if (inode) {
  831. inode->i_ino = get_next_ino();
  832. inode_init_owner(inode, dir, mode);
  833. inode->i_blocks = 0;
  834. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  835. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  836. inode->i_generation = get_seconds();
  837. info = SHMEM_I(inode);
  838. memset(info, 0, (char *)inode - (char *)info);
  839. spin_lock_init(&info->lock);
  840. info->flags = flags & VM_NORESERVE;
  841. INIT_LIST_HEAD(&info->swaplist);
  842. INIT_LIST_HEAD(&info->xattr_list);
  843. cache_no_acl(inode);
  844. switch (mode & S_IFMT) {
  845. default:
  846. inode->i_op = &shmem_special_inode_operations;
  847. init_special_inode(inode, mode, dev);
  848. break;
  849. case S_IFREG:
  850. inode->i_mapping->a_ops = &shmem_aops;
  851. inode->i_op = &shmem_inode_operations;
  852. inode->i_fop = &shmem_file_operations;
  853. mpol_shared_policy_init(&info->policy,
  854. shmem_get_sbmpol(sbinfo));
  855. break;
  856. case S_IFDIR:
  857. inc_nlink(inode);
  858. /* Some things misbehave if size == 0 on a directory */
  859. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  860. inode->i_op = &shmem_dir_inode_operations;
  861. inode->i_fop = &simple_dir_operations;
  862. break;
  863. case S_IFLNK:
  864. /*
  865. * Must not load anything in the rbtree,
  866. * mpol_free_shared_policy will not be called.
  867. */
  868. mpol_shared_policy_init(&info->policy, NULL);
  869. break;
  870. }
  871. } else
  872. shmem_free_inode(sb);
  873. return inode;
  874. }
  875. #ifdef CONFIG_TMPFS
  876. static const struct inode_operations shmem_symlink_inode_operations;
  877. static const struct inode_operations shmem_symlink_inline_operations;
  878. static int
  879. shmem_write_begin(struct file *file, struct address_space *mapping,
  880. loff_t pos, unsigned len, unsigned flags,
  881. struct page **pagep, void **fsdata)
  882. {
  883. struct inode *inode = mapping->host;
  884. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  885. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  886. }
  887. static int
  888. shmem_write_end(struct file *file, struct address_space *mapping,
  889. loff_t pos, unsigned len, unsigned copied,
  890. struct page *page, void *fsdata)
  891. {
  892. struct inode *inode = mapping->host;
  893. if (pos + copied > inode->i_size)
  894. i_size_write(inode, pos + copied);
  895. set_page_dirty(page);
  896. unlock_page(page);
  897. page_cache_release(page);
  898. return copied;
  899. }
  900. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  901. {
  902. struct inode *inode = filp->f_path.dentry->d_inode;
  903. struct address_space *mapping = inode->i_mapping;
  904. pgoff_t index;
  905. unsigned long offset;
  906. enum sgp_type sgp = SGP_READ;
  907. /*
  908. * Might this read be for a stacking filesystem? Then when reading
  909. * holes of a sparse file, we actually need to allocate those pages,
  910. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  911. */
  912. if (segment_eq(get_fs(), KERNEL_DS))
  913. sgp = SGP_DIRTY;
  914. index = *ppos >> PAGE_CACHE_SHIFT;
  915. offset = *ppos & ~PAGE_CACHE_MASK;
  916. for (;;) {
  917. struct page *page = NULL;
  918. pgoff_t end_index;
  919. unsigned long nr, ret;
  920. loff_t i_size = i_size_read(inode);
  921. end_index = i_size >> PAGE_CACHE_SHIFT;
  922. if (index > end_index)
  923. break;
  924. if (index == end_index) {
  925. nr = i_size & ~PAGE_CACHE_MASK;
  926. if (nr <= offset)
  927. break;
  928. }
  929. desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
  930. if (desc->error) {
  931. if (desc->error == -EINVAL)
  932. desc->error = 0;
  933. break;
  934. }
  935. if (page)
  936. unlock_page(page);
  937. /*
  938. * We must evaluate after, since reads (unlike writes)
  939. * are called without i_mutex protection against truncate
  940. */
  941. nr = PAGE_CACHE_SIZE;
  942. i_size = i_size_read(inode);
  943. end_index = i_size >> PAGE_CACHE_SHIFT;
  944. if (index == end_index) {
  945. nr = i_size & ~PAGE_CACHE_MASK;
  946. if (nr <= offset) {
  947. if (page)
  948. page_cache_release(page);
  949. break;
  950. }
  951. }
  952. nr -= offset;
  953. if (page) {
  954. /*
  955. * If users can be writing to this page using arbitrary
  956. * virtual addresses, take care about potential aliasing
  957. * before reading the page on the kernel side.
  958. */
  959. if (mapping_writably_mapped(mapping))
  960. flush_dcache_page(page);
  961. /*
  962. * Mark the page accessed if we read the beginning.
  963. */
  964. if (!offset)
  965. mark_page_accessed(page);
  966. } else {
  967. page = ZERO_PAGE(0);
  968. page_cache_get(page);
  969. }
  970. /*
  971. * Ok, we have the page, and it's up-to-date, so
  972. * now we can copy it to user space...
  973. *
  974. * The actor routine returns how many bytes were actually used..
  975. * NOTE! This may not be the same as how much of a user buffer
  976. * we filled up (we may be padding etc), so we can only update
  977. * "pos" here (the actor routine has to update the user buffer
  978. * pointers and the remaining count).
  979. */
  980. ret = actor(desc, page, offset, nr);
  981. offset += ret;
  982. index += offset >> PAGE_CACHE_SHIFT;
  983. offset &= ~PAGE_CACHE_MASK;
  984. page_cache_release(page);
  985. if (ret != nr || !desc->count)
  986. break;
  987. cond_resched();
  988. }
  989. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  990. file_accessed(filp);
  991. }
  992. static ssize_t shmem_file_aio_read(struct kiocb *iocb,
  993. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  994. {
  995. struct file *filp = iocb->ki_filp;
  996. ssize_t retval;
  997. unsigned long seg;
  998. size_t count;
  999. loff_t *ppos = &iocb->ki_pos;
  1000. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1001. if (retval)
  1002. return retval;
  1003. for (seg = 0; seg < nr_segs; seg++) {
  1004. read_descriptor_t desc;
  1005. desc.written = 0;
  1006. desc.arg.buf = iov[seg].iov_base;
  1007. desc.count = iov[seg].iov_len;
  1008. if (desc.count == 0)
  1009. continue;
  1010. desc.error = 0;
  1011. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1012. retval += desc.written;
  1013. if (desc.error) {
  1014. retval = retval ?: desc.error;
  1015. break;
  1016. }
  1017. if (desc.count > 0)
  1018. break;
  1019. }
  1020. return retval;
  1021. }
  1022. static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
  1023. struct pipe_inode_info *pipe, size_t len,
  1024. unsigned int flags)
  1025. {
  1026. struct address_space *mapping = in->f_mapping;
  1027. struct inode *inode = mapping->host;
  1028. unsigned int loff, nr_pages, req_pages;
  1029. struct page *pages[PIPE_DEF_BUFFERS];
  1030. struct partial_page partial[PIPE_DEF_BUFFERS];
  1031. struct page *page;
  1032. pgoff_t index, end_index;
  1033. loff_t isize, left;
  1034. int error, page_nr;
  1035. struct splice_pipe_desc spd = {
  1036. .pages = pages,
  1037. .partial = partial,
  1038. .flags = flags,
  1039. .ops = &page_cache_pipe_buf_ops,
  1040. .spd_release = spd_release_page,
  1041. };
  1042. isize = i_size_read(inode);
  1043. if (unlikely(*ppos >= isize))
  1044. return 0;
  1045. left = isize - *ppos;
  1046. if (unlikely(left < len))
  1047. len = left;
  1048. if (splice_grow_spd(pipe, &spd))
  1049. return -ENOMEM;
  1050. index = *ppos >> PAGE_CACHE_SHIFT;
  1051. loff = *ppos & ~PAGE_CACHE_MASK;
  1052. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1053. nr_pages = min(req_pages, pipe->buffers);
  1054. spd.nr_pages = find_get_pages_contig(mapping, index,
  1055. nr_pages, spd.pages);
  1056. index += spd.nr_pages;
  1057. error = 0;
  1058. while (spd.nr_pages < nr_pages) {
  1059. error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
  1060. if (error)
  1061. break;
  1062. unlock_page(page);
  1063. spd.pages[spd.nr_pages++] = page;
  1064. index++;
  1065. }
  1066. index = *ppos >> PAGE_CACHE_SHIFT;
  1067. nr_pages = spd.nr_pages;
  1068. spd.nr_pages = 0;
  1069. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  1070. unsigned int this_len;
  1071. if (!len)
  1072. break;
  1073. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  1074. page = spd.pages[page_nr];
  1075. if (!PageUptodate(page) || page->mapping != mapping) {
  1076. error = shmem_getpage(inode, index, &page,
  1077. SGP_CACHE, NULL);
  1078. if (error)
  1079. break;
  1080. unlock_page(page);
  1081. page_cache_release(spd.pages[page_nr]);
  1082. spd.pages[page_nr] = page;
  1083. }
  1084. isize = i_size_read(inode);
  1085. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1086. if (unlikely(!isize || index > end_index))
  1087. break;
  1088. if (end_index == index) {
  1089. unsigned int plen;
  1090. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1091. if (plen <= loff)
  1092. break;
  1093. this_len = min(this_len, plen - loff);
  1094. len = this_len;
  1095. }
  1096. spd.partial[page_nr].offset = loff;
  1097. spd.partial[page_nr].len = this_len;
  1098. len -= this_len;
  1099. loff = 0;
  1100. spd.nr_pages++;
  1101. index++;
  1102. }
  1103. while (page_nr < nr_pages)
  1104. page_cache_release(spd.pages[page_nr++]);
  1105. if (spd.nr_pages)
  1106. error = splice_to_pipe(pipe, &spd);
  1107. splice_shrink_spd(pipe, &spd);
  1108. if (error > 0) {
  1109. *ppos += error;
  1110. file_accessed(in);
  1111. }
  1112. return error;
  1113. }
  1114. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1115. {
  1116. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1117. buf->f_type = TMPFS_MAGIC;
  1118. buf->f_bsize = PAGE_CACHE_SIZE;
  1119. buf->f_namelen = NAME_MAX;
  1120. if (sbinfo->max_blocks) {
  1121. buf->f_blocks = sbinfo->max_blocks;
  1122. buf->f_bavail =
  1123. buf->f_bfree = sbinfo->max_blocks -
  1124. percpu_counter_sum(&sbinfo->used_blocks);
  1125. }
  1126. if (sbinfo->max_inodes) {
  1127. buf->f_files = sbinfo->max_inodes;
  1128. buf->f_ffree = sbinfo->free_inodes;
  1129. }
  1130. /* else leave those fields 0 like simple_statfs */
  1131. return 0;
  1132. }
  1133. /*
  1134. * File creation. Allocate an inode, and we're done..
  1135. */
  1136. static int
  1137. shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1138. {
  1139. struct inode *inode;
  1140. int error = -ENOSPC;
  1141. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
  1142. if (inode) {
  1143. error = security_inode_init_security(inode, dir,
  1144. &dentry->d_name, NULL,
  1145. NULL, NULL);
  1146. if (error) {
  1147. if (error != -EOPNOTSUPP) {
  1148. iput(inode);
  1149. return error;
  1150. }
  1151. }
  1152. #ifdef CONFIG_TMPFS_POSIX_ACL
  1153. error = generic_acl_init(inode, dir);
  1154. if (error) {
  1155. iput(inode);
  1156. return error;
  1157. }
  1158. #else
  1159. error = 0;
  1160. #endif
  1161. dir->i_size += BOGO_DIRENT_SIZE;
  1162. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1163. d_instantiate(dentry, inode);
  1164. dget(dentry); /* Extra count - pin the dentry in core */
  1165. }
  1166. return error;
  1167. }
  1168. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1169. {
  1170. int error;
  1171. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1172. return error;
  1173. inc_nlink(dir);
  1174. return 0;
  1175. }
  1176. static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
  1177. struct nameidata *nd)
  1178. {
  1179. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1180. }
  1181. /*
  1182. * Link a file..
  1183. */
  1184. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1185. {
  1186. struct inode *inode = old_dentry->d_inode;
  1187. int ret;
  1188. /*
  1189. * No ordinary (disk based) filesystem counts links as inodes;
  1190. * but each new link needs a new dentry, pinning lowmem, and
  1191. * tmpfs dentries cannot be pruned until they are unlinked.
  1192. */
  1193. ret = shmem_reserve_inode(inode->i_sb);
  1194. if (ret)
  1195. goto out;
  1196. dir->i_size += BOGO_DIRENT_SIZE;
  1197. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1198. inc_nlink(inode);
  1199. ihold(inode); /* New dentry reference */
  1200. dget(dentry); /* Extra pinning count for the created dentry */
  1201. d_instantiate(dentry, inode);
  1202. out:
  1203. return ret;
  1204. }
  1205. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1206. {
  1207. struct inode *inode = dentry->d_inode;
  1208. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  1209. shmem_free_inode(inode->i_sb);
  1210. dir->i_size -= BOGO_DIRENT_SIZE;
  1211. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1212. drop_nlink(inode);
  1213. dput(dentry); /* Undo the count from "create" - this does all the work */
  1214. return 0;
  1215. }
  1216. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1217. {
  1218. if (!simple_empty(dentry))
  1219. return -ENOTEMPTY;
  1220. drop_nlink(dentry->d_inode);
  1221. drop_nlink(dir);
  1222. return shmem_unlink(dir, dentry);
  1223. }
  1224. /*
  1225. * The VFS layer already does all the dentry stuff for rename,
  1226. * we just have to decrement the usage count for the target if
  1227. * it exists so that the VFS layer correctly free's it when it
  1228. * gets overwritten.
  1229. */
  1230. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1231. {
  1232. struct inode *inode = old_dentry->d_inode;
  1233. int they_are_dirs = S_ISDIR(inode->i_mode);
  1234. if (!simple_empty(new_dentry))
  1235. return -ENOTEMPTY;
  1236. if (new_dentry->d_inode) {
  1237. (void) shmem_unlink(new_dir, new_dentry);
  1238. if (they_are_dirs)
  1239. drop_nlink(old_dir);
  1240. } else if (they_are_dirs) {
  1241. drop_nlink(old_dir);
  1242. inc_nlink(new_dir);
  1243. }
  1244. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1245. new_dir->i_size += BOGO_DIRENT_SIZE;
  1246. old_dir->i_ctime = old_dir->i_mtime =
  1247. new_dir->i_ctime = new_dir->i_mtime =
  1248. inode->i_ctime = CURRENT_TIME;
  1249. return 0;
  1250. }
  1251. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1252. {
  1253. int error;
  1254. int len;
  1255. struct inode *inode;
  1256. struct page *page;
  1257. char *kaddr;
  1258. struct shmem_inode_info *info;
  1259. len = strlen(symname) + 1;
  1260. if (len > PAGE_CACHE_SIZE)
  1261. return -ENAMETOOLONG;
  1262. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
  1263. if (!inode)
  1264. return -ENOSPC;
  1265. error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
  1266. NULL, NULL);
  1267. if (error) {
  1268. if (error != -EOPNOTSUPP) {
  1269. iput(inode);
  1270. return error;
  1271. }
  1272. error = 0;
  1273. }
  1274. info = SHMEM_I(inode);
  1275. inode->i_size = len-1;
  1276. if (len <= SHMEM_SYMLINK_INLINE_LEN) {
  1277. /* do it inline */
  1278. memcpy(info->inline_symlink, symname, len);
  1279. inode->i_op = &shmem_symlink_inline_operations;
  1280. } else {
  1281. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1282. if (error) {
  1283. iput(inode);
  1284. return error;
  1285. }
  1286. inode->i_mapping->a_ops = &shmem_aops;
  1287. inode->i_op = &shmem_symlink_inode_operations;
  1288. kaddr = kmap_atomic(page, KM_USER0);
  1289. memcpy(kaddr, symname, len);
  1290. kunmap_atomic(kaddr, KM_USER0);
  1291. set_page_dirty(page);
  1292. unlock_page(page);
  1293. page_cache_release(page);
  1294. }
  1295. dir->i_size += BOGO_DIRENT_SIZE;
  1296. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1297. d_instantiate(dentry, inode);
  1298. dget(dentry);
  1299. return 0;
  1300. }
  1301. static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
  1302. {
  1303. nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
  1304. return NULL;
  1305. }
  1306. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1307. {
  1308. struct page *page = NULL;
  1309. int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1310. nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
  1311. if (page)
  1312. unlock_page(page);
  1313. return page;
  1314. }
  1315. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1316. {
  1317. if (!IS_ERR(nd_get_link(nd))) {
  1318. struct page *page = cookie;
  1319. kunmap(page);
  1320. mark_page_accessed(page);
  1321. page_cache_release(page);
  1322. }
  1323. }
  1324. #ifdef CONFIG_TMPFS_XATTR
  1325. /*
  1326. * Superblocks without xattr inode operations may get some security.* xattr
  1327. * support from the LSM "for free". As soon as we have any other xattrs
  1328. * like ACLs, we also need to implement the security.* handlers at
  1329. * filesystem level, though.
  1330. */
  1331. static int shmem_xattr_get(struct dentry *dentry, const char *name,
  1332. void *buffer, size_t size)
  1333. {
  1334. struct shmem_inode_info *info;
  1335. struct shmem_xattr *xattr;
  1336. int ret = -ENODATA;
  1337. info = SHMEM_I(dentry->d_inode);
  1338. spin_lock(&info->lock);
  1339. list_for_each_entry(xattr, &info->xattr_list, list) {
  1340. if (strcmp(name, xattr->name))
  1341. continue;
  1342. ret = xattr->size;
  1343. if (buffer) {
  1344. if (size < xattr->size)
  1345. ret = -ERANGE;
  1346. else
  1347. memcpy(buffer, xattr->value, xattr->size);
  1348. }
  1349. break;
  1350. }
  1351. spin_unlock(&info->lock);
  1352. return ret;
  1353. }
  1354. static int shmem_xattr_set(struct dentry *dentry, const char *name,
  1355. const void *value, size_t size, int flags)
  1356. {
  1357. struct inode *inode = dentry->d_inode;
  1358. struct shmem_inode_info *info = SHMEM_I(inode);
  1359. struct shmem_xattr *xattr;
  1360. struct shmem_xattr *new_xattr = NULL;
  1361. size_t len;
  1362. int err = 0;
  1363. /* value == NULL means remove */
  1364. if (value) {
  1365. /* wrap around? */
  1366. len = sizeof(*new_xattr) + size;
  1367. if (len <= sizeof(*new_xattr))
  1368. return -ENOMEM;
  1369. new_xattr = kmalloc(len, GFP_KERNEL);
  1370. if (!new_xattr)
  1371. return -ENOMEM;
  1372. new_xattr->name = kstrdup(name, GFP_KERNEL);
  1373. if (!new_xattr->name) {
  1374. kfree(new_xattr);
  1375. return -ENOMEM;
  1376. }
  1377. new_xattr->size = size;
  1378. memcpy(new_xattr->value, value, size);
  1379. }
  1380. spin_lock(&info->lock);
  1381. list_for_each_entry(xattr, &info->xattr_list, list) {
  1382. if (!strcmp(name, xattr->name)) {
  1383. if (flags & XATTR_CREATE) {
  1384. xattr = new_xattr;
  1385. err = -EEXIST;
  1386. } else if (new_xattr) {
  1387. list_replace(&xattr->list, &new_xattr->list);
  1388. } else {
  1389. list_del(&xattr->list);
  1390. }
  1391. goto out;
  1392. }
  1393. }
  1394. if (flags & XATTR_REPLACE) {
  1395. xattr = new_xattr;
  1396. err = -ENODATA;
  1397. } else {
  1398. list_add(&new_xattr->list, &info->xattr_list);
  1399. xattr = NULL;
  1400. }
  1401. out:
  1402. spin_unlock(&info->lock);
  1403. if (xattr)
  1404. kfree(xattr->name);
  1405. kfree(xattr);
  1406. return err;
  1407. }
  1408. static const struct xattr_handler *shmem_xattr_handlers[] = {
  1409. #ifdef CONFIG_TMPFS_POSIX_ACL
  1410. &generic_acl_access_handler,
  1411. &generic_acl_default_handler,
  1412. #endif
  1413. NULL
  1414. };
  1415. static int shmem_xattr_validate(const char *name)
  1416. {
  1417. struct { const char *prefix; size_t len; } arr[] = {
  1418. { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
  1419. { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
  1420. };
  1421. int i;
  1422. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1423. size_t preflen = arr[i].len;
  1424. if (strncmp(name, arr[i].prefix, preflen) == 0) {
  1425. if (!name[preflen])
  1426. return -EINVAL;
  1427. return 0;
  1428. }
  1429. }
  1430. return -EOPNOTSUPP;
  1431. }
  1432. static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
  1433. void *buffer, size_t size)
  1434. {
  1435. int err;
  1436. /*
  1437. * If this is a request for a synthetic attribute in the system.*
  1438. * namespace use the generic infrastructure to resolve a handler
  1439. * for it via sb->s_xattr.
  1440. */
  1441. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1442. return generic_getxattr(dentry, name, buffer, size);
  1443. err = shmem_xattr_validate(name);
  1444. if (err)
  1445. return err;
  1446. return shmem_xattr_get(dentry, name, buffer, size);
  1447. }
  1448. static int shmem_setxattr(struct dentry *dentry, const char *name,
  1449. const void *value, size_t size, int flags)
  1450. {
  1451. int err;
  1452. /*
  1453. * If this is a request for a synthetic attribute in the system.*
  1454. * namespace use the generic infrastructure to resolve a handler
  1455. * for it via sb->s_xattr.
  1456. */
  1457. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1458. return generic_setxattr(dentry, name, value, size, flags);
  1459. err = shmem_xattr_validate(name);
  1460. if (err)
  1461. return err;
  1462. if (size == 0)
  1463. value = ""; /* empty EA, do not remove */
  1464. return shmem_xattr_set(dentry, name, value, size, flags);
  1465. }
  1466. static int shmem_removexattr(struct dentry *dentry, const char *name)
  1467. {
  1468. int err;
  1469. /*
  1470. * If this is a request for a synthetic attribute in the system.*
  1471. * namespace use the generic infrastructure to resolve a handler
  1472. * for it via sb->s_xattr.
  1473. */
  1474. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1475. return generic_removexattr(dentry, name);
  1476. err = shmem_xattr_validate(name);
  1477. if (err)
  1478. return err;
  1479. return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
  1480. }
  1481. static bool xattr_is_trusted(const char *name)
  1482. {
  1483. return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  1484. }
  1485. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  1486. {
  1487. bool trusted = capable(CAP_SYS_ADMIN);
  1488. struct shmem_xattr *xattr;
  1489. struct shmem_inode_info *info;
  1490. size_t used = 0;
  1491. info = SHMEM_I(dentry->d_inode);
  1492. spin_lock(&info->lock);
  1493. list_for_each_entry(xattr, &info->xattr_list, list) {
  1494. size_t len;
  1495. /* skip "trusted." attributes for unprivileged callers */
  1496. if (!trusted && xattr_is_trusted(xattr->name))
  1497. continue;
  1498. len = strlen(xattr->name) + 1;
  1499. used += len;
  1500. if (buffer) {
  1501. if (size < used) {
  1502. used = -ERANGE;
  1503. break;
  1504. }
  1505. memcpy(buffer, xattr->name, len);
  1506. buffer += len;
  1507. }
  1508. }
  1509. spin_unlock(&info->lock);
  1510. return used;
  1511. }
  1512. #endif /* CONFIG_TMPFS_XATTR */
  1513. static const struct inode_operations shmem_symlink_inline_operations = {
  1514. .readlink = generic_readlink,
  1515. .follow_link = shmem_follow_link_inline,
  1516. #ifdef CONFIG_TMPFS_XATTR
  1517. .setxattr = shmem_setxattr,
  1518. .getxattr = shmem_getxattr,
  1519. .listxattr = shmem_listxattr,
  1520. .removexattr = shmem_removexattr,
  1521. #endif
  1522. };
  1523. static const struct inode_operations shmem_symlink_inode_operations = {
  1524. .readlink = generic_readlink,
  1525. .follow_link = shmem_follow_link,
  1526. .put_link = shmem_put_link,
  1527. #ifdef CONFIG_TMPFS_XATTR
  1528. .setxattr = shmem_setxattr,
  1529. .getxattr = shmem_getxattr,
  1530. .listxattr = shmem_listxattr,
  1531. .removexattr = shmem_removexattr,
  1532. #endif
  1533. };
  1534. static struct dentry *shmem_get_parent(struct dentry *child)
  1535. {
  1536. return ERR_PTR(-ESTALE);
  1537. }
  1538. static int shmem_match(struct inode *ino, void *vfh)
  1539. {
  1540. __u32 *fh = vfh;
  1541. __u64 inum = fh[2];
  1542. inum = (inum << 32) | fh[1];
  1543. return ino->i_ino == inum && fh[0] == ino->i_generation;
  1544. }
  1545. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  1546. struct fid *fid, int fh_len, int fh_type)
  1547. {
  1548. struct inode *inode;
  1549. struct dentry *dentry = NULL;
  1550. u64 inum = fid->raw[2];
  1551. inum = (inum << 32) | fid->raw[1];
  1552. if (fh_len < 3)
  1553. return NULL;
  1554. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  1555. shmem_match, fid->raw);
  1556. if (inode) {
  1557. dentry = d_find_alias(inode);
  1558. iput(inode);
  1559. }
  1560. return dentry;
  1561. }
  1562. static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
  1563. int connectable)
  1564. {
  1565. struct inode *inode = dentry->d_inode;
  1566. if (*len < 3) {
  1567. *len = 3;
  1568. return 255;
  1569. }
  1570. if (inode_unhashed(inode)) {
  1571. /* Unfortunately insert_inode_hash is not idempotent,
  1572. * so as we hash inodes here rather than at creation
  1573. * time, we need a lock to ensure we only try
  1574. * to do it once
  1575. */
  1576. static DEFINE_SPINLOCK(lock);
  1577. spin_lock(&lock);
  1578. if (inode_unhashed(inode))
  1579. __insert_inode_hash(inode,
  1580. inode->i_ino + inode->i_generation);
  1581. spin_unlock(&lock);
  1582. }
  1583. fh[0] = inode->i_generation;
  1584. fh[1] = inode->i_ino;
  1585. fh[2] = ((__u64)inode->i_ino) >> 32;
  1586. *len = 3;
  1587. return 1;
  1588. }
  1589. static const struct export_operations shmem_export_ops = {
  1590. .get_parent = shmem_get_parent,
  1591. .encode_fh = shmem_encode_fh,
  1592. .fh_to_dentry = shmem_fh_to_dentry,
  1593. };
  1594. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  1595. bool remount)
  1596. {
  1597. char *this_char, *value, *rest;
  1598. while (options != NULL) {
  1599. this_char = options;
  1600. for (;;) {
  1601. /*
  1602. * NUL-terminate this option: unfortunately,
  1603. * mount options form a comma-separated list,
  1604. * but mpol's nodelist may also contain commas.
  1605. */
  1606. options = strchr(options, ',');
  1607. if (options == NULL)
  1608. break;
  1609. options++;
  1610. if (!isdigit(*options)) {
  1611. options[-1] = '\0';
  1612. break;
  1613. }
  1614. }
  1615. if (!*this_char)
  1616. continue;
  1617. if ((value = strchr(this_char,'=')) != NULL) {
  1618. *value++ = 0;
  1619. } else {
  1620. printk(KERN_ERR
  1621. "tmpfs: No value for mount option '%s'\n",
  1622. this_char);
  1623. return 1;
  1624. }
  1625. if (!strcmp(this_char,"size")) {
  1626. unsigned long long size;
  1627. size = memparse(value,&rest);
  1628. if (*rest == '%') {
  1629. size <<= PAGE_SHIFT;
  1630. size *= totalram_pages;
  1631. do_div(size, 100);
  1632. rest++;
  1633. }
  1634. if (*rest)
  1635. goto bad_val;
  1636. sbinfo->max_blocks =
  1637. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  1638. } else if (!strcmp(this_char,"nr_blocks")) {
  1639. sbinfo->max_blocks = memparse(value, &rest);
  1640. if (*rest)
  1641. goto bad_val;
  1642. } else if (!strcmp(this_char,"nr_inodes")) {
  1643. sbinfo->max_inodes = memparse(value, &rest);
  1644. if (*rest)
  1645. goto bad_val;
  1646. } else if (!strcmp(this_char,"mode")) {
  1647. if (remount)
  1648. continue;
  1649. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  1650. if (*rest)
  1651. goto bad_val;
  1652. } else if (!strcmp(this_char,"uid")) {
  1653. if (remount)
  1654. continue;
  1655. sbinfo->uid = simple_strtoul(value, &rest, 0);
  1656. if (*rest)
  1657. goto bad_val;
  1658. } else if (!strcmp(this_char,"gid")) {
  1659. if (remount)
  1660. continue;
  1661. sbinfo->gid = simple_strtoul(value, &rest, 0);
  1662. if (*rest)
  1663. goto bad_val;
  1664. } else if (!strcmp(this_char,"mpol")) {
  1665. if (mpol_parse_str(value, &sbinfo->mpol, 1))
  1666. goto bad_val;
  1667. } else {
  1668. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  1669. this_char);
  1670. return 1;
  1671. }
  1672. }
  1673. return 0;
  1674. bad_val:
  1675. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  1676. value, this_char);
  1677. return 1;
  1678. }
  1679. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  1680. {
  1681. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1682. struct shmem_sb_info config = *sbinfo;
  1683. unsigned long inodes;
  1684. int error = -EINVAL;
  1685. if (shmem_parse_options(data, &config, true))
  1686. return error;
  1687. spin_lock(&sbinfo->stat_lock);
  1688. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  1689. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  1690. goto out;
  1691. if (config.max_inodes < inodes)
  1692. goto out;
  1693. /*
  1694. * Those tests also disallow limited->unlimited while any are in
  1695. * use, so i_blocks will always be zero when max_blocks is zero;
  1696. * but we must separately disallow unlimited->limited, because
  1697. * in that case we have no record of how much is already in use.
  1698. */
  1699. if (config.max_blocks && !sbinfo->max_blocks)
  1700. goto out;
  1701. if (config.max_inodes && !sbinfo->max_inodes)
  1702. goto out;
  1703. error = 0;
  1704. sbinfo->max_blocks = config.max_blocks;
  1705. sbinfo->max_inodes = config.max_inodes;
  1706. sbinfo->free_inodes = config.max_inodes - inodes;
  1707. mpol_put(sbinfo->mpol);
  1708. sbinfo->mpol = config.mpol; /* transfers initial ref */
  1709. out:
  1710. spin_unlock(&sbinfo->stat_lock);
  1711. return error;
  1712. }
  1713. static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
  1714. {
  1715. struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
  1716. if (sbinfo->max_blocks != shmem_default_max_blocks())
  1717. seq_printf(seq, ",size=%luk",
  1718. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  1719. if (sbinfo->max_inodes != shmem_default_max_inodes())
  1720. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  1721. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  1722. seq_printf(seq, ",mode=%03o", sbinfo->mode);
  1723. if (sbinfo->uid != 0)
  1724. seq_printf(seq, ",uid=%u", sbinfo->uid);
  1725. if (sbinfo->gid != 0)
  1726. seq_printf(seq, ",gid=%u", sbinfo->gid);
  1727. shmem_show_mpol(seq, sbinfo->mpol);
  1728. return 0;
  1729. }
  1730. #endif /* CONFIG_TMPFS */
  1731. static void shmem_put_super(struct super_block *sb)
  1732. {
  1733. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1734. percpu_counter_destroy(&sbinfo->used_blocks);
  1735. kfree(sbinfo);
  1736. sb->s_fs_info = NULL;
  1737. }
  1738. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  1739. {
  1740. struct inode *inode;
  1741. struct dentry *root;
  1742. struct shmem_sb_info *sbinfo;
  1743. int err = -ENOMEM;
  1744. /* Round up to L1_CACHE_BYTES to resist false sharing */
  1745. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  1746. L1_CACHE_BYTES), GFP_KERNEL);
  1747. if (!sbinfo)
  1748. return -ENOMEM;
  1749. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  1750. sbinfo->uid = current_fsuid();
  1751. sbinfo->gid = current_fsgid();
  1752. sb->s_fs_info = sbinfo;
  1753. #ifdef CONFIG_TMPFS
  1754. /*
  1755. * Per default we only allow half of the physical ram per
  1756. * tmpfs instance, limiting inodes to one per page of lowmem;
  1757. * but the internal instance is left unlimited.
  1758. */
  1759. if (!(sb->s_flags & MS_NOUSER)) {
  1760. sbinfo->max_blocks = shmem_default_max_blocks();
  1761. sbinfo->max_inodes = shmem_default_max_inodes();
  1762. if (shmem_parse_options(data, sbinfo, false)) {
  1763. err = -EINVAL;
  1764. goto failed;
  1765. }
  1766. }
  1767. sb->s_export_op = &shmem_export_ops;
  1768. #else
  1769. sb->s_flags |= MS_NOUSER;
  1770. #endif
  1771. spin_lock_init(&sbinfo->stat_lock);
  1772. if (percpu_counter_init(&sbinfo->used_blocks, 0))
  1773. goto failed;
  1774. sbinfo->free_inodes = sbinfo->max_inodes;
  1775. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1776. sb->s_blocksize = PAGE_CACHE_SIZE;
  1777. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1778. sb->s_magic = TMPFS_MAGIC;
  1779. sb->s_op = &shmem_ops;
  1780. sb->s_time_gran = 1;
  1781. #ifdef CONFIG_TMPFS_XATTR
  1782. sb->s_xattr = shmem_xattr_handlers;
  1783. #endif
  1784. #ifdef CONFIG_TMPFS_POSIX_ACL
  1785. sb->s_flags |= MS_POSIXACL;
  1786. #endif
  1787. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
  1788. if (!inode)
  1789. goto failed;
  1790. inode->i_uid = sbinfo->uid;
  1791. inode->i_gid = sbinfo->gid;
  1792. root = d_alloc_root(inode);
  1793. if (!root)
  1794. goto failed_iput;
  1795. sb->s_root = root;
  1796. return 0;
  1797. failed_iput:
  1798. iput(inode);
  1799. failed:
  1800. shmem_put_super(sb);
  1801. return err;
  1802. }
  1803. static struct kmem_cache *shmem_inode_cachep;
  1804. static struct inode *shmem_alloc_inode(struct super_block *sb)
  1805. {
  1806. struct shmem_inode_info *info;
  1807. info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  1808. if (!info)
  1809. return NULL;
  1810. return &info->vfs_inode;
  1811. }
  1812. static void shmem_destroy_callback(struct rcu_head *head)
  1813. {
  1814. struct inode *inode = container_of(head, struct inode, i_rcu);
  1815. INIT_LIST_HEAD(&inode->i_dentry);
  1816. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  1817. }
  1818. static void shmem_destroy_inode(struct inode *inode)
  1819. {
  1820. if ((inode->i_mode & S_IFMT) == S_IFREG) {
  1821. /* only struct inode is valid if it's an inline symlink */
  1822. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  1823. }
  1824. call_rcu(&inode->i_rcu, shmem_destroy_callback);
  1825. }
  1826. static void shmem_init_inode(void *foo)
  1827. {
  1828. struct shmem_inode_info *info = foo;
  1829. inode_init_once(&info->vfs_inode);
  1830. }
  1831. static int shmem_init_inodecache(void)
  1832. {
  1833. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  1834. sizeof(struct shmem_inode_info),
  1835. 0, SLAB_PANIC, shmem_init_inode);
  1836. return 0;
  1837. }
  1838. static void shmem_destroy_inodecache(void)
  1839. {
  1840. kmem_cache_destroy(shmem_inode_cachep);
  1841. }
  1842. static const struct address_space_operations shmem_aops = {
  1843. .writepage = shmem_writepage,
  1844. .set_page_dirty = __set_page_dirty_no_writeback,
  1845. #ifdef CONFIG_TMPFS
  1846. .write_begin = shmem_write_begin,
  1847. .write_end = shmem_write_end,
  1848. #endif
  1849. .migratepage = migrate_page,
  1850. .error_remove_page = generic_error_remove_page,
  1851. };
  1852. static const struct file_operations shmem_file_operations = {
  1853. .mmap = shmem_mmap,
  1854. #ifdef CONFIG_TMPFS
  1855. .llseek = generic_file_llseek,
  1856. .read = do_sync_read,
  1857. .write = do_sync_write,
  1858. .aio_read = shmem_file_aio_read,
  1859. .aio_write = generic_file_aio_write,
  1860. .fsync = noop_fsync,
  1861. .splice_read = shmem_file_splice_read,
  1862. .splice_write = generic_file_splice_write,
  1863. #endif
  1864. };
  1865. static const struct inode_operations shmem_inode_operations = {
  1866. .setattr = shmem_setattr,
  1867. .truncate_range = shmem_truncate_range,
  1868. #ifdef CONFIG_TMPFS_XATTR
  1869. .setxattr = shmem_setxattr,
  1870. .getxattr = shmem_getxattr,
  1871. .listxattr = shmem_listxattr,
  1872. .removexattr = shmem_removexattr,
  1873. #endif
  1874. };
  1875. static const struct inode_operations shmem_dir_inode_operations = {
  1876. #ifdef CONFIG_TMPFS
  1877. .create = shmem_create,
  1878. .lookup = simple_lookup,
  1879. .link = shmem_link,
  1880. .unlink = shmem_unlink,
  1881. .symlink = shmem_symlink,
  1882. .mkdir = shmem_mkdir,
  1883. .rmdir = shmem_rmdir,
  1884. .mknod = shmem_mknod,
  1885. .rename = shmem_rename,
  1886. #endif
  1887. #ifdef CONFIG_TMPFS_XATTR
  1888. .setxattr = shmem_setxattr,
  1889. .getxattr = shmem_getxattr,
  1890. .listxattr = shmem_listxattr,
  1891. .removexattr = shmem_removexattr,
  1892. #endif
  1893. #ifdef CONFIG_TMPFS_POSIX_ACL
  1894. .setattr = shmem_setattr,
  1895. #endif
  1896. };
  1897. static const struct inode_operations shmem_special_inode_operations = {
  1898. #ifdef CONFIG_TMPFS_XATTR
  1899. .setxattr = shmem_setxattr,
  1900. .getxattr = shmem_getxattr,
  1901. .listxattr = shmem_listxattr,
  1902. .removexattr = shmem_removexattr,
  1903. #endif
  1904. #ifdef CONFIG_TMPFS_POSIX_ACL
  1905. .setattr = shmem_setattr,
  1906. #endif
  1907. };
  1908. static const struct super_operations shmem_ops = {
  1909. .alloc_inode = shmem_alloc_inode,
  1910. .destroy_inode = shmem_destroy_inode,
  1911. #ifdef CONFIG_TMPFS
  1912. .statfs = shmem_statfs,
  1913. .remount_fs = shmem_remount_fs,
  1914. .show_options = shmem_show_options,
  1915. #endif
  1916. .evict_inode = shmem_evict_inode,
  1917. .drop_inode = generic_delete_inode,
  1918. .put_super = shmem_put_super,
  1919. };
  1920. static const struct vm_operations_struct shmem_vm_ops = {
  1921. .fault = shmem_fault,
  1922. #ifdef CONFIG_NUMA
  1923. .set_policy = shmem_set_policy,
  1924. .get_policy = shmem_get_policy,
  1925. #endif
  1926. };
  1927. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  1928. int flags, const char *dev_name, void *data)
  1929. {
  1930. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  1931. }
  1932. static struct file_system_type shmem_fs_type = {
  1933. .owner = THIS_MODULE,
  1934. .name = "tmpfs",
  1935. .mount = shmem_mount,
  1936. .kill_sb = kill_litter_super,
  1937. };
  1938. int __init shmem_init(void)
  1939. {
  1940. int error;
  1941. error = bdi_init(&shmem_backing_dev_info);
  1942. if (error)
  1943. goto out4;
  1944. error = shmem_init_inodecache();
  1945. if (error)
  1946. goto out3;
  1947. error = register_filesystem(&shmem_fs_type);
  1948. if (error) {
  1949. printk(KERN_ERR "Could not register tmpfs\n");
  1950. goto out2;
  1951. }
  1952. shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
  1953. shmem_fs_type.name, NULL);
  1954. if (IS_ERR(shm_mnt)) {
  1955. error = PTR_ERR(shm_mnt);
  1956. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  1957. goto out1;
  1958. }
  1959. return 0;
  1960. out1:
  1961. unregister_filesystem(&shmem_fs_type);
  1962. out2:
  1963. shmem_destroy_inodecache();
  1964. out3:
  1965. bdi_destroy(&shmem_backing_dev_info);
  1966. out4:
  1967. shm_mnt = ERR_PTR(error);
  1968. return error;
  1969. }
  1970. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  1971. /**
  1972. * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file
  1973. * @inode: the inode to be searched
  1974. * @index: the page offset to be searched
  1975. * @pagep: the pointer for the found page to be stored
  1976. * @swapp: the pointer for the found swap entry to be stored
  1977. *
  1978. * If a page is found, refcount of it is incremented. Callers should handle
  1979. * these refcount.
  1980. */
  1981. void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index,
  1982. struct page **pagep, swp_entry_t *swapp)
  1983. {
  1984. struct shmem_inode_info *info = SHMEM_I(inode);
  1985. struct page *page = NULL;
  1986. swp_entry_t swap = {0};
  1987. if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  1988. goto out;
  1989. spin_lock(&info->lock);
  1990. #ifdef CONFIG_SWAP
  1991. swap = shmem_get_swap(info, index);
  1992. if (swap.val)
  1993. page = find_get_page(&swapper_space, swap.val);
  1994. else
  1995. #endif
  1996. page = find_get_page(inode->i_mapping, index);
  1997. spin_unlock(&info->lock);
  1998. out:
  1999. *pagep = page;
  2000. *swapp = swap;
  2001. }
  2002. #endif
  2003. #else /* !CONFIG_SHMEM */
  2004. /*
  2005. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  2006. *
  2007. * This is intended for small system where the benefits of the full
  2008. * shmem code (swap-backed and resource-limited) are outweighed by
  2009. * their complexity. On systems without swap this code should be
  2010. * effectively equivalent, but much lighter weight.
  2011. */
  2012. #include <linux/ramfs.h>
  2013. static struct file_system_type shmem_fs_type = {
  2014. .name = "tmpfs",
  2015. .mount = ramfs_mount,
  2016. .kill_sb = kill_litter_super,
  2017. };
  2018. int __init shmem_init(void)
  2019. {
  2020. BUG_ON(register_filesystem(&shmem_fs_type) != 0);
  2021. shm_mnt = kern_mount(&shmem_fs_type);
  2022. BUG_ON(IS_ERR(shm_mnt));
  2023. return 0;
  2024. }
  2025. int shmem_unuse(swp_entry_t swap, struct page *page)
  2026. {
  2027. return 0;
  2028. }
  2029. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  2030. {
  2031. return 0;
  2032. }
  2033. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  2034. {
  2035. truncate_inode_pages_range(inode->i_mapping, lstart, lend);
  2036. }
  2037. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  2038. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  2039. /**
  2040. * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file
  2041. * @inode: the inode to be searched
  2042. * @index: the page offset to be searched
  2043. * @pagep: the pointer for the found page to be stored
  2044. * @swapp: the pointer for the found swap entry to be stored
  2045. *
  2046. * If a page is found, refcount of it is incremented. Callers should handle
  2047. * these refcount.
  2048. */
  2049. void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index,
  2050. struct page **pagep, swp_entry_t *swapp)
  2051. {
  2052. struct page *page = NULL;
  2053. if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  2054. goto out;
  2055. page = find_get_page(inode->i_mapping, index);
  2056. out:
  2057. *pagep = page;
  2058. *swapp = (swp_entry_t){0};
  2059. }
  2060. #endif
  2061. #define shmem_vm_ops generic_file_vm_ops
  2062. #define shmem_file_operations ramfs_file_operations
  2063. #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
  2064. #define shmem_acct_size(flags, size) 0
  2065. #define shmem_unacct_size(flags, size) do {} while (0)
  2066. #endif /* CONFIG_SHMEM */
  2067. /* common code */
  2068. /**
  2069. * shmem_file_setup - get an unlinked file living in tmpfs
  2070. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2071. * @size: size to be set for the file
  2072. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2073. */
  2074. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  2075. {
  2076. int error;
  2077. struct file *file;
  2078. struct inode *inode;
  2079. struct path path;
  2080. struct dentry *root;
  2081. struct qstr this;
  2082. if (IS_ERR(shm_mnt))
  2083. return (void *)shm_mnt;
  2084. if (size < 0 || size > MAX_LFS_FILESIZE)
  2085. return ERR_PTR(-EINVAL);
  2086. if (shmem_acct_size(flags, size))
  2087. return ERR_PTR(-ENOMEM);
  2088. error = -ENOMEM;
  2089. this.name = name;
  2090. this.len = strlen(name);
  2091. this.hash = 0; /* will go */
  2092. root = shm_mnt->mnt_root;
  2093. path.dentry = d_alloc(root, &this);
  2094. if (!path.dentry)
  2095. goto put_memory;
  2096. path.mnt = mntget(shm_mnt);
  2097. error = -ENOSPC;
  2098. inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
  2099. if (!inode)
  2100. goto put_dentry;
  2101. d_instantiate(path.dentry, inode);
  2102. inode->i_size = size;
  2103. inode->i_nlink = 0; /* It is unlinked */
  2104. #ifndef CONFIG_MMU
  2105. error = ramfs_nommu_expand_for_mapping(inode, size);
  2106. if (error)
  2107. goto put_dentry;
  2108. #endif
  2109. error = -ENFILE;
  2110. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  2111. &shmem_file_operations);
  2112. if (!file)
  2113. goto put_dentry;
  2114. return file;
  2115. put_dentry:
  2116. path_put(&path);
  2117. put_memory:
  2118. shmem_unacct_size(flags, size);
  2119. return ERR_PTR(error);
  2120. }
  2121. EXPORT_SYMBOL_GPL(shmem_file_setup);
  2122. /**
  2123. * shmem_zero_setup - setup a shared anonymous mapping
  2124. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2125. */
  2126. int shmem_zero_setup(struct vm_area_struct *vma)
  2127. {
  2128. struct file *file;
  2129. loff_t size = vma->vm_end - vma->vm_start;
  2130. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2131. if (IS_ERR(file))
  2132. return PTR_ERR(file);
  2133. if (vma->vm_file)
  2134. fput(vma->vm_file);
  2135. vma->vm_file = file;
  2136. vma->vm_ops = &shmem_vm_ops;
  2137. vma->vm_flags |= VM_CAN_NONLINEAR;
  2138. return 0;
  2139. }
  2140. /**
  2141. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  2142. * @mapping: the page's address_space
  2143. * @index: the page index
  2144. * @gfp: the page allocator flags to use if allocating
  2145. *
  2146. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  2147. * with any new page allocations done using the specified allocation flags.
  2148. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  2149. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  2150. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  2151. *
  2152. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  2153. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  2154. */
  2155. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  2156. pgoff_t index, gfp_t gfp)
  2157. {
  2158. #ifdef CONFIG_SHMEM
  2159. struct inode *inode = mapping->host;
  2160. struct page *page;
  2161. int error;
  2162. BUG_ON(mapping->a_ops != &shmem_aops);
  2163. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
  2164. if (error)
  2165. page = ERR_PTR(error);
  2166. else
  2167. unlock_page(page);
  2168. return page;
  2169. #else
  2170. /*
  2171. * The tiny !SHMEM case uses ramfs without swap
  2172. */
  2173. return read_cache_page_gfp(mapping, index, gfp);
  2174. #endif
  2175. }
  2176. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);