xref: /titanic_44/usr/src/uts/common/fs/zfs/zio.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
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)
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, gsize, bp, gbh_ndvas, txg, NULL, B_FALSE);
1259 	if (error == ENOSPC)
1260 		panic("can't allocate gang block header");
1261 	ASSERT(error == 0);
1262 
1263 	for (d = 0; d < gbh_ndvas; d++)
1264 		DVA_SET_GANG(&dva[d], 1);
1265 
1266 	bp->blk_birth = txg;
1267 
1268 	gbh = zio_buf_alloc(gsize);
1269 	bzero(gbh, gsize);
1270 
1271 	/* We need to test multi-level gang blocks */
1272 	if (maxalloc >= zio_gang_bang && (lbolt & 0x1) == 0)
1273 		maxalloc = MAX(maxalloc >> 2, SPA_MINBLOCKSIZE);
1274 
1275 	for (loff = 0, i = 0; loff != zio->io_size;
1276 	    loff += lsize, resid -= lsize, gbps_left--, i++) {
1277 		blkptr_t *gbp = &gbh->zg_blkptr[i];
1278 		dva = gbp->blk_dva;
1279 
1280 		ASSERT(gbps_left != 0);
1281 		maxalloc = MIN(maxalloc, resid);
1282 
1283 		while (resid <= maxalloc * gbps_left) {
1284 			error = metaslab_alloc(spa, maxalloc, gbp, ndvas,
1285 			    txg, bp, B_FALSE);
1286 			if (error == 0)
1287 				break;
1288 			ASSERT3U(error, ==, ENOSPC);
1289 			if (maxalloc == SPA_MINBLOCKSIZE)
1290 				panic("really out of space");
1291 			maxalloc = P2ROUNDUP(maxalloc >> 1, SPA_MINBLOCKSIZE);
1292 		}
1293 
1294 		if (resid <= maxalloc * gbps_left) {
1295 			lsize = maxalloc;
1296 			BP_SET_LSIZE(gbp, lsize);
1297 			BP_SET_PSIZE(gbp, lsize);
1298 			BP_SET_COMPRESS(gbp, ZIO_COMPRESS_OFF);
1299 			gbp->blk_birth = txg;
1300 			zio_nowait(zio_rewrite(zio, spa,
1301 			    zio->io_checksum, txg, gbp,
1302 			    (char *)zio->io_data + loff, lsize,
1303 			    zio_write_allocate_gang_member_done, NULL,
1304 			    zio->io_priority, zio->io_flags,
1305 			    &zio->io_bookmark));
1306 		} else {
1307 			lsize = P2ROUNDUP(resid / gbps_left, SPA_MINBLOCKSIZE);
1308 			ASSERT(lsize != SPA_MINBLOCKSIZE);
1309 			zio_nowait(zio_write_allocate(zio, spa,
1310 			    zio->io_checksum, txg, gbp,
1311 			    (char *)zio->io_data + loff, lsize,
1312 			    zio_write_allocate_gang_member_done, NULL,
1313 			    zio->io_priority, zio->io_flags));
1314 		}
1315 	}
1316 
1317 	ASSERT(resid == 0 && loff == zio->io_size);
1318 
1319 	zio->io_pipeline |= 1U << ZIO_STAGE_GANG_CHECKSUM_GENERATE;
1320 
1321 	zio_push_transform(zio, gbh, gsize, gsize);
1322 	/*
1323 	 * As much as we'd like this to be zio_wait_children_ready(),
1324 	 * updating our ASIZE doesn't happen until the io_done callback,
1325 	 * so we have to wait for that to finish in order for our BP
1326 	 * to be stable.
1327 	 */
1328 	zio_wait_children_done(zio);
1329 }
1330 
1331 /*
1332  * ==========================================================================
1333  * Allocate and free blocks
1334  * ==========================================================================
1335  */
1336 static void
1337 zio_dva_allocate(zio_t *zio)
1338 {
1339 	blkptr_t *bp = zio->io_bp;
1340 	int error;
1341 
1342 	ASSERT(BP_IS_HOLE(bp));
1343 	ASSERT3U(BP_GET_NDVAS(bp), ==, 0);
1344 	ASSERT3U(zio->io_ndvas, >, 0);
1345 	ASSERT3U(zio->io_ndvas, <=, spa_max_replication(zio->io_spa));
1346 
1347 	/* For testing, make some blocks above a certain size be gang blocks */
1348 	if (zio->io_size >= zio_gang_bang && (lbolt & 0x3) == 0) {
1349 		zio_write_allocate_gang_members(zio);
1350 		return;
1351 	}
1352 
1353 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
1354 
1355 	error = metaslab_alloc(zio->io_spa, zio->io_size, bp, zio->io_ndvas,
1356 	    zio->io_txg, NULL, B_FALSE);
1357 
1358 	if (error == 0) {
1359 		bp->blk_birth = zio->io_txg;
1360 	} else if (error == ENOSPC) {
1361 		if (zio->io_size == SPA_MINBLOCKSIZE)
1362 			panic("really, truly out of space");
1363 		zio_write_allocate_gang_members(zio);
1364 		return;
1365 	} else {
1366 		zio->io_error = error;
1367 	}
1368 	zio_next_stage(zio);
1369 }
1370 
1371 static void
1372 zio_dva_free(zio_t *zio)
1373 {
1374 	blkptr_t *bp = zio->io_bp;
1375 
1376 	metaslab_free(zio->io_spa, bp, zio->io_txg, B_FALSE);
1377 
1378 	BP_ZERO(bp);
1379 
1380 	zio_next_stage(zio);
1381 }
1382 
1383 static void
1384 zio_dva_claim(zio_t *zio)
1385 {
1386 	zio->io_error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
1387 
1388 	zio_next_stage(zio);
1389 }
1390 
1391 /*
1392  * ==========================================================================
1393  * Read and write to physical devices
1394  * ==========================================================================
1395  */
1396 
1397 static void
1398 zio_vdev_io_start(zio_t *zio)
1399 {
1400 	vdev_t *vd = zio->io_vd;
1401 	vdev_t *tvd = vd ? vd->vdev_top : NULL;
1402 	blkptr_t *bp = zio->io_bp;
1403 	uint64_t align;
1404 
1405 	if (vd == NULL) {
1406 		/* The mirror_ops handle multiple DVAs in a single BP */
1407 		vdev_mirror_ops.vdev_op_io_start(zio);
1408 		return;
1409 	}
1410 
1411 	align = 1ULL << tvd->vdev_ashift;
1412 
1413 	if (zio->io_retries == 0 && vd == tvd)
1414 		zio->io_flags |= ZIO_FLAG_FAILFAST;
1415 
1416 	if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
1417 	    vd->vdev_children == 0) {
1418 		zio->io_flags |= ZIO_FLAG_PHYSICAL;
1419 		zio->io_offset += VDEV_LABEL_START_SIZE;
1420 	}
1421 
1422 	if (P2PHASE(zio->io_size, align) != 0) {
1423 		uint64_t asize = P2ROUNDUP(zio->io_size, align);
1424 		char *abuf = zio_buf_alloc(asize);
1425 		ASSERT(vd == tvd);
1426 		if (zio->io_type == ZIO_TYPE_WRITE) {
1427 			bcopy(zio->io_data, abuf, zio->io_size);
1428 			bzero(abuf + zio->io_size, asize - zio->io_size);
1429 		}
1430 		zio_push_transform(zio, abuf, asize, asize);
1431 		ASSERT(!(zio->io_flags & ZIO_FLAG_SUBBLOCK));
1432 		zio->io_flags |= ZIO_FLAG_SUBBLOCK;
1433 	}
1434 
1435 	ASSERT(P2PHASE(zio->io_offset, align) == 0);
1436 	ASSERT(P2PHASE(zio->io_size, align) == 0);
1437 	ASSERT(bp == NULL ||
1438 	    P2ROUNDUP(ZIO_GET_IOSIZE(zio), align) == zio->io_size);
1439 	ASSERT(zio->io_type != ZIO_TYPE_WRITE || (spa_mode & FWRITE));
1440 
1441 	vdev_io_start(zio);
1442 
1443 	/* zio_next_stage_async() gets called from io completion interrupt */
1444 }
1445 
1446 static void
1447 zio_vdev_io_done(zio_t *zio)
1448 {
1449 	if (zio->io_vd == NULL)
1450 		/* The mirror_ops handle multiple DVAs in a single BP */
1451 		vdev_mirror_ops.vdev_op_io_done(zio);
1452 	else
1453 		vdev_io_done(zio);
1454 }
1455 
1456 /* XXPOLICY */
1457 boolean_t
1458 zio_should_retry(zio_t *zio)
1459 {
1460 	vdev_t *vd = zio->io_vd;
1461 
1462 	if (zio->io_error == 0)
1463 		return (B_FALSE);
1464 	if (zio->io_delegate_list != NULL)
1465 		return (B_FALSE);
1466 	if (vd && vd != vd->vdev_top)
1467 		return (B_FALSE);
1468 	if (zio->io_flags & ZIO_FLAG_DONT_RETRY)
1469 		return (B_FALSE);
1470 	if (zio->io_retries > 0)
1471 		return (B_FALSE);
1472 
1473 	return (B_TRUE);
1474 }
1475 
1476 static void
1477 zio_vdev_io_assess(zio_t *zio)
1478 {
1479 	vdev_t *vd = zio->io_vd;
1480 	vdev_t *tvd = vd ? vd->vdev_top : NULL;
1481 
1482 	ASSERT(zio->io_vsd == NULL);
1483 
1484 	if (zio->io_flags & ZIO_FLAG_SUBBLOCK) {
1485 		void *abuf;
1486 		uint64_t asize;
1487 		ASSERT(vd == tvd);
1488 		zio_pop_transform(zio, &abuf, &asize, &asize);
1489 		if (zio->io_type == ZIO_TYPE_READ)
1490 			bcopy(abuf, zio->io_data, zio->io_size);
1491 		zio_buf_free(abuf, asize);
1492 		zio->io_flags &= ~ZIO_FLAG_SUBBLOCK;
1493 	}
1494 
1495 	if (zio_injection_enabled && !zio->io_error)
1496 		zio->io_error = zio_handle_fault_injection(zio, EIO);
1497 
1498 	/*
1499 	 * If the I/O failed, determine whether we should attempt to retry it.
1500 	 */
1501 	/* XXPOLICY */
1502 	if (zio_should_retry(zio)) {
1503 		ASSERT(tvd == vd);
1504 
1505 		zio->io_retries++;
1506 		zio->io_error = 0;
1507 		zio->io_flags &= ZIO_FLAG_VDEV_INHERIT |
1508 		    ZIO_FLAG_CONFIG_GRABBED;
1509 		/* XXPOLICY */
1510 		zio->io_flags &= ~ZIO_FLAG_FAILFAST;
1511 		zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1512 		zio->io_stage = ZIO_STAGE_VDEV_IO_START - 1;
1513 
1514 		dprintf("retry #%d for %s to %s offset %llx\n",
1515 		    zio->io_retries, zio_type_name[zio->io_type],
1516 		    vdev_description(vd), zio->io_offset);
1517 
1518 		zio_next_stage_async(zio);
1519 		return;
1520 	}
1521 
1522 	if (zio->io_error != 0 && zio->io_error != ECKSUM &&
1523 	    !(zio->io_flags & ZIO_FLAG_SPECULATIVE) && vd) {
1524 		/*
1525 		 * Poor man's hotplug support.  Even if we're done retrying this
1526 		 * I/O, try to reopen the vdev to see if it's still attached.
1527 		 * To avoid excessive thrashing, we only try it once a minute.
1528 		 * This also has the effect of detecting when missing devices
1529 		 * have come back, by polling the device once a minute.
1530 		 *
1531 		 * We need to do this asynchronously because we can't grab
1532 		 * all the necessary locks way down here.
1533 		 */
1534 		if (gethrtime() - vd->vdev_last_try > 60ULL * NANOSEC) {
1535 			vd->vdev_last_try = gethrtime();
1536 			tvd->vdev_reopen_wanted = 1;
1537 			spa_async_request(vd->vdev_spa, SPA_ASYNC_REOPEN);
1538 		}
1539 	}
1540 
1541 	zio_next_stage(zio);
1542 }
1543 
1544 void
1545 zio_vdev_io_reissue(zio_t *zio)
1546 {
1547 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1548 	ASSERT(zio->io_error == 0);
1549 
1550 	zio->io_stage--;
1551 }
1552 
1553 void
1554 zio_vdev_io_redone(zio_t *zio)
1555 {
1556 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
1557 
1558 	zio->io_stage--;
1559 }
1560 
1561 void
1562 zio_vdev_io_bypass(zio_t *zio)
1563 {
1564 	ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1565 	ASSERT(zio->io_error == 0);
1566 
1567 	zio->io_flags |= ZIO_FLAG_IO_BYPASS;
1568 	zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS - 1;
1569 }
1570 
1571 /*
1572  * ==========================================================================
1573  * Generate and verify checksums
1574  * ==========================================================================
1575  */
1576 static void
1577 zio_checksum_generate(zio_t *zio)
1578 {
1579 	int checksum = zio->io_checksum;
1580 	blkptr_t *bp = zio->io_bp;
1581 
1582 	ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
1583 
1584 	BP_SET_CHECKSUM(bp, checksum);
1585 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1586 
1587 	zio_checksum(checksum, &bp->blk_cksum, zio->io_data, zio->io_size);
1588 
1589 	zio_next_stage(zio);
1590 }
1591 
1592 static void
1593 zio_gang_checksum_generate(zio_t *zio)
1594 {
1595 	zio_cksum_t zc;
1596 	zio_gbh_phys_t *gbh = zio->io_data;
1597 
1598 	ASSERT(BP_IS_GANG(zio->io_bp));
1599 	ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE);
1600 
1601 	zio_set_gang_verifier(zio, &gbh->zg_tail.zbt_cksum);
1602 
1603 	zio_checksum(ZIO_CHECKSUM_GANG_HEADER, &zc, zio->io_data, zio->io_size);
1604 
1605 	zio_next_stage(zio);
1606 }
1607 
1608 static void
1609 zio_checksum_verify(zio_t *zio)
1610 {
1611 	if (zio->io_bp != NULL) {
1612 		zio->io_error = zio_checksum_error(zio);
1613 		if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE))
1614 			zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM,
1615 			    zio->io_spa, zio->io_vd, zio, 0, 0);
1616 	}
1617 
1618 	zio_next_stage(zio);
1619 }
1620 
1621 /*
1622  * Called by RAID-Z to ensure we don't compute the checksum twice.
1623  */
1624 void
1625 zio_checksum_verified(zio_t *zio)
1626 {
1627 	zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY);
1628 }
1629 
1630 /*
1631  * Set the external verifier for a gang block based on stuff in the bp
1632  */
1633 void
1634 zio_set_gang_verifier(zio_t *zio, zio_cksum_t *zcp)
1635 {
1636 	blkptr_t *bp = zio->io_bp;
1637 
1638 	zcp->zc_word[0] = DVA_GET_VDEV(BP_IDENTITY(bp));
1639 	zcp->zc_word[1] = DVA_GET_OFFSET(BP_IDENTITY(bp));
1640 	zcp->zc_word[2] = bp->blk_birth;
1641 	zcp->zc_word[3] = 0;
1642 }
1643 
1644 /*
1645  * ==========================================================================
1646  * Define the pipeline
1647  * ==========================================================================
1648  */
1649 typedef void zio_pipe_stage_t(zio_t *zio);
1650 
1651 static void
1652 zio_badop(zio_t *zio)
1653 {
1654 	panic("Invalid I/O pipeline stage %u for zio %p", zio->io_stage, zio);
1655 }
1656 
1657 zio_pipe_stage_t *zio_pipeline[ZIO_STAGE_DONE + 2] = {
1658 	zio_badop,
1659 	zio_wait_children_ready,
1660 	zio_write_compress,
1661 	zio_checksum_generate,
1662 	zio_gang_pipeline,
1663 	zio_get_gang_header,
1664 	zio_rewrite_gang_members,
1665 	zio_free_gang_members,
1666 	zio_claim_gang_members,
1667 	zio_dva_allocate,
1668 	zio_dva_free,
1669 	zio_dva_claim,
1670 	zio_gang_checksum_generate,
1671 	zio_ready,
1672 	zio_vdev_io_start,
1673 	zio_vdev_io_done,
1674 	zio_vdev_io_assess,
1675 	zio_wait_children_done,
1676 	zio_checksum_verify,
1677 	zio_read_gang_members,
1678 	zio_read_decompress,
1679 	zio_done,
1680 	zio_badop
1681 };
1682 
1683 /*
1684  * Move an I/O to the next stage of the pipeline and execute that stage.
1685  * There's no locking on io_stage because there's no legitimate way for
1686  * multiple threads to be attempting to process the same I/O.
1687  */
1688 void
1689 zio_next_stage(zio_t *zio)
1690 {
1691 	uint32_t pipeline = zio->io_pipeline;
1692 
1693 	ASSERT(!MUTEX_HELD(&zio->io_lock));
1694 
1695 	if (zio->io_error) {
1696 		dprintf("zio %p vdev %s offset %llx stage %d error %d\n",
1697 		    zio, vdev_description(zio->io_vd),
1698 		    zio->io_offset, zio->io_stage, zio->io_error);
1699 		if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0)
1700 			pipeline &= ZIO_ERROR_PIPELINE_MASK;
1701 	}
1702 
1703 	while (((1U << ++zio->io_stage) & pipeline) == 0)
1704 		continue;
1705 
1706 	ASSERT(zio->io_stage <= ZIO_STAGE_DONE);
1707 	ASSERT(zio->io_stalled == 0);
1708 
1709 	/*
1710 	 * See the comment in zio_next_stage_async() about per-CPU taskqs.
1711 	 */
1712 	if (((1U << zio->io_stage) & zio->io_async_stages) &&
1713 	    (zio->io_stage == ZIO_STAGE_WRITE_COMPRESS) &&
1714 	    !(zio->io_flags & ZIO_FLAG_METADATA)) {
1715 		taskq_t *tq = zio->io_spa->spa_zio_issue_taskq[zio->io_type];
1716 		(void) taskq_dispatch(tq,
1717 		    (task_func_t *)zio_pipeline[zio->io_stage], zio, TQ_SLEEP);
1718 	} else {
1719 		zio_pipeline[zio->io_stage](zio);
1720 	}
1721 }
1722 
1723 void
1724 zio_next_stage_async(zio_t *zio)
1725 {
1726 	taskq_t *tq;
1727 	uint32_t pipeline = zio->io_pipeline;
1728 
1729 	ASSERT(!MUTEX_HELD(&zio->io_lock));
1730 
1731 	if (zio->io_error) {
1732 		dprintf("zio %p vdev %s offset %llx stage %d error %d\n",
1733 		    zio, vdev_description(zio->io_vd),
1734 		    zio->io_offset, zio->io_stage, zio->io_error);
1735 		if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0)
1736 			pipeline &= ZIO_ERROR_PIPELINE_MASK;
1737 	}
1738 
1739 	while (((1U << ++zio->io_stage) & pipeline) == 0)
1740 		continue;
1741 
1742 	ASSERT(zio->io_stage <= ZIO_STAGE_DONE);
1743 	ASSERT(zio->io_stalled == 0);
1744 
1745 	/*
1746 	 * For performance, we'll probably want two sets of task queues:
1747 	 * per-CPU issue taskqs and per-CPU completion taskqs.  The per-CPU
1748 	 * part is for read performance: since we have to make a pass over
1749 	 * the data to checksum it anyway, we want to do this on the same CPU
1750 	 * that issued the read, because (assuming CPU scheduling affinity)
1751 	 * that thread is probably still there.  Getting this optimization
1752 	 * right avoids performance-hostile cache-to-cache transfers.
1753 	 *
1754 	 * Note that having two sets of task queues is also necessary for
1755 	 * correctness: if all of the issue threads get bogged down waiting
1756 	 * for dependent reads (e.g. metaslab freelist) to complete, then
1757 	 * there won't be any threads available to service I/O completion
1758 	 * interrupts.
1759 	 */
1760 	if ((1U << zio->io_stage) & zio->io_async_stages) {
1761 		if (zio->io_stage < ZIO_STAGE_VDEV_IO_DONE)
1762 			tq = zio->io_spa->spa_zio_issue_taskq[zio->io_type];
1763 		else
1764 			tq = zio->io_spa->spa_zio_intr_taskq[zio->io_type];
1765 		(void) taskq_dispatch(tq,
1766 		    (task_func_t *)zio_pipeline[zio->io_stage], zio, TQ_SLEEP);
1767 	} else {
1768 		zio_pipeline[zio->io_stage](zio);
1769 	}
1770 }
1771 
1772 static boolean_t
1773 zio_alloc_should_fail(void)
1774 {
1775 	static uint16_t	allocs = 0;
1776 
1777 	return (P2PHASE(allocs++, 1U<<zio_zil_fail_shift) == 0);
1778 }
1779 
1780 /*
1781  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
1782  */
1783 int
1784 zio_alloc_blk(spa_t *spa, uint64_t size, blkptr_t *new_bp, blkptr_t *old_bp,
1785     uint64_t txg)
1786 {
1787 	int error;
1788 
1789 	spa_config_enter(spa, RW_READER, FTAG);
1790 
1791 	if (zio_zil_fail_shift && zio_alloc_should_fail()) {
1792 		spa_config_exit(spa, FTAG);
1793 		return (ENOSPC);
1794 	}
1795 
1796 	/*
1797 	 * We were passed the previous log blocks dva_t in bp->blk_dva[0].
1798 	 */
1799 	error = metaslab_alloc(spa, size, new_bp, 1, txg, old_bp, B_TRUE);
1800 
1801 	if (error == 0) {
1802 		BP_SET_LSIZE(new_bp, size);
1803 		BP_SET_PSIZE(new_bp, size);
1804 		BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
1805 		BP_SET_CHECKSUM(new_bp, ZIO_CHECKSUM_ZILOG);
1806 		BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
1807 		BP_SET_LEVEL(new_bp, 0);
1808 		BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
1809 		new_bp->blk_birth = txg;
1810 	}
1811 
1812 	spa_config_exit(spa, FTAG);
1813 
1814 	return (error);
1815 }
1816 
1817 /*
1818  * Free an intent log block.  We know it can't be a gang block, so there's
1819  * nothing to do except metaslab_free() it.
1820  */
1821 void
1822 zio_free_blk(spa_t *spa, blkptr_t *bp, uint64_t txg)
1823 {
1824 	ASSERT(!BP_IS_GANG(bp));
1825 
1826 	spa_config_enter(spa, RW_READER, FTAG);
1827 
1828 	metaslab_free(spa, bp, txg, B_FALSE);
1829 
1830 	spa_config_exit(spa, FTAG);
1831 }
1832