shmem.c 110 KB

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