shmem.c 108 KB

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