shmem.c 63 KB

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