Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs...
[pandora-kernel.git] / fs / nfs / nfs4filelayoutdev.c
index 3b7bf13..ed388aa 100644 (file)
@@ -56,54 +56,139 @@ print_ds(struct nfs4_pnfs_ds *ds)
                printk("%s NULL device\n", __func__);
                return;
        }
-       printk("        ip_addr %x port %hu\n"
+       printk("        ds %s\n"
                "        ref count %d\n"
                "        client %p\n"
                "        cl_exchange_flags %x\n",
-               ntohl(ds->ds_ip_addr), ntohs(ds->ds_port),
+               ds->ds_remotestr,
                atomic_read(&ds->ds_count), ds->ds_clp,
                ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
 }
 
-/* nfs4_ds_cache_lock is held */
-static struct nfs4_pnfs_ds *
-_data_server_lookup_locked(u32 ip_addr, u32 port)
+static bool
+same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
 {
-       struct nfs4_pnfs_ds *ds;
+       struct sockaddr_in *a, *b;
+       struct sockaddr_in6 *a6, *b6;
+
+       if (addr1->sa_family != addr2->sa_family)
+               return false;
+
+       switch (addr1->sa_family) {
+       case AF_INET:
+               a = (struct sockaddr_in *)addr1;
+               b = (struct sockaddr_in *)addr2;
+
+               if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
+                   a->sin_port == b->sin_port)
+                       return true;
+               break;
+
+       case AF_INET6:
+               a6 = (struct sockaddr_in6 *)addr1;
+               b6 = (struct sockaddr_in6 *)addr2;
+
+               /* LINKLOCAL addresses must have matching scope_id */
+               if (ipv6_addr_scope(&a6->sin6_addr) ==
+                   IPV6_ADDR_SCOPE_LINKLOCAL &&
+                   a6->sin6_scope_id != b6->sin6_scope_id)
+                       return false;
+
+               if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
+                   a6->sin6_port == b6->sin6_port)
+                       return true;
+               break;
+
+       default:
+               dprintk("%s: unhandled address family: %u\n",
+                       __func__, addr1->sa_family);
+               return false;
+       }
 
-       dprintk("_data_server_lookup: ip_addr=%x port=%hu\n",
-                       ntohl(ip_addr), ntohs(port));
+       return false;
+}
 
-       list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) {
-               if (ds->ds_ip_addr == ip_addr &&
-                   ds->ds_port == port) {
-                       return ds;
+/*
+ * Lookup DS by addresses.  The first matching address returns true.
+ * nfs4_ds_cache_lock is held
+ */
+static struct nfs4_pnfs_ds *
+_data_server_lookup_locked(struct list_head *dsaddrs)
+{
+       struct nfs4_pnfs_ds *ds;
+       struct nfs4_pnfs_ds_addr *da1, *da2;
+
+       list_for_each_entry(da1, dsaddrs, da_node) {
+               list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) {
+                       list_for_each_entry(da2, &ds->ds_addrs, da_node) {
+                               if (same_sockaddr(
+                                       (struct sockaddr *)&da1->da_addr,
+                                       (struct sockaddr *)&da2->da_addr))
+                                       return ds;
+                       }
                }
        }
        return NULL;
 }
 
+/*
+ * Compare two lists of addresses.
+ */
+static bool
+_data_server_match_all_addrs_locked(struct list_head *dsaddrs1,
+                                   struct list_head *dsaddrs2)
+{
+       struct nfs4_pnfs_ds_addr *da1, *da2;
+       size_t count1 = 0,
+              count2 = 0;
+
+       list_for_each_entry(da1, dsaddrs1, da_node)
+               count1++;
+
+       list_for_each_entry(da2, dsaddrs2, da_node) {
+               bool found = false;
+               count2++;
+               list_for_each_entry(da1, dsaddrs1, da_node) {
+                       if (same_sockaddr((struct sockaddr *)&da1->da_addr,
+                               (struct sockaddr *)&da2->da_addr)) {
+                               found = true;
+                               break;
+                       }
+               }
+               if (!found)
+                       return false;
+       }
+
+       return (count1 == count2);
+}
+
 /*
  * Create an rpc connection to the nfs4_pnfs_ds data server
- * Currently only support IPv4
+ * Currently only supports IPv4 and IPv6 addresses
  */
 static int
 nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds)
 {
-       struct nfs_client *clp;
-       struct sockaddr_in sin;
+       struct nfs_client *clp = ERR_PTR(-EIO);
+       struct nfs4_pnfs_ds_addr *da;
        int status = 0;
 
-       dprintk("--> %s ip:port %x:%hu au_flavor %d\n", __func__,
-               ntohl(ds->ds_ip_addr), ntohs(ds->ds_port),
+       dprintk("--> %s DS %s au_flavor %d\n", __func__, ds->ds_remotestr,
                mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor);
 
-       sin.sin_family = AF_INET;
-       sin.sin_addr.s_addr = ds->ds_ip_addr;
-       sin.sin_port = ds->ds_port;
+       BUG_ON(list_empty(&ds->ds_addrs));
+
+       list_for_each_entry(da, &ds->ds_addrs, da_node) {
+               dprintk("%s: DS %s: trying address %s\n",
+                       __func__, ds->ds_remotestr, da->da_remotestr);
+
+               clp = nfs4_set_ds_client(mds_srv->nfs_client,
+                                (struct sockaddr *)&da->da_addr,
+                                da->da_addrlen, IPPROTO_TCP);
+               if (!IS_ERR(clp))
+                       break;
+       }
 
-       clp = nfs4_set_ds_client(mds_srv->nfs_client, (struct sockaddr *)&sin,
-                                sizeof(sin), IPPROTO_TCP);
        if (IS_ERR(clp)) {
                status = PTR_ERR(clp);
                goto out;
@@ -115,8 +200,8 @@ nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds)
                        goto out_put;
                }
                ds->ds_clp = clp;
-               dprintk("%s [existing] ip=%x, port=%hu\n", __func__,
-                       ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
+               dprintk("%s [existing] server=%s\n", __func__,
+                       ds->ds_remotestr);
                goto out;
        }
 
@@ -135,8 +220,7 @@ nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds)
                goto out_put;
 
        ds->ds_clp = clp;
-       dprintk("%s [new] ip=%x, port=%hu\n", __func__, ntohl(ds->ds_ip_addr),
-               ntohs(ds->ds_port));
+       dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
 out:
        return status;
 out_put:
@@ -147,12 +231,25 @@ out_put:
 static void
 destroy_ds(struct nfs4_pnfs_ds *ds)
 {
+       struct nfs4_pnfs_ds_addr *da;
+
        dprintk("--> %s\n", __func__);
        ifdebug(FACILITY)
                print_ds(ds);
 
        if (ds->ds_clp)
                nfs_put_client(ds->ds_clp);
+
+       while (!list_empty(&ds->ds_addrs)) {
+               da = list_first_entry(&ds->ds_addrs,
+                                     struct nfs4_pnfs_ds_addr,
+                                     da_node);
+               list_del_init(&da->da_node);
+               kfree(da->da_remotestr);
+               kfree(da);
+       }
+
+       kfree(ds->ds_remotestr);
        kfree(ds);
 }
 
@@ -179,31 +276,96 @@ nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
        kfree(dsaddr);
 }
 
+/*
+ * Create a string with a human readable address and port to avoid
+ * complicated setup around many dprinks.
+ */
+static char *
+nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
+{
+       struct nfs4_pnfs_ds_addr *da;
+       char *remotestr;
+       size_t len;
+       char *p;
+
+       len = 3;        /* '{', '}' and eol */
+       list_for_each_entry(da, dsaddrs, da_node) {
+               len += strlen(da->da_remotestr) + 1;    /* string plus comma */
+       }
+
+       remotestr = kzalloc(len, gfp_flags);
+       if (!remotestr)
+               return NULL;
+
+       p = remotestr;
+       *(p++) = '{';
+       len--;
+       list_for_each_entry(da, dsaddrs, da_node) {
+               size_t ll = strlen(da->da_remotestr);
+
+               if (ll > len)
+                       goto out_err;
+
+               memcpy(p, da->da_remotestr, ll);
+               p += ll;
+               len -= ll;
+
+               if (len < 1)
+                       goto out_err;
+               (*p++) = ',';
+               len--;
+       }
+       if (len < 2)
+               goto out_err;
+       *(p++) = '}';
+       *p = '\0';
+       return remotestr;
+out_err:
+       kfree(remotestr);
+       return NULL;
+}
+
 static struct nfs4_pnfs_ds *
-nfs4_pnfs_ds_add(struct inode *inode, u32 ip_addr, u32 port, gfp_t gfp_flags)
+nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
 {
-       struct nfs4_pnfs_ds *tmp_ds, *ds;
+       struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
+       char *remotestr;
 
-       ds = kzalloc(sizeof(*tmp_ds), gfp_flags);
+       if (list_empty(dsaddrs)) {
+               dprintk("%s: no addresses defined\n", __func__);
+               goto out;
+       }
+
+       ds = kzalloc(sizeof(*ds), gfp_flags);
        if (!ds)
                goto out;
 
+       /* this is only used for debugging, so it's ok if its NULL */
+       remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
+
        spin_lock(&nfs4_ds_cache_lock);
-       tmp_ds = _data_server_lookup_locked(ip_addr, port);
+       tmp_ds = _data_server_lookup_locked(dsaddrs);
        if (tmp_ds == NULL) {
-               ds->ds_ip_addr = ip_addr;
-               ds->ds_port = port;
+               INIT_LIST_HEAD(&ds->ds_addrs);
+               list_splice_init(dsaddrs, &ds->ds_addrs);
+               ds->ds_remotestr = remotestr;
                atomic_set(&ds->ds_count, 1);
                INIT_LIST_HEAD(&ds->ds_node);
                ds->ds_clp = NULL;
                list_add(&ds->ds_node, &nfs4_data_server_cache);
-               dprintk("%s add new data server ip 0x%x\n", __func__,
-                       ds->ds_ip_addr);
+               dprintk("%s add new data server %s\n", __func__,
+                       ds->ds_remotestr);
        } else {
+               if (!_data_server_match_all_addrs_locked(&tmp_ds->ds_addrs,
+                                                        dsaddrs)) {
+                       dprintk("%s:  multipath address mismatch: %s != %s",
+                               __func__, tmp_ds->ds_remotestr, remotestr);
+               }
+               kfree(remotestr);
                kfree(ds);
                atomic_inc(&tmp_ds->ds_count);
-               dprintk("%s data server found ip 0x%x, inc'ed ds_count to %d\n",
-                       __func__, tmp_ds->ds_ip_addr,
+               dprintk("%s data server %s found, inc'ed ds_count to %d\n",
+                       __func__, tmp_ds->ds_remotestr,
                        atomic_read(&tmp_ds->ds_count));
                ds = tmp_ds;
        }
@@ -213,18 +375,22 @@ out:
 }
 
 /*
- * Currently only support ipv4, and one multi-path address.
+ * Currently only supports ipv4, ipv6 and one multi-path address.
  */
-static struct nfs4_pnfs_ds *
-decode_and_add_ds(struct xdr_stream *streamp, struct inode *inode, gfp_t gfp_flags)
+static struct nfs4_pnfs_ds_addr *
+decode_ds_addr(struct xdr_stream *streamp, gfp_t gfp_flags)
 {
-       struct nfs4_pnfs_ds *ds = NULL;
-       char *buf;
-       const char *ipend, *pstr;
-       u32 ip_addr, port;
-       int nlen, rlen, i;
+       struct nfs4_pnfs_ds_addr *da = NULL;
+       char *buf, *portstr;
+       u32 port;
+       int nlen, rlen;
        int tmp[2];
        __be32 *p;
+       char *netid, *match_netid;
+       size_t len, match_netid_len;
+       char *startsep = "";
+       char *endsep = "";
+
 
        /* r_netid */
        p = xdr_inline_decode(streamp, 4);
@@ -236,64 +402,123 @@ decode_and_add_ds(struct xdr_stream *streamp, struct inode *inode, gfp_t gfp_fla
        if (unlikely(!p))
                goto out_err;
 
-       /* Check that netid is "tcp" */
-       if (nlen != 3 ||  memcmp((char *)p, "tcp", 3)) {
-               dprintk("%s: ERROR: non ipv4 TCP r_netid\n", __func__);
+       netid = kmalloc(nlen+1, gfp_flags);
+       if (unlikely(!netid))
                goto out_err;
-       }
 
-       /* r_addr */
+       netid[nlen] = '\0';
+       memcpy(netid, p, nlen);
+
+       /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
        p = xdr_inline_decode(streamp, 4);
        if (unlikely(!p))
-               goto out_err;
+               goto out_free_netid;
        rlen = be32_to_cpup(p);
 
        p = xdr_inline_decode(streamp, rlen);
        if (unlikely(!p))
-               goto out_err;
+               goto out_free_netid;
 
-       /* ipv6 length plus port is legal */
-       if (rlen > INET6_ADDRSTRLEN + 8) {
+       /* port is ".ABC.DEF", 8 chars max */
+       if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
                dprintk("%s: Invalid address, length %d\n", __func__,
                        rlen);
-               goto out_err;
+               goto out_free_netid;
        }
        buf = kmalloc(rlen + 1, gfp_flags);
        if (!buf) {
                dprintk("%s: Not enough memory\n", __func__);
-               goto out_err;
+               goto out_free_netid;
        }
        buf[rlen] = '\0';
        memcpy(buf, p, rlen);
 
-       /* replace the port dots with dashes for the in4_pton() delimiter*/
-       for (i = 0; i < 2; i++) {
-               char *res = strrchr(buf, '.');
-               if (!res) {
-                       dprintk("%s: Failed finding expected dots in port\n",
-                               __func__);
-                       goto out_free;
-               }
-               *res = '-';
+       /* replace port '.' with '-' */
+       portstr = strrchr(buf, '.');
+       if (!portstr) {
+               dprintk("%s: Failed finding expected dot in port\n",
+                       __func__);
+               goto out_free_buf;
+       }
+       *portstr = '-';
+
+       /* find '.' between address and port */
+       portstr = strrchr(buf, '.');
+       if (!portstr) {
+               dprintk("%s: Failed finding expected dot between address and "
+                       "port\n", __func__);
+               goto out_free_buf;
        }
+       *portstr = '\0';
 
-       /* Currently only support ipv4 address */
-       if (in4_pton(buf, rlen, (u8 *)&ip_addr, '-', &ipend) == 0) {
-               dprintk("%s: Only ipv4 addresses supported\n", __func__);
-               goto out_free;
+       da = kzalloc(sizeof(*da), gfp_flags);
+       if (unlikely(!da))
+               goto out_free_buf;
+
+       INIT_LIST_HEAD(&da->da_node);
+
+       if (!rpc_pton(buf, portstr-buf, (struct sockaddr *)&da->da_addr,
+                     sizeof(da->da_addr))) {
+               dprintk("%s: error parsing address %s\n", __func__, buf);
+               goto out_free_da;
        }
 
-       /* port */
-       pstr = ipend;
-       sscanf(pstr, "-%d-%d", &tmp[0], &tmp[1]);
+       portstr++;
+       sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
        port = htons((tmp[0] << 8) | (tmp[1]));
 
-       ds = nfs4_pnfs_ds_add(inode, ip_addr, port, gfp_flags);
-       dprintk("%s: Decoded address and port %s\n", __func__, buf);
-out_free:
+       switch (da->da_addr.ss_family) {
+       case AF_INET:
+               ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
+               da->da_addrlen = sizeof(struct sockaddr_in);
+               match_netid = "tcp";
+               match_netid_len = 3;
+               break;
+
+       case AF_INET6:
+               ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
+               da->da_addrlen = sizeof(struct sockaddr_in6);
+               match_netid = "tcp6";
+               match_netid_len = 4;
+               startsep = "[";
+               endsep = "]";
+               break;
+
+       default:
+               dprintk("%s: unsupported address family: %u\n",
+                       __func__, da->da_addr.ss_family);
+               goto out_free_da;
+       }
+
+       if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
+               dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
+                       __func__, netid, match_netid);
+               goto out_free_da;
+       }
+
+       /* save human readable address */
+       len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
+       da->da_remotestr = kzalloc(len, gfp_flags);
+
+       /* NULL is ok, only used for dprintk */
+       if (da->da_remotestr)
+               snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
+                        buf, endsep, ntohs(port));
+
+       dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
        kfree(buf);
+       kfree(netid);
+       return da;
+
+out_free_da:
+       kfree(da);
+out_free_buf:
+       dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
+       kfree(buf);
+out_free_netid:
+       kfree(netid);
 out_err:
-       return ds;
+       return NULL;
 }
 
 /* Decode opaque device data and return the result */
@@ -310,6 +535,8 @@ decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags)
        struct xdr_stream stream;
        struct xdr_buf buf;
        struct page *scratch;
+       struct list_head dsaddrs;
+       struct nfs4_pnfs_ds_addr *da;
 
        /* set up xdr stream */
        scratch = alloc_page(gfp_flags);
@@ -386,6 +613,8 @@ decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags)
                                NFS_SERVER(ino)->nfs_client,
                                &pdev->dev_id);
 
+       INIT_LIST_HEAD(&dsaddrs);
+
        for (i = 0; i < dsaddr->ds_num; i++) {
                int j;
                u32 mp_count;
@@ -395,48 +624,43 @@ decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags)
                        goto out_err_free_deviceid;
 
                mp_count = be32_to_cpup(p); /* multipath count */
-               if (mp_count > 1) {
-                       printk(KERN_WARNING
-                              "%s: Multipath count %d not supported, "
-                              "skipping all greater than 1\n", __func__,
-                               mp_count);
-               }
                for (j = 0; j < mp_count; j++) {
-                       if (j == 0) {
-                               dsaddr->ds_list[i] = decode_and_add_ds(&stream,
-                                       ino, gfp_flags);
-                               if (dsaddr->ds_list[i] == NULL)
-                                       goto out_err_free_deviceid;
-                       } else {
-                               u32 len;
-                               /* skip extra multipath */
-
-                               /* read len, skip */
-                               p = xdr_inline_decode(&stream, 4);
-                               if (unlikely(!p))
-                                       goto out_err_free_deviceid;
-                               len = be32_to_cpup(p);
-
-                               p = xdr_inline_decode(&stream, len);
-                               if (unlikely(!p))
-                                       goto out_err_free_deviceid;
-
-                               /* read len, skip */
-                               p = xdr_inline_decode(&stream, 4);
-                               if (unlikely(!p))
-                                       goto out_err_free_deviceid;
-                               len = be32_to_cpup(p);
-
-                               p = xdr_inline_decode(&stream, len);
-                               if (unlikely(!p))
-                                       goto out_err_free_deviceid;
-                       }
+                       da = decode_ds_addr(&stream, gfp_flags);
+                       if (da)
+                               list_add_tail(&da->da_node, &dsaddrs);
+               }
+               if (list_empty(&dsaddrs)) {
+                       dprintk("%s: no suitable DS addresses found\n",
+                               __func__);
+                       goto out_err_free_deviceid;
+               }
+
+               dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags);
+               if (!dsaddr->ds_list[i])
+                       goto out_err_drain_dsaddrs;
+
+               /* If DS was already in cache, free ds addrs */
+               while (!list_empty(&dsaddrs)) {
+                       da = list_first_entry(&dsaddrs,
+                                             struct nfs4_pnfs_ds_addr,
+                                             da_node);
+                       list_del_init(&da->da_node);
+                       kfree(da->da_remotestr);
+                       kfree(da);
                }
        }
 
        __free_page(scratch);
        return dsaddr;
 
+out_err_drain_dsaddrs:
+       while (!list_empty(&dsaddrs)) {
+               da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr,
+                                     da_node);
+               list_del_init(&da->da_node);
+               kfree(da->da_remotestr);
+               kfree(da);
+       }
 out_err_free_deviceid:
        nfs4_fl_free_deviceid(dsaddr);
        /* stripe_indicies was part of dsaddr */
@@ -591,13 +815,13 @@ nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
 
 static void
 filelayout_mark_devid_negative(struct nfs4_file_layout_dsaddr *dsaddr,
-                              int err, u32 ds_addr)
+                              int err, const char *ds_remotestr)
 {
        u32 *p = (u32 *)&dsaddr->id_node.deviceid;
 
-       printk(KERN_ERR "NFS: data server %x connection error %d."
+       printk(KERN_ERR "NFS: data server %s connection error %d."
                " Deviceid [%x%x%x%x] marked out of use.\n",
-               ds_addr, err, p[0], p[1], p[2], p[3]);
+               ds_remotestr, err, p[0], p[1], p[2], p[3]);
 
        spin_lock(&nfs4_ds_cache_lock);
        dsaddr->flags |= NFS4_DEVICE_ID_NEG_ENTRY;
@@ -628,7 +852,7 @@ nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
                err = nfs4_ds_connect(s, ds);
                if (err) {
                        filelayout_mark_devid_negative(dsaddr, err,
-                                                      ntohl(ds->ds_ip_addr));
+                                                      ds->ds_remotestr);
                        return NULL;
                }
        }