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