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