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