shmem.c 78 KB

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