shmem.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584
  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_async(struct shared_policy *p,
  946. swp_entry_t entry, unsigned long idx)
  947. {
  948. struct page *page;
  949. struct vm_area_struct pvma;
  950. /* Create a pseudo vma that just contains the policy */
  951. memset(&pvma, 0, sizeof(struct vm_area_struct));
  952. pvma.vm_end = PAGE_SIZE;
  953. pvma.vm_pgoff = idx;
  954. pvma.vm_policy = mpol_shared_policy_lookup(p, idx);
  955. page = read_swap_cache_async(entry, &pvma, 0);
  956. mpol_free(pvma.vm_policy);
  957. return page;
  958. }
  959. static struct page *shmem_swapin(struct shmem_inode_info *info,
  960. swp_entry_t entry, unsigned long idx)
  961. {
  962. struct shared_policy *p = &info->policy;
  963. int i, num;
  964. struct page *page;
  965. unsigned long offset;
  966. num = valid_swaphandles(entry, &offset);
  967. for (i = 0; i < num; offset++, i++) {
  968. page = shmem_swapin_async(p,
  969. swp_entry(swp_type(entry), offset), idx);
  970. if (!page)
  971. break;
  972. page_cache_release(page);
  973. }
  974. lru_add_drain(); /* Push any new pages onto the LRU now */
  975. return shmem_swapin_async(p, entry, idx);
  976. }
  977. static struct page *
  978. shmem_alloc_page(gfp_t gfp, struct shmem_inode_info *info,
  979. unsigned long idx)
  980. {
  981. struct vm_area_struct pvma;
  982. struct page *page;
  983. memset(&pvma, 0, sizeof(struct vm_area_struct));
  984. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
  985. pvma.vm_pgoff = idx;
  986. pvma.vm_end = PAGE_SIZE;
  987. page = alloc_page_vma(gfp | __GFP_ZERO, &pvma, 0);
  988. mpol_free(pvma.vm_policy);
  989. return page;
  990. }
  991. #else
  992. static inline int shmem_parse_mpol(char *value, int *policy,
  993. nodemask_t *policy_nodes)
  994. {
  995. return 1;
  996. }
  997. static inline struct page *
  998. shmem_swapin(struct shmem_inode_info *info,swp_entry_t entry,unsigned long idx)
  999. {
  1000. swapin_readahead(entry, 0, NULL);
  1001. return read_swap_cache_async(entry, NULL, 0);
  1002. }
  1003. static inline struct page *
  1004. shmem_alloc_page(gfp_t gfp,struct shmem_inode_info *info, unsigned long idx)
  1005. {
  1006. return alloc_page(gfp | __GFP_ZERO);
  1007. }
  1008. #endif
  1009. /*
  1010. * shmem_getpage - either get the page from swap or allocate a new one
  1011. *
  1012. * If we allocate a new one we do not mark it dirty. That's up to the
  1013. * vm. If we swap it in we mark it dirty since we also free the swap
  1014. * entry since a page cannot live in both the swap and page cache
  1015. */
  1016. static int shmem_getpage(struct inode *inode, unsigned long idx,
  1017. struct page **pagep, enum sgp_type sgp, int *type)
  1018. {
  1019. struct address_space *mapping = inode->i_mapping;
  1020. struct shmem_inode_info *info = SHMEM_I(inode);
  1021. struct shmem_sb_info *sbinfo;
  1022. struct page *filepage = *pagep;
  1023. struct page *swappage;
  1024. swp_entry_t *entry;
  1025. swp_entry_t swap;
  1026. int error;
  1027. if (idx >= SHMEM_MAX_INDEX)
  1028. return -EFBIG;
  1029. if (type)
  1030. *type = 0;
  1031. /*
  1032. * Normally, filepage is NULL on entry, and either found
  1033. * uptodate immediately, or allocated and zeroed, or read
  1034. * in under swappage, which is then assigned to filepage.
  1035. * But shmem_readpage and shmem_write_begin pass in a locked
  1036. * filepage, which may be found not uptodate by other callers
  1037. * too, and may need to be copied from the swappage read in.
  1038. */
  1039. repeat:
  1040. if (!filepage)
  1041. filepage = find_lock_page(mapping, idx);
  1042. if (filepage && PageUptodate(filepage))
  1043. goto done;
  1044. error = 0;
  1045. if (sgp == SGP_QUICK)
  1046. goto failed;
  1047. spin_lock(&info->lock);
  1048. shmem_recalc_inode(inode);
  1049. entry = shmem_swp_alloc(info, idx, sgp);
  1050. if (IS_ERR(entry)) {
  1051. spin_unlock(&info->lock);
  1052. error = PTR_ERR(entry);
  1053. goto failed;
  1054. }
  1055. swap = *entry;
  1056. if (swap.val) {
  1057. /* Look it up and read it in.. */
  1058. swappage = lookup_swap_cache(swap);
  1059. if (!swappage) {
  1060. shmem_swp_unmap(entry);
  1061. /* here we actually do the io */
  1062. if (type && !(*type & VM_FAULT_MAJOR)) {
  1063. __count_vm_event(PGMAJFAULT);
  1064. *type |= VM_FAULT_MAJOR;
  1065. }
  1066. spin_unlock(&info->lock);
  1067. swappage = shmem_swapin(info, swap, idx);
  1068. if (!swappage) {
  1069. spin_lock(&info->lock);
  1070. entry = shmem_swp_alloc(info, idx, sgp);
  1071. if (IS_ERR(entry))
  1072. error = PTR_ERR(entry);
  1073. else {
  1074. if (entry->val == swap.val)
  1075. error = -ENOMEM;
  1076. shmem_swp_unmap(entry);
  1077. }
  1078. spin_unlock(&info->lock);
  1079. if (error)
  1080. goto failed;
  1081. goto repeat;
  1082. }
  1083. wait_on_page_locked(swappage);
  1084. page_cache_release(swappage);
  1085. goto repeat;
  1086. }
  1087. /* We have to do this with page locked to prevent races */
  1088. if (TestSetPageLocked(swappage)) {
  1089. shmem_swp_unmap(entry);
  1090. spin_unlock(&info->lock);
  1091. wait_on_page_locked(swappage);
  1092. page_cache_release(swappage);
  1093. goto repeat;
  1094. }
  1095. if (PageWriteback(swappage)) {
  1096. shmem_swp_unmap(entry);
  1097. spin_unlock(&info->lock);
  1098. wait_on_page_writeback(swappage);
  1099. unlock_page(swappage);
  1100. page_cache_release(swappage);
  1101. goto repeat;
  1102. }
  1103. if (!PageUptodate(swappage)) {
  1104. shmem_swp_unmap(entry);
  1105. spin_unlock(&info->lock);
  1106. unlock_page(swappage);
  1107. page_cache_release(swappage);
  1108. error = -EIO;
  1109. goto failed;
  1110. }
  1111. if (filepage) {
  1112. shmem_swp_set(info, entry, 0);
  1113. shmem_swp_unmap(entry);
  1114. delete_from_swap_cache(swappage);
  1115. spin_unlock(&info->lock);
  1116. copy_highpage(filepage, swappage);
  1117. unlock_page(swappage);
  1118. page_cache_release(swappage);
  1119. flush_dcache_page(filepage);
  1120. SetPageUptodate(filepage);
  1121. set_page_dirty(filepage);
  1122. swap_free(swap);
  1123. } else if (!(error = move_from_swap_cache(
  1124. swappage, idx, mapping))) {
  1125. info->flags |= SHMEM_PAGEIN;
  1126. shmem_swp_set(info, entry, 0);
  1127. shmem_swp_unmap(entry);
  1128. spin_unlock(&info->lock);
  1129. filepage = swappage;
  1130. swap_free(swap);
  1131. } else {
  1132. shmem_swp_unmap(entry);
  1133. spin_unlock(&info->lock);
  1134. unlock_page(swappage);
  1135. page_cache_release(swappage);
  1136. if (error == -ENOMEM) {
  1137. /* let kswapd refresh zone for GFP_ATOMICs */
  1138. congestion_wait(WRITE, HZ/50);
  1139. }
  1140. goto repeat;
  1141. }
  1142. } else if (sgp == SGP_READ && !filepage) {
  1143. shmem_swp_unmap(entry);
  1144. filepage = find_get_page(mapping, idx);
  1145. if (filepage &&
  1146. (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
  1147. spin_unlock(&info->lock);
  1148. wait_on_page_locked(filepage);
  1149. page_cache_release(filepage);
  1150. filepage = NULL;
  1151. goto repeat;
  1152. }
  1153. spin_unlock(&info->lock);
  1154. } else {
  1155. shmem_swp_unmap(entry);
  1156. sbinfo = SHMEM_SB(inode->i_sb);
  1157. if (sbinfo->max_blocks) {
  1158. spin_lock(&sbinfo->stat_lock);
  1159. if (sbinfo->free_blocks == 0 ||
  1160. shmem_acct_block(info->flags)) {
  1161. spin_unlock(&sbinfo->stat_lock);
  1162. spin_unlock(&info->lock);
  1163. error = -ENOSPC;
  1164. goto failed;
  1165. }
  1166. sbinfo->free_blocks--;
  1167. inode->i_blocks += BLOCKS_PER_PAGE;
  1168. spin_unlock(&sbinfo->stat_lock);
  1169. } else if (shmem_acct_block(info->flags)) {
  1170. spin_unlock(&info->lock);
  1171. error = -ENOSPC;
  1172. goto failed;
  1173. }
  1174. if (!filepage) {
  1175. spin_unlock(&info->lock);
  1176. filepage = shmem_alloc_page(mapping_gfp_mask(mapping),
  1177. info,
  1178. idx);
  1179. if (!filepage) {
  1180. shmem_unacct_blocks(info->flags, 1);
  1181. shmem_free_blocks(inode, 1);
  1182. error = -ENOMEM;
  1183. goto failed;
  1184. }
  1185. spin_lock(&info->lock);
  1186. entry = shmem_swp_alloc(info, idx, sgp);
  1187. if (IS_ERR(entry))
  1188. error = PTR_ERR(entry);
  1189. else {
  1190. swap = *entry;
  1191. shmem_swp_unmap(entry);
  1192. }
  1193. if (error || swap.val || 0 != add_to_page_cache_lru(
  1194. filepage, mapping, idx, GFP_ATOMIC)) {
  1195. spin_unlock(&info->lock);
  1196. page_cache_release(filepage);
  1197. shmem_unacct_blocks(info->flags, 1);
  1198. shmem_free_blocks(inode, 1);
  1199. filepage = NULL;
  1200. if (error)
  1201. goto failed;
  1202. goto repeat;
  1203. }
  1204. info->flags |= SHMEM_PAGEIN;
  1205. }
  1206. info->alloced++;
  1207. spin_unlock(&info->lock);
  1208. flush_dcache_page(filepage);
  1209. SetPageUptodate(filepage);
  1210. }
  1211. done:
  1212. if (*pagep != filepage) {
  1213. *pagep = filepage;
  1214. if (sgp != SGP_FAULT)
  1215. unlock_page(filepage);
  1216. }
  1217. return 0;
  1218. failed:
  1219. if (*pagep != filepage) {
  1220. unlock_page(filepage);
  1221. page_cache_release(filepage);
  1222. }
  1223. return error;
  1224. }
  1225. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1226. {
  1227. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1228. int error;
  1229. int ret;
  1230. if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  1231. return VM_FAULT_SIGBUS;
  1232. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_FAULT, &ret);
  1233. if (error)
  1234. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  1235. mark_page_accessed(vmf->page);
  1236. return ret | VM_FAULT_LOCKED;
  1237. }
  1238. #ifdef CONFIG_NUMA
  1239. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  1240. {
  1241. struct inode *i = vma->vm_file->f_path.dentry->d_inode;
  1242. return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
  1243. }
  1244. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  1245. unsigned long addr)
  1246. {
  1247. struct inode *i = vma->vm_file->f_path.dentry->d_inode;
  1248. unsigned long idx;
  1249. idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1250. return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
  1251. }
  1252. #endif
  1253. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1254. {
  1255. struct inode *inode = file->f_path.dentry->d_inode;
  1256. struct shmem_inode_info *info = SHMEM_I(inode);
  1257. int retval = -ENOMEM;
  1258. spin_lock(&info->lock);
  1259. if (lock && !(info->flags & VM_LOCKED)) {
  1260. if (!user_shm_lock(inode->i_size, user))
  1261. goto out_nomem;
  1262. info->flags |= VM_LOCKED;
  1263. }
  1264. if (!lock && (info->flags & VM_LOCKED) && user) {
  1265. user_shm_unlock(inode->i_size, user);
  1266. info->flags &= ~VM_LOCKED;
  1267. }
  1268. retval = 0;
  1269. out_nomem:
  1270. spin_unlock(&info->lock);
  1271. return retval;
  1272. }
  1273. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1274. {
  1275. file_accessed(file);
  1276. vma->vm_ops = &shmem_vm_ops;
  1277. vma->vm_flags |= VM_CAN_NONLINEAR;
  1278. return 0;
  1279. }
  1280. static struct inode *
  1281. shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
  1282. {
  1283. struct inode *inode;
  1284. struct shmem_inode_info *info;
  1285. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1286. if (sbinfo->max_inodes) {
  1287. spin_lock(&sbinfo->stat_lock);
  1288. if (!sbinfo->free_inodes) {
  1289. spin_unlock(&sbinfo->stat_lock);
  1290. return NULL;
  1291. }
  1292. sbinfo->free_inodes--;
  1293. spin_unlock(&sbinfo->stat_lock);
  1294. }
  1295. inode = new_inode(sb);
  1296. if (inode) {
  1297. inode->i_mode = mode;
  1298. inode->i_uid = current->fsuid;
  1299. inode->i_gid = current->fsgid;
  1300. inode->i_blocks = 0;
  1301. inode->i_mapping->a_ops = &shmem_aops;
  1302. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1303. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1304. inode->i_generation = get_seconds();
  1305. info = SHMEM_I(inode);
  1306. memset(info, 0, (char *)inode - (char *)info);
  1307. spin_lock_init(&info->lock);
  1308. INIT_LIST_HEAD(&info->swaplist);
  1309. switch (mode & S_IFMT) {
  1310. default:
  1311. inode->i_op = &shmem_special_inode_operations;
  1312. init_special_inode(inode, mode, dev);
  1313. break;
  1314. case S_IFREG:
  1315. inode->i_op = &shmem_inode_operations;
  1316. inode->i_fop = &shmem_file_operations;
  1317. mpol_shared_policy_init(&info->policy, sbinfo->policy,
  1318. &sbinfo->policy_nodes);
  1319. break;
  1320. case S_IFDIR:
  1321. inc_nlink(inode);
  1322. /* Some things misbehave if size == 0 on a directory */
  1323. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1324. inode->i_op = &shmem_dir_inode_operations;
  1325. inode->i_fop = &simple_dir_operations;
  1326. break;
  1327. case S_IFLNK:
  1328. /*
  1329. * Must not load anything in the rbtree,
  1330. * mpol_free_shared_policy will not be called.
  1331. */
  1332. mpol_shared_policy_init(&info->policy, MPOL_DEFAULT,
  1333. NULL);
  1334. break;
  1335. }
  1336. } else if (sbinfo->max_inodes) {
  1337. spin_lock(&sbinfo->stat_lock);
  1338. sbinfo->free_inodes++;
  1339. spin_unlock(&sbinfo->stat_lock);
  1340. }
  1341. return inode;
  1342. }
  1343. #ifdef CONFIG_TMPFS
  1344. static const struct inode_operations shmem_symlink_inode_operations;
  1345. static const struct inode_operations shmem_symlink_inline_operations;
  1346. /*
  1347. * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
  1348. * but providing them allows a tmpfs file to be used for splice, sendfile, and
  1349. * below the loop driver, in the generic fashion that many filesystems support.
  1350. */
  1351. static int shmem_readpage(struct file *file, struct page *page)
  1352. {
  1353. struct inode *inode = page->mapping->host;
  1354. int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
  1355. unlock_page(page);
  1356. return error;
  1357. }
  1358. static int
  1359. shmem_write_begin(struct file *file, struct address_space *mapping,
  1360. loff_t pos, unsigned len, unsigned flags,
  1361. struct page **pagep, void **fsdata)
  1362. {
  1363. struct inode *inode = mapping->host;
  1364. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1365. *pagep = NULL;
  1366. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1367. }
  1368. static int
  1369. shmem_write_end(struct file *file, struct address_space *mapping,
  1370. loff_t pos, unsigned len, unsigned copied,
  1371. struct page *page, void *fsdata)
  1372. {
  1373. struct inode *inode = mapping->host;
  1374. set_page_dirty(page);
  1375. page_cache_release(page);
  1376. if (pos+copied > inode->i_size)
  1377. i_size_write(inode, pos+copied);
  1378. return copied;
  1379. }
  1380. static ssize_t
  1381. shmem_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  1382. {
  1383. struct inode *inode = file->f_path.dentry->d_inode;
  1384. loff_t pos;
  1385. unsigned long written;
  1386. ssize_t err;
  1387. if ((ssize_t) count < 0)
  1388. return -EINVAL;
  1389. if (!access_ok(VERIFY_READ, buf, count))
  1390. return -EFAULT;
  1391. mutex_lock(&inode->i_mutex);
  1392. pos = *ppos;
  1393. written = 0;
  1394. err = generic_write_checks(file, &pos, &count, 0);
  1395. if (err || !count)
  1396. goto out;
  1397. err = remove_suid(file->f_path.dentry);
  1398. if (err)
  1399. goto out;
  1400. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1401. do {
  1402. struct page *page = NULL;
  1403. unsigned long bytes, index, offset;
  1404. char *kaddr;
  1405. int left;
  1406. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  1407. index = pos >> PAGE_CACHE_SHIFT;
  1408. bytes = PAGE_CACHE_SIZE - offset;
  1409. if (bytes > count)
  1410. bytes = count;
  1411. /*
  1412. * We don't hold page lock across copy from user -
  1413. * what would it guard against? - so no deadlock here.
  1414. * But it still may be a good idea to prefault below.
  1415. */
  1416. err = shmem_getpage(inode, index, &page, SGP_WRITE, NULL);
  1417. if (err)
  1418. break;
  1419. left = bytes;
  1420. if (PageHighMem(page)) {
  1421. volatile unsigned char dummy;
  1422. __get_user(dummy, buf);
  1423. __get_user(dummy, buf + bytes - 1);
  1424. kaddr = kmap_atomic(page, KM_USER0);
  1425. left = __copy_from_user_inatomic(kaddr + offset,
  1426. buf, bytes);
  1427. kunmap_atomic(kaddr, KM_USER0);
  1428. }
  1429. if (left) {
  1430. kaddr = kmap(page);
  1431. left = __copy_from_user(kaddr + offset, buf, bytes);
  1432. kunmap(page);
  1433. }
  1434. written += bytes;
  1435. count -= bytes;
  1436. pos += bytes;
  1437. buf += bytes;
  1438. if (pos > inode->i_size)
  1439. i_size_write(inode, pos);
  1440. flush_dcache_page(page);
  1441. set_page_dirty(page);
  1442. mark_page_accessed(page);
  1443. page_cache_release(page);
  1444. if (left) {
  1445. pos -= left;
  1446. written -= left;
  1447. err = -EFAULT;
  1448. break;
  1449. }
  1450. /*
  1451. * Our dirty pages are not counted in nr_dirty,
  1452. * and we do not attempt to balance dirty pages.
  1453. */
  1454. cond_resched();
  1455. } while (count);
  1456. *ppos = pos;
  1457. if (written)
  1458. err = written;
  1459. out:
  1460. mutex_unlock(&inode->i_mutex);
  1461. return err;
  1462. }
  1463. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1464. {
  1465. struct inode *inode = filp->f_path.dentry->d_inode;
  1466. struct address_space *mapping = inode->i_mapping;
  1467. unsigned long index, offset;
  1468. index = *ppos >> PAGE_CACHE_SHIFT;
  1469. offset = *ppos & ~PAGE_CACHE_MASK;
  1470. for (;;) {
  1471. struct page *page = NULL;
  1472. unsigned long end_index, nr, ret;
  1473. loff_t i_size = i_size_read(inode);
  1474. end_index = i_size >> PAGE_CACHE_SHIFT;
  1475. if (index > end_index)
  1476. break;
  1477. if (index == end_index) {
  1478. nr = i_size & ~PAGE_CACHE_MASK;
  1479. if (nr <= offset)
  1480. break;
  1481. }
  1482. desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
  1483. if (desc->error) {
  1484. if (desc->error == -EINVAL)
  1485. desc->error = 0;
  1486. break;
  1487. }
  1488. /*
  1489. * We must evaluate after, since reads (unlike writes)
  1490. * are called without i_mutex protection against truncate
  1491. */
  1492. nr = PAGE_CACHE_SIZE;
  1493. i_size = i_size_read(inode);
  1494. end_index = i_size >> PAGE_CACHE_SHIFT;
  1495. if (index == end_index) {
  1496. nr = i_size & ~PAGE_CACHE_MASK;
  1497. if (nr <= offset) {
  1498. if (page)
  1499. page_cache_release(page);
  1500. break;
  1501. }
  1502. }
  1503. nr -= offset;
  1504. if (page) {
  1505. /*
  1506. * If users can be writing to this page using arbitrary
  1507. * virtual addresses, take care about potential aliasing
  1508. * before reading the page on the kernel side.
  1509. */
  1510. if (mapping_writably_mapped(mapping))
  1511. flush_dcache_page(page);
  1512. /*
  1513. * Mark the page accessed if we read the beginning.
  1514. */
  1515. if (!offset)
  1516. mark_page_accessed(page);
  1517. } else {
  1518. page = ZERO_PAGE(0);
  1519. page_cache_get(page);
  1520. }
  1521. /*
  1522. * Ok, we have the page, and it's up-to-date, so
  1523. * now we can copy it to user space...
  1524. *
  1525. * The actor routine returns how many bytes were actually used..
  1526. * NOTE! This may not be the same as how much of a user buffer
  1527. * we filled up (we may be padding etc), so we can only update
  1528. * "pos" here (the actor routine has to update the user buffer
  1529. * pointers and the remaining count).
  1530. */
  1531. ret = actor(desc, page, offset, nr);
  1532. offset += ret;
  1533. index += offset >> PAGE_CACHE_SHIFT;
  1534. offset &= ~PAGE_CACHE_MASK;
  1535. page_cache_release(page);
  1536. if (ret != nr || !desc->count)
  1537. break;
  1538. cond_resched();
  1539. }
  1540. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1541. file_accessed(filp);
  1542. }
  1543. static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  1544. {
  1545. read_descriptor_t desc;
  1546. if ((ssize_t) count < 0)
  1547. return -EINVAL;
  1548. if (!access_ok(VERIFY_WRITE, buf, count))
  1549. return -EFAULT;
  1550. if (!count)
  1551. return 0;
  1552. desc.written = 0;
  1553. desc.count = count;
  1554. desc.arg.buf = buf;
  1555. desc.error = 0;
  1556. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1557. if (desc.written)
  1558. return desc.written;
  1559. return desc.error;
  1560. }
  1561. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1562. {
  1563. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1564. buf->f_type = TMPFS_MAGIC;
  1565. buf->f_bsize = PAGE_CACHE_SIZE;
  1566. buf->f_namelen = NAME_MAX;
  1567. spin_lock(&sbinfo->stat_lock);
  1568. if (sbinfo->max_blocks) {
  1569. buf->f_blocks = sbinfo->max_blocks;
  1570. buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
  1571. }
  1572. if (sbinfo->max_inodes) {
  1573. buf->f_files = sbinfo->max_inodes;
  1574. buf->f_ffree = sbinfo->free_inodes;
  1575. }
  1576. /* else leave those fields 0 like simple_statfs */
  1577. spin_unlock(&sbinfo->stat_lock);
  1578. return 0;
  1579. }
  1580. /*
  1581. * File creation. Allocate an inode, and we're done..
  1582. */
  1583. static int
  1584. shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1585. {
  1586. struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
  1587. int error = -ENOSPC;
  1588. if (inode) {
  1589. error = security_inode_init_security(inode, dir, NULL, NULL,
  1590. NULL);
  1591. if (error) {
  1592. if (error != -EOPNOTSUPP) {
  1593. iput(inode);
  1594. return error;
  1595. }
  1596. }
  1597. error = shmem_acl_init(inode, dir);
  1598. if (error) {
  1599. iput(inode);
  1600. return error;
  1601. }
  1602. if (dir->i_mode & S_ISGID) {
  1603. inode->i_gid = dir->i_gid;
  1604. if (S_ISDIR(mode))
  1605. inode->i_mode |= S_ISGID;
  1606. }
  1607. dir->i_size += BOGO_DIRENT_SIZE;
  1608. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1609. d_instantiate(dentry, inode);
  1610. dget(dentry); /* Extra count - pin the dentry in core */
  1611. }
  1612. return error;
  1613. }
  1614. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1615. {
  1616. int error;
  1617. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1618. return error;
  1619. inc_nlink(dir);
  1620. return 0;
  1621. }
  1622. static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
  1623. struct nameidata *nd)
  1624. {
  1625. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1626. }
  1627. /*
  1628. * Link a file..
  1629. */
  1630. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1631. {
  1632. struct inode *inode = old_dentry->d_inode;
  1633. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1634. /*
  1635. * No ordinary (disk based) filesystem counts links as inodes;
  1636. * but each new link needs a new dentry, pinning lowmem, and
  1637. * tmpfs dentries cannot be pruned until they are unlinked.
  1638. */
  1639. if (sbinfo->max_inodes) {
  1640. spin_lock(&sbinfo->stat_lock);
  1641. if (!sbinfo->free_inodes) {
  1642. spin_unlock(&sbinfo->stat_lock);
  1643. return -ENOSPC;
  1644. }
  1645. sbinfo->free_inodes--;
  1646. spin_unlock(&sbinfo->stat_lock);
  1647. }
  1648. dir->i_size += BOGO_DIRENT_SIZE;
  1649. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1650. inc_nlink(inode);
  1651. atomic_inc(&inode->i_count); /* New dentry reference */
  1652. dget(dentry); /* Extra pinning count for the created dentry */
  1653. d_instantiate(dentry, inode);
  1654. return 0;
  1655. }
  1656. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1657. {
  1658. struct inode *inode = dentry->d_inode;
  1659. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) {
  1660. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1661. if (sbinfo->max_inodes) {
  1662. spin_lock(&sbinfo->stat_lock);
  1663. sbinfo->free_inodes++;
  1664. spin_unlock(&sbinfo->stat_lock);
  1665. }
  1666. }
  1667. dir->i_size -= BOGO_DIRENT_SIZE;
  1668. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1669. drop_nlink(inode);
  1670. dput(dentry); /* Undo the count from "create" - this does all the work */
  1671. return 0;
  1672. }
  1673. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1674. {
  1675. if (!simple_empty(dentry))
  1676. return -ENOTEMPTY;
  1677. drop_nlink(dentry->d_inode);
  1678. drop_nlink(dir);
  1679. return shmem_unlink(dir, dentry);
  1680. }
  1681. /*
  1682. * The VFS layer already does all the dentry stuff for rename,
  1683. * we just have to decrement the usage count for the target if
  1684. * it exists so that the VFS layer correctly free's it when it
  1685. * gets overwritten.
  1686. */
  1687. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1688. {
  1689. struct inode *inode = old_dentry->d_inode;
  1690. int they_are_dirs = S_ISDIR(inode->i_mode);
  1691. if (!simple_empty(new_dentry))
  1692. return -ENOTEMPTY;
  1693. if (new_dentry->d_inode) {
  1694. (void) shmem_unlink(new_dir, new_dentry);
  1695. if (they_are_dirs)
  1696. drop_nlink(old_dir);
  1697. } else if (they_are_dirs) {
  1698. drop_nlink(old_dir);
  1699. inc_nlink(new_dir);
  1700. }
  1701. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1702. new_dir->i_size += BOGO_DIRENT_SIZE;
  1703. old_dir->i_ctime = old_dir->i_mtime =
  1704. new_dir->i_ctime = new_dir->i_mtime =
  1705. inode->i_ctime = CURRENT_TIME;
  1706. return 0;
  1707. }
  1708. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1709. {
  1710. int error;
  1711. int len;
  1712. struct inode *inode;
  1713. struct page *page = NULL;
  1714. char *kaddr;
  1715. struct shmem_inode_info *info;
  1716. len = strlen(symname) + 1;
  1717. if (len > PAGE_CACHE_SIZE)
  1718. return -ENAMETOOLONG;
  1719. inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
  1720. if (!inode)
  1721. return -ENOSPC;
  1722. error = security_inode_init_security(inode, dir, NULL, NULL,
  1723. NULL);
  1724. if (error) {
  1725. if (error != -EOPNOTSUPP) {
  1726. iput(inode);
  1727. return error;
  1728. }
  1729. error = 0;
  1730. }
  1731. info = SHMEM_I(inode);
  1732. inode->i_size = len-1;
  1733. if (len <= (char *)inode - (char *)info) {
  1734. /* do it inline */
  1735. memcpy(info, symname, len);
  1736. inode->i_op = &shmem_symlink_inline_operations;
  1737. } else {
  1738. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1739. if (error) {
  1740. iput(inode);
  1741. return error;
  1742. }
  1743. inode->i_op = &shmem_symlink_inode_operations;
  1744. kaddr = kmap_atomic(page, KM_USER0);
  1745. memcpy(kaddr, symname, len);
  1746. kunmap_atomic(kaddr, KM_USER0);
  1747. set_page_dirty(page);
  1748. page_cache_release(page);
  1749. }
  1750. if (dir->i_mode & S_ISGID)
  1751. inode->i_gid = dir->i_gid;
  1752. dir->i_size += BOGO_DIRENT_SIZE;
  1753. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1754. d_instantiate(dentry, inode);
  1755. dget(dentry);
  1756. return 0;
  1757. }
  1758. static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
  1759. {
  1760. nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
  1761. return NULL;
  1762. }
  1763. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1764. {
  1765. struct page *page = NULL;
  1766. int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1767. nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
  1768. return page;
  1769. }
  1770. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1771. {
  1772. if (!IS_ERR(nd_get_link(nd))) {
  1773. struct page *page = cookie;
  1774. kunmap(page);
  1775. mark_page_accessed(page);
  1776. page_cache_release(page);
  1777. }
  1778. }
  1779. static const struct inode_operations shmem_symlink_inline_operations = {
  1780. .readlink = generic_readlink,
  1781. .follow_link = shmem_follow_link_inline,
  1782. };
  1783. static const struct inode_operations shmem_symlink_inode_operations = {
  1784. .truncate = shmem_truncate,
  1785. .readlink = generic_readlink,
  1786. .follow_link = shmem_follow_link,
  1787. .put_link = shmem_put_link,
  1788. };
  1789. #ifdef CONFIG_TMPFS_POSIX_ACL
  1790. /**
  1791. * Superblocks without xattr inode operations will get security.* xattr
  1792. * support from the VFS "for free". As soon as we have any other xattrs
  1793. * like ACLs, we also need to implement the security.* handlers at
  1794. * filesystem level, though.
  1795. */
  1796. static size_t shmem_xattr_security_list(struct inode *inode, char *list,
  1797. size_t list_len, const char *name,
  1798. size_t name_len)
  1799. {
  1800. return security_inode_listsecurity(inode, list, list_len);
  1801. }
  1802. static int shmem_xattr_security_get(struct inode *inode, const char *name,
  1803. void *buffer, size_t size)
  1804. {
  1805. if (strcmp(name, "") == 0)
  1806. return -EINVAL;
  1807. return security_inode_getsecurity(inode, name, buffer, size,
  1808. -EOPNOTSUPP);
  1809. }
  1810. static int shmem_xattr_security_set(struct inode *inode, const char *name,
  1811. const void *value, size_t size, int flags)
  1812. {
  1813. if (strcmp(name, "") == 0)
  1814. return -EINVAL;
  1815. return security_inode_setsecurity(inode, name, value, size, flags);
  1816. }
  1817. static struct xattr_handler shmem_xattr_security_handler = {
  1818. .prefix = XATTR_SECURITY_PREFIX,
  1819. .list = shmem_xattr_security_list,
  1820. .get = shmem_xattr_security_get,
  1821. .set = shmem_xattr_security_set,
  1822. };
  1823. static struct xattr_handler *shmem_xattr_handlers[] = {
  1824. &shmem_xattr_acl_access_handler,
  1825. &shmem_xattr_acl_default_handler,
  1826. &shmem_xattr_security_handler,
  1827. NULL
  1828. };
  1829. #endif
  1830. static struct dentry *shmem_get_parent(struct dentry *child)
  1831. {
  1832. return ERR_PTR(-ESTALE);
  1833. }
  1834. static int shmem_match(struct inode *ino, void *vfh)
  1835. {
  1836. __u32 *fh = vfh;
  1837. __u64 inum = fh[2];
  1838. inum = (inum << 32) | fh[1];
  1839. return ino->i_ino == inum && fh[0] == ino->i_generation;
  1840. }
  1841. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  1842. struct fid *fid, int fh_len, int fh_type)
  1843. {
  1844. struct inode *inode;
  1845. struct dentry *dentry = NULL;
  1846. u64 inum = fid->raw[2];
  1847. inum = (inum << 32) | fid->raw[1];
  1848. if (fh_len < 3)
  1849. return NULL;
  1850. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  1851. shmem_match, fid->raw);
  1852. if (inode) {
  1853. dentry = d_find_alias(inode);
  1854. iput(inode);
  1855. }
  1856. return dentry;
  1857. }
  1858. static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
  1859. int connectable)
  1860. {
  1861. struct inode *inode = dentry->d_inode;
  1862. if (*len < 3)
  1863. return 255;
  1864. if (hlist_unhashed(&inode->i_hash)) {
  1865. /* Unfortunately insert_inode_hash is not idempotent,
  1866. * so as we hash inodes here rather than at creation
  1867. * time, we need a lock to ensure we only try
  1868. * to do it once
  1869. */
  1870. static DEFINE_SPINLOCK(lock);
  1871. spin_lock(&lock);
  1872. if (hlist_unhashed(&inode->i_hash))
  1873. __insert_inode_hash(inode,
  1874. inode->i_ino + inode->i_generation);
  1875. spin_unlock(&lock);
  1876. }
  1877. fh[0] = inode->i_generation;
  1878. fh[1] = inode->i_ino;
  1879. fh[2] = ((__u64)inode->i_ino) >> 32;
  1880. *len = 3;
  1881. return 1;
  1882. }
  1883. static const struct export_operations shmem_export_ops = {
  1884. .get_parent = shmem_get_parent,
  1885. .encode_fh = shmem_encode_fh,
  1886. .fh_to_dentry = shmem_fh_to_dentry,
  1887. };
  1888. static int shmem_parse_options(char *options, int *mode, uid_t *uid,
  1889. gid_t *gid, unsigned long *blocks, unsigned long *inodes,
  1890. int *policy, nodemask_t *policy_nodes)
  1891. {
  1892. char *this_char, *value, *rest;
  1893. while (options != NULL) {
  1894. this_char = options;
  1895. for (;;) {
  1896. /*
  1897. * NUL-terminate this option: unfortunately,
  1898. * mount options form a comma-separated list,
  1899. * but mpol's nodelist may also contain commas.
  1900. */
  1901. options = strchr(options, ',');
  1902. if (options == NULL)
  1903. break;
  1904. options++;
  1905. if (!isdigit(*options)) {
  1906. options[-1] = '\0';
  1907. break;
  1908. }
  1909. }
  1910. if (!*this_char)
  1911. continue;
  1912. if ((value = strchr(this_char,'=')) != NULL) {
  1913. *value++ = 0;
  1914. } else {
  1915. printk(KERN_ERR
  1916. "tmpfs: No value for mount option '%s'\n",
  1917. this_char);
  1918. return 1;
  1919. }
  1920. if (!strcmp(this_char,"size")) {
  1921. unsigned long long size;
  1922. size = memparse(value,&rest);
  1923. if (*rest == '%') {
  1924. size <<= PAGE_SHIFT;
  1925. size *= totalram_pages;
  1926. do_div(size, 100);
  1927. rest++;
  1928. }
  1929. if (*rest)
  1930. goto bad_val;
  1931. *blocks = size >> PAGE_CACHE_SHIFT;
  1932. } else if (!strcmp(this_char,"nr_blocks")) {
  1933. *blocks = memparse(value,&rest);
  1934. if (*rest)
  1935. goto bad_val;
  1936. } else if (!strcmp(this_char,"nr_inodes")) {
  1937. *inodes = memparse(value,&rest);
  1938. if (*rest)
  1939. goto bad_val;
  1940. } else if (!strcmp(this_char,"mode")) {
  1941. if (!mode)
  1942. continue;
  1943. *mode = simple_strtoul(value,&rest,8);
  1944. if (*rest)
  1945. goto bad_val;
  1946. } else if (!strcmp(this_char,"uid")) {
  1947. if (!uid)
  1948. continue;
  1949. *uid = simple_strtoul(value,&rest,0);
  1950. if (*rest)
  1951. goto bad_val;
  1952. } else if (!strcmp(this_char,"gid")) {
  1953. if (!gid)
  1954. continue;
  1955. *gid = simple_strtoul(value,&rest,0);
  1956. if (*rest)
  1957. goto bad_val;
  1958. } else if (!strcmp(this_char,"mpol")) {
  1959. if (shmem_parse_mpol(value,policy,policy_nodes))
  1960. goto bad_val;
  1961. } else {
  1962. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  1963. this_char);
  1964. return 1;
  1965. }
  1966. }
  1967. return 0;
  1968. bad_val:
  1969. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  1970. value, this_char);
  1971. return 1;
  1972. }
  1973. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  1974. {
  1975. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1976. unsigned long max_blocks = sbinfo->max_blocks;
  1977. unsigned long max_inodes = sbinfo->max_inodes;
  1978. int policy = sbinfo->policy;
  1979. nodemask_t policy_nodes = sbinfo->policy_nodes;
  1980. unsigned long blocks;
  1981. unsigned long inodes;
  1982. int error = -EINVAL;
  1983. if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks,
  1984. &max_inodes, &policy, &policy_nodes))
  1985. return error;
  1986. spin_lock(&sbinfo->stat_lock);
  1987. blocks = sbinfo->max_blocks - sbinfo->free_blocks;
  1988. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  1989. if (max_blocks < blocks)
  1990. goto out;
  1991. if (max_inodes < inodes)
  1992. goto out;
  1993. /*
  1994. * Those tests also disallow limited->unlimited while any are in
  1995. * use, so i_blocks will always be zero when max_blocks is zero;
  1996. * but we must separately disallow unlimited->limited, because
  1997. * in that case we have no record of how much is already in use.
  1998. */
  1999. if (max_blocks && !sbinfo->max_blocks)
  2000. goto out;
  2001. if (max_inodes && !sbinfo->max_inodes)
  2002. goto out;
  2003. error = 0;
  2004. sbinfo->max_blocks = max_blocks;
  2005. sbinfo->free_blocks = max_blocks - blocks;
  2006. sbinfo->max_inodes = max_inodes;
  2007. sbinfo->free_inodes = max_inodes - inodes;
  2008. sbinfo->policy = policy;
  2009. sbinfo->policy_nodes = policy_nodes;
  2010. out:
  2011. spin_unlock(&sbinfo->stat_lock);
  2012. return error;
  2013. }
  2014. #endif
  2015. static void shmem_put_super(struct super_block *sb)
  2016. {
  2017. kfree(sb->s_fs_info);
  2018. sb->s_fs_info = NULL;
  2019. }
  2020. static int shmem_fill_super(struct super_block *sb,
  2021. void *data, int silent)
  2022. {
  2023. struct inode *inode;
  2024. struct dentry *root;
  2025. int mode = S_IRWXUGO | S_ISVTX;
  2026. uid_t uid = current->fsuid;
  2027. gid_t gid = current->fsgid;
  2028. int err = -ENOMEM;
  2029. struct shmem_sb_info *sbinfo;
  2030. unsigned long blocks = 0;
  2031. unsigned long inodes = 0;
  2032. int policy = MPOL_DEFAULT;
  2033. nodemask_t policy_nodes = node_states[N_HIGH_MEMORY];
  2034. #ifdef CONFIG_TMPFS
  2035. /*
  2036. * Per default we only allow half of the physical ram per
  2037. * tmpfs instance, limiting inodes to one per page of lowmem;
  2038. * but the internal instance is left unlimited.
  2039. */
  2040. if (!(sb->s_flags & MS_NOUSER)) {
  2041. blocks = totalram_pages / 2;
  2042. inodes = totalram_pages - totalhigh_pages;
  2043. if (inodes > blocks)
  2044. inodes = blocks;
  2045. if (shmem_parse_options(data, &mode, &uid, &gid, &blocks,
  2046. &inodes, &policy, &policy_nodes))
  2047. return -EINVAL;
  2048. }
  2049. sb->s_export_op = &shmem_export_ops;
  2050. #else
  2051. sb->s_flags |= MS_NOUSER;
  2052. #endif
  2053. /* Round up to L1_CACHE_BYTES to resist false sharing */
  2054. sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
  2055. L1_CACHE_BYTES), GFP_KERNEL);
  2056. if (!sbinfo)
  2057. return -ENOMEM;
  2058. spin_lock_init(&sbinfo->stat_lock);
  2059. sbinfo->max_blocks = blocks;
  2060. sbinfo->free_blocks = blocks;
  2061. sbinfo->max_inodes = inodes;
  2062. sbinfo->free_inodes = inodes;
  2063. sbinfo->policy = policy;
  2064. sbinfo->policy_nodes = policy_nodes;
  2065. sb->s_fs_info = sbinfo;
  2066. sb->s_maxbytes = SHMEM_MAX_BYTES;
  2067. sb->s_blocksize = PAGE_CACHE_SIZE;
  2068. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  2069. sb->s_magic = TMPFS_MAGIC;
  2070. sb->s_op = &shmem_ops;
  2071. sb->s_time_gran = 1;
  2072. #ifdef CONFIG_TMPFS_POSIX_ACL
  2073. sb->s_xattr = shmem_xattr_handlers;
  2074. sb->s_flags |= MS_POSIXACL;
  2075. #endif
  2076. inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
  2077. if (!inode)
  2078. goto failed;
  2079. inode->i_uid = uid;
  2080. inode->i_gid = gid;
  2081. root = d_alloc_root(inode);
  2082. if (!root)
  2083. goto failed_iput;
  2084. sb->s_root = root;
  2085. return 0;
  2086. failed_iput:
  2087. iput(inode);
  2088. failed:
  2089. shmem_put_super(sb);
  2090. return err;
  2091. }
  2092. static struct kmem_cache *shmem_inode_cachep;
  2093. static struct inode *shmem_alloc_inode(struct super_block *sb)
  2094. {
  2095. struct shmem_inode_info *p;
  2096. p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  2097. if (!p)
  2098. return NULL;
  2099. return &p->vfs_inode;
  2100. }
  2101. static void shmem_destroy_inode(struct inode *inode)
  2102. {
  2103. if ((inode->i_mode & S_IFMT) == S_IFREG) {
  2104. /* only struct inode is valid if it's an inline symlink */
  2105. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  2106. }
  2107. shmem_acl_destroy_inode(inode);
  2108. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  2109. }
  2110. static void init_once(struct kmem_cache *cachep, void *foo)
  2111. {
  2112. struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
  2113. inode_init_once(&p->vfs_inode);
  2114. #ifdef CONFIG_TMPFS_POSIX_ACL
  2115. p->i_acl = NULL;
  2116. p->i_default_acl = NULL;
  2117. #endif
  2118. }
  2119. static int init_inodecache(void)
  2120. {
  2121. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  2122. sizeof(struct shmem_inode_info),
  2123. 0, SLAB_PANIC, init_once);
  2124. return 0;
  2125. }
  2126. static void destroy_inodecache(void)
  2127. {
  2128. kmem_cache_destroy(shmem_inode_cachep);
  2129. }
  2130. static const struct address_space_operations shmem_aops = {
  2131. .writepage = shmem_writepage,
  2132. .set_page_dirty = __set_page_dirty_no_writeback,
  2133. #ifdef CONFIG_TMPFS
  2134. .readpage = shmem_readpage,
  2135. .write_begin = shmem_write_begin,
  2136. .write_end = shmem_write_end,
  2137. #endif
  2138. .migratepage = migrate_page,
  2139. };
  2140. static const struct file_operations shmem_file_operations = {
  2141. .mmap = shmem_mmap,
  2142. #ifdef CONFIG_TMPFS
  2143. .llseek = generic_file_llseek,
  2144. .read = shmem_file_read,
  2145. .write = shmem_file_write,
  2146. .fsync = simple_sync_file,
  2147. .splice_read = generic_file_splice_read,
  2148. .splice_write = generic_file_splice_write,
  2149. #endif
  2150. };
  2151. static const struct inode_operations shmem_inode_operations = {
  2152. .truncate = shmem_truncate,
  2153. .setattr = shmem_notify_change,
  2154. .truncate_range = shmem_truncate_range,
  2155. #ifdef CONFIG_TMPFS_POSIX_ACL
  2156. .setxattr = generic_setxattr,
  2157. .getxattr = generic_getxattr,
  2158. .listxattr = generic_listxattr,
  2159. .removexattr = generic_removexattr,
  2160. .permission = shmem_permission,
  2161. #endif
  2162. };
  2163. static const struct inode_operations shmem_dir_inode_operations = {
  2164. #ifdef CONFIG_TMPFS
  2165. .create = shmem_create,
  2166. .lookup = simple_lookup,
  2167. .link = shmem_link,
  2168. .unlink = shmem_unlink,
  2169. .symlink = shmem_symlink,
  2170. .mkdir = shmem_mkdir,
  2171. .rmdir = shmem_rmdir,
  2172. .mknod = shmem_mknod,
  2173. .rename = shmem_rename,
  2174. #endif
  2175. #ifdef CONFIG_TMPFS_POSIX_ACL
  2176. .setattr = shmem_notify_change,
  2177. .setxattr = generic_setxattr,
  2178. .getxattr = generic_getxattr,
  2179. .listxattr = generic_listxattr,
  2180. .removexattr = generic_removexattr,
  2181. .permission = shmem_permission,
  2182. #endif
  2183. };
  2184. static const struct inode_operations shmem_special_inode_operations = {
  2185. #ifdef CONFIG_TMPFS_POSIX_ACL
  2186. .setattr = shmem_notify_change,
  2187. .setxattr = generic_setxattr,
  2188. .getxattr = generic_getxattr,
  2189. .listxattr = generic_listxattr,
  2190. .removexattr = generic_removexattr,
  2191. .permission = shmem_permission,
  2192. #endif
  2193. };
  2194. static const struct super_operations shmem_ops = {
  2195. .alloc_inode = shmem_alloc_inode,
  2196. .destroy_inode = shmem_destroy_inode,
  2197. #ifdef CONFIG_TMPFS
  2198. .statfs = shmem_statfs,
  2199. .remount_fs = shmem_remount_fs,
  2200. #endif
  2201. .delete_inode = shmem_delete_inode,
  2202. .drop_inode = generic_delete_inode,
  2203. .put_super = shmem_put_super,
  2204. };
  2205. static struct vm_operations_struct shmem_vm_ops = {
  2206. .fault = shmem_fault,
  2207. #ifdef CONFIG_NUMA
  2208. .set_policy = shmem_set_policy,
  2209. .get_policy = shmem_get_policy,
  2210. #endif
  2211. };
  2212. static int shmem_get_sb(struct file_system_type *fs_type,
  2213. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  2214. {
  2215. return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt);
  2216. }
  2217. static struct file_system_type tmpfs_fs_type = {
  2218. .owner = THIS_MODULE,
  2219. .name = "tmpfs",
  2220. .get_sb = shmem_get_sb,
  2221. .kill_sb = kill_litter_super,
  2222. };
  2223. static struct vfsmount *shm_mnt;
  2224. static int __init init_tmpfs(void)
  2225. {
  2226. int error;
  2227. error = bdi_init(&shmem_backing_dev_info);
  2228. if (error)
  2229. goto out4;
  2230. error = init_inodecache();
  2231. if (error)
  2232. goto out3;
  2233. error = register_filesystem(&tmpfs_fs_type);
  2234. if (error) {
  2235. printk(KERN_ERR "Could not register tmpfs\n");
  2236. goto out2;
  2237. }
  2238. shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
  2239. tmpfs_fs_type.name, NULL);
  2240. if (IS_ERR(shm_mnt)) {
  2241. error = PTR_ERR(shm_mnt);
  2242. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2243. goto out1;
  2244. }
  2245. return 0;
  2246. out1:
  2247. unregister_filesystem(&tmpfs_fs_type);
  2248. out2:
  2249. destroy_inodecache();
  2250. out3:
  2251. bdi_destroy(&shmem_backing_dev_info);
  2252. out4:
  2253. shm_mnt = ERR_PTR(error);
  2254. return error;
  2255. }
  2256. module_init(init_tmpfs)
  2257. /*
  2258. * shmem_file_setup - get an unlinked file living in tmpfs
  2259. *
  2260. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2261. * @size: size to be set for the file
  2262. *
  2263. */
  2264. struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
  2265. {
  2266. int error;
  2267. struct file *file;
  2268. struct inode *inode;
  2269. struct dentry *dentry, *root;
  2270. struct qstr this;
  2271. if (IS_ERR(shm_mnt))
  2272. return (void *)shm_mnt;
  2273. if (size < 0 || size > SHMEM_MAX_BYTES)
  2274. return ERR_PTR(-EINVAL);
  2275. if (shmem_acct_size(flags, size))
  2276. return ERR_PTR(-ENOMEM);
  2277. error = -ENOMEM;
  2278. this.name = name;
  2279. this.len = strlen(name);
  2280. this.hash = 0; /* will go */
  2281. root = shm_mnt->mnt_root;
  2282. dentry = d_alloc(root, &this);
  2283. if (!dentry)
  2284. goto put_memory;
  2285. error = -ENFILE;
  2286. file = get_empty_filp();
  2287. if (!file)
  2288. goto put_dentry;
  2289. error = -ENOSPC;
  2290. inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
  2291. if (!inode)
  2292. goto close_file;
  2293. SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
  2294. d_instantiate(dentry, inode);
  2295. inode->i_size = size;
  2296. inode->i_nlink = 0; /* It is unlinked */
  2297. init_file(file, shm_mnt, dentry, FMODE_WRITE | FMODE_READ,
  2298. &shmem_file_operations);
  2299. return file;
  2300. close_file:
  2301. put_filp(file);
  2302. put_dentry:
  2303. dput(dentry);
  2304. put_memory:
  2305. shmem_unacct_size(flags, size);
  2306. return ERR_PTR(error);
  2307. }
  2308. /*
  2309. * shmem_zero_setup - setup a shared anonymous mapping
  2310. *
  2311. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2312. */
  2313. int shmem_zero_setup(struct vm_area_struct *vma)
  2314. {
  2315. struct file *file;
  2316. loff_t size = vma->vm_end - vma->vm_start;
  2317. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2318. if (IS_ERR(file))
  2319. return PTR_ERR(file);
  2320. if (vma->vm_file)
  2321. fput(vma->vm_file);
  2322. vma->vm_file = file;
  2323. vma->vm_ops = &shmem_vm_ops;
  2324. return 0;
  2325. }