Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Device operations for the pnfs nfs4 file layout driver. |
| 3 | * |
| 4 | * Copyright (c) 2002 |
| 5 | * The Regents of the University of Michigan |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * Dean Hildebrand <dhildebz@umich.edu> |
| 9 | * Garth Goodson <Garth.Goodson@netapp.com> |
| 10 | * |
| 11 | * Permission is granted to use, copy, create derivative works, and |
| 12 | * redistribute this software and such derivative works for any purpose, |
| 13 | * so long as the name of the University of Michigan is not used in |
| 14 | * any advertising or publicity pertaining to the use or distribution |
| 15 | * of this software without specific, written prior authorization. If |
| 16 | * the above copyright notice or any other identification of the |
| 17 | * University of Michigan is included in any copy of any portion of |
| 18 | * this software, then the disclaimer below must also be included. |
| 19 | * |
| 20 | * This software is provided as is, without representation or warranty |
| 21 | * of any kind either express or implied, including without limitation |
| 22 | * the implied warranties of merchantability, fitness for a particular |
| 23 | * purpose, or noninfringement. The Regents of the University of |
| 24 | * Michigan shall not be liable for any damages, including special, |
| 25 | * indirect, incidental, or consequential damages, with respect to any |
| 26 | * claim arising out of or in connection with the use of the software, |
| 27 | * even if it has been or is hereafter advised of the possibility of |
| 28 | * such damages. |
| 29 | */ |
| 30 | |
| 31 | #include <linux/nfs_fs.h> |
| 32 | #include <linux/vmalloc.h> |
| 33 | |
| 34 | #include "internal.h" |
| 35 | #include "nfs4filelayout.h" |
| 36 | |
| 37 | #define NFSDBG_FACILITY NFSDBG_PNFS_LD |
| 38 | |
| 39 | /* |
| 40 | * Data server cache |
| 41 | * |
| 42 | * Data servers can be mapped to different device ids. |
| 43 | * nfs4_pnfs_ds reference counting |
| 44 | * - set to 1 on allocation |
| 45 | * - incremented when a device id maps a data server already in the cache. |
| 46 | * - decremented when deviceid is removed from the cache. |
| 47 | */ |
Trond Myklebust | 1728017 | 2012-03-11 13:11:00 -0400 | [diff] [blame] | 48 | static DEFINE_SPINLOCK(nfs4_ds_cache_lock); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 49 | static LIST_HEAD(nfs4_data_server_cache); |
| 50 | |
| 51 | /* Debug routines */ |
| 52 | void |
| 53 | print_ds(struct nfs4_pnfs_ds *ds) |
| 54 | { |
| 55 | if (ds == NULL) { |
| 56 | printk("%s NULL device\n", __func__); |
| 57 | return; |
| 58 | } |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 59 | printk(" ds %s\n" |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 60 | " ref count %d\n" |
| 61 | " client %p\n" |
| 62 | " cl_exchange_flags %x\n", |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 63 | ds->ds_remotestr, |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 64 | atomic_read(&ds->ds_count), ds->ds_clp, |
| 65 | ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0); |
| 66 | } |
| 67 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 68 | static bool |
| 69 | same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 70 | { |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 71 | struct sockaddr_in *a, *b; |
| 72 | struct sockaddr_in6 *a6, *b6; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 73 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 74 | if (addr1->sa_family != addr2->sa_family) |
| 75 | return false; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 76 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 77 | switch (addr1->sa_family) { |
| 78 | case AF_INET: |
| 79 | a = (struct sockaddr_in *)addr1; |
| 80 | b = (struct sockaddr_in *)addr2; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 81 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 82 | if (a->sin_addr.s_addr == b->sin_addr.s_addr && |
| 83 | a->sin_port == b->sin_port) |
| 84 | return true; |
| 85 | break; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 86 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 87 | case AF_INET6: |
| 88 | a6 = (struct sockaddr_in6 *)addr1; |
| 89 | b6 = (struct sockaddr_in6 *)addr2; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 90 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 91 | /* LINKLOCAL addresses must have matching scope_id */ |
| 92 | if (ipv6_addr_scope(&a6->sin6_addr) == |
| 93 | IPV6_ADDR_SCOPE_LINKLOCAL && |
| 94 | a6->sin6_scope_id != b6->sin6_scope_id) |
| 95 | return false; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 96 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 97 | if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) && |
| 98 | a6->sin6_port == b6->sin6_port) |
| 99 | return true; |
| 100 | break; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 101 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 102 | default: |
| 103 | dprintk("%s: unhandled address family: %u\n", |
| 104 | __func__, addr1->sa_family); |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
Trond Myklebust | 1728017 | 2012-03-11 13:11:00 -0400 | [diff] [blame] | 111 | static bool |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 112 | _same_data_server_addrs_locked(const struct list_head *dsaddrs1, |
| 113 | const struct list_head *dsaddrs2) |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 114 | { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 115 | struct nfs4_pnfs_ds_addr *da1, *da2; |
| 116 | |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 117 | /* step through both lists, comparing as we go */ |
| 118 | for (da1 = list_first_entry(dsaddrs1, typeof(*da1), da_node), |
| 119 | da2 = list_first_entry(dsaddrs2, typeof(*da2), da_node); |
| 120 | da1 != NULL && da2 != NULL; |
| 121 | da1 = list_entry(da1->da_node.next, typeof(*da1), da_node), |
| 122 | da2 = list_entry(da2->da_node.next, typeof(*da2), da_node)) { |
| 123 | if (!same_sockaddr((struct sockaddr *)&da1->da_addr, |
| 124 | (struct sockaddr *)&da2->da_addr)) |
| 125 | return false; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 126 | } |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 127 | if (da1 == NULL && da2 == NULL) |
| 128 | return true; |
| 129 | |
| 130 | return false; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 131 | } |
| 132 | |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 133 | /* |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 134 | * Lookup DS by addresses. nfs4_ds_cache_lock is held |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 135 | */ |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 136 | static struct nfs4_pnfs_ds * |
| 137 | _data_server_lookup_locked(const struct list_head *dsaddrs) |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 138 | { |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 139 | struct nfs4_pnfs_ds *ds; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 140 | |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 141 | list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) |
| 142 | if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs)) |
| 143 | return ds; |
| 144 | return NULL; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 148 | * Create an rpc connection to the nfs4_pnfs_ds data server |
Weston Andros Adamson | 35dbbc9 | 2011-06-01 16:32:21 -0400 | [diff] [blame] | 149 | * Currently only supports IPv4 and IPv6 addresses |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 150 | */ |
| 151 | static int |
| 152 | nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds) |
| 153 | { |
Weston Andros Adamson | 7e574f0 | 2011-05-31 18:48:58 -0400 | [diff] [blame] | 154 | struct nfs_client *clp = ERR_PTR(-EIO); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 155 | struct nfs4_pnfs_ds_addr *da; |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 156 | int status = 0; |
| 157 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 158 | dprintk("--> %s DS %s au_flavor %d\n", __func__, ds->ds_remotestr, |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 159 | mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor); |
| 160 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 161 | BUG_ON(list_empty(&ds->ds_addrs)); |
| 162 | |
Weston Andros Adamson | 7e574f0 | 2011-05-31 18:48:58 -0400 | [diff] [blame] | 163 | list_for_each_entry(da, &ds->ds_addrs, da_node) { |
| 164 | dprintk("%s: DS %s: trying address %s\n", |
| 165 | __func__, ds->ds_remotestr, da->da_remotestr); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 166 | |
Weston Andros Adamson | 7e574f0 | 2011-05-31 18:48:58 -0400 | [diff] [blame] | 167 | clp = nfs4_set_ds_client(mds_srv->nfs_client, |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 168 | (struct sockaddr *)&da->da_addr, |
| 169 | da->da_addrlen, IPPROTO_TCP); |
Weston Andros Adamson | 7e574f0 | 2011-05-31 18:48:58 -0400 | [diff] [blame] | 170 | if (!IS_ERR(clp)) |
| 171 | break; |
| 172 | } |
| 173 | |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 174 | if (IS_ERR(clp)) { |
| 175 | status = PTR_ERR(clp); |
| 176 | goto out; |
| 177 | } |
| 178 | |
| 179 | if ((clp->cl_exchange_flags & EXCHGID4_FLAG_MASK_PNFS) != 0) { |
| 180 | if (!is_ds_client(clp)) { |
| 181 | status = -ENODEV; |
| 182 | goto out_put; |
| 183 | } |
| 184 | ds->ds_clp = clp; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 185 | dprintk("%s [existing] server=%s\n", __func__, |
| 186 | ds->ds_remotestr); |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 187 | goto out; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Do not set NFS_CS_CHECK_LEASE_TIME instead set the DS lease to |
| 192 | * be equal to the MDS lease. Renewal is scheduled in create_session. |
| 193 | */ |
| 194 | spin_lock(&mds_srv->nfs_client->cl_lock); |
| 195 | clp->cl_lease_time = mds_srv->nfs_client->cl_lease_time; |
| 196 | spin_unlock(&mds_srv->nfs_client->cl_lock); |
| 197 | clp->cl_last_renewal = jiffies; |
| 198 | |
| 199 | /* New nfs_client */ |
| 200 | status = nfs4_init_ds_session(clp); |
| 201 | if (status) |
| 202 | goto out_put; |
| 203 | |
| 204 | ds->ds_clp = clp; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 205 | dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr); |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 206 | out: |
| 207 | return status; |
| 208 | out_put: |
| 209 | nfs_put_client(clp); |
| 210 | goto out; |
| 211 | } |
| 212 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 213 | static void |
| 214 | destroy_ds(struct nfs4_pnfs_ds *ds) |
| 215 | { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 216 | struct nfs4_pnfs_ds_addr *da; |
| 217 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 218 | dprintk("--> %s\n", __func__); |
| 219 | ifdebug(FACILITY) |
| 220 | print_ds(ds); |
| 221 | |
| 222 | if (ds->ds_clp) |
| 223 | nfs_put_client(ds->ds_clp); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 224 | |
| 225 | while (!list_empty(&ds->ds_addrs)) { |
| 226 | da = list_first_entry(&ds->ds_addrs, |
| 227 | struct nfs4_pnfs_ds_addr, |
| 228 | da_node); |
| 229 | list_del_init(&da->da_node); |
| 230 | kfree(da->da_remotestr); |
| 231 | kfree(da); |
| 232 | } |
| 233 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 234 | kfree(ds->ds_remotestr); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 235 | kfree(ds); |
| 236 | } |
| 237 | |
Benny Halevy | 1775bc3 | 2011-05-20 13:47:33 +0200 | [diff] [blame] | 238 | void |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 239 | nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr) |
| 240 | { |
| 241 | struct nfs4_pnfs_ds *ds; |
| 242 | int i; |
| 243 | |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 244 | nfs4_print_deviceid(&dsaddr->id_node.deviceid); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 245 | |
| 246 | for (i = 0; i < dsaddr->ds_num; i++) { |
| 247 | ds = dsaddr->ds_list[i]; |
| 248 | if (ds != NULL) { |
| 249 | if (atomic_dec_and_lock(&ds->ds_count, |
| 250 | &nfs4_ds_cache_lock)) { |
| 251 | list_del_init(&ds->ds_node); |
| 252 | spin_unlock(&nfs4_ds_cache_lock); |
| 253 | destroy_ds(ds); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | kfree(dsaddr->stripe_indices); |
| 258 | kfree(dsaddr); |
| 259 | } |
| 260 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 261 | /* |
| 262 | * Create a string with a human readable address and port to avoid |
| 263 | * complicated setup around many dprinks. |
| 264 | */ |
| 265 | static char * |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 266 | nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 267 | { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 268 | struct nfs4_pnfs_ds_addr *da; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 269 | char *remotestr; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 270 | size_t len; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 271 | char *p; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 272 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 273 | len = 3; /* '{', '}' and eol */ |
| 274 | list_for_each_entry(da, dsaddrs, da_node) { |
| 275 | len += strlen(da->da_remotestr) + 1; /* string plus comma */ |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 276 | } |
| 277 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 278 | remotestr = kzalloc(len, gfp_flags); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 279 | if (!remotestr) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 280 | return NULL; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 281 | |
| 282 | p = remotestr; |
| 283 | *(p++) = '{'; |
| 284 | len--; |
| 285 | list_for_each_entry(da, dsaddrs, da_node) { |
| 286 | size_t ll = strlen(da->da_remotestr); |
| 287 | |
| 288 | if (ll > len) |
| 289 | goto out_err; |
| 290 | |
| 291 | memcpy(p, da->da_remotestr, ll); |
| 292 | p += ll; |
| 293 | len -= ll; |
| 294 | |
| 295 | if (len < 1) |
| 296 | goto out_err; |
| 297 | (*p++) = ','; |
| 298 | len--; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 299 | } |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 300 | if (len < 2) |
| 301 | goto out_err; |
| 302 | *(p++) = '}'; |
| 303 | *p = '\0'; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 304 | return remotestr; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 305 | out_err: |
| 306 | kfree(remotestr); |
| 307 | return NULL; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static struct nfs4_pnfs_ds * |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 311 | nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 312 | { |
| 313 | struct nfs4_pnfs_ds *tmp_ds, *ds = NULL; |
| 314 | char *remotestr; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 315 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 316 | if (list_empty(dsaddrs)) { |
| 317 | dprintk("%s: no addresses defined\n", __func__); |
| 318 | goto out; |
| 319 | } |
| 320 | |
| 321 | ds = kzalloc(sizeof(*ds), gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 322 | if (!ds) |
| 323 | goto out; |
| 324 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 325 | /* this is only used for debugging, so it's ok if its NULL */ |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 326 | remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 327 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 328 | spin_lock(&nfs4_ds_cache_lock); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 329 | tmp_ds = _data_server_lookup_locked(dsaddrs); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 330 | if (tmp_ds == NULL) { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 331 | INIT_LIST_HEAD(&ds->ds_addrs); |
| 332 | list_splice_init(dsaddrs, &ds->ds_addrs); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 333 | ds->ds_remotestr = remotestr; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 334 | atomic_set(&ds->ds_count, 1); |
| 335 | INIT_LIST_HEAD(&ds->ds_node); |
| 336 | ds->ds_clp = NULL; |
| 337 | list_add(&ds->ds_node, &nfs4_data_server_cache); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 338 | dprintk("%s add new data server %s\n", __func__, |
| 339 | ds->ds_remotestr); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 340 | } else { |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 341 | kfree(remotestr); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 342 | kfree(ds); |
| 343 | atomic_inc(&tmp_ds->ds_count); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 344 | dprintk("%s data server %s found, inc'ed ds_count to %d\n", |
| 345 | __func__, tmp_ds->ds_remotestr, |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 346 | atomic_read(&tmp_ds->ds_count)); |
| 347 | ds = tmp_ds; |
| 348 | } |
| 349 | spin_unlock(&nfs4_ds_cache_lock); |
| 350 | out: |
| 351 | return ds; |
| 352 | } |
| 353 | |
| 354 | /* |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 355 | * Currently only supports ipv4, ipv6 and one multi-path address. |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 356 | */ |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 357 | static struct nfs4_pnfs_ds_addr * |
Stanislav Kinsbursky | 1709427 | 2012-01-19 19:05:57 +0400 | [diff] [blame] | 358 | decode_ds_addr(struct net *net, struct xdr_stream *streamp, gfp_t gfp_flags) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 359 | { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 360 | struct nfs4_pnfs_ds_addr *da = NULL; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 361 | char *buf, *portstr; |
Dan Carpenter | 13fff2f | 2012-01-12 10:07:35 +0300 | [diff] [blame] | 362 | __be16 port; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 363 | int nlen, rlen; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 364 | int tmp[2]; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 365 | __be32 *p; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 366 | char *netid, *match_netid; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 367 | size_t len, match_netid_len; |
| 368 | char *startsep = ""; |
| 369 | char *endsep = ""; |
| 370 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 371 | |
| 372 | /* r_netid */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 373 | p = xdr_inline_decode(streamp, 4); |
| 374 | if (unlikely(!p)) |
| 375 | goto out_err; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 376 | nlen = be32_to_cpup(p++); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 377 | |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 378 | p = xdr_inline_decode(streamp, nlen); |
| 379 | if (unlikely(!p)) |
| 380 | goto out_err; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 381 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 382 | netid = kmalloc(nlen+1, gfp_flags); |
| 383 | if (unlikely(!netid)) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 384 | goto out_err; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 385 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 386 | netid[nlen] = '\0'; |
| 387 | memcpy(netid, p, nlen); |
| 388 | |
| 389 | /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 390 | p = xdr_inline_decode(streamp, 4); |
| 391 | if (unlikely(!p)) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 392 | goto out_free_netid; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 393 | rlen = be32_to_cpup(p); |
| 394 | |
| 395 | p = xdr_inline_decode(streamp, rlen); |
| 396 | if (unlikely(!p)) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 397 | goto out_free_netid; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 398 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 399 | /* port is ".ABC.DEF", 8 chars max */ |
| 400 | if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) { |
Jesper Juhl | ad3d2ee | 2011-01-17 18:41:50 +0000 | [diff] [blame] | 401 | dprintk("%s: Invalid address, length %d\n", __func__, |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 402 | rlen); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 403 | goto out_free_netid; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 404 | } |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 405 | buf = kmalloc(rlen + 1, gfp_flags); |
Stanislav Fomichev | b9f8105 | 2011-02-05 23:13:01 +0000 | [diff] [blame] | 406 | if (!buf) { |
| 407 | dprintk("%s: Not enough memory\n", __func__); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 408 | goto out_free_netid; |
Stanislav Fomichev | b9f8105 | 2011-02-05 23:13:01 +0000 | [diff] [blame] | 409 | } |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 410 | buf[rlen] = '\0'; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 411 | memcpy(buf, p, rlen); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 412 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 413 | /* replace port '.' with '-' */ |
| 414 | portstr = strrchr(buf, '.'); |
| 415 | if (!portstr) { |
| 416 | dprintk("%s: Failed finding expected dot in port\n", |
| 417 | __func__); |
| 418 | goto out_free_buf; |
| 419 | } |
| 420 | *portstr = '-'; |
| 421 | |
| 422 | /* find '.' between address and port */ |
| 423 | portstr = strrchr(buf, '.'); |
| 424 | if (!portstr) { |
| 425 | dprintk("%s: Failed finding expected dot between address and " |
| 426 | "port\n", __func__); |
| 427 | goto out_free_buf; |
| 428 | } |
| 429 | *portstr = '\0'; |
| 430 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 431 | da = kzalloc(sizeof(*da), gfp_flags); |
| 432 | if (unlikely(!da)) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 433 | goto out_free_buf; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 434 | |
| 435 | INIT_LIST_HEAD(&da->da_node); |
| 436 | |
Stanislav Kinsbursky | 1709427 | 2012-01-19 19:05:57 +0400 | [diff] [blame] | 437 | if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr, |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 438 | sizeof(da->da_addr))) { |
| 439 | dprintk("%s: error parsing address %s\n", __func__, buf); |
| 440 | goto out_free_da; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 441 | } |
| 442 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 443 | portstr++; |
| 444 | sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 445 | port = htons((tmp[0] << 8) | (tmp[1])); |
| 446 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 447 | switch (da->da_addr.ss_family) { |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 448 | case AF_INET: |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 449 | ((struct sockaddr_in *)&da->da_addr)->sin_port = port; |
| 450 | da->da_addrlen = sizeof(struct sockaddr_in); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 451 | match_netid = "tcp"; |
| 452 | match_netid_len = 3; |
| 453 | break; |
| 454 | |
| 455 | case AF_INET6: |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 456 | ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port; |
| 457 | da->da_addrlen = sizeof(struct sockaddr_in6); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 458 | match_netid = "tcp6"; |
| 459 | match_netid_len = 4; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 460 | startsep = "["; |
| 461 | endsep = "]"; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 462 | break; |
| 463 | |
| 464 | default: |
| 465 | dprintk("%s: unsupported address family: %u\n", |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 466 | __func__, da->da_addr.ss_family); |
| 467 | goto out_free_da; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) { |
| 471 | dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n", |
| 472 | __func__, netid, match_netid); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 473 | goto out_free_da; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 474 | } |
| 475 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 476 | /* save human readable address */ |
| 477 | len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7; |
| 478 | da->da_remotestr = kzalloc(len, gfp_flags); |
| 479 | |
| 480 | /* NULL is ok, only used for dprintk */ |
| 481 | if (da->da_remotestr) |
| 482 | snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep, |
| 483 | buf, endsep, ntohs(port)); |
| 484 | |
| 485 | dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr); |
| 486 | kfree(buf); |
| 487 | kfree(netid); |
| 488 | return da; |
| 489 | |
| 490 | out_free_da: |
| 491 | kfree(da); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 492 | out_free_buf: |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 493 | dprintk("%s: Error parsing DS addr: %s\n", __func__, buf); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 494 | kfree(buf); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 495 | out_free_netid: |
| 496 | kfree(netid); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 497 | out_err: |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 498 | return NULL; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* Decode opaque device data and return the result */ |
| 502 | static struct nfs4_file_layout_dsaddr* |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 503 | decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 504 | { |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 505 | int i; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 506 | u32 cnt, num; |
| 507 | u8 *indexp; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 508 | __be32 *p; |
| 509 | u8 *stripe_indices; |
| 510 | u8 max_stripe_index; |
| 511 | struct nfs4_file_layout_dsaddr *dsaddr = NULL; |
| 512 | struct xdr_stream stream; |
Benny Halevy | f7da7a1 | 2011-05-19 14:16:47 -0400 | [diff] [blame] | 513 | struct xdr_buf buf; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 514 | struct page *scratch; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 515 | struct list_head dsaddrs; |
| 516 | struct nfs4_pnfs_ds_addr *da; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 517 | |
| 518 | /* set up xdr stream */ |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 519 | scratch = alloc_page(gfp_flags); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 520 | if (!scratch) |
| 521 | goto out_err; |
| 522 | |
Benny Halevy | f7da7a1 | 2011-05-19 14:16:47 -0400 | [diff] [blame] | 523 | xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 524 | xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 525 | |
| 526 | /* Get the stripe count (number of stripe index) */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 527 | p = xdr_inline_decode(&stream, 4); |
| 528 | if (unlikely(!p)) |
| 529 | goto out_err_free_scratch; |
| 530 | |
| 531 | cnt = be32_to_cpup(p); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 532 | dprintk("%s stripe count %d\n", __func__, cnt); |
| 533 | if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 534 | printk(KERN_WARNING "NFS: %s: stripe count %d greater than " |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 535 | "supported maximum %d\n", __func__, |
| 536 | cnt, NFS4_PNFS_MAX_STRIPE_CNT); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 537 | goto out_err_free_scratch; |
| 538 | } |
| 539 | |
| 540 | /* read stripe indices */ |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 541 | stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 542 | if (!stripe_indices) |
| 543 | goto out_err_free_scratch; |
| 544 | |
| 545 | p = xdr_inline_decode(&stream, cnt << 2); |
| 546 | if (unlikely(!p)) |
| 547 | goto out_err_free_stripe_indices; |
| 548 | |
| 549 | indexp = &stripe_indices[0]; |
| 550 | max_stripe_index = 0; |
| 551 | for (i = 0; i < cnt; i++) { |
| 552 | *indexp = be32_to_cpup(p++); |
| 553 | max_stripe_index = max(max_stripe_index, *indexp); |
| 554 | indexp++; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /* Check the multipath list count */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 558 | p = xdr_inline_decode(&stream, 4); |
| 559 | if (unlikely(!p)) |
| 560 | goto out_err_free_stripe_indices; |
| 561 | |
| 562 | num = be32_to_cpup(p); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 563 | dprintk("%s ds_num %u\n", __func__, num); |
| 564 | if (num > NFS4_PNFS_MAX_MULTI_CNT) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 565 | printk(KERN_WARNING "NFS: %s: multipath count %d greater than " |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 566 | "supported maximum %d\n", __func__, |
| 567 | num, NFS4_PNFS_MAX_MULTI_CNT); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 568 | goto out_err_free_stripe_indices; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 569 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 570 | |
| 571 | /* validate stripe indices are all < num */ |
| 572 | if (max_stripe_index >= num) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 573 | printk(KERN_WARNING "NFS: %s: stripe index %u >= num ds %u\n", |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 574 | __func__, max_stripe_index, num); |
| 575 | goto out_err_free_stripe_indices; |
| 576 | } |
| 577 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 578 | dsaddr = kzalloc(sizeof(*dsaddr) + |
| 579 | (sizeof(struct nfs4_pnfs_ds *) * (num - 1)), |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 580 | gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 581 | if (!dsaddr) |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 582 | goto out_err_free_stripe_indices; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 583 | |
| 584 | dsaddr->stripe_count = cnt; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 585 | dsaddr->stripe_indices = stripe_indices; |
| 586 | stripe_indices = NULL; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 587 | dsaddr->ds_num = num; |
Benny Halevy | 1775bc3 | 2011-05-20 13:47:33 +0200 | [diff] [blame] | 588 | nfs4_init_deviceid_node(&dsaddr->id_node, |
| 589 | NFS_SERVER(ino)->pnfs_curr_ld, |
| 590 | NFS_SERVER(ino)->nfs_client, |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 591 | &pdev->dev_id); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 592 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 593 | INIT_LIST_HEAD(&dsaddrs); |
| 594 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 595 | for (i = 0; i < dsaddr->ds_num; i++) { |
| 596 | int j; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 597 | u32 mp_count; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 598 | |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 599 | p = xdr_inline_decode(&stream, 4); |
| 600 | if (unlikely(!p)) |
| 601 | goto out_err_free_deviceid; |
| 602 | |
| 603 | mp_count = be32_to_cpup(p); /* multipath count */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 604 | for (j = 0; j < mp_count; j++) { |
Stanislav Kinsbursky | 1709427 | 2012-01-19 19:05:57 +0400 | [diff] [blame] | 605 | da = decode_ds_addr(NFS_SERVER(ino)->nfs_client->net, |
| 606 | &stream, gfp_flags); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 607 | if (da) |
| 608 | list_add_tail(&da->da_node, &dsaddrs); |
| 609 | } |
| 610 | if (list_empty(&dsaddrs)) { |
| 611 | dprintk("%s: no suitable DS addresses found\n", |
| 612 | __func__); |
| 613 | goto out_err_free_deviceid; |
| 614 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 615 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 616 | dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags); |
| 617 | if (!dsaddr->ds_list[i]) |
| 618 | goto out_err_drain_dsaddrs; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 619 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 620 | /* If DS was already in cache, free ds addrs */ |
| 621 | while (!list_empty(&dsaddrs)) { |
| 622 | da = list_first_entry(&dsaddrs, |
| 623 | struct nfs4_pnfs_ds_addr, |
| 624 | da_node); |
| 625 | list_del_init(&da->da_node); |
| 626 | kfree(da->da_remotestr); |
| 627 | kfree(da); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 628 | } |
| 629 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 630 | |
| 631 | __free_page(scratch); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 632 | return dsaddr; |
| 633 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 634 | out_err_drain_dsaddrs: |
| 635 | while (!list_empty(&dsaddrs)) { |
| 636 | da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr, |
| 637 | da_node); |
| 638 | list_del_init(&da->da_node); |
| 639 | kfree(da->da_remotestr); |
| 640 | kfree(da); |
| 641 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 642 | out_err_free_deviceid: |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 643 | nfs4_fl_free_deviceid(dsaddr); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 644 | /* stripe_indicies was part of dsaddr */ |
| 645 | goto out_err_free_scratch; |
| 646 | out_err_free_stripe_indices: |
| 647 | kfree(stripe_indices); |
| 648 | out_err_free_scratch: |
| 649 | __free_page(scratch); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 650 | out_err: |
| 651 | dprintk("%s ERROR: returning NULL\n", __func__); |
| 652 | return NULL; |
| 653 | } |
| 654 | |
| 655 | /* |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 656 | * Decode the opaque device specified in 'dev' and add it to the cache of |
| 657 | * available devices. |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 658 | */ |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 659 | static struct nfs4_file_layout_dsaddr * |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 660 | decode_and_add_device(struct inode *inode, struct pnfs_device *dev, gfp_t gfp_flags) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 661 | { |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 662 | struct nfs4_deviceid_node *d; |
| 663 | struct nfs4_file_layout_dsaddr *n, *new; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 664 | |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 665 | new = decode_device(inode, dev, gfp_flags); |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 666 | if (!new) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 667 | printk(KERN_WARNING "NFS: %s: Could not decode or add device\n", |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 668 | __func__); |
| 669 | return NULL; |
| 670 | } |
| 671 | |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 672 | d = nfs4_insert_deviceid_node(&new->id_node); |
| 673 | n = container_of(d, struct nfs4_file_layout_dsaddr, id_node); |
| 674 | if (n != new) { |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 675 | nfs4_fl_free_deviceid(new); |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 676 | return n; |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 677 | } |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 678 | |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 679 | return new; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* |
| 683 | * Retrieve the information for dev_id, add it to the list |
| 684 | * of available devices, and return it. |
| 685 | */ |
| 686 | struct nfs4_file_layout_dsaddr * |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 687 | get_device_info(struct inode *inode, struct nfs4_deviceid *dev_id, gfp_t gfp_flags) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 688 | { |
| 689 | struct pnfs_device *pdev = NULL; |
| 690 | u32 max_resp_sz; |
| 691 | int max_pages; |
| 692 | struct page **pages = NULL; |
| 693 | struct nfs4_file_layout_dsaddr *dsaddr = NULL; |
| 694 | int rc, i; |
| 695 | struct nfs_server *server = NFS_SERVER(inode); |
| 696 | |
| 697 | /* |
| 698 | * Use the session max response size as the basis for setting |
| 699 | * GETDEVICEINFO's maxcount |
| 700 | */ |
| 701 | max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; |
Andy Adamson | e5265a0 | 2012-04-14 03:56:35 -0400 | [diff] [blame^] | 702 | max_pages = nfs_page_array_len(0, max_resp_sz); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 703 | dprintk("%s inode %p max_resp_sz %u max_pages %d\n", |
| 704 | __func__, inode, max_resp_sz, max_pages); |
| 705 | |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 706 | pdev = kzalloc(sizeof(struct pnfs_device), gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 707 | if (pdev == NULL) |
| 708 | return NULL; |
| 709 | |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 710 | pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 711 | if (pages == NULL) { |
| 712 | kfree(pdev); |
| 713 | return NULL; |
| 714 | } |
| 715 | for (i = 0; i < max_pages; i++) { |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 716 | pages[i] = alloc_page(gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 717 | if (!pages[i]) |
| 718 | goto out_free; |
| 719 | } |
| 720 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 721 | memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id)); |
| 722 | pdev->layout_type = LAYOUT_NFSV4_1_FILES; |
| 723 | pdev->pages = pages; |
| 724 | pdev->pgbase = 0; |
| 725 | pdev->pglen = PAGE_SIZE * max_pages; |
| 726 | pdev->mincount = 0; |
| 727 | |
| 728 | rc = nfs4_proc_getdeviceinfo(server, pdev); |
| 729 | dprintk("%s getdevice info returns %d\n", __func__, rc); |
| 730 | if (rc) |
| 731 | goto out_free; |
| 732 | |
| 733 | /* |
| 734 | * Found new device, need to decode it and then add it to the |
| 735 | * list of known devices for this mountpoint. |
| 736 | */ |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 737 | dsaddr = decode_and_add_device(inode, pdev, gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 738 | out_free: |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 739 | for (i = 0; i < max_pages; i++) |
| 740 | __free_page(pages[i]); |
| 741 | kfree(pages); |
| 742 | kfree(pdev); |
| 743 | dprintk("<-- %s dsaddr %p\n", __func__, dsaddr); |
| 744 | return dsaddr; |
| 745 | } |
| 746 | |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 747 | void |
| 748 | nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 749 | { |
Benny Halevy | 1775bc3 | 2011-05-20 13:47:33 +0200 | [diff] [blame] | 750 | nfs4_put_deviceid_node(&dsaddr->id_node); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 751 | } |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 752 | |
| 753 | /* |
| 754 | * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit |
| 755 | * Then: ((res + fsi) % dsaddr->stripe_count) |
| 756 | */ |
| 757 | u32 |
| 758 | nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset) |
| 759 | { |
| 760 | struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg); |
| 761 | u64 tmp; |
| 762 | |
| 763 | tmp = offset - flseg->pattern_offset; |
| 764 | do_div(tmp, flseg->stripe_unit); |
| 765 | tmp += flseg->first_stripe_index; |
| 766 | return do_div(tmp, flseg->dsaddr->stripe_count); |
| 767 | } |
| 768 | |
| 769 | u32 |
| 770 | nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j) |
| 771 | { |
| 772 | return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j]; |
| 773 | } |
| 774 | |
| 775 | struct nfs_fh * |
| 776 | nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j) |
| 777 | { |
| 778 | struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg); |
| 779 | u32 i; |
| 780 | |
| 781 | if (flseg->stripe_type == STRIPE_SPARSE) { |
| 782 | if (flseg->num_fh == 1) |
| 783 | i = 0; |
| 784 | else if (flseg->num_fh == 0) |
| 785 | /* Use the MDS OPEN fh set in nfs_read_rpcsetup */ |
| 786 | return NULL; |
| 787 | else |
| 788 | i = nfs4_fl_calc_ds_index(lseg, j); |
| 789 | } else |
| 790 | i = j; |
| 791 | return flseg->fh_array[i]; |
| 792 | } |
| 793 | |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 794 | static void |
| 795 | filelayout_mark_devid_negative(struct nfs4_file_layout_dsaddr *dsaddr, |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 796 | int err, const char *ds_remotestr) |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 797 | { |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 798 | u32 *p = (u32 *)&dsaddr->id_node.deviceid; |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 799 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 800 | printk(KERN_ERR "NFS: data server %s connection error %d." |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 801 | " Deviceid [%x%x%x%x] marked out of use.\n", |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 802 | ds_remotestr, err, p[0], p[1], p[2], p[3]); |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 803 | |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 804 | spin_lock(&nfs4_ds_cache_lock); |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 805 | dsaddr->flags |= NFS4_DEVICE_ID_NEG_ENTRY; |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 806 | spin_unlock(&nfs4_ds_cache_lock); |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 809 | struct nfs4_pnfs_ds * |
| 810 | nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx) |
| 811 | { |
| 812 | struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr; |
| 813 | struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx]; |
| 814 | |
| 815 | if (ds == NULL) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 816 | printk(KERN_ERR "NFS: %s: No data server for offset index %d\n", |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 817 | __func__, ds_idx); |
| 818 | return NULL; |
| 819 | } |
| 820 | |
| 821 | if (!ds->ds_clp) { |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 822 | struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode); |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 823 | int err; |
| 824 | |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 825 | if (dsaddr->flags & NFS4_DEVICE_ID_NEG_ENTRY) { |
| 826 | /* Already tried to connect, don't try again */ |
| 827 | dprintk("%s Deviceid marked out of use\n", __func__); |
| 828 | return NULL; |
| 829 | } |
| 830 | err = nfs4_ds_connect(s, ds); |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 831 | if (err) { |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 832 | filelayout_mark_devid_negative(dsaddr, err, |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 833 | ds->ds_remotestr); |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 834 | return NULL; |
| 835 | } |
| 836 | } |
| 837 | return ds; |
| 838 | } |