shmem.c 71 KB

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