shmem.c 104 KB

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