shmem.c 61 KB

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