shmem.c 83 KB

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