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 <linux/vserver/namespace.h>
26 #include <linux/vserver/xid.h>
27 #include <asm/uaccess.h>
28 #include <asm/unistd.h>
30 extern int __init init_rootfs(void);
33 extern int __init sysfs_init(void);
35 static inline int sysfs_init(void)
41 /* spinlock for vfsmount related operations, inplace of dcache_lock */
42 __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
44 static struct list_head *mount_hashtable;
45 static int hash_mask, hash_bits;
46 static kmem_cache_t *mnt_cache;
48 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
50 unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
51 tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
52 tmp = tmp + (tmp >> hash_bits);
53 return tmp & hash_mask;
56 struct vfsmount *alloc_vfsmnt(const char *name)
58 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
60 memset(mnt, 0, sizeof(struct vfsmount));
61 atomic_set(&mnt->mnt_count,1);
62 INIT_LIST_HEAD(&mnt->mnt_hash);
63 INIT_LIST_HEAD(&mnt->mnt_child);
64 INIT_LIST_HEAD(&mnt->mnt_mounts);
65 INIT_LIST_HEAD(&mnt->mnt_list);
66 INIT_LIST_HEAD(&mnt->mnt_fslink);
68 int size = strlen(name)+1;
69 char *newname = kmalloc(size, GFP_KERNEL);
71 memcpy(newname, name, size);
72 mnt->mnt_devname = newname;
79 void free_vfsmnt(struct vfsmount *mnt)
81 kfree(mnt->mnt_devname);
82 kmem_cache_free(mnt_cache, mnt);
86 * Now, lookup_mnt increments the ref count before returning
87 * the vfsmount struct.
89 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
91 struct list_head * head = mount_hashtable + hash(mnt, dentry);
92 struct list_head * tmp = head;
93 struct vfsmount *p, *found = NULL;
95 spin_lock(&vfsmount_lock);
101 p = list_entry(tmp, struct vfsmount, mnt_hash);
102 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
107 spin_unlock(&vfsmount_lock);
111 static inline int check_mnt(struct vfsmount *mnt)
113 return mnt->mnt_namespace == current->namespace;
116 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
118 old_nd->dentry = mnt->mnt_mountpoint;
119 old_nd->mnt = mnt->mnt_parent;
120 mnt->mnt_parent = mnt;
121 mnt->mnt_mountpoint = mnt->mnt_root;
122 list_del_init(&mnt->mnt_child);
123 list_del_init(&mnt->mnt_hash);
124 old_nd->dentry->d_mounted--;
127 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
129 mnt->mnt_parent = mntget(nd->mnt);
130 mnt->mnt_mountpoint = dget(nd->dentry);
131 list_add(&mnt->mnt_hash, mount_hashtable+hash(nd->mnt, nd->dentry));
132 list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
133 nd->dentry->d_mounted++;
136 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
138 struct list_head *next = p->mnt_mounts.next;
139 if (next == &p->mnt_mounts) {
143 next = p->mnt_child.next;
144 if (next != &p->mnt_parent->mnt_mounts)
149 return list_entry(next, struct vfsmount, mnt_child);
152 static struct vfsmount *
153 clone_mnt(struct vfsmount *old, struct dentry *root)
155 struct super_block *sb = old->mnt_sb;
156 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
159 mnt->mnt_flags = old->mnt_flags;
160 atomic_inc(&sb->s_active);
162 mnt->mnt_root = dget(root);
163 mnt->mnt_mountpoint = mnt->mnt_root;
164 mnt->mnt_parent = mnt;
165 mnt->mnt_namespace = old->mnt_namespace;
166 mnt->mnt_xid = old->mnt_xid;
168 /* stick the duplicate mount on the same expiry list
169 * as the original if that was on one */
170 spin_lock(&vfsmount_lock);
171 if (!list_empty(&old->mnt_fslink))
172 list_add(&mnt->mnt_fslink, &old->mnt_fslink);
173 spin_unlock(&vfsmount_lock);
178 void __mntput(struct vfsmount *mnt)
180 struct super_block *sb = mnt->mnt_sb;
183 deactivate_super(sb);
186 EXPORT_SYMBOL(__mntput);
189 static void *m_start(struct seq_file *m, loff_t *pos)
191 struct namespace *n = m->private;
196 list_for_each(p, &n->list)
198 return list_entry(p, struct vfsmount, mnt_list);
202 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
204 struct namespace *n = m->private;
205 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
207 return p==&n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
210 static void m_stop(struct seq_file *m, void *v)
212 struct namespace *n = m->private;
216 static inline void mangle(struct seq_file *m, const char *s)
218 seq_escape(m, s, " \t\n\\");
221 static int mnt_is_reachable(struct vfsmount *mnt)
223 struct vfsmount *root_mnt;
224 struct dentry *root, *point;
227 if (mnt == mnt->mnt_namespace->root)
230 spin_lock(&dcache_lock);
231 root_mnt = current->fs->rootmnt;
232 root = current->fs->root;
235 while ((mnt != mnt->mnt_parent) && (mnt != root_mnt)) {
236 point = mnt->mnt_mountpoint;
237 mnt = mnt->mnt_parent;
240 ret = (mnt == root_mnt) && is_subdir(point, root);
242 spin_unlock(&dcache_lock);
247 static int show_vfsmnt(struct seq_file *m, void *v)
249 struct vfsmount *mnt = v;
251 static struct proc_fs_info {
257 { MS_RDONLY, MNT_RDONLY, "ro", "rw" },
258 { MS_SYNCHRONOUS, 0, ",sync", NULL },
259 { MS_DIRSYNC, 0, ",dirsync", NULL },
260 { MS_MANDLOCK, 0, ",mand", NULL },
261 { MS_NOATIME, MNT_NOATIME, ",noatime", NULL },
262 { MS_NODIRATIME, MNT_NODIRATIME, ",nodiratime", NULL },
263 { MS_TAGXID, MS_TAGXID, ",tagxid", NULL },
264 { 0, MNT_NOSUID, ",nosuid", NULL },
265 { 0, MNT_NODEV, ",nodev", NULL },
266 { 0, MNT_NOEXEC, ",noexec", NULL },
269 struct proc_fs_info *p;
270 unsigned long s_flags = mnt->mnt_sb->s_flags;
271 int mnt_flags = mnt->mnt_flags;
273 if (vx_flags(VXF_HIDE_MOUNT, 0))
275 if (!mnt_is_reachable(mnt))
278 if (!vx_check(0, VX_ADMIN|VX_WATCH) &&
279 mnt == current->fs->rootmnt) {
280 seq_puts(m, "/dev/root / ");
282 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
284 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
287 mangle(m, mnt->mnt_sb->s_type->name);
289 for (p = fs_info; (p->s_flag | p->mnt_flag) ; p++) {
290 if ((s_flags & p->s_flag) || (mnt_flags & p->mnt_flag)) {
292 seq_puts(m, p->set_str);
295 seq_puts(m, p->unset_str);
298 if (mnt->mnt_flags & MNT_XID)
299 seq_printf(m, ",xid=%d", mnt->mnt_xid);
300 if (mnt->mnt_sb->s_op->show_options)
301 err = mnt->mnt_sb->s_op->show_options(m, mnt);
302 seq_puts(m, " 0 0\n");
306 struct seq_operations mounts_op = {
314 * may_umount_tree - check if a mount tree is busy
315 * @mnt: root of mount tree
317 * This is called to check if a tree of mounts has any
318 * open files, pwds, chroots or sub mounts that are
321 int may_umount_tree(struct vfsmount *mnt)
323 struct list_head *next;
324 struct vfsmount *this_parent = mnt;
328 spin_lock(&vfsmount_lock);
329 actual_refs = atomic_read(&mnt->mnt_count);
332 next = this_parent->mnt_mounts.next;
334 while (next != &this_parent->mnt_mounts) {
335 struct vfsmount *p = list_entry(next, struct vfsmount, mnt_child);
339 actual_refs += atomic_read(&p->mnt_count);
342 if (!list_empty(&p->mnt_mounts)) {
348 if (this_parent != mnt) {
349 next = this_parent->mnt_child.next;
350 this_parent = this_parent->mnt_parent;
353 spin_unlock(&vfsmount_lock);
355 if (actual_refs > minimum_refs)
361 EXPORT_SYMBOL(may_umount_tree);
364 * may_umount - check if a mount point is busy
365 * @mnt: root of mount
367 * This is called to check if a mount point has any
368 * open files, pwds, chroots or sub mounts. If the
369 * mount has sub mounts this will return busy
370 * regardless of whether the sub mounts are busy.
372 * Doesn't take quota and stuff into account. IOW, in some cases it will
373 * give false negatives. The main reason why it's here is that we need
374 * a non-destructive way to look for easily umountable filesystems.
376 int may_umount(struct vfsmount *mnt)
378 if (atomic_read(&mnt->mnt_count) > 2)
383 EXPORT_SYMBOL(may_umount);
385 static inline void __umount_list(struct list_head *kill)
387 struct vfsmount *mnt;
389 while (!list_empty(kill)) {
390 mnt = list_entry(kill->next, struct vfsmount, mnt_list);
391 list_del_init(&mnt->mnt_list);
392 list_del_init(&mnt->mnt_fslink);
393 if (mnt->mnt_parent == mnt) {
394 spin_unlock(&vfsmount_lock);
396 struct nameidata old_nd;
397 detach_mnt(mnt, &old_nd);
398 spin_unlock(&vfsmount_lock);
399 path_release(&old_nd);
402 spin_lock(&vfsmount_lock);
406 void umount_tree(struct vfsmount *mnt)
411 for (p = mnt; p; p = next_mnt(p, mnt)) {
412 list_del(&p->mnt_list);
413 list_add(&p->mnt_list, &kill);
415 __umount_list(&kill);
418 void umount_unused(struct vfsmount *mnt, struct fs_struct *fs)
423 for (p = mnt; p; p = next_mnt(p, mnt)) {
424 if (p == fs->rootmnt || p == fs->pwdmnt)
426 list_del(&p->mnt_list);
427 list_add(&p->mnt_list, &kill);
429 __umount_list(&kill);
432 static int do_umount(struct vfsmount *mnt, int flags)
434 struct super_block * sb = mnt->mnt_sb;
437 retval = security_sb_umount(mnt, flags);
442 * Allow userspace to request a mountpoint be expired rather than
443 * unmounting unconditionally. Unmount only happens if:
444 * (1) the mark is already set (the mark is cleared by mntput())
445 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
447 if (flags & MNT_EXPIRE) {
448 if (mnt == current->fs->rootmnt ||
449 flags & (MNT_FORCE | MNT_DETACH))
452 if (atomic_read(&mnt->mnt_count) != 2)
455 if (!xchg(&mnt->mnt_expiry_mark, 1))
460 * If we may have to abort operations to get out of this
461 * mount, and they will themselves hold resources we must
462 * allow the fs to do things. In the Unix tradition of
463 * 'Gee thats tricky lets do it in userspace' the umount_begin
464 * might fail to complete on the first run through as other tasks
465 * must return, and the like. Thats for the mount program to worry
466 * about for the moment.
470 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
471 sb->s_op->umount_begin(sb);
475 * No sense to grab the lock for this test, but test itself looks
476 * somewhat bogus. Suggestions for better replacement?
477 * Ho-hum... In principle, we might treat that as umount + switch
478 * to rootfs. GC would eventually take care of the old vfsmount.
479 * Actually it makes sense, especially if rootfs would contain a
480 * /reboot - static binary that would close all descriptors and
481 * call reboot(9). Then init(8) could umount root and exec /reboot.
483 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
485 * Special case for "unmounting" root ...
486 * we just try to remount it readonly.
488 down_write(&sb->s_umount);
489 if (!(sb->s_flags & MS_RDONLY)) {
492 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
495 up_write(&sb->s_umount);
499 down_write(¤t->namespace->sem);
500 spin_lock(&vfsmount_lock);
502 if (atomic_read(&sb->s_active) == 1) {
503 /* last instance - try to be smart */
504 spin_unlock(&vfsmount_lock);
509 security_sb_umount_close(mnt);
510 spin_lock(&vfsmount_lock);
513 if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
514 if (!list_empty(&mnt->mnt_list))
518 spin_unlock(&vfsmount_lock);
520 security_sb_umount_busy(mnt);
521 up_write(¤t->namespace->sem);
526 * Now umount can handle mount points as well as block devices.
527 * This is important for filesystems which use unnamed block devices.
529 * We now support a flag for forced unmount like the other 'big iron'
530 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
533 asmlinkage long sys_umount(char __user * name, int flags)
538 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
542 if (nd.dentry != nd.mnt->mnt_root)
544 if (!check_mnt(nd.mnt))
548 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
551 retval = do_umount(nd.mnt, flags);
553 path_release_on_umount(&nd);
558 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
561 * The 2.0 compatible umount. No flags.
564 asmlinkage long sys_oldumount(char __user * name)
566 return sys_umount(name,0);
571 static int mount_is_safe(struct nameidata *nd)
573 if (capable(CAP_SYS_ADMIN))
575 if (vx_ccaps(VXC_SECURE_MOUNT))
579 if (S_ISLNK(nd->dentry->d_inode->i_mode))
581 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
582 if (current->uid != nd->dentry->d_inode->i_uid)
585 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
592 lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
597 if (d == NULL || d == d->d_parent)
603 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
605 struct vfsmount *res, *p, *q, *r, *s;
609 res = q = clone_mnt(mnt, dentry);
612 q->mnt_mountpoint = mnt->mnt_mountpoint;
615 for (h = mnt->mnt_mounts.next; h != &mnt->mnt_mounts; h = h->next) {
616 r = list_entry(h, struct vfsmount, mnt_child);
617 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
620 for (s = r; s; s = next_mnt(s, r)) {
621 while (p != s->mnt_parent) {
627 nd.dentry = p->mnt_mountpoint;
628 q = clone_mnt(p, p->mnt_root);
631 spin_lock(&vfsmount_lock);
632 list_add_tail(&q->mnt_list, &res->mnt_list);
634 spin_unlock(&vfsmount_lock);
640 spin_lock(&vfsmount_lock);
642 spin_unlock(&vfsmount_lock);
647 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
650 if (mnt->mnt_sb->s_flags & MS_NOUSER)
653 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
654 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
658 down(&nd->dentry->d_inode->i_sem);
659 if (IS_DEADDIR(nd->dentry->d_inode))
662 err = security_sb_check_sb(mnt, nd);
667 spin_lock(&vfsmount_lock);
668 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
669 struct list_head head;
672 list_add_tail(&head, &mnt->mnt_list);
673 list_splice(&head, current->namespace->list.prev);
677 spin_unlock(&vfsmount_lock);
679 up(&nd->dentry->d_inode->i_sem);
681 security_sb_post_addmount(mnt, nd);
688 static int do_loopback(struct nameidata *nd, char *old_name, xid_t xid, unsigned long flags, int mnt_flags)
690 struct nameidata old_nd;
691 struct vfsmount *mnt = NULL;
692 int recurse = flags & MS_REC;
693 int err = mount_is_safe(nd);
697 if (!old_name || !*old_name)
699 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
703 down_write(¤t->namespace->sem);
705 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
708 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
710 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
714 /* stop bind mounts from expiring */
715 spin_lock(&vfsmount_lock);
716 list_del_init(&mnt->mnt_fslink);
717 spin_unlock(&vfsmount_lock);
719 if (flags & MS_XID) {
721 mnt->mnt_flags |= MNT_XID;
723 err = graft_tree(mnt, nd);
725 spin_lock(&vfsmount_lock);
727 spin_unlock(&vfsmount_lock);
730 mnt->mnt_flags = mnt_flags;
733 up_write(¤t->namespace->sem);
734 path_release(&old_nd);
739 * change filesystem flags. dir should be a physical root of filesystem.
740 * If you've mounted a non-root directory somewhere and want to do remount
741 * on it - tough luck.
744 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
745 void *data, xid_t xid)
748 struct super_block * sb = nd->mnt->mnt_sb;
750 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_REMOUNT))
753 if (!check_mnt(nd->mnt))
756 if (nd->dentry != nd->mnt->mnt_root)
759 if (vx_ccaps(VXC_SECURE_REMOUNT))
760 mnt_flags |= MNT_NODEV;
761 down_write(&sb->s_umount);
762 err = do_remount_sb(sb, flags, data, 0);
764 nd->mnt->mnt_flags=mnt_flags;
766 nd->mnt->mnt_xid = xid;
768 up_write(&sb->s_umount);
770 security_sb_post_remount(nd->mnt, flags, data);
774 static int do_move_mount(struct nameidata *nd, char *old_name)
776 struct nameidata old_nd, parent_nd;
779 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
781 if (!old_name || !*old_name)
783 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
787 down_write(¤t->namespace->sem);
788 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
791 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
795 down(&nd->dentry->d_inode->i_sem);
796 if (IS_DEADDIR(nd->dentry->d_inode))
799 spin_lock(&vfsmount_lock);
800 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
804 if (old_nd.dentry != old_nd.mnt->mnt_root)
807 if (old_nd.mnt == old_nd.mnt->mnt_parent)
810 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
811 S_ISDIR(old_nd.dentry->d_inode->i_mode))
815 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
820 detach_mnt(old_nd.mnt, &parent_nd);
821 attach_mnt(old_nd.mnt, nd);
823 /* if the mount is moved, it should no longer be expire
825 list_del_init(&old_nd.mnt->mnt_fslink);
827 spin_unlock(&vfsmount_lock);
829 up(&nd->dentry->d_inode->i_sem);
831 up_write(¤t->namespace->sem);
833 path_release(&parent_nd);
834 path_release(&old_nd);
839 * create a new mount for userspace and request it to be added into the
842 static int do_new_mount(struct nameidata *nd, char *type, int flags,
843 int mnt_flags, char *name, void *data)
845 struct vfsmount *mnt;
847 if (!type || !memchr(type, 0, PAGE_SIZE))
850 /* we need capabilities... */
851 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
854 mnt = do_kern_mount(type, flags, name, data);
858 return do_add_mount(mnt, nd, mnt_flags, NULL);
862 * add a mount into a namespace's mount tree
863 * - provide the option of adding the new mount to an expiration list
865 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
866 int mnt_flags, struct list_head *fslist)
870 down_write(¤t->namespace->sem);
871 /* Something was mounted here while we slept */
872 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
875 if (!check_mnt(nd->mnt))
878 /* Refuse the same filesystem on the same mount point */
880 if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
881 nd->mnt->mnt_root == nd->dentry)
885 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
888 newmnt->mnt_flags = mnt_flags;
889 err = graft_tree(newmnt, nd);
891 if (err == 0 && fslist) {
892 /* add to the specified expiration list */
893 spin_lock(&vfsmount_lock);
894 list_add_tail(&newmnt->mnt_fslink, fslist);
895 spin_unlock(&vfsmount_lock);
899 up_write(¤t->namespace->sem);
904 EXPORT_SYMBOL_GPL(do_add_mount);
907 * process a list of expirable mountpoints with the intent of discarding any
908 * mountpoints that aren't in use and haven't been touched since last we came
911 void mark_mounts_for_expiry(struct list_head *mounts)
913 struct namespace *namespace;
914 struct vfsmount *mnt, *next;
915 LIST_HEAD(graveyard);
917 if (list_empty(mounts))
920 spin_lock(&vfsmount_lock);
922 /* extract from the expiration list every vfsmount that matches the
923 * following criteria:
924 * - only referenced by its parent vfsmount
925 * - still marked for expiry (marked on the last call here; marks are
926 * cleared by mntput())
928 list_for_each_entry_safe(mnt, next, mounts, mnt_fslink) {
929 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
930 atomic_read(&mnt->mnt_count) != 1)
934 list_move(&mnt->mnt_fslink, &graveyard);
938 * go through the vfsmounts we've just consigned to the graveyard to
939 * - check that they're still dead
940 * - delete the vfsmount from the appropriate namespace under lock
941 * - dispose of the corpse
943 while (!list_empty(&graveyard)) {
944 mnt = list_entry(graveyard.next, struct vfsmount, mnt_fslink);
945 list_del_init(&mnt->mnt_fslink);
947 /* don't do anything if the namespace is dead - all the
948 * vfsmounts from it are going away anyway */
949 namespace = mnt->mnt_namespace;
950 if (!namespace || atomic_read(&namespace->count) <= 0)
952 get_namespace(namespace);
954 spin_unlock(&vfsmount_lock);
955 down_write(&namespace->sem);
956 spin_lock(&vfsmount_lock);
958 /* check that it is still dead: the count should now be 2 - as
959 * contributed by the vfsmount parent and the mntget above */
960 if (atomic_read(&mnt->mnt_count) == 2) {
961 struct vfsmount *xdmnt;
962 struct dentry *xdentry;
964 /* delete from the namespace */
965 list_del_init(&mnt->mnt_list);
966 list_del_init(&mnt->mnt_child);
967 list_del_init(&mnt->mnt_hash);
968 mnt->mnt_mountpoint->d_mounted--;
970 xdentry = mnt->mnt_mountpoint;
971 mnt->mnt_mountpoint = mnt->mnt_root;
972 xdmnt = mnt->mnt_parent;
973 mnt->mnt_parent = mnt;
975 spin_unlock(&vfsmount_lock);
980 /* now lay it to rest if this was the last ref on the
982 if (atomic_read(&mnt->mnt_sb->s_active) == 1) {
983 /* last instance - try to be smart */
985 DQUOT_OFF(mnt->mnt_sb);
986 acct_auto_close(mnt->mnt_sb);
992 /* someone brought it back to life whilst we didn't
993 * have any locks held so return it to the expiration
995 list_add_tail(&mnt->mnt_fslink, mounts);
996 spin_unlock(&vfsmount_lock);
999 up_write(&namespace->sem);
1002 put_namespace(namespace);
1004 spin_lock(&vfsmount_lock);
1007 spin_unlock(&vfsmount_lock);
1010 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
1013 * Some copy_from_user() implementations do not return the exact number of
1014 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
1015 * Note that this function differs from copy_from_user() in that it will oops
1016 * on bad values of `to', rather than returning a short copy.
1019 exact_copy_from_user(void *to, const void __user *from, unsigned long n)
1022 const char __user *f = from;
1025 if (!access_ok(VERIFY_READ, from, n))
1029 if (__get_user(c, f)) {
1040 int copy_mount_options(const void __user *data, unsigned long *where)
1050 if (!(page = __get_free_page(GFP_KERNEL)))
1053 /* We only care that *some* data at the address the user
1054 * gave us is valid. Just in case, we'll zero
1055 * the remainder of the page.
1057 /* copy_from_user cannot cross TASK_SIZE ! */
1058 size = TASK_SIZE - (unsigned long)data;
1059 if (size > PAGE_SIZE)
1062 i = size - exact_copy_from_user((void *)page, data, size);
1068 memset((char *)page + i, 0, PAGE_SIZE - i);
1074 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1075 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1077 * data is a (void *) that can point to any structure up to
1078 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1079 * information (or be NULL).
1081 * Pre-0.97 versions of mount() didn't have a flags word.
1082 * When the flags word was introduced its top half was required
1083 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1084 * Therefore, if this magic number is present, it carries no information
1085 * and must be discarded.
1087 long do_mount(char * dev_name, char * dir_name, char *type_page,
1088 unsigned long flags, void *data_page)
1090 struct nameidata nd;
1096 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1097 flags &= ~MS_MGC_MSK;
1099 /* Basic sanity checks */
1101 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1103 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1107 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1109 retval = vx_parse_xid(data_page, &xid, 1);
1111 mnt_flags |= MNT_XID;
1112 /* bind and re-mounts get xid flag */
1113 if (flags & (MS_BIND|MS_REMOUNT))
1117 /* Separate the per-mountpoint flags */
1118 if (flags & MS_RDONLY)
1119 mnt_flags |= MNT_RDONLY;
1120 if (flags & MS_NOSUID)
1121 mnt_flags |= MNT_NOSUID;
1122 if (flags & MS_NODEV)
1123 mnt_flags |= MNT_NODEV;
1124 if (flags & MS_NOEXEC)
1125 mnt_flags |= MNT_NOEXEC;
1126 if (flags & MS_NOATIME)
1127 mnt_flags |= MNT_NOATIME;
1128 if (flags & MS_NODIRATIME)
1129 mnt_flags |= MNT_NODIRATIME;
1130 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
1132 if (vx_ccaps(VXC_SECURE_MOUNT))
1133 mnt_flags |= MNT_NODEV;
1135 /* ... and get the mountpoint */
1136 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1140 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1144 if (flags & MS_REMOUNT)
1145 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1147 else if (flags & MS_BIND)
1148 retval = do_loopback(&nd, dev_name, xid, flags, mnt_flags);
1149 else if (flags & MS_MOVE)
1150 retval = do_move_mount(&nd, dev_name);
1152 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1153 dev_name, data_page);
1159 int copy_namespace(int flags, struct task_struct *tsk)
1161 struct namespace *namespace = tsk->namespace;
1162 struct namespace *new_ns;
1163 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1164 struct fs_struct *fs = tsk->fs;
1165 struct vfsmount *p, *q;
1170 get_namespace(namespace);
1172 if (!(flags & CLONE_NEWNS))
1175 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT)) {
1176 put_namespace(namespace);
1180 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1184 atomic_set(&new_ns->count, 1);
1185 init_rwsem(&new_ns->sem);
1186 INIT_LIST_HEAD(&new_ns->list);
1188 down_write(&tsk->namespace->sem);
1189 /* First pass: copy the tree topology */
1190 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
1191 if (!new_ns->root) {
1192 up_write(&tsk->namespace->sem);
1196 spin_lock(&vfsmount_lock);
1197 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1198 spin_unlock(&vfsmount_lock);
1201 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1202 * as belonging to new namespace. We have already acquired a private
1203 * fs_struct, so tsk->fs->lock is not needed.
1205 p = namespace->root;
1208 q->mnt_namespace = new_ns;
1210 if (p == fs->rootmnt) {
1212 fs->rootmnt = mntget(q);
1214 if (p == fs->pwdmnt) {
1216 fs->pwdmnt = mntget(q);
1218 if (p == fs->altrootmnt) {
1220 fs->altrootmnt = mntget(q);
1223 p = next_mnt(p, namespace->root);
1224 q = next_mnt(q, new_ns->root);
1226 up_write(&tsk->namespace->sem);
1228 tsk->namespace = new_ns;
1237 put_namespace(namespace);
1241 put_namespace(namespace);
1245 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1246 char __user * type, unsigned long flags,
1250 unsigned long data_page;
1251 unsigned long type_page;
1252 unsigned long dev_page;
1255 retval = copy_mount_options (type, &type_page);
1259 dir_page = getname(dir_name);
1260 retval = PTR_ERR(dir_page);
1261 if (IS_ERR(dir_page))
1264 retval = copy_mount_options (dev_name, &dev_page);
1268 retval = copy_mount_options (data, &data_page);
1273 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
1274 flags, (void*)data_page);
1276 free_page(data_page);
1279 free_page(dev_page);
1283 free_page(type_page);
1288 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1289 * It can block. Requires the big lock held.
1291 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1292 struct dentry *dentry)
1294 struct dentry *old_root;
1295 struct vfsmount *old_rootmnt;
1296 write_lock(&fs->lock);
1297 old_root = fs->root;
1298 old_rootmnt = fs->rootmnt;
1299 fs->rootmnt = mntget(mnt);
1300 fs->root = dget(dentry);
1301 write_unlock(&fs->lock);
1304 mntput(old_rootmnt);
1308 EXPORT_SYMBOL_GPL(set_fs_root);
1311 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1312 * It can block. Requires the big lock held.
1314 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1315 struct dentry *dentry)
1317 struct dentry *old_pwd;
1318 struct vfsmount *old_pwdmnt;
1320 write_lock(&fs->lock);
1322 old_pwdmnt = fs->pwdmnt;
1323 fs->pwdmnt = mntget(mnt);
1324 fs->pwd = dget(dentry);
1325 write_unlock(&fs->lock);
1333 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1335 struct task_struct *g, *p;
1336 struct fs_struct *fs;
1338 read_lock(&tasklist_lock);
1339 do_each_thread(g, p) {
1343 atomic_inc(&fs->count);
1345 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1346 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1347 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1348 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1352 } while_each_thread(g, p);
1353 read_unlock(&tasklist_lock);
1357 * pivot_root Semantics:
1358 * Moves the root file system of the current process to the directory put_old,
1359 * makes new_root as the new root file system of the current process, and sets
1360 * root/cwd of all processes which had them on the current root to new_root.
1363 * The new_root and put_old must be directories, and must not be on the
1364 * same file system as the current process root. The put_old must be
1365 * underneath new_root, i.e. adding a non-zero number of /.. to the string
1366 * pointed to by put_old must yield the same directory as new_root. No other
1367 * file system may be mounted on put_old. After all, new_root is a mountpoint.
1370 * - we don't move root/cwd if they are not at the root (reason: if something
1371 * cared enough to change them, it's probably wrong to force them elsewhere)
1372 * - it's okay to pick a root that isn't the root of a file system, e.g.
1373 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1374 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1378 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1380 struct vfsmount *tmp;
1381 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1384 if (!capable(CAP_SYS_ADMIN))
1389 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1393 if (!check_mnt(new_nd.mnt))
1396 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1400 error = security_sb_pivotroot(&old_nd, &new_nd);
1402 path_release(&old_nd);
1406 read_lock(¤t->fs->lock);
1407 user_nd.mnt = mntget(current->fs->rootmnt);
1408 user_nd.dentry = dget(current->fs->root);
1409 read_unlock(¤t->fs->lock);
1410 down_write(¤t->namespace->sem);
1411 down(&old_nd.dentry->d_inode->i_sem);
1413 if (!check_mnt(user_nd.mnt))
1416 if (IS_DEADDIR(new_nd.dentry->d_inode))
1418 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1420 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1423 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1424 goto out2; /* loop, on the same file system */
1426 if (user_nd.mnt->mnt_root != user_nd.dentry)
1427 goto out2; /* not a mountpoint */
1428 if (new_nd.mnt->mnt_root != new_nd.dentry)
1429 goto out2; /* not a mountpoint */
1430 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1431 spin_lock(&vfsmount_lock);
1432 if (tmp != new_nd.mnt) {
1434 if (tmp->mnt_parent == tmp)
1435 goto out3; /* already mounted on put_old */
1436 if (tmp->mnt_parent == new_nd.mnt)
1438 tmp = tmp->mnt_parent;
1440 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1442 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1444 detach_mnt(new_nd.mnt, &parent_nd);
1445 detach_mnt(user_nd.mnt, &root_parent);
1446 attach_mnt(user_nd.mnt, &old_nd); /* mount old root on put_old */
1447 attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */
1448 spin_unlock(&vfsmount_lock);
1449 chroot_fs_refs(&user_nd, &new_nd);
1450 security_sb_post_pivotroot(&user_nd, &new_nd);
1452 path_release(&root_parent);
1453 path_release(&parent_nd);
1455 up(&old_nd.dentry->d_inode->i_sem);
1456 up_write(¤t->namespace->sem);
1457 path_release(&user_nd);
1458 path_release(&old_nd);
1460 path_release(&new_nd);
1465 spin_unlock(&vfsmount_lock);
1469 static void __init init_mount_tree(void)
1471 struct vfsmount *mnt;
1472 struct namespace *namespace;
1473 struct task_struct *g, *p;
1475 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1477 panic("Can't create rootfs");
1478 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1480 panic("Can't allocate initial namespace");
1481 atomic_set(&namespace->count, 1);
1482 INIT_LIST_HEAD(&namespace->list);
1483 init_rwsem(&namespace->sem);
1484 list_add(&mnt->mnt_list, &namespace->list);
1485 namespace->root = mnt;
1486 mnt->mnt_namespace = namespace;
1488 init_task.namespace = namespace;
1489 read_lock(&tasklist_lock);
1490 do_each_thread(g, p) {
1491 get_namespace(namespace);
1492 p->namespace = namespace;
1493 } while_each_thread(g, p);
1494 read_unlock(&tasklist_lock);
1496 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1497 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1500 void __init mnt_init(unsigned long mempages)
1502 struct list_head *d;
1503 unsigned int nr_hash;
1506 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1507 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1509 mount_hashtable = (struct list_head *)
1510 __get_free_page(GFP_ATOMIC);
1512 if (!mount_hashtable)
1513 panic("Failed to allocate mount hash table\n");
1516 * Find the power-of-two list-heads that can fit into the allocation..
1517 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1520 nr_hash = PAGE_SIZE / sizeof(struct list_head);
1524 } while ((nr_hash >> hash_bits) != 0);
1528 * Re-calculate the actual number of entries and the mask
1529 * from the number of bits we can fit.
1531 nr_hash = 1UL << hash_bits;
1532 hash_mask = nr_hash-1;
1534 printk("Mount-cache hash table entries: %d\n", nr_hash);
1536 /* And initialize the newly allocated array */
1537 d = mount_hashtable;
1549 void __put_namespace(struct namespace *namespace)
1551 struct vfsmount *mnt;
1553 down_write(&namespace->sem);
1554 spin_lock(&vfsmount_lock);
1556 list_for_each_entry(mnt, &namespace->list, mnt_list) {
1557 mnt->mnt_namespace = NULL;
1560 umount_tree(namespace->root);
1561 spin_unlock(&vfsmount_lock);
1562 up_write(&namespace->sem);