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