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