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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2012 by Delphix. All rights reserved. 24 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved. 25 */ 26 27 #include <sys/zfs_context.h> 28 #include <sys/fm/fs/zfs.h> 29 #include <sys/spa.h> 30 #include <sys/txg.h> 31 #include <sys/spa_impl.h> 32 #include <sys/vdev_impl.h> 33 #include <sys/zio_impl.h> 34 #include <sys/zio_compress.h> 35 #include <sys/zio_checksum.h> 36 #include <sys/dmu_objset.h> 37 #include <sys/arc.h> 38 #include <sys/ddt.h> 39 40 /* 41 * ========================================================================== 42 * I/O priority table 43 * ========================================================================== 44 */ 45 uint8_t zio_priority_table[ZIO_PRIORITY_TABLE_SIZE] = { 46 0, /* ZIO_PRIORITY_NOW */ 47 0, /* ZIO_PRIORITY_SYNC_READ */ 48 0, /* ZIO_PRIORITY_SYNC_WRITE */ 49 0, /* ZIO_PRIORITY_LOG_WRITE */ 50 1, /* ZIO_PRIORITY_CACHE_FILL */ 51 1, /* ZIO_PRIORITY_AGG */ 52 4, /* ZIO_PRIORITY_FREE */ 53 4, /* ZIO_PRIORITY_ASYNC_WRITE */ 54 6, /* ZIO_PRIORITY_ASYNC_READ */ 55 10, /* ZIO_PRIORITY_RESILVER */ 56 20, /* ZIO_PRIORITY_SCRUB */ 57 2, /* ZIO_PRIORITY_DDT_PREFETCH */ 58 }; 59 60 /* 61 * ========================================================================== 62 * I/O type descriptions 63 * ========================================================================== 64 */ 65 char *zio_type_name[ZIO_TYPES] = { 66 "zio_null", "zio_read", "zio_write", "zio_free", "zio_claim", 67 "zio_ioctl" 68 }; 69 70 /* 71 * ========================================================================== 72 * I/O kmem caches 73 * ========================================================================== 74 */ 75 kmem_cache_t *zio_cache; 76 kmem_cache_t *zio_link_cache; 77 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 78 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 79 80 #ifdef _KERNEL 81 extern vmem_t *zio_alloc_arena; 82 #endif 83 extern int zfs_mg_alloc_failures; 84 85 /* 86 * An allocating zio is one that either currently has the DVA allocate 87 * stage set or will have it later in its lifetime. 88 */ 89 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE) 90 91 boolean_t zio_requeue_io_start_cut_in_line = B_TRUE; 92 93 #ifdef ZFS_DEBUG 94 int zio_buf_debug_limit = 16384; 95 #else 96 int zio_buf_debug_limit = 0; 97 #endif 98 99 void 100 zio_init(void) 101 { 102 size_t c; 103 vmem_t *data_alloc_arena = NULL; 104 105 #ifdef _KERNEL 106 data_alloc_arena = zio_alloc_arena; 107 #endif 108 zio_cache = kmem_cache_create("zio_cache", 109 sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 110 zio_link_cache = kmem_cache_create("zio_link_cache", 111 sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 112 113 /* 114 * For small buffers, we want a cache for each multiple of 115 * SPA_MINBLOCKSIZE. For medium-size buffers, we want a cache 116 * for each quarter-power of 2. For large buffers, we want 117 * a cache for each multiple of PAGESIZE. 118 */ 119 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) { 120 size_t size = (c + 1) << SPA_MINBLOCKSHIFT; 121 size_t p2 = size; 122 size_t align = 0; 123 size_t cflags = (size > zio_buf_debug_limit) ? KMC_NODEBUG : 0; 124 125 while (p2 & (p2 - 1)) 126 p2 &= p2 - 1; 127 128 #ifndef _KERNEL 129 /* 130 * If we are using watchpoints, put each buffer on its own page, 131 * to eliminate the performance overhead of trapping to the 132 * kernel when modifying a non-watched buffer that shares the 133 * page with a watched buffer. 134 */ 135 if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE)) 136 continue; 137 #endif 138 if (size <= 4 * SPA_MINBLOCKSIZE) { 139 align = SPA_MINBLOCKSIZE; 140 } else if (IS_P2ALIGNED(size, PAGESIZE)) { 141 align = PAGESIZE; 142 } else if (IS_P2ALIGNED(size, p2 >> 2)) { 143 align = p2 >> 2; 144 } 145 146 if (align != 0) { 147 char name[36]; 148 (void) sprintf(name, "zio_buf_%lu", (ulong_t)size); 149 zio_buf_cache[c] = kmem_cache_create(name, size, 150 align, NULL, NULL, NULL, NULL, NULL, cflags); 151 152 /* 153 * Since zio_data bufs do not appear in crash dumps, we 154 * pass KMC_NOTOUCH so that no allocator metadata is 155 * stored with the buffers. 156 */ 157 (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size); 158 zio_data_buf_cache[c] = kmem_cache_create(name, size, 159 align, NULL, NULL, NULL, NULL, data_alloc_arena, 160 cflags | KMC_NOTOUCH); 161 } 162 } 163 164 while (--c != 0) { 165 ASSERT(zio_buf_cache[c] != NULL); 166 if (zio_buf_cache[c - 1] == NULL) 167 zio_buf_cache[c - 1] = zio_buf_cache[c]; 168 169 ASSERT(zio_data_buf_cache[c] != NULL); 170 if (zio_data_buf_cache[c - 1] == NULL) 171 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c]; 172 } 173 174 /* 175 * The zio write taskqs have 1 thread per cpu, allow 1/2 of the taskqs 176 * to fail 3 times per txg or 8 failures, whichever is greater. 177 */ 178 zfs_mg_alloc_failures = MAX((3 * max_ncpus / 2), 8); 179 180 zio_inject_init(); 181 } 182 183 void 184 zio_fini(void) 185 { 186 size_t c; 187 kmem_cache_t *last_cache = NULL; 188 kmem_cache_t *last_data_cache = NULL; 189 190 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) { 191 if (zio_buf_cache[c] != last_cache) { 192 last_cache = zio_buf_cache[c]; 193 kmem_cache_destroy(zio_buf_cache[c]); 194 } 195 zio_buf_cache[c] = NULL; 196 197 if (zio_data_buf_cache[c] != last_data_cache) { 198 last_data_cache = zio_data_buf_cache[c]; 199 kmem_cache_destroy(zio_data_buf_cache[c]); 200 } 201 zio_data_buf_cache[c] = NULL; 202 } 203 204 kmem_cache_destroy(zio_link_cache); 205 kmem_cache_destroy(zio_cache); 206 207 zio_inject_fini(); 208 } 209 210 /* 211 * ========================================================================== 212 * Allocate and free I/O buffers 213 * ========================================================================== 214 */ 215 216 /* 217 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a 218 * crashdump if the kernel panics, so use it judiciously. Obviously, it's 219 * useful to inspect ZFS metadata, but if possible, we should avoid keeping 220 * excess / transient data in-core during a crashdump. 221 */ 222 void * 223 zio_buf_alloc(size_t size) 224 { 225 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 226 227 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 228 229 return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE)); 230 } 231 232 /* 233 * Use zio_data_buf_alloc to allocate data. The data will not appear in a 234 * crashdump if the kernel panics. This exists so that we will limit the amount 235 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount 236 * of kernel heap dumped to disk when the kernel panics) 237 */ 238 void * 239 zio_data_buf_alloc(size_t size) 240 { 241 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 242 243 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 244 245 return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE)); 246 } 247 248 void 249 zio_buf_free(void *buf, size_t size) 250 { 251 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 252 253 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 254 255 kmem_cache_free(zio_buf_cache[c], buf); 256 } 257 258 void 259 zio_data_buf_free(void *buf, size_t size) 260 { 261 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 262 263 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 264 265 kmem_cache_free(zio_data_buf_cache[c], buf); 266 } 267 268 /* 269 * ========================================================================== 270 * Push and pop I/O transform buffers 271 * ========================================================================== 272 */ 273 static void 274 zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize, 275 zio_transform_func_t *transform) 276 { 277 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP); 278 279 zt->zt_orig_data = zio->io_data; 280 zt->zt_orig_size = zio->io_size; 281 zt->zt_bufsize = bufsize; 282 zt->zt_transform = transform; 283 284 zt->zt_next = zio->io_transform_stack; 285 zio->io_transform_stack = zt; 286 287 zio->io_data = data; 288 zio->io_size = size; 289 } 290 291 static void 292 zio_pop_transforms(zio_t *zio) 293 { 294 zio_transform_t *zt; 295 296 while ((zt = zio->io_transform_stack) != NULL) { 297 if (zt->zt_transform != NULL) 298 zt->zt_transform(zio, 299 zt->zt_orig_data, zt->zt_orig_size); 300 301 if (zt->zt_bufsize != 0) 302 zio_buf_free(zio->io_data, zt->zt_bufsize); 303 304 zio->io_data = zt->zt_orig_data; 305 zio->io_size = zt->zt_orig_size; 306 zio->io_transform_stack = zt->zt_next; 307 308 kmem_free(zt, sizeof (zio_transform_t)); 309 } 310 } 311 312 /* 313 * ========================================================================== 314 * I/O transform callbacks for subblocks and decompression 315 * ========================================================================== 316 */ 317 static void 318 zio_subblock(zio_t *zio, void *data, uint64_t size) 319 { 320 ASSERT(zio->io_size > size); 321 322 if (zio->io_type == ZIO_TYPE_READ) 323 bcopy(zio->io_data, data, size); 324 } 325 326 static void 327 zio_decompress(zio_t *zio, void *data, uint64_t size) 328 { 329 if (zio->io_error == 0 && 330 zio_decompress_data(BP_GET_COMPRESS(zio->io_bp), 331 zio->io_data, data, zio->io_size, size) != 0) 332 zio->io_error = EIO; 333 } 334 335 /* 336 * ========================================================================== 337 * I/O parent/child relationships and pipeline interlocks 338 * ========================================================================== 339 */ 340 /* 341 * NOTE - Callers to zio_walk_parents() and zio_walk_children must 342 * continue calling these functions until they return NULL. 343 * Otherwise, the next caller will pick up the list walk in 344 * some indeterminate state. (Otherwise every caller would 345 * have to pass in a cookie to keep the state represented by 346 * io_walk_link, which gets annoying.) 347 */ 348 zio_t * 349 zio_walk_parents(zio_t *cio) 350 { 351 zio_link_t *zl = cio->io_walk_link; 352 list_t *pl = &cio->io_parent_list; 353 354 zl = (zl == NULL) ? list_head(pl) : list_next(pl, zl); 355 cio->io_walk_link = zl; 356 357 if (zl == NULL) 358 return (NULL); 359 360 ASSERT(zl->zl_child == cio); 361 return (zl->zl_parent); 362 } 363 364 zio_t * 365 zio_walk_children(zio_t *pio) 366 { 367 zio_link_t *zl = pio->io_walk_link; 368 list_t *cl = &pio->io_child_list; 369 370 zl = (zl == NULL) ? list_head(cl) : list_next(cl, zl); 371 pio->io_walk_link = zl; 372 373 if (zl == NULL) 374 return (NULL); 375 376 ASSERT(zl->zl_parent == pio); 377 return (zl->zl_child); 378 } 379 380 zio_t * 381 zio_unique_parent(zio_t *cio) 382 { 383 zio_t *pio = zio_walk_parents(cio); 384 385 VERIFY(zio_walk_parents(cio) == NULL); 386 return (pio); 387 } 388 389 void 390 zio_add_child(zio_t *pio, zio_t *cio) 391 { 392 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP); 393 394 /* 395 * Logical I/Os can have logical, gang, or vdev children. 396 * Gang I/Os can have gang or vdev children. 397 * Vdev I/Os can only have vdev children. 398 * The following ASSERT captures all of these constraints. 399 */ 400 ASSERT(cio->io_child_type <= pio->io_child_type); 401 402 zl->zl_parent = pio; 403 zl->zl_child = cio; 404 405 mutex_enter(&cio->io_lock); 406 mutex_enter(&pio->io_lock); 407 408 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0); 409 410 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 411 pio->io_children[cio->io_child_type][w] += !cio->io_state[w]; 412 413 list_insert_head(&pio->io_child_list, zl); 414 list_insert_head(&cio->io_parent_list, zl); 415 416 pio->io_child_count++; 417 cio->io_parent_count++; 418 419 mutex_exit(&pio->io_lock); 420 mutex_exit(&cio->io_lock); 421 } 422 423 static void 424 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl) 425 { 426 ASSERT(zl->zl_parent == pio); 427 ASSERT(zl->zl_child == cio); 428 429 mutex_enter(&cio->io_lock); 430 mutex_enter(&pio->io_lock); 431 432 list_remove(&pio->io_child_list, zl); 433 list_remove(&cio->io_parent_list, zl); 434 435 pio->io_child_count--; 436 cio->io_parent_count--; 437 438 mutex_exit(&pio->io_lock); 439 mutex_exit(&cio->io_lock); 440 441 kmem_cache_free(zio_link_cache, zl); 442 } 443 444 static boolean_t 445 zio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait) 446 { 447 uint64_t *countp = &zio->io_children[child][wait]; 448 boolean_t waiting = B_FALSE; 449 450 mutex_enter(&zio->io_lock); 451 ASSERT(zio->io_stall == NULL); 452 if (*countp != 0) { 453 zio->io_stage >>= 1; 454 zio->io_stall = countp; 455 waiting = B_TRUE; 456 } 457 mutex_exit(&zio->io_lock); 458 459 return (waiting); 460 } 461 462 static void 463 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait) 464 { 465 uint64_t *countp = &pio->io_children[zio->io_child_type][wait]; 466 int *errorp = &pio->io_child_error[zio->io_child_type]; 467 468 mutex_enter(&pio->io_lock); 469 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 470 *errorp = zio_worst_error(*errorp, zio->io_error); 471 pio->io_reexecute |= zio->io_reexecute; 472 ASSERT3U(*countp, >, 0); 473 if (--*countp == 0 && pio->io_stall == countp) { 474 pio->io_stall = NULL; 475 mutex_exit(&pio->io_lock); 476 zio_execute(pio); 477 } else { 478 mutex_exit(&pio->io_lock); 479 } 480 } 481 482 static void 483 zio_inherit_child_errors(zio_t *zio, enum zio_child c) 484 { 485 if (zio->io_child_error[c] != 0 && zio->io_error == 0) 486 zio->io_error = zio->io_child_error[c]; 487 } 488 489 /* 490 * ========================================================================== 491 * Create the various types of I/O (read, write, free, etc) 492 * ========================================================================== 493 */ 494 static zio_t * 495 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 496 void *data, uint64_t size, zio_done_func_t *done, void *private, 497 zio_type_t type, int priority, enum zio_flag flags, 498 vdev_t *vd, uint64_t offset, const zbookmark_t *zb, 499 enum zio_stage stage, enum zio_stage pipeline) 500 { 501 zio_t *zio; 502 503 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 504 ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0); 505 ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0); 506 507 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER)); 508 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER)); 509 ASSERT(vd || stage == ZIO_STAGE_OPEN); 510 511 zio = kmem_cache_alloc(zio_cache, KM_SLEEP); 512 bzero(zio, sizeof (zio_t)); 513 514 mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL); 515 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL); 516 517 list_create(&zio->io_parent_list, sizeof (zio_link_t), 518 offsetof(zio_link_t, zl_parent_node)); 519 list_create(&zio->io_child_list, sizeof (zio_link_t), 520 offsetof(zio_link_t, zl_child_node)); 521 522 if (vd != NULL) 523 zio->io_child_type = ZIO_CHILD_VDEV; 524 else if (flags & ZIO_FLAG_GANG_CHILD) 525 zio->io_child_type = ZIO_CHILD_GANG; 526 else if (flags & ZIO_FLAG_DDT_CHILD) 527 zio->io_child_type = ZIO_CHILD_DDT; 528 else 529 zio->io_child_type = ZIO_CHILD_LOGICAL; 530 531 if (bp != NULL) { 532 zio->io_bp = (blkptr_t *)bp; 533 zio->io_bp_copy = *bp; 534 zio->io_bp_orig = *bp; 535 if (type != ZIO_TYPE_WRITE || 536 zio->io_child_type == ZIO_CHILD_DDT) 537 zio->io_bp = &zio->io_bp_copy; /* so caller can free */ 538 if (zio->io_child_type == ZIO_CHILD_LOGICAL) 539 zio->io_logical = zio; 540 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp)) 541 pipeline |= ZIO_GANG_STAGES; 542 } 543 544 zio->io_spa = spa; 545 zio->io_txg = txg; 546 zio->io_done = done; 547 zio->io_private = private; 548 zio->io_type = type; 549 zio->io_priority = priority; 550 zio->io_vd = vd; 551 zio->io_offset = offset; 552 zio->io_orig_data = zio->io_data = data; 553 zio->io_orig_size = zio->io_size = size; 554 zio->io_orig_flags = zio->io_flags = flags; 555 zio->io_orig_stage = zio->io_stage = stage; 556 zio->io_orig_pipeline = zio->io_pipeline = pipeline; 557 558 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY); 559 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE); 560 561 if (zb != NULL) 562 zio->io_bookmark = *zb; 563 564 if (pio != NULL) { 565 if (zio->io_logical == NULL) 566 zio->io_logical = pio->io_logical; 567 if (zio->io_child_type == ZIO_CHILD_GANG) 568 zio->io_gang_leader = pio->io_gang_leader; 569 zio_add_child(pio, zio); 570 } 571 572 return (zio); 573 } 574 575 static void 576 zio_destroy(zio_t *zio) 577 { 578 list_destroy(&zio->io_parent_list); 579 list_destroy(&zio->io_child_list); 580 mutex_destroy(&zio->io_lock); 581 cv_destroy(&zio->io_cv); 582 kmem_cache_free(zio_cache, zio); 583 } 584 585 zio_t * 586 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done, 587 void *private, enum zio_flag flags) 588 { 589 zio_t *zio; 590 591 zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private, 592 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL, 593 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE); 594 595 return (zio); 596 } 597 598 zio_t * 599 zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags) 600 { 601 return (zio_null(NULL, spa, NULL, done, private, flags)); 602 } 603 604 zio_t * 605 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, 606 void *data, uint64_t size, zio_done_func_t *done, void *private, 607 int priority, enum zio_flag flags, const zbookmark_t *zb) 608 { 609 zio_t *zio; 610 611 zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp, 612 data, size, done, private, 613 ZIO_TYPE_READ, priority, flags, NULL, 0, zb, 614 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ? 615 ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE); 616 617 return (zio); 618 } 619 620 zio_t * 621 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 622 void *data, uint64_t size, const zio_prop_t *zp, 623 zio_done_func_t *ready, zio_done_func_t *done, void *private, 624 int priority, enum zio_flag flags, const zbookmark_t *zb) 625 { 626 zio_t *zio; 627 628 ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF && 629 zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS && 630 zp->zp_compress >= ZIO_COMPRESS_OFF && 631 zp->zp_compress < ZIO_COMPRESS_FUNCTIONS && 632 DMU_OT_IS_VALID(zp->zp_type) && 633 zp->zp_level < 32 && 634 zp->zp_copies > 0 && 635 zp->zp_copies <= spa_max_replication(spa) && 636 zp->zp_dedup <= 1 && 637 zp->zp_dedup_verify <= 1); 638 639 zio = zio_create(pio, spa, txg, bp, data, size, done, private, 640 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb, 641 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ? 642 ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE); 643 644 zio->io_ready = ready; 645 zio->io_prop = *zp; 646 647 return (zio); 648 } 649 650 zio_t * 651 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, void *data, 652 uint64_t size, zio_done_func_t *done, void *private, int priority, 653 enum zio_flag flags, zbookmark_t *zb) 654 { 655 zio_t *zio; 656 657 zio = zio_create(pio, spa, txg, bp, data, size, done, private, 658 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb, 659 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE); 660 661 return (zio); 662 } 663 664 void 665 zio_write_override(zio_t *zio, blkptr_t *bp, int copies) 666 { 667 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 668 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 669 ASSERT(zio->io_stage == ZIO_STAGE_OPEN); 670 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa)); 671 672 zio->io_prop.zp_copies = copies; 673 zio->io_bp_override = bp; 674 } 675 676 void 677 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp) 678 { 679 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp); 680 } 681 682 zio_t * 683 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 684 enum zio_flag flags) 685 { 686 zio_t *zio; 687 688 dprintf_bp(bp, "freeing in txg %llu, pass %u", 689 (longlong_t)txg, spa->spa_sync_pass); 690 691 ASSERT(!BP_IS_HOLE(bp)); 692 ASSERT(spa_syncing_txg(spa) == txg); 693 ASSERT(spa_sync_pass(spa) <= SYNC_PASS_DEFERRED_FREE); 694 695 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp), 696 NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_FREE, flags, 697 NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_FREE_PIPELINE); 698 699 return (zio); 700 } 701 702 zio_t * 703 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 704 zio_done_func_t *done, void *private, enum zio_flag flags) 705 { 706 zio_t *zio; 707 708 /* 709 * A claim is an allocation of a specific block. Claims are needed 710 * to support immediate writes in the intent log. The issue is that 711 * immediate writes contain committed data, but in a txg that was 712 * *not* committed. Upon opening the pool after an unclean shutdown, 713 * the intent log claims all blocks that contain immediate write data 714 * so that the SPA knows they're in use. 715 * 716 * All claims *must* be resolved in the first txg -- before the SPA 717 * starts allocating blocks -- so that nothing is allocated twice. 718 * If txg == 0 we just verify that the block is claimable. 719 */ 720 ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa)); 721 ASSERT(txg == spa_first_txg(spa) || txg == 0); 722 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(1M) */ 723 724 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp), 725 done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, flags, 726 NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE); 727 728 return (zio); 729 } 730 731 zio_t * 732 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd, 733 zio_done_func_t *done, void *private, int priority, enum zio_flag flags) 734 { 735 zio_t *zio; 736 int c; 737 738 if (vd->vdev_children == 0) { 739 zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private, 740 ZIO_TYPE_IOCTL, priority, flags, vd, 0, NULL, 741 ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE); 742 743 zio->io_cmd = cmd; 744 } else { 745 zio = zio_null(pio, spa, NULL, NULL, NULL, flags); 746 747 for (c = 0; c < vd->vdev_children; c++) 748 zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd, 749 done, private, priority, flags)); 750 } 751 752 return (zio); 753 } 754 755 zio_t * 756 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 757 void *data, int checksum, zio_done_func_t *done, void *private, 758 int priority, enum zio_flag flags, boolean_t labels) 759 { 760 zio_t *zio; 761 762 ASSERT(vd->vdev_children == 0); 763 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE || 764 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 765 ASSERT3U(offset + size, <=, vd->vdev_psize); 766 767 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private, 768 ZIO_TYPE_READ, priority, flags, vd, offset, NULL, 769 ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE); 770 771 zio->io_prop.zp_checksum = checksum; 772 773 return (zio); 774 } 775 776 zio_t * 777 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 778 void *data, int checksum, zio_done_func_t *done, void *private, 779 int priority, enum zio_flag flags, boolean_t labels) 780 { 781 zio_t *zio; 782 783 ASSERT(vd->vdev_children == 0); 784 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE || 785 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 786 ASSERT3U(offset + size, <=, vd->vdev_psize); 787 788 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private, 789 ZIO_TYPE_WRITE, priority, flags, vd, offset, NULL, 790 ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE); 791 792 zio->io_prop.zp_checksum = checksum; 793 794 if (zio_checksum_table[checksum].ci_eck) { 795 /* 796 * zec checksums are necessarily destructive -- they modify 797 * the end of the write buffer to hold the verifier/checksum. 798 * Therefore, we must make a local copy in case the data is 799 * being written to multiple places in parallel. 800 */ 801 void *wbuf = zio_buf_alloc(size); 802 bcopy(data, wbuf, size); 803 zio_push_transform(zio, wbuf, size, size, NULL); 804 } 805 806 return (zio); 807 } 808 809 /* 810 * Create a child I/O to do some work for us. 811 */ 812 zio_t * 813 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset, 814 void *data, uint64_t size, int type, int priority, enum zio_flag flags, 815 zio_done_func_t *done, void *private) 816 { 817 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE; 818 zio_t *zio; 819 820 ASSERT(vd->vdev_parent == 821 (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev)); 822 823 if (type == ZIO_TYPE_READ && bp != NULL) { 824 /* 825 * If we have the bp, then the child should perform the 826 * checksum and the parent need not. This pushes error 827 * detection as close to the leaves as possible and 828 * eliminates redundant checksums in the interior nodes. 829 */ 830 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY; 831 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY; 832 } 833 834 if (vd->vdev_children == 0) 835 offset += VDEV_LABEL_START_SIZE; 836 837 flags |= ZIO_VDEV_CHILD_FLAGS(pio) | ZIO_FLAG_DONT_PROPAGATE; 838 839 /* 840 * If we've decided to do a repair, the write is not speculative -- 841 * even if the original read was. 842 */ 843 if (flags & ZIO_FLAG_IO_REPAIR) 844 flags &= ~ZIO_FLAG_SPECULATIVE; 845 846 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, 847 done, private, type, priority, flags, vd, offset, &pio->io_bookmark, 848 ZIO_STAGE_VDEV_IO_START >> 1, pipeline); 849 850 return (zio); 851 } 852 853 zio_t * 854 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, void *data, uint64_t size, 855 int type, int priority, enum zio_flag flags, 856 zio_done_func_t *done, void *private) 857 { 858 zio_t *zio; 859 860 ASSERT(vd->vdev_ops->vdev_op_leaf); 861 862 zio = zio_create(NULL, vd->vdev_spa, 0, NULL, 863 data, size, done, private, type, priority, 864 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY, 865 vd, offset, NULL, 866 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE); 867 868 return (zio); 869 } 870 871 void 872 zio_flush(zio_t *zio, vdev_t *vd) 873 { 874 zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE, 875 NULL, NULL, ZIO_PRIORITY_NOW, 876 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY)); 877 } 878 879 void 880 zio_shrink(zio_t *zio, uint64_t size) 881 { 882 ASSERT(zio->io_executor == NULL); 883 ASSERT(zio->io_orig_size == zio->io_size); 884 ASSERT(size <= zio->io_size); 885 886 /* 887 * We don't shrink for raidz because of problems with the 888 * reconstruction when reading back less than the block size. 889 * Note, BP_IS_RAIDZ() assumes no compression. 890 */ 891 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 892 if (!BP_IS_RAIDZ(zio->io_bp)) 893 zio->io_orig_size = zio->io_size = size; 894 } 895 896 /* 897 * ========================================================================== 898 * Prepare to read and write logical blocks 899 * ========================================================================== 900 */ 901 902 static int 903 zio_read_bp_init(zio_t *zio) 904 { 905 blkptr_t *bp = zio->io_bp; 906 907 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF && 908 zio->io_child_type == ZIO_CHILD_LOGICAL && 909 !(zio->io_flags & ZIO_FLAG_RAW)) { 910 uint64_t psize = BP_GET_PSIZE(bp); 911 void *cbuf = zio_buf_alloc(psize); 912 913 zio_push_transform(zio, cbuf, psize, psize, zio_decompress); 914 } 915 916 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0) 917 zio->io_flags |= ZIO_FLAG_DONT_CACHE; 918 919 if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP) 920 zio->io_flags |= ZIO_FLAG_DONT_CACHE; 921 922 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL) 923 zio->io_pipeline = ZIO_DDT_READ_PIPELINE; 924 925 return (ZIO_PIPELINE_CONTINUE); 926 } 927 928 static int 929 zio_write_bp_init(zio_t *zio) 930 { 931 spa_t *spa = zio->io_spa; 932 zio_prop_t *zp = &zio->io_prop; 933 enum zio_compress compress = zp->zp_compress; 934 blkptr_t *bp = zio->io_bp; 935 uint64_t lsize = zio->io_size; 936 uint64_t psize = lsize; 937 int pass = 1; 938 939 /* 940 * If our children haven't all reached the ready stage, 941 * wait for them and then repeat this pipeline stage. 942 */ 943 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) || 944 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY)) 945 return (ZIO_PIPELINE_STOP); 946 947 if (!IO_IS_ALLOCATING(zio)) 948 return (ZIO_PIPELINE_CONTINUE); 949 950 ASSERT(zio->io_child_type != ZIO_CHILD_DDT); 951 952 if (zio->io_bp_override) { 953 ASSERT(bp->blk_birth != zio->io_txg); 954 ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0); 955 956 *bp = *zio->io_bp_override; 957 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 958 959 if (BP_IS_HOLE(bp) || !zp->zp_dedup) 960 return (ZIO_PIPELINE_CONTINUE); 961 962 ASSERT(zio_checksum_table[zp->zp_checksum].ci_dedup || 963 zp->zp_dedup_verify); 964 965 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) { 966 BP_SET_DEDUP(bp, 1); 967 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE; 968 return (ZIO_PIPELINE_CONTINUE); 969 } 970 zio->io_bp_override = NULL; 971 BP_ZERO(bp); 972 } 973 974 if (bp->blk_birth == zio->io_txg) { 975 /* 976 * We're rewriting an existing block, which means we're 977 * working on behalf of spa_sync(). For spa_sync() to 978 * converge, it must eventually be the case that we don't 979 * have to allocate new blocks. But compression changes 980 * the blocksize, which forces a reallocate, and makes 981 * convergence take longer. Therefore, after the first 982 * few passes, stop compressing to ensure convergence. 983 */ 984 pass = spa_sync_pass(spa); 985 986 ASSERT(zio->io_txg == spa_syncing_txg(spa)); 987 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 988 ASSERT(!BP_GET_DEDUP(bp)); 989 990 if (pass > SYNC_PASS_DONT_COMPRESS) 991 compress = ZIO_COMPRESS_OFF; 992 993 /* Make sure someone doesn't change their mind on overwrites */ 994 ASSERT(MIN(zp->zp_copies + BP_IS_GANG(bp), 995 spa_max_replication(spa)) == BP_GET_NDVAS(bp)); 996 } 997 998 if (compress != ZIO_COMPRESS_OFF) { 999 void *cbuf = zio_buf_alloc(lsize); 1000 psize = zio_compress_data(compress, zio->io_data, cbuf, lsize); 1001 if (psize == 0 || psize == lsize) { 1002 compress = ZIO_COMPRESS_OFF; 1003 zio_buf_free(cbuf, lsize); 1004 } else { 1005 ASSERT(psize < lsize); 1006 zio_push_transform(zio, cbuf, psize, lsize, NULL); 1007 } 1008 } 1009 1010 /* 1011 * The final pass of spa_sync() must be all rewrites, but the first 1012 * few passes offer a trade-off: allocating blocks defers convergence, 1013 * but newly allocated blocks are sequential, so they can be written 1014 * to disk faster. Therefore, we allow the first few passes of 1015 * spa_sync() to allocate new blocks, but force rewrites after that. 1016 * There should only be a handful of blocks after pass 1 in any case. 1017 */ 1018 if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == psize && 1019 pass > SYNC_PASS_REWRITE) { 1020 ASSERT(psize != 0); 1021 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES; 1022 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages; 1023 zio->io_flags |= ZIO_FLAG_IO_REWRITE; 1024 } else { 1025 BP_ZERO(bp); 1026 zio->io_pipeline = ZIO_WRITE_PIPELINE; 1027 } 1028 1029 if (psize == 0) { 1030 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1031 } else { 1032 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER); 1033 BP_SET_LSIZE(bp, lsize); 1034 BP_SET_PSIZE(bp, psize); 1035 BP_SET_COMPRESS(bp, compress); 1036 BP_SET_CHECKSUM(bp, zp->zp_checksum); 1037 BP_SET_TYPE(bp, zp->zp_type); 1038 BP_SET_LEVEL(bp, zp->zp_level); 1039 BP_SET_DEDUP(bp, zp->zp_dedup); 1040 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 1041 if (zp->zp_dedup) { 1042 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1043 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 1044 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE; 1045 } 1046 } 1047 1048 return (ZIO_PIPELINE_CONTINUE); 1049 } 1050 1051 static int 1052 zio_free_bp_init(zio_t *zio) 1053 { 1054 blkptr_t *bp = zio->io_bp; 1055 1056 if (zio->io_child_type == ZIO_CHILD_LOGICAL) { 1057 if (BP_GET_DEDUP(bp)) 1058 zio->io_pipeline = ZIO_DDT_FREE_PIPELINE; 1059 } 1060 1061 return (ZIO_PIPELINE_CONTINUE); 1062 } 1063 1064 /* 1065 * ========================================================================== 1066 * Execute the I/O pipeline 1067 * ========================================================================== 1068 */ 1069 1070 static void 1071 zio_taskq_dispatch(zio_t *zio, enum zio_taskq_type q, boolean_t cutinline) 1072 { 1073 spa_t *spa = zio->io_spa; 1074 zio_type_t t = zio->io_type; 1075 int flags = (cutinline ? TQ_FRONT : 0); 1076 1077 /* 1078 * If we're a config writer or a probe, the normal issue and 1079 * interrupt threads may all be blocked waiting for the config lock. 1080 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL. 1081 */ 1082 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE)) 1083 t = ZIO_TYPE_NULL; 1084 1085 /* 1086 * A similar issue exists for the L2ARC write thread until L2ARC 2.0. 1087 */ 1088 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux) 1089 t = ZIO_TYPE_NULL; 1090 1091 /* 1092 * If this is a high priority I/O, then use the high priority taskq. 1093 */ 1094 if (zio->io_priority == ZIO_PRIORITY_NOW && 1095 spa->spa_zio_taskq[t][q + 1] != NULL) 1096 q++; 1097 1098 ASSERT3U(q, <, ZIO_TASKQ_TYPES); 1099 1100 /* 1101 * NB: We are assuming that the zio can only be dispatched 1102 * to a single taskq at a time. It would be a grievous error 1103 * to dispatch the zio to another taskq at the same time. 1104 */ 1105 ASSERT(zio->io_tqent.tqent_next == NULL); 1106 taskq_dispatch_ent(spa->spa_zio_taskq[t][q], 1107 (task_func_t *)zio_execute, zio, flags, &zio->io_tqent); 1108 } 1109 1110 static boolean_t 1111 zio_taskq_member(zio_t *zio, enum zio_taskq_type q) 1112 { 1113 kthread_t *executor = zio->io_executor; 1114 spa_t *spa = zio->io_spa; 1115 1116 for (zio_type_t t = 0; t < ZIO_TYPES; t++) 1117 if (taskq_member(spa->spa_zio_taskq[t][q], executor)) 1118 return (B_TRUE); 1119 1120 return (B_FALSE); 1121 } 1122 1123 static int 1124 zio_issue_async(zio_t *zio) 1125 { 1126 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE); 1127 1128 return (ZIO_PIPELINE_STOP); 1129 } 1130 1131 void 1132 zio_interrupt(zio_t *zio) 1133 { 1134 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE); 1135 } 1136 1137 /* 1138 * Execute the I/O pipeline until one of the following occurs: 1139 * (1) the I/O completes; (2) the pipeline stalls waiting for 1140 * dependent child I/Os; (3) the I/O issues, so we're waiting 1141 * for an I/O completion interrupt; (4) the I/O is delegated by 1142 * vdev-level caching or aggregation; (5) the I/O is deferred 1143 * due to vdev-level queueing; (6) the I/O is handed off to 1144 * another thread. In all cases, the pipeline stops whenever 1145 * there's no CPU work; it never burns a thread in cv_wait(). 1146 * 1147 * There's no locking on io_stage because there's no legitimate way 1148 * for multiple threads to be attempting to process the same I/O. 1149 */ 1150 static zio_pipe_stage_t *zio_pipeline[]; 1151 1152 void 1153 zio_execute(zio_t *zio) 1154 { 1155 zio->io_executor = curthread; 1156 1157 while (zio->io_stage < ZIO_STAGE_DONE) { 1158 enum zio_stage pipeline = zio->io_pipeline; 1159 enum zio_stage stage = zio->io_stage; 1160 int rv; 1161 1162 ASSERT(!MUTEX_HELD(&zio->io_lock)); 1163 ASSERT(ISP2(stage)); 1164 ASSERT(zio->io_stall == NULL); 1165 1166 do { 1167 stage <<= 1; 1168 } while ((stage & pipeline) == 0); 1169 1170 ASSERT(stage <= ZIO_STAGE_DONE); 1171 1172 /* 1173 * If we are in interrupt context and this pipeline stage 1174 * will grab a config lock that is held across I/O, 1175 * or may wait for an I/O that needs an interrupt thread 1176 * to complete, issue async to avoid deadlock. 1177 * 1178 * For VDEV_IO_START, we cut in line so that the io will 1179 * be sent to disk promptly. 1180 */ 1181 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL && 1182 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) { 1183 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ? 1184 zio_requeue_io_start_cut_in_line : B_FALSE; 1185 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut); 1186 return; 1187 } 1188 1189 zio->io_stage = stage; 1190 rv = zio_pipeline[highbit(stage) - 1](zio); 1191 1192 if (rv == ZIO_PIPELINE_STOP) 1193 return; 1194 1195 ASSERT(rv == ZIO_PIPELINE_CONTINUE); 1196 } 1197 } 1198 1199 /* 1200 * ========================================================================== 1201 * Initiate I/O, either sync or async 1202 * ========================================================================== 1203 */ 1204 int 1205 zio_wait(zio_t *zio) 1206 { 1207 int error; 1208 1209 ASSERT(zio->io_stage == ZIO_STAGE_OPEN); 1210 ASSERT(zio->io_executor == NULL); 1211 1212 zio->io_waiter = curthread; 1213 1214 zio_execute(zio); 1215 1216 mutex_enter(&zio->io_lock); 1217 while (zio->io_executor != NULL) 1218 cv_wait(&zio->io_cv, &zio->io_lock); 1219 mutex_exit(&zio->io_lock); 1220 1221 error = zio->io_error; 1222 zio_destroy(zio); 1223 1224 return (error); 1225 } 1226 1227 void 1228 zio_nowait(zio_t *zio) 1229 { 1230 ASSERT(zio->io_executor == NULL); 1231 1232 if (zio->io_child_type == ZIO_CHILD_LOGICAL && 1233 zio_unique_parent(zio) == NULL) { 1234 /* 1235 * This is a logical async I/O with no parent to wait for it. 1236 * We add it to the spa_async_root_zio "Godfather" I/O which 1237 * will ensure they complete prior to unloading the pool. 1238 */ 1239 spa_t *spa = zio->io_spa; 1240 1241 zio_add_child(spa->spa_async_zio_root, zio); 1242 } 1243 1244 zio_execute(zio); 1245 } 1246 1247 /* 1248 * ========================================================================== 1249 * Reexecute or suspend/resume failed I/O 1250 * ========================================================================== 1251 */ 1252 1253 static void 1254 zio_reexecute(zio_t *pio) 1255 { 1256 zio_t *cio, *cio_next; 1257 1258 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL); 1259 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN); 1260 ASSERT(pio->io_gang_leader == NULL); 1261 ASSERT(pio->io_gang_tree == NULL); 1262 1263 pio->io_flags = pio->io_orig_flags; 1264 pio->io_stage = pio->io_orig_stage; 1265 pio->io_pipeline = pio->io_orig_pipeline; 1266 pio->io_reexecute = 0; 1267 pio->io_error = 0; 1268 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 1269 pio->io_state[w] = 0; 1270 for (int c = 0; c < ZIO_CHILD_TYPES; c++) 1271 pio->io_child_error[c] = 0; 1272 1273 if (IO_IS_ALLOCATING(pio)) 1274 BP_ZERO(pio->io_bp); 1275 1276 /* 1277 * As we reexecute pio's children, new children could be created. 1278 * New children go to the head of pio's io_child_list, however, 1279 * so we will (correctly) not reexecute them. The key is that 1280 * the remainder of pio's io_child_list, from 'cio_next' onward, 1281 * cannot be affected by any side effects of reexecuting 'cio'. 1282 */ 1283 for (cio = zio_walk_children(pio); cio != NULL; cio = cio_next) { 1284 cio_next = zio_walk_children(pio); 1285 mutex_enter(&pio->io_lock); 1286 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 1287 pio->io_children[cio->io_child_type][w]++; 1288 mutex_exit(&pio->io_lock); 1289 zio_reexecute(cio); 1290 } 1291 1292 /* 1293 * Now that all children have been reexecuted, execute the parent. 1294 * We don't reexecute "The Godfather" I/O here as it's the 1295 * responsibility of the caller to wait on him. 1296 */ 1297 if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) 1298 zio_execute(pio); 1299 } 1300 1301 void 1302 zio_suspend(spa_t *spa, zio_t *zio) 1303 { 1304 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC) 1305 fm_panic("Pool '%s' has encountered an uncorrectable I/O " 1306 "failure and the failure mode property for this pool " 1307 "is set to panic.", spa_name(spa)); 1308 1309 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0); 1310 1311 mutex_enter(&spa->spa_suspend_lock); 1312 1313 if (spa->spa_suspend_zio_root == NULL) 1314 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL, 1315 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 1316 ZIO_FLAG_GODFATHER); 1317 1318 spa->spa_suspended = B_TRUE; 1319 1320 if (zio != NULL) { 1321 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER)); 1322 ASSERT(zio != spa->spa_suspend_zio_root); 1323 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1324 ASSERT(zio_unique_parent(zio) == NULL); 1325 ASSERT(zio->io_stage == ZIO_STAGE_DONE); 1326 zio_add_child(spa->spa_suspend_zio_root, zio); 1327 } 1328 1329 mutex_exit(&spa->spa_suspend_lock); 1330 } 1331 1332 int 1333 zio_resume(spa_t *spa) 1334 { 1335 zio_t *pio; 1336 1337 /* 1338 * Reexecute all previously suspended i/o. 1339 */ 1340 mutex_enter(&spa->spa_suspend_lock); 1341 spa->spa_suspended = B_FALSE; 1342 cv_broadcast(&spa->spa_suspend_cv); 1343 pio = spa->spa_suspend_zio_root; 1344 spa->spa_suspend_zio_root = NULL; 1345 mutex_exit(&spa->spa_suspend_lock); 1346 1347 if (pio == NULL) 1348 return (0); 1349 1350 zio_reexecute(pio); 1351 return (zio_wait(pio)); 1352 } 1353 1354 void 1355 zio_resume_wait(spa_t *spa) 1356 { 1357 mutex_enter(&spa->spa_suspend_lock); 1358 while (spa_suspended(spa)) 1359 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock); 1360 mutex_exit(&spa->spa_suspend_lock); 1361 } 1362 1363 /* 1364 * ========================================================================== 1365 * Gang blocks. 1366 * 1367 * A gang block is a collection of small blocks that looks to the DMU 1368 * like one large block. When zio_dva_allocate() cannot find a block 1369 * of the requested size, due to either severe fragmentation or the pool 1370 * being nearly full, it calls zio_write_gang_block() to construct the 1371 * block from smaller fragments. 1372 * 1373 * A gang block consists of a gang header (zio_gbh_phys_t) and up to 1374 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like 1375 * an indirect block: it's an array of block pointers. It consumes 1376 * only one sector and hence is allocatable regardless of fragmentation. 1377 * The gang header's bps point to its gang members, which hold the data. 1378 * 1379 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg> 1380 * as the verifier to ensure uniqueness of the SHA256 checksum. 1381 * Critically, the gang block bp's blk_cksum is the checksum of the data, 1382 * not the gang header. This ensures that data block signatures (needed for 1383 * deduplication) are independent of how the block is physically stored. 1384 * 1385 * Gang blocks can be nested: a gang member may itself be a gang block. 1386 * Thus every gang block is a tree in which root and all interior nodes are 1387 * gang headers, and the leaves are normal blocks that contain user data. 1388 * The root of the gang tree is called the gang leader. 1389 * 1390 * To perform any operation (read, rewrite, free, claim) on a gang block, 1391 * zio_gang_assemble() first assembles the gang tree (minus data leaves) 1392 * in the io_gang_tree field of the original logical i/o by recursively 1393 * reading the gang leader and all gang headers below it. This yields 1394 * an in-core tree containing the contents of every gang header and the 1395 * bps for every constituent of the gang block. 1396 * 1397 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree 1398 * and invokes a callback on each bp. To free a gang block, zio_gang_issue() 1399 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp. 1400 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim(). 1401 * zio_read_gang() is a wrapper around zio_read() that omits reading gang 1402 * headers, since we already have those in io_gang_tree. zio_rewrite_gang() 1403 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite() 1404 * of the gang header plus zio_checksum_compute() of the data to update the 1405 * gang header's blk_cksum as described above. 1406 * 1407 * The two-phase assemble/issue model solves the problem of partial failure -- 1408 * what if you'd freed part of a gang block but then couldn't read the 1409 * gang header for another part? Assembling the entire gang tree first 1410 * ensures that all the necessary gang header I/O has succeeded before 1411 * starting the actual work of free, claim, or write. Once the gang tree 1412 * is assembled, free and claim are in-memory operations that cannot fail. 1413 * 1414 * In the event that a gang write fails, zio_dva_unallocate() walks the 1415 * gang tree to immediately free (i.e. insert back into the space map) 1416 * everything we've allocated. This ensures that we don't get ENOSPC 1417 * errors during repeated suspend/resume cycles due to a flaky device. 1418 * 1419 * Gang rewrites only happen during sync-to-convergence. If we can't assemble 1420 * the gang tree, we won't modify the block, so we can safely defer the free 1421 * (knowing that the block is still intact). If we *can* assemble the gang 1422 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free 1423 * each constituent bp and we can allocate a new block on the next sync pass. 1424 * 1425 * In all cases, the gang tree allows complete recovery from partial failure. 1426 * ========================================================================== 1427 */ 1428 1429 static zio_t * 1430 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data) 1431 { 1432 if (gn != NULL) 1433 return (pio); 1434 1435 return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp), 1436 NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), 1437 &pio->io_bookmark)); 1438 } 1439 1440 zio_t * 1441 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data) 1442 { 1443 zio_t *zio; 1444 1445 if (gn != NULL) { 1446 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp, 1447 gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority, 1448 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 1449 /* 1450 * As we rewrite each gang header, the pipeline will compute 1451 * a new gang block header checksum for it; but no one will 1452 * compute a new data checksum, so we do that here. The one 1453 * exception is the gang leader: the pipeline already computed 1454 * its data checksum because that stage precedes gang assembly. 1455 * (Presently, nothing actually uses interior data checksums; 1456 * this is just good hygiene.) 1457 */ 1458 if (gn != pio->io_gang_leader->io_gang_tree) { 1459 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp), 1460 data, BP_GET_PSIZE(bp)); 1461 } 1462 /* 1463 * If we are here to damage data for testing purposes, 1464 * leave the GBH alone so that we can detect the damage. 1465 */ 1466 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE) 1467 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES; 1468 } else { 1469 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp, 1470 data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority, 1471 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 1472 } 1473 1474 return (zio); 1475 } 1476 1477 /* ARGSUSED */ 1478 zio_t * 1479 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data) 1480 { 1481 return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp, 1482 ZIO_GANG_CHILD_FLAGS(pio))); 1483 } 1484 1485 /* ARGSUSED */ 1486 zio_t * 1487 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data) 1488 { 1489 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp, 1490 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio))); 1491 } 1492 1493 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = { 1494 NULL, 1495 zio_read_gang, 1496 zio_rewrite_gang, 1497 zio_free_gang, 1498 zio_claim_gang, 1499 NULL 1500 }; 1501 1502 static void zio_gang_tree_assemble_done(zio_t *zio); 1503 1504 static zio_gang_node_t * 1505 zio_gang_node_alloc(zio_gang_node_t **gnpp) 1506 { 1507 zio_gang_node_t *gn; 1508 1509 ASSERT(*gnpp == NULL); 1510 1511 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP); 1512 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE); 1513 *gnpp = gn; 1514 1515 return (gn); 1516 } 1517 1518 static void 1519 zio_gang_node_free(zio_gang_node_t **gnpp) 1520 { 1521 zio_gang_node_t *gn = *gnpp; 1522 1523 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) 1524 ASSERT(gn->gn_child[g] == NULL); 1525 1526 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE); 1527 kmem_free(gn, sizeof (*gn)); 1528 *gnpp = NULL; 1529 } 1530 1531 static void 1532 zio_gang_tree_free(zio_gang_node_t **gnpp) 1533 { 1534 zio_gang_node_t *gn = *gnpp; 1535 1536 if (gn == NULL) 1537 return; 1538 1539 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) 1540 zio_gang_tree_free(&gn->gn_child[g]); 1541 1542 zio_gang_node_free(gnpp); 1543 } 1544 1545 static void 1546 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp) 1547 { 1548 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp); 1549 1550 ASSERT(gio->io_gang_leader == gio); 1551 ASSERT(BP_IS_GANG(bp)); 1552 1553 zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh, 1554 SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn, 1555 gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark)); 1556 } 1557 1558 static void 1559 zio_gang_tree_assemble_done(zio_t *zio) 1560 { 1561 zio_t *gio = zio->io_gang_leader; 1562 zio_gang_node_t *gn = zio->io_private; 1563 blkptr_t *bp = zio->io_bp; 1564 1565 ASSERT(gio == zio_unique_parent(zio)); 1566 ASSERT(zio->io_child_count == 0); 1567 1568 if (zio->io_error) 1569 return; 1570 1571 if (BP_SHOULD_BYTESWAP(bp)) 1572 byteswap_uint64_array(zio->io_data, zio->io_size); 1573 1574 ASSERT(zio->io_data == gn->gn_gbh); 1575 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE); 1576 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC); 1577 1578 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 1579 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g]; 1580 if (!BP_IS_GANG(gbp)) 1581 continue; 1582 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]); 1583 } 1584 } 1585 1586 static void 1587 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data) 1588 { 1589 zio_t *gio = pio->io_gang_leader; 1590 zio_t *zio; 1591 1592 ASSERT(BP_IS_GANG(bp) == !!gn); 1593 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp)); 1594 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree); 1595 1596 /* 1597 * If you're a gang header, your data is in gn->gn_gbh. 1598 * If you're a gang member, your data is in 'data' and gn == NULL. 1599 */ 1600 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data); 1601 1602 if (gn != NULL) { 1603 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC); 1604 1605 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 1606 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g]; 1607 if (BP_IS_HOLE(gbp)) 1608 continue; 1609 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data); 1610 data = (char *)data + BP_GET_PSIZE(gbp); 1611 } 1612 } 1613 1614 if (gn == gio->io_gang_tree) 1615 ASSERT3P((char *)gio->io_data + gio->io_size, ==, data); 1616 1617 if (zio != pio) 1618 zio_nowait(zio); 1619 } 1620 1621 static int 1622 zio_gang_assemble(zio_t *zio) 1623 { 1624 blkptr_t *bp = zio->io_bp; 1625 1626 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL); 1627 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 1628 1629 zio->io_gang_leader = zio; 1630 1631 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree); 1632 1633 return (ZIO_PIPELINE_CONTINUE); 1634 } 1635 1636 static int 1637 zio_gang_issue(zio_t *zio) 1638 { 1639 blkptr_t *bp = zio->io_bp; 1640 1641 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE)) 1642 return (ZIO_PIPELINE_STOP); 1643 1644 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio); 1645 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 1646 1647 if (zio->io_child_error[ZIO_CHILD_GANG] == 0) 1648 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data); 1649 else 1650 zio_gang_tree_free(&zio->io_gang_tree); 1651 1652 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1653 1654 return (ZIO_PIPELINE_CONTINUE); 1655 } 1656 1657 static void 1658 zio_write_gang_member_ready(zio_t *zio) 1659 { 1660 zio_t *pio = zio_unique_parent(zio); 1661 zio_t *gio = zio->io_gang_leader; 1662 dva_t *cdva = zio->io_bp->blk_dva; 1663 dva_t *pdva = pio->io_bp->blk_dva; 1664 uint64_t asize; 1665 1666 if (BP_IS_HOLE(zio->io_bp)) 1667 return; 1668 1669 ASSERT(BP_IS_HOLE(&zio->io_bp_orig)); 1670 1671 ASSERT(zio->io_child_type == ZIO_CHILD_GANG); 1672 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies); 1673 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp)); 1674 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp)); 1675 ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp)); 1676 1677 mutex_enter(&pio->io_lock); 1678 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) { 1679 ASSERT(DVA_GET_GANG(&pdva[d])); 1680 asize = DVA_GET_ASIZE(&pdva[d]); 1681 asize += DVA_GET_ASIZE(&cdva[d]); 1682 DVA_SET_ASIZE(&pdva[d], asize); 1683 } 1684 mutex_exit(&pio->io_lock); 1685 } 1686 1687 static int 1688 zio_write_gang_block(zio_t *pio) 1689 { 1690 spa_t *spa = pio->io_spa; 1691 blkptr_t *bp = pio->io_bp; 1692 zio_t *gio = pio->io_gang_leader; 1693 zio_t *zio; 1694 zio_gang_node_t *gn, **gnpp; 1695 zio_gbh_phys_t *gbh; 1696 uint64_t txg = pio->io_txg; 1697 uint64_t resid = pio->io_size; 1698 uint64_t lsize; 1699 int copies = gio->io_prop.zp_copies; 1700 int gbh_copies = MIN(copies + 1, spa_max_replication(spa)); 1701 zio_prop_t zp; 1702 int error; 1703 1704 error = metaslab_alloc(spa, spa_normal_class(spa), SPA_GANGBLOCKSIZE, 1705 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, 1706 METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER); 1707 if (error) { 1708 pio->io_error = error; 1709 return (ZIO_PIPELINE_CONTINUE); 1710 } 1711 1712 if (pio == gio) { 1713 gnpp = &gio->io_gang_tree; 1714 } else { 1715 gnpp = pio->io_private; 1716 ASSERT(pio->io_ready == zio_write_gang_member_ready); 1717 } 1718 1719 gn = zio_gang_node_alloc(gnpp); 1720 gbh = gn->gn_gbh; 1721 bzero(gbh, SPA_GANGBLOCKSIZE); 1722 1723 /* 1724 * Create the gang header. 1725 */ 1726 zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL, 1727 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 1728 1729 /* 1730 * Create and nowait the gang children. 1731 */ 1732 for (int g = 0; resid != 0; resid -= lsize, g++) { 1733 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g), 1734 SPA_MINBLOCKSIZE); 1735 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid); 1736 1737 zp.zp_checksum = gio->io_prop.zp_checksum; 1738 zp.zp_compress = ZIO_COMPRESS_OFF; 1739 zp.zp_type = DMU_OT_NONE; 1740 zp.zp_level = 0; 1741 zp.zp_copies = gio->io_prop.zp_copies; 1742 zp.zp_dedup = 0; 1743 zp.zp_dedup_verify = 0; 1744 1745 zio_nowait(zio_write(zio, spa, txg, &gbh->zg_blkptr[g], 1746 (char *)pio->io_data + (pio->io_size - resid), lsize, &zp, 1747 zio_write_gang_member_ready, NULL, &gn->gn_child[g], 1748 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), 1749 &pio->io_bookmark)); 1750 } 1751 1752 /* 1753 * Set pio's pipeline to just wait for zio to finish. 1754 */ 1755 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1756 1757 zio_nowait(zio); 1758 1759 return (ZIO_PIPELINE_CONTINUE); 1760 } 1761 1762 /* 1763 * ========================================================================== 1764 * Dedup 1765 * ========================================================================== 1766 */ 1767 static void 1768 zio_ddt_child_read_done(zio_t *zio) 1769 { 1770 blkptr_t *bp = zio->io_bp; 1771 ddt_entry_t *dde = zio->io_private; 1772 ddt_phys_t *ddp; 1773 zio_t *pio = zio_unique_parent(zio); 1774 1775 mutex_enter(&pio->io_lock); 1776 ddp = ddt_phys_select(dde, bp); 1777 if (zio->io_error == 0) 1778 ddt_phys_clear(ddp); /* this ddp doesn't need repair */ 1779 if (zio->io_error == 0 && dde->dde_repair_data == NULL) 1780 dde->dde_repair_data = zio->io_data; 1781 else 1782 zio_buf_free(zio->io_data, zio->io_size); 1783 mutex_exit(&pio->io_lock); 1784 } 1785 1786 static int 1787 zio_ddt_read_start(zio_t *zio) 1788 { 1789 blkptr_t *bp = zio->io_bp; 1790 1791 ASSERT(BP_GET_DEDUP(bp)); 1792 ASSERT(BP_GET_PSIZE(bp) == zio->io_size); 1793 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1794 1795 if (zio->io_child_error[ZIO_CHILD_DDT]) { 1796 ddt_t *ddt = ddt_select(zio->io_spa, bp); 1797 ddt_entry_t *dde = ddt_repair_start(ddt, bp); 1798 ddt_phys_t *ddp = dde->dde_phys; 1799 ddt_phys_t *ddp_self = ddt_phys_select(dde, bp); 1800 blkptr_t blk; 1801 1802 ASSERT(zio->io_vsd == NULL); 1803 zio->io_vsd = dde; 1804 1805 if (ddp_self == NULL) 1806 return (ZIO_PIPELINE_CONTINUE); 1807 1808 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 1809 if (ddp->ddp_phys_birth == 0 || ddp == ddp_self) 1810 continue; 1811 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp, 1812 &blk); 1813 zio_nowait(zio_read(zio, zio->io_spa, &blk, 1814 zio_buf_alloc(zio->io_size), zio->io_size, 1815 zio_ddt_child_read_done, dde, zio->io_priority, 1816 ZIO_DDT_CHILD_FLAGS(zio) | ZIO_FLAG_DONT_PROPAGATE, 1817 &zio->io_bookmark)); 1818 } 1819 return (ZIO_PIPELINE_CONTINUE); 1820 } 1821 1822 zio_nowait(zio_read(zio, zio->io_spa, bp, 1823 zio->io_data, zio->io_size, NULL, NULL, zio->io_priority, 1824 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark)); 1825 1826 return (ZIO_PIPELINE_CONTINUE); 1827 } 1828 1829 static int 1830 zio_ddt_read_done(zio_t *zio) 1831 { 1832 blkptr_t *bp = zio->io_bp; 1833 1834 if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE)) 1835 return (ZIO_PIPELINE_STOP); 1836 1837 ASSERT(BP_GET_DEDUP(bp)); 1838 ASSERT(BP_GET_PSIZE(bp) == zio->io_size); 1839 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1840 1841 if (zio->io_child_error[ZIO_CHILD_DDT]) { 1842 ddt_t *ddt = ddt_select(zio->io_spa, bp); 1843 ddt_entry_t *dde = zio->io_vsd; 1844 if (ddt == NULL) { 1845 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE); 1846 return (ZIO_PIPELINE_CONTINUE); 1847 } 1848 if (dde == NULL) { 1849 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1; 1850 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE); 1851 return (ZIO_PIPELINE_STOP); 1852 } 1853 if (dde->dde_repair_data != NULL) { 1854 bcopy(dde->dde_repair_data, zio->io_data, zio->io_size); 1855 zio->io_child_error[ZIO_CHILD_DDT] = 0; 1856 } 1857 ddt_repair_done(ddt, dde); 1858 zio->io_vsd = NULL; 1859 } 1860 1861 ASSERT(zio->io_vsd == NULL); 1862 1863 return (ZIO_PIPELINE_CONTINUE); 1864 } 1865 1866 static boolean_t 1867 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde) 1868 { 1869 spa_t *spa = zio->io_spa; 1870 1871 /* 1872 * Note: we compare the original data, not the transformed data, 1873 * because when zio->io_bp is an override bp, we will not have 1874 * pushed the I/O transforms. That's an important optimization 1875 * because otherwise we'd compress/encrypt all dmu_sync() data twice. 1876 */ 1877 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) { 1878 zio_t *lio = dde->dde_lead_zio[p]; 1879 1880 if (lio != NULL) { 1881 return (lio->io_orig_size != zio->io_orig_size || 1882 bcmp(zio->io_orig_data, lio->io_orig_data, 1883 zio->io_orig_size) != 0); 1884 } 1885 } 1886 1887 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) { 1888 ddt_phys_t *ddp = &dde->dde_phys[p]; 1889 1890 if (ddp->ddp_phys_birth != 0) { 1891 arc_buf_t *abuf = NULL; 1892 uint32_t aflags = ARC_WAIT; 1893 blkptr_t blk = *zio->io_bp; 1894 int error; 1895 1896 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth); 1897 1898 ddt_exit(ddt); 1899 1900 error = arc_read_nolock(NULL, spa, &blk, 1901 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ, 1902 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 1903 &aflags, &zio->io_bookmark); 1904 1905 if (error == 0) { 1906 if (arc_buf_size(abuf) != zio->io_orig_size || 1907 bcmp(abuf->b_data, zio->io_orig_data, 1908 zio->io_orig_size) != 0) 1909 error = EEXIST; 1910 VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 1911 } 1912 1913 ddt_enter(ddt); 1914 return (error != 0); 1915 } 1916 } 1917 1918 return (B_FALSE); 1919 } 1920 1921 static void 1922 zio_ddt_child_write_ready(zio_t *zio) 1923 { 1924 int p = zio->io_prop.zp_copies; 1925 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp); 1926 ddt_entry_t *dde = zio->io_private; 1927 ddt_phys_t *ddp = &dde->dde_phys[p]; 1928 zio_t *pio; 1929 1930 if (zio->io_error) 1931 return; 1932 1933 ddt_enter(ddt); 1934 1935 ASSERT(dde->dde_lead_zio[p] == zio); 1936 1937 ddt_phys_fill(ddp, zio->io_bp); 1938 1939 while ((pio = zio_walk_parents(zio)) != NULL) 1940 ddt_bp_fill(ddp, pio->io_bp, zio->io_txg); 1941 1942 ddt_exit(ddt); 1943 } 1944 1945 static void 1946 zio_ddt_child_write_done(zio_t *zio) 1947 { 1948 int p = zio->io_prop.zp_copies; 1949 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp); 1950 ddt_entry_t *dde = zio->io_private; 1951 ddt_phys_t *ddp = &dde->dde_phys[p]; 1952 1953 ddt_enter(ddt); 1954 1955 ASSERT(ddp->ddp_refcnt == 0); 1956 ASSERT(dde->dde_lead_zio[p] == zio); 1957 dde->dde_lead_zio[p] = NULL; 1958 1959 if (zio->io_error == 0) { 1960 while (zio_walk_parents(zio) != NULL) 1961 ddt_phys_addref(ddp); 1962 } else { 1963 ddt_phys_clear(ddp); 1964 } 1965 1966 ddt_exit(ddt); 1967 } 1968 1969 static void 1970 zio_ddt_ditto_write_done(zio_t *zio) 1971 { 1972 int p = DDT_PHYS_DITTO; 1973 zio_prop_t *zp = &zio->io_prop; 1974 blkptr_t *bp = zio->io_bp; 1975 ddt_t *ddt = ddt_select(zio->io_spa, bp); 1976 ddt_entry_t *dde = zio->io_private; 1977 ddt_phys_t *ddp = &dde->dde_phys[p]; 1978 ddt_key_t *ddk = &dde->dde_key; 1979 1980 ddt_enter(ddt); 1981 1982 ASSERT(ddp->ddp_refcnt == 0); 1983 ASSERT(dde->dde_lead_zio[p] == zio); 1984 dde->dde_lead_zio[p] = NULL; 1985 1986 if (zio->io_error == 0) { 1987 ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum)); 1988 ASSERT(zp->zp_copies < SPA_DVAS_PER_BP); 1989 ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp)); 1990 if (ddp->ddp_phys_birth != 0) 1991 ddt_phys_free(ddt, ddk, ddp, zio->io_txg); 1992 ddt_phys_fill(ddp, bp); 1993 } 1994 1995 ddt_exit(ddt); 1996 } 1997 1998 static int 1999 zio_ddt_write(zio_t *zio) 2000 { 2001 spa_t *spa = zio->io_spa; 2002 blkptr_t *bp = zio->io_bp; 2003 uint64_t txg = zio->io_txg; 2004 zio_prop_t *zp = &zio->io_prop; 2005 int p = zp->zp_copies; 2006 int ditto_copies; 2007 zio_t *cio = NULL; 2008 zio_t *dio = NULL; 2009 ddt_t *ddt = ddt_select(spa, bp); 2010 ddt_entry_t *dde; 2011 ddt_phys_t *ddp; 2012 2013 ASSERT(BP_GET_DEDUP(bp)); 2014 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum); 2015 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override); 2016 2017 ddt_enter(ddt); 2018 dde = ddt_lookup(ddt, bp, B_TRUE); 2019 ddp = &dde->dde_phys[p]; 2020 2021 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) { 2022 /* 2023 * If we're using a weak checksum, upgrade to a strong checksum 2024 * and try again. If we're already using a strong checksum, 2025 * we can't resolve it, so just convert to an ordinary write. 2026 * (And automatically e-mail a paper to Nature?) 2027 */ 2028 if (!zio_checksum_table[zp->zp_checksum].ci_dedup) { 2029 zp->zp_checksum = spa_dedup_checksum(spa); 2030 zio_pop_transforms(zio); 2031 zio->io_stage = ZIO_STAGE_OPEN; 2032 BP_ZERO(bp); 2033 } else { 2034 zp->zp_dedup = 0; 2035 } 2036 zio->io_pipeline = ZIO_WRITE_PIPELINE; 2037 ddt_exit(ddt); 2038 return (ZIO_PIPELINE_CONTINUE); 2039 } 2040 2041 ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp); 2042 ASSERT(ditto_copies < SPA_DVAS_PER_BP); 2043 2044 if (ditto_copies > ddt_ditto_copies_present(dde) && 2045 dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) { 2046 zio_prop_t czp = *zp; 2047 2048 czp.zp_copies = ditto_copies; 2049 2050 /* 2051 * If we arrived here with an override bp, we won't have run 2052 * the transform stack, so we won't have the data we need to 2053 * generate a child i/o. So, toss the override bp and restart. 2054 * This is safe, because using the override bp is just an 2055 * optimization; and it's rare, so the cost doesn't matter. 2056 */ 2057 if (zio->io_bp_override) { 2058 zio_pop_transforms(zio); 2059 zio->io_stage = ZIO_STAGE_OPEN; 2060 zio->io_pipeline = ZIO_WRITE_PIPELINE; 2061 zio->io_bp_override = NULL; 2062 BP_ZERO(bp); 2063 ddt_exit(ddt); 2064 return (ZIO_PIPELINE_CONTINUE); 2065 } 2066 2067 dio = zio_write(zio, spa, txg, bp, zio->io_orig_data, 2068 zio->io_orig_size, &czp, NULL, 2069 zio_ddt_ditto_write_done, dde, zio->io_priority, 2070 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark); 2071 2072 zio_push_transform(dio, zio->io_data, zio->io_size, 0, NULL); 2073 dde->dde_lead_zio[DDT_PHYS_DITTO] = dio; 2074 } 2075 2076 if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) { 2077 if (ddp->ddp_phys_birth != 0) 2078 ddt_bp_fill(ddp, bp, txg); 2079 if (dde->dde_lead_zio[p] != NULL) 2080 zio_add_child(zio, dde->dde_lead_zio[p]); 2081 else 2082 ddt_phys_addref(ddp); 2083 } else if (zio->io_bp_override) { 2084 ASSERT(bp->blk_birth == txg); 2085 ASSERT(BP_EQUAL(bp, zio->io_bp_override)); 2086 ddt_phys_fill(ddp, bp); 2087 ddt_phys_addref(ddp); 2088 } else { 2089 cio = zio_write(zio, spa, txg, bp, zio->io_orig_data, 2090 zio->io_orig_size, zp, zio_ddt_child_write_ready, 2091 zio_ddt_child_write_done, dde, zio->io_priority, 2092 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark); 2093 2094 zio_push_transform(cio, zio->io_data, zio->io_size, 0, NULL); 2095 dde->dde_lead_zio[p] = cio; 2096 } 2097 2098 ddt_exit(ddt); 2099 2100 if (cio) 2101 zio_nowait(cio); 2102 if (dio) 2103 zio_nowait(dio); 2104 2105 return (ZIO_PIPELINE_CONTINUE); 2106 } 2107 2108 ddt_entry_t *freedde; /* for debugging */ 2109 2110 static int 2111 zio_ddt_free(zio_t *zio) 2112 { 2113 spa_t *spa = zio->io_spa; 2114 blkptr_t *bp = zio->io_bp; 2115 ddt_t *ddt = ddt_select(spa, bp); 2116 ddt_entry_t *dde; 2117 ddt_phys_t *ddp; 2118 2119 ASSERT(BP_GET_DEDUP(bp)); 2120 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2121 2122 ddt_enter(ddt); 2123 freedde = dde = ddt_lookup(ddt, bp, B_TRUE); 2124 ddp = ddt_phys_select(dde, bp); 2125 ddt_phys_decref(ddp); 2126 ddt_exit(ddt); 2127 2128 return (ZIO_PIPELINE_CONTINUE); 2129 } 2130 2131 /* 2132 * ========================================================================== 2133 * Allocate and free blocks 2134 * ========================================================================== 2135 */ 2136 static int 2137 zio_dva_allocate(zio_t *zio) 2138 { 2139 spa_t *spa = zio->io_spa; 2140 metaslab_class_t *mc = spa_normal_class(spa); 2141 blkptr_t *bp = zio->io_bp; 2142 int error; 2143 int flags = 0; 2144 2145 if (zio->io_gang_leader == NULL) { 2146 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 2147 zio->io_gang_leader = zio; 2148 } 2149 2150 ASSERT(BP_IS_HOLE(bp)); 2151 ASSERT0(BP_GET_NDVAS(bp)); 2152 ASSERT3U(zio->io_prop.zp_copies, >, 0); 2153 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa)); 2154 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp)); 2155 2156 /* 2157 * The dump device does not support gang blocks so allocation on 2158 * behalf of the dump device (i.e. ZIO_FLAG_NODATA) must avoid 2159 * the "fast" gang feature. 2160 */ 2161 flags |= (zio->io_flags & ZIO_FLAG_NODATA) ? METASLAB_GANG_AVOID : 0; 2162 flags |= (zio->io_flags & ZIO_FLAG_GANG_CHILD) ? 2163 METASLAB_GANG_CHILD : 0; 2164 error = metaslab_alloc(spa, mc, zio->io_size, bp, 2165 zio->io_prop.zp_copies, zio->io_txg, NULL, flags); 2166 2167 if (error) { 2168 spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, " 2169 "size %llu, error %d", spa_name(spa), zio, zio->io_size, 2170 error); 2171 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE) 2172 return (zio_write_gang_block(zio)); 2173 zio->io_error = error; 2174 } 2175 2176 return (ZIO_PIPELINE_CONTINUE); 2177 } 2178 2179 static int 2180 zio_dva_free(zio_t *zio) 2181 { 2182 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE); 2183 2184 return (ZIO_PIPELINE_CONTINUE); 2185 } 2186 2187 static int 2188 zio_dva_claim(zio_t *zio) 2189 { 2190 int error; 2191 2192 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg); 2193 if (error) 2194 zio->io_error = error; 2195 2196 return (ZIO_PIPELINE_CONTINUE); 2197 } 2198 2199 /* 2200 * Undo an allocation. This is used by zio_done() when an I/O fails 2201 * and we want to give back the block we just allocated. 2202 * This handles both normal blocks and gang blocks. 2203 */ 2204 static void 2205 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp) 2206 { 2207 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp)); 2208 ASSERT(zio->io_bp_override == NULL); 2209 2210 if (!BP_IS_HOLE(bp)) 2211 metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE); 2212 2213 if (gn != NULL) { 2214 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 2215 zio_dva_unallocate(zio, gn->gn_child[g], 2216 &gn->gn_gbh->zg_blkptr[g]); 2217 } 2218 } 2219 } 2220 2221 /* 2222 * Try to allocate an intent log block. Return 0 on success, errno on failure. 2223 */ 2224 int 2225 zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, blkptr_t *old_bp, 2226 uint64_t size, boolean_t use_slog) 2227 { 2228 int error = 1; 2229 2230 ASSERT(txg > spa_syncing_txg(spa)); 2231 2232 /* 2233 * ZIL blocks are always contiguous (i.e. not gang blocks) so we 2234 * set the METASLAB_GANG_AVOID flag so that they don't "fast gang" 2235 * when allocating them. 2236 */ 2237 if (use_slog) { 2238 error = metaslab_alloc(spa, spa_log_class(spa), size, 2239 new_bp, 1, txg, old_bp, 2240 METASLAB_HINTBP_AVOID | METASLAB_GANG_AVOID); 2241 } 2242 2243 if (error) { 2244 error = metaslab_alloc(spa, spa_normal_class(spa), size, 2245 new_bp, 1, txg, old_bp, 2246 METASLAB_HINTBP_AVOID | METASLAB_GANG_AVOID); 2247 } 2248 2249 if (error == 0) { 2250 BP_SET_LSIZE(new_bp, size); 2251 BP_SET_PSIZE(new_bp, size); 2252 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF); 2253 BP_SET_CHECKSUM(new_bp, 2254 spa_version(spa) >= SPA_VERSION_SLIM_ZIL 2255 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG); 2256 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG); 2257 BP_SET_LEVEL(new_bp, 0); 2258 BP_SET_DEDUP(new_bp, 0); 2259 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER); 2260 } 2261 2262 return (error); 2263 } 2264 2265 /* 2266 * Free an intent log block. 2267 */ 2268 void 2269 zio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp) 2270 { 2271 ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG); 2272 ASSERT(!BP_IS_GANG(bp)); 2273 2274 zio_free(spa, txg, bp); 2275 } 2276 2277 /* 2278 * ========================================================================== 2279 * Read and write to physical devices 2280 * ========================================================================== 2281 */ 2282 static int 2283 zio_vdev_io_start(zio_t *zio) 2284 { 2285 vdev_t *vd = zio->io_vd; 2286 uint64_t align; 2287 spa_t *spa = zio->io_spa; 2288 2289 ASSERT(zio->io_error == 0); 2290 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0); 2291 2292 if (vd == NULL) { 2293 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER)) 2294 spa_config_enter(spa, SCL_ZIO, zio, RW_READER); 2295 2296 /* 2297 * The mirror_ops handle multiple DVAs in a single BP. 2298 */ 2299 return (vdev_mirror_ops.vdev_op_io_start(zio)); 2300 } 2301 2302 /* 2303 * We keep track of time-sensitive I/Os so that the scan thread 2304 * can quickly react to certain workloads. In particular, we care 2305 * about non-scrubbing, top-level reads and writes with the following 2306 * characteristics: 2307 * - synchronous writes of user data to non-slog devices 2308 * - any reads of user data 2309 * When these conditions are met, adjust the timestamp of spa_last_io 2310 * which allows the scan thread to adjust its workload accordingly. 2311 */ 2312 if (!(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && zio->io_bp != NULL && 2313 vd == vd->vdev_top && !vd->vdev_islog && 2314 zio->io_bookmark.zb_objset != DMU_META_OBJSET && 2315 zio->io_txg != spa_syncing_txg(spa)) { 2316 uint64_t old = spa->spa_last_io; 2317 uint64_t new = ddi_get_lbolt64(); 2318 if (old != new) 2319 (void) atomic_cas_64(&spa->spa_last_io, old, new); 2320 } 2321 2322 align = 1ULL << vd->vdev_top->vdev_ashift; 2323 2324 if (P2PHASE(zio->io_size, align) != 0) { 2325 uint64_t asize = P2ROUNDUP(zio->io_size, align); 2326 char *abuf = zio_buf_alloc(asize); 2327 ASSERT(vd == vd->vdev_top); 2328 if (zio->io_type == ZIO_TYPE_WRITE) { 2329 bcopy(zio->io_data, abuf, zio->io_size); 2330 bzero(abuf + zio->io_size, asize - zio->io_size); 2331 } 2332 zio_push_transform(zio, abuf, asize, asize, zio_subblock); 2333 } 2334 2335 ASSERT(P2PHASE(zio->io_offset, align) == 0); 2336 ASSERT(P2PHASE(zio->io_size, align) == 0); 2337 VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa)); 2338 2339 /* 2340 * If this is a repair I/O, and there's no self-healing involved -- 2341 * that is, we're just resilvering what we expect to resilver -- 2342 * then don't do the I/O unless zio's txg is actually in vd's DTL. 2343 * This prevents spurious resilvering with nested replication. 2344 * For example, given a mirror of mirrors, (A+B)+(C+D), if only 2345 * A is out of date, we'll read from C+D, then use the data to 2346 * resilver A+B -- but we don't actually want to resilver B, just A. 2347 * The top-level mirror has no way to know this, so instead we just 2348 * discard unnecessary repairs as we work our way down the vdev tree. 2349 * The same logic applies to any form of nested replication: 2350 * ditto + mirror, RAID-Z + replacing, etc. This covers them all. 2351 */ 2352 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) && 2353 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) && 2354 zio->io_txg != 0 && /* not a delegated i/o */ 2355 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) { 2356 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 2357 zio_vdev_io_bypass(zio); 2358 return (ZIO_PIPELINE_CONTINUE); 2359 } 2360 2361 if (vd->vdev_ops->vdev_op_leaf && 2362 (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) { 2363 2364 if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0) 2365 return (ZIO_PIPELINE_CONTINUE); 2366 2367 if ((zio = vdev_queue_io(zio)) == NULL) 2368 return (ZIO_PIPELINE_STOP); 2369 2370 if (!vdev_accessible(vd, zio)) { 2371 zio->io_error = ENXIO; 2372 zio_interrupt(zio); 2373 return (ZIO_PIPELINE_STOP); 2374 } 2375 } 2376 2377 return (vd->vdev_ops->vdev_op_io_start(zio)); 2378 } 2379 2380 static int 2381 zio_vdev_io_done(zio_t *zio) 2382 { 2383 vdev_t *vd = zio->io_vd; 2384 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops; 2385 boolean_t unexpected_error = B_FALSE; 2386 2387 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE)) 2388 return (ZIO_PIPELINE_STOP); 2389 2390 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE); 2391 2392 if (vd != NULL && vd->vdev_ops->vdev_op_leaf) { 2393 2394 vdev_queue_io_done(zio); 2395 2396 if (zio->io_type == ZIO_TYPE_WRITE) 2397 vdev_cache_write(zio); 2398 2399 if (zio_injection_enabled && zio->io_error == 0) 2400 zio->io_error = zio_handle_device_injection(vd, 2401 zio, EIO); 2402 2403 if (zio_injection_enabled && zio->io_error == 0) 2404 zio->io_error = zio_handle_label_injection(zio, EIO); 2405 2406 if (zio->io_error) { 2407 if (!vdev_accessible(vd, zio)) { 2408 zio->io_error = ENXIO; 2409 } else { 2410 unexpected_error = B_TRUE; 2411 } 2412 } 2413 } 2414 2415 ops->vdev_op_io_done(zio); 2416 2417 if (unexpected_error) 2418 VERIFY(vdev_probe(vd, zio) == NULL); 2419 2420 return (ZIO_PIPELINE_CONTINUE); 2421 } 2422 2423 /* 2424 * For non-raidz ZIOs, we can just copy aside the bad data read from the 2425 * disk, and use that to finish the checksum ereport later. 2426 */ 2427 static void 2428 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr, 2429 const void *good_buf) 2430 { 2431 /* no processing needed */ 2432 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE); 2433 } 2434 2435 /*ARGSUSED*/ 2436 void 2437 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored) 2438 { 2439 void *buf = zio_buf_alloc(zio->io_size); 2440 2441 bcopy(zio->io_data, buf, zio->io_size); 2442 2443 zcr->zcr_cbinfo = zio->io_size; 2444 zcr->zcr_cbdata = buf; 2445 zcr->zcr_finish = zio_vsd_default_cksum_finish; 2446 zcr->zcr_free = zio_buf_free; 2447 } 2448 2449 static int 2450 zio_vdev_io_assess(zio_t *zio) 2451 { 2452 vdev_t *vd = zio->io_vd; 2453 2454 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE)) 2455 return (ZIO_PIPELINE_STOP); 2456 2457 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER)) 2458 spa_config_exit(zio->io_spa, SCL_ZIO, zio); 2459 2460 if (zio->io_vsd != NULL) { 2461 zio->io_vsd_ops->vsd_free(zio); 2462 zio->io_vsd = NULL; 2463 } 2464 2465 if (zio_injection_enabled && zio->io_error == 0) 2466 zio->io_error = zio_handle_fault_injection(zio, EIO); 2467 2468 /* 2469 * If the I/O failed, determine whether we should attempt to retry it. 2470 * 2471 * On retry, we cut in line in the issue queue, since we don't want 2472 * compression/checksumming/etc. work to prevent our (cheap) IO reissue. 2473 */ 2474 if (zio->io_error && vd == NULL && 2475 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) { 2476 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */ 2477 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */ 2478 zio->io_error = 0; 2479 zio->io_flags |= ZIO_FLAG_IO_RETRY | 2480 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE; 2481 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1; 2482 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, 2483 zio_requeue_io_start_cut_in_line); 2484 return (ZIO_PIPELINE_STOP); 2485 } 2486 2487 /* 2488 * If we got an error on a leaf device, convert it to ENXIO 2489 * if the device is not accessible at all. 2490 */ 2491 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf && 2492 !vdev_accessible(vd, zio)) 2493 zio->io_error = ENXIO; 2494 2495 /* 2496 * If we can't write to an interior vdev (mirror or RAID-Z), 2497 * set vdev_cant_write so that we stop trying to allocate from it. 2498 */ 2499 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE && 2500 vd != NULL && !vd->vdev_ops->vdev_op_leaf) 2501 vd->vdev_cant_write = B_TRUE; 2502 2503 if (zio->io_error) 2504 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 2505 2506 return (ZIO_PIPELINE_CONTINUE); 2507 } 2508 2509 void 2510 zio_vdev_io_reissue(zio_t *zio) 2511 { 2512 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 2513 ASSERT(zio->io_error == 0); 2514 2515 zio->io_stage >>= 1; 2516 } 2517 2518 void 2519 zio_vdev_io_redone(zio_t *zio) 2520 { 2521 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE); 2522 2523 zio->io_stage >>= 1; 2524 } 2525 2526 void 2527 zio_vdev_io_bypass(zio_t *zio) 2528 { 2529 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 2530 ASSERT(zio->io_error == 0); 2531 2532 zio->io_flags |= ZIO_FLAG_IO_BYPASS; 2533 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1; 2534 } 2535 2536 /* 2537 * ========================================================================== 2538 * Generate and verify checksums 2539 * ========================================================================== 2540 */ 2541 static int 2542 zio_checksum_generate(zio_t *zio) 2543 { 2544 blkptr_t *bp = zio->io_bp; 2545 enum zio_checksum checksum; 2546 2547 if (bp == NULL) { 2548 /* 2549 * This is zio_write_phys(). 2550 * We're either generating a label checksum, or none at all. 2551 */ 2552 checksum = zio->io_prop.zp_checksum; 2553 2554 if (checksum == ZIO_CHECKSUM_OFF) 2555 return (ZIO_PIPELINE_CONTINUE); 2556 2557 ASSERT(checksum == ZIO_CHECKSUM_LABEL); 2558 } else { 2559 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) { 2560 ASSERT(!IO_IS_ALLOCATING(zio)); 2561 checksum = ZIO_CHECKSUM_GANG_HEADER; 2562 } else { 2563 checksum = BP_GET_CHECKSUM(bp); 2564 } 2565 } 2566 2567 zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size); 2568 2569 return (ZIO_PIPELINE_CONTINUE); 2570 } 2571 2572 static int 2573 zio_checksum_verify(zio_t *zio) 2574 { 2575 zio_bad_cksum_t info; 2576 blkptr_t *bp = zio->io_bp; 2577 int error; 2578 2579 ASSERT(zio->io_vd != NULL); 2580 2581 if (bp == NULL) { 2582 /* 2583 * This is zio_read_phys(). 2584 * We're either verifying a label checksum, or nothing at all. 2585 */ 2586 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF) 2587 return (ZIO_PIPELINE_CONTINUE); 2588 2589 ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL); 2590 } 2591 2592 if ((error = zio_checksum_error(zio, &info)) != 0) { 2593 zio->io_error = error; 2594 if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 2595 zfs_ereport_start_checksum(zio->io_spa, 2596 zio->io_vd, zio, zio->io_offset, 2597 zio->io_size, NULL, &info); 2598 } 2599 } 2600 2601 return (ZIO_PIPELINE_CONTINUE); 2602 } 2603 2604 /* 2605 * Called by RAID-Z to ensure we don't compute the checksum twice. 2606 */ 2607 void 2608 zio_checksum_verified(zio_t *zio) 2609 { 2610 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY; 2611 } 2612 2613 /* 2614 * ========================================================================== 2615 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other. 2616 * An error of 0 indictes success. ENXIO indicates whole-device failure, 2617 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO 2618 * indicate errors that are specific to one I/O, and most likely permanent. 2619 * Any other error is presumed to be worse because we weren't expecting it. 2620 * ========================================================================== 2621 */ 2622 int 2623 zio_worst_error(int e1, int e2) 2624 { 2625 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO }; 2626 int r1, r2; 2627 2628 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++) 2629 if (e1 == zio_error_rank[r1]) 2630 break; 2631 2632 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++) 2633 if (e2 == zio_error_rank[r2]) 2634 break; 2635 2636 return (r1 > r2 ? e1 : e2); 2637 } 2638 2639 /* 2640 * ========================================================================== 2641 * I/O completion 2642 * ========================================================================== 2643 */ 2644 static int 2645 zio_ready(zio_t *zio) 2646 { 2647 blkptr_t *bp = zio->io_bp; 2648 zio_t *pio, *pio_next; 2649 2650 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) || 2651 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY)) 2652 return (ZIO_PIPELINE_STOP); 2653 2654 if (zio->io_ready) { 2655 ASSERT(IO_IS_ALLOCATING(zio)); 2656 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp)); 2657 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0); 2658 2659 zio->io_ready(zio); 2660 } 2661 2662 if (bp != NULL && bp != &zio->io_bp_copy) 2663 zio->io_bp_copy = *bp; 2664 2665 if (zio->io_error) 2666 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 2667 2668 mutex_enter(&zio->io_lock); 2669 zio->io_state[ZIO_WAIT_READY] = 1; 2670 pio = zio_walk_parents(zio); 2671 mutex_exit(&zio->io_lock); 2672 2673 /* 2674 * As we notify zio's parents, new parents could be added. 2675 * New parents go to the head of zio's io_parent_list, however, 2676 * so we will (correctly) not notify them. The remainder of zio's 2677 * io_parent_list, from 'pio_next' onward, cannot change because 2678 * all parents must wait for us to be done before they can be done. 2679 */ 2680 for (; pio != NULL; pio = pio_next) { 2681 pio_next = zio_walk_parents(zio); 2682 zio_notify_parent(pio, zio, ZIO_WAIT_READY); 2683 } 2684 2685 if (zio->io_flags & ZIO_FLAG_NODATA) { 2686 if (BP_IS_GANG(bp)) { 2687 zio->io_flags &= ~ZIO_FLAG_NODATA; 2688 } else { 2689 ASSERT((uintptr_t)zio->io_data < SPA_MAXBLOCKSIZE); 2690 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES; 2691 } 2692 } 2693 2694 if (zio_injection_enabled && 2695 zio->io_spa->spa_syncing_txg == zio->io_txg) 2696 zio_handle_ignored_writes(zio); 2697 2698 return (ZIO_PIPELINE_CONTINUE); 2699 } 2700 2701 static int 2702 zio_done(zio_t *zio) 2703 { 2704 spa_t *spa = zio->io_spa; 2705 zio_t *lio = zio->io_logical; 2706 blkptr_t *bp = zio->io_bp; 2707 vdev_t *vd = zio->io_vd; 2708 uint64_t psize = zio->io_size; 2709 zio_t *pio, *pio_next; 2710 2711 /* 2712 * If our children haven't all completed, 2713 * wait for them and then repeat this pipeline stage. 2714 */ 2715 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) || 2716 zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) || 2717 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) || 2718 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE)) 2719 return (ZIO_PIPELINE_STOP); 2720 2721 for (int c = 0; c < ZIO_CHILD_TYPES; c++) 2722 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 2723 ASSERT(zio->io_children[c][w] == 0); 2724 2725 if (bp != NULL) { 2726 ASSERT(bp->blk_pad[0] == 0); 2727 ASSERT(bp->blk_pad[1] == 0); 2728 ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 || 2729 (bp == zio_unique_parent(zio)->io_bp)); 2730 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) && 2731 zio->io_bp_override == NULL && 2732 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) { 2733 ASSERT(!BP_SHOULD_BYTESWAP(bp)); 2734 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp)); 2735 ASSERT(BP_COUNT_GANG(bp) == 0 || 2736 (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp))); 2737 } 2738 } 2739 2740 /* 2741 * If there were child vdev/gang/ddt errors, they apply to us now. 2742 */ 2743 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV); 2744 zio_inherit_child_errors(zio, ZIO_CHILD_GANG); 2745 zio_inherit_child_errors(zio, ZIO_CHILD_DDT); 2746 2747 /* 2748 * If the I/O on the transformed data was successful, generate any 2749 * checksum reports now while we still have the transformed data. 2750 */ 2751 if (zio->io_error == 0) { 2752 while (zio->io_cksum_report != NULL) { 2753 zio_cksum_report_t *zcr = zio->io_cksum_report; 2754 uint64_t align = zcr->zcr_align; 2755 uint64_t asize = P2ROUNDUP(psize, align); 2756 char *abuf = zio->io_data; 2757 2758 if (asize != psize) { 2759 abuf = zio_buf_alloc(asize); 2760 bcopy(zio->io_data, abuf, psize); 2761 bzero(abuf + psize, asize - psize); 2762 } 2763 2764 zio->io_cksum_report = zcr->zcr_next; 2765 zcr->zcr_next = NULL; 2766 zcr->zcr_finish(zcr, abuf); 2767 zfs_ereport_free_checksum(zcr); 2768 2769 if (asize != psize) 2770 zio_buf_free(abuf, asize); 2771 } 2772 } 2773 2774 zio_pop_transforms(zio); /* note: may set zio->io_error */ 2775 2776 vdev_stat_update(zio, psize); 2777 2778 if (zio->io_error) { 2779 /* 2780 * If this I/O is attached to a particular vdev, 2781 * generate an error message describing the I/O failure 2782 * at the block level. We ignore these errors if the 2783 * device is currently unavailable. 2784 */ 2785 if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd)) 2786 zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0); 2787 2788 if ((zio->io_error == EIO || !(zio->io_flags & 2789 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) && 2790 zio == lio) { 2791 /* 2792 * For logical I/O requests, tell the SPA to log the 2793 * error and generate a logical data ereport. 2794 */ 2795 spa_log_error(spa, zio); 2796 zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio, 2797 0, 0); 2798 } 2799 } 2800 2801 if (zio->io_error && zio == lio) { 2802 /* 2803 * Determine whether zio should be reexecuted. This will 2804 * propagate all the way to the root via zio_notify_parent(). 2805 */ 2806 ASSERT(vd == NULL && bp != NULL); 2807 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2808 2809 if (IO_IS_ALLOCATING(zio) && 2810 !(zio->io_flags & ZIO_FLAG_CANFAIL)) { 2811 if (zio->io_error != ENOSPC) 2812 zio->io_reexecute |= ZIO_REEXECUTE_NOW; 2813 else 2814 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 2815 } 2816 2817 if ((zio->io_type == ZIO_TYPE_READ || 2818 zio->io_type == ZIO_TYPE_FREE) && 2819 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && 2820 zio->io_error == ENXIO && 2821 spa_load_state(spa) == SPA_LOAD_NONE && 2822 spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE) 2823 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 2824 2825 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute) 2826 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 2827 2828 /* 2829 * Here is a possibly good place to attempt to do 2830 * either combinatorial reconstruction or error correction 2831 * based on checksums. It also might be a good place 2832 * to send out preliminary ereports before we suspend 2833 * processing. 2834 */ 2835 } 2836 2837 /* 2838 * If there were logical child errors, they apply to us now. 2839 * We defer this until now to avoid conflating logical child 2840 * errors with errors that happened to the zio itself when 2841 * updating vdev stats and reporting FMA events above. 2842 */ 2843 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL); 2844 2845 if ((zio->io_error || zio->io_reexecute) && 2846 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio && 2847 !(zio->io_flags & ZIO_FLAG_IO_REWRITE)) 2848 zio_dva_unallocate(zio, zio->io_gang_tree, bp); 2849 2850 zio_gang_tree_free(&zio->io_gang_tree); 2851 2852 /* 2853 * Godfather I/Os should never suspend. 2854 */ 2855 if ((zio->io_flags & ZIO_FLAG_GODFATHER) && 2856 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) 2857 zio->io_reexecute = 0; 2858 2859 if (zio->io_reexecute) { 2860 /* 2861 * This is a logical I/O that wants to reexecute. 2862 * 2863 * Reexecute is top-down. When an i/o fails, if it's not 2864 * the root, it simply notifies its parent and sticks around. 2865 * The parent, seeing that it still has children in zio_done(), 2866 * does the same. This percolates all the way up to the root. 2867 * The root i/o will reexecute or suspend the entire tree. 2868 * 2869 * This approach ensures that zio_reexecute() honors 2870 * all the original i/o dependency relationships, e.g. 2871 * parents not executing until children are ready. 2872 */ 2873 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2874 2875 zio->io_gang_leader = NULL; 2876 2877 mutex_enter(&zio->io_lock); 2878 zio->io_state[ZIO_WAIT_DONE] = 1; 2879 mutex_exit(&zio->io_lock); 2880 2881 /* 2882 * "The Godfather" I/O monitors its children but is 2883 * not a true parent to them. It will track them through 2884 * the pipeline but severs its ties whenever they get into 2885 * trouble (e.g. suspended). This allows "The Godfather" 2886 * I/O to return status without blocking. 2887 */ 2888 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) { 2889 zio_link_t *zl = zio->io_walk_link; 2890 pio_next = zio_walk_parents(zio); 2891 2892 if ((pio->io_flags & ZIO_FLAG_GODFATHER) && 2893 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) { 2894 zio_remove_child(pio, zio, zl); 2895 zio_notify_parent(pio, zio, ZIO_WAIT_DONE); 2896 } 2897 } 2898 2899 if ((pio = zio_unique_parent(zio)) != NULL) { 2900 /* 2901 * We're not a root i/o, so there's nothing to do 2902 * but notify our parent. Don't propagate errors 2903 * upward since we haven't permanently failed yet. 2904 */ 2905 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER)); 2906 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE; 2907 zio_notify_parent(pio, zio, ZIO_WAIT_DONE); 2908 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) { 2909 /* 2910 * We'd fail again if we reexecuted now, so suspend 2911 * until conditions improve (e.g. device comes online). 2912 */ 2913 zio_suspend(spa, zio); 2914 } else { 2915 /* 2916 * Reexecution is potentially a huge amount of work. 2917 * Hand it off to the otherwise-unused claim taskq. 2918 */ 2919 ASSERT(zio->io_tqent.tqent_next == NULL); 2920 (void) taskq_dispatch_ent( 2921 spa->spa_zio_taskq[ZIO_TYPE_CLAIM][ZIO_TASKQ_ISSUE], 2922 (task_func_t *)zio_reexecute, zio, 0, 2923 &zio->io_tqent); 2924 } 2925 return (ZIO_PIPELINE_STOP); 2926 } 2927 2928 ASSERT(zio->io_child_count == 0); 2929 ASSERT(zio->io_reexecute == 0); 2930 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL)); 2931 2932 /* 2933 * Report any checksum errors, since the I/O is complete. 2934 */ 2935 while (zio->io_cksum_report != NULL) { 2936 zio_cksum_report_t *zcr = zio->io_cksum_report; 2937 zio->io_cksum_report = zcr->zcr_next; 2938 zcr->zcr_next = NULL; 2939 zcr->zcr_finish(zcr, NULL); 2940 zfs_ereport_free_checksum(zcr); 2941 } 2942 2943 /* 2944 * It is the responsibility of the done callback to ensure that this 2945 * particular zio is no longer discoverable for adoption, and as 2946 * such, cannot acquire any new parents. 2947 */ 2948 if (zio->io_done) 2949 zio->io_done(zio); 2950 2951 mutex_enter(&zio->io_lock); 2952 zio->io_state[ZIO_WAIT_DONE] = 1; 2953 mutex_exit(&zio->io_lock); 2954 2955 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) { 2956 zio_link_t *zl = zio->io_walk_link; 2957 pio_next = zio_walk_parents(zio); 2958 zio_remove_child(pio, zio, zl); 2959 zio_notify_parent(pio, zio, ZIO_WAIT_DONE); 2960 } 2961 2962 if (zio->io_waiter != NULL) { 2963 mutex_enter(&zio->io_lock); 2964 zio->io_executor = NULL; 2965 cv_broadcast(&zio->io_cv); 2966 mutex_exit(&zio->io_lock); 2967 } else { 2968 zio_destroy(zio); 2969 } 2970 2971 return (ZIO_PIPELINE_STOP); 2972 } 2973 2974 /* 2975 * ========================================================================== 2976 * I/O pipeline definition 2977 * ========================================================================== 2978 */ 2979 static zio_pipe_stage_t *zio_pipeline[] = { 2980 NULL, 2981 zio_read_bp_init, 2982 zio_free_bp_init, 2983 zio_issue_async, 2984 zio_write_bp_init, 2985 zio_checksum_generate, 2986 zio_ddt_read_start, 2987 zio_ddt_read_done, 2988 zio_ddt_write, 2989 zio_ddt_free, 2990 zio_gang_assemble, 2991 zio_gang_issue, 2992 zio_dva_allocate, 2993 zio_dva_free, 2994 zio_dva_claim, 2995 zio_ready, 2996 zio_vdev_io_start, 2997 zio_vdev_io_done, 2998 zio_vdev_io_assess, 2999 zio_checksum_verify, 3000 zio_done 3001 }; 3002 3003 /* dnp is the dnode for zb1->zb_object */ 3004 boolean_t 3005 zbookmark_is_before(const dnode_phys_t *dnp, const zbookmark_t *zb1, 3006 const zbookmark_t *zb2) 3007 { 3008 uint64_t zb1nextL0, zb2thisobj; 3009 3010 ASSERT(zb1->zb_objset == zb2->zb_objset); 3011 ASSERT(zb2->zb_level == 0); 3012 3013 /* 3014 * A bookmark in the deadlist is considered to be after 3015 * everything else. 3016 */ 3017 if (zb2->zb_object == DMU_DEADLIST_OBJECT) 3018 return (B_TRUE); 3019 3020 /* The objset_phys_t isn't before anything. */ 3021 if (dnp == NULL) 3022 return (B_FALSE); 3023 3024 zb1nextL0 = (zb1->zb_blkid + 1) << 3025 ((zb1->zb_level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)); 3026 3027 zb2thisobj = zb2->zb_object ? zb2->zb_object : 3028 zb2->zb_blkid << (DNODE_BLOCK_SHIFT - DNODE_SHIFT); 3029 3030 if (zb1->zb_object == DMU_META_DNODE_OBJECT) { 3031 uint64_t nextobj = zb1nextL0 * 3032 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT) >> DNODE_SHIFT; 3033 return (nextobj <= zb2thisobj); 3034 } 3035 3036 if (zb1->zb_object < zb2thisobj) 3037 return (B_TRUE); 3038 if (zb1->zb_object > zb2thisobj) 3039 return (B_FALSE); 3040 if (zb2->zb_object == DMU_META_DNODE_OBJECT) 3041 return (B_FALSE); 3042 return (zb1nextL0 <= zb2->zb_blkid); 3043 } 3044