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