]> err.no Git - linux-2.6/blobdiff - fs/nfs/super.c
NFS: Set default port for NFSv4, with support for AF_INET6
[linux-2.6] / fs / nfs / super.c
index fa517ae9207f6d04f66fcf4c0b12ef229bc5a176..a88697ff19efc1aa460d324a4d7389f89b5ef4c4 100644 (file)
@@ -45,6 +45,8 @@
 #include <linux/nfs_idmap.h>
 #include <linux/vfs.h>
 #include <linux/inet.h>
+#include <linux/in6.h>
+#include <net/ipv6.h>
 #include <linux/nfs_xdr.h>
 #include <linux/magic.h>
 #include <linux/parser.h>
@@ -83,8 +85,8 @@ enum {
        Opt_actimeo,
        Opt_namelen,
        Opt_mountport,
-       Opt_mountprog, Opt_mountvers,
-       Opt_nfsprog, Opt_nfsvers,
+       Opt_mountvers,
+       Opt_nfsvers,
 
        /* Mount options that take string arguments */
        Opt_sec, Opt_proto, Opt_mountproto,
@@ -137,9 +139,7 @@ static match_table_t nfs_mount_option_tokens = {
        { Opt_userspace, "retry=%u" },
        { Opt_namelen, "namlen=%u" },
        { Opt_mountport, "mountport=%u" },
-       { Opt_mountprog, "mountprog=%u" },
        { Opt_mountvers, "mountvers=%u" },
-       { Opt_nfsprog, "nfsprog=%u" },
        { Opt_nfsvers, "nfsvers=%u" },
        { Opt_nfsvers, "vers=%u" },
 
@@ -202,6 +202,7 @@ static int nfs_get_sb(struct file_system_type *, int, const char *, void *, stru
 static int nfs_xdev_get_sb(struct file_system_type *fs_type,
                int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
 static void nfs_kill_super(struct super_block *);
+static void nfs_put_super(struct super_block *);
 
 static struct file_system_type nfs_fs_type = {
        .owner          = THIS_MODULE,
@@ -223,6 +224,7 @@ static const struct super_operations nfs_sops = {
        .alloc_inode    = nfs_alloc_inode,
        .destroy_inode  = nfs_destroy_inode,
        .write_inode    = nfs_write_inode,
+       .put_super      = nfs_put_super,
        .statfs         = nfs_statfs,
        .clear_inode    = nfs_clear_inode,
        .umount_begin   = nfs_umount_begin,
@@ -325,6 +327,28 @@ void __exit unregister_nfs_fs(void)
        unregister_filesystem(&nfs_fs_type);
 }
 
+void nfs_sb_active(struct nfs_server *server)
+{
+       atomic_inc(&server->active);
+}
+
+void nfs_sb_deactive(struct nfs_server *server)
+{
+       if (atomic_dec_and_test(&server->active))
+               wake_up(&server->active_wq);
+}
+
+static void nfs_put_super(struct super_block *sb)
+{
+       struct nfs_server *server = NFS_SB(sb);
+       /*
+        * Make sure there are no outstanding ops to this server.
+        * If so, wait for them to finish before allowing the
+        * unmount to continue.
+        */
+       wait_event(server->active_wq, atomic_read(&server->active) == 0);
+}
+
 /*
  * Deliver file system statistics to userspace
  */
@@ -469,8 +493,9 @@ static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
 
        nfs_show_mount_options(m, nfss, 0);
 
-       seq_printf(m, ",addr="NIPQUAD_FMT,
-               NIPQUAD(nfss->nfs_client->cl_addr.sin_addr));
+       seq_printf(m, ",addr=%s",
+                       rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
+                                                       RPC_DISPLAY_ADDR));
 
        return 0;
 }
@@ -507,7 +532,7 @@ static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
        seq_printf(m, ",namelen=%d", nfss->namelen);
 
 #ifdef CONFIG_NFS_V4
-       if (nfss->nfs_client->cl_nfsversion == 4) {
+       if (nfss->nfs_client->rpc_ops->version == 4) {
                seq_printf(m, "\n\tnfsv4:\t");
                seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
                seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
@@ -575,16 +600,40 @@ static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
 }
 
 /*
- * Sanity-check a server address provided by the mount command
+ * Set the port number in an address.  Be agnostic about the address family.
+ */
+static void nfs_set_port(struct sockaddr *sap, unsigned short port)
+{
+       switch (sap->sa_family) {
+       case AF_INET: {
+               struct sockaddr_in *ap = (struct sockaddr_in *)sap;
+               ap->sin_port = htons(port);
+               break;
+       }
+       case AF_INET6: {
+               struct sockaddr_in6 *ap = (struct sockaddr_in6 *)sap;
+               ap->sin6_port = htons(port);
+               break;
+       }
+       }
+}
+
+/*
+ * Sanity-check a server address provided by the mount command.
+ *
+ * Address family must be initialized, and address must not be
+ * the ANY address for that family.
  */
 static int nfs_verify_server_address(struct sockaddr *addr)
 {
        switch (addr->sa_family) {
        case AF_INET: {
-               struct sockaddr_in *sa = (struct sockaddr_in *) addr;
-               if (sa->sin_addr.s_addr != INADDR_ANY)
-                       return 1;
-               break;
+               struct sockaddr_in *sa = (struct sockaddr_in *)addr;
+               return sa->sin_addr.s_addr != INADDR_ANY;
+       }
+       case AF_INET6: {
+               struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
+               return !ipv6_addr_any(sa);
        }
        }
 
@@ -599,6 +648,7 @@ static int nfs_parse_mount_options(char *raw,
                                   struct nfs_parsed_mount_data *mnt)
 {
        char *p, *string;
+       unsigned short port = 0;
 
        if (!raw) {
                dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
@@ -701,7 +751,7 @@ static int nfs_parse_mount_options(char *raw,
                                return 0;
                        if (option < 0 || option > 65535)
                                return 0;
-                       mnt->nfs_server.address.sin_port = htons(option);
+                       port = option;
                        break;
                case Opt_rsize:
                        if (match_int(args, &mnt->rsize))
@@ -763,13 +813,6 @@ static int nfs_parse_mount_options(char *raw,
                                return 0;
                        mnt->mount_server.port = option;
                        break;
-               case Opt_mountprog:
-                       if (match_int(args, &option))
-                               return 0;
-                       if (option < 0)
-                               return 0;
-                       mnt->mount_server.program = option;
-                       break;
                case Opt_mountvers:
                        if (match_int(args, &option))
                                return 0;
@@ -777,13 +820,6 @@ static int nfs_parse_mount_options(char *raw,
                                return 0;
                        mnt->mount_server.version = option;
                        break;
-               case Opt_nfsprog:
-                       if (match_int(args, &option))
-                               return 0;
-                       if (option < 0)
-                               return 0;
-                       mnt->nfs_server.program = option;
-                       break;
                case Opt_nfsvers:
                        if (match_int(args, &option))
                                return 0;
@@ -957,6 +993,8 @@ static int nfs_parse_mount_options(char *raw,
                }
        }
 
+       nfs_set_port((struct sockaddr *)&mnt->nfs_server.address, port);
+
        return 1;
 
 out_nomem:
@@ -1007,7 +1045,7 @@ static int nfs_try_mount(struct nfs_parsed_mount_data *args,
        /*
         * autobind will be used if mount_server.port == 0
         */
-       sin.sin_port = htons(args->mount_server.port);
+       nfs_set_port((struct sockaddr *)&sin, args->mount_server.port);
 
        /*
         * Now ask the mount server to map our export path
@@ -1043,9 +1081,6 @@ static int nfs_try_mount(struct nfs_parsed_mount_data *args,
  *
  * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
  *   mountproto=tcp after mountproto=udp, and so on
- *
- * XXX: as far as I can tell, changing the NFS program number is not
- *      supported in the NFS client.
  */
 static int nfs_validate_mount_data(void *options,
                                   struct nfs_parsed_mount_data *args,
@@ -1054,10 +1089,11 @@ static int nfs_validate_mount_data(void *options,
 {
        struct nfs_mount_data *data = (struct nfs_mount_data *)options;
 
+       memset(args, 0, sizeof(*args));
+
        if (data == NULL)
                goto out_no_data;
 
-       memset(args, 0, sizeof(*args));
        args->flags             = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
        args->rsize             = NFS_MAX_FILE_IO_SIZE;
        args->wsize             = NFS_MAX_FILE_IO_SIZE;
@@ -1068,9 +1104,7 @@ static int nfs_validate_mount_data(void *options,
        args->acdirmin          = 30;
        args->acdirmax          = 60;
        args->mount_server.protocol = XPRT_TRANSPORT_UDP;
-       args->mount_server.program = NFS_MNT_PROGRAM;
        args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
-       args->nfs_server.program = NFS_PROGRAM;
 
        switch (data->version) {
        case 1:
@@ -1321,15 +1355,50 @@ static int nfs_set_super(struct super_block *s, void *data)
        return ret;
 }
 
+static int nfs_compare_super_address(struct nfs_server *server1,
+                                    struct nfs_server *server2)
+{
+       struct sockaddr *sap1, *sap2;
+
+       sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
+       sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
+
+       if (sap1->sa_family != sap2->sa_family)
+               return 0;
+
+       switch (sap1->sa_family) {
+       case AF_INET: {
+               struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
+               struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
+               if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
+                       return 0;
+               if (sin1->sin_port != sin2->sin_port)
+                       return 0;
+               break;
+       }
+       case AF_INET6: {
+               struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
+               struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
+               if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
+                       return 0;
+               if (sin1->sin6_port != sin2->sin6_port)
+                       return 0;
+               break;
+       }
+       default:
+               return 0;
+       }
+
+       return 1;
+}
+
 static int nfs_compare_super(struct super_block *sb, void *data)
 {
        struct nfs_sb_mountdata *sb_mntdata = data;
        struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
        int mntflags = sb_mntdata->mntflags;
 
-       if (memcmp(&old->nfs_client->cl_addr,
-                               &server->nfs_client->cl_addr,
-                               sizeof(old->nfs_client->cl_addr)) != 0)
+       if (!nfs_compare_super_address(old, server))
                return 0;
        /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
        if (old->flags & NFS_MOUNT_UNSHARED)
@@ -1474,6 +1543,11 @@ static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
                error = PTR_ERR(mntroot);
                goto error_splat_super;
        }
+       if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
+               dput(mntroot);
+               error = -ESTALE;
+               goto error_splat_super;
+       }
 
        s->s_flags |= MS_ACTIVE;
        mnt->mnt_sb = s;
@@ -1521,6 +1595,28 @@ static void nfs4_fill_super(struct super_block *sb)
        nfs_initialise_sb(sb);
 }
 
+/*
+ * If the user didn't specify a port, set the port number to
+ * the NFS version 4 default port.
+ */
+static void nfs4_default_port(struct sockaddr *sap)
+{
+       switch (sap->sa_family) {
+       case AF_INET: {
+               struct sockaddr_in *ap = (struct sockaddr_in *)sap;
+               if (ap->sin_port == 0)
+                       ap->sin_port = htons(NFS_PORT);
+               break;
+       }
+       case AF_INET6: {
+               struct sockaddr_in6 *ap = (struct sockaddr_in6 *)sap;
+               if (ap->sin6_port == 0)
+                       ap->sin6_port = htons(NFS_PORT);
+               break;
+       }
+       }
+}
+
 /*
  * Validate NFSv4 mount options
  */
@@ -1531,10 +1627,11 @@ static int nfs4_validate_mount_data(void *options,
        struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
        char *c;
 
+       memset(args, 0, sizeof(*args));
+
        if (data == NULL)
                goto out_no_data;
 
-       memset(args, 0, sizeof(*args));
        args->rsize             = NFS_MAX_FILE_IO_SIZE;
        args->wsize             = NFS_MAX_FILE_IO_SIZE;
        args->timeo             = 600;
@@ -1553,12 +1650,13 @@ static int nfs4_validate_mount_data(void *options,
                                   data->host_addr,
                                   sizeof(args->nfs_server.address)))
                        return -EFAULT;
-               if (args->nfs_server.address.sin_port == 0)
-                       args->nfs_server.address.sin_port = htons(NFS_PORT);
                if (!nfs_verify_server_address((struct sockaddr *)
                                                &args->nfs_server.address))
                        goto out_no_address;
 
+               nfs4_default_port((struct sockaddr *)
+                                 &args->nfs_server.address);
+
                switch (data->auth_flavourlen) {
                case 0:
                        args->auth_flavors[0] = RPC_AUTH_UNIX;
@@ -1616,6 +1714,9 @@ static int nfs4_validate_mount_data(void *options,
                                                &args->nfs_server.address))
                        return -EINVAL;
 
+               nfs4_default_port((struct sockaddr *)
+                                 &args->nfs_server.address);
+
                switch (args->auth_flavor_len) {
                case 0:
                        args->auth_flavors[0] = RPC_AUTH_UNIX;
@@ -1636,21 +1737,16 @@ static int nfs4_validate_mount_data(void *options,
                len = c - dev_name;
                if (len > NFS4_MAXNAMLEN)
                        return -ENAMETOOLONG;
-               args->nfs_server.hostname = kzalloc(len, GFP_KERNEL);
-               if (args->nfs_server.hostname == NULL)
-                       return -ENOMEM;
-               strncpy(args->nfs_server.hostname, dev_name, len - 1);
+               /* N.B. caller will free nfs_server.hostname in all cases */
+               args->nfs_server.hostname = kstrndup(dev_name, len, GFP_KERNEL);
 
                c++;                    /* step over the ':' */
                len = strlen(c);
                if (len > NFS4_MAXPATHLEN)
                        return -ENAMETOOLONG;
-               args->nfs_server.export_path = kzalloc(len + 1, GFP_KERNEL);
-               if (args->nfs_server.export_path == NULL)
-                       return -ENOMEM;
-               strncpy(args->nfs_server.export_path, c, len);
+               args->nfs_server.export_path = kstrndup(c, len, GFP_KERNEL);
 
-               dprintk("MNTPATH: %s\n", args->nfs_server.export_path);
+               dprintk("NFS: MNTPATH: '%s'\n", args->nfs_server.export_path);
 
                if (args->client_address == NULL)
                        goto out_no_client_address;
@@ -1819,6 +1915,11 @@ static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
                error = PTR_ERR(mntroot);
                goto error_splat_super;
        }
+       if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
+               dput(mntroot);
+               error = -ESTALE;
+               goto error_splat_super;
+       }
 
        s->s_flags |= MS_ACTIVE;
        mnt->mnt_sb = s;
@@ -1893,6 +1994,11 @@ static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
                error = PTR_ERR(mntroot);
                goto error_splat_super;
        }
+       if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
+               dput(mntroot);
+               error = -ESTALE;
+               goto error_splat_super;
+       }
 
        s->s_flags |= MS_ACTIVE;
        mnt->mnt_sb = s;