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