fix deadlock in pivot_root()
[pandora-kernel.git] / fs / namespace.c
index 7b0b953..46cc26b 100644 (file)
@@ -196,7 +196,7 @@ unsigned int mnt_get_count(struct vfsmount *mnt)
 #endif
 }
 
-struct vfsmount *alloc_vfsmnt(const char *name)
+static struct vfsmount *alloc_vfsmnt(const char *name)
 {
        struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
        if (mnt) {
@@ -466,15 +466,7 @@ static void __mnt_unmake_readonly(struct vfsmount *mnt)
        br_write_unlock(vfsmount_lock);
 }
 
-void simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
-{
-       mnt->mnt_sb = sb;
-       mnt->mnt_root = dget(sb->s_root);
-}
-
-EXPORT_SYMBOL(simple_set_mnt);
-
-void free_vfsmnt(struct vfsmount *mnt)
+static void free_vfsmnt(struct vfsmount *mnt)
 {
        kfree(mnt->mnt_devname);
        mnt_free_id(mnt);
@@ -678,6 +670,36 @@ static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
        return p;
 }
 
+struct vfsmount *
+vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
+{
+       struct vfsmount *mnt;
+       struct dentry *root;
+
+       if (!type)
+               return ERR_PTR(-ENODEV);
+
+       mnt = alloc_vfsmnt(name);
+       if (!mnt)
+               return ERR_PTR(-ENOMEM);
+
+       if (flags & MS_KERNMOUNT)
+               mnt->mnt_flags = MNT_INTERNAL;
+
+       root = mount_fs(type, flags, name, data);
+       if (IS_ERR(root)) {
+               free_vfsmnt(mnt);
+               return ERR_CAST(root);
+       }
+
+       mnt->mnt_root = root;
+       mnt->mnt_sb = root->d_sb;
+       mnt->mnt_mountpoint = mnt->mnt_root;
+       mnt->mnt_parent = mnt;
+       return mnt;
+}
+EXPORT_SYMBOL_GPL(vfs_kern_mount);
+
 static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
                                        int flag)
 {
@@ -978,7 +1000,13 @@ static int show_vfsmnt(struct seq_file *m, void *v)
        int err = 0;
        struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
 
-       mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
+       if (mnt->mnt_sb->s_op->show_devname) {
+               err = mnt->mnt_sb->s_op->show_devname(m, mnt);
+               if (err)
+                       goto out;
+       } else {
+               mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
+       }
        seq_putc(m, ' ');
        seq_path(m, &mnt_path, " \t\n\\");
        seq_putc(m, ' ');
@@ -1002,6 +1030,18 @@ const struct seq_operations mounts_op = {
        .show   = show_vfsmnt
 };
 
+static int uuid_is_nil(u8 *uuid)
+{
+       int i;
+       u8  *cp = (u8 *)uuid;
+
+       for (i = 0; i < 16; i++) {
+               if (*cp++)
+                       return 0;
+       }
+       return 1;
+}
+
 static int show_mountinfo(struct seq_file *m, void *v)
 {
        struct proc_mounts *p = m->private;
@@ -1013,7 +1053,12 @@ static int show_mountinfo(struct seq_file *m, void *v)
 
        seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id,
                   MAJOR(sb->s_dev), MINOR(sb->s_dev));
-       seq_dentry(m, mnt->mnt_root, " \t\n\\");
+       if (sb->s_op->show_path)
+               err = sb->s_op->show_path(m, mnt);
+       else
+               seq_dentry(m, mnt->mnt_root, " \t\n\\");
+       if (err)
+               goto out;
        seq_putc(m, ' ');
        seq_path_root(m, &mnt_path, &root, " \t\n\\");
        if (root.mnt != p->root.mnt || root.dentry != p->root.dentry) {
@@ -1040,11 +1085,20 @@ static int show_mountinfo(struct seq_file *m, void *v)
        if (IS_MNT_UNBINDABLE(mnt))
                seq_puts(m, " unbindable");
 
+       if (!uuid_is_nil(mnt->mnt_sb->s_uuid))
+               /* print the uuid */
+               seq_printf(m, " uuid:%pU", mnt->mnt_sb->s_uuid);
+
        /* Filesystem specific data */
        seq_puts(m, " - ");
        show_type(m, sb);
        seq_putc(m, ' ');
-       mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
+       if (sb->s_op->show_devname)
+               err = sb->s_op->show_devname(m, mnt);
+       else
+               mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
+       if (err)
+               goto out;
        seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
        err = show_sb_opts(m, sb);
        if (err)
@@ -1070,11 +1124,15 @@ static int show_vfsstat(struct seq_file *m, void *v)
        int err = 0;
 
        /* device */
-       if (mnt->mnt_devname) {
-               seq_puts(m, "device ");
-               mangle(m, mnt->mnt_devname);
-       } else
-               seq_puts(m, "no device");
+       if (mnt->mnt_sb->s_op->show_devname) {
+               err = mnt->mnt_sb->s_op->show_devname(m, mnt);
+       } else {
+               if (mnt->mnt_devname) {
+                       seq_puts(m, "device ");
+                       mangle(m, mnt->mnt_devname);
+               } else
+                       seq_puts(m, "no device");
+       }
 
        /* mount point */
        seq_puts(m, " mounted on ");
@@ -1088,7 +1146,8 @@ static int show_vfsstat(struct seq_file *m, void *v)
        /* optional statistics */
        if (mnt->mnt_sb->s_op->show_stats) {
                seq_putc(m, ' ');
-               err = mnt->mnt_sb->s_op->show_stats(m, mnt);
+               if (!err)
+                       err = mnt->mnt_sb->s_op->show_stats(m, mnt);
        }
 
        seq_putc(m, '\n');
@@ -1244,7 +1303,7 @@ static int do_umount(struct vfsmount *mnt, int flags)
                 */
                br_write_lock(vfsmount_lock);
                if (mnt_get_count(mnt) != 2) {
-                       br_write_lock(vfsmount_lock);
+                       br_write_unlock(vfsmount_lock);
                        return -EBUSY;
                }
                br_write_unlock(vfsmount_lock);
@@ -1767,6 +1826,10 @@ static int do_remount(struct path *path, int flags, int mnt_flags,
        if (path->dentry != path->mnt->mnt_root)
                return -EINVAL;
 
+       err = security_sb_remount(sb, data);
+       if (err)
+               return err;
+
        down_write(&sb->s_umount);
        if (flags & MS_BIND)
                err = change_mount_flags(path->mnt, flags);
@@ -1872,7 +1935,81 @@ out:
        return err;
 }
 
-static int do_add_mount(struct vfsmount *, struct path *, int);
+static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
+{
+       int err;
+       const char *subtype = strchr(fstype, '.');
+       if (subtype) {
+               subtype++;
+               err = -EINVAL;
+               if (!subtype[0])
+                       goto err;
+       } else
+               subtype = "";
+
+       mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
+       err = -ENOMEM;
+       if (!mnt->mnt_sb->s_subtype)
+               goto err;
+       return mnt;
+
+ err:
+       mntput(mnt);
+       return ERR_PTR(err);
+}
+
+struct vfsmount *
+do_kern_mount(const char *fstype, int flags, const char *name, void *data)
+{
+       struct file_system_type *type = get_fs_type(fstype);
+       struct vfsmount *mnt;
+       if (!type)
+               return ERR_PTR(-ENODEV);
+       mnt = vfs_kern_mount(type, flags, name, data);
+       if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
+           !mnt->mnt_sb->s_subtype)
+               mnt = fs_set_subtype(mnt, fstype);
+       put_filesystem(type);
+       return mnt;
+}
+EXPORT_SYMBOL_GPL(do_kern_mount);
+
+/*
+ * add a mount into a namespace's mount tree
+ */
+static int do_add_mount(struct vfsmount *newmnt, struct path *path, int mnt_flags)
+{
+       int err;
+
+       mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
+
+       down_write(&namespace_sem);
+       /* Something was mounted here while we slept */
+       err = follow_down(path, true);
+       if (err < 0)
+               goto unlock;
+
+       err = -EINVAL;
+       if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))
+               goto unlock;
+
+       /* Refuse the same filesystem on the same mount point */
+       err = -EBUSY;
+       if (path->mnt->mnt_sb == newmnt->mnt_sb &&
+           path->mnt->mnt_root == path->dentry)
+               goto unlock;
+
+       err = -EINVAL;
+       if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
+               goto unlock;
+
+       newmnt->mnt_flags = mnt_flags;
+       err = graft_tree(newmnt, path);
+
+unlock:
+       up_write(&namespace_sem);
+       return err;
+}
 
 /*
  * create a new mount for userspace and request it to be added into the
@@ -1932,43 +2069,6 @@ fail:
        return err;
 }
 
-/*
- * add a mount into a namespace's mount tree
- */
-static int do_add_mount(struct vfsmount *newmnt, struct path *path, int mnt_flags)
-{
-       int err;
-
-       mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
-
-       down_write(&namespace_sem);
-       /* Something was mounted here while we slept */
-       err = follow_down(path, true);
-       if (err < 0)
-               goto unlock;
-
-       err = -EINVAL;
-       if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))
-               goto unlock;
-
-       /* Refuse the same filesystem on the same mount point */
-       err = -EBUSY;
-       if (path->mnt->mnt_sb == newmnt->mnt_sb &&
-           path->mnt->mnt_root == path->dentry)
-               goto unlock;
-
-       err = -EINVAL;
-       if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
-               goto unlock;
-
-       newmnt->mnt_flags = mnt_flags;
-       err = graft_tree(newmnt, path);
-
-unlock:
-       up_write(&namespace_sem);
-       return err;
-}
-
 /**
  * mnt_set_expiry - Put a mount on an expiration list
  * @mnt: The mount to list.
@@ -2469,9 +2569,6 @@ SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
        error = user_path_dir(new_root, &new);
        if (error)
                goto out0;
-       error = -EINVAL;
-       if (!check_mnt(new.mnt))
-               goto out1;
 
        error = user_path_dir(put_old, &old);
        if (error)
@@ -2491,7 +2588,7 @@ SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
                IS_MNT_SHARED(new.mnt->mnt_parent) ||
                IS_MNT_SHARED(root.mnt->mnt_parent))
                goto out2;
-       if (!check_mnt(root.mnt))
+       if (!check_mnt(root.mnt) || !check_mnt(new.mnt))
                goto out2;
        error = -ENOENT;
        if (cant_mount(old.dentry))
@@ -2515,19 +2612,19 @@ SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
                goto out2; /* not attached */
        /* make sure we can reach put_old from new_root */
        tmp = old.mnt;
-       br_write_lock(vfsmount_lock);
        if (tmp != new.mnt) {
                for (;;) {
                        if (tmp->mnt_parent == tmp)
-                               goto out3; /* already mounted on put_old */
+                               goto out2; /* already mounted on put_old */
                        if (tmp->mnt_parent == new.mnt)
                                break;
                        tmp = tmp->mnt_parent;
                }
                if (!is_subdir(tmp->mnt_mountpoint, new.dentry))
-                       goto out3;
+                       goto out2;
        } else if (!is_subdir(old.dentry, new.dentry))
-               goto out3;
+               goto out2;
+       br_write_lock(vfsmount_lock);
        detach_mnt(new.mnt, &parent_path);
        detach_mnt(root.mnt, &root_parent);
        /* mount old root on put_old */
@@ -2550,9 +2647,6 @@ out1:
        path_put(&new);
 out0:
        return error;
-out3:
-       br_write_unlock(vfsmount_lock);
-       goto out2;
 }
 
 static void __init init_mount_tree(void)
@@ -2627,3 +2721,9 @@ void put_mnt_ns(struct mnt_namespace *ns)
        kfree(ns);
 }
 EXPORT_SYMBOL(put_mnt_ns);
+
+struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
+{
+       return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
+}
+EXPORT_SYMBOL_GPL(kern_mount_data);