shmem.c 68 KB

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