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