shmem.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471
  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/pagemap.h>
  28. #include <linux/file.h>
  29. #include <linux/mm.h>
  30. #include <linux/export.h>
  31. #include <linux/swap.h>
  32. static struct vfsmount *shm_mnt;
  33. #ifdef CONFIG_SHMEM
  34. /*
  35. * This virtual memory filesystem is heavily based on the ramfs. It
  36. * extends ramfs by the ability to use swap and honor resource limits
  37. * which makes it a completely usable filesystem.
  38. */
  39. #include <linux/xattr.h>
  40. #include <linux/exportfs.h>
  41. #include <linux/posix_acl.h>
  42. #include <linux/posix_acl_xattr.h>
  43. #include <linux/mman.h>
  44. #include <linux/string.h>
  45. #include <linux/slab.h>
  46. #include <linux/backing-dev.h>
  47. #include <linux/shmem_fs.h>
  48. #include <linux/writeback.h>
  49. #include <linux/blkdev.h>
  50. #include <linux/pagevec.h>
  51. #include <linux/percpu_counter.h>
  52. #include <linux/falloc.h>
  53. #include <linux/splice.h>
  54. #include <linux/security.h>
  55. #include <linux/swapops.h>
  56. #include <linux/mempolicy.h>
  57. #include <linux/namei.h>
  58. #include <linux/ctype.h>
  59. #include <linux/migrate.h>
  60. #include <linux/highmem.h>
  61. #include <linux/seq_file.h>
  62. #include <linux/syscalls.h>
  63. #include <linux/magic.h>
  64. #include <uapi/linux/memfd.h>
  65. #include <linux/fcntl.h>
  66. #include <asm/uaccess.h>
  67. #include <asm/pgtable.h>
  68. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  69. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  70. /* Pretend that each entry is of this size in directory's i_size */
  71. #define BOGO_DIRENT_SIZE 20
  72. /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
  73. #define SHORT_SYMLINK_LEN 128
  74. /*
  75. * shmem_fallocate communicates with shmem_fault or shmem_writepage via
  76. * inode->i_private (with i_mutex making sure that it has only one user at
  77. * a time): we would prefer not to enlarge the shmem inode just for that.
  78. */
  79. struct shmem_falloc {
  80. wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
  81. pgoff_t start; /* start of range currently being fallocated */
  82. pgoff_t next; /* the next page offset to be fallocated */
  83. pgoff_t nr_falloced; /* how many new pages have been fallocated */
  84. pgoff_t nr_unswapped; /* how often writepage refused to swap out */
  85. };
  86. /* Flag allocation requirements to shmem_getpage */
  87. enum sgp_type {
  88. SGP_READ, /* don't exceed i_size, don't allocate page */
  89. SGP_CACHE, /* don't exceed i_size, may allocate page */
  90. SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
  91. SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
  92. SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
  93. };
  94. #ifdef CONFIG_TMPFS
  95. static unsigned long shmem_default_max_blocks(void)
  96. {
  97. return totalram_pages / 2;
  98. }
  99. static unsigned long shmem_default_max_inodes(void)
  100. {
  101. return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
  102. }
  103. #endif
  104. static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
  105. static int shmem_replace_page(struct page **pagep, gfp_t gfp,
  106. struct shmem_inode_info *info, pgoff_t index);
  107. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  108. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
  109. static inline int shmem_getpage(struct inode *inode, pgoff_t index,
  110. struct page **pagep, enum sgp_type sgp, int *fault_type)
  111. {
  112. return shmem_getpage_gfp(inode, index, pagep, sgp,
  113. mapping_gfp_mask(inode->i_mapping), fault_type);
  114. }
  115. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  116. {
  117. return sb->s_fs_info;
  118. }
  119. /*
  120. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  121. * for shared memory and for shared anonymous (/dev/zero) mappings
  122. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  123. * consistent with the pre-accounting of private mappings ...
  124. */
  125. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  126. {
  127. return (flags & VM_NORESERVE) ?
  128. 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
  129. }
  130. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  131. {
  132. if (!(flags & VM_NORESERVE))
  133. vm_unacct_memory(VM_ACCT(size));
  134. }
  135. static inline int shmem_reacct_size(unsigned long flags,
  136. loff_t oldsize, loff_t newsize)
  137. {
  138. if (!(flags & VM_NORESERVE)) {
  139. if (VM_ACCT(newsize) > VM_ACCT(oldsize))
  140. return security_vm_enough_memory_mm(current->mm,
  141. VM_ACCT(newsize) - VM_ACCT(oldsize));
  142. else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
  143. vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
  144. }
  145. return 0;
  146. }
  147. /*
  148. * ... whereas tmpfs objects are accounted incrementally as
  149. * pages are allocated, in order to allow huge sparse files.
  150. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  151. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  152. */
  153. static inline int shmem_acct_block(unsigned long flags)
  154. {
  155. return (flags & VM_NORESERVE) ?
  156. security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
  157. }
  158. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  159. {
  160. if (flags & VM_NORESERVE)
  161. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  162. }
  163. static const struct super_operations shmem_ops;
  164. static const struct address_space_operations shmem_aops;
  165. static const struct file_operations shmem_file_operations;
  166. static const struct inode_operations shmem_inode_operations;
  167. static const struct inode_operations shmem_dir_inode_operations;
  168. static const struct inode_operations shmem_special_inode_operations;
  169. static const struct vm_operations_struct shmem_vm_ops;
  170. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  171. .ra_pages = 0, /* No readahead */
  172. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
  173. };
  174. static LIST_HEAD(shmem_swaplist);
  175. static DEFINE_MUTEX(shmem_swaplist_mutex);
  176. static int shmem_reserve_inode(struct super_block *sb)
  177. {
  178. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  179. if (sbinfo->max_inodes) {
  180. spin_lock(&sbinfo->stat_lock);
  181. if (!sbinfo->free_inodes) {
  182. spin_unlock(&sbinfo->stat_lock);
  183. return -ENOSPC;
  184. }
  185. sbinfo->free_inodes--;
  186. spin_unlock(&sbinfo->stat_lock);
  187. }
  188. return 0;
  189. }
  190. static void shmem_free_inode(struct super_block *sb)
  191. {
  192. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  193. if (sbinfo->max_inodes) {
  194. spin_lock(&sbinfo->stat_lock);
  195. sbinfo->free_inodes++;
  196. spin_unlock(&sbinfo->stat_lock);
  197. }
  198. }
  199. /**
  200. * shmem_recalc_inode - recalculate the block usage of an inode
  201. * @inode: inode to recalc
  202. *
  203. * We have to calculate the free blocks since the mm can drop
  204. * undirtied hole pages behind our back.
  205. *
  206. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  207. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  208. *
  209. * It has to be called with the spinlock held.
  210. */
  211. static void shmem_recalc_inode(struct inode *inode)
  212. {
  213. struct shmem_inode_info *info = SHMEM_I(inode);
  214. long freed;
  215. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  216. if (freed > 0) {
  217. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  218. if (sbinfo->max_blocks)
  219. percpu_counter_add(&sbinfo->used_blocks, -freed);
  220. info->alloced -= freed;
  221. inode->i_blocks -= freed * BLOCKS_PER_PAGE;
  222. shmem_unacct_blocks(info->flags, freed);
  223. }
  224. }
  225. /*
  226. * Replace item expected in radix tree by a new item, while holding tree lock.
  227. */
  228. static int shmem_radix_tree_replace(struct address_space *mapping,
  229. pgoff_t index, void *expected, void *replacement)
  230. {
  231. void **pslot;
  232. void *item = NULL;
  233. VM_BUG_ON(!expected);
  234. pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
  235. if (pslot)
  236. item = radix_tree_deref_slot_protected(pslot,
  237. &mapping->tree_lock);
  238. if (item != expected)
  239. return -ENOENT;
  240. if (replacement)
  241. radix_tree_replace_slot(pslot, replacement);
  242. else
  243. radix_tree_delete(&mapping->page_tree, index);
  244. return 0;
  245. }
  246. /*
  247. * Sometimes, before we decide whether to proceed or to fail, we must check
  248. * that an entry was not already brought back from swap by a racing thread.
  249. *
  250. * Checking page is not enough: by the time a SwapCache page is locked, it
  251. * might be reused, and again be SwapCache, using the same swap as before.
  252. */
  253. static bool shmem_confirm_swap(struct address_space *mapping,
  254. pgoff_t index, swp_entry_t swap)
  255. {
  256. void *item;
  257. rcu_read_lock();
  258. item = radix_tree_lookup(&mapping->page_tree, index);
  259. rcu_read_unlock();
  260. return item == swp_to_radix_entry(swap);
  261. }
  262. /*
  263. * Like add_to_page_cache_locked, but error if expected item has gone.
  264. */
  265. static int shmem_add_to_page_cache(struct page *page,
  266. struct address_space *mapping,
  267. pgoff_t index, gfp_t gfp, void *expected)
  268. {
  269. int error;
  270. VM_BUG_ON(!PageLocked(page));
  271. VM_BUG_ON(!PageSwapBacked(page));
  272. page_cache_get(page);
  273. page->mapping = mapping;
  274. page->index = index;
  275. spin_lock_irq(&mapping->tree_lock);
  276. if (!expected)
  277. error = radix_tree_insert(&mapping->page_tree, index, page);
  278. else
  279. error = shmem_radix_tree_replace(mapping, index, expected,
  280. page);
  281. if (!error) {
  282. mapping->nrpages++;
  283. __inc_zone_page_state(page, NR_FILE_PAGES);
  284. __inc_zone_page_state(page, NR_SHMEM);
  285. spin_unlock_irq(&mapping->tree_lock);
  286. } else {
  287. page->mapping = NULL;
  288. spin_unlock_irq(&mapping->tree_lock);
  289. page_cache_release(page);
  290. }
  291. return error;
  292. }
  293. /*
  294. * Like delete_from_page_cache, but substitutes swap for page.
  295. */
  296. static void shmem_delete_from_page_cache(struct page *page, void *radswap)
  297. {
  298. struct address_space *mapping = page->mapping;
  299. int error;
  300. spin_lock_irq(&mapping->tree_lock);
  301. error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
  302. page->mapping = NULL;
  303. mapping->nrpages--;
  304. __dec_zone_page_state(page, NR_FILE_PAGES);
  305. __dec_zone_page_state(page, NR_SHMEM);
  306. spin_unlock_irq(&mapping->tree_lock);
  307. page_cache_release(page);
  308. BUG_ON(error);
  309. }
  310. /*
  311. * Like find_get_pages, but collecting swap entries as well as pages.
  312. */
  313. static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
  314. pgoff_t start, unsigned int nr_pages,
  315. struct page **pages, pgoff_t *indices)
  316. {
  317. void **slot;
  318. unsigned int ret = 0;
  319. struct radix_tree_iter iter;
  320. if (!nr_pages)
  321. return 0;
  322. rcu_read_lock();
  323. restart:
  324. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
  325. struct page *page;
  326. repeat:
  327. page = radix_tree_deref_slot(slot);
  328. if (unlikely(!page))
  329. continue;
  330. if (radix_tree_exception(page)) {
  331. if (radix_tree_deref_retry(page))
  332. goto restart;
  333. /*
  334. * Otherwise, we must be storing a swap entry
  335. * here as an exceptional entry: so return it
  336. * without attempting to raise page count.
  337. */
  338. goto export;
  339. }
  340. if (!page_cache_get_speculative(page))
  341. goto repeat;
  342. /* Has the page moved? */
  343. if (unlikely(page != *slot)) {
  344. page_cache_release(page);
  345. goto repeat;
  346. }
  347. export:
  348. indices[ret] = iter.index;
  349. pages[ret] = page;
  350. if (++ret == nr_pages)
  351. break;
  352. }
  353. rcu_read_unlock();
  354. return ret;
  355. }
  356. /*
  357. * Remove swap entry from radix tree, free the swap and its page cache.
  358. */
  359. static int shmem_free_swap(struct address_space *mapping,
  360. pgoff_t index, void *radswap)
  361. {
  362. int error;
  363. spin_lock_irq(&mapping->tree_lock);
  364. error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
  365. spin_unlock_irq(&mapping->tree_lock);
  366. if (!error)
  367. free_swap_and_cache(radix_to_swp_entry(radswap));
  368. return error;
  369. }
  370. /*
  371. * Pagevec may contain swap entries, so shuffle up pages before releasing.
  372. */
  373. static void shmem_deswap_pagevec(struct pagevec *pvec)
  374. {
  375. int i, j;
  376. for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
  377. struct page *page = pvec->pages[i];
  378. if (!radix_tree_exceptional_entry(page))
  379. pvec->pages[j++] = page;
  380. }
  381. pvec->nr = j;
  382. }
  383. /*
  384. * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
  385. */
  386. void shmem_unlock_mapping(struct address_space *mapping)
  387. {
  388. struct pagevec pvec;
  389. pgoff_t indices[PAGEVEC_SIZE];
  390. pgoff_t index = 0;
  391. pagevec_init(&pvec, 0);
  392. /*
  393. * Minor point, but we might as well stop if someone else SHM_LOCKs it.
  394. */
  395. while (!mapping_unevictable(mapping)) {
  396. /*
  397. * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
  398. * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
  399. */
  400. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  401. PAGEVEC_SIZE, pvec.pages, indices);
  402. if (!pvec.nr)
  403. break;
  404. index = indices[pvec.nr - 1] + 1;
  405. shmem_deswap_pagevec(&pvec);
  406. check_move_unevictable_pages(pvec.pages, pvec.nr);
  407. pagevec_release(&pvec);
  408. cond_resched();
  409. }
  410. }
  411. /*
  412. * Remove range of pages and swap entries from radix tree, and free them.
  413. * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
  414. */
  415. static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
  416. bool unfalloc)
  417. {
  418. struct address_space *mapping = inode->i_mapping;
  419. struct shmem_inode_info *info = SHMEM_I(inode);
  420. pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  421. pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
  422. unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
  423. unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
  424. struct pagevec pvec;
  425. pgoff_t indices[PAGEVEC_SIZE];
  426. long nr_swaps_freed = 0;
  427. pgoff_t index;
  428. int i;
  429. if (lend == -1)
  430. end = -1; /* unsigned, so actually very big */
  431. pagevec_init(&pvec, 0);
  432. index = start;
  433. while (index < end) {
  434. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  435. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  436. pvec.pages, indices);
  437. if (!pvec.nr)
  438. break;
  439. mem_cgroup_uncharge_start();
  440. for (i = 0; i < pagevec_count(&pvec); i++) {
  441. struct page *page = pvec.pages[i];
  442. index = indices[i];
  443. if (index >= end)
  444. break;
  445. if (radix_tree_exceptional_entry(page)) {
  446. if (unfalloc)
  447. continue;
  448. nr_swaps_freed += !shmem_free_swap(mapping,
  449. index, page);
  450. continue;
  451. }
  452. if (!trylock_page(page))
  453. continue;
  454. if (!unfalloc || !PageUptodate(page)) {
  455. if (page->mapping == mapping) {
  456. VM_BUG_ON(PageWriteback(page));
  457. truncate_inode_page(mapping, page);
  458. }
  459. }
  460. unlock_page(page);
  461. }
  462. shmem_deswap_pagevec(&pvec);
  463. pagevec_release(&pvec);
  464. mem_cgroup_uncharge_end();
  465. cond_resched();
  466. index++;
  467. }
  468. if (partial_start) {
  469. struct page *page = NULL;
  470. shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
  471. if (page) {
  472. unsigned int top = PAGE_CACHE_SIZE;
  473. if (start > end) {
  474. top = partial_end;
  475. partial_end = 0;
  476. }
  477. zero_user_segment(page, partial_start, top);
  478. set_page_dirty(page);
  479. unlock_page(page);
  480. page_cache_release(page);
  481. }
  482. }
  483. if (partial_end) {
  484. struct page *page = NULL;
  485. shmem_getpage(inode, end, &page, SGP_READ, NULL);
  486. if (page) {
  487. zero_user_segment(page, 0, partial_end);
  488. set_page_dirty(page);
  489. unlock_page(page);
  490. page_cache_release(page);
  491. }
  492. }
  493. if (start >= end)
  494. return;
  495. index = start;
  496. while (index < end) {
  497. cond_resched();
  498. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  499. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  500. pvec.pages, indices);
  501. if (!pvec.nr) {
  502. /* If all gone or hole-punch or unfalloc, we're done */
  503. if (index == start || end != -1)
  504. break;
  505. /* But if truncating, restart to make sure all gone */
  506. index = start;
  507. continue;
  508. }
  509. mem_cgroup_uncharge_start();
  510. for (i = 0; i < pagevec_count(&pvec); i++) {
  511. struct page *page = pvec.pages[i];
  512. index = indices[i];
  513. if (index >= end)
  514. break;
  515. if (radix_tree_exceptional_entry(page)) {
  516. if (unfalloc)
  517. continue;
  518. if (shmem_free_swap(mapping, index, page)) {
  519. /* Swap was replaced by page: retry */
  520. index--;
  521. break;
  522. }
  523. nr_swaps_freed++;
  524. continue;
  525. }
  526. lock_page(page);
  527. if (!unfalloc || !PageUptodate(page)) {
  528. if (page->mapping == mapping) {
  529. VM_BUG_ON(PageWriteback(page));
  530. truncate_inode_page(mapping, page);
  531. } else {
  532. /* Page was replaced by swap: retry */
  533. unlock_page(page);
  534. index--;
  535. break;
  536. }
  537. }
  538. unlock_page(page);
  539. }
  540. shmem_deswap_pagevec(&pvec);
  541. pagevec_release(&pvec);
  542. mem_cgroup_uncharge_end();
  543. index++;
  544. }
  545. spin_lock(&info->lock);
  546. info->swapped -= nr_swaps_freed;
  547. shmem_recalc_inode(inode);
  548. spin_unlock(&info->lock);
  549. }
  550. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  551. {
  552. shmem_undo_range(inode, lstart, lend, false);
  553. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  554. }
  555. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  556. static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
  557. {
  558. struct inode *inode = dentry->d_inode;
  559. struct shmem_inode_info *info = SHMEM_I(inode);
  560. int error;
  561. error = inode_change_ok(inode, attr);
  562. if (error)
  563. return error;
  564. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  565. loff_t oldsize = inode->i_size;
  566. loff_t newsize = attr->ia_size;
  567. /* protected by i_mutex */
  568. if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
  569. (newsize > oldsize && (info->seals & F_SEAL_GROW)))
  570. return -EPERM;
  571. if (newsize != oldsize) {
  572. error = shmem_reacct_size(SHMEM_I(inode)->flags,
  573. oldsize, newsize);
  574. if (error)
  575. return error;
  576. i_size_write(inode, newsize);
  577. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  578. }
  579. if (newsize < oldsize) {
  580. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  581. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  582. shmem_truncate_range(inode, newsize, (loff_t)-1);
  583. /* unmap again to remove racily COWed private pages */
  584. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  585. }
  586. }
  587. setattr_copy(inode, attr);
  588. if (attr->ia_valid & ATTR_MODE)
  589. error = posix_acl_chmod(inode, inode->i_mode);
  590. return error;
  591. }
  592. static void shmem_evict_inode(struct inode *inode)
  593. {
  594. struct shmem_inode_info *info = SHMEM_I(inode);
  595. if (inode->i_mapping->a_ops == &shmem_aops) {
  596. shmem_unacct_size(info->flags, inode->i_size);
  597. inode->i_size = 0;
  598. shmem_truncate_range(inode, 0, (loff_t)-1);
  599. if (!list_empty(&info->swaplist)) {
  600. mutex_lock(&shmem_swaplist_mutex);
  601. list_del_init(&info->swaplist);
  602. mutex_unlock(&shmem_swaplist_mutex);
  603. }
  604. } else
  605. kfree(info->symlink);
  606. simple_xattrs_free(&info->xattrs);
  607. WARN_ON(inode->i_blocks);
  608. shmem_free_inode(inode->i_sb);
  609. end_writeback(inode);
  610. }
  611. /*
  612. * If swap found in inode, free it and move page from swapcache to filecache.
  613. */
  614. static int shmem_unuse_inode(struct shmem_inode_info *info,
  615. swp_entry_t swap, struct page **pagep)
  616. {
  617. struct address_space *mapping = info->vfs_inode.i_mapping;
  618. void *radswap;
  619. pgoff_t index;
  620. gfp_t gfp;
  621. int error = 0;
  622. radswap = swp_to_radix_entry(swap);
  623. index = radix_tree_locate_item(&mapping->page_tree, radswap);
  624. if (index == -1)
  625. return 0;
  626. /*
  627. * Move _head_ to start search for next from here.
  628. * But be careful: shmem_evict_inode checks list_empty without taking
  629. * mutex, and there's an instant in list_move_tail when info->swaplist
  630. * would appear empty, if it were the only one on shmem_swaplist.
  631. */
  632. if (shmem_swaplist.next != &info->swaplist)
  633. list_move_tail(&shmem_swaplist, &info->swaplist);
  634. gfp = mapping_gfp_mask(mapping);
  635. if (shmem_should_replace_page(*pagep, gfp)) {
  636. mutex_unlock(&shmem_swaplist_mutex);
  637. error = shmem_replace_page(pagep, gfp, info, index);
  638. mutex_lock(&shmem_swaplist_mutex);
  639. /*
  640. * We needed to drop mutex to make that restrictive page
  641. * allocation, but the inode might have been freed while we
  642. * dropped it: although a racing shmem_evict_inode() cannot
  643. * complete without emptying the radix_tree, our page lock
  644. * on this swapcache page is not enough to prevent that -
  645. * free_swap_and_cache() of our swap entry will only
  646. * trylock_page(), removing swap from radix_tree whatever.
  647. *
  648. * We must not proceed to shmem_add_to_page_cache() if the
  649. * inode has been freed, but of course we cannot rely on
  650. * inode or mapping or info to check that. However, we can
  651. * safely check if our swap entry is still in use (and here
  652. * it can't have got reused for another page): if it's still
  653. * in use, then the inode cannot have been freed yet, and we
  654. * can safely proceed (if it's no longer in use, that tells
  655. * nothing about the inode, but we don't need to unuse swap).
  656. */
  657. if (!page_swapcount(*pagep))
  658. error = -ENOENT;
  659. }
  660. /*
  661. * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
  662. * but also to hold up shmem_evict_inode(): so inode cannot be freed
  663. * beneath us (pagelock doesn't help until the page is in pagecache).
  664. */
  665. if (!error)
  666. error = shmem_add_to_page_cache(*pagep, mapping, index,
  667. GFP_NOWAIT, radswap);
  668. if (error != -ENOMEM) {
  669. /*
  670. * Truncation and eviction use free_swap_and_cache(), which
  671. * only does trylock page: if we raced, best clean up here.
  672. */
  673. delete_from_swap_cache(*pagep);
  674. set_page_dirty(*pagep);
  675. if (!error) {
  676. spin_lock(&info->lock);
  677. info->swapped--;
  678. spin_unlock(&info->lock);
  679. swap_free(swap);
  680. }
  681. error = 1; /* not an error, but entry was found */
  682. }
  683. return error;
  684. }
  685. /*
  686. * Search through swapped inodes to find and replace swap by page.
  687. */
  688. int shmem_unuse(swp_entry_t swap, struct page *page)
  689. {
  690. struct list_head *this, *next;
  691. struct shmem_inode_info *info;
  692. int found = 0;
  693. int error = 0;
  694. /*
  695. * There's a faint possibility that swap page was replaced before
  696. * caller locked it: caller will come back later with the right page.
  697. */
  698. if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
  699. goto out;
  700. /*
  701. * Charge page using GFP_KERNEL while we can wait, before taking
  702. * the shmem_swaplist_mutex which might hold up shmem_writepage().
  703. * Charged back to the user (not to caller) when swap account is used.
  704. */
  705. error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
  706. if (error)
  707. goto out;
  708. /* No radix_tree_preload: swap entry keeps a place for page in tree */
  709. mutex_lock(&shmem_swaplist_mutex);
  710. list_for_each_safe(this, next, &shmem_swaplist) {
  711. info = list_entry(this, struct shmem_inode_info, swaplist);
  712. if (info->swapped)
  713. found = shmem_unuse_inode(info, swap, &page);
  714. else
  715. list_del_init(&info->swaplist);
  716. cond_resched();
  717. if (found)
  718. break;
  719. }
  720. mutex_unlock(&shmem_swaplist_mutex);
  721. if (found < 0)
  722. error = found;
  723. out:
  724. unlock_page(page);
  725. page_cache_release(page);
  726. return error;
  727. }
  728. /*
  729. * Move the page from the page cache to the swap cache.
  730. */
  731. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  732. {
  733. struct shmem_inode_info *info;
  734. struct address_space *mapping;
  735. struct inode *inode;
  736. swp_entry_t swap;
  737. pgoff_t index;
  738. BUG_ON(!PageLocked(page));
  739. mapping = page->mapping;
  740. index = page->index;
  741. inode = mapping->host;
  742. info = SHMEM_I(inode);
  743. if (info->flags & VM_LOCKED)
  744. goto redirty;
  745. #ifdef CONFIG_RUNTIME_COMPCACHE
  746. /*
  747. * Modification for runtime compcache
  748. * shmem_writepage can be reason of kernel panic when using swap.
  749. * This modification prevent using swap by shmem.
  750. */
  751. goto redirty;
  752. #else
  753. if (!total_swap_pages)
  754. goto redirty;
  755. #endif /* CONFIG_RUNTIME_COMPCACHE */
  756. /*
  757. * shmem_backing_dev_info's capabilities prevent regular writeback or
  758. * sync from ever calling shmem_writepage; but a stacking filesystem
  759. * might use ->writepage of its underlying filesystem, in which case
  760. * tmpfs should write out to swap only in response to memory pressure,
  761. * and not for the writeback threads or sync.
  762. */
  763. if (!wbc->for_reclaim) {
  764. WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
  765. goto redirty;
  766. }
  767. /*
  768. * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
  769. * value into swapfile.c, the only way we can correctly account for a
  770. * fallocated page arriving here is now to initialize it and write it.
  771. *
  772. * That's okay for a page already fallocated earlier, but if we have
  773. * not yet completed the fallocation, then (a) we want to keep track
  774. * of this page in case we have to undo it, and (b) it may not be a
  775. * good idea to continue anyway, once we're pushing into swap. So
  776. * reactivate the page, and let shmem_fallocate() quit when too many.
  777. */
  778. if (!PageUptodate(page)) {
  779. if (inode->i_private) {
  780. struct shmem_falloc *shmem_falloc;
  781. spin_lock(&inode->i_lock);
  782. shmem_falloc = inode->i_private;
  783. if (shmem_falloc &&
  784. !shmem_falloc->waitq &&
  785. index >= shmem_falloc->start &&
  786. index < shmem_falloc->next)
  787. shmem_falloc->nr_unswapped++;
  788. else
  789. shmem_falloc = NULL;
  790. spin_unlock(&inode->i_lock);
  791. if (shmem_falloc)
  792. goto redirty;
  793. }
  794. clear_highpage(page);
  795. flush_dcache_page(page);
  796. SetPageUptodate(page);
  797. }
  798. swap = get_swap_page();
  799. if (!swap.val)
  800. goto redirty;
  801. /*
  802. * Add inode to shmem_unuse()'s list of swapped-out inodes,
  803. * if it's not already there. Do it now before the page is
  804. * moved to swap cache, when its pagelock no longer protects
  805. * the inode from eviction. But don't unlock the mutex until
  806. * we've incremented swapped, because shmem_unuse_inode() will
  807. * prune a !swapped inode from the swaplist under this mutex.
  808. */
  809. mutex_lock(&shmem_swaplist_mutex);
  810. if (list_empty(&info->swaplist))
  811. list_add_tail(&info->swaplist, &shmem_swaplist);
  812. if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
  813. swap_shmem_alloc(swap);
  814. shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
  815. spin_lock(&info->lock);
  816. info->swapped++;
  817. shmem_recalc_inode(inode);
  818. spin_unlock(&info->lock);
  819. mutex_unlock(&shmem_swaplist_mutex);
  820. BUG_ON(page_mapped(page));
  821. swap_writepage(page, wbc);
  822. return 0;
  823. }
  824. mutex_unlock(&shmem_swaplist_mutex);
  825. swapcache_free(swap, NULL);
  826. redirty:
  827. set_page_dirty(page);
  828. if (wbc->for_reclaim)
  829. return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
  830. unlock_page(page);
  831. return 0;
  832. }
  833. #ifdef CONFIG_NUMA
  834. #ifdef CONFIG_TMPFS
  835. static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  836. {
  837. char buffer[64];
  838. if (!mpol || mpol->mode == MPOL_DEFAULT)
  839. return; /* show nothing */
  840. mpol_to_str(buffer, sizeof(buffer), mpol, 1);
  841. seq_printf(seq, ",mpol=%s", buffer);
  842. }
  843. static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  844. {
  845. struct mempolicy *mpol = NULL;
  846. if (sbinfo->mpol) {
  847. spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
  848. mpol = sbinfo->mpol;
  849. mpol_get(mpol);
  850. spin_unlock(&sbinfo->stat_lock);
  851. }
  852. return mpol;
  853. }
  854. #endif /* CONFIG_TMPFS */
  855. static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  856. struct shmem_inode_info *info, pgoff_t index)
  857. {
  858. struct vm_area_struct pvma;
  859. struct page *page;
  860. /* Create a pseudo vma that just contains the policy */
  861. pvma.vm_start = 0;
  862. /* Bias interleave by inode number to distribute better across nodes */
  863. pvma.vm_pgoff = index + info->vfs_inode.i_ino;
  864. pvma.vm_ops = NULL;
  865. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  866. page = swapin_readahead(swap, gfp, &pvma, 0);
  867. /* Drop reference taken by mpol_shared_policy_lookup() */
  868. mpol_cond_put(pvma.vm_policy);
  869. return page;
  870. }
  871. static struct page *shmem_alloc_page(gfp_t gfp,
  872. struct shmem_inode_info *info, pgoff_t index)
  873. {
  874. struct vm_area_struct pvma;
  875. struct page *page;
  876. /* Create a pseudo vma that just contains the policy */
  877. pvma.vm_start = 0;
  878. /* Bias interleave by inode number to distribute better across nodes */
  879. pvma.vm_pgoff = index + info->vfs_inode.i_ino;
  880. pvma.vm_ops = NULL;
  881. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  882. page = alloc_page_vma(gfp, &pvma, 0);
  883. /* Drop reference taken by mpol_shared_policy_lookup() */
  884. mpol_cond_put(pvma.vm_policy);
  885. return page;
  886. }
  887. #else /* !CONFIG_NUMA */
  888. #ifdef CONFIG_TMPFS
  889. static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  890. {
  891. }
  892. #endif /* CONFIG_TMPFS */
  893. static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  894. struct shmem_inode_info *info, pgoff_t index)
  895. {
  896. return swapin_readahead(swap, gfp, NULL, 0);
  897. }
  898. static inline struct page *shmem_alloc_page(gfp_t gfp,
  899. struct shmem_inode_info *info, pgoff_t index)
  900. {
  901. return alloc_page(gfp);
  902. }
  903. #endif /* CONFIG_NUMA */
  904. #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
  905. static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  906. {
  907. return NULL;
  908. }
  909. #endif
  910. /*
  911. * When a page is moved from swapcache to shmem filecache (either by the
  912. * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
  913. * shmem_unuse_inode()), it may have been read in earlier from swap, in
  914. * ignorance of the mapping it belongs to. If that mapping has special
  915. * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
  916. * we may need to copy to a suitable page before moving to filecache.
  917. *
  918. * In a future release, this may well be extended to respect cpuset and
  919. * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
  920. * but for now it is a simple matter of zone.
  921. */
  922. static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
  923. {
  924. return page_zonenum(page) > gfp_zone(gfp);
  925. }
  926. static int shmem_replace_page(struct page **pagep, gfp_t gfp,
  927. struct shmem_inode_info *info, pgoff_t index)
  928. {
  929. struct page *oldpage, *newpage;
  930. struct address_space *swap_mapping;
  931. pgoff_t swap_index;
  932. int error;
  933. oldpage = *pagep;
  934. swap_index = page_private(oldpage);
  935. swap_mapping = page_mapping(oldpage);
  936. /*
  937. * We have arrived here because our zones are constrained, so don't
  938. * limit chance of success by further cpuset and node constraints.
  939. */
  940. gfp &= ~GFP_CONSTRAINT_MASK;
  941. newpage = shmem_alloc_page(gfp, info, index);
  942. if (!newpage)
  943. return -ENOMEM;
  944. page_cache_get(newpage);
  945. copy_highpage(newpage, oldpage);
  946. flush_dcache_page(newpage);
  947. __set_page_locked(newpage);
  948. SetPageUptodate(newpage);
  949. SetPageSwapBacked(newpage);
  950. set_page_private(newpage, swap_index);
  951. SetPageSwapCache(newpage);
  952. /*
  953. * Our caller will very soon move newpage out of swapcache, but it's
  954. * a nice clean interface for us to replace oldpage by newpage there.
  955. */
  956. spin_lock_irq(&swap_mapping->tree_lock);
  957. error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
  958. newpage);
  959. if (!error) {
  960. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  961. __dec_zone_page_state(oldpage, NR_FILE_PAGES);
  962. }
  963. spin_unlock_irq(&swap_mapping->tree_lock);
  964. if (unlikely(error)) {
  965. /*
  966. * Is this possible? I think not, now that our callers check
  967. * both PageSwapCache and page_private after getting page lock;
  968. * but be defensive. Reverse old to newpage for clear and free.
  969. */
  970. oldpage = newpage;
  971. } else {
  972. mem_cgroup_replace_page_cache(oldpage, newpage);
  973. lru_cache_add_anon(newpage);
  974. *pagep = newpage;
  975. }
  976. ClearPageSwapCache(oldpage);
  977. set_page_private(oldpage, 0);
  978. unlock_page(oldpage);
  979. page_cache_release(oldpage);
  980. page_cache_release(oldpage);
  981. return error;
  982. }
  983. /*
  984. * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
  985. *
  986. * If we allocate a new one we do not mark it dirty. That's up to the
  987. * vm. If we swap it in we mark it dirty since we also free the swap
  988. * entry since a page cannot live in both the swap and page cache
  989. */
  990. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  991. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
  992. {
  993. struct address_space *mapping = inode->i_mapping;
  994. struct shmem_inode_info *info;
  995. struct shmem_sb_info *sbinfo;
  996. struct page *page;
  997. swp_entry_t swap;
  998. int error;
  999. int once = 0;
  1000. int alloced = 0;
  1001. if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
  1002. return -EFBIG;
  1003. repeat:
  1004. swap.val = 0;
  1005. page = find_lock_page(mapping, index);
  1006. if (radix_tree_exceptional_entry(page)) {
  1007. swap = radix_to_swp_entry(page);
  1008. page = NULL;
  1009. }
  1010. if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
  1011. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  1012. error = -EINVAL;
  1013. goto failed;
  1014. }
  1015. /* fallocated page? */
  1016. if (page && !PageUptodate(page)) {
  1017. if (sgp != SGP_READ)
  1018. goto clear;
  1019. unlock_page(page);
  1020. page_cache_release(page);
  1021. page = NULL;
  1022. }
  1023. if (page || (sgp == SGP_READ && !swap.val)) {
  1024. *pagep = page;
  1025. return 0;
  1026. }
  1027. /*
  1028. * Fast cache lookup did not find it:
  1029. * bring it back from swap or allocate.
  1030. */
  1031. info = SHMEM_I(inode);
  1032. sbinfo = SHMEM_SB(inode->i_sb);
  1033. if (swap.val) {
  1034. /* Look it up and read it in.. */
  1035. page = lookup_swap_cache(swap);
  1036. if (!page) {
  1037. /* here we actually do the io */
  1038. if (fault_type)
  1039. *fault_type |= VM_FAULT_MAJOR;
  1040. page = shmem_swapin(swap, gfp, info, index);
  1041. if (!page) {
  1042. error = -ENOMEM;
  1043. goto failed;
  1044. }
  1045. }
  1046. /* We have to do this with page locked to prevent races */
  1047. lock_page(page);
  1048. if (!PageSwapCache(page) || page_private(page) != swap.val ||
  1049. !shmem_confirm_swap(mapping, index, swap)) {
  1050. error = -EEXIST; /* try again */
  1051. goto unlock;
  1052. }
  1053. if (!PageUptodate(page)) {
  1054. error = -EIO;
  1055. goto failed;
  1056. }
  1057. wait_on_page_writeback(page);
  1058. if (shmem_should_replace_page(page, gfp)) {
  1059. error = shmem_replace_page(&page, gfp, info, index);
  1060. if (error)
  1061. goto failed;
  1062. }
  1063. error = mem_cgroup_cache_charge(page, current->mm,
  1064. gfp & GFP_RECLAIM_MASK);
  1065. if (!error) {
  1066. error = shmem_add_to_page_cache(page, mapping, index,
  1067. gfp, swp_to_radix_entry(swap));
  1068. /*
  1069. * We already confirmed swap under page lock, and make
  1070. * no memory allocation here, so usually no possibility
  1071. * of error; but free_swap_and_cache() only trylocks a
  1072. * page, so it is just possible that the entry has been
  1073. * truncated or holepunched since swap was confirmed.
  1074. * shmem_undo_range() will have done some of the
  1075. * unaccounting, now delete_from_swap_cache() will do
  1076. * the rest (including mem_cgroup_uncharge_swapcache).
  1077. * Reset swap.val? No, leave it so "failed" goes back to
  1078. * "repeat": reading a hole and writing should succeed.
  1079. */
  1080. if (error)
  1081. delete_from_swap_cache(page);
  1082. }
  1083. if (error)
  1084. goto failed;
  1085. spin_lock(&info->lock);
  1086. info->swapped--;
  1087. shmem_recalc_inode(inode);
  1088. spin_unlock(&info->lock);
  1089. delete_from_swap_cache(page);
  1090. set_page_dirty(page);
  1091. swap_free(swap);
  1092. } else {
  1093. if (shmem_acct_block(info->flags)) {
  1094. error = -ENOSPC;
  1095. goto failed;
  1096. }
  1097. if (sbinfo->max_blocks) {
  1098. if (percpu_counter_compare(&sbinfo->used_blocks,
  1099. sbinfo->max_blocks) >= 0) {
  1100. error = -ENOSPC;
  1101. goto unacct;
  1102. }
  1103. percpu_counter_inc(&sbinfo->used_blocks);
  1104. }
  1105. page = shmem_alloc_page(gfp, info, index);
  1106. if (!page) {
  1107. error = -ENOMEM;
  1108. goto decused;
  1109. }
  1110. SetPageSwapBacked(page);
  1111. __set_page_locked(page);
  1112. error = mem_cgroup_cache_charge(page, current->mm,
  1113. gfp & GFP_RECLAIM_MASK);
  1114. if (error)
  1115. goto decused;
  1116. error = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK);
  1117. if (!error) {
  1118. error = shmem_add_to_page_cache(page, mapping, index,
  1119. gfp, NULL);
  1120. radix_tree_preload_end();
  1121. }
  1122. if (error) {
  1123. mem_cgroup_uncharge_cache_page(page);
  1124. goto decused;
  1125. }
  1126. lru_cache_add_anon(page);
  1127. spin_lock(&info->lock);
  1128. info->alloced++;
  1129. inode->i_blocks += BLOCKS_PER_PAGE;
  1130. shmem_recalc_inode(inode);
  1131. spin_unlock(&info->lock);
  1132. alloced = true;
  1133. /*
  1134. * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
  1135. */
  1136. if (sgp == SGP_FALLOC)
  1137. sgp = SGP_WRITE;
  1138. clear:
  1139. /*
  1140. * Let SGP_WRITE caller clear ends if write does not fill page;
  1141. * but SGP_FALLOC on a page fallocated earlier must initialize
  1142. * it now, lest undo on failure cancel our earlier guarantee.
  1143. */
  1144. if (sgp != SGP_WRITE) {
  1145. clear_highpage(page);
  1146. flush_dcache_page(page);
  1147. SetPageUptodate(page);
  1148. }
  1149. if (sgp == SGP_DIRTY)
  1150. set_page_dirty(page);
  1151. }
  1152. /* Perhaps the file has been truncated since we checked */
  1153. if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
  1154. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  1155. error = -EINVAL;
  1156. if (alloced)
  1157. goto trunc;
  1158. else
  1159. goto failed;
  1160. }
  1161. *pagep = page;
  1162. return 0;
  1163. /*
  1164. * Error recovery.
  1165. */
  1166. trunc:
  1167. info = SHMEM_I(inode);
  1168. ClearPageDirty(page);
  1169. delete_from_page_cache(page);
  1170. spin_lock(&info->lock);
  1171. info->alloced--;
  1172. inode->i_blocks -= BLOCKS_PER_PAGE;
  1173. spin_unlock(&info->lock);
  1174. decused:
  1175. sbinfo = SHMEM_SB(inode->i_sb);
  1176. if (sbinfo->max_blocks)
  1177. percpu_counter_add(&sbinfo->used_blocks, -1);
  1178. unacct:
  1179. shmem_unacct_blocks(info->flags, 1);
  1180. failed:
  1181. if (swap.val && error != -EINVAL &&
  1182. !shmem_confirm_swap(mapping, index, swap))
  1183. error = -EEXIST;
  1184. unlock:
  1185. if (page) {
  1186. unlock_page(page);
  1187. page_cache_release(page);
  1188. }
  1189. if (error == -ENOSPC && !once++) {
  1190. info = SHMEM_I(inode);
  1191. spin_lock(&info->lock);
  1192. shmem_recalc_inode(inode);
  1193. spin_unlock(&info->lock);
  1194. goto repeat;
  1195. }
  1196. if (error == -EEXIST) /* from above or from radix_tree_insert */
  1197. goto repeat;
  1198. return error;
  1199. }
  1200. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1201. {
  1202. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1203. int error;
  1204. int ret = VM_FAULT_LOCKED;
  1205. /*
  1206. * Trinity finds that probing a hole which tmpfs is punching can
  1207. * prevent the hole-punch from ever completing: which in turn
  1208. * locks writers out with its hold on i_mutex. So refrain from
  1209. * faulting pages into the hole while it's being punched. Although
  1210. * shmem_undo_range() does remove the additions, it may be unable to
  1211. * keep up, as each new page needs its own unmap_mapping_range() call,
  1212. * and the i_mmap tree grows ever slower to scan if new vmas are added.
  1213. *
  1214. * It does not matter if we sometimes reach this check just before the
  1215. * hole-punch begins, so that one fault then races with the punch:
  1216. * we just need to make racing faults a rare case.
  1217. *
  1218. * The implementation below would be much simpler if we just used a
  1219. * standard mutex or completion: but we cannot take i_mutex in fault,
  1220. * and bloating every shmem inode for this unlikely case would be sad.
  1221. */
  1222. if (unlikely(inode->i_private)) {
  1223. struct shmem_falloc *shmem_falloc;
  1224. spin_lock(&inode->i_lock);
  1225. shmem_falloc = inode->i_private;
  1226. if (shmem_falloc &&
  1227. shmem_falloc->waitq &&
  1228. vmf->pgoff >= shmem_falloc->start &&
  1229. vmf->pgoff < shmem_falloc->next) {
  1230. wait_queue_head_t *shmem_falloc_waitq;
  1231. DEFINE_WAIT(shmem_fault_wait);
  1232. ret = VM_FAULT_NOPAGE;
  1233. if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
  1234. !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
  1235. /* It's polite to up mmap_sem if we can */
  1236. up_read(&vma->vm_mm->mmap_sem);
  1237. ret = VM_FAULT_RETRY;
  1238. }
  1239. shmem_falloc_waitq = shmem_falloc->waitq;
  1240. prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
  1241. TASK_UNINTERRUPTIBLE);
  1242. spin_unlock(&inode->i_lock);
  1243. schedule();
  1244. /*
  1245. * shmem_falloc_waitq points into the shmem_fallocate()
  1246. * stack of the hole-punching task: shmem_falloc_waitq
  1247. * is usually invalid by the time we reach here, but
  1248. * finish_wait() does not dereference it in that case;
  1249. * though i_lock needed lest racing with wake_up_all().
  1250. */
  1251. spin_lock(&inode->i_lock);
  1252. finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
  1253. spin_unlock(&inode->i_lock);
  1254. return ret;
  1255. }
  1256. spin_unlock(&inode->i_lock);
  1257. }
  1258. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
  1259. if (error)
  1260. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  1261. if (ret & VM_FAULT_MAJOR) {
  1262. count_vm_event(PGMAJFAULT);
  1263. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  1264. }
  1265. return ret;
  1266. }
  1267. #ifdef CONFIG_NUMA
  1268. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
  1269. {
  1270. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1271. return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
  1272. }
  1273. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  1274. unsigned long addr)
  1275. {
  1276. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1277. pgoff_t index;
  1278. index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1279. return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
  1280. }
  1281. #endif
  1282. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1283. {
  1284. struct inode *inode = file->f_path.dentry->d_inode;
  1285. struct shmem_inode_info *info = SHMEM_I(inode);
  1286. int retval = -ENOMEM;
  1287. spin_lock(&info->lock);
  1288. if (lock && !(info->flags & VM_LOCKED)) {
  1289. if (!user_shm_lock(inode->i_size, user))
  1290. goto out_nomem;
  1291. info->flags |= VM_LOCKED;
  1292. mapping_set_unevictable(file->f_mapping);
  1293. }
  1294. if (!lock && (info->flags & VM_LOCKED) && user) {
  1295. user_shm_unlock(inode->i_size, user);
  1296. info->flags &= ~VM_LOCKED;
  1297. mapping_clear_unevictable(file->f_mapping);
  1298. }
  1299. retval = 0;
  1300. out_nomem:
  1301. spin_unlock(&info->lock);
  1302. return retval;
  1303. }
  1304. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1305. {
  1306. file_accessed(file);
  1307. #ifdef CONFIG_TIMA_RKP
  1308. if (vma->vm_end - vma->vm_start) {
  1309. /* iommu optimization- needs to be turned ON from
  1310. * the tz side.
  1311. */
  1312. cpu_v7_tima_iommu_opt(vma->vm_start, vma->vm_end, (unsigned long)vma->vm_mm->pgd);
  1313. __asm__ __volatile__ (
  1314. "mcr p15, 0, r0, c8, c3, 0\n"
  1315. "dsb\n"
  1316. "isb\n");
  1317. }
  1318. #endif
  1319. vma->vm_ops = &shmem_vm_ops;
  1320. return 0;
  1321. }
  1322. static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
  1323. umode_t mode, dev_t dev, unsigned long flags)
  1324. {
  1325. struct inode *inode;
  1326. struct shmem_inode_info *info;
  1327. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1328. if (shmem_reserve_inode(sb))
  1329. return NULL;
  1330. inode = new_inode(sb);
  1331. if (inode) {
  1332. inode->i_ino = get_next_ino();
  1333. inode_init_owner(inode, dir, mode);
  1334. inode->i_blocks = 0;
  1335. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1336. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1337. inode->i_generation = get_seconds();
  1338. info = SHMEM_I(inode);
  1339. memset(info, 0, (char *)inode - (char *)info);
  1340. spin_lock_init(&info->lock);
  1341. info->seals = F_SEAL_SEAL;
  1342. info->flags = flags & VM_NORESERVE;
  1343. INIT_LIST_HEAD(&info->swaplist);
  1344. simple_xattrs_init(&info->xattrs);
  1345. cache_no_acl(inode);
  1346. switch (mode & S_IFMT) {
  1347. default:
  1348. inode->i_op = &shmem_special_inode_operations;
  1349. init_special_inode(inode, mode, dev);
  1350. break;
  1351. case S_IFREG:
  1352. inode->i_mapping->a_ops = &shmem_aops;
  1353. inode->i_op = &shmem_inode_operations;
  1354. inode->i_fop = &shmem_file_operations;
  1355. mpol_shared_policy_init(&info->policy,
  1356. shmem_get_sbmpol(sbinfo));
  1357. break;
  1358. case S_IFDIR:
  1359. inc_nlink(inode);
  1360. /* Some things misbehave if size == 0 on a directory */
  1361. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1362. inode->i_op = &shmem_dir_inode_operations;
  1363. inode->i_fop = &simple_dir_operations;
  1364. break;
  1365. case S_IFLNK:
  1366. /*
  1367. * Must not load anything in the rbtree,
  1368. * mpol_free_shared_policy will not be called.
  1369. */
  1370. mpol_shared_policy_init(&info->policy, NULL);
  1371. break;
  1372. }
  1373. } else
  1374. shmem_free_inode(sb);
  1375. return inode;
  1376. }
  1377. #ifdef CONFIG_TMPFS
  1378. static const struct inode_operations shmem_symlink_inode_operations;
  1379. static const struct inode_operations shmem_short_symlink_operations;
  1380. #ifdef CONFIG_TMPFS_XATTR
  1381. static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
  1382. #else
  1383. #define shmem_initxattrs NULL
  1384. #endif
  1385. static int
  1386. shmem_write_begin(struct file *file, struct address_space *mapping,
  1387. loff_t pos, unsigned len, unsigned flags,
  1388. struct page **pagep, void **fsdata)
  1389. {
  1390. struct inode *inode = mapping->host;
  1391. struct shmem_inode_info *info = SHMEM_I(inode);
  1392. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1393. /* i_mutex is held by caller */
  1394. if (unlikely(info->seals)) {
  1395. if (info->seals & F_SEAL_WRITE)
  1396. return -EPERM;
  1397. if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
  1398. return -EPERM;
  1399. }
  1400. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1401. }
  1402. static int
  1403. shmem_write_end(struct file *file, struct address_space *mapping,
  1404. loff_t pos, unsigned len, unsigned copied,
  1405. struct page *page, void *fsdata)
  1406. {
  1407. struct inode *inode = mapping->host;
  1408. if (pos + copied > inode->i_size)
  1409. i_size_write(inode, pos + copied);
  1410. if (!PageUptodate(page)) {
  1411. if (copied < PAGE_CACHE_SIZE) {
  1412. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  1413. zero_user_segments(page, 0, from,
  1414. from + copied, PAGE_CACHE_SIZE);
  1415. }
  1416. SetPageUptodate(page);
  1417. }
  1418. set_page_dirty(page);
  1419. unlock_page(page);
  1420. page_cache_release(page);
  1421. return copied;
  1422. }
  1423. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1424. {
  1425. struct inode *inode = filp->f_path.dentry->d_inode;
  1426. struct address_space *mapping = inode->i_mapping;
  1427. pgoff_t index;
  1428. unsigned long offset;
  1429. enum sgp_type sgp = SGP_READ;
  1430. /*
  1431. * Might this read be for a stacking filesystem? Then when reading
  1432. * holes of a sparse file, we actually need to allocate those pages,
  1433. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  1434. */
  1435. if (segment_eq(get_fs(), KERNEL_DS))
  1436. sgp = SGP_DIRTY;
  1437. index = *ppos >> PAGE_CACHE_SHIFT;
  1438. offset = *ppos & ~PAGE_CACHE_MASK;
  1439. for (;;) {
  1440. struct page *page = NULL;
  1441. pgoff_t end_index;
  1442. unsigned long nr, ret;
  1443. loff_t i_size = i_size_read(inode);
  1444. end_index = i_size >> PAGE_CACHE_SHIFT;
  1445. if (index > end_index)
  1446. break;
  1447. if (index == end_index) {
  1448. nr = i_size & ~PAGE_CACHE_MASK;
  1449. if (nr <= offset)
  1450. break;
  1451. }
  1452. desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
  1453. if (desc->error) {
  1454. if (desc->error == -EINVAL)
  1455. desc->error = 0;
  1456. break;
  1457. }
  1458. if (page)
  1459. unlock_page(page);
  1460. /*
  1461. * We must evaluate after, since reads (unlike writes)
  1462. * are called without i_mutex protection against truncate
  1463. */
  1464. nr = PAGE_CACHE_SIZE;
  1465. i_size = i_size_read(inode);
  1466. end_index = i_size >> PAGE_CACHE_SHIFT;
  1467. if (index == end_index) {
  1468. nr = i_size & ~PAGE_CACHE_MASK;
  1469. if (nr <= offset) {
  1470. if (page)
  1471. page_cache_release(page);
  1472. break;
  1473. }
  1474. }
  1475. nr -= offset;
  1476. if (page) {
  1477. /*
  1478. * If users can be writing to this page using arbitrary
  1479. * virtual addresses, take care about potential aliasing
  1480. * before reading the page on the kernel side.
  1481. */
  1482. if (mapping_writably_mapped(mapping))
  1483. flush_dcache_page(page);
  1484. /*
  1485. * Mark the page accessed if we read the beginning.
  1486. */
  1487. if (!offset)
  1488. mark_page_accessed(page);
  1489. } else {
  1490. page = ZERO_PAGE(0);
  1491. page_cache_get(page);
  1492. }
  1493. /*
  1494. * Ok, we have the page, and it's up-to-date, so
  1495. * now we can copy it to user space...
  1496. *
  1497. * The actor routine returns how many bytes were actually used..
  1498. * NOTE! This may not be the same as how much of a user buffer
  1499. * we filled up (we may be padding etc), so we can only update
  1500. * "pos" here (the actor routine has to update the user buffer
  1501. * pointers and the remaining count).
  1502. */
  1503. ret = actor(desc, page, offset, nr);
  1504. offset += ret;
  1505. index += offset >> PAGE_CACHE_SHIFT;
  1506. offset &= ~PAGE_CACHE_MASK;
  1507. page_cache_release(page);
  1508. if (ret != nr || !desc->count)
  1509. break;
  1510. cond_resched();
  1511. }
  1512. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1513. file_accessed(filp);
  1514. }
  1515. static ssize_t shmem_file_aio_read(struct kiocb *iocb,
  1516. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  1517. {
  1518. struct file *filp = iocb->ki_filp;
  1519. ssize_t retval;
  1520. unsigned long seg;
  1521. size_t count;
  1522. loff_t *ppos = &iocb->ki_pos;
  1523. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1524. if (retval)
  1525. return retval;
  1526. for (seg = 0; seg < nr_segs; seg++) {
  1527. read_descriptor_t desc;
  1528. desc.written = 0;
  1529. desc.arg.buf = iov[seg].iov_base;
  1530. desc.count = iov[seg].iov_len;
  1531. if (desc.count == 0)
  1532. continue;
  1533. desc.error = 0;
  1534. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1535. retval += desc.written;
  1536. if (desc.error) {
  1537. retval = retval ?: desc.error;
  1538. break;
  1539. }
  1540. if (desc.count > 0)
  1541. break;
  1542. }
  1543. return retval;
  1544. }
  1545. static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
  1546. struct pipe_inode_info *pipe, size_t len,
  1547. unsigned int flags)
  1548. {
  1549. struct address_space *mapping = in->f_mapping;
  1550. struct inode *inode = mapping->host;
  1551. unsigned int loff, nr_pages, req_pages;
  1552. struct page *pages[PIPE_DEF_BUFFERS];
  1553. struct partial_page partial[PIPE_DEF_BUFFERS];
  1554. struct page *page;
  1555. pgoff_t index, end_index;
  1556. loff_t isize, left;
  1557. int error, page_nr;
  1558. struct splice_pipe_desc spd = {
  1559. .pages = pages,
  1560. .partial = partial,
  1561. .nr_pages_max = PIPE_DEF_BUFFERS,
  1562. .flags = flags,
  1563. .ops = &page_cache_pipe_buf_ops,
  1564. .spd_release = spd_release_page,
  1565. };
  1566. isize = i_size_read(inode);
  1567. if (unlikely(*ppos >= isize))
  1568. return 0;
  1569. left = isize - *ppos;
  1570. if (unlikely(left < len))
  1571. len = left;
  1572. if (splice_grow_spd(pipe, &spd))
  1573. return -ENOMEM;
  1574. index = *ppos >> PAGE_CACHE_SHIFT;
  1575. loff = *ppos & ~PAGE_CACHE_MASK;
  1576. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1577. nr_pages = min(req_pages, pipe->buffers);
  1578. spd.nr_pages = find_get_pages_contig(mapping, index,
  1579. nr_pages, spd.pages);
  1580. index += spd.nr_pages;
  1581. error = 0;
  1582. while (spd.nr_pages < nr_pages) {
  1583. error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
  1584. if (error)
  1585. break;
  1586. unlock_page(page);
  1587. spd.pages[spd.nr_pages++] = page;
  1588. index++;
  1589. }
  1590. index = *ppos >> PAGE_CACHE_SHIFT;
  1591. nr_pages = spd.nr_pages;
  1592. spd.nr_pages = 0;
  1593. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  1594. unsigned int this_len;
  1595. if (!len)
  1596. break;
  1597. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  1598. page = spd.pages[page_nr];
  1599. if (!PageUptodate(page) || page->mapping != mapping) {
  1600. error = shmem_getpage(inode, index, &page,
  1601. SGP_CACHE, NULL);
  1602. if (error)
  1603. break;
  1604. unlock_page(page);
  1605. page_cache_release(spd.pages[page_nr]);
  1606. spd.pages[page_nr] = page;
  1607. }
  1608. isize = i_size_read(inode);
  1609. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1610. if (unlikely(!isize || index > end_index))
  1611. break;
  1612. if (end_index == index) {
  1613. unsigned int plen;
  1614. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1615. if (plen <= loff)
  1616. break;
  1617. this_len = min(this_len, plen - loff);
  1618. len = this_len;
  1619. }
  1620. spd.partial[page_nr].offset = loff;
  1621. spd.partial[page_nr].len = this_len;
  1622. len -= this_len;
  1623. loff = 0;
  1624. spd.nr_pages++;
  1625. index++;
  1626. }
  1627. while (page_nr < nr_pages)
  1628. page_cache_release(spd.pages[page_nr++]);
  1629. if (spd.nr_pages)
  1630. error = splice_to_pipe(pipe, &spd);
  1631. splice_shrink_spd(&spd);
  1632. if (error > 0) {
  1633. *ppos += error;
  1634. file_accessed(in);
  1635. }
  1636. return error;
  1637. }
  1638. /*
  1639. * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
  1640. */
  1641. static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
  1642. pgoff_t index, pgoff_t end, int origin)
  1643. {
  1644. struct page *page;
  1645. struct pagevec pvec;
  1646. pgoff_t indices[PAGEVEC_SIZE];
  1647. bool done = false;
  1648. int i;
  1649. pagevec_init(&pvec, 0);
  1650. pvec.nr = 1; /* start small: we may be there already */
  1651. while (!done) {
  1652. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  1653. pvec.nr, pvec.pages, indices);
  1654. if (!pvec.nr) {
  1655. if (origin == SEEK_DATA)
  1656. index = end;
  1657. break;
  1658. }
  1659. for (i = 0; i < pvec.nr; i++, index++) {
  1660. if (index < indices[i]) {
  1661. if (origin == SEEK_HOLE) {
  1662. done = true;
  1663. break;
  1664. }
  1665. index = indices[i];
  1666. }
  1667. page = pvec.pages[i];
  1668. if (page && !radix_tree_exceptional_entry(page)) {
  1669. if (!PageUptodate(page))
  1670. page = NULL;
  1671. }
  1672. if (index >= end ||
  1673. (page && origin == SEEK_DATA) ||
  1674. (!page && origin == SEEK_HOLE)) {
  1675. done = true;
  1676. break;
  1677. }
  1678. }
  1679. shmem_deswap_pagevec(&pvec);
  1680. pagevec_release(&pvec);
  1681. pvec.nr = PAGEVEC_SIZE;
  1682. cond_resched();
  1683. }
  1684. return index;
  1685. }
  1686. static loff_t shmem_file_llseek(struct file *file, loff_t offset, int origin)
  1687. {
  1688. struct address_space *mapping = file->f_mapping;
  1689. struct inode *inode = mapping->host;
  1690. pgoff_t start, end;
  1691. loff_t new_offset;
  1692. if (origin != SEEK_DATA && origin != SEEK_HOLE)
  1693. return generic_file_llseek_size(file, offset, origin,
  1694. MAX_LFS_FILESIZE);
  1695. mutex_lock(&inode->i_mutex);
  1696. /* We're holding i_mutex so we can access i_size directly */
  1697. if (offset < 0)
  1698. offset = -EINVAL;
  1699. else if (offset >= inode->i_size)
  1700. offset = -ENXIO;
  1701. else {
  1702. start = offset >> PAGE_CACHE_SHIFT;
  1703. end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1704. new_offset = shmem_seek_hole_data(mapping, start, end, origin);
  1705. new_offset <<= PAGE_CACHE_SHIFT;
  1706. if (new_offset > offset) {
  1707. if (new_offset < inode->i_size)
  1708. offset = new_offset;
  1709. else if (origin == SEEK_DATA)
  1710. offset = -ENXIO;
  1711. else
  1712. offset = inode->i_size;
  1713. }
  1714. }
  1715. if (offset >= 0 && offset != file->f_pos) {
  1716. file->f_pos = offset;
  1717. file->f_version = 0;
  1718. }
  1719. mutex_unlock(&inode->i_mutex);
  1720. return offset;
  1721. }
  1722. /*
  1723. * We need a tag: a new tag would expand every radix_tree_node by 8 bytes,
  1724. * so reuse a tag which we firmly believe is never set or cleared on shmem.
  1725. */
  1726. #define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE
  1727. #define LAST_SCAN 4 /* about 150ms max */
  1728. static void shmem_tag_pins(struct address_space *mapping)
  1729. {
  1730. struct radix_tree_iter iter;
  1731. void **slot;
  1732. pgoff_t start;
  1733. struct page *page;
  1734. unsigned int tagged = 0;
  1735. lru_add_drain();
  1736. start = 0;
  1737. spin_lock_irq(&mapping->tree_lock);
  1738. restart:
  1739. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
  1740. page = radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
  1741. if (!page || radix_tree_exception(page)) {
  1742. if (radix_tree_deref_retry(page))
  1743. goto restart;
  1744. } else if (page_count(page) - page_mapcount(page) > 1) {
  1745. radix_tree_tag_set(&mapping->page_tree, iter.index,
  1746. SHMEM_TAG_PINNED);
  1747. }
  1748. if (++tagged % 1024)
  1749. continue;
  1750. spin_unlock_irq(&mapping->tree_lock);
  1751. cond_resched();
  1752. start = iter.index + 1;
  1753. spin_lock_irq(&mapping->tree_lock);
  1754. goto restart;
  1755. }
  1756. spin_unlock_irq(&mapping->tree_lock);
  1757. }
  1758. /*
  1759. * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
  1760. * via get_user_pages(), drivers might have some pending I/O without any active
  1761. * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
  1762. * and see whether it has an elevated ref-count. If so, we tag them and wait for
  1763. * them to be dropped.
  1764. * The caller must guarantee that no new user will acquire writable references
  1765. * to those pages to avoid races.
  1766. */
  1767. static int shmem_wait_for_pins(struct address_space *mapping)
  1768. {
  1769. struct radix_tree_iter iter;
  1770. void **slot;
  1771. pgoff_t start;
  1772. struct page *page;
  1773. int error, scan;
  1774. shmem_tag_pins(mapping);
  1775. error = 0;
  1776. for (scan = 0; scan <= LAST_SCAN; scan++) {
  1777. if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED))
  1778. break;
  1779. if (!scan)
  1780. lru_add_drain_all();
  1781. else if (schedule_timeout_killable((HZ << scan) / 200))
  1782. scan = LAST_SCAN;
  1783. start = 0;
  1784. rcu_read_lock();
  1785. restart:
  1786. radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter,
  1787. start, SHMEM_TAG_PINNED) {
  1788. page = radix_tree_deref_slot(slot);
  1789. if (radix_tree_exception(page)) {
  1790. if (radix_tree_deref_retry(page))
  1791. goto restart;
  1792. page = NULL;
  1793. }
  1794. if (page &&
  1795. page_count(page) - page_mapcount(page) != 1) {
  1796. if (scan < LAST_SCAN)
  1797. goto continue_resched;
  1798. /*
  1799. * On the last scan, we clean up all those tags
  1800. * we inserted; but make a note that we still
  1801. * found pages pinned.
  1802. */
  1803. error = -EBUSY;
  1804. }
  1805. spin_lock_irq(&mapping->tree_lock);
  1806. radix_tree_tag_clear(&mapping->page_tree,
  1807. iter.index, SHMEM_TAG_PINNED);
  1808. spin_unlock_irq(&mapping->tree_lock);
  1809. continue_resched:
  1810. if (need_resched()) {
  1811. cond_resched_rcu();
  1812. start = iter.index + 1;
  1813. goto restart;
  1814. }
  1815. }
  1816. rcu_read_unlock();
  1817. }
  1818. return error;
  1819. }
  1820. #define F_ALL_SEALS (F_SEAL_SEAL | \
  1821. F_SEAL_SHRINK | \
  1822. F_SEAL_GROW | \
  1823. F_SEAL_WRITE)
  1824. int shmem_add_seals(struct file *file, unsigned int seals)
  1825. {
  1826. struct inode *inode = file->f_path.dentry->d_inode;
  1827. struct shmem_inode_info *info = SHMEM_I(inode);
  1828. int error;
  1829. /*
  1830. * SEALING
  1831. * Sealing allows multiple parties to share a shmem-file but restrict
  1832. * access to a specific subset of file operations. Seals can only be
  1833. * added, but never removed. This way, mutually untrusted parties can
  1834. * share common memory regions with a well-defined policy. A malicious
  1835. * peer can thus never perform unwanted operations on a shared object.
  1836. *
  1837. * Seals are only supported on special shmem-files and always affect
  1838. * the whole underlying inode. Once a seal is set, it may prevent some
  1839. * kinds of access to the file. Currently, the following seals are
  1840. * defined:
  1841. * SEAL_SEAL: Prevent further seals from being set on this file
  1842. * SEAL_SHRINK: Prevent the file from shrinking
  1843. * SEAL_GROW: Prevent the file from growing
  1844. * SEAL_WRITE: Prevent write access to the file
  1845. *
  1846. * As we don't require any trust relationship between two parties, we
  1847. * must prevent seals from being removed. Therefore, sealing a file
  1848. * only adds a given set of seals to the file, it never touches
  1849. * existing seals. Furthermore, the "setting seals"-operation can be
  1850. * sealed itself, which basically prevents any further seal from being
  1851. * added.
  1852. *
  1853. * Semantics of sealing are only defined on volatile files. Only
  1854. * anonymous shmem files support sealing. More importantly, seals are
  1855. * never written to disk. Therefore, there's no plan to support it on
  1856. * other file types.
  1857. */
  1858. if (file->f_op != &shmem_file_operations)
  1859. return -EINVAL;
  1860. if (!(file->f_mode & FMODE_WRITE))
  1861. return -EPERM;
  1862. if (seals & ~(unsigned int)F_ALL_SEALS)
  1863. return -EINVAL;
  1864. mutex_lock(&inode->i_mutex);
  1865. if (info->seals & F_SEAL_SEAL) {
  1866. error = -EPERM;
  1867. goto unlock;
  1868. }
  1869. if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
  1870. error = mapping_deny_writable(file->f_mapping);
  1871. if (error)
  1872. goto unlock;
  1873. error = shmem_wait_for_pins(file->f_mapping);
  1874. if (error) {
  1875. mapping_allow_writable(file->f_mapping);
  1876. goto unlock;
  1877. }
  1878. }
  1879. info->seals |= seals;
  1880. error = 0;
  1881. unlock:
  1882. mutex_unlock(&inode->i_mutex);
  1883. return error;
  1884. }
  1885. EXPORT_SYMBOL_GPL(shmem_add_seals);
  1886. int shmem_get_seals(struct file *file)
  1887. {
  1888. if (file->f_op != &shmem_file_operations)
  1889. return -EINVAL;
  1890. return SHMEM_I(file->f_path.dentry->d_inode)->seals;
  1891. }
  1892. EXPORT_SYMBOL_GPL(shmem_get_seals);
  1893. long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  1894. {
  1895. long error;
  1896. switch (cmd) {
  1897. case F_ADD_SEALS:
  1898. /* disallow upper 32bit */
  1899. if (arg > UINT_MAX)
  1900. return -EINVAL;
  1901. error = shmem_add_seals(file, arg);
  1902. break;
  1903. case F_GET_SEALS:
  1904. error = shmem_get_seals(file);
  1905. break;
  1906. default:
  1907. error = -EINVAL;
  1908. break;
  1909. }
  1910. return error;
  1911. }
  1912. static long shmem_fallocate(struct file *file, int mode, loff_t offset,
  1913. loff_t len)
  1914. {
  1915. struct inode *inode = file->f_path.dentry->d_inode;
  1916. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1917. struct shmem_inode_info *info = SHMEM_I(inode);
  1918. struct shmem_falloc shmem_falloc;
  1919. pgoff_t start, index, end;
  1920. int error;
  1921. mutex_lock(&inode->i_mutex);
  1922. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1923. struct address_space *mapping = file->f_mapping;
  1924. loff_t unmap_start = round_up(offset, PAGE_SIZE);
  1925. loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
  1926. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
  1927. /* protected by i_mutex */
  1928. if (info->seals & F_SEAL_WRITE) {
  1929. error = -EPERM;
  1930. goto out;
  1931. }
  1932. shmem_falloc.waitq = &shmem_falloc_waitq;
  1933. shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
  1934. shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
  1935. spin_lock(&inode->i_lock);
  1936. inode->i_private = &shmem_falloc;
  1937. spin_unlock(&inode->i_lock);
  1938. if ((u64)unmap_end > (u64)unmap_start)
  1939. unmap_mapping_range(mapping, unmap_start,
  1940. 1 + unmap_end - unmap_start, 0);
  1941. shmem_truncate_range(inode, offset, offset + len - 1);
  1942. /* No need to unmap again: hole-punching leaves COWed pages */
  1943. spin_lock(&inode->i_lock);
  1944. inode->i_private = NULL;
  1945. wake_up_all(&shmem_falloc_waitq);
  1946. spin_unlock(&inode->i_lock);
  1947. error = 0;
  1948. goto out;
  1949. }
  1950. /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
  1951. error = inode_newsize_ok(inode, offset + len);
  1952. if (error)
  1953. goto out;
  1954. if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
  1955. error = -EPERM;
  1956. goto out;
  1957. }
  1958. start = offset >> PAGE_CACHE_SHIFT;
  1959. end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1960. /* Try to avoid a swapstorm if len is impossible to satisfy */
  1961. if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
  1962. error = -ENOSPC;
  1963. goto out;
  1964. }
  1965. shmem_falloc.waitq = NULL;
  1966. shmem_falloc.start = start;
  1967. shmem_falloc.next = start;
  1968. shmem_falloc.nr_falloced = 0;
  1969. shmem_falloc.nr_unswapped = 0;
  1970. spin_lock(&inode->i_lock);
  1971. inode->i_private = &shmem_falloc;
  1972. spin_unlock(&inode->i_lock);
  1973. for (index = start; index < end; index++) {
  1974. struct page *page;
  1975. /*
  1976. * Good, the fallocate(2) manpage permits EINTR: we may have
  1977. * been interrupted because we are using up too much memory.
  1978. */
  1979. if (signal_pending(current))
  1980. error = -EINTR;
  1981. else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
  1982. error = -ENOMEM;
  1983. else
  1984. error = shmem_getpage(inode, index, &page, SGP_FALLOC,
  1985. NULL);
  1986. if (error) {
  1987. /* Remove the !PageUptodate pages we added */
  1988. shmem_undo_range(inode,
  1989. (loff_t)start << PAGE_CACHE_SHIFT,
  1990. (loff_t)index << PAGE_CACHE_SHIFT, true);
  1991. goto undone;
  1992. }
  1993. /*
  1994. * Inform shmem_writepage() how far we have reached.
  1995. * No need for lock or barrier: we have the page lock.
  1996. */
  1997. shmem_falloc.next++;
  1998. if (!PageUptodate(page))
  1999. shmem_falloc.nr_falloced++;
  2000. /*
  2001. * If !PageUptodate, leave it that way so that freeable pages
  2002. * can be recognized if we need to rollback on error later.
  2003. * But set_page_dirty so that memory pressure will swap rather
  2004. * than free the pages we are allocating (and SGP_CACHE pages
  2005. * might still be clean: we now need to mark those dirty too).
  2006. */
  2007. set_page_dirty(page);
  2008. unlock_page(page);
  2009. page_cache_release(page);
  2010. cond_resched();
  2011. }
  2012. if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
  2013. i_size_write(inode, offset + len);
  2014. inode->i_ctime = CURRENT_TIME;
  2015. undone:
  2016. spin_lock(&inode->i_lock);
  2017. inode->i_private = NULL;
  2018. spin_unlock(&inode->i_lock);
  2019. out:
  2020. mutex_unlock(&inode->i_mutex);
  2021. return error;
  2022. }
  2023. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  2024. {
  2025. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  2026. buf->f_type = TMPFS_MAGIC;
  2027. buf->f_bsize = PAGE_CACHE_SIZE;
  2028. buf->f_namelen = NAME_MAX;
  2029. if (sbinfo->max_blocks) {
  2030. buf->f_blocks = sbinfo->max_blocks;
  2031. buf->f_bavail =
  2032. buf->f_bfree = sbinfo->max_blocks -
  2033. percpu_counter_sum(&sbinfo->used_blocks);
  2034. }
  2035. if (sbinfo->max_inodes) {
  2036. buf->f_files = sbinfo->max_inodes;
  2037. buf->f_ffree = sbinfo->free_inodes;
  2038. }
  2039. /* else leave those fields 0 like simple_statfs */
  2040. return 0;
  2041. }
  2042. /*
  2043. * File creation. Allocate an inode, and we're done..
  2044. */
  2045. static int
  2046. shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  2047. {
  2048. struct inode *inode;
  2049. int error = -ENOSPC;
  2050. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
  2051. if (inode) {
  2052. error = simple_acl_create(dir, inode);
  2053. if (error)
  2054. goto out_iput;
  2055. error = security_inode_init_security(inode, dir,
  2056. &dentry->d_name,
  2057. shmem_initxattrs, NULL);
  2058. if (error && error != -EOPNOTSUPP)
  2059. goto out_iput;
  2060. error = 0;
  2061. dir->i_size += BOGO_DIRENT_SIZE;
  2062. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  2063. d_instantiate(dentry, inode);
  2064. dget(dentry); /* Extra count - pin the dentry in core */
  2065. }
  2066. return error;
  2067. out_iput:
  2068. iput(inode);
  2069. return error;
  2070. }
  2071. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  2072. {
  2073. int error;
  2074. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  2075. return error;
  2076. inc_nlink(dir);
  2077. return 0;
  2078. }
  2079. static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2080. struct nameidata *nd)
  2081. {
  2082. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  2083. }
  2084. /*
  2085. * Link a file..
  2086. */
  2087. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  2088. {
  2089. struct inode *inode = old_dentry->d_inode;
  2090. int ret;
  2091. /*
  2092. * No ordinary (disk based) filesystem counts links as inodes;
  2093. * but each new link needs a new dentry, pinning lowmem, and
  2094. * tmpfs dentries cannot be pruned until they are unlinked.
  2095. */
  2096. ret = shmem_reserve_inode(inode->i_sb);
  2097. if (ret)
  2098. goto out;
  2099. dir->i_size += BOGO_DIRENT_SIZE;
  2100. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  2101. inc_nlink(inode);
  2102. ihold(inode); /* New dentry reference */
  2103. dget(dentry); /* Extra pinning count for the created dentry */
  2104. d_instantiate(dentry, inode);
  2105. out:
  2106. return ret;
  2107. }
  2108. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  2109. {
  2110. struct inode *inode = dentry->d_inode;
  2111. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  2112. shmem_free_inode(inode->i_sb);
  2113. dir->i_size -= BOGO_DIRENT_SIZE;
  2114. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  2115. drop_nlink(inode);
  2116. dput(dentry); /* Undo the count from "create" - this does all the work */
  2117. return 0;
  2118. }
  2119. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  2120. {
  2121. if (!simple_empty(dentry))
  2122. return -ENOTEMPTY;
  2123. drop_nlink(dentry->d_inode);
  2124. drop_nlink(dir);
  2125. return shmem_unlink(dir, dentry);
  2126. }
  2127. /*
  2128. * The VFS layer already does all the dentry stuff for rename,
  2129. * we just have to decrement the usage count for the target if
  2130. * it exists so that the VFS layer correctly free's it when it
  2131. * gets overwritten.
  2132. */
  2133. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  2134. {
  2135. struct inode *inode = old_dentry->d_inode;
  2136. int they_are_dirs = S_ISDIR(inode->i_mode);
  2137. if (!simple_empty(new_dentry))
  2138. return -ENOTEMPTY;
  2139. if (new_dentry->d_inode) {
  2140. (void) shmem_unlink(new_dir, new_dentry);
  2141. if (they_are_dirs) {
  2142. drop_nlink(new_dentry->d_inode);
  2143. drop_nlink(old_dir);
  2144. }
  2145. } else if (they_are_dirs) {
  2146. drop_nlink(old_dir);
  2147. inc_nlink(new_dir);
  2148. }
  2149. old_dir->i_size -= BOGO_DIRENT_SIZE;
  2150. new_dir->i_size += BOGO_DIRENT_SIZE;
  2151. old_dir->i_ctime = old_dir->i_mtime =
  2152. new_dir->i_ctime = new_dir->i_mtime =
  2153. inode->i_ctime = CURRENT_TIME;
  2154. return 0;
  2155. }
  2156. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  2157. {
  2158. int error;
  2159. int len;
  2160. struct inode *inode;
  2161. struct page *page;
  2162. char *kaddr;
  2163. struct shmem_inode_info *info;
  2164. len = strlen(symname) + 1;
  2165. if (len > PAGE_CACHE_SIZE)
  2166. return -ENAMETOOLONG;
  2167. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
  2168. if (!inode)
  2169. return -ENOSPC;
  2170. error = security_inode_init_security(inode, dir, &dentry->d_name,
  2171. shmem_initxattrs, NULL);
  2172. if (error) {
  2173. if (error != -EOPNOTSUPP) {
  2174. iput(inode);
  2175. return error;
  2176. }
  2177. error = 0;
  2178. }
  2179. info = SHMEM_I(inode);
  2180. inode->i_size = len-1;
  2181. if (len <= SHORT_SYMLINK_LEN) {
  2182. info->symlink = kmemdup(symname, len, GFP_KERNEL);
  2183. if (!info->symlink) {
  2184. iput(inode);
  2185. return -ENOMEM;
  2186. }
  2187. inode->i_op = &shmem_short_symlink_operations;
  2188. } else {
  2189. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  2190. if (error) {
  2191. iput(inode);
  2192. return error;
  2193. }
  2194. inode->i_mapping->a_ops = &shmem_aops;
  2195. inode->i_op = &shmem_symlink_inode_operations;
  2196. kaddr = kmap_atomic(page);
  2197. memcpy(kaddr, symname, len);
  2198. kunmap_atomic(kaddr);
  2199. SetPageUptodate(page);
  2200. set_page_dirty(page);
  2201. unlock_page(page);
  2202. page_cache_release(page);
  2203. }
  2204. dir->i_size += BOGO_DIRENT_SIZE;
  2205. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  2206. d_instantiate(dentry, inode);
  2207. dget(dentry);
  2208. return 0;
  2209. }
  2210. static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
  2211. {
  2212. nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
  2213. return NULL;
  2214. }
  2215. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  2216. {
  2217. struct page *page = NULL;
  2218. int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  2219. nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
  2220. if (page)
  2221. unlock_page(page);
  2222. return page;
  2223. }
  2224. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2225. {
  2226. if (!IS_ERR(nd_get_link(nd))) {
  2227. struct page *page = cookie;
  2228. kunmap(page);
  2229. mark_page_accessed(page);
  2230. page_cache_release(page);
  2231. }
  2232. }
  2233. #ifdef CONFIG_TMPFS_XATTR
  2234. /*
  2235. * Superblocks without xattr inode operations may get some security.* xattr
  2236. * support from the LSM "for free". As soon as we have any other xattrs
  2237. * like ACLs, we also need to implement the security.* handlers at
  2238. * filesystem level, though.
  2239. */
  2240. /*
  2241. * Callback for security_inode_init_security() for acquiring xattrs.
  2242. */
  2243. static int shmem_initxattrs(struct inode *inode,
  2244. const struct xattr *xattr_array,
  2245. void *fs_info)
  2246. {
  2247. struct shmem_inode_info *info = SHMEM_I(inode);
  2248. const struct xattr *xattr;
  2249. struct simple_xattr *new_xattr;
  2250. size_t len;
  2251. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  2252. new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
  2253. if (!new_xattr)
  2254. return -ENOMEM;
  2255. len = strlen(xattr->name) + 1;
  2256. new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
  2257. GFP_KERNEL);
  2258. if (!new_xattr->name) {
  2259. kfree(new_xattr);
  2260. return -ENOMEM;
  2261. }
  2262. memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
  2263. XATTR_SECURITY_PREFIX_LEN);
  2264. memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
  2265. xattr->name, len);
  2266. simple_xattr_list_add(&info->xattrs, new_xattr);
  2267. }
  2268. return 0;
  2269. }
  2270. static const struct xattr_handler *shmem_xattr_handlers[] = {
  2271. #ifdef CONFIG_TMPFS_POSIX_ACL
  2272. &posix_acl_access_xattr_handler,
  2273. &posix_acl_default_xattr_handler,
  2274. #endif
  2275. NULL
  2276. };
  2277. static int shmem_xattr_validate(const char *name)
  2278. {
  2279. struct { const char *prefix; size_t len; } arr[] = {
  2280. { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
  2281. { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
  2282. };
  2283. int i;
  2284. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  2285. size_t preflen = arr[i].len;
  2286. if (strncmp(name, arr[i].prefix, preflen) == 0) {
  2287. if (!name[preflen])
  2288. return -EINVAL;
  2289. return 0;
  2290. }
  2291. }
  2292. return -EOPNOTSUPP;
  2293. }
  2294. static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
  2295. void *buffer, size_t size)
  2296. {
  2297. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2298. int err;
  2299. /*
  2300. * If this is a request for a synthetic attribute in the system.*
  2301. * namespace use the generic infrastructure to resolve a handler
  2302. * for it via sb->s_xattr.
  2303. */
  2304. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2305. return generic_getxattr(dentry, name, buffer, size);
  2306. err = shmem_xattr_validate(name);
  2307. if (err)
  2308. return err;
  2309. return simple_xattr_get(&info->xattrs, name, buffer, size);
  2310. }
  2311. static int shmem_setxattr(struct dentry *dentry, const char *name,
  2312. const void *value, size_t size, int flags)
  2313. {
  2314. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2315. int err;
  2316. /*
  2317. * If this is a request for a synthetic attribute in the system.*
  2318. * namespace use the generic infrastructure to resolve a handler
  2319. * for it via sb->s_xattr.
  2320. */
  2321. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2322. return generic_setxattr(dentry, name, value, size, flags);
  2323. err = shmem_xattr_validate(name);
  2324. if (err)
  2325. return err;
  2326. return simple_xattr_set(&info->xattrs, name, value, size, flags);
  2327. }
  2328. static int shmem_removexattr(struct dentry *dentry, const char *name)
  2329. {
  2330. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2331. int err;
  2332. /*
  2333. * If this is a request for a synthetic attribute in the system.*
  2334. * namespace use the generic infrastructure to resolve a handler
  2335. * for it via sb->s_xattr.
  2336. */
  2337. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2338. return generic_removexattr(dentry, name);
  2339. err = shmem_xattr_validate(name);
  2340. if (err)
  2341. return err;
  2342. return simple_xattr_remove(&info->xattrs, name);
  2343. }
  2344. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  2345. {
  2346. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2347. return simple_xattr_list(&info->xattrs, buffer, size);
  2348. }
  2349. #endif /* CONFIG_TMPFS_XATTR */
  2350. static const struct inode_operations shmem_short_symlink_operations = {
  2351. .readlink = generic_readlink,
  2352. .follow_link = shmem_follow_short_symlink,
  2353. #ifdef CONFIG_TMPFS_XATTR
  2354. .setxattr = shmem_setxattr,
  2355. .getxattr = shmem_getxattr,
  2356. .listxattr = shmem_listxattr,
  2357. .removexattr = shmem_removexattr,
  2358. #endif
  2359. };
  2360. static const struct inode_operations shmem_symlink_inode_operations = {
  2361. .readlink = generic_readlink,
  2362. .follow_link = shmem_follow_link,
  2363. .put_link = shmem_put_link,
  2364. #ifdef CONFIG_TMPFS_XATTR
  2365. .setxattr = shmem_setxattr,
  2366. .getxattr = shmem_getxattr,
  2367. .listxattr = shmem_listxattr,
  2368. .removexattr = shmem_removexattr,
  2369. #endif
  2370. };
  2371. static struct dentry *shmem_get_parent(struct dentry *child)
  2372. {
  2373. return ERR_PTR(-ESTALE);
  2374. }
  2375. static int shmem_match(struct inode *ino, void *vfh)
  2376. {
  2377. __u32 *fh = vfh;
  2378. __u64 inum = fh[2];
  2379. inum = (inum << 32) | fh[1];
  2380. return ino->i_ino == inum && fh[0] == ino->i_generation;
  2381. }
  2382. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  2383. struct fid *fid, int fh_len, int fh_type)
  2384. {
  2385. struct inode *inode;
  2386. struct dentry *dentry = NULL;
  2387. u64 inum;
  2388. if (fh_len < 3)
  2389. return NULL;
  2390. inum = fid->raw[2];
  2391. inum = (inum << 32) | fid->raw[1];
  2392. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  2393. shmem_match, fid->raw);
  2394. if (inode) {
  2395. dentry = d_find_alias(inode);
  2396. iput(inode);
  2397. }
  2398. return dentry;
  2399. }
  2400. static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
  2401. int connectable)
  2402. {
  2403. struct inode *inode = dentry->d_inode;
  2404. if (*len < 3) {
  2405. *len = 3;
  2406. return 255;
  2407. }
  2408. if (inode_unhashed(inode)) {
  2409. /* Unfortunately insert_inode_hash is not idempotent,
  2410. * so as we hash inodes here rather than at creation
  2411. * time, we need a lock to ensure we only try
  2412. * to do it once
  2413. */
  2414. static DEFINE_SPINLOCK(lock);
  2415. spin_lock(&lock);
  2416. if (inode_unhashed(inode))
  2417. __insert_inode_hash(inode,
  2418. inode->i_ino + inode->i_generation);
  2419. spin_unlock(&lock);
  2420. }
  2421. fh[0] = inode->i_generation;
  2422. fh[1] = inode->i_ino;
  2423. fh[2] = ((__u64)inode->i_ino) >> 32;
  2424. *len = 3;
  2425. return 1;
  2426. }
  2427. static const struct export_operations shmem_export_ops = {
  2428. .get_parent = shmem_get_parent,
  2429. .encode_fh = shmem_encode_fh,
  2430. .fh_to_dentry = shmem_fh_to_dentry,
  2431. };
  2432. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  2433. bool remount)
  2434. {
  2435. char *this_char, *value, *rest;
  2436. struct mempolicy *mpol = NULL;
  2437. while (options != NULL) {
  2438. this_char = options;
  2439. for (;;) {
  2440. /*
  2441. * NUL-terminate this option: unfortunately,
  2442. * mount options form a comma-separated list,
  2443. * but mpol's nodelist may also contain commas.
  2444. */
  2445. options = strchr(options, ',');
  2446. if (options == NULL)
  2447. break;
  2448. options++;
  2449. if (!isdigit(*options)) {
  2450. options[-1] = '\0';
  2451. break;
  2452. }
  2453. }
  2454. if (!*this_char)
  2455. continue;
  2456. if ((value = strchr(this_char,'=')) != NULL) {
  2457. *value++ = 0;
  2458. } else {
  2459. printk(KERN_ERR
  2460. "tmpfs: No value for mount option '%s'\n",
  2461. this_char);
  2462. goto error;
  2463. }
  2464. if (!strcmp(this_char,"size")) {
  2465. unsigned long long size;
  2466. size = memparse(value,&rest);
  2467. if (*rest == '%') {
  2468. size <<= PAGE_SHIFT;
  2469. size *= totalram_pages;
  2470. do_div(size, 100);
  2471. rest++;
  2472. }
  2473. if (*rest)
  2474. goto bad_val;
  2475. sbinfo->max_blocks =
  2476. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  2477. } else if (!strcmp(this_char,"nr_blocks")) {
  2478. sbinfo->max_blocks = memparse(value, &rest);
  2479. if (*rest)
  2480. goto bad_val;
  2481. } else if (!strcmp(this_char,"nr_inodes")) {
  2482. sbinfo->max_inodes = memparse(value, &rest);
  2483. if (*rest)
  2484. goto bad_val;
  2485. } else if (!strcmp(this_char,"mode")) {
  2486. if (remount)
  2487. continue;
  2488. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  2489. if (*rest)
  2490. goto bad_val;
  2491. } else if (!strcmp(this_char,"uid")) {
  2492. if (remount)
  2493. continue;
  2494. sbinfo->uid = simple_strtoul(value, &rest, 0);
  2495. if (*rest)
  2496. goto bad_val;
  2497. } else if (!strcmp(this_char,"gid")) {
  2498. if (remount)
  2499. continue;
  2500. sbinfo->gid = simple_strtoul(value, &rest, 0);
  2501. if (*rest)
  2502. goto bad_val;
  2503. } else if (!strcmp(this_char,"mpol")) {
  2504. mpol_put(mpol);
  2505. mpol = NULL;
  2506. if (mpol_parse_str(value, &mpol, 1))
  2507. goto bad_val;
  2508. } else {
  2509. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  2510. this_char);
  2511. goto error;
  2512. }
  2513. }
  2514. sbinfo->mpol = mpol;
  2515. return 0;
  2516. bad_val:
  2517. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  2518. value, this_char);
  2519. error:
  2520. mpol_put(mpol);
  2521. return 1;
  2522. }
  2523. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  2524. {
  2525. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2526. struct shmem_sb_info config = *sbinfo;
  2527. unsigned long inodes;
  2528. int error = -EINVAL;
  2529. config.mpol = NULL;
  2530. if (shmem_parse_options(data, &config, true))
  2531. return error;
  2532. spin_lock(&sbinfo->stat_lock);
  2533. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  2534. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  2535. goto out;
  2536. if (config.max_inodes < inodes)
  2537. goto out;
  2538. /*
  2539. * Those tests disallow limited->unlimited while any are in use;
  2540. * but we must separately disallow unlimited->limited, because
  2541. * in that case we have no record of how much is already in use.
  2542. */
  2543. if (config.max_blocks && !sbinfo->max_blocks)
  2544. goto out;
  2545. if (config.max_inodes && !sbinfo->max_inodes)
  2546. goto out;
  2547. error = 0;
  2548. sbinfo->max_blocks = config.max_blocks;
  2549. sbinfo->max_inodes = config.max_inodes;
  2550. sbinfo->free_inodes = config.max_inodes - inodes;
  2551. /*
  2552. * Preserve previous mempolicy unless mpol remount option was specified.
  2553. */
  2554. if (config.mpol) {
  2555. mpol_put(sbinfo->mpol);
  2556. sbinfo->mpol = config.mpol; /* transfers initial ref */
  2557. }
  2558. out:
  2559. spin_unlock(&sbinfo->stat_lock);
  2560. return error;
  2561. }
  2562. static int shmem_show_options(struct seq_file *seq, struct dentry *root)
  2563. {
  2564. struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
  2565. if (sbinfo->max_blocks != shmem_default_max_blocks())
  2566. seq_printf(seq, ",size=%luk",
  2567. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  2568. if (sbinfo->max_inodes != shmem_default_max_inodes())
  2569. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  2570. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  2571. seq_printf(seq, ",mode=%03ho", sbinfo->mode);
  2572. if (sbinfo->uid != 0)
  2573. seq_printf(seq, ",uid=%u", sbinfo->uid);
  2574. if (sbinfo->gid != 0)
  2575. seq_printf(seq, ",gid=%u", sbinfo->gid);
  2576. shmem_show_mpol(seq, sbinfo->mpol);
  2577. return 0;
  2578. }
  2579. #define MFD_NAME_PREFIX "memfd:"
  2580. #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
  2581. #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
  2582. #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
  2583. SYSCALL_DEFINE2(memfd_create,
  2584. const char __user *, uname,
  2585. unsigned int, flags)
  2586. {
  2587. struct shmem_inode_info *info;
  2588. struct file *file;
  2589. int fd, error;
  2590. char *name;
  2591. long len;
  2592. if (flags & ~(unsigned int)MFD_ALL_FLAGS)
  2593. return -EINVAL;
  2594. /* length includes terminating zero */
  2595. len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
  2596. if (len <= 0)
  2597. return -EFAULT;
  2598. if (len > MFD_NAME_MAX_LEN + 1)
  2599. return -EINVAL;
  2600. name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
  2601. if (!name)
  2602. return -ENOMEM;
  2603. strcpy(name, MFD_NAME_PREFIX);
  2604. if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
  2605. error = -EFAULT;
  2606. goto err_name;
  2607. }
  2608. /* terminating-zero may have changed after strnlen_user() returned */
  2609. if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
  2610. error = -EFAULT;
  2611. goto err_name;
  2612. }
  2613. fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
  2614. if (fd < 0) {
  2615. error = fd;
  2616. goto err_name;
  2617. }
  2618. file = shmem_file_setup(name, 0, VM_NORESERVE);
  2619. if (IS_ERR(file)) {
  2620. error = PTR_ERR(file);
  2621. goto err_fd;
  2622. }
  2623. info = SHMEM_I(file_inode(file));
  2624. file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
  2625. file->f_flags |= O_RDWR | O_LARGEFILE;
  2626. if (flags & MFD_ALLOW_SEALING)
  2627. info->seals &= ~F_SEAL_SEAL;
  2628. fd_install(fd, file);
  2629. kfree(name);
  2630. return fd;
  2631. err_fd:
  2632. put_unused_fd(fd);
  2633. err_name:
  2634. kfree(name);
  2635. return error;
  2636. }
  2637. #endif /* CONFIG_TMPFS */
  2638. static void shmem_put_super(struct super_block *sb)
  2639. {
  2640. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2641. percpu_counter_destroy(&sbinfo->used_blocks);
  2642. mpol_put(sbinfo->mpol);
  2643. kfree(sbinfo);
  2644. sb->s_fs_info = NULL;
  2645. }
  2646. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  2647. {
  2648. struct inode *inode;
  2649. struct shmem_sb_info *sbinfo;
  2650. int err = -ENOMEM;
  2651. /* Round up to L1_CACHE_BYTES to resist false sharing */
  2652. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  2653. L1_CACHE_BYTES), GFP_KERNEL);
  2654. if (!sbinfo)
  2655. return -ENOMEM;
  2656. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  2657. sbinfo->uid = current_fsuid();
  2658. sbinfo->gid = current_fsgid();
  2659. sb->s_fs_info = sbinfo;
  2660. #ifdef CONFIG_TMPFS
  2661. /*
  2662. * Per default we only allow half of the physical ram per
  2663. * tmpfs instance, limiting inodes to one per page of lowmem;
  2664. * but the internal instance is left unlimited.
  2665. */
  2666. if (!(sb->s_flags & MS_NOUSER)) {
  2667. sbinfo->max_blocks = shmem_default_max_blocks();
  2668. sbinfo->max_inodes = shmem_default_max_inodes();
  2669. if (shmem_parse_options(data, sbinfo, false)) {
  2670. err = -EINVAL;
  2671. goto failed;
  2672. }
  2673. }
  2674. sb->s_export_op = &shmem_export_ops;
  2675. sb->s_flags |= MS_NOSEC;
  2676. #else
  2677. sb->s_flags |= MS_NOUSER;
  2678. #endif
  2679. spin_lock_init(&sbinfo->stat_lock);
  2680. if (percpu_counter_init(&sbinfo->used_blocks, 0))
  2681. goto failed;
  2682. sbinfo->free_inodes = sbinfo->max_inodes;
  2683. sb->s_maxbytes = MAX_LFS_FILESIZE;
  2684. sb->s_blocksize = PAGE_CACHE_SIZE;
  2685. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  2686. sb->s_magic = TMPFS_MAGIC;
  2687. sb->s_op = &shmem_ops;
  2688. sb->s_time_gran = 1;
  2689. #ifdef CONFIG_TMPFS_XATTR
  2690. sb->s_xattr = shmem_xattr_handlers;
  2691. #endif
  2692. #ifdef CONFIG_TMPFS_POSIX_ACL
  2693. sb->s_flags |= MS_POSIXACL;
  2694. #endif
  2695. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
  2696. if (!inode)
  2697. goto failed;
  2698. inode->i_uid = sbinfo->uid;
  2699. inode->i_gid = sbinfo->gid;
  2700. sb->s_root = d_make_root(inode);
  2701. if (!sb->s_root)
  2702. goto failed;
  2703. return 0;
  2704. failed:
  2705. shmem_put_super(sb);
  2706. return err;
  2707. }
  2708. static struct kmem_cache *shmem_inode_cachep;
  2709. static struct inode *shmem_alloc_inode(struct super_block *sb)
  2710. {
  2711. struct shmem_inode_info *info;
  2712. info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  2713. if (!info)
  2714. return NULL;
  2715. return &info->vfs_inode;
  2716. }
  2717. static void shmem_destroy_callback(struct rcu_head *head)
  2718. {
  2719. struct inode *inode = container_of(head, struct inode, i_rcu);
  2720. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  2721. }
  2722. static void shmem_destroy_inode(struct inode *inode)
  2723. {
  2724. if (S_ISREG(inode->i_mode))
  2725. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  2726. call_rcu(&inode->i_rcu, shmem_destroy_callback);
  2727. }
  2728. static void shmem_init_inode(void *foo)
  2729. {
  2730. struct shmem_inode_info *info = foo;
  2731. inode_init_once(&info->vfs_inode);
  2732. }
  2733. static int shmem_init_inodecache(void)
  2734. {
  2735. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  2736. sizeof(struct shmem_inode_info),
  2737. 0, SLAB_PANIC, shmem_init_inode);
  2738. return 0;
  2739. }
  2740. static void shmem_destroy_inodecache(void)
  2741. {
  2742. kmem_cache_destroy(shmem_inode_cachep);
  2743. }
  2744. static const struct address_space_operations shmem_aops = {
  2745. .writepage = shmem_writepage,
  2746. .set_page_dirty = __set_page_dirty_no_writeback,
  2747. #ifdef CONFIG_TMPFS
  2748. .write_begin = shmem_write_begin,
  2749. .write_end = shmem_write_end,
  2750. #endif
  2751. .migratepage = migrate_page,
  2752. .error_remove_page = generic_error_remove_page,
  2753. };
  2754. static const struct file_operations shmem_file_operations = {
  2755. .mmap = shmem_mmap,
  2756. #ifdef CONFIG_TMPFS
  2757. .llseek = shmem_file_llseek,
  2758. .read = do_sync_read,
  2759. .write = do_sync_write,
  2760. .aio_read = shmem_file_aio_read,
  2761. .aio_write = generic_file_aio_write,
  2762. .fsync = noop_fsync,
  2763. .splice_read = shmem_file_splice_read,
  2764. .splice_write = generic_file_splice_write,
  2765. .fallocate = shmem_fallocate,
  2766. #endif
  2767. };
  2768. static const struct inode_operations shmem_inode_operations = {
  2769. .setattr = shmem_setattr,
  2770. #ifdef CONFIG_TMPFS_XATTR
  2771. .setxattr = shmem_setxattr,
  2772. .getxattr = shmem_getxattr,
  2773. .listxattr = shmem_listxattr,
  2774. .removexattr = shmem_removexattr,
  2775. .set_acl = simple_set_acl,
  2776. #endif
  2777. };
  2778. static const struct inode_operations shmem_dir_inode_operations = {
  2779. #ifdef CONFIG_TMPFS
  2780. .create = shmem_create,
  2781. .lookup = simple_lookup,
  2782. .link = shmem_link,
  2783. .unlink = shmem_unlink,
  2784. .symlink = shmem_symlink,
  2785. .mkdir = shmem_mkdir,
  2786. .rmdir = shmem_rmdir,
  2787. .mknod = shmem_mknod,
  2788. .rename = shmem_rename,
  2789. #endif
  2790. #ifdef CONFIG_TMPFS_XATTR
  2791. .setxattr = shmem_setxattr,
  2792. .getxattr = shmem_getxattr,
  2793. .listxattr = shmem_listxattr,
  2794. .removexattr = shmem_removexattr,
  2795. #endif
  2796. #ifdef CONFIG_TMPFS_POSIX_ACL
  2797. .setattr = shmem_setattr,
  2798. .set_acl = simple_set_acl,
  2799. #endif
  2800. };
  2801. static const struct inode_operations shmem_special_inode_operations = {
  2802. #ifdef CONFIG_TMPFS_XATTR
  2803. .setxattr = shmem_setxattr,
  2804. .getxattr = shmem_getxattr,
  2805. .listxattr = shmem_listxattr,
  2806. .removexattr = shmem_removexattr,
  2807. #endif
  2808. #ifdef CONFIG_TMPFS_POSIX_ACL
  2809. .setattr = shmem_setattr,
  2810. .set_acl = simple_set_acl,
  2811. #endif
  2812. };
  2813. static const struct super_operations shmem_ops = {
  2814. .alloc_inode = shmem_alloc_inode,
  2815. .destroy_inode = shmem_destroy_inode,
  2816. #ifdef CONFIG_TMPFS
  2817. .statfs = shmem_statfs,
  2818. .remount_fs = shmem_remount_fs,
  2819. .show_options = shmem_show_options,
  2820. #endif
  2821. .evict_inode = shmem_evict_inode,
  2822. .drop_inode = generic_delete_inode,
  2823. .put_super = shmem_put_super,
  2824. };
  2825. static const struct vm_operations_struct shmem_vm_ops = {
  2826. .fault = shmem_fault,
  2827. #ifdef CONFIG_NUMA
  2828. .set_policy = shmem_set_policy,
  2829. .get_policy = shmem_get_policy,
  2830. #endif
  2831. .remap_pages = generic_file_remap_pages,
  2832. };
  2833. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  2834. int flags, const char *dev_name, void *data)
  2835. {
  2836. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  2837. }
  2838. static struct file_system_type shmem_fs_type = {
  2839. .owner = THIS_MODULE,
  2840. .name = "tmpfs",
  2841. .mount = shmem_mount,
  2842. .kill_sb = kill_litter_super,
  2843. .fs_flags = FS_USERNS_MOUNT,
  2844. };
  2845. int __init shmem_init(void)
  2846. {
  2847. int error;
  2848. error = bdi_init(&shmem_backing_dev_info);
  2849. if (error)
  2850. goto out4;
  2851. error = shmem_init_inodecache();
  2852. if (error)
  2853. goto out3;
  2854. error = register_filesystem(&shmem_fs_type);
  2855. if (error) {
  2856. printk(KERN_ERR "Could not register tmpfs\n");
  2857. goto out2;
  2858. }
  2859. shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
  2860. shmem_fs_type.name, NULL);
  2861. if (IS_ERR(shm_mnt)) {
  2862. error = PTR_ERR(shm_mnt);
  2863. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2864. goto out1;
  2865. }
  2866. return 0;
  2867. out1:
  2868. unregister_filesystem(&shmem_fs_type);
  2869. out2:
  2870. shmem_destroy_inodecache();
  2871. out3:
  2872. bdi_destroy(&shmem_backing_dev_info);
  2873. out4:
  2874. shm_mnt = ERR_PTR(error);
  2875. return error;
  2876. }
  2877. #else /* !CONFIG_SHMEM */
  2878. /*
  2879. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  2880. *
  2881. * This is intended for small system where the benefits of the full
  2882. * shmem code (swap-backed and resource-limited) are outweighed by
  2883. * their complexity. On systems without swap this code should be
  2884. * effectively equivalent, but much lighter weight.
  2885. */
  2886. #include <linux/ramfs.h>
  2887. static struct file_system_type shmem_fs_type = {
  2888. .name = "tmpfs",
  2889. .mount = ramfs_mount,
  2890. .kill_sb = kill_litter_super,
  2891. .fs_flags = FS_USERNS_MOUNT,
  2892. };
  2893. int __init shmem_init(void)
  2894. {
  2895. BUG_ON(register_filesystem(&shmem_fs_type) != 0);
  2896. shm_mnt = kern_mount(&shmem_fs_type);
  2897. BUG_ON(IS_ERR(shm_mnt));
  2898. return 0;
  2899. }
  2900. int shmem_unuse(swp_entry_t swap, struct page *page)
  2901. {
  2902. return 0;
  2903. }
  2904. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  2905. {
  2906. return 0;
  2907. }
  2908. void shmem_unlock_mapping(struct address_space *mapping)
  2909. {
  2910. }
  2911. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  2912. {
  2913. truncate_inode_pages_range(inode->i_mapping, lstart, lend);
  2914. }
  2915. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  2916. #define shmem_vm_ops generic_file_vm_ops
  2917. #define shmem_file_operations ramfs_file_operations
  2918. #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
  2919. #define shmem_acct_size(flags, size) 0
  2920. #define shmem_unacct_size(flags, size) do {} while (0)
  2921. #endif /* CONFIG_SHMEM */
  2922. /* common code */
  2923. static struct file *__shmem_file_setup(const char *name, loff_t size,
  2924. unsigned long flags, unsigned int i_flags)
  2925. {
  2926. int error;
  2927. struct file *file;
  2928. struct inode *inode;
  2929. struct path path;
  2930. struct dentry *root;
  2931. struct qstr this;
  2932. if (IS_ERR(shm_mnt))
  2933. return (void *)shm_mnt;
  2934. if (size < 0 || size > MAX_LFS_FILESIZE)
  2935. return ERR_PTR(-EINVAL);
  2936. if (shmem_acct_size(flags, size))
  2937. return ERR_PTR(-ENOMEM);
  2938. error = -ENOMEM;
  2939. this.name = name;
  2940. this.len = strlen(name);
  2941. this.hash = 0; /* will go */
  2942. root = shm_mnt->mnt_root;
  2943. path.dentry = d_alloc(root, &this);
  2944. if (!path.dentry)
  2945. goto put_memory;
  2946. path.mnt = mntget(shm_mnt);
  2947. error = -ENOSPC;
  2948. inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
  2949. if (!inode)
  2950. goto put_dentry;
  2951. inode->i_flags |= i_flags;
  2952. d_instantiate(path.dentry, inode);
  2953. inode->i_size = size;
  2954. clear_nlink(inode); /* It is unlinked */
  2955. #ifndef CONFIG_MMU
  2956. error = ramfs_nommu_expand_for_mapping(inode, size);
  2957. if (error)
  2958. goto put_dentry;
  2959. #endif
  2960. error = -ENFILE;
  2961. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  2962. &shmem_file_operations);
  2963. if (!file)
  2964. goto put_dentry;
  2965. return file;
  2966. put_dentry:
  2967. path_put(&path);
  2968. put_memory:
  2969. shmem_unacct_size(flags, size);
  2970. return ERR_PTR(error);
  2971. }
  2972. /**
  2973. * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
  2974. * kernel internal. There will be NO LSM permission checks against the
  2975. * underlying inode. So users of this interface must do LSM checks at a
  2976. * higher layer. The one user is the big_key implementation. LSM checks
  2977. * are provided at the key level rather than the inode level.
  2978. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2979. * @size: size to be set for the file
  2980. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2981. */
  2982. struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
  2983. {
  2984. return __shmem_file_setup(name, size, flags, S_PRIVATE);
  2985. }
  2986. /**
  2987. * shmem_file_setup - get an unlinked file living in tmpfs
  2988. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2989. * @size: size to be set for the file
  2990. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2991. */
  2992. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  2993. {
  2994. return __shmem_file_setup(name, size, flags, 0);
  2995. }
  2996. EXPORT_SYMBOL_GPL(shmem_file_setup);
  2997. void shmem_set_file(struct vm_area_struct *vma, struct file *file)
  2998. {
  2999. if (vma->vm_file)
  3000. fput(vma->vm_file);
  3001. vma->vm_file = file;
  3002. vma->vm_ops = &shmem_vm_ops;
  3003. }
  3004. /**
  3005. * shmem_zero_setup - setup a shared anonymous mapping
  3006. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  3007. */
  3008. int shmem_zero_setup(struct vm_area_struct *vma)
  3009. {
  3010. struct file *file;
  3011. loff_t size = vma->vm_end - vma->vm_start;
  3012. /*
  3013. * Cloning a new file under mmap_sem leads to a lock ordering conflict
  3014. * between XFS directory reading and selinux: since this file is only
  3015. * accessible to the user through its mapping, use S_PRIVATE flag to
  3016. * bypass file security, in the same way as shmem_kernel_file_setup().
  3017. */
  3018. file = __shmem_file_setup("dev/zero", size, vma->vm_flags, S_PRIVATE);
  3019. if (IS_ERR(file))
  3020. return PTR_ERR(file);
  3021. shmem_set_file(vma, file);
  3022. return 0;
  3023. }
  3024. /**
  3025. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  3026. * @mapping: the page's address_space
  3027. * @index: the page index
  3028. * @gfp: the page allocator flags to use if allocating
  3029. *
  3030. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  3031. * with any new page allocations done using the specified allocation flags.
  3032. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  3033. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  3034. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  3035. *
  3036. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  3037. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  3038. */
  3039. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  3040. pgoff_t index, gfp_t gfp)
  3041. {
  3042. #ifdef CONFIG_SHMEM
  3043. struct inode *inode = mapping->host;
  3044. struct page *page;
  3045. int error;
  3046. BUG_ON(mapping->a_ops != &shmem_aops);
  3047. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
  3048. if (error)
  3049. page = ERR_PTR(error);
  3050. else
  3051. unlock_page(page);
  3052. return page;
  3053. #else
  3054. /*
  3055. * The tiny !SHMEM case uses ramfs without swap
  3056. */
  3057. return read_cache_page_gfp(mapping, index, gfp);
  3058. #endif
  3059. }
  3060. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);