shmem.c 78 KB

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