shmem.c 69 KB

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