1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * This file and its contents are supplied under the terms of the 4 * Common Development and Distribution License ("CDDL"), version 1.0. 5 * You may only use this file in accordance with the terms of version 6 * 1.0 of the CDDL. 7 * 8 * A full copy of the text of the CDDL should have accompanied this 9 * source. A copy of the CDDL is also available via the Internet at 10 * https://opensource.org/license/CDDL-1.0. 11 */ 12 /* 13 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 14 * Copyright (c) 2011, 2022 by Delphix. All rights reserved. 15 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved. 16 * Copyright (c) 2017, Intel Corporation. 17 * Copyright (c) 2019, 2023, 2024, 2025, Klara, Inc. 18 * Copyright (c) 2019, Allan Jude 19 * Copyright (c) 2021, Datto, Inc. 20 * Copyright (c) 2021, 2024 by George Melikov. All rights reserved. 21 */ 22 23 #include <sys/sysmacros.h> 24 #include <sys/zfs_context.h> 25 #include <sys/fm/fs/zfs.h> 26 #include <sys/spa.h> 27 #include <sys/txg.h> 28 #include <sys/spa_impl.h> 29 #include <sys/vdev_impl.h> 30 #include <sys/vdev_trim.h> 31 #include <sys/zio_impl.h> 32 #include <sys/zio_compress.h> 33 #include <sys/zio_checksum.h> 34 #include <sys/dmu_objset.h> 35 #include <sys/arc.h> 36 #include <sys/brt.h> 37 #include <sys/ddt.h> 38 #include <sys/blkptr.h> 39 #include <sys/zfeature.h> 40 #include <sys/dsl_scan.h> 41 #include <sys/metaslab_impl.h> 42 #include <sys/time.h> 43 #include <sys/trace_zfs.h> 44 #include <sys/abd.h> 45 #include <sys/dsl_crypt.h> 46 #include <cityhash.h> 47 48 /* 49 * ========================================================================== 50 * I/O type descriptions 51 * ========================================================================== 52 */ 53 const char *const zio_type_name[ZIO_TYPES] = { 54 /* 55 * Note: Linux kernel thread name length is limited 56 * so these names will differ from upstream open zfs. 57 */ 58 "z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_flush", "z_trim" 59 }; 60 61 int zio_dva_throttle_enabled = B_TRUE; 62 static int zio_deadman_log_all = B_FALSE; 63 64 /* 65 * ========================================================================== 66 * I/O kmem caches 67 * ========================================================================== 68 */ 69 static kmem_cache_t *zio_cache; 70 static kmem_cache_t *zio_link_cache; 71 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 72 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 73 #if defined(ZFS_DEBUG) && !defined(_KERNEL) 74 static uint64_t zio_buf_cache_allocs[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 75 static uint64_t zio_buf_cache_frees[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 76 #endif 77 78 /* Mark IOs as "slow" if they take longer than 30 seconds */ 79 static uint_t zio_slow_io_ms = (30 * MILLISEC); 80 81 #define BP_SPANB(indblkshift, level) \ 82 (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT))) 83 #define COMPARE_META_LEVEL 0x80000000ul 84 /* 85 * The following actions directly effect the spa's sync-to-convergence logic. 86 * The values below define the sync pass when we start performing the action. 87 * Care should be taken when changing these values as they directly impact 88 * spa_sync() performance. Tuning these values may introduce subtle performance 89 * pathologies and should only be done in the context of performance analysis. 90 * These tunables will eventually be removed and replaced with #defines once 91 * enough analysis has been done to determine optimal values. 92 * 93 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that 94 * regular blocks are not deferred. 95 * 96 * Starting in sync pass 8 (zfs_sync_pass_dont_compress), we disable 97 * compression (including of metadata). In practice, we don't have this 98 * many sync passes, so this has no effect. 99 * 100 * The original intent was that disabling compression would help the sync 101 * passes to converge. However, in practice disabling compression increases 102 * the average number of sync passes, because when we turn compression off, a 103 * lot of block's size will change and thus we have to re-allocate (not 104 * overwrite) them. It also increases the number of 128KB allocations (e.g. 105 * for indirect blocks and spacemaps) because these will not be compressed. 106 * The 128K allocations are especially detrimental to performance on highly 107 * fragmented systems, which may have very few free segments of this size, 108 * and may need to load new metaslabs to satisfy 128K allocations. 109 */ 110 111 /* defer frees starting in this pass */ 112 uint_t zfs_sync_pass_deferred_free = 2; 113 114 /* don't compress starting in this pass */ 115 static uint_t zfs_sync_pass_dont_compress = 8; 116 117 /* rewrite new bps starting in this pass */ 118 static uint_t zfs_sync_pass_rewrite = 2; 119 120 /* 121 * An allocating zio is one that either currently has the DVA allocate 122 * stage set or will have it later in its lifetime. 123 */ 124 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE) 125 126 /* 127 * Enable smaller cores by excluding metadata 128 * allocations as well. 129 */ 130 int zio_exclude_metadata = 0; 131 static int zio_requeue_io_start_cut_in_line = 1; 132 133 #ifdef ZFS_DEBUG 134 static const int zio_buf_debug_limit = 16384; 135 #else 136 static const int zio_buf_debug_limit = 0; 137 #endif 138 139 typedef struct zio_stats { 140 kstat_named_t ziostat_total_allocations; 141 kstat_named_t ziostat_alloc_class_fallbacks; 142 kstat_named_t ziostat_gang_writes; 143 kstat_named_t ziostat_gang_multilevel; 144 } zio_stats_t; 145 146 static zio_stats_t zio_stats = { 147 { "total_allocations", KSTAT_DATA_UINT64 }, 148 { "alloc_class_fallbacks", KSTAT_DATA_UINT64 }, 149 { "gang_writes", KSTAT_DATA_UINT64 }, 150 { "gang_multilevel", KSTAT_DATA_UINT64 }, 151 }; 152 153 struct { 154 wmsum_t ziostat_total_allocations; 155 wmsum_t ziostat_alloc_class_fallbacks; 156 wmsum_t ziostat_gang_writes; 157 wmsum_t ziostat_gang_multilevel; 158 } ziostat_sums; 159 160 #define ZIOSTAT_BUMP(stat) wmsum_add(&ziostat_sums.stat, 1); 161 162 static kstat_t *zio_ksp; 163 164 static inline void __zio_execute(zio_t *zio); 165 166 static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t); 167 static void zio_batch_join(zio_batch_t *, zio_t *); 168 169 static int 170 zio_kstats_update(kstat_t *ksp, int rw) 171 { 172 zio_stats_t *zs = ksp->ks_data; 173 if (rw == KSTAT_WRITE) 174 return (EACCES); 175 176 zs->ziostat_total_allocations.value.ui64 = 177 wmsum_value(&ziostat_sums.ziostat_total_allocations); 178 zs->ziostat_alloc_class_fallbacks.value.ui64 = 179 wmsum_value(&ziostat_sums.ziostat_alloc_class_fallbacks); 180 zs->ziostat_gang_writes.value.ui64 = 181 wmsum_value(&ziostat_sums.ziostat_gang_writes); 182 zs->ziostat_gang_multilevel.value.ui64 = 183 wmsum_value(&ziostat_sums.ziostat_gang_multilevel); 184 return (0); 185 } 186 187 void 188 zio_init(void) 189 { 190 size_t c; 191 192 zio_cache = kmem_cache_create("zio_cache", 193 sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 194 zio_link_cache = kmem_cache_create("zio_link_cache", 195 sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 196 197 wmsum_init(&ziostat_sums.ziostat_total_allocations, 0); 198 wmsum_init(&ziostat_sums.ziostat_alloc_class_fallbacks, 0); 199 wmsum_init(&ziostat_sums.ziostat_gang_writes, 0); 200 wmsum_init(&ziostat_sums.ziostat_gang_multilevel, 0); 201 zio_ksp = kstat_create("zfs", 0, "zio_stats", 202 "misc", KSTAT_TYPE_NAMED, sizeof (zio_stats) / 203 sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 204 if (zio_ksp != NULL) { 205 zio_ksp->ks_data = &zio_stats; 206 zio_ksp->ks_update = zio_kstats_update; 207 kstat_install(zio_ksp); 208 } 209 210 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) { 211 size_t size = (c + 1) << SPA_MINBLOCKSHIFT; 212 size_t align, cflags, data_cflags; 213 char name[32]; 214 215 /* 216 * Create cache for each half-power of 2 size, starting from 217 * SPA_MINBLOCKSIZE. It should give us memory space efficiency 218 * of ~7/8, sufficient for transient allocations mostly using 219 * these caches. 220 */ 221 size_t p2 = size; 222 while (!ISP2(p2)) 223 p2 &= p2 - 1; 224 if (!IS_P2ALIGNED(size, p2 / 2)) 225 continue; 226 227 #ifndef _KERNEL 228 /* 229 * If we are using watchpoints, put each buffer on its own page, 230 * to eliminate the performance overhead of trapping to the 231 * kernel when modifying a non-watched buffer that shares the 232 * page with a watched buffer. 233 */ 234 if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE)) 235 continue; 236 #endif 237 238 if (IS_P2ALIGNED(size, PAGESIZE)) 239 align = PAGESIZE; 240 else 241 align = 1 << (highbit64(size ^ (size - 1)) - 1); 242 243 cflags = (zio_exclude_metadata || size > zio_buf_debug_limit) ? 244 KMC_NODEBUG : 0; 245 data_cflags = KMC_NODEBUG; 246 if (abd_size_alloc_linear(size)) { 247 cflags |= KMC_RECLAIMABLE; 248 data_cflags |= KMC_RECLAIMABLE; 249 } 250 if (cflags == data_cflags) { 251 /* 252 * Resulting kmem caches would be identical. 253 * Save memory by creating only one. 254 */ 255 (void) snprintf(name, sizeof (name), 256 "zio_buf_comb_%lu", (ulong_t)size); 257 zio_buf_cache[c] = kmem_cache_create(name, size, align, 258 NULL, NULL, NULL, NULL, NULL, cflags); 259 zio_data_buf_cache[c] = zio_buf_cache[c]; 260 continue; 261 } 262 (void) snprintf(name, sizeof (name), "zio_buf_%lu", 263 (ulong_t)size); 264 zio_buf_cache[c] = kmem_cache_create(name, size, align, 265 NULL, NULL, NULL, NULL, NULL, cflags); 266 267 (void) snprintf(name, sizeof (name), "zio_data_buf_%lu", 268 (ulong_t)size); 269 zio_data_buf_cache[c] = kmem_cache_create(name, size, align, 270 NULL, NULL, NULL, NULL, NULL, data_cflags); 271 } 272 273 while (--c != 0) { 274 ASSERT(zio_buf_cache[c] != NULL); 275 if (zio_buf_cache[c - 1] == NULL) 276 zio_buf_cache[c - 1] = zio_buf_cache[c]; 277 278 ASSERT(zio_data_buf_cache[c] != NULL); 279 if (zio_data_buf_cache[c - 1] == NULL) 280 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c]; 281 } 282 283 zio_inject_init(); 284 285 lz4_init(); 286 } 287 288 void 289 zio_fini(void) 290 { 291 size_t n = SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; 292 293 #if defined(ZFS_DEBUG) && !defined(_KERNEL) 294 for (size_t i = 0; i < n; i++) { 295 if (zio_buf_cache_allocs[i] != zio_buf_cache_frees[i]) 296 (void) printf("zio_fini: [%d] %llu != %llu\n", 297 (int)((i + 1) << SPA_MINBLOCKSHIFT), 298 (long long unsigned)zio_buf_cache_allocs[i], 299 (long long unsigned)zio_buf_cache_frees[i]); 300 } 301 #endif 302 303 /* 304 * The same kmem cache can show up multiple times in both zio_buf_cache 305 * and zio_data_buf_cache. Do a wasteful but trivially correct scan to 306 * sort it out. 307 */ 308 for (size_t i = 0; i < n; i++) { 309 kmem_cache_t *cache = zio_buf_cache[i]; 310 if (cache == NULL) 311 continue; 312 for (size_t j = i; j < n; j++) { 313 if (cache == zio_buf_cache[j]) 314 zio_buf_cache[j] = NULL; 315 if (cache == zio_data_buf_cache[j]) 316 zio_data_buf_cache[j] = NULL; 317 } 318 kmem_cache_destroy(cache); 319 } 320 321 for (size_t i = 0; i < n; i++) { 322 kmem_cache_t *cache = zio_data_buf_cache[i]; 323 if (cache == NULL) 324 continue; 325 for (size_t j = i; j < n; j++) { 326 if (cache == zio_data_buf_cache[j]) 327 zio_data_buf_cache[j] = NULL; 328 } 329 kmem_cache_destroy(cache); 330 } 331 332 for (size_t i = 0; i < n; i++) { 333 VERIFY0P(zio_buf_cache[i]); 334 VERIFY0P(zio_data_buf_cache[i]); 335 } 336 337 if (zio_ksp != NULL) { 338 kstat_delete(zio_ksp); 339 zio_ksp = NULL; 340 } 341 342 wmsum_fini(&ziostat_sums.ziostat_total_allocations); 343 wmsum_fini(&ziostat_sums.ziostat_alloc_class_fallbacks); 344 wmsum_fini(&ziostat_sums.ziostat_gang_writes); 345 wmsum_fini(&ziostat_sums.ziostat_gang_multilevel); 346 347 kmem_cache_destroy(zio_link_cache); 348 kmem_cache_destroy(zio_cache); 349 350 zio_inject_fini(); 351 352 lz4_fini(); 353 } 354 355 /* 356 * ========================================================================== 357 * Allocate and free I/O buffers 358 * ========================================================================== 359 */ 360 361 #if defined(ZFS_DEBUG) && defined(_KERNEL) 362 #define ZFS_ZIO_BUF_CANARY 1 363 #endif 364 365 #ifdef ZFS_ZIO_BUF_CANARY 366 static const ulong_t zio_buf_canary = (ulong_t)0xdeadc0dedead210b; 367 368 /* 369 * Use empty space after the buffer to detect overflows. 370 * 371 * Since zio_init() creates kmem caches only for certain set of buffer sizes, 372 * allocations of different sizes may have some unused space after the data. 373 * Filling part of that space with a known pattern on allocation and checking 374 * it on free should allow us to detect some buffer overflows. 375 */ 376 static void 377 zio_buf_put_canary(ulong_t *p, size_t size, kmem_cache_t **cache, size_t c) 378 { 379 size_t off = P2ROUNDUP(size, sizeof (ulong_t)); 380 ulong_t *canary = p + off / sizeof (ulong_t); 381 size_t asize = (c + 1) << SPA_MINBLOCKSHIFT; 382 if (c + 1 < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT && 383 cache[c] == cache[c + 1]) 384 asize = (c + 2) << SPA_MINBLOCKSHIFT; 385 for (; off < asize; canary++, off += sizeof (ulong_t)) 386 *canary = zio_buf_canary; 387 } 388 389 static void 390 zio_buf_check_canary(ulong_t *p, size_t size, kmem_cache_t **cache, size_t c) 391 { 392 size_t off = P2ROUNDUP(size, sizeof (ulong_t)); 393 ulong_t *canary = p + off / sizeof (ulong_t); 394 size_t asize = (c + 1) << SPA_MINBLOCKSHIFT; 395 if (c + 1 < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT && 396 cache[c] == cache[c + 1]) 397 asize = (c + 2) << SPA_MINBLOCKSHIFT; 398 for (; off < asize; canary++, off += sizeof (ulong_t)) { 399 if (unlikely(*canary != zio_buf_canary)) { 400 PANIC("ZIO buffer overflow %p (%zu) + %zu %#lx != %#lx", 401 p, size, (canary - p) * sizeof (ulong_t), 402 *canary, zio_buf_canary); 403 } 404 } 405 } 406 #endif 407 408 /* 409 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a 410 * crashdump if the kernel panics, so use it judiciously. Obviously, it's 411 * useful to inspect ZFS metadata, but if possible, we should avoid keeping 412 * excess / transient data in-core during a crashdump. 413 */ 414 void * 415 zio_buf_alloc(size_t size) 416 { 417 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 418 419 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 420 #if defined(ZFS_DEBUG) && !defined(_KERNEL) 421 atomic_add_64(&zio_buf_cache_allocs[c], 1); 422 #endif 423 424 void *p = kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE); 425 #ifdef ZFS_ZIO_BUF_CANARY 426 zio_buf_put_canary(p, size, zio_buf_cache, c); 427 #endif 428 return (p); 429 } 430 431 /* 432 * Use zio_data_buf_alloc to allocate data. The data will not appear in a 433 * crashdump if the kernel panics. This exists so that we will limit the amount 434 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount 435 * of kernel heap dumped to disk when the kernel panics) 436 */ 437 void * 438 zio_data_buf_alloc(size_t size) 439 { 440 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 441 442 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 443 444 void *p = kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE); 445 #ifdef ZFS_ZIO_BUF_CANARY 446 zio_buf_put_canary(p, size, zio_data_buf_cache, c); 447 #endif 448 return (p); 449 } 450 451 void 452 zio_buf_free(void *buf, size_t size) 453 { 454 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 455 456 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 457 #if defined(ZFS_DEBUG) && !defined(_KERNEL) 458 atomic_add_64(&zio_buf_cache_frees[c], 1); 459 #endif 460 461 #ifdef ZFS_ZIO_BUF_CANARY 462 zio_buf_check_canary(buf, size, zio_buf_cache, c); 463 #endif 464 kmem_cache_free(zio_buf_cache[c], buf); 465 } 466 467 void 468 zio_data_buf_free(void *buf, size_t size) 469 { 470 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 471 472 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 473 474 #ifdef ZFS_ZIO_BUF_CANARY 475 zio_buf_check_canary(buf, size, zio_data_buf_cache, c); 476 #endif 477 kmem_cache_free(zio_data_buf_cache[c], buf); 478 } 479 480 static void 481 zio_abd_free(void *abd, size_t size) 482 { 483 (void) size; 484 abd_free((abd_t *)abd); 485 } 486 487 /* 488 * ========================================================================== 489 * Push and pop I/O transform buffers 490 * ========================================================================== 491 */ 492 void 493 zio_push_transform(zio_t *zio, abd_t *data, uint64_t size, uint64_t bufsize, 494 zio_transform_func_t *transform) 495 { 496 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP); 497 498 zt->zt_orig_abd = zio->io_abd; 499 zt->zt_orig_size = zio->io_size; 500 zt->zt_bufsize = bufsize; 501 zt->zt_transform = transform; 502 503 zt->zt_next = zio->io_transform_stack; 504 zio->io_transform_stack = zt; 505 506 zio->io_abd = data; 507 zio->io_size = size; 508 } 509 510 void 511 zio_pop_transforms(zio_t *zio) 512 { 513 zio_transform_t *zt; 514 515 while ((zt = zio->io_transform_stack) != NULL) { 516 if (zt->zt_transform != NULL) 517 zt->zt_transform(zio, 518 zt->zt_orig_abd, zt->zt_orig_size); 519 520 if (zt->zt_bufsize != 0) 521 abd_free(zio->io_abd); 522 523 zio->io_abd = zt->zt_orig_abd; 524 zio->io_size = zt->zt_orig_size; 525 zio->io_transform_stack = zt->zt_next; 526 527 kmem_free(zt, sizeof (zio_transform_t)); 528 } 529 } 530 531 /* 532 * ========================================================================== 533 * I/O transform callbacks for subblocks, decompression, and decryption 534 * ========================================================================== 535 */ 536 static void 537 zio_subblock(zio_t *zio, abd_t *data, uint64_t size) 538 { 539 ASSERT(zio->io_size > size); 540 541 if (zio->io_type == ZIO_TYPE_READ) 542 abd_copy(data, zio->io_abd, size); 543 } 544 545 static void 546 zio_decompress(zio_t *zio, abd_t *data, uint64_t size) 547 { 548 if (zio->io_error == 0) { 549 int ret = zio_decompress_data(BP_GET_COMPRESS(zio->io_bp), 550 zio->io_abd, data, zio->io_size, size, 551 &zio->io_prop.zp_complevel); 552 553 if (zio_injection_enabled && ret == 0) 554 ret = zio_handle_fault_injection(zio, EINVAL); 555 556 if (ret != 0) 557 zio->io_error = SET_ERROR(EIO); 558 } 559 } 560 561 static void 562 zio_decrypt(zio_t *zio, abd_t *data, uint64_t size) 563 { 564 int ret; 565 void *tmp; 566 blkptr_t *bp = zio->io_bp; 567 spa_t *spa = zio->io_spa; 568 uint64_t dsobj = zio->io_bookmark.zb_objset; 569 uint64_t lsize = BP_GET_LSIZE(bp); 570 dmu_object_type_t ot = BP_GET_TYPE(bp); 571 uint8_t salt[ZIO_DATA_SALT_LEN]; 572 uint8_t iv[ZIO_DATA_IV_LEN]; 573 uint8_t mac[ZIO_DATA_MAC_LEN]; 574 boolean_t no_crypt = B_FALSE; 575 576 ASSERT(BP_USES_CRYPT(bp)); 577 ASSERT3U(size, !=, 0); 578 579 if (zio->io_error != 0) 580 return; 581 582 /* 583 * Verify the cksum of MACs stored in an indirect bp. It will always 584 * be possible to verify this since it does not require an encryption 585 * key. 586 */ 587 if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) { 588 zio_crypt_decode_mac_bp(bp, mac); 589 590 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) { 591 /* 592 * We haven't decompressed the data yet, but 593 * zio_crypt_do_indirect_mac_checksum() requires 594 * decompressed data to be able to parse out the MACs 595 * from the indirect block. We decompress it now and 596 * throw away the result after we are finished. 597 */ 598 abd_t *abd = abd_alloc_linear(lsize, B_TRUE); 599 ret = zio_decompress_data(BP_GET_COMPRESS(bp), 600 zio->io_abd, abd, zio->io_size, lsize, 601 &zio->io_prop.zp_complevel); 602 if (ret != 0) { 603 abd_free(abd); 604 ret = SET_ERROR(EIO); 605 goto error; 606 } 607 ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE, 608 abd, lsize, BP_SHOULD_BYTESWAP(bp), mac); 609 abd_free(abd); 610 } else { 611 ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE, 612 zio->io_abd, size, BP_SHOULD_BYTESWAP(bp), mac); 613 } 614 abd_copy(data, zio->io_abd, size); 615 616 if (zio_injection_enabled && ot != DMU_OT_DNODE && ret == 0) { 617 ret = zio_handle_decrypt_injection(spa, 618 &zio->io_bookmark, ot, ECKSUM); 619 } 620 if (ret != 0) 621 goto error; 622 623 return; 624 } 625 626 /* 627 * If this is an authenticated block, just check the MAC. It would be 628 * nice to separate this out into its own flag, but when this was done, 629 * we had run out of bits in what is now zio_flag_t. Future cleanup 630 * could make this a flag bit. 631 */ 632 if (BP_IS_AUTHENTICATED(bp)) { 633 if (ot == DMU_OT_OBJSET) { 634 ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa, 635 dsobj, zio->io_abd, size, BP_SHOULD_BYTESWAP(bp)); 636 } else { 637 zio_crypt_decode_mac_bp(bp, mac); 638 ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj, 639 zio->io_abd, size, mac); 640 if (zio_injection_enabled && ret == 0) { 641 ret = zio_handle_decrypt_injection(spa, 642 &zio->io_bookmark, ot, ECKSUM); 643 } 644 } 645 abd_copy(data, zio->io_abd, size); 646 647 if (ret != 0) 648 goto error; 649 650 return; 651 } 652 653 zio_crypt_decode_params_bp(bp, salt, iv); 654 655 if (ot == DMU_OT_INTENT_LOG) { 656 tmp = abd_borrow_buf_copy(zio->io_abd, sizeof (zil_chain_t)); 657 zio_crypt_decode_mac_zil(tmp, mac); 658 abd_return_buf(zio->io_abd, tmp, sizeof (zil_chain_t)); 659 } else { 660 zio_crypt_decode_mac_bp(bp, mac); 661 } 662 663 ret = spa_do_crypt_abd(B_FALSE, spa, &zio->io_bookmark, BP_GET_TYPE(bp), 664 BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), salt, iv, mac, size, data, 665 zio->io_abd, &no_crypt); 666 if (no_crypt) 667 abd_copy(data, zio->io_abd, size); 668 669 if (ret != 0) 670 goto error; 671 672 return; 673 674 error: 675 /* the key was found unless this was speculative or a thorough scrub */ 676 ASSERT(ret != EACCES || (zio->io_flags & ZIO_FLAG_SPECULATIVE) || 677 ((zio->io_flags & ZIO_FLAG_SCRUB) && 678 !(zio->io_flags & ZIO_FLAG_RAW))); 679 680 /* 681 * If there was a decryption / authentication error return EIO as 682 * the io_error. If this was not a speculative zio, create an ereport. 683 */ 684 if (ret == ECKSUM) { 685 zio->io_error = SET_ERROR(EIO); 686 if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) { 687 spa_log_error(spa, &zio->io_bookmark, 688 BP_GET_PHYSICAL_BIRTH(zio->io_bp)); 689 (void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION, 690 spa, NULL, &zio->io_bookmark, zio, 0); 691 } 692 } else { 693 zio->io_error = ret; 694 } 695 } 696 697 /* 698 * ========================================================================== 699 * I/O parent/child relationships and pipeline interlocks 700 * ========================================================================== 701 */ 702 zio_t * 703 zio_walk_parents(zio_t *cio, zio_link_t **zl) 704 { 705 list_t *pl = &cio->io_parent_list; 706 707 *zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl); 708 if (*zl == NULL) 709 return (NULL); 710 711 ASSERT((*zl)->zl_child == cio); 712 return ((*zl)->zl_parent); 713 } 714 715 zio_t * 716 zio_walk_children(zio_t *pio, zio_link_t **zl) 717 { 718 list_t *cl = &pio->io_child_list; 719 720 ASSERT(MUTEX_HELD(&pio->io_lock)); 721 722 *zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl); 723 if (*zl == NULL) 724 return (NULL); 725 726 ASSERT((*zl)->zl_parent == pio); 727 return ((*zl)->zl_child); 728 } 729 730 zio_t * 731 zio_unique_parent(zio_t *cio) 732 { 733 zio_link_t *zl = NULL; 734 zio_t *pio = zio_walk_parents(cio, &zl); 735 736 VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL); 737 return (pio); 738 } 739 740 static void 741 zio_add_child_impl(zio_t *pio, zio_t *cio, boolean_t first) 742 { 743 /* 744 * Logical I/Os can have logical, gang, or vdev children. 745 * Gang I/Os can have gang or vdev children. 746 * Vdev I/Os can only have vdev children. 747 * The following ASSERT captures all of these constraints. 748 */ 749 ASSERT3S(cio->io_child_type, <=, pio->io_child_type); 750 751 /* Parent should not have READY stage if child doesn't have it. */ 752 IMPLY((cio->io_pipeline & ZIO_STAGE_READY) == 0 && 753 (cio->io_child_type != ZIO_CHILD_VDEV), 754 (pio->io_pipeline & ZIO_STAGE_READY) == 0); 755 756 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP); 757 zl->zl_parent = pio; 758 zl->zl_child = cio; 759 760 mutex_enter(&pio->io_lock); 761 762 if (first) 763 ASSERT(list_is_empty(&cio->io_parent_list)); 764 else 765 mutex_enter(&cio->io_lock); 766 767 ASSERT0(pio->io_state[ZIO_WAIT_DONE]); 768 769 uint64_t *countp = pio->io_children[cio->io_child_type]; 770 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 771 countp[w] += !cio->io_state[w]; 772 773 list_insert_head(&pio->io_child_list, zl); 774 list_insert_head(&cio->io_parent_list, zl); 775 776 if (!first) 777 mutex_exit(&cio->io_lock); 778 779 mutex_exit(&pio->io_lock); 780 } 781 782 void 783 zio_add_child(zio_t *pio, zio_t *cio) 784 { 785 zio_add_child_impl(pio, cio, B_FALSE); 786 } 787 788 static void 789 zio_add_child_first(zio_t *pio, zio_t *cio) 790 { 791 zio_add_child_impl(pio, cio, B_TRUE); 792 } 793 794 static void 795 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl) 796 { 797 ASSERT(zl->zl_parent == pio); 798 ASSERT(zl->zl_child == cio); 799 800 mutex_enter(&pio->io_lock); 801 mutex_enter(&cio->io_lock); 802 803 list_remove(&pio->io_child_list, zl); 804 list_remove(&cio->io_parent_list, zl); 805 806 mutex_exit(&cio->io_lock); 807 mutex_exit(&pio->io_lock); 808 kmem_cache_free(zio_link_cache, zl); 809 } 810 811 static boolean_t 812 zio_wait_for_children(zio_t *zio, uint8_t childbits, enum zio_wait_type wait) 813 { 814 boolean_t waiting = B_FALSE; 815 816 mutex_enter(&zio->io_lock); 817 ASSERT0P(zio->io_stall); 818 for (int c = 0; c < ZIO_CHILD_TYPES; c++) { 819 if (!(ZIO_CHILD_BIT_IS_SET(childbits, c))) 820 continue; 821 822 uint64_t *countp = &zio->io_children[c][wait]; 823 if (*countp != 0) { 824 zio->io_stage >>= 1; 825 ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN); 826 zio->io_stall = countp; 827 waiting = B_TRUE; 828 break; 829 } 830 } 831 mutex_exit(&zio->io_lock); 832 return (waiting); 833 } 834 835 /* 836 * The zios a pipeline stage hands back to zio_execute() to run once the 837 * current one stops, chained through io_exec_next in the order they were 838 * added. 839 */ 840 typedef struct zio_next { 841 zio_t *zn_list; 842 zio_t **zn_tailp; /* where the next one is appended */ 843 } zio_next_t; 844 845 static inline void 846 zio_next_init(zio_next_t *next) 847 { 848 next->zn_list = NULL; 849 next->zn_tailp = &next->zn_list; 850 } 851 852 __attribute__((always_inline)) 853 static inline void 854 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait, 855 zio_next_t *nextp) 856 { 857 uint64_t *countp = &pio->io_children[zio->io_child_type][wait]; 858 int *errorp = &pio->io_child_error[zio->io_child_type]; 859 860 mutex_enter(&pio->io_lock); 861 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 862 *errorp = zio_worst_error(*errorp, zio->io_error); 863 pio->io_post |= zio->io_post; 864 ASSERT3U(*countp, >, 0); 865 866 (*countp)--; 867 868 if (*countp == 0 && pio->io_stall == countp) { 869 zio_taskq_type_t type = 870 pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE : 871 ZIO_TASKQ_INTERRUPT; 872 pio->io_stall = NULL; 873 mutex_exit(&pio->io_lock); 874 875 /* 876 * If we can tell the caller to execute this parent next, do 877 * so. We do this if the parent's zio type matches the child's 878 * type, or if it's a zio_null() with no done callback, and so 879 * has no actual work to do. Otherwise dispatch the parent zio 880 * in its own taskq. 881 * 882 * Having the caller execute the parent when possible reduces 883 * locking on the zio taskq's, reduces context switch 884 * overhead, and has no recursion penalty. Note that one 885 * read from disk typically causes at least 3 zio's: a 886 * zio_null(), the logical zio_read(), and then a physical 887 * zio. When the physical ZIO completes, we are able to call 888 * zio_done() on all 3 of these zio's from one invocation of 889 * zio_execute() by returning the parent back to 890 * zio_execute(). Since the parent isn't executed until this 891 * thread returns back to zio_execute(), the caller should do 892 * so promptly. 893 * 894 * In other cases, dispatching the parent prevents 895 * overflowing the stack when we have deeply nested 896 * parent-child relationships, as we do with the "mega zio" 897 * of writes for spa_sync(), and the chain of ZIL blocks. 898 * 899 * More than one parent may become executable at once, and all 900 * of them go back to the caller. It is the caller that keeps 901 * one and dispatches the rest, since only it knows what else 902 * is already waiting for its thread. 903 */ 904 if (nextp != NULL && 905 (pio->io_type == zio->io_type || 906 (pio->io_type == ZIO_TYPE_NULL && !pio->io_done))) { 907 ASSERT3P(pio->io_exec_next, ==, NULL); 908 *nextp->zn_tailp = pio; 909 nextp->zn_tailp = &pio->io_exec_next; 910 } else { 911 zio_taskq_dispatch(pio, type, B_FALSE); 912 } 913 } else { 914 mutex_exit(&pio->io_lock); 915 } 916 } 917 918 static void 919 zio_inherit_child_errors(zio_t *zio, enum zio_child c) 920 { 921 if (zio->io_child_error[c] != 0 && zio->io_error == 0) 922 zio->io_error = zio->io_child_error[c]; 923 } 924 925 int 926 zio_bookmark_compare(const void *x1, const void *x2) 927 { 928 const zio_t *z1 = x1; 929 const zio_t *z2 = x2; 930 const zbookmark_phys_t *zb1 = &z1->io_bookmark; 931 const zbookmark_phys_t *zb2 = &z2->io_bookmark; 932 933 int cmp = TREE_CMP(zb1->zb_objset, zb2->zb_objset); 934 if (cmp != 0) 935 return (cmp); 936 937 cmp = TREE_CMP(zb1->zb_object, zb2->zb_object); 938 if (cmp != 0) 939 return (cmp); 940 941 cmp = TREE_CMP(zb1->zb_level, zb2->zb_level); 942 if (cmp != 0) 943 return (cmp); 944 945 cmp = TREE_CMP(zb1->zb_blkid, zb2->zb_blkid); 946 if (cmp != 0) 947 return (cmp); 948 949 return (TREE_PCMP(z1, z2)); 950 } 951 952 /* 953 * ========================================================================== 954 * Create the various types of I/O (read, write, free, etc) 955 * ========================================================================== 956 */ 957 static zio_t * 958 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 959 abd_t *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done, 960 void *private, zio_type_t type, zio_priority_t priority, 961 zio_flag_t flags, vdev_t *vd, uint64_t offset, 962 const zbookmark_phys_t *zb, enum zio_stage stage, 963 enum zio_stage pipeline) 964 { 965 zio_t *zio; 966 967 IMPLY(type != ZIO_TYPE_TRIM, psize <= SPA_MAXBLOCKSIZE); 968 ASSERT0(P2PHASE(psize, SPA_MINBLOCKSIZE)); 969 ASSERT0(P2PHASE(offset, SPA_MINBLOCKSIZE)); 970 971 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER)); 972 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER)); 973 ASSERT(vd || stage == ZIO_STAGE_OPEN); 974 975 IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW_COMPRESS) != 0); 976 977 zio = kmem_cache_alloc(zio_cache, KM_SLEEP); 978 memset(zio, 0, sizeof (zio_t)); 979 980 mutex_init(&zio->io_lock, NULL, MUTEX_NOLOCKDEP, NULL); 981 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL); 982 983 list_create(&zio->io_parent_list, sizeof (zio_link_t), 984 offsetof(zio_link_t, zl_parent_node)); 985 list_create(&zio->io_child_list, sizeof (zio_link_t), 986 offsetof(zio_link_t, zl_child_node)); 987 metaslab_trace_init(ZIO_ALLOC_LIST(zio)); 988 989 if (vd != NULL) 990 zio->io_child_type = ZIO_CHILD_VDEV; 991 else if (flags & ZIO_FLAG_GANG_CHILD) 992 zio->io_child_type = ZIO_CHILD_GANG; 993 else if (flags & ZIO_FLAG_DDT_CHILD) 994 zio->io_child_type = ZIO_CHILD_DDT; 995 else 996 zio->io_child_type = ZIO_CHILD_LOGICAL; 997 998 if (bp != NULL) { 999 if (type != ZIO_TYPE_WRITE || 1000 zio->io_child_type == ZIO_CHILD_DDT) { 1001 zio->io_bp_copy = *bp; 1002 zio->io_bp = &zio->io_bp_copy; /* so caller can free */ 1003 } else { 1004 zio->io_bp = (blkptr_t *)bp; 1005 } 1006 zio->io_bp_orig = *bp; 1007 if (zio->io_child_type == ZIO_CHILD_LOGICAL) 1008 zio->io_logical = zio; 1009 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp)) 1010 pipeline |= ZIO_GANG_STAGES; 1011 if (flags & ZIO_FLAG_PREALLOCATED) { 1012 BP_ZERO_DVAS(zio->io_bp); 1013 BP_SET_BIRTH(zio->io_bp, 0, 0); 1014 } 1015 } 1016 1017 zio->io_spa = spa; 1018 zio->io_txg = txg; 1019 zio->io_done = done; 1020 zio->io_private = private; 1021 zio->io_type = type; 1022 zio->io_priority = priority; 1023 zio->io_vd = vd; 1024 zio->io_offset = offset; 1025 zio->io_orig_abd = zio->io_abd = data; 1026 zio->io_orig_size = zio->io_size = psize; 1027 zio->io_lsize = lsize; 1028 zio->io_orig_flags = zio->io_flags = flags; 1029 zio->io_orig_stage = zio->io_stage = stage; 1030 zio->io_orig_pipeline = zio->io_pipeline = pipeline; 1031 zio->io_pipeline_trace = ZIO_STAGE_OPEN; 1032 zio->io_allocator = ZIO_ALLOCATOR_NONE; 1033 1034 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY) || 1035 (pipeline & ZIO_STAGE_READY) == 0; 1036 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE); 1037 1038 if (zb != NULL) 1039 zio->io_bookmark = *zb; 1040 1041 if (pio != NULL) { 1042 zio->io_metaslab_class = pio->io_metaslab_class; 1043 if (zio->io_logical == NULL) 1044 zio->io_logical = pio->io_logical; 1045 if (zio->io_child_type == ZIO_CHILD_GANG) 1046 zio->io_gang_leader = pio->io_gang_leader; 1047 zio_add_child_first(pio, zio); 1048 } 1049 1050 taskq_init_ent(&zio->io_tqent); 1051 1052 return (zio); 1053 } 1054 1055 void 1056 zio_destroy(zio_t *zio) 1057 { 1058 ASSERT3P(zio->io_batch, ==, NULL); 1059 ASSERT3P(zio->io_child_batch, ==, NULL); 1060 ASSERT3P(zio->io_exec_next, ==, NULL); 1061 metaslab_trace_fini(ZIO_ALLOC_LIST(zio)); 1062 list_destroy(&zio->io_parent_list); 1063 list_destroy(&zio->io_child_list); 1064 mutex_destroy(&zio->io_lock); 1065 cv_destroy(&zio->io_cv); 1066 kmem_cache_free(zio_cache, zio); 1067 } 1068 1069 /* 1070 * ZIO intended to be between others. Provides synchronization at READY 1071 * and DONE pipeline stages and calls the respective callbacks. 1072 */ 1073 zio_t * 1074 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done, 1075 void *private, zio_flag_t flags) 1076 { 1077 zio_t *zio; 1078 1079 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private, 1080 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL, 1081 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE); 1082 1083 return (zio); 1084 } 1085 1086 /* 1087 * ZIO intended to be a root of a tree. Unlike null ZIO does not have a 1088 * READY pipeline stage (is ready on creation), so it should not be used 1089 * as child of any ZIO that may need waiting for grandchildren READY stage 1090 * (any other ZIO type). 1091 */ 1092 zio_t * 1093 zio_root(spa_t *spa, zio_done_func_t *done, void *private, zio_flag_t flags) 1094 { 1095 zio_t *zio; 1096 1097 zio = zio_create(NULL, spa, 0, NULL, NULL, 0, 0, done, private, 1098 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, NULL, 0, NULL, 1099 ZIO_STAGE_OPEN, ZIO_ROOT_PIPELINE); 1100 1101 return (zio); 1102 } 1103 1104 static int 1105 zfs_blkptr_verify_log(spa_t *spa, const blkptr_t *bp, 1106 enum blk_verify_flag blk_verify, const char *fmt, ...) 1107 { 1108 va_list adx; 1109 char buf[256]; 1110 1111 va_start(adx, fmt); 1112 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 1113 va_end(adx); 1114 1115 zfs_dbgmsg("bad blkptr at %px: " 1116 "DVA[0]=%#llx/%#llx " 1117 "DVA[1]=%#llx/%#llx " 1118 "DVA[2]=%#llx/%#llx " 1119 "prop=%#llx " 1120 "prop2=%#llx " 1121 "pad=%#llx " 1122 "phys_birth=%#llx " 1123 "birth=%#llx " 1124 "fill=%#llx " 1125 "cksum=%#llx/%#llx/%#llx/%#llx", 1126 bp, 1127 (long long)bp->blk_dva[0].dva_word[0], 1128 (long long)bp->blk_dva[0].dva_word[1], 1129 (long long)bp->blk_dva[1].dva_word[0], 1130 (long long)bp->blk_dva[1].dva_word[1], 1131 (long long)bp->blk_dva[2].dva_word[0], 1132 (long long)bp->blk_dva[2].dva_word[1], 1133 (long long)bp->blk_prop, 1134 (long long)bp->blk_prop2, 1135 (long long)bp->blk_pad, 1136 (long long)BP_GET_RAW_PHYSICAL_BIRTH(bp), 1137 (long long)BP_GET_LOGICAL_BIRTH(bp), 1138 (long long)bp->blk_fill, 1139 (long long)bp->blk_cksum.zc_word[0], 1140 (long long)bp->blk_cksum.zc_word[1], 1141 (long long)bp->blk_cksum.zc_word[2], 1142 (long long)bp->blk_cksum.zc_word[3]); 1143 switch (blk_verify) { 1144 case BLK_VERIFY_HALT: 1145 zfs_panic_recover("%s: %s", spa_name(spa), buf); 1146 break; 1147 case BLK_VERIFY_LOG: 1148 zfs_dbgmsg("%s: %s", spa_name(spa), buf); 1149 break; 1150 case BLK_VERIFY_ONLY: 1151 break; 1152 } 1153 1154 return (1); 1155 } 1156 1157 /* 1158 * Verify the block pointer fields contain reasonable values. This means 1159 * it only contains known object types, checksum/compression identifiers, 1160 * block sizes within the maximum allowed limits, valid DVAs, etc. 1161 * 1162 * If everything checks out 0 is returned. The zfs_blkptr_verify 1163 * argument controls the behavior when an invalid field is detected. 1164 * 1165 * Values for blk_verify_flag: 1166 * BLK_VERIFY_ONLY: evaluate the block 1167 * BLK_VERIFY_LOG: evaluate the block and log problems 1168 * BLK_VERIFY_HALT: call zfs_panic_recover on error 1169 * 1170 * Values for blk_config_flag: 1171 * BLK_CONFIG_HELD: caller holds SCL_VDEV for writer 1172 * BLK_CONFIG_NEEDED: caller holds no config lock, SCL_VDEV will be 1173 * obtained for reader 1174 * BLK_CONFIG_SKIP: skip checks which require SCL_VDEV, for better 1175 * performance 1176 */ 1177 int 1178 zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp, 1179 enum blk_config_flag blk_config, enum blk_verify_flag blk_verify) 1180 { 1181 int errors = 0; 1182 1183 if (unlikely(!DMU_OT_IS_VALID(BP_GET_TYPE(bp)))) { 1184 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1185 "blkptr at %px has invalid TYPE %llu", 1186 bp, (longlong_t)BP_GET_TYPE(bp)); 1187 } 1188 if (unlikely(BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS)) { 1189 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1190 "blkptr at %px has invalid COMPRESS %llu", 1191 bp, (longlong_t)BP_GET_COMPRESS(bp)); 1192 } 1193 if (unlikely(BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE)) { 1194 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1195 "blkptr at %px has invalid LSIZE %llu", 1196 bp, (longlong_t)BP_GET_LSIZE(bp)); 1197 } 1198 if (BP_IS_EMBEDDED(bp)) { 1199 if (unlikely(BPE_GET_ETYPE(bp) >= NUM_BP_EMBEDDED_TYPES)) { 1200 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1201 "blkptr at %px has invalid ETYPE %llu", 1202 bp, (longlong_t)BPE_GET_ETYPE(bp)); 1203 } 1204 if (unlikely(BPE_GET_PSIZE(bp) > BPE_PAYLOAD_SIZE)) { 1205 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1206 "blkptr at %px has invalid PSIZE %llu", 1207 bp, (longlong_t)BPE_GET_PSIZE(bp)); 1208 } 1209 return (errors ? ECKSUM : 0); 1210 } else if (BP_IS_HOLE(bp)) { 1211 /* 1212 * Holes are allowed (expected, even) to have no DVAs, no 1213 * checksum, and no psize. 1214 */ 1215 return (errors ? ECKSUM : 0); 1216 } else if (unlikely(!DVA_IS_VALID(&bp->blk_dva[0]))) { 1217 /* Non-hole, non-embedded BPs _must_ have at least one DVA */ 1218 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1219 "blkptr at %px has no valid DVAs", bp); 1220 } 1221 if (unlikely(BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS)) { 1222 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1223 "blkptr at %px has invalid CHECKSUM %llu", 1224 bp, (longlong_t)BP_GET_CHECKSUM(bp)); 1225 } 1226 if (unlikely(BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE)) { 1227 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1228 "blkptr at %px has invalid PSIZE %llu", 1229 bp, (longlong_t)BP_GET_PSIZE(bp)); 1230 } 1231 1232 /* 1233 * Do not verify individual DVAs if the config is not trusted. This 1234 * will be done once the zio is executed in vdev_mirror_map_alloc. 1235 */ 1236 if (unlikely(!spa->spa_trust_config)) 1237 return (errors ? ECKSUM : 0); 1238 1239 switch (blk_config) { 1240 case BLK_CONFIG_HELD: 1241 ASSERT(spa_config_held(spa, SCL_VDEV, RW_WRITER)); 1242 break; 1243 case BLK_CONFIG_NEEDED: 1244 spa_config_enter(spa, SCL_VDEV, bp, RW_READER); 1245 break; 1246 case BLK_CONFIG_NEEDED_TRY: 1247 if (!spa_config_tryenter(spa, SCL_VDEV, bp, RW_READER)) 1248 return (EBUSY); 1249 break; 1250 case BLK_CONFIG_SKIP: 1251 return (errors ? ECKSUM : 0); 1252 default: 1253 panic("invalid blk_config %u", blk_config); 1254 } 1255 1256 /* 1257 * Pool-specific checks. 1258 * 1259 * Note: it would be nice to verify that the logical birth 1260 * and physical birth are not too large. However, 1261 * spa_freeze() allows the birth time of log blocks (and 1262 * dmu_sync()-ed blocks that are in the log) to be arbitrarily 1263 * large. 1264 */ 1265 for (int i = 0; i < BP_GET_NDVAS(bp); i++) { 1266 const dva_t *dva = &bp->blk_dva[i]; 1267 uint64_t vdevid = DVA_GET_VDEV(dva); 1268 1269 if (unlikely(vdevid >= spa->spa_root_vdev->vdev_children)) { 1270 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1271 "blkptr at %px DVA %u has invalid VDEV %llu", 1272 bp, i, (longlong_t)vdevid); 1273 continue; 1274 } 1275 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid]; 1276 if (unlikely(vd == NULL)) { 1277 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1278 "blkptr at %px DVA %u has invalid VDEV %llu", 1279 bp, i, (longlong_t)vdevid); 1280 continue; 1281 } 1282 if (unlikely(vd->vdev_ops == &vdev_hole_ops)) { 1283 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1284 "blkptr at %px DVA %u has hole VDEV %llu", 1285 bp, i, (longlong_t)vdevid); 1286 continue; 1287 } 1288 if (vd->vdev_ops == &vdev_missing_ops) { 1289 /* 1290 * "missing" vdevs are valid during import, but we 1291 * don't have their detailed info (e.g. asize), so 1292 * we can't perform any more checks on them. 1293 */ 1294 continue; 1295 } 1296 uint64_t offset = DVA_GET_OFFSET(dva); 1297 uint64_t asize = DVA_GET_ASIZE(dva); 1298 if (DVA_GET_GANG(dva)) 1299 asize = vdev_gang_header_asize(vd); 1300 if (unlikely(offset + asize > vd->vdev_asize)) { 1301 errors += zfs_blkptr_verify_log(spa, bp, blk_verify, 1302 "blkptr at %px DVA %u has invalid OFFSET %llu", 1303 bp, i, (longlong_t)offset); 1304 } 1305 } 1306 if (blk_config == BLK_CONFIG_NEEDED || blk_config == 1307 BLK_CONFIG_NEEDED_TRY) 1308 spa_config_exit(spa, SCL_VDEV, bp); 1309 1310 return (errors ? ECKSUM : 0); 1311 } 1312 1313 boolean_t 1314 zfs_dva_valid(spa_t *spa, const dva_t *dva, const blkptr_t *bp) 1315 { 1316 (void) bp; 1317 uint64_t vdevid = DVA_GET_VDEV(dva); 1318 1319 if (vdevid >= spa->spa_root_vdev->vdev_children) 1320 return (B_FALSE); 1321 1322 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid]; 1323 if (vd == NULL) 1324 return (B_FALSE); 1325 1326 if (vd->vdev_ops == &vdev_hole_ops) 1327 return (B_FALSE); 1328 1329 if (vd->vdev_ops == &vdev_missing_ops) { 1330 return (B_FALSE); 1331 } 1332 1333 uint64_t offset = DVA_GET_OFFSET(dva); 1334 uint64_t asize = DVA_GET_ASIZE(dva); 1335 1336 if (DVA_GET_GANG(dva)) 1337 asize = vdev_gang_header_asize(vd); 1338 if (offset + asize > vd->vdev_asize) 1339 return (B_FALSE); 1340 1341 return (B_TRUE); 1342 } 1343 1344 zio_t * 1345 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, 1346 abd_t *data, uint64_t size, zio_done_func_t *done, void *private, 1347 zio_priority_t priority, zio_flag_t flags, const zbookmark_phys_t *zb) 1348 { 1349 zio_t *zio; 1350 1351 zio = zio_create(pio, spa, BP_GET_PHYSICAL_BIRTH(bp), bp, 1352 data, size, size, done, private, 1353 ZIO_TYPE_READ, priority, flags, NULL, 0, zb, 1354 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ? 1355 ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE); 1356 1357 return (zio); 1358 } 1359 1360 zio_t * 1361 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 1362 abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp, 1363 zio_done_func_t *ready, zio_done_func_t *children_ready, 1364 zio_done_func_t *done, void *private, zio_priority_t priority, 1365 zio_flag_t flags, const zbookmark_phys_t *zb) 1366 { 1367 zio_t *zio; 1368 enum zio_stage pipeline = zp->zp_direct_write == B_TRUE ? 1369 ZIO_DIRECT_WRITE_PIPELINE : (flags & ZIO_FLAG_DDT_CHILD) ? 1370 ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE; 1371 1372 1373 zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private, 1374 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb, 1375 ZIO_STAGE_OPEN, pipeline); 1376 1377 zio->io_ready = ready; 1378 zio->io_children_ready = children_ready; 1379 zio->io_prop = *zp; 1380 1381 /* 1382 * Data can be NULL if we are going to call zio_write_override() to 1383 * provide the already-allocated BP. But we may need the data to 1384 * verify a dedup hit (if requested). In this case, don't try to 1385 * dedup (just take the already-allocated BP verbatim). Encrypted 1386 * dedup blocks need data as well so we also disable dedup in this 1387 * case. 1388 */ 1389 if (data == NULL && 1390 (zio->io_prop.zp_dedup_verify || zio->io_prop.zp_encrypt)) { 1391 zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE; 1392 } 1393 1394 return (zio); 1395 } 1396 1397 zio_t * 1398 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data, 1399 uint64_t size, zio_done_func_t *done, void *private, 1400 zio_priority_t priority, zio_flag_t flags, zbookmark_phys_t *zb) 1401 { 1402 zio_t *zio; 1403 1404 zio = zio_create(pio, spa, txg, bp, data, size, size, done, private, 1405 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb, 1406 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE); 1407 1408 return (zio); 1409 } 1410 1411 void 1412 zio_write_override(zio_t *zio, blkptr_t *bp, int copies, int gang_copies, 1413 boolean_t nopwrite, boolean_t brtwrite) 1414 { 1415 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 1416 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1417 ASSERT(zio->io_stage == ZIO_STAGE_OPEN); 1418 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa)); 1419 ASSERT(!brtwrite || !nopwrite); 1420 1421 /* 1422 * We must reset the io_prop to match the values that existed 1423 * when the bp was first written by dmu_sync() keeping in mind 1424 * that nopwrite and dedup are mutually exclusive. 1425 */ 1426 zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup; 1427 zio->io_prop.zp_nopwrite = nopwrite; 1428 zio->io_prop.zp_brtwrite = brtwrite; 1429 zio->io_prop.zp_copies = copies; 1430 zio->io_prop.zp_gang_copies = gang_copies; 1431 zio->io_bp_override = bp; 1432 } 1433 1434 void 1435 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp) 1436 { 1437 1438 (void) zfs_blkptr_verify(spa, bp, BLK_CONFIG_NEEDED, BLK_VERIFY_HALT); 1439 1440 /* 1441 * The check for EMBEDDED is a performance optimization. We 1442 * process the free here (by ignoring it) rather than 1443 * putting it on the list and then processing it in zio_free_sync(). 1444 */ 1445 if (BP_IS_EMBEDDED(bp)) 1446 return; 1447 1448 /* 1449 * Frees that are for the currently-syncing txg, are not going to be 1450 * deferred, and which will not need to do a read (i.e. not GANG or 1451 * DEDUP), can be processed immediately. Otherwise, put them on the 1452 * in-memory list for later processing. 1453 * 1454 * Note that we only defer frees after zfs_sync_pass_deferred_free 1455 * when the log space map feature is disabled. [see relevant comment 1456 * in spa_sync_iterate_to_convergence()] 1457 */ 1458 if (BP_IS_GANG(bp) || 1459 BP_GET_DEDUP(bp) || 1460 txg != spa->spa_syncing_txg || 1461 (spa_sync_pass(spa) >= zfs_sync_pass_deferred_free && 1462 !spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) || 1463 brt_maybe_exists(spa, bp)) { 1464 metaslab_check_free(spa, bp); 1465 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp); 1466 } else { 1467 VERIFY0P(zio_free_sync(NULL, spa, txg, bp, 0)); 1468 } 1469 } 1470 1471 /* 1472 * To improve performance, this function may return NULL if we were able 1473 * to do the free immediately. This avoids the cost of creating a zio 1474 * (and linking it to the parent, etc). 1475 */ 1476 zio_t * 1477 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 1478 zio_flag_t flags) 1479 { 1480 ASSERT(!BP_IS_HOLE(bp)); 1481 ASSERT(spa_syncing_txg(spa) == txg); 1482 1483 if (BP_IS_EMBEDDED(bp)) 1484 return (NULL); 1485 1486 metaslab_check_free(spa, bp); 1487 arc_freed(spa, bp); 1488 dsl_scan_freed(spa, bp); 1489 1490 if (BP_IS_GANG(bp) || 1491 BP_GET_DEDUP(bp) || 1492 brt_maybe_exists(spa, bp)) { 1493 /* 1494 * GANG, DEDUP and BRT blocks can induce a read (for the gang 1495 * block header, the DDT or the BRT), so issue them 1496 * asynchronously so that this thread is not tied up. 1497 */ 1498 enum zio_stage stage = 1499 ZIO_FREE_PIPELINE | ZIO_STAGE_ISSUE_ASYNC; 1500 1501 return (zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp), 1502 BP_GET_PSIZE(bp), NULL, NULL, 1503 ZIO_TYPE_FREE, ZIO_PRIORITY_NOW, 1504 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage)); 1505 } else { 1506 metaslab_free(spa, bp, txg, B_FALSE); 1507 return (NULL); 1508 } 1509 } 1510 1511 zio_t * 1512 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 1513 zio_done_func_t *done, void *private, zio_flag_t flags) 1514 { 1515 zio_t *zio; 1516 1517 (void) zfs_blkptr_verify(spa, bp, (flags & ZIO_FLAG_CONFIG_WRITER) ? 1518 BLK_CONFIG_HELD : BLK_CONFIG_NEEDED, BLK_VERIFY_HALT); 1519 1520 if (BP_IS_EMBEDDED(bp)) 1521 return (zio_null(pio, spa, NULL, NULL, NULL, 0)); 1522 1523 /* 1524 * A claim is an allocation of a specific block. Claims are needed 1525 * to support immediate writes in the intent log. The issue is that 1526 * immediate writes contain committed data, but in a txg that was 1527 * *not* committed. Upon opening the pool after an unclean shutdown, 1528 * the intent log claims all blocks that contain immediate write data 1529 * so that the SPA knows they're in use. 1530 * 1531 * All claims *must* be resolved in the first txg -- before the SPA 1532 * starts allocating blocks -- so that nothing is allocated twice. 1533 * If txg == 0 we just verify that the block is claimable. 1534 */ 1535 ASSERT3U(BP_GET_LOGICAL_BIRTH(&spa->spa_uberblock.ub_rootbp), <, 1536 spa_min_claim_txg(spa)); 1537 ASSERT(txg == spa_min_claim_txg(spa) || txg == 0); 1538 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(8) */ 1539 1540 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp), 1541 BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, 1542 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE); 1543 ASSERT0(zio->io_queued_timestamp); 1544 1545 return (zio); 1546 } 1547 1548 zio_t * 1549 zio_trim(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 1550 zio_done_func_t *done, void *private, zio_priority_t priority, 1551 zio_flag_t flags, enum trim_flag trim_flags) 1552 { 1553 zio_t *zio; 1554 1555 ASSERT0(vd->vdev_children); 1556 ASSERT0(P2PHASE(offset, 1ULL << vd->vdev_ashift)); 1557 ASSERT0(P2PHASE(size, 1ULL << vd->vdev_ashift)); 1558 ASSERT3U(size, !=, 0); 1559 1560 zio = zio_create(pio, vd->vdev_spa, 0, NULL, NULL, size, size, done, 1561 private, ZIO_TYPE_TRIM, priority, flags | ZIO_FLAG_PHYSICAL, 1562 vd, offset, NULL, ZIO_STAGE_OPEN, ZIO_TRIM_PIPELINE); 1563 zio->io_trim_flags = trim_flags; 1564 1565 return (zio); 1566 } 1567 1568 zio_t * 1569 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 1570 abd_t *data, int checksum, zio_done_func_t *done, void *private, 1571 zio_priority_t priority, zio_flag_t flags, boolean_t labels) 1572 { 1573 zio_t *zio; 1574 1575 ASSERT0(vd->vdev_children); 1576 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE || 1577 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 1578 ASSERT3U(offset + size, <=, vd->vdev_psize); 1579 1580 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done, 1581 private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd, 1582 offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE); 1583 1584 zio->io_prop.zp_checksum = checksum; 1585 1586 return (zio); 1587 } 1588 1589 zio_t * 1590 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 1591 abd_t *data, int checksum, zio_done_func_t *done, void *private, 1592 zio_priority_t priority, zio_flag_t flags, boolean_t labels) 1593 { 1594 zio_t *zio; 1595 1596 ASSERT0(vd->vdev_children); 1597 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE || 1598 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 1599 ASSERT3U(offset + size, <=, vd->vdev_psize); 1600 1601 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done, 1602 private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd, 1603 offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE); 1604 1605 zio->io_prop.zp_checksum = checksum; 1606 1607 if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) { 1608 /* 1609 * zec checksums are necessarily destructive -- they modify 1610 * the end of the write buffer to hold the verifier/checksum. 1611 * Therefore, we must make a local copy in case the data is 1612 * being written to multiple places in parallel. 1613 */ 1614 abd_t *wbuf = abd_alloc_sametype(data, size); 1615 abd_copy(wbuf, data, size); 1616 1617 zio_push_transform(zio, wbuf, size, size, NULL); 1618 } 1619 1620 return (zio); 1621 } 1622 1623 /* 1624 * Create a child I/O to do some work for us. 1625 */ 1626 zio_t * 1627 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset, 1628 abd_t *data, uint64_t size, int type, zio_priority_t priority, 1629 zio_flag_t flags, zio_done_func_t *done, void *private) 1630 { 1631 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE; 1632 zio_t *zio; 1633 1634 /* 1635 * vdev child I/Os do not propagate their error to the parent. 1636 * Therefore, for correct operation the caller *must* check for 1637 * and handle the error in the child i/o's done callback. 1638 * The only exceptions are i/os that we don't care about 1639 * (OPTIONAL or REPAIR). 1640 */ 1641 ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) || 1642 done != NULL); 1643 1644 if (type == ZIO_TYPE_READ && bp != NULL) { 1645 /* 1646 * If we have the bp, then the child should perform the 1647 * checksum and the parent need not. This pushes error 1648 * detection as close to the leaves as possible and 1649 * eliminates redundant checksums in the interior nodes. 1650 */ 1651 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY; 1652 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY; 1653 /* 1654 * We never allow the mirror VDEV to attempt reading from any 1655 * additional data copies after the first Direct I/O checksum 1656 * verify failure. This is to avoid bad data being written out 1657 * through the mirror during self healing. See comment in 1658 * vdev_mirror_io_done() for more details. 1659 */ 1660 ASSERT0(pio->io_post & ZIO_POST_DIO_CHKSUM_ERR); 1661 } else if (type == ZIO_TYPE_WRITE && 1662 pio->io_prop.zp_direct_write == B_TRUE) { 1663 /* 1664 * By default we only will verify checksums for Direct I/O 1665 * writes for Linux. FreeBSD is able to place user pages under 1666 * write protection before issuing them to the ZIO pipeline. 1667 * 1668 * Checksum validation errors will only be reported through 1669 * the top-level VDEV, which is set by this child ZIO. 1670 */ 1671 ASSERT3P(bp, !=, NULL); 1672 ASSERT3U(pio->io_child_type, ==, ZIO_CHILD_LOGICAL); 1673 pipeline |= ZIO_STAGE_DIO_CHECKSUM_VERIFY; 1674 } 1675 1676 if (vd->vdev_ops->vdev_op_leaf) { 1677 ASSERT0(vd->vdev_children); 1678 offset += VDEV_LABEL_START_SIZE; 1679 } 1680 1681 flags |= ZIO_VDEV_CHILD_FLAGS(pio); 1682 1683 /* 1684 * If we've decided to do a repair, the write is not speculative -- 1685 * even if the original read was. Rebuild is an exception since we 1686 * cannot always ensure its data integrity. 1687 */ 1688 if ((flags & ZIO_FLAG_IO_REPAIR) && 1689 pio->io_priority != ZIO_PRIORITY_REBUILD) 1690 flags &= ~ZIO_FLAG_SPECULATIVE; 1691 1692 /* 1693 * If we're creating a child I/O that is not associated with a 1694 * top-level vdev, then the child zio is not an allocating I/O. 1695 * If this is a retried I/O then we ignore it since we will 1696 * have already processed the original allocating I/O. 1697 */ 1698 if (flags & ZIO_FLAG_ALLOC_THROTTLED && 1699 (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY)) && 1700 type == ZIO_TYPE_WRITE) { 1701 ASSERT(pio->io_metaslab_class != NULL); 1702 ASSERT(pio->io_metaslab_class->mc_alloc_throttle_enabled); 1703 ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE); 1704 ASSERT(!(flags & ZIO_FLAG_IO_REPAIR)); 1705 ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) || 1706 pio->io_child_type == ZIO_CHILD_GANG); 1707 1708 flags &= ~ZIO_FLAG_ALLOC_THROTTLED; 1709 } 1710 1711 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size, 1712 done, private, type, priority, flags, vd, offset, &pio->io_bookmark, 1713 ZIO_STAGE_VDEV_IO_START >> 1, pipeline); 1714 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV); 1715 1716 if (pio->io_child_batch != NULL) { 1717 /* 1718 * Whatever wakes this child up, all it has left to do are the 1719 * few cheap stages of ZIO_VDEV_CHILD_PIPELINE, so it is better 1720 * run right there than dispatched. 1721 */ 1722 zio->io_flags |= ZIO_FLAG_LIGHTWEIGHT; 1723 1724 /* 1725 * Only children that come back from the block layer gain 1726 * anything from a batch. Interior ones are dispatched by their 1727 * own child's zio_notify_parent() instead, as are distributed 1728 * spares, which are leaves that issue children of their own. 1729 * The scheduler may change before a queue slot is actually 1730 * taken, so vdev_should_queue_io() here only keeps the batch 1731 * away from vdevs that can never use it; the binding decision 1732 * is vdev_queue_io()'s. 1733 */ 1734 if (vd->vdev_ops->vdev_op_leaf && 1735 vd->vdev_ops != &vdev_draid_spare_ops && 1736 !vdev_should_queue_io(zio)) { 1737 /* 1738 * The batch is dispatched to the taskq chosen for 1739 * whichever member arrives last, so they all have to 1740 * choose the same one. The flags that steer the choice 1741 * are vdev-inherited, and type and priority come from 1742 * the parent at every call site. 1743 */ 1744 ASSERT3U(zio->io_type, ==, pio->io_type); 1745 ASSERT3U(zio->io_priority, ==, pio->io_priority); 1746 zio_batch_join(pio->io_child_batch, zio); 1747 } 1748 } 1749 1750 return (zio); 1751 } 1752 1753 zio_t * 1754 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, abd_t *data, uint64_t size, 1755 zio_type_t type, zio_priority_t priority, zio_flag_t flags, 1756 zio_done_func_t *done, void *private) 1757 { 1758 zio_t *zio; 1759 1760 ASSERT(vd->vdev_ops->vdev_op_leaf); 1761 1762 zio = zio_create(NULL, vd->vdev_spa, 0, NULL, 1763 data, size, size, done, private, type, priority, 1764 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED, 1765 vd, offset, NULL, 1766 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE); 1767 1768 return (zio); 1769 } 1770 1771 1772 /* 1773 * Send a flush command to the given vdev. Unlike most zio creation functions, 1774 * the flush zios are issued immediately. You can wait on pio to pause until 1775 * the flushes complete. 1776 */ 1777 void 1778 zio_flush(zio_t *pio, vdev_t *vd) 1779 { 1780 const zio_flag_t flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | 1781 ZIO_FLAG_DONT_RETRY; 1782 1783 if (vd->vdev_nowritecache) 1784 return; 1785 1786 if (vd->vdev_children == 0) { 1787 /* 1788 * A non-concrete vdev (a hole or indirect vdev left behind 1789 * by removing a log or data device) has no leaf device to 1790 * flush. Skip it; issuing a flush to an indirect vdev would 1791 * trip the ZIO_TYPE_WRITE assertion in 1792 * vdev_indirect_io_start(). 1793 */ 1794 if (!vdev_is_concrete(vd)) 1795 return; 1796 zio_nowait(zio_create(pio, vd->vdev_spa, 0, NULL, NULL, 0, 0, 1797 NULL, NULL, ZIO_TYPE_FLUSH, ZIO_PRIORITY_NOW, flags, vd, 0, 1798 NULL, ZIO_STAGE_OPEN, ZIO_FLUSH_PIPELINE)); 1799 } else { 1800 for (uint64_t c = 0; c < vd->vdev_children; c++) 1801 zio_flush(pio, vd->vdev_child[c]); 1802 } 1803 } 1804 1805 void 1806 zio_shrink(zio_t *zio, uint64_t size) 1807 { 1808 ASSERT0P(zio->io_executor); 1809 ASSERT3U(zio->io_orig_size, ==, zio->io_size); 1810 ASSERT3U(size, <=, zio->io_size); 1811 1812 /* 1813 * We don't shrink for raidz because of problems with the 1814 * reconstruction when reading back less than the block size. 1815 * Note, BP_IS_RAIDZ() assumes no compression. 1816 */ 1817 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 1818 if (!BP_IS_RAIDZ(zio->io_bp)) { 1819 /* we are not doing a raw write */ 1820 ASSERT3U(zio->io_size, ==, zio->io_lsize); 1821 zio->io_orig_size = zio->io_size = zio->io_lsize = size; 1822 } 1823 } 1824 1825 /* 1826 * Round provided allocation size up to a value that can be allocated 1827 * by at least some vdev(s) in the pool with minimum or no additional 1828 * padding and without extra space usage on others 1829 */ 1830 static uint64_t 1831 zio_roundup_alloc_size(spa_t *spa, uint64_t size) 1832 { 1833 if (size > spa->spa_min_alloc) 1834 return (roundup(size, spa->spa_gcd_alloc)); 1835 return (spa->spa_min_alloc); 1836 } 1837 1838 size_t 1839 zio_get_compression_max_size(enum zio_compress compress, uint64_t gcd_alloc, 1840 uint64_t min_alloc, size_t s_len) 1841 { 1842 size_t d_len; 1843 1844 /* minimum 12.5% must be saved (legacy value, may be changed later) */ 1845 d_len = s_len - (s_len >> 3); 1846 1847 /* ZLE can't use exactly d_len bytes, it needs more, so ignore it */ 1848 if (compress == ZIO_COMPRESS_ZLE) 1849 return (d_len); 1850 1851 d_len = d_len - d_len % gcd_alloc; 1852 1853 if (d_len < min_alloc) 1854 return (BPE_PAYLOAD_SIZE); 1855 return (d_len); 1856 } 1857 1858 /* 1859 * ========================================================================== 1860 * Prepare to read and write logical blocks 1861 * ========================================================================== 1862 */ 1863 1864 static zio_t * 1865 zio_read_bp_init(zio_t *zio) 1866 { 1867 blkptr_t *bp = zio->io_bp; 1868 uint64_t psize = 1869 BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp); 1870 1871 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy); 1872 1873 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF && 1874 zio->io_child_type == ZIO_CHILD_LOGICAL && 1875 !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) { 1876 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize), 1877 psize, psize, zio_decompress); 1878 } 1879 1880 if (((BP_IS_PROTECTED(bp) && !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) || 1881 BP_HAS_INDIRECT_MAC_CKSUM(bp)) && 1882 zio->io_child_type == ZIO_CHILD_LOGICAL) { 1883 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize), 1884 psize, psize, zio_decrypt); 1885 } 1886 1887 if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) { 1888 int psize = BPE_GET_PSIZE(bp); 1889 void *data = abd_borrow_buf(zio->io_abd, psize); 1890 1891 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1892 decode_embedded_bp_compressed(bp, data); 1893 abd_return_buf_copy(zio->io_abd, data, psize); 1894 } else { 1895 ASSERT(!BP_IS_EMBEDDED(bp)); 1896 } 1897 1898 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL) 1899 zio->io_pipeline = ZIO_DDT_READ_PIPELINE; 1900 1901 return (zio); 1902 } 1903 1904 static zio_t * 1905 zio_write_bp_init(zio_t *zio) 1906 { 1907 if (!IO_IS_ALLOCATING(zio)) 1908 return (zio); 1909 1910 ASSERT(zio->io_child_type != ZIO_CHILD_DDT); 1911 1912 if (zio->io_bp_override) { 1913 blkptr_t *bp = zio->io_bp; 1914 zio_prop_t *zp = &zio->io_prop; 1915 1916 ASSERT(BP_GET_BIRTH(bp) != zio->io_txg); 1917 1918 *bp = *zio->io_bp_override; 1919 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1920 1921 if (zp->zp_brtwrite) 1922 return (zio); 1923 1924 ASSERT(!BP_GET_DEDUP(zio->io_bp_override)); 1925 1926 if (BP_IS_EMBEDDED(bp)) 1927 return (zio); 1928 1929 /* 1930 * If we've been overridden and nopwrite is set then 1931 * set the flag accordingly to indicate that a nopwrite 1932 * has already occurred. 1933 */ 1934 if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) { 1935 ASSERT(!zp->zp_dedup); 1936 ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum); 1937 zio->io_flags |= ZIO_FLAG_NOPWRITE; 1938 return (zio); 1939 } 1940 1941 ASSERT(!zp->zp_nopwrite); 1942 1943 if (BP_IS_HOLE(bp) || !zp->zp_dedup) 1944 return (zio); 1945 1946 ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags & 1947 ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify); 1948 1949 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum && 1950 !zp->zp_encrypt) { 1951 BP_SET_DEDUP(bp, 1); 1952 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE; 1953 return (zio); 1954 } 1955 1956 /* 1957 * We were unable to handle this as an override bp, treat 1958 * it as a regular write I/O. 1959 */ 1960 zio->io_bp_override = NULL; 1961 *bp = zio->io_bp_orig; 1962 zio->io_pipeline = zio->io_orig_pipeline; 1963 } 1964 1965 return (zio); 1966 } 1967 1968 static zio_t * 1969 zio_write_compress(zio_t *zio) 1970 { 1971 spa_t *spa = zio->io_spa; 1972 zio_prop_t *zp = &zio->io_prop; 1973 enum zio_compress compress = zp->zp_compress; 1974 blkptr_t *bp = zio->io_bp; 1975 uint64_t lsize = zio->io_lsize; 1976 uint64_t psize = zio->io_size; 1977 uint32_t pass = 1; 1978 1979 /* 1980 * If our children haven't all reached the ready stage, 1981 * wait for them and then repeat this pipeline stage. 1982 */ 1983 if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT | 1984 ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) { 1985 return (NULL); 1986 } 1987 1988 if (!IO_IS_ALLOCATING(zio)) 1989 return (zio); 1990 1991 if (zio->io_children_ready != NULL) { 1992 /* 1993 * Now that all our children are ready, run the callback 1994 * associated with this zio in case it wants to modify the 1995 * data to be written. 1996 */ 1997 ASSERT3U(zp->zp_level, >, 0); 1998 zio->io_children_ready(zio); 1999 } 2000 2001 ASSERT(zio->io_child_type != ZIO_CHILD_DDT); 2002 ASSERT0P(zio->io_bp_override); 2003 2004 if (!BP_IS_HOLE(bp) && BP_GET_BIRTH(bp) == zio->io_txg) { 2005 /* 2006 * We're rewriting an existing block, which means we're 2007 * working on behalf of spa_sync(). For spa_sync() to 2008 * converge, it must eventually be the case that we don't 2009 * have to allocate new blocks. But compression changes 2010 * the blocksize, which forces a reallocate, and makes 2011 * convergence take longer. Therefore, after the first 2012 * few passes, stop compressing to ensure convergence. 2013 */ 2014 pass = spa_sync_pass(spa); 2015 2016 ASSERT(zio->io_txg == spa_syncing_txg(spa)); 2017 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2018 ASSERT(!BP_GET_DEDUP(bp)); 2019 2020 if (pass >= zfs_sync_pass_dont_compress) 2021 compress = ZIO_COMPRESS_OFF; 2022 2023 /* Make sure someone doesn't change their mind on overwrites */ 2024 ASSERT(BP_IS_EMBEDDED(bp) || BP_IS_GANG(bp) || 2025 MIN(zp->zp_copies, spa_max_replication(spa)) 2026 == BP_GET_NDVAS(bp)); 2027 } 2028 2029 /* If it's a compressed write that is not raw, compress the buffer. */ 2030 if (compress != ZIO_COMPRESS_OFF && 2031 !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) { 2032 abd_t *cabd = NULL; 2033 if (abd_cmp_zero(zio->io_abd, lsize) == 0) 2034 psize = 0; 2035 else if (compress == ZIO_COMPRESS_EMPTY) 2036 psize = lsize; 2037 else 2038 psize = zio_compress_data(compress, zio->io_abd, &cabd, 2039 lsize, 2040 zio_get_compression_max_size(compress, 2041 spa->spa_gcd_alloc, spa->spa_min_alloc, lsize), 2042 zp->zp_complevel); 2043 if (psize == 0) { 2044 compress = ZIO_COMPRESS_OFF; 2045 } else if (psize >= lsize) { 2046 compress = ZIO_COMPRESS_OFF; 2047 if (cabd != NULL) 2048 abd_free(cabd); 2049 } else if (psize <= BPE_PAYLOAD_SIZE && !zp->zp_encrypt && 2050 zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) && 2051 spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) { 2052 void *cbuf = abd_borrow_buf_copy(cabd, lsize); 2053 encode_embedded_bp_compressed(bp, 2054 cbuf, compress, lsize, psize); 2055 BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA); 2056 BP_SET_TYPE(bp, zio->io_prop.zp_type); 2057 BP_SET_LEVEL(bp, zio->io_prop.zp_level); 2058 abd_return_buf(cabd, cbuf, lsize); 2059 abd_free(cabd); 2060 BP_SET_LOGICAL_BIRTH(bp, zio->io_txg); 2061 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 2062 ASSERT(spa_feature_is_active(spa, 2063 SPA_FEATURE_EMBEDDED_DATA)); 2064 return (zio); 2065 } else { 2066 /* 2067 * Round compressed size up to the minimum allocation 2068 * size of the smallest-ashift device, and zero the 2069 * tail. This ensures that the compressed size of the 2070 * BP (and thus compressratio property) are correct, 2071 * in that we charge for the padding used to fill out 2072 * the last sector. 2073 */ 2074 size_t rounded = (size_t)zio_roundup_alloc_size(spa, 2075 psize); 2076 if (rounded >= lsize) { 2077 compress = ZIO_COMPRESS_OFF; 2078 abd_free(cabd); 2079 psize = lsize; 2080 } else { 2081 abd_zero_off(cabd, psize, rounded - psize); 2082 psize = rounded; 2083 zio_push_transform(zio, cabd, 2084 psize, lsize, NULL); 2085 } 2086 } 2087 2088 /* 2089 * We were unable to handle this as an override bp, treat 2090 * it as a regular write I/O. 2091 */ 2092 zio->io_bp_override = NULL; 2093 *bp = zio->io_bp_orig; 2094 zio->io_pipeline = zio->io_orig_pipeline; 2095 2096 } else if ((zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) != 0 && 2097 zp->zp_type == DMU_OT_DNODE) { 2098 /* 2099 * The DMU actually relies on the zio layer's compression 2100 * to free metadnode blocks that have had all contained 2101 * dnodes freed. As a result, even when doing a raw 2102 * receive, we must check whether the block can be compressed 2103 * to a hole. 2104 */ 2105 if (abd_cmp_zero(zio->io_abd, lsize) == 0) { 2106 psize = 0; 2107 compress = ZIO_COMPRESS_OFF; 2108 } else { 2109 psize = lsize; 2110 } 2111 } else if (zio->io_flags & ZIO_FLAG_RAW_COMPRESS && 2112 !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) { 2113 /* 2114 * If we are raw receiving an encrypted dataset we should not 2115 * take this codepath because it will change the on-disk block 2116 * and decryption will fail. 2117 */ 2118 size_t rounded = MIN((size_t)zio_roundup_alloc_size(spa, psize), 2119 lsize); 2120 2121 if (rounded != psize) { 2122 abd_t *cdata = abd_alloc_linear(rounded, B_TRUE); 2123 abd_zero_off(cdata, psize, rounded - psize); 2124 abd_copy_off(cdata, zio->io_abd, 0, 0, psize); 2125 psize = rounded; 2126 zio_push_transform(zio, cdata, 2127 psize, rounded, NULL); 2128 } 2129 } else { 2130 ASSERT3U(psize, !=, 0); 2131 } 2132 2133 /* 2134 * The final pass of spa_sync() must be all rewrites, but the first 2135 * few passes offer a trade-off: allocating blocks defers convergence, 2136 * but newly allocated blocks are sequential, so they can be written 2137 * to disk faster. Therefore, we allow the first few passes of 2138 * spa_sync() to allocate new blocks, but force rewrites after that. 2139 * There should only be a handful of blocks after pass 1 in any case. 2140 */ 2141 if (!BP_IS_HOLE(bp) && BP_GET_BIRTH(bp) == zio->io_txg && 2142 BP_GET_PSIZE(bp) == psize && 2143 pass >= zfs_sync_pass_rewrite) { 2144 VERIFY3U(psize, !=, 0); 2145 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES; 2146 2147 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages; 2148 zio->io_flags |= ZIO_FLAG_IO_REWRITE; 2149 } else { 2150 BP_ZERO(bp); 2151 zio->io_pipeline = ZIO_WRITE_PIPELINE; 2152 } 2153 2154 if (psize == 0) { 2155 if (BP_GET_LOGICAL_BIRTH(&zio->io_bp_orig) != 0 && 2156 spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) { 2157 BP_SET_LSIZE(bp, lsize); 2158 BP_SET_TYPE(bp, zp->zp_type); 2159 BP_SET_LEVEL(bp, zp->zp_level); 2160 BP_SET_BIRTH(bp, zio->io_txg, 0); 2161 } 2162 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 2163 } else { 2164 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER); 2165 BP_SET_LSIZE(bp, lsize); 2166 BP_SET_TYPE(bp, zp->zp_type); 2167 BP_SET_LEVEL(bp, zp->zp_level); 2168 BP_SET_PSIZE(bp, psize); 2169 BP_SET_COMPRESS(bp, compress); 2170 BP_SET_CHECKSUM(bp, zp->zp_checksum); 2171 BP_SET_DEDUP(bp, zp->zp_dedup); 2172 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 2173 if (zp->zp_dedup) { 2174 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2175 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 2176 ASSERT(!zp->zp_encrypt || 2177 DMU_OT_IS_ENCRYPTED(zp->zp_type)); 2178 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE; 2179 } 2180 if (zp->zp_nopwrite) { 2181 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2182 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 2183 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE; 2184 } 2185 } 2186 return (zio); 2187 } 2188 2189 static zio_t * 2190 zio_free_bp_init(zio_t *zio) 2191 { 2192 blkptr_t *bp = zio->io_bp; 2193 2194 if (zio->io_child_type == ZIO_CHILD_LOGICAL) { 2195 if (BP_GET_DEDUP(bp)) 2196 /* 2197 * Keep the gang stages zio_create() added: if 2198 * zio_ddt_free() falls back to a plain free, they 2199 * free the gang members along with the header. 2200 */ 2201 zio->io_pipeline |= ZIO_DDT_FREE_PIPELINE; 2202 } 2203 2204 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy); 2205 2206 return (zio); 2207 } 2208 2209 /* 2210 * ========================================================================== 2211 * Execute the I/O pipeline 2212 * ========================================================================== 2213 */ 2214 2215 static void 2216 zio_taskq_dispatch_func(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline, 2217 task_func_t *func) 2218 { 2219 spa_t *spa = zio->io_spa; 2220 zio_type_t t = zio->io_type; 2221 2222 /* 2223 * If we're a config writer or a probe, the normal issue and 2224 * interrupt threads may all be blocked waiting for the config lock. 2225 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL. 2226 */ 2227 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE)) 2228 t = ZIO_TYPE_NULL; 2229 2230 /* 2231 * A similar issue exists for the L2ARC write thread until L2ARC 2.0. 2232 */ 2233 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux) 2234 t = ZIO_TYPE_NULL; 2235 2236 /* 2237 * If this is a high priority I/O, then use the high priority taskq if 2238 * available or cut the line otherwise. 2239 */ 2240 if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) { 2241 if (spa->spa_zio_taskq[t][q + 1].stqs_count != 0) 2242 q++; 2243 else 2244 cutinline = B_TRUE; 2245 } 2246 2247 ASSERT3U(q, <, ZIO_TASKQ_TYPES); 2248 2249 spa_taskq_dispatch(spa, t, q, func, zio, cutinline); 2250 } 2251 2252 static void 2253 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline) 2254 { 2255 zio_taskq_dispatch_func(zio, q, cutinline, zio_execute); 2256 } 2257 2258 static boolean_t 2259 zio_taskq_member(zio_t *zio, zio_taskq_type_t q) 2260 { 2261 spa_t *spa = zio->io_spa; 2262 2263 taskq_t *tq = taskq_of_curthread(); 2264 2265 for (zio_type_t t = 0; t < ZIO_TYPES; t++) { 2266 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 2267 uint_t i; 2268 for (i = 0; i < tqs->stqs_count; i++) { 2269 if (tqs->stqs_taskq[i] == tq) 2270 return (B_TRUE); 2271 } 2272 } 2273 2274 return (B_FALSE); 2275 } 2276 2277 static zio_t * 2278 zio_issue_async(zio_t *zio) 2279 { 2280 ASSERT((zio->io_type != ZIO_TYPE_WRITE) || ZIO_HAS_ALLOCATOR(zio)); 2281 2282 /* Whatever may execute this again, it won't be this thread. */ 2283 zio->io_pipeline &= ~ZIO_STAGE_ISSUE_ASYNC; 2284 2285 /* 2286 * A zio whose children are not ready yet, such as an indirect block 2287 * write, has nothing to do in WRITE_COMPRESS but wait for them, so a 2288 * thread dispatched for it would only block. Do that wait here and let 2289 * whoever wakes it up carry on, since that is not this thread anymore. 2290 */ 2291 if ((zio->io_pipeline & ZIO_STAGE_WRITE_COMPRESS) && 2292 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT | 2293 ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) 2294 return (NULL); 2295 2296 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE); 2297 return (NULL); 2298 } 2299 2300 /* 2301 * ========================================================================== 2302 * Completion batching 2303 * ========================================================================== 2304 * 2305 * A vdev child's entire life after the block layer returns is three pipeline 2306 * stages: VDEV_IO_DONE, VDEV_IO_ASSESS and DONE (ZIO_VDEV_CHILD_PIPELINE). 2307 * For a parent with many children, such as RAIDZ or a mirror, every child but 2308 * the last does nothing there except decrement the parent's child count, yet 2309 * each one costs a taskq dispatch and a context switch to get there. 2310 * 2311 * A batch collects the children of one parent as they return, and once the last 2312 * of them is in, runs all of their completions, and then the parent's, on one 2313 * thread. Arrival happens in the block layer completion context, so it is 2314 * lock-free: bio_endio() on Linux can run in softirq, where the sleepable 2315 * mutex_t is not usable. 2316 * 2317 * Only children that actually arrive from the block layer join zb_arrived; one 2318 * that reaches its completion on a pipeline thread instead just releases its 2319 * hold and runs that completion itself, as it would have without any of this. 2320 * Building the list on arrival is what allows that, since such a child may run 2321 * all the way to zio_destroy() long before the batch does. 2322 * 2323 * A child that occupies a vdev queue slot must never be a member. The slot is 2324 * released by vdev_queue_io_done(), part of the deferred completion, while a 2325 * sibling may still be queued for a slot on another vdev whose slots are in 2326 * turn held by the members of other waiting batches -- a cycle that deadlocks. 2327 */ 2328 static int zio_batch_enabled = 1; 2329 2330 /* 2331 * Open a batch collecting the completions of the vdev children this zio is 2332 * about to create, which do little but count down to it. Every one of those 2333 * children must be created before the matching zio_batch_rele(). 2334 */ 2335 void 2336 zio_batch_create(zio_t *pio) 2337 { 2338 zio_batch_t *zb; 2339 2340 ASSERT3P(pio->io_child_batch, ==, NULL); 2341 2342 if (!zio_batch_enabled) 2343 return; 2344 2345 zb = kmem_alloc(sizeof (*zb), KM_SLEEP); 2346 zb->zb_arrived = NULL; 2347 zb->zb_holds = 1; /* creator's hold */ 2348 pio->io_child_batch = zb; 2349 } 2350 2351 /* 2352 * Free the batch and return the list of members that arrived on it, for the 2353 * caller to execute. Members arrive by prepending, and are equal peers of one 2354 * parent, so their order should not matter; the list is reversed into 2355 * completion order only because it is walked here anyway. Membership is 2356 * dropped in that walk, both because VDEV_IO_ASSESS may reissue a member, which 2357 * must not rejoin, and so that a member's later zio_batch_leave() does not 2358 * touch the batch once it is freed. 2359 */ 2360 static zio_t * 2361 zio_batch_run(zio_batch_t *zb) 2362 { 2363 zio_t *list = NULL, *zio, *next; 2364 2365 /* Pairs with zio_batch_arrive(). */ 2366 membar_consumer(); 2367 2368 for (zio = zb->zb_arrived; zio != NULL; zio = next) { 2369 next = zio->io_exec_next; 2370 zio->io_batch = NULL; 2371 zio->io_exec_next = list; 2372 list = zio; 2373 } 2374 2375 kmem_free(zb, sizeof (*zb)); 2376 2377 return (list); 2378 } 2379 2380 static void 2381 zio_batch_execute(void *arg) 2382 { 2383 zio_execute(zio_batch_run(((zio_t *)arg)->io_batch)); 2384 } 2385 2386 static void 2387 zio_batch_join(zio_batch_t *zb, zio_t *zio) 2388 { 2389 ASSERT3P(zio->io_batch, ==, NULL); 2390 atomic_inc_64(&zb->zb_holds); 2391 zio->io_batch = zb; 2392 } 2393 2394 /* 2395 * Close the batch, once all of its members have been created, dropping the hold 2396 * that kept it from running while they were still being created. Callers do 2397 * this before advancing the parent into VDEV_IO_DONE, where it will wait for 2398 * them; the members go ahead of it in the list, which is harmless, since all 2399 * they do there is decrement its child count. io_child_batch is cleared, so 2400 * that children created later, such as the repair writes from 2401 * vdev_raidz_io_done(), do not join a batch that is already gone. Returns the 2402 * parent, preceded by any members that arrived while it was still creating 2403 * them, for the caller to execute. 2404 */ 2405 zio_t * 2406 zio_batch_rele(zio_t *pio) 2407 { 2408 zio_batch_t *zb = pio->io_child_batch; 2409 zio_t *list, *last; 2410 2411 ASSERT3P(pio->io_exec_next, ==, NULL); 2412 2413 if (zb == NULL) 2414 return (pio); 2415 2416 pio->io_child_batch = NULL; 2417 if (atomic_dec_64_nv(&zb->zb_holds) != 0) 2418 return (pio); 2419 2420 if ((list = zio_batch_run(zb)) == NULL) 2421 return (pio); 2422 2423 last = list; 2424 while (last->io_exec_next != NULL) 2425 last = last->io_exec_next; 2426 last->io_exec_next = pio; 2427 return (list); 2428 } 2429 2430 /* 2431 * Called in place of a member's taskq dispatch, from the block layer 2432 * completion context. Returns B_TRUE if the zio was absorbed by a batch, in 2433 * which case the caller must not touch it again. 2434 */ 2435 static boolean_t 2436 zio_batch_arrive(zio_t *zio) 2437 { 2438 zio_batch_t *zb = zio->io_batch; 2439 zio_t *head; 2440 2441 if (zb == NULL) 2442 return (B_FALSE); 2443 2444 /* 2445 * The completion is deferred, so take the service time here, while it 2446 * still is one: vdev_child_slow_outlier() sits out RAIDZ children based 2447 * on io_delta and io_delay. A non-zero io_delta also tells the stages 2448 * below when the block layer returned, as io_timestamp + io_delta. 2449 */ 2450 ASSERT3U(zio->io_timestamp, !=, 0); 2451 zio->io_delta = gethrtime() - zio->io_timestamp; 2452 2453 do { 2454 head = zb->zb_arrived; 2455 zio->io_exec_next = head; 2456 } while (atomic_cas_ptr(&zb->zb_arrived, head, zio) != head); 2457 2458 /* Publish the arrival before dropping the hold that runs the batch. */ 2459 membar_producer(); 2460 2461 if (atomic_dec_64_nv(&zb->zb_holds) == 0) { 2462 zio_taskq_dispatch_func(zio, ZIO_TASKQ_INTERRUPT, B_FALSE, 2463 zio_batch_execute); 2464 } 2465 return (B_TRUE); 2466 } 2467 2468 /* 2469 * Give up a membership, either because the zio is about to take a vdev queue 2470 * slot after all, or because it reached its completion on a pipeline thread 2471 * rather than from the block layer, and so will run that completion itself. 2472 * Clearing io_batch makes this idempotent. Returns the members for the caller 2473 * to execute if this was the last hold on the batch, and NULL otherwise. 2474 */ 2475 zio_t * 2476 zio_batch_leave(zio_t *zio) 2477 { 2478 zio_batch_t *zb = zio->io_batch; 2479 2480 if (likely(zb == NULL)) 2481 return (NULL); 2482 2483 zio->io_batch = NULL; 2484 if (atomic_dec_64_nv(&zb->zb_holds) != 0) 2485 return (NULL); 2486 2487 return (zio_batch_run(zb)); 2488 } 2489 2490 void 2491 zio_interrupt(void *zio) 2492 { 2493 if (zio_batch_arrive(zio)) 2494 return; 2495 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE); 2496 } 2497 2498 void 2499 zio_delay_interrupt(zio_t *zio) 2500 { 2501 /* 2502 * The timeout_generic() function isn't defined in userspace, so 2503 * rather than trying to implement the function, the zio delay 2504 * functionality has been disabled for userspace builds. 2505 */ 2506 2507 #ifdef _KERNEL 2508 /* 2509 * If io_target_timestamp is zero, then no delay has been registered 2510 * for this IO, thus jump to the end of this function and "skip" the 2511 * delay; issuing it directly to the zio layer. 2512 */ 2513 if (zio->io_target_timestamp != 0) { 2514 hrtime_t now = gethrtime(); 2515 2516 if (now >= zio->io_target_timestamp) { 2517 /* 2518 * This IO has already taken longer than the target 2519 * delay to complete, so we don't want to delay it 2520 * any longer; we "miss" the delay and issue it 2521 * directly to the zio layer. This is likely due to 2522 * the target latency being set to a value less than 2523 * the underlying hardware can satisfy (e.g. delay 2524 * set to 1ms, but the disks take 10ms to complete an 2525 * IO request). 2526 */ 2527 2528 DTRACE_PROBE2(zio__delay__miss, zio_t *, zio, 2529 hrtime_t, now); 2530 2531 zio_interrupt(zio); 2532 } else { 2533 taskqid_t tid; 2534 hrtime_t diff = zio->io_target_timestamp - now; 2535 int ticks = MAX(1, NSEC_TO_TICK(diff)); 2536 clock_t expire_at_tick = ddi_get_lbolt() + ticks; 2537 2538 DTRACE_PROBE3(zio__delay__hit, zio_t *, zio, 2539 hrtime_t, now, hrtime_t, diff); 2540 2541 tid = taskq_dispatch_delay(system_taskq, zio_interrupt, 2542 zio, TQ_NOSLEEP, expire_at_tick); 2543 if (tid == TASKQID_INVALID) { 2544 /* 2545 * Couldn't allocate a task. Just finish the 2546 * zio without a delay. 2547 */ 2548 zio_interrupt(zio); 2549 } 2550 } 2551 return; 2552 } 2553 #endif 2554 DTRACE_PROBE1(zio__delay__skip, zio_t *, zio); 2555 zio_interrupt(zio); 2556 } 2557 2558 static void 2559 zio_deadman_impl(zio_t *pio, int ziodepth) 2560 { 2561 zio_t *cio, *cio_next; 2562 zio_link_t *zl = NULL; 2563 vdev_t *vd = pio->io_vd; 2564 uint64_t failmode = spa_get_deadman_failmode(pio->io_spa); 2565 2566 if (zio_deadman_log_all || (vd != NULL && vd->vdev_ops->vdev_op_leaf)) { 2567 vdev_queue_t *vq = vd ? &vd->vdev_queue : NULL; 2568 zbookmark_phys_t *zb = &pio->io_bookmark; 2569 uint64_t delta = gethrtime() - pio->io_timestamp; 2570 2571 zfs_dbgmsg("slow zio[%d]: zio=%px timestamp=%llu " 2572 "delta=%llu queued=%llu io=%llu " 2573 "path=%s " 2574 "last=%llu type=%d " 2575 "priority=%d flags=0x%llx stage=0x%x " 2576 "pipeline=0x%x pipeline-trace=0x%x " 2577 "objset=%llu object=%llu " 2578 "level=%llu blkid=%llu " 2579 "offset=%llu size=%llu " 2580 "error=%d", 2581 ziodepth, pio, pio->io_timestamp, 2582 (u_longlong_t)delta, pio->io_delta, pio->io_delay, 2583 vd ? vd->vdev_path : "NULL", 2584 vq ? vq->vq_io_complete_ts : 0, pio->io_type, 2585 pio->io_priority, (u_longlong_t)pio->io_flags, 2586 pio->io_stage, pio->io_pipeline, pio->io_pipeline_trace, 2587 (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object, 2588 (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid, 2589 (u_longlong_t)pio->io_offset, (u_longlong_t)pio->io_size, 2590 pio->io_error); 2591 (void) zfs_ereport_post(FM_EREPORT_ZFS_DEADMAN, 2592 pio->io_spa, vd, zb, pio, 0); 2593 } 2594 2595 if (vd != NULL && vd->vdev_ops->vdev_op_leaf && 2596 list_is_empty(&pio->io_child_list) && 2597 failmode == ZIO_FAILURE_MODE_CONTINUE && 2598 taskq_empty_ent(&pio->io_tqent) && 2599 pio->io_queue_state == ZIO_QS_ACTIVE) { 2600 pio->io_error = EINTR; 2601 zio_interrupt(pio); 2602 } 2603 2604 mutex_enter(&pio->io_lock); 2605 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) { 2606 cio_next = zio_walk_children(pio, &zl); 2607 zio_deadman_impl(cio, ziodepth + 1); 2608 } 2609 mutex_exit(&pio->io_lock); 2610 } 2611 2612 /* 2613 * Log the critical information describing this zio and all of its children 2614 * using the zfs_dbgmsg() interface then post deadman event for the ZED. 2615 */ 2616 void 2617 zio_deadman(zio_t *pio, const char *tag) 2618 { 2619 spa_t *spa = pio->io_spa; 2620 char *name = spa_name(spa); 2621 2622 if (!zfs_deadman_enabled || spa_suspended(spa)) 2623 return; 2624 2625 zio_deadman_impl(pio, 0); 2626 2627 switch (spa_get_deadman_failmode(spa)) { 2628 case ZIO_FAILURE_MODE_WAIT: 2629 zfs_dbgmsg("%s waiting for hung I/O to pool '%s'", tag, name); 2630 break; 2631 2632 case ZIO_FAILURE_MODE_CONTINUE: 2633 zfs_dbgmsg("%s restarting hung I/O for pool '%s'", tag, name); 2634 break; 2635 2636 case ZIO_FAILURE_MODE_PANIC: 2637 fm_panic("%s determined I/O to pool '%s' is hung.", tag, name); 2638 break; 2639 } 2640 } 2641 2642 /* 2643 * Execute the I/O pipeline until one of the following occurs: 2644 * (1) the I/O completes; (2) the pipeline stalls waiting for 2645 * dependent child I/Os; (3) the I/O issues, so we're waiting 2646 * for an I/O completion interrupt; (4) the I/O is delegated by 2647 * vdev-level caching or aggregation; (5) the I/O is deferred 2648 * due to vdev-level queueing; (6) the I/O is handed off to 2649 * another thread. In all cases, the pipeline stops whenever 2650 * there's no CPU work; it never burns a thread in cv_wait_io(). 2651 * 2652 * There's no locking on io_stage because there's no legitimate way 2653 * for multiple threads to be attempting to process the same I/O. 2654 */ 2655 static zio_pipe_stage_t *zio_pipeline[]; 2656 2657 /* 2658 * zio_execute() is a wrapper around the static function 2659 * __zio_execute() so that we can force __zio_execute() to be 2660 * inlined. This reduces stack overhead which is important 2661 * because __zio_execute() is called recursively in several zio 2662 * code paths. zio_execute() itself cannot be inlined because 2663 * it is externally visible. 2664 */ 2665 void 2666 zio_execute(void *zio) 2667 { 2668 fstrans_cookie_t cookie; 2669 2670 cookie = spl_fstrans_mark(); 2671 __zio_execute(zio); 2672 spl_fstrans_unmark(cookie); 2673 } 2674 2675 /* 2676 * Used to determine if in the current context the stack is sized large 2677 * enough to allow zio_execute() to be called recursively. A minimum 2678 * stack size of 16K is required to avoid needing to re-dispatch the zio. 2679 */ 2680 static boolean_t 2681 zio_execute_stack_check(zio_t *zio) 2682 { 2683 #if !defined(HAVE_LARGE_STACKS) 2684 dsl_pool_t *dp = spa_get_dsl(zio->io_spa); 2685 2686 /* Executing in txg_sync_thread() context. */ 2687 if (dp && curthread == dp->dp_tx.tx_sync_thread) 2688 return (B_TRUE); 2689 2690 /* Pool initialization outside of zio_taskq context. */ 2691 if (dp && spa_is_initializing(dp->dp_spa) && 2692 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE) && 2693 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE_HIGH)) 2694 return (B_TRUE); 2695 #else 2696 (void) zio; 2697 #endif /* HAVE_LARGE_STACKS */ 2698 2699 return (B_FALSE); 2700 } 2701 2702 /* 2703 * Run one pipeline stage, returning a list of zios to continue with, or NULL 2704 * if this thread is done with it. 2705 */ 2706 __attribute__((always_inline)) 2707 static inline zio_t * 2708 zio_execute_stage(zio_t *zio) 2709 { 2710 enum zio_stage pipeline = zio->io_pipeline; 2711 enum zio_stage stage = zio->io_stage; 2712 2713 zio->io_executor = curthread; 2714 2715 ASSERT(!MUTEX_HELD(&zio->io_lock)); 2716 ASSERT0P(zio->io_stall); 2717 ASSERT(ISP2(stage)); 2718 ASSERT(pipeline & ~((stage << 1) - 1)); 2719 2720 do { 2721 stage <<= 1; 2722 } while ((stage & pipeline) == 0); 2723 2724 ASSERT(stage <= ZIO_STAGE_DONE); 2725 2726 /* 2727 * If we are in interrupt context and this pipeline stage will grab 2728 * a config lock that is held across I/O, or may wait for an I/O that 2729 * needs an interrupt thread to complete, issue async to avoid deadlock. 2730 * 2731 * For VDEV_IO_START, we cut in line so that the io will be sent to 2732 * disk promptly. 2733 */ 2734 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL && 2735 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) { 2736 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ? 2737 zio_requeue_io_start_cut_in_line : B_FALSE; 2738 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut); 2739 return (NULL); 2740 } 2741 2742 /* 2743 * If the current context doesn't have large enough stacks 2744 * the zio must be issued asynchronously to prevent overflow. 2745 */ 2746 if (zio_execute_stack_check(zio)) { 2747 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ? 2748 zio_requeue_io_start_cut_in_line : B_FALSE; 2749 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut); 2750 return (NULL); 2751 } 2752 2753 zio->io_stage = stage; 2754 zio->io_pipeline_trace |= zio->io_stage; 2755 2756 /* 2757 * The zio pipeline stage returns the next zio to execute (typically 2758 * the same as this one), or NULL if we should stop. It may also 2759 * chain more zios to it for us to execute later. 2760 */ 2761 return (zio_pipeline[highbit64(stage) - 1](zio)); 2762 } 2763 2764 /* 2765 * Take all but the first of the zios a stage handed back off its head, and 2766 * prepend the rest to those already pending. Dispatch heavyweight ZIOs except 2767 * the last, so that they could run in parallel. 2768 */ 2769 static inline void 2770 zio_execute_defer(zio_t *zio, zio_t **pendingp) 2771 { 2772 zio_t *list = NULL, **tailp = &list; 2773 zio_t *next; 2774 2775 for (zio_t *cur = zio->io_exec_next; cur != NULL; cur = next) { 2776 next = cur->io_exec_next; 2777 cur->io_exec_next = NULL; 2778 if ((next != NULL || *pendingp != NULL) && 2779 !(cur->io_flags & ZIO_FLAG_LIGHTWEIGHT)) { 2780 zio_taskq_dispatch(cur, 2781 cur->io_stage < ZIO_STAGE_VDEV_IO_START ? 2782 ZIO_TASKQ_ISSUE : ZIO_TASKQ_INTERRUPT, B_FALSE); 2783 continue; 2784 } 2785 *tailp = cur; 2786 tailp = &cur->io_exec_next; 2787 } 2788 2789 *tailp = *pendingp; 2790 *pendingp = list; 2791 zio->io_exec_next = NULL; 2792 } 2793 2794 __attribute__((always_inline)) 2795 static inline void 2796 __zio_execute(zio_t *zio) 2797 { 2798 zio_t *pending = zio->io_exec_next; 2799 zio->io_exec_next = NULL; 2800 2801 for (;;) { 2802 zio_t *last = zio; 2803 while ((zio = zio_execute_stage(zio)) != NULL) { 2804 if (zio->io_exec_next != NULL) 2805 zio_execute_defer(zio, &pending); 2806 2807 /* 2808 * A heavyweight zio is dispatched if others are already 2809 * waiting for this thread to let them run in parallel. 2810 */ 2811 if (zio != last && pending != NULL && 2812 !(zio->io_flags & ZIO_FLAG_LIGHTWEIGHT)) { 2813 zio_taskq_dispatch(zio, 2814 zio->io_stage < ZIO_STAGE_VDEV_IO_START ? 2815 ZIO_TASKQ_ISSUE : ZIO_TASKQ_INTERRUPT, 2816 B_FALSE); 2817 break; 2818 } 2819 last = zio; 2820 } 2821 2822 if ((zio = pending) == NULL) 2823 return; 2824 pending = zio->io_exec_next; 2825 zio->io_exec_next = NULL; 2826 } 2827 } 2828 2829 2830 /* 2831 * ========================================================================== 2832 * Initiate I/O, either sync or async 2833 * ========================================================================== 2834 */ 2835 int 2836 zio_wait(zio_t *zio) 2837 { 2838 /* 2839 * Some routines, like zio_free_sync(), may return a NULL zio 2840 * to avoid the performance overhead of creating and then destroying 2841 * an unneeded zio. For the callers' simplicity, we accept a NULL 2842 * zio and ignore it. 2843 */ 2844 if (zio == NULL) 2845 return (0); 2846 2847 long timeout = MSEC_TO_TICK(zfs_deadman_ziotime_ms); 2848 int error; 2849 2850 ASSERT3S(zio->io_stage, ==, ZIO_STAGE_OPEN); 2851 ASSERT0P(zio->io_executor); 2852 2853 zio->io_waiter = curthread; 2854 ASSERT0(zio->io_queued_timestamp); 2855 zio->io_queued_timestamp = gethrtime(); 2856 2857 if (zio->io_type == ZIO_TYPE_WRITE) { 2858 spa_select_allocator(zio); 2859 } 2860 __zio_execute(zio); 2861 2862 mutex_enter(&zio->io_lock); 2863 while (zio->io_executor != NULL) { 2864 error = cv_timedwait_io(&zio->io_cv, &zio->io_lock, 2865 ddi_get_lbolt() + timeout); 2866 2867 if (zfs_deadman_enabled && error == -1 && 2868 gethrtime() - zio->io_queued_timestamp > 2869 spa_deadman_ziotime(zio->io_spa)) { 2870 mutex_exit(&zio->io_lock); 2871 timeout = MSEC_TO_TICK(zfs_deadman_checktime_ms); 2872 zio_deadman(zio, FTAG); 2873 mutex_enter(&zio->io_lock); 2874 } 2875 } 2876 mutex_exit(&zio->io_lock); 2877 2878 error = zio->io_error; 2879 zio_destroy(zio); 2880 2881 return (error); 2882 } 2883 2884 void 2885 zio_nowait(zio_t *zio) 2886 { 2887 /* 2888 * See comment in zio_wait(). 2889 */ 2890 if (zio == NULL) 2891 return; 2892 2893 ASSERT0P(zio->io_executor); 2894 2895 if (zio->io_child_type == ZIO_CHILD_LOGICAL && 2896 list_is_empty(&zio->io_parent_list)) { 2897 zio_t *pio; 2898 2899 /* 2900 * This is a logical async I/O with no parent to wait for it. 2901 * We add it to the spa_async_root_zio "Godfather" I/O which 2902 * will ensure they complete prior to unloading the pool. 2903 */ 2904 spa_t *spa = zio->io_spa; 2905 pio = spa->spa_async_zio_root[CPU_SEQID_UNSTABLE]; 2906 2907 zio_add_child(pio, zio); 2908 } 2909 2910 ASSERT0(zio->io_queued_timestamp); 2911 zio->io_queued_timestamp = gethrtime(); 2912 if (zio->io_type == ZIO_TYPE_WRITE) { 2913 spa_select_allocator(zio); 2914 } 2915 __zio_execute(zio); 2916 } 2917 2918 /* 2919 * ========================================================================== 2920 * Reexecute, cancel, or suspend/resume failed I/O 2921 * ========================================================================== 2922 */ 2923 2924 static void 2925 zio_reexecute(void *arg) 2926 { 2927 zio_t *pio = arg; 2928 zio_t *cio, *cio_next, *gio; 2929 2930 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL); 2931 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN); 2932 ASSERT0P(pio->io_gang_leader); 2933 ASSERT0P(pio->io_gang_tree); 2934 2935 mutex_enter(&pio->io_lock); 2936 pio->io_flags = pio->io_orig_flags; 2937 pio->io_stage = pio->io_orig_stage; 2938 pio->io_pipeline = pio->io_orig_pipeline; 2939 pio->io_post = 0; 2940 pio->io_flags |= ZIO_FLAG_REEXECUTED; 2941 pio->io_pipeline_trace = 0; 2942 pio->io_error = 0; 2943 pio->io_state[ZIO_WAIT_READY] = (pio->io_stage >= ZIO_STAGE_READY) || 2944 (pio->io_pipeline & ZIO_STAGE_READY) == 0; 2945 pio->io_state[ZIO_WAIT_DONE] = (pio->io_stage >= ZIO_STAGE_DONE); 2946 2947 /* 2948 * It's possible for a failed ZIO to be a descendant of more than one 2949 * ZIO tree. When reexecuting it, we have to be sure to add its wait 2950 * states to all parent wait counts. 2951 * 2952 * Those parents, in turn, may have other children that are currently 2953 * active, usually because they've already been reexecuted after 2954 * resuming. Those children may be executing and may call 2955 * zio_notify_parent() at the same time as we're updating our parent's 2956 * counts. To avoid races while updating the counts, we take 2957 * gio->io_lock before each update. 2958 */ 2959 zio_link_t *zl = NULL; 2960 while ((gio = zio_walk_parents(pio, &zl)) != NULL) { 2961 mutex_enter(&gio->io_lock); 2962 for (int w = 0; w < ZIO_WAIT_TYPES; w++) { 2963 gio->io_children[pio->io_child_type][w] += 2964 !pio->io_state[w]; 2965 } 2966 mutex_exit(&gio->io_lock); 2967 } 2968 2969 for (int c = 0; c < ZIO_CHILD_TYPES; c++) 2970 pio->io_child_error[c] = 0; 2971 2972 if (IO_IS_ALLOCATING(pio)) 2973 BP_ZERO(pio->io_bp); 2974 2975 /* 2976 * As we reexecute pio's children, new children could be created. 2977 * New children go to the head of pio's io_child_list, however, 2978 * so we will (correctly) not reexecute them. The key is that 2979 * the remainder of pio's io_child_list, from 'cio_next' onward, 2980 * cannot be affected by any side effects of reexecuting 'cio'. 2981 */ 2982 zl = NULL; 2983 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) { 2984 cio_next = zio_walk_children(pio, &zl); 2985 mutex_exit(&pio->io_lock); 2986 zio_reexecute(cio); 2987 mutex_enter(&pio->io_lock); 2988 } 2989 mutex_exit(&pio->io_lock); 2990 2991 /* 2992 * Now that all children have been reexecuted, execute the parent. 2993 * We don't reexecute "The Godfather" I/O here as it's the 2994 * responsibility of the caller to wait on it. 2995 */ 2996 if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) { 2997 pio->io_queued_timestamp = gethrtime(); 2998 __zio_execute(pio); 2999 } 3000 } 3001 3002 void 3003 zio_suspend(spa_t *spa, zio_t *zio, zio_suspend_reason_t reason) 3004 { 3005 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC) 3006 fm_panic("Pool '%s' has encountered an uncorrectable I/O " 3007 "failure and the failure mode property for this pool " 3008 "is set to panic.", spa_name(spa)); 3009 3010 if (reason != ZIO_SUSPEND_MMP) { 3011 cmn_err(CE_WARN, "Pool '%s' has encountered an uncorrectable " 3012 "I/O failure and has been suspended.", spa_name(spa)); 3013 } 3014 3015 (void) zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, 3016 NULL, NULL, 0); 3017 3018 mutex_enter(&spa->spa_suspend_lock); 3019 3020 if (spa->spa_suspend_zio_root == NULL) 3021 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL, 3022 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 3023 ZIO_FLAG_GODFATHER); 3024 3025 spa->spa_suspended = reason; 3026 3027 if (zio != NULL) { 3028 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER)); 3029 ASSERT(zio != spa->spa_suspend_zio_root); 3030 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 3031 ASSERT0P(zio_unique_parent(zio)); 3032 ASSERT(zio->io_stage == ZIO_STAGE_DONE); 3033 zio_add_child(spa->spa_suspend_zio_root, zio); 3034 } 3035 3036 mutex_exit(&spa->spa_suspend_lock); 3037 3038 txg_wait_kick(spa->spa_dsl_pool); 3039 } 3040 3041 int 3042 zio_resume(spa_t *spa) 3043 { 3044 zio_t *pio; 3045 3046 /* 3047 * Reexecute all previously suspended i/o. 3048 */ 3049 mutex_enter(&spa->spa_suspend_lock); 3050 if (spa->spa_suspended != ZIO_SUSPEND_NONE) 3051 cmn_err(CE_WARN, "Pool '%s' was suspended and is being " 3052 "resumed. Failed I/O will be retried.", 3053 spa_name(spa)); 3054 spa->spa_suspended = ZIO_SUSPEND_NONE; 3055 cv_broadcast(&spa->spa_suspend_cv); 3056 pio = spa->spa_suspend_zio_root; 3057 spa->spa_suspend_zio_root = NULL; 3058 mutex_exit(&spa->spa_suspend_lock); 3059 3060 if (pio == NULL) 3061 return (0); 3062 3063 zio_reexecute(pio); 3064 return (zio_wait(pio)); 3065 } 3066 3067 void 3068 zio_resume_wait(spa_t *spa) 3069 { 3070 mutex_enter(&spa->spa_suspend_lock); 3071 while (spa_suspended(spa)) 3072 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock); 3073 mutex_exit(&spa->spa_suspend_lock); 3074 } 3075 3076 /* 3077 * ========================================================================== 3078 * Gang blocks. 3079 * 3080 * A gang block is a collection of small blocks that looks to the DMU 3081 * like one large block. When zio_dva_allocate() cannot find a block 3082 * of the requested size, due to either severe fragmentation or the pool 3083 * being nearly full, it calls zio_write_gang_block() to construct the 3084 * block from smaller fragments. 3085 * 3086 * A gang block consists of a a gang header and up to gbh_nblkptrs(size) 3087 * gang members. The gang header is like an indirect block: it's an array 3088 * of block pointers, though the header has a small tail (a zio_eck_t) 3089 * that stores an embedded checksum. It is allocated using only a single 3090 * sector as the requested size, and hence is allocatable regardless of 3091 * fragmentation. Its size is determined by the smallest allocatable 3092 * asize of the vdevs it was allocated on. The gang header's bps point 3093 * to its gang members, which hold the data. 3094 * 3095 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg> 3096 * as the verifier to ensure uniqueness of the SHA256 checksum. 3097 * Critically, the gang block bp's blk_cksum is the checksum of the data, 3098 * not the gang header. This ensures that data block signatures (needed for 3099 * deduplication) are independent of how the block is physically stored. 3100 * 3101 * Gang blocks can be nested: a gang member may itself be a gang block. 3102 * Thus every gang block is a tree in which root and all interior nodes are 3103 * gang headers, and the leaves are normal blocks that contain user data. 3104 * The root of the gang tree is called the gang leader. 3105 * 3106 * To perform any operation (read, rewrite, free, claim) on a gang block, 3107 * zio_gang_assemble() first assembles the gang tree (minus data leaves) 3108 * in the io_gang_tree field of the original logical i/o by recursively 3109 * reading the gang leader and all gang headers below it. This yields 3110 * an in-core tree containing the contents of every gang header and the 3111 * bps for every constituent of the gang block. 3112 * 3113 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree 3114 * and invokes a callback on each bp. To free a gang block, zio_gang_issue() 3115 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp. 3116 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim(). 3117 * zio_read_gang() is a wrapper around zio_read() that omits reading gang 3118 * headers, since we already have those in io_gang_tree. zio_rewrite_gang() 3119 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite() 3120 * of the gang header plus zio_checksum_compute() of the data to update the 3121 * gang header's blk_cksum as described above. 3122 * 3123 * The two-phase assemble/issue model solves the problem of partial failure -- 3124 * what if you'd freed part of a gang block but then couldn't read the 3125 * gang header for another part? Assembling the entire gang tree first 3126 * ensures that all the necessary gang header I/O has succeeded before 3127 * starting the actual work of free, claim, or write. Once the gang tree 3128 * is assembled, free and claim are in-memory operations that cannot fail. 3129 * 3130 * In the event that a gang write fails, zio_dva_unallocate() walks the 3131 * gang tree to immediately free (i.e. insert back into the space map) 3132 * everything we've allocated. This ensures that we don't get ENOSPC 3133 * errors during repeated suspend/resume cycles due to a flaky device. 3134 * 3135 * Gang rewrites only happen during sync-to-convergence. If we can't assemble 3136 * the gang tree, we won't modify the block, so we can safely defer the free 3137 * (knowing that the block is still intact). If we *can* assemble the gang 3138 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free 3139 * each constituent bp and we can allocate a new block on the next sync pass. 3140 * 3141 * In all cases, the gang tree allows complete recovery from partial failure. 3142 * ========================================================================== 3143 */ 3144 3145 static void 3146 zio_gang_issue_func_done(zio_t *zio) 3147 { 3148 abd_free(zio->io_abd); 3149 } 3150 3151 static zio_t * 3152 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data, 3153 uint64_t offset) 3154 { 3155 if (gn != NULL) 3156 return (pio); 3157 3158 return (zio_read(pio, pio->io_spa, bp, abd_get_offset(data, offset), 3159 BP_GET_PSIZE(bp), zio_gang_issue_func_done, 3160 NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), 3161 &pio->io_bookmark)); 3162 } 3163 3164 static zio_t * 3165 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data, 3166 uint64_t offset) 3167 { 3168 zio_t *zio; 3169 3170 if (gn != NULL) { 3171 abd_t *gbh_abd = 3172 abd_get_from_buf(gn->gn_gbh, gn->gn_gangblocksize); 3173 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp, 3174 gbh_abd, gn->gn_gangblocksize, zio_gang_issue_func_done, 3175 NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), 3176 &pio->io_bookmark); 3177 /* 3178 * As we rewrite each gang header, the pipeline will compute 3179 * a new gang block header checksum for it; but no one will 3180 * compute a new data checksum, so we do that here. The one 3181 * exception is the gang leader: the pipeline already computed 3182 * its data checksum because that stage precedes gang assembly. 3183 * (Presently, nothing actually uses interior data checksums; 3184 * this is just good hygiene.) 3185 */ 3186 if (gn != pio->io_gang_leader->io_gang_tree) { 3187 abd_t *buf = abd_get_offset(data, offset); 3188 3189 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp), 3190 buf, BP_GET_PSIZE(bp)); 3191 3192 abd_free(buf); 3193 } 3194 /* 3195 * If we are here to damage data for testing purposes, 3196 * leave the GBH alone so that we can detect the damage. 3197 */ 3198 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE) 3199 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES; 3200 } else { 3201 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp, 3202 abd_get_offset(data, offset), BP_GET_PSIZE(bp), 3203 zio_gang_issue_func_done, NULL, pio->io_priority, 3204 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 3205 } 3206 3207 return (zio); 3208 } 3209 3210 static zio_t * 3211 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data, 3212 uint64_t offset) 3213 { 3214 (void) gn, (void) data, (void) offset; 3215 3216 zio_t *zio = zio_free_sync(pio, pio->io_spa, pio->io_txg, bp, 3217 ZIO_GANG_CHILD_FLAGS(pio)); 3218 if (zio == NULL) { 3219 zio = zio_null(pio, pio->io_spa, 3220 NULL, NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)); 3221 } 3222 return (zio); 3223 } 3224 3225 static zio_t * 3226 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data, 3227 uint64_t offset) 3228 { 3229 (void) gn, (void) data, (void) offset; 3230 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp, 3231 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio))); 3232 } 3233 3234 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = { 3235 NULL, 3236 zio_read_gang, 3237 zio_rewrite_gang, 3238 zio_free_gang, 3239 zio_claim_gang, 3240 NULL 3241 }; 3242 3243 static void zio_gang_tree_assemble_done(zio_t *zio); 3244 3245 static zio_gang_node_t * 3246 zio_gang_node_alloc(zio_gang_node_t **gnpp, uint64_t gangblocksize) 3247 { 3248 zio_gang_node_t *gn; 3249 3250 ASSERT0P(*gnpp); 3251 3252 gn = kmem_zalloc(sizeof (*gn) + 3253 (gbh_nblkptrs(gangblocksize) * sizeof (gn)), KM_SLEEP); 3254 gn->gn_gangblocksize = gn->gn_allocsize = gangblocksize; 3255 gn->gn_gbh = zio_buf_alloc(gangblocksize); 3256 *gnpp = gn; 3257 3258 return (gn); 3259 } 3260 3261 static void 3262 zio_gang_node_free(zio_gang_node_t **gnpp) 3263 { 3264 zio_gang_node_t *gn = *gnpp; 3265 3266 for (int g = 0; g < gbh_nblkptrs(gn->gn_allocsize); g++) 3267 ASSERT0P(gn->gn_child[g]); 3268 3269 zio_buf_free(gn->gn_gbh, gn->gn_allocsize); 3270 kmem_free(gn, sizeof (*gn) + 3271 (gbh_nblkptrs(gn->gn_allocsize) * sizeof (gn))); 3272 *gnpp = NULL; 3273 } 3274 3275 static void 3276 zio_gang_tree_free(zio_gang_node_t **gnpp) 3277 { 3278 zio_gang_node_t *gn = *gnpp; 3279 3280 if (gn == NULL) 3281 return; 3282 3283 for (int g = 0; g < gbh_nblkptrs(gn->gn_allocsize); g++) 3284 zio_gang_tree_free(&gn->gn_child[g]); 3285 3286 zio_gang_node_free(gnpp); 3287 } 3288 3289 static void 3290 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp) 3291 { 3292 uint64_t gangblocksize = UINT64_MAX; 3293 if (spa_feature_is_active(gio->io_spa, 3294 SPA_FEATURE_DYNAMIC_GANG_HEADER)) { 3295 spa_config_enter(gio->io_spa, SCL_VDEV, FTAG, RW_READER); 3296 for (int dva = 0; dva < BP_GET_NDVAS(bp); dva++) { 3297 vdev_t *vd = vdev_lookup_top(gio->io_spa, 3298 DVA_GET_VDEV(&bp->blk_dva[dva])); 3299 uint64_t psize = vdev_gang_header_psize(vd); 3300 gangblocksize = MIN(gangblocksize, psize); 3301 } 3302 spa_config_exit(gio->io_spa, SCL_VDEV, FTAG); 3303 } else { 3304 gangblocksize = SPA_OLD_GANGBLOCKSIZE; 3305 } 3306 ASSERT3U(gangblocksize, !=, UINT64_MAX); 3307 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp, gangblocksize); 3308 abd_t *gbh_abd = abd_get_from_buf(gn->gn_gbh, gangblocksize); 3309 3310 ASSERT(gio->io_gang_leader == gio); 3311 ASSERT(BP_IS_GANG(bp)); 3312 3313 zio_nowait(zio_read(gio, gio->io_spa, bp, gbh_abd, gangblocksize, 3314 zio_gang_tree_assemble_done, gn, gio->io_priority, 3315 ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark)); 3316 } 3317 3318 static void 3319 zio_gang_tree_assemble_done(zio_t *zio) 3320 { 3321 zio_t *gio = zio->io_gang_leader; 3322 zio_gang_node_t *gn = zio->io_private; 3323 blkptr_t *bp = zio->io_bp; 3324 3325 ASSERT(gio == zio_unique_parent(zio)); 3326 ASSERT(list_is_empty(&zio->io_child_list)); 3327 3328 if (zio->io_error) 3329 return; 3330 3331 /* this ABD was created from a linear buf in zio_gang_tree_assemble */ 3332 if (BP_SHOULD_BYTESWAP(bp)) 3333 byteswap_uint64_array(abd_to_buf(zio->io_abd), zio->io_size); 3334 3335 ASSERT3P(abd_to_buf(zio->io_abd), ==, gn->gn_gbh); 3336 /* 3337 * If this was an old-style gangblock, the gangblocksize should have 3338 * been updated in zio_checksum_error to reflect that. 3339 */ 3340 ASSERT3U(gbh_eck(gn->gn_gbh, gn->gn_gangblocksize)->zec_magic, 3341 ==, ZEC_MAGIC); 3342 3343 abd_free(zio->io_abd); 3344 3345 for (int g = 0; g < gbh_nblkptrs(gn->gn_gangblocksize); g++) { 3346 blkptr_t *gbp = gbh_bp(gn->gn_gbh, g); 3347 if (!BP_IS_GANG(gbp)) 3348 continue; 3349 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]); 3350 } 3351 } 3352 3353 static void 3354 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, abd_t *data, 3355 uint64_t offset) 3356 { 3357 zio_t *gio = pio->io_gang_leader; 3358 zio_t *zio; 3359 3360 ASSERT(BP_IS_GANG(bp) == !!gn); 3361 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp)); 3362 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree); 3363 3364 /* 3365 * If you're a gang header, your data is in gn->gn_gbh. 3366 * If you're a gang member, your data is in 'data' and gn == NULL. 3367 */ 3368 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data, offset); 3369 3370 if (gn != NULL) { 3371 ASSERT3U(gbh_eck(gn->gn_gbh, 3372 gn->gn_gangblocksize)->zec_magic, ==, ZEC_MAGIC); 3373 3374 for (int g = 0; g < gbh_nblkptrs(gn->gn_gangblocksize); g++) { 3375 blkptr_t *gbp = gbh_bp(gn->gn_gbh, g); 3376 if (BP_IS_HOLE(gbp)) 3377 continue; 3378 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data, 3379 offset); 3380 offset += BP_GET_PSIZE(gbp); 3381 } 3382 } 3383 3384 if (gn == gio->io_gang_tree) 3385 ASSERT3U(gio->io_size, ==, offset); 3386 3387 if (zio != pio) 3388 zio_nowait(zio); 3389 } 3390 3391 static zio_t * 3392 zio_gang_assemble(zio_t *zio) 3393 { 3394 blkptr_t *bp = zio->io_bp; 3395 3396 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL); 3397 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 3398 3399 zio->io_gang_leader = zio; 3400 3401 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree); 3402 3403 return (zio); 3404 } 3405 3406 static zio_t * 3407 zio_gang_issue(zio_t *zio) 3408 { 3409 blkptr_t *bp = zio->io_bp; 3410 3411 if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT, ZIO_WAIT_DONE)) { 3412 return (NULL); 3413 } 3414 3415 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio); 3416 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 3417 3418 if (zio->io_child_error[ZIO_CHILD_GANG] == 0) 3419 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd, 3420 0); 3421 else 3422 zio_gang_tree_free(&zio->io_gang_tree); 3423 3424 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 3425 3426 return (zio); 3427 } 3428 3429 static void 3430 zio_inherit_allocator(zio_t *pio, zio_t *cio) 3431 { 3432 cio->io_allocator = pio->io_allocator; 3433 } 3434 3435 static void 3436 zio_write_gang_member_ready(zio_t *zio) 3437 { 3438 zio_t *pio = zio_unique_parent(zio); 3439 dva_t *cdva = zio->io_bp->blk_dva; 3440 dva_t *pdva = pio->io_bp->blk_dva; 3441 uint64_t asize; 3442 zio_t *gio __maybe_unused = zio->io_gang_leader; 3443 3444 if (BP_IS_HOLE(zio->io_bp)) 3445 return; 3446 3447 /* 3448 * If we're getting direct-invoked from zio_write_gang_block(), 3449 * the bp_orig will be set. 3450 */ 3451 ASSERT(BP_IS_HOLE(&zio->io_bp_orig) || 3452 zio->io_flags & ZIO_FLAG_PREALLOCATED); 3453 3454 ASSERT(zio->io_child_type == ZIO_CHILD_GANG); 3455 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies); 3456 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp)); 3457 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp)); 3458 VERIFY3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp)); 3459 3460 mutex_enter(&pio->io_lock); 3461 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) { 3462 ASSERT(DVA_GET_GANG(&pdva[d])); 3463 asize = DVA_GET_ASIZE(&pdva[d]); 3464 asize += DVA_GET_ASIZE(&cdva[d]); 3465 DVA_SET_ASIZE(&pdva[d], asize); 3466 } 3467 mutex_exit(&pio->io_lock); 3468 } 3469 3470 static void 3471 zio_write_gang_done(zio_t *zio) 3472 { 3473 /* 3474 * The io_abd field will be NULL for a zio with no data. The io_flags 3475 * will initially have the ZIO_FLAG_NODATA bit flag set, but we can't 3476 * check for it here as it is cleared in zio_ready. 3477 */ 3478 if (zio->io_abd != NULL) 3479 abd_free(zio->io_abd); 3480 } 3481 3482 static void 3483 zio_update_feature(void *arg, dmu_tx_t *tx) 3484 { 3485 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 3486 spa_feature_incr(spa, (spa_feature_t)(uintptr_t)arg, tx); 3487 } 3488 3489 static zio_t * 3490 zio_write_gang_block(zio_t *pio, metaslab_class_t *mc) 3491 { 3492 spa_t *spa = pio->io_spa; 3493 blkptr_t *bp = pio->io_bp; 3494 zio_t *gio = pio->io_gang_leader; 3495 zio_t *zio; 3496 zio_gang_node_t *gn, **gnpp; 3497 zio_gbh_phys_t *gbh; 3498 abd_t *gbh_abd; 3499 uint64_t txg = pio->io_txg; 3500 uint64_t resid = pio->io_size; 3501 zio_prop_t zp; 3502 int error; 3503 boolean_t has_data = !(pio->io_flags & ZIO_FLAG_NODATA); 3504 3505 /* 3506 * Store multiple copies of the GBH, so that we can still traverse 3507 * all the data (e.g. to free or scrub) even if a block is damaged. 3508 * This value respects the redundant_metadata property. 3509 */ 3510 int gbh_copies = gio->io_prop.zp_gang_copies; 3511 if (gbh_copies == 0) { 3512 /* 3513 * This should only happen in the case where we're filling in 3514 * DDT entries for a parent that wants more copies than the DDT 3515 * has. In that case, we cannot gang without creating a mixed 3516 * blkptr, which is illegal. 3517 */ 3518 ASSERT3U(gio->io_child_type, ==, ZIO_CHILD_DDT); 3519 pio->io_error = EAGAIN; 3520 return (pio); 3521 } 3522 ASSERT3S(gbh_copies, >, 0); 3523 ASSERT3S(gbh_copies, <=, SPA_DVAS_PER_BP); 3524 3525 ASSERT(ZIO_HAS_ALLOCATOR(pio)); 3526 int flags = METASLAB_GANG_HEADER; 3527 if (pio->io_flags & ZIO_FLAG_ALLOC_THROTTLED) { 3528 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE); 3529 ASSERT(has_data); 3530 3531 flags |= METASLAB_ASYNC_ALLOC; 3532 } 3533 3534 uint64_t gangblocksize = SPA_OLD_GANGBLOCKSIZE; 3535 uint64_t candidate = gangblocksize; 3536 error = metaslab_alloc_range(spa, mc, gangblocksize, gangblocksize, 3537 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags, 3538 ZIO_ALLOC_LIST(pio), pio->io_allocator, pio, &candidate); 3539 if (error) { 3540 pio->io_error = error; 3541 return (pio); 3542 } 3543 if (spa_feature_is_active(spa, SPA_FEATURE_DYNAMIC_GANG_HEADER)) 3544 gangblocksize = candidate; 3545 3546 if (pio == gio) { 3547 gnpp = &gio->io_gang_tree; 3548 } else { 3549 gnpp = pio->io_private; 3550 ASSERT(pio->io_ready == zio_write_gang_member_ready); 3551 } 3552 3553 gn = zio_gang_node_alloc(gnpp, gangblocksize); 3554 gbh = gn->gn_gbh; 3555 memset(gbh, 0, gangblocksize); 3556 gbh_abd = abd_get_from_buf(gbh, gangblocksize); 3557 3558 /* 3559 * Create the gang header. 3560 */ 3561 zio = zio_rewrite(pio, spa, txg, bp, gbh_abd, gangblocksize, 3562 zio_write_gang_done, NULL, pio->io_priority, 3563 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 3564 3565 zio_inherit_allocator(pio, zio); 3566 if (pio->io_flags & ZIO_FLAG_ALLOC_THROTTLED) { 3567 boolean_t more; 3568 VERIFY(metaslab_class_throttle_reserve(mc, zio->io_allocator, 3569 gbh_copies, zio->io_size, B_TRUE, &more)); 3570 zio->io_flags |= ZIO_FLAG_ALLOC_THROTTLED; 3571 } 3572 3573 /* 3574 * Create and nowait the gang children. First, we try to do 3575 * opportunistic allocations. If that fails to generate enough 3576 * space, we fall back to normal zio_write calls for nested gang. 3577 */ 3578 int g; 3579 boolean_t any_failed = B_FALSE; 3580 for (g = 0; resid != 0; g++) { 3581 flags &= METASLAB_ASYNC_ALLOC; 3582 flags |= METASLAB_GANG_CHILD; 3583 zp.zp_checksum = gio->io_prop.zp_checksum; 3584 zp.zp_compress = ZIO_COMPRESS_OFF; 3585 zp.zp_complevel = gio->io_prop.zp_complevel; 3586 zp.zp_type = zp.zp_storage_type = DMU_OT_NONE; 3587 zp.zp_level = 0; 3588 zp.zp_copies = gio->io_prop.zp_copies; 3589 zp.zp_gang_copies = gio->io_prop.zp_gang_copies; 3590 zp.zp_dedup = B_FALSE; 3591 zp.zp_dedup_verify = B_FALSE; 3592 zp.zp_nopwrite = B_FALSE; 3593 zp.zp_encrypt = gio->io_prop.zp_encrypt; 3594 zp.zp_byteorder = gio->io_prop.zp_byteorder; 3595 zp.zp_direct_write = B_FALSE; 3596 memset(zp.zp_salt, 0, ZIO_DATA_SALT_LEN); 3597 memset(zp.zp_iv, 0, ZIO_DATA_IV_LEN); 3598 memset(zp.zp_mac, 0, ZIO_DATA_MAC_LEN); 3599 3600 uint64_t min_size = zio_roundup_alloc_size(spa, 3601 resid / (gbh_nblkptrs(gangblocksize) - g)); 3602 min_size = MIN(min_size, resid); 3603 bp = &((blkptr_t *)gbh)[g]; 3604 3605 zio_alloc_list_t cio_list; 3606 metaslab_trace_init(&cio_list); 3607 uint64_t allocated_size = UINT64_MAX; 3608 error = metaslab_alloc_range(spa, mc, min_size, resid, 3609 bp, gio->io_prop.zp_copies, txg, NULL, 3610 flags, &cio_list, zio->io_allocator, NULL, &allocated_size); 3611 3612 boolean_t allocated = error == 0; 3613 any_failed |= !allocated; 3614 3615 uint64_t psize = allocated ? MIN(resid, allocated_size) : 3616 min_size; 3617 ASSERT3U(psize, >=, min_size); 3618 3619 zio_t *cio = zio_write(zio, spa, txg, bp, has_data ? 3620 abd_get_offset(pio->io_abd, pio->io_size - resid) : NULL, 3621 psize, psize, &zp, zio_write_gang_member_ready, NULL, 3622 zio_write_gang_done, &gn->gn_child[g], pio->io_priority, 3623 ZIO_GANG_CHILD_FLAGS(pio) | 3624 (allocated ? ZIO_FLAG_PREALLOCATED : 0), &pio->io_bookmark); 3625 3626 resid -= psize; 3627 zio_inherit_allocator(zio, cio); 3628 if (allocated) { 3629 metaslab_trace_move(&cio_list, ZIO_ALLOC_LIST(cio)); 3630 metaslab_group_alloc_increment_all(spa, 3631 &cio->io_bp_orig, zio->io_allocator, flags, psize, 3632 cio); 3633 } 3634 /* 3635 * We do not reserve for the child writes, since we already 3636 * reserved for the parent. Unreserve though will be called 3637 * for individual children. We can do this since sum of all 3638 * child's physical sizes is equal to parent's physical size. 3639 * It would not work for potentially bigger allocation sizes. 3640 */ 3641 3642 zio_nowait(cio); 3643 } 3644 3645 /* 3646 * If we used more gang children than the old limit, we must already be 3647 * using the new headers. No need to update anything, just move on. 3648 * 3649 * Otherwise, we might be in a case where we need to turn on the new 3650 * feature, so we check that. We enable the new feature if we didn't 3651 * manage to fit everything into 3 gang children and we could have 3652 * written more than that. 3653 */ 3654 if (g > gbh_nblkptrs(SPA_OLD_GANGBLOCKSIZE)) { 3655 ASSERT(spa_feature_is_active(spa, 3656 SPA_FEATURE_DYNAMIC_GANG_HEADER)); 3657 } else if (any_failed && candidate > SPA_OLD_GANGBLOCKSIZE && 3658 spa_feature_is_enabled(spa, SPA_FEATURE_DYNAMIC_GANG_HEADER) && 3659 !spa_feature_is_active(spa, SPA_FEATURE_DYNAMIC_GANG_HEADER)) { 3660 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, 3661 MAX(txg, spa_syncing_txg(spa) + 1)); 3662 dsl_sync_task_nowait(spa->spa_dsl_pool, 3663 zio_update_feature, 3664 (void *)SPA_FEATURE_DYNAMIC_GANG_HEADER, tx); 3665 dmu_tx_commit(tx); 3666 } 3667 3668 /* 3669 * Set pio's pipeline to just wait for zio to finish. 3670 */ 3671 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 3672 3673 zio_nowait(zio); 3674 3675 return (pio); 3676 } 3677 3678 /* 3679 * The zio_nop_write stage in the pipeline determines if allocating a 3680 * new bp is necessary. The nopwrite feature can handle writes in 3681 * either syncing or open context (i.e. zil writes) and as a result is 3682 * mutually exclusive with dedup. 3683 * 3684 * By leveraging a cryptographically secure checksum, such as SHA256, we 3685 * can compare the checksums of the new data and the old to determine if 3686 * allocating a new block is required. Note that our requirements for 3687 * cryptographic strength are fairly weak: there can't be any accidental 3688 * hash collisions, but we don't need to be secure against intentional 3689 * (malicious) collisions. To trigger a nopwrite, you have to be able 3690 * to write the file to begin with, and triggering an incorrect (hash 3691 * collision) nopwrite is no worse than simply writing to the file. 3692 * That said, there are no known attacks against the checksum algorithms 3693 * used for nopwrite, assuming that the salt and the checksums 3694 * themselves remain secret. 3695 */ 3696 static zio_t * 3697 zio_nop_write(zio_t *zio) 3698 { 3699 blkptr_t *bp = zio->io_bp; 3700 blkptr_t *bp_orig = &zio->io_bp_orig; 3701 zio_prop_t *zp = &zio->io_prop; 3702 3703 ASSERT(BP_IS_HOLE(bp)); 3704 ASSERT0(BP_GET_LEVEL(bp)); 3705 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 3706 ASSERT(zp->zp_nopwrite); 3707 ASSERT(!zp->zp_dedup); 3708 ASSERT0P(zio->io_bp_override); 3709 ASSERT(IO_IS_ALLOCATING(zio)); 3710 3711 /* 3712 * Check to see if the original bp and the new bp have matching 3713 * characteristics (i.e. same checksum, compression algorithms, etc). 3714 * If they don't then just continue with the pipeline which will 3715 * allocate a new bp. 3716 */ 3717 if (BP_IS_HOLE(bp_orig) || 3718 !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags & 3719 ZCHECKSUM_FLAG_NOPWRITE) || 3720 BP_IS_ENCRYPTED(bp) || BP_IS_ENCRYPTED(bp_orig) || 3721 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) || 3722 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) || 3723 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) || 3724 zp->zp_copies != BP_GET_NDVAS(bp_orig)) 3725 return (zio); 3726 3727 /* 3728 * If the checksums match then reset the pipeline so that we 3729 * avoid allocating a new bp and issuing any I/O. 3730 */ 3731 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) { 3732 ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags & 3733 ZCHECKSUM_FLAG_NOPWRITE); 3734 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig)); 3735 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig)); 3736 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF); 3737 ASSERT3U(bp->blk_prop, ==, bp_orig->blk_prop); 3738 3739 /* 3740 * If we're overwriting a block that is currently on an 3741 * indirect vdev, then ignore the nopwrite request and 3742 * allow a new block to be allocated on a concrete vdev. 3743 */ 3744 spa_config_enter(zio->io_spa, SCL_VDEV, FTAG, RW_READER); 3745 for (int d = 0; d < BP_GET_NDVAS(bp_orig); d++) { 3746 vdev_t *tvd = vdev_lookup_top(zio->io_spa, 3747 DVA_GET_VDEV(&bp_orig->blk_dva[d])); 3748 if (tvd->vdev_ops == &vdev_indirect_ops) { 3749 spa_config_exit(zio->io_spa, SCL_VDEV, FTAG); 3750 return (zio); 3751 } 3752 } 3753 spa_config_exit(zio->io_spa, SCL_VDEV, FTAG); 3754 3755 *bp = *bp_orig; 3756 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 3757 zio->io_flags |= ZIO_FLAG_NOPWRITE; 3758 } 3759 3760 return (zio); 3761 } 3762 3763 /* 3764 * ========================================================================== 3765 * Block Reference Table 3766 * ========================================================================== 3767 */ 3768 static zio_t * 3769 zio_brt_free(zio_t *zio) 3770 { 3771 blkptr_t *bp; 3772 3773 bp = zio->io_bp; 3774 3775 if (BP_GET_LEVEL(bp) > 0 || 3776 BP_IS_METADATA(bp) || 3777 !brt_maybe_exists(zio->io_spa, bp)) { 3778 return (zio); 3779 } 3780 3781 if (!brt_entry_decref(zio->io_spa, bp)) { 3782 /* 3783 * This isn't the last reference, so we cannot free 3784 * the data yet. 3785 */ 3786 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 3787 } 3788 3789 return (zio); 3790 } 3791 3792 /* 3793 * ========================================================================== 3794 * Dedup 3795 * ========================================================================== 3796 */ 3797 static void 3798 zio_ddt_child_read_done(zio_t *zio) 3799 { 3800 blkptr_t *bp = zio->io_bp; 3801 ddt_t *ddt; 3802 ddt_entry_t *dde = zio->io_private; 3803 zio_t *pio = zio_unique_parent(zio); 3804 3805 mutex_enter(&pio->io_lock); 3806 ddt = ddt_select(zio->io_spa, bp); 3807 3808 if (zio->io_error == 0) { 3809 ddt_phys_variant_t v = ddt_phys_select(ddt, dde, bp); 3810 /* this phys variant doesn't need repair */ 3811 ddt_phys_clear(dde->dde_phys, v); 3812 } 3813 3814 if (zio->io_error == 0 && dde->dde_io->dde_repair_abd == NULL) 3815 dde->dde_io->dde_repair_abd = zio->io_abd; 3816 else 3817 abd_free(zio->io_abd); 3818 mutex_exit(&pio->io_lock); 3819 } 3820 3821 static zio_t * 3822 zio_ddt_read_start(zio_t *zio) 3823 { 3824 blkptr_t *bp = zio->io_bp; 3825 3826 ASSERT(BP_GET_DEDUP(bp)); 3827 ASSERT(BP_GET_PSIZE(bp) == zio->io_size); 3828 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 3829 3830 if (zio->io_child_error[ZIO_CHILD_DDT]) { 3831 ddt_t *ddt = ddt_select(zio->io_spa, bp); 3832 ddt_entry_t *dde = ddt_repair_start(ddt, bp); 3833 ddt_phys_variant_t v_self = ddt_phys_select(ddt, dde, bp); 3834 ddt_univ_phys_t *ddp = dde->dde_phys; 3835 blkptr_t blk; 3836 3837 ASSERT0P(zio->io_vsd); 3838 zio->io_vsd = dde; 3839 3840 if (v_self == DDT_PHYS_NONE) 3841 return (zio); 3842 3843 /* issue I/O for the other copies */ 3844 for (int p = 0; p < DDT_NPHYS(ddt); p++) { 3845 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p); 3846 3847 if (ddt_phys_birth(ddp, v) == 0 || v == v_self) 3848 continue; 3849 3850 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, 3851 ddp, v, &blk); 3852 zio_nowait(zio_read(zio, zio->io_spa, &blk, 3853 abd_alloc_for_io(zio->io_size, B_TRUE), 3854 zio->io_size, zio_ddt_child_read_done, dde, 3855 zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) | 3856 ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark)); 3857 } 3858 return (zio); 3859 } 3860 3861 zio_nowait(zio_read(zio, zio->io_spa, bp, 3862 zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority, 3863 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark)); 3864 3865 return (zio); 3866 } 3867 3868 static zio_t * 3869 zio_ddt_read_done(zio_t *zio) 3870 { 3871 blkptr_t *bp = zio->io_bp; 3872 3873 if (zio_wait_for_children(zio, ZIO_CHILD_DDT_BIT, ZIO_WAIT_DONE)) { 3874 return (NULL); 3875 } 3876 3877 ASSERT(BP_GET_DEDUP(bp)); 3878 ASSERT(BP_GET_PSIZE(bp) == zio->io_size); 3879 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 3880 3881 if (zio->io_child_error[ZIO_CHILD_DDT]) { 3882 ddt_t *ddt = ddt_select(zio->io_spa, bp); 3883 ddt_entry_t *dde = zio->io_vsd; 3884 if (ddt == NULL) { 3885 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE); 3886 return (zio); 3887 } 3888 if (dde == NULL) { 3889 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1; 3890 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE); 3891 return (NULL); 3892 } 3893 if (dde->dde_io->dde_repair_abd != NULL) { 3894 abd_copy(zio->io_abd, dde->dde_io->dde_repair_abd, 3895 zio->io_size); 3896 zio->io_child_error[ZIO_CHILD_DDT] = 0; 3897 } 3898 ddt_repair_done(ddt, dde); 3899 zio->io_vsd = NULL; 3900 } 3901 3902 ASSERT0P(zio->io_vsd); 3903 3904 return (zio); 3905 } 3906 3907 static boolean_t 3908 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde) 3909 { 3910 spa_t *spa = zio->io_spa; 3911 boolean_t do_raw = !!(zio->io_flags & ZIO_FLAG_RAW); 3912 3913 ASSERT(!(zio->io_bp_override && do_raw)); 3914 3915 /* 3916 * Note: we compare the original data, not the transformed data, 3917 * because when zio->io_bp is an override bp, we will not have 3918 * pushed the I/O transforms. That's an important optimization 3919 * because otherwise we'd compress/encrypt all dmu_sync() data twice. 3920 * However, we should never get a raw, override zio so in these 3921 * cases we can compare the io_abd directly. This is useful because 3922 * it allows us to do dedup verification even if we don't have access 3923 * to the original data (for instance, if the encryption keys aren't 3924 * loaded). 3925 */ 3926 3927 for (int p = 0; p < DDT_NPHYS(ddt); p++) { 3928 if (DDT_PHYS_IS_DITTO(ddt, p)) 3929 continue; 3930 3931 if (dde->dde_io == NULL) 3932 continue; 3933 3934 /* 3935 * Lock dde_io to prevent the lead zio from completing 3936 * and freeing its ABD while we compare against it. 3937 */ 3938 mutex_enter(&dde->dde_io->dde_io_lock); 3939 zio_t *lio = dde->dde_io->dde_lead_zio[p]; 3940 if (lio == NULL) { 3941 mutex_exit(&dde->dde_io->dde_io_lock); 3942 continue; 3943 } 3944 boolean_t collision; 3945 if (do_raw) { 3946 collision = lio->io_size != zio->io_size || 3947 abd_cmp(zio->io_abd, lio->io_abd) != 0; 3948 } else { 3949 collision = lio->io_orig_size != zio->io_orig_size || 3950 abd_cmp(zio->io_orig_abd, lio->io_orig_abd) != 0; 3951 } 3952 mutex_exit(&dde->dde_io->dde_io_lock); 3953 return (collision); 3954 } 3955 3956 for (int p = 0; p < DDT_NPHYS(ddt); p++) { 3957 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p); 3958 uint64_t phys_birth = ddt_phys_birth(dde->dde_phys, v); 3959 3960 if (phys_birth != 0 && do_raw) { 3961 blkptr_t blk = *zio->io_bp; 3962 uint64_t psize; 3963 abd_t *tmpabd; 3964 int error; 3965 3966 ddt_bp_fill(dde->dde_phys, v, &blk, phys_birth); 3967 psize = BP_GET_PSIZE(&blk); 3968 3969 if (psize != zio->io_size) 3970 return (B_TRUE); 3971 3972 ddt_exit(ddt); 3973 3974 tmpabd = abd_alloc_for_io(psize, B_TRUE); 3975 3976 error = zio_wait(zio_read(NULL, spa, &blk, tmpabd, 3977 psize, NULL, NULL, ZIO_PRIORITY_SYNC_READ, 3978 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 3979 ZIO_FLAG_RAW, &zio->io_bookmark)); 3980 3981 if (error == 0) { 3982 if (abd_cmp(tmpabd, zio->io_abd) != 0) 3983 error = SET_ERROR(ENOENT); 3984 } 3985 3986 abd_free(tmpabd); 3987 ddt_enter(ddt); 3988 return (error != 0); 3989 } else if (phys_birth != 0) { 3990 arc_buf_t *abuf = NULL; 3991 arc_flags_t aflags = ARC_FLAG_WAIT; 3992 blkptr_t blk = *zio->io_bp; 3993 int error; 3994 3995 ddt_bp_fill(dde->dde_phys, v, &blk, phys_birth); 3996 3997 if (BP_GET_LSIZE(&blk) != zio->io_orig_size) 3998 return (B_TRUE); 3999 4000 ddt_exit(ddt); 4001 4002 error = arc_read(NULL, spa, &blk, 4003 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ, 4004 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 4005 &aflags, &zio->io_bookmark); 4006 4007 if (error == 0) { 4008 if (abd_cmp_buf(zio->io_orig_abd, abuf->b_data, 4009 zio->io_orig_size) != 0) 4010 error = SET_ERROR(ENOENT); 4011 arc_buf_destroy(abuf, &abuf); 4012 } 4013 4014 ddt_enter(ddt); 4015 return (error != 0); 4016 } 4017 } 4018 4019 return (B_FALSE); 4020 } 4021 4022 static void 4023 zio_ddt_child_write_done(zio_t *zio) 4024 { 4025 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp); 4026 ddt_entry_t *dde = zio->io_private; 4027 4028 zio_link_t *zl = NULL; 4029 ASSERT3P(zio_walk_parents(zio, &zl), !=, NULL); 4030 4031 int p = DDT_PHYS_FOR_COPIES(ddt, zio->io_prop.zp_copies); 4032 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p); 4033 ddt_univ_phys_t *ddp = dde->dde_phys; 4034 4035 mutex_enter(&dde->dde_io->dde_io_lock); 4036 4037 /* we're the lead, so once we're done there's no one else outstanding */ 4038 if (dde->dde_io->dde_lead_zio[p] == zio) 4039 dde->dde_io->dde_lead_zio[p] = NULL; 4040 4041 ddt_univ_phys_t *orig = &dde->dde_io->dde_orig_phys; 4042 4043 if (zio->io_error != 0) { 4044 /* 4045 * The write failed, so we're about to abort the entire IO 4046 * chain. We need to revert the entry back to what it was at 4047 * the last time it was successfully extended. 4048 */ 4049 ddt_phys_unextend(ddp, orig, v); 4050 ddt_phys_clear(orig, v); 4051 4052 mutex_exit(&dde->dde_io->dde_io_lock); 4053 4054 /* 4055 * Undo the optimistic refcount increments that were done in 4056 * zio_ddt_write() for all non-DDT-child parents. Since errors 4057 * are rare, taking the global lock here is acceptable. 4058 */ 4059 ddt_enter(ddt); 4060 zio_t *pio; 4061 zl = NULL; 4062 while ((pio = zio_walk_parents(zio, &zl)) != NULL) { 4063 if (!(pio->io_flags & ZIO_FLAG_DDT_CHILD)) 4064 ddt_phys_decref(ddp, v); 4065 } 4066 ddt_exit(ddt); 4067 return; 4068 } 4069 4070 /* 4071 * We've successfully added new DVAs to the entry. Clear the saved 4072 * state or, if there's still outstanding IO, remember it so we can 4073 * revert to a known good state if that IO fails. 4074 */ 4075 if (dde->dde_io->dde_lead_zio[p] == NULL) 4076 ddt_phys_clear(orig, v); 4077 else 4078 ddt_phys_copy(orig, ddp, v); 4079 4080 mutex_exit(&dde->dde_io->dde_io_lock); 4081 } 4082 4083 static void 4084 zio_ddt_child_write_ready(zio_t *zio) 4085 { 4086 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp); 4087 ddt_entry_t *dde = zio->io_private; 4088 4089 zio_link_t *zl = NULL; 4090 ASSERT3P(zio_walk_parents(zio, &zl), !=, NULL); 4091 4092 int p = DDT_PHYS_FOR_COPIES(ddt, zio->io_prop.zp_copies); 4093 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p); 4094 4095 if (ddt_phys_is_gang(dde->dde_phys, v)) { 4096 for (int i = 0; i < BP_GET_NDVAS(zio->io_bp); i++) { 4097 dva_t *d = &zio->io_bp->blk_dva[i]; 4098 metaslab_group_alloc_decrement(zio->io_spa, 4099 DVA_GET_VDEV(d), zio->io_allocator, 4100 METASLAB_ASYNC_ALLOC, zio->io_size, zio); 4101 } 4102 zio->io_error = EAGAIN; 4103 } 4104 4105 if (zio->io_error != 0) 4106 return; 4107 4108 mutex_enter(&dde->dde_io->dde_io_lock); 4109 4110 ddt_phys_extend(dde->dde_phys, v, zio->io_bp); 4111 4112 zio_t *pio; 4113 zl = NULL; 4114 while ((pio = zio_walk_parents(zio, &zl)) != NULL) { 4115 if (!(pio->io_flags & ZIO_FLAG_DDT_CHILD)) 4116 ddt_bp_fill(dde->dde_phys, v, pio->io_bp, zio->io_txg); 4117 } 4118 4119 mutex_exit(&dde->dde_io->dde_io_lock); 4120 } 4121 4122 static zio_t * 4123 zio_ddt_write(zio_t *zio) 4124 { 4125 spa_t *spa = zio->io_spa; 4126 blkptr_t *bp = zio->io_bp; 4127 uint64_t txg = zio->io_txg; 4128 zio_prop_t *zp = &zio->io_prop; 4129 ddt_t *ddt = ddt_select(spa, bp); 4130 ddt_entry_t *dde; 4131 4132 ASSERT(BP_GET_DEDUP(bp)); 4133 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum); 4134 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override); 4135 ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW))); 4136 /* 4137 * Deduplication will not take place for Direct I/O writes. The 4138 * ddt_tree will be emptied in syncing context. Direct I/O writes take 4139 * place in the open-context. Direct I/O write can not attempt to 4140 * modify the ddt_tree while issuing out a write. 4141 */ 4142 ASSERT3B(zio->io_prop.zp_direct_write, ==, B_FALSE); 4143 4144 ddt_enter(ddt); 4145 /* 4146 * Search DDT for matching entry. Skip DVAs verification here, since 4147 * they can go only from override, and once we get here the override 4148 * pointer can't have "D" flag to be confused with pruned DDT entries. 4149 */ 4150 IMPLY(zio->io_bp_override, !BP_GET_DEDUP(zio->io_bp_override)); 4151 dde = ddt_lookup(ddt, bp, B_FALSE); 4152 if (dde == NULL) { 4153 /* DDT size is over its quota so no new entries */ 4154 ddt_exit(ddt); 4155 zp->zp_dedup = B_FALSE; 4156 BP_SET_DEDUP(bp, B_FALSE); 4157 if (zio->io_bp_override == NULL) 4158 zio->io_pipeline = ZIO_WRITE_PIPELINE; 4159 return (zio); 4160 } 4161 4162 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) { 4163 /* 4164 * If we're using a weak checksum, upgrade to a strong checksum 4165 * and try again. If we're already using a strong checksum, 4166 * we can't resolve it, so just convert to an ordinary write. 4167 * (And automatically e-mail a paper to Nature?) 4168 */ 4169 ddt_exit(ddt); 4170 if (!(zio_checksum_table[zp->zp_checksum].ci_flags & 4171 ZCHECKSUM_FLAG_DEDUP)) { 4172 zp->zp_checksum = spa_dedup_checksum(spa); 4173 zio_pop_transforms(zio); 4174 zio->io_stage = ZIO_STAGE_OPEN; 4175 BP_ZERO(bp); 4176 } else { 4177 zp->zp_dedup = B_FALSE; 4178 BP_SET_DEDUP(bp, B_FALSE); 4179 } 4180 ASSERT(!BP_GET_DEDUP(bp)); 4181 zio->io_pipeline = ZIO_WRITE_PIPELINE; 4182 return (zio); 4183 } 4184 4185 int p = DDT_PHYS_FOR_COPIES(ddt, zp->zp_copies); 4186 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p); 4187 4188 /* 4189 * In the common cases, at this point we have a regular BP with no 4190 * allocated DVAs, and the corresponding DDT entry for its checksum. 4191 * Our goal is to fill the BP with enough DVAs to satisfy its copies= 4192 * requirement. 4193 * 4194 * One of three things needs to happen to fulfill this: 4195 * 4196 * - if the DDT entry has enough DVAs to satisfy the BP, we just copy 4197 * them out of the entry and return; 4198 * 4199 * - if the DDT entry has no DVAs (ie its brand new), then we have to 4200 * issue the write as normal so that DVAs can be allocated and the 4201 * data land on disk. We then copy the DVAs into the DDT entry on 4202 * return. 4203 * 4204 * - if the DDT entry has some DVAs, but too few, we have to issue the 4205 * write, adjusted to have allocate fewer copies. When it returns, we 4206 * add the new DVAs to the DDT entry, and update the BP to have the 4207 * full amount it originally requested. 4208 * 4209 * In all cases, if there's already a writing IO in flight, we need to 4210 * defer the action until after the write is done. If our action is to 4211 * write, we need to adjust our request for additional DVAs to match 4212 * what will be in the DDT entry after it completes. In this way every 4213 * IO can be guaranteed to recieve enough DVAs simply by joining the 4214 * end of the chain and letting the sequence play out. 4215 */ 4216 4217 /* Number of DVAs requested by the IO. */ 4218 uint8_t need_dvas = zp->zp_copies; 4219 /* Number of DVAs in outstanding writes for this dde. */ 4220 uint8_t parent_dvas = 0; 4221 4222 /* 4223 * What we do next depends on whether or not there's IO outstanding 4224 * that will update this entry. If dde_io exists, we need to hold 4225 * its lock to safely check and use dde_lead_zio. 4226 */ 4227 ddt_entry_io_t *dde_io = dde->dde_io; 4228 if (dde_io != NULL) 4229 mutex_enter(&dde_io->dde_io_lock); 4230 4231 /* 4232 * Number of DVAs in the DDT entry. If the BP is encrypted we ignore 4233 * the third one as normal. 4234 * 4235 * Must be computed after taking dde_io_lock (if held) to avoid 4236 * racing with ddt_phys_unextend() in zio_ddt_child_write_done() 4237 * error path, which can zero DVAs under dde_io_lock. Without the 4238 * lock, a stale have_dvas causes ddt_bp_fill() to copy a zeroed 4239 * DVA into the BP, producing a hole that reads back as zeros. 4240 */ 4241 ddt_univ_phys_t *ddp = dde->dde_phys; 4242 int have_dvas = ddt_phys_dva_count(ddp, v, BP_IS_ENCRYPTED(bp)); 4243 IMPLY(have_dvas == 0, ddt_phys_birth(ddp, v) == 0); 4244 boolean_t is_ganged = ddt_phys_is_gang(ddp, v); 4245 4246 if (dde_io == NULL || dde_io->dde_lead_zio[p] == NULL) { 4247 /* 4248 * No IO outstanding, so we only need to worry about ourselves. 4249 */ 4250 4251 /* 4252 * Override BPs bring their own DVAs and their own problems. 4253 */ 4254 if (zio->io_bp_override) { 4255 /* 4256 * For a brand-new entry, all the work has been done 4257 * for us, and we can just fill it out from the provided 4258 * block and leave. 4259 */ 4260 if (have_dvas == 0) { 4261 if (dde_io != NULL) 4262 mutex_exit(&dde_io->dde_io_lock); 4263 ASSERT(BP_GET_BIRTH(bp) == txg); 4264 ASSERT(BP_EQUAL(bp, zio->io_bp_override)); 4265 ddt_phys_extend(ddp, v, bp); 4266 ddt_phys_addref(ddp, v); 4267 ddt_exit(ddt); 4268 return (zio); 4269 } 4270 4271 /* 4272 * If we already have this entry, then we want to treat 4273 * it like a regular write. To do this we just wipe 4274 * them out and proceed like a regular write. 4275 * 4276 * Even if there are some DVAs in the entry, we still 4277 * have to clear them out. We can't use them to fill 4278 * out the dedup entry, as they are all referenced 4279 * together by a bp already on disk, and will be freed 4280 * as a group. 4281 */ 4282 BP_ZERO_DVAS(bp); 4283 BP_SET_BIRTH(bp, 0, 0); 4284 } 4285 4286 /* 4287 * If there are enough DVAs in the entry to service our request, 4288 * then we can just use them as-is. 4289 */ 4290 if (have_dvas >= need_dvas) { 4291 if (dde_io != NULL) 4292 mutex_exit(&dde_io->dde_io_lock); 4293 4294 /* 4295 * For rewrite operations, try preserving the original 4296 * logical birth time. If the result matches the 4297 * original BP, this becomes a NOP. 4298 */ 4299 if (zp->zp_rewrite) { 4300 uint64_t orig_logical_birth = 4301 BP_GET_LOGICAL_BIRTH(&zio->io_bp_orig); 4302 ddt_bp_fill(ddp, v, bp, orig_logical_birth); 4303 if (BP_EQUAL(bp, &zio->io_bp_orig)) { 4304 /* We can skip accounting. */ 4305 ddt_exit(ddt); 4306 zio->io_flags |= ZIO_FLAG_NOPWRITE; 4307 return (zio); 4308 } 4309 } 4310 4311 ddt_bp_fill(ddp, v, bp, txg); 4312 ddt_phys_addref(ddp, v); 4313 ddt_exit(ddt); 4314 return (zio); 4315 } 4316 4317 /* 4318 * Otherwise, we have to issue IO to fill the entry up to the 4319 * amount we need. 4320 */ 4321 need_dvas -= have_dvas; 4322 } else { 4323 /* 4324 * There's a write in-flight. If there's already enough DVAs on 4325 * the entry, then either there were already enough to start 4326 * with, or the in-flight IO is between READY and DONE, and so 4327 * has extended the entry with new DVAs. Either way, we don't 4328 * need to do anything, we can just slot in behind it. 4329 */ 4330 4331 if (zio->io_bp_override) { 4332 /* 4333 * If there's a write out, then we're soon going to 4334 * have our own copies of this block, so clear out the 4335 * override block and treat it as a regular dedup 4336 * write. See comment above. 4337 */ 4338 BP_ZERO_DVAS(bp); 4339 BP_SET_BIRTH(bp, 0, 0); 4340 } 4341 4342 if (have_dvas >= need_dvas) { 4343 /* 4344 * A minor point: there might already be enough 4345 * committed DVAs in the entry to service our request, 4346 * but we don't know which are completed and which are 4347 * allocated but not yet written. In this case, should 4348 * the IO for the new DVAs fail, we will be on the end 4349 * of the IO chain and will also recieve an error, even 4350 * though our request could have been serviced. 4351 * 4352 * This is an extremely rare case, as it requires the 4353 * original block to be copied with a request for a 4354 * larger number of DVAs, then copied again requesting 4355 * the same (or already fulfilled) number of DVAs while 4356 * the first request is active, and then that first 4357 * request errors. In return, the logic required to 4358 * catch and handle it is complex. For now, I'm just 4359 * not going to bother with it. 4360 */ 4361 4362 /* 4363 * We always fill the bp here as we may have arrived 4364 * after the in-flight write has passed READY, and so 4365 * missed out. 4366 */ 4367 ddt_bp_fill(ddp, v, bp, txg); 4368 piggyback: 4369 zio_add_child(zio, dde_io->dde_lead_zio[p]); 4370 4371 /* 4372 * Optimistically increment refcount for this parent. 4373 * If the write fails, zio_ddt_child_write_done() will 4374 * decrement for all non-DDT-child parents. 4375 */ 4376 ddt_phys_addref(ddp, v); 4377 mutex_exit(&dde_io->dde_io_lock); 4378 ddt_exit(ddt); 4379 return (zio); 4380 } 4381 4382 /* 4383 * There's not enough in the entry yet, so we need to look at 4384 * the write in-flight and see how many DVAs it will have once 4385 * it completes. 4386 * 4387 * The in-flight write has potentially had its copies request 4388 * reduced (if we're filling out an existing entry), so we need 4389 * to reach in and get the original write to find out what it is 4390 * expecting. 4391 * 4392 * Note that the parent of the lead zio will always have the 4393 * highest zp_copies of any zio in the chain, because ones that 4394 * can be serviced without additional IO are always added to 4395 * the back of the chain. 4396 */ 4397 zio_link_t *zl = NULL; 4398 zio_t *pio = 4399 zio_walk_parents(dde->dde_io->dde_lead_zio[p], &zl); 4400 ASSERT(pio); 4401 parent_dvas = pio->io_prop.zp_copies; 4402 4403 if (parent_dvas >= need_dvas) 4404 goto piggyback; 4405 4406 /* 4407 * Still not enough, so we will need to issue to get the 4408 * shortfall. 4409 */ 4410 need_dvas -= parent_dvas; 4411 } 4412 4413 if (is_ganged) { 4414 if (dde_io != NULL) 4415 mutex_exit(&dde_io->dde_io_lock); 4416 ddt_exit(ddt); 4417 zp->zp_dedup = B_FALSE; 4418 BP_SET_DEDUP(bp, B_FALSE); 4419 zio->io_pipeline = ZIO_WRITE_PIPELINE; 4420 return (zio); 4421 } 4422 4423 /* 4424 * We need to write. We will create a new write with the copies 4425 * property adjusted to match the number of DVAs we need to grow 4426 * the DDT entry by to satisfy the request. 4427 */ 4428 zio_prop_t czp; 4429 if (have_dvas > 0 || parent_dvas > 0) { 4430 czp = *zp; 4431 czp.zp_copies = need_dvas; 4432 czp.zp_gang_copies = 0; 4433 zp = &czp; 4434 } else { 4435 ASSERT3U(zp->zp_copies, ==, need_dvas); 4436 } 4437 4438 zio_t *cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd, 4439 zio->io_orig_size, zio->io_orig_size, zp, 4440 zio_ddt_child_write_ready, NULL, 4441 zio_ddt_child_write_done, dde, zio->io_priority, 4442 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark); 4443 zio_inherit_allocator(zio, cio); 4444 4445 zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL); 4446 4447 /* 4448 * We are the new lead zio, because our parent has the highest 4449 * zp_copies that has been requested for this entry so far. 4450 */ 4451 if (dde_io == NULL) { 4452 /* 4453 * New dde_io. No lock needed since no other thread can have 4454 * a reference yet. 4455 */ 4456 ddt_alloc_entry_io(dde); 4457 dde_io = dde->dde_io; 4458 /* 4459 * First time out, take a copy of the stable entry to revert 4460 * to if there's an error (see zio_ddt_child_write_done()) 4461 */ 4462 ddt_phys_copy(&dde_io->dde_orig_phys, dde->dde_phys, v); 4463 dde_io->dde_lead_zio[p] = cio; 4464 } else { 4465 if (dde_io->dde_lead_zio[p] == NULL) { 4466 /* 4467 * First time out, take a copy of the stable entry 4468 * to revert to if there's an error (see 4469 * zio_ddt_child_write_done()) 4470 */ 4471 ddt_phys_copy(&dde_io->dde_orig_phys, dde->dde_phys, 4472 v); 4473 } else { 4474 /* 4475 * Make the existing chain our child, because it 4476 * cannot complete until we have. 4477 */ 4478 zio_add_child(cio, dde_io->dde_lead_zio[p]); 4479 } 4480 dde_io->dde_lead_zio[p] = cio; 4481 mutex_exit(&dde_io->dde_io_lock); 4482 } 4483 4484 /* 4485 * Optimistically increment the refcount for this dedup write. 4486 * If the write fails, zio_ddt_child_write_done() will decrement 4487 * for all non-DDT-child parents. 4488 */ 4489 ddt_phys_addref(ddp, v); 4490 4491 ddt_exit(ddt); 4492 4493 zio_nowait(cio); 4494 4495 return (zio); 4496 } 4497 4498 static ddt_entry_t *freedde; /* for debugging */ 4499 4500 static zio_t * 4501 zio_ddt_free(zio_t *zio) 4502 { 4503 spa_t *spa = zio->io_spa; 4504 blkptr_t *bp = zio->io_bp; 4505 ddt_t *ddt = ddt_select(spa, bp); 4506 ddt_entry_t *dde = NULL; 4507 4508 ASSERT(BP_GET_DEDUP(bp)); 4509 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 4510 4511 ddt_enter(ddt); 4512 freedde = dde = ddt_lookup(ddt, bp, B_TRUE); 4513 if (dde) { 4514 ddt_phys_variant_t v = ddt_phys_select(ddt, dde, bp); 4515 if (v != DDT_PHYS_NONE) 4516 ddt_phys_decref(dde->dde_phys, v); 4517 else 4518 /* 4519 * No phys matches this BP; ddt_lookup() returned a 4520 * fresh, empty entry because the key is not in the 4521 * table at all (eg the original entry was pruned). 4522 * There is no reference to release, so we need to do 4523 * a normal (not dedup) free. Clear dde so we fall 4524 * into the block below. 4525 */ 4526 dde = NULL; 4527 } 4528 ddt_exit(ddt); 4529 4530 if (dde) { 4531 /* 4532 * DDT entry found and the refcount has been decremented. 4533 * Stop the pipeline — there is nothing more to do right now. 4534 */ 4535 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 4536 } else { 4537 /* 4538 * No DDT entry; the block must have been pruned from the 4539 * table. Clear the DEDUP bit so it is treated as a normal 4540 * block from here on. BRT_FREE and DVA_FREE follow in the 4541 * pipeline and will handle any cloned references and the 4542 * actual block free respectively, along with the gang stages 4543 * for a gang BP. 4544 * 4545 * Only flat (FDT) tables are ever pruned, so a miss against 4546 * a traditional table means the table and the BP disagree, 4547 * which should not be possible. The plain free below is 4548 * still the best we can do for this BP, but leave a trace. 4549 */ 4550 if (!(ddt->ddt_flags & DDT_FLAG_FLAT)) { 4551 zfs_dbgmsg("%s: no matching traditional DDT phys for " 4552 "dedup BP DVA[0]=<%llu:%llx:%llx> phys_birth=%llu; " 4553 "freeing without a refcount decrement", 4554 spa_name(spa), 4555 (u_longlong_t)DVA_GET_VDEV(&bp->blk_dva[0]), 4556 (u_longlong_t)DVA_GET_OFFSET(&bp->blk_dva[0]), 4557 (u_longlong_t)DVA_GET_ASIZE(&bp->blk_dva[0]), 4558 (u_longlong_t)BP_GET_PHYSICAL_BIRTH(bp)); 4559 } 4560 BP_SET_DEDUP(bp, 0); 4561 } 4562 4563 return (zio); 4564 } 4565 4566 /* 4567 * ========================================================================== 4568 * Allocate and free blocks 4569 * ========================================================================== 4570 */ 4571 4572 static zio_t * 4573 zio_io_to_allocate(metaslab_class_allocator_t *mca, boolean_t *more) 4574 { 4575 zio_t *zio; 4576 4577 ASSERT(MUTEX_HELD(&mca->mca_lock)); 4578 4579 zio = avl_first(&mca->mca_tree); 4580 if (zio == NULL) { 4581 *more = B_FALSE; 4582 return (NULL); 4583 } 4584 4585 ASSERT(IO_IS_ALLOCATING(zio)); 4586 ASSERT(ZIO_HAS_ALLOCATOR(zio)); 4587 4588 /* 4589 * Try to place a reservation for this zio. If we're unable to 4590 * reserve then we throttle. 4591 */ 4592 if (!metaslab_class_throttle_reserve(zio->io_metaslab_class, 4593 zio->io_allocator, zio->io_prop.zp_copies, zio->io_size, 4594 B_FALSE, more)) { 4595 return (NULL); 4596 } 4597 zio->io_flags |= ZIO_FLAG_ALLOC_THROTTLED; 4598 4599 avl_remove(&mca->mca_tree, zio); 4600 ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE); 4601 4602 if (avl_is_empty(&mca->mca_tree)) 4603 *more = B_FALSE; 4604 return (zio); 4605 } 4606 4607 static zio_t * 4608 zio_dva_throttle(zio_t *zio) 4609 { 4610 spa_t *spa = zio->io_spa; 4611 zio_t *nio; 4612 metaslab_class_t *mc; 4613 boolean_t more; 4614 4615 /* 4616 * If not already chosen, choose an appropriate allocation class. 4617 */ 4618 mc = zio->io_metaslab_class; 4619 if (mc == NULL) 4620 mc = spa_preferred_class(spa, zio); 4621 4622 if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE || 4623 !mc->mc_alloc_throttle_enabled || 4624 zio->io_child_type == ZIO_CHILD_GANG || 4625 zio->io_flags & ZIO_FLAG_NODATA) { 4626 return (zio); 4627 } 4628 4629 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 4630 ASSERT(ZIO_HAS_ALLOCATOR(zio)); 4631 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 4632 ASSERT3U(zio->io_queued_timestamp, >, 0); 4633 ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE); 4634 4635 zio->io_metaslab_class = mc; 4636 metaslab_class_allocator_t *mca = &mc->mc_allocator[zio->io_allocator]; 4637 mutex_enter(&mca->mca_lock); 4638 avl_add(&mca->mca_tree, zio); 4639 nio = zio_io_to_allocate(mca, &more); 4640 mutex_exit(&mca->mca_lock); 4641 return (nio); 4642 } 4643 4644 static void 4645 zio_allocate_dispatch(metaslab_class_t *mc, int allocator) 4646 { 4647 metaslab_class_allocator_t *mca = &mc->mc_allocator[allocator]; 4648 zio_t *zio; 4649 boolean_t more; 4650 4651 do { 4652 mutex_enter(&mca->mca_lock); 4653 zio = zio_io_to_allocate(mca, &more); 4654 mutex_exit(&mca->mca_lock); 4655 if (zio == NULL) 4656 return; 4657 4658 ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE); 4659 ASSERT0(zio->io_error); 4660 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE); 4661 } while (more); 4662 } 4663 4664 static zio_t * 4665 zio_dva_allocate(zio_t *zio) 4666 { 4667 spa_t *spa = zio->io_spa; 4668 metaslab_class_t *mc, *newmc; 4669 blkptr_t *bp = zio->io_bp; 4670 int error; 4671 int flags = 0; 4672 4673 if (zio->io_gang_leader == NULL) { 4674 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 4675 zio->io_gang_leader = zio; 4676 } 4677 if (zio->io_flags & ZIO_FLAG_PREALLOCATED) { 4678 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_GANG); 4679 memcpy(zio->io_bp->blk_dva, zio->io_bp_orig.blk_dva, 4680 3 * sizeof (dva_t)); 4681 BP_SET_LOGICAL_BIRTH(zio->io_bp, 4682 BP_GET_LOGICAL_BIRTH(&zio->io_bp_orig)); 4683 BP_SET_PHYSICAL_BIRTH(zio->io_bp, 4684 BP_GET_RAW_PHYSICAL_BIRTH(&zio->io_bp_orig)); 4685 return (zio); 4686 } 4687 4688 ASSERT(BP_IS_HOLE(bp)); 4689 ASSERT0(BP_GET_NDVAS(bp)); 4690 ASSERT3U(zio->io_prop.zp_copies, >, 0); 4691 4692 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa)); 4693 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp)); 4694 4695 if (zio->io_flags & ZIO_FLAG_GANG_CHILD) 4696 flags |= METASLAB_GANG_CHILD; 4697 if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE) 4698 flags |= METASLAB_ASYNC_ALLOC; 4699 4700 /* 4701 * If not already chosen, choose an appropriate allocation class. 4702 */ 4703 mc = zio->io_metaslab_class; 4704 if (mc == NULL) { 4705 mc = spa_preferred_class(spa, zio); 4706 zio->io_metaslab_class = mc; 4707 } 4708 ZIOSTAT_BUMP(ziostat_total_allocations); 4709 4710 again: 4711 /* 4712 * Try allocating the block in the usual metaslab class. 4713 * If that's full, allocate it in some other class(es). 4714 * If that's full, allocate as a gang block, 4715 * and if all are full, the allocation fails (which shouldn't happen). 4716 * 4717 * Note that we do not fall back on embedded slog (ZIL) space, to 4718 * preserve unfragmented slog space, which is critical for decent 4719 * sync write performance. If a log allocation fails, we will fall 4720 * back to spa_sync() which is abysmal for performance. 4721 */ 4722 ASSERT(ZIO_HAS_ALLOCATOR(zio)); 4723 error = metaslab_alloc(spa, mc, zio->io_size, bp, 4724 zio->io_prop.zp_copies, zio->io_txg, NULL, flags, 4725 ZIO_ALLOC_LIST(zio), zio->io_allocator, zio); 4726 4727 /* 4728 * When the dedup or special class is spilling into the normal class, 4729 * there can still be significant space available due to deferred 4730 * frees that are in-flight. We track the txg when this occurred and 4731 * back off adding new DDT entries for a few txgs to allow the free 4732 * blocks to be processed. 4733 */ 4734 if (error == ENOSPC && spa->spa_dedup_class_full_txg != zio->io_txg && 4735 (mc == spa_dedup_class(spa) || (mc == spa_special_class(spa) && 4736 !spa_has_dedup(spa) && spa_special_has_ddt(spa)))) { 4737 spa->spa_dedup_class_full_txg = zio->io_txg; 4738 zfs_dbgmsg("%s[%llu]: %s class spilling, req size %llu, " 4739 "%llu allocated of %llu", 4740 spa_name(spa), (u_longlong_t)zio->io_txg, 4741 metaslab_class_get_name(mc), 4742 (u_longlong_t)zio->io_size, 4743 (u_longlong_t)metaslab_class_get_alloc(mc), 4744 (u_longlong_t)metaslab_class_get_space(mc)); 4745 } 4746 4747 /* 4748 * Fall back to some other class when this one is full. 4749 */ 4750 if (error == ENOSPC && (newmc = spa_preferred_class(spa, zio)) != mc) { 4751 /* 4752 * If we are holding old class reservation, drop it. 4753 * Dispatch the next ZIO(s) there if some are waiting. 4754 */ 4755 if (zio->io_flags & ZIO_FLAG_ALLOC_THROTTLED) { 4756 if (metaslab_class_throttle_unreserve(mc, 4757 zio->io_allocator, zio->io_prop.zp_copies, 4758 zio->io_size)) { 4759 zio_allocate_dispatch(zio->io_metaslab_class, 4760 zio->io_allocator); 4761 } 4762 zio->io_flags &= ~ZIO_FLAG_ALLOC_THROTTLED; 4763 } 4764 4765 if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) { 4766 zfs_dbgmsg("%s: metaslab allocation failure in %s " 4767 "class, trying fallback to %s class: zio %px, " 4768 "size %llu, error %d", spa_name(spa), 4769 metaslab_class_get_name(mc), 4770 metaslab_class_get_name(newmc), 4771 zio, (u_longlong_t)zio->io_size, error); 4772 } 4773 zio->io_metaslab_class = mc = newmc; 4774 ZIOSTAT_BUMP(ziostat_alloc_class_fallbacks); 4775 4776 /* 4777 * If the new class uses throttling, return to that pipeline 4778 * stage. Otherwise just do another allocation attempt. 4779 */ 4780 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE && 4781 mc->mc_alloc_throttle_enabled && 4782 zio->io_child_type != ZIO_CHILD_GANG && 4783 !(zio->io_flags & ZIO_FLAG_NODATA)) { 4784 zio->io_stage = ZIO_STAGE_DVA_THROTTLE >> 1; 4785 return (zio); 4786 } 4787 goto again; 4788 } 4789 4790 if (error == ENOSPC && zio->io_size > spa->spa_min_alloc) { 4791 if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) { 4792 zfs_dbgmsg("%s: metaslab allocation failure, " 4793 "trying ganging: zio %px, size %llu, error %d", 4794 spa_name(spa), zio, (u_longlong_t)zio->io_size, 4795 error); 4796 } 4797 ZIOSTAT_BUMP(ziostat_gang_writes); 4798 if (flags & METASLAB_GANG_CHILD) 4799 ZIOSTAT_BUMP(ziostat_gang_multilevel); 4800 return (zio_write_gang_block(zio, mc)); 4801 } 4802 if (error != 0) { 4803 if (error != ENOSPC || 4804 (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC)) { 4805 zfs_dbgmsg("%s: metaslab allocation failure: zio %px, " 4806 "size %llu, error %d", 4807 spa_name(spa), zio, (u_longlong_t)zio->io_size, 4808 error); 4809 } 4810 zio->io_error = error; 4811 } else if (zio->io_prop.zp_rewrite) { 4812 /* 4813 * For rewrite operations, preserve the logical birth time 4814 * but set the physical birth time to the current txg. 4815 */ 4816 uint64_t logical_birth = BP_GET_LOGICAL_BIRTH(&zio->io_bp_orig); 4817 ASSERT3U(logical_birth, <=, zio->io_txg); 4818 BP_SET_BIRTH(zio->io_bp, logical_birth, zio->io_txg); 4819 BP_SET_REWRITE(zio->io_bp, 1); 4820 } 4821 4822 return (zio); 4823 } 4824 4825 static zio_t * 4826 zio_dva_free(zio_t *zio) 4827 { 4828 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE); 4829 4830 return (zio); 4831 } 4832 4833 static zio_t * 4834 zio_dva_claim(zio_t *zio) 4835 { 4836 int error; 4837 4838 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg); 4839 if (error) 4840 zio->io_error = error; 4841 4842 return (zio); 4843 } 4844 4845 /* 4846 * Undo an allocation. This is used by zio_done() when an I/O fails 4847 * and we want to give back the block we just allocated. 4848 * This handles both normal blocks and gang blocks. 4849 */ 4850 static void 4851 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp) 4852 { 4853 ASSERT(BP_GET_BIRTH(bp) == zio->io_txg || BP_IS_HOLE(bp)); 4854 ASSERT0P(zio->io_bp_override); 4855 4856 if (!BP_IS_HOLE(bp)) { 4857 metaslab_free(zio->io_spa, bp, BP_GET_BIRTH(bp), B_TRUE); 4858 } 4859 4860 if (gn != NULL) { 4861 for (int g = 0; g < gbh_nblkptrs(gn->gn_gangblocksize); g++) { 4862 zio_dva_unallocate(zio, gn->gn_child[g], 4863 gbh_bp(gn->gn_gbh, g)); 4864 } 4865 } 4866 } 4867 4868 /* 4869 * Try to allocate an intent log block. Return 0 on success, errno on failure. 4870 */ 4871 int 4872 zio_alloc_zil(spa_t *spa, objset_t *os, uint64_t txg, blkptr_t *new_bp, 4873 uint64_t min_size, uint64_t max_size, boolean_t *slog, 4874 boolean_t allow_larger) 4875 { 4876 int error; 4877 zio_alloc_list_t io_alloc_list; 4878 uint64_t alloc_size = 0; 4879 4880 ASSERT(txg > spa_syncing_txg(spa)); 4881 ASSERT3U(min_size, <=, max_size); 4882 4883 metaslab_trace_init(&io_alloc_list); 4884 4885 /* 4886 * Block pointer fields are useful to metaslabs for stats and debugging. 4887 * Fill in the obvious ones before calling into metaslab_alloc(). 4888 */ 4889 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG); 4890 BP_SET_PSIZE(new_bp, max_size); 4891 BP_SET_LEVEL(new_bp, 0); 4892 4893 /* 4894 * When allocating a zil block, we don't have information about 4895 * the final destination of the block except the objset it's part 4896 * of, so we just hash the objset ID to pick the allocator to get 4897 * some parallelism. 4898 */ 4899 int flags = METASLAB_ZIL; 4900 int allocator = (uint_t)cityhash1(os->os_dsl_dataset->ds_object) 4901 % spa->spa_alloc_count; 4902 ZIOSTAT_BUMP(ziostat_total_allocations); 4903 4904 /* Try log class (dedicated slog devices) first */ 4905 error = metaslab_alloc_range(spa, spa_log_class(spa), min_size, 4906 max_size, new_bp, 1, txg, NULL, flags, &io_alloc_list, allocator, 4907 NULL, &alloc_size); 4908 *slog = (error == 0); 4909 4910 /* Try special_embedded_log class (reserved on special vdevs) */ 4911 if (error != 0) { 4912 error = metaslab_alloc_range(spa, 4913 spa_special_embedded_log_class(spa), min_size, max_size, 4914 new_bp, 1, txg, NULL, flags, &io_alloc_list, allocator, 4915 NULL, &alloc_size); 4916 } 4917 4918 /* Try special class (general special vdev allocation) */ 4919 if (error != 0) { 4920 error = metaslab_alloc_range(spa, spa_special_class(spa), 4921 min_size, max_size, new_bp, 1, txg, NULL, flags, 4922 &io_alloc_list, allocator, NULL, &alloc_size); 4923 } 4924 4925 /* Try embedded_log class (reserved on normal vdevs) */ 4926 if (error != 0) { 4927 error = metaslab_alloc_range(spa, spa_embedded_log_class(spa), 4928 min_size, max_size, new_bp, 1, txg, NULL, flags, 4929 &io_alloc_list, allocator, NULL, &alloc_size); 4930 } 4931 4932 /* Finally fall back to normal class */ 4933 if (error != 0) { 4934 ZIOSTAT_BUMP(ziostat_alloc_class_fallbacks); 4935 error = metaslab_alloc_range(spa, spa_normal_class(spa), 4936 min_size, max_size, new_bp, 1, txg, NULL, flags, 4937 &io_alloc_list, allocator, NULL, &alloc_size); 4938 } 4939 metaslab_trace_fini(&io_alloc_list); 4940 4941 if (error == 0) { 4942 if (!allow_larger) 4943 alloc_size = MIN(alloc_size, max_size); 4944 else if (max_size <= SPA_OLD_MAXBLOCKSIZE) 4945 alloc_size = MIN(alloc_size, SPA_OLD_MAXBLOCKSIZE); 4946 alloc_size = P2ALIGN_TYPED(alloc_size, ZIL_MIN_BLKSZ, uint64_t); 4947 4948 BP_SET_LSIZE(new_bp, alloc_size); 4949 BP_SET_PSIZE(new_bp, alloc_size); 4950 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF); 4951 BP_SET_CHECKSUM(new_bp, 4952 spa_version(spa) >= SPA_VERSION_SLIM_ZIL 4953 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG); 4954 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG); 4955 BP_SET_LEVEL(new_bp, 0); 4956 BP_SET_DEDUP(new_bp, 0); 4957 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER); 4958 4959 /* 4960 * encrypted blocks will require an IV and salt. We generate 4961 * these now since we will not be rewriting the bp at 4962 * rewrite time. 4963 */ 4964 if (os->os_encrypted) { 4965 uint8_t iv[ZIO_DATA_IV_LEN]; 4966 uint8_t salt[ZIO_DATA_SALT_LEN]; 4967 4968 BP_SET_CRYPT(new_bp, B_TRUE); 4969 VERIFY0(spa_crypt_get_salt(spa, 4970 dmu_objset_id(os), salt)); 4971 VERIFY0(zio_crypt_generate_iv(iv)); 4972 4973 zio_crypt_encode_params_bp(new_bp, salt, iv); 4974 } 4975 } else { 4976 zfs_dbgmsg("%s: zil block allocation failure: " 4977 "min_size %llu, max_size %llu, error %d", spa_name(spa), 4978 (u_longlong_t)min_size, (u_longlong_t)max_size, error); 4979 } 4980 4981 return (error); 4982 } 4983 4984 /* 4985 * ========================================================================== 4986 * Read and write to physical devices 4987 * ========================================================================== 4988 */ 4989 4990 /* 4991 * Issue an I/O to the underlying vdev. Typically the issue pipeline 4992 * stops after this stage and will resume upon I/O completion. 4993 * However, there are instances where the vdev layer may need to 4994 * continue the pipeline when an I/O was not issued. Since the I/O 4995 * that was sent to the vdev layer might be different than the one 4996 * currently active in the pipeline (see vdev_queue_io()), we explicitly 4997 * force the underlying vdev layers to call either zio_execute() or 4998 * zio_interrupt() to ensure that the pipeline continues with the correct I/O. 4999 */ 5000 static zio_t * 5001 zio_vdev_io_start(zio_t *zio) 5002 { 5003 vdev_t *vd = zio->io_vd; 5004 uint64_t align; 5005 spa_t *spa = zio->io_spa; 5006 5007 zio->io_delta = 0; 5008 zio->io_delay = 0; 5009 5010 ASSERT0(zio->io_error); 5011 ASSERT0(zio->io_child_error[ZIO_CHILD_VDEV]); 5012 5013 if (vd == NULL) { 5014 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER)) { 5015 /* 5016 * A deadlock workaround. The ddt_prune_unique_entries() 5017 * -> prune_candidates_sync() code path takes the 5018 * SCL_ZIO reader lock and may request it again here. 5019 * If there is another thread who wants the SCL_ZIO 5020 * writer lock, then scl_write_wanted will be set. 5021 * Thus, the spa_config_enter_priority() is used to 5022 * ignore pending writer requests. 5023 * 5024 * The locking should be revised to remove the need 5025 * for this workaround. If that's not workable then 5026 * it should only be applied to the zios involved in 5027 * the pruning process. This impacts the read/write 5028 * I/O balance while pruning. 5029 */ 5030 if (spa->spa_active_ddt_prune) 5031 spa_config_enter_priority(spa, SCL_ZIO, zio, 5032 RW_READER); 5033 else 5034 spa_config_enter(spa, SCL_ZIO, zio, 5035 RW_READER); 5036 } 5037 5038 /* 5039 * The mirror_ops handle multiple DVAs in a single BP. 5040 */ 5041 vdev_mirror_ops.vdev_op_io_start(zio); 5042 return (NULL); 5043 } 5044 5045 ASSERT3P(zio->io_logical, !=, zio); 5046 if (zio->io_type == ZIO_TYPE_WRITE) { 5047 ASSERT(spa->spa_trust_config); 5048 5049 /* 5050 * Note: the code can handle other kinds of writes, 5051 * but we don't expect them. 5052 */ 5053 if (zio->io_vd->vdev_noalloc) { 5054 ASSERT(zio->io_flags & 5055 (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL | 5056 ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE)); 5057 } 5058 } 5059 5060 align = 1ULL << vd->vdev_top->vdev_ashift; 5061 5062 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) && 5063 P2PHASE(zio->io_size, align) != 0) { 5064 /* Transform logical writes to be a full physical block size. */ 5065 uint64_t asize = P2ROUNDUP(zio->io_size, align); 5066 abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize); 5067 ASSERT(vd == vd->vdev_top); 5068 if (zio->io_type == ZIO_TYPE_WRITE) { 5069 abd_copy(abuf, zio->io_abd, zio->io_size); 5070 abd_zero_off(abuf, zio->io_size, asize - zio->io_size); 5071 } 5072 zio_push_transform(zio, abuf, asize, asize, zio_subblock); 5073 } 5074 5075 /* 5076 * If this is not a physical io, make sure that it is properly aligned 5077 * before proceeding. 5078 */ 5079 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) { 5080 ASSERT0(P2PHASE(zio->io_offset, align)); 5081 ASSERT0(P2PHASE(zio->io_size, align)); 5082 } else { 5083 /* 5084 * For physical writes, we allow 512b aligned writes and assume 5085 * the device will perform a read-modify-write as necessary. 5086 */ 5087 ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE)); 5088 ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE)); 5089 } 5090 5091 VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa)); 5092 5093 /* 5094 * If this is a repair I/O, and there's no self-healing involved -- 5095 * that is, we're just resilvering what we expect to resilver -- 5096 * then don't do the I/O unless zio's txg is actually in vd's DTL. 5097 * This prevents spurious resilvering. 5098 * 5099 * There are a few ways that we can end up creating these spurious 5100 * resilver i/os: 5101 * 5102 * 1. A resilver i/o will be issued if any DVA in the BP has a 5103 * dirty DTL. The mirror code will issue resilver writes to 5104 * each DVA, including the one(s) that are not on vdevs with dirty 5105 * DTLs. 5106 * 5107 * 2. With nested replication, which happens when we have a 5108 * "replacing" or "spare" vdev that's a child of a mirror or raidz. 5109 * For example, given mirror(replacing(A+B), C), it's likely that 5110 * only A is out of date (it's the new device). In this case, we'll 5111 * read from C, then use the data to resilver A+B -- but we don't 5112 * actually want to resilver B, just A. The top-level mirror has no 5113 * way to know this, so instead we just discard unnecessary repairs 5114 * as we work our way down the vdev tree. 5115 * 5116 * 3. ZTEST also creates mirrors of mirrors, mirrors of raidz, etc. 5117 * The same logic applies to any form of nested replication: ditto 5118 * + mirror, RAID-Z + replacing, etc. 5119 * 5120 * However, indirect vdevs point off to other vdevs which may have 5121 * DTL's, so we never bypass them. The child i/os on concrete vdevs 5122 * will be properly bypassed instead. 5123 * 5124 * Leaf DTL_PARTIAL can be empty when a legitimate write comes from 5125 * a dRAID spare vdev. For example, when a dRAID spare is first 5126 * used, its spare blocks need to be written to but the leaf vdev's 5127 * of such blocks can have empty DTL_PARTIAL. 5128 * 5129 * There seemed no clean way to allow such writes while bypassing 5130 * spurious ones. At this point, just avoid all bypassing for dRAID 5131 * for correctness. 5132 */ 5133 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) && 5134 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) && 5135 zio->io_txg != 0 && /* not a delegated i/o */ 5136 vd->vdev_ops != &vdev_indirect_ops && 5137 vd->vdev_top->vdev_ops != &vdev_draid_ops && 5138 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) { 5139 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 5140 zio_vdev_io_bypass(zio); 5141 return (zio); 5142 } 5143 5144 /* 5145 * Select the next best leaf I/O to process. Distributed spares are 5146 * excluded since they dispatch the I/O directly to a leaf vdev after 5147 * applying the dRAID mapping. 5148 */ 5149 if (vd->vdev_ops->vdev_op_leaf && 5150 vd->vdev_ops != &vdev_draid_spare_ops && 5151 (zio->io_type == ZIO_TYPE_READ || 5152 zio->io_type == ZIO_TYPE_WRITE || 5153 zio->io_type == ZIO_TYPE_TRIM)) { 5154 5155 if ((zio = vdev_queue_io(zio)) == NULL) 5156 return (NULL); 5157 5158 if (!vdev_accessible(vd, zio)) { 5159 zio->io_error = SET_ERROR(ENXIO); 5160 zio_interrupt(zio); 5161 return (NULL); 5162 } 5163 zio->io_delay = gethrtime(); 5164 5165 int error = zio_handle_device_injections(vd, zio, ENOSYS, 5166 EFAULT); 5167 if (error == ENOSYS || (error == EFAULT && 5168 !(zio->io_flags & ZIO_FLAG_IO_REPAIR))) { 5169 /* 5170 * "no-op" injections return success, but do no actual 5171 * work. Just return it. "io-prefail" injections are 5172 * similar, but don't return success. 5173 */ 5174 if (error == EFAULT) 5175 zio->io_error = EIO; 5176 zio_delay_interrupt(zio); 5177 return (NULL); 5178 } 5179 } 5180 5181 vd->vdev_ops->vdev_op_io_start(zio); 5182 return (NULL); 5183 } 5184 5185 static zio_t * 5186 zio_vdev_io_done(zio_t *zio) 5187 { 5188 vdev_t *vd = zio->io_vd; 5189 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops; 5190 boolean_t unexpected_error = B_FALSE; 5191 5192 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) { 5193 return (NULL); 5194 } 5195 5196 ASSERT(zio->io_type == ZIO_TYPE_READ || 5197 zio->io_type == ZIO_TYPE_WRITE || 5198 zio->io_type == ZIO_TYPE_FLUSH || 5199 zio->io_type == ZIO_TYPE_TRIM); 5200 5201 if (zio->io_delay) { 5202 /* io_delta is set only if the completion was deferred. */ 5203 zio->io_delay = (zio->io_delta != 0 ? 5204 zio->io_timestamp + zio->io_delta : gethrtime()) - 5205 zio->io_delay; 5206 } 5207 5208 if (vd != NULL && vd->vdev_ops->vdev_op_leaf && 5209 vd->vdev_ops != &vdev_draid_spare_ops) { 5210 if (zio->io_type != ZIO_TYPE_FLUSH) 5211 vdev_queue_io_done(zio); 5212 5213 if (zio_injection_enabled && zio->io_error == 0) 5214 zio->io_error = zio_handle_device_injections(vd, zio, 5215 EIO, EILSEQ); 5216 5217 if (zio_injection_enabled && zio->io_error == 0) 5218 zio->io_error = zio_handle_label_injection(zio, EIO); 5219 5220 if (zio->io_error && zio->io_type != ZIO_TYPE_FLUSH && 5221 zio->io_type != ZIO_TYPE_TRIM) { 5222 if (!vdev_accessible(vd, zio)) { 5223 zio->io_error = SET_ERROR(ENXIO); 5224 } else { 5225 unexpected_error = B_TRUE; 5226 } 5227 } 5228 } 5229 5230 /* 5231 * This zio got here on a pipeline thread rather than from the block 5232 * layer, so it runs its own completion and gives up its membership. 5233 * The batch is chained only below, to keep it clear of whatever 5234 * vdev_op_io_done() may do with this zio. 5235 */ 5236 zio_t *batch = zio_batch_leave(zio); 5237 5238 ops->vdev_op_io_done(zio); 5239 5240 if (unexpected_error && vd->vdev_remove_wanted == B_FALSE) 5241 VERIFY0P(vdev_probe(vd, zio)); 5242 5243 zio->io_exec_next = batch; 5244 return (zio); 5245 } 5246 5247 /* 5248 * This function is used to change the priority of an existing zio that is 5249 * currently in-flight. This is used by the arc to upgrade priority in the 5250 * event that a demand read is made for a block that is currently queued 5251 * as a scrub or async read IO. Otherwise, the high priority read request 5252 * would end up having to wait for the lower priority IO. 5253 */ 5254 void 5255 zio_change_priority(zio_t *pio, zio_priority_t priority) 5256 { 5257 zio_t *cio, *cio_next; 5258 zio_link_t *zl = NULL; 5259 5260 ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 5261 5262 if (pio->io_vd != NULL && pio->io_vd->vdev_ops->vdev_op_leaf) { 5263 vdev_queue_change_io_priority(pio, priority); 5264 } else { 5265 pio->io_priority = priority; 5266 } 5267 5268 mutex_enter(&pio->io_lock); 5269 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) { 5270 cio_next = zio_walk_children(pio, &zl); 5271 zio_change_priority(cio, priority); 5272 } 5273 mutex_exit(&pio->io_lock); 5274 } 5275 5276 /* 5277 * For non-raidz ZIOs, we can just copy aside the bad data read from the 5278 * disk, and use that to finish the checksum ereport later. 5279 */ 5280 static void 5281 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr, 5282 const abd_t *good_buf) 5283 { 5284 /* no processing needed */ 5285 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE); 5286 } 5287 5288 void 5289 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr) 5290 { 5291 void *abd = abd_alloc_sametype(zio->io_abd, zio->io_size); 5292 5293 abd_copy(abd, zio->io_abd, zio->io_size); 5294 5295 zcr->zcr_cbinfo = zio->io_size; 5296 zcr->zcr_cbdata = abd; 5297 zcr->zcr_finish = zio_vsd_default_cksum_finish; 5298 zcr->zcr_free = zio_abd_free; 5299 } 5300 5301 static zio_t * 5302 zio_vdev_io_assess(zio_t *zio) 5303 { 5304 vdev_t *vd = zio->io_vd; 5305 5306 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) { 5307 return (NULL); 5308 } 5309 5310 /* A repair write bypass skips VDEV_IO_DONE entirely. */ 5311 zio->io_exec_next = zio_batch_leave(zio); 5312 5313 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER)) 5314 spa_config_exit(zio->io_spa, SCL_ZIO, zio); 5315 5316 if (zio->io_vsd != NULL) { 5317 zio->io_vsd_ops->vsd_free(zio); 5318 zio->io_vsd = NULL; 5319 } 5320 5321 /* 5322 * If a Direct I/O operation has a checksum verify error then this I/O 5323 * should not attempt to be issued again. 5324 */ 5325 if (zio->io_post & ZIO_POST_DIO_CHKSUM_ERR) { 5326 if (zio->io_type == ZIO_TYPE_WRITE) { 5327 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_LOGICAL); 5328 ASSERT3U(zio->io_error, ==, EIO); 5329 } 5330 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 5331 return (zio); 5332 } 5333 5334 if (zio_injection_enabled && zio->io_error == 0) 5335 zio->io_error = zio_handle_fault_injection(zio, EIO); 5336 5337 /* 5338 * If the I/O failed, determine whether we should attempt to retry it. 5339 * 5340 * On retry, we cut in line in the issue queue, since we don't want 5341 * compression/checksumming/etc. work to prevent our (cheap) IO reissue. 5342 */ 5343 if (zio->io_error && vd == NULL && 5344 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) { 5345 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */ 5346 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */ 5347 zio->io_error = 0; 5348 zio->io_flags |= ZIO_FLAG_IO_RETRY | ZIO_FLAG_DONT_AGGREGATE; 5349 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1; 5350 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, 5351 zio_requeue_io_start_cut_in_line); 5352 return (NULL); 5353 } 5354 5355 /* 5356 * If we got an error on a leaf device, convert it to ENXIO 5357 * if the device is not accessible at all. 5358 */ 5359 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf && 5360 !vdev_accessible(vd, zio)) 5361 zio->io_error = SET_ERROR(ENXIO); 5362 5363 /* 5364 * If we can't write to an interior vdev (mirror or RAID-Z), 5365 * set vdev_cant_write so that we stop trying to allocate from it. 5366 */ 5367 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE && 5368 vd != NULL && !vd->vdev_ops->vdev_op_leaf) { 5369 vdev_dbgmsg(vd, "zio_vdev_io_assess(zio=%px) setting " 5370 "cant_write=TRUE due to write failure with ENXIO", 5371 zio); 5372 vd->vdev_cant_write = B_TRUE; 5373 } 5374 5375 /* 5376 * If a cache flush returns ENOTSUP we know that no future 5377 * attempts will ever succeed. In this case we set a persistent 5378 * boolean flag so that we don't bother with it in the future, and 5379 * then we act like the flush succeeded. 5380 */ 5381 if (zio->io_error == ENOTSUP && zio->io_type == ZIO_TYPE_FLUSH && 5382 vd != NULL) { 5383 vd->vdev_nowritecache = B_TRUE; 5384 zio->io_error = 0; 5385 } 5386 5387 if (zio->io_error) 5388 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 5389 5390 return (zio); 5391 } 5392 5393 void 5394 zio_vdev_io_reissue(zio_t *zio) 5395 { 5396 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 5397 ASSERT0(zio->io_error); 5398 5399 zio->io_stage >>= 1; 5400 } 5401 5402 void 5403 zio_vdev_io_redone(zio_t *zio) 5404 { 5405 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE); 5406 5407 zio->io_stage >>= 1; 5408 } 5409 5410 void 5411 zio_vdev_io_bypass(zio_t *zio) 5412 { 5413 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 5414 ASSERT0(zio->io_error); 5415 5416 zio->io_flags |= ZIO_FLAG_IO_BYPASS; 5417 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1; 5418 } 5419 5420 /* 5421 * ========================================================================== 5422 * Encrypt and store encryption parameters 5423 * ========================================================================== 5424 */ 5425 5426 5427 /* 5428 * This function is used for ZIO_STAGE_ENCRYPT. It is responsible for 5429 * managing the storage of encryption parameters and passing them to the 5430 * lower-level encryption functions. 5431 */ 5432 static zio_t * 5433 zio_encrypt(zio_t *zio) 5434 { 5435 zio_prop_t *zp = &zio->io_prop; 5436 spa_t *spa = zio->io_spa; 5437 blkptr_t *bp = zio->io_bp; 5438 uint64_t psize = BP_GET_PSIZE(bp); 5439 uint64_t dsobj = zio->io_bookmark.zb_objset; 5440 dmu_object_type_t ot = BP_GET_TYPE(bp); 5441 void *enc_buf = NULL; 5442 abd_t *eabd = NULL; 5443 uint8_t salt[ZIO_DATA_SALT_LEN]; 5444 uint8_t iv[ZIO_DATA_IV_LEN]; 5445 uint8_t mac[ZIO_DATA_MAC_LEN]; 5446 boolean_t no_crypt = B_FALSE; 5447 5448 /* the root zio already encrypted the data */ 5449 if (zio->io_child_type == ZIO_CHILD_GANG) 5450 return (zio); 5451 5452 /* only ZIL blocks are re-encrypted on rewrite */ 5453 if (!IO_IS_ALLOCATING(zio) && ot != DMU_OT_INTENT_LOG) 5454 return (zio); 5455 5456 if (!(zp->zp_encrypt || BP_IS_ENCRYPTED(bp))) { 5457 BP_SET_CRYPT(bp, B_FALSE); 5458 return (zio); 5459 } 5460 5461 /* if we are doing raw encryption set the provided encryption params */ 5462 if (zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) { 5463 ASSERT0(BP_GET_LEVEL(bp)); 5464 BP_SET_CRYPT(bp, B_TRUE); 5465 BP_SET_BYTEORDER(bp, zp->zp_byteorder); 5466 if (ot != DMU_OT_OBJSET) 5467 zio_crypt_encode_mac_bp(bp, zp->zp_mac); 5468 5469 /* dnode blocks must be written out in the provided byteorder */ 5470 if (zp->zp_byteorder != ZFS_HOST_BYTEORDER && 5471 ot == DMU_OT_DNODE) { 5472 void *bswap_buf = zio_buf_alloc(psize); 5473 abd_t *babd = abd_get_from_buf(bswap_buf, psize); 5474 5475 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF); 5476 abd_copy_to_buf(bswap_buf, zio->io_abd, psize); 5477 dmu_ot_byteswap[DMU_OT_BYTESWAP(ot)].ob_func(bswap_buf, 5478 psize); 5479 5480 abd_take_ownership_of_buf(babd, B_TRUE); 5481 zio_push_transform(zio, babd, psize, psize, NULL); 5482 } 5483 5484 if (DMU_OT_IS_ENCRYPTED(ot)) 5485 zio_crypt_encode_params_bp(bp, zp->zp_salt, zp->zp_iv); 5486 return (zio); 5487 } 5488 5489 /* indirect blocks only maintain a cksum of the lower level MACs */ 5490 if (BP_GET_LEVEL(bp) > 0) { 5491 BP_SET_CRYPT(bp, B_TRUE); 5492 VERIFY0(zio_crypt_do_indirect_mac_checksum_abd(B_TRUE, 5493 zio->io_orig_abd, BP_GET_LSIZE(bp), BP_SHOULD_BYTESWAP(bp), 5494 mac)); 5495 zio_crypt_encode_mac_bp(bp, mac); 5496 return (zio); 5497 } 5498 5499 /* 5500 * Objset blocks are a special case since they have 2 256-bit MACs 5501 * embedded within them. 5502 */ 5503 if (ot == DMU_OT_OBJSET) { 5504 ASSERT0(DMU_OT_IS_ENCRYPTED(ot)); 5505 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF); 5506 BP_SET_CRYPT(bp, B_TRUE); 5507 VERIFY0(spa_do_crypt_objset_mac_abd(B_TRUE, spa, dsobj, 5508 zio->io_abd, psize, BP_SHOULD_BYTESWAP(bp))); 5509 return (zio); 5510 } 5511 5512 /* unencrypted object types are only authenticated with a MAC */ 5513 if (!DMU_OT_IS_ENCRYPTED(ot)) { 5514 BP_SET_CRYPT(bp, B_TRUE); 5515 VERIFY0(spa_do_crypt_mac_abd(B_TRUE, spa, dsobj, 5516 zio->io_abd, psize, mac)); 5517 zio_crypt_encode_mac_bp(bp, mac); 5518 return (zio); 5519 } 5520 5521 /* 5522 * Later passes of sync-to-convergence may decide to rewrite data 5523 * in place to avoid more disk reallocations. This presents a problem 5524 * for encryption because this constitutes rewriting the new data with 5525 * the same encryption key and IV. However, this only applies to blocks 5526 * in the MOS (particularly the spacemaps) and we do not encrypt the 5527 * MOS. We assert that the zio is allocating or an intent log write 5528 * to enforce this. 5529 */ 5530 ASSERT(IO_IS_ALLOCATING(zio) || ot == DMU_OT_INTENT_LOG); 5531 ASSERT(BP_GET_LEVEL(bp) == 0 || ot == DMU_OT_INTENT_LOG); 5532 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION)); 5533 ASSERT3U(psize, !=, 0); 5534 5535 enc_buf = zio_buf_alloc(psize); 5536 eabd = abd_get_from_buf(enc_buf, psize); 5537 abd_take_ownership_of_buf(eabd, B_TRUE); 5538 5539 /* 5540 * For an explanation of what encryption parameters are stored 5541 * where, see the block comment in zio_crypt.c. 5542 */ 5543 if (ot == DMU_OT_INTENT_LOG) { 5544 zio_crypt_decode_params_bp(bp, salt, iv); 5545 } else { 5546 BP_SET_CRYPT(bp, B_TRUE); 5547 } 5548 5549 /* Perform the encryption. This should not fail */ 5550 VERIFY0(spa_do_crypt_abd(B_TRUE, spa, &zio->io_bookmark, 5551 BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), 5552 salt, iv, mac, psize, zio->io_abd, eabd, &no_crypt)); 5553 5554 /* encode encryption metadata into the bp */ 5555 if (ot == DMU_OT_INTENT_LOG) { 5556 /* 5557 * ZIL blocks store the MAC in the embedded checksum, so the 5558 * transform must always be applied. 5559 */ 5560 zio_crypt_encode_mac_zil(enc_buf, mac); 5561 zio_push_transform(zio, eabd, psize, psize, NULL); 5562 } else { 5563 BP_SET_CRYPT(bp, B_TRUE); 5564 zio_crypt_encode_params_bp(bp, salt, iv); 5565 zio_crypt_encode_mac_bp(bp, mac); 5566 5567 if (no_crypt) { 5568 ASSERT3U(ot, ==, DMU_OT_DNODE); 5569 abd_free(eabd); 5570 } else { 5571 zio_push_transform(zio, eabd, psize, psize, NULL); 5572 } 5573 } 5574 5575 return (zio); 5576 } 5577 5578 /* 5579 * ========================================================================== 5580 * Generate and verify checksums 5581 * ========================================================================== 5582 */ 5583 static zio_t * 5584 zio_checksum_generate(zio_t *zio) 5585 { 5586 blkptr_t *bp = zio->io_bp; 5587 enum zio_checksum checksum; 5588 5589 if (bp == NULL) { 5590 /* 5591 * This is zio_write_phys(). 5592 * We're either generating a label checksum, or none at all. 5593 */ 5594 checksum = zio->io_prop.zp_checksum; 5595 5596 if (checksum == ZIO_CHECKSUM_OFF) 5597 return (zio); 5598 5599 ASSERT(checksum == ZIO_CHECKSUM_LABEL); 5600 } else { 5601 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) { 5602 ASSERT(!IO_IS_ALLOCATING(zio)); 5603 checksum = ZIO_CHECKSUM_GANG_HEADER; 5604 } else { 5605 checksum = BP_GET_CHECKSUM(bp); 5606 } 5607 } 5608 5609 zio_checksum_compute(zio, checksum, zio->io_abd, zio->io_size); 5610 5611 return (zio); 5612 } 5613 5614 static zio_t * 5615 zio_checksum_verify(zio_t *zio) 5616 { 5617 zio_bad_cksum_t info; 5618 blkptr_t *bp = zio->io_bp; 5619 int error; 5620 5621 ASSERT(zio->io_vd != NULL); 5622 5623 if (bp == NULL) { 5624 /* 5625 * This is zio_read_phys(). 5626 * We're either verifying a label checksum, or nothing at all. 5627 */ 5628 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF) 5629 return (zio); 5630 5631 ASSERT3U(zio->io_prop.zp_checksum, ==, ZIO_CHECKSUM_LABEL); 5632 } 5633 5634 ASSERT0(zio->io_post & ZIO_POST_DIO_CHKSUM_ERR); 5635 IMPLY(zio->io_flags & ZIO_FLAG_DIO_READ, 5636 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)); 5637 5638 if ((error = zio_checksum_error(zio, &info)) != 0) { 5639 zio->io_error = error; 5640 if (error == ECKSUM && 5641 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 5642 if (zio->io_flags & ZIO_FLAG_DIO_READ) { 5643 zio->io_post |= ZIO_POST_DIO_CHKSUM_ERR; 5644 zio_t *pio = zio_unique_parent(zio); 5645 /* 5646 * Any Direct I/O read that has a checksum 5647 * error must be treated as suspicous as the 5648 * contents of the buffer could be getting 5649 * manipulated while the I/O is taking place. 5650 * 5651 * The checksum verify error will only be 5652 * reported here for disk and file VDEV's and 5653 * will be reported on those that the failure 5654 * occurred on. Other types of VDEV's report the 5655 * verify failure in their own code paths. 5656 */ 5657 if (pio->io_child_type == ZIO_CHILD_LOGICAL) { 5658 zio_dio_chksum_verify_error_report(zio); 5659 } 5660 } else { 5661 mutex_enter(&zio->io_vd->vdev_stat_lock); 5662 zio->io_vd->vdev_stat.vs_checksum_errors++; 5663 mutex_exit(&zio->io_vd->vdev_stat_lock); 5664 (void) zfs_ereport_start_checksum(zio->io_spa, 5665 zio->io_vd, &zio->io_bookmark, zio, 5666 zio->io_offset, zio->io_size, &info); 5667 } 5668 } 5669 } 5670 5671 return (zio); 5672 } 5673 5674 static zio_t * 5675 zio_dio_checksum_verify(zio_t *zio) 5676 { 5677 zio_t *pio = zio_unique_parent(zio); 5678 int error; 5679 5680 ASSERT3P(zio->io_vd, !=, NULL); 5681 ASSERT3P(zio->io_bp, !=, NULL); 5682 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV); 5683 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE); 5684 ASSERT3B(pio->io_prop.zp_direct_write, ==, B_TRUE); 5685 ASSERT3U(pio->io_child_type, ==, ZIO_CHILD_LOGICAL); 5686 5687 if (zfs_vdev_direct_write_verify == 0 || zio->io_error != 0) 5688 goto out; 5689 5690 if ((error = zio_checksum_error(zio, NULL)) != 0) { 5691 zio->io_error = error; 5692 if (error == ECKSUM) { 5693 zio->io_post |= ZIO_POST_DIO_CHKSUM_ERR; 5694 zio_dio_chksum_verify_error_report(zio); 5695 } 5696 } 5697 5698 out: 5699 return (zio); 5700 } 5701 5702 5703 /* 5704 * Called by RAID-Z to ensure we don't compute the checksum twice. 5705 */ 5706 void 5707 zio_checksum_verified(zio_t *zio) 5708 { 5709 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY; 5710 } 5711 5712 /* 5713 * Report Direct I/O checksum verify error and create ZED event. 5714 */ 5715 void 5716 zio_dio_chksum_verify_error_report(zio_t *zio) 5717 { 5718 ASSERT(zio->io_post & ZIO_POST_DIO_CHKSUM_ERR); 5719 5720 if (zio->io_child_type == ZIO_CHILD_LOGICAL) 5721 return; 5722 5723 mutex_enter(&zio->io_vd->vdev_stat_lock); 5724 zio->io_vd->vdev_stat.vs_dio_verify_errors++; 5725 mutex_exit(&zio->io_vd->vdev_stat_lock); 5726 if (zio->io_type == ZIO_TYPE_WRITE) { 5727 /* 5728 * Convert checksum error for writes into EIO. 5729 */ 5730 zio->io_error = SET_ERROR(EIO); 5731 /* 5732 * Report dio_verify_wr ZED event, rate limited. 5733 */ 5734 if (zfs_ratelimit(&zio->io_vd->vdev_dio_verify_rl)) 5735 (void) zfs_ereport_post(FM_EREPORT_ZFS_DIO_VERIFY_WR, 5736 zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0); 5737 } else { 5738 /* 5739 * Report dio_verify_rd ZED event, rate limited. 5740 */ 5741 if (zfs_ratelimit(&zio->io_vd->vdev_dio_verify_rl)) 5742 (void) zfs_ereport_post(FM_EREPORT_ZFS_DIO_VERIFY_RD, 5743 zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0); 5744 } 5745 } 5746 5747 /* 5748 * ========================================================================== 5749 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other. 5750 * An error of 0 indicates success. ENXIO indicates whole-device failure, 5751 * which may be transient (e.g. unplugged) or permanent. ECKSUM and EIO 5752 * indicate errors that are specific to one I/O, and most likely permanent. 5753 * Any other error is presumed to be worse because we weren't expecting it. 5754 * ========================================================================== 5755 */ 5756 int 5757 zio_worst_error(int e1, int e2) 5758 { 5759 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO }; 5760 int r1, r2; 5761 5762 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++) 5763 if (e1 == zio_error_rank[r1]) 5764 break; 5765 5766 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++) 5767 if (e2 == zio_error_rank[r2]) 5768 break; 5769 5770 return (r1 > r2 ? e1 : e2); 5771 } 5772 5773 /* 5774 * ========================================================================== 5775 * I/O completion 5776 * ========================================================================== 5777 */ 5778 static zio_t * 5779 zio_ready(zio_t *zio) 5780 { 5781 blkptr_t *bp = zio->io_bp; 5782 zio_t *pio, *pio_next; 5783 zio_link_t *zl = NULL; 5784 5785 if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT | 5786 ZIO_CHILD_GANG_BIT | ZIO_CHILD_DDT_BIT, ZIO_WAIT_READY)) { 5787 return (NULL); 5788 } 5789 5790 if (zio_injection_enabled) { 5791 hrtime_t target = zio_handle_ready_delay(zio); 5792 if (target != 0 && zio->io_target_timestamp == 0) { 5793 zio->io_stage >>= 1; 5794 zio->io_target_timestamp = target; 5795 zio_delay_interrupt(zio); 5796 return (NULL); 5797 } 5798 } 5799 5800 if (zio->io_ready) { 5801 ASSERT(IO_IS_ALLOCATING(zio)); 5802 ASSERT(BP_GET_BIRTH(bp) == zio->io_txg || 5803 BP_IS_HOLE(bp) || (zio->io_flags & ZIO_FLAG_NOPWRITE)); 5804 ASSERT0(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY]); 5805 5806 zio->io_ready(zio); 5807 } 5808 5809 #ifdef ZFS_DEBUG 5810 if (bp != NULL && bp != &zio->io_bp_copy) 5811 zio->io_bp_copy = *bp; 5812 #endif 5813 5814 if (zio->io_error != 0) { 5815 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 5816 5817 if (zio->io_flags & ZIO_FLAG_ALLOC_THROTTLED) { 5818 ASSERT(IO_IS_ALLOCATING(zio)); 5819 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE); 5820 ASSERT(zio->io_metaslab_class != NULL); 5821 ASSERT(ZIO_HAS_ALLOCATOR(zio)); 5822 5823 /* 5824 * We were unable to allocate anything, unreserve and 5825 * issue the next I/O to allocate. 5826 */ 5827 if (metaslab_class_throttle_unreserve( 5828 zio->io_metaslab_class, zio->io_allocator, 5829 zio->io_prop.zp_copies, zio->io_size)) { 5830 zio_allocate_dispatch(zio->io_metaslab_class, 5831 zio->io_allocator); 5832 } 5833 } 5834 } 5835 5836 mutex_enter(&zio->io_lock); 5837 zio->io_state[ZIO_WAIT_READY] = 1; 5838 pio = zio_walk_parents(zio, &zl); 5839 mutex_exit(&zio->io_lock); 5840 5841 /* 5842 * As we notify zio's parents, new parents could be added. 5843 * New parents go to the head of zio's io_parent_list, however, 5844 * so we will (correctly) not notify them. The remainder of zio's 5845 * io_parent_list, from 'pio_next' onward, cannot change because 5846 * all parents must wait for us to be done before they can be done. 5847 */ 5848 zio_next_t next; 5849 zio_next_init(&next); 5850 for (; pio != NULL; pio = pio_next) { 5851 pio_next = zio_walk_parents(zio, &zl); 5852 zio_notify_parent(pio, zio, ZIO_WAIT_READY, &next); 5853 } 5854 ASSERT3P(zio->io_exec_next, ==, NULL); 5855 zio->io_exec_next = next.zn_list; 5856 5857 if (zio->io_flags & ZIO_FLAG_NODATA) { 5858 if (bp != NULL && BP_IS_GANG(bp)) { 5859 zio->io_flags &= ~ZIO_FLAG_NODATA; 5860 } else { 5861 ASSERT((uintptr_t)zio->io_abd < SPA_MAXBLOCKSIZE); 5862 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES; 5863 } 5864 } 5865 5866 if (zio_injection_enabled && 5867 zio->io_spa->spa_syncing_txg == zio->io_txg) 5868 zio_handle_ignored_writes(zio); 5869 5870 return (zio); 5871 } 5872 5873 /* 5874 * Update the allocation throttle accounting. 5875 */ 5876 static void 5877 zio_dva_throttle_done(zio_t *zio) 5878 { 5879 zio_t *pio = zio_unique_parent(zio); 5880 vdev_t *vd = zio->io_vd; 5881 int flags = METASLAB_ASYNC_ALLOC; 5882 const void *tag = pio; 5883 uint64_t size = pio->io_size; 5884 5885 ASSERT3P(zio->io_bp, !=, NULL); 5886 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE); 5887 ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE); 5888 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV); 5889 ASSERT(vd != NULL); 5890 ASSERT3P(vd, ==, vd->vdev_top); 5891 ASSERT(zio_injection_enabled || !(zio->io_flags & ZIO_FLAG_IO_RETRY)); 5892 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR)); 5893 ASSERT(zio->io_flags & ZIO_FLAG_ALLOC_THROTTLED); 5894 5895 /* 5896 * Parents of gang children can have two flavors -- ones that allocated 5897 * the gang header (will have ZIO_FLAG_IO_REWRITE set) and ones that 5898 * allocated the constituent blocks. The first use their parent as tag. 5899 * We set the size to match the original allocation call for that case. 5900 */ 5901 if (pio->io_child_type == ZIO_CHILD_GANG && 5902 (pio->io_flags & ZIO_FLAG_IO_REWRITE)) { 5903 tag = zio_unique_parent(pio); 5904 size = SPA_OLD_GANGBLOCKSIZE; 5905 } 5906 5907 ASSERT(IO_IS_ALLOCATING(pio) || (pio->io_child_type == ZIO_CHILD_GANG && 5908 (pio->io_flags & ZIO_FLAG_IO_REWRITE))); 5909 ASSERT(ZIO_HAS_ALLOCATOR(pio)); 5910 ASSERT3P(zio, !=, zio->io_logical); 5911 ASSERT(zio->io_logical != NULL); 5912 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR)); 5913 ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE); 5914 ASSERT(zio->io_metaslab_class != NULL); 5915 ASSERT(zio->io_metaslab_class->mc_alloc_throttle_enabled); 5916 5917 metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, 5918 pio->io_allocator, flags, size, tag); 5919 5920 if (metaslab_class_throttle_unreserve(pio->io_metaslab_class, 5921 pio->io_allocator, 1, pio->io_size)) { 5922 zio_allocate_dispatch(zio->io_metaslab_class, 5923 pio->io_allocator); 5924 } 5925 } 5926 5927 static void 5928 zio_done_postread_done(zio_t *zio) 5929 { 5930 abd_free(zio->io_abd); 5931 } 5932 5933 static zio_t * 5934 zio_done(zio_t *zio) 5935 { 5936 /* 5937 * Always attempt to keep stack usage minimal here since 5938 * we can be called recursively up to 19 levels deep. 5939 */ 5940 const uint64_t psize = zio->io_size; 5941 zio_t *pio, *pio_next; 5942 zio_link_t *zl = NULL; 5943 5944 /* 5945 * If our children haven't all completed, 5946 * wait for them and then repeat this pipeline stage. 5947 */ 5948 if (zio_wait_for_children(zio, ZIO_CHILD_ALL_BITS, ZIO_WAIT_DONE)) { 5949 return (NULL); 5950 } 5951 5952 /* 5953 * If the allocation throttle is enabled, then update the accounting. 5954 * We only track child I/Os that are part of an allocating async 5955 * write. We must do this since the allocation is performed 5956 * by the logical I/O but the actual write is done by child I/Os. 5957 */ 5958 if (zio->io_flags & ZIO_FLAG_ALLOC_THROTTLED && 5959 zio->io_child_type == ZIO_CHILD_VDEV) 5960 zio_dva_throttle_done(zio); 5961 5962 for (int c = 0; c < ZIO_CHILD_TYPES; c++) 5963 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 5964 ASSERT0(zio->io_children[c][w]); 5965 5966 if (zio->io_bp != NULL && !BP_IS_EMBEDDED(zio->io_bp)) { 5967 ASSERT(memcmp(zio->io_bp, &zio->io_bp_copy, 5968 sizeof (blkptr_t)) == 0 || 5969 (zio->io_bp == zio_unique_parent(zio)->io_bp)); 5970 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(zio->io_bp) && 5971 zio->io_bp_override == NULL && 5972 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) { 5973 ASSERT3U(zio->io_prop.zp_copies, <=, 5974 BP_GET_NDVAS(zio->io_bp)); 5975 ASSERT(BP_COUNT_GANG(zio->io_bp) == 0 || 5976 (BP_COUNT_GANG(zio->io_bp) == 5977 BP_GET_NDVAS(zio->io_bp))); 5978 } 5979 if (zio->io_flags & ZIO_FLAG_NOPWRITE) 5980 VERIFY(BP_EQUAL(zio->io_bp, &zio->io_bp_orig)); 5981 } 5982 5983 /* 5984 * If there were child vdev/gang/ddt errors, they apply to us now. 5985 */ 5986 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV); 5987 zio_inherit_child_errors(zio, ZIO_CHILD_GANG); 5988 zio_inherit_child_errors(zio, ZIO_CHILD_DDT); 5989 5990 /* 5991 * If the I/O on the transformed data was successful, generate any 5992 * checksum reports now while we still have the transformed data. 5993 */ 5994 if (zio->io_error == 0) { 5995 while (zio->io_cksum_report != NULL) { 5996 zio_cksum_report_t *zcr = zio->io_cksum_report; 5997 uint64_t align = zcr->zcr_align; 5998 uint64_t asize = P2ROUNDUP(psize, align); 5999 abd_t *adata = zio->io_abd; 6000 6001 if (adata != NULL && asize != psize) { 6002 adata = abd_alloc(asize, B_TRUE); 6003 abd_copy(adata, zio->io_abd, psize); 6004 abd_zero_off(adata, psize, asize - psize); 6005 } 6006 6007 zio->io_cksum_report = zcr->zcr_next; 6008 zcr->zcr_next = NULL; 6009 zcr->zcr_finish(zcr, adata); 6010 zfs_ereport_free_checksum(zcr); 6011 6012 if (adata != NULL && asize != psize) 6013 abd_free(adata); 6014 } 6015 } 6016 6017 zio_pop_transforms(zio); /* note: may set zio->io_error */ 6018 6019 /* 6020 * During thorough scrub, if the dataset key is not loaded, decryption 6021 * or MAC verification fails with EACCES (spa_do_crypt_abd() and the 6022 * MAC helpers). Since the block's checksum was already successfully 6023 * verified by zio_checksum_verify() before we got here, treat it as 6024 * success and move on; this is as much as we can do without the keys 6025 * loaded. 6026 */ 6027 if (zio->io_error == EACCES && (zio->io_flags & ZIO_FLAG_SCRUB) && 6028 !(zio->io_flags & ZIO_FLAG_RAW)) 6029 zio->io_error = 0; 6030 6031 vdev_stat_update(zio, psize); 6032 6033 /* 6034 * If this I/O is attached to a particular vdev is slow, exceeding 6035 * 30 seconds to complete, post an error described the I/O delay. 6036 * We ignore these errors if the device is currently unavailable. 6037 */ 6038 if (zio->io_delay >= MSEC2NSEC(zio_slow_io_ms)) { 6039 if (zio->io_vd != NULL && !vdev_is_dead(zio->io_vd)) { 6040 /* 6041 * We want to only increment our slow IO counters if 6042 * the IO is valid (i.e. not if the drive is removed). 6043 * 6044 * zfs_ereport_post() will also do these checks, but 6045 * it can also ratelimit and have other failures, so we 6046 * need to increment the slow_io counters independent 6047 * of it. 6048 */ 6049 if (zfs_ereport_is_valid(FM_EREPORT_ZFS_DELAY, 6050 zio->io_spa, zio->io_vd, zio)) { 6051 mutex_enter(&zio->io_vd->vdev_stat_lock); 6052 zio->io_vd->vdev_stat.vs_slow_ios++; 6053 mutex_exit(&zio->io_vd->vdev_stat_lock); 6054 6055 if (zio->io_vd->vdev_slow_io_events) { 6056 (void) zfs_ereport_post( 6057 FM_EREPORT_ZFS_DELAY, 6058 zio->io_spa, zio->io_vd, 6059 &zio->io_bookmark, zio, 0); 6060 } 6061 } 6062 } 6063 } 6064 6065 if (zio->io_error) { 6066 /* 6067 * If this I/O is attached to a particular vdev, 6068 * generate an error message describing the I/O failure 6069 * at the block level. We ignore these errors if the 6070 * device is currently unavailable. 6071 */ 6072 if (zio->io_error != ECKSUM && zio->io_vd != NULL && 6073 !vdev_is_dead(zio->io_vd) && 6074 !(zio->io_post & ZIO_POST_DIO_CHKSUM_ERR)) { 6075 int ret = zfs_ereport_post(FM_EREPORT_ZFS_IO, 6076 zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0); 6077 if (ret != EALREADY) { 6078 mutex_enter(&zio->io_vd->vdev_stat_lock); 6079 if (zio->io_type == ZIO_TYPE_READ) 6080 zio->io_vd->vdev_stat.vs_read_errors++; 6081 else if (zio->io_type == ZIO_TYPE_WRITE) 6082 zio->io_vd->vdev_stat.vs_write_errors++; 6083 mutex_exit(&zio->io_vd->vdev_stat_lock); 6084 } 6085 } 6086 6087 if ((zio->io_error == EIO || !(zio->io_flags & 6088 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) && 6089 !(zio->io_post & ZIO_POST_DIO_CHKSUM_ERR) && 6090 zio == zio->io_logical) { 6091 /* 6092 * For logical I/O requests, tell the SPA to log the 6093 * error and generate a logical data ereport. 6094 */ 6095 spa_log_error(zio->io_spa, &zio->io_bookmark, 6096 BP_GET_PHYSICAL_BIRTH(zio->io_bp)); 6097 (void) zfs_ereport_post(FM_EREPORT_ZFS_DATA, 6098 zio->io_spa, NULL, &zio->io_bookmark, zio, 0); 6099 } 6100 } 6101 6102 if (zio->io_error && zio == zio->io_logical) { 6103 6104 /* 6105 * A DDT child tried to create a mixed gang/non-gang BP. We're 6106 * going to have to just retry as a non-dedup IO. 6107 */ 6108 if (zio->io_error == EAGAIN && IO_IS_ALLOCATING(zio) && 6109 zio->io_prop.zp_dedup) { 6110 zio->io_post |= ZIO_POST_REEXECUTE; 6111 zio->io_prop.zp_dedup = B_FALSE; 6112 } 6113 /* 6114 * Determine whether zio should be reexecuted. This will 6115 * propagate all the way to the root via zio_notify_parent(). 6116 */ 6117 ASSERT(zio->io_vd == NULL && zio->io_bp != NULL); 6118 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 6119 6120 if (IO_IS_ALLOCATING(zio) && 6121 !(zio->io_flags & ZIO_FLAG_CANFAIL) && 6122 !(zio->io_post & ZIO_POST_DIO_CHKSUM_ERR)) { 6123 if (zio->io_error != ENOSPC) 6124 zio->io_post |= ZIO_POST_REEXECUTE; 6125 else 6126 zio->io_post |= ZIO_POST_SUSPEND; 6127 } 6128 6129 if ((zio->io_type == ZIO_TYPE_READ || 6130 zio->io_type == ZIO_TYPE_FREE) && 6131 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && 6132 zio->io_error == ENXIO && 6133 spa_load_state(zio->io_spa) == SPA_LOAD_NONE && 6134 spa_get_failmode(zio->io_spa) != ZIO_FAILURE_MODE_CONTINUE) 6135 zio->io_post |= ZIO_POST_SUSPEND; 6136 6137 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && 6138 !(zio->io_post & (ZIO_POST_REEXECUTE|ZIO_POST_SUSPEND))) 6139 zio->io_post |= ZIO_POST_SUSPEND; 6140 6141 /* 6142 * Here is a possibly good place to attempt to do 6143 * either combinatorial reconstruction or error correction 6144 * based on checksums. It also might be a good place 6145 * to send out preliminary ereports before we suspend 6146 * processing. 6147 */ 6148 } 6149 6150 /* 6151 * If there were logical child errors, they apply to us now. 6152 * We defer this until now to avoid conflating logical child 6153 * errors with errors that happened to the zio itself when 6154 * updating vdev stats and reporting FMA events above. 6155 */ 6156 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL); 6157 6158 if ((zio->io_error || 6159 (zio->io_post & (ZIO_POST_REEXECUTE|ZIO_POST_SUSPEND))) && 6160 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio && 6161 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE))) 6162 zio_dva_unallocate(zio, zio->io_gang_tree, zio->io_bp); 6163 6164 zio_gang_tree_free(&zio->io_gang_tree); 6165 6166 /* 6167 * Godfather I/Os should never suspend. 6168 */ 6169 if ((zio->io_flags & ZIO_FLAG_GODFATHER) && 6170 (zio->io_post & ZIO_POST_SUSPEND)) 6171 zio->io_post &= ~ZIO_POST_SUSPEND; 6172 6173 if (zio->io_post & (ZIO_POST_REEXECUTE|ZIO_POST_SUSPEND)) { 6174 /* 6175 * A Direct I/O operation that has a checksum verify error 6176 * should not attempt to reexecute. Instead, the error should 6177 * just be propagated back. 6178 */ 6179 ASSERT0(zio->io_post & ZIO_POST_DIO_CHKSUM_ERR); 6180 6181 /* 6182 * This is a logical I/O that wants to reexecute. 6183 * 6184 * Reexecute is top-down. When an i/o fails, if it's not 6185 * the root, it simply notifies its parent and sticks around. 6186 * The parent, seeing that it still has children in zio_done(), 6187 * does the same. This percolates all the way up to the root. 6188 * The root i/o will reexecute or suspend the entire tree. 6189 * 6190 * This approach ensures that zio_reexecute() honors 6191 * all the original i/o dependency relationships, e.g. 6192 * parents not executing until children are ready. 6193 */ 6194 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 6195 6196 zio->io_gang_leader = NULL; 6197 6198 mutex_enter(&zio->io_lock); 6199 zio->io_state[ZIO_WAIT_DONE] = 1; 6200 mutex_exit(&zio->io_lock); 6201 6202 /* 6203 * "The Godfather" I/O monitors its children but is 6204 * not a true parent to them. It will track them through 6205 * the pipeline but severs its ties whenever they get into 6206 * trouble (e.g. suspended). This allows "The Godfather" 6207 * I/O to return status without blocking. 6208 */ 6209 zl = NULL; 6210 for (pio = zio_walk_parents(zio, &zl); pio != NULL; 6211 pio = pio_next) { 6212 zio_link_t *remove_zl = zl; 6213 pio_next = zio_walk_parents(zio, &zl); 6214 6215 if ((pio->io_flags & ZIO_FLAG_GODFATHER) && 6216 (zio->io_post & ZIO_POST_SUSPEND)) { 6217 zio_remove_child(pio, zio, remove_zl); 6218 /* 6219 * This is a rare code path, so we don't 6220 * bother with the "next" list. 6221 */ 6222 zio_notify_parent(pio, zio, ZIO_WAIT_DONE, 6223 NULL); 6224 } 6225 } 6226 6227 if ((pio = zio_unique_parent(zio)) != NULL) { 6228 /* 6229 * We're not a root i/o, so there's nothing to do 6230 * but notify our parent. Don't propagate errors 6231 * upward since we haven't permanently failed yet. 6232 */ 6233 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER)); 6234 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE; 6235 /* 6236 * This is a rare code path, so we don't bother with 6237 * the "next" list. 6238 */ 6239 zio_notify_parent(pio, zio, ZIO_WAIT_DONE, NULL); 6240 } else if (zio->io_post & ZIO_POST_SUSPEND) { 6241 /* 6242 * We'd fail again if we reexecuted now, so suspend 6243 * until conditions improve (e.g. device comes online). 6244 */ 6245 zio_suspend(zio->io_spa, zio, ZIO_SUSPEND_IOERR); 6246 } else { 6247 ASSERT(zio->io_post & ZIO_POST_REEXECUTE); 6248 /* 6249 * Reexecution is potentially a huge amount of work. 6250 * Hand it off to the otherwise-unused claim taskq. 6251 */ 6252 spa_taskq_dispatch(zio->io_spa, 6253 ZIO_TYPE_CLAIM, ZIO_TASKQ_ISSUE, 6254 zio_reexecute, zio, B_FALSE); 6255 } 6256 return (NULL); 6257 } 6258 6259 ASSERT(list_is_empty(&zio->io_child_list)); 6260 ASSERT0(zio->io_post & ZIO_POST_REEXECUTE); 6261 ASSERT0(zio->io_post & ZIO_POST_SUSPEND); 6262 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL)); 6263 6264 /* 6265 * Report any checksum errors, since the I/O is complete. 6266 */ 6267 while (zio->io_cksum_report != NULL) { 6268 zio_cksum_report_t *zcr = zio->io_cksum_report; 6269 zio->io_cksum_report = zcr->zcr_next; 6270 zcr->zcr_next = NULL; 6271 zcr->zcr_finish(zcr, NULL); 6272 zfs_ereport_free_checksum(zcr); 6273 } 6274 6275 if (zio->io_flags & ZIO_FLAG_POSTREAD) { 6276 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE); 6277 zl = NULL; 6278 zio_t *pio = zio_walk_parents(zio, &zl); 6279 blkptr_t *bp = zio->io_bp; 6280 abd_t *abd = abd_alloc_for_io(BP_GET_PSIZE(bp), B_FALSE); 6281 zio_priority_t prio = zio->io_priority == 6282 ZIO_PRIORITY_SYNC_WRITE ? ZIO_PRIORITY_SYNC_READ : 6283 ZIO_PRIORITY_SCRUB; 6284 zio_t *cio = zio_vdev_child_io(pio, zio->io_bp, zio->io_vd, 6285 zio->io_offset, abd, zio->io_size, ZIO_TYPE_READ, prio, 6286 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL | 6287 ZIO_FLAG_RESILVER | ZIO_FLAG_DONT_PROPAGATE, 6288 zio_done_postread_done, NULL); 6289 cio->io_flags &= ~ZIO_FLAG_ALLOC_THROTTLED; 6290 zio_nowait(cio); 6291 } 6292 6293 /* 6294 * It is the responsibility of the done callback to ensure that this 6295 * particular zio is no longer discoverable for adoption, and as 6296 * such, cannot acquire any new parents. 6297 */ 6298 if (zio->io_done) 6299 zio->io_done(zio); 6300 6301 mutex_enter(&zio->io_lock); 6302 zio->io_state[ZIO_WAIT_DONE] = 1; 6303 mutex_exit(&zio->io_lock); 6304 6305 /* 6306 * We are done executing this zio. We may want to execute some of its 6307 * parents next. See the comment in zio_notify_parent(). 6308 */ 6309 zio_next_t next; 6310 zio_next_init(&next); 6311 zl = NULL; 6312 for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) { 6313 zio_link_t *remove_zl = zl; 6314 pio_next = zio_walk_parents(zio, &zl); 6315 zio_remove_child(pio, zio, remove_zl); 6316 zio_notify_parent(pio, zio, ZIO_WAIT_DONE, &next); 6317 } 6318 6319 if (zio->io_waiter != NULL) { 6320 mutex_enter(&zio->io_lock); 6321 zio->io_executor = NULL; 6322 cv_broadcast(&zio->io_cv); 6323 mutex_exit(&zio->io_lock); 6324 } else { 6325 zio_destroy(zio); 6326 } 6327 6328 return (next.zn_list); 6329 } 6330 6331 /* 6332 * ========================================================================== 6333 * I/O pipeline definition 6334 * ========================================================================== 6335 */ 6336 static zio_pipe_stage_t *zio_pipeline[] = { 6337 NULL, 6338 zio_read_bp_init, 6339 zio_write_bp_init, 6340 zio_free_bp_init, 6341 zio_issue_async, 6342 zio_write_compress, 6343 zio_encrypt, 6344 zio_checksum_generate, 6345 zio_nop_write, 6346 zio_ddt_read_start, 6347 zio_ddt_read_done, 6348 zio_ddt_write, 6349 zio_ddt_free, 6350 zio_brt_free, 6351 zio_gang_assemble, 6352 zio_gang_issue, 6353 zio_dva_throttle, 6354 zio_dva_allocate, 6355 zio_dva_free, 6356 zio_dva_claim, 6357 zio_ready, 6358 zio_vdev_io_start, 6359 zio_vdev_io_done, 6360 zio_vdev_io_assess, 6361 zio_checksum_verify, 6362 zio_dio_checksum_verify, 6363 zio_done 6364 }; 6365 6366 6367 6368 6369 /* 6370 * Compare two zbookmark_phys_t's to see which we would reach first in a 6371 * pre-order traversal of the object tree. 6372 * 6373 * This is simple in every case aside from the meta-dnode object. For all other 6374 * objects, we traverse them in order (object 1 before object 2, and so on). 6375 * However, all of these objects are traversed while traversing object 0, since 6376 * the data it points to is the list of objects. Thus, we need to convert to a 6377 * canonical representation so we can compare meta-dnode bookmarks to 6378 * non-meta-dnode bookmarks. 6379 * 6380 * We do this by calculating "equivalents" for each field of the zbookmark. 6381 * zbookmarks outside of the meta-dnode use their own object and level, and 6382 * calculate the level 0 equivalent (the first L0 blkid that is contained in the 6383 * blocks this bookmark refers to) by multiplying their blkid by their span 6384 * (the number of L0 blocks contained within one block at their level). 6385 * zbookmarks inside the meta-dnode calculate their object equivalent 6386 * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use 6387 * level + 1<<31 (any value larger than a level could ever be) for their level. 6388 * This causes them to always compare before a bookmark in their object 6389 * equivalent, compare appropriately to bookmarks in other objects, and to 6390 * compare appropriately to other bookmarks in the meta-dnode. 6391 */ 6392 int 6393 zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2, 6394 const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2) 6395 { 6396 /* 6397 * These variables represent the "equivalent" values for the zbookmark, 6398 * after converting zbookmarks inside the meta dnode to their 6399 * normal-object equivalents. 6400 */ 6401 uint64_t zb1obj, zb2obj; 6402 uint64_t zb1L0, zb2L0; 6403 uint64_t zb1level, zb2level; 6404 6405 if (zb1->zb_object == zb2->zb_object && 6406 zb1->zb_level == zb2->zb_level && 6407 zb1->zb_blkid == zb2->zb_blkid) 6408 return (0); 6409 6410 if (zb1->zb_level < 0 || zb2->zb_level < 0) { 6411 /* 6412 * "Negative" levels are ZB_ROOT_LEVEL, ZB_ZIL_LEVEL or 6413 * ZB_DNODE_LEVEL, and represent some sort of auxiliary dataset 6414 * block or object. In this case, we're usually being called 6415 * from dsl_scan or dmu_traverse. 6416 * 6417 * These "levels" are more like a "type" signal, not directly 6418 * comparable, but we have to do something. So we order them in 6419 * the order we would see them during a typical scan or 6420 * traverse: 6421 * 6422 * - ZB_ROOT_LEVEL: the "top" block carrying the dataset head 6423 * - ZB_ZIL_LEVEL: the head ZIL block attached to the dataset 6424 * - ZB_DNODE_LEVEL: "virtual" position representing an 6425 * entire object. Sorts ahead of the true 6426 * data blocks for the object. 6427 * - level >= 0: data blocks 6428 * 6429 * We work through these cases from top to bottom, with 6430 * appropriate tiebreaks for each kind. 6431 */ 6432 6433 /* 6434 * Root level wins. It shouldn't be possible for both to be the 6435 * root level in this per-dataset tree, and there's no obvious 6436 * tiebreaker, but we handle it as a defensive measure. 6437 */ 6438 if (zb1->zb_level == ZB_ROOT_LEVEL && 6439 zb2->zb_level == ZB_ROOT_LEVEL) 6440 return (TREE_PCMP(zb1, zb2)); 6441 if (zb1->zb_level == ZB_ROOT_LEVEL) 6442 return (-1); 6443 if (zb2->zb_level == ZB_ROOT_LEVEL) 6444 return (1); 6445 6446 /* ZIL bookmarks have valid blkid, so the earlier one wins. */ 6447 if (zb1->zb_level == ZB_ZIL_LEVEL && 6448 zb2->zb_level == ZB_ZIL_LEVEL) 6449 return (TREE_CMP(zb1->zb_blkid, zb2->zb_blkid)); 6450 if (zb1->zb_level == ZB_ZIL_LEVEL) 6451 return (-1); 6452 if (zb2->zb_level == ZB_ZIL_LEVEL) 6453 return (1); 6454 6455 /* 6456 * If we get this far, then at least one is ZB_DNODE_LEVEL, and 6457 * the other is either ZB_DNODE_LEVEL or a data block. 6458 * Regardless, the one with the lower-numbered object wins - 6459 * earler ZB_DNODE_LEVEL beats later, but data block on earlier 6460 * objects beats the virtual marker on later objects. 6461 */ 6462 int cmp = TREE_CMP(zb1->zb_object, zb2->zb_object); 6463 if (cmp != 0) 6464 return (cmp); 6465 6466 if (zb1->zb_level == ZB_DNODE_LEVEL) 6467 return (-1); 6468 return (1); 6469 } 6470 6471 IMPLY(zb1->zb_level > 0, ibs1 >= SPA_MINBLOCKSHIFT); 6472 IMPLY(zb2->zb_level > 0, ibs2 >= SPA_MINBLOCKSHIFT); 6473 6474 /* 6475 * BP_SPANB calculates the span in blocks. 6476 */ 6477 zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level); 6478 zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level); 6479 6480 if (zb1->zb_object == DMU_META_DNODE_OBJECT) { 6481 zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT)); 6482 zb1L0 = 0; 6483 zb1level = zb1->zb_level + COMPARE_META_LEVEL; 6484 } else { 6485 zb1obj = zb1->zb_object; 6486 zb1level = zb1->zb_level; 6487 } 6488 6489 if (zb2->zb_object == DMU_META_DNODE_OBJECT) { 6490 zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT)); 6491 zb2L0 = 0; 6492 zb2level = zb2->zb_level + COMPARE_META_LEVEL; 6493 } else { 6494 zb2obj = zb2->zb_object; 6495 zb2level = zb2->zb_level; 6496 } 6497 6498 /* Now that we have a canonical representation, do the comparison. */ 6499 if (zb1obj != zb2obj) 6500 return (zb1obj < zb2obj ? -1 : 1); 6501 else if (zb1L0 != zb2L0) 6502 return (zb1L0 < zb2L0 ? -1 : 1); 6503 else if (zb1level != zb2level) 6504 return (zb1level > zb2level ? -1 : 1); 6505 /* 6506 * This can (theoretically) happen if the bookmarks have the same object 6507 * and level, but different blkids, if the block sizes are not the same. 6508 * There is presently no way to change the indirect block sizes 6509 */ 6510 return (0); 6511 } 6512 6513 /* 6514 * This function checks the following: given that last_block is the place that 6515 * our traversal stopped last time, does that guarantee that we've visited 6516 * every node under subtree_root? Therefore, we can't just use the raw output 6517 * of zbookmark_compare. We have to pass in a modified version of 6518 * subtree_root; by incrementing the block id, and then checking whether 6519 * last_block is before or equal to that, we can tell whether or not having 6520 * visited last_block implies that all of subtree_root's children have been 6521 * visited. 6522 */ 6523 boolean_t 6524 zbookmark_subtree_completed(const dnode_phys_t *dnp, 6525 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block) 6526 { 6527 zbookmark_phys_t mod_zb = *subtree_root; 6528 mod_zb.zb_blkid++; 6529 ASSERT0(last_block->zb_level); 6530 6531 /* The objset_phys_t isn't before anything. */ 6532 if (dnp == NULL) 6533 return (B_FALSE); 6534 6535 /* 6536 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the 6537 * data block size in sectors, because that variable is only used if 6538 * the bookmark refers to a block in the meta-dnode. Since we don't 6539 * know without examining it what object it refers to, and there's no 6540 * harm in passing in this value in other cases, we always pass it in. 6541 * 6542 * We pass in 0 for the indirect block size shift because zb2 must be 6543 * level 0. The indirect block size is only used to calculate the span 6544 * of the bookmark, but since the bookmark must be level 0, the span is 6545 * always 1, so the math works out. 6546 * 6547 * If you make changes to how the zbookmark_compare code works, be sure 6548 * to make sure that this code still works afterwards. 6549 */ 6550 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift, 6551 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb, 6552 last_block) <= 0); 6553 } 6554 6555 /* 6556 * This function is similar to zbookmark_subtree_completed(), but returns true 6557 * if subtree_root is equal or ahead of last_block, i.e. still to be done. 6558 */ 6559 boolean_t 6560 zbookmark_subtree_tbd(const dnode_phys_t *dnp, 6561 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block) 6562 { 6563 ASSERT0(last_block->zb_level); 6564 if (dnp == NULL) 6565 return (B_FALSE); 6566 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift, 6567 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, subtree_root, 6568 last_block) >= 0); 6569 } 6570 6571 EXPORT_SYMBOL(zio_type_name); 6572 EXPORT_SYMBOL(zio_buf_alloc); 6573 EXPORT_SYMBOL(zio_data_buf_alloc); 6574 EXPORT_SYMBOL(zio_buf_free); 6575 EXPORT_SYMBOL(zio_data_buf_free); 6576 6577 ZFS_MODULE_PARAM(zfs_zio, zio_, slow_io_ms, INT, ZMOD_RW, 6578 "Max I/O completion time (milliseconds) before marking it as slow"); 6579 6580 ZFS_MODULE_PARAM(zfs_zio, zio_, requeue_io_start_cut_in_line, INT, ZMOD_RW, 6581 "Prioritize requeued I/O"); 6582 6583 ZFS_MODULE_PARAM(zfs_zio, zio_, batch_enabled, INT, ZMOD_RW, 6584 "Batch processing of vdev children I/O completions"); 6585 6586 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_deferred_free, UINT, ZMOD_RW, 6587 "Defer frees starting in this pass"); 6588 6589 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_dont_compress, UINT, ZMOD_RW, 6590 "Don't compress starting in this pass"); 6591 6592 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_rewrite, UINT, ZMOD_RW, 6593 "Rewrite new bps starting in this pass"); 6594 6595 ZFS_MODULE_PARAM(zfs_zio, zio_, dva_throttle_enabled, INT, ZMOD_RW, 6596 "Throttle block allocations in the ZIO pipeline"); 6597 6598 ZFS_MODULE_PARAM(zfs_zio, zio_, deadman_log_all, INT, ZMOD_RW, 6599 "Log all slow ZIOs, not just those with vdevs"); 6600