shmem.c 105 KB

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