namespace.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460
  1. /*
  2. * linux/fs/namespace.c
  3. *
  4. * (C) Copyright Al Viro 2000, 2001
  5. * Released under GPL v2.
  6. *
  7. * Based on code from fs/super.c, copyright Linus Torvalds and others.
  8. * Heavily rewritten.
  9. */
  10. #include <linux/syscalls.h>
  11. #include <linux/export.h>
  12. #include <linux/capability.h>
  13. #include <linux/mnt_namespace.h>
  14. #include <linux/user_namespace.h>
  15. #include <linux/namei.h>
  16. #include <linux/security.h>
  17. #include <linux/idr.h>
  18. #include <linux/init.h> /* init_rootfs */
  19. #include <linux/fs_struct.h> /* get_fs_root et.al. */
  20. #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
  21. #include <linux/uaccess.h>
  22. #include <linux/proc_ns.h>
  23. #include <linux/magic.h>
  24. #include <linux/bootmem.h>
  25. #include <linux/task_work.h>
  26. #include "pnode.h"
  27. #include "internal.h"
  28. /* Maximum number of mounts in a mount namespace */
  29. unsigned int sysctl_mount_max __read_mostly = 100000;
  30. static unsigned int m_hash_mask __read_mostly;
  31. static unsigned int m_hash_shift __read_mostly;
  32. static unsigned int mp_hash_mask __read_mostly;
  33. static unsigned int mp_hash_shift __read_mostly;
  34. static __initdata unsigned long mhash_entries;
  35. static int __init set_mhash_entries(char *str)
  36. {
  37. if (!str)
  38. return 0;
  39. mhash_entries = simple_strtoul(str, &str, 0);
  40. return 1;
  41. }
  42. __setup("mhash_entries=", set_mhash_entries);
  43. static __initdata unsigned long mphash_entries;
  44. static int __init set_mphash_entries(char *str)
  45. {
  46. if (!str)
  47. return 0;
  48. mphash_entries = simple_strtoul(str, &str, 0);
  49. return 1;
  50. }
  51. __setup("mphash_entries=", set_mphash_entries);
  52. static u64 event;
  53. static DEFINE_IDA(mnt_id_ida);
  54. static DEFINE_IDA(mnt_group_ida);
  55. static DEFINE_SPINLOCK(mnt_id_lock);
  56. static int mnt_id_start = 0;
  57. static int mnt_group_start = 1;
  58. static struct hlist_head *mount_hashtable __read_mostly;
  59. static struct hlist_head *mountpoint_hashtable __read_mostly;
  60. static struct kmem_cache *mnt_cache __read_mostly;
  61. static DECLARE_RWSEM(namespace_sem);
  62. /* /sys/fs */
  63. struct kobject *fs_kobj;
  64. EXPORT_SYMBOL_GPL(fs_kobj);
  65. /*
  66. * vfsmount lock may be taken for read to prevent changes to the
  67. * vfsmount hash, ie. during mountpoint lookups or walking back
  68. * up the tree.
  69. *
  70. * It should be taken for write in all cases where the vfsmount
  71. * tree or hash is modified or when a vfsmount structure is modified.
  72. */
  73. __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
  74. static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
  75. {
  76. unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
  77. tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
  78. tmp = tmp + (tmp >> m_hash_shift);
  79. return &mount_hashtable[tmp & m_hash_mask];
  80. }
  81. static inline struct hlist_head *mp_hash(struct dentry *dentry)
  82. {
  83. unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
  84. tmp = tmp + (tmp >> mp_hash_shift);
  85. return &mountpoint_hashtable[tmp & mp_hash_mask];
  86. }
  87. /*
  88. * allocation is serialized by namespace_sem, but we need the spinlock to
  89. * serialize with freeing.
  90. */
  91. static int mnt_alloc_id(struct mount *mnt)
  92. {
  93. int res;
  94. retry:
  95. ida_pre_get(&mnt_id_ida, GFP_KERNEL);
  96. spin_lock(&mnt_id_lock);
  97. res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
  98. if (!res)
  99. mnt_id_start = mnt->mnt_id + 1;
  100. spin_unlock(&mnt_id_lock);
  101. if (res == -EAGAIN)
  102. goto retry;
  103. return res;
  104. }
  105. static void mnt_free_id(struct mount *mnt)
  106. {
  107. int id = mnt->mnt_id;
  108. spin_lock(&mnt_id_lock);
  109. ida_remove(&mnt_id_ida, id);
  110. if (mnt_id_start > id)
  111. mnt_id_start = id;
  112. spin_unlock(&mnt_id_lock);
  113. }
  114. /*
  115. * Allocate a new peer group ID
  116. *
  117. * mnt_group_ida is protected by namespace_sem
  118. */
  119. static int mnt_alloc_group_id(struct mount *mnt)
  120. {
  121. int res;
  122. if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
  123. return -ENOMEM;
  124. res = ida_get_new_above(&mnt_group_ida,
  125. mnt_group_start,
  126. &mnt->mnt_group_id);
  127. if (!res)
  128. mnt_group_start = mnt->mnt_group_id + 1;
  129. return res;
  130. }
  131. /*
  132. * Release a peer group ID
  133. */
  134. void mnt_release_group_id(struct mount *mnt)
  135. {
  136. int id = mnt->mnt_group_id;
  137. ida_remove(&mnt_group_ida, id);
  138. if (mnt_group_start > id)
  139. mnt_group_start = id;
  140. mnt->mnt_group_id = 0;
  141. }
  142. /*
  143. * vfsmount lock must be held for read
  144. */
  145. static inline void mnt_add_count(struct mount *mnt, int n)
  146. {
  147. #ifdef CONFIG_SMP
  148. this_cpu_add(mnt->mnt_pcp->mnt_count, n);
  149. #else
  150. preempt_disable();
  151. mnt->mnt_count += n;
  152. preempt_enable();
  153. #endif
  154. }
  155. /*
  156. * vfsmount lock must be held for write
  157. */
  158. unsigned int mnt_get_count(struct mount *mnt)
  159. {
  160. #ifdef CONFIG_SMP
  161. unsigned int count = 0;
  162. int cpu;
  163. for_each_possible_cpu(cpu) {
  164. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
  165. }
  166. return count;
  167. #else
  168. return mnt->mnt_count;
  169. #endif
  170. }
  171. static void drop_mountpoint(struct fs_pin *p)
  172. {
  173. struct mount *m = container_of(p, struct mount, mnt_umount);
  174. dput(m->mnt_ex_mountpoint);
  175. pin_remove(p);
  176. mntput(&m->mnt);
  177. }
  178. static struct mount *alloc_vfsmnt(const char *name)
  179. {
  180. struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
  181. if (mnt) {
  182. int err;
  183. err = mnt_alloc_id(mnt);
  184. if (err)
  185. goto out_free_cache;
  186. if (name) {
  187. mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
  188. if (!mnt->mnt_devname)
  189. goto out_free_id;
  190. }
  191. #ifdef CONFIG_SMP
  192. mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
  193. if (!mnt->mnt_pcp)
  194. goto out_free_devname;
  195. this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
  196. #else
  197. mnt->mnt_count = 1;
  198. mnt->mnt_writers = 0;
  199. #endif
  200. INIT_HLIST_NODE(&mnt->mnt_hash);
  201. INIT_LIST_HEAD(&mnt->mnt_child);
  202. INIT_LIST_HEAD(&mnt->mnt_mounts);
  203. INIT_LIST_HEAD(&mnt->mnt_list);
  204. INIT_LIST_HEAD(&mnt->mnt_expire);
  205. INIT_LIST_HEAD(&mnt->mnt_share);
  206. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  207. INIT_LIST_HEAD(&mnt->mnt_slave);
  208. INIT_HLIST_NODE(&mnt->mnt_mp_list);
  209. #ifdef CONFIG_FSNOTIFY
  210. INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
  211. #endif
  212. init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
  213. }
  214. return mnt;
  215. #ifdef CONFIG_SMP
  216. out_free_devname:
  217. kfree_const(mnt->mnt_devname);
  218. #endif
  219. out_free_id:
  220. mnt_free_id(mnt);
  221. out_free_cache:
  222. kmem_cache_free(mnt_cache, mnt);
  223. return NULL;
  224. }
  225. /*
  226. * Most r/o checks on a fs are for operations that take
  227. * discrete amounts of time, like a write() or unlink().
  228. * We must keep track of when those operations start
  229. * (for permission checks) and when they end, so that
  230. * we can determine when writes are able to occur to
  231. * a filesystem.
  232. */
  233. /*
  234. * __mnt_is_readonly: check whether a mount is read-only
  235. * @mnt: the mount to check for its write status
  236. *
  237. * This shouldn't be used directly ouside of the VFS.
  238. * It does not guarantee that the filesystem will stay
  239. * r/w, just that it is right *now*. This can not and
  240. * should not be used in place of IS_RDONLY(inode).
  241. * mnt_want/drop_write() will _keep_ the filesystem
  242. * r/w.
  243. */
  244. int __mnt_is_readonly(struct vfsmount *mnt)
  245. {
  246. if (mnt->mnt_flags & MNT_READONLY)
  247. return 1;
  248. if (mnt->mnt_sb->s_flags & MS_RDONLY)
  249. return 1;
  250. return 0;
  251. }
  252. EXPORT_SYMBOL_GPL(__mnt_is_readonly);
  253. static inline void mnt_inc_writers(struct mount *mnt)
  254. {
  255. #ifdef CONFIG_SMP
  256. this_cpu_inc(mnt->mnt_pcp->mnt_writers);
  257. #else
  258. mnt->mnt_writers++;
  259. #endif
  260. }
  261. static inline void mnt_dec_writers(struct mount *mnt)
  262. {
  263. #ifdef CONFIG_SMP
  264. this_cpu_dec(mnt->mnt_pcp->mnt_writers);
  265. #else
  266. mnt->mnt_writers--;
  267. #endif
  268. }
  269. static unsigned int mnt_get_writers(struct mount *mnt)
  270. {
  271. #ifdef CONFIG_SMP
  272. unsigned int count = 0;
  273. int cpu;
  274. for_each_possible_cpu(cpu) {
  275. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
  276. }
  277. return count;
  278. #else
  279. return mnt->mnt_writers;
  280. #endif
  281. }
  282. static int mnt_is_readonly(struct vfsmount *mnt)
  283. {
  284. if (mnt->mnt_sb->s_readonly_remount)
  285. return 1;
  286. /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
  287. smp_rmb();
  288. return __mnt_is_readonly(mnt);
  289. }
  290. /*
  291. * Most r/o & frozen checks on a fs are for operations that take discrete
  292. * amounts of time, like a write() or unlink(). We must keep track of when
  293. * those operations start (for permission checks) and when they end, so that we
  294. * can determine when writes are able to occur to a filesystem.
  295. */
  296. /**
  297. * __mnt_want_write - get write access to a mount without freeze protection
  298. * @m: the mount on which to take a write
  299. *
  300. * This tells the low-level filesystem that a write is about to be performed to
  301. * it, and makes sure that writes are allowed (mnt it read-write) before
  302. * returning success. This operation does not protect against filesystem being
  303. * frozen. When the write operation is finished, __mnt_drop_write() must be
  304. * called. This is effectively a refcount.
  305. */
  306. int __mnt_want_write(struct vfsmount *m)
  307. {
  308. struct mount *mnt = real_mount(m);
  309. int ret = 0;
  310. preempt_disable();
  311. mnt_inc_writers(mnt);
  312. /*
  313. * The store to mnt_inc_writers must be visible before we pass
  314. * MNT_WRITE_HOLD loop below, so that the slowpath can see our
  315. * incremented count after it has set MNT_WRITE_HOLD.
  316. */
  317. smp_mb();
  318. while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  319. cpu_relax();
  320. /*
  321. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  322. * be set to match its requirements. So we must not load that until
  323. * MNT_WRITE_HOLD is cleared.
  324. */
  325. smp_rmb();
  326. if (mnt_is_readonly(m)) {
  327. mnt_dec_writers(mnt);
  328. ret = -EROFS;
  329. }
  330. preempt_enable();
  331. return ret;
  332. }
  333. /**
  334. * mnt_want_write - get write access to a mount
  335. * @m: the mount on which to take a write
  336. *
  337. * This tells the low-level filesystem that a write is about to be performed to
  338. * it, and makes sure that writes are allowed (mount is read-write, filesystem
  339. * is not frozen) before returning success. When the write operation is
  340. * finished, mnt_drop_write() must be called. This is effectively a refcount.
  341. */
  342. int mnt_want_write(struct vfsmount *m)
  343. {
  344. int ret;
  345. sb_start_write(m->mnt_sb);
  346. ret = __mnt_want_write(m);
  347. if (ret)
  348. sb_end_write(m->mnt_sb);
  349. return ret;
  350. }
  351. EXPORT_SYMBOL_GPL(mnt_want_write);
  352. /**
  353. * mnt_clone_write - get write access to a mount
  354. * @mnt: the mount on which to take a write
  355. *
  356. * This is effectively like mnt_want_write, except
  357. * it must only be used to take an extra write reference
  358. * on a mountpoint that we already know has a write reference
  359. * on it. This allows some optimisation.
  360. *
  361. * After finished, mnt_drop_write must be called as usual to
  362. * drop the reference.
  363. */
  364. int mnt_clone_write(struct vfsmount *mnt)
  365. {
  366. /* superblock may be r/o */
  367. if (__mnt_is_readonly(mnt))
  368. return -EROFS;
  369. preempt_disable();
  370. mnt_inc_writers(real_mount(mnt));
  371. preempt_enable();
  372. return 0;
  373. }
  374. EXPORT_SYMBOL_GPL(mnt_clone_write);
  375. /**
  376. * __mnt_want_write_file - get write access to a file's mount
  377. * @file: the file who's mount on which to take a write
  378. *
  379. * This is like __mnt_want_write, but it takes a file and can
  380. * do some optimisations if the file is open for write already
  381. */
  382. int __mnt_want_write_file(struct file *file)
  383. {
  384. if (!(file->f_mode & FMODE_WRITER))
  385. return __mnt_want_write(file->f_path.mnt);
  386. else
  387. return mnt_clone_write(file->f_path.mnt);
  388. }
  389. /**
  390. * mnt_want_write_file - get write access to a file's mount
  391. * @file: the file who's mount on which to take a write
  392. *
  393. * This is like mnt_want_write, but it takes a file and can
  394. * do some optimisations if the file is open for write already
  395. */
  396. int mnt_want_write_file(struct file *file)
  397. {
  398. int ret;
  399. sb_start_write(file->f_path.mnt->mnt_sb);
  400. ret = __mnt_want_write_file(file);
  401. if (ret)
  402. sb_end_write(file->f_path.mnt->mnt_sb);
  403. return ret;
  404. }
  405. EXPORT_SYMBOL_GPL(mnt_want_write_file);
  406. /**
  407. * __mnt_drop_write - give up write access to a mount
  408. * @mnt: the mount on which to give up write access
  409. *
  410. * Tells the low-level filesystem that we are done
  411. * performing writes to it. Must be matched with
  412. * __mnt_want_write() call above.
  413. */
  414. void __mnt_drop_write(struct vfsmount *mnt)
  415. {
  416. preempt_disable();
  417. mnt_dec_writers(real_mount(mnt));
  418. preempt_enable();
  419. }
  420. /**
  421. * mnt_drop_write - give up write access to a mount
  422. * @mnt: the mount on which to give up write access
  423. *
  424. * Tells the low-level filesystem that we are done performing writes to it and
  425. * also allows filesystem to be frozen again. Must be matched with
  426. * mnt_want_write() call above.
  427. */
  428. void mnt_drop_write(struct vfsmount *mnt)
  429. {
  430. __mnt_drop_write(mnt);
  431. sb_end_write(mnt->mnt_sb);
  432. }
  433. EXPORT_SYMBOL_GPL(mnt_drop_write);
  434. void __mnt_drop_write_file(struct file *file)
  435. {
  436. __mnt_drop_write(file->f_path.mnt);
  437. }
  438. void mnt_drop_write_file(struct file *file)
  439. {
  440. mnt_drop_write(file->f_path.mnt);
  441. }
  442. EXPORT_SYMBOL(mnt_drop_write_file);
  443. static int mnt_make_readonly(struct mount *mnt)
  444. {
  445. int ret = 0;
  446. lock_mount_hash();
  447. mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
  448. /*
  449. * After storing MNT_WRITE_HOLD, we'll read the counters. This store
  450. * should be visible before we do.
  451. */
  452. smp_mb();
  453. /*
  454. * With writers on hold, if this value is zero, then there are
  455. * definitely no active writers (although held writers may subsequently
  456. * increment the count, they'll have to wait, and decrement it after
  457. * seeing MNT_READONLY).
  458. *
  459. * It is OK to have counter incremented on one CPU and decremented on
  460. * another: the sum will add up correctly. The danger would be when we
  461. * sum up each counter, if we read a counter before it is incremented,
  462. * but then read another CPU's count which it has been subsequently
  463. * decremented from -- we would see more decrements than we should.
  464. * MNT_WRITE_HOLD protects against this scenario, because
  465. * mnt_want_write first increments count, then smp_mb, then spins on
  466. * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
  467. * we're counting up here.
  468. */
  469. if (mnt_get_writers(mnt) > 0)
  470. ret = -EBUSY;
  471. else
  472. mnt->mnt.mnt_flags |= MNT_READONLY;
  473. /*
  474. * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
  475. * that become unheld will see MNT_READONLY.
  476. */
  477. smp_wmb();
  478. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  479. unlock_mount_hash();
  480. return ret;
  481. }
  482. static void __mnt_unmake_readonly(struct mount *mnt)
  483. {
  484. lock_mount_hash();
  485. mnt->mnt.mnt_flags &= ~MNT_READONLY;
  486. unlock_mount_hash();
  487. }
  488. int sb_prepare_remount_readonly(struct super_block *sb)
  489. {
  490. struct mount *mnt;
  491. int err = 0;
  492. /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
  493. if (atomic_long_read(&sb->s_remove_count))
  494. return -EBUSY;
  495. lock_mount_hash();
  496. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  497. if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
  498. mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
  499. smp_mb();
  500. if (mnt_get_writers(mnt) > 0) {
  501. err = -EBUSY;
  502. break;
  503. }
  504. }
  505. }
  506. if (!err && atomic_long_read(&sb->s_remove_count))
  507. err = -EBUSY;
  508. if (!err) {
  509. sb->s_readonly_remount = 1;
  510. smp_wmb();
  511. }
  512. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  513. if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
  514. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  515. }
  516. unlock_mount_hash();
  517. return err;
  518. }
  519. static void free_vfsmnt(struct mount *mnt)
  520. {
  521. kfree_const(mnt->mnt_devname);
  522. #ifdef CONFIG_SMP
  523. free_percpu(mnt->mnt_pcp);
  524. #endif
  525. kmem_cache_free(mnt_cache, mnt);
  526. }
  527. static void delayed_free_vfsmnt(struct rcu_head *head)
  528. {
  529. free_vfsmnt(container_of(head, struct mount, mnt_rcu));
  530. }
  531. /* call under rcu_read_lock */
  532. int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
  533. {
  534. struct mount *mnt;
  535. if (read_seqretry(&mount_lock, seq))
  536. return 1;
  537. if (bastard == NULL)
  538. return 0;
  539. mnt = real_mount(bastard);
  540. mnt_add_count(mnt, 1);
  541. if (likely(!read_seqretry(&mount_lock, seq)))
  542. return 0;
  543. if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
  544. mnt_add_count(mnt, -1);
  545. return 1;
  546. }
  547. return -1;
  548. }
  549. /* call under rcu_read_lock */
  550. bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
  551. {
  552. int res = __legitimize_mnt(bastard, seq);
  553. if (likely(!res))
  554. return true;
  555. if (unlikely(res < 0)) {
  556. rcu_read_unlock();
  557. mntput(bastard);
  558. rcu_read_lock();
  559. }
  560. return false;
  561. }
  562. /*
  563. * find the first mount at @dentry on vfsmount @mnt.
  564. * call under rcu_read_lock()
  565. */
  566. struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
  567. {
  568. struct hlist_head *head = m_hash(mnt, dentry);
  569. struct mount *p;
  570. hlist_for_each_entry_rcu(p, head, mnt_hash)
  571. if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
  572. return p;
  573. return NULL;
  574. }
  575. /*
  576. * find the last mount at @dentry on vfsmount @mnt.
  577. * mount_lock must be held.
  578. */
  579. struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry)
  580. {
  581. struct mount *p, *res = NULL;
  582. p = __lookup_mnt(mnt, dentry);
  583. if (!p)
  584. goto out;
  585. if (!(p->mnt.mnt_flags & MNT_UMOUNT))
  586. res = p;
  587. hlist_for_each_entry_continue(p, mnt_hash) {
  588. if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry)
  589. break;
  590. if (!(p->mnt.mnt_flags & MNT_UMOUNT))
  591. res = p;
  592. }
  593. out:
  594. return res;
  595. }
  596. /*
  597. * lookup_mnt - Return the first child mount mounted at path
  598. *
  599. * "First" means first mounted chronologically. If you create the
  600. * following mounts:
  601. *
  602. * mount /dev/sda1 /mnt
  603. * mount /dev/sda2 /mnt
  604. * mount /dev/sda3 /mnt
  605. *
  606. * Then lookup_mnt() on the base /mnt dentry in the root mount will
  607. * return successively the root dentry and vfsmount of /dev/sda1, then
  608. * /dev/sda2, then /dev/sda3, then NULL.
  609. *
  610. * lookup_mnt takes a reference to the found vfsmount.
  611. */
  612. struct vfsmount *lookup_mnt(const struct path *path)
  613. {
  614. struct mount *child_mnt;
  615. struct vfsmount *m;
  616. unsigned seq;
  617. rcu_read_lock();
  618. do {
  619. seq = read_seqbegin(&mount_lock);
  620. child_mnt = __lookup_mnt(path->mnt, path->dentry);
  621. m = child_mnt ? &child_mnt->mnt : NULL;
  622. } while (!legitimize_mnt(m, seq));
  623. rcu_read_unlock();
  624. return m;
  625. }
  626. /*
  627. * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
  628. * current mount namespace.
  629. *
  630. * The common case is dentries are not mountpoints at all and that
  631. * test is handled inline. For the slow case when we are actually
  632. * dealing with a mountpoint of some kind, walk through all of the
  633. * mounts in the current mount namespace and test to see if the dentry
  634. * is a mountpoint.
  635. *
  636. * The mount_hashtable is not usable in the context because we
  637. * need to identify all mounts that may be in the current mount
  638. * namespace not just a mount that happens to have some specified
  639. * parent mount.
  640. */
  641. bool __is_local_mountpoint(struct dentry *dentry)
  642. {
  643. struct mnt_namespace *ns = current->nsproxy->mnt_ns;
  644. struct mount *mnt;
  645. bool is_covered = false;
  646. if (!d_mountpoint(dentry))
  647. goto out;
  648. down_read(&namespace_sem);
  649. list_for_each_entry(mnt, &ns->list, mnt_list) {
  650. is_covered = (mnt->mnt_mountpoint == dentry);
  651. if (is_covered)
  652. break;
  653. }
  654. up_read(&namespace_sem);
  655. out:
  656. return is_covered;
  657. }
  658. static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
  659. {
  660. struct hlist_head *chain = mp_hash(dentry);
  661. struct mountpoint *mp;
  662. hlist_for_each_entry(mp, chain, m_hash) {
  663. if (mp->m_dentry == dentry) {
  664. /* might be worth a WARN_ON() */
  665. if (d_unlinked(dentry))
  666. return ERR_PTR(-ENOENT);
  667. mp->m_count++;
  668. return mp;
  669. }
  670. }
  671. return NULL;
  672. }
  673. static struct mountpoint *new_mountpoint(struct dentry *dentry)
  674. {
  675. struct hlist_head *chain = mp_hash(dentry);
  676. struct mountpoint *mp;
  677. int ret;
  678. mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
  679. if (!mp)
  680. return ERR_PTR(-ENOMEM);
  681. ret = d_set_mounted(dentry);
  682. if (ret) {
  683. kfree(mp);
  684. return ERR_PTR(ret);
  685. }
  686. mp->m_dentry = dentry;
  687. mp->m_count = 1;
  688. hlist_add_head(&mp->m_hash, chain);
  689. INIT_HLIST_HEAD(&mp->m_list);
  690. return mp;
  691. }
  692. static void put_mountpoint(struct mountpoint *mp)
  693. {
  694. if (!--mp->m_count) {
  695. struct dentry *dentry = mp->m_dentry;
  696. BUG_ON(!hlist_empty(&mp->m_list));
  697. spin_lock(&dentry->d_lock);
  698. dentry->d_flags &= ~DCACHE_MOUNTED;
  699. spin_unlock(&dentry->d_lock);
  700. hlist_del(&mp->m_hash);
  701. kfree(mp);
  702. }
  703. }
  704. static inline int check_mnt(struct mount *mnt)
  705. {
  706. return mnt->mnt_ns == current->nsproxy->mnt_ns;
  707. }
  708. /*
  709. * vfsmount lock must be held for write
  710. */
  711. static void touch_mnt_namespace(struct mnt_namespace *ns)
  712. {
  713. if (ns) {
  714. ns->event = ++event;
  715. wake_up_interruptible(&ns->poll);
  716. }
  717. }
  718. /*
  719. * vfsmount lock must be held for write
  720. */
  721. static void __touch_mnt_namespace(struct mnt_namespace *ns)
  722. {
  723. if (ns && ns->event != event) {
  724. ns->event = event;
  725. wake_up_interruptible(&ns->poll);
  726. }
  727. }
  728. /*
  729. * vfsmount lock must be held for write
  730. */
  731. static void unhash_mnt(struct mount *mnt)
  732. {
  733. mnt->mnt_parent = mnt;
  734. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  735. list_del_init(&mnt->mnt_child);
  736. hlist_del_init_rcu(&mnt->mnt_hash);
  737. hlist_del_init(&mnt->mnt_mp_list);
  738. put_mountpoint(mnt->mnt_mp);
  739. mnt->mnt_mp = NULL;
  740. }
  741. /*
  742. * vfsmount lock must be held for write
  743. */
  744. static void detach_mnt(struct mount *mnt, struct path *old_path)
  745. {
  746. old_path->dentry = mnt->mnt_mountpoint;
  747. old_path->mnt = &mnt->mnt_parent->mnt;
  748. unhash_mnt(mnt);
  749. }
  750. /*
  751. * vfsmount lock must be held for write
  752. */
  753. static void umount_mnt(struct mount *mnt)
  754. {
  755. /* old mountpoint will be dropped when we can do that */
  756. mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
  757. unhash_mnt(mnt);
  758. }
  759. /*
  760. * vfsmount lock must be held for write
  761. */
  762. void mnt_set_mountpoint(struct mount *mnt,
  763. struct mountpoint *mp,
  764. struct mount *child_mnt)
  765. {
  766. mp->m_count++;
  767. mnt_add_count(mnt, 1); /* essentially, that's mntget */
  768. child_mnt->mnt_mountpoint = dget(mp->m_dentry);
  769. child_mnt->mnt_parent = mnt;
  770. child_mnt->mnt_mp = mp;
  771. hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
  772. }
  773. /*
  774. * vfsmount lock must be held for write
  775. */
  776. static void attach_mnt(struct mount *mnt,
  777. struct mount *parent,
  778. struct mountpoint *mp)
  779. {
  780. mnt_set_mountpoint(parent, mp, mnt);
  781. hlist_add_head_rcu(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry));
  782. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  783. }
  784. static void attach_shadowed(struct mount *mnt,
  785. struct mount *parent,
  786. struct mount *shadows)
  787. {
  788. if (shadows) {
  789. hlist_add_behind_rcu(&mnt->mnt_hash, &shadows->mnt_hash);
  790. list_add(&mnt->mnt_child, &shadows->mnt_child);
  791. } else {
  792. hlist_add_head_rcu(&mnt->mnt_hash,
  793. m_hash(&parent->mnt, mnt->mnt_mountpoint));
  794. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  795. }
  796. }
  797. /*
  798. * vfsmount lock must be held for write
  799. */
  800. static void commit_tree(struct mount *mnt, struct mount *shadows)
  801. {
  802. struct mount *parent = mnt->mnt_parent;
  803. struct mount *m;
  804. LIST_HEAD(head);
  805. struct mnt_namespace *n = parent->mnt_ns;
  806. BUG_ON(parent == mnt);
  807. list_add_tail(&head, &mnt->mnt_list);
  808. list_for_each_entry(m, &head, mnt_list)
  809. m->mnt_ns = n;
  810. list_splice(&head, n->list.prev);
  811. n->mounts += n->pending_mounts;
  812. n->pending_mounts = 0;
  813. attach_shadowed(mnt, parent, shadows);
  814. touch_mnt_namespace(n);
  815. }
  816. static struct mount *next_mnt(struct mount *p, struct mount *root)
  817. {
  818. struct list_head *next = p->mnt_mounts.next;
  819. if (next == &p->mnt_mounts) {
  820. while (1) {
  821. if (p == root)
  822. return NULL;
  823. next = p->mnt_child.next;
  824. if (next != &p->mnt_parent->mnt_mounts)
  825. break;
  826. p = p->mnt_parent;
  827. }
  828. }
  829. return list_entry(next, struct mount, mnt_child);
  830. }
  831. static struct mount *skip_mnt_tree(struct mount *p)
  832. {
  833. struct list_head *prev = p->mnt_mounts.prev;
  834. while (prev != &p->mnt_mounts) {
  835. p = list_entry(prev, struct mount, mnt_child);
  836. prev = p->mnt_mounts.prev;
  837. }
  838. return p;
  839. }
  840. struct vfsmount *
  841. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  842. {
  843. struct mount *mnt;
  844. struct dentry *root;
  845. if (!type)
  846. return ERR_PTR(-ENODEV);
  847. mnt = alloc_vfsmnt(name);
  848. if (!mnt)
  849. return ERR_PTR(-ENOMEM);
  850. if (flags & MS_KERNMOUNT)
  851. mnt->mnt.mnt_flags = MNT_INTERNAL;
  852. root = mount_fs(type, flags, name, data);
  853. if (IS_ERR(root)) {
  854. mnt_free_id(mnt);
  855. free_vfsmnt(mnt);
  856. return ERR_CAST(root);
  857. }
  858. mnt->mnt.mnt_root = root;
  859. mnt->mnt.mnt_sb = root->d_sb;
  860. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  861. mnt->mnt_parent = mnt;
  862. lock_mount_hash();
  863. list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
  864. unlock_mount_hash();
  865. return &mnt->mnt;
  866. }
  867. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  868. static struct mount *clone_mnt(struct mount *old, struct dentry *root,
  869. int flag)
  870. {
  871. struct super_block *sb = old->mnt.mnt_sb;
  872. struct mount *mnt;
  873. int err;
  874. mnt = alloc_vfsmnt(old->mnt_devname);
  875. if (!mnt)
  876. return ERR_PTR(-ENOMEM);
  877. if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
  878. mnt->mnt_group_id = 0; /* not a peer of original */
  879. else
  880. mnt->mnt_group_id = old->mnt_group_id;
  881. if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
  882. err = mnt_alloc_group_id(mnt);
  883. if (err)
  884. goto out_free;
  885. }
  886. mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED);
  887. /* Don't allow unprivileged users to change mount flags */
  888. if (flag & CL_UNPRIVILEGED) {
  889. mnt->mnt.mnt_flags |= MNT_LOCK_ATIME;
  890. if (mnt->mnt.mnt_flags & MNT_READONLY)
  891. mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
  892. if (mnt->mnt.mnt_flags & MNT_NODEV)
  893. mnt->mnt.mnt_flags |= MNT_LOCK_NODEV;
  894. if (mnt->mnt.mnt_flags & MNT_NOSUID)
  895. mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID;
  896. if (mnt->mnt.mnt_flags & MNT_NOEXEC)
  897. mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC;
  898. }
  899. /* Don't allow unprivileged users to reveal what is under a mount */
  900. if ((flag & CL_UNPRIVILEGED) &&
  901. (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire)))
  902. mnt->mnt.mnt_flags |= MNT_LOCKED;
  903. atomic_inc(&sb->s_active);
  904. mnt->mnt.mnt_sb = sb;
  905. mnt->mnt.mnt_root = dget(root);
  906. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  907. mnt->mnt_parent = mnt;
  908. lock_mount_hash();
  909. list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
  910. unlock_mount_hash();
  911. if ((flag & CL_SLAVE) ||
  912. ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
  913. list_add(&mnt->mnt_slave, &old->mnt_slave_list);
  914. mnt->mnt_master = old;
  915. CLEAR_MNT_SHARED(mnt);
  916. } else if (!(flag & CL_PRIVATE)) {
  917. if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
  918. list_add(&mnt->mnt_share, &old->mnt_share);
  919. if (IS_MNT_SLAVE(old))
  920. list_add(&mnt->mnt_slave, &old->mnt_slave);
  921. mnt->mnt_master = old->mnt_master;
  922. }
  923. if (flag & CL_MAKE_SHARED)
  924. set_mnt_shared(mnt);
  925. /* stick the duplicate mount on the same expiry list
  926. * as the original if that was on one */
  927. if (flag & CL_EXPIRE) {
  928. if (!list_empty(&old->mnt_expire))
  929. list_add(&mnt->mnt_expire, &old->mnt_expire);
  930. }
  931. return mnt;
  932. out_free:
  933. mnt_free_id(mnt);
  934. free_vfsmnt(mnt);
  935. return ERR_PTR(err);
  936. }
  937. static void cleanup_mnt(struct mount *mnt)
  938. {
  939. /*
  940. * This probably indicates that somebody messed
  941. * up a mnt_want/drop_write() pair. If this
  942. * happens, the filesystem was probably unable
  943. * to make r/w->r/o transitions.
  944. */
  945. /*
  946. * The locking used to deal with mnt_count decrement provides barriers,
  947. * so mnt_get_writers() below is safe.
  948. */
  949. WARN_ON(mnt_get_writers(mnt));
  950. if (unlikely(mnt->mnt_pins.first))
  951. mnt_pin_kill(mnt);
  952. fsnotify_vfsmount_delete(&mnt->mnt);
  953. dput(mnt->mnt.mnt_root);
  954. deactivate_super(mnt->mnt.mnt_sb);
  955. mnt_free_id(mnt);
  956. call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
  957. }
  958. static void __cleanup_mnt(struct rcu_head *head)
  959. {
  960. cleanup_mnt(container_of(head, struct mount, mnt_rcu));
  961. }
  962. static LLIST_HEAD(delayed_mntput_list);
  963. static void delayed_mntput(struct work_struct *unused)
  964. {
  965. struct llist_node *node = llist_del_all(&delayed_mntput_list);
  966. struct llist_node *next;
  967. for (; node; node = next) {
  968. next = llist_next(node);
  969. cleanup_mnt(llist_entry(node, struct mount, mnt_llist));
  970. }
  971. }
  972. static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
  973. static void mntput_no_expire(struct mount *mnt)
  974. {
  975. rcu_read_lock();
  976. mnt_add_count(mnt, -1);
  977. if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */
  978. rcu_read_unlock();
  979. return;
  980. }
  981. lock_mount_hash();
  982. if (mnt_get_count(mnt)) {
  983. rcu_read_unlock();
  984. unlock_mount_hash();
  985. return;
  986. }
  987. if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
  988. rcu_read_unlock();
  989. unlock_mount_hash();
  990. return;
  991. }
  992. mnt->mnt.mnt_flags |= MNT_DOOMED;
  993. rcu_read_unlock();
  994. list_del(&mnt->mnt_instance);
  995. if (unlikely(!list_empty(&mnt->mnt_mounts))) {
  996. struct mount *p, *tmp;
  997. list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
  998. umount_mnt(p);
  999. }
  1000. }
  1001. unlock_mount_hash();
  1002. if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
  1003. struct task_struct *task = current;
  1004. if (likely(!(task->flags & PF_KTHREAD))) {
  1005. init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
  1006. if (!task_work_add(task, &mnt->mnt_rcu, true))
  1007. return;
  1008. }
  1009. if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
  1010. schedule_delayed_work(&delayed_mntput_work, 1);
  1011. return;
  1012. }
  1013. cleanup_mnt(mnt);
  1014. }
  1015. void mntput(struct vfsmount *mnt)
  1016. {
  1017. if (mnt) {
  1018. struct mount *m = real_mount(mnt);
  1019. /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
  1020. if (unlikely(m->mnt_expiry_mark))
  1021. m->mnt_expiry_mark = 0;
  1022. mntput_no_expire(m);
  1023. }
  1024. }
  1025. EXPORT_SYMBOL(mntput);
  1026. struct vfsmount *mntget(struct vfsmount *mnt)
  1027. {
  1028. if (mnt)
  1029. mnt_add_count(real_mount(mnt), 1);
  1030. return mnt;
  1031. }
  1032. EXPORT_SYMBOL(mntget);
  1033. /* path_is_mountpoint() - Check if path is a mount in the current
  1034. * namespace.
  1035. *
  1036. * d_mountpoint() can only be used reliably to establish if a dentry is
  1037. * not mounted in any namespace and that common case is handled inline.
  1038. * d_mountpoint() isn't aware of the possibility there may be multiple
  1039. * mounts using a given dentry in a different namespace. This function
  1040. * checks if the passed in path is a mountpoint rather than the dentry
  1041. * alone.
  1042. */
  1043. bool path_is_mountpoint(const struct path *path)
  1044. {
  1045. unsigned seq;
  1046. bool res;
  1047. if (!d_mountpoint(path->dentry))
  1048. return false;
  1049. rcu_read_lock();
  1050. do {
  1051. seq = read_seqbegin(&mount_lock);
  1052. res = __path_is_mountpoint(path);
  1053. } while (read_seqretry(&mount_lock, seq));
  1054. rcu_read_unlock();
  1055. return res;
  1056. }
  1057. EXPORT_SYMBOL(path_is_mountpoint);
  1058. struct vfsmount *mnt_clone_internal(const struct path *path)
  1059. {
  1060. struct mount *p;
  1061. p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
  1062. if (IS_ERR(p))
  1063. return ERR_CAST(p);
  1064. p->mnt.mnt_flags |= MNT_INTERNAL;
  1065. return &p->mnt;
  1066. }
  1067. static inline void mangle(struct seq_file *m, const char *s)
  1068. {
  1069. seq_escape(m, s, " \t\n\\");
  1070. }
  1071. /*
  1072. * Simple .show_options callback for filesystems which don't want to
  1073. * implement more complex mount option showing.
  1074. *
  1075. * See also save_mount_options().
  1076. */
  1077. int generic_show_options(struct seq_file *m, struct dentry *root)
  1078. {
  1079. const char *options;
  1080. rcu_read_lock();
  1081. options = rcu_dereference(root->d_sb->s_options);
  1082. if (options != NULL && options[0]) {
  1083. seq_putc(m, ',');
  1084. mangle(m, options);
  1085. }
  1086. rcu_read_unlock();
  1087. return 0;
  1088. }
  1089. EXPORT_SYMBOL(generic_show_options);
  1090. /*
  1091. * If filesystem uses generic_show_options(), this function should be
  1092. * called from the fill_super() callback.
  1093. *
  1094. * The .remount_fs callback usually needs to be handled in a special
  1095. * way, to make sure, that previous options are not overwritten if the
  1096. * remount fails.
  1097. *
  1098. * Also note, that if the filesystem's .remount_fs function doesn't
  1099. * reset all options to their default value, but changes only newly
  1100. * given options, then the displayed options will not reflect reality
  1101. * any more.
  1102. */
  1103. void save_mount_options(struct super_block *sb, char *options)
  1104. {
  1105. BUG_ON(sb->s_options);
  1106. rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
  1107. }
  1108. EXPORT_SYMBOL(save_mount_options);
  1109. void replace_mount_options(struct super_block *sb, char *options)
  1110. {
  1111. char *old = sb->s_options;
  1112. rcu_assign_pointer(sb->s_options, options);
  1113. if (old) {
  1114. synchronize_rcu();
  1115. kfree(old);
  1116. }
  1117. }
  1118. EXPORT_SYMBOL(replace_mount_options);
  1119. #ifdef CONFIG_PROC_FS
  1120. /* iterator; we want it to have access to namespace_sem, thus here... */
  1121. static void *m_start(struct seq_file *m, loff_t *pos)
  1122. {
  1123. struct proc_mounts *p = m->private;
  1124. down_read(&namespace_sem);
  1125. if (p->cached_event == p->ns->event) {
  1126. void *v = p->cached_mount;
  1127. if (*pos == p->cached_index)
  1128. return v;
  1129. if (*pos == p->cached_index + 1) {
  1130. v = seq_list_next(v, &p->ns->list, &p->cached_index);
  1131. return p->cached_mount = v;
  1132. }
  1133. }
  1134. p->cached_event = p->ns->event;
  1135. p->cached_mount = seq_list_start(&p->ns->list, *pos);
  1136. p->cached_index = *pos;
  1137. return p->cached_mount;
  1138. }
  1139. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  1140. {
  1141. struct proc_mounts *p = m->private;
  1142. p->cached_mount = seq_list_next(v, &p->ns->list, pos);
  1143. p->cached_index = *pos;
  1144. return p->cached_mount;
  1145. }
  1146. static void m_stop(struct seq_file *m, void *v)
  1147. {
  1148. up_read(&namespace_sem);
  1149. }
  1150. static int m_show(struct seq_file *m, void *v)
  1151. {
  1152. struct proc_mounts *p = m->private;
  1153. struct mount *r = list_entry(v, struct mount, mnt_list);
  1154. return p->show(m, &r->mnt);
  1155. }
  1156. const struct seq_operations mounts_op = {
  1157. .start = m_start,
  1158. .next = m_next,
  1159. .stop = m_stop,
  1160. .show = m_show,
  1161. };
  1162. #endif /* CONFIG_PROC_FS */
  1163. /**
  1164. * may_umount_tree - check if a mount tree is busy
  1165. * @mnt: root of mount tree
  1166. *
  1167. * This is called to check if a tree of mounts has any
  1168. * open files, pwds, chroots or sub mounts that are
  1169. * busy.
  1170. */
  1171. int may_umount_tree(struct vfsmount *m)
  1172. {
  1173. struct mount *mnt = real_mount(m);
  1174. int actual_refs = 0;
  1175. int minimum_refs = 0;
  1176. struct mount *p;
  1177. BUG_ON(!m);
  1178. /* write lock needed for mnt_get_count */
  1179. lock_mount_hash();
  1180. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1181. actual_refs += mnt_get_count(p);
  1182. minimum_refs += 2;
  1183. }
  1184. unlock_mount_hash();
  1185. if (actual_refs > minimum_refs)
  1186. return 0;
  1187. return 1;
  1188. }
  1189. EXPORT_SYMBOL(may_umount_tree);
  1190. /**
  1191. * may_umount - check if a mount point is busy
  1192. * @mnt: root of mount
  1193. *
  1194. * This is called to check if a mount point has any
  1195. * open files, pwds, chroots or sub mounts. If the
  1196. * mount has sub mounts this will return busy
  1197. * regardless of whether the sub mounts are busy.
  1198. *
  1199. * Doesn't take quota and stuff into account. IOW, in some cases it will
  1200. * give false negatives. The main reason why it's here is that we need
  1201. * a non-destructive way to look for easily umountable filesystems.
  1202. */
  1203. int may_umount(struct vfsmount *mnt)
  1204. {
  1205. int ret = 1;
  1206. down_read(&namespace_sem);
  1207. lock_mount_hash();
  1208. if (propagate_mount_busy(real_mount(mnt), 2))
  1209. ret = 0;
  1210. unlock_mount_hash();
  1211. up_read(&namespace_sem);
  1212. return ret;
  1213. }
  1214. EXPORT_SYMBOL(may_umount);
  1215. static HLIST_HEAD(unmounted); /* protected by namespace_sem */
  1216. static void namespace_unlock(void)
  1217. {
  1218. struct hlist_head head;
  1219. hlist_move_list(&unmounted, &head);
  1220. up_write(&namespace_sem);
  1221. if (likely(hlist_empty(&head)))
  1222. return;
  1223. synchronize_rcu();
  1224. group_pin_kill(&head);
  1225. }
  1226. static inline void namespace_lock(void)
  1227. {
  1228. down_write(&namespace_sem);
  1229. }
  1230. enum umount_tree_flags {
  1231. UMOUNT_SYNC = 1,
  1232. UMOUNT_PROPAGATE = 2,
  1233. UMOUNT_CONNECTED = 4,
  1234. };
  1235. static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
  1236. {
  1237. /* Leaving mounts connected is only valid for lazy umounts */
  1238. if (how & UMOUNT_SYNC)
  1239. return true;
  1240. /* A mount without a parent has nothing to be connected to */
  1241. if (!mnt_has_parent(mnt))
  1242. return true;
  1243. /* Because the reference counting rules change when mounts are
  1244. * unmounted and connected, umounted mounts may not be
  1245. * connected to mounted mounts.
  1246. */
  1247. if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
  1248. return true;
  1249. /* Has it been requested that the mount remain connected? */
  1250. if (how & UMOUNT_CONNECTED)
  1251. return false;
  1252. /* Is the mount locked such that it needs to remain connected? */
  1253. if (IS_MNT_LOCKED(mnt))
  1254. return false;
  1255. /* By default disconnect the mount */
  1256. return true;
  1257. }
  1258. /*
  1259. * mount_lock must be held
  1260. * namespace_sem must be held for write
  1261. */
  1262. static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
  1263. {
  1264. LIST_HEAD(tmp_list);
  1265. struct mount *p;
  1266. if (how & UMOUNT_PROPAGATE)
  1267. propagate_mount_unlock(mnt);
  1268. /* Gather the mounts to umount */
  1269. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1270. p->mnt.mnt_flags |= MNT_UMOUNT;
  1271. list_move(&p->mnt_list, &tmp_list);
  1272. }
  1273. /* Hide the mounts from mnt_mounts */
  1274. list_for_each_entry(p, &tmp_list, mnt_list) {
  1275. list_del_init(&p->mnt_child);
  1276. }
  1277. /* Add propogated mounts to the tmp_list */
  1278. if (how & UMOUNT_PROPAGATE)
  1279. propagate_umount(&tmp_list);
  1280. while (!list_empty(&tmp_list)) {
  1281. struct mnt_namespace *ns;
  1282. bool disconnect;
  1283. p = list_first_entry(&tmp_list, struct mount, mnt_list);
  1284. list_del_init(&p->mnt_expire);
  1285. list_del_init(&p->mnt_list);
  1286. ns = p->mnt_ns;
  1287. if (ns) {
  1288. ns->mounts--;
  1289. __touch_mnt_namespace(ns);
  1290. }
  1291. p->mnt_ns = NULL;
  1292. if (how & UMOUNT_SYNC)
  1293. p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
  1294. disconnect = disconnect_mount(p, how);
  1295. pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt,
  1296. disconnect ? &unmounted : NULL);
  1297. if (mnt_has_parent(p)) {
  1298. mnt_add_count(p->mnt_parent, -1);
  1299. if (!disconnect) {
  1300. /* Don't forget about p */
  1301. list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
  1302. } else {
  1303. umount_mnt(p);
  1304. }
  1305. }
  1306. change_mnt_propagation(p, MS_PRIVATE);
  1307. }
  1308. }
  1309. static void shrink_submounts(struct mount *mnt);
  1310. static int do_umount(struct mount *mnt, int flags)
  1311. {
  1312. struct super_block *sb = mnt->mnt.mnt_sb;
  1313. int retval;
  1314. retval = security_sb_umount(&mnt->mnt, flags);
  1315. if (retval)
  1316. return retval;
  1317. /*
  1318. * Allow userspace to request a mountpoint be expired rather than
  1319. * unmounting unconditionally. Unmount only happens if:
  1320. * (1) the mark is already set (the mark is cleared by mntput())
  1321. * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
  1322. */
  1323. if (flags & MNT_EXPIRE) {
  1324. if (&mnt->mnt == current->fs->root.mnt ||
  1325. flags & (MNT_FORCE | MNT_DETACH))
  1326. return -EINVAL;
  1327. /*
  1328. * probably don't strictly need the lock here if we examined
  1329. * all race cases, but it's a slowpath.
  1330. */
  1331. lock_mount_hash();
  1332. if (mnt_get_count(mnt) != 2) {
  1333. unlock_mount_hash();
  1334. return -EBUSY;
  1335. }
  1336. unlock_mount_hash();
  1337. if (!xchg(&mnt->mnt_expiry_mark, 1))
  1338. return -EAGAIN;
  1339. }
  1340. /*
  1341. * If we may have to abort operations to get out of this
  1342. * mount, and they will themselves hold resources we must
  1343. * allow the fs to do things. In the Unix tradition of
  1344. * 'Gee thats tricky lets do it in userspace' the umount_begin
  1345. * might fail to complete on the first run through as other tasks
  1346. * must return, and the like. Thats for the mount program to worry
  1347. * about for the moment.
  1348. */
  1349. if (flags & MNT_FORCE && sb->s_op->umount_begin) {
  1350. sb->s_op->umount_begin(sb);
  1351. }
  1352. /*
  1353. * No sense to grab the lock for this test, but test itself looks
  1354. * somewhat bogus. Suggestions for better replacement?
  1355. * Ho-hum... In principle, we might treat that as umount + switch
  1356. * to rootfs. GC would eventually take care of the old vfsmount.
  1357. * Actually it makes sense, especially if rootfs would contain a
  1358. * /reboot - static binary that would close all descriptors and
  1359. * call reboot(9). Then init(8) could umount root and exec /reboot.
  1360. */
  1361. if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
  1362. /*
  1363. * Special case for "unmounting" root ...
  1364. * we just try to remount it readonly.
  1365. */
  1366. if (!capable(CAP_SYS_ADMIN))
  1367. return -EPERM;
  1368. down_write(&sb->s_umount);
  1369. if (!(sb->s_flags & MS_RDONLY))
  1370. retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
  1371. up_write(&sb->s_umount);
  1372. return retval;
  1373. }
  1374. namespace_lock();
  1375. lock_mount_hash();
  1376. event++;
  1377. if (flags & MNT_DETACH) {
  1378. if (!list_empty(&mnt->mnt_list))
  1379. umount_tree(mnt, UMOUNT_PROPAGATE);
  1380. retval = 0;
  1381. } else {
  1382. shrink_submounts(mnt);
  1383. retval = -EBUSY;
  1384. if (!propagate_mount_busy(mnt, 2)) {
  1385. if (!list_empty(&mnt->mnt_list))
  1386. umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
  1387. retval = 0;
  1388. }
  1389. }
  1390. unlock_mount_hash();
  1391. namespace_unlock();
  1392. return retval;
  1393. }
  1394. /*
  1395. * __detach_mounts - lazily unmount all mounts on the specified dentry
  1396. *
  1397. * During unlink, rmdir, and d_drop it is possible to loose the path
  1398. * to an existing mountpoint, and wind up leaking the mount.
  1399. * detach_mounts allows lazily unmounting those mounts instead of
  1400. * leaking them.
  1401. *
  1402. * The caller may hold dentry->d_inode->i_mutex.
  1403. */
  1404. void __detach_mounts(struct dentry *dentry)
  1405. {
  1406. struct mountpoint *mp;
  1407. struct mount *mnt;
  1408. namespace_lock();
  1409. mp = lookup_mountpoint(dentry);
  1410. if (IS_ERR_OR_NULL(mp))
  1411. goto out_unlock;
  1412. lock_mount_hash();
  1413. event++;
  1414. while (!hlist_empty(&mp->m_list)) {
  1415. mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
  1416. if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
  1417. hlist_add_head(&mnt->mnt_umount.s_list, &unmounted);
  1418. umount_mnt(mnt);
  1419. }
  1420. else umount_tree(mnt, UMOUNT_CONNECTED);
  1421. }
  1422. unlock_mount_hash();
  1423. put_mountpoint(mp);
  1424. out_unlock:
  1425. namespace_unlock();
  1426. }
  1427. /*
  1428. * Is the caller allowed to modify his namespace?
  1429. */
  1430. static inline bool may_mount(void)
  1431. {
  1432. return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
  1433. }
  1434. static inline bool may_mandlock(void)
  1435. {
  1436. #ifndef CONFIG_MANDATORY_FILE_LOCKING
  1437. return false;
  1438. #endif
  1439. return capable(CAP_SYS_ADMIN);
  1440. }
  1441. /*
  1442. * Now umount can handle mount points as well as block devices.
  1443. * This is important for filesystems which use unnamed block devices.
  1444. *
  1445. * We now support a flag for forced unmount like the other 'big iron'
  1446. * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
  1447. */
  1448. SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
  1449. {
  1450. struct path path;
  1451. struct mount *mnt;
  1452. int retval;
  1453. int lookup_flags = 0;
  1454. if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
  1455. return -EINVAL;
  1456. if (!may_mount())
  1457. return -EPERM;
  1458. if (!(flags & UMOUNT_NOFOLLOW))
  1459. lookup_flags |= LOOKUP_FOLLOW;
  1460. retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
  1461. if (retval)
  1462. goto out;
  1463. mnt = real_mount(path.mnt);
  1464. retval = -EINVAL;
  1465. if (path.dentry != path.mnt->mnt_root)
  1466. goto dput_and_out;
  1467. if (!check_mnt(mnt))
  1468. goto dput_and_out;
  1469. if (mnt->mnt.mnt_flags & MNT_LOCKED)
  1470. goto dput_and_out;
  1471. retval = -EPERM;
  1472. if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
  1473. goto dput_and_out;
  1474. retval = do_umount(mnt, flags);
  1475. dput_and_out:
  1476. /* we mustn't call path_put() as that would clear mnt_expiry_mark */
  1477. dput(path.dentry);
  1478. mntput_no_expire(mnt);
  1479. out:
  1480. return retval;
  1481. }
  1482. #ifdef __ARCH_WANT_SYS_OLDUMOUNT
  1483. /*
  1484. * The 2.0 compatible umount. No flags.
  1485. */
  1486. SYSCALL_DEFINE1(oldumount, char __user *, name)
  1487. {
  1488. return sys_umount(name, 0);
  1489. }
  1490. #endif
  1491. static bool is_mnt_ns_file(struct dentry *dentry)
  1492. {
  1493. /* Is this a proxy for a mount namespace? */
  1494. return dentry->d_op == &ns_dentry_operations &&
  1495. dentry->d_fsdata == &mntns_operations;
  1496. }
  1497. struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
  1498. {
  1499. return container_of(ns, struct mnt_namespace, ns);
  1500. }
  1501. static bool mnt_ns_loop(struct dentry *dentry)
  1502. {
  1503. /* Could bind mounting the mount namespace inode cause a
  1504. * mount namespace loop?
  1505. */
  1506. struct mnt_namespace *mnt_ns;
  1507. if (!is_mnt_ns_file(dentry))
  1508. return false;
  1509. mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
  1510. return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
  1511. }
  1512. struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
  1513. int flag)
  1514. {
  1515. struct mount *res, *p, *q, *r, *parent;
  1516. if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
  1517. return ERR_PTR(-EINVAL);
  1518. if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
  1519. return ERR_PTR(-EINVAL);
  1520. res = q = clone_mnt(mnt, dentry, flag);
  1521. if (IS_ERR(q))
  1522. return q;
  1523. q->mnt_mountpoint = mnt->mnt_mountpoint;
  1524. p = mnt;
  1525. list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
  1526. struct mount *s;
  1527. if (!is_subdir(r->mnt_mountpoint, dentry))
  1528. continue;
  1529. for (s = r; s; s = next_mnt(s, r)) {
  1530. struct mount *t = NULL;
  1531. if (!(flag & CL_COPY_UNBINDABLE) &&
  1532. IS_MNT_UNBINDABLE(s)) {
  1533. s = skip_mnt_tree(s);
  1534. continue;
  1535. }
  1536. if (!(flag & CL_COPY_MNT_NS_FILE) &&
  1537. is_mnt_ns_file(s->mnt.mnt_root)) {
  1538. s = skip_mnt_tree(s);
  1539. continue;
  1540. }
  1541. while (p != s->mnt_parent) {
  1542. p = p->mnt_parent;
  1543. q = q->mnt_parent;
  1544. }
  1545. p = s;
  1546. parent = q;
  1547. q = clone_mnt(p, p->mnt.mnt_root, flag);
  1548. if (IS_ERR(q))
  1549. goto out;
  1550. lock_mount_hash();
  1551. list_add_tail(&q->mnt_list, &res->mnt_list);
  1552. mnt_set_mountpoint(parent, p->mnt_mp, q);
  1553. if (!list_empty(&parent->mnt_mounts)) {
  1554. t = list_last_entry(&parent->mnt_mounts,
  1555. struct mount, mnt_child);
  1556. if (t->mnt_mp != p->mnt_mp)
  1557. t = NULL;
  1558. }
  1559. attach_shadowed(q, parent, t);
  1560. unlock_mount_hash();
  1561. }
  1562. }
  1563. return res;
  1564. out:
  1565. if (res) {
  1566. lock_mount_hash();
  1567. umount_tree(res, UMOUNT_SYNC);
  1568. unlock_mount_hash();
  1569. }
  1570. return q;
  1571. }
  1572. /* Caller should check returned pointer for errors */
  1573. struct vfsmount *collect_mounts(const struct path *path)
  1574. {
  1575. struct mount *tree;
  1576. namespace_lock();
  1577. if (!check_mnt(real_mount(path->mnt)))
  1578. tree = ERR_PTR(-EINVAL);
  1579. else
  1580. tree = copy_tree(real_mount(path->mnt), path->dentry,
  1581. CL_COPY_ALL | CL_PRIVATE);
  1582. namespace_unlock();
  1583. if (IS_ERR(tree))
  1584. return ERR_CAST(tree);
  1585. return &tree->mnt;
  1586. }
  1587. void drop_collected_mounts(struct vfsmount *mnt)
  1588. {
  1589. namespace_lock();
  1590. lock_mount_hash();
  1591. umount_tree(real_mount(mnt), UMOUNT_SYNC);
  1592. unlock_mount_hash();
  1593. namespace_unlock();
  1594. }
  1595. /**
  1596. * clone_private_mount - create a private clone of a path
  1597. *
  1598. * This creates a new vfsmount, which will be the clone of @path. The new will
  1599. * not be attached anywhere in the namespace and will be private (i.e. changes
  1600. * to the originating mount won't be propagated into this).
  1601. *
  1602. * Release with mntput().
  1603. */
  1604. struct vfsmount *clone_private_mount(const struct path *path)
  1605. {
  1606. struct mount *old_mnt = real_mount(path->mnt);
  1607. struct mount *new_mnt;
  1608. if (IS_MNT_UNBINDABLE(old_mnt))
  1609. return ERR_PTR(-EINVAL);
  1610. down_read(&namespace_sem);
  1611. new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
  1612. up_read(&namespace_sem);
  1613. if (IS_ERR(new_mnt))
  1614. return ERR_CAST(new_mnt);
  1615. return &new_mnt->mnt;
  1616. }
  1617. EXPORT_SYMBOL_GPL(clone_private_mount);
  1618. int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
  1619. struct vfsmount *root)
  1620. {
  1621. struct mount *mnt;
  1622. int res = f(root, arg);
  1623. if (res)
  1624. return res;
  1625. list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
  1626. res = f(&mnt->mnt, arg);
  1627. if (res)
  1628. return res;
  1629. }
  1630. return 0;
  1631. }
  1632. static void cleanup_group_ids(struct mount *mnt, struct mount *end)
  1633. {
  1634. struct mount *p;
  1635. for (p = mnt; p != end; p = next_mnt(p, mnt)) {
  1636. if (p->mnt_group_id && !IS_MNT_SHARED(p))
  1637. mnt_release_group_id(p);
  1638. }
  1639. }
  1640. static int invent_group_ids(struct mount *mnt, bool recurse)
  1641. {
  1642. struct mount *p;
  1643. for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
  1644. if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
  1645. int err = mnt_alloc_group_id(p);
  1646. if (err) {
  1647. cleanup_group_ids(mnt, p);
  1648. return err;
  1649. }
  1650. }
  1651. }
  1652. return 0;
  1653. }
  1654. int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
  1655. {
  1656. unsigned int max = READ_ONCE(sysctl_mount_max);
  1657. unsigned int mounts = 0, old, pending, sum;
  1658. struct mount *p;
  1659. for (p = mnt; p; p = next_mnt(p, mnt))
  1660. mounts++;
  1661. old = ns->mounts;
  1662. pending = ns->pending_mounts;
  1663. sum = old + pending;
  1664. if ((old > sum) ||
  1665. (pending > sum) ||
  1666. (max < sum) ||
  1667. (mounts > (max - sum)))
  1668. return -ENOSPC;
  1669. ns->pending_mounts = pending + mounts;
  1670. return 0;
  1671. }
  1672. /*
  1673. * @source_mnt : mount tree to be attached
  1674. * @nd : place the mount tree @source_mnt is attached
  1675. * @parent_nd : if non-null, detach the source_mnt from its parent and
  1676. * store the parent mount and mountpoint dentry.
  1677. * (done when source_mnt is moved)
  1678. *
  1679. * NOTE: in the table below explains the semantics when a source mount
  1680. * of a given type is attached to a destination mount of a given type.
  1681. * ---------------------------------------------------------------------------
  1682. * | BIND MOUNT OPERATION |
  1683. * |**************************************************************************
  1684. * | source-->| shared | private | slave | unbindable |
  1685. * | dest | | | | |
  1686. * | | | | | | |
  1687. * | v | | | | |
  1688. * |**************************************************************************
  1689. * | shared | shared (++) | shared (+) | shared(+++)| invalid |
  1690. * | | | | | |
  1691. * |non-shared| shared (+) | private | slave (*) | invalid |
  1692. * ***************************************************************************
  1693. * A bind operation clones the source mount and mounts the clone on the
  1694. * destination mount.
  1695. *
  1696. * (++) the cloned mount is propagated to all the mounts in the propagation
  1697. * tree of the destination mount and the cloned mount is added to
  1698. * the peer group of the source mount.
  1699. * (+) the cloned mount is created under the destination mount and is marked
  1700. * as shared. The cloned mount is added to the peer group of the source
  1701. * mount.
  1702. * (+++) the mount is propagated to all the mounts in the propagation tree
  1703. * of the destination mount and the cloned mount is made slave
  1704. * of the same master as that of the source mount. The cloned mount
  1705. * is marked as 'shared and slave'.
  1706. * (*) the cloned mount is made a slave of the same master as that of the
  1707. * source mount.
  1708. *
  1709. * ---------------------------------------------------------------------------
  1710. * | MOVE MOUNT OPERATION |
  1711. * |**************************************************************************
  1712. * | source-->| shared | private | slave | unbindable |
  1713. * | dest | | | | |
  1714. * | | | | | | |
  1715. * | v | | | | |
  1716. * |**************************************************************************
  1717. * | shared | shared (+) | shared (+) | shared(+++) | invalid |
  1718. * | | | | | |
  1719. * |non-shared| shared (+*) | private | slave (*) | unbindable |
  1720. * ***************************************************************************
  1721. *
  1722. * (+) the mount is moved to the destination. And is then propagated to
  1723. * all the mounts in the propagation tree of the destination mount.
  1724. * (+*) the mount is moved to the destination.
  1725. * (+++) the mount is moved to the destination and is then propagated to
  1726. * all the mounts belonging to the destination mount's propagation tree.
  1727. * the mount is marked as 'shared and slave'.
  1728. * (*) the mount continues to be a slave at the new location.
  1729. *
  1730. * if the source mount is a tree, the operations explained above is
  1731. * applied to each mount in the tree.
  1732. * Must be called without spinlocks held, since this function can sleep
  1733. * in allocations.
  1734. */
  1735. static int attach_recursive_mnt(struct mount *source_mnt,
  1736. struct mount *dest_mnt,
  1737. struct mountpoint *dest_mp,
  1738. struct path *parent_path)
  1739. {
  1740. HLIST_HEAD(tree_list);
  1741. struct mnt_namespace *ns = dest_mnt->mnt_ns;
  1742. struct mount *child, *p;
  1743. struct hlist_node *n;
  1744. int err;
  1745. /* Is there space to add these mounts to the mount namespace? */
  1746. if (!parent_path) {
  1747. err = count_mounts(ns, source_mnt);
  1748. if (err)
  1749. goto out;
  1750. }
  1751. if (IS_MNT_SHARED(dest_mnt)) {
  1752. err = invent_group_ids(source_mnt, true);
  1753. if (err)
  1754. goto out;
  1755. err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
  1756. lock_mount_hash();
  1757. if (err)
  1758. goto out_cleanup_ids;
  1759. for (p = source_mnt; p; p = next_mnt(p, source_mnt))
  1760. set_mnt_shared(p);
  1761. } else {
  1762. lock_mount_hash();
  1763. }
  1764. if (parent_path) {
  1765. detach_mnt(source_mnt, parent_path);
  1766. attach_mnt(source_mnt, dest_mnt, dest_mp);
  1767. touch_mnt_namespace(source_mnt->mnt_ns);
  1768. } else {
  1769. mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
  1770. commit_tree(source_mnt, NULL);
  1771. }
  1772. hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
  1773. struct mount *q;
  1774. hlist_del_init(&child->mnt_hash);
  1775. q = __lookup_mnt_last(&child->mnt_parent->mnt,
  1776. child->mnt_mountpoint);
  1777. commit_tree(child, q);
  1778. }
  1779. unlock_mount_hash();
  1780. return 0;
  1781. out_cleanup_ids:
  1782. while (!hlist_empty(&tree_list)) {
  1783. child = hlist_entry(tree_list.first, struct mount, mnt_hash);
  1784. child->mnt_parent->mnt_ns->pending_mounts = 0;
  1785. umount_tree(child, UMOUNT_SYNC);
  1786. }
  1787. unlock_mount_hash();
  1788. cleanup_group_ids(source_mnt, NULL);
  1789. out:
  1790. ns->pending_mounts = 0;
  1791. return err;
  1792. }
  1793. static struct mountpoint *lock_mount(struct path *path)
  1794. {
  1795. struct vfsmount *mnt;
  1796. struct dentry *dentry = path->dentry;
  1797. retry:
  1798. inode_lock(dentry->d_inode);
  1799. if (unlikely(cant_mount(dentry))) {
  1800. inode_unlock(dentry->d_inode);
  1801. return ERR_PTR(-ENOENT);
  1802. }
  1803. namespace_lock();
  1804. mnt = lookup_mnt(path);
  1805. if (likely(!mnt)) {
  1806. struct mountpoint *mp = lookup_mountpoint(dentry);
  1807. if (!mp)
  1808. mp = new_mountpoint(dentry);
  1809. if (IS_ERR(mp)) {
  1810. namespace_unlock();
  1811. inode_unlock(dentry->d_inode);
  1812. return mp;
  1813. }
  1814. return mp;
  1815. }
  1816. namespace_unlock();
  1817. inode_unlock(path->dentry->d_inode);
  1818. path_put(path);
  1819. path->mnt = mnt;
  1820. dentry = path->dentry = dget(mnt->mnt_root);
  1821. goto retry;
  1822. }
  1823. static void unlock_mount(struct mountpoint *where)
  1824. {
  1825. struct dentry *dentry = where->m_dentry;
  1826. put_mountpoint(where);
  1827. namespace_unlock();
  1828. inode_unlock(dentry->d_inode);
  1829. }
  1830. static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
  1831. {
  1832. if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
  1833. return -EINVAL;
  1834. if (d_is_dir(mp->m_dentry) !=
  1835. d_is_dir(mnt->mnt.mnt_root))
  1836. return -ENOTDIR;
  1837. return attach_recursive_mnt(mnt, p, mp, NULL);
  1838. }
  1839. /*
  1840. * Sanity check the flags to change_mnt_propagation.
  1841. */
  1842. static int flags_to_propagation_type(int flags)
  1843. {
  1844. int type = flags & ~(MS_REC | MS_SILENT);
  1845. /* Fail if any non-propagation flags are set */
  1846. if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1847. return 0;
  1848. /* Only one propagation flag should be set */
  1849. if (!is_power_of_2(type))
  1850. return 0;
  1851. return type;
  1852. }
  1853. /*
  1854. * recursively change the type of the mountpoint.
  1855. */
  1856. static int do_change_type(struct path *path, int flag)
  1857. {
  1858. struct mount *m;
  1859. struct mount *mnt = real_mount(path->mnt);
  1860. int recurse = flag & MS_REC;
  1861. int type;
  1862. int err = 0;
  1863. if (path->dentry != path->mnt->mnt_root)
  1864. return -EINVAL;
  1865. type = flags_to_propagation_type(flag);
  1866. if (!type)
  1867. return -EINVAL;
  1868. namespace_lock();
  1869. if (type == MS_SHARED) {
  1870. err = invent_group_ids(mnt, recurse);
  1871. if (err)
  1872. goto out_unlock;
  1873. }
  1874. lock_mount_hash();
  1875. for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
  1876. change_mnt_propagation(m, type);
  1877. unlock_mount_hash();
  1878. out_unlock:
  1879. namespace_unlock();
  1880. return err;
  1881. }
  1882. static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
  1883. {
  1884. struct mount *child;
  1885. list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
  1886. if (!is_subdir(child->mnt_mountpoint, dentry))
  1887. continue;
  1888. if (child->mnt.mnt_flags & MNT_LOCKED)
  1889. return true;
  1890. }
  1891. return false;
  1892. }
  1893. /*
  1894. * do loopback mount.
  1895. */
  1896. static int do_loopback(struct path *path, const char *old_name,
  1897. int recurse)
  1898. {
  1899. struct path old_path;
  1900. struct mount *mnt = NULL, *old, *parent;
  1901. struct mountpoint *mp;
  1902. int err;
  1903. if (!old_name || !*old_name)
  1904. return -EINVAL;
  1905. err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
  1906. if (err)
  1907. return err;
  1908. err = -EINVAL;
  1909. if (mnt_ns_loop(old_path.dentry))
  1910. goto out;
  1911. mp = lock_mount(path);
  1912. err = PTR_ERR(mp);
  1913. if (IS_ERR(mp))
  1914. goto out;
  1915. old = real_mount(old_path.mnt);
  1916. parent = real_mount(path->mnt);
  1917. err = -EINVAL;
  1918. if (IS_MNT_UNBINDABLE(old))
  1919. goto out2;
  1920. if (!check_mnt(parent))
  1921. goto out2;
  1922. if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations)
  1923. goto out2;
  1924. if (!recurse && has_locked_children(old, old_path.dentry))
  1925. goto out2;
  1926. if (recurse)
  1927. mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
  1928. else
  1929. mnt = clone_mnt(old, old_path.dentry, 0);
  1930. if (IS_ERR(mnt)) {
  1931. err = PTR_ERR(mnt);
  1932. goto out2;
  1933. }
  1934. mnt->mnt.mnt_flags &= ~MNT_LOCKED;
  1935. err = graft_tree(mnt, parent, mp);
  1936. if (err) {
  1937. lock_mount_hash();
  1938. umount_tree(mnt, UMOUNT_SYNC);
  1939. unlock_mount_hash();
  1940. }
  1941. out2:
  1942. unlock_mount(mp);
  1943. out:
  1944. path_put(&old_path);
  1945. return err;
  1946. }
  1947. static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
  1948. {
  1949. int error = 0;
  1950. int readonly_request = 0;
  1951. if (ms_flags & MS_RDONLY)
  1952. readonly_request = 1;
  1953. if (readonly_request == __mnt_is_readonly(mnt))
  1954. return 0;
  1955. if (readonly_request)
  1956. error = mnt_make_readonly(real_mount(mnt));
  1957. else
  1958. __mnt_unmake_readonly(real_mount(mnt));
  1959. return error;
  1960. }
  1961. /*
  1962. * change filesystem flags. dir should be a physical root of filesystem.
  1963. * If you've mounted a non-root directory somewhere and want to do remount
  1964. * on it - tough luck.
  1965. */
  1966. static int do_remount(struct path *path, int flags, int mnt_flags,
  1967. void *data)
  1968. {
  1969. int err;
  1970. struct super_block *sb = path->mnt->mnt_sb;
  1971. struct mount *mnt = real_mount(path->mnt);
  1972. if (!check_mnt(mnt))
  1973. return -EINVAL;
  1974. if (path->dentry != path->mnt->mnt_root)
  1975. return -EINVAL;
  1976. /* Don't allow changing of locked mnt flags.
  1977. *
  1978. * No locks need to be held here while testing the various
  1979. * MNT_LOCK flags because those flags can never be cleared
  1980. * once they are set.
  1981. */
  1982. if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
  1983. !(mnt_flags & MNT_READONLY)) {
  1984. return -EPERM;
  1985. }
  1986. if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
  1987. !(mnt_flags & MNT_NODEV)) {
  1988. return -EPERM;
  1989. }
  1990. if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
  1991. !(mnt_flags & MNT_NOSUID)) {
  1992. return -EPERM;
  1993. }
  1994. if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
  1995. !(mnt_flags & MNT_NOEXEC)) {
  1996. return -EPERM;
  1997. }
  1998. if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
  1999. ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
  2000. return -EPERM;
  2001. }
  2002. err = security_sb_remount(sb, data);
  2003. if (err)
  2004. return err;
  2005. down_write(&sb->s_umount);
  2006. if (flags & MS_BIND)
  2007. err = change_mount_flags(path->mnt, flags);
  2008. else if (!capable(CAP_SYS_ADMIN))
  2009. err = -EPERM;
  2010. else
  2011. err = do_remount_sb(sb, flags, data, 0);
  2012. if (!err) {
  2013. lock_mount_hash();
  2014. mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
  2015. mnt->mnt.mnt_flags = mnt_flags;
  2016. touch_mnt_namespace(mnt->mnt_ns);
  2017. unlock_mount_hash();
  2018. }
  2019. up_write(&sb->s_umount);
  2020. return err;
  2021. }
  2022. static inline int tree_contains_unbindable(struct mount *mnt)
  2023. {
  2024. struct mount *p;
  2025. for (p = mnt; p; p = next_mnt(p, mnt)) {
  2026. if (IS_MNT_UNBINDABLE(p))
  2027. return 1;
  2028. }
  2029. return 0;
  2030. }
  2031. static int do_move_mount(struct path *path, const char *old_name)
  2032. {
  2033. struct path old_path, parent_path;
  2034. struct mount *p;
  2035. struct mount *old;
  2036. struct mountpoint *mp;
  2037. int err;
  2038. if (!old_name || !*old_name)
  2039. return -EINVAL;
  2040. err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
  2041. if (err)
  2042. return err;
  2043. mp = lock_mount(path);
  2044. err = PTR_ERR(mp);
  2045. if (IS_ERR(mp))
  2046. goto out;
  2047. old = real_mount(old_path.mnt);
  2048. p = real_mount(path->mnt);
  2049. err = -EINVAL;
  2050. if (!check_mnt(p) || !check_mnt(old))
  2051. goto out1;
  2052. if (old->mnt.mnt_flags & MNT_LOCKED)
  2053. goto out1;
  2054. err = -EINVAL;
  2055. if (old_path.dentry != old_path.mnt->mnt_root)
  2056. goto out1;
  2057. if (!mnt_has_parent(old))
  2058. goto out1;
  2059. if (d_is_dir(path->dentry) !=
  2060. d_is_dir(old_path.dentry))
  2061. goto out1;
  2062. /*
  2063. * Don't move a mount residing in a shared parent.
  2064. */
  2065. if (IS_MNT_SHARED(old->mnt_parent))
  2066. goto out1;
  2067. /*
  2068. * Don't move a mount tree containing unbindable mounts to a destination
  2069. * mount which is shared.
  2070. */
  2071. if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
  2072. goto out1;
  2073. err = -ELOOP;
  2074. for (; mnt_has_parent(p); p = p->mnt_parent)
  2075. if (p == old)
  2076. goto out1;
  2077. err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
  2078. if (err)
  2079. goto out1;
  2080. /* if the mount is moved, it should no longer be expire
  2081. * automatically */
  2082. list_del_init(&old->mnt_expire);
  2083. out1:
  2084. unlock_mount(mp);
  2085. out:
  2086. if (!err)
  2087. path_put(&parent_path);
  2088. path_put(&old_path);
  2089. return err;
  2090. }
  2091. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  2092. {
  2093. int err;
  2094. const char *subtype = strchr(fstype, '.');
  2095. if (subtype) {
  2096. subtype++;
  2097. err = -EINVAL;
  2098. if (!subtype[0])
  2099. goto err;
  2100. } else
  2101. subtype = "";
  2102. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  2103. err = -ENOMEM;
  2104. if (!mnt->mnt_sb->s_subtype)
  2105. goto err;
  2106. return mnt;
  2107. err:
  2108. mntput(mnt);
  2109. return ERR_PTR(err);
  2110. }
  2111. /*
  2112. * add a mount into a namespace's mount tree
  2113. */
  2114. static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
  2115. {
  2116. struct mountpoint *mp;
  2117. struct mount *parent;
  2118. int err;
  2119. mnt_flags &= ~MNT_INTERNAL_FLAGS;
  2120. mp = lock_mount(path);
  2121. if (IS_ERR(mp))
  2122. return PTR_ERR(mp);
  2123. parent = real_mount(path->mnt);
  2124. err = -EINVAL;
  2125. if (unlikely(!check_mnt(parent))) {
  2126. /* that's acceptable only for automounts done in private ns */
  2127. if (!(mnt_flags & MNT_SHRINKABLE))
  2128. goto unlock;
  2129. /* ... and for those we'd better have mountpoint still alive */
  2130. if (!parent->mnt_ns)
  2131. goto unlock;
  2132. }
  2133. /* Refuse the same filesystem on the same mount point */
  2134. err = -EBUSY;
  2135. if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
  2136. path->mnt->mnt_root == path->dentry)
  2137. goto unlock;
  2138. err = -EINVAL;
  2139. if (d_is_symlink(newmnt->mnt.mnt_root))
  2140. goto unlock;
  2141. newmnt->mnt.mnt_flags = mnt_flags;
  2142. err = graft_tree(newmnt, parent, mp);
  2143. unlock:
  2144. unlock_mount(mp);
  2145. return err;
  2146. }
  2147. static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags);
  2148. /*
  2149. * create a new mount for userspace and request it to be added into the
  2150. * namespace's tree
  2151. */
  2152. static int do_new_mount(struct path *path, const char *fstype, int flags,
  2153. int mnt_flags, const char *name, void *data)
  2154. {
  2155. struct file_system_type *type;
  2156. struct vfsmount *mnt;
  2157. int err;
  2158. if (!fstype)
  2159. return -EINVAL;
  2160. type = get_fs_type(fstype);
  2161. if (!type)
  2162. return -ENODEV;
  2163. mnt = vfs_kern_mount(type, flags, name, data);
  2164. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  2165. !mnt->mnt_sb->s_subtype)
  2166. mnt = fs_set_subtype(mnt, fstype);
  2167. put_filesystem(type);
  2168. if (IS_ERR(mnt))
  2169. return PTR_ERR(mnt);
  2170. if (mount_too_revealing(mnt, &mnt_flags)) {
  2171. mntput(mnt);
  2172. return -EPERM;
  2173. }
  2174. err = do_add_mount(real_mount(mnt), path, mnt_flags);
  2175. if (err)
  2176. mntput(mnt);
  2177. return err;
  2178. }
  2179. int finish_automount(struct vfsmount *m, struct path *path)
  2180. {
  2181. struct mount *mnt = real_mount(m);
  2182. int err;
  2183. /* The new mount record should have at least 2 refs to prevent it being
  2184. * expired before we get a chance to add it
  2185. */
  2186. BUG_ON(mnt_get_count(mnt) < 2);
  2187. if (m->mnt_sb == path->mnt->mnt_sb &&
  2188. m->mnt_root == path->dentry) {
  2189. err = -ELOOP;
  2190. goto fail;
  2191. }
  2192. err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
  2193. if (!err)
  2194. return 0;
  2195. fail:
  2196. /* remove m from any expiration list it may be on */
  2197. if (!list_empty(&mnt->mnt_expire)) {
  2198. namespace_lock();
  2199. list_del_init(&mnt->mnt_expire);
  2200. namespace_unlock();
  2201. }
  2202. mntput(m);
  2203. mntput(m);
  2204. return err;
  2205. }
  2206. /**
  2207. * mnt_set_expiry - Put a mount on an expiration list
  2208. * @mnt: The mount to list.
  2209. * @expiry_list: The list to add the mount to.
  2210. */
  2211. void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
  2212. {
  2213. namespace_lock();
  2214. list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
  2215. namespace_unlock();
  2216. }
  2217. EXPORT_SYMBOL(mnt_set_expiry);
  2218. /*
  2219. * process a list of expirable mountpoints with the intent of discarding any
  2220. * mountpoints that aren't in use and haven't been touched since last we came
  2221. * here
  2222. */
  2223. void mark_mounts_for_expiry(struct list_head *mounts)
  2224. {
  2225. struct mount *mnt, *next;
  2226. LIST_HEAD(graveyard);
  2227. if (list_empty(mounts))
  2228. return;
  2229. namespace_lock();
  2230. lock_mount_hash();
  2231. /* extract from the expiration list every vfsmount that matches the
  2232. * following criteria:
  2233. * - only referenced by its parent vfsmount
  2234. * - still marked for expiry (marked on the last call here; marks are
  2235. * cleared by mntput())
  2236. */
  2237. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  2238. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  2239. propagate_mount_busy(mnt, 1))
  2240. continue;
  2241. list_move(&mnt->mnt_expire, &graveyard);
  2242. }
  2243. while (!list_empty(&graveyard)) {
  2244. mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
  2245. touch_mnt_namespace(mnt->mnt_ns);
  2246. umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
  2247. }
  2248. unlock_mount_hash();
  2249. namespace_unlock();
  2250. }
  2251. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  2252. /*
  2253. * Ripoff of 'select_parent()'
  2254. *
  2255. * search the list of submounts for a given mountpoint, and move any
  2256. * shrinkable submounts to the 'graveyard' list.
  2257. */
  2258. static int select_submounts(struct mount *parent, struct list_head *graveyard)
  2259. {
  2260. struct mount *this_parent = parent;
  2261. struct list_head *next;
  2262. int found = 0;
  2263. repeat:
  2264. next = this_parent->mnt_mounts.next;
  2265. resume:
  2266. while (next != &this_parent->mnt_mounts) {
  2267. struct list_head *tmp = next;
  2268. struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
  2269. next = tmp->next;
  2270. if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
  2271. continue;
  2272. /*
  2273. * Descend a level if the d_mounts list is non-empty.
  2274. */
  2275. if (!list_empty(&mnt->mnt_mounts)) {
  2276. this_parent = mnt;
  2277. goto repeat;
  2278. }
  2279. if (!propagate_mount_busy(mnt, 1)) {
  2280. list_move_tail(&mnt->mnt_expire, graveyard);
  2281. found++;
  2282. }
  2283. }
  2284. /*
  2285. * All done at this level ... ascend and resume the search
  2286. */
  2287. if (this_parent != parent) {
  2288. next = this_parent->mnt_child.next;
  2289. this_parent = this_parent->mnt_parent;
  2290. goto resume;
  2291. }
  2292. return found;
  2293. }
  2294. /*
  2295. * process a list of expirable mountpoints with the intent of discarding any
  2296. * submounts of a specific parent mountpoint
  2297. *
  2298. * mount_lock must be held for write
  2299. */
  2300. static void shrink_submounts(struct mount *mnt)
  2301. {
  2302. LIST_HEAD(graveyard);
  2303. struct mount *m;
  2304. /* extract submounts of 'mountpoint' from the expiration list */
  2305. while (select_submounts(mnt, &graveyard)) {
  2306. while (!list_empty(&graveyard)) {
  2307. m = list_first_entry(&graveyard, struct mount,
  2308. mnt_expire);
  2309. touch_mnt_namespace(m->mnt_ns);
  2310. umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
  2311. }
  2312. }
  2313. }
  2314. /*
  2315. * Some copy_from_user() implementations do not return the exact number of
  2316. * bytes remaining to copy on a fault. But copy_mount_options() requires that.
  2317. * Note that this function differs from copy_from_user() in that it will oops
  2318. * on bad values of `to', rather than returning a short copy.
  2319. */
  2320. static long exact_copy_from_user(void *to, const void __user * from,
  2321. unsigned long n)
  2322. {
  2323. char *t = to;
  2324. const char __user *f = from;
  2325. char c;
  2326. if (!access_ok(VERIFY_READ, from, n))
  2327. return n;
  2328. while (n) {
  2329. if (__get_user(c, f)) {
  2330. memset(t, 0, n);
  2331. break;
  2332. }
  2333. *t++ = c;
  2334. f++;
  2335. n--;
  2336. }
  2337. return n;
  2338. }
  2339. void *copy_mount_options(const void __user * data)
  2340. {
  2341. int i;
  2342. unsigned long size;
  2343. char *copy;
  2344. if (!data)
  2345. return NULL;
  2346. copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2347. if (!copy)
  2348. return ERR_PTR(-ENOMEM);
  2349. /* We only care that *some* data at the address the user
  2350. * gave us is valid. Just in case, we'll zero
  2351. * the remainder of the page.
  2352. */
  2353. /* copy_from_user cannot cross TASK_SIZE ! */
  2354. size = TASK_SIZE - (unsigned long)data;
  2355. if (size > PAGE_SIZE)
  2356. size = PAGE_SIZE;
  2357. i = size - exact_copy_from_user(copy, data, size);
  2358. if (!i) {
  2359. kfree(copy);
  2360. return ERR_PTR(-EFAULT);
  2361. }
  2362. if (i != PAGE_SIZE)
  2363. memset(copy + i, 0, PAGE_SIZE - i);
  2364. return copy;
  2365. }
  2366. char *copy_mount_string(const void __user *data)
  2367. {
  2368. return data ? strndup_user(data, PAGE_SIZE) : NULL;
  2369. }
  2370. /*
  2371. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  2372. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  2373. *
  2374. * data is a (void *) that can point to any structure up to
  2375. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  2376. * information (or be NULL).
  2377. *
  2378. * Pre-0.97 versions of mount() didn't have a flags word.
  2379. * When the flags word was introduced its top half was required
  2380. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  2381. * Therefore, if this magic number is present, it carries no information
  2382. * and must be discarded.
  2383. */
  2384. long do_mount(const char *dev_name, const char __user *dir_name,
  2385. const char *type_page, unsigned long flags, void *data_page)
  2386. {
  2387. struct path path;
  2388. int retval = 0;
  2389. int mnt_flags = 0;
  2390. /* Discard magic */
  2391. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  2392. flags &= ~MS_MGC_MSK;
  2393. /* Basic sanity checks */
  2394. if (data_page)
  2395. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  2396. /* ... and get the mountpoint */
  2397. retval = user_path(dir_name, &path);
  2398. if (retval)
  2399. return retval;
  2400. retval = security_sb_mount(dev_name, &path,
  2401. type_page, flags, data_page);
  2402. if (!retval && !may_mount())
  2403. retval = -EPERM;
  2404. if (!retval && (flags & MS_MANDLOCK) && !may_mandlock())
  2405. retval = -EPERM;
  2406. if (retval)
  2407. goto dput_out;
  2408. /* Default to relatime unless overriden */
  2409. if (!(flags & MS_NOATIME))
  2410. mnt_flags |= MNT_RELATIME;
  2411. /* Separate the per-mountpoint flags */
  2412. if (flags & MS_NOSUID)
  2413. mnt_flags |= MNT_NOSUID;
  2414. if (flags & MS_NODEV)
  2415. mnt_flags |= MNT_NODEV;
  2416. if (flags & MS_NOEXEC)
  2417. mnt_flags |= MNT_NOEXEC;
  2418. if (flags & MS_NOATIME)
  2419. mnt_flags |= MNT_NOATIME;
  2420. if (flags & MS_NODIRATIME)
  2421. mnt_flags |= MNT_NODIRATIME;
  2422. if (flags & MS_STRICTATIME)
  2423. mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
  2424. if (flags & MS_RDONLY)
  2425. mnt_flags |= MNT_READONLY;
  2426. /* The default atime for remount is preservation */
  2427. if ((flags & MS_REMOUNT) &&
  2428. ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
  2429. MS_STRICTATIME)) == 0)) {
  2430. mnt_flags &= ~MNT_ATIME_MASK;
  2431. mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
  2432. }
  2433. flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
  2434. MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
  2435. MS_STRICTATIME | MS_NOREMOTELOCK);
  2436. if (flags & MS_REMOUNT)
  2437. retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
  2438. data_page);
  2439. else if (flags & MS_BIND)
  2440. retval = do_loopback(&path, dev_name, flags & MS_REC);
  2441. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  2442. retval = do_change_type(&path, flags);
  2443. else if (flags & MS_MOVE)
  2444. retval = do_move_mount(&path, dev_name);
  2445. else
  2446. retval = do_new_mount(&path, type_page, flags, mnt_flags,
  2447. dev_name, data_page);
  2448. dput_out:
  2449. path_put(&path);
  2450. return retval;
  2451. }
  2452. static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
  2453. {
  2454. return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
  2455. }
  2456. static void dec_mnt_namespaces(struct ucounts *ucounts)
  2457. {
  2458. dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
  2459. }
  2460. static void free_mnt_ns(struct mnt_namespace *ns)
  2461. {
  2462. ns_free_inum(&ns->ns);
  2463. dec_mnt_namespaces(ns->ucounts);
  2464. put_user_ns(ns->user_ns);
  2465. kfree(ns);
  2466. }
  2467. /*
  2468. * Assign a sequence number so we can detect when we attempt to bind
  2469. * mount a reference to an older mount namespace into the current
  2470. * mount namespace, preventing reference counting loops. A 64bit
  2471. * number incrementing at 10Ghz will take 12,427 years to wrap which
  2472. * is effectively never, so we can ignore the possibility.
  2473. */
  2474. static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
  2475. static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
  2476. {
  2477. struct mnt_namespace *new_ns;
  2478. struct ucounts *ucounts;
  2479. int ret;
  2480. ucounts = inc_mnt_namespaces(user_ns);
  2481. if (!ucounts)
  2482. return ERR_PTR(-ENOSPC);
  2483. new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
  2484. if (!new_ns) {
  2485. dec_mnt_namespaces(ucounts);
  2486. return ERR_PTR(-ENOMEM);
  2487. }
  2488. ret = ns_alloc_inum(&new_ns->ns);
  2489. if (ret) {
  2490. kfree(new_ns);
  2491. dec_mnt_namespaces(ucounts);
  2492. return ERR_PTR(ret);
  2493. }
  2494. new_ns->ns.ops = &mntns_operations;
  2495. new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
  2496. atomic_set(&new_ns->count, 1);
  2497. new_ns->root = NULL;
  2498. INIT_LIST_HEAD(&new_ns->list);
  2499. init_waitqueue_head(&new_ns->poll);
  2500. new_ns->event = 0;
  2501. new_ns->user_ns = get_user_ns(user_ns);
  2502. new_ns->ucounts = ucounts;
  2503. new_ns->mounts = 0;
  2504. new_ns->pending_mounts = 0;
  2505. return new_ns;
  2506. }
  2507. __latent_entropy
  2508. struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
  2509. struct user_namespace *user_ns, struct fs_struct *new_fs)
  2510. {
  2511. struct mnt_namespace *new_ns;
  2512. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
  2513. struct mount *p, *q;
  2514. struct mount *old;
  2515. struct mount *new;
  2516. int copy_flags;
  2517. BUG_ON(!ns);
  2518. if (likely(!(flags & CLONE_NEWNS))) {
  2519. get_mnt_ns(ns);
  2520. return ns;
  2521. }
  2522. old = ns->root;
  2523. new_ns = alloc_mnt_ns(user_ns);
  2524. if (IS_ERR(new_ns))
  2525. return new_ns;
  2526. namespace_lock();
  2527. /* First pass: copy the tree topology */
  2528. copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
  2529. if (user_ns != ns->user_ns)
  2530. copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
  2531. new = copy_tree(old, old->mnt.mnt_root, copy_flags);
  2532. if (IS_ERR(new)) {
  2533. namespace_unlock();
  2534. free_mnt_ns(new_ns);
  2535. return ERR_CAST(new);
  2536. }
  2537. new_ns->root = new;
  2538. list_add_tail(&new_ns->list, &new->mnt_list);
  2539. /*
  2540. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  2541. * as belonging to new namespace. We have already acquired a private
  2542. * fs_struct, so tsk->fs->lock is not needed.
  2543. */
  2544. p = old;
  2545. q = new;
  2546. while (p) {
  2547. q->mnt_ns = new_ns;
  2548. new_ns->mounts++;
  2549. if (new_fs) {
  2550. if (&p->mnt == new_fs->root.mnt) {
  2551. new_fs->root.mnt = mntget(&q->mnt);
  2552. rootmnt = &p->mnt;
  2553. }
  2554. if (&p->mnt == new_fs->pwd.mnt) {
  2555. new_fs->pwd.mnt = mntget(&q->mnt);
  2556. pwdmnt = &p->mnt;
  2557. }
  2558. }
  2559. p = next_mnt(p, old);
  2560. q = next_mnt(q, new);
  2561. if (!q)
  2562. break;
  2563. while (p->mnt.mnt_root != q->mnt.mnt_root)
  2564. p = next_mnt(p, old);
  2565. }
  2566. namespace_unlock();
  2567. if (rootmnt)
  2568. mntput(rootmnt);
  2569. if (pwdmnt)
  2570. mntput(pwdmnt);
  2571. return new_ns;
  2572. }
  2573. /**
  2574. * create_mnt_ns - creates a private namespace and adds a root filesystem
  2575. * @mnt: pointer to the new root filesystem mountpoint
  2576. */
  2577. static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
  2578. {
  2579. struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
  2580. if (!IS_ERR(new_ns)) {
  2581. struct mount *mnt = real_mount(m);
  2582. mnt->mnt_ns = new_ns;
  2583. new_ns->root = mnt;
  2584. new_ns->mounts++;
  2585. list_add(&mnt->mnt_list, &new_ns->list);
  2586. } else {
  2587. mntput(m);
  2588. }
  2589. return new_ns;
  2590. }
  2591. struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
  2592. {
  2593. struct mnt_namespace *ns;
  2594. struct super_block *s;
  2595. struct path path;
  2596. int err;
  2597. ns = create_mnt_ns(mnt);
  2598. if (IS_ERR(ns))
  2599. return ERR_CAST(ns);
  2600. err = vfs_path_lookup(mnt->mnt_root, mnt,
  2601. name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  2602. put_mnt_ns(ns);
  2603. if (err)
  2604. return ERR_PTR(err);
  2605. /* trade a vfsmount reference for active sb one */
  2606. s = path.mnt->mnt_sb;
  2607. atomic_inc(&s->s_active);
  2608. mntput(path.mnt);
  2609. /* lock the sucker */
  2610. down_write(&s->s_umount);
  2611. /* ... and return the root of (sub)tree on it */
  2612. return path.dentry;
  2613. }
  2614. EXPORT_SYMBOL(mount_subtree);
  2615. SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
  2616. char __user *, type, unsigned long, flags, void __user *, data)
  2617. {
  2618. int ret;
  2619. char *kernel_type;
  2620. char *kernel_dev;
  2621. void *options;
  2622. kernel_type = copy_mount_string(type);
  2623. ret = PTR_ERR(kernel_type);
  2624. if (IS_ERR(kernel_type))
  2625. goto out_type;
  2626. kernel_dev = copy_mount_string(dev_name);
  2627. ret = PTR_ERR(kernel_dev);
  2628. if (IS_ERR(kernel_dev))
  2629. goto out_dev;
  2630. options = copy_mount_options(data);
  2631. ret = PTR_ERR(options);
  2632. if (IS_ERR(options))
  2633. goto out_data;
  2634. ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
  2635. kfree(options);
  2636. out_data:
  2637. kfree(kernel_dev);
  2638. out_dev:
  2639. kfree(kernel_type);
  2640. out_type:
  2641. return ret;
  2642. }
  2643. /*
  2644. * Return true if path is reachable from root
  2645. *
  2646. * namespace_sem or mount_lock is held
  2647. */
  2648. bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
  2649. const struct path *root)
  2650. {
  2651. while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
  2652. dentry = mnt->mnt_mountpoint;
  2653. mnt = mnt->mnt_parent;
  2654. }
  2655. return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
  2656. }
  2657. bool path_is_under(const struct path *path1, const struct path *path2)
  2658. {
  2659. bool res;
  2660. read_seqlock_excl(&mount_lock);
  2661. res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
  2662. read_sequnlock_excl(&mount_lock);
  2663. return res;
  2664. }
  2665. EXPORT_SYMBOL(path_is_under);
  2666. /*
  2667. * pivot_root Semantics:
  2668. * Moves the root file system of the current process to the directory put_old,
  2669. * makes new_root as the new root file system of the current process, and sets
  2670. * root/cwd of all processes which had them on the current root to new_root.
  2671. *
  2672. * Restrictions:
  2673. * The new_root and put_old must be directories, and must not be on the
  2674. * same file system as the current process root. The put_old must be
  2675. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  2676. * pointed to by put_old must yield the same directory as new_root. No other
  2677. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  2678. *
  2679. * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
  2680. * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
  2681. * in this situation.
  2682. *
  2683. * Notes:
  2684. * - we don't move root/cwd if they are not at the root (reason: if something
  2685. * cared enough to change them, it's probably wrong to force them elsewhere)
  2686. * - it's okay to pick a root that isn't the root of a file system, e.g.
  2687. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  2688. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  2689. * first.
  2690. */
  2691. SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
  2692. const char __user *, put_old)
  2693. {
  2694. struct path new, old, parent_path, root_parent, root;
  2695. struct mount *new_mnt, *root_mnt, *old_mnt;
  2696. struct mountpoint *old_mp, *root_mp;
  2697. int error;
  2698. if (!may_mount())
  2699. return -EPERM;
  2700. error = user_path_dir(new_root, &new);
  2701. if (error)
  2702. goto out0;
  2703. error = user_path_dir(put_old, &old);
  2704. if (error)
  2705. goto out1;
  2706. error = security_sb_pivotroot(&old, &new);
  2707. if (error)
  2708. goto out2;
  2709. get_fs_root(current->fs, &root);
  2710. old_mp = lock_mount(&old);
  2711. error = PTR_ERR(old_mp);
  2712. if (IS_ERR(old_mp))
  2713. goto out3;
  2714. error = -EINVAL;
  2715. new_mnt = real_mount(new.mnt);
  2716. root_mnt = real_mount(root.mnt);
  2717. old_mnt = real_mount(old.mnt);
  2718. if (IS_MNT_SHARED(old_mnt) ||
  2719. IS_MNT_SHARED(new_mnt->mnt_parent) ||
  2720. IS_MNT_SHARED(root_mnt->mnt_parent))
  2721. goto out4;
  2722. if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
  2723. goto out4;
  2724. if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
  2725. goto out4;
  2726. error = -ENOENT;
  2727. if (d_unlinked(new.dentry))
  2728. goto out4;
  2729. error = -EBUSY;
  2730. if (new_mnt == root_mnt || old_mnt == root_mnt)
  2731. goto out4; /* loop, on the same file system */
  2732. error = -EINVAL;
  2733. if (root.mnt->mnt_root != root.dentry)
  2734. goto out4; /* not a mountpoint */
  2735. if (!mnt_has_parent(root_mnt))
  2736. goto out4; /* not attached */
  2737. root_mp = root_mnt->mnt_mp;
  2738. if (new.mnt->mnt_root != new.dentry)
  2739. goto out4; /* not a mountpoint */
  2740. if (!mnt_has_parent(new_mnt))
  2741. goto out4; /* not attached */
  2742. /* make sure we can reach put_old from new_root */
  2743. if (!is_path_reachable(old_mnt, old.dentry, &new))
  2744. goto out4;
  2745. /* make certain new is below the root */
  2746. if (!is_path_reachable(new_mnt, new.dentry, &root))
  2747. goto out4;
  2748. root_mp->m_count++; /* pin it so it won't go away */
  2749. lock_mount_hash();
  2750. detach_mnt(new_mnt, &parent_path);
  2751. detach_mnt(root_mnt, &root_parent);
  2752. if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
  2753. new_mnt->mnt.mnt_flags |= MNT_LOCKED;
  2754. root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
  2755. }
  2756. /* mount old root on put_old */
  2757. attach_mnt(root_mnt, old_mnt, old_mp);
  2758. /* mount new_root on / */
  2759. attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
  2760. touch_mnt_namespace(current->nsproxy->mnt_ns);
  2761. /* A moved mount should not expire automatically */
  2762. list_del_init(&new_mnt->mnt_expire);
  2763. unlock_mount_hash();
  2764. chroot_fs_refs(&root, &new);
  2765. put_mountpoint(root_mp);
  2766. error = 0;
  2767. out4:
  2768. unlock_mount(old_mp);
  2769. if (!error) {
  2770. path_put(&root_parent);
  2771. path_put(&parent_path);
  2772. }
  2773. out3:
  2774. path_put(&root);
  2775. out2:
  2776. path_put(&old);
  2777. out1:
  2778. path_put(&new);
  2779. out0:
  2780. return error;
  2781. }
  2782. static void __init init_mount_tree(void)
  2783. {
  2784. struct vfsmount *mnt;
  2785. struct mnt_namespace *ns;
  2786. struct path root;
  2787. struct file_system_type *type;
  2788. type = get_fs_type("rootfs");
  2789. if (!type)
  2790. panic("Can't find rootfs type");
  2791. mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
  2792. put_filesystem(type);
  2793. if (IS_ERR(mnt))
  2794. panic("Can't create rootfs");
  2795. ns = create_mnt_ns(mnt);
  2796. if (IS_ERR(ns))
  2797. panic("Can't allocate initial namespace");
  2798. init_task.nsproxy->mnt_ns = ns;
  2799. get_mnt_ns(ns);
  2800. root.mnt = mnt;
  2801. root.dentry = mnt->mnt_root;
  2802. mnt->mnt_flags |= MNT_LOCKED;
  2803. set_fs_pwd(current->fs, &root);
  2804. set_fs_root(current->fs, &root);
  2805. }
  2806. void __init mnt_init(void)
  2807. {
  2808. unsigned u;
  2809. int err;
  2810. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
  2811. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2812. mount_hashtable = alloc_large_system_hash("Mount-cache",
  2813. sizeof(struct hlist_head),
  2814. mhash_entries, 19,
  2815. 0,
  2816. &m_hash_shift, &m_hash_mask, 0, 0);
  2817. mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
  2818. sizeof(struct hlist_head),
  2819. mphash_entries, 19,
  2820. 0,
  2821. &mp_hash_shift, &mp_hash_mask, 0, 0);
  2822. if (!mount_hashtable || !mountpoint_hashtable)
  2823. panic("Failed to allocate mount hash table\n");
  2824. for (u = 0; u <= m_hash_mask; u++)
  2825. INIT_HLIST_HEAD(&mount_hashtable[u]);
  2826. for (u = 0; u <= mp_hash_mask; u++)
  2827. INIT_HLIST_HEAD(&mountpoint_hashtable[u]);
  2828. kernfs_init();
  2829. err = sysfs_init();
  2830. if (err)
  2831. printk(KERN_WARNING "%s: sysfs_init error: %d\n",
  2832. __func__, err);
  2833. fs_kobj = kobject_create_and_add("fs", NULL);
  2834. if (!fs_kobj)
  2835. printk(KERN_WARNING "%s: kobj create error\n", __func__);
  2836. init_rootfs();
  2837. init_mount_tree();
  2838. }
  2839. void put_mnt_ns(struct mnt_namespace *ns)
  2840. {
  2841. if (!atomic_dec_and_test(&ns->count))
  2842. return;
  2843. drop_collected_mounts(&ns->root->mnt);
  2844. free_mnt_ns(ns);
  2845. }
  2846. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  2847. {
  2848. struct vfsmount *mnt;
  2849. mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  2850. if (!IS_ERR(mnt)) {
  2851. /*
  2852. * it is a longterm mount, don't release mnt until
  2853. * we unmount before file sys is unregistered
  2854. */
  2855. real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
  2856. }
  2857. return mnt;
  2858. }
  2859. EXPORT_SYMBOL_GPL(kern_mount_data);
  2860. void kern_unmount(struct vfsmount *mnt)
  2861. {
  2862. /* release long term mount so mount point can be released */
  2863. if (!IS_ERR_OR_NULL(mnt)) {
  2864. real_mount(mnt)->mnt_ns = NULL;
  2865. synchronize_rcu(); /* yecchhh... */
  2866. mntput(mnt);
  2867. }
  2868. }
  2869. EXPORT_SYMBOL(kern_unmount);
  2870. bool our_mnt(struct vfsmount *mnt)
  2871. {
  2872. return check_mnt(real_mount(mnt));
  2873. }
  2874. bool current_chrooted(void)
  2875. {
  2876. /* Does the current process have a non-standard root */
  2877. struct path ns_root;
  2878. struct path fs_root;
  2879. bool chrooted;
  2880. /* Find the namespace root */
  2881. ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
  2882. ns_root.dentry = ns_root.mnt->mnt_root;
  2883. path_get(&ns_root);
  2884. while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
  2885. ;
  2886. get_fs_root(current->fs, &fs_root);
  2887. chrooted = !path_equal(&fs_root, &ns_root);
  2888. path_put(&fs_root);
  2889. path_put(&ns_root);
  2890. return chrooted;
  2891. }
  2892. static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new,
  2893. int *new_mnt_flags)
  2894. {
  2895. int new_flags = *new_mnt_flags;
  2896. struct mount *mnt;
  2897. bool visible = false;
  2898. down_read(&namespace_sem);
  2899. list_for_each_entry(mnt, &ns->list, mnt_list) {
  2900. struct mount *child;
  2901. int mnt_flags;
  2902. if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type)
  2903. continue;
  2904. /* This mount is not fully visible if it's root directory
  2905. * is not the root directory of the filesystem.
  2906. */
  2907. if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
  2908. continue;
  2909. /* A local view of the mount flags */
  2910. mnt_flags = mnt->mnt.mnt_flags;
  2911. /* Don't miss readonly hidden in the superblock flags */
  2912. if (mnt->mnt.mnt_sb->s_flags & MS_RDONLY)
  2913. mnt_flags |= MNT_LOCK_READONLY;
  2914. /* Verify the mount flags are equal to or more permissive
  2915. * than the proposed new mount.
  2916. */
  2917. if ((mnt_flags & MNT_LOCK_READONLY) &&
  2918. !(new_flags & MNT_READONLY))
  2919. continue;
  2920. if ((mnt_flags & MNT_LOCK_ATIME) &&
  2921. ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
  2922. continue;
  2923. /* This mount is not fully visible if there are any
  2924. * locked child mounts that cover anything except for
  2925. * empty directories.
  2926. */
  2927. list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
  2928. struct inode *inode = child->mnt_mountpoint->d_inode;
  2929. /* Only worry about locked mounts */
  2930. if (!(child->mnt.mnt_flags & MNT_LOCKED))
  2931. continue;
  2932. /* Is the directory permanetly empty? */
  2933. if (!is_empty_dir_inode(inode))
  2934. goto next;
  2935. }
  2936. /* Preserve the locked attributes */
  2937. *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
  2938. MNT_LOCK_ATIME);
  2939. visible = true;
  2940. goto found;
  2941. next: ;
  2942. }
  2943. found:
  2944. up_read(&namespace_sem);
  2945. return visible;
  2946. }
  2947. static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags)
  2948. {
  2949. const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
  2950. struct mnt_namespace *ns = current->nsproxy->mnt_ns;
  2951. unsigned long s_iflags;
  2952. if (ns->user_ns == &init_user_ns)
  2953. return false;
  2954. /* Can this filesystem be too revealing? */
  2955. s_iflags = mnt->mnt_sb->s_iflags;
  2956. if (!(s_iflags & SB_I_USERNS_VISIBLE))
  2957. return false;
  2958. if ((s_iflags & required_iflags) != required_iflags) {
  2959. WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
  2960. required_iflags);
  2961. return true;
  2962. }
  2963. return !mnt_already_visible(ns, mnt, new_mnt_flags);
  2964. }
  2965. bool mnt_may_suid(struct vfsmount *mnt)
  2966. {
  2967. /*
  2968. * Foreign mounts (accessed via fchdir or through /proc
  2969. * symlinks) are always treated as if they are nosuid. This
  2970. * prevents namespaces from trusting potentially unsafe
  2971. * suid/sgid bits, file caps, or security labels that originate
  2972. * in other namespaces.
  2973. */
  2974. return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
  2975. current_in_userns(mnt->mnt_sb->s_user_ns);
  2976. }
  2977. static struct ns_common *mntns_get(struct task_struct *task)
  2978. {
  2979. struct ns_common *ns = NULL;
  2980. struct nsproxy *nsproxy;
  2981. task_lock(task);
  2982. nsproxy = task->nsproxy;
  2983. if (nsproxy) {
  2984. ns = &nsproxy->mnt_ns->ns;
  2985. get_mnt_ns(to_mnt_ns(ns));
  2986. }
  2987. task_unlock(task);
  2988. return ns;
  2989. }
  2990. static void mntns_put(struct ns_common *ns)
  2991. {
  2992. put_mnt_ns(to_mnt_ns(ns));
  2993. }
  2994. static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  2995. {
  2996. struct fs_struct *fs = current->fs;
  2997. struct mnt_namespace *mnt_ns = to_mnt_ns(ns);
  2998. struct path root;
  2999. if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
  3000. !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
  3001. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  3002. return -EPERM;
  3003. if (fs->users != 1)
  3004. return -EINVAL;
  3005. get_mnt_ns(mnt_ns);
  3006. put_mnt_ns(nsproxy->mnt_ns);
  3007. nsproxy->mnt_ns = mnt_ns;
  3008. /* Find the root */
  3009. root.mnt = &mnt_ns->root->mnt;
  3010. root.dentry = mnt_ns->root->mnt.mnt_root;
  3011. path_get(&root);
  3012. while(d_mountpoint(root.dentry) && follow_down_one(&root))
  3013. ;
  3014. /* Update the pwd and root */
  3015. set_fs_pwd(fs, &root);
  3016. set_fs_root(fs, &root);
  3017. path_put(&root);
  3018. return 0;
  3019. }
  3020. static struct user_namespace *mntns_owner(struct ns_common *ns)
  3021. {
  3022. return to_mnt_ns(ns)->user_ns;
  3023. }
  3024. const struct proc_ns_operations mntns_operations = {
  3025. .name = "mnt",
  3026. .type = CLONE_NEWNS,
  3027. .get = mntns_get,
  3028. .put = mntns_put,
  3029. .install = mntns_install,
  3030. .owner = mntns_owner,
  3031. };