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/slab.h>
13 #include <linux/sched.h>
14 #include <linux/smp_lock.h>
15 #include <linux/init.h>
16 #include <linux/quotaops.h>
17 #include <linux/acct.h>
18 #include <linux/module.h>
19 #include <linux/seq_file.h>
20 #include <linux/namespace.h>
21 #include <linux/namei.h>
22 #include <linux/security.h>
23 #include <linux/mount.h>
24 #include <asm/uaccess.h>
25 #include <asm/unistd.h>
27 extern int __init init_rootfs(void);
30 extern int __init sysfs_init(void);
32 static inline int sysfs_init(void)
38 /* spinlock for vfsmount related operations, inplace of dcache_lock */
39 spinlock_t vfsmount_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
41 static struct list_head *mount_hashtable;
42 static int hash_mask, hash_bits;
43 static kmem_cache_t *mnt_cache;
45 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
47 unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
48 tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
49 tmp = tmp + (tmp >> hash_bits);
50 return tmp & hash_mask;
53 struct vfsmount *alloc_vfsmnt(const char *name)
55 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
57 memset(mnt, 0, sizeof(struct vfsmount));
58 atomic_set(&mnt->mnt_count,1);
59 INIT_LIST_HEAD(&mnt->mnt_hash);
60 INIT_LIST_HEAD(&mnt->mnt_child);
61 INIT_LIST_HEAD(&mnt->mnt_mounts);
62 INIT_LIST_HEAD(&mnt->mnt_list);
64 int size = strlen(name)+1;
65 char *newname = kmalloc(size, GFP_KERNEL);
67 memcpy(newname, name, size);
68 mnt->mnt_devname = newname;
75 void free_vfsmnt(struct vfsmount *mnt)
77 kfree(mnt->mnt_devname);
78 kmem_cache_free(mnt_cache, mnt);
82 * Now, lookup_mnt increments the ref count before returning
83 * the vfsmount struct.
85 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
87 struct list_head * head = mount_hashtable + hash(mnt, dentry);
88 struct list_head * tmp = head;
89 struct vfsmount *p, *found = NULL;
91 spin_lock(&vfsmount_lock);
97 p = list_entry(tmp, struct vfsmount, mnt_hash);
98 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
103 spin_unlock(&vfsmount_lock);
107 EXPORT_SYMBOL(lookup_mnt);
109 static int check_mnt(struct vfsmount *mnt)
111 spin_lock(&vfsmount_lock);
112 while (mnt->mnt_parent != mnt)
113 mnt = mnt->mnt_parent;
114 spin_unlock(&vfsmount_lock);
115 return mnt == current->namespace->root;
118 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
120 old_nd->dentry = mnt->mnt_mountpoint;
121 old_nd->mnt = mnt->mnt_parent;
122 mnt->mnt_parent = mnt;
123 mnt->mnt_mountpoint = mnt->mnt_root;
124 list_del_init(&mnt->mnt_child);
125 list_del_init(&mnt->mnt_hash);
126 old_nd->dentry->d_mounted--;
129 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
131 mnt->mnt_parent = mntget(nd->mnt);
132 mnt->mnt_mountpoint = dget(nd->dentry);
133 list_add(&mnt->mnt_hash, mount_hashtable+hash(nd->mnt, nd->dentry));
134 list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
135 nd->dentry->d_mounted++;
138 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
140 struct list_head *next = p->mnt_mounts.next;
141 if (next == &p->mnt_mounts) {
145 next = p->mnt_child.next;
146 if (next != &p->mnt_parent->mnt_mounts)
151 return list_entry(next, struct vfsmount, mnt_child);
154 static struct vfsmount *
155 clone_mnt(struct vfsmount *old, struct dentry *root)
157 struct super_block *sb = old->mnt_sb;
158 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
161 mnt->mnt_flags = old->mnt_flags;
162 atomic_inc(&sb->s_active);
164 mnt->mnt_root = dget(root);
165 mnt->mnt_mountpoint = mnt->mnt_root;
166 mnt->mnt_parent = mnt;
171 void __mntput(struct vfsmount *mnt)
173 struct super_block *sb = mnt->mnt_sb;
176 deactivate_super(sb);
179 EXPORT_SYMBOL(__mntput);
182 static void *m_start(struct seq_file *m, loff_t *pos)
184 struct namespace *n = m->private;
189 list_for_each(p, &n->list)
191 return list_entry(p, struct vfsmount, mnt_list);
195 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
197 struct namespace *n = m->private;
198 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
200 return p==&n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
203 static void m_stop(struct seq_file *m, void *v)
205 struct namespace *n = m->private;
209 static inline void mangle(struct seq_file *m, const char *s)
211 seq_escape(m, s, " \t\n\\");
214 static int show_vfsmnt(struct seq_file *m, void *v)
216 struct vfsmount *mnt = v;
218 static struct proc_fs_info {
222 { MS_SYNCHRONOUS, ",sync" },
223 { MS_DIRSYNC, ",dirsync" },
224 { MS_MANDLOCK, ",mand" },
225 { MS_NOATIME, ",noatime" },
226 { MS_NODIRATIME, ",nodiratime" },
229 static struct proc_fs_info mnt_info[] = {
230 { MNT_NOSUID, ",nosuid" },
231 { MNT_NODEV, ",nodev" },
232 { MNT_NOEXEC, ",noexec" },
235 struct proc_fs_info *fs_infop;
237 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
239 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
241 mangle(m, mnt->mnt_sb->s_type->name);
242 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
243 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
244 if (mnt->mnt_sb->s_flags & fs_infop->flag)
245 seq_puts(m, fs_infop->str);
247 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
248 if (mnt->mnt_flags & fs_infop->flag)
249 seq_puts(m, fs_infop->str);
251 if (mnt->mnt_sb->s_op->show_options)
252 err = mnt->mnt_sb->s_op->show_options(m, mnt);
253 seq_puts(m, " 0 0\n");
257 struct seq_operations mounts_op = {
265 * may_umount_tree - check if a mount tree is busy
266 * @mnt: root of mount tree
268 * This is called to check if a tree of mounts has any
269 * open files, pwds, chroots or sub mounts that are
272 int may_umount_tree(struct vfsmount *mnt)
274 struct list_head *next;
275 struct vfsmount *this_parent = mnt;
279 spin_lock(&vfsmount_lock);
280 actual_refs = atomic_read(&mnt->mnt_count);
283 next = this_parent->mnt_mounts.next;
285 while (next != &this_parent->mnt_mounts) {
286 struct vfsmount *p = list_entry(next, struct vfsmount, mnt_child);
290 actual_refs += atomic_read(&p->mnt_count);
293 if (!list_empty(&p->mnt_mounts)) {
299 if (this_parent != mnt) {
300 next = this_parent->mnt_child.next;
301 this_parent = this_parent->mnt_parent;
304 spin_unlock(&vfsmount_lock);
306 if (actual_refs > minimum_refs)
312 EXPORT_SYMBOL(may_umount_tree);
315 * may_umount - check if a mount point is busy
316 * @mnt: root of mount
318 * This is called to check if a mount point has any
319 * open files, pwds, chroots or sub mounts. If the
320 * mount has sub mounts this will return busy
321 * regardless of whether the sub mounts are busy.
323 * Doesn't take quota and stuff into account. IOW, in some cases it will
324 * give false negatives. The main reason why it's here is that we need
325 * a non-destructive way to look for easily umountable filesystems.
327 int may_umount(struct vfsmount *mnt)
329 if (atomic_read(&mnt->mnt_count) > 2)
334 EXPORT_SYMBOL(may_umount);
336 void umount_tree(struct vfsmount *mnt)
341 for (p = mnt; p; p = next_mnt(p, mnt)) {
342 list_del(&p->mnt_list);
343 list_add(&p->mnt_list, &kill);
346 while (!list_empty(&kill)) {
347 mnt = list_entry(kill.next, struct vfsmount, mnt_list);
348 list_del_init(&mnt->mnt_list);
349 if (mnt->mnt_parent == mnt) {
350 spin_unlock(&vfsmount_lock);
352 struct nameidata old_nd;
353 detach_mnt(mnt, &old_nd);
354 spin_unlock(&vfsmount_lock);
355 path_release(&old_nd);
358 spin_lock(&vfsmount_lock);
362 static int do_umount(struct vfsmount *mnt, int flags)
364 struct super_block * sb = mnt->mnt_sb;
367 retval = security_sb_umount(mnt, flags);
372 * If we may have to abort operations to get out of this
373 * mount, and they will themselves hold resources we must
374 * allow the fs to do things. In the Unix tradition of
375 * 'Gee thats tricky lets do it in userspace' the umount_begin
376 * might fail to complete on the first run through as other tasks
377 * must return, and the like. Thats for the mount program to worry
378 * about for the moment.
382 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
383 sb->s_op->umount_begin(sb);
387 * No sense to grab the lock for this test, but test itself looks
388 * somewhat bogus. Suggestions for better replacement?
389 * Ho-hum... In principle, we might treat that as umount + switch
390 * to rootfs. GC would eventually take care of the old vfsmount.
391 * Actually it makes sense, especially if rootfs would contain a
392 * /reboot - static binary that would close all descriptors and
393 * call reboot(9). Then init(8) could umount root and exec /reboot.
395 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
397 * Special case for "unmounting" root ...
398 * we just try to remount it readonly.
400 down_write(&sb->s_umount);
401 if (!(sb->s_flags & MS_RDONLY)) {
403 retval = do_remount_sb(sb, MS_RDONLY, 0, 0);
406 up_write(&sb->s_umount);
410 down_write(¤t->namespace->sem);
411 spin_lock(&vfsmount_lock);
413 if (atomic_read(&sb->s_active) == 1) {
414 /* last instance - try to be smart */
415 spin_unlock(&vfsmount_lock);
420 security_sb_umount_close(mnt);
421 spin_lock(&vfsmount_lock);
424 if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
425 if (!list_empty(&mnt->mnt_list))
429 spin_unlock(&vfsmount_lock);
431 security_sb_umount_busy(mnt);
432 up_write(¤t->namespace->sem);
437 * Now umount can handle mount points as well as block devices.
438 * This is important for filesystems which use unnamed block devices.
440 * We now support a flag for forced unmount like the other 'big iron'
441 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
444 asmlinkage long sys_umount(char __user * name, int flags)
449 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
453 if (nd.dentry != nd.mnt->mnt_root)
455 if (!check_mnt(nd.mnt))
459 if (!capable(CAP_SYS_ADMIN))
462 retval = do_umount(nd.mnt, flags);
469 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
472 * The 2.0 compatible umount. No flags.
475 asmlinkage long sys_oldumount(char __user * name)
477 return sys_umount(name,0);
482 static int mount_is_safe(struct nameidata *nd)
484 if (capable(CAP_SYS_ADMIN))
488 if (S_ISLNK(nd->dentry->d_inode->i_mode))
490 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
491 if (current->uid != nd->dentry->d_inode->i_uid)
494 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
501 lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
506 if (d == NULL || d == d->d_parent)
512 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
514 struct vfsmount *res, *p, *q, *r, *s;
518 res = q = clone_mnt(mnt, dentry);
521 q->mnt_mountpoint = mnt->mnt_mountpoint;
524 for (h = mnt->mnt_mounts.next; h != &mnt->mnt_mounts; h = h->next) {
525 r = list_entry(h, struct vfsmount, mnt_child);
526 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
529 for (s = r; s; s = next_mnt(s, r)) {
530 while (p != s->mnt_parent) {
536 nd.dentry = p->mnt_mountpoint;
537 q = clone_mnt(p, p->mnt_root);
540 spin_lock(&vfsmount_lock);
541 list_add_tail(&q->mnt_list, &res->mnt_list);
543 spin_unlock(&vfsmount_lock);
549 spin_lock(&vfsmount_lock);
551 spin_unlock(&vfsmount_lock);
556 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
559 if (mnt->mnt_sb->s_flags & MS_NOUSER)
562 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
563 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
567 down(&nd->dentry->d_inode->i_sem);
568 if (IS_DEADDIR(nd->dentry->d_inode))
571 err = security_sb_check_sb(mnt, nd);
576 spin_lock(&vfsmount_lock);
577 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
578 struct list_head head;
581 list_add_tail(&head, &mnt->mnt_list);
582 list_splice(&head, current->namespace->list.prev);
586 spin_unlock(&vfsmount_lock);
588 up(&nd->dentry->d_inode->i_sem);
590 security_sb_post_addmount(mnt, nd);
597 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
599 struct nameidata old_nd;
600 struct vfsmount *mnt = NULL;
601 int err = mount_is_safe(nd);
604 if (!old_name || !*old_name)
606 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
610 down_write(¤t->namespace->sem);
612 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
615 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
617 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
621 err = graft_tree(mnt, nd);
623 spin_lock(&vfsmount_lock);
625 spin_unlock(&vfsmount_lock);
630 up_write(¤t->namespace->sem);
631 path_release(&old_nd);
636 * change filesystem flags. dir should be a physical root of filesystem.
637 * If you've mounted a non-root directory somewhere and want to do remount
638 * on it - tough luck.
641 static int do_remount(struct nameidata *nd,int flags,int mnt_flags,void *data)
644 struct super_block * sb = nd->mnt->mnt_sb;
646 if (!capable(CAP_SYS_ADMIN))
649 if (!check_mnt(nd->mnt))
652 if (nd->dentry != nd->mnt->mnt_root)
655 down_write(&sb->s_umount);
656 err = do_remount_sb(sb, flags, data, 0);
658 nd->mnt->mnt_flags=mnt_flags;
659 up_write(&sb->s_umount);
661 security_sb_post_remount(nd->mnt, flags, data);
665 static int do_move_mount(struct nameidata *nd, char *old_name)
667 struct nameidata old_nd, parent_nd;
670 if (!capable(CAP_SYS_ADMIN))
672 if (!old_name || !*old_name)
674 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
678 down_write(¤t->namespace->sem);
679 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
682 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
686 down(&nd->dentry->d_inode->i_sem);
687 if (IS_DEADDIR(nd->dentry->d_inode))
690 spin_lock(&vfsmount_lock);
691 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
695 if (old_nd.dentry != old_nd.mnt->mnt_root)
698 if (old_nd.mnt == old_nd.mnt->mnt_parent)
701 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
702 S_ISDIR(old_nd.dentry->d_inode->i_mode))
706 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
711 detach_mnt(old_nd.mnt, &parent_nd);
712 attach_mnt(old_nd.mnt, nd);
714 spin_unlock(&vfsmount_lock);
716 up(&nd->dentry->d_inode->i_sem);
718 up_write(¤t->namespace->sem);
720 path_release(&parent_nd);
721 path_release(&old_nd);
725 static int do_add_mount(struct nameidata *nd, char *type, int flags,
726 int mnt_flags, char *name, void *data)
728 struct vfsmount *mnt;
731 if (!type || !memchr(type, 0, PAGE_SIZE))
734 /* we need capabilities... */
735 if (!capable(CAP_SYS_ADMIN))
738 mnt = do_kern_mount(type, flags, name, data);
743 down_write(¤t->namespace->sem);
744 /* Something was mounted here while we slept */
745 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
748 if (!check_mnt(nd->mnt))
751 /* Refuse the same filesystem on the same mount point */
753 if (nd->mnt->mnt_sb == mnt->mnt_sb && nd->mnt->mnt_root == nd->dentry)
757 if (S_ISLNK(mnt->mnt_root->d_inode->i_mode))
760 mnt->mnt_flags = mnt_flags;
761 err = graft_tree(mnt, nd);
763 up_write(¤t->namespace->sem);
769 int copy_mount_options (const void __user *data, unsigned long *where)
779 if (!(page = __get_free_page(GFP_KERNEL)))
782 /* We only care that *some* data at the address the user
783 * gave us is valid. Just in case, we'll zero
784 * the remainder of the page.
786 /* copy_from_user cannot cross TASK_SIZE ! */
787 size = TASK_SIZE - (unsigned long)data;
788 if (size > PAGE_SIZE)
791 i = size - copy_from_user((void *)page, data, size);
797 memset((char *)page + i, 0, PAGE_SIZE - i);
803 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
804 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
806 * data is a (void *) that can point to any structure up to
807 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
808 * information (or be NULL).
810 * Pre-0.97 versions of mount() didn't have a flags word.
811 * When the flags word was introduced its top half was required
812 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
813 * Therefore, if this magic number is present, it carries no information
814 * and must be discarded.
816 long do_mount(char * dev_name, char * dir_name, char *type_page,
817 unsigned long flags, void *data_page)
824 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
825 flags &= ~MS_MGC_MSK;
827 /* Basic sanity checks */
829 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
831 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
835 ((char *)data_page)[PAGE_SIZE - 1] = 0;
837 /* Separate the per-mountpoint flags */
838 if (flags & MS_NOSUID)
839 mnt_flags |= MNT_NOSUID;
840 if (flags & MS_NODEV)
841 mnt_flags |= MNT_NODEV;
842 if (flags & MS_NOEXEC)
843 mnt_flags |= MNT_NOEXEC;
844 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
846 /* ... and get the mountpoint */
847 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
851 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
855 if (flags & MS_REMOUNT)
856 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
858 else if (flags & MS_BIND)
859 retval = do_loopback(&nd, dev_name, flags & MS_REC);
860 else if (flags & MS_MOVE)
861 retval = do_move_mount(&nd, dev_name);
863 retval = do_add_mount(&nd, type_page, flags, mnt_flags,
864 dev_name, data_page);
870 int copy_namespace(int flags, struct task_struct *tsk)
872 struct namespace *namespace = tsk->namespace;
873 struct namespace *new_ns;
874 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
875 struct fs_struct *fs = tsk->fs;
880 get_namespace(namespace);
882 if (!(flags & CLONE_NEWNS))
885 if (!capable(CAP_SYS_ADMIN)) {
886 put_namespace(namespace);
890 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
894 atomic_set(&new_ns->count, 1);
895 init_rwsem(&new_ns->sem);
896 INIT_LIST_HEAD(&new_ns->list);
898 down_write(&tsk->namespace->sem);
899 /* First pass: copy the tree topology */
900 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
902 up_write(&tsk->namespace->sem);
906 spin_lock(&vfsmount_lock);
907 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
908 spin_unlock(&vfsmount_lock);
910 /* Second pass: switch the tsk->fs->* elements */
912 struct vfsmount *p, *q;
913 write_lock(&fs->lock);
918 if (p == fs->rootmnt) {
920 fs->rootmnt = mntget(q);
922 if (p == fs->pwdmnt) {
924 fs->pwdmnt = mntget(q);
926 if (p == fs->altrootmnt) {
928 fs->altrootmnt = mntget(q);
930 p = next_mnt(p, namespace->root);
931 q = next_mnt(q, new_ns->root);
933 write_unlock(&fs->lock);
935 up_write(&tsk->namespace->sem);
937 tsk->namespace = new_ns;
946 put_namespace(namespace);
950 put_namespace(namespace);
954 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
955 char __user * type, unsigned long flags,
959 unsigned long data_page;
960 unsigned long type_page;
961 unsigned long dev_page;
964 retval = copy_mount_options (type, &type_page);
968 dir_page = getname(dir_name);
969 retval = PTR_ERR(dir_page);
970 if (IS_ERR(dir_page))
973 retval = copy_mount_options (dev_name, &dev_page);
977 retval = copy_mount_options (data, &data_page);
982 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
983 flags, (void*)data_page);
985 free_page(data_page);
992 free_page(type_page);
997 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
998 * It can block. Requires the big lock held.
1000 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1001 struct dentry *dentry)
1003 struct dentry *old_root;
1004 struct vfsmount *old_rootmnt;
1005 write_lock(&fs->lock);
1006 old_root = fs->root;
1007 old_rootmnt = fs->rootmnt;
1008 fs->rootmnt = mntget(mnt);
1009 fs->root = dget(dentry);
1010 write_unlock(&fs->lock);
1013 mntput(old_rootmnt);
1017 EXPORT_SYMBOL(set_fs_root);
1020 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1021 * It can block. Requires the big lock held.
1023 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1024 struct dentry *dentry)
1026 struct dentry *old_pwd;
1027 struct vfsmount *old_pwdmnt;
1029 write_lock(&fs->lock);
1031 old_pwdmnt = fs->pwdmnt;
1032 fs->pwdmnt = mntget(mnt);
1033 fs->pwd = dget(dentry);
1034 write_unlock(&fs->lock);
1042 EXPORT_SYMBOL(set_fs_pwd);
1044 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1046 struct task_struct *g, *p;
1047 struct fs_struct *fs;
1049 read_lock(&tasklist_lock);
1050 do_each_thread(g, p) {
1054 atomic_inc(&fs->count);
1056 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1057 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1058 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1059 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1063 } while_each_thread(g, p);
1064 read_unlock(&tasklist_lock);
1068 * Moves the current root to put_root, and sets root/cwd of all processes
1069 * which had them on the old root to new_root.
1072 * - we don't move root/cwd if they are not at the root (reason: if something
1073 * cared enough to change them, it's probably wrong to force them elsewhere)
1074 * - it's okay to pick a root that isn't the root of a file system, e.g.
1075 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1076 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1080 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1082 struct vfsmount *tmp;
1083 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1086 if (!capable(CAP_SYS_ADMIN))
1091 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1095 if (!check_mnt(new_nd.mnt))
1098 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1102 error = security_sb_pivotroot(&old_nd, &new_nd);
1104 path_release(&old_nd);
1108 read_lock(¤t->fs->lock);
1109 user_nd.mnt = mntget(current->fs->rootmnt);
1110 user_nd.dentry = dget(current->fs->root);
1111 read_unlock(¤t->fs->lock);
1112 down_write(¤t->namespace->sem);
1113 down(&old_nd.dentry->d_inode->i_sem);
1115 if (!check_mnt(user_nd.mnt))
1118 if (IS_DEADDIR(new_nd.dentry->d_inode))
1120 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1122 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1125 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1126 goto out2; /* loop */
1128 if (user_nd.mnt->mnt_root != user_nd.dentry)
1130 if (new_nd.mnt->mnt_root != new_nd.dentry)
1131 goto out2; /* not a mountpoint */
1132 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1133 spin_lock(&vfsmount_lock);
1134 if (tmp != new_nd.mnt) {
1136 if (tmp->mnt_parent == tmp)
1138 if (tmp->mnt_parent == new_nd.mnt)
1140 tmp = tmp->mnt_parent;
1142 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1144 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1146 detach_mnt(new_nd.mnt, &parent_nd);
1147 detach_mnt(user_nd.mnt, &root_parent);
1148 attach_mnt(user_nd.mnt, &old_nd);
1149 attach_mnt(new_nd.mnt, &root_parent);
1150 spin_unlock(&vfsmount_lock);
1151 chroot_fs_refs(&user_nd, &new_nd);
1152 security_sb_post_pivotroot(&user_nd, &new_nd);
1154 path_release(&root_parent);
1155 path_release(&parent_nd);
1157 up(&old_nd.dentry->d_inode->i_sem);
1158 up_write(¤t->namespace->sem);
1159 path_release(&user_nd);
1160 path_release(&old_nd);
1162 path_release(&new_nd);
1167 spin_unlock(&vfsmount_lock);
1171 static void __init init_mount_tree(void)
1173 struct vfsmount *mnt;
1174 struct namespace *namespace;
1175 struct task_struct *g, *p;
1177 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1179 panic("Can't create rootfs");
1180 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1182 panic("Can't allocate initial namespace");
1183 atomic_set(&namespace->count, 1);
1184 INIT_LIST_HEAD(&namespace->list);
1185 init_rwsem(&namespace->sem);
1186 list_add(&mnt->mnt_list, &namespace->list);
1187 namespace->root = mnt;
1189 init_task.namespace = namespace;
1190 read_lock(&tasklist_lock);
1191 do_each_thread(g, p) {
1192 get_namespace(namespace);
1193 p->namespace = namespace;
1194 } while_each_thread(g, p);
1195 read_unlock(&tasklist_lock);
1197 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1198 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1201 void __init mnt_init(unsigned long mempages)
1203 struct list_head *d;
1204 unsigned long order;
1205 unsigned int nr_hash;
1208 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1209 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1212 mount_hashtable = (struct list_head *)
1213 __get_free_pages(GFP_ATOMIC, order);
1215 if (!mount_hashtable)
1216 panic("Failed to allocate mount hash table\n");
1219 * Find the power-of-two list-heads that can fit into the allocation..
1220 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1223 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1227 } while ((nr_hash >> hash_bits) != 0);
1231 * Re-calculate the actual number of entries and the mask
1232 * from the number of bits we can fit.
1234 nr_hash = 1UL << hash_bits;
1235 hash_mask = nr_hash-1;
1237 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1238 nr_hash, order, (PAGE_SIZE << order));
1240 /* And initialize the newly allocated array */
1241 d = mount_hashtable;
1253 void __put_namespace(struct namespace *namespace)
1255 down_write(&namespace->sem);
1256 spin_lock(&vfsmount_lock);
1257 umount_tree(namespace->root);
1258 spin_unlock(&vfsmount_lock);
1259 up_write(&namespace->sem);