shmem.c 72 KB

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