shmem.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304
  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/config.h>
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/devfs_fs_kernel.h>
  28. #include <linux/fs.h>
  29. #include <linux/mm.h>
  30. #include <linux/mman.h>
  31. #include <linux/file.h>
  32. #include <linux/swap.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/string.h>
  35. #include <linux/slab.h>
  36. #include <linux/backing-dev.h>
  37. #include <linux/shmem_fs.h>
  38. #include <linux/mount.h>
  39. #include <linux/writeback.h>
  40. #include <linux/vfs.h>
  41. #include <linux/blkdev.h>
  42. #include <linux/security.h>
  43. #include <linux/swapops.h>
  44. #include <linux/mempolicy.h>
  45. #include <linux/namei.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/div64.h>
  48. #include <asm/pgtable.h>
  49. /* This magic number is used in glibc for posix shared memory */
  50. #define TMPFS_MAGIC 0x01021994
  51. #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
  52. #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
  53. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  54. #define SHMEM_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
  55. #define SHMEM_MAX_BYTES ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
  56. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  57. /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
  58. #define SHMEM_PAGEIN VM_READ
  59. #define SHMEM_TRUNCATE VM_WRITE
  60. /* Definition to limit shmem_truncate's steps between cond_rescheds */
  61. #define LATENCY_LIMIT 64
  62. /* Pretend that each entry is of this size in directory's i_size */
  63. #define BOGO_DIRENT_SIZE 20
  64. /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
  65. enum sgp_type {
  66. SGP_QUICK, /* don't try more than file page cache lookup */
  67. SGP_READ, /* don't exceed i_size, don't allocate page */
  68. SGP_CACHE, /* don't exceed i_size, may allocate page */
  69. SGP_WRITE, /* may exceed i_size, may allocate page */
  70. };
  71. static int shmem_getpage(struct inode *inode, unsigned long idx,
  72. struct page **pagep, enum sgp_type sgp, int *type);
  73. static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
  74. {
  75. /*
  76. * The above definition of ENTRIES_PER_PAGE, and the use of
  77. * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
  78. * might be reconsidered if it ever diverges from PAGE_SIZE.
  79. */
  80. return alloc_pages(gfp_mask, PAGE_CACHE_SHIFT-PAGE_SHIFT);
  81. }
  82. static inline void shmem_dir_free(struct page *page)
  83. {
  84. __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
  85. }
  86. static struct page **shmem_dir_map(struct page *page)
  87. {
  88. return (struct page **)kmap_atomic(page, KM_USER0);
  89. }
  90. static inline void shmem_dir_unmap(struct page **dir)
  91. {
  92. kunmap_atomic(dir, KM_USER0);
  93. }
  94. static swp_entry_t *shmem_swp_map(struct page *page)
  95. {
  96. return (swp_entry_t *)kmap_atomic(page, KM_USER1);
  97. }
  98. static inline void shmem_swp_balance_unmap(void)
  99. {
  100. /*
  101. * When passing a pointer to an i_direct entry, to code which
  102. * also handles indirect entries and so will shmem_swp_unmap,
  103. * we must arrange for the preempt count to remain in balance.
  104. * What kmap_atomic of a lowmem page does depends on config
  105. * and architecture, so pretend to kmap_atomic some lowmem page.
  106. */
  107. (void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
  108. }
  109. static inline void shmem_swp_unmap(swp_entry_t *entry)
  110. {
  111. kunmap_atomic(entry, KM_USER1);
  112. }
  113. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  114. {
  115. return sb->s_fs_info;
  116. }
  117. /*
  118. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  119. * for shared memory and for shared anonymous (/dev/zero) mappings
  120. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  121. * consistent with the pre-accounting of private mappings ...
  122. */
  123. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  124. {
  125. return (flags & VM_ACCOUNT)?
  126. security_vm_enough_memory(VM_ACCT(size)): 0;
  127. }
  128. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  129. {
  130. if (flags & VM_ACCOUNT)
  131. vm_unacct_memory(VM_ACCT(size));
  132. }
  133. /*
  134. * ... whereas tmpfs objects are accounted incrementally as
  135. * pages are allocated, in order to allow huge sparse files.
  136. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  137. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  138. */
  139. static inline int shmem_acct_block(unsigned long flags)
  140. {
  141. return (flags & VM_ACCOUNT)?
  142. 0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
  143. }
  144. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  145. {
  146. if (!(flags & VM_ACCOUNT))
  147. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  148. }
  149. static struct super_operations shmem_ops;
  150. static struct address_space_operations shmem_aops;
  151. static struct file_operations shmem_file_operations;
  152. static struct inode_operations shmem_inode_operations;
  153. static struct inode_operations shmem_dir_inode_operations;
  154. static struct vm_operations_struct shmem_vm_ops;
  155. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  156. .ra_pages = 0, /* No readahead */
  157. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  158. .unplug_io_fn = default_unplug_io_fn,
  159. };
  160. static LIST_HEAD(shmem_swaplist);
  161. static DEFINE_SPINLOCK(shmem_swaplist_lock);
  162. static void shmem_free_blocks(struct inode *inode, long pages)
  163. {
  164. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  165. if (sbinfo->max_blocks) {
  166. spin_lock(&sbinfo->stat_lock);
  167. sbinfo->free_blocks += pages;
  168. inode->i_blocks -= pages*BLOCKS_PER_PAGE;
  169. spin_unlock(&sbinfo->stat_lock);
  170. }
  171. }
  172. /*
  173. * shmem_recalc_inode - recalculate the size of an inode
  174. *
  175. * @inode: inode to recalc
  176. *
  177. * We have to calculate the free blocks since the mm can drop
  178. * undirtied hole pages behind our back.
  179. *
  180. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  181. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  182. *
  183. * It has to be called with the spinlock held.
  184. */
  185. static void shmem_recalc_inode(struct inode *inode)
  186. {
  187. struct shmem_inode_info *info = SHMEM_I(inode);
  188. long freed;
  189. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  190. if (freed > 0) {
  191. info->alloced -= freed;
  192. shmem_unacct_blocks(info->flags, freed);
  193. shmem_free_blocks(inode, freed);
  194. }
  195. }
  196. /*
  197. * shmem_swp_entry - find the swap vector position in the info structure
  198. *
  199. * @info: info structure for the inode
  200. * @index: index of the page to find
  201. * @page: optional page to add to the structure. Has to be preset to
  202. * all zeros
  203. *
  204. * If there is no space allocated yet it will return NULL when
  205. * page is NULL, else it will use the page for the needed block,
  206. * setting it to NULL on return to indicate that it has been used.
  207. *
  208. * The swap vector is organized the following way:
  209. *
  210. * There are SHMEM_NR_DIRECT entries directly stored in the
  211. * shmem_inode_info structure. So small files do not need an addional
  212. * allocation.
  213. *
  214. * For pages with index > SHMEM_NR_DIRECT there is the pointer
  215. * i_indirect which points to a page which holds in the first half
  216. * doubly indirect blocks, in the second half triple indirect blocks:
  217. *
  218. * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
  219. * following layout (for SHMEM_NR_DIRECT == 16):
  220. *
  221. * i_indirect -> dir --> 16-19
  222. * | +-> 20-23
  223. * |
  224. * +-->dir2 --> 24-27
  225. * | +-> 28-31
  226. * | +-> 32-35
  227. * | +-> 36-39
  228. * |
  229. * +-->dir3 --> 40-43
  230. * +-> 44-47
  231. * +-> 48-51
  232. * +-> 52-55
  233. */
  234. static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
  235. {
  236. unsigned long offset;
  237. struct page **dir;
  238. struct page *subdir;
  239. if (index < SHMEM_NR_DIRECT) {
  240. shmem_swp_balance_unmap();
  241. return info->i_direct+index;
  242. }
  243. if (!info->i_indirect) {
  244. if (page) {
  245. info->i_indirect = *page;
  246. *page = NULL;
  247. }
  248. return NULL; /* need another page */
  249. }
  250. index -= SHMEM_NR_DIRECT;
  251. offset = index % ENTRIES_PER_PAGE;
  252. index /= ENTRIES_PER_PAGE;
  253. dir = shmem_dir_map(info->i_indirect);
  254. if (index >= ENTRIES_PER_PAGE/2) {
  255. index -= ENTRIES_PER_PAGE/2;
  256. dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
  257. index %= ENTRIES_PER_PAGE;
  258. subdir = *dir;
  259. if (!subdir) {
  260. if (page) {
  261. *dir = *page;
  262. *page = NULL;
  263. }
  264. shmem_dir_unmap(dir);
  265. return NULL; /* need another page */
  266. }
  267. shmem_dir_unmap(dir);
  268. dir = shmem_dir_map(subdir);
  269. }
  270. dir += index;
  271. subdir = *dir;
  272. if (!subdir) {
  273. if (!page || !(subdir = *page)) {
  274. shmem_dir_unmap(dir);
  275. return NULL; /* need a page */
  276. }
  277. *dir = subdir;
  278. *page = NULL;
  279. }
  280. shmem_dir_unmap(dir);
  281. return shmem_swp_map(subdir) + offset;
  282. }
  283. static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
  284. {
  285. long incdec = value? 1: -1;
  286. entry->val = value;
  287. info->swapped += incdec;
  288. if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
  289. struct page *page = kmap_atomic_to_page(entry);
  290. set_page_private(page, page_private(page) + incdec);
  291. }
  292. }
  293. /*
  294. * shmem_swp_alloc - get the position of the swap entry for the page.
  295. * If it does not exist allocate the entry.
  296. *
  297. * @info: info structure for the inode
  298. * @index: index of the page to find
  299. * @sgp: check and recheck i_size? skip allocation?
  300. */
  301. static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
  302. {
  303. struct inode *inode = &info->vfs_inode;
  304. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  305. struct page *page = NULL;
  306. swp_entry_t *entry;
  307. if (sgp != SGP_WRITE &&
  308. ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  309. return ERR_PTR(-EINVAL);
  310. while (!(entry = shmem_swp_entry(info, index, &page))) {
  311. if (sgp == SGP_READ)
  312. return shmem_swp_map(ZERO_PAGE(0));
  313. /*
  314. * Test free_blocks against 1 not 0, since we have 1 data
  315. * page (and perhaps indirect index pages) yet to allocate:
  316. * a waste to allocate index if we cannot allocate data.
  317. */
  318. if (sbinfo->max_blocks) {
  319. spin_lock(&sbinfo->stat_lock);
  320. if (sbinfo->free_blocks <= 1) {
  321. spin_unlock(&sbinfo->stat_lock);
  322. return ERR_PTR(-ENOSPC);
  323. }
  324. sbinfo->free_blocks--;
  325. inode->i_blocks += BLOCKS_PER_PAGE;
  326. spin_unlock(&sbinfo->stat_lock);
  327. }
  328. spin_unlock(&info->lock);
  329. page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping) | __GFP_ZERO);
  330. if (page)
  331. set_page_private(page, 0);
  332. spin_lock(&info->lock);
  333. if (!page) {
  334. shmem_free_blocks(inode, 1);
  335. return ERR_PTR(-ENOMEM);
  336. }
  337. if (sgp != SGP_WRITE &&
  338. ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  339. entry = ERR_PTR(-EINVAL);
  340. break;
  341. }
  342. if (info->next_index <= index)
  343. info->next_index = index + 1;
  344. }
  345. if (page) {
  346. /* another task gave its page, or truncated the file */
  347. shmem_free_blocks(inode, 1);
  348. shmem_dir_free(page);
  349. }
  350. if (info->next_index <= index && !IS_ERR(entry))
  351. info->next_index = index + 1;
  352. return entry;
  353. }
  354. /*
  355. * shmem_free_swp - free some swap entries in a directory
  356. *
  357. * @dir: pointer to the directory
  358. * @edir: pointer after last entry of the directory
  359. */
  360. static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir)
  361. {
  362. swp_entry_t *ptr;
  363. int freed = 0;
  364. for (ptr = dir; ptr < edir; ptr++) {
  365. if (ptr->val) {
  366. free_swap_and_cache(*ptr);
  367. *ptr = (swp_entry_t){0};
  368. freed++;
  369. }
  370. }
  371. return freed;
  372. }
  373. static int shmem_map_and_free_swp(struct page *subdir,
  374. int offset, int limit, struct page ***dir)
  375. {
  376. swp_entry_t *ptr;
  377. int freed = 0;
  378. ptr = shmem_swp_map(subdir);
  379. for (; offset < limit; offset += LATENCY_LIMIT) {
  380. int size = limit - offset;
  381. if (size > LATENCY_LIMIT)
  382. size = LATENCY_LIMIT;
  383. freed += shmem_free_swp(ptr+offset, ptr+offset+size);
  384. if (need_resched()) {
  385. shmem_swp_unmap(ptr);
  386. if (*dir) {
  387. shmem_dir_unmap(*dir);
  388. *dir = NULL;
  389. }
  390. cond_resched();
  391. ptr = shmem_swp_map(subdir);
  392. }
  393. }
  394. shmem_swp_unmap(ptr);
  395. return freed;
  396. }
  397. static void shmem_free_pages(struct list_head *next)
  398. {
  399. struct page *page;
  400. int freed = 0;
  401. do {
  402. page = container_of(next, struct page, lru);
  403. next = next->next;
  404. shmem_dir_free(page);
  405. freed++;
  406. if (freed >= LATENCY_LIMIT) {
  407. cond_resched();
  408. freed = 0;
  409. }
  410. } while (next);
  411. }
  412. static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
  413. {
  414. struct shmem_inode_info *info = SHMEM_I(inode);
  415. unsigned long idx;
  416. unsigned long size;
  417. unsigned long limit;
  418. unsigned long stage;
  419. unsigned long diroff;
  420. struct page **dir;
  421. struct page *topdir;
  422. struct page *middir;
  423. struct page *subdir;
  424. swp_entry_t *ptr;
  425. LIST_HEAD(pages_to_free);
  426. long nr_pages_to_free = 0;
  427. long nr_swaps_freed = 0;
  428. int offset;
  429. int freed;
  430. int punch_hole = 0;
  431. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  432. idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  433. if (idx >= info->next_index)
  434. return;
  435. spin_lock(&info->lock);
  436. info->flags |= SHMEM_TRUNCATE;
  437. if (likely(end == (loff_t) -1)) {
  438. limit = info->next_index;
  439. info->next_index = idx;
  440. } else {
  441. limit = (end + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  442. if (limit > info->next_index)
  443. limit = info->next_index;
  444. punch_hole = 1;
  445. }
  446. topdir = info->i_indirect;
  447. if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
  448. info->i_indirect = NULL;
  449. nr_pages_to_free++;
  450. list_add(&topdir->lru, &pages_to_free);
  451. }
  452. spin_unlock(&info->lock);
  453. if (info->swapped && idx < SHMEM_NR_DIRECT) {
  454. ptr = info->i_direct;
  455. size = limit;
  456. if (size > SHMEM_NR_DIRECT)
  457. size = SHMEM_NR_DIRECT;
  458. nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size);
  459. }
  460. if (!topdir)
  461. goto done2;
  462. BUG_ON(limit <= SHMEM_NR_DIRECT);
  463. limit -= SHMEM_NR_DIRECT;
  464. idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
  465. offset = idx % ENTRIES_PER_PAGE;
  466. idx -= offset;
  467. dir = shmem_dir_map(topdir);
  468. stage = ENTRIES_PER_PAGEPAGE/2;
  469. if (idx < ENTRIES_PER_PAGEPAGE/2) {
  470. middir = topdir;
  471. diroff = idx/ENTRIES_PER_PAGE;
  472. } else {
  473. dir += ENTRIES_PER_PAGE/2;
  474. dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
  475. while (stage <= idx)
  476. stage += ENTRIES_PER_PAGEPAGE;
  477. middir = *dir;
  478. if (*dir) {
  479. diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
  480. ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
  481. if (!diroff && !offset) {
  482. *dir = NULL;
  483. nr_pages_to_free++;
  484. list_add(&middir->lru, &pages_to_free);
  485. }
  486. shmem_dir_unmap(dir);
  487. dir = shmem_dir_map(middir);
  488. } else {
  489. diroff = 0;
  490. offset = 0;
  491. idx = stage;
  492. }
  493. }
  494. for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
  495. if (unlikely(idx == stage)) {
  496. shmem_dir_unmap(dir);
  497. dir = shmem_dir_map(topdir) +
  498. ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
  499. while (!*dir) {
  500. dir++;
  501. idx += ENTRIES_PER_PAGEPAGE;
  502. if (idx >= limit)
  503. goto done1;
  504. }
  505. stage = idx + ENTRIES_PER_PAGEPAGE;
  506. middir = *dir;
  507. *dir = NULL;
  508. nr_pages_to_free++;
  509. list_add(&middir->lru, &pages_to_free);
  510. shmem_dir_unmap(dir);
  511. cond_resched();
  512. dir = shmem_dir_map(middir);
  513. diroff = 0;
  514. }
  515. subdir = dir[diroff];
  516. if (subdir && page_private(subdir)) {
  517. size = limit - idx;
  518. if (size > ENTRIES_PER_PAGE)
  519. size = ENTRIES_PER_PAGE;
  520. freed = shmem_map_and_free_swp(subdir,
  521. offset, size, &dir);
  522. if (!dir)
  523. dir = shmem_dir_map(middir);
  524. nr_swaps_freed += freed;
  525. if (offset)
  526. spin_lock(&info->lock);
  527. set_page_private(subdir, page_private(subdir) - freed);
  528. if (offset)
  529. spin_unlock(&info->lock);
  530. if (!punch_hole)
  531. BUG_ON(page_private(subdir) > offset);
  532. }
  533. if (offset)
  534. offset = 0;
  535. else if (subdir && !page_private(subdir)) {
  536. dir[diroff] = NULL;
  537. nr_pages_to_free++;
  538. list_add(&subdir->lru, &pages_to_free);
  539. }
  540. }
  541. done1:
  542. shmem_dir_unmap(dir);
  543. done2:
  544. if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
  545. /*
  546. * Call truncate_inode_pages again: racing shmem_unuse_inode
  547. * may have swizzled a page in from swap since vmtruncate or
  548. * generic_delete_inode did it, before we lowered next_index.
  549. * Also, though shmem_getpage checks i_size before adding to
  550. * cache, no recheck after: so fix the narrow window there too.
  551. */
  552. truncate_inode_pages_range(inode->i_mapping, start, end);
  553. }
  554. spin_lock(&info->lock);
  555. info->flags &= ~SHMEM_TRUNCATE;
  556. info->swapped -= nr_swaps_freed;
  557. if (nr_pages_to_free)
  558. shmem_free_blocks(inode, nr_pages_to_free);
  559. shmem_recalc_inode(inode);
  560. spin_unlock(&info->lock);
  561. /*
  562. * Empty swap vector directory pages to be freed?
  563. */
  564. if (!list_empty(&pages_to_free)) {
  565. pages_to_free.prev->next = NULL;
  566. shmem_free_pages(pages_to_free.next);
  567. }
  568. }
  569. static void shmem_truncate(struct inode *inode)
  570. {
  571. shmem_truncate_range(inode, inode->i_size, (loff_t)-1);
  572. }
  573. static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
  574. {
  575. struct inode *inode = dentry->d_inode;
  576. struct page *page = NULL;
  577. int error;
  578. if (attr->ia_valid & ATTR_SIZE) {
  579. if (attr->ia_size < inode->i_size) {
  580. /*
  581. * If truncating down to a partial page, then
  582. * if that page is already allocated, hold it
  583. * in memory until the truncation is over, so
  584. * truncate_partial_page cannnot miss it were
  585. * it assigned to swap.
  586. */
  587. if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
  588. (void) shmem_getpage(inode,
  589. attr->ia_size>>PAGE_CACHE_SHIFT,
  590. &page, SGP_READ, NULL);
  591. }
  592. /*
  593. * Reset SHMEM_PAGEIN flag so that shmem_truncate can
  594. * detect if any pages might have been added to cache
  595. * after truncate_inode_pages. But we needn't bother
  596. * if it's being fully truncated to zero-length: the
  597. * nrpages check is efficient enough in that case.
  598. */
  599. if (attr->ia_size) {
  600. struct shmem_inode_info *info = SHMEM_I(inode);
  601. spin_lock(&info->lock);
  602. info->flags &= ~SHMEM_PAGEIN;
  603. spin_unlock(&info->lock);
  604. }
  605. }
  606. }
  607. error = inode_change_ok(inode, attr);
  608. if (!error)
  609. error = inode_setattr(inode, attr);
  610. if (page)
  611. page_cache_release(page);
  612. return error;
  613. }
  614. static void shmem_delete_inode(struct inode *inode)
  615. {
  616. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  617. struct shmem_inode_info *info = SHMEM_I(inode);
  618. if (inode->i_op->truncate == shmem_truncate) {
  619. truncate_inode_pages(inode->i_mapping, 0);
  620. shmem_unacct_size(info->flags, inode->i_size);
  621. inode->i_size = 0;
  622. shmem_truncate(inode);
  623. if (!list_empty(&info->swaplist)) {
  624. spin_lock(&shmem_swaplist_lock);
  625. list_del_init(&info->swaplist);
  626. spin_unlock(&shmem_swaplist_lock);
  627. }
  628. }
  629. BUG_ON(inode->i_blocks);
  630. if (sbinfo->max_inodes) {
  631. spin_lock(&sbinfo->stat_lock);
  632. sbinfo->free_inodes++;
  633. spin_unlock(&sbinfo->stat_lock);
  634. }
  635. clear_inode(inode);
  636. }
  637. static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
  638. {
  639. swp_entry_t *ptr;
  640. for (ptr = dir; ptr < edir; ptr++) {
  641. if (ptr->val == entry.val)
  642. return ptr - dir;
  643. }
  644. return -1;
  645. }
  646. static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
  647. {
  648. struct inode *inode;
  649. unsigned long idx;
  650. unsigned long size;
  651. unsigned long limit;
  652. unsigned long stage;
  653. struct page **dir;
  654. struct page *subdir;
  655. swp_entry_t *ptr;
  656. int offset;
  657. idx = 0;
  658. ptr = info->i_direct;
  659. spin_lock(&info->lock);
  660. limit = info->next_index;
  661. size = limit;
  662. if (size > SHMEM_NR_DIRECT)
  663. size = SHMEM_NR_DIRECT;
  664. offset = shmem_find_swp(entry, ptr, ptr+size);
  665. if (offset >= 0) {
  666. shmem_swp_balance_unmap();
  667. goto found;
  668. }
  669. if (!info->i_indirect)
  670. goto lost2;
  671. dir = shmem_dir_map(info->i_indirect);
  672. stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
  673. for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
  674. if (unlikely(idx == stage)) {
  675. shmem_dir_unmap(dir-1);
  676. dir = shmem_dir_map(info->i_indirect) +
  677. ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
  678. while (!*dir) {
  679. dir++;
  680. idx += ENTRIES_PER_PAGEPAGE;
  681. if (idx >= limit)
  682. goto lost1;
  683. }
  684. stage = idx + ENTRIES_PER_PAGEPAGE;
  685. subdir = *dir;
  686. shmem_dir_unmap(dir);
  687. dir = shmem_dir_map(subdir);
  688. }
  689. subdir = *dir;
  690. if (subdir && page_private(subdir)) {
  691. ptr = shmem_swp_map(subdir);
  692. size = limit - idx;
  693. if (size > ENTRIES_PER_PAGE)
  694. size = ENTRIES_PER_PAGE;
  695. offset = shmem_find_swp(entry, ptr, ptr+size);
  696. if (offset >= 0) {
  697. shmem_dir_unmap(dir);
  698. goto found;
  699. }
  700. shmem_swp_unmap(ptr);
  701. }
  702. }
  703. lost1:
  704. shmem_dir_unmap(dir-1);
  705. lost2:
  706. spin_unlock(&info->lock);
  707. return 0;
  708. found:
  709. idx += offset;
  710. inode = &info->vfs_inode;
  711. if (move_from_swap_cache(page, idx, inode->i_mapping) == 0) {
  712. info->flags |= SHMEM_PAGEIN;
  713. shmem_swp_set(info, ptr + offset, 0);
  714. }
  715. shmem_swp_unmap(ptr);
  716. spin_unlock(&info->lock);
  717. /*
  718. * Decrement swap count even when the entry is left behind:
  719. * try_to_unuse will skip over mms, then reincrement count.
  720. */
  721. swap_free(entry);
  722. return 1;
  723. }
  724. /*
  725. * shmem_unuse() search for an eventually swapped out shmem page.
  726. */
  727. int shmem_unuse(swp_entry_t entry, struct page *page)
  728. {
  729. struct list_head *p, *next;
  730. struct shmem_inode_info *info;
  731. int found = 0;
  732. spin_lock(&shmem_swaplist_lock);
  733. list_for_each_safe(p, next, &shmem_swaplist) {
  734. info = list_entry(p, struct shmem_inode_info, swaplist);
  735. if (!info->swapped)
  736. list_del_init(&info->swaplist);
  737. else if (shmem_unuse_inode(info, entry, page)) {
  738. /* move head to start search for next from here */
  739. list_move_tail(&shmem_swaplist, &info->swaplist);
  740. found = 1;
  741. break;
  742. }
  743. }
  744. spin_unlock(&shmem_swaplist_lock);
  745. return found;
  746. }
  747. /*
  748. * Move the page from the page cache to the swap cache.
  749. */
  750. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  751. {
  752. struct shmem_inode_info *info;
  753. swp_entry_t *entry, swap;
  754. struct address_space *mapping;
  755. unsigned long index;
  756. struct inode *inode;
  757. BUG_ON(!PageLocked(page));
  758. BUG_ON(page_mapped(page));
  759. mapping = page->mapping;
  760. index = page->index;
  761. inode = mapping->host;
  762. info = SHMEM_I(inode);
  763. if (info->flags & VM_LOCKED)
  764. goto redirty;
  765. swap = get_swap_page();
  766. if (!swap.val)
  767. goto redirty;
  768. spin_lock(&info->lock);
  769. shmem_recalc_inode(inode);
  770. if (index >= info->next_index) {
  771. BUG_ON(!(info->flags & SHMEM_TRUNCATE));
  772. goto unlock;
  773. }
  774. entry = shmem_swp_entry(info, index, NULL);
  775. BUG_ON(!entry);
  776. BUG_ON(entry->val);
  777. if (move_to_swap_cache(page, swap) == 0) {
  778. shmem_swp_set(info, entry, swap.val);
  779. shmem_swp_unmap(entry);
  780. spin_unlock(&info->lock);
  781. if (list_empty(&info->swaplist)) {
  782. spin_lock(&shmem_swaplist_lock);
  783. /* move instead of add in case we're racing */
  784. list_move_tail(&info->swaplist, &shmem_swaplist);
  785. spin_unlock(&shmem_swaplist_lock);
  786. }
  787. unlock_page(page);
  788. return 0;
  789. }
  790. shmem_swp_unmap(entry);
  791. unlock:
  792. spin_unlock(&info->lock);
  793. swap_free(swap);
  794. redirty:
  795. set_page_dirty(page);
  796. return AOP_WRITEPAGE_ACTIVATE; /* Return with the page locked */
  797. }
  798. #ifdef CONFIG_NUMA
  799. static struct page *shmem_swapin_async(struct shared_policy *p,
  800. swp_entry_t entry, unsigned long idx)
  801. {
  802. struct page *page;
  803. struct vm_area_struct pvma;
  804. /* Create a pseudo vma that just contains the policy */
  805. memset(&pvma, 0, sizeof(struct vm_area_struct));
  806. pvma.vm_end = PAGE_SIZE;
  807. pvma.vm_pgoff = idx;
  808. pvma.vm_policy = mpol_shared_policy_lookup(p, idx);
  809. page = read_swap_cache_async(entry, &pvma, 0);
  810. mpol_free(pvma.vm_policy);
  811. return page;
  812. }
  813. struct page *shmem_swapin(struct shmem_inode_info *info, swp_entry_t entry,
  814. unsigned long idx)
  815. {
  816. struct shared_policy *p = &info->policy;
  817. int i, num;
  818. struct page *page;
  819. unsigned long offset;
  820. num = valid_swaphandles(entry, &offset);
  821. for (i = 0; i < num; offset++, i++) {
  822. page = shmem_swapin_async(p,
  823. swp_entry(swp_type(entry), offset), idx);
  824. if (!page)
  825. break;
  826. page_cache_release(page);
  827. }
  828. lru_add_drain(); /* Push any new pages onto the LRU now */
  829. return shmem_swapin_async(p, entry, idx);
  830. }
  831. static struct page *
  832. shmem_alloc_page(gfp_t gfp, struct shmem_inode_info *info,
  833. unsigned long idx)
  834. {
  835. struct vm_area_struct pvma;
  836. struct page *page;
  837. memset(&pvma, 0, sizeof(struct vm_area_struct));
  838. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
  839. pvma.vm_pgoff = idx;
  840. pvma.vm_end = PAGE_SIZE;
  841. page = alloc_page_vma(gfp | __GFP_ZERO, &pvma, 0);
  842. mpol_free(pvma.vm_policy);
  843. return page;
  844. }
  845. #else
  846. static inline struct page *
  847. shmem_swapin(struct shmem_inode_info *info,swp_entry_t entry,unsigned long idx)
  848. {
  849. swapin_readahead(entry, 0, NULL);
  850. return read_swap_cache_async(entry, NULL, 0);
  851. }
  852. static inline struct page *
  853. shmem_alloc_page(gfp_t gfp,struct shmem_inode_info *info, unsigned long idx)
  854. {
  855. return alloc_page(gfp | __GFP_ZERO);
  856. }
  857. #endif
  858. /*
  859. * shmem_getpage - either get the page from swap or allocate a new one
  860. *
  861. * If we allocate a new one we do not mark it dirty. That's up to the
  862. * vm. If we swap it in we mark it dirty since we also free the swap
  863. * entry since a page cannot live in both the swap and page cache
  864. */
  865. static int shmem_getpage(struct inode *inode, unsigned long idx,
  866. struct page **pagep, enum sgp_type sgp, int *type)
  867. {
  868. struct address_space *mapping = inode->i_mapping;
  869. struct shmem_inode_info *info = SHMEM_I(inode);
  870. struct shmem_sb_info *sbinfo;
  871. struct page *filepage = *pagep;
  872. struct page *swappage;
  873. swp_entry_t *entry;
  874. swp_entry_t swap;
  875. int error;
  876. if (idx >= SHMEM_MAX_INDEX)
  877. return -EFBIG;
  878. /*
  879. * Normally, filepage is NULL on entry, and either found
  880. * uptodate immediately, or allocated and zeroed, or read
  881. * in under swappage, which is then assigned to filepage.
  882. * But shmem_prepare_write passes in a locked filepage,
  883. * which may be found not uptodate by other callers too,
  884. * and may need to be copied from the swappage read in.
  885. */
  886. repeat:
  887. if (!filepage)
  888. filepage = find_lock_page(mapping, idx);
  889. if (filepage && PageUptodate(filepage))
  890. goto done;
  891. error = 0;
  892. if (sgp == SGP_QUICK)
  893. goto failed;
  894. spin_lock(&info->lock);
  895. shmem_recalc_inode(inode);
  896. entry = shmem_swp_alloc(info, idx, sgp);
  897. if (IS_ERR(entry)) {
  898. spin_unlock(&info->lock);
  899. error = PTR_ERR(entry);
  900. goto failed;
  901. }
  902. swap = *entry;
  903. if (swap.val) {
  904. /* Look it up and read it in.. */
  905. swappage = lookup_swap_cache(swap);
  906. if (!swappage) {
  907. shmem_swp_unmap(entry);
  908. spin_unlock(&info->lock);
  909. /* here we actually do the io */
  910. if (type && *type == VM_FAULT_MINOR) {
  911. inc_page_state(pgmajfault);
  912. *type = VM_FAULT_MAJOR;
  913. }
  914. swappage = shmem_swapin(info, swap, idx);
  915. if (!swappage) {
  916. spin_lock(&info->lock);
  917. entry = shmem_swp_alloc(info, idx, sgp);
  918. if (IS_ERR(entry))
  919. error = PTR_ERR(entry);
  920. else {
  921. if (entry->val == swap.val)
  922. error = -ENOMEM;
  923. shmem_swp_unmap(entry);
  924. }
  925. spin_unlock(&info->lock);
  926. if (error)
  927. goto failed;
  928. goto repeat;
  929. }
  930. wait_on_page_locked(swappage);
  931. page_cache_release(swappage);
  932. goto repeat;
  933. }
  934. /* We have to do this with page locked to prevent races */
  935. if (TestSetPageLocked(swappage)) {
  936. shmem_swp_unmap(entry);
  937. spin_unlock(&info->lock);
  938. wait_on_page_locked(swappage);
  939. page_cache_release(swappage);
  940. goto repeat;
  941. }
  942. if (!PageSwapCache(swappage)) {
  943. /* Page migration has occured */
  944. shmem_swp_unmap(entry);
  945. spin_unlock(&info->lock);
  946. unlock_page(swappage);
  947. page_cache_release(swappage);
  948. goto repeat;
  949. }
  950. if (PageWriteback(swappage)) {
  951. shmem_swp_unmap(entry);
  952. spin_unlock(&info->lock);
  953. wait_on_page_writeback(swappage);
  954. unlock_page(swappage);
  955. page_cache_release(swappage);
  956. goto repeat;
  957. }
  958. if (!PageUptodate(swappage)) {
  959. shmem_swp_unmap(entry);
  960. spin_unlock(&info->lock);
  961. unlock_page(swappage);
  962. page_cache_release(swappage);
  963. error = -EIO;
  964. goto failed;
  965. }
  966. if (filepage) {
  967. shmem_swp_set(info, entry, 0);
  968. shmem_swp_unmap(entry);
  969. delete_from_swap_cache(swappage);
  970. spin_unlock(&info->lock);
  971. copy_highpage(filepage, swappage);
  972. unlock_page(swappage);
  973. page_cache_release(swappage);
  974. flush_dcache_page(filepage);
  975. SetPageUptodate(filepage);
  976. set_page_dirty(filepage);
  977. swap_free(swap);
  978. } else if (!(error = move_from_swap_cache(
  979. swappage, idx, mapping))) {
  980. info->flags |= SHMEM_PAGEIN;
  981. shmem_swp_set(info, entry, 0);
  982. shmem_swp_unmap(entry);
  983. spin_unlock(&info->lock);
  984. filepage = swappage;
  985. swap_free(swap);
  986. } else {
  987. shmem_swp_unmap(entry);
  988. spin_unlock(&info->lock);
  989. unlock_page(swappage);
  990. page_cache_release(swappage);
  991. if (error == -ENOMEM) {
  992. /* let kswapd refresh zone for GFP_ATOMICs */
  993. blk_congestion_wait(WRITE, HZ/50);
  994. }
  995. goto repeat;
  996. }
  997. } else if (sgp == SGP_READ && !filepage) {
  998. shmem_swp_unmap(entry);
  999. filepage = find_get_page(mapping, idx);
  1000. if (filepage &&
  1001. (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
  1002. spin_unlock(&info->lock);
  1003. wait_on_page_locked(filepage);
  1004. page_cache_release(filepage);
  1005. filepage = NULL;
  1006. goto repeat;
  1007. }
  1008. spin_unlock(&info->lock);
  1009. } else {
  1010. shmem_swp_unmap(entry);
  1011. sbinfo = SHMEM_SB(inode->i_sb);
  1012. if (sbinfo->max_blocks) {
  1013. spin_lock(&sbinfo->stat_lock);
  1014. if (sbinfo->free_blocks == 0 ||
  1015. shmem_acct_block(info->flags)) {
  1016. spin_unlock(&sbinfo->stat_lock);
  1017. spin_unlock(&info->lock);
  1018. error = -ENOSPC;
  1019. goto failed;
  1020. }
  1021. sbinfo->free_blocks--;
  1022. inode->i_blocks += BLOCKS_PER_PAGE;
  1023. spin_unlock(&sbinfo->stat_lock);
  1024. } else if (shmem_acct_block(info->flags)) {
  1025. spin_unlock(&info->lock);
  1026. error = -ENOSPC;
  1027. goto failed;
  1028. }
  1029. if (!filepage) {
  1030. spin_unlock(&info->lock);
  1031. filepage = shmem_alloc_page(mapping_gfp_mask(mapping),
  1032. info,
  1033. idx);
  1034. if (!filepage) {
  1035. shmem_unacct_blocks(info->flags, 1);
  1036. shmem_free_blocks(inode, 1);
  1037. error = -ENOMEM;
  1038. goto failed;
  1039. }
  1040. spin_lock(&info->lock);
  1041. entry = shmem_swp_alloc(info, idx, sgp);
  1042. if (IS_ERR(entry))
  1043. error = PTR_ERR(entry);
  1044. else {
  1045. swap = *entry;
  1046. shmem_swp_unmap(entry);
  1047. }
  1048. if (error || swap.val || 0 != add_to_page_cache_lru(
  1049. filepage, mapping, idx, GFP_ATOMIC)) {
  1050. spin_unlock(&info->lock);
  1051. page_cache_release(filepage);
  1052. shmem_unacct_blocks(info->flags, 1);
  1053. shmem_free_blocks(inode, 1);
  1054. filepage = NULL;
  1055. if (error)
  1056. goto failed;
  1057. goto repeat;
  1058. }
  1059. info->flags |= SHMEM_PAGEIN;
  1060. }
  1061. info->alloced++;
  1062. spin_unlock(&info->lock);
  1063. flush_dcache_page(filepage);
  1064. SetPageUptodate(filepage);
  1065. }
  1066. done:
  1067. if (*pagep != filepage) {
  1068. unlock_page(filepage);
  1069. *pagep = filepage;
  1070. }
  1071. return 0;
  1072. failed:
  1073. if (*pagep != filepage) {
  1074. unlock_page(filepage);
  1075. page_cache_release(filepage);
  1076. }
  1077. return error;
  1078. }
  1079. struct page *shmem_nopage(struct vm_area_struct *vma, unsigned long address, int *type)
  1080. {
  1081. struct inode *inode = vma->vm_file->f_dentry->d_inode;
  1082. struct page *page = NULL;
  1083. unsigned long idx;
  1084. int error;
  1085. idx = (address - vma->vm_start) >> PAGE_SHIFT;
  1086. idx += vma->vm_pgoff;
  1087. idx >>= PAGE_CACHE_SHIFT - PAGE_SHIFT;
  1088. if (((loff_t) idx << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  1089. return NOPAGE_SIGBUS;
  1090. error = shmem_getpage(inode, idx, &page, SGP_CACHE, type);
  1091. if (error)
  1092. return (error == -ENOMEM)? NOPAGE_OOM: NOPAGE_SIGBUS;
  1093. mark_page_accessed(page);
  1094. return page;
  1095. }
  1096. static int shmem_populate(struct vm_area_struct *vma,
  1097. unsigned long addr, unsigned long len,
  1098. pgprot_t prot, unsigned long pgoff, int nonblock)
  1099. {
  1100. struct inode *inode = vma->vm_file->f_dentry->d_inode;
  1101. struct mm_struct *mm = vma->vm_mm;
  1102. enum sgp_type sgp = nonblock? SGP_QUICK: SGP_CACHE;
  1103. unsigned long size;
  1104. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1105. if (pgoff >= size || pgoff + (len >> PAGE_SHIFT) > size)
  1106. return -EINVAL;
  1107. while ((long) len > 0) {
  1108. struct page *page = NULL;
  1109. int err;
  1110. /*
  1111. * Will need changing if PAGE_CACHE_SIZE != PAGE_SIZE
  1112. */
  1113. err = shmem_getpage(inode, pgoff, &page, sgp, NULL);
  1114. if (err)
  1115. return err;
  1116. /* Page may still be null, but only if nonblock was set. */
  1117. if (page) {
  1118. mark_page_accessed(page);
  1119. err = install_page(mm, vma, addr, page, prot);
  1120. if (err) {
  1121. page_cache_release(page);
  1122. return err;
  1123. }
  1124. } else if (vma->vm_flags & VM_NONLINEAR) {
  1125. /* No page was found just because we can't read it in
  1126. * now (being here implies nonblock != 0), but the page
  1127. * may exist, so set the PTE to fault it in later. */
  1128. err = install_file_pte(mm, vma, addr, pgoff, prot);
  1129. if (err)
  1130. return err;
  1131. }
  1132. len -= PAGE_SIZE;
  1133. addr += PAGE_SIZE;
  1134. pgoff++;
  1135. }
  1136. return 0;
  1137. }
  1138. #ifdef CONFIG_NUMA
  1139. int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  1140. {
  1141. struct inode *i = vma->vm_file->f_dentry->d_inode;
  1142. return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
  1143. }
  1144. struct mempolicy *
  1145. shmem_get_policy(struct vm_area_struct *vma, unsigned long addr)
  1146. {
  1147. struct inode *i = vma->vm_file->f_dentry->d_inode;
  1148. unsigned long idx;
  1149. idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1150. return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
  1151. }
  1152. #endif
  1153. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1154. {
  1155. struct inode *inode = file->f_dentry->d_inode;
  1156. struct shmem_inode_info *info = SHMEM_I(inode);
  1157. int retval = -ENOMEM;
  1158. spin_lock(&info->lock);
  1159. if (lock && !(info->flags & VM_LOCKED)) {
  1160. if (!user_shm_lock(inode->i_size, user))
  1161. goto out_nomem;
  1162. info->flags |= VM_LOCKED;
  1163. }
  1164. if (!lock && (info->flags & VM_LOCKED) && user) {
  1165. user_shm_unlock(inode->i_size, user);
  1166. info->flags &= ~VM_LOCKED;
  1167. }
  1168. retval = 0;
  1169. out_nomem:
  1170. spin_unlock(&info->lock);
  1171. return retval;
  1172. }
  1173. int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1174. {
  1175. file_accessed(file);
  1176. vma->vm_ops = &shmem_vm_ops;
  1177. return 0;
  1178. }
  1179. static struct inode *
  1180. shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
  1181. {
  1182. struct inode *inode;
  1183. struct shmem_inode_info *info;
  1184. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1185. if (sbinfo->max_inodes) {
  1186. spin_lock(&sbinfo->stat_lock);
  1187. if (!sbinfo->free_inodes) {
  1188. spin_unlock(&sbinfo->stat_lock);
  1189. return NULL;
  1190. }
  1191. sbinfo->free_inodes--;
  1192. spin_unlock(&sbinfo->stat_lock);
  1193. }
  1194. inode = new_inode(sb);
  1195. if (inode) {
  1196. inode->i_mode = mode;
  1197. inode->i_uid = current->fsuid;
  1198. inode->i_gid = current->fsgid;
  1199. inode->i_blksize = PAGE_CACHE_SIZE;
  1200. inode->i_blocks = 0;
  1201. inode->i_mapping->a_ops = &shmem_aops;
  1202. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1203. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1204. info = SHMEM_I(inode);
  1205. memset(info, 0, (char *)inode - (char *)info);
  1206. spin_lock_init(&info->lock);
  1207. INIT_LIST_HEAD(&info->swaplist);
  1208. switch (mode & S_IFMT) {
  1209. default:
  1210. init_special_inode(inode, mode, dev);
  1211. break;
  1212. case S_IFREG:
  1213. inode->i_op = &shmem_inode_operations;
  1214. inode->i_fop = &shmem_file_operations;
  1215. mpol_shared_policy_init(&info->policy, sbinfo->policy,
  1216. &sbinfo->policy_nodes);
  1217. break;
  1218. case S_IFDIR:
  1219. inode->i_nlink++;
  1220. /* Some things misbehave if size == 0 on a directory */
  1221. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1222. inode->i_op = &shmem_dir_inode_operations;
  1223. inode->i_fop = &simple_dir_operations;
  1224. break;
  1225. case S_IFLNK:
  1226. /*
  1227. * Must not load anything in the rbtree,
  1228. * mpol_free_shared_policy will not be called.
  1229. */
  1230. mpol_shared_policy_init(&info->policy, MPOL_DEFAULT,
  1231. NULL);
  1232. break;
  1233. }
  1234. } else if (sbinfo->max_inodes) {
  1235. spin_lock(&sbinfo->stat_lock);
  1236. sbinfo->free_inodes++;
  1237. spin_unlock(&sbinfo->stat_lock);
  1238. }
  1239. return inode;
  1240. }
  1241. #ifdef CONFIG_TMPFS
  1242. static struct inode_operations shmem_symlink_inode_operations;
  1243. static struct inode_operations shmem_symlink_inline_operations;
  1244. /*
  1245. * Normally tmpfs makes no use of shmem_prepare_write, but it
  1246. * lets a tmpfs file be used read-write below the loop driver.
  1247. */
  1248. static int
  1249. shmem_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  1250. {
  1251. struct inode *inode = page->mapping->host;
  1252. return shmem_getpage(inode, page->index, &page, SGP_WRITE, NULL);
  1253. }
  1254. static ssize_t
  1255. shmem_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  1256. {
  1257. struct inode *inode = file->f_dentry->d_inode;
  1258. loff_t pos;
  1259. unsigned long written;
  1260. ssize_t err;
  1261. if ((ssize_t) count < 0)
  1262. return -EINVAL;
  1263. if (!access_ok(VERIFY_READ, buf, count))
  1264. return -EFAULT;
  1265. mutex_lock(&inode->i_mutex);
  1266. pos = *ppos;
  1267. written = 0;
  1268. err = generic_write_checks(file, &pos, &count, 0);
  1269. if (err || !count)
  1270. goto out;
  1271. err = remove_suid(file->f_dentry);
  1272. if (err)
  1273. goto out;
  1274. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1275. do {
  1276. struct page *page = NULL;
  1277. unsigned long bytes, index, offset;
  1278. char *kaddr;
  1279. int left;
  1280. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  1281. index = pos >> PAGE_CACHE_SHIFT;
  1282. bytes = PAGE_CACHE_SIZE - offset;
  1283. if (bytes > count)
  1284. bytes = count;
  1285. /*
  1286. * We don't hold page lock across copy from user -
  1287. * what would it guard against? - so no deadlock here.
  1288. * But it still may be a good idea to prefault below.
  1289. */
  1290. err = shmem_getpage(inode, index, &page, SGP_WRITE, NULL);
  1291. if (err)
  1292. break;
  1293. left = bytes;
  1294. if (PageHighMem(page)) {
  1295. volatile unsigned char dummy;
  1296. __get_user(dummy, buf);
  1297. __get_user(dummy, buf + bytes - 1);
  1298. kaddr = kmap_atomic(page, KM_USER0);
  1299. left = __copy_from_user_inatomic(kaddr + offset,
  1300. buf, bytes);
  1301. kunmap_atomic(kaddr, KM_USER0);
  1302. }
  1303. if (left) {
  1304. kaddr = kmap(page);
  1305. left = __copy_from_user(kaddr + offset, buf, bytes);
  1306. kunmap(page);
  1307. }
  1308. written += bytes;
  1309. count -= bytes;
  1310. pos += bytes;
  1311. buf += bytes;
  1312. if (pos > inode->i_size)
  1313. i_size_write(inode, pos);
  1314. flush_dcache_page(page);
  1315. set_page_dirty(page);
  1316. mark_page_accessed(page);
  1317. page_cache_release(page);
  1318. if (left) {
  1319. pos -= left;
  1320. written -= left;
  1321. err = -EFAULT;
  1322. break;
  1323. }
  1324. /*
  1325. * Our dirty pages are not counted in nr_dirty,
  1326. * and we do not attempt to balance dirty pages.
  1327. */
  1328. cond_resched();
  1329. } while (count);
  1330. *ppos = pos;
  1331. if (written)
  1332. err = written;
  1333. out:
  1334. mutex_unlock(&inode->i_mutex);
  1335. return err;
  1336. }
  1337. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1338. {
  1339. struct inode *inode = filp->f_dentry->d_inode;
  1340. struct address_space *mapping = inode->i_mapping;
  1341. unsigned long index, offset;
  1342. index = *ppos >> PAGE_CACHE_SHIFT;
  1343. offset = *ppos & ~PAGE_CACHE_MASK;
  1344. for (;;) {
  1345. struct page *page = NULL;
  1346. unsigned long end_index, nr, ret;
  1347. loff_t i_size = i_size_read(inode);
  1348. end_index = i_size >> PAGE_CACHE_SHIFT;
  1349. if (index > end_index)
  1350. break;
  1351. if (index == end_index) {
  1352. nr = i_size & ~PAGE_CACHE_MASK;
  1353. if (nr <= offset)
  1354. break;
  1355. }
  1356. desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
  1357. if (desc->error) {
  1358. if (desc->error == -EINVAL)
  1359. desc->error = 0;
  1360. break;
  1361. }
  1362. /*
  1363. * We must evaluate after, since reads (unlike writes)
  1364. * are called without i_mutex protection against truncate
  1365. */
  1366. nr = PAGE_CACHE_SIZE;
  1367. i_size = i_size_read(inode);
  1368. end_index = i_size >> PAGE_CACHE_SHIFT;
  1369. if (index == end_index) {
  1370. nr = i_size & ~PAGE_CACHE_MASK;
  1371. if (nr <= offset) {
  1372. if (page)
  1373. page_cache_release(page);
  1374. break;
  1375. }
  1376. }
  1377. nr -= offset;
  1378. if (page) {
  1379. /*
  1380. * If users can be writing to this page using arbitrary
  1381. * virtual addresses, take care about potential aliasing
  1382. * before reading the page on the kernel side.
  1383. */
  1384. if (mapping_writably_mapped(mapping))
  1385. flush_dcache_page(page);
  1386. /*
  1387. * Mark the page accessed if we read the beginning.
  1388. */
  1389. if (!offset)
  1390. mark_page_accessed(page);
  1391. } else {
  1392. page = ZERO_PAGE(0);
  1393. page_cache_get(page);
  1394. }
  1395. /*
  1396. * Ok, we have the page, and it's up-to-date, so
  1397. * now we can copy it to user space...
  1398. *
  1399. * The actor routine returns how many bytes were actually used..
  1400. * NOTE! This may not be the same as how much of a user buffer
  1401. * we filled up (we may be padding etc), so we can only update
  1402. * "pos" here (the actor routine has to update the user buffer
  1403. * pointers and the remaining count).
  1404. */
  1405. ret = actor(desc, page, offset, nr);
  1406. offset += ret;
  1407. index += offset >> PAGE_CACHE_SHIFT;
  1408. offset &= ~PAGE_CACHE_MASK;
  1409. page_cache_release(page);
  1410. if (ret != nr || !desc->count)
  1411. break;
  1412. cond_resched();
  1413. }
  1414. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1415. file_accessed(filp);
  1416. }
  1417. static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  1418. {
  1419. read_descriptor_t desc;
  1420. if ((ssize_t) count < 0)
  1421. return -EINVAL;
  1422. if (!access_ok(VERIFY_WRITE, buf, count))
  1423. return -EFAULT;
  1424. if (!count)
  1425. return 0;
  1426. desc.written = 0;
  1427. desc.count = count;
  1428. desc.arg.buf = buf;
  1429. desc.error = 0;
  1430. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1431. if (desc.written)
  1432. return desc.written;
  1433. return desc.error;
  1434. }
  1435. static ssize_t shmem_file_sendfile(struct file *in_file, loff_t *ppos,
  1436. size_t count, read_actor_t actor, void *target)
  1437. {
  1438. read_descriptor_t desc;
  1439. if (!count)
  1440. return 0;
  1441. desc.written = 0;
  1442. desc.count = count;
  1443. desc.arg.data = target;
  1444. desc.error = 0;
  1445. do_shmem_file_read(in_file, ppos, &desc, actor);
  1446. if (desc.written)
  1447. return desc.written;
  1448. return desc.error;
  1449. }
  1450. static int shmem_statfs(struct super_block *sb, struct kstatfs *buf)
  1451. {
  1452. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1453. buf->f_type = TMPFS_MAGIC;
  1454. buf->f_bsize = PAGE_CACHE_SIZE;
  1455. buf->f_namelen = NAME_MAX;
  1456. spin_lock(&sbinfo->stat_lock);
  1457. if (sbinfo->max_blocks) {
  1458. buf->f_blocks = sbinfo->max_blocks;
  1459. buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
  1460. }
  1461. if (sbinfo->max_inodes) {
  1462. buf->f_files = sbinfo->max_inodes;
  1463. buf->f_ffree = sbinfo->free_inodes;
  1464. }
  1465. /* else leave those fields 0 like simple_statfs */
  1466. spin_unlock(&sbinfo->stat_lock);
  1467. return 0;
  1468. }
  1469. /*
  1470. * File creation. Allocate an inode, and we're done..
  1471. */
  1472. static int
  1473. shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1474. {
  1475. struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
  1476. int error = -ENOSPC;
  1477. if (inode) {
  1478. error = security_inode_init_security(inode, dir, NULL, NULL,
  1479. NULL);
  1480. if (error) {
  1481. if (error != -EOPNOTSUPP) {
  1482. iput(inode);
  1483. return error;
  1484. }
  1485. error = 0;
  1486. }
  1487. if (dir->i_mode & S_ISGID) {
  1488. inode->i_gid = dir->i_gid;
  1489. if (S_ISDIR(mode))
  1490. inode->i_mode |= S_ISGID;
  1491. }
  1492. dir->i_size += BOGO_DIRENT_SIZE;
  1493. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1494. d_instantiate(dentry, inode);
  1495. dget(dentry); /* Extra count - pin the dentry in core */
  1496. }
  1497. return error;
  1498. }
  1499. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1500. {
  1501. int error;
  1502. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1503. return error;
  1504. dir->i_nlink++;
  1505. return 0;
  1506. }
  1507. static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
  1508. struct nameidata *nd)
  1509. {
  1510. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1511. }
  1512. /*
  1513. * Link a file..
  1514. */
  1515. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1516. {
  1517. struct inode *inode = old_dentry->d_inode;
  1518. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1519. /*
  1520. * No ordinary (disk based) filesystem counts links as inodes;
  1521. * but each new link needs a new dentry, pinning lowmem, and
  1522. * tmpfs dentries cannot be pruned until they are unlinked.
  1523. */
  1524. if (sbinfo->max_inodes) {
  1525. spin_lock(&sbinfo->stat_lock);
  1526. if (!sbinfo->free_inodes) {
  1527. spin_unlock(&sbinfo->stat_lock);
  1528. return -ENOSPC;
  1529. }
  1530. sbinfo->free_inodes--;
  1531. spin_unlock(&sbinfo->stat_lock);
  1532. }
  1533. dir->i_size += BOGO_DIRENT_SIZE;
  1534. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1535. inode->i_nlink++;
  1536. atomic_inc(&inode->i_count); /* New dentry reference */
  1537. dget(dentry); /* Extra pinning count for the created dentry */
  1538. d_instantiate(dentry, inode);
  1539. return 0;
  1540. }
  1541. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1542. {
  1543. struct inode *inode = dentry->d_inode;
  1544. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) {
  1545. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1546. if (sbinfo->max_inodes) {
  1547. spin_lock(&sbinfo->stat_lock);
  1548. sbinfo->free_inodes++;
  1549. spin_unlock(&sbinfo->stat_lock);
  1550. }
  1551. }
  1552. dir->i_size -= BOGO_DIRENT_SIZE;
  1553. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1554. inode->i_nlink--;
  1555. dput(dentry); /* Undo the count from "create" - this does all the work */
  1556. return 0;
  1557. }
  1558. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1559. {
  1560. if (!simple_empty(dentry))
  1561. return -ENOTEMPTY;
  1562. dir->i_nlink--;
  1563. return shmem_unlink(dir, dentry);
  1564. }
  1565. /*
  1566. * The VFS layer already does all the dentry stuff for rename,
  1567. * we just have to decrement the usage count for the target if
  1568. * it exists so that the VFS layer correctly free's it when it
  1569. * gets overwritten.
  1570. */
  1571. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1572. {
  1573. struct inode *inode = old_dentry->d_inode;
  1574. int they_are_dirs = S_ISDIR(inode->i_mode);
  1575. if (!simple_empty(new_dentry))
  1576. return -ENOTEMPTY;
  1577. if (new_dentry->d_inode) {
  1578. (void) shmem_unlink(new_dir, new_dentry);
  1579. if (they_are_dirs)
  1580. old_dir->i_nlink--;
  1581. } else if (they_are_dirs) {
  1582. old_dir->i_nlink--;
  1583. new_dir->i_nlink++;
  1584. }
  1585. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1586. new_dir->i_size += BOGO_DIRENT_SIZE;
  1587. old_dir->i_ctime = old_dir->i_mtime =
  1588. new_dir->i_ctime = new_dir->i_mtime =
  1589. inode->i_ctime = CURRENT_TIME;
  1590. return 0;
  1591. }
  1592. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1593. {
  1594. int error;
  1595. int len;
  1596. struct inode *inode;
  1597. struct page *page = NULL;
  1598. char *kaddr;
  1599. struct shmem_inode_info *info;
  1600. len = strlen(symname) + 1;
  1601. if (len > PAGE_CACHE_SIZE)
  1602. return -ENAMETOOLONG;
  1603. inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
  1604. if (!inode)
  1605. return -ENOSPC;
  1606. error = security_inode_init_security(inode, dir, NULL, NULL,
  1607. NULL);
  1608. if (error) {
  1609. if (error != -EOPNOTSUPP) {
  1610. iput(inode);
  1611. return error;
  1612. }
  1613. error = 0;
  1614. }
  1615. info = SHMEM_I(inode);
  1616. inode->i_size = len-1;
  1617. if (len <= (char *)inode - (char *)info) {
  1618. /* do it inline */
  1619. memcpy(info, symname, len);
  1620. inode->i_op = &shmem_symlink_inline_operations;
  1621. } else {
  1622. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1623. if (error) {
  1624. iput(inode);
  1625. return error;
  1626. }
  1627. inode->i_op = &shmem_symlink_inode_operations;
  1628. kaddr = kmap_atomic(page, KM_USER0);
  1629. memcpy(kaddr, symname, len);
  1630. kunmap_atomic(kaddr, KM_USER0);
  1631. set_page_dirty(page);
  1632. page_cache_release(page);
  1633. }
  1634. if (dir->i_mode & S_ISGID)
  1635. inode->i_gid = dir->i_gid;
  1636. dir->i_size += BOGO_DIRENT_SIZE;
  1637. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1638. d_instantiate(dentry, inode);
  1639. dget(dentry);
  1640. return 0;
  1641. }
  1642. static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
  1643. {
  1644. nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
  1645. return NULL;
  1646. }
  1647. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1648. {
  1649. struct page *page = NULL;
  1650. int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1651. nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
  1652. return page;
  1653. }
  1654. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1655. {
  1656. if (!IS_ERR(nd_get_link(nd))) {
  1657. struct page *page = cookie;
  1658. kunmap(page);
  1659. mark_page_accessed(page);
  1660. page_cache_release(page);
  1661. }
  1662. }
  1663. static struct inode_operations shmem_symlink_inline_operations = {
  1664. .readlink = generic_readlink,
  1665. .follow_link = shmem_follow_link_inline,
  1666. };
  1667. static struct inode_operations shmem_symlink_inode_operations = {
  1668. .truncate = shmem_truncate,
  1669. .readlink = generic_readlink,
  1670. .follow_link = shmem_follow_link,
  1671. .put_link = shmem_put_link,
  1672. };
  1673. static int shmem_parse_options(char *options, int *mode, uid_t *uid,
  1674. gid_t *gid, unsigned long *blocks, unsigned long *inodes,
  1675. int *policy, nodemask_t *policy_nodes)
  1676. {
  1677. char *this_char, *value, *rest;
  1678. while ((this_char = strsep(&options, ",")) != NULL) {
  1679. if (!*this_char)
  1680. continue;
  1681. if ((value = strchr(this_char,'=')) != NULL) {
  1682. *value++ = 0;
  1683. } else {
  1684. printk(KERN_ERR
  1685. "tmpfs: No value for mount option '%s'\n",
  1686. this_char);
  1687. return 1;
  1688. }
  1689. if (!strcmp(this_char,"size")) {
  1690. unsigned long long size;
  1691. size = memparse(value,&rest);
  1692. if (*rest == '%') {
  1693. size <<= PAGE_SHIFT;
  1694. size *= totalram_pages;
  1695. do_div(size, 100);
  1696. rest++;
  1697. }
  1698. if (*rest)
  1699. goto bad_val;
  1700. *blocks = size >> PAGE_CACHE_SHIFT;
  1701. } else if (!strcmp(this_char,"nr_blocks")) {
  1702. *blocks = memparse(value,&rest);
  1703. if (*rest)
  1704. goto bad_val;
  1705. } else if (!strcmp(this_char,"nr_inodes")) {
  1706. *inodes = memparse(value,&rest);
  1707. if (*rest)
  1708. goto bad_val;
  1709. } else if (!strcmp(this_char,"mode")) {
  1710. if (!mode)
  1711. continue;
  1712. *mode = simple_strtoul(value,&rest,8);
  1713. if (*rest)
  1714. goto bad_val;
  1715. } else if (!strcmp(this_char,"uid")) {
  1716. if (!uid)
  1717. continue;
  1718. *uid = simple_strtoul(value,&rest,0);
  1719. if (*rest)
  1720. goto bad_val;
  1721. } else if (!strcmp(this_char,"gid")) {
  1722. if (!gid)
  1723. continue;
  1724. *gid = simple_strtoul(value,&rest,0);
  1725. if (*rest)
  1726. goto bad_val;
  1727. } else if (!strcmp(this_char,"mpol")) {
  1728. if (!strcmp(value,"default"))
  1729. *policy = MPOL_DEFAULT;
  1730. else if (!strcmp(value,"preferred"))
  1731. *policy = MPOL_PREFERRED;
  1732. else if (!strcmp(value,"bind"))
  1733. *policy = MPOL_BIND;
  1734. else if (!strcmp(value,"interleave"))
  1735. *policy = MPOL_INTERLEAVE;
  1736. else
  1737. goto bad_val;
  1738. } else if (!strcmp(this_char,"mpol_nodelist")) {
  1739. nodelist_parse(value, *policy_nodes);
  1740. } else {
  1741. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  1742. this_char);
  1743. return 1;
  1744. }
  1745. }
  1746. return 0;
  1747. bad_val:
  1748. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  1749. value, this_char);
  1750. return 1;
  1751. }
  1752. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  1753. {
  1754. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1755. unsigned long max_blocks = sbinfo->max_blocks;
  1756. unsigned long max_inodes = sbinfo->max_inodes;
  1757. int policy = sbinfo->policy;
  1758. nodemask_t policy_nodes = sbinfo->policy_nodes;
  1759. unsigned long blocks;
  1760. unsigned long inodes;
  1761. int error = -EINVAL;
  1762. if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks,
  1763. &max_inodes, &policy, &policy_nodes))
  1764. return error;
  1765. spin_lock(&sbinfo->stat_lock);
  1766. blocks = sbinfo->max_blocks - sbinfo->free_blocks;
  1767. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  1768. if (max_blocks < blocks)
  1769. goto out;
  1770. if (max_inodes < inodes)
  1771. goto out;
  1772. /*
  1773. * Those tests also disallow limited->unlimited while any are in
  1774. * use, so i_blocks will always be zero when max_blocks is zero;
  1775. * but we must separately disallow unlimited->limited, because
  1776. * in that case we have no record of how much is already in use.
  1777. */
  1778. if (max_blocks && !sbinfo->max_blocks)
  1779. goto out;
  1780. if (max_inodes && !sbinfo->max_inodes)
  1781. goto out;
  1782. error = 0;
  1783. sbinfo->max_blocks = max_blocks;
  1784. sbinfo->free_blocks = max_blocks - blocks;
  1785. sbinfo->max_inodes = max_inodes;
  1786. sbinfo->free_inodes = max_inodes - inodes;
  1787. sbinfo->policy = policy;
  1788. sbinfo->policy_nodes = policy_nodes;
  1789. out:
  1790. spin_unlock(&sbinfo->stat_lock);
  1791. return error;
  1792. }
  1793. #endif
  1794. static void shmem_put_super(struct super_block *sb)
  1795. {
  1796. kfree(sb->s_fs_info);
  1797. sb->s_fs_info = NULL;
  1798. }
  1799. static int shmem_fill_super(struct super_block *sb,
  1800. void *data, int silent)
  1801. {
  1802. struct inode *inode;
  1803. struct dentry *root;
  1804. int mode = S_IRWXUGO | S_ISVTX;
  1805. uid_t uid = current->fsuid;
  1806. gid_t gid = current->fsgid;
  1807. int err = -ENOMEM;
  1808. struct shmem_sb_info *sbinfo;
  1809. unsigned long blocks = 0;
  1810. unsigned long inodes = 0;
  1811. int policy = MPOL_DEFAULT;
  1812. nodemask_t policy_nodes = node_online_map;
  1813. #ifdef CONFIG_TMPFS
  1814. /*
  1815. * Per default we only allow half of the physical ram per
  1816. * tmpfs instance, limiting inodes to one per page of lowmem;
  1817. * but the internal instance is left unlimited.
  1818. */
  1819. if (!(sb->s_flags & MS_NOUSER)) {
  1820. blocks = totalram_pages / 2;
  1821. inodes = totalram_pages - totalhigh_pages;
  1822. if (inodes > blocks)
  1823. inodes = blocks;
  1824. if (shmem_parse_options(data, &mode, &uid, &gid, &blocks,
  1825. &inodes, &policy, &policy_nodes))
  1826. return -EINVAL;
  1827. }
  1828. #else
  1829. sb->s_flags |= MS_NOUSER;
  1830. #endif
  1831. /* Round up to L1_CACHE_BYTES to resist false sharing */
  1832. sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
  1833. L1_CACHE_BYTES), GFP_KERNEL);
  1834. if (!sbinfo)
  1835. return -ENOMEM;
  1836. spin_lock_init(&sbinfo->stat_lock);
  1837. sbinfo->max_blocks = blocks;
  1838. sbinfo->free_blocks = blocks;
  1839. sbinfo->max_inodes = inodes;
  1840. sbinfo->free_inodes = inodes;
  1841. sbinfo->policy = policy;
  1842. sbinfo->policy_nodes = policy_nodes;
  1843. sb->s_fs_info = sbinfo;
  1844. sb->s_maxbytes = SHMEM_MAX_BYTES;
  1845. sb->s_blocksize = PAGE_CACHE_SIZE;
  1846. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1847. sb->s_magic = TMPFS_MAGIC;
  1848. sb->s_op = &shmem_ops;
  1849. inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
  1850. if (!inode)
  1851. goto failed;
  1852. inode->i_uid = uid;
  1853. inode->i_gid = gid;
  1854. root = d_alloc_root(inode);
  1855. if (!root)
  1856. goto failed_iput;
  1857. sb->s_root = root;
  1858. return 0;
  1859. failed_iput:
  1860. iput(inode);
  1861. failed:
  1862. shmem_put_super(sb);
  1863. return err;
  1864. }
  1865. static kmem_cache_t *shmem_inode_cachep;
  1866. static struct inode *shmem_alloc_inode(struct super_block *sb)
  1867. {
  1868. struct shmem_inode_info *p;
  1869. p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, SLAB_KERNEL);
  1870. if (!p)
  1871. return NULL;
  1872. return &p->vfs_inode;
  1873. }
  1874. static void shmem_destroy_inode(struct inode *inode)
  1875. {
  1876. if ((inode->i_mode & S_IFMT) == S_IFREG) {
  1877. /* only struct inode is valid if it's an inline symlink */
  1878. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  1879. }
  1880. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  1881. }
  1882. static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
  1883. {
  1884. struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
  1885. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  1886. SLAB_CTOR_CONSTRUCTOR) {
  1887. inode_init_once(&p->vfs_inode);
  1888. }
  1889. }
  1890. static int init_inodecache(void)
  1891. {
  1892. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  1893. sizeof(struct shmem_inode_info),
  1894. 0, 0, init_once, NULL);
  1895. if (shmem_inode_cachep == NULL)
  1896. return -ENOMEM;
  1897. return 0;
  1898. }
  1899. static void destroy_inodecache(void)
  1900. {
  1901. if (kmem_cache_destroy(shmem_inode_cachep))
  1902. printk(KERN_INFO "shmem_inode_cache: not all structures were freed\n");
  1903. }
  1904. static struct address_space_operations shmem_aops = {
  1905. .writepage = shmem_writepage,
  1906. .set_page_dirty = __set_page_dirty_nobuffers,
  1907. #ifdef CONFIG_TMPFS
  1908. .prepare_write = shmem_prepare_write,
  1909. .commit_write = simple_commit_write,
  1910. #endif
  1911. };
  1912. static struct file_operations shmem_file_operations = {
  1913. .mmap = shmem_mmap,
  1914. #ifdef CONFIG_TMPFS
  1915. .llseek = generic_file_llseek,
  1916. .read = shmem_file_read,
  1917. .write = shmem_file_write,
  1918. .fsync = simple_sync_file,
  1919. .sendfile = shmem_file_sendfile,
  1920. #endif
  1921. };
  1922. static struct inode_operations shmem_inode_operations = {
  1923. .truncate = shmem_truncate,
  1924. .setattr = shmem_notify_change,
  1925. .truncate_range = shmem_truncate_range,
  1926. };
  1927. static struct inode_operations shmem_dir_inode_operations = {
  1928. #ifdef CONFIG_TMPFS
  1929. .create = shmem_create,
  1930. .lookup = simple_lookup,
  1931. .link = shmem_link,
  1932. .unlink = shmem_unlink,
  1933. .symlink = shmem_symlink,
  1934. .mkdir = shmem_mkdir,
  1935. .rmdir = shmem_rmdir,
  1936. .mknod = shmem_mknod,
  1937. .rename = shmem_rename,
  1938. #endif
  1939. };
  1940. static struct super_operations shmem_ops = {
  1941. .alloc_inode = shmem_alloc_inode,
  1942. .destroy_inode = shmem_destroy_inode,
  1943. #ifdef CONFIG_TMPFS
  1944. .statfs = shmem_statfs,
  1945. .remount_fs = shmem_remount_fs,
  1946. #endif
  1947. .delete_inode = shmem_delete_inode,
  1948. .drop_inode = generic_delete_inode,
  1949. .put_super = shmem_put_super,
  1950. };
  1951. static struct vm_operations_struct shmem_vm_ops = {
  1952. .nopage = shmem_nopage,
  1953. .populate = shmem_populate,
  1954. #ifdef CONFIG_NUMA
  1955. .set_policy = shmem_set_policy,
  1956. .get_policy = shmem_get_policy,
  1957. #endif
  1958. };
  1959. static struct super_block *shmem_get_sb(struct file_system_type *fs_type,
  1960. int flags, const char *dev_name, void *data)
  1961. {
  1962. return get_sb_nodev(fs_type, flags, data, shmem_fill_super);
  1963. }
  1964. static struct file_system_type tmpfs_fs_type = {
  1965. .owner = THIS_MODULE,
  1966. .name = "tmpfs",
  1967. .get_sb = shmem_get_sb,
  1968. .kill_sb = kill_litter_super,
  1969. };
  1970. static struct vfsmount *shm_mnt;
  1971. static int __init init_tmpfs(void)
  1972. {
  1973. int error;
  1974. error = init_inodecache();
  1975. if (error)
  1976. goto out3;
  1977. error = register_filesystem(&tmpfs_fs_type);
  1978. if (error) {
  1979. printk(KERN_ERR "Could not register tmpfs\n");
  1980. goto out2;
  1981. }
  1982. #ifdef CONFIG_TMPFS
  1983. devfs_mk_dir("shm");
  1984. #endif
  1985. shm_mnt = do_kern_mount(tmpfs_fs_type.name, MS_NOUSER,
  1986. tmpfs_fs_type.name, NULL);
  1987. if (IS_ERR(shm_mnt)) {
  1988. error = PTR_ERR(shm_mnt);
  1989. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  1990. goto out1;
  1991. }
  1992. return 0;
  1993. out1:
  1994. unregister_filesystem(&tmpfs_fs_type);
  1995. out2:
  1996. destroy_inodecache();
  1997. out3:
  1998. shm_mnt = ERR_PTR(error);
  1999. return error;
  2000. }
  2001. module_init(init_tmpfs)
  2002. /*
  2003. * shmem_file_setup - get an unlinked file living in tmpfs
  2004. *
  2005. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2006. * @size: size to be set for the file
  2007. *
  2008. */
  2009. struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
  2010. {
  2011. int error;
  2012. struct file *file;
  2013. struct inode *inode;
  2014. struct dentry *dentry, *root;
  2015. struct qstr this;
  2016. if (IS_ERR(shm_mnt))
  2017. return (void *)shm_mnt;
  2018. if (size < 0 || size > SHMEM_MAX_BYTES)
  2019. return ERR_PTR(-EINVAL);
  2020. if (shmem_acct_size(flags, size))
  2021. return ERR_PTR(-ENOMEM);
  2022. error = -ENOMEM;
  2023. this.name = name;
  2024. this.len = strlen(name);
  2025. this.hash = 0; /* will go */
  2026. root = shm_mnt->mnt_root;
  2027. dentry = d_alloc(root, &this);
  2028. if (!dentry)
  2029. goto put_memory;
  2030. error = -ENFILE;
  2031. file = get_empty_filp();
  2032. if (!file)
  2033. goto put_dentry;
  2034. error = -ENOSPC;
  2035. inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
  2036. if (!inode)
  2037. goto close_file;
  2038. SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
  2039. d_instantiate(dentry, inode);
  2040. inode->i_size = size;
  2041. inode->i_nlink = 0; /* It is unlinked */
  2042. file->f_vfsmnt = mntget(shm_mnt);
  2043. file->f_dentry = dentry;
  2044. file->f_mapping = inode->i_mapping;
  2045. file->f_op = &shmem_file_operations;
  2046. file->f_mode = FMODE_WRITE | FMODE_READ;
  2047. return file;
  2048. close_file:
  2049. put_filp(file);
  2050. put_dentry:
  2051. dput(dentry);
  2052. put_memory:
  2053. shmem_unacct_size(flags, size);
  2054. return ERR_PTR(error);
  2055. }
  2056. /*
  2057. * shmem_zero_setup - setup a shared anonymous mapping
  2058. *
  2059. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2060. */
  2061. int shmem_zero_setup(struct vm_area_struct *vma)
  2062. {
  2063. struct file *file;
  2064. loff_t size = vma->vm_end - vma->vm_start;
  2065. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2066. if (IS_ERR(file))
  2067. return PTR_ERR(file);
  2068. if (vma->vm_file)
  2069. fput(vma->vm_file);
  2070. vma->vm_file = file;
  2071. vma->vm_ops = &shmem_vm_ops;
  2072. return 0;
  2073. }