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);
70 INIT_LIST_HEAD(&mnt->mnt_slave_list);
71 INIT_LIST_HEAD(&mnt->mnt_slave);
73 int size = strlen(name) + 1;
74 char *newname = kmalloc(size, GFP_KERNEL);
76 memcpy(newname, name, size);
77 mnt->mnt_devname = newname;
84 void free_vfsmnt(struct vfsmount *mnt)
86 kfree(mnt->mnt_devname);
87 kmem_cache_free(mnt_cache, mnt);
91 * find the first or last mount at @dentry on vfsmount @mnt depending on
92 * @dir. If @dir is set return the first mount else return the last mount.
94 struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
97 struct list_head *head = mount_hashtable + hash(mnt, dentry);
98 struct list_head *tmp = head;
99 struct vfsmount *p, *found = NULL;
102 tmp = dir ? tmp->next : tmp->prev;
106 p = list_entry(tmp, struct vfsmount, mnt_hash);
107 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
116 * lookup_mnt increments the ref count before returning
117 * the vfsmount struct.
119 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
121 struct vfsmount *child_mnt;
122 spin_lock(&vfsmount_lock);
123 if ((child_mnt = __lookup_mnt(mnt, dentry, 1)))
125 spin_unlock(&vfsmount_lock);
129 static inline int check_mnt(struct vfsmount *mnt)
131 return mnt->mnt_namespace == current->namespace;
134 static void touch_namespace(struct namespace *ns)
138 wake_up_interruptible(&ns->poll);
142 static void __touch_namespace(struct namespace *ns)
144 if (ns && ns->event != event) {
146 wake_up_interruptible(&ns->poll);
150 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
152 old_nd->dentry = mnt->mnt_mountpoint;
153 old_nd->mnt = mnt->mnt_parent;
154 mnt->mnt_parent = mnt;
155 mnt->mnt_mountpoint = mnt->mnt_root;
156 list_del_init(&mnt->mnt_child);
157 list_del_init(&mnt->mnt_hash);
158 old_nd->dentry->d_mounted--;
161 void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
162 struct vfsmount *child_mnt)
164 child_mnt->mnt_parent = mntget(mnt);
165 child_mnt->mnt_mountpoint = dget(dentry);
169 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
171 mnt_set_mountpoint(nd->mnt, nd->dentry, mnt);
172 list_add_tail(&mnt->mnt_hash, mount_hashtable +
173 hash(nd->mnt, nd->dentry));
174 list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
178 * the caller must hold vfsmount_lock
180 static void commit_tree(struct vfsmount *mnt)
182 struct vfsmount *parent = mnt->mnt_parent;
185 struct namespace *n = parent->mnt_namespace;
187 BUG_ON(parent == mnt);
189 list_add_tail(&head, &mnt->mnt_list);
190 list_for_each_entry(m, &head, mnt_list)
191 m->mnt_namespace = n;
192 list_splice(&head, n->list.prev);
194 list_add_tail(&mnt->mnt_hash, mount_hashtable +
195 hash(parent, mnt->mnt_mountpoint));
196 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
200 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
202 struct list_head *next = p->mnt_mounts.next;
203 if (next == &p->mnt_mounts) {
207 next = p->mnt_child.next;
208 if (next != &p->mnt_parent->mnt_mounts)
213 return list_entry(next, struct vfsmount, mnt_child);
216 static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
218 struct list_head *prev = p->mnt_mounts.prev;
219 while (prev != &p->mnt_mounts) {
220 p = list_entry(prev, struct vfsmount, mnt_child);
221 prev = p->mnt_mounts.prev;
226 static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
229 struct super_block *sb = old->mnt_sb;
230 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
233 mnt->mnt_flags = old->mnt_flags;
234 atomic_inc(&sb->s_active);
236 mnt->mnt_root = dget(root);
237 mnt->mnt_mountpoint = mnt->mnt_root;
238 mnt->mnt_parent = mnt;
240 if (flag & CL_SLAVE) {
241 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
242 mnt->mnt_master = old;
243 CLEAR_MNT_SHARED(mnt);
245 if ((flag & CL_PROPAGATION) || IS_MNT_SHARED(old))
246 list_add(&mnt->mnt_share, &old->mnt_share);
247 if (IS_MNT_SLAVE(old))
248 list_add(&mnt->mnt_slave, &old->mnt_slave);
249 mnt->mnt_master = old->mnt_master;
251 if (flag & CL_MAKE_SHARED)
254 /* stick the duplicate mount on the same expiry list
255 * as the original if that was on one */
256 if (flag & CL_EXPIRE) {
257 spin_lock(&vfsmount_lock);
258 if (!list_empty(&old->mnt_expire))
259 list_add(&mnt->mnt_expire, &old->mnt_expire);
260 spin_unlock(&vfsmount_lock);
266 static inline void __mntput(struct vfsmount *mnt)
268 struct super_block *sb = mnt->mnt_sb;
271 deactivate_super(sb);
274 void mntput_no_expire(struct vfsmount *mnt)
277 if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
278 if (likely(!mnt->mnt_pinned)) {
279 spin_unlock(&vfsmount_lock);
283 atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
285 spin_unlock(&vfsmount_lock);
286 acct_auto_close_mnt(mnt);
287 security_sb_umount_close(mnt);
292 EXPORT_SYMBOL(mntput_no_expire);
294 void mnt_pin(struct vfsmount *mnt)
296 spin_lock(&vfsmount_lock);
298 spin_unlock(&vfsmount_lock);
301 EXPORT_SYMBOL(mnt_pin);
303 void mnt_unpin(struct vfsmount *mnt)
305 spin_lock(&vfsmount_lock);
306 if (mnt->mnt_pinned) {
307 atomic_inc(&mnt->mnt_count);
310 spin_unlock(&vfsmount_lock);
313 EXPORT_SYMBOL(mnt_unpin);
316 static void *m_start(struct seq_file *m, loff_t *pos)
318 struct namespace *n = m->private;
322 down_read(&namespace_sem);
323 list_for_each(p, &n->list)
325 return list_entry(p, struct vfsmount, mnt_list);
329 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
331 struct namespace *n = m->private;
332 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
334 return p == &n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
337 static void m_stop(struct seq_file *m, void *v)
339 up_read(&namespace_sem);
342 static inline void mangle(struct seq_file *m, const char *s)
344 seq_escape(m, s, " \t\n\\");
347 static int show_vfsmnt(struct seq_file *m, void *v)
349 struct vfsmount *mnt = v;
351 static struct proc_fs_info {
355 { MS_SYNCHRONOUS, ",sync" },
356 { MS_DIRSYNC, ",dirsync" },
357 { MS_MANDLOCK, ",mand" },
358 { MS_NOATIME, ",noatime" },
359 { MS_NODIRATIME, ",nodiratime" },
362 static struct proc_fs_info mnt_info[] = {
363 { MNT_NOSUID, ",nosuid" },
364 { MNT_NODEV, ",nodev" },
365 { MNT_NOEXEC, ",noexec" },
368 struct proc_fs_info *fs_infop;
370 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
372 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
374 mangle(m, mnt->mnt_sb->s_type->name);
375 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
376 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
377 if (mnt->mnt_sb->s_flags & fs_infop->flag)
378 seq_puts(m, fs_infop->str);
380 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
381 if (mnt->mnt_flags & fs_infop->flag)
382 seq_puts(m, fs_infop->str);
384 if (mnt->mnt_sb->s_op->show_options)
385 err = mnt->mnt_sb->s_op->show_options(m, mnt);
386 seq_puts(m, " 0 0\n");
390 struct seq_operations mounts_op = {
398 * may_umount_tree - check if a mount tree is busy
399 * @mnt: root of mount tree
401 * This is called to check if a tree of mounts has any
402 * open files, pwds, chroots or sub mounts that are
405 int may_umount_tree(struct vfsmount *mnt)
408 int minimum_refs = 0;
411 spin_lock(&vfsmount_lock);
412 for (p = mnt; p; p = next_mnt(p, mnt)) {
413 actual_refs += atomic_read(&p->mnt_count);
416 spin_unlock(&vfsmount_lock);
418 if (actual_refs > minimum_refs)
424 EXPORT_SYMBOL(may_umount_tree);
427 * may_umount - check if a mount point is busy
428 * @mnt: root of mount
430 * This is called to check if a mount point has any
431 * open files, pwds, chroots or sub mounts. If the
432 * mount has sub mounts this will return busy
433 * regardless of whether the sub mounts are busy.
435 * Doesn't take quota and stuff into account. IOW, in some cases it will
436 * give false negatives. The main reason why it's here is that we need
437 * a non-destructive way to look for easily umountable filesystems.
439 int may_umount(struct vfsmount *mnt)
442 spin_lock(&vfsmount_lock);
443 if (propagate_mount_busy(mnt, 2))
445 spin_unlock(&vfsmount_lock);
449 EXPORT_SYMBOL(may_umount);
451 void release_mounts(struct list_head *head)
453 struct vfsmount *mnt;
454 while(!list_empty(head)) {
455 mnt = list_entry(head->next, struct vfsmount, mnt_hash);
456 list_del_init(&mnt->mnt_hash);
457 if (mnt->mnt_parent != mnt) {
458 struct dentry *dentry;
460 spin_lock(&vfsmount_lock);
461 dentry = mnt->mnt_mountpoint;
463 mnt->mnt_mountpoint = mnt->mnt_root;
464 mnt->mnt_parent = mnt;
465 spin_unlock(&vfsmount_lock);
473 void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
477 for (p = mnt; p; p = next_mnt(p, mnt)) {
478 list_del(&p->mnt_hash);
479 list_add(&p->mnt_hash, kill);
483 propagate_umount(kill);
485 list_for_each_entry(p, kill, mnt_hash) {
486 list_del_init(&p->mnt_expire);
487 list_del_init(&p->mnt_list);
488 __touch_namespace(p->mnt_namespace);
489 p->mnt_namespace = NULL;
490 list_del_init(&p->mnt_child);
491 if (p->mnt_parent != p)
492 mnt->mnt_mountpoint->d_mounted--;
493 change_mnt_propagation(p, MS_PRIVATE);
497 static int do_umount(struct vfsmount *mnt, int flags)
499 struct super_block *sb = mnt->mnt_sb;
501 LIST_HEAD(umount_list);
503 retval = security_sb_umount(mnt, flags);
508 * Allow userspace to request a mountpoint be expired rather than
509 * unmounting unconditionally. Unmount only happens if:
510 * (1) the mark is already set (the mark is cleared by mntput())
511 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
513 if (flags & MNT_EXPIRE) {
514 if (mnt == current->fs->rootmnt ||
515 flags & (MNT_FORCE | MNT_DETACH))
518 if (atomic_read(&mnt->mnt_count) != 2)
521 if (!xchg(&mnt->mnt_expiry_mark, 1))
526 * If we may have to abort operations to get out of this
527 * mount, and they will themselves hold resources we must
528 * allow the fs to do things. In the Unix tradition of
529 * 'Gee thats tricky lets do it in userspace' the umount_begin
530 * might fail to complete on the first run through as other tasks
531 * must return, and the like. Thats for the mount program to worry
532 * about for the moment.
536 if ((flags & MNT_FORCE) && sb->s_op->umount_begin)
537 sb->s_op->umount_begin(sb);
541 * No sense to grab the lock for this test, but test itself looks
542 * somewhat bogus. Suggestions for better replacement?
543 * Ho-hum... In principle, we might treat that as umount + switch
544 * to rootfs. GC would eventually take care of the old vfsmount.
545 * Actually it makes sense, especially if rootfs would contain a
546 * /reboot - static binary that would close all descriptors and
547 * call reboot(9). Then init(8) could umount root and exec /reboot.
549 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
551 * Special case for "unmounting" root ...
552 * we just try to remount it readonly.
554 down_write(&sb->s_umount);
555 if (!(sb->s_flags & MS_RDONLY)) {
558 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
561 up_write(&sb->s_umount);
565 down_write(&namespace_sem);
566 spin_lock(&vfsmount_lock);
570 if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
571 if (!list_empty(&mnt->mnt_list))
572 umount_tree(mnt, 1, &umount_list);
575 spin_unlock(&vfsmount_lock);
577 security_sb_umount_busy(mnt);
578 up_write(&namespace_sem);
579 release_mounts(&umount_list);
584 * Now umount can handle mount points as well as block devices.
585 * This is important for filesystems which use unnamed block devices.
587 * We now support a flag for forced unmount like the other 'big iron'
588 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
591 asmlinkage long sys_umount(char __user * name, int flags)
596 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
600 if (nd.dentry != nd.mnt->mnt_root)
602 if (!check_mnt(nd.mnt))
606 if (!capable(CAP_SYS_ADMIN))
609 retval = do_umount(nd.mnt, flags);
611 path_release_on_umount(&nd);
616 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
619 * The 2.0 compatible umount. No flags.
621 asmlinkage long sys_oldumount(char __user * name)
623 return sys_umount(name, 0);
628 static int mount_is_safe(struct nameidata *nd)
630 if (capable(CAP_SYS_ADMIN))
634 if (S_ISLNK(nd->dentry->d_inode->i_mode))
636 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
637 if (current->uid != nd->dentry->d_inode->i_uid)
640 if (vfs_permission(nd, MAY_WRITE))
646 static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
651 if (d == NULL || d == d->d_parent)
657 struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
660 struct vfsmount *res, *p, *q, *r, *s;
663 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
666 res = q = clone_mnt(mnt, dentry, flag);
669 q->mnt_mountpoint = mnt->mnt_mountpoint;
672 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
673 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
676 for (s = r; s; s = next_mnt(s, r)) {
677 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
678 s = skip_mnt_tree(s);
681 while (p != s->mnt_parent) {
687 nd.dentry = p->mnt_mountpoint;
688 q = clone_mnt(p, p->mnt_root, flag);
691 spin_lock(&vfsmount_lock);
692 list_add_tail(&q->mnt_list, &res->mnt_list);
694 spin_unlock(&vfsmount_lock);
700 LIST_HEAD(umount_list);
701 spin_lock(&vfsmount_lock);
702 umount_tree(res, 0, &umount_list);
703 spin_unlock(&vfsmount_lock);
704 release_mounts(&umount_list);
710 * @source_mnt : mount tree to be attached
711 * @nd : place the mount tree @source_mnt is attached
712 * @parent_nd : if non-null, detach the source_mnt from its parent and
713 * store the parent mount and mountpoint dentry.
714 * (done when source_mnt is moved)
716 * NOTE: in the table below explains the semantics when a source mount
717 * of a given type is attached to a destination mount of a given type.
718 * ---------------------------------------------------------------------------
719 * | BIND MOUNT OPERATION |
720 * |**************************************************************************
721 * | source-->| shared | private | slave | unbindable |
725 * |**************************************************************************
726 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
728 * |non-shared| shared (+) | private | slave (*) | invalid |
729 * ***************************************************************************
730 * A bind operation clones the source mount and mounts the clone on the
733 * (++) the cloned mount is propagated to all the mounts in the propagation
734 * tree of the destination mount and the cloned mount is added to
735 * the peer group of the source mount.
736 * (+) the cloned mount is created under the destination mount and is marked
737 * as shared. The cloned mount is added to the peer group of the source
739 * (+++) the mount is propagated to all the mounts in the propagation tree
740 * of the destination mount and the cloned mount is made slave
741 * of the same master as that of the source mount. The cloned mount
742 * is marked as 'shared and slave'.
743 * (*) the cloned mount is made a slave of the same master as that of the
746 * ---------------------------------------------------------------------------
747 * | MOVE MOUNT OPERATION |
748 * |**************************************************************************
749 * | source-->| shared | private | slave | unbindable |
753 * |**************************************************************************
754 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
756 * |non-shared| shared (+*) | private | slave (*) | unbindable |
757 * ***************************************************************************
759 * (+) the mount is moved to the destination. And is then propagated to
760 * all the mounts in the propagation tree of the destination mount.
761 * (+*) the mount is moved to the destination.
762 * (+++) the mount is moved to the destination and is then propagated to
763 * all the mounts belonging to the destination mount's propagation tree.
764 * the mount is marked as 'shared and slave'.
765 * (*) the mount continues to be a slave at the new location.
767 * if the source mount is a tree, the operations explained above is
768 * applied to each mount in the tree.
769 * Must be called without spinlocks held, since this function can sleep
772 static int attach_recursive_mnt(struct vfsmount *source_mnt,
773 struct nameidata *nd, struct nameidata *parent_nd)
775 LIST_HEAD(tree_list);
776 struct vfsmount *dest_mnt = nd->mnt;
777 struct dentry *dest_dentry = nd->dentry;
778 struct vfsmount *child, *p;
780 if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list))
783 if (IS_MNT_SHARED(dest_mnt)) {
784 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
788 spin_lock(&vfsmount_lock);
790 detach_mnt(source_mnt, parent_nd);
791 attach_mnt(source_mnt, nd);
792 touch_namespace(current->namespace);
794 mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
795 commit_tree(source_mnt);
798 list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
799 list_del_init(&child->mnt_hash);
802 spin_unlock(&vfsmount_lock);
806 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
809 if (mnt->mnt_sb->s_flags & MS_NOUSER)
812 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
813 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
817 down(&nd->dentry->d_inode->i_sem);
818 if (IS_DEADDIR(nd->dentry->d_inode))
821 err = security_sb_check_sb(mnt, nd);
826 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry))
827 err = attach_recursive_mnt(mnt, nd, NULL);
829 up(&nd->dentry->d_inode->i_sem);
831 security_sb_post_addmount(mnt, nd);
836 * recursively change the type of the mountpoint.
838 static int do_change_type(struct nameidata *nd, int flag)
840 struct vfsmount *m, *mnt = nd->mnt;
841 int recurse = flag & MS_REC;
842 int type = flag & ~MS_REC;
844 if (nd->dentry != nd->mnt->mnt_root)
847 down_write(&namespace_sem);
848 spin_lock(&vfsmount_lock);
849 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
850 change_mnt_propagation(m, type);
851 spin_unlock(&vfsmount_lock);
852 up_write(&namespace_sem);
859 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
861 struct nameidata old_nd;
862 struct vfsmount *mnt = NULL;
863 int err = mount_is_safe(nd);
866 if (!old_name || !*old_name)
868 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
872 down_write(&namespace_sem);
874 if (IS_MNT_UNBINDABLE(old_nd.mnt))
877 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
882 mnt = copy_tree(old_nd.mnt, old_nd.dentry, 0);
884 mnt = clone_mnt(old_nd.mnt, old_nd.dentry, 0);
889 err = graft_tree(mnt, nd);
891 LIST_HEAD(umount_list);
892 spin_lock(&vfsmount_lock);
893 umount_tree(mnt, 0, &umount_list);
894 spin_unlock(&vfsmount_lock);
895 release_mounts(&umount_list);
899 up_write(&namespace_sem);
900 path_release(&old_nd);
905 * change filesystem flags. dir should be a physical root of filesystem.
906 * If you've mounted a non-root directory somewhere and want to do remount
907 * on it - tough luck.
909 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
913 struct super_block *sb = nd->mnt->mnt_sb;
915 if (!capable(CAP_SYS_ADMIN))
918 if (!check_mnt(nd->mnt))
921 if (nd->dentry != nd->mnt->mnt_root)
924 down_write(&sb->s_umount);
925 err = do_remount_sb(sb, flags, data, 0);
927 nd->mnt->mnt_flags = mnt_flags;
928 up_write(&sb->s_umount);
930 security_sb_post_remount(nd->mnt, flags, data);
934 static inline int tree_contains_unbindable(struct vfsmount *mnt)
937 for (p = mnt; p; p = next_mnt(p, mnt)) {
938 if (IS_MNT_UNBINDABLE(p))
944 static int do_move_mount(struct nameidata *nd, char *old_name)
946 struct nameidata old_nd, parent_nd;
949 if (!capable(CAP_SYS_ADMIN))
951 if (!old_name || !*old_name)
953 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
957 down_write(&namespace_sem);
958 while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
961 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
965 down(&nd->dentry->d_inode->i_sem);
966 if (IS_DEADDIR(nd->dentry->d_inode))
969 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
973 if (old_nd.dentry != old_nd.mnt->mnt_root)
976 if (old_nd.mnt == old_nd.mnt->mnt_parent)
979 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
980 S_ISDIR(old_nd.dentry->d_inode->i_mode))
983 * Don't move a mount residing in a shared parent.
985 if (old_nd.mnt->mnt_parent && IS_MNT_SHARED(old_nd.mnt->mnt_parent))
988 * Don't move a mount tree containing unbindable mounts to a destination
989 * mount which is shared.
991 if (IS_MNT_SHARED(nd->mnt) && tree_contains_unbindable(old_nd.mnt))
994 for (p = nd->mnt; p->mnt_parent != p; p = p->mnt_parent)
998 if ((err = attach_recursive_mnt(old_nd.mnt, nd, &parent_nd)))
1001 spin_lock(&vfsmount_lock);
1002 /* if the mount is moved, it should no longer be expire
1004 list_del_init(&old_nd.mnt->mnt_expire);
1005 spin_unlock(&vfsmount_lock);
1007 up(&nd->dentry->d_inode->i_sem);
1009 up_write(&namespace_sem);
1011 path_release(&parent_nd);
1012 path_release(&old_nd);
1017 * create a new mount for userspace and request it to be added into the
1020 static int do_new_mount(struct nameidata *nd, char *type, int flags,
1021 int mnt_flags, char *name, void *data)
1023 struct vfsmount *mnt;
1025 if (!type || !memchr(type, 0, PAGE_SIZE))
1028 /* we need capabilities... */
1029 if (!capable(CAP_SYS_ADMIN))
1032 mnt = do_kern_mount(type, flags, name, data);
1034 return PTR_ERR(mnt);
1036 return do_add_mount(mnt, nd, mnt_flags, NULL);
1040 * add a mount into a namespace's mount tree
1041 * - provide the option of adding the new mount to an expiration list
1043 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
1044 int mnt_flags, struct list_head *fslist)
1048 down_write(&namespace_sem);
1049 /* Something was mounted here while we slept */
1050 while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
1053 if (!check_mnt(nd->mnt))
1056 /* Refuse the same filesystem on the same mount point */
1058 if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
1059 nd->mnt->mnt_root == nd->dentry)
1063 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
1066 newmnt->mnt_flags = mnt_flags;
1067 if ((err = graft_tree(newmnt, nd)))
1071 /* add to the specified expiration list */
1072 spin_lock(&vfsmount_lock);
1073 list_add_tail(&newmnt->mnt_expire, fslist);
1074 spin_unlock(&vfsmount_lock);
1076 up_write(&namespace_sem);
1080 up_write(&namespace_sem);
1085 EXPORT_SYMBOL_GPL(do_add_mount);
1087 static void expire_mount(struct vfsmount *mnt, struct list_head *mounts,
1088 struct list_head *umounts)
1090 spin_lock(&vfsmount_lock);
1093 * Check if mount is still attached, if not, let whoever holds it deal
1096 if (mnt->mnt_parent == mnt) {
1097 spin_unlock(&vfsmount_lock);
1102 * Check that it is still dead: the count should now be 2 - as
1103 * contributed by the vfsmount parent and the mntget above
1105 if (!propagate_mount_busy(mnt, 2)) {
1106 /* delete from the namespace */
1107 touch_namespace(mnt->mnt_namespace);
1108 list_del_init(&mnt->mnt_list);
1109 mnt->mnt_namespace = NULL;
1110 umount_tree(mnt, 1, umounts);
1111 spin_unlock(&vfsmount_lock);
1114 * Someone brought it back to life whilst we didn't have any
1115 * locks held so return it to the expiration list
1117 list_add_tail(&mnt->mnt_expire, mounts);
1118 spin_unlock(&vfsmount_lock);
1123 * process a list of expirable mountpoints with the intent of discarding any
1124 * mountpoints that aren't in use and haven't been touched since last we came
1127 void mark_mounts_for_expiry(struct list_head *mounts)
1129 struct namespace *namespace;
1130 struct vfsmount *mnt, *next;
1131 LIST_HEAD(graveyard);
1133 if (list_empty(mounts))
1136 spin_lock(&vfsmount_lock);
1138 /* extract from the expiration list every vfsmount that matches the
1139 * following criteria:
1140 * - only referenced by its parent vfsmount
1141 * - still marked for expiry (marked on the last call here; marks are
1142 * cleared by mntput())
1144 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
1145 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1146 atomic_read(&mnt->mnt_count) != 1)
1150 list_move(&mnt->mnt_expire, &graveyard);
1154 * go through the vfsmounts we've just consigned to the graveyard to
1155 * - check that they're still dead
1156 * - delete the vfsmount from the appropriate namespace under lock
1157 * - dispose of the corpse
1159 while (!list_empty(&graveyard)) {
1161 mnt = list_entry(graveyard.next, struct vfsmount, mnt_expire);
1162 list_del_init(&mnt->mnt_expire);
1164 /* don't do anything if the namespace is dead - all the
1165 * vfsmounts from it are going away anyway */
1166 namespace = mnt->mnt_namespace;
1167 if (!namespace || !namespace->root)
1169 get_namespace(namespace);
1171 spin_unlock(&vfsmount_lock);
1172 down_write(&namespace_sem);
1173 expire_mount(mnt, mounts, &umounts);
1174 up_write(&namespace_sem);
1175 release_mounts(&umounts);
1177 put_namespace(namespace);
1178 spin_lock(&vfsmount_lock);
1181 spin_unlock(&vfsmount_lock);
1184 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
1187 * Some copy_from_user() implementations do not return the exact number of
1188 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
1189 * Note that this function differs from copy_from_user() in that it will oops
1190 * on bad values of `to', rather than returning a short copy.
1192 static long exact_copy_from_user(void *to, const void __user * from,
1196 const char __user *f = from;
1199 if (!access_ok(VERIFY_READ, from, n))
1203 if (__get_user(c, f)) {
1214 int copy_mount_options(const void __user * data, unsigned long *where)
1224 if (!(page = __get_free_page(GFP_KERNEL)))
1227 /* We only care that *some* data at the address the user
1228 * gave us is valid. Just in case, we'll zero
1229 * the remainder of the page.
1231 /* copy_from_user cannot cross TASK_SIZE ! */
1232 size = TASK_SIZE - (unsigned long)data;
1233 if (size > PAGE_SIZE)
1236 i = size - exact_copy_from_user((void *)page, data, size);
1242 memset((char *)page + i, 0, PAGE_SIZE - i);
1248 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1249 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1251 * data is a (void *) that can point to any structure up to
1252 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1253 * information (or be NULL).
1255 * Pre-0.97 versions of mount() didn't have a flags word.
1256 * When the flags word was introduced its top half was required
1257 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1258 * Therefore, if this magic number is present, it carries no information
1259 * and must be discarded.
1261 long do_mount(char *dev_name, char *dir_name, char *type_page,
1262 unsigned long flags, void *data_page)
1264 struct nameidata nd;
1269 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1270 flags &= ~MS_MGC_MSK;
1272 /* Basic sanity checks */
1274 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1276 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1280 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1282 /* Separate the per-mountpoint flags */
1283 if (flags & MS_NOSUID)
1284 mnt_flags |= MNT_NOSUID;
1285 if (flags & MS_NODEV)
1286 mnt_flags |= MNT_NODEV;
1287 if (flags & MS_NOEXEC)
1288 mnt_flags |= MNT_NOEXEC;
1289 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE);
1291 /* ... and get the mountpoint */
1292 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1296 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1300 if (flags & MS_REMOUNT)
1301 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1303 else if (flags & MS_BIND)
1304 retval = do_loopback(&nd, dev_name, flags & MS_REC);
1305 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1306 retval = do_change_type(&nd, flags);
1307 else if (flags & MS_MOVE)
1308 retval = do_move_mount(&nd, dev_name);
1310 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1311 dev_name, data_page);
1317 int copy_namespace(int flags, struct task_struct *tsk)
1319 struct namespace *namespace = tsk->namespace;
1320 struct namespace *new_ns;
1321 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1322 struct fs_struct *fs = tsk->fs;
1323 struct vfsmount *p, *q;
1328 get_namespace(namespace);
1330 if (!(flags & CLONE_NEWNS))
1333 if (!capable(CAP_SYS_ADMIN)) {
1334 put_namespace(namespace);
1338 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1342 atomic_set(&new_ns->count, 1);
1343 INIT_LIST_HEAD(&new_ns->list);
1344 init_waitqueue_head(&new_ns->poll);
1347 down_write(&namespace_sem);
1348 /* First pass: copy the tree topology */
1349 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root,
1350 CL_COPY_ALL | CL_EXPIRE);
1351 if (!new_ns->root) {
1352 up_write(&namespace_sem);
1356 spin_lock(&vfsmount_lock);
1357 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1358 spin_unlock(&vfsmount_lock);
1361 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1362 * as belonging to new namespace. We have already acquired a private
1363 * fs_struct, so tsk->fs->lock is not needed.
1365 p = namespace->root;
1368 q->mnt_namespace = new_ns;
1370 if (p == fs->rootmnt) {
1372 fs->rootmnt = mntget(q);
1374 if (p == fs->pwdmnt) {
1376 fs->pwdmnt = mntget(q);
1378 if (p == fs->altrootmnt) {
1380 fs->altrootmnt = mntget(q);
1383 p = next_mnt(p, namespace->root);
1384 q = next_mnt(q, new_ns->root);
1386 up_write(&namespace_sem);
1388 tsk->namespace = new_ns;
1397 put_namespace(namespace);
1401 put_namespace(namespace);
1405 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1406 char __user * type, unsigned long flags,
1410 unsigned long data_page;
1411 unsigned long type_page;
1412 unsigned long dev_page;
1415 retval = copy_mount_options(type, &type_page);
1419 dir_page = getname(dir_name);
1420 retval = PTR_ERR(dir_page);
1421 if (IS_ERR(dir_page))
1424 retval = copy_mount_options(dev_name, &dev_page);
1428 retval = copy_mount_options(data, &data_page);
1433 retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
1434 flags, (void *)data_page);
1436 free_page(data_page);
1439 free_page(dev_page);
1443 free_page(type_page);
1448 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1449 * It can block. Requires the big lock held.
1451 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1452 struct dentry *dentry)
1454 struct dentry *old_root;
1455 struct vfsmount *old_rootmnt;
1456 write_lock(&fs->lock);
1457 old_root = fs->root;
1458 old_rootmnt = fs->rootmnt;
1459 fs->rootmnt = mntget(mnt);
1460 fs->root = dget(dentry);
1461 write_unlock(&fs->lock);
1464 mntput(old_rootmnt);
1469 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1470 * It can block. Requires the big lock held.
1472 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1473 struct dentry *dentry)
1475 struct dentry *old_pwd;
1476 struct vfsmount *old_pwdmnt;
1478 write_lock(&fs->lock);
1480 old_pwdmnt = fs->pwdmnt;
1481 fs->pwdmnt = mntget(mnt);
1482 fs->pwd = dget(dentry);
1483 write_unlock(&fs->lock);
1491 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1493 struct task_struct *g, *p;
1494 struct fs_struct *fs;
1496 read_lock(&tasklist_lock);
1497 do_each_thread(g, p) {
1501 atomic_inc(&fs->count);
1503 if (fs->root == old_nd->dentry
1504 && fs->rootmnt == old_nd->mnt)
1505 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1506 if (fs->pwd == old_nd->dentry
1507 && fs->pwdmnt == old_nd->mnt)
1508 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1512 } while_each_thread(g, p);
1513 read_unlock(&tasklist_lock);
1517 * pivot_root Semantics:
1518 * Moves the root file system of the current process to the directory put_old,
1519 * makes new_root as the new root file system of the current process, and sets
1520 * root/cwd of all processes which had them on the current root to new_root.
1523 * The new_root and put_old must be directories, and must not be on the
1524 * same file system as the current process root. The put_old must be
1525 * underneath new_root, i.e. adding a non-zero number of /.. to the string
1526 * pointed to by put_old must yield the same directory as new_root. No other
1527 * file system may be mounted on put_old. After all, new_root is a mountpoint.
1530 * - we don't move root/cwd if they are not at the root (reason: if something
1531 * cared enough to change them, it's probably wrong to force them elsewhere)
1532 * - it's okay to pick a root that isn't the root of a file system, e.g.
1533 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1534 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1537 asmlinkage long sys_pivot_root(const char __user * new_root,
1538 const char __user * put_old)
1540 struct vfsmount *tmp;
1541 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1544 if (!capable(CAP_SYS_ADMIN))
1549 error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1554 if (!check_mnt(new_nd.mnt))
1557 error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
1561 error = security_sb_pivotroot(&old_nd, &new_nd);
1563 path_release(&old_nd);
1567 read_lock(¤t->fs->lock);
1568 user_nd.mnt = mntget(current->fs->rootmnt);
1569 user_nd.dentry = dget(current->fs->root);
1570 read_unlock(¤t->fs->lock);
1571 down_write(&namespace_sem);
1572 down(&old_nd.dentry->d_inode->i_sem);
1574 if (IS_MNT_SHARED(old_nd.mnt) ||
1575 IS_MNT_SHARED(new_nd.mnt->mnt_parent) ||
1576 IS_MNT_SHARED(user_nd.mnt->mnt_parent))
1578 if (!check_mnt(user_nd.mnt))
1581 if (IS_DEADDIR(new_nd.dentry->d_inode))
1583 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1585 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1588 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1589 goto out2; /* loop, on the same file system */
1591 if (user_nd.mnt->mnt_root != user_nd.dentry)
1592 goto out2; /* not a mountpoint */
1593 if (user_nd.mnt->mnt_parent == user_nd.mnt)
1594 goto out2; /* not attached */
1595 if (new_nd.mnt->mnt_root != new_nd.dentry)
1596 goto out2; /* not a mountpoint */
1597 if (new_nd.mnt->mnt_parent == new_nd.mnt)
1598 goto out2; /* not attached */
1599 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1600 spin_lock(&vfsmount_lock);
1601 if (tmp != new_nd.mnt) {
1603 if (tmp->mnt_parent == tmp)
1604 goto out3; /* already mounted on put_old */
1605 if (tmp->mnt_parent == new_nd.mnt)
1607 tmp = tmp->mnt_parent;
1609 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1611 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1613 detach_mnt(new_nd.mnt, &parent_nd);
1614 detach_mnt(user_nd.mnt, &root_parent);
1615 attach_mnt(user_nd.mnt, &old_nd); /* mount old root on put_old */
1616 attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */
1617 touch_namespace(current->namespace);
1618 spin_unlock(&vfsmount_lock);
1619 chroot_fs_refs(&user_nd, &new_nd);
1620 security_sb_post_pivotroot(&user_nd, &new_nd);
1622 path_release(&root_parent);
1623 path_release(&parent_nd);
1625 up(&old_nd.dentry->d_inode->i_sem);
1626 up_write(&namespace_sem);
1627 path_release(&user_nd);
1628 path_release(&old_nd);
1630 path_release(&new_nd);
1635 spin_unlock(&vfsmount_lock);
1639 static void __init init_mount_tree(void)
1641 struct vfsmount *mnt;
1642 struct namespace *namespace;
1643 struct task_struct *g, *p;
1645 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1647 panic("Can't create rootfs");
1648 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1650 panic("Can't allocate initial namespace");
1651 atomic_set(&namespace->count, 1);
1652 INIT_LIST_HEAD(&namespace->list);
1653 init_waitqueue_head(&namespace->poll);
1654 namespace->event = 0;
1655 list_add(&mnt->mnt_list, &namespace->list);
1656 namespace->root = mnt;
1657 mnt->mnt_namespace = namespace;
1659 init_task.namespace = namespace;
1660 read_lock(&tasklist_lock);
1661 do_each_thread(g, p) {
1662 get_namespace(namespace);
1663 p->namespace = namespace;
1664 } while_each_thread(g, p);
1665 read_unlock(&tasklist_lock);
1667 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1668 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1671 void __init mnt_init(unsigned long mempages)
1673 struct list_head *d;
1674 unsigned int nr_hash;
1677 init_rwsem(&namespace_sem);
1679 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1680 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL, NULL);
1682 mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
1684 if (!mount_hashtable)
1685 panic("Failed to allocate mount hash table\n");
1688 * Find the power-of-two list-heads that can fit into the allocation..
1689 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1692 nr_hash = PAGE_SIZE / sizeof(struct list_head);
1696 } while ((nr_hash >> hash_bits) != 0);
1700 * Re-calculate the actual number of entries and the mask
1701 * from the number of bits we can fit.
1703 nr_hash = 1UL << hash_bits;
1704 hash_mask = nr_hash - 1;
1706 printk("Mount-cache hash table entries: %d\n", nr_hash);
1708 /* And initialize the newly allocated array */
1709 d = mount_hashtable;
1721 void __put_namespace(struct namespace *namespace)
1723 struct vfsmount *root = namespace->root;
1724 LIST_HEAD(umount_list);
1725 namespace->root = NULL;
1726 spin_unlock(&vfsmount_lock);
1727 down_write(&namespace_sem);
1728 spin_lock(&vfsmount_lock);
1729 umount_tree(root, 0, &umount_list);
1730 spin_unlock(&vfsmount_lock);
1731 up_write(&namespace_sem);
1732 release_mounts(&umount_list);