shmem.c 69 KB

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