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