shmem.c 78 KB

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