1d67ae825STom Haynes /* 2d67ae825STom Haynes * Device operations for the pnfs nfs4 file layout driver. 3d67ae825STom Haynes * 4d67ae825STom Haynes * Copyright (c) 2014, Primary Data, Inc. All rights reserved. 5d67ae825STom Haynes * 6d67ae825STom Haynes * Tao Peng <bergwolf@primarydata.com> 7d67ae825STom Haynes */ 8d67ae825STom Haynes 9d67ae825STom Haynes #include <linux/nfs_fs.h> 10d67ae825STom Haynes #include <linux/vmalloc.h> 11d67ae825STom Haynes #include <linux/module.h> 12d67ae825STom Haynes #include <linux/sunrpc/addr.h> 13d67ae825STom Haynes 14d67ae825STom Haynes #include "../internal.h" 15d67ae825STom Haynes #include "../nfs4session.h" 16d67ae825STom Haynes #include "flexfilelayout.h" 17d67ae825STom Haynes 18d67ae825STom Haynes #define NFSDBG_FACILITY NFSDBG_PNFS_LD 19d67ae825STom Haynes 2015d03055STrond Myklebust static unsigned int dataserver_timeo = NFS_DEF_TCP_RETRANS; 2115d03055STrond Myklebust static unsigned int dataserver_retrans; 22d67ae825STom Haynes 2365990d1aSFred Isaman static bool ff_layout_has_available_ds(struct pnfs_layout_segment *lseg); 2465990d1aSFred Isaman 25d67ae825STom Haynes void nfs4_ff_layout_put_deviceid(struct nfs4_ff_layout_ds *mirror_ds) 26d67ae825STom Haynes { 2765990d1aSFred Isaman if (!IS_ERR_OR_NULL(mirror_ds)) 28d67ae825STom Haynes nfs4_put_deviceid_node(&mirror_ds->id_node); 29d67ae825STom Haynes } 30d67ae825STom Haynes 31d67ae825STom Haynes void nfs4_ff_layout_free_deviceid(struct nfs4_ff_layout_ds *mirror_ds) 32d67ae825STom Haynes { 33d67ae825STom Haynes nfs4_print_deviceid(&mirror_ds->id_node.deviceid); 34d67ae825STom Haynes nfs4_pnfs_ds_put(mirror_ds->ds); 35*1feb2616SWeston Andros Adamson kfree(mirror_ds->ds_versions); 3684a80f62STrond Myklebust kfree_rcu(mirror_ds, id_node.rcu); 37d67ae825STom Haynes } 38d67ae825STom Haynes 39d67ae825STom Haynes /* Decode opaque device data and construct new_ds using it */ 40d67ae825STom Haynes struct nfs4_ff_layout_ds * 41d67ae825STom Haynes nfs4_ff_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev, 42d67ae825STom Haynes gfp_t gfp_flags) 43d67ae825STom Haynes { 44d67ae825STom Haynes struct xdr_stream stream; 45d67ae825STom Haynes struct xdr_buf buf; 46d67ae825STom Haynes struct page *scratch; 47d67ae825STom Haynes struct list_head dsaddrs; 48d67ae825STom Haynes struct nfs4_pnfs_ds_addr *da; 49d67ae825STom Haynes struct nfs4_ff_layout_ds *new_ds = NULL; 50d67ae825STom Haynes struct nfs4_ff_ds_version *ds_versions = NULL; 51d67ae825STom Haynes u32 mp_count; 52d67ae825STom Haynes u32 version_count; 53d67ae825STom Haynes __be32 *p; 54d67ae825STom Haynes int i, ret = -ENOMEM; 55d67ae825STom Haynes 56d67ae825STom Haynes /* set up xdr stream */ 57d67ae825STom Haynes scratch = alloc_page(gfp_flags); 58d67ae825STom Haynes if (!scratch) 59d67ae825STom Haynes goto out_err; 60d67ae825STom Haynes 61d67ae825STom Haynes new_ds = kzalloc(sizeof(struct nfs4_ff_layout_ds), gfp_flags); 62d67ae825STom Haynes if (!new_ds) 63d67ae825STom Haynes goto out_scratch; 64d67ae825STom Haynes 65d67ae825STom Haynes nfs4_init_deviceid_node(&new_ds->id_node, 66d67ae825STom Haynes server, 67d67ae825STom Haynes &pdev->dev_id); 68d67ae825STom Haynes INIT_LIST_HEAD(&dsaddrs); 69d67ae825STom Haynes 70d67ae825STom Haynes xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen); 71d67ae825STom Haynes xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 72d67ae825STom Haynes 73d67ae825STom Haynes /* multipath count */ 74d67ae825STom Haynes p = xdr_inline_decode(&stream, 4); 75d67ae825STom Haynes if (unlikely(!p)) 76d67ae825STom Haynes goto out_err_drain_dsaddrs; 77d67ae825STom Haynes mp_count = be32_to_cpup(p); 78d67ae825STom Haynes dprintk("%s: multipath ds count %d\n", __func__, mp_count); 79d67ae825STom Haynes 80d67ae825STom Haynes for (i = 0; i < mp_count; i++) { 81d67ae825STom Haynes /* multipath ds */ 82d67ae825STom Haynes da = nfs4_decode_mp_ds_addr(server->nfs_client->cl_net, 83d67ae825STom Haynes &stream, gfp_flags); 84d67ae825STom Haynes if (da) 85d67ae825STom Haynes list_add_tail(&da->da_node, &dsaddrs); 86d67ae825STom Haynes } 87d67ae825STom Haynes if (list_empty(&dsaddrs)) { 88d67ae825STom Haynes dprintk("%s: no suitable DS addresses found\n", 89d67ae825STom Haynes __func__); 90d67ae825STom Haynes ret = -ENOMEDIUM; 91d67ae825STom Haynes goto out_err_drain_dsaddrs; 92d67ae825STom Haynes } 93d67ae825STom Haynes 94d67ae825STom Haynes /* version count */ 95d67ae825STom Haynes p = xdr_inline_decode(&stream, 4); 96d67ae825STom Haynes if (unlikely(!p)) 97d67ae825STom Haynes goto out_err_drain_dsaddrs; 98d67ae825STom Haynes version_count = be32_to_cpup(p); 99d67ae825STom Haynes dprintk("%s: version count %d\n", __func__, version_count); 100d67ae825STom Haynes 101d67ae825STom Haynes ds_versions = kzalloc(version_count * sizeof(struct nfs4_ff_ds_version), 102d67ae825STom Haynes gfp_flags); 103d67ae825STom Haynes if (!ds_versions) 104d67ae825STom Haynes goto out_scratch; 105d67ae825STom Haynes 106d67ae825STom Haynes for (i = 0; i < version_count; i++) { 107d67ae825STom Haynes /* 20 = version(4) + minor_version(4) + rsize(4) + wsize(4) + 108d67ae825STom Haynes * tightly_coupled(4) */ 109d67ae825STom Haynes p = xdr_inline_decode(&stream, 20); 110d67ae825STom Haynes if (unlikely(!p)) 111d67ae825STom Haynes goto out_err_drain_dsaddrs; 112d67ae825STom Haynes ds_versions[i].version = be32_to_cpup(p++); 113d67ae825STom Haynes ds_versions[i].minor_version = be32_to_cpup(p++); 114d67ae825STom Haynes ds_versions[i].rsize = nfs_block_size(be32_to_cpup(p++), NULL); 115d67ae825STom Haynes ds_versions[i].wsize = nfs_block_size(be32_to_cpup(p++), NULL); 116d67ae825STom Haynes ds_versions[i].tightly_coupled = be32_to_cpup(p); 117d67ae825STom Haynes 118d67ae825STom Haynes if (ds_versions[i].rsize > NFS_MAX_FILE_IO_SIZE) 119d67ae825STom Haynes ds_versions[i].rsize = NFS_MAX_FILE_IO_SIZE; 120d67ae825STom Haynes if (ds_versions[i].wsize > NFS_MAX_FILE_IO_SIZE) 121d67ae825STom Haynes ds_versions[i].wsize = NFS_MAX_FILE_IO_SIZE; 122d67ae825STom Haynes 123a7878ca1STigran Mkrtchyan /* 124a7878ca1STigran Mkrtchyan * check for valid major/minor combination. 125a7878ca1STigran Mkrtchyan * currently we support dataserver which talk: 126a7878ca1STigran Mkrtchyan * v3, v4.0, v4.1, v4.2 127a7878ca1STigran Mkrtchyan */ 128a7878ca1STigran Mkrtchyan if (!((ds_versions[i].version == 3 && ds_versions[i].minor_version == 0) || 129a7878ca1STigran Mkrtchyan (ds_versions[i].version == 4 && ds_versions[i].minor_version < 3))) { 130d67ae825STom Haynes dprintk("%s: [%d] unsupported ds version %d-%d\n", __func__, 131d67ae825STom Haynes i, ds_versions[i].version, 132d67ae825STom Haynes ds_versions[i].minor_version); 133d67ae825STom Haynes ret = -EPROTONOSUPPORT; 134d67ae825STom Haynes goto out_err_drain_dsaddrs; 135d67ae825STom Haynes } 136d67ae825STom Haynes 137d67ae825STom Haynes dprintk("%s: [%d] vers %u minor_ver %u rsize %u wsize %u coupled %d\n", 138d67ae825STom Haynes __func__, i, ds_versions[i].version, 139d67ae825STom Haynes ds_versions[i].minor_version, 140d67ae825STom Haynes ds_versions[i].rsize, 141d67ae825STom Haynes ds_versions[i].wsize, 142d67ae825STom Haynes ds_versions[i].tightly_coupled); 143d67ae825STom Haynes } 144d67ae825STom Haynes 145d67ae825STom Haynes new_ds->ds_versions = ds_versions; 146d67ae825STom Haynes new_ds->ds_versions_cnt = version_count; 147d67ae825STom Haynes 148d67ae825STom Haynes new_ds->ds = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags); 149d67ae825STom Haynes if (!new_ds->ds) 150d67ae825STom Haynes goto out_err_drain_dsaddrs; 151d67ae825STom Haynes 152d67ae825STom Haynes /* If DS was already in cache, free ds addrs */ 153d67ae825STom Haynes while (!list_empty(&dsaddrs)) { 154d67ae825STom Haynes da = list_first_entry(&dsaddrs, 155d67ae825STom Haynes struct nfs4_pnfs_ds_addr, 156d67ae825STom Haynes da_node); 157d67ae825STom Haynes list_del_init(&da->da_node); 158d67ae825STom Haynes kfree(da->da_remotestr); 159d67ae825STom Haynes kfree(da); 160d67ae825STom Haynes } 161d67ae825STom Haynes 162d67ae825STom Haynes __free_page(scratch); 163d67ae825STom Haynes return new_ds; 164d67ae825STom Haynes 165d67ae825STom Haynes out_err_drain_dsaddrs: 166d67ae825STom Haynes while (!list_empty(&dsaddrs)) { 167d67ae825STom Haynes da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr, 168d67ae825STom Haynes da_node); 169d67ae825STom Haynes list_del_init(&da->da_node); 170d67ae825STom Haynes kfree(da->da_remotestr); 171d67ae825STom Haynes kfree(da); 172d67ae825STom Haynes } 173d67ae825STom Haynes 174d67ae825STom Haynes kfree(ds_versions); 175d67ae825STom Haynes out_scratch: 176d67ae825STom Haynes __free_page(scratch); 177d67ae825STom Haynes out_err: 178d67ae825STom Haynes kfree(new_ds); 179d67ae825STom Haynes 180d67ae825STom Haynes dprintk("%s ERROR: returning %d\n", __func__, ret); 181d67ae825STom Haynes return NULL; 182d67ae825STom Haynes } 183d67ae825STom Haynes 184889d94d4STrond Myklebust static void ff_layout_mark_devid_invalid(struct pnfs_layout_segment *lseg, 185889d94d4STrond Myklebust struct nfs4_deviceid_node *devid) 186889d94d4STrond Myklebust { 1871c48cee8SWeston Andros Adamson nfs4_delete_deviceid(devid->ld, devid->nfs_client, &devid->deviceid); 188889d94d4STrond Myklebust if (!ff_layout_has_available_ds(lseg)) 189889d94d4STrond Myklebust pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, 190889d94d4STrond Myklebust lseg); 191889d94d4STrond Myklebust } 192889d94d4STrond Myklebust 193889d94d4STrond Myklebust static bool ff_layout_mirror_valid(struct pnfs_layout_segment *lseg, 19465990d1aSFred Isaman struct nfs4_ff_layout_mirror *mirror, 19565990d1aSFred Isaman bool create) 196889d94d4STrond Myklebust { 19765990d1aSFred Isaman if (mirror == NULL || IS_ERR(mirror->mirror_ds)) 19865990d1aSFred Isaman goto outerr; 19965990d1aSFred Isaman if (mirror->mirror_ds == NULL) { 20065990d1aSFred Isaman if (create) { 20165990d1aSFred Isaman struct nfs4_deviceid_node *node; 20265990d1aSFred Isaman struct pnfs_layout_hdr *lh = lseg->pls_layout; 20365990d1aSFred Isaman struct nfs4_ff_layout_ds *mirror_ds = ERR_PTR(-ENODEV); 20465990d1aSFred Isaman 20565990d1aSFred Isaman node = nfs4_find_get_deviceid(NFS_SERVER(lh->plh_inode), 20665990d1aSFred Isaman &mirror->devid, lh->plh_lc_cred, 20765990d1aSFred Isaman GFP_KERNEL); 20865990d1aSFred Isaman if (node) 20965990d1aSFred Isaman mirror_ds = FF_LAYOUT_MIRROR_DS(node); 21065990d1aSFred Isaman 21165990d1aSFred Isaman /* check for race with another call to this function */ 21265990d1aSFred Isaman if (cmpxchg(&mirror->mirror_ds, NULL, mirror_ds) && 21365990d1aSFred Isaman mirror_ds != ERR_PTR(-ENODEV)) 21465990d1aSFred Isaman nfs4_put_deviceid_node(node); 21565990d1aSFred Isaman } else 21665990d1aSFred Isaman goto outerr; 217889d94d4STrond Myklebust } 218f17f8a14STigran Mkrtchyan 219f17f8a14STigran Mkrtchyan if (IS_ERR(mirror->mirror_ds)) 220f17f8a14STigran Mkrtchyan goto outerr; 221f17f8a14STigran Mkrtchyan 222889d94d4STrond Myklebust if (mirror->mirror_ds->ds == NULL) { 223889d94d4STrond Myklebust struct nfs4_deviceid_node *devid; 224889d94d4STrond Myklebust devid = &mirror->mirror_ds->id_node; 225889d94d4STrond Myklebust ff_layout_mark_devid_invalid(lseg, devid); 226889d94d4STrond Myklebust return false; 227889d94d4STrond Myklebust } 228889d94d4STrond Myklebust return true; 22965990d1aSFred Isaman outerr: 23065990d1aSFred Isaman pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, lseg); 23165990d1aSFred Isaman return false; 232889d94d4STrond Myklebust } 233889d94d4STrond Myklebust 234d67ae825STom Haynes static void extend_ds_error(struct nfs4_ff_layout_ds_err *err, 235d67ae825STom Haynes u64 offset, u64 length) 236d67ae825STom Haynes { 237d67ae825STom Haynes u64 end; 238d67ae825STom Haynes 23917822b20STrond Myklebust end = max_t(u64, pnfs_end_offset(err->offset, err->length), 24017822b20STrond Myklebust pnfs_end_offset(offset, length)); 241d67ae825STom Haynes err->offset = min_t(u64, err->offset, offset); 242d67ae825STom Haynes err->length = end - err->offset; 243d67ae825STom Haynes } 244d67ae825STom Haynes 245b819ed4bSTrond Myklebust static int 246b819ed4bSTrond Myklebust ff_ds_error_match(const struct nfs4_ff_layout_ds_err *e1, 247b819ed4bSTrond Myklebust const struct nfs4_ff_layout_ds_err *e2) 248d67ae825STom Haynes { 249b819ed4bSTrond Myklebust int ret; 250b819ed4bSTrond Myklebust 251b819ed4bSTrond Myklebust if (e1->opnum != e2->opnum) 252b819ed4bSTrond Myklebust return e1->opnum < e2->opnum ? -1 : 1; 253b819ed4bSTrond Myklebust if (e1->status != e2->status) 254b819ed4bSTrond Myklebust return e1->status < e2->status ? -1 : 1; 25593b717fdSTrond Myklebust ret = memcmp(e1->stateid.data, e2->stateid.data, 25693b717fdSTrond Myklebust sizeof(e1->stateid.data)); 257b819ed4bSTrond Myklebust if (ret != 0) 258b819ed4bSTrond Myklebust return ret; 259b819ed4bSTrond Myklebust ret = memcmp(&e1->deviceid, &e2->deviceid, sizeof(e1->deviceid)); 260b819ed4bSTrond Myklebust if (ret != 0) 261b819ed4bSTrond Myklebust return ret; 26217822b20STrond Myklebust if (pnfs_end_offset(e1->offset, e1->length) < e2->offset) 263b819ed4bSTrond Myklebust return -1; 26417822b20STrond Myklebust if (e1->offset > pnfs_end_offset(e2->offset, e2->length)) 265b819ed4bSTrond Myklebust return 1; 266b819ed4bSTrond Myklebust /* If ranges overlap or are contiguous, they are the same */ 267b819ed4bSTrond Myklebust return 0; 268d67ae825STom Haynes } 269d67ae825STom Haynes 270b819ed4bSTrond Myklebust static void 271d67ae825STom Haynes ff_layout_add_ds_error_locked(struct nfs4_flexfile_layout *flo, 272d67ae825STom Haynes struct nfs4_ff_layout_ds_err *dserr) 273d67ae825STom Haynes { 274b819ed4bSTrond Myklebust struct nfs4_ff_layout_ds_err *err, *tmp; 275b819ed4bSTrond Myklebust struct list_head *head = &flo->error_list; 276b819ed4bSTrond Myklebust int match; 277d67ae825STom Haynes 278b819ed4bSTrond Myklebust /* Do insertion sort w/ merges */ 279b819ed4bSTrond Myklebust list_for_each_entry_safe(err, tmp, &flo->error_list, list) { 280b819ed4bSTrond Myklebust match = ff_ds_error_match(err, dserr); 281b819ed4bSTrond Myklebust if (match < 0) 282b819ed4bSTrond Myklebust continue; 283b819ed4bSTrond Myklebust if (match > 0) { 284b819ed4bSTrond Myklebust /* Add entry "dserr" _before_ entry "err" */ 285b819ed4bSTrond Myklebust head = &err->list; 286d67ae825STom Haynes break; 287d67ae825STom Haynes } 288b819ed4bSTrond Myklebust /* Entries match, so merge "err" into "dserr" */ 289b819ed4bSTrond Myklebust extend_ds_error(dserr, err->offset, err->length); 290cb067935STrond Myklebust list_replace(&err->list, &dserr->list); 291b819ed4bSTrond Myklebust kfree(err); 292cb067935STrond Myklebust return; 293d67ae825STom Haynes } 294d67ae825STom Haynes 295b819ed4bSTrond Myklebust list_add_tail(&dserr->list, head); 296d67ae825STom Haynes } 297d67ae825STom Haynes 298d67ae825STom Haynes int ff_layout_track_ds_error(struct nfs4_flexfile_layout *flo, 299d67ae825STom Haynes struct nfs4_ff_layout_mirror *mirror, u64 offset, 300d67ae825STom Haynes u64 length, int status, enum nfs_opnum4 opnum, 301d67ae825STom Haynes gfp_t gfp_flags) 302d67ae825STom Haynes { 303d67ae825STom Haynes struct nfs4_ff_layout_ds_err *dserr; 304d67ae825STom Haynes 305d67ae825STom Haynes if (status == 0) 306d67ae825STom Haynes return 0; 307d67ae825STom Haynes 308d67ae825STom Haynes if (mirror->mirror_ds == NULL) 309d67ae825STom Haynes return -EINVAL; 310d67ae825STom Haynes 311d67ae825STom Haynes dserr = kmalloc(sizeof(*dserr), gfp_flags); 312d67ae825STom Haynes if (!dserr) 313d67ae825STom Haynes return -ENOMEM; 314d67ae825STom Haynes 315d67ae825STom Haynes INIT_LIST_HEAD(&dserr->list); 316d67ae825STom Haynes dserr->offset = offset; 317d67ae825STom Haynes dserr->length = length; 318d67ae825STom Haynes dserr->status = status; 319d67ae825STom Haynes dserr->opnum = opnum; 320d67ae825STom Haynes nfs4_stateid_copy(&dserr->stateid, &mirror->stateid); 321d67ae825STom Haynes memcpy(&dserr->deviceid, &mirror->mirror_ds->id_node.deviceid, 322d67ae825STom Haynes NFS4_DEVICEID4_SIZE); 323d67ae825STom Haynes 324d67ae825STom Haynes spin_lock(&flo->generic_hdr.plh_inode->i_lock); 325b819ed4bSTrond Myklebust ff_layout_add_ds_error_locked(flo, dserr); 326d67ae825STom Haynes spin_unlock(&flo->generic_hdr.plh_inode->i_lock); 327d67ae825STom Haynes 328d67ae825STom Haynes return 0; 329d67ae825STom Haynes } 330d67ae825STom Haynes 33157f3f4c0SJeff Layton static struct rpc_cred * 33257f3f4c0SJeff Layton ff_layout_get_mirror_cred(struct nfs4_ff_layout_mirror *mirror, u32 iomode) 33357f3f4c0SJeff Layton { 3343064b686SJeff Layton struct rpc_cred *cred, __rcu **pcred; 33557f3f4c0SJeff Layton 3363064b686SJeff Layton if (iomode == IOMODE_READ) 3373064b686SJeff Layton pcred = &mirror->ro_cred; 3383064b686SJeff Layton else 3393064b686SJeff Layton pcred = &mirror->rw_cred; 34057f3f4c0SJeff Layton 34157f3f4c0SJeff Layton rcu_read_lock(); 34257f3f4c0SJeff Layton do { 34357f3f4c0SJeff Layton cred = rcu_dereference(*pcred); 34457f3f4c0SJeff Layton if (!cred) 34557f3f4c0SJeff Layton break; 34657f3f4c0SJeff Layton 34757f3f4c0SJeff Layton cred = get_rpccred_rcu(cred); 34857f3f4c0SJeff Layton } while(!cred); 34957f3f4c0SJeff Layton rcu_read_unlock(); 35057f3f4c0SJeff Layton return cred; 35157f3f4c0SJeff Layton } 35257f3f4c0SJeff Layton 353d67ae825STom Haynes struct nfs_fh * 354d67ae825STom Haynes nfs4_ff_layout_select_ds_fh(struct pnfs_layout_segment *lseg, u32 mirror_idx) 355d67ae825STom Haynes { 356d67ae825STom Haynes struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, mirror_idx); 357d67ae825STom Haynes struct nfs_fh *fh = NULL; 358d67ae825STom Haynes 35965990d1aSFred Isaman if (!ff_layout_mirror_valid(lseg, mirror, false)) { 360889d94d4STrond Myklebust pr_err_ratelimited("NFS: %s: No data server for mirror offset index %d\n", 361d67ae825STom Haynes __func__, mirror_idx); 362d67ae825STom Haynes goto out; 363d67ae825STom Haynes } 364d67ae825STom Haynes 365d67ae825STom Haynes /* FIXME: For now assume there is only 1 version available for the DS */ 366d67ae825STom Haynes fh = &mirror->fh_versions[0]; 367d67ae825STom Haynes out: 368d67ae825STom Haynes return fh; 369d67ae825STom Haynes } 370d67ae825STom Haynes 37195e2b7e9SJeff Layton /** 37295e2b7e9SJeff Layton * nfs4_ff_layout_prepare_ds - prepare a DS connection for an RPC call 37395e2b7e9SJeff Layton * @lseg: the layout segment we're operating on 37495e2b7e9SJeff Layton * @ds_idx: index of the DS to use 37595e2b7e9SJeff Layton * @fail_return: return layout on connect failure? 37695e2b7e9SJeff Layton * 37795e2b7e9SJeff Layton * Try to prepare a DS connection to accept an RPC call. This involves 37895e2b7e9SJeff Layton * selecting a mirror to use and connecting the client to it if it's not 37995e2b7e9SJeff Layton * already connected. 38095e2b7e9SJeff Layton * 38195e2b7e9SJeff Layton * Since we only need a single functioning mirror to satisfy a read, we don't 38295e2b7e9SJeff Layton * want to return the layout if there is one. For writes though, any down 38395e2b7e9SJeff Layton * mirror should result in a LAYOUTRETURN. @fail_return is how we distinguish 38495e2b7e9SJeff Layton * between the two cases. 38595e2b7e9SJeff Layton * 38695e2b7e9SJeff Layton * Returns a pointer to a connected DS object on success or NULL on failure. 38795e2b7e9SJeff Layton */ 388d67ae825STom Haynes struct nfs4_pnfs_ds * 389d67ae825STom Haynes nfs4_ff_layout_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx, 390d67ae825STom Haynes bool fail_return) 391d67ae825STom Haynes { 392d67ae825STom Haynes struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx); 393d67ae825STom Haynes struct nfs4_pnfs_ds *ds = NULL; 394d67ae825STom Haynes struct nfs4_deviceid_node *devid; 395d67ae825STom Haynes struct inode *ino = lseg->pls_layout->plh_inode; 396d67ae825STom Haynes struct nfs_server *s = NFS_SERVER(ino); 397d67ae825STom Haynes unsigned int max_payload; 398a33e4b03SWeston Andros Adamson int status; 399d67ae825STom Haynes 40065990d1aSFred Isaman if (!ff_layout_mirror_valid(lseg, mirror, true)) { 401889d94d4STrond Myklebust pr_err_ratelimited("NFS: %s: No data server for offset index %d\n", 402d67ae825STom Haynes __func__, ds_idx); 403d67ae825STom Haynes goto out; 404d67ae825STom Haynes } 405d67ae825STom Haynes 406d67ae825STom Haynes devid = &mirror->mirror_ds->id_node; 407d67ae825STom Haynes if (ff_layout_test_devid_unavailable(devid)) 4083dc14735STrond Myklebust goto out_fail; 409d67ae825STom Haynes 410d67ae825STom Haynes ds = mirror->mirror_ds->ds; 411d67ae825STom Haynes /* matching smp_wmb() in _nfs4_pnfs_v3/4_ds_connect */ 412d67ae825STom Haynes smp_rmb(); 413d67ae825STom Haynes if (ds->ds_clp) 41490a0be00SJeff Layton goto out; 415d67ae825STom Haynes 416d67ae825STom Haynes /* FIXME: For now we assume the server sent only one version of NFS 417d67ae825STom Haynes * to use for the DS. 418d67ae825STom Haynes */ 419a33e4b03SWeston Andros Adamson status = nfs4_pnfs_ds_connect(s, ds, devid, dataserver_timeo, 420d67ae825STom Haynes dataserver_retrans, 421d67ae825STom Haynes mirror->mirror_ds->ds_versions[0].version, 4227d38de3fSAnna Schumaker mirror->mirror_ds->ds_versions[0].minor_version); 423d67ae825STom Haynes 424d67ae825STom Haynes /* connect success, check rsize/wsize limit */ 425260f32adSTrond Myklebust if (!status) { 426d67ae825STom Haynes max_payload = 427d67ae825STom Haynes nfs_block_size(rpc_max_payload(ds->ds_clp->cl_rpcclient), 428d67ae825STom Haynes NULL); 429d67ae825STom Haynes if (mirror->mirror_ds->ds_versions[0].rsize > max_payload) 430d67ae825STom Haynes mirror->mirror_ds->ds_versions[0].rsize = max_payload; 431d67ae825STom Haynes if (mirror->mirror_ds->ds_versions[0].wsize > max_payload) 432d67ae825STom Haynes mirror->mirror_ds->ds_versions[0].wsize = max_payload; 4333dc14735STrond Myklebust goto out; 4343dc14735STrond Myklebust } 435da066f3fSWeston Andros Adamson out_fail: 436d67ae825STom Haynes ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout), 437d67ae825STom Haynes mirror, lseg->pls_range.offset, 438d67ae825STom Haynes lseg->pls_range.length, NFS4ERR_NXIO, 439d67ae825STom Haynes OP_ILLEGAL, GFP_NOIO); 440094069f1SJeff Layton if (fail_return || !ff_layout_has_available_ds(lseg)) 4412e5b29f0STrond Myklebust pnfs_error_mark_layout_for_return(ino, lseg); 442849dc324SJeff Layton ds = NULL; 443d67ae825STom Haynes out: 444d67ae825STom Haynes return ds; 445d67ae825STom Haynes } 446d67ae825STom Haynes 447d67ae825STom Haynes struct rpc_cred * 448d67ae825STom Haynes ff_layout_get_ds_cred(struct pnfs_layout_segment *lseg, u32 ds_idx, 449d67ae825STom Haynes struct rpc_cred *mdscred) 450d67ae825STom Haynes { 451d67ae825STom Haynes struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx); 452547a6376SJeff Layton struct rpc_cred *cred; 453d67ae825STom Haynes 45457f3f4c0SJeff Layton if (mirror) { 45557f3f4c0SJeff Layton cred = ff_layout_get_mirror_cred(mirror, lseg->pls_range.iomode); 45657f3f4c0SJeff Layton if (!cred) 45757f3f4c0SJeff Layton cred = get_rpccred(mdscred); 45857f3f4c0SJeff Layton } else { 45957f3f4c0SJeff Layton cred = get_rpccred(mdscred); 46057f3f4c0SJeff Layton } 461d67ae825STom Haynes return cred; 462d67ae825STom Haynes } 463d67ae825STom Haynes 464d67ae825STom Haynes /** 465d67ae825STom Haynes * Find or create a DS rpc client with th MDS server rpc client auth flavor 466d67ae825STom Haynes * in the nfs_client cl_ds_clients list. 467d67ae825STom Haynes */ 468d67ae825STom Haynes struct rpc_clnt * 469d67ae825STom Haynes nfs4_ff_find_or_create_ds_client(struct pnfs_layout_segment *lseg, u32 ds_idx, 470d67ae825STom Haynes struct nfs_client *ds_clp, struct inode *inode) 471d67ae825STom Haynes { 472d67ae825STom Haynes struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx); 473d67ae825STom Haynes 474d67ae825STom Haynes switch (mirror->mirror_ds->ds_versions[0].version) { 475d67ae825STom Haynes case 3: 476d67ae825STom Haynes /* For NFSv3 DS, flavor is set when creating DS connections */ 477d67ae825STom Haynes return ds_clp->cl_rpcclient; 478d67ae825STom Haynes case 4: 479d67ae825STom Haynes return nfs4_find_or_create_ds_client(ds_clp, inode); 480d67ae825STom Haynes default: 481d67ae825STom Haynes BUG(); 482d67ae825STom Haynes } 483d67ae825STom Haynes } 484d67ae825STom Haynes 4855b9b3c85STrond Myklebust void ff_layout_free_ds_ioerr(struct list_head *head) 486d67ae825STom Haynes { 4875b9b3c85STrond Myklebust struct nfs4_ff_layout_ds_err *err; 4885b9b3c85STrond Myklebust 4895b9b3c85STrond Myklebust while (!list_empty(head)) { 4905b9b3c85STrond Myklebust err = list_first_entry(head, 4915b9b3c85STrond Myklebust struct nfs4_ff_layout_ds_err, 4925b9b3c85STrond Myklebust list); 4935b9b3c85STrond Myklebust list_del(&err->list); 4945b9b3c85STrond Myklebust kfree(err); 4955b9b3c85STrond Myklebust } 4965b9b3c85STrond Myklebust } 4975b9b3c85STrond Myklebust 4985b9b3c85STrond Myklebust /* called with inode i_lock held */ 4995b9b3c85STrond Myklebust int ff_layout_encode_ds_ioerr(struct xdr_stream *xdr, const struct list_head *head) 5005b9b3c85STrond Myklebust { 5015b9b3c85STrond Myklebust struct nfs4_ff_layout_ds_err *err; 502d67ae825STom Haynes __be32 *p; 503d67ae825STom Haynes 5045b9b3c85STrond Myklebust list_for_each_entry(err, head, list) { 505d67ae825STom Haynes /* offset(8) + length(8) + stateid(NFS4_STATEID_SIZE) 506d1354907STrond Myklebust * + array length + deviceid(NFS4_DEVICEID4_SIZE) 507d1354907STrond Myklebust * + status(4) + opnum(4) 508d67ae825STom Haynes */ 509d67ae825STom Haynes p = xdr_reserve_space(xdr, 510d1354907STrond Myklebust 28 + NFS4_STATEID_SIZE + NFS4_DEVICEID4_SIZE); 511d67ae825STom Haynes if (unlikely(!p)) 512d67ae825STom Haynes return -ENOBUFS; 513d67ae825STom Haynes p = xdr_encode_hyper(p, err->offset); 514d67ae825STom Haynes p = xdr_encode_hyper(p, err->length); 515d67ae825STom Haynes p = xdr_encode_opaque_fixed(p, &err->stateid, 516d67ae825STom Haynes NFS4_STATEID_SIZE); 517d1354907STrond Myklebust /* Encode 1 error */ 518d1354907STrond Myklebust *p++ = cpu_to_be32(1); 519d67ae825STom Haynes p = xdr_encode_opaque_fixed(p, &err->deviceid, 520d67ae825STom Haynes NFS4_DEVICEID4_SIZE); 521d67ae825STom Haynes *p++ = cpu_to_be32(err->status); 522d67ae825STom Haynes *p++ = cpu_to_be32(err->opnum); 5235b9b3c85STrond Myklebust dprintk("%s: offset %llu length %llu status %d op %d\n", 524d67ae825STom Haynes __func__, err->offset, err->length, err->status, 5255b9b3c85STrond Myklebust err->opnum); 526d67ae825STom Haynes } 527d67ae825STom Haynes 528d67ae825STom Haynes return 0; 529d67ae825STom Haynes } 530d67ae825STom Haynes 5315b9b3c85STrond Myklebust static 5325b9b3c85STrond Myklebust unsigned int do_layout_fetch_ds_ioerr(struct pnfs_layout_hdr *lo, 5335b9b3c85STrond Myklebust const struct pnfs_layout_range *range, 5345b9b3c85STrond Myklebust struct list_head *head, 5355b9b3c85STrond Myklebust unsigned int maxnum) 5365b9b3c85STrond Myklebust { 5375b9b3c85STrond Myklebust struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo); 5385b9b3c85STrond Myklebust struct inode *inode = lo->plh_inode; 5395b9b3c85STrond Myklebust struct nfs4_ff_layout_ds_err *err, *n; 5405b9b3c85STrond Myklebust unsigned int ret = 0; 5415b9b3c85STrond Myklebust 5425b9b3c85STrond Myklebust spin_lock(&inode->i_lock); 5435b9b3c85STrond Myklebust list_for_each_entry_safe(err, n, &flo->error_list, list) { 5445b9b3c85STrond Myklebust if (!pnfs_is_range_intersecting(err->offset, 5455b9b3c85STrond Myklebust pnfs_end_offset(err->offset, err->length), 5465b9b3c85STrond Myklebust range->offset, 5475b9b3c85STrond Myklebust pnfs_end_offset(range->offset, range->length))) 5485b9b3c85STrond Myklebust continue; 5495b9b3c85STrond Myklebust if (!maxnum) 5505b9b3c85STrond Myklebust break; 5515b9b3c85STrond Myklebust list_move(&err->list, head); 5525b9b3c85STrond Myklebust maxnum--; 5535b9b3c85STrond Myklebust ret++; 5545b9b3c85STrond Myklebust } 5555b9b3c85STrond Myklebust spin_unlock(&inode->i_lock); 5565b9b3c85STrond Myklebust return ret; 5575b9b3c85STrond Myklebust } 5585b9b3c85STrond Myklebust 5595b9b3c85STrond Myklebust unsigned int ff_layout_fetch_ds_ioerr(struct pnfs_layout_hdr *lo, 5605b9b3c85STrond Myklebust const struct pnfs_layout_range *range, 5615b9b3c85STrond Myklebust struct list_head *head, 5625b9b3c85STrond Myklebust unsigned int maxnum) 5635b9b3c85STrond Myklebust { 5645b9b3c85STrond Myklebust unsigned int ret; 5655b9b3c85STrond Myklebust 5665b9b3c85STrond Myklebust ret = do_layout_fetch_ds_ioerr(lo, range, head, maxnum); 5675b9b3c85STrond Myklebust /* If we're over the max, discard all remaining entries */ 5685b9b3c85STrond Myklebust if (ret == maxnum) { 5695b9b3c85STrond Myklebust LIST_HEAD(discard); 5705b9b3c85STrond Myklebust do_layout_fetch_ds_ioerr(lo, range, &discard, -1); 5715b9b3c85STrond Myklebust ff_layout_free_ds_ioerr(&discard); 5725b9b3c85STrond Myklebust } 5735b9b3c85STrond Myklebust return ret; 5745b9b3c85STrond Myklebust } 5755b9b3c85STrond Myklebust 57681d6dc8bSTrond Myklebust static bool ff_read_layout_has_available_ds(struct pnfs_layout_segment *lseg) 577d67ae825STom Haynes { 578d67ae825STom Haynes struct nfs4_ff_layout_mirror *mirror; 579d67ae825STom Haynes struct nfs4_deviceid_node *devid; 58081d6dc8bSTrond Myklebust u32 idx; 581d67ae825STom Haynes 582d67ae825STom Haynes for (idx = 0; idx < FF_LAYOUT_MIRROR_COUNT(lseg); idx++) { 583d67ae825STom Haynes mirror = FF_LAYOUT_COMP(lseg, idx); 58465990d1aSFred Isaman if (mirror) { 58565990d1aSFred Isaman if (!mirror->mirror_ds) 58665990d1aSFred Isaman return true; 58765990d1aSFred Isaman if (IS_ERR(mirror->mirror_ds)) 58865990d1aSFred Isaman continue; 589d67ae825STom Haynes devid = &mirror->mirror_ds->id_node; 590d67ae825STom Haynes if (!ff_layout_test_devid_unavailable(devid)) 591d67ae825STom Haynes return true; 592d67ae825STom Haynes } 593d67ae825STom Haynes } 594d67ae825STom Haynes 595d67ae825STom Haynes return false; 596d67ae825STom Haynes } 597d67ae825STom Haynes 59881d6dc8bSTrond Myklebust static bool ff_rw_layout_has_available_ds(struct pnfs_layout_segment *lseg) 59981d6dc8bSTrond Myklebust { 60081d6dc8bSTrond Myklebust struct nfs4_ff_layout_mirror *mirror; 60181d6dc8bSTrond Myklebust struct nfs4_deviceid_node *devid; 60281d6dc8bSTrond Myklebust u32 idx; 60381d6dc8bSTrond Myklebust 60481d6dc8bSTrond Myklebust for (idx = 0; idx < FF_LAYOUT_MIRROR_COUNT(lseg); idx++) { 60581d6dc8bSTrond Myklebust mirror = FF_LAYOUT_COMP(lseg, idx); 60665990d1aSFred Isaman if (!mirror || IS_ERR(mirror->mirror_ds)) 60781d6dc8bSTrond Myklebust return false; 60865990d1aSFred Isaman if (!mirror->mirror_ds) 60965990d1aSFred Isaman continue; 61081d6dc8bSTrond Myklebust devid = &mirror->mirror_ds->id_node; 61181d6dc8bSTrond Myklebust if (ff_layout_test_devid_unavailable(devid)) 61281d6dc8bSTrond Myklebust return false; 61381d6dc8bSTrond Myklebust } 61481d6dc8bSTrond Myklebust 61581d6dc8bSTrond Myklebust return FF_LAYOUT_MIRROR_COUNT(lseg) != 0; 61681d6dc8bSTrond Myklebust } 61781d6dc8bSTrond Myklebust 61865990d1aSFred Isaman static bool ff_layout_has_available_ds(struct pnfs_layout_segment *lseg) 61981d6dc8bSTrond Myklebust { 62081d6dc8bSTrond Myklebust if (lseg->pls_range.iomode == IOMODE_READ) 62181d6dc8bSTrond Myklebust return ff_read_layout_has_available_ds(lseg); 62281d6dc8bSTrond Myklebust /* Note: RW layout needs all mirrors available */ 62381d6dc8bSTrond Myklebust return ff_rw_layout_has_available_ds(lseg); 62481d6dc8bSTrond Myklebust } 62581d6dc8bSTrond Myklebust 6263b13b4b3STom Haynes bool ff_layout_avoid_mds_available_ds(struct pnfs_layout_segment *lseg) 6273b13b4b3STom Haynes { 6283b13b4b3STom Haynes return ff_layout_no_fallback_to_mds(lseg) || 6293b13b4b3STom Haynes ff_layout_has_available_ds(lseg); 6303b13b4b3STom Haynes } 6313b13b4b3STom Haynes 632fb1084e3STom Haynes bool ff_layout_avoid_read_on_rw(struct pnfs_layout_segment *lseg) 633fb1084e3STom Haynes { 634fb1084e3STom Haynes return lseg->pls_range.iomode == IOMODE_RW && 635fb1084e3STom Haynes ff_layout_no_read_on_rw(lseg); 636fb1084e3STom Haynes } 637fb1084e3STom Haynes 638d67ae825STom Haynes module_param(dataserver_retrans, uint, 0644); 639d67ae825STom Haynes MODULE_PARM_DESC(dataserver_retrans, "The number of times the NFSv4.1 client " 640d67ae825STom Haynes "retries a request before it attempts further " 641d67ae825STom Haynes " recovery action."); 642d67ae825STom Haynes module_param(dataserver_timeo, uint, 0644); 643d67ae825STom Haynes MODULE_PARM_DESC(dataserver_timeo, "The time (in tenths of a second) the " 644d67ae825STom Haynes "NFSv4.1 client waits for a response from a " 645d67ae825STom Haynes " data server before it retries an NFS request."); 646