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