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