shmem.c 65 KB

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