shmem.c 89 KB

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