shmem.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474
  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, true);
  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, int atomic_copy)
  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. if (atomic_copy)
  1275. inode->i_flags |= S_ATOMIC_COPY;
  1276. INIT_LIST_HEAD(&info->swaplist);
  1277. simple_xattrs_init(&info->xattrs);
  1278. cache_no_acl(inode);
  1279. switch (mode & S_IFMT) {
  1280. default:
  1281. inode->i_op = &shmem_special_inode_operations;
  1282. init_special_inode(inode, mode, dev);
  1283. break;
  1284. case S_IFREG:
  1285. inode->i_mapping->a_ops = &shmem_aops;
  1286. inode->i_op = &shmem_inode_operations;
  1287. inode->i_fop = &shmem_file_operations;
  1288. mpol_shared_policy_init(&info->policy,
  1289. shmem_get_sbmpol(sbinfo));
  1290. break;
  1291. case S_IFDIR:
  1292. inc_nlink(inode);
  1293. /* Some things misbehave if size == 0 on a directory */
  1294. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1295. inode->i_op = &shmem_dir_inode_operations;
  1296. inode->i_fop = &simple_dir_operations;
  1297. break;
  1298. case S_IFLNK:
  1299. /*
  1300. * Must not load anything in the rbtree,
  1301. * mpol_free_shared_policy will not be called.
  1302. */
  1303. mpol_shared_policy_init(&info->policy, NULL);
  1304. break;
  1305. }
  1306. } else
  1307. shmem_free_inode(sb);
  1308. return inode;
  1309. }
  1310. bool shmem_mapping(struct address_space *mapping)
  1311. {
  1312. return mapping->backing_dev_info == &shmem_backing_dev_info;
  1313. }
  1314. #ifdef CONFIG_TMPFS
  1315. static const struct inode_operations shmem_symlink_inode_operations;
  1316. static const struct inode_operations shmem_short_symlink_operations;
  1317. #ifdef CONFIG_TMPFS_XATTR
  1318. static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
  1319. #else
  1320. #define shmem_initxattrs NULL
  1321. #endif
  1322. static int
  1323. shmem_write_begin(struct file *file, struct address_space *mapping,
  1324. loff_t pos, unsigned len, unsigned flags,
  1325. struct page **pagep, void **fsdata)
  1326. {
  1327. struct inode *inode = mapping->host;
  1328. struct shmem_inode_info *info = SHMEM_I(inode);
  1329. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1330. /* i_mutex is held by caller */
  1331. if (unlikely(info->seals)) {
  1332. if (info->seals & F_SEAL_WRITE)
  1333. return -EPERM;
  1334. if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
  1335. return -EPERM;
  1336. }
  1337. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1338. }
  1339. static int
  1340. shmem_write_end(struct file *file, struct address_space *mapping,
  1341. loff_t pos, unsigned len, unsigned copied,
  1342. struct page *page, void *fsdata)
  1343. {
  1344. struct inode *inode = mapping->host;
  1345. if (pos + copied > inode->i_size)
  1346. i_size_write(inode, pos + copied);
  1347. if (!PageUptodate(page)) {
  1348. if (copied < PAGE_CACHE_SIZE) {
  1349. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  1350. zero_user_segments(page, 0, from,
  1351. from + copied, PAGE_CACHE_SIZE);
  1352. }
  1353. SetPageUptodate(page);
  1354. }
  1355. set_page_dirty(page);
  1356. unlock_page(page);
  1357. page_cache_release(page);
  1358. return copied;
  1359. }
  1360. static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1361. {
  1362. struct file *file = iocb->ki_filp;
  1363. struct inode *inode = file_inode(file);
  1364. struct address_space *mapping = inode->i_mapping;
  1365. pgoff_t index;
  1366. unsigned long offset;
  1367. enum sgp_type sgp = SGP_READ;
  1368. int error = 0;
  1369. ssize_t retval = 0;
  1370. loff_t *ppos = &iocb->ki_pos;
  1371. /*
  1372. * Might this read be for a stacking filesystem? Then when reading
  1373. * holes of a sparse file, we actually need to allocate those pages,
  1374. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  1375. */
  1376. if (segment_eq(get_fs(), KERNEL_DS))
  1377. sgp = SGP_DIRTY;
  1378. index = *ppos >> PAGE_CACHE_SHIFT;
  1379. offset = *ppos & ~PAGE_CACHE_MASK;
  1380. for (;;) {
  1381. struct page *page = NULL;
  1382. pgoff_t end_index;
  1383. unsigned long nr, ret;
  1384. loff_t i_size = i_size_read(inode);
  1385. end_index = i_size >> PAGE_CACHE_SHIFT;
  1386. if (index > end_index)
  1387. break;
  1388. if (index == end_index) {
  1389. nr = i_size & ~PAGE_CACHE_MASK;
  1390. if (nr <= offset)
  1391. break;
  1392. }
  1393. error = shmem_getpage(inode, index, &page, sgp, NULL);
  1394. if (error) {
  1395. if (error == -EINVAL)
  1396. error = 0;
  1397. break;
  1398. }
  1399. if (page)
  1400. unlock_page(page);
  1401. /*
  1402. * We must evaluate after, since reads (unlike writes)
  1403. * are called without i_mutex protection against truncate
  1404. */
  1405. nr = PAGE_CACHE_SIZE;
  1406. i_size = i_size_read(inode);
  1407. end_index = i_size >> PAGE_CACHE_SHIFT;
  1408. if (index == end_index) {
  1409. nr = i_size & ~PAGE_CACHE_MASK;
  1410. if (nr <= offset) {
  1411. if (page)
  1412. page_cache_release(page);
  1413. break;
  1414. }
  1415. }
  1416. nr -= offset;
  1417. if (page) {
  1418. /*
  1419. * If users can be writing to this page using arbitrary
  1420. * virtual addresses, take care about potential aliasing
  1421. * before reading the page on the kernel side.
  1422. */
  1423. if (mapping_writably_mapped(mapping))
  1424. flush_dcache_page(page);
  1425. /*
  1426. * Mark the page accessed if we read the beginning.
  1427. */
  1428. if (!offset)
  1429. mark_page_accessed(page);
  1430. } else {
  1431. page = ZERO_PAGE(0);
  1432. page_cache_get(page);
  1433. }
  1434. /*
  1435. * Ok, we have the page, and it's up-to-date, so
  1436. * now we can copy it to user space...
  1437. */
  1438. ret = copy_page_to_iter(page, offset, nr, to);
  1439. retval += ret;
  1440. offset += ret;
  1441. index += offset >> PAGE_CACHE_SHIFT;
  1442. offset &= ~PAGE_CACHE_MASK;
  1443. page_cache_release(page);
  1444. if (!iov_iter_count(to))
  1445. break;
  1446. if (ret < nr) {
  1447. error = -EFAULT;
  1448. break;
  1449. }
  1450. cond_resched();
  1451. }
  1452. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1453. file_accessed(file);
  1454. return retval ? retval : error;
  1455. }
  1456. static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
  1457. struct pipe_inode_info *pipe, size_t len,
  1458. unsigned int flags)
  1459. {
  1460. struct address_space *mapping = in->f_mapping;
  1461. struct inode *inode = mapping->host;
  1462. unsigned int loff, nr_pages, req_pages;
  1463. struct page *pages[PIPE_DEF_BUFFERS];
  1464. struct partial_page partial[PIPE_DEF_BUFFERS];
  1465. struct page *page;
  1466. pgoff_t index, end_index;
  1467. loff_t isize, left;
  1468. int error, page_nr;
  1469. struct splice_pipe_desc spd = {
  1470. .pages = pages,
  1471. .partial = partial,
  1472. .nr_pages_max = PIPE_DEF_BUFFERS,
  1473. .flags = flags,
  1474. .ops = &page_cache_pipe_buf_ops,
  1475. .spd_release = spd_release_page,
  1476. };
  1477. isize = i_size_read(inode);
  1478. if (unlikely(*ppos >= isize))
  1479. return 0;
  1480. left = isize - *ppos;
  1481. if (unlikely(left < len))
  1482. len = left;
  1483. if (splice_grow_spd(pipe, &spd))
  1484. return -ENOMEM;
  1485. index = *ppos >> PAGE_CACHE_SHIFT;
  1486. loff = *ppos & ~PAGE_CACHE_MASK;
  1487. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1488. nr_pages = min(req_pages, spd.nr_pages_max);
  1489. spd.nr_pages = find_get_pages_contig(mapping, index,
  1490. nr_pages, spd.pages);
  1491. index += spd.nr_pages;
  1492. error = 0;
  1493. while (spd.nr_pages < nr_pages) {
  1494. error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
  1495. if (error)
  1496. break;
  1497. unlock_page(page);
  1498. spd.pages[spd.nr_pages++] = page;
  1499. index++;
  1500. }
  1501. index = *ppos >> PAGE_CACHE_SHIFT;
  1502. nr_pages = spd.nr_pages;
  1503. spd.nr_pages = 0;
  1504. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  1505. unsigned int this_len;
  1506. if (!len)
  1507. break;
  1508. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  1509. page = spd.pages[page_nr];
  1510. if (!PageUptodate(page) || page->mapping != mapping) {
  1511. error = shmem_getpage(inode, index, &page,
  1512. SGP_CACHE, NULL);
  1513. if (error)
  1514. break;
  1515. unlock_page(page);
  1516. page_cache_release(spd.pages[page_nr]);
  1517. spd.pages[page_nr] = page;
  1518. }
  1519. isize = i_size_read(inode);
  1520. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1521. if (unlikely(!isize || index > end_index))
  1522. break;
  1523. if (end_index == index) {
  1524. unsigned int plen;
  1525. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1526. if (plen <= loff)
  1527. break;
  1528. this_len = min(this_len, plen - loff);
  1529. len = this_len;
  1530. }
  1531. spd.partial[page_nr].offset = loff;
  1532. spd.partial[page_nr].len = this_len;
  1533. len -= this_len;
  1534. loff = 0;
  1535. spd.nr_pages++;
  1536. index++;
  1537. }
  1538. while (page_nr < nr_pages)
  1539. page_cache_release(spd.pages[page_nr++]);
  1540. if (spd.nr_pages)
  1541. error = splice_to_pipe(pipe, &spd);
  1542. splice_shrink_spd(&spd);
  1543. if (error > 0) {
  1544. *ppos += error;
  1545. file_accessed(in);
  1546. }
  1547. return error;
  1548. }
  1549. /*
  1550. * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
  1551. */
  1552. static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
  1553. pgoff_t index, pgoff_t end, int whence)
  1554. {
  1555. struct page *page;
  1556. struct pagevec pvec;
  1557. pgoff_t indices[PAGEVEC_SIZE];
  1558. bool done = false;
  1559. int i;
  1560. pagevec_init(&pvec, 0);
  1561. pvec.nr = 1; /* start small: we may be there already */
  1562. while (!done) {
  1563. pvec.nr = find_get_entries(mapping, index,
  1564. pvec.nr, pvec.pages, indices);
  1565. if (!pvec.nr) {
  1566. if (whence == SEEK_DATA)
  1567. index = end;
  1568. break;
  1569. }
  1570. for (i = 0; i < pvec.nr; i++, index++) {
  1571. if (index < indices[i]) {
  1572. if (whence == SEEK_HOLE) {
  1573. done = true;
  1574. break;
  1575. }
  1576. index = indices[i];
  1577. }
  1578. page = pvec.pages[i];
  1579. if (page && !radix_tree_exceptional_entry(page)) {
  1580. if (!PageUptodate(page))
  1581. page = NULL;
  1582. }
  1583. if (index >= end ||
  1584. (page && whence == SEEK_DATA) ||
  1585. (!page && whence == SEEK_HOLE)) {
  1586. done = true;
  1587. break;
  1588. }
  1589. }
  1590. pagevec_remove_exceptionals(&pvec);
  1591. pagevec_release(&pvec);
  1592. pvec.nr = PAGEVEC_SIZE;
  1593. cond_resched();
  1594. }
  1595. return index;
  1596. }
  1597. static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
  1598. {
  1599. struct address_space *mapping = file->f_mapping;
  1600. struct inode *inode = mapping->host;
  1601. pgoff_t start, end;
  1602. loff_t new_offset;
  1603. if (whence != SEEK_DATA && whence != SEEK_HOLE)
  1604. return generic_file_llseek_size(file, offset, whence,
  1605. MAX_LFS_FILESIZE, i_size_read(inode));
  1606. mutex_lock(&inode->i_mutex);
  1607. /* We're holding i_mutex so we can access i_size directly */
  1608. if (offset < 0)
  1609. offset = -EINVAL;
  1610. else if (offset >= inode->i_size)
  1611. offset = -ENXIO;
  1612. else {
  1613. start = offset >> PAGE_CACHE_SHIFT;
  1614. end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1615. new_offset = shmem_seek_hole_data(mapping, start, end, whence);
  1616. new_offset <<= PAGE_CACHE_SHIFT;
  1617. if (new_offset > offset) {
  1618. if (new_offset < inode->i_size)
  1619. offset = new_offset;
  1620. else if (whence == SEEK_DATA)
  1621. offset = -ENXIO;
  1622. else
  1623. offset = inode->i_size;
  1624. }
  1625. }
  1626. if (offset >= 0)
  1627. offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
  1628. mutex_unlock(&inode->i_mutex);
  1629. return offset;
  1630. }
  1631. /*
  1632. * We need a tag: a new tag would expand every radix_tree_node by 8 bytes,
  1633. * so reuse a tag which we firmly believe is never set or cleared on shmem.
  1634. */
  1635. #define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE
  1636. #define LAST_SCAN 4 /* about 150ms max */
  1637. static void shmem_tag_pins(struct address_space *mapping)
  1638. {
  1639. struct radix_tree_iter iter;
  1640. void **slot;
  1641. pgoff_t start;
  1642. struct page *page;
  1643. lru_add_drain();
  1644. start = 0;
  1645. rcu_read_lock();
  1646. restart:
  1647. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
  1648. page = radix_tree_deref_slot(slot);
  1649. if (!page || radix_tree_exception(page)) {
  1650. if (radix_tree_deref_retry(page))
  1651. goto restart;
  1652. } else if (page_count(page) - page_mapcount(page) > 1) {
  1653. spin_lock_irq(&mapping->tree_lock);
  1654. radix_tree_tag_set(&mapping->page_tree, iter.index,
  1655. SHMEM_TAG_PINNED);
  1656. spin_unlock_irq(&mapping->tree_lock);
  1657. }
  1658. if (need_resched()) {
  1659. cond_resched_rcu();
  1660. start = iter.index + 1;
  1661. goto restart;
  1662. }
  1663. }
  1664. rcu_read_unlock();
  1665. }
  1666. /*
  1667. * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
  1668. * via get_user_pages(), drivers might have some pending I/O without any active
  1669. * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
  1670. * and see whether it has an elevated ref-count. If so, we tag them and wait for
  1671. * them to be dropped.
  1672. * The caller must guarantee that no new user will acquire writable references
  1673. * to those pages to avoid races.
  1674. */
  1675. static int shmem_wait_for_pins(struct address_space *mapping)
  1676. {
  1677. struct radix_tree_iter iter;
  1678. void **slot;
  1679. pgoff_t start;
  1680. struct page *page;
  1681. int error, scan;
  1682. shmem_tag_pins(mapping);
  1683. error = 0;
  1684. for (scan = 0; scan <= LAST_SCAN; scan++) {
  1685. if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED))
  1686. break;
  1687. if (!scan)
  1688. lru_add_drain_all();
  1689. else if (schedule_timeout_killable((HZ << scan) / 200))
  1690. scan = LAST_SCAN;
  1691. start = 0;
  1692. rcu_read_lock();
  1693. restart:
  1694. radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter,
  1695. start, SHMEM_TAG_PINNED) {
  1696. page = radix_tree_deref_slot(slot);
  1697. if (radix_tree_exception(page)) {
  1698. if (radix_tree_deref_retry(page))
  1699. goto restart;
  1700. page = NULL;
  1701. }
  1702. if (page &&
  1703. page_count(page) - page_mapcount(page) != 1) {
  1704. if (scan < LAST_SCAN)
  1705. goto continue_resched;
  1706. /*
  1707. * On the last scan, we clean up all those tags
  1708. * we inserted; but make a note that we still
  1709. * found pages pinned.
  1710. */
  1711. error = -EBUSY;
  1712. }
  1713. spin_lock_irq(&mapping->tree_lock);
  1714. radix_tree_tag_clear(&mapping->page_tree,
  1715. iter.index, SHMEM_TAG_PINNED);
  1716. spin_unlock_irq(&mapping->tree_lock);
  1717. continue_resched:
  1718. if (need_resched()) {
  1719. cond_resched_rcu();
  1720. start = iter.index + 1;
  1721. goto restart;
  1722. }
  1723. }
  1724. rcu_read_unlock();
  1725. }
  1726. return error;
  1727. }
  1728. #define F_ALL_SEALS (F_SEAL_SEAL | \
  1729. F_SEAL_SHRINK | \
  1730. F_SEAL_GROW | \
  1731. F_SEAL_WRITE)
  1732. int shmem_add_seals(struct file *file, unsigned int seals)
  1733. {
  1734. struct inode *inode = file_inode(file);
  1735. struct shmem_inode_info *info = SHMEM_I(inode);
  1736. int error;
  1737. /*
  1738. * SEALING
  1739. * Sealing allows multiple parties to share a shmem-file but restrict
  1740. * access to a specific subset of file operations. Seals can only be
  1741. * added, but never removed. This way, mutually untrusted parties can
  1742. * share common memory regions with a well-defined policy. A malicious
  1743. * peer can thus never perform unwanted operations on a shared object.
  1744. *
  1745. * Seals are only supported on special shmem-files and always affect
  1746. * the whole underlying inode. Once a seal is set, it may prevent some
  1747. * kinds of access to the file. Currently, the following seals are
  1748. * defined:
  1749. * SEAL_SEAL: Prevent further seals from being set on this file
  1750. * SEAL_SHRINK: Prevent the file from shrinking
  1751. * SEAL_GROW: Prevent the file from growing
  1752. * SEAL_WRITE: Prevent write access to the file
  1753. *
  1754. * As we don't require any trust relationship between two parties, we
  1755. * must prevent seals from being removed. Therefore, sealing a file
  1756. * only adds a given set of seals to the file, it never touches
  1757. * existing seals. Furthermore, the "setting seals"-operation can be
  1758. * sealed itself, which basically prevents any further seal from being
  1759. * added.
  1760. *
  1761. * Semantics of sealing are only defined on volatile files. Only
  1762. * anonymous shmem files support sealing. More importantly, seals are
  1763. * never written to disk. Therefore, there's no plan to support it on
  1764. * other file types.
  1765. */
  1766. if (file->f_op != &shmem_file_operations)
  1767. return -EINVAL;
  1768. if (!(file->f_mode & FMODE_WRITE))
  1769. return -EPERM;
  1770. if (seals & ~(unsigned int)F_ALL_SEALS)
  1771. return -EINVAL;
  1772. mutex_lock(&inode->i_mutex);
  1773. if (info->seals & F_SEAL_SEAL) {
  1774. error = -EPERM;
  1775. goto unlock;
  1776. }
  1777. if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
  1778. error = mapping_deny_writable(file->f_mapping);
  1779. if (error)
  1780. goto unlock;
  1781. error = shmem_wait_for_pins(file->f_mapping);
  1782. if (error) {
  1783. mapping_allow_writable(file->f_mapping);
  1784. goto unlock;
  1785. }
  1786. }
  1787. info->seals |= seals;
  1788. error = 0;
  1789. unlock:
  1790. mutex_unlock(&inode->i_mutex);
  1791. return error;
  1792. }
  1793. EXPORT_SYMBOL_GPL(shmem_add_seals);
  1794. int shmem_get_seals(struct file *file)
  1795. {
  1796. if (file->f_op != &shmem_file_operations)
  1797. return -EINVAL;
  1798. return SHMEM_I(file_inode(file))->seals;
  1799. }
  1800. EXPORT_SYMBOL_GPL(shmem_get_seals);
  1801. long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  1802. {
  1803. long error;
  1804. switch (cmd) {
  1805. case F_ADD_SEALS:
  1806. /* disallow upper 32bit */
  1807. if (arg > UINT_MAX)
  1808. return -EINVAL;
  1809. error = shmem_add_seals(file, arg);
  1810. break;
  1811. case F_GET_SEALS:
  1812. error = shmem_get_seals(file);
  1813. break;
  1814. default:
  1815. error = -EINVAL;
  1816. break;
  1817. }
  1818. return error;
  1819. }
  1820. static long shmem_fallocate(struct file *file, int mode, loff_t offset,
  1821. loff_t len)
  1822. {
  1823. struct inode *inode = file_inode(file);
  1824. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1825. struct shmem_inode_info *info = SHMEM_I(inode);
  1826. struct shmem_falloc shmem_falloc;
  1827. pgoff_t start, index, end;
  1828. int error;
  1829. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1830. return -EOPNOTSUPP;
  1831. /*To prevent nested lock under memory reclaim*/
  1832. if (!mutex_trylock(&inode->i_mutex))
  1833. return -1;
  1834. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1835. struct address_space *mapping = file->f_mapping;
  1836. loff_t unmap_start = round_up(offset, PAGE_SIZE);
  1837. loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
  1838. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
  1839. /* protected by i_mutex */
  1840. if (info->seals & F_SEAL_WRITE) {
  1841. error = -EPERM;
  1842. goto out;
  1843. }
  1844. shmem_falloc.waitq = &shmem_falloc_waitq;
  1845. shmem_falloc.start = unmap_start >> PAGE_SHIFT;
  1846. shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
  1847. spin_lock(&inode->i_lock);
  1848. inode->i_private = &shmem_falloc;
  1849. spin_unlock(&inode->i_lock);
  1850. if ((u64)unmap_end > (u64)unmap_start)
  1851. unmap_mapping_range(mapping, unmap_start,
  1852. 1 + unmap_end - unmap_start, 0);
  1853. shmem_truncate_range(inode, offset, offset + len - 1);
  1854. /* No need to unmap again: hole-punching leaves COWed pages */
  1855. spin_lock(&inode->i_lock);
  1856. inode->i_private = NULL;
  1857. wake_up_all(&shmem_falloc_waitq);
  1858. spin_unlock(&inode->i_lock);
  1859. error = 0;
  1860. goto out;
  1861. }
  1862. /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
  1863. error = inode_newsize_ok(inode, offset + len);
  1864. if (error)
  1865. goto out;
  1866. if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
  1867. error = -EPERM;
  1868. goto out;
  1869. }
  1870. start = offset >> PAGE_CACHE_SHIFT;
  1871. end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1872. /* Try to avoid a swapstorm if len is impossible to satisfy */
  1873. if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
  1874. error = -ENOSPC;
  1875. goto out;
  1876. }
  1877. shmem_falloc.waitq = NULL;
  1878. shmem_falloc.start = start;
  1879. shmem_falloc.next = start;
  1880. shmem_falloc.nr_falloced = 0;
  1881. shmem_falloc.nr_unswapped = 0;
  1882. spin_lock(&inode->i_lock);
  1883. inode->i_private = &shmem_falloc;
  1884. spin_unlock(&inode->i_lock);
  1885. for (index = start; index < end; index++) {
  1886. struct page *page;
  1887. /*
  1888. * Good, the fallocate(2) manpage permits EINTR: we may have
  1889. * been interrupted because we are using up too much memory.
  1890. */
  1891. if (signal_pending(current))
  1892. error = -EINTR;
  1893. else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
  1894. error = -ENOMEM;
  1895. else
  1896. error = shmem_getpage(inode, index, &page, SGP_FALLOC,
  1897. NULL);
  1898. if (error) {
  1899. /* Remove the !PageUptodate pages we added */
  1900. shmem_undo_range(inode,
  1901. (loff_t)start << PAGE_CACHE_SHIFT,
  1902. (loff_t)index << PAGE_CACHE_SHIFT, true);
  1903. goto undone;
  1904. }
  1905. /*
  1906. * Inform shmem_writepage() how far we have reached.
  1907. * No need for lock or barrier: we have the page lock.
  1908. */
  1909. shmem_falloc.next++;
  1910. if (!PageUptodate(page))
  1911. shmem_falloc.nr_falloced++;
  1912. /*
  1913. * If !PageUptodate, leave it that way so that freeable pages
  1914. * can be recognized if we need to rollback on error later.
  1915. * But set_page_dirty so that memory pressure will swap rather
  1916. * than free the pages we are allocating (and SGP_CACHE pages
  1917. * might still be clean: we now need to mark those dirty too).
  1918. */
  1919. set_page_dirty(page);
  1920. unlock_page(page);
  1921. page_cache_release(page);
  1922. cond_resched();
  1923. }
  1924. if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
  1925. i_size_write(inode, offset + len);
  1926. inode->i_ctime = CURRENT_TIME;
  1927. undone:
  1928. spin_lock(&inode->i_lock);
  1929. inode->i_private = NULL;
  1930. spin_unlock(&inode->i_lock);
  1931. out:
  1932. mutex_unlock(&inode->i_mutex);
  1933. return error;
  1934. }
  1935. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1936. {
  1937. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1938. buf->f_type = TMPFS_MAGIC;
  1939. buf->f_bsize = PAGE_CACHE_SIZE;
  1940. buf->f_namelen = NAME_MAX;
  1941. if (sbinfo->max_blocks) {
  1942. buf->f_blocks = sbinfo->max_blocks;
  1943. buf->f_bavail =
  1944. buf->f_bfree = sbinfo->max_blocks -
  1945. percpu_counter_sum(&sbinfo->used_blocks);
  1946. }
  1947. if (sbinfo->max_inodes) {
  1948. buf->f_files = sbinfo->max_inodes;
  1949. buf->f_ffree = sbinfo->free_inodes;
  1950. }
  1951. /* else leave those fields 0 like simple_statfs */
  1952. return 0;
  1953. }
  1954. /*
  1955. * File creation. Allocate an inode, and we're done..
  1956. */
  1957. static int
  1958. shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  1959. {
  1960. struct inode *inode;
  1961. int error = -ENOSPC;
  1962. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE, 0);
  1963. if (inode) {
  1964. error = simple_acl_create(dir, inode);
  1965. if (error)
  1966. goto out_iput;
  1967. error = security_inode_init_security(inode, dir,
  1968. &dentry->d_name,
  1969. shmem_initxattrs, NULL);
  1970. if (error && error != -EOPNOTSUPP)
  1971. goto out_iput;
  1972. error = 0;
  1973. dir->i_size += BOGO_DIRENT_SIZE;
  1974. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1975. d_instantiate(dentry, inode);
  1976. dget(dentry); /* Extra count - pin the dentry in core */
  1977. }
  1978. return error;
  1979. out_iput:
  1980. iput(inode);
  1981. return error;
  1982. }
  1983. static int
  1984. shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  1985. {
  1986. struct inode *inode;
  1987. int error = -ENOSPC;
  1988. inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE, 0);
  1989. if (inode) {
  1990. error = security_inode_init_security(inode, dir,
  1991. NULL,
  1992. shmem_initxattrs, NULL);
  1993. if (error && error != -EOPNOTSUPP)
  1994. goto out_iput;
  1995. error = simple_acl_create(dir, inode);
  1996. if (error)
  1997. goto out_iput;
  1998. d_tmpfile(dentry, inode);
  1999. }
  2000. return error;
  2001. out_iput:
  2002. iput(inode);
  2003. return error;
  2004. }
  2005. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  2006. {
  2007. int error;
  2008. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  2009. return error;
  2010. inc_nlink(dir);
  2011. return 0;
  2012. }
  2013. static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2014. bool excl)
  2015. {
  2016. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  2017. }
  2018. /*
  2019. * Link a file..
  2020. */
  2021. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  2022. {
  2023. struct inode *inode = old_dentry->d_inode;
  2024. int ret;
  2025. /*
  2026. * No ordinary (disk based) filesystem counts links as inodes;
  2027. * but each new link needs a new dentry, pinning lowmem, and
  2028. * tmpfs dentries cannot be pruned until they are unlinked.
  2029. */
  2030. ret = shmem_reserve_inode(inode->i_sb);
  2031. if (ret)
  2032. goto out;
  2033. dir->i_size += BOGO_DIRENT_SIZE;
  2034. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  2035. inc_nlink(inode);
  2036. ihold(inode); /* New dentry reference */
  2037. dget(dentry); /* Extra pinning count for the created dentry */
  2038. d_instantiate(dentry, inode);
  2039. out:
  2040. return ret;
  2041. }
  2042. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  2043. {
  2044. struct inode *inode = dentry->d_inode;
  2045. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  2046. shmem_free_inode(inode->i_sb);
  2047. dir->i_size -= BOGO_DIRENT_SIZE;
  2048. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  2049. drop_nlink(inode);
  2050. dput(dentry); /* Undo the count from "create" - this does all the work */
  2051. return 0;
  2052. }
  2053. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  2054. {
  2055. if (!simple_empty(dentry))
  2056. return -ENOTEMPTY;
  2057. drop_nlink(dentry->d_inode);
  2058. drop_nlink(dir);
  2059. return shmem_unlink(dir, dentry);
  2060. }
  2061. static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  2062. {
  2063. bool old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2064. bool new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
  2065. if (old_dir != new_dir && old_is_dir != new_is_dir) {
  2066. if (old_is_dir) {
  2067. drop_nlink(old_dir);
  2068. inc_nlink(new_dir);
  2069. } else {
  2070. drop_nlink(new_dir);
  2071. inc_nlink(old_dir);
  2072. }
  2073. }
  2074. old_dir->i_ctime = old_dir->i_mtime =
  2075. new_dir->i_ctime = new_dir->i_mtime =
  2076. old_dentry->d_inode->i_ctime =
  2077. new_dentry->d_inode->i_ctime = CURRENT_TIME;
  2078. return 0;
  2079. }
  2080. static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
  2081. {
  2082. struct dentry *whiteout;
  2083. int error;
  2084. whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
  2085. if (!whiteout)
  2086. return -ENOMEM;
  2087. error = shmem_mknod(old_dir, whiteout,
  2088. S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
  2089. dput(whiteout);
  2090. if (error)
  2091. return error;
  2092. /*
  2093. * Cheat and hash the whiteout while the old dentry is still in
  2094. * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
  2095. *
  2096. * d_lookup() will consistently find one of them at this point,
  2097. * not sure which one, but that isn't even important.
  2098. */
  2099. d_rehash(whiteout);
  2100. return 0;
  2101. }
  2102. /*
  2103. * The VFS layer already does all the dentry stuff for rename,
  2104. * we just have to decrement the usage count for the target if
  2105. * it exists so that the VFS layer correctly free's it when it
  2106. * gets overwritten.
  2107. */
  2108. static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags)
  2109. {
  2110. struct inode *inode = old_dentry->d_inode;
  2111. int they_are_dirs = S_ISDIR(inode->i_mode);
  2112. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  2113. return -EINVAL;
  2114. if (flags & RENAME_EXCHANGE)
  2115. return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
  2116. if (!simple_empty(new_dentry))
  2117. return -ENOTEMPTY;
  2118. if (flags & RENAME_WHITEOUT) {
  2119. int error;
  2120. error = shmem_whiteout(old_dir, old_dentry);
  2121. if (error)
  2122. return error;
  2123. }
  2124. if (new_dentry->d_inode) {
  2125. (void) shmem_unlink(new_dir, new_dentry);
  2126. if (they_are_dirs) {
  2127. drop_nlink(new_dentry->d_inode);
  2128. drop_nlink(old_dir);
  2129. }
  2130. } else if (they_are_dirs) {
  2131. drop_nlink(old_dir);
  2132. inc_nlink(new_dir);
  2133. }
  2134. old_dir->i_size -= BOGO_DIRENT_SIZE;
  2135. new_dir->i_size += BOGO_DIRENT_SIZE;
  2136. old_dir->i_ctime = old_dir->i_mtime =
  2137. new_dir->i_ctime = new_dir->i_mtime =
  2138. inode->i_ctime = CURRENT_TIME;
  2139. return 0;
  2140. }
  2141. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  2142. {
  2143. int error;
  2144. int len;
  2145. struct inode *inode;
  2146. struct page *page;
  2147. char *kaddr;
  2148. struct shmem_inode_info *info;
  2149. len = strlen(symname) + 1;
  2150. if (len > PAGE_CACHE_SIZE)
  2151. return -ENAMETOOLONG;
  2152. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE, 0);
  2153. if (!inode)
  2154. return -ENOSPC;
  2155. error = security_inode_init_security(inode, dir, &dentry->d_name,
  2156. shmem_initxattrs, NULL);
  2157. if (error) {
  2158. if (error != -EOPNOTSUPP) {
  2159. iput(inode);
  2160. return error;
  2161. }
  2162. error = 0;
  2163. }
  2164. info = SHMEM_I(inode);
  2165. inode->i_size = len-1;
  2166. if (len <= SHORT_SYMLINK_LEN) {
  2167. info->symlink = kmemdup(symname, len, GFP_KERNEL);
  2168. if (!info->symlink) {
  2169. iput(inode);
  2170. return -ENOMEM;
  2171. }
  2172. inode->i_op = &shmem_short_symlink_operations;
  2173. } else {
  2174. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  2175. if (error) {
  2176. iput(inode);
  2177. return error;
  2178. }
  2179. inode->i_mapping->a_ops = &shmem_aops;
  2180. inode->i_op = &shmem_symlink_inode_operations;
  2181. kaddr = kmap_atomic(page);
  2182. memcpy(kaddr, symname, len);
  2183. kunmap_atomic(kaddr);
  2184. SetPageUptodate(page);
  2185. set_page_dirty(page);
  2186. unlock_page(page);
  2187. page_cache_release(page);
  2188. }
  2189. dir->i_size += BOGO_DIRENT_SIZE;
  2190. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  2191. d_instantiate(dentry, inode);
  2192. dget(dentry);
  2193. return 0;
  2194. }
  2195. static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
  2196. {
  2197. nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
  2198. return NULL;
  2199. }
  2200. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  2201. {
  2202. struct page *page = NULL;
  2203. int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  2204. nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
  2205. if (page)
  2206. unlock_page(page);
  2207. return page;
  2208. }
  2209. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2210. {
  2211. if (!IS_ERR(nd_get_link(nd))) {
  2212. struct page *page = cookie;
  2213. kunmap(page);
  2214. mark_page_accessed(page);
  2215. page_cache_release(page);
  2216. }
  2217. }
  2218. #ifdef CONFIG_TMPFS_XATTR
  2219. /*
  2220. * Superblocks without xattr inode operations may get some security.* xattr
  2221. * support from the LSM "for free". As soon as we have any other xattrs
  2222. * like ACLs, we also need to implement the security.* handlers at
  2223. * filesystem level, though.
  2224. */
  2225. /*
  2226. * Callback for security_inode_init_security() for acquiring xattrs.
  2227. */
  2228. static int shmem_initxattrs(struct inode *inode,
  2229. const struct xattr *xattr_array,
  2230. void *fs_info)
  2231. {
  2232. struct shmem_inode_info *info = SHMEM_I(inode);
  2233. const struct xattr *xattr;
  2234. struct simple_xattr *new_xattr;
  2235. size_t len;
  2236. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  2237. new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
  2238. if (!new_xattr)
  2239. return -ENOMEM;
  2240. len = strlen(xattr->name) + 1;
  2241. new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
  2242. GFP_KERNEL);
  2243. if (!new_xattr->name) {
  2244. kfree(new_xattr);
  2245. return -ENOMEM;
  2246. }
  2247. memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
  2248. XATTR_SECURITY_PREFIX_LEN);
  2249. memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
  2250. xattr->name, len);
  2251. simple_xattr_list_add(&info->xattrs, new_xattr);
  2252. }
  2253. return 0;
  2254. }
  2255. static const struct xattr_handler *shmem_xattr_handlers[] = {
  2256. #ifdef CONFIG_TMPFS_POSIX_ACL
  2257. &posix_acl_access_xattr_handler,
  2258. &posix_acl_default_xattr_handler,
  2259. #endif
  2260. NULL
  2261. };
  2262. static int shmem_xattr_validate(const char *name)
  2263. {
  2264. struct { const char *prefix; size_t len; } arr[] = {
  2265. { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
  2266. { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
  2267. };
  2268. int i;
  2269. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  2270. size_t preflen = arr[i].len;
  2271. if (strncmp(name, arr[i].prefix, preflen) == 0) {
  2272. if (!name[preflen])
  2273. return -EINVAL;
  2274. return 0;
  2275. }
  2276. }
  2277. return -EOPNOTSUPP;
  2278. }
  2279. static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
  2280. void *buffer, size_t size)
  2281. {
  2282. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2283. int err;
  2284. /*
  2285. * If this is a request for a synthetic attribute in the system.*
  2286. * namespace use the generic infrastructure to resolve a handler
  2287. * for it via sb->s_xattr.
  2288. */
  2289. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2290. return generic_getxattr(dentry, name, buffer, size);
  2291. err = shmem_xattr_validate(name);
  2292. if (err)
  2293. return err;
  2294. return simple_xattr_get(&info->xattrs, name, buffer, size);
  2295. }
  2296. static int shmem_setxattr(struct dentry *dentry, const char *name,
  2297. const void *value, size_t size, int flags)
  2298. {
  2299. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2300. int err;
  2301. /*
  2302. * If this is a request for a synthetic attribute in the system.*
  2303. * namespace use the generic infrastructure to resolve a handler
  2304. * for it via sb->s_xattr.
  2305. */
  2306. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2307. return generic_setxattr(dentry, name, value, size, flags);
  2308. err = shmem_xattr_validate(name);
  2309. if (err)
  2310. return err;
  2311. return simple_xattr_set(&info->xattrs, name, value, size, flags);
  2312. }
  2313. static int shmem_removexattr(struct dentry *dentry, const char *name)
  2314. {
  2315. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2316. int err;
  2317. /*
  2318. * If this is a request for a synthetic attribute in the system.*
  2319. * namespace use the generic infrastructure to resolve a handler
  2320. * for it via sb->s_xattr.
  2321. */
  2322. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2323. return generic_removexattr(dentry, name);
  2324. err = shmem_xattr_validate(name);
  2325. if (err)
  2326. return err;
  2327. return simple_xattr_remove(&info->xattrs, name);
  2328. }
  2329. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  2330. {
  2331. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2332. return simple_xattr_list(&info->xattrs, buffer, size);
  2333. }
  2334. #endif /* CONFIG_TMPFS_XATTR */
  2335. static const struct inode_operations shmem_short_symlink_operations = {
  2336. .readlink = generic_readlink,
  2337. .follow_link = shmem_follow_short_symlink,
  2338. #ifdef CONFIG_TMPFS_XATTR
  2339. .setxattr = shmem_setxattr,
  2340. .getxattr = shmem_getxattr,
  2341. .listxattr = shmem_listxattr,
  2342. .removexattr = shmem_removexattr,
  2343. #endif
  2344. };
  2345. static const struct inode_operations shmem_symlink_inode_operations = {
  2346. .readlink = generic_readlink,
  2347. .follow_link = shmem_follow_link,
  2348. .put_link = shmem_put_link,
  2349. #ifdef CONFIG_TMPFS_XATTR
  2350. .setxattr = shmem_setxattr,
  2351. .getxattr = shmem_getxattr,
  2352. .listxattr = shmem_listxattr,
  2353. .removexattr = shmem_removexattr,
  2354. #endif
  2355. };
  2356. static struct dentry *shmem_get_parent(struct dentry *child)
  2357. {
  2358. return ERR_PTR(-ESTALE);
  2359. }
  2360. static int shmem_match(struct inode *ino, void *vfh)
  2361. {
  2362. __u32 *fh = vfh;
  2363. __u64 inum = fh[2];
  2364. inum = (inum << 32) | fh[1];
  2365. return ino->i_ino == inum && fh[0] == ino->i_generation;
  2366. }
  2367. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  2368. struct fid *fid, int fh_len, int fh_type)
  2369. {
  2370. struct inode *inode;
  2371. struct dentry *dentry = NULL;
  2372. u64 inum;
  2373. if (fh_len < 3)
  2374. return NULL;
  2375. inum = fid->raw[2];
  2376. inum = (inum << 32) | fid->raw[1];
  2377. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  2378. shmem_match, fid->raw);
  2379. if (inode) {
  2380. dentry = d_find_alias(inode);
  2381. iput(inode);
  2382. }
  2383. return dentry;
  2384. }
  2385. static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
  2386. struct inode *parent)
  2387. {
  2388. if (*len < 3) {
  2389. *len = 3;
  2390. return FILEID_INVALID;
  2391. }
  2392. if (inode_unhashed(inode)) {
  2393. /* Unfortunately insert_inode_hash is not idempotent,
  2394. * so as we hash inodes here rather than at creation
  2395. * time, we need a lock to ensure we only try
  2396. * to do it once
  2397. */
  2398. static DEFINE_SPINLOCK(lock);
  2399. spin_lock(&lock);
  2400. if (inode_unhashed(inode))
  2401. __insert_inode_hash(inode,
  2402. inode->i_ino + inode->i_generation);
  2403. spin_unlock(&lock);
  2404. }
  2405. fh[0] = inode->i_generation;
  2406. fh[1] = inode->i_ino;
  2407. fh[2] = ((__u64)inode->i_ino) >> 32;
  2408. *len = 3;
  2409. return 1;
  2410. }
  2411. static const struct export_operations shmem_export_ops = {
  2412. .get_parent = shmem_get_parent,
  2413. .encode_fh = shmem_encode_fh,
  2414. .fh_to_dentry = shmem_fh_to_dentry,
  2415. };
  2416. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  2417. bool remount)
  2418. {
  2419. char *this_char, *value, *rest;
  2420. struct mempolicy *mpol = NULL;
  2421. uid_t uid;
  2422. gid_t gid;
  2423. while (options != NULL) {
  2424. this_char = options;
  2425. for (;;) {
  2426. /*
  2427. * NUL-terminate this option: unfortunately,
  2428. * mount options form a comma-separated list,
  2429. * but mpol's nodelist may also contain commas.
  2430. */
  2431. options = strchr(options, ',');
  2432. if (options == NULL)
  2433. break;
  2434. options++;
  2435. if (!isdigit(*options)) {
  2436. options[-1] = '\0';
  2437. break;
  2438. }
  2439. }
  2440. if (!*this_char)
  2441. continue;
  2442. if ((value = strchr(this_char,'=')) != NULL) {
  2443. *value++ = 0;
  2444. } else {
  2445. printk(KERN_ERR
  2446. "tmpfs: No value for mount option '%s'\n",
  2447. this_char);
  2448. goto error;
  2449. }
  2450. if (!strcmp(this_char,"size")) {
  2451. unsigned long long size;
  2452. size = memparse(value,&rest);
  2453. if (*rest == '%') {
  2454. size <<= PAGE_SHIFT;
  2455. size *= totalram_pages;
  2456. do_div(size, 100);
  2457. rest++;
  2458. }
  2459. if (*rest)
  2460. goto bad_val;
  2461. sbinfo->max_blocks =
  2462. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  2463. } else if (!strcmp(this_char,"nr_blocks")) {
  2464. sbinfo->max_blocks = memparse(value, &rest);
  2465. if (*rest)
  2466. goto bad_val;
  2467. } else if (!strcmp(this_char,"nr_inodes")) {
  2468. sbinfo->max_inodes = memparse(value, &rest);
  2469. if (*rest)
  2470. goto bad_val;
  2471. } else if (!strcmp(this_char,"mode")) {
  2472. if (remount)
  2473. continue;
  2474. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  2475. if (*rest)
  2476. goto bad_val;
  2477. } else if (!strcmp(this_char,"uid")) {
  2478. if (remount)
  2479. continue;
  2480. uid = simple_strtoul(value, &rest, 0);
  2481. if (*rest)
  2482. goto bad_val;
  2483. sbinfo->uid = make_kuid(current_user_ns(), uid);
  2484. if (!uid_valid(sbinfo->uid))
  2485. goto bad_val;
  2486. } else if (!strcmp(this_char,"gid")) {
  2487. if (remount)
  2488. continue;
  2489. gid = simple_strtoul(value, &rest, 0);
  2490. if (*rest)
  2491. goto bad_val;
  2492. sbinfo->gid = make_kgid(current_user_ns(), gid);
  2493. if (!gid_valid(sbinfo->gid))
  2494. goto bad_val;
  2495. } else if (!strcmp(this_char,"mpol")) {
  2496. mpol_put(mpol);
  2497. mpol = NULL;
  2498. if (mpol_parse_str(value, &mpol))
  2499. goto bad_val;
  2500. } else {
  2501. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  2502. this_char);
  2503. goto error;
  2504. }
  2505. }
  2506. sbinfo->mpol = mpol;
  2507. return 0;
  2508. bad_val:
  2509. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  2510. value, this_char);
  2511. error:
  2512. mpol_put(mpol);
  2513. return 1;
  2514. }
  2515. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  2516. {
  2517. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2518. struct shmem_sb_info config = *sbinfo;
  2519. unsigned long inodes;
  2520. int error = -EINVAL;
  2521. config.mpol = NULL;
  2522. if (shmem_parse_options(data, &config, true))
  2523. return error;
  2524. spin_lock(&sbinfo->stat_lock);
  2525. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  2526. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  2527. goto out;
  2528. if (config.max_inodes < inodes)
  2529. goto out;
  2530. /*
  2531. * Those tests disallow limited->unlimited while any are in use;
  2532. * but we must separately disallow unlimited->limited, because
  2533. * in that case we have no record of how much is already in use.
  2534. */
  2535. if (config.max_blocks && !sbinfo->max_blocks)
  2536. goto out;
  2537. if (config.max_inodes && !sbinfo->max_inodes)
  2538. goto out;
  2539. error = 0;
  2540. sbinfo->max_blocks = config.max_blocks;
  2541. sbinfo->max_inodes = config.max_inodes;
  2542. sbinfo->free_inodes = config.max_inodes - inodes;
  2543. /*
  2544. * Preserve previous mempolicy unless mpol remount option was specified.
  2545. */
  2546. if (config.mpol) {
  2547. mpol_put(sbinfo->mpol);
  2548. sbinfo->mpol = config.mpol; /* transfers initial ref */
  2549. }
  2550. out:
  2551. spin_unlock(&sbinfo->stat_lock);
  2552. return error;
  2553. }
  2554. static int shmem_show_options(struct seq_file *seq, struct dentry *root)
  2555. {
  2556. struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
  2557. if (sbinfo->max_blocks != shmem_default_max_blocks())
  2558. seq_printf(seq, ",size=%luk",
  2559. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  2560. if (sbinfo->max_inodes != shmem_default_max_inodes())
  2561. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  2562. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  2563. seq_printf(seq, ",mode=%03ho", sbinfo->mode);
  2564. if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
  2565. seq_printf(seq, ",uid=%u",
  2566. from_kuid_munged(&init_user_ns, sbinfo->uid));
  2567. if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
  2568. seq_printf(seq, ",gid=%u",
  2569. from_kgid_munged(&init_user_ns, sbinfo->gid));
  2570. shmem_show_mpol(seq, sbinfo->mpol);
  2571. return 0;
  2572. }
  2573. #define MFD_NAME_PREFIX "memfd:"
  2574. #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
  2575. #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
  2576. #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
  2577. SYSCALL_DEFINE2(memfd_create,
  2578. const char __user *, uname,
  2579. unsigned int, flags)
  2580. {
  2581. struct shmem_inode_info *info;
  2582. struct file *file;
  2583. int fd, error;
  2584. char *name;
  2585. long len;
  2586. if (flags & ~(unsigned int)MFD_ALL_FLAGS)
  2587. return -EINVAL;
  2588. /* length includes terminating zero */
  2589. len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
  2590. if (len <= 0)
  2591. return -EFAULT;
  2592. if (len > MFD_NAME_MAX_LEN + 1)
  2593. return -EINVAL;
  2594. name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
  2595. if (!name)
  2596. return -ENOMEM;
  2597. strcpy(name, MFD_NAME_PREFIX);
  2598. if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
  2599. error = -EFAULT;
  2600. goto err_name;
  2601. }
  2602. /* terminating-zero may have changed after strnlen_user() returned */
  2603. if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
  2604. error = -EFAULT;
  2605. goto err_name;
  2606. }
  2607. fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
  2608. if (fd < 0) {
  2609. error = fd;
  2610. goto err_name;
  2611. }
  2612. file = shmem_file_setup(name, 0, VM_NORESERVE, 0);
  2613. if (IS_ERR(file)) {
  2614. error = PTR_ERR(file);
  2615. goto err_fd;
  2616. }
  2617. info = SHMEM_I(file_inode(file));
  2618. file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
  2619. file->f_flags |= O_RDWR | O_LARGEFILE;
  2620. if (flags & MFD_ALLOW_SEALING)
  2621. info->seals &= ~F_SEAL_SEAL;
  2622. fd_install(fd, file);
  2623. kfree(name);
  2624. return fd;
  2625. err_fd:
  2626. put_unused_fd(fd);
  2627. err_name:
  2628. kfree(name);
  2629. return error;
  2630. }
  2631. #endif /* CONFIG_TMPFS */
  2632. static void shmem_put_super(struct super_block *sb)
  2633. {
  2634. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2635. percpu_counter_destroy(&sbinfo->used_blocks);
  2636. mpol_put(sbinfo->mpol);
  2637. kfree(sbinfo);
  2638. sb->s_fs_info = NULL;
  2639. }
  2640. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  2641. {
  2642. struct inode *inode;
  2643. struct shmem_sb_info *sbinfo;
  2644. int err = -ENOMEM;
  2645. /* Round up to L1_CACHE_BYTES to resist false sharing */
  2646. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  2647. L1_CACHE_BYTES), GFP_KERNEL);
  2648. if (!sbinfo)
  2649. return -ENOMEM;
  2650. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  2651. sbinfo->uid = current_fsuid();
  2652. sbinfo->gid = current_fsgid();
  2653. sb->s_fs_info = sbinfo;
  2654. #ifdef CONFIG_TMPFS
  2655. /*
  2656. * Per default we only allow half of the physical ram per
  2657. * tmpfs instance, limiting inodes to one per page of lowmem;
  2658. * but the internal instance is left unlimited.
  2659. */
  2660. if (!(sb->s_flags & MS_KERNMOUNT)) {
  2661. sbinfo->max_blocks = shmem_default_max_blocks();
  2662. sbinfo->max_inodes = shmem_default_max_inodes();
  2663. if (shmem_parse_options(data, sbinfo, false)) {
  2664. err = -EINVAL;
  2665. goto failed;
  2666. }
  2667. } else {
  2668. sb->s_flags |= MS_NOUSER;
  2669. }
  2670. sb->s_export_op = &shmem_export_ops;
  2671. sb->s_flags |= MS_NOSEC;
  2672. #else
  2673. sb->s_flags |= MS_NOUSER;
  2674. #endif
  2675. spin_lock_init(&sbinfo->stat_lock);
  2676. if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
  2677. goto failed;
  2678. sbinfo->free_inodes = sbinfo->max_inodes;
  2679. sb->s_maxbytes = MAX_LFS_FILESIZE;
  2680. sb->s_blocksize = PAGE_CACHE_SIZE;
  2681. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  2682. sb->s_magic = TMPFS_MAGIC;
  2683. sb->s_op = &shmem_ops;
  2684. sb->s_time_gran = 1;
  2685. #ifdef CONFIG_TMPFS_XATTR
  2686. sb->s_xattr = shmem_xattr_handlers;
  2687. #endif
  2688. #ifdef CONFIG_TMPFS_POSIX_ACL
  2689. sb->s_flags |= MS_POSIXACL;
  2690. #endif
  2691. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE, 0);
  2692. if (!inode)
  2693. goto failed;
  2694. inode->i_uid = sbinfo->uid;
  2695. inode->i_gid = sbinfo->gid;
  2696. sb->s_root = d_make_root(inode);
  2697. if (!sb->s_root)
  2698. goto failed;
  2699. return 0;
  2700. failed:
  2701. shmem_put_super(sb);
  2702. return err;
  2703. }
  2704. static struct kmem_cache *shmem_inode_cachep;
  2705. static struct inode *shmem_alloc_inode(struct super_block *sb)
  2706. {
  2707. struct shmem_inode_info *info;
  2708. info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  2709. if (!info)
  2710. return NULL;
  2711. return &info->vfs_inode;
  2712. }
  2713. static void shmem_destroy_callback(struct rcu_head *head)
  2714. {
  2715. struct inode *inode = container_of(head, struct inode, i_rcu);
  2716. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  2717. }
  2718. static void shmem_destroy_inode(struct inode *inode)
  2719. {
  2720. if (S_ISREG(inode->i_mode))
  2721. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  2722. call_rcu(&inode->i_rcu, shmem_destroy_callback);
  2723. }
  2724. static void shmem_init_inode(void *foo)
  2725. {
  2726. struct shmem_inode_info *info = foo;
  2727. inode_init_once(&info->vfs_inode);
  2728. }
  2729. static int shmem_init_inodecache(void)
  2730. {
  2731. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  2732. sizeof(struct shmem_inode_info),
  2733. 0, SLAB_PANIC, shmem_init_inode);
  2734. return 0;
  2735. }
  2736. static void shmem_destroy_inodecache(void)
  2737. {
  2738. kmem_cache_destroy(shmem_inode_cachep);
  2739. }
  2740. static const struct address_space_operations shmem_aops = {
  2741. .writepage = shmem_writepage,
  2742. .set_page_dirty = __set_page_dirty_no_writeback,
  2743. #ifdef CONFIG_TMPFS
  2744. .write_begin = shmem_write_begin,
  2745. .write_end = shmem_write_end,
  2746. #endif
  2747. #ifdef CONFIG_MIGRATION
  2748. .migratepage = migrate_page,
  2749. #endif
  2750. .error_remove_page = generic_error_remove_page,
  2751. };
  2752. static const struct file_operations shmem_file_operations = {
  2753. .mmap = shmem_mmap,
  2754. #ifdef CONFIG_TMPFS
  2755. .llseek = shmem_file_llseek,
  2756. .read = new_sync_read,
  2757. .write = new_sync_write,
  2758. .read_iter = shmem_file_read_iter,
  2759. .write_iter = generic_file_write_iter,
  2760. .fsync = noop_fsync,
  2761. .splice_read = shmem_file_splice_read,
  2762. .splice_write = iter_file_splice_write,
  2763. .fallocate = shmem_fallocate,
  2764. #endif
  2765. };
  2766. static const struct inode_operations shmem_inode_operations = {
  2767. .setattr = shmem_setattr,
  2768. #ifdef CONFIG_TMPFS_XATTR
  2769. .setxattr = shmem_setxattr,
  2770. .getxattr = shmem_getxattr,
  2771. .listxattr = shmem_listxattr,
  2772. .removexattr = shmem_removexattr,
  2773. .set_acl = simple_set_acl,
  2774. #endif
  2775. };
  2776. static const struct inode_operations shmem_dir_inode_operations = {
  2777. #ifdef CONFIG_TMPFS
  2778. .create = shmem_create,
  2779. .lookup = simple_lookup,
  2780. .link = shmem_link,
  2781. .unlink = shmem_unlink,
  2782. .symlink = shmem_symlink,
  2783. .mkdir = shmem_mkdir,
  2784. .rmdir = shmem_rmdir,
  2785. .mknod = shmem_mknod,
  2786. .rename2 = shmem_rename2,
  2787. .tmpfile = shmem_tmpfile,
  2788. #endif
  2789. #ifdef CONFIG_TMPFS_XATTR
  2790. .setxattr = shmem_setxattr,
  2791. .getxattr = shmem_getxattr,
  2792. .listxattr = shmem_listxattr,
  2793. .removexattr = shmem_removexattr,
  2794. #endif
  2795. #ifdef CONFIG_TMPFS_POSIX_ACL
  2796. .setattr = shmem_setattr,
  2797. .set_acl = simple_set_acl,
  2798. #endif
  2799. };
  2800. static const struct inode_operations shmem_special_inode_operations = {
  2801. #ifdef CONFIG_TMPFS_XATTR
  2802. .setxattr = shmem_setxattr,
  2803. .getxattr = shmem_getxattr,
  2804. .listxattr = shmem_listxattr,
  2805. .removexattr = shmem_removexattr,
  2806. #endif
  2807. #ifdef CONFIG_TMPFS_POSIX_ACL
  2808. .setattr = shmem_setattr,
  2809. .set_acl = simple_set_acl,
  2810. #endif
  2811. };
  2812. static const struct super_operations shmem_ops = {
  2813. .alloc_inode = shmem_alloc_inode,
  2814. .destroy_inode = shmem_destroy_inode,
  2815. #ifdef CONFIG_TMPFS
  2816. .statfs = shmem_statfs,
  2817. .remount_fs = shmem_remount_fs,
  2818. .show_options = shmem_show_options,
  2819. #endif
  2820. .evict_inode = shmem_evict_inode,
  2821. .drop_inode = generic_delete_inode,
  2822. .put_super = shmem_put_super,
  2823. };
  2824. static const struct vm_operations_struct shmem_vm_ops = {
  2825. .fault = shmem_fault,
  2826. .map_pages = filemap_map_pages,
  2827. #ifdef CONFIG_NUMA
  2828. .set_policy = shmem_set_policy,
  2829. .get_policy = shmem_get_policy,
  2830. #endif
  2831. .remap_pages = generic_file_remap_pages,
  2832. };
  2833. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  2834. int flags, const char *dev_name, void *data)
  2835. {
  2836. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  2837. }
  2838. static struct file_system_type shmem_fs_type = {
  2839. .owner = THIS_MODULE,
  2840. .name = "tmpfs",
  2841. .mount = shmem_mount,
  2842. .kill_sb = kill_litter_super,
  2843. .fs_flags = FS_USERNS_MOUNT,
  2844. };
  2845. int __init shmem_init(void)
  2846. {
  2847. int error;
  2848. /* If rootfs called this, don't re-init */
  2849. if (shmem_inode_cachep)
  2850. return 0;
  2851. error = bdi_init(&shmem_backing_dev_info);
  2852. if (error)
  2853. goto out4;
  2854. error = shmem_init_inodecache();
  2855. if (error)
  2856. goto out3;
  2857. error = register_filesystem(&shmem_fs_type);
  2858. if (error) {
  2859. printk(KERN_ERR "Could not register tmpfs\n");
  2860. goto out2;
  2861. }
  2862. shm_mnt = kern_mount(&shmem_fs_type);
  2863. if (IS_ERR(shm_mnt)) {
  2864. error = PTR_ERR(shm_mnt);
  2865. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2866. goto out1;
  2867. }
  2868. return 0;
  2869. out1:
  2870. unregister_filesystem(&shmem_fs_type);
  2871. out2:
  2872. shmem_destroy_inodecache();
  2873. out3:
  2874. bdi_destroy(&shmem_backing_dev_info);
  2875. out4:
  2876. shm_mnt = ERR_PTR(error);
  2877. return error;
  2878. }
  2879. #else /* !CONFIG_SHMEM */
  2880. /*
  2881. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  2882. *
  2883. * This is intended for small system where the benefits of the full
  2884. * shmem code (swap-backed and resource-limited) are outweighed by
  2885. * their complexity. On systems without swap this code should be
  2886. * effectively equivalent, but much lighter weight.
  2887. */
  2888. static struct file_system_type shmem_fs_type = {
  2889. .name = "tmpfs",
  2890. .mount = ramfs_mount,
  2891. .kill_sb = kill_litter_super,
  2892. .fs_flags = FS_USERNS_MOUNT,
  2893. };
  2894. int __init shmem_init(void)
  2895. {
  2896. BUG_ON(register_filesystem(&shmem_fs_type) != 0);
  2897. shm_mnt = kern_mount(&shmem_fs_type);
  2898. BUG_ON(IS_ERR(shm_mnt));
  2899. return 0;
  2900. }
  2901. int shmem_unuse(swp_entry_t swap, struct page *page)
  2902. {
  2903. return 0;
  2904. }
  2905. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  2906. {
  2907. return 0;
  2908. }
  2909. void shmem_unlock_mapping(struct address_space *mapping)
  2910. {
  2911. }
  2912. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  2913. {
  2914. truncate_inode_pages_range(inode->i_mapping, lstart, lend);
  2915. }
  2916. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  2917. #define shmem_vm_ops generic_file_vm_ops
  2918. #define shmem_file_operations ramfs_file_operations
  2919. #define shmem_get_inode(sb, dir, mode, dev, flags, atomic_copy) ramfs_get_inode(sb, dir, mode, dev)
  2920. #define shmem_acct_size(flags, size) 0
  2921. #define shmem_unacct_size(flags, size) do {} while (0)
  2922. #endif /* CONFIG_SHMEM */
  2923. /* common code */
  2924. static struct dentry_operations anon_ops = {
  2925. .d_dname = simple_dname
  2926. };
  2927. static struct file *__shmem_file_setup(const char *name, loff_t size,
  2928. unsigned long flags, unsigned int i_flags,
  2929. int atomic_copy)
  2930. {
  2931. struct file *res;
  2932. struct inode *inode;
  2933. struct path path;
  2934. struct super_block *sb;
  2935. struct qstr this;
  2936. if (IS_ERR(shm_mnt))
  2937. return ERR_CAST(shm_mnt);
  2938. if (size < 0 || size > MAX_LFS_FILESIZE)
  2939. return ERR_PTR(-EINVAL);
  2940. if (shmem_acct_size(flags, size))
  2941. return ERR_PTR(-ENOMEM);
  2942. res = ERR_PTR(-ENOMEM);
  2943. this.name = name;
  2944. this.len = strlen(name);
  2945. this.hash = 0; /* will go */
  2946. sb = shm_mnt->mnt_sb;
  2947. path.mnt = mntget(shm_mnt);
  2948. path.dentry = d_alloc_pseudo(sb, &this);
  2949. if (!path.dentry)
  2950. goto put_memory;
  2951. d_set_d_op(path.dentry, &anon_ops);
  2952. res = ERR_PTR(-ENOSPC);
  2953. inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags, atomic_copy);
  2954. if (!inode)
  2955. goto put_memory;
  2956. inode->i_flags |= i_flags;
  2957. d_instantiate(path.dentry, inode);
  2958. inode->i_size = size;
  2959. clear_nlink(inode); /* It is unlinked */
  2960. res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
  2961. if (IS_ERR(res))
  2962. goto put_path;
  2963. res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  2964. &shmem_file_operations);
  2965. if (IS_ERR(res))
  2966. goto put_path;
  2967. return res;
  2968. put_memory:
  2969. shmem_unacct_size(flags, size);
  2970. put_path:
  2971. path_put(&path);
  2972. return res;
  2973. }
  2974. /**
  2975. * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
  2976. * kernel internal. There will be NO LSM permission checks against the
  2977. * underlying inode. So users of this interface must do LSM checks at a
  2978. * higher layer. The one user is the big_key implementation. LSM checks
  2979. * are provided at the key level rather than the inode level.
  2980. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2981. * @size: size to be set for the file
  2982. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2983. */
  2984. struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags, int atomic_copy)
  2985. {
  2986. return __shmem_file_setup(name, size, flags, S_PRIVATE, atomic_copy);
  2987. }
  2988. /**
  2989. * shmem_file_setup - get an unlinked file living in tmpfs
  2990. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2991. * @size: size to be set for the file
  2992. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2993. */
  2994. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags, int atomic_copy)
  2995. {
  2996. return __shmem_file_setup(name, size, flags, 0, atomic_copy);
  2997. }
  2998. EXPORT_SYMBOL_GPL(shmem_file_setup);
  2999. void shmem_set_file(struct vm_area_struct *vma, struct file *file)
  3000. {
  3001. if (vma->vm_file)
  3002. fput(vma->vm_file);
  3003. vma->vm_file = file;
  3004. vma->vm_ops = &shmem_vm_ops;
  3005. }
  3006. /**
  3007. * shmem_zero_setup - setup a shared anonymous mapping
  3008. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  3009. */
  3010. int shmem_zero_setup(struct vm_area_struct *vma)
  3011. {
  3012. struct file *file;
  3013. loff_t size = vma->vm_end - vma->vm_start;
  3014. file = shmem_file_setup("dev/zero", size, vma->vm_flags, 0);
  3015. if (IS_ERR(file))
  3016. return PTR_ERR(file);
  3017. shmem_set_file(vma, file);
  3018. return 0;
  3019. }
  3020. /**
  3021. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  3022. * @mapping: the page's address_space
  3023. * @index: the page index
  3024. * @gfp: the page allocator flags to use if allocating
  3025. *
  3026. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  3027. * with any new page allocations done using the specified allocation flags.
  3028. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  3029. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  3030. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  3031. *
  3032. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  3033. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  3034. */
  3035. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  3036. pgoff_t index, gfp_t gfp)
  3037. {
  3038. #ifdef CONFIG_SHMEM
  3039. struct inode *inode = mapping->host;
  3040. struct page *page;
  3041. int error;
  3042. BUG_ON(mapping->a_ops != &shmem_aops);
  3043. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
  3044. if (error)
  3045. page = ERR_PTR(error);
  3046. else
  3047. unlock_page(page);
  3048. return page;
  3049. #else
  3050. /*
  3051. * The tiny !SHMEM case uses ramfs without swap
  3052. */
  3053. return read_cache_page_gfp(mapping, index, gfp);
  3054. #endif
  3055. }
  3056. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);