shmem.c 104 KB

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