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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/zfs_context.h>
27 #include <sys/fm/fs/zfs.h>
30 #include <sys/spa_impl.h>
31 #include <sys/vdev_impl.h>
32 #include <sys/zio_impl.h>
33 #include <sys/zio_compress.h>
34 #include <sys/zio_checksum.h>
36 SYSCTL_DECL(_vfs_zfs);
37 SYSCTL_NODE(_vfs_zfs, OID_AUTO, zio, CTLFLAG_RW, 0, "ZFS ZIO");
38 static int zio_use_uma = 0;
39 TUNABLE_INT("vfs.zfs.zio.use_uma", &zio_use_uma);
40 SYSCTL_INT(_vfs_zfs_zio, OID_AUTO, use_uma, CTLFLAG_RDTUN, &zio_use_uma, 0,
41 "Use uma(9) for ZIO allocations");
44 * ==========================================================================
46 * ==========================================================================
48 uint8_t zio_priority_table[ZIO_PRIORITY_TABLE_SIZE] = {
49 0, /* ZIO_PRIORITY_NOW */
50 0, /* ZIO_PRIORITY_SYNC_READ */
51 0, /* ZIO_PRIORITY_SYNC_WRITE */
52 0, /* ZIO_PRIORITY_LOG_WRITE */
53 1, /* ZIO_PRIORITY_CACHE_FILL */
54 1, /* ZIO_PRIORITY_AGG */
55 4, /* ZIO_PRIORITY_FREE */
56 4, /* ZIO_PRIORITY_ASYNC_WRITE */
57 6, /* ZIO_PRIORITY_ASYNC_READ */
58 10, /* ZIO_PRIORITY_RESILVER */
59 20, /* ZIO_PRIORITY_SCRUB */
63 * ==========================================================================
64 * I/O type descriptions
65 * ==========================================================================
67 char *zio_type_name[ZIO_TYPES] = {
68 "zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
72 #define SYNC_PASS_DEFERRED_FREE 1 /* defer frees after this pass */
73 #define SYNC_PASS_DONT_COMPRESS 4 /* don't compress after this pass */
74 #define SYNC_PASS_REWRITE 1 /* rewrite new bps after this pass */
77 * ==========================================================================
79 * ==========================================================================
81 kmem_cache_t *zio_cache;
82 kmem_cache_t *zio_link_cache;
83 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
84 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
87 extern vmem_t *zio_alloc_arena;
91 * An allocating zio is one that either currently has the DVA allocate
92 * stage set or will have it later in its lifetime.
94 #define IO_IS_ALLOCATING(zio) \
95 ((zio)->io_orig_pipeline & (1U << ZIO_STAGE_DVA_ALLOCATE))
101 zio_cache = kmem_cache_create("zio_cache",
102 sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
103 zio_link_cache = kmem_cache_create("zio_link_cache",
104 sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
107 * For small buffers, we want a cache for each multiple of
108 * SPA_MINBLOCKSIZE. For medium-size buffers, we want a cache
109 * for each quarter-power of 2. For large buffers, we want
110 * a cache for each multiple of PAGESIZE.
112 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
113 size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
117 while (p2 & (p2 - 1))
120 if (size <= 4 * SPA_MINBLOCKSIZE) {
121 align = SPA_MINBLOCKSIZE;
122 } else if (P2PHASE(size, PAGESIZE) == 0) {
124 } else if (P2PHASE(size, p2 >> 2) == 0) {
130 (void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
131 zio_buf_cache[c] = kmem_cache_create(name, size,
132 align, NULL, NULL, NULL, NULL, NULL, KMC_NODEBUG);
134 (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
135 zio_data_buf_cache[c] = kmem_cache_create(name, size,
136 align, NULL, NULL, NULL, NULL, NULL, KMC_NODEBUG);
141 ASSERT(zio_buf_cache[c] != NULL);
142 if (zio_buf_cache[c - 1] == NULL)
143 zio_buf_cache[c - 1] = zio_buf_cache[c];
145 ASSERT(zio_data_buf_cache[c] != NULL);
146 if (zio_data_buf_cache[c - 1] == NULL)
147 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
157 kmem_cache_t *last_cache = NULL;
158 kmem_cache_t *last_data_cache = NULL;
160 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
161 if (zio_buf_cache[c] != last_cache) {
162 last_cache = zio_buf_cache[c];
163 kmem_cache_destroy(zio_buf_cache[c]);
165 zio_buf_cache[c] = NULL;
167 if (zio_data_buf_cache[c] != last_data_cache) {
168 last_data_cache = zio_data_buf_cache[c];
169 kmem_cache_destroy(zio_data_buf_cache[c]);
171 zio_data_buf_cache[c] = NULL;
174 kmem_cache_destroy(zio_link_cache);
175 kmem_cache_destroy(zio_cache);
181 * ==========================================================================
182 * Allocate and free I/O buffers
183 * ==========================================================================
187 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
188 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
189 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
190 * excess / transient data in-core during a crashdump.
193 zio_buf_alloc(size_t size)
195 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
197 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
200 return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
202 return (kmem_alloc(size, KM_SLEEP));
206 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
207 * crashdump if the kernel panics. This exists so that we will limit the amount
208 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
209 * of kernel heap dumped to disk when the kernel panics)
212 zio_data_buf_alloc(size_t size)
214 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
216 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
219 return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
221 return (kmem_alloc(size, KM_SLEEP));
225 zio_buf_free(void *buf, size_t size)
227 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
229 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
232 kmem_cache_free(zio_buf_cache[c], buf);
234 kmem_free(buf, size);
238 zio_data_buf_free(void *buf, size_t size)
240 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
242 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
245 kmem_cache_free(zio_data_buf_cache[c], buf);
247 kmem_free(buf, size);
251 * ==========================================================================
252 * Push and pop I/O transform buffers
253 * ==========================================================================
256 zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize,
257 zio_transform_func_t *transform)
259 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
261 zt->zt_orig_data = zio->io_data;
262 zt->zt_orig_size = zio->io_size;
263 zt->zt_bufsize = bufsize;
264 zt->zt_transform = transform;
266 zt->zt_next = zio->io_transform_stack;
267 zio->io_transform_stack = zt;
274 zio_pop_transforms(zio_t *zio)
278 while ((zt = zio->io_transform_stack) != NULL) {
279 if (zt->zt_transform != NULL)
280 zt->zt_transform(zio,
281 zt->zt_orig_data, zt->zt_orig_size);
283 zio_buf_free(zio->io_data, zt->zt_bufsize);
285 zio->io_data = zt->zt_orig_data;
286 zio->io_size = zt->zt_orig_size;
287 zio->io_transform_stack = zt->zt_next;
289 kmem_free(zt, sizeof (zio_transform_t));
294 * ==========================================================================
295 * I/O transform callbacks for subblocks and decompression
296 * ==========================================================================
299 zio_subblock(zio_t *zio, void *data, uint64_t size)
301 ASSERT(zio->io_size > size);
303 if (zio->io_type == ZIO_TYPE_READ)
304 bcopy(zio->io_data, data, size);
308 zio_decompress(zio_t *zio, void *data, uint64_t size)
310 if (zio->io_error == 0 &&
311 zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
312 zio->io_data, zio->io_size, data, size) != 0)
317 * ==========================================================================
318 * I/O parent/child relationships and pipeline interlocks
319 * ==========================================================================
322 * NOTE - Callers to zio_walk_parents() and zio_walk_children must
323 * continue calling these functions until they return NULL.
324 * Otherwise, the next caller will pick up the list walk in
325 * some indeterminate state. (Otherwise every caller would
326 * have to pass in a cookie to keep the state represented by
327 * io_walk_link, which gets annoying.)
330 zio_walk_parents(zio_t *cio)
332 zio_link_t *zl = cio->io_walk_link;
333 list_t *pl = &cio->io_parent_list;
335 zl = (zl == NULL) ? list_head(pl) : list_next(pl, zl);
336 cio->io_walk_link = zl;
341 ASSERT(zl->zl_child == cio);
342 return (zl->zl_parent);
346 zio_walk_children(zio_t *pio)
348 zio_link_t *zl = pio->io_walk_link;
349 list_t *cl = &pio->io_child_list;
351 zl = (zl == NULL) ? list_head(cl) : list_next(cl, zl);
352 pio->io_walk_link = zl;
357 ASSERT(zl->zl_parent == pio);
358 return (zl->zl_child);
362 zio_unique_parent(zio_t *cio)
364 zio_t *pio = zio_walk_parents(cio);
366 VERIFY(zio_walk_parents(cio) == NULL);
371 zio_add_child(zio_t *pio, zio_t *cio)
373 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
376 * Logical I/Os can have logical, gang, or vdev children.
377 * Gang I/Os can have gang or vdev children.
378 * Vdev I/Os can only have vdev children.
379 * The following ASSERT captures all of these constraints.
381 ASSERT(cio->io_child_type <= pio->io_child_type);
386 mutex_enter(&cio->io_lock);
387 mutex_enter(&pio->io_lock);
389 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
391 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
392 pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
394 list_insert_head(&pio->io_child_list, zl);
395 list_insert_head(&cio->io_parent_list, zl);
397 mutex_exit(&pio->io_lock);
398 mutex_exit(&cio->io_lock);
402 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
404 ASSERT(zl->zl_parent == pio);
405 ASSERT(zl->zl_child == cio);
407 mutex_enter(&cio->io_lock);
408 mutex_enter(&pio->io_lock);
410 list_remove(&pio->io_child_list, zl);
411 list_remove(&cio->io_parent_list, zl);
413 mutex_exit(&pio->io_lock);
414 mutex_exit(&cio->io_lock);
416 kmem_cache_free(zio_link_cache, zl);
420 zio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait)
422 uint64_t *countp = &zio->io_children[child][wait];
423 boolean_t waiting = B_FALSE;
425 mutex_enter(&zio->io_lock);
426 ASSERT(zio->io_stall == NULL);
429 zio->io_stall = countp;
432 mutex_exit(&zio->io_lock);
438 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
440 uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
441 int *errorp = &pio->io_child_error[zio->io_child_type];
443 mutex_enter(&pio->io_lock);
444 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
445 *errorp = zio_worst_error(*errorp, zio->io_error);
446 pio->io_reexecute |= zio->io_reexecute;
447 ASSERT3U(*countp, >, 0);
448 if (--*countp == 0 && pio->io_stall == countp) {
449 pio->io_stall = NULL;
450 mutex_exit(&pio->io_lock);
453 mutex_exit(&pio->io_lock);
458 zio_inherit_child_errors(zio_t *zio, enum zio_child c)
460 if (zio->io_child_error[c] != 0 && zio->io_error == 0)
461 zio->io_error = zio->io_child_error[c];
465 * ==========================================================================
466 * Create the various types of I/O (read, write, free, etc)
467 * ==========================================================================
470 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
471 void *data, uint64_t size, zio_done_func_t *done, void *private,
472 zio_type_t type, int priority, int flags, vdev_t *vd, uint64_t offset,
473 const zbookmark_t *zb, uint8_t stage, uint32_t pipeline)
477 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
478 ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
479 ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
481 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
482 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
483 ASSERT(vd || stage == ZIO_STAGE_OPEN);
485 zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
486 bzero(zio, sizeof (zio_t));
488 mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
489 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
491 list_create(&zio->io_parent_list, sizeof (zio_link_t),
492 offsetof(zio_link_t, zl_parent_node));
493 list_create(&zio->io_child_list, sizeof (zio_link_t),
494 offsetof(zio_link_t, zl_child_node));
497 zio->io_child_type = ZIO_CHILD_VDEV;
498 else if (flags & ZIO_FLAG_GANG_CHILD)
499 zio->io_child_type = ZIO_CHILD_GANG;
501 zio->io_child_type = ZIO_CHILD_LOGICAL;
505 zio->io_bp_copy = *bp;
506 zio->io_bp_orig = *bp;
507 if (type != ZIO_TYPE_WRITE)
508 zio->io_bp = &zio->io_bp_copy; /* so caller can free */
509 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
510 zio->io_logical = zio;
511 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
512 pipeline |= ZIO_GANG_STAGES;
520 zio->io_private = private;
522 zio->io_priority = priority;
524 zio->io_offset = offset;
525 zio->io_orig_flags = zio->io_flags = flags;
526 zio->io_orig_stage = zio->io_stage = stage;
527 zio->io_orig_pipeline = zio->io_pipeline = pipeline;
529 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
530 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
533 zio->io_bookmark = *zb;
536 if (zio->io_logical == NULL)
537 zio->io_logical = pio->io_logical;
538 if (zio->io_child_type == ZIO_CHILD_GANG)
539 zio->io_gang_leader = pio->io_gang_leader;
540 zio_add_child(pio, zio);
547 zio_destroy(zio_t *zio)
549 list_destroy(&zio->io_parent_list);
550 list_destroy(&zio->io_child_list);
551 mutex_destroy(&zio->io_lock);
552 cv_destroy(&zio->io_cv);
553 kmem_cache_free(zio_cache, zio);
557 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
558 void *private, int flags)
562 zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
563 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
564 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
570 zio_root(spa_t *spa, zio_done_func_t *done, void *private, int flags)
572 return (zio_null(NULL, spa, NULL, done, private, flags));
576 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
577 void *data, uint64_t size, zio_done_func_t *done, void *private,
578 int priority, int flags, const zbookmark_t *zb)
582 zio = zio_create(pio, spa, bp->blk_birth, (blkptr_t *)bp,
583 data, size, done, private,
584 ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
585 ZIO_STAGE_OPEN, ZIO_READ_PIPELINE);
591 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
592 void *data, uint64_t size, zio_prop_t *zp,
593 zio_done_func_t *ready, zio_done_func_t *done, void *private,
594 int priority, int flags, const zbookmark_t *zb)
598 ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
599 zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
600 zp->zp_compress >= ZIO_COMPRESS_OFF &&
601 zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
602 zp->zp_type < DMU_OT_NUMTYPES &&
605 zp->zp_ndvas <= spa_max_replication(spa));
606 ASSERT(ready != NULL);
608 zio = zio_create(pio, spa, txg, bp, data, size, done, private,
609 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
610 ZIO_STAGE_OPEN, ZIO_WRITE_PIPELINE);
612 zio->io_ready = ready;
619 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, void *data,
620 uint64_t size, zio_done_func_t *done, void *private, int priority,
621 int flags, zbookmark_t *zb)
625 zio = zio_create(pio, spa, txg, bp, data, size, done, private,
626 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
627 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
633 zio_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
634 zio_done_func_t *done, void *private, int flags)
638 ASSERT(!BP_IS_HOLE(bp));
640 if (bp->blk_fill == BLK_FILL_ALREADY_FREED)
641 return (zio_null(pio, spa, NULL, NULL, NULL, flags));
643 if (txg == spa->spa_syncing_txg &&
644 spa_sync_pass(spa) > SYNC_PASS_DEFERRED_FREE) {
645 bplist_enqueue_deferred(&spa->spa_sync_bplist, bp);
646 return (zio_null(pio, spa, NULL, NULL, NULL, flags));
649 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
650 done, private, ZIO_TYPE_FREE, ZIO_PRIORITY_FREE, flags,
651 NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_FREE_PIPELINE);
657 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
658 zio_done_func_t *done, void *private, int flags)
663 * A claim is an allocation of a specific block. Claims are needed
664 * to support immediate writes in the intent log. The issue is that
665 * immediate writes contain committed data, but in a txg that was
666 * *not* committed. Upon opening the pool after an unclean shutdown,
667 * the intent log claims all blocks that contain immediate write data
668 * so that the SPA knows they're in use.
670 * All claims *must* be resolved in the first txg -- before the SPA
671 * starts allocating blocks -- so that nothing is allocated twice.
673 ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
674 ASSERT3U(spa_first_txg(spa), <=, txg);
676 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
677 done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, flags,
678 NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
684 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
685 zio_done_func_t *done, void *private, int priority, int flags)
690 if (vd->vdev_children == 0) {
691 zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
692 ZIO_TYPE_IOCTL, priority, flags, vd, 0, NULL,
693 ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
697 zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
699 for (c = 0; c < vd->vdev_children; c++)
700 zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
701 done, private, priority, flags));
708 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
709 void *data, int checksum, zio_done_func_t *done, void *private,
710 int priority, int flags, boolean_t labels)
714 ASSERT(vd->vdev_children == 0);
715 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
716 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
717 ASSERT3U(offset + size, <=, vd->vdev_psize);
719 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
720 ZIO_TYPE_READ, priority, flags, vd, offset, NULL,
721 ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
723 zio->io_prop.zp_checksum = checksum;
729 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
730 void *data, int checksum, zio_done_func_t *done, void *private,
731 int priority, int flags, boolean_t labels)
735 ASSERT(vd->vdev_children == 0);
736 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
737 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
738 ASSERT3U(offset + size, <=, vd->vdev_psize);
740 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
741 ZIO_TYPE_WRITE, priority, flags, vd, offset, NULL,
742 ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
744 zio->io_prop.zp_checksum = checksum;
746 if (zio_checksum_table[checksum].ci_zbt) {
748 * zbt checksums are necessarily destructive -- they modify
749 * the end of the write buffer to hold the verifier/checksum.
750 * Therefore, we must make a local copy in case the data is
751 * being written to multiple places in parallel.
753 void *wbuf = zio_buf_alloc(size);
754 bcopy(data, wbuf, size);
755 zio_push_transform(zio, wbuf, size, size, NULL);
762 * Create a child I/O to do some work for us.
765 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
766 void *data, uint64_t size, int type, int priority, int flags,
767 zio_done_func_t *done, void *private)
769 uint32_t pipeline = ZIO_VDEV_CHILD_PIPELINE;
772 ASSERT(vd->vdev_parent ==
773 (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev));
775 if (type == ZIO_TYPE_READ && bp != NULL) {
777 * If we have the bp, then the child should perform the
778 * checksum and the parent need not. This pushes error
779 * detection as close to the leaves as possible and
780 * eliminates redundant checksums in the interior nodes.
782 pipeline |= 1U << ZIO_STAGE_CHECKSUM_VERIFY;
783 pio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY);
786 if (vd->vdev_children == 0)
787 offset += VDEV_LABEL_START_SIZE;
789 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size,
790 done, private, type, priority,
791 (pio->io_flags & ZIO_FLAG_VDEV_INHERIT) |
792 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | flags,
793 vd, offset, &pio->io_bookmark,
794 ZIO_STAGE_VDEV_IO_START - 1, pipeline);
800 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, void *data, uint64_t size,
801 int type, int priority, int flags, zio_done_func_t *done, void *private)
805 ASSERT(vd->vdev_ops->vdev_op_leaf);
807 zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
808 data, size, done, private, type, priority,
809 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY,
811 ZIO_STAGE_VDEV_IO_START - 1, ZIO_VDEV_CHILD_PIPELINE);
817 zio_flush(zio_t *zio, vdev_t *vd)
819 zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
820 NULL, NULL, ZIO_PRIORITY_NOW,
821 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
825 * ==========================================================================
826 * Prepare to read and write logical blocks
827 * ==========================================================================
831 zio_read_bp_init(zio_t *zio)
833 blkptr_t *bp = zio->io_bp;
835 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
836 zio->io_child_type == ZIO_CHILD_LOGICAL &&
837 !(zio->io_flags & ZIO_FLAG_RAW)) {
838 uint64_t csize = BP_GET_PSIZE(bp);
839 void *cbuf = zio_buf_alloc(csize);
841 zio_push_transform(zio, cbuf, csize, csize, zio_decompress);
844 if (!dmu_ot[BP_GET_TYPE(bp)].ot_metadata && BP_GET_LEVEL(bp) == 0)
845 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
847 return (ZIO_PIPELINE_CONTINUE);
851 zio_write_bp_init(zio_t *zio)
853 zio_prop_t *zp = &zio->io_prop;
854 int compress = zp->zp_compress;
855 blkptr_t *bp = zio->io_bp;
857 uint64_t lsize = zio->io_size;
858 uint64_t csize = lsize;
859 uint64_t cbufsize = 0;
863 * If our children haven't all reached the ready stage,
864 * wait for them and then repeat this pipeline stage.
866 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
867 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY))
868 return (ZIO_PIPELINE_STOP);
870 if (!IO_IS_ALLOCATING(zio))
871 return (ZIO_PIPELINE_CONTINUE);
873 ASSERT(compress != ZIO_COMPRESS_INHERIT);
875 if (bp->blk_birth == zio->io_txg) {
877 * We're rewriting an existing block, which means we're
878 * working on behalf of spa_sync(). For spa_sync() to
879 * converge, it must eventually be the case that we don't
880 * have to allocate new blocks. But compression changes
881 * the blocksize, which forces a reallocate, and makes
882 * convergence take longer. Therefore, after the first
883 * few passes, stop compressing to ensure convergence.
885 pass = spa_sync_pass(zio->io_spa);
887 if (pass > SYNC_PASS_DONT_COMPRESS)
888 compress = ZIO_COMPRESS_OFF;
890 /* Make sure someone doesn't change their mind on overwrites */
891 ASSERT(MIN(zp->zp_ndvas + BP_IS_GANG(bp),
892 spa_max_replication(zio->io_spa)) == BP_GET_NDVAS(bp));
895 if (compress != ZIO_COMPRESS_OFF) {
896 if (!zio_compress_data(compress, zio->io_data, zio->io_size,
897 &cbuf, &csize, &cbufsize)) {
898 compress = ZIO_COMPRESS_OFF;
899 } else if (csize != 0) {
900 zio_push_transform(zio, cbuf, csize, cbufsize, NULL);
905 * The final pass of spa_sync() must be all rewrites, but the first
906 * few passes offer a trade-off: allocating blocks defers convergence,
907 * but newly allocated blocks are sequential, so they can be written
908 * to disk faster. Therefore, we allow the first few passes of
909 * spa_sync() to allocate new blocks, but force rewrites after that.
910 * There should only be a handful of blocks after pass 1 in any case.
912 if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == csize &&
913 pass > SYNC_PASS_REWRITE) {
915 uint32_t gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
916 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
917 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
920 zio->io_pipeline = ZIO_WRITE_PIPELINE;
924 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
926 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
927 BP_SET_LSIZE(bp, lsize);
928 BP_SET_PSIZE(bp, csize);
929 BP_SET_COMPRESS(bp, compress);
930 BP_SET_CHECKSUM(bp, zp->zp_checksum);
931 BP_SET_TYPE(bp, zp->zp_type);
932 BP_SET_LEVEL(bp, zp->zp_level);
933 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
936 return (ZIO_PIPELINE_CONTINUE);
940 * ==========================================================================
941 * Execute the I/O pipeline
942 * ==========================================================================
946 zio_taskq_dispatch(zio_t *zio, enum zio_taskq_type q)
948 spa_t *spa = zio->io_spa;
949 zio_type_t t = zio->io_type;
954 ASSERT(q == ZIO_TASKQ_ISSUE || q == ZIO_TASKQ_INTERRUPT);
957 if (q == ZIO_TASKQ_ISSUE)
958 task = &zio->io_task_issue;
959 else /* if (q == ZIO_TASKQ_INTERRUPT) */
960 task = &zio->io_task_interrupt;
964 * If we're a config writer or a probe, the normal issue and
965 * interrupt threads may all be blocked waiting for the config lock.
966 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
968 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
972 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
974 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
978 * If this is a high priority I/O, then use the high priority taskq.
980 if (zio->io_priority == ZIO_PRIORITY_NOW &&
981 spa->spa_zio_taskq[t][q + 1] != NULL)
984 ASSERT3U(q, <, ZIO_TASKQ_TYPES);
986 (void) taskq_dispatch_safe(spa->spa_zio_taskq[t][q],
987 (task_func_t *)zio_execute, zio, task);
989 (void) taskq_dispatch(spa->spa_zio_taskq[t][q],
990 (task_func_t *)zio_execute, zio, TQ_SLEEP);
995 zio_taskq_member(zio_t *zio, enum zio_taskq_type q)
997 kthread_t *executor = zio->io_executor;
998 spa_t *spa = zio->io_spa;
1000 for (zio_type_t t = 0; t < ZIO_TYPES; t++)
1001 if (taskq_member(spa->spa_zio_taskq[t][q], executor))
1008 zio_issue_async(zio_t *zio)
1010 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE);
1012 return (ZIO_PIPELINE_STOP);
1016 zio_interrupt(zio_t *zio)
1018 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT);
1022 * Execute the I/O pipeline until one of the following occurs:
1023 * (1) the I/O completes; (2) the pipeline stalls waiting for
1024 * dependent child I/Os; (3) the I/O issues, so we're waiting
1025 * for an I/O completion interrupt; (4) the I/O is delegated by
1026 * vdev-level caching or aggregation; (5) the I/O is deferred
1027 * due to vdev-level queueing; (6) the I/O is handed off to
1028 * another thread. In all cases, the pipeline stops whenever
1029 * there's no CPU work; it never burns a thread in cv_wait().
1031 * There's no locking on io_stage because there's no legitimate way
1032 * for multiple threads to be attempting to process the same I/O.
1034 static zio_pipe_stage_t *zio_pipeline[ZIO_STAGES];
1037 zio_execute(zio_t *zio)
1039 zio->io_executor = curthread;
1041 while (zio->io_stage < ZIO_STAGE_DONE) {
1042 uint32_t pipeline = zio->io_pipeline;
1043 zio_stage_t stage = zio->io_stage;
1046 ASSERT(!MUTEX_HELD(&zio->io_lock));
1048 while (((1U << ++stage) & pipeline) == 0)
1051 ASSERT(stage <= ZIO_STAGE_DONE);
1052 ASSERT(zio->io_stall == NULL);
1055 * If we are in interrupt context and this pipeline stage
1056 * will grab a config lock that is held across I/O,
1057 * issue async to avoid deadlock.
1059 if (((1U << stage) & ZIO_CONFIG_LOCK_BLOCKING_STAGES) &&
1060 zio->io_vd == NULL &&
1061 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1062 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE);
1066 zio->io_stage = stage;
1067 rv = zio_pipeline[stage](zio);
1069 if (rv == ZIO_PIPELINE_STOP)
1072 ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1077 * ==========================================================================
1078 * Initiate I/O, either sync or async
1079 * ==========================================================================
1082 zio_wait(zio_t *zio)
1086 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1087 ASSERT(zio->io_executor == NULL);
1089 zio->io_waiter = curthread;
1093 mutex_enter(&zio->io_lock);
1094 while (zio->io_executor != NULL)
1095 cv_wait(&zio->io_cv, &zio->io_lock);
1096 mutex_exit(&zio->io_lock);
1098 error = zio->io_error;
1105 zio_nowait(zio_t *zio)
1107 ASSERT(zio->io_executor == NULL);
1109 if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1110 zio_unique_parent(zio) == NULL) {
1112 * This is a logical async I/O with no parent to wait for it.
1113 * We add it to the spa_async_root_zio "Godfather" I/O which
1114 * will ensure they complete prior to unloading the pool.
1116 spa_t *spa = zio->io_spa;
1118 zio_add_child(spa->spa_async_zio_root, zio);
1125 * ==========================================================================
1126 * Reexecute or suspend/resume failed I/O
1127 * ==========================================================================
1131 zio_reexecute(zio_t *pio)
1133 zio_t *cio, *cio_next;
1135 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1136 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1137 ASSERT(pio->io_gang_leader == NULL);
1138 ASSERT(pio->io_gang_tree == NULL);
1140 pio->io_flags = pio->io_orig_flags;
1141 pio->io_stage = pio->io_orig_stage;
1142 pio->io_pipeline = pio->io_orig_pipeline;
1143 pio->io_reexecute = 0;
1145 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1146 pio->io_state[w] = 0;
1147 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
1148 pio->io_child_error[c] = 0;
1150 if (IO_IS_ALLOCATING(pio)) {
1152 * Remember the failed bp so that the io_ready() callback
1153 * can update its accounting upon reexecution. The block
1154 * was already freed in zio_done(); we indicate this with
1155 * a fill count of -1 so that zio_free() knows to skip it.
1157 blkptr_t *bp = pio->io_bp;
1158 ASSERT(bp->blk_birth == 0 || bp->blk_birth == pio->io_txg);
1159 bp->blk_fill = BLK_FILL_ALREADY_FREED;
1160 pio->io_bp_orig = *bp;
1165 * As we reexecute pio's children, new children could be created.
1166 * New children go to the head of pio's io_child_list, however,
1167 * so we will (correctly) not reexecute them. The key is that
1168 * the remainder of pio's io_child_list, from 'cio_next' onward,
1169 * cannot be affected by any side effects of reexecuting 'cio'.
1171 for (cio = zio_walk_children(pio); cio != NULL; cio = cio_next) {
1172 cio_next = zio_walk_children(pio);
1173 mutex_enter(&pio->io_lock);
1174 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1175 pio->io_children[cio->io_child_type][w]++;
1176 mutex_exit(&pio->io_lock);
1181 * Now that all children have been reexecuted, execute the parent.
1182 * We don't reexecute "The Godfather" I/O here as it's the
1183 * responsibility of the caller to wait on him.
1185 if (!(pio->io_flags & ZIO_FLAG_GODFATHER))
1190 zio_suspend(spa_t *spa, zio_t *zio)
1192 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1193 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1194 "failure and the failure mode property for this pool "
1195 "is set to panic.", spa_name(spa));
1197 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
1199 mutex_enter(&spa->spa_suspend_lock);
1201 if (spa->spa_suspend_zio_root == NULL)
1202 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
1203 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1204 ZIO_FLAG_GODFATHER);
1206 spa->spa_suspended = B_TRUE;
1209 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
1210 ASSERT(zio != spa->spa_suspend_zio_root);
1211 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1212 ASSERT(zio_unique_parent(zio) == NULL);
1213 ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1214 zio_add_child(spa->spa_suspend_zio_root, zio);
1217 mutex_exit(&spa->spa_suspend_lock);
1221 zio_resume(spa_t *spa)
1226 * Reexecute all previously suspended i/o.
1228 mutex_enter(&spa->spa_suspend_lock);
1229 spa->spa_suspended = B_FALSE;
1230 cv_broadcast(&spa->spa_suspend_cv);
1231 pio = spa->spa_suspend_zio_root;
1232 spa->spa_suspend_zio_root = NULL;
1233 mutex_exit(&spa->spa_suspend_lock);
1239 return (zio_wait(pio));
1243 zio_resume_wait(spa_t *spa)
1245 mutex_enter(&spa->spa_suspend_lock);
1246 while (spa_suspended(spa))
1247 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
1248 mutex_exit(&spa->spa_suspend_lock);
1252 * ==========================================================================
1255 * A gang block is a collection of small blocks that looks to the DMU
1256 * like one large block. When zio_dva_allocate() cannot find a block
1257 * of the requested size, due to either severe fragmentation or the pool
1258 * being nearly full, it calls zio_write_gang_block() to construct the
1259 * block from smaller fragments.
1261 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1262 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
1263 * an indirect block: it's an array of block pointers. It consumes
1264 * only one sector and hence is allocatable regardless of fragmentation.
1265 * The gang header's bps point to its gang members, which hold the data.
1267 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1268 * as the verifier to ensure uniqueness of the SHA256 checksum.
1269 * Critically, the gang block bp's blk_cksum is the checksum of the data,
1270 * not the gang header. This ensures that data block signatures (needed for
1271 * deduplication) are independent of how the block is physically stored.
1273 * Gang blocks can be nested: a gang member may itself be a gang block.
1274 * Thus every gang block is a tree in which root and all interior nodes are
1275 * gang headers, and the leaves are normal blocks that contain user data.
1276 * The root of the gang tree is called the gang leader.
1278 * To perform any operation (read, rewrite, free, claim) on a gang block,
1279 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1280 * in the io_gang_tree field of the original logical i/o by recursively
1281 * reading the gang leader and all gang headers below it. This yields
1282 * an in-core tree containing the contents of every gang header and the
1283 * bps for every constituent of the gang block.
1285 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1286 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
1287 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1288 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1289 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1290 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
1291 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1292 * of the gang header plus zio_checksum_compute() of the data to update the
1293 * gang header's blk_cksum as described above.
1295 * The two-phase assemble/issue model solves the problem of partial failure --
1296 * what if you'd freed part of a gang block but then couldn't read the
1297 * gang header for another part? Assembling the entire gang tree first
1298 * ensures that all the necessary gang header I/O has succeeded before
1299 * starting the actual work of free, claim, or write. Once the gang tree
1300 * is assembled, free and claim are in-memory operations that cannot fail.
1302 * In the event that a gang write fails, zio_dva_unallocate() walks the
1303 * gang tree to immediately free (i.e. insert back into the space map)
1304 * everything we've allocated. This ensures that we don't get ENOSPC
1305 * errors during repeated suspend/resume cycles due to a flaky device.
1307 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
1308 * the gang tree, we won't modify the block, so we can safely defer the free
1309 * (knowing that the block is still intact). If we *can* assemble the gang
1310 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1311 * each constituent bp and we can allocate a new block on the next sync pass.
1313 * In all cases, the gang tree allows complete recovery from partial failure.
1314 * ==========================================================================
1318 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1323 return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp),
1324 NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1325 &pio->io_bookmark));
1329 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1334 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1335 gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority,
1336 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1338 * As we rewrite each gang header, the pipeline will compute
1339 * a new gang block header checksum for it; but no one will
1340 * compute a new data checksum, so we do that here. The one
1341 * exception is the gang leader: the pipeline already computed
1342 * its data checksum because that stage precedes gang assembly.
1343 * (Presently, nothing actually uses interior data checksums;
1344 * this is just good hygiene.)
1346 if (gn != pio->io_gang_leader->io_gang_tree) {
1347 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
1348 data, BP_GET_PSIZE(bp));
1351 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1352 data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority,
1353 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1361 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1363 return (zio_free(pio, pio->io_spa, pio->io_txg, bp,
1364 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
1369 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1371 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
1372 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
1375 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
1384 static void zio_gang_tree_assemble_done(zio_t *zio);
1386 static zio_gang_node_t *
1387 zio_gang_node_alloc(zio_gang_node_t **gnpp)
1389 zio_gang_node_t *gn;
1391 ASSERT(*gnpp == NULL);
1393 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
1394 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
1401 zio_gang_node_free(zio_gang_node_t **gnpp)
1403 zio_gang_node_t *gn = *gnpp;
1405 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
1406 ASSERT(gn->gn_child[g] == NULL);
1408 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
1409 kmem_free(gn, sizeof (*gn));
1414 zio_gang_tree_free(zio_gang_node_t **gnpp)
1416 zio_gang_node_t *gn = *gnpp;
1421 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
1422 zio_gang_tree_free(&gn->gn_child[g]);
1424 zio_gang_node_free(gnpp);
1428 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
1430 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
1432 ASSERT(gio->io_gang_leader == gio);
1433 ASSERT(BP_IS_GANG(bp));
1435 zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh,
1436 SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn,
1437 gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
1441 zio_gang_tree_assemble_done(zio_t *zio)
1443 zio_t *gio = zio->io_gang_leader;
1444 zio_gang_node_t *gn = zio->io_private;
1445 blkptr_t *bp = zio->io_bp;
1447 ASSERT(gio == zio_unique_parent(zio));
1448 ASSERT(zio_walk_children(zio) == NULL);
1453 if (BP_SHOULD_BYTESWAP(bp))
1454 byteswap_uint64_array(zio->io_data, zio->io_size);
1456 ASSERT(zio->io_data == gn->gn_gbh);
1457 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
1458 ASSERT(gn->gn_gbh->zg_tail.zbt_magic == ZBT_MAGIC);
1460 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1461 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1462 if (!BP_IS_GANG(gbp))
1464 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
1469 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data)
1471 zio_t *gio = pio->io_gang_leader;
1474 ASSERT(BP_IS_GANG(bp) == !!gn);
1475 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
1476 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
1479 * If you're a gang header, your data is in gn->gn_gbh.
1480 * If you're a gang member, your data is in 'data' and gn == NULL.
1482 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data);
1485 ASSERT(gn->gn_gbh->zg_tail.zbt_magic == ZBT_MAGIC);
1487 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1488 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1489 if (BP_IS_HOLE(gbp))
1491 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data);
1492 data = (char *)data + BP_GET_PSIZE(gbp);
1496 if (gn == gio->io_gang_tree)
1497 ASSERT3P((char *)gio->io_data + gio->io_size, ==, data);
1504 zio_gang_assemble(zio_t *zio)
1506 blkptr_t *bp = zio->io_bp;
1508 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
1509 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
1511 zio->io_gang_leader = zio;
1513 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
1515 return (ZIO_PIPELINE_CONTINUE);
1519 zio_gang_issue(zio_t *zio)
1521 blkptr_t *bp = zio->io_bp;
1523 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
1524 return (ZIO_PIPELINE_STOP);
1526 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
1527 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
1529 if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
1530 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data);
1532 zio_gang_tree_free(&zio->io_gang_tree);
1534 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1536 return (ZIO_PIPELINE_CONTINUE);
1540 zio_write_gang_member_ready(zio_t *zio)
1542 zio_t *pio = zio_unique_parent(zio);
1543 zio_t *gio = zio->io_gang_leader;
1544 dva_t *cdva = zio->io_bp->blk_dva;
1545 dva_t *pdva = pio->io_bp->blk_dva;
1548 if (BP_IS_HOLE(zio->io_bp))
1551 ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
1553 ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
1554 ASSERT3U(zio->io_prop.zp_ndvas, ==, gio->io_prop.zp_ndvas);
1555 ASSERT3U(zio->io_prop.zp_ndvas, <=, BP_GET_NDVAS(zio->io_bp));
1556 ASSERT3U(pio->io_prop.zp_ndvas, <=, BP_GET_NDVAS(pio->io_bp));
1557 ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
1559 mutex_enter(&pio->io_lock);
1560 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
1561 ASSERT(DVA_GET_GANG(&pdva[d]));
1562 asize = DVA_GET_ASIZE(&pdva[d]);
1563 asize += DVA_GET_ASIZE(&cdva[d]);
1564 DVA_SET_ASIZE(&pdva[d], asize);
1566 mutex_exit(&pio->io_lock);
1570 zio_write_gang_block(zio_t *pio)
1572 spa_t *spa = pio->io_spa;
1573 blkptr_t *bp = pio->io_bp;
1574 zio_t *gio = pio->io_gang_leader;
1576 zio_gang_node_t *gn, **gnpp;
1577 zio_gbh_phys_t *gbh;
1578 uint64_t txg = pio->io_txg;
1579 uint64_t resid = pio->io_size;
1581 int ndvas = gio->io_prop.zp_ndvas;
1582 int gbh_ndvas = MIN(ndvas + 1, spa_max_replication(spa));
1586 error = metaslab_alloc(spa, spa->spa_normal_class, SPA_GANGBLOCKSIZE,
1587 bp, gbh_ndvas, txg, pio == gio ? NULL : gio->io_bp,
1588 METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER);
1590 pio->io_error = error;
1591 return (ZIO_PIPELINE_CONTINUE);
1595 gnpp = &gio->io_gang_tree;
1597 gnpp = pio->io_private;
1598 ASSERT(pio->io_ready == zio_write_gang_member_ready);
1601 gn = zio_gang_node_alloc(gnpp);
1603 bzero(gbh, SPA_GANGBLOCKSIZE);
1606 * Create the gang header.
1608 zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL,
1609 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1612 * Create and nowait the gang children.
1614 for (int g = 0; resid != 0; resid -= lsize, g++) {
1615 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
1617 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
1619 zp.zp_checksum = gio->io_prop.zp_checksum;
1620 zp.zp_compress = ZIO_COMPRESS_OFF;
1621 zp.zp_type = DMU_OT_NONE;
1623 zp.zp_ndvas = gio->io_prop.zp_ndvas;
1625 zio_nowait(zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
1626 (char *)pio->io_data + (pio->io_size - resid), lsize, &zp,
1627 zio_write_gang_member_ready, NULL, &gn->gn_child[g],
1628 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1629 &pio->io_bookmark));
1633 * Set pio's pipeline to just wait for zio to finish.
1635 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1639 return (ZIO_PIPELINE_CONTINUE);
1643 * ==========================================================================
1644 * Allocate and free blocks
1645 * ==========================================================================
1649 zio_dva_allocate(zio_t *zio)
1651 spa_t *spa = zio->io_spa;
1652 metaslab_class_t *mc = spa->spa_normal_class;
1653 blkptr_t *bp = zio->io_bp;
1656 if (zio->io_gang_leader == NULL) {
1657 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
1658 zio->io_gang_leader = zio;
1661 ASSERT(BP_IS_HOLE(bp));
1662 ASSERT3U(BP_GET_NDVAS(bp), ==, 0);
1663 ASSERT3U(zio->io_prop.zp_ndvas, >, 0);
1664 ASSERT3U(zio->io_prop.zp_ndvas, <=, spa_max_replication(spa));
1665 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
1667 error = metaslab_alloc(spa, mc, zio->io_size, bp,
1668 zio->io_prop.zp_ndvas, zio->io_txg, NULL, 0);
1671 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
1672 return (zio_write_gang_block(zio));
1673 zio->io_error = error;
1676 return (ZIO_PIPELINE_CONTINUE);
1680 zio_dva_free(zio_t *zio)
1682 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
1684 return (ZIO_PIPELINE_CONTINUE);
1688 zio_dva_claim(zio_t *zio)
1692 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
1694 zio->io_error = error;
1696 return (ZIO_PIPELINE_CONTINUE);
1700 * Undo an allocation. This is used by zio_done() when an I/O fails
1701 * and we want to give back the block we just allocated.
1702 * This handles both normal blocks and gang blocks.
1705 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
1707 spa_t *spa = zio->io_spa;
1708 boolean_t now = !(zio->io_flags & ZIO_FLAG_IO_REWRITE);
1710 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
1712 if (zio->io_bp == bp && !now) {
1714 * This is a rewrite for sync-to-convergence.
1715 * We can't do a metaslab_free(NOW) because bp wasn't allocated
1716 * during this sync pass, which means that metaslab_sync()
1717 * already committed the allocation.
1719 ASSERT(DVA_EQUAL(BP_IDENTITY(bp),
1720 BP_IDENTITY(&zio->io_bp_orig)));
1721 ASSERT(spa_sync_pass(spa) > 1);
1723 if (BP_IS_GANG(bp) && gn == NULL) {
1725 * This is a gang leader whose gang header(s) we
1726 * couldn't read now, so defer the free until later.
1727 * The block should still be intact because without
1728 * the headers, we'd never even start the rewrite.
1730 bplist_enqueue_deferred(&spa->spa_sync_bplist, bp);
1735 if (!BP_IS_HOLE(bp))
1736 metaslab_free(spa, bp, bp->blk_birth, now);
1739 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1740 zio_dva_unallocate(zio, gn->gn_child[g],
1741 &gn->gn_gbh->zg_blkptr[g]);
1747 * Try to allocate an intent log block. Return 0 on success, errno on failure.
1750 zio_alloc_blk(spa_t *spa, uint64_t size, blkptr_t *new_bp, blkptr_t *old_bp,
1755 error = metaslab_alloc(spa, spa->spa_log_class, size,
1756 new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID);
1759 error = metaslab_alloc(spa, spa->spa_normal_class, size,
1760 new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID);
1763 BP_SET_LSIZE(new_bp, size);
1764 BP_SET_PSIZE(new_bp, size);
1765 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
1766 BP_SET_CHECKSUM(new_bp, ZIO_CHECKSUM_ZILOG);
1767 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
1768 BP_SET_LEVEL(new_bp, 0);
1769 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
1776 * Free an intent log block. We know it can't be a gang block, so there's
1777 * nothing to do except metaslab_free() it.
1780 zio_free_blk(spa_t *spa, blkptr_t *bp, uint64_t txg)
1782 ASSERT(!BP_IS_GANG(bp));
1784 metaslab_free(spa, bp, txg, B_FALSE);
1788 * ==========================================================================
1789 * Read and write to physical devices
1790 * ==========================================================================
1793 zio_vdev_io_start(zio_t *zio)
1795 vdev_t *vd = zio->io_vd;
1797 spa_t *spa = zio->io_spa;
1799 ASSERT(zio->io_error == 0);
1800 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
1803 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
1804 spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
1807 * The mirror_ops handle multiple DVAs in a single BP.
1809 return (vdev_mirror_ops.vdev_op_io_start(zio));
1812 align = 1ULL << vd->vdev_top->vdev_ashift;
1814 if (P2PHASE(zio->io_size, align) != 0) {
1815 uint64_t asize = P2ROUNDUP(zio->io_size, align);
1816 char *abuf = zio_buf_alloc(asize);
1817 ASSERT(vd == vd->vdev_top);
1818 if (zio->io_type == ZIO_TYPE_WRITE) {
1819 bcopy(zio->io_data, abuf, zio->io_size);
1820 bzero(abuf + zio->io_size, asize - zio->io_size);
1822 zio_push_transform(zio, abuf, asize, asize, zio_subblock);
1825 ASSERT(P2PHASE(zio->io_offset, align) == 0);
1826 ASSERT(P2PHASE(zio->io_size, align) == 0);
1827 ASSERT(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
1830 * If this is a repair I/O, and there's no self-healing involved --
1831 * that is, we're just resilvering what we expect to resilver --
1832 * then don't do the I/O unless zio's txg is actually in vd's DTL.
1833 * This prevents spurious resilvering with nested replication.
1834 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
1835 * A is out of date, we'll read from C+D, then use the data to
1836 * resilver A+B -- but we don't actually want to resilver B, just A.
1837 * The top-level mirror has no way to know this, so instead we just
1838 * discard unnecessary repairs as we work our way down the vdev tree.
1839 * The same logic applies to any form of nested replication:
1840 * ditto + mirror, RAID-Z + replacing, etc. This covers them all.
1842 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
1843 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
1844 zio->io_txg != 0 && /* not a delegated i/o */
1845 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
1846 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1847 zio_vdev_io_bypass(zio);
1848 return (ZIO_PIPELINE_CONTINUE);
1851 if (vd->vdev_ops->vdev_op_leaf &&
1852 (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) {
1854 if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
1855 return (ZIO_PIPELINE_CONTINUE);
1857 if ((zio = vdev_queue_io(zio)) == NULL)
1858 return (ZIO_PIPELINE_STOP);
1860 if (!vdev_accessible(vd, zio)) {
1861 zio->io_error = ENXIO;
1863 return (ZIO_PIPELINE_STOP);
1867 return (vd->vdev_ops->vdev_op_io_start(zio));
1871 zio_vdev_io_done(zio_t *zio)
1873 vdev_t *vd = zio->io_vd;
1874 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
1875 boolean_t unexpected_error = B_FALSE;
1877 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
1878 return (ZIO_PIPELINE_STOP);
1880 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
1882 if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
1884 vdev_queue_io_done(zio);
1886 if (zio->io_type == ZIO_TYPE_WRITE)
1887 vdev_cache_write(zio);
1889 if (zio_injection_enabled && zio->io_error == 0)
1890 zio->io_error = zio_handle_device_injection(vd,
1893 if (zio_injection_enabled && zio->io_error == 0)
1894 zio->io_error = zio_handle_label_injection(zio, EIO);
1896 if (zio->io_error) {
1897 if (!vdev_accessible(vd, zio)) {
1898 zio->io_error = ENXIO;
1900 unexpected_error = B_TRUE;
1905 ops->vdev_op_io_done(zio);
1907 if (unexpected_error)
1908 VERIFY(vdev_probe(vd, zio) == NULL);
1910 return (ZIO_PIPELINE_CONTINUE);
1914 zio_vdev_io_assess(zio_t *zio)
1916 vdev_t *vd = zio->io_vd;
1918 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
1919 return (ZIO_PIPELINE_STOP);
1921 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
1922 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
1924 if (zio->io_vsd != NULL) {
1925 zio->io_vsd_free(zio);
1929 if (zio_injection_enabled && zio->io_error == 0)
1930 zio->io_error = zio_handle_fault_injection(zio, EIO);
1933 * If the I/O failed, determine whether we should attempt to retry it.
1935 if (zio->io_error && vd == NULL &&
1936 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
1937 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
1938 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */
1940 zio->io_flags |= ZIO_FLAG_IO_RETRY |
1941 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
1942 zio->io_stage = ZIO_STAGE_VDEV_IO_START - 1;
1943 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE);
1944 return (ZIO_PIPELINE_STOP);
1948 * If we got an error on a leaf device, convert it to ENXIO
1949 * if the device is not accessible at all.
1951 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
1952 !vdev_accessible(vd, zio))
1953 zio->io_error = ENXIO;
1956 * If we can't write to an interior vdev (mirror or RAID-Z),
1957 * set vdev_cant_write so that we stop trying to allocate from it.
1959 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
1960 vd != NULL && !vd->vdev_ops->vdev_op_leaf)
1961 vd->vdev_cant_write = B_TRUE;
1964 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1966 return (ZIO_PIPELINE_CONTINUE);
1970 zio_vdev_io_reissue(zio_t *zio)
1972 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1973 ASSERT(zio->io_error == 0);
1979 zio_vdev_io_redone(zio_t *zio)
1981 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
1987 zio_vdev_io_bypass(zio_t *zio)
1989 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
1990 ASSERT(zio->io_error == 0);
1992 zio->io_flags |= ZIO_FLAG_IO_BYPASS;
1993 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS - 1;
1997 * ==========================================================================
1998 * Generate and verify checksums
1999 * ==========================================================================
2002 zio_checksum_generate(zio_t *zio)
2004 blkptr_t *bp = zio->io_bp;
2005 enum zio_checksum checksum;
2009 * This is zio_write_phys().
2010 * We're either generating a label checksum, or none at all.
2012 checksum = zio->io_prop.zp_checksum;
2014 if (checksum == ZIO_CHECKSUM_OFF)
2015 return (ZIO_PIPELINE_CONTINUE);
2017 ASSERT(checksum == ZIO_CHECKSUM_LABEL);
2019 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
2020 ASSERT(!IO_IS_ALLOCATING(zio));
2021 checksum = ZIO_CHECKSUM_GANG_HEADER;
2023 checksum = BP_GET_CHECKSUM(bp);
2027 zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size);
2029 return (ZIO_PIPELINE_CONTINUE);
2033 zio_checksum_verify(zio_t *zio)
2035 blkptr_t *bp = zio->io_bp;
2040 * This is zio_read_phys().
2041 * We're either verifying a label checksum, or nothing at all.
2043 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
2044 return (ZIO_PIPELINE_CONTINUE);
2046 ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
2049 if ((error = zio_checksum_error(zio)) != 0) {
2050 zio->io_error = error;
2051 if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2052 zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM,
2053 zio->io_spa, zio->io_vd, zio, 0, 0);
2057 return (ZIO_PIPELINE_CONTINUE);
2061 * Called by RAID-Z to ensure we don't compute the checksum twice.
2064 zio_checksum_verified(zio_t *zio)
2066 zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY);
2070 * ==========================================================================
2071 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
2072 * An error of 0 indictes success. ENXIO indicates whole-device failure,
2073 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
2074 * indicate errors that are specific to one I/O, and most likely permanent.
2075 * Any other error is presumed to be worse because we weren't expecting it.
2076 * ==========================================================================
2079 zio_worst_error(int e1, int e2)
2081 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
2084 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
2085 if (e1 == zio_error_rank[r1])
2088 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
2089 if (e2 == zio_error_rank[r2])
2092 return (r1 > r2 ? e1 : e2);
2096 * ==========================================================================
2098 * ==========================================================================
2101 zio_ready(zio_t *zio)
2103 blkptr_t *bp = zio->io_bp;
2104 zio_t *pio, *pio_next;
2106 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY))
2107 return (ZIO_PIPELINE_STOP);
2109 if (zio->io_ready) {
2110 ASSERT(IO_IS_ALLOCATING(zio));
2111 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
2112 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
2117 if (bp != NULL && bp != &zio->io_bp_copy)
2118 zio->io_bp_copy = *bp;
2121 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2123 mutex_enter(&zio->io_lock);
2124 zio->io_state[ZIO_WAIT_READY] = 1;
2125 pio = zio_walk_parents(zio);
2126 mutex_exit(&zio->io_lock);
2129 * As we notify zio's parents, new parents could be added.
2130 * New parents go to the head of zio's io_parent_list, however,
2131 * so we will (correctly) not notify them. The remainder of zio's
2132 * io_parent_list, from 'pio_next' onward, cannot change because
2133 * all parents must wait for us to be done before they can be done.
2135 for (; pio != NULL; pio = pio_next) {
2136 pio_next = zio_walk_parents(zio);
2137 zio_notify_parent(pio, zio, ZIO_WAIT_READY);
2140 return (ZIO_PIPELINE_CONTINUE);
2144 zio_done(zio_t *zio)
2146 spa_t *spa = zio->io_spa;
2147 zio_t *lio = zio->io_logical;
2148 blkptr_t *bp = zio->io_bp;
2149 vdev_t *vd = zio->io_vd;
2150 uint64_t psize = zio->io_size;
2151 zio_t *pio, *pio_next;
2154 * If our children haven't all completed,
2155 * wait for them and then repeat this pipeline stage.
2157 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
2158 zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
2159 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
2160 return (ZIO_PIPELINE_STOP);
2162 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
2163 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
2164 ASSERT(zio->io_children[c][w] == 0);
2167 ASSERT(bp->blk_pad[0] == 0);
2168 ASSERT(bp->blk_pad[1] == 0);
2169 ASSERT(bp->blk_pad[2] == 0);
2170 ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
2171 (bp == zio_unique_parent(zio)->io_bp));
2172 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
2173 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
2174 ASSERT(!BP_SHOULD_BYTESWAP(bp));
2175 ASSERT3U(zio->io_prop.zp_ndvas, <=, BP_GET_NDVAS(bp));
2176 ASSERT(BP_COUNT_GANG(bp) == 0 ||
2177 (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
2182 * If there were child vdev or gang errors, they apply to us now.
2184 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
2185 zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
2187 zio_pop_transforms(zio); /* note: may set zio->io_error */
2189 vdev_stat_update(zio, psize);
2191 if (zio->io_error) {
2193 * If this I/O is attached to a particular vdev,
2194 * generate an error message describing the I/O failure
2195 * at the block level. We ignore these errors if the
2196 * device is currently unavailable.
2198 if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
2199 zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0);
2201 if ((zio->io_error == EIO ||
2202 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) && zio == lio) {
2204 * For logical I/O requests, tell the SPA to log the
2205 * error and generate a logical data ereport.
2207 spa_log_error(spa, zio);
2208 zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio,
2213 if (zio->io_error && zio == lio) {
2215 * Determine whether zio should be reexecuted. This will
2216 * propagate all the way to the root via zio_notify_parent().
2218 ASSERT(vd == NULL && bp != NULL);
2220 if (IO_IS_ALLOCATING(zio))
2221 if (zio->io_error != ENOSPC)
2222 zio->io_reexecute |= ZIO_REEXECUTE_NOW;
2224 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2226 if ((zio->io_type == ZIO_TYPE_READ ||
2227 zio->io_type == ZIO_TYPE_FREE) &&
2228 zio->io_error == ENXIO &&
2229 spa->spa_load_state == SPA_LOAD_NONE &&
2230 spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
2231 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2233 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
2234 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2238 * If there were logical child errors, they apply to us now.
2239 * We defer this until now to avoid conflating logical child
2240 * errors with errors that happened to the zio itself when
2241 * updating vdev stats and reporting FMA events above.
2243 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
2245 if ((zio->io_error || zio->io_reexecute) && IO_IS_ALLOCATING(zio) &&
2246 zio->io_child_type == ZIO_CHILD_LOGICAL) {
2247 ASSERT(zio->io_child_type != ZIO_CHILD_GANG);
2248 zio_dva_unallocate(zio, zio->io_gang_tree, bp);
2251 zio_gang_tree_free(&zio->io_gang_tree);
2254 * Godfather I/Os should never suspend.
2256 if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
2257 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
2258 zio->io_reexecute = 0;
2260 if (zio->io_reexecute) {
2262 * This is a logical I/O that wants to reexecute.
2264 * Reexecute is top-down. When an i/o fails, if it's not
2265 * the root, it simply notifies its parent and sticks around.
2266 * The parent, seeing that it still has children in zio_done(),
2267 * does the same. This percolates all the way up to the root.
2268 * The root i/o will reexecute or suspend the entire tree.
2270 * This approach ensures that zio_reexecute() honors
2271 * all the original i/o dependency relationships, e.g.
2272 * parents not executing until children are ready.
2274 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2276 zio->io_gang_leader = NULL;
2278 mutex_enter(&zio->io_lock);
2279 zio->io_state[ZIO_WAIT_DONE] = 1;
2280 mutex_exit(&zio->io_lock);
2283 * "The Godfather" I/O monitors its children but is
2284 * not a true parent to them. It will track them through
2285 * the pipeline but severs its ties whenever they get into
2286 * trouble (e.g. suspended). This allows "The Godfather"
2287 * I/O to return status without blocking.
2289 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
2290 zio_link_t *zl = zio->io_walk_link;
2291 pio_next = zio_walk_parents(zio);
2293 if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
2294 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
2295 zio_remove_child(pio, zio, zl);
2296 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
2300 if ((pio = zio_unique_parent(zio)) != NULL) {
2302 * We're not a root i/o, so there's nothing to do
2303 * but notify our parent. Don't propagate errors
2304 * upward since we haven't permanently failed yet.
2306 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
2307 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
2308 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
2309 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
2311 * We'd fail again if we reexecuted now, so suspend
2312 * until conditions improve (e.g. device comes online).
2314 zio_suspend(spa, zio);
2317 * Reexecution is potentially a huge amount of work.
2318 * Hand it off to the otherwise-unused claim taskq.
2321 (void) taskq_dispatch_safe(
2322 spa->spa_zio_taskq[ZIO_TYPE_CLAIM][ZIO_TASKQ_ISSUE],
2323 (task_func_t *)zio_reexecute, zio,
2324 &zio->io_task_issue);
2326 (void) taskq_dispatch(
2327 spa->spa_zio_taskq[ZIO_TYPE_CLAIM][ZIO_TASKQ_ISSUE],
2328 (task_func_t *)zio_reexecute, zio, TQ_SLEEP);
2331 return (ZIO_PIPELINE_STOP);
2334 ASSERT(zio_walk_children(zio) == NULL);
2335 ASSERT(zio->io_reexecute == 0);
2336 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
2339 * It is the responsibility of the done callback to ensure that this
2340 * particular zio is no longer discoverable for adoption, and as
2341 * such, cannot acquire any new parents.
2346 mutex_enter(&zio->io_lock);
2347 zio->io_state[ZIO_WAIT_DONE] = 1;
2348 mutex_exit(&zio->io_lock);
2350 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
2351 zio_link_t *zl = zio->io_walk_link;
2352 pio_next = zio_walk_parents(zio);
2353 zio_remove_child(pio, zio, zl);
2354 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
2357 if (zio->io_waiter != NULL) {
2358 mutex_enter(&zio->io_lock);
2359 zio->io_executor = NULL;
2360 cv_broadcast(&zio->io_cv);
2361 mutex_exit(&zio->io_lock);
2366 return (ZIO_PIPELINE_STOP);
2370 * ==========================================================================
2371 * I/O pipeline definition
2372 * ==========================================================================
2374 static zio_pipe_stage_t *zio_pipeline[ZIO_STAGES] = {
2379 zio_checksum_generate,
2389 zio_checksum_verify,