shmem.c 56 KB

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