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