Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Device operations for the pnfs nfs4 file layout driver. |
| 3 | * |
| 4 | * Copyright (c) 2014, Primary Data, Inc. All rights reserved. |
| 5 | * |
| 6 | * Tao Peng <bergwolf@primarydata.com> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/nfs_fs.h> |
| 10 | #include <linux/vmalloc.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/sunrpc/addr.h> |
| 13 | |
| 14 | #include "../internal.h" |
| 15 | #include "../nfs4session.h" |
| 16 | #include "flexfilelayout.h" |
| 17 | |
| 18 | #define NFSDBG_FACILITY NFSDBG_PNFS_LD |
| 19 | |
| 20 | static unsigned int dataserver_timeo = NFS4_DEF_DS_TIMEO; |
| 21 | static unsigned int dataserver_retrans = NFS4_DEF_DS_RETRANS; |
| 22 | |
| 23 | void nfs4_ff_layout_put_deviceid(struct nfs4_ff_layout_ds *mirror_ds) |
| 24 | { |
| 25 | if (mirror_ds) |
| 26 | nfs4_put_deviceid_node(&mirror_ds->id_node); |
| 27 | } |
| 28 | |
| 29 | void nfs4_ff_layout_free_deviceid(struct nfs4_ff_layout_ds *mirror_ds) |
| 30 | { |
| 31 | nfs4_print_deviceid(&mirror_ds->id_node.deviceid); |
| 32 | nfs4_pnfs_ds_put(mirror_ds->ds); |
Trond Myklebust | 84a80f6 | 2015-03-09 15:23:35 -0400 | [diff] [blame] | 33 | kfree_rcu(mirror_ds, id_node.rcu); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /* Decode opaque device data and construct new_ds using it */ |
| 37 | struct nfs4_ff_layout_ds * |
| 38 | nfs4_ff_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev, |
| 39 | gfp_t gfp_flags) |
| 40 | { |
| 41 | struct xdr_stream stream; |
| 42 | struct xdr_buf buf; |
| 43 | struct page *scratch; |
| 44 | struct list_head dsaddrs; |
| 45 | struct nfs4_pnfs_ds_addr *da; |
| 46 | struct nfs4_ff_layout_ds *new_ds = NULL; |
| 47 | struct nfs4_ff_ds_version *ds_versions = NULL; |
| 48 | u32 mp_count; |
| 49 | u32 version_count; |
| 50 | __be32 *p; |
| 51 | int i, ret = -ENOMEM; |
| 52 | |
| 53 | /* set up xdr stream */ |
| 54 | scratch = alloc_page(gfp_flags); |
| 55 | if (!scratch) |
| 56 | goto out_err; |
| 57 | |
| 58 | new_ds = kzalloc(sizeof(struct nfs4_ff_layout_ds), gfp_flags); |
| 59 | if (!new_ds) |
| 60 | goto out_scratch; |
| 61 | |
| 62 | nfs4_init_deviceid_node(&new_ds->id_node, |
| 63 | server, |
| 64 | &pdev->dev_id); |
| 65 | INIT_LIST_HEAD(&dsaddrs); |
| 66 | |
| 67 | xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen); |
| 68 | xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); |
| 69 | |
| 70 | /* multipath count */ |
| 71 | p = xdr_inline_decode(&stream, 4); |
| 72 | if (unlikely(!p)) |
| 73 | goto out_err_drain_dsaddrs; |
| 74 | mp_count = be32_to_cpup(p); |
| 75 | dprintk("%s: multipath ds count %d\n", __func__, mp_count); |
| 76 | |
| 77 | for (i = 0; i < mp_count; i++) { |
| 78 | /* multipath ds */ |
| 79 | da = nfs4_decode_mp_ds_addr(server->nfs_client->cl_net, |
| 80 | &stream, gfp_flags); |
| 81 | if (da) |
| 82 | list_add_tail(&da->da_node, &dsaddrs); |
| 83 | } |
| 84 | if (list_empty(&dsaddrs)) { |
| 85 | dprintk("%s: no suitable DS addresses found\n", |
| 86 | __func__); |
| 87 | ret = -ENOMEDIUM; |
| 88 | goto out_err_drain_dsaddrs; |
| 89 | } |
| 90 | |
| 91 | /* version count */ |
| 92 | p = xdr_inline_decode(&stream, 4); |
| 93 | if (unlikely(!p)) |
| 94 | goto out_err_drain_dsaddrs; |
| 95 | version_count = be32_to_cpup(p); |
| 96 | dprintk("%s: version count %d\n", __func__, version_count); |
| 97 | |
| 98 | ds_versions = kzalloc(version_count * sizeof(struct nfs4_ff_ds_version), |
| 99 | gfp_flags); |
| 100 | if (!ds_versions) |
| 101 | goto out_scratch; |
| 102 | |
| 103 | for (i = 0; i < version_count; i++) { |
| 104 | /* 20 = version(4) + minor_version(4) + rsize(4) + wsize(4) + |
| 105 | * tightly_coupled(4) */ |
| 106 | p = xdr_inline_decode(&stream, 20); |
| 107 | if (unlikely(!p)) |
| 108 | goto out_err_drain_dsaddrs; |
| 109 | ds_versions[i].version = be32_to_cpup(p++); |
| 110 | ds_versions[i].minor_version = be32_to_cpup(p++); |
| 111 | ds_versions[i].rsize = nfs_block_size(be32_to_cpup(p++), NULL); |
| 112 | ds_versions[i].wsize = nfs_block_size(be32_to_cpup(p++), NULL); |
| 113 | ds_versions[i].tightly_coupled = be32_to_cpup(p); |
| 114 | |
| 115 | if (ds_versions[i].rsize > NFS_MAX_FILE_IO_SIZE) |
| 116 | ds_versions[i].rsize = NFS_MAX_FILE_IO_SIZE; |
| 117 | if (ds_versions[i].wsize > NFS_MAX_FILE_IO_SIZE) |
| 118 | ds_versions[i].wsize = NFS_MAX_FILE_IO_SIZE; |
| 119 | |
| 120 | if (ds_versions[i].version != 3 || ds_versions[i].minor_version != 0) { |
| 121 | dprintk("%s: [%d] unsupported ds version %d-%d\n", __func__, |
| 122 | i, ds_versions[i].version, |
| 123 | ds_versions[i].minor_version); |
| 124 | ret = -EPROTONOSUPPORT; |
| 125 | goto out_err_drain_dsaddrs; |
| 126 | } |
| 127 | |
| 128 | dprintk("%s: [%d] vers %u minor_ver %u rsize %u wsize %u coupled %d\n", |
| 129 | __func__, i, ds_versions[i].version, |
| 130 | ds_versions[i].minor_version, |
| 131 | ds_versions[i].rsize, |
| 132 | ds_versions[i].wsize, |
| 133 | ds_versions[i].tightly_coupled); |
| 134 | } |
| 135 | |
| 136 | new_ds->ds_versions = ds_versions; |
| 137 | new_ds->ds_versions_cnt = version_count; |
| 138 | |
| 139 | new_ds->ds = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags); |
| 140 | if (!new_ds->ds) |
| 141 | goto out_err_drain_dsaddrs; |
| 142 | |
| 143 | /* If DS was already in cache, free ds addrs */ |
| 144 | while (!list_empty(&dsaddrs)) { |
| 145 | da = list_first_entry(&dsaddrs, |
| 146 | struct nfs4_pnfs_ds_addr, |
| 147 | da_node); |
| 148 | list_del_init(&da->da_node); |
| 149 | kfree(da->da_remotestr); |
| 150 | kfree(da); |
| 151 | } |
| 152 | |
| 153 | __free_page(scratch); |
| 154 | return new_ds; |
| 155 | |
| 156 | out_err_drain_dsaddrs: |
| 157 | while (!list_empty(&dsaddrs)) { |
| 158 | da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr, |
| 159 | da_node); |
| 160 | list_del_init(&da->da_node); |
| 161 | kfree(da->da_remotestr); |
| 162 | kfree(da); |
| 163 | } |
| 164 | |
| 165 | kfree(ds_versions); |
| 166 | out_scratch: |
| 167 | __free_page(scratch); |
| 168 | out_err: |
| 169 | kfree(new_ds); |
| 170 | |
| 171 | dprintk("%s ERROR: returning %d\n", __func__, ret); |
| 172 | return NULL; |
| 173 | } |
| 174 | |
Trond Myklebust | 889d94d | 2015-09-01 00:58:24 -0700 | [diff] [blame] | 175 | static void ff_layout_mark_devid_invalid(struct pnfs_layout_segment *lseg, |
| 176 | struct nfs4_deviceid_node *devid) |
| 177 | { |
| 178 | nfs4_mark_deviceid_unavailable(devid); |
| 179 | if (!ff_layout_has_available_ds(lseg)) |
| 180 | pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, |
| 181 | lseg); |
| 182 | } |
| 183 | |
| 184 | static bool ff_layout_mirror_valid(struct pnfs_layout_segment *lseg, |
| 185 | struct nfs4_ff_layout_mirror *mirror) |
| 186 | { |
| 187 | if (mirror == NULL || mirror->mirror_ds == NULL) { |
| 188 | pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, |
| 189 | lseg); |
| 190 | return false; |
| 191 | } |
| 192 | if (mirror->mirror_ds->ds == NULL) { |
| 193 | struct nfs4_deviceid_node *devid; |
| 194 | devid = &mirror->mirror_ds->id_node; |
| 195 | ff_layout_mark_devid_invalid(lseg, devid); |
| 196 | return false; |
| 197 | } |
| 198 | return true; |
| 199 | } |
| 200 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 201 | static u64 |
| 202 | end_offset(u64 start, u64 len) |
| 203 | { |
| 204 | u64 end; |
| 205 | |
| 206 | end = start + len; |
| 207 | return end >= start ? end : NFS4_MAX_UINT64; |
| 208 | } |
| 209 | |
| 210 | static void extend_ds_error(struct nfs4_ff_layout_ds_err *err, |
| 211 | u64 offset, u64 length) |
| 212 | { |
| 213 | u64 end; |
| 214 | |
| 215 | end = max_t(u64, end_offset(err->offset, err->length), |
| 216 | end_offset(offset, length)); |
| 217 | err->offset = min_t(u64, err->offset, offset); |
| 218 | err->length = end - err->offset; |
| 219 | } |
| 220 | |
Trond Myklebust | b819ed4 | 2016-01-21 14:48:49 -0500 | [diff] [blame] | 221 | static int |
| 222 | ff_ds_error_match(const struct nfs4_ff_layout_ds_err *e1, |
| 223 | const struct nfs4_ff_layout_ds_err *e2) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 224 | { |
Trond Myklebust | b819ed4 | 2016-01-21 14:48:49 -0500 | [diff] [blame] | 225 | int ret; |
| 226 | |
| 227 | if (e1->opnum != e2->opnum) |
| 228 | return e1->opnum < e2->opnum ? -1 : 1; |
| 229 | if (e1->status != e2->status) |
| 230 | return e1->status < e2->status ? -1 : 1; |
| 231 | ret = memcmp(&e1->stateid, &e2->stateid, sizeof(e1->stateid)); |
| 232 | if (ret != 0) |
| 233 | return ret; |
| 234 | ret = memcmp(&e1->deviceid, &e2->deviceid, sizeof(e1->deviceid)); |
| 235 | if (ret != 0) |
| 236 | return ret; |
| 237 | if (end_offset(e1->offset, e1->length) < e2->offset) |
| 238 | return -1; |
| 239 | if (e1->offset > end_offset(e2->offset, e2->length)) |
| 240 | return 1; |
| 241 | /* If ranges overlap or are contiguous, they are the same */ |
| 242 | return 0; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 243 | } |
| 244 | |
Trond Myklebust | b819ed4 | 2016-01-21 14:48:49 -0500 | [diff] [blame] | 245 | static void |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 246 | ff_layout_add_ds_error_locked(struct nfs4_flexfile_layout *flo, |
| 247 | struct nfs4_ff_layout_ds_err *dserr) |
| 248 | { |
Trond Myklebust | b819ed4 | 2016-01-21 14:48:49 -0500 | [diff] [blame] | 249 | struct nfs4_ff_layout_ds_err *err, *tmp; |
| 250 | struct list_head *head = &flo->error_list; |
| 251 | int match; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 252 | |
Trond Myklebust | b819ed4 | 2016-01-21 14:48:49 -0500 | [diff] [blame] | 253 | /* Do insertion sort w/ merges */ |
| 254 | list_for_each_entry_safe(err, tmp, &flo->error_list, list) { |
| 255 | match = ff_ds_error_match(err, dserr); |
| 256 | if (match < 0) |
| 257 | continue; |
| 258 | if (match > 0) { |
| 259 | /* Add entry "dserr" _before_ entry "err" */ |
| 260 | head = &err->list; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 261 | break; |
| 262 | } |
Trond Myklebust | b819ed4 | 2016-01-21 14:48:49 -0500 | [diff] [blame] | 263 | /* Entries match, so merge "err" into "dserr" */ |
| 264 | extend_ds_error(dserr, err->offset, err->length); |
| 265 | list_del(&err->list); |
| 266 | kfree(err); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 267 | } |
| 268 | |
Trond Myklebust | b819ed4 | 2016-01-21 14:48:49 -0500 | [diff] [blame] | 269 | list_add_tail(&dserr->list, head); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | int ff_layout_track_ds_error(struct nfs4_flexfile_layout *flo, |
| 273 | struct nfs4_ff_layout_mirror *mirror, u64 offset, |
| 274 | u64 length, int status, enum nfs_opnum4 opnum, |
| 275 | gfp_t gfp_flags) |
| 276 | { |
| 277 | struct nfs4_ff_layout_ds_err *dserr; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 278 | |
| 279 | if (status == 0) |
| 280 | return 0; |
| 281 | |
| 282 | if (mirror->mirror_ds == NULL) |
| 283 | return -EINVAL; |
| 284 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 285 | dserr = kmalloc(sizeof(*dserr), gfp_flags); |
| 286 | if (!dserr) |
| 287 | return -ENOMEM; |
| 288 | |
| 289 | INIT_LIST_HEAD(&dserr->list); |
| 290 | dserr->offset = offset; |
| 291 | dserr->length = length; |
| 292 | dserr->status = status; |
| 293 | dserr->opnum = opnum; |
| 294 | nfs4_stateid_copy(&dserr->stateid, &mirror->stateid); |
| 295 | memcpy(&dserr->deviceid, &mirror->mirror_ds->id_node.deviceid, |
| 296 | NFS4_DEVICEID4_SIZE); |
| 297 | |
| 298 | spin_lock(&flo->generic_hdr.plh_inode->i_lock); |
Trond Myklebust | b819ed4 | 2016-01-21 14:48:49 -0500 | [diff] [blame] | 299 | ff_layout_add_ds_error_locked(flo, dserr); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 300 | spin_unlock(&flo->generic_hdr.plh_inode->i_lock); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | /* currently we only support AUTH_NONE and AUTH_SYS */ |
| 306 | static rpc_authflavor_t |
| 307 | nfs4_ff_layout_choose_authflavor(struct nfs4_ff_layout_mirror *mirror) |
| 308 | { |
| 309 | if (mirror->uid == (u32)-1) |
| 310 | return RPC_AUTH_NULL; |
| 311 | return RPC_AUTH_UNIX; |
| 312 | } |
| 313 | |
| 314 | /* fetch cred for NFSv3 DS */ |
| 315 | static int ff_layout_update_mirror_cred(struct nfs4_ff_layout_mirror *mirror, |
| 316 | struct nfs4_pnfs_ds *ds) |
| 317 | { |
| 318 | if (ds->ds_clp && !mirror->cred && |
| 319 | mirror->mirror_ds->ds_versions[0].version == 3) { |
| 320 | struct rpc_auth *auth = ds->ds_clp->cl_rpcclient->cl_auth; |
| 321 | struct rpc_cred *cred; |
| 322 | struct auth_cred acred = { |
| 323 | .uid = make_kuid(&init_user_ns, mirror->uid), |
| 324 | .gid = make_kgid(&init_user_ns, mirror->gid), |
| 325 | }; |
| 326 | |
| 327 | /* AUTH_NULL ignores acred */ |
| 328 | cred = auth->au_ops->lookup_cred(auth, &acred, 0); |
| 329 | if (IS_ERR(cred)) { |
| 330 | dprintk("%s: lookup_cred failed with %ld\n", |
| 331 | __func__, PTR_ERR(cred)); |
| 332 | return PTR_ERR(cred); |
| 333 | } else { |
Jeff Layton | a24221d | 2015-06-24 12:10:23 -0400 | [diff] [blame] | 334 | if (cmpxchg(&mirror->cred, NULL, cred)) |
| 335 | put_rpccred(cred); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | struct nfs_fh * |
| 342 | nfs4_ff_layout_select_ds_fh(struct pnfs_layout_segment *lseg, u32 mirror_idx) |
| 343 | { |
| 344 | struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, mirror_idx); |
| 345 | struct nfs_fh *fh = NULL; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 346 | |
Trond Myklebust | 889d94d | 2015-09-01 00:58:24 -0700 | [diff] [blame] | 347 | if (!ff_layout_mirror_valid(lseg, mirror)) { |
| 348 | pr_err_ratelimited("NFS: %s: No data server for mirror offset index %d\n", |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 349 | __func__, mirror_idx); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 350 | goto out; |
| 351 | } |
| 352 | |
| 353 | /* FIXME: For now assume there is only 1 version available for the DS */ |
| 354 | fh = &mirror->fh_versions[0]; |
| 355 | out: |
| 356 | return fh; |
| 357 | } |
| 358 | |
| 359 | /* Upon return, either ds is connected, or ds is NULL */ |
| 360 | struct nfs4_pnfs_ds * |
| 361 | nfs4_ff_layout_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx, |
| 362 | bool fail_return) |
| 363 | { |
| 364 | struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx); |
| 365 | struct nfs4_pnfs_ds *ds = NULL; |
| 366 | struct nfs4_deviceid_node *devid; |
| 367 | struct inode *ino = lseg->pls_layout->plh_inode; |
| 368 | struct nfs_server *s = NFS_SERVER(ino); |
| 369 | unsigned int max_payload; |
| 370 | rpc_authflavor_t flavor; |
| 371 | |
Trond Myklebust | 889d94d | 2015-09-01 00:58:24 -0700 | [diff] [blame] | 372 | if (!ff_layout_mirror_valid(lseg, mirror)) { |
| 373 | pr_err_ratelimited("NFS: %s: No data server for offset index %d\n", |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 374 | __func__, ds_idx); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 375 | goto out; |
| 376 | } |
| 377 | |
| 378 | devid = &mirror->mirror_ds->id_node; |
| 379 | if (ff_layout_test_devid_unavailable(devid)) |
| 380 | goto out; |
| 381 | |
| 382 | ds = mirror->mirror_ds->ds; |
| 383 | /* matching smp_wmb() in _nfs4_pnfs_v3/4_ds_connect */ |
| 384 | smp_rmb(); |
| 385 | if (ds->ds_clp) |
Jeff Layton | 0c8315d | 2015-06-24 12:10:24 -0400 | [diff] [blame] | 386 | goto out_update_creds; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 387 | |
| 388 | flavor = nfs4_ff_layout_choose_authflavor(mirror); |
| 389 | |
| 390 | /* FIXME: For now we assume the server sent only one version of NFS |
| 391 | * to use for the DS. |
| 392 | */ |
| 393 | nfs4_pnfs_ds_connect(s, ds, devid, dataserver_timeo, |
| 394 | dataserver_retrans, |
| 395 | mirror->mirror_ds->ds_versions[0].version, |
| 396 | mirror->mirror_ds->ds_versions[0].minor_version, |
| 397 | flavor); |
| 398 | |
| 399 | /* connect success, check rsize/wsize limit */ |
| 400 | if (ds->ds_clp) { |
| 401 | max_payload = |
| 402 | nfs_block_size(rpc_max_payload(ds->ds_clp->cl_rpcclient), |
| 403 | NULL); |
| 404 | if (mirror->mirror_ds->ds_versions[0].rsize > max_payload) |
| 405 | mirror->mirror_ds->ds_versions[0].rsize = max_payload; |
| 406 | if (mirror->mirror_ds->ds_versions[0].wsize > max_payload) |
| 407 | mirror->mirror_ds->ds_versions[0].wsize = max_payload; |
| 408 | } else { |
| 409 | ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout), |
| 410 | mirror, lseg->pls_range.offset, |
| 411 | lseg->pls_range.length, NFS4ERR_NXIO, |
| 412 | OP_ILLEGAL, GFP_NOIO); |
Trond Myklebust | 2e5b29f | 2015-12-14 16:25:11 -0500 | [diff] [blame] | 413 | if (!fail_return) { |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 414 | if (ff_layout_has_available_ds(lseg)) |
Trond Myklebust | 2370abd | 2016-01-27 20:32:50 -0500 | [diff] [blame] | 415 | set_bit(NFS_LAYOUT_RETURN_REQUESTED, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 416 | &lseg->pls_layout->plh_flags); |
Trond Myklebust | 2e5b29f | 2015-12-14 16:25:11 -0500 | [diff] [blame] | 417 | else |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 418 | pnfs_error_mark_layout_for_return(ino, lseg); |
Trond Myklebust | 2e5b29f | 2015-12-14 16:25:11 -0500 | [diff] [blame] | 419 | } else |
| 420 | pnfs_error_mark_layout_for_return(ino, lseg); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 421 | } |
Jeff Layton | 0c8315d | 2015-06-24 12:10:24 -0400 | [diff] [blame] | 422 | out_update_creds: |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 423 | if (ff_layout_update_mirror_cred(mirror, ds)) |
| 424 | ds = NULL; |
| 425 | out: |
| 426 | return ds; |
| 427 | } |
| 428 | |
| 429 | struct rpc_cred * |
| 430 | ff_layout_get_ds_cred(struct pnfs_layout_segment *lseg, u32 ds_idx, |
| 431 | struct rpc_cred *mdscred) |
| 432 | { |
| 433 | struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx); |
| 434 | struct rpc_cred *cred = ERR_PTR(-EINVAL); |
| 435 | |
| 436 | if (!nfs4_ff_layout_prepare_ds(lseg, ds_idx, true)) |
| 437 | goto out; |
| 438 | |
| 439 | if (mirror && mirror->cred) |
| 440 | cred = mirror->cred; |
| 441 | else |
| 442 | cred = mdscred; |
| 443 | out: |
| 444 | return cred; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Find or create a DS rpc client with th MDS server rpc client auth flavor |
| 449 | * in the nfs_client cl_ds_clients list. |
| 450 | */ |
| 451 | struct rpc_clnt * |
| 452 | nfs4_ff_find_or_create_ds_client(struct pnfs_layout_segment *lseg, u32 ds_idx, |
| 453 | struct nfs_client *ds_clp, struct inode *inode) |
| 454 | { |
| 455 | struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx); |
| 456 | |
| 457 | switch (mirror->mirror_ds->ds_versions[0].version) { |
| 458 | case 3: |
| 459 | /* For NFSv3 DS, flavor is set when creating DS connections */ |
| 460 | return ds_clp->cl_rpcclient; |
| 461 | case 4: |
| 462 | return nfs4_find_or_create_ds_client(ds_clp, inode); |
| 463 | default: |
| 464 | BUG(); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | static bool is_range_intersecting(u64 offset1, u64 length1, |
| 469 | u64 offset2, u64 length2) |
| 470 | { |
| 471 | u64 end1 = end_offset(offset1, length1); |
| 472 | u64 end2 = end_offset(offset2, length2); |
| 473 | |
| 474 | return (end1 == NFS4_MAX_UINT64 || end1 > offset2) && |
| 475 | (end2 == NFS4_MAX_UINT64 || end2 > offset1); |
| 476 | } |
| 477 | |
| 478 | /* called with inode i_lock held */ |
| 479 | int ff_layout_encode_ds_ioerr(struct nfs4_flexfile_layout *flo, |
| 480 | struct xdr_stream *xdr, int *count, |
| 481 | const struct pnfs_layout_range *range) |
| 482 | { |
| 483 | struct nfs4_ff_layout_ds_err *err, *n; |
| 484 | __be32 *p; |
| 485 | |
| 486 | list_for_each_entry_safe(err, n, &flo->error_list, list) { |
| 487 | if (!is_range_intersecting(err->offset, err->length, |
| 488 | range->offset, range->length)) |
| 489 | continue; |
| 490 | /* offset(8) + length(8) + stateid(NFS4_STATEID_SIZE) |
Trond Myklebust | d135490 | 2015-08-27 20:37:39 -0400 | [diff] [blame] | 491 | * + array length + deviceid(NFS4_DEVICEID4_SIZE) |
| 492 | * + status(4) + opnum(4) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 493 | */ |
| 494 | p = xdr_reserve_space(xdr, |
Trond Myklebust | d135490 | 2015-08-27 20:37:39 -0400 | [diff] [blame] | 495 | 28 + NFS4_STATEID_SIZE + NFS4_DEVICEID4_SIZE); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 496 | if (unlikely(!p)) |
| 497 | return -ENOBUFS; |
| 498 | p = xdr_encode_hyper(p, err->offset); |
| 499 | p = xdr_encode_hyper(p, err->length); |
| 500 | p = xdr_encode_opaque_fixed(p, &err->stateid, |
| 501 | NFS4_STATEID_SIZE); |
Trond Myklebust | d135490 | 2015-08-27 20:37:39 -0400 | [diff] [blame] | 502 | /* Encode 1 error */ |
| 503 | *p++ = cpu_to_be32(1); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 504 | p = xdr_encode_opaque_fixed(p, &err->deviceid, |
| 505 | NFS4_DEVICEID4_SIZE); |
| 506 | *p++ = cpu_to_be32(err->status); |
| 507 | *p++ = cpu_to_be32(err->opnum); |
| 508 | *count += 1; |
| 509 | list_del(&err->list); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 510 | dprintk("%s: offset %llu length %llu status %d op %d count %d\n", |
| 511 | __func__, err->offset, err->length, err->status, |
| 512 | err->opnum, *count); |
Tom Haynes | 480486b | 2015-02-09 17:48:32 -0800 | [diff] [blame] | 513 | kfree(err); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
Trond Myklebust | 81d6dc8 | 2015-09-01 02:49:44 -0700 | [diff] [blame] | 519 | static bool ff_read_layout_has_available_ds(struct pnfs_layout_segment *lseg) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 520 | { |
| 521 | struct nfs4_ff_layout_mirror *mirror; |
| 522 | struct nfs4_deviceid_node *devid; |
Trond Myklebust | 81d6dc8 | 2015-09-01 02:49:44 -0700 | [diff] [blame] | 523 | u32 idx; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 524 | |
| 525 | for (idx = 0; idx < FF_LAYOUT_MIRROR_COUNT(lseg); idx++) { |
| 526 | mirror = FF_LAYOUT_COMP(lseg, idx); |
| 527 | if (mirror && mirror->mirror_ds) { |
| 528 | devid = &mirror->mirror_ds->id_node; |
| 529 | if (!ff_layout_test_devid_unavailable(devid)) |
| 530 | return true; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return false; |
| 535 | } |
| 536 | |
Trond Myklebust | 81d6dc8 | 2015-09-01 02:49:44 -0700 | [diff] [blame] | 537 | static bool ff_rw_layout_has_available_ds(struct pnfs_layout_segment *lseg) |
| 538 | { |
| 539 | struct nfs4_ff_layout_mirror *mirror; |
| 540 | struct nfs4_deviceid_node *devid; |
| 541 | u32 idx; |
| 542 | |
| 543 | for (idx = 0; idx < FF_LAYOUT_MIRROR_COUNT(lseg); idx++) { |
| 544 | mirror = FF_LAYOUT_COMP(lseg, idx); |
| 545 | if (!mirror || !mirror->mirror_ds) |
| 546 | return false; |
| 547 | devid = &mirror->mirror_ds->id_node; |
| 548 | if (ff_layout_test_devid_unavailable(devid)) |
| 549 | return false; |
| 550 | } |
| 551 | |
| 552 | return FF_LAYOUT_MIRROR_COUNT(lseg) != 0; |
| 553 | } |
| 554 | |
| 555 | bool ff_layout_has_available_ds(struct pnfs_layout_segment *lseg) |
| 556 | { |
| 557 | if (lseg->pls_range.iomode == IOMODE_READ) |
| 558 | return ff_read_layout_has_available_ds(lseg); |
| 559 | /* Note: RW layout needs all mirrors available */ |
| 560 | return ff_rw_layout_has_available_ds(lseg); |
| 561 | } |
| 562 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 563 | module_param(dataserver_retrans, uint, 0644); |
| 564 | MODULE_PARM_DESC(dataserver_retrans, "The number of times the NFSv4.1 client " |
| 565 | "retries a request before it attempts further " |
| 566 | " recovery action."); |
| 567 | module_param(dataserver_timeo, uint, 0644); |
| 568 | MODULE_PARM_DESC(dataserver_timeo, "The time (in tenths of a second) the " |
| 569 | "NFSv4.1 client waits for a response from a " |
| 570 | " data server before it retries an NFS request."); |