namespace.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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/config.h>
  11. #include <linux/syscalls.h>
  12. #include <linux/slab.h>
  13. #include <linux/sched.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/init.h>
  16. #include <linux/quotaops.h>
  17. #include <linux/acct.h>
  18. #include <linux/module.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/namespace.h>
  21. #include <linux/namei.h>
  22. #include <linux/security.h>
  23. #include <linux/mount.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/unistd.h>
  26. #include "pnode.h"
  27. extern int __init init_rootfs(void);
  28. #ifdef CONFIG_SYSFS
  29. extern int __init sysfs_init(void);
  30. #else
  31. static inline int sysfs_init(void)
  32. {
  33. return 0;
  34. }
  35. #endif
  36. /* spinlock for vfsmount related operations, inplace of dcache_lock */
  37. __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
  38. static int event;
  39. static struct list_head *mount_hashtable;
  40. static int hash_mask __read_mostly, hash_bits __read_mostly;
  41. static kmem_cache_t *mnt_cache;
  42. static struct rw_semaphore namespace_sem;
  43. static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
  44. {
  45. unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
  46. tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
  47. tmp = tmp + (tmp >> hash_bits);
  48. return tmp & hash_mask;
  49. }
  50. struct vfsmount *alloc_vfsmnt(const char *name)
  51. {
  52. struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
  53. if (mnt) {
  54. memset(mnt, 0, sizeof(struct vfsmount));
  55. atomic_set(&mnt->mnt_count, 1);
  56. INIT_LIST_HEAD(&mnt->mnt_hash);
  57. INIT_LIST_HEAD(&mnt->mnt_child);
  58. INIT_LIST_HEAD(&mnt->mnt_mounts);
  59. INIT_LIST_HEAD(&mnt->mnt_list);
  60. INIT_LIST_HEAD(&mnt->mnt_expire);
  61. INIT_LIST_HEAD(&mnt->mnt_share);
  62. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  63. INIT_LIST_HEAD(&mnt->mnt_slave);
  64. if (name) {
  65. int size = strlen(name) + 1;
  66. char *newname = kmalloc(size, GFP_KERNEL);
  67. if (newname) {
  68. memcpy(newname, name, size);
  69. mnt->mnt_devname = newname;
  70. }
  71. }
  72. }
  73. return mnt;
  74. }
  75. void free_vfsmnt(struct vfsmount *mnt)
  76. {
  77. kfree(mnt->mnt_devname);
  78. kmem_cache_free(mnt_cache, mnt);
  79. }
  80. /*
  81. * find the first or last mount at @dentry on vfsmount @mnt depending on
  82. * @dir. If @dir is set return the first mount else return the last mount.
  83. */
  84. struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
  85. int dir)
  86. {
  87. struct list_head *head = mount_hashtable + hash(mnt, dentry);
  88. struct list_head *tmp = head;
  89. struct vfsmount *p, *found = NULL;
  90. for (;;) {
  91. tmp = dir ? tmp->next : tmp->prev;
  92. p = NULL;
  93. if (tmp == head)
  94. break;
  95. p = list_entry(tmp, struct vfsmount, mnt_hash);
  96. if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
  97. found = p;
  98. break;
  99. }
  100. }
  101. return found;
  102. }
  103. /*
  104. * lookup_mnt increments the ref count before returning
  105. * the vfsmount struct.
  106. */
  107. struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
  108. {
  109. struct vfsmount *child_mnt;
  110. spin_lock(&vfsmount_lock);
  111. if ((child_mnt = __lookup_mnt(mnt, dentry, 1)))
  112. mntget(child_mnt);
  113. spin_unlock(&vfsmount_lock);
  114. return child_mnt;
  115. }
  116. static inline int check_mnt(struct vfsmount *mnt)
  117. {
  118. return mnt->mnt_namespace == current->namespace;
  119. }
  120. static void touch_namespace(struct namespace *ns)
  121. {
  122. if (ns) {
  123. ns->event = ++event;
  124. wake_up_interruptible(&ns->poll);
  125. }
  126. }
  127. static void __touch_namespace(struct namespace *ns)
  128. {
  129. if (ns && ns->event != event) {
  130. ns->event = event;
  131. wake_up_interruptible(&ns->poll);
  132. }
  133. }
  134. static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
  135. {
  136. old_nd->dentry = mnt->mnt_mountpoint;
  137. old_nd->mnt = mnt->mnt_parent;
  138. mnt->mnt_parent = mnt;
  139. mnt->mnt_mountpoint = mnt->mnt_root;
  140. list_del_init(&mnt->mnt_child);
  141. list_del_init(&mnt->mnt_hash);
  142. old_nd->dentry->d_mounted--;
  143. }
  144. void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
  145. struct vfsmount *child_mnt)
  146. {
  147. child_mnt->mnt_parent = mntget(mnt);
  148. child_mnt->mnt_mountpoint = dget(dentry);
  149. dentry->d_mounted++;
  150. }
  151. static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
  152. {
  153. mnt_set_mountpoint(nd->mnt, nd->dentry, mnt);
  154. list_add_tail(&mnt->mnt_hash, mount_hashtable +
  155. hash(nd->mnt, nd->dentry));
  156. list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
  157. }
  158. /*
  159. * the caller must hold vfsmount_lock
  160. */
  161. static void commit_tree(struct vfsmount *mnt)
  162. {
  163. struct vfsmount *parent = mnt->mnt_parent;
  164. struct vfsmount *m;
  165. LIST_HEAD(head);
  166. struct namespace *n = parent->mnt_namespace;
  167. BUG_ON(parent == mnt);
  168. list_add_tail(&head, &mnt->mnt_list);
  169. list_for_each_entry(m, &head, mnt_list)
  170. m->mnt_namespace = n;
  171. list_splice(&head, n->list.prev);
  172. list_add_tail(&mnt->mnt_hash, mount_hashtable +
  173. hash(parent, mnt->mnt_mountpoint));
  174. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  175. touch_namespace(n);
  176. }
  177. static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
  178. {
  179. struct list_head *next = p->mnt_mounts.next;
  180. if (next == &p->mnt_mounts) {
  181. while (1) {
  182. if (p == root)
  183. return NULL;
  184. next = p->mnt_child.next;
  185. if (next != &p->mnt_parent->mnt_mounts)
  186. break;
  187. p = p->mnt_parent;
  188. }
  189. }
  190. return list_entry(next, struct vfsmount, mnt_child);
  191. }
  192. static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
  193. {
  194. struct list_head *prev = p->mnt_mounts.prev;
  195. while (prev != &p->mnt_mounts) {
  196. p = list_entry(prev, struct vfsmount, mnt_child);
  197. prev = p->mnt_mounts.prev;
  198. }
  199. return p;
  200. }
  201. static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
  202. int flag)
  203. {
  204. struct super_block *sb = old->mnt_sb;
  205. struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
  206. if (mnt) {
  207. mnt->mnt_flags = old->mnt_flags;
  208. atomic_inc(&sb->s_active);
  209. mnt->mnt_sb = sb;
  210. mnt->mnt_root = dget(root);
  211. mnt->mnt_mountpoint = mnt->mnt_root;
  212. mnt->mnt_parent = mnt;
  213. if (flag & CL_SLAVE) {
  214. list_add(&mnt->mnt_slave, &old->mnt_slave_list);
  215. mnt->mnt_master = old;
  216. CLEAR_MNT_SHARED(mnt);
  217. } else {
  218. if ((flag & CL_PROPAGATION) || IS_MNT_SHARED(old))
  219. list_add(&mnt->mnt_share, &old->mnt_share);
  220. if (IS_MNT_SLAVE(old))
  221. list_add(&mnt->mnt_slave, &old->mnt_slave);
  222. mnt->mnt_master = old->mnt_master;
  223. }
  224. if (flag & CL_MAKE_SHARED)
  225. set_mnt_shared(mnt);
  226. /* stick the duplicate mount on the same expiry list
  227. * as the original if that was on one */
  228. if (flag & CL_EXPIRE) {
  229. spin_lock(&vfsmount_lock);
  230. if (!list_empty(&old->mnt_expire))
  231. list_add(&mnt->mnt_expire, &old->mnt_expire);
  232. spin_unlock(&vfsmount_lock);
  233. }
  234. }
  235. return mnt;
  236. }
  237. static inline void __mntput(struct vfsmount *mnt)
  238. {
  239. struct super_block *sb = mnt->mnt_sb;
  240. dput(mnt->mnt_root);
  241. free_vfsmnt(mnt);
  242. deactivate_super(sb);
  243. }
  244. void mntput_no_expire(struct vfsmount *mnt)
  245. {
  246. repeat:
  247. if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
  248. if (likely(!mnt->mnt_pinned)) {
  249. spin_unlock(&vfsmount_lock);
  250. __mntput(mnt);
  251. return;
  252. }
  253. atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
  254. mnt->mnt_pinned = 0;
  255. spin_unlock(&vfsmount_lock);
  256. acct_auto_close_mnt(mnt);
  257. security_sb_umount_close(mnt);
  258. goto repeat;
  259. }
  260. }
  261. EXPORT_SYMBOL(mntput_no_expire);
  262. void mnt_pin(struct vfsmount *mnt)
  263. {
  264. spin_lock(&vfsmount_lock);
  265. mnt->mnt_pinned++;
  266. spin_unlock(&vfsmount_lock);
  267. }
  268. EXPORT_SYMBOL(mnt_pin);
  269. void mnt_unpin(struct vfsmount *mnt)
  270. {
  271. spin_lock(&vfsmount_lock);
  272. if (mnt->mnt_pinned) {
  273. atomic_inc(&mnt->mnt_count);
  274. mnt->mnt_pinned--;
  275. }
  276. spin_unlock(&vfsmount_lock);
  277. }
  278. EXPORT_SYMBOL(mnt_unpin);
  279. /* iterator */
  280. static void *m_start(struct seq_file *m, loff_t *pos)
  281. {
  282. struct namespace *n = m->private;
  283. struct list_head *p;
  284. loff_t l = *pos;
  285. down_read(&namespace_sem);
  286. list_for_each(p, &n->list)
  287. if (!l--)
  288. return list_entry(p, struct vfsmount, mnt_list);
  289. return NULL;
  290. }
  291. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  292. {
  293. struct namespace *n = m->private;
  294. struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
  295. (*pos)++;
  296. return p == &n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
  297. }
  298. static void m_stop(struct seq_file *m, void *v)
  299. {
  300. up_read(&namespace_sem);
  301. }
  302. static inline void mangle(struct seq_file *m, const char *s)
  303. {
  304. seq_escape(m, s, " \t\n\\");
  305. }
  306. static int show_vfsmnt(struct seq_file *m, void *v)
  307. {
  308. struct vfsmount *mnt = v;
  309. int err = 0;
  310. static struct proc_fs_info {
  311. int flag;
  312. char *str;
  313. } fs_info[] = {
  314. { MS_SYNCHRONOUS, ",sync" },
  315. { MS_DIRSYNC, ",dirsync" },
  316. { MS_MANDLOCK, ",mand" },
  317. { MS_NOATIME, ",noatime" },
  318. { MS_NODIRATIME, ",nodiratime" },
  319. { 0, NULL }
  320. };
  321. static struct proc_fs_info mnt_info[] = {
  322. { MNT_NOSUID, ",nosuid" },
  323. { MNT_NODEV, ",nodev" },
  324. { MNT_NOEXEC, ",noexec" },
  325. { 0, NULL }
  326. };
  327. struct proc_fs_info *fs_infop;
  328. mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
  329. seq_putc(m, ' ');
  330. seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
  331. seq_putc(m, ' ');
  332. mangle(m, mnt->mnt_sb->s_type->name);
  333. seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
  334. for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
  335. if (mnt->mnt_sb->s_flags & fs_infop->flag)
  336. seq_puts(m, fs_infop->str);
  337. }
  338. for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
  339. if (mnt->mnt_flags & fs_infop->flag)
  340. seq_puts(m, fs_infop->str);
  341. }
  342. if (mnt->mnt_sb->s_op->show_options)
  343. err = mnt->mnt_sb->s_op->show_options(m, mnt);
  344. seq_puts(m, " 0 0\n");
  345. return err;
  346. }
  347. struct seq_operations mounts_op = {
  348. .start = m_start,
  349. .next = m_next,
  350. .stop = m_stop,
  351. .show = show_vfsmnt
  352. };
  353. /**
  354. * may_umount_tree - check if a mount tree is busy
  355. * @mnt: root of mount tree
  356. *
  357. * This is called to check if a tree of mounts has any
  358. * open files, pwds, chroots or sub mounts that are
  359. * busy.
  360. */
  361. int may_umount_tree(struct vfsmount *mnt)
  362. {
  363. int actual_refs = 0;
  364. int minimum_refs = 0;
  365. struct vfsmount *p;
  366. spin_lock(&vfsmount_lock);
  367. for (p = mnt; p; p = next_mnt(p, mnt)) {
  368. actual_refs += atomic_read(&p->mnt_count);
  369. minimum_refs += 2;
  370. }
  371. spin_unlock(&vfsmount_lock);
  372. if (actual_refs > minimum_refs)
  373. return -EBUSY;
  374. return 0;
  375. }
  376. EXPORT_SYMBOL(may_umount_tree);
  377. /**
  378. * may_umount - check if a mount point is busy
  379. * @mnt: root of mount
  380. *
  381. * This is called to check if a mount point has any
  382. * open files, pwds, chroots or sub mounts. If the
  383. * mount has sub mounts this will return busy
  384. * regardless of whether the sub mounts are busy.
  385. *
  386. * Doesn't take quota and stuff into account. IOW, in some cases it will
  387. * give false negatives. The main reason why it's here is that we need
  388. * a non-destructive way to look for easily umountable filesystems.
  389. */
  390. int may_umount(struct vfsmount *mnt)
  391. {
  392. int ret = 0;
  393. spin_lock(&vfsmount_lock);
  394. if (propagate_mount_busy(mnt, 2))
  395. ret = -EBUSY;
  396. spin_unlock(&vfsmount_lock);
  397. return ret;
  398. }
  399. EXPORT_SYMBOL(may_umount);
  400. void release_mounts(struct list_head *head)
  401. {
  402. struct vfsmount *mnt;
  403. while(!list_empty(head)) {
  404. mnt = list_entry(head->next, struct vfsmount, mnt_hash);
  405. list_del_init(&mnt->mnt_hash);
  406. if (mnt->mnt_parent != mnt) {
  407. struct dentry *dentry;
  408. struct vfsmount *m;
  409. spin_lock(&vfsmount_lock);
  410. dentry = mnt->mnt_mountpoint;
  411. m = mnt->mnt_parent;
  412. mnt->mnt_mountpoint = mnt->mnt_root;
  413. mnt->mnt_parent = mnt;
  414. spin_unlock(&vfsmount_lock);
  415. dput(dentry);
  416. mntput(m);
  417. }
  418. mntput(mnt);
  419. }
  420. }
  421. void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
  422. {
  423. struct vfsmount *p;
  424. for (p = mnt; p; p = next_mnt(p, mnt)) {
  425. list_del(&p->mnt_hash);
  426. list_add(&p->mnt_hash, kill);
  427. }
  428. if (propagate)
  429. propagate_umount(kill);
  430. list_for_each_entry(p, kill, mnt_hash) {
  431. list_del_init(&p->mnt_expire);
  432. list_del_init(&p->mnt_list);
  433. __touch_namespace(p->mnt_namespace);
  434. p->mnt_namespace = NULL;
  435. list_del_init(&p->mnt_child);
  436. if (p->mnt_parent != p)
  437. mnt->mnt_mountpoint->d_mounted--;
  438. change_mnt_propagation(p, MS_PRIVATE);
  439. }
  440. }
  441. static int do_umount(struct vfsmount *mnt, int flags)
  442. {
  443. struct super_block *sb = mnt->mnt_sb;
  444. int retval;
  445. LIST_HEAD(umount_list);
  446. retval = security_sb_umount(mnt, flags);
  447. if (retval)
  448. return retval;
  449. /*
  450. * Allow userspace to request a mountpoint be expired rather than
  451. * unmounting unconditionally. Unmount only happens if:
  452. * (1) the mark is already set (the mark is cleared by mntput())
  453. * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
  454. */
  455. if (flags & MNT_EXPIRE) {
  456. if (mnt == current->fs->rootmnt ||
  457. flags & (MNT_FORCE | MNT_DETACH))
  458. return -EINVAL;
  459. if (atomic_read(&mnt->mnt_count) != 2)
  460. return -EBUSY;
  461. if (!xchg(&mnt->mnt_expiry_mark, 1))
  462. return -EAGAIN;
  463. }
  464. /*
  465. * If we may have to abort operations to get out of this
  466. * mount, and they will themselves hold resources we must
  467. * allow the fs to do things. In the Unix tradition of
  468. * 'Gee thats tricky lets do it in userspace' the umount_begin
  469. * might fail to complete on the first run through as other tasks
  470. * must return, and the like. Thats for the mount program to worry
  471. * about for the moment.
  472. */
  473. lock_kernel();
  474. if ((flags & MNT_FORCE) && sb->s_op->umount_begin)
  475. sb->s_op->umount_begin(sb);
  476. unlock_kernel();
  477. /*
  478. * No sense to grab the lock for this test, but test itself looks
  479. * somewhat bogus. Suggestions for better replacement?
  480. * Ho-hum... In principle, we might treat that as umount + switch
  481. * to rootfs. GC would eventually take care of the old vfsmount.
  482. * Actually it makes sense, especially if rootfs would contain a
  483. * /reboot - static binary that would close all descriptors and
  484. * call reboot(9). Then init(8) could umount root and exec /reboot.
  485. */
  486. if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
  487. /*
  488. * Special case for "unmounting" root ...
  489. * we just try to remount it readonly.
  490. */
  491. down_write(&sb->s_umount);
  492. if (!(sb->s_flags & MS_RDONLY)) {
  493. lock_kernel();
  494. DQUOT_OFF(sb);
  495. retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
  496. unlock_kernel();
  497. }
  498. up_write(&sb->s_umount);
  499. return retval;
  500. }
  501. down_write(&namespace_sem);
  502. spin_lock(&vfsmount_lock);
  503. event++;
  504. retval = -EBUSY;
  505. if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
  506. if (!list_empty(&mnt->mnt_list))
  507. umount_tree(mnt, 1, &umount_list);
  508. retval = 0;
  509. }
  510. spin_unlock(&vfsmount_lock);
  511. if (retval)
  512. security_sb_umount_busy(mnt);
  513. up_write(&namespace_sem);
  514. release_mounts(&umount_list);
  515. return retval;
  516. }
  517. /*
  518. * Now umount can handle mount points as well as block devices.
  519. * This is important for filesystems which use unnamed block devices.
  520. *
  521. * We now support a flag for forced unmount like the other 'big iron'
  522. * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
  523. */
  524. asmlinkage long sys_umount(char __user * name, int flags)
  525. {
  526. struct nameidata nd;
  527. int retval;
  528. retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
  529. if (retval)
  530. goto out;
  531. retval = -EINVAL;
  532. if (nd.dentry != nd.mnt->mnt_root)
  533. goto dput_and_out;
  534. if (!check_mnt(nd.mnt))
  535. goto dput_and_out;
  536. retval = -EPERM;
  537. if (!capable(CAP_SYS_ADMIN))
  538. goto dput_and_out;
  539. retval = do_umount(nd.mnt, flags);
  540. dput_and_out:
  541. path_release_on_umount(&nd);
  542. out:
  543. return retval;
  544. }
  545. #ifdef __ARCH_WANT_SYS_OLDUMOUNT
  546. /*
  547. * The 2.0 compatible umount. No flags.
  548. */
  549. asmlinkage long sys_oldumount(char __user * name)
  550. {
  551. return sys_umount(name, 0);
  552. }
  553. #endif
  554. static int mount_is_safe(struct nameidata *nd)
  555. {
  556. if (capable(CAP_SYS_ADMIN))
  557. return 0;
  558. return -EPERM;
  559. #ifdef notyet
  560. if (S_ISLNK(nd->dentry->d_inode->i_mode))
  561. return -EPERM;
  562. if (nd->dentry->d_inode->i_mode & S_ISVTX) {
  563. if (current->uid != nd->dentry->d_inode->i_uid)
  564. return -EPERM;
  565. }
  566. if (vfs_permission(nd, MAY_WRITE))
  567. return -EPERM;
  568. return 0;
  569. #endif
  570. }
  571. static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
  572. {
  573. while (1) {
  574. if (d == dentry)
  575. return 1;
  576. if (d == NULL || d == d->d_parent)
  577. return 0;
  578. d = d->d_parent;
  579. }
  580. }
  581. struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
  582. int flag)
  583. {
  584. struct vfsmount *res, *p, *q, *r, *s;
  585. struct nameidata nd;
  586. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
  587. return NULL;
  588. res = q = clone_mnt(mnt, dentry, flag);
  589. if (!q)
  590. goto Enomem;
  591. q->mnt_mountpoint = mnt->mnt_mountpoint;
  592. p = mnt;
  593. list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
  594. if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
  595. continue;
  596. for (s = r; s; s = next_mnt(s, r)) {
  597. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
  598. s = skip_mnt_tree(s);
  599. continue;
  600. }
  601. while (p != s->mnt_parent) {
  602. p = p->mnt_parent;
  603. q = q->mnt_parent;
  604. }
  605. p = s;
  606. nd.mnt = q;
  607. nd.dentry = p->mnt_mountpoint;
  608. q = clone_mnt(p, p->mnt_root, flag);
  609. if (!q)
  610. goto Enomem;
  611. spin_lock(&vfsmount_lock);
  612. list_add_tail(&q->mnt_list, &res->mnt_list);
  613. attach_mnt(q, &nd);
  614. spin_unlock(&vfsmount_lock);
  615. }
  616. }
  617. return res;
  618. Enomem:
  619. if (res) {
  620. LIST_HEAD(umount_list);
  621. spin_lock(&vfsmount_lock);
  622. umount_tree(res, 0, &umount_list);
  623. spin_unlock(&vfsmount_lock);
  624. release_mounts(&umount_list);
  625. }
  626. return NULL;
  627. }
  628. /*
  629. * @source_mnt : mount tree to be attached
  630. * @nd : place the mount tree @source_mnt is attached
  631. * @parent_nd : if non-null, detach the source_mnt from its parent and
  632. * store the parent mount and mountpoint dentry.
  633. * (done when source_mnt is moved)
  634. *
  635. * NOTE: in the table below explains the semantics when a source mount
  636. * of a given type is attached to a destination mount of a given type.
  637. * ---------------------------------------------------------------------------
  638. * | BIND MOUNT OPERATION |
  639. * |**************************************************************************
  640. * | source-->| shared | private | slave | unbindable |
  641. * | dest | | | | |
  642. * | | | | | | |
  643. * | v | | | | |
  644. * |**************************************************************************
  645. * | shared | shared (++) | shared (+) | shared(+++)| invalid |
  646. * | | | | | |
  647. * |non-shared| shared (+) | private | slave (*) | invalid |
  648. * ***************************************************************************
  649. * A bind operation clones the source mount and mounts the clone on the
  650. * destination mount.
  651. *
  652. * (++) the cloned mount is propagated to all the mounts in the propagation
  653. * tree of the destination mount and the cloned mount is added to
  654. * the peer group of the source mount.
  655. * (+) the cloned mount is created under the destination mount and is marked
  656. * as shared. The cloned mount is added to the peer group of the source
  657. * mount.
  658. * (+++) the mount is propagated to all the mounts in the propagation tree
  659. * of the destination mount and the cloned mount is made slave
  660. * of the same master as that of the source mount. The cloned mount
  661. * is marked as 'shared and slave'.
  662. * (*) the cloned mount is made a slave of the same master as that of the
  663. * source mount.
  664. *
  665. * ---------------------------------------------------------------------------
  666. * | MOVE MOUNT OPERATION |
  667. * |**************************************************************************
  668. * | source-->| shared | private | slave | unbindable |
  669. * | dest | | | | |
  670. * | | | | | | |
  671. * | v | | | | |
  672. * |**************************************************************************
  673. * | shared | shared (+) | shared (+) | shared(+++) | invalid |
  674. * | | | | | |
  675. * |non-shared| shared (+*) | private | slave (*) | unbindable |
  676. * ***************************************************************************
  677. *
  678. * (+) the mount is moved to the destination. And is then propagated to
  679. * all the mounts in the propagation tree of the destination mount.
  680. * (+*) the mount is moved to the destination.
  681. * (+++) the mount is moved to the destination and is then propagated to
  682. * all the mounts belonging to the destination mount's propagation tree.
  683. * the mount is marked as 'shared and slave'.
  684. * (*) the mount continues to be a slave at the new location.
  685. *
  686. * if the source mount is a tree, the operations explained above is
  687. * applied to each mount in the tree.
  688. * Must be called without spinlocks held, since this function can sleep
  689. * in allocations.
  690. */
  691. static int attach_recursive_mnt(struct vfsmount *source_mnt,
  692. struct nameidata *nd, struct nameidata *parent_nd)
  693. {
  694. LIST_HEAD(tree_list);
  695. struct vfsmount *dest_mnt = nd->mnt;
  696. struct dentry *dest_dentry = nd->dentry;
  697. struct vfsmount *child, *p;
  698. if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list))
  699. return -EINVAL;
  700. if (IS_MNT_SHARED(dest_mnt)) {
  701. for (p = source_mnt; p; p = next_mnt(p, source_mnt))
  702. set_mnt_shared(p);
  703. }
  704. spin_lock(&vfsmount_lock);
  705. if (parent_nd) {
  706. detach_mnt(source_mnt, parent_nd);
  707. attach_mnt(source_mnt, nd);
  708. touch_namespace(current->namespace);
  709. } else {
  710. mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
  711. commit_tree(source_mnt);
  712. }
  713. list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
  714. list_del_init(&child->mnt_hash);
  715. commit_tree(child);
  716. }
  717. spin_unlock(&vfsmount_lock);
  718. return 0;
  719. }
  720. static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
  721. {
  722. int err;
  723. if (mnt->mnt_sb->s_flags & MS_NOUSER)
  724. return -EINVAL;
  725. if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
  726. S_ISDIR(mnt->mnt_root->d_inode->i_mode))
  727. return -ENOTDIR;
  728. err = -ENOENT;
  729. down(&nd->dentry->d_inode->i_sem);
  730. if (IS_DEADDIR(nd->dentry->d_inode))
  731. goto out_unlock;
  732. err = security_sb_check_sb(mnt, nd);
  733. if (err)
  734. goto out_unlock;
  735. err = -ENOENT;
  736. if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry))
  737. err = attach_recursive_mnt(mnt, nd, NULL);
  738. out_unlock:
  739. up(&nd->dentry->d_inode->i_sem);
  740. if (!err)
  741. security_sb_post_addmount(mnt, nd);
  742. return err;
  743. }
  744. /*
  745. * recursively change the type of the mountpoint.
  746. */
  747. static int do_change_type(struct nameidata *nd, int flag)
  748. {
  749. struct vfsmount *m, *mnt = nd->mnt;
  750. int recurse = flag & MS_REC;
  751. int type = flag & ~MS_REC;
  752. if (nd->dentry != nd->mnt->mnt_root)
  753. return -EINVAL;
  754. down_write(&namespace_sem);
  755. spin_lock(&vfsmount_lock);
  756. for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
  757. change_mnt_propagation(m, type);
  758. spin_unlock(&vfsmount_lock);
  759. up_write(&namespace_sem);
  760. return 0;
  761. }
  762. /*
  763. * do loopback mount.
  764. */
  765. static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
  766. {
  767. struct nameidata old_nd;
  768. struct vfsmount *mnt = NULL;
  769. int err = mount_is_safe(nd);
  770. if (err)
  771. return err;
  772. if (!old_name || !*old_name)
  773. return -EINVAL;
  774. err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
  775. if (err)
  776. return err;
  777. down_write(&namespace_sem);
  778. err = -EINVAL;
  779. if (IS_MNT_UNBINDABLE(old_nd.mnt))
  780. goto out;
  781. if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
  782. goto out;
  783. err = -ENOMEM;
  784. if (recurse)
  785. mnt = copy_tree(old_nd.mnt, old_nd.dentry, 0);
  786. else
  787. mnt = clone_mnt(old_nd.mnt, old_nd.dentry, 0);
  788. if (!mnt)
  789. goto out;
  790. err = graft_tree(mnt, nd);
  791. if (err) {
  792. LIST_HEAD(umount_list);
  793. spin_lock(&vfsmount_lock);
  794. umount_tree(mnt, 0, &umount_list);
  795. spin_unlock(&vfsmount_lock);
  796. release_mounts(&umount_list);
  797. }
  798. out:
  799. up_write(&namespace_sem);
  800. path_release(&old_nd);
  801. return err;
  802. }
  803. /*
  804. * change filesystem flags. dir should be a physical root of filesystem.
  805. * If you've mounted a non-root directory somewhere and want to do remount
  806. * on it - tough luck.
  807. */
  808. static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
  809. void *data)
  810. {
  811. int err;
  812. struct super_block *sb = nd->mnt->mnt_sb;
  813. if (!capable(CAP_SYS_ADMIN))
  814. return -EPERM;
  815. if (!check_mnt(nd->mnt))
  816. return -EINVAL;
  817. if (nd->dentry != nd->mnt->mnt_root)
  818. return -EINVAL;
  819. down_write(&sb->s_umount);
  820. err = do_remount_sb(sb, flags, data, 0);
  821. if (!err)
  822. nd->mnt->mnt_flags = mnt_flags;
  823. up_write(&sb->s_umount);
  824. if (!err)
  825. security_sb_post_remount(nd->mnt, flags, data);
  826. return err;
  827. }
  828. static inline int tree_contains_unbindable(struct vfsmount *mnt)
  829. {
  830. struct vfsmount *p;
  831. for (p = mnt; p; p = next_mnt(p, mnt)) {
  832. if (IS_MNT_UNBINDABLE(p))
  833. return 1;
  834. }
  835. return 0;
  836. }
  837. static int do_move_mount(struct nameidata *nd, char *old_name)
  838. {
  839. struct nameidata old_nd, parent_nd;
  840. struct vfsmount *p;
  841. int err = 0;
  842. if (!capable(CAP_SYS_ADMIN))
  843. return -EPERM;
  844. if (!old_name || !*old_name)
  845. return -EINVAL;
  846. err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
  847. if (err)
  848. return err;
  849. down_write(&namespace_sem);
  850. while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
  851. ;
  852. err = -EINVAL;
  853. if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
  854. goto out;
  855. err = -ENOENT;
  856. down(&nd->dentry->d_inode->i_sem);
  857. if (IS_DEADDIR(nd->dentry->d_inode))
  858. goto out1;
  859. if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
  860. goto out1;
  861. err = -EINVAL;
  862. if (old_nd.dentry != old_nd.mnt->mnt_root)
  863. goto out1;
  864. if (old_nd.mnt == old_nd.mnt->mnt_parent)
  865. goto out1;
  866. if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
  867. S_ISDIR(old_nd.dentry->d_inode->i_mode))
  868. goto out1;
  869. /*
  870. * Don't move a mount residing in a shared parent.
  871. */
  872. if (old_nd.mnt->mnt_parent && IS_MNT_SHARED(old_nd.mnt->mnt_parent))
  873. goto out1;
  874. /*
  875. * Don't move a mount tree containing unbindable mounts to a destination
  876. * mount which is shared.
  877. */
  878. if (IS_MNT_SHARED(nd->mnt) && tree_contains_unbindable(old_nd.mnt))
  879. goto out1;
  880. err = -ELOOP;
  881. for (p = nd->mnt; p->mnt_parent != p; p = p->mnt_parent)
  882. if (p == old_nd.mnt)
  883. goto out1;
  884. if ((err = attach_recursive_mnt(old_nd.mnt, nd, &parent_nd)))
  885. goto out1;
  886. spin_lock(&vfsmount_lock);
  887. /* if the mount is moved, it should no longer be expire
  888. * automatically */
  889. list_del_init(&old_nd.mnt->mnt_expire);
  890. spin_unlock(&vfsmount_lock);
  891. out1:
  892. up(&nd->dentry->d_inode->i_sem);
  893. out:
  894. up_write(&namespace_sem);
  895. if (!err)
  896. path_release(&parent_nd);
  897. path_release(&old_nd);
  898. return err;
  899. }
  900. /*
  901. * create a new mount for userspace and request it to be added into the
  902. * namespace's tree
  903. */
  904. static int do_new_mount(struct nameidata *nd, char *type, int flags,
  905. int mnt_flags, char *name, void *data)
  906. {
  907. struct vfsmount *mnt;
  908. if (!type || !memchr(type, 0, PAGE_SIZE))
  909. return -EINVAL;
  910. /* we need capabilities... */
  911. if (!capable(CAP_SYS_ADMIN))
  912. return -EPERM;
  913. mnt = do_kern_mount(type, flags, name, data);
  914. if (IS_ERR(mnt))
  915. return PTR_ERR(mnt);
  916. return do_add_mount(mnt, nd, mnt_flags, NULL);
  917. }
  918. /*
  919. * add a mount into a namespace's mount tree
  920. * - provide the option of adding the new mount to an expiration list
  921. */
  922. int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
  923. int mnt_flags, struct list_head *fslist)
  924. {
  925. int err;
  926. down_write(&namespace_sem);
  927. /* Something was mounted here while we slept */
  928. while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
  929. ;
  930. err = -EINVAL;
  931. if (!check_mnt(nd->mnt))
  932. goto unlock;
  933. /* Refuse the same filesystem on the same mount point */
  934. err = -EBUSY;
  935. if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
  936. nd->mnt->mnt_root == nd->dentry)
  937. goto unlock;
  938. err = -EINVAL;
  939. if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
  940. goto unlock;
  941. newmnt->mnt_flags = mnt_flags;
  942. if ((err = graft_tree(newmnt, nd)))
  943. goto unlock;
  944. if (fslist) {
  945. /* add to the specified expiration list */
  946. spin_lock(&vfsmount_lock);
  947. list_add_tail(&newmnt->mnt_expire, fslist);
  948. spin_unlock(&vfsmount_lock);
  949. }
  950. up_write(&namespace_sem);
  951. return 0;
  952. unlock:
  953. up_write(&namespace_sem);
  954. mntput(newmnt);
  955. return err;
  956. }
  957. EXPORT_SYMBOL_GPL(do_add_mount);
  958. static void expire_mount(struct vfsmount *mnt, struct list_head *mounts,
  959. struct list_head *umounts)
  960. {
  961. spin_lock(&vfsmount_lock);
  962. /*
  963. * Check if mount is still attached, if not, let whoever holds it deal
  964. * with the sucker
  965. */
  966. if (mnt->mnt_parent == mnt) {
  967. spin_unlock(&vfsmount_lock);
  968. return;
  969. }
  970. /*
  971. * Check that it is still dead: the count should now be 2 - as
  972. * contributed by the vfsmount parent and the mntget above
  973. */
  974. if (!propagate_mount_busy(mnt, 2)) {
  975. /* delete from the namespace */
  976. touch_namespace(mnt->mnt_namespace);
  977. list_del_init(&mnt->mnt_list);
  978. mnt->mnt_namespace = NULL;
  979. umount_tree(mnt, 1, umounts);
  980. spin_unlock(&vfsmount_lock);
  981. } else {
  982. /*
  983. * Someone brought it back to life whilst we didn't have any
  984. * locks held so return it to the expiration list
  985. */
  986. list_add_tail(&mnt->mnt_expire, mounts);
  987. spin_unlock(&vfsmount_lock);
  988. }
  989. }
  990. /*
  991. * process a list of expirable mountpoints with the intent of discarding any
  992. * mountpoints that aren't in use and haven't been touched since last we came
  993. * here
  994. */
  995. void mark_mounts_for_expiry(struct list_head *mounts)
  996. {
  997. struct namespace *namespace;
  998. struct vfsmount *mnt, *next;
  999. LIST_HEAD(graveyard);
  1000. if (list_empty(mounts))
  1001. return;
  1002. spin_lock(&vfsmount_lock);
  1003. /* extract from the expiration list every vfsmount that matches the
  1004. * following criteria:
  1005. * - only referenced by its parent vfsmount
  1006. * - still marked for expiry (marked on the last call here; marks are
  1007. * cleared by mntput())
  1008. */
  1009. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  1010. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  1011. atomic_read(&mnt->mnt_count) != 1)
  1012. continue;
  1013. mntget(mnt);
  1014. list_move(&mnt->mnt_expire, &graveyard);
  1015. }
  1016. /*
  1017. * go through the vfsmounts we've just consigned to the graveyard to
  1018. * - check that they're still dead
  1019. * - delete the vfsmount from the appropriate namespace under lock
  1020. * - dispose of the corpse
  1021. */
  1022. while (!list_empty(&graveyard)) {
  1023. LIST_HEAD(umounts);
  1024. mnt = list_entry(graveyard.next, struct vfsmount, mnt_expire);
  1025. list_del_init(&mnt->mnt_expire);
  1026. /* don't do anything if the namespace is dead - all the
  1027. * vfsmounts from it are going away anyway */
  1028. namespace = mnt->mnt_namespace;
  1029. if (!namespace || !namespace->root)
  1030. continue;
  1031. get_namespace(namespace);
  1032. spin_unlock(&vfsmount_lock);
  1033. down_write(&namespace_sem);
  1034. expire_mount(mnt, mounts, &umounts);
  1035. up_write(&namespace_sem);
  1036. release_mounts(&umounts);
  1037. mntput(mnt);
  1038. put_namespace(namespace);
  1039. spin_lock(&vfsmount_lock);
  1040. }
  1041. spin_unlock(&vfsmount_lock);
  1042. }
  1043. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  1044. /*
  1045. * Some copy_from_user() implementations do not return the exact number of
  1046. * bytes remaining to copy on a fault. But copy_mount_options() requires that.
  1047. * Note that this function differs from copy_from_user() in that it will oops
  1048. * on bad values of `to', rather than returning a short copy.
  1049. */
  1050. static long exact_copy_from_user(void *to, const void __user * from,
  1051. unsigned long n)
  1052. {
  1053. char *t = to;
  1054. const char __user *f = from;
  1055. char c;
  1056. if (!access_ok(VERIFY_READ, from, n))
  1057. return n;
  1058. while (n) {
  1059. if (__get_user(c, f)) {
  1060. memset(t, 0, n);
  1061. break;
  1062. }
  1063. *t++ = c;
  1064. f++;
  1065. n--;
  1066. }
  1067. return n;
  1068. }
  1069. int copy_mount_options(const void __user * data, unsigned long *where)
  1070. {
  1071. int i;
  1072. unsigned long page;
  1073. unsigned long size;
  1074. *where = 0;
  1075. if (!data)
  1076. return 0;
  1077. if (!(page = __get_free_page(GFP_KERNEL)))
  1078. return -ENOMEM;
  1079. /* We only care that *some* data at the address the user
  1080. * gave us is valid. Just in case, we'll zero
  1081. * the remainder of the page.
  1082. */
  1083. /* copy_from_user cannot cross TASK_SIZE ! */
  1084. size = TASK_SIZE - (unsigned long)data;
  1085. if (size > PAGE_SIZE)
  1086. size = PAGE_SIZE;
  1087. i = size - exact_copy_from_user((void *)page, data, size);
  1088. if (!i) {
  1089. free_page(page);
  1090. return -EFAULT;
  1091. }
  1092. if (i != PAGE_SIZE)
  1093. memset((char *)page + i, 0, PAGE_SIZE - i);
  1094. *where = page;
  1095. return 0;
  1096. }
  1097. /*
  1098. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  1099. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  1100. *
  1101. * data is a (void *) that can point to any structure up to
  1102. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  1103. * information (or be NULL).
  1104. *
  1105. * Pre-0.97 versions of mount() didn't have a flags word.
  1106. * When the flags word was introduced its top half was required
  1107. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  1108. * Therefore, if this magic number is present, it carries no information
  1109. * and must be discarded.
  1110. */
  1111. long do_mount(char *dev_name, char *dir_name, char *type_page,
  1112. unsigned long flags, void *data_page)
  1113. {
  1114. struct nameidata nd;
  1115. int retval = 0;
  1116. int mnt_flags = 0;
  1117. /* Discard magic */
  1118. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  1119. flags &= ~MS_MGC_MSK;
  1120. /* Basic sanity checks */
  1121. if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
  1122. return -EINVAL;
  1123. if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
  1124. return -EINVAL;
  1125. if (data_page)
  1126. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  1127. /* Separate the per-mountpoint flags */
  1128. if (flags & MS_NOSUID)
  1129. mnt_flags |= MNT_NOSUID;
  1130. if (flags & MS_NODEV)
  1131. mnt_flags |= MNT_NODEV;
  1132. if (flags & MS_NOEXEC)
  1133. mnt_flags |= MNT_NOEXEC;
  1134. flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE);
  1135. /* ... and get the mountpoint */
  1136. retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
  1137. if (retval)
  1138. return retval;
  1139. retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
  1140. if (retval)
  1141. goto dput_out;
  1142. if (flags & MS_REMOUNT)
  1143. retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
  1144. data_page);
  1145. else if (flags & MS_BIND)
  1146. retval = do_loopback(&nd, dev_name, flags & MS_REC);
  1147. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1148. retval = do_change_type(&nd, flags);
  1149. else if (flags & MS_MOVE)
  1150. retval = do_move_mount(&nd, dev_name);
  1151. else
  1152. retval = do_new_mount(&nd, type_page, flags, mnt_flags,
  1153. dev_name, data_page);
  1154. dput_out:
  1155. path_release(&nd);
  1156. return retval;
  1157. }
  1158. int copy_namespace(int flags, struct task_struct *tsk)
  1159. {
  1160. struct namespace *namespace = tsk->namespace;
  1161. struct namespace *new_ns;
  1162. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
  1163. struct fs_struct *fs = tsk->fs;
  1164. struct vfsmount *p, *q;
  1165. if (!namespace)
  1166. return 0;
  1167. get_namespace(namespace);
  1168. if (!(flags & CLONE_NEWNS))
  1169. return 0;
  1170. if (!capable(CAP_SYS_ADMIN)) {
  1171. put_namespace(namespace);
  1172. return -EPERM;
  1173. }
  1174. new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
  1175. if (!new_ns)
  1176. goto out;
  1177. atomic_set(&new_ns->count, 1);
  1178. INIT_LIST_HEAD(&new_ns->list);
  1179. init_waitqueue_head(&new_ns->poll);
  1180. new_ns->event = 0;
  1181. down_write(&namespace_sem);
  1182. /* First pass: copy the tree topology */
  1183. new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root,
  1184. CL_COPY_ALL | CL_EXPIRE);
  1185. if (!new_ns->root) {
  1186. up_write(&namespace_sem);
  1187. kfree(new_ns);
  1188. goto out;
  1189. }
  1190. spin_lock(&vfsmount_lock);
  1191. list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
  1192. spin_unlock(&vfsmount_lock);
  1193. /*
  1194. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  1195. * as belonging to new namespace. We have already acquired a private
  1196. * fs_struct, so tsk->fs->lock is not needed.
  1197. */
  1198. p = namespace->root;
  1199. q = new_ns->root;
  1200. while (p) {
  1201. q->mnt_namespace = new_ns;
  1202. if (fs) {
  1203. if (p == fs->rootmnt) {
  1204. rootmnt = p;
  1205. fs->rootmnt = mntget(q);
  1206. }
  1207. if (p == fs->pwdmnt) {
  1208. pwdmnt = p;
  1209. fs->pwdmnt = mntget(q);
  1210. }
  1211. if (p == fs->altrootmnt) {
  1212. altrootmnt = p;
  1213. fs->altrootmnt = mntget(q);
  1214. }
  1215. }
  1216. p = next_mnt(p, namespace->root);
  1217. q = next_mnt(q, new_ns->root);
  1218. }
  1219. up_write(&namespace_sem);
  1220. tsk->namespace = new_ns;
  1221. if (rootmnt)
  1222. mntput(rootmnt);
  1223. if (pwdmnt)
  1224. mntput(pwdmnt);
  1225. if (altrootmnt)
  1226. mntput(altrootmnt);
  1227. put_namespace(namespace);
  1228. return 0;
  1229. out:
  1230. put_namespace(namespace);
  1231. return -ENOMEM;
  1232. }
  1233. asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
  1234. char __user * type, unsigned long flags,
  1235. void __user * data)
  1236. {
  1237. int retval;
  1238. unsigned long data_page;
  1239. unsigned long type_page;
  1240. unsigned long dev_page;
  1241. char *dir_page;
  1242. retval = copy_mount_options(type, &type_page);
  1243. if (retval < 0)
  1244. return retval;
  1245. dir_page = getname(dir_name);
  1246. retval = PTR_ERR(dir_page);
  1247. if (IS_ERR(dir_page))
  1248. goto out1;
  1249. retval = copy_mount_options(dev_name, &dev_page);
  1250. if (retval < 0)
  1251. goto out2;
  1252. retval = copy_mount_options(data, &data_page);
  1253. if (retval < 0)
  1254. goto out3;
  1255. lock_kernel();
  1256. retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
  1257. flags, (void *)data_page);
  1258. unlock_kernel();
  1259. free_page(data_page);
  1260. out3:
  1261. free_page(dev_page);
  1262. out2:
  1263. putname(dir_page);
  1264. out1:
  1265. free_page(type_page);
  1266. return retval;
  1267. }
  1268. /*
  1269. * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
  1270. * It can block. Requires the big lock held.
  1271. */
  1272. void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
  1273. struct dentry *dentry)
  1274. {
  1275. struct dentry *old_root;
  1276. struct vfsmount *old_rootmnt;
  1277. write_lock(&fs->lock);
  1278. old_root = fs->root;
  1279. old_rootmnt = fs->rootmnt;
  1280. fs->rootmnt = mntget(mnt);
  1281. fs->root = dget(dentry);
  1282. write_unlock(&fs->lock);
  1283. if (old_root) {
  1284. dput(old_root);
  1285. mntput(old_rootmnt);
  1286. }
  1287. }
  1288. /*
  1289. * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
  1290. * It can block. Requires the big lock held.
  1291. */
  1292. void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
  1293. struct dentry *dentry)
  1294. {
  1295. struct dentry *old_pwd;
  1296. struct vfsmount *old_pwdmnt;
  1297. write_lock(&fs->lock);
  1298. old_pwd = fs->pwd;
  1299. old_pwdmnt = fs->pwdmnt;
  1300. fs->pwdmnt = mntget(mnt);
  1301. fs->pwd = dget(dentry);
  1302. write_unlock(&fs->lock);
  1303. if (old_pwd) {
  1304. dput(old_pwd);
  1305. mntput(old_pwdmnt);
  1306. }
  1307. }
  1308. static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
  1309. {
  1310. struct task_struct *g, *p;
  1311. struct fs_struct *fs;
  1312. read_lock(&tasklist_lock);
  1313. do_each_thread(g, p) {
  1314. task_lock(p);
  1315. fs = p->fs;
  1316. if (fs) {
  1317. atomic_inc(&fs->count);
  1318. task_unlock(p);
  1319. if (fs->root == old_nd->dentry
  1320. && fs->rootmnt == old_nd->mnt)
  1321. set_fs_root(fs, new_nd->mnt, new_nd->dentry);
  1322. if (fs->pwd == old_nd->dentry
  1323. && fs->pwdmnt == old_nd->mnt)
  1324. set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
  1325. put_fs_struct(fs);
  1326. } else
  1327. task_unlock(p);
  1328. } while_each_thread(g, p);
  1329. read_unlock(&tasklist_lock);
  1330. }
  1331. /*
  1332. * pivot_root Semantics:
  1333. * Moves the root file system of the current process to the directory put_old,
  1334. * makes new_root as the new root file system of the current process, and sets
  1335. * root/cwd of all processes which had them on the current root to new_root.
  1336. *
  1337. * Restrictions:
  1338. * The new_root and put_old must be directories, and must not be on the
  1339. * same file system as the current process root. The put_old must be
  1340. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  1341. * pointed to by put_old must yield the same directory as new_root. No other
  1342. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  1343. *
  1344. * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
  1345. * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
  1346. * in this situation.
  1347. *
  1348. * Notes:
  1349. * - we don't move root/cwd if they are not at the root (reason: if something
  1350. * cared enough to change them, it's probably wrong to force them elsewhere)
  1351. * - it's okay to pick a root that isn't the root of a file system, e.g.
  1352. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  1353. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  1354. * first.
  1355. */
  1356. asmlinkage long sys_pivot_root(const char __user * new_root,
  1357. const char __user * put_old)
  1358. {
  1359. struct vfsmount *tmp;
  1360. struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
  1361. int error;
  1362. if (!capable(CAP_SYS_ADMIN))
  1363. return -EPERM;
  1364. lock_kernel();
  1365. error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
  1366. &new_nd);
  1367. if (error)
  1368. goto out0;
  1369. error = -EINVAL;
  1370. if (!check_mnt(new_nd.mnt))
  1371. goto out1;
  1372. error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
  1373. if (error)
  1374. goto out1;
  1375. error = security_sb_pivotroot(&old_nd, &new_nd);
  1376. if (error) {
  1377. path_release(&old_nd);
  1378. goto out1;
  1379. }
  1380. read_lock(&current->fs->lock);
  1381. user_nd.mnt = mntget(current->fs->rootmnt);
  1382. user_nd.dentry = dget(current->fs->root);
  1383. read_unlock(&current->fs->lock);
  1384. down_write(&namespace_sem);
  1385. down(&old_nd.dentry->d_inode->i_sem);
  1386. error = -EINVAL;
  1387. if (IS_MNT_SHARED(old_nd.mnt) ||
  1388. IS_MNT_SHARED(new_nd.mnt->mnt_parent) ||
  1389. IS_MNT_SHARED(user_nd.mnt->mnt_parent))
  1390. goto out2;
  1391. if (!check_mnt(user_nd.mnt))
  1392. goto out2;
  1393. error = -ENOENT;
  1394. if (IS_DEADDIR(new_nd.dentry->d_inode))
  1395. goto out2;
  1396. if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
  1397. goto out2;
  1398. if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
  1399. goto out2;
  1400. error = -EBUSY;
  1401. if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
  1402. goto out2; /* loop, on the same file system */
  1403. error = -EINVAL;
  1404. if (user_nd.mnt->mnt_root != user_nd.dentry)
  1405. goto out2; /* not a mountpoint */
  1406. if (user_nd.mnt->mnt_parent == user_nd.mnt)
  1407. goto out2; /* not attached */
  1408. if (new_nd.mnt->mnt_root != new_nd.dentry)
  1409. goto out2; /* not a mountpoint */
  1410. if (new_nd.mnt->mnt_parent == new_nd.mnt)
  1411. goto out2; /* not attached */
  1412. tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
  1413. spin_lock(&vfsmount_lock);
  1414. if (tmp != new_nd.mnt) {
  1415. for (;;) {
  1416. if (tmp->mnt_parent == tmp)
  1417. goto out3; /* already mounted on put_old */
  1418. if (tmp->mnt_parent == new_nd.mnt)
  1419. break;
  1420. tmp = tmp->mnt_parent;
  1421. }
  1422. if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
  1423. goto out3;
  1424. } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
  1425. goto out3;
  1426. detach_mnt(new_nd.mnt, &parent_nd);
  1427. detach_mnt(user_nd.mnt, &root_parent);
  1428. attach_mnt(user_nd.mnt, &old_nd); /* mount old root on put_old */
  1429. attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */
  1430. touch_namespace(current->namespace);
  1431. spin_unlock(&vfsmount_lock);
  1432. chroot_fs_refs(&user_nd, &new_nd);
  1433. security_sb_post_pivotroot(&user_nd, &new_nd);
  1434. error = 0;
  1435. path_release(&root_parent);
  1436. path_release(&parent_nd);
  1437. out2:
  1438. up(&old_nd.dentry->d_inode->i_sem);
  1439. up_write(&namespace_sem);
  1440. path_release(&user_nd);
  1441. path_release(&old_nd);
  1442. out1:
  1443. path_release(&new_nd);
  1444. out0:
  1445. unlock_kernel();
  1446. return error;
  1447. out3:
  1448. spin_unlock(&vfsmount_lock);
  1449. goto out2;
  1450. }
  1451. static void __init init_mount_tree(void)
  1452. {
  1453. struct vfsmount *mnt;
  1454. struct namespace *namespace;
  1455. struct task_struct *g, *p;
  1456. mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
  1457. if (IS_ERR(mnt))
  1458. panic("Can't create rootfs");
  1459. namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
  1460. if (!namespace)
  1461. panic("Can't allocate initial namespace");
  1462. atomic_set(&namespace->count, 1);
  1463. INIT_LIST_HEAD(&namespace->list);
  1464. init_waitqueue_head(&namespace->poll);
  1465. namespace->event = 0;
  1466. list_add(&mnt->mnt_list, &namespace->list);
  1467. namespace->root = mnt;
  1468. mnt->mnt_namespace = namespace;
  1469. init_task.namespace = namespace;
  1470. read_lock(&tasklist_lock);
  1471. do_each_thread(g, p) {
  1472. get_namespace(namespace);
  1473. p->namespace = namespace;
  1474. } while_each_thread(g, p);
  1475. read_unlock(&tasklist_lock);
  1476. set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
  1477. set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
  1478. }
  1479. void __init mnt_init(unsigned long mempages)
  1480. {
  1481. struct list_head *d;
  1482. unsigned int nr_hash;
  1483. int i;
  1484. init_rwsem(&namespace_sem);
  1485. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
  1486. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL, NULL);
  1487. mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
  1488. if (!mount_hashtable)
  1489. panic("Failed to allocate mount hash table\n");
  1490. /*
  1491. * Find the power-of-two list-heads that can fit into the allocation..
  1492. * We don't guarantee that "sizeof(struct list_head)" is necessarily
  1493. * a power-of-two.
  1494. */
  1495. nr_hash = PAGE_SIZE / sizeof(struct list_head);
  1496. hash_bits = 0;
  1497. do {
  1498. hash_bits++;
  1499. } while ((nr_hash >> hash_bits) != 0);
  1500. hash_bits--;
  1501. /*
  1502. * Re-calculate the actual number of entries and the mask
  1503. * from the number of bits we can fit.
  1504. */
  1505. nr_hash = 1UL << hash_bits;
  1506. hash_mask = nr_hash - 1;
  1507. printk("Mount-cache hash table entries: %d\n", nr_hash);
  1508. /* And initialize the newly allocated array */
  1509. d = mount_hashtable;
  1510. i = nr_hash;
  1511. do {
  1512. INIT_LIST_HEAD(d);
  1513. d++;
  1514. i--;
  1515. } while (i);
  1516. sysfs_init();
  1517. init_rootfs();
  1518. init_mount_tree();
  1519. }
  1520. void __put_namespace(struct namespace *namespace)
  1521. {
  1522. struct vfsmount *root = namespace->root;
  1523. LIST_HEAD(umount_list);
  1524. namespace->root = NULL;
  1525. spin_unlock(&vfsmount_lock);
  1526. down_write(&namespace_sem);
  1527. spin_lock(&vfsmount_lock);
  1528. umount_tree(root, 0, &umount_list);
  1529. spin_unlock(&vfsmount_lock);
  1530. up_write(&namespace_sem);
  1531. release_mounts(&umount_list);
  1532. kfree(namespace);
  1533. }