shmem.c 68 KB

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