shmem.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  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. * This file is released under the GPL.
  18. */
  19. /*
  20. * This virtual memory filesystem is heavily based on the ramfs. It
  21. * extends ramfs by the ability to use swap and honor resource limits
  22. * which makes it a completely usable filesystem.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/fs.h>
  27. #include <linux/xattr.h>
  28. #include <linux/exportfs.h>
  29. #include <linux/generic_acl.h>
  30. #include <linux/mm.h>
  31. #include <linux/mman.h>
  32. #include <linux/file.h>
  33. #include <linux/swap.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/string.h>
  36. #include <linux/slab.h>
  37. #include <linux/backing-dev.h>
  38. #include <linux/shmem_fs.h>
  39. #include <linux/mount.h>
  40. #include <linux/writeback.h>
  41. #include <linux/vfs.h>
  42. #include <linux/blkdev.h>
  43. #include <linux/security.h>
  44. #include <linux/swapops.h>
  45. #include <linux/mempolicy.h>
  46. #include <linux/namei.h>
  47. #include <linux/ctype.h>
  48. #include <linux/migrate.h>
  49. #include <linux/highmem.h>
  50. #include <asm/uaccess.h>
  51. #include <asm/div64.h>
  52. #include <asm/pgtable.h>
  53. /* This magic number is used in glibc for posix shared memory */
  54. #define TMPFS_MAGIC 0x01021994
  55. #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
  56. #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
  57. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  58. #define SHMEM_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
  59. #define SHMEM_MAX_BYTES ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
  60. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  61. /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
  62. #define SHMEM_PAGEIN VM_READ
  63. #define SHMEM_TRUNCATE VM_WRITE
  64. /* Definition to limit shmem_truncate's steps between cond_rescheds */
  65. #define LATENCY_LIMIT 64
  66. /* Pretend that each entry is of this size in directory's i_size */
  67. #define BOGO_DIRENT_SIZE 20
  68. /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
  69. enum sgp_type {
  70. SGP_READ, /* don't exceed i_size, don't allocate page */
  71. SGP_CACHE, /* don't exceed i_size, may allocate page */
  72. SGP_WRITE, /* may exceed i_size, may allocate page */
  73. };
  74. static int shmem_getpage(struct inode *inode, unsigned long idx,
  75. struct page **pagep, enum sgp_type sgp, int *type);
  76. static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
  77. {
  78. /*
  79. * The above definition of ENTRIES_PER_PAGE, and the use of
  80. * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
  81. * might be reconsidered if it ever diverges from PAGE_SIZE.
  82. *
  83. * Mobility flags are masked out as swap vectors cannot move
  84. */
  85. return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
  86. PAGE_CACHE_SHIFT-PAGE_SHIFT);
  87. }
  88. static inline void shmem_dir_free(struct page *page)
  89. {
  90. __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
  91. }
  92. static struct page **shmem_dir_map(struct page *page)
  93. {
  94. return (struct page **)kmap_atomic(page, KM_USER0);
  95. }
  96. static inline void shmem_dir_unmap(struct page **dir)
  97. {
  98. kunmap_atomic(dir, KM_USER0);
  99. }
  100. static swp_entry_t *shmem_swp_map(struct page *page)
  101. {
  102. return (swp_entry_t *)kmap_atomic(page, KM_USER1);
  103. }
  104. static inline void shmem_swp_balance_unmap(void)
  105. {
  106. /*
  107. * When passing a pointer to an i_direct entry, to code which
  108. * also handles indirect entries and so will shmem_swp_unmap,
  109. * we must arrange for the preempt count to remain in balance.
  110. * What kmap_atomic of a lowmem page does depends on config
  111. * and architecture, so pretend to kmap_atomic some lowmem page.
  112. */
  113. (void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
  114. }
  115. static inline void shmem_swp_unmap(swp_entry_t *entry)
  116. {
  117. kunmap_atomic(entry, KM_USER1);
  118. }
  119. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  120. {
  121. return sb->s_fs_info;
  122. }
  123. /*
  124. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  125. * for shared memory and for shared anonymous (/dev/zero) mappings
  126. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  127. * consistent with the pre-accounting of private mappings ...
  128. */
  129. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  130. {
  131. return (flags & VM_ACCOUNT)?
  132. security_vm_enough_memory(VM_ACCT(size)): 0;
  133. }
  134. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  135. {
  136. if (flags & VM_ACCOUNT)
  137. vm_unacct_memory(VM_ACCT(size));
  138. }
  139. /*
  140. * ... whereas tmpfs objects are accounted incrementally as
  141. * pages are allocated, in order to allow huge sparse files.
  142. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  143. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  144. */
  145. static inline int shmem_acct_block(unsigned long flags)
  146. {
  147. return (flags & VM_ACCOUNT)?
  148. 0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
  149. }
  150. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  151. {
  152. if (!(flags & VM_ACCOUNT))
  153. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  154. }
  155. static const struct super_operations shmem_ops;
  156. static const struct address_space_operations shmem_aops;
  157. static const struct file_operations shmem_file_operations;
  158. static const struct inode_operations shmem_inode_operations;
  159. static const struct inode_operations shmem_dir_inode_operations;
  160. static const struct inode_operations shmem_special_inode_operations;
  161. static struct vm_operations_struct shmem_vm_ops;
  162. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  163. .ra_pages = 0, /* No readahead */
  164. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  165. .unplug_io_fn = default_unplug_io_fn,
  166. };
  167. static LIST_HEAD(shmem_swaplist);
  168. static DEFINE_SPINLOCK(shmem_swaplist_lock);
  169. static void shmem_free_blocks(struct inode *inode, long pages)
  170. {
  171. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  172. if (sbinfo->max_blocks) {
  173. spin_lock(&sbinfo->stat_lock);
  174. sbinfo->free_blocks += pages;
  175. inode->i_blocks -= pages*BLOCKS_PER_PAGE;
  176. spin_unlock(&sbinfo->stat_lock);
  177. }
  178. }
  179. static int shmem_reserve_inode(struct super_block *sb)
  180. {
  181. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  182. if (sbinfo->max_inodes) {
  183. spin_lock(&sbinfo->stat_lock);
  184. if (!sbinfo->free_inodes) {
  185. spin_unlock(&sbinfo->stat_lock);
  186. return -ENOSPC;
  187. }
  188. sbinfo->free_inodes--;
  189. spin_unlock(&sbinfo->stat_lock);
  190. }
  191. return 0;
  192. }
  193. static void shmem_free_inode(struct super_block *sb)
  194. {
  195. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  196. if (sbinfo->max_inodes) {
  197. spin_lock(&sbinfo->stat_lock);
  198. sbinfo->free_inodes++;
  199. spin_unlock(&sbinfo->stat_lock);
  200. }
  201. }
  202. /*
  203. * shmem_recalc_inode - recalculate the size of an inode
  204. *
  205. * @inode: inode to recalc
  206. *
  207. * We have to calculate the free blocks since the mm can drop
  208. * undirtied hole pages behind our back.
  209. *
  210. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  211. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  212. *
  213. * It has to be called with the spinlock held.
  214. */
  215. static void shmem_recalc_inode(struct inode *inode)
  216. {
  217. struct shmem_inode_info *info = SHMEM_I(inode);
  218. long freed;
  219. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  220. if (freed > 0) {
  221. info->alloced -= freed;
  222. shmem_unacct_blocks(info->flags, freed);
  223. shmem_free_blocks(inode, freed);
  224. }
  225. }
  226. /*
  227. * shmem_swp_entry - find the swap vector position in the info structure
  228. *
  229. * @info: info structure for the inode
  230. * @index: index of the page to find
  231. * @page: optional page to add to the structure. Has to be preset to
  232. * all zeros
  233. *
  234. * If there is no space allocated yet it will return NULL when
  235. * page is NULL, else it will use the page for the needed block,
  236. * setting it to NULL on return to indicate that it has been used.
  237. *
  238. * The swap vector is organized the following way:
  239. *
  240. * There are SHMEM_NR_DIRECT entries directly stored in the
  241. * shmem_inode_info structure. So small files do not need an addional
  242. * allocation.
  243. *
  244. * For pages with index > SHMEM_NR_DIRECT there is the pointer
  245. * i_indirect which points to a page which holds in the first half
  246. * doubly indirect blocks, in the second half triple indirect blocks:
  247. *
  248. * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
  249. * following layout (for SHMEM_NR_DIRECT == 16):
  250. *
  251. * i_indirect -> dir --> 16-19
  252. * | +-> 20-23
  253. * |
  254. * +-->dir2 --> 24-27
  255. * | +-> 28-31
  256. * | +-> 32-35
  257. * | +-> 36-39
  258. * |
  259. * +-->dir3 --> 40-43
  260. * +-> 44-47
  261. * +-> 48-51
  262. * +-> 52-55
  263. */
  264. static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
  265. {
  266. unsigned long offset;
  267. struct page **dir;
  268. struct page *subdir;
  269. if (index < SHMEM_NR_DIRECT) {
  270. shmem_swp_balance_unmap();
  271. return info->i_direct+index;
  272. }
  273. if (!info->i_indirect) {
  274. if (page) {
  275. info->i_indirect = *page;
  276. *page = NULL;
  277. }
  278. return NULL; /* need another page */
  279. }
  280. index -= SHMEM_NR_DIRECT;
  281. offset = index % ENTRIES_PER_PAGE;
  282. index /= ENTRIES_PER_PAGE;
  283. dir = shmem_dir_map(info->i_indirect);
  284. if (index >= ENTRIES_PER_PAGE/2) {
  285. index -= ENTRIES_PER_PAGE/2;
  286. dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
  287. index %= ENTRIES_PER_PAGE;
  288. subdir = *dir;
  289. if (!subdir) {
  290. if (page) {
  291. *dir = *page;
  292. *page = NULL;
  293. }
  294. shmem_dir_unmap(dir);
  295. return NULL; /* need another page */
  296. }
  297. shmem_dir_unmap(dir);
  298. dir = shmem_dir_map(subdir);
  299. }
  300. dir += index;
  301. subdir = *dir;
  302. if (!subdir) {
  303. if (!page || !(subdir = *page)) {
  304. shmem_dir_unmap(dir);
  305. return NULL; /* need a page */
  306. }
  307. *dir = subdir;
  308. *page = NULL;
  309. }
  310. shmem_dir_unmap(dir);
  311. return shmem_swp_map(subdir) + offset;
  312. }
  313. static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
  314. {
  315. long incdec = value? 1: -1;
  316. entry->val = value;
  317. info->swapped += incdec;
  318. if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
  319. struct page *page = kmap_atomic_to_page(entry);
  320. set_page_private(page, page_private(page) + incdec);
  321. }
  322. }
  323. /*
  324. * shmem_swp_alloc - get the position of the swap entry for the page.
  325. * If it does not exist allocate the entry.
  326. *
  327. * @info: info structure for the inode
  328. * @index: index of the page to find
  329. * @sgp: check and recheck i_size? skip allocation?
  330. */
  331. static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
  332. {
  333. struct inode *inode = &info->vfs_inode;
  334. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  335. struct page *page = NULL;
  336. swp_entry_t *entry;
  337. if (sgp != SGP_WRITE &&
  338. ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  339. return ERR_PTR(-EINVAL);
  340. while (!(entry = shmem_swp_entry(info, index, &page))) {
  341. if (sgp == SGP_READ)
  342. return shmem_swp_map(ZERO_PAGE(0));
  343. /*
  344. * Test free_blocks against 1 not 0, since we have 1 data
  345. * page (and perhaps indirect index pages) yet to allocate:
  346. * a waste to allocate index if we cannot allocate data.
  347. */
  348. if (sbinfo->max_blocks) {
  349. spin_lock(&sbinfo->stat_lock);
  350. if (sbinfo->free_blocks <= 1) {
  351. spin_unlock(&sbinfo->stat_lock);
  352. return ERR_PTR(-ENOSPC);
  353. }
  354. sbinfo->free_blocks--;
  355. inode->i_blocks += BLOCKS_PER_PAGE;
  356. spin_unlock(&sbinfo->stat_lock);
  357. }
  358. spin_unlock(&info->lock);
  359. page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping));
  360. if (page)
  361. set_page_private(page, 0);
  362. spin_lock(&info->lock);
  363. if (!page) {
  364. shmem_free_blocks(inode, 1);
  365. return ERR_PTR(-ENOMEM);
  366. }
  367. if (sgp != SGP_WRITE &&
  368. ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  369. entry = ERR_PTR(-EINVAL);
  370. break;
  371. }
  372. if (info->next_index <= index)
  373. info->next_index = index + 1;
  374. }
  375. if (page) {
  376. /* another task gave its page, or truncated the file */
  377. shmem_free_blocks(inode, 1);
  378. shmem_dir_free(page);
  379. }
  380. if (info->next_index <= index && !IS_ERR(entry))
  381. info->next_index = index + 1;
  382. return entry;
  383. }
  384. /*
  385. * shmem_free_swp - free some swap entries in a directory
  386. *
  387. * @dir: pointer to the directory
  388. * @edir: pointer after last entry of the directory
  389. * @punch_lock: pointer to spinlock when needed for the holepunch case
  390. */
  391. static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
  392. spinlock_t *punch_lock)
  393. {
  394. spinlock_t *punch_unlock = NULL;
  395. swp_entry_t *ptr;
  396. int freed = 0;
  397. for (ptr = dir; ptr < edir; ptr++) {
  398. if (ptr->val) {
  399. if (unlikely(punch_lock)) {
  400. punch_unlock = punch_lock;
  401. punch_lock = NULL;
  402. spin_lock(punch_unlock);
  403. if (!ptr->val)
  404. continue;
  405. }
  406. free_swap_and_cache(*ptr);
  407. *ptr = (swp_entry_t){0};
  408. freed++;
  409. }
  410. }
  411. if (punch_unlock)
  412. spin_unlock(punch_unlock);
  413. return freed;
  414. }
  415. static int shmem_map_and_free_swp(struct page *subdir, int offset,
  416. int limit, struct page ***dir, spinlock_t *punch_lock)
  417. {
  418. swp_entry_t *ptr;
  419. int freed = 0;
  420. ptr = shmem_swp_map(subdir);
  421. for (; offset < limit; offset += LATENCY_LIMIT) {
  422. int size = limit - offset;
  423. if (size > LATENCY_LIMIT)
  424. size = LATENCY_LIMIT;
  425. freed += shmem_free_swp(ptr+offset, ptr+offset+size,
  426. punch_lock);
  427. if (need_resched()) {
  428. shmem_swp_unmap(ptr);
  429. if (*dir) {
  430. shmem_dir_unmap(*dir);
  431. *dir = NULL;
  432. }
  433. cond_resched();
  434. ptr = shmem_swp_map(subdir);
  435. }
  436. }
  437. shmem_swp_unmap(ptr);
  438. return freed;
  439. }
  440. static void shmem_free_pages(struct list_head *next)
  441. {
  442. struct page *page;
  443. int freed = 0;
  444. do {
  445. page = container_of(next, struct page, lru);
  446. next = next->next;
  447. shmem_dir_free(page);
  448. freed++;
  449. if (freed >= LATENCY_LIMIT) {
  450. cond_resched();
  451. freed = 0;
  452. }
  453. } while (next);
  454. }
  455. static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
  456. {
  457. struct shmem_inode_info *info = SHMEM_I(inode);
  458. unsigned long idx;
  459. unsigned long size;
  460. unsigned long limit;
  461. unsigned long stage;
  462. unsigned long diroff;
  463. struct page **dir;
  464. struct page *topdir;
  465. struct page *middir;
  466. struct page *subdir;
  467. swp_entry_t *ptr;
  468. LIST_HEAD(pages_to_free);
  469. long nr_pages_to_free = 0;
  470. long nr_swaps_freed = 0;
  471. int offset;
  472. int freed;
  473. int punch_hole;
  474. spinlock_t *needs_lock;
  475. spinlock_t *punch_lock;
  476. unsigned long upper_limit;
  477. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  478. idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  479. if (idx >= info->next_index)
  480. return;
  481. spin_lock(&info->lock);
  482. info->flags |= SHMEM_TRUNCATE;
  483. if (likely(end == (loff_t) -1)) {
  484. limit = info->next_index;
  485. upper_limit = SHMEM_MAX_INDEX;
  486. info->next_index = idx;
  487. needs_lock = NULL;
  488. punch_hole = 0;
  489. } else {
  490. if (end + 1 >= inode->i_size) { /* we may free a little more */
  491. limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
  492. PAGE_CACHE_SHIFT;
  493. upper_limit = SHMEM_MAX_INDEX;
  494. } else {
  495. limit = (end + 1) >> PAGE_CACHE_SHIFT;
  496. upper_limit = limit;
  497. }
  498. needs_lock = &info->lock;
  499. punch_hole = 1;
  500. }
  501. topdir = info->i_indirect;
  502. if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
  503. info->i_indirect = NULL;
  504. nr_pages_to_free++;
  505. list_add(&topdir->lru, &pages_to_free);
  506. }
  507. spin_unlock(&info->lock);
  508. if (info->swapped && idx < SHMEM_NR_DIRECT) {
  509. ptr = info->i_direct;
  510. size = limit;
  511. if (size > SHMEM_NR_DIRECT)
  512. size = SHMEM_NR_DIRECT;
  513. nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
  514. }
  515. /*
  516. * If there are no indirect blocks or we are punching a hole
  517. * below indirect blocks, nothing to be done.
  518. */
  519. if (!topdir || limit <= SHMEM_NR_DIRECT)
  520. goto done2;
  521. /*
  522. * The truncation case has already dropped info->lock, and we're safe
  523. * because i_size and next_index have already been lowered, preventing
  524. * access beyond. But in the punch_hole case, we still need to take
  525. * the lock when updating the swap directory, because there might be
  526. * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
  527. * shmem_writepage. However, whenever we find we can remove a whole
  528. * directory page (not at the misaligned start or end of the range),
  529. * we first NULLify its pointer in the level above, and then have no
  530. * need to take the lock when updating its contents: needs_lock and
  531. * punch_lock (either pointing to info->lock or NULL) manage this.
  532. */
  533. upper_limit -= SHMEM_NR_DIRECT;
  534. limit -= SHMEM_NR_DIRECT;
  535. idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
  536. offset = idx % ENTRIES_PER_PAGE;
  537. idx -= offset;
  538. dir = shmem_dir_map(topdir);
  539. stage = ENTRIES_PER_PAGEPAGE/2;
  540. if (idx < ENTRIES_PER_PAGEPAGE/2) {
  541. middir = topdir;
  542. diroff = idx/ENTRIES_PER_PAGE;
  543. } else {
  544. dir += ENTRIES_PER_PAGE/2;
  545. dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
  546. while (stage <= idx)
  547. stage += ENTRIES_PER_PAGEPAGE;
  548. middir = *dir;
  549. if (*dir) {
  550. diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
  551. ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
  552. if (!diroff && !offset && upper_limit >= stage) {
  553. if (needs_lock) {
  554. spin_lock(needs_lock);
  555. *dir = NULL;
  556. spin_unlock(needs_lock);
  557. needs_lock = NULL;
  558. } else
  559. *dir = NULL;
  560. nr_pages_to_free++;
  561. list_add(&middir->lru, &pages_to_free);
  562. }
  563. shmem_dir_unmap(dir);
  564. dir = shmem_dir_map(middir);
  565. } else {
  566. diroff = 0;
  567. offset = 0;
  568. idx = stage;
  569. }
  570. }
  571. for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
  572. if (unlikely(idx == stage)) {
  573. shmem_dir_unmap(dir);
  574. dir = shmem_dir_map(topdir) +
  575. ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
  576. while (!*dir) {
  577. dir++;
  578. idx += ENTRIES_PER_PAGEPAGE;
  579. if (idx >= limit)
  580. goto done1;
  581. }
  582. stage = idx + ENTRIES_PER_PAGEPAGE;
  583. middir = *dir;
  584. if (punch_hole)
  585. needs_lock = &info->lock;
  586. if (upper_limit >= stage) {
  587. if (needs_lock) {
  588. spin_lock(needs_lock);
  589. *dir = NULL;
  590. spin_unlock(needs_lock);
  591. needs_lock = NULL;
  592. } else
  593. *dir = NULL;
  594. nr_pages_to_free++;
  595. list_add(&middir->lru, &pages_to_free);
  596. }
  597. shmem_dir_unmap(dir);
  598. cond_resched();
  599. dir = shmem_dir_map(middir);
  600. diroff = 0;
  601. }
  602. punch_lock = needs_lock;
  603. subdir = dir[diroff];
  604. if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
  605. if (needs_lock) {
  606. spin_lock(needs_lock);
  607. dir[diroff] = NULL;
  608. spin_unlock(needs_lock);
  609. punch_lock = NULL;
  610. } else
  611. dir[diroff] = NULL;
  612. nr_pages_to_free++;
  613. list_add(&subdir->lru, &pages_to_free);
  614. }
  615. if (subdir && page_private(subdir) /* has swap entries */) {
  616. size = limit - idx;
  617. if (size > ENTRIES_PER_PAGE)
  618. size = ENTRIES_PER_PAGE;
  619. freed = shmem_map_and_free_swp(subdir,
  620. offset, size, &dir, punch_lock);
  621. if (!dir)
  622. dir = shmem_dir_map(middir);
  623. nr_swaps_freed += freed;
  624. if (offset || punch_lock) {
  625. spin_lock(&info->lock);
  626. set_page_private(subdir,
  627. page_private(subdir) - freed);
  628. spin_unlock(&info->lock);
  629. } else
  630. BUG_ON(page_private(subdir) != freed);
  631. }
  632. offset = 0;
  633. }
  634. done1:
  635. shmem_dir_unmap(dir);
  636. done2:
  637. if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
  638. /*
  639. * Call truncate_inode_pages again: racing shmem_unuse_inode
  640. * may have swizzled a page in from swap since vmtruncate or
  641. * generic_delete_inode did it, before we lowered next_index.
  642. * Also, though shmem_getpage checks i_size before adding to
  643. * cache, no recheck after: so fix the narrow window there too.
  644. *
  645. * Recalling truncate_inode_pages_range and unmap_mapping_range
  646. * every time for punch_hole (which never got a chance to clear
  647. * SHMEM_PAGEIN at the start of vmtruncate_range) is expensive,
  648. * yet hardly ever necessary: try to optimize them out later.
  649. */
  650. truncate_inode_pages_range(inode->i_mapping, start, end);
  651. if (punch_hole)
  652. unmap_mapping_range(inode->i_mapping, start,
  653. end - start, 1);
  654. }
  655. spin_lock(&info->lock);
  656. info->flags &= ~SHMEM_TRUNCATE;
  657. info->swapped -= nr_swaps_freed;
  658. if (nr_pages_to_free)
  659. shmem_free_blocks(inode, nr_pages_to_free);
  660. shmem_recalc_inode(inode);
  661. spin_unlock(&info->lock);
  662. /*
  663. * Empty swap vector directory pages to be freed?
  664. */
  665. if (!list_empty(&pages_to_free)) {
  666. pages_to_free.prev->next = NULL;
  667. shmem_free_pages(pages_to_free.next);
  668. }
  669. }
  670. static void shmem_truncate(struct inode *inode)
  671. {
  672. shmem_truncate_range(inode, inode->i_size, (loff_t)-1);
  673. }
  674. static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
  675. {
  676. struct inode *inode = dentry->d_inode;
  677. struct page *page = NULL;
  678. int error;
  679. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  680. if (attr->ia_size < inode->i_size) {
  681. /*
  682. * If truncating down to a partial page, then
  683. * if that page is already allocated, hold it
  684. * in memory until the truncation is over, so
  685. * truncate_partial_page cannnot miss it were
  686. * it assigned to swap.
  687. */
  688. if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
  689. (void) shmem_getpage(inode,
  690. attr->ia_size>>PAGE_CACHE_SHIFT,
  691. &page, SGP_READ, NULL);
  692. if (page)
  693. unlock_page(page);
  694. }
  695. /*
  696. * Reset SHMEM_PAGEIN flag so that shmem_truncate can
  697. * detect if any pages might have been added to cache
  698. * after truncate_inode_pages. But we needn't bother
  699. * if it's being fully truncated to zero-length: the
  700. * nrpages check is efficient enough in that case.
  701. */
  702. if (attr->ia_size) {
  703. struct shmem_inode_info *info = SHMEM_I(inode);
  704. spin_lock(&info->lock);
  705. info->flags &= ~SHMEM_PAGEIN;
  706. spin_unlock(&info->lock);
  707. }
  708. }
  709. }
  710. error = inode_change_ok(inode, attr);
  711. if (!error)
  712. error = inode_setattr(inode, attr);
  713. #ifdef CONFIG_TMPFS_POSIX_ACL
  714. if (!error && (attr->ia_valid & ATTR_MODE))
  715. error = generic_acl_chmod(inode, &shmem_acl_ops);
  716. #endif
  717. if (page)
  718. page_cache_release(page);
  719. return error;
  720. }
  721. static void shmem_delete_inode(struct inode *inode)
  722. {
  723. struct shmem_inode_info *info = SHMEM_I(inode);
  724. if (inode->i_op->truncate == shmem_truncate) {
  725. truncate_inode_pages(inode->i_mapping, 0);
  726. shmem_unacct_size(info->flags, inode->i_size);
  727. inode->i_size = 0;
  728. shmem_truncate(inode);
  729. if (!list_empty(&info->swaplist)) {
  730. spin_lock(&shmem_swaplist_lock);
  731. list_del_init(&info->swaplist);
  732. spin_unlock(&shmem_swaplist_lock);
  733. }
  734. }
  735. BUG_ON(inode->i_blocks);
  736. shmem_free_inode(inode->i_sb);
  737. clear_inode(inode);
  738. }
  739. static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
  740. {
  741. swp_entry_t *ptr;
  742. for (ptr = dir; ptr < edir; ptr++) {
  743. if (ptr->val == entry.val)
  744. return ptr - dir;
  745. }
  746. return -1;
  747. }
  748. static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
  749. {
  750. struct inode *inode;
  751. unsigned long idx;
  752. unsigned long size;
  753. unsigned long limit;
  754. unsigned long stage;
  755. struct page **dir;
  756. struct page *subdir;
  757. swp_entry_t *ptr;
  758. int offset;
  759. idx = 0;
  760. ptr = info->i_direct;
  761. spin_lock(&info->lock);
  762. limit = info->next_index;
  763. size = limit;
  764. if (size > SHMEM_NR_DIRECT)
  765. size = SHMEM_NR_DIRECT;
  766. offset = shmem_find_swp(entry, ptr, ptr+size);
  767. if (offset >= 0) {
  768. shmem_swp_balance_unmap();
  769. goto found;
  770. }
  771. if (!info->i_indirect)
  772. goto lost2;
  773. dir = shmem_dir_map(info->i_indirect);
  774. stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
  775. for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
  776. if (unlikely(idx == stage)) {
  777. shmem_dir_unmap(dir-1);
  778. dir = shmem_dir_map(info->i_indirect) +
  779. ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
  780. while (!*dir) {
  781. dir++;
  782. idx += ENTRIES_PER_PAGEPAGE;
  783. if (idx >= limit)
  784. goto lost1;
  785. }
  786. stage = idx + ENTRIES_PER_PAGEPAGE;
  787. subdir = *dir;
  788. shmem_dir_unmap(dir);
  789. dir = shmem_dir_map(subdir);
  790. }
  791. subdir = *dir;
  792. if (subdir && page_private(subdir)) {
  793. ptr = shmem_swp_map(subdir);
  794. size = limit - idx;
  795. if (size > ENTRIES_PER_PAGE)
  796. size = ENTRIES_PER_PAGE;
  797. offset = shmem_find_swp(entry, ptr, ptr+size);
  798. if (offset >= 0) {
  799. shmem_dir_unmap(dir);
  800. goto found;
  801. }
  802. shmem_swp_unmap(ptr);
  803. }
  804. }
  805. lost1:
  806. shmem_dir_unmap(dir-1);
  807. lost2:
  808. spin_unlock(&info->lock);
  809. return 0;
  810. found:
  811. idx += offset;
  812. inode = &info->vfs_inode;
  813. if (move_from_swap_cache(page, idx, inode->i_mapping) == 0) {
  814. info->flags |= SHMEM_PAGEIN;
  815. shmem_swp_set(info, ptr + offset, 0);
  816. }
  817. shmem_swp_unmap(ptr);
  818. spin_unlock(&info->lock);
  819. /*
  820. * Decrement swap count even when the entry is left behind:
  821. * try_to_unuse will skip over mms, then reincrement count.
  822. */
  823. swap_free(entry);
  824. return 1;
  825. }
  826. /*
  827. * shmem_unuse() search for an eventually swapped out shmem page.
  828. */
  829. int shmem_unuse(swp_entry_t entry, struct page *page)
  830. {
  831. struct list_head *p, *next;
  832. struct shmem_inode_info *info;
  833. int found = 0;
  834. spin_lock(&shmem_swaplist_lock);
  835. list_for_each_safe(p, next, &shmem_swaplist) {
  836. info = list_entry(p, struct shmem_inode_info, swaplist);
  837. if (!info->swapped)
  838. list_del_init(&info->swaplist);
  839. else if (shmem_unuse_inode(info, entry, page)) {
  840. /* move head to start search for next from here */
  841. list_move_tail(&shmem_swaplist, &info->swaplist);
  842. found = 1;
  843. break;
  844. }
  845. }
  846. spin_unlock(&shmem_swaplist_lock);
  847. return found;
  848. }
  849. /*
  850. * Move the page from the page cache to the swap cache.
  851. */
  852. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  853. {
  854. struct shmem_inode_info *info;
  855. swp_entry_t *entry, swap;
  856. struct address_space *mapping;
  857. unsigned long index;
  858. struct inode *inode;
  859. BUG_ON(!PageLocked(page));
  860. /*
  861. * shmem_backing_dev_info's capabilities prevent regular writeback or
  862. * sync from ever calling shmem_writepage; but a stacking filesystem
  863. * may use the ->writepage of its underlying filesystem, in which case
  864. * we want to do nothing when that underlying filesystem is tmpfs
  865. * (writing out to swap is useful as a response to memory pressure, but
  866. * of no use to stabilize the data) - just redirty the page, unlock it
  867. * and claim success in this case. AOP_WRITEPAGE_ACTIVATE, and the
  868. * page_mapped check below, must be avoided unless we're in reclaim.
  869. */
  870. if (!wbc->for_reclaim) {
  871. set_page_dirty(page);
  872. unlock_page(page);
  873. return 0;
  874. }
  875. BUG_ON(page_mapped(page));
  876. mapping = page->mapping;
  877. index = page->index;
  878. inode = mapping->host;
  879. info = SHMEM_I(inode);
  880. if (info->flags & VM_LOCKED)
  881. goto redirty;
  882. swap = get_swap_page();
  883. if (!swap.val)
  884. goto redirty;
  885. spin_lock(&info->lock);
  886. shmem_recalc_inode(inode);
  887. if (index >= info->next_index) {
  888. BUG_ON(!(info->flags & SHMEM_TRUNCATE));
  889. goto unlock;
  890. }
  891. entry = shmem_swp_entry(info, index, NULL);
  892. BUG_ON(!entry);
  893. BUG_ON(entry->val);
  894. if (move_to_swap_cache(page, swap) == 0) {
  895. shmem_swp_set(info, entry, swap.val);
  896. shmem_swp_unmap(entry);
  897. spin_unlock(&info->lock);
  898. if (list_empty(&info->swaplist)) {
  899. spin_lock(&shmem_swaplist_lock);
  900. /* move instead of add in case we're racing */
  901. list_move_tail(&info->swaplist, &shmem_swaplist);
  902. spin_unlock(&shmem_swaplist_lock);
  903. }
  904. unlock_page(page);
  905. return 0;
  906. }
  907. shmem_swp_unmap(entry);
  908. unlock:
  909. spin_unlock(&info->lock);
  910. swap_free(swap);
  911. redirty:
  912. set_page_dirty(page);
  913. return AOP_WRITEPAGE_ACTIVATE; /* Return with the page locked */
  914. }
  915. #ifdef CONFIG_NUMA
  916. static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes)
  917. {
  918. char *nodelist = strchr(value, ':');
  919. int err = 1;
  920. if (nodelist) {
  921. /* NUL-terminate policy string */
  922. *nodelist++ = '\0';
  923. if (nodelist_parse(nodelist, *policy_nodes))
  924. goto out;
  925. if (!nodes_subset(*policy_nodes, node_states[N_HIGH_MEMORY]))
  926. goto out;
  927. }
  928. if (!strcmp(value, "default")) {
  929. *policy = MPOL_DEFAULT;
  930. /* Don't allow a nodelist */
  931. if (!nodelist)
  932. err = 0;
  933. } else if (!strcmp(value, "prefer")) {
  934. *policy = MPOL_PREFERRED;
  935. /* Insist on a nodelist of one node only */
  936. if (nodelist) {
  937. char *rest = nodelist;
  938. while (isdigit(*rest))
  939. rest++;
  940. if (!*rest)
  941. err = 0;
  942. }
  943. } else if (!strcmp(value, "bind")) {
  944. *policy = MPOL_BIND;
  945. /* Insist on a nodelist */
  946. if (nodelist)
  947. err = 0;
  948. } else if (!strcmp(value, "interleave")) {
  949. *policy = MPOL_INTERLEAVE;
  950. /*
  951. * Default to online nodes with memory if no nodelist
  952. */
  953. if (!nodelist)
  954. *policy_nodes = node_states[N_HIGH_MEMORY];
  955. err = 0;
  956. }
  957. out:
  958. /* Restore string for error message */
  959. if (nodelist)
  960. *--nodelist = ':';
  961. return err;
  962. }
  963. static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
  964. struct shmem_inode_info *info, unsigned long idx)
  965. {
  966. struct vm_area_struct pvma;
  967. struct page *page;
  968. /* Create a pseudo vma that just contains the policy */
  969. pvma.vm_start = 0;
  970. pvma.vm_pgoff = idx;
  971. pvma.vm_ops = NULL;
  972. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
  973. page = swapin_readahead(entry, gfp, &pvma, 0);
  974. mpol_free(pvma.vm_policy);
  975. return page;
  976. }
  977. static struct page *shmem_alloc_page(gfp_t gfp,
  978. struct shmem_inode_info *info, unsigned long idx)
  979. {
  980. struct vm_area_struct pvma;
  981. struct page *page;
  982. /* Create a pseudo vma that just contains the policy */
  983. pvma.vm_start = 0;
  984. pvma.vm_pgoff = idx;
  985. pvma.vm_ops = NULL;
  986. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
  987. page = alloc_page_vma(gfp, &pvma, 0);
  988. mpol_free(pvma.vm_policy);
  989. return page;
  990. }
  991. #else
  992. static inline int shmem_parse_mpol(char *value, int *policy,
  993. nodemask_t *policy_nodes)
  994. {
  995. return 1;
  996. }
  997. static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
  998. struct shmem_inode_info *info, unsigned long idx)
  999. {
  1000. return swapin_readahead(entry, gfp, NULL, 0);
  1001. }
  1002. static inline struct page *shmem_alloc_page(gfp_t gfp,
  1003. struct shmem_inode_info *info, unsigned long idx)
  1004. {
  1005. return alloc_page(gfp);
  1006. }
  1007. #endif
  1008. /*
  1009. * shmem_getpage - either get the page from swap or allocate a new one
  1010. *
  1011. * If we allocate a new one we do not mark it dirty. That's up to the
  1012. * vm. If we swap it in we mark it dirty since we also free the swap
  1013. * entry since a page cannot live in both the swap and page cache
  1014. */
  1015. static int shmem_getpage(struct inode *inode, unsigned long idx,
  1016. struct page **pagep, enum sgp_type sgp, int *type)
  1017. {
  1018. struct address_space *mapping = inode->i_mapping;
  1019. struct shmem_inode_info *info = SHMEM_I(inode);
  1020. struct shmem_sb_info *sbinfo;
  1021. struct page *filepage = *pagep;
  1022. struct page *swappage;
  1023. swp_entry_t *entry;
  1024. swp_entry_t swap;
  1025. gfp_t gfp;
  1026. int error;
  1027. if (idx >= SHMEM_MAX_INDEX)
  1028. return -EFBIG;
  1029. if (type)
  1030. *type = 0;
  1031. /*
  1032. * Normally, filepage is NULL on entry, and either found
  1033. * uptodate immediately, or allocated and zeroed, or read
  1034. * in under swappage, which is then assigned to filepage.
  1035. * But shmem_readpage (required for splice) passes in a locked
  1036. * filepage, which may be found not uptodate by other callers
  1037. * too, and may need to be copied from the swappage read in.
  1038. */
  1039. repeat:
  1040. if (!filepage)
  1041. filepage = find_lock_page(mapping, idx);
  1042. if (filepage && PageUptodate(filepage))
  1043. goto done;
  1044. error = 0;
  1045. gfp = mapping_gfp_mask(mapping);
  1046. spin_lock(&info->lock);
  1047. shmem_recalc_inode(inode);
  1048. entry = shmem_swp_alloc(info, idx, sgp);
  1049. if (IS_ERR(entry)) {
  1050. spin_unlock(&info->lock);
  1051. error = PTR_ERR(entry);
  1052. goto failed;
  1053. }
  1054. swap = *entry;
  1055. if (swap.val) {
  1056. /* Look it up and read it in.. */
  1057. swappage = lookup_swap_cache(swap);
  1058. if (!swappage) {
  1059. shmem_swp_unmap(entry);
  1060. /* here we actually do the io */
  1061. if (type && !(*type & VM_FAULT_MAJOR)) {
  1062. __count_vm_event(PGMAJFAULT);
  1063. *type |= VM_FAULT_MAJOR;
  1064. }
  1065. spin_unlock(&info->lock);
  1066. swappage = shmem_swapin(swap, gfp, info, idx);
  1067. if (!swappage) {
  1068. spin_lock(&info->lock);
  1069. entry = shmem_swp_alloc(info, idx, sgp);
  1070. if (IS_ERR(entry))
  1071. error = PTR_ERR(entry);
  1072. else {
  1073. if (entry->val == swap.val)
  1074. error = -ENOMEM;
  1075. shmem_swp_unmap(entry);
  1076. }
  1077. spin_unlock(&info->lock);
  1078. if (error)
  1079. goto failed;
  1080. goto repeat;
  1081. }
  1082. wait_on_page_locked(swappage);
  1083. page_cache_release(swappage);
  1084. goto repeat;
  1085. }
  1086. /* We have to do this with page locked to prevent races */
  1087. if (TestSetPageLocked(swappage)) {
  1088. shmem_swp_unmap(entry);
  1089. spin_unlock(&info->lock);
  1090. wait_on_page_locked(swappage);
  1091. page_cache_release(swappage);
  1092. goto repeat;
  1093. }
  1094. if (PageWriteback(swappage)) {
  1095. shmem_swp_unmap(entry);
  1096. spin_unlock(&info->lock);
  1097. wait_on_page_writeback(swappage);
  1098. unlock_page(swappage);
  1099. page_cache_release(swappage);
  1100. goto repeat;
  1101. }
  1102. if (!PageUptodate(swappage)) {
  1103. shmem_swp_unmap(entry);
  1104. spin_unlock(&info->lock);
  1105. unlock_page(swappage);
  1106. page_cache_release(swappage);
  1107. error = -EIO;
  1108. goto failed;
  1109. }
  1110. if (filepage) {
  1111. shmem_swp_set(info, entry, 0);
  1112. shmem_swp_unmap(entry);
  1113. delete_from_swap_cache(swappage);
  1114. spin_unlock(&info->lock);
  1115. copy_highpage(filepage, swappage);
  1116. unlock_page(swappage);
  1117. page_cache_release(swappage);
  1118. flush_dcache_page(filepage);
  1119. SetPageUptodate(filepage);
  1120. set_page_dirty(filepage);
  1121. swap_free(swap);
  1122. } else if (!(error = move_from_swap_cache(
  1123. swappage, idx, mapping))) {
  1124. info->flags |= SHMEM_PAGEIN;
  1125. shmem_swp_set(info, entry, 0);
  1126. shmem_swp_unmap(entry);
  1127. spin_unlock(&info->lock);
  1128. filepage = swappage;
  1129. swap_free(swap);
  1130. } else {
  1131. shmem_swp_unmap(entry);
  1132. spin_unlock(&info->lock);
  1133. unlock_page(swappage);
  1134. page_cache_release(swappage);
  1135. if (error == -ENOMEM) {
  1136. /* let kswapd refresh zone for GFP_ATOMICs */
  1137. congestion_wait(WRITE, HZ/50);
  1138. }
  1139. goto repeat;
  1140. }
  1141. } else if (sgp == SGP_READ && !filepage) {
  1142. shmem_swp_unmap(entry);
  1143. filepage = find_get_page(mapping, idx);
  1144. if (filepage &&
  1145. (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
  1146. spin_unlock(&info->lock);
  1147. wait_on_page_locked(filepage);
  1148. page_cache_release(filepage);
  1149. filepage = NULL;
  1150. goto repeat;
  1151. }
  1152. spin_unlock(&info->lock);
  1153. } else {
  1154. shmem_swp_unmap(entry);
  1155. sbinfo = SHMEM_SB(inode->i_sb);
  1156. if (sbinfo->max_blocks) {
  1157. spin_lock(&sbinfo->stat_lock);
  1158. if (sbinfo->free_blocks == 0 ||
  1159. shmem_acct_block(info->flags)) {
  1160. spin_unlock(&sbinfo->stat_lock);
  1161. spin_unlock(&info->lock);
  1162. error = -ENOSPC;
  1163. goto failed;
  1164. }
  1165. sbinfo->free_blocks--;
  1166. inode->i_blocks += BLOCKS_PER_PAGE;
  1167. spin_unlock(&sbinfo->stat_lock);
  1168. } else if (shmem_acct_block(info->flags)) {
  1169. spin_unlock(&info->lock);
  1170. error = -ENOSPC;
  1171. goto failed;
  1172. }
  1173. if (!filepage) {
  1174. spin_unlock(&info->lock);
  1175. filepage = shmem_alloc_page(gfp, info, idx);
  1176. if (!filepage) {
  1177. shmem_unacct_blocks(info->flags, 1);
  1178. shmem_free_blocks(inode, 1);
  1179. error = -ENOMEM;
  1180. goto failed;
  1181. }
  1182. spin_lock(&info->lock);
  1183. entry = shmem_swp_alloc(info, idx, sgp);
  1184. if (IS_ERR(entry))
  1185. error = PTR_ERR(entry);
  1186. else {
  1187. swap = *entry;
  1188. shmem_swp_unmap(entry);
  1189. }
  1190. if (error || swap.val || 0 != add_to_page_cache_lru(
  1191. filepage, mapping, idx, GFP_ATOMIC)) {
  1192. spin_unlock(&info->lock);
  1193. page_cache_release(filepage);
  1194. shmem_unacct_blocks(info->flags, 1);
  1195. shmem_free_blocks(inode, 1);
  1196. filepage = NULL;
  1197. if (error)
  1198. goto failed;
  1199. goto repeat;
  1200. }
  1201. info->flags |= SHMEM_PAGEIN;
  1202. }
  1203. info->alloced++;
  1204. spin_unlock(&info->lock);
  1205. clear_highpage(filepage);
  1206. flush_dcache_page(filepage);
  1207. SetPageUptodate(filepage);
  1208. }
  1209. done:
  1210. *pagep = filepage;
  1211. return 0;
  1212. failed:
  1213. if (*pagep != filepage) {
  1214. unlock_page(filepage);
  1215. page_cache_release(filepage);
  1216. }
  1217. return error;
  1218. }
  1219. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1220. {
  1221. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1222. int error;
  1223. int ret;
  1224. if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  1225. return VM_FAULT_SIGBUS;
  1226. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
  1227. if (error)
  1228. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  1229. mark_page_accessed(vmf->page);
  1230. return ret | VM_FAULT_LOCKED;
  1231. }
  1232. #ifdef CONFIG_NUMA
  1233. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  1234. {
  1235. struct inode *i = vma->vm_file->f_path.dentry->d_inode;
  1236. return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
  1237. }
  1238. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  1239. unsigned long addr)
  1240. {
  1241. struct inode *i = vma->vm_file->f_path.dentry->d_inode;
  1242. unsigned long idx;
  1243. idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1244. return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
  1245. }
  1246. #endif
  1247. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1248. {
  1249. struct inode *inode = file->f_path.dentry->d_inode;
  1250. struct shmem_inode_info *info = SHMEM_I(inode);
  1251. int retval = -ENOMEM;
  1252. spin_lock(&info->lock);
  1253. if (lock && !(info->flags & VM_LOCKED)) {
  1254. if (!user_shm_lock(inode->i_size, user))
  1255. goto out_nomem;
  1256. info->flags |= VM_LOCKED;
  1257. }
  1258. if (!lock && (info->flags & VM_LOCKED) && user) {
  1259. user_shm_unlock(inode->i_size, user);
  1260. info->flags &= ~VM_LOCKED;
  1261. }
  1262. retval = 0;
  1263. out_nomem:
  1264. spin_unlock(&info->lock);
  1265. return retval;
  1266. }
  1267. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1268. {
  1269. file_accessed(file);
  1270. vma->vm_ops = &shmem_vm_ops;
  1271. vma->vm_flags |= VM_CAN_NONLINEAR;
  1272. return 0;
  1273. }
  1274. static struct inode *
  1275. shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
  1276. {
  1277. struct inode *inode;
  1278. struct shmem_inode_info *info;
  1279. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1280. if (shmem_reserve_inode(sb))
  1281. return NULL;
  1282. inode = new_inode(sb);
  1283. if (inode) {
  1284. inode->i_mode = mode;
  1285. inode->i_uid = current->fsuid;
  1286. inode->i_gid = current->fsgid;
  1287. inode->i_blocks = 0;
  1288. inode->i_mapping->a_ops = &shmem_aops;
  1289. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1290. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1291. inode->i_generation = get_seconds();
  1292. info = SHMEM_I(inode);
  1293. memset(info, 0, (char *)inode - (char *)info);
  1294. spin_lock_init(&info->lock);
  1295. INIT_LIST_HEAD(&info->swaplist);
  1296. switch (mode & S_IFMT) {
  1297. default:
  1298. inode->i_op = &shmem_special_inode_operations;
  1299. init_special_inode(inode, mode, dev);
  1300. break;
  1301. case S_IFREG:
  1302. inode->i_op = &shmem_inode_operations;
  1303. inode->i_fop = &shmem_file_operations;
  1304. mpol_shared_policy_init(&info->policy, sbinfo->policy,
  1305. &sbinfo->policy_nodes);
  1306. break;
  1307. case S_IFDIR:
  1308. inc_nlink(inode);
  1309. /* Some things misbehave if size == 0 on a directory */
  1310. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1311. inode->i_op = &shmem_dir_inode_operations;
  1312. inode->i_fop = &simple_dir_operations;
  1313. break;
  1314. case S_IFLNK:
  1315. /*
  1316. * Must not load anything in the rbtree,
  1317. * mpol_free_shared_policy will not be called.
  1318. */
  1319. mpol_shared_policy_init(&info->policy, MPOL_DEFAULT,
  1320. NULL);
  1321. break;
  1322. }
  1323. } else
  1324. shmem_free_inode(sb);
  1325. return inode;
  1326. }
  1327. #ifdef CONFIG_TMPFS
  1328. static const struct inode_operations shmem_symlink_inode_operations;
  1329. static const struct inode_operations shmem_symlink_inline_operations;
  1330. /*
  1331. * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
  1332. * but providing them allows a tmpfs file to be used for splice, sendfile, and
  1333. * below the loop driver, in the generic fashion that many filesystems support.
  1334. */
  1335. static int shmem_readpage(struct file *file, struct page *page)
  1336. {
  1337. struct inode *inode = page->mapping->host;
  1338. int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
  1339. unlock_page(page);
  1340. return error;
  1341. }
  1342. static int
  1343. shmem_write_begin(struct file *file, struct address_space *mapping,
  1344. loff_t pos, unsigned len, unsigned flags,
  1345. struct page **pagep, void **fsdata)
  1346. {
  1347. struct inode *inode = mapping->host;
  1348. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1349. *pagep = NULL;
  1350. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1351. }
  1352. static int
  1353. shmem_write_end(struct file *file, struct address_space *mapping,
  1354. loff_t pos, unsigned len, unsigned copied,
  1355. struct page *page, void *fsdata)
  1356. {
  1357. struct inode *inode = mapping->host;
  1358. if (pos + copied > inode->i_size)
  1359. i_size_write(inode, pos + copied);
  1360. unlock_page(page);
  1361. set_page_dirty(page);
  1362. page_cache_release(page);
  1363. return copied;
  1364. }
  1365. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1366. {
  1367. struct inode *inode = filp->f_path.dentry->d_inode;
  1368. struct address_space *mapping = inode->i_mapping;
  1369. unsigned long index, offset;
  1370. index = *ppos >> PAGE_CACHE_SHIFT;
  1371. offset = *ppos & ~PAGE_CACHE_MASK;
  1372. for (;;) {
  1373. struct page *page = NULL;
  1374. unsigned long end_index, nr, ret;
  1375. loff_t i_size = i_size_read(inode);
  1376. end_index = i_size >> PAGE_CACHE_SHIFT;
  1377. if (index > end_index)
  1378. break;
  1379. if (index == end_index) {
  1380. nr = i_size & ~PAGE_CACHE_MASK;
  1381. if (nr <= offset)
  1382. break;
  1383. }
  1384. desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
  1385. if (desc->error) {
  1386. if (desc->error == -EINVAL)
  1387. desc->error = 0;
  1388. break;
  1389. }
  1390. if (page)
  1391. unlock_page(page);
  1392. /*
  1393. * We must evaluate after, since reads (unlike writes)
  1394. * are called without i_mutex protection against truncate
  1395. */
  1396. nr = PAGE_CACHE_SIZE;
  1397. i_size = i_size_read(inode);
  1398. end_index = i_size >> PAGE_CACHE_SHIFT;
  1399. if (index == end_index) {
  1400. nr = i_size & ~PAGE_CACHE_MASK;
  1401. if (nr <= offset) {
  1402. if (page)
  1403. page_cache_release(page);
  1404. break;
  1405. }
  1406. }
  1407. nr -= offset;
  1408. if (page) {
  1409. /*
  1410. * If users can be writing to this page using arbitrary
  1411. * virtual addresses, take care about potential aliasing
  1412. * before reading the page on the kernel side.
  1413. */
  1414. if (mapping_writably_mapped(mapping))
  1415. flush_dcache_page(page);
  1416. /*
  1417. * Mark the page accessed if we read the beginning.
  1418. */
  1419. if (!offset)
  1420. mark_page_accessed(page);
  1421. } else {
  1422. page = ZERO_PAGE(0);
  1423. page_cache_get(page);
  1424. }
  1425. /*
  1426. * Ok, we have the page, and it's up-to-date, so
  1427. * now we can copy it to user space...
  1428. *
  1429. * The actor routine returns how many bytes were actually used..
  1430. * NOTE! This may not be the same as how much of a user buffer
  1431. * we filled up (we may be padding etc), so we can only update
  1432. * "pos" here (the actor routine has to update the user buffer
  1433. * pointers and the remaining count).
  1434. */
  1435. ret = actor(desc, page, offset, nr);
  1436. offset += ret;
  1437. index += offset >> PAGE_CACHE_SHIFT;
  1438. offset &= ~PAGE_CACHE_MASK;
  1439. page_cache_release(page);
  1440. if (ret != nr || !desc->count)
  1441. break;
  1442. cond_resched();
  1443. }
  1444. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1445. file_accessed(filp);
  1446. }
  1447. static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  1448. {
  1449. read_descriptor_t desc;
  1450. if ((ssize_t) count < 0)
  1451. return -EINVAL;
  1452. if (!access_ok(VERIFY_WRITE, buf, count))
  1453. return -EFAULT;
  1454. if (!count)
  1455. return 0;
  1456. desc.written = 0;
  1457. desc.count = count;
  1458. desc.arg.buf = buf;
  1459. desc.error = 0;
  1460. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1461. if (desc.written)
  1462. return desc.written;
  1463. return desc.error;
  1464. }
  1465. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1466. {
  1467. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1468. buf->f_type = TMPFS_MAGIC;
  1469. buf->f_bsize = PAGE_CACHE_SIZE;
  1470. buf->f_namelen = NAME_MAX;
  1471. spin_lock(&sbinfo->stat_lock);
  1472. if (sbinfo->max_blocks) {
  1473. buf->f_blocks = sbinfo->max_blocks;
  1474. buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
  1475. }
  1476. if (sbinfo->max_inodes) {
  1477. buf->f_files = sbinfo->max_inodes;
  1478. buf->f_ffree = sbinfo->free_inodes;
  1479. }
  1480. /* else leave those fields 0 like simple_statfs */
  1481. spin_unlock(&sbinfo->stat_lock);
  1482. return 0;
  1483. }
  1484. /*
  1485. * File creation. Allocate an inode, and we're done..
  1486. */
  1487. static int
  1488. shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1489. {
  1490. struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
  1491. int error = -ENOSPC;
  1492. if (inode) {
  1493. error = security_inode_init_security(inode, dir, NULL, NULL,
  1494. NULL);
  1495. if (error) {
  1496. if (error != -EOPNOTSUPP) {
  1497. iput(inode);
  1498. return error;
  1499. }
  1500. }
  1501. error = shmem_acl_init(inode, dir);
  1502. if (error) {
  1503. iput(inode);
  1504. return error;
  1505. }
  1506. if (dir->i_mode & S_ISGID) {
  1507. inode->i_gid = dir->i_gid;
  1508. if (S_ISDIR(mode))
  1509. inode->i_mode |= S_ISGID;
  1510. }
  1511. dir->i_size += BOGO_DIRENT_SIZE;
  1512. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1513. d_instantiate(dentry, inode);
  1514. dget(dentry); /* Extra count - pin the dentry in core */
  1515. }
  1516. return error;
  1517. }
  1518. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1519. {
  1520. int error;
  1521. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1522. return error;
  1523. inc_nlink(dir);
  1524. return 0;
  1525. }
  1526. static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
  1527. struct nameidata *nd)
  1528. {
  1529. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1530. }
  1531. /*
  1532. * Link a file..
  1533. */
  1534. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1535. {
  1536. struct inode *inode = old_dentry->d_inode;
  1537. int ret;
  1538. /*
  1539. * No ordinary (disk based) filesystem counts links as inodes;
  1540. * but each new link needs a new dentry, pinning lowmem, and
  1541. * tmpfs dentries cannot be pruned until they are unlinked.
  1542. */
  1543. ret = shmem_reserve_inode(inode->i_sb);
  1544. if (ret)
  1545. goto out;
  1546. dir->i_size += BOGO_DIRENT_SIZE;
  1547. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1548. inc_nlink(inode);
  1549. atomic_inc(&inode->i_count); /* New dentry reference */
  1550. dget(dentry); /* Extra pinning count for the created dentry */
  1551. d_instantiate(dentry, inode);
  1552. out:
  1553. return ret;
  1554. }
  1555. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1556. {
  1557. struct inode *inode = dentry->d_inode;
  1558. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  1559. shmem_free_inode(inode->i_sb);
  1560. dir->i_size -= BOGO_DIRENT_SIZE;
  1561. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1562. drop_nlink(inode);
  1563. dput(dentry); /* Undo the count from "create" - this does all the work */
  1564. return 0;
  1565. }
  1566. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1567. {
  1568. if (!simple_empty(dentry))
  1569. return -ENOTEMPTY;
  1570. drop_nlink(dentry->d_inode);
  1571. drop_nlink(dir);
  1572. return shmem_unlink(dir, dentry);
  1573. }
  1574. /*
  1575. * The VFS layer already does all the dentry stuff for rename,
  1576. * we just have to decrement the usage count for the target if
  1577. * it exists so that the VFS layer correctly free's it when it
  1578. * gets overwritten.
  1579. */
  1580. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1581. {
  1582. struct inode *inode = old_dentry->d_inode;
  1583. int they_are_dirs = S_ISDIR(inode->i_mode);
  1584. if (!simple_empty(new_dentry))
  1585. return -ENOTEMPTY;
  1586. if (new_dentry->d_inode) {
  1587. (void) shmem_unlink(new_dir, new_dentry);
  1588. if (they_are_dirs)
  1589. drop_nlink(old_dir);
  1590. } else if (they_are_dirs) {
  1591. drop_nlink(old_dir);
  1592. inc_nlink(new_dir);
  1593. }
  1594. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1595. new_dir->i_size += BOGO_DIRENT_SIZE;
  1596. old_dir->i_ctime = old_dir->i_mtime =
  1597. new_dir->i_ctime = new_dir->i_mtime =
  1598. inode->i_ctime = CURRENT_TIME;
  1599. return 0;
  1600. }
  1601. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1602. {
  1603. int error;
  1604. int len;
  1605. struct inode *inode;
  1606. struct page *page = NULL;
  1607. char *kaddr;
  1608. struct shmem_inode_info *info;
  1609. len = strlen(symname) + 1;
  1610. if (len > PAGE_CACHE_SIZE)
  1611. return -ENAMETOOLONG;
  1612. inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
  1613. if (!inode)
  1614. return -ENOSPC;
  1615. error = security_inode_init_security(inode, dir, NULL, NULL,
  1616. NULL);
  1617. if (error) {
  1618. if (error != -EOPNOTSUPP) {
  1619. iput(inode);
  1620. return error;
  1621. }
  1622. error = 0;
  1623. }
  1624. info = SHMEM_I(inode);
  1625. inode->i_size = len-1;
  1626. if (len <= (char *)inode - (char *)info) {
  1627. /* do it inline */
  1628. memcpy(info, symname, len);
  1629. inode->i_op = &shmem_symlink_inline_operations;
  1630. } else {
  1631. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1632. if (error) {
  1633. iput(inode);
  1634. return error;
  1635. }
  1636. unlock_page(page);
  1637. inode->i_op = &shmem_symlink_inode_operations;
  1638. kaddr = kmap_atomic(page, KM_USER0);
  1639. memcpy(kaddr, symname, len);
  1640. kunmap_atomic(kaddr, KM_USER0);
  1641. set_page_dirty(page);
  1642. page_cache_release(page);
  1643. }
  1644. if (dir->i_mode & S_ISGID)
  1645. inode->i_gid = dir->i_gid;
  1646. dir->i_size += BOGO_DIRENT_SIZE;
  1647. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1648. d_instantiate(dentry, inode);
  1649. dget(dentry);
  1650. return 0;
  1651. }
  1652. static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
  1653. {
  1654. nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
  1655. return NULL;
  1656. }
  1657. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1658. {
  1659. struct page *page = NULL;
  1660. int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1661. nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
  1662. if (page)
  1663. unlock_page(page);
  1664. return page;
  1665. }
  1666. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1667. {
  1668. if (!IS_ERR(nd_get_link(nd))) {
  1669. struct page *page = cookie;
  1670. kunmap(page);
  1671. mark_page_accessed(page);
  1672. page_cache_release(page);
  1673. }
  1674. }
  1675. static const struct inode_operations shmem_symlink_inline_operations = {
  1676. .readlink = generic_readlink,
  1677. .follow_link = shmem_follow_link_inline,
  1678. };
  1679. static const struct inode_operations shmem_symlink_inode_operations = {
  1680. .truncate = shmem_truncate,
  1681. .readlink = generic_readlink,
  1682. .follow_link = shmem_follow_link,
  1683. .put_link = shmem_put_link,
  1684. };
  1685. #ifdef CONFIG_TMPFS_POSIX_ACL
  1686. /**
  1687. * Superblocks without xattr inode operations will get security.* xattr
  1688. * support from the VFS "for free". As soon as we have any other xattrs
  1689. * like ACLs, we also need to implement the security.* handlers at
  1690. * filesystem level, though.
  1691. */
  1692. static size_t shmem_xattr_security_list(struct inode *inode, char *list,
  1693. size_t list_len, const char *name,
  1694. size_t name_len)
  1695. {
  1696. return security_inode_listsecurity(inode, list, list_len);
  1697. }
  1698. static int shmem_xattr_security_get(struct inode *inode, const char *name,
  1699. void *buffer, size_t size)
  1700. {
  1701. if (strcmp(name, "") == 0)
  1702. return -EINVAL;
  1703. return security_inode_getsecurity(inode, name, buffer, size,
  1704. -EOPNOTSUPP);
  1705. }
  1706. static int shmem_xattr_security_set(struct inode *inode, const char *name,
  1707. const void *value, size_t size, int flags)
  1708. {
  1709. if (strcmp(name, "") == 0)
  1710. return -EINVAL;
  1711. return security_inode_setsecurity(inode, name, value, size, flags);
  1712. }
  1713. static struct xattr_handler shmem_xattr_security_handler = {
  1714. .prefix = XATTR_SECURITY_PREFIX,
  1715. .list = shmem_xattr_security_list,
  1716. .get = shmem_xattr_security_get,
  1717. .set = shmem_xattr_security_set,
  1718. };
  1719. static struct xattr_handler *shmem_xattr_handlers[] = {
  1720. &shmem_xattr_acl_access_handler,
  1721. &shmem_xattr_acl_default_handler,
  1722. &shmem_xattr_security_handler,
  1723. NULL
  1724. };
  1725. #endif
  1726. static struct dentry *shmem_get_parent(struct dentry *child)
  1727. {
  1728. return ERR_PTR(-ESTALE);
  1729. }
  1730. static int shmem_match(struct inode *ino, void *vfh)
  1731. {
  1732. __u32 *fh = vfh;
  1733. __u64 inum = fh[2];
  1734. inum = (inum << 32) | fh[1];
  1735. return ino->i_ino == inum && fh[0] == ino->i_generation;
  1736. }
  1737. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  1738. struct fid *fid, int fh_len, int fh_type)
  1739. {
  1740. struct inode *inode;
  1741. struct dentry *dentry = NULL;
  1742. u64 inum = fid->raw[2];
  1743. inum = (inum << 32) | fid->raw[1];
  1744. if (fh_len < 3)
  1745. return NULL;
  1746. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  1747. shmem_match, fid->raw);
  1748. if (inode) {
  1749. dentry = d_find_alias(inode);
  1750. iput(inode);
  1751. }
  1752. return dentry;
  1753. }
  1754. static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
  1755. int connectable)
  1756. {
  1757. struct inode *inode = dentry->d_inode;
  1758. if (*len < 3)
  1759. return 255;
  1760. if (hlist_unhashed(&inode->i_hash)) {
  1761. /* Unfortunately insert_inode_hash is not idempotent,
  1762. * so as we hash inodes here rather than at creation
  1763. * time, we need a lock to ensure we only try
  1764. * to do it once
  1765. */
  1766. static DEFINE_SPINLOCK(lock);
  1767. spin_lock(&lock);
  1768. if (hlist_unhashed(&inode->i_hash))
  1769. __insert_inode_hash(inode,
  1770. inode->i_ino + inode->i_generation);
  1771. spin_unlock(&lock);
  1772. }
  1773. fh[0] = inode->i_generation;
  1774. fh[1] = inode->i_ino;
  1775. fh[2] = ((__u64)inode->i_ino) >> 32;
  1776. *len = 3;
  1777. return 1;
  1778. }
  1779. static const struct export_operations shmem_export_ops = {
  1780. .get_parent = shmem_get_parent,
  1781. .encode_fh = shmem_encode_fh,
  1782. .fh_to_dentry = shmem_fh_to_dentry,
  1783. };
  1784. static int shmem_parse_options(char *options, int *mode, uid_t *uid,
  1785. gid_t *gid, unsigned long *blocks, unsigned long *inodes,
  1786. int *policy, nodemask_t *policy_nodes)
  1787. {
  1788. char *this_char, *value, *rest;
  1789. while (options != NULL) {
  1790. this_char = options;
  1791. for (;;) {
  1792. /*
  1793. * NUL-terminate this option: unfortunately,
  1794. * mount options form a comma-separated list,
  1795. * but mpol's nodelist may also contain commas.
  1796. */
  1797. options = strchr(options, ',');
  1798. if (options == NULL)
  1799. break;
  1800. options++;
  1801. if (!isdigit(*options)) {
  1802. options[-1] = '\0';
  1803. break;
  1804. }
  1805. }
  1806. if (!*this_char)
  1807. continue;
  1808. if ((value = strchr(this_char,'=')) != NULL) {
  1809. *value++ = 0;
  1810. } else {
  1811. printk(KERN_ERR
  1812. "tmpfs: No value for mount option '%s'\n",
  1813. this_char);
  1814. return 1;
  1815. }
  1816. if (!strcmp(this_char,"size")) {
  1817. unsigned long long size;
  1818. size = memparse(value,&rest);
  1819. if (*rest == '%') {
  1820. size <<= PAGE_SHIFT;
  1821. size *= totalram_pages;
  1822. do_div(size, 100);
  1823. rest++;
  1824. }
  1825. if (*rest)
  1826. goto bad_val;
  1827. *blocks = size >> PAGE_CACHE_SHIFT;
  1828. } else if (!strcmp(this_char,"nr_blocks")) {
  1829. *blocks = memparse(value,&rest);
  1830. if (*rest)
  1831. goto bad_val;
  1832. } else if (!strcmp(this_char,"nr_inodes")) {
  1833. *inodes = memparse(value,&rest);
  1834. if (*rest)
  1835. goto bad_val;
  1836. } else if (!strcmp(this_char,"mode")) {
  1837. if (!mode)
  1838. continue;
  1839. *mode = simple_strtoul(value,&rest,8);
  1840. if (*rest)
  1841. goto bad_val;
  1842. } else if (!strcmp(this_char,"uid")) {
  1843. if (!uid)
  1844. continue;
  1845. *uid = simple_strtoul(value,&rest,0);
  1846. if (*rest)
  1847. goto bad_val;
  1848. } else if (!strcmp(this_char,"gid")) {
  1849. if (!gid)
  1850. continue;
  1851. *gid = simple_strtoul(value,&rest,0);
  1852. if (*rest)
  1853. goto bad_val;
  1854. } else if (!strcmp(this_char,"mpol")) {
  1855. if (shmem_parse_mpol(value,policy,policy_nodes))
  1856. goto bad_val;
  1857. } else {
  1858. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  1859. this_char);
  1860. return 1;
  1861. }
  1862. }
  1863. return 0;
  1864. bad_val:
  1865. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  1866. value, this_char);
  1867. return 1;
  1868. }
  1869. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  1870. {
  1871. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1872. unsigned long max_blocks = sbinfo->max_blocks;
  1873. unsigned long max_inodes = sbinfo->max_inodes;
  1874. int policy = sbinfo->policy;
  1875. nodemask_t policy_nodes = sbinfo->policy_nodes;
  1876. unsigned long blocks;
  1877. unsigned long inodes;
  1878. int error = -EINVAL;
  1879. if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks,
  1880. &max_inodes, &policy, &policy_nodes))
  1881. return error;
  1882. spin_lock(&sbinfo->stat_lock);
  1883. blocks = sbinfo->max_blocks - sbinfo->free_blocks;
  1884. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  1885. if (max_blocks < blocks)
  1886. goto out;
  1887. if (max_inodes < inodes)
  1888. goto out;
  1889. /*
  1890. * Those tests also disallow limited->unlimited while any are in
  1891. * use, so i_blocks will always be zero when max_blocks is zero;
  1892. * but we must separately disallow unlimited->limited, because
  1893. * in that case we have no record of how much is already in use.
  1894. */
  1895. if (max_blocks && !sbinfo->max_blocks)
  1896. goto out;
  1897. if (max_inodes && !sbinfo->max_inodes)
  1898. goto out;
  1899. error = 0;
  1900. sbinfo->max_blocks = max_blocks;
  1901. sbinfo->free_blocks = max_blocks - blocks;
  1902. sbinfo->max_inodes = max_inodes;
  1903. sbinfo->free_inodes = max_inodes - inodes;
  1904. sbinfo->policy = policy;
  1905. sbinfo->policy_nodes = policy_nodes;
  1906. out:
  1907. spin_unlock(&sbinfo->stat_lock);
  1908. return error;
  1909. }
  1910. #endif
  1911. static void shmem_put_super(struct super_block *sb)
  1912. {
  1913. kfree(sb->s_fs_info);
  1914. sb->s_fs_info = NULL;
  1915. }
  1916. static int shmem_fill_super(struct super_block *sb,
  1917. void *data, int silent)
  1918. {
  1919. struct inode *inode;
  1920. struct dentry *root;
  1921. int mode = S_IRWXUGO | S_ISVTX;
  1922. uid_t uid = current->fsuid;
  1923. gid_t gid = current->fsgid;
  1924. int err = -ENOMEM;
  1925. struct shmem_sb_info *sbinfo;
  1926. unsigned long blocks = 0;
  1927. unsigned long inodes = 0;
  1928. int policy = MPOL_DEFAULT;
  1929. nodemask_t policy_nodes = node_states[N_HIGH_MEMORY];
  1930. #ifdef CONFIG_TMPFS
  1931. /*
  1932. * Per default we only allow half of the physical ram per
  1933. * tmpfs instance, limiting inodes to one per page of lowmem;
  1934. * but the internal instance is left unlimited.
  1935. */
  1936. if (!(sb->s_flags & MS_NOUSER)) {
  1937. blocks = totalram_pages / 2;
  1938. inodes = totalram_pages - totalhigh_pages;
  1939. if (inodes > blocks)
  1940. inodes = blocks;
  1941. if (shmem_parse_options(data, &mode, &uid, &gid, &blocks,
  1942. &inodes, &policy, &policy_nodes))
  1943. return -EINVAL;
  1944. }
  1945. sb->s_export_op = &shmem_export_ops;
  1946. #else
  1947. sb->s_flags |= MS_NOUSER;
  1948. #endif
  1949. /* Round up to L1_CACHE_BYTES to resist false sharing */
  1950. sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
  1951. L1_CACHE_BYTES), GFP_KERNEL);
  1952. if (!sbinfo)
  1953. return -ENOMEM;
  1954. spin_lock_init(&sbinfo->stat_lock);
  1955. sbinfo->max_blocks = blocks;
  1956. sbinfo->free_blocks = blocks;
  1957. sbinfo->max_inodes = inodes;
  1958. sbinfo->free_inodes = inodes;
  1959. sbinfo->policy = policy;
  1960. sbinfo->policy_nodes = policy_nodes;
  1961. sb->s_fs_info = sbinfo;
  1962. sb->s_maxbytes = SHMEM_MAX_BYTES;
  1963. sb->s_blocksize = PAGE_CACHE_SIZE;
  1964. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1965. sb->s_magic = TMPFS_MAGIC;
  1966. sb->s_op = &shmem_ops;
  1967. sb->s_time_gran = 1;
  1968. #ifdef CONFIG_TMPFS_POSIX_ACL
  1969. sb->s_xattr = shmem_xattr_handlers;
  1970. sb->s_flags |= MS_POSIXACL;
  1971. #endif
  1972. inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
  1973. if (!inode)
  1974. goto failed;
  1975. inode->i_uid = uid;
  1976. inode->i_gid = gid;
  1977. root = d_alloc_root(inode);
  1978. if (!root)
  1979. goto failed_iput;
  1980. sb->s_root = root;
  1981. return 0;
  1982. failed_iput:
  1983. iput(inode);
  1984. failed:
  1985. shmem_put_super(sb);
  1986. return err;
  1987. }
  1988. static struct kmem_cache *shmem_inode_cachep;
  1989. static struct inode *shmem_alloc_inode(struct super_block *sb)
  1990. {
  1991. struct shmem_inode_info *p;
  1992. p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  1993. if (!p)
  1994. return NULL;
  1995. return &p->vfs_inode;
  1996. }
  1997. static void shmem_destroy_inode(struct inode *inode)
  1998. {
  1999. if ((inode->i_mode & S_IFMT) == S_IFREG) {
  2000. /* only struct inode is valid if it's an inline symlink */
  2001. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  2002. }
  2003. shmem_acl_destroy_inode(inode);
  2004. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  2005. }
  2006. static void init_once(struct kmem_cache *cachep, void *foo)
  2007. {
  2008. struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
  2009. inode_init_once(&p->vfs_inode);
  2010. #ifdef CONFIG_TMPFS_POSIX_ACL
  2011. p->i_acl = NULL;
  2012. p->i_default_acl = NULL;
  2013. #endif
  2014. }
  2015. static int init_inodecache(void)
  2016. {
  2017. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  2018. sizeof(struct shmem_inode_info),
  2019. 0, SLAB_PANIC, init_once);
  2020. return 0;
  2021. }
  2022. static void destroy_inodecache(void)
  2023. {
  2024. kmem_cache_destroy(shmem_inode_cachep);
  2025. }
  2026. static const struct address_space_operations shmem_aops = {
  2027. .writepage = shmem_writepage,
  2028. .set_page_dirty = __set_page_dirty_no_writeback,
  2029. #ifdef CONFIG_TMPFS
  2030. .readpage = shmem_readpage,
  2031. .write_begin = shmem_write_begin,
  2032. .write_end = shmem_write_end,
  2033. #endif
  2034. .migratepage = migrate_page,
  2035. };
  2036. static const struct file_operations shmem_file_operations = {
  2037. .mmap = shmem_mmap,
  2038. #ifdef CONFIG_TMPFS
  2039. .llseek = generic_file_llseek,
  2040. .read = shmem_file_read,
  2041. .write = do_sync_write,
  2042. .aio_write = generic_file_aio_write,
  2043. .fsync = simple_sync_file,
  2044. .splice_read = generic_file_splice_read,
  2045. .splice_write = generic_file_splice_write,
  2046. #endif
  2047. };
  2048. static const struct inode_operations shmem_inode_operations = {
  2049. .truncate = shmem_truncate,
  2050. .setattr = shmem_notify_change,
  2051. .truncate_range = shmem_truncate_range,
  2052. #ifdef CONFIG_TMPFS_POSIX_ACL
  2053. .setxattr = generic_setxattr,
  2054. .getxattr = generic_getxattr,
  2055. .listxattr = generic_listxattr,
  2056. .removexattr = generic_removexattr,
  2057. .permission = shmem_permission,
  2058. #endif
  2059. };
  2060. static const struct inode_operations shmem_dir_inode_operations = {
  2061. #ifdef CONFIG_TMPFS
  2062. .create = shmem_create,
  2063. .lookup = simple_lookup,
  2064. .link = shmem_link,
  2065. .unlink = shmem_unlink,
  2066. .symlink = shmem_symlink,
  2067. .mkdir = shmem_mkdir,
  2068. .rmdir = shmem_rmdir,
  2069. .mknod = shmem_mknod,
  2070. .rename = shmem_rename,
  2071. #endif
  2072. #ifdef CONFIG_TMPFS_POSIX_ACL
  2073. .setattr = shmem_notify_change,
  2074. .setxattr = generic_setxattr,
  2075. .getxattr = generic_getxattr,
  2076. .listxattr = generic_listxattr,
  2077. .removexattr = generic_removexattr,
  2078. .permission = shmem_permission,
  2079. #endif
  2080. };
  2081. static const struct inode_operations shmem_special_inode_operations = {
  2082. #ifdef CONFIG_TMPFS_POSIX_ACL
  2083. .setattr = shmem_notify_change,
  2084. .setxattr = generic_setxattr,
  2085. .getxattr = generic_getxattr,
  2086. .listxattr = generic_listxattr,
  2087. .removexattr = generic_removexattr,
  2088. .permission = shmem_permission,
  2089. #endif
  2090. };
  2091. static const struct super_operations shmem_ops = {
  2092. .alloc_inode = shmem_alloc_inode,
  2093. .destroy_inode = shmem_destroy_inode,
  2094. #ifdef CONFIG_TMPFS
  2095. .statfs = shmem_statfs,
  2096. .remount_fs = shmem_remount_fs,
  2097. #endif
  2098. .delete_inode = shmem_delete_inode,
  2099. .drop_inode = generic_delete_inode,
  2100. .put_super = shmem_put_super,
  2101. };
  2102. static struct vm_operations_struct shmem_vm_ops = {
  2103. .fault = shmem_fault,
  2104. #ifdef CONFIG_NUMA
  2105. .set_policy = shmem_set_policy,
  2106. .get_policy = shmem_get_policy,
  2107. #endif
  2108. };
  2109. static int shmem_get_sb(struct file_system_type *fs_type,
  2110. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  2111. {
  2112. return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt);
  2113. }
  2114. static struct file_system_type tmpfs_fs_type = {
  2115. .owner = THIS_MODULE,
  2116. .name = "tmpfs",
  2117. .get_sb = shmem_get_sb,
  2118. .kill_sb = kill_litter_super,
  2119. };
  2120. static struct vfsmount *shm_mnt;
  2121. static int __init init_tmpfs(void)
  2122. {
  2123. int error;
  2124. error = bdi_init(&shmem_backing_dev_info);
  2125. if (error)
  2126. goto out4;
  2127. error = init_inodecache();
  2128. if (error)
  2129. goto out3;
  2130. error = register_filesystem(&tmpfs_fs_type);
  2131. if (error) {
  2132. printk(KERN_ERR "Could not register tmpfs\n");
  2133. goto out2;
  2134. }
  2135. shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
  2136. tmpfs_fs_type.name, NULL);
  2137. if (IS_ERR(shm_mnt)) {
  2138. error = PTR_ERR(shm_mnt);
  2139. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2140. goto out1;
  2141. }
  2142. return 0;
  2143. out1:
  2144. unregister_filesystem(&tmpfs_fs_type);
  2145. out2:
  2146. destroy_inodecache();
  2147. out3:
  2148. bdi_destroy(&shmem_backing_dev_info);
  2149. out4:
  2150. shm_mnt = ERR_PTR(error);
  2151. return error;
  2152. }
  2153. module_init(init_tmpfs)
  2154. /*
  2155. * shmem_file_setup - get an unlinked file living in tmpfs
  2156. *
  2157. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2158. * @size: size to be set for the file
  2159. *
  2160. */
  2161. struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
  2162. {
  2163. int error;
  2164. struct file *file;
  2165. struct inode *inode;
  2166. struct dentry *dentry, *root;
  2167. struct qstr this;
  2168. if (IS_ERR(shm_mnt))
  2169. return (void *)shm_mnt;
  2170. if (size < 0 || size > SHMEM_MAX_BYTES)
  2171. return ERR_PTR(-EINVAL);
  2172. if (shmem_acct_size(flags, size))
  2173. return ERR_PTR(-ENOMEM);
  2174. error = -ENOMEM;
  2175. this.name = name;
  2176. this.len = strlen(name);
  2177. this.hash = 0; /* will go */
  2178. root = shm_mnt->mnt_root;
  2179. dentry = d_alloc(root, &this);
  2180. if (!dentry)
  2181. goto put_memory;
  2182. error = -ENFILE;
  2183. file = get_empty_filp();
  2184. if (!file)
  2185. goto put_dentry;
  2186. error = -ENOSPC;
  2187. inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
  2188. if (!inode)
  2189. goto close_file;
  2190. SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
  2191. d_instantiate(dentry, inode);
  2192. inode->i_size = size;
  2193. inode->i_nlink = 0; /* It is unlinked */
  2194. init_file(file, shm_mnt, dentry, FMODE_WRITE | FMODE_READ,
  2195. &shmem_file_operations);
  2196. return file;
  2197. close_file:
  2198. put_filp(file);
  2199. put_dentry:
  2200. dput(dentry);
  2201. put_memory:
  2202. shmem_unacct_size(flags, size);
  2203. return ERR_PTR(error);
  2204. }
  2205. /*
  2206. * shmem_zero_setup - setup a shared anonymous mapping
  2207. *
  2208. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2209. */
  2210. int shmem_zero_setup(struct vm_area_struct *vma)
  2211. {
  2212. struct file *file;
  2213. loff_t size = vma->vm_end - vma->vm_start;
  2214. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2215. if (IS_ERR(file))
  2216. return PTR_ERR(file);
  2217. if (vma->vm_file)
  2218. fput(vma->vm_file);
  2219. vma->vm_file = file;
  2220. vma->vm_ops = &shmem_vm_ops;
  2221. return 0;
  2222. }