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