shmem.c 64 KB

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