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>
36 #include "nfs4filelayout.h"
38 #define NFSDBG_FACILITY NFSDBG_PNFS_LD
40 static unsigned int dataserver_timeo = NFS4_DEF_DS_TIMEO;
41 static unsigned int dataserver_retrans = NFS4_DEF_DS_RETRANS;
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.
52 static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
53 static LIST_HEAD(nfs4_data_server_cache);
57 print_ds(struct nfs4_pnfs_ds *ds)
60 printk("%s NULL device\n", __func__);
66 " cl_exchange_flags %x\n",
68 atomic_read(&ds->ds_count), ds->ds_clp,
69 ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
73 same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
75 struct sockaddr_in *a, *b;
76 struct sockaddr_in6 *a6, *b6;
78 if (addr1->sa_family != addr2->sa_family)
81 switch (addr1->sa_family) {
83 a = (struct sockaddr_in *)addr1;
84 b = (struct sockaddr_in *)addr2;
86 if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
87 a->sin_port == b->sin_port)
92 a6 = (struct sockaddr_in6 *)addr1;
93 b6 = (struct sockaddr_in6 *)addr2;
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)
101 if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
102 a6->sin6_port == b6->sin6_port)
107 dprintk("%s: unhandled address family: %u\n",
108 __func__, addr1->sa_family);
116 _same_data_server_addrs_locked(const struct list_head *dsaddrs1,
117 const struct list_head *dsaddrs2)
119 struct nfs4_pnfs_ds_addr *da1, *da2;
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))
131 if (da1 == NULL && da2 == NULL)
138 * Lookup DS by addresses. nfs4_ds_cache_lock is held
140 static struct nfs4_pnfs_ds *
141 _data_server_lookup_locked(const struct list_head *dsaddrs)
143 struct nfs4_pnfs_ds *ds;
145 list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
146 if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
152 * Create an rpc connection to the nfs4_pnfs_ds data server
153 * Currently only supports IPv4 and IPv6 addresses
156 nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds)
158 struct nfs_client *clp = ERR_PTR(-EIO);
159 struct nfs4_pnfs_ds_addr *da;
162 dprintk("--> %s DS %s au_flavor %d\n", __func__, ds->ds_remotestr,
163 mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor);
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);
169 clp = nfs4_set_ds_client(mds_srv->nfs_client,
170 (struct sockaddr *)&da->da_addr,
171 da->da_addrlen, IPPROTO_TCP,
172 dataserver_timeo, dataserver_retrans);
178 status = PTR_ERR(clp);
182 status = nfs4_init_ds_session(clp, mds_srv->nfs_client->cl_lease_time);
187 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
196 destroy_ds(struct nfs4_pnfs_ds *ds)
198 struct nfs4_pnfs_ds_addr *da;
200 dprintk("--> %s\n", __func__);
205 nfs_put_client(ds->ds_clp);
207 while (!list_empty(&ds->ds_addrs)) {
208 da = list_first_entry(&ds->ds_addrs,
209 struct nfs4_pnfs_ds_addr,
211 list_del_init(&da->da_node);
212 kfree(da->da_remotestr);
216 kfree(ds->ds_remotestr);
221 nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
223 struct nfs4_pnfs_ds *ds;
226 nfs4_print_deviceid(&dsaddr->id_node.deviceid);
228 for (i = 0; i < dsaddr->ds_num; i++) {
229 ds = dsaddr->ds_list[i];
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);
239 kfree(dsaddr->stripe_indices);
244 * Create a string with a human readable address and port to avoid
245 * complicated setup around many dprinks.
248 nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
250 struct nfs4_pnfs_ds_addr *da;
255 len = 3; /* '{', '}' and eol */
256 list_for_each_entry(da, dsaddrs, da_node) {
257 len += strlen(da->da_remotestr) + 1; /* string plus comma */
260 remotestr = kzalloc(len, gfp_flags);
267 list_for_each_entry(da, dsaddrs, da_node) {
268 size_t ll = strlen(da->da_remotestr);
273 memcpy(p, da->da_remotestr, ll);
292 static struct nfs4_pnfs_ds *
293 nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
295 struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
298 if (list_empty(dsaddrs)) {
299 dprintk("%s: no addresses defined\n", __func__);
303 ds = kzalloc(sizeof(*ds), gfp_flags);
307 /* this is only used for debugging, so it's ok if its NULL */
308 remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
310 spin_lock(&nfs4_ds_cache_lock);
311 tmp_ds = _data_server_lookup_locked(dsaddrs);
312 if (tmp_ds == NULL) {
313 INIT_LIST_HEAD(&ds->ds_addrs);
314 list_splice_init(dsaddrs, &ds->ds_addrs);
315 ds->ds_remotestr = remotestr;
316 atomic_set(&ds->ds_count, 1);
317 INIT_LIST_HEAD(&ds->ds_node);
319 list_add(&ds->ds_node, &nfs4_data_server_cache);
320 dprintk("%s add new data server %s\n", __func__,
325 atomic_inc(&tmp_ds->ds_count);
326 dprintk("%s data server %s found, inc'ed ds_count to %d\n",
327 __func__, tmp_ds->ds_remotestr,
328 atomic_read(&tmp_ds->ds_count));
331 spin_unlock(&nfs4_ds_cache_lock);
337 * Currently only supports ipv4, ipv6 and one multi-path address.
339 static struct nfs4_pnfs_ds_addr *
340 decode_ds_addr(struct net *net, struct xdr_stream *streamp, gfp_t gfp_flags)
342 struct nfs4_pnfs_ds_addr *da = NULL;
348 char *netid, *match_netid;
349 size_t len, match_netid_len;
355 p = xdr_inline_decode(streamp, 4);
358 nlen = be32_to_cpup(p++);
360 p = xdr_inline_decode(streamp, nlen);
364 netid = kmalloc(nlen+1, gfp_flags);
365 if (unlikely(!netid))
369 memcpy(netid, p, nlen);
371 /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
372 p = xdr_inline_decode(streamp, 4);
375 rlen = be32_to_cpup(p);
377 p = xdr_inline_decode(streamp, rlen);
381 /* port is ".ABC.DEF", 8 chars max */
382 if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
383 dprintk("%s: Invalid address, length %d\n", __func__,
387 buf = kmalloc(rlen + 1, gfp_flags);
389 dprintk("%s: Not enough memory\n", __func__);
393 memcpy(buf, p, rlen);
395 /* replace port '.' with '-' */
396 portstr = strrchr(buf, '.');
398 dprintk("%s: Failed finding expected dot in port\n",
404 /* find '.' between address and port */
405 portstr = strrchr(buf, '.');
407 dprintk("%s: Failed finding expected dot between address and "
413 da = kzalloc(sizeof(*da), gfp_flags);
417 INIT_LIST_HEAD(&da->da_node);
419 if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
420 sizeof(da->da_addr))) {
421 dprintk("%s: error parsing address %s\n", __func__, buf);
426 sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
427 port = htons((tmp[0] << 8) | (tmp[1]));
429 switch (da->da_addr.ss_family) {
431 ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
432 da->da_addrlen = sizeof(struct sockaddr_in);
438 ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
439 da->da_addrlen = sizeof(struct sockaddr_in6);
440 match_netid = "tcp6";
447 dprintk("%s: unsupported address family: %u\n",
448 __func__, da->da_addr.ss_family);
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);
458 /* save human readable address */
459 len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
460 da->da_remotestr = kzalloc(len, gfp_flags);
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));
467 dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
475 dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
483 /* Decode opaque device data and return the result */
484 static struct nfs4_file_layout_dsaddr*
485 decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags)
493 struct nfs4_file_layout_dsaddr *dsaddr = NULL;
494 struct xdr_stream stream;
496 struct page *scratch;
497 struct list_head dsaddrs;
498 struct nfs4_pnfs_ds_addr *da;
500 /* set up xdr stream */
501 scratch = alloc_page(gfp_flags);
505 xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen);
506 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
508 /* Get the stripe count (number of stripe index) */
509 p = xdr_inline_decode(&stream, 4);
511 goto out_err_free_scratch;
513 cnt = be32_to_cpup(p);
514 dprintk("%s stripe count %d\n", __func__, cnt);
515 if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) {
516 printk(KERN_WARNING "NFS: %s: stripe count %d greater than "
517 "supported maximum %d\n", __func__,
518 cnt, NFS4_PNFS_MAX_STRIPE_CNT);
519 goto out_err_free_scratch;
522 /* read stripe indices */
523 stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags);
525 goto out_err_free_scratch;
527 p = xdr_inline_decode(&stream, cnt << 2);
529 goto out_err_free_stripe_indices;
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);
539 /* Check the multipath list count */
540 p = xdr_inline_decode(&stream, 4);
542 goto out_err_free_stripe_indices;
544 num = be32_to_cpup(p);
545 dprintk("%s ds_num %u\n", __func__, num);
546 if (num > NFS4_PNFS_MAX_MULTI_CNT) {
547 printk(KERN_WARNING "NFS: %s: multipath count %d greater than "
548 "supported maximum %d\n", __func__,
549 num, NFS4_PNFS_MAX_MULTI_CNT);
550 goto out_err_free_stripe_indices;
553 /* validate stripe indices are all < num */
554 if (max_stripe_index >= num) {
555 printk(KERN_WARNING "NFS: %s: stripe index %u >= num ds %u\n",
556 __func__, max_stripe_index, num);
557 goto out_err_free_stripe_indices;
560 dsaddr = kzalloc(sizeof(*dsaddr) +
561 (sizeof(struct nfs4_pnfs_ds *) * (num - 1)),
564 goto out_err_free_stripe_indices;
566 dsaddr->stripe_count = cnt;
567 dsaddr->stripe_indices = stripe_indices;
568 stripe_indices = NULL;
569 dsaddr->ds_num = num;
570 nfs4_init_deviceid_node(&dsaddr->id_node,
571 NFS_SERVER(ino)->pnfs_curr_ld,
572 NFS_SERVER(ino)->nfs_client,
575 INIT_LIST_HEAD(&dsaddrs);
577 for (i = 0; i < dsaddr->ds_num; i++) {
581 p = xdr_inline_decode(&stream, 4);
583 goto out_err_free_deviceid;
585 mp_count = be32_to_cpup(p); /* multipath count */
586 for (j = 0; j < mp_count; j++) {
587 da = decode_ds_addr(NFS_SERVER(ino)->nfs_client->cl_net,
590 list_add_tail(&da->da_node, &dsaddrs);
592 if (list_empty(&dsaddrs)) {
593 dprintk("%s: no suitable DS addresses found\n",
595 goto out_err_free_deviceid;
598 dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags);
599 if (!dsaddr->ds_list[i])
600 goto out_err_drain_dsaddrs;
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,
607 list_del_init(&da->da_node);
608 kfree(da->da_remotestr);
613 __free_page(scratch);
616 out_err_drain_dsaddrs:
617 while (!list_empty(&dsaddrs)) {
618 da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr,
620 list_del_init(&da->da_node);
621 kfree(da->da_remotestr);
624 out_err_free_deviceid:
625 nfs4_fl_free_deviceid(dsaddr);
626 /* stripe_indicies was part of dsaddr */
627 goto out_err_free_scratch;
628 out_err_free_stripe_indices:
629 kfree(stripe_indices);
630 out_err_free_scratch:
631 __free_page(scratch);
633 dprintk("%s ERROR: returning NULL\n", __func__);
638 * Decode the opaque device specified in 'dev' and add it to the cache of
641 static struct nfs4_file_layout_dsaddr *
642 decode_and_add_device(struct inode *inode, struct pnfs_device *dev, gfp_t gfp_flags)
644 struct nfs4_deviceid_node *d;
645 struct nfs4_file_layout_dsaddr *n, *new;
647 new = decode_device(inode, dev, gfp_flags);
649 printk(KERN_WARNING "NFS: %s: Could not decode or add device\n",
654 d = nfs4_insert_deviceid_node(&new->id_node);
655 n = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
657 nfs4_fl_free_deviceid(new);
665 * Retrieve the information for dev_id, add it to the list
666 * of available devices, and return it.
668 struct nfs4_file_layout_dsaddr *
669 filelayout_get_device_info(struct inode *inode, struct nfs4_deviceid *dev_id, gfp_t gfp_flags)
671 struct pnfs_device *pdev = NULL;
674 struct page **pages = NULL;
675 struct nfs4_file_layout_dsaddr *dsaddr = NULL;
677 struct nfs_server *server = NFS_SERVER(inode);
680 * Use the session max response size as the basis for setting
681 * GETDEVICEINFO's maxcount
683 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
684 max_pages = nfs_page_array_len(0, max_resp_sz);
685 dprintk("%s inode %p max_resp_sz %u max_pages %d\n",
686 __func__, inode, max_resp_sz, max_pages);
688 pdev = kzalloc(sizeof(struct pnfs_device), gfp_flags);
692 pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
697 for (i = 0; i < max_pages; i++) {
698 pages[i] = alloc_page(gfp_flags);
703 memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id));
704 pdev->layout_type = LAYOUT_NFSV4_1_FILES;
707 pdev->pglen = max_resp_sz;
710 rc = nfs4_proc_getdeviceinfo(server, pdev);
711 dprintk("%s getdevice info returns %d\n", __func__, rc);
716 * Found new device, need to decode it and then add it to the
717 * list of known devices for this mountpoint.
719 dsaddr = decode_and_add_device(inode, pdev, gfp_flags);
721 for (i = 0; i < max_pages; i++)
722 __free_page(pages[i]);
725 dprintk("<-- %s dsaddr %p\n", __func__, dsaddr);
730 nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
732 nfs4_put_deviceid_node(&dsaddr->id_node);
736 * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
737 * Then: ((res + fsi) % dsaddr->stripe_count)
740 nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset)
742 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
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);
752 nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j)
754 return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j];
758 nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
760 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
763 if (flseg->stripe_type == STRIPE_SPARSE) {
764 if (flseg->num_fh == 1)
766 else if (flseg->num_fh == 0)
767 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
770 i = nfs4_fl_calc_ds_index(lseg, j);
773 return flseg->fh_array[i];
776 struct nfs4_pnfs_ds *
777 nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
779 struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr;
780 struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx];
781 struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
783 if (filelayout_test_devid_unavailable(devid))
787 printk(KERN_ERR "NFS: %s: No data server for offset index %d\n",
789 filelayout_mark_devid_invalid(devid);
794 struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode);
797 err = nfs4_ds_connect(s, ds);
799 nfs4_mark_deviceid_unavailable(devid);
806 module_param(dataserver_retrans, uint, 0644);
807 MODULE_PARM_DESC(dataserver_retrans, "The number of times the NFSv4.1 client "
808 "retries a request before it attempts further "
809 " recovery action.");
810 module_param(dataserver_timeo, uint, 0644);
811 MODULE_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.");