shmem.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195
  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-2005 Hugh Dickins.
  10. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  11. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  12. *
  13. * Extended attribute support for tmpfs:
  14. * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  15. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  16. *
  17. * tiny-shmem:
  18. * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
  19. *
  20. * This file is released under the GPL.
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/init.h>
  24. #include <linux/vfs.h>
  25. #include <linux/mount.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/file.h>
  28. #include <linux/mm.h>
  29. #include <linux/module.h>
  30. #include <linux/percpu_counter.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/generic_acl.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/splice.h>
  51. #include <linux/security.h>
  52. #include <linux/swapops.h>
  53. #include <linux/mempolicy.h>
  54. #include <linux/namei.h>
  55. #include <linux/ctype.h>
  56. #include <linux/migrate.h>
  57. #include <linux/highmem.h>
  58. #include <linux/seq_file.h>
  59. #include <linux/magic.h>
  60. #include <asm/uaccess.h>
  61. #include <asm/div64.h>
  62. #include <asm/pgtable.h>
  63. /*
  64. * The maximum size of a shmem/tmpfs file is limited by the maximum size of
  65. * its triple-indirect swap vector - see illustration at shmem_swp_entry().
  66. *
  67. * With 4kB page size, maximum file size is just over 2TB on a 32-bit kernel,
  68. * but one eighth of that on a 64-bit kernel. With 8kB page size, maximum
  69. * file size is just over 4TB on a 64-bit kernel, but 16TB on a 32-bit kernel,
  70. * MAX_LFS_FILESIZE being then more restrictive than swap vector layout.
  71. *
  72. * We use / and * instead of shifts in the definitions below, so that the swap
  73. * vector can be tested with small even values (e.g. 20) for ENTRIES_PER_PAGE.
  74. */
  75. #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
  76. #define ENTRIES_PER_PAGEPAGE ((unsigned long long)ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
  77. #define SHMSWP_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
  78. #define SHMSWP_MAX_BYTES (SHMSWP_MAX_INDEX << PAGE_CACHE_SHIFT)
  79. #define SHMEM_MAX_BYTES min_t(unsigned long long, SHMSWP_MAX_BYTES, MAX_LFS_FILESIZE)
  80. #define SHMEM_MAX_INDEX ((unsigned long)((SHMEM_MAX_BYTES+1) >> PAGE_CACHE_SHIFT))
  81. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  82. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  83. /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
  84. #define SHMEM_PAGEIN VM_READ
  85. #define SHMEM_TRUNCATE VM_WRITE
  86. /* Definition to limit shmem_truncate's steps between cond_rescheds */
  87. #define LATENCY_LIMIT 64
  88. /* Pretend that each entry is of this size in directory's i_size */
  89. #define BOGO_DIRENT_SIZE 20
  90. struct shmem_xattr {
  91. struct list_head list; /* anchored by shmem_inode_info->xattr_list */
  92. char *name; /* xattr name */
  93. size_t size;
  94. char value[0];
  95. };
  96. /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
  97. enum sgp_type {
  98. SGP_READ, /* don't exceed i_size, don't allocate page */
  99. SGP_CACHE, /* don't exceed i_size, may allocate page */
  100. SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
  101. SGP_WRITE, /* may exceed i_size, may allocate page */
  102. };
  103. #ifdef CONFIG_TMPFS
  104. static unsigned long shmem_default_max_blocks(void)
  105. {
  106. return totalram_pages / 2;
  107. }
  108. static unsigned long shmem_default_max_inodes(void)
  109. {
  110. return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
  111. }
  112. #endif
  113. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  114. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
  115. static inline int shmem_getpage(struct inode *inode, pgoff_t index,
  116. struct page **pagep, enum sgp_type sgp, int *fault_type)
  117. {
  118. return shmem_getpage_gfp(inode, index, pagep, sgp,
  119. mapping_gfp_mask(inode->i_mapping), fault_type);
  120. }
  121. static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
  122. {
  123. /*
  124. * The above definition of ENTRIES_PER_PAGE, and the use of
  125. * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
  126. * might be reconsidered if it ever diverges from PAGE_SIZE.
  127. *
  128. * Mobility flags are masked out as swap vectors cannot move
  129. */
  130. return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
  131. PAGE_CACHE_SHIFT-PAGE_SHIFT);
  132. }
  133. static inline void shmem_dir_free(struct page *page)
  134. {
  135. __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
  136. }
  137. static struct page **shmem_dir_map(struct page *page)
  138. {
  139. return (struct page **)kmap_atomic(page, KM_USER0);
  140. }
  141. static inline void shmem_dir_unmap(struct page **dir)
  142. {
  143. kunmap_atomic(dir, KM_USER0);
  144. }
  145. static swp_entry_t *shmem_swp_map(struct page *page)
  146. {
  147. return (swp_entry_t *)kmap_atomic(page, KM_USER1);
  148. }
  149. static inline void shmem_swp_balance_unmap(void)
  150. {
  151. /*
  152. * When passing a pointer to an i_direct entry, to code which
  153. * also handles indirect entries and so will shmem_swp_unmap,
  154. * we must arrange for the preempt count to remain in balance.
  155. * What kmap_atomic of a lowmem page does depends on config
  156. * and architecture, so pretend to kmap_atomic some lowmem page.
  157. */
  158. (void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
  159. }
  160. static inline void shmem_swp_unmap(swp_entry_t *entry)
  161. {
  162. kunmap_atomic(entry, KM_USER1);
  163. }
  164. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  165. {
  166. return sb->s_fs_info;
  167. }
  168. /*
  169. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  170. * for shared memory and for shared anonymous (/dev/zero) mappings
  171. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  172. * consistent with the pre-accounting of private mappings ...
  173. */
  174. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  175. {
  176. return (flags & VM_NORESERVE) ?
  177. 0 : security_vm_enough_memory_kern(VM_ACCT(size));
  178. }
  179. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  180. {
  181. if (!(flags & VM_NORESERVE))
  182. vm_unacct_memory(VM_ACCT(size));
  183. }
  184. /*
  185. * ... whereas tmpfs objects are accounted incrementally as
  186. * pages are allocated, in order to allow huge sparse files.
  187. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  188. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  189. */
  190. static inline int shmem_acct_block(unsigned long flags)
  191. {
  192. return (flags & VM_NORESERVE) ?
  193. security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
  194. }
  195. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  196. {
  197. if (flags & VM_NORESERVE)
  198. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  199. }
  200. static const struct super_operations shmem_ops;
  201. static const struct address_space_operations shmem_aops;
  202. static const struct file_operations shmem_file_operations;
  203. static const struct inode_operations shmem_inode_operations;
  204. static const struct inode_operations shmem_dir_inode_operations;
  205. static const struct inode_operations shmem_special_inode_operations;
  206. static const struct vm_operations_struct shmem_vm_ops;
  207. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  208. .ra_pages = 0, /* No readahead */
  209. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
  210. };
  211. static LIST_HEAD(shmem_swaplist);
  212. static DEFINE_MUTEX(shmem_swaplist_mutex);
  213. static void shmem_free_blocks(struct inode *inode, long pages)
  214. {
  215. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  216. if (sbinfo->max_blocks) {
  217. percpu_counter_add(&sbinfo->used_blocks, -pages);
  218. inode->i_blocks -= pages*BLOCKS_PER_PAGE;
  219. }
  220. }
  221. static int shmem_reserve_inode(struct super_block *sb)
  222. {
  223. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  224. if (sbinfo->max_inodes) {
  225. spin_lock(&sbinfo->stat_lock);
  226. if (!sbinfo->free_inodes) {
  227. spin_unlock(&sbinfo->stat_lock);
  228. return -ENOSPC;
  229. }
  230. sbinfo->free_inodes--;
  231. spin_unlock(&sbinfo->stat_lock);
  232. }
  233. return 0;
  234. }
  235. static void shmem_free_inode(struct super_block *sb)
  236. {
  237. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  238. if (sbinfo->max_inodes) {
  239. spin_lock(&sbinfo->stat_lock);
  240. sbinfo->free_inodes++;
  241. spin_unlock(&sbinfo->stat_lock);
  242. }
  243. }
  244. /**
  245. * shmem_recalc_inode - recalculate the size of an inode
  246. * @inode: inode to recalc
  247. *
  248. * We have to calculate the free blocks since the mm can drop
  249. * undirtied hole pages behind our back.
  250. *
  251. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  252. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  253. *
  254. * It has to be called with the spinlock held.
  255. */
  256. static void shmem_recalc_inode(struct inode *inode)
  257. {
  258. struct shmem_inode_info *info = SHMEM_I(inode);
  259. long freed;
  260. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  261. if (freed > 0) {
  262. info->alloced -= freed;
  263. shmem_unacct_blocks(info->flags, freed);
  264. shmem_free_blocks(inode, freed);
  265. }
  266. }
  267. /**
  268. * shmem_swp_entry - find the swap vector position in the info structure
  269. * @info: info structure for the inode
  270. * @index: index of the page to find
  271. * @page: optional page to add to the structure. Has to be preset to
  272. * all zeros
  273. *
  274. * If there is no space allocated yet it will return NULL when
  275. * page is NULL, else it will use the page for the needed block,
  276. * setting it to NULL on return to indicate that it has been used.
  277. *
  278. * The swap vector is organized the following way:
  279. *
  280. * There are SHMEM_NR_DIRECT entries directly stored in the
  281. * shmem_inode_info structure. So small files do not need an addional
  282. * allocation.
  283. *
  284. * For pages with index > SHMEM_NR_DIRECT there is the pointer
  285. * i_indirect which points to a page which holds in the first half
  286. * doubly indirect blocks, in the second half triple indirect blocks:
  287. *
  288. * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
  289. * following layout (for SHMEM_NR_DIRECT == 16):
  290. *
  291. * i_indirect -> dir --> 16-19
  292. * | +-> 20-23
  293. * |
  294. * +-->dir2 --> 24-27
  295. * | +-> 28-31
  296. * | +-> 32-35
  297. * | +-> 36-39
  298. * |
  299. * +-->dir3 --> 40-43
  300. * +-> 44-47
  301. * +-> 48-51
  302. * +-> 52-55
  303. */
  304. static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
  305. {
  306. unsigned long offset;
  307. struct page **dir;
  308. struct page *subdir;
  309. if (index < SHMEM_NR_DIRECT) {
  310. shmem_swp_balance_unmap();
  311. return info->i_direct+index;
  312. }
  313. if (!info->i_indirect) {
  314. if (page) {
  315. info->i_indirect = *page;
  316. *page = NULL;
  317. }
  318. return NULL; /* need another page */
  319. }
  320. index -= SHMEM_NR_DIRECT;
  321. offset = index % ENTRIES_PER_PAGE;
  322. index /= ENTRIES_PER_PAGE;
  323. dir = shmem_dir_map(info->i_indirect);
  324. if (index >= ENTRIES_PER_PAGE/2) {
  325. index -= ENTRIES_PER_PAGE/2;
  326. dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
  327. index %= ENTRIES_PER_PAGE;
  328. subdir = *dir;
  329. if (!subdir) {
  330. if (page) {
  331. *dir = *page;
  332. *page = NULL;
  333. }
  334. shmem_dir_unmap(dir);
  335. return NULL; /* need another page */
  336. }
  337. shmem_dir_unmap(dir);
  338. dir = shmem_dir_map(subdir);
  339. }
  340. dir += index;
  341. subdir = *dir;
  342. if (!subdir) {
  343. if (!page || !(subdir = *page)) {
  344. shmem_dir_unmap(dir);
  345. return NULL; /* need a page */
  346. }
  347. *dir = subdir;
  348. *page = NULL;
  349. }
  350. shmem_dir_unmap(dir);
  351. return shmem_swp_map(subdir) + offset;
  352. }
  353. static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
  354. {
  355. long incdec = value? 1: -1;
  356. entry->val = value;
  357. info->swapped += incdec;
  358. if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
  359. struct page *page = kmap_atomic_to_page(entry);
  360. set_page_private(page, page_private(page) + incdec);
  361. }
  362. }
  363. /**
  364. * shmem_swp_alloc - get the position of the swap entry for the page.
  365. * @info: info structure for the inode
  366. * @index: index of the page to find
  367. * @sgp: check and recheck i_size? skip allocation?
  368. * @gfp: gfp mask to use for any page allocation
  369. *
  370. * If the entry does not exist, allocate it.
  371. */
  372. static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info,
  373. unsigned long index, enum sgp_type sgp, gfp_t gfp)
  374. {
  375. struct inode *inode = &info->vfs_inode;
  376. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  377. struct page *page = NULL;
  378. swp_entry_t *entry;
  379. if (sgp != SGP_WRITE &&
  380. ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  381. return ERR_PTR(-EINVAL);
  382. while (!(entry = shmem_swp_entry(info, index, &page))) {
  383. if (sgp == SGP_READ)
  384. return shmem_swp_map(ZERO_PAGE(0));
  385. /*
  386. * Test used_blocks against 1 less max_blocks, since we have 1 data
  387. * page (and perhaps indirect index pages) yet to allocate:
  388. * a waste to allocate index if we cannot allocate data.
  389. */
  390. if (sbinfo->max_blocks) {
  391. if (percpu_counter_compare(&sbinfo->used_blocks,
  392. sbinfo->max_blocks - 1) >= 0)
  393. return ERR_PTR(-ENOSPC);
  394. percpu_counter_inc(&sbinfo->used_blocks);
  395. inode->i_blocks += BLOCKS_PER_PAGE;
  396. }
  397. spin_unlock(&info->lock);
  398. page = shmem_dir_alloc(gfp);
  399. spin_lock(&info->lock);
  400. if (!page) {
  401. shmem_free_blocks(inode, 1);
  402. return ERR_PTR(-ENOMEM);
  403. }
  404. if (sgp != SGP_WRITE &&
  405. ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  406. entry = ERR_PTR(-EINVAL);
  407. break;
  408. }
  409. if (info->next_index <= index)
  410. info->next_index = index + 1;
  411. }
  412. if (page) {
  413. /* another task gave its page, or truncated the file */
  414. shmem_free_blocks(inode, 1);
  415. shmem_dir_free(page);
  416. }
  417. if (info->next_index <= index && !IS_ERR(entry))
  418. info->next_index = index + 1;
  419. return entry;
  420. }
  421. /**
  422. * shmem_free_swp - free some swap entries in a directory
  423. * @dir: pointer to the directory
  424. * @edir: pointer after last entry of the directory
  425. * @punch_lock: pointer to spinlock when needed for the holepunch case
  426. */
  427. static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
  428. spinlock_t *punch_lock)
  429. {
  430. spinlock_t *punch_unlock = NULL;
  431. swp_entry_t *ptr;
  432. int freed = 0;
  433. for (ptr = dir; ptr < edir; ptr++) {
  434. if (ptr->val) {
  435. if (unlikely(punch_lock)) {
  436. punch_unlock = punch_lock;
  437. punch_lock = NULL;
  438. spin_lock(punch_unlock);
  439. if (!ptr->val)
  440. continue;
  441. }
  442. free_swap_and_cache(*ptr);
  443. *ptr = (swp_entry_t){0};
  444. freed++;
  445. }
  446. }
  447. if (punch_unlock)
  448. spin_unlock(punch_unlock);
  449. return freed;
  450. }
  451. static int shmem_map_and_free_swp(struct page *subdir, int offset,
  452. int limit, struct page ***dir, spinlock_t *punch_lock)
  453. {
  454. swp_entry_t *ptr;
  455. int freed = 0;
  456. ptr = shmem_swp_map(subdir);
  457. for (; offset < limit; offset += LATENCY_LIMIT) {
  458. int size = limit - offset;
  459. if (size > LATENCY_LIMIT)
  460. size = LATENCY_LIMIT;
  461. freed += shmem_free_swp(ptr+offset, ptr+offset+size,
  462. punch_lock);
  463. if (need_resched()) {
  464. shmem_swp_unmap(ptr);
  465. if (*dir) {
  466. shmem_dir_unmap(*dir);
  467. *dir = NULL;
  468. }
  469. cond_resched();
  470. ptr = shmem_swp_map(subdir);
  471. }
  472. }
  473. shmem_swp_unmap(ptr);
  474. return freed;
  475. }
  476. static void shmem_free_pages(struct list_head *next)
  477. {
  478. struct page *page;
  479. int freed = 0;
  480. do {
  481. page = container_of(next, struct page, lru);
  482. next = next->next;
  483. shmem_dir_free(page);
  484. freed++;
  485. if (freed >= LATENCY_LIMIT) {
  486. cond_resched();
  487. freed = 0;
  488. }
  489. } while (next);
  490. }
  491. void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
  492. {
  493. struct shmem_inode_info *info = SHMEM_I(inode);
  494. unsigned long idx;
  495. unsigned long size;
  496. unsigned long limit;
  497. unsigned long stage;
  498. unsigned long diroff;
  499. struct page **dir;
  500. struct page *topdir;
  501. struct page *middir;
  502. struct page *subdir;
  503. swp_entry_t *ptr;
  504. LIST_HEAD(pages_to_free);
  505. long nr_pages_to_free = 0;
  506. long nr_swaps_freed = 0;
  507. int offset;
  508. int freed;
  509. int punch_hole;
  510. spinlock_t *needs_lock;
  511. spinlock_t *punch_lock;
  512. unsigned long upper_limit;
  513. truncate_inode_pages_range(inode->i_mapping, start, end);
  514. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  515. idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  516. if (idx >= info->next_index)
  517. return;
  518. spin_lock(&info->lock);
  519. info->flags |= SHMEM_TRUNCATE;
  520. if (likely(end == (loff_t) -1)) {
  521. limit = info->next_index;
  522. upper_limit = SHMEM_MAX_INDEX;
  523. info->next_index = idx;
  524. needs_lock = NULL;
  525. punch_hole = 0;
  526. } else {
  527. if (end + 1 >= inode->i_size) { /* we may free a little more */
  528. limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
  529. PAGE_CACHE_SHIFT;
  530. upper_limit = SHMEM_MAX_INDEX;
  531. } else {
  532. limit = (end + 1) >> PAGE_CACHE_SHIFT;
  533. upper_limit = limit;
  534. }
  535. needs_lock = &info->lock;
  536. punch_hole = 1;
  537. }
  538. topdir = info->i_indirect;
  539. if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
  540. info->i_indirect = NULL;
  541. nr_pages_to_free++;
  542. list_add(&topdir->lru, &pages_to_free);
  543. }
  544. spin_unlock(&info->lock);
  545. if (info->swapped && idx < SHMEM_NR_DIRECT) {
  546. ptr = info->i_direct;
  547. size = limit;
  548. if (size > SHMEM_NR_DIRECT)
  549. size = SHMEM_NR_DIRECT;
  550. nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
  551. }
  552. /*
  553. * If there are no indirect blocks or we are punching a hole
  554. * below indirect blocks, nothing to be done.
  555. */
  556. if (!topdir || limit <= SHMEM_NR_DIRECT)
  557. goto done2;
  558. /*
  559. * The truncation case has already dropped info->lock, and we're safe
  560. * because i_size and next_index have already been lowered, preventing
  561. * access beyond. But in the punch_hole case, we still need to take
  562. * the lock when updating the swap directory, because there might be
  563. * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
  564. * shmem_writepage. However, whenever we find we can remove a whole
  565. * directory page (not at the misaligned start or end of the range),
  566. * we first NULLify its pointer in the level above, and then have no
  567. * need to take the lock when updating its contents: needs_lock and
  568. * punch_lock (either pointing to info->lock or NULL) manage this.
  569. */
  570. upper_limit -= SHMEM_NR_DIRECT;
  571. limit -= SHMEM_NR_DIRECT;
  572. idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
  573. offset = idx % ENTRIES_PER_PAGE;
  574. idx -= offset;
  575. dir = shmem_dir_map(topdir);
  576. stage = ENTRIES_PER_PAGEPAGE/2;
  577. if (idx < ENTRIES_PER_PAGEPAGE/2) {
  578. middir = topdir;
  579. diroff = idx/ENTRIES_PER_PAGE;
  580. } else {
  581. dir += ENTRIES_PER_PAGE/2;
  582. dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
  583. while (stage <= idx)
  584. stage += ENTRIES_PER_PAGEPAGE;
  585. middir = *dir;
  586. if (*dir) {
  587. diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
  588. ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
  589. if (!diroff && !offset && upper_limit >= stage) {
  590. if (needs_lock) {
  591. spin_lock(needs_lock);
  592. *dir = NULL;
  593. spin_unlock(needs_lock);
  594. needs_lock = NULL;
  595. } else
  596. *dir = NULL;
  597. nr_pages_to_free++;
  598. list_add(&middir->lru, &pages_to_free);
  599. }
  600. shmem_dir_unmap(dir);
  601. dir = shmem_dir_map(middir);
  602. } else {
  603. diroff = 0;
  604. offset = 0;
  605. idx = stage;
  606. }
  607. }
  608. for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
  609. if (unlikely(idx == stage)) {
  610. shmem_dir_unmap(dir);
  611. dir = shmem_dir_map(topdir) +
  612. ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
  613. while (!*dir) {
  614. dir++;
  615. idx += ENTRIES_PER_PAGEPAGE;
  616. if (idx >= limit)
  617. goto done1;
  618. }
  619. stage = idx + ENTRIES_PER_PAGEPAGE;
  620. middir = *dir;
  621. if (punch_hole)
  622. needs_lock = &info->lock;
  623. if (upper_limit >= stage) {
  624. if (needs_lock) {
  625. spin_lock(needs_lock);
  626. *dir = NULL;
  627. spin_unlock(needs_lock);
  628. needs_lock = NULL;
  629. } else
  630. *dir = NULL;
  631. nr_pages_to_free++;
  632. list_add(&middir->lru, &pages_to_free);
  633. }
  634. shmem_dir_unmap(dir);
  635. cond_resched();
  636. dir = shmem_dir_map(middir);
  637. diroff = 0;
  638. }
  639. punch_lock = needs_lock;
  640. subdir = dir[diroff];
  641. if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
  642. if (needs_lock) {
  643. spin_lock(needs_lock);
  644. dir[diroff] = NULL;
  645. spin_unlock(needs_lock);
  646. punch_lock = NULL;
  647. } else
  648. dir[diroff] = NULL;
  649. nr_pages_to_free++;
  650. list_add(&subdir->lru, &pages_to_free);
  651. }
  652. if (subdir && page_private(subdir) /* has swap entries */) {
  653. size = limit - idx;
  654. if (size > ENTRIES_PER_PAGE)
  655. size = ENTRIES_PER_PAGE;
  656. freed = shmem_map_and_free_swp(subdir,
  657. offset, size, &dir, punch_lock);
  658. if (!dir)
  659. dir = shmem_dir_map(middir);
  660. nr_swaps_freed += freed;
  661. if (offset || punch_lock) {
  662. spin_lock(&info->lock);
  663. set_page_private(subdir,
  664. page_private(subdir) - freed);
  665. spin_unlock(&info->lock);
  666. } else
  667. BUG_ON(page_private(subdir) != freed);
  668. }
  669. offset = 0;
  670. }
  671. done1:
  672. shmem_dir_unmap(dir);
  673. done2:
  674. if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
  675. /*
  676. * Call truncate_inode_pages again: racing shmem_unuse_inode
  677. * may have swizzled a page in from swap since
  678. * truncate_pagecache or generic_delete_inode did it, before we
  679. * lowered next_index. Also, though shmem_getpage checks
  680. * i_size before adding to cache, no recheck after: so fix the
  681. * narrow window there too.
  682. */
  683. truncate_inode_pages_range(inode->i_mapping, start, end);
  684. }
  685. spin_lock(&info->lock);
  686. info->flags &= ~SHMEM_TRUNCATE;
  687. info->swapped -= nr_swaps_freed;
  688. if (nr_pages_to_free)
  689. shmem_free_blocks(inode, nr_pages_to_free);
  690. shmem_recalc_inode(inode);
  691. spin_unlock(&info->lock);
  692. /*
  693. * Empty swap vector directory pages to be freed?
  694. */
  695. if (!list_empty(&pages_to_free)) {
  696. pages_to_free.prev->next = NULL;
  697. shmem_free_pages(pages_to_free.next);
  698. }
  699. }
  700. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  701. static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
  702. {
  703. struct inode *inode = dentry->d_inode;
  704. int error;
  705. error = inode_change_ok(inode, attr);
  706. if (error)
  707. return error;
  708. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  709. loff_t oldsize = inode->i_size;
  710. loff_t newsize = attr->ia_size;
  711. struct page *page = NULL;
  712. if (newsize < oldsize) {
  713. /*
  714. * If truncating down to a partial page, then
  715. * if that page is already allocated, hold it
  716. * in memory until the truncation is over, so
  717. * truncate_partial_page cannot miss it were
  718. * it assigned to swap.
  719. */
  720. if (newsize & (PAGE_CACHE_SIZE-1)) {
  721. (void) shmem_getpage(inode,
  722. newsize >> PAGE_CACHE_SHIFT,
  723. &page, SGP_READ, NULL);
  724. if (page)
  725. unlock_page(page);
  726. }
  727. /*
  728. * Reset SHMEM_PAGEIN flag so that shmem_truncate can
  729. * detect if any pages might have been added to cache
  730. * after truncate_inode_pages. But we needn't bother
  731. * if it's being fully truncated to zero-length: the
  732. * nrpages check is efficient enough in that case.
  733. */
  734. if (newsize) {
  735. struct shmem_inode_info *info = SHMEM_I(inode);
  736. spin_lock(&info->lock);
  737. info->flags &= ~SHMEM_PAGEIN;
  738. spin_unlock(&info->lock);
  739. }
  740. }
  741. if (newsize != oldsize) {
  742. i_size_write(inode, newsize);
  743. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  744. }
  745. if (newsize < oldsize) {
  746. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  747. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  748. shmem_truncate_range(inode, newsize, (loff_t)-1);
  749. /* unmap again to remove racily COWed private pages */
  750. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  751. }
  752. if (page)
  753. page_cache_release(page);
  754. }
  755. setattr_copy(inode, attr);
  756. #ifdef CONFIG_TMPFS_POSIX_ACL
  757. if (attr->ia_valid & ATTR_MODE)
  758. error = generic_acl_chmod(inode);
  759. #endif
  760. return error;
  761. }
  762. static void shmem_evict_inode(struct inode *inode)
  763. {
  764. struct shmem_inode_info *info = SHMEM_I(inode);
  765. struct shmem_xattr *xattr, *nxattr;
  766. if (inode->i_mapping->a_ops == &shmem_aops) {
  767. shmem_unacct_size(info->flags, inode->i_size);
  768. inode->i_size = 0;
  769. shmem_truncate_range(inode, 0, (loff_t)-1);
  770. if (!list_empty(&info->swaplist)) {
  771. mutex_lock(&shmem_swaplist_mutex);
  772. list_del_init(&info->swaplist);
  773. mutex_unlock(&shmem_swaplist_mutex);
  774. }
  775. }
  776. list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
  777. kfree(xattr->name);
  778. kfree(xattr);
  779. }
  780. BUG_ON(inode->i_blocks);
  781. shmem_free_inode(inode->i_sb);
  782. end_writeback(inode);
  783. }
  784. static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
  785. {
  786. swp_entry_t *ptr;
  787. for (ptr = dir; ptr < edir; ptr++) {
  788. if (ptr->val == entry.val)
  789. return ptr - dir;
  790. }
  791. return -1;
  792. }
  793. static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
  794. {
  795. struct address_space *mapping;
  796. unsigned long idx;
  797. unsigned long size;
  798. unsigned long limit;
  799. unsigned long stage;
  800. struct page **dir;
  801. struct page *subdir;
  802. swp_entry_t *ptr;
  803. int offset;
  804. int error;
  805. idx = 0;
  806. ptr = info->i_direct;
  807. spin_lock(&info->lock);
  808. if (!info->swapped) {
  809. list_del_init(&info->swaplist);
  810. goto lost2;
  811. }
  812. limit = info->next_index;
  813. size = limit;
  814. if (size > SHMEM_NR_DIRECT)
  815. size = SHMEM_NR_DIRECT;
  816. offset = shmem_find_swp(entry, ptr, ptr+size);
  817. if (offset >= 0) {
  818. shmem_swp_balance_unmap();
  819. goto found;
  820. }
  821. if (!info->i_indirect)
  822. goto lost2;
  823. dir = shmem_dir_map(info->i_indirect);
  824. stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
  825. for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
  826. if (unlikely(idx == stage)) {
  827. shmem_dir_unmap(dir-1);
  828. if (cond_resched_lock(&info->lock)) {
  829. /* check it has not been truncated */
  830. if (limit > info->next_index) {
  831. limit = info->next_index;
  832. if (idx >= limit)
  833. goto lost2;
  834. }
  835. }
  836. dir = shmem_dir_map(info->i_indirect) +
  837. ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
  838. while (!*dir) {
  839. dir++;
  840. idx += ENTRIES_PER_PAGEPAGE;
  841. if (idx >= limit)
  842. goto lost1;
  843. }
  844. stage = idx + ENTRIES_PER_PAGEPAGE;
  845. subdir = *dir;
  846. shmem_dir_unmap(dir);
  847. dir = shmem_dir_map(subdir);
  848. }
  849. subdir = *dir;
  850. if (subdir && page_private(subdir)) {
  851. ptr = shmem_swp_map(subdir);
  852. size = limit - idx;
  853. if (size > ENTRIES_PER_PAGE)
  854. size = ENTRIES_PER_PAGE;
  855. offset = shmem_find_swp(entry, ptr, ptr+size);
  856. shmem_swp_unmap(ptr);
  857. if (offset >= 0) {
  858. shmem_dir_unmap(dir);
  859. ptr = shmem_swp_map(subdir);
  860. goto found;
  861. }
  862. }
  863. }
  864. lost1:
  865. shmem_dir_unmap(dir-1);
  866. lost2:
  867. spin_unlock(&info->lock);
  868. return 0;
  869. found:
  870. idx += offset;
  871. ptr += offset;
  872. /*
  873. * Move _head_ to start search for next from here.
  874. * But be careful: shmem_evict_inode checks list_empty without taking
  875. * mutex, and there's an instant in list_move_tail when info->swaplist
  876. * would appear empty, if it were the only one on shmem_swaplist. We
  877. * could avoid doing it if inode NULL; or use this minor optimization.
  878. */
  879. if (shmem_swaplist.next != &info->swaplist)
  880. list_move_tail(&shmem_swaplist, &info->swaplist);
  881. /*
  882. * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
  883. * but also to hold up shmem_evict_inode(): so inode cannot be freed
  884. * beneath us (pagelock doesn't help until the page is in pagecache).
  885. */
  886. mapping = info->vfs_inode.i_mapping;
  887. error = add_to_page_cache_locked(page, mapping, idx, GFP_NOWAIT);
  888. /* which does mem_cgroup_uncharge_cache_page on error */
  889. if (error == -EEXIST) {
  890. struct page *filepage = find_get_page(mapping, idx);
  891. error = 1;
  892. if (filepage) {
  893. /*
  894. * There might be a more uptodate page coming down
  895. * from a stacked writepage: forget our swappage if so.
  896. */
  897. if (PageUptodate(filepage))
  898. error = 0;
  899. page_cache_release(filepage);
  900. }
  901. }
  902. if (!error) {
  903. delete_from_swap_cache(page);
  904. set_page_dirty(page);
  905. info->flags |= SHMEM_PAGEIN;
  906. shmem_swp_set(info, ptr, 0);
  907. swap_free(entry);
  908. error = 1; /* not an error, but entry was found */
  909. }
  910. shmem_swp_unmap(ptr);
  911. spin_unlock(&info->lock);
  912. return error;
  913. }
  914. /*
  915. * shmem_unuse() search for an eventually swapped out shmem page.
  916. */
  917. int shmem_unuse(swp_entry_t entry, struct page *page)
  918. {
  919. struct list_head *p, *next;
  920. struct shmem_inode_info *info;
  921. int found = 0;
  922. int error;
  923. /*
  924. * Charge page using GFP_KERNEL while we can wait, before taking
  925. * the shmem_swaplist_mutex which might hold up shmem_writepage().
  926. * Charged back to the user (not to caller) when swap account is used.
  927. * add_to_page_cache() will be called with GFP_NOWAIT.
  928. */
  929. error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
  930. if (error)
  931. goto out;
  932. /*
  933. * Try to preload while we can wait, to not make a habit of
  934. * draining atomic reserves; but don't latch on to this cpu,
  935. * it's okay if sometimes we get rescheduled after this.
  936. */
  937. error = radix_tree_preload(GFP_KERNEL);
  938. if (error)
  939. goto uncharge;
  940. radix_tree_preload_end();
  941. mutex_lock(&shmem_swaplist_mutex);
  942. list_for_each_safe(p, next, &shmem_swaplist) {
  943. info = list_entry(p, struct shmem_inode_info, swaplist);
  944. found = shmem_unuse_inode(info, entry, page);
  945. cond_resched();
  946. if (found)
  947. break;
  948. }
  949. mutex_unlock(&shmem_swaplist_mutex);
  950. uncharge:
  951. if (!found)
  952. mem_cgroup_uncharge_cache_page(page);
  953. if (found < 0)
  954. error = found;
  955. out:
  956. unlock_page(page);
  957. page_cache_release(page);
  958. return error;
  959. }
  960. /*
  961. * Move the page from the page cache to the swap cache.
  962. */
  963. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  964. {
  965. struct shmem_inode_info *info;
  966. swp_entry_t *entry, swap;
  967. struct address_space *mapping;
  968. unsigned long index;
  969. struct inode *inode;
  970. BUG_ON(!PageLocked(page));
  971. mapping = page->mapping;
  972. index = page->index;
  973. inode = mapping->host;
  974. info = SHMEM_I(inode);
  975. if (info->flags & VM_LOCKED)
  976. goto redirty;
  977. if (!total_swap_pages)
  978. goto redirty;
  979. /*
  980. * shmem_backing_dev_info's capabilities prevent regular writeback or
  981. * sync from ever calling shmem_writepage; but a stacking filesystem
  982. * may use the ->writepage of its underlying filesystem, in which case
  983. * tmpfs should write out to swap only in response to memory pressure,
  984. * and not for the writeback threads or sync. However, in those cases,
  985. * we do still want to check if there's a redundant swappage to be
  986. * discarded.
  987. */
  988. if (wbc->for_reclaim)
  989. swap = get_swap_page();
  990. else
  991. swap.val = 0;
  992. /*
  993. * Add inode to shmem_unuse()'s list of swapped-out inodes,
  994. * if it's not already there. Do it now because we cannot take
  995. * mutex while holding spinlock, and must do so before the page
  996. * is moved to swap cache, when its pagelock no longer protects
  997. * the inode from eviction. But don't unlock the mutex until
  998. * we've taken the spinlock, because shmem_unuse_inode() will
  999. * prune a !swapped inode from the swaplist under both locks.
  1000. */
  1001. if (swap.val) {
  1002. mutex_lock(&shmem_swaplist_mutex);
  1003. if (list_empty(&info->swaplist))
  1004. list_add_tail(&info->swaplist, &shmem_swaplist);
  1005. }
  1006. spin_lock(&info->lock);
  1007. if (swap.val)
  1008. mutex_unlock(&shmem_swaplist_mutex);
  1009. if (index >= info->next_index) {
  1010. BUG_ON(!(info->flags & SHMEM_TRUNCATE));
  1011. goto unlock;
  1012. }
  1013. entry = shmem_swp_entry(info, index, NULL);
  1014. if (entry->val) {
  1015. /*
  1016. * The more uptodate page coming down from a stacked
  1017. * writepage should replace our old swappage.
  1018. */
  1019. free_swap_and_cache(*entry);
  1020. shmem_swp_set(info, entry, 0);
  1021. }
  1022. shmem_recalc_inode(inode);
  1023. if (swap.val && add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
  1024. delete_from_page_cache(page);
  1025. shmem_swp_set(info, entry, swap.val);
  1026. shmem_swp_unmap(entry);
  1027. swap_shmem_alloc(swap);
  1028. spin_unlock(&info->lock);
  1029. BUG_ON(page_mapped(page));
  1030. swap_writepage(page, wbc);
  1031. return 0;
  1032. }
  1033. shmem_swp_unmap(entry);
  1034. unlock:
  1035. spin_unlock(&info->lock);
  1036. /*
  1037. * add_to_swap_cache() doesn't return -EEXIST, so we can safely
  1038. * clear SWAP_HAS_CACHE flag.
  1039. */
  1040. swapcache_free(swap, NULL);
  1041. redirty:
  1042. set_page_dirty(page);
  1043. if (wbc->for_reclaim)
  1044. return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
  1045. unlock_page(page);
  1046. return 0;
  1047. }
  1048. #ifdef CONFIG_NUMA
  1049. #ifdef CONFIG_TMPFS
  1050. static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  1051. {
  1052. char buffer[64];
  1053. if (!mpol || mpol->mode == MPOL_DEFAULT)
  1054. return; /* show nothing */
  1055. mpol_to_str(buffer, sizeof(buffer), mpol, 1);
  1056. seq_printf(seq, ",mpol=%s", buffer);
  1057. }
  1058. static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  1059. {
  1060. struct mempolicy *mpol = NULL;
  1061. if (sbinfo->mpol) {
  1062. spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
  1063. mpol = sbinfo->mpol;
  1064. mpol_get(mpol);
  1065. spin_unlock(&sbinfo->stat_lock);
  1066. }
  1067. return mpol;
  1068. }
  1069. #endif /* CONFIG_TMPFS */
  1070. static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
  1071. struct shmem_inode_info *info, unsigned long idx)
  1072. {
  1073. struct mempolicy mpol, *spol;
  1074. struct vm_area_struct pvma;
  1075. struct page *page;
  1076. spol = mpol_cond_copy(&mpol,
  1077. mpol_shared_policy_lookup(&info->policy, idx));
  1078. /* Create a pseudo vma that just contains the policy */
  1079. pvma.vm_start = 0;
  1080. pvma.vm_pgoff = idx;
  1081. pvma.vm_ops = NULL;
  1082. pvma.vm_policy = spol;
  1083. page = swapin_readahead(entry, gfp, &pvma, 0);
  1084. return page;
  1085. }
  1086. static struct page *shmem_alloc_page(gfp_t gfp,
  1087. struct shmem_inode_info *info, unsigned long idx)
  1088. {
  1089. struct vm_area_struct pvma;
  1090. /* Create a pseudo vma that just contains the policy */
  1091. pvma.vm_start = 0;
  1092. pvma.vm_pgoff = idx;
  1093. pvma.vm_ops = NULL;
  1094. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
  1095. /*
  1096. * alloc_page_vma() will drop the shared policy reference
  1097. */
  1098. return alloc_page_vma(gfp, &pvma, 0);
  1099. }
  1100. #else /* !CONFIG_NUMA */
  1101. #ifdef CONFIG_TMPFS
  1102. static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *p)
  1103. {
  1104. }
  1105. #endif /* CONFIG_TMPFS */
  1106. static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
  1107. struct shmem_inode_info *info, unsigned long idx)
  1108. {
  1109. return swapin_readahead(entry, gfp, NULL, 0);
  1110. }
  1111. static inline struct page *shmem_alloc_page(gfp_t gfp,
  1112. struct shmem_inode_info *info, unsigned long idx)
  1113. {
  1114. return alloc_page(gfp);
  1115. }
  1116. #endif /* CONFIG_NUMA */
  1117. #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
  1118. static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  1119. {
  1120. return NULL;
  1121. }
  1122. #endif
  1123. /*
  1124. * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
  1125. *
  1126. * If we allocate a new one we do not mark it dirty. That's up to the
  1127. * vm. If we swap it in we mark it dirty since we also free the swap
  1128. * entry since a page cannot live in both the swap and page cache
  1129. */
  1130. static int shmem_getpage_gfp(struct inode *inode, pgoff_t idx,
  1131. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
  1132. {
  1133. struct address_space *mapping = inode->i_mapping;
  1134. struct shmem_inode_info *info = SHMEM_I(inode);
  1135. struct shmem_sb_info *sbinfo;
  1136. struct page *filepage = *pagep;
  1137. struct page *swappage;
  1138. struct page *prealloc_page = NULL;
  1139. swp_entry_t *entry;
  1140. swp_entry_t swap;
  1141. int error;
  1142. if (idx >= SHMEM_MAX_INDEX)
  1143. return -EFBIG;
  1144. /*
  1145. * Normally, filepage is NULL on entry, and either found
  1146. * uptodate immediately, or allocated and zeroed, or read
  1147. * in under swappage, which is then assigned to filepage.
  1148. * But shmem_readpage (required for splice) passes in a locked
  1149. * filepage, which may be found not uptodate by other callers
  1150. * too, and may need to be copied from the swappage read in.
  1151. */
  1152. repeat:
  1153. if (!filepage)
  1154. filepage = find_lock_page(mapping, idx);
  1155. if (filepage && PageUptodate(filepage))
  1156. goto done;
  1157. if (!filepage) {
  1158. /*
  1159. * Try to preload while we can wait, to not make a habit of
  1160. * draining atomic reserves; but don't latch on to this cpu.
  1161. */
  1162. error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
  1163. if (error)
  1164. goto failed;
  1165. radix_tree_preload_end();
  1166. if (sgp != SGP_READ && !prealloc_page) {
  1167. /* We don't care if this fails */
  1168. prealloc_page = shmem_alloc_page(gfp, info, idx);
  1169. if (prealloc_page) {
  1170. if (mem_cgroup_cache_charge(prealloc_page,
  1171. current->mm, GFP_KERNEL)) {
  1172. page_cache_release(prealloc_page);
  1173. prealloc_page = NULL;
  1174. }
  1175. }
  1176. }
  1177. }
  1178. error = 0;
  1179. spin_lock(&info->lock);
  1180. shmem_recalc_inode(inode);
  1181. entry = shmem_swp_alloc(info, idx, sgp, gfp);
  1182. if (IS_ERR(entry)) {
  1183. spin_unlock(&info->lock);
  1184. error = PTR_ERR(entry);
  1185. goto failed;
  1186. }
  1187. swap = *entry;
  1188. if (swap.val) {
  1189. /* Look it up and read it in.. */
  1190. swappage = lookup_swap_cache(swap);
  1191. if (!swappage) {
  1192. shmem_swp_unmap(entry);
  1193. spin_unlock(&info->lock);
  1194. /* here we actually do the io */
  1195. if (fault_type)
  1196. *fault_type |= VM_FAULT_MAJOR;
  1197. swappage = shmem_swapin(swap, gfp, info, idx);
  1198. if (!swappage) {
  1199. spin_lock(&info->lock);
  1200. entry = shmem_swp_alloc(info, idx, sgp, gfp);
  1201. if (IS_ERR(entry))
  1202. error = PTR_ERR(entry);
  1203. else {
  1204. if (entry->val == swap.val)
  1205. error = -ENOMEM;
  1206. shmem_swp_unmap(entry);
  1207. }
  1208. spin_unlock(&info->lock);
  1209. if (error)
  1210. goto failed;
  1211. goto repeat;
  1212. }
  1213. wait_on_page_locked(swappage);
  1214. page_cache_release(swappage);
  1215. goto repeat;
  1216. }
  1217. /* We have to do this with page locked to prevent races */
  1218. if (!trylock_page(swappage)) {
  1219. shmem_swp_unmap(entry);
  1220. spin_unlock(&info->lock);
  1221. wait_on_page_locked(swappage);
  1222. page_cache_release(swappage);
  1223. goto repeat;
  1224. }
  1225. if (PageWriteback(swappage)) {
  1226. shmem_swp_unmap(entry);
  1227. spin_unlock(&info->lock);
  1228. wait_on_page_writeback(swappage);
  1229. unlock_page(swappage);
  1230. page_cache_release(swappage);
  1231. goto repeat;
  1232. }
  1233. if (!PageUptodate(swappage)) {
  1234. shmem_swp_unmap(entry);
  1235. spin_unlock(&info->lock);
  1236. unlock_page(swappage);
  1237. page_cache_release(swappage);
  1238. error = -EIO;
  1239. goto failed;
  1240. }
  1241. if (filepage) {
  1242. shmem_swp_set(info, entry, 0);
  1243. shmem_swp_unmap(entry);
  1244. delete_from_swap_cache(swappage);
  1245. spin_unlock(&info->lock);
  1246. copy_highpage(filepage, swappage);
  1247. unlock_page(swappage);
  1248. page_cache_release(swappage);
  1249. flush_dcache_page(filepage);
  1250. SetPageUptodate(filepage);
  1251. set_page_dirty(filepage);
  1252. swap_free(swap);
  1253. } else if (!(error = add_to_page_cache_locked(swappage, mapping,
  1254. idx, GFP_NOWAIT))) {
  1255. info->flags |= SHMEM_PAGEIN;
  1256. shmem_swp_set(info, entry, 0);
  1257. shmem_swp_unmap(entry);
  1258. delete_from_swap_cache(swappage);
  1259. spin_unlock(&info->lock);
  1260. filepage = swappage;
  1261. set_page_dirty(filepage);
  1262. swap_free(swap);
  1263. } else {
  1264. shmem_swp_unmap(entry);
  1265. spin_unlock(&info->lock);
  1266. if (error == -ENOMEM) {
  1267. /*
  1268. * reclaim from proper memory cgroup and
  1269. * call memcg's OOM if needed.
  1270. */
  1271. error = mem_cgroup_shmem_charge_fallback(
  1272. swappage,
  1273. current->mm,
  1274. gfp);
  1275. if (error) {
  1276. unlock_page(swappage);
  1277. page_cache_release(swappage);
  1278. goto failed;
  1279. }
  1280. }
  1281. unlock_page(swappage);
  1282. page_cache_release(swappage);
  1283. goto repeat;
  1284. }
  1285. } else if (sgp == SGP_READ && !filepage) {
  1286. shmem_swp_unmap(entry);
  1287. filepage = find_get_page(mapping, idx);
  1288. if (filepage &&
  1289. (!PageUptodate(filepage) || !trylock_page(filepage))) {
  1290. spin_unlock(&info->lock);
  1291. wait_on_page_locked(filepage);
  1292. page_cache_release(filepage);
  1293. filepage = NULL;
  1294. goto repeat;
  1295. }
  1296. spin_unlock(&info->lock);
  1297. } else {
  1298. shmem_swp_unmap(entry);
  1299. sbinfo = SHMEM_SB(inode->i_sb);
  1300. if (sbinfo->max_blocks) {
  1301. if (percpu_counter_compare(&sbinfo->used_blocks,
  1302. sbinfo->max_blocks) >= 0 ||
  1303. shmem_acct_block(info->flags))
  1304. goto nospace;
  1305. percpu_counter_inc(&sbinfo->used_blocks);
  1306. inode->i_blocks += BLOCKS_PER_PAGE;
  1307. } else if (shmem_acct_block(info->flags))
  1308. goto nospace;
  1309. if (!filepage) {
  1310. int ret;
  1311. if (!prealloc_page) {
  1312. spin_unlock(&info->lock);
  1313. filepage = shmem_alloc_page(gfp, info, idx);
  1314. if (!filepage) {
  1315. spin_lock(&info->lock);
  1316. shmem_unacct_blocks(info->flags, 1);
  1317. shmem_free_blocks(inode, 1);
  1318. spin_unlock(&info->lock);
  1319. error = -ENOMEM;
  1320. goto failed;
  1321. }
  1322. SetPageSwapBacked(filepage);
  1323. /*
  1324. * Precharge page while we can wait, compensate
  1325. * after
  1326. */
  1327. error = mem_cgroup_cache_charge(filepage,
  1328. current->mm, GFP_KERNEL);
  1329. if (error) {
  1330. page_cache_release(filepage);
  1331. spin_lock(&info->lock);
  1332. shmem_unacct_blocks(info->flags, 1);
  1333. shmem_free_blocks(inode, 1);
  1334. spin_unlock(&info->lock);
  1335. filepage = NULL;
  1336. goto failed;
  1337. }
  1338. spin_lock(&info->lock);
  1339. } else {
  1340. filepage = prealloc_page;
  1341. prealloc_page = NULL;
  1342. SetPageSwapBacked(filepage);
  1343. }
  1344. entry = shmem_swp_alloc(info, idx, sgp, gfp);
  1345. if (IS_ERR(entry))
  1346. error = PTR_ERR(entry);
  1347. else {
  1348. swap = *entry;
  1349. shmem_swp_unmap(entry);
  1350. }
  1351. ret = error || swap.val;
  1352. if (ret)
  1353. mem_cgroup_uncharge_cache_page(filepage);
  1354. else
  1355. ret = add_to_page_cache_lru(filepage, mapping,
  1356. idx, GFP_NOWAIT);
  1357. /*
  1358. * At add_to_page_cache_lru() failure, uncharge will
  1359. * be done automatically.
  1360. */
  1361. if (ret) {
  1362. shmem_unacct_blocks(info->flags, 1);
  1363. shmem_free_blocks(inode, 1);
  1364. spin_unlock(&info->lock);
  1365. page_cache_release(filepage);
  1366. filepage = NULL;
  1367. if (error)
  1368. goto failed;
  1369. goto repeat;
  1370. }
  1371. info->flags |= SHMEM_PAGEIN;
  1372. }
  1373. info->alloced++;
  1374. spin_unlock(&info->lock);
  1375. clear_highpage(filepage);
  1376. flush_dcache_page(filepage);
  1377. SetPageUptodate(filepage);
  1378. if (sgp == SGP_DIRTY)
  1379. set_page_dirty(filepage);
  1380. }
  1381. done:
  1382. *pagep = filepage;
  1383. error = 0;
  1384. goto out;
  1385. nospace:
  1386. /*
  1387. * Perhaps the page was brought in from swap between find_lock_page
  1388. * and taking info->lock? We allow for that at add_to_page_cache_lru,
  1389. * but must also avoid reporting a spurious ENOSPC while working on a
  1390. * full tmpfs. (When filepage has been passed in to shmem_getpage, it
  1391. * is already in page cache, which prevents this race from occurring.)
  1392. */
  1393. if (!filepage) {
  1394. struct page *page = find_get_page(mapping, idx);
  1395. if (page) {
  1396. spin_unlock(&info->lock);
  1397. page_cache_release(page);
  1398. goto repeat;
  1399. }
  1400. }
  1401. spin_unlock(&info->lock);
  1402. error = -ENOSPC;
  1403. failed:
  1404. if (*pagep != filepage) {
  1405. unlock_page(filepage);
  1406. page_cache_release(filepage);
  1407. }
  1408. out:
  1409. if (prealloc_page) {
  1410. mem_cgroup_uncharge_cache_page(prealloc_page);
  1411. page_cache_release(prealloc_page);
  1412. }
  1413. return error;
  1414. }
  1415. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1416. {
  1417. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1418. int error;
  1419. int ret = VM_FAULT_LOCKED;
  1420. if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  1421. return VM_FAULT_SIGBUS;
  1422. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
  1423. if (error)
  1424. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  1425. if (ret & VM_FAULT_MAJOR) {
  1426. count_vm_event(PGMAJFAULT);
  1427. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  1428. }
  1429. return ret;
  1430. }
  1431. #ifdef CONFIG_NUMA
  1432. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  1433. {
  1434. struct inode *i = vma->vm_file->f_path.dentry->d_inode;
  1435. return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
  1436. }
  1437. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  1438. unsigned long addr)
  1439. {
  1440. struct inode *i = vma->vm_file->f_path.dentry->d_inode;
  1441. unsigned long idx;
  1442. idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1443. return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
  1444. }
  1445. #endif
  1446. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1447. {
  1448. struct inode *inode = file->f_path.dentry->d_inode;
  1449. struct shmem_inode_info *info = SHMEM_I(inode);
  1450. int retval = -ENOMEM;
  1451. spin_lock(&info->lock);
  1452. if (lock && !(info->flags & VM_LOCKED)) {
  1453. if (!user_shm_lock(inode->i_size, user))
  1454. goto out_nomem;
  1455. info->flags |= VM_LOCKED;
  1456. mapping_set_unevictable(file->f_mapping);
  1457. }
  1458. if (!lock && (info->flags & VM_LOCKED) && user) {
  1459. user_shm_unlock(inode->i_size, user);
  1460. info->flags &= ~VM_LOCKED;
  1461. mapping_clear_unevictable(file->f_mapping);
  1462. scan_mapping_unevictable_pages(file->f_mapping);
  1463. }
  1464. retval = 0;
  1465. out_nomem:
  1466. spin_unlock(&info->lock);
  1467. return retval;
  1468. }
  1469. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1470. {
  1471. file_accessed(file);
  1472. vma->vm_ops = &shmem_vm_ops;
  1473. vma->vm_flags |= VM_CAN_NONLINEAR;
  1474. return 0;
  1475. }
  1476. static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
  1477. int mode, dev_t dev, unsigned long flags)
  1478. {
  1479. struct inode *inode;
  1480. struct shmem_inode_info *info;
  1481. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1482. if (shmem_reserve_inode(sb))
  1483. return NULL;
  1484. inode = new_inode(sb);
  1485. if (inode) {
  1486. inode->i_ino = get_next_ino();
  1487. inode_init_owner(inode, dir, mode);
  1488. inode->i_blocks = 0;
  1489. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1490. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1491. inode->i_generation = get_seconds();
  1492. info = SHMEM_I(inode);
  1493. memset(info, 0, (char *)inode - (char *)info);
  1494. spin_lock_init(&info->lock);
  1495. info->flags = flags & VM_NORESERVE;
  1496. INIT_LIST_HEAD(&info->swaplist);
  1497. INIT_LIST_HEAD(&info->xattr_list);
  1498. cache_no_acl(inode);
  1499. switch (mode & S_IFMT) {
  1500. default:
  1501. inode->i_op = &shmem_special_inode_operations;
  1502. init_special_inode(inode, mode, dev);
  1503. break;
  1504. case S_IFREG:
  1505. inode->i_mapping->a_ops = &shmem_aops;
  1506. inode->i_op = &shmem_inode_operations;
  1507. inode->i_fop = &shmem_file_operations;
  1508. mpol_shared_policy_init(&info->policy,
  1509. shmem_get_sbmpol(sbinfo));
  1510. break;
  1511. case S_IFDIR:
  1512. inc_nlink(inode);
  1513. /* Some things misbehave if size == 0 on a directory */
  1514. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1515. inode->i_op = &shmem_dir_inode_operations;
  1516. inode->i_fop = &simple_dir_operations;
  1517. break;
  1518. case S_IFLNK:
  1519. /*
  1520. * Must not load anything in the rbtree,
  1521. * mpol_free_shared_policy will not be called.
  1522. */
  1523. mpol_shared_policy_init(&info->policy, NULL);
  1524. break;
  1525. }
  1526. } else
  1527. shmem_free_inode(sb);
  1528. return inode;
  1529. }
  1530. #ifdef CONFIG_TMPFS
  1531. static const struct inode_operations shmem_symlink_inode_operations;
  1532. static const struct inode_operations shmem_symlink_inline_operations;
  1533. /*
  1534. * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
  1535. * but providing them allows a tmpfs file to be used for splice, sendfile, and
  1536. * below the loop driver, in the generic fashion that many filesystems support.
  1537. */
  1538. static int shmem_readpage(struct file *file, struct page *page)
  1539. {
  1540. struct inode *inode = page->mapping->host;
  1541. int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
  1542. unlock_page(page);
  1543. return error;
  1544. }
  1545. static int
  1546. shmem_write_begin(struct file *file, struct address_space *mapping,
  1547. loff_t pos, unsigned len, unsigned flags,
  1548. struct page **pagep, void **fsdata)
  1549. {
  1550. struct inode *inode = mapping->host;
  1551. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1552. *pagep = NULL;
  1553. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1554. }
  1555. static int
  1556. shmem_write_end(struct file *file, struct address_space *mapping,
  1557. loff_t pos, unsigned len, unsigned copied,
  1558. struct page *page, void *fsdata)
  1559. {
  1560. struct inode *inode = mapping->host;
  1561. if (pos + copied > inode->i_size)
  1562. i_size_write(inode, pos + copied);
  1563. set_page_dirty(page);
  1564. unlock_page(page);
  1565. page_cache_release(page);
  1566. return copied;
  1567. }
  1568. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1569. {
  1570. struct inode *inode = filp->f_path.dentry->d_inode;
  1571. struct address_space *mapping = inode->i_mapping;
  1572. unsigned long index, offset;
  1573. enum sgp_type sgp = SGP_READ;
  1574. /*
  1575. * Might this read be for a stacking filesystem? Then when reading
  1576. * holes of a sparse file, we actually need to allocate those pages,
  1577. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  1578. */
  1579. if (segment_eq(get_fs(), KERNEL_DS))
  1580. sgp = SGP_DIRTY;
  1581. index = *ppos >> PAGE_CACHE_SHIFT;
  1582. offset = *ppos & ~PAGE_CACHE_MASK;
  1583. for (;;) {
  1584. struct page *page = NULL;
  1585. unsigned long end_index, nr, ret;
  1586. loff_t i_size = i_size_read(inode);
  1587. end_index = i_size >> PAGE_CACHE_SHIFT;
  1588. if (index > end_index)
  1589. break;
  1590. if (index == end_index) {
  1591. nr = i_size & ~PAGE_CACHE_MASK;
  1592. if (nr <= offset)
  1593. break;
  1594. }
  1595. desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
  1596. if (desc->error) {
  1597. if (desc->error == -EINVAL)
  1598. desc->error = 0;
  1599. break;
  1600. }
  1601. if (page)
  1602. unlock_page(page);
  1603. /*
  1604. * We must evaluate after, since reads (unlike writes)
  1605. * are called without i_mutex protection against truncate
  1606. */
  1607. nr = PAGE_CACHE_SIZE;
  1608. i_size = i_size_read(inode);
  1609. end_index = i_size >> PAGE_CACHE_SHIFT;
  1610. if (index == end_index) {
  1611. nr = i_size & ~PAGE_CACHE_MASK;
  1612. if (nr <= offset) {
  1613. if (page)
  1614. page_cache_release(page);
  1615. break;
  1616. }
  1617. }
  1618. nr -= offset;
  1619. if (page) {
  1620. /*
  1621. * If users can be writing to this page using arbitrary
  1622. * virtual addresses, take care about potential aliasing
  1623. * before reading the page on the kernel side.
  1624. */
  1625. if (mapping_writably_mapped(mapping))
  1626. flush_dcache_page(page);
  1627. /*
  1628. * Mark the page accessed if we read the beginning.
  1629. */
  1630. if (!offset)
  1631. mark_page_accessed(page);
  1632. } else {
  1633. page = ZERO_PAGE(0);
  1634. page_cache_get(page);
  1635. }
  1636. /*
  1637. * Ok, we have the page, and it's up-to-date, so
  1638. * now we can copy it to user space...
  1639. *
  1640. * The actor routine returns how many bytes were actually used..
  1641. * NOTE! This may not be the same as how much of a user buffer
  1642. * we filled up (we may be padding etc), so we can only update
  1643. * "pos" here (the actor routine has to update the user buffer
  1644. * pointers and the remaining count).
  1645. */
  1646. ret = actor(desc, page, offset, nr);
  1647. offset += ret;
  1648. index += offset >> PAGE_CACHE_SHIFT;
  1649. offset &= ~PAGE_CACHE_MASK;
  1650. page_cache_release(page);
  1651. if (ret != nr || !desc->count)
  1652. break;
  1653. cond_resched();
  1654. }
  1655. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1656. file_accessed(filp);
  1657. }
  1658. static ssize_t shmem_file_aio_read(struct kiocb *iocb,
  1659. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  1660. {
  1661. struct file *filp = iocb->ki_filp;
  1662. ssize_t retval;
  1663. unsigned long seg;
  1664. size_t count;
  1665. loff_t *ppos = &iocb->ki_pos;
  1666. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1667. if (retval)
  1668. return retval;
  1669. for (seg = 0; seg < nr_segs; seg++) {
  1670. read_descriptor_t desc;
  1671. desc.written = 0;
  1672. desc.arg.buf = iov[seg].iov_base;
  1673. desc.count = iov[seg].iov_len;
  1674. if (desc.count == 0)
  1675. continue;
  1676. desc.error = 0;
  1677. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1678. retval += desc.written;
  1679. if (desc.error) {
  1680. retval = retval ?: desc.error;
  1681. break;
  1682. }
  1683. if (desc.count > 0)
  1684. break;
  1685. }
  1686. return retval;
  1687. }
  1688. static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
  1689. struct pipe_inode_info *pipe, size_t len,
  1690. unsigned int flags)
  1691. {
  1692. struct address_space *mapping = in->f_mapping;
  1693. struct inode *inode = mapping->host;
  1694. unsigned int loff, nr_pages, req_pages;
  1695. struct page *pages[PIPE_DEF_BUFFERS];
  1696. struct partial_page partial[PIPE_DEF_BUFFERS];
  1697. struct page *page;
  1698. pgoff_t index, end_index;
  1699. loff_t isize, left;
  1700. int error, page_nr;
  1701. struct splice_pipe_desc spd = {
  1702. .pages = pages,
  1703. .partial = partial,
  1704. .flags = flags,
  1705. .ops = &page_cache_pipe_buf_ops,
  1706. .spd_release = spd_release_page,
  1707. };
  1708. isize = i_size_read(inode);
  1709. if (unlikely(*ppos >= isize))
  1710. return 0;
  1711. left = isize - *ppos;
  1712. if (unlikely(left < len))
  1713. len = left;
  1714. if (splice_grow_spd(pipe, &spd))
  1715. return -ENOMEM;
  1716. index = *ppos >> PAGE_CACHE_SHIFT;
  1717. loff = *ppos & ~PAGE_CACHE_MASK;
  1718. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1719. nr_pages = min(req_pages, pipe->buffers);
  1720. spd.nr_pages = find_get_pages_contig(mapping, index,
  1721. nr_pages, spd.pages);
  1722. index += spd.nr_pages;
  1723. error = 0;
  1724. while (spd.nr_pages < nr_pages) {
  1725. page = NULL;
  1726. error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
  1727. if (error)
  1728. break;
  1729. unlock_page(page);
  1730. spd.pages[spd.nr_pages++] = page;
  1731. index++;
  1732. }
  1733. index = *ppos >> PAGE_CACHE_SHIFT;
  1734. nr_pages = spd.nr_pages;
  1735. spd.nr_pages = 0;
  1736. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  1737. unsigned int this_len;
  1738. if (!len)
  1739. break;
  1740. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  1741. page = spd.pages[page_nr];
  1742. if (!PageUptodate(page) || page->mapping != mapping) {
  1743. page = NULL;
  1744. error = shmem_getpage(inode, index, &page,
  1745. SGP_CACHE, NULL);
  1746. if (error)
  1747. break;
  1748. unlock_page(page);
  1749. page_cache_release(spd.pages[page_nr]);
  1750. spd.pages[page_nr] = page;
  1751. }
  1752. isize = i_size_read(inode);
  1753. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1754. if (unlikely(!isize || index > end_index))
  1755. break;
  1756. if (end_index == index) {
  1757. unsigned int plen;
  1758. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1759. if (plen <= loff)
  1760. break;
  1761. this_len = min(this_len, plen - loff);
  1762. len = this_len;
  1763. }
  1764. spd.partial[page_nr].offset = loff;
  1765. spd.partial[page_nr].len = this_len;
  1766. len -= this_len;
  1767. loff = 0;
  1768. spd.nr_pages++;
  1769. index++;
  1770. }
  1771. while (page_nr < nr_pages)
  1772. page_cache_release(spd.pages[page_nr++]);
  1773. if (spd.nr_pages)
  1774. error = splice_to_pipe(pipe, &spd);
  1775. splice_shrink_spd(pipe, &spd);
  1776. if (error > 0) {
  1777. *ppos += error;
  1778. file_accessed(in);
  1779. }
  1780. return error;
  1781. }
  1782. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1783. {
  1784. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1785. buf->f_type = TMPFS_MAGIC;
  1786. buf->f_bsize = PAGE_CACHE_SIZE;
  1787. buf->f_namelen = NAME_MAX;
  1788. if (sbinfo->max_blocks) {
  1789. buf->f_blocks = sbinfo->max_blocks;
  1790. buf->f_bavail = buf->f_bfree =
  1791. sbinfo->max_blocks - percpu_counter_sum(&sbinfo->used_blocks);
  1792. }
  1793. if (sbinfo->max_inodes) {
  1794. buf->f_files = sbinfo->max_inodes;
  1795. buf->f_ffree = sbinfo->free_inodes;
  1796. }
  1797. /* else leave those fields 0 like simple_statfs */
  1798. return 0;
  1799. }
  1800. /*
  1801. * File creation. Allocate an inode, and we're done..
  1802. */
  1803. static int
  1804. shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1805. {
  1806. struct inode *inode;
  1807. int error = -ENOSPC;
  1808. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
  1809. if (inode) {
  1810. error = security_inode_init_security(inode, dir,
  1811. &dentry->d_name, NULL,
  1812. NULL, NULL);
  1813. if (error) {
  1814. if (error != -EOPNOTSUPP) {
  1815. iput(inode);
  1816. return error;
  1817. }
  1818. }
  1819. #ifdef CONFIG_TMPFS_POSIX_ACL
  1820. error = generic_acl_init(inode, dir);
  1821. if (error) {
  1822. iput(inode);
  1823. return error;
  1824. }
  1825. #else
  1826. error = 0;
  1827. #endif
  1828. dir->i_size += BOGO_DIRENT_SIZE;
  1829. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1830. d_instantiate(dentry, inode);
  1831. dget(dentry); /* Extra count - pin the dentry in core */
  1832. }
  1833. return error;
  1834. }
  1835. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1836. {
  1837. int error;
  1838. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1839. return error;
  1840. inc_nlink(dir);
  1841. return 0;
  1842. }
  1843. static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
  1844. struct nameidata *nd)
  1845. {
  1846. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1847. }
  1848. /*
  1849. * Link a file..
  1850. */
  1851. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1852. {
  1853. struct inode *inode = old_dentry->d_inode;
  1854. int ret;
  1855. /*
  1856. * No ordinary (disk based) filesystem counts links as inodes;
  1857. * but each new link needs a new dentry, pinning lowmem, and
  1858. * tmpfs dentries cannot be pruned until they are unlinked.
  1859. */
  1860. ret = shmem_reserve_inode(inode->i_sb);
  1861. if (ret)
  1862. goto out;
  1863. dir->i_size += BOGO_DIRENT_SIZE;
  1864. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1865. inc_nlink(inode);
  1866. ihold(inode); /* New dentry reference */
  1867. dget(dentry); /* Extra pinning count for the created dentry */
  1868. d_instantiate(dentry, inode);
  1869. out:
  1870. return ret;
  1871. }
  1872. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1873. {
  1874. struct inode *inode = dentry->d_inode;
  1875. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  1876. shmem_free_inode(inode->i_sb);
  1877. dir->i_size -= BOGO_DIRENT_SIZE;
  1878. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1879. drop_nlink(inode);
  1880. dput(dentry); /* Undo the count from "create" - this does all the work */
  1881. return 0;
  1882. }
  1883. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1884. {
  1885. if (!simple_empty(dentry))
  1886. return -ENOTEMPTY;
  1887. drop_nlink(dentry->d_inode);
  1888. drop_nlink(dir);
  1889. return shmem_unlink(dir, dentry);
  1890. }
  1891. /*
  1892. * The VFS layer already does all the dentry stuff for rename,
  1893. * we just have to decrement the usage count for the target if
  1894. * it exists so that the VFS layer correctly free's it when it
  1895. * gets overwritten.
  1896. */
  1897. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1898. {
  1899. struct inode *inode = old_dentry->d_inode;
  1900. int they_are_dirs = S_ISDIR(inode->i_mode);
  1901. if (!simple_empty(new_dentry))
  1902. return -ENOTEMPTY;
  1903. if (new_dentry->d_inode) {
  1904. (void) shmem_unlink(new_dir, new_dentry);
  1905. if (they_are_dirs)
  1906. drop_nlink(old_dir);
  1907. } else if (they_are_dirs) {
  1908. drop_nlink(old_dir);
  1909. inc_nlink(new_dir);
  1910. }
  1911. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1912. new_dir->i_size += BOGO_DIRENT_SIZE;
  1913. old_dir->i_ctime = old_dir->i_mtime =
  1914. new_dir->i_ctime = new_dir->i_mtime =
  1915. inode->i_ctime = CURRENT_TIME;
  1916. return 0;
  1917. }
  1918. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1919. {
  1920. int error;
  1921. int len;
  1922. struct inode *inode;
  1923. struct page *page = NULL;
  1924. char *kaddr;
  1925. struct shmem_inode_info *info;
  1926. len = strlen(symname) + 1;
  1927. if (len > PAGE_CACHE_SIZE)
  1928. return -ENAMETOOLONG;
  1929. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
  1930. if (!inode)
  1931. return -ENOSPC;
  1932. error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
  1933. NULL, NULL);
  1934. if (error) {
  1935. if (error != -EOPNOTSUPP) {
  1936. iput(inode);
  1937. return error;
  1938. }
  1939. error = 0;
  1940. }
  1941. info = SHMEM_I(inode);
  1942. inode->i_size = len-1;
  1943. if (len <= SHMEM_SYMLINK_INLINE_LEN) {
  1944. /* do it inline */
  1945. memcpy(info->inline_symlink, symname, len);
  1946. inode->i_op = &shmem_symlink_inline_operations;
  1947. } else {
  1948. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1949. if (error) {
  1950. iput(inode);
  1951. return error;
  1952. }
  1953. inode->i_mapping->a_ops = &shmem_aops;
  1954. inode->i_op = &shmem_symlink_inode_operations;
  1955. kaddr = kmap_atomic(page, KM_USER0);
  1956. memcpy(kaddr, symname, len);
  1957. kunmap_atomic(kaddr, KM_USER0);
  1958. set_page_dirty(page);
  1959. unlock_page(page);
  1960. page_cache_release(page);
  1961. }
  1962. dir->i_size += BOGO_DIRENT_SIZE;
  1963. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1964. d_instantiate(dentry, inode);
  1965. dget(dentry);
  1966. return 0;
  1967. }
  1968. static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
  1969. {
  1970. nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
  1971. return NULL;
  1972. }
  1973. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1974. {
  1975. struct page *page = NULL;
  1976. int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1977. nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
  1978. if (page)
  1979. unlock_page(page);
  1980. return page;
  1981. }
  1982. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1983. {
  1984. if (!IS_ERR(nd_get_link(nd))) {
  1985. struct page *page = cookie;
  1986. kunmap(page);
  1987. mark_page_accessed(page);
  1988. page_cache_release(page);
  1989. }
  1990. }
  1991. #ifdef CONFIG_TMPFS_XATTR
  1992. /*
  1993. * Superblocks without xattr inode operations may get some security.* xattr
  1994. * support from the LSM "for free". As soon as we have any other xattrs
  1995. * like ACLs, we also need to implement the security.* handlers at
  1996. * filesystem level, though.
  1997. */
  1998. static int shmem_xattr_get(struct dentry *dentry, const char *name,
  1999. void *buffer, size_t size)
  2000. {
  2001. struct shmem_inode_info *info;
  2002. struct shmem_xattr *xattr;
  2003. int ret = -ENODATA;
  2004. info = SHMEM_I(dentry->d_inode);
  2005. spin_lock(&info->lock);
  2006. list_for_each_entry(xattr, &info->xattr_list, list) {
  2007. if (strcmp(name, xattr->name))
  2008. continue;
  2009. ret = xattr->size;
  2010. if (buffer) {
  2011. if (size < xattr->size)
  2012. ret = -ERANGE;
  2013. else
  2014. memcpy(buffer, xattr->value, xattr->size);
  2015. }
  2016. break;
  2017. }
  2018. spin_unlock(&info->lock);
  2019. return ret;
  2020. }
  2021. static int shmem_xattr_set(struct dentry *dentry, const char *name,
  2022. const void *value, size_t size, int flags)
  2023. {
  2024. struct inode *inode = dentry->d_inode;
  2025. struct shmem_inode_info *info = SHMEM_I(inode);
  2026. struct shmem_xattr *xattr;
  2027. struct shmem_xattr *new_xattr = NULL;
  2028. size_t len;
  2029. int err = 0;
  2030. /* value == NULL means remove */
  2031. if (value) {
  2032. /* wrap around? */
  2033. len = sizeof(*new_xattr) + size;
  2034. if (len <= sizeof(*new_xattr))
  2035. return -ENOMEM;
  2036. new_xattr = kmalloc(len, GFP_KERNEL);
  2037. if (!new_xattr)
  2038. return -ENOMEM;
  2039. new_xattr->name = kstrdup(name, GFP_KERNEL);
  2040. if (!new_xattr->name) {
  2041. kfree(new_xattr);
  2042. return -ENOMEM;
  2043. }
  2044. new_xattr->size = size;
  2045. memcpy(new_xattr->value, value, size);
  2046. }
  2047. spin_lock(&info->lock);
  2048. list_for_each_entry(xattr, &info->xattr_list, list) {
  2049. if (!strcmp(name, xattr->name)) {
  2050. if (flags & XATTR_CREATE) {
  2051. xattr = new_xattr;
  2052. err = -EEXIST;
  2053. } else if (new_xattr) {
  2054. list_replace(&xattr->list, &new_xattr->list);
  2055. } else {
  2056. list_del(&xattr->list);
  2057. }
  2058. goto out;
  2059. }
  2060. }
  2061. if (flags & XATTR_REPLACE) {
  2062. xattr = new_xattr;
  2063. err = -ENODATA;
  2064. } else {
  2065. list_add(&new_xattr->list, &info->xattr_list);
  2066. xattr = NULL;
  2067. }
  2068. out:
  2069. spin_unlock(&info->lock);
  2070. if (xattr)
  2071. kfree(xattr->name);
  2072. kfree(xattr);
  2073. return err;
  2074. }
  2075. static const struct xattr_handler *shmem_xattr_handlers[] = {
  2076. #ifdef CONFIG_TMPFS_POSIX_ACL
  2077. &generic_acl_access_handler,
  2078. &generic_acl_default_handler,
  2079. #endif
  2080. NULL
  2081. };
  2082. static int shmem_xattr_validate(const char *name)
  2083. {
  2084. struct { const char *prefix; size_t len; } arr[] = {
  2085. { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
  2086. { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
  2087. };
  2088. int i;
  2089. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  2090. size_t preflen = arr[i].len;
  2091. if (strncmp(name, arr[i].prefix, preflen) == 0) {
  2092. if (!name[preflen])
  2093. return -EINVAL;
  2094. return 0;
  2095. }
  2096. }
  2097. return -EOPNOTSUPP;
  2098. }
  2099. static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
  2100. void *buffer, size_t size)
  2101. {
  2102. int err;
  2103. /*
  2104. * If this is a request for a synthetic attribute in the system.*
  2105. * namespace use the generic infrastructure to resolve a handler
  2106. * for it via sb->s_xattr.
  2107. */
  2108. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2109. return generic_getxattr(dentry, name, buffer, size);
  2110. err = shmem_xattr_validate(name);
  2111. if (err)
  2112. return err;
  2113. return shmem_xattr_get(dentry, name, buffer, size);
  2114. }
  2115. static int shmem_setxattr(struct dentry *dentry, const char *name,
  2116. const void *value, size_t size, int flags)
  2117. {
  2118. int err;
  2119. /*
  2120. * If this is a request for a synthetic attribute in the system.*
  2121. * namespace use the generic infrastructure to resolve a handler
  2122. * for it via sb->s_xattr.
  2123. */
  2124. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2125. return generic_setxattr(dentry, name, value, size, flags);
  2126. err = shmem_xattr_validate(name);
  2127. if (err)
  2128. return err;
  2129. if (size == 0)
  2130. value = ""; /* empty EA, do not remove */
  2131. return shmem_xattr_set(dentry, name, value, size, flags);
  2132. }
  2133. static int shmem_removexattr(struct dentry *dentry, const char *name)
  2134. {
  2135. int err;
  2136. /*
  2137. * If this is a request for a synthetic attribute in the system.*
  2138. * namespace use the generic infrastructure to resolve a handler
  2139. * for it via sb->s_xattr.
  2140. */
  2141. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2142. return generic_removexattr(dentry, name);
  2143. err = shmem_xattr_validate(name);
  2144. if (err)
  2145. return err;
  2146. return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
  2147. }
  2148. static bool xattr_is_trusted(const char *name)
  2149. {
  2150. return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  2151. }
  2152. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  2153. {
  2154. bool trusted = capable(CAP_SYS_ADMIN);
  2155. struct shmem_xattr *xattr;
  2156. struct shmem_inode_info *info;
  2157. size_t used = 0;
  2158. info = SHMEM_I(dentry->d_inode);
  2159. spin_lock(&info->lock);
  2160. list_for_each_entry(xattr, &info->xattr_list, list) {
  2161. size_t len;
  2162. /* skip "trusted." attributes for unprivileged callers */
  2163. if (!trusted && xattr_is_trusted(xattr->name))
  2164. continue;
  2165. len = strlen(xattr->name) + 1;
  2166. used += len;
  2167. if (buffer) {
  2168. if (size < used) {
  2169. used = -ERANGE;
  2170. break;
  2171. }
  2172. memcpy(buffer, xattr->name, len);
  2173. buffer += len;
  2174. }
  2175. }
  2176. spin_unlock(&info->lock);
  2177. return used;
  2178. }
  2179. #endif /* CONFIG_TMPFS_XATTR */
  2180. static const struct inode_operations shmem_symlink_inline_operations = {
  2181. .readlink = generic_readlink,
  2182. .follow_link = shmem_follow_link_inline,
  2183. #ifdef CONFIG_TMPFS_XATTR
  2184. .setxattr = shmem_setxattr,
  2185. .getxattr = shmem_getxattr,
  2186. .listxattr = shmem_listxattr,
  2187. .removexattr = shmem_removexattr,
  2188. #endif
  2189. };
  2190. static const struct inode_operations shmem_symlink_inode_operations = {
  2191. .readlink = generic_readlink,
  2192. .follow_link = shmem_follow_link,
  2193. .put_link = shmem_put_link,
  2194. #ifdef CONFIG_TMPFS_XATTR
  2195. .setxattr = shmem_setxattr,
  2196. .getxattr = shmem_getxattr,
  2197. .listxattr = shmem_listxattr,
  2198. .removexattr = shmem_removexattr,
  2199. #endif
  2200. };
  2201. static struct dentry *shmem_get_parent(struct dentry *child)
  2202. {
  2203. return ERR_PTR(-ESTALE);
  2204. }
  2205. static int shmem_match(struct inode *ino, void *vfh)
  2206. {
  2207. __u32 *fh = vfh;
  2208. __u64 inum = fh[2];
  2209. inum = (inum << 32) | fh[1];
  2210. return ino->i_ino == inum && fh[0] == ino->i_generation;
  2211. }
  2212. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  2213. struct fid *fid, int fh_len, int fh_type)
  2214. {
  2215. struct inode *inode;
  2216. struct dentry *dentry = NULL;
  2217. u64 inum = fid->raw[2];
  2218. inum = (inum << 32) | fid->raw[1];
  2219. if (fh_len < 3)
  2220. return NULL;
  2221. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  2222. shmem_match, fid->raw);
  2223. if (inode) {
  2224. dentry = d_find_alias(inode);
  2225. iput(inode);
  2226. }
  2227. return dentry;
  2228. }
  2229. static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
  2230. int connectable)
  2231. {
  2232. struct inode *inode = dentry->d_inode;
  2233. if (*len < 3) {
  2234. *len = 3;
  2235. return 255;
  2236. }
  2237. if (inode_unhashed(inode)) {
  2238. /* Unfortunately insert_inode_hash is not idempotent,
  2239. * so as we hash inodes here rather than at creation
  2240. * time, we need a lock to ensure we only try
  2241. * to do it once
  2242. */
  2243. static DEFINE_SPINLOCK(lock);
  2244. spin_lock(&lock);
  2245. if (inode_unhashed(inode))
  2246. __insert_inode_hash(inode,
  2247. inode->i_ino + inode->i_generation);
  2248. spin_unlock(&lock);
  2249. }
  2250. fh[0] = inode->i_generation;
  2251. fh[1] = inode->i_ino;
  2252. fh[2] = ((__u64)inode->i_ino) >> 32;
  2253. *len = 3;
  2254. return 1;
  2255. }
  2256. static const struct export_operations shmem_export_ops = {
  2257. .get_parent = shmem_get_parent,
  2258. .encode_fh = shmem_encode_fh,
  2259. .fh_to_dentry = shmem_fh_to_dentry,
  2260. };
  2261. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  2262. bool remount)
  2263. {
  2264. char *this_char, *value, *rest;
  2265. while (options != NULL) {
  2266. this_char = options;
  2267. for (;;) {
  2268. /*
  2269. * NUL-terminate this option: unfortunately,
  2270. * mount options form a comma-separated list,
  2271. * but mpol's nodelist may also contain commas.
  2272. */
  2273. options = strchr(options, ',');
  2274. if (options == NULL)
  2275. break;
  2276. options++;
  2277. if (!isdigit(*options)) {
  2278. options[-1] = '\0';
  2279. break;
  2280. }
  2281. }
  2282. if (!*this_char)
  2283. continue;
  2284. if ((value = strchr(this_char,'=')) != NULL) {
  2285. *value++ = 0;
  2286. } else {
  2287. printk(KERN_ERR
  2288. "tmpfs: No value for mount option '%s'\n",
  2289. this_char);
  2290. return 1;
  2291. }
  2292. if (!strcmp(this_char,"size")) {
  2293. unsigned long long size;
  2294. size = memparse(value,&rest);
  2295. if (*rest == '%') {
  2296. size <<= PAGE_SHIFT;
  2297. size *= totalram_pages;
  2298. do_div(size, 100);
  2299. rest++;
  2300. }
  2301. if (*rest)
  2302. goto bad_val;
  2303. sbinfo->max_blocks =
  2304. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  2305. } else if (!strcmp(this_char,"nr_blocks")) {
  2306. sbinfo->max_blocks = memparse(value, &rest);
  2307. if (*rest)
  2308. goto bad_val;
  2309. } else if (!strcmp(this_char,"nr_inodes")) {
  2310. sbinfo->max_inodes = memparse(value, &rest);
  2311. if (*rest)
  2312. goto bad_val;
  2313. } else if (!strcmp(this_char,"mode")) {
  2314. if (remount)
  2315. continue;
  2316. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  2317. if (*rest)
  2318. goto bad_val;
  2319. } else if (!strcmp(this_char,"uid")) {
  2320. if (remount)
  2321. continue;
  2322. sbinfo->uid = simple_strtoul(value, &rest, 0);
  2323. if (*rest)
  2324. goto bad_val;
  2325. } else if (!strcmp(this_char,"gid")) {
  2326. if (remount)
  2327. continue;
  2328. sbinfo->gid = simple_strtoul(value, &rest, 0);
  2329. if (*rest)
  2330. goto bad_val;
  2331. } else if (!strcmp(this_char,"mpol")) {
  2332. if (mpol_parse_str(value, &sbinfo->mpol, 1))
  2333. goto bad_val;
  2334. } else {
  2335. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  2336. this_char);
  2337. return 1;
  2338. }
  2339. }
  2340. return 0;
  2341. bad_val:
  2342. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  2343. value, this_char);
  2344. return 1;
  2345. }
  2346. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  2347. {
  2348. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2349. struct shmem_sb_info config = *sbinfo;
  2350. unsigned long inodes;
  2351. int error = -EINVAL;
  2352. if (shmem_parse_options(data, &config, true))
  2353. return error;
  2354. spin_lock(&sbinfo->stat_lock);
  2355. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  2356. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  2357. goto out;
  2358. if (config.max_inodes < inodes)
  2359. goto out;
  2360. /*
  2361. * Those tests also disallow limited->unlimited while any are in
  2362. * use, so i_blocks will always be zero when max_blocks is zero;
  2363. * but we must separately disallow unlimited->limited, because
  2364. * in that case we have no record of how much is already in use.
  2365. */
  2366. if (config.max_blocks && !sbinfo->max_blocks)
  2367. goto out;
  2368. if (config.max_inodes && !sbinfo->max_inodes)
  2369. goto out;
  2370. error = 0;
  2371. sbinfo->max_blocks = config.max_blocks;
  2372. sbinfo->max_inodes = config.max_inodes;
  2373. sbinfo->free_inodes = config.max_inodes - inodes;
  2374. mpol_put(sbinfo->mpol);
  2375. sbinfo->mpol = config.mpol; /* transfers initial ref */
  2376. out:
  2377. spin_unlock(&sbinfo->stat_lock);
  2378. return error;
  2379. }
  2380. static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
  2381. {
  2382. struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
  2383. if (sbinfo->max_blocks != shmem_default_max_blocks())
  2384. seq_printf(seq, ",size=%luk",
  2385. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  2386. if (sbinfo->max_inodes != shmem_default_max_inodes())
  2387. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  2388. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  2389. seq_printf(seq, ",mode=%03o", sbinfo->mode);
  2390. if (sbinfo->uid != 0)
  2391. seq_printf(seq, ",uid=%u", sbinfo->uid);
  2392. if (sbinfo->gid != 0)
  2393. seq_printf(seq, ",gid=%u", sbinfo->gid);
  2394. shmem_show_mpol(seq, sbinfo->mpol);
  2395. return 0;
  2396. }
  2397. #endif /* CONFIG_TMPFS */
  2398. static void shmem_put_super(struct super_block *sb)
  2399. {
  2400. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2401. percpu_counter_destroy(&sbinfo->used_blocks);
  2402. kfree(sbinfo);
  2403. sb->s_fs_info = NULL;
  2404. }
  2405. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  2406. {
  2407. struct inode *inode;
  2408. struct dentry *root;
  2409. struct shmem_sb_info *sbinfo;
  2410. int err = -ENOMEM;
  2411. /* Round up to L1_CACHE_BYTES to resist false sharing */
  2412. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  2413. L1_CACHE_BYTES), GFP_KERNEL);
  2414. if (!sbinfo)
  2415. return -ENOMEM;
  2416. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  2417. sbinfo->uid = current_fsuid();
  2418. sbinfo->gid = current_fsgid();
  2419. sb->s_fs_info = sbinfo;
  2420. #ifdef CONFIG_TMPFS
  2421. /*
  2422. * Per default we only allow half of the physical ram per
  2423. * tmpfs instance, limiting inodes to one per page of lowmem;
  2424. * but the internal instance is left unlimited.
  2425. */
  2426. if (!(sb->s_flags & MS_NOUSER)) {
  2427. sbinfo->max_blocks = shmem_default_max_blocks();
  2428. sbinfo->max_inodes = shmem_default_max_inodes();
  2429. if (shmem_parse_options(data, sbinfo, false)) {
  2430. err = -EINVAL;
  2431. goto failed;
  2432. }
  2433. }
  2434. sb->s_export_op = &shmem_export_ops;
  2435. #else
  2436. sb->s_flags |= MS_NOUSER;
  2437. #endif
  2438. spin_lock_init(&sbinfo->stat_lock);
  2439. if (percpu_counter_init(&sbinfo->used_blocks, 0))
  2440. goto failed;
  2441. sbinfo->free_inodes = sbinfo->max_inodes;
  2442. sb->s_maxbytes = SHMEM_MAX_BYTES;
  2443. sb->s_blocksize = PAGE_CACHE_SIZE;
  2444. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  2445. sb->s_magic = TMPFS_MAGIC;
  2446. sb->s_op = &shmem_ops;
  2447. sb->s_time_gran = 1;
  2448. #ifdef CONFIG_TMPFS_XATTR
  2449. sb->s_xattr = shmem_xattr_handlers;
  2450. #endif
  2451. #ifdef CONFIG_TMPFS_POSIX_ACL
  2452. sb->s_flags |= MS_POSIXACL;
  2453. #endif
  2454. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
  2455. if (!inode)
  2456. goto failed;
  2457. inode->i_uid = sbinfo->uid;
  2458. inode->i_gid = sbinfo->gid;
  2459. root = d_alloc_root(inode);
  2460. if (!root)
  2461. goto failed_iput;
  2462. sb->s_root = root;
  2463. return 0;
  2464. failed_iput:
  2465. iput(inode);
  2466. failed:
  2467. shmem_put_super(sb);
  2468. return err;
  2469. }
  2470. static struct kmem_cache *shmem_inode_cachep;
  2471. static struct inode *shmem_alloc_inode(struct super_block *sb)
  2472. {
  2473. struct shmem_inode_info *p;
  2474. p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  2475. if (!p)
  2476. return NULL;
  2477. return &p->vfs_inode;
  2478. }
  2479. static void shmem_i_callback(struct rcu_head *head)
  2480. {
  2481. struct inode *inode = container_of(head, struct inode, i_rcu);
  2482. INIT_LIST_HEAD(&inode->i_dentry);
  2483. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  2484. }
  2485. static void shmem_destroy_inode(struct inode *inode)
  2486. {
  2487. if ((inode->i_mode & S_IFMT) == S_IFREG) {
  2488. /* only struct inode is valid if it's an inline symlink */
  2489. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  2490. }
  2491. call_rcu(&inode->i_rcu, shmem_i_callback);
  2492. }
  2493. static void init_once(void *foo)
  2494. {
  2495. struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
  2496. inode_init_once(&p->vfs_inode);
  2497. }
  2498. static int init_inodecache(void)
  2499. {
  2500. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  2501. sizeof(struct shmem_inode_info),
  2502. 0, SLAB_PANIC, init_once);
  2503. return 0;
  2504. }
  2505. static void destroy_inodecache(void)
  2506. {
  2507. kmem_cache_destroy(shmem_inode_cachep);
  2508. }
  2509. static const struct address_space_operations shmem_aops = {
  2510. .writepage = shmem_writepage,
  2511. .set_page_dirty = __set_page_dirty_no_writeback,
  2512. #ifdef CONFIG_TMPFS
  2513. .readpage = shmem_readpage,
  2514. .write_begin = shmem_write_begin,
  2515. .write_end = shmem_write_end,
  2516. #endif
  2517. .migratepage = migrate_page,
  2518. .error_remove_page = generic_error_remove_page,
  2519. };
  2520. static const struct file_operations shmem_file_operations = {
  2521. .mmap = shmem_mmap,
  2522. #ifdef CONFIG_TMPFS
  2523. .llseek = generic_file_llseek,
  2524. .read = do_sync_read,
  2525. .write = do_sync_write,
  2526. .aio_read = shmem_file_aio_read,
  2527. .aio_write = generic_file_aio_write,
  2528. .fsync = noop_fsync,
  2529. .splice_read = shmem_file_splice_read,
  2530. .splice_write = generic_file_splice_write,
  2531. #endif
  2532. };
  2533. static const struct inode_operations shmem_inode_operations = {
  2534. .setattr = shmem_setattr,
  2535. .truncate_range = shmem_truncate_range,
  2536. #ifdef CONFIG_TMPFS_XATTR
  2537. .setxattr = shmem_setxattr,
  2538. .getxattr = shmem_getxattr,
  2539. .listxattr = shmem_listxattr,
  2540. .removexattr = shmem_removexattr,
  2541. #endif
  2542. #ifdef CONFIG_TMPFS_POSIX_ACL
  2543. .check_acl = generic_check_acl,
  2544. #endif
  2545. };
  2546. static const struct inode_operations shmem_dir_inode_operations = {
  2547. #ifdef CONFIG_TMPFS
  2548. .create = shmem_create,
  2549. .lookup = simple_lookup,
  2550. .link = shmem_link,
  2551. .unlink = shmem_unlink,
  2552. .symlink = shmem_symlink,
  2553. .mkdir = shmem_mkdir,
  2554. .rmdir = shmem_rmdir,
  2555. .mknod = shmem_mknod,
  2556. .rename = shmem_rename,
  2557. #endif
  2558. #ifdef CONFIG_TMPFS_XATTR
  2559. .setxattr = shmem_setxattr,
  2560. .getxattr = shmem_getxattr,
  2561. .listxattr = shmem_listxattr,
  2562. .removexattr = shmem_removexattr,
  2563. #endif
  2564. #ifdef CONFIG_TMPFS_POSIX_ACL
  2565. .setattr = shmem_setattr,
  2566. .check_acl = generic_check_acl,
  2567. #endif
  2568. };
  2569. static const struct inode_operations shmem_special_inode_operations = {
  2570. #ifdef CONFIG_TMPFS_XATTR
  2571. .setxattr = shmem_setxattr,
  2572. .getxattr = shmem_getxattr,
  2573. .listxattr = shmem_listxattr,
  2574. .removexattr = shmem_removexattr,
  2575. #endif
  2576. #ifdef CONFIG_TMPFS_POSIX_ACL
  2577. .setattr = shmem_setattr,
  2578. .check_acl = generic_check_acl,
  2579. #endif
  2580. };
  2581. static const struct super_operations shmem_ops = {
  2582. .alloc_inode = shmem_alloc_inode,
  2583. .destroy_inode = shmem_destroy_inode,
  2584. #ifdef CONFIG_TMPFS
  2585. .statfs = shmem_statfs,
  2586. .remount_fs = shmem_remount_fs,
  2587. .show_options = shmem_show_options,
  2588. #endif
  2589. .evict_inode = shmem_evict_inode,
  2590. .drop_inode = generic_delete_inode,
  2591. .put_super = shmem_put_super,
  2592. };
  2593. static const struct vm_operations_struct shmem_vm_ops = {
  2594. .fault = shmem_fault,
  2595. #ifdef CONFIG_NUMA
  2596. .set_policy = shmem_set_policy,
  2597. .get_policy = shmem_get_policy,
  2598. #endif
  2599. };
  2600. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  2601. int flags, const char *dev_name, void *data)
  2602. {
  2603. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  2604. }
  2605. static struct file_system_type tmpfs_fs_type = {
  2606. .owner = THIS_MODULE,
  2607. .name = "tmpfs",
  2608. .mount = shmem_mount,
  2609. .kill_sb = kill_litter_super,
  2610. };
  2611. int __init init_tmpfs(void)
  2612. {
  2613. int error;
  2614. error = bdi_init(&shmem_backing_dev_info);
  2615. if (error)
  2616. goto out4;
  2617. error = init_inodecache();
  2618. if (error)
  2619. goto out3;
  2620. error = register_filesystem(&tmpfs_fs_type);
  2621. if (error) {
  2622. printk(KERN_ERR "Could not register tmpfs\n");
  2623. goto out2;
  2624. }
  2625. shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
  2626. tmpfs_fs_type.name, NULL);
  2627. if (IS_ERR(shm_mnt)) {
  2628. error = PTR_ERR(shm_mnt);
  2629. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2630. goto out1;
  2631. }
  2632. return 0;
  2633. out1:
  2634. unregister_filesystem(&tmpfs_fs_type);
  2635. out2:
  2636. destroy_inodecache();
  2637. out3:
  2638. bdi_destroy(&shmem_backing_dev_info);
  2639. out4:
  2640. shm_mnt = ERR_PTR(error);
  2641. return error;
  2642. }
  2643. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  2644. /**
  2645. * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
  2646. * @inode: the inode to be searched
  2647. * @pgoff: the offset to be searched
  2648. * @pagep: the pointer for the found page to be stored
  2649. * @ent: the pointer for the found swap entry to be stored
  2650. *
  2651. * If a page is found, refcount of it is incremented. Callers should handle
  2652. * these refcount.
  2653. */
  2654. void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
  2655. struct page **pagep, swp_entry_t *ent)
  2656. {
  2657. swp_entry_t entry = { .val = 0 }, *ptr;
  2658. struct page *page = NULL;
  2659. struct shmem_inode_info *info = SHMEM_I(inode);
  2660. if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  2661. goto out;
  2662. spin_lock(&info->lock);
  2663. ptr = shmem_swp_entry(info, pgoff, NULL);
  2664. #ifdef CONFIG_SWAP
  2665. if (ptr && ptr->val) {
  2666. entry.val = ptr->val;
  2667. page = find_get_page(&swapper_space, entry.val);
  2668. } else
  2669. #endif
  2670. page = find_get_page(inode->i_mapping, pgoff);
  2671. if (ptr)
  2672. shmem_swp_unmap(ptr);
  2673. spin_unlock(&info->lock);
  2674. out:
  2675. *pagep = page;
  2676. *ent = entry;
  2677. }
  2678. #endif
  2679. #else /* !CONFIG_SHMEM */
  2680. /*
  2681. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  2682. *
  2683. * This is intended for small system where the benefits of the full
  2684. * shmem code (swap-backed and resource-limited) are outweighed by
  2685. * their complexity. On systems without swap this code should be
  2686. * effectively equivalent, but much lighter weight.
  2687. */
  2688. #include <linux/ramfs.h>
  2689. static struct file_system_type tmpfs_fs_type = {
  2690. .name = "tmpfs",
  2691. .mount = ramfs_mount,
  2692. .kill_sb = kill_litter_super,
  2693. };
  2694. int __init init_tmpfs(void)
  2695. {
  2696. BUG_ON(register_filesystem(&tmpfs_fs_type) != 0);
  2697. shm_mnt = kern_mount(&tmpfs_fs_type);
  2698. BUG_ON(IS_ERR(shm_mnt));
  2699. return 0;
  2700. }
  2701. int shmem_unuse(swp_entry_t entry, struct page *page)
  2702. {
  2703. return 0;
  2704. }
  2705. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  2706. {
  2707. return 0;
  2708. }
  2709. void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
  2710. {
  2711. truncate_inode_pages_range(inode->i_mapping, start, end);
  2712. }
  2713. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  2714. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  2715. /**
  2716. * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
  2717. * @inode: the inode to be searched
  2718. * @pgoff: the offset to be searched
  2719. * @pagep: the pointer for the found page to be stored
  2720. * @ent: the pointer for the found swap entry to be stored
  2721. *
  2722. * If a page is found, refcount of it is incremented. Callers should handle
  2723. * these refcount.
  2724. */
  2725. void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
  2726. struct page **pagep, swp_entry_t *ent)
  2727. {
  2728. struct page *page = NULL;
  2729. if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  2730. goto out;
  2731. page = find_get_page(inode->i_mapping, pgoff);
  2732. out:
  2733. *pagep = page;
  2734. *ent = (swp_entry_t){ .val = 0 };
  2735. }
  2736. #endif
  2737. #define shmem_vm_ops generic_file_vm_ops
  2738. #define shmem_file_operations ramfs_file_operations
  2739. #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
  2740. #define shmem_acct_size(flags, size) 0
  2741. #define shmem_unacct_size(flags, size) do {} while (0)
  2742. #define SHMEM_MAX_BYTES MAX_LFS_FILESIZE
  2743. #endif /* CONFIG_SHMEM */
  2744. /* common code */
  2745. /**
  2746. * shmem_file_setup - get an unlinked file living in tmpfs
  2747. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2748. * @size: size to be set for the file
  2749. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2750. */
  2751. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  2752. {
  2753. int error;
  2754. struct file *file;
  2755. struct inode *inode;
  2756. struct path path;
  2757. struct dentry *root;
  2758. struct qstr this;
  2759. if (IS_ERR(shm_mnt))
  2760. return (void *)shm_mnt;
  2761. if (size < 0 || size > SHMEM_MAX_BYTES)
  2762. return ERR_PTR(-EINVAL);
  2763. if (shmem_acct_size(flags, size))
  2764. return ERR_PTR(-ENOMEM);
  2765. error = -ENOMEM;
  2766. this.name = name;
  2767. this.len = strlen(name);
  2768. this.hash = 0; /* will go */
  2769. root = shm_mnt->mnt_root;
  2770. path.dentry = d_alloc(root, &this);
  2771. if (!path.dentry)
  2772. goto put_memory;
  2773. path.mnt = mntget(shm_mnt);
  2774. error = -ENOSPC;
  2775. inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
  2776. if (!inode)
  2777. goto put_dentry;
  2778. d_instantiate(path.dentry, inode);
  2779. inode->i_size = size;
  2780. inode->i_nlink = 0; /* It is unlinked */
  2781. #ifndef CONFIG_MMU
  2782. error = ramfs_nommu_expand_for_mapping(inode, size);
  2783. if (error)
  2784. goto put_dentry;
  2785. #endif
  2786. error = -ENFILE;
  2787. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  2788. &shmem_file_operations);
  2789. if (!file)
  2790. goto put_dentry;
  2791. return file;
  2792. put_dentry:
  2793. path_put(&path);
  2794. put_memory:
  2795. shmem_unacct_size(flags, size);
  2796. return ERR_PTR(error);
  2797. }
  2798. EXPORT_SYMBOL_GPL(shmem_file_setup);
  2799. /**
  2800. * shmem_zero_setup - setup a shared anonymous mapping
  2801. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2802. */
  2803. int shmem_zero_setup(struct vm_area_struct *vma)
  2804. {
  2805. struct file *file;
  2806. loff_t size = vma->vm_end - vma->vm_start;
  2807. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2808. if (IS_ERR(file))
  2809. return PTR_ERR(file);
  2810. if (vma->vm_file)
  2811. fput(vma->vm_file);
  2812. vma->vm_file = file;
  2813. vma->vm_ops = &shmem_vm_ops;
  2814. vma->vm_flags |= VM_CAN_NONLINEAR;
  2815. return 0;
  2816. }
  2817. /**
  2818. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  2819. * @mapping: the page's address_space
  2820. * @index: the page index
  2821. * @gfp: the page allocator flags to use if allocating
  2822. *
  2823. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  2824. * with any new page allocations done using the specified allocation flags.
  2825. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  2826. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  2827. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  2828. *
  2829. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  2830. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  2831. */
  2832. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  2833. pgoff_t index, gfp_t gfp)
  2834. {
  2835. #ifdef CONFIG_SHMEM
  2836. struct inode *inode = mapping->host;
  2837. struct page *page = NULL;
  2838. int error;
  2839. BUG_ON(mapping->a_ops != &shmem_aops);
  2840. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
  2841. if (error)
  2842. page = ERR_PTR(error);
  2843. else
  2844. unlock_page(page);
  2845. return page;
  2846. #else
  2847. /*
  2848. * The tiny !SHMEM case uses ramfs without swap
  2849. */
  2850. return read_cache_page_gfp(mapping, index, gfp);
  2851. #endif
  2852. }
  2853. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);