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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
24 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
28 #include <sys/sysmacros.h>
29 #include <sys/zfs_context.h>
30 #include <sys/fm/fs/zfs.h>
33 #include <sys/spa_impl.h>
34 #include <sys/vdev_impl.h>
35 #include <sys/zio_impl.h>
36 #include <sys/zio_compress.h>
37 #include <sys/zio_checksum.h>
38 #include <sys/dmu_objset.h>
41 #include <sys/trim_map.h>
42 #include <sys/blkptr.h>
43 #include <sys/zfeature.h>
45 SYSCTL_DECL(_vfs_zfs);
46 SYSCTL_NODE(_vfs_zfs, OID_AUTO, zio, CTLFLAG_RW, 0, "ZFS ZIO");
47 #if defined(__amd64__)
48 static int zio_use_uma = 1;
50 static int zio_use_uma = 0;
52 TUNABLE_INT("vfs.zfs.zio.use_uma", &zio_use_uma);
53 SYSCTL_INT(_vfs_zfs_zio, OID_AUTO, use_uma, CTLFLAG_RDTUN, &zio_use_uma, 0,
54 "Use uma(9) for ZIO allocations");
55 static int zio_exclude_metadata = 0;
56 TUNABLE_INT("vfs.zfs.zio.exclude_metadata", &zio_exclude_metadata);
57 SYSCTL_INT(_vfs_zfs_zio, OID_AUTO, exclude_metadata, CTLFLAG_RDTUN, &zio_exclude_metadata, 0,
58 "Exclude metadata buffers from dumps as well");
60 zio_trim_stats_t zio_trim_stats = {
61 { "bytes", KSTAT_DATA_UINT64,
62 "Number of bytes successfully TRIMmed" },
63 { "success", KSTAT_DATA_UINT64,
64 "Number of successful TRIM requests" },
65 { "unsupported", KSTAT_DATA_UINT64,
66 "Number of TRIM requests that failed because TRIM is not supported" },
67 { "failed", KSTAT_DATA_UINT64,
68 "Number of TRIM requests that failed for reasons other than not supported" },
71 static kstat_t *zio_trim_ksp;
74 * ==========================================================================
75 * I/O type descriptions
76 * ==========================================================================
78 const char *zio_type_name[ZIO_TYPES] = {
79 "zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
84 * ==========================================================================
86 * ==========================================================================
88 kmem_cache_t *zio_cache;
89 kmem_cache_t *zio_link_cache;
90 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
91 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
94 extern vmem_t *zio_alloc_arena;
97 #define ZIO_PIPELINE_CONTINUE 0x100
98 #define ZIO_PIPELINE_STOP 0x101
100 #define BP_SPANB(indblkshift, level) \
101 (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
102 #define COMPARE_META_LEVEL 0x80000000ul
104 * The following actions directly effect the spa's sync-to-convergence logic.
105 * The values below define the sync pass when we start performing the action.
106 * Care should be taken when changing these values as they directly impact
107 * spa_sync() performance. Tuning these values may introduce subtle performance
108 * pathologies and should only be done in the context of performance analysis.
109 * These tunables will eventually be removed and replaced with #defines once
110 * enough analysis has been done to determine optimal values.
112 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
113 * regular blocks are not deferred.
115 int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
116 TUNABLE_INT("vfs.zfs.sync_pass_deferred_free", &zfs_sync_pass_deferred_free);
117 SYSCTL_INT(_vfs_zfs, OID_AUTO, sync_pass_deferred_free, CTLFLAG_RDTUN,
118 &zfs_sync_pass_deferred_free, 0, "defer frees starting in this pass");
119 int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
120 TUNABLE_INT("vfs.zfs.sync_pass_dont_compress", &zfs_sync_pass_dont_compress);
121 SYSCTL_INT(_vfs_zfs, OID_AUTO, sync_pass_dont_compress, CTLFLAG_RDTUN,
122 &zfs_sync_pass_dont_compress, 0, "don't compress starting in this pass");
123 int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
124 TUNABLE_INT("vfs.zfs.sync_pass_rewrite", &zfs_sync_pass_rewrite);
125 SYSCTL_INT(_vfs_zfs, OID_AUTO, sync_pass_rewrite, CTLFLAG_RDTUN,
126 &zfs_sync_pass_rewrite, 0, "rewrite new bps starting in this pass");
129 * An allocating zio is one that either currently has the DVA allocate
130 * stage set or will have it later in its lifetime.
132 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
134 boolean_t zio_requeue_io_start_cut_in_line = B_TRUE;
137 int zio_buf_debug_limit = 16384;
139 int zio_buf_debug_limit = 0;
146 zio_cache = kmem_cache_create("zio_cache",
147 sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
148 zio_link_cache = kmem_cache_create("zio_link_cache",
149 sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
154 * For small buffers, we want a cache for each multiple of
155 * SPA_MINBLOCKSIZE. For larger buffers, we want a cache
156 * for each quarter-power of 2.
158 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
159 size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
162 size_t cflags = (size > zio_buf_debug_limit) ? KMC_NODEBUG : 0;
170 * If we are using watchpoints, put each buffer on its own page,
171 * to eliminate the performance overhead of trapping to the
172 * kernel when modifying a non-watched buffer that shares the
173 * page with a watched buffer.
175 if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
179 if (size <= 4 * SPA_MINBLOCKSIZE) {
180 align = SPA_MINBLOCKSIZE;
181 } else if (IS_P2ALIGNED(size, p2 >> 2)) {
182 align = MIN(p2 >> 2, PAGESIZE);
187 (void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
188 zio_buf_cache[c] = kmem_cache_create(name, size,
189 align, NULL, NULL, NULL, NULL, NULL, cflags);
192 * Since zio_data bufs do not appear in crash dumps, we
193 * pass KMC_NOTOUCH so that no allocator metadata is
194 * stored with the buffers.
196 (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
197 zio_data_buf_cache[c] = kmem_cache_create(name, size,
198 align, NULL, NULL, NULL, NULL, NULL,
199 cflags | KMC_NOTOUCH | KMC_NODEBUG);
204 ASSERT(zio_buf_cache[c] != NULL);
205 if (zio_buf_cache[c - 1] == NULL)
206 zio_buf_cache[c - 1] = zio_buf_cache[c];
208 ASSERT(zio_data_buf_cache[c] != NULL);
209 if (zio_data_buf_cache[c - 1] == NULL)
210 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
216 zio_trim_ksp = kstat_create("zfs", 0, "zio_trim", "misc",
218 sizeof(zio_trim_stats) / sizeof(kstat_named_t),
221 if (zio_trim_ksp != NULL) {
222 zio_trim_ksp->ks_data = &zio_trim_stats;
223 kstat_install(zio_trim_ksp);
231 kmem_cache_t *last_cache = NULL;
232 kmem_cache_t *last_data_cache = NULL;
234 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
235 if (zio_buf_cache[c] != last_cache) {
236 last_cache = zio_buf_cache[c];
237 kmem_cache_destroy(zio_buf_cache[c]);
239 zio_buf_cache[c] = NULL;
241 if (zio_data_buf_cache[c] != last_data_cache) {
242 last_data_cache = zio_data_buf_cache[c];
243 kmem_cache_destroy(zio_data_buf_cache[c]);
245 zio_data_buf_cache[c] = NULL;
248 kmem_cache_destroy(zio_link_cache);
249 kmem_cache_destroy(zio_cache);
253 if (zio_trim_ksp != NULL) {
254 kstat_delete(zio_trim_ksp);
260 * ==========================================================================
261 * Allocate and free I/O buffers
262 * ==========================================================================
266 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
267 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
268 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
269 * excess / transient data in-core during a crashdump.
272 zio_buf_alloc(size_t size)
274 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
275 int flags = zio_exclude_metadata ? KM_NODEBUG : 0;
277 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
280 return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
282 return (kmem_alloc(size, KM_SLEEP|flags));
286 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
287 * crashdump if the kernel panics. This exists so that we will limit the amount
288 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
289 * of kernel heap dumped to disk when the kernel panics)
292 zio_data_buf_alloc(size_t size)
294 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
296 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
299 return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
301 return (kmem_alloc(size, KM_SLEEP | KM_NODEBUG));
305 zio_buf_free(void *buf, size_t size)
307 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
309 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
312 kmem_cache_free(zio_buf_cache[c], buf);
314 kmem_free(buf, size);
318 zio_data_buf_free(void *buf, size_t size)
320 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
322 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
325 kmem_cache_free(zio_data_buf_cache[c], buf);
327 kmem_free(buf, size);
331 * ==========================================================================
332 * Push and pop I/O transform buffers
333 * ==========================================================================
336 zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize,
337 zio_transform_func_t *transform)
339 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
341 zt->zt_orig_data = zio->io_data;
342 zt->zt_orig_size = zio->io_size;
343 zt->zt_bufsize = bufsize;
344 zt->zt_transform = transform;
346 zt->zt_next = zio->io_transform_stack;
347 zio->io_transform_stack = zt;
354 zio_pop_transforms(zio_t *zio)
358 while ((zt = zio->io_transform_stack) != NULL) {
359 if (zt->zt_transform != NULL)
360 zt->zt_transform(zio,
361 zt->zt_orig_data, zt->zt_orig_size);
363 if (zt->zt_bufsize != 0)
364 zio_buf_free(zio->io_data, zt->zt_bufsize);
366 zio->io_data = zt->zt_orig_data;
367 zio->io_size = zt->zt_orig_size;
368 zio->io_transform_stack = zt->zt_next;
370 kmem_free(zt, sizeof (zio_transform_t));
375 * ==========================================================================
376 * I/O transform callbacks for subblocks and decompression
377 * ==========================================================================
380 zio_subblock(zio_t *zio, void *data, uint64_t size)
382 ASSERT(zio->io_size > size);
384 if (zio->io_type == ZIO_TYPE_READ)
385 bcopy(zio->io_data, data, size);
389 zio_decompress(zio_t *zio, void *data, uint64_t size)
391 if (zio->io_error == 0 &&
392 zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
393 zio->io_data, data, zio->io_size, size) != 0)
394 zio->io_error = SET_ERROR(EIO);
398 * ==========================================================================
399 * I/O parent/child relationships and pipeline interlocks
400 * ==========================================================================
403 * NOTE - Callers to zio_walk_parents() and zio_walk_children must
404 * continue calling these functions until they return NULL.
405 * Otherwise, the next caller will pick up the list walk in
406 * some indeterminate state. (Otherwise every caller would
407 * have to pass in a cookie to keep the state represented by
408 * io_walk_link, which gets annoying.)
411 zio_walk_parents(zio_t *cio)
413 zio_link_t *zl = cio->io_walk_link;
414 list_t *pl = &cio->io_parent_list;
416 zl = (zl == NULL) ? list_head(pl) : list_next(pl, zl);
417 cio->io_walk_link = zl;
422 ASSERT(zl->zl_child == cio);
423 return (zl->zl_parent);
427 zio_walk_children(zio_t *pio)
429 zio_link_t *zl = pio->io_walk_link;
430 list_t *cl = &pio->io_child_list;
432 zl = (zl == NULL) ? list_head(cl) : list_next(cl, zl);
433 pio->io_walk_link = zl;
438 ASSERT(zl->zl_parent == pio);
439 return (zl->zl_child);
443 zio_unique_parent(zio_t *cio)
445 zio_t *pio = zio_walk_parents(cio);
447 VERIFY(zio_walk_parents(cio) == NULL);
452 zio_add_child(zio_t *pio, zio_t *cio)
454 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
457 * Logical I/Os can have logical, gang, or vdev children.
458 * Gang I/Os can have gang or vdev children.
459 * Vdev I/Os can only have vdev children.
460 * The following ASSERT captures all of these constraints.
462 ASSERT(cio->io_child_type <= pio->io_child_type);
467 mutex_enter(&cio->io_lock);
468 mutex_enter(&pio->io_lock);
470 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
472 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
473 pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
475 list_insert_head(&pio->io_child_list, zl);
476 list_insert_head(&cio->io_parent_list, zl);
478 pio->io_child_count++;
479 cio->io_parent_count++;
481 mutex_exit(&pio->io_lock);
482 mutex_exit(&cio->io_lock);
486 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
488 ASSERT(zl->zl_parent == pio);
489 ASSERT(zl->zl_child == cio);
491 mutex_enter(&cio->io_lock);
492 mutex_enter(&pio->io_lock);
494 list_remove(&pio->io_child_list, zl);
495 list_remove(&cio->io_parent_list, zl);
497 pio->io_child_count--;
498 cio->io_parent_count--;
500 mutex_exit(&pio->io_lock);
501 mutex_exit(&cio->io_lock);
503 kmem_cache_free(zio_link_cache, zl);
507 zio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait)
509 uint64_t *countp = &zio->io_children[child][wait];
510 boolean_t waiting = B_FALSE;
512 mutex_enter(&zio->io_lock);
513 ASSERT(zio->io_stall == NULL);
516 zio->io_stall = countp;
519 mutex_exit(&zio->io_lock);
525 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
527 uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
528 int *errorp = &pio->io_child_error[zio->io_child_type];
530 mutex_enter(&pio->io_lock);
531 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
532 *errorp = zio_worst_error(*errorp, zio->io_error);
533 pio->io_reexecute |= zio->io_reexecute;
534 ASSERT3U(*countp, >, 0);
538 if (*countp == 0 && pio->io_stall == countp) {
539 pio->io_stall = NULL;
540 mutex_exit(&pio->io_lock);
543 mutex_exit(&pio->io_lock);
548 zio_inherit_child_errors(zio_t *zio, enum zio_child c)
550 if (zio->io_child_error[c] != 0 && zio->io_error == 0)
551 zio->io_error = zio->io_child_error[c];
555 * ==========================================================================
556 * Create the various types of I/O (read, write, free, etc)
557 * ==========================================================================
560 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
561 void *data, uint64_t size, zio_done_func_t *done, void *private,
562 zio_type_t type, zio_priority_t priority, enum zio_flag flags,
563 vdev_t *vd, uint64_t offset, const zbookmark_phys_t *zb,
564 enum zio_stage stage, enum zio_stage pipeline)
568 ASSERT3U(type == ZIO_TYPE_FREE || size, <=, SPA_MAXBLOCKSIZE);
569 ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
570 ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
572 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
573 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
574 ASSERT(vd || stage == ZIO_STAGE_OPEN);
576 zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
577 bzero(zio, sizeof (zio_t));
579 mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
580 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
582 list_create(&zio->io_parent_list, sizeof (zio_link_t),
583 offsetof(zio_link_t, zl_parent_node));
584 list_create(&zio->io_child_list, sizeof (zio_link_t),
585 offsetof(zio_link_t, zl_child_node));
588 zio->io_child_type = ZIO_CHILD_VDEV;
589 else if (flags & ZIO_FLAG_GANG_CHILD)
590 zio->io_child_type = ZIO_CHILD_GANG;
591 else if (flags & ZIO_FLAG_DDT_CHILD)
592 zio->io_child_type = ZIO_CHILD_DDT;
594 zio->io_child_type = ZIO_CHILD_LOGICAL;
597 zio->io_bp = (blkptr_t *)bp;
598 zio->io_bp_copy = *bp;
599 zio->io_bp_orig = *bp;
600 if (type != ZIO_TYPE_WRITE ||
601 zio->io_child_type == ZIO_CHILD_DDT)
602 zio->io_bp = &zio->io_bp_copy; /* so caller can free */
603 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
604 zio->io_logical = zio;
605 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
606 pipeline |= ZIO_GANG_STAGES;
612 zio->io_private = private;
614 zio->io_priority = priority;
616 zio->io_offset = offset;
617 zio->io_orig_data = zio->io_data = data;
618 zio->io_orig_size = zio->io_size = size;
619 zio->io_orig_flags = zio->io_flags = flags;
620 zio->io_orig_stage = zio->io_stage = stage;
621 zio->io_orig_pipeline = zio->io_pipeline = pipeline;
623 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
624 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
627 zio->io_bookmark = *zb;
630 if (zio->io_logical == NULL)
631 zio->io_logical = pio->io_logical;
632 if (zio->io_child_type == ZIO_CHILD_GANG)
633 zio->io_gang_leader = pio->io_gang_leader;
634 zio_add_child(pio, zio);
641 zio_destroy(zio_t *zio)
643 list_destroy(&zio->io_parent_list);
644 list_destroy(&zio->io_child_list);
645 mutex_destroy(&zio->io_lock);
646 cv_destroy(&zio->io_cv);
647 kmem_cache_free(zio_cache, zio);
651 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
652 void *private, enum zio_flag flags)
656 zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
657 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
658 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
664 zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
666 return (zio_null(NULL, spa, NULL, done, private, flags));
670 zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp)
672 if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) {
673 zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
674 bp, (longlong_t)BP_GET_TYPE(bp));
676 if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS ||
677 BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) {
678 zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
679 bp, (longlong_t)BP_GET_CHECKSUM(bp));
681 if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS ||
682 BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) {
683 zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
684 bp, (longlong_t)BP_GET_COMPRESS(bp));
686 if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) {
687 zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
688 bp, (longlong_t)BP_GET_LSIZE(bp));
690 if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) {
691 zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
692 bp, (longlong_t)BP_GET_PSIZE(bp));
695 if (BP_IS_EMBEDDED(bp)) {
696 if (BPE_GET_ETYPE(bp) > NUM_BP_EMBEDDED_TYPES) {
697 zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
698 bp, (longlong_t)BPE_GET_ETYPE(bp));
703 * Pool-specific checks.
705 * Note: it would be nice to verify that the blk_birth and
706 * BP_PHYSICAL_BIRTH() are not too large. However, spa_freeze()
707 * allows the birth time of log blocks (and dmu_sync()-ed blocks
708 * that are in the log) to be arbitrarily large.
710 for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
711 uint64_t vdevid = DVA_GET_VDEV(&bp->blk_dva[i]);
712 if (vdevid >= spa->spa_root_vdev->vdev_children) {
713 zfs_panic_recover("blkptr at %p DVA %u has invalid "
715 bp, i, (longlong_t)vdevid);
718 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
720 zfs_panic_recover("blkptr at %p DVA %u has invalid "
722 bp, i, (longlong_t)vdevid);
725 if (vd->vdev_ops == &vdev_hole_ops) {
726 zfs_panic_recover("blkptr at %p DVA %u has hole "
728 bp, i, (longlong_t)vdevid);
731 if (vd->vdev_ops == &vdev_missing_ops) {
733 * "missing" vdevs are valid during import, but we
734 * don't have their detailed info (e.g. asize), so
735 * we can't perform any more checks on them.
739 uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
740 uint64_t asize = DVA_GET_ASIZE(&bp->blk_dva[i]);
742 asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
743 if (offset + asize > vd->vdev_asize) {
744 zfs_panic_recover("blkptr at %p DVA %u has invalid "
746 bp, i, (longlong_t)offset);
752 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
753 void *data, uint64_t size, zio_done_func_t *done, void *private,
754 zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
758 zfs_blkptr_verify(spa, bp);
760 zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
761 data, size, done, private,
762 ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
763 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
764 ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
770 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
771 void *data, uint64_t size, const zio_prop_t *zp,
772 zio_done_func_t *ready, zio_done_func_t *physdone, zio_done_func_t *done,
774 zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
778 ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
779 zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
780 zp->zp_compress >= ZIO_COMPRESS_OFF &&
781 zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
782 DMU_OT_IS_VALID(zp->zp_type) &&
785 zp->zp_copies <= spa_max_replication(spa));
787 zio = zio_create(pio, spa, txg, bp, data, size, done, private,
788 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
789 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
790 ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
792 zio->io_ready = ready;
793 zio->io_physdone = physdone;
797 * Data can be NULL if we are going to call zio_write_override() to
798 * provide the already-allocated BP. But we may need the data to
799 * verify a dedup hit (if requested). In this case, don't try to
800 * dedup (just take the already-allocated BP verbatim).
802 if (data == NULL && zio->io_prop.zp_dedup_verify) {
803 zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
810 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, void *data,
811 uint64_t size, zio_done_func_t *done, void *private,
812 zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
816 zio = zio_create(pio, spa, txg, bp, data, size, done, private,
817 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
818 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
824 zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite)
826 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
827 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
828 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
829 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
832 * We must reset the io_prop to match the values that existed
833 * when the bp was first written by dmu_sync() keeping in mind
834 * that nopwrite and dedup are mutually exclusive.
836 zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
837 zio->io_prop.zp_nopwrite = nopwrite;
838 zio->io_prop.zp_copies = copies;
839 zio->io_bp_override = bp;
843 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
847 * The check for EMBEDDED is a performance optimization. We
848 * process the free here (by ignoring it) rather than
849 * putting it on the list and then processing it in zio_free_sync().
851 if (BP_IS_EMBEDDED(bp))
853 metaslab_check_free(spa, bp);
856 * Frees that are for the currently-syncing txg, are not going to be
857 * deferred, and which will not need to do a read (i.e. not GANG or
858 * DEDUP), can be processed immediately. Otherwise, put them on the
859 * in-memory list for later processing.
861 if (zfs_trim_enabled || BP_IS_GANG(bp) || BP_GET_DEDUP(bp) ||
862 txg != spa->spa_syncing_txg ||
863 spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) {
864 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
866 VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp,
867 BP_GET_PSIZE(bp), 0)));
872 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
873 uint64_t size, enum zio_flag flags)
876 enum zio_stage stage = ZIO_FREE_PIPELINE;
878 ASSERT(!BP_IS_HOLE(bp));
879 ASSERT(spa_syncing_txg(spa) == txg);
880 ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free);
882 if (BP_IS_EMBEDDED(bp))
883 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
885 metaslab_check_free(spa, bp);
888 if (zfs_trim_enabled)
889 stage |= ZIO_STAGE_ISSUE_ASYNC | ZIO_STAGE_VDEV_IO_START |
890 ZIO_STAGE_VDEV_IO_ASSESS;
892 * GANG and DEDUP blocks can induce a read (for the gang block header,
893 * or the DDT), so issue them asynchronously so that this thread is
896 else if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
897 stage |= ZIO_STAGE_ISSUE_ASYNC;
899 flags |= ZIO_FLAG_DONT_QUEUE;
901 zio = zio_create(pio, spa, txg, bp, NULL, size,
902 NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW, flags,
903 NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
909 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
910 zio_done_func_t *done, void *private, enum zio_flag flags)
914 dprintf_bp(bp, "claiming in txg %llu", txg);
916 if (BP_IS_EMBEDDED(bp))
917 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
920 * A claim is an allocation of a specific block. Claims are needed
921 * to support immediate writes in the intent log. The issue is that
922 * immediate writes contain committed data, but in a txg that was
923 * *not* committed. Upon opening the pool after an unclean shutdown,
924 * the intent log claims all blocks that contain immediate write data
925 * so that the SPA knows they're in use.
927 * All claims *must* be resolved in the first txg -- before the SPA
928 * starts allocating blocks -- so that nothing is allocated twice.
929 * If txg == 0 we just verify that the block is claimable.
931 ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
932 ASSERT(txg == spa_first_txg(spa) || txg == 0);
933 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(1M) */
935 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
936 done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, flags,
937 NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
943 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd, uint64_t offset,
944 uint64_t size, zio_done_func_t *done, void *private,
945 zio_priority_t priority, enum zio_flag flags)
950 if (vd->vdev_children == 0) {
951 zio = zio_create(pio, spa, 0, NULL, NULL, size, done, private,
952 ZIO_TYPE_IOCTL, priority, flags, vd, offset, NULL,
953 ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
957 zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
959 for (c = 0; c < vd->vdev_children; c++)
960 zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
961 offset, size, done, private, priority, flags));
968 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
969 void *data, int checksum, zio_done_func_t *done, void *private,
970 zio_priority_t priority, enum zio_flag flags, boolean_t labels)
974 ASSERT(vd->vdev_children == 0);
975 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
976 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
977 ASSERT3U(offset + size, <=, vd->vdev_psize);
979 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
980 ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd, offset,
981 NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
983 zio->io_prop.zp_checksum = checksum;
989 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
990 void *data, int checksum, zio_done_func_t *done, void *private,
991 zio_priority_t priority, enum zio_flag flags, boolean_t labels)
995 ASSERT(vd->vdev_children == 0);
996 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
997 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
998 ASSERT3U(offset + size, <=, vd->vdev_psize);
1000 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
1001 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd, offset,
1002 NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
1004 zio->io_prop.zp_checksum = checksum;
1006 if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
1008 * zec checksums are necessarily destructive -- they modify
1009 * the end of the write buffer to hold the verifier/checksum.
1010 * Therefore, we must make a local copy in case the data is
1011 * being written to multiple places in parallel.
1013 void *wbuf = zio_buf_alloc(size);
1014 bcopy(data, wbuf, size);
1015 zio_push_transform(zio, wbuf, size, size, NULL);
1022 * Create a child I/O to do some work for us.
1025 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1026 void *data, uint64_t size, int type, zio_priority_t priority,
1027 enum zio_flag flags, zio_done_func_t *done, void *private)
1029 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1032 ASSERT(vd->vdev_parent ==
1033 (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev));
1035 if (type == ZIO_TYPE_READ && bp != NULL) {
1037 * If we have the bp, then the child should perform the
1038 * checksum and the parent need not. This pushes error
1039 * detection as close to the leaves as possible and
1040 * eliminates redundant checksums in the interior nodes.
1042 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1043 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1046 /* Not all IO types require vdev io done stage e.g. free */
1047 if (!(pio->io_pipeline & ZIO_STAGE_VDEV_IO_DONE))
1048 pipeline &= ~ZIO_STAGE_VDEV_IO_DONE;
1050 if (vd->vdev_children == 0)
1051 offset += VDEV_LABEL_START_SIZE;
1053 flags |= ZIO_VDEV_CHILD_FLAGS(pio) | ZIO_FLAG_DONT_PROPAGATE;
1056 * If we've decided to do a repair, the write is not speculative --
1057 * even if the original read was.
1059 if (flags & ZIO_FLAG_IO_REPAIR)
1060 flags &= ~ZIO_FLAG_SPECULATIVE;
1062 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size,
1063 done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1064 ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
1066 zio->io_physdone = pio->io_physdone;
1067 if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
1068 zio->io_logical->io_phys_children++;
1074 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, void *data, uint64_t size,
1075 int type, zio_priority_t priority, enum zio_flag flags,
1076 zio_done_func_t *done, void *private)
1080 ASSERT(vd->vdev_ops->vdev_op_leaf);
1082 zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
1083 data, size, done, private, type, priority,
1084 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
1086 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
1092 zio_flush(zio_t *zio, vdev_t *vd)
1094 zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE, 0, 0,
1095 NULL, NULL, ZIO_PRIORITY_NOW,
1096 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
1100 zio_trim(zio_t *zio, spa_t *spa, vdev_t *vd, uint64_t offset, uint64_t size)
1103 ASSERT(vd->vdev_ops->vdev_op_leaf);
1105 return (zio_create(zio, spa, 0, NULL, NULL, size, NULL, NULL,
1106 ZIO_TYPE_FREE, ZIO_PRIORITY_TRIM, ZIO_FLAG_DONT_AGGREGATE |
1107 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY,
1108 vd, offset, NULL, ZIO_STAGE_OPEN, ZIO_FREE_PHYS_PIPELINE));
1112 zio_shrink(zio_t *zio, uint64_t size)
1114 ASSERT(zio->io_executor == NULL);
1115 ASSERT(zio->io_orig_size == zio->io_size);
1116 ASSERT(size <= zio->io_size);
1119 * We don't shrink for raidz because of problems with the
1120 * reconstruction when reading back less than the block size.
1121 * Note, BP_IS_RAIDZ() assumes no compression.
1123 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1124 if (!BP_IS_RAIDZ(zio->io_bp))
1125 zio->io_orig_size = zio->io_size = size;
1129 * ==========================================================================
1130 * Prepare to read and write logical blocks
1131 * ==========================================================================
1135 zio_read_bp_init(zio_t *zio)
1137 blkptr_t *bp = zio->io_bp;
1139 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1140 zio->io_child_type == ZIO_CHILD_LOGICAL &&
1141 !(zio->io_flags & ZIO_FLAG_RAW)) {
1143 BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1144 void *cbuf = zio_buf_alloc(psize);
1146 zio_push_transform(zio, cbuf, psize, psize, zio_decompress);
1149 if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1150 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1151 decode_embedded_bp_compressed(bp, zio->io_data);
1153 ASSERT(!BP_IS_EMBEDDED(bp));
1156 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
1157 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1159 if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
1160 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1162 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1163 zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1165 return (ZIO_PIPELINE_CONTINUE);
1169 zio_write_bp_init(zio_t *zio)
1171 spa_t *spa = zio->io_spa;
1172 zio_prop_t *zp = &zio->io_prop;
1173 enum zio_compress compress = zp->zp_compress;
1174 blkptr_t *bp = zio->io_bp;
1175 uint64_t lsize = zio->io_size;
1176 uint64_t psize = lsize;
1180 * If our children haven't all reached the ready stage,
1181 * wait for them and then repeat this pipeline stage.
1183 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
1184 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY))
1185 return (ZIO_PIPELINE_STOP);
1187 if (!IO_IS_ALLOCATING(zio))
1188 return (ZIO_PIPELINE_CONTINUE);
1190 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1192 if (zio->io_bp_override) {
1193 ASSERT(bp->blk_birth != zio->io_txg);
1194 ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
1196 *bp = *zio->io_bp_override;
1197 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1199 if (BP_IS_EMBEDDED(bp))
1200 return (ZIO_PIPELINE_CONTINUE);
1203 * If we've been overridden and nopwrite is set then
1204 * set the flag accordingly to indicate that a nopwrite
1205 * has already occurred.
1207 if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1208 ASSERT(!zp->zp_dedup);
1209 zio->io_flags |= ZIO_FLAG_NOPWRITE;
1210 return (ZIO_PIPELINE_CONTINUE);
1213 ASSERT(!zp->zp_nopwrite);
1215 if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1216 return (ZIO_PIPELINE_CONTINUE);
1218 ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
1219 ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
1221 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) {
1222 BP_SET_DEDUP(bp, 1);
1223 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1224 return (ZIO_PIPELINE_CONTINUE);
1226 zio->io_bp_override = NULL;
1230 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
1232 * We're rewriting an existing block, which means we're
1233 * working on behalf of spa_sync(). For spa_sync() to
1234 * converge, it must eventually be the case that we don't
1235 * have to allocate new blocks. But compression changes
1236 * the blocksize, which forces a reallocate, and makes
1237 * convergence take longer. Therefore, after the first
1238 * few passes, stop compressing to ensure convergence.
1240 pass = spa_sync_pass(spa);
1242 ASSERT(zio->io_txg == spa_syncing_txg(spa));
1243 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1244 ASSERT(!BP_GET_DEDUP(bp));
1246 if (pass >= zfs_sync_pass_dont_compress)
1247 compress = ZIO_COMPRESS_OFF;
1249 /* Make sure someone doesn't change their mind on overwrites */
1250 ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
1251 spa_max_replication(spa)) == BP_GET_NDVAS(bp));
1254 if (compress != ZIO_COMPRESS_OFF) {
1255 void *cbuf = zio_buf_alloc(lsize);
1256 psize = zio_compress_data(compress, zio->io_data, cbuf, lsize);
1257 if (psize == 0 || psize == lsize) {
1258 compress = ZIO_COMPRESS_OFF;
1259 zio_buf_free(cbuf, lsize);
1260 } else if (!zp->zp_dedup && psize <= BPE_PAYLOAD_SIZE &&
1261 zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1262 spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1263 encode_embedded_bp_compressed(bp,
1264 cbuf, compress, lsize, psize);
1265 BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1266 BP_SET_TYPE(bp, zio->io_prop.zp_type);
1267 BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1268 zio_buf_free(cbuf, lsize);
1269 bp->blk_birth = zio->io_txg;
1270 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1271 ASSERT(spa_feature_is_active(spa,
1272 SPA_FEATURE_EMBEDDED_DATA));
1273 return (ZIO_PIPELINE_CONTINUE);
1276 * Round up compressed size up to the ashift
1277 * of the smallest-ashift device, and zero the tail.
1278 * This ensures that the compressed size of the BP
1279 * (and thus compressratio property) are correct,
1280 * in that we charge for the padding used to fill out
1283 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
1284 size_t rounded = (size_t)P2ROUNDUP(psize,
1285 1ULL << spa->spa_min_ashift);
1286 if (rounded >= lsize) {
1287 compress = ZIO_COMPRESS_OFF;
1288 zio_buf_free(cbuf, lsize);
1291 bzero((char *)cbuf + psize, rounded - psize);
1293 zio_push_transform(zio, cbuf,
1294 psize, lsize, NULL);
1300 * The final pass of spa_sync() must be all rewrites, but the first
1301 * few passes offer a trade-off: allocating blocks defers convergence,
1302 * but newly allocated blocks are sequential, so they can be written
1303 * to disk faster. Therefore, we allow the first few passes of
1304 * spa_sync() to allocate new blocks, but force rewrites after that.
1305 * There should only be a handful of blocks after pass 1 in any case.
1307 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
1308 BP_GET_PSIZE(bp) == psize &&
1309 pass >= zfs_sync_pass_rewrite) {
1311 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1312 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1313 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1316 zio->io_pipeline = ZIO_WRITE_PIPELINE;
1320 if (zio->io_bp_orig.blk_birth != 0 &&
1321 spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
1322 BP_SET_LSIZE(bp, lsize);
1323 BP_SET_TYPE(bp, zp->zp_type);
1324 BP_SET_LEVEL(bp, zp->zp_level);
1325 BP_SET_BIRTH(bp, zio->io_txg, 0);
1327 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1329 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1330 BP_SET_LSIZE(bp, lsize);
1331 BP_SET_TYPE(bp, zp->zp_type);
1332 BP_SET_LEVEL(bp, zp->zp_level);
1333 BP_SET_PSIZE(bp, psize);
1334 BP_SET_COMPRESS(bp, compress);
1335 BP_SET_CHECKSUM(bp, zp->zp_checksum);
1336 BP_SET_DEDUP(bp, zp->zp_dedup);
1337 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1339 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1340 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1341 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1343 if (zp->zp_nopwrite) {
1344 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1345 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1346 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
1350 return (ZIO_PIPELINE_CONTINUE);
1354 zio_free_bp_init(zio_t *zio)
1356 blkptr_t *bp = zio->io_bp;
1358 if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1359 if (BP_GET_DEDUP(bp))
1360 zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1363 return (ZIO_PIPELINE_CONTINUE);
1367 * ==========================================================================
1368 * Execute the I/O pipeline
1369 * ==========================================================================
1373 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
1375 spa_t *spa = zio->io_spa;
1376 zio_type_t t = zio->io_type;
1377 int flags = (cutinline ? TQ_FRONT : 0);
1379 ASSERT(q == ZIO_TASKQ_ISSUE || q == ZIO_TASKQ_INTERRUPT);
1382 * If we're a config writer or a probe, the normal issue and
1383 * interrupt threads may all be blocked waiting for the config lock.
1384 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1386 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1390 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1392 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1396 * If this is a high priority I/O, then use the high priority taskq if
1399 if (zio->io_priority == ZIO_PRIORITY_NOW &&
1400 spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
1403 ASSERT3U(q, <, ZIO_TASKQ_TYPES);
1406 * NB: We are assuming that the zio can only be dispatched
1407 * to a single taskq at a time. It would be a grievous error
1408 * to dispatch the zio to another taskq at the same time.
1410 #if defined(illumos) || !defined(_KERNEL)
1411 ASSERT(zio->io_tqent.tqent_next == NULL);
1413 ASSERT(zio->io_tqent.tqent_task.ta_pending == 0);
1415 spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
1416 flags, &zio->io_tqent);
1420 zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
1422 kthread_t *executor = zio->io_executor;
1423 spa_t *spa = zio->io_spa;
1425 for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
1426 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1428 for (i = 0; i < tqs->stqs_count; i++) {
1429 if (taskq_member(tqs->stqs_taskq[i], executor))
1438 zio_issue_async(zio_t *zio)
1440 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1442 return (ZIO_PIPELINE_STOP);
1446 zio_interrupt(zio_t *zio)
1448 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
1452 zio_delay_interrupt(zio_t *zio)
1455 * The timeout_generic() function isn't defined in userspace, so
1456 * rather than trying to implement the function, the zio delay
1457 * functionality has been disabled for userspace builds.
1462 * If io_target_timestamp is zero, then no delay has been registered
1463 * for this IO, thus jump to the end of this function and "skip" the
1464 * delay; issuing it directly to the zio layer.
1466 if (zio->io_target_timestamp != 0) {
1467 hrtime_t now = gethrtime();
1469 if (now >= zio->io_target_timestamp) {
1471 * This IO has already taken longer than the target
1472 * delay to complete, so we don't want to delay it
1473 * any longer; we "miss" the delay and issue it
1474 * directly to the zio layer. This is likely due to
1475 * the target latency being set to a value less than
1476 * the underlying hardware can satisfy (e.g. delay
1477 * set to 1ms, but the disks take 10ms to complete an
1481 DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
1486 hrtime_t diff = zio->io_target_timestamp - now;
1488 DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
1489 hrtime_t, now, hrtime_t, diff);
1491 (void) timeout_generic(CALLOUT_NORMAL,
1492 (void (*)(void *))zio_interrupt, zio, diff, 1, 0);
1499 DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
1504 * Execute the I/O pipeline until one of the following occurs:
1506 * (1) the I/O completes
1507 * (2) the pipeline stalls waiting for dependent child I/Os
1508 * (3) the I/O issues, so we're waiting for an I/O completion interrupt
1509 * (4) the I/O is delegated by vdev-level caching or aggregation
1510 * (5) the I/O is deferred due to vdev-level queueing
1511 * (6) the I/O is handed off to another thread.
1513 * In all cases, the pipeline stops whenever there's no CPU work; it never
1514 * burns a thread in cv_wait().
1516 * There's no locking on io_stage because there's no legitimate way
1517 * for multiple threads to be attempting to process the same I/O.
1519 static zio_pipe_stage_t *zio_pipeline[];
1522 zio_execute(zio_t *zio)
1524 zio->io_executor = curthread;
1526 while (zio->io_stage < ZIO_STAGE_DONE) {
1527 enum zio_stage pipeline = zio->io_pipeline;
1528 enum zio_stage stage = zio->io_stage;
1531 ASSERT(!MUTEX_HELD(&zio->io_lock));
1532 ASSERT(ISP2(stage));
1533 ASSERT(zio->io_stall == NULL);
1537 } while ((stage & pipeline) == 0);
1539 ASSERT(stage <= ZIO_STAGE_DONE);
1542 * If we are in interrupt context and this pipeline stage
1543 * will grab a config lock that is held across I/O,
1544 * or may wait for an I/O that needs an interrupt thread
1545 * to complete, issue async to avoid deadlock.
1547 * For VDEV_IO_START, we cut in line so that the io will
1548 * be sent to disk promptly.
1550 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1551 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1552 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1553 zio_requeue_io_start_cut_in_line : B_FALSE;
1554 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1558 zio->io_stage = stage;
1559 rv = zio_pipeline[highbit64(stage) - 1](zio);
1561 if (rv == ZIO_PIPELINE_STOP)
1564 ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1569 * ==========================================================================
1570 * Initiate I/O, either sync or async
1571 * ==========================================================================
1574 zio_wait(zio_t *zio)
1578 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1579 ASSERT(zio->io_executor == NULL);
1581 zio->io_waiter = curthread;
1585 mutex_enter(&zio->io_lock);
1586 while (zio->io_executor != NULL)
1587 cv_wait(&zio->io_cv, &zio->io_lock);
1588 mutex_exit(&zio->io_lock);
1590 error = zio->io_error;
1597 zio_nowait(zio_t *zio)
1599 ASSERT(zio->io_executor == NULL);
1601 if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1602 zio_unique_parent(zio) == NULL) {
1604 * This is a logical async I/O with no parent to wait for it.
1605 * We add it to the spa_async_root_zio "Godfather" I/O which
1606 * will ensure they complete prior to unloading the pool.
1608 spa_t *spa = zio->io_spa;
1610 zio_add_child(spa->spa_async_zio_root[CPU_SEQID], zio);
1617 * ==========================================================================
1618 * Reexecute or suspend/resume failed I/O
1619 * ==========================================================================
1623 zio_reexecute(zio_t *pio)
1625 zio_t *cio, *cio_next;
1627 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1628 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1629 ASSERT(pio->io_gang_leader == NULL);
1630 ASSERT(pio->io_gang_tree == NULL);
1632 pio->io_flags = pio->io_orig_flags;
1633 pio->io_stage = pio->io_orig_stage;
1634 pio->io_pipeline = pio->io_orig_pipeline;
1635 pio->io_reexecute = 0;
1636 pio->io_flags |= ZIO_FLAG_REEXECUTED;
1638 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1639 pio->io_state[w] = 0;
1640 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
1641 pio->io_child_error[c] = 0;
1643 if (IO_IS_ALLOCATING(pio))
1644 BP_ZERO(pio->io_bp);
1647 * As we reexecute pio's children, new children could be created.
1648 * New children go to the head of pio's io_child_list, however,
1649 * so we will (correctly) not reexecute them. The key is that
1650 * the remainder of pio's io_child_list, from 'cio_next' onward,
1651 * cannot be affected by any side effects of reexecuting 'cio'.
1653 for (cio = zio_walk_children(pio); cio != NULL; cio = cio_next) {
1654 cio_next = zio_walk_children(pio);
1655 mutex_enter(&pio->io_lock);
1656 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1657 pio->io_children[cio->io_child_type][w]++;
1658 mutex_exit(&pio->io_lock);
1663 * Now that all children have been reexecuted, execute the parent.
1664 * We don't reexecute "The Godfather" I/O here as it's the
1665 * responsibility of the caller to wait on him.
1667 if (!(pio->io_flags & ZIO_FLAG_GODFATHER))
1672 zio_suspend(spa_t *spa, zio_t *zio)
1674 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1675 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1676 "failure and the failure mode property for this pool "
1677 "is set to panic.", spa_name(spa));
1679 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
1681 mutex_enter(&spa->spa_suspend_lock);
1683 if (spa->spa_suspend_zio_root == NULL)
1684 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
1685 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1686 ZIO_FLAG_GODFATHER);
1688 spa->spa_suspended = B_TRUE;
1691 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
1692 ASSERT(zio != spa->spa_suspend_zio_root);
1693 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1694 ASSERT(zio_unique_parent(zio) == NULL);
1695 ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1696 zio_add_child(spa->spa_suspend_zio_root, zio);
1699 mutex_exit(&spa->spa_suspend_lock);
1703 zio_resume(spa_t *spa)
1708 * Reexecute all previously suspended i/o.
1710 mutex_enter(&spa->spa_suspend_lock);
1711 spa->spa_suspended = B_FALSE;
1712 cv_broadcast(&spa->spa_suspend_cv);
1713 pio = spa->spa_suspend_zio_root;
1714 spa->spa_suspend_zio_root = NULL;
1715 mutex_exit(&spa->spa_suspend_lock);
1721 return (zio_wait(pio));
1725 zio_resume_wait(spa_t *spa)
1727 mutex_enter(&spa->spa_suspend_lock);
1728 while (spa_suspended(spa))
1729 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
1730 mutex_exit(&spa->spa_suspend_lock);
1734 * ==========================================================================
1737 * A gang block is a collection of small blocks that looks to the DMU
1738 * like one large block. When zio_dva_allocate() cannot find a block
1739 * of the requested size, due to either severe fragmentation or the pool
1740 * being nearly full, it calls zio_write_gang_block() to construct the
1741 * block from smaller fragments.
1743 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1744 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
1745 * an indirect block: it's an array of block pointers. It consumes
1746 * only one sector and hence is allocatable regardless of fragmentation.
1747 * The gang header's bps point to its gang members, which hold the data.
1749 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1750 * as the verifier to ensure uniqueness of the SHA256 checksum.
1751 * Critically, the gang block bp's blk_cksum is the checksum of the data,
1752 * not the gang header. This ensures that data block signatures (needed for
1753 * deduplication) are independent of how the block is physically stored.
1755 * Gang blocks can be nested: a gang member may itself be a gang block.
1756 * Thus every gang block is a tree in which root and all interior nodes are
1757 * gang headers, and the leaves are normal blocks that contain user data.
1758 * The root of the gang tree is called the gang leader.
1760 * To perform any operation (read, rewrite, free, claim) on a gang block,
1761 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1762 * in the io_gang_tree field of the original logical i/o by recursively
1763 * reading the gang leader and all gang headers below it. This yields
1764 * an in-core tree containing the contents of every gang header and the
1765 * bps for every constituent of the gang block.
1767 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1768 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
1769 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1770 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1771 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1772 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
1773 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1774 * of the gang header plus zio_checksum_compute() of the data to update the
1775 * gang header's blk_cksum as described above.
1777 * The two-phase assemble/issue model solves the problem of partial failure --
1778 * what if you'd freed part of a gang block but then couldn't read the
1779 * gang header for another part? Assembling the entire gang tree first
1780 * ensures that all the necessary gang header I/O has succeeded before
1781 * starting the actual work of free, claim, or write. Once the gang tree
1782 * is assembled, free and claim are in-memory operations that cannot fail.
1784 * In the event that a gang write fails, zio_dva_unallocate() walks the
1785 * gang tree to immediately free (i.e. insert back into the space map)
1786 * everything we've allocated. This ensures that we don't get ENOSPC
1787 * errors during repeated suspend/resume cycles due to a flaky device.
1789 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
1790 * the gang tree, we won't modify the block, so we can safely defer the free
1791 * (knowing that the block is still intact). If we *can* assemble the gang
1792 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1793 * each constituent bp and we can allocate a new block on the next sync pass.
1795 * In all cases, the gang tree allows complete recovery from partial failure.
1796 * ==========================================================================
1800 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1805 return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp),
1806 NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1807 &pio->io_bookmark));
1811 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1816 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1817 gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority,
1818 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1820 * As we rewrite each gang header, the pipeline will compute
1821 * a new gang block header checksum for it; but no one will
1822 * compute a new data checksum, so we do that here. The one
1823 * exception is the gang leader: the pipeline already computed
1824 * its data checksum because that stage precedes gang assembly.
1825 * (Presently, nothing actually uses interior data checksums;
1826 * this is just good hygiene.)
1828 if (gn != pio->io_gang_leader->io_gang_tree) {
1829 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
1830 data, BP_GET_PSIZE(bp));
1833 * If we are here to damage data for testing purposes,
1834 * leave the GBH alone so that we can detect the damage.
1836 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
1837 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
1839 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1840 data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority,
1841 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1849 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1851 return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
1852 BP_IS_GANG(bp) ? SPA_GANGBLOCKSIZE : BP_GET_PSIZE(bp),
1853 ZIO_GANG_CHILD_FLAGS(pio)));
1858 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1860 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
1861 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
1864 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
1873 static void zio_gang_tree_assemble_done(zio_t *zio);
1875 static zio_gang_node_t *
1876 zio_gang_node_alloc(zio_gang_node_t **gnpp)
1878 zio_gang_node_t *gn;
1880 ASSERT(*gnpp == NULL);
1882 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
1883 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
1890 zio_gang_node_free(zio_gang_node_t **gnpp)
1892 zio_gang_node_t *gn = *gnpp;
1894 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
1895 ASSERT(gn->gn_child[g] == NULL);
1897 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
1898 kmem_free(gn, sizeof (*gn));
1903 zio_gang_tree_free(zio_gang_node_t **gnpp)
1905 zio_gang_node_t *gn = *gnpp;
1910 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
1911 zio_gang_tree_free(&gn->gn_child[g]);
1913 zio_gang_node_free(gnpp);
1917 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
1919 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
1921 ASSERT(gio->io_gang_leader == gio);
1922 ASSERT(BP_IS_GANG(bp));
1924 zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh,
1925 SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn,
1926 gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
1930 zio_gang_tree_assemble_done(zio_t *zio)
1932 zio_t *gio = zio->io_gang_leader;
1933 zio_gang_node_t *gn = zio->io_private;
1934 blkptr_t *bp = zio->io_bp;
1936 ASSERT(gio == zio_unique_parent(zio));
1937 ASSERT(zio->io_child_count == 0);
1942 if (BP_SHOULD_BYTESWAP(bp))
1943 byteswap_uint64_array(zio->io_data, zio->io_size);
1945 ASSERT(zio->io_data == gn->gn_gbh);
1946 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
1947 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
1949 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1950 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1951 if (!BP_IS_GANG(gbp))
1953 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
1958 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data)
1960 zio_t *gio = pio->io_gang_leader;
1963 ASSERT(BP_IS_GANG(bp) == !!gn);
1964 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
1965 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
1968 * If you're a gang header, your data is in gn->gn_gbh.
1969 * If you're a gang member, your data is in 'data' and gn == NULL.
1971 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data);
1974 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
1976 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1977 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1978 if (BP_IS_HOLE(gbp))
1980 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data);
1981 data = (char *)data + BP_GET_PSIZE(gbp);
1985 if (gn == gio->io_gang_tree && gio->io_data != NULL)
1986 ASSERT3P((char *)gio->io_data + gio->io_size, ==, data);
1993 zio_gang_assemble(zio_t *zio)
1995 blkptr_t *bp = zio->io_bp;
1997 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
1998 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2000 zio->io_gang_leader = zio;
2002 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2004 return (ZIO_PIPELINE_CONTINUE);
2008 zio_gang_issue(zio_t *zio)
2010 blkptr_t *bp = zio->io_bp;
2012 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
2013 return (ZIO_PIPELINE_STOP);
2015 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2016 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2018 if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2019 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data);
2021 zio_gang_tree_free(&zio->io_gang_tree);
2023 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2025 return (ZIO_PIPELINE_CONTINUE);
2029 zio_write_gang_member_ready(zio_t *zio)
2031 zio_t *pio = zio_unique_parent(zio);
2032 zio_t *gio = zio->io_gang_leader;
2033 dva_t *cdva = zio->io_bp->blk_dva;
2034 dva_t *pdva = pio->io_bp->blk_dva;
2037 if (BP_IS_HOLE(zio->io_bp))
2040 ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
2042 ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
2043 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
2044 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
2045 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
2046 ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
2048 mutex_enter(&pio->io_lock);
2049 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
2050 ASSERT(DVA_GET_GANG(&pdva[d]));
2051 asize = DVA_GET_ASIZE(&pdva[d]);
2052 asize += DVA_GET_ASIZE(&cdva[d]);
2053 DVA_SET_ASIZE(&pdva[d], asize);
2055 mutex_exit(&pio->io_lock);
2059 zio_write_gang_block(zio_t *pio)
2061 spa_t *spa = pio->io_spa;
2062 blkptr_t *bp = pio->io_bp;
2063 zio_t *gio = pio->io_gang_leader;
2065 zio_gang_node_t *gn, **gnpp;
2066 zio_gbh_phys_t *gbh;
2067 uint64_t txg = pio->io_txg;
2068 uint64_t resid = pio->io_size;
2070 int copies = gio->io_prop.zp_copies;
2071 int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
2075 error = metaslab_alloc(spa, spa_normal_class(spa), SPA_GANGBLOCKSIZE,
2076 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp,
2077 METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER);
2079 pio->io_error = error;
2080 return (ZIO_PIPELINE_CONTINUE);
2084 gnpp = &gio->io_gang_tree;
2086 gnpp = pio->io_private;
2087 ASSERT(pio->io_ready == zio_write_gang_member_ready);
2090 gn = zio_gang_node_alloc(gnpp);
2092 bzero(gbh, SPA_GANGBLOCKSIZE);
2095 * Create the gang header.
2097 zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL,
2098 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2101 * Create and nowait the gang children.
2103 for (int g = 0; resid != 0; resid -= lsize, g++) {
2104 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
2106 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
2108 zp.zp_checksum = gio->io_prop.zp_checksum;
2109 zp.zp_compress = ZIO_COMPRESS_OFF;
2110 zp.zp_type = DMU_OT_NONE;
2112 zp.zp_copies = gio->io_prop.zp_copies;
2113 zp.zp_dedup = B_FALSE;
2114 zp.zp_dedup_verify = B_FALSE;
2115 zp.zp_nopwrite = B_FALSE;
2117 zio_nowait(zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
2118 (char *)pio->io_data + (pio->io_size - resid), lsize, &zp,
2119 zio_write_gang_member_ready, NULL, NULL, &gn->gn_child[g],
2120 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2121 &pio->io_bookmark));
2125 * Set pio's pipeline to just wait for zio to finish.
2127 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2131 return (ZIO_PIPELINE_CONTINUE);
2135 * The zio_nop_write stage in the pipeline determines if allocating a
2136 * new bp is necessary. The nopwrite feature can handle writes in
2137 * either syncing or open context (i.e. zil writes) and as a result is
2138 * mutually exclusive with dedup.
2140 * By leveraging a cryptographically secure checksum, such as SHA256, we
2141 * can compare the checksums of the new data and the old to determine if
2142 * allocating a new block is required. Note that our requirements for
2143 * cryptographic strength are fairly weak: there can't be any accidental
2144 * hash collisions, but we don't need to be secure against intentional
2145 * (malicious) collisions. To trigger a nopwrite, you have to be able
2146 * to write the file to begin with, and triggering an incorrect (hash
2147 * collision) nopwrite is no worse than simply writing to the file.
2148 * That said, there are no known attacks against the checksum algorithms
2149 * used for nopwrite, assuming that the salt and the checksums
2150 * themselves remain secret.
2153 zio_nop_write(zio_t *zio)
2155 blkptr_t *bp = zio->io_bp;
2156 blkptr_t *bp_orig = &zio->io_bp_orig;
2157 zio_prop_t *zp = &zio->io_prop;
2159 ASSERT(BP_GET_LEVEL(bp) == 0);
2160 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2161 ASSERT(zp->zp_nopwrite);
2162 ASSERT(!zp->zp_dedup);
2163 ASSERT(zio->io_bp_override == NULL);
2164 ASSERT(IO_IS_ALLOCATING(zio));
2167 * Check to see if the original bp and the new bp have matching
2168 * characteristics (i.e. same checksum, compression algorithms, etc).
2169 * If they don't then just continue with the pipeline which will
2170 * allocate a new bp.
2172 if (BP_IS_HOLE(bp_orig) ||
2173 !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
2174 ZCHECKSUM_FLAG_NOPWRITE) ||
2175 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
2176 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
2177 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
2178 zp->zp_copies != BP_GET_NDVAS(bp_orig))
2179 return (ZIO_PIPELINE_CONTINUE);
2182 * If the checksums match then reset the pipeline so that we
2183 * avoid allocating a new bp and issuing any I/O.
2185 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
2186 ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
2187 ZCHECKSUM_FLAG_NOPWRITE);
2188 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
2189 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
2190 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
2191 ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop,
2192 sizeof (uint64_t)) == 0);
2195 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2196 zio->io_flags |= ZIO_FLAG_NOPWRITE;
2199 return (ZIO_PIPELINE_CONTINUE);
2203 * ==========================================================================
2205 * ==========================================================================
2208 zio_ddt_child_read_done(zio_t *zio)
2210 blkptr_t *bp = zio->io_bp;
2211 ddt_entry_t *dde = zio->io_private;
2213 zio_t *pio = zio_unique_parent(zio);
2215 mutex_enter(&pio->io_lock);
2216 ddp = ddt_phys_select(dde, bp);
2217 if (zio->io_error == 0)
2218 ddt_phys_clear(ddp); /* this ddp doesn't need repair */
2219 if (zio->io_error == 0 && dde->dde_repair_data == NULL)
2220 dde->dde_repair_data = zio->io_data;
2222 zio_buf_free(zio->io_data, zio->io_size);
2223 mutex_exit(&pio->io_lock);
2227 zio_ddt_read_start(zio_t *zio)
2229 blkptr_t *bp = zio->io_bp;
2231 ASSERT(BP_GET_DEDUP(bp));
2232 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2233 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2235 if (zio->io_child_error[ZIO_CHILD_DDT]) {
2236 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2237 ddt_entry_t *dde = ddt_repair_start(ddt, bp);
2238 ddt_phys_t *ddp = dde->dde_phys;
2239 ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
2242 ASSERT(zio->io_vsd == NULL);
2245 if (ddp_self == NULL)
2246 return (ZIO_PIPELINE_CONTINUE);
2248 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2249 if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
2251 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
2253 zio_nowait(zio_read(zio, zio->io_spa, &blk,
2254 zio_buf_alloc(zio->io_size), zio->io_size,
2255 zio_ddt_child_read_done, dde, zio->io_priority,
2256 ZIO_DDT_CHILD_FLAGS(zio) | ZIO_FLAG_DONT_PROPAGATE,
2257 &zio->io_bookmark));
2259 return (ZIO_PIPELINE_CONTINUE);
2262 zio_nowait(zio_read(zio, zio->io_spa, bp,
2263 zio->io_data, zio->io_size, NULL, NULL, zio->io_priority,
2264 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
2266 return (ZIO_PIPELINE_CONTINUE);
2270 zio_ddt_read_done(zio_t *zio)
2272 blkptr_t *bp = zio->io_bp;
2274 if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE))
2275 return (ZIO_PIPELINE_STOP);
2277 ASSERT(BP_GET_DEDUP(bp));
2278 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2279 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2281 if (zio->io_child_error[ZIO_CHILD_DDT]) {
2282 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2283 ddt_entry_t *dde = zio->io_vsd;
2285 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
2286 return (ZIO_PIPELINE_CONTINUE);
2289 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
2290 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2291 return (ZIO_PIPELINE_STOP);
2293 if (dde->dde_repair_data != NULL) {
2294 bcopy(dde->dde_repair_data, zio->io_data, zio->io_size);
2295 zio->io_child_error[ZIO_CHILD_DDT] = 0;
2297 ddt_repair_done(ddt, dde);
2301 ASSERT(zio->io_vsd == NULL);
2303 return (ZIO_PIPELINE_CONTINUE);
2307 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
2309 spa_t *spa = zio->io_spa;
2312 * Note: we compare the original data, not the transformed data,
2313 * because when zio->io_bp is an override bp, we will not have
2314 * pushed the I/O transforms. That's an important optimization
2315 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2317 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2318 zio_t *lio = dde->dde_lead_zio[p];
2321 return (lio->io_orig_size != zio->io_orig_size ||
2322 bcmp(zio->io_orig_data, lio->io_orig_data,
2323 zio->io_orig_size) != 0);
2327 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2328 ddt_phys_t *ddp = &dde->dde_phys[p];
2330 if (ddp->ddp_phys_birth != 0) {
2331 arc_buf_t *abuf = NULL;
2332 arc_flags_t aflags = ARC_FLAG_WAIT;
2333 blkptr_t blk = *zio->io_bp;
2336 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2340 error = arc_read(NULL, spa, &blk,
2341 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
2342 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2343 &aflags, &zio->io_bookmark);
2346 if (arc_buf_size(abuf) != zio->io_orig_size ||
2347 bcmp(abuf->b_data, zio->io_orig_data,
2348 zio->io_orig_size) != 0)
2349 error = SET_ERROR(EEXIST);
2350 VERIFY(arc_buf_remove_ref(abuf, &abuf));
2354 return (error != 0);
2362 zio_ddt_child_write_ready(zio_t *zio)
2364 int p = zio->io_prop.zp_copies;
2365 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2366 ddt_entry_t *dde = zio->io_private;
2367 ddt_phys_t *ddp = &dde->dde_phys[p];
2375 ASSERT(dde->dde_lead_zio[p] == zio);
2377 ddt_phys_fill(ddp, zio->io_bp);
2379 while ((pio = zio_walk_parents(zio)) != NULL)
2380 ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
2386 zio_ddt_child_write_done(zio_t *zio)
2388 int p = zio->io_prop.zp_copies;
2389 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2390 ddt_entry_t *dde = zio->io_private;
2391 ddt_phys_t *ddp = &dde->dde_phys[p];
2395 ASSERT(ddp->ddp_refcnt == 0);
2396 ASSERT(dde->dde_lead_zio[p] == zio);
2397 dde->dde_lead_zio[p] = NULL;
2399 if (zio->io_error == 0) {
2400 while (zio_walk_parents(zio) != NULL)
2401 ddt_phys_addref(ddp);
2403 ddt_phys_clear(ddp);
2410 zio_ddt_ditto_write_done(zio_t *zio)
2412 int p = DDT_PHYS_DITTO;
2413 zio_prop_t *zp = &zio->io_prop;
2414 blkptr_t *bp = zio->io_bp;
2415 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2416 ddt_entry_t *dde = zio->io_private;
2417 ddt_phys_t *ddp = &dde->dde_phys[p];
2418 ddt_key_t *ddk = &dde->dde_key;
2422 ASSERT(ddp->ddp_refcnt == 0);
2423 ASSERT(dde->dde_lead_zio[p] == zio);
2424 dde->dde_lead_zio[p] = NULL;
2426 if (zio->io_error == 0) {
2427 ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
2428 ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
2429 ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
2430 if (ddp->ddp_phys_birth != 0)
2431 ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
2432 ddt_phys_fill(ddp, bp);
2439 zio_ddt_write(zio_t *zio)
2441 spa_t *spa = zio->io_spa;
2442 blkptr_t *bp = zio->io_bp;
2443 uint64_t txg = zio->io_txg;
2444 zio_prop_t *zp = &zio->io_prop;
2445 int p = zp->zp_copies;
2449 ddt_t *ddt = ddt_select(spa, bp);
2453 ASSERT(BP_GET_DEDUP(bp));
2454 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
2455 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
2458 dde = ddt_lookup(ddt, bp, B_TRUE);
2459 ddp = &dde->dde_phys[p];
2461 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
2463 * If we're using a weak checksum, upgrade to a strong checksum
2464 * and try again. If we're already using a strong checksum,
2465 * we can't resolve it, so just convert to an ordinary write.
2466 * (And automatically e-mail a paper to Nature?)
2468 if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
2469 ZCHECKSUM_FLAG_DEDUP)) {
2470 zp->zp_checksum = spa_dedup_checksum(spa);
2471 zio_pop_transforms(zio);
2472 zio->io_stage = ZIO_STAGE_OPEN;
2475 zp->zp_dedup = B_FALSE;
2477 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2479 return (ZIO_PIPELINE_CONTINUE);
2482 ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
2483 ASSERT(ditto_copies < SPA_DVAS_PER_BP);
2485 if (ditto_copies > ddt_ditto_copies_present(dde) &&
2486 dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
2487 zio_prop_t czp = *zp;
2489 czp.zp_copies = ditto_copies;
2492 * If we arrived here with an override bp, we won't have run
2493 * the transform stack, so we won't have the data we need to
2494 * generate a child i/o. So, toss the override bp and restart.
2495 * This is safe, because using the override bp is just an
2496 * optimization; and it's rare, so the cost doesn't matter.
2498 if (zio->io_bp_override) {
2499 zio_pop_transforms(zio);
2500 zio->io_stage = ZIO_STAGE_OPEN;
2501 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2502 zio->io_bp_override = NULL;
2505 return (ZIO_PIPELINE_CONTINUE);
2508 dio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2509 zio->io_orig_size, &czp, NULL, NULL,
2510 zio_ddt_ditto_write_done, dde, zio->io_priority,
2511 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2513 zio_push_transform(dio, zio->io_data, zio->io_size, 0, NULL);
2514 dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
2517 if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
2518 if (ddp->ddp_phys_birth != 0)
2519 ddt_bp_fill(ddp, bp, txg);
2520 if (dde->dde_lead_zio[p] != NULL)
2521 zio_add_child(zio, dde->dde_lead_zio[p]);
2523 ddt_phys_addref(ddp);
2524 } else if (zio->io_bp_override) {
2525 ASSERT(bp->blk_birth == txg);
2526 ASSERT(BP_EQUAL(bp, zio->io_bp_override));
2527 ddt_phys_fill(ddp, bp);
2528 ddt_phys_addref(ddp);
2530 cio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2531 zio->io_orig_size, zp, zio_ddt_child_write_ready, NULL,
2532 zio_ddt_child_write_done, dde, zio->io_priority,
2533 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2535 zio_push_transform(cio, zio->io_data, zio->io_size, 0, NULL);
2536 dde->dde_lead_zio[p] = cio;
2546 return (ZIO_PIPELINE_CONTINUE);
2549 ddt_entry_t *freedde; /* for debugging */
2552 zio_ddt_free(zio_t *zio)
2554 spa_t *spa = zio->io_spa;
2555 blkptr_t *bp = zio->io_bp;
2556 ddt_t *ddt = ddt_select(spa, bp);
2560 ASSERT(BP_GET_DEDUP(bp));
2561 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2564 freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
2565 ddp = ddt_phys_select(dde, bp);
2566 ddt_phys_decref(ddp);
2569 return (ZIO_PIPELINE_CONTINUE);
2573 * ==========================================================================
2574 * Allocate and free blocks
2575 * ==========================================================================
2578 zio_dva_allocate(zio_t *zio)
2580 spa_t *spa = zio->io_spa;
2581 metaslab_class_t *mc = spa_normal_class(spa);
2582 blkptr_t *bp = zio->io_bp;
2586 if (zio->io_gang_leader == NULL) {
2587 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2588 zio->io_gang_leader = zio;
2591 ASSERT(BP_IS_HOLE(bp));
2592 ASSERT0(BP_GET_NDVAS(bp));
2593 ASSERT3U(zio->io_prop.zp_copies, >, 0);
2594 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
2595 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
2598 * The dump device does not support gang blocks so allocation on
2599 * behalf of the dump device (i.e. ZIO_FLAG_NODATA) must avoid
2600 * the "fast" gang feature.
2602 flags |= (zio->io_flags & ZIO_FLAG_NODATA) ? METASLAB_GANG_AVOID : 0;
2603 flags |= (zio->io_flags & ZIO_FLAG_GANG_CHILD) ?
2604 METASLAB_GANG_CHILD : 0;
2605 error = metaslab_alloc(spa, mc, zio->io_size, bp,
2606 zio->io_prop.zp_copies, zio->io_txg, NULL, flags);
2609 spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, "
2610 "size %llu, error %d", spa_name(spa), zio, zio->io_size,
2612 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
2613 return (zio_write_gang_block(zio));
2614 zio->io_error = error;
2617 return (ZIO_PIPELINE_CONTINUE);
2621 zio_dva_free(zio_t *zio)
2623 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
2625 return (ZIO_PIPELINE_CONTINUE);
2629 zio_dva_claim(zio_t *zio)
2633 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
2635 zio->io_error = error;
2637 return (ZIO_PIPELINE_CONTINUE);
2641 * Undo an allocation. This is used by zio_done() when an I/O fails
2642 * and we want to give back the block we just allocated.
2643 * This handles both normal blocks and gang blocks.
2646 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
2648 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
2649 ASSERT(zio->io_bp_override == NULL);
2651 if (!BP_IS_HOLE(bp))
2652 metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
2655 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2656 zio_dva_unallocate(zio, gn->gn_child[g],
2657 &gn->gn_gbh->zg_blkptr[g]);
2663 * Try to allocate an intent log block. Return 0 on success, errno on failure.
2666 zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, blkptr_t *old_bp,
2667 uint64_t size, boolean_t use_slog)
2671 ASSERT(txg > spa_syncing_txg(spa));
2674 * ZIL blocks are always contiguous (i.e. not gang blocks) so we
2675 * set the METASLAB_GANG_AVOID flag so that they don't "fast gang"
2676 * when allocating them.
2679 error = metaslab_alloc(spa, spa_log_class(spa), size,
2680 new_bp, 1, txg, old_bp,
2681 METASLAB_HINTBP_AVOID | METASLAB_GANG_AVOID);
2685 error = metaslab_alloc(spa, spa_normal_class(spa), size,
2686 new_bp, 1, txg, old_bp,
2687 METASLAB_HINTBP_AVOID);
2691 BP_SET_LSIZE(new_bp, size);
2692 BP_SET_PSIZE(new_bp, size);
2693 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
2694 BP_SET_CHECKSUM(new_bp,
2695 spa_version(spa) >= SPA_VERSION_SLIM_ZIL
2696 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
2697 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
2698 BP_SET_LEVEL(new_bp, 0);
2699 BP_SET_DEDUP(new_bp, 0);
2700 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
2707 * Free an intent log block.
2710 zio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp)
2712 ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG);
2713 ASSERT(!BP_IS_GANG(bp));
2715 zio_free(spa, txg, bp);
2719 * ==========================================================================
2720 * Read, write and delete to physical devices
2721 * ==========================================================================
2726 * Issue an I/O to the underlying vdev. Typically the issue pipeline
2727 * stops after this stage and will resume upon I/O completion.
2728 * However, there are instances where the vdev layer may need to
2729 * continue the pipeline when an I/O was not issued. Since the I/O
2730 * that was sent to the vdev layer might be different than the one
2731 * currently active in the pipeline (see vdev_queue_io()), we explicitly
2732 * force the underlying vdev layers to call either zio_execute() or
2733 * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
2736 zio_vdev_io_start(zio_t *zio)
2738 vdev_t *vd = zio->io_vd;
2740 spa_t *spa = zio->io_spa;
2743 ASSERT(zio->io_error == 0);
2744 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
2747 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
2748 spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
2751 * The mirror_ops handle multiple DVAs in a single BP.
2753 vdev_mirror_ops.vdev_op_io_start(zio);
2754 return (ZIO_PIPELINE_STOP);
2757 if (vd->vdev_ops->vdev_op_leaf && zio->io_type == ZIO_TYPE_FREE &&
2758 zio->io_priority == ZIO_PRIORITY_NOW) {
2759 trim_map_free(vd, zio->io_offset, zio->io_size, zio->io_txg);
2760 return (ZIO_PIPELINE_CONTINUE);
2764 * We keep track of time-sensitive I/Os so that the scan thread
2765 * can quickly react to certain workloads. In particular, we care
2766 * about non-scrubbing, top-level reads and writes with the following
2768 * - synchronous writes of user data to non-slog devices
2769 * - any reads of user data
2770 * When these conditions are met, adjust the timestamp of spa_last_io
2771 * which allows the scan thread to adjust its workload accordingly.
2773 if (!(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && zio->io_bp != NULL &&
2774 vd == vd->vdev_top && !vd->vdev_islog &&
2775 zio->io_bookmark.zb_objset != DMU_META_OBJSET &&
2776 zio->io_txg != spa_syncing_txg(spa)) {
2777 uint64_t old = spa->spa_last_io;
2778 uint64_t new = ddi_get_lbolt64();
2780 (void) atomic_cas_64(&spa->spa_last_io, old, new);
2783 align = 1ULL << vd->vdev_top->vdev_ashift;
2785 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
2786 P2PHASE(zio->io_size, align) != 0) {
2787 /* Transform logical writes to be a full physical block size. */
2788 uint64_t asize = P2ROUNDUP(zio->io_size, align);
2790 if (zio->io_type == ZIO_TYPE_READ ||
2791 zio->io_type == ZIO_TYPE_WRITE)
2792 abuf = zio_buf_alloc(asize);
2793 ASSERT(vd == vd->vdev_top);
2794 if (zio->io_type == ZIO_TYPE_WRITE) {
2795 bcopy(zio->io_data, abuf, zio->io_size);
2796 bzero(abuf + zio->io_size, asize - zio->io_size);
2798 zio_push_transform(zio, abuf, asize, abuf ? asize : 0,
2803 * If this is not a physical io, make sure that it is properly aligned
2804 * before proceeding.
2806 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
2807 ASSERT0(P2PHASE(zio->io_offset, align));
2808 ASSERT0(P2PHASE(zio->io_size, align));
2811 * For physical writes, we allow 512b aligned writes and assume
2812 * the device will perform a read-modify-write as necessary.
2814 ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
2815 ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
2818 VERIFY(zio->io_type == ZIO_TYPE_READ || spa_writeable(spa));
2821 * If this is a repair I/O, and there's no self-healing involved --
2822 * that is, we're just resilvering what we expect to resilver --
2823 * then don't do the I/O unless zio's txg is actually in vd's DTL.
2824 * This prevents spurious resilvering with nested replication.
2825 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
2826 * A is out of date, we'll read from C+D, then use the data to
2827 * resilver A+B -- but we don't actually want to resilver B, just A.
2828 * The top-level mirror has no way to know this, so instead we just
2829 * discard unnecessary repairs as we work our way down the vdev tree.
2830 * The same logic applies to any form of nested replication:
2831 * ditto + mirror, RAID-Z + replacing, etc. This covers them all.
2833 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
2834 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
2835 zio->io_txg != 0 && /* not a delegated i/o */
2836 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
2837 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
2838 zio_vdev_io_bypass(zio);
2839 return (ZIO_PIPELINE_CONTINUE);
2842 if (vd->vdev_ops->vdev_op_leaf) {
2843 switch (zio->io_type) {
2845 if (vdev_cache_read(zio))
2846 return (ZIO_PIPELINE_CONTINUE);
2848 case ZIO_TYPE_WRITE:
2850 if ((zio = vdev_queue_io(zio)) == NULL)
2851 return (ZIO_PIPELINE_STOP);
2853 if (!vdev_accessible(vd, zio)) {
2854 zio->io_error = SET_ERROR(ENXIO);
2856 return (ZIO_PIPELINE_STOP);
2861 * Note that we ignore repair writes for TRIM because they can
2862 * conflict with normal writes. This isn't an issue because, by
2863 * definition, we only repair blocks that aren't freed.
2865 if (zio->io_type == ZIO_TYPE_WRITE &&
2866 !(zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
2867 !trim_map_write_start(zio))
2868 return (ZIO_PIPELINE_STOP);
2871 vd->vdev_ops->vdev_op_io_start(zio);
2872 return (ZIO_PIPELINE_STOP);
2876 zio_vdev_io_done(zio_t *zio)
2878 vdev_t *vd = zio->io_vd;
2879 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
2880 boolean_t unexpected_error = B_FALSE;
2882 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
2883 return (ZIO_PIPELINE_STOP);
2885 ASSERT(zio->io_type == ZIO_TYPE_READ ||
2886 zio->io_type == ZIO_TYPE_WRITE || zio->io_type == ZIO_TYPE_FREE);
2888 if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
2889 (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE ||
2890 zio->io_type == ZIO_TYPE_FREE)) {
2892 if (zio->io_type == ZIO_TYPE_WRITE &&
2893 !(zio->io_flags & ZIO_FLAG_IO_REPAIR))
2894 trim_map_write_done(zio);
2896 vdev_queue_io_done(zio);
2898 if (zio->io_type == ZIO_TYPE_WRITE)
2899 vdev_cache_write(zio);
2901 if (zio_injection_enabled && zio->io_error == 0)
2902 zio->io_error = zio_handle_device_injection(vd,
2905 if (zio_injection_enabled && zio->io_error == 0)
2906 zio->io_error = zio_handle_label_injection(zio, EIO);
2908 if (zio->io_error) {
2909 if (zio->io_error == ENOTSUP &&
2910 zio->io_type == ZIO_TYPE_FREE) {
2911 /* Not all devices support TRIM. */
2912 } else if (!vdev_accessible(vd, zio)) {
2913 zio->io_error = SET_ERROR(ENXIO);
2915 unexpected_error = B_TRUE;
2920 ops->vdev_op_io_done(zio);
2922 if (unexpected_error)
2923 VERIFY(vdev_probe(vd, zio) == NULL);
2925 return (ZIO_PIPELINE_CONTINUE);
2929 * For non-raidz ZIOs, we can just copy aside the bad data read from the
2930 * disk, and use that to finish the checksum ereport later.
2933 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
2934 const void *good_buf)
2936 /* no processing needed */
2937 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
2942 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
2944 void *buf = zio_buf_alloc(zio->io_size);
2946 bcopy(zio->io_data, buf, zio->io_size);
2948 zcr->zcr_cbinfo = zio->io_size;
2949 zcr->zcr_cbdata = buf;
2950 zcr->zcr_finish = zio_vsd_default_cksum_finish;
2951 zcr->zcr_free = zio_buf_free;
2955 zio_vdev_io_assess(zio_t *zio)
2957 vdev_t *vd = zio->io_vd;
2959 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
2960 return (ZIO_PIPELINE_STOP);
2962 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
2963 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
2965 if (zio->io_vsd != NULL) {
2966 zio->io_vsd_ops->vsd_free(zio);
2970 if (zio_injection_enabled && zio->io_error == 0)
2971 zio->io_error = zio_handle_fault_injection(zio, EIO);
2973 if (zio->io_type == ZIO_TYPE_FREE &&
2974 zio->io_priority != ZIO_PRIORITY_NOW) {
2975 switch (zio->io_error) {
2977 ZIO_TRIM_STAT_INCR(bytes, zio->io_size);
2978 ZIO_TRIM_STAT_BUMP(success);
2981 ZIO_TRIM_STAT_BUMP(unsupported);
2984 ZIO_TRIM_STAT_BUMP(failed);
2990 * If the I/O failed, determine whether we should attempt to retry it.
2992 * On retry, we cut in line in the issue queue, since we don't want
2993 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
2995 if (zio->io_error && vd == NULL &&
2996 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
2997 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
2998 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */
3000 zio->io_flags |= ZIO_FLAG_IO_RETRY |
3001 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
3002 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
3003 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
3004 zio_requeue_io_start_cut_in_line);
3005 return (ZIO_PIPELINE_STOP);
3009 * If we got an error on a leaf device, convert it to ENXIO
3010 * if the device is not accessible at all.
3012 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3013 !vdev_accessible(vd, zio))
3014 zio->io_error = SET_ERROR(ENXIO);
3017 * If we can't write to an interior vdev (mirror or RAID-Z),
3018 * set vdev_cant_write so that we stop trying to allocate from it.
3020 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
3021 vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
3022 vd->vdev_cant_write = B_TRUE;
3026 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3028 if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3029 zio->io_physdone != NULL) {
3030 ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED));
3031 ASSERT(zio->io_child_type == ZIO_CHILD_VDEV);
3032 zio->io_physdone(zio->io_logical);
3035 return (ZIO_PIPELINE_CONTINUE);
3039 zio_vdev_io_reissue(zio_t *zio)
3041 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3042 ASSERT(zio->io_error == 0);
3044 zio->io_stage >>= 1;
3048 zio_vdev_io_redone(zio_t *zio)
3050 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
3052 zio->io_stage >>= 1;
3056 zio_vdev_io_bypass(zio_t *zio)
3058 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3059 ASSERT(zio->io_error == 0);
3061 zio->io_flags |= ZIO_FLAG_IO_BYPASS;
3062 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
3066 * ==========================================================================
3067 * Generate and verify checksums
3068 * ==========================================================================
3071 zio_checksum_generate(zio_t *zio)
3073 blkptr_t *bp = zio->io_bp;
3074 enum zio_checksum checksum;
3078 * This is zio_write_phys().
3079 * We're either generating a label checksum, or none at all.
3081 checksum = zio->io_prop.zp_checksum;
3083 if (checksum == ZIO_CHECKSUM_OFF)
3084 return (ZIO_PIPELINE_CONTINUE);
3086 ASSERT(checksum == ZIO_CHECKSUM_LABEL);
3088 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
3089 ASSERT(!IO_IS_ALLOCATING(zio));
3090 checksum = ZIO_CHECKSUM_GANG_HEADER;
3092 checksum = BP_GET_CHECKSUM(bp);
3096 zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size);
3098 return (ZIO_PIPELINE_CONTINUE);
3102 zio_checksum_verify(zio_t *zio)
3104 zio_bad_cksum_t info;
3105 blkptr_t *bp = zio->io_bp;
3108 ASSERT(zio->io_vd != NULL);
3112 * This is zio_read_phys().
3113 * We're either verifying a label checksum, or nothing at all.
3115 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
3116 return (ZIO_PIPELINE_CONTINUE);
3118 ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
3121 if ((error = zio_checksum_error(zio, &info)) != 0) {
3122 zio->io_error = error;
3123 if (error == ECKSUM &&
3124 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
3125 zfs_ereport_start_checksum(zio->io_spa,
3126 zio->io_vd, zio, zio->io_offset,
3127 zio->io_size, NULL, &info);
3131 return (ZIO_PIPELINE_CONTINUE);
3135 * Called by RAID-Z to ensure we don't compute the checksum twice.
3138 zio_checksum_verified(zio_t *zio)
3140 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
3144 * ==========================================================================
3145 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
3146 * An error of 0 indicates success. ENXIO indicates whole-device failure,
3147 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
3148 * indicate errors that are specific to one I/O, and most likely permanent.
3149 * Any other error is presumed to be worse because we weren't expecting it.
3150 * ==========================================================================
3153 zio_worst_error(int e1, int e2)
3155 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
3158 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
3159 if (e1 == zio_error_rank[r1])
3162 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
3163 if (e2 == zio_error_rank[r2])
3166 return (r1 > r2 ? e1 : e2);
3170 * ==========================================================================
3172 * ==========================================================================
3175 zio_ready(zio_t *zio)
3177 blkptr_t *bp = zio->io_bp;
3178 zio_t *pio, *pio_next;
3180 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
3181 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY))
3182 return (ZIO_PIPELINE_STOP);
3184 if (zio->io_ready) {
3185 ASSERT(IO_IS_ALLOCATING(zio));
3186 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
3187 (zio->io_flags & ZIO_FLAG_NOPWRITE));
3188 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
3193 if (bp != NULL && bp != &zio->io_bp_copy)
3194 zio->io_bp_copy = *bp;
3197 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3199 mutex_enter(&zio->io_lock);
3200 zio->io_state[ZIO_WAIT_READY] = 1;
3201 pio = zio_walk_parents(zio);
3202 mutex_exit(&zio->io_lock);
3205 * As we notify zio's parents, new parents could be added.
3206 * New parents go to the head of zio's io_parent_list, however,
3207 * so we will (correctly) not notify them. The remainder of zio's
3208 * io_parent_list, from 'pio_next' onward, cannot change because
3209 * all parents must wait for us to be done before they can be done.
3211 for (; pio != NULL; pio = pio_next) {
3212 pio_next = zio_walk_parents(zio);
3213 zio_notify_parent(pio, zio, ZIO_WAIT_READY);
3216 if (zio->io_flags & ZIO_FLAG_NODATA) {
3217 if (BP_IS_GANG(bp)) {
3218 zio->io_flags &= ~ZIO_FLAG_NODATA;
3220 ASSERT((uintptr_t)zio->io_data < SPA_MAXBLOCKSIZE);
3221 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
3225 if (zio_injection_enabled &&
3226 zio->io_spa->spa_syncing_txg == zio->io_txg)
3227 zio_handle_ignored_writes(zio);
3229 return (ZIO_PIPELINE_CONTINUE);
3233 zio_done(zio_t *zio)
3235 spa_t *spa = zio->io_spa;
3236 zio_t *lio = zio->io_logical;
3237 blkptr_t *bp = zio->io_bp;
3238 vdev_t *vd = zio->io_vd;
3239 uint64_t psize = zio->io_size;
3240 zio_t *pio, *pio_next;
3243 * If our children haven't all completed,
3244 * wait for them and then repeat this pipeline stage.
3246 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
3247 zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
3248 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) ||
3249 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
3250 return (ZIO_PIPELINE_STOP);
3252 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
3253 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
3254 ASSERT(zio->io_children[c][w] == 0);
3256 if (bp != NULL && !BP_IS_EMBEDDED(bp)) {
3257 ASSERT(bp->blk_pad[0] == 0);
3258 ASSERT(bp->blk_pad[1] == 0);
3259 ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
3260 (bp == zio_unique_parent(zio)->io_bp));
3261 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
3262 zio->io_bp_override == NULL &&
3263 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
3264 ASSERT(!BP_SHOULD_BYTESWAP(bp));
3265 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp));
3266 ASSERT(BP_COUNT_GANG(bp) == 0 ||
3267 (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
3269 if (zio->io_flags & ZIO_FLAG_NOPWRITE)
3270 VERIFY(BP_EQUAL(bp, &zio->io_bp_orig));
3274 * If there were child vdev/gang/ddt errors, they apply to us now.
3276 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
3277 zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
3278 zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
3281 * If the I/O on the transformed data was successful, generate any
3282 * checksum reports now while we still have the transformed data.
3284 if (zio->io_error == 0) {
3285 while (zio->io_cksum_report != NULL) {
3286 zio_cksum_report_t *zcr = zio->io_cksum_report;
3287 uint64_t align = zcr->zcr_align;
3288 uint64_t asize = P2ROUNDUP(psize, align);
3289 char *abuf = zio->io_data;
3291 if (asize != psize) {
3292 abuf = zio_buf_alloc(asize);
3293 bcopy(zio->io_data, abuf, psize);
3294 bzero(abuf + psize, asize - psize);
3297 zio->io_cksum_report = zcr->zcr_next;
3298 zcr->zcr_next = NULL;
3299 zcr->zcr_finish(zcr, abuf);
3300 zfs_ereport_free_checksum(zcr);
3303 zio_buf_free(abuf, asize);
3307 zio_pop_transforms(zio); /* note: may set zio->io_error */
3309 vdev_stat_update(zio, psize);
3311 if (zio->io_error) {
3313 * If this I/O is attached to a particular vdev,
3314 * generate an error message describing the I/O failure
3315 * at the block level. We ignore these errors if the
3316 * device is currently unavailable.
3318 if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
3319 zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0);
3321 if ((zio->io_error == EIO || !(zio->io_flags &
3322 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
3325 * For logical I/O requests, tell the SPA to log the
3326 * error and generate a logical data ereport.
3328 spa_log_error(spa, zio);
3329 zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio,
3334 if (zio->io_error && zio == lio) {
3336 * Determine whether zio should be reexecuted. This will
3337 * propagate all the way to the root via zio_notify_parent().
3339 ASSERT(vd == NULL && bp != NULL);
3340 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3342 if (IO_IS_ALLOCATING(zio) &&
3343 !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
3344 if (zio->io_error != ENOSPC)
3345 zio->io_reexecute |= ZIO_REEXECUTE_NOW;
3347 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3350 if ((zio->io_type == ZIO_TYPE_READ ||
3351 zio->io_type == ZIO_TYPE_FREE) &&
3352 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
3353 zio->io_error == ENXIO &&
3354 spa_load_state(spa) == SPA_LOAD_NONE &&
3355 spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
3356 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3358 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
3359 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3362 * Here is a possibly good place to attempt to do
3363 * either combinatorial reconstruction or error correction
3364 * based on checksums. It also might be a good place
3365 * to send out preliminary ereports before we suspend
3371 * If there were logical child errors, they apply to us now.
3372 * We defer this until now to avoid conflating logical child
3373 * errors with errors that happened to the zio itself when
3374 * updating vdev stats and reporting FMA events above.
3376 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
3378 if ((zio->io_error || zio->io_reexecute) &&
3379 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
3380 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
3381 zio_dva_unallocate(zio, zio->io_gang_tree, bp);
3383 zio_gang_tree_free(&zio->io_gang_tree);
3386 * Godfather I/Os should never suspend.
3388 if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
3389 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
3390 zio->io_reexecute = 0;
3392 if (zio->io_reexecute) {
3394 * This is a logical I/O that wants to reexecute.
3396 * Reexecute is top-down. When an i/o fails, if it's not
3397 * the root, it simply notifies its parent and sticks around.
3398 * The parent, seeing that it still has children in zio_done(),
3399 * does the same. This percolates all the way up to the root.
3400 * The root i/o will reexecute or suspend the entire tree.
3402 * This approach ensures that zio_reexecute() honors
3403 * all the original i/o dependency relationships, e.g.
3404 * parents not executing until children are ready.
3406 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3408 zio->io_gang_leader = NULL;
3410 mutex_enter(&zio->io_lock);
3411 zio->io_state[ZIO_WAIT_DONE] = 1;
3412 mutex_exit(&zio->io_lock);
3415 * "The Godfather" I/O monitors its children but is
3416 * not a true parent to them. It will track them through
3417 * the pipeline but severs its ties whenever they get into
3418 * trouble (e.g. suspended). This allows "The Godfather"
3419 * I/O to return status without blocking.
3421 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
3422 zio_link_t *zl = zio->io_walk_link;
3423 pio_next = zio_walk_parents(zio);
3425 if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
3426 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
3427 zio_remove_child(pio, zio, zl);
3428 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3432 if ((pio = zio_unique_parent(zio)) != NULL) {
3434 * We're not a root i/o, so there's nothing to do
3435 * but notify our parent. Don't propagate errors
3436 * upward since we haven't permanently failed yet.
3438 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
3439 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
3440 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3441 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
3443 * We'd fail again if we reexecuted now, so suspend
3444 * until conditions improve (e.g. device comes online).
3446 zio_suspend(spa, zio);
3449 * Reexecution is potentially a huge amount of work.
3450 * Hand it off to the otherwise-unused claim taskq.
3452 #if defined(illumos) || !defined(_KERNEL)
3453 ASSERT(zio->io_tqent.tqent_next == NULL);
3455 ASSERT(zio->io_tqent.tqent_task.ta_pending == 0);
3457 spa_taskq_dispatch_ent(spa, ZIO_TYPE_CLAIM,
3458 ZIO_TASKQ_ISSUE, (task_func_t *)zio_reexecute, zio,
3461 return (ZIO_PIPELINE_STOP);
3464 ASSERT(zio->io_child_count == 0);
3465 ASSERT(zio->io_reexecute == 0);
3466 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
3469 * Report any checksum errors, since the I/O is complete.
3471 while (zio->io_cksum_report != NULL) {
3472 zio_cksum_report_t *zcr = zio->io_cksum_report;
3473 zio->io_cksum_report = zcr->zcr_next;
3474 zcr->zcr_next = NULL;
3475 zcr->zcr_finish(zcr, NULL);
3476 zfs_ereport_free_checksum(zcr);
3480 * It is the responsibility of the done callback to ensure that this
3481 * particular zio is no longer discoverable for adoption, and as
3482 * such, cannot acquire any new parents.
3487 mutex_enter(&zio->io_lock);
3488 zio->io_state[ZIO_WAIT_DONE] = 1;
3489 mutex_exit(&zio->io_lock);
3491 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
3492 zio_link_t *zl = zio->io_walk_link;
3493 pio_next = zio_walk_parents(zio);
3494 zio_remove_child(pio, zio, zl);
3495 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3498 if (zio->io_waiter != NULL) {
3499 mutex_enter(&zio->io_lock);
3500 zio->io_executor = NULL;
3501 cv_broadcast(&zio->io_cv);
3502 mutex_exit(&zio->io_lock);
3507 return (ZIO_PIPELINE_STOP);
3511 * ==========================================================================
3512 * I/O pipeline definition
3513 * ==========================================================================
3515 static zio_pipe_stage_t *zio_pipeline[] = {
3521 zio_checksum_generate,
3536 zio_checksum_verify,
3544 * Compare two zbookmark_phys_t's to see which we would reach first in a
3545 * pre-order traversal of the object tree.
3547 * This is simple in every case aside from the meta-dnode object. For all other
3548 * objects, we traverse them in order (object 1 before object 2, and so on).
3549 * However, all of these objects are traversed while traversing object 0, since
3550 * the data it points to is the list of objects. Thus, we need to convert to a
3551 * canonical representation so we can compare meta-dnode bookmarks to
3552 * non-meta-dnode bookmarks.
3554 * We do this by calculating "equivalents" for each field of the zbookmark.
3555 * zbookmarks outside of the meta-dnode use their own object and level, and
3556 * calculate the level 0 equivalent (the first L0 blkid that is contained in the
3557 * blocks this bookmark refers to) by multiplying their blkid by their span
3558 * (the number of L0 blocks contained within one block at their level).
3559 * zbookmarks inside the meta-dnode calculate their object equivalent
3560 * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
3561 * level + 1<<31 (any value larger than a level could ever be) for their level.
3562 * This causes them to always compare before a bookmark in their object
3563 * equivalent, compare appropriately to bookmarks in other objects, and to
3564 * compare appropriately to other bookmarks in the meta-dnode.
3567 zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
3568 const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
3571 * These variables represent the "equivalent" values for the zbookmark,
3572 * after converting zbookmarks inside the meta dnode to their
3573 * normal-object equivalents.
3575 uint64_t zb1obj, zb2obj;
3576 uint64_t zb1L0, zb2L0;
3577 uint64_t zb1level, zb2level;
3579 if (zb1->zb_object == zb2->zb_object &&
3580 zb1->zb_level == zb2->zb_level &&
3581 zb1->zb_blkid == zb2->zb_blkid)
3585 * BP_SPANB calculates the span in blocks.
3587 zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
3588 zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
3590 if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
3591 zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
3593 zb1level = zb1->zb_level + COMPARE_META_LEVEL;
3595 zb1obj = zb1->zb_object;
3596 zb1level = zb1->zb_level;
3599 if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
3600 zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
3602 zb2level = zb2->zb_level + COMPARE_META_LEVEL;
3604 zb2obj = zb2->zb_object;
3605 zb2level = zb2->zb_level;
3608 /* Now that we have a canonical representation, do the comparison. */
3609 if (zb1obj != zb2obj)
3610 return (zb1obj < zb2obj ? -1 : 1);
3611 else if (zb1L0 != zb2L0)
3612 return (zb1L0 < zb2L0 ? -1 : 1);
3613 else if (zb1level != zb2level)
3614 return (zb1level > zb2level ? -1 : 1);
3616 * This can (theoretically) happen if the bookmarks have the same object
3617 * and level, but different blkids, if the block sizes are not the same.
3618 * There is presently no way to change the indirect block sizes
3624 * This function checks the following: given that last_block is the place that
3625 * our traversal stopped last time, does that guarantee that we've visited
3626 * every node under subtree_root? Therefore, we can't just use the raw output
3627 * of zbookmark_compare. We have to pass in a modified version of
3628 * subtree_root; by incrementing the block id, and then checking whether
3629 * last_block is before or equal to that, we can tell whether or not having
3630 * visited last_block implies that all of subtree_root's children have been
3634 zbookmark_subtree_completed(const dnode_phys_t *dnp,
3635 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
3637 zbookmark_phys_t mod_zb = *subtree_root;
3639 ASSERT(last_block->zb_level == 0);
3641 /* The objset_phys_t isn't before anything. */
3646 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
3647 * data block size in sectors, because that variable is only used if
3648 * the bookmark refers to a block in the meta-dnode. Since we don't
3649 * know without examining it what object it refers to, and there's no
3650 * harm in passing in this value in other cases, we always pass it in.
3652 * We pass in 0 for the indirect block size shift because zb2 must be
3653 * level 0. The indirect block size is only used to calculate the span
3654 * of the bookmark, but since the bookmark must be level 0, the span is
3655 * always 1, so the math works out.
3657 * If you make changes to how the zbookmark_compare code works, be sure
3658 * to make sure that this code still works afterwards.
3660 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
3661 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,