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