4 * (C) Copyright Al Viro 2000, 2001
5 * Released under GPL v2.
7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
11 #include <linux/config.h>
12 #include <linux/syscalls.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/smp_lock.h>
16 #include <linux/init.h>
17 #include <linux/quotaops.h>
18 #include <linux/acct.h>
19 #include <linux/module.h>
20 #include <linux/seq_file.h>
21 #include <linux/namespace.h>
22 #include <linux/namei.h>
23 #include <linux/security.h>
24 #include <linux/mount.h>
25 #include <asm/uaccess.h>
26 #include <asm/unistd.h>
29 extern int __init init_rootfs(void);
32 extern int __init sysfs_init(void);
34 static inline int sysfs_init(void)
40 /* spinlock for vfsmount related operations, inplace of dcache_lock */
41 __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
45 static struct list_head *mount_hashtable;
46 static int hash_mask __read_mostly, hash_bits __read_mostly;
47 static kmem_cache_t *mnt_cache;
48 static struct rw_semaphore namespace_sem;
50 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
52 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
53 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
54 tmp = tmp + (tmp >> hash_bits);
55 return tmp & hash_mask;
58 struct vfsmount *alloc_vfsmnt(const char *name)
60 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
62 memset(mnt, 0, sizeof(struct vfsmount));
63 atomic_set(&mnt->mnt_count, 1);
64 INIT_LIST_HEAD(&mnt->mnt_hash);
65 INIT_LIST_HEAD(&mnt->mnt_child);
66 INIT_LIST_HEAD(&mnt->mnt_mounts);
67 INIT_LIST_HEAD(&mnt->mnt_list);
68 INIT_LIST_HEAD(&mnt->mnt_expire);
69 INIT_LIST_HEAD(&mnt->mnt_share);
71 int size = strlen(name) + 1;
72 char *newname = kmalloc(size, GFP_KERNEL);
74 memcpy(newname, name, size);
75 mnt->mnt_devname = newname;
82 void free_vfsmnt(struct vfsmount *mnt)
84 kfree(mnt->mnt_devname);
85 kmem_cache_free(mnt_cache, mnt);
89 * find the first or last mount at @dentry on vfsmount @mnt depending on
90 * @dir. If @dir is set return the first mount else return the last mount.
92 struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
95 struct list_head *head = mount_hashtable + hash(mnt, dentry);
96 struct list_head *tmp = head;
97 struct vfsmount *p, *found = NULL;
100 tmp = dir ? tmp->next : tmp->prev;
104 p = list_entry(tmp, struct vfsmount, mnt_hash);
105 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
114 * lookup_mnt increments the ref count before returning
115 * the vfsmount struct.
117 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
119 struct vfsmount *child_mnt;
120 spin_lock(&vfsmount_lock);
121 if ((child_mnt = __lookup_mnt(mnt, dentry, 1)))
123 spin_unlock(&vfsmount_lock);
127 static inline int check_mnt(struct vfsmount *mnt)
129 return mnt->mnt_namespace == current->namespace;
132 static void touch_namespace(struct namespace *ns)
136 wake_up_interruptible(&ns->poll);
140 static void __touch_namespace(struct namespace *ns)
142 if (ns && ns->event != event) {
144 wake_up_interruptible(&ns->poll);
148 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
150 old_nd->dentry = mnt->mnt_mountpoint;
151 old_nd->mnt = mnt->mnt_parent;
152 mnt->mnt_parent = mnt;
153 mnt->mnt_mountpoint = mnt->mnt_root;
154 list_del_init(&mnt->mnt_child);
155 list_del_init(&mnt->mnt_hash);
156 old_nd->dentry->d_mounted--;
159 void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
160 struct vfsmount *child_mnt)
162 child_mnt->mnt_parent = mntget(mnt);
163 child_mnt->mnt_mountpoint = dget(dentry);
167 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
169 mnt_set_mountpoint(nd->mnt, nd->dentry, mnt);
170 list_add_tail(&mnt->mnt_hash, mount_hashtable +
171 hash(nd->mnt, nd->dentry));
172 list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
176 * the caller must hold vfsmount_lock
178 static void commit_tree(struct vfsmount *mnt)
180 struct vfsmount *parent = mnt->mnt_parent;
183 struct namespace *n = parent->mnt_namespace;
185 BUG_ON(parent == mnt);
187 list_add_tail(&head, &mnt->mnt_list);
188 list_for_each_entry(m, &head, mnt_list)
189 m->mnt_namespace = n;
190 list_splice(&head, n->list.prev);
192 list_add_tail(&mnt->mnt_hash, mount_hashtable +
193 hash(parent, mnt->mnt_mountpoint));
194 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
198 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
200 struct list_head *next = p->mnt_mounts.next;
201 if (next == &p->mnt_mounts) {
205 next = p->mnt_child.next;
206 if (next != &p->mnt_parent->mnt_mounts)
211 return list_entry(next, struct vfsmount, mnt_child);
214 static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
217 struct super_block *sb = old->mnt_sb;
218 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
221 mnt->mnt_flags = old->mnt_flags;
222 atomic_inc(&sb->s_active);
224 mnt->mnt_root = dget(root);
225 mnt->mnt_mountpoint = mnt->mnt_root;
226 mnt->mnt_parent = mnt;
228 if ((flag & CL_PROPAGATION) || IS_MNT_SHARED(old))
229 list_add(&mnt->mnt_share, &old->mnt_share);
230 if (flag & CL_MAKE_SHARED)
233 /* stick the duplicate mount on the same expiry list
234 * as the original if that was on one */
235 if (flag & CL_EXPIRE) {
236 spin_lock(&vfsmount_lock);
237 if (!list_empty(&old->mnt_expire))
238 list_add(&mnt->mnt_expire, &old->mnt_expire);
239 spin_unlock(&vfsmount_lock);
245 static inline void __mntput(struct vfsmount *mnt)
247 struct super_block *sb = mnt->mnt_sb;
250 deactivate_super(sb);
253 void mntput_no_expire(struct vfsmount *mnt)
256 if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
257 if (likely(!mnt->mnt_pinned)) {
258 spin_unlock(&vfsmount_lock);
262 atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
264 spin_unlock(&vfsmount_lock);
265 acct_auto_close_mnt(mnt);
266 security_sb_umount_close(mnt);
271 EXPORT_SYMBOL(mntput_no_expire);
273 void mnt_pin(struct vfsmount *mnt)
275 spin_lock(&vfsmount_lock);
277 spin_unlock(&vfsmount_lock);
280 EXPORT_SYMBOL(mnt_pin);
282 void mnt_unpin(struct vfsmount *mnt)
284 spin_lock(&vfsmount_lock);
285 if (mnt->mnt_pinned) {
286 atomic_inc(&mnt->mnt_count);
289 spin_unlock(&vfsmount_lock);
292 EXPORT_SYMBOL(mnt_unpin);
295 static void *m_start(struct seq_file *m, loff_t *pos)
297 struct namespace *n = m->private;
301 down_read(&namespace_sem);
302 list_for_each(p, &n->list)
304 return list_entry(p, struct vfsmount, mnt_list);
308 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
310 struct namespace *n = m->private;
311 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
313 return p == &n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
316 static void m_stop(struct seq_file *m, void *v)
318 up_read(&namespace_sem);
321 static inline void mangle(struct seq_file *m, const char *s)
323 seq_escape(m, s, " \t\n\\");
326 static int show_vfsmnt(struct seq_file *m, void *v)
328 struct vfsmount *mnt = v;
330 static struct proc_fs_info {
334 { MS_SYNCHRONOUS, ",sync" },
335 { MS_DIRSYNC, ",dirsync" },
336 { MS_MANDLOCK, ",mand" },
337 { MS_NOATIME, ",noatime" },
338 { MS_NODIRATIME, ",nodiratime" },
341 static struct proc_fs_info mnt_info[] = {
342 { MNT_NOSUID, ",nosuid" },
343 { MNT_NODEV, ",nodev" },
344 { MNT_NOEXEC, ",noexec" },
347 struct proc_fs_info *fs_infop;
349 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
351 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
353 mangle(m, mnt->mnt_sb->s_type->name);
354 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
355 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
356 if (mnt->mnt_sb->s_flags & fs_infop->flag)
357 seq_puts(m, fs_infop->str);
359 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
360 if (mnt->mnt_flags & fs_infop->flag)
361 seq_puts(m, fs_infop->str);
363 if (mnt->mnt_sb->s_op->show_options)
364 err = mnt->mnt_sb->s_op->show_options(m, mnt);
365 seq_puts(m, " 0 0\n");
369 struct seq_operations mounts_op = {
377 * may_umount_tree - check if a mount tree is busy
378 * @mnt: root of mount tree
380 * This is called to check if a tree of mounts has any
381 * open files, pwds, chroots or sub mounts that are
384 int may_umount_tree(struct vfsmount *mnt)
387 int minimum_refs = 0;
390 spin_lock(&vfsmount_lock);
391 for (p = mnt; p; p = next_mnt(p, mnt)) {
392 actual_refs += atomic_read(&p->mnt_count);
395 spin_unlock(&vfsmount_lock);
397 if (actual_refs > minimum_refs)
403 EXPORT_SYMBOL(may_umount_tree);
406 * may_umount - check if a mount point is busy
407 * @mnt: root of mount
409 * This is called to check if a mount point has any
410 * open files, pwds, chroots or sub mounts. If the
411 * mount has sub mounts this will return busy
412 * regardless of whether the sub mounts are busy.
414 * Doesn't take quota and stuff into account. IOW, in some cases it will
415 * give false negatives. The main reason why it's here is that we need
416 * a non-destructive way to look for easily umountable filesystems.
418 int may_umount(struct vfsmount *mnt)
421 spin_lock(&vfsmount_lock);
422 if (propagate_mount_busy(mnt, 2))
424 spin_unlock(&vfsmount_lock);
428 EXPORT_SYMBOL(may_umount);
430 void release_mounts(struct list_head *head)
432 struct vfsmount *mnt;
433 while(!list_empty(head)) {
434 mnt = list_entry(head->next, struct vfsmount, mnt_hash);
435 list_del_init(&mnt->mnt_hash);
436 if (mnt->mnt_parent != mnt) {
437 struct dentry *dentry;
439 spin_lock(&vfsmount_lock);
440 dentry = mnt->mnt_mountpoint;
442 mnt->mnt_mountpoint = mnt->mnt_root;
443 mnt->mnt_parent = mnt;
444 spin_unlock(&vfsmount_lock);
452 void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
456 for (p = mnt; p; p = next_mnt(p, mnt)) {
457 list_del(&p->mnt_hash);
458 list_add(&p->mnt_hash, kill);
462 propagate_umount(kill);
464 list_for_each_entry(p, kill, mnt_hash) {
465 list_del_init(&p->mnt_expire);
466 list_del_init(&p->mnt_list);
467 __touch_namespace(p->mnt_namespace);
468 p->mnt_namespace = NULL;
469 list_del_init(&p->mnt_child);
470 if (p->mnt_parent != p)
471 mnt->mnt_mountpoint->d_mounted--;
472 change_mnt_propagation(p, MS_PRIVATE);
476 static int do_umount(struct vfsmount *mnt, int flags)
478 struct super_block *sb = mnt->mnt_sb;
480 LIST_HEAD(umount_list);
482 retval = security_sb_umount(mnt, flags);
487 * Allow userspace to request a mountpoint be expired rather than
488 * unmounting unconditionally. Unmount only happens if:
489 * (1) the mark is already set (the mark is cleared by mntput())
490 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
492 if (flags & MNT_EXPIRE) {
493 if (mnt == current->fs->rootmnt ||
494 flags & (MNT_FORCE | MNT_DETACH))
497 if (atomic_read(&mnt->mnt_count) != 2)
500 if (!xchg(&mnt->mnt_expiry_mark, 1))
505 * If we may have to abort operations to get out of this
506 * mount, and they will themselves hold resources we must
507 * allow the fs to do things. In the Unix tradition of
508 * 'Gee thats tricky lets do it in userspace' the umount_begin
509 * might fail to complete on the first run through as other tasks
510 * must return, and the like. Thats for the mount program to worry
511 * about for the moment.
515 if ((flags & MNT_FORCE) && sb->s_op->umount_begin)
516 sb->s_op->umount_begin(sb);
520 * No sense to grab the lock for this test, but test itself looks
521 * somewhat bogus. Suggestions for better replacement?
522 * Ho-hum... In principle, we might treat that as umount + switch
523 * to rootfs. GC would eventually take care of the old vfsmount.
524 * Actually it makes sense, especially if rootfs would contain a
525 * /reboot - static binary that would close all descriptors and
526 * call reboot(9). Then init(8) could umount root and exec /reboot.
528 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
530 * Special case for "unmounting" root ...
531 * we just try to remount it readonly.
533 down_write(&sb->s_umount);
534 if (!(sb->s_flags & MS_RDONLY)) {
537 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
540 up_write(&sb->s_umount);
544 down_write(&namespace_sem);
545 spin_lock(&vfsmount_lock);
549 if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
550 if (!list_empty(&mnt->mnt_list))
551 umount_tree(mnt, 1, &umount_list);
554 spin_unlock(&vfsmount_lock);
556 security_sb_umount_busy(mnt);
557 up_write(&namespace_sem);
558 release_mounts(&umount_list);
563 * Now umount can handle mount points as well as block devices.
564 * This is important for filesystems which use unnamed block devices.
566 * We now support a flag for forced unmount like the other 'big iron'
567 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
570 asmlinkage long sys_umount(char __user * name, int flags)
575 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
579 if (nd.dentry != nd.mnt->mnt_root)
581 if (!check_mnt(nd.mnt))
585 if (!capable(CAP_SYS_ADMIN))
588 retval = do_umount(nd.mnt, flags);
590 path_release_on_umount(&nd);
595 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
598 * The 2.0 compatible umount. No flags.
600 asmlinkage long sys_oldumount(char __user * name)
602 return sys_umount(name, 0);
607 static int mount_is_safe(struct nameidata *nd)
609 if (capable(CAP_SYS_ADMIN))
613 if (S_ISLNK(nd->dentry->d_inode->i_mode))
615 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
616 if (current->uid != nd->dentry->d_inode->i_uid)
619 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
625 static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
630 if (d == NULL || d == d->d_parent)
636 struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
639 struct vfsmount *res, *p, *q, *r, *s;
642 res = q = clone_mnt(mnt, dentry, flag);
645 q->mnt_mountpoint = mnt->mnt_mountpoint;
648 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
649 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
652 for (s = r; s; s = next_mnt(s, r)) {
653 while (p != s->mnt_parent) {
659 nd.dentry = p->mnt_mountpoint;
660 q = clone_mnt(p, p->mnt_root, flag);
663 spin_lock(&vfsmount_lock);
664 list_add_tail(&q->mnt_list, &res->mnt_list);
666 spin_unlock(&vfsmount_lock);
672 LIST_HEAD(umount_list);
673 spin_lock(&vfsmount_lock);
674 umount_tree(res, 0, &umount_list);
675 spin_unlock(&vfsmount_lock);
676 release_mounts(&umount_list);
682 * @source_mnt : mount tree to be attached
683 * @nd : place the mount tree @source_mnt is attached
684 * @parent_nd : if non-null, detach the source_mnt from its parent and
685 * store the parent mount and mountpoint dentry.
686 * (done when source_mnt is moved)
688 * NOTE: in the table below explains the semantics when a source mount
689 * of a given type is attached to a destination mount of a given type.
690 * ---------------------------------------------
691 * | BIND MOUNT OPERATION |
692 * |********************************************
693 * | source-->| shared | private |
697 * |********************************************
698 * | shared | shared (++) | shared (+) |
700 * |non-shared| shared (+) | private |
701 * *********************************************
702 * A bind operation clones the source mount and mounts the clone on the
705 * (++) the cloned mount is propagated to all the mounts in the propagation
706 * tree of the destination mount and the cloned mount is added to
707 * the peer group of the source mount.
708 * (+) the cloned mount is created under the destination mount and is marked
709 * as shared. The cloned mount is added to the peer group of the source
711 * ---------------------------------------------
712 * | MOVE MOUNT OPERATION |
713 * |********************************************
714 * | source-->| shared | private |
718 * |********************************************
719 * | shared | shared (+) | shared (+) |
721 * |non-shared| shared (+*) | private |
722 * *********************************************
723 * (+) the mount is moved to the destination. And is then propagated to all
724 * the mounts in the propagation tree of the destination mount.
725 * (+*) the mount is moved to the destination.
727 * if the source mount is a tree, the operations explained above is
728 * applied to each mount in the tree.
729 * Must be called without spinlocks held, since this function can sleep
732 static int attach_recursive_mnt(struct vfsmount *source_mnt,
733 struct nameidata *nd, struct nameidata *parent_nd)
735 LIST_HEAD(tree_list);
736 struct vfsmount *dest_mnt = nd->mnt;
737 struct dentry *dest_dentry = nd->dentry;
738 struct vfsmount *child, *p;
740 if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list))
743 if (IS_MNT_SHARED(dest_mnt)) {
744 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
748 spin_lock(&vfsmount_lock);
750 detach_mnt(source_mnt, parent_nd);
751 attach_mnt(source_mnt, nd);
752 touch_namespace(current->namespace);
754 mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
755 commit_tree(source_mnt);
758 list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
759 list_del_init(&child->mnt_hash);
762 spin_unlock(&vfsmount_lock);
766 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
769 if (mnt->mnt_sb->s_flags & MS_NOUSER)
772 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
773 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
777 down(&nd->dentry->d_inode->i_sem);
778 if (IS_DEADDIR(nd->dentry->d_inode))
781 err = security_sb_check_sb(mnt, nd);
786 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry))
787 err = attach_recursive_mnt(mnt, nd, NULL);
789 up(&nd->dentry->d_inode->i_sem);
791 security_sb_post_addmount(mnt, nd);
796 * recursively change the type of the mountpoint.
798 static int do_change_type(struct nameidata *nd, int flag)
800 struct vfsmount *m, *mnt = nd->mnt;
801 int recurse = flag & MS_REC;
802 int type = flag & ~MS_REC;
804 if (nd->dentry != nd->mnt->mnt_root)
807 down_write(&namespace_sem);
808 spin_lock(&vfsmount_lock);
809 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
810 change_mnt_propagation(m, type);
811 spin_unlock(&vfsmount_lock);
812 up_write(&namespace_sem);
819 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
821 struct nameidata old_nd;
822 struct vfsmount *mnt = NULL;
823 int err = mount_is_safe(nd);
826 if (!old_name || !*old_name)
828 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
832 down_write(&namespace_sem);
834 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
839 mnt = copy_tree(old_nd.mnt, old_nd.dentry, 0);
841 mnt = clone_mnt(old_nd.mnt, old_nd.dentry, 0);
846 err = graft_tree(mnt, nd);
848 LIST_HEAD(umount_list);
849 spin_lock(&vfsmount_lock);
850 umount_tree(mnt, 0, &umount_list);
851 spin_unlock(&vfsmount_lock);
852 release_mounts(&umount_list);
856 up_write(&namespace_sem);
857 path_release(&old_nd);
862 * change filesystem flags. dir should be a physical root of filesystem.
863 * If you've mounted a non-root directory somewhere and want to do remount
864 * on it - tough luck.
866 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
870 struct super_block *sb = nd->mnt->mnt_sb;
872 if (!capable(CAP_SYS_ADMIN))
875 if (!check_mnt(nd->mnt))
878 if (nd->dentry != nd->mnt->mnt_root)
881 down_write(&sb->s_umount);
882 err = do_remount_sb(sb, flags, data, 0);
884 nd->mnt->mnt_flags = mnt_flags;
885 up_write(&sb->s_umount);
887 security_sb_post_remount(nd->mnt, flags, data);
891 static int do_move_mount(struct nameidata *nd, char *old_name)
893 struct nameidata old_nd, parent_nd;
896 if (!capable(CAP_SYS_ADMIN))
898 if (!old_name || !*old_name)
900 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
904 down_write(&namespace_sem);
905 while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
908 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
912 down(&nd->dentry->d_inode->i_sem);
913 if (IS_DEADDIR(nd->dentry->d_inode))
916 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
920 if (old_nd.dentry != old_nd.mnt->mnt_root)
923 if (old_nd.mnt == old_nd.mnt->mnt_parent)
926 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
927 S_ISDIR(old_nd.dentry->d_inode->i_mode))
930 * Don't move a mount residing in a shared parent.
932 if (old_nd.mnt->mnt_parent && IS_MNT_SHARED(old_nd.mnt->mnt_parent))
935 for (p = nd->mnt; p->mnt_parent != p; p = p->mnt_parent)
939 if ((err = attach_recursive_mnt(old_nd.mnt, nd, &parent_nd)))
942 spin_lock(&vfsmount_lock);
943 /* if the mount is moved, it should no longer be expire
945 list_del_init(&old_nd.mnt->mnt_expire);
946 spin_unlock(&vfsmount_lock);
948 up(&nd->dentry->d_inode->i_sem);
950 up_write(&namespace_sem);
952 path_release(&parent_nd);
953 path_release(&old_nd);
958 * create a new mount for userspace and request it to be added into the
961 static int do_new_mount(struct nameidata *nd, char *type, int flags,
962 int mnt_flags, char *name, void *data)
964 struct vfsmount *mnt;
966 if (!type || !memchr(type, 0, PAGE_SIZE))
969 /* we need capabilities... */
970 if (!capable(CAP_SYS_ADMIN))
973 mnt = do_kern_mount(type, flags, name, data);
977 return do_add_mount(mnt, nd, mnt_flags, NULL);
981 * add a mount into a namespace's mount tree
982 * - provide the option of adding the new mount to an expiration list
984 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
985 int mnt_flags, struct list_head *fslist)
989 down_write(&namespace_sem);
990 /* Something was mounted here while we slept */
991 while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
994 if (!check_mnt(nd->mnt))
997 /* Refuse the same filesystem on the same mount point */
999 if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
1000 nd->mnt->mnt_root == nd->dentry)
1004 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
1007 newmnt->mnt_flags = mnt_flags;
1008 if ((err = graft_tree(newmnt, nd)))
1012 /* add to the specified expiration list */
1013 spin_lock(&vfsmount_lock);
1014 list_add_tail(&newmnt->mnt_expire, fslist);
1015 spin_unlock(&vfsmount_lock);
1017 up_write(&namespace_sem);
1021 up_write(&namespace_sem);
1026 EXPORT_SYMBOL_GPL(do_add_mount);
1028 static void expire_mount(struct vfsmount *mnt, struct list_head *mounts,
1029 struct list_head *umounts)
1031 spin_lock(&vfsmount_lock);
1034 * Check if mount is still attached, if not, let whoever holds it deal
1037 if (mnt->mnt_parent == mnt) {
1038 spin_unlock(&vfsmount_lock);
1043 * Check that it is still dead: the count should now be 2 - as
1044 * contributed by the vfsmount parent and the mntget above
1046 if (!propagate_mount_busy(mnt, 2)) {
1047 /* delete from the namespace */
1048 touch_namespace(mnt->mnt_namespace);
1049 list_del_init(&mnt->mnt_list);
1050 mnt->mnt_namespace = NULL;
1051 umount_tree(mnt, 1, umounts);
1052 spin_unlock(&vfsmount_lock);
1055 * Someone brought it back to life whilst we didn't have any
1056 * locks held so return it to the expiration list
1058 list_add_tail(&mnt->mnt_expire, mounts);
1059 spin_unlock(&vfsmount_lock);
1064 * process a list of expirable mountpoints with the intent of discarding any
1065 * mountpoints that aren't in use and haven't been touched since last we came
1068 void mark_mounts_for_expiry(struct list_head *mounts)
1070 struct namespace *namespace;
1071 struct vfsmount *mnt, *next;
1072 LIST_HEAD(graveyard);
1074 if (list_empty(mounts))
1077 spin_lock(&vfsmount_lock);
1079 /* extract from the expiration list every vfsmount that matches the
1080 * following criteria:
1081 * - only referenced by its parent vfsmount
1082 * - still marked for expiry (marked on the last call here; marks are
1083 * cleared by mntput())
1085 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
1086 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1087 atomic_read(&mnt->mnt_count) != 1)
1091 list_move(&mnt->mnt_expire, &graveyard);
1095 * go through the vfsmounts we've just consigned to the graveyard to
1096 * - check that they're still dead
1097 * - delete the vfsmount from the appropriate namespace under lock
1098 * - dispose of the corpse
1100 while (!list_empty(&graveyard)) {
1102 mnt = list_entry(graveyard.next, struct vfsmount, mnt_expire);
1103 list_del_init(&mnt->mnt_expire);
1105 /* don't do anything if the namespace is dead - all the
1106 * vfsmounts from it are going away anyway */
1107 namespace = mnt->mnt_namespace;
1108 if (!namespace || !namespace->root)
1110 get_namespace(namespace);
1112 spin_unlock(&vfsmount_lock);
1113 down_write(&namespace_sem);
1114 expire_mount(mnt, mounts, &umounts);
1115 up_write(&namespace_sem);
1116 release_mounts(&umounts);
1118 put_namespace(namespace);
1119 spin_lock(&vfsmount_lock);
1122 spin_unlock(&vfsmount_lock);
1125 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
1128 * Some copy_from_user() implementations do not return the exact number of
1129 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
1130 * Note that this function differs from copy_from_user() in that it will oops
1131 * on bad values of `to', rather than returning a short copy.
1133 static long exact_copy_from_user(void *to, const void __user * from,
1137 const char __user *f = from;
1140 if (!access_ok(VERIFY_READ, from, n))
1144 if (__get_user(c, f)) {
1155 int copy_mount_options(const void __user * data, unsigned long *where)
1165 if (!(page = __get_free_page(GFP_KERNEL)))
1168 /* We only care that *some* data at the address the user
1169 * gave us is valid. Just in case, we'll zero
1170 * the remainder of the page.
1172 /* copy_from_user cannot cross TASK_SIZE ! */
1173 size = TASK_SIZE - (unsigned long)data;
1174 if (size > PAGE_SIZE)
1177 i = size - exact_copy_from_user((void *)page, data, size);
1183 memset((char *)page + i, 0, PAGE_SIZE - i);
1189 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1190 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1192 * data is a (void *) that can point to any structure up to
1193 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1194 * information (or be NULL).
1196 * Pre-0.97 versions of mount() didn't have a flags word.
1197 * When the flags word was introduced its top half was required
1198 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1199 * Therefore, if this magic number is present, it carries no information
1200 * and must be discarded.
1202 long do_mount(char *dev_name, char *dir_name, char *type_page,
1203 unsigned long flags, void *data_page)
1205 struct nameidata nd;
1210 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1211 flags &= ~MS_MGC_MSK;
1213 /* Basic sanity checks */
1215 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1217 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1221 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1223 /* Separate the per-mountpoint flags */
1224 if (flags & MS_NOSUID)
1225 mnt_flags |= MNT_NOSUID;
1226 if (flags & MS_NODEV)
1227 mnt_flags |= MNT_NODEV;
1228 if (flags & MS_NOEXEC)
1229 mnt_flags |= MNT_NOEXEC;
1230 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE);
1232 /* ... and get the mountpoint */
1233 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1237 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1241 if (flags & MS_REMOUNT)
1242 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1244 else if (flags & MS_BIND)
1245 retval = do_loopback(&nd, dev_name, flags & MS_REC);
1246 else if (flags & (MS_SHARED | MS_PRIVATE))
1247 retval = do_change_type(&nd, flags);
1248 else if (flags & MS_MOVE)
1249 retval = do_move_mount(&nd, dev_name);
1251 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1252 dev_name, data_page);
1258 int copy_namespace(int flags, struct task_struct *tsk)
1260 struct namespace *namespace = tsk->namespace;
1261 struct namespace *new_ns;
1262 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1263 struct fs_struct *fs = tsk->fs;
1264 struct vfsmount *p, *q;
1269 get_namespace(namespace);
1271 if (!(flags & CLONE_NEWNS))
1274 if (!capable(CAP_SYS_ADMIN)) {
1275 put_namespace(namespace);
1279 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1283 atomic_set(&new_ns->count, 1);
1284 INIT_LIST_HEAD(&new_ns->list);
1285 init_waitqueue_head(&new_ns->poll);
1288 down_write(&namespace_sem);
1289 /* First pass: copy the tree topology */
1290 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root,
1292 if (!new_ns->root) {
1293 up_write(&namespace_sem);
1297 spin_lock(&vfsmount_lock);
1298 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1299 spin_unlock(&vfsmount_lock);
1302 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1303 * as belonging to new namespace. We have already acquired a private
1304 * fs_struct, so tsk->fs->lock is not needed.
1306 p = namespace->root;
1309 q->mnt_namespace = new_ns;
1311 if (p == fs->rootmnt) {
1313 fs->rootmnt = mntget(q);
1315 if (p == fs->pwdmnt) {
1317 fs->pwdmnt = mntget(q);
1319 if (p == fs->altrootmnt) {
1321 fs->altrootmnt = mntget(q);
1324 p = next_mnt(p, namespace->root);
1325 q = next_mnt(q, new_ns->root);
1327 up_write(&namespace_sem);
1329 tsk->namespace = new_ns;
1338 put_namespace(namespace);
1342 put_namespace(namespace);
1346 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1347 char __user * type, unsigned long flags,
1351 unsigned long data_page;
1352 unsigned long type_page;
1353 unsigned long dev_page;
1356 retval = copy_mount_options(type, &type_page);
1360 dir_page = getname(dir_name);
1361 retval = PTR_ERR(dir_page);
1362 if (IS_ERR(dir_page))
1365 retval = copy_mount_options(dev_name, &dev_page);
1369 retval = copy_mount_options(data, &data_page);
1374 retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
1375 flags, (void *)data_page);
1377 free_page(data_page);
1380 free_page(dev_page);
1384 free_page(type_page);
1389 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1390 * It can block. Requires the big lock held.
1392 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1393 struct dentry *dentry)
1395 struct dentry *old_root;
1396 struct vfsmount *old_rootmnt;
1397 write_lock(&fs->lock);
1398 old_root = fs->root;
1399 old_rootmnt = fs->rootmnt;
1400 fs->rootmnt = mntget(mnt);
1401 fs->root = dget(dentry);
1402 write_unlock(&fs->lock);
1405 mntput(old_rootmnt);
1410 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1411 * It can block. Requires the big lock held.
1413 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1414 struct dentry *dentry)
1416 struct dentry *old_pwd;
1417 struct vfsmount *old_pwdmnt;
1419 write_lock(&fs->lock);
1421 old_pwdmnt = fs->pwdmnt;
1422 fs->pwdmnt = mntget(mnt);
1423 fs->pwd = dget(dentry);
1424 write_unlock(&fs->lock);
1432 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1434 struct task_struct *g, *p;
1435 struct fs_struct *fs;
1437 read_lock(&tasklist_lock);
1438 do_each_thread(g, p) {
1442 atomic_inc(&fs->count);
1444 if (fs->root == old_nd->dentry
1445 && fs->rootmnt == old_nd->mnt)
1446 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1447 if (fs->pwd == old_nd->dentry
1448 && fs->pwdmnt == old_nd->mnt)
1449 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1453 } while_each_thread(g, p);
1454 read_unlock(&tasklist_lock);
1458 * pivot_root Semantics:
1459 * Moves the root file system of the current process to the directory put_old,
1460 * makes new_root as the new root file system of the current process, and sets
1461 * root/cwd of all processes which had them on the current root to new_root.
1464 * The new_root and put_old must be directories, and must not be on the
1465 * same file system as the current process root. The put_old must be
1466 * underneath new_root, i.e. adding a non-zero number of /.. to the string
1467 * pointed to by put_old must yield the same directory as new_root. No other
1468 * file system may be mounted on put_old. After all, new_root is a mountpoint.
1471 * - we don't move root/cwd if they are not at the root (reason: if something
1472 * cared enough to change them, it's probably wrong to force them elsewhere)
1473 * - it's okay to pick a root that isn't the root of a file system, e.g.
1474 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1475 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1478 asmlinkage long sys_pivot_root(const char __user * new_root,
1479 const char __user * put_old)
1481 struct vfsmount *tmp;
1482 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1485 if (!capable(CAP_SYS_ADMIN))
1490 error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1495 if (!check_mnt(new_nd.mnt))
1498 error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
1502 error = security_sb_pivotroot(&old_nd, &new_nd);
1504 path_release(&old_nd);
1508 read_lock(¤t->fs->lock);
1509 user_nd.mnt = mntget(current->fs->rootmnt);
1510 user_nd.dentry = dget(current->fs->root);
1511 read_unlock(¤t->fs->lock);
1512 down_write(&namespace_sem);
1513 down(&old_nd.dentry->d_inode->i_sem);
1515 if (IS_MNT_SHARED(old_nd.mnt) ||
1516 IS_MNT_SHARED(new_nd.mnt->mnt_parent) ||
1517 IS_MNT_SHARED(user_nd.mnt->mnt_parent))
1519 if (!check_mnt(user_nd.mnt))
1522 if (IS_DEADDIR(new_nd.dentry->d_inode))
1524 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1526 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1529 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1530 goto out2; /* loop, on the same file system */
1532 if (user_nd.mnt->mnt_root != user_nd.dentry)
1533 goto out2; /* not a mountpoint */
1534 if (user_nd.mnt->mnt_parent == user_nd.mnt)
1535 goto out2; /* not attached */
1536 if (new_nd.mnt->mnt_root != new_nd.dentry)
1537 goto out2; /* not a mountpoint */
1538 if (new_nd.mnt->mnt_parent == new_nd.mnt)
1539 goto out2; /* not attached */
1540 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1541 spin_lock(&vfsmount_lock);
1542 if (tmp != new_nd.mnt) {
1544 if (tmp->mnt_parent == tmp)
1545 goto out3; /* already mounted on put_old */
1546 if (tmp->mnt_parent == new_nd.mnt)
1548 tmp = tmp->mnt_parent;
1550 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1552 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1554 detach_mnt(new_nd.mnt, &parent_nd);
1555 detach_mnt(user_nd.mnt, &root_parent);
1556 attach_mnt(user_nd.mnt, &old_nd); /* mount old root on put_old */
1557 attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */
1558 touch_namespace(current->namespace);
1559 spin_unlock(&vfsmount_lock);
1560 chroot_fs_refs(&user_nd, &new_nd);
1561 security_sb_post_pivotroot(&user_nd, &new_nd);
1563 path_release(&root_parent);
1564 path_release(&parent_nd);
1566 up(&old_nd.dentry->d_inode->i_sem);
1567 up_write(&namespace_sem);
1568 path_release(&user_nd);
1569 path_release(&old_nd);
1571 path_release(&new_nd);
1576 spin_unlock(&vfsmount_lock);
1580 static void __init init_mount_tree(void)
1582 struct vfsmount *mnt;
1583 struct namespace *namespace;
1584 struct task_struct *g, *p;
1586 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1588 panic("Can't create rootfs");
1589 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1591 panic("Can't allocate initial namespace");
1592 atomic_set(&namespace->count, 1);
1593 INIT_LIST_HEAD(&namespace->list);
1594 init_waitqueue_head(&namespace->poll);
1595 namespace->event = 0;
1596 list_add(&mnt->mnt_list, &namespace->list);
1597 namespace->root = mnt;
1598 mnt->mnt_namespace = namespace;
1600 init_task.namespace = namespace;
1601 read_lock(&tasklist_lock);
1602 do_each_thread(g, p) {
1603 get_namespace(namespace);
1604 p->namespace = namespace;
1605 } while_each_thread(g, p);
1606 read_unlock(&tasklist_lock);
1608 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1609 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1612 void __init mnt_init(unsigned long mempages)
1614 struct list_head *d;
1615 unsigned int nr_hash;
1618 init_rwsem(&namespace_sem);
1620 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1621 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL, NULL);
1623 mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
1625 if (!mount_hashtable)
1626 panic("Failed to allocate mount hash table\n");
1629 * Find the power-of-two list-heads that can fit into the allocation..
1630 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1633 nr_hash = PAGE_SIZE / sizeof(struct list_head);
1637 } while ((nr_hash >> hash_bits) != 0);
1641 * Re-calculate the actual number of entries and the mask
1642 * from the number of bits we can fit.
1644 nr_hash = 1UL << hash_bits;
1645 hash_mask = nr_hash - 1;
1647 printk("Mount-cache hash table entries: %d\n", nr_hash);
1649 /* And initialize the newly allocated array */
1650 d = mount_hashtable;
1662 void __put_namespace(struct namespace *namespace)
1664 struct vfsmount *root = namespace->root;
1665 LIST_HEAD(umount_list);
1666 namespace->root = NULL;
1667 spin_unlock(&vfsmount_lock);
1668 down_write(&namespace_sem);
1669 spin_lock(&vfsmount_lock);
1670 umount_tree(root, 0, &umount_list);
1671 spin_unlock(&vfsmount_lock);
1672 up_write(&namespace_sem);
1673 release_mounts(&umount_list);