shmem.c 65 KB

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