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