shmem.c 63 KB

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