shmem.c 78 KB

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