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