shmem.c 66 KB

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