shmem.c 90 KB

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