shmem.c 110 KB

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