Home | History | Annotate | Download | only in zfs
      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 2009 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 #include <sys/zfs_context.h>
     27 #include <sys/fm/fs/zfs.h>
     28 #include <sys/spa.h>
     29 #include <sys/txg.h>
     30 #include <sys/spa_impl.h>
     31 #include <sys/vdev_impl.h>
     32 #include <sys/zio_impl.h>
     33 #include <sys/zio_compress.h>
     34 #include <sys/zio_checksum.h>
     35 #include <sys/dmu_objset.h>
     36 #include <sys/arc.h>
     37 #include <sys/ddt.h>
     38 
     39 /*
     40  * ==========================================================================
     41  * I/O priority table
     42  * ==========================================================================
     43  */
     44 uint8_t zio_priority_table[ZIO_PRIORITY_TABLE_SIZE] = {
     45 	0,	/* ZIO_PRIORITY_NOW		*/
     46 	0,	/* ZIO_PRIORITY_SYNC_READ	*/
     47 	0,	/* ZIO_PRIORITY_SYNC_WRITE	*/
     48 	6,	/* ZIO_PRIORITY_ASYNC_READ	*/
     49 	4,	/* ZIO_PRIORITY_ASYNC_WRITE	*/
     50 	4,	/* ZIO_PRIORITY_FREE		*/
     51 	0,	/* ZIO_PRIORITY_CACHE_FILL	*/
     52 	0,	/* ZIO_PRIORITY_LOG_WRITE	*/
     53 	10,	/* ZIO_PRIORITY_RESILVER	*/
     54 	20,	/* ZIO_PRIORITY_SCRUB		*/
     55 };
     56 
     57 /*
     58  * ==========================================================================
     59  * I/O type descriptions
     60  * ==========================================================================
     61  */
     62 char *zio_type_name[ZIO_TYPES] = {
     63 	"null", "read", "write", "free", "claim", "ioctl" };
     64 
     65 /*
     66  * ==========================================================================
     67  * I/O kmem caches
     68  * ==========================================================================
     69  */
     70 kmem_cache_t *zio_cache;
     71 kmem_cache_t *zio_link_cache;
     72 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
     73 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
     74 
     75 #ifdef _KERNEL
     76 extern vmem_t *zio_alloc_arena;
     77 #endif
     78 
     79 /*
     80  * An allocating zio is one that either currently has the DVA allocate
     81  * stage set or will have it later in its lifetime.
     82  */
     83 #define	IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
     84 
     85 #ifdef ZFS_DEBUG
     86 int zio_buf_debug_limit = 16384;
     87 #else
     88 int zio_buf_debug_limit = 0;
     89 #endif
     90 
     91 void
     92 zio_init(void)
     93 {
     94 	size_t c;
     95 	vmem_t *data_alloc_arena = NULL;
     96 
     97 #ifdef _KERNEL
     98 	data_alloc_arena = zio_alloc_arena;
     99 #endif
    100 	zio_cache = kmem_cache_create("zio_cache",
    101 	    sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
    102 	zio_link_cache = kmem_cache_create("zio_link_cache",
    103 	    sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
    104 
    105 	/*
    106 	 * For small buffers, we want a cache for each multiple of
    107 	 * SPA_MINBLOCKSIZE.  For medium-size buffers, we want a cache
    108 	 * for each quarter-power of 2.  For large buffers, we want
    109 	 * a cache for each multiple of PAGESIZE.
    110 	 */
    111 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
    112 		size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
    113 		size_t p2 = size;
    114 		size_t align = 0;
    115 
    116 		while (p2 & (p2 - 1))
    117 			p2 &= p2 - 1;
    118 
    119 		if (size <= 4 * SPA_MINBLOCKSIZE) {
    120 			align = SPA_MINBLOCKSIZE;
    121 		} else if (P2PHASE(size, PAGESIZE) == 0) {
    122 			align = PAGESIZE;
    123 		} else if (P2PHASE(size, p2 >> 2) == 0) {
    124 			align = p2 >> 2;
    125 		}
    126 
    127 		if (align != 0) {
    128 			char name[36];
    129 			(void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
    130 			zio_buf_cache[c] = kmem_cache_create(name, size,
    131 			    align, NULL, NULL, NULL, NULL, NULL,
    132 			    size > zio_buf_debug_limit ? KMC_NODEBUG : 0);
    133 
    134 			(void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
    135 			zio_data_buf_cache[c] = kmem_cache_create(name, size,
    136 			    align, NULL, NULL, NULL, NULL, data_alloc_arena,
    137 			    size > zio_buf_debug_limit ? KMC_NODEBUG : 0);
    138 		}
    139 	}
    140 
    141 	while (--c != 0) {
    142 		ASSERT(zio_buf_cache[c] != NULL);
    143 		if (zio_buf_cache[c - 1] == NULL)
    144 			zio_buf_cache[c - 1] = zio_buf_cache[c];
    145 
    146 		ASSERT(zio_data_buf_cache[c] != NULL);
    147 		if (zio_data_buf_cache[c - 1] == NULL)
    148 			zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
    149 	}
    150 
    151 	zio_inject_init();
    152 }
    153 
    154 void
    155 zio_fini(void)
    156 {
    157 	size_t c;
    158 	kmem_cache_t *last_cache = NULL;
    159 	kmem_cache_t *last_data_cache = NULL;
    160 
    161 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
    162 		if (zio_buf_cache[c] != last_cache) {
    163 			last_cache = zio_buf_cache[c];
    164 			kmem_cache_destroy(zio_buf_cache[c]);
    165 		}
    166 		zio_buf_cache[c] = NULL;
    167 
    168 		if (zio_data_buf_cache[c] != last_data_cache) {
    169 			last_data_cache = zio_data_buf_cache[c];
    170 			kmem_cache_destroy(zio_data_buf_cache[c]);
    171 		}
    172 		zio_data_buf_cache[c] = NULL;
    173 	}
    174 
    175 	kmem_cache_destroy(zio_link_cache);
    176 	kmem_cache_destroy(zio_cache);
    177 
    178 	zio_inject_fini();
    179 }
    180 
    181 /*
    182  * ==========================================================================
    183  * Allocate and free I/O buffers
    184  * ==========================================================================
    185  */
    186 
    187 /*
    188  * Use zio_buf_alloc to allocate ZFS metadata.  This data will appear in a
    189  * crashdump if the kernel panics, so use it judiciously.  Obviously, it's
    190  * useful to inspect ZFS metadata, but if possible, we should avoid keeping
    191  * excess / transient data in-core during a crashdump.
    192  */
    193 void *
    194 zio_buf_alloc(size_t size)
    195 {
    196 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
    197 
    198 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
    199 
    200 	return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
    201 }
    202 
    203 /*
    204  * Use zio_data_buf_alloc to allocate data.  The data will not appear in a
    205  * crashdump if the kernel panics.  This exists so that we will limit the amount
    206  * of ZFS data that shows up in a kernel crashdump.  (Thus reducing the amount
    207  * of kernel heap dumped to disk when the kernel panics)
    208  */
    209 void *
    210 zio_data_buf_alloc(size_t size)
    211 {
    212 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
    213 
    214 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
    215 
    216 	return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
    217 }
    218 
    219 void
    220 zio_buf_free(void *buf, size_t size)
    221 {
    222 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
    223 
    224 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
    225 
    226 	kmem_cache_free(zio_buf_cache[c], buf);
    227 }
    228 
    229 void
    230 zio_data_buf_free(void *buf, size_t size)
    231 {
    232 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
    233 
    234 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
    235 
    236 	kmem_cache_free(zio_data_buf_cache[c], buf);
    237 }
    238 
    239 /*
    240  * ==========================================================================
    241  * Push and pop I/O transform buffers
    242  * ==========================================================================
    243  */
    244 static void
    245 zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize,
    246 	zio_transform_func_t *transform)
    247 {
    248 	zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
    249 
    250 	zt->zt_orig_data = zio->io_data;
    251 	zt->zt_orig_size = zio->io_size;
    252 	zt->zt_bufsize = bufsize;
    253 	zt->zt_transform = transform;
    254 
    255 	zt->zt_next = zio->io_transform_stack;
    256 	zio->io_transform_stack = zt;
    257 
    258 	zio->io_data = data;
    259 	zio->io_size = size;
    260 }
    261 
    262 static void
    263 zio_pop_transforms(zio_t *zio)
    264 {
    265 	zio_transform_t *zt;
    266 
    267 	while ((zt = zio->io_transform_stack) != NULL) {
    268 		if (zt->zt_transform != NULL)
    269 			zt->zt_transform(zio,
    270 			    zt->zt_orig_data, zt->zt_orig_size);
    271 
    272 		if (zt->zt_bufsize != 0)
    273 			zio_buf_free(zio->io_data, zt->zt_bufsize);
    274 
    275 		zio->io_data = zt->zt_orig_data;
    276 		zio->io_size = zt->zt_orig_size;
    277 		zio->io_transform_stack = zt->zt_next;
    278 
    279 		kmem_free(zt, sizeof (zio_transform_t));
    280 	}
    281 }
    282 
    283 /*
    284  * ==========================================================================
    285  * I/O transform callbacks for subblocks and decompression
    286  * ==========================================================================
    287  */
    288 static void
    289 zio_subblock(zio_t *zio, void *data, uint64_t size)
    290 {
    291 	ASSERT(zio->io_size > size);
    292 
    293 	if (zio->io_type == ZIO_TYPE_READ)
    294 		bcopy(zio->io_data, data, size);
    295 }
    296 
    297 static void
    298 zio_decompress(zio_t *zio, void *data, uint64_t size)
    299 {
    300 	if (zio->io_error == 0 &&
    301 	    zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
    302 	    zio->io_data, data, zio->io_size, size) != 0)
    303 		zio->io_error = EIO;
    304 }
    305 
    306 /*
    307  * ==========================================================================
    308  * I/O parent/child relationships and pipeline interlocks
    309  * ==========================================================================
    310  */
    311 /*
    312  * NOTE - Callers to zio_walk_parents() and zio_walk_children must
    313  *        continue calling these functions until they return NULL.
    314  *        Otherwise, the next caller will pick up the list walk in
    315  *        some indeterminate state.  (Otherwise every caller would
    316  *        have to pass in a cookie to keep the state represented by
    317  *        io_walk_link, which gets annoying.)
    318  */
    319 zio_t *
    320 zio_walk_parents(zio_t *cio)
    321 {
    322 	zio_link_t *zl = cio->io_walk_link;
    323 	list_t *pl = &cio->io_parent_list;
    324 
    325 	zl = (zl == NULL) ? list_head(pl) : list_next(pl, zl);
    326 	cio->io_walk_link = zl;
    327 
    328 	if (zl == NULL)
    329 		return (NULL);
    330 
    331 	ASSERT(zl->zl_child == cio);
    332 	return (zl->zl_parent);
    333 }
    334 
    335 zio_t *
    336 zio_walk_children(zio_t *pio)
    337 {
    338 	zio_link_t *zl = pio->io_walk_link;
    339 	list_t *cl = &pio->io_child_list;
    340 
    341 	zl = (zl == NULL) ? list_head(cl) : list_next(cl, zl);
    342 	pio->io_walk_link = zl;
    343 
    344 	if (zl == NULL)
    345 		return (NULL);
    346 
    347 	ASSERT(zl->zl_parent == pio);
    348 	return (zl->zl_child);
    349 }
    350 
    351 zio_t *
    352 zio_unique_parent(zio_t *cio)
    353 {
    354 	zio_t *pio = zio_walk_parents(cio);
    355 
    356 	VERIFY(zio_walk_parents(cio) == NULL);
    357 	return (pio);
    358 }
    359 
    360 void
    361 zio_add_child(zio_t *pio, zio_t *cio)
    362 {
    363 	zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
    364 
    365 	/*
    366 	 * Logical I/Os can have logical, gang, or vdev children.
    367 	 * Gang I/Os can have gang or vdev children.
    368 	 * Vdev I/Os can only have vdev children.
    369 	 * The following ASSERT captures all of these constraints.
    370 	 */
    371 	ASSERT(cio->io_child_type <= pio->io_child_type);
    372 
    373 	zl->zl_parent = pio;
    374 	zl->zl_child = cio;
    375 
    376 	mutex_enter(&cio->io_lock);
    377 	mutex_enter(&pio->io_lock);
    378 
    379 	ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
    380 
    381 	for (int w = 0; w < ZIO_WAIT_TYPES; w++)
    382 		pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
    383 
    384 	list_insert_head(&pio->io_child_list, zl);
    385 	list_insert_head(&cio->io_parent_list, zl);
    386 
    387 	pio->io_child_count++;
    388 	cio->io_parent_count++;
    389 
    390 	mutex_exit(&pio->io_lock);
    391 	mutex_exit(&cio->io_lock);
    392 }
    393 
    394 static void
    395 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
    396 {
    397 	ASSERT(zl->zl_parent == pio);
    398 	ASSERT(zl->zl_child == cio);
    399 
    400 	mutex_enter(&cio->io_lock);
    401 	mutex_enter(&pio->io_lock);
    402 
    403 	list_remove(&pio->io_child_list, zl);
    404 	list_remove(&cio->io_parent_list, zl);
    405 
    406 	pio->io_child_count--;
    407 	cio->io_parent_count--;
    408 
    409 	mutex_exit(&pio->io_lock);
    410 	mutex_exit(&cio->io_lock);
    411 
    412 	kmem_cache_free(zio_link_cache, zl);
    413 }
    414 
    415 static boolean_t
    416 zio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait)
    417 {
    418 	uint64_t *countp = &zio->io_children[child][wait];
    419 	boolean_t waiting = B_FALSE;
    420 
    421 	mutex_enter(&zio->io_lock);
    422 	ASSERT(zio->io_stall == NULL);
    423 	if (*countp != 0) {
    424 		zio->io_stage >>= 1;
    425 		zio->io_stall = countp;
    426 		waiting = B_TRUE;
    427 	}
    428 	mutex_exit(&zio->io_lock);
    429 
    430 	return (waiting);
    431 }
    432 
    433 static void
    434 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
    435 {
    436 	uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
    437 	int *errorp = &pio->io_child_error[zio->io_child_type];
    438 
    439 	mutex_enter(&pio->io_lock);
    440 	if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
    441 		*errorp = zio_worst_error(*errorp, zio->io_error);
    442 	pio->io_reexecute |= zio->io_reexecute;
    443 	ASSERT3U(*countp, >, 0);
    444 	if (--*countp == 0 && pio->io_stall == countp) {
    445 		pio->io_stall = NULL;
    446 		mutex_exit(&pio->io_lock);
    447 		zio_execute(pio);
    448 	} else {
    449 		mutex_exit(&pio->io_lock);
    450 	}
    451 }
    452 
    453 static void
    454 zio_inherit_child_errors(zio_t *zio, enum zio_child c)
    455 {
    456 	if (zio->io_child_error[c] != 0 && zio->io_error == 0)
    457 		zio->io_error = zio->io_child_error[c];
    458 }
    459 
    460 /*
    461  * ==========================================================================
    462  * Create the various types of I/O (read, write, free, etc)
    463  * ==========================================================================
    464  */
    465 static zio_t *
    466 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
    467     void *data, uint64_t size, zio_done_func_t *done, void *private,
    468     zio_type_t type, int priority, enum zio_flag flags,
    469     vdev_t *vd, uint64_t offset, const zbookmark_t *zb,
    470     enum zio_stage stage, enum zio_stage pipeline)
    471 {
    472 	zio_t *zio;
    473 
    474 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
    475 	ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
    476 	ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
    477 
    478 	ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
    479 	ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
    480 	ASSERT(vd || stage == ZIO_STAGE_OPEN);
    481 
    482 	zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
    483 	bzero(zio, sizeof (zio_t));
    484 
    485 	mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
    486 	cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
    487 
    488 	list_create(&zio->io_parent_list, sizeof (zio_link_t),
    489 	    offsetof(zio_link_t, zl_parent_node));
    490 	list_create(&zio->io_child_list, sizeof (zio_link_t),
    491 	    offsetof(zio_link_t, zl_child_node));
    492 
    493 	if (vd != NULL)
    494 		zio->io_child_type = ZIO_CHILD_VDEV;
    495 	else if (flags & ZIO_FLAG_GANG_CHILD)
    496 		zio->io_child_type = ZIO_CHILD_GANG;
    497 	else if (flags & ZIO_FLAG_DDT_CHILD)
    498 		zio->io_child_type = ZIO_CHILD_DDT;
    499 	else
    500 		zio->io_child_type = ZIO_CHILD_LOGICAL;
    501 
    502 	if (bp != NULL) {
    503 		zio->io_bp = (blkptr_t *)bp;
    504 		zio->io_bp_copy = *bp;
    505 		zio->io_bp_orig = *bp;
    506 		if (type != ZIO_TYPE_WRITE ||
    507 		    zio->io_child_type == ZIO_CHILD_DDT)
    508 			zio->io_bp = &zio->io_bp_copy;	/* so caller can free */
    509 		if (zio->io_child_type == ZIO_CHILD_LOGICAL)
    510 			zio->io_logical = zio;
    511 		if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
    512 			pipeline |= ZIO_GANG_STAGES;
    513 	}
    514 
    515 	zio->io_spa = spa;
    516 	zio->io_txg = txg;
    517 	zio->io_done = done;
    518 	zio->io_private = private;
    519 	zio->io_type = type;
    520 	zio->io_priority = priority;
    521 	zio->io_vd = vd;
    522 	zio->io_offset = offset;
    523 	zio->io_orig_data = zio->io_data = data;
    524 	zio->io_orig_size = zio->io_size = size;
    525 	zio->io_orig_flags = zio->io_flags = flags;
    526 	zio->io_orig_stage = zio->io_stage = stage;
    527 	zio->io_orig_pipeline = zio->io_pipeline = pipeline;
    528 
    529 	zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
    530 	zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
    531 
    532 	if (zb != NULL)
    533 		zio->io_bookmark = *zb;
    534 
    535 	if (pio != NULL) {
    536 		if (zio->io_logical == NULL)
    537 			zio->io_logical = pio->io_logical;
    538 		if (zio->io_child_type == ZIO_CHILD_GANG)
    539 			zio->io_gang_leader = pio->io_gang_leader;
    540 		zio_add_child(pio, zio);
    541 	}
    542 
    543 	return (zio);
    544 }
    545 
    546 static void
    547 zio_destroy(zio_t *zio)
    548 {
    549 	list_destroy(&zio->io_parent_list);
    550 	list_destroy(&zio->io_child_list);
    551 	mutex_destroy(&zio->io_lock);
    552 	cv_destroy(&zio->io_cv);
    553 	kmem_cache_free(zio_cache, zio);
    554 }
    555 
    556 zio_t *
    557 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
    558     void *private, enum zio_flag flags)
    559 {
    560 	zio_t *zio;
    561 
    562 	zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
    563 	    ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
    564 	    ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
    565 
    566 	return (zio);
    567 }
    568 
    569 zio_t *
    570 zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
    571 {
    572 	return (zio_null(NULL, spa, NULL, done, private, flags));
    573 }
    574 
    575 zio_t *
    576 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
    577     void *data, uint64_t size, zio_done_func_t *done, void *private,
    578     int priority, enum zio_flag flags, const zbookmark_t *zb)
    579 {
    580 	zio_t *zio;
    581 
    582 	zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
    583 	    data, size, done, private,
    584 	    ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
    585 	    ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
    586 	    ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
    587 
    588 	return (zio);
    589 }
    590 
    591 zio_t *
    592 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
    593     void *data, uint64_t size, const zio_prop_t *zp,
    594     zio_done_func_t *ready, zio_done_func_t *done, void *private,
    595     int priority, enum zio_flag flags, const zbookmark_t *zb)
    596 {
    597 	zio_t *zio;
    598 
    599 	ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
    600 	    zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
    601 	    zp->zp_compress >= ZIO_COMPRESS_OFF &&
    602 	    zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
    603 	    zp->zp_type < DMU_OT_NUMTYPES &&
    604 	    zp->zp_level < 32 &&
    605 	    zp->zp_copies > 0 &&
    606 	    zp->zp_copies <= spa_max_replication(spa) &&
    607 	    zp->zp_dedup <= 1 &&
    608 	    zp->zp_dedup_verify <= 1);
    609 
    610 	zio = zio_create(pio, spa, txg, bp, data, size, done, private,
    611 	    ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
    612 	    ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
    613 	    ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
    614 
    615 	zio->io_ready = ready;
    616 	zio->io_prop = *zp;
    617 
    618 	return (zio);
    619 }
    620 
    621 zio_t *
    622 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, void *data,
    623     uint64_t size, zio_done_func_t *done, void *private, int priority,
    624     enum zio_flag flags, zbookmark_t *zb)
    625 {
    626 	zio_t *zio;
    627 
    628 	zio = zio_create(pio, spa, txg, bp, data, size, done, private,
    629 	    ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
    630 	    ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
    631 
    632 	return (zio);
    633 }
    634 
    635 void
    636 zio_write_override(zio_t *zio, blkptr_t *bp, int copies)
    637 {
    638 	ASSERT(zio->io_type == ZIO_TYPE_WRITE);
    639 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
    640 	ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
    641 	ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
    642 
    643 	zio->io_prop.zp_copies = copies;
    644 	zio->io_bp_override = bp;
    645 }
    646 
    647 void
    648 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
    649 {
    650 	bplist_enqueue_deferred(&spa->spa_free_bplist[txg & TXG_MASK], bp);
    651 }
    652 
    653 zio_t *
    654 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
    655     enum zio_flag flags)
    656 {
    657 	zio_t *zio;
    658 
    659 	ASSERT(!BP_IS_HOLE(bp));
    660 	ASSERT(spa_syncing_txg(spa) == txg);
    661 	ASSERT(spa_sync_pass(spa) <= SYNC_PASS_DEFERRED_FREE);
    662 
    663 	zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
    664 	    NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_FREE, flags,
    665 	    NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_FREE_PIPELINE);
    666 
    667 	return (zio);
    668 }
    669 
    670 zio_t *
    671 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
    672     zio_done_func_t *done, void *private, enum zio_flag flags)
    673 {
    674 	zio_t *zio;
    675 
    676 	/*
    677 	 * A claim is an allocation of a specific block.  Claims are needed
    678 	 * to support immediate writes in the intent log.  The issue is that
    679 	 * immediate writes contain committed data, but in a txg that was
    680 	 * *not* committed.  Upon opening the pool after an unclean shutdown,
    681 	 * the intent log claims all blocks that contain immediate write data
    682 	 * so that the SPA knows they're in use.
    683 	 *
    684 	 * All claims *must* be resolved in the first txg -- before the SPA
    685 	 * starts allocating blocks -- so that nothing is allocated twice.
    686 	 * If txg == 0 we just verify that the block is claimable.
    687 	 */
    688 	ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
    689 	ASSERT(txg == spa_first_txg(spa) || txg == 0);
    690 	ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa));	/* zdb(1M) */
    691 
    692 	zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
    693 	    done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, flags,
    694 	    NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
    695 
    696 	return (zio);
    697 }
    698 
    699 zio_t *
    700 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
    701     zio_done_func_t *done, void *private, int priority, enum zio_flag flags)
    702 {
    703 	zio_t *zio;
    704 	int c;
    705 
    706 	if (vd->vdev_children == 0) {
    707 		zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
    708 		    ZIO_TYPE_IOCTL, priority, flags, vd, 0, NULL,
    709 		    ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
    710 
    711 		zio->io_cmd = cmd;
    712 	} else {
    713 		zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
    714 
    715 		for (c = 0; c < vd->vdev_children; c++)
    716 			zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
    717 			    done, private, priority, flags));
    718 	}
    719 
    720 	return (zio);
    721 }
    722 
    723 zio_t *
    724 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
    725     void *data, int checksum, zio_done_func_t *done, void *private,
    726     int priority, enum zio_flag flags, boolean_t labels)
    727 {
    728 	zio_t *zio;
    729 
    730 	ASSERT(vd->vdev_children == 0);
    731 	ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
    732 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
    733 	ASSERT3U(offset + size, <=, vd->vdev_psize);
    734 
    735 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
    736 	    ZIO_TYPE_READ, priority, flags, vd, offset, NULL,
    737 	    ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
    738 
    739 	zio->io_prop.zp_checksum = checksum;
    740 
    741 	return (zio);
    742 }
    743 
    744 zio_t *
    745 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
    746     void *data, int checksum, zio_done_func_t *done, void *private,
    747     int priority, enum zio_flag flags, boolean_t labels)
    748 {
    749 	zio_t *zio;
    750 
    751 	ASSERT(vd->vdev_children == 0);
    752 	ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
    753 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
    754 	ASSERT3U(offset + size, <=, vd->vdev_psize);
    755 
    756 	zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
    757 	    ZIO_TYPE_WRITE, priority, flags, vd, offset, NULL,
    758 	    ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
    759 
    760 	zio->io_prop.zp_checksum = checksum;
    761 
    762 	if (zio_checksum_table[checksum].ci_zbt) {
    763 		/*
    764 		 * zbt checksums are necessarily destructive -- they modify
    765 		 * the end of the write buffer to hold the verifier/checksum.
    766 		 * Therefore, we must make a local copy in case the data is
    767 		 * being written to multiple places in parallel.
    768 		 */
    769 		void *wbuf = zio_buf_alloc(size);
    770 		bcopy(data, wbuf, size);
    771 		zio_push_transform(zio, wbuf, size, size, NULL);
    772 	}
    773 
    774 	return (zio);
    775 }
    776 
    777 /*
    778  * Create a child I/O to do some work for us.
    779  */
    780 zio_t *
    781 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
    782 	void *data, uint64_t size, int type, int priority, enum zio_flag flags,
    783 	zio_done_func_t *done, void *private)
    784 {
    785 	enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
    786 	zio_t *zio;
    787 
    788 	ASSERT(vd->vdev_parent ==
    789 	    (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev));
    790 
    791 	if (type == ZIO_TYPE_READ && bp != NULL) {
    792 		/*
    793 		 * If we have the bp, then the child should perform the
    794 		 * checksum and the parent need not.  This pushes error
    795 		 * detection as close to the leaves as possible and
    796 		 * eliminates redundant checksums in the interior nodes.
    797 		 */
    798 		pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
    799 		pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
    800 	}
    801 
    802 	if (vd->vdev_children == 0)
    803 		offset += VDEV_LABEL_START_SIZE;
    804 
    805 	flags |= ZIO_VDEV_CHILD_FLAGS(pio) | ZIO_FLAG_DONT_PROPAGATE;
    806 
    807 	/*
    808 	 * If we've decided to do a repair, the write is not speculative --
    809 	 * even if the original read was.
    810 	 */
    811 	if (flags & ZIO_FLAG_IO_REPAIR)
    812 		flags &= ~ZIO_FLAG_SPECULATIVE;
    813 
    814 	zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size,
    815 	    done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
    816 	    ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
    817 
    818 	return (zio);
    819 }
    820 
    821 zio_t *
    822 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, void *data, uint64_t size,
    823 	int type, int priority, enum zio_flag flags,
    824 	zio_done_func_t *done, void *private)
    825 {
    826 	zio_t *zio;
    827 
    828 	ASSERT(vd->vdev_ops->vdev_op_leaf);
    829 
    830 	zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
    831 	    data, size, done, private, type, priority,
    832 	    flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY,
    833 	    vd, offset, NULL,
    834 	    ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
    835 
    836 	return (zio);
    837 }
    838 
    839 void
    840 zio_flush(zio_t *zio, vdev_t *vd)
    841 {
    842 	zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
    843 	    NULL, NULL, ZIO_PRIORITY_NOW,
    844 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
    845 }
    846 
    847 /*
    848  * ==========================================================================
    849  * Prepare to read and write logical blocks
    850  * ==========================================================================
    851  */
    852 
    853 static int
    854 zio_read_bp_init(zio_t *zio)
    855 {
    856 	blkptr_t *bp = zio->io_bp;
    857 
    858 	if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
    859 	    zio->io_child_type == ZIO_CHILD_LOGICAL &&
    860 	    !(zio->io_flags & ZIO_FLAG_RAW)) {
    861 		uint64_t psize = BP_GET_PSIZE(bp);
    862 		void *cbuf = zio_buf_alloc(psize);
    863 
    864 		zio_push_transform(zio, cbuf, psize, psize, zio_decompress);
    865 	}
    866 
    867 	if (!dmu_ot[BP_GET_TYPE(bp)].ot_metadata && BP_GET_LEVEL(bp) == 0)
    868 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
    869 
    870 	if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
    871 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
    872 
    873 	if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
    874 		zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
    875 
    876 	return (ZIO_PIPELINE_CONTINUE);
    877 }
    878 
    879 static int
    880 zio_write_bp_init(zio_t *zio)
    881 {
    882 	spa_t *spa = zio->io_spa;
    883 	zio_prop_t *zp = &zio->io_prop;
    884 	enum zio_compress compress = zp->zp_compress;
    885 	blkptr_t *bp = zio->io_bp;
    886 	uint64_t lsize = zio->io_size;
    887 	uint64_t psize = lsize;
    888 	int pass = 1;
    889 
    890 	/*
    891 	 * If our children haven't all reached the ready stage,
    892 	 * wait for them and then repeat this pipeline stage.
    893 	 */
    894 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
    895 	    zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY))
    896 		return (ZIO_PIPELINE_STOP);
    897 
    898 	if (!IO_IS_ALLOCATING(zio))
    899 		return (ZIO_PIPELINE_CONTINUE);
    900 
    901 	ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
    902 
    903 	if (zio->io_bp_override) {
    904 		ASSERT(bp->blk_birth != zio->io_txg);
    905 		ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
    906 
    907 		*bp = *zio->io_bp_override;
    908 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
    909 
    910 		if (BP_IS_HOLE(bp) || !zp->zp_dedup)
    911 			return (ZIO_PIPELINE_CONTINUE);
    912 
    913 		ASSERT(zio_checksum_table[zp->zp_checksum].ci_dedup ||
    914 		    zp->zp_dedup_verify);
    915 
    916 		if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) {
    917 			BP_SET_DEDUP(bp, 1);
    918 			zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
    919 			return (ZIO_PIPELINE_CONTINUE);
    920 		}
    921 		zio->io_bp_override = NULL;
    922 		BP_ZERO(bp);
    923 	}
    924 
    925 	if (bp->blk_birth == zio->io_txg) {
    926 		/*
    927 		 * We're rewriting an existing block, which means we're
    928 		 * working on behalf of spa_sync().  For spa_sync() to
    929 		 * converge, it must eventually be the case that we don't
    930 		 * have to allocate new blocks.  But compression changes
    931 		 * the blocksize, which forces a reallocate, and makes
    932 		 * convergence take longer.  Therefore, after the first
    933 		 * few passes, stop compressing to ensure convergence.
    934 		 */
    935 		pass = spa_sync_pass(spa);
    936 
    937 		ASSERT(zio->io_txg == spa_syncing_txg(spa));
    938 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
    939 		ASSERT(!BP_GET_DEDUP(bp));
    940 
    941 		if (pass > SYNC_PASS_DONT_COMPRESS)
    942 			compress = ZIO_COMPRESS_OFF;
    943 
    944 		/* Make sure someone doesn't change their mind on overwrites */
    945 		ASSERT(MIN(zp->zp_copies + BP_IS_GANG(bp),
    946 		    spa_max_replication(spa)) == BP_GET_NDVAS(bp));
    947 	}
    948 
    949 	if (compress != ZIO_COMPRESS_OFF) {
    950 		void *cbuf = zio_buf_alloc(lsize);
    951 		psize = zio_compress_data(compress, zio->io_data, cbuf, lsize);
    952 		if (psize == 0 || psize == lsize) {
    953 			compress = ZIO_COMPRESS_OFF;
    954 			zio_buf_free(cbuf, lsize);
    955 		} else {
    956 			ASSERT(psize < lsize);
    957 			zio_push_transform(zio, cbuf, psize, lsize, NULL);
    958 		}
    959 	}
    960 
    961 	/*
    962 	 * The final pass of spa_sync() must be all rewrites, but the first
    963 	 * few passes offer a trade-off: allocating blocks defers convergence,
    964 	 * but newly allocated blocks are sequential, so they can be written
    965 	 * to disk faster.  Therefore, we allow the first few passes of
    966 	 * spa_sync() to allocate new blocks, but force rewrites after that.
    967 	 * There should only be a handful of blocks after pass 1 in any case.
    968 	 */
    969 	if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == psize &&
    970 	    pass > SYNC_PASS_REWRITE) {
    971 		ASSERT(psize != 0);
    972 		enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
    973 		zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
    974 		zio->io_flags |= ZIO_FLAG_IO_REWRITE;
    975 	} else {
    976 		BP_ZERO(bp);
    977 		zio->io_pipeline = ZIO_WRITE_PIPELINE;
    978 	}
    979 
    980 	if (psize == 0) {
    981 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
    982 	} else {
    983 		ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
    984 		BP_SET_LSIZE(bp, lsize);
    985 		BP_SET_PSIZE(bp, psize);
    986 		BP_SET_COMPRESS(bp, compress);
    987 		BP_SET_CHECKSUM(bp, zp->zp_checksum);
    988 		BP_SET_TYPE(bp, zp->zp_type);
    989 		BP_SET_LEVEL(bp, zp->zp_level);
    990 		BP_SET_DEDUP(bp, zp->zp_dedup);
    991 		BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
    992 		if (zp->zp_dedup) {
    993 			ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
    994 			ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
    995 			zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
    996 		}
    997 	}
    998 
    999 	return (ZIO_PIPELINE_CONTINUE);
   1000 }
   1001 
   1002 static int
   1003 zio_free_bp_init(zio_t *zio)
   1004 {
   1005 	blkptr_t *bp = zio->io_bp;
   1006 
   1007 	if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
   1008 		if (BP_GET_DEDUP(bp))
   1009 			zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
   1010 		else
   1011 			arc_free(zio->io_spa, bp);
   1012 	}
   1013 
   1014 	return (ZIO_PIPELINE_CONTINUE);
   1015 }
   1016 
   1017 /*
   1018  * ==========================================================================
   1019  * Execute the I/O pipeline
   1020  * ==========================================================================
   1021  */
   1022 
   1023 static void
   1024 zio_taskq_dispatch(zio_t *zio, enum zio_taskq_type q)
   1025 {
   1026 	zio_type_t t = zio->io_type;
   1027 
   1028 	/*
   1029 	 * If we're a config writer or a probe, the normal issue and
   1030 	 * interrupt threads may all be blocked waiting for the config lock.
   1031 	 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
   1032 	 */
   1033 	if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
   1034 		t = ZIO_TYPE_NULL;
   1035 
   1036 	/*
   1037 	 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
   1038 	 */
   1039 	if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
   1040 		t = ZIO_TYPE_NULL;
   1041 
   1042 	(void) taskq_dispatch(zio->io_spa->spa_zio_taskq[t][q],
   1043 	    (task_func_t *)zio_execute, zio, TQ_SLEEP);
   1044 }
   1045 
   1046 static boolean_t
   1047 zio_taskq_member(zio_t *zio, enum zio_taskq_type q)
   1048 {
   1049 	kthread_t *executor = zio->io_executor;
   1050 	spa_t *spa = zio->io_spa;
   1051 
   1052 	for (zio_type_t t = 0; t < ZIO_TYPES; t++)
   1053 		if (taskq_member(spa->spa_zio_taskq[t][q], executor))
   1054 			return (B_TRUE);
   1055 
   1056 	return (B_FALSE);
   1057 }
   1058 
   1059 static int
   1060 zio_issue_async(zio_t *zio)
   1061 {
   1062 	zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE);
   1063 
   1064 	return (ZIO_PIPELINE_STOP);
   1065 }
   1066 
   1067 void
   1068 zio_interrupt(zio_t *zio)
   1069 {
   1070 	zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT);
   1071 }
   1072 
   1073 /*
   1074  * Execute the I/O pipeline until one of the following occurs:
   1075  * (1) the I/O completes; (2) the pipeline stalls waiting for
   1076  * dependent child I/Os; (3) the I/O issues, so we're waiting
   1077  * for an I/O completion interrupt; (4) the I/O is delegated by
   1078  * vdev-level caching or aggregation; (5) the I/O is deferred
   1079  * due to vdev-level queueing; (6) the I/O is handed off to
   1080  * another thread.  In all cases, the pipeline stops whenever
   1081  * there's no CPU work; it never burns a thread in cv_wait().
   1082  *
   1083  * There's no locking on io_stage because there's no legitimate way
   1084  * for multiple threads to be attempting to process the same I/O.
   1085  */
   1086 static zio_pipe_stage_t *zio_pipeline[];
   1087 
   1088 void
   1089 zio_execute(zio_t *zio)
   1090 {
   1091 	zio->io_executor = curthread;
   1092 
   1093 	while (zio->io_stage < ZIO_STAGE_DONE) {
   1094 		enum zio_stage pipeline = zio->io_pipeline;
   1095 		enum zio_stage stage = zio->io_stage;
   1096 		int rv;
   1097 
   1098 		ASSERT(!MUTEX_HELD(&zio->io_lock));
   1099 		ASSERT(ISP2(stage));
   1100 		ASSERT(zio->io_stall == NULL);
   1101 
   1102 		do {
   1103 			stage <<= 1;
   1104 		} while ((stage & pipeline) == 0);
   1105 
   1106 		ASSERT(stage <= ZIO_STAGE_DONE);
   1107 
   1108 		/*
   1109 		 * If we are in interrupt context and this pipeline stage
   1110 		 * will grab a config lock that is held across I/O,
   1111 		 * or may wait for an I/O that needs an interrupt thread
   1112 		 * to complete, issue async to avoid deadlock.
   1113 		 */
   1114 		if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
   1115 		    zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
   1116 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE);
   1117 			return;
   1118 		}
   1119 
   1120 		zio->io_stage = stage;
   1121 		rv = zio_pipeline[highbit(stage) - 1](zio);
   1122 
   1123 		if (rv == ZIO_PIPELINE_STOP)
   1124 			return;
   1125 
   1126 		ASSERT(rv == ZIO_PIPELINE_CONTINUE);
   1127 	}
   1128 }
   1129 
   1130 /*
   1131  * ==========================================================================
   1132  * Initiate I/O, either sync or async
   1133  * ==========================================================================
   1134  */
   1135 int
   1136 zio_wait(zio_t *zio)
   1137 {
   1138 	int error;
   1139 
   1140 	ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
   1141 	ASSERT(zio->io_executor == NULL);
   1142 
   1143 	zio->io_waiter = curthread;
   1144 
   1145 	zio_execute(zio);
   1146 
   1147 	mutex_enter(&zio->io_lock);
   1148 	while (zio->io_executor != NULL)
   1149 		cv_wait(&zio->io_cv, &zio->io_lock);
   1150 	mutex_exit(&zio->io_lock);
   1151 
   1152 	error = zio->io_error;
   1153 	zio_destroy(zio);
   1154 
   1155 	return (error);
   1156 }
   1157 
   1158 void
   1159 zio_nowait(zio_t *zio)
   1160 {
   1161 	ASSERT(zio->io_executor == NULL);
   1162 
   1163 	if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
   1164 	    zio_unique_parent(zio) == NULL) {
   1165 		/*
   1166 		 * This is a logical async I/O with no parent to wait for it.
   1167 		 * We add it to the spa_async_root_zio "Godfather" I/O which
   1168 		 * will ensure they complete prior to unloading the pool.
   1169 		 */
   1170 		spa_t *spa = zio->io_spa;
   1171 
   1172 		zio_add_child(spa->spa_async_zio_root, zio);
   1173 	}
   1174 
   1175 	zio_execute(zio);
   1176 }
   1177 
   1178 /*
   1179  * ==========================================================================
   1180  * Reexecute or suspend/resume failed I/O
   1181  * ==========================================================================
   1182  */
   1183 
   1184 static void
   1185 zio_reexecute(zio_t *pio)
   1186 {
   1187 	zio_t *cio, *cio_next;
   1188 
   1189 	ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
   1190 	ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
   1191 	ASSERT(pio->io_gang_leader == NULL);
   1192 	ASSERT(pio->io_gang_tree == NULL);
   1193 
   1194 	pio->io_flags = pio->io_orig_flags;
   1195 	pio->io_stage = pio->io_orig_stage;
   1196 	pio->io_pipeline = pio->io_orig_pipeline;
   1197 	pio->io_reexecute = 0;
   1198 	pio->io_error = 0;
   1199 	for (int w = 0; w < ZIO_WAIT_TYPES; w++)
   1200 		pio->io_state[w] = 0;
   1201 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
   1202 		pio->io_child_error[c] = 0;
   1203 
   1204 	if (IO_IS_ALLOCATING(pio))
   1205 		BP_ZERO(pio->io_bp);
   1206 
   1207 	/*
   1208 	 * As we reexecute pio's children, new children could be created.
   1209 	 * New children go to the head of pio's io_child_list, however,
   1210 	 * so we will (correctly) not reexecute them.  The key is that
   1211 	 * the remainder of pio's io_child_list, from 'cio_next' onward,
   1212 	 * cannot be affected by any side effects of reexecuting 'cio'.
   1213 	 */
   1214 	for (cio = zio_walk_children(pio); cio != NULL; cio = cio_next) {
   1215 		cio_next = zio_walk_children(pio);
   1216 		mutex_enter(&pio->io_lock);
   1217 		for (int w = 0; w < ZIO_WAIT_TYPES; w++)
   1218 			pio->io_children[cio->io_child_type][w]++;
   1219 		mutex_exit(&pio->io_lock);
   1220 		zio_reexecute(cio);
   1221 	}
   1222 
   1223 	/*
   1224 	 * Now that all children have been reexecuted, execute the parent.
   1225 	 * We don't reexecute "The Godfather" I/O here as it's the
   1226 	 * responsibility of the caller to wait on him.
   1227 	 */
   1228 	if (!(pio->io_flags & ZIO_FLAG_GODFATHER))
   1229 		zio_execute(pio);
   1230 }
   1231 
   1232 void
   1233 zio_suspend(spa_t *spa, zio_t *zio)
   1234 {
   1235 	if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
   1236 		fm_panic("Pool '%s' has encountered an uncorrectable I/O "
   1237 		    "failure and the failure mode property for this pool "
   1238 		    "is set to panic.", spa_name(spa));
   1239 
   1240 	zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
   1241 
   1242 	mutex_enter(&spa->spa_suspend_lock);
   1243 
   1244 	if (spa->spa_suspend_zio_root == NULL)
   1245 		spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
   1246 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
   1247 		    ZIO_FLAG_GODFATHER);
   1248 
   1249 	spa->spa_suspended = B_TRUE;
   1250 
   1251 	if (zio != NULL) {
   1252 		ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
   1253 		ASSERT(zio != spa->spa_suspend_zio_root);
   1254 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
   1255 		ASSERT(zio_unique_parent(zio) == NULL);
   1256 		ASSERT(zio->io_stage == ZIO_STAGE_DONE);
   1257 		zio_add_child(spa->spa_suspend_zio_root, zio);
   1258 	}
   1259 
   1260 	mutex_exit(&spa->spa_suspend_lock);
   1261 }
   1262 
   1263 int
   1264 zio_resume(spa_t *spa)
   1265 {
   1266 	zio_t *pio;
   1267 
   1268 	/*
   1269 	 * Reexecute all previously suspended i/o.
   1270 	 */
   1271 	mutex_enter(&spa->spa_suspend_lock);
   1272 	spa->spa_suspended = B_FALSE;
   1273 	cv_broadcast(&spa->spa_suspend_cv);
   1274 	pio = spa->spa_suspend_zio_root;
   1275 	spa->spa_suspend_zio_root = NULL;
   1276 	mutex_exit(&spa->spa_suspend_lock);
   1277 
   1278 	if (pio == NULL)
   1279 		return (0);
   1280 
   1281 	zio_reexecute(pio);
   1282 	return (zio_wait(pio));
   1283 }
   1284 
   1285 void
   1286 zio_resume_wait(spa_t *spa)
   1287 {
   1288 	mutex_enter(&spa->spa_suspend_lock);
   1289 	while (spa_suspended(spa))
   1290 		cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
   1291 	mutex_exit(&spa->spa_suspend_lock);
   1292 }
   1293 
   1294 /*
   1295  * ==========================================================================
   1296  * Gang blocks.
   1297  *
   1298  * A gang block is a collection of small blocks that looks to the DMU
   1299  * like one large block.  When zio_dva_allocate() cannot find a block
   1300  * of the requested size, due to either severe fragmentation or the pool
   1301  * being nearly full, it calls zio_write_gang_block() to construct the
   1302  * block from smaller fragments.
   1303  *
   1304  * A gang block consists of a gang header (zio_gbh_phys_t) and up to
   1305  * three (SPA_GBH_NBLKPTRS) gang members.  The gang header is just like
   1306  * an indirect block: it's an array of block pointers.  It consumes
   1307  * only one sector and hence is allocatable regardless of fragmentation.
   1308  * The gang header's bps point to its gang members, which hold the data.
   1309  *
   1310  * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
   1311  * as the verifier to ensure uniqueness of the SHA256 checksum.
   1312  * Critically, the gang block bp's blk_cksum is the checksum of the data,
   1313  * not the gang header.  This ensures that data block signatures (needed for
   1314  * deduplication) are independent of how the block is physically stored.
   1315  *
   1316  * Gang blocks can be nested: a gang member may itself be a gang block.
   1317  * Thus every gang block is a tree in which root and all interior nodes are
   1318  * gang headers, and the leaves are normal blocks that contain user data.
   1319  * The root of the gang tree is called the gang leader.
   1320  *
   1321  * To perform any operation (read, rewrite, free, claim) on a gang block,
   1322  * zio_gang_assemble() first assembles the gang tree (minus data leaves)
   1323  * in the io_gang_tree field of the original logical i/o by recursively
   1324  * reading the gang leader and all gang headers below it.  This yields
   1325  * an in-core tree containing the contents of every gang header and the
   1326  * bps for every constituent of the gang block.
   1327  *
   1328  * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
   1329  * and invokes a callback on each bp.  To free a gang block, zio_gang_issue()
   1330  * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
   1331  * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
   1332  * zio_read_gang() is a wrapper around zio_read() that omits reading gang
   1333  * headers, since we already have those in io_gang_tree.  zio_rewrite_gang()
   1334  * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
   1335  * of the gang header plus zio_checksum_compute() of the data to update the
   1336  * gang header's blk_cksum as described above.
   1337  *
   1338  * The two-phase assemble/issue model solves the problem of partial failure --
   1339  * what if you'd freed part of a gang block but then couldn't read the
   1340  * gang header for another part?  Assembling the entire gang tree first
   1341  * ensures that all the necessary gang header I/O has succeeded before
   1342  * starting the actual work of free, claim, or write.  Once the gang tree
   1343  * is assembled, free and claim are in-memory operations that cannot fail.
   1344  *
   1345  * In the event that a gang write fails, zio_dva_unallocate() walks the
   1346  * gang tree to immediately free (i.e. insert back into the space map)
   1347  * everything we've allocated.  This ensures that we don't get ENOSPC
   1348  * errors during repeated suspend/resume cycles due to a flaky device.
   1349  *
   1350  * Gang rewrites only happen during sync-to-convergence.  If we can't assemble
   1351  * the gang tree, we won't modify the block, so we can safely defer the free
   1352  * (knowing that the block is still intact).  If we *can* assemble the gang
   1353  * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
   1354  * each constituent bp and we can allocate a new block on the next sync pass.
   1355  *
   1356  * In all cases, the gang tree allows complete recovery from partial failure.
   1357  * ==========================================================================
   1358  */
   1359 
   1360 static zio_t *
   1361 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
   1362 {
   1363 	if (gn != NULL)
   1364 		return (pio);
   1365 
   1366 	return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp),
   1367 	    NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
   1368 	    &pio->io_bookmark));
   1369 }
   1370 
   1371 zio_t *
   1372 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
   1373 {
   1374 	zio_t *zio;
   1375 
   1376 	if (gn != NULL) {
   1377 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
   1378 		    gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority,
   1379 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
   1380 		/*
   1381 		 * As we rewrite each gang header, the pipeline will compute
   1382 		 * a new gang block header checksum for it; but no one will
   1383 		 * compute a new data checksum, so we do that here.  The one
   1384 		 * exception is the gang leader: the pipeline already computed
   1385 		 * its data checksum because that stage precedes gang assembly.
   1386 		 * (Presently, nothing actually uses interior data checksums;
   1387 		 * this is just good hygiene.)
   1388 		 */
   1389 		if (gn != pio->io_gang_leader->io_gang_tree) {
   1390 			zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
   1391 			    data, BP_GET_PSIZE(bp));
   1392 		}
   1393 		/*
   1394 		 * If we are here to damage data for testing purposes,
   1395 		 * leave the GBH alone so that we can detect the damage.
   1396 		 */
   1397 		if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
   1398 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
   1399 	} else {
   1400 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
   1401 		    data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority,
   1402 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
   1403 	}
   1404 
   1405 	return (zio);
   1406 }
   1407 
   1408 /* ARGSUSED */
   1409 zio_t *
   1410 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
   1411 {
   1412 	return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
   1413 	    ZIO_GANG_CHILD_FLAGS(pio)));
   1414 }
   1415 
   1416 /* ARGSUSED */
   1417 zio_t *
   1418 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
   1419 {
   1420 	return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
   1421 	    NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
   1422 }
   1423 
   1424 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
   1425 	NULL,
   1426 	zio_read_gang,
   1427 	zio_rewrite_gang,
   1428 	zio_free_gang,
   1429 	zio_claim_gang,
   1430 	NULL
   1431 };
   1432 
   1433 static void zio_gang_tree_assemble_done(zio_t *zio);
   1434 
   1435 static zio_gang_node_t *
   1436 zio_gang_node_alloc(zio_gang_node_t **gnpp)
   1437 {
   1438 	zio_gang_node_t *gn;
   1439 
   1440 	ASSERT(*gnpp == NULL);
   1441 
   1442 	gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
   1443 	gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
   1444 	*gnpp = gn;
   1445 
   1446 	return (gn);
   1447 }
   1448 
   1449 static void
   1450 zio_gang_node_free(zio_gang_node_t **gnpp)
   1451 {
   1452 	zio_gang_node_t *gn = *gnpp;
   1453 
   1454 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
   1455 		ASSERT(gn->gn_child[g] == NULL);
   1456 
   1457 	zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
   1458 	kmem_free(gn, sizeof (*gn));
   1459 	*gnpp = NULL;
   1460 }
   1461 
   1462 static void
   1463 zio_gang_tree_free(zio_gang_node_t **gnpp)
   1464 {
   1465 	zio_gang_node_t *gn = *gnpp;
   1466 
   1467 	if (gn == NULL)
   1468 		return;
   1469 
   1470 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
   1471 		zio_gang_tree_free(&gn->gn_child[g]);
   1472 
   1473 	zio_gang_node_free(gnpp);
   1474 }
   1475 
   1476 static void
   1477 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
   1478 {
   1479 	zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
   1480 
   1481 	ASSERT(gio->io_gang_leader == gio);
   1482 	ASSERT(BP_IS_GANG(bp));
   1483 
   1484 	zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh,
   1485 	    SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn,
   1486 	    gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
   1487 }
   1488 
   1489 static void
   1490 zio_gang_tree_assemble_done(zio_t *zio)
   1491 {
   1492 	zio_t *gio = zio->io_gang_leader;
   1493 	zio_gang_node_t *gn = zio->io_private;
   1494 	blkptr_t *bp = zio->io_bp;
   1495 
   1496 	ASSERT(gio == zio_unique_parent(zio));
   1497 	ASSERT(zio->io_child_count == 0);
   1498 
   1499 	if (zio->io_error)
   1500 		return;
   1501 
   1502 	if (BP_SHOULD_BYTESWAP(bp))
   1503 		byteswap_uint64_array(zio->io_data, zio->io_size);
   1504 
   1505 	ASSERT(zio->io_data == gn->gn_gbh);
   1506 	ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
   1507 	ASSERT(gn->gn_gbh->zg_tail.zbt_magic == ZBT_MAGIC);
   1508 
   1509 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
   1510 		blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
   1511 		if (!BP_IS_GANG(gbp))
   1512 			continue;
   1513 		zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
   1514 	}
   1515 }
   1516 
   1517 static void
   1518 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data)
   1519 {
   1520 	zio_t *gio = pio->io_gang_leader;
   1521 	zio_t *zio;
   1522 
   1523 	ASSERT(BP_IS_GANG(bp) == !!gn);
   1524 	ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
   1525 	ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
   1526 
   1527 	/*
   1528 	 * If you're a gang header, your data is in gn->gn_gbh.
   1529 	 * If you're a gang member, your data is in 'data' and gn == NULL.
   1530 	 */
   1531 	zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data);
   1532 
   1533 	if (gn != NULL) {
   1534 		ASSERT(gn->gn_gbh->zg_tail.zbt_magic == ZBT_MAGIC);
   1535 
   1536 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
   1537 			blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
   1538 			if (BP_IS_HOLE(gbp))
   1539 				continue;
   1540 			zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data);
   1541 			data = (char *)data + BP_GET_PSIZE(gbp);
   1542 		}
   1543 	}
   1544 
   1545 	if (gn == gio->io_gang_tree)
   1546 		ASSERT3P((char *)gio->io_data + gio->io_size, ==, data);
   1547 
   1548 	if (zio != pio)
   1549 		zio_nowait(zio);
   1550 }
   1551 
   1552 static int
   1553 zio_gang_assemble(zio_t *zio)
   1554 {
   1555 	blkptr_t *bp = zio->io_bp;
   1556 
   1557 	ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
   1558 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
   1559 
   1560 	zio->io_gang_leader = zio;
   1561 
   1562 	zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
   1563 
   1564 	return (ZIO_PIPELINE_CONTINUE);
   1565 }
   1566 
   1567 static int
   1568 zio_gang_issue(zio_t *zio)
   1569 {
   1570 	blkptr_t *bp = zio->io_bp;
   1571 
   1572 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
   1573 		return (ZIO_PIPELINE_STOP);
   1574 
   1575 	ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
   1576 	ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
   1577 
   1578 	if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
   1579 		zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data);
   1580 	else
   1581 		zio_gang_tree_free(&zio->io_gang_tree);
   1582 
   1583 	zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
   1584 
   1585 	return (ZIO_PIPELINE_CONTINUE);
   1586 }
   1587 
   1588 static void
   1589 zio_write_gang_member_ready(zio_t *zio)
   1590 {
   1591 	zio_t *pio = zio_unique_parent(zio);
   1592 	zio_t *gio = zio->io_gang_leader;
   1593 	dva_t *cdva = zio->io_bp->blk_dva;
   1594 	dva_t *pdva = pio->io_bp->blk_dva;
   1595 	uint64_t asize;
   1596 
   1597 	if (BP_IS_HOLE(zio->io_bp))
   1598 		return;
   1599 
   1600 	ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
   1601 
   1602 	ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
   1603 	ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
   1604 	ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
   1605 	ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
   1606 	ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
   1607 
   1608 	mutex_enter(&pio->io_lock);
   1609 	for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
   1610 		ASSERT(DVA_GET_GANG(&pdva[d]));
   1611 		asize = DVA_GET_ASIZE(&pdva[d]);
   1612 		asize += DVA_GET_ASIZE(&cdva[d]);
   1613 		DVA_SET_ASIZE(&pdva[d], asize);
   1614 	}
   1615 	mutex_exit(&pio->io_lock);
   1616 }
   1617 
   1618 static int
   1619 zio_write_gang_block(zio_t *pio)
   1620 {
   1621 	spa_t *spa = pio->io_spa;
   1622 	blkptr_t *bp = pio->io_bp;
   1623 	zio_t *gio = pio->io_gang_leader;
   1624 	zio_t *zio;
   1625 	zio_gang_node_t *gn, **gnpp;
   1626 	zio_gbh_phys_t *gbh;
   1627 	uint64_t txg = pio->io_txg;
   1628 	uint64_t resid = pio->io_size;
   1629 	uint64_t lsize;
   1630 	int copies = gio->io_prop.zp_copies;
   1631 	int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
   1632 	zio_prop_t zp;
   1633 	int error;
   1634 
   1635 	error = metaslab_alloc(spa, spa_normal_class(spa), SPA_GANGBLOCKSIZE,
   1636 	    bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp,
   1637 	    METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER);
   1638 	if (error) {
   1639 		pio->io_error = error;
   1640 		return (ZIO_PIPELINE_CONTINUE);
   1641 	}
   1642 
   1643 	if (pio == gio) {
   1644 		gnpp = &gio->io_gang_tree;
   1645 	} else {
   1646 		gnpp = pio->io_private;
   1647 		ASSERT(pio->io_ready == zio_write_gang_member_ready);
   1648 	}
   1649 
   1650 	gn = zio_gang_node_alloc(gnpp);
   1651 	gbh = gn->gn_gbh;
   1652 	bzero(gbh, SPA_GANGBLOCKSIZE);
   1653 
   1654 	/*
   1655 	 * Create the gang header.
   1656 	 */
   1657 	zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL,
   1658 	    pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
   1659 
   1660 	/*
   1661 	 * Create and nowait the gang children.
   1662 	 */
   1663 	for (int g = 0; resid != 0; resid -= lsize, g++) {
   1664 		lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
   1665 		    SPA_MINBLOCKSIZE);
   1666 		ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
   1667 
   1668 		zp.zp_checksum = gio->io_prop.zp_checksum;
   1669 		zp.zp_compress = ZIO_COMPRESS_OFF;
   1670 		zp.zp_type = DMU_OT_NONE;
   1671 		zp.zp_level = 0;
   1672 		zp.zp_copies = gio->io_prop.zp_copies;
   1673 		zp.zp_dedup = 0;
   1674 		zp.zp_dedup_verify = 0;
   1675 
   1676 		zio_nowait(zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
   1677 		    (char *)pio->io_data + (pio->io_size - resid), lsize, &zp,
   1678 		    zio_write_gang_member_ready, NULL, &gn->gn_child[g],
   1679 		    pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
   1680 		    &pio->io_bookmark));
   1681 	}
   1682 
   1683 	/*
   1684 	 * Set pio's pipeline to just wait for zio to finish.
   1685 	 */
   1686 	pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
   1687 
   1688 	zio_nowait(zio);
   1689 
   1690 	return (ZIO_PIPELINE_CONTINUE);
   1691 }
   1692 
   1693 /*
   1694  * ==========================================================================
   1695  * Dedup
   1696  * ==========================================================================
   1697  */
   1698 static void
   1699 zio_ddt_child_read_done(zio_t *zio)
   1700 {
   1701 	blkptr_t *bp = zio->io_bp;
   1702 	ddt_entry_t *dde = zio->io_private;
   1703 	ddt_phys_t *ddp;
   1704 	zio_t *pio = zio_unique_parent(zio);
   1705 
   1706 	mutex_enter(&pio->io_lock);
   1707 	ddp = ddt_phys_select(dde, bp);
   1708 	if (zio->io_error == 0)
   1709 		ddt_phys_clear(ddp);	/* this ddp doesn't need repair */
   1710 	if (zio->io_error == 0 && dde->dde_repair_data == NULL)
   1711 		dde->dde_repair_data = zio->io_data;
   1712 	else
   1713 		zio_buf_free(zio->io_data, zio->io_size);
   1714 	mutex_exit(&pio->io_lock);
   1715 }
   1716 
   1717 static int
   1718 zio_ddt_read_start(zio_t *zio)
   1719 {
   1720 	blkptr_t *bp = zio->io_bp;
   1721 
   1722 	ASSERT(BP_GET_DEDUP(bp));
   1723 	ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
   1724 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
   1725 
   1726 	if (zio->io_child_error[ZIO_CHILD_DDT]) {
   1727 		ddt_t *ddt = ddt_select(zio->io_spa, bp);
   1728 		ddt_entry_t *dde = ddt_repair_start(ddt, bp);
   1729 		ddt_phys_t *ddp = dde->dde_phys;
   1730 		ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
   1731 		blkptr_t blk;
   1732 
   1733 		ASSERT(zio->io_vsd == NULL);
   1734 		zio->io_vsd = dde;
   1735 
   1736 		if (ddp_self == NULL)
   1737 			return (ZIO_PIPELINE_CONTINUE);
   1738 
   1739 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
   1740 			if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
   1741 				continue;
   1742 			ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
   1743 			    &blk);
   1744 			zio_nowait(zio_read(zio, zio->io_spa, &blk,
   1745 			    zio_buf_alloc(zio->io_size), zio->io_size,
   1746 			    zio_ddt_child_read_done, dde, zio->io_priority,
   1747 			    ZIO_DDT_CHILD_FLAGS(zio) | ZIO_FLAG_DONT_PROPAGATE,
   1748 			    &zio->io_bookmark));
   1749 		}
   1750 		return (ZIO_PIPELINE_CONTINUE);
   1751 	}
   1752 
   1753 	zio_nowait(zio_read(zio, zio->io_spa, bp,
   1754 	    zio->io_data, zio->io_size, NULL, NULL, zio->io_priority,
   1755 	    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
   1756 
   1757 	return (ZIO_PIPELINE_CONTINUE);
   1758 }
   1759 
   1760 static int
   1761 zio_ddt_read_done(zio_t *zio)
   1762 {
   1763 	blkptr_t *bp = zio->io_bp;
   1764 
   1765 	if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE))
   1766 		return (ZIO_PIPELINE_STOP);
   1767 
   1768 	ASSERT(BP_GET_DEDUP(bp));
   1769 	ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
   1770 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
   1771 
   1772 	if (zio->io_child_error[ZIO_CHILD_DDT]) {
   1773 		ddt_t *ddt = ddt_select(zio->io_spa, bp);
   1774 		ddt_entry_t *dde = zio->io_vsd;
   1775 		if (ddt == NULL) {
   1776 			ASSERT(zio->io_spa->spa_load_state != SPA_LOAD_NONE);
   1777 			return (ZIO_PIPELINE_CONTINUE);
   1778 		}
   1779 		if (dde == NULL) {
   1780 			zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
   1781 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE);
   1782 			return (ZIO_PIPELINE_STOP);
   1783 		}
   1784 		if (dde->dde_repair_data != NULL) {
   1785 			bcopy(dde->dde_repair_data, zio->io_data, zio->io_size);
   1786 			zio->io_child_error[ZIO_CHILD_DDT] = 0;
   1787 		}
   1788 		ddt_repair_done(ddt, dde);
   1789 		zio->io_vsd = NULL;
   1790 	}
   1791 
   1792 	ASSERT(zio->io_vsd == NULL);
   1793 
   1794 	return (ZIO_PIPELINE_CONTINUE);
   1795 }
   1796 
   1797 static boolean_t
   1798 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
   1799 {
   1800 	spa_t *spa = zio->io_spa;
   1801 
   1802 	/*
   1803 	 * Note: we compare the original data, not the transformed data,
   1804 	 * because when zio->io_bp is an override bp, we will not have
   1805 	 * pushed the I/O transforms.  That's an important optimization
   1806 	 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
   1807 	 */
   1808 	for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
   1809 		zio_t *lio = dde->dde_lead_zio[p];
   1810 
   1811 		if (lio != NULL) {
   1812 			return (lio->io_orig_size != zio->io_orig_size ||
   1813 			    bcmp(zio->io_orig_data, lio->io_orig_data,
   1814 			    zio->io_orig_size) != 0);
   1815 		}
   1816 	}
   1817 
   1818 	for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
   1819 		ddt_phys_t *ddp = &dde->dde_phys[p];
   1820 
   1821 		if (ddp->ddp_phys_birth != 0) {
   1822 			arc_buf_t *abuf = NULL;
   1823 			uint32_t aflags = ARC_WAIT;
   1824 			blkptr_t blk = *zio->io_bp;
   1825 			int error;
   1826 
   1827 			ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
   1828 
   1829 			ddt_exit(ddt);
   1830 
   1831 			error = arc_read_nolock(NULL, spa, &blk,
   1832 			    arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
   1833 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
   1834 			    &aflags, &zio->io_bookmark);
   1835 
   1836 			if (error == 0) {
   1837 				if (arc_buf_size(abuf) != zio->io_orig_size ||
   1838 				    bcmp(abuf->b_data, zio->io_orig_data,
   1839 				    zio->io_orig_size) != 0)
   1840 					error = EEXIST;
   1841 				VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
   1842 			}
   1843 
   1844 			ddt_enter(ddt);
   1845 			return (error != 0);
   1846 		}
   1847 	}
   1848 
   1849 	return (B_FALSE);
   1850 }
   1851 
   1852 static void
   1853 zio_ddt_child_write_ready(zio_t *zio)
   1854 {
   1855 	int p = zio->io_prop.zp_copies;
   1856 	ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
   1857 	ddt_entry_t *dde = zio->io_private;
   1858 	ddt_phys_t *ddp = &dde->dde_phys[p];
   1859 	zio_t *pio;
   1860 
   1861 	if (zio->io_error)
   1862 		return;
   1863 
   1864 	ddt_enter(ddt);
   1865 
   1866 	ASSERT(dde->dde_lead_zio[p] == zio);
   1867 
   1868 	ddt_phys_fill(ddp, zio->io_bp);
   1869 
   1870 	while ((pio = zio_walk_parents(zio)) != NULL)
   1871 		ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
   1872 
   1873 	ddt_exit(ddt);
   1874 }
   1875 
   1876 static void
   1877 zio_ddt_child_write_done(zio_t *zio)
   1878 {
   1879 	int p = zio->io_prop.zp_copies;
   1880 	ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
   1881 	ddt_entry_t *dde = zio->io_private;
   1882 	ddt_phys_t *ddp = &dde->dde_phys[p];
   1883 
   1884 	ddt_enter(ddt);
   1885 
   1886 	ASSERT(ddp->ddp_refcnt == 0);
   1887 	ASSERT(dde->dde_lead_zio[p] == zio);
   1888 	dde->dde_lead_zio[p] = NULL;
   1889 
   1890 	if (zio->io_error == 0) {
   1891 		while (zio_walk_parents(zio) != NULL)
   1892 			ddt_phys_addref(ddp);
   1893 	} else {
   1894 		ddt_phys_clear(ddp);
   1895 	}
   1896 
   1897 	ddt_exit(ddt);
   1898 }
   1899 
   1900 static void
   1901 zio_ddt_ditto_write_done(zio_t *zio)
   1902 {
   1903 	int p = DDT_PHYS_DITTO;
   1904 	zio_prop_t *zp = &zio->io_prop;
   1905 	blkptr_t *bp = zio->io_bp;
   1906 	ddt_t *ddt = ddt_select(zio->io_spa, bp);
   1907 	ddt_entry_t *dde = zio->io_private;
   1908 	ddt_phys_t *ddp = &dde->dde_phys[p];
   1909 	ddt_key_t *ddk = &dde->dde_key;
   1910 
   1911 	ddt_enter(ddt);
   1912 
   1913 	ASSERT(ddp->ddp_refcnt == 0);
   1914 	ASSERT(dde->dde_lead_zio[p] == zio);
   1915 	dde->dde_lead_zio[p] = NULL;
   1916 
   1917 	if (zio->io_error == 0) {
   1918 		ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
   1919 		ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
   1920 		ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
   1921 		if (ddp->ddp_phys_birth != 0)
   1922 			ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
   1923 		ddt_phys_fill(ddp, bp);
   1924 	}
   1925 
   1926 	ddt_exit(ddt);
   1927 }
   1928 
   1929 static int
   1930 zio_ddt_write(zio_t *zio)
   1931 {
   1932 	spa_t *spa = zio->io_spa;
   1933 	blkptr_t *bp = zio->io_bp;
   1934 	uint64_t txg = zio->io_txg;
   1935 	zio_prop_t *zp = &zio->io_prop;
   1936 	int p = zp->zp_copies;
   1937 	int ditto_copies;
   1938 	zio_t *cio = NULL;
   1939 	zio_t *dio = NULL;
   1940 	ddt_t *ddt = ddt_select(spa, bp);
   1941 	ddt_entry_t *dde;
   1942 	ddt_phys_t *ddp;
   1943 
   1944 	ASSERT(BP_GET_DEDUP(bp));
   1945 	ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
   1946 	ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
   1947 
   1948 	ddt_enter(ddt);
   1949 	dde = ddt_lookup(ddt, bp, B_TRUE);
   1950 	ddp = &dde->dde_phys[p];
   1951 
   1952 	if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
   1953 		/*
   1954 		 * If we're using a weak checksum, upgrade to a strong checksum
   1955 		 * and try again.  If we're already using a strong checksum,
   1956 		 * we can't resolve it, so just convert to an ordinary write.
   1957 		 * (And automatically e-mail a paper to Nature?)
   1958 		 */
   1959 		if (!zio_checksum_table[zp->zp_checksum].ci_dedup) {
   1960 			zp->zp_checksum = spa_dedup_checksum(spa);
   1961 			zio_pop_transforms(zio);
   1962 			zio->io_stage = ZIO_STAGE_OPEN;
   1963 			BP_ZERO(bp);
   1964 		} else {
   1965 			zp->zp_dedup = 0;
   1966 		}
   1967 		zio->io_pipeline = ZIO_WRITE_PIPELINE;
   1968 		ddt_exit(ddt);
   1969 		return (ZIO_PIPELINE_CONTINUE);
   1970 	}
   1971 
   1972 	ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
   1973 	ASSERT(ditto_copies < SPA_DVAS_PER_BP);
   1974 
   1975 	if (ditto_copies > ddt_ditto_copies_present(dde) &&
   1976 	    dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
   1977 		zio_prop_t czp = *zp;
   1978 
   1979 		czp.zp_copies = ditto_copies;
   1980 
   1981 		/*
   1982 		 * If we arrived here with an override bp, we won't have run
   1983 		 * the transform stack, so we won't have the data we need to
   1984 		 * generate a child i/o.  So, toss the override bp and restart.
   1985 		 * This is safe, because using the override bp is just an
   1986 		 * optimization; and it's rare, so the cost doesn't matter.
   1987 		 */
   1988 		if (zio->io_bp_override) {
   1989 			zio_pop_transforms(zio);
   1990 			zio->io_stage = ZIO_STAGE_OPEN;
   1991 			zio->io_pipeline = ZIO_WRITE_PIPELINE;
   1992 			zio->io_bp_override = NULL;
   1993 			BP_ZERO(bp);
   1994 			ddt_exit(ddt);
   1995 			return (ZIO_PIPELINE_CONTINUE);
   1996 		}
   1997 
   1998 		dio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
   1999 		    zio->io_orig_size, &czp, NULL,
   2000 		    zio_ddt_ditto_write_done, dde, zio->io_priority,
   2001 		    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
   2002 
   2003 		zio_push_transform(dio, zio->io_data, zio->io_size, 0, NULL);
   2004 		dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
   2005 	}
   2006 
   2007 	if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
   2008 		if (ddp->ddp_phys_birth != 0)
   2009 			ddt_bp_fill(ddp, bp, txg);
   2010 		if (dde->dde_lead_zio[p] != NULL)
   2011 			zio_add_child(zio, dde->dde_lead_zio[p]);
   2012 		else
   2013 			ddt_phys_addref(ddp);
   2014 	} else if (zio->io_bp_override) {
   2015 		ASSERT(bp->blk_birth == txg);
   2016 		ASSERT(BP_EQUAL(bp, zio->io_bp_override));
   2017 		ddt_phys_fill(ddp, bp);
   2018 		ddt_phys_addref(ddp);
   2019 	} else {
   2020 		cio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
   2021 		    zio->io_orig_size, zp, zio_ddt_child_write_ready,
   2022 		    zio_ddt_child_write_done, dde, zio->io_priority,
   2023 		    ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
   2024 
   2025 		zio_push_transform(cio, zio->io_data, zio->io_size, 0, NULL);
   2026 		dde->dde_lead_zio[p] = cio;
   2027 	}
   2028 
   2029 	ddt_exit(ddt);
   2030 
   2031 	if (cio)
   2032 		zio_nowait(cio);
   2033 	if (dio)
   2034 		zio_nowait(dio);
   2035 
   2036 	return (ZIO_PIPELINE_CONTINUE);
   2037 }
   2038 
   2039 static int
   2040 zio_ddt_free(zio_t *zio)
   2041 {
   2042 	spa_t *spa = zio->io_spa;
   2043 	blkptr_t *bp = zio->io_bp;
   2044 	ddt_t *ddt = ddt_select(spa, bp);
   2045 	ddt_entry_t *dde;
   2046 	ddt_phys_t *ddp;
   2047 
   2048 	ASSERT(BP_GET_DEDUP(bp));
   2049 	ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
   2050 
   2051 	ddt_enter(ddt);
   2052 	dde = ddt_lookup(ddt, bp, B_TRUE);
   2053 	ddp = ddt_phys_select(dde, bp);
   2054 	ddt_phys_decref(ddp);
   2055 	ddt_exit(ddt);
   2056 
   2057 	return (ZIO_PIPELINE_CONTINUE);
   2058 }
   2059 
   2060 /*
   2061  * ==========================================================================
   2062  * Allocate and free blocks
   2063  * ==========================================================================
   2064  */
   2065 static int
   2066 zio_dva_allocate(zio_t *zio)
   2067 {
   2068 	spa_t *spa = zio->io_spa;
   2069 	metaslab_class_t *mc = spa_normal_class(spa);
   2070 	blkptr_t *bp = zio->io_bp;
   2071 	int error;
   2072 
   2073 	if (zio->io_gang_leader == NULL) {
   2074 		ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
   2075 		zio->io_gang_leader = zio;
   2076 	}
   2077 
   2078 	ASSERT(BP_IS_HOLE(bp));
   2079 	ASSERT3U(BP_GET_NDVAS(bp), ==, 0);
   2080 	ASSERT3U(zio->io_prop.zp_copies, >, 0);
   2081 	ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
   2082 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
   2083 
   2084 	error = metaslab_alloc(spa, mc, zio->io_size, bp,
   2085 	    zio->io_prop.zp_copies, zio->io_txg, NULL, 0);
   2086 
   2087 	if (error) {
   2088 		if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
   2089 			return (zio_write_gang_block(zio));
   2090 		zio->io_error = error;
   2091 	}
   2092 
   2093 	return (ZIO_PIPELINE_CONTINUE);
   2094 }
   2095 
   2096 static int
   2097 zio_dva_free(zio_t *zio)
   2098 {
   2099 	metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
   2100 
   2101 	return (ZIO_PIPELINE_CONTINUE);
   2102 }
   2103 
   2104 static int
   2105 zio_dva_claim(zio_t *zio)
   2106 {
   2107 	int error;
   2108 
   2109 	error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
   2110 	if (error)
   2111 		zio->io_error = error;
   2112 
   2113 	return (ZIO_PIPELINE_CONTINUE);
   2114 }
   2115 
   2116 /*
   2117  * Undo an allocation.  This is used by zio_done() when an I/O fails
   2118  * and we want to give back the block we just allocated.
   2119  * This handles both normal blocks and gang blocks.
   2120  */
   2121 static void
   2122 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
   2123 {
   2124 	ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
   2125 	ASSERT(zio->io_bp_override == NULL);
   2126 
   2127 	if (!BP_IS_HOLE(bp))
   2128 		metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
   2129 
   2130 	if (gn != NULL) {
   2131 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
   2132 			zio_dva_unallocate(zio, gn->gn_child[g],
   2133 			    &gn->gn_gbh->zg_blkptr[g]);
   2134 		}
   2135 	}
   2136 }
   2137 
   2138 /*
   2139  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
   2140  */
   2141 int
   2142 zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, blkptr_t *old_bp,
   2143     uint64_t size, boolean_t use_slog)
   2144 {
   2145 	int error = 1;
   2146 
   2147 	ASSERT(txg > spa_syncing_txg(spa));
   2148 
   2149 	if (use_slog)
   2150 		error = metaslab_alloc(spa, spa_log_class(spa), size,
   2151 		    new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID);
   2152 
   2153 	if (error)
   2154 		error = metaslab_alloc(spa, spa_normal_class(spa), size,
   2155 		    new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID);
   2156 
   2157 	if (error == 0) {
   2158 		BP_SET_LSIZE(new_bp, size);
   2159 		BP_SET_PSIZE(new_bp, size);
   2160 		BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
   2161 		BP_SET_CHECKSUM(new_bp, ZIO_CHECKSUM_ZILOG);
   2162 		BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
   2163 		BP_SET_LEVEL(new_bp, 0);
   2164 		BP_SET_DEDUP(new_bp, 0);
   2165 		BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
   2166 	}
   2167 
   2168 	return (error);
   2169 }
   2170 
   2171 /*
   2172  * Free an intent log block.
   2173  */
   2174 void
   2175 zio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp)
   2176 {
   2177 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG);
   2178 	ASSERT(!BP_IS_GANG(bp));
   2179 
   2180 	zio_free(spa, txg, bp);
   2181 }
   2182 
   2183 /*
   2184  * ==========================================================================
   2185  * Read and write to physical devices
   2186  * ==========================================================================
   2187  */
   2188 static int
   2189 zio_vdev_io_start(zio_t *zio)
   2190 {
   2191 	vdev_t *vd = zio->io_vd;
   2192 	uint64_t align;
   2193 	spa_t *spa = zio->io_spa;
   2194 
   2195 	ASSERT(zio->io_error == 0);
   2196 	ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
   2197 
   2198 	if (vd == NULL) {
   2199 		if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
   2200 			spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
   2201 
   2202 		/*
   2203 		 * The mirror_ops handle multiple DVAs in a single BP.
   2204 		 */
   2205 		return (vdev_mirror_ops.vdev_op_io_start(zio));
   2206 	}
   2207 
   2208 	align = 1ULL << vd->vdev_top->vdev_ashift;
   2209 
   2210 	if (P2PHASE(zio->io_size, align) != 0) {
   2211 		uint64_t asize = P2ROUNDUP(zio->io_size, align);
   2212 		char *abuf = zio_buf_alloc(asize);
   2213 		ASSERT(vd == vd->vdev_top);
   2214 		if (zio->io_type == ZIO_TYPE_WRITE) {
   2215 			bcopy(zio->io_data, abuf, zio->io_size);
   2216 			bzero(abuf + zio->io_size, asize - zio->io_size);
   2217 		}
   2218 		zio_push_transform(zio, abuf, asize, asize, zio_subblock);
   2219 	}
   2220 
   2221 	ASSERT(P2PHASE(zio->io_offset, align) == 0);
   2222 	ASSERT(P2PHASE(zio->io_size, align) == 0);
   2223 	ASSERT(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
   2224 
   2225 	/*
   2226 	 * If this is a repair I/O, and there's no self-healing involved --
   2227 	 * that is, we're just resilvering what we expect to resilver --
   2228 	 * then don't do the I/O unless zio's txg is actually in vd's DTL.
   2229 	 * This prevents spurious resilvering with nested replication.
   2230 	 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
   2231 	 * A is out of date, we'll read from C+D, then use the data to
   2232 	 * resilver A+B -- but we don't actually want to resilver B, just A.
   2233 	 * The top-level mirror has no way to know this, so instead we just
   2234 	 * discard unnecessary repairs as we work our way down the vdev tree.
   2235 	 * The same logic applies to any form of nested replication:
   2236 	 * ditto + mirror, RAID-Z + replacing, etc.  This covers them all.
   2237 	 */
   2238 	if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
   2239 	    !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
   2240 	    zio->io_txg != 0 &&	/* not a delegated i/o */
   2241 	    !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
   2242 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
   2243 		zio_vdev_io_bypass(zio);
   2244 		return (ZIO_PIPELINE_CONTINUE);
   2245 	}
   2246 
   2247 	if (vd->vdev_ops->vdev_op_leaf &&
   2248 	    (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) {
   2249 
   2250 		if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
   2251 			return (ZIO_PIPELINE_CONTINUE);
   2252 
   2253 		if ((zio = vdev_queue_io(zio)) == NULL)
   2254 			return (ZIO_PIPELINE_STOP);
   2255 
   2256 		if (!vdev_accessible(vd, zio)) {
   2257 			zio->io_error = ENXIO;
   2258 			zio_interrupt(zio);
   2259 			return (ZIO_PIPELINE_STOP);
   2260 		}
   2261 	}
   2262 
   2263 	return (vd->vdev_ops->vdev_op_io_start(zio));
   2264 }
   2265 
   2266 static int
   2267 zio_vdev_io_done(zio_t *zio)
   2268 {
   2269 	vdev_t *vd = zio->io_vd;
   2270 	vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
   2271 	boolean_t unexpected_error = B_FALSE;
   2272 
   2273 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
   2274 		return (ZIO_PIPELINE_STOP);
   2275 
   2276 	ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
   2277 
   2278 	if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
   2279 
   2280 		vdev_queue_io_done(zio);
   2281 
   2282 		if (zio->io_type == ZIO_TYPE_WRITE)
   2283 			vdev_cache_write(zio);
   2284 
   2285 		if (zio_injection_enabled && zio->io_error == 0)
   2286 			zio->io_error = zio_handle_device_injection(vd,
   2287 			    zio, EIO);
   2288 
   2289 		if (zio_injection_enabled && zio->io_error == 0)
   2290 			zio->io_error = zio_handle_label_injection(zio, EIO);
   2291 
   2292 		if (zio->io_error) {
   2293 			if (!vdev_accessible(vd, zio)) {
   2294 				zio->io_error = ENXIO;
   2295 			} else {
   2296 				unexpected_error = B_TRUE;
   2297 			}
   2298 		}
   2299 	}
   2300 
   2301 	ops->vdev_op_io_done(zio);
   2302 
   2303 	if (unexpected_error)
   2304 		VERIFY(vdev_probe(vd, zio) == NULL);
   2305 
   2306 	return (ZIO_PIPELINE_CONTINUE);
   2307 }
   2308 
   2309 /*
   2310  * For non-raidz ZIOs, we can just copy aside the bad data read from the
   2311  * disk, and use that to finish the checksum ereport later.
   2312  */
   2313 static void
   2314 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
   2315     const void *good_buf)
   2316 {
   2317 	/* no processing needed */
   2318 	zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
   2319 }
   2320 
   2321 /*ARGSUSED*/
   2322 void
   2323 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
   2324 {
   2325 	void *buf = zio_buf_alloc(zio->io_size);
   2326 
   2327 	bcopy(zio->io_data, buf, zio->io_size);
   2328 
   2329 	zcr->zcr_cbinfo = zio->io_size;
   2330 	zcr->zcr_cbdata = buf;
   2331 	zcr->zcr_finish = zio_vsd_default_cksum_finish;
   2332 	zcr->zcr_free = zio_buf_free;
   2333 }
   2334 
   2335 static int
   2336 zio_vdev_io_assess(zio_t *zio)
   2337 {
   2338 	vdev_t *vd = zio->io_vd;
   2339 
   2340 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
   2341 		return (ZIO_PIPELINE_STOP);
   2342 
   2343 	if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
   2344 		spa_config_exit(zio->io_spa, SCL_ZIO, zio);
   2345 
   2346 	if (zio->io_vsd != NULL) {
   2347 		zio->io_vsd_ops->vsd_free(zio);
   2348 		zio->io_vsd = NULL;
   2349 	}
   2350 
   2351 	if (zio_injection_enabled && zio->io_error == 0)
   2352 		zio->io_error = zio_handle_fault_injection(zio, EIO);
   2353 
   2354 	/*
   2355 	 * If the I/O failed, determine whether we should attempt to retry it.
   2356 	 */
   2357 	if (zio->io_error && vd == NULL &&
   2358 	    !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
   2359 		ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE));	/* not a leaf */
   2360 		ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS));	/* not a leaf */
   2361 		zio->io_error = 0;
   2362 		zio->io_flags |= ZIO_FLAG_IO_RETRY |
   2363 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
   2364 		zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
   2365 		zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE);
   2366 		return (ZIO_PIPELINE_STOP);
   2367 	}
   2368 
   2369 	/*
   2370 	 * If we got an error on a leaf device, convert it to ENXIO
   2371 	 * if the device is not accessible at all.
   2372 	 */
   2373 	if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
   2374 	    !vdev_accessible(vd, zio))
   2375 		zio->io_error = ENXIO;
   2376 
   2377 	/*
   2378 	 * If we can't write to an interior vdev (mirror or RAID-Z),
   2379 	 * set vdev_cant_write so that we stop trying to allocate from it.
   2380 	 */
   2381 	if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
   2382 	    vd != NULL && !vd->vdev_ops->vdev_op_leaf)
   2383 		vd->vdev_cant_write = B_TRUE;
   2384 
   2385 	if (zio->io_error)
   2386 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
   2387 
   2388 	return (ZIO_PIPELINE_CONTINUE);
   2389 }
   2390 
   2391 void
   2392 zio_vdev_io_reissue(zio_t *zio)
   2393 {
   2394 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
   2395 	ASSERT(zio->io_error == 0);
   2396 
   2397 	zio->io_stage >>= 1;
   2398 }
   2399 
   2400 void
   2401 zio_vdev_io_redone(zio_t *zio)
   2402 {
   2403 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
   2404 
   2405 	zio->io_stage >>= 1;
   2406 }
   2407 
   2408 void
   2409 zio_vdev_io_bypass(zio_t *zio)
   2410 {
   2411 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
   2412 	ASSERT(zio->io_error == 0);
   2413 
   2414 	zio->io_flags |= ZIO_FLAG_IO_BYPASS;
   2415 	zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
   2416 }
   2417 
   2418 /*
   2419  * ==========================================================================
   2420  * Generate and verify checksums
   2421  * ==========================================================================
   2422  */
   2423 static int
   2424 zio_checksum_generate(zio_t *zio)
   2425 {
   2426 	blkptr_t *bp = zio->io_bp;
   2427 	enum zio_checksum checksum;
   2428 
   2429 	if (bp == NULL) {
   2430 		/*
   2431 		 * This is zio_write_phys().
   2432 		 * We're either generating a label checksum, or none at all.
   2433 		 */
   2434 		checksum = zio->io_prop.zp_checksum;
   2435 
   2436 		if (checksum == ZIO_CHECKSUM_OFF)
   2437 			return (ZIO_PIPELINE_CONTINUE);
   2438 
   2439 		ASSERT(checksum == ZIO_CHECKSUM_LABEL);
   2440 	} else {
   2441 		if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
   2442 			ASSERT(!IO_IS_ALLOCATING(zio));
   2443 			checksum = ZIO_CHECKSUM_GANG_HEADER;
   2444 		} else {
   2445 			checksum = BP_GET_CHECKSUM(bp);
   2446 		}
   2447 	}
   2448 
   2449 	zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size);
   2450 
   2451 	return (ZIO_PIPELINE_CONTINUE);
   2452 }
   2453 
   2454 static int
   2455 zio_checksum_verify(zio_t *zio)
   2456 {
   2457 	zio_bad_cksum_t info;
   2458 	blkptr_t *bp = zio->io_bp;
   2459 	int error;
   2460 
   2461 	ASSERT(zio->io_vd != NULL);
   2462 
   2463 	if (bp == NULL) {
   2464 		/*
   2465 		 * This is zio_read_phys().
   2466 		 * We're either verifying a label checksum, or nothing at all.
   2467 		 */
   2468 		if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
   2469 			return (ZIO_PIPELINE_CONTINUE);
   2470 
   2471 		ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
   2472 	}
   2473 
   2474 	if ((error = zio_checksum_error(zio, &info)) != 0) {
   2475 		zio->io_error = error;
   2476 		if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
   2477 			zfs_ereport_start_checksum(zio->io_spa,
   2478 			    zio->io_vd, zio, zio->io_offset,
   2479 			    zio->io_size, NULL, &info);
   2480 		}
   2481 	}
   2482 
   2483 	return (ZIO_PIPELINE_CONTINUE);
   2484 }
   2485 
   2486 /*
   2487  * Called by RAID-Z to ensure we don't compute the checksum twice.
   2488  */
   2489 void
   2490 zio_checksum_verified(zio_t *zio)
   2491 {
   2492 	zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
   2493 }
   2494 
   2495 /*
   2496  * ==========================================================================
   2497  * Error rank.  Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
   2498  * An error of 0 indictes success.  ENXIO indicates whole-device failure,
   2499  * which may be transient (e.g. unplugged) or permament.  ECKSUM and EIO
   2500  * indicate errors that are specific to one I/O, and most likely permanent.
   2501  * Any other error is presumed to be worse because we weren't expecting it.
   2502  * ==========================================================================
   2503  */
   2504 int
   2505 zio_worst_error(int e1, int e2)
   2506 {
   2507 	static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
   2508 	int r1, r2;
   2509 
   2510 	for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
   2511 		if (e1 == zio_error_rank[r1])
   2512 			break;
   2513 
   2514 	for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
   2515 		if (e2 == zio_error_rank[r2])
   2516 			break;
   2517 
   2518 	return (r1 > r2 ? e1 : e2);
   2519 }
   2520 
   2521 /*
   2522  * ==========================================================================
   2523  * I/O completion
   2524  * ==========================================================================
   2525  */
   2526 static int
   2527 zio_ready(zio_t *zio)
   2528 {
   2529 	blkptr_t *bp = zio->io_bp;
   2530 	zio_t *pio, *pio_next;
   2531 
   2532 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
   2533 	    zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY))
   2534 		return (ZIO_PIPELINE_STOP);
   2535 
   2536 	if (zio->io_ready) {
   2537 		ASSERT(IO_IS_ALLOCATING(zio));
   2538 		ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
   2539 		ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
   2540 
   2541 		zio->io_ready(zio);
   2542 	}
   2543 
   2544 	if (bp != NULL && bp != &zio->io_bp_copy)
   2545 		zio->io_bp_copy = *bp;
   2546 
   2547 	if (zio->io_error)
   2548 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
   2549 
   2550 	mutex_enter(&zio->io_lock);
   2551 	zio->io_state[ZIO_WAIT_READY] = 1;
   2552 	pio = zio_walk_parents(zio);
   2553 	mutex_exit(&zio->io_lock);
   2554 
   2555 	/*
   2556 	 * As we notify zio's parents, new parents could be added.
   2557 	 * New parents go to the head of zio's io_parent_list, however,
   2558 	 * so we will (correctly) not notify them.  The remainder of zio's
   2559 	 * io_parent_list, from 'pio_next' onward, cannot change because
   2560 	 * all parents must wait for us to be done before they can be done.
   2561 	 */
   2562 	for (; pio != NULL; pio = pio_next) {
   2563 		pio_next = zio_walk_parents(zio);
   2564 		zio_notify_parent(pio, zio, ZIO_WAIT_READY);
   2565 	}
   2566 
   2567 	if (zio->io_flags & ZIO_FLAG_NODATA) {
   2568 		if (BP_IS_GANG(bp)) {
   2569 			zio->io_flags &= ~ZIO_FLAG_NODATA;
   2570 		} else {
   2571 			ASSERT((uintptr_t)zio->io_data < SPA_MAXBLOCKSIZE);
   2572 			zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
   2573 		}
   2574 	}
   2575 
   2576 	if (zio_injection_enabled &&
   2577 	    zio->io_spa->spa_syncing_txg == zio->io_txg)
   2578 		zio_handle_ignored_writes(zio);
   2579 
   2580 	return (ZIO_PIPELINE_CONTINUE);
   2581 }
   2582 
   2583 static int
   2584 zio_done(zio_t *zio)
   2585 {
   2586 	spa_t *spa = zio->io_spa;
   2587 	zio_t *lio = zio->io_logical;
   2588 	blkptr_t *bp = zio->io_bp;
   2589 	vdev_t *vd = zio->io_vd;
   2590 	uint64_t psize = zio->io_size;
   2591 	zio_t *pio, *pio_next;
   2592 
   2593 	/*
   2594 	 * If our children haven't all completed,
   2595 	 * wait for them and then repeat this pipeline stage.
   2596 	 */
   2597 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
   2598 	    zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
   2599 	    zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) ||
   2600 	    zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
   2601 		return (ZIO_PIPELINE_STOP);
   2602 
   2603 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
   2604 		for (int w = 0; w < ZIO_WAIT_TYPES; w++)
   2605 			ASSERT(zio->io_children[c][w] == 0);
   2606 
   2607 	if (bp != NULL) {
   2608 		ASSERT(bp->blk_pad[0] == 0);
   2609 		ASSERT(bp->blk_pad[1] == 0);
   2610 		ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
   2611 		    (bp == zio_unique_parent(zio)->io_bp));
   2612 		if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
   2613 		    zio->io_bp_override == NULL &&
   2614 		    !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
   2615 			ASSERT(!BP_SHOULD_BYTESWAP(bp));
   2616 			ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp));
   2617 			ASSERT(BP_COUNT_GANG(bp) == 0 ||
   2618 			    (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
   2619 		}
   2620 	}
   2621 
   2622 	/*
   2623 	 * If there were child vdev/gang/ddt errors, they apply to us now.
   2624 	 */
   2625 	zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
   2626 	zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
   2627 	zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
   2628 
   2629 	/*
   2630 	 * If the I/O on the transformed data was successful, generate any
   2631 	 * checksum reports now while we still have the transformed data.
   2632 	 */
   2633 	if (zio->io_error == 0) {
   2634 		while (zio->io_cksum_report != NULL) {
   2635 			zio_cksum_report_t *zcr = zio->io_cksum_report;
   2636 			uint64_t align = zcr->zcr_align;
   2637 			uint64_t asize = P2ROUNDUP(psize, align);
   2638 			char *abuf = zio->io_data;
   2639 
   2640 			if (asize != psize) {
   2641 				abuf = zio_buf_alloc(asize);
   2642 				bcopy(zio->io_data, abuf, psize);
   2643 				bzero(abuf + psize, asize - psize);
   2644 			}
   2645 
   2646 			zio->io_cksum_report = zcr->zcr_next;
   2647 			zcr->zcr_next = NULL;
   2648 			zcr->zcr_finish(zcr, abuf);
   2649 			zfs_ereport_free_checksum(zcr);
   2650 
   2651 			if (asize != psize)
   2652 				zio_buf_free(abuf, asize);
   2653 		}
   2654 	}
   2655 
   2656 	zio_pop_transforms(zio);	/* note: may set zio->io_error */
   2657 
   2658 	vdev_stat_update(zio, psize);
   2659 
   2660 	if (zio->io_error) {
   2661 		/*
   2662 		 * If this I/O is attached to a particular vdev,
   2663 		 * generate an error message describing the I/O failure
   2664 		 * at the block level.  We ignore these errors if the
   2665 		 * device is currently unavailable.
   2666 		 */
   2667 		if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
   2668 			zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0);
   2669 
   2670 		if ((zio->io_error == EIO || !(zio->io_flags &
   2671 		    (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
   2672 		    zio == lio) {
   2673 			/*
   2674 			 * For logical I/O requests, tell the SPA to log the
   2675 			 * error and generate a logical data ereport.
   2676 			 */
   2677 			spa_log_error(spa, zio);
   2678 			zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio,
   2679 			    0, 0);
   2680 		}
   2681 	}
   2682 
   2683 	if (zio->io_error && zio == lio) {
   2684 		/*
   2685 		 * Determine whether zio should be reexecuted.  This will
   2686 		 * propagate all the way to the root via zio_notify_parent().
   2687 		 */
   2688 		ASSERT(vd == NULL && bp != NULL);
   2689 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
   2690 
   2691 		if (IO_IS_ALLOCATING(zio) &&
   2692 		    !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
   2693 			if (zio->io_error != ENOSPC)
   2694 				zio->io_reexecute |= ZIO_REEXECUTE_NOW;
   2695 			else
   2696 				zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
   2697 		}
   2698 
   2699 		if ((zio->io_type == ZIO_TYPE_READ ||
   2700 		    zio->io_type == ZIO_TYPE_FREE) &&
   2701 		    zio->io_error == ENXIO &&
   2702 		    spa->spa_load_state == SPA_LOAD_NONE &&
   2703 		    spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
   2704 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
   2705 
   2706 		if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
   2707 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
   2708 
   2709 		/*
   2710 		 * Here is a possibly good place to attempt to do
   2711 		 * either combinatorial reconstruction or error correction
   2712 		 * based on checksums.  It also might be a good place
   2713 		 * to send out preliminary ereports before we suspend
   2714 		 * processing.
   2715 		 */
   2716 	}
   2717 
   2718 	/*
   2719 	 * If there were logical child errors, they apply to us now.
   2720 	 * We defer this until now to avoid conflating logical child
   2721 	 * errors with errors that happened to the zio itself when
   2722 	 * updating vdev stats and reporting FMA events above.
   2723 	 */
   2724 	zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
   2725 
   2726 	if ((zio->io_error || zio->io_reexecute) &&
   2727 	    IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
   2728 	    !(zio->io_flags & ZIO_FLAG_IO_REWRITE))
   2729 		zio_dva_unallocate(zio, zio->io_gang_tree, bp);
   2730 
   2731 	zio_gang_tree_free(&zio->io_gang_tree);
   2732 
   2733 	/*
   2734 	 * Godfather I/Os should never suspend.
   2735 	 */
   2736 	if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
   2737 	    (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
   2738 		zio->io_reexecute = 0;
   2739 
   2740 	if (zio->io_reexecute) {
   2741 		/*
   2742 		 * This is a logical I/O that wants to reexecute.
   2743 		 *
   2744 		 * Reexecute is top-down.  When an i/o fails, if it's not
   2745 		 * the root, it simply notifies its parent and sticks around.
   2746 		 * The parent, seeing that it still has children in zio_done(),
   2747 		 * does the same.  This percolates all the way up to the root.
   2748 		 * The root i/o will reexecute or suspend the entire tree.
   2749 		 *
   2750 		 * This approach ensures that zio_reexecute() honors
   2751 		 * all the original i/o dependency relationships, e.g.
   2752 		 * parents not executing until children are ready.
   2753 		 */
   2754 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
   2755 
   2756 		zio->io_gang_leader = NULL;
   2757 
   2758 		mutex_enter(&zio->io_lock);
   2759 		zio->io_state[ZIO_WAIT_DONE] = 1;
   2760 		mutex_exit(&zio->io_lock);
   2761 
   2762 		/*
   2763 		 * "The Godfather" I/O monitors its children but is
   2764 		 * not a true parent to them. It will track them through
   2765 		 * the pipeline but severs its ties whenever they get into
   2766 		 * trouble (e.g. suspended). This allows "The Godfather"
   2767 		 * I/O to return status without blocking.
   2768 		 */
   2769 		for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
   2770 			zio_link_t *zl = zio->io_walk_link;
   2771 			pio_next = zio_walk_parents(zio);
   2772 
   2773 			if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
   2774 			    (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
   2775 				zio_remove_child(pio, zio, zl);
   2776 				zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
   2777 			}
   2778 		}
   2779 
   2780 		if ((pio = zio_unique_parent(zio)) != NULL) {
   2781 			/*
   2782 			 * We're not a root i/o, so there's nothing to do
   2783 			 * but notify our parent.  Don't propagate errors
   2784 			 * upward since we haven't permanently failed yet.
   2785 			 */
   2786 			ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
   2787 			zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
   2788 			zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
   2789 		} else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
   2790 			/*
   2791 			 * We'd fail again if we reexecuted now, so suspend
   2792 			 * until conditions improve (e.g. device comes online).
   2793 			 */
   2794 			zio_suspend(spa, zio);
   2795 		} else {
   2796 			/*
   2797 			 * Reexecution is potentially a huge amount of work.
   2798 			 * Hand it off to the otherwise-unused claim taskq.
   2799 			 */
   2800 			(void) taskq_dispatch(
   2801 			    spa->spa_zio_taskq[ZIO_TYPE_CLAIM][ZIO_TASKQ_ISSUE],
   2802 			    (task_func_t *)zio_reexecute, zio, TQ_SLEEP);
   2803 		}
   2804 		return (ZIO_PIPELINE_STOP);
   2805 	}
   2806 
   2807 	ASSERT(zio->io_child_count == 0);
   2808 	ASSERT(zio->io_reexecute == 0);
   2809 	ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
   2810 
   2811 	/*
   2812 	 * Report any checksum errors, since the I/O is complete.
   2813 	 */
   2814 	while (zio->io_cksum_report != NULL) {
   2815 		zio_cksum_report_t *zcr = zio->io_cksum_report;
   2816 		zio->io_cksum_report = zcr->zcr_next;
   2817 		zcr->zcr_next = NULL;
   2818 		zcr->zcr_finish(zcr, NULL);
   2819 		zfs_ereport_free_checksum(zcr);
   2820 	}
   2821 
   2822 	/*
   2823 	 * It is the responsibility of the done callback to ensure that this
   2824 	 * particular zio is no longer discoverable for adoption, and as
   2825 	 * such, cannot acquire any new parents.
   2826 	 */
   2827 	if (zio->io_done)
   2828 		zio->io_done(zio);
   2829 
   2830 	mutex_enter(&zio->io_lock);
   2831 	zio->io_state[ZIO_WAIT_DONE] = 1;
   2832 	mutex_exit(&zio->io_lock);
   2833 
   2834 	for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
   2835 		zio_link_t *zl = zio->io_walk_link;
   2836 		pio_next = zio_walk_parents(zio);
   2837 		zio_remove_child(pio, zio, zl);
   2838 		zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
   2839 	}
   2840 
   2841 	if (zio->io_waiter != NULL) {
   2842 		mutex_enter(&zio->io_lock);
   2843 		zio->io_executor = NULL;
   2844 		cv_broadcast(&zio->io_cv);
   2845 		mutex_exit(&zio->io_lock);
   2846 	} else {
   2847 		zio_destroy(zio);
   2848 	}
   2849 
   2850 	return (ZIO_PIPELINE_STOP);
   2851 }
   2852 
   2853 /*
   2854  * ==========================================================================
   2855  * I/O pipeline definition
   2856  * ==========================================================================
   2857  */
   2858 static zio_pipe_stage_t *zio_pipeline[] = {
   2859 	NULL,
   2860 	zio_read_bp_init,
   2861 	zio_free_bp_init,
   2862 	zio_issue_async,
   2863 	zio_write_bp_init,
   2864 	zio_checksum_generate,
   2865 	zio_ddt_read_start,
   2866 	zio_ddt_read_done,
   2867 	zio_ddt_write,
   2868 	zio_ddt_free,
   2869 	zio_gang_assemble,
   2870 	zio_gang_issue,
   2871 	zio_dva_allocate,
   2872 	zio_dva_free,
   2873 	zio_dva_claim,
   2874 	zio_ready,
   2875 	zio_vdev_io_start,
   2876 	zio_vdev_io_done,
   2877 	zio_vdev_io_assess,
   2878 	zio_checksum_verify,
   2879 	zio_done
   2880 };
   2881