1 /* 2 * Module for pnfs flexfile 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/nfs_page.h> 11 #include <linux/module.h> 12 13 #include <linux/sunrpc/metrics.h> 14 15 #include "flexfilelayout.h" 16 #include "../nfs4session.h" 17 #include "../nfs4idmap.h" 18 #include "../internal.h" 19 #include "../delegation.h" 20 #include "../nfs4trace.h" 21 #include "../iostat.h" 22 #include "../nfs.h" 23 #include "../nfs42.h" 24 25 #define NFSDBG_FACILITY NFSDBG_PNFS_LD 26 27 #define FF_LAYOUT_POLL_RETRY_MAX (15*HZ) 28 29 static struct group_info *ff_zero_group; 30 31 static struct pnfs_layout_hdr * 32 ff_layout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags) 33 { 34 struct nfs4_flexfile_layout *ffl; 35 36 ffl = kzalloc(sizeof(*ffl), gfp_flags); 37 if (ffl) { 38 INIT_LIST_HEAD(&ffl->error_list); 39 INIT_LIST_HEAD(&ffl->mirrors); 40 return &ffl->generic_hdr; 41 } else 42 return NULL; 43 } 44 45 static void 46 ff_layout_free_layout_hdr(struct pnfs_layout_hdr *lo) 47 { 48 struct nfs4_ff_layout_ds_err *err, *n; 49 50 list_for_each_entry_safe(err, n, &FF_LAYOUT_FROM_HDR(lo)->error_list, 51 list) { 52 list_del(&err->list); 53 kfree(err); 54 } 55 kfree(FF_LAYOUT_FROM_HDR(lo)); 56 } 57 58 static int decode_pnfs_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) 59 { 60 __be32 *p; 61 62 p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE); 63 if (unlikely(p == NULL)) 64 return -ENOBUFS; 65 stateid->type = NFS4_PNFS_DS_STATEID_TYPE; 66 memcpy(stateid->data, p, NFS4_STATEID_SIZE); 67 dprintk("%s: stateid id= [%x%x%x%x]\n", __func__, 68 p[0], p[1], p[2], p[3]); 69 return 0; 70 } 71 72 static int decode_deviceid(struct xdr_stream *xdr, struct nfs4_deviceid *devid) 73 { 74 __be32 *p; 75 76 p = xdr_inline_decode(xdr, NFS4_DEVICEID4_SIZE); 77 if (unlikely(!p)) 78 return -ENOBUFS; 79 memcpy(devid, p, NFS4_DEVICEID4_SIZE); 80 nfs4_print_deviceid(devid); 81 return 0; 82 } 83 84 static int decode_nfs_fh(struct xdr_stream *xdr, struct nfs_fh *fh) 85 { 86 __be32 *p; 87 88 p = xdr_inline_decode(xdr, 4); 89 if (unlikely(!p)) 90 return -ENOBUFS; 91 fh->size = be32_to_cpup(p++); 92 if (fh->size > sizeof(struct nfs_fh)) { 93 printk(KERN_ERR "NFS flexfiles: Too big fh received %d\n", 94 fh->size); 95 return -EOVERFLOW; 96 } 97 /* fh.data */ 98 p = xdr_inline_decode(xdr, fh->size); 99 if (unlikely(!p)) 100 return -ENOBUFS; 101 memcpy(&fh->data, p, fh->size); 102 dprintk("%s: fh len %d\n", __func__, fh->size); 103 104 return 0; 105 } 106 107 /* 108 * Currently only stringified uids and gids are accepted. 109 * I.e., kerberos is not supported to the DSes, so no pricipals. 110 * 111 * That means that one common function will suffice, but when 112 * principals are added, this should be split to accomodate 113 * calls to both nfs_map_name_to_uid() and nfs_map_group_to_gid(). 114 */ 115 static int 116 decode_name(struct xdr_stream *xdr, u32 *id) 117 { 118 __be32 *p; 119 int len; 120 121 /* opaque_length(4)*/ 122 p = xdr_inline_decode(xdr, 4); 123 if (unlikely(!p)) 124 return -ENOBUFS; 125 len = be32_to_cpup(p++); 126 if (len < 0) 127 return -EINVAL; 128 129 dprintk("%s: len %u\n", __func__, len); 130 131 /* opaque body */ 132 p = xdr_inline_decode(xdr, len); 133 if (unlikely(!p)) 134 return -ENOBUFS; 135 136 if (!nfs_map_string_to_numeric((char *)p, len, id)) 137 return -EINVAL; 138 139 return 0; 140 } 141 142 static bool ff_mirror_match_fh(const struct nfs4_ff_layout_mirror *m1, 143 const struct nfs4_ff_layout_mirror *m2) 144 { 145 int i, j; 146 147 if (m1->fh_versions_cnt != m2->fh_versions_cnt) 148 return false; 149 for (i = 0; i < m1->fh_versions_cnt; i++) { 150 bool found_fh = false; 151 for (j = 0; j < m2->fh_versions_cnt; j++) { 152 if (nfs_compare_fh(&m1->fh_versions[i], 153 &m2->fh_versions[j]) == 0) { 154 found_fh = true; 155 break; 156 } 157 } 158 if (!found_fh) 159 return false; 160 } 161 return true; 162 } 163 164 static struct nfs4_ff_layout_mirror * 165 ff_layout_add_mirror(struct pnfs_layout_hdr *lo, 166 struct nfs4_ff_layout_mirror *mirror) 167 { 168 struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo); 169 struct nfs4_ff_layout_mirror *pos; 170 struct inode *inode = lo->plh_inode; 171 172 spin_lock(&inode->i_lock); 173 list_for_each_entry(pos, &ff_layout->mirrors, mirrors) { 174 if (mirror->mirror_ds != pos->mirror_ds) 175 continue; 176 if (!ff_mirror_match_fh(mirror, pos)) 177 continue; 178 if (atomic_inc_not_zero(&pos->ref)) { 179 spin_unlock(&inode->i_lock); 180 return pos; 181 } 182 } 183 list_add(&mirror->mirrors, &ff_layout->mirrors); 184 mirror->layout = lo; 185 spin_unlock(&inode->i_lock); 186 return mirror; 187 } 188 189 static void 190 ff_layout_remove_mirror(struct nfs4_ff_layout_mirror *mirror) 191 { 192 struct inode *inode; 193 if (mirror->layout == NULL) 194 return; 195 inode = mirror->layout->plh_inode; 196 spin_lock(&inode->i_lock); 197 list_del(&mirror->mirrors); 198 spin_unlock(&inode->i_lock); 199 mirror->layout = NULL; 200 } 201 202 static struct nfs4_ff_layout_mirror *ff_layout_alloc_mirror(gfp_t gfp_flags) 203 { 204 struct nfs4_ff_layout_mirror *mirror; 205 206 mirror = kzalloc(sizeof(*mirror), gfp_flags); 207 if (mirror != NULL) { 208 spin_lock_init(&mirror->lock); 209 atomic_set(&mirror->ref, 1); 210 INIT_LIST_HEAD(&mirror->mirrors); 211 } 212 return mirror; 213 } 214 215 static void ff_layout_free_mirror(struct nfs4_ff_layout_mirror *mirror) 216 { 217 struct rpc_cred *cred; 218 219 ff_layout_remove_mirror(mirror); 220 kfree(mirror->fh_versions); 221 cred = rcu_access_pointer(mirror->ro_cred); 222 if (cred) 223 put_rpccred(cred); 224 cred = rcu_access_pointer(mirror->rw_cred); 225 if (cred) 226 put_rpccred(cred); 227 nfs4_ff_layout_put_deviceid(mirror->mirror_ds); 228 kfree(mirror); 229 } 230 231 static void ff_layout_put_mirror(struct nfs4_ff_layout_mirror *mirror) 232 { 233 if (mirror != NULL && atomic_dec_and_test(&mirror->ref)) 234 ff_layout_free_mirror(mirror); 235 } 236 237 static void ff_layout_free_mirror_array(struct nfs4_ff_layout_segment *fls) 238 { 239 int i; 240 241 if (fls->mirror_array) { 242 for (i = 0; i < fls->mirror_array_cnt; i++) { 243 /* normally mirror_ds is freed in 244 * .free_deviceid_node but we still do it here 245 * for .alloc_lseg error path */ 246 ff_layout_put_mirror(fls->mirror_array[i]); 247 } 248 kfree(fls->mirror_array); 249 fls->mirror_array = NULL; 250 } 251 } 252 253 static int ff_layout_check_layout(struct nfs4_layoutget_res *lgr) 254 { 255 int ret = 0; 256 257 dprintk("--> %s\n", __func__); 258 259 /* FIXME: remove this check when layout segment support is added */ 260 if (lgr->range.offset != 0 || 261 lgr->range.length != NFS4_MAX_UINT64) { 262 dprintk("%s Only whole file layouts supported. Use MDS i/o\n", 263 __func__); 264 ret = -EINVAL; 265 } 266 267 dprintk("--> %s returns %d\n", __func__, ret); 268 return ret; 269 } 270 271 static void _ff_layout_free_lseg(struct nfs4_ff_layout_segment *fls) 272 { 273 if (fls) { 274 ff_layout_free_mirror_array(fls); 275 kfree(fls); 276 } 277 } 278 279 static bool 280 ff_lseg_range_is_after(const struct pnfs_layout_range *l1, 281 const struct pnfs_layout_range *l2) 282 { 283 u64 end1, end2; 284 285 if (l1->iomode != l2->iomode) 286 return l1->iomode != IOMODE_READ; 287 end1 = pnfs_calc_offset_end(l1->offset, l1->length); 288 end2 = pnfs_calc_offset_end(l2->offset, l2->length); 289 if (end1 < l2->offset) 290 return false; 291 if (end2 < l1->offset) 292 return true; 293 return l2->offset <= l1->offset; 294 } 295 296 static bool 297 ff_lseg_merge(struct pnfs_layout_segment *new, 298 struct pnfs_layout_segment *old) 299 { 300 u64 new_end, old_end; 301 302 if (test_bit(NFS_LSEG_LAYOUTRETURN, &old->pls_flags)) 303 return false; 304 if (new->pls_range.iomode != old->pls_range.iomode) 305 return false; 306 old_end = pnfs_calc_offset_end(old->pls_range.offset, 307 old->pls_range.length); 308 if (old_end < new->pls_range.offset) 309 return false; 310 new_end = pnfs_calc_offset_end(new->pls_range.offset, 311 new->pls_range.length); 312 if (new_end < old->pls_range.offset) 313 return false; 314 315 /* Mergeable: copy info from 'old' to 'new' */ 316 if (new_end < old_end) 317 new_end = old_end; 318 if (new->pls_range.offset < old->pls_range.offset) 319 new->pls_range.offset = old->pls_range.offset; 320 new->pls_range.length = pnfs_calc_offset_length(new->pls_range.offset, 321 new_end); 322 if (test_bit(NFS_LSEG_ROC, &old->pls_flags)) 323 set_bit(NFS_LSEG_ROC, &new->pls_flags); 324 return true; 325 } 326 327 static void 328 ff_layout_add_lseg(struct pnfs_layout_hdr *lo, 329 struct pnfs_layout_segment *lseg, 330 struct list_head *free_me) 331 { 332 pnfs_generic_layout_insert_lseg(lo, lseg, 333 ff_lseg_range_is_after, 334 ff_lseg_merge, 335 free_me); 336 } 337 338 static void ff_layout_sort_mirrors(struct nfs4_ff_layout_segment *fls) 339 { 340 int i, j; 341 342 for (i = 0; i < fls->mirror_array_cnt - 1; i++) { 343 for (j = i + 1; j < fls->mirror_array_cnt; j++) 344 if (fls->mirror_array[i]->efficiency < 345 fls->mirror_array[j]->efficiency) 346 swap(fls->mirror_array[i], 347 fls->mirror_array[j]); 348 } 349 } 350 351 static void ff_layout_mark_devices_valid(struct nfs4_ff_layout_segment *fls) 352 { 353 struct nfs4_deviceid_node *node; 354 int i; 355 356 if (!(fls->flags & FF_FLAGS_NO_IO_THRU_MDS)) 357 return; 358 for (i = 0; i < fls->mirror_array_cnt; i++) { 359 node = &fls->mirror_array[i]->mirror_ds->id_node; 360 clear_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags); 361 } 362 } 363 364 static struct pnfs_layout_segment * 365 ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh, 366 struct nfs4_layoutget_res *lgr, 367 gfp_t gfp_flags) 368 { 369 struct pnfs_layout_segment *ret; 370 struct nfs4_ff_layout_segment *fls = NULL; 371 struct xdr_stream stream; 372 struct xdr_buf buf; 373 struct page *scratch; 374 u64 stripe_unit; 375 u32 mirror_array_cnt; 376 __be32 *p; 377 int i, rc; 378 379 dprintk("--> %s\n", __func__); 380 scratch = alloc_page(gfp_flags); 381 if (!scratch) 382 return ERR_PTR(-ENOMEM); 383 384 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, 385 lgr->layoutp->len); 386 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); 387 388 /* stripe unit and mirror_array_cnt */ 389 rc = -EIO; 390 p = xdr_inline_decode(&stream, 8 + 4); 391 if (!p) 392 goto out_err_free; 393 394 p = xdr_decode_hyper(p, &stripe_unit); 395 mirror_array_cnt = be32_to_cpup(p++); 396 dprintk("%s: stripe_unit=%llu mirror_array_cnt=%u\n", __func__, 397 stripe_unit, mirror_array_cnt); 398 399 if (mirror_array_cnt > NFS4_FLEXFILE_LAYOUT_MAX_MIRROR_CNT || 400 mirror_array_cnt == 0) 401 goto out_err_free; 402 403 rc = -ENOMEM; 404 fls = kzalloc(sizeof(*fls), gfp_flags); 405 if (!fls) 406 goto out_err_free; 407 408 fls->mirror_array_cnt = mirror_array_cnt; 409 fls->stripe_unit = stripe_unit; 410 fls->mirror_array = kcalloc(fls->mirror_array_cnt, 411 sizeof(fls->mirror_array[0]), gfp_flags); 412 if (fls->mirror_array == NULL) 413 goto out_err_free; 414 415 for (i = 0; i < fls->mirror_array_cnt; i++) { 416 struct nfs4_ff_layout_mirror *mirror; 417 struct nfs4_deviceid devid; 418 struct nfs4_deviceid_node *idnode; 419 struct auth_cred acred = { .group_info = ff_zero_group }; 420 struct rpc_cred __rcu *cred; 421 u32 ds_count, fh_count, id; 422 int j; 423 424 rc = -EIO; 425 p = xdr_inline_decode(&stream, 4); 426 if (!p) 427 goto out_err_free; 428 ds_count = be32_to_cpup(p); 429 430 /* FIXME: allow for striping? */ 431 if (ds_count != 1) 432 goto out_err_free; 433 434 fls->mirror_array[i] = ff_layout_alloc_mirror(gfp_flags); 435 if (fls->mirror_array[i] == NULL) { 436 rc = -ENOMEM; 437 goto out_err_free; 438 } 439 440 fls->mirror_array[i]->ds_count = ds_count; 441 442 /* deviceid */ 443 rc = decode_deviceid(&stream, &devid); 444 if (rc) 445 goto out_err_free; 446 447 idnode = nfs4_find_get_deviceid(NFS_SERVER(lh->plh_inode), 448 &devid, lh->plh_lc_cred, 449 gfp_flags); 450 /* 451 * upon success, mirror_ds is allocated by previous 452 * getdeviceinfo, or newly by .alloc_deviceid_node 453 * nfs4_find_get_deviceid failure is indeed getdeviceinfo falure 454 */ 455 if (idnode) 456 fls->mirror_array[i]->mirror_ds = 457 FF_LAYOUT_MIRROR_DS(idnode); 458 else 459 goto out_err_free; 460 461 /* efficiency */ 462 rc = -EIO; 463 p = xdr_inline_decode(&stream, 4); 464 if (!p) 465 goto out_err_free; 466 fls->mirror_array[i]->efficiency = be32_to_cpup(p); 467 468 /* stateid */ 469 rc = decode_pnfs_stateid(&stream, &fls->mirror_array[i]->stateid); 470 if (rc) 471 goto out_err_free; 472 473 /* fh */ 474 p = xdr_inline_decode(&stream, 4); 475 if (!p) 476 goto out_err_free; 477 fh_count = be32_to_cpup(p); 478 479 fls->mirror_array[i]->fh_versions = 480 kzalloc(fh_count * sizeof(struct nfs_fh), 481 gfp_flags); 482 if (fls->mirror_array[i]->fh_versions == NULL) { 483 rc = -ENOMEM; 484 goto out_err_free; 485 } 486 487 for (j = 0; j < fh_count; j++) { 488 rc = decode_nfs_fh(&stream, 489 &fls->mirror_array[i]->fh_versions[j]); 490 if (rc) 491 goto out_err_free; 492 } 493 494 fls->mirror_array[i]->fh_versions_cnt = fh_count; 495 496 /* user */ 497 rc = decode_name(&stream, &id); 498 if (rc) 499 goto out_err_free; 500 501 acred.uid = make_kuid(&init_user_ns, id); 502 503 /* group */ 504 rc = decode_name(&stream, &id); 505 if (rc) 506 goto out_err_free; 507 508 acred.gid = make_kgid(&init_user_ns, id); 509 510 /* find the cred for it */ 511 rcu_assign_pointer(cred, rpc_lookup_generic_cred(&acred, 0, gfp_flags)); 512 if (IS_ERR(cred)) { 513 rc = PTR_ERR(cred); 514 goto out_err_free; 515 } 516 517 if (lgr->range.iomode == IOMODE_READ) 518 rcu_assign_pointer(fls->mirror_array[i]->ro_cred, cred); 519 else 520 rcu_assign_pointer(fls->mirror_array[i]->rw_cred, cred); 521 522 mirror = ff_layout_add_mirror(lh, fls->mirror_array[i]); 523 if (mirror != fls->mirror_array[i]) { 524 /* swap cred ptrs so free_mirror will clean up old */ 525 if (lgr->range.iomode == IOMODE_READ) { 526 cred = xchg(&mirror->ro_cred, cred); 527 rcu_assign_pointer(fls->mirror_array[i]->ro_cred, cred); 528 } else { 529 cred = xchg(&mirror->rw_cred, cred); 530 rcu_assign_pointer(fls->mirror_array[i]->rw_cred, cred); 531 } 532 ff_layout_free_mirror(fls->mirror_array[i]); 533 fls->mirror_array[i] = mirror; 534 } 535 536 dprintk("%s: iomode %s uid %u gid %u\n", __func__, 537 lgr->range.iomode == IOMODE_READ ? "READ" : "RW", 538 from_kuid(&init_user_ns, acred.uid), 539 from_kgid(&init_user_ns, acred.gid)); 540 } 541 542 p = xdr_inline_decode(&stream, 4); 543 if (!p) 544 goto out_sort_mirrors; 545 fls->flags = be32_to_cpup(p); 546 547 p = xdr_inline_decode(&stream, 4); 548 if (!p) 549 goto out_sort_mirrors; 550 for (i=0; i < fls->mirror_array_cnt; i++) 551 fls->mirror_array[i]->report_interval = be32_to_cpup(p); 552 553 out_sort_mirrors: 554 ff_layout_sort_mirrors(fls); 555 rc = ff_layout_check_layout(lgr); 556 if (rc) 557 goto out_err_free; 558 ff_layout_mark_devices_valid(fls); 559 560 ret = &fls->generic_hdr; 561 dprintk("<-- %s (success)\n", __func__); 562 out_free_page: 563 __free_page(scratch); 564 return ret; 565 out_err_free: 566 _ff_layout_free_lseg(fls); 567 ret = ERR_PTR(rc); 568 dprintk("<-- %s (%d)\n", __func__, rc); 569 goto out_free_page; 570 } 571 572 static bool ff_layout_has_rw_segments(struct pnfs_layout_hdr *layout) 573 { 574 struct pnfs_layout_segment *lseg; 575 576 list_for_each_entry(lseg, &layout->plh_segs, pls_list) 577 if (lseg->pls_range.iomode == IOMODE_RW) 578 return true; 579 580 return false; 581 } 582 583 static void 584 ff_layout_free_lseg(struct pnfs_layout_segment *lseg) 585 { 586 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg); 587 588 dprintk("--> %s\n", __func__); 589 590 if (lseg->pls_range.iomode == IOMODE_RW) { 591 struct nfs4_flexfile_layout *ffl; 592 struct inode *inode; 593 594 ffl = FF_LAYOUT_FROM_HDR(lseg->pls_layout); 595 inode = ffl->generic_hdr.plh_inode; 596 spin_lock(&inode->i_lock); 597 if (!ff_layout_has_rw_segments(lseg->pls_layout)) { 598 ffl->commit_info.nbuckets = 0; 599 kfree(ffl->commit_info.buckets); 600 ffl->commit_info.buckets = NULL; 601 } 602 spin_unlock(&inode->i_lock); 603 } 604 _ff_layout_free_lseg(fls); 605 } 606 607 /* Return 1 until we have multiple lsegs support */ 608 static int 609 ff_layout_get_lseg_count(struct nfs4_ff_layout_segment *fls) 610 { 611 return 1; 612 } 613 614 static void 615 nfs4_ff_start_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now) 616 { 617 /* first IO request? */ 618 if (atomic_inc_return(&timer->n_ops) == 1) { 619 timer->start_time = now; 620 } 621 } 622 623 static ktime_t 624 nfs4_ff_end_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now) 625 { 626 ktime_t start; 627 628 if (atomic_dec_return(&timer->n_ops) < 0) 629 WARN_ON_ONCE(1); 630 631 start = timer->start_time; 632 timer->start_time = now; 633 return ktime_sub(now, start); 634 } 635 636 static bool 637 nfs4_ff_layoutstat_start_io(struct nfs4_ff_layout_mirror *mirror, 638 struct nfs4_ff_layoutstat *layoutstat, 639 ktime_t now) 640 { 641 static const ktime_t notime = {0}; 642 s64 report_interval = FF_LAYOUTSTATS_REPORT_INTERVAL; 643 644 nfs4_ff_start_busy_timer(&layoutstat->busy_timer, now); 645 if (ktime_equal(mirror->start_time, notime)) 646 mirror->start_time = now; 647 if (ktime_equal(mirror->last_report_time, notime)) 648 mirror->last_report_time = now; 649 if (mirror->report_interval != 0) 650 report_interval = (s64)mirror->report_interval * 1000LL; 651 else if (layoutstats_timer != 0) 652 report_interval = (s64)layoutstats_timer * 1000LL; 653 if (ktime_to_ms(ktime_sub(now, mirror->last_report_time)) >= 654 report_interval) { 655 mirror->last_report_time = now; 656 return true; 657 } 658 659 return false; 660 } 661 662 static void 663 nfs4_ff_layout_stat_io_update_requested(struct nfs4_ff_layoutstat *layoutstat, 664 __u64 requested) 665 { 666 struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat; 667 668 iostat->ops_requested++; 669 iostat->bytes_requested += requested; 670 } 671 672 static void 673 nfs4_ff_layout_stat_io_update_completed(struct nfs4_ff_layoutstat *layoutstat, 674 __u64 requested, 675 __u64 completed, 676 ktime_t time_completed, 677 ktime_t time_started) 678 { 679 struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat; 680 ktime_t completion_time = ktime_sub(time_completed, time_started); 681 ktime_t timer; 682 683 iostat->ops_completed++; 684 iostat->bytes_completed += completed; 685 iostat->bytes_not_delivered += requested - completed; 686 687 timer = nfs4_ff_end_busy_timer(&layoutstat->busy_timer, time_completed); 688 iostat->total_busy_time = 689 ktime_add(iostat->total_busy_time, timer); 690 iostat->aggregate_completion_time = 691 ktime_add(iostat->aggregate_completion_time, 692 completion_time); 693 } 694 695 static void 696 nfs4_ff_layout_stat_io_start_read(struct inode *inode, 697 struct nfs4_ff_layout_mirror *mirror, 698 __u64 requested, ktime_t now) 699 { 700 bool report; 701 702 spin_lock(&mirror->lock); 703 report = nfs4_ff_layoutstat_start_io(mirror, &mirror->read_stat, now); 704 nfs4_ff_layout_stat_io_update_requested(&mirror->read_stat, requested); 705 spin_unlock(&mirror->lock); 706 707 if (report) 708 pnfs_report_layoutstat(inode, GFP_KERNEL); 709 } 710 711 static void 712 nfs4_ff_layout_stat_io_end_read(struct rpc_task *task, 713 struct nfs4_ff_layout_mirror *mirror, 714 __u64 requested, 715 __u64 completed) 716 { 717 spin_lock(&mirror->lock); 718 nfs4_ff_layout_stat_io_update_completed(&mirror->read_stat, 719 requested, completed, 720 ktime_get(), task->tk_start); 721 spin_unlock(&mirror->lock); 722 } 723 724 static void 725 nfs4_ff_layout_stat_io_start_write(struct inode *inode, 726 struct nfs4_ff_layout_mirror *mirror, 727 __u64 requested, ktime_t now) 728 { 729 bool report; 730 731 spin_lock(&mirror->lock); 732 report = nfs4_ff_layoutstat_start_io(mirror , &mirror->write_stat, now); 733 nfs4_ff_layout_stat_io_update_requested(&mirror->write_stat, requested); 734 spin_unlock(&mirror->lock); 735 736 if (report) 737 pnfs_report_layoutstat(inode, GFP_NOIO); 738 } 739 740 static void 741 nfs4_ff_layout_stat_io_end_write(struct rpc_task *task, 742 struct nfs4_ff_layout_mirror *mirror, 743 __u64 requested, 744 __u64 completed, 745 enum nfs3_stable_how committed) 746 { 747 if (committed == NFS_UNSTABLE) 748 requested = completed = 0; 749 750 spin_lock(&mirror->lock); 751 nfs4_ff_layout_stat_io_update_completed(&mirror->write_stat, 752 requested, completed, ktime_get(), task->tk_start); 753 spin_unlock(&mirror->lock); 754 } 755 756 static int 757 ff_layout_alloc_commit_info(struct pnfs_layout_segment *lseg, 758 struct nfs_commit_info *cinfo, 759 gfp_t gfp_flags) 760 { 761 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg); 762 struct pnfs_commit_bucket *buckets; 763 int size; 764 765 if (cinfo->ds->nbuckets != 0) { 766 /* This assumes there is only one RW lseg per file. 767 * To support multiple lseg per file, we need to 768 * change struct pnfs_commit_bucket to allow dynamic 769 * increasing nbuckets. 770 */ 771 return 0; 772 } 773 774 size = ff_layout_get_lseg_count(fls) * FF_LAYOUT_MIRROR_COUNT(lseg); 775 776 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket), 777 gfp_flags); 778 if (!buckets) 779 return -ENOMEM; 780 else { 781 int i; 782 783 spin_lock(&cinfo->inode->i_lock); 784 if (cinfo->ds->nbuckets != 0) 785 kfree(buckets); 786 else { 787 cinfo->ds->buckets = buckets; 788 cinfo->ds->nbuckets = size; 789 for (i = 0; i < size; i++) { 790 INIT_LIST_HEAD(&buckets[i].written); 791 INIT_LIST_HEAD(&buckets[i].committing); 792 /* mark direct verifier as unset */ 793 buckets[i].direct_verf.committed = 794 NFS_INVALID_STABLE_HOW; 795 } 796 } 797 spin_unlock(&cinfo->inode->i_lock); 798 return 0; 799 } 800 } 801 802 static struct nfs4_pnfs_ds * 803 ff_layout_choose_best_ds_for_read(struct pnfs_layout_segment *lseg, 804 int start_idx, 805 int *best_idx) 806 { 807 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg); 808 struct nfs4_pnfs_ds *ds; 809 int idx; 810 811 /* mirrors are sorted by efficiency */ 812 for (idx = start_idx; idx < fls->mirror_array_cnt; idx++) { 813 ds = nfs4_ff_layout_prepare_ds(lseg, idx, false); 814 if (ds) { 815 *best_idx = idx; 816 return ds; 817 } 818 } 819 820 return NULL; 821 } 822 823 static void 824 ff_layout_pg_get_read(struct nfs_pageio_descriptor *pgio, 825 struct nfs_page *req, 826 bool strict_iomode) 827 { 828 retry_strict: 829 pnfs_put_lseg(pgio->pg_lseg); 830 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, 831 req->wb_context, 832 0, 833 NFS4_MAX_UINT64, 834 IOMODE_READ, 835 strict_iomode, 836 GFP_KERNEL); 837 if (IS_ERR(pgio->pg_lseg)) { 838 pgio->pg_error = PTR_ERR(pgio->pg_lseg); 839 pgio->pg_lseg = NULL; 840 } 841 842 /* If we don't have checking, do get a IOMODE_RW 843 * segment, and the server wants to avoid READs 844 * there, then retry! 845 */ 846 if (pgio->pg_lseg && !strict_iomode && 847 ff_layout_avoid_read_on_rw(pgio->pg_lseg)) { 848 strict_iomode = true; 849 goto retry_strict; 850 } 851 } 852 853 static void 854 ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio, 855 struct nfs_page *req) 856 { 857 struct nfs_pgio_mirror *pgm; 858 struct nfs4_ff_layout_mirror *mirror; 859 struct nfs4_pnfs_ds *ds; 860 int ds_idx; 861 862 /* Use full layout for now */ 863 if (!pgio->pg_lseg) 864 ff_layout_pg_get_read(pgio, req, false); 865 else if (ff_layout_avoid_read_on_rw(pgio->pg_lseg)) 866 ff_layout_pg_get_read(pgio, req, true); 867 868 /* If no lseg, fall back to read through mds */ 869 if (pgio->pg_lseg == NULL) 870 goto out_mds; 871 872 ds = ff_layout_choose_best_ds_for_read(pgio->pg_lseg, 0, &ds_idx); 873 if (!ds) { 874 if (ff_layout_no_fallback_to_mds(pgio->pg_lseg)) 875 goto out_pnfs; 876 else 877 goto out_mds; 878 } 879 880 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx); 881 882 pgio->pg_mirror_idx = ds_idx; 883 884 /* read always uses only one mirror - idx 0 for pgio layer */ 885 pgm = &pgio->pg_mirrors[0]; 886 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize; 887 888 return; 889 out_mds: 890 pnfs_put_lseg(pgio->pg_lseg); 891 pgio->pg_lseg = NULL; 892 nfs_pageio_reset_read_mds(pgio); 893 return; 894 895 out_pnfs: 896 pnfs_set_lo_fail(pgio->pg_lseg); 897 pnfs_put_lseg(pgio->pg_lseg); 898 pgio->pg_lseg = NULL; 899 } 900 901 static void 902 ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio, 903 struct nfs_page *req) 904 { 905 struct nfs4_ff_layout_mirror *mirror; 906 struct nfs_pgio_mirror *pgm; 907 struct nfs_commit_info cinfo; 908 struct nfs4_pnfs_ds *ds; 909 int i; 910 int status; 911 912 if (!pgio->pg_lseg) { 913 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, 914 req->wb_context, 915 0, 916 NFS4_MAX_UINT64, 917 IOMODE_RW, 918 false, 919 GFP_NOFS); 920 if (IS_ERR(pgio->pg_lseg)) { 921 pgio->pg_error = PTR_ERR(pgio->pg_lseg); 922 pgio->pg_lseg = NULL; 923 return; 924 } 925 } 926 /* If no lseg, fall back to write through mds */ 927 if (pgio->pg_lseg == NULL) 928 goto out_mds; 929 930 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq); 931 status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS); 932 if (status < 0) 933 goto out_mds; 934 935 /* Use a direct mapping of ds_idx to pgio mirror_idx */ 936 if (WARN_ON_ONCE(pgio->pg_mirror_count != 937 FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg))) 938 goto out_mds; 939 940 for (i = 0; i < pgio->pg_mirror_count; i++) { 941 ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, i, true); 942 if (!ds) { 943 if (ff_layout_no_fallback_to_mds(pgio->pg_lseg)) 944 goto out_pnfs; 945 else 946 goto out_mds; 947 } 948 pgm = &pgio->pg_mirrors[i]; 949 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i); 950 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize; 951 } 952 953 return; 954 955 out_mds: 956 pnfs_put_lseg(pgio->pg_lseg); 957 pgio->pg_lseg = NULL; 958 nfs_pageio_reset_write_mds(pgio); 959 return; 960 961 out_pnfs: 962 pnfs_set_lo_fail(pgio->pg_lseg); 963 pnfs_put_lseg(pgio->pg_lseg); 964 pgio->pg_lseg = NULL; 965 } 966 967 static unsigned int 968 ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio, 969 struct nfs_page *req) 970 { 971 if (!pgio->pg_lseg) { 972 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, 973 req->wb_context, 974 0, 975 NFS4_MAX_UINT64, 976 IOMODE_RW, 977 false, 978 GFP_NOFS); 979 if (IS_ERR(pgio->pg_lseg)) { 980 pgio->pg_error = PTR_ERR(pgio->pg_lseg); 981 pgio->pg_lseg = NULL; 982 goto out; 983 } 984 } 985 if (pgio->pg_lseg) 986 return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg); 987 988 /* no lseg means that pnfs is not in use, so no mirroring here */ 989 nfs_pageio_reset_write_mds(pgio); 990 out: 991 return 1; 992 } 993 994 static const struct nfs_pageio_ops ff_layout_pg_read_ops = { 995 .pg_init = ff_layout_pg_init_read, 996 .pg_test = pnfs_generic_pg_test, 997 .pg_doio = pnfs_generic_pg_readpages, 998 .pg_cleanup = pnfs_generic_pg_cleanup, 999 }; 1000 1001 static const struct nfs_pageio_ops ff_layout_pg_write_ops = { 1002 .pg_init = ff_layout_pg_init_write, 1003 .pg_test = pnfs_generic_pg_test, 1004 .pg_doio = pnfs_generic_pg_writepages, 1005 .pg_get_mirror_count = ff_layout_pg_get_mirror_count_write, 1006 .pg_cleanup = pnfs_generic_pg_cleanup, 1007 }; 1008 1009 static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs) 1010 { 1011 struct rpc_task *task = &hdr->task; 1012 1013 pnfs_layoutcommit_inode(hdr->inode, false); 1014 1015 if (retry_pnfs) { 1016 dprintk("%s Reset task %5u for i/o through pNFS " 1017 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__, 1018 hdr->task.tk_pid, 1019 hdr->inode->i_sb->s_id, 1020 (unsigned long long)NFS_FILEID(hdr->inode), 1021 hdr->args.count, 1022 (unsigned long long)hdr->args.offset); 1023 1024 hdr->completion_ops->reschedule_io(hdr); 1025 return; 1026 } 1027 1028 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { 1029 dprintk("%s Reset task %5u for i/o through MDS " 1030 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__, 1031 hdr->task.tk_pid, 1032 hdr->inode->i_sb->s_id, 1033 (unsigned long long)NFS_FILEID(hdr->inode), 1034 hdr->args.count, 1035 (unsigned long long)hdr->args.offset); 1036 1037 task->tk_status = pnfs_write_done_resend_to_mds(hdr); 1038 } 1039 } 1040 1041 static void ff_layout_reset_read(struct nfs_pgio_header *hdr) 1042 { 1043 struct rpc_task *task = &hdr->task; 1044 1045 pnfs_layoutcommit_inode(hdr->inode, false); 1046 1047 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { 1048 dprintk("%s Reset task %5u for i/o through MDS " 1049 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__, 1050 hdr->task.tk_pid, 1051 hdr->inode->i_sb->s_id, 1052 (unsigned long long)NFS_FILEID(hdr->inode), 1053 hdr->args.count, 1054 (unsigned long long)hdr->args.offset); 1055 1056 task->tk_status = pnfs_read_done_resend_to_mds(hdr); 1057 } 1058 } 1059 1060 static int ff_layout_async_handle_error_v4(struct rpc_task *task, 1061 struct nfs4_state *state, 1062 struct nfs_client *clp, 1063 struct pnfs_layout_segment *lseg, 1064 int idx) 1065 { 1066 struct pnfs_layout_hdr *lo = lseg->pls_layout; 1067 struct inode *inode = lo->plh_inode; 1068 struct nfs_server *mds_server = NFS_SERVER(inode); 1069 1070 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx); 1071 struct nfs_client *mds_client = mds_server->nfs_client; 1072 struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table; 1073 1074 if (task->tk_status >= 0) 1075 return 0; 1076 1077 switch (task->tk_status) { 1078 /* MDS state errors */ 1079 case -NFS4ERR_DELEG_REVOKED: 1080 case -NFS4ERR_ADMIN_REVOKED: 1081 case -NFS4ERR_BAD_STATEID: 1082 if (state == NULL) 1083 break; 1084 nfs_remove_bad_delegation(state->inode); 1085 case -NFS4ERR_OPENMODE: 1086 if (state == NULL) 1087 break; 1088 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0) 1089 goto out_bad_stateid; 1090 goto wait_on_recovery; 1091 case -NFS4ERR_EXPIRED: 1092 if (state != NULL) { 1093 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0) 1094 goto out_bad_stateid; 1095 } 1096 nfs4_schedule_lease_recovery(mds_client); 1097 goto wait_on_recovery; 1098 /* DS session errors */ 1099 case -NFS4ERR_BADSESSION: 1100 case -NFS4ERR_BADSLOT: 1101 case -NFS4ERR_BAD_HIGH_SLOT: 1102 case -NFS4ERR_DEADSESSION: 1103 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 1104 case -NFS4ERR_SEQ_FALSE_RETRY: 1105 case -NFS4ERR_SEQ_MISORDERED: 1106 dprintk("%s ERROR %d, Reset session. Exchangeid " 1107 "flags 0x%x\n", __func__, task->tk_status, 1108 clp->cl_exchange_flags); 1109 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status); 1110 break; 1111 case -NFS4ERR_DELAY: 1112 case -NFS4ERR_GRACE: 1113 rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX); 1114 break; 1115 case -NFS4ERR_RETRY_UNCACHED_REP: 1116 break; 1117 /* Invalidate Layout errors */ 1118 case -NFS4ERR_PNFS_NO_LAYOUT: 1119 case -ESTALE: /* mapped NFS4ERR_STALE */ 1120 case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */ 1121 case -EISDIR: /* mapped NFS4ERR_ISDIR */ 1122 case -NFS4ERR_FHEXPIRED: 1123 case -NFS4ERR_WRONG_TYPE: 1124 dprintk("%s Invalid layout error %d\n", __func__, 1125 task->tk_status); 1126 /* 1127 * Destroy layout so new i/o will get a new layout. 1128 * Layout will not be destroyed until all current lseg 1129 * references are put. Mark layout as invalid to resend failed 1130 * i/o and all i/o waiting on the slot table to the MDS until 1131 * layout is destroyed and a new valid layout is obtained. 1132 */ 1133 pnfs_destroy_layout(NFS_I(inode)); 1134 rpc_wake_up(&tbl->slot_tbl_waitq); 1135 goto reset; 1136 /* RPC connection errors */ 1137 case -ECONNREFUSED: 1138 case -EHOSTDOWN: 1139 case -EHOSTUNREACH: 1140 case -ENETUNREACH: 1141 case -EIO: 1142 case -ETIMEDOUT: 1143 case -EPIPE: 1144 dprintk("%s DS connection error %d\n", __func__, 1145 task->tk_status); 1146 nfs4_mark_deviceid_unavailable(devid); 1147 rpc_wake_up(&tbl->slot_tbl_waitq); 1148 /* fall through */ 1149 default: 1150 if (ff_layout_avoid_mds_available_ds(lseg)) 1151 return -NFS4ERR_RESET_TO_PNFS; 1152 reset: 1153 dprintk("%s Retry through MDS. Error %d\n", __func__, 1154 task->tk_status); 1155 return -NFS4ERR_RESET_TO_MDS; 1156 } 1157 out: 1158 task->tk_status = 0; 1159 return -EAGAIN; 1160 out_bad_stateid: 1161 task->tk_status = -EIO; 1162 return 0; 1163 wait_on_recovery: 1164 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL); 1165 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0) 1166 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task); 1167 goto out; 1168 } 1169 1170 /* Retry all errors through either pNFS or MDS except for -EJUKEBOX */ 1171 static int ff_layout_async_handle_error_v3(struct rpc_task *task, 1172 struct pnfs_layout_segment *lseg, 1173 int idx) 1174 { 1175 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx); 1176 1177 if (task->tk_status >= 0) 1178 return 0; 1179 1180 switch (task->tk_status) { 1181 /* File access problems. Don't mark the device as unavailable */ 1182 case -EACCES: 1183 case -ESTALE: 1184 case -EISDIR: 1185 case -EBADHANDLE: 1186 case -ELOOP: 1187 case -ENOSPC: 1188 break; 1189 case -EJUKEBOX: 1190 nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY); 1191 goto out_retry; 1192 default: 1193 dprintk("%s DS connection error %d\n", __func__, 1194 task->tk_status); 1195 nfs4_mark_deviceid_unavailable(devid); 1196 } 1197 /* FIXME: Need to prevent infinite looping here. */ 1198 return -NFS4ERR_RESET_TO_PNFS; 1199 out_retry: 1200 task->tk_status = 0; 1201 rpc_restart_call_prepare(task); 1202 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME); 1203 return -EAGAIN; 1204 } 1205 1206 static int ff_layout_async_handle_error(struct rpc_task *task, 1207 struct nfs4_state *state, 1208 struct nfs_client *clp, 1209 struct pnfs_layout_segment *lseg, 1210 int idx) 1211 { 1212 int vers = clp->cl_nfs_mod->rpc_vers->number; 1213 1214 switch (vers) { 1215 case 3: 1216 return ff_layout_async_handle_error_v3(task, lseg, idx); 1217 case 4: 1218 return ff_layout_async_handle_error_v4(task, state, clp, 1219 lseg, idx); 1220 default: 1221 /* should never happen */ 1222 WARN_ON_ONCE(1); 1223 return 0; 1224 } 1225 } 1226 1227 static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg, 1228 int idx, u64 offset, u64 length, 1229 u32 status, int opnum, int error) 1230 { 1231 struct nfs4_ff_layout_mirror *mirror; 1232 int err; 1233 1234 if (status == 0) { 1235 switch (error) { 1236 case -ETIMEDOUT: 1237 case -EPFNOSUPPORT: 1238 case -EPROTONOSUPPORT: 1239 case -EOPNOTSUPP: 1240 case -ECONNREFUSED: 1241 case -ECONNRESET: 1242 case -EHOSTDOWN: 1243 case -EHOSTUNREACH: 1244 case -ENETUNREACH: 1245 case -EADDRINUSE: 1246 case -ENOBUFS: 1247 case -EPIPE: 1248 case -EPERM: 1249 status = NFS4ERR_NXIO; 1250 break; 1251 case -EACCES: 1252 status = NFS4ERR_ACCESS; 1253 break; 1254 default: 1255 return; 1256 } 1257 } 1258 1259 switch (status) { 1260 case NFS4ERR_DELAY: 1261 case NFS4ERR_GRACE: 1262 return; 1263 default: 1264 break; 1265 } 1266 1267 mirror = FF_LAYOUT_COMP(lseg, idx); 1268 err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout), 1269 mirror, offset, length, status, opnum, 1270 GFP_NOIO); 1271 pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, lseg); 1272 dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status); 1273 } 1274 1275 /* NFS_PROTO call done callback routines */ 1276 static int ff_layout_read_done_cb(struct rpc_task *task, 1277 struct nfs_pgio_header *hdr) 1278 { 1279 int err; 1280 1281 trace_nfs4_pnfs_read(hdr, task->tk_status); 1282 if (task->tk_status < 0) 1283 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx, 1284 hdr->args.offset, hdr->args.count, 1285 hdr->res.op_status, OP_READ, 1286 task->tk_status); 1287 err = ff_layout_async_handle_error(task, hdr->args.context->state, 1288 hdr->ds_clp, hdr->lseg, 1289 hdr->pgio_mirror_idx); 1290 1291 switch (err) { 1292 case -NFS4ERR_RESET_TO_PNFS: 1293 if (ff_layout_choose_best_ds_for_read(hdr->lseg, 1294 hdr->pgio_mirror_idx + 1, 1295 &hdr->pgio_mirror_idx)) 1296 goto out_eagain; 1297 pnfs_read_resend_pnfs(hdr); 1298 return task->tk_status; 1299 case -NFS4ERR_RESET_TO_MDS: 1300 ff_layout_reset_read(hdr); 1301 return task->tk_status; 1302 case -EAGAIN: 1303 goto out_eagain; 1304 } 1305 1306 return 0; 1307 out_eagain: 1308 rpc_restart_call_prepare(task); 1309 return -EAGAIN; 1310 } 1311 1312 static bool 1313 ff_layout_need_layoutcommit(struct pnfs_layout_segment *lseg) 1314 { 1315 return !(FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_LAYOUTCOMMIT); 1316 } 1317 1318 /* 1319 * We reference the rpc_cred of the first WRITE that triggers the need for 1320 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound. 1321 * rfc5661 is not clear about which credential should be used. 1322 * 1323 * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so 1324 * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751 1325 * we always send layoutcommit after DS writes. 1326 */ 1327 static void 1328 ff_layout_set_layoutcommit(struct nfs_pgio_header *hdr) 1329 { 1330 if (!ff_layout_need_layoutcommit(hdr->lseg)) 1331 return; 1332 1333 pnfs_set_layoutcommit(hdr->inode, hdr->lseg, 1334 hdr->mds_offset + hdr->res.count); 1335 dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino, 1336 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb); 1337 } 1338 1339 static bool 1340 ff_layout_device_unavailable(struct pnfs_layout_segment *lseg, int idx) 1341 { 1342 /* No mirroring for now */ 1343 struct nfs4_deviceid_node *node = FF_LAYOUT_DEVID_NODE(lseg, idx); 1344 1345 return ff_layout_test_devid_unavailable(node); 1346 } 1347 1348 static void ff_layout_read_record_layoutstats_start(struct rpc_task *task, 1349 struct nfs_pgio_header *hdr) 1350 { 1351 if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags)) 1352 return; 1353 nfs4_ff_layout_stat_io_start_read(hdr->inode, 1354 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), 1355 hdr->args.count, 1356 task->tk_start); 1357 } 1358 1359 static void ff_layout_read_record_layoutstats_done(struct rpc_task *task, 1360 struct nfs_pgio_header *hdr) 1361 { 1362 if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags)) 1363 return; 1364 nfs4_ff_layout_stat_io_end_read(task, 1365 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), 1366 hdr->args.count, 1367 hdr->res.count); 1368 } 1369 1370 static int ff_layout_read_prepare_common(struct rpc_task *task, 1371 struct nfs_pgio_header *hdr) 1372 { 1373 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) { 1374 rpc_exit(task, -EIO); 1375 return -EIO; 1376 } 1377 if (ff_layout_device_unavailable(hdr->lseg, hdr->pgio_mirror_idx)) { 1378 rpc_exit(task, -EHOSTDOWN); 1379 return -EAGAIN; 1380 } 1381 1382 ff_layout_read_record_layoutstats_start(task, hdr); 1383 return 0; 1384 } 1385 1386 /* 1387 * Call ops for the async read/write cases 1388 * In the case of dense layouts, the offset needs to be reset to its 1389 * original value. 1390 */ 1391 static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data) 1392 { 1393 struct nfs_pgio_header *hdr = data; 1394 1395 if (ff_layout_read_prepare_common(task, hdr)) 1396 return; 1397 1398 rpc_call_start(task); 1399 } 1400 1401 static int ff_layout_setup_sequence(struct nfs_client *ds_clp, 1402 struct nfs4_sequence_args *args, 1403 struct nfs4_sequence_res *res, 1404 struct rpc_task *task) 1405 { 1406 if (ds_clp->cl_session) 1407 return nfs41_setup_sequence(ds_clp->cl_session, 1408 args, 1409 res, 1410 task); 1411 return nfs40_setup_sequence(ds_clp->cl_slot_tbl, 1412 args, 1413 res, 1414 task); 1415 } 1416 1417 static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data) 1418 { 1419 struct nfs_pgio_header *hdr = data; 1420 1421 if (ff_layout_setup_sequence(hdr->ds_clp, 1422 &hdr->args.seq_args, 1423 &hdr->res.seq_res, 1424 task)) 1425 return; 1426 1427 if (ff_layout_read_prepare_common(task, hdr)) 1428 return; 1429 1430 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, 1431 hdr->args.lock_context, FMODE_READ) == -EIO) 1432 rpc_exit(task, -EIO); /* lost lock, terminate I/O */ 1433 } 1434 1435 static void ff_layout_read_call_done(struct rpc_task *task, void *data) 1436 { 1437 struct nfs_pgio_header *hdr = data; 1438 1439 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status); 1440 1441 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) && 1442 task->tk_status == 0) { 1443 nfs4_sequence_done(task, &hdr->res.seq_res); 1444 return; 1445 } 1446 1447 /* Note this may cause RPC to be resent */ 1448 hdr->mds_ops->rpc_call_done(task, hdr); 1449 } 1450 1451 static void ff_layout_read_count_stats(struct rpc_task *task, void *data) 1452 { 1453 struct nfs_pgio_header *hdr = data; 1454 1455 ff_layout_read_record_layoutstats_done(task, hdr); 1456 rpc_count_iostats_metrics(task, 1457 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]); 1458 } 1459 1460 static void ff_layout_read_release(void *data) 1461 { 1462 struct nfs_pgio_header *hdr = data; 1463 1464 ff_layout_read_record_layoutstats_done(&hdr->task, hdr); 1465 pnfs_generic_rw_release(data); 1466 } 1467 1468 1469 static int ff_layout_write_done_cb(struct rpc_task *task, 1470 struct nfs_pgio_header *hdr) 1471 { 1472 int err; 1473 1474 trace_nfs4_pnfs_write(hdr, task->tk_status); 1475 if (task->tk_status < 0) 1476 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx, 1477 hdr->args.offset, hdr->args.count, 1478 hdr->res.op_status, OP_WRITE, 1479 task->tk_status); 1480 err = ff_layout_async_handle_error(task, hdr->args.context->state, 1481 hdr->ds_clp, hdr->lseg, 1482 hdr->pgio_mirror_idx); 1483 1484 switch (err) { 1485 case -NFS4ERR_RESET_TO_PNFS: 1486 ff_layout_reset_write(hdr, true); 1487 return task->tk_status; 1488 case -NFS4ERR_RESET_TO_MDS: 1489 ff_layout_reset_write(hdr, false); 1490 return task->tk_status; 1491 case -EAGAIN: 1492 return -EAGAIN; 1493 } 1494 1495 if (hdr->res.verf->committed == NFS_FILE_SYNC || 1496 hdr->res.verf->committed == NFS_DATA_SYNC) 1497 ff_layout_set_layoutcommit(hdr); 1498 1499 /* zero out fattr since we don't care DS attr at all */ 1500 hdr->fattr.valid = 0; 1501 if (task->tk_status >= 0) 1502 nfs_writeback_update_inode(hdr); 1503 1504 return 0; 1505 } 1506 1507 static int ff_layout_commit_done_cb(struct rpc_task *task, 1508 struct nfs_commit_data *data) 1509 { 1510 int err; 1511 1512 trace_nfs4_pnfs_commit_ds(data, task->tk_status); 1513 if (task->tk_status < 0) 1514 ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index, 1515 data->args.offset, data->args.count, 1516 data->res.op_status, OP_COMMIT, 1517 task->tk_status); 1518 err = ff_layout_async_handle_error(task, NULL, data->ds_clp, 1519 data->lseg, data->ds_commit_index); 1520 1521 switch (err) { 1522 case -NFS4ERR_RESET_TO_PNFS: 1523 pnfs_generic_prepare_to_resend_writes(data); 1524 return -EAGAIN; 1525 case -NFS4ERR_RESET_TO_MDS: 1526 pnfs_generic_prepare_to_resend_writes(data); 1527 return -EAGAIN; 1528 case -EAGAIN: 1529 rpc_restart_call_prepare(task); 1530 return -EAGAIN; 1531 } 1532 1533 if (data->verf.committed == NFS_UNSTABLE 1534 && ff_layout_need_layoutcommit(data->lseg)) 1535 pnfs_set_layoutcommit(data->inode, data->lseg, data->lwb); 1536 1537 return 0; 1538 } 1539 1540 static void ff_layout_write_record_layoutstats_start(struct rpc_task *task, 1541 struct nfs_pgio_header *hdr) 1542 { 1543 if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags)) 1544 return; 1545 nfs4_ff_layout_stat_io_start_write(hdr->inode, 1546 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), 1547 hdr->args.count, 1548 task->tk_start); 1549 } 1550 1551 static void ff_layout_write_record_layoutstats_done(struct rpc_task *task, 1552 struct nfs_pgio_header *hdr) 1553 { 1554 if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags)) 1555 return; 1556 nfs4_ff_layout_stat_io_end_write(task, 1557 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), 1558 hdr->args.count, hdr->res.count, 1559 hdr->res.verf->committed); 1560 } 1561 1562 static int ff_layout_write_prepare_common(struct rpc_task *task, 1563 struct nfs_pgio_header *hdr) 1564 { 1565 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) { 1566 rpc_exit(task, -EIO); 1567 return -EIO; 1568 } 1569 1570 if (ff_layout_device_unavailable(hdr->lseg, hdr->pgio_mirror_idx)) { 1571 rpc_exit(task, -EHOSTDOWN); 1572 return -EAGAIN; 1573 } 1574 1575 ff_layout_write_record_layoutstats_start(task, hdr); 1576 return 0; 1577 } 1578 1579 static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data) 1580 { 1581 struct nfs_pgio_header *hdr = data; 1582 1583 if (ff_layout_write_prepare_common(task, hdr)) 1584 return; 1585 1586 rpc_call_start(task); 1587 } 1588 1589 static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data) 1590 { 1591 struct nfs_pgio_header *hdr = data; 1592 1593 if (ff_layout_setup_sequence(hdr->ds_clp, 1594 &hdr->args.seq_args, 1595 &hdr->res.seq_res, 1596 task)) 1597 return; 1598 1599 if (ff_layout_write_prepare_common(task, hdr)) 1600 return; 1601 1602 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, 1603 hdr->args.lock_context, FMODE_WRITE) == -EIO) 1604 rpc_exit(task, -EIO); /* lost lock, terminate I/O */ 1605 } 1606 1607 static void ff_layout_write_call_done(struct rpc_task *task, void *data) 1608 { 1609 struct nfs_pgio_header *hdr = data; 1610 1611 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) && 1612 task->tk_status == 0) { 1613 nfs4_sequence_done(task, &hdr->res.seq_res); 1614 return; 1615 } 1616 1617 /* Note this may cause RPC to be resent */ 1618 hdr->mds_ops->rpc_call_done(task, hdr); 1619 } 1620 1621 static void ff_layout_write_count_stats(struct rpc_task *task, void *data) 1622 { 1623 struct nfs_pgio_header *hdr = data; 1624 1625 ff_layout_write_record_layoutstats_done(task, hdr); 1626 rpc_count_iostats_metrics(task, 1627 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]); 1628 } 1629 1630 static void ff_layout_write_release(void *data) 1631 { 1632 struct nfs_pgio_header *hdr = data; 1633 1634 ff_layout_write_record_layoutstats_done(&hdr->task, hdr); 1635 pnfs_generic_rw_release(data); 1636 } 1637 1638 static void ff_layout_commit_record_layoutstats_start(struct rpc_task *task, 1639 struct nfs_commit_data *cdata) 1640 { 1641 if (test_and_set_bit(NFS_IOHDR_STAT, &cdata->flags)) 1642 return; 1643 nfs4_ff_layout_stat_io_start_write(cdata->inode, 1644 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index), 1645 0, task->tk_start); 1646 } 1647 1648 static void ff_layout_commit_record_layoutstats_done(struct rpc_task *task, 1649 struct nfs_commit_data *cdata) 1650 { 1651 struct nfs_page *req; 1652 __u64 count = 0; 1653 1654 if (!test_and_clear_bit(NFS_IOHDR_STAT, &cdata->flags)) 1655 return; 1656 1657 if (task->tk_status == 0) { 1658 list_for_each_entry(req, &cdata->pages, wb_list) 1659 count += req->wb_bytes; 1660 } 1661 nfs4_ff_layout_stat_io_end_write(task, 1662 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index), 1663 count, count, NFS_FILE_SYNC); 1664 } 1665 1666 static void ff_layout_commit_prepare_common(struct rpc_task *task, 1667 struct nfs_commit_data *cdata) 1668 { 1669 ff_layout_commit_record_layoutstats_start(task, cdata); 1670 } 1671 1672 static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data) 1673 { 1674 ff_layout_commit_prepare_common(task, data); 1675 rpc_call_start(task); 1676 } 1677 1678 static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data) 1679 { 1680 struct nfs_commit_data *wdata = data; 1681 1682 if (ff_layout_setup_sequence(wdata->ds_clp, 1683 &wdata->args.seq_args, 1684 &wdata->res.seq_res, 1685 task)) 1686 return; 1687 ff_layout_commit_prepare_common(task, data); 1688 } 1689 1690 static void ff_layout_commit_done(struct rpc_task *task, void *data) 1691 { 1692 pnfs_generic_write_commit_done(task, data); 1693 } 1694 1695 static void ff_layout_commit_count_stats(struct rpc_task *task, void *data) 1696 { 1697 struct nfs_commit_data *cdata = data; 1698 1699 ff_layout_commit_record_layoutstats_done(task, cdata); 1700 rpc_count_iostats_metrics(task, 1701 &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]); 1702 } 1703 1704 static void ff_layout_commit_release(void *data) 1705 { 1706 struct nfs_commit_data *cdata = data; 1707 1708 ff_layout_commit_record_layoutstats_done(&cdata->task, cdata); 1709 pnfs_generic_commit_release(data); 1710 } 1711 1712 static const struct rpc_call_ops ff_layout_read_call_ops_v3 = { 1713 .rpc_call_prepare = ff_layout_read_prepare_v3, 1714 .rpc_call_done = ff_layout_read_call_done, 1715 .rpc_count_stats = ff_layout_read_count_stats, 1716 .rpc_release = ff_layout_read_release, 1717 }; 1718 1719 static const struct rpc_call_ops ff_layout_read_call_ops_v4 = { 1720 .rpc_call_prepare = ff_layout_read_prepare_v4, 1721 .rpc_call_done = ff_layout_read_call_done, 1722 .rpc_count_stats = ff_layout_read_count_stats, 1723 .rpc_release = ff_layout_read_release, 1724 }; 1725 1726 static const struct rpc_call_ops ff_layout_write_call_ops_v3 = { 1727 .rpc_call_prepare = ff_layout_write_prepare_v3, 1728 .rpc_call_done = ff_layout_write_call_done, 1729 .rpc_count_stats = ff_layout_write_count_stats, 1730 .rpc_release = ff_layout_write_release, 1731 }; 1732 1733 static const struct rpc_call_ops ff_layout_write_call_ops_v4 = { 1734 .rpc_call_prepare = ff_layout_write_prepare_v4, 1735 .rpc_call_done = ff_layout_write_call_done, 1736 .rpc_count_stats = ff_layout_write_count_stats, 1737 .rpc_release = ff_layout_write_release, 1738 }; 1739 1740 static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = { 1741 .rpc_call_prepare = ff_layout_commit_prepare_v3, 1742 .rpc_call_done = ff_layout_commit_done, 1743 .rpc_count_stats = ff_layout_commit_count_stats, 1744 .rpc_release = ff_layout_commit_release, 1745 }; 1746 1747 static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = { 1748 .rpc_call_prepare = ff_layout_commit_prepare_v4, 1749 .rpc_call_done = ff_layout_commit_done, 1750 .rpc_count_stats = ff_layout_commit_count_stats, 1751 .rpc_release = ff_layout_commit_release, 1752 }; 1753 1754 static enum pnfs_try_status 1755 ff_layout_read_pagelist(struct nfs_pgio_header *hdr) 1756 { 1757 struct pnfs_layout_segment *lseg = hdr->lseg; 1758 struct nfs4_pnfs_ds *ds; 1759 struct rpc_clnt *ds_clnt; 1760 struct rpc_cred *ds_cred; 1761 loff_t offset = hdr->args.offset; 1762 u32 idx = hdr->pgio_mirror_idx; 1763 int vers; 1764 struct nfs_fh *fh; 1765 1766 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n", 1767 __func__, hdr->inode->i_ino, 1768 hdr->args.pgbase, (size_t)hdr->args.count, offset); 1769 1770 ds = nfs4_ff_layout_prepare_ds(lseg, idx, false); 1771 if (!ds) 1772 goto out_failed; 1773 1774 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp, 1775 hdr->inode); 1776 if (IS_ERR(ds_clnt)) 1777 goto out_failed; 1778 1779 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred); 1780 if (!ds_cred) 1781 goto out_failed; 1782 1783 vers = nfs4_ff_layout_ds_version(lseg, idx); 1784 1785 dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__, 1786 ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), vers); 1787 1788 hdr->pgio_done_cb = ff_layout_read_done_cb; 1789 atomic_inc(&ds->ds_clp->cl_count); 1790 hdr->ds_clp = ds->ds_clp; 1791 fh = nfs4_ff_layout_select_ds_fh(lseg, idx); 1792 if (fh) 1793 hdr->args.fh = fh; 1794 /* 1795 * Note that if we ever decide to split across DSes, 1796 * then we may need to handle dense-like offsets. 1797 */ 1798 hdr->args.offset = offset; 1799 hdr->mds_offset = offset; 1800 1801 /* Perform an asynchronous read to ds */ 1802 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, 1803 vers == 3 ? &ff_layout_read_call_ops_v3 : 1804 &ff_layout_read_call_ops_v4, 1805 0, RPC_TASK_SOFTCONN); 1806 put_rpccred(ds_cred); 1807 return PNFS_ATTEMPTED; 1808 1809 out_failed: 1810 if (ff_layout_avoid_mds_available_ds(lseg)) 1811 return PNFS_TRY_AGAIN; 1812 return PNFS_NOT_ATTEMPTED; 1813 } 1814 1815 /* Perform async writes. */ 1816 static enum pnfs_try_status 1817 ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync) 1818 { 1819 struct pnfs_layout_segment *lseg = hdr->lseg; 1820 struct nfs4_pnfs_ds *ds; 1821 struct rpc_clnt *ds_clnt; 1822 struct rpc_cred *ds_cred; 1823 loff_t offset = hdr->args.offset; 1824 int vers; 1825 struct nfs_fh *fh; 1826 int idx = hdr->pgio_mirror_idx; 1827 1828 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true); 1829 if (!ds) 1830 return PNFS_NOT_ATTEMPTED; 1831 1832 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp, 1833 hdr->inode); 1834 if (IS_ERR(ds_clnt)) 1835 return PNFS_NOT_ATTEMPTED; 1836 1837 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred); 1838 if (!ds_cred) 1839 return PNFS_NOT_ATTEMPTED; 1840 1841 vers = nfs4_ff_layout_ds_version(lseg, idx); 1842 1843 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d vers %d\n", 1844 __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count, 1845 offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), 1846 vers); 1847 1848 hdr->pgio_done_cb = ff_layout_write_done_cb; 1849 atomic_inc(&ds->ds_clp->cl_count); 1850 hdr->ds_clp = ds->ds_clp; 1851 hdr->ds_commit_idx = idx; 1852 fh = nfs4_ff_layout_select_ds_fh(lseg, idx); 1853 if (fh) 1854 hdr->args.fh = fh; 1855 1856 /* 1857 * Note that if we ever decide to split across DSes, 1858 * then we may need to handle dense-like offsets. 1859 */ 1860 hdr->args.offset = offset; 1861 1862 /* Perform an asynchronous write */ 1863 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, 1864 vers == 3 ? &ff_layout_write_call_ops_v3 : 1865 &ff_layout_write_call_ops_v4, 1866 sync, RPC_TASK_SOFTCONN); 1867 put_rpccred(ds_cred); 1868 return PNFS_ATTEMPTED; 1869 } 1870 1871 static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i) 1872 { 1873 return i; 1874 } 1875 1876 static struct nfs_fh * 1877 select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i) 1878 { 1879 struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg); 1880 1881 /* FIXME: Assume that there is only one NFS version available 1882 * for the DS. 1883 */ 1884 return &flseg->mirror_array[i]->fh_versions[0]; 1885 } 1886 1887 static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how) 1888 { 1889 struct pnfs_layout_segment *lseg = data->lseg; 1890 struct nfs4_pnfs_ds *ds; 1891 struct rpc_clnt *ds_clnt; 1892 struct rpc_cred *ds_cred; 1893 u32 idx; 1894 int vers, ret; 1895 struct nfs_fh *fh; 1896 1897 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index); 1898 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true); 1899 if (!ds) 1900 goto out_err; 1901 1902 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp, 1903 data->inode); 1904 if (IS_ERR(ds_clnt)) 1905 goto out_err; 1906 1907 ds_cred = ff_layout_get_ds_cred(lseg, idx, data->cred); 1908 if (!ds_cred) 1909 goto out_err; 1910 1911 vers = nfs4_ff_layout_ds_version(lseg, idx); 1912 1913 dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__, 1914 data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count), 1915 vers); 1916 data->commit_done_cb = ff_layout_commit_done_cb; 1917 data->cred = ds_cred; 1918 atomic_inc(&ds->ds_clp->cl_count); 1919 data->ds_clp = ds->ds_clp; 1920 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index); 1921 if (fh) 1922 data->args.fh = fh; 1923 1924 ret = nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops, 1925 vers == 3 ? &ff_layout_commit_call_ops_v3 : 1926 &ff_layout_commit_call_ops_v4, 1927 how, RPC_TASK_SOFTCONN); 1928 put_rpccred(ds_cred); 1929 return ret; 1930 out_err: 1931 pnfs_generic_prepare_to_resend_writes(data); 1932 pnfs_generic_commit_release(data); 1933 return -EAGAIN; 1934 } 1935 1936 static int 1937 ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages, 1938 int how, struct nfs_commit_info *cinfo) 1939 { 1940 return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo, 1941 ff_layout_initiate_commit); 1942 } 1943 1944 static struct pnfs_ds_commit_info * 1945 ff_layout_get_ds_info(struct inode *inode) 1946 { 1947 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout; 1948 1949 if (layout == NULL) 1950 return NULL; 1951 1952 return &FF_LAYOUT_FROM_HDR(layout)->commit_info; 1953 } 1954 1955 static void 1956 ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d) 1957 { 1958 nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds, 1959 id_node)); 1960 } 1961 1962 static int ff_layout_encode_ioerr(struct nfs4_flexfile_layout *flo, 1963 struct xdr_stream *xdr, 1964 const struct nfs4_layoutreturn_args *args) 1965 { 1966 struct pnfs_layout_hdr *hdr = &flo->generic_hdr; 1967 __be32 *start; 1968 int count = 0, ret = 0; 1969 1970 start = xdr_reserve_space(xdr, 4); 1971 if (unlikely(!start)) 1972 return -E2BIG; 1973 1974 /* This assume we always return _ALL_ layouts */ 1975 spin_lock(&hdr->plh_inode->i_lock); 1976 ret = ff_layout_encode_ds_ioerr(flo, xdr, &count, &args->range); 1977 spin_unlock(&hdr->plh_inode->i_lock); 1978 1979 *start = cpu_to_be32(count); 1980 1981 return ret; 1982 } 1983 1984 /* report nothing for now */ 1985 static void ff_layout_encode_iostats(struct nfs4_flexfile_layout *flo, 1986 struct xdr_stream *xdr, 1987 const struct nfs4_layoutreturn_args *args) 1988 { 1989 __be32 *p; 1990 1991 p = xdr_reserve_space(xdr, 4); 1992 if (likely(p)) 1993 *p = cpu_to_be32(0); 1994 } 1995 1996 static struct nfs4_deviceid_node * 1997 ff_layout_alloc_deviceid_node(struct nfs_server *server, 1998 struct pnfs_device *pdev, gfp_t gfp_flags) 1999 { 2000 struct nfs4_ff_layout_ds *dsaddr; 2001 2002 dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags); 2003 if (!dsaddr) 2004 return NULL; 2005 return &dsaddr->id_node; 2006 } 2007 2008 static void 2009 ff_layout_encode_layoutreturn(struct pnfs_layout_hdr *lo, 2010 struct xdr_stream *xdr, 2011 const struct nfs4_layoutreturn_args *args) 2012 { 2013 struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo); 2014 __be32 *start; 2015 2016 dprintk("%s: Begin\n", __func__); 2017 start = xdr_reserve_space(xdr, 4); 2018 BUG_ON(!start); 2019 2020 ff_layout_encode_ioerr(flo, xdr, args); 2021 ff_layout_encode_iostats(flo, xdr, args); 2022 2023 *start = cpu_to_be32((xdr->p - start - 1) * 4); 2024 dprintk("%s: Return\n", __func__); 2025 } 2026 2027 static int 2028 ff_layout_ntop4(const struct sockaddr *sap, char *buf, const size_t buflen) 2029 { 2030 const struct sockaddr_in *sin = (struct sockaddr_in *)sap; 2031 2032 return snprintf(buf, buflen, "%pI4", &sin->sin_addr); 2033 } 2034 2035 static size_t 2036 ff_layout_ntop6_noscopeid(const struct sockaddr *sap, char *buf, 2037 const int buflen) 2038 { 2039 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; 2040 const struct in6_addr *addr = &sin6->sin6_addr; 2041 2042 /* 2043 * RFC 4291, Section 2.2.2 2044 * 2045 * Shorthanded ANY address 2046 */ 2047 if (ipv6_addr_any(addr)) 2048 return snprintf(buf, buflen, "::"); 2049 2050 /* 2051 * RFC 4291, Section 2.2.2 2052 * 2053 * Shorthanded loopback address 2054 */ 2055 if (ipv6_addr_loopback(addr)) 2056 return snprintf(buf, buflen, "::1"); 2057 2058 /* 2059 * RFC 4291, Section 2.2.3 2060 * 2061 * Special presentation address format for mapped v4 2062 * addresses. 2063 */ 2064 if (ipv6_addr_v4mapped(addr)) 2065 return snprintf(buf, buflen, "::ffff:%pI4", 2066 &addr->s6_addr32[3]); 2067 2068 /* 2069 * RFC 4291, Section 2.2.1 2070 */ 2071 return snprintf(buf, buflen, "%pI6c", addr); 2072 } 2073 2074 /* Derived from rpc_sockaddr2uaddr */ 2075 static void 2076 ff_layout_encode_netaddr(struct xdr_stream *xdr, struct nfs4_pnfs_ds_addr *da) 2077 { 2078 struct sockaddr *sap = (struct sockaddr *)&da->da_addr; 2079 char portbuf[RPCBIND_MAXUADDRPLEN]; 2080 char addrbuf[RPCBIND_MAXUADDRLEN]; 2081 char *netid; 2082 unsigned short port; 2083 int len, netid_len; 2084 __be32 *p; 2085 2086 switch (sap->sa_family) { 2087 case AF_INET: 2088 if (ff_layout_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0) 2089 return; 2090 port = ntohs(((struct sockaddr_in *)sap)->sin_port); 2091 netid = "tcp"; 2092 netid_len = 3; 2093 break; 2094 case AF_INET6: 2095 if (ff_layout_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0) 2096 return; 2097 port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port); 2098 netid = "tcp6"; 2099 netid_len = 4; 2100 break; 2101 default: 2102 /* we only support tcp and tcp6 */ 2103 WARN_ON_ONCE(1); 2104 return; 2105 } 2106 2107 snprintf(portbuf, sizeof(portbuf), ".%u.%u", port >> 8, port & 0xff); 2108 len = strlcat(addrbuf, portbuf, sizeof(addrbuf)); 2109 2110 p = xdr_reserve_space(xdr, 4 + netid_len); 2111 xdr_encode_opaque(p, netid, netid_len); 2112 2113 p = xdr_reserve_space(xdr, 4 + len); 2114 xdr_encode_opaque(p, addrbuf, len); 2115 } 2116 2117 static void 2118 ff_layout_encode_nfstime(struct xdr_stream *xdr, 2119 ktime_t t) 2120 { 2121 struct timespec64 ts; 2122 __be32 *p; 2123 2124 p = xdr_reserve_space(xdr, 12); 2125 ts = ktime_to_timespec64(t); 2126 p = xdr_encode_hyper(p, ts.tv_sec); 2127 *p++ = cpu_to_be32(ts.tv_nsec); 2128 } 2129 2130 static void 2131 ff_layout_encode_io_latency(struct xdr_stream *xdr, 2132 struct nfs4_ff_io_stat *stat) 2133 { 2134 __be32 *p; 2135 2136 p = xdr_reserve_space(xdr, 5 * 8); 2137 p = xdr_encode_hyper(p, stat->ops_requested); 2138 p = xdr_encode_hyper(p, stat->bytes_requested); 2139 p = xdr_encode_hyper(p, stat->ops_completed); 2140 p = xdr_encode_hyper(p, stat->bytes_completed); 2141 p = xdr_encode_hyper(p, stat->bytes_not_delivered); 2142 ff_layout_encode_nfstime(xdr, stat->total_busy_time); 2143 ff_layout_encode_nfstime(xdr, stat->aggregate_completion_time); 2144 } 2145 2146 static void 2147 ff_layout_encode_layoutstats(struct xdr_stream *xdr, 2148 struct nfs42_layoutstat_args *args, 2149 struct nfs42_layoutstat_devinfo *devinfo) 2150 { 2151 struct nfs4_ff_layout_mirror *mirror = devinfo->layout_private; 2152 struct nfs4_pnfs_ds_addr *da; 2153 struct nfs4_pnfs_ds *ds = mirror->mirror_ds->ds; 2154 struct nfs_fh *fh = &mirror->fh_versions[0]; 2155 __be32 *p, *start; 2156 2157 da = list_first_entry(&ds->ds_addrs, struct nfs4_pnfs_ds_addr, da_node); 2158 dprintk("%s: DS %s: encoding address %s\n", 2159 __func__, ds->ds_remotestr, da->da_remotestr); 2160 /* layoutupdate length */ 2161 start = xdr_reserve_space(xdr, 4); 2162 /* netaddr4 */ 2163 ff_layout_encode_netaddr(xdr, da); 2164 /* nfs_fh4 */ 2165 p = xdr_reserve_space(xdr, 4 + fh->size); 2166 xdr_encode_opaque(p, fh->data, fh->size); 2167 /* ff_io_latency4 read */ 2168 spin_lock(&mirror->lock); 2169 ff_layout_encode_io_latency(xdr, &mirror->read_stat.io_stat); 2170 /* ff_io_latency4 write */ 2171 ff_layout_encode_io_latency(xdr, &mirror->write_stat.io_stat); 2172 spin_unlock(&mirror->lock); 2173 /* nfstime4 */ 2174 ff_layout_encode_nfstime(xdr, ktime_sub(ktime_get(), mirror->start_time)); 2175 /* bool */ 2176 p = xdr_reserve_space(xdr, 4); 2177 *p = cpu_to_be32(false); 2178 2179 *start = cpu_to_be32((xdr->p - start - 1) * 4); 2180 } 2181 2182 static int 2183 ff_layout_mirror_prepare_stats(struct nfs42_layoutstat_args *args, 2184 struct pnfs_layout_hdr *lo, 2185 int dev_limit) 2186 { 2187 struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo); 2188 struct nfs4_ff_layout_mirror *mirror; 2189 struct nfs4_deviceid_node *dev; 2190 struct nfs42_layoutstat_devinfo *devinfo; 2191 int i = 0; 2192 2193 list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) { 2194 if (i >= dev_limit) 2195 break; 2196 if (!mirror->mirror_ds) 2197 continue; 2198 /* mirror refcount put in cleanup_layoutstats */ 2199 if (!atomic_inc_not_zero(&mirror->ref)) 2200 continue; 2201 dev = &mirror->mirror_ds->id_node; 2202 devinfo = &args->devinfo[i]; 2203 memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE); 2204 devinfo->offset = 0; 2205 devinfo->length = NFS4_MAX_UINT64; 2206 devinfo->read_count = mirror->read_stat.io_stat.ops_completed; 2207 devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed; 2208 devinfo->write_count = mirror->write_stat.io_stat.ops_completed; 2209 devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed; 2210 devinfo->layout_type = LAYOUT_FLEX_FILES; 2211 devinfo->layoutstats_encode = ff_layout_encode_layoutstats; 2212 devinfo->layout_private = mirror; 2213 2214 i++; 2215 } 2216 return i; 2217 } 2218 2219 static int 2220 ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args) 2221 { 2222 struct nfs4_flexfile_layout *ff_layout; 2223 struct nfs4_ff_layout_mirror *mirror; 2224 int dev_count = 0; 2225 2226 spin_lock(&args->inode->i_lock); 2227 ff_layout = FF_LAYOUT_FROM_HDR(NFS_I(args->inode)->layout); 2228 list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) { 2229 if (atomic_read(&mirror->ref) != 0) 2230 dev_count ++; 2231 } 2232 spin_unlock(&args->inode->i_lock); 2233 /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */ 2234 if (dev_count > PNFS_LAYOUTSTATS_MAXDEV) { 2235 dprintk("%s: truncating devinfo to limit (%d:%d)\n", 2236 __func__, dev_count, PNFS_LAYOUTSTATS_MAXDEV); 2237 dev_count = PNFS_LAYOUTSTATS_MAXDEV; 2238 } 2239 args->devinfo = kmalloc_array(dev_count, sizeof(*args->devinfo), GFP_NOIO); 2240 if (!args->devinfo) 2241 return -ENOMEM; 2242 2243 spin_lock(&args->inode->i_lock); 2244 args->num_dev = ff_layout_mirror_prepare_stats(args, 2245 &ff_layout->generic_hdr, dev_count); 2246 spin_unlock(&args->inode->i_lock); 2247 2248 return 0; 2249 } 2250 2251 static void 2252 ff_layout_cleanup_layoutstats(struct nfs42_layoutstat_data *data) 2253 { 2254 struct nfs4_ff_layout_mirror *mirror; 2255 int i; 2256 2257 for (i = 0; i < data->args.num_dev; i++) { 2258 mirror = data->args.devinfo[i].layout_private; 2259 data->args.devinfo[i].layout_private = NULL; 2260 ff_layout_put_mirror(mirror); 2261 } 2262 } 2263 2264 static struct pnfs_layoutdriver_type flexfilelayout_type = { 2265 .id = LAYOUT_FLEX_FILES, 2266 .name = "LAYOUT_FLEX_FILES", 2267 .owner = THIS_MODULE, 2268 .alloc_layout_hdr = ff_layout_alloc_layout_hdr, 2269 .free_layout_hdr = ff_layout_free_layout_hdr, 2270 .alloc_lseg = ff_layout_alloc_lseg, 2271 .free_lseg = ff_layout_free_lseg, 2272 .add_lseg = ff_layout_add_lseg, 2273 .pg_read_ops = &ff_layout_pg_read_ops, 2274 .pg_write_ops = &ff_layout_pg_write_ops, 2275 .get_ds_info = ff_layout_get_ds_info, 2276 .free_deviceid_node = ff_layout_free_deviceid_node, 2277 .mark_request_commit = pnfs_layout_mark_request_commit, 2278 .clear_request_commit = pnfs_generic_clear_request_commit, 2279 .scan_commit_lists = pnfs_generic_scan_commit_lists, 2280 .recover_commit_reqs = pnfs_generic_recover_commit_reqs, 2281 .commit_pagelist = ff_layout_commit_pagelist, 2282 .read_pagelist = ff_layout_read_pagelist, 2283 .write_pagelist = ff_layout_write_pagelist, 2284 .alloc_deviceid_node = ff_layout_alloc_deviceid_node, 2285 .encode_layoutreturn = ff_layout_encode_layoutreturn, 2286 .sync = pnfs_nfs_generic_sync, 2287 .prepare_layoutstats = ff_layout_prepare_layoutstats, 2288 .cleanup_layoutstats = ff_layout_cleanup_layoutstats, 2289 }; 2290 2291 static int __init nfs4flexfilelayout_init(void) 2292 { 2293 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n", 2294 __func__); 2295 if (!ff_zero_group) { 2296 ff_zero_group = groups_alloc(0); 2297 if (!ff_zero_group) 2298 return -ENOMEM; 2299 } 2300 return pnfs_register_layoutdriver(&flexfilelayout_type); 2301 } 2302 2303 static void __exit nfs4flexfilelayout_exit(void) 2304 { 2305 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n", 2306 __func__); 2307 pnfs_unregister_layoutdriver(&flexfilelayout_type); 2308 if (ff_zero_group) { 2309 put_group_info(ff_zero_group); 2310 ff_zero_group = NULL; 2311 } 2312 } 2313 2314 MODULE_ALIAS("nfs-layouttype4-4"); 2315 2316 MODULE_LICENSE("GPL"); 2317 MODULE_DESCRIPTION("The NFSv4 flexfile layout driver"); 2318 2319 module_init(nfs4flexfilelayout_init); 2320 module_exit(nfs4flexfilelayout_exit); 2321