2 * Device operations for the pnfs nfs4 file layout driver.
5 * The Regents of the University of Michigan
8 * Dean Hildebrand <dhildebz@umich.edu>
9 * Garth Goodson <Garth.Goodson@netapp.com>
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.
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
31 #include <linux/nfs_fs.h>
32 #include <linux/vmalloc.h>
33 #include <linux/module.h>
34 #include <linux/sunrpc/addr.h>
36 #include "../internal.h"
37 #include "../nfs4session.h"
38 #include "filelayout.h"
40 #define NFSDBG_FACILITY NFSDBG_PNFS_LD
42 static unsigned int dataserver_timeo
= NFS4_DEF_DS_TIMEO
;
43 static unsigned int dataserver_retrans
= NFS4_DEF_DS_RETRANS
;
48 * Data servers can be mapped to different device ids.
49 * nfs4_pnfs_ds reference counting
50 * - set to 1 on allocation
51 * - incremented when a device id maps a data server already in the cache.
52 * - decremented when deviceid is removed from the cache.
54 static DEFINE_SPINLOCK(nfs4_ds_cache_lock
);
55 static LIST_HEAD(nfs4_data_server_cache
);
59 print_ds(struct nfs4_pnfs_ds
*ds
)
62 printk("%s NULL device\n", __func__
);
68 " cl_exchange_flags %x\n",
70 atomic_read(&ds
->ds_count
), ds
->ds_clp
,
71 ds
->ds_clp
? ds
->ds_clp
->cl_exchange_flags
: 0);
75 same_sockaddr(struct sockaddr
*addr1
, struct sockaddr
*addr2
)
77 struct sockaddr_in
*a
, *b
;
78 struct sockaddr_in6
*a6
, *b6
;
80 if (addr1
->sa_family
!= addr2
->sa_family
)
83 switch (addr1
->sa_family
) {
85 a
= (struct sockaddr_in
*)addr1
;
86 b
= (struct sockaddr_in
*)addr2
;
88 if (a
->sin_addr
.s_addr
== b
->sin_addr
.s_addr
&&
89 a
->sin_port
== b
->sin_port
)
94 a6
= (struct sockaddr_in6
*)addr1
;
95 b6
= (struct sockaddr_in6
*)addr2
;
97 /* LINKLOCAL addresses must have matching scope_id */
98 if (ipv6_addr_src_scope(&a6
->sin6_addr
) ==
99 IPV6_ADDR_SCOPE_LINKLOCAL
&&
100 a6
->sin6_scope_id
!= b6
->sin6_scope_id
)
103 if (ipv6_addr_equal(&a6
->sin6_addr
, &b6
->sin6_addr
) &&
104 a6
->sin6_port
== b6
->sin6_port
)
109 dprintk("%s: unhandled address family: %u\n",
110 __func__
, addr1
->sa_family
);
118 _same_data_server_addrs_locked(const struct list_head
*dsaddrs1
,
119 const struct list_head
*dsaddrs2
)
121 struct nfs4_pnfs_ds_addr
*da1
, *da2
;
123 /* step through both lists, comparing as we go */
124 for (da1
= list_first_entry(dsaddrs1
, typeof(*da1
), da_node
),
125 da2
= list_first_entry(dsaddrs2
, typeof(*da2
), da_node
);
126 da1
!= NULL
&& da2
!= NULL
;
127 da1
= list_entry(da1
->da_node
.next
, typeof(*da1
), da_node
),
128 da2
= list_entry(da2
->da_node
.next
, typeof(*da2
), da_node
)) {
129 if (!same_sockaddr((struct sockaddr
*)&da1
->da_addr
,
130 (struct sockaddr
*)&da2
->da_addr
))
133 if (da1
== NULL
&& da2
== NULL
)
140 * Lookup DS by addresses. nfs4_ds_cache_lock is held
142 static struct nfs4_pnfs_ds
*
143 _data_server_lookup_locked(const struct list_head
*dsaddrs
)
145 struct nfs4_pnfs_ds
*ds
;
147 list_for_each_entry(ds
, &nfs4_data_server_cache
, ds_node
)
148 if (_same_data_server_addrs_locked(&ds
->ds_addrs
, dsaddrs
))
154 * Create an rpc connection to the nfs4_pnfs_ds data server
155 * Currently only supports IPv4 and IPv6 addresses
158 nfs4_ds_connect(struct nfs_server
*mds_srv
, struct nfs4_pnfs_ds
*ds
)
160 struct nfs_client
*clp
= ERR_PTR(-EIO
);
161 struct nfs4_pnfs_ds_addr
*da
;
164 dprintk("--> %s DS %s au_flavor %d\n", __func__
, ds
->ds_remotestr
,
165 mds_srv
->nfs_client
->cl_rpcclient
->cl_auth
->au_flavor
);
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
);
171 clp
= nfs4_set_ds_client(mds_srv
->nfs_client
,
172 (struct sockaddr
*)&da
->da_addr
,
173 da
->da_addrlen
, IPPROTO_TCP
,
174 dataserver_timeo
, dataserver_retrans
);
180 status
= PTR_ERR(clp
);
184 status
= nfs4_init_ds_session(clp
, mds_srv
->nfs_client
->cl_lease_time
);
190 dprintk("%s [new] addr: %s\n", __func__
, ds
->ds_remotestr
);
199 destroy_ds(struct nfs4_pnfs_ds
*ds
)
201 struct nfs4_pnfs_ds_addr
*da
;
203 dprintk("--> %s\n", __func__
);
208 nfs_put_client(ds
->ds_clp
);
210 while (!list_empty(&ds
->ds_addrs
)) {
211 da
= list_first_entry(&ds
->ds_addrs
,
212 struct nfs4_pnfs_ds_addr
,
214 list_del_init(&da
->da_node
);
215 kfree(da
->da_remotestr
);
219 kfree(ds
->ds_remotestr
);
224 nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr
*dsaddr
)
226 struct nfs4_pnfs_ds
*ds
;
229 nfs4_print_deviceid(&dsaddr
->id_node
.deviceid
);
231 for (i
= 0; i
< dsaddr
->ds_num
; i
++) {
232 ds
= dsaddr
->ds_list
[i
];
234 if (atomic_dec_and_lock(&ds
->ds_count
,
235 &nfs4_ds_cache_lock
)) {
236 list_del_init(&ds
->ds_node
);
237 spin_unlock(&nfs4_ds_cache_lock
);
242 kfree(dsaddr
->stripe_indices
);
247 * Create a string with a human readable address and port to avoid
248 * complicated setup around many dprinks.
251 nfs4_pnfs_remotestr(struct list_head
*dsaddrs
, gfp_t gfp_flags
)
253 struct nfs4_pnfs_ds_addr
*da
;
258 len
= 3; /* '{', '}' and eol */
259 list_for_each_entry(da
, dsaddrs
, da_node
) {
260 len
+= strlen(da
->da_remotestr
) + 1; /* string plus comma */
263 remotestr
= kzalloc(len
, gfp_flags
);
270 list_for_each_entry(da
, dsaddrs
, da_node
) {
271 size_t ll
= strlen(da
->da_remotestr
);
276 memcpy(p
, da
->da_remotestr
, ll
);
295 static struct nfs4_pnfs_ds
*
296 nfs4_pnfs_ds_add(struct list_head
*dsaddrs
, gfp_t gfp_flags
)
298 struct nfs4_pnfs_ds
*tmp_ds
, *ds
= NULL
;
301 if (list_empty(dsaddrs
)) {
302 dprintk("%s: no addresses defined\n", __func__
);
306 ds
= kzalloc(sizeof(*ds
), gfp_flags
);
310 /* this is only used for debugging, so it's ok if its NULL */
311 remotestr
= nfs4_pnfs_remotestr(dsaddrs
, gfp_flags
);
313 spin_lock(&nfs4_ds_cache_lock
);
314 tmp_ds
= _data_server_lookup_locked(dsaddrs
);
315 if (tmp_ds
== NULL
) {
316 INIT_LIST_HEAD(&ds
->ds_addrs
);
317 list_splice_init(dsaddrs
, &ds
->ds_addrs
);
318 ds
->ds_remotestr
= remotestr
;
319 atomic_set(&ds
->ds_count
, 1);
320 INIT_LIST_HEAD(&ds
->ds_node
);
322 list_add(&ds
->ds_node
, &nfs4_data_server_cache
);
323 dprintk("%s add new data server %s\n", __func__
,
328 atomic_inc(&tmp_ds
->ds_count
);
329 dprintk("%s data server %s found, inc'ed ds_count to %d\n",
330 __func__
, tmp_ds
->ds_remotestr
,
331 atomic_read(&tmp_ds
->ds_count
));
334 spin_unlock(&nfs4_ds_cache_lock
);
340 * Currently only supports ipv4, ipv6 and one multi-path address.
342 static struct nfs4_pnfs_ds_addr
*
343 decode_ds_addr(struct net
*net
, struct xdr_stream
*streamp
, gfp_t gfp_flags
)
345 struct nfs4_pnfs_ds_addr
*da
= NULL
;
351 char *netid
, *match_netid
;
352 size_t len
, match_netid_len
;
358 p
= xdr_inline_decode(streamp
, 4);
361 nlen
= be32_to_cpup(p
++);
363 p
= xdr_inline_decode(streamp
, nlen
);
367 netid
= kmalloc(nlen
+1, gfp_flags
);
368 if (unlikely(!netid
))
372 memcpy(netid
, p
, nlen
);
374 /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
375 p
= xdr_inline_decode(streamp
, 4);
378 rlen
= be32_to_cpup(p
);
380 p
= xdr_inline_decode(streamp
, rlen
);
384 /* port is ".ABC.DEF", 8 chars max */
385 if (rlen
> INET6_ADDRSTRLEN
+ IPV6_SCOPE_ID_LEN
+ 8) {
386 dprintk("%s: Invalid address, length %d\n", __func__
,
390 buf
= kmalloc(rlen
+ 1, gfp_flags
);
392 dprintk("%s: Not enough memory\n", __func__
);
396 memcpy(buf
, p
, rlen
);
398 /* replace port '.' with '-' */
399 portstr
= strrchr(buf
, '.');
401 dprintk("%s: Failed finding expected dot in port\n",
407 /* find '.' between address and port */
408 portstr
= strrchr(buf
, '.');
410 dprintk("%s: Failed finding expected dot between address and "
416 da
= kzalloc(sizeof(*da
), gfp_flags
);
420 INIT_LIST_HEAD(&da
->da_node
);
422 if (!rpc_pton(net
, buf
, portstr
-buf
, (struct sockaddr
*)&da
->da_addr
,
423 sizeof(da
->da_addr
))) {
424 dprintk("%s: error parsing address %s\n", __func__
, buf
);
429 sscanf(portstr
, "%d-%d", &tmp
[0], &tmp
[1]);
430 port
= htons((tmp
[0] << 8) | (tmp
[1]));
432 switch (da
->da_addr
.ss_family
) {
434 ((struct sockaddr_in
*)&da
->da_addr
)->sin_port
= port
;
435 da
->da_addrlen
= sizeof(struct sockaddr_in
);
441 ((struct sockaddr_in6
*)&da
->da_addr
)->sin6_port
= port
;
442 da
->da_addrlen
= sizeof(struct sockaddr_in6
);
443 match_netid
= "tcp6";
450 dprintk("%s: unsupported address family: %u\n",
451 __func__
, da
->da_addr
.ss_family
);
455 if (nlen
!= match_netid_len
|| strncmp(netid
, match_netid
, nlen
)) {
456 dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
457 __func__
, netid
, match_netid
);
461 /* save human readable address */
462 len
= strlen(startsep
) + strlen(buf
) + strlen(endsep
) + 7;
463 da
->da_remotestr
= kzalloc(len
, gfp_flags
);
465 /* NULL is ok, only used for dprintk */
466 if (da
->da_remotestr
)
467 snprintf(da
->da_remotestr
, len
, "%s%s%s:%u", startsep
,
468 buf
, endsep
, ntohs(port
));
470 dprintk("%s: Parsed DS addr %s\n", __func__
, da
->da_remotestr
);
478 dprintk("%s: Error parsing DS addr: %s\n", __func__
, buf
);
486 /* Decode opaque device data and return the result */
487 struct nfs4_file_layout_dsaddr
*
488 nfs4_fl_alloc_deviceid_node(struct nfs_server
*server
, struct pnfs_device
*pdev
,
497 struct nfs4_file_layout_dsaddr
*dsaddr
= NULL
;
498 struct xdr_stream stream
;
500 struct page
*scratch
;
501 struct list_head dsaddrs
;
502 struct nfs4_pnfs_ds_addr
*da
;
504 /* set up xdr stream */
505 scratch
= alloc_page(gfp_flags
);
509 xdr_init_decode_pages(&stream
, &buf
, pdev
->pages
, pdev
->pglen
);
510 xdr_set_scratch_buffer(&stream
, page_address(scratch
), PAGE_SIZE
);
512 /* Get the stripe count (number of stripe index) */
513 p
= xdr_inline_decode(&stream
, 4);
515 goto out_err_free_scratch
;
517 cnt
= be32_to_cpup(p
);
518 dprintk("%s stripe count %d\n", __func__
, cnt
);
519 if (cnt
> NFS4_PNFS_MAX_STRIPE_CNT
) {
520 printk(KERN_WARNING
"NFS: %s: stripe count %d greater than "
521 "supported maximum %d\n", __func__
,
522 cnt
, NFS4_PNFS_MAX_STRIPE_CNT
);
523 goto out_err_free_scratch
;
526 /* read stripe indices */
527 stripe_indices
= kcalloc(cnt
, sizeof(u8
), gfp_flags
);
529 goto out_err_free_scratch
;
531 p
= xdr_inline_decode(&stream
, cnt
<< 2);
533 goto out_err_free_stripe_indices
;
535 indexp
= &stripe_indices
[0];
536 max_stripe_index
= 0;
537 for (i
= 0; i
< cnt
; i
++) {
538 *indexp
= be32_to_cpup(p
++);
539 max_stripe_index
= max(max_stripe_index
, *indexp
);
543 /* Check the multipath list count */
544 p
= xdr_inline_decode(&stream
, 4);
546 goto out_err_free_stripe_indices
;
548 num
= be32_to_cpup(p
);
549 dprintk("%s ds_num %u\n", __func__
, num
);
550 if (num
> NFS4_PNFS_MAX_MULTI_CNT
) {
551 printk(KERN_WARNING
"NFS: %s: multipath count %d greater than "
552 "supported maximum %d\n", __func__
,
553 num
, NFS4_PNFS_MAX_MULTI_CNT
);
554 goto out_err_free_stripe_indices
;
557 /* validate stripe indices are all < num */
558 if (max_stripe_index
>= num
) {
559 printk(KERN_WARNING
"NFS: %s: stripe index %u >= num ds %u\n",
560 __func__
, max_stripe_index
, num
);
561 goto out_err_free_stripe_indices
;
564 dsaddr
= kzalloc(sizeof(*dsaddr
) +
565 (sizeof(struct nfs4_pnfs_ds
*) * (num
- 1)),
568 goto out_err_free_stripe_indices
;
570 dsaddr
->stripe_count
= cnt
;
571 dsaddr
->stripe_indices
= stripe_indices
;
572 stripe_indices
= NULL
;
573 dsaddr
->ds_num
= num
;
574 nfs4_init_deviceid_node(&dsaddr
->id_node
, server
, &pdev
->dev_id
);
576 INIT_LIST_HEAD(&dsaddrs
);
578 for (i
= 0; i
< dsaddr
->ds_num
; i
++) {
582 p
= xdr_inline_decode(&stream
, 4);
584 goto out_err_free_deviceid
;
586 mp_count
= be32_to_cpup(p
); /* multipath count */
587 for (j
= 0; j
< mp_count
; j
++) {
588 da
= decode_ds_addr(server
->nfs_client
->cl_net
,
591 list_add_tail(&da
->da_node
, &dsaddrs
);
593 if (list_empty(&dsaddrs
)) {
594 dprintk("%s: no suitable DS addresses found\n",
596 goto out_err_free_deviceid
;
599 dsaddr
->ds_list
[i
] = nfs4_pnfs_ds_add(&dsaddrs
, gfp_flags
);
600 if (!dsaddr
->ds_list
[i
])
601 goto out_err_drain_dsaddrs
;
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
,
608 list_del_init(&da
->da_node
);
609 kfree(da
->da_remotestr
);
614 __free_page(scratch
);
617 out_err_drain_dsaddrs
:
618 while (!list_empty(&dsaddrs
)) {
619 da
= list_first_entry(&dsaddrs
, struct nfs4_pnfs_ds_addr
,
621 list_del_init(&da
->da_node
);
622 kfree(da
->da_remotestr
);
625 out_err_free_deviceid
:
626 nfs4_fl_free_deviceid(dsaddr
);
627 /* stripe_indicies was part of dsaddr */
628 goto out_err_free_scratch
;
629 out_err_free_stripe_indices
:
630 kfree(stripe_indices
);
631 out_err_free_scratch
:
632 __free_page(scratch
);
634 dprintk("%s ERROR: returning NULL\n", __func__
);
639 nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr
*dsaddr
)
641 nfs4_put_deviceid_node(&dsaddr
->id_node
);
645 * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
646 * Then: ((res + fsi) % dsaddr->stripe_count)
649 nfs4_fl_calc_j_index(struct pnfs_layout_segment
*lseg
, loff_t offset
)
651 struct nfs4_filelayout_segment
*flseg
= FILELAYOUT_LSEG(lseg
);
654 tmp
= offset
- flseg
->pattern_offset
;
655 do_div(tmp
, flseg
->stripe_unit
);
656 tmp
+= flseg
->first_stripe_index
;
657 return do_div(tmp
, flseg
->dsaddr
->stripe_count
);
661 nfs4_fl_calc_ds_index(struct pnfs_layout_segment
*lseg
, u32 j
)
663 return FILELAYOUT_LSEG(lseg
)->dsaddr
->stripe_indices
[j
];
667 nfs4_fl_select_ds_fh(struct pnfs_layout_segment
*lseg
, u32 j
)
669 struct nfs4_filelayout_segment
*flseg
= FILELAYOUT_LSEG(lseg
);
672 if (flseg
->stripe_type
== STRIPE_SPARSE
) {
673 if (flseg
->num_fh
== 1)
675 else if (flseg
->num_fh
== 0)
676 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
679 i
= nfs4_fl_calc_ds_index(lseg
, j
);
682 return flseg
->fh_array
[i
];
685 static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds
*ds
)
688 wait_on_bit_action(&ds
->ds_state
, NFS4DS_CONNECTING
,
689 nfs_wait_bit_killable
, TASK_KILLABLE
);
692 static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds
*ds
)
694 smp_mb__before_atomic();
695 clear_bit(NFS4DS_CONNECTING
, &ds
->ds_state
);
696 smp_mb__after_atomic();
697 wake_up_bit(&ds
->ds_state
, NFS4DS_CONNECTING
);
701 struct nfs4_pnfs_ds
*
702 nfs4_fl_prepare_ds(struct pnfs_layout_segment
*lseg
, u32 ds_idx
)
704 struct nfs4_file_layout_dsaddr
*dsaddr
= FILELAYOUT_LSEG(lseg
)->dsaddr
;
705 struct nfs4_pnfs_ds
*ds
= dsaddr
->ds_list
[ds_idx
];
706 struct nfs4_deviceid_node
*devid
= FILELAYOUT_DEVID_NODE(lseg
);
707 struct nfs4_pnfs_ds
*ret
= ds
;
710 printk(KERN_ERR
"NFS: %s: No data server for offset index %d\n",
712 filelayout_mark_devid_invalid(devid
);
719 if (test_and_set_bit(NFS4DS_CONNECTING
, &ds
->ds_state
) == 0) {
720 struct nfs_server
*s
= NFS_SERVER(lseg
->pls_layout
->plh_inode
);
723 err
= nfs4_ds_connect(s
, ds
);
725 nfs4_mark_deviceid_unavailable(devid
);
726 nfs4_clear_ds_conn_bit(ds
);
728 /* Either ds is connected, or ds is NULL */
729 nfs4_wait_ds_connect(ds
);
732 if (filelayout_test_devid_unavailable(devid
))
738 module_param(dataserver_retrans
, uint
, 0644);
739 MODULE_PARM_DESC(dataserver_retrans
, "The number of times the NFSv4.1 client "
740 "retries a request before it attempts further "
741 " recovery action.");
742 module_param(dataserver_timeo
, uint
, 0644);
743 MODULE_PARM_DESC(dataserver_timeo
, "The time (in tenths of a second) the "
744 "NFSv4.1 client waits for a response from a "
745 " data server before it retries an NFS request.");