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, 2016 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>
44 #include <sys/metaslab_impl.h>
46 SYSCTL_DECL(_vfs_zfs);
47 SYSCTL_NODE(_vfs_zfs, OID_AUTO, zio, CTLFLAG_RW, 0, "ZFS ZIO");
48 #if defined(__amd64__)
49 static int zio_use_uma = 1;
51 static int zio_use_uma = 0;
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 SYSCTL_INT(_vfs_zfs_zio, OID_AUTO, exclude_metadata, CTLFLAG_RDTUN, &zio_exclude_metadata, 0,
57 "Exclude metadata buffers from dumps as well");
59 zio_trim_stats_t zio_trim_stats = {
60 { "bytes", KSTAT_DATA_UINT64,
61 "Number of bytes successfully TRIMmed" },
62 { "success", KSTAT_DATA_UINT64,
63 "Number of successful TRIM requests" },
64 { "unsupported", KSTAT_DATA_UINT64,
65 "Number of TRIM requests that failed because TRIM is not supported" },
66 { "failed", KSTAT_DATA_UINT64,
67 "Number of TRIM requests that failed for reasons other than not supported" },
70 static kstat_t *zio_trim_ksp;
73 * ==========================================================================
74 * I/O type descriptions
75 * ==========================================================================
77 const char *zio_type_name[ZIO_TYPES] = {
78 "zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
82 boolean_t zio_dva_throttle_enabled = B_TRUE;
83 SYSCTL_INT(_vfs_zfs_zio, OID_AUTO, dva_throttle_enabled, CTLFLAG_RDTUN,
84 &zio_dva_throttle_enabled, 0, "");
87 * ==========================================================================
89 * ==========================================================================
91 kmem_cache_t *zio_cache;
92 kmem_cache_t *zio_link_cache;
93 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
94 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
97 extern vmem_t *zio_alloc_arena;
100 #define ZIO_PIPELINE_CONTINUE 0x100
101 #define ZIO_PIPELINE_STOP 0x101
103 #define BP_SPANB(indblkshift, level) \
104 (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
105 #define COMPARE_META_LEVEL 0x80000000ul
107 * The following actions directly effect the spa's sync-to-convergence logic.
108 * The values below define the sync pass when we start performing the action.
109 * Care should be taken when changing these values as they directly impact
110 * spa_sync() performance. Tuning these values may introduce subtle performance
111 * pathologies and should only be done in the context of performance analysis.
112 * These tunables will eventually be removed and replaced with #defines once
113 * enough analysis has been done to determine optimal values.
115 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
116 * regular blocks are not deferred.
118 int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
119 SYSCTL_INT(_vfs_zfs, OID_AUTO, sync_pass_deferred_free, CTLFLAG_RDTUN,
120 &zfs_sync_pass_deferred_free, 0, "defer frees starting in this pass");
121 int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
122 SYSCTL_INT(_vfs_zfs, OID_AUTO, sync_pass_dont_compress, CTLFLAG_RDTUN,
123 &zfs_sync_pass_dont_compress, 0, "don't compress starting in this pass");
124 int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
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;
138 int zio_buf_debug_limit = 16384;
140 int zio_buf_debug_limit = 0;
144 static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
150 zio_cache = kmem_cache_create("zio_cache",
151 sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
152 zio_link_cache = kmem_cache_create("zio_link_cache",
153 sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
158 * For small buffers, we want a cache for each multiple of
159 * SPA_MINBLOCKSIZE. For larger buffers, we want a cache
160 * for each quarter-power of 2.
162 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
163 size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
166 int cflags = zio_exclude_metadata ? KMC_NODEBUG : 0;
174 * If we are using watchpoints, put each buffer on its own page,
175 * to eliminate the performance overhead of trapping to the
176 * kernel when modifying a non-watched buffer that shares the
177 * page with a watched buffer.
179 if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
183 if (size <= 4 * SPA_MINBLOCKSIZE) {
184 align = SPA_MINBLOCKSIZE;
185 } else if (IS_P2ALIGNED(size, p2 >> 2)) {
186 align = MIN(p2 >> 2, PAGESIZE);
191 (void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
192 zio_buf_cache[c] = kmem_cache_create(name, size,
193 align, NULL, NULL, NULL, NULL, NULL, cflags);
196 * Since zio_data bufs do not appear in crash dumps, we
197 * pass KMC_NOTOUCH so that no allocator metadata is
198 * stored with the buffers.
200 (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
201 zio_data_buf_cache[c] = kmem_cache_create(name, size,
202 align, NULL, NULL, NULL, NULL, NULL,
203 cflags | KMC_NOTOUCH | KMC_NODEBUG);
208 ASSERT(zio_buf_cache[c] != NULL);
209 if (zio_buf_cache[c - 1] == NULL)
210 zio_buf_cache[c - 1] = zio_buf_cache[c];
212 ASSERT(zio_data_buf_cache[c] != NULL);
213 if (zio_data_buf_cache[c - 1] == NULL)
214 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
220 zio_trim_ksp = kstat_create("zfs", 0, "zio_trim", "misc",
222 sizeof(zio_trim_stats) / sizeof(kstat_named_t),
225 if (zio_trim_ksp != NULL) {
226 zio_trim_ksp->ks_data = &zio_trim_stats;
227 kstat_install(zio_trim_ksp);
235 kmem_cache_t *last_cache = NULL;
236 kmem_cache_t *last_data_cache = NULL;
238 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
239 if (zio_buf_cache[c] != last_cache) {
240 last_cache = zio_buf_cache[c];
241 kmem_cache_destroy(zio_buf_cache[c]);
243 zio_buf_cache[c] = NULL;
245 if (zio_data_buf_cache[c] != last_data_cache) {
246 last_data_cache = zio_data_buf_cache[c];
247 kmem_cache_destroy(zio_data_buf_cache[c]);
249 zio_data_buf_cache[c] = NULL;
252 kmem_cache_destroy(zio_link_cache);
253 kmem_cache_destroy(zio_cache);
257 if (zio_trim_ksp != NULL) {
258 kstat_delete(zio_trim_ksp);
264 * ==========================================================================
265 * Allocate and free I/O buffers
266 * ==========================================================================
270 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
271 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
272 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
273 * excess / transient data in-core during a crashdump.
276 zio_buf_alloc_impl(size_t size, boolean_t canwait)
278 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
279 int flags = zio_exclude_metadata ? KM_NODEBUG : 0;
281 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
284 return (kmem_cache_alloc(zio_buf_cache[c],
285 canwait ? KM_PUSHPAGE : KM_NOSLEEP));
287 return (kmem_alloc(size,
288 (canwait ? KM_SLEEP : KM_NOSLEEP) | flags));
293 zio_buf_alloc(size_t size)
295 return (zio_buf_alloc_impl(size, B_TRUE));
299 zio_buf_alloc_nowait(size_t size)
301 return (zio_buf_alloc_impl(size, B_FALSE));
305 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
306 * crashdump if the kernel panics. This exists so that we will limit the amount
307 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
308 * of kernel heap dumped to disk when the kernel panics)
311 zio_data_buf_alloc(size_t size)
313 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
315 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
318 return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
320 return (kmem_alloc(size, KM_SLEEP | KM_NODEBUG));
324 zio_buf_free(void *buf, size_t size)
326 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
328 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
331 kmem_cache_free(zio_buf_cache[c], buf);
333 kmem_free(buf, size);
337 zio_data_buf_free(void *buf, size_t size)
339 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
341 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
344 kmem_cache_free(zio_data_buf_cache[c], buf);
346 kmem_free(buf, size);
350 * ==========================================================================
351 * Push and pop I/O transform buffers
352 * ==========================================================================
355 zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize,
356 zio_transform_func_t *transform)
358 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
360 zt->zt_orig_data = zio->io_data;
361 zt->zt_orig_size = zio->io_size;
362 zt->zt_bufsize = bufsize;
363 zt->zt_transform = transform;
365 zt->zt_next = zio->io_transform_stack;
366 zio->io_transform_stack = zt;
373 zio_pop_transforms(zio_t *zio)
377 while ((zt = zio->io_transform_stack) != NULL) {
378 if (zt->zt_transform != NULL)
379 zt->zt_transform(zio,
380 zt->zt_orig_data, zt->zt_orig_size);
382 if (zt->zt_bufsize != 0)
383 zio_buf_free(zio->io_data, zt->zt_bufsize);
385 zio->io_data = zt->zt_orig_data;
386 zio->io_size = zt->zt_orig_size;
387 zio->io_transform_stack = zt->zt_next;
389 kmem_free(zt, sizeof (zio_transform_t));
394 * ==========================================================================
395 * I/O transform callbacks for subblocks and decompression
396 * ==========================================================================
399 zio_subblock(zio_t *zio, void *data, uint64_t size)
401 ASSERT(zio->io_size > size);
403 if (zio->io_type == ZIO_TYPE_READ)
404 bcopy(zio->io_data, data, size);
408 zio_decompress(zio_t *zio, void *data, uint64_t size)
410 if (zio->io_error == 0 &&
411 zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
412 zio->io_data, data, zio->io_size, size) != 0)
413 zio->io_error = SET_ERROR(EIO);
417 * ==========================================================================
418 * I/O parent/child relationships and pipeline interlocks
419 * ==========================================================================
422 zio_walk_parents(zio_t *cio, zio_link_t **zl)
424 list_t *pl = &cio->io_parent_list;
426 *zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
430 ASSERT((*zl)->zl_child == cio);
431 return ((*zl)->zl_parent);
435 zio_walk_children(zio_t *pio, zio_link_t **zl)
437 list_t *cl = &pio->io_child_list;
439 *zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
443 ASSERT((*zl)->zl_parent == pio);
444 return ((*zl)->zl_child);
448 zio_unique_parent(zio_t *cio)
450 zio_link_t *zl = NULL;
451 zio_t *pio = zio_walk_parents(cio, &zl);
453 VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
458 zio_add_child(zio_t *pio, zio_t *cio)
460 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
463 * Logical I/Os can have logical, gang, or vdev children.
464 * Gang I/Os can have gang or vdev children.
465 * Vdev I/Os can only have vdev children.
466 * The following ASSERT captures all of these constraints.
468 ASSERT(cio->io_child_type <= pio->io_child_type);
473 mutex_enter(&cio->io_lock);
474 mutex_enter(&pio->io_lock);
476 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
478 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
479 pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
481 list_insert_head(&pio->io_child_list, zl);
482 list_insert_head(&cio->io_parent_list, zl);
484 pio->io_child_count++;
485 cio->io_parent_count++;
487 mutex_exit(&pio->io_lock);
488 mutex_exit(&cio->io_lock);
492 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
494 ASSERT(zl->zl_parent == pio);
495 ASSERT(zl->zl_child == cio);
497 mutex_enter(&cio->io_lock);
498 mutex_enter(&pio->io_lock);
500 list_remove(&pio->io_child_list, zl);
501 list_remove(&cio->io_parent_list, zl);
503 pio->io_child_count--;
504 cio->io_parent_count--;
506 mutex_exit(&pio->io_lock);
507 mutex_exit(&cio->io_lock);
509 kmem_cache_free(zio_link_cache, zl);
513 zio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait)
515 uint64_t *countp = &zio->io_children[child][wait];
516 boolean_t waiting = B_FALSE;
518 mutex_enter(&zio->io_lock);
519 ASSERT(zio->io_stall == NULL);
522 ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
523 zio->io_stall = countp;
526 mutex_exit(&zio->io_lock);
532 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
534 uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
535 int *errorp = &pio->io_child_error[zio->io_child_type];
537 mutex_enter(&pio->io_lock);
538 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
539 *errorp = zio_worst_error(*errorp, zio->io_error);
540 pio->io_reexecute |= zio->io_reexecute;
541 ASSERT3U(*countp, >, 0);
545 if (*countp == 0 && pio->io_stall == countp) {
546 zio_taskq_type_t type =
547 pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
549 pio->io_stall = NULL;
550 mutex_exit(&pio->io_lock);
552 * Dispatch the parent zio in its own taskq so that
553 * the child can continue to make progress. This also
554 * prevents overflowing the stack when we have deeply nested
555 * parent-child relationships.
557 zio_taskq_dispatch(pio, type, B_FALSE);
559 mutex_exit(&pio->io_lock);
564 zio_inherit_child_errors(zio_t *zio, enum zio_child c)
566 if (zio->io_child_error[c] != 0 && zio->io_error == 0)
567 zio->io_error = zio->io_child_error[c];
571 zio_timestamp_compare(const void *x1, const void *x2)
573 const zio_t *z1 = x1;
574 const zio_t *z2 = x2;
576 if (z1->io_queued_timestamp < z2->io_queued_timestamp)
578 if (z1->io_queued_timestamp > z2->io_queued_timestamp)
581 if (z1->io_bookmark.zb_objset < z2->io_bookmark.zb_objset)
583 if (z1->io_bookmark.zb_objset > z2->io_bookmark.zb_objset)
586 if (z1->io_bookmark.zb_object < z2->io_bookmark.zb_object)
588 if (z1->io_bookmark.zb_object > z2->io_bookmark.zb_object)
591 if (z1->io_bookmark.zb_level < z2->io_bookmark.zb_level)
593 if (z1->io_bookmark.zb_level > z2->io_bookmark.zb_level)
596 if (z1->io_bookmark.zb_blkid < z2->io_bookmark.zb_blkid)
598 if (z1->io_bookmark.zb_blkid > z2->io_bookmark.zb_blkid)
610 * ==========================================================================
611 * Create the various types of I/O (read, write, free, etc)
612 * ==========================================================================
615 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
616 void *data, uint64_t size, zio_done_func_t *done, void *private,
617 zio_type_t type, zio_priority_t priority, enum zio_flag flags,
618 vdev_t *vd, uint64_t offset, const zbookmark_phys_t *zb,
619 enum zio_stage stage, enum zio_stage pipeline)
623 ASSERT3U(type == ZIO_TYPE_FREE || size, <=, SPA_MAXBLOCKSIZE);
624 ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
625 ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
627 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
628 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
629 ASSERT(vd || stage == ZIO_STAGE_OPEN);
631 zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
632 bzero(zio, sizeof (zio_t));
634 mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
635 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
637 list_create(&zio->io_parent_list, sizeof (zio_link_t),
638 offsetof(zio_link_t, zl_parent_node));
639 list_create(&zio->io_child_list, sizeof (zio_link_t),
640 offsetof(zio_link_t, zl_child_node));
641 metaslab_trace_init(&zio->io_alloc_list);
644 zio->io_child_type = ZIO_CHILD_VDEV;
645 else if (flags & ZIO_FLAG_GANG_CHILD)
646 zio->io_child_type = ZIO_CHILD_GANG;
647 else if (flags & ZIO_FLAG_DDT_CHILD)
648 zio->io_child_type = ZIO_CHILD_DDT;
650 zio->io_child_type = ZIO_CHILD_LOGICAL;
653 zio->io_bp = (blkptr_t *)bp;
654 zio->io_bp_copy = *bp;
655 zio->io_bp_orig = *bp;
656 if (type != ZIO_TYPE_WRITE ||
657 zio->io_child_type == ZIO_CHILD_DDT)
658 zio->io_bp = &zio->io_bp_copy; /* so caller can free */
659 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
660 zio->io_logical = zio;
661 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
662 pipeline |= ZIO_GANG_STAGES;
668 zio->io_private = private;
670 zio->io_priority = priority;
672 zio->io_offset = offset;
673 zio->io_orig_data = zio->io_data = data;
674 zio->io_orig_size = zio->io_size = size;
675 zio->io_orig_flags = zio->io_flags = flags;
676 zio->io_orig_stage = zio->io_stage = stage;
677 zio->io_orig_pipeline = zio->io_pipeline = pipeline;
678 zio->io_pipeline_trace = ZIO_STAGE_OPEN;
680 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
681 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
684 zio->io_bookmark = *zb;
687 if (zio->io_logical == NULL)
688 zio->io_logical = pio->io_logical;
689 if (zio->io_child_type == ZIO_CHILD_GANG)
690 zio->io_gang_leader = pio->io_gang_leader;
691 zio_add_child(pio, zio);
698 zio_destroy(zio_t *zio)
700 metaslab_trace_fini(&zio->io_alloc_list);
701 list_destroy(&zio->io_parent_list);
702 list_destroy(&zio->io_child_list);
703 mutex_destroy(&zio->io_lock);
704 cv_destroy(&zio->io_cv);
705 kmem_cache_free(zio_cache, zio);
709 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
710 void *private, enum zio_flag flags)
714 zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
715 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
716 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
722 zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
724 return (zio_null(NULL, spa, NULL, done, private, flags));
728 zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp)
730 if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) {
731 zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
732 bp, (longlong_t)BP_GET_TYPE(bp));
734 if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS ||
735 BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) {
736 zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
737 bp, (longlong_t)BP_GET_CHECKSUM(bp));
739 if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS ||
740 BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) {
741 zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
742 bp, (longlong_t)BP_GET_COMPRESS(bp));
744 if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) {
745 zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
746 bp, (longlong_t)BP_GET_LSIZE(bp));
748 if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) {
749 zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
750 bp, (longlong_t)BP_GET_PSIZE(bp));
753 if (BP_IS_EMBEDDED(bp)) {
754 if (BPE_GET_ETYPE(bp) > NUM_BP_EMBEDDED_TYPES) {
755 zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
756 bp, (longlong_t)BPE_GET_ETYPE(bp));
761 * Pool-specific checks.
763 * Note: it would be nice to verify that the blk_birth and
764 * BP_PHYSICAL_BIRTH() are not too large. However, spa_freeze()
765 * allows the birth time of log blocks (and dmu_sync()-ed blocks
766 * that are in the log) to be arbitrarily large.
768 for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
769 uint64_t vdevid = DVA_GET_VDEV(&bp->blk_dva[i]);
770 if (vdevid >= spa->spa_root_vdev->vdev_children) {
771 zfs_panic_recover("blkptr at %p DVA %u has invalid "
773 bp, i, (longlong_t)vdevid);
776 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
778 zfs_panic_recover("blkptr at %p DVA %u has invalid "
780 bp, i, (longlong_t)vdevid);
783 if (vd->vdev_ops == &vdev_hole_ops) {
784 zfs_panic_recover("blkptr at %p DVA %u has hole "
786 bp, i, (longlong_t)vdevid);
789 if (vd->vdev_ops == &vdev_missing_ops) {
791 * "missing" vdevs are valid during import, but we
792 * don't have their detailed info (e.g. asize), so
793 * we can't perform any more checks on them.
797 uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
798 uint64_t asize = DVA_GET_ASIZE(&bp->blk_dva[i]);
800 asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
801 if (offset + asize > vd->vdev_asize) {
802 zfs_panic_recover("blkptr at %p DVA %u has invalid "
804 bp, i, (longlong_t)offset);
810 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
811 void *data, uint64_t size, zio_done_func_t *done, void *private,
812 zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
816 zfs_blkptr_verify(spa, bp);
818 zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
819 data, size, done, private,
820 ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
821 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
822 ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
828 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
829 void *data, uint64_t size, const zio_prop_t *zp,
830 zio_done_func_t *ready, zio_done_func_t *children_ready,
831 zio_done_func_t *physdone, zio_done_func_t *done,
832 void *private, zio_priority_t priority, enum zio_flag flags,
833 const zbookmark_phys_t *zb)
837 ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
838 zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
839 zp->zp_compress >= ZIO_COMPRESS_OFF &&
840 zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
841 DMU_OT_IS_VALID(zp->zp_type) &&
844 zp->zp_copies <= spa_max_replication(spa));
846 zio = zio_create(pio, spa, txg, bp, data, size, done, private,
847 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
848 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
849 ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
851 zio->io_ready = ready;
852 zio->io_children_ready = children_ready;
853 zio->io_physdone = physdone;
857 * Data can be NULL if we are going to call zio_write_override() to
858 * provide the already-allocated BP. But we may need the data to
859 * verify a dedup hit (if requested). In this case, don't try to
860 * dedup (just take the already-allocated BP verbatim).
862 if (data == NULL && zio->io_prop.zp_dedup_verify) {
863 zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
870 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, void *data,
871 uint64_t size, zio_done_func_t *done, void *private,
872 zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
876 zio = zio_create(pio, spa, txg, bp, data, size, done, private,
877 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
878 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
884 zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite)
886 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
887 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
888 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
889 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
892 * We must reset the io_prop to match the values that existed
893 * when the bp was first written by dmu_sync() keeping in mind
894 * that nopwrite and dedup are mutually exclusive.
896 zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
897 zio->io_prop.zp_nopwrite = nopwrite;
898 zio->io_prop.zp_copies = copies;
899 zio->io_bp_override = bp;
903 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
907 * The check for EMBEDDED is a performance optimization. We
908 * process the free here (by ignoring it) rather than
909 * putting it on the list and then processing it in zio_free_sync().
911 if (BP_IS_EMBEDDED(bp))
913 metaslab_check_free(spa, bp);
916 * Frees that are for the currently-syncing txg, are not going to be
917 * deferred, and which will not need to do a read (i.e. not GANG or
918 * DEDUP), can be processed immediately. Otherwise, put them on the
919 * in-memory list for later processing.
921 if (zfs_trim_enabled || BP_IS_GANG(bp) || BP_GET_DEDUP(bp) ||
922 txg != spa->spa_syncing_txg ||
923 spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) {
924 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
926 VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp,
927 BP_GET_PSIZE(bp), 0)));
932 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
933 uint64_t size, enum zio_flag flags)
936 enum zio_stage stage = ZIO_FREE_PIPELINE;
938 ASSERT(!BP_IS_HOLE(bp));
939 ASSERT(spa_syncing_txg(spa) == txg);
940 ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free);
942 if (BP_IS_EMBEDDED(bp))
943 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
945 metaslab_check_free(spa, bp);
948 if (zfs_trim_enabled)
949 stage |= ZIO_STAGE_ISSUE_ASYNC | ZIO_STAGE_VDEV_IO_START |
950 ZIO_STAGE_VDEV_IO_ASSESS;
952 * GANG and DEDUP blocks can induce a read (for the gang block header,
953 * or the DDT), so issue them asynchronously so that this thread is
956 else if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
957 stage |= ZIO_STAGE_ISSUE_ASYNC;
959 flags |= ZIO_FLAG_DONT_QUEUE;
961 zio = zio_create(pio, spa, txg, bp, NULL, size,
962 NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW, flags,
963 NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
969 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
970 zio_done_func_t *done, void *private, enum zio_flag flags)
974 dprintf_bp(bp, "claiming in txg %llu", txg);
976 if (BP_IS_EMBEDDED(bp))
977 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
980 * A claim is an allocation of a specific block. Claims are needed
981 * to support immediate writes in the intent log. The issue is that
982 * immediate writes contain committed data, but in a txg that was
983 * *not* committed. Upon opening the pool after an unclean shutdown,
984 * the intent log claims all blocks that contain immediate write data
985 * so that the SPA knows they're in use.
987 * All claims *must* be resolved in the first txg -- before the SPA
988 * starts allocating blocks -- so that nothing is allocated twice.
989 * If txg == 0 we just verify that the block is claimable.
991 ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
992 ASSERT(txg == spa_first_txg(spa) || txg == 0);
993 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(1M) */
995 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
996 done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, flags,
997 NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
998 ASSERT0(zio->io_queued_timestamp);
1004 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd, uint64_t offset,
1005 uint64_t size, zio_done_func_t *done, void *private,
1006 zio_priority_t priority, enum zio_flag flags)
1011 if (vd->vdev_children == 0) {
1012 zio = zio_create(pio, spa, 0, NULL, NULL, size, done, private,
1013 ZIO_TYPE_IOCTL, priority, flags, vd, offset, NULL,
1014 ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
1018 zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
1020 for (c = 0; c < vd->vdev_children; c++)
1021 zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
1022 offset, size, done, private, priority, flags));
1029 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1030 void *data, int checksum, zio_done_func_t *done, void *private,
1031 zio_priority_t priority, enum zio_flag flags, boolean_t labels)
1035 ASSERT(vd->vdev_children == 0);
1036 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1037 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1038 ASSERT3U(offset + size, <=, vd->vdev_psize);
1040 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
1041 ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd, offset,
1042 NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
1044 zio->io_prop.zp_checksum = checksum;
1050 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1051 void *data, int checksum, zio_done_func_t *done, void *private,
1052 zio_priority_t priority, enum zio_flag flags, boolean_t labels)
1056 ASSERT(vd->vdev_children == 0);
1057 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1058 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1059 ASSERT3U(offset + size, <=, vd->vdev_psize);
1061 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
1062 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd, offset,
1063 NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
1065 zio->io_prop.zp_checksum = checksum;
1067 if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
1069 * zec checksums are necessarily destructive -- they modify
1070 * the end of the write buffer to hold the verifier/checksum.
1071 * Therefore, we must make a local copy in case the data is
1072 * being written to multiple places in parallel.
1074 void *wbuf = zio_buf_alloc(size);
1075 bcopy(data, wbuf, size);
1076 zio_push_transform(zio, wbuf, size, size, NULL);
1083 * Create a child I/O to do some work for us.
1086 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1087 void *data, uint64_t size, int type, zio_priority_t priority,
1088 enum zio_flag flags, zio_done_func_t *done, void *private)
1090 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1093 ASSERT(vd->vdev_parent ==
1094 (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev));
1096 if (type == ZIO_TYPE_READ && bp != NULL) {
1098 * If we have the bp, then the child should perform the
1099 * checksum and the parent need not. This pushes error
1100 * detection as close to the leaves as possible and
1101 * eliminates redundant checksums in the interior nodes.
1103 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1104 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1107 /* Not all IO types require vdev io done stage e.g. free */
1108 if (!(pio->io_pipeline & ZIO_STAGE_VDEV_IO_DONE))
1109 pipeline &= ~ZIO_STAGE_VDEV_IO_DONE;
1111 if (vd->vdev_children == 0)
1112 offset += VDEV_LABEL_START_SIZE;
1114 flags |= ZIO_VDEV_CHILD_FLAGS(pio) | ZIO_FLAG_DONT_PROPAGATE;
1117 * If we've decided to do a repair, the write is not speculative --
1118 * even if the original read was.
1120 if (flags & ZIO_FLAG_IO_REPAIR)
1121 flags &= ~ZIO_FLAG_SPECULATIVE;
1124 * If we're creating a child I/O that is not associated with a
1125 * top-level vdev, then the child zio is not an allocating I/O.
1126 * If this is a retried I/O then we ignore it since we will
1127 * have already processed the original allocating I/O.
1129 if (flags & ZIO_FLAG_IO_ALLOCATING &&
1130 (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
1131 metaslab_class_t *mc = spa_normal_class(pio->io_spa);
1133 ASSERT(mc->mc_alloc_throttle_enabled);
1134 ASSERT(type == ZIO_TYPE_WRITE);
1135 ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
1136 ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
1137 ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
1138 pio->io_child_type == ZIO_CHILD_GANG);
1140 flags &= ~ZIO_FLAG_IO_ALLOCATING;
1143 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size,
1144 done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1145 ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
1146 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1148 zio->io_physdone = pio->io_physdone;
1149 if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
1150 zio->io_logical->io_phys_children++;
1156 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, void *data, uint64_t size,
1157 int type, zio_priority_t priority, enum zio_flag flags,
1158 zio_done_func_t *done, void *private)
1162 ASSERT(vd->vdev_ops->vdev_op_leaf);
1164 zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
1165 data, size, done, private, type, priority,
1166 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
1168 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
1174 zio_flush(zio_t *zio, vdev_t *vd)
1176 zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE, 0, 0,
1177 NULL, NULL, ZIO_PRIORITY_NOW,
1178 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
1182 zio_trim(zio_t *zio, spa_t *spa, vdev_t *vd, uint64_t offset, uint64_t size)
1185 ASSERT(vd->vdev_ops->vdev_op_leaf);
1187 return (zio_create(zio, spa, 0, NULL, NULL, size, NULL, NULL,
1188 ZIO_TYPE_FREE, ZIO_PRIORITY_TRIM, ZIO_FLAG_DONT_AGGREGATE |
1189 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY,
1190 vd, offset, NULL, ZIO_STAGE_OPEN, ZIO_FREE_PHYS_PIPELINE));
1194 zio_shrink(zio_t *zio, uint64_t size)
1196 ASSERT(zio->io_executor == NULL);
1197 ASSERT(zio->io_orig_size == zio->io_size);
1198 ASSERT(size <= zio->io_size);
1201 * We don't shrink for raidz because of problems with the
1202 * reconstruction when reading back less than the block size.
1203 * Note, BP_IS_RAIDZ() assumes no compression.
1205 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1206 if (!BP_IS_RAIDZ(zio->io_bp))
1207 zio->io_orig_size = zio->io_size = size;
1211 * ==========================================================================
1212 * Prepare to read and write logical blocks
1213 * ==========================================================================
1217 zio_read_bp_init(zio_t *zio)
1219 blkptr_t *bp = zio->io_bp;
1221 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1222 zio->io_child_type == ZIO_CHILD_LOGICAL &&
1223 !(zio->io_flags & ZIO_FLAG_RAW)) {
1225 BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1226 void *cbuf = zio_buf_alloc(psize);
1228 zio_push_transform(zio, cbuf, psize, psize, zio_decompress);
1231 if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1232 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1233 decode_embedded_bp_compressed(bp, zio->io_data);
1235 ASSERT(!BP_IS_EMBEDDED(bp));
1238 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
1239 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1241 if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
1242 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1244 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1245 zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1247 return (ZIO_PIPELINE_CONTINUE);
1251 zio_write_bp_init(zio_t *zio)
1253 if (!IO_IS_ALLOCATING(zio))
1254 return (ZIO_PIPELINE_CONTINUE);
1256 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1258 if (zio->io_bp_override) {
1259 blkptr_t *bp = zio->io_bp;
1260 zio_prop_t *zp = &zio->io_prop;
1262 ASSERT(bp->blk_birth != zio->io_txg);
1263 ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
1265 *bp = *zio->io_bp_override;
1266 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1268 if (BP_IS_EMBEDDED(bp))
1269 return (ZIO_PIPELINE_CONTINUE);
1272 * If we've been overridden and nopwrite is set then
1273 * set the flag accordingly to indicate that a nopwrite
1274 * has already occurred.
1276 if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1277 ASSERT(!zp->zp_dedup);
1278 ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
1279 zio->io_flags |= ZIO_FLAG_NOPWRITE;
1280 return (ZIO_PIPELINE_CONTINUE);
1283 ASSERT(!zp->zp_nopwrite);
1285 if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1286 return (ZIO_PIPELINE_CONTINUE);
1288 ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
1289 ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
1291 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) {
1292 BP_SET_DEDUP(bp, 1);
1293 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1294 return (ZIO_PIPELINE_CONTINUE);
1298 * We were unable to handle this as an override bp, treat
1299 * it as a regular write I/O.
1301 zio->io_bp_override = NULL;
1302 *bp = zio->io_bp_orig;
1303 zio->io_pipeline = zio->io_orig_pipeline;
1306 return (ZIO_PIPELINE_CONTINUE);
1310 zio_write_compress(zio_t *zio)
1312 spa_t *spa = zio->io_spa;
1313 zio_prop_t *zp = &zio->io_prop;
1314 enum zio_compress compress = zp->zp_compress;
1315 blkptr_t *bp = zio->io_bp;
1316 uint64_t lsize = zio->io_size;
1317 uint64_t psize = lsize;
1321 * If our children haven't all reached the ready stage,
1322 * wait for them and then repeat this pipeline stage.
1324 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
1325 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY))
1326 return (ZIO_PIPELINE_STOP);
1328 if (!IO_IS_ALLOCATING(zio))
1329 return (ZIO_PIPELINE_CONTINUE);
1331 if (zio->io_children_ready != NULL) {
1333 * Now that all our children are ready, run the callback
1334 * associated with this zio in case it wants to modify the
1335 * data to be written.
1337 ASSERT3U(zp->zp_level, >, 0);
1338 zio->io_children_ready(zio);
1341 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1342 ASSERT(zio->io_bp_override == NULL);
1344 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
1346 * We're rewriting an existing block, which means we're
1347 * working on behalf of spa_sync(). For spa_sync() to
1348 * converge, it must eventually be the case that we don't
1349 * have to allocate new blocks. But compression changes
1350 * the blocksize, which forces a reallocate, and makes
1351 * convergence take longer. Therefore, after the first
1352 * few passes, stop compressing to ensure convergence.
1354 pass = spa_sync_pass(spa);
1356 ASSERT(zio->io_txg == spa_syncing_txg(spa));
1357 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1358 ASSERT(!BP_GET_DEDUP(bp));
1360 if (pass >= zfs_sync_pass_dont_compress)
1361 compress = ZIO_COMPRESS_OFF;
1363 /* Make sure someone doesn't change their mind on overwrites */
1364 ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
1365 spa_max_replication(spa)) == BP_GET_NDVAS(bp));
1368 if (compress != ZIO_COMPRESS_OFF) {
1369 void *cbuf = zio_buf_alloc(lsize);
1370 psize = zio_compress_data(compress, zio->io_data, cbuf, lsize);
1371 if (psize == 0 || psize == lsize) {
1372 compress = ZIO_COMPRESS_OFF;
1373 zio_buf_free(cbuf, lsize);
1374 } else if (!zp->zp_dedup && psize <= BPE_PAYLOAD_SIZE &&
1375 zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1376 spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1377 encode_embedded_bp_compressed(bp,
1378 cbuf, compress, lsize, psize);
1379 BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1380 BP_SET_TYPE(bp, zio->io_prop.zp_type);
1381 BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1382 zio_buf_free(cbuf, lsize);
1383 bp->blk_birth = zio->io_txg;
1384 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1385 ASSERT(spa_feature_is_active(spa,
1386 SPA_FEATURE_EMBEDDED_DATA));
1387 return (ZIO_PIPELINE_CONTINUE);
1390 * Round up compressed size up to the ashift
1391 * of the smallest-ashift device, and zero the tail.
1392 * This ensures that the compressed size of the BP
1393 * (and thus compressratio property) are correct,
1394 * in that we charge for the padding used to fill out
1397 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
1398 size_t rounded = (size_t)P2ROUNDUP(psize,
1399 1ULL << spa->spa_min_ashift);
1400 if (rounded >= lsize) {
1401 compress = ZIO_COMPRESS_OFF;
1402 zio_buf_free(cbuf, lsize);
1405 bzero((char *)cbuf + psize, rounded - psize);
1407 zio_push_transform(zio, cbuf,
1408 psize, lsize, NULL);
1413 * We were unable to handle this as an override bp, treat
1414 * it as a regular write I/O.
1416 zio->io_bp_override = NULL;
1417 *bp = zio->io_bp_orig;
1418 zio->io_pipeline = zio->io_orig_pipeline;
1422 * The final pass of spa_sync() must be all rewrites, but the first
1423 * few passes offer a trade-off: allocating blocks defers convergence,
1424 * but newly allocated blocks are sequential, so they can be written
1425 * to disk faster. Therefore, we allow the first few passes of
1426 * spa_sync() to allocate new blocks, but force rewrites after that.
1427 * There should only be a handful of blocks after pass 1 in any case.
1429 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
1430 BP_GET_PSIZE(bp) == psize &&
1431 pass >= zfs_sync_pass_rewrite) {
1433 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1434 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1435 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1438 zio->io_pipeline = ZIO_WRITE_PIPELINE;
1442 if (zio->io_bp_orig.blk_birth != 0 &&
1443 spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
1444 BP_SET_LSIZE(bp, lsize);
1445 BP_SET_TYPE(bp, zp->zp_type);
1446 BP_SET_LEVEL(bp, zp->zp_level);
1447 BP_SET_BIRTH(bp, zio->io_txg, 0);
1449 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1451 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1452 BP_SET_LSIZE(bp, lsize);
1453 BP_SET_TYPE(bp, zp->zp_type);
1454 BP_SET_LEVEL(bp, zp->zp_level);
1455 BP_SET_PSIZE(bp, psize);
1456 BP_SET_COMPRESS(bp, compress);
1457 BP_SET_CHECKSUM(bp, zp->zp_checksum);
1458 BP_SET_DEDUP(bp, zp->zp_dedup);
1459 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1461 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1462 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1463 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1465 if (zp->zp_nopwrite) {
1466 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1467 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1468 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
1471 return (ZIO_PIPELINE_CONTINUE);
1475 zio_free_bp_init(zio_t *zio)
1477 blkptr_t *bp = zio->io_bp;
1479 if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1480 if (BP_GET_DEDUP(bp))
1481 zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1484 return (ZIO_PIPELINE_CONTINUE);
1488 * ==========================================================================
1489 * Execute the I/O pipeline
1490 * ==========================================================================
1494 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
1496 spa_t *spa = zio->io_spa;
1497 zio_type_t t = zio->io_type;
1498 int flags = (cutinline ? TQ_FRONT : 0);
1500 ASSERT(q == ZIO_TASKQ_ISSUE || q == ZIO_TASKQ_INTERRUPT);
1503 * If we're a config writer or a probe, the normal issue and
1504 * interrupt threads may all be blocked waiting for the config lock.
1505 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1507 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1511 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1513 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1517 * If this is a high priority I/O, then use the high priority taskq if
1520 if (zio->io_priority == ZIO_PRIORITY_NOW &&
1521 spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
1524 ASSERT3U(q, <, ZIO_TASKQ_TYPES);
1527 * NB: We are assuming that the zio can only be dispatched
1528 * to a single taskq at a time. It would be a grievous error
1529 * to dispatch the zio to another taskq at the same time.
1531 #if defined(illumos) || !defined(_KERNEL)
1532 ASSERT(zio->io_tqent.tqent_next == NULL);
1534 ASSERT(zio->io_tqent.tqent_task.ta_pending == 0);
1536 spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
1537 flags, &zio->io_tqent);
1541 zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
1543 kthread_t *executor = zio->io_executor;
1544 spa_t *spa = zio->io_spa;
1546 for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
1547 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1549 for (i = 0; i < tqs->stqs_count; i++) {
1550 if (taskq_member(tqs->stqs_taskq[i], executor))
1559 zio_issue_async(zio_t *zio)
1561 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1563 return (ZIO_PIPELINE_STOP);
1567 zio_interrupt(zio_t *zio)
1569 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
1573 zio_delay_interrupt(zio_t *zio)
1576 * The timeout_generic() function isn't defined in userspace, so
1577 * rather than trying to implement the function, the zio delay
1578 * functionality has been disabled for userspace builds.
1583 * If io_target_timestamp is zero, then no delay has been registered
1584 * for this IO, thus jump to the end of this function and "skip" the
1585 * delay; issuing it directly to the zio layer.
1587 if (zio->io_target_timestamp != 0) {
1588 hrtime_t now = gethrtime();
1590 if (now >= zio->io_target_timestamp) {
1592 * This IO has already taken longer than the target
1593 * delay to complete, so we don't want to delay it
1594 * any longer; we "miss" the delay and issue it
1595 * directly to the zio layer. This is likely due to
1596 * the target latency being set to a value less than
1597 * the underlying hardware can satisfy (e.g. delay
1598 * set to 1ms, but the disks take 10ms to complete an
1602 DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
1607 hrtime_t diff = zio->io_target_timestamp - now;
1609 DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
1610 hrtime_t, now, hrtime_t, diff);
1612 (void) timeout_generic(CALLOUT_NORMAL,
1613 (void (*)(void *))zio_interrupt, zio, diff, 1, 0);
1620 DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
1625 * Execute the I/O pipeline until one of the following occurs:
1627 * (1) the I/O completes
1628 * (2) the pipeline stalls waiting for dependent child I/Os
1629 * (3) the I/O issues, so we're waiting for an I/O completion interrupt
1630 * (4) the I/O is delegated by vdev-level caching or aggregation
1631 * (5) the I/O is deferred due to vdev-level queueing
1632 * (6) the I/O is handed off to another thread.
1634 * In all cases, the pipeline stops whenever there's no CPU work; it never
1635 * burns a thread in cv_wait().
1637 * There's no locking on io_stage because there's no legitimate way
1638 * for multiple threads to be attempting to process the same I/O.
1640 static zio_pipe_stage_t *zio_pipeline[];
1643 zio_execute(zio_t *zio)
1645 zio->io_executor = curthread;
1647 ASSERT3U(zio->io_queued_timestamp, >, 0);
1649 while (zio->io_stage < ZIO_STAGE_DONE) {
1650 enum zio_stage pipeline = zio->io_pipeline;
1651 enum zio_stage stage = zio->io_stage;
1654 ASSERT(!MUTEX_HELD(&zio->io_lock));
1655 ASSERT(ISP2(stage));
1656 ASSERT(zio->io_stall == NULL);
1660 } while ((stage & pipeline) == 0);
1662 ASSERT(stage <= ZIO_STAGE_DONE);
1665 * If we are in interrupt context and this pipeline stage
1666 * will grab a config lock that is held across I/O,
1667 * or may wait for an I/O that needs an interrupt thread
1668 * to complete, issue async to avoid deadlock.
1670 * For VDEV_IO_START, we cut in line so that the io will
1671 * be sent to disk promptly.
1673 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1674 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1675 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1676 zio_requeue_io_start_cut_in_line : B_FALSE;
1677 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1681 zio->io_stage = stage;
1682 zio->io_pipeline_trace |= zio->io_stage;
1683 rv = zio_pipeline[highbit64(stage) - 1](zio);
1685 if (rv == ZIO_PIPELINE_STOP)
1688 ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1693 * ==========================================================================
1694 * Initiate I/O, either sync or async
1695 * ==========================================================================
1698 zio_wait(zio_t *zio)
1702 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1703 ASSERT(zio->io_executor == NULL);
1705 zio->io_waiter = curthread;
1706 ASSERT0(zio->io_queued_timestamp);
1707 zio->io_queued_timestamp = gethrtime();
1711 mutex_enter(&zio->io_lock);
1712 while (zio->io_executor != NULL)
1713 cv_wait(&zio->io_cv, &zio->io_lock);
1714 mutex_exit(&zio->io_lock);
1716 error = zio->io_error;
1723 zio_nowait(zio_t *zio)
1725 ASSERT(zio->io_executor == NULL);
1727 if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1728 zio_unique_parent(zio) == NULL) {
1730 * This is a logical async I/O with no parent to wait for it.
1731 * We add it to the spa_async_root_zio "Godfather" I/O which
1732 * will ensure they complete prior to unloading the pool.
1734 spa_t *spa = zio->io_spa;
1736 zio_add_child(spa->spa_async_zio_root[CPU_SEQID], zio);
1739 ASSERT0(zio->io_queued_timestamp);
1740 zio->io_queued_timestamp = gethrtime();
1745 * ==========================================================================
1746 * Reexecute or suspend/resume failed I/O
1747 * ==========================================================================
1751 zio_reexecute(zio_t *pio)
1753 zio_t *cio, *cio_next;
1755 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1756 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1757 ASSERT(pio->io_gang_leader == NULL);
1758 ASSERT(pio->io_gang_tree == NULL);
1760 pio->io_flags = pio->io_orig_flags;
1761 pio->io_stage = pio->io_orig_stage;
1762 pio->io_pipeline = pio->io_orig_pipeline;
1763 pio->io_reexecute = 0;
1764 pio->io_flags |= ZIO_FLAG_REEXECUTED;
1765 pio->io_pipeline_trace = 0;
1767 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1768 pio->io_state[w] = 0;
1769 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
1770 pio->io_child_error[c] = 0;
1772 if (IO_IS_ALLOCATING(pio))
1773 BP_ZERO(pio->io_bp);
1776 * As we reexecute pio's children, new children could be created.
1777 * New children go to the head of pio's io_child_list, however,
1778 * so we will (correctly) not reexecute them. The key is that
1779 * the remainder of pio's io_child_list, from 'cio_next' onward,
1780 * cannot be affected by any side effects of reexecuting 'cio'.
1782 zio_link_t *zl = NULL;
1783 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
1784 cio_next = zio_walk_children(pio, &zl);
1785 mutex_enter(&pio->io_lock);
1786 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1787 pio->io_children[cio->io_child_type][w]++;
1788 mutex_exit(&pio->io_lock);
1793 * Now that all children have been reexecuted, execute the parent.
1794 * We don't reexecute "The Godfather" I/O here as it's the
1795 * responsibility of the caller to wait on him.
1797 if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
1798 pio->io_queued_timestamp = gethrtime();
1804 zio_suspend(spa_t *spa, zio_t *zio)
1806 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1807 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1808 "failure and the failure mode property for this pool "
1809 "is set to panic.", spa_name(spa));
1811 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
1813 mutex_enter(&spa->spa_suspend_lock);
1815 if (spa->spa_suspend_zio_root == NULL)
1816 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
1817 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1818 ZIO_FLAG_GODFATHER);
1820 spa->spa_suspended = B_TRUE;
1823 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
1824 ASSERT(zio != spa->spa_suspend_zio_root);
1825 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1826 ASSERT(zio_unique_parent(zio) == NULL);
1827 ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1828 zio_add_child(spa->spa_suspend_zio_root, zio);
1831 mutex_exit(&spa->spa_suspend_lock);
1835 zio_resume(spa_t *spa)
1840 * Reexecute all previously suspended i/o.
1842 mutex_enter(&spa->spa_suspend_lock);
1843 spa->spa_suspended = B_FALSE;
1844 cv_broadcast(&spa->spa_suspend_cv);
1845 pio = spa->spa_suspend_zio_root;
1846 spa->spa_suspend_zio_root = NULL;
1847 mutex_exit(&spa->spa_suspend_lock);
1853 return (zio_wait(pio));
1857 zio_resume_wait(spa_t *spa)
1859 mutex_enter(&spa->spa_suspend_lock);
1860 while (spa_suspended(spa))
1861 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
1862 mutex_exit(&spa->spa_suspend_lock);
1866 * ==========================================================================
1869 * A gang block is a collection of small blocks that looks to the DMU
1870 * like one large block. When zio_dva_allocate() cannot find a block
1871 * of the requested size, due to either severe fragmentation or the pool
1872 * being nearly full, it calls zio_write_gang_block() to construct the
1873 * block from smaller fragments.
1875 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1876 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
1877 * an indirect block: it's an array of block pointers. It consumes
1878 * only one sector and hence is allocatable regardless of fragmentation.
1879 * The gang header's bps point to its gang members, which hold the data.
1881 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1882 * as the verifier to ensure uniqueness of the SHA256 checksum.
1883 * Critically, the gang block bp's blk_cksum is the checksum of the data,
1884 * not the gang header. This ensures that data block signatures (needed for
1885 * deduplication) are independent of how the block is physically stored.
1887 * Gang blocks can be nested: a gang member may itself be a gang block.
1888 * Thus every gang block is a tree in which root and all interior nodes are
1889 * gang headers, and the leaves are normal blocks that contain user data.
1890 * The root of the gang tree is called the gang leader.
1892 * To perform any operation (read, rewrite, free, claim) on a gang block,
1893 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1894 * in the io_gang_tree field of the original logical i/o by recursively
1895 * reading the gang leader and all gang headers below it. This yields
1896 * an in-core tree containing the contents of every gang header and the
1897 * bps for every constituent of the gang block.
1899 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1900 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
1901 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1902 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1903 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1904 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
1905 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1906 * of the gang header plus zio_checksum_compute() of the data to update the
1907 * gang header's blk_cksum as described above.
1909 * The two-phase assemble/issue model solves the problem of partial failure --
1910 * what if you'd freed part of a gang block but then couldn't read the
1911 * gang header for another part? Assembling the entire gang tree first
1912 * ensures that all the necessary gang header I/O has succeeded before
1913 * starting the actual work of free, claim, or write. Once the gang tree
1914 * is assembled, free and claim are in-memory operations that cannot fail.
1916 * In the event that a gang write fails, zio_dva_unallocate() walks the
1917 * gang tree to immediately free (i.e. insert back into the space map)
1918 * everything we've allocated. This ensures that we don't get ENOSPC
1919 * errors during repeated suspend/resume cycles due to a flaky device.
1921 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
1922 * the gang tree, we won't modify the block, so we can safely defer the free
1923 * (knowing that the block is still intact). If we *can* assemble the gang
1924 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1925 * each constituent bp and we can allocate a new block on the next sync pass.
1927 * In all cases, the gang tree allows complete recovery from partial failure.
1928 * ==========================================================================
1932 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1937 return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp),
1938 NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1939 &pio->io_bookmark));
1943 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1948 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1949 gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority,
1950 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1952 * As we rewrite each gang header, the pipeline will compute
1953 * a new gang block header checksum for it; but no one will
1954 * compute a new data checksum, so we do that here. The one
1955 * exception is the gang leader: the pipeline already computed
1956 * its data checksum because that stage precedes gang assembly.
1957 * (Presently, nothing actually uses interior data checksums;
1958 * this is just good hygiene.)
1960 if (gn != pio->io_gang_leader->io_gang_tree) {
1961 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
1962 data, BP_GET_PSIZE(bp));
1965 * If we are here to damage data for testing purposes,
1966 * leave the GBH alone so that we can detect the damage.
1968 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
1969 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
1971 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1972 data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority,
1973 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1981 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1983 return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
1984 BP_IS_GANG(bp) ? SPA_GANGBLOCKSIZE : BP_GET_PSIZE(bp),
1985 ZIO_GANG_CHILD_FLAGS(pio)));
1990 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1992 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
1993 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
1996 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
2005 static void zio_gang_tree_assemble_done(zio_t *zio);
2007 static zio_gang_node_t *
2008 zio_gang_node_alloc(zio_gang_node_t **gnpp)
2010 zio_gang_node_t *gn;
2012 ASSERT(*gnpp == NULL);
2014 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
2015 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
2022 zio_gang_node_free(zio_gang_node_t **gnpp)
2024 zio_gang_node_t *gn = *gnpp;
2026 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2027 ASSERT(gn->gn_child[g] == NULL);
2029 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2030 kmem_free(gn, sizeof (*gn));
2035 zio_gang_tree_free(zio_gang_node_t **gnpp)
2037 zio_gang_node_t *gn = *gnpp;
2042 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2043 zio_gang_tree_free(&gn->gn_child[g]);
2045 zio_gang_node_free(gnpp);
2049 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
2051 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
2053 ASSERT(gio->io_gang_leader == gio);
2054 ASSERT(BP_IS_GANG(bp));
2056 zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh,
2057 SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn,
2058 gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
2062 zio_gang_tree_assemble_done(zio_t *zio)
2064 zio_t *gio = zio->io_gang_leader;
2065 zio_gang_node_t *gn = zio->io_private;
2066 blkptr_t *bp = zio->io_bp;
2068 ASSERT(gio == zio_unique_parent(zio));
2069 ASSERT(zio->io_child_count == 0);
2074 if (BP_SHOULD_BYTESWAP(bp))
2075 byteswap_uint64_array(zio->io_data, zio->io_size);
2077 ASSERT(zio->io_data == gn->gn_gbh);
2078 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
2079 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2081 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2082 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2083 if (!BP_IS_GANG(gbp))
2085 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
2090 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data)
2092 zio_t *gio = pio->io_gang_leader;
2095 ASSERT(BP_IS_GANG(bp) == !!gn);
2096 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
2097 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
2100 * If you're a gang header, your data is in gn->gn_gbh.
2101 * If you're a gang member, your data is in 'data' and gn == NULL.
2103 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data);
2106 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2108 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2109 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2110 if (BP_IS_HOLE(gbp))
2112 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data);
2113 data = (char *)data + BP_GET_PSIZE(gbp);
2117 if (gn == gio->io_gang_tree && gio->io_data != NULL)
2118 ASSERT3P((char *)gio->io_data + gio->io_size, ==, data);
2125 zio_gang_assemble(zio_t *zio)
2127 blkptr_t *bp = zio->io_bp;
2129 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2130 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2132 zio->io_gang_leader = zio;
2134 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2136 return (ZIO_PIPELINE_CONTINUE);
2140 zio_gang_issue(zio_t *zio)
2142 blkptr_t *bp = zio->io_bp;
2144 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
2145 return (ZIO_PIPELINE_STOP);
2147 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2148 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2150 if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2151 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data);
2153 zio_gang_tree_free(&zio->io_gang_tree);
2155 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2157 return (ZIO_PIPELINE_CONTINUE);
2161 zio_write_gang_member_ready(zio_t *zio)
2163 zio_t *pio = zio_unique_parent(zio);
2164 zio_t *gio = zio->io_gang_leader;
2165 dva_t *cdva = zio->io_bp->blk_dva;
2166 dva_t *pdva = pio->io_bp->blk_dva;
2169 if (BP_IS_HOLE(zio->io_bp))
2172 ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
2174 ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
2175 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
2176 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
2177 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
2178 ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
2180 mutex_enter(&pio->io_lock);
2181 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
2182 ASSERT(DVA_GET_GANG(&pdva[d]));
2183 asize = DVA_GET_ASIZE(&pdva[d]);
2184 asize += DVA_GET_ASIZE(&cdva[d]);
2185 DVA_SET_ASIZE(&pdva[d], asize);
2187 mutex_exit(&pio->io_lock);
2191 zio_write_gang_block(zio_t *pio)
2193 spa_t *spa = pio->io_spa;
2194 metaslab_class_t *mc = spa_normal_class(spa);
2195 blkptr_t *bp = pio->io_bp;
2196 zio_t *gio = pio->io_gang_leader;
2198 zio_gang_node_t *gn, **gnpp;
2199 zio_gbh_phys_t *gbh;
2200 uint64_t txg = pio->io_txg;
2201 uint64_t resid = pio->io_size;
2203 int copies = gio->io_prop.zp_copies;
2204 int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
2208 int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
2209 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2210 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2211 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2213 flags |= METASLAB_ASYNC_ALLOC;
2214 VERIFY(refcount_held(&mc->mc_alloc_slots, pio));
2217 * The logical zio has already placed a reservation for
2218 * 'copies' allocation slots but gang blocks may require
2219 * additional copies. These additional copies
2220 * (i.e. gbh_copies - copies) are guaranteed to succeed
2221 * since metaslab_class_throttle_reserve() always allows
2222 * additional reservations for gang blocks.
2224 VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies,
2228 error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE,
2229 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags,
2230 &pio->io_alloc_list, pio);
2232 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2233 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2234 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2237 * If we failed to allocate the gang block header then
2238 * we remove any additional allocation reservations that
2239 * we placed here. The original reservation will
2240 * be removed when the logical I/O goes to the ready
2243 metaslab_class_throttle_unreserve(mc,
2244 gbh_copies - copies, pio);
2246 pio->io_error = error;
2247 return (ZIO_PIPELINE_CONTINUE);
2251 gnpp = &gio->io_gang_tree;
2253 gnpp = pio->io_private;
2254 ASSERT(pio->io_ready == zio_write_gang_member_ready);
2257 gn = zio_gang_node_alloc(gnpp);
2259 bzero(gbh, SPA_GANGBLOCKSIZE);
2262 * Create the gang header.
2264 zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL,
2265 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2268 * Create and nowait the gang children.
2270 for (int g = 0; resid != 0; resid -= lsize, g++) {
2271 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
2273 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
2275 zp.zp_checksum = gio->io_prop.zp_checksum;
2276 zp.zp_compress = ZIO_COMPRESS_OFF;
2277 zp.zp_type = DMU_OT_NONE;
2279 zp.zp_copies = gio->io_prop.zp_copies;
2280 zp.zp_dedup = B_FALSE;
2281 zp.zp_dedup_verify = B_FALSE;
2282 zp.zp_nopwrite = B_FALSE;
2284 zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
2285 (char *)pio->io_data + (pio->io_size - resid), lsize, &zp,
2286 zio_write_gang_member_ready, NULL, NULL, NULL,
2287 &gn->gn_child[g], pio->io_priority,
2288 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2290 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2291 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2292 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2295 * Gang children won't throttle but we should
2296 * account for their work, so reserve an allocation
2297 * slot for them here.
2299 VERIFY(metaslab_class_throttle_reserve(mc,
2300 zp.zp_copies, cio, flags));
2306 * Set pio's pipeline to just wait for zio to finish.
2308 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2312 return (ZIO_PIPELINE_CONTINUE);
2316 * The zio_nop_write stage in the pipeline determines if allocating a
2317 * new bp is necessary. The nopwrite feature can handle writes in
2318 * either syncing or open context (i.e. zil writes) and as a result is
2319 * mutually exclusive with dedup.
2321 * By leveraging a cryptographically secure checksum, such as SHA256, we
2322 * can compare the checksums of the new data and the old to determine if
2323 * allocating a new block is required. Note that our requirements for
2324 * cryptographic strength are fairly weak: there can't be any accidental
2325 * hash collisions, but we don't need to be secure against intentional
2326 * (malicious) collisions. To trigger a nopwrite, you have to be able
2327 * to write the file to begin with, and triggering an incorrect (hash
2328 * collision) nopwrite is no worse than simply writing to the file.
2329 * That said, there are no known attacks against the checksum algorithms
2330 * used for nopwrite, assuming that the salt and the checksums
2331 * themselves remain secret.
2334 zio_nop_write(zio_t *zio)
2336 blkptr_t *bp = zio->io_bp;
2337 blkptr_t *bp_orig = &zio->io_bp_orig;
2338 zio_prop_t *zp = &zio->io_prop;
2340 ASSERT(BP_GET_LEVEL(bp) == 0);
2341 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2342 ASSERT(zp->zp_nopwrite);
2343 ASSERT(!zp->zp_dedup);
2344 ASSERT(zio->io_bp_override == NULL);
2345 ASSERT(IO_IS_ALLOCATING(zio));
2348 * Check to see if the original bp and the new bp have matching
2349 * characteristics (i.e. same checksum, compression algorithms, etc).
2350 * If they don't then just continue with the pipeline which will
2351 * allocate a new bp.
2353 if (BP_IS_HOLE(bp_orig) ||
2354 !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
2355 ZCHECKSUM_FLAG_NOPWRITE) ||
2356 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
2357 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
2358 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
2359 zp->zp_copies != BP_GET_NDVAS(bp_orig))
2360 return (ZIO_PIPELINE_CONTINUE);
2363 * If the checksums match then reset the pipeline so that we
2364 * avoid allocating a new bp and issuing any I/O.
2366 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
2367 ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
2368 ZCHECKSUM_FLAG_NOPWRITE);
2369 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
2370 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
2371 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
2372 ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop,
2373 sizeof (uint64_t)) == 0);
2376 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2377 zio->io_flags |= ZIO_FLAG_NOPWRITE;
2380 return (ZIO_PIPELINE_CONTINUE);
2384 * ==========================================================================
2386 * ==========================================================================
2389 zio_ddt_child_read_done(zio_t *zio)
2391 blkptr_t *bp = zio->io_bp;
2392 ddt_entry_t *dde = zio->io_private;
2394 zio_t *pio = zio_unique_parent(zio);
2396 mutex_enter(&pio->io_lock);
2397 ddp = ddt_phys_select(dde, bp);
2398 if (zio->io_error == 0)
2399 ddt_phys_clear(ddp); /* this ddp doesn't need repair */
2400 if (zio->io_error == 0 && dde->dde_repair_data == NULL)
2401 dde->dde_repair_data = zio->io_data;
2403 zio_buf_free(zio->io_data, zio->io_size);
2404 mutex_exit(&pio->io_lock);
2408 zio_ddt_read_start(zio_t *zio)
2410 blkptr_t *bp = zio->io_bp;
2412 ASSERT(BP_GET_DEDUP(bp));
2413 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2414 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2416 if (zio->io_child_error[ZIO_CHILD_DDT]) {
2417 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2418 ddt_entry_t *dde = ddt_repair_start(ddt, bp);
2419 ddt_phys_t *ddp = dde->dde_phys;
2420 ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
2423 ASSERT(zio->io_vsd == NULL);
2426 if (ddp_self == NULL)
2427 return (ZIO_PIPELINE_CONTINUE);
2429 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2430 if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
2432 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
2434 zio_nowait(zio_read(zio, zio->io_spa, &blk,
2435 zio_buf_alloc(zio->io_size), zio->io_size,
2436 zio_ddt_child_read_done, dde, zio->io_priority,
2437 ZIO_DDT_CHILD_FLAGS(zio) | ZIO_FLAG_DONT_PROPAGATE,
2438 &zio->io_bookmark));
2440 return (ZIO_PIPELINE_CONTINUE);
2443 zio_nowait(zio_read(zio, zio->io_spa, bp,
2444 zio->io_data, zio->io_size, NULL, NULL, zio->io_priority,
2445 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
2447 return (ZIO_PIPELINE_CONTINUE);
2451 zio_ddt_read_done(zio_t *zio)
2453 blkptr_t *bp = zio->io_bp;
2455 if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE))
2456 return (ZIO_PIPELINE_STOP);
2458 ASSERT(BP_GET_DEDUP(bp));
2459 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2460 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2462 if (zio->io_child_error[ZIO_CHILD_DDT]) {
2463 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2464 ddt_entry_t *dde = zio->io_vsd;
2466 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
2467 return (ZIO_PIPELINE_CONTINUE);
2470 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
2471 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2472 return (ZIO_PIPELINE_STOP);
2474 if (dde->dde_repair_data != NULL) {
2475 bcopy(dde->dde_repair_data, zio->io_data, zio->io_size);
2476 zio->io_child_error[ZIO_CHILD_DDT] = 0;
2478 ddt_repair_done(ddt, dde);
2482 ASSERT(zio->io_vsd == NULL);
2484 return (ZIO_PIPELINE_CONTINUE);
2488 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
2490 spa_t *spa = zio->io_spa;
2493 * Note: we compare the original data, not the transformed data,
2494 * because when zio->io_bp is an override bp, we will not have
2495 * pushed the I/O transforms. That's an important optimization
2496 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2498 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2499 zio_t *lio = dde->dde_lead_zio[p];
2502 return (lio->io_orig_size != zio->io_orig_size ||
2503 bcmp(zio->io_orig_data, lio->io_orig_data,
2504 zio->io_orig_size) != 0);
2508 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2509 ddt_phys_t *ddp = &dde->dde_phys[p];
2511 if (ddp->ddp_phys_birth != 0) {
2512 arc_buf_t *abuf = NULL;
2513 arc_flags_t aflags = ARC_FLAG_WAIT;
2514 blkptr_t blk = *zio->io_bp;
2517 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2521 error = arc_read(NULL, spa, &blk,
2522 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
2523 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2524 &aflags, &zio->io_bookmark);
2527 if (arc_buf_size(abuf) != zio->io_orig_size ||
2528 bcmp(abuf->b_data, zio->io_orig_data,
2529 zio->io_orig_size) != 0)
2530 error = SET_ERROR(EEXIST);
2531 arc_buf_destroy(abuf, &abuf);
2535 return (error != 0);
2543 zio_ddt_child_write_ready(zio_t *zio)
2545 int p = zio->io_prop.zp_copies;
2546 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2547 ddt_entry_t *dde = zio->io_private;
2548 ddt_phys_t *ddp = &dde->dde_phys[p];
2556 ASSERT(dde->dde_lead_zio[p] == zio);
2558 ddt_phys_fill(ddp, zio->io_bp);
2560 zio_link_t *zl = NULL;
2561 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
2562 ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
2568 zio_ddt_child_write_done(zio_t *zio)
2570 int p = zio->io_prop.zp_copies;
2571 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2572 ddt_entry_t *dde = zio->io_private;
2573 ddt_phys_t *ddp = &dde->dde_phys[p];
2577 ASSERT(ddp->ddp_refcnt == 0);
2578 ASSERT(dde->dde_lead_zio[p] == zio);
2579 dde->dde_lead_zio[p] = NULL;
2581 if (zio->io_error == 0) {
2582 zio_link_t *zl = NULL;
2583 while (zio_walk_parents(zio, &zl) != NULL)
2584 ddt_phys_addref(ddp);
2586 ddt_phys_clear(ddp);
2593 zio_ddt_ditto_write_done(zio_t *zio)
2595 int p = DDT_PHYS_DITTO;
2596 zio_prop_t *zp = &zio->io_prop;
2597 blkptr_t *bp = zio->io_bp;
2598 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2599 ddt_entry_t *dde = zio->io_private;
2600 ddt_phys_t *ddp = &dde->dde_phys[p];
2601 ddt_key_t *ddk = &dde->dde_key;
2605 ASSERT(ddp->ddp_refcnt == 0);
2606 ASSERT(dde->dde_lead_zio[p] == zio);
2607 dde->dde_lead_zio[p] = NULL;
2609 if (zio->io_error == 0) {
2610 ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
2611 ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
2612 ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
2613 if (ddp->ddp_phys_birth != 0)
2614 ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
2615 ddt_phys_fill(ddp, bp);
2622 zio_ddt_write(zio_t *zio)
2624 spa_t *spa = zio->io_spa;
2625 blkptr_t *bp = zio->io_bp;
2626 uint64_t txg = zio->io_txg;
2627 zio_prop_t *zp = &zio->io_prop;
2628 int p = zp->zp_copies;
2632 ddt_t *ddt = ddt_select(spa, bp);
2636 ASSERT(BP_GET_DEDUP(bp));
2637 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
2638 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
2641 dde = ddt_lookup(ddt, bp, B_TRUE);
2642 ddp = &dde->dde_phys[p];
2644 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
2646 * If we're using a weak checksum, upgrade to a strong checksum
2647 * and try again. If we're already using a strong checksum,
2648 * we can't resolve it, so just convert to an ordinary write.
2649 * (And automatically e-mail a paper to Nature?)
2651 if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
2652 ZCHECKSUM_FLAG_DEDUP)) {
2653 zp->zp_checksum = spa_dedup_checksum(spa);
2654 zio_pop_transforms(zio);
2655 zio->io_stage = ZIO_STAGE_OPEN;
2658 zp->zp_dedup = B_FALSE;
2660 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2662 return (ZIO_PIPELINE_CONTINUE);
2665 ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
2666 ASSERT(ditto_copies < SPA_DVAS_PER_BP);
2668 if (ditto_copies > ddt_ditto_copies_present(dde) &&
2669 dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
2670 zio_prop_t czp = *zp;
2672 czp.zp_copies = ditto_copies;
2675 * If we arrived here with an override bp, we won't have run
2676 * the transform stack, so we won't have the data we need to
2677 * generate a child i/o. So, toss the override bp and restart.
2678 * This is safe, because using the override bp is just an
2679 * optimization; and it's rare, so the cost doesn't matter.
2681 if (zio->io_bp_override) {
2682 zio_pop_transforms(zio);
2683 zio->io_stage = ZIO_STAGE_OPEN;
2684 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2685 zio->io_bp_override = NULL;
2688 return (ZIO_PIPELINE_CONTINUE);
2691 dio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2692 zio->io_orig_size, &czp, NULL, NULL,
2693 NULL, zio_ddt_ditto_write_done, dde, zio->io_priority,
2694 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2696 zio_push_transform(dio, zio->io_data, zio->io_size, 0, NULL);
2697 dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
2700 if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
2701 if (ddp->ddp_phys_birth != 0)
2702 ddt_bp_fill(ddp, bp, txg);
2703 if (dde->dde_lead_zio[p] != NULL)
2704 zio_add_child(zio, dde->dde_lead_zio[p]);
2706 ddt_phys_addref(ddp);
2707 } else if (zio->io_bp_override) {
2708 ASSERT(bp->blk_birth == txg);
2709 ASSERT(BP_EQUAL(bp, zio->io_bp_override));
2710 ddt_phys_fill(ddp, bp);
2711 ddt_phys_addref(ddp);
2713 cio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2714 zio->io_orig_size, zp,
2715 zio_ddt_child_write_ready, NULL, NULL,
2716 zio_ddt_child_write_done, dde, zio->io_priority,
2717 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2719 zio_push_transform(cio, zio->io_data, zio->io_size, 0, NULL);
2720 dde->dde_lead_zio[p] = cio;
2730 return (ZIO_PIPELINE_CONTINUE);
2733 ddt_entry_t *freedde; /* for debugging */
2736 zio_ddt_free(zio_t *zio)
2738 spa_t *spa = zio->io_spa;
2739 blkptr_t *bp = zio->io_bp;
2740 ddt_t *ddt = ddt_select(spa, bp);
2744 ASSERT(BP_GET_DEDUP(bp));
2745 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2748 freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
2749 ddp = ddt_phys_select(dde, bp);
2750 ddt_phys_decref(ddp);
2753 return (ZIO_PIPELINE_CONTINUE);
2757 * ==========================================================================
2758 * Allocate and free blocks
2759 * ==========================================================================
2763 zio_io_to_allocate(spa_t *spa)
2767 ASSERT(MUTEX_HELD(&spa->spa_alloc_lock));
2769 zio = avl_first(&spa->spa_alloc_tree);
2773 ASSERT(IO_IS_ALLOCATING(zio));
2776 * Try to place a reservation for this zio. If we're unable to
2777 * reserve then we throttle.
2779 if (!metaslab_class_throttle_reserve(spa_normal_class(spa),
2780 zio->io_prop.zp_copies, zio, 0)) {
2784 avl_remove(&spa->spa_alloc_tree, zio);
2785 ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
2791 zio_dva_throttle(zio_t *zio)
2793 spa_t *spa = zio->io_spa;
2796 if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
2797 !spa_normal_class(zio->io_spa)->mc_alloc_throttle_enabled ||
2798 zio->io_child_type == ZIO_CHILD_GANG ||
2799 zio->io_flags & ZIO_FLAG_NODATA) {
2800 return (ZIO_PIPELINE_CONTINUE);
2803 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2805 ASSERT3U(zio->io_queued_timestamp, >, 0);
2806 ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
2808 mutex_enter(&spa->spa_alloc_lock);
2810 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
2811 avl_add(&spa->spa_alloc_tree, zio);
2813 nio = zio_io_to_allocate(zio->io_spa);
2814 mutex_exit(&spa->spa_alloc_lock);
2817 return (ZIO_PIPELINE_CONTINUE);
2820 ASSERT3U(nio->io_queued_timestamp, <=,
2821 zio->io_queued_timestamp);
2822 ASSERT(nio->io_stage == ZIO_STAGE_DVA_THROTTLE);
2824 * We are passing control to a new zio so make sure that
2825 * it is processed by a different thread. We do this to
2826 * avoid stack overflows that can occur when parents are
2827 * throttled and children are making progress. We allow
2828 * it to go to the head of the taskq since it's already
2831 zio_taskq_dispatch(nio, ZIO_TASKQ_ISSUE, B_TRUE);
2833 return (ZIO_PIPELINE_STOP);
2837 zio_allocate_dispatch(spa_t *spa)
2841 mutex_enter(&spa->spa_alloc_lock);
2842 zio = zio_io_to_allocate(spa);
2843 mutex_exit(&spa->spa_alloc_lock);
2847 ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
2848 ASSERT0(zio->io_error);
2849 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
2853 zio_dva_allocate(zio_t *zio)
2855 spa_t *spa = zio->io_spa;
2856 metaslab_class_t *mc = spa_normal_class(spa);
2857 blkptr_t *bp = zio->io_bp;
2861 if (zio->io_gang_leader == NULL) {
2862 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2863 zio->io_gang_leader = zio;
2866 ASSERT(BP_IS_HOLE(bp));
2867 ASSERT0(BP_GET_NDVAS(bp));
2868 ASSERT3U(zio->io_prop.zp_copies, >, 0);
2869 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
2870 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
2872 if (zio->io_flags & ZIO_FLAG_NODATA) {
2873 flags |= METASLAB_DONT_THROTTLE;
2875 if (zio->io_flags & ZIO_FLAG_GANG_CHILD) {
2876 flags |= METASLAB_GANG_CHILD;
2878 if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE) {
2879 flags |= METASLAB_ASYNC_ALLOC;
2882 error = metaslab_alloc(spa, mc, zio->io_size, bp,
2883 zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
2884 &zio->io_alloc_list, zio);
2887 spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, "
2888 "size %llu, error %d", spa_name(spa), zio, zio->io_size,
2890 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
2891 return (zio_write_gang_block(zio));
2892 zio->io_error = error;
2895 return (ZIO_PIPELINE_CONTINUE);
2899 zio_dva_free(zio_t *zio)
2901 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
2903 return (ZIO_PIPELINE_CONTINUE);
2907 zio_dva_claim(zio_t *zio)
2911 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
2913 zio->io_error = error;
2915 return (ZIO_PIPELINE_CONTINUE);
2919 * Undo an allocation. This is used by zio_done() when an I/O fails
2920 * and we want to give back the block we just allocated.
2921 * This handles both normal blocks and gang blocks.
2924 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
2926 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
2927 ASSERT(zio->io_bp_override == NULL);
2929 if (!BP_IS_HOLE(bp))
2930 metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
2933 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2934 zio_dva_unallocate(zio, gn->gn_child[g],
2935 &gn->gn_gbh->zg_blkptr[g]);
2941 * Try to allocate an intent log block. Return 0 on success, errno on failure.
2944 zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, blkptr_t *old_bp,
2945 uint64_t size, boolean_t *slog)
2948 zio_alloc_list_t io_alloc_list;
2950 ASSERT(txg > spa_syncing_txg(spa));
2952 metaslab_trace_init(&io_alloc_list);
2953 error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1,
2954 txg, old_bp, METASLAB_HINTBP_AVOID, &io_alloc_list, NULL);
2958 error = metaslab_alloc(spa, spa_normal_class(spa), size,
2959 new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID,
2960 &io_alloc_list, NULL);
2964 metaslab_trace_fini(&io_alloc_list);
2967 BP_SET_LSIZE(new_bp, size);
2968 BP_SET_PSIZE(new_bp, size);
2969 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
2970 BP_SET_CHECKSUM(new_bp,
2971 spa_version(spa) >= SPA_VERSION_SLIM_ZIL
2972 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
2973 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
2974 BP_SET_LEVEL(new_bp, 0);
2975 BP_SET_DEDUP(new_bp, 0);
2976 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
2983 * Free an intent log block.
2986 zio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp)
2988 ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG);
2989 ASSERT(!BP_IS_GANG(bp));
2991 zio_free(spa, txg, bp);
2995 * ==========================================================================
2996 * Read, write and delete to physical devices
2997 * ==========================================================================
3002 * Issue an I/O to the underlying vdev. Typically the issue pipeline
3003 * stops after this stage and will resume upon I/O completion.
3004 * However, there are instances where the vdev layer may need to
3005 * continue the pipeline when an I/O was not issued. Since the I/O
3006 * that was sent to the vdev layer might be different than the one
3007 * currently active in the pipeline (see vdev_queue_io()), we explicitly
3008 * force the underlying vdev layers to call either zio_execute() or
3009 * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3012 zio_vdev_io_start(zio_t *zio)
3014 vdev_t *vd = zio->io_vd;
3016 spa_t *spa = zio->io_spa;
3019 ASSERT(zio->io_error == 0);
3020 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
3023 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3024 spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
3027 * The mirror_ops handle multiple DVAs in a single BP.
3029 vdev_mirror_ops.vdev_op_io_start(zio);
3030 return (ZIO_PIPELINE_STOP);
3033 if (vd->vdev_ops->vdev_op_leaf && zio->io_type == ZIO_TYPE_FREE &&
3034 zio->io_priority == ZIO_PRIORITY_NOW) {
3035 trim_map_free(vd, zio->io_offset, zio->io_size, zio->io_txg);
3036 return (ZIO_PIPELINE_CONTINUE);
3039 ASSERT3P(zio->io_logical, !=, zio);
3042 * We keep track of time-sensitive I/Os so that the scan thread
3043 * can quickly react to certain workloads. In particular, we care
3044 * about non-scrubbing, top-level reads and writes with the following
3046 * - synchronous writes of user data to non-slog devices
3047 * - any reads of user data
3048 * When these conditions are met, adjust the timestamp of spa_last_io
3049 * which allows the scan thread to adjust its workload accordingly.
3051 if (!(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && zio->io_bp != NULL &&
3052 vd == vd->vdev_top && !vd->vdev_islog &&
3053 zio->io_bookmark.zb_objset != DMU_META_OBJSET &&
3054 zio->io_txg != spa_syncing_txg(spa)) {
3055 uint64_t old = spa->spa_last_io;
3056 uint64_t new = ddi_get_lbolt64();
3058 (void) atomic_cas_64(&spa->spa_last_io, old, new);
3061 align = 1ULL << vd->vdev_top->vdev_ashift;
3063 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
3064 P2PHASE(zio->io_size, align) != 0) {
3065 /* Transform logical writes to be a full physical block size. */
3066 uint64_t asize = P2ROUNDUP(zio->io_size, align);
3068 if (zio->io_type == ZIO_TYPE_READ ||
3069 zio->io_type == ZIO_TYPE_WRITE)
3070 abuf = zio_buf_alloc(asize);
3071 ASSERT(vd == vd->vdev_top);
3072 if (zio->io_type == ZIO_TYPE_WRITE) {
3073 bcopy(zio->io_data, abuf, zio->io_size);
3074 bzero(abuf + zio->io_size, asize - zio->io_size);
3076 zio_push_transform(zio, abuf, asize, abuf ? asize : 0,
3081 * If this is not a physical io, make sure that it is properly aligned
3082 * before proceeding.
3084 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
3085 ASSERT0(P2PHASE(zio->io_offset, align));
3086 ASSERT0(P2PHASE(zio->io_size, align));
3089 * For the physical io we allow alignment
3090 * to a logical block size.
3092 uint64_t log_align =
3093 1ULL << vd->vdev_top->vdev_logical_ashift;
3094 ASSERT0(P2PHASE(zio->io_offset, log_align));
3095 ASSERT0(P2PHASE(zio->io_size, log_align));
3098 VERIFY(zio->io_type == ZIO_TYPE_READ || spa_writeable(spa));
3101 * If this is a repair I/O, and there's no self-healing involved --
3102 * that is, we're just resilvering what we expect to resilver --
3103 * then don't do the I/O unless zio's txg is actually in vd's DTL.
3104 * This prevents spurious resilvering with nested replication.
3105 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
3106 * A is out of date, we'll read from C+D, then use the data to
3107 * resilver A+B -- but we don't actually want to resilver B, just A.
3108 * The top-level mirror has no way to know this, so instead we just
3109 * discard unnecessary repairs as we work our way down the vdev tree.
3110 * The same logic applies to any form of nested replication:
3111 * ditto + mirror, RAID-Z + replacing, etc. This covers them all.
3113 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
3114 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
3115 zio->io_txg != 0 && /* not a delegated i/o */
3116 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
3117 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3118 zio_vdev_io_bypass(zio);
3119 return (ZIO_PIPELINE_CONTINUE);
3122 if (vd->vdev_ops->vdev_op_leaf) {
3123 switch (zio->io_type) {
3125 if (vdev_cache_read(zio))
3126 return (ZIO_PIPELINE_CONTINUE);
3128 case ZIO_TYPE_WRITE:
3130 if ((zio = vdev_queue_io(zio)) == NULL)
3131 return (ZIO_PIPELINE_STOP);
3133 if (!vdev_accessible(vd, zio)) {
3134 zio->io_error = SET_ERROR(ENXIO);
3136 return (ZIO_PIPELINE_STOP);
3141 * Note that we ignore repair writes for TRIM because they can
3142 * conflict with normal writes. This isn't an issue because, by
3143 * definition, we only repair blocks that aren't freed.
3145 if (zio->io_type == ZIO_TYPE_WRITE &&
3146 !(zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
3147 !trim_map_write_start(zio))
3148 return (ZIO_PIPELINE_STOP);
3151 vd->vdev_ops->vdev_op_io_start(zio);
3152 return (ZIO_PIPELINE_STOP);
3156 zio_vdev_io_done(zio_t *zio)
3158 vdev_t *vd = zio->io_vd;
3159 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
3160 boolean_t unexpected_error = B_FALSE;
3162 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
3163 return (ZIO_PIPELINE_STOP);
3165 ASSERT(zio->io_type == ZIO_TYPE_READ ||
3166 zio->io_type == ZIO_TYPE_WRITE || zio->io_type == ZIO_TYPE_FREE);
3168 if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3169 (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE ||
3170 zio->io_type == ZIO_TYPE_FREE)) {
3172 if (zio->io_type == ZIO_TYPE_WRITE &&
3173 !(zio->io_flags & ZIO_FLAG_IO_REPAIR))
3174 trim_map_write_done(zio);
3176 vdev_queue_io_done(zio);
3178 if (zio->io_type == ZIO_TYPE_WRITE)
3179 vdev_cache_write(zio);
3181 if (zio_injection_enabled && zio->io_error == 0)
3182 zio->io_error = zio_handle_device_injection(vd,
3185 if (zio_injection_enabled && zio->io_error == 0)
3186 zio->io_error = zio_handle_label_injection(zio, EIO);
3188 if (zio->io_error) {
3189 if (zio->io_error == ENOTSUP &&
3190 zio->io_type == ZIO_TYPE_FREE) {
3191 /* Not all devices support TRIM. */
3192 } else if (!vdev_accessible(vd, zio)) {
3193 zio->io_error = SET_ERROR(ENXIO);
3195 unexpected_error = B_TRUE;
3200 ops->vdev_op_io_done(zio);
3202 if (unexpected_error)
3203 VERIFY(vdev_probe(vd, zio) == NULL);
3205 return (ZIO_PIPELINE_CONTINUE);
3209 * For non-raidz ZIOs, we can just copy aside the bad data read from the
3210 * disk, and use that to finish the checksum ereport later.
3213 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
3214 const void *good_buf)
3216 /* no processing needed */
3217 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
3222 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
3224 void *buf = zio_buf_alloc(zio->io_size);
3226 bcopy(zio->io_data, buf, zio->io_size);
3228 zcr->zcr_cbinfo = zio->io_size;
3229 zcr->zcr_cbdata = buf;
3230 zcr->zcr_finish = zio_vsd_default_cksum_finish;
3231 zcr->zcr_free = zio_buf_free;
3235 zio_vdev_io_assess(zio_t *zio)
3237 vdev_t *vd = zio->io_vd;
3239 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
3240 return (ZIO_PIPELINE_STOP);
3242 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3243 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
3245 if (zio->io_vsd != NULL) {
3246 zio->io_vsd_ops->vsd_free(zio);
3250 if (zio_injection_enabled && zio->io_error == 0)
3251 zio->io_error = zio_handle_fault_injection(zio, EIO);
3253 if (zio->io_type == ZIO_TYPE_FREE &&
3254 zio->io_priority != ZIO_PRIORITY_NOW) {
3255 switch (zio->io_error) {
3257 ZIO_TRIM_STAT_INCR(bytes, zio->io_size);
3258 ZIO_TRIM_STAT_BUMP(success);
3261 ZIO_TRIM_STAT_BUMP(unsupported);
3264 ZIO_TRIM_STAT_BUMP(failed);
3270 * If the I/O failed, determine whether we should attempt to retry it.
3272 * On retry, we cut in line in the issue queue, since we don't want
3273 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
3275 if (zio->io_error && vd == NULL &&
3276 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
3277 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
3278 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */
3280 zio->io_flags |= ZIO_FLAG_IO_RETRY |
3281 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
3282 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
3283 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
3284 zio_requeue_io_start_cut_in_line);
3285 return (ZIO_PIPELINE_STOP);
3289 * If we got an error on a leaf device, convert it to ENXIO
3290 * if the device is not accessible at all.
3292 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3293 !vdev_accessible(vd, zio))
3294 zio->io_error = SET_ERROR(ENXIO);
3297 * If we can't write to an interior vdev (mirror or RAID-Z),
3298 * set vdev_cant_write so that we stop trying to allocate from it.
3300 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
3301 vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
3302 vd->vdev_cant_write = B_TRUE;
3306 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3308 if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3309 zio->io_physdone != NULL) {
3310 ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED));
3311 ASSERT(zio->io_child_type == ZIO_CHILD_VDEV);
3312 zio->io_physdone(zio->io_logical);
3315 return (ZIO_PIPELINE_CONTINUE);
3319 zio_vdev_io_reissue(zio_t *zio)
3321 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3322 ASSERT(zio->io_error == 0);
3324 zio->io_stage >>= 1;
3328 zio_vdev_io_redone(zio_t *zio)
3330 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
3332 zio->io_stage >>= 1;
3336 zio_vdev_io_bypass(zio_t *zio)
3338 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3339 ASSERT(zio->io_error == 0);
3341 zio->io_flags |= ZIO_FLAG_IO_BYPASS;
3342 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
3346 * ==========================================================================
3347 * Generate and verify checksums
3348 * ==========================================================================
3351 zio_checksum_generate(zio_t *zio)
3353 blkptr_t *bp = zio->io_bp;
3354 enum zio_checksum checksum;
3358 * This is zio_write_phys().
3359 * We're either generating a label checksum, or none at all.
3361 checksum = zio->io_prop.zp_checksum;
3363 if (checksum == ZIO_CHECKSUM_OFF)
3364 return (ZIO_PIPELINE_CONTINUE);
3366 ASSERT(checksum == ZIO_CHECKSUM_LABEL);
3368 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
3369 ASSERT(!IO_IS_ALLOCATING(zio));
3370 checksum = ZIO_CHECKSUM_GANG_HEADER;
3372 checksum = BP_GET_CHECKSUM(bp);
3376 zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size);
3378 return (ZIO_PIPELINE_CONTINUE);
3382 zio_checksum_verify(zio_t *zio)
3384 zio_bad_cksum_t info;
3385 blkptr_t *bp = zio->io_bp;
3388 ASSERT(zio->io_vd != NULL);
3392 * This is zio_read_phys().
3393 * We're either verifying a label checksum, or nothing at all.
3395 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
3396 return (ZIO_PIPELINE_CONTINUE);
3398 ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
3401 if ((error = zio_checksum_error(zio, &info)) != 0) {
3402 zio->io_error = error;
3403 if (error == ECKSUM &&
3404 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
3405 zfs_ereport_start_checksum(zio->io_spa,
3406 zio->io_vd, zio, zio->io_offset,
3407 zio->io_size, NULL, &info);
3411 return (ZIO_PIPELINE_CONTINUE);
3415 * Called by RAID-Z to ensure we don't compute the checksum twice.
3418 zio_checksum_verified(zio_t *zio)
3420 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
3424 * ==========================================================================
3425 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
3426 * An error of 0 indicates success. ENXIO indicates whole-device failure,
3427 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
3428 * indicate errors that are specific to one I/O, and most likely permanent.
3429 * Any other error is presumed to be worse because we weren't expecting it.
3430 * ==========================================================================
3433 zio_worst_error(int e1, int e2)
3435 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
3438 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
3439 if (e1 == zio_error_rank[r1])
3442 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
3443 if (e2 == zio_error_rank[r2])
3446 return (r1 > r2 ? e1 : e2);
3450 * ==========================================================================
3452 * ==========================================================================
3455 zio_ready(zio_t *zio)
3457 blkptr_t *bp = zio->io_bp;
3458 zio_t *pio, *pio_next;
3459 zio_link_t *zl = NULL;
3461 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
3462 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY))
3463 return (ZIO_PIPELINE_STOP);
3465 if (zio->io_ready) {
3466 ASSERT(IO_IS_ALLOCATING(zio));
3467 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
3468 (zio->io_flags & ZIO_FLAG_NOPWRITE));
3469 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
3474 if (bp != NULL && bp != &zio->io_bp_copy)
3475 zio->io_bp_copy = *bp;
3477 if (zio->io_error != 0) {
3478 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3480 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3481 ASSERT(IO_IS_ALLOCATING(zio));
3482 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3484 * We were unable to allocate anything, unreserve and
3485 * issue the next I/O to allocate.
3487 metaslab_class_throttle_unreserve(
3488 spa_normal_class(zio->io_spa),
3489 zio->io_prop.zp_copies, zio);
3490 zio_allocate_dispatch(zio->io_spa);
3494 mutex_enter(&zio->io_lock);
3495 zio->io_state[ZIO_WAIT_READY] = 1;
3496 pio = zio_walk_parents(zio, &zl);
3497 mutex_exit(&zio->io_lock);
3500 * As we notify zio's parents, new parents could be added.
3501 * New parents go to the head of zio's io_parent_list, however,
3502 * so we will (correctly) not notify them. The remainder of zio's
3503 * io_parent_list, from 'pio_next' onward, cannot change because
3504 * all parents must wait for us to be done before they can be done.
3506 for (; pio != NULL; pio = pio_next) {
3507 pio_next = zio_walk_parents(zio, &zl);
3508 zio_notify_parent(pio, zio, ZIO_WAIT_READY);
3511 if (zio->io_flags & ZIO_FLAG_NODATA) {
3512 if (BP_IS_GANG(bp)) {
3513 zio->io_flags &= ~ZIO_FLAG_NODATA;
3515 ASSERT((uintptr_t)zio->io_data < SPA_MAXBLOCKSIZE);
3516 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
3520 if (zio_injection_enabled &&
3521 zio->io_spa->spa_syncing_txg == zio->io_txg)
3522 zio_handle_ignored_writes(zio);
3524 return (ZIO_PIPELINE_CONTINUE);
3528 * Update the allocation throttle accounting.
3531 zio_dva_throttle_done(zio_t *zio)
3533 zio_t *lio = zio->io_logical;
3534 zio_t *pio = zio_unique_parent(zio);
3535 vdev_t *vd = zio->io_vd;
3536 int flags = METASLAB_ASYNC_ALLOC;
3538 ASSERT3P(zio->io_bp, !=, NULL);
3539 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
3540 ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
3541 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
3543 ASSERT3P(vd, ==, vd->vdev_top);
3544 ASSERT(!(zio->io_flags & (ZIO_FLAG_IO_REPAIR | ZIO_FLAG_IO_RETRY)));
3545 ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING);
3546 ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE));
3547 ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA));
3550 * Parents of gang children can have two flavors -- ones that
3551 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
3552 * and ones that allocated the constituent blocks. The allocation
3553 * throttle needs to know the allocating parent zio so we must find
3556 if (pio->io_child_type == ZIO_CHILD_GANG) {
3558 * If our parent is a rewrite gang child then our grandparent
3559 * would have been the one that performed the allocation.
3561 if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
3562 pio = zio_unique_parent(pio);
3563 flags |= METASLAB_GANG_CHILD;
3566 ASSERT(IO_IS_ALLOCATING(pio));
3567 ASSERT3P(zio, !=, zio->io_logical);
3568 ASSERT(zio->io_logical != NULL);
3569 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
3570 ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
3572 mutex_enter(&pio->io_lock);
3573 metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags);
3574 mutex_exit(&pio->io_lock);
3576 metaslab_class_throttle_unreserve(spa_normal_class(zio->io_spa),
3580 * Call into the pipeline to see if there is more work that
3581 * needs to be done. If there is work to be done it will be
3582 * dispatched to another taskq thread.
3584 zio_allocate_dispatch(zio->io_spa);
3588 zio_done(zio_t *zio)
3590 spa_t *spa = zio->io_spa;
3591 zio_t *lio = zio->io_logical;
3592 blkptr_t *bp = zio->io_bp;
3593 vdev_t *vd = zio->io_vd;
3594 uint64_t psize = zio->io_size;
3595 zio_t *pio, *pio_next;
3596 metaslab_class_t *mc = spa_normal_class(spa);
3597 zio_link_t *zl = NULL;
3600 * If our children haven't all completed,
3601 * wait for them and then repeat this pipeline stage.
3603 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
3604 zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
3605 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) ||
3606 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
3607 return (ZIO_PIPELINE_STOP);
3610 * If the allocation throttle is enabled, then update the accounting.
3611 * We only track child I/Os that are part of an allocating async
3612 * write. We must do this since the allocation is performed
3613 * by the logical I/O but the actual write is done by child I/Os.
3615 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
3616 zio->io_child_type == ZIO_CHILD_VDEV) {
3617 ASSERT(mc->mc_alloc_throttle_enabled);
3618 zio_dva_throttle_done(zio);
3622 * If the allocation throttle is enabled, verify that
3623 * we have decremented the refcounts for every I/O that was throttled.
3625 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3626 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3627 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3629 metaslab_group_alloc_verify(spa, zio->io_bp, zio);
3630 VERIFY(refcount_not_held(&mc->mc_alloc_slots, zio));
3633 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
3634 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
3635 ASSERT(zio->io_children[c][w] == 0);
3637 if (bp != NULL && !BP_IS_EMBEDDED(bp)) {
3638 ASSERT(bp->blk_pad[0] == 0);
3639 ASSERT(bp->blk_pad[1] == 0);
3640 ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
3641 (bp == zio_unique_parent(zio)->io_bp));
3642 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
3643 zio->io_bp_override == NULL &&
3644 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
3645 ASSERT(!BP_SHOULD_BYTESWAP(bp));
3646 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp));
3647 ASSERT(BP_COUNT_GANG(bp) == 0 ||
3648 (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
3650 if (zio->io_flags & ZIO_FLAG_NOPWRITE)
3651 VERIFY(BP_EQUAL(bp, &zio->io_bp_orig));
3655 * If there were child vdev/gang/ddt errors, they apply to us now.
3657 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
3658 zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
3659 zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
3662 * If the I/O on the transformed data was successful, generate any
3663 * checksum reports now while we still have the transformed data.
3665 if (zio->io_error == 0) {
3666 while (zio->io_cksum_report != NULL) {
3667 zio_cksum_report_t *zcr = zio->io_cksum_report;
3668 uint64_t align = zcr->zcr_align;
3669 uint64_t asize = P2ROUNDUP(psize, align);
3670 char *abuf = zio->io_data;
3672 if (asize != psize) {
3673 abuf = zio_buf_alloc(asize);
3674 bcopy(zio->io_data, abuf, psize);
3675 bzero(abuf + psize, asize - psize);
3678 zio->io_cksum_report = zcr->zcr_next;
3679 zcr->zcr_next = NULL;
3680 zcr->zcr_finish(zcr, abuf);
3681 zfs_ereport_free_checksum(zcr);
3684 zio_buf_free(abuf, asize);
3688 zio_pop_transforms(zio); /* note: may set zio->io_error */
3690 vdev_stat_update(zio, psize);
3692 if (zio->io_error) {
3694 * If this I/O is attached to a particular vdev,
3695 * generate an error message describing the I/O failure
3696 * at the block level. We ignore these errors if the
3697 * device is currently unavailable.
3699 if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
3700 zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0);
3702 if ((zio->io_error == EIO || !(zio->io_flags &
3703 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
3706 * For logical I/O requests, tell the SPA to log the
3707 * error and generate a logical data ereport.
3709 spa_log_error(spa, zio);
3710 zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio,
3715 if (zio->io_error && zio == lio) {
3717 * Determine whether zio should be reexecuted. This will
3718 * propagate all the way to the root via zio_notify_parent().
3720 ASSERT(vd == NULL && bp != NULL);
3721 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3723 if (IO_IS_ALLOCATING(zio) &&
3724 !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
3725 if (zio->io_error != ENOSPC)
3726 zio->io_reexecute |= ZIO_REEXECUTE_NOW;
3728 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3731 if ((zio->io_type == ZIO_TYPE_READ ||
3732 zio->io_type == ZIO_TYPE_FREE) &&
3733 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
3734 zio->io_error == ENXIO &&
3735 spa_load_state(spa) == SPA_LOAD_NONE &&
3736 spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
3737 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3739 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
3740 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3743 * Here is a possibly good place to attempt to do
3744 * either combinatorial reconstruction or error correction
3745 * based on checksums. It also might be a good place
3746 * to send out preliminary ereports before we suspend
3752 * If there were logical child errors, they apply to us now.
3753 * We defer this until now to avoid conflating logical child
3754 * errors with errors that happened to the zio itself when
3755 * updating vdev stats and reporting FMA events above.
3757 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
3759 if ((zio->io_error || zio->io_reexecute) &&
3760 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
3761 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
3762 zio_dva_unallocate(zio, zio->io_gang_tree, bp);
3764 zio_gang_tree_free(&zio->io_gang_tree);
3767 * Godfather I/Os should never suspend.
3769 if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
3770 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
3771 zio->io_reexecute = 0;
3773 if (zio->io_reexecute) {
3775 * This is a logical I/O that wants to reexecute.
3777 * Reexecute is top-down. When an i/o fails, if it's not
3778 * the root, it simply notifies its parent and sticks around.
3779 * The parent, seeing that it still has children in zio_done(),
3780 * does the same. This percolates all the way up to the root.
3781 * The root i/o will reexecute or suspend the entire tree.
3783 * This approach ensures that zio_reexecute() honors
3784 * all the original i/o dependency relationships, e.g.
3785 * parents not executing until children are ready.
3787 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3789 zio->io_gang_leader = NULL;
3791 mutex_enter(&zio->io_lock);
3792 zio->io_state[ZIO_WAIT_DONE] = 1;
3793 mutex_exit(&zio->io_lock);
3796 * "The Godfather" I/O monitors its children but is
3797 * not a true parent to them. It will track them through
3798 * the pipeline but severs its ties whenever they get into
3799 * trouble (e.g. suspended). This allows "The Godfather"
3800 * I/O to return status without blocking.
3803 for (pio = zio_walk_parents(zio, &zl); pio != NULL;
3805 zio_link_t *remove_zl = zl;
3806 pio_next = zio_walk_parents(zio, &zl);
3808 if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
3809 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
3810 zio_remove_child(pio, zio, remove_zl);
3811 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3815 if ((pio = zio_unique_parent(zio)) != NULL) {
3817 * We're not a root i/o, so there's nothing to do
3818 * but notify our parent. Don't propagate errors
3819 * upward since we haven't permanently failed yet.
3821 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
3822 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
3823 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3824 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
3826 * We'd fail again if we reexecuted now, so suspend
3827 * until conditions improve (e.g. device comes online).
3829 zio_suspend(spa, zio);
3832 * Reexecution is potentially a huge amount of work.
3833 * Hand it off to the otherwise-unused claim taskq.
3835 #if defined(illumos) || !defined(_KERNEL)
3836 ASSERT(zio->io_tqent.tqent_next == NULL);
3838 ASSERT(zio->io_tqent.tqent_task.ta_pending == 0);
3840 spa_taskq_dispatch_ent(spa, ZIO_TYPE_CLAIM,
3841 ZIO_TASKQ_ISSUE, (task_func_t *)zio_reexecute, zio,
3844 return (ZIO_PIPELINE_STOP);
3847 ASSERT(zio->io_child_count == 0);
3848 ASSERT(zio->io_reexecute == 0);
3849 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
3852 * Report any checksum errors, since the I/O is complete.
3854 while (zio->io_cksum_report != NULL) {
3855 zio_cksum_report_t *zcr = zio->io_cksum_report;
3856 zio->io_cksum_report = zcr->zcr_next;
3857 zcr->zcr_next = NULL;
3858 zcr->zcr_finish(zcr, NULL);
3859 zfs_ereport_free_checksum(zcr);
3863 * It is the responsibility of the done callback to ensure that this
3864 * particular zio is no longer discoverable for adoption, and as
3865 * such, cannot acquire any new parents.
3870 mutex_enter(&zio->io_lock);
3871 zio->io_state[ZIO_WAIT_DONE] = 1;
3872 mutex_exit(&zio->io_lock);
3875 for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
3876 zio_link_t *remove_zl = zl;
3877 pio_next = zio_walk_parents(zio, &zl);
3878 zio_remove_child(pio, zio, remove_zl);
3879 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3882 if (zio->io_waiter != NULL) {
3883 mutex_enter(&zio->io_lock);
3884 zio->io_executor = NULL;
3885 cv_broadcast(&zio->io_cv);
3886 mutex_exit(&zio->io_lock);
3891 return (ZIO_PIPELINE_STOP);
3895 * ==========================================================================
3896 * I/O pipeline definition
3897 * ==========================================================================
3899 static zio_pipe_stage_t *zio_pipeline[] = {
3906 zio_checksum_generate,
3922 zio_checksum_verify,
3930 * Compare two zbookmark_phys_t's to see which we would reach first in a
3931 * pre-order traversal of the object tree.
3933 * This is simple in every case aside from the meta-dnode object. For all other
3934 * objects, we traverse them in order (object 1 before object 2, and so on).
3935 * However, all of these objects are traversed while traversing object 0, since
3936 * the data it points to is the list of objects. Thus, we need to convert to a
3937 * canonical representation so we can compare meta-dnode bookmarks to
3938 * non-meta-dnode bookmarks.
3940 * We do this by calculating "equivalents" for each field of the zbookmark.
3941 * zbookmarks outside of the meta-dnode use their own object and level, and
3942 * calculate the level 0 equivalent (the first L0 blkid that is contained in the
3943 * blocks this bookmark refers to) by multiplying their blkid by their span
3944 * (the number of L0 blocks contained within one block at their level).
3945 * zbookmarks inside the meta-dnode calculate their object equivalent
3946 * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
3947 * level + 1<<31 (any value larger than a level could ever be) for their level.
3948 * This causes them to always compare before a bookmark in their object
3949 * equivalent, compare appropriately to bookmarks in other objects, and to
3950 * compare appropriately to other bookmarks in the meta-dnode.
3953 zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
3954 const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
3957 * These variables represent the "equivalent" values for the zbookmark,
3958 * after converting zbookmarks inside the meta dnode to their
3959 * normal-object equivalents.
3961 uint64_t zb1obj, zb2obj;
3962 uint64_t zb1L0, zb2L0;
3963 uint64_t zb1level, zb2level;
3965 if (zb1->zb_object == zb2->zb_object &&
3966 zb1->zb_level == zb2->zb_level &&
3967 zb1->zb_blkid == zb2->zb_blkid)
3971 * BP_SPANB calculates the span in blocks.
3973 zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
3974 zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
3976 if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
3977 zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
3979 zb1level = zb1->zb_level + COMPARE_META_LEVEL;
3981 zb1obj = zb1->zb_object;
3982 zb1level = zb1->zb_level;
3985 if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
3986 zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
3988 zb2level = zb2->zb_level + COMPARE_META_LEVEL;
3990 zb2obj = zb2->zb_object;
3991 zb2level = zb2->zb_level;
3994 /* Now that we have a canonical representation, do the comparison. */
3995 if (zb1obj != zb2obj)
3996 return (zb1obj < zb2obj ? -1 : 1);
3997 else if (zb1L0 != zb2L0)
3998 return (zb1L0 < zb2L0 ? -1 : 1);
3999 else if (zb1level != zb2level)
4000 return (zb1level > zb2level ? -1 : 1);
4002 * This can (theoretically) happen if the bookmarks have the same object
4003 * and level, but different blkids, if the block sizes are not the same.
4004 * There is presently no way to change the indirect block sizes
4010 * This function checks the following: given that last_block is the place that
4011 * our traversal stopped last time, does that guarantee that we've visited
4012 * every node under subtree_root? Therefore, we can't just use the raw output
4013 * of zbookmark_compare. We have to pass in a modified version of
4014 * subtree_root; by incrementing the block id, and then checking whether
4015 * last_block is before or equal to that, we can tell whether or not having
4016 * visited last_block implies that all of subtree_root's children have been
4020 zbookmark_subtree_completed(const dnode_phys_t *dnp,
4021 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
4023 zbookmark_phys_t mod_zb = *subtree_root;
4025 ASSERT(last_block->zb_level == 0);
4027 /* The objset_phys_t isn't before anything. */
4032 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
4033 * data block size in sectors, because that variable is only used if
4034 * the bookmark refers to a block in the meta-dnode. Since we don't
4035 * know without examining it what object it refers to, and there's no
4036 * harm in passing in this value in other cases, we always pass it in.
4038 * We pass in 0 for the indirect block size shift because zb2 must be
4039 * level 0. The indirect block size is only used to calculate the span
4040 * of the bookmark, but since the bookmark must be level 0, the span is
4041 * always 1, so the math works out.
4043 * If you make changes to how the zbookmark_compare code works, be sure
4044 * to make sure that this code still works afterwards.
4046 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
4047 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,