xref: /illumos-gate/usr/src/uts/common/fs/zfs/zio.c (revision d63d470b66194d40e74d5a669bd21e215bd63fc2)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
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 
38 /*
39  * ==========================================================================
40  * I/O priority table
41  * ==========================================================================
42  */
43 uint8_t zio_priority_table[ZIO_PRIORITY_TABLE_SIZE] = {
44 	0,	/* ZIO_PRIORITY_NOW		*/
45 	0,	/* ZIO_PRIORITY_SYNC_READ	*/
46 	0,	/* ZIO_PRIORITY_SYNC_WRITE	*/
47 	6,	/* ZIO_PRIORITY_ASYNC_READ	*/
48 	4,	/* ZIO_PRIORITY_ASYNC_WRITE	*/
49 	4,	/* ZIO_PRIORITY_FREE		*/
50 	0,	/* ZIO_PRIORITY_CACHE_FILL	*/
51 	0,	/* ZIO_PRIORITY_LOG_WRITE	*/
52 	10,	/* ZIO_PRIORITY_RESILVER	*/
53 	20,	/* ZIO_PRIORITY_SCRUB		*/
54 };
55 
56 /*
57  * ==========================================================================
58  * I/O type descriptions
59  * ==========================================================================
60  */
61 char *zio_type_name[ZIO_TYPES] = {
62 	"null", "read", "write", "free", "claim", "ioctl" };
63 
64 /* At or above this size, force gang blocking - for testing */
65 uint64_t zio_gang_bang = SPA_MAXBLOCKSIZE + 1;
66 
67 /* Force an allocation failure when non-zero */
68 uint16_t zio_zil_fail_shift = 0;
69 
70 typedef struct zio_sync_pass {
71 	int	zp_defer_free;		/* defer frees after this pass */
72 	int	zp_dontcompress;	/* don't compress after this pass */
73 	int	zp_rewrite;		/* rewrite new bps after this pass */
74 } zio_sync_pass_t;
75 
76 zio_sync_pass_t zio_sync_pass = {
77 	1,	/* zp_defer_free */
78 	4,	/* zp_dontcompress */
79 	1,	/* zp_rewrite */
80 };
81 
82 /*
83  * ==========================================================================
84  * I/O kmem caches
85  * ==========================================================================
86  */
87 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
88 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
89 
90 #ifdef _KERNEL
91 extern vmem_t *zio_alloc_arena;
92 #endif
93 
94 void
95 zio_init(void)
96 {
97 	size_t c;
98 	vmem_t *data_alloc_arena = NULL;
99 
100 #ifdef _KERNEL
101 	data_alloc_arena = zio_alloc_arena;
102 #endif
103 
104 	/*
105 	 * For small buffers, we want a cache for each multiple of
106 	 * SPA_MINBLOCKSIZE.  For medium-size buffers, we want a cache
107 	 * for each quarter-power of 2.  For large buffers, we want
108 	 * a cache for each multiple of PAGESIZE.
109 	 */
110 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
111 		size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
112 		size_t p2 = size;
113 		size_t align = 0;
114 
115 		while (p2 & (p2 - 1))
116 			p2 &= p2 - 1;
117 
118 		if (size <= 4 * SPA_MINBLOCKSIZE) {
119 			align = SPA_MINBLOCKSIZE;
120 		} else if (P2PHASE(size, PAGESIZE) == 0) {
121 			align = PAGESIZE;
122 		} else if (P2PHASE(size, p2 >> 2) == 0) {
123 			align = p2 >> 2;
124 		}
125 
126 		if (align != 0) {
127 			char name[36];
128 			(void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
129 			zio_buf_cache[c] = kmem_cache_create(name, size,
130 			    align, NULL, NULL, NULL, NULL, NULL, KMC_NODEBUG);
131 
132 			(void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
133 			zio_data_buf_cache[c] = kmem_cache_create(name, size,
134 			    align, NULL, NULL, NULL, NULL, data_alloc_arena,
135 			    KMC_NODEBUG);
136 
137 			dprintf("creating cache for size %5lx align %5lx\n",
138 			    size, align);
139 		}
140 	}
141 
142 	while (--c != 0) {
143 		ASSERT(zio_buf_cache[c] != NULL);
144 		if (zio_buf_cache[c - 1] == NULL)
145 			zio_buf_cache[c - 1] = zio_buf_cache[c];
146 
147 		ASSERT(zio_data_buf_cache[c] != NULL);
148 		if (zio_data_buf_cache[c - 1] == NULL)
149 			zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
150 	}
151 
152 	zio_inject_init();
153 }
154 
155 void
156 zio_fini(void)
157 {
158 	size_t c;
159 	kmem_cache_t *last_cache = NULL;
160 	kmem_cache_t *last_data_cache = NULL;
161 
162 	for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
163 		if (zio_buf_cache[c] != last_cache) {
164 			last_cache = zio_buf_cache[c];
165 			kmem_cache_destroy(zio_buf_cache[c]);
166 		}
167 		zio_buf_cache[c] = NULL;
168 
169 		if (zio_data_buf_cache[c] != last_data_cache) {
170 			last_data_cache = zio_data_buf_cache[c];
171 			kmem_cache_destroy(zio_data_buf_cache[c]);
172 		}
173 		zio_data_buf_cache[c] = NULL;
174 	}
175 
176 	zio_inject_fini();
177 }
178 
179 /*
180  * ==========================================================================
181  * Allocate and free I/O buffers
182  * ==========================================================================
183  */
184 
185 /*
186  * Use zio_buf_alloc to allocate ZFS metadata.  This data will appear in a
187  * crashdump if the kernel panics, so use it judiciously.  Obviously, it's
188  * useful to inspect ZFS metadata, but if possible, we should avoid keeping
189  * excess / transient data in-core during a crashdump.
190  */
191 void *
192 zio_buf_alloc(size_t size)
193 {
194 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
195 
196 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
197 
198 	return (kmem_cache_alloc(zio_buf_cache[c], KM_SLEEP));
199 }
200 
201 /*
202  * Use zio_data_buf_alloc to allocate data.  The data will not appear in a
203  * crashdump if the kernel panics.  This exists so that we will limit the amount
204  * of ZFS data that shows up in a kernel crashdump.  (Thus reducing the amount
205  * of kernel heap dumped to disk when the kernel panics)
206  */
207 void *
208 zio_data_buf_alloc(size_t size)
209 {
210 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
211 
212 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
213 
214 	return (kmem_cache_alloc(zio_data_buf_cache[c], KM_SLEEP));
215 }
216 
217 void
218 zio_buf_free(void *buf, size_t size)
219 {
220 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
221 
222 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
223 
224 	kmem_cache_free(zio_buf_cache[c], buf);
225 }
226 
227 void
228 zio_data_buf_free(void *buf, size_t size)
229 {
230 	size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
231 
232 	ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
233 
234 	kmem_cache_free(zio_data_buf_cache[c], buf);
235 }
236 
237 /*
238  * ==========================================================================
239  * Push and pop I/O transform buffers
240  * ==========================================================================
241  */
242 static void
243 zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize)
244 {
245 	zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
246 
247 	zt->zt_data = data;
248 	zt->zt_size = size;
249 	zt->zt_bufsize = bufsize;
250 
251 	zt->zt_next = zio->io_transform_stack;
252 	zio->io_transform_stack = zt;
253 
254 	zio->io_data = data;
255 	zio->io_size = size;
256 }
257 
258 static void
259 zio_pop_transform(zio_t *zio, void **data, uint64_t *size, uint64_t *bufsize)
260 {
261 	zio_transform_t *zt = zio->io_transform_stack;
262 
263 	*data = zt->zt_data;
264 	*size = zt->zt_size;
265 	*bufsize = zt->zt_bufsize;
266 
267 	zio->io_transform_stack = zt->zt_next;
268 	kmem_free(zt, sizeof (zio_transform_t));
269 
270 	if ((zt = zio->io_transform_stack) != NULL) {
271 		zio->io_data = zt->zt_data;
272 		zio->io_size = zt->zt_size;
273 	}
274 }
275 
276 static void
277 zio_clear_transform_stack(zio_t *zio)
278 {
279 	void *data;
280 	uint64_t size, bufsize;
281 
282 	ASSERT(zio->io_transform_stack != NULL);
283 
284 	zio_pop_transform(zio, &data, &size, &bufsize);
285 	while (zio->io_transform_stack != NULL) {
286 		zio_buf_free(data, bufsize);
287 		zio_pop_transform(zio, &data, &size, &bufsize);
288 	}
289 }
290 
291 /*
292  * ==========================================================================
293  * Create the various types of I/O (read, write, free)
294  * ==========================================================================
295  */
296 static zio_t *
297 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
298     void *data, uint64_t size, zio_done_func_t *done, void *private,
299     zio_type_t type, int priority, int flags, uint8_t stage, uint32_t pipeline)
300 {
301 	zio_t *zio;
302 
303 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
304 	ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
305 
306 	zio = kmem_zalloc(sizeof (zio_t), KM_SLEEP);
307 	zio->io_parent = pio;
308 	zio->io_spa = spa;
309 	zio->io_txg = txg;
310 	if (bp != NULL) {
311 		zio->io_bp = bp;
312 		zio->io_bp_copy = *bp;
313 		zio->io_bp_orig = *bp;
314 	}
315 	zio->io_done = done;
316 	zio->io_private = private;
317 	zio->io_type = type;
318 	zio->io_priority = priority;
319 	zio->io_stage = stage;
320 	zio->io_pipeline = pipeline;
321 	zio->io_async_stages = ZIO_ASYNC_PIPELINE_STAGES;
322 	zio->io_timestamp = lbolt64;
323 	zio->io_flags = flags;
324 	mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
325 	zio_push_transform(zio, data, size, size);
326 
327 	/*
328 	 * Note on config lock:
329 	 *
330 	 * If CONFIG_HELD is set, then the caller already has the config
331 	 * lock, so we don't need it for this io.
332 	 *
333 	 * We set CONFIG_GRABBED to indicate that we have grabbed the
334 	 * config lock on behalf of this io, so it should be released
335 	 * in zio_done.
336 	 *
337 	 * Unless CONFIG_HELD is set, we will grab the config lock for
338 	 * any top-level (parent-less) io, *except* NULL top-level ios.
339 	 * The NULL top-level ios rarely have any children, so we delay
340 	 * grabbing the lock until the first child is added (but it is
341 	 * still grabbed on behalf of the top-level i/o, so additional
342 	 * children don't need to also grab it).  This greatly reduces
343 	 * contention on the config lock.
344 	 */
345 	if (pio == NULL) {
346 		if (type != ZIO_TYPE_NULL &&
347 		    !(flags & ZIO_FLAG_CONFIG_HELD)) {
348 			spa_config_enter(zio->io_spa, RW_READER, zio);
349 			zio->io_flags |= ZIO_FLAG_CONFIG_GRABBED;
350 		}
351 		zio->io_root = zio;
352 	} else {
353 		zio->io_root = pio->io_root;
354 		if (!(flags & ZIO_FLAG_NOBOOKMARK))
355 			zio->io_logical = pio->io_logical;
356 		mutex_enter(&pio->io_lock);
357 		if (pio->io_parent == NULL &&
358 		    pio->io_type == ZIO_TYPE_NULL &&
359 		    !(pio->io_flags & ZIO_FLAG_CONFIG_GRABBED) &&
360 		    !(pio->io_flags & ZIO_FLAG_CONFIG_HELD)) {
361 			pio->io_flags |= ZIO_FLAG_CONFIG_GRABBED;
362 			spa_config_enter(zio->io_spa, RW_READER, pio);
363 		}
364 		if (stage < ZIO_STAGE_READY)
365 			pio->io_children_notready++;
366 		pio->io_children_notdone++;
367 		zio->io_sibling_next = pio->io_child;
368 		zio->io_sibling_prev = NULL;
369 		if (pio->io_child != NULL)
370 			pio->io_child->io_sibling_prev = zio;
371 		pio->io_child = zio;
372 		zio->io_ndvas = pio->io_ndvas;
373 		mutex_exit(&pio->io_lock);
374 	}
375 
376 	return (zio);
377 }
378 
379 zio_t *
380 zio_null(zio_t *pio, spa_t *spa, zio_done_func_t *done, void *private,
381 	int flags)
382 {
383 	zio_t *zio;
384 
385 	zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
386 	    ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, ZIO_STAGE_OPEN,
387 	    ZIO_WAIT_FOR_CHILDREN_PIPELINE);
388 
389 	return (zio);
390 }
391 
392 zio_t *
393 zio_root(spa_t *spa, zio_done_func_t *done, void *private, int flags)
394 {
395 	return (zio_null(NULL, spa, done, private, flags));
396 }
397 
398 zio_t *
399 zio_read(zio_t *pio, spa_t *spa, blkptr_t *bp, void *data,
400     uint64_t size, zio_done_func_t *done, void *private,
401     int priority, int flags, zbookmark_t *zb)
402 {
403 	zio_t *zio;
404 
405 	ASSERT3U(size, ==, BP_GET_LSIZE(bp));
406 
407 	zio = zio_create(pio, spa, bp->blk_birth, bp, data, size, done, private,
408 	    ZIO_TYPE_READ, priority, flags | ZIO_FLAG_USER,
409 	    ZIO_STAGE_OPEN, ZIO_READ_PIPELINE);
410 	zio->io_bookmark = *zb;
411 
412 	zio->io_logical = zio;
413 
414 	/*
415 	 * Work off our copy of the bp so the caller can free it.
416 	 */
417 	zio->io_bp = &zio->io_bp_copy;
418 
419 	if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
420 		uint64_t csize = BP_GET_PSIZE(bp);
421 		void *cbuf = zio_buf_alloc(csize);
422 
423 		zio_push_transform(zio, cbuf, csize, csize);
424 		zio->io_pipeline |= 1U << ZIO_STAGE_READ_DECOMPRESS;
425 	}
426 
427 	if (BP_IS_GANG(bp)) {
428 		uint64_t gsize = SPA_GANGBLOCKSIZE;
429 		void *gbuf = zio_buf_alloc(gsize);
430 
431 		zio_push_transform(zio, gbuf, gsize, gsize);
432 		zio->io_pipeline |= 1U << ZIO_STAGE_READ_GANG_MEMBERS;
433 	}
434 
435 	return (zio);
436 }
437 
438 zio_t *
439 zio_write(zio_t *pio, spa_t *spa, int checksum, int compress, int ncopies,
440     uint64_t txg, blkptr_t *bp, void *data, uint64_t size,
441     zio_done_func_t *ready, zio_done_func_t *done, void *private, int priority,
442     int flags, zbookmark_t *zb)
443 {
444 	zio_t *zio;
445 
446 	ASSERT(checksum >= ZIO_CHECKSUM_OFF &&
447 	    checksum < ZIO_CHECKSUM_FUNCTIONS);
448 
449 	ASSERT(compress >= ZIO_COMPRESS_OFF &&
450 	    compress < ZIO_COMPRESS_FUNCTIONS);
451 
452 	zio = zio_create(pio, spa, txg, bp, data, size, done, private,
453 	    ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_USER,
454 	    ZIO_STAGE_OPEN, ZIO_WRITE_PIPELINE);
455 
456 	zio->io_ready = ready;
457 
458 	zio->io_bookmark = *zb;
459 
460 	zio->io_logical = zio;
461 
462 	zio->io_checksum = checksum;
463 	zio->io_compress = compress;
464 	zio->io_ndvas = ncopies;
465 
466 	if (compress != ZIO_COMPRESS_OFF)
467 		zio->io_async_stages |= 1U << ZIO_STAGE_WRITE_COMPRESS;
468 
469 	if (bp->blk_birth != txg) {
470 		/* XXX the bp usually (always?) gets re-zeroed later */
471 		BP_ZERO(bp);
472 		BP_SET_LSIZE(bp, size);
473 		BP_SET_PSIZE(bp, size);
474 	} else {
475 		/* Make sure someone doesn't change their mind on overwrites */
476 		ASSERT(MIN(zio->io_ndvas + BP_IS_GANG(bp),
477 		    spa_max_replication(spa)) == BP_GET_NDVAS(bp));
478 	}
479 
480 	return (zio);
481 }
482 
483 zio_t *
484 zio_rewrite(zio_t *pio, spa_t *spa, int checksum,
485     uint64_t txg, blkptr_t *bp, void *data, uint64_t size,
486     zio_done_func_t *done, void *private, int priority, int flags,
487     zbookmark_t *zb)
488 {
489 	zio_t *zio;
490 
491 	zio = zio_create(pio, spa, txg, bp, data, size, done, private,
492 	    ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_USER,
493 	    ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
494 
495 	zio->io_bookmark = *zb;
496 	zio->io_checksum = checksum;
497 	zio->io_compress = ZIO_COMPRESS_OFF;
498 
499 	if (pio != NULL)
500 		ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp));
501 
502 	return (zio);
503 }
504 
505 static zio_t *
506 zio_write_allocate(zio_t *pio, spa_t *spa, int checksum,
507     uint64_t txg, blkptr_t *bp, void *data, uint64_t size,
508     zio_done_func_t *done, void *private, int priority, int flags)
509 {
510 	zio_t *zio;
511 
512 	BP_ZERO(bp);
513 	BP_SET_LSIZE(bp, size);
514 	BP_SET_PSIZE(bp, size);
515 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
516 
517 	zio = zio_create(pio, spa, txg, bp, data, size, done, private,
518 	    ZIO_TYPE_WRITE, priority, flags,
519 	    ZIO_STAGE_OPEN, ZIO_WRITE_ALLOCATE_PIPELINE);
520 
521 	zio->io_checksum = checksum;
522 	zio->io_compress = ZIO_COMPRESS_OFF;
523 
524 	return (zio);
525 }
526 
527 zio_t *
528 zio_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
529     zio_done_func_t *done, void *private)
530 {
531 	zio_t *zio;
532 
533 	ASSERT(!BP_IS_HOLE(bp));
534 
535 	if (txg == spa->spa_syncing_txg &&
536 	    spa->spa_sync_pass > zio_sync_pass.zp_defer_free) {
537 		bplist_enqueue_deferred(&spa->spa_sync_bplist, bp);
538 		return (zio_null(pio, spa, NULL, NULL, 0));
539 	}
540 
541 	zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private,
542 	    ZIO_TYPE_FREE, ZIO_PRIORITY_FREE, ZIO_FLAG_USER,
543 	    ZIO_STAGE_OPEN, ZIO_FREE_PIPELINE);
544 
545 	zio->io_bp = &zio->io_bp_copy;
546 
547 	return (zio);
548 }
549 
550 zio_t *
551 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
552     zio_done_func_t *done, void *private)
553 {
554 	zio_t *zio;
555 
556 	/*
557 	 * A claim is an allocation of a specific block.  Claims are needed
558 	 * to support immediate writes in the intent log.  The issue is that
559 	 * immediate writes contain committed data, but in a txg that was
560 	 * *not* committed.  Upon opening the pool after an unclean shutdown,
561 	 * the intent log claims all blocks that contain immediate write data
562 	 * so that the SPA knows they're in use.
563 	 *
564 	 * All claims *must* be resolved in the first txg -- before the SPA
565 	 * starts allocating blocks -- so that nothing is allocated twice.
566 	 */
567 	ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
568 	ASSERT3U(spa_first_txg(spa), <=, txg);
569 
570 	zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private,
571 	    ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, 0,
572 	    ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
573 
574 	zio->io_bp = &zio->io_bp_copy;
575 
576 	return (zio);
577 }
578 
579 zio_t *
580 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
581     zio_done_func_t *done, void *private, int priority, int flags)
582 {
583 	zio_t *zio;
584 	int c;
585 
586 	if (vd->vdev_children == 0) {
587 		zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
588 		    ZIO_TYPE_IOCTL, priority, flags,
589 		    ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
590 
591 		zio->io_vd = vd;
592 		zio->io_cmd = cmd;
593 	} else {
594 		zio = zio_null(pio, spa, NULL, NULL, flags);
595 
596 		for (c = 0; c < vd->vdev_children; c++)
597 			zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
598 			    done, private, priority, flags));
599 	}
600 
601 	return (zio);
602 }
603 
604 static void
605 zio_phys_bp_init(vdev_t *vd, blkptr_t *bp, uint64_t offset, uint64_t size,
606     int checksum)
607 {
608 	ASSERT(vd->vdev_children == 0);
609 
610 	ASSERT(size <= SPA_MAXBLOCKSIZE);
611 	ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
612 	ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
613 
614 	ASSERT(offset + size <= VDEV_LABEL_START_SIZE ||
615 	    offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
616 	ASSERT3U(offset + size, <=, vd->vdev_psize);
617 
618 	BP_ZERO(bp);
619 
620 	BP_SET_LSIZE(bp, size);
621 	BP_SET_PSIZE(bp, size);
622 
623 	BP_SET_CHECKSUM(bp, checksum);
624 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
625 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
626 
627 	if (checksum != ZIO_CHECKSUM_OFF)
628 		ZIO_SET_CHECKSUM(&bp->blk_cksum, offset, 0, 0, 0);
629 }
630 
631 zio_t *
632 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
633     void *data, int checksum, zio_done_func_t *done, void *private,
634     int priority, int flags)
635 {
636 	zio_t *zio;
637 	blkptr_t blk;
638 
639 	zio_phys_bp_init(vd, &blk, offset, size, checksum);
640 
641 	zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private,
642 	    ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL,
643 	    ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
644 
645 	zio->io_vd = vd;
646 	zio->io_offset = offset;
647 
648 	/*
649 	 * Work off our copy of the bp so the caller can free it.
650 	 */
651 	zio->io_bp = &zio->io_bp_copy;
652 
653 	return (zio);
654 }
655 
656 zio_t *
657 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
658     void *data, int checksum, zio_done_func_t *done, void *private,
659     int priority, int flags)
660 {
661 	zio_block_tail_t *zbt;
662 	void *wbuf;
663 	zio_t *zio;
664 	blkptr_t blk;
665 
666 	zio_phys_bp_init(vd, &blk, offset, size, checksum);
667 
668 	zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private,
669 	    ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL,
670 	    ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
671 
672 	zio->io_vd = vd;
673 	zio->io_offset = offset;
674 
675 	zio->io_bp = &zio->io_bp_copy;
676 	zio->io_checksum = checksum;
677 
678 	if (zio_checksum_table[checksum].ci_zbt) {
679 		/*
680 		 * zbt checksums are necessarily destructive -- they modify
681 		 * one word of the write buffer to hold the verifier/checksum.
682 		 * Therefore, we must make a local copy in case the data is
683 		 * being written to multiple places.
684 		 */
685 		wbuf = zio_buf_alloc(size);
686 		bcopy(data, wbuf, size);
687 		zio_push_transform(zio, wbuf, size, size);
688 
689 		zbt = (zio_block_tail_t *)((char *)wbuf + size) - 1;
690 		zbt->zbt_cksum = blk.blk_cksum;
691 	}
692 
693 	return (zio);
694 }
695 
696 /*
697  * Create a child I/O to do some work for us.  It has no associated bp.
698  */
699 zio_t *
700 zio_vdev_child_io(zio_t *zio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
701 	void *data, uint64_t size, int type, int priority, int flags,
702 	zio_done_func_t *done, void *private)
703 {
704 	uint32_t pipeline = ZIO_VDEV_CHILD_PIPELINE;
705 	zio_t *cio;
706 
707 	if (type == ZIO_TYPE_READ && bp != NULL) {
708 		/*
709 		 * If we have the bp, then the child should perform the
710 		 * checksum and the parent need not.  This pushes error
711 		 * detection as close to the leaves as possible and
712 		 * eliminates redundant checksums in the interior nodes.
713 		 */
714 		pipeline |= 1U << ZIO_STAGE_CHECKSUM_VERIFY;
715 		zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY);
716 	}
717 
718 	cio = zio_create(zio, zio->io_spa, zio->io_txg, bp, data, size,
719 	    done, private, type, priority,
720 	    (zio->io_flags & ZIO_FLAG_VDEV_INHERIT) | ZIO_FLAG_CANFAIL | flags,
721 	    ZIO_STAGE_VDEV_IO_START - 1, pipeline);
722 
723 	cio->io_vd = vd;
724 	cio->io_offset = offset;
725 
726 	return (cio);
727 }
728 
729 /*
730  * ==========================================================================
731  * Initiate I/O, either sync or async
732  * ==========================================================================
733  */
734 int
735 zio_wait(zio_t *zio)
736 {
737 	int error;
738 
739 	ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
740 
741 	zio->io_waiter = curthread;
742 
743 	zio_next_stage_async(zio);
744 
745 	mutex_enter(&zio->io_lock);
746 	while (zio->io_stalled != ZIO_STAGE_DONE)
747 		cv_wait(&zio->io_cv, &zio->io_lock);
748 	mutex_exit(&zio->io_lock);
749 
750 	error = zio->io_error;
751 	mutex_destroy(&zio->io_lock);
752 	kmem_free(zio, sizeof (zio_t));
753 
754 	return (error);
755 }
756 
757 void
758 zio_nowait(zio_t *zio)
759 {
760 	zio_next_stage_async(zio);
761 }
762 
763 /*
764  * ==========================================================================
765  * I/O pipeline interlocks: parent/child dependency scoreboarding
766  * ==========================================================================
767  */
768 static void
769 zio_wait_for_children(zio_t *zio, uint32_t stage, uint64_t *countp)
770 {
771 	mutex_enter(&zio->io_lock);
772 	if (*countp == 0) {
773 		ASSERT(zio->io_stalled == 0);
774 		mutex_exit(&zio->io_lock);
775 		zio_next_stage(zio);
776 	} else {
777 		zio->io_stalled = stage;
778 		mutex_exit(&zio->io_lock);
779 	}
780 }
781 
782 static void
783 zio_notify_parent(zio_t *zio, uint32_t stage, uint64_t *countp)
784 {
785 	zio_t *pio = zio->io_parent;
786 
787 	mutex_enter(&pio->io_lock);
788 	if (pio->io_error == 0 && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
789 		pio->io_error = zio->io_error;
790 	if (--*countp == 0 && pio->io_stalled == stage) {
791 		pio->io_stalled = 0;
792 		mutex_exit(&pio->io_lock);
793 		zio_next_stage_async(pio);
794 	} else {
795 		mutex_exit(&pio->io_lock);
796 	}
797 }
798 
799 static void
800 zio_wait_children_ready(zio_t *zio)
801 {
802 	zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_READY,
803 	    &zio->io_children_notready);
804 }
805 
806 void
807 zio_wait_children_done(zio_t *zio)
808 {
809 	zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_DONE,
810 	    &zio->io_children_notdone);
811 }
812 
813 static void
814 zio_ready(zio_t *zio)
815 {
816 	zio_t *pio = zio->io_parent;
817 
818 	if (zio->io_ready)
819 		zio->io_ready(zio);
820 
821 	if (pio != NULL)
822 		zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_READY,
823 		    &pio->io_children_notready);
824 
825 	if (zio->io_bp)
826 		zio->io_bp_copy = *zio->io_bp;
827 
828 	zio_next_stage(zio);
829 }
830 
831 static void
832 zio_done(zio_t *zio)
833 {
834 	zio_t *pio = zio->io_parent;
835 	spa_t *spa = zio->io_spa;
836 	blkptr_t *bp = zio->io_bp;
837 	vdev_t *vd = zio->io_vd;
838 
839 	ASSERT(zio->io_children_notready == 0);
840 	ASSERT(zio->io_children_notdone == 0);
841 
842 	if (bp != NULL) {
843 		ASSERT(bp->blk_pad[0] == 0);
844 		ASSERT(bp->blk_pad[1] == 0);
845 		ASSERT(bp->blk_pad[2] == 0);
846 		ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0);
847 		if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
848 		    !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
849 			ASSERT(!BP_SHOULD_BYTESWAP(bp));
850 			if (zio->io_ndvas != 0)
851 				ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp));
852 			ASSERT(BP_COUNT_GANG(bp) == 0 ||
853 			    (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
854 		}
855 	}
856 
857 	if (vd != NULL)
858 		vdev_stat_update(zio);
859 
860 	if (zio->io_error) {
861 		/*
862 		 * If this I/O is attached to a particular vdev,
863 		 * generate an error message describing the I/O failure
864 		 * at the block level.  We ignore these errors if the
865 		 * device is currently unavailable.
866 		 */
867 		if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
868 			zfs_ereport_post(FM_EREPORT_ZFS_IO,
869 			    zio->io_spa, vd, zio, 0, 0);
870 
871 		if ((zio->io_error == EIO ||
872 		    !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) &&
873 		    zio->io_logical == zio) {
874 			/*
875 			 * For root I/O requests, tell the SPA to log the error
876 			 * appropriately.  Also, generate a logical data
877 			 * ereport.
878 			 */
879 			spa_log_error(zio->io_spa, zio);
880 
881 			zfs_ereport_post(FM_EREPORT_ZFS_DATA,
882 			    zio->io_spa, NULL, zio, 0, 0);
883 		}
884 
885 		/*
886 		 * For I/O requests that cannot fail, panic appropriately.
887 		 */
888 		if (!(zio->io_flags & ZIO_FLAG_CANFAIL)) {
889 			char *blkbuf;
890 
891 			blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_NOSLEEP);
892 			if (blkbuf) {
893 				sprintf_blkptr(blkbuf, BP_SPRINTF_LEN,
894 				    bp ? bp : &zio->io_bp_copy);
895 			}
896 			panic("ZFS: %s (%s on %s off %llx: zio %p %s): error "
897 			    "%d", zio->io_error == ECKSUM ?
898 			    "bad checksum" : "I/O failure",
899 			    zio_type_name[zio->io_type],
900 			    vdev_description(vd),
901 			    (u_longlong_t)zio->io_offset,
902 			    zio, blkbuf ? blkbuf : "", zio->io_error);
903 		}
904 	}
905 	zio_clear_transform_stack(zio);
906 
907 	if (zio->io_done)
908 		zio->io_done(zio);
909 
910 	ASSERT(zio->io_delegate_list == NULL);
911 	ASSERT(zio->io_delegate_next == NULL);
912 
913 	if (pio != NULL) {
914 		zio_t *next, *prev;
915 
916 		mutex_enter(&pio->io_lock);
917 		next = zio->io_sibling_next;
918 		prev = zio->io_sibling_prev;
919 		if (next != NULL)
920 			next->io_sibling_prev = prev;
921 		if (prev != NULL)
922 			prev->io_sibling_next = next;
923 		if (pio->io_child == zio)
924 			pio->io_child = next;
925 		mutex_exit(&pio->io_lock);
926 
927 		zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_DONE,
928 		    &pio->io_children_notdone);
929 	}
930 
931 	/*
932 	 * Note: this I/O is now done, and will shortly be
933 	 * kmem_free()'d, so there is no need to clear this (or any
934 	 * other) flag.
935 	 */
936 	if (zio->io_flags & ZIO_FLAG_CONFIG_GRABBED)
937 		spa_config_exit(spa, zio);
938 
939 	if (zio->io_waiter != NULL) {
940 		mutex_enter(&zio->io_lock);
941 		ASSERT(zio->io_stage == ZIO_STAGE_DONE);
942 		zio->io_stalled = zio->io_stage;
943 		cv_broadcast(&zio->io_cv);
944 		mutex_exit(&zio->io_lock);
945 	} else {
946 		kmem_free(zio, sizeof (zio_t));
947 	}
948 }
949 
950 /*
951  * ==========================================================================
952  * Compression support
953  * ==========================================================================
954  */
955 static void
956 zio_write_compress(zio_t *zio)
957 {
958 	int compress = zio->io_compress;
959 	blkptr_t *bp = zio->io_bp;
960 	void *cbuf;
961 	uint64_t lsize = zio->io_size;
962 	uint64_t csize = lsize;
963 	uint64_t cbufsize = 0;
964 	int pass;
965 
966 	if (bp->blk_birth == zio->io_txg) {
967 		/*
968 		 * We're rewriting an existing block, which means we're
969 		 * working on behalf of spa_sync().  For spa_sync() to
970 		 * converge, it must eventually be the case that we don't
971 		 * have to allocate new blocks.  But compression changes
972 		 * the blocksize, which forces a reallocate, and makes
973 		 * convergence take longer.  Therefore, after the first
974 		 * few passes, stop compressing to ensure convergence.
975 		 */
976 		pass = spa_sync_pass(zio->io_spa);
977 		if (pass > zio_sync_pass.zp_dontcompress)
978 			compress = ZIO_COMPRESS_OFF;
979 	} else {
980 		ASSERT(BP_IS_HOLE(bp));
981 		pass = 1;
982 	}
983 
984 	if (compress != ZIO_COMPRESS_OFF)
985 		if (!zio_compress_data(compress, zio->io_data, zio->io_size,
986 		    &cbuf, &csize, &cbufsize))
987 			compress = ZIO_COMPRESS_OFF;
988 
989 	if (compress != ZIO_COMPRESS_OFF && csize != 0)
990 		zio_push_transform(zio, cbuf, csize, cbufsize);
991 
992 	/*
993 	 * The final pass of spa_sync() must be all rewrites, but the first
994 	 * few passes offer a trade-off: allocating blocks defers convergence,
995 	 * but newly allocated blocks are sequential, so they can be written
996 	 * to disk faster.  Therefore, we allow the first few passes of
997 	 * spa_sync() to reallocate new blocks, but force rewrites after that.
998 	 * There should only be a handful of blocks after pass 1 in any case.
999 	 */
1000 	if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == csize &&
1001 	    pass > zio_sync_pass.zp_rewrite) {
1002 		ASSERT(csize != 0);
1003 		BP_SET_LSIZE(bp, lsize);
1004 		BP_SET_COMPRESS(bp, compress);
1005 		zio->io_pipeline = ZIO_REWRITE_PIPELINE;
1006 	} else {
1007 		if (bp->blk_birth == zio->io_txg) {
1008 			ASSERT3U(BP_GET_LSIZE(bp), ==, lsize);
1009 			bzero(bp, sizeof (blkptr_t));
1010 		}
1011 		if (csize == 0) {
1012 			BP_ZERO(bp);
1013 			zio->io_pipeline = ZIO_WAIT_FOR_CHILDREN_PIPELINE;
1014 		} else {
1015 			ASSERT3U(BP_GET_NDVAS(bp), ==, 0);
1016 			BP_SET_LSIZE(bp, lsize);
1017 			BP_SET_PSIZE(bp, csize);
1018 			BP_SET_COMPRESS(bp, compress);
1019 			zio->io_pipeline = ZIO_WRITE_ALLOCATE_PIPELINE;
1020 		}
1021 	}
1022 
1023 	zio_next_stage(zio);
1024 }
1025 
1026 static void
1027 zio_read_decompress(zio_t *zio)
1028 {
1029 	blkptr_t *bp = zio->io_bp;
1030 	void *data;
1031 	uint64_t size;
1032 	uint64_t bufsize;
1033 	int compress = BP_GET_COMPRESS(bp);
1034 
1035 	ASSERT(compress != ZIO_COMPRESS_OFF);
1036 
1037 	zio_pop_transform(zio, &data, &size, &bufsize);
1038 
1039 	if (zio_decompress_data(compress, data, size,
1040 	    zio->io_data, zio->io_size))
1041 		zio->io_error = EIO;
1042 
1043 	zio_buf_free(data, bufsize);
1044 
1045 	zio_next_stage(zio);
1046 }
1047 
1048 /*
1049  * ==========================================================================
1050  * Gang block support
1051  * ==========================================================================
1052  */
1053 static void
1054 zio_gang_pipeline(zio_t *zio)
1055 {
1056 	/*
1057 	 * By default, the pipeline assumes that we're dealing with a gang
1058 	 * block.  If we're not, strip out any gang-specific stages.
1059 	 */
1060 	if (!BP_IS_GANG(zio->io_bp))
1061 		zio->io_pipeline &= ~ZIO_GANG_STAGES;
1062 
1063 	zio_next_stage(zio);
1064 }
1065 
1066 static void
1067 zio_gang_byteswap(zio_t *zio)
1068 {
1069 	ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
1070 
1071 	if (BP_SHOULD_BYTESWAP(zio->io_bp))
1072 		byteswap_uint64_array(zio->io_data, zio->io_size);
1073 }
1074 
1075 static void
1076 zio_get_gang_header(zio_t *zio)
1077 {
1078 	blkptr_t *bp = zio->io_bp;
1079 	uint64_t gsize = SPA_GANGBLOCKSIZE;
1080 	void *gbuf = zio_buf_alloc(gsize);
1081 
1082 	ASSERT(BP_IS_GANG(bp));
1083 
1084 	zio_push_transform(zio, gbuf, gsize, gsize);
1085 
1086 	zio_nowait(zio_create(zio, zio->io_spa, bp->blk_birth, bp, gbuf, gsize,
1087 	    NULL, NULL, ZIO_TYPE_READ, zio->io_priority,
1088 	    zio->io_flags & ZIO_FLAG_GANG_INHERIT,
1089 	    ZIO_STAGE_OPEN, ZIO_READ_PIPELINE));
1090 
1091 	zio_wait_children_done(zio);
1092 }
1093 
1094 static void
1095 zio_read_gang_members(zio_t *zio)
1096 {
1097 	zio_gbh_phys_t *gbh;
1098 	uint64_t gsize, gbufsize, loff, lsize;
1099 	int i;
1100 
1101 	ASSERT(BP_IS_GANG(zio->io_bp));
1102 
1103 	zio_gang_byteswap(zio);
1104 	zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize);
1105 
1106 	for (loff = 0, i = 0; loff != zio->io_size; loff += lsize, i++) {
1107 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1108 		lsize = BP_GET_PSIZE(gbp);
1109 
1110 		ASSERT(BP_GET_COMPRESS(gbp) == ZIO_COMPRESS_OFF);
1111 		ASSERT3U(lsize, ==, BP_GET_LSIZE(gbp));
1112 		ASSERT3U(loff + lsize, <=, zio->io_size);
1113 		ASSERT(i < SPA_GBH_NBLKPTRS);
1114 		ASSERT(!BP_IS_HOLE(gbp));
1115 
1116 		zio_nowait(zio_read(zio, zio->io_spa, gbp,
1117 		    (char *)zio->io_data + loff, lsize, NULL, NULL,
1118 		    zio->io_priority, zio->io_flags & ZIO_FLAG_GANG_INHERIT,
1119 		    &zio->io_bookmark));
1120 	}
1121 
1122 	zio_buf_free(gbh, gbufsize);
1123 	zio_wait_children_done(zio);
1124 }
1125 
1126 static void
1127 zio_rewrite_gang_members(zio_t *zio)
1128 {
1129 	zio_gbh_phys_t *gbh;
1130 	uint64_t gsize, gbufsize, loff, lsize;
1131 	int i;
1132 
1133 	ASSERT(BP_IS_GANG(zio->io_bp));
1134 	ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE);
1135 
1136 	zio_gang_byteswap(zio);
1137 	zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize);
1138 
1139 	ASSERT(gsize == gbufsize);
1140 
1141 	for (loff = 0, i = 0; loff != zio->io_size; loff += lsize, i++) {
1142 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1143 		lsize = BP_GET_PSIZE(gbp);
1144 
1145 		ASSERT(BP_GET_COMPRESS(gbp) == ZIO_COMPRESS_OFF);
1146 		ASSERT3U(lsize, ==, BP_GET_LSIZE(gbp));
1147 		ASSERT3U(loff + lsize, <=, zio->io_size);
1148 		ASSERT(i < SPA_GBH_NBLKPTRS);
1149 		ASSERT(!BP_IS_HOLE(gbp));
1150 
1151 		zio_nowait(zio_rewrite(zio, zio->io_spa, zio->io_checksum,
1152 		    zio->io_txg, gbp, (char *)zio->io_data + loff, lsize,
1153 		    NULL, NULL, zio->io_priority, zio->io_flags,
1154 		    &zio->io_bookmark));
1155 	}
1156 
1157 	zio_push_transform(zio, gbh, gsize, gbufsize);
1158 	zio_wait_children_ready(zio);
1159 }
1160 
1161 static void
1162 zio_free_gang_members(zio_t *zio)
1163 {
1164 	zio_gbh_phys_t *gbh;
1165 	uint64_t gsize, gbufsize;
1166 	int i;
1167 
1168 	ASSERT(BP_IS_GANG(zio->io_bp));
1169 
1170 	zio_gang_byteswap(zio);
1171 	zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize);
1172 
1173 	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1174 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1175 
1176 		if (BP_IS_HOLE(gbp))
1177 			continue;
1178 		zio_nowait(zio_free(zio, zio->io_spa, zio->io_txg,
1179 		    gbp, NULL, NULL));
1180 	}
1181 
1182 	zio_buf_free(gbh, gbufsize);
1183 	zio_next_stage(zio);
1184 }
1185 
1186 static void
1187 zio_claim_gang_members(zio_t *zio)
1188 {
1189 	zio_gbh_phys_t *gbh;
1190 	uint64_t gsize, gbufsize;
1191 	int i;
1192 
1193 	ASSERT(BP_IS_GANG(zio->io_bp));
1194 
1195 	zio_gang_byteswap(zio);
1196 	zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize);
1197 
1198 	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1199 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1200 		if (BP_IS_HOLE(gbp))
1201 			continue;
1202 		zio_nowait(zio_claim(zio, zio->io_spa, zio->io_txg,
1203 		    gbp, NULL, NULL));
1204 	}
1205 
1206 	zio_buf_free(gbh, gbufsize);
1207 	zio_next_stage(zio);
1208 }
1209 
1210 static void
1211 zio_write_allocate_gang_member_done(zio_t *zio)
1212 {
1213 	zio_t *pio = zio->io_parent;
1214 	dva_t *cdva = zio->io_bp->blk_dva;
1215 	dva_t *pdva = pio->io_bp->blk_dva;
1216 	uint64_t asize;
1217 	int d;
1218 
1219 	ASSERT3U(pio->io_ndvas, ==, zio->io_ndvas);
1220 	ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
1221 	ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(zio->io_bp));
1222 	ASSERT3U(pio->io_ndvas, <=, BP_GET_NDVAS(pio->io_bp));
1223 
1224 	mutex_enter(&pio->io_lock);
1225 	for (d = 0; d < BP_GET_NDVAS(pio->io_bp); d++) {
1226 		ASSERT(DVA_GET_GANG(&pdva[d]));
1227 		asize = DVA_GET_ASIZE(&pdva[d]);
1228 		asize += DVA_GET_ASIZE(&cdva[d]);
1229 		DVA_SET_ASIZE(&pdva[d], asize);
1230 	}
1231 	mutex_exit(&pio->io_lock);
1232 }
1233 
1234 static void
1235 zio_write_allocate_gang_members(zio_t *zio)
1236 {
1237 	blkptr_t *bp = zio->io_bp;
1238 	dva_t *dva = bp->blk_dva;
1239 	spa_t *spa = zio->io_spa;
1240 	zio_gbh_phys_t *gbh;
1241 	uint64_t txg = zio->io_txg;
1242 	uint64_t resid = zio->io_size;
1243 	uint64_t maxalloc = P2ROUNDUP(zio->io_size >> 1, SPA_MINBLOCKSIZE);
1244 	uint64_t gsize, loff, lsize;
1245 	uint32_t gbps_left;
1246 	int ndvas = zio->io_ndvas;
1247 	int gbh_ndvas = MIN(ndvas + 1, spa_max_replication(spa));
1248 	int error;
1249 	int i, d;
1250 
1251 	gsize = SPA_GANGBLOCKSIZE;
1252 	gbps_left = SPA_GBH_NBLKPTRS;
1253 
1254 	error = metaslab_alloc(spa, gsize, bp, gbh_ndvas, txg, NULL, B_FALSE);
1255 	if (error == ENOSPC)
1256 		panic("can't allocate gang block header");
1257 	ASSERT(error == 0);
1258 
1259 	for (d = 0; d < gbh_ndvas; d++)
1260 		DVA_SET_GANG(&dva[d], 1);
1261 
1262 	bp->blk_birth = txg;
1263 
1264 	gbh = zio_buf_alloc(gsize);
1265 	bzero(gbh, gsize);
1266 
1267 	/* We need to test multi-level gang blocks */
1268 	if (maxalloc >= zio_gang_bang && (lbolt & 0x1) == 0)
1269 		maxalloc = MAX(maxalloc >> 2, SPA_MINBLOCKSIZE);
1270 
1271 	for (loff = 0, i = 0; loff != zio->io_size;
1272 	    loff += lsize, resid -= lsize, gbps_left--, i++) {
1273 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1274 		dva = gbp->blk_dva;
1275 
1276 		ASSERT(gbps_left != 0);
1277 		maxalloc = MIN(maxalloc, resid);
1278 
1279 		while (resid <= maxalloc * gbps_left) {
1280 			error = metaslab_alloc(spa, maxalloc, gbp, ndvas,
1281 			    txg, bp, B_FALSE);
1282 			if (error == 0)
1283 				break;
1284 			ASSERT3U(error, ==, ENOSPC);
1285 			if (maxalloc == SPA_MINBLOCKSIZE)
1286 				panic("really out of space");
1287 			maxalloc = P2ROUNDUP(maxalloc >> 1, SPA_MINBLOCKSIZE);
1288 		}
1289 
1290 		if (resid <= maxalloc * gbps_left) {
1291 			lsize = maxalloc;
1292 			BP_SET_LSIZE(gbp, lsize);
1293 			BP_SET_PSIZE(gbp, lsize);
1294 			BP_SET_COMPRESS(gbp, ZIO_COMPRESS_OFF);
1295 			gbp->blk_birth = txg;
1296 			zio_nowait(zio_rewrite(zio, spa,
1297 			    zio->io_checksum, txg, gbp,
1298 			    (char *)zio->io_data + loff, lsize,
1299 			    zio_write_allocate_gang_member_done, NULL,
1300 			    zio->io_priority, zio->io_flags,
1301 			    &zio->io_bookmark));
1302 		} else {
1303 			lsize = P2ROUNDUP(resid / gbps_left, SPA_MINBLOCKSIZE);
1304 			ASSERT(lsize != SPA_MINBLOCKSIZE);
1305 			zio_nowait(zio_write_allocate(zio, spa,
1306 			    zio->io_checksum, txg, gbp,
1307 			    (char *)zio->io_data + loff, lsize,
1308 			    zio_write_allocate_gang_member_done, NULL,
1309 			    zio->io_priority, zio->io_flags));
1310 		}
1311 	}
1312 
1313 	ASSERT(resid == 0 && loff == zio->io_size);
1314 
1315 	zio->io_pipeline |= 1U << ZIO_STAGE_GANG_CHECKSUM_GENERATE;
1316 
1317 	zio_push_transform(zio, gbh, gsize, gsize);
1318 	/*
1319 	 * As much as we'd like this to be zio_wait_children_ready(),
1320 	 * updating our ASIZE doesn't happen until the io_done callback,
1321 	 * so we have to wait for that to finish in order for our BP
1322 	 * to be stable.
1323 	 */
1324 	zio_wait_children_done(zio);
1325 }
1326 
1327 /*
1328  * ==========================================================================
1329  * Allocate and free blocks
1330  * ==========================================================================
1331  */
1332 static void
1333 zio_dva_allocate(zio_t *zio)
1334 {
1335 	blkptr_t *bp = zio->io_bp;
1336 	int error;
1337 
1338 	ASSERT(BP_IS_HOLE(bp));
1339 	ASSERT3U(BP_GET_NDVAS(bp), ==, 0);
1340 	ASSERT3U(zio->io_ndvas, >, 0);
1341 	ASSERT3U(zio->io_ndvas, <=, spa_max_replication(zio->io_spa));
1342 
1343 	/* For testing, make some blocks above a certain size be gang blocks */
1344 	if (zio->io_size >= zio_gang_bang && (lbolt & 0x3) == 0) {
1345 		zio_write_allocate_gang_members(zio);
1346 		return;
1347 	}
1348 
1349 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
1350 
1351 	error = metaslab_alloc(zio->io_spa, zio->io_size, bp, zio->io_ndvas,
1352 	    zio->io_txg, NULL, B_FALSE);
1353 
1354 	if (error == 0) {
1355 		bp->blk_birth = zio->io_txg;
1356 	} else if (error == ENOSPC) {
1357 		if (zio->io_size == SPA_MINBLOCKSIZE)
1358 			panic("really, truly out of space");
1359 		zio_write_allocate_gang_members(zio);
1360 		return;
1361 	} else {
1362 		zio->io_error = error;
1363 	}
1364 	zio_next_stage(zio);
1365 }
1366 
1367 static void
1368 zio_dva_free(zio_t *zio)
1369 {
1370 	blkptr_t *bp = zio->io_bp;
1371 
1372 	metaslab_free(zio->io_spa, bp, zio->io_txg, B_FALSE);
1373 
1374 	BP_ZERO(bp);
1375 
1376 	zio_next_stage(zio);
1377 }
1378 
1379 static void
1380 zio_dva_claim(zio_t *zio)
1381 {
1382 	zio->io_error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
1383 
1384 	zio_next_stage(zio);
1385 }
1386 
1387 /*
1388  * ==========================================================================
1389  * Read and write to physical devices
1390  * ==========================================================================
1391  */
1392 
1393 static void
1394 zio_vdev_io_start(zio_t *zio)
1395 {
1396 	vdev_t *vd = zio->io_vd;
1397 	vdev_t *tvd = vd ? vd->vdev_top : NULL;
1398 	blkptr_t *bp = zio->io_bp;
1399 	uint64_t align;
1400 
1401 	if (vd == NULL) {
1402 		/* The mirror_ops handle multiple DVAs in a single BP */
1403 		vdev_mirror_ops.vdev_op_io_start(zio);
1404 		return;
1405 	}
1406 
1407 	align = 1ULL << tvd->vdev_ashift;
1408 
1409 	if (zio->io_retries == 0 && vd == tvd)
1410 		zio->io_flags |= ZIO_FLAG_FAILFAST;
1411 
1412 	if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
1413 	    vd->vdev_children == 0) {
1414 		zio->io_flags |= ZIO_FLAG_PHYSICAL;
1415 		zio->io_offset += VDEV_LABEL_START_SIZE;
1416 	}
1417 
1418 	if (P2PHASE(zio->io_size, align) != 0) {
1419 		uint64_t asize = P2ROUNDUP(zio->io_size, align);
1420 		char *abuf = zio_buf_alloc(asize);
1421 		ASSERT(vd == tvd);
1422 		if (zio->io_type == ZIO_TYPE_WRITE) {
1423 			bcopy(zio->io_data, abuf, zio->io_size);
1424 			bzero(abuf + zio->io_size, asize - zio->io_size);
1425 		}
1426 		zio_push_transform(zio, abuf, asize, asize);
1427 		ASSERT(!(zio->io_flags & ZIO_FLAG_SUBBLOCK));
1428 		zio->io_flags |= ZIO_FLAG_SUBBLOCK;
1429 	}
1430 
1431 	ASSERT(P2PHASE(zio->io_offset, align) == 0);
1432 	ASSERT(P2PHASE(zio->io_size, align) == 0);
1433 	ASSERT(bp == NULL ||
1434 	    P2ROUNDUP(ZIO_GET_IOSIZE(zio), align) == zio->io_size);
1435 	ASSERT(zio->io_type != ZIO_TYPE_WRITE || (spa_mode & FWRITE));
1436 
1437 	vdev_io_start(zio);
1438 
1439 	/* zio_next_stage_async() gets called from io completion interrupt */
1440 }
1441 
1442 static void
1443 zio_vdev_io_done(zio_t *zio)
1444 {
1445 	if (zio->io_vd == NULL)
1446 		/* The mirror_ops handle multiple DVAs in a single BP */
1447 		vdev_mirror_ops.vdev_op_io_done(zio);
1448 	else
1449 		vdev_io_done(zio);
1450 }
1451 
1452 /* XXPOLICY */
1453 boolean_t
1454 zio_should_retry(zio_t *zio)
1455 {
1456 	vdev_t *vd = zio->io_vd;
1457 
1458 	if (zio->io_error == 0)
1459 		return (B_FALSE);
1460 	if (zio->io_delegate_list != NULL)
1461 		return (B_FALSE);
1462 	if (vd && vd != vd->vdev_top)
1463 		return (B_FALSE);
1464 	if (zio->io_flags & ZIO_FLAG_DONT_RETRY)
1465 		return (B_FALSE);
1466 	if (zio->io_retries > 0)
1467 		return (B_FALSE);
1468 
1469 	return (B_TRUE);
1470 }
1471 
1472 static void
1473 zio_vdev_io_assess(zio_t *zio)
1474 {
1475 	vdev_t *vd = zio->io_vd;
1476 	vdev_t *tvd = vd ? vd->vdev_top : NULL;
1477 
1478 	ASSERT(zio->io_vsd == NULL);
1479 
1480 	if (zio->io_flags & ZIO_FLAG_SUBBLOCK) {
1481 		void *abuf;
1482 		uint64_t asize;
1483 		ASSERT(vd == tvd);
1484 		zio_pop_transform(zio, &abuf, &asize, &asize);
1485 		if (zio->io_type == ZIO_TYPE_READ)
1486 			bcopy(abuf, zio->io_data, zio->io_size);
1487 		zio_buf_free(abuf, asize);
1488 		zio->io_flags &= ~ZIO_FLAG_SUBBLOCK;
1489 	}
1490 
1491 	if (zio_injection_enabled && !zio->io_error)
1492 		zio->io_error = zio_handle_fault_injection(zio, EIO);
1493 
1494 	/*
1495 	 * If the I/O failed, determine whether we should attempt to retry it.
1496 	 */
1497 	/* XXPOLICY */
1498 	if (zio_should_retry(zio)) {
1499 		ASSERT(tvd == vd);
1500 
1501 		zio->io_retries++;
1502 		zio->io_error = 0;
1503 		zio->io_flags &= ZIO_FLAG_VDEV_INHERIT |
1504 		    ZIO_FLAG_CONFIG_GRABBED;
1505 		/* XXPOLICY */
1506 		zio->io_flags &= ~ZIO_FLAG_FAILFAST;
1507 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1508 		zio->io_stage = ZIO_STAGE_VDEV_IO_START - 1;
1509 
1510 		dprintf("retry #%d for %s to %s offset %llx\n",
1511 		    zio->io_retries, zio_type_name[zio->io_type],
1512 		    vdev_description(vd), zio->io_offset);
1513 
1514 		zio_next_stage_async(zio);
1515 		return;
1516 	}
1517 
1518 	if (zio->io_error != 0 && zio->io_error != ECKSUM &&
1519 	    !(zio->io_flags & ZIO_FLAG_SPECULATIVE) && vd) {
1520 		/*
1521 		 * Poor man's hotplug support.  Even if we're done retrying this
1522 		 * I/O, try to reopen the vdev to see if it's still attached.
1523 		 * To avoid excessive thrashing, we only try it once a minute.
1524 		 * This also has the effect of detecting when missing devices
1525 		 * have come back, by polling the device once a minute.
1526 		 *
1527 		 * We need to do this asynchronously because we can't grab
1528 		 * all the necessary locks way down here.
1529 		 */
1530 		if (gethrtime() - vd->vdev_last_try > 60ULL * NANOSEC) {
1531 			vd->vdev_last_try = gethrtime();
1532 			tvd->vdev_reopen_wanted = 1;
1533 			spa_async_request(vd->vdev_spa, SPA_ASYNC_REOPEN);
1534 		}
1535 	}
1536 
1537 	zio_next_stage(zio);
1538 }
1539 
1540 void
1541 zio_vdev_io_reissue(zio_t *zio)
1542 {
1543 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1544 	ASSERT(zio->io_error == 0);
1545 
1546 	zio->io_stage--;
1547 }
1548 
1549 void
1550 zio_vdev_io_redone(zio_t *zio)
1551 {
1552 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
1553 
1554 	zio->io_stage--;
1555 }
1556 
1557 void
1558 zio_vdev_io_bypass(zio_t *zio)
1559 {
1560 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1561 	ASSERT(zio->io_error == 0);
1562 
1563 	zio->io_flags |= ZIO_FLAG_IO_BYPASS;
1564 	zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS - 1;
1565 }
1566 
1567 /*
1568  * ==========================================================================
1569  * Generate and verify checksums
1570  * ==========================================================================
1571  */
1572 static void
1573 zio_checksum_generate(zio_t *zio)
1574 {
1575 	int checksum = zio->io_checksum;
1576 	blkptr_t *bp = zio->io_bp;
1577 
1578 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
1579 
1580 	BP_SET_CHECKSUM(bp, checksum);
1581 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1582 
1583 	zio_checksum(checksum, &bp->blk_cksum, zio->io_data, zio->io_size);
1584 
1585 	zio_next_stage(zio);
1586 }
1587 
1588 static void
1589 zio_gang_checksum_generate(zio_t *zio)
1590 {
1591 	zio_cksum_t zc;
1592 	zio_gbh_phys_t *gbh = zio->io_data;
1593 
1594 	ASSERT(BP_IS_GANG(zio->io_bp));
1595 	ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE);
1596 
1597 	zio_set_gang_verifier(zio, &gbh->zg_tail.zbt_cksum);
1598 
1599 	zio_checksum(ZIO_CHECKSUM_GANG_HEADER, &zc, zio->io_data, zio->io_size);
1600 
1601 	zio_next_stage(zio);
1602 }
1603 
1604 static void
1605 zio_checksum_verify(zio_t *zio)
1606 {
1607 	if (zio->io_bp != NULL) {
1608 		zio->io_error = zio_checksum_error(zio);
1609 		if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE))
1610 			zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM,
1611 			    zio->io_spa, zio->io_vd, zio, 0, 0);
1612 	}
1613 
1614 	zio_next_stage(zio);
1615 }
1616 
1617 /*
1618  * Called by RAID-Z to ensure we don't compute the checksum twice.
1619  */
1620 void
1621 zio_checksum_verified(zio_t *zio)
1622 {
1623 	zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY);
1624 }
1625 
1626 /*
1627  * Set the external verifier for a gang block based on stuff in the bp
1628  */
1629 void
1630 zio_set_gang_verifier(zio_t *zio, zio_cksum_t *zcp)
1631 {
1632 	blkptr_t *bp = zio->io_bp;
1633 
1634 	zcp->zc_word[0] = DVA_GET_VDEV(BP_IDENTITY(bp));
1635 	zcp->zc_word[1] = DVA_GET_OFFSET(BP_IDENTITY(bp));
1636 	zcp->zc_word[2] = bp->blk_birth;
1637 	zcp->zc_word[3] = 0;
1638 }
1639 
1640 /*
1641  * ==========================================================================
1642  * Define the pipeline
1643  * ==========================================================================
1644  */
1645 typedef void zio_pipe_stage_t(zio_t *zio);
1646 
1647 static void
1648 zio_badop(zio_t *zio)
1649 {
1650 	panic("Invalid I/O pipeline stage %u for zio %p", zio->io_stage, zio);
1651 }
1652 
1653 zio_pipe_stage_t *zio_pipeline[ZIO_STAGE_DONE + 2] = {
1654 	zio_badop,
1655 	zio_wait_children_ready,
1656 	zio_write_compress,
1657 	zio_checksum_generate,
1658 	zio_gang_pipeline,
1659 	zio_get_gang_header,
1660 	zio_rewrite_gang_members,
1661 	zio_free_gang_members,
1662 	zio_claim_gang_members,
1663 	zio_dva_allocate,
1664 	zio_dva_free,
1665 	zio_dva_claim,
1666 	zio_gang_checksum_generate,
1667 	zio_ready,
1668 	zio_vdev_io_start,
1669 	zio_vdev_io_done,
1670 	zio_vdev_io_assess,
1671 	zio_wait_children_done,
1672 	zio_checksum_verify,
1673 	zio_read_gang_members,
1674 	zio_read_decompress,
1675 	zio_done,
1676 	zio_badop
1677 };
1678 
1679 /*
1680  * Move an I/O to the next stage of the pipeline and execute that stage.
1681  * There's no locking on io_stage because there's no legitimate way for
1682  * multiple threads to be attempting to process the same I/O.
1683  */
1684 void
1685 zio_next_stage(zio_t *zio)
1686 {
1687 	uint32_t pipeline = zio->io_pipeline;
1688 
1689 	ASSERT(!MUTEX_HELD(&zio->io_lock));
1690 
1691 	if (zio->io_error) {
1692 		dprintf("zio %p vdev %s offset %llx stage %d error %d\n",
1693 		    zio, vdev_description(zio->io_vd),
1694 		    zio->io_offset, zio->io_stage, zio->io_error);
1695 		if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0)
1696 			pipeline &= ZIO_ERROR_PIPELINE_MASK;
1697 	}
1698 
1699 	while (((1U << ++zio->io_stage) & pipeline) == 0)
1700 		continue;
1701 
1702 	ASSERT(zio->io_stage <= ZIO_STAGE_DONE);
1703 	ASSERT(zio->io_stalled == 0);
1704 
1705 	zio_pipeline[zio->io_stage](zio);
1706 }
1707 
1708 void
1709 zio_next_stage_async(zio_t *zio)
1710 {
1711 	taskq_t *tq;
1712 	uint32_t pipeline = zio->io_pipeline;
1713 
1714 	ASSERT(!MUTEX_HELD(&zio->io_lock));
1715 
1716 	if (zio->io_error) {
1717 		dprintf("zio %p vdev %s offset %llx stage %d error %d\n",
1718 		    zio, vdev_description(zio->io_vd),
1719 		    zio->io_offset, zio->io_stage, zio->io_error);
1720 		if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0)
1721 			pipeline &= ZIO_ERROR_PIPELINE_MASK;
1722 	}
1723 
1724 	while (((1U << ++zio->io_stage) & pipeline) == 0)
1725 		continue;
1726 
1727 	ASSERT(zio->io_stage <= ZIO_STAGE_DONE);
1728 	ASSERT(zio->io_stalled == 0);
1729 
1730 	/*
1731 	 * For performance, we'll probably want two sets of task queues:
1732 	 * per-CPU issue taskqs and per-CPU completion taskqs.  The per-CPU
1733 	 * part is for read performance: since we have to make a pass over
1734 	 * the data to checksum it anyway, we want to do this on the same CPU
1735 	 * that issued the read, because (assuming CPU scheduling affinity)
1736 	 * that thread is probably still there.  Getting this optimization
1737 	 * right avoids performance-hostile cache-to-cache transfers.
1738 	 *
1739 	 * Note that having two sets of task queues is also necessary for
1740 	 * correctness: if all of the issue threads get bogged down waiting
1741 	 * for dependent reads (e.g. metaslab freelist) to complete, then
1742 	 * there won't be any threads available to service I/O completion
1743 	 * interrupts.
1744 	 */
1745 	if ((1U << zio->io_stage) & zio->io_async_stages) {
1746 		if (zio->io_stage < ZIO_STAGE_VDEV_IO_DONE)
1747 			tq = zio->io_spa->spa_zio_issue_taskq[zio->io_type];
1748 		else
1749 			tq = zio->io_spa->spa_zio_intr_taskq[zio->io_type];
1750 		(void) taskq_dispatch(tq,
1751 		    (task_func_t *)zio_pipeline[zio->io_stage], zio, TQ_SLEEP);
1752 	} else {
1753 		zio_pipeline[zio->io_stage](zio);
1754 	}
1755 }
1756 
1757 static boolean_t
1758 zio_alloc_should_fail(void)
1759 {
1760 	static uint16_t	allocs = 0;
1761 
1762 	return (P2PHASE(allocs++, 1U<<zio_zil_fail_shift) == 0);
1763 }
1764 
1765 /*
1766  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
1767  */
1768 int
1769 zio_alloc_blk(spa_t *spa, uint64_t size, blkptr_t *new_bp, blkptr_t *old_bp,
1770     uint64_t txg)
1771 {
1772 	int error;
1773 
1774 	spa_config_enter(spa, RW_READER, FTAG);
1775 
1776 	if (zio_zil_fail_shift && zio_alloc_should_fail()) {
1777 		spa_config_exit(spa, FTAG);
1778 		return (ENOSPC);
1779 	}
1780 
1781 	/*
1782 	 * We were passed the previous log blocks dva_t in bp->blk_dva[0].
1783 	 */
1784 	error = metaslab_alloc(spa, size, new_bp, 1, txg, old_bp, B_TRUE);
1785 
1786 	if (error == 0) {
1787 		BP_SET_LSIZE(new_bp, size);
1788 		BP_SET_PSIZE(new_bp, size);
1789 		BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
1790 		BP_SET_CHECKSUM(new_bp, ZIO_CHECKSUM_ZILOG);
1791 		BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
1792 		BP_SET_LEVEL(new_bp, 0);
1793 		BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
1794 		new_bp->blk_birth = txg;
1795 	}
1796 
1797 	spa_config_exit(spa, FTAG);
1798 
1799 	return (error);
1800 }
1801 
1802 /*
1803  * Free an intent log block.  We know it can't be a gang block, so there's
1804  * nothing to do except metaslab_free() it.
1805  */
1806 void
1807 zio_free_blk(spa_t *spa, blkptr_t *bp, uint64_t txg)
1808 {
1809 	ASSERT(!BP_IS_GANG(bp));
1810 
1811 	spa_config_enter(spa, RW_READER, FTAG);
1812 
1813 	metaslab_free(spa, bp, txg, B_FALSE);
1814 
1815 	spa_config_exit(spa, FTAG);
1816 }
1817