shmem.c 68 KB

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