NFS: Add ability to send MOUNTPROC_UMNT to the kernel's mountd client
[safe/jmp/linux-2.6] / fs / nfs / super.c
index b798ed1..83a3107 100644 (file)
@@ -42,6 +42,8 @@
 #include <linux/smp_lock.h>
 #include <linux/seq_file.h>
 #include <linux/mount.h>
+#include <linux/mnt_namespace.h>
+#include <linux/namei.h>
 #include <linux/nfs_idmap.h>
 #include <linux/vfs.h>
 #include <linux/inet.h>
@@ -90,6 +92,7 @@ enum {
        Opt_mountport,
        Opt_mountvers,
        Opt_nfsvers,
+       Opt_minorversion,
 
        /* Mount options that take string arguments */
        Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
@@ -155,6 +158,7 @@ static const match_table_t nfs_mount_option_tokens = {
        { Opt_mountvers, "mountvers=%s" },
        { Opt_nfsvers, "nfsvers=%s" },
        { Opt_nfsvers, "vers=%s" },
+       { Opt_minorversion, "minorversion=%s" },
 
        { Opt_sec, "sec=%s" },
        { Opt_proto, "proto=%s" },
@@ -270,10 +274,14 @@ static const struct super_operations nfs_sops = {
 #ifdef CONFIG_NFS_V4
 static int nfs4_get_sb(struct file_system_type *fs_type,
        int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
+static int nfs4_remote_get_sb(struct file_system_type *fs_type,
+       int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
 static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
        int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
 static int nfs4_referral_get_sb(struct file_system_type *fs_type,
        int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
+static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
+       int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
 static void nfs4_kill_super(struct super_block *sb);
 
 static struct file_system_type nfs4_fs_type = {
@@ -284,6 +292,14 @@ static struct file_system_type nfs4_fs_type = {
        .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
 };
 
+static struct file_system_type nfs4_remote_fs_type = {
+       .owner          = THIS_MODULE,
+       .name           = "nfs4",
+       .get_sb         = nfs4_remote_get_sb,
+       .kill_sb        = nfs4_kill_super,
+       .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
+};
+
 struct file_system_type nfs4_xdev_fs_type = {
        .owner          = THIS_MODULE,
        .name           = "nfs4",
@@ -292,6 +308,14 @@ struct file_system_type nfs4_xdev_fs_type = {
        .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
 };
 
+static struct file_system_type nfs4_remote_referral_fs_type = {
+       .owner          = THIS_MODULE,
+       .name           = "nfs4",
+       .get_sb         = nfs4_remote_referral_get_sb,
+       .kill_sb        = nfs4_kill_super,
+       .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
+};
+
 struct file_system_type nfs4_referral_fs_type = {
        .owner          = THIS_MODULE,
        .name           = "nfs4",
@@ -1247,6 +1271,18 @@ static int nfs_parse_mount_options(char *raw,
                                goto out_invalid_value;
                        }
                        break;
+               case Opt_minorversion:
+                       string = match_strdup(args);
+                       if (string == NULL)
+                               goto out_nomem;
+                       rc = strict_strtoul(string, 10, &option);
+                       kfree(string);
+                       if (rc != 0)
+                               goto out_invalid_value;
+                       if (option > NFS4_MAX_MINOR_VERSION)
+                               goto out_invalid_value;
+                       mnt->minorversion = option;
+                       break;
 
                /*
                 * options that take text values
@@ -2308,6 +2344,7 @@ static int nfs4_validate_mount_data(void *options,
        args->nfs_server.port   = NFS_PORT; /* 2049 unless user set port= */
        args->auth_flavors[0]   = RPC_AUTH_UNIX;
        args->auth_flavor_len   = 0;
+       args->minorversion      = 0;
 
        switch (data->version) {
        case 1:
@@ -2422,12 +2459,12 @@ out_no_client_address:
 }
 
 /*
- * Get the superblock for an NFS4 mountpoint
+ * Get the superblock for the NFS4 root partition
  */
-static int nfs4_get_sb(struct file_system_type *fs_type,
+static int nfs4_remote_get_sb(struct file_system_type *fs_type,
        int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
 {
-       struct nfs_parsed_mount_data *data;
+       struct nfs_parsed_mount_data *data = raw_data;
        struct super_block *s;
        struct nfs_server *server;
        struct nfs_fh *mntfh;
@@ -2438,18 +2475,12 @@ static int nfs4_get_sb(struct file_system_type *fs_type,
        };
        int error = -ENOMEM;
 
-       data = kzalloc(sizeof(*data), GFP_KERNEL);
        mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
        if (data == NULL || mntfh == NULL)
                goto out_free_fh;
 
        security_init_mnt_opts(&data->lsm_opts);
 
-       /* Validate the mount data */
-       error = nfs4_validate_mount_data(raw_data, data, dev_name);
-       if (error < 0)
-               goto out;
-
        /* Get a volume representation */
        server = nfs4_create_server(data, mntfh);
        if (IS_ERR(server)) {
@@ -2462,7 +2493,7 @@ static int nfs4_get_sb(struct file_system_type *fs_type,
                compare_super = NULL;
 
        /* Get a superblock - note that we may end up sharing one that already exists */
-       s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
+       s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
        if (IS_ERR(s)) {
                error = PTR_ERR(s);
                goto out_free;
@@ -2499,14 +2530,9 @@ static int nfs4_get_sb(struct file_system_type *fs_type,
        error = 0;
 
 out:
-       kfree(data->client_address);
-       kfree(data->nfs_server.export_path);
-       kfree(data->nfs_server.hostname);
-       kfree(data->fscache_uniq);
        security_free_mnt_opts(&data->lsm_opts);
 out_free_fh:
        kfree(mntfh);
-       kfree(data);
        return error;
 
 out_free:
@@ -2520,16 +2546,137 @@ error_splat_super:
        goto out;
 }
 
+static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
+               int flags, void *data, const char *hostname)
+{
+       struct vfsmount *root_mnt;
+       char *root_devname;
+       size_t len;
+
+       len = strlen(hostname) + 3;
+       root_devname = kmalloc(len, GFP_KERNEL);
+       if (root_devname == NULL)
+               return ERR_PTR(-ENOMEM);
+       snprintf(root_devname, len, "%s:/", hostname);
+       root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
+       kfree(root_devname);
+       return root_mnt;
+}
+
+static void nfs_fix_devname(const struct path *path, struct vfsmount *mnt)
+{
+       char *page = (char *) __get_free_page(GFP_KERNEL);
+       char *devname, *tmp;
+
+       if (page == NULL)
+               return;
+       devname = nfs_path(path->mnt->mnt_devname,
+                       path->mnt->mnt_root, path->dentry,
+                       page, PAGE_SIZE);
+       if (devname == NULL)
+               goto out_freepage;
+       tmp = kstrdup(devname, GFP_KERNEL);
+       if (tmp == NULL)
+               goto out_freepage;
+       kfree(mnt->mnt_devname);
+       mnt->mnt_devname = tmp;
+out_freepage:
+       free_page((unsigned long)page);
+}
+
+static int nfs_follow_remote_path(struct vfsmount *root_mnt,
+               const char *export_path, struct vfsmount *mnt_target)
+{
+       struct mnt_namespace *ns_private;
+       struct nameidata nd;
+       struct super_block *s;
+       int ret;
+
+       ns_private = create_mnt_ns(root_mnt);
+       ret = PTR_ERR(ns_private);
+       if (IS_ERR(ns_private))
+               goto out_mntput;
+
+       ret = vfs_path_lookup(root_mnt->mnt_root, root_mnt,
+                       export_path, LOOKUP_FOLLOW, &nd);
+
+       put_mnt_ns(ns_private);
+
+       if (ret != 0)
+               goto out_err;
+
+       s = nd.path.mnt->mnt_sb;
+       atomic_inc(&s->s_active);
+       mnt_target->mnt_sb = s;
+       mnt_target->mnt_root = dget(nd.path.dentry);
+
+       /* Correct the device pathname */
+       nfs_fix_devname(&nd.path, mnt_target);
+
+       path_put(&nd.path);
+       down_write(&s->s_umount);
+       return 0;
+out_mntput:
+       mntput(root_mnt);
+out_err:
+       return ret;
+}
+
+/*
+ * Get the superblock for an NFS4 mountpoint
+ */
+static int nfs4_get_sb(struct file_system_type *fs_type,
+       int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
+{
+       struct nfs_parsed_mount_data *data;
+       char *export_path;
+       struct vfsmount *root_mnt;
+       int error = -ENOMEM;
+
+       data = kzalloc(sizeof(*data), GFP_KERNEL);
+       if (data == NULL)
+               goto out_free_data;
+
+       /* Validate the mount data */
+       error = nfs4_validate_mount_data(raw_data, data, dev_name);
+       if (error < 0)
+               goto out;
+
+       export_path = data->nfs_server.export_path;
+       data->nfs_server.export_path = "/";
+       root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, data,
+                       data->nfs_server.hostname);
+       data->nfs_server.export_path = export_path;
+
+       error = PTR_ERR(root_mnt);
+       if (IS_ERR(root_mnt))
+               goto out;
+
+       error = nfs_follow_remote_path(root_mnt, export_path, mnt);
+
+out:
+       kfree(data->client_address);
+       kfree(data->nfs_server.export_path);
+       kfree(data->nfs_server.hostname);
+       kfree(data->fscache_uniq);
+out_free_data:
+       kfree(data);
+       dprintk("<-- nfs4_get_sb() = %d%s\n", error,
+                       error != 0 ? " [error]" : "");
+       return error;
+}
+
 static void nfs4_kill_super(struct super_block *sb)
 {
        struct nfs_server *server = NFS_SB(sb);
 
+       dprintk("--> %s\n", __func__);
        nfs_super_return_all_delegations(sb);
        kill_anon_super(sb);
-
        nfs4_renewd_prepare_shutdown(server);
        nfs_fscache_release_super_cookie(sb);
        nfs_free_server(server);
+       dprintk("<-- %s\n", __func__);
 }
 
 /*
@@ -2615,12 +2762,9 @@ error_splat_super:
        return error;
 }
 
-/*
- * Create an NFS4 server record on referral traversal
- */
-static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
-                               const char *dev_name, void *raw_data,
-                               struct vfsmount *mnt)
+static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
+               int flags, const char *dev_name, void *raw_data,
+               struct vfsmount *mnt)
 {
        struct nfs_clone_mount *data = raw_data;
        struct super_block *s;
@@ -2699,4 +2843,36 @@ error_splat_super:
        return error;
 }
 
+/*
+ * Create an NFS4 server record on referral traversal
+ */
+static int nfs4_referral_get_sb(struct file_system_type *fs_type,
+               int flags, const char *dev_name, void *raw_data,
+               struct vfsmount *mnt)
+{
+       struct nfs_clone_mount *data = raw_data;
+       char *export_path;
+       struct vfsmount *root_mnt;
+       int error;
+
+       dprintk("--> nfs4_referral_get_sb()\n");
+
+       export_path = data->mnt_path;
+       data->mnt_path = "/";
+
+       root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
+                       flags, data, data->hostname);
+       data->mnt_path = export_path;
+
+       error = PTR_ERR(root_mnt);
+       if (IS_ERR(root_mnt))
+               goto out;
+
+       error = nfs_follow_remote_path(root_mnt, export_path, mnt);
+out:
+       dprintk("<-- nfs4_referral_get_sb() = %d%s\n", error,
+                       error != 0 ? " [error]" : "");
+       return error;
+}
+
 #endif /* CONFIG_NFS_V4 */