namespace.c 74 KB

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