shmem.c 66 KB

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