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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
26 * Copyright 2016 Nexenta Systems, Inc.
27 * Copyright (c) 2017, 2018 Lawrence Livermore National Security, LLC.
28 * Copyright (c) 2015, 2017, Intel Corporation.
29 * Copyright (c) 2020 Datto Inc.
30 * Copyright (c) 2020, The FreeBSD Foundation [1]
32 * [1] Portions of this software were developed by Allan Jude
33 * under sponsorship from the FreeBSD Foundation.
40 #include <sys/zfs_context.h>
42 #include <sys/spa_impl.h>
45 #include <sys/fs/zfs.h>
46 #include <sys/zfs_znode.h>
47 #include <sys/zfs_sa.h>
49 #include <sys/sa_impl.h>
51 #include <sys/vdev_impl.h>
52 #include <sys/metaslab_impl.h>
53 #include <sys/dmu_objset.h>
54 #include <sys/dsl_dir.h>
55 #include <sys/dsl_dataset.h>
56 #include <sys/dsl_pool.h>
57 #include <sys/dsl_bookmark.h>
60 #include <sys/zil_impl.h>
62 #include <sys/resource.h>
63 #include <sys/dmu_send.h>
64 #include <sys/dmu_traverse.h>
65 #include <sys/zio_checksum.h>
66 #include <sys/zio_compress.h>
67 #include <sys/zfs_fuid.h>
69 #include <sys/arc_impl.h>
71 #include <sys/zfeature.h>
73 #include <sys/blkptr.h>
74 #include <sys/dsl_crypt.h>
75 #include <sys/dsl_scan.h>
76 #include <sys/btree.h>
77 #include <zfs_comutil.h>
78 #include <sys/zstd/zstd.h>
80 #include <libnvpair.h>
85 #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
86 zio_compress_table[(idx)].ci_name : "UNKNOWN")
87 #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
88 zio_checksum_table[(idx)].ci_name : "UNKNOWN")
89 #define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \
90 (idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA ? \
92 (idx) == DMU_OTN_UINT64_DATA || (idx) == DMU_OTN_UINT64_METADATA ? \
93 DMU_OT_UINT64_OTHER : DMU_OT_NUMTYPES)
96 zdb_ot_name(dmu_object_type_t type)
98 if (type < DMU_OT_NUMTYPES)
99 return (dmu_ot[type].ot_name);
100 else if ((type & DMU_OT_NEWTYPE) &&
101 ((type & DMU_OT_BYTESWAP_MASK) < DMU_BSWAP_NUMFUNCS))
102 return (dmu_ot_byteswap[type & DMU_OT_BYTESWAP_MASK].ob_name);
107 extern int reference_tracking_enable;
108 extern int zfs_recover;
109 extern unsigned long zfs_arc_meta_min, zfs_arc_meta_limit;
110 extern int zfs_vdev_async_read_max_active;
111 extern boolean_t spa_load_verify_dryrun;
112 extern int zfs_reconstruct_indirect_combinations_max;
113 extern int zfs_btree_verify_intensity;
115 static const char cmdname[] = "zdb";
116 uint8_t dump_opt[256];
118 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
120 uint64_t *zopt_metaslab = NULL;
121 static unsigned zopt_metaslab_args = 0;
123 typedef struct zopt_object_range {
124 uint64_t zor_obj_start;
125 uint64_t zor_obj_end;
127 } zopt_object_range_t;
128 zopt_object_range_t *zopt_object_ranges = NULL;
129 static unsigned zopt_object_args = 0;
131 static int flagbits[256];
133 #define ZOR_FLAG_PLAIN_FILE 0x0001
134 #define ZOR_FLAG_DIRECTORY 0x0002
135 #define ZOR_FLAG_SPACE_MAP 0x0004
136 #define ZOR_FLAG_ZAP 0x0008
137 #define ZOR_FLAG_ALL_TYPES -1
138 #define ZOR_SUPPORTED_FLAGS (ZOR_FLAG_PLAIN_FILE | \
139 ZOR_FLAG_DIRECTORY | \
140 ZOR_FLAG_SPACE_MAP | \
143 #define ZDB_FLAG_CHECKSUM 0x0001
144 #define ZDB_FLAG_DECOMPRESS 0x0002
145 #define ZDB_FLAG_BSWAP 0x0004
146 #define ZDB_FLAG_GBH 0x0008
147 #define ZDB_FLAG_INDIRECT 0x0010
148 #define ZDB_FLAG_RAW 0x0020
149 #define ZDB_FLAG_PRINT_BLKPTR 0x0040
150 #define ZDB_FLAG_VERBOSE 0x0080
152 uint64_t max_inflight_bytes = 256 * 1024 * 1024; /* 256MB */
153 static int leaked_objects = 0;
154 static range_tree_t *mos_refd_objs;
156 static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *,
158 static void mos_obj_refd(uint64_t);
159 static void mos_obj_refd_multiple(uint64_t);
160 static int dump_bpobj_cb(void *arg, const blkptr_t *bp, boolean_t free,
163 typedef struct sublivelist_verify {
164 /* all ALLOC'd blkptr_t in one sub-livelist */
165 zfs_btree_t sv_all_allocs;
167 /* all FREE'd blkptr_t in one sub-livelist */
168 zfs_btree_t sv_all_frees;
170 /* FREE's that haven't yet matched to an ALLOC, in one sub-livelist */
173 /* ALLOC's without a matching FREE, accumulates across sub-livelists */
174 zfs_btree_t sv_leftover;
175 } sublivelist_verify_t;
178 livelist_compare(const void *larg, const void *rarg)
180 const blkptr_t *l = larg;
181 const blkptr_t *r = rarg;
183 /* Sort them according to dva[0] */
184 uint64_t l_dva0_vdev, r_dva0_vdev;
185 l_dva0_vdev = DVA_GET_VDEV(&l->blk_dva[0]);
186 r_dva0_vdev = DVA_GET_VDEV(&r->blk_dva[0]);
187 if (l_dva0_vdev < r_dva0_vdev)
189 else if (l_dva0_vdev > r_dva0_vdev)
192 /* if vdevs are equal, sort by offsets. */
193 uint64_t l_dva0_offset;
194 uint64_t r_dva0_offset;
195 l_dva0_offset = DVA_GET_OFFSET(&l->blk_dva[0]);
196 r_dva0_offset = DVA_GET_OFFSET(&r->blk_dva[0]);
197 if (l_dva0_offset < r_dva0_offset) {
199 } else if (l_dva0_offset > r_dva0_offset) {
204 * Since we're storing blkptrs without cancelling FREE/ALLOC pairs,
205 * it's possible the offsets are equal. In that case, sort by txg
207 if (l->blk_birth < r->blk_birth) {
209 } else if (l->blk_birth > r->blk_birth) {
215 typedef struct sublivelist_verify_block {
219 * We need this to check if the block marked as allocated
220 * in the livelist was freed (and potentially reallocated)
221 * in the metaslab spacemaps at a later TXG.
223 uint64_t svb_allocated_txg;
224 } sublivelist_verify_block_t;
226 static void zdb_print_blkptr(const blkptr_t *bp, int flags);
229 sublivelist_verify_blkptr(void *arg, const blkptr_t *bp, boolean_t free,
232 ASSERT3P(tx, ==, NULL);
233 struct sublivelist_verify *sv = arg;
234 char blkbuf[BP_SPRINTF_LEN];
235 zfs_btree_index_t where;
237 zfs_btree_add(&sv->sv_pair, bp);
238 /* Check if the FREE is a duplicate */
239 if (zfs_btree_find(&sv->sv_all_frees, bp, &where) != NULL) {
240 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp,
242 (void) printf("\tERROR: Duplicate FREE: %s\n", blkbuf);
244 zfs_btree_add_idx(&sv->sv_all_frees, bp, &where);
247 /* Check if the ALLOC has been freed */
248 if (zfs_btree_find(&sv->sv_pair, bp, &where) != NULL) {
249 zfs_btree_remove_idx(&sv->sv_pair, &where);
251 for (int i = 0; i < SPA_DVAS_PER_BP; i++) {
252 if (DVA_IS_EMPTY(&bp->blk_dva[i]))
254 sublivelist_verify_block_t svb = {
255 .svb_dva = bp->blk_dva[i],
256 .svb_allocated_txg = bp->blk_birth
259 if (zfs_btree_find(&sv->sv_leftover, &svb,
261 zfs_btree_add_idx(&sv->sv_leftover,
266 /* Check if the ALLOC is a duplicate */
267 if (zfs_btree_find(&sv->sv_all_allocs, bp, &where) != NULL) {
268 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp,
270 (void) printf("\tERROR: Duplicate ALLOC: %s\n", blkbuf);
272 zfs_btree_add_idx(&sv->sv_all_allocs, bp, &where);
279 sublivelist_verify_func(void *args, dsl_deadlist_entry_t *dle)
282 char blkbuf[BP_SPRINTF_LEN];
283 struct sublivelist_verify *sv = args;
285 zfs_btree_create(&sv->sv_all_allocs, livelist_compare,
288 zfs_btree_create(&sv->sv_all_frees, livelist_compare,
291 zfs_btree_create(&sv->sv_pair, livelist_compare,
294 err = bpobj_iterate_nofree(&dle->dle_bpobj, sublivelist_verify_blkptr,
297 zfs_btree_clear(&sv->sv_all_allocs);
298 zfs_btree_destroy(&sv->sv_all_allocs);
300 zfs_btree_clear(&sv->sv_all_frees);
301 zfs_btree_destroy(&sv->sv_all_frees);
304 zfs_btree_index_t *cookie = NULL;
305 while ((e = zfs_btree_destroy_nodes(&sv->sv_pair, &cookie)) != NULL) {
306 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), e, B_TRUE);
307 (void) printf("\tERROR: Unmatched FREE: %s\n", blkbuf);
309 zfs_btree_destroy(&sv->sv_pair);
315 livelist_block_compare(const void *larg, const void *rarg)
317 const sublivelist_verify_block_t *l = larg;
318 const sublivelist_verify_block_t *r = rarg;
320 if (DVA_GET_VDEV(&l->svb_dva) < DVA_GET_VDEV(&r->svb_dva))
322 else if (DVA_GET_VDEV(&l->svb_dva) > DVA_GET_VDEV(&r->svb_dva))
325 if (DVA_GET_OFFSET(&l->svb_dva) < DVA_GET_OFFSET(&r->svb_dva))
327 else if (DVA_GET_OFFSET(&l->svb_dva) > DVA_GET_OFFSET(&r->svb_dva))
330 if (DVA_GET_ASIZE(&l->svb_dva) < DVA_GET_ASIZE(&r->svb_dva))
332 else if (DVA_GET_ASIZE(&l->svb_dva) > DVA_GET_ASIZE(&r->svb_dva))
339 * Check for errors in a livelist while tracking all unfreed ALLOCs in the
340 * sublivelist_verify_t: sv->sv_leftover
343 livelist_verify(dsl_deadlist_t *dl, void *arg)
345 sublivelist_verify_t *sv = arg;
346 dsl_deadlist_iterate(dl, sublivelist_verify_func, sv);
350 * Check for errors in the livelist entry and discard the intermediary
355 sublivelist_verify_lightweight(void *args, dsl_deadlist_entry_t *dle)
357 sublivelist_verify_t sv;
358 zfs_btree_create(&sv.sv_leftover, livelist_block_compare,
359 sizeof (sublivelist_verify_block_t));
360 int err = sublivelist_verify_func(&sv, dle);
361 zfs_btree_clear(&sv.sv_leftover);
362 zfs_btree_destroy(&sv.sv_leftover);
366 typedef struct metaslab_verify {
368 * Tree containing all the leftover ALLOCs from the livelists
369 * that are part of this metaslab.
371 zfs_btree_t mv_livelist_allocs;
374 * Metaslab information.
382 * What's currently allocated for this metaslab.
384 range_tree_t *mv_allocated;
387 typedef void ll_iter_t(dsl_deadlist_t *ll, void *arg);
389 typedef int (*zdb_log_sm_cb_t)(spa_t *spa, space_map_entry_t *sme, uint64_t txg,
392 typedef struct unflushed_iter_cb_arg {
396 zdb_log_sm_cb_t uic_cb;
397 } unflushed_iter_cb_arg_t;
400 iterate_through_spacemap_logs_cb(space_map_entry_t *sme, void *arg)
402 unflushed_iter_cb_arg_t *uic = arg;
403 return (uic->uic_cb(uic->uic_spa, sme, uic->uic_txg, uic->uic_arg));
407 iterate_through_spacemap_logs(spa_t *spa, zdb_log_sm_cb_t cb, void *arg)
409 if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))
412 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
413 for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg);
414 sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls)) {
415 space_map_t *sm = NULL;
416 VERIFY0(space_map_open(&sm, spa_meta_objset(spa),
417 sls->sls_sm_obj, 0, UINT64_MAX, SPA_MINBLOCKSHIFT));
419 unflushed_iter_cb_arg_t uic = {
421 .uic_txg = sls->sls_txg,
425 VERIFY0(space_map_iterate(sm, space_map_length(sm),
426 iterate_through_spacemap_logs_cb, &uic));
429 spa_config_exit(spa, SCL_CONFIG, FTAG);
433 verify_livelist_allocs(metaslab_verify_t *mv, uint64_t txg,
434 uint64_t offset, uint64_t size)
436 sublivelist_verify_block_t svb;
437 DVA_SET_VDEV(&svb.svb_dva, mv->mv_vdid);
438 DVA_SET_OFFSET(&svb.svb_dva, offset);
439 DVA_SET_ASIZE(&svb.svb_dva, size);
440 zfs_btree_index_t where;
441 uint64_t end_offset = offset + size;
444 * Look for an exact match for spacemap entry in the livelist entries.
445 * Then, look for other livelist entries that fall within the range
446 * of the spacemap entry as it may have been condensed
448 sublivelist_verify_block_t *found =
449 zfs_btree_find(&mv->mv_livelist_allocs, &svb, &where);
451 found = zfs_btree_next(&mv->mv_livelist_allocs, &where, &where);
453 for (; found != NULL && DVA_GET_VDEV(&found->svb_dva) == mv->mv_vdid &&
454 DVA_GET_OFFSET(&found->svb_dva) < end_offset;
455 found = zfs_btree_next(&mv->mv_livelist_allocs, &where, &where)) {
456 if (found->svb_allocated_txg <= txg) {
457 (void) printf("ERROR: Livelist ALLOC [%llx:%llx] "
458 "from TXG %llx FREED at TXG %llx\n",
459 (u_longlong_t)DVA_GET_OFFSET(&found->svb_dva),
460 (u_longlong_t)DVA_GET_ASIZE(&found->svb_dva),
461 (u_longlong_t)found->svb_allocated_txg,
468 metaslab_spacemap_validation_cb(space_map_entry_t *sme, void *arg)
470 metaslab_verify_t *mv = arg;
471 uint64_t offset = sme->sme_offset;
472 uint64_t size = sme->sme_run;
473 uint64_t txg = sme->sme_txg;
475 if (sme->sme_type == SM_ALLOC) {
476 if (range_tree_contains(mv->mv_allocated,
478 (void) printf("ERROR: DOUBLE ALLOC: "
480 "%llu:%llu LOG_SM\n",
481 (u_longlong_t)txg, (u_longlong_t)offset,
482 (u_longlong_t)size, (u_longlong_t)mv->mv_vdid,
483 (u_longlong_t)mv->mv_msid);
485 range_tree_add(mv->mv_allocated,
489 if (!range_tree_contains(mv->mv_allocated,
491 (void) printf("ERROR: DOUBLE FREE: "
493 "%llu:%llu LOG_SM\n",
494 (u_longlong_t)txg, (u_longlong_t)offset,
495 (u_longlong_t)size, (u_longlong_t)mv->mv_vdid,
496 (u_longlong_t)mv->mv_msid);
498 range_tree_remove(mv->mv_allocated,
503 if (sme->sme_type != SM_ALLOC) {
505 * If something is freed in the spacemap, verify that
506 * it is not listed as allocated in the livelist.
508 verify_livelist_allocs(mv, txg, offset, size);
514 spacemap_check_sm_log_cb(spa_t *spa, space_map_entry_t *sme,
515 uint64_t txg, void *arg)
517 metaslab_verify_t *mv = arg;
518 uint64_t offset = sme->sme_offset;
519 uint64_t vdev_id = sme->sme_vdev;
521 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
523 /* skip indirect vdevs */
524 if (!vdev_is_concrete(vd))
527 if (vdev_id != mv->mv_vdid)
530 metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
531 if (ms->ms_id != mv->mv_msid)
534 if (txg < metaslab_unflushed_txg(ms))
538 ASSERT3U(txg, ==, sme->sme_txg);
539 return (metaslab_spacemap_validation_cb(sme, mv));
543 spacemap_check_sm_log(spa_t *spa, metaslab_verify_t *mv)
545 iterate_through_spacemap_logs(spa, spacemap_check_sm_log_cb, mv);
549 spacemap_check_ms_sm(space_map_t *sm, metaslab_verify_t *mv)
554 VERIFY0(space_map_iterate(sm, space_map_length(sm),
555 metaslab_spacemap_validation_cb, mv));
558 static void iterate_deleted_livelists(spa_t *spa, ll_iter_t func, void *arg);
561 * Transfer blocks from sv_leftover tree to the mv_livelist_allocs if
562 * they are part of that metaslab (mv_msid).
565 mv_populate_livelist_allocs(metaslab_verify_t *mv, sublivelist_verify_t *sv)
567 zfs_btree_index_t where;
568 sublivelist_verify_block_t *svb;
569 ASSERT3U(zfs_btree_numnodes(&mv->mv_livelist_allocs), ==, 0);
570 for (svb = zfs_btree_first(&sv->sv_leftover, &where);
572 svb = zfs_btree_next(&sv->sv_leftover, &where, &where)) {
573 if (DVA_GET_VDEV(&svb->svb_dva) != mv->mv_vdid)
576 if (DVA_GET_OFFSET(&svb->svb_dva) < mv->mv_start &&
577 (DVA_GET_OFFSET(&svb->svb_dva) +
578 DVA_GET_ASIZE(&svb->svb_dva)) > mv->mv_start) {
579 (void) printf("ERROR: Found block that crosses "
580 "metaslab boundary: <%llu:%llx:%llx>\n",
581 (u_longlong_t)DVA_GET_VDEV(&svb->svb_dva),
582 (u_longlong_t)DVA_GET_OFFSET(&svb->svb_dva),
583 (u_longlong_t)DVA_GET_ASIZE(&svb->svb_dva));
587 if (DVA_GET_OFFSET(&svb->svb_dva) < mv->mv_start)
590 if (DVA_GET_OFFSET(&svb->svb_dva) >= mv->mv_end)
593 if ((DVA_GET_OFFSET(&svb->svb_dva) +
594 DVA_GET_ASIZE(&svb->svb_dva)) > mv->mv_end) {
595 (void) printf("ERROR: Found block that crosses "
596 "metaslab boundary: <%llu:%llx:%llx>\n",
597 (u_longlong_t)DVA_GET_VDEV(&svb->svb_dva),
598 (u_longlong_t)DVA_GET_OFFSET(&svb->svb_dva),
599 (u_longlong_t)DVA_GET_ASIZE(&svb->svb_dva));
603 zfs_btree_add(&mv->mv_livelist_allocs, svb);
606 for (svb = zfs_btree_first(&mv->mv_livelist_allocs, &where);
608 svb = zfs_btree_next(&mv->mv_livelist_allocs, &where, &where)) {
609 zfs_btree_remove(&sv->sv_leftover, svb);
615 * Iterate through all the sublivelists and:
616 * - report leftover frees
617 * - report double ALLOCs/FREEs
618 * - record leftover ALLOCs together with their TXG [see Cross Check]
622 * - iterate over spacemap and then the metaslab's entries in the
623 * spacemap log, then report any double FREEs and ALLOCs (do not
627 * After finishing the Livelist Check phase and while being in the
628 * Spacemap Check phase, we find all the recorded leftover ALLOCs
629 * of the livelist check that are part of the metaslab that we are
630 * currently looking at in the Spacemap Check. We report any entries
631 * that are marked as ALLOCs in the livelists but have been actually
632 * freed (and potentially allocated again) after their TXG stamp in
633 * the spacemaps. Also report any ALLOCs from the livelists that
634 * belong to indirect vdevs (e.g. their vdev completed removal).
636 * Note that this will miss Log Spacemap entries that cancelled each other
637 * out before being flushed to the metaslab, so we are not guaranteed
638 * to match all erroneous ALLOCs.
641 livelist_metaslab_validate(spa_t *spa)
643 (void) printf("Verifying deleted livelist entries\n");
645 sublivelist_verify_t sv;
646 zfs_btree_create(&sv.sv_leftover, livelist_block_compare,
647 sizeof (sublivelist_verify_block_t));
648 iterate_deleted_livelists(spa, livelist_verify, &sv);
650 (void) printf("Verifying metaslab entries\n");
651 vdev_t *rvd = spa->spa_root_vdev;
652 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
653 vdev_t *vd = rvd->vdev_child[c];
655 if (!vdev_is_concrete(vd))
658 for (uint64_t mid = 0; mid < vd->vdev_ms_count; mid++) {
659 metaslab_t *m = vd->vdev_ms[mid];
661 (void) fprintf(stderr,
662 "\rverifying concrete vdev %llu, "
663 "metaslab %llu of %llu ...",
664 (longlong_t)vd->vdev_id,
666 (longlong_t)vd->vdev_ms_count);
668 uint64_t shift, start;
669 range_seg_type_t type =
670 metaslab_calculate_range_tree_type(vd, m,
672 metaslab_verify_t mv;
673 mv.mv_allocated = range_tree_create(NULL,
674 type, NULL, start, shift);
675 mv.mv_vdid = vd->vdev_id;
676 mv.mv_msid = m->ms_id;
677 mv.mv_start = m->ms_start;
678 mv.mv_end = m->ms_start + m->ms_size;
679 zfs_btree_create(&mv.mv_livelist_allocs,
680 livelist_block_compare,
681 sizeof (sublivelist_verify_block_t));
683 mv_populate_livelist_allocs(&mv, &sv);
685 spacemap_check_ms_sm(m->ms_sm, &mv);
686 spacemap_check_sm_log(spa, &mv);
688 range_tree_vacate(mv.mv_allocated, NULL, NULL);
689 range_tree_destroy(mv.mv_allocated);
690 zfs_btree_clear(&mv.mv_livelist_allocs);
691 zfs_btree_destroy(&mv.mv_livelist_allocs);
694 (void) fprintf(stderr, "\n");
697 * If there are any segments in the leftover tree after we walked
698 * through all the metaslabs in the concrete vdevs then this means
699 * that we have segments in the livelists that belong to indirect
700 * vdevs and are marked as allocated.
702 if (zfs_btree_numnodes(&sv.sv_leftover) == 0) {
703 zfs_btree_destroy(&sv.sv_leftover);
706 (void) printf("ERROR: Found livelist blocks marked as allocated "
707 "for indirect vdevs:\n");
709 zfs_btree_index_t *where = NULL;
710 sublivelist_verify_block_t *svb;
711 while ((svb = zfs_btree_destroy_nodes(&sv.sv_leftover, &where)) !=
713 int vdev_id = DVA_GET_VDEV(&svb->svb_dva);
714 ASSERT3U(vdev_id, <, rvd->vdev_children);
715 vdev_t *vd = rvd->vdev_child[vdev_id];
716 ASSERT(!vdev_is_concrete(vd));
717 (void) printf("<%d:%llx:%llx> TXG %llx\n",
718 vdev_id, (u_longlong_t)DVA_GET_OFFSET(&svb->svb_dva),
719 (u_longlong_t)DVA_GET_ASIZE(&svb->svb_dva),
720 (u_longlong_t)svb->svb_allocated_txg);
723 zfs_btree_destroy(&sv.sv_leftover);
727 * These libumem hooks provide a reasonable set of defaults for the allocator's
728 * debugging facilities.
731 _umem_debug_init(void)
733 return ("default,verbose"); /* $UMEM_DEBUG setting */
737 _umem_logging_init(void)
739 return ("fail,contents"); /* $UMEM_LOGGING setting */
745 (void) fprintf(stderr,
746 "Usage:\t%s [-AbcdDFGhikLMPsvXy] [-e [-V] [-p <path> ...]] "
747 "[-I <inflight I/Os>]\n"
748 "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
749 "\t\t[<poolname>[/<dataset | objset id>] [<object | range> ...]]\n"
750 "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
751 "\t\t[<poolname>[/<dataset | objset id>] [<object | range> ...]\n"
752 "\t%s [-v] <bookmark>\n"
753 "\t%s -C [-A] [-U <cache>]\n"
754 "\t%s -l [-Aqu] <device>\n"
755 "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] "
756 "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n"
757 "\t%s -O <dataset> <path>\n"
758 "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
759 "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n"
760 "\t%s -E [-A] word0:word1:...:word15\n"
761 "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] "
763 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname,
764 cmdname, cmdname, cmdname);
766 (void) fprintf(stderr, " Dataset name must include at least one "
767 "separator character '/' or '@'\n");
768 (void) fprintf(stderr, " If dataset name is specified, only that "
769 "dataset is dumped\n");
770 (void) fprintf(stderr, " If object numbers or object number "
771 "ranges are specified, only those\n"
772 " objects or ranges are dumped.\n\n");
773 (void) fprintf(stderr,
774 " Object ranges take the form <start>:<end>[:<flags>]\n"
775 " start Starting object number\n"
776 " end Ending object number, or -1 for no upper bound\n"
777 " flags Optional flags to select object types:\n"
778 " A All objects (this is the default)\n"
779 " d ZFS directories\n"
781 " m SPA space maps\n"
783 " - Negate effect of next flag\n\n");
784 (void) fprintf(stderr, " Options to control amount of output:\n");
785 (void) fprintf(stderr, " -b block statistics\n");
786 (void) fprintf(stderr, " -c checksum all metadata (twice for "
787 "all data) blocks\n");
788 (void) fprintf(stderr, " -C config (or cachefile if alone)\n");
789 (void) fprintf(stderr, " -d dataset(s)\n");
790 (void) fprintf(stderr, " -D dedup statistics\n");
791 (void) fprintf(stderr, " -E decode and display block from an "
792 "embedded block pointer\n");
793 (void) fprintf(stderr, " -h pool history\n");
794 (void) fprintf(stderr, " -i intent logs\n");
795 (void) fprintf(stderr, " -l read label contents\n");
796 (void) fprintf(stderr, " -k examine the checkpointed state "
798 (void) fprintf(stderr, " -L disable leak tracking (do not "
799 "load spacemaps)\n");
800 (void) fprintf(stderr, " -m metaslabs\n");
801 (void) fprintf(stderr, " -M metaslab groups\n");
802 (void) fprintf(stderr, " -O perform object lookups by path\n");
803 (void) fprintf(stderr, " -R read and display block from a "
805 (void) fprintf(stderr, " -s report stats on zdb's I/O\n");
806 (void) fprintf(stderr, " -S simulate dedup to measure effect\n");
807 (void) fprintf(stderr, " -v verbose (applies to all "
809 (void) fprintf(stderr, " -y perform livelist and metaslab "
810 "validation on any livelists being deleted\n\n");
811 (void) fprintf(stderr, " Below options are intended for use "
812 "with other options:\n");
813 (void) fprintf(stderr, " -A ignore assertions (-A), enable "
814 "panic recovery (-AA) or both (-AAA)\n");
815 (void) fprintf(stderr, " -e pool is exported/destroyed/"
816 "has altroot/not in a cachefile\n");
817 (void) fprintf(stderr, " -F attempt automatic rewind within "
818 "safe range of transaction groups\n");
819 (void) fprintf(stderr, " -G dump zfs_dbgmsg buffer before "
821 (void) fprintf(stderr, " -I <number of inflight I/Os> -- "
822 "specify the maximum number of\n "
823 "checksumming I/Os [default is 200]\n");
824 (void) fprintf(stderr, " -o <variable>=<value> set global "
825 "variable to an unsigned 32-bit integer\n");
826 (void) fprintf(stderr, " -p <path> -- use one or more with "
827 "-e to specify path to vdev dir\n");
828 (void) fprintf(stderr, " -P print numbers in parseable form\n");
829 (void) fprintf(stderr, " -q don't print label contents\n");
830 (void) fprintf(stderr, " -t <txg> -- highest txg to use when "
831 "searching for uberblocks\n");
832 (void) fprintf(stderr, " -u uberblock\n");
833 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate "
835 (void) fprintf(stderr, " -V do verbatim import\n");
836 (void) fprintf(stderr, " -x <dumpdir> -- "
837 "dump all read blocks into specified directory\n");
838 (void) fprintf(stderr, " -X attempt extreme rewind (does not "
839 "work with dataset)\n");
840 (void) fprintf(stderr, " -Y attempt all reconstruction "
841 "combinations for split blocks\n");
842 (void) fprintf(stderr, " -Z show ZSTD headers \n");
843 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
844 "to make only that option verbose\n");
845 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
850 dump_debug_buffer(void)
854 (void) fflush(stdout);
855 zfs_dbgmsg_print("zdb");
860 * Called for usage errors that are discovered after a call to spa_open(),
861 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors.
865 fatal(const char *fmt, ...)
870 (void) fprintf(stderr, "%s: ", cmdname);
871 (void) vfprintf(stderr, fmt, ap);
873 (void) fprintf(stderr, "\n");
882 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
885 size_t nvsize = *(uint64_t *)data;
886 char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
888 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
890 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
892 umem_free(packed, nvsize);
901 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
903 spa_history_phys_t *shp = data;
908 (void) printf("\t\tpool_create_len = %llu\n",
909 (u_longlong_t)shp->sh_pool_create_len);
910 (void) printf("\t\tphys_max_off = %llu\n",
911 (u_longlong_t)shp->sh_phys_max_off);
912 (void) printf("\t\tbof = %llu\n",
913 (u_longlong_t)shp->sh_bof);
914 (void) printf("\t\teof = %llu\n",
915 (u_longlong_t)shp->sh_eof);
916 (void) printf("\t\trecords_lost = %llu\n",
917 (u_longlong_t)shp->sh_records_lost);
921 zdb_nicenum(uint64_t num, char *buf, size_t buflen)
924 (void) snprintf(buf, buflen, "%llu", (longlong_t)num);
926 nicenum(num, buf, sizeof (buf));
929 static const char histo_stars[] = "****************************************";
930 static const uint64_t histo_width = sizeof (histo_stars) - 1;
933 dump_histogram(const uint64_t *histo, int size, int offset)
936 int minidx = size - 1;
940 for (i = 0; i < size; i++) {
943 if (histo[i] > 0 && i > maxidx)
945 if (histo[i] > 0 && i < minidx)
949 if (max < histo_width)
952 for (i = minidx; i <= maxidx; i++) {
953 (void) printf("\t\t\t%3u: %6llu %s\n",
954 i + offset, (u_longlong_t)histo[i],
955 &histo_stars[(max - histo[i]) * histo_width / max]);
960 dump_zap_stats(objset_t *os, uint64_t object)
965 error = zap_get_stats(os, object, &zs);
969 if (zs.zs_ptrtbl_len == 0) {
970 ASSERT(zs.zs_num_blocks == 1);
971 (void) printf("\tmicrozap: %llu bytes, %llu entries\n",
972 (u_longlong_t)zs.zs_blocksize,
973 (u_longlong_t)zs.zs_num_entries);
977 (void) printf("\tFat ZAP stats:\n");
979 (void) printf("\t\tPointer table:\n");
980 (void) printf("\t\t\t%llu elements\n",
981 (u_longlong_t)zs.zs_ptrtbl_len);
982 (void) printf("\t\t\tzt_blk: %llu\n",
983 (u_longlong_t)zs.zs_ptrtbl_zt_blk);
984 (void) printf("\t\t\tzt_numblks: %llu\n",
985 (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
986 (void) printf("\t\t\tzt_shift: %llu\n",
987 (u_longlong_t)zs.zs_ptrtbl_zt_shift);
988 (void) printf("\t\t\tzt_blks_copied: %llu\n",
989 (u_longlong_t)zs.zs_ptrtbl_blks_copied);
990 (void) printf("\t\t\tzt_nextblk: %llu\n",
991 (u_longlong_t)zs.zs_ptrtbl_nextblk);
993 (void) printf("\t\tZAP entries: %llu\n",
994 (u_longlong_t)zs.zs_num_entries);
995 (void) printf("\t\tLeaf blocks: %llu\n",
996 (u_longlong_t)zs.zs_num_leafs);
997 (void) printf("\t\tTotal blocks: %llu\n",
998 (u_longlong_t)zs.zs_num_blocks);
999 (void) printf("\t\tzap_block_type: 0x%llx\n",
1000 (u_longlong_t)zs.zs_block_type);
1001 (void) printf("\t\tzap_magic: 0x%llx\n",
1002 (u_longlong_t)zs.zs_magic);
1003 (void) printf("\t\tzap_salt: 0x%llx\n",
1004 (u_longlong_t)zs.zs_salt);
1006 (void) printf("\t\tLeafs with 2^n pointers:\n");
1007 dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
1009 (void) printf("\t\tBlocks with n*5 entries:\n");
1010 dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
1012 (void) printf("\t\tBlocks n/10 full:\n");
1013 dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
1015 (void) printf("\t\tEntries with n chunks:\n");
1016 dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
1018 (void) printf("\t\tBuckets with n entries:\n");
1019 dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
1024 dump_none(objset_t *os, uint64_t object, void *data, size_t size)
1030 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
1032 (void) printf("\tUNKNOWN OBJECT TYPE\n");
1037 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
1043 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
1047 if (dump_opt['d'] < 6)
1051 dmu_object_info_t doi;
1053 VERIFY0(dmu_object_info(os, object, &doi));
1054 size = doi.doi_max_offset;
1056 * We cap the size at 1 mebibyte here to prevent
1057 * allocation failures and nigh-infinite printing if the
1058 * object is extremely large.
1060 oursize = MIN(size, 1 << 20);
1061 arr = kmem_alloc(oursize, KM_SLEEP);
1063 int err = dmu_read(os, object, 0, oursize, arr, 0);
1065 (void) printf("got error %u from dmu_read\n", err);
1066 kmem_free(arr, oursize);
1071 * Even though the allocation is already done in this code path,
1072 * we still cap the size to prevent excessive printing.
1074 oursize = MIN(size, 1 << 20);
1079 (void) printf("\t\t[]\n");
1083 (void) printf("\t\t[%0llx", (u_longlong_t)arr[0]);
1084 for (size_t i = 1; i * sizeof (uint64_t) < oursize; i++) {
1086 (void) printf(", %0llx", (u_longlong_t)arr[i]);
1088 (void) printf(",\n\t\t%0llx", (u_longlong_t)arr[i]);
1090 if (oursize != size)
1091 (void) printf(", ... ");
1092 (void) printf("]\n");
1095 kmem_free(arr, oursize);
1100 dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
1103 zap_attribute_t attr;
1107 dump_zap_stats(os, object);
1108 (void) printf("\n");
1110 for (zap_cursor_init(&zc, os, object);
1111 zap_cursor_retrieve(&zc, &attr) == 0;
1112 zap_cursor_advance(&zc)) {
1113 (void) printf("\t\t%s = ", attr.za_name);
1114 if (attr.za_num_integers == 0) {
1115 (void) printf("\n");
1118 prop = umem_zalloc(attr.za_num_integers *
1119 attr.za_integer_length, UMEM_NOFAIL);
1120 (void) zap_lookup(os, object, attr.za_name,
1121 attr.za_integer_length, attr.za_num_integers, prop);
1122 if (attr.za_integer_length == 1) {
1123 if (strcmp(attr.za_name,
1124 DSL_CRYPTO_KEY_MASTER_KEY) == 0 ||
1125 strcmp(attr.za_name,
1126 DSL_CRYPTO_KEY_HMAC_KEY) == 0 ||
1127 strcmp(attr.za_name, DSL_CRYPTO_KEY_IV) == 0 ||
1128 strcmp(attr.za_name, DSL_CRYPTO_KEY_MAC) == 0 ||
1129 strcmp(attr.za_name, DMU_POOL_CHECKSUM_SALT) == 0) {
1132 for (i = 0; i < attr.za_num_integers; i++) {
1133 (void) printf("%02x", u8[i]);
1136 (void) printf("%s", (char *)prop);
1139 for (i = 0; i < attr.za_num_integers; i++) {
1140 switch (attr.za_integer_length) {
1142 (void) printf("%u ",
1143 ((uint16_t *)prop)[i]);
1146 (void) printf("%u ",
1147 ((uint32_t *)prop)[i]);
1150 (void) printf("%lld ",
1151 (u_longlong_t)((int64_t *)prop)[i]);
1156 (void) printf("\n");
1157 umem_free(prop, attr.za_num_integers * attr.za_integer_length);
1159 zap_cursor_fini(&zc);
1163 dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
1165 bpobj_phys_t *bpop = data;
1167 char bytes[32], comp[32], uncomp[32];
1169 /* make sure the output won't get truncated */
1170 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1171 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1172 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1177 zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
1178 zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
1179 zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
1181 (void) printf("\t\tnum_blkptrs = %llu\n",
1182 (u_longlong_t)bpop->bpo_num_blkptrs);
1183 (void) printf("\t\tbytes = %s\n", bytes);
1184 if (size >= BPOBJ_SIZE_V1) {
1185 (void) printf("\t\tcomp = %s\n", comp);
1186 (void) printf("\t\tuncomp = %s\n", uncomp);
1188 if (size >= BPOBJ_SIZE_V2) {
1189 (void) printf("\t\tsubobjs = %llu\n",
1190 (u_longlong_t)bpop->bpo_subobjs);
1191 (void) printf("\t\tnum_subobjs = %llu\n",
1192 (u_longlong_t)bpop->bpo_num_subobjs);
1194 if (size >= sizeof (*bpop)) {
1195 (void) printf("\t\tnum_freed = %llu\n",
1196 (u_longlong_t)bpop->bpo_num_freed);
1199 if (dump_opt['d'] < 5)
1202 for (i = 0; i < bpop->bpo_num_blkptrs; i++) {
1203 char blkbuf[BP_SPRINTF_LEN];
1206 int err = dmu_read(os, object,
1207 i * sizeof (bp), sizeof (bp), &bp, 0);
1209 (void) printf("got error %u from dmu_read\n", err);
1212 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp,
1214 (void) printf("\t%s\n", blkbuf);
1220 dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
1222 dmu_object_info_t doi;
1225 VERIFY0(dmu_object_info(os, object, &doi));
1226 uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
1228 int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
1230 (void) printf("got error %u from dmu_read\n", err);
1231 kmem_free(subobjs, doi.doi_max_offset);
1235 int64_t last_nonzero = -1;
1236 for (i = 0; i < doi.doi_max_offset / 8; i++) {
1237 if (subobjs[i] != 0)
1241 for (i = 0; i <= last_nonzero; i++) {
1242 (void) printf("\t%llu\n", (u_longlong_t)subobjs[i]);
1244 kmem_free(subobjs, doi.doi_max_offset);
1249 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
1251 dump_zap_stats(os, object);
1252 /* contents are printed elsewhere, properly decoded */
1257 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
1260 zap_attribute_t attr;
1262 dump_zap_stats(os, object);
1263 (void) printf("\n");
1265 for (zap_cursor_init(&zc, os, object);
1266 zap_cursor_retrieve(&zc, &attr) == 0;
1267 zap_cursor_advance(&zc)) {
1268 (void) printf("\t\t%s = ", attr.za_name);
1269 if (attr.za_num_integers == 0) {
1270 (void) printf("\n");
1273 (void) printf(" %llx : [%d:%d:%d]\n",
1274 (u_longlong_t)attr.za_first_integer,
1275 (int)ATTR_LENGTH(attr.za_first_integer),
1276 (int)ATTR_BSWAP(attr.za_first_integer),
1277 (int)ATTR_NUM(attr.za_first_integer));
1279 zap_cursor_fini(&zc);
1284 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
1287 zap_attribute_t attr;
1288 uint16_t *layout_attrs;
1291 dump_zap_stats(os, object);
1292 (void) printf("\n");
1294 for (zap_cursor_init(&zc, os, object);
1295 zap_cursor_retrieve(&zc, &attr) == 0;
1296 zap_cursor_advance(&zc)) {
1297 (void) printf("\t\t%s = [", attr.za_name);
1298 if (attr.za_num_integers == 0) {
1299 (void) printf("\n");
1303 VERIFY(attr.za_integer_length == 2);
1304 layout_attrs = umem_zalloc(attr.za_num_integers *
1305 attr.za_integer_length, UMEM_NOFAIL);
1307 VERIFY(zap_lookup(os, object, attr.za_name,
1308 attr.za_integer_length,
1309 attr.za_num_integers, layout_attrs) == 0);
1311 for (i = 0; i != attr.za_num_integers; i++)
1312 (void) printf(" %d ", (int)layout_attrs[i]);
1313 (void) printf("]\n");
1314 umem_free(layout_attrs,
1315 attr.za_num_integers * attr.za_integer_length);
1317 zap_cursor_fini(&zc);
1322 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
1325 zap_attribute_t attr;
1326 const char *typenames[] = {
1327 /* 0 */ "not specified",
1329 /* 2 */ "Character Device",
1330 /* 3 */ "3 (invalid)",
1331 /* 4 */ "Directory",
1332 /* 5 */ "5 (invalid)",
1333 /* 6 */ "Block Device",
1334 /* 7 */ "7 (invalid)",
1335 /* 8 */ "Regular File",
1336 /* 9 */ "9 (invalid)",
1337 /* 10 */ "Symbolic Link",
1338 /* 11 */ "11 (invalid)",
1341 /* 14 */ "Event Port",
1342 /* 15 */ "15 (invalid)",
1345 dump_zap_stats(os, object);
1346 (void) printf("\n");
1348 for (zap_cursor_init(&zc, os, object);
1349 zap_cursor_retrieve(&zc, &attr) == 0;
1350 zap_cursor_advance(&zc)) {
1351 (void) printf("\t\t%s = %lld (type: %s)\n",
1352 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
1353 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
1355 zap_cursor_fini(&zc);
1359 get_dtl_refcount(vdev_t *vd)
1363 if (vd->vdev_ops->vdev_op_leaf) {
1364 space_map_t *sm = vd->vdev_dtl_sm;
1367 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
1372 for (unsigned c = 0; c < vd->vdev_children; c++)
1373 refcount += get_dtl_refcount(vd->vdev_child[c]);
1378 get_metaslab_refcount(vdev_t *vd)
1382 if (vd->vdev_top == vd) {
1383 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
1384 space_map_t *sm = vd->vdev_ms[m]->ms_sm;
1387 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
1391 for (unsigned c = 0; c < vd->vdev_children; c++)
1392 refcount += get_metaslab_refcount(vd->vdev_child[c]);
1398 get_obsolete_refcount(vdev_t *vd)
1400 uint64_t obsolete_sm_object;
1403 VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object));
1404 if (vd->vdev_top == vd && obsolete_sm_object != 0) {
1405 dmu_object_info_t doi;
1406 VERIFY0(dmu_object_info(vd->vdev_spa->spa_meta_objset,
1407 obsolete_sm_object, &doi));
1408 if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
1412 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
1413 ASSERT3U(obsolete_sm_object, ==, 0);
1415 for (unsigned c = 0; c < vd->vdev_children; c++) {
1416 refcount += get_obsolete_refcount(vd->vdev_child[c]);
1423 get_prev_obsolete_spacemap_refcount(spa_t *spa)
1426 spa->spa_condensing_indirect_phys.scip_prev_obsolete_sm_object;
1427 if (prev_obj != 0) {
1428 dmu_object_info_t doi;
1429 VERIFY0(dmu_object_info(spa->spa_meta_objset, prev_obj, &doi));
1430 if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
1438 get_checkpoint_refcount(vdev_t *vd)
1442 if (vd->vdev_top == vd && vd->vdev_top_zap != 0 &&
1443 zap_contains(spa_meta_objset(vd->vdev_spa),
1444 vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) == 0)
1447 for (uint64_t c = 0; c < vd->vdev_children; c++)
1448 refcount += get_checkpoint_refcount(vd->vdev_child[c]);
1454 get_log_spacemap_refcount(spa_t *spa)
1456 return (avl_numnodes(&spa->spa_sm_logs_by_txg));
1460 verify_spacemap_refcounts(spa_t *spa)
1462 uint64_t expected_refcount = 0;
1463 uint64_t actual_refcount;
1465 (void) feature_get_refcount(spa,
1466 &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
1467 &expected_refcount);
1468 actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
1469 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
1470 actual_refcount += get_obsolete_refcount(spa->spa_root_vdev);
1471 actual_refcount += get_prev_obsolete_spacemap_refcount(spa);
1472 actual_refcount += get_checkpoint_refcount(spa->spa_root_vdev);
1473 actual_refcount += get_log_spacemap_refcount(spa);
1475 if (expected_refcount != actual_refcount) {
1476 (void) printf("space map refcount mismatch: expected %lld != "
1478 (longlong_t)expected_refcount,
1479 (longlong_t)actual_refcount);
1486 dump_spacemap(objset_t *os, space_map_t *sm)
1488 const char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
1489 "INVALID", "INVALID", "INVALID", "INVALID" };
1494 (void) printf("space map object %llu:\n",
1495 (longlong_t)sm->sm_object);
1496 (void) printf(" smp_length = 0x%llx\n",
1497 (longlong_t)sm->sm_phys->smp_length);
1498 (void) printf(" smp_alloc = 0x%llx\n",
1499 (longlong_t)sm->sm_phys->smp_alloc);
1501 if (dump_opt['d'] < 6 && dump_opt['m'] < 4)
1505 * Print out the freelist entries in both encoded and decoded form.
1507 uint8_t mapshift = sm->sm_shift;
1509 uint64_t word, entry_id = 0;
1510 for (uint64_t offset = 0; offset < space_map_length(sm);
1511 offset += sizeof (word)) {
1513 VERIFY0(dmu_read(os, space_map_object(sm), offset,
1514 sizeof (word), &word, DMU_READ_PREFETCH));
1516 if (sm_entry_is_debug(word)) {
1517 uint64_t de_txg = SM_DEBUG_TXG_DECODE(word);
1518 uint64_t de_sync_pass = SM_DEBUG_SYNCPASS_DECODE(word);
1521 "\t [%6llu] PADDING\n",
1522 (u_longlong_t)entry_id);
1525 "\t [%6llu] %s: txg %llu pass %llu\n",
1526 (u_longlong_t)entry_id,
1527 ddata[SM_DEBUG_ACTION_DECODE(word)],
1528 (u_longlong_t)de_txg,
1529 (u_longlong_t)de_sync_pass);
1537 uint64_t entry_off, entry_run, entry_vdev = SM_NO_VDEVID;
1539 if (sm_entry_is_single_word(word)) {
1540 entry_type = (SM_TYPE_DECODE(word) == SM_ALLOC) ?
1542 entry_off = (SM_OFFSET_DECODE(word) << mapshift) +
1544 entry_run = SM_RUN_DECODE(word) << mapshift;
1547 /* it is a two-word entry so we read another word */
1548 ASSERT(sm_entry_is_double_word(word));
1550 uint64_t extra_word;
1551 offset += sizeof (extra_word);
1552 VERIFY0(dmu_read(os, space_map_object(sm), offset,
1553 sizeof (extra_word), &extra_word,
1554 DMU_READ_PREFETCH));
1556 ASSERT3U(offset, <=, space_map_length(sm));
1558 entry_run = SM2_RUN_DECODE(word) << mapshift;
1559 entry_vdev = SM2_VDEV_DECODE(word);
1560 entry_type = (SM2_TYPE_DECODE(extra_word) == SM_ALLOC) ?
1562 entry_off = (SM2_OFFSET_DECODE(extra_word) <<
1563 mapshift) + sm->sm_start;
1567 (void) printf("\t [%6llu] %c range:"
1568 " %010llx-%010llx size: %06llx vdev: %06llu words: %u\n",
1569 (u_longlong_t)entry_id,
1570 entry_type, (u_longlong_t)entry_off,
1571 (u_longlong_t)(entry_off + entry_run),
1572 (u_longlong_t)entry_run,
1573 (u_longlong_t)entry_vdev, words);
1575 if (entry_type == 'A')
1581 if (alloc != space_map_allocated(sm)) {
1582 (void) printf("space_map_object alloc (%lld) INCONSISTENT "
1583 "with space map summary (%lld)\n",
1584 (longlong_t)space_map_allocated(sm), (longlong_t)alloc);
1589 dump_metaslab_stats(metaslab_t *msp)
1592 range_tree_t *rt = msp->ms_allocatable;
1593 zfs_btree_t *t = &msp->ms_allocatable_by_size;
1594 int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
1596 /* max sure nicenum has enough space */
1597 CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ);
1599 zdb_nicenum(metaslab_largest_allocatable(msp), maxbuf, sizeof (maxbuf));
1601 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n",
1602 "segments", zfs_btree_numnodes(t), "maxsize", maxbuf,
1603 "freepct", free_pct);
1604 (void) printf("\tIn-memory histogram:\n");
1605 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
1609 dump_metaslab(metaslab_t *msp)
1611 vdev_t *vd = msp->ms_group->mg_vd;
1612 spa_t *spa = vd->vdev_spa;
1613 space_map_t *sm = msp->ms_sm;
1616 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf,
1620 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n",
1621 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
1622 (u_longlong_t)space_map_object(sm), freebuf);
1624 if (dump_opt['m'] > 2 && !dump_opt['L']) {
1625 mutex_enter(&msp->ms_lock);
1626 VERIFY0(metaslab_load(msp));
1627 range_tree_stat_verify(msp->ms_allocatable);
1628 dump_metaslab_stats(msp);
1629 metaslab_unload(msp);
1630 mutex_exit(&msp->ms_lock);
1633 if (dump_opt['m'] > 1 && sm != NULL &&
1634 spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
1636 * The space map histogram represents free space in chunks
1637 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
1639 (void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
1640 (u_longlong_t)msp->ms_fragmentation);
1641 dump_histogram(sm->sm_phys->smp_histogram,
1642 SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
1645 ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
1646 dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
1648 if (spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) {
1649 (void) printf("\tFlush data:\n\tunflushed txg=%llu\n\n",
1650 (u_longlong_t)metaslab_unflushed_txg(msp));
1655 print_vdev_metaslab_header(vdev_t *vd)
1657 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
1658 const char *bias_str = "";
1659 if (alloc_bias == VDEV_BIAS_LOG || vd->vdev_islog) {
1660 bias_str = VDEV_ALLOC_BIAS_LOG;
1661 } else if (alloc_bias == VDEV_BIAS_SPECIAL) {
1662 bias_str = VDEV_ALLOC_BIAS_SPECIAL;
1663 } else if (alloc_bias == VDEV_BIAS_DEDUP) {
1664 bias_str = VDEV_ALLOC_BIAS_DEDUP;
1667 uint64_t ms_flush_data_obj = 0;
1668 if (vd->vdev_top_zap != 0) {
1669 int error = zap_lookup(spa_meta_objset(vd->vdev_spa),
1670 vd->vdev_top_zap, VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS,
1671 sizeof (uint64_t), 1, &ms_flush_data_obj);
1672 if (error != ENOENT) {
1677 (void) printf("\tvdev %10llu %s",
1678 (u_longlong_t)vd->vdev_id, bias_str);
1680 if (ms_flush_data_obj != 0) {
1681 (void) printf(" ms_unflushed_phys object %llu",
1682 (u_longlong_t)ms_flush_data_obj);
1685 (void) printf("\n\t%-10s%5llu %-19s %-15s %-12s\n",
1686 "metaslabs", (u_longlong_t)vd->vdev_ms_count,
1687 "offset", "spacemap", "free");
1688 (void) printf("\t%15s %19s %15s %12s\n",
1689 "---------------", "-------------------",
1690 "---------------", "------------");
1694 dump_metaslab_groups(spa_t *spa)
1696 vdev_t *rvd = spa->spa_root_vdev;
1697 metaslab_class_t *mc = spa_normal_class(spa);
1698 uint64_t fragmentation;
1700 metaslab_class_histogram_verify(mc);
1702 for (unsigned c = 0; c < rvd->vdev_children; c++) {
1703 vdev_t *tvd = rvd->vdev_child[c];
1704 metaslab_group_t *mg = tvd->vdev_mg;
1706 if (mg == NULL || mg->mg_class != mc)
1709 metaslab_group_histogram_verify(mg);
1710 mg->mg_fragmentation = metaslab_group_fragmentation(mg);
1712 (void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
1714 (u_longlong_t)tvd->vdev_id,
1715 (u_longlong_t)tvd->vdev_ms_count);
1716 if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
1717 (void) printf("%3s\n", "-");
1719 (void) printf("%3llu%%\n",
1720 (u_longlong_t)mg->mg_fragmentation);
1722 dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
1725 (void) printf("\tpool %s\tfragmentation", spa_name(spa));
1726 fragmentation = metaslab_class_fragmentation(mc);
1727 if (fragmentation == ZFS_FRAG_INVALID)
1728 (void) printf("\t%3s\n", "-");
1730 (void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
1731 dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
1735 print_vdev_indirect(vdev_t *vd)
1737 vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
1738 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
1739 vdev_indirect_births_t *vib = vd->vdev_indirect_births;
1742 ASSERT3P(vib, ==, NULL);
1746 ASSERT3U(vdev_indirect_mapping_object(vim), ==,
1747 vic->vic_mapping_object);
1748 ASSERT3U(vdev_indirect_births_object(vib), ==,
1749 vic->vic_births_object);
1751 (void) printf("indirect births obj %llu:\n",
1752 (longlong_t)vic->vic_births_object);
1753 (void) printf(" vib_count = %llu\n",
1754 (longlong_t)vdev_indirect_births_count(vib));
1755 for (uint64_t i = 0; i < vdev_indirect_births_count(vib); i++) {
1756 vdev_indirect_birth_entry_phys_t *cur_vibe =
1757 &vib->vib_entries[i];
1758 (void) printf("\toffset %llx -> txg %llu\n",
1759 (longlong_t)cur_vibe->vibe_offset,
1760 (longlong_t)cur_vibe->vibe_phys_birth_txg);
1762 (void) printf("\n");
1764 (void) printf("indirect mapping obj %llu:\n",
1765 (longlong_t)vic->vic_mapping_object);
1766 (void) printf(" vim_max_offset = 0x%llx\n",
1767 (longlong_t)vdev_indirect_mapping_max_offset(vim));
1768 (void) printf(" vim_bytes_mapped = 0x%llx\n",
1769 (longlong_t)vdev_indirect_mapping_bytes_mapped(vim));
1770 (void) printf(" vim_count = %llu\n",
1771 (longlong_t)vdev_indirect_mapping_num_entries(vim));
1773 if (dump_opt['d'] <= 5 && dump_opt['m'] <= 3)
1776 uint32_t *counts = vdev_indirect_mapping_load_obsolete_counts(vim);
1778 for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
1779 vdev_indirect_mapping_entry_phys_t *vimep =
1780 &vim->vim_entries[i];
1781 (void) printf("\t<%llx:%llx:%llx> -> "
1782 "<%llx:%llx:%llx> (%x obsolete)\n",
1783 (longlong_t)vd->vdev_id,
1784 (longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
1785 (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
1786 (longlong_t)DVA_GET_VDEV(&vimep->vimep_dst),
1787 (longlong_t)DVA_GET_OFFSET(&vimep->vimep_dst),
1788 (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
1791 (void) printf("\n");
1793 uint64_t obsolete_sm_object;
1794 VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object));
1795 if (obsolete_sm_object != 0) {
1796 objset_t *mos = vd->vdev_spa->spa_meta_objset;
1797 (void) printf("obsolete space map object %llu:\n",
1798 (u_longlong_t)obsolete_sm_object);
1799 ASSERT(vd->vdev_obsolete_sm != NULL);
1800 ASSERT3U(space_map_object(vd->vdev_obsolete_sm), ==,
1801 obsolete_sm_object);
1802 dump_spacemap(mos, vd->vdev_obsolete_sm);
1803 (void) printf("\n");
1808 dump_metaslabs(spa_t *spa)
1810 vdev_t *vd, *rvd = spa->spa_root_vdev;
1811 uint64_t m, c = 0, children = rvd->vdev_children;
1813 (void) printf("\nMetaslabs:\n");
1815 if (!dump_opt['d'] && zopt_metaslab_args > 0) {
1816 c = zopt_metaslab[0];
1819 (void) fatal("bad vdev id: %llu", (u_longlong_t)c);
1821 if (zopt_metaslab_args > 1) {
1822 vd = rvd->vdev_child[c];
1823 print_vdev_metaslab_header(vd);
1825 for (m = 1; m < zopt_metaslab_args; m++) {
1826 if (zopt_metaslab[m] < vd->vdev_ms_count)
1828 vd->vdev_ms[zopt_metaslab[m]]);
1830 (void) fprintf(stderr, "bad metaslab "
1832 (u_longlong_t)zopt_metaslab[m]);
1834 (void) printf("\n");
1839 for (; c < children; c++) {
1840 vd = rvd->vdev_child[c];
1841 print_vdev_metaslab_header(vd);
1843 print_vdev_indirect(vd);
1845 for (m = 0; m < vd->vdev_ms_count; m++)
1846 dump_metaslab(vd->vdev_ms[m]);
1847 (void) printf("\n");
1852 dump_log_spacemaps(spa_t *spa)
1854 if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))
1857 (void) printf("\nLog Space Maps in Pool:\n");
1858 for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg);
1859 sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls)) {
1860 space_map_t *sm = NULL;
1861 VERIFY0(space_map_open(&sm, spa_meta_objset(spa),
1862 sls->sls_sm_obj, 0, UINT64_MAX, SPA_MINBLOCKSHIFT));
1864 (void) printf("Log Spacemap object %llu txg %llu\n",
1865 (u_longlong_t)sls->sls_sm_obj, (u_longlong_t)sls->sls_txg);
1866 dump_spacemap(spa->spa_meta_objset, sm);
1867 space_map_close(sm);
1869 (void) printf("\n");
1873 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
1875 const ddt_phys_t *ddp = dde->dde_phys;
1876 const ddt_key_t *ddk = &dde->dde_key;
1877 const char *types[4] = { "ditto", "single", "double", "triple" };
1878 char blkbuf[BP_SPRINTF_LEN];
1882 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1883 if (ddp->ddp_phys_birth == 0)
1885 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
1886 snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
1887 (void) printf("index %llx refcnt %llu %s %s\n",
1888 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
1894 dump_dedup_ratio(const ddt_stat_t *dds)
1896 double rL, rP, rD, D, dedup, compress, copies;
1898 if (dds->dds_blocks == 0)
1901 rL = (double)dds->dds_ref_lsize;
1902 rP = (double)dds->dds_ref_psize;
1903 rD = (double)dds->dds_ref_dsize;
1904 D = (double)dds->dds_dsize;
1910 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
1911 "dedup * compress / copies = %.2f\n\n",
1912 dedup, compress, copies, dedup * compress / copies);
1916 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
1918 char name[DDT_NAMELEN];
1921 dmu_object_info_t doi;
1922 uint64_t count, dspace, mspace;
1925 error = ddt_object_info(ddt, type, class, &doi);
1927 if (error == ENOENT)
1931 error = ddt_object_count(ddt, type, class, &count);
1936 dspace = doi.doi_physical_blocks_512 << 9;
1937 mspace = doi.doi_fill_count * doi.doi_data_block_size;
1939 ddt_object_name(ddt, type, class, name);
1941 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
1943 (u_longlong_t)count,
1944 (u_longlong_t)(dspace / count),
1945 (u_longlong_t)(mspace / count));
1947 if (dump_opt['D'] < 3)
1950 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
1952 if (dump_opt['D'] < 4)
1955 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
1958 (void) printf("%s contents:\n\n", name);
1960 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
1961 dump_dde(ddt, &dde, walk);
1963 ASSERT3U(error, ==, ENOENT);
1965 (void) printf("\n");
1969 dump_all_ddts(spa_t *spa)
1971 ddt_histogram_t ddh_total;
1972 ddt_stat_t dds_total;
1974 bzero(&ddh_total, sizeof (ddh_total));
1975 bzero(&dds_total, sizeof (dds_total));
1977 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1978 ddt_t *ddt = spa->spa_ddt[c];
1979 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
1980 for (enum ddt_class class = 0; class < DDT_CLASSES;
1982 dump_ddt(ddt, type, class);
1987 ddt_get_dedup_stats(spa, &dds_total);
1989 if (dds_total.dds_blocks == 0) {
1990 (void) printf("All DDTs are empty\n");
1994 (void) printf("\n");
1996 if (dump_opt['D'] > 1) {
1997 (void) printf("DDT histogram (aggregated over all DDTs):\n");
1998 ddt_get_dedup_histogram(spa, &ddh_total);
1999 zpool_dump_ddt(&dds_total, &ddh_total);
2002 dump_dedup_ratio(&dds_total);
2006 dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
2010 (void) printf("%s [%llu,%llu) length %llu\n",
2012 (u_longlong_t)start,
2013 (u_longlong_t)(start + size),
2014 (u_longlong_t)(size));
2018 dump_dtl(vdev_t *vd, int indent)
2020 spa_t *spa = vd->vdev_spa;
2022 const char *name[DTL_TYPES] = { "missing", "partial", "scrub",
2026 spa_vdev_state_enter(spa, SCL_NONE);
2027 required = vdev_dtl_required(vd);
2028 (void) spa_vdev_state_exit(spa, NULL, 0);
2031 (void) printf("\nDirty time logs:\n\n");
2033 (void) printf("\t%*s%s [%s]\n", indent, "",
2034 vd->vdev_path ? vd->vdev_path :
2035 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
2036 required ? "DTL-required" : "DTL-expendable");
2038 for (int t = 0; t < DTL_TYPES; t++) {
2039 range_tree_t *rt = vd->vdev_dtl[t];
2040 if (range_tree_space(rt) == 0)
2042 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
2043 indent + 2, "", name[t]);
2044 range_tree_walk(rt, dump_dtl_seg, prefix);
2045 if (dump_opt['d'] > 5 && vd->vdev_children == 0)
2046 dump_spacemap(spa->spa_meta_objset,
2050 for (unsigned c = 0; c < vd->vdev_children; c++)
2051 dump_dtl(vd->vdev_child[c], indent + 4);
2055 dump_history(spa_t *spa)
2057 nvlist_t **events = NULL;
2059 uint64_t resid, len, off = 0;
2065 char internalstr[MAXPATHLEN];
2067 if ((buf = malloc(SPA_OLD_MAXBLOCKSIZE)) == NULL) {
2068 (void) fprintf(stderr, "%s: unable to allocate I/O buffer\n",
2074 len = SPA_OLD_MAXBLOCKSIZE;
2076 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
2077 (void) fprintf(stderr, "Unable to read history: "
2078 "error %d\n", error);
2083 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
2089 (void) printf("\nHistory:\n");
2090 for (unsigned i = 0; i < num; i++) {
2091 uint64_t time, txg, ievent;
2093 boolean_t printed = B_FALSE;
2095 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
2098 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
2100 if (nvlist_lookup_uint64(events[i],
2101 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
2103 verify(nvlist_lookup_uint64(events[i],
2104 ZPOOL_HIST_TXG, &txg) == 0);
2105 verify(nvlist_lookup_string(events[i],
2106 ZPOOL_HIST_INT_STR, &intstr) == 0);
2107 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
2110 (void) snprintf(internalstr,
2111 sizeof (internalstr),
2112 "[internal %s txg:%lld] %s",
2113 zfs_history_event_names[ievent],
2114 (longlong_t)txg, intstr);
2118 (void) localtime_r(&tsec, &t);
2119 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
2120 (void) printf("%s %s\n", tbuf, cmd);
2124 if (dump_opt['h'] > 1) {
2126 (void) printf("unrecognized record:\n");
2127 dump_nvlist(events[i], 2);
2135 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
2140 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
2141 const zbookmark_phys_t *zb)
2144 ASSERT(zb->zb_level < 0);
2145 if (zb->zb_object == 0)
2146 return (zb->zb_blkid);
2147 return (zb->zb_blkid * BP_GET_LSIZE(bp));
2150 ASSERT(zb->zb_level >= 0);
2152 return ((zb->zb_blkid <<
2153 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
2154 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
2158 snprintf_zstd_header(spa_t *spa, char *blkbuf, size_t buflen,
2164 zfs_zstdhdr_t zstd_hdr;
2167 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_ZSTD)
2173 if (BP_IS_EMBEDDED(bp)) {
2174 buf = malloc(SPA_MAXBLOCKSIZE);
2176 (void) fprintf(stderr, "out of memory\n");
2179 decode_embedded_bp_compressed(bp, buf);
2180 memcpy(&zstd_hdr, buf, sizeof (zstd_hdr));
2182 zstd_hdr.c_len = BE_32(zstd_hdr.c_len);
2183 zstd_hdr.raw_version_level = BE_32(zstd_hdr.raw_version_level);
2184 (void) snprintf(blkbuf + strlen(blkbuf),
2185 buflen - strlen(blkbuf),
2186 " ZSTD:size=%u:version=%u:level=%u:EMBEDDED",
2187 zstd_hdr.c_len, zstd_hdr.version, zstd_hdr.level);
2191 pabd = abd_alloc_for_io(SPA_MAXBLOCKSIZE, B_FALSE);
2192 zio = zio_root(spa, NULL, NULL, 0);
2194 /* Decrypt but don't decompress so we can read the compression header */
2195 zio_nowait(zio_read(zio, spa, bp, pabd, BP_GET_PSIZE(bp), NULL, NULL,
2196 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW_COMPRESS,
2198 error = zio_wait(zio);
2200 (void) fprintf(stderr, "read failed: %d\n", error);
2203 buf = abd_borrow_buf_copy(pabd, BP_GET_LSIZE(bp));
2204 memcpy(&zstd_hdr, buf, sizeof (zstd_hdr));
2205 zstd_hdr.c_len = BE_32(zstd_hdr.c_len);
2206 zstd_hdr.raw_version_level = BE_32(zstd_hdr.raw_version_level);
2208 (void) snprintf(blkbuf + strlen(blkbuf),
2209 buflen - strlen(blkbuf),
2210 " ZSTD:size=%u:version=%u:level=%u:NORMAL",
2211 zstd_hdr.c_len, zstd_hdr.version, zstd_hdr.level);
2213 abd_return_buf_copy(pabd, buf, BP_GET_LSIZE(bp));
2217 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp,
2220 const dva_t *dva = bp->blk_dva;
2221 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
2224 if (dump_opt['b'] >= 6) {
2225 snprintf_blkptr(blkbuf, buflen, bp);
2227 (void) snprintf(blkbuf + strlen(blkbuf),
2228 buflen - strlen(blkbuf), " %s", "FREE");
2233 if (BP_IS_EMBEDDED(bp)) {
2234 (void) sprintf(blkbuf,
2235 "EMBEDDED et=%u %llxL/%llxP B=%llu",
2236 (int)BPE_GET_ETYPE(bp),
2237 (u_longlong_t)BPE_GET_LSIZE(bp),
2238 (u_longlong_t)BPE_GET_PSIZE(bp),
2239 (u_longlong_t)bp->blk_birth);
2245 for (i = 0; i < ndvas; i++)
2246 (void) snprintf(blkbuf + strlen(blkbuf),
2247 buflen - strlen(blkbuf), "%llu:%llx:%llx ",
2248 (u_longlong_t)DVA_GET_VDEV(&dva[i]),
2249 (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
2250 (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
2252 if (BP_IS_HOLE(bp)) {
2253 (void) snprintf(blkbuf + strlen(blkbuf),
2254 buflen - strlen(blkbuf),
2256 (u_longlong_t)BP_GET_LSIZE(bp),
2257 (u_longlong_t)bp->blk_birth);
2259 (void) snprintf(blkbuf + strlen(blkbuf),
2260 buflen - strlen(blkbuf),
2261 "%llxL/%llxP F=%llu B=%llu/%llu",
2262 (u_longlong_t)BP_GET_LSIZE(bp),
2263 (u_longlong_t)BP_GET_PSIZE(bp),
2264 (u_longlong_t)BP_GET_FILL(bp),
2265 (u_longlong_t)bp->blk_birth,
2266 (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
2268 (void) snprintf(blkbuf + strlen(blkbuf),
2269 buflen - strlen(blkbuf), " %s", "FREE");
2270 (void) snprintf(blkbuf + strlen(blkbuf),
2271 buflen - strlen(blkbuf), " cksum=%llx:%llx:%llx:%llx",
2272 (u_longlong_t)bp->blk_cksum.zc_word[0],
2273 (u_longlong_t)bp->blk_cksum.zc_word[1],
2274 (u_longlong_t)bp->blk_cksum.zc_word[2],
2275 (u_longlong_t)bp->blk_cksum.zc_word[3]);
2280 print_indirect(spa_t *spa, blkptr_t *bp, const zbookmark_phys_t *zb,
2281 const dnode_phys_t *dnp)
2283 char blkbuf[BP_SPRINTF_LEN];
2286 if (!BP_IS_EMBEDDED(bp)) {
2287 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
2288 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
2291 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
2293 ASSERT(zb->zb_level >= 0);
2295 for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
2296 if (l == zb->zb_level) {
2297 (void) printf("L%llx", (u_longlong_t)zb->zb_level);
2303 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp, B_FALSE);
2304 if (dump_opt['Z'] && BP_GET_COMPRESS(bp) == ZIO_COMPRESS_ZSTD)
2305 snprintf_zstd_header(spa, blkbuf, sizeof (blkbuf), bp);
2306 (void) printf("%s\n", blkbuf);
2310 visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
2311 blkptr_t *bp, const zbookmark_phys_t *zb)
2315 if (bp->blk_birth == 0)
2318 print_indirect(spa, bp, zb, dnp);
2320 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
2321 arc_flags_t flags = ARC_FLAG_WAIT;
2324 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
2327 ASSERT(!BP_IS_REDACTED(bp));
2329 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
2330 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
2333 ASSERT(buf->b_data);
2335 /* recursively visit blocks below this */
2337 for (i = 0; i < epb; i++, cbp++) {
2338 zbookmark_phys_t czb;
2340 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
2342 zb->zb_blkid * epb + i);
2343 err = visit_indirect(spa, dnp, cbp, &czb);
2346 fill += BP_GET_FILL(cbp);
2349 ASSERT3U(fill, ==, BP_GET_FILL(bp));
2350 arc_buf_destroy(buf, &buf);
2358 dump_indirect(dnode_t *dn)
2360 dnode_phys_t *dnp = dn->dn_phys;
2362 zbookmark_phys_t czb;
2364 (void) printf("Indirect blocks:\n");
2366 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
2367 dn->dn_object, dnp->dn_nlevels - 1, 0);
2368 for (j = 0; j < dnp->dn_nblkptr; j++) {
2370 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
2371 &dnp->dn_blkptr[j], &czb);
2374 (void) printf("\n");
2379 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
2381 dsl_dir_phys_t *dd = data;
2385 /* make sure nicenum has enough space */
2386 CTASSERT(sizeof (nice) >= NN_NUMBUF_SZ);
2391 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
2393 crtime = dd->dd_creation_time;
2394 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
2395 (void) printf("\t\thead_dataset_obj = %llu\n",
2396 (u_longlong_t)dd->dd_head_dataset_obj);
2397 (void) printf("\t\tparent_dir_obj = %llu\n",
2398 (u_longlong_t)dd->dd_parent_obj);
2399 (void) printf("\t\torigin_obj = %llu\n",
2400 (u_longlong_t)dd->dd_origin_obj);
2401 (void) printf("\t\tchild_dir_zapobj = %llu\n",
2402 (u_longlong_t)dd->dd_child_dir_zapobj);
2403 zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice));
2404 (void) printf("\t\tused_bytes = %s\n", nice);
2405 zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice));
2406 (void) printf("\t\tcompressed_bytes = %s\n", nice);
2407 zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice));
2408 (void) printf("\t\tuncompressed_bytes = %s\n", nice);
2409 zdb_nicenum(dd->dd_quota, nice, sizeof (nice));
2410 (void) printf("\t\tquota = %s\n", nice);
2411 zdb_nicenum(dd->dd_reserved, nice, sizeof (nice));
2412 (void) printf("\t\treserved = %s\n", nice);
2413 (void) printf("\t\tprops_zapobj = %llu\n",
2414 (u_longlong_t)dd->dd_props_zapobj);
2415 (void) printf("\t\tdeleg_zapobj = %llu\n",
2416 (u_longlong_t)dd->dd_deleg_zapobj);
2417 (void) printf("\t\tflags = %llx\n",
2418 (u_longlong_t)dd->dd_flags);
2421 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \
2423 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
2430 (void) printf("\t\tclones = %llu\n",
2431 (u_longlong_t)dd->dd_clones);
2436 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
2438 dsl_dataset_phys_t *ds = data;
2440 char used[32], compressed[32], uncompressed[32], unique[32];
2441 char blkbuf[BP_SPRINTF_LEN];
2443 /* make sure nicenum has enough space */
2444 CTASSERT(sizeof (used) >= NN_NUMBUF_SZ);
2445 CTASSERT(sizeof (compressed) >= NN_NUMBUF_SZ);
2446 CTASSERT(sizeof (uncompressed) >= NN_NUMBUF_SZ);
2447 CTASSERT(sizeof (unique) >= NN_NUMBUF_SZ);
2452 ASSERT(size == sizeof (*ds));
2453 crtime = ds->ds_creation_time;
2454 zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used));
2455 zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed));
2456 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed,
2457 sizeof (uncompressed));
2458 zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique));
2459 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
2461 (void) printf("\t\tdir_obj = %llu\n",
2462 (u_longlong_t)ds->ds_dir_obj);
2463 (void) printf("\t\tprev_snap_obj = %llu\n",
2464 (u_longlong_t)ds->ds_prev_snap_obj);
2465 (void) printf("\t\tprev_snap_txg = %llu\n",
2466 (u_longlong_t)ds->ds_prev_snap_txg);
2467 (void) printf("\t\tnext_snap_obj = %llu\n",
2468 (u_longlong_t)ds->ds_next_snap_obj);
2469 (void) printf("\t\tsnapnames_zapobj = %llu\n",
2470 (u_longlong_t)ds->ds_snapnames_zapobj);
2471 (void) printf("\t\tnum_children = %llu\n",
2472 (u_longlong_t)ds->ds_num_children);
2473 (void) printf("\t\tuserrefs_obj = %llu\n",
2474 (u_longlong_t)ds->ds_userrefs_obj);
2475 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
2476 (void) printf("\t\tcreation_txg = %llu\n",
2477 (u_longlong_t)ds->ds_creation_txg);
2478 (void) printf("\t\tdeadlist_obj = %llu\n",
2479 (u_longlong_t)ds->ds_deadlist_obj);
2480 (void) printf("\t\tused_bytes = %s\n", used);
2481 (void) printf("\t\tcompressed_bytes = %s\n", compressed);
2482 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
2483 (void) printf("\t\tunique = %s\n", unique);
2484 (void) printf("\t\tfsid_guid = %llu\n",
2485 (u_longlong_t)ds->ds_fsid_guid);
2486 (void) printf("\t\tguid = %llu\n",
2487 (u_longlong_t)ds->ds_guid);
2488 (void) printf("\t\tflags = %llx\n",
2489 (u_longlong_t)ds->ds_flags);
2490 (void) printf("\t\tnext_clones_obj = %llu\n",
2491 (u_longlong_t)ds->ds_next_clones_obj);
2492 (void) printf("\t\tprops_obj = %llu\n",
2493 (u_longlong_t)ds->ds_props_obj);
2494 (void) printf("\t\tbp = %s\n", blkbuf);
2499 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2501 char blkbuf[BP_SPRINTF_LEN];
2503 if (bp->blk_birth != 0) {
2504 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2505 (void) printf("\t%s\n", blkbuf);
2511 dump_bptree(objset_t *os, uint64_t obj, const char *name)
2517 /* make sure nicenum has enough space */
2518 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
2520 if (dump_opt['d'] < 3)
2523 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
2525 zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes));
2526 (void) printf("\n %s: %llu datasets, %s\n",
2527 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
2528 dmu_buf_rele(db, FTAG);
2530 if (dump_opt['d'] < 5)
2533 (void) printf("\n");
2535 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
2540 dump_bpobj_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, dmu_tx_t *tx)
2542 char blkbuf[BP_SPRINTF_LEN];
2544 ASSERT(bp->blk_birth != 0);
2545 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp, bp_freed);
2546 (void) printf("\t%s\n", blkbuf);
2551 dump_full_bpobj(bpobj_t *bpo, const char *name, int indent)
2558 /* make sure nicenum has enough space */
2559 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
2560 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
2561 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
2563 if (dump_opt['d'] < 3)
2566 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes));
2567 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
2568 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp));
2569 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp));
2570 if (bpo->bpo_havefreed) {
2571 (void) printf(" %*s: object %llu, %llu local "
2572 "blkptrs, %llu freed, %llu subobjs in object %llu, "
2573 "%s (%s/%s comp)\n",
2575 (u_longlong_t)bpo->bpo_object,
2576 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
2577 (u_longlong_t)bpo->bpo_phys->bpo_num_freed,
2578 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
2579 (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
2580 bytes, comp, uncomp);
2582 (void) printf(" %*s: object %llu, %llu local "
2583 "blkptrs, %llu subobjs in object %llu, "
2584 "%s (%s/%s comp)\n",
2586 (u_longlong_t)bpo->bpo_object,
2587 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
2588 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
2589 (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
2590 bytes, comp, uncomp);
2593 for (i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
2597 VERIFY0(dmu_read(bpo->bpo_os,
2598 bpo->bpo_phys->bpo_subobjs,
2599 i * sizeof (subobj), sizeof (subobj), &subobj, 0));
2600 error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
2602 (void) printf("ERROR %u while trying to open "
2604 error, (u_longlong_t)subobj);
2607 dump_full_bpobj(&subbpo, "subobj", indent + 1);
2608 bpobj_close(&subbpo);
2611 if (bpo->bpo_havefreed) {
2612 (void) printf(" %*s: object %llu, %llu blkptrs, "
2615 (u_longlong_t)bpo->bpo_object,
2616 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
2617 (u_longlong_t)bpo->bpo_phys->bpo_num_freed,
2620 (void) printf(" %*s: object %llu, %llu blkptrs, "
2623 (u_longlong_t)bpo->bpo_object,
2624 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
2629 if (dump_opt['d'] < 5)
2634 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
2635 (void) printf("\n");
2640 dump_bookmark(dsl_pool_t *dp, char *name, boolean_t print_redact,
2641 boolean_t print_list)
2644 zfs_bookmark_phys_t prop;
2645 objset_t *mos = dp->dp_spa->spa_meta_objset;
2646 err = dsl_bookmark_lookup(dp, name, NULL, &prop);
2652 (void) printf("\t#%s: ", strchr(name, '#') + 1);
2653 (void) printf("{guid: %llx creation_txg: %llu creation_time: "
2654 "%llu redaction_obj: %llu}\n", (u_longlong_t)prop.zbm_guid,
2655 (u_longlong_t)prop.zbm_creation_txg,
2656 (u_longlong_t)prop.zbm_creation_time,
2657 (u_longlong_t)prop.zbm_redaction_obj);
2659 IMPLY(print_list, print_redact);
2660 if (!print_redact || prop.zbm_redaction_obj == 0)
2663 redaction_list_t *rl;
2664 VERIFY0(dsl_redaction_list_hold_obj(dp,
2665 prop.zbm_redaction_obj, FTAG, &rl));
2667 redaction_list_phys_t *rlp = rl->rl_phys;
2668 (void) printf("\tRedacted:\n\t\tProgress: ");
2669 if (rlp->rlp_last_object != UINT64_MAX ||
2670 rlp->rlp_last_blkid != UINT64_MAX) {
2671 (void) printf("%llu %llu (incomplete)\n",
2672 (u_longlong_t)rlp->rlp_last_object,
2673 (u_longlong_t)rlp->rlp_last_blkid);
2675 (void) printf("complete\n");
2677 (void) printf("\t\tSnapshots: [");
2678 for (unsigned int i = 0; i < rlp->rlp_num_snaps; i++) {
2680 (void) printf(", ");
2681 (void) printf("%0llu",
2682 (u_longlong_t)rlp->rlp_snaps[i]);
2684 (void) printf("]\n\t\tLength: %llu\n",
2685 (u_longlong_t)rlp->rlp_num_entries);
2688 dsl_redaction_list_rele(rl, FTAG);
2692 if (rlp->rlp_num_entries == 0) {
2693 dsl_redaction_list_rele(rl, FTAG);
2694 (void) printf("\t\tRedaction List: []\n\n");
2698 redact_block_phys_t *rbp_buf;
2700 dmu_object_info_t doi;
2702 VERIFY0(dmu_object_info(mos, prop.zbm_redaction_obj, &doi));
2703 size = doi.doi_max_offset;
2704 rbp_buf = kmem_alloc(size, KM_SLEEP);
2706 err = dmu_read(mos, prop.zbm_redaction_obj, 0, size,
2709 dsl_redaction_list_rele(rl, FTAG);
2710 kmem_free(rbp_buf, size);
2714 (void) printf("\t\tRedaction List: [{object: %llx, offset: "
2715 "%llx, blksz: %x, count: %llx}",
2716 (u_longlong_t)rbp_buf[0].rbp_object,
2717 (u_longlong_t)rbp_buf[0].rbp_blkid,
2718 (uint_t)(redact_block_get_size(&rbp_buf[0])),
2719 (u_longlong_t)redact_block_get_count(&rbp_buf[0]));
2721 for (size_t i = 1; i < rlp->rlp_num_entries; i++) {
2722 (void) printf(",\n\t\t{object: %llx, offset: %llx, "
2723 "blksz: %x, count: %llx}",
2724 (u_longlong_t)rbp_buf[i].rbp_object,
2725 (u_longlong_t)rbp_buf[i].rbp_blkid,
2726 (uint_t)(redact_block_get_size(&rbp_buf[i])),
2727 (u_longlong_t)redact_block_get_count(&rbp_buf[i]));
2729 dsl_redaction_list_rele(rl, FTAG);
2730 kmem_free(rbp_buf, size);
2731 (void) printf("]\n\n");
2736 dump_bookmarks(objset_t *os, int verbosity)
2739 zap_attribute_t attr;
2740 dsl_dataset_t *ds = dmu_objset_ds(os);
2741 dsl_pool_t *dp = spa_get_dsl(os->os_spa);
2742 objset_t *mos = os->os_spa->spa_meta_objset;
2745 dsl_pool_config_enter(dp, FTAG);
2747 for (zap_cursor_init(&zc, mos, ds->ds_bookmarks_obj);
2748 zap_cursor_retrieve(&zc, &attr) == 0;
2749 zap_cursor_advance(&zc)) {
2750 char osname[ZFS_MAX_DATASET_NAME_LEN];
2751 char buf[ZFS_MAX_DATASET_NAME_LEN];
2752 dmu_objset_name(os, osname);
2753 VERIFY3S(0, <=, snprintf(buf, sizeof (buf), "%s#%s", osname,
2755 (void) dump_bookmark(dp, buf, verbosity >= 5, verbosity >= 6);
2757 zap_cursor_fini(&zc);
2758 dsl_pool_config_exit(dp, FTAG);
2762 bpobj_count_refd(bpobj_t *bpo)
2764 mos_obj_refd(bpo->bpo_object);
2766 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
2767 mos_obj_refd(bpo->bpo_phys->bpo_subobjs);
2768 for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
2772 VERIFY0(dmu_read(bpo->bpo_os,
2773 bpo->bpo_phys->bpo_subobjs,
2774 i * sizeof (subobj), sizeof (subobj), &subobj, 0));
2775 error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
2777 (void) printf("ERROR %u while trying to open "
2779 error, (u_longlong_t)subobj);
2782 bpobj_count_refd(&subbpo);
2783 bpobj_close(&subbpo);
2789 dsl_deadlist_entry_count_refd(void *arg, dsl_deadlist_entry_t *dle)
2792 uint64_t empty_bpobj = spa->spa_dsl_pool->dp_empty_bpobj;
2793 if (dle->dle_bpobj.bpo_object != empty_bpobj)
2794 bpobj_count_refd(&dle->dle_bpobj);
2799 dsl_deadlist_entry_dump(void *arg, dsl_deadlist_entry_t *dle)
2801 ASSERT(arg == NULL);
2802 if (dump_opt['d'] >= 5) {
2804 (void) snprintf(buf, sizeof (buf),
2805 "mintxg %llu -> obj %llu",
2806 (longlong_t)dle->dle_mintxg,
2807 (longlong_t)dle->dle_bpobj.bpo_object);
2809 dump_full_bpobj(&dle->dle_bpobj, buf, 0);
2811 (void) printf("mintxg %llu -> obj %llu\n",
2812 (longlong_t)dle->dle_mintxg,
2813 (longlong_t)dle->dle_bpobj.bpo_object);
2819 dump_blkptr_list(dsl_deadlist_t *dl, char *name)
2825 spa_t *spa = dmu_objset_spa(dl->dl_os);
2826 uint64_t empty_bpobj = spa->spa_dsl_pool->dp_empty_bpobj;
2828 if (dl->dl_oldfmt) {
2829 if (dl->dl_bpobj.bpo_object != empty_bpobj)
2830 bpobj_count_refd(&dl->dl_bpobj);
2832 mos_obj_refd(dl->dl_object);
2833 dsl_deadlist_iterate(dl, dsl_deadlist_entry_count_refd, spa);
2836 /* make sure nicenum has enough space */
2837 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
2838 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
2839 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
2840 CTASSERT(sizeof (entries) >= NN_NUMBUF_SZ);
2842 if (dump_opt['d'] < 3)
2845 if (dl->dl_oldfmt) {
2846 dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
2850 zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes));
2851 zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp));
2852 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp));
2853 zdb_nicenum(avl_numnodes(&dl->dl_tree), entries, sizeof (entries));
2854 (void) printf("\n %s: %s (%s/%s comp), %s entries\n",
2855 name, bytes, comp, uncomp, entries);
2857 if (dump_opt['d'] < 4)
2860 (void) printf("\n");
2862 dsl_deadlist_iterate(dl, dsl_deadlist_entry_dump, NULL);
2866 verify_dd_livelist(objset_t *os)
2868 uint64_t ll_used, used, ll_comp, comp, ll_uncomp, uncomp;
2869 dsl_pool_t *dp = spa_get_dsl(os->os_spa);
2870 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
2872 ASSERT(!dmu_objset_is_snapshot(os));
2873 if (!dsl_deadlist_is_open(&dd->dd_livelist))
2876 /* Iterate through the livelist to check for duplicates */
2877 dsl_deadlist_iterate(&dd->dd_livelist, sublivelist_verify_lightweight,
2880 dsl_pool_config_enter(dp, FTAG);
2881 dsl_deadlist_space(&dd->dd_livelist, &ll_used,
2882 &ll_comp, &ll_uncomp);
2884 dsl_dataset_t *origin_ds;
2885 ASSERT(dsl_pool_config_held(dp));
2886 VERIFY0(dsl_dataset_hold_obj(dp,
2887 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin_ds));
2888 VERIFY0(dsl_dataset_space_written(origin_ds, os->os_dsl_dataset,
2889 &used, &comp, &uncomp));
2890 dsl_dataset_rele(origin_ds, FTAG);
2891 dsl_pool_config_exit(dp, FTAG);
2893 * It's possible that the dataset's uncomp space is larger than the
2894 * livelist's because livelists do not track embedded block pointers
2896 if (used != ll_used || comp != ll_comp || uncomp < ll_uncomp) {
2897 char nice_used[32], nice_comp[32], nice_uncomp[32];
2898 (void) printf("Discrepancy in space accounting:\n");
2899 zdb_nicenum(used, nice_used, sizeof (nice_used));
2900 zdb_nicenum(comp, nice_comp, sizeof (nice_comp));
2901 zdb_nicenum(uncomp, nice_uncomp, sizeof (nice_uncomp));
2902 (void) printf("dir: used %s, comp %s, uncomp %s\n",
2903 nice_used, nice_comp, nice_uncomp);
2904 zdb_nicenum(ll_used, nice_used, sizeof (nice_used));
2905 zdb_nicenum(ll_comp, nice_comp, sizeof (nice_comp));
2906 zdb_nicenum(ll_uncomp, nice_uncomp, sizeof (nice_uncomp));
2907 (void) printf("livelist: used %s, comp %s, uncomp %s\n",
2908 nice_used, nice_comp, nice_uncomp);
2914 static avl_tree_t idx_tree;
2915 static avl_tree_t domain_tree;
2916 static boolean_t fuid_table_loaded;
2917 static objset_t *sa_os = NULL;
2918 static sa_attr_type_t *sa_attr_table = NULL;
2921 open_objset(const char *path, void *tag, objset_t **osp)
2924 uint64_t sa_attrs = 0;
2925 uint64_t version = 0;
2927 VERIFY3P(sa_os, ==, NULL);
2929 * We can't own an objset if it's redacted. Therefore, we do this
2930 * dance: hold the objset, then acquire a long hold on its dataset, then
2931 * release the pool (which is held as part of holding the objset).
2933 err = dmu_objset_hold(path, tag, osp);
2935 (void) fprintf(stderr, "failed to hold dataset '%s': %s\n",
2936 path, strerror(err));
2939 dsl_dataset_long_hold(dmu_objset_ds(*osp), tag);
2940 dsl_pool_rele(dmu_objset_pool(*osp), tag);
2942 if (dmu_objset_type(*osp) == DMU_OST_ZFS && !(*osp)->os_encrypted) {
2943 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2945 if (version >= ZPL_VERSION_SA) {
2946 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
2949 err = sa_setup(*osp, sa_attrs, zfs_attr_table, ZPL_END,
2952 (void) fprintf(stderr, "sa_setup failed: %s\n",
2954 dsl_dataset_long_rele(dmu_objset_ds(*osp), tag);
2955 dsl_dataset_rele(dmu_objset_ds(*osp), tag);
2965 close_objset(objset_t *os, void *tag)
2967 VERIFY3P(os, ==, sa_os);
2968 if (os->os_sa != NULL)
2970 dsl_dataset_long_rele(dmu_objset_ds(os), tag);
2971 dsl_dataset_rele(dmu_objset_ds(os), tag);
2972 sa_attr_table = NULL;
2977 fuid_table_destroy(void)
2979 if (fuid_table_loaded) {
2980 zfs_fuid_table_destroy(&idx_tree, &domain_tree);
2981 fuid_table_loaded = B_FALSE;
2986 * print uid or gid information.
2987 * For normal POSIX id just the id is printed in decimal format.
2988 * For CIFS files with FUID the fuid is printed in hex followed by
2989 * the domain-rid string.
2992 print_idstr(uint64_t id, const char *id_type)
2994 if (FUID_INDEX(id)) {
2997 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
2998 (void) printf("\t%s %llx [%s-%d]\n", id_type,
2999 (u_longlong_t)id, domain, (int)FUID_RID(id));
3001 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id);
3007 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
3009 uint32_t uid_idx, gid_idx;
3011 uid_idx = FUID_INDEX(uid);
3012 gid_idx = FUID_INDEX(gid);
3014 /* Load domain table, if not already loaded */
3015 if (!fuid_table_loaded && (uid_idx || gid_idx)) {
3018 /* first find the fuid object. It lives in the master node */
3019 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
3020 8, 1, &fuid_obj) == 0);
3021 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
3022 (void) zfs_fuid_table_load(os, fuid_obj,
3023 &idx_tree, &domain_tree);
3024 fuid_table_loaded = B_TRUE;
3027 print_idstr(uid, "uid");
3028 print_idstr(gid, "gid");
3032 dump_znode_sa_xattr(sa_handle_t *hdl)
3035 nvpair_t *elem = NULL;
3036 int sa_xattr_size = 0;
3037 int sa_xattr_entries = 0;
3039 char *sa_xattr_packed;
3041 error = sa_size(hdl, sa_attr_table[ZPL_DXATTR], &sa_xattr_size);
3042 if (error || sa_xattr_size == 0)
3045 sa_xattr_packed = malloc(sa_xattr_size);
3046 if (sa_xattr_packed == NULL)
3049 error = sa_lookup(hdl, sa_attr_table[ZPL_DXATTR],
3050 sa_xattr_packed, sa_xattr_size);
3052 free(sa_xattr_packed);
3056 error = nvlist_unpack(sa_xattr_packed, sa_xattr_size, &sa_xattr, 0);
3058 free(sa_xattr_packed);
3062 while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL)
3065 (void) printf("\tSA xattrs: %d bytes, %d entries\n\n",
3066 sa_xattr_size, sa_xattr_entries);
3067 while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL) {
3071 (void) printf("\t\t%s = ", nvpair_name(elem));
3072 nvpair_value_byte_array(elem, &value, &cnt);
3073 for (idx = 0; idx < cnt; ++idx) {
3074 if (isprint(value[idx]))
3075 (void) putchar(value[idx]);
3077 (void) printf("\\%3.3o", value[idx]);
3079 (void) putchar('\n');
3082 nvlist_free(sa_xattr);
3083 free(sa_xattr_packed);
3087 dump_znode_symlink(sa_handle_t *hdl)
3089 int sa_symlink_size = 0;
3090 char linktarget[MAXPATHLEN];
3091 linktarget[0] = '\0';
3094 error = sa_size(hdl, sa_attr_table[ZPL_SYMLINK], &sa_symlink_size);
3095 if (error || sa_symlink_size == 0) {
3098 if (sa_lookup(hdl, sa_attr_table[ZPL_SYMLINK],
3099 &linktarget, sa_symlink_size) == 0)
3100 (void) printf("\ttarget %s\n", linktarget);
3105 dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
3107 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */
3109 uint64_t xattr, rdev, gen;
3110 uint64_t uid, gid, mode, fsize, parent, links;
3112 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
3113 time_t z_crtime, z_atime, z_mtime, z_ctime;
3114 sa_bulk_attr_t bulk[12];
3118 VERIFY3P(os, ==, sa_os);
3119 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
3120 (void) printf("Failed to get handle for SA znode\n");
3124 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
3125 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
3126 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
3128 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
3129 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
3131 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
3133 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
3135 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
3137 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
3139 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
3141 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
3143 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
3146 if (sa_bulk_lookup(hdl, bulk, idx)) {
3147 (void) sa_handle_destroy(hdl);
3151 z_crtime = (time_t)crtm[0];
3152 z_atime = (time_t)acctm[0];
3153 z_mtime = (time_t)modtm[0];
3154 z_ctime = (time_t)chgtm[0];
3156 if (dump_opt['d'] > 4) {
3157 error = zfs_obj_to_path(os, object, path, sizeof (path));
3158 if (error == ESTALE) {
3159 (void) snprintf(path, sizeof (path), "on delete queue");
3160 } else if (error != 0) {
3162 (void) snprintf(path, sizeof (path),
3163 "path not found, possibly leaked");
3165 (void) printf("\tpath %s\n", path);
3169 dump_znode_symlink(hdl);
3170 dump_uidgid(os, uid, gid);
3171 (void) printf("\tatime %s", ctime(&z_atime));
3172 (void) printf("\tmtime %s", ctime(&z_mtime));
3173 (void) printf("\tctime %s", ctime(&z_ctime));
3174 (void) printf("\tcrtime %s", ctime(&z_crtime));
3175 (void) printf("\tgen %llu\n", (u_longlong_t)gen);
3176 (void) printf("\tmode %llo\n", (u_longlong_t)mode);
3177 (void) printf("\tsize %llu\n", (u_longlong_t)fsize);
3178 (void) printf("\tparent %llu\n", (u_longlong_t)parent);
3179 (void) printf("\tlinks %llu\n", (u_longlong_t)links);
3180 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags);
3181 if (dmu_objset_projectquota_enabled(os) && (pflags & ZFS_PROJID)) {
3184 if (sa_lookup(hdl, sa_attr_table[ZPL_PROJID], &projid,
3185 sizeof (uint64_t)) == 0)
3186 (void) printf("\tprojid %llu\n", (u_longlong_t)projid);
3188 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
3189 sizeof (uint64_t)) == 0)
3190 (void) printf("\txattr %llu\n", (u_longlong_t)xattr);
3191 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
3192 sizeof (uint64_t)) == 0)
3193 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev);
3194 dump_znode_sa_xattr(hdl);
3195 sa_handle_destroy(hdl);
3200 dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
3206 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
3210 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
3211 dump_none, /* unallocated */
3212 dump_zap, /* object directory */
3213 dump_uint64, /* object array */
3214 dump_none, /* packed nvlist */
3215 dump_packed_nvlist, /* packed nvlist size */
3216 dump_none, /* bpobj */
3217 dump_bpobj, /* bpobj header */
3218 dump_none, /* SPA space map header */
3219 dump_none, /* SPA space map */
3220 dump_none, /* ZIL intent log */
3221 dump_dnode, /* DMU dnode */
3222 dump_dmu_objset, /* DMU objset */
3223 dump_dsl_dir, /* DSL directory */
3224 dump_zap, /* DSL directory child map */
3225 dump_zap, /* DSL dataset snap map */
3226 dump_zap, /* DSL props */
3227 dump_dsl_dataset, /* DSL dataset */
3228 dump_znode, /* ZFS znode */
3229 dump_acl, /* ZFS V0 ACL */
3230 dump_uint8, /* ZFS plain file */
3231 dump_zpldir, /* ZFS directory */
3232 dump_zap, /* ZFS master node */
3233 dump_zap, /* ZFS delete queue */
3234 dump_uint8, /* zvol object */
3235 dump_zap, /* zvol prop */
3236 dump_uint8, /* other uint8[] */
3237 dump_uint64, /* other uint64[] */
3238 dump_zap, /* other ZAP */
3239 dump_zap, /* persistent error log */
3240 dump_uint8, /* SPA history */
3241 dump_history_offsets, /* SPA history offsets */
3242 dump_zap, /* Pool properties */
3243 dump_zap, /* DSL permissions */
3244 dump_acl, /* ZFS ACL */
3245 dump_uint8, /* ZFS SYSACL */
3246 dump_none, /* FUID nvlist */
3247 dump_packed_nvlist, /* FUID nvlist size */
3248 dump_zap, /* DSL dataset next clones */
3249 dump_zap, /* DSL scrub queue */
3250 dump_zap, /* ZFS user/group/project used */
3251 dump_zap, /* ZFS user/group/project quota */
3252 dump_zap, /* snapshot refcount tags */
3253 dump_ddt_zap, /* DDT ZAP object */
3254 dump_zap, /* DDT statistics */
3255 dump_znode, /* SA object */
3256 dump_zap, /* SA Master Node */
3257 dump_sa_attrs, /* SA attribute registration */
3258 dump_sa_layouts, /* SA attribute layouts */
3259 dump_zap, /* DSL scrub translations */
3260 dump_none, /* fake dedup BP */
3261 dump_zap, /* deadlist */
3262 dump_none, /* deadlist hdr */
3263 dump_zap, /* dsl clones */
3264 dump_bpobj_subobjs, /* bpobj subobjs */
3265 dump_unknown, /* Unknown type, must be last */
3269 match_object_type(dmu_object_type_t obj_type, uint64_t flags)
3271 boolean_t match = B_TRUE;
3274 case DMU_OT_DIRECTORY_CONTENTS:
3275 if (!(flags & ZOR_FLAG_DIRECTORY))
3278 case DMU_OT_PLAIN_FILE_CONTENTS:
3279 if (!(flags & ZOR_FLAG_PLAIN_FILE))
3282 case DMU_OT_SPACE_MAP:
3283 if (!(flags & ZOR_FLAG_SPACE_MAP))
3287 if (strcmp(zdb_ot_name(obj_type), "zap") == 0) {
3288 if (!(flags & ZOR_FLAG_ZAP))
3294 * If all bits except some of the supported flags are
3295 * set, the user combined the all-types flag (A) with
3296 * a negated flag to exclude some types (e.g. A-f to
3297 * show all object types except plain files).
3299 if ((flags | ZOR_SUPPORTED_FLAGS) != ZOR_FLAG_ALL_TYPES)
3309 dump_object(objset_t *os, uint64_t object, int verbosity,
3310 boolean_t *print_header, uint64_t *dnode_slots_used, uint64_t flags)
3312 dmu_buf_t *db = NULL;
3313 dmu_object_info_t doi;
3315 boolean_t dnode_held = B_FALSE;
3318 char iblk[32], dblk[32], lsize[32], asize[32], fill[32], dnsize[32];
3319 char bonus_size[32];
3323 /* make sure nicenum has enough space */
3324 CTASSERT(sizeof (iblk) >= NN_NUMBUF_SZ);
3325 CTASSERT(sizeof (dblk) >= NN_NUMBUF_SZ);
3326 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
3327 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
3328 CTASSERT(sizeof (bonus_size) >= NN_NUMBUF_SZ);
3330 if (*print_header) {
3331 (void) printf("\n%10s %3s %5s %5s %5s %6s %5s %6s %s\n",
3332 "Object", "lvl", "iblk", "dblk", "dsize", "dnsize",
3333 "lsize", "%full", "type");
3338 dn = DMU_META_DNODE(os);
3339 dmu_object_info_from_dnode(dn, &doi);
3342 * Encrypted datasets will have sensitive bonus buffers
3343 * encrypted. Therefore we cannot hold the bonus buffer and
3344 * must hold the dnode itself instead.
3346 error = dmu_object_info(os, object, &doi);
3348 fatal("dmu_object_info() failed, errno %u", error);
3350 if (os->os_encrypted &&
3351 DMU_OT_IS_ENCRYPTED(doi.doi_bonus_type)) {
3352 error = dnode_hold(os, object, FTAG, &dn);
3354 fatal("dnode_hold() failed, errno %u", error);
3355 dnode_held = B_TRUE;
3357 error = dmu_bonus_hold(os, object, FTAG, &db);
3359 fatal("dmu_bonus_hold(%llu) failed, errno %u",
3361 bonus = db->db_data;
3362 bsize = db->db_size;
3363 dn = DB_DNODE((dmu_buf_impl_t *)db);
3368 * Default to showing all object types if no flags were specified.
3370 if (flags != 0 && flags != ZOR_FLAG_ALL_TYPES &&
3371 !match_object_type(doi.doi_type, flags))
3374 if (dnode_slots_used)
3375 *dnode_slots_used = doi.doi_dnodesize / DNODE_MIN_SIZE;
3377 zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk));
3378 zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk));
3379 zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize));
3380 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize));
3381 zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size));
3382 zdb_nicenum(doi.doi_dnodesize, dnsize, sizeof (dnsize));
3383 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
3384 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
3385 doi.doi_max_offset);
3389 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
3390 (void) snprintf(aux + strlen(aux), sizeof (aux) - strlen(aux),
3391 " (K=%s)", ZDB_CHECKSUM_NAME(doi.doi_checksum));
3394 if (doi.doi_compress == ZIO_COMPRESS_INHERIT &&
3395 ZIO_COMPRESS_HASLEVEL(os->os_compress) && verbosity >= 6) {
3396 const char *compname = NULL;
3397 if (zfs_prop_index_to_string(ZFS_PROP_COMPRESSION,
3398 ZIO_COMPRESS_RAW(os->os_compress, os->os_complevel),
3400 (void) snprintf(aux + strlen(aux),
3401 sizeof (aux) - strlen(aux), " (Z=inherit=%s)",
3404 (void) snprintf(aux + strlen(aux),
3405 sizeof (aux) - strlen(aux),
3406 " (Z=inherit=%s-unknown)",
3407 ZDB_COMPRESS_NAME(os->os_compress));
3409 } else if (doi.doi_compress == ZIO_COMPRESS_INHERIT && verbosity >= 6) {
3410 (void) snprintf(aux + strlen(aux), sizeof (aux) - strlen(aux),
3411 " (Z=inherit=%s)", ZDB_COMPRESS_NAME(os->os_compress));
3412 } else if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
3413 (void) snprintf(aux + strlen(aux), sizeof (aux) - strlen(aux),
3414 " (Z=%s)", ZDB_COMPRESS_NAME(doi.doi_compress));
3417 (void) printf("%10lld %3u %5s %5s %5s %6s %5s %6s %s%s\n",
3418 (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
3419 asize, dnsize, lsize, fill, zdb_ot_name(doi.doi_type), aux);
3421 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
3422 (void) printf("%10s %3s %5s %5s %5s %5s %5s %6s %s\n",
3423 "", "", "", "", "", "", bonus_size, "bonus",
3424 zdb_ot_name(doi.doi_bonus_type));
3427 if (verbosity >= 4) {
3428 (void) printf("\tdnode flags: %s%s%s%s\n",
3429 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
3431 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
3432 "USERUSED_ACCOUNTED " : "",
3433 (dn->dn_phys->dn_flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) ?
3434 "USEROBJUSED_ACCOUNTED " : "",
3435 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
3436 "SPILL_BLKPTR" : "");
3437 (void) printf("\tdnode maxblkid: %llu\n",
3438 (longlong_t)dn->dn_phys->dn_maxblkid);
3441 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os,
3442 object, bonus, bsize);
3444 (void) printf("\t\t(bonus encrypted)\n");
3447 if (!os->os_encrypted || !DMU_OT_IS_ENCRYPTED(doi.doi_type)) {
3448 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object,
3451 (void) printf("\t\t(object encrypted)\n");
3454 *print_header = B_TRUE;
3460 if (verbosity >= 5) {
3462 * Report the list of segments that comprise the object.
3466 uint64_t blkfill = 1;
3469 if (dn->dn_type == DMU_OT_DNODE) {
3471 blkfill = DNODES_PER_BLOCK;
3476 /* make sure nicenum has enough space */
3477 CTASSERT(sizeof (segsize) >= NN_NUMBUF_SZ);
3478 error = dnode_next_offset(dn,
3479 0, &start, minlvl, blkfill, 0);
3483 error = dnode_next_offset(dn,
3484 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
3485 zdb_nicenum(end - start, segsize, sizeof (segsize));
3486 (void) printf("\t\tsegment [%016llx, %016llx)"
3487 " size %5s\n", (u_longlong_t)start,
3488 (u_longlong_t)end, segsize);
3497 dmu_buf_rele(db, FTAG);
3499 dnode_rele(dn, FTAG);
3503 count_dir_mos_objects(dsl_dir_t *dd)
3505 mos_obj_refd(dd->dd_object);
3506 mos_obj_refd(dsl_dir_phys(dd)->dd_child_dir_zapobj);
3507 mos_obj_refd(dsl_dir_phys(dd)->dd_deleg_zapobj);
3508 mos_obj_refd(dsl_dir_phys(dd)->dd_props_zapobj);
3509 mos_obj_refd(dsl_dir_phys(dd)->dd_clones);
3512 * The dd_crypto_obj can be referenced by multiple dsl_dir's.
3513 * Ignore the references after the first one.
3515 mos_obj_refd_multiple(dd->dd_crypto_obj);
3519 count_ds_mos_objects(dsl_dataset_t *ds)
3521 mos_obj_refd(ds->ds_object);
3522 mos_obj_refd(dsl_dataset_phys(ds)->ds_next_clones_obj);
3523 mos_obj_refd(dsl_dataset_phys(ds)->ds_props_obj);
3524 mos_obj_refd(dsl_dataset_phys(ds)->ds_userrefs_obj);
3525 mos_obj_refd(dsl_dataset_phys(ds)->ds_snapnames_zapobj);
3526 mos_obj_refd(ds->ds_bookmarks_obj);
3528 if (!dsl_dataset_is_snapshot(ds)) {
3529 count_dir_mos_objects(ds->ds_dir);
3533 static const char *objset_types[DMU_OST_NUMTYPES] = {
3534 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
3537 * Parse a string denoting a range of object IDs of the form
3538 * <start>[:<end>[:flags]], and store the results in zor.
3539 * Return 0 on success. On error, return 1 and update the msg
3540 * pointer to point to a descriptive error message.
3543 parse_object_range(char *range, zopt_object_range_t *zor, char **msg)
3546 char *p, *s, *dup, *flagstr;
3551 if (strchr(range, ':') == NULL) {
3552 zor->zor_obj_start = strtoull(range, &p, 0);
3554 *msg = "Invalid characters in object ID";
3557 zor->zor_obj_end = zor->zor_obj_start;
3561 if (strchr(range, ':') == range) {
3562 *msg = "Invalid leading colon";
3567 len = strlen(range);
3568 if (range[len - 1] == ':') {
3569 *msg = "Invalid trailing colon";
3574 dup = strdup(range);
3575 s = strtok(dup, ":");
3576 zor->zor_obj_start = strtoull(s, &p, 0);
3579 *msg = "Invalid characters in start object ID";
3584 s = strtok(NULL, ":");
3585 zor->zor_obj_end = strtoull(s, &p, 0);
3588 *msg = "Invalid characters in end object ID";
3593 if (zor->zor_obj_start > zor->zor_obj_end) {
3594 *msg = "Start object ID may not exceed end object ID";
3599 s = strtok(NULL, ":");
3601 zor->zor_flags = ZOR_FLAG_ALL_TYPES;
3603 } else if (strtok(NULL, ":") != NULL) {
3604 *msg = "Invalid colon-delimited field after flags";
3610 for (i = 0; flagstr[i]; i++) {
3612 boolean_t negation = (flagstr[i] == '-');
3616 if (flagstr[i] == '\0') {
3617 *msg = "Invalid trailing negation operator";
3622 bit = flagbits[(uchar_t)flagstr[i]];
3624 *msg = "Invalid flag";
3633 zor->zor_flags = flags;
3641 dump_objset(objset_t *os)
3643 dmu_objset_stats_t dds = { 0 };
3644 uint64_t object, object_count;
3645 uint64_t refdbytes, usedobjs, scratch;
3647 char blkbuf[BP_SPRINTF_LEN + 20];
3648 char osname[ZFS_MAX_DATASET_NAME_LEN];
3649 const char *type = "UNKNOWN";
3650 int verbosity = dump_opt['d'];
3651 boolean_t print_header;
3654 uint64_t total_slots_used = 0;
3655 uint64_t max_slot_used = 0;
3656 uint64_t dnode_slots;
3661 /* make sure nicenum has enough space */
3662 CTASSERT(sizeof (numbuf) >= NN_NUMBUF_SZ);
3664 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
3665 dmu_objset_fast_stat(os, &dds);
3666 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
3668 print_header = B_TRUE;
3670 if (dds.dds_type < DMU_OST_NUMTYPES)
3671 type = objset_types[dds.dds_type];
3673 if (dds.dds_type == DMU_OST_META) {
3674 dds.dds_creation_txg = TXG_INITIAL;
3675 usedobjs = BP_GET_FILL(os->os_rootbp);
3676 refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
3679 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
3682 ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
3684 zdb_nicenum(refdbytes, numbuf, sizeof (numbuf));
3686 if (verbosity >= 4) {
3687 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
3688 (void) snprintf_blkptr(blkbuf + strlen(blkbuf),
3689 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
3694 dmu_objset_name(os, osname);
3696 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
3697 "%s, %llu objects%s%s\n",
3698 osname, type, (u_longlong_t)dmu_objset_id(os),
3699 (u_longlong_t)dds.dds_creation_txg,
3700 numbuf, (u_longlong_t)usedobjs, blkbuf,
3701 (dds.dds_inconsistent) ? " (inconsistent)" : "");
3703 for (i = 0; i < zopt_object_args; i++) {
3704 obj_start = zopt_object_ranges[i].zor_obj_start;
3705 obj_end = zopt_object_ranges[i].zor_obj_end;
3706 flags = zopt_object_ranges[i].zor_flags;
3709 if (object == 0 || obj_start == obj_end)
3710 dump_object(os, object, verbosity, &print_header, NULL,
3715 while ((dmu_object_next(os, &object, B_FALSE, 0) == 0) &&
3716 object <= obj_end) {
3717 dump_object(os, object, verbosity, &print_header, NULL,
3722 if (zopt_object_args > 0) {
3723 (void) printf("\n");
3727 if (dump_opt['i'] != 0 || verbosity >= 2)
3728 dump_intent_log(dmu_objset_zil(os));
3730 if (dmu_objset_ds(os) != NULL) {
3731 dsl_dataset_t *ds = dmu_objset_ds(os);
3732 dump_blkptr_list(&ds->ds_deadlist, "Deadlist");
3733 if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) &&
3734 !dmu_objset_is_snapshot(os)) {
3735 dump_blkptr_list(&ds->ds_dir->dd_livelist, "Livelist");
3736 if (verify_dd_livelist(os) != 0)
3737 fatal("livelist is incorrect");
3740 if (dsl_dataset_remap_deadlist_exists(ds)) {
3741 (void) printf("ds_remap_deadlist:\n");
3742 dump_blkptr_list(&ds->ds_remap_deadlist, "Deadlist");
3744 count_ds_mos_objects(ds);
3747 if (dmu_objset_ds(os) != NULL)
3748 dump_bookmarks(os, verbosity);
3753 if (BP_IS_HOLE(os->os_rootbp))
3756 dump_object(os, 0, verbosity, &print_header, NULL, 0);
3758 if (DMU_USERUSED_DNODE(os) != NULL &&
3759 DMU_USERUSED_DNODE(os)->dn_type != 0) {
3760 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header,
3762 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header,
3766 if (DMU_PROJECTUSED_DNODE(os) != NULL &&
3767 DMU_PROJECTUSED_DNODE(os)->dn_type != 0)
3768 dump_object(os, DMU_PROJECTUSED_OBJECT, verbosity,
3769 &print_header, NULL, 0);
3772 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
3773 dump_object(os, object, verbosity, &print_header, &dnode_slots,
3776 total_slots_used += dnode_slots;
3777 max_slot_used = object + dnode_slots - 1;
3780 (void) printf("\n");
3782 (void) printf(" Dnode slots:\n");
3783 (void) printf("\tTotal used: %10llu\n",
3784 (u_longlong_t)total_slots_used);
3785 (void) printf("\tMax used: %10llu\n",
3786 (u_longlong_t)max_slot_used);
3787 (void) printf("\tPercent empty: %10lf\n",
3788 (double)(max_slot_used - total_slots_used)*100 /
3789 (double)max_slot_used);
3790 (void) printf("\n");
3792 if (error != ESRCH) {
3793 (void) fprintf(stderr, "dmu_object_next() = %d\n", error);
3797 ASSERT3U(object_count, ==, usedobjs);
3799 if (leaked_objects != 0) {
3800 (void) printf("%d potentially leaked objects detected\n",
3807 dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
3809 time_t timestamp = ub->ub_timestamp;
3811 (void) printf("%s", header ? header : "");
3812 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
3813 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
3814 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
3815 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
3816 (void) printf("\ttimestamp = %llu UTC = %s",
3817 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp)));
3819 (void) printf("\tmmp_magic = %016llx\n",
3820 (u_longlong_t)ub->ub_mmp_magic);
3821 if (MMP_VALID(ub)) {
3822 (void) printf("\tmmp_delay = %0llu\n",
3823 (u_longlong_t)ub->ub_mmp_delay);
3824 if (MMP_SEQ_VALID(ub))
3825 (void) printf("\tmmp_seq = %u\n",
3826 (unsigned int) MMP_SEQ(ub));
3827 if (MMP_FAIL_INT_VALID(ub))
3828 (void) printf("\tmmp_fail = %u\n",
3829 (unsigned int) MMP_FAIL_INT(ub));
3830 if (MMP_INTERVAL_VALID(ub))
3831 (void) printf("\tmmp_write = %u\n",
3832 (unsigned int) MMP_INTERVAL(ub));
3833 /* After MMP_* to make summarize_uberblock_mmp cleaner */
3834 (void) printf("\tmmp_valid = %x\n",
3835 (unsigned int) ub->ub_mmp_config & 0xFF);
3838 if (dump_opt['u'] >= 4) {
3839 char blkbuf[BP_SPRINTF_LEN];
3840 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
3841 (void) printf("\trootbp = %s\n", blkbuf);
3843 (void) printf("\tcheckpoint_txg = %llu\n",
3844 (u_longlong_t)ub->ub_checkpoint_txg);
3845 (void) printf("%s", footer ? footer : "");
3849 dump_config(spa_t *spa)
3856 error = dmu_bonus_hold(spa->spa_meta_objset,
3857 spa->spa_config_object, FTAG, &db);
3860 nvsize = *(uint64_t *)db->db_data;
3861 dmu_buf_rele(db, FTAG);
3863 (void) printf("\nMOS Configuration:\n");
3864 dump_packed_nvlist(spa->spa_meta_objset,
3865 spa->spa_config_object, (void *)&nvsize, 1);
3867 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
3868 (u_longlong_t)spa->spa_config_object, error);
3873 dump_cachefile(const char *cachefile)
3876 struct stat64 statbuf;
3880 if ((fd = open64(cachefile, O_RDONLY)) < 0) {
3881 (void) printf("cannot open '%s': %s\n", cachefile,
3886 if (fstat64(fd, &statbuf) != 0) {
3887 (void) printf("failed to stat '%s': %s\n", cachefile,
3892 if ((buf = malloc(statbuf.st_size)) == NULL) {
3893 (void) fprintf(stderr, "failed to allocate %llu bytes\n",
3894 (u_longlong_t)statbuf.st_size);
3898 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
3899 (void) fprintf(stderr, "failed to read %llu bytes\n",
3900 (u_longlong_t)statbuf.st_size);
3906 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
3907 (void) fprintf(stderr, "failed to unpack nvlist\n");
3913 dump_nvlist(config, 0);
3915 nvlist_free(config);
3919 * ZFS label nvlist stats
3921 typedef struct zdb_nvl_stats {
3924 size_t zns_leaf_largest;
3925 size_t zns_leaf_total;
3926 nvlist_t *zns_string;
3927 nvlist_t *zns_uint64;
3928 nvlist_t *zns_boolean;
3932 collect_nvlist_stats(nvlist_t *nvl, zdb_nvl_stats_t *stats)
3934 nvlist_t *list, **array;
3935 nvpair_t *nvp = NULL;
3939 stats->zns_list_count++;
3941 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
3942 name = nvpair_name(nvp);
3944 switch (nvpair_type(nvp)) {
3945 case DATA_TYPE_STRING:
3946 fnvlist_add_string(stats->zns_string, name,
3947 fnvpair_value_string(nvp));
3949 case DATA_TYPE_UINT64:
3950 fnvlist_add_uint64(stats->zns_uint64, name,
3951 fnvpair_value_uint64(nvp));
3953 case DATA_TYPE_BOOLEAN:
3954 fnvlist_add_boolean(stats->zns_boolean, name);
3956 case DATA_TYPE_NVLIST:
3957 if (nvpair_value_nvlist(nvp, &list) == 0)
3958 collect_nvlist_stats(list, stats);
3960 case DATA_TYPE_NVLIST_ARRAY:
3961 if (nvpair_value_nvlist_array(nvp, &array, &items) != 0)
3964 for (i = 0; i < items; i++) {
3965 collect_nvlist_stats(array[i], stats);
3967 /* collect stats on leaf vdev */
3968 if (strcmp(name, "children") == 0) {
3971 (void) nvlist_size(array[i], &size,
3973 stats->zns_leaf_total += size;
3974 if (size > stats->zns_leaf_largest)
3975 stats->zns_leaf_largest = size;
3976 stats->zns_leaf_count++;
3981 (void) printf("skip type %d!\n", (int)nvpair_type(nvp));
3987 dump_nvlist_stats(nvlist_t *nvl, size_t cap)
3989 zdb_nvl_stats_t stats = { 0 };
3990 size_t size, sum = 0, total;
3993 /* requires nvlist with non-unique names for stat collection */
3994 VERIFY0(nvlist_alloc(&stats.zns_string, 0, 0));
3995 VERIFY0(nvlist_alloc(&stats.zns_uint64, 0, 0));
3996 VERIFY0(nvlist_alloc(&stats.zns_boolean, 0, 0));
3997 VERIFY0(nvlist_size(stats.zns_boolean, &noise, NV_ENCODE_XDR));
3999 (void) printf("\n\nZFS Label NVList Config Stats:\n");
4001 VERIFY0(nvlist_size(nvl, &total, NV_ENCODE_XDR));
4002 (void) printf(" %d bytes used, %d bytes free (using %4.1f%%)\n\n",
4003 (int)total, (int)(cap - total), 100.0 * total / cap);
4005 collect_nvlist_stats(nvl, &stats);
4007 VERIFY0(nvlist_size(stats.zns_uint64, &size, NV_ENCODE_XDR));
4010 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "integers:",
4011 (int)fnvlist_num_pairs(stats.zns_uint64),
4012 (int)size, 100.0 * size / total);
4014 VERIFY0(nvlist_size(stats.zns_string, &size, NV_ENCODE_XDR));
4017 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "strings:",
4018 (int)fnvlist_num_pairs(stats.zns_string),
4019 (int)size, 100.0 * size / total);
4021 VERIFY0(nvlist_size(stats.zns_boolean, &size, NV_ENCODE_XDR));
4024 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "booleans:",
4025 (int)fnvlist_num_pairs(stats.zns_boolean),
4026 (int)size, 100.0 * size / total);
4028 size = total - sum; /* treat remainder as nvlist overhead */
4029 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n\n", "nvlists:",
4030 stats.zns_list_count, (int)size, 100.0 * size / total);
4032 if (stats.zns_leaf_count > 0) {
4033 size_t average = stats.zns_leaf_total / stats.zns_leaf_count;
4035 (void) printf("%12s %4d %6d bytes average\n", "leaf vdevs:",
4036 stats.zns_leaf_count, (int)average);
4037 (void) printf("%24d bytes largest\n",
4038 (int)stats.zns_leaf_largest);
4040 if (dump_opt['l'] >= 3 && average > 0)
4041 (void) printf(" space for %d additional leaf vdevs\n",
4042 (int)((cap - total) / average));
4044 (void) printf("\n");
4046 nvlist_free(stats.zns_string);
4047 nvlist_free(stats.zns_uint64);
4048 nvlist_free(stats.zns_boolean);
4051 typedef struct cksum_record {
4053 boolean_t labels[VDEV_LABELS];
4058 cksum_record_compare(const void *x1, const void *x2)
4060 const cksum_record_t *l = (cksum_record_t *)x1;
4061 const cksum_record_t *r = (cksum_record_t *)x2;
4062 int arraysize = ARRAY_SIZE(l->cksum.zc_word);
4065 for (int i = 0; i < arraysize; i++) {
4066 difference = TREE_CMP(l->cksum.zc_word[i], r->cksum.zc_word[i]);
4071 return (difference);
4074 static cksum_record_t *
4075 cksum_record_alloc(zio_cksum_t *cksum, int l)
4077 cksum_record_t *rec;
4079 rec = umem_zalloc(sizeof (*rec), UMEM_NOFAIL);
4080 rec->cksum = *cksum;
4081 rec->labels[l] = B_TRUE;
4086 static cksum_record_t *
4087 cksum_record_lookup(avl_tree_t *tree, zio_cksum_t *cksum)
4089 cksum_record_t lookup = { .cksum = *cksum };
4092 return (avl_find(tree, &lookup, &where));
4095 static cksum_record_t *
4096 cksum_record_insert(avl_tree_t *tree, zio_cksum_t *cksum, int l)
4098 cksum_record_t *rec;
4100 rec = cksum_record_lookup(tree, cksum);
4102 rec->labels[l] = B_TRUE;
4104 rec = cksum_record_alloc(cksum, l);
4112 first_label(cksum_record_t *rec)
4114 for (int i = 0; i < VDEV_LABELS; i++)
4122 print_label_numbers(char *prefix, cksum_record_t *rec)
4124 printf("%s", prefix);
4125 for (int i = 0; i < VDEV_LABELS; i++)
4126 if (rec->labels[i] == B_TRUE)
4131 #define MAX_UBERBLOCK_COUNT (VDEV_UBERBLOCK_RING >> UBERBLOCK_SHIFT)
4133 typedef struct zdb_label {
4135 nvlist_t *config_nv;
4136 cksum_record_t *config;
4137 cksum_record_t *uberblocks[MAX_UBERBLOCK_COUNT];
4138 boolean_t header_printed;
4139 boolean_t read_failed;
4143 print_label_header(zdb_label_t *label, int l)
4149 if (label->header_printed == B_TRUE)
4152 (void) printf("------------------------------------\n");
4153 (void) printf("LABEL %d\n", l);
4154 (void) printf("------------------------------------\n");
4156 label->header_printed = B_TRUE;
4160 print_l2arc_header(void)
4162 (void) printf("------------------------------------\n");
4163 (void) printf("L2ARC device header\n");
4164 (void) printf("------------------------------------\n");
4168 print_l2arc_log_blocks(void)
4170 (void) printf("------------------------------------\n");
4171 (void) printf("L2ARC device log blocks\n");
4172 (void) printf("------------------------------------\n");
4176 dump_l2arc_log_entries(uint64_t log_entries,
4177 l2arc_log_ent_phys_t *le, uint64_t i)
4179 for (int j = 0; j < log_entries; j++) {
4180 dva_t dva = le[j].le_dva;
4181 (void) printf("lb[%4llu]\tle[%4d]\tDVA asize: %llu, "
4182 "vdev: %llu, offset: %llu\n",
4183 (u_longlong_t)i, j + 1,
4184 (u_longlong_t)DVA_GET_ASIZE(&dva),
4185 (u_longlong_t)DVA_GET_VDEV(&dva),
4186 (u_longlong_t)DVA_GET_OFFSET(&dva));
4187 (void) printf("|\t\t\t\tbirth: %llu\n",
4188 (u_longlong_t)le[j].le_birth);
4189 (void) printf("|\t\t\t\tlsize: %llu\n",
4190 (u_longlong_t)L2BLK_GET_LSIZE((&le[j])->le_prop));
4191 (void) printf("|\t\t\t\tpsize: %llu\n",
4192 (u_longlong_t)L2BLK_GET_PSIZE((&le[j])->le_prop));
4193 (void) printf("|\t\t\t\tcompr: %llu\n",
4194 (u_longlong_t)L2BLK_GET_COMPRESS((&le[j])->le_prop));
4195 (void) printf("|\t\t\t\tcomplevel: %llu\n",
4196 (u_longlong_t)(&le[j])->le_complevel);
4197 (void) printf("|\t\t\t\ttype: %llu\n",
4198 (u_longlong_t)L2BLK_GET_TYPE((&le[j])->le_prop));
4199 (void) printf("|\t\t\t\tprotected: %llu\n",
4200 (u_longlong_t)L2BLK_GET_PROTECTED((&le[j])->le_prop));
4201 (void) printf("|\t\t\t\tprefetch: %llu\n",
4202 (u_longlong_t)L2BLK_GET_PREFETCH((&le[j])->le_prop));
4203 (void) printf("|\t\t\t\taddress: %llu\n",
4204 (u_longlong_t)le[j].le_daddr);
4205 (void) printf("|\n");
4207 (void) printf("\n");
4211 dump_l2arc_log_blkptr(l2arc_log_blkptr_t lbps)
4213 (void) printf("|\t\tdaddr: %llu\n", (u_longlong_t)lbps.lbp_daddr);
4214 (void) printf("|\t\tpayload_asize: %llu\n",
4215 (u_longlong_t)lbps.lbp_payload_asize);
4216 (void) printf("|\t\tpayload_start: %llu\n",
4217 (u_longlong_t)lbps.lbp_payload_start);
4218 (void) printf("|\t\tlsize: %llu\n",
4219 (u_longlong_t)L2BLK_GET_LSIZE((&lbps)->lbp_prop));
4220 (void) printf("|\t\tasize: %llu\n",
4221 (u_longlong_t)L2BLK_GET_PSIZE((&lbps)->lbp_prop));
4222 (void) printf("|\t\tcompralgo: %llu\n",
4223 (u_longlong_t)L2BLK_GET_COMPRESS((&lbps)->lbp_prop));
4224 (void) printf("|\t\tcksumalgo: %llu\n",
4225 (u_longlong_t)L2BLK_GET_CHECKSUM((&lbps)->lbp_prop));
4226 (void) printf("|\n\n");
4230 dump_l2arc_log_blocks(int fd, l2arc_dev_hdr_phys_t l2dhdr,
4231 l2arc_dev_hdr_phys_t *rebuild)
4233 l2arc_log_blk_phys_t this_lb;
4235 l2arc_log_blkptr_t lbps[2];
4242 print_l2arc_log_blocks();
4243 bcopy((&l2dhdr)->dh_start_lbps, lbps, sizeof (lbps));
4245 dev.l2ad_evict = l2dhdr.dh_evict;
4246 dev.l2ad_start = l2dhdr.dh_start;
4247 dev.l2ad_end = l2dhdr.dh_end;
4249 if (l2dhdr.dh_start_lbps[0].lbp_daddr == 0) {
4250 /* no log blocks to read */
4251 if (!dump_opt['q']) {
4252 (void) printf("No log blocks to read\n");
4253 (void) printf("\n");
4257 dev.l2ad_hand = lbps[0].lbp_daddr +
4258 L2BLK_GET_PSIZE((&lbps[0])->lbp_prop);
4261 dev.l2ad_first = !!(l2dhdr.dh_flags & L2ARC_DEV_HDR_EVICT_FIRST);
4264 if (!l2arc_log_blkptr_valid(&dev, &lbps[0]))
4267 /* L2BLK_GET_PSIZE returns aligned size for log blocks */
4268 asize = L2BLK_GET_PSIZE((&lbps[0])->lbp_prop);
4269 if (pread64(fd, &this_lb, asize, lbps[0].lbp_daddr) != asize) {
4270 if (!dump_opt['q']) {
4271 (void) printf("Error while reading next log "
4277 fletcher_4_native_varsize(&this_lb, asize, &cksum);
4278 if (!ZIO_CHECKSUM_EQUAL(cksum, lbps[0].lbp_cksum)) {
4280 if (!dump_opt['q']) {
4281 (void) printf("Invalid cksum\n");
4282 dump_l2arc_log_blkptr(lbps[0]);
4287 switch (L2BLK_GET_COMPRESS((&lbps[0])->lbp_prop)) {
4288 case ZIO_COMPRESS_OFF:
4291 abd = abd_alloc_for_io(asize, B_TRUE);
4292 abd_copy_from_buf_off(abd, &this_lb, 0, asize);
4293 zio_decompress_data(L2BLK_GET_COMPRESS(
4294 (&lbps[0])->lbp_prop), abd, &this_lb,
4295 asize, sizeof (this_lb), NULL);
4300 if (this_lb.lb_magic == BSWAP_64(L2ARC_LOG_BLK_MAGIC))
4301 byteswap_uint64_array(&this_lb, sizeof (this_lb));
4302 if (this_lb.lb_magic != L2ARC_LOG_BLK_MAGIC) {
4304 (void) printf("Invalid log block magic\n\n");
4308 rebuild->dh_lb_count++;
4309 rebuild->dh_lb_asize += asize;
4310 if (dump_opt['l'] > 1 && !dump_opt['q']) {
4311 (void) printf("lb[%4llu]\tmagic: %llu\n",
4312 (u_longlong_t)rebuild->dh_lb_count,
4313 (u_longlong_t)this_lb.lb_magic);
4314 dump_l2arc_log_blkptr(lbps[0]);
4317 if (dump_opt['l'] > 2 && !dump_opt['q'])
4318 dump_l2arc_log_entries(l2dhdr.dh_log_entries,
4320 rebuild->dh_lb_count);
4322 if (l2arc_range_check_overlap(lbps[1].lbp_payload_start,
4323 lbps[0].lbp_payload_start, dev.l2ad_evict) &&
4328 lbps[1] = this_lb.lb_prev_lbp;
4331 if (!dump_opt['q']) {
4332 (void) printf("log_blk_count:\t %llu with valid cksum\n",
4333 (u_longlong_t)rebuild->dh_lb_count);
4334 (void) printf("\t\t %d with invalid cksum\n", failed);
4335 (void) printf("log_blk_asize:\t %llu\n\n",
4336 (u_longlong_t)rebuild->dh_lb_asize);
4341 dump_l2arc_header(int fd)
4343 l2arc_dev_hdr_phys_t l2dhdr, rebuild;
4344 int error = B_FALSE;
4346 bzero(&l2dhdr, sizeof (l2dhdr));
4347 bzero(&rebuild, sizeof (rebuild));
4349 if (pread64(fd, &l2dhdr, sizeof (l2dhdr),
4350 VDEV_LABEL_START_SIZE) != sizeof (l2dhdr)) {
4353 if (l2dhdr.dh_magic == BSWAP_64(L2ARC_DEV_HDR_MAGIC))
4354 byteswap_uint64_array(&l2dhdr, sizeof (l2dhdr));
4356 if (l2dhdr.dh_magic != L2ARC_DEV_HDR_MAGIC)
4361 (void) printf("L2ARC device header not found\n\n");
4362 /* Do not return an error here for backward compatibility */
4364 } else if (!dump_opt['q']) {
4365 print_l2arc_header();
4367 (void) printf(" magic: %llu\n",
4368 (u_longlong_t)l2dhdr.dh_magic);
4369 (void) printf(" version: %llu\n",
4370 (u_longlong_t)l2dhdr.dh_version);
4371 (void) printf(" pool_guid: %llu\n",
4372 (u_longlong_t)l2dhdr.dh_spa_guid);
4373 (void) printf(" flags: %llu\n",
4374 (u_longlong_t)l2dhdr.dh_flags);
4375 (void) printf(" start_lbps[0]: %llu\n",
4377 l2dhdr.dh_start_lbps[0].lbp_daddr);
4378 (void) printf(" start_lbps[1]: %llu\n",
4380 l2dhdr.dh_start_lbps[1].lbp_daddr);
4381 (void) printf(" log_blk_ent: %llu\n",
4382 (u_longlong_t)l2dhdr.dh_log_entries);
4383 (void) printf(" start: %llu\n",
4384 (u_longlong_t)l2dhdr.dh_start);
4385 (void) printf(" end: %llu\n",
4386 (u_longlong_t)l2dhdr.dh_end);
4387 (void) printf(" evict: %llu\n",
4388 (u_longlong_t)l2dhdr.dh_evict);
4389 (void) printf(" lb_asize_refcount: %llu\n",
4390 (u_longlong_t)l2dhdr.dh_lb_asize);
4391 (void) printf(" lb_count_refcount: %llu\n",
4392 (u_longlong_t)l2dhdr.dh_lb_count);
4393 (void) printf(" trim_action_time: %llu\n",
4394 (u_longlong_t)l2dhdr.dh_trim_action_time);
4395 (void) printf(" trim_state: %llu\n\n",
4396 (u_longlong_t)l2dhdr.dh_trim_state);
4399 dump_l2arc_log_blocks(fd, l2dhdr, &rebuild);
4401 * The total aligned size of log blocks and the number of log blocks
4402 * reported in the header of the device may be less than what zdb
4403 * reports by dump_l2arc_log_blocks() which emulates l2arc_rebuild().
4404 * This happens because dump_l2arc_log_blocks() lacks the memory
4405 * pressure valve that l2arc_rebuild() has. Thus, if we are on a system
4406 * with low memory, l2arc_rebuild will exit prematurely and dh_lb_asize
4407 * and dh_lb_count will be lower to begin with than what exists on the
4408 * device. This is normal and zdb should not exit with an error. The
4409 * opposite case should never happen though, the values reported in the
4410 * header should never be higher than what dump_l2arc_log_blocks() and
4411 * l2arc_rebuild() report. If this happens there is a leak in the
4412 * accounting of log blocks.
4414 if (l2dhdr.dh_lb_asize > rebuild.dh_lb_asize ||
4415 l2dhdr.dh_lb_count > rebuild.dh_lb_count)
4422 dump_config_from_label(zdb_label_t *label, size_t buflen, int l)
4427 if ((dump_opt['l'] < 3) && (first_label(label->config) != l))
4430 print_label_header(label, l);
4431 dump_nvlist(label->config_nv, 4);
4432 print_label_numbers(" labels = ", label->config);
4434 if (dump_opt['l'] >= 2)
4435 dump_nvlist_stats(label->config_nv, buflen);
4438 #define ZDB_MAX_UB_HEADER_SIZE 32
4441 dump_label_uberblocks(zdb_label_t *label, uint64_t ashift, int label_num)
4445 char header[ZDB_MAX_UB_HEADER_SIZE];
4447 vd.vdev_ashift = ashift;
4450 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) {
4451 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i);
4452 uberblock_t *ub = (void *)((char *)&label->label + uoff);
4453 cksum_record_t *rec = label->uberblocks[i];
4456 if (dump_opt['u'] >= 2) {
4457 print_label_header(label, label_num);
4458 (void) printf(" Uberblock[%d] invalid\n", i);
4463 if ((dump_opt['u'] < 3) && (first_label(rec) != label_num))
4466 if ((dump_opt['u'] < 4) &&
4467 (ub->ub_mmp_magic == MMP_MAGIC) && ub->ub_mmp_delay &&
4468 (i >= VDEV_UBERBLOCK_COUNT(&vd) - MMP_BLOCKS_PER_LABEL))
4471 print_label_header(label, label_num);
4472 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
4473 " Uberblock[%d]\n", i);
4474 dump_uberblock(ub, header, "");
4475 print_label_numbers(" labels = ", rec);
4479 static char curpath[PATH_MAX];
4482 * Iterate through the path components, recursively passing
4483 * current one's obj and remaining path until we find the obj
4487 dump_path_impl(objset_t *os, uint64_t obj, char *name)
4490 boolean_t header = B_TRUE;
4494 dmu_object_info_t doi;
4496 if ((s = strchr(name, '/')) != NULL)
4498 err = zap_lookup(os, obj, name, 8, 1, &child_obj);
4500 (void) strlcat(curpath, name, sizeof (curpath));
4503 (void) fprintf(stderr, "failed to lookup %s: %s\n",
4504 curpath, strerror(err));
4508 child_obj = ZFS_DIRENT_OBJ(child_obj);
4509 err = sa_buf_hold(os, child_obj, FTAG, &db);
4511 (void) fprintf(stderr,
4512 "failed to get SA dbuf for obj %llu: %s\n",
4513 (u_longlong_t)child_obj, strerror(err));
4516 dmu_object_info_from_db(db, &doi);
4517 sa_buf_rele(db, FTAG);
4519 if (doi.doi_bonus_type != DMU_OT_SA &&
4520 doi.doi_bonus_type != DMU_OT_ZNODE) {
4521 (void) fprintf(stderr, "invalid bonus type %d for obj %llu\n",
4522 doi.doi_bonus_type, (u_longlong_t)child_obj);
4526 if (dump_opt['v'] > 6) {
4527 (void) printf("obj=%llu %s type=%d bonustype=%d\n",
4528 (u_longlong_t)child_obj, curpath, doi.doi_type,
4529 doi.doi_bonus_type);
4532 (void) strlcat(curpath, "/", sizeof (curpath));
4534 switch (doi.doi_type) {
4535 case DMU_OT_DIRECTORY_CONTENTS:
4536 if (s != NULL && *(s + 1) != '\0')
4537 return (dump_path_impl(os, child_obj, s + 1));
4539 case DMU_OT_PLAIN_FILE_CONTENTS:
4540 dump_object(os, child_obj, dump_opt['v'], &header, NULL, 0);
4543 (void) fprintf(stderr, "object %llu has non-file/directory "
4544 "type %d\n", (u_longlong_t)obj, doi.doi_type);
4552 * Dump the blocks for the object specified by path inside the dataset.
4555 dump_path(char *ds, char *path)
4561 err = open_objset(ds, FTAG, &os);
4565 err = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &root_obj);
4567 (void) fprintf(stderr, "can't lookup root znode: %s\n",
4569 close_objset(os, FTAG);
4573 (void) snprintf(curpath, sizeof (curpath), "dataset=%s path=/", ds);
4575 err = dump_path_impl(os, root_obj, path);
4577 close_objset(os, FTAG);
4582 dump_label(const char *dev)
4584 char path[MAXPATHLEN];
4585 zdb_label_t labels[VDEV_LABELS];
4586 uint64_t psize, ashift, l2cache;
4587 struct stat64 statbuf;
4588 boolean_t config_found = B_FALSE;
4589 boolean_t error = B_FALSE;
4590 boolean_t read_l2arc_header = B_FALSE;
4591 avl_tree_t config_tree;
4592 avl_tree_t uberblock_tree;
4593 void *node, *cookie;
4596 bzero(labels, sizeof (labels));
4599 * Check if we were given absolute path and use it as is.
4600 * Otherwise if the provided vdev name doesn't point to a file,
4601 * try prepending expected disk paths and partition numbers.
4603 (void) strlcpy(path, dev, sizeof (path));
4604 if (dev[0] != '/' && stat64(path, &statbuf) != 0) {
4607 error = zfs_resolve_shortname(dev, path, MAXPATHLEN);
4608 if (error == 0 && zfs_dev_is_whole_disk(path)) {
4609 if (zfs_append_partition(path, MAXPATHLEN) == -1)
4613 if (error || (stat64(path, &statbuf) != 0)) {
4614 (void) printf("failed to find device %s, try "
4615 "specifying absolute path instead\n", dev);
4620 if ((fd = open64(path, O_RDONLY)) < 0) {
4621 (void) printf("cannot open '%s': %s\n", path, strerror(errno));
4625 if (fstat64_blk(fd, &statbuf) != 0) {
4626 (void) printf("failed to stat '%s': %s\n", path,
4632 if (S_ISBLK(statbuf.st_mode) && zfs_dev_flush(fd) != 0)
4633 (void) printf("failed to invalidate cache '%s' : %s\n", path,
4636 avl_create(&config_tree, cksum_record_compare,
4637 sizeof (cksum_record_t), offsetof(cksum_record_t, link));
4638 avl_create(&uberblock_tree, cksum_record_compare,
4639 sizeof (cksum_record_t), offsetof(cksum_record_t, link));
4641 psize = statbuf.st_size;
4642 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
4643 ashift = SPA_MINBLOCKSHIFT;
4646 * 1. Read the label from disk
4647 * 2. Unpack the configuration and insert in config tree.
4648 * 3. Traverse all uberblocks and insert in uberblock tree.
4650 for (int l = 0; l < VDEV_LABELS; l++) {
4651 zdb_label_t *label = &labels[l];
4652 char *buf = label->label.vl_vdev_phys.vp_nvlist;
4653 size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist);
4655 cksum_record_t *rec;
4659 if (pread64(fd, &label->label, sizeof (label->label),
4660 vdev_label_offset(psize, l, 0)) != sizeof (label->label)) {
4662 (void) printf("failed to read label %d\n", l);
4663 label->read_failed = B_TRUE;
4668 label->read_failed = B_FALSE;
4670 if (nvlist_unpack(buf, buflen, &config, 0) == 0) {
4671 nvlist_t *vdev_tree = NULL;
4674 if ((nvlist_lookup_nvlist(config,
4675 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
4676 (nvlist_lookup_uint64(vdev_tree,
4677 ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
4678 ashift = SPA_MINBLOCKSHIFT;
4680 if (nvlist_size(config, &size, NV_ENCODE_XDR) != 0)
4683 /* If the device is a cache device clear the header. */
4684 if (!read_l2arc_header) {
4685 if (nvlist_lookup_uint64(config,
4686 ZPOOL_CONFIG_POOL_STATE, &l2cache) == 0 &&
4687 l2cache == POOL_STATE_L2CACHE) {
4688 read_l2arc_header = B_TRUE;
4692 fletcher_4_native_varsize(buf, size, &cksum);
4693 rec = cksum_record_insert(&config_tree, &cksum, l);
4695 label->config = rec;
4696 label->config_nv = config;
4697 config_found = B_TRUE;
4702 vd.vdev_ashift = ashift;
4705 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) {
4706 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i);
4707 uberblock_t *ub = (void *)((char *)label + uoff);
4709 if (uberblock_verify(ub))
4712 fletcher_4_native_varsize(ub, sizeof (*ub), &cksum);
4713 rec = cksum_record_insert(&uberblock_tree, &cksum, l);
4715 label->uberblocks[i] = rec;
4720 * Dump the label and uberblocks.
4722 for (int l = 0; l < VDEV_LABELS; l++) {
4723 zdb_label_t *label = &labels[l];
4724 size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist);
4726 if (label->read_failed == B_TRUE)
4729 if (label->config_nv) {
4730 dump_config_from_label(label, buflen, l);
4733 (void) printf("failed to unpack label %d\n", l);
4737 dump_label_uberblocks(label, ashift, l);
4739 nvlist_free(label->config_nv);
4743 * Dump the L2ARC header, if existent.
4745 if (read_l2arc_header)
4746 error |= dump_l2arc_header(fd);
4749 while ((node = avl_destroy_nodes(&config_tree, &cookie)) != NULL)
4750 umem_free(node, sizeof (cksum_record_t));
4753 while ((node = avl_destroy_nodes(&uberblock_tree, &cookie)) != NULL)
4754 umem_free(node, sizeof (cksum_record_t));
4756 avl_destroy(&config_tree);
4757 avl_destroy(&uberblock_tree);
4761 return (config_found == B_FALSE ? 2 :
4762 (error == B_TRUE ? 1 : 0));
4765 static uint64_t dataset_feature_count[SPA_FEATURES];
4766 static uint64_t global_feature_count[SPA_FEATURES];
4767 static uint64_t remap_deadlist_count = 0;
4771 dump_one_objset(const char *dsname, void *arg)
4777 error = open_objset(dsname, FTAG, &os);
4781 for (f = 0; f < SPA_FEATURES; f++) {
4782 if (!dsl_dataset_feature_is_active(dmu_objset_ds(os), f))
4784 ASSERT(spa_feature_table[f].fi_flags &
4785 ZFEATURE_FLAG_PER_DATASET);
4786 dataset_feature_count[f]++;
4789 if (dsl_dataset_remap_deadlist_exists(dmu_objset_ds(os))) {
4790 remap_deadlist_count++;
4793 for (dsl_bookmark_node_t *dbn =
4794 avl_first(&dmu_objset_ds(os)->ds_bookmarks); dbn != NULL;
4795 dbn = AVL_NEXT(&dmu_objset_ds(os)->ds_bookmarks, dbn)) {
4796 mos_obj_refd(dbn->dbn_phys.zbm_redaction_obj);
4797 if (dbn->dbn_phys.zbm_redaction_obj != 0)
4798 global_feature_count[SPA_FEATURE_REDACTION_BOOKMARKS]++;
4799 if (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN)
4800 global_feature_count[SPA_FEATURE_BOOKMARK_WRITTEN]++;
4803 if (dsl_deadlist_is_open(&dmu_objset_ds(os)->ds_dir->dd_livelist) &&
4804 !dmu_objset_is_snapshot(os)) {
4805 global_feature_count[SPA_FEATURE_LIVELIST]++;
4809 close_objset(os, FTAG);
4810 fuid_table_destroy();
4817 #define PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
4818 typedef struct zdb_blkstats {
4824 uint64_t zb_ditto_samevdev;
4825 uint64_t zb_ditto_same_ms;
4826 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
4830 * Extended object types to report deferred frees and dedup auto-ditto blocks.
4832 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0)
4833 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1)
4834 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2)
4835 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3)
4837 static const char *zdb_ot_extname[] = {
4844 #define ZB_TOTAL DN_MAX_LEVELS
4845 #define SPA_MAX_FOR_16M (SPA_MAXBLOCKSHIFT+1)
4847 typedef struct zdb_cb {
4848 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
4849 uint64_t zcb_removing_size;
4850 uint64_t zcb_checkpoint_size;
4851 uint64_t zcb_dedup_asize;
4852 uint64_t zcb_dedup_blocks;
4853 uint64_t zcb_psize_count[SPA_MAX_FOR_16M];
4854 uint64_t zcb_lsize_count[SPA_MAX_FOR_16M];
4855 uint64_t zcb_asize_count[SPA_MAX_FOR_16M];
4856 uint64_t zcb_psize_len[SPA_MAX_FOR_16M];
4857 uint64_t zcb_lsize_len[SPA_MAX_FOR_16M];
4858 uint64_t zcb_asize_len[SPA_MAX_FOR_16M];
4859 uint64_t zcb_psize_total;
4860 uint64_t zcb_lsize_total;
4861 uint64_t zcb_asize_total;
4862 uint64_t zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
4863 uint64_t zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
4864 [BPE_PAYLOAD_SIZE + 1];
4866 hrtime_t zcb_lastprint;
4867 uint64_t zcb_totalasize;
4868 uint64_t zcb_errors[256];
4872 uint32_t **zcb_vd_obsolete_counts;
4875 /* test if two DVA offsets from same vdev are within the same metaslab */
4877 same_metaslab(spa_t *spa, uint64_t vdev, uint64_t off1, uint64_t off2)
4879 vdev_t *vd = vdev_lookup_top(spa, vdev);
4880 uint64_t ms_shift = vd->vdev_ms_shift;
4882 return ((off1 >> ms_shift) == (off2 >> ms_shift));
4886 * Used to simplify reporting of the histogram data.
4888 typedef struct one_histo {
4892 uint64_t cumulative;
4896 * The number of separate histograms processed for psize, lsize and asize.
4901 * This routine will create a fixed column size output of three different
4902 * histograms showing by blocksize of 512 - 2^ SPA_MAX_FOR_16M
4903 * the count, length and cumulative length of the psize, lsize and
4906 * All three types of blocks are listed on a single line
4908 * By default the table is printed in nicenumber format (e.g. 123K) but
4909 * if the '-P' parameter is specified then the full raw number (parseable)
4913 dump_size_histograms(zdb_cb_t *zcb)
4916 * A temporary buffer that allows us to convert a number into
4917 * a string using zdb_nicenumber to allow either raw or human
4918 * readable numbers to be output.
4923 * Define titles which are used in the headers of the tables
4924 * printed by this routine.
4926 const char blocksize_title1[] = "block";
4927 const char blocksize_title2[] = "size";
4928 const char count_title[] = "Count";
4929 const char length_title[] = "Size";
4930 const char cumulative_title[] = "Cum.";
4933 * Setup the histogram arrays (psize, lsize, and asize).
4935 one_histo_t parm_histo[NUM_HISTO];
4937 parm_histo[0].name = "psize";
4938 parm_histo[0].count = zcb->zcb_psize_count;
4939 parm_histo[0].len = zcb->zcb_psize_len;
4940 parm_histo[0].cumulative = 0;
4942 parm_histo[1].name = "lsize";
4943 parm_histo[1].count = zcb->zcb_lsize_count;
4944 parm_histo[1].len = zcb->zcb_lsize_len;
4945 parm_histo[1].cumulative = 0;
4947 parm_histo[2].name = "asize";
4948 parm_histo[2].count = zcb->zcb_asize_count;
4949 parm_histo[2].len = zcb->zcb_asize_len;
4950 parm_histo[2].cumulative = 0;
4953 (void) printf("\nBlock Size Histogram\n");
4955 * Print the first line titles
4958 (void) printf("\n%s\t", blocksize_title1);
4960 (void) printf("\n%7s ", blocksize_title1);
4962 for (int j = 0; j < NUM_HISTO; j++) {
4963 if (dump_opt['P']) {
4964 if (j < NUM_HISTO - 1) {
4965 (void) printf("%s\t\t\t", parm_histo[j].name);
4967 /* Don't print trailing spaces */
4968 (void) printf(" %s", parm_histo[j].name);
4971 if (j < NUM_HISTO - 1) {
4972 /* Left aligned strings in the output */
4973 (void) printf("%-7s ",
4974 parm_histo[j].name);
4976 /* Don't print trailing spaces */
4977 (void) printf("%s", parm_histo[j].name);
4981 (void) printf("\n");
4984 * Print the second line titles
4986 if (dump_opt['P']) {
4987 (void) printf("%s\t", blocksize_title2);
4989 (void) printf("%7s ", blocksize_title2);
4992 for (int i = 0; i < NUM_HISTO; i++) {
4993 if (dump_opt['P']) {
4994 (void) printf("%s\t%s\t%s\t",
4995 count_title, length_title, cumulative_title);
4997 (void) printf("%7s%7s%7s",
4998 count_title, length_title, cumulative_title);
5001 (void) printf("\n");
5006 for (int i = SPA_MINBLOCKSHIFT; i < SPA_MAX_FOR_16M; i++) {
5009 * Print the first column showing the blocksize
5011 zdb_nicenum((1ULL << i), numbuf, sizeof (numbuf));
5013 if (dump_opt['P']) {
5014 printf("%s", numbuf);
5016 printf("%7s:", numbuf);
5020 * Print the remaining set of 3 columns per size:
5021 * for psize, lsize and asize
5023 for (int j = 0; j < NUM_HISTO; j++) {
5024 parm_histo[j].cumulative += parm_histo[j].len[i];
5026 zdb_nicenum(parm_histo[j].count[i],
5027 numbuf, sizeof (numbuf));
5029 (void) printf("\t%s", numbuf);
5031 (void) printf("%7s", numbuf);
5033 zdb_nicenum(parm_histo[j].len[i],
5034 numbuf, sizeof (numbuf));
5036 (void) printf("\t%s", numbuf);
5038 (void) printf("%7s", numbuf);
5040 zdb_nicenum(parm_histo[j].cumulative,
5041 numbuf, sizeof (numbuf));
5043 (void) printf("\t%s", numbuf);
5045 (void) printf("%7s", numbuf);
5047 (void) printf("\n");
5052 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
5053 dmu_object_type_t type)
5055 uint64_t refcnt = 0;
5058 ASSERT(type < ZDB_OT_TOTAL);
5060 if (zilog && zil_bp_tree_add(zilog, bp) != 0)
5063 spa_config_enter(zcb->zcb_spa, SCL_CONFIG, FTAG, RW_READER);
5065 for (i = 0; i < 4; i++) {
5066 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
5067 int t = (i & 1) ? type : ZDB_OT_TOTAL;
5069 zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
5071 zb->zb_asize += BP_GET_ASIZE(bp);
5072 zb->zb_lsize += BP_GET_LSIZE(bp);
5073 zb->zb_psize += BP_GET_PSIZE(bp);
5077 * The histogram is only big enough to record blocks up to
5078 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
5081 unsigned idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
5082 idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
5083 zb->zb_psize_histogram[idx]++;
5085 zb->zb_gangs += BP_COUNT_GANG(bp);
5087 switch (BP_GET_NDVAS(bp)) {
5089 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
5090 DVA_GET_VDEV(&bp->blk_dva[1])) {
5091 zb->zb_ditto_samevdev++;
5093 if (same_metaslab(zcb->zcb_spa,
5094 DVA_GET_VDEV(&bp->blk_dva[0]),
5095 DVA_GET_OFFSET(&bp->blk_dva[0]),
5096 DVA_GET_OFFSET(&bp->blk_dva[1])))
5097 zb->zb_ditto_same_ms++;
5101 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
5102 DVA_GET_VDEV(&bp->blk_dva[1])) +
5103 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
5104 DVA_GET_VDEV(&bp->blk_dva[2])) +
5105 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
5106 DVA_GET_VDEV(&bp->blk_dva[2]));
5108 zb->zb_ditto_samevdev++;
5110 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
5111 DVA_GET_VDEV(&bp->blk_dva[1]) &&
5112 same_metaslab(zcb->zcb_spa,
5113 DVA_GET_VDEV(&bp->blk_dva[0]),
5114 DVA_GET_OFFSET(&bp->blk_dva[0]),
5115 DVA_GET_OFFSET(&bp->blk_dva[1])))
5116 zb->zb_ditto_same_ms++;
5117 else if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
5118 DVA_GET_VDEV(&bp->blk_dva[2]) &&
5119 same_metaslab(zcb->zcb_spa,
5120 DVA_GET_VDEV(&bp->blk_dva[0]),
5121 DVA_GET_OFFSET(&bp->blk_dva[0]),
5122 DVA_GET_OFFSET(&bp->blk_dva[2])))
5123 zb->zb_ditto_same_ms++;
5124 else if (DVA_GET_VDEV(&bp->blk_dva[1]) ==
5125 DVA_GET_VDEV(&bp->blk_dva[2]) &&
5126 same_metaslab(zcb->zcb_spa,
5127 DVA_GET_VDEV(&bp->blk_dva[1]),
5128 DVA_GET_OFFSET(&bp->blk_dva[1]),
5129 DVA_GET_OFFSET(&bp->blk_dva[2])))
5130 zb->zb_ditto_same_ms++;
5136 spa_config_exit(zcb->zcb_spa, SCL_CONFIG, FTAG);
5138 if (BP_IS_EMBEDDED(bp)) {
5139 zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
5140 zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
5141 [BPE_GET_PSIZE(bp)]++;
5145 * The binning histogram bins by powers of two up to
5146 * SPA_MAXBLOCKSIZE rather than creating bins for
5147 * every possible blocksize found in the pool.
5149 int bin = highbit64(BP_GET_PSIZE(bp)) - 1;
5151 zcb->zcb_psize_count[bin]++;
5152 zcb->zcb_psize_len[bin] += BP_GET_PSIZE(bp);
5153 zcb->zcb_psize_total += BP_GET_PSIZE(bp);
5155 bin = highbit64(BP_GET_LSIZE(bp)) - 1;
5157 zcb->zcb_lsize_count[bin]++;
5158 zcb->zcb_lsize_len[bin] += BP_GET_LSIZE(bp);
5159 zcb->zcb_lsize_total += BP_GET_LSIZE(bp);
5161 bin = highbit64(BP_GET_ASIZE(bp)) - 1;
5163 zcb->zcb_asize_count[bin]++;
5164 zcb->zcb_asize_len[bin] += BP_GET_ASIZE(bp);
5165 zcb->zcb_asize_total += BP_GET_ASIZE(bp);
5170 if (BP_GET_DEDUP(bp)) {
5174 ddt = ddt_select(zcb->zcb_spa, bp);
5176 dde = ddt_lookup(ddt, bp, B_FALSE);
5181 ddt_phys_t *ddp = ddt_phys_select(dde, bp);
5182 ddt_phys_decref(ddp);
5183 refcnt = ddp->ddp_refcnt;
5184 if (ddt_phys_total_refcnt(dde) == 0)
5185 ddt_remove(ddt, dde);
5190 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
5191 refcnt ? 0 : spa_min_claim_txg(zcb->zcb_spa),
5192 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
5196 zdb_blkptr_done(zio_t *zio)
5198 spa_t *spa = zio->io_spa;
5199 blkptr_t *bp = zio->io_bp;
5200 int ioerr = zio->io_error;
5201 zdb_cb_t *zcb = zio->io_private;
5202 zbookmark_phys_t *zb = &zio->io_bookmark;
5204 abd_free(zio->io_abd);
5206 mutex_enter(&spa->spa_scrub_lock);
5207 spa->spa_load_verify_bytes -= BP_GET_PSIZE(bp);
5208 cv_broadcast(&spa->spa_scrub_io_cv);
5210 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
5211 char blkbuf[BP_SPRINTF_LEN];
5213 zcb->zcb_haderrors = 1;
5214 zcb->zcb_errors[ioerr]++;
5216 if (dump_opt['b'] >= 2)
5217 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
5221 (void) printf("zdb_blkptr_cb: "
5222 "Got error %d reading "
5223 "<%llu, %llu, %lld, %llx> %s -- skipping\n",
5225 (u_longlong_t)zb->zb_objset,
5226 (u_longlong_t)zb->zb_object,
5227 (u_longlong_t)zb->zb_level,
5228 (u_longlong_t)zb->zb_blkid,
5231 mutex_exit(&spa->spa_scrub_lock);
5235 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
5236 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
5238 zdb_cb_t *zcb = arg;
5239 dmu_object_type_t type;
5240 boolean_t is_metadata;
5242 if (zb->zb_level == ZB_DNODE_LEVEL)
5245 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
5246 char blkbuf[BP_SPRINTF_LEN];
5247 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
5248 (void) printf("objset %llu object %llu "
5249 "level %lld offset 0x%llx %s\n",
5250 (u_longlong_t)zb->zb_objset,
5251 (u_longlong_t)zb->zb_object,
5252 (longlong_t)zb->zb_level,
5253 (u_longlong_t)blkid2offset(dnp, bp, zb),
5257 if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp))
5260 type = BP_GET_TYPE(bp);
5262 zdb_count_block(zcb, zilog, bp,
5263 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
5265 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
5267 if (!BP_IS_EMBEDDED(bp) &&
5268 (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
5269 size_t size = BP_GET_PSIZE(bp);
5270 abd_t *abd = abd_alloc(size, B_FALSE);
5271 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
5273 /* If it's an intent log block, failure is expected. */
5274 if (zb->zb_level == ZB_ZIL_LEVEL)
5275 flags |= ZIO_FLAG_SPECULATIVE;
5277 mutex_enter(&spa->spa_scrub_lock);
5278 while (spa->spa_load_verify_bytes > max_inflight_bytes)
5279 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
5280 spa->spa_load_verify_bytes += size;
5281 mutex_exit(&spa->spa_scrub_lock);
5283 zio_nowait(zio_read(NULL, spa, bp, abd, size,
5284 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
5287 zcb->zcb_readfails = 0;
5289 /* only call gethrtime() every 100 blocks */
5296 if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
5297 uint64_t now = gethrtime();
5299 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
5301 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
5303 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
5305 /* make sure nicenum has enough space */
5306 CTASSERT(sizeof (buf) >= NN_NUMBUF_SZ);
5308 zfs_nicebytes(bytes, buf, sizeof (buf));
5309 (void) fprintf(stderr,
5310 "\r%5s completed (%4dMB/s) "
5311 "estimated time remaining: %uhr %02umin %02usec ",
5312 buf, kb_per_sec / 1024,
5313 sec_remaining / 60 / 60,
5314 sec_remaining / 60 % 60,
5315 sec_remaining % 60);
5317 zcb->zcb_lastprint = now;
5324 zdb_leak(void *arg, uint64_t start, uint64_t size)
5328 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
5329 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
5332 static metaslab_ops_t zdb_metaslab_ops = {
5338 load_unflushed_svr_segs_cb(spa_t *spa, space_map_entry_t *sme,
5339 uint64_t txg, void *arg)
5341 spa_vdev_removal_t *svr = arg;
5343 uint64_t offset = sme->sme_offset;
5344 uint64_t size = sme->sme_run;
5346 /* skip vdevs we don't care about */
5347 if (sme->sme_vdev != svr->svr_vdev_id)
5350 vdev_t *vd = vdev_lookup_top(spa, sme->sme_vdev);
5351 metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
5352 ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE);
5354 if (txg < metaslab_unflushed_txg(ms))
5357 if (sme->sme_type == SM_ALLOC)
5358 range_tree_add(svr->svr_allocd_segs, offset, size);
5360 range_tree_remove(svr->svr_allocd_segs, offset, size);
5367 claim_segment_impl_cb(uint64_t inner_offset, vdev_t *vd, uint64_t offset,
5368 uint64_t size, void *arg)
5371 * This callback was called through a remap from
5372 * a device being removed. Therefore, the vdev that
5373 * this callback is applied to is a concrete
5376 ASSERT(vdev_is_concrete(vd));
5378 VERIFY0(metaslab_claim_impl(vd, offset, size,
5379 spa_min_claim_txg(vd->vdev_spa)));
5383 claim_segment_cb(void *arg, uint64_t offset, uint64_t size)
5387 vdev_indirect_ops.vdev_op_remap(vd, offset, size,
5388 claim_segment_impl_cb, NULL);
5392 * After accounting for all allocated blocks that are directly referenced,
5393 * we might have missed a reference to a block from a partially complete
5394 * (and thus unused) indirect mapping object. We perform a secondary pass
5395 * through the metaslabs we have already mapped and claim the destination
5399 zdb_claim_removing(spa_t *spa, zdb_cb_t *zcb)
5404 if (spa->spa_vdev_removal == NULL)
5407 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5409 spa_vdev_removal_t *svr = spa->spa_vdev_removal;
5410 vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
5411 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
5413 ASSERT0(range_tree_space(svr->svr_allocd_segs));
5415 range_tree_t *allocs = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
5416 for (uint64_t msi = 0; msi < vd->vdev_ms_count; msi++) {
5417 metaslab_t *msp = vd->vdev_ms[msi];
5419 ASSERT0(range_tree_space(allocs));
5420 if (msp->ms_sm != NULL)
5421 VERIFY0(space_map_load(msp->ms_sm, allocs, SM_ALLOC));
5422 range_tree_vacate(allocs, range_tree_add, svr->svr_allocd_segs);
5424 range_tree_destroy(allocs);
5426 iterate_through_spacemap_logs(spa, load_unflushed_svr_segs_cb, svr);
5429 * Clear everything past what has been synced,
5430 * because we have not allocated mappings for
5433 range_tree_clear(svr->svr_allocd_segs,
5434 vdev_indirect_mapping_max_offset(vim),
5435 vd->vdev_asize - vdev_indirect_mapping_max_offset(vim));
5437 zcb->zcb_removing_size += range_tree_space(svr->svr_allocd_segs);
5438 range_tree_vacate(svr->svr_allocd_segs, claim_segment_cb, vd);
5440 spa_config_exit(spa, SCL_CONFIG, FTAG);
5445 increment_indirect_mapping_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed,
5448 zdb_cb_t *zcb = arg;
5449 spa_t *spa = zcb->zcb_spa;
5451 const dva_t *dva = &bp->blk_dva[0];
5454 ASSERT(!dump_opt['L']);
5455 ASSERT3U(BP_GET_NDVAS(bp), ==, 1);
5457 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
5458 vd = vdev_lookup_top(zcb->zcb_spa, DVA_GET_VDEV(dva));
5459 ASSERT3P(vd, !=, NULL);
5460 spa_config_exit(spa, SCL_VDEV, FTAG);
5462 ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0);
5463 ASSERT3P(zcb->zcb_vd_obsolete_counts[vd->vdev_id], !=, NULL);
5465 vdev_indirect_mapping_increment_obsolete_count(
5466 vd->vdev_indirect_mapping,
5467 DVA_GET_OFFSET(dva), DVA_GET_ASIZE(dva),
5468 zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
5474 zdb_load_obsolete_counts(vdev_t *vd)
5476 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
5477 spa_t *spa = vd->vdev_spa;
5478 spa_condensing_indirect_phys_t *scip =
5479 &spa->spa_condensing_indirect_phys;
5480 uint64_t obsolete_sm_object;
5483 VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object));
5484 EQUIV(obsolete_sm_object != 0, vd->vdev_obsolete_sm != NULL);
5485 counts = vdev_indirect_mapping_load_obsolete_counts(vim);
5486 if (vd->vdev_obsolete_sm != NULL) {
5487 vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
5488 vd->vdev_obsolete_sm);
5490 if (scip->scip_vdev == vd->vdev_id &&
5491 scip->scip_prev_obsolete_sm_object != 0) {
5492 space_map_t *prev_obsolete_sm = NULL;
5493 VERIFY0(space_map_open(&prev_obsolete_sm, spa->spa_meta_objset,
5494 scip->scip_prev_obsolete_sm_object, 0, vd->vdev_asize, 0));
5495 vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
5497 space_map_close(prev_obsolete_sm);
5503 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
5510 ASSERT(!dump_opt['L']);
5512 bzero(&ddb, sizeof (ddb));
5513 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
5515 ddt_phys_t *ddp = dde.dde_phys;
5517 if (ddb.ddb_class == DDT_CLASS_UNIQUE)
5520 ASSERT(ddt_phys_total_refcnt(&dde) > 1);
5522 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
5523 if (ddp->ddp_phys_birth == 0)
5525 ddt_bp_create(ddb.ddb_checksum,
5526 &dde.dde_key, ddp, &blk);
5527 if (p == DDT_PHYS_DITTO) {
5528 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
5530 zcb->zcb_dedup_asize +=
5531 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
5532 zcb->zcb_dedup_blocks++;
5535 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
5537 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
5541 ASSERT(error == ENOENT);
5544 typedef struct checkpoint_sm_exclude_entry_arg {
5546 uint64_t cseea_checkpoint_size;
5547 } checkpoint_sm_exclude_entry_arg_t;
5550 checkpoint_sm_exclude_entry_cb(space_map_entry_t *sme, void *arg)
5552 checkpoint_sm_exclude_entry_arg_t *cseea = arg;
5553 vdev_t *vd = cseea->cseea_vd;
5554 metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift];
5555 uint64_t end = sme->sme_offset + sme->sme_run;
5557 ASSERT(sme->sme_type == SM_FREE);
5560 * Since the vdev_checkpoint_sm exists in the vdev level
5561 * and the ms_sm space maps exist in the metaslab level,
5562 * an entry in the checkpoint space map could theoretically
5563 * cross the boundaries of the metaslab that it belongs.
5565 * In reality, because of the way that we populate and
5566 * manipulate the checkpoint's space maps currently,
5567 * there shouldn't be any entries that cross metaslabs.
5568 * Hence the assertion below.
5570 * That said, there is no fundamental requirement that
5571 * the checkpoint's space map entries should not cross
5572 * metaslab boundaries. So if needed we could add code
5573 * that handles metaslab-crossing segments in the future.
5575 VERIFY3U(sme->sme_offset, >=, ms->ms_start);
5576 VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
5579 * By removing the entry from the allocated segments we
5580 * also verify that the entry is there to begin with.
5582 mutex_enter(&ms->ms_lock);
5583 range_tree_remove(ms->ms_allocatable, sme->sme_offset, sme->sme_run);
5584 mutex_exit(&ms->ms_lock);
5586 cseea->cseea_checkpoint_size += sme->sme_run;
5591 zdb_leak_init_vdev_exclude_checkpoint(vdev_t *vd, zdb_cb_t *zcb)
5593 spa_t *spa = vd->vdev_spa;
5594 space_map_t *checkpoint_sm = NULL;
5595 uint64_t checkpoint_sm_obj;
5598 * If there is no vdev_top_zap, we are in a pool whose
5599 * version predates the pool checkpoint feature.
5601 if (vd->vdev_top_zap == 0)
5605 * If there is no reference of the vdev_checkpoint_sm in
5606 * the vdev_top_zap, then one of the following scenarios
5609 * 1] There is no checkpoint
5610 * 2] There is a checkpoint, but no checkpointed blocks
5611 * have been freed yet
5612 * 3] The current vdev is indirect
5614 * In these cases we return immediately.
5616 if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
5617 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
5620 VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
5621 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1,
5622 &checkpoint_sm_obj));
5624 checkpoint_sm_exclude_entry_arg_t cseea;
5625 cseea.cseea_vd = vd;
5626 cseea.cseea_checkpoint_size = 0;
5628 VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
5629 checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
5631 VERIFY0(space_map_iterate(checkpoint_sm,
5632 space_map_length(checkpoint_sm),
5633 checkpoint_sm_exclude_entry_cb, &cseea));
5634 space_map_close(checkpoint_sm);
5636 zcb->zcb_checkpoint_size += cseea.cseea_checkpoint_size;
5640 zdb_leak_init_exclude_checkpoint(spa_t *spa, zdb_cb_t *zcb)
5642 ASSERT(!dump_opt['L']);
5644 vdev_t *rvd = spa->spa_root_vdev;
5645 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
5646 ASSERT3U(c, ==, rvd->vdev_child[c]->vdev_id);
5647 zdb_leak_init_vdev_exclude_checkpoint(rvd->vdev_child[c], zcb);
5652 count_unflushed_space_cb(spa_t *spa, space_map_entry_t *sme,
5653 uint64_t txg, void *arg)
5655 int64_t *ualloc_space = arg;
5657 uint64_t offset = sme->sme_offset;
5658 uint64_t vdev_id = sme->sme_vdev;
5660 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
5661 if (!vdev_is_concrete(vd))
5664 metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
5665 ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE);
5667 if (txg < metaslab_unflushed_txg(ms))
5670 if (sme->sme_type == SM_ALLOC)
5671 *ualloc_space += sme->sme_run;
5673 *ualloc_space -= sme->sme_run;
5679 get_unflushed_alloc_space(spa_t *spa)
5684 int64_t ualloc_space = 0;
5685 iterate_through_spacemap_logs(spa, count_unflushed_space_cb,
5687 return (ualloc_space);
5691 load_unflushed_cb(spa_t *spa, space_map_entry_t *sme, uint64_t txg, void *arg)
5693 maptype_t *uic_maptype = arg;
5695 uint64_t offset = sme->sme_offset;
5696 uint64_t size = sme->sme_run;
5697 uint64_t vdev_id = sme->sme_vdev;
5699 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
5701 /* skip indirect vdevs */
5702 if (!vdev_is_concrete(vd))
5705 metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
5707 ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE);
5708 ASSERT(*uic_maptype == SM_ALLOC || *uic_maptype == SM_FREE);
5710 if (txg < metaslab_unflushed_txg(ms))
5713 if (*uic_maptype == sme->sme_type)
5714 range_tree_add(ms->ms_allocatable, offset, size);
5716 range_tree_remove(ms->ms_allocatable, offset, size);
5722 load_unflushed_to_ms_allocatables(spa_t *spa, maptype_t maptype)
5724 iterate_through_spacemap_logs(spa, load_unflushed_cb, &maptype);
5728 load_concrete_ms_allocatable_trees(spa_t *spa, maptype_t maptype)
5730 vdev_t *rvd = spa->spa_root_vdev;
5731 for (uint64_t i = 0; i < rvd->vdev_children; i++) {
5732 vdev_t *vd = rvd->vdev_child[i];
5734 ASSERT3U(i, ==, vd->vdev_id);
5736 if (vd->vdev_ops == &vdev_indirect_ops)
5739 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
5740 metaslab_t *msp = vd->vdev_ms[m];
5742 (void) fprintf(stderr,
5743 "\rloading concrete vdev %llu, "
5744 "metaslab %llu of %llu ...",
5745 (longlong_t)vd->vdev_id,
5746 (longlong_t)msp->ms_id,
5747 (longlong_t)vd->vdev_ms_count);
5749 mutex_enter(&msp->ms_lock);
5750 range_tree_vacate(msp->ms_allocatable, NULL, NULL);
5753 * We don't want to spend the CPU manipulating the
5754 * size-ordered tree, so clear the range_tree ops.
5756 msp->ms_allocatable->rt_ops = NULL;
5758 if (msp->ms_sm != NULL) {
5759 VERIFY0(space_map_load(msp->ms_sm,
5760 msp->ms_allocatable, maptype));
5762 if (!msp->ms_loaded)
5763 msp->ms_loaded = B_TRUE;
5764 mutex_exit(&msp->ms_lock);
5768 load_unflushed_to_ms_allocatables(spa, maptype);
5772 * vm_idxp is an in-out parameter which (for indirect vdevs) is the
5773 * index in vim_entries that has the first entry in this metaslab.
5774 * On return, it will be set to the first entry after this metaslab.
5777 load_indirect_ms_allocatable_tree(vdev_t *vd, metaslab_t *msp,
5780 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
5782 mutex_enter(&msp->ms_lock);
5783 range_tree_vacate(msp->ms_allocatable, NULL, NULL);
5786 * We don't want to spend the CPU manipulating the
5787 * size-ordered tree, so clear the range_tree ops.
5789 msp->ms_allocatable->rt_ops = NULL;
5791 for (; *vim_idxp < vdev_indirect_mapping_num_entries(vim);
5793 vdev_indirect_mapping_entry_phys_t *vimep =
5794 &vim->vim_entries[*vim_idxp];
5795 uint64_t ent_offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
5796 uint64_t ent_len = DVA_GET_ASIZE(&vimep->vimep_dst);
5797 ASSERT3U(ent_offset, >=, msp->ms_start);
5798 if (ent_offset >= msp->ms_start + msp->ms_size)
5802 * Mappings do not cross metaslab boundaries,
5803 * because we create them by walking the metaslabs.
5805 ASSERT3U(ent_offset + ent_len, <=,
5806 msp->ms_start + msp->ms_size);
5807 range_tree_add(msp->ms_allocatable, ent_offset, ent_len);
5810 if (!msp->ms_loaded)
5811 msp->ms_loaded = B_TRUE;
5812 mutex_exit(&msp->ms_lock);
5816 zdb_leak_init_prepare_indirect_vdevs(spa_t *spa, zdb_cb_t *zcb)
5818 ASSERT(!dump_opt['L']);
5820 vdev_t *rvd = spa->spa_root_vdev;
5821 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
5822 vdev_t *vd = rvd->vdev_child[c];
5824 ASSERT3U(c, ==, vd->vdev_id);
5826 if (vd->vdev_ops != &vdev_indirect_ops)
5830 * Note: we don't check for mapping leaks on
5831 * removing vdevs because their ms_allocatable's
5832 * are used to look for leaks in allocated space.
5834 zcb->zcb_vd_obsolete_counts[c] = zdb_load_obsolete_counts(vd);
5837 * Normally, indirect vdevs don't have any
5838 * metaslabs. We want to set them up for
5841 VERIFY0(vdev_metaslab_init(vd, 0));
5843 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
5844 uint64_t vim_idx = 0;
5845 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
5847 (void) fprintf(stderr,
5848 "\rloading indirect vdev %llu, "
5849 "metaslab %llu of %llu ...",
5850 (longlong_t)vd->vdev_id,
5851 (longlong_t)vd->vdev_ms[m]->ms_id,
5852 (longlong_t)vd->vdev_ms_count);
5854 load_indirect_ms_allocatable_tree(vd, vd->vdev_ms[m],
5857 ASSERT3U(vim_idx, ==, vdev_indirect_mapping_num_entries(vim));
5862 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
5869 dsl_pool_t *dp = spa->spa_dsl_pool;
5870 vdev_t *rvd = spa->spa_root_vdev;
5873 * We are going to be changing the meaning of the metaslab's
5874 * ms_allocatable. Ensure that the allocator doesn't try to
5877 spa->spa_normal_class->mc_ops = &zdb_metaslab_ops;
5878 spa->spa_log_class->mc_ops = &zdb_metaslab_ops;
5880 zcb->zcb_vd_obsolete_counts =
5881 umem_zalloc(rvd->vdev_children * sizeof (uint32_t *),
5885 * For leak detection, we overload the ms_allocatable trees
5886 * to contain allocated segments instead of free segments.
5887 * As a result, we can't use the normal metaslab_load/unload
5890 zdb_leak_init_prepare_indirect_vdevs(spa, zcb);
5891 load_concrete_ms_allocatable_trees(spa, SM_ALLOC);
5894 * On load_concrete_ms_allocatable_trees() we loaded all the
5895 * allocated entries from the ms_sm to the ms_allocatable for
5896 * each metaslab. If the pool has a checkpoint or is in the
5897 * middle of discarding a checkpoint, some of these blocks
5898 * may have been freed but their ms_sm may not have been
5899 * updated because they are referenced by the checkpoint. In
5900 * order to avoid false-positives during leak-detection, we
5901 * go through the vdev's checkpoint space map and exclude all
5902 * its entries from their relevant ms_allocatable.
5904 * We also aggregate the space held by the checkpoint and add
5905 * it to zcb_checkpoint_size.
5907 * Note that at this point we are also verifying that all the
5908 * entries on the checkpoint_sm are marked as allocated in
5909 * the ms_sm of their relevant metaslab.
5910 * [see comment in checkpoint_sm_exclude_entry_cb()]
5912 zdb_leak_init_exclude_checkpoint(spa, zcb);
5913 ASSERT3U(zcb->zcb_checkpoint_size, ==, spa_get_checkpoint_space(spa));
5915 /* for cleaner progress output */
5916 (void) fprintf(stderr, "\n");
5918 if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
5919 ASSERT(spa_feature_is_enabled(spa,
5920 SPA_FEATURE_DEVICE_REMOVAL));
5921 (void) bpobj_iterate_nofree(&dp->dp_obsolete_bpobj,
5922 increment_indirect_mapping_cb, zcb, NULL);
5925 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5926 zdb_ddt_leak_init(spa, zcb);
5927 spa_config_exit(spa, SCL_CONFIG, FTAG);
5931 zdb_check_for_obsolete_leaks(vdev_t *vd, zdb_cb_t *zcb)
5933 boolean_t leaks = B_FALSE;
5934 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
5935 uint64_t total_leaked = 0;
5936 boolean_t are_precise = B_FALSE;
5938 ASSERT(vim != NULL);
5940 for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
5941 vdev_indirect_mapping_entry_phys_t *vimep =
5942 &vim->vim_entries[i];
5943 uint64_t obsolete_bytes = 0;
5944 uint64_t offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
5945 metaslab_t *msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
5948 * This is not very efficient but it's easy to
5949 * verify correctness.
5951 for (uint64_t inner_offset = 0;
5952 inner_offset < DVA_GET_ASIZE(&vimep->vimep_dst);
5953 inner_offset += 1 << vd->vdev_ashift) {
5954 if (range_tree_contains(msp->ms_allocatable,
5955 offset + inner_offset, 1 << vd->vdev_ashift)) {
5956 obsolete_bytes += 1 << vd->vdev_ashift;
5960 int64_t bytes_leaked = obsolete_bytes -
5961 zcb->zcb_vd_obsolete_counts[vd->vdev_id][i];
5962 ASSERT3U(DVA_GET_ASIZE(&vimep->vimep_dst), >=,
5963 zcb->zcb_vd_obsolete_counts[vd->vdev_id][i]);
5965 VERIFY0(vdev_obsolete_counts_are_precise(vd, &are_precise));
5966 if (bytes_leaked != 0 && (are_precise || dump_opt['d'] >= 5)) {
5967 (void) printf("obsolete indirect mapping count "
5968 "mismatch on %llu:%llx:%llx : %llx bytes leaked\n",
5969 (u_longlong_t)vd->vdev_id,
5970 (u_longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
5971 (u_longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
5972 (u_longlong_t)bytes_leaked);
5974 total_leaked += ABS(bytes_leaked);
5977 VERIFY0(vdev_obsolete_counts_are_precise(vd, &are_precise));
5978 if (!are_precise && total_leaked > 0) {
5979 int pct_leaked = total_leaked * 100 /
5980 vdev_indirect_mapping_bytes_mapped(vim);
5981 (void) printf("cannot verify obsolete indirect mapping "
5982 "counts of vdev %llu because precise feature was not "
5983 "enabled when it was removed: %d%% (%llx bytes) of mapping"
5985 (u_longlong_t)vd->vdev_id, pct_leaked,
5986 (u_longlong_t)total_leaked);
5987 } else if (total_leaked > 0) {
5988 (void) printf("obsolete indirect mapping count mismatch "
5989 "for vdev %llu -- %llx total bytes mismatched\n",
5990 (u_longlong_t)vd->vdev_id,
5991 (u_longlong_t)total_leaked);
5995 vdev_indirect_mapping_free_obsolete_counts(vim,
5996 zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
5997 zcb->zcb_vd_obsolete_counts[vd->vdev_id] = NULL;
6003 zdb_leak_fini(spa_t *spa, zdb_cb_t *zcb)
6008 boolean_t leaks = B_FALSE;
6009 vdev_t *rvd = spa->spa_root_vdev;
6010 for (unsigned c = 0; c < rvd->vdev_children; c++) {
6011 vdev_t *vd = rvd->vdev_child[c];
6012 metaslab_group_t *mg __maybe_unused = vd->vdev_mg;
6014 if (zcb->zcb_vd_obsolete_counts[c] != NULL) {
6015 leaks |= zdb_check_for_obsolete_leaks(vd, zcb);
6018 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
6019 metaslab_t *msp = vd->vdev_ms[m];
6020 ASSERT3P(mg, ==, msp->ms_group);
6023 * ms_allocatable has been overloaded
6024 * to contain allocated segments. Now that
6025 * we finished traversing all blocks, any
6026 * block that remains in the ms_allocatable
6027 * represents an allocated block that we
6028 * did not claim during the traversal.
6029 * Claimed blocks would have been removed
6030 * from the ms_allocatable. For indirect
6031 * vdevs, space remaining in the tree
6032 * represents parts of the mapping that are
6033 * not referenced, which is not a bug.
6035 if (vd->vdev_ops == &vdev_indirect_ops) {
6036 range_tree_vacate(msp->ms_allocatable,
6039 range_tree_vacate(msp->ms_allocatable,
6042 if (msp->ms_loaded) {
6043 msp->ms_loaded = B_FALSE;
6048 umem_free(zcb->zcb_vd_obsolete_counts,
6049 rvd->vdev_children * sizeof (uint32_t *));
6050 zcb->zcb_vd_obsolete_counts = NULL;
6057 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6059 zdb_cb_t *zcb = arg;
6061 if (dump_opt['b'] >= 5) {
6062 char blkbuf[BP_SPRINTF_LEN];
6063 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
6064 (void) printf("[%s] %s\n",
6065 "deferred free", blkbuf);
6067 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
6072 * Iterate over livelists which have been destroyed by the user but
6073 * are still present in the MOS, waiting to be freed
6076 iterate_deleted_livelists(spa_t *spa, ll_iter_t func, void *arg)
6078 objset_t *mos = spa->spa_meta_objset;
6080 int err = zap_lookup(mos, DMU_POOL_DIRECTORY_OBJECT,
6081 DMU_POOL_DELETED_CLONES, sizeof (uint64_t), 1, &zap_obj);
6087 zap_attribute_t attr;
6089 /* NULL out os prior to dsl_deadlist_open in case it's garbage */
6091 for (zap_cursor_init(&zc, mos, zap_obj);
6092 zap_cursor_retrieve(&zc, &attr) == 0;
6093 (void) zap_cursor_advance(&zc)) {
6094 dsl_deadlist_open(&ll, mos, attr.za_first_integer);
6096 dsl_deadlist_close(&ll);
6098 zap_cursor_fini(&zc);
6102 bpobj_count_block_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed,
6106 return (count_block_cb(arg, bp, tx));
6110 livelist_entry_count_blocks_cb(void *args, dsl_deadlist_entry_t *dle)
6112 zdb_cb_t *zbc = args;
6114 bplist_create(&blks);
6115 /* determine which blocks have been alloc'd but not freed */
6116 VERIFY0(dsl_process_sub_livelist(&dle->dle_bpobj, &blks, NULL, NULL));
6117 /* count those blocks */
6118 (void) bplist_iterate(&blks, count_block_cb, zbc, NULL);
6119 bplist_destroy(&blks);
6124 livelist_count_blocks(dsl_deadlist_t *ll, void *arg)
6126 dsl_deadlist_iterate(ll, livelist_entry_count_blocks_cb, arg);
6130 * Count the blocks in the livelists that have been destroyed by the user
6131 * but haven't yet been freed.
6134 deleted_livelists_count_blocks(spa_t *spa, zdb_cb_t *zbc)
6136 iterate_deleted_livelists(spa, livelist_count_blocks, zbc);
6140 dump_livelist_cb(dsl_deadlist_t *ll, void *arg)
6142 ASSERT3P(arg, ==, NULL);
6143 global_feature_count[SPA_FEATURE_LIVELIST]++;
6144 dump_blkptr_list(ll, "Deleted Livelist");
6145 dsl_deadlist_iterate(ll, sublivelist_verify_lightweight, NULL);
6149 * Print out, register object references to, and increment feature counts for
6150 * livelists that have been destroyed by the user but haven't yet been freed.
6153 deleted_livelists_dump_mos(spa_t *spa)
6156 objset_t *mos = spa->spa_meta_objset;
6157 int err = zap_lookup(mos, DMU_POOL_DIRECTORY_OBJECT,
6158 DMU_POOL_DELETED_CLONES, sizeof (uint64_t), 1, &zap_obj);
6161 mos_obj_refd(zap_obj);
6162 iterate_deleted_livelists(spa, dump_livelist_cb, NULL);
6166 dump_block_stats(spa_t *spa)
6169 zdb_blkstats_t *zb, *tzb;
6170 uint64_t norm_alloc, norm_space, total_alloc, total_found;
6171 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
6172 TRAVERSE_NO_DECRYPT | TRAVERSE_HARD;
6173 boolean_t leaks = B_FALSE;
6175 bp_embedded_type_t i;
6177 bzero(&zcb, sizeof (zcb));
6178 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
6179 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
6180 (dump_opt['c'] == 1) ? "metadata " : "",
6181 dump_opt['c'] ? "checksums " : "",
6182 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
6183 !dump_opt['L'] ? "nothing leaked " : "");
6186 * When leak detection is enabled we load all space maps as SM_ALLOC
6187 * maps, then traverse the pool claiming each block we discover. If
6188 * the pool is perfectly consistent, the segment trees will be empty
6189 * when we're done. Anything left over is a leak; any block we can't
6190 * claim (because it's not part of any space map) is a double
6191 * allocation, reference to a freed block, or an unclaimed log block.
6193 * When leak detection is disabled (-L option) we still traverse the
6194 * pool claiming each block we discover, but we skip opening any space
6197 bzero(&zcb, sizeof (zdb_cb_t));
6198 zdb_leak_init(spa, &zcb);
6201 * If there's a deferred-free bplist, process that first.
6203 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
6204 bpobj_count_block_cb, &zcb, NULL);
6206 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
6207 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
6208 bpobj_count_block_cb, &zcb, NULL);
6211 zdb_claim_removing(spa, &zcb);
6213 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
6214 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
6215 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
6219 deleted_livelists_count_blocks(spa, &zcb);
6221 if (dump_opt['c'] > 1)
6222 flags |= TRAVERSE_PREFETCH_DATA;
6224 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
6225 zcb.zcb_totalasize += metaslab_class_get_alloc(spa_special_class(spa));
6226 zcb.zcb_totalasize += metaslab_class_get_alloc(spa_dedup_class(spa));
6227 zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
6228 err = traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
6231 * If we've traversed the data blocks then we need to wait for those
6232 * I/Os to complete. We leverage "The Godfather" zio to wait on
6233 * all async I/Os to complete.
6235 if (dump_opt['c']) {
6236 for (c = 0; c < max_ncpus; c++) {
6237 (void) zio_wait(spa->spa_async_zio_root[c]);
6238 spa->spa_async_zio_root[c] = zio_root(spa, NULL, NULL,
6239 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
6240 ZIO_FLAG_GODFATHER);
6243 ASSERT0(spa->spa_load_verify_bytes);
6246 * Done after zio_wait() since zcb_haderrors is modified in
6249 zcb.zcb_haderrors |= err;
6251 if (zcb.zcb_haderrors) {
6252 (void) printf("\nError counts:\n\n");
6253 (void) printf("\t%5s %s\n", "errno", "count");
6254 for (e = 0; e < 256; e++) {
6255 if (zcb.zcb_errors[e] != 0) {
6256 (void) printf("\t%5d %llu\n",
6257 e, (u_longlong_t)zcb.zcb_errors[e]);
6263 * Report any leaked segments.
6265 leaks |= zdb_leak_fini(spa, &zcb);
6267 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
6269 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6270 norm_space = metaslab_class_get_space(spa_normal_class(spa));
6272 total_alloc = norm_alloc +
6273 metaslab_class_get_alloc(spa_log_class(spa)) +
6274 metaslab_class_get_alloc(spa_special_class(spa)) +
6275 metaslab_class_get_alloc(spa_dedup_class(spa)) +
6276 get_unflushed_alloc_space(spa);
6277 total_found = tzb->zb_asize - zcb.zcb_dedup_asize +
6278 zcb.zcb_removing_size + zcb.zcb_checkpoint_size;
6280 if (total_found == total_alloc && !dump_opt['L']) {
6281 (void) printf("\n\tNo leaks (block sum matches space"
6282 " maps exactly)\n");
6283 } else if (!dump_opt['L']) {
6284 (void) printf("block traversal size %llu != alloc %llu "
6286 (u_longlong_t)total_found,
6287 (u_longlong_t)total_alloc,
6288 (dump_opt['L']) ? "unreachable" : "leaked",
6289 (longlong_t)(total_alloc - total_found));
6293 if (tzb->zb_count == 0)
6296 (void) printf("\n");
6297 (void) printf("\t%-16s %14llu\n", "bp count:",
6298 (u_longlong_t)tzb->zb_count);
6299 (void) printf("\t%-16s %14llu\n", "ganged count:",
6300 (longlong_t)tzb->zb_gangs);
6301 (void) printf("\t%-16s %14llu avg: %6llu\n", "bp logical:",
6302 (u_longlong_t)tzb->zb_lsize,
6303 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
6304 (void) printf("\t%-16s %14llu avg: %6llu compression: %6.2f\n",
6305 "bp physical:", (u_longlong_t)tzb->zb_psize,
6306 (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
6307 (double)tzb->zb_lsize / tzb->zb_psize);
6308 (void) printf("\t%-16s %14llu avg: %6llu compression: %6.2f\n",
6309 "bp allocated:", (u_longlong_t)tzb->zb_asize,
6310 (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
6311 (double)tzb->zb_lsize / tzb->zb_asize);
6312 (void) printf("\t%-16s %14llu ref>1: %6llu deduplication: %6.2f\n",
6313 "bp deduped:", (u_longlong_t)zcb.zcb_dedup_asize,
6314 (u_longlong_t)zcb.zcb_dedup_blocks,
6315 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
6316 (void) printf("\t%-16s %14llu used: %5.2f%%\n", "Normal class:",
6317 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
6319 if (spa_special_class(spa)->mc_rotor != NULL) {
6320 uint64_t alloc = metaslab_class_get_alloc(
6321 spa_special_class(spa));
6322 uint64_t space = metaslab_class_get_space(
6323 spa_special_class(spa));
6325 (void) printf("\t%-16s %14llu used: %5.2f%%\n",
6326 "Special class", (u_longlong_t)alloc,
6327 100.0 * alloc / space);
6330 if (spa_dedup_class(spa)->mc_rotor != NULL) {
6331 uint64_t alloc = metaslab_class_get_alloc(
6332 spa_dedup_class(spa));
6333 uint64_t space = metaslab_class_get_space(
6334 spa_dedup_class(spa));
6336 (void) printf("\t%-16s %14llu used: %5.2f%%\n",
6337 "Dedup class", (u_longlong_t)alloc,
6338 100.0 * alloc / space);
6341 for (i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
6342 if (zcb.zcb_embedded_blocks[i] == 0)
6344 (void) printf("\n");
6345 (void) printf("\tadditional, non-pointer bps of type %u: "
6347 i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
6349 if (dump_opt['b'] >= 3) {
6350 (void) printf("\t number of (compressed) bytes: "
6352 dump_histogram(zcb.zcb_embedded_histogram[i],
6353 sizeof (zcb.zcb_embedded_histogram[i]) /
6354 sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
6358 if (tzb->zb_ditto_samevdev != 0) {
6359 (void) printf("\tDittoed blocks on same vdev: %llu\n",
6360 (longlong_t)tzb->zb_ditto_samevdev);
6362 if (tzb->zb_ditto_same_ms != 0) {
6363 (void) printf("\tDittoed blocks in same metaslab: %llu\n",
6364 (longlong_t)tzb->zb_ditto_same_ms);
6367 for (uint64_t v = 0; v < spa->spa_root_vdev->vdev_children; v++) {
6368 vdev_t *vd = spa->spa_root_vdev->vdev_child[v];
6369 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
6376 zdb_nicenum(vdev_indirect_mapping_num_entries(vim),
6377 mem, vdev_indirect_mapping_size(vim));
6379 (void) printf("\tindirect vdev id %llu has %llu segments "
6381 (longlong_t)vd->vdev_id,
6382 (longlong_t)vdev_indirect_mapping_num_entries(vim), mem);
6385 if (dump_opt['b'] >= 2) {
6387 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
6388 "\t avg\t comp\t%%Total\tType\n");
6390 for (t = 0; t <= ZDB_OT_TOTAL; t++) {
6391 char csize[32], lsize[32], psize[32], asize[32];
6392 char avg[32], gang[32];
6393 const char *typename;
6395 /* make sure nicenum has enough space */
6396 CTASSERT(sizeof (csize) >= NN_NUMBUF_SZ);
6397 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
6398 CTASSERT(sizeof (psize) >= NN_NUMBUF_SZ);
6399 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
6400 CTASSERT(sizeof (avg) >= NN_NUMBUF_SZ);
6401 CTASSERT(sizeof (gang) >= NN_NUMBUF_SZ);
6403 if (t < DMU_OT_NUMTYPES)
6404 typename = dmu_ot[t].ot_name;
6406 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
6408 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
6409 (void) printf("%6s\t%5s\t%5s\t%5s"
6410 "\t%5s\t%5s\t%6s\t%s\n",
6422 for (l = ZB_TOTAL - 1; l >= -1; l--) {
6423 level = (l == -1 ? ZB_TOTAL : l);
6424 zb = &zcb.zcb_type[level][t];
6426 if (zb->zb_asize == 0)
6429 if (dump_opt['b'] < 3 && level != ZB_TOTAL)
6432 if (level == 0 && zb->zb_asize ==
6433 zcb.zcb_type[ZB_TOTAL][t].zb_asize)
6436 zdb_nicenum(zb->zb_count, csize,
6438 zdb_nicenum(zb->zb_lsize, lsize,
6440 zdb_nicenum(zb->zb_psize, psize,
6442 zdb_nicenum(zb->zb_asize, asize,
6444 zdb_nicenum(zb->zb_asize / zb->zb_count, avg,
6446 zdb_nicenum(zb->zb_gangs, gang, sizeof (gang));
6448 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
6450 csize, lsize, psize, asize, avg,
6451 (double)zb->zb_lsize / zb->zb_psize,
6452 100.0 * zb->zb_asize / tzb->zb_asize);
6454 if (level == ZB_TOTAL)
6455 (void) printf("%s\n", typename);
6457 (void) printf(" L%d %s\n",
6460 if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
6461 (void) printf("\t number of ganged "
6462 "blocks: %s\n", gang);
6465 if (dump_opt['b'] >= 4) {
6466 (void) printf("psize "
6467 "(in 512-byte sectors): "
6468 "number of blocks\n");
6469 dump_histogram(zb->zb_psize_histogram,
6470 PSIZE_HISTO_SIZE, 0);
6475 /* Output a table summarizing block sizes in the pool */
6476 if (dump_opt['b'] >= 2) {
6477 dump_size_histograms(&zcb);
6481 (void) printf("\n");
6486 if (zcb.zcb_haderrors)
6492 typedef struct zdb_ddt_entry {
6494 uint64_t zdde_ref_blocks;
6495 uint64_t zdde_ref_lsize;
6496 uint64_t zdde_ref_psize;
6497 uint64_t zdde_ref_dsize;
6498 avl_node_t zdde_node;
6503 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
6504 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
6506 avl_tree_t *t = arg;
6508 zdb_ddt_entry_t *zdde, zdde_search;
6510 if (zb->zb_level == ZB_DNODE_LEVEL || BP_IS_HOLE(bp) ||
6514 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
6515 (void) printf("traversing objset %llu, %llu objects, "
6516 "%lu blocks so far\n",
6517 (u_longlong_t)zb->zb_objset,
6518 (u_longlong_t)BP_GET_FILL(bp),
6522 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
6523 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
6526 ddt_key_fill(&zdde_search.zdde_key, bp);
6528 zdde = avl_find(t, &zdde_search, &where);
6531 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
6532 zdde->zdde_key = zdde_search.zdde_key;
6533 avl_insert(t, zdde, where);
6536 zdde->zdde_ref_blocks += 1;
6537 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
6538 zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
6539 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
6545 dump_simulated_ddt(spa_t *spa)
6548 void *cookie = NULL;
6549 zdb_ddt_entry_t *zdde;
6550 ddt_histogram_t ddh_total;
6551 ddt_stat_t dds_total;
6553 bzero(&ddh_total, sizeof (ddh_total));
6554 bzero(&dds_total, sizeof (dds_total));
6555 avl_create(&t, ddt_entry_compare,
6556 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
6558 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6560 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
6561 TRAVERSE_NO_DECRYPT, zdb_ddt_add_cb, &t);
6563 spa_config_exit(spa, SCL_CONFIG, FTAG);
6565 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
6567 uint64_t refcnt = zdde->zdde_ref_blocks;
6568 ASSERT(refcnt != 0);
6570 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
6571 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
6572 dds.dds_psize = zdde->zdde_ref_psize / refcnt;
6573 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
6575 dds.dds_ref_blocks = zdde->zdde_ref_blocks;
6576 dds.dds_ref_lsize = zdde->zdde_ref_lsize;
6577 dds.dds_ref_psize = zdde->zdde_ref_psize;
6578 dds.dds_ref_dsize = zdde->zdde_ref_dsize;
6580 ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
6583 umem_free(zdde, sizeof (*zdde));
6588 ddt_histogram_stat(&dds_total, &ddh_total);
6590 (void) printf("Simulated DDT histogram:\n");
6592 zpool_dump_ddt(&dds_total, &ddh_total);
6594 dump_dedup_ratio(&dds_total);
6598 verify_device_removal_feature_counts(spa_t *spa)
6600 uint64_t dr_feature_refcount = 0;
6601 uint64_t oc_feature_refcount = 0;
6602 uint64_t indirect_vdev_count = 0;
6603 uint64_t precise_vdev_count = 0;
6604 uint64_t obsolete_counts_object_count = 0;
6605 uint64_t obsolete_sm_count = 0;
6606 uint64_t obsolete_counts_count = 0;
6607 uint64_t scip_count = 0;
6608 uint64_t obsolete_bpobj_count = 0;
6611 spa_condensing_indirect_phys_t *scip =
6612 &spa->spa_condensing_indirect_phys;
6613 if (scip->scip_next_mapping_object != 0) {
6614 vdev_t *vd = spa->spa_root_vdev->vdev_child[scip->scip_vdev];
6615 ASSERT(scip->scip_prev_obsolete_sm_object != 0);
6616 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
6618 (void) printf("Condensing indirect vdev %llu: new mapping "
6619 "object %llu, prev obsolete sm %llu\n",
6620 (u_longlong_t)scip->scip_vdev,
6621 (u_longlong_t)scip->scip_next_mapping_object,
6622 (u_longlong_t)scip->scip_prev_obsolete_sm_object);
6623 if (scip->scip_prev_obsolete_sm_object != 0) {
6624 space_map_t *prev_obsolete_sm = NULL;
6625 VERIFY0(space_map_open(&prev_obsolete_sm,
6626 spa->spa_meta_objset,
6627 scip->scip_prev_obsolete_sm_object,
6628 0, vd->vdev_asize, 0));
6629 dump_spacemap(spa->spa_meta_objset, prev_obsolete_sm);
6630 (void) printf("\n");
6631 space_map_close(prev_obsolete_sm);
6637 for (uint64_t i = 0; i < spa->spa_root_vdev->vdev_children; i++) {
6638 vdev_t *vd = spa->spa_root_vdev->vdev_child[i];
6639 vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
6641 if (vic->vic_mapping_object != 0) {
6642 ASSERT(vd->vdev_ops == &vdev_indirect_ops ||
6644 indirect_vdev_count++;
6646 if (vd->vdev_indirect_mapping->vim_havecounts) {
6647 obsolete_counts_count++;
6651 boolean_t are_precise;
6652 VERIFY0(vdev_obsolete_counts_are_precise(vd, &are_precise));
6654 ASSERT(vic->vic_mapping_object != 0);
6655 precise_vdev_count++;
6658 uint64_t obsolete_sm_object;
6659 VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object));
6660 if (obsolete_sm_object != 0) {
6661 ASSERT(vic->vic_mapping_object != 0);
6662 obsolete_sm_count++;
6666 (void) feature_get_refcount(spa,
6667 &spa_feature_table[SPA_FEATURE_DEVICE_REMOVAL],
6668 &dr_feature_refcount);
6669 (void) feature_get_refcount(spa,
6670 &spa_feature_table[SPA_FEATURE_OBSOLETE_COUNTS],
6671 &oc_feature_refcount);
6673 if (dr_feature_refcount != indirect_vdev_count) {
6675 (void) printf("Number of indirect vdevs (%llu) " \
6676 "does not match feature count (%llu)\n",
6677 (u_longlong_t)indirect_vdev_count,
6678 (u_longlong_t)dr_feature_refcount);
6680 (void) printf("Verified device_removal feature refcount " \
6681 "of %llu is correct\n",
6682 (u_longlong_t)dr_feature_refcount);
6685 if (zap_contains(spa_meta_objset(spa), DMU_POOL_DIRECTORY_OBJECT,
6686 DMU_POOL_OBSOLETE_BPOBJ) == 0) {
6687 obsolete_bpobj_count++;
6691 obsolete_counts_object_count = precise_vdev_count;
6692 obsolete_counts_object_count += obsolete_sm_count;
6693 obsolete_counts_object_count += obsolete_counts_count;
6694 obsolete_counts_object_count += scip_count;
6695 obsolete_counts_object_count += obsolete_bpobj_count;
6696 obsolete_counts_object_count += remap_deadlist_count;
6698 if (oc_feature_refcount != obsolete_counts_object_count) {
6700 (void) printf("Number of obsolete counts objects (%llu) " \
6701 "does not match feature count (%llu)\n",
6702 (u_longlong_t)obsolete_counts_object_count,
6703 (u_longlong_t)oc_feature_refcount);
6704 (void) printf("pv:%llu os:%llu oc:%llu sc:%llu "
6705 "ob:%llu rd:%llu\n",
6706 (u_longlong_t)precise_vdev_count,
6707 (u_longlong_t)obsolete_sm_count,
6708 (u_longlong_t)obsolete_counts_count,
6709 (u_longlong_t)scip_count,
6710 (u_longlong_t)obsolete_bpobj_count,
6711 (u_longlong_t)remap_deadlist_count);
6713 (void) printf("Verified indirect_refcount feature refcount " \
6714 "of %llu is correct\n",
6715 (u_longlong_t)oc_feature_refcount);
6721 zdb_set_skip_mmp(char *target)
6726 * Disable the activity check to allow examination of
6729 mutex_enter(&spa_namespace_lock);
6730 if ((spa = spa_lookup(target)) != NULL) {
6731 spa->spa_import_flags |= ZFS_IMPORT_SKIP_MMP;
6733 mutex_exit(&spa_namespace_lock);
6736 #define BOGUS_SUFFIX "_CHECKPOINTED_UNIVERSE"
6738 * Import the checkpointed state of the pool specified by the target
6739 * parameter as readonly. The function also accepts a pool config
6740 * as an optional parameter, else it attempts to infer the config by
6741 * the name of the target pool.
6743 * Note that the checkpointed state's pool name will be the name of
6744 * the original pool with the above suffix appended to it. In addition,
6745 * if the target is not a pool name (e.g. a path to a dataset) then
6746 * the new_path parameter is populated with the updated path to
6747 * reflect the fact that we are looking into the checkpointed state.
6749 * The function returns a newly-allocated copy of the name of the
6750 * pool containing the checkpointed state. When this copy is no
6751 * longer needed it should be freed with free(3C). Same thing
6752 * applies to the new_path parameter if allocated.
6755 import_checkpointed_state(char *target, nvlist_t *cfg, char **new_path)
6758 char *poolname, *bogus_name = NULL;
6760 /* If the target is not a pool, the extract the pool name */
6761 char *path_start = strchr(target, '/');
6762 if (path_start != NULL) {
6763 size_t poolname_len = path_start - target;
6764 poolname = strndup(target, poolname_len);
6770 zdb_set_skip_mmp(poolname);
6771 error = spa_get_stats(poolname, &cfg, NULL, 0);
6773 fatal("Tried to read config of pool \"%s\" but "
6774 "spa_get_stats() failed with error %d\n",
6779 if (asprintf(&bogus_name, "%s%s", poolname, BOGUS_SUFFIX) == -1)
6781 fnvlist_add_string(cfg, ZPOOL_CONFIG_POOL_NAME, bogus_name);
6783 error = spa_import(bogus_name, cfg, NULL,
6784 ZFS_IMPORT_MISSING_LOG | ZFS_IMPORT_CHECKPOINT |
6785 ZFS_IMPORT_SKIP_MMP);
6787 fatal("Tried to import pool \"%s\" but spa_import() failed "
6788 "with error %d\n", bogus_name, error);
6791 if (new_path != NULL && path_start != NULL) {
6792 if (asprintf(new_path, "%s%s", bogus_name, path_start) == -1) {
6793 if (path_start != NULL)
6799 if (target != poolname)
6802 return (bogus_name);
6805 typedef struct verify_checkpoint_sm_entry_cb_arg {
6808 /* the following fields are only used for printing progress */
6809 uint64_t vcsec_entryid;
6810 uint64_t vcsec_num_entries;
6811 } verify_checkpoint_sm_entry_cb_arg_t;
6813 #define ENTRIES_PER_PROGRESS_UPDATE 10000
6816 verify_checkpoint_sm_entry_cb(space_map_entry_t *sme, void *arg)
6818 verify_checkpoint_sm_entry_cb_arg_t *vcsec = arg;
6819 vdev_t *vd = vcsec->vcsec_vd;
6820 metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift];
6821 uint64_t end = sme->sme_offset + sme->sme_run;
6823 ASSERT(sme->sme_type == SM_FREE);
6825 if ((vcsec->vcsec_entryid % ENTRIES_PER_PROGRESS_UPDATE) == 0) {
6826 (void) fprintf(stderr,
6827 "\rverifying vdev %llu, space map entry %llu of %llu ...",
6828 (longlong_t)vd->vdev_id,
6829 (longlong_t)vcsec->vcsec_entryid,
6830 (longlong_t)vcsec->vcsec_num_entries);
6832 vcsec->vcsec_entryid++;
6835 * See comment in checkpoint_sm_exclude_entry_cb()
6837 VERIFY3U(sme->sme_offset, >=, ms->ms_start);
6838 VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
6841 * The entries in the vdev_checkpoint_sm should be marked as
6842 * allocated in the checkpointed state of the pool, therefore
6843 * their respective ms_allocateable trees should not contain them.
6845 mutex_enter(&ms->ms_lock);
6846 range_tree_verify_not_present(ms->ms_allocatable,
6847 sme->sme_offset, sme->sme_run);
6848 mutex_exit(&ms->ms_lock);
6854 * Verify that all segments in the vdev_checkpoint_sm are allocated
6855 * according to the checkpoint's ms_sm (i.e. are not in the checkpoint's
6858 * Do so by comparing the checkpoint space maps (vdev_checkpoint_sm) of
6859 * each vdev in the current state of the pool to the metaslab space maps
6860 * (ms_sm) of the checkpointed state of the pool.
6862 * Note that the function changes the state of the ms_allocatable
6863 * trees of the current spa_t. The entries of these ms_allocatable
6864 * trees are cleared out and then repopulated from with the free
6865 * entries of their respective ms_sm space maps.
6868 verify_checkpoint_vdev_spacemaps(spa_t *checkpoint, spa_t *current)
6870 vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
6871 vdev_t *current_rvd = current->spa_root_vdev;
6873 load_concrete_ms_allocatable_trees(checkpoint, SM_FREE);
6875 for (uint64_t c = 0; c < ckpoint_rvd->vdev_children; c++) {
6876 vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[c];
6877 vdev_t *current_vd = current_rvd->vdev_child[c];
6879 space_map_t *checkpoint_sm = NULL;
6880 uint64_t checkpoint_sm_obj;
6882 if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
6884 * Since we don't allow device removal in a pool
6885 * that has a checkpoint, we expect that all removed
6886 * vdevs were removed from the pool before the
6889 ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
6894 * If the checkpoint space map doesn't exist, then nothing
6895 * here is checkpointed so there's nothing to verify.
6897 if (current_vd->vdev_top_zap == 0 ||
6898 zap_contains(spa_meta_objset(current),
6899 current_vd->vdev_top_zap,
6900 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
6903 VERIFY0(zap_lookup(spa_meta_objset(current),
6904 current_vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
6905 sizeof (uint64_t), 1, &checkpoint_sm_obj));
6907 VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(current),
6908 checkpoint_sm_obj, 0, current_vd->vdev_asize,
6909 current_vd->vdev_ashift));
6911 verify_checkpoint_sm_entry_cb_arg_t vcsec;
6912 vcsec.vcsec_vd = ckpoint_vd;
6913 vcsec.vcsec_entryid = 0;
6914 vcsec.vcsec_num_entries =
6915 space_map_length(checkpoint_sm) / sizeof (uint64_t);
6916 VERIFY0(space_map_iterate(checkpoint_sm,
6917 space_map_length(checkpoint_sm),
6918 verify_checkpoint_sm_entry_cb, &vcsec));
6919 if (dump_opt['m'] > 3)
6920 dump_spacemap(current->spa_meta_objset, checkpoint_sm);
6921 space_map_close(checkpoint_sm);
6925 * If we've added vdevs since we took the checkpoint, ensure
6926 * that their checkpoint space maps are empty.
6928 if (ckpoint_rvd->vdev_children < current_rvd->vdev_children) {
6929 for (uint64_t c = ckpoint_rvd->vdev_children;
6930 c < current_rvd->vdev_children; c++) {
6931 vdev_t *current_vd = current_rvd->vdev_child[c];
6932 ASSERT3P(current_vd->vdev_checkpoint_sm, ==, NULL);
6936 /* for cleaner progress output */
6937 (void) fprintf(stderr, "\n");
6941 * Verifies that all space that's allocated in the checkpoint is
6942 * still allocated in the current version, by checking that everything
6943 * in checkpoint's ms_allocatable (which is actually allocated, not
6944 * allocatable/free) is not present in current's ms_allocatable.
6946 * Note that the function changes the state of the ms_allocatable
6947 * trees of both spas when called. The entries of all ms_allocatable
6948 * trees are cleared out and then repopulated from their respective
6949 * ms_sm space maps. In the checkpointed state we load the allocated
6950 * entries, and in the current state we load the free entries.
6953 verify_checkpoint_ms_spacemaps(spa_t *checkpoint, spa_t *current)
6955 vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
6956 vdev_t *current_rvd = current->spa_root_vdev;
6958 load_concrete_ms_allocatable_trees(checkpoint, SM_ALLOC);
6959 load_concrete_ms_allocatable_trees(current, SM_FREE);
6961 for (uint64_t i = 0; i < ckpoint_rvd->vdev_children; i++) {
6962 vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[i];
6963 vdev_t *current_vd = current_rvd->vdev_child[i];
6965 if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
6967 * See comment in verify_checkpoint_vdev_spacemaps()
6969 ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
6973 for (uint64_t m = 0; m < ckpoint_vd->vdev_ms_count; m++) {
6974 metaslab_t *ckpoint_msp = ckpoint_vd->vdev_ms[m];
6975 metaslab_t *current_msp = current_vd->vdev_ms[m];
6977 (void) fprintf(stderr,
6978 "\rverifying vdev %llu of %llu, "
6979 "metaslab %llu of %llu ...",
6980 (longlong_t)current_vd->vdev_id,
6981 (longlong_t)current_rvd->vdev_children,
6982 (longlong_t)current_vd->vdev_ms[m]->ms_id,
6983 (longlong_t)current_vd->vdev_ms_count);
6986 * We walk through the ms_allocatable trees that
6987 * are loaded with the allocated blocks from the
6988 * ms_sm spacemaps of the checkpoint. For each
6989 * one of these ranges we ensure that none of them
6990 * exists in the ms_allocatable trees of the
6991 * current state which are loaded with the ranges
6992 * that are currently free.
6994 * This way we ensure that none of the blocks that
6995 * are part of the checkpoint were freed by mistake.
6997 range_tree_walk(ckpoint_msp->ms_allocatable,
6998 (range_tree_func_t *)range_tree_verify_not_present,
6999 current_msp->ms_allocatable);
7003 /* for cleaner progress output */
7004 (void) fprintf(stderr, "\n");
7008 verify_checkpoint_blocks(spa_t *spa)
7010 ASSERT(!dump_opt['L']);
7012 spa_t *checkpoint_spa;
7013 char *checkpoint_pool;
7014 nvlist_t *config = NULL;
7018 * We import the checkpointed state of the pool (under a different
7019 * name) so we can do verification on it against the current state
7022 checkpoint_pool = import_checkpointed_state(spa->spa_name, config,
7024 ASSERT(strcmp(spa->spa_name, checkpoint_pool) != 0);
7026 error = spa_open(checkpoint_pool, &checkpoint_spa, FTAG);
7028 fatal("Tried to open pool \"%s\" but spa_open() failed with "
7029 "error %d\n", checkpoint_pool, error);
7033 * Ensure that ranges in the checkpoint space maps of each vdev
7034 * are allocated according to the checkpointed state's metaslab
7037 verify_checkpoint_vdev_spacemaps(checkpoint_spa, spa);
7040 * Ensure that allocated ranges in the checkpoint's metaslab
7041 * space maps remain allocated in the metaslab space maps of
7042 * the current state.
7044 verify_checkpoint_ms_spacemaps(checkpoint_spa, spa);
7047 * Once we are done, we get rid of the checkpointed state.
7049 spa_close(checkpoint_spa, FTAG);
7050 free(checkpoint_pool);
7054 dump_leftover_checkpoint_blocks(spa_t *spa)
7056 vdev_t *rvd = spa->spa_root_vdev;
7058 for (uint64_t i = 0; i < rvd->vdev_children; i++) {
7059 vdev_t *vd = rvd->vdev_child[i];
7061 space_map_t *checkpoint_sm = NULL;
7062 uint64_t checkpoint_sm_obj;
7064 if (vd->vdev_top_zap == 0)
7067 if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
7068 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
7071 VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
7072 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
7073 sizeof (uint64_t), 1, &checkpoint_sm_obj));
7075 VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
7076 checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
7077 dump_spacemap(spa->spa_meta_objset, checkpoint_sm);
7078 space_map_close(checkpoint_sm);
7083 verify_checkpoint(spa_t *spa)
7085 uberblock_t checkpoint;
7088 if (!spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT))
7091 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
7092 DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t),
7093 sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint);
7095 if (error == ENOENT && !dump_opt['L']) {
7097 * If the feature is active but the uberblock is missing
7098 * then we must be in the middle of discarding the
7101 (void) printf("\nPartially discarded checkpoint "
7103 if (dump_opt['m'] > 3)
7104 dump_leftover_checkpoint_blocks(spa);
7106 } else if (error != 0) {
7107 (void) printf("lookup error %d when looking for "
7108 "checkpointed uberblock in MOS\n", error);
7111 dump_uberblock(&checkpoint, "\nCheckpointed uberblock found:\n", "\n");
7113 if (checkpoint.ub_checkpoint_txg == 0) {
7114 (void) printf("\nub_checkpoint_txg not set in checkpointed "
7119 if (error == 0 && !dump_opt['L'])
7120 verify_checkpoint_blocks(spa);
7127 mos_leaks_cb(void *arg, uint64_t start, uint64_t size)
7129 for (uint64_t i = start; i < size; i++) {
7130 (void) printf("MOS object %llu referenced but not allocated\n",
7136 mos_obj_refd(uint64_t obj)
7138 if (obj != 0 && mos_refd_objs != NULL)
7139 range_tree_add(mos_refd_objs, obj, 1);
7143 * Call on a MOS object that may already have been referenced.
7146 mos_obj_refd_multiple(uint64_t obj)
7148 if (obj != 0 && mos_refd_objs != NULL &&
7149 !range_tree_contains(mos_refd_objs, obj, 1))
7150 range_tree_add(mos_refd_objs, obj, 1);
7154 mos_leak_vdev_top_zap(vdev_t *vd)
7156 uint64_t ms_flush_data_obj;
7157 int error = zap_lookup(spa_meta_objset(vd->vdev_spa),
7158 vd->vdev_top_zap, VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS,
7159 sizeof (ms_flush_data_obj), 1, &ms_flush_data_obj);
7160 if (error == ENOENT)
7164 mos_obj_refd(ms_flush_data_obj);
7168 mos_leak_vdev(vdev_t *vd)
7170 mos_obj_refd(vd->vdev_dtl_object);
7171 mos_obj_refd(vd->vdev_ms_array);
7172 mos_obj_refd(vd->vdev_indirect_config.vic_births_object);
7173 mos_obj_refd(vd->vdev_indirect_config.vic_mapping_object);
7174 mos_obj_refd(vd->vdev_leaf_zap);
7175 if (vd->vdev_checkpoint_sm != NULL)
7176 mos_obj_refd(vd->vdev_checkpoint_sm->sm_object);
7177 if (vd->vdev_indirect_mapping != NULL) {
7178 mos_obj_refd(vd->vdev_indirect_mapping->
7179 vim_phys->vimp_counts_object);
7181 if (vd->vdev_obsolete_sm != NULL)
7182 mos_obj_refd(vd->vdev_obsolete_sm->sm_object);
7184 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
7185 metaslab_t *ms = vd->vdev_ms[m];
7186 mos_obj_refd(space_map_object(ms->ms_sm));
7189 if (vd->vdev_top_zap != 0) {
7190 mos_obj_refd(vd->vdev_top_zap);
7191 mos_leak_vdev_top_zap(vd);
7194 for (uint64_t c = 0; c < vd->vdev_children; c++) {
7195 mos_leak_vdev(vd->vdev_child[c]);
7200 mos_leak_log_spacemaps(spa_t *spa)
7202 uint64_t spacemap_zap;
7203 int error = zap_lookup(spa_meta_objset(spa),
7204 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_LOG_SPACEMAP_ZAP,
7205 sizeof (spacemap_zap), 1, &spacemap_zap);
7206 if (error == ENOENT)
7210 mos_obj_refd(spacemap_zap);
7211 for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg);
7212 sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls))
7213 mos_obj_refd(sls->sls_sm_obj);
7217 dump_mos_leaks(spa_t *spa)
7220 objset_t *mos = spa->spa_meta_objset;
7221 dsl_pool_t *dp = spa->spa_dsl_pool;
7223 /* Visit and mark all referenced objects in the MOS */
7225 mos_obj_refd(DMU_POOL_DIRECTORY_OBJECT);
7226 mos_obj_refd(spa->spa_pool_props_object);
7227 mos_obj_refd(spa->spa_config_object);
7228 mos_obj_refd(spa->spa_ddt_stat_object);
7229 mos_obj_refd(spa->spa_feat_desc_obj);
7230 mos_obj_refd(spa->spa_feat_enabled_txg_obj);
7231 mos_obj_refd(spa->spa_feat_for_read_obj);
7232 mos_obj_refd(spa->spa_feat_for_write_obj);
7233 mos_obj_refd(spa->spa_history);
7234 mos_obj_refd(spa->spa_errlog_last);
7235 mos_obj_refd(spa->spa_errlog_scrub);
7236 mos_obj_refd(spa->spa_all_vdev_zaps);
7237 mos_obj_refd(spa->spa_dsl_pool->dp_bptree_obj);
7238 mos_obj_refd(spa->spa_dsl_pool->dp_tmp_userrefs_obj);
7239 mos_obj_refd(spa->spa_dsl_pool->dp_scan->scn_phys.scn_queue_obj);
7240 bpobj_count_refd(&spa->spa_deferred_bpobj);
7241 mos_obj_refd(dp->dp_empty_bpobj);
7242 bpobj_count_refd(&dp->dp_obsolete_bpobj);
7243 bpobj_count_refd(&dp->dp_free_bpobj);
7244 mos_obj_refd(spa->spa_l2cache.sav_object);
7245 mos_obj_refd(spa->spa_spares.sav_object);
7247 if (spa->spa_syncing_log_sm != NULL)
7248 mos_obj_refd(spa->spa_syncing_log_sm->sm_object);
7249 mos_leak_log_spacemaps(spa);
7251 mos_obj_refd(spa->spa_condensing_indirect_phys.
7252 scip_next_mapping_object);
7253 mos_obj_refd(spa->spa_condensing_indirect_phys.
7254 scip_prev_obsolete_sm_object);
7255 if (spa->spa_condensing_indirect_phys.scip_next_mapping_object != 0) {
7256 vdev_indirect_mapping_t *vim =
7257 vdev_indirect_mapping_open(mos,
7258 spa->spa_condensing_indirect_phys.scip_next_mapping_object);
7259 mos_obj_refd(vim->vim_phys->vimp_counts_object);
7260 vdev_indirect_mapping_close(vim);
7262 deleted_livelists_dump_mos(spa);
7264 if (dp->dp_origin_snap != NULL) {
7267 dsl_pool_config_enter(dp, FTAG);
7268 VERIFY0(dsl_dataset_hold_obj(dp,
7269 dsl_dataset_phys(dp->dp_origin_snap)->ds_next_snap_obj,
7271 count_ds_mos_objects(ds);
7272 dump_blkptr_list(&ds->ds_deadlist, "Deadlist");
7273 dsl_dataset_rele(ds, FTAG);
7274 dsl_pool_config_exit(dp, FTAG);
7276 count_ds_mos_objects(dp->dp_origin_snap);
7277 dump_blkptr_list(&dp->dp_origin_snap->ds_deadlist, "Deadlist");
7279 count_dir_mos_objects(dp->dp_mos_dir);
7280 if (dp->dp_free_dir != NULL)
7281 count_dir_mos_objects(dp->dp_free_dir);
7282 if (dp->dp_leak_dir != NULL)
7283 count_dir_mos_objects(dp->dp_leak_dir);
7285 mos_leak_vdev(spa->spa_root_vdev);
7287 for (uint64_t class = 0; class < DDT_CLASSES; class++) {
7288 for (uint64_t type = 0; type < DDT_TYPES; type++) {
7289 for (uint64_t cksum = 0;
7290 cksum < ZIO_CHECKSUM_FUNCTIONS; cksum++) {
7291 ddt_t *ddt = spa->spa_ddt[cksum];
7292 mos_obj_refd(ddt->ddt_object[type][class]);
7298 * Visit all allocated objects and make sure they are referenced.
7300 uint64_t object = 0;
7301 while (dmu_object_next(mos, &object, B_FALSE, 0) == 0) {
7302 if (range_tree_contains(mos_refd_objs, object, 1)) {
7303 range_tree_remove(mos_refd_objs, object, 1);
7305 dmu_object_info_t doi;
7307 dmu_object_info(mos, object, &doi);
7308 if (doi.doi_type & DMU_OT_NEWTYPE) {
7309 dmu_object_byteswap_t bswap =
7310 DMU_OT_BYTESWAP(doi.doi_type);
7311 name = dmu_ot_byteswap[bswap].ob_name;
7313 name = dmu_ot[doi.doi_type].ot_name;
7316 (void) printf("MOS object %llu (%s) leaked\n",
7317 (u_longlong_t)object, name);
7321 (void) range_tree_walk(mos_refd_objs, mos_leaks_cb, NULL);
7322 if (!range_tree_is_empty(mos_refd_objs))
7324 range_tree_vacate(mos_refd_objs, NULL, NULL);
7325 range_tree_destroy(mos_refd_objs);
7329 typedef struct log_sm_obsolete_stats_arg {
7330 uint64_t lsos_current_txg;
7332 uint64_t lsos_total_entries;
7333 uint64_t lsos_valid_entries;
7335 uint64_t lsos_sm_entries;
7336 uint64_t lsos_valid_sm_entries;
7337 } log_sm_obsolete_stats_arg_t;
7340 log_spacemap_obsolete_stats_cb(spa_t *spa, space_map_entry_t *sme,
7341 uint64_t txg, void *arg)
7343 log_sm_obsolete_stats_arg_t *lsos = arg;
7345 uint64_t offset = sme->sme_offset;
7346 uint64_t vdev_id = sme->sme_vdev;
7348 if (lsos->lsos_current_txg == 0) {
7349 /* this is the first log */
7350 lsos->lsos_current_txg = txg;
7351 } else if (lsos->lsos_current_txg < txg) {
7352 /* we just changed log - print stats and reset */
7353 (void) printf("%-8llu valid entries out of %-8llu - txg %llu\n",
7354 (u_longlong_t)lsos->lsos_valid_sm_entries,
7355 (u_longlong_t)lsos->lsos_sm_entries,
7356 (u_longlong_t)lsos->lsos_current_txg);
7357 lsos->lsos_valid_sm_entries = 0;
7358 lsos->lsos_sm_entries = 0;
7359 lsos->lsos_current_txg = txg;
7361 ASSERT3U(lsos->lsos_current_txg, ==, txg);
7363 lsos->lsos_sm_entries++;
7364 lsos->lsos_total_entries++;
7366 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
7367 if (!vdev_is_concrete(vd))
7370 metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
7371 ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE);
7373 if (txg < metaslab_unflushed_txg(ms))
7375 lsos->lsos_valid_sm_entries++;
7376 lsos->lsos_valid_entries++;
7381 dump_log_spacemap_obsolete_stats(spa_t *spa)
7383 if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))
7386 log_sm_obsolete_stats_arg_t lsos;
7387 bzero(&lsos, sizeof (lsos));
7389 (void) printf("Log Space Map Obsolete Entry Statistics:\n");
7391 iterate_through_spacemap_logs(spa,
7392 log_spacemap_obsolete_stats_cb, &lsos);
7394 /* print stats for latest log */
7395 (void) printf("%-8llu valid entries out of %-8llu - txg %llu\n",
7396 (u_longlong_t)lsos.lsos_valid_sm_entries,
7397 (u_longlong_t)lsos.lsos_sm_entries,
7398 (u_longlong_t)lsos.lsos_current_txg);
7400 (void) printf("%-8llu valid entries out of %-8llu - total\n\n",
7401 (u_longlong_t)lsos.lsos_valid_entries,
7402 (u_longlong_t)lsos.lsos_total_entries);
7406 dump_zpool(spa_t *spa)
7408 dsl_pool_t *dp = spa_get_dsl(spa);
7411 if (dump_opt['y']) {
7412 livelist_metaslab_validate(spa);
7415 if (dump_opt['S']) {
7416 dump_simulated_ddt(spa);
7420 if (!dump_opt['e'] && dump_opt['C'] > 1) {
7421 (void) printf("\nCached configuration:\n");
7422 dump_nvlist(spa->spa_config, 8);
7429 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
7434 if (dump_opt['d'] > 2 || dump_opt['m'])
7435 dump_metaslabs(spa);
7437 dump_metaslab_groups(spa);
7438 if (dump_opt['d'] > 2 || dump_opt['m']) {
7439 dump_log_spacemaps(spa);
7440 dump_log_spacemap_obsolete_stats(spa);
7443 if (dump_opt['d'] || dump_opt['i']) {
7445 mos_refd_objs = range_tree_create(NULL, RANGE_SEG64, NULL, 0,
7447 dump_objset(dp->dp_meta_objset);
7449 if (dump_opt['d'] >= 3) {
7450 dsl_pool_t *dp = spa->spa_dsl_pool;
7451 dump_full_bpobj(&spa->spa_deferred_bpobj,
7452 "Deferred frees", 0);
7453 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
7454 dump_full_bpobj(&dp->dp_free_bpobj,
7455 "Pool snapshot frees", 0);
7457 if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
7458 ASSERT(spa_feature_is_enabled(spa,
7459 SPA_FEATURE_DEVICE_REMOVAL));
7460 dump_full_bpobj(&dp->dp_obsolete_bpobj,
7461 "Pool obsolete blocks", 0);
7464 if (spa_feature_is_active(spa,
7465 SPA_FEATURE_ASYNC_DESTROY)) {
7466 dump_bptree(spa->spa_meta_objset,
7468 "Pool dataset frees");
7470 dump_dtl(spa->spa_root_vdev, 0);
7473 for (spa_feature_t f = 0; f < SPA_FEATURES; f++)
7474 global_feature_count[f] = UINT64_MAX;
7475 global_feature_count[SPA_FEATURE_REDACTION_BOOKMARKS] = 0;
7476 global_feature_count[SPA_FEATURE_BOOKMARK_WRITTEN] = 0;
7477 global_feature_count[SPA_FEATURE_LIVELIST] = 0;
7479 (void) dmu_objset_find(spa_name(spa), dump_one_objset,
7480 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
7482 if (rc == 0 && !dump_opt['L'])
7483 rc = dump_mos_leaks(spa);
7485 for (f = 0; f < SPA_FEATURES; f++) {
7489 if (!(spa_feature_table[f].fi_flags &
7490 ZFEATURE_FLAG_PER_DATASET)) {
7491 if (global_feature_count[f] == UINT64_MAX)
7493 if (!spa_feature_is_enabled(spa, f)) {
7494 ASSERT0(global_feature_count[f]);
7497 arr = global_feature_count;
7499 if (!spa_feature_is_enabled(spa, f)) {
7500 ASSERT0(dataset_feature_count[f]);
7503 arr = dataset_feature_count;
7505 if (feature_get_refcount(spa, &spa_feature_table[f],
7506 &refcount) == ENOTSUP)
7508 if (arr[f] != refcount) {
7509 (void) printf("%s feature refcount mismatch: "
7510 "%lld consumers != %lld refcount\n",
7511 spa_feature_table[f].fi_uname,
7512 (longlong_t)arr[f], (longlong_t)refcount);
7515 (void) printf("Verified %s feature refcount "
7516 "of %llu is correct\n",
7517 spa_feature_table[f].fi_uname,
7518 (longlong_t)refcount);
7523 rc = verify_device_removal_feature_counts(spa);
7526 if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
7527 rc = dump_block_stats(spa);
7530 rc = verify_spacemap_refcounts(spa);
7533 show_pool_stats(spa);
7539 rc = verify_checkpoint(spa);
7542 dump_debug_buffer();
7547 #define ZDB_FLAG_CHECKSUM 0x0001
7548 #define ZDB_FLAG_DECOMPRESS 0x0002
7549 #define ZDB_FLAG_BSWAP 0x0004
7550 #define ZDB_FLAG_GBH 0x0008
7551 #define ZDB_FLAG_INDIRECT 0x0010
7552 #define ZDB_FLAG_RAW 0x0020
7553 #define ZDB_FLAG_PRINT_BLKPTR 0x0040
7554 #define ZDB_FLAG_VERBOSE 0x0080
7556 static int flagbits[256];
7557 static char flagbitstr[16];
7560 zdb_print_blkptr(const blkptr_t *bp, int flags)
7562 char blkbuf[BP_SPRINTF_LEN];
7564 if (flags & ZDB_FLAG_BSWAP)
7565 byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
7567 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
7568 (void) printf("%s\n", blkbuf);
7572 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
7576 for (i = 0; i < nbps; i++)
7577 zdb_print_blkptr(&bp[i], flags);
7581 zdb_dump_gbh(void *buf, int flags)
7583 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
7587 zdb_dump_block_raw(void *buf, uint64_t size, int flags)
7589 if (flags & ZDB_FLAG_BSWAP)
7590 byteswap_uint64_array(buf, size);
7591 VERIFY(write(fileno(stdout), buf, size) == size);
7595 zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
7597 uint64_t *d = (uint64_t *)buf;
7598 unsigned nwords = size / sizeof (uint64_t);
7599 int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
7606 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8";
7608 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f";
7610 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr);
7612 #ifdef _LITTLE_ENDIAN
7613 /* correct the endianness */
7614 do_bswap = !do_bswap;
7616 for (i = 0; i < nwords; i += 2) {
7617 (void) printf("%06llx: %016llx %016llx ",
7618 (u_longlong_t)(i * sizeof (uint64_t)),
7619 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
7620 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
7623 for (j = 0; j < 2 * sizeof (uint64_t); j++)
7624 (void) printf("%c", isprint(c[j]) ? c[j] : '.');
7625 (void) printf("\n");
7630 * There are two acceptable formats:
7631 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a
7632 * child[.child]* - For example: 0.1.1
7634 * The second form can be used to specify arbitrary vdevs anywhere
7635 * in the hierarchy. For example, in a pool with a mirror of
7636 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
7639 zdb_vdev_lookup(vdev_t *vdev, const char *path)
7647 /* First, assume the x.x.x.x format */
7648 i = strtoul(path, &s, 10);
7649 if (s == path || (s && *s != '.' && *s != '\0'))
7651 if (i >= vdev->vdev_children)
7654 vdev = vdev->vdev_child[i];
7655 if (s && *s == '\0')
7657 return (zdb_vdev_lookup(vdev, s+1));
7660 for (i = 0; i < vdev->vdev_children; i++) {
7661 vdev_t *vc = vdev->vdev_child[i];
7663 if (vc->vdev_path == NULL) {
7664 vc = zdb_vdev_lookup(vc, path);
7671 p = strrchr(vc->vdev_path, '/');
7672 p = p ? p + 1 : vc->vdev_path;
7673 q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
7675 if (strcmp(vc->vdev_path, path) == 0)
7677 if (strcmp(p, path) == 0)
7679 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
7687 name_from_objset_id(spa_t *spa, uint64_t objset_id, char *outstr)
7691 dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
7692 int error = dsl_dataset_hold_obj(spa->spa_dsl_pool, objset_id,
7695 (void) fprintf(stderr, "failed to hold objset %llu: %s\n",
7696 (u_longlong_t)objset_id, strerror(error));
7697 dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
7700 dsl_dataset_name(ds, outstr);
7701 dsl_dataset_rele(ds, NULL);
7702 dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
7707 zdb_parse_block_sizes(char *sizes, uint64_t *lsize, uint64_t *psize)
7714 s0 = strtok(sizes, "/");
7717 s1 = strtok(NULL, "/");
7718 *lsize = strtoull(s0, NULL, 16);
7719 *psize = s1 ? strtoull(s1, NULL, 16) : *lsize;
7720 return (*lsize >= *psize && *psize > 0);
7723 #define ZIO_COMPRESS_MASK(alg) (1ULL << (ZIO_COMPRESS_##alg))
7726 zdb_decompress_block(abd_t *pabd, void *buf, void *lbuf, uint64_t lsize,
7727 uint64_t psize, int flags)
7729 boolean_t exceeded = B_FALSE;
7731 * We don't know how the data was compressed, so just try
7732 * every decompress function at every inflated blocksize.
7734 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
7735 int cfuncs[ZIO_COMPRESS_FUNCTIONS] = { 0 };
7736 int *cfuncp = cfuncs;
7737 uint64_t maxlsize = SPA_MAXBLOCKSIZE;
7738 uint64_t mask = ZIO_COMPRESS_MASK(ON) | ZIO_COMPRESS_MASK(OFF) |
7739 ZIO_COMPRESS_MASK(INHERIT) | ZIO_COMPRESS_MASK(EMPTY) |
7740 (getenv("ZDB_NO_ZLE") ? ZIO_COMPRESS_MASK(ZLE) : 0);
7741 *cfuncp++ = ZIO_COMPRESS_LZ4;
7742 *cfuncp++ = ZIO_COMPRESS_LZJB;
7743 mask |= ZIO_COMPRESS_MASK(LZ4) | ZIO_COMPRESS_MASK(LZJB);
7744 for (int c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++)
7745 if (((1ULL << c) & mask) == 0)
7749 * On the one hand, with SPA_MAXBLOCKSIZE at 16MB, this
7750 * could take a while and we should let the user know
7751 * we are not stuck. On the other hand, printing progress
7752 * info gets old after a while. User can specify 'v' flag
7753 * to see the progression.
7756 lsize += SPA_MINBLOCKSIZE;
7759 for (; lsize <= maxlsize; lsize += SPA_MINBLOCKSIZE) {
7760 for (cfuncp = cfuncs; *cfuncp; cfuncp++) {
7761 if (flags & ZDB_FLAG_VERBOSE) {
7762 (void) fprintf(stderr,
7763 "Trying %05llx -> %05llx (%s)\n",
7764 (u_longlong_t)psize,
7765 (u_longlong_t)lsize,
7766 zio_compress_table[*cfuncp].\
7771 * We randomize lbuf2, and decompress to both
7772 * lbuf and lbuf2. This way, we will know if
7773 * decompression fill exactly to lsize.
7775 VERIFY0(random_get_pseudo_bytes(lbuf2, lsize));
7777 if (zio_decompress_data(*cfuncp, pabd,
7778 lbuf, psize, lsize, NULL) == 0 &&
7779 zio_decompress_data(*cfuncp, pabd,
7780 lbuf2, psize, lsize, NULL) == 0 &&
7781 bcmp(lbuf, lbuf2, lsize) == 0)
7787 umem_free(lbuf2, SPA_MAXBLOCKSIZE);
7789 if (lsize > maxlsize) {
7793 if (*cfuncp == ZIO_COMPRESS_ZLE) {
7794 printf("\nZLE decompression was selected. If you "
7795 "suspect the results are wrong,\ntry avoiding ZLE "
7796 "by setting and exporting ZDB_NO_ZLE=\"true\"\n");
7803 * Read a block from a pool and print it out. The syntax of the
7804 * block descriptor is:
7806 * pool:vdev_specifier:offset:[lsize/]psize[:flags]
7808 * pool - The name of the pool you wish to read from
7809 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
7810 * offset - offset, in hex, in bytes
7811 * size - Amount of data to read, in hex, in bytes
7812 * flags - A string of characters specifying options
7813 * b: Decode a blkptr at given offset within block
7814 * c: Calculate and display checksums
7815 * d: Decompress data before dumping
7816 * e: Byteswap data before dumping
7817 * g: Display data as a gang block header
7818 * i: Display as an indirect block
7819 * r: Dump raw data to stdout
7824 zdb_read_block(char *thing, spa_t *spa)
7826 blkptr_t blk, *bp = &blk;
7827 dva_t *dva = bp->blk_dva;
7829 uint64_t offset = 0, psize = 0, lsize = 0, blkptr_offset = 0;
7834 char *s, *p, *dup, *vdev, *flagstr, *sizes;
7836 boolean_t borrowed = B_FALSE, found = B_FALSE;
7838 dup = strdup(thing);
7839 s = strtok(dup, ":");
7841 s = strtok(NULL, ":");
7842 offset = strtoull(s ? s : "", NULL, 16);
7843 sizes = strtok(NULL, ":");
7844 s = strtok(NULL, ":");
7845 flagstr = strdup(s ? s : "");
7848 if (!zdb_parse_block_sizes(sizes, &lsize, &psize))
7849 s = "invalid size(s)";
7850 if (!IS_P2ALIGNED(psize, DEV_BSIZE) || !IS_P2ALIGNED(lsize, DEV_BSIZE))
7851 s = "size must be a multiple of sector size";
7852 if (!IS_P2ALIGNED(offset, DEV_BSIZE))
7853 s = "offset must be a multiple of sector size";
7855 (void) printf("Invalid block specifier: %s - %s\n", thing, s);
7859 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
7860 for (i = 0; i < strlen(flagstr); i++) {
7861 int bit = flagbits[(uchar_t)flagstr[i]];
7864 (void) printf("***Ignoring flag: %c\n",
7865 (uchar_t)flagstr[i]);
7871 p = &flagstr[i + 1];
7872 if (*p != ':' && *p != '\0') {
7873 int j = 0, nextbit = flagbits[(uchar_t)*p];
7874 char *end, offstr[8] = { 0 };
7875 if ((bit == ZDB_FLAG_PRINT_BLKPTR) &&
7877 /* look ahead to isolate the offset */
7878 while (nextbit == 0 &&
7879 strchr(flagbitstr, *p) == NULL) {
7882 if (i + j > strlen(flagstr))
7885 nextbit = flagbits[(uchar_t)*p];
7887 blkptr_offset = strtoull(offstr, &end,
7890 } else if (nextbit == 0) {
7891 (void) printf("***Ignoring flag arg:"
7892 " '%c'\n", (uchar_t)*p);
7897 if (blkptr_offset % sizeof (blkptr_t)) {
7898 printf("Block pointer offset 0x%llx "
7899 "must be divisible by 0x%x\n",
7900 (longlong_t)blkptr_offset, (int)sizeof (blkptr_t));
7903 if (found == B_FALSE && strlen(flagstr) > 0) {
7904 printf("Invalid flag arg: '%s'\n", flagstr);
7908 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
7910 (void) printf("***Invalid vdev: %s\n", vdev);
7915 (void) fprintf(stderr, "Found vdev: %s\n",
7918 (void) fprintf(stderr, "Found vdev type: %s\n",
7919 vd->vdev_ops->vdev_op_type);
7922 pabd = abd_alloc_for_io(SPA_MAXBLOCKSIZE, B_FALSE);
7923 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
7927 DVA_SET_VDEV(&dva[0], vd->vdev_id);
7928 DVA_SET_OFFSET(&dva[0], offset);
7929 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
7930 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
7932 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
7934 BP_SET_LSIZE(bp, lsize);
7935 BP_SET_PSIZE(bp, psize);
7936 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
7937 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
7938 BP_SET_TYPE(bp, DMU_OT_NONE);
7939 BP_SET_LEVEL(bp, 0);
7940 BP_SET_DEDUP(bp, 0);
7941 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
7943 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
7944 zio = zio_root(spa, NULL, NULL, 0);
7946 if (vd == vd->vdev_top) {
7948 * Treat this as a normal block read.
7950 zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL,
7951 ZIO_PRIORITY_SYNC_READ,
7952 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
7955 * Treat this as a vdev child I/O.
7957 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd,
7958 psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
7959 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_PROPAGATE |
7960 ZIO_FLAG_DONT_RETRY | ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW |
7961 ZIO_FLAG_OPTIONAL, NULL, NULL));
7964 error = zio_wait(zio);
7965 spa_config_exit(spa, SCL_STATE, FTAG);
7968 (void) printf("Read of %s failed, error: %d\n", thing, error);
7972 uint64_t orig_lsize = lsize;
7974 if (flags & ZDB_FLAG_DECOMPRESS) {
7975 boolean_t failed = zdb_decompress_block(pabd, buf, lbuf,
7976 lsize, psize, flags);
7978 (void) printf("Decompress of %s failed\n", thing);
7982 buf = abd_borrow_buf_copy(pabd, lsize);
7986 * Try to detect invalid block pointer. If invalid, try
7989 if ((flags & ZDB_FLAG_PRINT_BLKPTR || flags & ZDB_FLAG_INDIRECT) &&
7990 !(flags & ZDB_FLAG_DECOMPRESS)) {
7991 const blkptr_t *b = (const blkptr_t *)(void *)
7992 ((uintptr_t)buf + (uintptr_t)blkptr_offset);
7993 if (zfs_blkptr_verify(spa, b, B_FALSE, BLK_VERIFY_ONLY) ==
7995 abd_return_buf_copy(pabd, buf, lsize);
7998 boolean_t failed = zdb_decompress_block(pabd, buf,
7999 lbuf, lsize, psize, flags);
8000 b = (const blkptr_t *)(void *)
8001 ((uintptr_t)buf + (uintptr_t)blkptr_offset);
8002 if (failed || zfs_blkptr_verify(spa, b, B_FALSE,
8003 BLK_VERIFY_LOG) == B_FALSE) {
8004 printf("invalid block pointer at this DVA\n");
8010 if (flags & ZDB_FLAG_PRINT_BLKPTR)
8011 zdb_print_blkptr((blkptr_t *)(void *)
8012 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
8013 else if (flags & ZDB_FLAG_RAW)
8014 zdb_dump_block_raw(buf, lsize, flags);
8015 else if (flags & ZDB_FLAG_INDIRECT)
8016 zdb_dump_indirect((blkptr_t *)buf,
8017 orig_lsize / sizeof (blkptr_t), flags);
8018 else if (flags & ZDB_FLAG_GBH)
8019 zdb_dump_gbh(buf, flags);
8021 zdb_dump_block(thing, buf, lsize, flags);
8024 * If :c was specified, iterate through the checksum table to
8025 * calculate and display each checksum for our specified
8028 if ((flags & ZDB_FLAG_CHECKSUM) && !(flags & ZDB_FLAG_RAW) &&
8029 !(flags & ZDB_FLAG_GBH)) {
8031 (void) printf("\n");
8032 for (enum zio_checksum ck = ZIO_CHECKSUM_LABEL;
8033 ck < ZIO_CHECKSUM_FUNCTIONS; ck++) {
8035 if ((zio_checksum_table[ck].ci_flags &
8036 ZCHECKSUM_FLAG_EMBEDDED) ||
8037 ck == ZIO_CHECKSUM_NOPARITY) {
8040 BP_SET_CHECKSUM(bp, ck);
8041 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
8042 czio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
8045 if (vd == vd->vdev_top) {
8046 zio_nowait(zio_read(czio, spa, bp, pabd, psize,
8048 ZIO_PRIORITY_SYNC_READ,
8049 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW |
8050 ZIO_FLAG_DONT_RETRY, NULL));
8052 zio_nowait(zio_vdev_child_io(czio, bp, vd,
8053 offset, pabd, psize, ZIO_TYPE_READ,
8054 ZIO_PRIORITY_SYNC_READ,
8055 ZIO_FLAG_DONT_CACHE |
8056 ZIO_FLAG_DONT_PROPAGATE |
8057 ZIO_FLAG_DONT_RETRY |
8058 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW |
8059 ZIO_FLAG_SPECULATIVE |
8060 ZIO_FLAG_OPTIONAL, NULL, NULL));
8062 error = zio_wait(czio);
8063 if (error == 0 || error == ECKSUM) {
8064 zio_t *ck_zio = zio_root(spa, NULL, NULL, 0);
8066 DVA_GET_OFFSET(&bp->blk_dva[0]);
8068 zio_checksum_compute(ck_zio, ck, pabd, lsize);
8069 printf("%12s\tcksum=%llx:%llx:%llx:%llx\n",
8070 zio_checksum_table[ck].ci_name,
8071 (u_longlong_t)bp->blk_cksum.zc_word[0],
8072 (u_longlong_t)bp->blk_cksum.zc_word[1],
8073 (u_longlong_t)bp->blk_cksum.zc_word[2],
8074 (u_longlong_t)bp->blk_cksum.zc_word[3]);
8077 printf("error %d reading block\n", error);
8079 spa_config_exit(spa, SCL_STATE, FTAG);
8084 abd_return_buf_copy(pabd, buf, lsize);
8088 umem_free(lbuf, SPA_MAXBLOCKSIZE);
8095 zdb_embedded_block(char *thing)
8098 unsigned long long *words = (void *)&bp;
8102 bzero(&bp, sizeof (bp));
8103 err = sscanf(thing, "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx:"
8104 "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx",
8105 words + 0, words + 1, words + 2, words + 3,
8106 words + 4, words + 5, words + 6, words + 7,
8107 words + 8, words + 9, words + 10, words + 11,
8108 words + 12, words + 13, words + 14, words + 15);
8110 (void) fprintf(stderr, "invalid input format\n");
8113 ASSERT3U(BPE_GET_LSIZE(&bp), <=, SPA_MAXBLOCKSIZE);
8114 buf = malloc(SPA_MAXBLOCKSIZE);
8116 (void) fprintf(stderr, "out of memory\n");
8119 err = decode_embedded_bp(&bp, buf, BPE_GET_LSIZE(&bp));
8121 (void) fprintf(stderr, "decode failed: %u\n", err);
8124 zdb_dump_block_raw(buf, BPE_GET_LSIZE(&bp), 0);
8129 main(int argc, char **argv)
8132 struct rlimit rl = { 1024, 1024 };
8134 objset_t *os = NULL;
8138 char **searchdirs = NULL;
8140 char *target, *target_pool, dsname[ZFS_MAX_DATASET_NAME_LEN];
8141 nvlist_t *policy = NULL;
8142 uint64_t max_txg = UINT64_MAX;
8143 int64_t objset_id = -1;
8144 int flags = ZFS_IMPORT_MISSING_LOG;
8145 int rewind = ZPOOL_NEVER_REWIND;
8146 char *spa_config_path_env, *objset_str;
8147 boolean_t target_is_spa = B_TRUE, dataset_lookup = B_FALSE;
8148 nvlist_t *cfg = NULL;
8150 (void) setrlimit(RLIMIT_NOFILE, &rl);
8151 (void) enable_extended_FILE_stdio(-1, -1);
8153 dprintf_setup(&argc, argv);
8156 * If there is an environment variable SPA_CONFIG_PATH it overrides
8157 * default spa_config_path setting. If -U flag is specified it will
8158 * override this environment variable settings once again.
8160 spa_config_path_env = getenv("SPA_CONFIG_PATH");
8161 if (spa_config_path_env != NULL)
8162 spa_config_path = spa_config_path_env;
8165 * For performance reasons, we set this tunable down. We do so before
8166 * the arg parsing section so that the user can override this value if
8169 zfs_btree_verify_intensity = 3;
8171 while ((c = getopt(argc, argv,
8172 "AbcCdDeEFGhiI:klLmMo:Op:PqRsSt:uU:vVx:XYyZ")) != -1) {
8207 zfs_reconstruct_indirect_combinations_max = INT_MAX;
8208 zfs_deadman_enabled = 0;
8210 /* NB: Sort single match options below. */
8212 max_inflight_bytes = strtoull(optarg, NULL, 0);
8213 if (max_inflight_bytes == 0) {
8214 (void) fprintf(stderr, "maximum number "
8215 "of inflight bytes must be greater "
8221 error = set_global_var(optarg);
8226 if (searchdirs == NULL) {
8227 searchdirs = umem_alloc(sizeof (char *),
8230 char **tmp = umem_alloc((nsearch + 1) *
8231 sizeof (char *), UMEM_NOFAIL);
8232 bcopy(searchdirs, tmp, nsearch *
8234 umem_free(searchdirs,
8235 nsearch * sizeof (char *));
8238 searchdirs[nsearch++] = optarg;
8241 max_txg = strtoull(optarg, NULL, 0);
8242 if (max_txg < TXG_INITIAL) {
8243 (void) fprintf(stderr, "incorrect txg "
8244 "specified: %s\n", optarg);
8249 spa_config_path = optarg;
8250 if (spa_config_path[0] != '/') {
8251 (void) fprintf(stderr,
8252 "cachefile must be an absolute path "
8253 "(i.e. start with a slash)\n");
8261 flags = ZFS_IMPORT_VERBATIM;
8264 vn_dumpdir = optarg;
8272 if (!dump_opt['e'] && searchdirs != NULL) {
8273 (void) fprintf(stderr, "-p option requires use of -e\n");
8276 if (dump_opt['d']) {
8277 /* <pool>[/<dataset | objset id> is accepted */
8278 if (argv[2] && (objset_str = strchr(argv[2], '/')) != NULL &&
8279 objset_str++ != NULL) {
8282 objset_id = strtoull(objset_str, &endptr, 0);
8283 /* dataset 0 is the same as opening the pool */
8284 if (errno == 0 && endptr != objset_str &&
8286 target_is_spa = B_FALSE;
8287 dataset_lookup = B_TRUE;
8288 } else if (objset_id != 0) {
8289 printf("failed to open objset %s "
8290 "%llu %s", objset_str,
8291 (u_longlong_t)objset_id,
8295 /* normal dataset name not an objset ID */
8296 if (endptr == objset_str) {
8304 * ZDB does not typically re-read blocks; therefore limit the ARC
8305 * to 256 MB, which can be used entirely for metadata.
8307 zfs_arc_min = zfs_arc_meta_min = 2ULL << SPA_MAXBLOCKSHIFT;
8308 zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
8312 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
8313 * "zdb -b" uses traversal prefetch which uses async reads.
8314 * For good performance, let several of them be active at once.
8316 zfs_vdev_async_read_max_active = 10;
8319 * Disable reference tracking for better performance.
8321 reference_tracking_enable = B_FALSE;
8324 * Do not fail spa_load when spa_load_verify fails. This is needed
8325 * to load non-idle pools.
8327 spa_load_verify_dryrun = B_TRUE;
8329 kernel_init(SPA_MODE_READ);
8332 verbose = MAX(verbose, 1);
8334 for (c = 0; c < 256; c++) {
8335 if (dump_all && strchr("AeEFklLOPRSXy", c) == NULL)
8338 dump_opt[c] += verbose;
8341 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
8342 zfs_recover = (dump_opt['A'] > 1);
8346 if (argc < 2 && dump_opt['R'])
8349 if (dump_opt['E']) {
8352 zdb_embedded_block(argv[0]);
8357 if (!dump_opt['e'] && dump_opt['C']) {
8358 dump_cachefile(spa_config_path);
8365 return (dump_label(argv[0]));
8367 if (dump_opt['O']) {
8370 dump_opt['v'] = verbose + 3;
8371 return (dump_path(argv[0], argv[1]));
8374 if (dump_opt['X'] || dump_opt['F'])
8375 rewind = ZPOOL_DO_REWIND |
8376 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
8378 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
8379 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, max_txg) != 0 ||
8380 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, rewind) != 0)
8381 fatal("internal error: %s", strerror(ENOMEM));
8386 if (strpbrk(target, "/@") != NULL) {
8389 target_pool = strdup(target);
8390 *strpbrk(target_pool, "/@") = '\0';
8392 target_is_spa = B_FALSE;
8393 targetlen = strlen(target);
8394 if (targetlen && target[targetlen - 1] == '/')
8395 target[targetlen - 1] = '\0';
8397 target_pool = target;
8400 if (dump_opt['e']) {
8401 importargs_t args = { 0 };
8403 args.paths = nsearch;
8404 args.path = searchdirs;
8405 args.can_be_active = B_TRUE;
8407 error = zpool_find_config(NULL, target_pool, &cfg, &args,
8408 &libzpool_config_ops);
8412 if (nvlist_add_nvlist(cfg,
8413 ZPOOL_LOAD_POLICY, policy) != 0) {
8414 fatal("can't open '%s': %s",
8415 target, strerror(ENOMEM));
8418 if (dump_opt['C'] > 1) {
8419 (void) printf("\nConfiguration for import:\n");
8420 dump_nvlist(cfg, 8);
8424 * Disable the activity check to allow examination of
8427 error = spa_import(target_pool, cfg, NULL,
8428 flags | ZFS_IMPORT_SKIP_MMP);
8433 * import_checkpointed_state makes the assumption that the
8434 * target pool that we pass it is already part of the spa
8435 * namespace. Because of that we need to make sure to call
8436 * it always after the -e option has been processed, which
8437 * imports the pool to the namespace if it's not in the
8440 char *checkpoint_pool = NULL;
8441 char *checkpoint_target = NULL;
8442 if (dump_opt['k']) {
8443 checkpoint_pool = import_checkpointed_state(target, cfg,
8444 &checkpoint_target);
8446 if (checkpoint_target != NULL)
8447 target = checkpoint_target;
8450 if (target_pool != target)
8454 if (dump_opt['k'] && (target_is_spa || dump_opt['R'])) {
8455 ASSERT(checkpoint_pool != NULL);
8456 ASSERT(checkpoint_target == NULL);
8458 error = spa_open(checkpoint_pool, &spa, FTAG);
8460 fatal("Tried to open pool \"%s\" but "
8461 "spa_open() failed with error %d\n",
8462 checkpoint_pool, error);
8465 } else if (target_is_spa || dump_opt['R'] || objset_id == 0) {
8466 zdb_set_skip_mmp(target);
8467 error = spa_open_rewind(target, &spa, FTAG, policy,
8471 * If we're missing the log device then
8472 * try opening the pool after clearing the
8475 mutex_enter(&spa_namespace_lock);
8476 if ((spa = spa_lookup(target)) != NULL &&
8477 spa->spa_log_state == SPA_LOG_MISSING) {
8478 spa->spa_log_state = SPA_LOG_CLEAR;
8481 mutex_exit(&spa_namespace_lock);
8484 error = spa_open_rewind(target, &spa,
8485 FTAG, policy, NULL);
8488 } else if (strpbrk(target, "#") != NULL) {
8490 error = dsl_pool_hold(target, FTAG, &dp);
8492 fatal("can't dump '%s': %s", target,
8495 error = dump_bookmark(dp, target, B_TRUE, verbose > 1);
8496 dsl_pool_rele(dp, FTAG);
8498 fatal("can't dump '%s': %s", target,
8503 zdb_set_skip_mmp(target);
8504 if (dataset_lookup == B_TRUE) {
8506 * Use the supplied id to get the name
8509 error = spa_open(target, &spa, FTAG);
8511 error = name_from_objset_id(spa,
8513 spa_close(spa, FTAG);
8519 error = open_objset(target, FTAG, &os);
8521 spa = dmu_objset_spa(os);
8524 nvlist_free(policy);
8527 fatal("can't open '%s': %s", target, strerror(error));
8530 * Set the pool failure mode to panic in order to prevent the pool
8531 * from suspending. A suspended I/O will have no way to resume and
8532 * can prevent the zdb(8) command from terminating as expected.
8535 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
8539 if (!dump_opt['R']) {
8540 flagbits['d'] = ZOR_FLAG_DIRECTORY;
8541 flagbits['f'] = ZOR_FLAG_PLAIN_FILE;
8542 flagbits['m'] = ZOR_FLAG_SPACE_MAP;
8543 flagbits['z'] = ZOR_FLAG_ZAP;
8544 flagbits['A'] = ZOR_FLAG_ALL_TYPES;
8546 if (argc > 0 && dump_opt['d']) {
8547 zopt_object_args = argc;
8548 zopt_object_ranges = calloc(zopt_object_args,
8549 sizeof (zopt_object_range_t));
8550 for (unsigned i = 0; i < zopt_object_args; i++) {
8554 err = parse_object_range(argv[i],
8555 &zopt_object_ranges[i], &msg);
8557 fatal("Bad object or range: '%s': %s\n",
8558 argv[i], msg ? msg : "");
8560 } else if (argc > 0 && dump_opt['m']) {
8561 zopt_metaslab_args = argc;
8562 zopt_metaslab = calloc(zopt_metaslab_args,
8564 for (unsigned i = 0; i < zopt_metaslab_args; i++) {
8566 zopt_metaslab[i] = strtoull(argv[i], NULL, 0);
8567 if (zopt_metaslab[i] == 0 && errno != 0)
8568 fatal("bad number %s: %s", argv[i],
8574 } else if (zopt_object_args > 0 && !dump_opt['m']) {
8575 dump_objset(spa->spa_meta_objset);
8580 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
8581 flagbits['c'] = ZDB_FLAG_CHECKSUM;
8582 flagbits['d'] = ZDB_FLAG_DECOMPRESS;
8583 flagbits['e'] = ZDB_FLAG_BSWAP;
8584 flagbits['g'] = ZDB_FLAG_GBH;
8585 flagbits['i'] = ZDB_FLAG_INDIRECT;
8586 flagbits['r'] = ZDB_FLAG_RAW;
8587 flagbits['v'] = ZDB_FLAG_VERBOSE;
8589 for (int i = 0; i < argc; i++)
8590 zdb_read_block(argv[i], spa);
8593 if (dump_opt['k']) {
8594 free(checkpoint_pool);
8596 free(checkpoint_target);
8600 close_objset(os, FTAG);
8602 spa_close(spa, FTAG);
8605 fuid_table_destroy();
8607 dump_debug_buffer();