Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / fs / namespace.c
index cd63893..e608199 100644 (file)
@@ -9,30 +9,17 @@
  */
 
 #include <linux/syscalls.h>
-#include <linux/slab.h>
-#include <linux/sched.h>
-#include <linux/spinlock.h>
-#include <linux/percpu.h>
-#include <linux/init.h>
-#include <linux/kernel.h>
-#include <linux/acct.h>
+#include <linux/export.h>
 #include <linux/capability.h>
-#include <linux/cpumask.h>
-#include <linux/module.h>
-#include <linux/sysfs.h>
-#include <linux/seq_file.h>
 #include <linux/mnt_namespace.h>
 #include <linux/namei.h>
-#include <linux/nsproxy.h>
 #include <linux/security.h>
-#include <linux/mount.h>
-#include <linux/ramfs.h>
-#include <linux/log2.h>
 #include <linux/idr.h>
-#include <linux/fs_struct.h>
-#include <linux/fsnotify.h>
-#include <asm/uaccess.h>
-#include <asm/unistd.h>
+#include <linux/acct.h>                /* acct_auto_close_mnt */
+#include <linux/ramfs.h>       /* init_rootfs */
+#include <linux/fs_struct.h>   /* get_fs_root et.al. */
+#include <linux/fsnotify.h>    /* fsnotify_vfsmount_delete */
+#include <linux/uaccess.h>
 #include "pnode.h"
 #include "internal.h"
 
@@ -286,6 +273,15 @@ static unsigned int mnt_get_writers(struct mount *mnt)
 #endif
 }
 
+static int mnt_is_readonly(struct vfsmount *mnt)
+{
+       if (mnt->mnt_sb->s_readonly_remount)
+               return 1;
+       /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
+       smp_rmb();
+       return __mnt_is_readonly(mnt);
+}
+
 /*
  * Most r/o checks on a fs are for operations that take
  * discrete amounts of time, like a write() or unlink().
@@ -325,12 +321,10 @@ int mnt_want_write(struct vfsmount *m)
         * MNT_WRITE_HOLD is cleared.
         */
        smp_rmb();
-       if (__mnt_is_readonly(m)) {
+       if (mnt_is_readonly(m)) {
                mnt_dec_writers(mnt);
                ret = -EROFS;
-               goto out;
        }
-out:
        preempt_enable();
        return ret;
 }
@@ -448,6 +442,42 @@ static void __mnt_unmake_readonly(struct mount *mnt)
        br_write_unlock(vfsmount_lock);
 }
 
+int sb_prepare_remount_readonly(struct super_block *sb)
+{
+       struct mount *mnt;
+       int err = 0;
+
+       /* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
+       if (atomic_long_read(&sb->s_remove_count))
+               return -EBUSY;
+
+       br_write_lock(vfsmount_lock);
+       list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
+               if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
+                       mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
+                       smp_mb();
+                       if (mnt_get_writers(mnt) > 0) {
+                               err = -EBUSY;
+                               break;
+                       }
+               }
+       }
+       if (!err && atomic_long_read(&sb->s_remove_count))
+               err = -EBUSY;
+
+       if (!err) {
+               sb->s_readonly_remount = 1;
+               smp_wmb();
+       }
+       list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
+               if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
+                       mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
+       }
+       br_write_unlock(vfsmount_lock);
+
+       return err;
+}
+
 static void free_vfsmnt(struct mount *mnt)
 {
        kfree(mnt->mnt_devname);
@@ -684,6 +714,9 @@ vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void
        mnt->mnt.mnt_sb = root->d_sb;
        mnt->mnt_mountpoint = mnt->mnt.mnt_root;
        mnt->mnt_parent = mnt;
+       br_write_lock(vfsmount_lock);
+       list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
+       br_write_unlock(vfsmount_lock);
        return &mnt->mnt;
 }
 EXPORT_SYMBOL_GPL(vfs_kern_mount);
@@ -712,6 +745,9 @@ static struct mount *clone_mnt(struct mount *old, struct dentry *root,
                mnt->mnt.mnt_root = dget(root);
                mnt->mnt_mountpoint = mnt->mnt.mnt_root;
                mnt->mnt_parent = mnt;
+               br_write_lock(vfsmount_lock);
+               list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
+               br_write_unlock(vfsmount_lock);
 
                if (flag & CL_SLAVE) {
                        list_add(&mnt->mnt_slave, &old->mnt_slave_list);
@@ -794,6 +830,7 @@ put_again:
                acct_auto_close_mnt(&mnt->mnt);
                goto put_again;
        }
+       list_del(&mnt->mnt_instance);
        br_write_unlock(vfsmount_lock);
        mntfree(mnt);
 }
@@ -849,12 +886,12 @@ static inline void mangle(struct seq_file *m, const char *s)
  *
  * See also save_mount_options().
  */
-int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
+int generic_show_options(struct seq_file *m, struct dentry *root)
 {
        const char *options;
 
        rcu_read_lock();
-       options = rcu_dereference(mnt->mnt_sb->s_options);
+       options = rcu_dereference(root->d_sb->s_options);
 
        if (options != NULL && options[0]) {
                seq_putc(m, ',');
@@ -898,10 +935,10 @@ void replace_mount_options(struct super_block *sb, char *options)
 EXPORT_SYMBOL(replace_mount_options);
 
 #ifdef CONFIG_PROC_FS
-/* iterator */
+/* iterator; we want it to have access to namespace_sem, thus here... */
 static void *m_start(struct seq_file *m, loff_t *pos)
 {
-       struct proc_mounts *p = m->private;
+       struct proc_mounts *p = container_of(m, struct proc_mounts, m);
 
        down_read(&namespace_sem);
        return seq_list_start(&p->ns->list, *pos);
@@ -909,7 +946,7 @@ static void *m_start(struct seq_file *m, loff_t *pos)
 
 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
 {
-       struct proc_mounts *p = m->private;
+       struct proc_mounts *p = container_of(m, struct proc_mounts, m);
 
        return seq_list_next(v, &p->ns->list, pos);
 }
@@ -919,222 +956,18 @@ static void m_stop(struct seq_file *m, void *v)
        up_read(&namespace_sem);
 }
 
-int mnt_had_events(struct proc_mounts *p)
-{
-       struct mnt_namespace *ns = p->ns;
-       int res = 0;
-
-       br_read_lock(vfsmount_lock);
-       if (p->m.poll_event != ns->event) {
-               p->m.poll_event = ns->event;
-               res = 1;
-       }
-       br_read_unlock(vfsmount_lock);
-
-       return res;
-}
-
-struct proc_fs_info {
-       int flag;
-       const char *str;
-};
-
-static int show_sb_opts(struct seq_file *m, struct super_block *sb)
-{
-       static const struct proc_fs_info fs_info[] = {
-               { MS_SYNCHRONOUS, ",sync" },
-               { MS_DIRSYNC, ",dirsync" },
-               { MS_MANDLOCK, ",mand" },
-               { 0, NULL }
-       };
-       const struct proc_fs_info *fs_infop;
-
-       for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
-               if (sb->s_flags & fs_infop->flag)
-                       seq_puts(m, fs_infop->str);
-       }
-
-       return security_sb_show_options(m, sb);
-}
-
-static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
-{
-       static const struct proc_fs_info mnt_info[] = {
-               { MNT_NOSUID, ",nosuid" },
-               { MNT_NODEV, ",nodev" },
-               { MNT_NOEXEC, ",noexec" },
-               { MNT_NOATIME, ",noatime" },
-               { MNT_NODIRATIME, ",nodiratime" },
-               { MNT_RELATIME, ",relatime" },
-               { 0, NULL }
-       };
-       const struct proc_fs_info *fs_infop;
-
-       for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
-               if (mnt->mnt_flags & fs_infop->flag)
-                       seq_puts(m, fs_infop->str);
-       }
-}
-
-static void show_type(struct seq_file *m, struct super_block *sb)
-{
-       mangle(m, sb->s_type->name);
-       if (sb->s_subtype && sb->s_subtype[0]) {
-               seq_putc(m, '.');
-               mangle(m, sb->s_subtype);
-       }
-}
-
-static int show_vfsmnt(struct seq_file *m, void *v)
+static int m_show(struct seq_file *m, void *v)
 {
+       struct proc_mounts *p = container_of(m, struct proc_mounts, m);
        struct mount *r = list_entry(v, struct mount, mnt_list);
-       struct vfsmount *mnt = &r->mnt;
-       int err = 0;
-       struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
-
-       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, r->mnt_devname ? r->mnt_devname : "none");
-       }
-       seq_putc(m, ' ');
-       seq_path(m, &mnt_path, " \t\n\\");
-       seq_putc(m, ' ');
-       show_type(m, mnt->mnt_sb);
-       seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
-       err = show_sb_opts(m, mnt->mnt_sb);
-       if (err)
-               goto out;
-       show_mnt_opts(m, mnt);
-       if (mnt->mnt_sb->s_op->show_options)
-               err = mnt->mnt_sb->s_op->show_options(m, mnt);
-       seq_puts(m, " 0 0\n");
-out:
-       return err;
+       return p->show(m, &r->mnt);
 }
 
 const struct seq_operations mounts_op = {
        .start  = m_start,
        .next   = m_next,
        .stop   = m_stop,
-       .show   = show_vfsmnt
-};
-
-static int show_mountinfo(struct seq_file *m, void *v)
-{
-       struct proc_mounts *p = m->private;
-       struct mount *r = list_entry(v, struct mount, mnt_list);
-       struct vfsmount *mnt = &r->mnt;
-       struct super_block *sb = mnt->mnt_sb;
-       struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
-       struct path root = p->root;
-       int err = 0;
-
-       seq_printf(m, "%i %i %u:%u ", r->mnt_id, r->mnt_parent->mnt_id,
-                  MAJOR(sb->s_dev), MINOR(sb->s_dev));
-       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, ' ');
-
-       /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
-       err = seq_path_root(m, &mnt_path, &root, " \t\n\\");
-       if (err)
-               goto out;
-
-       seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
-       show_mnt_opts(m, mnt);
-
-       /* Tagged fields ("foo:X" or "bar") */
-       if (IS_MNT_SHARED(r))
-               seq_printf(m, " shared:%i", r->mnt_group_id);
-       if (IS_MNT_SLAVE(r)) {
-               int master = r->mnt_master->mnt_group_id;
-               int dom = get_dominating_id(r, &p->root);
-               seq_printf(m, " master:%i", master);
-               if (dom && dom != master)
-                       seq_printf(m, " propagate_from:%i", dom);
-       }
-       if (IS_MNT_UNBINDABLE(r))
-               seq_puts(m, " unbindable");
-
-       /* Filesystem specific data */
-       seq_puts(m, " - ");
-       show_type(m, sb);
-       seq_putc(m, ' ');
-       if (sb->s_op->show_devname)
-               err = sb->s_op->show_devname(m, mnt);
-       else
-               mangle(m, r->mnt_devname ? r->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)
-               goto out;
-       if (sb->s_op->show_options)
-               err = sb->s_op->show_options(m, mnt);
-       seq_putc(m, '\n');
-out:
-       return err;
-}
-
-const struct seq_operations mountinfo_op = {
-       .start  = m_start,
-       .next   = m_next,
-       .stop   = m_stop,
-       .show   = show_mountinfo,
-};
-
-static int show_vfsstat(struct seq_file *m, void *v)
-{
-       struct mount *r = list_entry(v, struct mount, mnt_list);
-       struct vfsmount *mnt = &r->mnt;
-       struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
-       int err = 0;
-
-       /* device */
-       if (mnt->mnt_sb->s_op->show_devname) {
-               seq_puts(m, "device ");
-               err = mnt->mnt_sb->s_op->show_devname(m, mnt);
-       } else {
-               if (r->mnt_devname) {
-                       seq_puts(m, "device ");
-                       mangle(m, r->mnt_devname);
-               } else
-                       seq_puts(m, "no device");
-       }
-
-       /* mount point */
-       seq_puts(m, " mounted on ");
-       seq_path(m, &mnt_path, " \t\n\\");
-       seq_putc(m, ' ');
-
-       /* file system type */
-       seq_puts(m, "with fstype ");
-       show_type(m, mnt->mnt_sb);
-
-       /* optional statistics */
-       if (mnt->mnt_sb->s_op->show_stats) {
-               seq_putc(m, ' ');
-               if (!err)
-                       err = mnt->mnt_sb->s_op->show_stats(m, mnt);
-       }
-
-       seq_putc(m, '\n');
-       return err;
-}
-
-const struct seq_operations mountstats_op = {
-       .start  = m_start,
-       .next   = m_next,
-       .stop   = m_stop,
-       .show   = show_vfsstat,
+       .show   = m_show,
 };
 #endif  /* CONFIG_PROC_FS */
 
@@ -2401,7 +2234,7 @@ static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
        struct mnt_namespace *new_ns;
        struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
        struct mount *p, *q;
-       struct mount *old = real_mount(mnt_ns->root);
+       struct mount *old = mnt_ns->root;
        struct mount *new;
 
        new_ns = alloc_mnt_ns();
@@ -2416,7 +2249,7 @@ static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
                kfree(new_ns);
                return ERR_PTR(-ENOMEM);
        }
-       new_ns->root = &new->mnt;
+       new_ns->root = new;
        br_write_lock(vfsmount_lock);
        list_add_tail(&new_ns->list, &new->mnt_list);
        br_write_unlock(vfsmount_lock);
@@ -2486,7 +2319,7 @@ static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
                struct mount *mnt = real_mount(m);
                mnt->mnt_ns = new_ns;
                __mnt_make_longterm(mnt);
-               new_ns->root = m;
+               new_ns->root = mnt;
                list_add(&new_ns->list, &mnt->mnt_list);
        } else {
                mntput(m);
@@ -2716,8 +2549,8 @@ static void __init init_mount_tree(void)
        init_task.nsproxy->mnt_ns = ns;
        get_mnt_ns(ns);
 
-       root.mnt = ns->root;
-       root.dentry = ns->root->mnt_root;
+       root.mnt = mnt;
+       root.dentry = mnt->mnt_root;
 
        set_fs_pwd(current->fs, &root);
        set_fs_root(current->fs, &root);
@@ -2764,7 +2597,7 @@ void put_mnt_ns(struct mnt_namespace *ns)
                return;
        down_write(&namespace_sem);
        br_write_lock(vfsmount_lock);
-       umount_tree(real_mount(ns->root), 0, &umount_list);
+       umount_tree(ns->root, 0, &umount_list);
        br_write_unlock(vfsmount_lock);
        up_write(&namespace_sem);
        release_mounts(&umount_list);