shmem.c 112 KB

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