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>
26 extern int __init init_rootfs(void);
29 extern int __init sysfs_init(void);
31 static inline int sysfs_init(void)
37 /* spinlock for vfsmount related operations, inplace of dcache_lock */
38 spinlock_t vfsmount_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
39 static struct list_head *mount_hashtable;
40 static int hash_mask, hash_bits;
41 static kmem_cache_t *mnt_cache;
43 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
45 unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
46 tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
47 tmp = tmp + (tmp >> hash_bits);
48 return tmp & hash_mask;
51 struct vfsmount *alloc_vfsmnt(const char *name)
53 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
55 memset(mnt, 0, sizeof(struct vfsmount));
56 atomic_set(&mnt->mnt_count,1);
57 INIT_LIST_HEAD(&mnt->mnt_hash);
58 INIT_LIST_HEAD(&mnt->mnt_child);
59 INIT_LIST_HEAD(&mnt->mnt_mounts);
60 INIT_LIST_HEAD(&mnt->mnt_list);
62 int size = strlen(name)+1;
63 char *newname = kmalloc(size, GFP_KERNEL);
65 memcpy(newname, name, size);
66 mnt->mnt_devname = newname;
73 void free_vfsmnt(struct vfsmount *mnt)
75 kfree(mnt->mnt_devname);
76 kmem_cache_free(mnt_cache, mnt);
80 * Now, lookup_mnt increments the ref count before returning
81 * the vfsmount struct.
83 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
85 struct list_head * head = mount_hashtable + hash(mnt, dentry);
86 struct list_head * tmp = head;
87 struct vfsmount *p, *found = NULL;
89 spin_lock(&vfsmount_lock);
95 p = list_entry(tmp, struct vfsmount, mnt_hash);
96 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
101 spin_unlock(&vfsmount_lock);
105 EXPORT_SYMBOL(lookup_mnt);
107 static int check_mnt(struct vfsmount *mnt)
109 spin_lock(&vfsmount_lock);
110 while (mnt->mnt_parent != mnt)
111 mnt = mnt->mnt_parent;
112 spin_unlock(&vfsmount_lock);
113 return mnt == current->namespace->root;
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;
169 void __mntput(struct vfsmount *mnt)
171 struct super_block *sb = mnt->mnt_sb;
174 deactivate_super(sb);
177 EXPORT_SYMBOL(__mntput);
180 static void *m_start(struct seq_file *m, loff_t *pos)
182 struct namespace *n = m->private;
187 list_for_each(p, &n->list)
189 return list_entry(p, struct vfsmount, mnt_list);
193 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
195 struct namespace *n = m->private;
196 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
198 return p==&n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
201 static void m_stop(struct seq_file *m, void *v)
203 struct namespace *n = m->private;
207 static inline void mangle(struct seq_file *m, const char *s)
209 seq_escape(m, s, " \t\n\\");
212 static int show_vfsmnt(struct seq_file *m, void *v)
214 struct vfsmount *mnt = v;
216 static struct proc_fs_info {
220 { MS_SYNCHRONOUS, ",sync" },
221 { MS_DIRSYNC, ",dirsync" },
222 { MS_MANDLOCK, ",mand" },
223 { MS_NOATIME, ",noatime" },
224 { MS_NODIRATIME, ",nodiratime" },
227 static struct proc_fs_info mnt_info[] = {
228 { MNT_NOSUID, ",nosuid" },
229 { MNT_NODEV, ",nodev" },
230 { MNT_NOEXEC, ",noexec" },
233 struct proc_fs_info *fs_infop;
235 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
237 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
239 mangle(m, mnt->mnt_sb->s_type->name);
240 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
241 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
242 if (mnt->mnt_sb->s_flags & fs_infop->flag)
243 seq_puts(m, fs_infop->str);
245 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
246 if (mnt->mnt_flags & fs_infop->flag)
247 seq_puts(m, fs_infop->str);
249 if (mnt->mnt_sb->s_op->show_options)
250 err = mnt->mnt_sb->s_op->show_options(m, mnt);
251 seq_puts(m, " 0 0\n");
255 struct seq_operations mounts_op = {
263 * Doesn't take quota and stuff into account. IOW, in some cases it will
264 * give false negatives. The main reason why it's here is that we need
265 * a non-destructive way to look for easily umountable filesystems.
267 int may_umount(struct vfsmount *mnt)
269 if (atomic_read(&mnt->mnt_count) > 2)
274 EXPORT_SYMBOL(may_umount);
276 void umount_tree(struct vfsmount *mnt)
281 for (p = mnt; p; p = next_mnt(p, mnt)) {
282 list_del(&p->mnt_list);
283 list_add(&p->mnt_list, &kill);
286 while (!list_empty(&kill)) {
287 mnt = list_entry(kill.next, struct vfsmount, mnt_list);
288 list_del_init(&mnt->mnt_list);
289 if (mnt->mnt_parent == mnt) {
290 spin_unlock(&vfsmount_lock);
292 struct nameidata old_nd;
293 detach_mnt(mnt, &old_nd);
294 spin_unlock(&vfsmount_lock);
295 path_release(&old_nd);
298 spin_lock(&vfsmount_lock);
302 static int do_umount(struct vfsmount *mnt, int flags)
304 struct super_block * sb = mnt->mnt_sb;
307 retval = security_sb_umount(mnt, flags);
312 * If we may have to abort operations to get out of this
313 * mount, and they will themselves hold resources we must
314 * allow the fs to do things. In the Unix tradition of
315 * 'Gee thats tricky lets do it in userspace' the umount_begin
316 * might fail to complete on the first run through as other tasks
317 * must return, and the like. Thats for the mount program to worry
318 * about for the moment.
322 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
323 sb->s_op->umount_begin(sb);
327 * No sense to grab the lock for this test, but test itself looks
328 * somewhat bogus. Suggestions for better replacement?
329 * Ho-hum... In principle, we might treat that as umount + switch
330 * to rootfs. GC would eventually take care of the old vfsmount.
331 * Actually it makes sense, especially if rootfs would contain a
332 * /reboot - static binary that would close all descriptors and
333 * call reboot(9). Then init(8) could umount root and exec /reboot.
335 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
337 * Special case for "unmounting" root ...
338 * we just try to remount it readonly.
340 down_write(&sb->s_umount);
341 if (!(sb->s_flags & MS_RDONLY)) {
343 retval = do_remount_sb(sb, MS_RDONLY, 0, 0);
346 up_write(&sb->s_umount);
350 down_write(¤t->namespace->sem);
351 spin_lock(&vfsmount_lock);
353 if (atomic_read(&sb->s_active) == 1) {
354 /* last instance - try to be smart */
355 spin_unlock(&vfsmount_lock);
360 security_sb_umount_close(mnt);
361 spin_lock(&vfsmount_lock);
364 if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
365 if (!list_empty(&mnt->mnt_list))
369 spin_unlock(&vfsmount_lock);
371 security_sb_umount_busy(mnt);
372 up_write(¤t->namespace->sem);
377 * Now umount can handle mount points as well as block devices.
378 * This is important for filesystems which use unnamed block devices.
380 * We now support a flag for forced unmount like the other 'big iron'
381 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
384 asmlinkage long sys_umount(char __user * name, int flags)
389 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
393 if (nd.dentry != nd.mnt->mnt_root)
395 if (!check_mnt(nd.mnt))
399 if (!capable(CAP_SYS_ADMIN))
402 retval = do_umount(nd.mnt, flags);
410 * The 2.0 compatible umount. No flags.
413 asmlinkage long sys_oldumount(char __user * name)
415 return sys_umount(name,0);
418 static int mount_is_safe(struct nameidata *nd)
420 if (capable(CAP_SYS_ADMIN))
424 if (S_ISLNK(nd->dentry->d_inode->i_mode))
426 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
427 if (current->uid != nd->dentry->d_inode->i_uid)
430 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
437 lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
442 if (d == NULL || d == d->d_parent)
448 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
450 struct vfsmount *res, *p, *q, *r, *s;
454 res = q = clone_mnt(mnt, dentry);
457 q->mnt_mountpoint = mnt->mnt_mountpoint;
460 for (h = mnt->mnt_mounts.next; h != &mnt->mnt_mounts; h = h->next) {
461 r = list_entry(h, struct vfsmount, mnt_child);
462 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
465 for (s = r; s; s = next_mnt(s, r)) {
466 while (p != s->mnt_parent) {
472 nd.dentry = p->mnt_mountpoint;
473 q = clone_mnt(p, p->mnt_root);
476 spin_lock(&vfsmount_lock);
477 list_add_tail(&q->mnt_list, &res->mnt_list);
479 spin_unlock(&vfsmount_lock);
485 spin_lock(&vfsmount_lock);
487 spin_unlock(&vfsmount_lock);
492 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
495 if (mnt->mnt_sb->s_flags & MS_NOUSER)
498 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
499 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
503 down(&nd->dentry->d_inode->i_sem);
504 if (IS_DEADDIR(nd->dentry->d_inode))
507 err = security_sb_check_sb(mnt, nd);
512 spin_lock(&vfsmount_lock);
513 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
514 struct list_head head;
517 list_add_tail(&head, &mnt->mnt_list);
518 list_splice(&head, current->namespace->list.prev);
522 spin_unlock(&vfsmount_lock);
524 up(&nd->dentry->d_inode->i_sem);
526 security_sb_post_addmount(mnt, nd);
533 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
535 struct nameidata old_nd;
536 struct vfsmount *mnt = NULL;
537 int err = mount_is_safe(nd);
540 if (!old_name || !*old_name)
542 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
546 down_write(¤t->namespace->sem);
548 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
551 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
553 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
557 err = graft_tree(mnt, nd);
559 spin_lock(&vfsmount_lock);
561 spin_unlock(&vfsmount_lock);
566 up_write(¤t->namespace->sem);
567 path_release(&old_nd);
572 * change filesystem flags. dir should be a physical root of filesystem.
573 * If you've mounted a non-root directory somewhere and want to do remount
574 * on it - tough luck.
577 static int do_remount(struct nameidata *nd,int flags,int mnt_flags,void *data)
580 struct super_block * sb = nd->mnt->mnt_sb;
582 if (!capable(CAP_SYS_ADMIN))
585 if (!check_mnt(nd->mnt))
588 if (nd->dentry != nd->mnt->mnt_root)
591 down_write(&sb->s_umount);
592 err = do_remount_sb(sb, flags, data, 0);
594 nd->mnt->mnt_flags=mnt_flags;
595 up_write(&sb->s_umount);
597 security_sb_post_remount(nd->mnt, flags, data);
601 static int do_move_mount(struct nameidata *nd, char *old_name)
603 struct nameidata old_nd, parent_nd;
606 if (!capable(CAP_SYS_ADMIN))
608 if (!old_name || !*old_name)
610 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
614 down_write(¤t->namespace->sem);
615 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
618 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
622 down(&nd->dentry->d_inode->i_sem);
623 if (IS_DEADDIR(nd->dentry->d_inode))
626 spin_lock(&vfsmount_lock);
627 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
631 if (old_nd.dentry != old_nd.mnt->mnt_root)
634 if (old_nd.mnt == old_nd.mnt->mnt_parent)
637 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
638 S_ISDIR(old_nd.dentry->d_inode->i_mode))
642 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
647 detach_mnt(old_nd.mnt, &parent_nd);
648 attach_mnt(old_nd.mnt, nd);
650 spin_unlock(&vfsmount_lock);
652 up(&nd->dentry->d_inode->i_sem);
654 up_write(¤t->namespace->sem);
656 path_release(&parent_nd);
657 path_release(&old_nd);
661 static int do_add_mount(struct nameidata *nd, char *type, int flags,
662 int mnt_flags, char *name, void *data)
664 struct vfsmount *mnt;
667 if (!type || !memchr(type, 0, PAGE_SIZE))
670 /* we need capabilities... */
671 if (!capable(CAP_SYS_ADMIN))
674 mnt = do_kern_mount(type, flags, name, data);
679 down_write(¤t->namespace->sem);
680 /* Something was mounted here while we slept */
681 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
684 if (!check_mnt(nd->mnt))
687 /* Refuse the same filesystem on the same mount point */
689 if (nd->mnt->mnt_sb == mnt->mnt_sb && nd->mnt->mnt_root == nd->dentry)
693 if (S_ISLNK(mnt->mnt_root->d_inode->i_mode))
696 mnt->mnt_flags = mnt_flags;
697 err = graft_tree(mnt, nd);
699 up_write(¤t->namespace->sem);
705 int copy_mount_options (const void __user *data, unsigned long *where)
715 if (!(page = __get_free_page(GFP_KERNEL)))
718 /* We only care that *some* data at the address the user
719 * gave us is valid. Just in case, we'll zero
720 * the remainder of the page.
722 /* copy_from_user cannot cross TASK_SIZE ! */
723 size = TASK_SIZE - (unsigned long)data;
724 if (size > PAGE_SIZE)
727 i = size - copy_from_user((void *)page, data, size);
733 memset((char *)page + i, 0, PAGE_SIZE - i);
739 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
740 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
742 * data is a (void *) that can point to any structure up to
743 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
744 * information (or be NULL).
746 * Pre-0.97 versions of mount() didn't have a flags word.
747 * When the flags word was introduced its top half was required
748 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
749 * Therefore, if this magic number is present, it carries no information
750 * and must be discarded.
752 long do_mount(char * dev_name, char * dir_name, char *type_page,
753 unsigned long flags, void *data_page)
760 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
761 flags &= ~MS_MGC_MSK;
763 /* Basic sanity checks */
765 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
767 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
771 ((char *)data_page)[PAGE_SIZE - 1] = 0;
773 /* Separate the per-mountpoint flags */
774 if (flags & MS_NOSUID)
775 mnt_flags |= MNT_NOSUID;
776 if (flags & MS_NODEV)
777 mnt_flags |= MNT_NODEV;
778 if (flags & MS_NOEXEC)
779 mnt_flags |= MNT_NOEXEC;
780 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
782 /* ... and get the mountpoint */
783 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
787 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
791 if (flags & MS_REMOUNT)
792 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
794 else if (flags & MS_BIND)
795 retval = do_loopback(&nd, dev_name, flags & MS_REC);
796 else if (flags & MS_MOVE)
797 retval = do_move_mount(&nd, dev_name);
799 retval = do_add_mount(&nd, type_page, flags, mnt_flags,
800 dev_name, data_page);
806 int copy_namespace(int flags, struct task_struct *tsk)
808 struct namespace *namespace = tsk->namespace;
809 struct namespace *new_ns;
810 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
811 struct fs_struct *fs = tsk->fs;
816 get_namespace(namespace);
818 if (!(flags & CLONE_NEWNS))
821 if (!capable(CAP_SYS_ADMIN)) {
822 put_namespace(namespace);
826 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
830 atomic_set(&new_ns->count, 1);
831 init_rwsem(&new_ns->sem);
832 INIT_LIST_HEAD(&new_ns->list);
834 down_write(&tsk->namespace->sem);
835 /* First pass: copy the tree topology */
836 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
838 up_write(&tsk->namespace->sem);
842 spin_lock(&vfsmount_lock);
843 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
844 spin_unlock(&vfsmount_lock);
846 /* Second pass: switch the tsk->fs->* elements */
848 struct vfsmount *p, *q;
849 write_lock(&fs->lock);
854 if (p == fs->rootmnt) {
856 fs->rootmnt = mntget(q);
858 if (p == fs->pwdmnt) {
860 fs->pwdmnt = mntget(q);
862 if (p == fs->altrootmnt) {
864 fs->altrootmnt = mntget(q);
866 p = next_mnt(p, namespace->root);
867 q = next_mnt(q, new_ns->root);
869 write_unlock(&fs->lock);
871 up_write(&tsk->namespace->sem);
873 tsk->namespace = new_ns;
882 put_namespace(namespace);
886 put_namespace(namespace);
890 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
891 char __user * type, unsigned long flags,
895 unsigned long data_page;
896 unsigned long type_page;
897 unsigned long dev_page;
900 retval = copy_mount_options (type, &type_page);
904 dir_page = getname(dir_name);
905 retval = PTR_ERR(dir_page);
906 if (IS_ERR(dir_page))
909 retval = copy_mount_options (dev_name, &dev_page);
913 retval = copy_mount_options (data, &data_page);
918 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
919 flags, (void*)data_page);
921 free_page(data_page);
928 free_page(type_page);
933 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
934 * It can block. Requires the big lock held.
936 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
937 struct dentry *dentry)
939 struct dentry *old_root;
940 struct vfsmount *old_rootmnt;
941 write_lock(&fs->lock);
943 old_rootmnt = fs->rootmnt;
944 fs->rootmnt = mntget(mnt);
945 fs->root = dget(dentry);
946 write_unlock(&fs->lock);
953 EXPORT_SYMBOL(set_fs_root);
956 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
957 * It can block. Requires the big lock held.
959 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
960 struct dentry *dentry)
962 struct dentry *old_pwd;
963 struct vfsmount *old_pwdmnt;
965 write_lock(&fs->lock);
967 old_pwdmnt = fs->pwdmnt;
968 fs->pwdmnt = mntget(mnt);
969 fs->pwd = dget(dentry);
970 write_unlock(&fs->lock);
978 EXPORT_SYMBOL(set_fs_pwd);
980 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
982 struct task_struct *g, *p;
983 struct fs_struct *fs;
985 read_lock(&tasklist_lock);
986 do_each_thread(g, p) {
990 atomic_inc(&fs->count);
992 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
993 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
994 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
995 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
999 } while_each_thread(g, p);
1000 read_unlock(&tasklist_lock);
1004 * Moves the current root to put_root, and sets root/cwd of all processes
1005 * which had them on the old root to new_root.
1008 * - we don't move root/cwd if they are not at the root (reason: if something
1009 * cared enough to change them, it's probably wrong to force them elsewhere)
1010 * - it's okay to pick a root that isn't the root of a file system, e.g.
1011 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1012 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1016 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1018 struct vfsmount *tmp;
1019 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1022 if (!capable(CAP_SYS_ADMIN))
1027 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1031 if (!check_mnt(new_nd.mnt))
1034 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1038 error = security_sb_pivotroot(&old_nd, &new_nd);
1040 path_release(&old_nd);
1044 read_lock(¤t->fs->lock);
1045 user_nd.mnt = mntget(current->fs->rootmnt);
1046 user_nd.dentry = dget(current->fs->root);
1047 read_unlock(¤t->fs->lock);
1048 down_write(¤t->namespace->sem);
1049 down(&old_nd.dentry->d_inode->i_sem);
1051 if (!check_mnt(user_nd.mnt))
1054 if (IS_DEADDIR(new_nd.dentry->d_inode))
1056 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1058 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1061 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1062 goto out2; /* loop */
1064 if (user_nd.mnt->mnt_root != user_nd.dentry)
1066 if (new_nd.mnt->mnt_root != new_nd.dentry)
1067 goto out2; /* not a mountpoint */
1068 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1069 spin_lock(&vfsmount_lock);
1070 if (tmp != new_nd.mnt) {
1072 if (tmp->mnt_parent == tmp)
1074 if (tmp->mnt_parent == new_nd.mnt)
1076 tmp = tmp->mnt_parent;
1078 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1080 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1082 detach_mnt(new_nd.mnt, &parent_nd);
1083 detach_mnt(user_nd.mnt, &root_parent);
1084 attach_mnt(user_nd.mnt, &old_nd);
1085 attach_mnt(new_nd.mnt, &root_parent);
1086 spin_unlock(&vfsmount_lock);
1087 chroot_fs_refs(&user_nd, &new_nd);
1088 security_sb_post_pivotroot(&user_nd, &new_nd);
1090 path_release(&root_parent);
1091 path_release(&parent_nd);
1093 up(&old_nd.dentry->d_inode->i_sem);
1094 up_write(¤t->namespace->sem);
1095 path_release(&user_nd);
1096 path_release(&old_nd);
1098 path_release(&new_nd);
1103 spin_unlock(&vfsmount_lock);
1107 static void __init init_mount_tree(void)
1109 struct vfsmount *mnt;
1110 struct namespace *namespace;
1111 struct task_struct *g, *p;
1113 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1115 panic("Can't create rootfs");
1116 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1118 panic("Can't allocate initial namespace");
1119 atomic_set(&namespace->count, 1);
1120 INIT_LIST_HEAD(&namespace->list);
1121 init_rwsem(&namespace->sem);
1122 list_add(&mnt->mnt_list, &namespace->list);
1123 namespace->root = mnt;
1125 init_task.namespace = namespace;
1126 read_lock(&tasklist_lock);
1127 do_each_thread(g, p) {
1128 get_namespace(namespace);
1129 p->namespace = namespace;
1130 } while_each_thread(g, p);
1131 read_unlock(&tasklist_lock);
1133 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1134 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1137 void __init mnt_init(unsigned long mempages)
1139 struct list_head *d;
1140 unsigned long order;
1141 unsigned int nr_hash;
1144 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1145 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
1147 panic("Cannot create vfsmount cache");
1150 mount_hashtable = (struct list_head *)
1151 __get_free_pages(GFP_ATOMIC, order);
1153 if (!mount_hashtable)
1154 panic("Failed to allocate mount hash table\n");
1157 * Find the power-of-two list-heads that can fit into the allocation..
1158 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1161 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1165 } while ((nr_hash >> hash_bits) != 0);
1169 * Re-calculate the actual number of entries and the mask
1170 * from the number of bits we can fit.
1172 nr_hash = 1UL << hash_bits;
1173 hash_mask = nr_hash-1;
1175 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1176 nr_hash, order, (PAGE_SIZE << order));
1178 /* And initialize the newly allocated array */
1179 d = mount_hashtable;
1191 void __put_namespace(struct namespace *namespace)
1193 down_write(&namespace->sem);
1194 spin_lock(&vfsmount_lock);
1195 umount_tree(namespace->root);
1196 spin_unlock(&vfsmount_lock);
1197 up_write(&namespace->sem);