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