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 if (vx_flags(VXF_HIDE_MOUNT, 0))
238 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
240 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
242 mangle(m, mnt->mnt_sb->s_type->name);
243 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
244 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
245 if (mnt->mnt_sb->s_flags & fs_infop->flag)
246 seq_puts(m, fs_infop->str);
248 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
249 if (mnt->mnt_flags & fs_infop->flag)
250 seq_puts(m, fs_infop->str);
252 if (mnt->mnt_sb->s_op->show_options)
253 err = mnt->mnt_sb->s_op->show_options(m, mnt);
254 seq_puts(m, " 0 0\n");
258 struct seq_operations mounts_op = {
266 * Doesn't take quota and stuff into account. IOW, in some cases it will
267 * give false negatives. The main reason why it's here is that we need
268 * a non-destructive way to look for easily umountable filesystems.
270 int may_umount(struct vfsmount *mnt)
272 if (atomic_read(&mnt->mnt_count) > 2)
277 EXPORT_SYMBOL(may_umount);
279 static inline void __umount_tree(struct vfsmount *mnt, struct list_head *kill)
281 while (!list_empty(kill)) {
282 mnt = list_entry(kill->next, struct vfsmount, mnt_list);
283 list_del_init(&mnt->mnt_list);
284 if (mnt->mnt_parent == mnt) {
285 spin_unlock(&vfsmount_lock);
287 struct nameidata old_nd;
288 detach_mnt(mnt, &old_nd);
289 spin_unlock(&vfsmount_lock);
290 path_release(&old_nd);
293 spin_lock(&vfsmount_lock);
297 void umount_tree(struct vfsmount *mnt)
302 for (p = mnt; p; p = next_mnt(p, mnt)) {
303 list_del(&p->mnt_list);
304 list_add(&p->mnt_list, &kill);
306 __umount_tree(mnt, &kill);
309 void umount_unused(struct vfsmount *mnt, struct fs_struct *fs)
314 for (p = mnt; p; p = next_mnt(p, mnt)) {
315 if (p == fs->rootmnt || p == fs->pwdmnt)
317 list_del(&p->mnt_list);
318 list_add(&p->mnt_list, &kill);
320 __umount_tree(mnt, &kill);
323 static int do_umount(struct vfsmount *mnt, int flags)
325 struct super_block * sb = mnt->mnt_sb;
328 retval = security_sb_umount(mnt, flags);
333 * If we may have to abort operations to get out of this
334 * mount, and they will themselves hold resources we must
335 * allow the fs to do things. In the Unix tradition of
336 * 'Gee thats tricky lets do it in userspace' the umount_begin
337 * might fail to complete on the first run through as other tasks
338 * must return, and the like. Thats for the mount program to worry
339 * about for the moment.
343 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
344 sb->s_op->umount_begin(sb);
348 * No sense to grab the lock for this test, but test itself looks
349 * somewhat bogus. Suggestions for better replacement?
350 * Ho-hum... In principle, we might treat that as umount + switch
351 * to rootfs. GC would eventually take care of the old vfsmount.
352 * Actually it makes sense, especially if rootfs would contain a
353 * /reboot - static binary that would close all descriptors and
354 * call reboot(9). Then init(8) could umount root and exec /reboot.
356 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
358 * Special case for "unmounting" root ...
359 * we just try to remount it readonly.
361 down_write(&sb->s_umount);
362 if (!(sb->s_flags & MS_RDONLY)) {
364 retval = do_remount_sb(sb, MS_RDONLY, 0, 0);
367 up_write(&sb->s_umount);
371 down_write(¤t->namespace->sem);
372 spin_lock(&vfsmount_lock);
374 if (atomic_read(&sb->s_active) == 1) {
375 /* last instance - try to be smart */
376 spin_unlock(&vfsmount_lock);
381 security_sb_umount_close(mnt);
382 spin_lock(&vfsmount_lock);
385 if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
386 if (!list_empty(&mnt->mnt_list))
390 spin_unlock(&vfsmount_lock);
392 security_sb_umount_busy(mnt);
393 up_write(¤t->namespace->sem);
398 * Now umount can handle mount points as well as block devices.
399 * This is important for filesystems which use unnamed block devices.
401 * We now support a flag for forced unmount like the other 'big iron'
402 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
405 asmlinkage long sys_umount(char __user * name, int flags)
410 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
414 if (nd.dentry != nd.mnt->mnt_root)
416 if (!check_mnt(nd.mnt))
420 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
423 retval = do_umount(nd.mnt, flags);
431 * The 2.0 compatible umount. No flags.
434 asmlinkage long sys_oldumount(char __user * name)
436 return sys_umount(name,0);
439 static int mount_is_safe(struct nameidata *nd)
441 if (capable(CAP_SYS_ADMIN))
443 if (vx_ccaps(VXC_SECURE_MOUNT))
447 if (S_ISLNK(nd->dentry->d_inode->i_mode))
449 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
450 if (current->uid != nd->dentry->d_inode->i_uid)
453 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
460 lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
465 if (d == NULL || d == d->d_parent)
471 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
473 struct vfsmount *res, *p, *q, *r, *s;
477 res = q = clone_mnt(mnt, dentry);
480 q->mnt_mountpoint = mnt->mnt_mountpoint;
483 for (h = mnt->mnt_mounts.next; h != &mnt->mnt_mounts; h = h->next) {
484 r = list_entry(h, struct vfsmount, mnt_child);
485 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
488 for (s = r; s; s = next_mnt(s, r)) {
489 while (p != s->mnt_parent) {
495 nd.dentry = p->mnt_mountpoint;
496 q = clone_mnt(p, p->mnt_root);
499 spin_lock(&vfsmount_lock);
500 list_add_tail(&q->mnt_list, &res->mnt_list);
502 spin_unlock(&vfsmount_lock);
508 spin_lock(&vfsmount_lock);
510 spin_unlock(&vfsmount_lock);
515 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
518 if (mnt->mnt_sb->s_flags & MS_NOUSER)
521 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
522 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
526 down(&nd->dentry->d_inode->i_sem);
527 if (IS_DEADDIR(nd->dentry->d_inode))
530 err = security_sb_check_sb(mnt, nd);
535 spin_lock(&vfsmount_lock);
536 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
537 struct list_head head;
540 list_add_tail(&head, &mnt->mnt_list);
541 list_splice(&head, current->namespace->list.prev);
545 spin_unlock(&vfsmount_lock);
547 up(&nd->dentry->d_inode->i_sem);
549 security_sb_post_addmount(mnt, nd);
556 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
558 struct nameidata old_nd;
559 struct vfsmount *mnt = NULL;
560 int err = mount_is_safe(nd);
563 if (!old_name || !*old_name)
565 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
569 down_write(¤t->namespace->sem);
571 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
574 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
576 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
580 err = graft_tree(mnt, nd);
582 spin_lock(&vfsmount_lock);
584 spin_unlock(&vfsmount_lock);
589 up_write(¤t->namespace->sem);
590 path_release(&old_nd);
595 * change filesystem flags. dir should be a physical root of filesystem.
596 * If you've mounted a non-root directory somewhere and want to do remount
597 * on it - tough luck.
600 static int do_remount(struct nameidata *nd,int flags,int mnt_flags,void *data)
603 struct super_block * sb = nd->mnt->mnt_sb;
605 if (!capable(CAP_SYS_ADMIN))
608 if (!check_mnt(nd->mnt))
611 if (nd->dentry != nd->mnt->mnt_root)
614 down_write(&sb->s_umount);
615 err = do_remount_sb(sb, flags, data, 0);
617 nd->mnt->mnt_flags=mnt_flags;
618 up_write(&sb->s_umount);
620 security_sb_post_remount(nd->mnt, flags, data);
624 static int do_move_mount(struct nameidata *nd, char *old_name)
626 struct nameidata old_nd, parent_nd;
629 if (!capable(CAP_SYS_ADMIN))
631 if (!old_name || !*old_name)
633 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
637 down_write(¤t->namespace->sem);
638 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
641 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
645 down(&nd->dentry->d_inode->i_sem);
646 if (IS_DEADDIR(nd->dentry->d_inode))
649 spin_lock(&vfsmount_lock);
650 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
654 if (old_nd.dentry != old_nd.mnt->mnt_root)
657 if (old_nd.mnt == old_nd.mnt->mnt_parent)
660 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
661 S_ISDIR(old_nd.dentry->d_inode->i_mode))
665 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
670 detach_mnt(old_nd.mnt, &parent_nd);
671 attach_mnt(old_nd.mnt, nd);
673 spin_unlock(&vfsmount_lock);
675 up(&nd->dentry->d_inode->i_sem);
677 up_write(¤t->namespace->sem);
679 path_release(&parent_nd);
680 path_release(&old_nd);
684 static int do_add_mount(struct nameidata *nd, char *type, int flags,
685 int mnt_flags, char *name, void *data)
687 struct vfsmount *mnt;
690 if (!type || !memchr(type, 0, PAGE_SIZE))
693 /* we need capabilities... */
694 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
697 mnt = do_kern_mount(type, flags, name, data);
702 down_write(¤t->namespace->sem);
703 /* Something was mounted here while we slept */
704 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
707 if (!check_mnt(nd->mnt))
710 /* Refuse the same filesystem on the same mount point */
712 if (nd->mnt->mnt_sb == mnt->mnt_sb && nd->mnt->mnt_root == nd->dentry)
716 if (S_ISLNK(mnt->mnt_root->d_inode->i_mode))
719 mnt->mnt_flags = mnt_flags;
720 err = graft_tree(mnt, nd);
722 up_write(¤t->namespace->sem);
728 int copy_mount_options (const void __user *data, unsigned long *where)
738 if (!(page = __get_free_page(GFP_KERNEL)))
741 /* We only care that *some* data at the address the user
742 * gave us is valid. Just in case, we'll zero
743 * the remainder of the page.
745 /* copy_from_user cannot cross TASK_SIZE ! */
746 size = TASK_SIZE - (unsigned long)data;
747 if (size > PAGE_SIZE)
750 i = size - copy_from_user((void *)page, data, size);
756 memset((char *)page + i, 0, PAGE_SIZE - i);
762 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
763 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
765 * data is a (void *) that can point to any structure up to
766 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
767 * information (or be NULL).
769 * Pre-0.97 versions of mount() didn't have a flags word.
770 * When the flags word was introduced its top half was required
771 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
772 * Therefore, if this magic number is present, it carries no information
773 * and must be discarded.
775 long do_mount(char * dev_name, char * dir_name, char *type_page,
776 unsigned long flags, void *data_page)
783 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
784 flags &= ~MS_MGC_MSK;
786 /* Basic sanity checks */
788 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
790 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
794 ((char *)data_page)[PAGE_SIZE - 1] = 0;
796 /* Separate the per-mountpoint flags */
797 if (flags & MS_NOSUID)
798 mnt_flags |= MNT_NOSUID;
799 if (flags & MS_NODEV)
800 mnt_flags |= MNT_NODEV;
801 if (flags & MS_NOEXEC)
802 mnt_flags |= MNT_NOEXEC;
803 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
805 if (vx_ccaps(VXC_SECURE_MOUNT))
806 mnt_flags |= MNT_NODEV;
808 /* ... and get the mountpoint */
809 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
813 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
817 if (flags & MS_REMOUNT)
818 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
820 else if (flags & MS_BIND)
821 retval = do_loopback(&nd, dev_name, flags & MS_REC);
822 else if (flags & MS_MOVE)
823 retval = do_move_mount(&nd, dev_name);
825 retval = do_add_mount(&nd, type_page, flags, mnt_flags,
826 dev_name, data_page);
832 int copy_namespace(int flags, struct task_struct *tsk)
834 struct namespace *namespace = tsk->namespace;
835 struct namespace *new_ns;
836 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
837 struct fs_struct *fs = tsk->fs;
842 get_namespace(namespace);
844 if (!(flags & CLONE_NEWNS))
847 if (!capable(CAP_SYS_ADMIN)) {
848 put_namespace(namespace);
852 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
856 atomic_set(&new_ns->count, 1);
857 init_rwsem(&new_ns->sem);
858 INIT_LIST_HEAD(&new_ns->list);
860 down_write(&tsk->namespace->sem);
861 /* First pass: copy the tree topology */
862 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
864 up_write(&tsk->namespace->sem);
868 spin_lock(&vfsmount_lock);
869 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
870 spin_unlock(&vfsmount_lock);
872 /* Second pass: switch the tsk->fs->* elements */
874 struct vfsmount *p, *q;
875 write_lock(&fs->lock);
880 if (p == fs->rootmnt) {
882 fs->rootmnt = mntget(q);
884 if (p == fs->pwdmnt) {
886 fs->pwdmnt = mntget(q);
888 if (p == fs->altrootmnt) {
890 fs->altrootmnt = mntget(q);
892 p = next_mnt(p, namespace->root);
893 q = next_mnt(q, new_ns->root);
895 write_unlock(&fs->lock);
897 up_write(&tsk->namespace->sem);
899 tsk->namespace = new_ns;
908 put_namespace(namespace);
912 put_namespace(namespace);
916 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
917 char __user * type, unsigned long flags,
921 unsigned long data_page;
922 unsigned long type_page;
923 unsigned long dev_page;
926 retval = copy_mount_options (type, &type_page);
930 dir_page = getname(dir_name);
931 retval = PTR_ERR(dir_page);
932 if (IS_ERR(dir_page))
935 retval = copy_mount_options (dev_name, &dev_page);
939 retval = copy_mount_options (data, &data_page);
944 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
945 flags, (void*)data_page);
947 free_page(data_page);
954 free_page(type_page);
959 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
960 * It can block. Requires the big lock held.
962 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
963 struct dentry *dentry)
965 struct dentry *old_root;
966 struct vfsmount *old_rootmnt;
967 write_lock(&fs->lock);
969 old_rootmnt = fs->rootmnt;
970 fs->rootmnt = mntget(mnt);
971 fs->root = dget(dentry);
972 write_unlock(&fs->lock);
979 EXPORT_SYMBOL(set_fs_root);
982 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
983 * It can block. Requires the big lock held.
985 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
986 struct dentry *dentry)
988 struct dentry *old_pwd;
989 struct vfsmount *old_pwdmnt;
991 write_lock(&fs->lock);
993 old_pwdmnt = fs->pwdmnt;
994 fs->pwdmnt = mntget(mnt);
995 fs->pwd = dget(dentry);
996 write_unlock(&fs->lock);
1004 EXPORT_SYMBOL(set_fs_pwd);
1006 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1008 struct task_struct *g, *p;
1009 struct fs_struct *fs;
1011 read_lock(&tasklist_lock);
1012 do_each_thread(g, p) {
1016 atomic_inc(&fs->count);
1018 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1019 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1020 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1021 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1025 } while_each_thread(g, p);
1026 read_unlock(&tasklist_lock);
1030 * Moves the current root to put_root, and sets root/cwd of all processes
1031 * which had them on the old root to new_root.
1034 * - we don't move root/cwd if they are not at the root (reason: if something
1035 * cared enough to change them, it's probably wrong to force them elsewhere)
1036 * - it's okay to pick a root that isn't the root of a file system, e.g.
1037 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1038 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1042 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1044 struct vfsmount *tmp;
1045 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1048 if (!capable(CAP_SYS_ADMIN))
1053 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1057 if (!check_mnt(new_nd.mnt))
1060 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1064 error = security_sb_pivotroot(&old_nd, &new_nd);
1066 path_release(&old_nd);
1070 read_lock(¤t->fs->lock);
1071 user_nd.mnt = mntget(current->fs->rootmnt);
1072 user_nd.dentry = dget(current->fs->root);
1073 read_unlock(¤t->fs->lock);
1074 down_write(¤t->namespace->sem);
1075 down(&old_nd.dentry->d_inode->i_sem);
1077 if (!check_mnt(user_nd.mnt))
1080 if (IS_DEADDIR(new_nd.dentry->d_inode))
1082 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1084 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1087 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1088 goto out2; /* loop */
1090 if (user_nd.mnt->mnt_root != user_nd.dentry)
1092 if (new_nd.mnt->mnt_root != new_nd.dentry)
1093 goto out2; /* not a mountpoint */
1094 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1095 spin_lock(&vfsmount_lock);
1096 if (tmp != new_nd.mnt) {
1098 if (tmp->mnt_parent == tmp)
1100 if (tmp->mnt_parent == new_nd.mnt)
1102 tmp = tmp->mnt_parent;
1104 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1106 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1108 detach_mnt(new_nd.mnt, &parent_nd);
1109 detach_mnt(user_nd.mnt, &root_parent);
1110 attach_mnt(user_nd.mnt, &old_nd);
1111 attach_mnt(new_nd.mnt, &root_parent);
1112 spin_unlock(&vfsmount_lock);
1113 chroot_fs_refs(&user_nd, &new_nd);
1114 security_sb_post_pivotroot(&user_nd, &new_nd);
1116 path_release(&root_parent);
1117 path_release(&parent_nd);
1119 up(&old_nd.dentry->d_inode->i_sem);
1120 up_write(¤t->namespace->sem);
1121 path_release(&user_nd);
1122 path_release(&old_nd);
1124 path_release(&new_nd);
1129 spin_unlock(&vfsmount_lock);
1133 static void __init init_mount_tree(void)
1135 struct vfsmount *mnt;
1136 struct namespace *namespace;
1137 struct task_struct *g, *p;
1139 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1141 panic("Can't create rootfs");
1142 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1144 panic("Can't allocate initial namespace");
1145 atomic_set(&namespace->count, 1);
1146 INIT_LIST_HEAD(&namespace->list);
1147 init_rwsem(&namespace->sem);
1148 list_add(&mnt->mnt_list, &namespace->list);
1149 namespace->root = mnt;
1151 init_task.namespace = namespace;
1152 read_lock(&tasklist_lock);
1153 do_each_thread(g, p) {
1154 get_namespace(namespace);
1155 p->namespace = namespace;
1156 } while_each_thread(g, p);
1157 read_unlock(&tasklist_lock);
1159 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1160 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1163 void __init mnt_init(unsigned long mempages)
1165 struct list_head *d;
1166 unsigned long order;
1167 unsigned int nr_hash;
1170 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1171 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
1173 panic("Cannot create vfsmount cache");
1176 mount_hashtable = (struct list_head *)
1177 __get_free_pages(GFP_ATOMIC, order);
1179 if (!mount_hashtable)
1180 panic("Failed to allocate mount hash table\n");
1183 * Find the power-of-two list-heads that can fit into the allocation..
1184 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1187 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1191 } while ((nr_hash >> hash_bits) != 0);
1195 * Re-calculate the actual number of entries and the mask
1196 * from the number of bits we can fit.
1198 nr_hash = 1UL << hash_bits;
1199 hash_mask = nr_hash-1;
1201 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1202 nr_hash, order, (PAGE_SIZE << order));
1204 /* And initialize the newly allocated array */
1205 d = mount_hashtable;
1217 void __put_namespace(struct namespace *namespace)
1219 down_write(&namespace->sem);
1220 spin_lock(&vfsmount_lock);
1221 umount_tree(namespace->root);
1222 spin_unlock(&vfsmount_lock);
1223 up_write(&namespace->sem);