xref: /titanic_41/usr/src/uts/common/fs/zfs/zio.c (revision 32b8d01a0d9b83fc9a4298502c1d2cab15ae3754)
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 		ASSERT(pass > 1);
879 
880 		if (pass > SYNC_PASS_DONT_COMPRESS)
881 			compress = ZIO_COMPRESS_OFF;
882 
883 		/*
884 		 * Only MOS (objset 0) data should need to be rewritten.
885 		 */
886 		ASSERT(zio->io_logical->io_bookmark.zb_objset == 0);
887 
888 		/* Make sure someone doesn't change their mind on overwrites */
889 		ASSERT(MIN(zp->zp_ndvas + BP_IS_GANG(bp),
890 		    spa_max_replication(zio->io_spa)) == BP_GET_NDVAS(bp));
891 	}
892 
893 	if (compress != ZIO_COMPRESS_OFF) {
894 		if (!zio_compress_data(compress, zio->io_data, zio->io_size,
895 		    &cbuf, &csize, &cbufsize)) {
896 			compress = ZIO_COMPRESS_OFF;
897 		} else if (csize != 0) {
898 			zio_push_transform(zio, cbuf, csize, cbufsize, NULL);
899 		}
900 	}
901 
902 	/*
903 	 * The final pass of spa_sync() must be all rewrites, but the first
904 	 * few passes offer a trade-off: allocating blocks defers convergence,
905 	 * but newly allocated blocks are sequential, so they can be written
906 	 * to disk faster.  Therefore, we allow the first few passes of
907 	 * spa_sync() to allocate new blocks, but force rewrites after that.
908 	 * There should only be a handful of blocks after pass 1 in any case.
909 	 */
910 	if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == csize &&
911 	    pass > SYNC_PASS_REWRITE) {
912 		ASSERT(csize != 0);
913 		uint32_t gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
914 		zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
915 		zio->io_flags |= ZIO_FLAG_IO_REWRITE;
916 	} else {
917 		BP_ZERO(bp);
918 		zio->io_pipeline = ZIO_WRITE_PIPELINE;
919 	}
920 
921 	if (csize == 0) {
922 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
923 	} else {
924 		ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
925 		BP_SET_LSIZE(bp, lsize);
926 		BP_SET_PSIZE(bp, csize);
927 		BP_SET_COMPRESS(bp, compress);
928 		BP_SET_CHECKSUM(bp, zp->zp_checksum);
929 		BP_SET_TYPE(bp, zp->zp_type);
930 		BP_SET_LEVEL(bp, zp->zp_level);
931 		BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
932 	}
933 
934 	return (ZIO_PIPELINE_CONTINUE);
935 }
936 
937 /*
938  * ==========================================================================
939  * Execute the I/O pipeline
940  * ==========================================================================
941  */
942 
943 static void
944 zio_taskq_dispatch(zio_t *zio, enum zio_taskq_type q)
945 {
946 	zio_type_t t = zio->io_type;
947 
948 	/*
949 	 * If we're a config writer, the normal issue and interrupt threads
950 	 * may all be blocked waiting for the config lock.  In this case,
951 	 * select the otherwise-unused taskq for ZIO_TYPE_NULL.
952 	 */
953 	if (zio->io_flags & ZIO_FLAG_CONFIG_WRITER)
954 		t = ZIO_TYPE_NULL;
955 
956 	/*
957 	 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
958 	 */
959 	if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
960 		t = ZIO_TYPE_NULL;
961 
962 	(void) taskq_dispatch(zio->io_spa->spa_zio_taskq[t][q],
963 	    (task_func_t *)zio_execute, zio, TQ_SLEEP);
964 }
965 
966 static boolean_t
967 zio_taskq_member(zio_t *zio, enum zio_taskq_type q)
968 {
969 	kthread_t *executor = zio->io_executor;
970 	spa_t *spa = zio->io_spa;
971 
972 	for (zio_type_t t = 0; t < ZIO_TYPES; t++)
973 		if (taskq_member(spa->spa_zio_taskq[t][q], executor))
974 			return (B_TRUE);
975 
976 	return (B_FALSE);
977 }
978 
979 static int
980 zio_issue_async(zio_t *zio)
981 {
982 	zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE);
983 
984 	return (ZIO_PIPELINE_STOP);
985 }
986 
987 void
988 zio_interrupt(zio_t *zio)
989 {
990 	zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT);
991 }
992 
993 /*
994  * Execute the I/O pipeline until one of the following occurs:
995  * (1) the I/O completes; (2) the pipeline stalls waiting for
996  * dependent child I/Os; (3) the I/O issues, so we're waiting
997  * for an I/O completion interrupt; (4) the I/O is delegated by
998  * vdev-level caching or aggregation; (5) the I/O is deferred
999  * due to vdev-level queueing; (6) the I/O is handed off to
1000  * another thread.  In all cases, the pipeline stops whenever
1001  * there's no CPU work; it never burns a thread in cv_wait().
1002  *
1003  * There's no locking on io_stage because there's no legitimate way
1004  * for multiple threads to be attempting to process the same I/O.
1005  */
1006 static zio_pipe_stage_t *zio_pipeline[ZIO_STAGES];
1007 
1008 void
1009 zio_execute(zio_t *zio)
1010 {
1011 	zio->io_executor = curthread;
1012 
1013 	while (zio->io_stage < ZIO_STAGE_DONE) {
1014 		uint32_t pipeline = zio->io_pipeline;
1015 		zio_stage_t stage = zio->io_stage;
1016 		int rv;
1017 
1018 		ASSERT(!MUTEX_HELD(&zio->io_lock));
1019 
1020 		while (((1U << ++stage) & pipeline) == 0)
1021 			continue;
1022 
1023 		ASSERT(stage <= ZIO_STAGE_DONE);
1024 		ASSERT(zio->io_stall == NULL);
1025 
1026 		/*
1027 		 * If we are in interrupt context and this pipeline stage
1028 		 * will grab a config lock that is held across I/O,
1029 		 * issue async to avoid deadlock.
1030 		 */
1031 		if (((1U << stage) & ZIO_CONFIG_LOCK_BLOCKING_STAGES) &&
1032 		    zio->io_vd == NULL &&
1033 		    zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1034 			zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE);
1035 			return;
1036 		}
1037 
1038 		zio->io_stage = stage;
1039 		rv = zio_pipeline[stage](zio);
1040 
1041 		if (rv == ZIO_PIPELINE_STOP)
1042 			return;
1043 
1044 		ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1045 	}
1046 }
1047 
1048 /*
1049  * ==========================================================================
1050  * Initiate I/O, either sync or async
1051  * ==========================================================================
1052  */
1053 int
1054 zio_wait(zio_t *zio)
1055 {
1056 	int error;
1057 
1058 	ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1059 	ASSERT(zio->io_executor == NULL);
1060 
1061 	zio->io_waiter = curthread;
1062 
1063 	zio_execute(zio);
1064 
1065 	mutex_enter(&zio->io_lock);
1066 	while (zio->io_executor != NULL)
1067 		cv_wait(&zio->io_cv, &zio->io_lock);
1068 	mutex_exit(&zio->io_lock);
1069 
1070 	error = zio->io_error;
1071 	zio_destroy(zio);
1072 
1073 	return (error);
1074 }
1075 
1076 void
1077 zio_nowait(zio_t *zio)
1078 {
1079 	ASSERT(zio->io_executor == NULL);
1080 
1081 	if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1082 	    zio_unique_parent(zio) == NULL) {
1083 		/*
1084 		 * This is a logical async I/O with no parent to wait for it.
1085 		 * We add it to the spa_async_root_zio "Godfather" I/O which
1086 		 * will ensure they complete prior to unloading the pool.
1087 		 */
1088 		spa_t *spa = zio->io_spa;
1089 
1090 		zio_add_child(spa->spa_async_zio_root, zio);
1091 	}
1092 
1093 	zio_execute(zio);
1094 }
1095 
1096 /*
1097  * ==========================================================================
1098  * Reexecute or suspend/resume failed I/O
1099  * ==========================================================================
1100  */
1101 
1102 static void
1103 zio_reexecute(zio_t *pio)
1104 {
1105 	zio_t *cio, *cio_next;
1106 
1107 	ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1108 	ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1109 
1110 	pio->io_flags = pio->io_orig_flags;
1111 	pio->io_stage = pio->io_orig_stage;
1112 	pio->io_pipeline = pio->io_orig_pipeline;
1113 	pio->io_reexecute = 0;
1114 	pio->io_error = 0;
1115 	for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1116 		pio->io_state[w] = 0;
1117 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
1118 		pio->io_child_error[c] = 0;
1119 
1120 	if (IO_IS_ALLOCATING(pio)) {
1121 		/*
1122 		 * Remember the failed bp so that the io_ready() callback
1123 		 * can update its accounting upon reexecution.  The block
1124 		 * was already freed in zio_done(); we indicate this with
1125 		 * a fill count of -1 so that zio_free() knows to skip it.
1126 		 */
1127 		blkptr_t *bp = pio->io_bp;
1128 		ASSERT(bp->blk_birth == 0 || bp->blk_birth == pio->io_txg);
1129 		bp->blk_fill = BLK_FILL_ALREADY_FREED;
1130 		pio->io_bp_orig = *bp;
1131 		BP_ZERO(bp);
1132 	}
1133 
1134 	/*
1135 	 * As we reexecute pio's children, new children could be created.
1136 	 * New children go to the head of pio's io_child_list, however,
1137 	 * so we will (correctly) not reexecute them.  The key is that
1138 	 * the remainder of pio's io_child_list, from 'cio_next' onward,
1139 	 * cannot be affected by any side effects of reexecuting 'cio'.
1140 	 */
1141 	for (cio = zio_walk_children(pio); cio != NULL; cio = cio_next) {
1142 		cio_next = zio_walk_children(pio);
1143 		mutex_enter(&pio->io_lock);
1144 		for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1145 			pio->io_children[cio->io_child_type][w]++;
1146 		mutex_exit(&pio->io_lock);
1147 		zio_reexecute(cio);
1148 	}
1149 
1150 	/*
1151 	 * Now that all children have been reexecuted, execute the parent.
1152 	 * We don't reexecute "The Godfather" I/O here as it's the
1153 	 * responsibility of the caller to wait on him.
1154 	 */
1155 	if (!(pio->io_flags & ZIO_FLAG_GODFATHER))
1156 		zio_execute(pio);
1157 }
1158 
1159 void
1160 zio_suspend(spa_t *spa, zio_t *zio)
1161 {
1162 	if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1163 		fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1164 		    "failure and the failure mode property for this pool "
1165 		    "is set to panic.", spa_name(spa));
1166 
1167 	zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
1168 
1169 	mutex_enter(&spa->spa_suspend_lock);
1170 
1171 	if (spa->spa_suspend_zio_root == NULL)
1172 		spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
1173 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1174 		    ZIO_FLAG_GODFATHER);
1175 
1176 	spa->spa_suspended = B_TRUE;
1177 
1178 	if (zio != NULL) {
1179 		ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
1180 		ASSERT(zio != spa->spa_suspend_zio_root);
1181 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1182 		ASSERT(zio_unique_parent(zio) == NULL);
1183 		ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1184 		zio_add_child(spa->spa_suspend_zio_root, zio);
1185 	}
1186 
1187 	mutex_exit(&spa->spa_suspend_lock);
1188 }
1189 
1190 int
1191 zio_resume(spa_t *spa)
1192 {
1193 	zio_t *pio;
1194 
1195 	/*
1196 	 * Reexecute all previously suspended i/o.
1197 	 */
1198 	mutex_enter(&spa->spa_suspend_lock);
1199 	spa->spa_suspended = B_FALSE;
1200 	cv_broadcast(&spa->spa_suspend_cv);
1201 	pio = spa->spa_suspend_zio_root;
1202 	spa->spa_suspend_zio_root = NULL;
1203 	mutex_exit(&spa->spa_suspend_lock);
1204 
1205 	if (pio == NULL)
1206 		return (0);
1207 
1208 	zio_reexecute(pio);
1209 	return (zio_wait(pio));
1210 }
1211 
1212 void
1213 zio_resume_wait(spa_t *spa)
1214 {
1215 	mutex_enter(&spa->spa_suspend_lock);
1216 	while (spa_suspended(spa))
1217 		cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
1218 	mutex_exit(&spa->spa_suspend_lock);
1219 }
1220 
1221 /*
1222  * ==========================================================================
1223  * Gang blocks.
1224  *
1225  * A gang block is a collection of small blocks that looks to the DMU
1226  * like one large block.  When zio_dva_allocate() cannot find a block
1227  * of the requested size, due to either severe fragmentation or the pool
1228  * being nearly full, it calls zio_write_gang_block() to construct the
1229  * block from smaller fragments.
1230  *
1231  * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1232  * three (SPA_GBH_NBLKPTRS) gang members.  The gang header is just like
1233  * an indirect block: it's an array of block pointers.  It consumes
1234  * only one sector and hence is allocatable regardless of fragmentation.
1235  * The gang header's bps point to its gang members, which hold the data.
1236  *
1237  * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1238  * as the verifier to ensure uniqueness of the SHA256 checksum.
1239  * Critically, the gang block bp's blk_cksum is the checksum of the data,
1240  * not the gang header.  This ensures that data block signatures (needed for
1241  * deduplication) are independent of how the block is physically stored.
1242  *
1243  * Gang blocks can be nested: a gang member may itself be a gang block.
1244  * Thus every gang block is a tree in which root and all interior nodes are
1245  * gang headers, and the leaves are normal blocks that contain user data.
1246  * The root of the gang tree is called the gang leader.
1247  *
1248  * To perform any operation (read, rewrite, free, claim) on a gang block,
1249  * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1250  * in the io_gang_tree field of the original logical i/o by recursively
1251  * reading the gang leader and all gang headers below it.  This yields
1252  * an in-core tree containing the contents of every gang header and the
1253  * bps for every constituent of the gang block.
1254  *
1255  * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1256  * and invokes a callback on each bp.  To free a gang block, zio_gang_issue()
1257  * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1258  * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1259  * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1260  * headers, since we already have those in io_gang_tree.  zio_rewrite_gang()
1261  * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1262  * of the gang header plus zio_checksum_compute() of the data to update the
1263  * gang header's blk_cksum as described above.
1264  *
1265  * The two-phase assemble/issue model solves the problem of partial failure --
1266  * what if you'd freed part of a gang block but then couldn't read the
1267  * gang header for another part?  Assembling the entire gang tree first
1268  * ensures that all the necessary gang header I/O has succeeded before
1269  * starting the actual work of free, claim, or write.  Once the gang tree
1270  * is assembled, free and claim are in-memory operations that cannot fail.
1271  *
1272  * In the event that a gang write fails, zio_dva_unallocate() walks the
1273  * gang tree to immediately free (i.e. insert back into the space map)
1274  * everything we've allocated.  This ensures that we don't get ENOSPC
1275  * errors during repeated suspend/resume cycles due to a flaky device.
1276  *
1277  * Gang rewrites only happen during sync-to-convergence.  If we can't assemble
1278  * the gang tree, we won't modify the block, so we can safely defer the free
1279  * (knowing that the block is still intact).  If we *can* assemble the gang
1280  * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1281  * each constituent bp and we can allocate a new block on the next sync pass.
1282  *
1283  * In all cases, the gang tree allows complete recovery from partial failure.
1284  * ==========================================================================
1285  */
1286 
1287 static zio_t *
1288 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1289 {
1290 	if (gn != NULL)
1291 		return (pio);
1292 
1293 	return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp),
1294 	    NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1295 	    &pio->io_bookmark));
1296 }
1297 
1298 zio_t *
1299 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1300 {
1301 	zio_t *zio;
1302 
1303 	if (gn != NULL) {
1304 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1305 		    gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority,
1306 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1307 		/*
1308 		 * As we rewrite each gang header, the pipeline will compute
1309 		 * a new gang block header checksum for it; but no one will
1310 		 * compute a new data checksum, so we do that here.  The one
1311 		 * exception is the gang leader: the pipeline already computed
1312 		 * its data checksum because that stage precedes gang assembly.
1313 		 * (Presently, nothing actually uses interior data checksums;
1314 		 * this is just good hygiene.)
1315 		 */
1316 		if (gn != pio->io_logical->io_gang_tree) {
1317 			zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
1318 			    data, BP_GET_PSIZE(bp));
1319 		}
1320 	} else {
1321 		zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1322 		    data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority,
1323 		    ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1324 	}
1325 
1326 	return (zio);
1327 }
1328 
1329 /* ARGSUSED */
1330 zio_t *
1331 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1332 {
1333 	return (zio_free(pio, pio->io_spa, pio->io_txg, bp,
1334 	    NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
1335 }
1336 
1337 /* ARGSUSED */
1338 zio_t *
1339 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1340 {
1341 	return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
1342 	    NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
1343 }
1344 
1345 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
1346 	NULL,
1347 	zio_read_gang,
1348 	zio_rewrite_gang,
1349 	zio_free_gang,
1350 	zio_claim_gang,
1351 	NULL
1352 };
1353 
1354 static void zio_gang_tree_assemble_done(zio_t *zio);
1355 
1356 static zio_gang_node_t *
1357 zio_gang_node_alloc(zio_gang_node_t **gnpp)
1358 {
1359 	zio_gang_node_t *gn;
1360 
1361 	ASSERT(*gnpp == NULL);
1362 
1363 	gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
1364 	gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
1365 	*gnpp = gn;
1366 
1367 	return (gn);
1368 }
1369 
1370 static void
1371 zio_gang_node_free(zio_gang_node_t **gnpp)
1372 {
1373 	zio_gang_node_t *gn = *gnpp;
1374 
1375 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
1376 		ASSERT(gn->gn_child[g] == NULL);
1377 
1378 	zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
1379 	kmem_free(gn, sizeof (*gn));
1380 	*gnpp = NULL;
1381 }
1382 
1383 static void
1384 zio_gang_tree_free(zio_gang_node_t **gnpp)
1385 {
1386 	zio_gang_node_t *gn = *gnpp;
1387 
1388 	if (gn == NULL)
1389 		return;
1390 
1391 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
1392 		zio_gang_tree_free(&gn->gn_child[g]);
1393 
1394 	zio_gang_node_free(gnpp);
1395 }
1396 
1397 static void
1398 zio_gang_tree_assemble(zio_t *lio, blkptr_t *bp, zio_gang_node_t **gnpp)
1399 {
1400 	zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
1401 
1402 	ASSERT(lio->io_logical == lio);
1403 	ASSERT(BP_IS_GANG(bp));
1404 
1405 	zio_nowait(zio_read(lio, lio->io_spa, bp, gn->gn_gbh,
1406 	    SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn,
1407 	    lio->io_priority, ZIO_GANG_CHILD_FLAGS(lio), &lio->io_bookmark));
1408 }
1409 
1410 static void
1411 zio_gang_tree_assemble_done(zio_t *zio)
1412 {
1413 	zio_t *lio = zio->io_logical;
1414 	zio_gang_node_t *gn = zio->io_private;
1415 	blkptr_t *bp = zio->io_bp;
1416 	zio_t *pio = zio_unique_parent(zio);
1417 
1418 	ASSERT(pio == lio);
1419 	ASSERT(zio_walk_children(zio) == NULL);
1420 
1421 	if (zio->io_error)
1422 		return;
1423 
1424 	if (BP_SHOULD_BYTESWAP(bp))
1425 		byteswap_uint64_array(zio->io_data, zio->io_size);
1426 
1427 	ASSERT(zio->io_data == gn->gn_gbh);
1428 	ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
1429 	ASSERT(gn->gn_gbh->zg_tail.zbt_magic == ZBT_MAGIC);
1430 
1431 	for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1432 		blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1433 		if (!BP_IS_GANG(gbp))
1434 			continue;
1435 		zio_gang_tree_assemble(lio, gbp, &gn->gn_child[g]);
1436 	}
1437 }
1438 
1439 static void
1440 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data)
1441 {
1442 	zio_t *lio = pio->io_logical;
1443 	zio_t *zio;
1444 
1445 	ASSERT(BP_IS_GANG(bp) == !!gn);
1446 	ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(lio->io_bp));
1447 	ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == lio->io_gang_tree);
1448 
1449 	/*
1450 	 * If you're a gang header, your data is in gn->gn_gbh.
1451 	 * If you're a gang member, your data is in 'data' and gn == NULL.
1452 	 */
1453 	zio = zio_gang_issue_func[lio->io_type](pio, bp, gn, data);
1454 
1455 	if (gn != NULL) {
1456 		ASSERT(gn->gn_gbh->zg_tail.zbt_magic == ZBT_MAGIC);
1457 
1458 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1459 			blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1460 			if (BP_IS_HOLE(gbp))
1461 				continue;
1462 			zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data);
1463 			data = (char *)data + BP_GET_PSIZE(gbp);
1464 		}
1465 	}
1466 
1467 	if (gn == lio->io_gang_tree)
1468 		ASSERT3P((char *)lio->io_data + lio->io_size, ==, data);
1469 
1470 	if (zio != pio)
1471 		zio_nowait(zio);
1472 }
1473 
1474 static int
1475 zio_gang_assemble(zio_t *zio)
1476 {
1477 	blkptr_t *bp = zio->io_bp;
1478 
1479 	ASSERT(BP_IS_GANG(bp) && zio == zio->io_logical);
1480 
1481 	zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
1482 
1483 	return (ZIO_PIPELINE_CONTINUE);
1484 }
1485 
1486 static int
1487 zio_gang_issue(zio_t *zio)
1488 {
1489 	zio_t *lio = zio->io_logical;
1490 	blkptr_t *bp = zio->io_bp;
1491 
1492 	if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
1493 		return (ZIO_PIPELINE_STOP);
1494 
1495 	ASSERT(BP_IS_GANG(bp) && zio == lio);
1496 
1497 	if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
1498 		zio_gang_tree_issue(lio, lio->io_gang_tree, bp, lio->io_data);
1499 	else
1500 		zio_gang_tree_free(&lio->io_gang_tree);
1501 
1502 	zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1503 
1504 	return (ZIO_PIPELINE_CONTINUE);
1505 }
1506 
1507 static void
1508 zio_write_gang_member_ready(zio_t *zio)
1509 {
1510 	zio_t *pio = zio_unique_parent(zio);
1511 	zio_t *lio = zio->io_logical;
1512 	dva_t *cdva = zio->io_bp->blk_dva;
1513 	dva_t *pdva = pio->io_bp->blk_dva;
1514 	uint64_t asize;
1515 
1516 	if (BP_IS_HOLE(zio->io_bp))
1517 		return;
1518 
1519 	ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
1520 
1521 	ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
1522 	ASSERT3U(zio->io_prop.zp_ndvas, ==, lio->io_prop.zp_ndvas);
1523 	ASSERT3U(zio->io_prop.zp_ndvas, <=, BP_GET_NDVAS(zio->io_bp));
1524 	ASSERT3U(pio->io_prop.zp_ndvas, <=, BP_GET_NDVAS(pio->io_bp));
1525 	ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
1526 
1527 	mutex_enter(&pio->io_lock);
1528 	for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
1529 		ASSERT(DVA_GET_GANG(&pdva[d]));
1530 		asize = DVA_GET_ASIZE(&pdva[d]);
1531 		asize += DVA_GET_ASIZE(&cdva[d]);
1532 		DVA_SET_ASIZE(&pdva[d], asize);
1533 	}
1534 	mutex_exit(&pio->io_lock);
1535 }
1536 
1537 static int
1538 zio_write_gang_block(zio_t *pio)
1539 {
1540 	spa_t *spa = pio->io_spa;
1541 	blkptr_t *bp = pio->io_bp;
1542 	zio_t *lio = pio->io_logical;
1543 	zio_t *zio;
1544 	zio_gang_node_t *gn, **gnpp;
1545 	zio_gbh_phys_t *gbh;
1546 	uint64_t txg = pio->io_txg;
1547 	uint64_t resid = pio->io_size;
1548 	uint64_t lsize;
1549 	int ndvas = lio->io_prop.zp_ndvas;
1550 	int gbh_ndvas = MIN(ndvas + 1, spa_max_replication(spa));
1551 	zio_prop_t zp;
1552 	int error;
1553 
1554 	error = metaslab_alloc(spa, spa->spa_normal_class, SPA_GANGBLOCKSIZE,
1555 	    bp, gbh_ndvas, txg, pio == lio ? NULL : lio->io_bp,
1556 	    METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER);
1557 	if (error) {
1558 		pio->io_error = error;
1559 		return (ZIO_PIPELINE_CONTINUE);
1560 	}
1561 
1562 	if (pio == lio) {
1563 		gnpp = &lio->io_gang_tree;
1564 	} else {
1565 		gnpp = pio->io_private;
1566 		ASSERT(pio->io_ready == zio_write_gang_member_ready);
1567 	}
1568 
1569 	gn = zio_gang_node_alloc(gnpp);
1570 	gbh = gn->gn_gbh;
1571 	bzero(gbh, SPA_GANGBLOCKSIZE);
1572 
1573 	/*
1574 	 * Create the gang header.
1575 	 */
1576 	zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL,
1577 	    pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1578 
1579 	/*
1580 	 * Create and nowait the gang children.
1581 	 */
1582 	for (int g = 0; resid != 0; resid -= lsize, g++) {
1583 		lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
1584 		    SPA_MINBLOCKSIZE);
1585 		ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
1586 
1587 		zp.zp_checksum = lio->io_prop.zp_checksum;
1588 		zp.zp_compress = ZIO_COMPRESS_OFF;
1589 		zp.zp_type = DMU_OT_NONE;
1590 		zp.zp_level = 0;
1591 		zp.zp_ndvas = lio->io_prop.zp_ndvas;
1592 
1593 		zio_nowait(zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
1594 		    (char *)pio->io_data + (pio->io_size - resid), lsize, &zp,
1595 		    zio_write_gang_member_ready, NULL, &gn->gn_child[g],
1596 		    pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1597 		    &pio->io_bookmark));
1598 	}
1599 
1600 	/*
1601 	 * Set pio's pipeline to just wait for zio to finish.
1602 	 */
1603 	pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1604 
1605 	zio_nowait(zio);
1606 
1607 	return (ZIO_PIPELINE_CONTINUE);
1608 }
1609 
1610 /*
1611  * ==========================================================================
1612  * Allocate and free blocks
1613  * ==========================================================================
1614  */
1615 
1616 static int
1617 zio_dva_allocate(zio_t *zio)
1618 {
1619 	spa_t *spa = zio->io_spa;
1620 	metaslab_class_t *mc = spa->spa_normal_class;
1621 	blkptr_t *bp = zio->io_bp;
1622 	int error;
1623 
1624 	ASSERT(BP_IS_HOLE(bp));
1625 	ASSERT3U(BP_GET_NDVAS(bp), ==, 0);
1626 	ASSERT3U(zio->io_prop.zp_ndvas, >, 0);
1627 	ASSERT3U(zio->io_prop.zp_ndvas, <=, spa_max_replication(spa));
1628 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
1629 
1630 	error = metaslab_alloc(spa, mc, zio->io_size, bp,
1631 	    zio->io_prop.zp_ndvas, zio->io_txg, NULL, 0);
1632 
1633 	if (error) {
1634 		if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
1635 			return (zio_write_gang_block(zio));
1636 		zio->io_error = error;
1637 	}
1638 
1639 	return (ZIO_PIPELINE_CONTINUE);
1640 }
1641 
1642 static int
1643 zio_dva_free(zio_t *zio)
1644 {
1645 	metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
1646 
1647 	return (ZIO_PIPELINE_CONTINUE);
1648 }
1649 
1650 static int
1651 zio_dva_claim(zio_t *zio)
1652 {
1653 	int error;
1654 
1655 	error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
1656 	if (error)
1657 		zio->io_error = error;
1658 
1659 	return (ZIO_PIPELINE_CONTINUE);
1660 }
1661 
1662 /*
1663  * Undo an allocation.  This is used by zio_done() when an I/O fails
1664  * and we want to give back the block we just allocated.
1665  * This handles both normal blocks and gang blocks.
1666  */
1667 static void
1668 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
1669 {
1670 	spa_t *spa = zio->io_spa;
1671 	boolean_t now = !(zio->io_flags & ZIO_FLAG_IO_REWRITE);
1672 
1673 	ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
1674 
1675 	if (zio->io_bp == bp && !now) {
1676 		/*
1677 		 * This is a rewrite for sync-to-convergence.
1678 		 * We can't do a metaslab_free(NOW) because bp wasn't allocated
1679 		 * during this sync pass, which means that metaslab_sync()
1680 		 * already committed the allocation.
1681 		 */
1682 		ASSERT(DVA_EQUAL(BP_IDENTITY(bp),
1683 		    BP_IDENTITY(&zio->io_bp_orig)));
1684 		ASSERT(spa_sync_pass(spa) > 1);
1685 
1686 		if (BP_IS_GANG(bp) && gn == NULL) {
1687 			/*
1688 			 * This is a gang leader whose gang header(s) we
1689 			 * couldn't read now, so defer the free until later.
1690 			 * The block should still be intact because without
1691 			 * the headers, we'd never even start the rewrite.
1692 			 */
1693 			bplist_enqueue_deferred(&spa->spa_sync_bplist, bp);
1694 			return;
1695 		}
1696 	}
1697 
1698 	if (!BP_IS_HOLE(bp))
1699 		metaslab_free(spa, bp, bp->blk_birth, now);
1700 
1701 	if (gn != NULL) {
1702 		for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1703 			zio_dva_unallocate(zio, gn->gn_child[g],
1704 			    &gn->gn_gbh->zg_blkptr[g]);
1705 		}
1706 	}
1707 }
1708 
1709 /*
1710  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
1711  */
1712 int
1713 zio_alloc_blk(spa_t *spa, uint64_t size, blkptr_t *new_bp, blkptr_t *old_bp,
1714     uint64_t txg)
1715 {
1716 	int error;
1717 
1718 	error = metaslab_alloc(spa, spa->spa_log_class, size,
1719 	    new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID);
1720 
1721 	if (error)
1722 		error = metaslab_alloc(spa, spa->spa_normal_class, size,
1723 		    new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID);
1724 
1725 	if (error == 0) {
1726 		BP_SET_LSIZE(new_bp, size);
1727 		BP_SET_PSIZE(new_bp, size);
1728 		BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
1729 		BP_SET_CHECKSUM(new_bp, ZIO_CHECKSUM_ZILOG);
1730 		BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
1731 		BP_SET_LEVEL(new_bp, 0);
1732 		BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
1733 	}
1734 
1735 	return (error);
1736 }
1737 
1738 /*
1739  * Free an intent log block.  We know it can't be a gang block, so there's
1740  * nothing to do except metaslab_free() it.
1741  */
1742 void
1743 zio_free_blk(spa_t *spa, blkptr_t *bp, uint64_t txg)
1744 {
1745 	ASSERT(!BP_IS_GANG(bp));
1746 
1747 	metaslab_free(spa, bp, txg, B_FALSE);
1748 }
1749 
1750 /*
1751  * ==========================================================================
1752  * Read and write to physical devices
1753  * ==========================================================================
1754  */
1755 static int
1756 zio_vdev_io_start(zio_t *zio)
1757 {
1758 	vdev_t *vd = zio->io_vd;
1759 	uint64_t align;
1760 	spa_t *spa = zio->io_spa;
1761 
1762 	ASSERT(zio->io_error == 0);
1763 	ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
1764 
1765 	if (vd == NULL) {
1766 		if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
1767 			spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
1768 
1769 		/*
1770 		 * The mirror_ops handle multiple DVAs in a single BP.
1771 		 */
1772 		return (vdev_mirror_ops.vdev_op_io_start(zio));
1773 	}
1774 
1775 	align = 1ULL << vd->vdev_top->vdev_ashift;
1776 
1777 	if (P2PHASE(zio->io_size, align) != 0) {
1778 		uint64_t asize = P2ROUNDUP(zio->io_size, align);
1779 		char *abuf = zio_buf_alloc(asize);
1780 		ASSERT(vd == vd->vdev_top);
1781 		if (zio->io_type == ZIO_TYPE_WRITE) {
1782 			bcopy(zio->io_data, abuf, zio->io_size);
1783 			bzero(abuf + zio->io_size, asize - zio->io_size);
1784 		}
1785 		zio_push_transform(zio, abuf, asize, asize, zio_subblock);
1786 	}
1787 
1788 	ASSERT(P2PHASE(zio->io_offset, align) == 0);
1789 	ASSERT(P2PHASE(zio->io_size, align) == 0);
1790 	ASSERT(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
1791 
1792 	/*
1793 	 * If this is a repair I/O, and there's no self-healing involved --
1794 	 * that is, we're just resilvering what we expect to resilver --
1795 	 * then don't do the I/O unless zio's txg is actually in vd's DTL.
1796 	 * This prevents spurious resilvering with nested replication.
1797 	 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
1798 	 * A is out of date, we'll read from C+D, then use the data to
1799 	 * resilver A+B -- but we don't actually want to resilver B, just A.
1800 	 * The top-level mirror has no way to know this, so instead we just
1801 	 * discard unnecessary repairs as we work our way down the vdev tree.
1802 	 * The same logic applies to any form of nested replication:
1803 	 * ditto + mirror, RAID-Z + replacing, etc.  This covers them all.
1804 	 */
1805 	if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
1806 	    !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
1807 	    zio->io_txg != 0 &&	/* not a delegated i/o */
1808 	    !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
1809 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1810 		zio_vdev_io_bypass(zio);
1811 		return (ZIO_PIPELINE_CONTINUE);
1812 	}
1813 
1814 	if (vd->vdev_ops->vdev_op_leaf &&
1815 	    (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) {
1816 
1817 		if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
1818 			return (ZIO_PIPELINE_CONTINUE);
1819 
1820 		if ((zio = vdev_queue_io(zio)) == NULL)
1821 			return (ZIO_PIPELINE_STOP);
1822 
1823 		if (!vdev_accessible(vd, zio)) {
1824 			zio->io_error = ENXIO;
1825 			zio_interrupt(zio);
1826 			return (ZIO_PIPELINE_STOP);
1827 		}
1828 	}
1829 
1830 	return (vd->vdev_ops->vdev_op_io_start(zio));
1831 }
1832 
1833 static int
1834 zio_vdev_io_done(zio_t *zio)
1835 {
1836 	vdev_t *vd = zio->io_vd;
1837 	vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
1838 	boolean_t unexpected_error = B_FALSE;
1839 
1840 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
1841 		return (ZIO_PIPELINE_STOP);
1842 
1843 	ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
1844 
1845 	if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
1846 
1847 		vdev_queue_io_done(zio);
1848 
1849 		if (zio->io_type == ZIO_TYPE_WRITE)
1850 			vdev_cache_write(zio);
1851 
1852 		if (zio_injection_enabled && zio->io_error == 0)
1853 			zio->io_error = zio_handle_device_injection(vd, EIO);
1854 
1855 		if (zio_injection_enabled && zio->io_error == 0)
1856 			zio->io_error = zio_handle_label_injection(zio, EIO);
1857 
1858 		if (zio->io_error) {
1859 			if (!vdev_accessible(vd, zio)) {
1860 				zio->io_error = ENXIO;
1861 			} else {
1862 				unexpected_error = B_TRUE;
1863 			}
1864 		}
1865 	}
1866 
1867 	ops->vdev_op_io_done(zio);
1868 
1869 	if (unexpected_error)
1870 		VERIFY(vdev_probe(vd, zio) == NULL);
1871 
1872 	return (ZIO_PIPELINE_CONTINUE);
1873 }
1874 
1875 static int
1876 zio_vdev_io_assess(zio_t *zio)
1877 {
1878 	vdev_t *vd = zio->io_vd;
1879 
1880 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
1881 		return (ZIO_PIPELINE_STOP);
1882 
1883 	if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
1884 		spa_config_exit(zio->io_spa, SCL_ZIO, zio);
1885 
1886 	if (zio->io_vsd != NULL) {
1887 		zio->io_vsd_free(zio);
1888 		zio->io_vsd = NULL;
1889 	}
1890 
1891 	if (zio_injection_enabled && zio->io_error == 0)
1892 		zio->io_error = zio_handle_fault_injection(zio, EIO);
1893 
1894 	/*
1895 	 * If the I/O failed, determine whether we should attempt to retry it.
1896 	 */
1897 	if (zio->io_error && vd == NULL &&
1898 	    !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
1899 		ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE));	/* not a leaf */
1900 		ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS));	/* not a leaf */
1901 		zio->io_error = 0;
1902 		zio->io_flags |= ZIO_FLAG_IO_RETRY |
1903 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
1904 		zio->io_stage = ZIO_STAGE_VDEV_IO_START - 1;
1905 		zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE);
1906 		return (ZIO_PIPELINE_STOP);
1907 	}
1908 
1909 	/*
1910 	 * If we got an error on a leaf device, convert it to ENXIO
1911 	 * if the device is not accessible at all.
1912 	 */
1913 	if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
1914 	    !vdev_accessible(vd, zio))
1915 		zio->io_error = ENXIO;
1916 
1917 	/*
1918 	 * If we can't write to an interior vdev (mirror or RAID-Z),
1919 	 * set vdev_cant_write so that we stop trying to allocate from it.
1920 	 */
1921 	if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
1922 	    vd != NULL && !vd->vdev_ops->vdev_op_leaf)
1923 		vd->vdev_cant_write = B_TRUE;
1924 
1925 	if (zio->io_error)
1926 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1927 
1928 	return (ZIO_PIPELINE_CONTINUE);
1929 }
1930 
1931 void
1932 zio_vdev_io_reissue(zio_t *zio)
1933 {
1934 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1935 	ASSERT(zio->io_error == 0);
1936 
1937 	zio->io_stage--;
1938 }
1939 
1940 void
1941 zio_vdev_io_redone(zio_t *zio)
1942 {
1943 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
1944 
1945 	zio->io_stage--;
1946 }
1947 
1948 void
1949 zio_vdev_io_bypass(zio_t *zio)
1950 {
1951 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1952 	ASSERT(zio->io_error == 0);
1953 
1954 	zio->io_flags |= ZIO_FLAG_IO_BYPASS;
1955 	zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS - 1;
1956 }
1957 
1958 /*
1959  * ==========================================================================
1960  * Generate and verify checksums
1961  * ==========================================================================
1962  */
1963 static int
1964 zio_checksum_generate(zio_t *zio)
1965 {
1966 	blkptr_t *bp = zio->io_bp;
1967 	enum zio_checksum checksum;
1968 
1969 	if (bp == NULL) {
1970 		/*
1971 		 * This is zio_write_phys().
1972 		 * We're either generating a label checksum, or none at all.
1973 		 */
1974 		checksum = zio->io_prop.zp_checksum;
1975 
1976 		if (checksum == ZIO_CHECKSUM_OFF)
1977 			return (ZIO_PIPELINE_CONTINUE);
1978 
1979 		ASSERT(checksum == ZIO_CHECKSUM_LABEL);
1980 	} else {
1981 		if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
1982 			ASSERT(!IO_IS_ALLOCATING(zio));
1983 			checksum = ZIO_CHECKSUM_GANG_HEADER;
1984 		} else {
1985 			checksum = BP_GET_CHECKSUM(bp);
1986 		}
1987 	}
1988 
1989 	zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size);
1990 
1991 	return (ZIO_PIPELINE_CONTINUE);
1992 }
1993 
1994 static int
1995 zio_checksum_verify(zio_t *zio)
1996 {
1997 	blkptr_t *bp = zio->io_bp;
1998 	int error;
1999 
2000 	if (bp == NULL) {
2001 		/*
2002 		 * This is zio_read_phys().
2003 		 * We're either verifying a label checksum, or nothing at all.
2004 		 */
2005 		if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
2006 			return (ZIO_PIPELINE_CONTINUE);
2007 
2008 		ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
2009 	}
2010 
2011 	if ((error = zio_checksum_error(zio)) != 0) {
2012 		zio->io_error = error;
2013 		if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2014 			zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM,
2015 			    zio->io_spa, zio->io_vd, zio, 0, 0);
2016 		}
2017 	}
2018 
2019 	return (ZIO_PIPELINE_CONTINUE);
2020 }
2021 
2022 /*
2023  * Called by RAID-Z to ensure we don't compute the checksum twice.
2024  */
2025 void
2026 zio_checksum_verified(zio_t *zio)
2027 {
2028 	zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY);
2029 }
2030 
2031 /*
2032  * ==========================================================================
2033  * Error rank.  Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
2034  * An error of 0 indictes success.  ENXIO indicates whole-device failure,
2035  * which may be transient (e.g. unplugged) or permament.  ECKSUM and EIO
2036  * indicate errors that are specific to one I/O, and most likely permanent.
2037  * Any other error is presumed to be worse because we weren't expecting it.
2038  * ==========================================================================
2039  */
2040 int
2041 zio_worst_error(int e1, int e2)
2042 {
2043 	static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
2044 	int r1, r2;
2045 
2046 	for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
2047 		if (e1 == zio_error_rank[r1])
2048 			break;
2049 
2050 	for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
2051 		if (e2 == zio_error_rank[r2])
2052 			break;
2053 
2054 	return (r1 > r2 ? e1 : e2);
2055 }
2056 
2057 /*
2058  * ==========================================================================
2059  * I/O completion
2060  * ==========================================================================
2061  */
2062 static int
2063 zio_ready(zio_t *zio)
2064 {
2065 	blkptr_t *bp = zio->io_bp;
2066 	zio_t *pio, *pio_next;
2067 
2068 	if (zio->io_ready) {
2069 		if (BP_IS_GANG(bp) &&
2070 		    zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY))
2071 			return (ZIO_PIPELINE_STOP);
2072 
2073 		ASSERT(IO_IS_ALLOCATING(zio));
2074 		ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
2075 		ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
2076 
2077 		zio->io_ready(zio);
2078 	}
2079 
2080 	if (bp != NULL && bp != &zio->io_bp_copy)
2081 		zio->io_bp_copy = *bp;
2082 
2083 	if (zio->io_error)
2084 		zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2085 
2086 	mutex_enter(&zio->io_lock);
2087 	zio->io_state[ZIO_WAIT_READY] = 1;
2088 	pio = zio_walk_parents(zio);
2089 	mutex_exit(&zio->io_lock);
2090 
2091 	/*
2092 	 * As we notify zio's parents, new parents could be added.
2093 	 * New parents go to the head of zio's io_parent_list, however,
2094 	 * so we will (correctly) not notify them.  The remainder of zio's
2095 	 * io_parent_list, from 'pio_next' onward, cannot change because
2096 	 * all parents must wait for us to be done before they can be done.
2097 	 */
2098 	for (; pio != NULL; pio = pio_next) {
2099 		pio_next = zio_walk_parents(zio);
2100 		zio_notify_parent(pio, zio, ZIO_WAIT_READY);
2101 	}
2102 
2103 	return (ZIO_PIPELINE_CONTINUE);
2104 }
2105 
2106 static int
2107 zio_done(zio_t *zio)
2108 {
2109 	spa_t *spa = zio->io_spa;
2110 	zio_t *lio = zio->io_logical;
2111 	blkptr_t *bp = zio->io_bp;
2112 	vdev_t *vd = zio->io_vd;
2113 	uint64_t psize = zio->io_size;
2114 	zio_t *pio, *pio_next;
2115 
2116 	/*
2117 	 * If our of children haven't all completed,
2118 	 * wait for them and then repeat this pipeline stage.
2119 	 */
2120 	if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
2121 	    zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
2122 	    zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
2123 		return (ZIO_PIPELINE_STOP);
2124 
2125 	for (int c = 0; c < ZIO_CHILD_TYPES; c++)
2126 		for (int w = 0; w < ZIO_WAIT_TYPES; w++)
2127 			ASSERT(zio->io_children[c][w] == 0);
2128 
2129 	if (bp != NULL) {
2130 		ASSERT(bp->blk_pad[0] == 0);
2131 		ASSERT(bp->blk_pad[1] == 0);
2132 		ASSERT(bp->blk_pad[2] == 0);
2133 		ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
2134 		    (bp == zio_unique_parent(zio)->io_bp));
2135 		if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
2136 		    !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
2137 			ASSERT(!BP_SHOULD_BYTESWAP(bp));
2138 			ASSERT3U(zio->io_prop.zp_ndvas, <=, BP_GET_NDVAS(bp));
2139 			ASSERT(BP_COUNT_GANG(bp) == 0 ||
2140 			    (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
2141 		}
2142 	}
2143 
2144 	/*
2145 	 * If there were child vdev or gang errors, they apply to us now.
2146 	 */
2147 	zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
2148 	zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
2149 
2150 	zio_pop_transforms(zio);	/* note: may set zio->io_error */
2151 
2152 	vdev_stat_update(zio, psize);
2153 
2154 	if (zio->io_error) {
2155 		/*
2156 		 * If this I/O is attached to a particular vdev,
2157 		 * generate an error message describing the I/O failure
2158 		 * at the block level.  We ignore these errors if the
2159 		 * device is currently unavailable.
2160 		 */
2161 		if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
2162 			zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0);
2163 
2164 		if ((zio->io_error == EIO ||
2165 		    !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) && zio == lio) {
2166 			/*
2167 			 * For logical I/O requests, tell the SPA to log the
2168 			 * error and generate a logical data ereport.
2169 			 */
2170 			spa_log_error(spa, zio);
2171 			zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio,
2172 			    0, 0);
2173 		}
2174 	}
2175 
2176 	if (zio->io_error && zio == lio) {
2177 		/*
2178 		 * Determine whether zio should be reexecuted.  This will
2179 		 * propagate all the way to the root via zio_notify_parent().
2180 		 */
2181 		ASSERT(vd == NULL && bp != NULL);
2182 
2183 		if (IO_IS_ALLOCATING(zio))
2184 			if (zio->io_error != ENOSPC)
2185 				zio->io_reexecute |= ZIO_REEXECUTE_NOW;
2186 			else
2187 				zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2188 
2189 		if ((zio->io_type == ZIO_TYPE_READ ||
2190 		    zio->io_type == ZIO_TYPE_FREE) &&
2191 		    zio->io_error == ENXIO &&
2192 		    spa->spa_load_state == SPA_LOAD_NONE &&
2193 		    spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
2194 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2195 
2196 		if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
2197 			zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2198 	}
2199 
2200 	/*
2201 	 * If there were logical child errors, they apply to us now.
2202 	 * We defer this until now to avoid conflating logical child
2203 	 * errors with errors that happened to the zio itself when
2204 	 * updating vdev stats and reporting FMA events above.
2205 	 */
2206 	zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
2207 
2208 	if (zio->io_reexecute && !(zio->io_flags & ZIO_FLAG_GODFATHER)) {
2209 		/*
2210 		 * This is a logical I/O that wants to reexecute.
2211 		 *
2212 		 * Reexecute is top-down.  When an i/o fails, if it's not
2213 		 * the root, it simply notifies its parent and sticks around.
2214 		 * The parent, seeing that it still has children in zio_done(),
2215 		 * does the same.  This percolates all the way up to the root.
2216 		 * The root i/o will reexecute or suspend the entire tree.
2217 		 *
2218 		 * This approach ensures that zio_reexecute() honors
2219 		 * all the original i/o dependency relationships, e.g.
2220 		 * parents not executing until children are ready.
2221 		 */
2222 		ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2223 
2224 		if (IO_IS_ALLOCATING(zio))
2225 			zio_dva_unallocate(zio, zio->io_gang_tree, bp);
2226 
2227 		zio_gang_tree_free(&zio->io_gang_tree);
2228 
2229 		mutex_enter(&zio->io_lock);
2230 		zio->io_state[ZIO_WAIT_DONE] = 1;
2231 		mutex_exit(&zio->io_lock);
2232 
2233 		/*
2234 		 * "The Godfather" I/O monitors its children but is
2235 		 * not a true parent to them. It will track them through
2236 		 * the pipeline but severs its ties whenever they get into
2237 		 * trouble (e.g. suspended). This allows "The Godfather"
2238 		 * I/O to return status without blocking.
2239 		 */
2240 		for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
2241 			zio_link_t *zl = zio->io_walk_link;
2242 			pio_next = zio_walk_parents(zio);
2243 
2244 			if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
2245 			    (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
2246 				zio_remove_child(pio, zio, zl);
2247 				zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
2248 			}
2249 		}
2250 
2251 		if ((pio = zio_unique_parent(zio)) != NULL) {
2252 			/*
2253 			 * We're not a root i/o, so there's nothing to do
2254 			 * but notify our parent.  Don't propagate errors
2255 			 * upward since we haven't permanently failed yet.
2256 			 */
2257 			zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
2258 			zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
2259 		} else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
2260 			/*
2261 			 * We'd fail again if we reexecuted now, so suspend
2262 			 * until conditions improve (e.g. device comes online).
2263 			 */
2264 			zio_suspend(spa, zio);
2265 		} else {
2266 			/*
2267 			 * Reexecution is potentially a huge amount of work.
2268 			 * Hand it off to the otherwise-unused claim taskq.
2269 			 */
2270 			(void) taskq_dispatch(
2271 			    spa->spa_zio_taskq[ZIO_TYPE_CLAIM][ZIO_TASKQ_ISSUE],
2272 			    (task_func_t *)zio_reexecute, zio, TQ_SLEEP);
2273 		}
2274 		return (ZIO_PIPELINE_STOP);
2275 	}
2276 
2277 	ASSERT(zio_walk_children(zio) == NULL);
2278 	ASSERT(zio->io_reexecute == 0 || (zio->io_flags & ZIO_FLAG_GODFATHER));
2279 	ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
2280 
2281 	/*
2282 	 * It is the responsibility of the done callback to ensure that this
2283 	 * particular zio is no longer discoverable for adoption, and as
2284 	 * such, cannot acquire any new parents.
2285 	 */
2286 	if (zio->io_done)
2287 		zio->io_done(zio);
2288 
2289 	zio_gang_tree_free(&zio->io_gang_tree);
2290 
2291 	mutex_enter(&zio->io_lock);
2292 	zio->io_state[ZIO_WAIT_DONE] = 1;
2293 	mutex_exit(&zio->io_lock);
2294 
2295 	for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
2296 		zio_link_t *zl = zio->io_walk_link;
2297 		pio_next = zio_walk_parents(zio);
2298 		zio_remove_child(pio, zio, zl);
2299 		zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
2300 	}
2301 
2302 	if (zio->io_waiter != NULL) {
2303 		mutex_enter(&zio->io_lock);
2304 		zio->io_executor = NULL;
2305 		cv_broadcast(&zio->io_cv);
2306 		mutex_exit(&zio->io_lock);
2307 	} else {
2308 		zio_destroy(zio);
2309 	}
2310 
2311 	return (ZIO_PIPELINE_STOP);
2312 }
2313 
2314 /*
2315  * ==========================================================================
2316  * I/O pipeline definition
2317  * ==========================================================================
2318  */
2319 static zio_pipe_stage_t *zio_pipeline[ZIO_STAGES] = {
2320 	NULL,
2321 	zio_issue_async,
2322 	zio_read_bp_init,
2323 	zio_write_bp_init,
2324 	zio_checksum_generate,
2325 	zio_gang_assemble,
2326 	zio_gang_issue,
2327 	zio_dva_allocate,
2328 	zio_dva_free,
2329 	zio_dva_claim,
2330 	zio_ready,
2331 	zio_vdev_io_start,
2332 	zio_vdev_io_done,
2333 	zio_vdev_io_assess,
2334 	zio_checksum_verify,
2335 	zio_done
2336 };
2337