shmem.c 65 KB

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