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 https://opensource.org/licenses/CDDL-1.0. 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, 2020 by Delphix. All rights reserved. 24 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved. 25 * Copyright (c) 2017, Intel Corporation. 26 * Copyright (c) 2019, Klara Inc. 27 * Copyright (c) 2019, Allan Jude 28 * Copyright (c) 2021, Datto, Inc. 29 */ 30 31 #include <sys/sysmacros.h> 32 #include <sys/zfs_context.h> 33 #include <sys/fm/fs/zfs.h> 34 #include <sys/spa.h> 35 #include <sys/txg.h> 36 #include <sys/spa_impl.h> 37 #include <sys/vdev_impl.h> 38 #include <sys/vdev_trim.h> 39 #include <sys/zio_impl.h> 40 #include <sys/zio_compress.h> 41 #include <sys/zio_checksum.h> 42 #include <sys/dmu_objset.h> 43 #include <sys/arc.h> 44 #include <sys/ddt.h> 45 #include <sys/blkptr.h> 46 #include <sys/zfeature.h> 47 #include <sys/dsl_scan.h> 48 #include <sys/metaslab_impl.h> 49 #include <sys/time.h> 50 #include <sys/trace_zfs.h> 51 #include <sys/abd.h> 52 #include <sys/dsl_crypt.h> 53 #include <cityhash.h> 54 55 /* 56 * ========================================================================== 57 * I/O type descriptions 58 * ========================================================================== 59 */ 60 const char *const zio_type_name[ZIO_TYPES] = { 61 /* 62 * Note: Linux kernel thread name length is limited 63 * so these names will differ from upstream open zfs. 64 */ 65 "z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_ioctl", "z_trim" 66 }; 67 68 int zio_dva_throttle_enabled = B_TRUE; 69 static int zio_deadman_log_all = B_FALSE; 70 71 /* 72 * ========================================================================== 73 * I/O kmem caches 74 * ========================================================================== 75 */ 76 static kmem_cache_t *zio_cache; 77 static kmem_cache_t *zio_link_cache; 78 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 79 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 80 #if defined(ZFS_DEBUG) && !defined(_KERNEL) 81 static uint64_t zio_buf_cache_allocs[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 82 static uint64_t zio_buf_cache_frees[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 83 #endif 84 85 /* Mark IOs as "slow" if they take longer than 30 seconds */ 86 static uint_t zio_slow_io_ms = (30 * MILLISEC); 87 88 #define BP_SPANB(indblkshift, level) \ 89 (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT))) 90 #define COMPARE_META_LEVEL 0x80000000ul 91 /* 92 * The following actions directly effect the spa's sync-to-convergence logic. 93 * The values below define the sync pass when we start performing the action. 94 * Care should be taken when changing these values as they directly impact 95 * spa_sync() performance. Tuning these values may introduce subtle performance 96 * pathologies and should only be done in the context of performance analysis. 97 * These tunables will eventually be removed and replaced with #defines once 98 * enough analysis has been done to determine optimal values. 99 * 100 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that 101 * regular blocks are not deferred. 102 * 103 * Starting in sync pass 8 (zfs_sync_pass_dont_compress), we disable 104 * compression (including of metadata). In practice, we don't have this 105 * many sync passes, so this has no effect. 106 * 107 * The original intent was that disabling compression would help the sync 108 * passes to converge. However, in practice disabling compression increases 109 * the average number of sync passes, because when we turn compression off, a 110 * lot of block's size will change and thus we have to re-allocate (not 111 * overwrite) them. It also increases the number of 128KB allocations (e.g. 112 * for indirect blocks and spacemaps) because these will not be compressed. 113 * The 128K allocations are especially detrimental to performance on highly 114 * fragmented systems, which may have very few free segments of this size, 115 * and may need to load new metaslabs to satisfy 128K allocations. 116 */ 117 118 /* defer frees starting in this pass */ 119 uint_t zfs_sync_pass_deferred_free = 2; 120 121 /* don't compress starting in this pass */ 122 static uint_t zfs_sync_pass_dont_compress = 8; 123 124 /* rewrite new bps starting in this pass */ 125 static uint_t zfs_sync_pass_rewrite = 2; 126 127 /* 128 * An allocating zio is one that either currently has the DVA allocate 129 * stage set or will have it later in its lifetime. 130 */ 131 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE) 132 133 /* 134 * Enable smaller cores by excluding metadata 135 * allocations as well. 136 */ 137 int zio_exclude_metadata = 0; 138 static int zio_requeue_io_start_cut_in_line = 1; 139 140 #ifdef ZFS_DEBUG 141 static const int zio_buf_debug_limit = 16384; 142 #else 143 static const int zio_buf_debug_limit = 0; 144 #endif 145 146 static inline void __zio_execute(zio_t *zio); 147 148 static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t); 149 150 void 151 zio_init(void) 152 { 153 size_t c; 154 155 zio_cache = kmem_cache_create("zio_cache", 156 sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 157 zio_link_cache = kmem_cache_create("zio_link_cache", 158 sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 159 160 /* 161 * For small buffers, we want a cache for each multiple of 162 * SPA_MINBLOCKSIZE. For larger buffers, we want a cache 163 * for each quarter-power of 2. 164 */ 165 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) { 166 size_t size = (c + 1) << SPA_MINBLOCKSHIFT; 167 size_t p2 = size; 168 size_t align = 0; 169 size_t data_cflags, cflags; 170 171 data_cflags = KMC_NODEBUG; 172 cflags = (zio_exclude_metadata || size > zio_buf_debug_limit) ? 173 KMC_NODEBUG : 0; 174 175 while (!ISP2(p2)) 176 p2 &= p2 - 1; 177 178 #ifndef _KERNEL 179 /* 180 * If we are using watchpoints, put each buffer on its own page, 181 * to eliminate the performance overhead of trapping to the 182 * kernel when modifying a non-watched buffer that shares the 183 * page with a watched buffer. 184 */ 185 if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE)) 186 continue; 187 /* 188 * Here's the problem - on 4K native devices in userland on 189 * Linux using O_DIRECT, buffers must be 4K aligned or I/O 190 * will fail with EINVAL, causing zdb (and others) to coredump. 191 * Since userland probably doesn't need optimized buffer caches, 192 * we just force 4K alignment on everything. 193 */ 194 align = 8 * SPA_MINBLOCKSIZE; 195 #else 196 if (size < PAGESIZE) { 197 align = SPA_MINBLOCKSIZE; 198 } else if (IS_P2ALIGNED(size, p2 >> 2)) { 199 align = PAGESIZE; 200 } 201 #endif 202 203 if (align != 0) { 204 char name[36]; 205 if (cflags == data_cflags) { 206 /* 207 * Resulting kmem caches would be identical. 208 * Save memory by creating only one. 209 */ 210 (void) snprintf(name, sizeof (name), 211 "zio_buf_comb_%lu", (ulong_t)size); 212 zio_buf_cache[c] = kmem_cache_create(name, 213 size, align, NULL, NULL, NULL, NULL, NULL, 214 cflags); 215 zio_data_buf_cache[c] = zio_buf_cache[c]; 216 continue; 217 } 218 (void) snprintf(name, sizeof (name), "zio_buf_%lu", 219 (ulong_t)size); 220 zio_buf_cache[c] = kmem_cache_create(name, size, 221 align, NULL, NULL, NULL, NULL, NULL, cflags); 222 223 (void) snprintf(name, sizeof (name), "zio_data_buf_%lu", 224 (ulong_t)size); 225 zio_data_buf_cache[c] = kmem_cache_create(name, size, 226 align, NULL, NULL, NULL, NULL, NULL, data_cflags); 227 } 228 } 229 230 while (--c != 0) { 231 ASSERT(zio_buf_cache[c] != NULL); 232 if (zio_buf_cache[c - 1] == NULL) 233 zio_buf_cache[c - 1] = zio_buf_cache[c]; 234 235 ASSERT(zio_data_buf_cache[c] != NULL); 236 if (zio_data_buf_cache[c - 1] == NULL) 237 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c]; 238 } 239 240 zio_inject_init(); 241 242 lz4_init(); 243 } 244 245 void 246 zio_fini(void) 247 { 248 size_t n = SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; 249 250 #if defined(ZFS_DEBUG) && !defined(_KERNEL) 251 for (size_t i = 0; i < n; i++) { 252 if (zio_buf_cache_allocs[i] != zio_buf_cache_frees[i]) 253 (void) printf("zio_fini: [%d] %llu != %llu\n", 254 (int)((i + 1) << SPA_MINBLOCKSHIFT), 255 (long long unsigned)zio_buf_cache_allocs[i], 256 (long long unsigned)zio_buf_cache_frees[i]); 257 } 258 #endif 259 260 /* 261 * The same kmem cache can show up multiple times in both zio_buf_cache 262 * and zio_data_buf_cache. Do a wasteful but trivially correct scan to 263 * sort it out. 264 */ 265 for (size_t i = 0; i < n; i++) { 266 kmem_cache_t *cache = zio_buf_cache[i]; 267 if (cache == NULL) 268 continue; 269 for (size_t j = i; j < n; j++) { 270 if (cache == zio_buf_cache[j]) 271 zio_buf_cache[j] = NULL; 272 if (cache == zio_data_buf_cache[j]) 273 zio_data_buf_cache[j] = NULL; 274 } 275 kmem_cache_destroy(cache); 276 } 277 278 for (size_t i = 0; i < n; i++) { 279 kmem_cache_t *cache = zio_data_buf_cache[i]; 280 if (cache == NULL) 281 continue; 282 for (size_t j = i; j < n; j++) { 283 if (cache == zio_data_buf_cache[j]) 284 zio_data_buf_cache[j] = NULL; 285 } 286 kmem_cache_destroy(cache); 287 } 288 289 for (size_t i = 0; i < n; i++) { 290 VERIFY3P(zio_buf_cache[i], ==, NULL); 291 VERIFY3P(zio_data_buf_cache[i], ==, NULL); 292 } 293 294 kmem_cache_destroy(zio_link_cache); 295 kmem_cache_destroy(zio_cache); 296 297 zio_inject_fini(); 298 299 lz4_fini(); 300 } 301 302 /* 303 * ========================================================================== 304 * Allocate and free I/O buffers 305 * ========================================================================== 306 */ 307 308 /* 309 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a 310 * crashdump if the kernel panics, so use it judiciously. Obviously, it's 311 * useful to inspect ZFS metadata, but if possible, we should avoid keeping 312 * excess / transient data in-core during a crashdump. 313 */ 314 void * 315 zio_buf_alloc(size_t size) 316 { 317 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 318 319 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 320 #if defined(ZFS_DEBUG) && !defined(_KERNEL) 321 atomic_add_64(&zio_buf_cache_allocs[c], 1); 322 #endif 323 324 return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE)); 325 } 326 327 /* 328 * Use zio_data_buf_alloc to allocate data. The data will not appear in a 329 * crashdump if the kernel panics. This exists so that we will limit the amount 330 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount 331 * of kernel heap dumped to disk when the kernel panics) 332 */ 333 void * 334 zio_data_buf_alloc(size_t size) 335 { 336 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 337 338 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 339 340 return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE)); 341 } 342 343 void 344 zio_buf_free(void *buf, size_t size) 345 { 346 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 347 348 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 349 #if defined(ZFS_DEBUG) && !defined(_KERNEL) 350 atomic_add_64(&zio_buf_cache_frees[c], 1); 351 #endif 352 353 kmem_cache_free(zio_buf_cache[c], buf); 354 } 355 356 void 357 zio_data_buf_free(void *buf, size_t size) 358 { 359 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 360 361 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 362 363 kmem_cache_free(zio_data_buf_cache[c], buf); 364 } 365 366 static void 367 zio_abd_free(void *abd, size_t size) 368 { 369 (void) size; 370 abd_free((abd_t *)abd); 371 } 372 373 /* 374 * ========================================================================== 375 * Push and pop I/O transform buffers 376 * ========================================================================== 377 */ 378 void 379 zio_push_transform(zio_t *zio, abd_t *data, uint64_t size, uint64_t bufsize, 380 zio_transform_func_t *transform) 381 { 382 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP); 383 384 zt->zt_orig_abd = zio->io_abd; 385 zt->zt_orig_size = zio->io_size; 386 zt->zt_bufsize = bufsize; 387 zt->zt_transform = transform; 388 389 zt->zt_next = zio->io_transform_stack; 390 zio->io_transform_stack = zt; 391 392 zio->io_abd = data; 393 zio->io_size = size; 394 } 395 396 void 397 zio_pop_transforms(zio_t *zio) 398 { 399 zio_transform_t *zt; 400 401 while ((zt = zio->io_transform_stack) != NULL) { 402 if (zt->zt_transform != NULL) 403 zt->zt_transform(zio, 404 zt->zt_orig_abd, zt->zt_orig_size); 405 406 if (zt->zt_bufsize != 0) 407 abd_free(zio->io_abd); 408 409 zio->io_abd = zt->zt_orig_abd; 410 zio->io_size = zt->zt_orig_size; 411 zio->io_transform_stack = zt->zt_next; 412 413 kmem_free(zt, sizeof (zio_transform_t)); 414 } 415 } 416 417 /* 418 * ========================================================================== 419 * I/O transform callbacks for subblocks, decompression, and decryption 420 * ========================================================================== 421 */ 422 static void 423 zio_subblock(zio_t *zio, abd_t *data, uint64_t size) 424 { 425 ASSERT(zio->io_size > size); 426 427 if (zio->io_type == ZIO_TYPE_READ) 428 abd_copy(data, zio->io_abd, size); 429 } 430 431 static void 432 zio_decompress(zio_t *zio, abd_t *data, uint64_t size) 433 { 434 if (zio->io_error == 0) { 435 void *tmp = abd_borrow_buf(data, size); 436 int ret = zio_decompress_data(BP_GET_COMPRESS(zio->io_bp), 437 zio->io_abd, tmp, zio->io_size, size, 438 &zio->io_prop.zp_complevel); 439 abd_return_buf_copy(data, tmp, size); 440 441 if (zio_injection_enabled && ret == 0) 442 ret = zio_handle_fault_injection(zio, EINVAL); 443 444 if (ret != 0) 445 zio->io_error = SET_ERROR(EIO); 446 } 447 } 448 449 static void 450 zio_decrypt(zio_t *zio, abd_t *data, uint64_t size) 451 { 452 int ret; 453 void *tmp; 454 blkptr_t *bp = zio->io_bp; 455 spa_t *spa = zio->io_spa; 456 uint64_t dsobj = zio->io_bookmark.zb_objset; 457 uint64_t lsize = BP_GET_LSIZE(bp); 458 dmu_object_type_t ot = BP_GET_TYPE(bp); 459 uint8_t salt[ZIO_DATA_SALT_LEN]; 460 uint8_t iv[ZIO_DATA_IV_LEN]; 461 uint8_t mac[ZIO_DATA_MAC_LEN]; 462 boolean_t no_crypt = B_FALSE; 463 464 ASSERT(BP_USES_CRYPT(bp)); 465 ASSERT3U(size, !=, 0); 466 467 if (zio->io_error != 0) 468 return; 469 470 /* 471 * Verify the cksum of MACs stored in an indirect bp. It will always 472 * be possible to verify this since it does not require an encryption 473 * key. 474 */ 475 if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) { 476 zio_crypt_decode_mac_bp(bp, mac); 477 478 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) { 479 /* 480 * We haven't decompressed the data yet, but 481 * zio_crypt_do_indirect_mac_checksum() requires 482 * decompressed data to be able to parse out the MACs 483 * from the indirect block. We decompress it now and 484 * throw away the result after we are finished. 485 */ 486 tmp = zio_buf_alloc(lsize); 487 ret = zio_decompress_data(BP_GET_COMPRESS(bp), 488 zio->io_abd, tmp, zio->io_size, lsize, 489 &zio->io_prop.zp_complevel); 490 if (ret != 0) { 491 ret = SET_ERROR(EIO); 492 goto error; 493 } 494 ret = zio_crypt_do_indirect_mac_checksum(B_FALSE, 495 tmp, lsize, BP_SHOULD_BYTESWAP(bp), mac); 496 zio_buf_free(tmp, lsize); 497 } else { 498 ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE, 499 zio->io_abd, size, BP_SHOULD_BYTESWAP(bp), mac); 500 } 501 abd_copy(data, zio->io_abd, size); 502 503 if (zio_injection_enabled && ot != DMU_OT_DNODE && ret == 0) { 504 ret = zio_handle_decrypt_injection(spa, 505 &zio->io_bookmark, ot, ECKSUM); 506 } 507 if (ret != 0) 508 goto error; 509 510 return; 511 } 512 513 /* 514 * If this is an authenticated block, just check the MAC. It would be 515 * nice to separate this out into its own flag, but for the moment 516 * enum zio_flag is out of bits. 517 */ 518 if (BP_IS_AUTHENTICATED(bp)) { 519 if (ot == DMU_OT_OBJSET) { 520 ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa, 521 dsobj, zio->io_abd, size, BP_SHOULD_BYTESWAP(bp)); 522 } else { 523 zio_crypt_decode_mac_bp(bp, mac); 524 ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj, 525 zio->io_abd, size, mac); 526 if (zio_injection_enabled && ret == 0) { 527 ret = zio_handle_decrypt_injection(spa, 528 &zio->io_bookmark, ot, ECKSUM); 529 } 530 } 531 abd_copy(data, zio->io_abd, size); 532 533 if (ret != 0) 534 goto error; 535 536 return; 537 } 538 539 zio_crypt_decode_params_bp(bp, salt, iv); 540 541 if (ot == DMU_OT_INTENT_LOG) { 542 tmp = abd_borrow_buf_copy(zio->io_abd, sizeof (zil_chain_t)); 543 zio_crypt_decode_mac_zil(tmp, mac); 544 abd_return_buf(zio->io_abd, tmp, sizeof (zil_chain_t)); 545 } else { 546 zio_crypt_decode_mac_bp(bp, mac); 547 } 548 549 ret = spa_do_crypt_abd(B_FALSE, spa, &zio->io_bookmark, BP_GET_TYPE(bp), 550 BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), salt, iv, mac, size, data, 551 zio->io_abd, &no_crypt); 552 if (no_crypt) 553 abd_copy(data, zio->io_abd, size); 554 555 if (ret != 0) 556 goto error; 557 558 return; 559 560 error: 561 /* assert that the key was found unless this was speculative */ 562 ASSERT(ret != EACCES || (zio->io_flags & ZIO_FLAG_SPECULATIVE)); 563 564 /* 565 * If there was a decryption / authentication error return EIO as 566 * the io_error. If this was not a speculative zio, create an ereport. 567 */ 568 if (ret == ECKSUM) { 569 zio->io_error = SET_ERROR(EIO); 570 if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) { 571 spa_log_error(spa, &zio->io_bookmark); 572 (void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION, 573 spa, NULL, &zio->io_bookmark, zio, 0); 574 } 575 } else { 576 zio->io_error = ret; 577 } 578 } 579 580 /* 581 * ========================================================================== 582 * I/O parent/child relationships and pipeline interlocks 583 * ========================================================================== 584 */ 585 zio_t * 586 zio_walk_parents(zio_t *cio, zio_link_t **zl) 587 { 588 list_t *pl = &cio->io_parent_list; 589 590 *zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl); 591 if (*zl == NULL) 592 return (NULL); 593 594 ASSERT((*zl)->zl_child == cio); 595 return ((*zl)->zl_parent); 596 } 597 598 zio_t * 599 zio_walk_children(zio_t *pio, zio_link_t **zl) 600 { 601 list_t *cl = &pio->io_child_list; 602 603 ASSERT(MUTEX_HELD(&pio->io_lock)); 604 605 *zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl); 606 if (*zl == NULL) 607 return (NULL); 608 609 ASSERT((*zl)->zl_parent == pio); 610 return ((*zl)->zl_child); 611 } 612 613 zio_t * 614 zio_unique_parent(zio_t *cio) 615 { 616 zio_link_t *zl = NULL; 617 zio_t *pio = zio_walk_parents(cio, &zl); 618 619 VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL); 620 return (pio); 621 } 622 623 void 624 zio_add_child(zio_t *pio, zio_t *cio) 625 { 626 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP); 627 628 /* 629 * Logical I/Os can have logical, gang, or vdev children. 630 * Gang I/Os can have gang or vdev children. 631 * Vdev I/Os can only have vdev children. 632 * The following ASSERT captures all of these constraints. 633 */ 634 ASSERT3S(cio->io_child_type, <=, pio->io_child_type); 635 636 zl->zl_parent = pio; 637 zl->zl_child = cio; 638 639 mutex_enter(&pio->io_lock); 640 mutex_enter(&cio->io_lock); 641 642 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0); 643 644 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 645 pio->io_children[cio->io_child_type][w] += !cio->io_state[w]; 646 647 list_insert_head(&pio->io_child_list, zl); 648 list_insert_head(&cio->io_parent_list, zl); 649 650 pio->io_child_count++; 651 cio->io_parent_count++; 652 653 mutex_exit(&cio->io_lock); 654 mutex_exit(&pio->io_lock); 655 } 656 657 static void 658 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl) 659 { 660 ASSERT(zl->zl_parent == pio); 661 ASSERT(zl->zl_child == cio); 662 663 mutex_enter(&pio->io_lock); 664 mutex_enter(&cio->io_lock); 665 666 list_remove(&pio->io_child_list, zl); 667 list_remove(&cio->io_parent_list, zl); 668 669 pio->io_child_count--; 670 cio->io_parent_count--; 671 672 mutex_exit(&cio->io_lock); 673 mutex_exit(&pio->io_lock); 674 kmem_cache_free(zio_link_cache, zl); 675 } 676 677 static boolean_t 678 zio_wait_for_children(zio_t *zio, uint8_t childbits, enum zio_wait_type wait) 679 { 680 boolean_t waiting = B_FALSE; 681 682 mutex_enter(&zio->io_lock); 683 ASSERT(zio->io_stall == NULL); 684 for (int c = 0; c < ZIO_CHILD_TYPES; c++) { 685 if (!(ZIO_CHILD_BIT_IS_SET(childbits, c))) 686 continue; 687 688 uint64_t *countp = &zio->io_children[c][wait]; 689 if (*countp != 0) { 690 zio->io_stage >>= 1; 691 ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN); 692 zio->io_stall = countp; 693 waiting = B_TRUE; 694 break; 695 } 696 } 697 mutex_exit(&zio->io_lock); 698 return (waiting); 699 } 700 701 __attribute__((always_inline)) 702 static inline void 703 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait, 704 zio_t **next_to_executep) 705 { 706 uint64_t *countp = &pio->io_children[zio->io_child_type][wait]; 707 int *errorp = &pio->io_child_error[zio->io_child_type]; 708 709 mutex_enter(&pio->io_lock); 710 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 711 *errorp = zio_worst_error(*errorp, zio->io_error); 712 pio->io_reexecute |= zio->io_reexecute; 713 ASSERT3U(*countp, >, 0); 714 715 (*countp)--; 716 717 if (*countp == 0 && pio->io_stall == countp) { 718 zio_taskq_type_t type = 719 pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE : 720 ZIO_TASKQ_INTERRUPT; 721 pio->io_stall = NULL; 722 mutex_exit(&pio->io_lock); 723 724 /* 725 * If we can tell the caller to execute this parent next, do 726 * so. Otherwise dispatch the parent zio as its own task. 727 * 728 * Having the caller execute the parent when possible reduces 729 * locking on the zio taskq's, reduces context switch 730 * overhead, and has no recursion penalty. Note that one 731 * read from disk typically causes at least 3 zio's: a 732 * zio_null(), the logical zio_read(), and then a physical 733 * zio. When the physical ZIO completes, we are able to call 734 * zio_done() on all 3 of these zio's from one invocation of 735 * zio_execute() by returning the parent back to 736 * zio_execute(). Since the parent isn't executed until this 737 * thread returns back to zio_execute(), the caller should do 738 * so promptly. 739 * 740 * In other cases, dispatching the parent prevents 741 * overflowing the stack when we have deeply nested 742 * parent-child relationships, as we do with the "mega zio" 743 * of writes for spa_sync(), and the chain of ZIL blocks. 744 */ 745 if (next_to_executep != NULL && *next_to_executep == NULL) { 746 *next_to_executep = pio; 747 } else { 748 zio_taskq_dispatch(pio, type, B_FALSE); 749 } 750 } else { 751 mutex_exit(&pio->io_lock); 752 } 753 } 754 755 static void 756 zio_inherit_child_errors(zio_t *zio, enum zio_child c) 757 { 758 if (zio->io_child_error[c] != 0 && zio->io_error == 0) 759 zio->io_error = zio->io_child_error[c]; 760 } 761 762 int 763 zio_bookmark_compare(const void *x1, const void *x2) 764 { 765 const zio_t *z1 = x1; 766 const zio_t *z2 = x2; 767 768 if (z1->io_bookmark.zb_objset < z2->io_bookmark.zb_objset) 769 return (-1); 770 if (z1->io_bookmark.zb_objset > z2->io_bookmark.zb_objset) 771 return (1); 772 773 if (z1->io_bookmark.zb_object < z2->io_bookmark.zb_object) 774 return (-1); 775 if (z1->io_bookmark.zb_object > z2->io_bookmark.zb_object) 776 return (1); 777 778 if (z1->io_bookmark.zb_level < z2->io_bookmark.zb_level) 779 return (-1); 780 if (z1->io_bookmark.zb_level > z2->io_bookmark.zb_level) 781 return (1); 782 783 if (z1->io_bookmark.zb_blkid < z2->io_bookmark.zb_blkid) 784 return (-1); 785 if (z1->io_bookmark.zb_blkid > z2->io_bookmark.zb_blkid) 786 return (1); 787 788 if (z1 < z2) 789 return (-1); 790 if (z1 > z2) 791 return (1); 792 793 return (0); 794 } 795 796 /* 797 * ========================================================================== 798 * Create the various types of I/O (read, write, free, etc) 799 * ========================================================================== 800 */ 801 static zio_t * 802 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 803 abd_t *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done, 804 void *private, zio_type_t type, zio_priority_t priority, 805 enum zio_flag flags, vdev_t *vd, uint64_t offset, 806 const zbookmark_phys_t *zb, enum zio_stage stage, 807 enum zio_stage pipeline) 808 { 809 zio_t *zio; 810 811 IMPLY(type != ZIO_TYPE_TRIM, psize <= SPA_MAXBLOCKSIZE); 812 ASSERT(P2PHASE(psize, SPA_MINBLOCKSIZE) == 0); 813 ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0); 814 815 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER)); 816 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER)); 817 ASSERT(vd || stage == ZIO_STAGE_OPEN); 818 819 IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW_COMPRESS) != 0); 820 821 zio = kmem_cache_alloc(zio_cache, KM_SLEEP); 822 memset(zio, 0, sizeof (zio_t)); 823 824 mutex_init(&zio->io_lock, NULL, MUTEX_NOLOCKDEP, NULL); 825 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL); 826 827 list_create(&zio->io_parent_list, sizeof (zio_link_t), 828 offsetof(zio_link_t, zl_parent_node)); 829 list_create(&zio->io_child_list, sizeof (zio_link_t), 830 offsetof(zio_link_t, zl_child_node)); 831 metaslab_trace_init(&zio->io_alloc_list); 832 833 if (vd != NULL) 834 zio->io_child_type = ZIO_CHILD_VDEV; 835 else if (flags & ZIO_FLAG_GANG_CHILD) 836 zio->io_child_type = ZIO_CHILD_GANG; 837 else if (flags & ZIO_FLAG_DDT_CHILD) 838 zio->io_child_type = ZIO_CHILD_DDT; 839 else 840 zio->io_child_type = ZIO_CHILD_LOGICAL; 841 842 if (bp != NULL) { 843 zio->io_bp = (blkptr_t *)bp; 844 zio->io_bp_copy = *bp; 845 zio->io_bp_orig = *bp; 846 if (type != ZIO_TYPE_WRITE || 847 zio->io_child_type == ZIO_CHILD_DDT) 848 zio->io_bp = &zio->io_bp_copy; /* so caller can free */ 849 if (zio->io_child_type == ZIO_CHILD_LOGICAL) 850 zio->io_logical = zio; 851 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp)) 852 pipeline |= ZIO_GANG_STAGES; 853 } 854 855 zio->io_spa = spa; 856 zio->io_txg = txg; 857 zio->io_done = done; 858 zio->io_private = private; 859 zio->io_type = type; 860 zio->io_priority = priority; 861 zio->io_vd = vd; 862 zio->io_offset = offset; 863 zio->io_orig_abd = zio->io_abd = data; 864 zio->io_orig_size = zio->io_size = psize; 865 zio->io_lsize = lsize; 866 zio->io_orig_flags = zio->io_flags = flags; 867 zio->io_orig_stage = zio->io_stage = stage; 868 zio->io_orig_pipeline = zio->io_pipeline = pipeline; 869 zio->io_pipeline_trace = ZIO_STAGE_OPEN; 870 871 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY); 872 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE); 873 874 if (zb != NULL) 875 zio->io_bookmark = *zb; 876 877 if (pio != NULL) { 878 zio->io_metaslab_class = pio->io_metaslab_class; 879 if (zio->io_logical == NULL) 880 zio->io_logical = pio->io_logical; 881 if (zio->io_child_type == ZIO_CHILD_GANG) 882 zio->io_gang_leader = pio->io_gang_leader; 883 zio_add_child(pio, zio); 884 } 885 886 taskq_init_ent(&zio->io_tqent); 887 888 return (zio); 889 } 890 891 void 892 zio_destroy(zio_t *zio) 893 { 894 metaslab_trace_fini(&zio->io_alloc_list); 895 list_destroy(&zio->io_parent_list); 896 list_destroy(&zio->io_child_list); 897 mutex_destroy(&zio->io_lock); 898 cv_destroy(&zio->io_cv); 899 kmem_cache_free(zio_cache, zio); 900 } 901 902 zio_t * 903 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done, 904 void *private, enum zio_flag flags) 905 { 906 zio_t *zio; 907 908 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private, 909 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL, 910 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE); 911 912 return (zio); 913 } 914 915 zio_t * 916 zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags) 917 { 918 return (zio_null(NULL, spa, NULL, done, private, flags)); 919 } 920 921 static int 922 zfs_blkptr_verify_log(spa_t *spa, const blkptr_t *bp, 923 enum blk_verify_flag blk_verify, const char *fmt, ...) 924 { 925 va_list adx; 926 char buf[256]; 927 928 va_start(adx, fmt); 929 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 930 va_end(adx); 931 932 switch (blk_verify) { 933 case BLK_VERIFY_HALT: 934 dprintf_bp(bp, "blkptr at %p dprintf_bp():", bp); 935 zfs_panic_recover("%s: %s", spa_name(spa), buf); 936 break; 937 case BLK_VERIFY_LOG: 938 zfs_dbgmsg("%s: %s", spa_name(spa), buf); 939 break; 940 case BLK_VERIFY_ONLY: 941 break; 942 } 943 944 return (1); 945 } 946 947 /* 948 * Verify the block pointer fields contain reasonable values. This means 949 * it only contains known object types, checksum/compression identifiers, 950 * block sizes within the maximum allowed limits, valid DVAs, etc. 951 * 952 * If everything checks out B_TRUE is returned. The zfs_blkptr_verify 953 * argument controls the behavior when an invalid field is detected. 954 * 955 * Modes for zfs_blkptr_verify: 956 * 1) BLK_VERIFY_ONLY (evaluate the block) 957 * 2) BLK_VERIFY_LOG (evaluate the block and log problems) 958 * 3) BLK_VERIFY_HALT (call zfs_panic_recover on error) 959 */ 960 boolean_t 961 zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp, boolean_t config_held, 962 enum blk_verify_flag blk_verify) 963 { 964 int errors = 0; 965 966 if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) { 967 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 968 "blkptr at %p has invalid TYPE %llu", 969 bp, (longlong_t)BP_GET_TYPE(bp)); 970 } 971 if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS) { 972 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 973 "blkptr at %p has invalid CHECKSUM %llu", 974 bp, (longlong_t)BP_GET_CHECKSUM(bp)); 975 } 976 if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS) { 977 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 978 "blkptr at %p has invalid COMPRESS %llu", 979 bp, (longlong_t)BP_GET_COMPRESS(bp)); 980 } 981 if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) { 982 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 983 "blkptr at %p has invalid LSIZE %llu", 984 bp, (longlong_t)BP_GET_LSIZE(bp)); 985 } 986 if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) { 987 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 988 "blkptr at %p has invalid PSIZE %llu", 989 bp, (longlong_t)BP_GET_PSIZE(bp)); 990 } 991 992 if (BP_IS_EMBEDDED(bp)) { 993 if (BPE_GET_ETYPE(bp) >= NUM_BP_EMBEDDED_TYPES) { 994 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 995 "blkptr at %p has invalid ETYPE %llu", 996 bp, (longlong_t)BPE_GET_ETYPE(bp)); 997 } 998 } 999 1000 /* 1001 * Do not verify individual DVAs if the config is not trusted. This 1002 * will be done once the zio is executed in vdev_mirror_map_alloc. 1003 */ 1004 if (!spa->spa_trust_config) 1005 return (errors == 0); 1006 1007 if (!config_held) 1008 spa_config_enter(spa, SCL_VDEV, bp, RW_READER); 1009 else 1010 ASSERT(spa_config_held(spa, SCL_VDEV, RW_WRITER)); 1011 /* 1012 * Pool-specific checks. 1013 * 1014 * Note: it would be nice to verify that the blk_birth and 1015 * BP_PHYSICAL_BIRTH() are not too large. However, spa_freeze() 1016 * allows the birth time of log blocks (and dmu_sync()-ed blocks 1017 * that are in the log) to be arbitrarily large. 1018 */ 1019 for (int i = 0; i < BP_GET_NDVAS(bp); i++) { 1020 const dva_t *dva = &bp->blk_dva[i]; 1021 uint64_t vdevid = DVA_GET_VDEV(dva); 1022 1023 if (vdevid >= spa->spa_root_vdev->vdev_children) { 1024 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1025 "blkptr at %p DVA %u has invalid VDEV %llu", 1026 bp, i, (longlong_t)vdevid); 1027 continue; 1028 } 1029 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid]; 1030 if (vd == NULL) { 1031 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1032 "blkptr at %p DVA %u has invalid VDEV %llu", 1033 bp, i, (longlong_t)vdevid); 1034 continue; 1035 } 1036 if (vd->vdev_ops == &vdev_hole_ops) { 1037 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1038 "blkptr at %p DVA %u has hole VDEV %llu", 1039 bp, i, (longlong_t)vdevid); 1040 continue; 1041 } 1042 if (vd->vdev_ops == &vdev_missing_ops) { 1043 /* 1044 * "missing" vdevs are valid during import, but we 1045 * don't have their detailed info (e.g. asize), so 1046 * we can't perform any more checks on them. 1047 */ 1048 continue; 1049 } 1050 uint64_t offset = DVA_GET_OFFSET(dva); 1051 uint64_t asize = DVA_GET_ASIZE(dva); 1052 if (DVA_GET_GANG(dva)) 1053 asize = vdev_gang_header_asize(vd); 1054 if (offset + asize > vd->vdev_asize) { 1055 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1056 "blkptr at %p DVA %u has invalid OFFSET %llu", 1057 bp, i, (longlong_t)offset); 1058 } 1059 } 1060 if (errors > 0) 1061 dprintf_bp(bp, "blkptr at %p dprintf_bp():", bp); 1062 if (!config_held) 1063 spa_config_exit(spa, SCL_VDEV, bp); 1064 1065 return (errors == 0); 1066 } 1067 1068 boolean_t 1069 zfs_dva_valid(spa_t *spa, const dva_t *dva, const blkptr_t *bp) 1070 { 1071 (void) bp; 1072 uint64_t vdevid = DVA_GET_VDEV(dva); 1073 1074 if (vdevid >= spa->spa_root_vdev->vdev_children) 1075 return (B_FALSE); 1076 1077 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid]; 1078 if (vd == NULL) 1079 return (B_FALSE); 1080 1081 if (vd->vdev_ops == &vdev_hole_ops) 1082 return (B_FALSE); 1083 1084 if (vd->vdev_ops == &vdev_missing_ops) { 1085 return (B_FALSE); 1086 } 1087 1088 uint64_t offset = DVA_GET_OFFSET(dva); 1089 uint64_t asize = DVA_GET_ASIZE(dva); 1090 1091 if (DVA_GET_GANG(dva)) 1092 asize = vdev_gang_header_asize(vd); 1093 if (offset + asize > vd->vdev_asize) 1094 return (B_FALSE); 1095 1096 return (B_TRUE); 1097 } 1098 1099 zio_t * 1100 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, 1101 abd_t *data, uint64_t size, zio_done_func_t *done, void *private, 1102 zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb) 1103 { 1104 zio_t *zio; 1105 1106 zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp, 1107 data, size, size, done, private, 1108 ZIO_TYPE_READ, priority, flags, NULL, 0, zb, 1109 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ? 1110 ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE); 1111 1112 return (zio); 1113 } 1114 1115 zio_t * 1116 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 1117 abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp, 1118 zio_done_func_t *ready, zio_done_func_t *children_ready, 1119 zio_done_func_t *physdone, zio_done_func_t *done, 1120 void *private, zio_priority_t priority, enum zio_flag flags, 1121 const zbookmark_phys_t *zb) 1122 { 1123 zio_t *zio; 1124 1125 ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF && 1126 zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS && 1127 zp->zp_compress >= ZIO_COMPRESS_OFF && 1128 zp->zp_compress < ZIO_COMPRESS_FUNCTIONS && 1129 DMU_OT_IS_VALID(zp->zp_type) && 1130 zp->zp_level < 32 && 1131 zp->zp_copies > 0 && 1132 zp->zp_copies <= spa_max_replication(spa)); 1133 1134 zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private, 1135 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb, 1136 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ? 1137 ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE); 1138 1139 zio->io_ready = ready; 1140 zio->io_children_ready = children_ready; 1141 zio->io_physdone = physdone; 1142 zio->io_prop = *zp; 1143 1144 /* 1145 * Data can be NULL if we are going to call zio_write_override() to 1146 * provide the already-allocated BP. But we may need the data to 1147 * verify a dedup hit (if requested). In this case, don't try to 1148 * dedup (just take the already-allocated BP verbatim). Encrypted 1149 * dedup blocks need data as well so we also disable dedup in this 1150 * case. 1151 */ 1152 if (data == NULL && 1153 (zio->io_prop.zp_dedup_verify || zio->io_prop.zp_encrypt)) { 1154 zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE; 1155 } 1156 1157 return (zio); 1158 } 1159 1160 zio_t * 1161 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data, 1162 uint64_t size, zio_done_func_t *done, void *private, 1163 zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb) 1164 { 1165 zio_t *zio; 1166 1167 zio = zio_create(pio, spa, txg, bp, data, size, size, done, private, 1168 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb, 1169 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE); 1170 1171 return (zio); 1172 } 1173 1174 void 1175 zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite) 1176 { 1177 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 1178 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1179 ASSERT(zio->io_stage == ZIO_STAGE_OPEN); 1180 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa)); 1181 1182 /* 1183 * We must reset the io_prop to match the values that existed 1184 * when the bp was first written by dmu_sync() keeping in mind 1185 * that nopwrite and dedup are mutually exclusive. 1186 */ 1187 zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup; 1188 zio->io_prop.zp_nopwrite = nopwrite; 1189 zio->io_prop.zp_copies = copies; 1190 zio->io_bp_override = bp; 1191 } 1192 1193 void 1194 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp) 1195 { 1196 1197 (void) zfs_blkptr_verify(spa, bp, B_FALSE, BLK_VERIFY_HALT); 1198 1199 /* 1200 * The check for EMBEDDED is a performance optimization. We 1201 * process the free here (by ignoring it) rather than 1202 * putting it on the list and then processing it in zio_free_sync(). 1203 */ 1204 if (BP_IS_EMBEDDED(bp)) 1205 return; 1206 metaslab_check_free(spa, bp); 1207 1208 /* 1209 * Frees that are for the currently-syncing txg, are not going to be 1210 * deferred, and which will not need to do a read (i.e. not GANG or 1211 * DEDUP), can be processed immediately. Otherwise, put them on the 1212 * in-memory list for later processing. 1213 * 1214 * Note that we only defer frees after zfs_sync_pass_deferred_free 1215 * when the log space map feature is disabled. [see relevant comment 1216 * in spa_sync_iterate_to_convergence()] 1217 */ 1218 if (BP_IS_GANG(bp) || 1219 BP_GET_DEDUP(bp) || 1220 txg != spa->spa_syncing_txg || 1221 (spa_sync_pass(spa) >= zfs_sync_pass_deferred_free && 1222 !spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))) { 1223 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp); 1224 } else { 1225 VERIFY3P(zio_free_sync(NULL, spa, txg, bp, 0), ==, NULL); 1226 } 1227 } 1228 1229 /* 1230 * To improve performance, this function may return NULL if we were able 1231 * to do the free immediately. This avoids the cost of creating a zio 1232 * (and linking it to the parent, etc). 1233 */ 1234 zio_t * 1235 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 1236 enum zio_flag flags) 1237 { 1238 ASSERT(!BP_IS_HOLE(bp)); 1239 ASSERT(spa_syncing_txg(spa) == txg); 1240 1241 if (BP_IS_EMBEDDED(bp)) 1242 return (NULL); 1243 1244 metaslab_check_free(spa, bp); 1245 arc_freed(spa, bp); 1246 dsl_scan_freed(spa, bp); 1247 1248 if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp)) { 1249 /* 1250 * GANG and DEDUP blocks can induce a read (for the gang block 1251 * header, or the DDT), so issue them asynchronously so that 1252 * this thread is not tied up. 1253 */ 1254 enum zio_stage stage = 1255 ZIO_FREE_PIPELINE | ZIO_STAGE_ISSUE_ASYNC; 1256 1257 return (zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp), 1258 BP_GET_PSIZE(bp), NULL, NULL, 1259 ZIO_TYPE_FREE, ZIO_PRIORITY_NOW, 1260 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage)); 1261 } else { 1262 metaslab_free(spa, bp, txg, B_FALSE); 1263 return (NULL); 1264 } 1265 } 1266 1267 zio_t * 1268 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 1269 zio_done_func_t *done, void *private, enum zio_flag flags) 1270 { 1271 zio_t *zio; 1272 1273 (void) zfs_blkptr_verify(spa, bp, flags & ZIO_FLAG_CONFIG_WRITER, 1274 BLK_VERIFY_HALT); 1275 1276 if (BP_IS_EMBEDDED(bp)) 1277 return (zio_null(pio, spa, NULL, NULL, NULL, 0)); 1278 1279 /* 1280 * A claim is an allocation of a specific block. Claims are needed 1281 * to support immediate writes in the intent log. The issue is that 1282 * immediate writes contain committed data, but in a txg that was 1283 * *not* committed. Upon opening the pool after an unclean shutdown, 1284 * the intent log claims all blocks that contain immediate write data 1285 * so that the SPA knows they're in use. 1286 * 1287 * All claims *must* be resolved in the first txg -- before the SPA 1288 * starts allocating blocks -- so that nothing is allocated twice. 1289 * If txg == 0 we just verify that the block is claimable. 1290 */ 1291 ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, 1292 spa_min_claim_txg(spa)); 1293 ASSERT(txg == spa_min_claim_txg(spa) || txg == 0); 1294 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(8) */ 1295 1296 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp), 1297 BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, 1298 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE); 1299 ASSERT0(zio->io_queued_timestamp); 1300 1301 return (zio); 1302 } 1303 1304 zio_t * 1305 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd, 1306 zio_done_func_t *done, void *private, enum zio_flag flags) 1307 { 1308 zio_t *zio; 1309 int c; 1310 1311 if (vd->vdev_children == 0) { 1312 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private, 1313 ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL, 1314 ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE); 1315 1316 zio->io_cmd = cmd; 1317 } else { 1318 zio = zio_null(pio, spa, NULL, NULL, NULL, flags); 1319 1320 for (c = 0; c < vd->vdev_children; c++) 1321 zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd, 1322 done, private, flags)); 1323 } 1324 1325 return (zio); 1326 } 1327 1328 zio_t * 1329 zio_trim(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 1330 zio_done_func_t *done, void *private, zio_priority_t priority, 1331 enum zio_flag flags, enum trim_flag trim_flags) 1332 { 1333 zio_t *zio; 1334 1335 ASSERT0(vd->vdev_children); 1336 ASSERT0(P2PHASE(offset, 1ULL << vd->vdev_ashift)); 1337 ASSERT0(P2PHASE(size, 1ULL << vd->vdev_ashift)); 1338 ASSERT3U(size, !=, 0); 1339 1340 zio = zio_create(pio, vd->vdev_spa, 0, NULL, NULL, size, size, done, 1341 private, ZIO_TYPE_TRIM, priority, flags | ZIO_FLAG_PHYSICAL, 1342 vd, offset, NULL, ZIO_STAGE_OPEN, ZIO_TRIM_PIPELINE); 1343 zio->io_trim_flags = trim_flags; 1344 1345 return (zio); 1346 } 1347 1348 zio_t * 1349 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 1350 abd_t *data, int checksum, zio_done_func_t *done, void *private, 1351 zio_priority_t priority, enum zio_flag flags, boolean_t labels) 1352 { 1353 zio_t *zio; 1354 1355 ASSERT(vd->vdev_children == 0); 1356 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE || 1357 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 1358 ASSERT3U(offset + size, <=, vd->vdev_psize); 1359 1360 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done, 1361 private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd, 1362 offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE); 1363 1364 zio->io_prop.zp_checksum = checksum; 1365 1366 return (zio); 1367 } 1368 1369 zio_t * 1370 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 1371 abd_t *data, int checksum, zio_done_func_t *done, void *private, 1372 zio_priority_t priority, enum zio_flag flags, boolean_t labels) 1373 { 1374 zio_t *zio; 1375 1376 ASSERT(vd->vdev_children == 0); 1377 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE || 1378 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 1379 ASSERT3U(offset + size, <=, vd->vdev_psize); 1380 1381 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done, 1382 private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd, 1383 offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE); 1384 1385 zio->io_prop.zp_checksum = checksum; 1386 1387 if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) { 1388 /* 1389 * zec checksums are necessarily destructive -- they modify 1390 * the end of the write buffer to hold the verifier/checksum. 1391 * Therefore, we must make a local copy in case the data is 1392 * being written to multiple places in parallel. 1393 */ 1394 abd_t *wbuf = abd_alloc_sametype(data, size); 1395 abd_copy(wbuf, data, size); 1396 1397 zio_push_transform(zio, wbuf, size, size, NULL); 1398 } 1399 1400 return (zio); 1401 } 1402 1403 /* 1404 * Create a child I/O to do some work for us. 1405 */ 1406 zio_t * 1407 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset, 1408 abd_t *data, uint64_t size, int type, zio_priority_t priority, 1409 enum zio_flag flags, zio_done_func_t *done, void *private) 1410 { 1411 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE; 1412 zio_t *zio; 1413 1414 /* 1415 * vdev child I/Os do not propagate their error to the parent. 1416 * Therefore, for correct operation the caller *must* check for 1417 * and handle the error in the child i/o's done callback. 1418 * The only exceptions are i/os that we don't care about 1419 * (OPTIONAL or REPAIR). 1420 */ 1421 ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) || 1422 done != NULL); 1423 1424 if (type == ZIO_TYPE_READ && bp != NULL) { 1425 /* 1426 * If we have the bp, then the child should perform the 1427 * checksum and the parent need not. This pushes error 1428 * detection as close to the leaves as possible and 1429 * eliminates redundant checksums in the interior nodes. 1430 */ 1431 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY; 1432 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY; 1433 } 1434 1435 if (vd->vdev_ops->vdev_op_leaf) { 1436 ASSERT0(vd->vdev_children); 1437 offset += VDEV_LABEL_START_SIZE; 1438 } 1439 1440 flags |= ZIO_VDEV_CHILD_FLAGS(pio); 1441 1442 /* 1443 * If we've decided to do a repair, the write is not speculative -- 1444 * even if the original read was. 1445 */ 1446 if (flags & ZIO_FLAG_IO_REPAIR) 1447 flags &= ~ZIO_FLAG_SPECULATIVE; 1448 1449 /* 1450 * If we're creating a child I/O that is not associated with a 1451 * top-level vdev, then the child zio is not an allocating I/O. 1452 * If this is a retried I/O then we ignore it since we will 1453 * have already processed the original allocating I/O. 1454 */ 1455 if (flags & ZIO_FLAG_IO_ALLOCATING && 1456 (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) { 1457 ASSERT(pio->io_metaslab_class != NULL); 1458 ASSERT(pio->io_metaslab_class->mc_alloc_throttle_enabled); 1459 ASSERT(type == ZIO_TYPE_WRITE); 1460 ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE); 1461 ASSERT(!(flags & ZIO_FLAG_IO_REPAIR)); 1462 ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) || 1463 pio->io_child_type == ZIO_CHILD_GANG); 1464 1465 flags &= ~ZIO_FLAG_IO_ALLOCATING; 1466 } 1467 1468 1469 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size, 1470 done, private, type, priority, flags, vd, offset, &pio->io_bookmark, 1471 ZIO_STAGE_VDEV_IO_START >> 1, pipeline); 1472 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV); 1473 1474 zio->io_physdone = pio->io_physdone; 1475 if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL) 1476 zio->io_logical->io_phys_children++; 1477 1478 return (zio); 1479 } 1480 1481 zio_t * 1482 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, abd_t *data, uint64_t size, 1483 zio_type_t type, zio_priority_t priority, enum zio_flag flags, 1484 zio_done_func_t *done, void *private) 1485 { 1486 zio_t *zio; 1487 1488 ASSERT(vd->vdev_ops->vdev_op_leaf); 1489 1490 zio = zio_create(NULL, vd->vdev_spa, 0, NULL, 1491 data, size, size, done, private, type, priority, 1492 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED, 1493 vd, offset, NULL, 1494 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE); 1495 1496 return (zio); 1497 } 1498 1499 void 1500 zio_flush(zio_t *zio, vdev_t *vd) 1501 { 1502 zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE, 1503 NULL, NULL, 1504 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY)); 1505 } 1506 1507 void 1508 zio_shrink(zio_t *zio, uint64_t size) 1509 { 1510 ASSERT3P(zio->io_executor, ==, NULL); 1511 ASSERT3U(zio->io_orig_size, ==, zio->io_size); 1512 ASSERT3U(size, <=, zio->io_size); 1513 1514 /* 1515 * We don't shrink for raidz because of problems with the 1516 * reconstruction when reading back less than the block size. 1517 * Note, BP_IS_RAIDZ() assumes no compression. 1518 */ 1519 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 1520 if (!BP_IS_RAIDZ(zio->io_bp)) { 1521 /* we are not doing a raw write */ 1522 ASSERT3U(zio->io_size, ==, zio->io_lsize); 1523 zio->io_orig_size = zio->io_size = zio->io_lsize = size; 1524 } 1525 } 1526 1527 /* 1528 * ========================================================================== 1529 * Prepare to read and write logical blocks 1530 * ========================================================================== 1531 */ 1532 1533 static zio_t * 1534 zio_read_bp_init(zio_t *zio) 1535 { 1536 blkptr_t *bp = zio->io_bp; 1537 uint64_t psize = 1538 BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp); 1539 1540 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy); 1541 1542 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF && 1543 zio->io_child_type == ZIO_CHILD_LOGICAL && 1544 !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) { 1545 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize), 1546 psize, psize, zio_decompress); 1547 } 1548 1549 if (((BP_IS_PROTECTED(bp) && !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) || 1550 BP_HAS_INDIRECT_MAC_CKSUM(bp)) && 1551 zio->io_child_type == ZIO_CHILD_LOGICAL) { 1552 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize), 1553 psize, psize, zio_decrypt); 1554 } 1555 1556 if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) { 1557 int psize = BPE_GET_PSIZE(bp); 1558 void *data = abd_borrow_buf(zio->io_abd, psize); 1559 1560 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1561 decode_embedded_bp_compressed(bp, data); 1562 abd_return_buf_copy(zio->io_abd, data, psize); 1563 } else { 1564 ASSERT(!BP_IS_EMBEDDED(bp)); 1565 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy); 1566 } 1567 1568 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0) 1569 zio->io_flags |= ZIO_FLAG_DONT_CACHE; 1570 1571 if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP) 1572 zio->io_flags |= ZIO_FLAG_DONT_CACHE; 1573 1574 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL) 1575 zio->io_pipeline = ZIO_DDT_READ_PIPELINE; 1576 1577 return (zio); 1578 } 1579 1580 static zio_t * 1581 zio_write_bp_init(zio_t *zio) 1582 { 1583 if (!IO_IS_ALLOCATING(zio)) 1584 return (zio); 1585 1586 ASSERT(zio->io_child_type != ZIO_CHILD_DDT); 1587 1588 if (zio->io_bp_override) { 1589 blkptr_t *bp = zio->io_bp; 1590 zio_prop_t *zp = &zio->io_prop; 1591 1592 ASSERT(bp->blk_birth != zio->io_txg); 1593 ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0); 1594 1595 *bp = *zio->io_bp_override; 1596 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1597 1598 if (BP_IS_EMBEDDED(bp)) 1599 return (zio); 1600 1601 /* 1602 * If we've been overridden and nopwrite is set then 1603 * set the flag accordingly to indicate that a nopwrite 1604 * has already occurred. 1605 */ 1606 if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) { 1607 ASSERT(!zp->zp_dedup); 1608 ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum); 1609 zio->io_flags |= ZIO_FLAG_NOPWRITE; 1610 return (zio); 1611 } 1612 1613 ASSERT(!zp->zp_nopwrite); 1614 1615 if (BP_IS_HOLE(bp) || !zp->zp_dedup) 1616 return (zio); 1617 1618 ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags & 1619 ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify); 1620 1621 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum && 1622 !zp->zp_encrypt) { 1623 BP_SET_DEDUP(bp, 1); 1624 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE; 1625 return (zio); 1626 } 1627 1628 /* 1629 * We were unable to handle this as an override bp, treat 1630 * it as a regular write I/O. 1631 */ 1632 zio->io_bp_override = NULL; 1633 *bp = zio->io_bp_orig; 1634 zio->io_pipeline = zio->io_orig_pipeline; 1635 } 1636 1637 return (zio); 1638 } 1639 1640 static zio_t * 1641 zio_write_compress(zio_t *zio) 1642 { 1643 spa_t *spa = zio->io_spa; 1644 zio_prop_t *zp = &zio->io_prop; 1645 enum zio_compress compress = zp->zp_compress; 1646 blkptr_t *bp = zio->io_bp; 1647 uint64_t lsize = zio->io_lsize; 1648 uint64_t psize = zio->io_size; 1649 uint32_t pass = 1; 1650 1651 /* 1652 * If our children haven't all reached the ready stage, 1653 * wait for them and then repeat this pipeline stage. 1654 */ 1655 if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT | 1656 ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) { 1657 return (NULL); 1658 } 1659 1660 if (!IO_IS_ALLOCATING(zio)) 1661 return (zio); 1662 1663 if (zio->io_children_ready != NULL) { 1664 /* 1665 * Now that all our children are ready, run the callback 1666 * associated with this zio in case it wants to modify the 1667 * data to be written. 1668 */ 1669 ASSERT3U(zp->zp_level, >, 0); 1670 zio->io_children_ready(zio); 1671 } 1672 1673 ASSERT(zio->io_child_type != ZIO_CHILD_DDT); 1674 ASSERT(zio->io_bp_override == NULL); 1675 1676 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) { 1677 /* 1678 * We're rewriting an existing block, which means we're 1679 * working on behalf of spa_sync(). For spa_sync() to 1680 * converge, it must eventually be the case that we don't 1681 * have to allocate new blocks. But compression changes 1682 * the blocksize, which forces a reallocate, and makes 1683 * convergence take longer. Therefore, after the first 1684 * few passes, stop compressing to ensure convergence. 1685 */ 1686 pass = spa_sync_pass(spa); 1687 1688 ASSERT(zio->io_txg == spa_syncing_txg(spa)); 1689 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1690 ASSERT(!BP_GET_DEDUP(bp)); 1691 1692 if (pass >= zfs_sync_pass_dont_compress) 1693 compress = ZIO_COMPRESS_OFF; 1694 1695 /* Make sure someone doesn't change their mind on overwrites */ 1696 ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp), 1697 spa_max_replication(spa)) == BP_GET_NDVAS(bp)); 1698 } 1699 1700 /* If it's a compressed write that is not raw, compress the buffer. */ 1701 if (compress != ZIO_COMPRESS_OFF && 1702 !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) { 1703 void *cbuf = zio_buf_alloc(lsize); 1704 psize = zio_compress_data(compress, zio->io_abd, cbuf, lsize, 1705 zp->zp_complevel); 1706 if (psize == 0 || psize >= lsize) { 1707 compress = ZIO_COMPRESS_OFF; 1708 zio_buf_free(cbuf, lsize); 1709 } else if (!zp->zp_dedup && !zp->zp_encrypt && 1710 psize <= BPE_PAYLOAD_SIZE && 1711 zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) && 1712 spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) { 1713 encode_embedded_bp_compressed(bp, 1714 cbuf, compress, lsize, psize); 1715 BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA); 1716 BP_SET_TYPE(bp, zio->io_prop.zp_type); 1717 BP_SET_LEVEL(bp, zio->io_prop.zp_level); 1718 zio_buf_free(cbuf, lsize); 1719 bp->blk_birth = zio->io_txg; 1720 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1721 ASSERT(spa_feature_is_active(spa, 1722 SPA_FEATURE_EMBEDDED_DATA)); 1723 return (zio); 1724 } else { 1725 /* 1726 * Round compressed size up to the minimum allocation 1727 * size of the smallest-ashift device, and zero the 1728 * tail. This ensures that the compressed size of the 1729 * BP (and thus compressratio property) are correct, 1730 * in that we charge for the padding used to fill out 1731 * the last sector. 1732 */ 1733 ASSERT3U(spa->spa_min_alloc, >=, SPA_MINBLOCKSHIFT); 1734 size_t rounded = (size_t)roundup(psize, 1735 spa->spa_min_alloc); 1736 if (rounded >= lsize) { 1737 compress = ZIO_COMPRESS_OFF; 1738 zio_buf_free(cbuf, lsize); 1739 psize = lsize; 1740 } else { 1741 abd_t *cdata = abd_get_from_buf(cbuf, lsize); 1742 abd_take_ownership_of_buf(cdata, B_TRUE); 1743 abd_zero_off(cdata, psize, rounded - psize); 1744 psize = rounded; 1745 zio_push_transform(zio, cdata, 1746 psize, lsize, NULL); 1747 } 1748 } 1749 1750 /* 1751 * We were unable to handle this as an override bp, treat 1752 * it as a regular write I/O. 1753 */ 1754 zio->io_bp_override = NULL; 1755 *bp = zio->io_bp_orig; 1756 zio->io_pipeline = zio->io_orig_pipeline; 1757 1758 } else if ((zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) != 0 && 1759 zp->zp_type == DMU_OT_DNODE) { 1760 /* 1761 * The DMU actually relies on the zio layer's compression 1762 * to free metadnode blocks that have had all contained 1763 * dnodes freed. As a result, even when doing a raw 1764 * receive, we must check whether the block can be compressed 1765 * to a hole. 1766 */ 1767 psize = zio_compress_data(ZIO_COMPRESS_EMPTY, 1768 zio->io_abd, NULL, lsize, zp->zp_complevel); 1769 if (psize == 0 || psize >= lsize) 1770 compress = ZIO_COMPRESS_OFF; 1771 } else if (zio->io_flags & ZIO_FLAG_RAW_COMPRESS && 1772 !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) { 1773 /* 1774 * If we are raw receiving an encrypted dataset we should not 1775 * take this codepath because it will change the on-disk block 1776 * and decryption will fail. 1777 */ 1778 size_t rounded = MIN((size_t)roundup(psize, 1779 spa->spa_min_alloc), lsize); 1780 1781 if (rounded != psize) { 1782 abd_t *cdata = abd_alloc_linear(rounded, B_TRUE); 1783 abd_zero_off(cdata, psize, rounded - psize); 1784 abd_copy_off(cdata, zio->io_abd, 0, 0, psize); 1785 psize = rounded; 1786 zio_push_transform(zio, cdata, 1787 psize, rounded, NULL); 1788 } 1789 } else { 1790 ASSERT3U(psize, !=, 0); 1791 } 1792 1793 /* 1794 * The final pass of spa_sync() must be all rewrites, but the first 1795 * few passes offer a trade-off: allocating blocks defers convergence, 1796 * but newly allocated blocks are sequential, so they can be written 1797 * to disk faster. Therefore, we allow the first few passes of 1798 * spa_sync() to allocate new blocks, but force rewrites after that. 1799 * There should only be a handful of blocks after pass 1 in any case. 1800 */ 1801 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg && 1802 BP_GET_PSIZE(bp) == psize && 1803 pass >= zfs_sync_pass_rewrite) { 1804 VERIFY3U(psize, !=, 0); 1805 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES; 1806 1807 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages; 1808 zio->io_flags |= ZIO_FLAG_IO_REWRITE; 1809 } else { 1810 BP_ZERO(bp); 1811 zio->io_pipeline = ZIO_WRITE_PIPELINE; 1812 } 1813 1814 if (psize == 0) { 1815 if (zio->io_bp_orig.blk_birth != 0 && 1816 spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) { 1817 BP_SET_LSIZE(bp, lsize); 1818 BP_SET_TYPE(bp, zp->zp_type); 1819 BP_SET_LEVEL(bp, zp->zp_level); 1820 BP_SET_BIRTH(bp, zio->io_txg, 0); 1821 } 1822 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1823 } else { 1824 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER); 1825 BP_SET_LSIZE(bp, lsize); 1826 BP_SET_TYPE(bp, zp->zp_type); 1827 BP_SET_LEVEL(bp, zp->zp_level); 1828 BP_SET_PSIZE(bp, psize); 1829 BP_SET_COMPRESS(bp, compress); 1830 BP_SET_CHECKSUM(bp, zp->zp_checksum); 1831 BP_SET_DEDUP(bp, zp->zp_dedup); 1832 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 1833 if (zp->zp_dedup) { 1834 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1835 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 1836 ASSERT(!zp->zp_encrypt || 1837 DMU_OT_IS_ENCRYPTED(zp->zp_type)); 1838 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE; 1839 } 1840 if (zp->zp_nopwrite) { 1841 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1842 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 1843 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE; 1844 } 1845 } 1846 return (zio); 1847 } 1848 1849 static zio_t * 1850 zio_free_bp_init(zio_t *zio) 1851 { 1852 blkptr_t *bp = zio->io_bp; 1853 1854 if (zio->io_child_type == ZIO_CHILD_LOGICAL) { 1855 if (BP_GET_DEDUP(bp)) 1856 zio->io_pipeline = ZIO_DDT_FREE_PIPELINE; 1857 } 1858 1859 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy); 1860 1861 return (zio); 1862 } 1863 1864 /* 1865 * ========================================================================== 1866 * Execute the I/O pipeline 1867 * ========================================================================== 1868 */ 1869 1870 static void 1871 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline) 1872 { 1873 spa_t *spa = zio->io_spa; 1874 zio_type_t t = zio->io_type; 1875 int flags = (cutinline ? TQ_FRONT : 0); 1876 1877 /* 1878 * If we're a config writer or a probe, the normal issue and 1879 * interrupt threads may all be blocked waiting for the config lock. 1880 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL. 1881 */ 1882 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE)) 1883 t = ZIO_TYPE_NULL; 1884 1885 /* 1886 * A similar issue exists for the L2ARC write thread until L2ARC 2.0. 1887 */ 1888 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux) 1889 t = ZIO_TYPE_NULL; 1890 1891 /* 1892 * If this is a high priority I/O, then use the high priority taskq if 1893 * available. 1894 */ 1895 if ((zio->io_priority == ZIO_PRIORITY_NOW || 1896 zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) && 1897 spa->spa_zio_taskq[t][q + 1].stqs_count != 0) 1898 q++; 1899 1900 ASSERT3U(q, <, ZIO_TASKQ_TYPES); 1901 1902 /* 1903 * NB: We are assuming that the zio can only be dispatched 1904 * to a single taskq at a time. It would be a grievous error 1905 * to dispatch the zio to another taskq at the same time. 1906 */ 1907 ASSERT(taskq_empty_ent(&zio->io_tqent)); 1908 spa_taskq_dispatch_ent(spa, t, q, zio_execute, zio, flags, 1909 &zio->io_tqent); 1910 } 1911 1912 static boolean_t 1913 zio_taskq_member(zio_t *zio, zio_taskq_type_t q) 1914 { 1915 spa_t *spa = zio->io_spa; 1916 1917 taskq_t *tq = taskq_of_curthread(); 1918 1919 for (zio_type_t t = 0; t < ZIO_TYPES; t++) { 1920 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 1921 uint_t i; 1922 for (i = 0; i < tqs->stqs_count; i++) { 1923 if (tqs->stqs_taskq[i] == tq) 1924 return (B_TRUE); 1925 } 1926 } 1927 1928 return (B_FALSE); 1929 } 1930 1931 static zio_t * 1932 zio_issue_async(zio_t *zio) 1933 { 1934 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE); 1935 1936 return (NULL); 1937 } 1938 1939 void 1940 zio_interrupt(void *zio) 1941 { 1942 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE); 1943 } 1944 1945 void 1946 zio_delay_interrupt(zio_t *zio) 1947 { 1948 /* 1949 * The timeout_generic() function isn't defined in userspace, so 1950 * rather than trying to implement the function, the zio delay 1951 * functionality has been disabled for userspace builds. 1952 */ 1953 1954 #ifdef _KERNEL 1955 /* 1956 * If io_target_timestamp is zero, then no delay has been registered 1957 * for this IO, thus jump to the end of this function and "skip" the 1958 * delay; issuing it directly to the zio layer. 1959 */ 1960 if (zio->io_target_timestamp != 0) { 1961 hrtime_t now = gethrtime(); 1962 1963 if (now >= zio->io_target_timestamp) { 1964 /* 1965 * This IO has already taken longer than the target 1966 * delay to complete, so we don't want to delay it 1967 * any longer; we "miss" the delay and issue it 1968 * directly to the zio layer. This is likely due to 1969 * the target latency being set to a value less than 1970 * the underlying hardware can satisfy (e.g. delay 1971 * set to 1ms, but the disks take 10ms to complete an 1972 * IO request). 1973 */ 1974 1975 DTRACE_PROBE2(zio__delay__miss, zio_t *, zio, 1976 hrtime_t, now); 1977 1978 zio_interrupt(zio); 1979 } else { 1980 taskqid_t tid; 1981 hrtime_t diff = zio->io_target_timestamp - now; 1982 clock_t expire_at_tick = ddi_get_lbolt() + 1983 NSEC_TO_TICK(diff); 1984 1985 DTRACE_PROBE3(zio__delay__hit, zio_t *, zio, 1986 hrtime_t, now, hrtime_t, diff); 1987 1988 if (NSEC_TO_TICK(diff) == 0) { 1989 /* Our delay is less than a jiffy - just spin */ 1990 zfs_sleep_until(zio->io_target_timestamp); 1991 zio_interrupt(zio); 1992 } else { 1993 /* 1994 * Use taskq_dispatch_delay() in the place of 1995 * OpenZFS's timeout_generic(). 1996 */ 1997 tid = taskq_dispatch_delay(system_taskq, 1998 zio_interrupt, zio, TQ_NOSLEEP, 1999 expire_at_tick); 2000 if (tid == TASKQID_INVALID) { 2001 /* 2002 * Couldn't allocate a task. Just 2003 * finish the zio without a delay. 2004 */ 2005 zio_interrupt(zio); 2006 } 2007 } 2008 } 2009 return; 2010 } 2011 #endif 2012 DTRACE_PROBE1(zio__delay__skip, zio_t *, zio); 2013 zio_interrupt(zio); 2014 } 2015 2016 static void 2017 zio_deadman_impl(zio_t *pio, int ziodepth) 2018 { 2019 zio_t *cio, *cio_next; 2020 zio_link_t *zl = NULL; 2021 vdev_t *vd = pio->io_vd; 2022 2023 if (zio_deadman_log_all || (vd != NULL && vd->vdev_ops->vdev_op_leaf)) { 2024 vdev_queue_t *vq = vd ? &vd->vdev_queue : NULL; 2025 zbookmark_phys_t *zb = &pio->io_bookmark; 2026 uint64_t delta = gethrtime() - pio->io_timestamp; 2027 uint64_t failmode = spa_get_deadman_failmode(pio->io_spa); 2028 2029 zfs_dbgmsg("slow zio[%d]: zio=%px timestamp=%llu " 2030 "delta=%llu queued=%llu io=%llu " 2031 "path=%s " 2032 "last=%llu type=%d " 2033 "priority=%d flags=0x%x stage=0x%x " 2034 "pipeline=0x%x pipeline-trace=0x%x " 2035 "objset=%llu object=%llu " 2036 "level=%llu blkid=%llu " 2037 "offset=%llu size=%llu " 2038 "error=%d", 2039 ziodepth, pio, pio->io_timestamp, 2040 (u_longlong_t)delta, pio->io_delta, pio->io_delay, 2041 vd ? vd->vdev_path : "NULL", 2042 vq ? vq->vq_io_complete_ts : 0, pio->io_type, 2043 pio->io_priority, pio->io_flags, pio->io_stage, 2044 pio->io_pipeline, pio->io_pipeline_trace, 2045 (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object, 2046 (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid, 2047 (u_longlong_t)pio->io_offset, (u_longlong_t)pio->io_size, 2048 pio->io_error); 2049 (void) zfs_ereport_post(FM_EREPORT_ZFS_DEADMAN, 2050 pio->io_spa, vd, zb, pio, 0); 2051 2052 if (failmode == ZIO_FAILURE_MODE_CONTINUE && 2053 taskq_empty_ent(&pio->io_tqent)) { 2054 zio_interrupt(pio); 2055 } 2056 } 2057 2058 mutex_enter(&pio->io_lock); 2059 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) { 2060 cio_next = zio_walk_children(pio, &zl); 2061 zio_deadman_impl(cio, ziodepth + 1); 2062 } 2063 mutex_exit(&pio->io_lock); 2064 } 2065 2066 /* 2067 * Log the critical information describing this zio and all of its children 2068 * using the zfs_dbgmsg() interface then post deadman event for the ZED. 2069 */ 2070 void 2071 zio_deadman(zio_t *pio, const char *tag) 2072 { 2073 spa_t *spa = pio->io_spa; 2074 char *name = spa_name(spa); 2075 2076 if (!zfs_deadman_enabled || spa_suspended(spa)) 2077 return; 2078 2079 zio_deadman_impl(pio, 0); 2080 2081 switch (spa_get_deadman_failmode(spa)) { 2082 case ZIO_FAILURE_MODE_WAIT: 2083 zfs_dbgmsg("%s waiting for hung I/O to pool '%s'", tag, name); 2084 break; 2085 2086 case ZIO_FAILURE_MODE_CONTINUE: 2087 zfs_dbgmsg("%s restarting hung I/O for pool '%s'", tag, name); 2088 break; 2089 2090 case ZIO_FAILURE_MODE_PANIC: 2091 fm_panic("%s determined I/O to pool '%s' is hung.", tag, name); 2092 break; 2093 } 2094 } 2095 2096 /* 2097 * Execute the I/O pipeline until one of the following occurs: 2098 * (1) the I/O completes; (2) the pipeline stalls waiting for 2099 * dependent child I/Os; (3) the I/O issues, so we're waiting 2100 * for an I/O completion interrupt; (4) the I/O is delegated by 2101 * vdev-level caching or aggregation; (5) the I/O is deferred 2102 * due to vdev-level queueing; (6) the I/O is handed off to 2103 * another thread. In all cases, the pipeline stops whenever 2104 * there's no CPU work; it never burns a thread in cv_wait_io(). 2105 * 2106 * There's no locking on io_stage because there's no legitimate way 2107 * for multiple threads to be attempting to process the same I/O. 2108 */ 2109 static zio_pipe_stage_t *zio_pipeline[]; 2110 2111 /* 2112 * zio_execute() is a wrapper around the static function 2113 * __zio_execute() so that we can force __zio_execute() to be 2114 * inlined. This reduces stack overhead which is important 2115 * because __zio_execute() is called recursively in several zio 2116 * code paths. zio_execute() itself cannot be inlined because 2117 * it is externally visible. 2118 */ 2119 void 2120 zio_execute(void *zio) 2121 { 2122 fstrans_cookie_t cookie; 2123 2124 cookie = spl_fstrans_mark(); 2125 __zio_execute(zio); 2126 spl_fstrans_unmark(cookie); 2127 } 2128 2129 /* 2130 * Used to determine if in the current context the stack is sized large 2131 * enough to allow zio_execute() to be called recursively. A minimum 2132 * stack size of 16K is required to avoid needing to re-dispatch the zio. 2133 */ 2134 static boolean_t 2135 zio_execute_stack_check(zio_t *zio) 2136 { 2137 #if !defined(HAVE_LARGE_STACKS) 2138 dsl_pool_t *dp = spa_get_dsl(zio->io_spa); 2139 2140 /* Executing in txg_sync_thread() context. */ 2141 if (dp && curthread == dp->dp_tx.tx_sync_thread) 2142 return (B_TRUE); 2143 2144 /* Pool initialization outside of zio_taskq context. */ 2145 if (dp && spa_is_initializing(dp->dp_spa) && 2146 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE) && 2147 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE_HIGH)) 2148 return (B_TRUE); 2149 #else 2150 (void) zio; 2151 #endif /* HAVE_LARGE_STACKS */ 2152 2153 return (B_FALSE); 2154 } 2155 2156 __attribute__((always_inline)) 2157 static inline void 2158 __zio_execute(zio_t *zio) 2159 { 2160 ASSERT3U(zio->io_queued_timestamp, >, 0); 2161 2162 while (zio->io_stage < ZIO_STAGE_DONE) { 2163 enum zio_stage pipeline = zio->io_pipeline; 2164 enum zio_stage stage = zio->io_stage; 2165 2166 zio->io_executor = curthread; 2167 2168 ASSERT(!MUTEX_HELD(&zio->io_lock)); 2169 ASSERT(ISP2(stage)); 2170 ASSERT(zio->io_stall == NULL); 2171 2172 do { 2173 stage <<= 1; 2174 } while ((stage & pipeline) == 0); 2175 2176 ASSERT(stage <= ZIO_STAGE_DONE); 2177 2178 /* 2179 * If we are in interrupt context and this pipeline stage 2180 * will grab a config lock that is held across I/O, 2181 * or may wait for an I/O that needs an interrupt thread 2182 * to complete, issue async to avoid deadlock. 2183 * 2184 * For VDEV_IO_START, we cut in line so that the io will 2185 * be sent to disk promptly. 2186 */ 2187 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL && 2188 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) { 2189 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ? 2190 zio_requeue_io_start_cut_in_line : B_FALSE; 2191 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut); 2192 return; 2193 } 2194 2195 /* 2196 * If the current context doesn't have large enough stacks 2197 * the zio must be issued asynchronously to prevent overflow. 2198 */ 2199 if (zio_execute_stack_check(zio)) { 2200 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ? 2201 zio_requeue_io_start_cut_in_line : B_FALSE; 2202 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut); 2203 return; 2204 } 2205 2206 zio->io_stage = stage; 2207 zio->io_pipeline_trace |= zio->io_stage; 2208 2209 /* 2210 * The zio pipeline stage returns the next zio to execute 2211 * (typically the same as this one), or NULL if we should 2212 * stop. 2213 */ 2214 zio = zio_pipeline[highbit64(stage) - 1](zio); 2215 2216 if (zio == NULL) 2217 return; 2218 } 2219 } 2220 2221 2222 /* 2223 * ========================================================================== 2224 * Initiate I/O, either sync or async 2225 * ========================================================================== 2226 */ 2227 int 2228 zio_wait(zio_t *zio) 2229 { 2230 /* 2231 * Some routines, like zio_free_sync(), may return a NULL zio 2232 * to avoid the performance overhead of creating and then destroying 2233 * an unneeded zio. For the callers' simplicity, we accept a NULL 2234 * zio and ignore it. 2235 */ 2236 if (zio == NULL) 2237 return (0); 2238 2239 long timeout = MSEC_TO_TICK(zfs_deadman_ziotime_ms); 2240 int error; 2241 2242 ASSERT3S(zio->io_stage, ==, ZIO_STAGE_OPEN); 2243 ASSERT3P(zio->io_executor, ==, NULL); 2244 2245 zio->io_waiter = curthread; 2246 ASSERT0(zio->io_queued_timestamp); 2247 zio->io_queued_timestamp = gethrtime(); 2248 2249 __zio_execute(zio); 2250 2251 mutex_enter(&zio->io_lock); 2252 while (zio->io_executor != NULL) { 2253 error = cv_timedwait_io(&zio->io_cv, &zio->io_lock, 2254 ddi_get_lbolt() + timeout); 2255 2256 if (zfs_deadman_enabled && error == -1 && 2257 gethrtime() - zio->io_queued_timestamp > 2258 spa_deadman_ziotime(zio->io_spa)) { 2259 mutex_exit(&zio->io_lock); 2260 timeout = MSEC_TO_TICK(zfs_deadman_checktime_ms); 2261 zio_deadman(zio, FTAG); 2262 mutex_enter(&zio->io_lock); 2263 } 2264 } 2265 mutex_exit(&zio->io_lock); 2266 2267 error = zio->io_error; 2268 zio_destroy(zio); 2269 2270 return (error); 2271 } 2272 2273 void 2274 zio_nowait(zio_t *zio) 2275 { 2276 /* 2277 * See comment in zio_wait(). 2278 */ 2279 if (zio == NULL) 2280 return; 2281 2282 ASSERT3P(zio->io_executor, ==, NULL); 2283 2284 if (zio->io_child_type == ZIO_CHILD_LOGICAL && 2285 zio_unique_parent(zio) == NULL) { 2286 zio_t *pio; 2287 2288 /* 2289 * This is a logical async I/O with no parent to wait for it. 2290 * We add it to the spa_async_root_zio "Godfather" I/O which 2291 * will ensure they complete prior to unloading the pool. 2292 */ 2293 spa_t *spa = zio->io_spa; 2294 pio = spa->spa_async_zio_root[CPU_SEQID_UNSTABLE]; 2295 2296 zio_add_child(pio, zio); 2297 } 2298 2299 ASSERT0(zio->io_queued_timestamp); 2300 zio->io_queued_timestamp = gethrtime(); 2301 __zio_execute(zio); 2302 } 2303 2304 /* 2305 * ========================================================================== 2306 * Reexecute, cancel, or suspend/resume failed I/O 2307 * ========================================================================== 2308 */ 2309 2310 static void 2311 zio_reexecute(void *arg) 2312 { 2313 zio_t *pio = arg; 2314 zio_t *cio, *cio_next; 2315 2316 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL); 2317 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN); 2318 ASSERT(pio->io_gang_leader == NULL); 2319 ASSERT(pio->io_gang_tree == NULL); 2320 2321 pio->io_flags = pio->io_orig_flags; 2322 pio->io_stage = pio->io_orig_stage; 2323 pio->io_pipeline = pio->io_orig_pipeline; 2324 pio->io_reexecute = 0; 2325 pio->io_flags |= ZIO_FLAG_REEXECUTED; 2326 pio->io_pipeline_trace = 0; 2327 pio->io_error = 0; 2328 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 2329 pio->io_state[w] = 0; 2330 for (int c = 0; c < ZIO_CHILD_TYPES; c++) 2331 pio->io_child_error[c] = 0; 2332 2333 if (IO_IS_ALLOCATING(pio)) 2334 BP_ZERO(pio->io_bp); 2335 2336 /* 2337 * As we reexecute pio's children, new children could be created. 2338 * New children go to the head of pio's io_child_list, however, 2339 * so we will (correctly) not reexecute them. The key is that 2340 * the remainder of pio's io_child_list, from 'cio_next' onward, 2341 * cannot be affected by any side effects of reexecuting 'cio'. 2342 */ 2343 zio_link_t *zl = NULL; 2344 mutex_enter(&pio->io_lock); 2345 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) { 2346 cio_next = zio_walk_children(pio, &zl); 2347 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 2348 pio->io_children[cio->io_child_type][w]++; 2349 mutex_exit(&pio->io_lock); 2350 zio_reexecute(cio); 2351 mutex_enter(&pio->io_lock); 2352 } 2353 mutex_exit(&pio->io_lock); 2354 2355 /* 2356 * Now that all children have been reexecuted, execute the parent. 2357 * We don't reexecute "The Godfather" I/O here as it's the 2358 * responsibility of the caller to wait on it. 2359 */ 2360 if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) { 2361 pio->io_queued_timestamp = gethrtime(); 2362 __zio_execute(pio); 2363 } 2364 } 2365 2366 void 2367 zio_suspend(spa_t *spa, zio_t *zio, zio_suspend_reason_t reason) 2368 { 2369 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC) 2370 fm_panic("Pool '%s' has encountered an uncorrectable I/O " 2371 "failure and the failure mode property for this pool " 2372 "is set to panic.", spa_name(spa)); 2373 2374 cmn_err(CE_WARN, "Pool '%s' has encountered an uncorrectable I/O " 2375 "failure and has been suspended.\n", spa_name(spa)); 2376 2377 (void) zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, 2378 NULL, NULL, 0); 2379 2380 mutex_enter(&spa->spa_suspend_lock); 2381 2382 if (spa->spa_suspend_zio_root == NULL) 2383 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL, 2384 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 2385 ZIO_FLAG_GODFATHER); 2386 2387 spa->spa_suspended = reason; 2388 2389 if (zio != NULL) { 2390 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER)); 2391 ASSERT(zio != spa->spa_suspend_zio_root); 2392 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2393 ASSERT(zio_unique_parent(zio) == NULL); 2394 ASSERT(zio->io_stage == ZIO_STAGE_DONE); 2395 zio_add_child(spa->spa_suspend_zio_root, zio); 2396 } 2397 2398 mutex_exit(&spa->spa_suspend_lock); 2399 } 2400 2401 int 2402 zio_resume(spa_t *spa) 2403 { 2404 zio_t *pio; 2405 2406 /* 2407 * Reexecute all previously suspended i/o. 2408 */ 2409 mutex_enter(&spa->spa_suspend_lock); 2410 spa->spa_suspended = ZIO_SUSPEND_NONE; 2411 cv_broadcast(&spa->spa_suspend_cv); 2412 pio = spa->spa_suspend_zio_root; 2413 spa->spa_suspend_zio_root = NULL; 2414 mutex_exit(&spa->spa_suspend_lock); 2415 2416 if (pio == NULL) 2417 return (0); 2418 2419 zio_reexecute(pio); 2420 return (zio_wait(pio)); 2421 } 2422 2423 void 2424 zio_resume_wait(spa_t *spa) 2425 { 2426 mutex_enter(&spa->spa_suspend_lock); 2427 while (spa_suspended(spa)) 2428 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock); 2429 mutex_exit(&spa->spa_suspend_lock); 2430 } 2431 2432 /* 2433 * ========================================================================== 2434 * Gang blocks. 2435 * 2436 * A gang block is a collection of small blocks that looks to the DMU 2437 * like one large block. When zio_dva_allocate() cannot find a block 2438 * of the requested size, due to either severe fragmentation or the pool 2439 * being nearly full, it calls zio_write_gang_block() to construct the 2440 * block from smaller fragments. 2441 * 2442 * A gang block consists of a gang header (zio_gbh_phys_t) and up to 2443 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like 2444 * an indirect block: it's an array of block pointers. It consumes 2445 * only one sector and hence is allocatable regardless of fragmentation. 2446 * The gang header's bps point to its gang members, which hold the data. 2447 * 2448 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg> 2449 * as the verifier to ensure uniqueness of the SHA256 checksum. 2450 * Critically, the gang block bp's blk_cksum is the checksum of the data, 2451 * not the gang header. This ensures that data block signatures (needed for 2452 * deduplication) are independent of how the block is physically stored. 2453 * 2454 * Gang blocks can be nested: a gang member may itself be a gang block. 2455 * Thus every gang block is a tree in which root and all interior nodes are 2456 * gang headers, and the leaves are normal blocks that contain user data. 2457 * The root of the gang tree is called the gang leader. 2458 * 2459 * To perform any operation (read, rewrite, free, claim) on a gang block, 2460 * zio_gang_assemble() first assembles the gang tree (minus data leaves) 2461 * in the io_gang_tree field of the original logical i/o by recursively 2462 * reading the gang leader and all gang headers below it. This yields 2463 * an in-core tree containing the contents of every gang header and the 2464 * bps for every constituent of the gang block. 2465 * 2466 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree 2467 * and invokes a callback on each bp. To free a gang block, zio_gang_issue() 2468 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp. 2469 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim(). 2470 * zio_read_gang() is a wrapper around zio_read() that omits reading gang 2471 * headers, since we already have those in io_gang_tree. zio_rewrite_gang() 2472 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite() 2473 * of the gang header plus zio_checksum_compute() of the data to update the 2474 * gang header's blk_cksum as described above. 2475 * 2476 * The two-phase assemble/issue model solves the problem of partial failure -- 2477 * what if you'd freed part of a gang block but then couldn't read the 2478 * gang header for another part? Assembling the entire gang tree first 2479 * ensures that all the necessary gang header I/O has succeeded before 2480 * starting the actual work of free, claim, or write. Once the gang tree 2481 * is assembled, free and claim are in-memory operations that cannot fail. 2482 * 2483 * In the event that a gang write fails, zio_dva_unallocate() walks the 2484 * gang tree to immediately free (i.e. insert back into the space map) 2485 * everything we've allocated. This ensures that we don't get ENOSPC 2486 * errors during repeated suspend/resume cycles due to a flaky device. 2487 * 2488 * Gang rewrites only happen during sync-to-convergence. If we can't assemble 2489 * the gang tree, we won't modify the block, so we can safely defer the free 2490 * (knowing that the block is still intact). If we *can* assemble the gang 2491 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free 2492 * each constituent bp and we can allocate a new block on the next sync pass. 2493 * 2494 * In all cases, the gang tree allows complete recovery from partial failure. 2495 * ========================================================================== 2496 */ 2497 2498 static void 2499 zio_gang_issue_func_done(zio_t *zio) 2500 { 2501 abd_free(zio->io_abd); 2502 } 2503 2504 static zio_t * 2505 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data, 2506 uint64_t offset) 2507 { 2508 if (gn != NULL) 2509 return (pio); 2510 2511 return (zio_read(pio, pio->io_spa, bp, abd_get_offset(data, offset), 2512 BP_GET_PSIZE(bp), zio_gang_issue_func_done, 2513 NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), 2514 &pio->io_bookmark)); 2515 } 2516 2517 static zio_t * 2518 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data, 2519 uint64_t offset) 2520 { 2521 zio_t *zio; 2522 2523 if (gn != NULL) { 2524 abd_t *gbh_abd = 2525 abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE); 2526 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp, 2527 gbh_abd, SPA_GANGBLOCKSIZE, zio_gang_issue_func_done, NULL, 2528 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), 2529 &pio->io_bookmark); 2530 /* 2531 * As we rewrite each gang header, the pipeline will compute 2532 * a new gang block header checksum for it; but no one will 2533 * compute a new data checksum, so we do that here. The one 2534 * exception is the gang leader: the pipeline already computed 2535 * its data checksum because that stage precedes gang assembly. 2536 * (Presently, nothing actually uses interior data checksums; 2537 * this is just good hygiene.) 2538 */ 2539 if (gn != pio->io_gang_leader->io_gang_tree) { 2540 abd_t *buf = abd_get_offset(data, offset); 2541 2542 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp), 2543 buf, BP_GET_PSIZE(bp)); 2544 2545 abd_free(buf); 2546 } 2547 /* 2548 * If we are here to damage data for testing purposes, 2549 * leave the GBH alone so that we can detect the damage. 2550 */ 2551 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE) 2552 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES; 2553 } else { 2554 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp, 2555 abd_get_offset(data, offset), BP_GET_PSIZE(bp), 2556 zio_gang_issue_func_done, NULL, pio->io_priority, 2557 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 2558 } 2559 2560 return (zio); 2561 } 2562 2563 static zio_t * 2564 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data, 2565 uint64_t offset) 2566 { 2567 (void) gn, (void) data, (void) offset; 2568 2569 zio_t *zio = zio_free_sync(pio, pio->io_spa, pio->io_txg, bp, 2570 ZIO_GANG_CHILD_FLAGS(pio)); 2571 if (zio == NULL) { 2572 zio = zio_null(pio, pio->io_spa, 2573 NULL, NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)); 2574 } 2575 return (zio); 2576 } 2577 2578 static zio_t * 2579 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data, 2580 uint64_t offset) 2581 { 2582 (void) gn, (void) data, (void) offset; 2583 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp, 2584 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio))); 2585 } 2586 2587 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = { 2588 NULL, 2589 zio_read_gang, 2590 zio_rewrite_gang, 2591 zio_free_gang, 2592 zio_claim_gang, 2593 NULL 2594 }; 2595 2596 static void zio_gang_tree_assemble_done(zio_t *zio); 2597 2598 static zio_gang_node_t * 2599 zio_gang_node_alloc(zio_gang_node_t **gnpp) 2600 { 2601 zio_gang_node_t *gn; 2602 2603 ASSERT(*gnpp == NULL); 2604 2605 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP); 2606 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE); 2607 *gnpp = gn; 2608 2609 return (gn); 2610 } 2611 2612 static void 2613 zio_gang_node_free(zio_gang_node_t **gnpp) 2614 { 2615 zio_gang_node_t *gn = *gnpp; 2616 2617 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) 2618 ASSERT(gn->gn_child[g] == NULL); 2619 2620 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE); 2621 kmem_free(gn, sizeof (*gn)); 2622 *gnpp = NULL; 2623 } 2624 2625 static void 2626 zio_gang_tree_free(zio_gang_node_t **gnpp) 2627 { 2628 zio_gang_node_t *gn = *gnpp; 2629 2630 if (gn == NULL) 2631 return; 2632 2633 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) 2634 zio_gang_tree_free(&gn->gn_child[g]); 2635 2636 zio_gang_node_free(gnpp); 2637 } 2638 2639 static void 2640 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp) 2641 { 2642 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp); 2643 abd_t *gbh_abd = abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE); 2644 2645 ASSERT(gio->io_gang_leader == gio); 2646 ASSERT(BP_IS_GANG(bp)); 2647 2648 zio_nowait(zio_read(gio, gio->io_spa, bp, gbh_abd, SPA_GANGBLOCKSIZE, 2649 zio_gang_tree_assemble_done, gn, gio->io_priority, 2650 ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark)); 2651 } 2652 2653 static void 2654 zio_gang_tree_assemble_done(zio_t *zio) 2655 { 2656 zio_t *gio = zio->io_gang_leader; 2657 zio_gang_node_t *gn = zio->io_private; 2658 blkptr_t *bp = zio->io_bp; 2659 2660 ASSERT(gio == zio_unique_parent(zio)); 2661 ASSERT(zio->io_child_count == 0); 2662 2663 if (zio->io_error) 2664 return; 2665 2666 /* this ABD was created from a linear buf in zio_gang_tree_assemble */ 2667 if (BP_SHOULD_BYTESWAP(bp)) 2668 byteswap_uint64_array(abd_to_buf(zio->io_abd), zio->io_size); 2669 2670 ASSERT3P(abd_to_buf(zio->io_abd), ==, gn->gn_gbh); 2671 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE); 2672 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC); 2673 2674 abd_free(zio->io_abd); 2675 2676 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 2677 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g]; 2678 if (!BP_IS_GANG(gbp)) 2679 continue; 2680 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]); 2681 } 2682 } 2683 2684 static void 2685 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, abd_t *data, 2686 uint64_t offset) 2687 { 2688 zio_t *gio = pio->io_gang_leader; 2689 zio_t *zio; 2690 2691 ASSERT(BP_IS_GANG(bp) == !!gn); 2692 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp)); 2693 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree); 2694 2695 /* 2696 * If you're a gang header, your data is in gn->gn_gbh. 2697 * If you're a gang member, your data is in 'data' and gn == NULL. 2698 */ 2699 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data, offset); 2700 2701 if (gn != NULL) { 2702 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC); 2703 2704 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 2705 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g]; 2706 if (BP_IS_HOLE(gbp)) 2707 continue; 2708 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data, 2709 offset); 2710 offset += BP_GET_PSIZE(gbp); 2711 } 2712 } 2713 2714 if (gn == gio->io_gang_tree) 2715 ASSERT3U(gio->io_size, ==, offset); 2716 2717 if (zio != pio) 2718 zio_nowait(zio); 2719 } 2720 2721 static zio_t * 2722 zio_gang_assemble(zio_t *zio) 2723 { 2724 blkptr_t *bp = zio->io_bp; 2725 2726 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL); 2727 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 2728 2729 zio->io_gang_leader = zio; 2730 2731 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree); 2732 2733 return (zio); 2734 } 2735 2736 static zio_t * 2737 zio_gang_issue(zio_t *zio) 2738 { 2739 blkptr_t *bp = zio->io_bp; 2740 2741 if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT, ZIO_WAIT_DONE)) { 2742 return (NULL); 2743 } 2744 2745 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio); 2746 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 2747 2748 if (zio->io_child_error[ZIO_CHILD_GANG] == 0) 2749 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd, 2750 0); 2751 else 2752 zio_gang_tree_free(&zio->io_gang_tree); 2753 2754 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 2755 2756 return (zio); 2757 } 2758 2759 static void 2760 zio_write_gang_member_ready(zio_t *zio) 2761 { 2762 zio_t *pio = zio_unique_parent(zio); 2763 dva_t *cdva = zio->io_bp->blk_dva; 2764 dva_t *pdva = pio->io_bp->blk_dva; 2765 uint64_t asize; 2766 zio_t *gio __maybe_unused = zio->io_gang_leader; 2767 2768 if (BP_IS_HOLE(zio->io_bp)) 2769 return; 2770 2771 ASSERT(BP_IS_HOLE(&zio->io_bp_orig)); 2772 2773 ASSERT(zio->io_child_type == ZIO_CHILD_GANG); 2774 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies); 2775 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp)); 2776 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp)); 2777 ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp)); 2778 2779 mutex_enter(&pio->io_lock); 2780 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) { 2781 ASSERT(DVA_GET_GANG(&pdva[d])); 2782 asize = DVA_GET_ASIZE(&pdva[d]); 2783 asize += DVA_GET_ASIZE(&cdva[d]); 2784 DVA_SET_ASIZE(&pdva[d], asize); 2785 } 2786 mutex_exit(&pio->io_lock); 2787 } 2788 2789 static void 2790 zio_write_gang_done(zio_t *zio) 2791 { 2792 /* 2793 * The io_abd field will be NULL for a zio with no data. The io_flags 2794 * will initially have the ZIO_FLAG_NODATA bit flag set, but we can't 2795 * check for it here as it is cleared in zio_ready. 2796 */ 2797 if (zio->io_abd != NULL) 2798 abd_free(zio->io_abd); 2799 } 2800 2801 static zio_t * 2802 zio_write_gang_block(zio_t *pio, metaslab_class_t *mc) 2803 { 2804 spa_t *spa = pio->io_spa; 2805 blkptr_t *bp = pio->io_bp; 2806 zio_t *gio = pio->io_gang_leader; 2807 zio_t *zio; 2808 zio_gang_node_t *gn, **gnpp; 2809 zio_gbh_phys_t *gbh; 2810 abd_t *gbh_abd; 2811 uint64_t txg = pio->io_txg; 2812 uint64_t resid = pio->io_size; 2813 uint64_t lsize; 2814 int copies = gio->io_prop.zp_copies; 2815 int gbh_copies; 2816 zio_prop_t zp; 2817 int error; 2818 boolean_t has_data = !(pio->io_flags & ZIO_FLAG_NODATA); 2819 2820 /* 2821 * encrypted blocks need DVA[2] free so encrypted gang headers can't 2822 * have a third copy. 2823 */ 2824 gbh_copies = MIN(copies + 1, spa_max_replication(spa)); 2825 if (gio->io_prop.zp_encrypt && gbh_copies >= SPA_DVAS_PER_BP) 2826 gbh_copies = SPA_DVAS_PER_BP - 1; 2827 2828 int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER; 2829 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) { 2830 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE); 2831 ASSERT(has_data); 2832 2833 flags |= METASLAB_ASYNC_ALLOC; 2834 VERIFY(zfs_refcount_held(&mc->mc_allocator[pio->io_allocator]. 2835 mca_alloc_slots, pio)); 2836 2837 /* 2838 * The logical zio has already placed a reservation for 2839 * 'copies' allocation slots but gang blocks may require 2840 * additional copies. These additional copies 2841 * (i.e. gbh_copies - copies) are guaranteed to succeed 2842 * since metaslab_class_throttle_reserve() always allows 2843 * additional reservations for gang blocks. 2844 */ 2845 VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies, 2846 pio->io_allocator, pio, flags)); 2847 } 2848 2849 error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE, 2850 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags, 2851 &pio->io_alloc_list, pio, pio->io_allocator); 2852 if (error) { 2853 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) { 2854 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE); 2855 ASSERT(has_data); 2856 2857 /* 2858 * If we failed to allocate the gang block header then 2859 * we remove any additional allocation reservations that 2860 * we placed here. The original reservation will 2861 * be removed when the logical I/O goes to the ready 2862 * stage. 2863 */ 2864 metaslab_class_throttle_unreserve(mc, 2865 gbh_copies - copies, pio->io_allocator, pio); 2866 } 2867 2868 pio->io_error = error; 2869 return (pio); 2870 } 2871 2872 if (pio == gio) { 2873 gnpp = &gio->io_gang_tree; 2874 } else { 2875 gnpp = pio->io_private; 2876 ASSERT(pio->io_ready == zio_write_gang_member_ready); 2877 } 2878 2879 gn = zio_gang_node_alloc(gnpp); 2880 gbh = gn->gn_gbh; 2881 memset(gbh, 0, SPA_GANGBLOCKSIZE); 2882 gbh_abd = abd_get_from_buf(gbh, SPA_GANGBLOCKSIZE); 2883 2884 /* 2885 * Create the gang header. 2886 */ 2887 zio = zio_rewrite(pio, spa, txg, bp, gbh_abd, SPA_GANGBLOCKSIZE, 2888 zio_write_gang_done, NULL, pio->io_priority, 2889 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 2890 2891 /* 2892 * Create and nowait the gang children. 2893 */ 2894 for (int g = 0; resid != 0; resid -= lsize, g++) { 2895 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g), 2896 SPA_MINBLOCKSIZE); 2897 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid); 2898 2899 zp.zp_checksum = gio->io_prop.zp_checksum; 2900 zp.zp_compress = ZIO_COMPRESS_OFF; 2901 zp.zp_complevel = gio->io_prop.zp_complevel; 2902 zp.zp_type = DMU_OT_NONE; 2903 zp.zp_level = 0; 2904 zp.zp_copies = gio->io_prop.zp_copies; 2905 zp.zp_dedup = B_FALSE; 2906 zp.zp_dedup_verify = B_FALSE; 2907 zp.zp_nopwrite = B_FALSE; 2908 zp.zp_encrypt = gio->io_prop.zp_encrypt; 2909 zp.zp_byteorder = gio->io_prop.zp_byteorder; 2910 memset(zp.zp_salt, 0, ZIO_DATA_SALT_LEN); 2911 memset(zp.zp_iv, 0, ZIO_DATA_IV_LEN); 2912 memset(zp.zp_mac, 0, ZIO_DATA_MAC_LEN); 2913 2914 zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g], 2915 has_data ? abd_get_offset(pio->io_abd, pio->io_size - 2916 resid) : NULL, lsize, lsize, &zp, 2917 zio_write_gang_member_ready, NULL, NULL, 2918 zio_write_gang_done, &gn->gn_child[g], pio->io_priority, 2919 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 2920 2921 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) { 2922 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE); 2923 ASSERT(has_data); 2924 2925 /* 2926 * Gang children won't throttle but we should 2927 * account for their work, so reserve an allocation 2928 * slot for them here. 2929 */ 2930 VERIFY(metaslab_class_throttle_reserve(mc, 2931 zp.zp_copies, cio->io_allocator, cio, flags)); 2932 } 2933 zio_nowait(cio); 2934 } 2935 2936 /* 2937 * Set pio's pipeline to just wait for zio to finish. 2938 */ 2939 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 2940 2941 /* 2942 * We didn't allocate this bp, so make sure it doesn't get unmarked. 2943 */ 2944 pio->io_flags &= ~ZIO_FLAG_FASTWRITE; 2945 2946 zio_nowait(zio); 2947 2948 return (pio); 2949 } 2950 2951 /* 2952 * The zio_nop_write stage in the pipeline determines if allocating a 2953 * new bp is necessary. The nopwrite feature can handle writes in 2954 * either syncing or open context (i.e. zil writes) and as a result is 2955 * mutually exclusive with dedup. 2956 * 2957 * By leveraging a cryptographically secure checksum, such as SHA256, we 2958 * can compare the checksums of the new data and the old to determine if 2959 * allocating a new block is required. Note that our requirements for 2960 * cryptographic strength are fairly weak: there can't be any accidental 2961 * hash collisions, but we don't need to be secure against intentional 2962 * (malicious) collisions. To trigger a nopwrite, you have to be able 2963 * to write the file to begin with, and triggering an incorrect (hash 2964 * collision) nopwrite is no worse than simply writing to the file. 2965 * That said, there are no known attacks against the checksum algorithms 2966 * used for nopwrite, assuming that the salt and the checksums 2967 * themselves remain secret. 2968 */ 2969 static zio_t * 2970 zio_nop_write(zio_t *zio) 2971 { 2972 blkptr_t *bp = zio->io_bp; 2973 blkptr_t *bp_orig = &zio->io_bp_orig; 2974 zio_prop_t *zp = &zio->io_prop; 2975 2976 ASSERT(BP_GET_LEVEL(bp) == 0); 2977 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 2978 ASSERT(zp->zp_nopwrite); 2979 ASSERT(!zp->zp_dedup); 2980 ASSERT(zio->io_bp_override == NULL); 2981 ASSERT(IO_IS_ALLOCATING(zio)); 2982 2983 /* 2984 * Check to see if the original bp and the new bp have matching 2985 * characteristics (i.e. same checksum, compression algorithms, etc). 2986 * If they don't then just continue with the pipeline which will 2987 * allocate a new bp. 2988 */ 2989 if (BP_IS_HOLE(bp_orig) || 2990 !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags & 2991 ZCHECKSUM_FLAG_NOPWRITE) || 2992 BP_IS_ENCRYPTED(bp) || BP_IS_ENCRYPTED(bp_orig) || 2993 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) || 2994 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) || 2995 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) || 2996 zp->zp_copies != BP_GET_NDVAS(bp_orig)) 2997 return (zio); 2998 2999 /* 3000 * If the checksums match then reset the pipeline so that we 3001 * avoid allocating a new bp and issuing any I/O. 3002 */ 3003 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) { 3004 ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags & 3005 ZCHECKSUM_FLAG_NOPWRITE); 3006 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig)); 3007 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig)); 3008 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF); 3009 ASSERT(memcmp(&bp->blk_prop, &bp_orig->blk_prop, 3010 sizeof (uint64_t)) == 0); 3011 3012 /* 3013 * If we're overwriting a block that is currently on an 3014 * indirect vdev, then ignore the nopwrite request and 3015 * allow a new block to be allocated on a concrete vdev. 3016 */ 3017 spa_config_enter(zio->io_spa, SCL_VDEV, FTAG, RW_READER); 3018 vdev_t *tvd = vdev_lookup_top(zio->io_spa, 3019 DVA_GET_VDEV(&bp->blk_dva[0])); 3020 if (tvd->vdev_ops == &vdev_indirect_ops) { 3021 spa_config_exit(zio->io_spa, SCL_VDEV, FTAG); 3022 return (zio); 3023 } 3024 spa_config_exit(zio->io_spa, SCL_VDEV, FTAG); 3025 3026 *bp = *bp_orig; 3027 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 3028 zio->io_flags |= ZIO_FLAG_NOPWRITE; 3029 } 3030 3031 return (zio); 3032 } 3033 3034 /* 3035 * ========================================================================== 3036 * Dedup 3037 * ========================================================================== 3038 */ 3039 static void 3040 zio_ddt_child_read_done(zio_t *zio) 3041 { 3042 blkptr_t *bp = zio->io_bp; 3043 ddt_entry_t *dde = zio->io_private; 3044 ddt_phys_t *ddp; 3045 zio_t *pio = zio_unique_parent(zio); 3046 3047 mutex_enter(&pio->io_lock); 3048 ddp = ddt_phys_select(dde, bp); 3049 if (zio->io_error == 0) 3050 ddt_phys_clear(ddp); /* this ddp doesn't need repair */ 3051 3052 if (zio->io_error == 0 && dde->dde_repair_abd == NULL) 3053 dde->dde_repair_abd = zio->io_abd; 3054 else 3055 abd_free(zio->io_abd); 3056 mutex_exit(&pio->io_lock); 3057 } 3058 3059 static zio_t * 3060 zio_ddt_read_start(zio_t *zio) 3061 { 3062 blkptr_t *bp = zio->io_bp; 3063 3064 ASSERT(BP_GET_DEDUP(bp)); 3065 ASSERT(BP_GET_PSIZE(bp) == zio->io_size); 3066 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 3067 3068 if (zio->io_child_error[ZIO_CHILD_DDT]) { 3069 ddt_t *ddt = ddt_select(zio->io_spa, bp); 3070 ddt_entry_t *dde = ddt_repair_start(ddt, bp); 3071 ddt_phys_t *ddp = dde->dde_phys; 3072 ddt_phys_t *ddp_self = ddt_phys_select(dde, bp); 3073 blkptr_t blk; 3074 3075 ASSERT(zio->io_vsd == NULL); 3076 zio->io_vsd = dde; 3077 3078 if (ddp_self == NULL) 3079 return (zio); 3080 3081 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 3082 if (ddp->ddp_phys_birth == 0 || ddp == ddp_self) 3083 continue; 3084 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp, 3085 &blk); 3086 zio_nowait(zio_read(zio, zio->io_spa, &blk, 3087 abd_alloc_for_io(zio->io_size, B_TRUE), 3088 zio->io_size, zio_ddt_child_read_done, dde, 3089 zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) | 3090 ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark)); 3091 } 3092 return (zio); 3093 } 3094 3095 zio_nowait(zio_read(zio, zio->io_spa, bp, 3096 zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority, 3097 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark)); 3098 3099 return (zio); 3100 } 3101 3102 static zio_t * 3103 zio_ddt_read_done(zio_t *zio) 3104 { 3105 blkptr_t *bp = zio->io_bp; 3106 3107 if (zio_wait_for_children(zio, ZIO_CHILD_DDT_BIT, ZIO_WAIT_DONE)) { 3108 return (NULL); 3109 } 3110 3111 ASSERT(BP_GET_DEDUP(bp)); 3112 ASSERT(BP_GET_PSIZE(bp) == zio->io_size); 3113 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 3114 3115 if (zio->io_child_error[ZIO_CHILD_DDT]) { 3116 ddt_t *ddt = ddt_select(zio->io_spa, bp); 3117 ddt_entry_t *dde = zio->io_vsd; 3118 if (ddt == NULL) { 3119 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE); 3120 return (zio); 3121 } 3122 if (dde == NULL) { 3123 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1; 3124 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE); 3125 return (NULL); 3126 } 3127 if (dde->dde_repair_abd != NULL) { 3128 abd_copy(zio->io_abd, dde->dde_repair_abd, 3129 zio->io_size); 3130 zio->io_child_error[ZIO_CHILD_DDT] = 0; 3131 } 3132 ddt_repair_done(ddt, dde); 3133 zio->io_vsd = NULL; 3134 } 3135 3136 ASSERT(zio->io_vsd == NULL); 3137 3138 return (zio); 3139 } 3140 3141 static boolean_t 3142 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde) 3143 { 3144 spa_t *spa = zio->io_spa; 3145 boolean_t do_raw = !!(zio->io_flags & ZIO_FLAG_RAW); 3146 3147 ASSERT(!(zio->io_bp_override && do_raw)); 3148 3149 /* 3150 * Note: we compare the original data, not the transformed data, 3151 * because when zio->io_bp is an override bp, we will not have 3152 * pushed the I/O transforms. That's an important optimization 3153 * because otherwise we'd compress/encrypt all dmu_sync() data twice. 3154 * However, we should never get a raw, override zio so in these 3155 * cases we can compare the io_abd directly. This is useful because 3156 * it allows us to do dedup verification even if we don't have access 3157 * to the original data (for instance, if the encryption keys aren't 3158 * loaded). 3159 */ 3160 3161 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) { 3162 zio_t *lio = dde->dde_lead_zio[p]; 3163 3164 if (lio != NULL && do_raw) { 3165 return (lio->io_size != zio->io_size || 3166 abd_cmp(zio->io_abd, lio->io_abd) != 0); 3167 } else if (lio != NULL) { 3168 return (lio->io_orig_size != zio->io_orig_size || 3169 abd_cmp(zio->io_orig_abd, lio->io_orig_abd) != 0); 3170 } 3171 } 3172 3173 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) { 3174 ddt_phys_t *ddp = &dde->dde_phys[p]; 3175 3176 if (ddp->ddp_phys_birth != 0 && do_raw) { 3177 blkptr_t blk = *zio->io_bp; 3178 uint64_t psize; 3179 abd_t *tmpabd; 3180 int error; 3181 3182 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth); 3183 psize = BP_GET_PSIZE(&blk); 3184 3185 if (psize != zio->io_size) 3186 return (B_TRUE); 3187 3188 ddt_exit(ddt); 3189 3190 tmpabd = abd_alloc_for_io(psize, B_TRUE); 3191 3192 error = zio_wait(zio_read(NULL, spa, &blk, tmpabd, 3193 psize, NULL, NULL, ZIO_PRIORITY_SYNC_READ, 3194 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 3195 ZIO_FLAG_RAW, &zio->io_bookmark)); 3196 3197 if (error == 0) { 3198 if (abd_cmp(tmpabd, zio->io_abd) != 0) 3199 error = SET_ERROR(ENOENT); 3200 } 3201 3202 abd_free(tmpabd); 3203 ddt_enter(ddt); 3204 return (error != 0); 3205 } else if (ddp->ddp_phys_birth != 0) { 3206 arc_buf_t *abuf = NULL; 3207 arc_flags_t aflags = ARC_FLAG_WAIT; 3208 blkptr_t blk = *zio->io_bp; 3209 int error; 3210 3211 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth); 3212 3213 if (BP_GET_LSIZE(&blk) != zio->io_orig_size) 3214 return (B_TRUE); 3215 3216 ddt_exit(ddt); 3217 3218 error = arc_read(NULL, spa, &blk, 3219 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ, 3220 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 3221 &aflags, &zio->io_bookmark); 3222 3223 if (error == 0) { 3224 if (abd_cmp_buf(zio->io_orig_abd, abuf->b_data, 3225 zio->io_orig_size) != 0) 3226 error = SET_ERROR(ENOENT); 3227 arc_buf_destroy(abuf, &abuf); 3228 } 3229 3230 ddt_enter(ddt); 3231 return (error != 0); 3232 } 3233 } 3234 3235 return (B_FALSE); 3236 } 3237 3238 static void 3239 zio_ddt_child_write_ready(zio_t *zio) 3240 { 3241 int p = zio->io_prop.zp_copies; 3242 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp); 3243 ddt_entry_t *dde = zio->io_private; 3244 ddt_phys_t *ddp = &dde->dde_phys[p]; 3245 zio_t *pio; 3246 3247 if (zio->io_error) 3248 return; 3249 3250 ddt_enter(ddt); 3251 3252 ASSERT(dde->dde_lead_zio[p] == zio); 3253 3254 ddt_phys_fill(ddp, zio->io_bp); 3255 3256 zio_link_t *zl = NULL; 3257 while ((pio = zio_walk_parents(zio, &zl)) != NULL) 3258 ddt_bp_fill(ddp, pio->io_bp, zio->io_txg); 3259 3260 ddt_exit(ddt); 3261 } 3262 3263 static void 3264 zio_ddt_child_write_done(zio_t *zio) 3265 { 3266 int p = zio->io_prop.zp_copies; 3267 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp); 3268 ddt_entry_t *dde = zio->io_private; 3269 ddt_phys_t *ddp = &dde->dde_phys[p]; 3270 3271 ddt_enter(ddt); 3272 3273 ASSERT(ddp->ddp_refcnt == 0); 3274 ASSERT(dde->dde_lead_zio[p] == zio); 3275 dde->dde_lead_zio[p] = NULL; 3276 3277 if (zio->io_error == 0) { 3278 zio_link_t *zl = NULL; 3279 while (zio_walk_parents(zio, &zl) != NULL) 3280 ddt_phys_addref(ddp); 3281 } else { 3282 ddt_phys_clear(ddp); 3283 } 3284 3285 ddt_exit(ddt); 3286 } 3287 3288 static zio_t * 3289 zio_ddt_write(zio_t *zio) 3290 { 3291 spa_t *spa = zio->io_spa; 3292 blkptr_t *bp = zio->io_bp; 3293 uint64_t txg = zio->io_txg; 3294 zio_prop_t *zp = &zio->io_prop; 3295 int p = zp->zp_copies; 3296 zio_t *cio = NULL; 3297 ddt_t *ddt = ddt_select(spa, bp); 3298 ddt_entry_t *dde; 3299 ddt_phys_t *ddp; 3300 3301 ASSERT(BP_GET_DEDUP(bp)); 3302 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum); 3303 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override); 3304 ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW))); 3305 3306 ddt_enter(ddt); 3307 dde = ddt_lookup(ddt, bp, B_TRUE); 3308 ddp = &dde->dde_phys[p]; 3309 3310 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) { 3311 /* 3312 * If we're using a weak checksum, upgrade to a strong checksum 3313 * and try again. If we're already using a strong checksum, 3314 * we can't resolve it, so just convert to an ordinary write. 3315 * (And automatically e-mail a paper to Nature?) 3316 */ 3317 if (!(zio_checksum_table[zp->zp_checksum].ci_flags & 3318 ZCHECKSUM_FLAG_DEDUP)) { 3319 zp->zp_checksum = spa_dedup_checksum(spa); 3320 zio_pop_transforms(zio); 3321 zio->io_stage = ZIO_STAGE_OPEN; 3322 BP_ZERO(bp); 3323 } else { 3324 zp->zp_dedup = B_FALSE; 3325 BP_SET_DEDUP(bp, B_FALSE); 3326 } 3327 ASSERT(!BP_GET_DEDUP(bp)); 3328 zio->io_pipeline = ZIO_WRITE_PIPELINE; 3329 ddt_exit(ddt); 3330 return (zio); 3331 } 3332 3333 if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) { 3334 if (ddp->ddp_phys_birth != 0) 3335 ddt_bp_fill(ddp, bp, txg); 3336 if (dde->dde_lead_zio[p] != NULL) 3337 zio_add_child(zio, dde->dde_lead_zio[p]); 3338 else 3339 ddt_phys_addref(ddp); 3340 } else if (zio->io_bp_override) { 3341 ASSERT(bp->blk_birth == txg); 3342 ASSERT(BP_EQUAL(bp, zio->io_bp_override)); 3343 ddt_phys_fill(ddp, bp); 3344 ddt_phys_addref(ddp); 3345 } else { 3346 cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd, 3347 zio->io_orig_size, zio->io_orig_size, zp, 3348 zio_ddt_child_write_ready, NULL, NULL, 3349 zio_ddt_child_write_done, dde, zio->io_priority, 3350 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark); 3351 3352 zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL); 3353 dde->dde_lead_zio[p] = cio; 3354 } 3355 3356 ddt_exit(ddt); 3357 3358 zio_nowait(cio); 3359 3360 return (zio); 3361 } 3362 3363 ddt_entry_t *freedde; /* for debugging */ 3364 3365 static zio_t * 3366 zio_ddt_free(zio_t *zio) 3367 { 3368 spa_t *spa = zio->io_spa; 3369 blkptr_t *bp = zio->io_bp; 3370 ddt_t *ddt = ddt_select(spa, bp); 3371 ddt_entry_t *dde; 3372 ddt_phys_t *ddp; 3373 3374 ASSERT(BP_GET_DEDUP(bp)); 3375 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 3376 3377 ddt_enter(ddt); 3378 freedde = dde = ddt_lookup(ddt, bp, B_TRUE); 3379 if (dde) { 3380 ddp = ddt_phys_select(dde, bp); 3381 if (ddp) 3382 ddt_phys_decref(ddp); 3383 } 3384 ddt_exit(ddt); 3385 3386 return (zio); 3387 } 3388 3389 /* 3390 * ========================================================================== 3391 * Allocate and free blocks 3392 * ========================================================================== 3393 */ 3394 3395 static zio_t * 3396 zio_io_to_allocate(spa_t *spa, int allocator) 3397 { 3398 zio_t *zio; 3399 3400 ASSERT(MUTEX_HELD(&spa->spa_allocs[allocator].spaa_lock)); 3401 3402 zio = avl_first(&spa->spa_allocs[allocator].spaa_tree); 3403 if (zio == NULL) 3404 return (NULL); 3405 3406 ASSERT(IO_IS_ALLOCATING(zio)); 3407 3408 /* 3409 * Try to place a reservation for this zio. If we're unable to 3410 * reserve then we throttle. 3411 */ 3412 ASSERT3U(zio->io_allocator, ==, allocator); 3413 if (!metaslab_class_throttle_reserve(zio->io_metaslab_class, 3414 zio->io_prop.zp_copies, allocator, zio, 0)) { 3415 return (NULL); 3416 } 3417 3418 avl_remove(&spa->spa_allocs[allocator].spaa_tree, zio); 3419 ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE); 3420 3421 return (zio); 3422 } 3423 3424 static zio_t * 3425 zio_dva_throttle(zio_t *zio) 3426 { 3427 spa_t *spa = zio->io_spa; 3428 zio_t *nio; 3429 metaslab_class_t *mc; 3430 3431 /* locate an appropriate allocation class */ 3432 mc = spa_preferred_class(spa, zio->io_size, zio->io_prop.zp_type, 3433 zio->io_prop.zp_level, zio->io_prop.zp_zpl_smallblk); 3434 3435 if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE || 3436 !mc->mc_alloc_throttle_enabled || 3437 zio->io_child_type == ZIO_CHILD_GANG || 3438 zio->io_flags & ZIO_FLAG_NODATA) { 3439 return (zio); 3440 } 3441 3442 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 3443 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 3444 ASSERT3U(zio->io_queued_timestamp, >, 0); 3445 ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE); 3446 3447 zbookmark_phys_t *bm = &zio->io_bookmark; 3448 /* 3449 * We want to try to use as many allocators as possible to help improve 3450 * performance, but we also want logically adjacent IOs to be physically 3451 * adjacent to improve sequential read performance. We chunk each object 3452 * into 2^20 block regions, and then hash based on the objset, object, 3453 * level, and region to accomplish both of these goals. 3454 */ 3455 int allocator = (uint_t)cityhash4(bm->zb_objset, bm->zb_object, 3456 bm->zb_level, bm->zb_blkid >> 20) % spa->spa_alloc_count; 3457 zio->io_allocator = allocator; 3458 zio->io_metaslab_class = mc; 3459 mutex_enter(&spa->spa_allocs[allocator].spaa_lock); 3460 avl_add(&spa->spa_allocs[allocator].spaa_tree, zio); 3461 nio = zio_io_to_allocate(spa, allocator); 3462 mutex_exit(&spa->spa_allocs[allocator].spaa_lock); 3463 return (nio); 3464 } 3465 3466 static void 3467 zio_allocate_dispatch(spa_t *spa, int allocator) 3468 { 3469 zio_t *zio; 3470 3471 mutex_enter(&spa->spa_allocs[allocator].spaa_lock); 3472 zio = zio_io_to_allocate(spa, allocator); 3473 mutex_exit(&spa->spa_allocs[allocator].spaa_lock); 3474 if (zio == NULL) 3475 return; 3476 3477 ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE); 3478 ASSERT0(zio->io_error); 3479 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE); 3480 } 3481 3482 static zio_t * 3483 zio_dva_allocate(zio_t *zio) 3484 { 3485 spa_t *spa = zio->io_spa; 3486 metaslab_class_t *mc; 3487 blkptr_t *bp = zio->io_bp; 3488 int error; 3489 int flags = 0; 3490 3491 if (zio->io_gang_leader == NULL) { 3492 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 3493 zio->io_gang_leader = zio; 3494 } 3495 3496 ASSERT(BP_IS_HOLE(bp)); 3497 ASSERT0(BP_GET_NDVAS(bp)); 3498 ASSERT3U(zio->io_prop.zp_copies, >, 0); 3499 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa)); 3500 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp)); 3501 3502 flags |= (zio->io_flags & ZIO_FLAG_FASTWRITE) ? METASLAB_FASTWRITE : 0; 3503 if (zio->io_flags & ZIO_FLAG_NODATA) 3504 flags |= METASLAB_DONT_THROTTLE; 3505 if (zio->io_flags & ZIO_FLAG_GANG_CHILD) 3506 flags |= METASLAB_GANG_CHILD; 3507 if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE) 3508 flags |= METASLAB_ASYNC_ALLOC; 3509 3510 /* 3511 * if not already chosen, locate an appropriate allocation class 3512 */ 3513 mc = zio->io_metaslab_class; 3514 if (mc == NULL) { 3515 mc = spa_preferred_class(spa, zio->io_size, 3516 zio->io_prop.zp_type, zio->io_prop.zp_level, 3517 zio->io_prop.zp_zpl_smallblk); 3518 zio->io_metaslab_class = mc; 3519 } 3520 3521 /* 3522 * Try allocating the block in the usual metaslab class. 3523 * If that's full, allocate it in the normal class. 3524 * If that's full, allocate as a gang block, 3525 * and if all are full, the allocation fails (which shouldn't happen). 3526 * 3527 * Note that we do not fall back on embedded slog (ZIL) space, to 3528 * preserve unfragmented slog space, which is critical for decent 3529 * sync write performance. If a log allocation fails, we will fall 3530 * back to spa_sync() which is abysmal for performance. 3531 */ 3532 error = metaslab_alloc(spa, mc, zio->io_size, bp, 3533 zio->io_prop.zp_copies, zio->io_txg, NULL, flags, 3534 &zio->io_alloc_list, zio, zio->io_allocator); 3535 3536 /* 3537 * Fallback to normal class when an alloc class is full 3538 */ 3539 if (error == ENOSPC && mc != spa_normal_class(spa)) { 3540 /* 3541 * If throttling, transfer reservation over to normal class. 3542 * The io_allocator slot can remain the same even though we 3543 * are switching classes. 3544 */ 3545 if (mc->mc_alloc_throttle_enabled && 3546 (zio->io_flags & ZIO_FLAG_IO_ALLOCATING)) { 3547 metaslab_class_throttle_unreserve(mc, 3548 zio->io_prop.zp_copies, zio->io_allocator, zio); 3549 zio->io_flags &= ~ZIO_FLAG_IO_ALLOCATING; 3550 3551 VERIFY(metaslab_class_throttle_reserve( 3552 spa_normal_class(spa), 3553 zio->io_prop.zp_copies, zio->io_allocator, zio, 3554 flags | METASLAB_MUST_RESERVE)); 3555 } 3556 zio->io_metaslab_class = mc = spa_normal_class(spa); 3557 if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) { 3558 zfs_dbgmsg("%s: metaslab allocation failure, " 3559 "trying normal class: zio %px, size %llu, error %d", 3560 spa_name(spa), zio, (u_longlong_t)zio->io_size, 3561 error); 3562 } 3563 3564 error = metaslab_alloc(spa, mc, zio->io_size, bp, 3565 zio->io_prop.zp_copies, zio->io_txg, NULL, flags, 3566 &zio->io_alloc_list, zio, zio->io_allocator); 3567 } 3568 3569 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE) { 3570 if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) { 3571 zfs_dbgmsg("%s: metaslab allocation failure, " 3572 "trying ganging: zio %px, size %llu, error %d", 3573 spa_name(spa), zio, (u_longlong_t)zio->io_size, 3574 error); 3575 } 3576 return (zio_write_gang_block(zio, mc)); 3577 } 3578 if (error != 0) { 3579 if (error != ENOSPC || 3580 (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC)) { 3581 zfs_dbgmsg("%s: metaslab allocation failure: zio %px, " 3582 "size %llu, error %d", 3583 spa_name(spa), zio, (u_longlong_t)zio->io_size, 3584 error); 3585 } 3586 zio->io_error = error; 3587 } 3588 3589 return (zio); 3590 } 3591 3592 static zio_t * 3593 zio_dva_free(zio_t *zio) 3594 { 3595 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE); 3596 3597 return (zio); 3598 } 3599 3600 static zio_t * 3601 zio_dva_claim(zio_t *zio) 3602 { 3603 int error; 3604 3605 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg); 3606 if (error) 3607 zio->io_error = error; 3608 3609 return (zio); 3610 } 3611 3612 /* 3613 * Undo an allocation. This is used by zio_done() when an I/O fails 3614 * and we want to give back the block we just allocated. 3615 * This handles both normal blocks and gang blocks. 3616 */ 3617 static void 3618 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp) 3619 { 3620 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp)); 3621 ASSERT(zio->io_bp_override == NULL); 3622 3623 if (!BP_IS_HOLE(bp)) 3624 metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE); 3625 3626 if (gn != NULL) { 3627 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 3628 zio_dva_unallocate(zio, gn->gn_child[g], 3629 &gn->gn_gbh->zg_blkptr[g]); 3630 } 3631 } 3632 } 3633 3634 /* 3635 * Try to allocate an intent log block. Return 0 on success, errno on failure. 3636 */ 3637 int 3638 zio_alloc_zil(spa_t *spa, objset_t *os, uint64_t txg, blkptr_t *new_bp, 3639 uint64_t size, boolean_t *slog) 3640 { 3641 int error = 1; 3642 zio_alloc_list_t io_alloc_list; 3643 3644 ASSERT(txg > spa_syncing_txg(spa)); 3645 3646 metaslab_trace_init(&io_alloc_list); 3647 3648 /* 3649 * Block pointer fields are useful to metaslabs for stats and debugging. 3650 * Fill in the obvious ones before calling into metaslab_alloc(). 3651 */ 3652 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG); 3653 BP_SET_PSIZE(new_bp, size); 3654 BP_SET_LEVEL(new_bp, 0); 3655 3656 /* 3657 * When allocating a zil block, we don't have information about 3658 * the final destination of the block except the objset it's part 3659 * of, so we just hash the objset ID to pick the allocator to get 3660 * some parallelism. 3661 */ 3662 int flags = METASLAB_FASTWRITE | METASLAB_ZIL; 3663 int allocator = (uint_t)cityhash4(0, 0, 0, 3664 os->os_dsl_dataset->ds_object) % spa->spa_alloc_count; 3665 error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1, 3666 txg, NULL, flags, &io_alloc_list, NULL, allocator); 3667 *slog = (error == 0); 3668 if (error != 0) { 3669 error = metaslab_alloc(spa, spa_embedded_log_class(spa), size, 3670 new_bp, 1, txg, NULL, flags, 3671 &io_alloc_list, NULL, allocator); 3672 } 3673 if (error != 0) { 3674 error = metaslab_alloc(spa, spa_normal_class(spa), size, 3675 new_bp, 1, txg, NULL, flags, 3676 &io_alloc_list, NULL, allocator); 3677 } 3678 metaslab_trace_fini(&io_alloc_list); 3679 3680 if (error == 0) { 3681 BP_SET_LSIZE(new_bp, size); 3682 BP_SET_PSIZE(new_bp, size); 3683 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF); 3684 BP_SET_CHECKSUM(new_bp, 3685 spa_version(spa) >= SPA_VERSION_SLIM_ZIL 3686 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG); 3687 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG); 3688 BP_SET_LEVEL(new_bp, 0); 3689 BP_SET_DEDUP(new_bp, 0); 3690 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER); 3691 3692 /* 3693 * encrypted blocks will require an IV and salt. We generate 3694 * these now since we will not be rewriting the bp at 3695 * rewrite time. 3696 */ 3697 if (os->os_encrypted) { 3698 uint8_t iv[ZIO_DATA_IV_LEN]; 3699 uint8_t salt[ZIO_DATA_SALT_LEN]; 3700 3701 BP_SET_CRYPT(new_bp, B_TRUE); 3702 VERIFY0(spa_crypt_get_salt(spa, 3703 dmu_objset_id(os), salt)); 3704 VERIFY0(zio_crypt_generate_iv(iv)); 3705 3706 zio_crypt_encode_params_bp(new_bp, salt, iv); 3707 } 3708 } else { 3709 zfs_dbgmsg("%s: zil block allocation failure: " 3710 "size %llu, error %d", spa_name(spa), (u_longlong_t)size, 3711 error); 3712 } 3713 3714 return (error); 3715 } 3716 3717 /* 3718 * ========================================================================== 3719 * Read and write to physical devices 3720 * ========================================================================== 3721 */ 3722 3723 /* 3724 * Issue an I/O to the underlying vdev. Typically the issue pipeline 3725 * stops after this stage and will resume upon I/O completion. 3726 * However, there are instances where the vdev layer may need to 3727 * continue the pipeline when an I/O was not issued. Since the I/O 3728 * that was sent to the vdev layer might be different than the one 3729 * currently active in the pipeline (see vdev_queue_io()), we explicitly 3730 * force the underlying vdev layers to call either zio_execute() or 3731 * zio_interrupt() to ensure that the pipeline continues with the correct I/O. 3732 */ 3733 static zio_t * 3734 zio_vdev_io_start(zio_t *zio) 3735 { 3736 vdev_t *vd = zio->io_vd; 3737 uint64_t align; 3738 spa_t *spa = zio->io_spa; 3739 3740 zio->io_delay = 0; 3741 3742 ASSERT(zio->io_error == 0); 3743 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0); 3744 3745 if (vd == NULL) { 3746 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER)) 3747 spa_config_enter(spa, SCL_ZIO, zio, RW_READER); 3748 3749 /* 3750 * The mirror_ops handle multiple DVAs in a single BP. 3751 */ 3752 vdev_mirror_ops.vdev_op_io_start(zio); 3753 return (NULL); 3754 } 3755 3756 ASSERT3P(zio->io_logical, !=, zio); 3757 if (zio->io_type == ZIO_TYPE_WRITE) { 3758 ASSERT(spa->spa_trust_config); 3759 3760 /* 3761 * Note: the code can handle other kinds of writes, 3762 * but we don't expect them. 3763 */ 3764 if (zio->io_vd->vdev_noalloc) { 3765 ASSERT(zio->io_flags & 3766 (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL | 3767 ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE)); 3768 } 3769 } 3770 3771 align = 1ULL << vd->vdev_top->vdev_ashift; 3772 3773 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) && 3774 P2PHASE(zio->io_size, align) != 0) { 3775 /* Transform logical writes to be a full physical block size. */ 3776 uint64_t asize = P2ROUNDUP(zio->io_size, align); 3777 abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize); 3778 ASSERT(vd == vd->vdev_top); 3779 if (zio->io_type == ZIO_TYPE_WRITE) { 3780 abd_copy(abuf, zio->io_abd, zio->io_size); 3781 abd_zero_off(abuf, zio->io_size, asize - zio->io_size); 3782 } 3783 zio_push_transform(zio, abuf, asize, asize, zio_subblock); 3784 } 3785 3786 /* 3787 * If this is not a physical io, make sure that it is properly aligned 3788 * before proceeding. 3789 */ 3790 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) { 3791 ASSERT0(P2PHASE(zio->io_offset, align)); 3792 ASSERT0(P2PHASE(zio->io_size, align)); 3793 } else { 3794 /* 3795 * For physical writes, we allow 512b aligned writes and assume 3796 * the device will perform a read-modify-write as necessary. 3797 */ 3798 ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE)); 3799 ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE)); 3800 } 3801 3802 VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa)); 3803 3804 /* 3805 * If this is a repair I/O, and there's no self-healing involved -- 3806 * that is, we're just resilvering what we expect to resilver -- 3807 * then don't do the I/O unless zio's txg is actually in vd's DTL. 3808 * This prevents spurious resilvering. 3809 * 3810 * There are a few ways that we can end up creating these spurious 3811 * resilver i/os: 3812 * 3813 * 1. A resilver i/o will be issued if any DVA in the BP has a 3814 * dirty DTL. The mirror code will issue resilver writes to 3815 * each DVA, including the one(s) that are not on vdevs with dirty 3816 * DTLs. 3817 * 3818 * 2. With nested replication, which happens when we have a 3819 * "replacing" or "spare" vdev that's a child of a mirror or raidz. 3820 * For example, given mirror(replacing(A+B), C), it's likely that 3821 * only A is out of date (it's the new device). In this case, we'll 3822 * read from C, then use the data to resilver A+B -- but we don't 3823 * actually want to resilver B, just A. The top-level mirror has no 3824 * way to know this, so instead we just discard unnecessary repairs 3825 * as we work our way down the vdev tree. 3826 * 3827 * 3. ZTEST also creates mirrors of mirrors, mirrors of raidz, etc. 3828 * The same logic applies to any form of nested replication: ditto 3829 * + mirror, RAID-Z + replacing, etc. 3830 * 3831 * However, indirect vdevs point off to other vdevs which may have 3832 * DTL's, so we never bypass them. The child i/os on concrete vdevs 3833 * will be properly bypassed instead. 3834 * 3835 * Leaf DTL_PARTIAL can be empty when a legitimate write comes from 3836 * a dRAID spare vdev. For example, when a dRAID spare is first 3837 * used, its spare blocks need to be written to but the leaf vdev's 3838 * of such blocks can have empty DTL_PARTIAL. 3839 * 3840 * There seemed no clean way to allow such writes while bypassing 3841 * spurious ones. At this point, just avoid all bypassing for dRAID 3842 * for correctness. 3843 */ 3844 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) && 3845 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) && 3846 zio->io_txg != 0 && /* not a delegated i/o */ 3847 vd->vdev_ops != &vdev_indirect_ops && 3848 vd->vdev_top->vdev_ops != &vdev_draid_ops && 3849 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) { 3850 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 3851 zio_vdev_io_bypass(zio); 3852 return (zio); 3853 } 3854 3855 /* 3856 * Select the next best leaf I/O to process. Distributed spares are 3857 * excluded since they dispatch the I/O directly to a leaf vdev after 3858 * applying the dRAID mapping. 3859 */ 3860 if (vd->vdev_ops->vdev_op_leaf && 3861 vd->vdev_ops != &vdev_draid_spare_ops && 3862 (zio->io_type == ZIO_TYPE_READ || 3863 zio->io_type == ZIO_TYPE_WRITE || 3864 zio->io_type == ZIO_TYPE_TRIM)) { 3865 3866 if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio)) 3867 return (zio); 3868 3869 if ((zio = vdev_queue_io(zio)) == NULL) 3870 return (NULL); 3871 3872 if (!vdev_accessible(vd, zio)) { 3873 zio->io_error = SET_ERROR(ENXIO); 3874 zio_interrupt(zio); 3875 return (NULL); 3876 } 3877 zio->io_delay = gethrtime(); 3878 } 3879 3880 vd->vdev_ops->vdev_op_io_start(zio); 3881 return (NULL); 3882 } 3883 3884 static zio_t * 3885 zio_vdev_io_done(zio_t *zio) 3886 { 3887 vdev_t *vd = zio->io_vd; 3888 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops; 3889 boolean_t unexpected_error = B_FALSE; 3890 3891 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) { 3892 return (NULL); 3893 } 3894 3895 ASSERT(zio->io_type == ZIO_TYPE_READ || 3896 zio->io_type == ZIO_TYPE_WRITE || zio->io_type == ZIO_TYPE_TRIM); 3897 3898 if (zio->io_delay) 3899 zio->io_delay = gethrtime() - zio->io_delay; 3900 3901 if (vd != NULL && vd->vdev_ops->vdev_op_leaf && 3902 vd->vdev_ops != &vdev_draid_spare_ops) { 3903 vdev_queue_io_done(zio); 3904 3905 if (zio->io_type == ZIO_TYPE_WRITE) 3906 vdev_cache_write(zio); 3907 3908 if (zio_injection_enabled && zio->io_error == 0) 3909 zio->io_error = zio_handle_device_injections(vd, zio, 3910 EIO, EILSEQ); 3911 3912 if (zio_injection_enabled && zio->io_error == 0) 3913 zio->io_error = zio_handle_label_injection(zio, EIO); 3914 3915 if (zio->io_error && zio->io_type != ZIO_TYPE_TRIM) { 3916 if (!vdev_accessible(vd, zio)) { 3917 zio->io_error = SET_ERROR(ENXIO); 3918 } else { 3919 unexpected_error = B_TRUE; 3920 } 3921 } 3922 } 3923 3924 ops->vdev_op_io_done(zio); 3925 3926 if (unexpected_error && vd->vdev_remove_wanted == B_FALSE) 3927 VERIFY(vdev_probe(vd, zio) == NULL); 3928 3929 return (zio); 3930 } 3931 3932 /* 3933 * This function is used to change the priority of an existing zio that is 3934 * currently in-flight. This is used by the arc to upgrade priority in the 3935 * event that a demand read is made for a block that is currently queued 3936 * as a scrub or async read IO. Otherwise, the high priority read request 3937 * would end up having to wait for the lower priority IO. 3938 */ 3939 void 3940 zio_change_priority(zio_t *pio, zio_priority_t priority) 3941 { 3942 zio_t *cio, *cio_next; 3943 zio_link_t *zl = NULL; 3944 3945 ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 3946 3947 if (pio->io_vd != NULL && pio->io_vd->vdev_ops->vdev_op_leaf) { 3948 vdev_queue_change_io_priority(pio, priority); 3949 } else { 3950 pio->io_priority = priority; 3951 } 3952 3953 mutex_enter(&pio->io_lock); 3954 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) { 3955 cio_next = zio_walk_children(pio, &zl); 3956 zio_change_priority(cio, priority); 3957 } 3958 mutex_exit(&pio->io_lock); 3959 } 3960 3961 /* 3962 * For non-raidz ZIOs, we can just copy aside the bad data read from the 3963 * disk, and use that to finish the checksum ereport later. 3964 */ 3965 static void 3966 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr, 3967 const abd_t *good_buf) 3968 { 3969 /* no processing needed */ 3970 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE); 3971 } 3972 3973 void 3974 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr) 3975 { 3976 void *abd = abd_alloc_sametype(zio->io_abd, zio->io_size); 3977 3978 abd_copy(abd, zio->io_abd, zio->io_size); 3979 3980 zcr->zcr_cbinfo = zio->io_size; 3981 zcr->zcr_cbdata = abd; 3982 zcr->zcr_finish = zio_vsd_default_cksum_finish; 3983 zcr->zcr_free = zio_abd_free; 3984 } 3985 3986 static zio_t * 3987 zio_vdev_io_assess(zio_t *zio) 3988 { 3989 vdev_t *vd = zio->io_vd; 3990 3991 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) { 3992 return (NULL); 3993 } 3994 3995 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER)) 3996 spa_config_exit(zio->io_spa, SCL_ZIO, zio); 3997 3998 if (zio->io_vsd != NULL) { 3999 zio->io_vsd_ops->vsd_free(zio); 4000 zio->io_vsd = NULL; 4001 } 4002 4003 if (zio_injection_enabled && zio->io_error == 0) 4004 zio->io_error = zio_handle_fault_injection(zio, EIO); 4005 4006 /* 4007 * If the I/O failed, determine whether we should attempt to retry it. 4008 * 4009 * On retry, we cut in line in the issue queue, since we don't want 4010 * compression/checksumming/etc. work to prevent our (cheap) IO reissue. 4011 */ 4012 if (zio->io_error && vd == NULL && 4013 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) { 4014 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */ 4015 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */ 4016 zio->io_error = 0; 4017 zio->io_flags |= ZIO_FLAG_IO_RETRY | 4018 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE; 4019 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1; 4020 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, 4021 zio_requeue_io_start_cut_in_line); 4022 return (NULL); 4023 } 4024 4025 /* 4026 * If we got an error on a leaf device, convert it to ENXIO 4027 * if the device is not accessible at all. 4028 */ 4029 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf && 4030 !vdev_accessible(vd, zio)) 4031 zio->io_error = SET_ERROR(ENXIO); 4032 4033 /* 4034 * If we can't write to an interior vdev (mirror or RAID-Z), 4035 * set vdev_cant_write so that we stop trying to allocate from it. 4036 */ 4037 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE && 4038 vd != NULL && !vd->vdev_ops->vdev_op_leaf) { 4039 vdev_dbgmsg(vd, "zio_vdev_io_assess(zio=%px) setting " 4040 "cant_write=TRUE due to write failure with ENXIO", 4041 zio); 4042 vd->vdev_cant_write = B_TRUE; 4043 } 4044 4045 /* 4046 * If a cache flush returns ENOTSUP or ENOTTY, we know that no future 4047 * attempts will ever succeed. In this case we set a persistent 4048 * boolean flag so that we don't bother with it in the future. 4049 */ 4050 if ((zio->io_error == ENOTSUP || zio->io_error == ENOTTY) && 4051 zio->io_type == ZIO_TYPE_IOCTL && 4052 zio->io_cmd == DKIOCFLUSHWRITECACHE && vd != NULL) 4053 vd->vdev_nowritecache = B_TRUE; 4054 4055 if (zio->io_error) 4056 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 4057 4058 if (vd != NULL && vd->vdev_ops->vdev_op_leaf && 4059 zio->io_physdone != NULL) { 4060 ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED)); 4061 ASSERT(zio->io_child_type == ZIO_CHILD_VDEV); 4062 zio->io_physdone(zio->io_logical); 4063 } 4064 4065 return (zio); 4066 } 4067 4068 void 4069 zio_vdev_io_reissue(zio_t *zio) 4070 { 4071 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 4072 ASSERT(zio->io_error == 0); 4073 4074 zio->io_stage >>= 1; 4075 } 4076 4077 void 4078 zio_vdev_io_redone(zio_t *zio) 4079 { 4080 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE); 4081 4082 zio->io_stage >>= 1; 4083 } 4084 4085 void 4086 zio_vdev_io_bypass(zio_t *zio) 4087 { 4088 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 4089 ASSERT(zio->io_error == 0); 4090 4091 zio->io_flags |= ZIO_FLAG_IO_BYPASS; 4092 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1; 4093 } 4094 4095 /* 4096 * ========================================================================== 4097 * Encrypt and store encryption parameters 4098 * ========================================================================== 4099 */ 4100 4101 4102 /* 4103 * This function is used for ZIO_STAGE_ENCRYPT. It is responsible for 4104 * managing the storage of encryption parameters and passing them to the 4105 * lower-level encryption functions. 4106 */ 4107 static zio_t * 4108 zio_encrypt(zio_t *zio) 4109 { 4110 zio_prop_t *zp = &zio->io_prop; 4111 spa_t *spa = zio->io_spa; 4112 blkptr_t *bp = zio->io_bp; 4113 uint64_t psize = BP_GET_PSIZE(bp); 4114 uint64_t dsobj = zio->io_bookmark.zb_objset; 4115 dmu_object_type_t ot = BP_GET_TYPE(bp); 4116 void *enc_buf = NULL; 4117 abd_t *eabd = NULL; 4118 uint8_t salt[ZIO_DATA_SALT_LEN]; 4119 uint8_t iv[ZIO_DATA_IV_LEN]; 4120 uint8_t mac[ZIO_DATA_MAC_LEN]; 4121 boolean_t no_crypt = B_FALSE; 4122 4123 /* the root zio already encrypted the data */ 4124 if (zio->io_child_type == ZIO_CHILD_GANG) 4125 return (zio); 4126 4127 /* only ZIL blocks are re-encrypted on rewrite */ 4128 if (!IO_IS_ALLOCATING(zio) && ot != DMU_OT_INTENT_LOG) 4129 return (zio); 4130 4131 if (!(zp->zp_encrypt || BP_IS_ENCRYPTED(bp))) { 4132 BP_SET_CRYPT(bp, B_FALSE); 4133 return (zio); 4134 } 4135 4136 /* if we are doing raw encryption set the provided encryption params */ 4137 if (zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) { 4138 ASSERT0(BP_GET_LEVEL(bp)); 4139 BP_SET_CRYPT(bp, B_TRUE); 4140 BP_SET_BYTEORDER(bp, zp->zp_byteorder); 4141 if (ot != DMU_OT_OBJSET) 4142 zio_crypt_encode_mac_bp(bp, zp->zp_mac); 4143 4144 /* dnode blocks must be written out in the provided byteorder */ 4145 if (zp->zp_byteorder != ZFS_HOST_BYTEORDER && 4146 ot == DMU_OT_DNODE) { 4147 void *bswap_buf = zio_buf_alloc(psize); 4148 abd_t *babd = abd_get_from_buf(bswap_buf, psize); 4149 4150 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF); 4151 abd_copy_to_buf(bswap_buf, zio->io_abd, psize); 4152 dmu_ot_byteswap[DMU_OT_BYTESWAP(ot)].ob_func(bswap_buf, 4153 psize); 4154 4155 abd_take_ownership_of_buf(babd, B_TRUE); 4156 zio_push_transform(zio, babd, psize, psize, NULL); 4157 } 4158 4159 if (DMU_OT_IS_ENCRYPTED(ot)) 4160 zio_crypt_encode_params_bp(bp, zp->zp_salt, zp->zp_iv); 4161 return (zio); 4162 } 4163 4164 /* indirect blocks only maintain a cksum of the lower level MACs */ 4165 if (BP_GET_LEVEL(bp) > 0) { 4166 BP_SET_CRYPT(bp, B_TRUE); 4167 VERIFY0(zio_crypt_do_indirect_mac_checksum_abd(B_TRUE, 4168 zio->io_orig_abd, BP_GET_LSIZE(bp), BP_SHOULD_BYTESWAP(bp), 4169 mac)); 4170 zio_crypt_encode_mac_bp(bp, mac); 4171 return (zio); 4172 } 4173 4174 /* 4175 * Objset blocks are a special case since they have 2 256-bit MACs 4176 * embedded within them. 4177 */ 4178 if (ot == DMU_OT_OBJSET) { 4179 ASSERT0(DMU_OT_IS_ENCRYPTED(ot)); 4180 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF); 4181 BP_SET_CRYPT(bp, B_TRUE); 4182 VERIFY0(spa_do_crypt_objset_mac_abd(B_TRUE, spa, dsobj, 4183 zio->io_abd, psize, BP_SHOULD_BYTESWAP(bp))); 4184 return (zio); 4185 } 4186 4187 /* unencrypted object types are only authenticated with a MAC */ 4188 if (!DMU_OT_IS_ENCRYPTED(ot)) { 4189 BP_SET_CRYPT(bp, B_TRUE); 4190 VERIFY0(spa_do_crypt_mac_abd(B_TRUE, spa, dsobj, 4191 zio->io_abd, psize, mac)); 4192 zio_crypt_encode_mac_bp(bp, mac); 4193 return (zio); 4194 } 4195 4196 /* 4197 * Later passes of sync-to-convergence may decide to rewrite data 4198 * in place to avoid more disk reallocations. This presents a problem 4199 * for encryption because this constitutes rewriting the new data with 4200 * the same encryption key and IV. However, this only applies to blocks 4201 * in the MOS (particularly the spacemaps) and we do not encrypt the 4202 * MOS. We assert that the zio is allocating or an intent log write 4203 * to enforce this. 4204 */ 4205 ASSERT(IO_IS_ALLOCATING(zio) || ot == DMU_OT_INTENT_LOG); 4206 ASSERT(BP_GET_LEVEL(bp) == 0 || ot == DMU_OT_INTENT_LOG); 4207 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION)); 4208 ASSERT3U(psize, !=, 0); 4209 4210 enc_buf = zio_buf_alloc(psize); 4211 eabd = abd_get_from_buf(enc_buf, psize); 4212 abd_take_ownership_of_buf(eabd, B_TRUE); 4213 4214 /* 4215 * For an explanation of what encryption parameters are stored 4216 * where, see the block comment in zio_crypt.c. 4217 */ 4218 if (ot == DMU_OT_INTENT_LOG) { 4219 zio_crypt_decode_params_bp(bp, salt, iv); 4220 } else { 4221 BP_SET_CRYPT(bp, B_TRUE); 4222 } 4223 4224 /* Perform the encryption. This should not fail */ 4225 VERIFY0(spa_do_crypt_abd(B_TRUE, spa, &zio->io_bookmark, 4226 BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), 4227 salt, iv, mac, psize, zio->io_abd, eabd, &no_crypt)); 4228 4229 /* encode encryption metadata into the bp */ 4230 if (ot == DMU_OT_INTENT_LOG) { 4231 /* 4232 * ZIL blocks store the MAC in the embedded checksum, so the 4233 * transform must always be applied. 4234 */ 4235 zio_crypt_encode_mac_zil(enc_buf, mac); 4236 zio_push_transform(zio, eabd, psize, psize, NULL); 4237 } else { 4238 BP_SET_CRYPT(bp, B_TRUE); 4239 zio_crypt_encode_params_bp(bp, salt, iv); 4240 zio_crypt_encode_mac_bp(bp, mac); 4241 4242 if (no_crypt) { 4243 ASSERT3U(ot, ==, DMU_OT_DNODE); 4244 abd_free(eabd); 4245 } else { 4246 zio_push_transform(zio, eabd, psize, psize, NULL); 4247 } 4248 } 4249 4250 return (zio); 4251 } 4252 4253 /* 4254 * ========================================================================== 4255 * Generate and verify checksums 4256 * ========================================================================== 4257 */ 4258 static zio_t * 4259 zio_checksum_generate(zio_t *zio) 4260 { 4261 blkptr_t *bp = zio->io_bp; 4262 enum zio_checksum checksum; 4263 4264 if (bp == NULL) { 4265 /* 4266 * This is zio_write_phys(). 4267 * We're either generating a label checksum, or none at all. 4268 */ 4269 checksum = zio->io_prop.zp_checksum; 4270 4271 if (checksum == ZIO_CHECKSUM_OFF) 4272 return (zio); 4273 4274 ASSERT(checksum == ZIO_CHECKSUM_LABEL); 4275 } else { 4276 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) { 4277 ASSERT(!IO_IS_ALLOCATING(zio)); 4278 checksum = ZIO_CHECKSUM_GANG_HEADER; 4279 } else { 4280 checksum = BP_GET_CHECKSUM(bp); 4281 } 4282 } 4283 4284 zio_checksum_compute(zio, checksum, zio->io_abd, zio->io_size); 4285 4286 return (zio); 4287 } 4288 4289 static zio_t * 4290 zio_checksum_verify(zio_t *zio) 4291 { 4292 zio_bad_cksum_t info; 4293 blkptr_t *bp = zio->io_bp; 4294 int error; 4295 4296 ASSERT(zio->io_vd != NULL); 4297 4298 if (bp == NULL) { 4299 /* 4300 * This is zio_read_phys(). 4301 * We're either verifying a label checksum, or nothing at all. 4302 */ 4303 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF) 4304 return (zio); 4305 4306 ASSERT3U(zio->io_prop.zp_checksum, ==, ZIO_CHECKSUM_LABEL); 4307 } 4308 4309 if ((error = zio_checksum_error(zio, &info)) != 0) { 4310 zio->io_error = error; 4311 if (error == ECKSUM && 4312 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 4313 (void) zfs_ereport_start_checksum(zio->io_spa, 4314 zio->io_vd, &zio->io_bookmark, zio, 4315 zio->io_offset, zio->io_size, &info); 4316 mutex_enter(&zio->io_vd->vdev_stat_lock); 4317 zio->io_vd->vdev_stat.vs_checksum_errors++; 4318 mutex_exit(&zio->io_vd->vdev_stat_lock); 4319 } 4320 } 4321 4322 return (zio); 4323 } 4324 4325 /* 4326 * Called by RAID-Z to ensure we don't compute the checksum twice. 4327 */ 4328 void 4329 zio_checksum_verified(zio_t *zio) 4330 { 4331 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY; 4332 } 4333 4334 /* 4335 * ========================================================================== 4336 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other. 4337 * An error of 0 indicates success. ENXIO indicates whole-device failure, 4338 * which may be transient (e.g. unplugged) or permanent. ECKSUM and EIO 4339 * indicate errors that are specific to one I/O, and most likely permanent. 4340 * Any other error is presumed to be worse because we weren't expecting it. 4341 * ========================================================================== 4342 */ 4343 int 4344 zio_worst_error(int e1, int e2) 4345 { 4346 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO }; 4347 int r1, r2; 4348 4349 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++) 4350 if (e1 == zio_error_rank[r1]) 4351 break; 4352 4353 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++) 4354 if (e2 == zio_error_rank[r2]) 4355 break; 4356 4357 return (r1 > r2 ? e1 : e2); 4358 } 4359 4360 /* 4361 * ========================================================================== 4362 * I/O completion 4363 * ========================================================================== 4364 */ 4365 static zio_t * 4366 zio_ready(zio_t *zio) 4367 { 4368 blkptr_t *bp = zio->io_bp; 4369 zio_t *pio, *pio_next; 4370 zio_link_t *zl = NULL; 4371 4372 if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT | ZIO_CHILD_DDT_BIT, 4373 ZIO_WAIT_READY)) { 4374 return (NULL); 4375 } 4376 4377 if (zio->io_ready) { 4378 ASSERT(IO_IS_ALLOCATING(zio)); 4379 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) || 4380 (zio->io_flags & ZIO_FLAG_NOPWRITE)); 4381 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0); 4382 4383 zio->io_ready(zio); 4384 } 4385 4386 if (bp != NULL && bp != &zio->io_bp_copy) 4387 zio->io_bp_copy = *bp; 4388 4389 if (zio->io_error != 0) { 4390 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 4391 4392 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) { 4393 ASSERT(IO_IS_ALLOCATING(zio)); 4394 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE); 4395 ASSERT(zio->io_metaslab_class != NULL); 4396 4397 /* 4398 * We were unable to allocate anything, unreserve and 4399 * issue the next I/O to allocate. 4400 */ 4401 metaslab_class_throttle_unreserve( 4402 zio->io_metaslab_class, zio->io_prop.zp_copies, 4403 zio->io_allocator, zio); 4404 zio_allocate_dispatch(zio->io_spa, zio->io_allocator); 4405 } 4406 } 4407 4408 mutex_enter(&zio->io_lock); 4409 zio->io_state[ZIO_WAIT_READY] = 1; 4410 pio = zio_walk_parents(zio, &zl); 4411 mutex_exit(&zio->io_lock); 4412 4413 /* 4414 * As we notify zio's parents, new parents could be added. 4415 * New parents go to the head of zio's io_parent_list, however, 4416 * so we will (correctly) not notify them. The remainder of zio's 4417 * io_parent_list, from 'pio_next' onward, cannot change because 4418 * all parents must wait for us to be done before they can be done. 4419 */ 4420 for (; pio != NULL; pio = pio_next) { 4421 pio_next = zio_walk_parents(zio, &zl); 4422 zio_notify_parent(pio, zio, ZIO_WAIT_READY, NULL); 4423 } 4424 4425 if (zio->io_flags & ZIO_FLAG_NODATA) { 4426 if (BP_IS_GANG(bp)) { 4427 zio->io_flags &= ~ZIO_FLAG_NODATA; 4428 } else { 4429 ASSERT((uintptr_t)zio->io_abd < SPA_MAXBLOCKSIZE); 4430 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES; 4431 } 4432 } 4433 4434 if (zio_injection_enabled && 4435 zio->io_spa->spa_syncing_txg == zio->io_txg) 4436 zio_handle_ignored_writes(zio); 4437 4438 return (zio); 4439 } 4440 4441 /* 4442 * Update the allocation throttle accounting. 4443 */ 4444 static void 4445 zio_dva_throttle_done(zio_t *zio) 4446 { 4447 zio_t *lio __maybe_unused = zio->io_logical; 4448 zio_t *pio = zio_unique_parent(zio); 4449 vdev_t *vd = zio->io_vd; 4450 int flags = METASLAB_ASYNC_ALLOC; 4451 4452 ASSERT3P(zio->io_bp, !=, NULL); 4453 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE); 4454 ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE); 4455 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV); 4456 ASSERT(vd != NULL); 4457 ASSERT3P(vd, ==, vd->vdev_top); 4458 ASSERT(zio_injection_enabled || !(zio->io_flags & ZIO_FLAG_IO_RETRY)); 4459 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR)); 4460 ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING); 4461 ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE)); 4462 ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA)); 4463 4464 /* 4465 * Parents of gang children can have two flavors -- ones that 4466 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set) 4467 * and ones that allocated the constituent blocks. The allocation 4468 * throttle needs to know the allocating parent zio so we must find 4469 * it here. 4470 */ 4471 if (pio->io_child_type == ZIO_CHILD_GANG) { 4472 /* 4473 * If our parent is a rewrite gang child then our grandparent 4474 * would have been the one that performed the allocation. 4475 */ 4476 if (pio->io_flags & ZIO_FLAG_IO_REWRITE) 4477 pio = zio_unique_parent(pio); 4478 flags |= METASLAB_GANG_CHILD; 4479 } 4480 4481 ASSERT(IO_IS_ALLOCATING(pio)); 4482 ASSERT3P(zio, !=, zio->io_logical); 4483 ASSERT(zio->io_logical != NULL); 4484 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR)); 4485 ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE); 4486 ASSERT(zio->io_metaslab_class != NULL); 4487 4488 mutex_enter(&pio->io_lock); 4489 metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags, 4490 pio->io_allocator, B_TRUE); 4491 mutex_exit(&pio->io_lock); 4492 4493 metaslab_class_throttle_unreserve(zio->io_metaslab_class, 1, 4494 pio->io_allocator, pio); 4495 4496 /* 4497 * Call into the pipeline to see if there is more work that 4498 * needs to be done. If there is work to be done it will be 4499 * dispatched to another taskq thread. 4500 */ 4501 zio_allocate_dispatch(zio->io_spa, pio->io_allocator); 4502 } 4503 4504 static zio_t * 4505 zio_done(zio_t *zio) 4506 { 4507 /* 4508 * Always attempt to keep stack usage minimal here since 4509 * we can be called recursively up to 19 levels deep. 4510 */ 4511 const uint64_t psize = zio->io_size; 4512 zio_t *pio, *pio_next; 4513 zio_link_t *zl = NULL; 4514 4515 /* 4516 * If our children haven't all completed, 4517 * wait for them and then repeat this pipeline stage. 4518 */ 4519 if (zio_wait_for_children(zio, ZIO_CHILD_ALL_BITS, ZIO_WAIT_DONE)) { 4520 return (NULL); 4521 } 4522 4523 /* 4524 * If the allocation throttle is enabled, then update the accounting. 4525 * We only track child I/Os that are part of an allocating async 4526 * write. We must do this since the allocation is performed 4527 * by the logical I/O but the actual write is done by child I/Os. 4528 */ 4529 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING && 4530 zio->io_child_type == ZIO_CHILD_VDEV) { 4531 ASSERT(zio->io_metaslab_class != NULL); 4532 ASSERT(zio->io_metaslab_class->mc_alloc_throttle_enabled); 4533 zio_dva_throttle_done(zio); 4534 } 4535 4536 /* 4537 * If the allocation throttle is enabled, verify that 4538 * we have decremented the refcounts for every I/O that was throttled. 4539 */ 4540 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) { 4541 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 4542 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE); 4543 ASSERT(zio->io_bp != NULL); 4544 4545 metaslab_group_alloc_verify(zio->io_spa, zio->io_bp, zio, 4546 zio->io_allocator); 4547 VERIFY(zfs_refcount_not_held(&zio->io_metaslab_class-> 4548 mc_allocator[zio->io_allocator].mca_alloc_slots, zio)); 4549 } 4550 4551 4552 for (int c = 0; c < ZIO_CHILD_TYPES; c++) 4553 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 4554 ASSERT(zio->io_children[c][w] == 0); 4555 4556 if (zio->io_bp != NULL && !BP_IS_EMBEDDED(zio->io_bp)) { 4557 ASSERT(zio->io_bp->blk_pad[0] == 0); 4558 ASSERT(zio->io_bp->blk_pad[1] == 0); 4559 ASSERT(memcmp(zio->io_bp, &zio->io_bp_copy, 4560 sizeof (blkptr_t)) == 0 || 4561 (zio->io_bp == zio_unique_parent(zio)->io_bp)); 4562 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(zio->io_bp) && 4563 zio->io_bp_override == NULL && 4564 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) { 4565 ASSERT3U(zio->io_prop.zp_copies, <=, 4566 BP_GET_NDVAS(zio->io_bp)); 4567 ASSERT(BP_COUNT_GANG(zio->io_bp) == 0 || 4568 (BP_COUNT_GANG(zio->io_bp) == 4569 BP_GET_NDVAS(zio->io_bp))); 4570 } 4571 if (zio->io_flags & ZIO_FLAG_NOPWRITE) 4572 VERIFY(BP_EQUAL(zio->io_bp, &zio->io_bp_orig)); 4573 } 4574 4575 /* 4576 * If there were child vdev/gang/ddt errors, they apply to us now. 4577 */ 4578 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV); 4579 zio_inherit_child_errors(zio, ZIO_CHILD_GANG); 4580 zio_inherit_child_errors(zio, ZIO_CHILD_DDT); 4581 4582 /* 4583 * If the I/O on the transformed data was successful, generate any 4584 * checksum reports now while we still have the transformed data. 4585 */ 4586 if (zio->io_error == 0) { 4587 while (zio->io_cksum_report != NULL) { 4588 zio_cksum_report_t *zcr = zio->io_cksum_report; 4589 uint64_t align = zcr->zcr_align; 4590 uint64_t asize = P2ROUNDUP(psize, align); 4591 abd_t *adata = zio->io_abd; 4592 4593 if (adata != NULL && asize != psize) { 4594 adata = abd_alloc(asize, B_TRUE); 4595 abd_copy(adata, zio->io_abd, psize); 4596 abd_zero_off(adata, psize, asize - psize); 4597 } 4598 4599 zio->io_cksum_report = zcr->zcr_next; 4600 zcr->zcr_next = NULL; 4601 zcr->zcr_finish(zcr, adata); 4602 zfs_ereport_free_checksum(zcr); 4603 4604 if (adata != NULL && asize != psize) 4605 abd_free(adata); 4606 } 4607 } 4608 4609 zio_pop_transforms(zio); /* note: may set zio->io_error */ 4610 4611 vdev_stat_update(zio, psize); 4612 4613 /* 4614 * If this I/O is attached to a particular vdev is slow, exceeding 4615 * 30 seconds to complete, post an error described the I/O delay. 4616 * We ignore these errors if the device is currently unavailable. 4617 */ 4618 if (zio->io_delay >= MSEC2NSEC(zio_slow_io_ms)) { 4619 if (zio->io_vd != NULL && !vdev_is_dead(zio->io_vd)) { 4620 /* 4621 * We want to only increment our slow IO counters if 4622 * the IO is valid (i.e. not if the drive is removed). 4623 * 4624 * zfs_ereport_post() will also do these checks, but 4625 * it can also ratelimit and have other failures, so we 4626 * need to increment the slow_io counters independent 4627 * of it. 4628 */ 4629 if (zfs_ereport_is_valid(FM_EREPORT_ZFS_DELAY, 4630 zio->io_spa, zio->io_vd, zio)) { 4631 mutex_enter(&zio->io_vd->vdev_stat_lock); 4632 zio->io_vd->vdev_stat.vs_slow_ios++; 4633 mutex_exit(&zio->io_vd->vdev_stat_lock); 4634 4635 (void) zfs_ereport_post(FM_EREPORT_ZFS_DELAY, 4636 zio->io_spa, zio->io_vd, &zio->io_bookmark, 4637 zio, 0); 4638 } 4639 } 4640 } 4641 4642 if (zio->io_error) { 4643 /* 4644 * If this I/O is attached to a particular vdev, 4645 * generate an error message describing the I/O failure 4646 * at the block level. We ignore these errors if the 4647 * device is currently unavailable. 4648 */ 4649 if (zio->io_error != ECKSUM && zio->io_vd != NULL && 4650 !vdev_is_dead(zio->io_vd)) { 4651 int ret = zfs_ereport_post(FM_EREPORT_ZFS_IO, 4652 zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0); 4653 if (ret != EALREADY) { 4654 mutex_enter(&zio->io_vd->vdev_stat_lock); 4655 if (zio->io_type == ZIO_TYPE_READ) 4656 zio->io_vd->vdev_stat.vs_read_errors++; 4657 else if (zio->io_type == ZIO_TYPE_WRITE) 4658 zio->io_vd->vdev_stat.vs_write_errors++; 4659 mutex_exit(&zio->io_vd->vdev_stat_lock); 4660 } 4661 } 4662 4663 if ((zio->io_error == EIO || !(zio->io_flags & 4664 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) && 4665 zio == zio->io_logical) { 4666 /* 4667 * For logical I/O requests, tell the SPA to log the 4668 * error and generate a logical data ereport. 4669 */ 4670 spa_log_error(zio->io_spa, &zio->io_bookmark); 4671 (void) zfs_ereport_post(FM_EREPORT_ZFS_DATA, 4672 zio->io_spa, NULL, &zio->io_bookmark, zio, 0); 4673 } 4674 } 4675 4676 if (zio->io_error && zio == zio->io_logical) { 4677 /* 4678 * Determine whether zio should be reexecuted. This will 4679 * propagate all the way to the root via zio_notify_parent(). 4680 */ 4681 ASSERT(zio->io_vd == NULL && zio->io_bp != NULL); 4682 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 4683 4684 if (IO_IS_ALLOCATING(zio) && 4685 !(zio->io_flags & ZIO_FLAG_CANFAIL)) { 4686 if (zio->io_error != ENOSPC) 4687 zio->io_reexecute |= ZIO_REEXECUTE_NOW; 4688 else 4689 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 4690 } 4691 4692 if ((zio->io_type == ZIO_TYPE_READ || 4693 zio->io_type == ZIO_TYPE_FREE) && 4694 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && 4695 zio->io_error == ENXIO && 4696 spa_load_state(zio->io_spa) == SPA_LOAD_NONE && 4697 spa_get_failmode(zio->io_spa) != ZIO_FAILURE_MODE_CONTINUE) 4698 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 4699 4700 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute) 4701 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 4702 4703 /* 4704 * Here is a possibly good place to attempt to do 4705 * either combinatorial reconstruction or error correction 4706 * based on checksums. It also might be a good place 4707 * to send out preliminary ereports before we suspend 4708 * processing. 4709 */ 4710 } 4711 4712 /* 4713 * If there were logical child errors, they apply to us now. 4714 * We defer this until now to avoid conflating logical child 4715 * errors with errors that happened to the zio itself when 4716 * updating vdev stats and reporting FMA events above. 4717 */ 4718 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL); 4719 4720 if ((zio->io_error || zio->io_reexecute) && 4721 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio && 4722 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE))) 4723 zio_dva_unallocate(zio, zio->io_gang_tree, zio->io_bp); 4724 4725 zio_gang_tree_free(&zio->io_gang_tree); 4726 4727 /* 4728 * Godfather I/Os should never suspend. 4729 */ 4730 if ((zio->io_flags & ZIO_FLAG_GODFATHER) && 4731 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) 4732 zio->io_reexecute &= ~ZIO_REEXECUTE_SUSPEND; 4733 4734 if (zio->io_reexecute) { 4735 /* 4736 * This is a logical I/O that wants to reexecute. 4737 * 4738 * Reexecute is top-down. When an i/o fails, if it's not 4739 * the root, it simply notifies its parent and sticks around. 4740 * The parent, seeing that it still has children in zio_done(), 4741 * does the same. This percolates all the way up to the root. 4742 * The root i/o will reexecute or suspend the entire tree. 4743 * 4744 * This approach ensures that zio_reexecute() honors 4745 * all the original i/o dependency relationships, e.g. 4746 * parents not executing until children are ready. 4747 */ 4748 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 4749 4750 zio->io_gang_leader = NULL; 4751 4752 mutex_enter(&zio->io_lock); 4753 zio->io_state[ZIO_WAIT_DONE] = 1; 4754 mutex_exit(&zio->io_lock); 4755 4756 /* 4757 * "The Godfather" I/O monitors its children but is 4758 * not a true parent to them. It will track them through 4759 * the pipeline but severs its ties whenever they get into 4760 * trouble (e.g. suspended). This allows "The Godfather" 4761 * I/O to return status without blocking. 4762 */ 4763 zl = NULL; 4764 for (pio = zio_walk_parents(zio, &zl); pio != NULL; 4765 pio = pio_next) { 4766 zio_link_t *remove_zl = zl; 4767 pio_next = zio_walk_parents(zio, &zl); 4768 4769 if ((pio->io_flags & ZIO_FLAG_GODFATHER) && 4770 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) { 4771 zio_remove_child(pio, zio, remove_zl); 4772 /* 4773 * This is a rare code path, so we don't 4774 * bother with "next_to_execute". 4775 */ 4776 zio_notify_parent(pio, zio, ZIO_WAIT_DONE, 4777 NULL); 4778 } 4779 } 4780 4781 if ((pio = zio_unique_parent(zio)) != NULL) { 4782 /* 4783 * We're not a root i/o, so there's nothing to do 4784 * but notify our parent. Don't propagate errors 4785 * upward since we haven't permanently failed yet. 4786 */ 4787 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER)); 4788 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE; 4789 /* 4790 * This is a rare code path, so we don't bother with 4791 * "next_to_execute". 4792 */ 4793 zio_notify_parent(pio, zio, ZIO_WAIT_DONE, NULL); 4794 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) { 4795 /* 4796 * We'd fail again if we reexecuted now, so suspend 4797 * until conditions improve (e.g. device comes online). 4798 */ 4799 zio_suspend(zio->io_spa, zio, ZIO_SUSPEND_IOERR); 4800 } else { 4801 /* 4802 * Reexecution is potentially a huge amount of work. 4803 * Hand it off to the otherwise-unused claim taskq. 4804 */ 4805 ASSERT(taskq_empty_ent(&zio->io_tqent)); 4806 spa_taskq_dispatch_ent(zio->io_spa, 4807 ZIO_TYPE_CLAIM, ZIO_TASKQ_ISSUE, 4808 zio_reexecute, zio, 0, &zio->io_tqent); 4809 } 4810 return (NULL); 4811 } 4812 4813 ASSERT(zio->io_child_count == 0); 4814 ASSERT(zio->io_reexecute == 0); 4815 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL)); 4816 4817 /* 4818 * Report any checksum errors, since the I/O is complete. 4819 */ 4820 while (zio->io_cksum_report != NULL) { 4821 zio_cksum_report_t *zcr = zio->io_cksum_report; 4822 zio->io_cksum_report = zcr->zcr_next; 4823 zcr->zcr_next = NULL; 4824 zcr->zcr_finish(zcr, NULL); 4825 zfs_ereport_free_checksum(zcr); 4826 } 4827 4828 if (zio->io_flags & ZIO_FLAG_FASTWRITE && zio->io_bp && 4829 !BP_IS_HOLE(zio->io_bp) && !BP_IS_EMBEDDED(zio->io_bp) && 4830 !(zio->io_flags & ZIO_FLAG_NOPWRITE)) { 4831 metaslab_fastwrite_unmark(zio->io_spa, zio->io_bp); 4832 } 4833 4834 /* 4835 * It is the responsibility of the done callback to ensure that this 4836 * particular zio is no longer discoverable for adoption, and as 4837 * such, cannot acquire any new parents. 4838 */ 4839 if (zio->io_done) 4840 zio->io_done(zio); 4841 4842 mutex_enter(&zio->io_lock); 4843 zio->io_state[ZIO_WAIT_DONE] = 1; 4844 mutex_exit(&zio->io_lock); 4845 4846 /* 4847 * We are done executing this zio. We may want to execute a parent 4848 * next. See the comment in zio_notify_parent(). 4849 */ 4850 zio_t *next_to_execute = NULL; 4851 zl = NULL; 4852 for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) { 4853 zio_link_t *remove_zl = zl; 4854 pio_next = zio_walk_parents(zio, &zl); 4855 zio_remove_child(pio, zio, remove_zl); 4856 zio_notify_parent(pio, zio, ZIO_WAIT_DONE, &next_to_execute); 4857 } 4858 4859 if (zio->io_waiter != NULL) { 4860 mutex_enter(&zio->io_lock); 4861 zio->io_executor = NULL; 4862 cv_broadcast(&zio->io_cv); 4863 mutex_exit(&zio->io_lock); 4864 } else { 4865 zio_destroy(zio); 4866 } 4867 4868 return (next_to_execute); 4869 } 4870 4871 /* 4872 * ========================================================================== 4873 * I/O pipeline definition 4874 * ========================================================================== 4875 */ 4876 static zio_pipe_stage_t *zio_pipeline[] = { 4877 NULL, 4878 zio_read_bp_init, 4879 zio_write_bp_init, 4880 zio_free_bp_init, 4881 zio_issue_async, 4882 zio_write_compress, 4883 zio_encrypt, 4884 zio_checksum_generate, 4885 zio_nop_write, 4886 zio_ddt_read_start, 4887 zio_ddt_read_done, 4888 zio_ddt_write, 4889 zio_ddt_free, 4890 zio_gang_assemble, 4891 zio_gang_issue, 4892 zio_dva_throttle, 4893 zio_dva_allocate, 4894 zio_dva_free, 4895 zio_dva_claim, 4896 zio_ready, 4897 zio_vdev_io_start, 4898 zio_vdev_io_done, 4899 zio_vdev_io_assess, 4900 zio_checksum_verify, 4901 zio_done 4902 }; 4903 4904 4905 4906 4907 /* 4908 * Compare two zbookmark_phys_t's to see which we would reach first in a 4909 * pre-order traversal of the object tree. 4910 * 4911 * This is simple in every case aside from the meta-dnode object. For all other 4912 * objects, we traverse them in order (object 1 before object 2, and so on). 4913 * However, all of these objects are traversed while traversing object 0, since 4914 * the data it points to is the list of objects. Thus, we need to convert to a 4915 * canonical representation so we can compare meta-dnode bookmarks to 4916 * non-meta-dnode bookmarks. 4917 * 4918 * We do this by calculating "equivalents" for each field of the zbookmark. 4919 * zbookmarks outside of the meta-dnode use their own object and level, and 4920 * calculate the level 0 equivalent (the first L0 blkid that is contained in the 4921 * blocks this bookmark refers to) by multiplying their blkid by their span 4922 * (the number of L0 blocks contained within one block at their level). 4923 * zbookmarks inside the meta-dnode calculate their object equivalent 4924 * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use 4925 * level + 1<<31 (any value larger than a level could ever be) for their level. 4926 * This causes them to always compare before a bookmark in their object 4927 * equivalent, compare appropriately to bookmarks in other objects, and to 4928 * compare appropriately to other bookmarks in the meta-dnode. 4929 */ 4930 int 4931 zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2, 4932 const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2) 4933 { 4934 /* 4935 * These variables represent the "equivalent" values for the zbookmark, 4936 * after converting zbookmarks inside the meta dnode to their 4937 * normal-object equivalents. 4938 */ 4939 uint64_t zb1obj, zb2obj; 4940 uint64_t zb1L0, zb2L0; 4941 uint64_t zb1level, zb2level; 4942 4943 if (zb1->zb_object == zb2->zb_object && 4944 zb1->zb_level == zb2->zb_level && 4945 zb1->zb_blkid == zb2->zb_blkid) 4946 return (0); 4947 4948 IMPLY(zb1->zb_level > 0, ibs1 >= SPA_MINBLOCKSHIFT); 4949 IMPLY(zb2->zb_level > 0, ibs2 >= SPA_MINBLOCKSHIFT); 4950 4951 /* 4952 * BP_SPANB calculates the span in blocks. 4953 */ 4954 zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level); 4955 zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level); 4956 4957 if (zb1->zb_object == DMU_META_DNODE_OBJECT) { 4958 zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT)); 4959 zb1L0 = 0; 4960 zb1level = zb1->zb_level + COMPARE_META_LEVEL; 4961 } else { 4962 zb1obj = zb1->zb_object; 4963 zb1level = zb1->zb_level; 4964 } 4965 4966 if (zb2->zb_object == DMU_META_DNODE_OBJECT) { 4967 zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT)); 4968 zb2L0 = 0; 4969 zb2level = zb2->zb_level + COMPARE_META_LEVEL; 4970 } else { 4971 zb2obj = zb2->zb_object; 4972 zb2level = zb2->zb_level; 4973 } 4974 4975 /* Now that we have a canonical representation, do the comparison. */ 4976 if (zb1obj != zb2obj) 4977 return (zb1obj < zb2obj ? -1 : 1); 4978 else if (zb1L0 != zb2L0) 4979 return (zb1L0 < zb2L0 ? -1 : 1); 4980 else if (zb1level != zb2level) 4981 return (zb1level > zb2level ? -1 : 1); 4982 /* 4983 * This can (theoretically) happen if the bookmarks have the same object 4984 * and level, but different blkids, if the block sizes are not the same. 4985 * There is presently no way to change the indirect block sizes 4986 */ 4987 return (0); 4988 } 4989 4990 /* 4991 * This function checks the following: given that last_block is the place that 4992 * our traversal stopped last time, does that guarantee that we've visited 4993 * every node under subtree_root? Therefore, we can't just use the raw output 4994 * of zbookmark_compare. We have to pass in a modified version of 4995 * subtree_root; by incrementing the block id, and then checking whether 4996 * last_block is before or equal to that, we can tell whether or not having 4997 * visited last_block implies that all of subtree_root's children have been 4998 * visited. 4999 */ 5000 boolean_t 5001 zbookmark_subtree_completed(const dnode_phys_t *dnp, 5002 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block) 5003 { 5004 zbookmark_phys_t mod_zb = *subtree_root; 5005 mod_zb.zb_blkid++; 5006 ASSERT0(last_block->zb_level); 5007 5008 /* The objset_phys_t isn't before anything. */ 5009 if (dnp == NULL) 5010 return (B_FALSE); 5011 5012 /* 5013 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the 5014 * data block size in sectors, because that variable is only used if 5015 * the bookmark refers to a block in the meta-dnode. Since we don't 5016 * know without examining it what object it refers to, and there's no 5017 * harm in passing in this value in other cases, we always pass it in. 5018 * 5019 * We pass in 0 for the indirect block size shift because zb2 must be 5020 * level 0. The indirect block size is only used to calculate the span 5021 * of the bookmark, but since the bookmark must be level 0, the span is 5022 * always 1, so the math works out. 5023 * 5024 * If you make changes to how the zbookmark_compare code works, be sure 5025 * to make sure that this code still works afterwards. 5026 */ 5027 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift, 5028 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb, 5029 last_block) <= 0); 5030 } 5031 5032 /* 5033 * This function is similar to zbookmark_subtree_completed(), but returns true 5034 * if subtree_root is equal or ahead of last_block, i.e. still to be done. 5035 */ 5036 boolean_t 5037 zbookmark_subtree_tbd(const dnode_phys_t *dnp, 5038 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block) 5039 { 5040 ASSERT0(last_block->zb_level); 5041 if (dnp == NULL) 5042 return (B_FALSE); 5043 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift, 5044 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, subtree_root, 5045 last_block) >= 0); 5046 } 5047 5048 EXPORT_SYMBOL(zio_type_name); 5049 EXPORT_SYMBOL(zio_buf_alloc); 5050 EXPORT_SYMBOL(zio_data_buf_alloc); 5051 EXPORT_SYMBOL(zio_buf_free); 5052 EXPORT_SYMBOL(zio_data_buf_free); 5053 5054 ZFS_MODULE_PARAM(zfs_zio, zio_, slow_io_ms, INT, ZMOD_RW, 5055 "Max I/O completion time (milliseconds) before marking it as slow"); 5056 5057 ZFS_MODULE_PARAM(zfs_zio, zio_, requeue_io_start_cut_in_line, INT, ZMOD_RW, 5058 "Prioritize requeued I/O"); 5059 5060 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_deferred_free, UINT, ZMOD_RW, 5061 "Defer frees starting in this pass"); 5062 5063 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_dont_compress, UINT, ZMOD_RW, 5064 "Don't compress starting in this pass"); 5065 5066 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_rewrite, UINT, ZMOD_RW, 5067 "Rewrite new bps starting in this pass"); 5068 5069 ZFS_MODULE_PARAM(zfs_zio, zio_, dva_throttle_enabled, INT, ZMOD_RW, 5070 "Throttle block allocations in the ZIO pipeline"); 5071 5072 ZFS_MODULE_PARAM(zfs_zio, zio_, deadman_log_all, INT, ZMOD_RW, 5073 "Log all slow ZIOs, not just those with vdevs"); 5074