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, 2014 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 zio->io_bp_override = NULL; 1132 BP_ZERO(bp); 1133 } 1134 1135 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) { 1136 /* 1137 * We're rewriting an existing block, which means we're 1138 * working on behalf of spa_sync(). For spa_sync() to 1139 * converge, it must eventually be the case that we don't 1140 * have to allocate new blocks. But compression changes 1141 * the blocksize, which forces a reallocate, and makes 1142 * convergence take longer. Therefore, after the first 1143 * few passes, stop compressing to ensure convergence. 1144 */ 1145 pass = spa_sync_pass(spa); 1146 1147 ASSERT(zio->io_txg == spa_syncing_txg(spa)); 1148 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1149 ASSERT(!BP_GET_DEDUP(bp)); 1150 1151 if (pass >= zfs_sync_pass_dont_compress) 1152 compress = ZIO_COMPRESS_OFF; 1153 1154 /* Make sure someone doesn't change their mind on overwrites */ 1155 ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp), 1156 spa_max_replication(spa)) == BP_GET_NDVAS(bp)); 1157 } 1158 1159 if (compress != ZIO_COMPRESS_OFF) { 1160 void *cbuf = zio_buf_alloc(lsize); 1161 psize = zio_compress_data(compress, zio->io_data, cbuf, lsize); 1162 if (psize == 0 || psize == lsize) { 1163 compress = ZIO_COMPRESS_OFF; 1164 zio_buf_free(cbuf, lsize); 1165 } else if (!zp->zp_dedup && psize <= BPE_PAYLOAD_SIZE && 1166 zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) && 1167 spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) { 1168 encode_embedded_bp_compressed(bp, 1169 cbuf, compress, lsize, psize); 1170 BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA); 1171 BP_SET_TYPE(bp, zio->io_prop.zp_type); 1172 BP_SET_LEVEL(bp, zio->io_prop.zp_level); 1173 zio_buf_free(cbuf, lsize); 1174 bp->blk_birth = zio->io_txg; 1175 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1176 ASSERT(spa_feature_is_active(spa, 1177 SPA_FEATURE_EMBEDDED_DATA)); 1178 return (ZIO_PIPELINE_CONTINUE); 1179 } else { 1180 /* 1181 * Round up compressed size to MINBLOCKSIZE and 1182 * zero the tail. 1183 */ 1184 size_t rounded = 1185 P2ROUNDUP(psize, (size_t)SPA_MINBLOCKSIZE); 1186 if (rounded > psize) { 1187 bzero((char *)cbuf + psize, rounded - psize); 1188 psize = rounded; 1189 } 1190 if (psize == lsize) { 1191 compress = ZIO_COMPRESS_OFF; 1192 zio_buf_free(cbuf, lsize); 1193 } else { 1194 zio_push_transform(zio, cbuf, 1195 psize, lsize, NULL); 1196 } 1197 } 1198 } 1199 1200 /* 1201 * The final pass of spa_sync() must be all rewrites, but the first 1202 * few passes offer a trade-off: allocating blocks defers convergence, 1203 * but newly allocated blocks are sequential, so they can be written 1204 * to disk faster. Therefore, we allow the first few passes of 1205 * spa_sync() to allocate new blocks, but force rewrites after that. 1206 * There should only be a handful of blocks after pass 1 in any case. 1207 */ 1208 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg && 1209 BP_GET_PSIZE(bp) == psize && 1210 pass >= zfs_sync_pass_rewrite) { 1211 ASSERT(psize != 0); 1212 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES; 1213 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages; 1214 zio->io_flags |= ZIO_FLAG_IO_REWRITE; 1215 } else { 1216 BP_ZERO(bp); 1217 zio->io_pipeline = ZIO_WRITE_PIPELINE; 1218 } 1219 1220 if (psize == 0) { 1221 if (zio->io_bp_orig.blk_birth != 0 && 1222 spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) { 1223 BP_SET_LSIZE(bp, lsize); 1224 BP_SET_TYPE(bp, zp->zp_type); 1225 BP_SET_LEVEL(bp, zp->zp_level); 1226 BP_SET_BIRTH(bp, zio->io_txg, 0); 1227 } 1228 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1229 } else { 1230 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER); 1231 BP_SET_LSIZE(bp, lsize); 1232 BP_SET_TYPE(bp, zp->zp_type); 1233 BP_SET_LEVEL(bp, zp->zp_level); 1234 BP_SET_PSIZE(bp, psize); 1235 BP_SET_COMPRESS(bp, compress); 1236 BP_SET_CHECKSUM(bp, zp->zp_checksum); 1237 BP_SET_DEDUP(bp, zp->zp_dedup); 1238 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 1239 if (zp->zp_dedup) { 1240 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1241 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 1242 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE; 1243 } 1244 if (zp->zp_nopwrite) { 1245 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1246 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 1247 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE; 1248 } 1249 } 1250 1251 return (ZIO_PIPELINE_CONTINUE); 1252 } 1253 1254 static int 1255 zio_free_bp_init(zio_t *zio) 1256 { 1257 blkptr_t *bp = zio->io_bp; 1258 1259 if (zio->io_child_type == ZIO_CHILD_LOGICAL) { 1260 if (BP_GET_DEDUP(bp)) 1261 zio->io_pipeline = ZIO_DDT_FREE_PIPELINE; 1262 } 1263 1264 return (ZIO_PIPELINE_CONTINUE); 1265 } 1266 1267 /* 1268 * ========================================================================== 1269 * Execute the I/O pipeline 1270 * ========================================================================== 1271 */ 1272 1273 static void 1274 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline) 1275 { 1276 spa_t *spa = zio->io_spa; 1277 zio_type_t t = zio->io_type; 1278 int flags = (cutinline ? TQ_FRONT : 0); 1279 1280 /* 1281 * If we're a config writer or a probe, the normal issue and 1282 * interrupt threads may all be blocked waiting for the config lock. 1283 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL. 1284 */ 1285 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE)) 1286 t = ZIO_TYPE_NULL; 1287 1288 /* 1289 * A similar issue exists for the L2ARC write thread until L2ARC 2.0. 1290 */ 1291 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux) 1292 t = ZIO_TYPE_NULL; 1293 1294 /* 1295 * If this is a high priority I/O, then use the high priority taskq if 1296 * available. 1297 */ 1298 if (zio->io_priority == ZIO_PRIORITY_NOW && 1299 spa->spa_zio_taskq[t][q + 1].stqs_count != 0) 1300 q++; 1301 1302 ASSERT3U(q, <, ZIO_TASKQ_TYPES); 1303 1304 /* 1305 * NB: We are assuming that the zio can only be dispatched 1306 * to a single taskq at a time. It would be a grievous error 1307 * to dispatch the zio to another taskq at the same time. 1308 */ 1309 ASSERT(zio->io_tqent.tqent_next == NULL); 1310 spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio, 1311 flags, &zio->io_tqent); 1312 } 1313 1314 static boolean_t 1315 zio_taskq_member(zio_t *zio, zio_taskq_type_t q) 1316 { 1317 kthread_t *executor = zio->io_executor; 1318 spa_t *spa = zio->io_spa; 1319 1320 for (zio_type_t t = 0; t < ZIO_TYPES; t++) { 1321 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 1322 uint_t i; 1323 for (i = 0; i < tqs->stqs_count; i++) { 1324 if (taskq_member(tqs->stqs_taskq[i], executor)) 1325 return (B_TRUE); 1326 } 1327 } 1328 1329 return (B_FALSE); 1330 } 1331 1332 static int 1333 zio_issue_async(zio_t *zio) 1334 { 1335 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE); 1336 1337 return (ZIO_PIPELINE_STOP); 1338 } 1339 1340 void 1341 zio_interrupt(zio_t *zio) 1342 { 1343 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE); 1344 } 1345 1346 /* 1347 * Execute the I/O pipeline until one of the following occurs: 1348 * 1349 * (1) the I/O completes 1350 * (2) the pipeline stalls waiting for dependent child I/Os 1351 * (3) the I/O issues, so we're waiting for an I/O completion interrupt 1352 * (4) the I/O is delegated by vdev-level caching or aggregation 1353 * (5) the I/O is deferred due to vdev-level queueing 1354 * (6) the I/O is handed off to another thread. 1355 * 1356 * In all cases, the pipeline stops whenever there's no CPU work; it never 1357 * burns a thread in cv_wait(). 1358 * 1359 * There's no locking on io_stage because there's no legitimate way 1360 * for multiple threads to be attempting to process the same I/O. 1361 */ 1362 static zio_pipe_stage_t *zio_pipeline[]; 1363 1364 void 1365 zio_execute(zio_t *zio) 1366 { 1367 zio->io_executor = curthread; 1368 1369 while (zio->io_stage < ZIO_STAGE_DONE) { 1370 enum zio_stage pipeline = zio->io_pipeline; 1371 enum zio_stage stage = zio->io_stage; 1372 int rv; 1373 1374 ASSERT(!MUTEX_HELD(&zio->io_lock)); 1375 ASSERT(ISP2(stage)); 1376 ASSERT(zio->io_stall == NULL); 1377 1378 do { 1379 stage <<= 1; 1380 } while ((stage & pipeline) == 0); 1381 1382 ASSERT(stage <= ZIO_STAGE_DONE); 1383 1384 /* 1385 * If we are in interrupt context and this pipeline stage 1386 * will grab a config lock that is held across I/O, 1387 * or may wait for an I/O that needs an interrupt thread 1388 * to complete, issue async to avoid deadlock. 1389 * 1390 * For VDEV_IO_START, we cut in line so that the io will 1391 * be sent to disk promptly. 1392 */ 1393 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL && 1394 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) { 1395 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ? 1396 zio_requeue_io_start_cut_in_line : B_FALSE; 1397 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut); 1398 return; 1399 } 1400 1401 zio->io_stage = stage; 1402 rv = zio_pipeline[highbit64(stage) - 1](zio); 1403 1404 if (rv == ZIO_PIPELINE_STOP) 1405 return; 1406 1407 ASSERT(rv == ZIO_PIPELINE_CONTINUE); 1408 } 1409 } 1410 1411 /* 1412 * ========================================================================== 1413 * Initiate I/O, either sync or async 1414 * ========================================================================== 1415 */ 1416 int 1417 zio_wait(zio_t *zio) 1418 { 1419 int error; 1420 1421 ASSERT(zio->io_stage == ZIO_STAGE_OPEN); 1422 ASSERT(zio->io_executor == NULL); 1423 1424 zio->io_waiter = curthread; 1425 1426 zio_execute(zio); 1427 1428 mutex_enter(&zio->io_lock); 1429 while (zio->io_executor != NULL) 1430 cv_wait(&zio->io_cv, &zio->io_lock); 1431 mutex_exit(&zio->io_lock); 1432 1433 error = zio->io_error; 1434 zio_destroy(zio); 1435 1436 return (error); 1437 } 1438 1439 void 1440 zio_nowait(zio_t *zio) 1441 { 1442 ASSERT(zio->io_executor == NULL); 1443 1444 if (zio->io_child_type == ZIO_CHILD_LOGICAL && 1445 zio_unique_parent(zio) == NULL) { 1446 /* 1447 * This is a logical async I/O with no parent to wait for it. 1448 * We add it to the spa_async_root_zio "Godfather" I/O which 1449 * will ensure they complete prior to unloading the pool. 1450 */ 1451 spa_t *spa = zio->io_spa; 1452 1453 zio_add_child(spa->spa_async_zio_root[CPU_SEQID], zio); 1454 } 1455 1456 zio_execute(zio); 1457 } 1458 1459 /* 1460 * ========================================================================== 1461 * Reexecute or suspend/resume failed I/O 1462 * ========================================================================== 1463 */ 1464 1465 static void 1466 zio_reexecute(zio_t *pio) 1467 { 1468 zio_t *cio, *cio_next; 1469 1470 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL); 1471 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN); 1472 ASSERT(pio->io_gang_leader == NULL); 1473 ASSERT(pio->io_gang_tree == NULL); 1474 1475 pio->io_flags = pio->io_orig_flags; 1476 pio->io_stage = pio->io_orig_stage; 1477 pio->io_pipeline = pio->io_orig_pipeline; 1478 pio->io_reexecute = 0; 1479 pio->io_flags |= ZIO_FLAG_REEXECUTED; 1480 pio->io_error = 0; 1481 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 1482 pio->io_state[w] = 0; 1483 for (int c = 0; c < ZIO_CHILD_TYPES; c++) 1484 pio->io_child_error[c] = 0; 1485 1486 if (IO_IS_ALLOCATING(pio)) 1487 BP_ZERO(pio->io_bp); 1488 1489 /* 1490 * As we reexecute pio's children, new children could be created. 1491 * New children go to the head of pio's io_child_list, however, 1492 * so we will (correctly) not reexecute them. The key is that 1493 * the remainder of pio's io_child_list, from 'cio_next' onward, 1494 * cannot be affected by any side effects of reexecuting 'cio'. 1495 */ 1496 for (cio = zio_walk_children(pio); cio != NULL; cio = cio_next) { 1497 cio_next = zio_walk_children(pio); 1498 mutex_enter(&pio->io_lock); 1499 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 1500 pio->io_children[cio->io_child_type][w]++; 1501 mutex_exit(&pio->io_lock); 1502 zio_reexecute(cio); 1503 } 1504 1505 /* 1506 * Now that all children have been reexecuted, execute the parent. 1507 * We don't reexecute "The Godfather" I/O here as it's the 1508 * responsibility of the caller to wait on him. 1509 */ 1510 if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) 1511 zio_execute(pio); 1512 } 1513 1514 void 1515 zio_suspend(spa_t *spa, zio_t *zio) 1516 { 1517 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC) 1518 fm_panic("Pool '%s' has encountered an uncorrectable I/O " 1519 "failure and the failure mode property for this pool " 1520 "is set to panic.", spa_name(spa)); 1521 1522 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0); 1523 1524 mutex_enter(&spa->spa_suspend_lock); 1525 1526 if (spa->spa_suspend_zio_root == NULL) 1527 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL, 1528 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 1529 ZIO_FLAG_GODFATHER); 1530 1531 spa->spa_suspended = B_TRUE; 1532 1533 if (zio != NULL) { 1534 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER)); 1535 ASSERT(zio != spa->spa_suspend_zio_root); 1536 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1537 ASSERT(zio_unique_parent(zio) == NULL); 1538 ASSERT(zio->io_stage == ZIO_STAGE_DONE); 1539 zio_add_child(spa->spa_suspend_zio_root, zio); 1540 } 1541 1542 mutex_exit(&spa->spa_suspend_lock); 1543 } 1544 1545 int 1546 zio_resume(spa_t *spa) 1547 { 1548 zio_t *pio; 1549 1550 /* 1551 * Reexecute all previously suspended i/o. 1552 */ 1553 mutex_enter(&spa->spa_suspend_lock); 1554 spa->spa_suspended = B_FALSE; 1555 cv_broadcast(&spa->spa_suspend_cv); 1556 pio = spa->spa_suspend_zio_root; 1557 spa->spa_suspend_zio_root = NULL; 1558 mutex_exit(&spa->spa_suspend_lock); 1559 1560 if (pio == NULL) 1561 return (0); 1562 1563 zio_reexecute(pio); 1564 return (zio_wait(pio)); 1565 } 1566 1567 void 1568 zio_resume_wait(spa_t *spa) 1569 { 1570 mutex_enter(&spa->spa_suspend_lock); 1571 while (spa_suspended(spa)) 1572 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock); 1573 mutex_exit(&spa->spa_suspend_lock); 1574 } 1575 1576 /* 1577 * ========================================================================== 1578 * Gang blocks. 1579 * 1580 * A gang block is a collection of small blocks that looks to the DMU 1581 * like one large block. When zio_dva_allocate() cannot find a block 1582 * of the requested size, due to either severe fragmentation or the pool 1583 * being nearly full, it calls zio_write_gang_block() to construct the 1584 * block from smaller fragments. 1585 * 1586 * A gang block consists of a gang header (zio_gbh_phys_t) and up to 1587 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like 1588 * an indirect block: it's an array of block pointers. It consumes 1589 * only one sector and hence is allocatable regardless of fragmentation. 1590 * The gang header's bps point to its gang members, which hold the data. 1591 * 1592 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg> 1593 * as the verifier to ensure uniqueness of the SHA256 checksum. 1594 * Critically, the gang block bp's blk_cksum is the checksum of the data, 1595 * not the gang header. This ensures that data block signatures (needed for 1596 * deduplication) are independent of how the block is physically stored. 1597 * 1598 * Gang blocks can be nested: a gang member may itself be a gang block. 1599 * Thus every gang block is a tree in which root and all interior nodes are 1600 * gang headers, and the leaves are normal blocks that contain user data. 1601 * The root of the gang tree is called the gang leader. 1602 * 1603 * To perform any operation (read, rewrite, free, claim) on a gang block, 1604 * zio_gang_assemble() first assembles the gang tree (minus data leaves) 1605 * in the io_gang_tree field of the original logical i/o by recursively 1606 * reading the gang leader and all gang headers below it. This yields 1607 * an in-core tree containing the contents of every gang header and the 1608 * bps for every constituent of the gang block. 1609 * 1610 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree 1611 * and invokes a callback on each bp. To free a gang block, zio_gang_issue() 1612 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp. 1613 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim(). 1614 * zio_read_gang() is a wrapper around zio_read() that omits reading gang 1615 * headers, since we already have those in io_gang_tree. zio_rewrite_gang() 1616 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite() 1617 * of the gang header plus zio_checksum_compute() of the data to update the 1618 * gang header's blk_cksum as described above. 1619 * 1620 * The two-phase assemble/issue model solves the problem of partial failure -- 1621 * what if you'd freed part of a gang block but then couldn't read the 1622 * gang header for another part? Assembling the entire gang tree first 1623 * ensures that all the necessary gang header I/O has succeeded before 1624 * starting the actual work of free, claim, or write. Once the gang tree 1625 * is assembled, free and claim are in-memory operations that cannot fail. 1626 * 1627 * In the event that a gang write fails, zio_dva_unallocate() walks the 1628 * gang tree to immediately free (i.e. insert back into the space map) 1629 * everything we've allocated. This ensures that we don't get ENOSPC 1630 * errors during repeated suspend/resume cycles due to a flaky device. 1631 * 1632 * Gang rewrites only happen during sync-to-convergence. If we can't assemble 1633 * the gang tree, we won't modify the block, so we can safely defer the free 1634 * (knowing that the block is still intact). If we *can* assemble the gang 1635 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free 1636 * each constituent bp and we can allocate a new block on the next sync pass. 1637 * 1638 * In all cases, the gang tree allows complete recovery from partial failure. 1639 * ========================================================================== 1640 */ 1641 1642 static zio_t * 1643 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data) 1644 { 1645 if (gn != NULL) 1646 return (pio); 1647 1648 return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp), 1649 NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), 1650 &pio->io_bookmark)); 1651 } 1652 1653 zio_t * 1654 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data) 1655 { 1656 zio_t *zio; 1657 1658 if (gn != NULL) { 1659 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp, 1660 gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority, 1661 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 1662 /* 1663 * As we rewrite each gang header, the pipeline will compute 1664 * a new gang block header checksum for it; but no one will 1665 * compute a new data checksum, so we do that here. The one 1666 * exception is the gang leader: the pipeline already computed 1667 * its data checksum because that stage precedes gang assembly. 1668 * (Presently, nothing actually uses interior data checksums; 1669 * this is just good hygiene.) 1670 */ 1671 if (gn != pio->io_gang_leader->io_gang_tree) { 1672 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp), 1673 data, BP_GET_PSIZE(bp)); 1674 } 1675 /* 1676 * If we are here to damage data for testing purposes, 1677 * leave the GBH alone so that we can detect the damage. 1678 */ 1679 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE) 1680 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES; 1681 } else { 1682 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp, 1683 data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority, 1684 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 1685 } 1686 1687 return (zio); 1688 } 1689 1690 /* ARGSUSED */ 1691 zio_t * 1692 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data) 1693 { 1694 return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp, 1695 ZIO_GANG_CHILD_FLAGS(pio))); 1696 } 1697 1698 /* ARGSUSED */ 1699 zio_t * 1700 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data) 1701 { 1702 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp, 1703 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio))); 1704 } 1705 1706 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = { 1707 NULL, 1708 zio_read_gang, 1709 zio_rewrite_gang, 1710 zio_free_gang, 1711 zio_claim_gang, 1712 NULL 1713 }; 1714 1715 static void zio_gang_tree_assemble_done(zio_t *zio); 1716 1717 static zio_gang_node_t * 1718 zio_gang_node_alloc(zio_gang_node_t **gnpp) 1719 { 1720 zio_gang_node_t *gn; 1721 1722 ASSERT(*gnpp == NULL); 1723 1724 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP); 1725 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE); 1726 *gnpp = gn; 1727 1728 return (gn); 1729 } 1730 1731 static void 1732 zio_gang_node_free(zio_gang_node_t **gnpp) 1733 { 1734 zio_gang_node_t *gn = *gnpp; 1735 1736 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) 1737 ASSERT(gn->gn_child[g] == NULL); 1738 1739 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE); 1740 kmem_free(gn, sizeof (*gn)); 1741 *gnpp = NULL; 1742 } 1743 1744 static void 1745 zio_gang_tree_free(zio_gang_node_t **gnpp) 1746 { 1747 zio_gang_node_t *gn = *gnpp; 1748 1749 if (gn == NULL) 1750 return; 1751 1752 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) 1753 zio_gang_tree_free(&gn->gn_child[g]); 1754 1755 zio_gang_node_free(gnpp); 1756 } 1757 1758 static void 1759 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp) 1760 { 1761 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp); 1762 1763 ASSERT(gio->io_gang_leader == gio); 1764 ASSERT(BP_IS_GANG(bp)); 1765 1766 zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh, 1767 SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn, 1768 gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark)); 1769 } 1770 1771 static void 1772 zio_gang_tree_assemble_done(zio_t *zio) 1773 { 1774 zio_t *gio = zio->io_gang_leader; 1775 zio_gang_node_t *gn = zio->io_private; 1776 blkptr_t *bp = zio->io_bp; 1777 1778 ASSERT(gio == zio_unique_parent(zio)); 1779 ASSERT(zio->io_child_count == 0); 1780 1781 if (zio->io_error) 1782 return; 1783 1784 if (BP_SHOULD_BYTESWAP(bp)) 1785 byteswap_uint64_array(zio->io_data, zio->io_size); 1786 1787 ASSERT(zio->io_data == gn->gn_gbh); 1788 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE); 1789 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC); 1790 1791 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 1792 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g]; 1793 if (!BP_IS_GANG(gbp)) 1794 continue; 1795 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]); 1796 } 1797 } 1798 1799 static void 1800 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data) 1801 { 1802 zio_t *gio = pio->io_gang_leader; 1803 zio_t *zio; 1804 1805 ASSERT(BP_IS_GANG(bp) == !!gn); 1806 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp)); 1807 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree); 1808 1809 /* 1810 * If you're a gang header, your data is in gn->gn_gbh. 1811 * If you're a gang member, your data is in 'data' and gn == NULL. 1812 */ 1813 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data); 1814 1815 if (gn != NULL) { 1816 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC); 1817 1818 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 1819 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g]; 1820 if (BP_IS_HOLE(gbp)) 1821 continue; 1822 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data); 1823 data = (char *)data + BP_GET_PSIZE(gbp); 1824 } 1825 } 1826 1827 if (gn == gio->io_gang_tree) 1828 ASSERT3P((char *)gio->io_data + gio->io_size, ==, data); 1829 1830 if (zio != pio) 1831 zio_nowait(zio); 1832 } 1833 1834 static int 1835 zio_gang_assemble(zio_t *zio) 1836 { 1837 blkptr_t *bp = zio->io_bp; 1838 1839 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL); 1840 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 1841 1842 zio->io_gang_leader = zio; 1843 1844 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree); 1845 1846 return (ZIO_PIPELINE_CONTINUE); 1847 } 1848 1849 static int 1850 zio_gang_issue(zio_t *zio) 1851 { 1852 blkptr_t *bp = zio->io_bp; 1853 1854 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE)) 1855 return (ZIO_PIPELINE_STOP); 1856 1857 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio); 1858 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 1859 1860 if (zio->io_child_error[ZIO_CHILD_GANG] == 0) 1861 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data); 1862 else 1863 zio_gang_tree_free(&zio->io_gang_tree); 1864 1865 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1866 1867 return (ZIO_PIPELINE_CONTINUE); 1868 } 1869 1870 static void 1871 zio_write_gang_member_ready(zio_t *zio) 1872 { 1873 zio_t *pio = zio_unique_parent(zio); 1874 zio_t *gio = zio->io_gang_leader; 1875 dva_t *cdva = zio->io_bp->blk_dva; 1876 dva_t *pdva = pio->io_bp->blk_dva; 1877 uint64_t asize; 1878 1879 if (BP_IS_HOLE(zio->io_bp)) 1880 return; 1881 1882 ASSERT(BP_IS_HOLE(&zio->io_bp_orig)); 1883 1884 ASSERT(zio->io_child_type == ZIO_CHILD_GANG); 1885 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies); 1886 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp)); 1887 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp)); 1888 ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp)); 1889 1890 mutex_enter(&pio->io_lock); 1891 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) { 1892 ASSERT(DVA_GET_GANG(&pdva[d])); 1893 asize = DVA_GET_ASIZE(&pdva[d]); 1894 asize += DVA_GET_ASIZE(&cdva[d]); 1895 DVA_SET_ASIZE(&pdva[d], asize); 1896 } 1897 mutex_exit(&pio->io_lock); 1898 } 1899 1900 static int 1901 zio_write_gang_block(zio_t *pio) 1902 { 1903 spa_t *spa = pio->io_spa; 1904 blkptr_t *bp = pio->io_bp; 1905 zio_t *gio = pio->io_gang_leader; 1906 zio_t *zio; 1907 zio_gang_node_t *gn, **gnpp; 1908 zio_gbh_phys_t *gbh; 1909 uint64_t txg = pio->io_txg; 1910 uint64_t resid = pio->io_size; 1911 uint64_t lsize; 1912 int copies = gio->io_prop.zp_copies; 1913 int gbh_copies = MIN(copies + 1, spa_max_replication(spa)); 1914 zio_prop_t zp; 1915 int error; 1916 1917 error = metaslab_alloc(spa, spa_normal_class(spa), SPA_GANGBLOCKSIZE, 1918 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, 1919 METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER); 1920 if (error) { 1921 pio->io_error = error; 1922 return (ZIO_PIPELINE_CONTINUE); 1923 } 1924 1925 if (pio == gio) { 1926 gnpp = &gio->io_gang_tree; 1927 } else { 1928 gnpp = pio->io_private; 1929 ASSERT(pio->io_ready == zio_write_gang_member_ready); 1930 } 1931 1932 gn = zio_gang_node_alloc(gnpp); 1933 gbh = gn->gn_gbh; 1934 bzero(gbh, SPA_GANGBLOCKSIZE); 1935 1936 /* 1937 * Create the gang header. 1938 */ 1939 zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL, 1940 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 1941 1942 /* 1943 * Create and nowait the gang children. 1944 */ 1945 for (int g = 0; resid != 0; resid -= lsize, g++) { 1946 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g), 1947 SPA_MINBLOCKSIZE); 1948 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid); 1949 1950 zp.zp_checksum = gio->io_prop.zp_checksum; 1951 zp.zp_compress = ZIO_COMPRESS_OFF; 1952 zp.zp_type = DMU_OT_NONE; 1953 zp.zp_level = 0; 1954 zp.zp_copies = gio->io_prop.zp_copies; 1955 zp.zp_dedup = B_FALSE; 1956 zp.zp_dedup_verify = B_FALSE; 1957 zp.zp_nopwrite = B_FALSE; 1958 1959 zio_nowait(zio_write(zio, spa, txg, &gbh->zg_blkptr[g], 1960 (char *)pio->io_data + (pio->io_size - resid), lsize, &zp, 1961 zio_write_gang_member_ready, NULL, NULL, &gn->gn_child[g], 1962 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), 1963 &pio->io_bookmark)); 1964 } 1965 1966 /* 1967 * Set pio's pipeline to just wait for zio to finish. 1968 */ 1969 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1970 1971 zio_nowait(zio); 1972 1973 return (ZIO_PIPELINE_CONTINUE); 1974 } 1975 1976 /* 1977 * The zio_nop_write stage in the pipeline determines if allocating 1978 * a new bp is necessary. By leveraging a cryptographically secure checksum, 1979 * such as SHA256, we can compare the checksums of the new data and the old 1980 * to determine if allocating a new block is required. The nopwrite 1981 * feature can handle writes in either syncing or open context (i.e. zil 1982 * writes) and as a result is mutually exclusive with dedup. 1983 */ 1984 static int 1985 zio_nop_write(zio_t *zio) 1986 { 1987 blkptr_t *bp = zio->io_bp; 1988 blkptr_t *bp_orig = &zio->io_bp_orig; 1989 zio_prop_t *zp = &zio->io_prop; 1990 1991 ASSERT(BP_GET_LEVEL(bp) == 0); 1992 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 1993 ASSERT(zp->zp_nopwrite); 1994 ASSERT(!zp->zp_dedup); 1995 ASSERT(zio->io_bp_override == NULL); 1996 ASSERT(IO_IS_ALLOCATING(zio)); 1997 1998 /* 1999 * Check to see if the original bp and the new bp have matching 2000 * characteristics (i.e. same checksum, compression algorithms, etc). 2001 * If they don't then just continue with the pipeline which will 2002 * allocate a new bp. 2003 */ 2004 if (BP_IS_HOLE(bp_orig) || 2005 !zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_dedup || 2006 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) || 2007 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) || 2008 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) || 2009 zp->zp_copies != BP_GET_NDVAS(bp_orig)) 2010 return (ZIO_PIPELINE_CONTINUE); 2011 2012 /* 2013 * If the checksums match then reset the pipeline so that we 2014 * avoid allocating a new bp and issuing any I/O. 2015 */ 2016 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) { 2017 ASSERT(zio_checksum_table[zp->zp_checksum].ci_dedup); 2018 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig)); 2019 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig)); 2020 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF); 2021 ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop, 2022 sizeof (uint64_t)) == 0); 2023 2024 *bp = *bp_orig; 2025 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 2026 zio->io_flags |= ZIO_FLAG_NOPWRITE; 2027 } 2028 2029 return (ZIO_PIPELINE_CONTINUE); 2030 } 2031 2032 /* 2033 * ========================================================================== 2034 * Dedup 2035 * ========================================================================== 2036 */ 2037 static void 2038 zio_ddt_child_read_done(zio_t *zio) 2039 { 2040 blkptr_t *bp = zio->io_bp; 2041 ddt_entry_t *dde = zio->io_private; 2042 ddt_phys_t *ddp; 2043 zio_t *pio = zio_unique_parent(zio); 2044 2045 mutex_enter(&pio->io_lock); 2046 ddp = ddt_phys_select(dde, bp); 2047 if (zio->io_error == 0) 2048 ddt_phys_clear(ddp); /* this ddp doesn't need repair */ 2049 if (zio->io_error == 0 && dde->dde_repair_data == NULL) 2050 dde->dde_repair_data = zio->io_data; 2051 else 2052 zio_buf_free(zio->io_data, zio->io_size); 2053 mutex_exit(&pio->io_lock); 2054 } 2055 2056 static int 2057 zio_ddt_read_start(zio_t *zio) 2058 { 2059 blkptr_t *bp = zio->io_bp; 2060 2061 ASSERT(BP_GET_DEDUP(bp)); 2062 ASSERT(BP_GET_PSIZE(bp) == zio->io_size); 2063 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2064 2065 if (zio->io_child_error[ZIO_CHILD_DDT]) { 2066 ddt_t *ddt = ddt_select(zio->io_spa, bp); 2067 ddt_entry_t *dde = ddt_repair_start(ddt, bp); 2068 ddt_phys_t *ddp = dde->dde_phys; 2069 ddt_phys_t *ddp_self = ddt_phys_select(dde, bp); 2070 blkptr_t blk; 2071 2072 ASSERT(zio->io_vsd == NULL); 2073 zio->io_vsd = dde; 2074 2075 if (ddp_self == NULL) 2076 return (ZIO_PIPELINE_CONTINUE); 2077 2078 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 2079 if (ddp->ddp_phys_birth == 0 || ddp == ddp_self) 2080 continue; 2081 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp, 2082 &blk); 2083 zio_nowait(zio_read(zio, zio->io_spa, &blk, 2084 zio_buf_alloc(zio->io_size), zio->io_size, 2085 zio_ddt_child_read_done, dde, zio->io_priority, 2086 ZIO_DDT_CHILD_FLAGS(zio) | ZIO_FLAG_DONT_PROPAGATE, 2087 &zio->io_bookmark)); 2088 } 2089 return (ZIO_PIPELINE_CONTINUE); 2090 } 2091 2092 zio_nowait(zio_read(zio, zio->io_spa, bp, 2093 zio->io_data, zio->io_size, NULL, NULL, zio->io_priority, 2094 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark)); 2095 2096 return (ZIO_PIPELINE_CONTINUE); 2097 } 2098 2099 static int 2100 zio_ddt_read_done(zio_t *zio) 2101 { 2102 blkptr_t *bp = zio->io_bp; 2103 2104 if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE)) 2105 return (ZIO_PIPELINE_STOP); 2106 2107 ASSERT(BP_GET_DEDUP(bp)); 2108 ASSERT(BP_GET_PSIZE(bp) == zio->io_size); 2109 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2110 2111 if (zio->io_child_error[ZIO_CHILD_DDT]) { 2112 ddt_t *ddt = ddt_select(zio->io_spa, bp); 2113 ddt_entry_t *dde = zio->io_vsd; 2114 if (ddt == NULL) { 2115 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE); 2116 return (ZIO_PIPELINE_CONTINUE); 2117 } 2118 if (dde == NULL) { 2119 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1; 2120 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE); 2121 return (ZIO_PIPELINE_STOP); 2122 } 2123 if (dde->dde_repair_data != NULL) { 2124 bcopy(dde->dde_repair_data, zio->io_data, zio->io_size); 2125 zio->io_child_error[ZIO_CHILD_DDT] = 0; 2126 } 2127 ddt_repair_done(ddt, dde); 2128 zio->io_vsd = NULL; 2129 } 2130 2131 ASSERT(zio->io_vsd == NULL); 2132 2133 return (ZIO_PIPELINE_CONTINUE); 2134 } 2135 2136 static boolean_t 2137 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde) 2138 { 2139 spa_t *spa = zio->io_spa; 2140 2141 /* 2142 * Note: we compare the original data, not the transformed data, 2143 * because when zio->io_bp is an override bp, we will not have 2144 * pushed the I/O transforms. That's an important optimization 2145 * because otherwise we'd compress/encrypt all dmu_sync() data twice. 2146 */ 2147 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) { 2148 zio_t *lio = dde->dde_lead_zio[p]; 2149 2150 if (lio != NULL) { 2151 return (lio->io_orig_size != zio->io_orig_size || 2152 bcmp(zio->io_orig_data, lio->io_orig_data, 2153 zio->io_orig_size) != 0); 2154 } 2155 } 2156 2157 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) { 2158 ddt_phys_t *ddp = &dde->dde_phys[p]; 2159 2160 if (ddp->ddp_phys_birth != 0) { 2161 arc_buf_t *abuf = NULL; 2162 arc_flags_t aflags = ARC_FLAG_WAIT; 2163 blkptr_t blk = *zio->io_bp; 2164 int error; 2165 2166 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth); 2167 2168 ddt_exit(ddt); 2169 2170 error = arc_read(NULL, spa, &blk, 2171 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ, 2172 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 2173 &aflags, &zio->io_bookmark); 2174 2175 if (error == 0) { 2176 if (arc_buf_size(abuf) != zio->io_orig_size || 2177 bcmp(abuf->b_data, zio->io_orig_data, 2178 zio->io_orig_size) != 0) 2179 error = SET_ERROR(EEXIST); 2180 VERIFY(arc_buf_remove_ref(abuf, &abuf)); 2181 } 2182 2183 ddt_enter(ddt); 2184 return (error != 0); 2185 } 2186 } 2187 2188 return (B_FALSE); 2189 } 2190 2191 static void 2192 zio_ddt_child_write_ready(zio_t *zio) 2193 { 2194 int p = zio->io_prop.zp_copies; 2195 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp); 2196 ddt_entry_t *dde = zio->io_private; 2197 ddt_phys_t *ddp = &dde->dde_phys[p]; 2198 zio_t *pio; 2199 2200 if (zio->io_error) 2201 return; 2202 2203 ddt_enter(ddt); 2204 2205 ASSERT(dde->dde_lead_zio[p] == zio); 2206 2207 ddt_phys_fill(ddp, zio->io_bp); 2208 2209 while ((pio = zio_walk_parents(zio)) != NULL) 2210 ddt_bp_fill(ddp, pio->io_bp, zio->io_txg); 2211 2212 ddt_exit(ddt); 2213 } 2214 2215 static void 2216 zio_ddt_child_write_done(zio_t *zio) 2217 { 2218 int p = zio->io_prop.zp_copies; 2219 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp); 2220 ddt_entry_t *dde = zio->io_private; 2221 ddt_phys_t *ddp = &dde->dde_phys[p]; 2222 2223 ddt_enter(ddt); 2224 2225 ASSERT(ddp->ddp_refcnt == 0); 2226 ASSERT(dde->dde_lead_zio[p] == zio); 2227 dde->dde_lead_zio[p] = NULL; 2228 2229 if (zio->io_error == 0) { 2230 while (zio_walk_parents(zio) != NULL) 2231 ddt_phys_addref(ddp); 2232 } else { 2233 ddt_phys_clear(ddp); 2234 } 2235 2236 ddt_exit(ddt); 2237 } 2238 2239 static void 2240 zio_ddt_ditto_write_done(zio_t *zio) 2241 { 2242 int p = DDT_PHYS_DITTO; 2243 zio_prop_t *zp = &zio->io_prop; 2244 blkptr_t *bp = zio->io_bp; 2245 ddt_t *ddt = ddt_select(zio->io_spa, bp); 2246 ddt_entry_t *dde = zio->io_private; 2247 ddt_phys_t *ddp = &dde->dde_phys[p]; 2248 ddt_key_t *ddk = &dde->dde_key; 2249 2250 ddt_enter(ddt); 2251 2252 ASSERT(ddp->ddp_refcnt == 0); 2253 ASSERT(dde->dde_lead_zio[p] == zio); 2254 dde->dde_lead_zio[p] = NULL; 2255 2256 if (zio->io_error == 0) { 2257 ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum)); 2258 ASSERT(zp->zp_copies < SPA_DVAS_PER_BP); 2259 ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp)); 2260 if (ddp->ddp_phys_birth != 0) 2261 ddt_phys_free(ddt, ddk, ddp, zio->io_txg); 2262 ddt_phys_fill(ddp, bp); 2263 } 2264 2265 ddt_exit(ddt); 2266 } 2267 2268 static int 2269 zio_ddt_write(zio_t *zio) 2270 { 2271 spa_t *spa = zio->io_spa; 2272 blkptr_t *bp = zio->io_bp; 2273 uint64_t txg = zio->io_txg; 2274 zio_prop_t *zp = &zio->io_prop; 2275 int p = zp->zp_copies; 2276 int ditto_copies; 2277 zio_t *cio = NULL; 2278 zio_t *dio = NULL; 2279 ddt_t *ddt = ddt_select(spa, bp); 2280 ddt_entry_t *dde; 2281 ddt_phys_t *ddp; 2282 2283 ASSERT(BP_GET_DEDUP(bp)); 2284 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum); 2285 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override); 2286 2287 ddt_enter(ddt); 2288 dde = ddt_lookup(ddt, bp, B_TRUE); 2289 ddp = &dde->dde_phys[p]; 2290 2291 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) { 2292 /* 2293 * If we're using a weak checksum, upgrade to a strong checksum 2294 * and try again. If we're already using a strong checksum, 2295 * we can't resolve it, so just convert to an ordinary write. 2296 * (And automatically e-mail a paper to Nature?) 2297 */ 2298 if (!zio_checksum_table[zp->zp_checksum].ci_dedup) { 2299 zp->zp_checksum = spa_dedup_checksum(spa); 2300 zio_pop_transforms(zio); 2301 zio->io_stage = ZIO_STAGE_OPEN; 2302 BP_ZERO(bp); 2303 } else { 2304 zp->zp_dedup = B_FALSE; 2305 } 2306 zio->io_pipeline = ZIO_WRITE_PIPELINE; 2307 ddt_exit(ddt); 2308 return (ZIO_PIPELINE_CONTINUE); 2309 } 2310 2311 ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp); 2312 ASSERT(ditto_copies < SPA_DVAS_PER_BP); 2313 2314 if (ditto_copies > ddt_ditto_copies_present(dde) && 2315 dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) { 2316 zio_prop_t czp = *zp; 2317 2318 czp.zp_copies = ditto_copies; 2319 2320 /* 2321 * If we arrived here with an override bp, we won't have run 2322 * the transform stack, so we won't have the data we need to 2323 * generate a child i/o. So, toss the override bp and restart. 2324 * This is safe, because using the override bp is just an 2325 * optimization; and it's rare, so the cost doesn't matter. 2326 */ 2327 if (zio->io_bp_override) { 2328 zio_pop_transforms(zio); 2329 zio->io_stage = ZIO_STAGE_OPEN; 2330 zio->io_pipeline = ZIO_WRITE_PIPELINE; 2331 zio->io_bp_override = NULL; 2332 BP_ZERO(bp); 2333 ddt_exit(ddt); 2334 return (ZIO_PIPELINE_CONTINUE); 2335 } 2336 2337 dio = zio_write(zio, spa, txg, bp, zio->io_orig_data, 2338 zio->io_orig_size, &czp, NULL, NULL, 2339 zio_ddt_ditto_write_done, dde, zio->io_priority, 2340 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark); 2341 2342 zio_push_transform(dio, zio->io_data, zio->io_size, 0, NULL); 2343 dde->dde_lead_zio[DDT_PHYS_DITTO] = dio; 2344 } 2345 2346 if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) { 2347 if (ddp->ddp_phys_birth != 0) 2348 ddt_bp_fill(ddp, bp, txg); 2349 if (dde->dde_lead_zio[p] != NULL) 2350 zio_add_child(zio, dde->dde_lead_zio[p]); 2351 else 2352 ddt_phys_addref(ddp); 2353 } else if (zio->io_bp_override) { 2354 ASSERT(bp->blk_birth == txg); 2355 ASSERT(BP_EQUAL(bp, zio->io_bp_override)); 2356 ddt_phys_fill(ddp, bp); 2357 ddt_phys_addref(ddp); 2358 } else { 2359 cio = zio_write(zio, spa, txg, bp, zio->io_orig_data, 2360 zio->io_orig_size, zp, zio_ddt_child_write_ready, NULL, 2361 zio_ddt_child_write_done, dde, zio->io_priority, 2362 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark); 2363 2364 zio_push_transform(cio, zio->io_data, zio->io_size, 0, NULL); 2365 dde->dde_lead_zio[p] = cio; 2366 } 2367 2368 ddt_exit(ddt); 2369 2370 if (cio) 2371 zio_nowait(cio); 2372 if (dio) 2373 zio_nowait(dio); 2374 2375 return (ZIO_PIPELINE_CONTINUE); 2376 } 2377 2378 ddt_entry_t *freedde; /* for debugging */ 2379 2380 static int 2381 zio_ddt_free(zio_t *zio) 2382 { 2383 spa_t *spa = zio->io_spa; 2384 blkptr_t *bp = zio->io_bp; 2385 ddt_t *ddt = ddt_select(spa, bp); 2386 ddt_entry_t *dde; 2387 ddt_phys_t *ddp; 2388 2389 ASSERT(BP_GET_DEDUP(bp)); 2390 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2391 2392 ddt_enter(ddt); 2393 freedde = dde = ddt_lookup(ddt, bp, B_TRUE); 2394 ddp = ddt_phys_select(dde, bp); 2395 ddt_phys_decref(ddp); 2396 ddt_exit(ddt); 2397 2398 return (ZIO_PIPELINE_CONTINUE); 2399 } 2400 2401 /* 2402 * ========================================================================== 2403 * Allocate and free blocks 2404 * ========================================================================== 2405 */ 2406 static int 2407 zio_dva_allocate(zio_t *zio) 2408 { 2409 spa_t *spa = zio->io_spa; 2410 metaslab_class_t *mc = spa_normal_class(spa); 2411 blkptr_t *bp = zio->io_bp; 2412 int error; 2413 int flags = 0; 2414 2415 if (zio->io_gang_leader == NULL) { 2416 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 2417 zio->io_gang_leader = zio; 2418 } 2419 2420 ASSERT(BP_IS_HOLE(bp)); 2421 ASSERT0(BP_GET_NDVAS(bp)); 2422 ASSERT3U(zio->io_prop.zp_copies, >, 0); 2423 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa)); 2424 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp)); 2425 2426 /* 2427 * The dump device does not support gang blocks so allocation on 2428 * behalf of the dump device (i.e. ZIO_FLAG_NODATA) must avoid 2429 * the "fast" gang feature. 2430 */ 2431 flags |= (zio->io_flags & ZIO_FLAG_NODATA) ? METASLAB_GANG_AVOID : 0; 2432 flags |= (zio->io_flags & ZIO_FLAG_GANG_CHILD) ? 2433 METASLAB_GANG_CHILD : 0; 2434 error = metaslab_alloc(spa, mc, zio->io_size, bp, 2435 zio->io_prop.zp_copies, zio->io_txg, NULL, flags); 2436 2437 if (error) { 2438 spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, " 2439 "size %llu, error %d", spa_name(spa), zio, zio->io_size, 2440 error); 2441 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE) 2442 return (zio_write_gang_block(zio)); 2443 zio->io_error = error; 2444 } 2445 2446 return (ZIO_PIPELINE_CONTINUE); 2447 } 2448 2449 static int 2450 zio_dva_free(zio_t *zio) 2451 { 2452 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE); 2453 2454 return (ZIO_PIPELINE_CONTINUE); 2455 } 2456 2457 static int 2458 zio_dva_claim(zio_t *zio) 2459 { 2460 int error; 2461 2462 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg); 2463 if (error) 2464 zio->io_error = error; 2465 2466 return (ZIO_PIPELINE_CONTINUE); 2467 } 2468 2469 /* 2470 * Undo an allocation. This is used by zio_done() when an I/O fails 2471 * and we want to give back the block we just allocated. 2472 * This handles both normal blocks and gang blocks. 2473 */ 2474 static void 2475 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp) 2476 { 2477 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp)); 2478 ASSERT(zio->io_bp_override == NULL); 2479 2480 if (!BP_IS_HOLE(bp)) 2481 metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE); 2482 2483 if (gn != NULL) { 2484 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 2485 zio_dva_unallocate(zio, gn->gn_child[g], 2486 &gn->gn_gbh->zg_blkptr[g]); 2487 } 2488 } 2489 } 2490 2491 /* 2492 * Try to allocate an intent log block. Return 0 on success, errno on failure. 2493 */ 2494 int 2495 zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, blkptr_t *old_bp, 2496 uint64_t size, boolean_t use_slog) 2497 { 2498 int error = 1; 2499 2500 ASSERT(txg > spa_syncing_txg(spa)); 2501 2502 /* 2503 * ZIL blocks are always contiguous (i.e. not gang blocks) so we 2504 * set the METASLAB_GANG_AVOID flag so that they don't "fast gang" 2505 * when allocating them. 2506 */ 2507 if (use_slog) { 2508 error = metaslab_alloc(spa, spa_log_class(spa), size, 2509 new_bp, 1, txg, old_bp, 2510 METASLAB_HINTBP_AVOID | METASLAB_GANG_AVOID); 2511 } 2512 2513 if (error) { 2514 error = metaslab_alloc(spa, spa_normal_class(spa), size, 2515 new_bp, 1, txg, old_bp, 2516 METASLAB_HINTBP_AVOID); 2517 } 2518 2519 if (error == 0) { 2520 BP_SET_LSIZE(new_bp, size); 2521 BP_SET_PSIZE(new_bp, size); 2522 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF); 2523 BP_SET_CHECKSUM(new_bp, 2524 spa_version(spa) >= SPA_VERSION_SLIM_ZIL 2525 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG); 2526 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG); 2527 BP_SET_LEVEL(new_bp, 0); 2528 BP_SET_DEDUP(new_bp, 0); 2529 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER); 2530 } 2531 2532 return (error); 2533 } 2534 2535 /* 2536 * Free an intent log block. 2537 */ 2538 void 2539 zio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp) 2540 { 2541 ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG); 2542 ASSERT(!BP_IS_GANG(bp)); 2543 2544 zio_free(spa, txg, bp); 2545 } 2546 2547 /* 2548 * ========================================================================== 2549 * Read and write to physical devices 2550 * ========================================================================== 2551 */ 2552 2553 2554 /* 2555 * Issue an I/O to the underlying vdev. Typically the issue pipeline 2556 * stops after this stage and will resume upon I/O completion. 2557 * However, there are instances where the vdev layer may need to 2558 * continue the pipeline when an I/O was not issued. Since the I/O 2559 * that was sent to the vdev layer might be different than the one 2560 * currently active in the pipeline (see vdev_queue_io()), we explicitly 2561 * force the underlying vdev layers to call either zio_execute() or 2562 * zio_interrupt() to ensure that the pipeline continues with the correct I/O. 2563 */ 2564 static int 2565 zio_vdev_io_start(zio_t *zio) 2566 { 2567 vdev_t *vd = zio->io_vd; 2568 uint64_t align; 2569 spa_t *spa = zio->io_spa; 2570 2571 ASSERT(zio->io_error == 0); 2572 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0); 2573 2574 if (vd == NULL) { 2575 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER)) 2576 spa_config_enter(spa, SCL_ZIO, zio, RW_READER); 2577 2578 /* 2579 * The mirror_ops handle multiple DVAs in a single BP. 2580 */ 2581 vdev_mirror_ops.vdev_op_io_start(zio); 2582 return (ZIO_PIPELINE_STOP); 2583 } 2584 2585 /* 2586 * We keep track of time-sensitive I/Os so that the scan thread 2587 * can quickly react to certain workloads. In particular, we care 2588 * about non-scrubbing, top-level reads and writes with the following 2589 * characteristics: 2590 * - synchronous writes of user data to non-slog devices 2591 * - any reads of user data 2592 * When these conditions are met, adjust the timestamp of spa_last_io 2593 * which allows the scan thread to adjust its workload accordingly. 2594 */ 2595 if (!(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && zio->io_bp != NULL && 2596 vd == vd->vdev_top && !vd->vdev_islog && 2597 zio->io_bookmark.zb_objset != DMU_META_OBJSET && 2598 zio->io_txg != spa_syncing_txg(spa)) { 2599 uint64_t old = spa->spa_last_io; 2600 uint64_t new = ddi_get_lbolt64(); 2601 if (old != new) 2602 (void) atomic_cas_64(&spa->spa_last_io, old, new); 2603 } 2604 2605 align = 1ULL << vd->vdev_top->vdev_ashift; 2606 2607 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) && 2608 P2PHASE(zio->io_size, align) != 0) { 2609 /* Transform logical writes to be a full physical block size. */ 2610 uint64_t asize = P2ROUNDUP(zio->io_size, align); 2611 char *abuf = zio_buf_alloc(asize); 2612 ASSERT(vd == vd->vdev_top); 2613 if (zio->io_type == ZIO_TYPE_WRITE) { 2614 bcopy(zio->io_data, abuf, zio->io_size); 2615 bzero(abuf + zio->io_size, asize - zio->io_size); 2616 } 2617 zio_push_transform(zio, abuf, asize, asize, zio_subblock); 2618 } 2619 2620 /* 2621 * If this is not a physical io, make sure that it is properly aligned 2622 * before proceeding. 2623 */ 2624 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) { 2625 ASSERT0(P2PHASE(zio->io_offset, align)); 2626 ASSERT0(P2PHASE(zio->io_size, align)); 2627 } else { 2628 /* 2629 * For physical writes, we allow 512b aligned writes and assume 2630 * the device will perform a read-modify-write as necessary. 2631 */ 2632 ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE)); 2633 ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE)); 2634 } 2635 2636 VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa)); 2637 2638 /* 2639 * If this is a repair I/O, and there's no self-healing involved -- 2640 * that is, we're just resilvering what we expect to resilver -- 2641 * then don't do the I/O unless zio's txg is actually in vd's DTL. 2642 * This prevents spurious resilvering with nested replication. 2643 * For example, given a mirror of mirrors, (A+B)+(C+D), if only 2644 * A is out of date, we'll read from C+D, then use the data to 2645 * resilver A+B -- but we don't actually want to resilver B, just A. 2646 * The top-level mirror has no way to know this, so instead we just 2647 * discard unnecessary repairs as we work our way down the vdev tree. 2648 * The same logic applies to any form of nested replication: 2649 * ditto + mirror, RAID-Z + replacing, etc. This covers them all. 2650 */ 2651 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) && 2652 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) && 2653 zio->io_txg != 0 && /* not a delegated i/o */ 2654 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) { 2655 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 2656 zio_vdev_io_bypass(zio); 2657 return (ZIO_PIPELINE_CONTINUE); 2658 } 2659 2660 if (vd->vdev_ops->vdev_op_leaf && 2661 (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) { 2662 2663 if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio)) 2664 return (ZIO_PIPELINE_CONTINUE); 2665 2666 if ((zio = vdev_queue_io(zio)) == NULL) 2667 return (ZIO_PIPELINE_STOP); 2668 2669 if (!vdev_accessible(vd, zio)) { 2670 zio->io_error = SET_ERROR(ENXIO); 2671 zio_interrupt(zio); 2672 return (ZIO_PIPELINE_STOP); 2673 } 2674 } 2675 2676 vd->vdev_ops->vdev_op_io_start(zio); 2677 return (ZIO_PIPELINE_STOP); 2678 } 2679 2680 static int 2681 zio_vdev_io_done(zio_t *zio) 2682 { 2683 vdev_t *vd = zio->io_vd; 2684 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops; 2685 boolean_t unexpected_error = B_FALSE; 2686 2687 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE)) 2688 return (ZIO_PIPELINE_STOP); 2689 2690 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE); 2691 2692 if (vd != NULL && vd->vdev_ops->vdev_op_leaf) { 2693 2694 vdev_queue_io_done(zio); 2695 2696 if (zio->io_type == ZIO_TYPE_WRITE) 2697 vdev_cache_write(zio); 2698 2699 if (zio_injection_enabled && zio->io_error == 0) 2700 zio->io_error = zio_handle_device_injection(vd, 2701 zio, EIO); 2702 2703 if (zio_injection_enabled && zio->io_error == 0) 2704 zio->io_error = zio_handle_label_injection(zio, EIO); 2705 2706 if (zio->io_error) { 2707 if (!vdev_accessible(vd, zio)) { 2708 zio->io_error = SET_ERROR(ENXIO); 2709 } else { 2710 unexpected_error = B_TRUE; 2711 } 2712 } 2713 } 2714 2715 ops->vdev_op_io_done(zio); 2716 2717 if (unexpected_error) 2718 VERIFY(vdev_probe(vd, zio) == NULL); 2719 2720 return (ZIO_PIPELINE_CONTINUE); 2721 } 2722 2723 /* 2724 * For non-raidz ZIOs, we can just copy aside the bad data read from the 2725 * disk, and use that to finish the checksum ereport later. 2726 */ 2727 static void 2728 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr, 2729 const void *good_buf) 2730 { 2731 /* no processing needed */ 2732 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE); 2733 } 2734 2735 /*ARGSUSED*/ 2736 void 2737 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored) 2738 { 2739 void *buf = zio_buf_alloc(zio->io_size); 2740 2741 bcopy(zio->io_data, buf, zio->io_size); 2742 2743 zcr->zcr_cbinfo = zio->io_size; 2744 zcr->zcr_cbdata = buf; 2745 zcr->zcr_finish = zio_vsd_default_cksum_finish; 2746 zcr->zcr_free = zio_buf_free; 2747 } 2748 2749 static int 2750 zio_vdev_io_assess(zio_t *zio) 2751 { 2752 vdev_t *vd = zio->io_vd; 2753 2754 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE)) 2755 return (ZIO_PIPELINE_STOP); 2756 2757 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER)) 2758 spa_config_exit(zio->io_spa, SCL_ZIO, zio); 2759 2760 if (zio->io_vsd != NULL) { 2761 zio->io_vsd_ops->vsd_free(zio); 2762 zio->io_vsd = NULL; 2763 } 2764 2765 if (zio_injection_enabled && zio->io_error == 0) 2766 zio->io_error = zio_handle_fault_injection(zio, EIO); 2767 2768 /* 2769 * If the I/O failed, determine whether we should attempt to retry it. 2770 * 2771 * On retry, we cut in line in the issue queue, since we don't want 2772 * compression/checksumming/etc. work to prevent our (cheap) IO reissue. 2773 */ 2774 if (zio->io_error && vd == NULL && 2775 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) { 2776 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */ 2777 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */ 2778 zio->io_error = 0; 2779 zio->io_flags |= ZIO_FLAG_IO_RETRY | 2780 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE; 2781 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1; 2782 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, 2783 zio_requeue_io_start_cut_in_line); 2784 return (ZIO_PIPELINE_STOP); 2785 } 2786 2787 /* 2788 * If we got an error on a leaf device, convert it to ENXIO 2789 * if the device is not accessible at all. 2790 */ 2791 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf && 2792 !vdev_accessible(vd, zio)) 2793 zio->io_error = SET_ERROR(ENXIO); 2794 2795 /* 2796 * If we can't write to an interior vdev (mirror or RAID-Z), 2797 * set vdev_cant_write so that we stop trying to allocate from it. 2798 */ 2799 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE && 2800 vd != NULL && !vd->vdev_ops->vdev_op_leaf) { 2801 vd->vdev_cant_write = B_TRUE; 2802 } 2803 2804 if (zio->io_error) 2805 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 2806 2807 if (vd != NULL && vd->vdev_ops->vdev_op_leaf && 2808 zio->io_physdone != NULL) { 2809 ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED)); 2810 ASSERT(zio->io_child_type == ZIO_CHILD_VDEV); 2811 zio->io_physdone(zio->io_logical); 2812 } 2813 2814 return (ZIO_PIPELINE_CONTINUE); 2815 } 2816 2817 void 2818 zio_vdev_io_reissue(zio_t *zio) 2819 { 2820 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 2821 ASSERT(zio->io_error == 0); 2822 2823 zio->io_stage >>= 1; 2824 } 2825 2826 void 2827 zio_vdev_io_redone(zio_t *zio) 2828 { 2829 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE); 2830 2831 zio->io_stage >>= 1; 2832 } 2833 2834 void 2835 zio_vdev_io_bypass(zio_t *zio) 2836 { 2837 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 2838 ASSERT(zio->io_error == 0); 2839 2840 zio->io_flags |= ZIO_FLAG_IO_BYPASS; 2841 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1; 2842 } 2843 2844 /* 2845 * ========================================================================== 2846 * Generate and verify checksums 2847 * ========================================================================== 2848 */ 2849 static int 2850 zio_checksum_generate(zio_t *zio) 2851 { 2852 blkptr_t *bp = zio->io_bp; 2853 enum zio_checksum checksum; 2854 2855 if (bp == NULL) { 2856 /* 2857 * This is zio_write_phys(). 2858 * We're either generating a label checksum, or none at all. 2859 */ 2860 checksum = zio->io_prop.zp_checksum; 2861 2862 if (checksum == ZIO_CHECKSUM_OFF) 2863 return (ZIO_PIPELINE_CONTINUE); 2864 2865 ASSERT(checksum == ZIO_CHECKSUM_LABEL); 2866 } else { 2867 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) { 2868 ASSERT(!IO_IS_ALLOCATING(zio)); 2869 checksum = ZIO_CHECKSUM_GANG_HEADER; 2870 } else { 2871 checksum = BP_GET_CHECKSUM(bp); 2872 } 2873 } 2874 2875 zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size); 2876 2877 return (ZIO_PIPELINE_CONTINUE); 2878 } 2879 2880 static int 2881 zio_checksum_verify(zio_t *zio) 2882 { 2883 zio_bad_cksum_t info; 2884 blkptr_t *bp = zio->io_bp; 2885 int error; 2886 2887 ASSERT(zio->io_vd != NULL); 2888 2889 if (bp == NULL) { 2890 /* 2891 * This is zio_read_phys(). 2892 * We're either verifying a label checksum, or nothing at all. 2893 */ 2894 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF) 2895 return (ZIO_PIPELINE_CONTINUE); 2896 2897 ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL); 2898 } 2899 2900 if ((error = zio_checksum_error(zio, &info)) != 0) { 2901 zio->io_error = error; 2902 if (error == ECKSUM && 2903 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 2904 zfs_ereport_start_checksum(zio->io_spa, 2905 zio->io_vd, zio, zio->io_offset, 2906 zio->io_size, NULL, &info); 2907 } 2908 } 2909 2910 return (ZIO_PIPELINE_CONTINUE); 2911 } 2912 2913 /* 2914 * Called by RAID-Z to ensure we don't compute the checksum twice. 2915 */ 2916 void 2917 zio_checksum_verified(zio_t *zio) 2918 { 2919 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY; 2920 } 2921 2922 /* 2923 * ========================================================================== 2924 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other. 2925 * An error of 0 indicates success. ENXIO indicates whole-device failure, 2926 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO 2927 * indicate errors that are specific to one I/O, and most likely permanent. 2928 * Any other error is presumed to be worse because we weren't expecting it. 2929 * ========================================================================== 2930 */ 2931 int 2932 zio_worst_error(int e1, int e2) 2933 { 2934 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO }; 2935 int r1, r2; 2936 2937 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++) 2938 if (e1 == zio_error_rank[r1]) 2939 break; 2940 2941 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++) 2942 if (e2 == zio_error_rank[r2]) 2943 break; 2944 2945 return (r1 > r2 ? e1 : e2); 2946 } 2947 2948 /* 2949 * ========================================================================== 2950 * I/O completion 2951 * ========================================================================== 2952 */ 2953 static int 2954 zio_ready(zio_t *zio) 2955 { 2956 blkptr_t *bp = zio->io_bp; 2957 zio_t *pio, *pio_next; 2958 2959 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) || 2960 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY)) 2961 return (ZIO_PIPELINE_STOP); 2962 2963 if (zio->io_ready) { 2964 ASSERT(IO_IS_ALLOCATING(zio)); 2965 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) || 2966 (zio->io_flags & ZIO_FLAG_NOPWRITE)); 2967 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0); 2968 2969 zio->io_ready(zio); 2970 } 2971 2972 if (bp != NULL && bp != &zio->io_bp_copy) 2973 zio->io_bp_copy = *bp; 2974 2975 if (zio->io_error) 2976 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 2977 2978 mutex_enter(&zio->io_lock); 2979 zio->io_state[ZIO_WAIT_READY] = 1; 2980 pio = zio_walk_parents(zio); 2981 mutex_exit(&zio->io_lock); 2982 2983 /* 2984 * As we notify zio's parents, new parents could be added. 2985 * New parents go to the head of zio's io_parent_list, however, 2986 * so we will (correctly) not notify them. The remainder of zio's 2987 * io_parent_list, from 'pio_next' onward, cannot change because 2988 * all parents must wait for us to be done before they can be done. 2989 */ 2990 for (; pio != NULL; pio = pio_next) { 2991 pio_next = zio_walk_parents(zio); 2992 zio_notify_parent(pio, zio, ZIO_WAIT_READY); 2993 } 2994 2995 if (zio->io_flags & ZIO_FLAG_NODATA) { 2996 if (BP_IS_GANG(bp)) { 2997 zio->io_flags &= ~ZIO_FLAG_NODATA; 2998 } else { 2999 ASSERT((uintptr_t)zio->io_data < SPA_MAXBLOCKSIZE); 3000 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES; 3001 } 3002 } 3003 3004 if (zio_injection_enabled && 3005 zio->io_spa->spa_syncing_txg == zio->io_txg) 3006 zio_handle_ignored_writes(zio); 3007 3008 return (ZIO_PIPELINE_CONTINUE); 3009 } 3010 3011 static int 3012 zio_done(zio_t *zio) 3013 { 3014 spa_t *spa = zio->io_spa; 3015 zio_t *lio = zio->io_logical; 3016 blkptr_t *bp = zio->io_bp; 3017 vdev_t *vd = zio->io_vd; 3018 uint64_t psize = zio->io_size; 3019 zio_t *pio, *pio_next; 3020 3021 /* 3022 * If our children haven't all completed, 3023 * wait for them and then repeat this pipeline stage. 3024 */ 3025 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) || 3026 zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) || 3027 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) || 3028 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE)) 3029 return (ZIO_PIPELINE_STOP); 3030 3031 for (int c = 0; c < ZIO_CHILD_TYPES; c++) 3032 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 3033 ASSERT(zio->io_children[c][w] == 0); 3034 3035 if (bp != NULL && !BP_IS_EMBEDDED(bp)) { 3036 ASSERT(bp->blk_pad[0] == 0); 3037 ASSERT(bp->blk_pad[1] == 0); 3038 ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 || 3039 (bp == zio_unique_parent(zio)->io_bp)); 3040 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) && 3041 zio->io_bp_override == NULL && 3042 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) { 3043 ASSERT(!BP_SHOULD_BYTESWAP(bp)); 3044 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp)); 3045 ASSERT(BP_COUNT_GANG(bp) == 0 || 3046 (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp))); 3047 } 3048 if (zio->io_flags & ZIO_FLAG_NOPWRITE) 3049 VERIFY(BP_EQUAL(bp, &zio->io_bp_orig)); 3050 } 3051 3052 /* 3053 * If there were child vdev/gang/ddt errors, they apply to us now. 3054 */ 3055 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV); 3056 zio_inherit_child_errors(zio, ZIO_CHILD_GANG); 3057 zio_inherit_child_errors(zio, ZIO_CHILD_DDT); 3058 3059 /* 3060 * If the I/O on the transformed data was successful, generate any 3061 * checksum reports now while we still have the transformed data. 3062 */ 3063 if (zio->io_error == 0) { 3064 while (zio->io_cksum_report != NULL) { 3065 zio_cksum_report_t *zcr = zio->io_cksum_report; 3066 uint64_t align = zcr->zcr_align; 3067 uint64_t asize = P2ROUNDUP(psize, align); 3068 char *abuf = zio->io_data; 3069 3070 if (asize != psize) { 3071 abuf = zio_buf_alloc(asize); 3072 bcopy(zio->io_data, abuf, psize); 3073 bzero(abuf + psize, asize - psize); 3074 } 3075 3076 zio->io_cksum_report = zcr->zcr_next; 3077 zcr->zcr_next = NULL; 3078 zcr->zcr_finish(zcr, abuf); 3079 zfs_ereport_free_checksum(zcr); 3080 3081 if (asize != psize) 3082 zio_buf_free(abuf, asize); 3083 } 3084 } 3085 3086 zio_pop_transforms(zio); /* note: may set zio->io_error */ 3087 3088 vdev_stat_update(zio, psize); 3089 3090 if (zio->io_error) { 3091 /* 3092 * If this I/O is attached to a particular vdev, 3093 * generate an error message describing the I/O failure 3094 * at the block level. We ignore these errors if the 3095 * device is currently unavailable. 3096 */ 3097 if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd)) 3098 zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0); 3099 3100 if ((zio->io_error == EIO || !(zio->io_flags & 3101 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) && 3102 zio == lio) { 3103 /* 3104 * For logical I/O requests, tell the SPA to log the 3105 * error and generate a logical data ereport. 3106 */ 3107 spa_log_error(spa, zio); 3108 zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio, 3109 0, 0); 3110 } 3111 } 3112 3113 if (zio->io_error && zio == lio) { 3114 /* 3115 * Determine whether zio should be reexecuted. This will 3116 * propagate all the way to the root via zio_notify_parent(). 3117 */ 3118 ASSERT(vd == NULL && bp != NULL); 3119 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 3120 3121 if (IO_IS_ALLOCATING(zio) && 3122 !(zio->io_flags & ZIO_FLAG_CANFAIL)) { 3123 if (zio->io_error != ENOSPC) 3124 zio->io_reexecute |= ZIO_REEXECUTE_NOW; 3125 else 3126 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 3127 } 3128 3129 if ((zio->io_type == ZIO_TYPE_READ || 3130 zio->io_type == ZIO_TYPE_FREE) && 3131 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && 3132 zio->io_error == ENXIO && 3133 spa_load_state(spa) == SPA_LOAD_NONE && 3134 spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE) 3135 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 3136 3137 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute) 3138 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 3139 3140 /* 3141 * Here is a possibly good place to attempt to do 3142 * either combinatorial reconstruction or error correction 3143 * based on checksums. It also might be a good place 3144 * to send out preliminary ereports before we suspend 3145 * processing. 3146 */ 3147 } 3148 3149 /* 3150 * If there were logical child errors, they apply to us now. 3151 * We defer this until now to avoid conflating logical child 3152 * errors with errors that happened to the zio itself when 3153 * updating vdev stats and reporting FMA events above. 3154 */ 3155 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL); 3156 3157 if ((zio->io_error || zio->io_reexecute) && 3158 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio && 3159 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE))) 3160 zio_dva_unallocate(zio, zio->io_gang_tree, bp); 3161 3162 zio_gang_tree_free(&zio->io_gang_tree); 3163 3164 /* 3165 * Godfather I/Os should never suspend. 3166 */ 3167 if ((zio->io_flags & ZIO_FLAG_GODFATHER) && 3168 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) 3169 zio->io_reexecute = 0; 3170 3171 if (zio->io_reexecute) { 3172 /* 3173 * This is a logical I/O that wants to reexecute. 3174 * 3175 * Reexecute is top-down. When an i/o fails, if it's not 3176 * the root, it simply notifies its parent and sticks around. 3177 * The parent, seeing that it still has children in zio_done(), 3178 * does the same. This percolates all the way up to the root. 3179 * The root i/o will reexecute or suspend the entire tree. 3180 * 3181 * This approach ensures that zio_reexecute() honors 3182 * all the original i/o dependency relationships, e.g. 3183 * parents not executing until children are ready. 3184 */ 3185 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 3186 3187 zio->io_gang_leader = NULL; 3188 3189 mutex_enter(&zio->io_lock); 3190 zio->io_state[ZIO_WAIT_DONE] = 1; 3191 mutex_exit(&zio->io_lock); 3192 3193 /* 3194 * "The Godfather" I/O monitors its children but is 3195 * not a true parent to them. It will track them through 3196 * the pipeline but severs its ties whenever they get into 3197 * trouble (e.g. suspended). This allows "The Godfather" 3198 * I/O to return status without blocking. 3199 */ 3200 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) { 3201 zio_link_t *zl = zio->io_walk_link; 3202 pio_next = zio_walk_parents(zio); 3203 3204 if ((pio->io_flags & ZIO_FLAG_GODFATHER) && 3205 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) { 3206 zio_remove_child(pio, zio, zl); 3207 zio_notify_parent(pio, zio, ZIO_WAIT_DONE); 3208 } 3209 } 3210 3211 if ((pio = zio_unique_parent(zio)) != NULL) { 3212 /* 3213 * We're not a root i/o, so there's nothing to do 3214 * but notify our parent. Don't propagate errors 3215 * upward since we haven't permanently failed yet. 3216 */ 3217 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER)); 3218 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE; 3219 zio_notify_parent(pio, zio, ZIO_WAIT_DONE); 3220 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) { 3221 /* 3222 * We'd fail again if we reexecuted now, so suspend 3223 * until conditions improve (e.g. device comes online). 3224 */ 3225 zio_suspend(spa, zio); 3226 } else { 3227 /* 3228 * Reexecution is potentially a huge amount of work. 3229 * Hand it off to the otherwise-unused claim taskq. 3230 */ 3231 ASSERT(zio->io_tqent.tqent_next == NULL); 3232 spa_taskq_dispatch_ent(spa, ZIO_TYPE_CLAIM, 3233 ZIO_TASKQ_ISSUE, (task_func_t *)zio_reexecute, zio, 3234 0, &zio->io_tqent); 3235 } 3236 return (ZIO_PIPELINE_STOP); 3237 } 3238 3239 ASSERT(zio->io_child_count == 0); 3240 ASSERT(zio->io_reexecute == 0); 3241 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL)); 3242 3243 /* 3244 * Report any checksum errors, since the I/O is complete. 3245 */ 3246 while (zio->io_cksum_report != NULL) { 3247 zio_cksum_report_t *zcr = zio->io_cksum_report; 3248 zio->io_cksum_report = zcr->zcr_next; 3249 zcr->zcr_next = NULL; 3250 zcr->zcr_finish(zcr, NULL); 3251 zfs_ereport_free_checksum(zcr); 3252 } 3253 3254 /* 3255 * It is the responsibility of the done callback to ensure that this 3256 * particular zio is no longer discoverable for adoption, and as 3257 * such, cannot acquire any new parents. 3258 */ 3259 if (zio->io_done) 3260 zio->io_done(zio); 3261 3262 mutex_enter(&zio->io_lock); 3263 zio->io_state[ZIO_WAIT_DONE] = 1; 3264 mutex_exit(&zio->io_lock); 3265 3266 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) { 3267 zio_link_t *zl = zio->io_walk_link; 3268 pio_next = zio_walk_parents(zio); 3269 zio_remove_child(pio, zio, zl); 3270 zio_notify_parent(pio, zio, ZIO_WAIT_DONE); 3271 } 3272 3273 if (zio->io_waiter != NULL) { 3274 mutex_enter(&zio->io_lock); 3275 zio->io_executor = NULL; 3276 cv_broadcast(&zio->io_cv); 3277 mutex_exit(&zio->io_lock); 3278 } else { 3279 zio_destroy(zio); 3280 } 3281 3282 return (ZIO_PIPELINE_STOP); 3283 } 3284 3285 /* 3286 * ========================================================================== 3287 * I/O pipeline definition 3288 * ========================================================================== 3289 */ 3290 static zio_pipe_stage_t *zio_pipeline[] = { 3291 NULL, 3292 zio_read_bp_init, 3293 zio_free_bp_init, 3294 zio_issue_async, 3295 zio_write_bp_init, 3296 zio_checksum_generate, 3297 zio_nop_write, 3298 zio_ddt_read_start, 3299 zio_ddt_read_done, 3300 zio_ddt_write, 3301 zio_ddt_free, 3302 zio_gang_assemble, 3303 zio_gang_issue, 3304 zio_dva_allocate, 3305 zio_dva_free, 3306 zio_dva_claim, 3307 zio_ready, 3308 zio_vdev_io_start, 3309 zio_vdev_io_done, 3310 zio_vdev_io_assess, 3311 zio_checksum_verify, 3312 zio_done 3313 }; 3314 3315 /* dnp is the dnode for zb1->zb_object */ 3316 boolean_t 3317 zbookmark_is_before(const dnode_phys_t *dnp, const zbookmark_phys_t *zb1, 3318 const zbookmark_phys_t *zb2) 3319 { 3320 uint64_t zb1nextL0, zb2thisobj; 3321 3322 ASSERT(zb1->zb_objset == zb2->zb_objset); 3323 ASSERT(zb2->zb_level == 0); 3324 3325 /* The objset_phys_t isn't before anything. */ 3326 if (dnp == NULL) 3327 return (B_FALSE); 3328 3329 zb1nextL0 = (zb1->zb_blkid + 1) << 3330 ((zb1->zb_level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)); 3331 3332 zb2thisobj = zb2->zb_object ? zb2->zb_object : 3333 zb2->zb_blkid << (DNODE_BLOCK_SHIFT - DNODE_SHIFT); 3334 3335 if (zb1->zb_object == DMU_META_DNODE_OBJECT) { 3336 uint64_t nextobj = zb1nextL0 * 3337 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT) >> DNODE_SHIFT; 3338 return (nextobj <= zb2thisobj); 3339 } 3340 3341 if (zb1->zb_object < zb2thisobj) 3342 return (B_TRUE); 3343 if (zb1->zb_object > zb2thisobj) 3344 return (B_FALSE); 3345 if (zb2->zb_object == DMU_META_DNODE_OBJECT) 3346 return (B_FALSE); 3347 return (zb1nextL0 <= zb2->zb_blkid); 3348 } 3349