shmem.c 66 KB

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