shmem.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496
  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-2011 Hugh Dickins.
  10. * Copyright (C) 2011 Google Inc.
  11. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  12. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  13. *
  14. * Extended attribute support for tmpfs:
  15. * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  16. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  17. *
  18. * tiny-shmem:
  19. * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
  20. *
  21. * This file is released under the GPL.
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/init.h>
  25. #include <linux/vfs.h>
  26. #include <linux/mount.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/file.h>
  29. #include <linux/mm.h>
  30. #include <linux/export.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/posix_acl_xattr.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/pagevec.h>
  51. #include <linux/percpu_counter.h>
  52. #include <linux/falloc.h>
  53. #include <linux/splice.h>
  54. #include <linux/security.h>
  55. #include <linux/swapops.h>
  56. #include <linux/mempolicy.h>
  57. #include <linux/namei.h>
  58. #include <linux/ctype.h>
  59. #include <linux/migrate.h>
  60. #include <linux/highmem.h>
  61. #include <linux/seq_file.h>
  62. #include <linux/syscalls.h>
  63. #include <linux/magic.h>
  64. #include <uapi/linux/memfd.h>
  65. #include <linux/fcntl.h>
  66. #include <asm/uaccess.h>
  67. #include <asm/pgtable.h>
  68. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  69. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  70. /* Pretend that each entry is of this size in directory's i_size */
  71. #define BOGO_DIRENT_SIZE 20
  72. /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
  73. #define SHORT_SYMLINK_LEN 128
  74. /*
  75. * shmem_fallocate communicates with shmem_fault or shmem_writepage via
  76. * inode->i_private (with i_mutex making sure that it has only one user at
  77. * a time): we would prefer not to enlarge the shmem inode just for that.
  78. */
  79. struct shmem_falloc {
  80. wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
  81. pgoff_t start; /* start of range currently being fallocated */
  82. pgoff_t next; /* the next page offset to be fallocated */
  83. pgoff_t nr_falloced; /* how many new pages have been fallocated */
  84. pgoff_t nr_unswapped; /* how often writepage refused to swap out */
  85. };
  86. /* Flag allocation requirements to shmem_getpage */
  87. enum sgp_type {
  88. SGP_READ, /* don't exceed i_size, don't allocate page */
  89. SGP_CACHE, /* don't exceed i_size, may allocate page */
  90. SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
  91. SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
  92. SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
  93. };
  94. #ifdef CONFIG_TMPFS
  95. static unsigned long shmem_default_max_blocks(void)
  96. {
  97. return totalram_pages / 2;
  98. }
  99. static unsigned long shmem_default_max_inodes(void)
  100. {
  101. return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
  102. }
  103. #endif
  104. static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
  105. static int shmem_replace_page(struct page **pagep, gfp_t gfp,
  106. struct shmem_inode_info *info, pgoff_t index);
  107. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  108. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
  109. static inline int shmem_getpage(struct inode *inode, pgoff_t index,
  110. struct page **pagep, enum sgp_type sgp, int *fault_type)
  111. {
  112. return shmem_getpage_gfp(inode, index, pagep, sgp,
  113. mapping_gfp_mask(inode->i_mapping), fault_type);
  114. }
  115. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  116. {
  117. return sb->s_fs_info;
  118. }
  119. /*
  120. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  121. * for shared memory and for shared anonymous (/dev/zero) mappings
  122. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  123. * consistent with the pre-accounting of private mappings ...
  124. */
  125. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  126. {
  127. return (flags & VM_NORESERVE) ?
  128. 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
  129. }
  130. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  131. {
  132. if (!(flags & VM_NORESERVE))
  133. vm_unacct_memory(VM_ACCT(size));
  134. }
  135. static inline int shmem_reacct_size(unsigned long flags,
  136. loff_t oldsize, loff_t newsize)
  137. {
  138. if (!(flags & VM_NORESERVE)) {
  139. if (VM_ACCT(newsize) > VM_ACCT(oldsize))
  140. return security_vm_enough_memory_mm(current->mm,
  141. VM_ACCT(newsize) - VM_ACCT(oldsize));
  142. else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
  143. vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
  144. }
  145. return 0;
  146. }
  147. /*
  148. * ... whereas tmpfs objects are accounted incrementally as
  149. * pages are allocated, in order to allow huge sparse files.
  150. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  151. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  152. */
  153. static inline int shmem_acct_block(unsigned long flags)
  154. {
  155. return (flags & VM_NORESERVE) ?
  156. security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
  157. }
  158. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  159. {
  160. if (flags & VM_NORESERVE)
  161. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  162. }
  163. static const struct super_operations shmem_ops;
  164. static const struct address_space_operations shmem_aops;
  165. static const struct file_operations shmem_file_operations;
  166. static const struct inode_operations shmem_inode_operations;
  167. static const struct inode_operations shmem_dir_inode_operations;
  168. static const struct inode_operations shmem_special_inode_operations;
  169. static const struct vm_operations_struct shmem_vm_ops;
  170. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  171. .ra_pages = 0, /* No readahead */
  172. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
  173. };
  174. static LIST_HEAD(shmem_swaplist);
  175. static DEFINE_MUTEX(shmem_swaplist_mutex);
  176. static int shmem_reserve_inode(struct super_block *sb)
  177. {
  178. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  179. if (sbinfo->max_inodes) {
  180. spin_lock(&sbinfo->stat_lock);
  181. if (!sbinfo->free_inodes) {
  182. spin_unlock(&sbinfo->stat_lock);
  183. return -ENOSPC;
  184. }
  185. sbinfo->free_inodes--;
  186. spin_unlock(&sbinfo->stat_lock);
  187. }
  188. return 0;
  189. }
  190. static void shmem_free_inode(struct super_block *sb)
  191. {
  192. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  193. if (sbinfo->max_inodes) {
  194. spin_lock(&sbinfo->stat_lock);
  195. sbinfo->free_inodes++;
  196. spin_unlock(&sbinfo->stat_lock);
  197. }
  198. }
  199. /**
  200. * shmem_recalc_inode - recalculate the block usage of an inode
  201. * @inode: inode to recalc
  202. *
  203. * We have to calculate the free blocks since the mm can drop
  204. * undirtied hole pages behind our back.
  205. *
  206. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  207. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  208. *
  209. * It has to be called with the spinlock held.
  210. */
  211. static void shmem_recalc_inode(struct inode *inode)
  212. {
  213. struct shmem_inode_info *info = SHMEM_I(inode);
  214. long freed;
  215. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  216. if (freed > 0) {
  217. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  218. if (sbinfo->max_blocks)
  219. percpu_counter_add(&sbinfo->used_blocks, -freed);
  220. info->alloced -= freed;
  221. inode->i_blocks -= freed * BLOCKS_PER_PAGE;
  222. shmem_unacct_blocks(info->flags, freed);
  223. }
  224. }
  225. /*
  226. * Replace item expected in radix tree by a new item, while holding tree lock.
  227. */
  228. static int shmem_radix_tree_replace(struct address_space *mapping,
  229. pgoff_t index, void *expected, void *replacement)
  230. {
  231. void **pslot;
  232. void *item = NULL;
  233. VM_BUG_ON(!expected);
  234. pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
  235. if (pslot)
  236. item = radix_tree_deref_slot_protected(pslot,
  237. &mapping->tree_lock);
  238. if (item != expected)
  239. return -ENOENT;
  240. if (replacement)
  241. radix_tree_replace_slot(pslot, replacement);
  242. else
  243. radix_tree_delete(&mapping->page_tree, index);
  244. return 0;
  245. }
  246. /*
  247. * Sometimes, before we decide whether to proceed or to fail, we must check
  248. * that an entry was not already brought back from swap by a racing thread.
  249. *
  250. * Checking page is not enough: by the time a SwapCache page is locked, it
  251. * might be reused, and again be SwapCache, using the same swap as before.
  252. */
  253. static bool shmem_confirm_swap(struct address_space *mapping,
  254. pgoff_t index, swp_entry_t swap)
  255. {
  256. void *item;
  257. rcu_read_lock();
  258. item = radix_tree_lookup(&mapping->page_tree, index);
  259. rcu_read_unlock();
  260. return item == swp_to_radix_entry(swap);
  261. }
  262. /*
  263. * Like add_to_page_cache_locked, but error if expected item has gone.
  264. */
  265. static int shmem_add_to_page_cache(struct page *page,
  266. struct address_space *mapping,
  267. pgoff_t index, gfp_t gfp, void *expected)
  268. {
  269. int error;
  270. VM_BUG_ON(!PageLocked(page));
  271. VM_BUG_ON(!PageSwapBacked(page));
  272. page_cache_get(page);
  273. page->mapping = mapping;
  274. page->index = index;
  275. spin_lock_irq(&mapping->tree_lock);
  276. if (!expected)
  277. error = radix_tree_insert(&mapping->page_tree, index, page);
  278. else
  279. error = shmem_radix_tree_replace(mapping, index, expected,
  280. page);
  281. if (!error) {
  282. mapping->nrpages++;
  283. __inc_zone_page_state(page, NR_FILE_PAGES);
  284. __inc_zone_page_state(page, NR_SHMEM);
  285. spin_unlock_irq(&mapping->tree_lock);
  286. } else {
  287. page->mapping = NULL;
  288. spin_unlock_irq(&mapping->tree_lock);
  289. page_cache_release(page);
  290. }
  291. return error;
  292. }
  293. /*
  294. * Like delete_from_page_cache, but substitutes swap for page.
  295. */
  296. static void shmem_delete_from_page_cache(struct page *page, void *radswap)
  297. {
  298. struct address_space *mapping = page->mapping;
  299. int error;
  300. spin_lock_irq(&mapping->tree_lock);
  301. error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
  302. page->mapping = NULL;
  303. mapping->nrpages--;
  304. __dec_zone_page_state(page, NR_FILE_PAGES);
  305. __dec_zone_page_state(page, NR_SHMEM);
  306. spin_unlock_irq(&mapping->tree_lock);
  307. page_cache_release(page);
  308. BUG_ON(error);
  309. }
  310. /*
  311. * Like find_get_pages, but collecting swap entries as well as pages.
  312. */
  313. static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
  314. pgoff_t start, unsigned int nr_pages,
  315. struct page **pages, pgoff_t *indices)
  316. {
  317. void **slot;
  318. unsigned int ret = 0;
  319. struct radix_tree_iter iter;
  320. if (!nr_pages)
  321. return 0;
  322. rcu_read_lock();
  323. restart:
  324. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
  325. struct page *page;
  326. repeat:
  327. page = radix_tree_deref_slot(slot);
  328. if (unlikely(!page))
  329. continue;
  330. if (radix_tree_exception(page)) {
  331. if (radix_tree_deref_retry(page))
  332. goto restart;
  333. /*
  334. * Otherwise, we must be storing a swap entry
  335. * here as an exceptional entry: so return it
  336. * without attempting to raise page count.
  337. */
  338. goto export;
  339. }
  340. if (!page_cache_get_speculative(page))
  341. goto repeat;
  342. /* Has the page moved? */
  343. if (unlikely(page != *slot)) {
  344. page_cache_release(page);
  345. goto repeat;
  346. }
  347. export:
  348. indices[ret] = iter.index;
  349. pages[ret] = page;
  350. if (++ret == nr_pages)
  351. break;
  352. }
  353. rcu_read_unlock();
  354. return ret;
  355. }
  356. /*
  357. * Remove swap entry from radix tree, free the swap and its page cache.
  358. */
  359. static int shmem_free_swap(struct address_space *mapping,
  360. pgoff_t index, void *radswap)
  361. {
  362. int error;
  363. spin_lock_irq(&mapping->tree_lock);
  364. error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
  365. spin_unlock_irq(&mapping->tree_lock);
  366. if (!error)
  367. free_swap_and_cache(radix_to_swp_entry(radswap));
  368. return error;
  369. }
  370. /*
  371. * Pagevec may contain swap entries, so shuffle up pages before releasing.
  372. */
  373. static void shmem_deswap_pagevec(struct pagevec *pvec)
  374. {
  375. int i, j;
  376. for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
  377. struct page *page = pvec->pages[i];
  378. if (!radix_tree_exceptional_entry(page))
  379. pvec->pages[j++] = page;
  380. }
  381. pvec->nr = j;
  382. }
  383. /*
  384. * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
  385. */
  386. void shmem_unlock_mapping(struct address_space *mapping)
  387. {
  388. struct pagevec pvec;
  389. pgoff_t indices[PAGEVEC_SIZE];
  390. pgoff_t index = 0;
  391. pagevec_init(&pvec, 0);
  392. /*
  393. * Minor point, but we might as well stop if someone else SHM_LOCKs it.
  394. */
  395. while (!mapping_unevictable(mapping)) {
  396. /*
  397. * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
  398. * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
  399. */
  400. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  401. PAGEVEC_SIZE, pvec.pages, indices);
  402. if (!pvec.nr)
  403. break;
  404. index = indices[pvec.nr - 1] + 1;
  405. shmem_deswap_pagevec(&pvec);
  406. check_move_unevictable_pages(pvec.pages, pvec.nr);
  407. pagevec_release(&pvec);
  408. cond_resched();
  409. }
  410. }
  411. /*
  412. * Remove range of pages and swap entries from radix tree, and free them.
  413. * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
  414. */
  415. static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
  416. bool unfalloc)
  417. {
  418. struct address_space *mapping = inode->i_mapping;
  419. struct shmem_inode_info *info = SHMEM_I(inode);
  420. pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  421. pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
  422. unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
  423. unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
  424. struct pagevec pvec;
  425. pgoff_t indices[PAGEVEC_SIZE];
  426. long nr_swaps_freed = 0;
  427. pgoff_t index;
  428. int i;
  429. if (lend == -1)
  430. end = -1; /* unsigned, so actually very big */
  431. pagevec_init(&pvec, 0);
  432. index = start;
  433. while (index < end) {
  434. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  435. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  436. pvec.pages, indices);
  437. if (!pvec.nr)
  438. break;
  439. mem_cgroup_uncharge_start();
  440. for (i = 0; i < pagevec_count(&pvec); i++) {
  441. struct page *page = pvec.pages[i];
  442. index = indices[i];
  443. if (index >= end)
  444. break;
  445. if (radix_tree_exceptional_entry(page)) {
  446. if (unfalloc)
  447. continue;
  448. nr_swaps_freed += !shmem_free_swap(mapping,
  449. index, page);
  450. continue;
  451. }
  452. if (!trylock_page(page))
  453. continue;
  454. if (!unfalloc || !PageUptodate(page)) {
  455. if (page->mapping == mapping) {
  456. VM_BUG_ON(PageWriteback(page));
  457. truncate_inode_page(mapping, page);
  458. }
  459. }
  460. unlock_page(page);
  461. }
  462. shmem_deswap_pagevec(&pvec);
  463. pagevec_release(&pvec);
  464. mem_cgroup_uncharge_end();
  465. cond_resched();
  466. index++;
  467. }
  468. if (partial_start) {
  469. struct page *page = NULL;
  470. shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
  471. if (page) {
  472. unsigned int top = PAGE_CACHE_SIZE;
  473. if (start > end) {
  474. top = partial_end;
  475. partial_end = 0;
  476. }
  477. zero_user_segment(page, partial_start, top);
  478. set_page_dirty(page);
  479. unlock_page(page);
  480. page_cache_release(page);
  481. }
  482. }
  483. if (partial_end) {
  484. struct page *page = NULL;
  485. shmem_getpage(inode, end, &page, SGP_READ, NULL);
  486. if (page) {
  487. zero_user_segment(page, 0, partial_end);
  488. set_page_dirty(page);
  489. unlock_page(page);
  490. page_cache_release(page);
  491. }
  492. }
  493. if (start >= end)
  494. return;
  495. index = start;
  496. while (index < end) {
  497. cond_resched();
  498. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  499. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  500. pvec.pages, indices);
  501. if (!pvec.nr) {
  502. /* If all gone or hole-punch or unfalloc, we're done */
  503. if (index == start || end != -1)
  504. break;
  505. /* But if truncating, restart to make sure all gone */
  506. index = start;
  507. continue;
  508. }
  509. mem_cgroup_uncharge_start();
  510. for (i = 0; i < pagevec_count(&pvec); i++) {
  511. struct page *page = pvec.pages[i];
  512. index = indices[i];
  513. if (index >= end)
  514. break;
  515. if (radix_tree_exceptional_entry(page)) {
  516. if (unfalloc)
  517. continue;
  518. if (shmem_free_swap(mapping, index, page)) {
  519. /* Swap was replaced by page: retry */
  520. index--;
  521. break;
  522. }
  523. nr_swaps_freed++;
  524. continue;
  525. }
  526. lock_page(page);
  527. if (!unfalloc || !PageUptodate(page)) {
  528. if (page->mapping == mapping) {
  529. VM_BUG_ON(PageWriteback(page));
  530. truncate_inode_page(mapping, page);
  531. } else {
  532. /* Page was replaced by swap: retry */
  533. unlock_page(page);
  534. index--;
  535. break;
  536. }
  537. }
  538. unlock_page(page);
  539. }
  540. shmem_deswap_pagevec(&pvec);
  541. pagevec_release(&pvec);
  542. mem_cgroup_uncharge_end();
  543. index++;
  544. }
  545. spin_lock(&info->lock);
  546. info->swapped -= nr_swaps_freed;
  547. shmem_recalc_inode(inode);
  548. spin_unlock(&info->lock);
  549. }
  550. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  551. {
  552. shmem_undo_range(inode, lstart, lend, false);
  553. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  554. }
  555. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  556. static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
  557. {
  558. struct inode *inode = dentry->d_inode;
  559. struct shmem_inode_info *info = SHMEM_I(inode);
  560. int error;
  561. error = inode_change_ok(inode, attr);
  562. if (error)
  563. return error;
  564. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  565. loff_t oldsize = inode->i_size;
  566. loff_t newsize = attr->ia_size;
  567. /* protected by i_mutex */
  568. if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
  569. (newsize > oldsize && (info->seals & F_SEAL_GROW)))
  570. return -EPERM;
  571. if (newsize != oldsize) {
  572. error = shmem_reacct_size(SHMEM_I(inode)->flags,
  573. oldsize, newsize);
  574. if (error)
  575. return error;
  576. i_size_write(inode, newsize);
  577. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  578. }
  579. if (newsize < oldsize) {
  580. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  581. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  582. shmem_truncate_range(inode, newsize, (loff_t)-1);
  583. /* unmap again to remove racily COWed private pages */
  584. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  585. }
  586. }
  587. setattr_copy(inode, attr);
  588. if (attr->ia_valid & ATTR_MODE)
  589. error = posix_acl_chmod(inode, inode->i_mode);
  590. return error;
  591. }
  592. static void shmem_evict_inode(struct inode *inode)
  593. {
  594. struct shmem_inode_info *info = SHMEM_I(inode);
  595. if (inode->i_mapping->a_ops == &shmem_aops) {
  596. shmem_unacct_size(info->flags, inode->i_size);
  597. inode->i_size = 0;
  598. shmem_truncate_range(inode, 0, (loff_t)-1);
  599. if (!list_empty(&info->swaplist)) {
  600. mutex_lock(&shmem_swaplist_mutex);
  601. list_del_init(&info->swaplist);
  602. mutex_unlock(&shmem_swaplist_mutex);
  603. }
  604. } else
  605. kfree(info->symlink);
  606. simple_xattrs_free(&info->xattrs);
  607. WARN_ON(inode->i_blocks);
  608. shmem_free_inode(inode->i_sb);
  609. end_writeback(inode);
  610. }
  611. /*
  612. * If swap found in inode, free it and move page from swapcache to filecache.
  613. */
  614. static int shmem_unuse_inode(struct shmem_inode_info *info,
  615. swp_entry_t swap, struct page **pagep)
  616. {
  617. struct address_space *mapping = info->vfs_inode.i_mapping;
  618. void *radswap;
  619. pgoff_t index;
  620. gfp_t gfp;
  621. int error = 0;
  622. radswap = swp_to_radix_entry(swap);
  623. index = radix_tree_locate_item(&mapping->page_tree, radswap);
  624. if (index == -1)
  625. return 0;
  626. /*
  627. * Move _head_ to start search for next from here.
  628. * But be careful: shmem_evict_inode checks list_empty without taking
  629. * mutex, and there's an instant in list_move_tail when info->swaplist
  630. * would appear empty, if it were the only one on shmem_swaplist.
  631. */
  632. if (shmem_swaplist.next != &info->swaplist)
  633. list_move_tail(&shmem_swaplist, &info->swaplist);
  634. gfp = mapping_gfp_mask(mapping);
  635. if (shmem_should_replace_page(*pagep, gfp)) {
  636. mutex_unlock(&shmem_swaplist_mutex);
  637. error = shmem_replace_page(pagep, gfp, info, index);
  638. mutex_lock(&shmem_swaplist_mutex);
  639. /*
  640. * We needed to drop mutex to make that restrictive page
  641. * allocation, but the inode might have been freed while we
  642. * dropped it: although a racing shmem_evict_inode() cannot
  643. * complete without emptying the radix_tree, our page lock
  644. * on this swapcache page is not enough to prevent that -
  645. * free_swap_and_cache() of our swap entry will only
  646. * trylock_page(), removing swap from radix_tree whatever.
  647. *
  648. * We must not proceed to shmem_add_to_page_cache() if the
  649. * inode has been freed, but of course we cannot rely on
  650. * inode or mapping or info to check that. However, we can
  651. * safely check if our swap entry is still in use (and here
  652. * it can't have got reused for another page): if it's still
  653. * in use, then the inode cannot have been freed yet, and we
  654. * can safely proceed (if it's no longer in use, that tells
  655. * nothing about the inode, but we don't need to unuse swap).
  656. */
  657. if (!page_swapcount(*pagep))
  658. error = -ENOENT;
  659. }
  660. /*
  661. * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
  662. * but also to hold up shmem_evict_inode(): so inode cannot be freed
  663. * beneath us (pagelock doesn't help until the page is in pagecache).
  664. */
  665. if (!error)
  666. error = shmem_add_to_page_cache(*pagep, mapping, index,
  667. GFP_NOWAIT, radswap);
  668. if (error != -ENOMEM) {
  669. /*
  670. * Truncation and eviction use free_swap_and_cache(), which
  671. * only does trylock page: if we raced, best clean up here.
  672. */
  673. delete_from_swap_cache(*pagep);
  674. set_page_dirty(*pagep);
  675. if (!error) {
  676. spin_lock(&info->lock);
  677. info->swapped--;
  678. spin_unlock(&info->lock);
  679. swap_free(swap);
  680. }
  681. error = 1; /* not an error, but entry was found */
  682. }
  683. return error;
  684. }
  685. /*
  686. * Search through swapped inodes to find and replace swap by page.
  687. */
  688. int shmem_unuse(swp_entry_t swap, struct page *page)
  689. {
  690. struct list_head *this, *next;
  691. struct shmem_inode_info *info;
  692. int found = 0;
  693. int error = 0;
  694. /*
  695. * There's a faint possibility that swap page was replaced before
  696. * caller locked it: caller will come back later with the right page.
  697. */
  698. if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
  699. goto out;
  700. /*
  701. * Charge page using GFP_KERNEL while we can wait, before taking
  702. * the shmem_swaplist_mutex which might hold up shmem_writepage().
  703. * Charged back to the user (not to caller) when swap account is used.
  704. */
  705. error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
  706. if (error)
  707. goto out;
  708. /* No radix_tree_preload: swap entry keeps a place for page in tree */
  709. mutex_lock(&shmem_swaplist_mutex);
  710. list_for_each_safe(this, next, &shmem_swaplist) {
  711. info = list_entry(this, struct shmem_inode_info, swaplist);
  712. if (info->swapped)
  713. found = shmem_unuse_inode(info, swap, &page);
  714. else
  715. list_del_init(&info->swaplist);
  716. cond_resched();
  717. if (found)
  718. break;
  719. }
  720. mutex_unlock(&shmem_swaplist_mutex);
  721. if (found < 0)
  722. error = found;
  723. out:
  724. unlock_page(page);
  725. page_cache_release(page);
  726. return error;
  727. }
  728. /*
  729. * Move the page from the page cache to the swap cache.
  730. */
  731. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  732. {
  733. struct shmem_inode_info *info;
  734. struct address_space *mapping;
  735. struct inode *inode;
  736. swp_entry_t swap;
  737. pgoff_t index;
  738. BUG_ON(!PageLocked(page));
  739. mapping = page->mapping;
  740. index = page->index;
  741. inode = mapping->host;
  742. info = SHMEM_I(inode);
  743. if (info->flags & VM_LOCKED)
  744. goto redirty;
  745. #ifdef CONFIG_RUNTIME_COMPCACHE
  746. /*
  747. * Modification for runtime compcache
  748. * shmem_writepage can be reason of kernel panic when using swap.
  749. * This modification prevent using swap by shmem.
  750. */
  751. goto redirty;
  752. #else
  753. if (!total_swap_pages)
  754. goto redirty;
  755. #endif /* CONFIG_RUNTIME_COMPCACHE */
  756. /*
  757. * shmem_backing_dev_info's capabilities prevent regular writeback or
  758. * sync from ever calling shmem_writepage; but a stacking filesystem
  759. * might use ->writepage of its underlying filesystem, in which case
  760. * tmpfs should write out to swap only in response to memory pressure,
  761. * and not for the writeback threads or sync.
  762. */
  763. if (!wbc->for_reclaim) {
  764. WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
  765. goto redirty;
  766. }
  767. /*
  768. * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
  769. * value into swapfile.c, the only way we can correctly account for a
  770. * fallocated page arriving here is now to initialize it and write it.
  771. *
  772. * That's okay for a page already fallocated earlier, but if we have
  773. * not yet completed the fallocation, then (a) we want to keep track
  774. * of this page in case we have to undo it, and (b) it may not be a
  775. * good idea to continue anyway, once we're pushing into swap. So
  776. * reactivate the page, and let shmem_fallocate() quit when too many.
  777. */
  778. if (!PageUptodate(page)) {
  779. if (inode->i_private) {
  780. struct shmem_falloc *shmem_falloc;
  781. spin_lock(&inode->i_lock);
  782. shmem_falloc = inode->i_private;
  783. if (shmem_falloc &&
  784. !shmem_falloc->waitq &&
  785. index >= shmem_falloc->start &&
  786. index < shmem_falloc->next)
  787. shmem_falloc->nr_unswapped++;
  788. else
  789. shmem_falloc = NULL;
  790. spin_unlock(&inode->i_lock);
  791. if (shmem_falloc)
  792. goto redirty;
  793. }
  794. clear_highpage(page);
  795. flush_dcache_page(page);
  796. SetPageUptodate(page);
  797. }
  798. swap = get_swap_page();
  799. if (!swap.val)
  800. goto redirty;
  801. /*
  802. * Add inode to shmem_unuse()'s list of swapped-out inodes,
  803. * if it's not already there. Do it now before the page is
  804. * moved to swap cache, when its pagelock no longer protects
  805. * the inode from eviction. But don't unlock the mutex until
  806. * we've incremented swapped, because shmem_unuse_inode() will
  807. * prune a !swapped inode from the swaplist under this mutex.
  808. */
  809. mutex_lock(&shmem_swaplist_mutex);
  810. if (list_empty(&info->swaplist))
  811. list_add_tail(&info->swaplist, &shmem_swaplist);
  812. if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
  813. swap_shmem_alloc(swap);
  814. shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
  815. spin_lock(&info->lock);
  816. info->swapped++;
  817. shmem_recalc_inode(inode);
  818. spin_unlock(&info->lock);
  819. mutex_unlock(&shmem_swaplist_mutex);
  820. BUG_ON(page_mapped(page));
  821. swap_writepage(page, wbc);
  822. return 0;
  823. }
  824. mutex_unlock(&shmem_swaplist_mutex);
  825. swapcache_free(swap, NULL);
  826. redirty:
  827. set_page_dirty(page);
  828. if (wbc->for_reclaim)
  829. return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
  830. unlock_page(page);
  831. return 0;
  832. }
  833. #ifdef CONFIG_NUMA
  834. #ifdef CONFIG_TMPFS
  835. static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  836. {
  837. char buffer[64];
  838. if (!mpol || mpol->mode == MPOL_DEFAULT)
  839. return; /* show nothing */
  840. mpol_to_str(buffer, sizeof(buffer), mpol, 1);
  841. seq_printf(seq, ",mpol=%s", buffer);
  842. }
  843. static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  844. {
  845. struct mempolicy *mpol = NULL;
  846. if (sbinfo->mpol) {
  847. spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
  848. mpol = sbinfo->mpol;
  849. mpol_get(mpol);
  850. spin_unlock(&sbinfo->stat_lock);
  851. }
  852. return mpol;
  853. }
  854. #endif /* CONFIG_TMPFS */
  855. static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  856. struct shmem_inode_info *info, pgoff_t index)
  857. {
  858. struct vm_area_struct pvma;
  859. struct page *page;
  860. /* Create a pseudo vma that just contains the policy */
  861. pvma.vm_start = 0;
  862. /* Bias interleave by inode number to distribute better across nodes */
  863. pvma.vm_pgoff = index + info->vfs_inode.i_ino;
  864. pvma.vm_ops = NULL;
  865. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  866. page = swapin_readahead(swap, gfp, &pvma, 0);
  867. /* Drop reference taken by mpol_shared_policy_lookup() */
  868. mpol_cond_put(pvma.vm_policy);
  869. return page;
  870. }
  871. static struct page *shmem_alloc_page(gfp_t gfp,
  872. struct shmem_inode_info *info, pgoff_t index)
  873. {
  874. struct vm_area_struct pvma;
  875. struct page *page;
  876. /* Create a pseudo vma that just contains the policy */
  877. pvma.vm_start = 0;
  878. /* Bias interleave by inode number to distribute better across nodes */
  879. pvma.vm_pgoff = index + info->vfs_inode.i_ino;
  880. pvma.vm_ops = NULL;
  881. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  882. page = alloc_page_vma(gfp, &pvma, 0);
  883. /* Drop reference taken by mpol_shared_policy_lookup() */
  884. mpol_cond_put(pvma.vm_policy);
  885. return page;
  886. }
  887. #else /* !CONFIG_NUMA */
  888. #ifdef CONFIG_TMPFS
  889. static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  890. {
  891. }
  892. #endif /* CONFIG_TMPFS */
  893. static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  894. struct shmem_inode_info *info, pgoff_t index)
  895. {
  896. return swapin_readahead(swap, gfp, NULL, 0);
  897. }
  898. static inline struct page *shmem_alloc_page(gfp_t gfp,
  899. struct shmem_inode_info *info, pgoff_t index)
  900. {
  901. return alloc_page(gfp);
  902. }
  903. #endif /* CONFIG_NUMA */
  904. #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
  905. static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  906. {
  907. return NULL;
  908. }
  909. #endif
  910. /*
  911. * When a page is moved from swapcache to shmem filecache (either by the
  912. * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
  913. * shmem_unuse_inode()), it may have been read in earlier from swap, in
  914. * ignorance of the mapping it belongs to. If that mapping has special
  915. * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
  916. * we may need to copy to a suitable page before moving to filecache.
  917. *
  918. * In a future release, this may well be extended to respect cpuset and
  919. * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
  920. * but for now it is a simple matter of zone.
  921. */
  922. static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
  923. {
  924. return page_zonenum(page) > gfp_zone(gfp);
  925. }
  926. static int shmem_replace_page(struct page **pagep, gfp_t gfp,
  927. struct shmem_inode_info *info, pgoff_t index)
  928. {
  929. struct page *oldpage, *newpage;
  930. struct address_space *swap_mapping;
  931. pgoff_t swap_index;
  932. int error;
  933. oldpage = *pagep;
  934. swap_index = page_private(oldpage);
  935. swap_mapping = page_mapping(oldpage);
  936. /*
  937. * We have arrived here because our zones are constrained, so don't
  938. * limit chance of success by further cpuset and node constraints.
  939. */
  940. gfp &= ~GFP_CONSTRAINT_MASK;
  941. newpage = shmem_alloc_page(gfp, info, index);
  942. if (!newpage)
  943. return -ENOMEM;
  944. page_cache_get(newpage);
  945. copy_highpage(newpage, oldpage);
  946. flush_dcache_page(newpage);
  947. __set_page_locked(newpage);
  948. SetPageUptodate(newpage);
  949. SetPageSwapBacked(newpage);
  950. set_page_private(newpage, swap_index);
  951. SetPageSwapCache(newpage);
  952. /*
  953. * Our caller will very soon move newpage out of swapcache, but it's
  954. * a nice clean interface for us to replace oldpage by newpage there.
  955. */
  956. spin_lock_irq(&swap_mapping->tree_lock);
  957. error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
  958. newpage);
  959. if (!error) {
  960. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  961. __dec_zone_page_state(oldpage, NR_FILE_PAGES);
  962. }
  963. spin_unlock_irq(&swap_mapping->tree_lock);
  964. if (unlikely(error)) {
  965. /*
  966. * Is this possible? I think not, now that our callers check
  967. * both PageSwapCache and page_private after getting page lock;
  968. * but be defensive. Reverse old to newpage for clear and free.
  969. */
  970. oldpage = newpage;
  971. } else {
  972. mem_cgroup_replace_page_cache(oldpage, newpage);
  973. lru_cache_add_anon(newpage);
  974. *pagep = newpage;
  975. }
  976. ClearPageSwapCache(oldpage);
  977. set_page_private(oldpage, 0);
  978. unlock_page(oldpage);
  979. page_cache_release(oldpage);
  980. page_cache_release(oldpage);
  981. return error;
  982. }
  983. /*
  984. * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
  985. *
  986. * If we allocate a new one we do not mark it dirty. That's up to the
  987. * vm. If we swap it in we mark it dirty since we also free the swap
  988. * entry since a page cannot live in both the swap and page cache
  989. */
  990. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  991. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
  992. {
  993. struct address_space *mapping = inode->i_mapping;
  994. struct shmem_inode_info *info;
  995. struct shmem_sb_info *sbinfo;
  996. struct page *page;
  997. swp_entry_t swap;
  998. int error;
  999. int once = 0;
  1000. int alloced = 0;
  1001. if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
  1002. return -EFBIG;
  1003. repeat:
  1004. swap.val = 0;
  1005. page = find_lock_page(mapping, index);
  1006. if (radix_tree_exceptional_entry(page)) {
  1007. swap = radix_to_swp_entry(page);
  1008. page = NULL;
  1009. }
  1010. if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
  1011. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  1012. error = -EINVAL;
  1013. goto failed;
  1014. }
  1015. /* fallocated page? */
  1016. if (page && !PageUptodate(page)) {
  1017. if (sgp != SGP_READ)
  1018. goto clear;
  1019. unlock_page(page);
  1020. page_cache_release(page);
  1021. page = NULL;
  1022. }
  1023. if (page || (sgp == SGP_READ && !swap.val)) {
  1024. *pagep = page;
  1025. return 0;
  1026. }
  1027. /*
  1028. * Fast cache lookup did not find it:
  1029. * bring it back from swap or allocate.
  1030. */
  1031. info = SHMEM_I(inode);
  1032. sbinfo = SHMEM_SB(inode->i_sb);
  1033. if (swap.val) {
  1034. /* Look it up and read it in.. */
  1035. page = lookup_swap_cache(swap);
  1036. if (!page) {
  1037. /* here we actually do the io */
  1038. if (fault_type)
  1039. *fault_type |= VM_FAULT_MAJOR;
  1040. page = shmem_swapin(swap, gfp, info, index);
  1041. if (!page) {
  1042. error = -ENOMEM;
  1043. goto failed;
  1044. }
  1045. }
  1046. /* We have to do this with page locked to prevent races */
  1047. lock_page(page);
  1048. if (!PageSwapCache(page) || page_private(page) != swap.val ||
  1049. !shmem_confirm_swap(mapping, index, swap)) {
  1050. error = -EEXIST; /* try again */
  1051. goto unlock;
  1052. }
  1053. if (!PageUptodate(page)) {
  1054. error = -EIO;
  1055. goto failed;
  1056. }
  1057. wait_on_page_writeback(page);
  1058. if (shmem_should_replace_page(page, gfp)) {
  1059. error = shmem_replace_page(&page, gfp, info, index);
  1060. if (error)
  1061. goto failed;
  1062. }
  1063. error = mem_cgroup_cache_charge(page, current->mm,
  1064. gfp & GFP_RECLAIM_MASK);
  1065. if (!error) {
  1066. error = shmem_add_to_page_cache(page, mapping, index,
  1067. gfp, swp_to_radix_entry(swap));
  1068. /*
  1069. * We already confirmed swap under page lock, and make
  1070. * no memory allocation here, so usually no possibility
  1071. * of error; but free_swap_and_cache() only trylocks a
  1072. * page, so it is just possible that the entry has been
  1073. * truncated or holepunched since swap was confirmed.
  1074. * shmem_undo_range() will have done some of the
  1075. * unaccounting, now delete_from_swap_cache() will do
  1076. * the rest (including mem_cgroup_uncharge_swapcache).
  1077. * Reset swap.val? No, leave it so "failed" goes back to
  1078. * "repeat": reading a hole and writing should succeed.
  1079. */
  1080. if (error)
  1081. delete_from_swap_cache(page);
  1082. }
  1083. if (error)
  1084. goto failed;
  1085. spin_lock(&info->lock);
  1086. info->swapped--;
  1087. shmem_recalc_inode(inode);
  1088. spin_unlock(&info->lock);
  1089. delete_from_swap_cache(page);
  1090. set_page_dirty(page);
  1091. swap_free(swap);
  1092. } else {
  1093. if (shmem_acct_block(info->flags)) {
  1094. error = -ENOSPC;
  1095. goto failed;
  1096. }
  1097. if (sbinfo->max_blocks) {
  1098. if (percpu_counter_compare(&sbinfo->used_blocks,
  1099. sbinfo->max_blocks) >= 0) {
  1100. error = -ENOSPC;
  1101. goto unacct;
  1102. }
  1103. percpu_counter_inc(&sbinfo->used_blocks);
  1104. }
  1105. page = shmem_alloc_page(gfp, info, index);
  1106. if (!page) {
  1107. error = -ENOMEM;
  1108. goto decused;
  1109. }
  1110. SetPageSwapBacked(page);
  1111. __set_page_locked(page);
  1112. error = mem_cgroup_cache_charge(page, current->mm,
  1113. gfp & GFP_RECLAIM_MASK);
  1114. if (error)
  1115. goto decused;
  1116. error = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK);
  1117. if (!error) {
  1118. error = shmem_add_to_page_cache(page, mapping, index,
  1119. gfp, NULL);
  1120. radix_tree_preload_end();
  1121. }
  1122. if (error) {
  1123. mem_cgroup_uncharge_cache_page(page);
  1124. goto decused;
  1125. }
  1126. lru_cache_add_anon(page);
  1127. spin_lock(&info->lock);
  1128. info->alloced++;
  1129. inode->i_blocks += BLOCKS_PER_PAGE;
  1130. shmem_recalc_inode(inode);
  1131. spin_unlock(&info->lock);
  1132. alloced = true;
  1133. /*
  1134. * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
  1135. */
  1136. if (sgp == SGP_FALLOC)
  1137. sgp = SGP_WRITE;
  1138. clear:
  1139. /*
  1140. * Let SGP_WRITE caller clear ends if write does not fill page;
  1141. * but SGP_FALLOC on a page fallocated earlier must initialize
  1142. * it now, lest undo on failure cancel our earlier guarantee.
  1143. */
  1144. if (sgp != SGP_WRITE) {
  1145. clear_highpage(page);
  1146. flush_dcache_page(page);
  1147. SetPageUptodate(page);
  1148. }
  1149. if (sgp == SGP_DIRTY)
  1150. set_page_dirty(page);
  1151. }
  1152. /* Perhaps the file has been truncated since we checked */
  1153. if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
  1154. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  1155. error = -EINVAL;
  1156. if (alloced)
  1157. goto trunc;
  1158. else
  1159. goto failed;
  1160. }
  1161. *pagep = page;
  1162. return 0;
  1163. /*
  1164. * Error recovery.
  1165. */
  1166. trunc:
  1167. info = SHMEM_I(inode);
  1168. ClearPageDirty(page);
  1169. delete_from_page_cache(page);
  1170. spin_lock(&info->lock);
  1171. info->alloced--;
  1172. inode->i_blocks -= BLOCKS_PER_PAGE;
  1173. spin_unlock(&info->lock);
  1174. decused:
  1175. sbinfo = SHMEM_SB(inode->i_sb);
  1176. if (sbinfo->max_blocks)
  1177. percpu_counter_add(&sbinfo->used_blocks, -1);
  1178. unacct:
  1179. shmem_unacct_blocks(info->flags, 1);
  1180. failed:
  1181. if (swap.val && error != -EINVAL &&
  1182. !shmem_confirm_swap(mapping, index, swap))
  1183. error = -EEXIST;
  1184. unlock:
  1185. if (page) {
  1186. unlock_page(page);
  1187. page_cache_release(page);
  1188. }
  1189. if (error == -ENOSPC && !once++) {
  1190. info = SHMEM_I(inode);
  1191. spin_lock(&info->lock);
  1192. shmem_recalc_inode(inode);
  1193. spin_unlock(&info->lock);
  1194. goto repeat;
  1195. }
  1196. if (error == -EEXIST) /* from above or from radix_tree_insert */
  1197. goto repeat;
  1198. return error;
  1199. }
  1200. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1201. {
  1202. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1203. int error;
  1204. int ret = VM_FAULT_LOCKED;
  1205. /*
  1206. * Trinity finds that probing a hole which tmpfs is punching can
  1207. * prevent the hole-punch from ever completing: which in turn
  1208. * locks writers out with its hold on i_mutex. So refrain from
  1209. * faulting pages into the hole while it's being punched. Although
  1210. * shmem_undo_range() does remove the additions, it may be unable to
  1211. * keep up, as each new page needs its own unmap_mapping_range() call,
  1212. * and the i_mmap tree grows ever slower to scan if new vmas are added.
  1213. *
  1214. * It does not matter if we sometimes reach this check just before the
  1215. * hole-punch begins, so that one fault then races with the punch:
  1216. * we just need to make racing faults a rare case.
  1217. *
  1218. * The implementation below would be much simpler if we just used a
  1219. * standard mutex or completion: but we cannot take i_mutex in fault,
  1220. * and bloating every shmem inode for this unlikely case would be sad.
  1221. */
  1222. if (unlikely(inode->i_private)) {
  1223. struct shmem_falloc *shmem_falloc;
  1224. spin_lock(&inode->i_lock);
  1225. shmem_falloc = inode->i_private;
  1226. if (shmem_falloc &&
  1227. shmem_falloc->waitq &&
  1228. vmf->pgoff >= shmem_falloc->start &&
  1229. vmf->pgoff < shmem_falloc->next) {
  1230. wait_queue_head_t *shmem_falloc_waitq;
  1231. DEFINE_WAIT(shmem_fault_wait);
  1232. ret = VM_FAULT_NOPAGE;
  1233. if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
  1234. !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
  1235. /* It's polite to up mmap_sem if we can */
  1236. up_read(&vma->vm_mm->mmap_sem);
  1237. ret = VM_FAULT_RETRY;
  1238. }
  1239. shmem_falloc_waitq = shmem_falloc->waitq;
  1240. prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
  1241. TASK_UNINTERRUPTIBLE);
  1242. spin_unlock(&inode->i_lock);
  1243. schedule();
  1244. /*
  1245. * shmem_falloc_waitq points into the shmem_fallocate()
  1246. * stack of the hole-punching task: shmem_falloc_waitq
  1247. * is usually invalid by the time we reach here, but
  1248. * finish_wait() does not dereference it in that case;
  1249. * though i_lock needed lest racing with wake_up_all().
  1250. */
  1251. spin_lock(&inode->i_lock);
  1252. finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
  1253. spin_unlock(&inode->i_lock);
  1254. return ret;
  1255. }
  1256. spin_unlock(&inode->i_lock);
  1257. }
  1258. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
  1259. if (error)
  1260. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  1261. if (ret & VM_FAULT_MAJOR) {
  1262. count_vm_event(PGMAJFAULT);
  1263. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  1264. }
  1265. return ret;
  1266. }
  1267. #ifdef CONFIG_NUMA
  1268. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
  1269. {
  1270. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1271. return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
  1272. }
  1273. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  1274. unsigned long addr)
  1275. {
  1276. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1277. pgoff_t index;
  1278. index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1279. return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
  1280. }
  1281. #endif
  1282. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1283. {
  1284. struct inode *inode = file->f_path.dentry->d_inode;
  1285. struct shmem_inode_info *info = SHMEM_I(inode);
  1286. int retval = -ENOMEM;
  1287. spin_lock(&info->lock);
  1288. if (lock && !(info->flags & VM_LOCKED)) {
  1289. if (!user_shm_lock(inode->i_size, user))
  1290. goto out_nomem;
  1291. info->flags |= VM_LOCKED;
  1292. mapping_set_unevictable(file->f_mapping);
  1293. }
  1294. if (!lock && (info->flags & VM_LOCKED) && user) {
  1295. user_shm_unlock(inode->i_size, user);
  1296. info->flags &= ~VM_LOCKED;
  1297. mapping_clear_unevictable(file->f_mapping);
  1298. }
  1299. retval = 0;
  1300. out_nomem:
  1301. spin_unlock(&info->lock);
  1302. return retval;
  1303. }
  1304. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1305. {
  1306. struct inode *inode = file->f_path.dentry->d_inode;
  1307. struct shmem_inode_info *info = SHMEM_I(inode);
  1308. if (info->seals & F_SEAL_FUTURE_WRITE) {
  1309. /*
  1310. * New PROT_WRITE and MAP_SHARED mmaps are not allowed when
  1311. * "future write" seal active.
  1312. */
  1313. if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
  1314. return -EPERM;
  1315. /*
  1316. * Since the F_SEAL_FUTURE_WRITE seals allow for a MAP_SHARED
  1317. * read-only mapping, take care to not allow mprotect to revert
  1318. * protections.
  1319. */
  1320. vma->vm_flags &= ~(VM_MAYWRITE);
  1321. }
  1322. file_accessed(file);
  1323. #ifdef CONFIG_TIMA_RKP
  1324. if (vma->vm_end - vma->vm_start) {
  1325. /* iommu optimization- needs to be turned ON from
  1326. * the tz side.
  1327. */
  1328. cpu_v7_tima_iommu_opt(vma->vm_start, vma->vm_end, (unsigned long)vma->vm_mm->pgd);
  1329. __asm__ __volatile__ (
  1330. "mcr p15, 0, r0, c8, c3, 0\n"
  1331. "dsb\n"
  1332. "isb\n");
  1333. }
  1334. #endif
  1335. vma->vm_ops = &shmem_vm_ops;
  1336. return 0;
  1337. }
  1338. static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
  1339. umode_t mode, dev_t dev, unsigned long flags)
  1340. {
  1341. struct inode *inode;
  1342. struct shmem_inode_info *info;
  1343. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1344. if (shmem_reserve_inode(sb))
  1345. return NULL;
  1346. inode = new_inode(sb);
  1347. if (inode) {
  1348. inode->i_ino = get_next_ino();
  1349. inode_init_owner(inode, dir, mode);
  1350. inode->i_blocks = 0;
  1351. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1352. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1353. inode->i_generation = get_seconds();
  1354. info = SHMEM_I(inode);
  1355. memset(info, 0, (char *)inode - (char *)info);
  1356. spin_lock_init(&info->lock);
  1357. info->seals = F_SEAL_SEAL;
  1358. info->flags = flags & VM_NORESERVE;
  1359. INIT_LIST_HEAD(&info->swaplist);
  1360. simple_xattrs_init(&info->xattrs);
  1361. cache_no_acl(inode);
  1362. switch (mode & S_IFMT) {
  1363. default:
  1364. inode->i_op = &shmem_special_inode_operations;
  1365. init_special_inode(inode, mode, dev);
  1366. break;
  1367. case S_IFREG:
  1368. inode->i_mapping->a_ops = &shmem_aops;
  1369. inode->i_op = &shmem_inode_operations;
  1370. inode->i_fop = &shmem_file_operations;
  1371. mpol_shared_policy_init(&info->policy,
  1372. shmem_get_sbmpol(sbinfo));
  1373. break;
  1374. case S_IFDIR:
  1375. inc_nlink(inode);
  1376. /* Some things misbehave if size == 0 on a directory */
  1377. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1378. inode->i_op = &shmem_dir_inode_operations;
  1379. inode->i_fop = &simple_dir_operations;
  1380. break;
  1381. case S_IFLNK:
  1382. /*
  1383. * Must not load anything in the rbtree,
  1384. * mpol_free_shared_policy will not be called.
  1385. */
  1386. mpol_shared_policy_init(&info->policy, NULL);
  1387. break;
  1388. }
  1389. } else
  1390. shmem_free_inode(sb);
  1391. return inode;
  1392. }
  1393. #ifdef CONFIG_TMPFS
  1394. static const struct inode_operations shmem_symlink_inode_operations;
  1395. static const struct inode_operations shmem_short_symlink_operations;
  1396. #ifdef CONFIG_TMPFS_XATTR
  1397. static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
  1398. #else
  1399. #define shmem_initxattrs NULL
  1400. #endif
  1401. static int
  1402. shmem_write_begin(struct file *file, struct address_space *mapping,
  1403. loff_t pos, unsigned len, unsigned flags,
  1404. struct page **pagep, void **fsdata)
  1405. {
  1406. struct inode *inode = mapping->host;
  1407. struct shmem_inode_info *info = SHMEM_I(inode);
  1408. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1409. /* i_mutex is held by caller */
  1410. if (unlikely(info->seals & (F_SEAL_GROW |
  1411. F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) {
  1412. if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))
  1413. return -EPERM;
  1414. if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
  1415. return -EPERM;
  1416. }
  1417. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1418. }
  1419. static int
  1420. shmem_write_end(struct file *file, struct address_space *mapping,
  1421. loff_t pos, unsigned len, unsigned copied,
  1422. struct page *page, void *fsdata)
  1423. {
  1424. struct inode *inode = mapping->host;
  1425. if (pos + copied > inode->i_size)
  1426. i_size_write(inode, pos + copied);
  1427. if (!PageUptodate(page)) {
  1428. if (copied < PAGE_CACHE_SIZE) {
  1429. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  1430. zero_user_segments(page, 0, from,
  1431. from + copied, PAGE_CACHE_SIZE);
  1432. }
  1433. SetPageUptodate(page);
  1434. }
  1435. set_page_dirty(page);
  1436. unlock_page(page);
  1437. page_cache_release(page);
  1438. return copied;
  1439. }
  1440. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1441. {
  1442. struct inode *inode = filp->f_path.dentry->d_inode;
  1443. struct address_space *mapping = inode->i_mapping;
  1444. pgoff_t index;
  1445. unsigned long offset;
  1446. enum sgp_type sgp = SGP_READ;
  1447. /*
  1448. * Might this read be for a stacking filesystem? Then when reading
  1449. * holes of a sparse file, we actually need to allocate those pages,
  1450. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  1451. */
  1452. if (segment_eq(get_fs(), KERNEL_DS))
  1453. sgp = SGP_DIRTY;
  1454. index = *ppos >> PAGE_CACHE_SHIFT;
  1455. offset = *ppos & ~PAGE_CACHE_MASK;
  1456. for (;;) {
  1457. struct page *page = NULL;
  1458. pgoff_t end_index;
  1459. unsigned long nr, ret;
  1460. loff_t i_size = i_size_read(inode);
  1461. end_index = i_size >> PAGE_CACHE_SHIFT;
  1462. if (index > end_index)
  1463. break;
  1464. if (index == end_index) {
  1465. nr = i_size & ~PAGE_CACHE_MASK;
  1466. if (nr <= offset)
  1467. break;
  1468. }
  1469. desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
  1470. if (desc->error) {
  1471. if (desc->error == -EINVAL)
  1472. desc->error = 0;
  1473. break;
  1474. }
  1475. if (page)
  1476. unlock_page(page);
  1477. /*
  1478. * We must evaluate after, since reads (unlike writes)
  1479. * are called without i_mutex protection against truncate
  1480. */
  1481. nr = PAGE_CACHE_SIZE;
  1482. i_size = i_size_read(inode);
  1483. end_index = i_size >> PAGE_CACHE_SHIFT;
  1484. if (index == end_index) {
  1485. nr = i_size & ~PAGE_CACHE_MASK;
  1486. if (nr <= offset) {
  1487. if (page)
  1488. page_cache_release(page);
  1489. break;
  1490. }
  1491. }
  1492. nr -= offset;
  1493. if (page) {
  1494. /*
  1495. * If users can be writing to this page using arbitrary
  1496. * virtual addresses, take care about potential aliasing
  1497. * before reading the page on the kernel side.
  1498. */
  1499. if (mapping_writably_mapped(mapping))
  1500. flush_dcache_page(page);
  1501. /*
  1502. * Mark the page accessed if we read the beginning.
  1503. */
  1504. if (!offset)
  1505. mark_page_accessed(page);
  1506. } else {
  1507. page = ZERO_PAGE(0);
  1508. page_cache_get(page);
  1509. }
  1510. /*
  1511. * Ok, we have the page, and it's up-to-date, so
  1512. * now we can copy it to user space...
  1513. *
  1514. * The actor routine returns how many bytes were actually used..
  1515. * NOTE! This may not be the same as how much of a user buffer
  1516. * we filled up (we may be padding etc), so we can only update
  1517. * "pos" here (the actor routine has to update the user buffer
  1518. * pointers and the remaining count).
  1519. */
  1520. ret = actor(desc, page, offset, nr);
  1521. offset += ret;
  1522. index += offset >> PAGE_CACHE_SHIFT;
  1523. offset &= ~PAGE_CACHE_MASK;
  1524. page_cache_release(page);
  1525. if (ret != nr || !desc->count)
  1526. break;
  1527. cond_resched();
  1528. }
  1529. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1530. file_accessed(filp);
  1531. }
  1532. static ssize_t shmem_file_aio_read(struct kiocb *iocb,
  1533. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  1534. {
  1535. struct file *filp = iocb->ki_filp;
  1536. ssize_t retval;
  1537. unsigned long seg;
  1538. size_t count;
  1539. loff_t *ppos = &iocb->ki_pos;
  1540. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1541. if (retval)
  1542. return retval;
  1543. for (seg = 0; seg < nr_segs; seg++) {
  1544. read_descriptor_t desc;
  1545. desc.written = 0;
  1546. desc.arg.buf = iov[seg].iov_base;
  1547. desc.count = iov[seg].iov_len;
  1548. if (desc.count == 0)
  1549. continue;
  1550. desc.error = 0;
  1551. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1552. retval += desc.written;
  1553. if (desc.error) {
  1554. retval = retval ?: desc.error;
  1555. break;
  1556. }
  1557. if (desc.count > 0)
  1558. break;
  1559. }
  1560. return retval;
  1561. }
  1562. static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
  1563. struct pipe_inode_info *pipe, size_t len,
  1564. unsigned int flags)
  1565. {
  1566. struct address_space *mapping = in->f_mapping;
  1567. struct inode *inode = mapping->host;
  1568. unsigned int loff, nr_pages, req_pages;
  1569. struct page *pages[PIPE_DEF_BUFFERS];
  1570. struct partial_page partial[PIPE_DEF_BUFFERS];
  1571. struct page *page;
  1572. pgoff_t index, end_index;
  1573. loff_t isize, left;
  1574. int error, page_nr;
  1575. struct splice_pipe_desc spd = {
  1576. .pages = pages,
  1577. .partial = partial,
  1578. .nr_pages_max = PIPE_DEF_BUFFERS,
  1579. .flags = flags,
  1580. .ops = &page_cache_pipe_buf_ops,
  1581. .spd_release = spd_release_page,
  1582. };
  1583. isize = i_size_read(inode);
  1584. if (unlikely(*ppos >= isize))
  1585. return 0;
  1586. left = isize - *ppos;
  1587. if (unlikely(left < len))
  1588. len = left;
  1589. if (splice_grow_spd(pipe, &spd))
  1590. return -ENOMEM;
  1591. index = *ppos >> PAGE_CACHE_SHIFT;
  1592. loff = *ppos & ~PAGE_CACHE_MASK;
  1593. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1594. nr_pages = min(req_pages, pipe->buffers);
  1595. spd.nr_pages = find_get_pages_contig(mapping, index,
  1596. nr_pages, spd.pages);
  1597. index += spd.nr_pages;
  1598. error = 0;
  1599. while (spd.nr_pages < nr_pages) {
  1600. error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
  1601. if (error)
  1602. break;
  1603. unlock_page(page);
  1604. spd.pages[spd.nr_pages++] = page;
  1605. index++;
  1606. }
  1607. index = *ppos >> PAGE_CACHE_SHIFT;
  1608. nr_pages = spd.nr_pages;
  1609. spd.nr_pages = 0;
  1610. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  1611. unsigned int this_len;
  1612. if (!len)
  1613. break;
  1614. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  1615. page = spd.pages[page_nr];
  1616. if (!PageUptodate(page) || page->mapping != mapping) {
  1617. error = shmem_getpage(inode, index, &page,
  1618. SGP_CACHE, NULL);
  1619. if (error)
  1620. break;
  1621. unlock_page(page);
  1622. page_cache_release(spd.pages[page_nr]);
  1623. spd.pages[page_nr] = page;
  1624. }
  1625. isize = i_size_read(inode);
  1626. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1627. if (unlikely(!isize || index > end_index))
  1628. break;
  1629. if (end_index == index) {
  1630. unsigned int plen;
  1631. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1632. if (plen <= loff)
  1633. break;
  1634. this_len = min(this_len, plen - loff);
  1635. len = this_len;
  1636. }
  1637. spd.partial[page_nr].offset = loff;
  1638. spd.partial[page_nr].len = this_len;
  1639. len -= this_len;
  1640. loff = 0;
  1641. spd.nr_pages++;
  1642. index++;
  1643. }
  1644. while (page_nr < nr_pages)
  1645. page_cache_release(spd.pages[page_nr++]);
  1646. if (spd.nr_pages)
  1647. error = splice_to_pipe(pipe, &spd);
  1648. splice_shrink_spd(&spd);
  1649. if (error > 0) {
  1650. *ppos += error;
  1651. file_accessed(in);
  1652. }
  1653. return error;
  1654. }
  1655. /*
  1656. * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
  1657. */
  1658. static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
  1659. pgoff_t index, pgoff_t end, int origin)
  1660. {
  1661. struct page *page;
  1662. struct pagevec pvec;
  1663. pgoff_t indices[PAGEVEC_SIZE];
  1664. bool done = false;
  1665. int i;
  1666. pagevec_init(&pvec, 0);
  1667. pvec.nr = 1; /* start small: we may be there already */
  1668. while (!done) {
  1669. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  1670. pvec.nr, pvec.pages, indices);
  1671. if (!pvec.nr) {
  1672. if (origin == SEEK_DATA)
  1673. index = end;
  1674. break;
  1675. }
  1676. for (i = 0; i < pvec.nr; i++, index++) {
  1677. if (index < indices[i]) {
  1678. if (origin == SEEK_HOLE) {
  1679. done = true;
  1680. break;
  1681. }
  1682. index = indices[i];
  1683. }
  1684. page = pvec.pages[i];
  1685. if (page && !radix_tree_exceptional_entry(page)) {
  1686. if (!PageUptodate(page))
  1687. page = NULL;
  1688. }
  1689. if (index >= end ||
  1690. (page && origin == SEEK_DATA) ||
  1691. (!page && origin == SEEK_HOLE)) {
  1692. done = true;
  1693. break;
  1694. }
  1695. }
  1696. shmem_deswap_pagevec(&pvec);
  1697. pagevec_release(&pvec);
  1698. pvec.nr = PAGEVEC_SIZE;
  1699. cond_resched();
  1700. }
  1701. return index;
  1702. }
  1703. static loff_t shmem_file_llseek(struct file *file, loff_t offset, int origin)
  1704. {
  1705. struct address_space *mapping = file->f_mapping;
  1706. struct inode *inode = mapping->host;
  1707. pgoff_t start, end;
  1708. loff_t new_offset;
  1709. if (origin != SEEK_DATA && origin != SEEK_HOLE)
  1710. return generic_file_llseek_size(file, offset, origin,
  1711. MAX_LFS_FILESIZE);
  1712. mutex_lock(&inode->i_mutex);
  1713. /* We're holding i_mutex so we can access i_size directly */
  1714. if (offset < 0)
  1715. offset = -EINVAL;
  1716. else if (offset >= inode->i_size)
  1717. offset = -ENXIO;
  1718. else {
  1719. start = offset >> PAGE_CACHE_SHIFT;
  1720. end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1721. new_offset = shmem_seek_hole_data(mapping, start, end, origin);
  1722. new_offset <<= PAGE_CACHE_SHIFT;
  1723. if (new_offset > offset) {
  1724. if (new_offset < inode->i_size)
  1725. offset = new_offset;
  1726. else if (origin == SEEK_DATA)
  1727. offset = -ENXIO;
  1728. else
  1729. offset = inode->i_size;
  1730. }
  1731. }
  1732. if (offset >= 0 && offset != file->f_pos) {
  1733. file->f_pos = offset;
  1734. file->f_version = 0;
  1735. }
  1736. mutex_unlock(&inode->i_mutex);
  1737. return offset;
  1738. }
  1739. /*
  1740. * We need a tag: a new tag would expand every radix_tree_node by 8 bytes,
  1741. * so reuse a tag which we firmly believe is never set or cleared on shmem.
  1742. */
  1743. #define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE
  1744. #define LAST_SCAN 4 /* about 150ms max */
  1745. static void shmem_tag_pins(struct address_space *mapping)
  1746. {
  1747. struct radix_tree_iter iter;
  1748. void **slot;
  1749. pgoff_t start;
  1750. struct page *page;
  1751. unsigned int tagged = 0;
  1752. lru_add_drain();
  1753. start = 0;
  1754. spin_lock_irq(&mapping->tree_lock);
  1755. restart:
  1756. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
  1757. page = radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
  1758. if (!page || radix_tree_exception(page)) {
  1759. if (radix_tree_deref_retry(page))
  1760. goto restart;
  1761. } else if (page_count(page) - page_mapcount(page) > 1) {
  1762. radix_tree_tag_set(&mapping->page_tree, iter.index,
  1763. SHMEM_TAG_PINNED);
  1764. }
  1765. if (++tagged % 1024)
  1766. continue;
  1767. spin_unlock_irq(&mapping->tree_lock);
  1768. cond_resched();
  1769. start = iter.index + 1;
  1770. spin_lock_irq(&mapping->tree_lock);
  1771. goto restart;
  1772. }
  1773. spin_unlock_irq(&mapping->tree_lock);
  1774. }
  1775. /*
  1776. * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
  1777. * via get_user_pages(), drivers might have some pending I/O without any active
  1778. * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
  1779. * and see whether it has an elevated ref-count. If so, we tag them and wait for
  1780. * them to be dropped.
  1781. * The caller must guarantee that no new user will acquire writable references
  1782. * to those pages to avoid races.
  1783. */
  1784. static int shmem_wait_for_pins(struct address_space *mapping)
  1785. {
  1786. struct radix_tree_iter iter;
  1787. void **slot;
  1788. pgoff_t start;
  1789. struct page *page;
  1790. int error, scan;
  1791. shmem_tag_pins(mapping);
  1792. error = 0;
  1793. for (scan = 0; scan <= LAST_SCAN; scan++) {
  1794. if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED))
  1795. break;
  1796. if (!scan)
  1797. lru_add_drain_all();
  1798. else if (schedule_timeout_killable((HZ << scan) / 200))
  1799. scan = LAST_SCAN;
  1800. start = 0;
  1801. rcu_read_lock();
  1802. restart:
  1803. radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter,
  1804. start, SHMEM_TAG_PINNED) {
  1805. page = radix_tree_deref_slot(slot);
  1806. if (radix_tree_exception(page)) {
  1807. if (radix_tree_deref_retry(page))
  1808. goto restart;
  1809. page = NULL;
  1810. }
  1811. if (page &&
  1812. page_count(page) - page_mapcount(page) != 1) {
  1813. if (scan < LAST_SCAN)
  1814. goto continue_resched;
  1815. /*
  1816. * On the last scan, we clean up all those tags
  1817. * we inserted; but make a note that we still
  1818. * found pages pinned.
  1819. */
  1820. error = -EBUSY;
  1821. }
  1822. spin_lock_irq(&mapping->tree_lock);
  1823. radix_tree_tag_clear(&mapping->page_tree,
  1824. iter.index, SHMEM_TAG_PINNED);
  1825. spin_unlock_irq(&mapping->tree_lock);
  1826. continue_resched:
  1827. if (need_resched()) {
  1828. cond_resched_rcu();
  1829. start = iter.index + 1;
  1830. goto restart;
  1831. }
  1832. }
  1833. rcu_read_unlock();
  1834. }
  1835. return error;
  1836. }
  1837. #define F_ALL_SEALS (F_SEAL_SEAL | \
  1838. F_SEAL_SHRINK | \
  1839. F_SEAL_GROW | \
  1840. F_SEAL_WRITE | \
  1841. F_SEAL_FUTURE_WRITE)
  1842. int shmem_add_seals(struct file *file, unsigned int seals)
  1843. {
  1844. struct inode *inode = file->f_path.dentry->d_inode;
  1845. struct shmem_inode_info *info = SHMEM_I(inode);
  1846. int error;
  1847. /*
  1848. * SEALING
  1849. * Sealing allows multiple parties to share a shmem-file but restrict
  1850. * access to a specific subset of file operations. Seals can only be
  1851. * added, but never removed. This way, mutually untrusted parties can
  1852. * share common memory regions with a well-defined policy. A malicious
  1853. * peer can thus never perform unwanted operations on a shared object.
  1854. *
  1855. * Seals are only supported on special shmem-files and always affect
  1856. * the whole underlying inode. Once a seal is set, it may prevent some
  1857. * kinds of access to the file. Currently, the following seals are
  1858. * defined:
  1859. * SEAL_SEAL: Prevent further seals from being set on this file
  1860. * SEAL_SHRINK: Prevent the file from shrinking
  1861. * SEAL_GROW: Prevent the file from growing
  1862. * SEAL_WRITE: Prevent write access to the file
  1863. *
  1864. * As we don't require any trust relationship between two parties, we
  1865. * must prevent seals from being removed. Therefore, sealing a file
  1866. * only adds a given set of seals to the file, it never touches
  1867. * existing seals. Furthermore, the "setting seals"-operation can be
  1868. * sealed itself, which basically prevents any further seal from being
  1869. * added.
  1870. *
  1871. * Semantics of sealing are only defined on volatile files. Only
  1872. * anonymous shmem files support sealing. More importantly, seals are
  1873. * never written to disk. Therefore, there's no plan to support it on
  1874. * other file types.
  1875. */
  1876. if (file->f_op != &shmem_file_operations)
  1877. return -EINVAL;
  1878. if (!(file->f_mode & FMODE_WRITE))
  1879. return -EPERM;
  1880. if (seals & ~(unsigned int)F_ALL_SEALS)
  1881. return -EINVAL;
  1882. mutex_lock(&inode->i_mutex);
  1883. if (info->seals & F_SEAL_SEAL) {
  1884. error = -EPERM;
  1885. goto unlock;
  1886. }
  1887. if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
  1888. error = mapping_deny_writable(file->f_mapping);
  1889. if (error)
  1890. goto unlock;
  1891. error = shmem_wait_for_pins(file->f_mapping);
  1892. if (error) {
  1893. mapping_allow_writable(file->f_mapping);
  1894. goto unlock;
  1895. }
  1896. }
  1897. info->seals |= seals;
  1898. error = 0;
  1899. unlock:
  1900. mutex_unlock(&inode->i_mutex);
  1901. return error;
  1902. }
  1903. EXPORT_SYMBOL_GPL(shmem_add_seals);
  1904. int shmem_get_seals(struct file *file)
  1905. {
  1906. if (file->f_op != &shmem_file_operations)
  1907. return -EINVAL;
  1908. return SHMEM_I(file->f_path.dentry->d_inode)->seals;
  1909. }
  1910. EXPORT_SYMBOL_GPL(shmem_get_seals);
  1911. long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  1912. {
  1913. long error;
  1914. switch (cmd) {
  1915. case F_ADD_SEALS:
  1916. /* disallow upper 32bit */
  1917. if (arg > UINT_MAX)
  1918. return -EINVAL;
  1919. error = shmem_add_seals(file, arg);
  1920. break;
  1921. case F_GET_SEALS:
  1922. error = shmem_get_seals(file);
  1923. break;
  1924. default:
  1925. error = -EINVAL;
  1926. break;
  1927. }
  1928. return error;
  1929. }
  1930. static long shmem_fallocate(struct file *file, int mode, loff_t offset,
  1931. loff_t len)
  1932. {
  1933. struct inode *inode = file->f_path.dentry->d_inode;
  1934. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1935. struct shmem_inode_info *info = SHMEM_I(inode);
  1936. struct shmem_falloc shmem_falloc;
  1937. pgoff_t start, index, end;
  1938. int error;
  1939. mutex_lock(&inode->i_mutex);
  1940. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1941. struct address_space *mapping = file->f_mapping;
  1942. loff_t unmap_start = round_up(offset, PAGE_SIZE);
  1943. loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
  1944. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
  1945. /* protected by i_mutex */
  1946. if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
  1947. error = -EPERM;
  1948. goto out;
  1949. }
  1950. shmem_falloc.waitq = &shmem_falloc_waitq;
  1951. shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
  1952. shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
  1953. spin_lock(&inode->i_lock);
  1954. inode->i_private = &shmem_falloc;
  1955. spin_unlock(&inode->i_lock);
  1956. if ((u64)unmap_end > (u64)unmap_start)
  1957. unmap_mapping_range(mapping, unmap_start,
  1958. 1 + unmap_end - unmap_start, 0);
  1959. shmem_truncate_range(inode, offset, offset + len - 1);
  1960. /* No need to unmap again: hole-punching leaves COWed pages */
  1961. spin_lock(&inode->i_lock);
  1962. inode->i_private = NULL;
  1963. wake_up_all(&shmem_falloc_waitq);
  1964. spin_unlock(&inode->i_lock);
  1965. error = 0;
  1966. goto out;
  1967. }
  1968. /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
  1969. error = inode_newsize_ok(inode, offset + len);
  1970. if (error)
  1971. goto out;
  1972. if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
  1973. error = -EPERM;
  1974. goto out;
  1975. }
  1976. start = offset >> PAGE_CACHE_SHIFT;
  1977. end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1978. /* Try to avoid a swapstorm if len is impossible to satisfy */
  1979. if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
  1980. error = -ENOSPC;
  1981. goto out;
  1982. }
  1983. shmem_falloc.waitq = NULL;
  1984. shmem_falloc.start = start;
  1985. shmem_falloc.next = start;
  1986. shmem_falloc.nr_falloced = 0;
  1987. shmem_falloc.nr_unswapped = 0;
  1988. spin_lock(&inode->i_lock);
  1989. inode->i_private = &shmem_falloc;
  1990. spin_unlock(&inode->i_lock);
  1991. for (index = start; index < end; index++) {
  1992. struct page *page;
  1993. /*
  1994. * Good, the fallocate(2) manpage permits EINTR: we may have
  1995. * been interrupted because we are using up too much memory.
  1996. */
  1997. if (signal_pending(current))
  1998. error = -EINTR;
  1999. else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
  2000. error = -ENOMEM;
  2001. else
  2002. error = shmem_getpage(inode, index, &page, SGP_FALLOC,
  2003. NULL);
  2004. if (error) {
  2005. /* Remove the !PageUptodate pages we added */
  2006. if (index > start) {
  2007. shmem_undo_range(inode,
  2008. (loff_t)start << PAGE_CACHE_SHIFT,
  2009. ((loff_t)index << PAGE_CACHE_SHIFT) - 1, true);
  2010. }
  2011. goto undone;
  2012. }
  2013. /*
  2014. * Inform shmem_writepage() how far we have reached.
  2015. * No need for lock or barrier: we have the page lock.
  2016. */
  2017. shmem_falloc.next++;
  2018. if (!PageUptodate(page))
  2019. shmem_falloc.nr_falloced++;
  2020. /*
  2021. * If !PageUptodate, leave it that way so that freeable pages
  2022. * can be recognized if we need to rollback on error later.
  2023. * But set_page_dirty so that memory pressure will swap rather
  2024. * than free the pages we are allocating (and SGP_CACHE pages
  2025. * might still be clean: we now need to mark those dirty too).
  2026. */
  2027. set_page_dirty(page);
  2028. unlock_page(page);
  2029. page_cache_release(page);
  2030. cond_resched();
  2031. }
  2032. if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
  2033. i_size_write(inode, offset + len);
  2034. inode->i_ctime = CURRENT_TIME;
  2035. undone:
  2036. spin_lock(&inode->i_lock);
  2037. inode->i_private = NULL;
  2038. spin_unlock(&inode->i_lock);
  2039. out:
  2040. mutex_unlock(&inode->i_mutex);
  2041. return error;
  2042. }
  2043. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  2044. {
  2045. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  2046. buf->f_type = TMPFS_MAGIC;
  2047. buf->f_bsize = PAGE_CACHE_SIZE;
  2048. buf->f_namelen = NAME_MAX;
  2049. if (sbinfo->max_blocks) {
  2050. buf->f_blocks = sbinfo->max_blocks;
  2051. buf->f_bavail =
  2052. buf->f_bfree = sbinfo->max_blocks -
  2053. percpu_counter_sum(&sbinfo->used_blocks);
  2054. }
  2055. if (sbinfo->max_inodes) {
  2056. buf->f_files = sbinfo->max_inodes;
  2057. buf->f_ffree = sbinfo->free_inodes;
  2058. }
  2059. /* else leave those fields 0 like simple_statfs */
  2060. return 0;
  2061. }
  2062. /*
  2063. * File creation. Allocate an inode, and we're done..
  2064. */
  2065. static int
  2066. shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  2067. {
  2068. struct inode *inode;
  2069. int error = -ENOSPC;
  2070. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
  2071. if (inode) {
  2072. error = simple_acl_create(dir, inode);
  2073. if (error)
  2074. goto out_iput;
  2075. error = security_inode_init_security(inode, dir,
  2076. &dentry->d_name,
  2077. shmem_initxattrs, NULL);
  2078. if (error && error != -EOPNOTSUPP)
  2079. goto out_iput;
  2080. error = 0;
  2081. dir->i_size += BOGO_DIRENT_SIZE;
  2082. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  2083. d_instantiate(dentry, inode);
  2084. dget(dentry); /* Extra count - pin the dentry in core */
  2085. }
  2086. return error;
  2087. out_iput:
  2088. iput(inode);
  2089. return error;
  2090. }
  2091. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  2092. {
  2093. int error;
  2094. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  2095. return error;
  2096. inc_nlink(dir);
  2097. return 0;
  2098. }
  2099. static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2100. struct nameidata *nd)
  2101. {
  2102. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  2103. }
  2104. /*
  2105. * Link a file..
  2106. */
  2107. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  2108. {
  2109. struct inode *inode = old_dentry->d_inode;
  2110. int ret = 0;
  2111. /*
  2112. * No ordinary (disk based) filesystem counts links as inodes;
  2113. * but each new link needs a new dentry, pinning lowmem, and
  2114. * tmpfs dentries cannot be pruned until they are unlinked.
  2115. * But if an O_TMPFILE file is linked into the tmpfs, the
  2116. * first link must skip that, to get the accounting right.
  2117. */
  2118. if (inode->i_nlink) {
  2119. ret = shmem_reserve_inode(inode->i_sb);
  2120. if (ret)
  2121. goto out;
  2122. }
  2123. dir->i_size += BOGO_DIRENT_SIZE;
  2124. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  2125. inc_nlink(inode);
  2126. ihold(inode); /* New dentry reference */
  2127. dget(dentry); /* Extra pinning count for the created dentry */
  2128. d_instantiate(dentry, inode);
  2129. out:
  2130. return ret;
  2131. }
  2132. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  2133. {
  2134. struct inode *inode = dentry->d_inode;
  2135. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  2136. shmem_free_inode(inode->i_sb);
  2137. dir->i_size -= BOGO_DIRENT_SIZE;
  2138. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  2139. drop_nlink(inode);
  2140. dput(dentry); /* Undo the count from "create" - this does all the work */
  2141. return 0;
  2142. }
  2143. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  2144. {
  2145. if (!simple_empty(dentry))
  2146. return -ENOTEMPTY;
  2147. drop_nlink(dentry->d_inode);
  2148. drop_nlink(dir);
  2149. return shmem_unlink(dir, dentry);
  2150. }
  2151. /*
  2152. * The VFS layer already does all the dentry stuff for rename,
  2153. * we just have to decrement the usage count for the target if
  2154. * it exists so that the VFS layer correctly free's it when it
  2155. * gets overwritten.
  2156. */
  2157. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  2158. {
  2159. struct inode *inode = old_dentry->d_inode;
  2160. int they_are_dirs = S_ISDIR(inode->i_mode);
  2161. if (!simple_empty(new_dentry))
  2162. return -ENOTEMPTY;
  2163. if (new_dentry->d_inode) {
  2164. (void) shmem_unlink(new_dir, new_dentry);
  2165. if (they_are_dirs) {
  2166. drop_nlink(new_dentry->d_inode);
  2167. drop_nlink(old_dir);
  2168. }
  2169. } else if (they_are_dirs) {
  2170. drop_nlink(old_dir);
  2171. inc_nlink(new_dir);
  2172. }
  2173. old_dir->i_size -= BOGO_DIRENT_SIZE;
  2174. new_dir->i_size += BOGO_DIRENT_SIZE;
  2175. old_dir->i_ctime = old_dir->i_mtime =
  2176. new_dir->i_ctime = new_dir->i_mtime =
  2177. inode->i_ctime = CURRENT_TIME;
  2178. return 0;
  2179. }
  2180. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  2181. {
  2182. int error;
  2183. int len;
  2184. struct inode *inode;
  2185. struct page *page;
  2186. char *kaddr;
  2187. struct shmem_inode_info *info;
  2188. len = strlen(symname) + 1;
  2189. if (len > PAGE_CACHE_SIZE)
  2190. return -ENAMETOOLONG;
  2191. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
  2192. if (!inode)
  2193. return -ENOSPC;
  2194. error = security_inode_init_security(inode, dir, &dentry->d_name,
  2195. shmem_initxattrs, NULL);
  2196. if (error) {
  2197. if (error != -EOPNOTSUPP) {
  2198. iput(inode);
  2199. return error;
  2200. }
  2201. error = 0;
  2202. }
  2203. info = SHMEM_I(inode);
  2204. inode->i_size = len-1;
  2205. if (len <= SHORT_SYMLINK_LEN) {
  2206. info->symlink = kmemdup(symname, len, GFP_KERNEL);
  2207. if (!info->symlink) {
  2208. iput(inode);
  2209. return -ENOMEM;
  2210. }
  2211. inode->i_op = &shmem_short_symlink_operations;
  2212. } else {
  2213. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  2214. if (error) {
  2215. iput(inode);
  2216. return error;
  2217. }
  2218. inode->i_mapping->a_ops = &shmem_aops;
  2219. inode->i_op = &shmem_symlink_inode_operations;
  2220. kaddr = kmap_atomic(page);
  2221. memcpy(kaddr, symname, len);
  2222. kunmap_atomic(kaddr);
  2223. SetPageUptodate(page);
  2224. set_page_dirty(page);
  2225. unlock_page(page);
  2226. page_cache_release(page);
  2227. }
  2228. dir->i_size += BOGO_DIRENT_SIZE;
  2229. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  2230. d_instantiate(dentry, inode);
  2231. dget(dentry);
  2232. return 0;
  2233. }
  2234. static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
  2235. {
  2236. nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
  2237. return NULL;
  2238. }
  2239. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  2240. {
  2241. struct page *page = NULL;
  2242. int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  2243. nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
  2244. if (page)
  2245. unlock_page(page);
  2246. return page;
  2247. }
  2248. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2249. {
  2250. if (!IS_ERR(nd_get_link(nd))) {
  2251. struct page *page = cookie;
  2252. kunmap(page);
  2253. mark_page_accessed(page);
  2254. page_cache_release(page);
  2255. }
  2256. }
  2257. #ifdef CONFIG_TMPFS_XATTR
  2258. /*
  2259. * Superblocks without xattr inode operations may get some security.* xattr
  2260. * support from the LSM "for free". As soon as we have any other xattrs
  2261. * like ACLs, we also need to implement the security.* handlers at
  2262. * filesystem level, though.
  2263. */
  2264. /*
  2265. * Callback for security_inode_init_security() for acquiring xattrs.
  2266. */
  2267. static int shmem_initxattrs(struct inode *inode,
  2268. const struct xattr *xattr_array,
  2269. void *fs_info)
  2270. {
  2271. struct shmem_inode_info *info = SHMEM_I(inode);
  2272. const struct xattr *xattr;
  2273. struct simple_xattr *new_xattr;
  2274. size_t len;
  2275. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  2276. new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
  2277. if (!new_xattr)
  2278. return -ENOMEM;
  2279. len = strlen(xattr->name) + 1;
  2280. new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
  2281. GFP_KERNEL);
  2282. if (!new_xattr->name) {
  2283. kfree(new_xattr);
  2284. return -ENOMEM;
  2285. }
  2286. memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
  2287. XATTR_SECURITY_PREFIX_LEN);
  2288. memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
  2289. xattr->name, len);
  2290. simple_xattr_list_add(&info->xattrs, new_xattr);
  2291. }
  2292. return 0;
  2293. }
  2294. static const struct xattr_handler *shmem_xattr_handlers[] = {
  2295. #ifdef CONFIG_TMPFS_POSIX_ACL
  2296. &posix_acl_access_xattr_handler,
  2297. &posix_acl_default_xattr_handler,
  2298. #endif
  2299. NULL
  2300. };
  2301. static int shmem_xattr_validate(const char *name)
  2302. {
  2303. struct { const char *prefix; size_t len; } arr[] = {
  2304. { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
  2305. { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
  2306. };
  2307. int i;
  2308. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  2309. size_t preflen = arr[i].len;
  2310. if (strncmp(name, arr[i].prefix, preflen) == 0) {
  2311. if (!name[preflen])
  2312. return -EINVAL;
  2313. return 0;
  2314. }
  2315. }
  2316. return -EOPNOTSUPP;
  2317. }
  2318. static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
  2319. void *buffer, size_t size)
  2320. {
  2321. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2322. int err;
  2323. /*
  2324. * If this is a request for a synthetic attribute in the system.*
  2325. * namespace use the generic infrastructure to resolve a handler
  2326. * for it via sb->s_xattr.
  2327. */
  2328. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2329. return generic_getxattr(dentry, name, buffer, size);
  2330. err = shmem_xattr_validate(name);
  2331. if (err)
  2332. return err;
  2333. return simple_xattr_get(&info->xattrs, name, buffer, size);
  2334. }
  2335. static int shmem_setxattr(struct dentry *dentry, const char *name,
  2336. const void *value, size_t size, int flags)
  2337. {
  2338. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2339. int err;
  2340. /*
  2341. * If this is a request for a synthetic attribute in the system.*
  2342. * namespace use the generic infrastructure to resolve a handler
  2343. * for it via sb->s_xattr.
  2344. */
  2345. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2346. return generic_setxattr(dentry, name, value, size, flags);
  2347. err = shmem_xattr_validate(name);
  2348. if (err)
  2349. return err;
  2350. return simple_xattr_set(&info->xattrs, name, value, size, flags);
  2351. }
  2352. static int shmem_removexattr(struct dentry *dentry, const char *name)
  2353. {
  2354. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2355. int err;
  2356. /*
  2357. * If this is a request for a synthetic attribute in the system.*
  2358. * namespace use the generic infrastructure to resolve a handler
  2359. * for it via sb->s_xattr.
  2360. */
  2361. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2362. return generic_removexattr(dentry, name);
  2363. err = shmem_xattr_validate(name);
  2364. if (err)
  2365. return err;
  2366. return simple_xattr_remove(&info->xattrs, name);
  2367. }
  2368. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  2369. {
  2370. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2371. return simple_xattr_list(&info->xattrs, buffer, size);
  2372. }
  2373. #endif /* CONFIG_TMPFS_XATTR */
  2374. static const struct inode_operations shmem_short_symlink_operations = {
  2375. .readlink = generic_readlink,
  2376. .follow_link = shmem_follow_short_symlink,
  2377. #ifdef CONFIG_TMPFS_XATTR
  2378. .setxattr = shmem_setxattr,
  2379. .getxattr = shmem_getxattr,
  2380. .listxattr = shmem_listxattr,
  2381. .removexattr = shmem_removexattr,
  2382. #endif
  2383. };
  2384. static const struct inode_operations shmem_symlink_inode_operations = {
  2385. .readlink = generic_readlink,
  2386. .follow_link = shmem_follow_link,
  2387. .put_link = shmem_put_link,
  2388. #ifdef CONFIG_TMPFS_XATTR
  2389. .setxattr = shmem_setxattr,
  2390. .getxattr = shmem_getxattr,
  2391. .listxattr = shmem_listxattr,
  2392. .removexattr = shmem_removexattr,
  2393. #endif
  2394. };
  2395. static struct dentry *shmem_get_parent(struct dentry *child)
  2396. {
  2397. return ERR_PTR(-ESTALE);
  2398. }
  2399. static int shmem_match(struct inode *ino, void *vfh)
  2400. {
  2401. __u32 *fh = vfh;
  2402. __u64 inum = fh[2];
  2403. inum = (inum << 32) | fh[1];
  2404. return ino->i_ino == inum && fh[0] == ino->i_generation;
  2405. }
  2406. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  2407. struct fid *fid, int fh_len, int fh_type)
  2408. {
  2409. struct inode *inode;
  2410. struct dentry *dentry = NULL;
  2411. u64 inum;
  2412. if (fh_len < 3)
  2413. return NULL;
  2414. inum = fid->raw[2];
  2415. inum = (inum << 32) | fid->raw[1];
  2416. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  2417. shmem_match, fid->raw);
  2418. if (inode) {
  2419. dentry = d_find_alias(inode);
  2420. iput(inode);
  2421. }
  2422. return dentry;
  2423. }
  2424. static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
  2425. struct inode *parent)
  2426. {
  2427. if (*len < 3) {
  2428. *len = 3;
  2429. return 255;
  2430. }
  2431. if (inode_unhashed(inode)) {
  2432. /* Unfortunately insert_inode_hash is not idempotent,
  2433. * so as we hash inodes here rather than at creation
  2434. * time, we need a lock to ensure we only try
  2435. * to do it once
  2436. */
  2437. static DEFINE_SPINLOCK(lock);
  2438. spin_lock(&lock);
  2439. if (inode_unhashed(inode))
  2440. __insert_inode_hash(inode,
  2441. inode->i_ino + inode->i_generation);
  2442. spin_unlock(&lock);
  2443. }
  2444. fh[0] = inode->i_generation;
  2445. fh[1] = inode->i_ino;
  2446. fh[2] = ((__u64)inode->i_ino) >> 32;
  2447. *len = 3;
  2448. return 1;
  2449. }
  2450. static const struct export_operations shmem_export_ops = {
  2451. .get_parent = shmem_get_parent,
  2452. .encode_fh = shmem_encode_fh,
  2453. .fh_to_dentry = shmem_fh_to_dentry,
  2454. };
  2455. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  2456. bool remount)
  2457. {
  2458. char *this_char, *value, *rest;
  2459. struct mempolicy *mpol = NULL;
  2460. while (options != NULL) {
  2461. this_char = options;
  2462. for (;;) {
  2463. /*
  2464. * NUL-terminate this option: unfortunately,
  2465. * mount options form a comma-separated list,
  2466. * but mpol's nodelist may also contain commas.
  2467. */
  2468. options = strchr(options, ',');
  2469. if (options == NULL)
  2470. break;
  2471. options++;
  2472. if (!isdigit(*options)) {
  2473. options[-1] = '\0';
  2474. break;
  2475. }
  2476. }
  2477. if (!*this_char)
  2478. continue;
  2479. if ((value = strchr(this_char,'=')) != NULL) {
  2480. *value++ = 0;
  2481. } else {
  2482. printk(KERN_ERR
  2483. "tmpfs: No value for mount option '%s'\n",
  2484. this_char);
  2485. goto error;
  2486. }
  2487. if (!strcmp(this_char,"size")) {
  2488. unsigned long long size;
  2489. size = memparse(value,&rest);
  2490. if (*rest == '%') {
  2491. size <<= PAGE_SHIFT;
  2492. size *= totalram_pages;
  2493. do_div(size, 100);
  2494. rest++;
  2495. }
  2496. if (*rest)
  2497. goto bad_val;
  2498. sbinfo->max_blocks =
  2499. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  2500. } else if (!strcmp(this_char,"nr_blocks")) {
  2501. sbinfo->max_blocks = memparse(value, &rest);
  2502. if (*rest)
  2503. goto bad_val;
  2504. } else if (!strcmp(this_char,"nr_inodes")) {
  2505. sbinfo->max_inodes = memparse(value, &rest);
  2506. if (*rest)
  2507. goto bad_val;
  2508. } else if (!strcmp(this_char,"mode")) {
  2509. if (remount)
  2510. continue;
  2511. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  2512. if (*rest)
  2513. goto bad_val;
  2514. } else if (!strcmp(this_char,"uid")) {
  2515. if (remount)
  2516. continue;
  2517. sbinfo->uid = simple_strtoul(value, &rest, 0);
  2518. if (*rest)
  2519. goto bad_val;
  2520. } else if (!strcmp(this_char,"gid")) {
  2521. if (remount)
  2522. continue;
  2523. sbinfo->gid = simple_strtoul(value, &rest, 0);
  2524. if (*rest)
  2525. goto bad_val;
  2526. } else if (!strcmp(this_char,"mpol")) {
  2527. mpol_put(mpol);
  2528. mpol = NULL;
  2529. if (mpol_parse_str(value, &mpol, 1))
  2530. goto bad_val;
  2531. } else {
  2532. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  2533. this_char);
  2534. goto error;
  2535. }
  2536. }
  2537. sbinfo->mpol = mpol;
  2538. return 0;
  2539. bad_val:
  2540. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  2541. value, this_char);
  2542. error:
  2543. mpol_put(mpol);
  2544. return 1;
  2545. }
  2546. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  2547. {
  2548. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2549. struct shmem_sb_info config = *sbinfo;
  2550. unsigned long inodes;
  2551. int error = -EINVAL;
  2552. config.mpol = NULL;
  2553. if (shmem_parse_options(data, &config, true))
  2554. return error;
  2555. spin_lock(&sbinfo->stat_lock);
  2556. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  2557. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  2558. goto out;
  2559. if (config.max_inodes < inodes)
  2560. goto out;
  2561. /*
  2562. * Those tests disallow limited->unlimited while any are in use;
  2563. * but we must separately disallow unlimited->limited, because
  2564. * in that case we have no record of how much is already in use.
  2565. */
  2566. if (config.max_blocks && !sbinfo->max_blocks)
  2567. goto out;
  2568. if (config.max_inodes && !sbinfo->max_inodes)
  2569. goto out;
  2570. error = 0;
  2571. sbinfo->max_blocks = config.max_blocks;
  2572. sbinfo->max_inodes = config.max_inodes;
  2573. sbinfo->free_inodes = config.max_inodes - inodes;
  2574. /*
  2575. * Preserve previous mempolicy unless mpol remount option was specified.
  2576. */
  2577. if (config.mpol) {
  2578. mpol_put(sbinfo->mpol);
  2579. sbinfo->mpol = config.mpol; /* transfers initial ref */
  2580. }
  2581. out:
  2582. spin_unlock(&sbinfo->stat_lock);
  2583. return error;
  2584. }
  2585. static int shmem_show_options(struct seq_file *seq, struct dentry *root)
  2586. {
  2587. struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
  2588. if (sbinfo->max_blocks != shmem_default_max_blocks())
  2589. seq_printf(seq, ",size=%luk",
  2590. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  2591. if (sbinfo->max_inodes != shmem_default_max_inodes())
  2592. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  2593. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  2594. seq_printf(seq, ",mode=%03ho", sbinfo->mode);
  2595. if (sbinfo->uid != 0)
  2596. seq_printf(seq, ",uid=%u", sbinfo->uid);
  2597. if (sbinfo->gid != 0)
  2598. seq_printf(seq, ",gid=%u", sbinfo->gid);
  2599. shmem_show_mpol(seq, sbinfo->mpol);
  2600. return 0;
  2601. }
  2602. #define MFD_NAME_PREFIX "memfd:"
  2603. #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
  2604. #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
  2605. #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
  2606. SYSCALL_DEFINE2(memfd_create,
  2607. const char __user *, uname,
  2608. unsigned int, flags)
  2609. {
  2610. struct shmem_inode_info *info;
  2611. struct file *file;
  2612. int fd, error;
  2613. char *name;
  2614. long len;
  2615. if (flags & ~(unsigned int)MFD_ALL_FLAGS)
  2616. return -EINVAL;
  2617. /* length includes terminating zero */
  2618. len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
  2619. if (len <= 0)
  2620. return -EFAULT;
  2621. if (len > MFD_NAME_MAX_LEN + 1)
  2622. return -EINVAL;
  2623. name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
  2624. if (!name)
  2625. return -ENOMEM;
  2626. strcpy(name, MFD_NAME_PREFIX);
  2627. if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
  2628. error = -EFAULT;
  2629. goto err_name;
  2630. }
  2631. /* terminating-zero may have changed after strnlen_user() returned */
  2632. if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
  2633. error = -EFAULT;
  2634. goto err_name;
  2635. }
  2636. fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
  2637. if (fd < 0) {
  2638. error = fd;
  2639. goto err_name;
  2640. }
  2641. file = shmem_file_setup(name, 0, VM_NORESERVE);
  2642. if (IS_ERR(file)) {
  2643. error = PTR_ERR(file);
  2644. goto err_fd;
  2645. }
  2646. info = SHMEM_I(file_inode(file));
  2647. file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
  2648. file->f_flags |= O_RDWR | O_LARGEFILE;
  2649. if (flags & MFD_ALLOW_SEALING)
  2650. info->seals &= ~F_SEAL_SEAL;
  2651. fd_install(fd, file);
  2652. kfree(name);
  2653. return fd;
  2654. err_fd:
  2655. put_unused_fd(fd);
  2656. err_name:
  2657. kfree(name);
  2658. return error;
  2659. }
  2660. #endif /* CONFIG_TMPFS */
  2661. static void shmem_put_super(struct super_block *sb)
  2662. {
  2663. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2664. percpu_counter_destroy(&sbinfo->used_blocks);
  2665. mpol_put(sbinfo->mpol);
  2666. kfree(sbinfo);
  2667. sb->s_fs_info = NULL;
  2668. }
  2669. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  2670. {
  2671. struct inode *inode;
  2672. struct shmem_sb_info *sbinfo;
  2673. int err = -ENOMEM;
  2674. /* Round up to L1_CACHE_BYTES to resist false sharing */
  2675. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  2676. L1_CACHE_BYTES), GFP_KERNEL);
  2677. if (!sbinfo)
  2678. return -ENOMEM;
  2679. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  2680. sbinfo->uid = current_fsuid();
  2681. sbinfo->gid = current_fsgid();
  2682. sb->s_fs_info = sbinfo;
  2683. #ifdef CONFIG_TMPFS
  2684. /*
  2685. * Per default we only allow half of the physical ram per
  2686. * tmpfs instance, limiting inodes to one per page of lowmem;
  2687. * but the internal instance is left unlimited.
  2688. */
  2689. if (!(sb->s_flags & MS_NOUSER)) {
  2690. sbinfo->max_blocks = shmem_default_max_blocks();
  2691. sbinfo->max_inodes = shmem_default_max_inodes();
  2692. if (shmem_parse_options(data, sbinfo, false)) {
  2693. err = -EINVAL;
  2694. goto failed;
  2695. }
  2696. }
  2697. sb->s_export_op = &shmem_export_ops;
  2698. sb->s_flags |= MS_NOSEC;
  2699. #else
  2700. sb->s_flags |= MS_NOUSER;
  2701. #endif
  2702. spin_lock_init(&sbinfo->stat_lock);
  2703. if (percpu_counter_init(&sbinfo->used_blocks, 0))
  2704. goto failed;
  2705. sbinfo->free_inodes = sbinfo->max_inodes;
  2706. sb->s_maxbytes = MAX_LFS_FILESIZE;
  2707. sb->s_blocksize = PAGE_CACHE_SIZE;
  2708. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  2709. sb->s_magic = TMPFS_MAGIC;
  2710. sb->s_op = &shmem_ops;
  2711. sb->s_time_gran = 1;
  2712. #ifdef CONFIG_TMPFS_XATTR
  2713. sb->s_xattr = shmem_xattr_handlers;
  2714. #endif
  2715. #ifdef CONFIG_TMPFS_POSIX_ACL
  2716. sb->s_flags |= MS_POSIXACL;
  2717. #endif
  2718. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
  2719. if (!inode)
  2720. goto failed;
  2721. inode->i_uid = sbinfo->uid;
  2722. inode->i_gid = sbinfo->gid;
  2723. sb->s_root = d_make_root(inode);
  2724. if (!sb->s_root)
  2725. goto failed;
  2726. return 0;
  2727. failed:
  2728. shmem_put_super(sb);
  2729. return err;
  2730. }
  2731. static struct kmem_cache *shmem_inode_cachep;
  2732. static struct inode *shmem_alloc_inode(struct super_block *sb)
  2733. {
  2734. struct shmem_inode_info *info;
  2735. info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  2736. if (!info)
  2737. return NULL;
  2738. return &info->vfs_inode;
  2739. }
  2740. static void shmem_destroy_callback(struct rcu_head *head)
  2741. {
  2742. struct inode *inode = container_of(head, struct inode, i_rcu);
  2743. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  2744. }
  2745. static void shmem_destroy_inode(struct inode *inode)
  2746. {
  2747. if (S_ISREG(inode->i_mode))
  2748. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  2749. call_rcu(&inode->i_rcu, shmem_destroy_callback);
  2750. }
  2751. static void shmem_init_inode(void *foo)
  2752. {
  2753. struct shmem_inode_info *info = foo;
  2754. inode_init_once(&info->vfs_inode);
  2755. }
  2756. static int shmem_init_inodecache(void)
  2757. {
  2758. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  2759. sizeof(struct shmem_inode_info),
  2760. 0, SLAB_PANIC, shmem_init_inode);
  2761. return 0;
  2762. }
  2763. static void shmem_destroy_inodecache(void)
  2764. {
  2765. kmem_cache_destroy(shmem_inode_cachep);
  2766. }
  2767. static const struct address_space_operations shmem_aops = {
  2768. .writepage = shmem_writepage,
  2769. .set_page_dirty = __set_page_dirty_no_writeback,
  2770. #ifdef CONFIG_TMPFS
  2771. .write_begin = shmem_write_begin,
  2772. .write_end = shmem_write_end,
  2773. #endif
  2774. .migratepage = migrate_page,
  2775. .error_remove_page = generic_error_remove_page,
  2776. };
  2777. static const struct file_operations shmem_file_operations = {
  2778. .mmap = shmem_mmap,
  2779. #ifdef CONFIG_TMPFS
  2780. .llseek = shmem_file_llseek,
  2781. .read = do_sync_read,
  2782. .write = do_sync_write,
  2783. .aio_read = shmem_file_aio_read,
  2784. .aio_write = generic_file_aio_write,
  2785. .fsync = noop_fsync,
  2786. .splice_read = shmem_file_splice_read,
  2787. .splice_write = generic_file_splice_write,
  2788. .fallocate = shmem_fallocate,
  2789. #endif
  2790. };
  2791. static const struct inode_operations shmem_inode_operations = {
  2792. .setattr = shmem_setattr,
  2793. #ifdef CONFIG_TMPFS_XATTR
  2794. .setxattr = shmem_setxattr,
  2795. .getxattr = shmem_getxattr,
  2796. .listxattr = shmem_listxattr,
  2797. .removexattr = shmem_removexattr,
  2798. .set_acl = simple_set_acl,
  2799. #endif
  2800. };
  2801. static const struct inode_operations shmem_dir_inode_operations = {
  2802. #ifdef CONFIG_TMPFS
  2803. .create = shmem_create,
  2804. .lookup = simple_lookup,
  2805. .link = shmem_link,
  2806. .unlink = shmem_unlink,
  2807. .symlink = shmem_symlink,
  2808. .mkdir = shmem_mkdir,
  2809. .rmdir = shmem_rmdir,
  2810. .mknod = shmem_mknod,
  2811. .rename = shmem_rename,
  2812. #endif
  2813. #ifdef CONFIG_TMPFS_XATTR
  2814. .setxattr = shmem_setxattr,
  2815. .getxattr = shmem_getxattr,
  2816. .listxattr = shmem_listxattr,
  2817. .removexattr = shmem_removexattr,
  2818. #endif
  2819. #ifdef CONFIG_TMPFS_POSIX_ACL
  2820. .setattr = shmem_setattr,
  2821. .set_acl = simple_set_acl,
  2822. #endif
  2823. };
  2824. static const struct inode_operations shmem_special_inode_operations = {
  2825. #ifdef CONFIG_TMPFS_XATTR
  2826. .setxattr = shmem_setxattr,
  2827. .getxattr = shmem_getxattr,
  2828. .listxattr = shmem_listxattr,
  2829. .removexattr = shmem_removexattr,
  2830. #endif
  2831. #ifdef CONFIG_TMPFS_POSIX_ACL
  2832. .setattr = shmem_setattr,
  2833. .set_acl = simple_set_acl,
  2834. #endif
  2835. };
  2836. static const struct super_operations shmem_ops = {
  2837. .alloc_inode = shmem_alloc_inode,
  2838. .destroy_inode = shmem_destroy_inode,
  2839. #ifdef CONFIG_TMPFS
  2840. .statfs = shmem_statfs,
  2841. .remount_fs = shmem_remount_fs,
  2842. .show_options = shmem_show_options,
  2843. #endif
  2844. .evict_inode = shmem_evict_inode,
  2845. .drop_inode = generic_delete_inode,
  2846. .put_super = shmem_put_super,
  2847. };
  2848. static const struct vm_operations_struct shmem_vm_ops = {
  2849. .fault = shmem_fault,
  2850. #ifdef CONFIG_NUMA
  2851. .set_policy = shmem_set_policy,
  2852. .get_policy = shmem_get_policy,
  2853. #endif
  2854. .remap_pages = generic_file_remap_pages,
  2855. };
  2856. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  2857. int flags, const char *dev_name, void *data)
  2858. {
  2859. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  2860. }
  2861. static struct file_system_type shmem_fs_type = {
  2862. .owner = THIS_MODULE,
  2863. .name = "tmpfs",
  2864. .mount = shmem_mount,
  2865. .kill_sb = kill_litter_super,
  2866. .fs_flags = FS_USERNS_MOUNT,
  2867. };
  2868. int __init shmem_init(void)
  2869. {
  2870. int error;
  2871. error = bdi_init(&shmem_backing_dev_info);
  2872. if (error)
  2873. goto out4;
  2874. error = shmem_init_inodecache();
  2875. if (error)
  2876. goto out3;
  2877. error = register_filesystem(&shmem_fs_type);
  2878. if (error) {
  2879. printk(KERN_ERR "Could not register tmpfs\n");
  2880. goto out2;
  2881. }
  2882. shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
  2883. shmem_fs_type.name, NULL);
  2884. if (IS_ERR(shm_mnt)) {
  2885. error = PTR_ERR(shm_mnt);
  2886. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2887. goto out1;
  2888. }
  2889. return 0;
  2890. out1:
  2891. unregister_filesystem(&shmem_fs_type);
  2892. out2:
  2893. shmem_destroy_inodecache();
  2894. out3:
  2895. bdi_destroy(&shmem_backing_dev_info);
  2896. out4:
  2897. shm_mnt = ERR_PTR(error);
  2898. return error;
  2899. }
  2900. #else /* !CONFIG_SHMEM */
  2901. /*
  2902. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  2903. *
  2904. * This is intended for small system where the benefits of the full
  2905. * shmem code (swap-backed and resource-limited) are outweighed by
  2906. * their complexity. On systems without swap this code should be
  2907. * effectively equivalent, but much lighter weight.
  2908. */
  2909. #include <linux/ramfs.h>
  2910. static struct file_system_type shmem_fs_type = {
  2911. .name = "tmpfs",
  2912. .mount = ramfs_mount,
  2913. .kill_sb = kill_litter_super,
  2914. .fs_flags = FS_USERNS_MOUNT,
  2915. };
  2916. int __init shmem_init(void)
  2917. {
  2918. BUG_ON(register_filesystem(&shmem_fs_type) != 0);
  2919. shm_mnt = kern_mount(&shmem_fs_type);
  2920. BUG_ON(IS_ERR(shm_mnt));
  2921. return 0;
  2922. }
  2923. int shmem_unuse(swp_entry_t swap, struct page *page)
  2924. {
  2925. return 0;
  2926. }
  2927. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  2928. {
  2929. return 0;
  2930. }
  2931. void shmem_unlock_mapping(struct address_space *mapping)
  2932. {
  2933. }
  2934. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  2935. {
  2936. truncate_inode_pages_range(inode->i_mapping, lstart, lend);
  2937. }
  2938. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  2939. #define shmem_vm_ops generic_file_vm_ops
  2940. #define shmem_file_operations ramfs_file_operations
  2941. #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
  2942. #define shmem_acct_size(flags, size) 0
  2943. #define shmem_unacct_size(flags, size) do {} while (0)
  2944. #endif /* CONFIG_SHMEM */
  2945. /* common code */
  2946. static struct file *__shmem_file_setup(const char *name, loff_t size,
  2947. unsigned long flags, unsigned int i_flags)
  2948. {
  2949. int error;
  2950. struct file *file;
  2951. struct inode *inode;
  2952. struct path path;
  2953. struct dentry *root;
  2954. struct qstr this;
  2955. if (IS_ERR(shm_mnt))
  2956. return (void *)shm_mnt;
  2957. if (size < 0 || size > MAX_LFS_FILESIZE)
  2958. return ERR_PTR(-EINVAL);
  2959. if (shmem_acct_size(flags, size))
  2960. return ERR_PTR(-ENOMEM);
  2961. error = -ENOMEM;
  2962. this.name = name;
  2963. this.len = strlen(name);
  2964. this.hash = 0; /* will go */
  2965. root = shm_mnt->mnt_root;
  2966. path.dentry = d_alloc(root, &this);
  2967. if (!path.dentry)
  2968. goto put_memory;
  2969. path.mnt = mntget(shm_mnt);
  2970. error = -ENOSPC;
  2971. inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
  2972. if (!inode)
  2973. goto put_dentry;
  2974. inode->i_flags |= i_flags;
  2975. d_instantiate(path.dentry, inode);
  2976. inode->i_size = size;
  2977. clear_nlink(inode); /* It is unlinked */
  2978. #ifndef CONFIG_MMU
  2979. error = ramfs_nommu_expand_for_mapping(inode, size);
  2980. if (error)
  2981. goto put_dentry;
  2982. #endif
  2983. error = -ENFILE;
  2984. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  2985. &shmem_file_operations);
  2986. if (!file)
  2987. goto put_dentry;
  2988. return file;
  2989. put_dentry:
  2990. path_put(&path);
  2991. put_memory:
  2992. shmem_unacct_size(flags, size);
  2993. return ERR_PTR(error);
  2994. }
  2995. /**
  2996. * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
  2997. * kernel internal. There will be NO LSM permission checks against the
  2998. * underlying inode. So users of this interface must do LSM checks at a
  2999. * higher layer. The one user is the big_key implementation. LSM checks
  3000. * are provided at the key level rather than the inode level.
  3001. * @name: name for dentry (to be seen in /proc/<pid>/maps
  3002. * @size: size to be set for the file
  3003. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  3004. */
  3005. struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
  3006. {
  3007. return __shmem_file_setup(name, size, flags, S_PRIVATE);
  3008. }
  3009. /**
  3010. * shmem_file_setup - get an unlinked file living in tmpfs
  3011. * @name: name for dentry (to be seen in /proc/<pid>/maps
  3012. * @size: size to be set for the file
  3013. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  3014. */
  3015. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  3016. {
  3017. return __shmem_file_setup(name, size, flags, 0);
  3018. }
  3019. EXPORT_SYMBOL_GPL(shmem_file_setup);
  3020. void shmem_set_file(struct vm_area_struct *vma, struct file *file)
  3021. {
  3022. if (vma->vm_file)
  3023. fput(vma->vm_file);
  3024. vma->vm_file = file;
  3025. vma->vm_ops = &shmem_vm_ops;
  3026. }
  3027. /**
  3028. * shmem_zero_setup - setup a shared anonymous mapping
  3029. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  3030. */
  3031. int shmem_zero_setup(struct vm_area_struct *vma)
  3032. {
  3033. struct file *file;
  3034. loff_t size = vma->vm_end - vma->vm_start;
  3035. /*
  3036. * Cloning a new file under mmap_sem leads to a lock ordering conflict
  3037. * between XFS directory reading and selinux: since this file is only
  3038. * accessible to the user through its mapping, use S_PRIVATE flag to
  3039. * bypass file security, in the same way as shmem_kernel_file_setup().
  3040. */
  3041. file = __shmem_file_setup("dev/zero", size, vma->vm_flags, S_PRIVATE);
  3042. if (IS_ERR(file))
  3043. return PTR_ERR(file);
  3044. shmem_set_file(vma, file);
  3045. return 0;
  3046. }
  3047. /**
  3048. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  3049. * @mapping: the page's address_space
  3050. * @index: the page index
  3051. * @gfp: the page allocator flags to use if allocating
  3052. *
  3053. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  3054. * with any new page allocations done using the specified allocation flags.
  3055. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  3056. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  3057. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  3058. *
  3059. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  3060. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  3061. */
  3062. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  3063. pgoff_t index, gfp_t gfp)
  3064. {
  3065. #ifdef CONFIG_SHMEM
  3066. struct inode *inode = mapping->host;
  3067. struct page *page;
  3068. int error;
  3069. BUG_ON(mapping->a_ops != &shmem_aops);
  3070. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
  3071. if (error)
  3072. page = ERR_PTR(error);
  3073. else
  3074. unlock_page(page);
  3075. return page;
  3076. #else
  3077. /*
  3078. * The tiny !SHMEM case uses ramfs without swap
  3079. */
  3080. return read_cache_page_gfp(mapping, index, gfp);
  3081. #endif
  3082. }
  3083. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);