shmem.c 77 KB

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