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