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