1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright (c) 2013 by Delphix. All rights reserved. 28 * Copyright (c) 2013 Steven Hartland. All rights reserved. 29 */ 30 31 #include <sys/zfs_context.h> 32 #include <sys/spa.h> 33 #include <sys/vdev_impl.h> 34 #include <sys/zio.h> 35 #include <sys/fs/zfs.h> 36 37 /* 38 * Virtual device vector for mirroring. 39 */ 40 41 typedef struct mirror_child { 42 vdev_t *mc_vd; 43 uint64_t mc_offset; 44 int mc_error; 45 int mc_load; 46 uint8_t mc_tried; 47 uint8_t mc_skipped; 48 uint8_t mc_speculative; 49 } mirror_child_t; 50 51 typedef struct mirror_map { 52 int *mm_preferred; 53 int mm_preferred_cnt; 54 int mm_children; 55 boolean_t mm_replacing; 56 boolean_t mm_root; 57 mirror_child_t mm_child[]; 58 } mirror_map_t; 59 60 static int vdev_mirror_shift = 21; 61 62 /* 63 * The load configuration settings below are tuned by default for 64 * the case where all devices are of the same rotational type. 65 * 66 * If there is a mixture of rotating and non-rotating media, setting 67 * non_rotating_seek_inc to 0 may well provide better results as it 68 * will direct more reads to the non-rotating vdevs which are more 69 * likely to have a higher performance. 70 */ 71 72 /* Rotating media load calculation configuration. */ 73 /* Rotating media load increment for non-seeking I/O's. */ 74 static int rotating_inc = 0; 75 76 /* Rotating media load increment for seeking I/O's. */ 77 static int rotating_seek_inc = 5; 78 79 /* 80 * Offset in bytes from the last I/O which triggers a reduced rotating media 81 * seek increment. 82 */ 83 static int rotating_seek_offset = 1 * 1024 * 1024; 84 85 /* Non-rotating media load calculation configuration. */ 86 /* Non-rotating media load increment for non-seeking I/O's. */ 87 static int non_rotating_inc = 0; 88 89 /* Non-rotating media load increment for seeking I/O's. */ 90 static int non_rotating_seek_inc = 1; 91 92 static inline size_t 93 vdev_mirror_map_size(int children) 94 { 95 return (offsetof(mirror_map_t, mm_child[children]) + 96 sizeof (int) * children); 97 } 98 99 static inline mirror_map_t * 100 vdev_mirror_map_alloc(int children, boolean_t replacing, boolean_t root) 101 { 102 mirror_map_t *mm; 103 104 mm = kmem_zalloc(vdev_mirror_map_size(children), KM_SLEEP); 105 mm->mm_children = children; 106 mm->mm_replacing = replacing; 107 mm->mm_root = root; 108 mm->mm_preferred = (int *)((uintptr_t)mm + 109 offsetof(mirror_map_t, mm_child[children])); 110 111 return (mm); 112 } 113 114 static void 115 vdev_mirror_map_free(zio_t *zio) 116 { 117 mirror_map_t *mm = zio->io_vsd; 118 119 kmem_free(mm, vdev_mirror_map_size(mm->mm_children)); 120 } 121 122 static const zio_vsd_ops_t vdev_mirror_vsd_ops = { 123 vdev_mirror_map_free, 124 zio_vsd_default_cksum_report 125 }; 126 127 static int 128 vdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset) 129 { 130 uint64_t lastoffset; 131 int load; 132 133 /* All DVAs have equal weight at the root. */ 134 if (mm->mm_root) 135 return (INT_MAX); 136 137 /* 138 * We don't return INT_MAX if the device is resilvering i.e. 139 * vdev_resilver_txg != 0 as when tested performance was slightly 140 * worse overall when resilvering with compared to without. 141 */ 142 143 /* Standard load based on pending queue length. */ 144 load = vdev_queue_length(vd); 145 lastoffset = vdev_queue_lastoffset(vd); 146 147 if (vd->vdev_rotation_rate == VDEV_RATE_NON_ROTATING) { 148 /* Non-rotating media. */ 149 if (lastoffset == zio_offset) 150 return (load + non_rotating_inc); 151 152 /* 153 * Apply a seek penalty even for non-rotating devices as 154 * sequential I/O'a can be aggregated into fewer operations 155 * on the device, thus avoiding unnecessary per-command 156 * overhead and boosting performance. 157 */ 158 return (load + non_rotating_seek_inc); 159 } 160 161 /* Rotating media I/O's which directly follow the last I/O. */ 162 if (lastoffset == zio_offset) 163 return (load + rotating_inc); 164 165 /* 166 * Apply half the seek increment to I/O's within seek offset 167 * of the last I/O queued to this vdev as they should incure less 168 * of a seek increment. 169 */ 170 if (ABS(lastoffset - zio_offset) < rotating_seek_offset) 171 return (load + (rotating_seek_inc / 2)); 172 173 /* Apply the full seek increment to all other I/O's. */ 174 return (load + rotating_seek_inc); 175 } 176 177 178 static mirror_map_t * 179 vdev_mirror_map_init(zio_t *zio) 180 { 181 mirror_map_t *mm = NULL; 182 mirror_child_t *mc; 183 vdev_t *vd = zio->io_vd; 184 int c; 185 186 if (vd == NULL) { 187 dva_t *dva = zio->io_bp->blk_dva; 188 spa_t *spa = zio->io_spa; 189 190 mm = vdev_mirror_map_alloc(BP_GET_NDVAS(zio->io_bp), B_FALSE, 191 B_TRUE); 192 for (c = 0; c < mm->mm_children; c++) { 193 mc = &mm->mm_child[c]; 194 mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c])); 195 mc->mc_offset = DVA_GET_OFFSET(&dva[c]); 196 } 197 } else { 198 mm = vdev_mirror_map_alloc(vd->vdev_children, 199 (vd->vdev_ops == &vdev_replacing_ops || 200 vd->vdev_ops == &vdev_spare_ops), B_FALSE); 201 for (c = 0; c < mm->mm_children; c++) { 202 mc = &mm->mm_child[c]; 203 mc->mc_vd = vd->vdev_child[c]; 204 mc->mc_offset = zio->io_offset; 205 } 206 } 207 208 zio->io_vsd = mm; 209 zio->io_vsd_ops = &vdev_mirror_vsd_ops; 210 return (mm); 211 } 212 213 static int 214 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize, 215 uint64_t *ashift) 216 { 217 int numerrors = 0; 218 int lasterror = 0; 219 220 if (vd->vdev_children == 0) { 221 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 222 return (SET_ERROR(EINVAL)); 223 } 224 225 vdev_open_children(vd); 226 227 for (int c = 0; c < vd->vdev_children; c++) { 228 vdev_t *cvd = vd->vdev_child[c]; 229 230 if (cvd->vdev_open_error) { 231 lasterror = cvd->vdev_open_error; 232 numerrors++; 233 continue; 234 } 235 236 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1; 237 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1; 238 *ashift = MAX(*ashift, cvd->vdev_ashift); 239 } 240 241 if (numerrors == vd->vdev_children) { 242 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS; 243 return (lasterror); 244 } 245 246 return (0); 247 } 248 249 static void 250 vdev_mirror_close(vdev_t *vd) 251 { 252 for (int c = 0; c < vd->vdev_children; c++) 253 vdev_close(vd->vdev_child[c]); 254 } 255 256 static void 257 vdev_mirror_child_done(zio_t *zio) 258 { 259 mirror_child_t *mc = zio->io_private; 260 261 mc->mc_error = zio->io_error; 262 mc->mc_tried = 1; 263 mc->mc_skipped = 0; 264 } 265 266 static void 267 vdev_mirror_scrub_done(zio_t *zio) 268 { 269 mirror_child_t *mc = zio->io_private; 270 271 if (zio->io_error == 0) { 272 zio_t *pio; 273 274 mutex_enter(&zio->io_lock); 275 while ((pio = zio_walk_parents(zio)) != NULL) { 276 mutex_enter(&pio->io_lock); 277 ASSERT3U(zio->io_size, >=, pio->io_size); 278 bcopy(zio->io_data, pio->io_data, pio->io_size); 279 mutex_exit(&pio->io_lock); 280 } 281 mutex_exit(&zio->io_lock); 282 } 283 284 zio_buf_free(zio->io_data, zio->io_size); 285 286 mc->mc_error = zio->io_error; 287 mc->mc_tried = 1; 288 mc->mc_skipped = 0; 289 } 290 291 /* 292 * Check the other, lower-index DVAs to see if they're on the same 293 * vdev as the child we picked. If they are, use them since they 294 * are likely to have been allocated from the primary metaslab in 295 * use at the time, and hence are more likely to have locality with 296 * single-copy data. 297 */ 298 static int 299 vdev_mirror_dva_select(zio_t *zio, int preferred) 300 { 301 dva_t *dva = zio->io_bp->blk_dva; 302 mirror_map_t *mm = zio->io_vsd; 303 int c; 304 305 for (c = preferred - 1; c >= 0; c--) { 306 if (DVA_GET_VDEV(&dva[c]) == DVA_GET_VDEV(&dva[preferred])) 307 preferred = c; 308 } 309 return (preferred); 310 } 311 312 static int 313 vdev_mirror_preferred_child_randomize(zio_t *zio) 314 { 315 mirror_map_t *mm = zio->io_vsd; 316 int p; 317 318 if (mm->mm_root) { 319 p = spa_get_random(mm->mm_preferred_cnt); 320 return (vdev_mirror_dva_select(zio, mm->mm_preferred[p])); 321 } 322 323 /* 324 * To ensure we don't always favour the first matching vdev, 325 * which could lead to wear leveling issues on SSD's, we 326 * use the I/O offset as a pseudo random seed into the vdevs 327 * which have the lowest load. 328 */ 329 p = (zio->io_offset >> vdev_mirror_shift) % mm->mm_preferred_cnt; 330 return (mm->mm_preferred[p]); 331 } 332 333 /* 334 * Try to find a vdev whose DTL doesn't contain the block we want to read 335 * prefering vdevs based on determined load. 336 * 337 * If we can't, try the read on any vdev we haven't already tried. 338 */ 339 static int 340 vdev_mirror_child_select(zio_t *zio) 341 { 342 mirror_map_t *mm = zio->io_vsd; 343 uint64_t txg = zio->io_txg; 344 int c, lowest_load; 345 346 ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg); 347 348 lowest_load = INT_MAX; 349 mm->mm_preferred_cnt = 0; 350 for (c = 0; c < mm->mm_children; c++) { 351 mirror_child_t *mc; 352 353 mc = &mm->mm_child[c]; 354 if (mc->mc_tried || mc->mc_skipped) 355 continue; 356 357 if (!vdev_readable(mc->mc_vd)) { 358 mc->mc_error = SET_ERROR(ENXIO); 359 mc->mc_tried = 1; /* don't even try */ 360 mc->mc_skipped = 1; 361 continue; 362 } 363 364 if (vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) { 365 mc->mc_error = SET_ERROR(ESTALE); 366 mc->mc_skipped = 1; 367 mc->mc_speculative = 1; 368 continue; 369 } 370 371 mc->mc_load = vdev_mirror_load(mm, mc->mc_vd, mc->mc_offset); 372 if (mc->mc_load > lowest_load) 373 continue; 374 375 if (mc->mc_load < lowest_load) { 376 lowest_load = mc->mc_load; 377 mm->mm_preferred_cnt = 0; 378 } 379 mm->mm_preferred[mm->mm_preferred_cnt] = c; 380 mm->mm_preferred_cnt++; 381 } 382 383 if (mm->mm_preferred_cnt == 1) { 384 vdev_queue_register_lastoffset( 385 mm->mm_child[mm->mm_preferred[0]].mc_vd, zio); 386 return (mm->mm_preferred[0]); 387 } 388 389 if (mm->mm_preferred_cnt > 1) { 390 int c = vdev_mirror_preferred_child_randomize(zio); 391 392 vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd, zio); 393 return (c); 394 } 395 396 /* 397 * Every device is either missing or has this txg in its DTL. 398 * Look for any child we haven't already tried before giving up. 399 */ 400 for (c = 0; c < mm->mm_children; c++) { 401 if (!mm->mm_child[c].mc_tried) { 402 vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd, 403 zio); 404 return (c); 405 } 406 } 407 408 /* 409 * Every child failed. There's no place left to look. 410 */ 411 return (-1); 412 } 413 414 static int 415 vdev_mirror_io_start(zio_t *zio) 416 { 417 mirror_map_t *mm; 418 mirror_child_t *mc; 419 int c, children; 420 421 mm = vdev_mirror_map_init(zio); 422 423 if (zio->io_type == ZIO_TYPE_READ) { 424 if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) { 425 /* 426 * For scrubbing reads we need to allocate a read 427 * buffer for each child and issue reads to all 428 * children. If any child succeeds, it will copy its 429 * data into zio->io_data in vdev_mirror_scrub_done. 430 */ 431 for (c = 0; c < mm->mm_children; c++) { 432 mc = &mm->mm_child[c]; 433 zio_nowait(zio_vdev_child_io(zio, zio->io_bp, 434 mc->mc_vd, mc->mc_offset, 435 zio_buf_alloc(zio->io_size), zio->io_size, 436 zio->io_type, zio->io_priority, 0, 437 vdev_mirror_scrub_done, mc)); 438 } 439 return (ZIO_PIPELINE_CONTINUE); 440 } 441 /* 442 * For normal reads just pick one child. 443 */ 444 c = vdev_mirror_child_select(zio); 445 children = (c >= 0); 446 } else { 447 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 448 449 /* 450 * Writes go to all children. 451 */ 452 c = 0; 453 children = mm->mm_children; 454 } 455 456 while (children--) { 457 mc = &mm->mm_child[c]; 458 zio_nowait(zio_vdev_child_io(zio, zio->io_bp, 459 mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size, 460 zio->io_type, zio->io_priority, 0, 461 vdev_mirror_child_done, mc)); 462 c++; 463 } 464 465 return (ZIO_PIPELINE_CONTINUE); 466 } 467 468 static int 469 vdev_mirror_worst_error(mirror_map_t *mm) 470 { 471 int error[2] = { 0, 0 }; 472 473 for (int c = 0; c < mm->mm_children; c++) { 474 mirror_child_t *mc = &mm->mm_child[c]; 475 int s = mc->mc_speculative; 476 error[s] = zio_worst_error(error[s], mc->mc_error); 477 } 478 479 return (error[0] ? error[0] : error[1]); 480 } 481 482 static void 483 vdev_mirror_io_done(zio_t *zio) 484 { 485 mirror_map_t *mm = zio->io_vsd; 486 mirror_child_t *mc; 487 int c; 488 int good_copies = 0; 489 int unexpected_errors = 0; 490 491 for (c = 0; c < mm->mm_children; c++) { 492 mc = &mm->mm_child[c]; 493 494 if (mc->mc_error) { 495 if (!mc->mc_skipped) 496 unexpected_errors++; 497 } else if (mc->mc_tried) { 498 good_copies++; 499 } 500 } 501 502 if (zio->io_type == ZIO_TYPE_WRITE) { 503 /* 504 * XXX -- for now, treat partial writes as success. 505 * 506 * Now that we support write reallocation, it would be better 507 * to treat partial failure as real failure unless there are 508 * no non-degraded top-level vdevs left, and not update DTLs 509 * if we intend to reallocate. 510 */ 511 /* XXPOLICY */ 512 if (good_copies != mm->mm_children) { 513 /* 514 * Always require at least one good copy. 515 * 516 * For ditto blocks (io_vd == NULL), require 517 * all copies to be good. 518 * 519 * XXX -- for replacing vdevs, there's no great answer. 520 * If the old device is really dead, we may not even 521 * be able to access it -- so we only want to 522 * require good writes to the new device. But if 523 * the new device turns out to be flaky, we want 524 * to be able to detach it -- which requires all 525 * writes to the old device to have succeeded. 526 */ 527 if (good_copies == 0 || zio->io_vd == NULL) 528 zio->io_error = vdev_mirror_worst_error(mm); 529 } 530 return; 531 } 532 533 ASSERT(zio->io_type == ZIO_TYPE_READ); 534 535 /* 536 * If we don't have a good copy yet, keep trying other children. 537 */ 538 /* XXPOLICY */ 539 if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) { 540 ASSERT(c >= 0 && c < mm->mm_children); 541 mc = &mm->mm_child[c]; 542 zio_vdev_io_redone(zio); 543 zio_nowait(zio_vdev_child_io(zio, zio->io_bp, 544 mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size, 545 ZIO_TYPE_READ, zio->io_priority, 0, 546 vdev_mirror_child_done, mc)); 547 return; 548 } 549 550 /* XXPOLICY */ 551 if (good_copies == 0) { 552 zio->io_error = vdev_mirror_worst_error(mm); 553 ASSERT(zio->io_error != 0); 554 } 555 556 if (good_copies && spa_writeable(zio->io_spa) && 557 (unexpected_errors || 558 (zio->io_flags & ZIO_FLAG_RESILVER) || 559 ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) { 560 /* 561 * Use the good data we have in hand to repair damaged children. 562 */ 563 for (c = 0; c < mm->mm_children; c++) { 564 /* 565 * Don't rewrite known good children. 566 * Not only is it unnecessary, it could 567 * actually be harmful: if the system lost 568 * power while rewriting the only good copy, 569 * there would be no good copies left! 570 */ 571 mc = &mm->mm_child[c]; 572 573 if (mc->mc_error == 0) { 574 if (mc->mc_tried) 575 continue; 576 if (!(zio->io_flags & ZIO_FLAG_SCRUB) && 577 !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL, 578 zio->io_txg, 1)) 579 continue; 580 mc->mc_error = SET_ERROR(ESTALE); 581 } 582 583 zio_nowait(zio_vdev_child_io(zio, zio->io_bp, 584 mc->mc_vd, mc->mc_offset, 585 zio->io_data, zio->io_size, 586 ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE, 587 ZIO_FLAG_IO_REPAIR | (unexpected_errors ? 588 ZIO_FLAG_SELF_HEAL : 0), NULL, NULL)); 589 } 590 } 591 } 592 593 static void 594 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded) 595 { 596 if (faulted == vd->vdev_children) 597 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 598 VDEV_AUX_NO_REPLICAS); 599 else if (degraded + faulted != 0) 600 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE); 601 else 602 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE); 603 } 604 605 vdev_ops_t vdev_mirror_ops = { 606 vdev_mirror_open, 607 vdev_mirror_close, 608 vdev_default_asize, 609 vdev_mirror_io_start, 610 vdev_mirror_io_done, 611 vdev_mirror_state_change, 612 NULL, 613 NULL, 614 VDEV_TYPE_MIRROR, /* name of this vdev type */ 615 B_FALSE /* not a leaf vdev */ 616 }; 617 618 vdev_ops_t vdev_replacing_ops = { 619 vdev_mirror_open, 620 vdev_mirror_close, 621 vdev_default_asize, 622 vdev_mirror_io_start, 623 vdev_mirror_io_done, 624 vdev_mirror_state_change, 625 NULL, 626 NULL, 627 VDEV_TYPE_REPLACING, /* name of this vdev type */ 628 B_FALSE /* not a leaf vdev */ 629 }; 630 631 vdev_ops_t vdev_spare_ops = { 632 vdev_mirror_open, 633 vdev_mirror_close, 634 vdev_default_asize, 635 vdev_mirror_io_start, 636 vdev_mirror_io_done, 637 vdev_mirror_state_change, 638 NULL, 639 NULL, 640 VDEV_TYPE_SPARE, /* name of this vdev type */ 641 B_FALSE /* not a leaf vdev */ 642 };