shmem.c 72 KB

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