shmem.c 71 KB

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