xref: /freebsd/sys/contrib/openzfs/cmd/zdb/zdb.c (revision 34f9f5680c9d5d5138e427ba3aeab0138cec3882)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 
13 /*
14  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
15  * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
16  * Copyright (c) 2014 Integros [integros.com]
17  * Copyright 2016 Nexenta Systems, Inc.
18  * Copyright (c) 2017, 2018 Lawrence Livermore National Security, LLC.
19  * Copyright (c) 2015, 2017, Intel Corporation.
20  * Copyright (c) 2020 Datto Inc.
21  * Copyright (c) 2020, The FreeBSD Foundation [1]
22  *
23  * [1] Portions of this software were developed by Allan Jude
24  *     under sponsorship from the FreeBSD Foundation.
25  * Copyright (c) 2021 Allan Jude
26  * Copyright (c) 2021 Toomas Soome <tsoome@me.com>
27  * Copyright (c) 2023, 2024, Klara Inc.
28  * Copyright (c) 2023, Rob Norris <robn@despairlabs.com>
29  * Copyright (c) 2026, TrueNAS.
30  * Copyright 2026 Edgecast Cloud LLC.
31  */
32 
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <ctype.h>
37 #include <getopt.h>
38 #include <openssl/evp.h>
39 #include <sys/zfs_context.h>
40 #include <sys/spa.h>
41 #include <sys/spa_impl.h>
42 #include <sys/dmu.h>
43 #include <sys/zap.h>
44 #include <sys/zap_impl.h>
45 #include <sys/fs/zfs.h>
46 #include <sys/zfs_znode.h>
47 #include <sys/zfs_sa.h>
48 #include <sys/sa.h>
49 #include <sys/sa_impl.h>
50 #include <sys/vdev.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>
58 #include <sys/dbuf.h>
59 #include <sys/zil.h>
60 #include <sys/zil_impl.h>
61 #include <sys/stat.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>
68 #include <sys/arc.h>
69 #include <sys/arc_impl.h>
70 #include <sys/ddt.h>
71 #include <sys/ddt_impl.h>
72 #include <sys/zfeature.h>
73 #include <sys/abd.h>
74 #include <sys/blkptr.h>
75 #include <sys/dsl_crypt.h>
76 #include <sys/dsl_scan.h>
77 #include <sys/btree.h>
78 #include <sys/brt.h>
79 #include <sys/brt_impl.h>
80 #include <zfs_comutil.h>
81 #include <sys/vdev_raidz.h>
82 #include <sys/vdev_raidz_impl.h>
83 #include <sys/zstd/zstd.h>
84 #include <sys/backtrace.h>
85 
86 #include <libzpool.h>
87 #include <libnvpair.h>
88 #include <libzutil.h>
89 #include <libzfs_core.h>
90 
91 #include <libzdb.h>
92 
93 #include "zdb.h"
94 
95 
96 extern int reference_tracking_enable;
97 extern int zfs_recover;
98 extern uint_t zfs_vdev_async_read_max_active;
99 extern boolean_t spa_load_verify_dryrun;
100 extern boolean_t spa_mode_readable_spacemaps;
101 extern uint_t zfs_reconstruct_indirect_combinations_max;
102 extern uint_t zfs_btree_verify_intensity;
103 
104 enum {
105 	ARG_ALLOCATED = 256,
106 	ARG_BLOCK_BIN_MODE,
107 	ARG_BLOCK_CLASSES,
108 };
109 
110 static const char cmdname[] = "zdb";
111 uint8_t dump_opt[512];
112 
113 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
114 
115 static uint64_t *zopt_metaslab = NULL;
116 static unsigned zopt_metaslab_args = 0;
117 
118 
119 static zopt_object_range_t *zopt_object_ranges = NULL;
120 static unsigned zopt_object_args = 0;
121 
122 static int flagbits[256];
123 
124 
125 static uint64_t max_inflight_bytes = 256 * 1024 * 1024; /* 256MB */
126 static int leaked_objects = 0;
127 static zfs_range_tree_t *mos_refd_objs;
128 static spa_t *spa;
129 static objset_t *os;
130 static boolean_t kernel_init_done;
131 static boolean_t corruption_found = B_FALSE;
132 
133 static enum {
134 	BIN_AUTO = 0,
135 	BIN_PSIZE,
136 	BIN_LSIZE,
137 	BIN_ASIZE,
138 } block_bin_mode = BIN_AUTO;
139 
140 static enum {
141 	CLASS_NORMAL = 1 << 1,
142 	CLASS_SPECIAL = 1 << 2,
143 	CLASS_DEDUP = 1 << 3,
144 	CLASS_OTHER = 1 << 4,
145 } block_classes = 0;
146 
147 static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *,
148     boolean_t);
149 static void mos_obj_refd(uint64_t);
150 static void mos_obj_refd_multiple(uint64_t);
151 static int dump_bpobj_cb(void *arg, const blkptr_t *bp, boolean_t free,
152     dmu_tx_t *tx);
153 
154 
155 
156 static void zdb_print_blkptr(const blkptr_t *bp, int flags);
157 static void zdb_exit(int reason);
158 
159 typedef struct sublivelist_verify_block_refcnt {
160 	/* block pointer entry in livelist being verified */
161 	blkptr_t svbr_blk;
162 
163 	/*
164 	 * Refcount gets incremented to 1 when we encounter the first
165 	 * FREE entry for the svfbr block pointer and a node for it
166 	 * is created in our ZDB verification/tracking metadata.
167 	 *
168 	 * As we encounter more FREE entries we increment this counter
169 	 * and similarly decrement it whenever we find the respective
170 	 * ALLOC entries for this block.
171 	 *
172 	 * When the refcount gets to 0 it means that all the FREE and
173 	 * ALLOC entries of this block have paired up and we no longer
174 	 * need to track it in our verification logic (e.g. the node
175 	 * containing this struct in our verification data structure
176 	 * should be freed).
177 	 *
178 	 * [refer to sublivelist_verify_blkptr() for the actual code]
179 	 */
180 	uint32_t svbr_refcnt;
181 } sublivelist_verify_block_refcnt_t;
182 
183 static int
184 sublivelist_block_refcnt_compare(const void *larg, const void *rarg)
185 {
186 	const sublivelist_verify_block_refcnt_t *l = larg;
187 	const sublivelist_verify_block_refcnt_t *r = rarg;
188 	return (livelist_compare(&l->svbr_blk, &r->svbr_blk));
189 }
190 
191 static int
192 sublivelist_verify_blkptr(void *arg, const blkptr_t *bp, boolean_t free,
193     dmu_tx_t *tx)
194 {
195 	ASSERT0P(tx);
196 	struct sublivelist_verify *sv = arg;
197 	sublivelist_verify_block_refcnt_t current = {
198 			.svbr_blk = *bp,
199 
200 			/*
201 			 * Start with 1 in case this is the first free entry.
202 			 * This field is not used for our B-Tree comparisons
203 			 * anyway.
204 			 */
205 			.svbr_refcnt = 1,
206 	};
207 
208 	zfs_btree_index_t where;
209 	sublivelist_verify_block_refcnt_t *pair =
210 	    zfs_btree_find(&sv->sv_pair, &current, &where);
211 	if (free) {
212 		if (pair == NULL) {
213 			/* first free entry for this block pointer */
214 			zfs_btree_add(&sv->sv_pair, &current);
215 		} else {
216 			pair->svbr_refcnt++;
217 		}
218 	} else {
219 		if (pair == NULL) {
220 			/* block that is currently marked as allocated */
221 			for (int i = 0; i < SPA_DVAS_PER_BP; i++) {
222 				if (DVA_IS_EMPTY(&bp->blk_dva[i]))
223 					break;
224 				sublivelist_verify_block_t svb = {
225 				    .svb_dva = bp->blk_dva[i],
226 				    .svb_allocated_txg =
227 				    BP_GET_BIRTH(bp)
228 				};
229 
230 				if (zfs_btree_find(&sv->sv_leftover, &svb,
231 				    &where) == NULL) {
232 					zfs_btree_add_idx(&sv->sv_leftover,
233 					    &svb, &where);
234 				}
235 			}
236 		} else {
237 			/* alloc matches a free entry */
238 			pair->svbr_refcnt--;
239 			if (pair->svbr_refcnt == 0) {
240 				/* all allocs and frees have been matched */
241 				zfs_btree_remove_idx(&sv->sv_pair, &where);
242 			}
243 		}
244 	}
245 
246 	return (0);
247 }
248 
249 static int
250 sublivelist_verify_func(void *args, dsl_deadlist_entry_t *dle)
251 {
252 	int err;
253 	struct sublivelist_verify *sv = args;
254 
255 	zfs_btree_create(&sv->sv_pair, sublivelist_block_refcnt_compare, NULL,
256 	    sizeof (sublivelist_verify_block_refcnt_t));
257 
258 	err = bpobj_iterate_nofree(&dle->dle_bpobj, sublivelist_verify_blkptr,
259 	    sv, NULL);
260 
261 	sublivelist_verify_block_refcnt_t *e;
262 	zfs_btree_index_t *cookie = NULL;
263 	while ((e = zfs_btree_destroy_nodes(&sv->sv_pair, &cookie)) != NULL) {
264 		char blkbuf[BP_SPRINTF_LEN];
265 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf),
266 		    &e->svbr_blk, B_TRUE);
267 		(void) printf("\tERROR: %d unmatched FREE(s): %s\n",
268 		    e->svbr_refcnt, blkbuf);
269 		corruption_found = B_TRUE;
270 	}
271 	zfs_btree_destroy(&sv->sv_pair);
272 
273 	return (err);
274 }
275 
276 static int
277 livelist_block_compare(const void *larg, const void *rarg)
278 {
279 	const sublivelist_verify_block_t *l = larg;
280 	const sublivelist_verify_block_t *r = rarg;
281 
282 	if (DVA_GET_VDEV(&l->svb_dva) < DVA_GET_VDEV(&r->svb_dva))
283 		return (-1);
284 	else if (DVA_GET_VDEV(&l->svb_dva) > DVA_GET_VDEV(&r->svb_dva))
285 		return (+1);
286 
287 	if (DVA_GET_OFFSET(&l->svb_dva) < DVA_GET_OFFSET(&r->svb_dva))
288 		return (-1);
289 	else if (DVA_GET_OFFSET(&l->svb_dva) > DVA_GET_OFFSET(&r->svb_dva))
290 		return (+1);
291 
292 	if (DVA_GET_ASIZE(&l->svb_dva) < DVA_GET_ASIZE(&r->svb_dva))
293 		return (-1);
294 	else if (DVA_GET_ASIZE(&l->svb_dva) > DVA_GET_ASIZE(&r->svb_dva))
295 		return (+1);
296 
297 	return (0);
298 }
299 
300 /*
301  * Check for errors in a livelist while tracking all unfreed ALLOCs in the
302  * sublivelist_verify_t: sv->sv_leftover
303  */
304 static void
305 livelist_verify(dsl_deadlist_t *dl, void *arg)
306 {
307 	sublivelist_verify_t *sv = arg;
308 	dsl_deadlist_iterate(dl, sublivelist_verify_func, sv);
309 }
310 
311 /*
312  * Check for errors in the livelist entry and discard the intermediary
313  * data structures
314  */
315 static int
316 sublivelist_verify_lightweight(void *args, dsl_deadlist_entry_t *dle)
317 {
318 	(void) args;
319 	sublivelist_verify_t sv;
320 	zfs_btree_create(&sv.sv_leftover, livelist_block_compare, NULL,
321 	    sizeof (sublivelist_verify_block_t));
322 	int err = sublivelist_verify_func(&sv, dle);
323 	zfs_btree_clear(&sv.sv_leftover);
324 	zfs_btree_destroy(&sv.sv_leftover);
325 	return (err);
326 }
327 
328 typedef struct metaslab_verify {
329 	/*
330 	 * Tree containing all the leftover ALLOCs from the livelists
331 	 * that are part of this metaslab.
332 	 */
333 	zfs_btree_t mv_livelist_allocs;
334 
335 	/*
336 	 * Metaslab information.
337 	 */
338 	uint64_t mv_vdid;
339 	uint64_t mv_msid;
340 	uint64_t mv_start;
341 	uint64_t mv_end;
342 
343 	/*
344 	 * What's currently allocated for this metaslab.
345 	 */
346 	zfs_range_tree_t *mv_allocated;
347 } metaslab_verify_t;
348 
349 typedef void ll_iter_t(dsl_deadlist_t *ll, void *arg);
350 
351 typedef int (*zdb_log_sm_cb_t)(spa_t *spa, space_map_entry_t *sme, uint64_t txg,
352     void *arg);
353 
354 typedef struct unflushed_iter_cb_arg {
355 	spa_t *uic_spa;
356 	uint64_t uic_txg;
357 	void *uic_arg;
358 	zdb_log_sm_cb_t uic_cb;
359 } unflushed_iter_cb_arg_t;
360 
361 static int
362 iterate_through_spacemap_logs_cb(space_map_entry_t *sme, void *arg)
363 {
364 	unflushed_iter_cb_arg_t *uic = arg;
365 	return (uic->uic_cb(uic->uic_spa, sme, uic->uic_txg, uic->uic_arg));
366 }
367 
368 static void
369 iterate_through_spacemap_logs(spa_t *spa, zdb_log_sm_cb_t cb, void *arg)
370 {
371 	if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))
372 		return;
373 
374 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
375 	for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg);
376 	    sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls)) {
377 		space_map_t *sm = NULL;
378 		VERIFY0(space_map_open(&sm, spa_meta_objset(spa),
379 		    sls->sls_sm_obj, 0, UINT64_MAX, SPA_MINBLOCKSHIFT));
380 
381 		unflushed_iter_cb_arg_t uic = {
382 			.uic_spa = spa,
383 			.uic_txg = sls->sls_txg,
384 			.uic_arg = arg,
385 			.uic_cb = cb
386 		};
387 		VERIFY0(space_map_iterate(sm, space_map_length(sm),
388 		    iterate_through_spacemap_logs_cb, &uic));
389 		space_map_close(sm);
390 	}
391 	spa_config_exit(spa, SCL_CONFIG, FTAG);
392 }
393 
394 static void
395 verify_livelist_allocs(metaslab_verify_t *mv, uint64_t txg,
396     uint64_t offset, uint64_t size)
397 {
398 	sublivelist_verify_block_t svb = {{{0}}};
399 	DVA_SET_VDEV(&svb.svb_dva, mv->mv_vdid);
400 	DVA_SET_OFFSET(&svb.svb_dva, offset);
401 	DVA_SET_ASIZE(&svb.svb_dva, 0);
402 	zfs_btree_index_t where;
403 	uint64_t end_offset = offset + size;
404 
405 	/*
406 	 *  Look for an exact match for spacemap entry in the livelist entries.
407 	 *  Then, look for other livelist entries that fall within the range
408 	 *  of the spacemap entry as it may have been condensed
409 	 */
410 	sublivelist_verify_block_t *found =
411 	    zfs_btree_find(&mv->mv_livelist_allocs, &svb, &where);
412 	if (found == NULL) {
413 		found = zfs_btree_next(&mv->mv_livelist_allocs, &where, &where);
414 	}
415 	for (; found != NULL && DVA_GET_VDEV(&found->svb_dva) == mv->mv_vdid &&
416 	    DVA_GET_OFFSET(&found->svb_dva) < end_offset;
417 	    found = zfs_btree_next(&mv->mv_livelist_allocs, &where, &where)) {
418 		if (found->svb_allocated_txg <= txg) {
419 			(void) printf("ERROR: Livelist ALLOC [%llx:%llx] "
420 			    "from TXG %llx FREED at TXG %llx\n",
421 			    (u_longlong_t)DVA_GET_OFFSET(&found->svb_dva),
422 			    (u_longlong_t)DVA_GET_ASIZE(&found->svb_dva),
423 			    (u_longlong_t)found->svb_allocated_txg,
424 			    (u_longlong_t)txg);
425 			corruption_found = B_TRUE;
426 		}
427 	}
428 }
429 
430 static int
431 metaslab_spacemap_validation_cb(space_map_entry_t *sme, void *arg)
432 {
433 	metaslab_verify_t *mv = arg;
434 	uint64_t offset = sme->sme_offset;
435 	uint64_t size = sme->sme_run;
436 	uint64_t txg = sme->sme_txg;
437 
438 	if (sme->sme_type == SM_ALLOC) {
439 		if (zfs_range_tree_contains(mv->mv_allocated,
440 		    offset, size)) {
441 			(void) printf("ERROR: DOUBLE ALLOC: "
442 			    "%llu [%llx:%llx] "
443 			    "%llu:%llu LOG_SM\n",
444 			    (u_longlong_t)txg, (u_longlong_t)offset,
445 			    (u_longlong_t)size, (u_longlong_t)mv->mv_vdid,
446 			    (u_longlong_t)mv->mv_msid);
447 			corruption_found = B_TRUE;
448 		} else {
449 			zfs_range_tree_add(mv->mv_allocated,
450 			    offset, size);
451 		}
452 	} else {
453 		if (!zfs_range_tree_contains(mv->mv_allocated,
454 		    offset, size)) {
455 			(void) printf("ERROR: DOUBLE FREE: "
456 			    "%llu [%llx:%llx] "
457 			    "%llu:%llu LOG_SM\n",
458 			    (u_longlong_t)txg, (u_longlong_t)offset,
459 			    (u_longlong_t)size, (u_longlong_t)mv->mv_vdid,
460 			    (u_longlong_t)mv->mv_msid);
461 			corruption_found = B_TRUE;
462 		} else {
463 			zfs_range_tree_remove(mv->mv_allocated,
464 			    offset, size);
465 		}
466 	}
467 
468 	if (sme->sme_type != SM_ALLOC) {
469 		/*
470 		 * If something is freed in the spacemap, verify that
471 		 * it is not listed as allocated in the livelist.
472 		 */
473 		verify_livelist_allocs(mv, txg, offset, size);
474 	}
475 	return (0);
476 }
477 
478 static int
479 spacemap_check_sm_log_cb(spa_t *spa, space_map_entry_t *sme,
480     uint64_t txg, void *arg)
481 {
482 	metaslab_verify_t *mv = arg;
483 	uint64_t offset = sme->sme_offset;
484 	uint64_t vdev_id = sme->sme_vdev;
485 
486 	vdev_t *vd = vdev_lookup_top(spa, vdev_id);
487 
488 	/* skip indirect vdevs */
489 	if (!vdev_is_concrete(vd))
490 		return (0);
491 
492 	if (vdev_id != mv->mv_vdid)
493 		return (0);
494 
495 	metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
496 	if (ms->ms_id != mv->mv_msid)
497 		return (0);
498 
499 	if (txg < metaslab_unflushed_txg(ms))
500 		return (0);
501 
502 
503 	ASSERT3U(txg, ==, sme->sme_txg);
504 	return (metaslab_spacemap_validation_cb(sme, mv));
505 }
506 
507 static void
508 spacemap_check_sm_log(spa_t *spa, metaslab_verify_t *mv)
509 {
510 	iterate_through_spacemap_logs(spa, spacemap_check_sm_log_cb, mv);
511 }
512 
513 static void
514 spacemap_check_ms_sm(space_map_t  *sm, metaslab_verify_t *mv)
515 {
516 	if (sm == NULL)
517 		return;
518 
519 	VERIFY0(space_map_iterate(sm, space_map_length(sm),
520 	    metaslab_spacemap_validation_cb, mv));
521 }
522 
523 static void iterate_deleted_livelists(spa_t *spa, ll_iter_t func, void *arg);
524 
525 /*
526  * Transfer blocks from sv_leftover tree to the mv_livelist_allocs if
527  * they are part of that metaslab (mv_msid).
528  */
529 static void
530 mv_populate_livelist_allocs(metaslab_verify_t *mv, sublivelist_verify_t *sv)
531 {
532 	zfs_btree_index_t where;
533 	sublivelist_verify_block_t *svb;
534 	ASSERT3U(zfs_btree_numnodes(&mv->mv_livelist_allocs), ==, 0);
535 	for (svb = zfs_btree_first(&sv->sv_leftover, &where);
536 	    svb != NULL;
537 	    svb = zfs_btree_next(&sv->sv_leftover, &where, &where)) {
538 		if (DVA_GET_VDEV(&svb->svb_dva) != mv->mv_vdid)
539 			continue;
540 
541 		if (DVA_GET_OFFSET(&svb->svb_dva) < mv->mv_start &&
542 		    (DVA_GET_OFFSET(&svb->svb_dva) +
543 		    DVA_GET_ASIZE(&svb->svb_dva)) > mv->mv_start) {
544 			(void) printf("ERROR: Found block that crosses "
545 			    "metaslab boundary: <%llu:%llx:%llx>\n",
546 			    (u_longlong_t)DVA_GET_VDEV(&svb->svb_dva),
547 			    (u_longlong_t)DVA_GET_OFFSET(&svb->svb_dva),
548 			    (u_longlong_t)DVA_GET_ASIZE(&svb->svb_dva));
549 			corruption_found = B_TRUE;
550 			continue;
551 		}
552 
553 		if (DVA_GET_OFFSET(&svb->svb_dva) < mv->mv_start)
554 			continue;
555 
556 		if (DVA_GET_OFFSET(&svb->svb_dva) >= mv->mv_end)
557 			continue;
558 
559 		if ((DVA_GET_OFFSET(&svb->svb_dva) +
560 		    DVA_GET_ASIZE(&svb->svb_dva)) > mv->mv_end) {
561 			(void) printf("ERROR: Found block that crosses "
562 			    "metaslab boundary: <%llu:%llx:%llx>\n",
563 			    (u_longlong_t)DVA_GET_VDEV(&svb->svb_dva),
564 			    (u_longlong_t)DVA_GET_OFFSET(&svb->svb_dva),
565 			    (u_longlong_t)DVA_GET_ASIZE(&svb->svb_dva));
566 			corruption_found = B_TRUE;
567 			continue;
568 		}
569 
570 		zfs_btree_add(&mv->mv_livelist_allocs, svb);
571 	}
572 
573 	for (svb = zfs_btree_first(&mv->mv_livelist_allocs, &where);
574 	    svb != NULL;
575 	    svb = zfs_btree_next(&mv->mv_livelist_allocs, &where, &where)) {
576 		zfs_btree_remove(&sv->sv_leftover, svb);
577 	}
578 }
579 
580 /*
581  * [Livelist Check]
582  * Iterate through all the sublivelists and:
583  * - report leftover frees (**)
584  * - record leftover ALLOCs together with their TXG [see Cross Check]
585  *
586  * (**) Note: Double ALLOCs are valid in datasets that have dedup
587  *      enabled. Similarly double FREEs are allowed as well but
588  *      only if they pair up with a corresponding ALLOC entry once
589  *      we our done with our sublivelist iteration.
590  *
591  * [Spacemap Check]
592  * for each metaslab:
593  * - iterate over spacemap and then the metaslab's entries in the
594  *   spacemap log, then report any double FREEs and ALLOCs (do not
595  *   blow up).
596  *
597  * [Cross Check]
598  * After finishing the Livelist Check phase and while being in the
599  * Spacemap Check phase, we find all the recorded leftover ALLOCs
600  * of the livelist check that are part of the metaslab that we are
601  * currently looking at in the Spacemap Check. We report any entries
602  * that are marked as ALLOCs in the livelists but have been actually
603  * freed (and potentially allocated again) after their TXG stamp in
604  * the spacemaps. Also report any ALLOCs from the livelists that
605  * belong to indirect vdevs (e.g. their vdev completed removal).
606  *
607  * Note that this will miss Log Spacemap entries that cancelled each other
608  * out before being flushed to the metaslab, so we are not guaranteed
609  * to match all erroneous ALLOCs.
610  */
611 static void
612 livelist_metaslab_validate(spa_t *spa)
613 {
614 	(void) printf("Verifying deleted livelist entries\n");
615 
616 	sublivelist_verify_t sv;
617 	zfs_btree_create(&sv.sv_leftover, livelist_block_compare, NULL,
618 	    sizeof (sublivelist_verify_block_t));
619 	iterate_deleted_livelists(spa, livelist_verify, &sv);
620 
621 	(void) printf("Verifying metaslab entries\n");
622 	vdev_t *rvd = spa->spa_root_vdev;
623 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
624 		vdev_t *vd = rvd->vdev_child[c];
625 
626 		if (!vdev_is_concrete(vd))
627 			continue;
628 
629 		for (uint64_t mid = 0; mid < vd->vdev_ms_count; mid++) {
630 			metaslab_t *m = vd->vdev_ms[mid];
631 
632 			(void) fprintf(stderr,
633 			    "\rverifying concrete vdev %llu, "
634 			    "metaslab %llu of %llu ...",
635 			    (longlong_t)vd->vdev_id,
636 			    (longlong_t)mid,
637 			    (longlong_t)vd->vdev_ms_count);
638 
639 			uint64_t shift, start;
640 			zfs_range_seg_type_t type =
641 			    metaslab_calculate_range_tree_type(vd, m,
642 			    &start, &shift);
643 			metaslab_verify_t mv;
644 			mv.mv_allocated = zfs_range_tree_create_flags(
645 			    NULL, type, NULL, start, shift,
646 			    0, "livelist_metaslab_validate:mv_allocated");
647 			mv.mv_vdid = vd->vdev_id;
648 			mv.mv_msid = m->ms_id;
649 			mv.mv_start = m->ms_start;
650 			mv.mv_end = m->ms_start + m->ms_size;
651 			zfs_btree_create(&mv.mv_livelist_allocs,
652 			    livelist_block_compare, NULL,
653 			    sizeof (sublivelist_verify_block_t));
654 
655 			mv_populate_livelist_allocs(&mv, &sv);
656 
657 			spacemap_check_ms_sm(m->ms_sm, &mv);
658 			spacemap_check_sm_log(spa, &mv);
659 
660 			zfs_range_tree_vacate(mv.mv_allocated, NULL, NULL);
661 			zfs_range_tree_destroy(mv.mv_allocated);
662 			zfs_btree_clear(&mv.mv_livelist_allocs);
663 			zfs_btree_destroy(&mv.mv_livelist_allocs);
664 		}
665 	}
666 	(void) fprintf(stderr, "\n");
667 
668 	/*
669 	 * If there are any segments in the leftover tree after we walked
670 	 * through all the metaslabs in the concrete vdevs then this means
671 	 * that we have segments in the livelists that belong to indirect
672 	 * vdevs and are marked as allocated.
673 	 */
674 	if (zfs_btree_numnodes(&sv.sv_leftover) == 0) {
675 		zfs_btree_destroy(&sv.sv_leftover);
676 		return;
677 	}
678 	(void) printf("ERROR: Found livelist blocks marked as allocated "
679 	    "for indirect vdevs:\n");
680 	corruption_found = B_TRUE;
681 
682 	zfs_btree_index_t *where = NULL;
683 	sublivelist_verify_block_t *svb;
684 	while ((svb = zfs_btree_destroy_nodes(&sv.sv_leftover, &where)) !=
685 	    NULL) {
686 		int vdev_id = DVA_GET_VDEV(&svb->svb_dva);
687 		ASSERT3U(vdev_id, <, rvd->vdev_children);
688 		vdev_t *vd = rvd->vdev_child[vdev_id];
689 		ASSERT(!vdev_is_concrete(vd));
690 		(void) printf("<%d:%llx:%llx> TXG %llx\n",
691 		    vdev_id, (u_longlong_t)DVA_GET_OFFSET(&svb->svb_dva),
692 		    (u_longlong_t)DVA_GET_ASIZE(&svb->svb_dva),
693 		    (u_longlong_t)svb->svb_allocated_txg);
694 	}
695 	(void) printf("\n");
696 	zfs_btree_destroy(&sv.sv_leftover);
697 }
698 
699 /*
700  * These libumem hooks provide a reasonable set of defaults for the allocator's
701  * debugging facilities.
702  */
703 const char *
704 _umem_debug_init(void)
705 {
706 	return ("default,verbose"); /* $UMEM_DEBUG setting */
707 }
708 
709 const char *
710 _umem_logging_init(void)
711 {
712 	return ("fail,contents"); /* $UMEM_LOGGING setting */
713 }
714 
715 static void
716 usage(void)
717 {
718 	(void) fprintf(stderr,
719 	    "Usage:\t%s [-AbcdDFGhikLMPsvXy] [-e [-V] [-p <path> ...]] "
720 	    "[-I <inflight I/Os>]\n"
721 	    "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
722 	    "\t\t[-K <key>]\n"
723 	    "\t\t[<poolname>[/<dataset | objset id>] [<object | range> ...]]\n"
724 	    "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>] [-K <key>]\n"
725 	    "\t\t[<poolname>[/<dataset | objset id>] [<object | range> ...]\n"
726 	    "\t%s -B [-e [-V] [-p <path> ...]] [-I <inflight I/Os>]\n"
727 	    "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
728 	    "\t\t[-K <key>] <poolname>/<objset id> [<backupflags>]\n"
729 	    "\t%s [-v] <bookmark>\n"
730 	    "\t%s -C [-A] [-U <cache>] [<poolname>]\n"
731 	    "\t%s -l [-Aqu] <device>\n"
732 	    "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] "
733 	    "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n"
734 	    "\t%s -O [-K <key>] <dataset> <path>\n"
735 	    "\t%s -r [-K <key>] <dataset> <path> <destination>\n"
736 	    "\t%s -r [-K <key>] -O <dataset> <object-id> <destination>\n"
737 	    "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
738 	    "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n"
739 	    "\t%s -f [-H] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
740 	    "\t\t[<poolname>[/<dataset | objset id>] [<object | range> ...]]\n"
741 	    "\t%s -E [-A] word0:word1:...:word15\n"
742 	    "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] "
743 	    "<poolname>\n\n",
744 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname,
745 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
746 
747 	(void) fprintf(stderr, "    Dataset name must include at least one "
748 	    "separator character '/' or '@'\n");
749 	(void) fprintf(stderr, "    If dataset name is specified, only that "
750 	    "dataset is dumped\n");
751 	(void) fprintf(stderr,  "    If object numbers or object number "
752 	    "ranges are specified, only those\n"
753 	    "    objects or ranges are dumped.\n\n");
754 	(void) fprintf(stderr,
755 	    "    Object ranges take the form <start>:<end>[:<flags>]\n"
756 	    "        start    Starting object number\n"
757 	    "        end      Ending object number, or -1 for no upper bound\n"
758 	    "        flags    Optional flags to select object types:\n"
759 	    "            A     All objects (this is the default)\n"
760 	    "            d     ZFS directories\n"
761 	    "            f     ZFS files \n"
762 	    "            m     SPA space maps\n"
763 	    "            v     ZVols\n"
764 	    "            z     ZAPs\n"
765 	    "            -     Negate effect of next flag\n\n");
766 	(void) fprintf(stderr, "    Options to control amount of output:\n");
767 	(void) fprintf(stderr, "        -b --block-stats             "
768 	    "block statistics\n");
769 	(void) fprintf(stderr, "           --bin=(lsize|psize|asize) "
770 	    "bin blocks based on this size in all three columns\n");
771 	(void) fprintf(stderr,
772 	    "           --class=(normal|special|dedup|other)[,...]\n"
773 	    "                                     only consider blocks from "
774 	    "these allocation classes\n");
775 	(void) fprintf(stderr, "        -B --backup                  "
776 	    "backup stream\n");
777 	(void) fprintf(stderr, "        -c --checksum                "
778 	    "checksum all metadata (twice for all data) blocks\n");
779 	(void) fprintf(stderr, "        -C --config                  "
780 	    "config (or cachefile if alone)\n");
781 	(void) fprintf(stderr, "        -d --datasets                "
782 	    "dataset(s)\n");
783 	(void) fprintf(stderr, "        -D --dedup-stats             "
784 	    "dedup statistics\n");
785 	(void) fprintf(stderr, "        -f --file-layout             "
786 	    "display a file's layout across raidz disks\n");
787 	(void) fprintf(stderr, "        -E --embedded-block-pointer=INTEGER\n"
788 	    "                                     decode and display block "
789 	    "from an embedded block pointer\n");
790 	(void) fprintf(stderr, "        -h --history                 "
791 	    "pool history\n");
792 	(void) fprintf(stderr, "        -i --intent-logs             "
793 	    "intent logs\n");
794 	(void) fprintf(stderr, "        -l --label                   "
795 	    "read label contents\n");
796 	(void) fprintf(stderr, "        -k --checkpointed-state      "
797 	    "examine the checkpointed state of the pool\n");
798 	(void) fprintf(stderr, "        -L --disable-leak-tracking   "
799 	    "disable leak tracking (do not load spacemaps)\n");
800 	(void) fprintf(stderr, "        -m --metaslabs               "
801 	    "metaslabs\n");
802 	(void) fprintf(stderr, "        -M --metaslab-groups         "
803 	    "metaslab groups\n");
804 	(void) fprintf(stderr, "        -O --object-lookups          "
805 	    "perform object lookups by path\n");
806 	(void) fprintf(stderr, "        -r --copy-object             "
807 	    "copy an object by path to file\n");
808 	(void) fprintf(stderr, "        -R --read-block              "
809 	    "read and display block from a device\n");
810 	(void) fprintf(stderr, "        -s --io-stats                "
811 	    "report stats on zdb's I/O\n");
812 	(void) fprintf(stderr, "        -S --simulate-dedup          "
813 	    "simulate dedup to measure effect\n");
814 	(void) fprintf(stderr, "        -v --verbose                 "
815 	    "verbose (applies to all others)\n");
816 	(void) fprintf(stderr, "        -y --livelist                "
817 	    "perform livelist and metaslab validation on any livelists being "
818 	    "deleted\n\n");
819 
820 	(void) fprintf(stderr, "    Below options are intended for use "
821 	    "with other options:\n");
822 	(void) fprintf(stderr, "        -A --ignore-assertions       "
823 	    "ignore assertions (-A), enable panic recovery (-AA) or both "
824 	    "(-AAA)\n");
825 	(void) fprintf(stderr, "        -e --exported                "
826 	    "pool is exported/destroyed/has altroot/not in a cachefile\n");
827 	(void) fprintf(stderr, "        -F --automatic-rewind        "
828 	    "attempt automatic rewind within safe range of transaction "
829 	    "groups\n");
830 	(void) fprintf(stderr, "        -G --dump-debug-msg          "
831 	    "dump zfs_dbgmsg buffer before exiting\n");
832 	(void) fprintf(stderr, "        -H --scripting-mode          "
833 	    "dump output in a format that is scripting friendly\n");
834 	(void) fprintf(stderr, "        -I --inflight=INTEGER        "
835 	    "specify the maximum number of checksumming I/Os "
836 	    "[default is 200]\n");
837 	(void) fprintf(stderr, "        -K --key=KEY                 "
838 	    "decryption key for encrypted dataset\n");
839 	(void) fprintf(stderr, "        -o --option=\"NAME=VALUE\" "
840 	    "set the named tunable to the given value\n");
841 	(void) fprintf(stderr, "        -p --path==PATH              "
842 	    "use one or more with -e to specify path to vdev dir\n");
843 	(void) fprintf(stderr, "        -P --parseable               "
844 	    "print numbers in parseable form\n");
845 	(void) fprintf(stderr, "        -q --skip-label              "
846 	    "don't print label contents\n");
847 	(void) fprintf(stderr, "        -t --txg=INTEGER             "
848 	    "highest txg to use when searching for uberblocks\n");
849 	(void) fprintf(stderr, "        -T --brt-stats               "
850 	    "BRT statistics\n");
851 	(void) fprintf(stderr, "        -u --uberblock               "
852 	    "uberblock\n");
853 	(void) fprintf(stderr, "        -U --cachefile=PATH          "
854 	    "use alternate cachefile\n");
855 	(void) fprintf(stderr, "        -V --verbatim                "
856 	    "do verbatim import\n");
857 	(void) fprintf(stderr, "        -x --dump-blocks=PATH        "
858 	    "dump all read blocks into specified directory\n");
859 	(void) fprintf(stderr, "        -X --extreme-rewind          "
860 	    "attempt extreme rewind (does not work with dataset)\n");
861 	(void) fprintf(stderr, "        -Y --all-reconstruction      "
862 	    "attempt all reconstruction combinations for split blocks\n");
863 	(void) fprintf(stderr, "        -Z --zstd-headers            "
864 	    "show ZSTD headers \n");
865 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
866 	    "to make only that option verbose\n");
867 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
868 	zdb_exit(2);
869 }
870 
871 static void
872 dump_debug_buffer(void)
873 {
874 	ssize_t ret __attribute__((unused));
875 
876 	if (!dump_opt['G'])
877 		return;
878 	/*
879 	 * We use write() instead of printf() so that this function
880 	 * is safe to call from a signal handler.
881 	 */
882 	ret = write(STDERR_FILENO, "\n", 1);
883 	zfs_dbgmsg_print(STDERR_FILENO, "zdb");
884 }
885 
886 static void sig_handler(int signo)
887 {
888 	struct sigaction action;
889 
890 	libspl_backtrace(STDERR_FILENO);
891 	dump_debug_buffer();
892 
893 	/*
894 	 * Restore default action and re-raise signal so SIGSEGV and
895 	 * SIGABRT can trigger a core dump.
896 	 */
897 	action.sa_handler = SIG_DFL;
898 	sigemptyset(&action.sa_mask);
899 	action.sa_flags = 0;
900 	(void) sigaction(signo, &action, NULL);
901 	raise(signo);
902 }
903 
904 /*
905  * Called for usage errors that are discovered after a call to spa_open(),
906  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
907  */
908 
909 static void
910 fatal(const char *fmt, ...)
911 {
912 	va_list ap;
913 
914 	va_start(ap, fmt);
915 	(void) fprintf(stderr, "%s: ", cmdname);
916 	(void) vfprintf(stderr, fmt, ap);
917 	va_end(ap);
918 	(void) fprintf(stderr, "\n");
919 
920 	dump_debug_buffer();
921 
922 	zdb_exit(1);
923 }
924 
925 static void
926 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
927 {
928 	(void) size;
929 	nvlist_t *nv;
930 	size_t nvsize = *(uint64_t *)data;
931 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
932 	int err;
933 
934 	err = dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH);
935 	if (err != 0) {
936 		(void) printf("got error %u from dmu_read\n", err);
937 		umem_free(packed, nvsize);
938 		return;
939 	}
940 
941 	err = nvlist_unpack(packed, nvsize, &nv, 0);
942 
943 	umem_free(packed, nvsize);
944 
945 	if (err != 0) {
946 		(void) printf("got error %u from nvlist_unpack\n", err);
947 		return;
948 	}
949 
950 	dump_nvlist(nv, 8);
951 
952 	nvlist_free(nv);
953 }
954 
955 static void
956 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
957 {
958 	(void) os, (void) object, (void) size;
959 	spa_history_phys_t *shp = data;
960 
961 	if (shp == NULL)
962 		return;
963 
964 	(void) printf("\t\tpool_create_len = %llu\n",
965 	    (u_longlong_t)shp->sh_pool_create_len);
966 	(void) printf("\t\tphys_max_off = %llu\n",
967 	    (u_longlong_t)shp->sh_phys_max_off);
968 	(void) printf("\t\tbof = %llu\n",
969 	    (u_longlong_t)shp->sh_bof);
970 	(void) printf("\t\teof = %llu\n",
971 	    (u_longlong_t)shp->sh_eof);
972 	(void) printf("\t\trecords_lost = %llu\n",
973 	    (u_longlong_t)shp->sh_records_lost);
974 }
975 
976 static void
977 zdb_nicenum(uint64_t num, char *buf, size_t buflen)
978 {
979 	if (dump_opt['P'])
980 		(void) snprintf(buf, buflen, "%llu", (longlong_t)num);
981 	else
982 		nicenum(num, buf, buflen);
983 }
984 
985 static void
986 zdb_nicebytes(uint64_t bytes, char *buf, size_t buflen)
987 {
988 	if (dump_opt['P'])
989 		(void) snprintf(buf, buflen, "%llu", (longlong_t)bytes);
990 	else
991 		zfs_nicebytes(bytes, buf, buflen);
992 }
993 
994 static const char histo_stars[] = "****************************************";
995 static const uint64_t histo_width = sizeof (histo_stars) - 1;
996 
997 static void
998 dump_histogram(const uint64_t *histo, int size, int offset)
999 {
1000 	int i;
1001 	int minidx = size - 1;
1002 	int maxidx = 0;
1003 	uint64_t max = 0;
1004 
1005 	for (i = 0; i < size; i++) {
1006 		if (histo[i] == 0)
1007 			continue;
1008 		if (histo[i] > max)
1009 			max = histo[i];
1010 		if (i > maxidx)
1011 			maxidx = i;
1012 		if (i < minidx)
1013 			minidx = i;
1014 	}
1015 
1016 	if (max < histo_width)
1017 		max = histo_width;
1018 
1019 	for (i = minidx; i <= maxidx; i++) {
1020 		(void) printf("\t\t\t%3u: %6llu %s\n",
1021 		    i + offset, (u_longlong_t)histo[i],
1022 		    &histo_stars[(max - histo[i]) * histo_width / max]);
1023 	}
1024 }
1025 
1026 static void
1027 dump_zap_stats(objset_t *os, uint64_t object)
1028 {
1029 	int error;
1030 	zap_stats_t zs;
1031 
1032 	error = zap_get_stats(os, object, &zs);
1033 	if (error)
1034 		return;
1035 
1036 	if (zs.zs_ptrtbl_len == 0) {
1037 		ASSERT(zs.zs_num_blocks == 1);
1038 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
1039 		    (u_longlong_t)zs.zs_blocksize,
1040 		    (u_longlong_t)zs.zs_num_entries);
1041 		return;
1042 	}
1043 
1044 	(void) printf("\tFat ZAP stats:\n");
1045 
1046 	(void) printf("\t\tPointer table:\n");
1047 	(void) printf("\t\t\t%llu elements\n",
1048 	    (u_longlong_t)zs.zs_ptrtbl_len);
1049 	(void) printf("\t\t\tzt_blk: %llu\n",
1050 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
1051 	(void) printf("\t\t\tzt_numblks: %llu\n",
1052 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
1053 	(void) printf("\t\t\tzt_shift: %llu\n",
1054 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
1055 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
1056 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
1057 	(void) printf("\t\t\tzt_nextblk: %llu\n",
1058 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
1059 
1060 	(void) printf("\t\tZAP entries: %llu\n",
1061 	    (u_longlong_t)zs.zs_num_entries);
1062 	(void) printf("\t\tLeaf blocks: %llu\n",
1063 	    (u_longlong_t)zs.zs_num_leafs);
1064 	(void) printf("\t\tTotal blocks: %llu\n",
1065 	    (u_longlong_t)zs.zs_num_blocks);
1066 	(void) printf("\t\tzap_block_type: 0x%llx\n",
1067 	    (u_longlong_t)zs.zs_block_type);
1068 	(void) printf("\t\tzap_magic: 0x%llx\n",
1069 	    (u_longlong_t)zs.zs_magic);
1070 	(void) printf("\t\tzap_salt: 0x%llx\n",
1071 	    (u_longlong_t)zs.zs_salt);
1072 
1073 	(void) printf("\t\tLeafs with 2^n pointers:\n");
1074 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
1075 
1076 	(void) printf("\t\tBlocks with n*5 entries:\n");
1077 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
1078 
1079 	(void) printf("\t\tBlocks n/10 full:\n");
1080 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
1081 
1082 	(void) printf("\t\tEntries with n chunks:\n");
1083 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
1084 
1085 	(void) printf("\t\tBuckets with n entries:\n");
1086 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
1087 }
1088 
1089 static void
1090 dump_none(objset_t *os, uint64_t object, void *data, size_t size)
1091 {
1092 	(void) os, (void) object, (void) data, (void) size;
1093 }
1094 
1095 static void
1096 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
1097 {
1098 	(void) os, (void) object, (void) data, (void) size;
1099 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
1100 }
1101 
1102 static void
1103 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
1104 {
1105 	(void) os, (void) object, (void) data, (void) size;
1106 }
1107 
1108 static void
1109 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
1110 {
1111 	uint64_t *arr;
1112 	uint64_t oursize;
1113 	if (dump_opt['d'] < 6)
1114 		return;
1115 
1116 	if (data == NULL) {
1117 		dmu_object_info_t doi;
1118 
1119 		VERIFY0(dmu_object_info(os, object, &doi));
1120 		size = doi.doi_max_offset;
1121 		/*
1122 		 * We cap the size at 1 mebibyte here to prevent
1123 		 * allocation failures and nigh-infinite printing if the
1124 		 * object is extremely large.
1125 		 */
1126 		oursize = MIN(size, 1 << 20);
1127 		arr = kmem_alloc(oursize, KM_SLEEP);
1128 
1129 		int err = dmu_read(os, object, 0, oursize, arr, 0);
1130 		if (err != 0) {
1131 			(void) printf("got error %u from dmu_read\n", err);
1132 			kmem_free(arr, oursize);
1133 			return;
1134 		}
1135 	} else {
1136 		/*
1137 		 * Even though the allocation is already done in this code path,
1138 		 * we still cap the size to prevent excessive printing.
1139 		 */
1140 		oursize = MIN(size, 1 << 20);
1141 		arr = data;
1142 	}
1143 
1144 	if (size == 0) {
1145 		if (data == NULL)
1146 			kmem_free(arr, oursize);
1147 		(void) printf("\t\t[]\n");
1148 		return;
1149 	}
1150 
1151 	(void) printf("\t\t[%0llx", (u_longlong_t)arr[0]);
1152 	for (size_t i = 1; i * sizeof (uint64_t) < oursize; i++) {
1153 		if (i % 4 != 0)
1154 			(void) printf(", %0llx", (u_longlong_t)arr[i]);
1155 		else
1156 			(void) printf(",\n\t\t%0llx", (u_longlong_t)arr[i]);
1157 	}
1158 	if (oursize != size)
1159 		(void) printf(", ... ");
1160 	(void) printf("]\n");
1161 
1162 	if (data == NULL)
1163 		kmem_free(arr, oursize);
1164 }
1165 
1166 static void
1167 dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
1168 {
1169 	(void) data, (void) size;
1170 	zap_cursor_t zc;
1171 	zap_attribute_t *attrp = zap_attribute_long_alloc();
1172 	void *prop;
1173 	unsigned i;
1174 
1175 	dump_zap_stats(os, object);
1176 	(void) printf("\n");
1177 
1178 	for (zap_cursor_init(&zc, os, object);
1179 	    zap_cursor_retrieve(&zc, attrp) == 0;
1180 	    zap_cursor_advance(&zc)) {
1181 		boolean_t key64 =
1182 		    !!(zap_getflags(zc.zc_zap) & ZAP_FLAG_UINT64_KEY);
1183 
1184 		if (key64)
1185 			(void) printf("\t\t0x%010" PRIu64 "x = ",
1186 			    *(uint64_t *)attrp->za_name);
1187 		else
1188 			(void) printf("\t\t%s = ", attrp->za_name);
1189 
1190 		if (attrp->za_num_integers == 0) {
1191 			(void) printf("\n");
1192 			continue;
1193 		}
1194 		prop = umem_zalloc(attrp->za_num_integers *
1195 		    attrp->za_integer_length, UMEM_NOFAIL);
1196 
1197 		if (key64)
1198 			(void) zap_lookup_uint64(os, object,
1199 			    (const uint64_t *)attrp->za_name, 1,
1200 			    attrp->za_integer_length, attrp->za_num_integers,
1201 			    prop);
1202 		else
1203 			(void) zap_lookup(os, object, attrp->za_name,
1204 			    attrp->za_integer_length, attrp->za_num_integers,
1205 			    prop);
1206 
1207 		if (attrp->za_integer_length == 1 && !key64) {
1208 			if (strcmp(attrp->za_name,
1209 			    DSL_CRYPTO_KEY_MASTER_KEY) == 0 ||
1210 			    strcmp(attrp->za_name,
1211 			    DSL_CRYPTO_KEY_HMAC_KEY) == 0 ||
1212 			    strcmp(attrp->za_name, DSL_CRYPTO_KEY_IV) == 0 ||
1213 			    strcmp(attrp->za_name, DSL_CRYPTO_KEY_MAC) == 0 ||
1214 			    strcmp(attrp->za_name,
1215 			    DMU_POOL_CHECKSUM_SALT) == 0) {
1216 				uint8_t *u8 = prop;
1217 
1218 				for (i = 0; i < attrp->za_num_integers; i++) {
1219 					(void) printf("%02x", u8[i]);
1220 				}
1221 			} else {
1222 				(void) printf("%s", (char *)prop);
1223 			}
1224 		} else {
1225 			for (i = 0; i < attrp->za_num_integers; i++) {
1226 				switch (attrp->za_integer_length) {
1227 				case 1:
1228 					(void) printf("%u ",
1229 					    ((uint8_t *)prop)[i]);
1230 					break;
1231 				case 2:
1232 					(void) printf("%u ",
1233 					    ((uint16_t *)prop)[i]);
1234 					break;
1235 				case 4:
1236 					(void) printf("%u ",
1237 					    ((uint32_t *)prop)[i]);
1238 					break;
1239 				case 8:
1240 					(void) printf("%lld ",
1241 					    (u_longlong_t)((int64_t *)prop)[i]);
1242 					break;
1243 				}
1244 			}
1245 		}
1246 		(void) printf("\n");
1247 		umem_free(prop,
1248 		    attrp->za_num_integers * attrp->za_integer_length);
1249 	}
1250 	zap_cursor_fini(&zc);
1251 	zap_attribute_free(attrp);
1252 }
1253 
1254 static void
1255 dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
1256 {
1257 	bpobj_phys_t *bpop = data;
1258 	uint64_t i;
1259 	char bytes[32], comp[32], uncomp[32];
1260 
1261 	/* make sure the output won't get truncated */
1262 	_Static_assert(sizeof (bytes) >= NN_NUMBUF_SZ, "bytes truncated");
1263 	_Static_assert(sizeof (comp) >= NN_NUMBUF_SZ, "comp truncated");
1264 	_Static_assert(sizeof (uncomp) >= NN_NUMBUF_SZ, "uncomp truncated");
1265 
1266 	if (bpop == NULL)
1267 		return;
1268 
1269 	zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
1270 	zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
1271 	zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
1272 
1273 	(void) printf("\t\tnum_blkptrs = %llu\n",
1274 	    (u_longlong_t)bpop->bpo_num_blkptrs);
1275 	(void) printf("\t\tbytes = %s\n", bytes);
1276 	if (size >= BPOBJ_SIZE_V1) {
1277 		(void) printf("\t\tcomp = %s\n", comp);
1278 		(void) printf("\t\tuncomp = %s\n", uncomp);
1279 	}
1280 	if (size >= BPOBJ_SIZE_V2) {
1281 		(void) printf("\t\tsubobjs = %llu\n",
1282 		    (u_longlong_t)bpop->bpo_subobjs);
1283 		(void) printf("\t\tnum_subobjs = %llu\n",
1284 		    (u_longlong_t)bpop->bpo_num_subobjs);
1285 	}
1286 	if (size >= sizeof (*bpop)) {
1287 		(void) printf("\t\tnum_freed = %llu\n",
1288 		    (u_longlong_t)bpop->bpo_num_freed);
1289 	}
1290 
1291 	if (dump_opt['d'] < 5)
1292 		return;
1293 
1294 	for (i = 0; i < bpop->bpo_num_blkptrs; i++) {
1295 		char blkbuf[BP_SPRINTF_LEN];
1296 		blkptr_t bp;
1297 
1298 		int err = dmu_read(os, object,
1299 		    i * sizeof (bp), sizeof (bp), &bp, 0);
1300 		if (err != 0) {
1301 			(void) printf("got error %u from dmu_read\n", err);
1302 			break;
1303 		}
1304 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp,
1305 		    BP_GET_FREE(&bp));
1306 		(void) printf("\t%s\n", blkbuf);
1307 	}
1308 }
1309 
1310 static void
1311 dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
1312 {
1313 	(void) data, (void) size;
1314 	dmu_object_info_t doi;
1315 	int64_t i;
1316 
1317 	VERIFY0(dmu_object_info(os, object, &doi));
1318 	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
1319 
1320 	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
1321 	if (err != 0) {
1322 		(void) printf("got error %u from dmu_read\n", err);
1323 		kmem_free(subobjs, doi.doi_max_offset);
1324 		return;
1325 	}
1326 
1327 	int64_t last_nonzero = -1;
1328 	for (i = 0; i < doi.doi_max_offset / 8; i++) {
1329 		if (subobjs[i] != 0)
1330 			last_nonzero = i;
1331 	}
1332 
1333 	for (i = 0; i <= last_nonzero; i++) {
1334 		(void) printf("\t%llu\n", (u_longlong_t)subobjs[i]);
1335 	}
1336 	kmem_free(subobjs, doi.doi_max_offset);
1337 }
1338 
1339 static void
1340 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
1341 {
1342 	(void) data, (void) size;
1343 	dump_zap_stats(os, object);
1344 	/* contents are printed elsewhere, properly decoded */
1345 }
1346 
1347 static void
1348 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
1349 {
1350 	(void) data, (void) size;
1351 	zap_cursor_t zc;
1352 	zap_attribute_t *attrp = zap_attribute_alloc();
1353 
1354 	dump_zap_stats(os, object);
1355 	(void) printf("\n");
1356 
1357 	for (zap_cursor_init(&zc, os, object);
1358 	    zap_cursor_retrieve(&zc, attrp) == 0;
1359 	    zap_cursor_advance(&zc)) {
1360 		(void) printf("\t\t%s = ", attrp->za_name);
1361 		if (attrp->za_num_integers == 0) {
1362 			(void) printf("\n");
1363 			continue;
1364 		}
1365 		(void) printf(" %llx : [%d:%d:%d]\n",
1366 		    (u_longlong_t)attrp->za_first_integer,
1367 		    (int)ATTR_LENGTH(attrp->za_first_integer),
1368 		    (int)ATTR_BSWAP(attrp->za_first_integer),
1369 		    (int)ATTR_NUM(attrp->za_first_integer));
1370 	}
1371 	zap_cursor_fini(&zc);
1372 	zap_attribute_free(attrp);
1373 }
1374 
1375 static void
1376 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
1377 {
1378 	(void) data, (void) size;
1379 	zap_cursor_t zc;
1380 	zap_attribute_t *attrp = zap_attribute_alloc();
1381 	uint16_t *layout_attrs;
1382 	unsigned i;
1383 
1384 	dump_zap_stats(os, object);
1385 	(void) printf("\n");
1386 
1387 	for (zap_cursor_init(&zc, os, object);
1388 	    zap_cursor_retrieve(&zc, attrp) == 0;
1389 	    zap_cursor_advance(&zc)) {
1390 		(void) printf("\t\t%s = [", attrp->za_name);
1391 		if (attrp->za_num_integers == 0) {
1392 			(void) printf("\n");
1393 			continue;
1394 		}
1395 
1396 		VERIFY(attrp->za_integer_length == 2);
1397 		layout_attrs = umem_zalloc(attrp->za_num_integers *
1398 		    attrp->za_integer_length, UMEM_NOFAIL);
1399 
1400 		VERIFY(zap_lookup(os, object, attrp->za_name,
1401 		    attrp->za_integer_length,
1402 		    attrp->za_num_integers, layout_attrs) == 0);
1403 
1404 		for (i = 0; i != attrp->za_num_integers; i++)
1405 			(void) printf(" %d ", (int)layout_attrs[i]);
1406 		(void) printf("]\n");
1407 		umem_free(layout_attrs,
1408 		    attrp->za_num_integers * attrp->za_integer_length);
1409 	}
1410 	zap_cursor_fini(&zc);
1411 	zap_attribute_free(attrp);
1412 }
1413 
1414 static void
1415 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
1416 {
1417 	(void) data, (void) size;
1418 	zap_cursor_t zc;
1419 	zap_attribute_t *attrp = zap_attribute_long_alloc();
1420 	const char *typenames[] = {
1421 		/* 0 */ "not specified",
1422 		/* 1 */ "FIFO",
1423 		/* 2 */ "Character Device",
1424 		/* 3 */ "3 (invalid)",
1425 		/* 4 */ "Directory",
1426 		/* 5 */ "5 (invalid)",
1427 		/* 6 */ "Block Device",
1428 		/* 7 */ "7 (invalid)",
1429 		/* 8 */ "Regular File",
1430 		/* 9 */ "9 (invalid)",
1431 		/* 10 */ "Symbolic Link",
1432 		/* 11 */ "11 (invalid)",
1433 		/* 12 */ "Socket",
1434 		/* 13 */ "Door",
1435 		/* 14 */ "Event Port",
1436 		/* 15 */ "15 (invalid)",
1437 	};
1438 
1439 	dump_zap_stats(os, object);
1440 	(void) printf("\n");
1441 
1442 	for (zap_cursor_init(&zc, os, object);
1443 	    zap_cursor_retrieve(&zc, attrp) == 0;
1444 	    zap_cursor_advance(&zc)) {
1445 		(void) printf("\t\t%s = %lld (type: %s)\n",
1446 		    attrp->za_name, ZFS_DIRENT_OBJ(attrp->za_first_integer),
1447 		    typenames[ZFS_DIRENT_TYPE(attrp->za_first_integer)]);
1448 	}
1449 	zap_cursor_fini(&zc);
1450 	zap_attribute_free(attrp);
1451 }
1452 
1453 static uint64_t
1454 get_dtl_refcount(vdev_t *vd, zfs_range_tree_t *spacemap_objs)
1455 {
1456 	uint64_t refcount = 0;
1457 
1458 	if (vd->vdev_ops->vdev_op_leaf) {
1459 		space_map_t *sm = vd->vdev_dtl_sm;
1460 
1461 		if (sm != NULL &&
1462 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t)) {
1463 			if (spacemap_objs != NULL &&
1464 			    !zfs_range_tree_contains(spacemap_objs,
1465 			    space_map_object(sm), 1)) {
1466 				zfs_range_tree_add(spacemap_objs,
1467 				    space_map_object(sm), 1);
1468 			}
1469 			return (1);
1470 		}
1471 		return (0);
1472 	}
1473 
1474 	for (unsigned c = 0; c < vd->vdev_children; c++)
1475 		refcount += get_dtl_refcount(vd->vdev_child[c], spacemap_objs);
1476 	return (refcount);
1477 }
1478 
1479 static uint64_t
1480 get_metaslab_refcount(vdev_t *vd, zfs_range_tree_t *spacemap_objs)
1481 {
1482 	uint64_t refcount = 0;
1483 
1484 	if (vd->vdev_top == vd) {
1485 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
1486 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
1487 
1488 			if (sm != NULL &&
1489 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t)) {
1490 				if (spacemap_objs != NULL &&
1491 				    !zfs_range_tree_contains(spacemap_objs,
1492 				    space_map_object(sm), 1)) {
1493 					zfs_range_tree_add(spacemap_objs,
1494 					    space_map_object(sm), 1);
1495 				}
1496 				refcount++;
1497 			}
1498 		}
1499 	}
1500 	for (unsigned c = 0; c < vd->vdev_children; c++)
1501 		refcount += get_metaslab_refcount(vd->vdev_child[c],
1502 		    spacemap_objs);
1503 
1504 	return (refcount);
1505 }
1506 
1507 static uint64_t
1508 get_obsolete_refcount(vdev_t *vd, zfs_range_tree_t *spacemap_objs)
1509 {
1510 	uint64_t obsolete_sm_object;
1511 	uint64_t refcount = 0;
1512 
1513 	VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object));
1514 	if (vd->vdev_top == vd && obsolete_sm_object != 0) {
1515 		dmu_object_info_t doi;
1516 		VERIFY0(dmu_object_info(vd->vdev_spa->spa_meta_objset,
1517 		    obsolete_sm_object, &doi));
1518 		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
1519 			if (spacemap_objs != NULL &&
1520 			    !zfs_range_tree_contains(spacemap_objs,
1521 			    obsolete_sm_object, 1)) {
1522 				zfs_range_tree_add(spacemap_objs,
1523 				    obsolete_sm_object, 1);
1524 			}
1525 			refcount++;
1526 		}
1527 	} else {
1528 		ASSERT0P(vd->vdev_obsolete_sm);
1529 		ASSERT0(obsolete_sm_object);
1530 	}
1531 	for (unsigned c = 0; c < vd->vdev_children; c++) {
1532 		refcount += get_obsolete_refcount(vd->vdev_child[c],
1533 		    spacemap_objs);
1534 	}
1535 
1536 	return (refcount);
1537 }
1538 
1539 static uint64_t
1540 get_prev_obsolete_spacemap_refcount(spa_t *spa,
1541     zfs_range_tree_t *spacemap_objs)
1542 {
1543 	uint64_t prev_obj =
1544 	    spa->spa_condensing_indirect_phys.scip_prev_obsolete_sm_object;
1545 	if (prev_obj != 0) {
1546 		dmu_object_info_t doi;
1547 		VERIFY0(dmu_object_info(spa->spa_meta_objset, prev_obj, &doi));
1548 		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
1549 			if (spacemap_objs != NULL &&
1550 			    !zfs_range_tree_contains(spacemap_objs,
1551 			    prev_obj, 1)) {
1552 				zfs_range_tree_add(spacemap_objs,
1553 				    prev_obj, 1);
1554 			}
1555 			return (1);
1556 		}
1557 	}
1558 	return (0);
1559 }
1560 
1561 static uint64_t
1562 get_checkpoint_refcount(vdev_t *vd, zfs_range_tree_t *spacemap_objs)
1563 {
1564 	uint64_t refcount = 0;
1565 
1566 	if (vd->vdev_top == vd && vd->vdev_top_zap != 0) {
1567 		uint64_t checkpoint_sm_obj;
1568 		int error = zap_lookup(spa_meta_objset(vd->vdev_spa),
1569 		    vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
1570 		    sizeof (checkpoint_sm_obj), 1, &checkpoint_sm_obj);
1571 		if (error == 0) {
1572 			if (spacemap_objs != NULL &&
1573 			    !zfs_range_tree_contains(spacemap_objs,
1574 			    checkpoint_sm_obj, 1)) {
1575 				zfs_range_tree_add(spacemap_objs,
1576 				    checkpoint_sm_obj, 1);
1577 			}
1578 			refcount++;
1579 		}
1580 	}
1581 
1582 	for (uint64_t c = 0; c < vd->vdev_children; c++)
1583 		refcount += get_checkpoint_refcount(vd->vdev_child[c],
1584 		    spacemap_objs);
1585 
1586 	return (refcount);
1587 }
1588 
1589 static uint64_t
1590 get_log_spacemap_refcount(spa_t *spa, zfs_range_tree_t *spacemap_objs)
1591 {
1592 	uint64_t refcount = 0;
1593 
1594 	for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg);
1595 	    sls != NULL;
1596 	    sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls)) {
1597 		if (spacemap_objs != NULL &&
1598 		    !zfs_range_tree_contains(spacemap_objs, sls->sls_sm_obj,
1599 		    1)) {
1600 			zfs_range_tree_add(spacemap_objs, sls->sls_sm_obj, 1);
1601 		}
1602 		refcount++;
1603 	}
1604 
1605 	return (refcount);
1606 }
1607 
1608 static void
1609 dump_spacemap_refcount_mismatch_details(spa_t *spa,
1610     uint64_t expected_refcount, uint64_t actual_refcount,
1611     zfs_range_tree_t *spacemap_objs)
1612 {
1613 	objset_t *mos = spa->spa_meta_objset;
1614 	uint64_t total_histogram_sm = 0;
1615 	uint64_t unreferenced_histogram_sm = 0;
1616 	uint64_t object = 0;
1617 	boolean_t printed_unreferenced_header = B_FALSE;
1618 
1619 	(void) printf("\tdelta(expected-actual)=%lld\n",
1620 	    (longlong_t)expected_refcount - (longlong_t)actual_refcount);
1621 
1622 	while (dmu_object_next(mos, &object, B_FALSE, 0) == 0) {
1623 		dmu_object_info_t doi;
1624 		VERIFY0(dmu_object_info(mos, object, &doi));
1625 		if (doi.doi_type != DMU_OT_SPACE_MAP ||
1626 		    doi.doi_bonus_size != sizeof (space_map_phys_t))
1627 			continue;
1628 
1629 		total_histogram_sm++;
1630 		if (zfs_range_tree_contains(spacemap_objs, object, 1))
1631 			continue;
1632 
1633 		unreferenced_histogram_sm++;
1634 		if (!printed_unreferenced_header) {
1635 			(void) printf(
1636 			    "\t  unreferenced histogram space maps:\n");
1637 			printed_unreferenced_header = B_TRUE;
1638 		}
1639 
1640 		dmu_buf_t *db = NULL;
1641 		int error = dmu_bonus_hold(mos, object, FTAG, &db);
1642 		if (error != 0) {
1643 			(void) printf("\t    object %llu "
1644 			    "(bonus hold error: %s)\n",
1645 			    (u_longlong_t)object, strerror(error));
1646 			continue;
1647 		}
1648 
1649 		space_map_phys_t *smp = db->db_data;
1650 		(void) printf("\t    object %llu smp_alloc=0x%llx "
1651 		    "smp_length=0x%llx\n", (u_longlong_t)object,
1652 		    (u_longlong_t)smp->smp_alloc,
1653 		    (u_longlong_t)smp->smp_length);
1654 		dmu_buf_rele(db, FTAG);
1655 	}
1656 
1657 	(void) printf("\t  allocated histogram space maps in MOS=%llu\n",
1658 	    (u_longlong_t)total_histogram_sm);
1659 	if (expected_refcount != total_histogram_sm) {
1660 		(void) printf("\t  WARNING: feature refcount disagrees "
1661 		    "with MOS scan by %lld\n",
1662 		    (longlong_t)expected_refcount -
1663 		    (longlong_t)total_histogram_sm);
1664 	}
1665 	if (unreferenced_histogram_sm == 0) {
1666 		(void) printf(
1667 		    "\t  no unreferenced histogram space maps found\n");
1668 	} else {
1669 		(void) printf("\t  total unreferenced histogram space "
1670 		    "maps=%llu\n",
1671 		    (u_longlong_t)unreferenced_histogram_sm);
1672 	}
1673 }
1674 
1675 static int
1676 verify_spacemap_refcounts(spa_t *spa)
1677 {
1678 	uint64_t expected_refcount = 0;
1679 	uint64_t actual_refcount = 0;
1680 	uint64_t dtl_refcount, metaslab_refcount, obsolete_refcount,
1681 	    prev_obsolete_refcount, checkpoint_refcount, log_spacemap_refcount;
1682 	zfs_range_tree_t *spacemap_objs = zfs_range_tree_create_flags(
1683 	    NULL, ZFS_RANGE_SEG64, NULL, 0, 0, 0,
1684 	    "verify_spacemap_refcounts:spacemap_objs");
1685 
1686 	(void) feature_get_refcount(spa,
1687 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
1688 	    &expected_refcount);
1689 	dtl_refcount = get_dtl_refcount(spa->spa_root_vdev, spacemap_objs);
1690 	metaslab_refcount = get_metaslab_refcount(spa->spa_root_vdev,
1691 	    spacemap_objs);
1692 	obsolete_refcount = get_obsolete_refcount(spa->spa_root_vdev,
1693 	    spacemap_objs);
1694 	prev_obsolete_refcount = get_prev_obsolete_spacemap_refcount(spa,
1695 	    spacemap_objs);
1696 	checkpoint_refcount = get_checkpoint_refcount(spa->spa_root_vdev,
1697 	    spacemap_objs);
1698 	log_spacemap_refcount = get_log_spacemap_refcount(spa, spacemap_objs);
1699 	actual_refcount = dtl_refcount + metaslab_refcount +
1700 	    obsolete_refcount + prev_obsolete_refcount +
1701 	    checkpoint_refcount + log_spacemap_refcount;
1702 
1703 	if (expected_refcount != actual_refcount) {
1704 		(void) printf("space map refcount mismatch: expected %llu != "
1705 		    "actual %llu\n",
1706 		    (u_longlong_t)expected_refcount,
1707 		    (u_longlong_t)actual_refcount);
1708 		(void) printf("\tDTL: %llu, metaslab: %llu, obsolete: %llu, "
1709 		    "prev obsolete: %llu, checkpoint: %llu, "
1710 		    "log spacemap: %llu\n",
1711 		    (u_longlong_t)dtl_refcount,
1712 		    (u_longlong_t)metaslab_refcount,
1713 		    (u_longlong_t)obsolete_refcount,
1714 		    (u_longlong_t)prev_obsolete_refcount,
1715 		    (u_longlong_t)checkpoint_refcount,
1716 		    (u_longlong_t)log_spacemap_refcount);
1717 		dump_spacemap_refcount_mismatch_details(spa, expected_refcount,
1718 		    actual_refcount, spacemap_objs);
1719 		zfs_range_tree_vacate(spacemap_objs, NULL, NULL);
1720 		zfs_range_tree_destroy(spacemap_objs);
1721 		return (2);
1722 	}
1723 
1724 	zfs_range_tree_vacate(spacemap_objs, NULL, NULL);
1725 	zfs_range_tree_destroy(spacemap_objs);
1726 	return (0);
1727 }
1728 
1729 static void
1730 dump_spacemap(objset_t *os, space_map_t *sm)
1731 {
1732 	const char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
1733 	    "INVALID", "INVALID", "INVALID", "INVALID" };
1734 
1735 	if (sm == NULL)
1736 		return;
1737 
1738 	(void) printf("space map object %llu:\n",
1739 	    (longlong_t)sm->sm_object);
1740 	(void) printf("  smp_length = 0x%llx\n",
1741 	    (longlong_t)sm->sm_phys->smp_length);
1742 	(void) printf("  smp_alloc = 0x%llx\n",
1743 	    (longlong_t)sm->sm_phys->smp_alloc);
1744 
1745 	if (dump_opt['d'] < 6 && dump_opt['m'] < 4)
1746 		return;
1747 
1748 	/*
1749 	 * Print out the freelist entries in both encoded and decoded form.
1750 	 */
1751 	uint8_t mapshift = sm->sm_shift;
1752 	int64_t alloc = 0;
1753 	uint64_t word, entry_id = 0;
1754 	for (uint64_t offset = 0; offset < space_map_length(sm);
1755 	    offset += sizeof (word)) {
1756 
1757 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
1758 		    sizeof (word), &word, DMU_READ_PREFETCH));
1759 
1760 		if (sm_entry_is_debug(word)) {
1761 			uint64_t de_txg = SM_DEBUG_TXG_DECODE(word);
1762 			uint64_t de_sync_pass = SM_DEBUG_SYNCPASS_DECODE(word);
1763 			if (de_txg == 0) {
1764 				(void) printf(
1765 				    "\t    [%6llu] PADDING\n",
1766 				    (u_longlong_t)entry_id);
1767 			} else {
1768 				(void) printf(
1769 				    "\t    [%6llu] %s: txg %llu pass %llu\n",
1770 				    (u_longlong_t)entry_id,
1771 				    ddata[SM_DEBUG_ACTION_DECODE(word)],
1772 				    (u_longlong_t)de_txg,
1773 				    (u_longlong_t)de_sync_pass);
1774 			}
1775 			entry_id++;
1776 			continue;
1777 		}
1778 
1779 		char entry_type;
1780 		uint64_t entry_off, entry_run, entry_vdev;
1781 
1782 		if (sm_entry_is_single_word(word)) {
1783 			entry_type = (SM_TYPE_DECODE(word) == SM_ALLOC) ?
1784 			    'A' : 'F';
1785 			entry_off = (SM_OFFSET_DECODE(word) << mapshift) +
1786 			    sm->sm_start;
1787 			entry_run = SM_RUN_DECODE(word) << mapshift;
1788 
1789 			(void) printf("\t    [%6llu] %c "
1790 			    "range: %012llx-%012llx size: %08llx\n",
1791 			    (u_longlong_t)entry_id, entry_type,
1792 			    (u_longlong_t)entry_off,
1793 			    (u_longlong_t)(entry_off + entry_run - 1),
1794 			    (u_longlong_t)entry_run);
1795 		} else {
1796 			/* it is a two-word entry so we read another word */
1797 			ASSERT(sm_entry_is_double_word(word));
1798 
1799 			uint64_t extra_word;
1800 			offset += sizeof (extra_word);
1801 			ASSERT3U(offset, <, space_map_length(sm));
1802 			VERIFY0(dmu_read(os, space_map_object(sm), offset,
1803 			    sizeof (extra_word), &extra_word,
1804 			    DMU_READ_PREFETCH));
1805 
1806 			entry_run = SM2_RUN_DECODE(word) << mapshift;
1807 			entry_vdev = SM2_VDEV_DECODE(word);
1808 			entry_type = (SM2_TYPE_DECODE(extra_word) == SM_ALLOC) ?
1809 			    'A' : 'F';
1810 			entry_off = (SM2_OFFSET_DECODE(extra_word) <<
1811 			    mapshift) + sm->sm_start;
1812 
1813 			if (zopt_metaslab_args == 0 ||
1814 			    zopt_metaslab[0] == entry_vdev) {
1815 				(void) printf("\t    [%6llu] %c "
1816 				    "range: %012llx-%012llx size: %08llx "
1817 				    "vdev: %llu\n",
1818 				    (u_longlong_t)entry_id, entry_type,
1819 				    (u_longlong_t)entry_off,
1820 				    (u_longlong_t)(entry_off + entry_run - 1),
1821 				    (u_longlong_t)entry_run,
1822 				    (u_longlong_t)entry_vdev);
1823 			}
1824 		}
1825 
1826 		if (entry_type == 'A')
1827 			alloc += entry_run;
1828 		else
1829 			alloc -= entry_run;
1830 		entry_id++;
1831 	}
1832 	if (alloc != space_map_allocated(sm)) {
1833 		(void) printf("space_map_object alloc (%lld) INCONSISTENT "
1834 		    "with space map summary (%lld)\n",
1835 		    (longlong_t)space_map_allocated(sm), (longlong_t)alloc);
1836 	}
1837 }
1838 
1839 static void
1840 dump_metaslab_stats(metaslab_t *msp)
1841 {
1842 	char maxbuf[32];
1843 	zfs_range_tree_t *rt = msp->ms_allocatable;
1844 	zfs_btree_t *t = &msp->ms_allocatable_by_size;
1845 	int free_pct = zfs_range_tree_space(rt) * 100 / msp->ms_size;
1846 
1847 	/* max sure nicenum has enough space */
1848 	_Static_assert(sizeof (maxbuf) >= NN_NUMBUF_SZ, "maxbuf truncated");
1849 
1850 	zdb_nicenum(metaslab_largest_allocatable(msp), maxbuf, sizeof (maxbuf));
1851 
1852 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
1853 	    "segments", zfs_btree_numnodes(t), "maxsize", maxbuf,
1854 	    "freepct", free_pct);
1855 	(void) printf("\tIn-memory histogram:\n");
1856 	dump_histogram(rt->rt_histogram, ZFS_RANGE_TREE_HISTOGRAM_SIZE, 0);
1857 }
1858 
1859 static void
1860 dump_allocated(void *arg, uint64_t start, uint64_t size)
1861 {
1862 	uint64_t *off = arg;
1863 	if (*off != start)
1864 		(void) printf("ALLOC: %"PRIu64" %"PRIu64"\n", *off,
1865 		    start - *off);
1866 	*off = start + size;
1867 }
1868 
1869 static void
1870 dump_metaslab(metaslab_t *msp)
1871 {
1872 	vdev_t *vd = msp->ms_group->mg_vd;
1873 	spa_t *spa = vd->vdev_spa;
1874 	space_map_t *sm = msp->ms_sm;
1875 	char freebuf[32];
1876 
1877 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf,
1878 	    sizeof (freebuf));
1879 
1880 	(void) printf(
1881 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
1882 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
1883 	    (u_longlong_t)space_map_object(sm), freebuf);
1884 
1885 	if (dump_opt[ARG_ALLOCATED] ||
1886 	    (dump_opt['m'] > 2 && !dump_opt['L'])) {
1887 		mutex_enter(&msp->ms_lock);
1888 		VERIFY0(metaslab_load(msp));
1889 	}
1890 
1891 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
1892 		zfs_range_tree_stat_verify(msp->ms_allocatable);
1893 		dump_metaslab_stats(msp);
1894 	}
1895 
1896 	if (dump_opt[ARG_ALLOCATED]) {
1897 		uint64_t off = msp->ms_start;
1898 		zfs_range_tree_walk(msp->ms_allocatable, dump_allocated,
1899 		    &off);
1900 		if (off != msp->ms_start + msp->ms_size)
1901 			(void) printf("ALLOC: %"PRIu64" %"PRIu64"\n", off,
1902 			    msp->ms_size - off);
1903 	}
1904 
1905 	if (dump_opt['m'] > 1 && sm != NULL &&
1906 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
1907 		/*
1908 		 * The space map histogram represents free space in chunks
1909 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
1910 		 */
1911 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
1912 		    (u_longlong_t)msp->ms_fragmentation);
1913 		dump_histogram(sm->sm_phys->smp_histogram,
1914 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
1915 	}
1916 
1917 	if (dump_opt[ARG_ALLOCATED] ||
1918 	    (dump_opt['m'] > 2 && !dump_opt['L'])) {
1919 		metaslab_unload(msp);
1920 		mutex_exit(&msp->ms_lock);
1921 	}
1922 
1923 	if (vd->vdev_ops == &vdev_draid_ops)
1924 		ASSERT3U(msp->ms_size, <=, 1ULL << vd->vdev_ms_shift);
1925 	else
1926 		ASSERT3U(msp->ms_size, ==, 1ULL << vd->vdev_ms_shift);
1927 
1928 	dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
1929 
1930 	if (spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) {
1931 		(void) printf("\tFlush data:\n\tunflushed txg=%llu\n\n",
1932 		    (u_longlong_t)metaslab_unflushed_txg(msp));
1933 	}
1934 }
1935 
1936 static void
1937 print_vdev_metaslab_header(vdev_t *vd)
1938 {
1939 	vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
1940 	const char *bias_str = "";
1941 	if (alloc_bias == VDEV_BIAS_LOG || vd->vdev_islog) {
1942 		bias_str = VDEV_ALLOC_BIAS_LOG;
1943 	} else if (alloc_bias == VDEV_BIAS_SPECIAL) {
1944 		bias_str = VDEV_ALLOC_BIAS_SPECIAL;
1945 	} else if (alloc_bias == VDEV_BIAS_DEDUP) {
1946 		bias_str = VDEV_ALLOC_BIAS_DEDUP;
1947 	}
1948 
1949 	uint64_t ms_flush_data_obj = 0;
1950 	if (vd->vdev_top_zap != 0) {
1951 		int error = zap_lookup(spa_meta_objset(vd->vdev_spa),
1952 		    vd->vdev_top_zap, VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS,
1953 		    sizeof (uint64_t), 1, &ms_flush_data_obj);
1954 		if (error != ENOENT) {
1955 			ASSERT0(error);
1956 		}
1957 	}
1958 
1959 	(void) printf("\tvdev %10llu\t%s  metaslab shift %4llu",
1960 	    (u_longlong_t)vd->vdev_id, bias_str,
1961 	    (u_longlong_t)vd->vdev_ms_shift);
1962 
1963 	if (ms_flush_data_obj != 0) {
1964 		(void) printf("   ms_unflushed_phys object %llu",
1965 		    (u_longlong_t)ms_flush_data_obj);
1966 	}
1967 
1968 	(void) printf("\n\t%-10s%5llu   %-19s   %-15s   %-12s\n",
1969 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
1970 	    "offset", "spacemap", "free");
1971 	(void) printf("\t%15s   %19s   %15s   %12s\n",
1972 	    "---------------", "-------------------",
1973 	    "---------------", "------------");
1974 }
1975 
1976 static void
1977 dump_metaslab_groups(spa_t *spa, boolean_t show_special)
1978 {
1979 	vdev_t *rvd = spa->spa_root_vdev;
1980 	metaslab_class_t *mc = spa_normal_class(spa);
1981 	metaslab_class_t *smc = spa_special_class(spa);
1982 	uint64_t fragmentation;
1983 
1984 	metaslab_class_histogram_verify(mc);
1985 
1986 	for (unsigned c = 0; c < rvd->vdev_children; c++) {
1987 		vdev_t *tvd = rvd->vdev_child[c];
1988 		metaslab_group_t *mg = tvd->vdev_mg;
1989 
1990 		if (mg == NULL || (mg->mg_class != mc &&
1991 		    (!show_special || mg->mg_class != smc)))
1992 			continue;
1993 
1994 		metaslab_group_histogram_verify(mg);
1995 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
1996 
1997 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
1998 		    "fragmentation",
1999 		    (u_longlong_t)tvd->vdev_id,
2000 		    (u_longlong_t)tvd->vdev_ms_count);
2001 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
2002 			(void) printf("%3s\n", "-");
2003 		} else {
2004 			(void) printf("%3llu%%\n",
2005 			    (u_longlong_t)mg->mg_fragmentation);
2006 		}
2007 		dump_histogram(mg->mg_histogram,
2008 		    ZFS_RANGE_TREE_HISTOGRAM_SIZE, 0);
2009 	}
2010 
2011 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
2012 	fragmentation = metaslab_class_fragmentation(mc);
2013 	if (fragmentation == ZFS_FRAG_INVALID)
2014 		(void) printf("\t%3s\n", "-");
2015 	else
2016 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
2017 	dump_histogram(mc->mc_histogram, ZFS_RANGE_TREE_HISTOGRAM_SIZE, 0);
2018 }
2019 
2020 static void
2021 print_vdev_indirect(vdev_t *vd)
2022 {
2023 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
2024 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
2025 	vdev_indirect_births_t *vib = vd->vdev_indirect_births;
2026 
2027 	if (vim == NULL) {
2028 		ASSERT0P(vib);
2029 		return;
2030 	}
2031 
2032 	ASSERT3U(vdev_indirect_mapping_object(vim), ==,
2033 	    vic->vic_mapping_object);
2034 	ASSERT3U(vdev_indirect_births_object(vib), ==,
2035 	    vic->vic_births_object);
2036 
2037 	(void) printf("indirect births obj %llu:\n",
2038 	    (longlong_t)vic->vic_births_object);
2039 	(void) printf("    vib_count = %llu\n",
2040 	    (longlong_t)vdev_indirect_births_count(vib));
2041 	for (uint64_t i = 0; i < vdev_indirect_births_count(vib); i++) {
2042 		vdev_indirect_birth_entry_phys_t *cur_vibe =
2043 		    &vib->vib_entries[i];
2044 		(void) printf("\toffset %llx -> txg %llu\n",
2045 		    (longlong_t)cur_vibe->vibe_offset,
2046 		    (longlong_t)cur_vibe->vibe_phys_birth_txg);
2047 	}
2048 	(void) printf("\n");
2049 
2050 	(void) printf("indirect mapping obj %llu:\n",
2051 	    (longlong_t)vic->vic_mapping_object);
2052 	(void) printf("    vim_max_offset = 0x%llx\n",
2053 	    (longlong_t)vdev_indirect_mapping_max_offset(vim));
2054 	(void) printf("    vim_bytes_mapped = 0x%llx\n",
2055 	    (longlong_t)vdev_indirect_mapping_bytes_mapped(vim));
2056 	(void) printf("    vim_count = %llu\n",
2057 	    (longlong_t)vdev_indirect_mapping_num_entries(vim));
2058 
2059 	if (dump_opt['d'] <= 5 && dump_opt['m'] <= 3)
2060 		return;
2061 
2062 	uint32_t *counts = vdev_indirect_mapping_load_obsolete_counts(vim);
2063 
2064 	for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
2065 		vdev_indirect_mapping_entry_phys_t *vimep =
2066 		    &vim->vim_entries[i];
2067 		(void) printf("\t<%llx:%llx:%llx> -> "
2068 		    "<%llx:%llx:%llx> (%x obsolete)\n",
2069 		    (longlong_t)vd->vdev_id,
2070 		    (longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
2071 		    (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
2072 		    (longlong_t)DVA_GET_VDEV(&vimep->vimep_dst),
2073 		    (longlong_t)DVA_GET_OFFSET(&vimep->vimep_dst),
2074 		    (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
2075 		    counts[i]);
2076 	}
2077 	(void) printf("\n");
2078 
2079 	uint64_t obsolete_sm_object;
2080 	VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object));
2081 	if (obsolete_sm_object != 0) {
2082 		objset_t *mos = vd->vdev_spa->spa_meta_objset;
2083 		(void) printf("obsolete space map object %llu:\n",
2084 		    (u_longlong_t)obsolete_sm_object);
2085 		ASSERT(vd->vdev_obsolete_sm != NULL);
2086 		ASSERT3U(space_map_object(vd->vdev_obsolete_sm), ==,
2087 		    obsolete_sm_object);
2088 		dump_spacemap(mos, vd->vdev_obsolete_sm);
2089 		(void) printf("\n");
2090 	}
2091 }
2092 
2093 static void
2094 dump_metaslabs(spa_t *spa)
2095 {
2096 	vdev_t *vd, *rvd = spa->spa_root_vdev;
2097 	uint64_t m, c = 0, children = rvd->vdev_children;
2098 
2099 	(void) printf("\nMetaslabs:\n");
2100 
2101 	if (zopt_metaslab_args > 0) {
2102 		c = zopt_metaslab[0];
2103 
2104 		if (c >= children)
2105 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
2106 
2107 		if (zopt_metaslab_args > 1) {
2108 			vd = rvd->vdev_child[c];
2109 			print_vdev_metaslab_header(vd);
2110 
2111 			for (m = 1; m < zopt_metaslab_args; m++) {
2112 				if (zopt_metaslab[m] < vd->vdev_ms_count)
2113 					dump_metaslab(
2114 					    vd->vdev_ms[zopt_metaslab[m]]);
2115 				else
2116 					(void) fprintf(stderr, "bad metaslab "
2117 					    "number %llu\n",
2118 					    (u_longlong_t)zopt_metaslab[m]);
2119 			}
2120 			(void) printf("\n");
2121 			return;
2122 		}
2123 		children = c + 1;
2124 	}
2125 	for (; c < children; c++) {
2126 		vd = rvd->vdev_child[c];
2127 		print_vdev_metaslab_header(vd);
2128 
2129 		print_vdev_indirect(vd);
2130 
2131 		for (m = 0; m < vd->vdev_ms_count; m++)
2132 			dump_metaslab(vd->vdev_ms[m]);
2133 		(void) printf("\n");
2134 	}
2135 }
2136 
2137 static void
2138 dump_log_spacemaps(spa_t *spa)
2139 {
2140 	if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))
2141 		return;
2142 
2143 	(void) printf("\nLog Space Maps in Pool:\n");
2144 	for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg);
2145 	    sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls)) {
2146 		space_map_t *sm = NULL;
2147 		VERIFY0(space_map_open(&sm, spa_meta_objset(spa),
2148 		    sls->sls_sm_obj, 0, UINT64_MAX, SPA_MINBLOCKSHIFT));
2149 
2150 		(void) printf("Log Spacemap object %llu txg %llu\n",
2151 		    (u_longlong_t)sls->sls_sm_obj, (u_longlong_t)sls->sls_txg);
2152 		dump_spacemap(spa->spa_meta_objset, sm);
2153 		space_map_close(sm);
2154 	}
2155 	(void) printf("\n");
2156 }
2157 
2158 static void
2159 dump_ddt_entry(const ddt_t *ddt, const ddt_lightweight_entry_t *ddlwe,
2160     uint64_t index)
2161 {
2162 	const ddt_key_t *ddk = &ddlwe->ddlwe_key;
2163 	char blkbuf[BP_SPRINTF_LEN];
2164 	blkptr_t blk;
2165 	int p;
2166 
2167 	for (p = 0; p < DDT_NPHYS(ddt); p++) {
2168 		const ddt_univ_phys_t *ddp = &ddlwe->ddlwe_phys;
2169 		ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
2170 
2171 		if (ddt_phys_birth(ddp, v) == 0)
2172 			continue;
2173 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, v, &blk);
2174 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
2175 		(void) printf("index %llx refcnt %llu phys %d %s\n",
2176 		    (u_longlong_t)index, (u_longlong_t)ddt_phys_refcnt(ddp, v),
2177 		    p, blkbuf);
2178 	}
2179 }
2180 
2181 static void
2182 dump_dedup_ratio(const ddt_stat_t *dds)
2183 {
2184 	double rL, rP, rD, D, dedup, compress, copies;
2185 
2186 	if (dds->dds_blocks == 0)
2187 		return;
2188 
2189 	rL = (double)dds->dds_ref_lsize;
2190 	rP = (double)dds->dds_ref_psize;
2191 	rD = (double)dds->dds_ref_dsize;
2192 	D = (double)dds->dds_dsize;
2193 
2194 	dedup = rD / D;
2195 	compress = rL / rP;
2196 	copies = rD / rP;
2197 
2198 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
2199 	    "dedup * compress / copies = %.2f\n\n",
2200 	    dedup, compress, copies, dedup * compress / copies);
2201 }
2202 
2203 static void
2204 dump_ddt_log(ddt_t *ddt)
2205 {
2206 	if (ddt->ddt_version != DDT_VERSION_FDT ||
2207 	    !(ddt->ddt_flags & DDT_FLAG_LOG))
2208 		return;
2209 
2210 	for (int n = 0; n < 2; n++) {
2211 		ddt_log_t *ddl = &ddt->ddt_log[n];
2212 
2213 		char flagstr[64] = {0};
2214 		if (ddl->ddl_flags > 0) {
2215 			flagstr[0] = ' ';
2216 			int c = 1;
2217 			if (ddl->ddl_flags & DDL_FLAG_FLUSHING)
2218 				c += strlcpy(&flagstr[c], " FLUSHING",
2219 				    sizeof (flagstr) - c);
2220 			if (ddl->ddl_flags & DDL_FLAG_CHECKPOINT)
2221 				c += strlcpy(&flagstr[c], " CHECKPOINT",
2222 				    sizeof (flagstr) - c);
2223 			if (ddl->ddl_flags &
2224 			    ~(DDL_FLAG_FLUSHING|DDL_FLAG_CHECKPOINT))
2225 				c += strlcpy(&flagstr[c], " UNKNOWN",
2226 				    sizeof (flagstr) - c);
2227 			flagstr[1] = '[';
2228 			flagstr[c] = ']';
2229 		}
2230 
2231 		uint64_t count = avl_numnodes(&ddl->ddl_tree);
2232 
2233 		printf(DMU_POOL_DDT_LOG ": flags=0x%02x%s; obj=%llu; "
2234 		    "len=%llu; txg=%llu; entries=%llu\n",
2235 		    zio_checksum_table[ddt->ddt_checksum].ci_name, n,
2236 		    ddl->ddl_flags, flagstr,
2237 		    (u_longlong_t)ddl->ddl_object,
2238 		    (u_longlong_t)ddl->ddl_length,
2239 		    (u_longlong_t)ddl->ddl_first_txg, (u_longlong_t)count);
2240 
2241 		if (ddl->ddl_flags & DDL_FLAG_CHECKPOINT) {
2242 			const ddt_key_t *ddk = &ddl->ddl_checkpoint;
2243 			printf("    checkpoint: "
2244 			    "%016llx:%016llx:%016llx:%016llx:%016llx\n",
2245 			    (u_longlong_t)ddk->ddk_cksum.zc_word[0],
2246 			    (u_longlong_t)ddk->ddk_cksum.zc_word[1],
2247 			    (u_longlong_t)ddk->ddk_cksum.zc_word[2],
2248 			    (u_longlong_t)ddk->ddk_cksum.zc_word[3],
2249 			    (u_longlong_t)ddk->ddk_prop);
2250 		}
2251 
2252 		if (count == 0 || dump_opt['D'] < 4)
2253 			continue;
2254 
2255 		ddt_lightweight_entry_t ddlwe;
2256 		uint64_t index = 0;
2257 		for (ddt_log_entry_t *ddle = avl_first(&ddl->ddl_tree);
2258 		    ddle; ddle = AVL_NEXT(&ddl->ddl_tree, ddle)) {
2259 			DDT_LOG_ENTRY_TO_LIGHTWEIGHT(ddt, ddle, &ddlwe);
2260 			dump_ddt_entry(ddt, &ddlwe, index++);
2261 		}
2262 	}
2263 }
2264 
2265 static void
2266 dump_ddt_object(ddt_t *ddt, ddt_type_t type, ddt_class_t class)
2267 {
2268 	char name[DDT_NAMELEN];
2269 	ddt_lightweight_entry_t ddlwe;
2270 	uint64_t walk = 0;
2271 	dmu_object_info_t doi;
2272 	uint64_t count, dspace, mspace;
2273 	int error;
2274 
2275 	error = ddt_object_info(ddt, type, class, &doi);
2276 
2277 	if (error == ENOENT)
2278 		return;
2279 	ASSERT0(error);
2280 
2281 	error = ddt_object_count(ddt, type, class, &count);
2282 	ASSERT0(error);
2283 	if (count == 0)
2284 		return;
2285 
2286 	dspace = doi.doi_physical_blocks_512 << 9;
2287 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
2288 
2289 	ddt_object_name(ddt, type, class, name);
2290 
2291 	(void) printf("%s: dspace=%llu; mspace=%llu; entries=%llu\n", name,
2292 	    (u_longlong_t)dspace, (u_longlong_t)mspace, (u_longlong_t)count);
2293 
2294 	if (dump_opt['D'] < 3)
2295 		return;
2296 
2297 	(void) printf("%s: object=%llu\n", name,
2298 	    (u_longlong_t)ddt->ddt_object[type][class]);
2299 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class],
2300 	    dump_opt['P'] > 0);
2301 
2302 	if (dump_opt['D'] < 4)
2303 		return;
2304 
2305 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
2306 		return;
2307 
2308 	(void) printf("%s contents:\n\n", name);
2309 
2310 	while ((error = ddt_object_walk(ddt, type, class, &walk, &ddlwe)) == 0)
2311 		dump_ddt_entry(ddt, &ddlwe, walk);
2312 
2313 	ASSERT3U(error, ==, ENOENT);
2314 
2315 	(void) printf("\n");
2316 }
2317 
2318 static void
2319 dump_ddt(ddt_t *ddt)
2320 {
2321 	if (!ddt || ddt->ddt_version == DDT_VERSION_UNCONFIGURED)
2322 		return;
2323 
2324 	char flagstr[64] = {0};
2325 	if (ddt->ddt_flags > 0) {
2326 		flagstr[0] = ' ';
2327 		int c = 1;
2328 		if (ddt->ddt_flags & DDT_FLAG_FLAT)
2329 			c += strlcpy(&flagstr[c], " FLAT",
2330 			    sizeof (flagstr) - c);
2331 		if (ddt->ddt_flags & DDT_FLAG_LOG)
2332 			c += strlcpy(&flagstr[c], " LOG",
2333 			    sizeof (flagstr) - c);
2334 		if (ddt->ddt_flags & ~DDT_FLAG_MASK)
2335 			c += strlcpy(&flagstr[c], " UNKNOWN",
2336 			    sizeof (flagstr) - c);
2337 		flagstr[1] = '[';
2338 		flagstr[c] = ']';
2339 	}
2340 
2341 	printf("DDT-%s: version=%llu [%s]; flags=0x%02llx%s; rootobj=%llu\n",
2342 	    zio_checksum_table[ddt->ddt_checksum].ci_name,
2343 	    (u_longlong_t)ddt->ddt_version,
2344 	    (ddt->ddt_version == 0) ? "LEGACY" :
2345 	    (ddt->ddt_version == 1) ? "FDT" : "UNKNOWN",
2346 	    (u_longlong_t)ddt->ddt_flags, flagstr,
2347 	    (u_longlong_t)ddt->ddt_dir_object);
2348 
2349 	for (ddt_type_t type = 0; type < DDT_TYPES; type++)
2350 		for (ddt_class_t class = 0; class < DDT_CLASSES; class++)
2351 			dump_ddt_object(ddt, type, class);
2352 
2353 	dump_ddt_log(ddt);
2354 }
2355 
2356 static void
2357 dump_all_ddts(spa_t *spa)
2358 {
2359 	ddt_histogram_t ddh_total = {{{0}}};
2360 	ddt_stat_t dds_total = {0};
2361 
2362 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++)
2363 		dump_ddt(spa->spa_ddt[c]);
2364 
2365 	ddt_get_dedup_stats(spa, &dds_total);
2366 
2367 	if (dds_total.dds_blocks == 0) {
2368 		(void) printf("All DDTs are empty\n");
2369 		return;
2370 	}
2371 
2372 	(void) printf("\n");
2373 
2374 	if (dump_opt['D'] > 1) {
2375 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
2376 		ddt_get_dedup_histogram(spa, &ddh_total);
2377 		zpool_dump_ddt(&dds_total, &ddh_total, dump_opt['P'] > 0);
2378 	}
2379 
2380 	dump_dedup_ratio(&dds_total);
2381 
2382 	/*
2383 	 * Dump a histogram of unique class entry age
2384 	 */
2385 	if (dump_opt['D'] == 3 && getenv("ZDB_DDT_UNIQUE_AGE_HIST") != NULL) {
2386 		ddt_age_histo_t histogram;
2387 
2388 		(void) printf("DDT walk unique, building age histogram...\n");
2389 		ddt_prune_walk(spa, 0, &histogram);
2390 
2391 		/*
2392 		 * print out histogram for unique entry class birth
2393 		 */
2394 		if (histogram.dah_entries > 0) {
2395 			(void) printf("%5s  %9s  %4s\n",
2396 			    "age", "blocks", "amnt");
2397 			(void) printf("%5s  %9s  %4s\n",
2398 			    "-----", "---------", "----");
2399 			for (int i = 0; i < HIST_BINS; i++) {
2400 				(void) printf("%5d  %9d %4d%%\n", 1 << i,
2401 				    (int)histogram.dah_age_histo[i],
2402 				    (int)((histogram.dah_age_histo[i] * 100) /
2403 				    histogram.dah_entries));
2404 			}
2405 		}
2406 	}
2407 }
2408 
2409 static void
2410 dump_brt(spa_t *spa)
2411 {
2412 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_BLOCK_CLONING)) {
2413 		printf("BRT: unsupported on this pool\n");
2414 		return;
2415 	}
2416 
2417 	if (!spa_feature_is_active(spa, SPA_FEATURE_BLOCK_CLONING)) {
2418 		printf("BRT: empty\n");
2419 		return;
2420 	}
2421 
2422 	char count[32], used[32], saved[32];
2423 	zdb_nicebytes(brt_get_used(spa), used, sizeof (used));
2424 	zdb_nicebytes(brt_get_saved(spa), saved, sizeof (saved));
2425 	uint64_t ratio = brt_get_ratio(spa);
2426 	printf("BRT: used %s; saved %s; ratio %llu.%02llux\n", used, saved,
2427 	    (u_longlong_t)(ratio / 100), (u_longlong_t)(ratio % 100));
2428 
2429 	if (dump_opt['T'] < 2)
2430 		return;
2431 
2432 	for (uint64_t vdevid = 0; vdevid < spa->spa_brt_nvdevs; vdevid++) {
2433 		brt_vdev_t *brtvd = spa->spa_brt_vdevs[vdevid];
2434 		if (!brtvd->bv_initiated) {
2435 			printf("BRT: vdev %" PRIu64 ": empty\n", vdevid);
2436 			continue;
2437 		}
2438 
2439 		zdb_nicenum(brtvd->bv_totalcount, count, sizeof (count));
2440 		zdb_nicebytes(brtvd->bv_usedspace, used, sizeof (used));
2441 		zdb_nicebytes(brtvd->bv_savedspace, saved, sizeof (saved));
2442 		printf("BRT: vdev %" PRIu64 ": refcnt %s; used %s; saved %s\n",
2443 		    vdevid, count, used, saved);
2444 	}
2445 
2446 	if (dump_opt['T'] < 3)
2447 		return;
2448 
2449 	/* -TTT shows a per-vdev histograms; -TTTT shows all entries */
2450 	boolean_t do_histo = dump_opt['T'] == 3;
2451 
2452 	char dva[64];
2453 
2454 	if (!do_histo)
2455 		printf("\n%-16s %-10s\n", "DVA", "REFCNT");
2456 
2457 	for (uint64_t vdevid = 0; vdevid < spa->spa_brt_nvdevs; vdevid++) {
2458 		brt_vdev_t *brtvd = spa->spa_brt_vdevs[vdevid];
2459 		if (!brtvd->bv_initiated)
2460 			continue;
2461 
2462 		uint64_t counts[64] = {};
2463 
2464 		zap_cursor_t zc;
2465 		zap_attribute_t *za = zap_attribute_alloc();
2466 		for (zap_cursor_init(&zc, spa->spa_meta_objset,
2467 		    brtvd->bv_mos_entries);
2468 		    zap_cursor_retrieve(&zc, za) == 0;
2469 		    zap_cursor_advance(&zc)) {
2470 			uint64_t refcnt;
2471 			VERIFY0(zap_lookup_uint64(spa->spa_meta_objset,
2472 			    brtvd->bv_mos_entries,
2473 			    (const uint64_t *)za->za_name, 1,
2474 			    za->za_integer_length, za->za_num_integers,
2475 			    &refcnt));
2476 
2477 			if (do_histo)
2478 				counts[highbit64(refcnt)]++;
2479 			else {
2480 				uint64_t offset =
2481 				    *(const uint64_t *)za->za_name;
2482 
2483 				snprintf(dva, sizeof (dva), "%" PRIu64 ":%llx",
2484 				    vdevid, (u_longlong_t)offset);
2485 				printf("%-16s %-10llu\n", dva,
2486 				    (u_longlong_t)refcnt);
2487 			}
2488 		}
2489 		zap_cursor_fini(&zc);
2490 		zap_attribute_free(za);
2491 
2492 		if (do_histo) {
2493 			printf("\nBRT: vdev %" PRIu64
2494 			    ": DVAs with 2^n refcnts:\n", vdevid);
2495 			dump_histogram(counts, 64, 0);
2496 		}
2497 	}
2498 }
2499 
2500 static void
2501 dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
2502 {
2503 	char *prefix = arg;
2504 
2505 	(void) printf("%s [%llu,%llu) length %llu\n",
2506 	    prefix,
2507 	    (u_longlong_t)start,
2508 	    (u_longlong_t)(start + size),
2509 	    (u_longlong_t)(size));
2510 }
2511 
2512 static void
2513 dump_dtl(vdev_t *vd, int indent)
2514 {
2515 	spa_t *spa = vd->vdev_spa;
2516 	boolean_t required;
2517 	const char *name[DTL_TYPES] = { "missing", "partial", "scrub",
2518 		"outage" };
2519 	char prefix[256];
2520 
2521 	spa_vdev_state_enter(spa, SCL_NONE);
2522 	required = vdev_dtl_required(vd);
2523 	(void) spa_vdev_state_exit(spa, NULL, 0);
2524 
2525 	if (indent == 0)
2526 		(void) printf("\nDirty time logs:\n\n");
2527 
2528 	(void) printf("\t%*s%s [%s]\n", indent, "",
2529 	    vd->vdev_path ? vd->vdev_path :
2530 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
2531 	    required ? "DTL-required" : "DTL-expendable");
2532 
2533 	for (int t = 0; t < DTL_TYPES; t++) {
2534 		zfs_range_tree_t *rt = vd->vdev_dtl[t];
2535 		if (zfs_range_tree_space(rt) == 0)
2536 			continue;
2537 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
2538 		    indent + 2, "", name[t]);
2539 		zfs_range_tree_walk(rt, dump_dtl_seg, prefix);
2540 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
2541 			dump_spacemap(spa->spa_meta_objset,
2542 			    vd->vdev_dtl_sm);
2543 	}
2544 
2545 	for (unsigned c = 0; c < vd->vdev_children; c++)
2546 		dump_dtl(vd->vdev_child[c], indent + 4);
2547 }
2548 
2549 static void
2550 dump_history(spa_t *spa)
2551 {
2552 	nvlist_t **events = NULL;
2553 	char *buf;
2554 	uint64_t resid, len, off = 0;
2555 	uint_t num = 0;
2556 	int error;
2557 	char tbuf[30];
2558 
2559 	if ((buf = malloc(SPA_OLD_MAXBLOCKSIZE)) == NULL) {
2560 		(void) fprintf(stderr, "%s: unable to allocate I/O buffer\n",
2561 		    __func__);
2562 		return;
2563 	}
2564 
2565 	do {
2566 		len = SPA_OLD_MAXBLOCKSIZE;
2567 
2568 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
2569 			(void) fprintf(stderr, "Unable to read history: "
2570 			    "error %d\n", error);
2571 			free(buf);
2572 			return;
2573 		}
2574 
2575 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
2576 			break;
2577 
2578 		off -= resid;
2579 	} while (len != 0);
2580 
2581 	(void) printf("\nHistory:\n");
2582 	for (unsigned i = 0; i < num; i++) {
2583 		boolean_t printed = B_FALSE;
2584 
2585 		if (nvlist_exists(events[i], ZPOOL_HIST_TIME)) {
2586 			time_t tsec;
2587 			struct tm t;
2588 
2589 			tsec = fnvlist_lookup_uint64(events[i],
2590 			    ZPOOL_HIST_TIME);
2591 			(void) localtime_r(&tsec, &t);
2592 			(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
2593 		} else {
2594 			tbuf[0] = '\0';
2595 		}
2596 
2597 		if (nvlist_exists(events[i], ZPOOL_HIST_CMD)) {
2598 			(void) printf("%s %s\n", tbuf,
2599 			    fnvlist_lookup_string(events[i], ZPOOL_HIST_CMD));
2600 		} else if (nvlist_exists(events[i], ZPOOL_HIST_INT_EVENT)) {
2601 			uint64_t ievent;
2602 
2603 			ievent = fnvlist_lookup_uint64(events[i],
2604 			    ZPOOL_HIST_INT_EVENT);
2605 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
2606 				goto next;
2607 
2608 			(void) printf(" %s [internal %s txg:%ju] %s\n",
2609 			    tbuf,
2610 			    zfs_history_event_names[ievent],
2611 			    fnvlist_lookup_uint64(events[i],
2612 			    ZPOOL_HIST_TXG),
2613 			    fnvlist_lookup_string(events[i],
2614 			    ZPOOL_HIST_INT_STR));
2615 		} else if (nvlist_exists(events[i], ZPOOL_HIST_INT_NAME)) {
2616 			(void) printf("%s [txg:%ju] %s", tbuf,
2617 			    fnvlist_lookup_uint64(events[i],
2618 			    ZPOOL_HIST_TXG),
2619 			    fnvlist_lookup_string(events[i],
2620 			    ZPOOL_HIST_INT_NAME));
2621 
2622 			if (nvlist_exists(events[i], ZPOOL_HIST_DSNAME)) {
2623 				(void) printf(" %s (%llu)",
2624 				    fnvlist_lookup_string(events[i],
2625 				    ZPOOL_HIST_DSNAME),
2626 				    (u_longlong_t)fnvlist_lookup_uint64(
2627 				    events[i],
2628 				    ZPOOL_HIST_DSID));
2629 			}
2630 
2631 			(void) printf(" %s\n", fnvlist_lookup_string(events[i],
2632 			    ZPOOL_HIST_INT_STR));
2633 		} else if (nvlist_exists(events[i], ZPOOL_HIST_IOCTL)) {
2634 			(void) printf("%s ioctl %s\n", tbuf,
2635 			    fnvlist_lookup_string(events[i],
2636 			    ZPOOL_HIST_IOCTL));
2637 
2638 			if (nvlist_exists(events[i], ZPOOL_HIST_INPUT_NVL)) {
2639 				(void) printf("    input:\n");
2640 				dump_nvlist(fnvlist_lookup_nvlist(events[i],
2641 				    ZPOOL_HIST_INPUT_NVL), 8);
2642 			}
2643 			if (nvlist_exists(events[i], ZPOOL_HIST_OUTPUT_NVL)) {
2644 				(void) printf("    output:\n");
2645 				dump_nvlist(fnvlist_lookup_nvlist(events[i],
2646 				    ZPOOL_HIST_OUTPUT_NVL), 8);
2647 			}
2648 			if (nvlist_exists(events[i], ZPOOL_HIST_ERRNO)) {
2649 				(void) printf("    errno: %lld\n",
2650 				    (longlong_t)fnvlist_lookup_int64(events[i],
2651 				    ZPOOL_HIST_ERRNO));
2652 			}
2653 		} else {
2654 			goto next;
2655 		}
2656 
2657 		printed = B_TRUE;
2658 next:
2659 		if (dump_opt['h'] > 1) {
2660 			if (!printed)
2661 				(void) printf("unrecognized record:\n");
2662 			dump_nvlist(events[i], 2);
2663 		}
2664 	}
2665 	free(buf);
2666 }
2667 
2668 static void
2669 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
2670 {
2671 	(void) os, (void) object, (void) data, (void) size;
2672 }
2673 
2674 static uint64_t
2675 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
2676     const zbookmark_phys_t *zb)
2677 {
2678 	if (dnp == NULL) {
2679 		ASSERT(zb->zb_level < 0);
2680 		if (zb->zb_object == 0)
2681 			return (zb->zb_blkid);
2682 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
2683 	}
2684 
2685 	ASSERT(zb->zb_level >= 0);
2686 
2687 	return ((zb->zb_blkid <<
2688 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
2689 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
2690 }
2691 
2692 static void
2693 snprintf_zstd_header(spa_t *spa, char *blkbuf, size_t buflen,
2694     const blkptr_t *bp)
2695 {
2696 	static abd_t *pabd = NULL;
2697 	void *buf;
2698 	zio_t *zio;
2699 	zfs_zstdhdr_t zstd_hdr;
2700 	int error;
2701 
2702 	if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_ZSTD)
2703 		return;
2704 
2705 	if (BP_IS_HOLE(bp))
2706 		return;
2707 
2708 	if (BP_IS_EMBEDDED(bp)) {
2709 		buf = malloc(SPA_MAXBLOCKSIZE);
2710 		if (buf == NULL) {
2711 			(void) fprintf(stderr, "out of memory\n");
2712 			zdb_exit(1);
2713 		}
2714 		decode_embedded_bp_compressed(bp, buf);
2715 		memcpy(&zstd_hdr, buf, sizeof (zstd_hdr));
2716 		free(buf);
2717 		zstd_hdr.c_len = BE_32(zstd_hdr.c_len);
2718 		zstd_hdr.raw_version_level = BE_32(zstd_hdr.raw_version_level);
2719 		(void) snprintf(blkbuf + strlen(blkbuf),
2720 		    buflen - strlen(blkbuf),
2721 		    " ZSTD:size=%u:version=%u:level=%u:EMBEDDED",
2722 		    zstd_hdr.c_len, zfs_get_hdrversion(&zstd_hdr),
2723 		    zfs_get_hdrlevel(&zstd_hdr));
2724 		return;
2725 	}
2726 
2727 	if (!pabd)
2728 		pabd = abd_alloc_for_io(SPA_MAXBLOCKSIZE, B_FALSE);
2729 	zio = zio_root(spa, NULL, NULL, 0);
2730 
2731 	/* Decrypt but don't decompress so we can read the compression header */
2732 	zio_nowait(zio_read(zio, spa, bp, pabd, BP_GET_PSIZE(bp), NULL, NULL,
2733 	    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW_COMPRESS,
2734 	    NULL));
2735 	error = zio_wait(zio);
2736 	if (error) {
2737 		(void) fprintf(stderr, "read failed: %d\n", error);
2738 		return;
2739 	}
2740 	buf = abd_borrow_buf_copy(pabd, BP_GET_LSIZE(bp));
2741 	memcpy(&zstd_hdr, buf, sizeof (zstd_hdr));
2742 	zstd_hdr.c_len = BE_32(zstd_hdr.c_len);
2743 	zstd_hdr.raw_version_level = BE_32(zstd_hdr.raw_version_level);
2744 
2745 	(void) snprintf(blkbuf + strlen(blkbuf),
2746 	    buflen - strlen(blkbuf),
2747 	    " ZSTD:size=%u:version=%u:level=%u:NORMAL",
2748 	    zstd_hdr.c_len, zfs_get_hdrversion(&zstd_hdr),
2749 	    zfs_get_hdrlevel(&zstd_hdr));
2750 
2751 	abd_return_buf_copy(pabd, buf, BP_GET_LSIZE(bp));
2752 }
2753 
2754 static void
2755 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp,
2756     boolean_t bp_freed)
2757 {
2758 	const dva_t *dva = bp->blk_dva;
2759 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
2760 	int i;
2761 
2762 	if (dump_opt['b'] >= 6) {
2763 		snprintf_blkptr(blkbuf, buflen, bp);
2764 		if (bp_freed) {
2765 			(void) snprintf(blkbuf + strlen(blkbuf),
2766 			    buflen - strlen(blkbuf), " %s", "FREE");
2767 		}
2768 		return;
2769 	}
2770 
2771 	if (BP_IS_EMBEDDED(bp)) {
2772 		(void) sprintf(blkbuf,
2773 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
2774 		    (int)BPE_GET_ETYPE(bp),
2775 		    (u_longlong_t)BPE_GET_LSIZE(bp),
2776 		    (u_longlong_t)BPE_GET_PSIZE(bp),
2777 		    (u_longlong_t)BP_GET_LOGICAL_BIRTH(bp));
2778 		return;
2779 	}
2780 
2781 	blkbuf[0] = '\0';
2782 
2783 	for (i = 0; i < ndvas; i++) {
2784 		(void) snprintf(blkbuf + strlen(blkbuf),
2785 		    buflen - strlen(blkbuf), "%llu:%llx:%llx%s ",
2786 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
2787 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
2788 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]),
2789 		    (DVA_GET_GANG(&dva[i]) ? "G" : ""));
2790 	}
2791 
2792 	if (BP_IS_HOLE(bp)) {
2793 		(void) snprintf(blkbuf + strlen(blkbuf),
2794 		    buflen - strlen(blkbuf),
2795 		    "%llxL B=%llu",
2796 		    (u_longlong_t)BP_GET_LSIZE(bp),
2797 		    (u_longlong_t)BP_GET_LOGICAL_BIRTH(bp));
2798 	} else {
2799 		(void) snprintf(blkbuf + strlen(blkbuf),
2800 		    buflen - strlen(blkbuf),
2801 		    "%llxL/%llxP F=%llu B=%llu/%llu",
2802 		    (u_longlong_t)BP_GET_LSIZE(bp),
2803 		    (u_longlong_t)BP_GET_PSIZE(bp),
2804 		    (u_longlong_t)BP_GET_FILL(bp),
2805 		    (u_longlong_t)BP_GET_LOGICAL_BIRTH(bp),
2806 		    (u_longlong_t)BP_GET_PHYSICAL_BIRTH(bp));
2807 		if (bp_freed)
2808 			(void) snprintf(blkbuf + strlen(blkbuf),
2809 			    buflen - strlen(blkbuf), " %s", "FREE");
2810 
2811 		(void) snprintf(blkbuf + strlen(blkbuf),
2812 		    buflen - strlen(blkbuf),
2813 		    " cksum=%016llx:%016llx:%016llx:%016llx",
2814 		    (u_longlong_t)bp->blk_cksum.zc_word[0],
2815 		    (u_longlong_t)bp->blk_cksum.zc_word[1],
2816 		    (u_longlong_t)bp->blk_cksum.zc_word[2],
2817 		    (u_longlong_t)bp->blk_cksum.zc_word[3]);
2818 	}
2819 }
2820 
2821 static void
2822 inverse_text(boolean_t on)
2823 {
2824 	if (isatty(STDOUT_FILENO)) {
2825 		if (on)
2826 			fputs("\x1b[7m", stdout);
2827 		else
2828 			fputs("\x1b[m", stdout);
2829 	}
2830 }
2831 
2832 static void
2833 print_file_layout_line(int line, int first_disk, int last_disk, int ashift,
2834     raidz_row_t *rr)
2835 {
2836 	if (first_disk != 0) {
2837 		/* Account for empty columns */
2838 		for (int c = 0; c < first_disk; c++) {
2839 			(void) printf("%s%s", (c == 0) ? " │ " : "  ",
2840 			    line == 0 ? "` ` ` ` ` " : " ` ` ` ` `");
2841 		}
2842 	}
2843 
2844 	/*
2845 	 * Check if we need to account for out of order disks in raidz1.
2846 	 * Here flip means the disks in first two column were exchanged.
2847 	 */
2848 	boolean_t flip = rr->rr_firstdatacol == 1 &&
2849 	    (rr->rr_offset & (1ULL << 20));
2850 
2851 	for (int c = 0; c < rr->rr_cols; c++) {
2852 		raidz_col_t *rc = &rr->rr_col[c];
2853 		char colname[8];
2854 		boolean_t pcol = flip ? (c == 1) : (c < rr->rr_firstdatacol);
2855 
2856 		if (rc->rc_devidx > last_disk)
2857 			continue;
2858 
2859 		(void) snprintf(colname, sizeof (colname), "%c%d",
2860 		    pcol ? 'P' : 'D', (flip && c < 2) ? 0 :
2861 		    pcol ? c : c - rr->rr_firstdatacol);
2862 
2863 		(void) printf("%s", (rc->rc_devidx == 0) ? " │ " : "  ");
2864 		if (pcol)
2865 			inverse_text(B_TRUE);
2866 		if (line == 0) {
2867 			(void) printf("%3s  %5d", colname,
2868 			    (int)(rc->rc_size >> ashift));
2869 		} else {
2870 			(void) printf("%10llx",
2871 			    (u_longlong_t)((rc->rc_offset +
2872 			    VDEV_LABEL_START_SIZE) >> ashift));
2873 		}
2874 		if (pcol)
2875 			inverse_text(B_FALSE);
2876 		if (rc->rc_devidx == last_disk) {
2877 			/* empty columns */
2878 			for (int c = last_disk + 1; c < rr->rr_cols; c++) {
2879 				(void) printf("  %s",
2880 				    line == 0 ? "` ` ` ` ` " : " ` ` ` ` `");
2881 			}
2882 			break;
2883 		}
2884 	}
2885 	(void) printf(" │\n");
2886 }
2887 
2888 
2889 static u_longlong_t
2890 print_indirect(spa_t *spa, blkptr_t *bp, const zbookmark_phys_t *zb,
2891     const dnode_phys_t *dnp)
2892 {
2893 	char blkbuf[BP_SPRINTF_LEN];
2894 	u_longlong_t offset;
2895 	int l;
2896 
2897 	offset = (u_longlong_t)blkid2offset(dnp, bp, zb);
2898 
2899 	(void) printf("%16llx ", offset);
2900 
2901 	ASSERT(zb->zb_level >= 0);
2902 
2903 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
2904 		if (l == zb->zb_level) {
2905 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
2906 		} else {
2907 			(void) printf(" ");
2908 		}
2909 	}
2910 
2911 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp, B_FALSE);
2912 	if (dump_opt['Z'] && BP_GET_COMPRESS(bp) == ZIO_COMPRESS_ZSTD)
2913 		snprintf_zstd_header(spa, blkbuf, sizeof (blkbuf), bp);
2914 	(void) printf("%s", blkbuf);
2915 
2916 	if (!BP_IS_EMBEDDED(bp)) {
2917 		if (BP_GET_TYPE(bp) != dnp->dn_type) {
2918 			(void) printf(" (ERROR: Block pointer type "
2919 			    "(%llu) does not match dnode type (%hhu))",
2920 			    BP_GET_TYPE(bp), dnp->dn_type);
2921 			corruption_found = B_TRUE;
2922 		}
2923 		if (BP_GET_LEVEL(bp) != zb->zb_level) {
2924 			(void) printf(" (ERROR: Block pointer level "
2925 			    "(%llu) does not match bookmark level (%lld))",
2926 			    BP_GET_LEVEL(bp), (longlong_t)zb->zb_level);
2927 			corruption_found = B_TRUE;
2928 		}
2929 	}
2930 	(void) printf("\n");
2931 
2932 	return (offset);
2933 }
2934 
2935 static void
2936 print_file_layout_raidz(vdev_t *vd, blkptr_t *bp, uint64_t file_offset,
2937     boolean_t last)
2938 {
2939 	/*
2940 	 * RAIDZ file data layout
2941 	 *
2942 	 * Use vdev_raidz_map_alloc() to get the actual layout
2943 	 */
2944 	const dva_t *dva = bp->blk_dva;
2945 	zio_t zio = {0};
2946 	zio.io_size = P2ROUNDUP_TYPED(BP_GET_PSIZE(bp),
2947 	    1ULL << vd->vdev_ashift, uint64_t);
2948 	zio.io_offset = DVA_GET_OFFSET(&dva[0]);
2949 	zio.io_type = ZIO_TYPE_READ;
2950 	zio.io_abd = abd_alloc_for_io(zio.io_size, B_FALSE);
2951 
2952 	vdev_raidz_t *vdrz = vd->vdev_tsd;
2953 	raidz_map_t *rm = vdev_raidz_map_alloc(&zio, vd->vdev_ashift,
2954 	    vd->vdev_children, vdrz->vd_nparity);
2955 	raidz_row_t *rr = rm->rm_row[0];
2956 
2957 	if (!dump_opt['H']) {
2958 		int last_disk = vd->vdev_children - 1;
2959 		/*
2960 		 * Account for out of order disks in raidz1.
2961 		 * For now just reverse them back and adjust for it later.
2962 		 */
2963 		if (rr->rr_firstdatacol == 1 &&
2964 		    (zio.io_offset & (1ULL << 20))) {
2965 			uint64_t devidx = rr->rr_col[0].rc_devidx;
2966 			rr->rr_col[0].rc_devidx = rr->rr_col[1].rc_devidx;
2967 			rr->rr_col[1].rc_devidx = devidx;
2968 		}
2969 		int first_disk = rr->rr_col[0].rc_devidx;
2970 
2971 		(void) printf("%12llx", (u_longlong_t)file_offset);
2972 		print_file_layout_line(0, first_disk, last_disk,
2973 		    vd->vdev_ashift, rr);
2974 		(void) printf("%*c", 12, ' ');
2975 		print_file_layout_line(1, first_disk, last_disk,
2976 		    vd->vdev_ashift, rr);
2977 		/* Check for split row */
2978 		if (first_disk != 0) {
2979 			(void) printf("%*c", 12, ' ');
2980 			print_file_layout_line(0, 0, first_disk - 1,
2981 			    vd->vdev_ashift, rr);
2982 			(void) printf("%*c", 12, ' ');
2983 			print_file_layout_line(1, 0, first_disk - 1,
2984 			    vd->vdev_ashift, rr);
2985 		}
2986 		/* seperate rows with a line */
2987 		(void) printf("%*c", 12, ' ');
2988 		for (int c = 0; c < vd->vdev_children; c++) {
2989 			(void) printf("%s%10s", (c == 0) ?
2990 			    (last ? " └─" : " ├─") : "──",
2991 			    "──────────");
2992 		}
2993 		(void) printf("─%s\n", last ? "┘" : "┤");
2994 	} else {
2995 		static uint64_t next_offset = 0;
2996 
2997 		if (next_offset != file_offset) {
2998 			(void) printf("skip hole\t-\t\t%lld\n",
2999 			    (u_longlong_t)((file_offset - next_offset) / 512));
3000 		}
3001 		next_offset = file_offset + BP_GET_LSIZE(bp);
3002 		uint64_t tmp_offset = file_offset;
3003 
3004 
3005 		for (int c = 0; c < rr->rr_cols; c++) {
3006 			boolean_t pcol = c < rr->rr_firstdatacol;
3007 			raidz_col_t *rc = &rr->rr_col[c];
3008 			char *path = vd->vdev_child[rc->rc_devidx]->vdev_path;
3009 
3010 			if (rc->rc_size == 0)
3011 				continue;
3012 			(void) printf("%s\t\t%llu\t%d",
3013 			    zfs_basename(path),
3014 			    (u_longlong_t)(rc->rc_offset +
3015 			    VDEV_LABEL_START_SIZE)/512,
3016 			    (int)rc->rc_size/512);
3017 			if (dump_opt['v']) {
3018 				char label = pcol ? 'P' : 'D';
3019 				int num;
3020 
3021 				if (c < 2) {
3022 					num = 0;
3023 				} else {
3024 					num = pcol ? c :
3025 					    (c - rr->rr_firstdatacol);
3026 				}
3027 				printf("\t%c%d", label, num);
3028 				if (dump_opt['v'] > 1) {
3029 					unsigned long long off;
3030 					if (pcol)
3031 						off = file_offset;
3032 					else
3033 						off = tmp_offset;
3034 					off = off / 512ULL;
3035 					printf("\t%llu", off);
3036 				}
3037 			}
3038 			if (!pcol)
3039 				tmp_offset += rc->rc_size;
3040 			printf("\n");
3041 		}
3042 	}
3043 }
3044 
3045 static void
3046 print_file_layout(spa_t *spa, blkptr_t *bp, const zbookmark_phys_t *zb,
3047     const dnode_phys_t *dnp)
3048 {
3049 	if (!BP_IS_EMBEDDED(bp)) {
3050 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
3051 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
3052 	}
3053 	ASSERT(zb->zb_level >= 0);
3054 
3055 	if (BP_IS_HOLE(bp))
3056 		return;
3057 
3058 	if (BP_IS_EMBEDDED(bp))
3059 		return;
3060 
3061 	const dva_t *dva = bp->blk_dva;
3062 	vdev_t *vd = spa->spa_root_vdev->vdev_child[DVA_GET_VDEV(&dva[0])];
3063 	uint64_t file_offset = blkid2offset(dnp, bp, zb);
3064 
3065 	if (strcmp("raidz", vd->vdev_ops->vdev_op_type) == 0) {
3066 		boolean_t last = (file_offset + BP_GET_LSIZE(bp)) >=
3067 		    (dnp->dn_used & ~0x03ffULL);
3068 		print_file_layout_raidz(vd, bp, file_offset, last);
3069 	}
3070 }
3071 
3072 static int
3073 visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
3074     blkptr_t *bp, const zbookmark_phys_t *zb)
3075 {
3076 	u_longlong_t offset = 0;
3077 	int err = 0;
3078 
3079 	if (BP_GET_BIRTH(bp) == 0)
3080 		return (0);
3081 
3082 	if (dump_opt['f']) {
3083 		if (BP_GET_LEVEL(bp) == 0)
3084 			print_file_layout(spa, bp, zb, dnp);
3085 	} else {
3086 		print_indirect(spa, bp, zb, dnp);
3087 	}
3088 
3089 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
3090 		arc_flags_t flags = ARC_FLAG_WAIT;
3091 		int i;
3092 		blkptr_t *cbp;
3093 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
3094 		arc_buf_t *buf;
3095 		uint64_t fill = 0;
3096 		ASSERT(!BP_IS_REDACTED(bp));
3097 
3098 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
3099 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
3100 		if (err)
3101 			return (err);
3102 		ASSERT(buf->b_data);
3103 
3104 		/* recursively visit blocks below this */
3105 		cbp = buf->b_data;
3106 		for (i = 0; i < epb; i++, cbp++) {
3107 			zbookmark_phys_t czb;
3108 
3109 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
3110 			    zb->zb_level - 1,
3111 			    zb->zb_blkid * epb + i);
3112 			err = visit_indirect(spa, dnp, cbp, &czb);
3113 			if (err)
3114 				break;
3115 			fill += BP_GET_FILL(cbp);
3116 		}
3117 		if (!err) {
3118 			if (fill != BP_GET_FILL(bp)) {
3119 				(void) printf("%16llx: Block pointer "
3120 				    "fill (%llu) does not match calculated "
3121 				    "value (%llu)\n", offset, BP_GET_FILL(bp),
3122 				    (u_longlong_t)fill);
3123 				corruption_found = B_TRUE;
3124 			}
3125 		}
3126 		arc_buf_destroy(buf, &buf);
3127 	}
3128 
3129 	return (err);
3130 }
3131 
3132 static void
3133 dump_indirect(dnode_t *dn)
3134 {
3135 	dnode_phys_t *dnp = dn->dn_phys;
3136 	zbookmark_phys_t czb;
3137 
3138 	(void) printf("Indirect blocks:\n");
3139 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
3140 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
3141 	for (int j = 0; j < dnp->dn_nblkptr; j++) {
3142 		czb.zb_blkid = j;
3143 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
3144 		    &dnp->dn_blkptr[j], &czb);
3145 	}
3146 
3147 	(void) printf("\n");
3148 }
3149 
3150 static int
3151 dump_indirect_layout(dnode_t *dn)
3152 {
3153 	dnode_phys_t *dnp = dn->dn_phys;
3154 	zbookmark_phys_t czb;
3155 
3156 	spa_t *spa = dmu_objset_spa(dn->dn_objset);
3157 	int ashift = spa->spa_root_vdev->vdev_child[0]->vdev_ashift;
3158 	int children = spa->spa_root_vdev->vdev_child[0]->vdev_children;
3159 
3160 	if (strcmp(spa->spa_root_vdev->vdev_child[0]->vdev_ops->vdev_op_type,
3161 	    "raidz") != 0) {
3162 		(void) fprintf(stderr, "file layout only supports raidz\n");
3163 		return (ENOTSUP);
3164 	}
3165 
3166 	/*
3167 	 * Start layout with a header
3168 	 */
3169 	if (dump_opt['H']) {
3170 		(void) printf("DISK\t\t\tLBA\tCOUNT");
3171 		if (dump_opt['v'])
3172 			(void) printf("\tTYPE");
3173 		if (dump_opt['v'] > 1)
3174 			(void) printf("\tOFFSET");
3175 		printf("\n");
3176 	} else {
3177 		char diskhdr[16];
3178 
3179 		(void) printf("%12s: %d\n", "block size", dn->dn_datablksz);
3180 		(void) printf("%12s: %s%d\n", "vdev_type",
3181 		    spa->spa_root_vdev->vdev_child[0]->vdev_ops->vdev_op_type,
3182 		    (int)vdev_get_nparity(spa->spa_root_vdev->vdev_child[0]));
3183 		(void) printf("%12s: %d\n", "sector size", 1 << ashift);
3184 		(void) printf("%12s: %d\n\n", "child disks", children);
3185 
3186 		(void) printf("%*c ", 12, ' ');
3187 
3188 		for (int c = 0; c < children; c++) {
3189 			(void) snprintf(diskhdr, sizeof (diskhdr),
3190 			    "V%d:DISK-%d", 0, c);
3191 			(void) printf(" %10s ", diskhdr);
3192 		}
3193 		(void) printf("\n");
3194 
3195 		(void) printf("%12s", "FILE OFFSET");
3196 		for (int c = 0; c < children; c++)
3197 			(void) printf("%s%10s", (c == 0) ? " ┌─" : "──",
3198 			    "──────────");
3199 		(void) printf("─┐\n");
3200 	}
3201 
3202 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
3203 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
3204 	for (int j = 0; j < dnp->dn_nblkptr; j++) {
3205 		czb.zb_blkid = j;
3206 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
3207 		    &dnp->dn_blkptr[j], &czb);
3208 	}
3209 
3210 	(void) printf("\n");
3211 	return (0);
3212 }
3213 
3214 static void
3215 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
3216 {
3217 	(void) os, (void) object;
3218 	dsl_dir_phys_t *dd = data;
3219 	time_t crtime;
3220 	char nice[32];
3221 
3222 	/* make sure nicenum has enough space */
3223 	_Static_assert(sizeof (nice) >= NN_NUMBUF_SZ, "nice truncated");
3224 
3225 	if (dd == NULL)
3226 		return;
3227 
3228 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
3229 
3230 	crtime = dd->dd_creation_time;
3231 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
3232 	(void) printf("\t\thead_dataset_obj = %llu\n",
3233 	    (u_longlong_t)dd->dd_head_dataset_obj);
3234 	(void) printf("\t\tparent_dir_obj = %llu\n",
3235 	    (u_longlong_t)dd->dd_parent_obj);
3236 	(void) printf("\t\torigin_obj = %llu\n",
3237 	    (u_longlong_t)dd->dd_origin_obj);
3238 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
3239 	    (u_longlong_t)dd->dd_child_dir_zapobj);
3240 	zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice));
3241 	(void) printf("\t\tused_bytes = %s\n", nice);
3242 	zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice));
3243 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
3244 	zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice));
3245 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
3246 	zdb_nicenum(dd->dd_quota, nice, sizeof (nice));
3247 	(void) printf("\t\tquota = %s\n", nice);
3248 	zdb_nicenum(dd->dd_reserved, nice, sizeof (nice));
3249 	(void) printf("\t\treserved = %s\n", nice);
3250 	(void) printf("\t\tprops_zapobj = %llu\n",
3251 	    (u_longlong_t)dd->dd_props_zapobj);
3252 	(void) printf("\t\tdeleg_zapobj = %llu\n",
3253 	    (u_longlong_t)dd->dd_deleg_zapobj);
3254 	(void) printf("\t\tflags = %llx\n",
3255 	    (u_longlong_t)dd->dd_flags);
3256 
3257 #define	DO(which) \
3258 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \
3259 	    sizeof (nice)); \
3260 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
3261 	DO(HEAD);
3262 	DO(SNAP);
3263 	DO(CHILD);
3264 	DO(CHILD_RSRV);
3265 	DO(REFRSRV);
3266 #undef DO
3267 	(void) printf("\t\tclones = %llu\n",
3268 	    (u_longlong_t)dd->dd_clones);
3269 }
3270 
3271 static void
3272 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
3273 {
3274 	(void) os, (void) object;
3275 	dsl_dataset_phys_t *ds = data;
3276 	time_t crtime;
3277 	char used[32], compressed[32], uncompressed[32], unique[32];
3278 	char blkbuf[BP_SPRINTF_LEN];
3279 
3280 	/* make sure nicenum has enough space */
3281 	_Static_assert(sizeof (used) >= NN_NUMBUF_SZ, "used truncated");
3282 	_Static_assert(sizeof (compressed) >= NN_NUMBUF_SZ,
3283 	    "compressed truncated");
3284 	_Static_assert(sizeof (uncompressed) >= NN_NUMBUF_SZ,
3285 	    "uncompressed truncated");
3286 	_Static_assert(sizeof (unique) >= NN_NUMBUF_SZ, "unique truncated");
3287 
3288 	if (ds == NULL)
3289 		return;
3290 
3291 	ASSERT(size == sizeof (*ds));
3292 	crtime = ds->ds_creation_time;
3293 	zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used));
3294 	zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed));
3295 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed,
3296 	    sizeof (uncompressed));
3297 	zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique));
3298 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
3299 
3300 	(void) printf("\t\tdir_obj = %llu\n",
3301 	    (u_longlong_t)ds->ds_dir_obj);
3302 	(void) printf("\t\tprev_snap_obj = %llu\n",
3303 	    (u_longlong_t)ds->ds_prev_snap_obj);
3304 	(void) printf("\t\tprev_snap_txg = %llu\n",
3305 	    (u_longlong_t)ds->ds_prev_snap_txg);
3306 	(void) printf("\t\tnext_snap_obj = %llu\n",
3307 	    (u_longlong_t)ds->ds_next_snap_obj);
3308 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
3309 	    (u_longlong_t)ds->ds_snapnames_zapobj);
3310 	(void) printf("\t\tnum_children = %llu\n",
3311 	    (u_longlong_t)ds->ds_num_children);
3312 	(void) printf("\t\tuserrefs_obj = %llu\n",
3313 	    (u_longlong_t)ds->ds_userrefs_obj);
3314 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
3315 	(void) printf("\t\tcreation_txg = %llu\n",
3316 	    (u_longlong_t)ds->ds_creation_txg);
3317 	(void) printf("\t\tdeadlist_obj = %llu\n",
3318 	    (u_longlong_t)ds->ds_deadlist_obj);
3319 	(void) printf("\t\tused_bytes = %s\n", used);
3320 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
3321 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
3322 	(void) printf("\t\tunique = %s\n", unique);
3323 	(void) printf("\t\tfsid_guid = %llu\n",
3324 	    (u_longlong_t)ds->ds_fsid_guid);
3325 	(void) printf("\t\tguid = %llu\n",
3326 	    (u_longlong_t)ds->ds_guid);
3327 	(void) printf("\t\tflags = %llx\n",
3328 	    (u_longlong_t)ds->ds_flags);
3329 	(void) printf("\t\tnext_clones_obj = %llu\n",
3330 	    (u_longlong_t)ds->ds_next_clones_obj);
3331 	(void) printf("\t\tprops_obj = %llu\n",
3332 	    (u_longlong_t)ds->ds_props_obj);
3333 	(void) printf("\t\tbp = %s\n", blkbuf);
3334 }
3335 
3336 static int
3337 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
3338 {
3339 	(void) arg, (void) tx;
3340 	char blkbuf[BP_SPRINTF_LEN];
3341 
3342 	if (BP_GET_BIRTH(bp) != 0) {
3343 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3344 		(void) printf("\t%s\n", blkbuf);
3345 	}
3346 	return (0);
3347 }
3348 
3349 static void
3350 dump_bptree(objset_t *os, uint64_t obj, const char *name)
3351 {
3352 	char bytes[32];
3353 	bptree_phys_t *bt;
3354 	dmu_buf_t *db;
3355 
3356 	/* make sure nicenum has enough space */
3357 	_Static_assert(sizeof (bytes) >= NN_NUMBUF_SZ, "bytes truncated");
3358 
3359 	if (dump_opt['d'] < 3)
3360 		return;
3361 
3362 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
3363 	bt = db->db_data;
3364 	zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes));
3365 	(void) printf("\n    %s: %llu datasets, %s\n",
3366 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
3367 	dmu_buf_rele(db, FTAG);
3368 
3369 	if (dump_opt['d'] < 5)
3370 		return;
3371 
3372 	(void) printf("\n");
3373 
3374 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
3375 }
3376 
3377 static int
3378 dump_bpobj_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, dmu_tx_t *tx)
3379 {
3380 	(void) arg, (void) tx;
3381 	char blkbuf[BP_SPRINTF_LEN];
3382 
3383 	ASSERT(BP_GET_BIRTH(bp) != 0);
3384 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp, bp_freed);
3385 	(void) printf("\t%s\n", blkbuf);
3386 	return (0);
3387 }
3388 
3389 static void
3390 dump_full_bpobj(bpobj_t *bpo, const char *name, int indent)
3391 {
3392 	char bytes[32];
3393 	char comp[32];
3394 	char uncomp[32];
3395 	uint64_t i;
3396 
3397 	/* make sure nicenum has enough space */
3398 	_Static_assert(sizeof (bytes) >= NN_NUMBUF_SZ, "bytes truncated");
3399 	_Static_assert(sizeof (comp) >= NN_NUMBUF_SZ, "comp truncated");
3400 	_Static_assert(sizeof (uncomp) >= NN_NUMBUF_SZ, "uncomp truncated");
3401 
3402 	if (dump_opt['d'] < 3)
3403 		return;
3404 
3405 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes));
3406 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
3407 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp));
3408 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp));
3409 		if (bpo->bpo_havefreed) {
3410 			(void) printf("    %*s: object %llu, %llu local "
3411 			    "blkptrs, %llu freed, %llu subobjs in object %llu, "
3412 			    "%s (%s/%s comp)\n",
3413 			    indent * 8, name,
3414 			    (u_longlong_t)bpo->bpo_object,
3415 			    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
3416 			    (u_longlong_t)bpo->bpo_phys->bpo_num_freed,
3417 			    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
3418 			    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
3419 			    bytes, comp, uncomp);
3420 		} else {
3421 			(void) printf("    %*s: object %llu, %llu local "
3422 			    "blkptrs, %llu subobjs in object %llu, "
3423 			    "%s (%s/%s comp)\n",
3424 			    indent * 8, name,
3425 			    (u_longlong_t)bpo->bpo_object,
3426 			    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
3427 			    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
3428 			    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
3429 			    bytes, comp, uncomp);
3430 		}
3431 
3432 		for (i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
3433 			uint64_t subobj;
3434 			bpobj_t subbpo;
3435 			int error;
3436 			VERIFY0(dmu_read(bpo->bpo_os,
3437 			    bpo->bpo_phys->bpo_subobjs,
3438 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
3439 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
3440 			if (error != 0) {
3441 				(void) printf("ERROR %u while trying to open "
3442 				    "subobj id %llu\n",
3443 				    error, (u_longlong_t)subobj);
3444 				corruption_found = B_TRUE;
3445 				continue;
3446 			}
3447 			dump_full_bpobj(&subbpo, "subobj", indent + 1);
3448 			bpobj_close(&subbpo);
3449 		}
3450 	} else {
3451 		if (bpo->bpo_havefreed) {
3452 			(void) printf("    %*s: object %llu, %llu blkptrs, "
3453 			    "%llu freed, %s\n",
3454 			    indent * 8, name,
3455 			    (u_longlong_t)bpo->bpo_object,
3456 			    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
3457 			    (u_longlong_t)bpo->bpo_phys->bpo_num_freed,
3458 			    bytes);
3459 		} else {
3460 			(void) printf("    %*s: object %llu, %llu blkptrs, "
3461 			    "%s\n",
3462 			    indent * 8, name,
3463 			    (u_longlong_t)bpo->bpo_object,
3464 			    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
3465 			    bytes);
3466 		}
3467 	}
3468 
3469 	if (dump_opt['d'] < 5)
3470 		return;
3471 
3472 	if (indent == 0) {
3473 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
3474 		(void) printf("\n");
3475 	}
3476 }
3477 
3478 static int
3479 dump_bookmark(dsl_pool_t *dp, char *name, boolean_t print_redact,
3480     boolean_t print_list)
3481 {
3482 	int err = 0;
3483 	zfs_bookmark_phys_t prop;
3484 	objset_t *mos = dp->dp_spa->spa_meta_objset;
3485 	err = dsl_bookmark_lookup(dp, name, NULL, &prop);
3486 
3487 	if (err != 0) {
3488 		return (err);
3489 	}
3490 
3491 	(void) printf("\t#%s: ", strchr(name, '#') + 1);
3492 	(void) printf("{guid: %llx creation_txg: %llu creation_time: "
3493 	    "%llu redaction_obj: %llu}\n", (u_longlong_t)prop.zbm_guid,
3494 	    (u_longlong_t)prop.zbm_creation_txg,
3495 	    (u_longlong_t)prop.zbm_creation_time,
3496 	    (u_longlong_t)prop.zbm_redaction_obj);
3497 
3498 	IMPLY(print_list, print_redact);
3499 	if (!print_redact || prop.zbm_redaction_obj == 0)
3500 		return (0);
3501 
3502 	redaction_list_t *rl;
3503 	VERIFY0(dsl_redaction_list_hold_obj(dp,
3504 	    prop.zbm_redaction_obj, FTAG, &rl));
3505 
3506 	redaction_list_phys_t *rlp = rl->rl_phys;
3507 	(void) printf("\tRedacted:\n\t\tProgress: ");
3508 	if (rlp->rlp_last_object != UINT64_MAX ||
3509 	    rlp->rlp_last_blkid != UINT64_MAX) {
3510 		(void) printf("%llu %llu (incomplete)\n",
3511 		    (u_longlong_t)rlp->rlp_last_object,
3512 		    (u_longlong_t)rlp->rlp_last_blkid);
3513 	} else {
3514 		(void) printf("complete\n");
3515 	}
3516 	(void) printf("\t\tSnapshots: [");
3517 	for (unsigned int i = 0; i < rlp->rlp_num_snaps; i++) {
3518 		if (i > 0)
3519 			(void) printf(", ");
3520 		(void) printf("%0llu",
3521 		    (u_longlong_t)rlp->rlp_snaps[i]);
3522 	}
3523 	(void) printf("]\n\t\tLength: %llu\n",
3524 	    (u_longlong_t)rlp->rlp_num_entries);
3525 
3526 	if (!print_list) {
3527 		dsl_redaction_list_rele(rl, FTAG);
3528 		return (0);
3529 	}
3530 
3531 	if (rlp->rlp_num_entries == 0) {
3532 		dsl_redaction_list_rele(rl, FTAG);
3533 		(void) printf("\t\tRedaction List: []\n\n");
3534 		return (0);
3535 	}
3536 
3537 	redact_block_phys_t *rbp_buf;
3538 	uint64_t size;
3539 	dmu_object_info_t doi;
3540 
3541 	VERIFY0(dmu_object_info(mos, prop.zbm_redaction_obj, &doi));
3542 	size = doi.doi_max_offset;
3543 	rbp_buf = kmem_alloc(size, KM_SLEEP);
3544 
3545 	err = dmu_read(mos, prop.zbm_redaction_obj, 0, size,
3546 	    rbp_buf, 0);
3547 	if (err != 0) {
3548 		dsl_redaction_list_rele(rl, FTAG);
3549 		kmem_free(rbp_buf, size);
3550 		return (err);
3551 	}
3552 
3553 	(void) printf("\t\tRedaction List: [{object: %llx, offset: "
3554 	    "%llx, blksz: %x, count: %llx}",
3555 	    (u_longlong_t)rbp_buf[0].rbp_object,
3556 	    (u_longlong_t)rbp_buf[0].rbp_blkid,
3557 	    (uint_t)(redact_block_get_size(&rbp_buf[0])),
3558 	    (u_longlong_t)redact_block_get_count(&rbp_buf[0]));
3559 
3560 	for (size_t i = 1; i < rlp->rlp_num_entries; i++) {
3561 		(void) printf(",\n\t\t{object: %llx, offset: %llx, "
3562 		    "blksz: %x, count: %llx}",
3563 		    (u_longlong_t)rbp_buf[i].rbp_object,
3564 		    (u_longlong_t)rbp_buf[i].rbp_blkid,
3565 		    (uint_t)(redact_block_get_size(&rbp_buf[i])),
3566 		    (u_longlong_t)redact_block_get_count(&rbp_buf[i]));
3567 	}
3568 	dsl_redaction_list_rele(rl, FTAG);
3569 	kmem_free(rbp_buf, size);
3570 	(void) printf("]\n\n");
3571 	return (0);
3572 }
3573 
3574 static void
3575 dump_bookmarks(objset_t *os, int verbosity)
3576 {
3577 	zap_cursor_t zc;
3578 	zap_attribute_t *attrp;
3579 	dsl_dataset_t *ds = dmu_objset_ds(os);
3580 	dsl_pool_t *dp = spa_get_dsl(os->os_spa);
3581 	objset_t *mos = os->os_spa->spa_meta_objset;
3582 	if (verbosity < 4)
3583 		return;
3584 	attrp = zap_attribute_alloc();
3585 	dsl_pool_config_enter(dp, FTAG);
3586 
3587 	for (zap_cursor_init(&zc, mos, ds->ds_bookmarks_obj);
3588 	    zap_cursor_retrieve(&zc, attrp) == 0;
3589 	    zap_cursor_advance(&zc)) {
3590 		char osname[ZFS_MAX_DATASET_NAME_LEN];
3591 		char buf[ZFS_MAX_DATASET_NAME_LEN];
3592 		int len;
3593 		dmu_objset_name(os, osname);
3594 		len = snprintf(buf, sizeof (buf), "%s#%s", osname,
3595 		    attrp->za_name);
3596 		VERIFY3S(len, <, ZFS_MAX_DATASET_NAME_LEN);
3597 		(void) dump_bookmark(dp, buf, verbosity >= 5, verbosity >= 6);
3598 	}
3599 	zap_cursor_fini(&zc);
3600 	dsl_pool_config_exit(dp, FTAG);
3601 	zap_attribute_free(attrp);
3602 }
3603 
3604 static void
3605 bpobj_count_refd(bpobj_t *bpo)
3606 {
3607 	mos_obj_refd(bpo->bpo_object);
3608 
3609 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
3610 		mos_obj_refd(bpo->bpo_phys->bpo_subobjs);
3611 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
3612 			uint64_t subobj;
3613 			bpobj_t subbpo;
3614 			int error;
3615 			VERIFY0(dmu_read(bpo->bpo_os,
3616 			    bpo->bpo_phys->bpo_subobjs,
3617 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
3618 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
3619 			if (error != 0) {
3620 				(void) printf("ERROR %u while trying to open "
3621 				    "subobj id %llu\n",
3622 				    error, (u_longlong_t)subobj);
3623 				corruption_found = B_TRUE;
3624 				continue;
3625 			}
3626 			bpobj_count_refd(&subbpo);
3627 			bpobj_close(&subbpo);
3628 		}
3629 	}
3630 }
3631 
3632 static int
3633 dsl_deadlist_entry_count_refd(void *arg, dsl_deadlist_entry_t *dle)
3634 {
3635 	spa_t *spa = arg;
3636 	uint64_t empty_bpobj = spa->spa_dsl_pool->dp_empty_bpobj;
3637 	if (dle->dle_bpobj.bpo_object != empty_bpobj)
3638 		bpobj_count_refd(&dle->dle_bpobj);
3639 	return (0);
3640 }
3641 
3642 static int
3643 dsl_deadlist_entry_dump(void *arg, dsl_deadlist_entry_t *dle)
3644 {
3645 	ASSERT0P(arg);
3646 	if (dump_opt['d'] >= 5) {
3647 		char buf[128];
3648 		(void) snprintf(buf, sizeof (buf),
3649 		    "mintxg %llu -> obj %llu",
3650 		    (longlong_t)dle->dle_mintxg,
3651 		    (longlong_t)dle->dle_bpobj.bpo_object);
3652 
3653 		dump_full_bpobj(&dle->dle_bpobj, buf, 0);
3654 	} else {
3655 		(void) printf("mintxg %llu -> obj %llu\n",
3656 		    (longlong_t)dle->dle_mintxg,
3657 		    (longlong_t)dle->dle_bpobj.bpo_object);
3658 	}
3659 	return (0);
3660 }
3661 
3662 static void
3663 dump_blkptr_list(dsl_deadlist_t *dl, const char *name)
3664 {
3665 	char bytes[32];
3666 	char comp[32];
3667 	char uncomp[32];
3668 	char entries[32];
3669 	spa_t *spa = dmu_objset_spa(dl->dl_os);
3670 	uint64_t empty_bpobj = spa->spa_dsl_pool->dp_empty_bpobj;
3671 
3672 	if (dl->dl_oldfmt) {
3673 		if (dl->dl_bpobj.bpo_object != empty_bpobj)
3674 			bpobj_count_refd(&dl->dl_bpobj);
3675 	} else {
3676 		mos_obj_refd(dl->dl_object);
3677 		dsl_deadlist_iterate(dl, dsl_deadlist_entry_count_refd, spa);
3678 	}
3679 
3680 	/* make sure nicenum has enough space */
3681 	_Static_assert(sizeof (bytes) >= NN_NUMBUF_SZ, "bytes truncated");
3682 	_Static_assert(sizeof (comp) >= NN_NUMBUF_SZ, "comp truncated");
3683 	_Static_assert(sizeof (uncomp) >= NN_NUMBUF_SZ, "uncomp truncated");
3684 	_Static_assert(sizeof (entries) >= NN_NUMBUF_SZ, "entries truncated");
3685 
3686 	if (dump_opt['d'] < 3)
3687 		return;
3688 
3689 	if (dl->dl_oldfmt) {
3690 		dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
3691 		return;
3692 	}
3693 
3694 	zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes));
3695 	zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp));
3696 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp));
3697 	zdb_nicenum(avl_numnodes(&dl->dl_tree), entries, sizeof (entries));
3698 	(void) printf("\n    %s: %s (%s/%s comp), %s entries\n",
3699 	    name, bytes, comp, uncomp, entries);
3700 
3701 	if (dump_opt['d'] < 4)
3702 		return;
3703 
3704 	(void) putchar('\n');
3705 
3706 	dsl_deadlist_iterate(dl, dsl_deadlist_entry_dump, NULL);
3707 }
3708 
3709 static int
3710 verify_dd_livelist(objset_t *os)
3711 {
3712 	uint64_t ll_used, used, ll_comp, comp, ll_uncomp, uncomp;
3713 	dsl_pool_t *dp = spa_get_dsl(os->os_spa);
3714 	dsl_dir_t  *dd = os->os_dsl_dataset->ds_dir;
3715 
3716 	ASSERT(!dmu_objset_is_snapshot(os));
3717 	if (!dsl_deadlist_is_open(&dd->dd_livelist))
3718 		return (0);
3719 
3720 	/* Iterate through the livelist to check for duplicates */
3721 	dsl_deadlist_iterate(&dd->dd_livelist, sublivelist_verify_lightweight,
3722 	    NULL);
3723 
3724 	dsl_pool_config_enter(dp, FTAG);
3725 	dsl_deadlist_space(&dd->dd_livelist, &ll_used,
3726 	    &ll_comp, &ll_uncomp);
3727 
3728 	dsl_dataset_t *origin_ds;
3729 	ASSERT(dsl_pool_config_held(dp));
3730 	VERIFY0(dsl_dataset_hold_obj(dp,
3731 	    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin_ds));
3732 	VERIFY0(dsl_dataset_space_written(origin_ds, os->os_dsl_dataset,
3733 	    &used, &comp, &uncomp));
3734 	dsl_dataset_rele(origin_ds, FTAG);
3735 	dsl_pool_config_exit(dp, FTAG);
3736 	/*
3737 	 *  It's possible that the dataset's uncomp space is larger than the
3738 	 *  livelist's because livelists do not track embedded block pointers
3739 	 */
3740 	if (used != ll_used || comp != ll_comp || uncomp < ll_uncomp) {
3741 		char nice_used[32], nice_comp[32], nice_uncomp[32];
3742 		(void) printf("Discrepancy in space accounting:\n");
3743 		zdb_nicenum(used, nice_used, sizeof (nice_used));
3744 		zdb_nicenum(comp, nice_comp, sizeof (nice_comp));
3745 		zdb_nicenum(uncomp, nice_uncomp, sizeof (nice_uncomp));
3746 		(void) printf("dir: used %s, comp %s, uncomp %s\n",
3747 		    nice_used, nice_comp, nice_uncomp);
3748 		zdb_nicenum(ll_used, nice_used, sizeof (nice_used));
3749 		zdb_nicenum(ll_comp, nice_comp, sizeof (nice_comp));
3750 		zdb_nicenum(ll_uncomp, nice_uncomp, sizeof (nice_uncomp));
3751 		(void) printf("livelist: used %s, comp %s, uncomp %s\n",
3752 		    nice_used, nice_comp, nice_uncomp);
3753 		return (1);
3754 	}
3755 	return (0);
3756 }
3757 
3758 static char *key_material = NULL;
3759 
3760 static boolean_t
3761 zdb_derive_key(dsl_dir_t *dd, uint8_t *key_out)
3762 {
3763 	uint64_t keyformat, salt, iters;
3764 	int i;
3765 	unsigned char c;
3766 	FILE *f;
3767 
3768 	VERIFY0(zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
3769 	    zfs_prop_to_name(ZFS_PROP_KEYFORMAT), sizeof (uint64_t),
3770 	    1, &keyformat));
3771 
3772 	switch (keyformat) {
3773 	case ZFS_KEYFORMAT_HEX:
3774 		for (i = 0; i < WRAPPING_KEY_LEN * 2; i += 2) {
3775 			if (!isxdigit(key_material[i]) ||
3776 			    !isxdigit(key_material[i+1]))
3777 				return (B_FALSE);
3778 			if (sscanf(&key_material[i], "%02hhx", &c) != 1)
3779 				return (B_FALSE);
3780 			key_out[i / 2] = c;
3781 		}
3782 		break;
3783 
3784 	case ZFS_KEYFORMAT_PASSPHRASE:
3785 		VERIFY0(zap_lookup(dd->dd_pool->dp_meta_objset,
3786 		    dd->dd_crypto_obj, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT),
3787 		    sizeof (uint64_t), 1, &salt));
3788 		VERIFY0(zap_lookup(dd->dd_pool->dp_meta_objset,
3789 		    dd->dd_crypto_obj, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS),
3790 		    sizeof (uint64_t), 1, &iters));
3791 
3792 		if (PKCS5_PBKDF2_HMAC_SHA1(key_material, strlen(key_material),
3793 		    ((uint8_t *)&salt), sizeof (uint64_t), iters,
3794 		    WRAPPING_KEY_LEN, key_out) != 1)
3795 			return (B_FALSE);
3796 
3797 		break;
3798 
3799 	case ZFS_KEYFORMAT_RAW:
3800 		if ((f = fopen(key_material, "r")) == NULL)
3801 			return (B_FALSE);
3802 
3803 		if (fread(key_out, 1, WRAPPING_KEY_LEN, f) !=
3804 		    WRAPPING_KEY_LEN) {
3805 			(void) fclose(f);
3806 			return (B_FALSE);
3807 		}
3808 
3809 		/* Check the key length */
3810 		if (fgetc(f) != EOF) {
3811 			(void) fclose(f);
3812 			return (B_FALSE);
3813 		}
3814 
3815 		(void) fclose(f);
3816 		break;
3817 
3818 	default:
3819 		fatal("no support for key format %u\n",
3820 		    (unsigned int) keyformat);
3821 	}
3822 
3823 	return (B_TRUE);
3824 }
3825 
3826 static char encroot[ZFS_MAX_DATASET_NAME_LEN];
3827 static boolean_t key_loaded = B_FALSE;
3828 
3829 static int
3830 zdb_load_key(objset_t *os)
3831 {
3832 	dsl_pool_t *dp;
3833 	dsl_dir_t *dd, *rdd;
3834 	uint8_t key[WRAPPING_KEY_LEN];
3835 	uint64_t rddobj;
3836 	int err = 0;
3837 
3838 	dp = spa_get_dsl(os->os_spa);
3839 	dd = os->os_dsl_dataset->ds_dir;
3840 
3841 	dsl_pool_config_enter(dp, FTAG);
3842 	VERIFY0(zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
3843 	    DSL_CRYPTO_KEY_ROOT_DDOBJ, sizeof (uint64_t), 1, &rddobj));
3844 	VERIFY0(dsl_dir_hold_obj(dd->dd_pool, rddobj, NULL, FTAG, &rdd));
3845 	dsl_dir_name(rdd, encroot);
3846 	dsl_dir_rele(rdd, FTAG);
3847 
3848 	if (!zdb_derive_key(dd, key))
3849 		err = EINVAL;
3850 	dsl_pool_config_exit(dp, FTAG);
3851 
3852 	if (err != 0) {
3853 		fprintf(stderr, "couldn't derive encryption key\n");
3854 		return (err);
3855 	}
3856 
3857 	ASSERT3U(dsl_dataset_get_keystatus(dd), ==, ZFS_KEYSTATUS_UNAVAILABLE);
3858 
3859 	dsl_crypto_params_t *dcp;
3860 	nvlist_t *crypto_args;
3861 
3862 	crypto_args = fnvlist_alloc();
3863 	fnvlist_add_uint8_array(crypto_args, "wkeydata",
3864 	    (uint8_t *)key, WRAPPING_KEY_LEN);
3865 	VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
3866 	    NULL, crypto_args, &dcp));
3867 	err = spa_keystore_load_wkey(encroot, dcp, B_FALSE);
3868 
3869 	dsl_crypto_params_free(dcp, (err != 0));
3870 	fnvlist_free(crypto_args);
3871 
3872 	if (err != 0) {
3873 		fprintf(stderr,
3874 		    "couldn't load encryption key for %s: %s\n",
3875 		    encroot, err == ZFS_ERR_CRYPTO_NOTSUP ?
3876 		    "crypto params not supported" : strerror(err));
3877 		return (err);
3878 	}
3879 
3880 	ASSERT3U(dsl_dataset_get_keystatus(dd), ==, ZFS_KEYSTATUS_AVAILABLE);
3881 
3882 	printf("Unlocked encryption root: %s\n", encroot);
3883 	key_loaded = B_TRUE;
3884 
3885 	return (0);
3886 }
3887 
3888 static void
3889 zdb_unload_key(void)
3890 {
3891 	if (!key_loaded)
3892 		return;
3893 
3894 	VERIFY0(spa_keystore_unload_wkey(encroot));
3895 	key_loaded = B_FALSE;
3896 }
3897 
3898 static avl_tree_t idx_tree;
3899 static avl_tree_t domain_tree;
3900 static boolean_t fuid_table_loaded;
3901 static objset_t *sa_os = NULL;
3902 static sa_attr_type_t *sa_attr_table = NULL;
3903 
3904 static int
3905 open_objset(const char *path, const void *tag, objset_t **osp)
3906 {
3907 	int err;
3908 	uint64_t sa_attrs = 0;
3909 	uint64_t version = 0;
3910 
3911 	VERIFY0P(sa_os);
3912 
3913 	/*
3914 	 * We can't own an objset if it's redacted.  Therefore, we do this
3915 	 * dance: hold the objset, then acquire a long hold on its dataset, then
3916 	 * release the pool (which is held as part of holding the objset).
3917 	 */
3918 
3919 	if (dump_opt['K']) {
3920 		/* decryption requested, try to load keys */
3921 		err = dmu_objset_hold(path, tag, osp);
3922 		if (err != 0) {
3923 			(void) fprintf(stderr, "failed to hold dataset "
3924 			    "'%s': %s\n",
3925 			    path, strerror(err));
3926 			return (err);
3927 		}
3928 
3929 		/*
3930 		 * Only try to load the key and unlock the dataset if it is
3931 		 * actually encrypted; otherwise we'll just crash. Just
3932 		 * ignore the -K switch entirely otherwise; it's useful to be
3933 		 * able to provide even if it's not needed.
3934 		 */
3935 		if ((*osp)->os_encrypted) {
3936 			dsl_dataset_long_hold(dmu_objset_ds(*osp), tag);
3937 			dsl_pool_rele(dmu_objset_pool(*osp), tag);
3938 
3939 			err = zdb_load_key(*osp);
3940 
3941 			/* release it all */
3942 			dsl_dataset_long_rele(dmu_objset_ds(*osp), tag);
3943 			dsl_dataset_rele(dmu_objset_ds(*osp), tag);
3944 
3945 			if (err != 0) {
3946 				*osp = NULL;
3947 				return (err);
3948 			}
3949 		} else {
3950 			dmu_objset_rele(*osp, tag);
3951 		}
3952 	}
3953 
3954 	int ds_hold_flags = key_loaded ? DS_HOLD_FLAG_DECRYPT : 0;
3955 
3956 	err = dmu_objset_hold_flags(path, ds_hold_flags, tag, osp);
3957 	if (err != 0) {
3958 		(void) fprintf(stderr, "failed to hold dataset '%s': %s\n",
3959 		    path, strerror(err));
3960 		*osp = NULL;
3961 		return (err);
3962 	}
3963 	dsl_dataset_long_hold(dmu_objset_ds(*osp), tag);
3964 	dsl_pool_rele(dmu_objset_pool(*osp), tag);
3965 
3966 	if (dmu_objset_type(*osp) == DMU_OST_ZFS &&
3967 	    (key_loaded || !(*osp)->os_encrypted)) {
3968 		(void) zap_lookup(*osp, MASTER_NODE_OBJ, ZPL_VERSION_STR,
3969 		    8, 1, &version);
3970 		if (version >= ZPL_VERSION_SA) {
3971 			(void) zap_lookup(*osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
3972 			    8, 1, &sa_attrs);
3973 		}
3974 		err = sa_setup(*osp, sa_attrs, zfs_attr_table, ZPL_END,
3975 		    &sa_attr_table);
3976 		if (err != 0) {
3977 			(void) fprintf(stderr, "sa_setup failed: %s\n",
3978 			    strerror(err));
3979 			dsl_dataset_long_rele(dmu_objset_ds(*osp), tag);
3980 			dsl_dataset_rele_flags(dmu_objset_ds(*osp),
3981 			    ds_hold_flags, tag);
3982 			*osp = NULL;
3983 		}
3984 	}
3985 	sa_os = *osp;
3986 
3987 	return (err);
3988 }
3989 
3990 static void
3991 close_objset(objset_t *os, const void *tag)
3992 {
3993 	VERIFY3P(os, ==, sa_os);
3994 	if (os->os_sa != NULL)
3995 		sa_tear_down(os);
3996 	dsl_dataset_long_rele(dmu_objset_ds(os), tag);
3997 	dsl_dataset_rele_flags(dmu_objset_ds(os),
3998 	    key_loaded ? DS_HOLD_FLAG_DECRYPT : 0, tag);
3999 	sa_attr_table = NULL;
4000 	sa_os = NULL;
4001 
4002 	zdb_unload_key();
4003 }
4004 
4005 static void
4006 fuid_table_destroy(void)
4007 {
4008 	if (fuid_table_loaded) {
4009 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
4010 		fuid_table_loaded = B_FALSE;
4011 	}
4012 }
4013 
4014 /*
4015  * Clean up DDT internal state. ddt_lookup() adds entries to ddt_tree, which on
4016  * a live pool are normally cleaned up during ddt_sync(). We can't do that (and
4017  * wouldn't want to anyway), but if we don't clean up the presence of stuff on
4018  * ddt_tree will trip asserts in ddt_table_free(). So, we clean up ourselves.
4019  *
4020  * Note that this is not a particularly efficient way to do this, but
4021  * ddt_remove() is the only public method that can do the work we need, and it
4022  * requires the right locks and etc to do the job. This is only ever called
4023  * during zdb shutdown so efficiency is not especially important.
4024  */
4025 static void
4026 zdb_ddt_cleanup(spa_t *spa)
4027 {
4028 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
4029 		ddt_t *ddt = spa->spa_ddt[c];
4030 		if (!ddt)
4031 			continue;
4032 
4033 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4034 		ddt_enter(ddt);
4035 		ddt_entry_t *dde = avl_first(&ddt->ddt_tree), *next;
4036 		while (dde) {
4037 			next = AVL_NEXT(&ddt->ddt_tree, dde);
4038 			dde->dde_io = NULL;
4039 			ddt_remove(ddt, dde);
4040 			dde = next;
4041 		}
4042 		ddt_exit(ddt);
4043 		spa_config_exit(spa, SCL_CONFIG, FTAG);
4044 	}
4045 }
4046 
4047 static void
4048 zdb_exit(int reason)
4049 {
4050 	if (spa != NULL)
4051 		zdb_ddt_cleanup(spa);
4052 
4053 	if (os != NULL) {
4054 		close_objset(os, FTAG);
4055 	} else if (spa != NULL) {
4056 		spa_close(spa, FTAG);
4057 	}
4058 
4059 	fuid_table_destroy();
4060 
4061 	if (kernel_init_done)
4062 		kernel_fini();
4063 
4064 	exit(reason);
4065 }
4066 
4067 /*
4068  * print uid or gid information.
4069  * For normal POSIX id just the id is printed in decimal format.
4070  * For CIFS files with FUID the fuid is printed in hex followed by
4071  * the domain-rid string.
4072  */
4073 static void
4074 print_idstr(uint64_t id, const char *id_type)
4075 {
4076 	if (FUID_INDEX(id)) {
4077 		const char *domain =
4078 		    zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
4079 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
4080 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
4081 	} else {
4082 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
4083 	}
4084 
4085 }
4086 
4087 static void
4088 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
4089 {
4090 	uint32_t uid_idx, gid_idx;
4091 
4092 	uid_idx = FUID_INDEX(uid);
4093 	gid_idx = FUID_INDEX(gid);
4094 
4095 	/* Load domain table, if not already loaded */
4096 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
4097 		uint64_t fuid_obj;
4098 
4099 		/* first find the fuid object.  It lives in the master node */
4100 		VERIFY0(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
4101 		    8, 1, &fuid_obj));
4102 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
4103 		(void) zfs_fuid_table_load(os, fuid_obj,
4104 		    &idx_tree, &domain_tree);
4105 		fuid_table_loaded = B_TRUE;
4106 	}
4107 
4108 	print_idstr(uid, "uid");
4109 	print_idstr(gid, "gid");
4110 }
4111 
4112 static void
4113 dump_znode_sa_xattr(sa_handle_t *hdl)
4114 {
4115 	nvlist_t *sa_xattr;
4116 	nvpair_t *elem = NULL;
4117 	int sa_xattr_size = 0;
4118 	int sa_xattr_entries = 0;
4119 	int error;
4120 	char *sa_xattr_packed;
4121 
4122 	error = sa_size(hdl, sa_attr_table[ZPL_DXATTR], &sa_xattr_size);
4123 	if (error || sa_xattr_size == 0)
4124 		return;
4125 
4126 	sa_xattr_packed = malloc(sa_xattr_size);
4127 	if (sa_xattr_packed == NULL)
4128 		return;
4129 
4130 	error = sa_lookup(hdl, sa_attr_table[ZPL_DXATTR],
4131 	    sa_xattr_packed, sa_xattr_size);
4132 	if (error) {
4133 		free(sa_xattr_packed);
4134 		return;
4135 	}
4136 
4137 	error = nvlist_unpack(sa_xattr_packed, sa_xattr_size, &sa_xattr, 0);
4138 	if (error) {
4139 		free(sa_xattr_packed);
4140 		return;
4141 	}
4142 
4143 	while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL)
4144 		sa_xattr_entries++;
4145 
4146 	(void) printf("\tSA xattrs: %d bytes, %d entries\n\n",
4147 	    sa_xattr_size, sa_xattr_entries);
4148 	while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL) {
4149 		boolean_t can_print = !dump_opt['P'];
4150 		uchar_t *value;
4151 		uint_t cnt, idx;
4152 
4153 		(void) printf("\t\t%s = ", nvpair_name(elem));
4154 		nvpair_value_byte_array(elem, &value, &cnt);
4155 
4156 		for (idx = 0; idx < cnt; ++idx) {
4157 			if (!isprint(value[idx])) {
4158 				can_print = B_FALSE;
4159 				break;
4160 			}
4161 		}
4162 
4163 		for (idx = 0; idx < cnt; ++idx) {
4164 			if (can_print)
4165 				(void) putchar(value[idx]);
4166 			else
4167 				(void) printf("\\%3.3o", value[idx]);
4168 		}
4169 		(void) putchar('\n');
4170 	}
4171 
4172 	nvlist_free(sa_xattr);
4173 	free(sa_xattr_packed);
4174 }
4175 
4176 static void
4177 dump_znode_symlink(sa_handle_t *hdl)
4178 {
4179 	int sa_symlink_size = 0;
4180 	char linktarget[MAXPATHLEN];
4181 	int error;
4182 
4183 	error = sa_size(hdl, sa_attr_table[ZPL_SYMLINK], &sa_symlink_size);
4184 	if (error || sa_symlink_size == 0) {
4185 		return;
4186 	}
4187 	if (sa_symlink_size >= sizeof (linktarget)) {
4188 		(void) printf("symlink size %d is too large\n",
4189 		    sa_symlink_size);
4190 		return;
4191 	}
4192 	linktarget[sa_symlink_size] = '\0';
4193 	if (sa_lookup(hdl, sa_attr_table[ZPL_SYMLINK],
4194 	    &linktarget, sa_symlink_size) == 0)
4195 		(void) printf("\ttarget	%s\n", linktarget);
4196 }
4197 
4198 static void
4199 dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
4200 {
4201 	(void) data, (void) size;
4202 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
4203 	sa_handle_t *hdl;
4204 	uint64_t xattr, rdev, gen;
4205 	uint64_t uid, gid, mode, fsize, parent, links;
4206 	uint64_t pflags;
4207 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
4208 	time_t z_crtime, z_atime, z_mtime, z_ctime;
4209 	sa_bulk_attr_t bulk[12];
4210 	int idx = 0;
4211 	int error;
4212 
4213 	VERIFY3P(os, ==, sa_os);
4214 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
4215 		(void) printf("Failed to get handle for SA znode\n");
4216 		return;
4217 	}
4218 
4219 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
4220 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
4221 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
4222 	    &links, 8);
4223 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
4224 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
4225 	    &mode, 8);
4226 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
4227 	    NULL, &parent, 8);
4228 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
4229 	    &fsize, 8);
4230 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
4231 	    acctm, 16);
4232 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
4233 	    modtm, 16);
4234 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
4235 	    crtm, 16);
4236 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
4237 	    chgtm, 16);
4238 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
4239 	    &pflags, 8);
4240 
4241 	if (sa_bulk_lookup(hdl, bulk, idx)) {
4242 		(void) sa_handle_destroy(hdl);
4243 		return;
4244 	}
4245 
4246 	z_crtime = (time_t)crtm[0];
4247 	z_atime = (time_t)acctm[0];
4248 	z_mtime = (time_t)modtm[0];
4249 	z_ctime = (time_t)chgtm[0];
4250 
4251 	if (dump_opt['d'] > 4) {
4252 		error = zfs_obj_to_path(os, object, path, sizeof (path));
4253 		if (error == ESTALE) {
4254 			(void) snprintf(path, sizeof (path), "on delete queue");
4255 		} else if (error != 0) {
4256 			leaked_objects++;
4257 			(void) snprintf(path, sizeof (path),
4258 			    "path not found, possibly leaked");
4259 		}
4260 		(void) printf("\tpath	%s\n", path);
4261 	}
4262 
4263 	if (S_ISLNK(mode))
4264 		dump_znode_symlink(hdl);
4265 	dump_uidgid(os, uid, gid);
4266 	(void) printf("\tatime	%s", ctime(&z_atime));
4267 	(void) printf("\tmtime	%s", ctime(&z_mtime));
4268 	(void) printf("\tctime	%s", ctime(&z_ctime));
4269 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
4270 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
4271 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
4272 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
4273 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
4274 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
4275 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
4276 	if (dmu_objset_projectquota_enabled(os) && (pflags & ZFS_PROJID)) {
4277 		uint64_t projid;
4278 
4279 		if (sa_lookup(hdl, sa_attr_table[ZPL_PROJID], &projid,
4280 		    sizeof (uint64_t)) == 0)
4281 			(void) printf("\tprojid	%llu\n", (u_longlong_t)projid);
4282 	}
4283 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
4284 	    sizeof (uint64_t)) == 0)
4285 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
4286 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
4287 	    sizeof (uint64_t)) == 0)
4288 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
4289 	dump_znode_sa_xattr(hdl);
4290 	sa_handle_destroy(hdl);
4291 }
4292 
4293 static void
4294 dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
4295 {
4296 	(void) os, (void) object, (void) data, (void) size;
4297 }
4298 
4299 static void
4300 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
4301 {
4302 	(void) os, (void) object, (void) data, (void) size;
4303 }
4304 
4305 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
4306 	dump_none,		/* unallocated			*/
4307 	dump_zap,		/* object directory		*/
4308 	dump_uint64,		/* object array			*/
4309 	dump_none,		/* packed nvlist		*/
4310 	dump_packed_nvlist,	/* packed nvlist size		*/
4311 	dump_none,		/* bpobj			*/
4312 	dump_bpobj,		/* bpobj header			*/
4313 	dump_none,		/* SPA space map header		*/
4314 	dump_none,		/* SPA space map		*/
4315 	dump_none,		/* ZIL intent log		*/
4316 	dump_dnode,		/* DMU dnode			*/
4317 	dump_dmu_objset,	/* DMU objset			*/
4318 	dump_dsl_dir,		/* DSL directory		*/
4319 	dump_zap,		/* DSL directory child map	*/
4320 	dump_zap,		/* DSL dataset snap map		*/
4321 	dump_zap,		/* DSL props			*/
4322 	dump_dsl_dataset,	/* DSL dataset			*/
4323 	dump_znode,		/* ZFS znode			*/
4324 	dump_acl,		/* ZFS V0 ACL			*/
4325 	dump_uint8,		/* ZFS plain file		*/
4326 	dump_zpldir,		/* ZFS directory		*/
4327 	dump_zap,		/* ZFS master node		*/
4328 	dump_zap,		/* ZFS delete queue		*/
4329 	dump_uint8,		/* zvol object			*/
4330 	dump_zap,		/* zvol prop			*/
4331 	dump_uint8,		/* other uint8[]		*/
4332 	dump_uint64,		/* other uint64[]		*/
4333 	dump_zap,		/* other ZAP			*/
4334 	dump_zap,		/* persistent error log		*/
4335 	dump_uint8,		/* SPA history			*/
4336 	dump_history_offsets,	/* SPA history offsets		*/
4337 	dump_zap,		/* Pool properties		*/
4338 	dump_zap,		/* DSL permissions		*/
4339 	dump_acl,		/* ZFS ACL			*/
4340 	dump_uint8,		/* ZFS SYSACL			*/
4341 	dump_none,		/* FUID nvlist			*/
4342 	dump_packed_nvlist,	/* FUID nvlist size		*/
4343 	dump_zap,		/* DSL dataset next clones	*/
4344 	dump_zap,		/* DSL scrub queue		*/
4345 	dump_zap,		/* ZFS user/group/project used	*/
4346 	dump_zap,		/* ZFS user/group/project quota	*/
4347 	dump_zap,		/* snapshot refcount tags	*/
4348 	dump_ddt_zap,		/* DDT ZAP object		*/
4349 	dump_zap,		/* DDT statistics		*/
4350 	dump_znode,		/* SA object			*/
4351 	dump_zap,		/* SA Master Node		*/
4352 	dump_sa_attrs,		/* SA attribute registration	*/
4353 	dump_sa_layouts,	/* SA attribute layouts		*/
4354 	dump_zap,		/* DSL scrub translations	*/
4355 	dump_none,		/* fake dedup BP		*/
4356 	dump_zap,		/* deadlist			*/
4357 	dump_none,		/* deadlist hdr			*/
4358 	dump_zap,		/* dsl clones			*/
4359 	dump_bpobj_subobjs,	/* bpobj subobjs		*/
4360 	dump_unknown,		/* Unknown type, must be last	*/
4361 };
4362 
4363 static boolean_t
4364 match_object_type(dmu_object_type_t obj_type, uint64_t flags)
4365 {
4366 	boolean_t match = B_TRUE;
4367 
4368 	switch (obj_type) {
4369 	case DMU_OT_DIRECTORY_CONTENTS:
4370 		if (!(flags & ZOR_FLAG_DIRECTORY))
4371 			match = B_FALSE;
4372 		break;
4373 	case DMU_OT_PLAIN_FILE_CONTENTS:
4374 		if (!(flags & ZOR_FLAG_PLAIN_FILE))
4375 			match = B_FALSE;
4376 		break;
4377 	case DMU_OT_SPACE_MAP:
4378 		if (!(flags & ZOR_FLAG_SPACE_MAP))
4379 			match = B_FALSE;
4380 		break;
4381 	case DMU_OT_ZVOL:
4382 		if (!(flags & ZOR_FLAG_ZVOL))
4383 			match = B_FALSE;
4384 		break;
4385 	default:
4386 		if (strcmp(zdb_ot_name(obj_type), "zap") == 0) {
4387 			if (!(flags & ZOR_FLAG_ZAP))
4388 				match = B_FALSE;
4389 			break;
4390 		}
4391 
4392 		/*
4393 		 * If all bits except some of the supported flags are
4394 		 * set, the user combined the all-types flag (A) with
4395 		 * a negated flag to exclude some types (e.g. A-f to
4396 		 * show all object types except plain files).
4397 		 */
4398 		if ((flags | ZOR_SUPPORTED_FLAGS) != ZOR_FLAG_ALL_TYPES)
4399 			match = B_FALSE;
4400 
4401 		break;
4402 	}
4403 
4404 	return (match);
4405 }
4406 
4407 static void
4408 dump_object(objset_t *os, uint64_t object, int verbosity,
4409     boolean_t *print_header, uint64_t *dnode_slots_used, uint64_t flags)
4410 {
4411 	dmu_buf_t *db = NULL;
4412 	dmu_object_info_t doi;
4413 	dnode_t *dn;
4414 	boolean_t dnode_held = B_FALSE;
4415 	void *bonus = NULL;
4416 	size_t bsize = 0;
4417 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32], dnsize[32];
4418 	char bonus_size[32];
4419 	char aux[50];
4420 	int error;
4421 
4422 	/* make sure nicenum has enough space */
4423 	_Static_assert(sizeof (iblk) >= NN_NUMBUF_SZ, "iblk truncated");
4424 	_Static_assert(sizeof (dblk) >= NN_NUMBUF_SZ, "dblk truncated");
4425 	_Static_assert(sizeof (lsize) >= NN_NUMBUF_SZ, "lsize truncated");
4426 	_Static_assert(sizeof (asize) >= NN_NUMBUF_SZ, "asize truncated");
4427 	_Static_assert(sizeof (bonus_size) >= NN_NUMBUF_SZ,
4428 	    "bonus_size truncated");
4429 
4430 	if (*print_header) {
4431 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %6s  %5s  %6s  %s\n",
4432 		    "Object", "lvl", "iblk", "dblk", "dsize", "dnsize",
4433 		    "lsize", "%full", "type");
4434 		*print_header = 0;
4435 	}
4436 
4437 	if (object == 0) {
4438 		dn = DMU_META_DNODE(os);
4439 		dmu_object_info_from_dnode(dn, &doi);
4440 	} else {
4441 		/*
4442 		 * Encrypted datasets will have sensitive bonus buffers
4443 		 * encrypted. Therefore we cannot hold the bonus buffer and
4444 		 * must hold the dnode itself instead.
4445 		 */
4446 		error = dmu_object_info(os, object, &doi);
4447 		if (error)
4448 			fatal("dmu_object_info() failed, errno %u", error);
4449 
4450 		if (!key_loaded && os->os_encrypted &&
4451 		    DMU_OT_IS_ENCRYPTED(doi.doi_bonus_type)) {
4452 			error = dnode_hold(os, object, FTAG, &dn);
4453 			if (error)
4454 				fatal("dnode_hold() failed, errno %u", error);
4455 			dnode_held = B_TRUE;
4456 		} else {
4457 			error = dmu_bonus_hold(os, object, FTAG, &db);
4458 			if (error)
4459 				fatal("dmu_bonus_hold(%llu) failed, errno %u",
4460 				    object, error);
4461 			bonus = db->db_data;
4462 			bsize = db->db_size;
4463 			dn = DB_DNODE((dmu_buf_impl_t *)db);
4464 		}
4465 	}
4466 
4467 	/*
4468 	 * Default to showing all object types if no flags were specified.
4469 	 */
4470 	if (flags != 0 && flags != ZOR_FLAG_ALL_TYPES &&
4471 	    !match_object_type(doi.doi_type, flags))
4472 		goto out;
4473 
4474 	if (dnode_slots_used)
4475 		*dnode_slots_used = doi.doi_dnodesize / DNODE_MIN_SIZE;
4476 
4477 	zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk));
4478 	zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk));
4479 	zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize));
4480 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize));
4481 	zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size));
4482 	zdb_nicenum(doi.doi_dnodesize, dnsize, sizeof (dnsize));
4483 	(void) snprintf(fill, sizeof (fill), "%6.2f", 100.0 *
4484 	    doi.doi_fill_count * doi.doi_data_block_size / (object == 0 ?
4485 	    DNODES_PER_BLOCK : 1) / doi.doi_max_offset);
4486 
4487 	aux[0] = '\0';
4488 
4489 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
4490 		(void) snprintf(aux + strlen(aux), sizeof (aux) - strlen(aux),
4491 		    " (K=%s)", ZDB_CHECKSUM_NAME(doi.doi_checksum));
4492 	}
4493 
4494 	if (doi.doi_compress == ZIO_COMPRESS_INHERIT &&
4495 	    ZIO_COMPRESS_HASLEVEL(os->os_compress) && verbosity >= 6) {
4496 		const char *compname = NULL;
4497 		if (zfs_prop_index_to_string(ZFS_PROP_COMPRESSION,
4498 		    ZIO_COMPRESS_RAW(os->os_compress, os->os_complevel),
4499 		    &compname) == 0) {
4500 			(void) snprintf(aux + strlen(aux),
4501 			    sizeof (aux) - strlen(aux), " (Z=inherit=%s)",
4502 			    compname);
4503 		} else {
4504 			(void) snprintf(aux + strlen(aux),
4505 			    sizeof (aux) - strlen(aux),
4506 			    " (Z=inherit=%s-unknown)",
4507 			    ZDB_COMPRESS_NAME(os->os_compress));
4508 		}
4509 	} else if (doi.doi_compress == ZIO_COMPRESS_INHERIT && verbosity >= 6) {
4510 		(void) snprintf(aux + strlen(aux), sizeof (aux) - strlen(aux),
4511 		    " (Z=inherit=%s)", ZDB_COMPRESS_NAME(os->os_compress));
4512 	} else if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
4513 		(void) snprintf(aux + strlen(aux), sizeof (aux) - strlen(aux),
4514 		    " (Z=%s)", ZDB_COMPRESS_NAME(doi.doi_compress));
4515 	}
4516 
4517 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %6s  %5s  %6s  %s%s\n",
4518 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
4519 	    asize, dnsize, lsize, fill, zdb_ot_name(doi.doi_type), aux);
4520 
4521 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
4522 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %5s  %6s  %s\n",
4523 		    "", "", "", "", "", "", bonus_size, "bonus",
4524 		    zdb_ot_name(doi.doi_bonus_type));
4525 	}
4526 
4527 	if (verbosity >= 4) {
4528 		(void) printf("\tdnode flags: %s%s%s%s\n",
4529 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
4530 		    "USED_BYTES " : "",
4531 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
4532 		    "USERUSED_ACCOUNTED " : "",
4533 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) ?
4534 		    "USEROBJUSED_ACCOUNTED " : "",
4535 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
4536 		    "SPILL_BLKPTR" : "");
4537 		(void) printf("\tdnode maxblkid: %llu\n",
4538 		    (longlong_t)dn->dn_phys->dn_maxblkid);
4539 
4540 		if (!dnode_held) {
4541 			object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os,
4542 			    object, bonus, bsize);
4543 		} else {
4544 			(void) printf("\t\t(bonus encrypted)\n");
4545 		}
4546 
4547 		if (key_loaded ||
4548 		    (!os->os_encrypted || !DMU_OT_IS_ENCRYPTED(doi.doi_type))) {
4549 			object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object,
4550 			    NULL, 0);
4551 		} else {
4552 			(void) printf("\t\t(object encrypted)\n");
4553 		}
4554 
4555 		*print_header = B_TRUE;
4556 	}
4557 
4558 	if (verbosity >= 5) {
4559 		if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
4560 			char blkbuf[BP_SPRINTF_LEN];
4561 			snprintf_blkptr_compact(blkbuf, sizeof (blkbuf),
4562 			    DN_SPILL_BLKPTR(dn->dn_phys), B_FALSE);
4563 			(void) printf("\nSpill block: %s\n", blkbuf);
4564 		}
4565 		dump_indirect(dn);
4566 	}
4567 
4568 	if (verbosity >= 5) {
4569 		/*
4570 		 * Report the list of segments that comprise the object.
4571 		 */
4572 		uint64_t start = 0;
4573 		uint64_t end;
4574 		uint64_t blkfill = 1;
4575 		int minlvl = 1;
4576 
4577 		if (dn->dn_type == DMU_OT_DNODE) {
4578 			minlvl = 0;
4579 			blkfill = DNODES_PER_BLOCK;
4580 		}
4581 
4582 		for (;;) {
4583 			char segsize[32];
4584 			/* make sure nicenum has enough space */
4585 			_Static_assert(sizeof (segsize) >= NN_NUMBUF_SZ,
4586 			    "segsize truncated");
4587 			error = dnode_next_offset(dn,
4588 			    0, &start, minlvl, blkfill, 0);
4589 			if (error)
4590 				break;
4591 			end = start;
4592 			error = dnode_next_offset(dn,
4593 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
4594 			zdb_nicenum(end - start, segsize, sizeof (segsize));
4595 			(void) printf("\t\tsegment [%016llx, %016llx)"
4596 			    " size %5s\n", (u_longlong_t)start,
4597 			    (u_longlong_t)end, segsize);
4598 			if (error)
4599 				break;
4600 			start = end;
4601 		}
4602 	}
4603 out:
4604 	if (db != NULL)
4605 		dmu_buf_rele(db, FTAG);
4606 	if (dnode_held)
4607 		dnode_rele(dn, FTAG);
4608 }
4609 
4610 static void
4611 dump_object_file_layout(objset_t *os, uint64_t object, int verbosity)
4612 {
4613 	(void) verbosity;
4614 	dmu_buf_t *db = NULL;
4615 	dmu_object_info_t doi;
4616 	dnode_t *dn;
4617 	boolean_t dnode_held = B_FALSE;
4618 	int error;
4619 
4620 	char osname[ZFS_MAX_DATASET_NAME_LEN];
4621 	dmu_objset_name(os, osname);
4622 	(void) printf("%12s: '%s'\n", "objset", osname);
4623 	(void) printf("%12s: %d\n", "object", (int)object);
4624 
4625 	if (object == 0) {
4626 		dn = DMU_META_DNODE(os);
4627 		dmu_object_info_from_dnode(dn, &doi);
4628 	} else {
4629 		/*
4630 		 * Encrypted datasets will have sensitive bonus buffers
4631 		 * encrypted. Therefore we cannot hold the bonus buffer and
4632 		 * must hold the dnode itself instead.
4633 		 */
4634 		error = dmu_object_info(os, object, &doi);
4635 		if (error)
4636 			fatal("dmu_object_info() failed, errno %u", error);
4637 
4638 		if (!key_loaded && os->os_encrypted &&
4639 		    DMU_OT_IS_ENCRYPTED(doi.doi_bonus_type)) {
4640 			error = dnode_hold(os, object, FTAG, &dn);
4641 			if (error)
4642 				fatal("dnode_hold() failed, errno %u", error);
4643 			dnode_held = B_TRUE;
4644 		} else {
4645 			error = dmu_bonus_hold(os, object, FTAG, &db);
4646 			if (error)
4647 				fatal("dmu_bonus_hold(%llu) failed, errno %u",
4648 				    object, error);
4649 			dn = DB_DNODE((dmu_buf_impl_t *)db);
4650 		}
4651 	}
4652 
4653 	error = dump_indirect_layout(dn);
4654 
4655 	if (db != NULL)
4656 		dmu_buf_rele(db, FTAG);
4657 	if (dnode_held)
4658 		dnode_rele(dn, FTAG);
4659 
4660 	if (error)
4661 		zdb_exit(1);
4662 }
4663 
4664 static void
4665 count_dir_mos_objects(dsl_dir_t *dd)
4666 {
4667 	mos_obj_refd(dd->dd_object);
4668 	mos_obj_refd(dsl_dir_phys(dd)->dd_child_dir_zapobj);
4669 	mos_obj_refd(dsl_dir_phys(dd)->dd_deleg_zapobj);
4670 	mos_obj_refd(dsl_dir_phys(dd)->dd_props_zapobj);
4671 	mos_obj_refd(dsl_dir_phys(dd)->dd_clones);
4672 
4673 	/*
4674 	 * The dd_crypto_obj can be referenced by multiple dsl_dir's.
4675 	 * Ignore the references after the first one.
4676 	 */
4677 	mos_obj_refd_multiple(dd->dd_crypto_obj);
4678 }
4679 
4680 static void
4681 count_ds_mos_objects(dsl_dataset_t *ds)
4682 {
4683 	mos_obj_refd(ds->ds_object);
4684 	mos_obj_refd(dsl_dataset_phys(ds)->ds_next_clones_obj);
4685 	mos_obj_refd(dsl_dataset_phys(ds)->ds_props_obj);
4686 	mos_obj_refd(dsl_dataset_phys(ds)->ds_userrefs_obj);
4687 	mos_obj_refd(dsl_dataset_phys(ds)->ds_snapnames_zapobj);
4688 	mos_obj_refd(ds->ds_bookmarks_obj);
4689 
4690 	if (!dsl_dataset_is_snapshot(ds)) {
4691 		count_dir_mos_objects(ds->ds_dir);
4692 	}
4693 }
4694 
4695 static const char *const objset_types[DMU_OST_NUMTYPES] = {
4696 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
4697 
4698 /*
4699  * Parse a string denoting a range of object IDs of the form
4700  * <start>[:<end>[:flags]], and store the results in zor.
4701  * Return 0 on success. On error, return 1 and update the msg
4702  * pointer to point to a descriptive error message.
4703  */
4704 static int
4705 parse_object_range(char *range, zopt_object_range_t *zor, const char **msg)
4706 {
4707 	uint64_t flags = 0;
4708 	char *p, *s, *dup, *flagstr, *tmp = NULL;
4709 	size_t len;
4710 	int i;
4711 	int rc = 0;
4712 
4713 	if (strchr(range, ':') == NULL) {
4714 		zor->zor_obj_start = strtoull(range, &p, 0);
4715 		if (*p != '\0') {
4716 			*msg = "Invalid characters in object ID";
4717 			rc = 1;
4718 		}
4719 		zor->zor_obj_start = ZDB_MAP_OBJECT_ID(zor->zor_obj_start);
4720 		zor->zor_obj_end = zor->zor_obj_start;
4721 		return (rc);
4722 	}
4723 
4724 	if (strchr(range, ':') == range) {
4725 		*msg = "Invalid leading colon";
4726 		rc = 1;
4727 		return (rc);
4728 	}
4729 
4730 	len = strlen(range);
4731 	if (range[len - 1] == ':') {
4732 		*msg = "Invalid trailing colon";
4733 		rc = 1;
4734 		return (rc);
4735 	}
4736 
4737 	dup = strdup(range);
4738 	s = strtok_r(dup, ":", &tmp);
4739 	zor->zor_obj_start = strtoull(s, &p, 0);
4740 
4741 	if (*p != '\0') {
4742 		*msg = "Invalid characters in start object ID";
4743 		rc = 1;
4744 		goto out;
4745 	}
4746 
4747 	s = strtok_r(NULL, ":", &tmp);
4748 	zor->zor_obj_end = strtoull(s, &p, 0);
4749 
4750 	if (*p != '\0') {
4751 		*msg = "Invalid characters in end object ID";
4752 		rc = 1;
4753 		goto out;
4754 	}
4755 
4756 	if (zor->zor_obj_start > zor->zor_obj_end) {
4757 		*msg = "Start object ID may not exceed end object ID";
4758 		rc = 1;
4759 		goto out;
4760 	}
4761 
4762 	s = strtok_r(NULL, ":", &tmp);
4763 	if (s == NULL) {
4764 		zor->zor_flags = ZOR_FLAG_ALL_TYPES;
4765 		goto out;
4766 	} else if (strtok_r(NULL, ":", &tmp) != NULL) {
4767 		*msg = "Invalid colon-delimited field after flags";
4768 		rc = 1;
4769 		goto out;
4770 	}
4771 
4772 	flagstr = s;
4773 	for (i = 0; flagstr[i]; i++) {
4774 		int bit;
4775 		boolean_t negation = (flagstr[i] == '-');
4776 
4777 		if (negation) {
4778 			i++;
4779 			if (flagstr[i] == '\0') {
4780 				*msg = "Invalid trailing negation operator";
4781 				rc = 1;
4782 				goto out;
4783 			}
4784 		}
4785 		bit = flagbits[(uchar_t)flagstr[i]];
4786 		if (bit == 0) {
4787 			*msg = "Invalid flag";
4788 			rc = 1;
4789 			goto out;
4790 		}
4791 		if (negation)
4792 			flags &= ~bit;
4793 		else
4794 			flags |= bit;
4795 	}
4796 	zor->zor_flags = flags;
4797 
4798 	zor->zor_obj_start = ZDB_MAP_OBJECT_ID(zor->zor_obj_start);
4799 	zor->zor_obj_end = ZDB_MAP_OBJECT_ID(zor->zor_obj_end);
4800 
4801 out:
4802 	free(dup);
4803 	return (rc);
4804 }
4805 
4806 static void
4807 dump_objset(objset_t *os)
4808 {
4809 	dmu_objset_stats_t dds = { 0 };
4810 	uint64_t object, object_count;
4811 	uint64_t refdbytes, usedobjs, scratch;
4812 	char numbuf[32];
4813 	char blkbuf[BP_SPRINTF_LEN + 20];
4814 	char osname[ZFS_MAX_DATASET_NAME_LEN];
4815 	const char *type = "UNKNOWN";
4816 	int verbosity = dump_opt['d'];
4817 	boolean_t print_header;
4818 	unsigned i;
4819 	int error;
4820 	uint64_t total_slots_used = 0;
4821 	uint64_t max_slot_used = 0;
4822 	uint64_t dnode_slots;
4823 	uint64_t obj_start;
4824 	uint64_t obj_end;
4825 	uint64_t flags;
4826 
4827 	/* make sure nicenum has enough space */
4828 	_Static_assert(sizeof (numbuf) >= NN_NUMBUF_SZ, "numbuf truncated");
4829 
4830 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
4831 	dmu_objset_fast_stat(os, &dds);
4832 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
4833 
4834 	print_header = B_TRUE;
4835 
4836 	if (dds.dds_type < DMU_OST_NUMTYPES)
4837 		type = objset_types[dds.dds_type];
4838 
4839 	if (dds.dds_type == DMU_OST_META) {
4840 		dds.dds_creation_txg = TXG_INITIAL;
4841 		usedobjs = BP_GET_FILL(os->os_rootbp);
4842 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
4843 		    dd_used_bytes;
4844 	} else {
4845 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
4846 	}
4847 
4848 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
4849 
4850 	zdb_nicenum(refdbytes, numbuf, sizeof (numbuf));
4851 
4852 	if (verbosity >= 4 || dump_opt['d']) {
4853 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
4854 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
4855 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
4856 	} else {
4857 		blkbuf[0] = '\0';
4858 	}
4859 
4860 	dmu_objset_name(os, osname);
4861 
4862 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
4863 	    "%s, %llu objects%s%s\n",
4864 	    osname, type, (u_longlong_t)dmu_objset_id(os),
4865 	    (u_longlong_t)dds.dds_creation_txg,
4866 	    numbuf, (u_longlong_t)usedobjs, blkbuf,
4867 	    (dds.dds_inconsistent) ? " (inconsistent)" : "");
4868 
4869 	for (i = 0; i < zopt_object_args; i++) {
4870 		obj_start = zopt_object_ranges[i].zor_obj_start;
4871 		obj_end = zopt_object_ranges[i].zor_obj_end;
4872 		flags = zopt_object_ranges[i].zor_flags;
4873 
4874 		object = obj_start;
4875 		if (object == 0 || obj_start == obj_end) {
4876 			dump_object(os, object, verbosity,
4877 			    &print_header, NULL, flags);
4878 		} else {
4879 			object--;
4880 		}
4881 
4882 		while ((dmu_object_next(os, &object, B_FALSE, 0) == 0) &&
4883 		    object <= obj_end) {
4884 			dump_object(os, object, verbosity,
4885 			    &print_header, NULL, flags);
4886 		}
4887 	}
4888 
4889 	if (zopt_object_args > 0) {
4890 		return;
4891 	}
4892 
4893 	if (dump_opt['i'] != 0 || verbosity >= 2)
4894 		dump_intent_log(dmu_objset_zil(os));
4895 
4896 	if (dmu_objset_ds(os) != NULL) {
4897 		dsl_dataset_t *ds = dmu_objset_ds(os);
4898 		dump_blkptr_list(&ds->ds_deadlist, "Deadlist");
4899 		if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) &&
4900 		    !dmu_objset_is_snapshot(os)) {
4901 			dump_blkptr_list(&ds->ds_dir->dd_livelist, "Livelist");
4902 			if (verify_dd_livelist(os) != 0)
4903 				fatal("livelist is incorrect");
4904 		}
4905 
4906 		if (dsl_dataset_remap_deadlist_exists(ds)) {
4907 			(void) printf("ds_remap_deadlist:\n");
4908 			dump_blkptr_list(&ds->ds_remap_deadlist, "Deadlist");
4909 		}
4910 		count_ds_mos_objects(ds);
4911 	}
4912 
4913 	if (dmu_objset_ds(os) != NULL)
4914 		dump_bookmarks(os, verbosity);
4915 
4916 	if (verbosity < 2)
4917 		return;
4918 
4919 	if (BP_IS_HOLE(os->os_rootbp))
4920 		return;
4921 
4922 	dump_object(os, 0, verbosity, &print_header, NULL, 0);
4923 	object_count = 0;
4924 	if (DMU_USERUSED_DNODE(os) != NULL &&
4925 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
4926 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header,
4927 		    NULL, 0);
4928 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header,
4929 		    NULL, 0);
4930 	}
4931 
4932 	if (DMU_PROJECTUSED_DNODE(os) != NULL &&
4933 	    DMU_PROJECTUSED_DNODE(os)->dn_type != 0)
4934 		dump_object(os, DMU_PROJECTUSED_OBJECT, verbosity,
4935 		    &print_header, NULL, 0);
4936 
4937 	object = 0;
4938 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
4939 		dump_object(os, object, verbosity, &print_header, &dnode_slots,
4940 		    0);
4941 		object_count++;
4942 		total_slots_used += dnode_slots;
4943 		max_slot_used = object + dnode_slots - 1;
4944 	}
4945 
4946 	(void) printf("\n");
4947 
4948 	(void) printf("    Dnode slots:\n");
4949 	(void) printf("\tTotal used:    %10llu\n",
4950 	    (u_longlong_t)total_slots_used);
4951 	(void) printf("\tMax used:      %10llu\n",
4952 	    (u_longlong_t)max_slot_used);
4953 	(void) printf("\tPercent empty: %10lf\n",
4954 	    (double)(max_slot_used - total_slots_used)*100 /
4955 	    (double)max_slot_used);
4956 	(void) printf("\n");
4957 
4958 	if (error != ESRCH) {
4959 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
4960 		abort();
4961 	}
4962 
4963 	ASSERT3U(object_count, ==, usedobjs);
4964 
4965 	if (leaked_objects != 0) {
4966 		(void) printf("%d potentially leaked objects detected\n",
4967 		    leaked_objects);
4968 		leaked_objects = 0;
4969 	}
4970 }
4971 
4972 static void
4973 dump_file_data_layout(objset_t *os)
4974 {
4975 	uint64_t object;
4976 	int verbosity = dump_opt['d'];
4977 	unsigned i;
4978 	uint64_t obj_start;
4979 	uint64_t obj_end;
4980 
4981 	for (i = 0; i < zopt_object_args; i++) {
4982 		obj_start = zopt_object_ranges[i].zor_obj_start;
4983 		obj_end = zopt_object_ranges[i].zor_obj_end;
4984 
4985 		object = obj_start;
4986 		if (object == 0 || obj_start == obj_end)
4987 			dump_object_file_layout(os, object, verbosity);
4988 		else
4989 			object--;
4990 
4991 		while ((dmu_object_next(os, &object, B_FALSE, 0) == 0) &&
4992 		    object <= obj_end) {
4993 			dump_object_file_layout(os, object, verbosity);
4994 		}
4995 	}
4996 }
4997 
4998 static void
4999 dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
5000 {
5001 	time_t timestamp = ub->ub_timestamp;
5002 
5003 	(void) printf("%s", header ? header : "");
5004 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
5005 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
5006 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
5007 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
5008 	(void) printf("\ttimestamp = %llu UTC = %s",
5009 	    (u_longlong_t)ub->ub_timestamp, ctime(&timestamp));
5010 
5011 	char blkbuf[BP_SPRINTF_LEN];
5012 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
5013 	(void) printf("\tbp = %s\n", blkbuf);
5014 
5015 	(void) printf("\tmmp_magic = %016llx\n",
5016 	    (u_longlong_t)ub->ub_mmp_magic);
5017 	if (MMP_VALID(ub)) {
5018 		(void) printf("\tmmp_delay = %0llu\n",
5019 		    (u_longlong_t)ub->ub_mmp_delay);
5020 		if (MMP_SEQ_VALID(ub))
5021 			(void) printf("\tmmp_seq = %u\n",
5022 			    (unsigned int) MMP_SEQ(ub));
5023 		if (MMP_FAIL_INT_VALID(ub))
5024 			(void) printf("\tmmp_fail = %u\n",
5025 			    (unsigned int) MMP_FAIL_INT(ub));
5026 		if (MMP_INTERVAL_VALID(ub))
5027 			(void) printf("\tmmp_write = %u\n",
5028 			    (unsigned int) MMP_INTERVAL(ub));
5029 		/* After MMP_* to make summarize_uberblock_mmp cleaner */
5030 		(void) printf("\tmmp_valid = %x\n",
5031 		    (unsigned int) ub->ub_mmp_config & 0xFF);
5032 	}
5033 
5034 	if (dump_opt['u'] >= 4) {
5035 		char blkbuf[BP_SPRINTF_LEN];
5036 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
5037 		(void) printf("\trootbp = %s\n", blkbuf);
5038 	}
5039 	(void) printf("\tcheckpoint_txg = %llu\n",
5040 	    (u_longlong_t)ub->ub_checkpoint_txg);
5041 
5042 	(void) printf("\traidz_reflow state=%u off=%llu\n",
5043 	    (int)RRSS_GET_STATE(ub),
5044 	    (u_longlong_t)RRSS_GET_OFFSET(ub));
5045 
5046 	(void) printf("%s", footer ? footer : "");
5047 }
5048 
5049 static void
5050 dump_config(spa_t *spa)
5051 {
5052 	dmu_buf_t *db;
5053 	size_t nvsize = 0;
5054 	int error = 0;
5055 
5056 
5057 	error = dmu_bonus_hold(spa->spa_meta_objset,
5058 	    spa->spa_config_object, FTAG, &db);
5059 
5060 	if (error == 0) {
5061 		nvsize = *(uint64_t *)db->db_data;
5062 		dmu_buf_rele(db, FTAG);
5063 
5064 		(void) printf("\nMOS Configuration:\n");
5065 		dump_packed_nvlist(spa->spa_meta_objset,
5066 		    spa->spa_config_object, (void *)&nvsize, 1);
5067 	} else {
5068 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
5069 		    (u_longlong_t)spa->spa_config_object, error);
5070 	}
5071 }
5072 
5073 static void
5074 dump_cachefile(const char *cachefile)
5075 {
5076 	int fd;
5077 	struct stat64 statbuf;
5078 	char *buf;
5079 	nvlist_t *config;
5080 
5081 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
5082 		(void) printf("cannot open '%s': %s\n", cachefile,
5083 		    strerror(errno));
5084 		zdb_exit(1);
5085 	}
5086 
5087 	if (fstat64(fd, &statbuf) != 0) {
5088 		(void) printf("failed to stat '%s': %s\n", cachefile,
5089 		    strerror(errno));
5090 		zdb_exit(1);
5091 	}
5092 
5093 	if ((buf = malloc(statbuf.st_size)) == NULL) {
5094 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
5095 		    (u_longlong_t)statbuf.st_size);
5096 		zdb_exit(1);
5097 	}
5098 
5099 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
5100 		(void) fprintf(stderr, "failed to read %llu bytes\n",
5101 		    (u_longlong_t)statbuf.st_size);
5102 		zdb_exit(1);
5103 	}
5104 
5105 	(void) close(fd);
5106 
5107 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
5108 		(void) fprintf(stderr, "failed to unpack nvlist\n");
5109 		zdb_exit(1);
5110 	}
5111 
5112 	free(buf);
5113 
5114 	dump_nvlist(config, 0);
5115 
5116 	nvlist_free(config);
5117 }
5118 
5119 /*
5120  * ZFS label nvlist stats
5121  */
5122 typedef struct zdb_nvl_stats {
5123 	int		zns_list_count;
5124 	int		zns_leaf_count;
5125 	size_t		zns_leaf_largest;
5126 	size_t		zns_leaf_total;
5127 	nvlist_t	*zns_string;
5128 	nvlist_t	*zns_uint64;
5129 	nvlist_t	*zns_boolean;
5130 } zdb_nvl_stats_t;
5131 
5132 static void
5133 collect_nvlist_stats(nvlist_t *nvl, zdb_nvl_stats_t *stats)
5134 {
5135 	nvlist_t *list, **array;
5136 	nvpair_t *nvp = NULL;
5137 	const char *name;
5138 	uint_t i, items;
5139 
5140 	stats->zns_list_count++;
5141 
5142 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5143 		name = nvpair_name(nvp);
5144 
5145 		switch (nvpair_type(nvp)) {
5146 		case DATA_TYPE_STRING:
5147 			fnvlist_add_string(stats->zns_string, name,
5148 			    fnvpair_value_string(nvp));
5149 			break;
5150 		case DATA_TYPE_UINT64:
5151 			fnvlist_add_uint64(stats->zns_uint64, name,
5152 			    fnvpair_value_uint64(nvp));
5153 			break;
5154 		case DATA_TYPE_BOOLEAN:
5155 			fnvlist_add_boolean(stats->zns_boolean, name);
5156 			break;
5157 		case DATA_TYPE_NVLIST:
5158 			if (nvpair_value_nvlist(nvp, &list) == 0)
5159 				collect_nvlist_stats(list, stats);
5160 			break;
5161 		case DATA_TYPE_NVLIST_ARRAY:
5162 			if (nvpair_value_nvlist_array(nvp, &array, &items) != 0)
5163 				break;
5164 
5165 			for (i = 0; i < items; i++) {
5166 				collect_nvlist_stats(array[i], stats);
5167 
5168 				/* collect stats on leaf vdev */
5169 				if (strcmp(name, "children") == 0) {
5170 					size_t size;
5171 
5172 					(void) nvlist_size(array[i], &size,
5173 					    NV_ENCODE_XDR);
5174 					stats->zns_leaf_total += size;
5175 					if (size > stats->zns_leaf_largest)
5176 						stats->zns_leaf_largest = size;
5177 					stats->zns_leaf_count++;
5178 				}
5179 			}
5180 			break;
5181 		default:
5182 			(void) printf("skip type %d!\n", (int)nvpair_type(nvp));
5183 		}
5184 	}
5185 }
5186 
5187 static void
5188 dump_nvlist_stats(nvlist_t *nvl, size_t cap)
5189 {
5190 	zdb_nvl_stats_t stats = { 0 };
5191 	size_t size, sum = 0, total;
5192 	size_t noise;
5193 
5194 	/* requires nvlist with non-unique names for stat collection */
5195 	VERIFY0(nvlist_alloc(&stats.zns_string, 0, 0));
5196 	VERIFY0(nvlist_alloc(&stats.zns_uint64, 0, 0));
5197 	VERIFY0(nvlist_alloc(&stats.zns_boolean, 0, 0));
5198 	VERIFY0(nvlist_size(stats.zns_boolean, &noise, NV_ENCODE_XDR));
5199 
5200 	(void) printf("\n\nZFS Label NVList Config Stats:\n");
5201 
5202 	VERIFY0(nvlist_size(nvl, &total, NV_ENCODE_XDR));
5203 	(void) printf("  %d bytes used, %d bytes free (using %4.1f%%)\n\n",
5204 	    (int)total, (int)(cap - total), 100.0 * total / cap);
5205 
5206 	collect_nvlist_stats(nvl, &stats);
5207 
5208 	VERIFY0(nvlist_size(stats.zns_uint64, &size, NV_ENCODE_XDR));
5209 	size -= noise;
5210 	sum += size;
5211 	(void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "integers:",
5212 	    (int)fnvlist_num_pairs(stats.zns_uint64),
5213 	    (int)size, 100.0 * size / total);
5214 
5215 	VERIFY0(nvlist_size(stats.zns_string, &size, NV_ENCODE_XDR));
5216 	size -= noise;
5217 	sum += size;
5218 	(void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "strings:",
5219 	    (int)fnvlist_num_pairs(stats.zns_string),
5220 	    (int)size, 100.0 * size / total);
5221 
5222 	VERIFY0(nvlist_size(stats.zns_boolean, &size, NV_ENCODE_XDR));
5223 	size -= noise;
5224 	sum += size;
5225 	(void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "booleans:",
5226 	    (int)fnvlist_num_pairs(stats.zns_boolean),
5227 	    (int)size, 100.0 * size / total);
5228 
5229 	size = total - sum;	/* treat remainder as nvlist overhead */
5230 	(void) printf("%12s %4d %6d bytes (%5.2f%%)\n\n", "nvlists:",
5231 	    stats.zns_list_count, (int)size, 100.0 * size / total);
5232 
5233 	if (stats.zns_leaf_count > 0) {
5234 		size_t average = stats.zns_leaf_total / stats.zns_leaf_count;
5235 
5236 		(void) printf("%12s %4d %6d bytes average\n", "leaf vdevs:",
5237 		    stats.zns_leaf_count, (int)average);
5238 		(void) printf("%24d bytes largest\n",
5239 		    (int)stats.zns_leaf_largest);
5240 
5241 		if (dump_opt['l'] >= 3 && average > 0)
5242 			(void) printf("  space for %d additional leaf vdevs\n",
5243 			    (int)((cap - total) / average));
5244 	}
5245 	(void) printf("\n");
5246 
5247 	nvlist_free(stats.zns_string);
5248 	nvlist_free(stats.zns_uint64);
5249 	nvlist_free(stats.zns_boolean);
5250 }
5251 
5252 typedef struct cksum_record {
5253 	zio_cksum_t cksum;
5254 	boolean_t labels[VDEV_LABELS];
5255 	avl_node_t link;
5256 } cksum_record_t;
5257 
5258 static int
5259 cksum_record_compare(const void *x1, const void *x2)
5260 {
5261 	const cksum_record_t *l = (cksum_record_t *)x1;
5262 	const cksum_record_t *r = (cksum_record_t *)x2;
5263 	int arraysize = ARRAY_SIZE(l->cksum.zc_word);
5264 	int difference = 0;
5265 
5266 	for (int i = 0; i < arraysize; i++) {
5267 		difference = TREE_CMP(l->cksum.zc_word[i], r->cksum.zc_word[i]);
5268 		if (difference)
5269 			break;
5270 	}
5271 
5272 	return (difference);
5273 }
5274 
5275 static cksum_record_t *
5276 cksum_record_alloc(zio_cksum_t *cksum, int l)
5277 {
5278 	cksum_record_t *rec;
5279 
5280 	rec = umem_zalloc(sizeof (*rec), UMEM_NOFAIL);
5281 	rec->cksum = *cksum;
5282 	rec->labels[l] = B_TRUE;
5283 
5284 	return (rec);
5285 }
5286 
5287 static cksum_record_t *
5288 cksum_record_lookup(avl_tree_t *tree, zio_cksum_t *cksum)
5289 {
5290 	cksum_record_t lookup = { .cksum = *cksum };
5291 	avl_index_t where;
5292 
5293 	return (avl_find(tree, &lookup, &where));
5294 }
5295 
5296 static cksum_record_t *
5297 cksum_record_insert(avl_tree_t *tree, zio_cksum_t *cksum, int l)
5298 {
5299 	cksum_record_t *rec;
5300 
5301 	rec = cksum_record_lookup(tree, cksum);
5302 	if (rec) {
5303 		rec->labels[l] = B_TRUE;
5304 	} else {
5305 		rec = cksum_record_alloc(cksum, l);
5306 		avl_add(tree, rec);
5307 	}
5308 
5309 	return (rec);
5310 }
5311 
5312 static int
5313 first_label(cksum_record_t *rec)
5314 {
5315 	for (int i = 0; i < VDEV_LABELS; i++)
5316 		if (rec->labels[i])
5317 			return (i);
5318 
5319 	return (-1);
5320 }
5321 
5322 static void
5323 print_label_numbers(const char *prefix, const cksum_record_t *rec)
5324 {
5325 	fputs(prefix, stdout);
5326 	for (int i = 0; i < VDEV_LABELS; i++)
5327 		if (rec->labels[i] == B_TRUE)
5328 			printf("%d ", i);
5329 	putchar('\n');
5330 }
5331 
5332 #define	MAX_UBERBLOCK_COUNT (VDEV_UBERBLOCK_RING >> UBERBLOCK_SHIFT)
5333 
5334 typedef struct zdb_label {
5335 	vdev_label_t label;
5336 	uint64_t label_offset;
5337 	nvlist_t *config_nv;
5338 	cksum_record_t *config;
5339 	cksum_record_t *uberblocks[MAX_UBERBLOCK_COUNT];
5340 	boolean_t header_printed;
5341 	boolean_t read_failed;
5342 	boolean_t cksum_valid;
5343 } zdb_label_t;
5344 
5345 static void
5346 print_label_header(zdb_label_t *label, int l)
5347 {
5348 
5349 	if (dump_opt['q'])
5350 		return;
5351 
5352 	if (label->header_printed == B_TRUE)
5353 		return;
5354 
5355 	(void) printf("------------------------------------\n");
5356 	(void) printf("LABEL %d %s\n", l,
5357 	    label->cksum_valid ? "" : "(Bad label cksum)");
5358 	(void) printf("------------------------------------\n");
5359 
5360 	label->header_printed = B_TRUE;
5361 }
5362 
5363 static void
5364 print_l2arc_header(void)
5365 {
5366 	(void) printf("------------------------------------\n");
5367 	(void) printf("L2ARC device header\n");
5368 	(void) printf("------------------------------------\n");
5369 }
5370 
5371 static void
5372 print_l2arc_log_blocks(void)
5373 {
5374 	(void) printf("------------------------------------\n");
5375 	(void) printf("L2ARC device log blocks\n");
5376 	(void) printf("------------------------------------\n");
5377 }
5378 
5379 static void
5380 dump_l2arc_log_entries(uint64_t log_entries,
5381     l2arc_log_ent_phys_t *le, uint64_t i)
5382 {
5383 	for (int j = 0; j < log_entries; j++) {
5384 		dva_t dva = le[j].le_dva;
5385 		(void) printf("lb[%4llu]\tle[%4d]\tDVA asize: %llu, "
5386 		    "vdev: %llu, offset: %llu\n",
5387 		    (u_longlong_t)i, j + 1,
5388 		    (u_longlong_t)DVA_GET_ASIZE(&dva),
5389 		    (u_longlong_t)DVA_GET_VDEV(&dva),
5390 		    (u_longlong_t)DVA_GET_OFFSET(&dva));
5391 		(void) printf("|\t\t\t\tbirth: %llu\n",
5392 		    (u_longlong_t)le[j].le_birth);
5393 		(void) printf("|\t\t\t\tlsize: %llu\n",
5394 		    (u_longlong_t)L2BLK_GET_LSIZE((&le[j])->le_prop));
5395 		(void) printf("|\t\t\t\tpsize: %llu\n",
5396 		    (u_longlong_t)L2BLK_GET_PSIZE((&le[j])->le_prop));
5397 		(void) printf("|\t\t\t\tcompr: %llu\n",
5398 		    (u_longlong_t)L2BLK_GET_COMPRESS((&le[j])->le_prop));
5399 		(void) printf("|\t\t\t\tcomplevel: %llu\n",
5400 		    (u_longlong_t)(&le[j])->le_complevel);
5401 		(void) printf("|\t\t\t\ttype: %llu\n",
5402 		    (u_longlong_t)L2BLK_GET_TYPE((&le[j])->le_prop));
5403 		(void) printf("|\t\t\t\tprotected: %llu\n",
5404 		    (u_longlong_t)L2BLK_GET_PROTECTED((&le[j])->le_prop));
5405 		(void) printf("|\t\t\t\tprefetch: %llu\n",
5406 		    (u_longlong_t)L2BLK_GET_PREFETCH((&le[j])->le_prop));
5407 		(void) printf("|\t\t\t\taddress: %llu\n",
5408 		    (u_longlong_t)le[j].le_daddr);
5409 		(void) printf("|\t\t\t\tARC state: %llu\n",
5410 		    (u_longlong_t)L2BLK_GET_STATE((&le[j])->le_prop));
5411 		(void) printf("|\n");
5412 	}
5413 	(void) printf("\n");
5414 }
5415 
5416 static void
5417 dump_l2arc_log_blkptr(const l2arc_log_blkptr_t *lbps)
5418 {
5419 	(void) printf("|\t\tdaddr: %llu\n", (u_longlong_t)lbps->lbp_daddr);
5420 	(void) printf("|\t\tpayload_asize: %llu\n",
5421 	    (u_longlong_t)lbps->lbp_payload_asize);
5422 	(void) printf("|\t\tpayload_start: %llu\n",
5423 	    (u_longlong_t)lbps->lbp_payload_start);
5424 	(void) printf("|\t\tlsize: %llu\n",
5425 	    (u_longlong_t)L2BLK_GET_LSIZE(lbps->lbp_prop));
5426 	(void) printf("|\t\tasize: %llu\n",
5427 	    (u_longlong_t)L2BLK_GET_PSIZE(lbps->lbp_prop));
5428 	(void) printf("|\t\tcompralgo: %llu\n",
5429 	    (u_longlong_t)L2BLK_GET_COMPRESS(lbps->lbp_prop));
5430 	(void) printf("|\t\tcksumalgo: %llu\n",
5431 	    (u_longlong_t)L2BLK_GET_CHECKSUM(lbps->lbp_prop));
5432 	(void) printf("|\n\n");
5433 }
5434 
5435 static void
5436 dump_l2arc_log_blocks(int fd, const l2arc_dev_hdr_phys_t *l2dhdr,
5437     l2arc_dev_hdr_phys_t *rebuild)
5438 {
5439 	l2arc_log_blk_phys_t this_lb;
5440 	uint64_t asize;
5441 	l2arc_log_blkptr_t lbps[2];
5442 	zio_cksum_t cksum;
5443 	int failed = 0;
5444 	l2arc_dev_t dev;
5445 
5446 	if (!dump_opt['q'])
5447 		print_l2arc_log_blocks();
5448 	memcpy(lbps, l2dhdr->dh_start_lbps, sizeof (lbps));
5449 
5450 	dev.l2ad_evict = l2dhdr->dh_evict;
5451 	dev.l2ad_start = l2dhdr->dh_start;
5452 	dev.l2ad_end = l2dhdr->dh_end;
5453 
5454 	if (l2dhdr->dh_start_lbps[0].lbp_daddr == 0) {
5455 		/* no log blocks to read */
5456 		if (!dump_opt['q']) {
5457 			(void) printf("No log blocks to read\n");
5458 			(void) printf("\n");
5459 		}
5460 		return;
5461 	} else {
5462 		dev.l2ad_hand = lbps[0].lbp_daddr +
5463 		    L2BLK_GET_PSIZE((&lbps[0])->lbp_prop);
5464 	}
5465 
5466 	dev.l2ad_first = !!(l2dhdr->dh_flags & L2ARC_DEV_HDR_EVICT_FIRST);
5467 
5468 	for (;;) {
5469 		if (!l2arc_log_blkptr_valid(&dev, &lbps[0]))
5470 			break;
5471 
5472 		/* L2BLK_GET_PSIZE returns aligned size for log blocks */
5473 		asize = L2BLK_GET_PSIZE((&lbps[0])->lbp_prop);
5474 		if (pread64(fd, &this_lb, asize, lbps[0].lbp_daddr) != asize) {
5475 			if (!dump_opt['q']) {
5476 				(void) printf("Error while reading next log "
5477 				    "block\n\n");
5478 			}
5479 			break;
5480 		}
5481 
5482 		fletcher_4_native_varsize(&this_lb, asize, &cksum);
5483 		if (!ZIO_CHECKSUM_EQUAL(cksum, lbps[0].lbp_cksum)) {
5484 			failed++;
5485 			if (!dump_opt['q']) {
5486 				(void) printf("Invalid cksum\n");
5487 				dump_l2arc_log_blkptr(&lbps[0]);
5488 			}
5489 			break;
5490 		}
5491 
5492 		switch (L2BLK_GET_COMPRESS((&lbps[0])->lbp_prop)) {
5493 		case ZIO_COMPRESS_OFF:
5494 			break;
5495 		default: {
5496 			abd_t *abd = abd_alloc_linear(asize, B_TRUE);
5497 			abd_copy_from_buf_off(abd, &this_lb, 0, asize);
5498 			abd_t dabd;
5499 			abd_get_from_buf_struct(&dabd, &this_lb,
5500 			    sizeof (this_lb));
5501 			int err = zio_decompress_data(L2BLK_GET_COMPRESS(
5502 			    (&lbps[0])->lbp_prop), abd, &dabd,
5503 			    asize, sizeof (this_lb), NULL);
5504 			abd_free(&dabd);
5505 			abd_free(abd);
5506 			if (err != 0) {
5507 				(void) printf("L2ARC block decompression "
5508 				    "failed\n");
5509 				goto out;
5510 			}
5511 			break;
5512 		}
5513 		}
5514 
5515 		if (this_lb.lb_magic == BSWAP_64(L2ARC_LOG_BLK_MAGIC))
5516 			byteswap_uint64_array(&this_lb, sizeof (this_lb));
5517 		if (this_lb.lb_magic != L2ARC_LOG_BLK_MAGIC) {
5518 			if (!dump_opt['q'])
5519 				(void) printf("Invalid log block magic\n\n");
5520 			break;
5521 		}
5522 
5523 		rebuild->dh_lb_count++;
5524 		rebuild->dh_lb_asize += asize;
5525 		if (dump_opt['l'] > 1 && !dump_opt['q']) {
5526 			(void) printf("lb[%4llu]\tmagic: %llu\n",
5527 			    (u_longlong_t)rebuild->dh_lb_count,
5528 			    (u_longlong_t)this_lb.lb_magic);
5529 			dump_l2arc_log_blkptr(&lbps[0]);
5530 		}
5531 
5532 		if (dump_opt['l'] > 2 && !dump_opt['q'])
5533 			dump_l2arc_log_entries(l2dhdr->dh_log_entries,
5534 			    this_lb.lb_entries,
5535 			    rebuild->dh_lb_count);
5536 
5537 		if (l2arc_range_check_overlap(lbps[1].lbp_payload_start,
5538 		    lbps[0].lbp_payload_start, dev.l2ad_evict) &&
5539 		    !dev.l2ad_first)
5540 			break;
5541 
5542 		lbps[0] = lbps[1];
5543 		lbps[1] = this_lb.lb_prev_lbp;
5544 	}
5545 out:
5546 	if (!dump_opt['q']) {
5547 		(void) printf("log_blk_count:\t %llu with valid cksum\n",
5548 		    (u_longlong_t)rebuild->dh_lb_count);
5549 		(void) printf("\t\t %d with invalid cksum\n", failed);
5550 		(void) printf("log_blk_asize:\t %llu\n\n",
5551 		    (u_longlong_t)rebuild->dh_lb_asize);
5552 	}
5553 }
5554 
5555 static int
5556 dump_l2arc_header(int fd)
5557 {
5558 	l2arc_dev_hdr_phys_t l2dhdr = {0}, rebuild = {0};
5559 	int error = B_FALSE;
5560 
5561 	if (pread64(fd, &l2dhdr, sizeof (l2dhdr),
5562 	    VDEV_LABEL_START_SIZE) != sizeof (l2dhdr)) {
5563 		error = B_TRUE;
5564 	} else {
5565 		if (l2dhdr.dh_magic == BSWAP_64(L2ARC_DEV_HDR_MAGIC))
5566 			byteswap_uint64_array(&l2dhdr, sizeof (l2dhdr));
5567 
5568 		if (l2dhdr.dh_magic != L2ARC_DEV_HDR_MAGIC)
5569 			error = B_TRUE;
5570 	}
5571 
5572 	if (error) {
5573 		(void) printf("L2ARC device header not found\n\n");
5574 		/* Do not return an error here for backward compatibility */
5575 		return (0);
5576 	} else if (!dump_opt['q']) {
5577 		print_l2arc_header();
5578 
5579 		(void) printf("    magic: %llu\n",
5580 		    (u_longlong_t)l2dhdr.dh_magic);
5581 		(void) printf("    version: %llu\n",
5582 		    (u_longlong_t)l2dhdr.dh_version);
5583 		(void) printf("    pool_guid: %llu\n",
5584 		    (u_longlong_t)l2dhdr.dh_spa_guid);
5585 		(void) printf("    flags: %llu\n",
5586 		    (u_longlong_t)l2dhdr.dh_flags);
5587 		(void) printf("    start_lbps[0]: %llu\n",
5588 		    (u_longlong_t)
5589 		    l2dhdr.dh_start_lbps[0].lbp_daddr);
5590 		(void) printf("    start_lbps[1]: %llu\n",
5591 		    (u_longlong_t)
5592 		    l2dhdr.dh_start_lbps[1].lbp_daddr);
5593 		(void) printf("    log_blk_ent: %llu\n",
5594 		    (u_longlong_t)l2dhdr.dh_log_entries);
5595 		(void) printf("    start: %llu\n",
5596 		    (u_longlong_t)l2dhdr.dh_start);
5597 		(void) printf("    end: %llu\n",
5598 		    (u_longlong_t)l2dhdr.dh_end);
5599 		(void) printf("    evict: %llu\n",
5600 		    (u_longlong_t)l2dhdr.dh_evict);
5601 		(void) printf("    lb_asize_refcount: %llu\n",
5602 		    (u_longlong_t)l2dhdr.dh_lb_asize);
5603 		(void) printf("    lb_count_refcount: %llu\n",
5604 		    (u_longlong_t)l2dhdr.dh_lb_count);
5605 		(void) printf("    trim_action_time: %llu\n",
5606 		    (u_longlong_t)l2dhdr.dh_trim_action_time);
5607 		(void) printf("    trim_state: %llu\n\n",
5608 		    (u_longlong_t)l2dhdr.dh_trim_state);
5609 	}
5610 
5611 	dump_l2arc_log_blocks(fd, &l2dhdr, &rebuild);
5612 	/*
5613 	 * The total aligned size of log blocks and the number of log blocks
5614 	 * reported in the header of the device may be less than what zdb
5615 	 * reports by dump_l2arc_log_blocks() which emulates l2arc_rebuild().
5616 	 * This happens because dump_l2arc_log_blocks() lacks the memory
5617 	 * pressure valve that l2arc_rebuild() has. Thus, if we are on a system
5618 	 * with low memory, l2arc_rebuild will exit prematurely and dh_lb_asize
5619 	 * and dh_lb_count will be lower to begin with than what exists on the
5620 	 * device. This is normal and zdb should not exit with an error. The
5621 	 * opposite case should never happen though, the values reported in the
5622 	 * header should never be higher than what dump_l2arc_log_blocks() and
5623 	 * l2arc_rebuild() report. If this happens there is a leak in the
5624 	 * accounting of log blocks.
5625 	 */
5626 	if (l2dhdr.dh_lb_asize > rebuild.dh_lb_asize ||
5627 	    l2dhdr.dh_lb_count > rebuild.dh_lb_count)
5628 		return (1);
5629 
5630 	return (0);
5631 }
5632 
5633 static void
5634 dump_config_from_label(zdb_label_t *label, size_t buflen, int l)
5635 {
5636 	if (dump_opt['q'])
5637 		return;
5638 
5639 	if ((dump_opt['l'] < 3) && (first_label(label->config) != l))
5640 		return;
5641 
5642 	print_label_header(label, l);
5643 	dump_nvlist(label->config_nv, 4);
5644 	print_label_numbers("    labels = ", label->config);
5645 
5646 	if (dump_opt['l'] >= 2)
5647 		dump_nvlist_stats(label->config_nv, buflen);
5648 }
5649 
5650 #define	ZDB_MAX_UB_HEADER_SIZE 32
5651 
5652 static void
5653 dump_label_uberblocks(zdb_label_t *label, uint64_t ashift, int label_num)
5654 {
5655 
5656 	vdev_t vd;
5657 	char header[ZDB_MAX_UB_HEADER_SIZE];
5658 
5659 	vd.vdev_ashift = ashift;
5660 	vd.vdev_top = &vd;
5661 
5662 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) {
5663 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i);
5664 		uberblock_t *ub = (void *)((char *)&label->label + uoff);
5665 		cksum_record_t *rec = label->uberblocks[i];
5666 
5667 		if (rec == NULL) {
5668 			if (dump_opt['u'] >= 2) {
5669 				print_label_header(label, label_num);
5670 				(void) printf("    Uberblock[%d] invalid\n", i);
5671 			}
5672 			continue;
5673 		}
5674 
5675 		if ((dump_opt['u'] < 3) && (first_label(rec) != label_num))
5676 			continue;
5677 
5678 		if ((dump_opt['u'] < 4) &&
5679 		    (ub->ub_mmp_magic == MMP_MAGIC) && ub->ub_mmp_delay &&
5680 		    (i >= VDEV_UBERBLOCK_COUNT(&vd) - MMP_BLOCKS_PER_LABEL))
5681 			continue;
5682 
5683 		print_label_header(label, label_num);
5684 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
5685 		    "    Uberblock[%d]\n", i);
5686 		dump_uberblock(ub, header, "");
5687 		print_label_numbers("        labels = ", rec);
5688 	}
5689 }
5690 
5691 static char curpath[PATH_MAX];
5692 
5693 /*
5694  * Iterate through the path components, recursively passing
5695  * current one's obj and remaining path until we find the obj
5696  * for the last one.
5697  */
5698 static int
5699 dump_path_impl(objset_t *os, uint64_t obj, char *name, uint64_t *retobj)
5700 {
5701 	int err;
5702 	boolean_t header = B_TRUE;
5703 	uint64_t child_obj;
5704 	char *s;
5705 	dmu_buf_t *db;
5706 	dmu_object_info_t doi;
5707 
5708 	if ((s = strchr(name, '/')) != NULL)
5709 		*s = '\0';
5710 	err = zap_lookup(os, obj, name, 8, 1, &child_obj);
5711 
5712 	(void) strlcat(curpath, name, sizeof (curpath));
5713 
5714 	if (err != 0) {
5715 		(void) fprintf(stderr, "failed to lookup %s: %s\n",
5716 		    curpath, strerror(err));
5717 		return (err);
5718 	}
5719 
5720 	child_obj = ZFS_DIRENT_OBJ(child_obj);
5721 	err = sa_buf_hold(os, child_obj, FTAG, &db);
5722 	if (err != 0) {
5723 		(void) fprintf(stderr,
5724 		    "failed to get SA dbuf for obj %llu: %s\n",
5725 		    (u_longlong_t)child_obj, strerror(err));
5726 		return (EINVAL);
5727 	}
5728 	dmu_object_info_from_db(db, &doi);
5729 	sa_buf_rele(db, FTAG);
5730 
5731 	if (doi.doi_bonus_type != DMU_OT_SA &&
5732 	    doi.doi_bonus_type != DMU_OT_ZNODE) {
5733 		(void) fprintf(stderr, "invalid bonus type %d for obj %llu\n",
5734 		    doi.doi_bonus_type, (u_longlong_t)child_obj);
5735 		return (EINVAL);
5736 	}
5737 
5738 	if (dump_opt['v'] > 6) {
5739 		(void) printf("obj=%llu %s type=%d bonustype=%d\n",
5740 		    (u_longlong_t)child_obj, curpath, doi.doi_type,
5741 		    doi.doi_bonus_type);
5742 	}
5743 
5744 	(void) strlcat(curpath, "/", sizeof (curpath));
5745 
5746 	switch (doi.doi_type) {
5747 	case DMU_OT_DIRECTORY_CONTENTS:
5748 		if (s != NULL && *(s + 1) != '\0')
5749 			return (dump_path_impl(os, child_obj, s + 1, retobj));
5750 		zfs_fallthrough;
5751 	case DMU_OT_PLAIN_FILE_CONTENTS:
5752 		if (retobj != NULL) {
5753 			*retobj = child_obj;
5754 		} else {
5755 			dump_object(os, child_obj, dump_opt['v'], &header,
5756 			    NULL, 0);
5757 		}
5758 		return (0);
5759 	default:
5760 		(void) fprintf(stderr, "object %llu has non-file/directory "
5761 		    "type %d\n", (u_longlong_t)obj, doi.doi_type);
5762 		break;
5763 	}
5764 
5765 	return (EINVAL);
5766 }
5767 
5768 /*
5769  * Dump the blocks for the object specified by path inside the dataset.
5770  */
5771 static int
5772 dump_path(char *ds, char *path, uint64_t *retobj)
5773 {
5774 	int err;
5775 	objset_t *os;
5776 	uint64_t root_obj;
5777 
5778 	err = open_objset(ds, FTAG, &os);
5779 	if (err != 0)
5780 		return (err);
5781 
5782 	err = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &root_obj);
5783 	if (err != 0) {
5784 		(void) fprintf(stderr, "can't lookup root znode: %s\n",
5785 		    strerror(err));
5786 		close_objset(os, FTAG);
5787 		return (EINVAL);
5788 	}
5789 
5790 	(void) snprintf(curpath, sizeof (curpath), "dataset=%s path=/", ds);
5791 
5792 	err = dump_path_impl(os, root_obj, path, retobj);
5793 
5794 	close_objset(os, FTAG);
5795 	return (err);
5796 }
5797 
5798 static int
5799 dump_backup_bytes(objset_t *os, void *buf, int len, void *arg)
5800 {
5801 	const char *p = (const char *)buf;
5802 	ssize_t nwritten;
5803 
5804 	(void) os;
5805 	(void) arg;
5806 
5807 	/* Write the data out, handling short writes and signals. */
5808 	while ((nwritten = write(STDOUT_FILENO, p, len)) < len) {
5809 		if (nwritten < 0) {
5810 			if (errno == EINTR)
5811 				continue;
5812 			return (errno);
5813 		}
5814 		p += nwritten;
5815 		len -= nwritten;
5816 	}
5817 
5818 	return (0);
5819 }
5820 
5821 static void
5822 dump_backup(const char *pool, uint64_t objset_id, const char *flagstr)
5823 {
5824 	boolean_t embed = B_FALSE;
5825 	boolean_t large_block = B_FALSE;
5826 	boolean_t compress = B_FALSE;
5827 	boolean_t raw = B_FALSE;
5828 
5829 	const char *c;
5830 	for (c = flagstr; c != NULL && *c != '\0'; c++) {
5831 		switch (*c) {
5832 			case 'e':
5833 				embed = B_TRUE;
5834 				break;
5835 			case 'L':
5836 				large_block = B_TRUE;
5837 				break;
5838 			case 'c':
5839 				compress = B_TRUE;
5840 				break;
5841 			case 'w':
5842 				raw = B_TRUE;
5843 				break;
5844 			default:
5845 				fprintf(stderr, "dump_backup: invalid flag "
5846 				    "'%c'\n", *c);
5847 				return;
5848 		}
5849 	}
5850 
5851 	if (isatty(STDOUT_FILENO)) {
5852 		fprintf(stderr, "dump_backup: stream cannot be written "
5853 		    "to a terminal\n");
5854 		return;
5855 	}
5856 
5857 	offset_t off = 0;
5858 	dmu_send_outparams_t out = {
5859 	    .dso_outfunc = dump_backup_bytes,
5860 	    .dso_dryrun  = B_FALSE,
5861 	};
5862 
5863 	int err = dmu_send_obj(pool, objset_id, /* fromsnap */0, embed,
5864 	    large_block, compress, raw, /* saved */ B_FALSE, STDOUT_FILENO,
5865 	    &off, &out);
5866 	if (err != 0) {
5867 		fprintf(stderr, "dump_backup: dmu_send_obj: %s\n",
5868 		    strerror(err));
5869 		return;
5870 	}
5871 }
5872 
5873 static int
5874 zdb_copy_object(objset_t *os, uint64_t srcobj, char *destfile)
5875 {
5876 	int err = 0;
5877 	uint64_t size, readsize, oursize, offset;
5878 	ssize_t writesize;
5879 	sa_handle_t *hdl;
5880 
5881 	(void) printf("Copying object %" PRIu64 " to file %s\n", srcobj,
5882 	    destfile);
5883 
5884 	VERIFY3P(os, ==, sa_os);
5885 	if ((err = sa_handle_get(os, srcobj, NULL, SA_HDL_PRIVATE, &hdl))) {
5886 		(void) printf("Failed to get handle for SA znode\n");
5887 		return (err);
5888 	}
5889 	if ((err = sa_lookup(hdl, sa_attr_table[ZPL_SIZE], &size, 8))) {
5890 		(void) sa_handle_destroy(hdl);
5891 		return (err);
5892 	}
5893 	(void) sa_handle_destroy(hdl);
5894 
5895 	(void) printf("Object %" PRIu64 " is %" PRIu64 " bytes\n", srcobj,
5896 	    size);
5897 	if (size == 0) {
5898 		return (EINVAL);
5899 	}
5900 
5901 	int fd = open(destfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5902 	if (fd == -1)
5903 		return (errno);
5904 	/*
5905 	 * We cap the size at 1 mebibyte here to prevent
5906 	 * allocation failures and nigh-infinite printing if the
5907 	 * object is extremely large.
5908 	 */
5909 	oursize = MIN(size, 1 << 20);
5910 	offset = 0;
5911 	char *buf = kmem_alloc(oursize, KM_NOSLEEP);
5912 	if (buf == NULL) {
5913 		(void) close(fd);
5914 		return (ENOMEM);
5915 	}
5916 
5917 	while (offset < size) {
5918 		readsize = MIN(size - offset, 1 << 20);
5919 		err = dmu_read(os, srcobj, offset, readsize, buf, 0);
5920 		if (err != 0) {
5921 			(void) printf("got error %u from dmu_read\n", err);
5922 			kmem_free(buf, oursize);
5923 			(void) close(fd);
5924 			return (err);
5925 		}
5926 		if (dump_opt['v'] > 3) {
5927 			(void) printf("Read offset=%" PRIu64 " size=%" PRIu64
5928 			    " error=%d\n", offset, readsize, err);
5929 		}
5930 
5931 		writesize = write(fd, buf, readsize);
5932 		if (writesize < 0) {
5933 			err = errno;
5934 			break;
5935 		} else if (writesize != readsize) {
5936 			/* Incomplete write */
5937 			(void) fprintf(stderr, "Short write, only wrote %llu of"
5938 			    " %" PRIu64 " bytes, exiting...\n",
5939 			    (u_longlong_t)writesize, readsize);
5940 			break;
5941 		}
5942 
5943 		offset += readsize;
5944 	}
5945 
5946 	(void) close(fd);
5947 
5948 	if (buf != NULL)
5949 		kmem_free(buf, oursize);
5950 
5951 	return (err);
5952 }
5953 
5954 static boolean_t
5955 label_cksum_valid(vdev_label_t *label, uint64_t offset)
5956 {
5957 	zio_checksum_info_t *ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL];
5958 	zio_cksum_t expected_cksum;
5959 	zio_cksum_t actual_cksum;
5960 	zio_cksum_t verifier;
5961 	zio_eck_t *eck;
5962 	int byteswap;
5963 
5964 	void *data = (char *)label + offsetof(vdev_label_t, vl_vdev_phys);
5965 	eck = (zio_eck_t *)((char *)(data) + VDEV_PHYS_SIZE) - 1;
5966 
5967 	offset += offsetof(vdev_label_t, vl_vdev_phys);
5968 	ZIO_SET_CHECKSUM(&verifier, offset, 0, 0, 0);
5969 
5970 	byteswap = (eck->zec_magic == BSWAP_64(ZEC_MAGIC));
5971 	if (byteswap)
5972 		byteswap_uint64_array(&verifier, sizeof (zio_cksum_t));
5973 
5974 	expected_cksum = eck->zec_cksum;
5975 	eck->zec_cksum = verifier;
5976 
5977 	abd_t *abd = abd_get_from_buf(data, VDEV_PHYS_SIZE);
5978 	ci->ci_func[byteswap](abd, VDEV_PHYS_SIZE, NULL, &actual_cksum);
5979 	abd_free(abd);
5980 
5981 	if (byteswap)
5982 		byteswap_uint64_array(&expected_cksum, sizeof (zio_cksum_t));
5983 
5984 	if (ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum))
5985 		return (B_TRUE);
5986 
5987 	return (B_FALSE);
5988 }
5989 
5990 static int
5991 dump_label(const char *dev)
5992 {
5993 	char path[MAXPATHLEN];
5994 	zdb_label_t labels[VDEV_LABELS] = {{{{0}}}};
5995 	uint64_t psize, ashift, l2cache;
5996 	struct stat64 statbuf;
5997 	boolean_t config_found = B_FALSE;
5998 	boolean_t error = B_FALSE;
5999 	boolean_t read_l2arc_header = B_FALSE;
6000 	avl_tree_t config_tree;
6001 	avl_tree_t uberblock_tree;
6002 	void *node, *cookie;
6003 	int fd;
6004 
6005 	/*
6006 	 * Check if we were given absolute path and use it as is.
6007 	 * Otherwise if the provided vdev name doesn't point to a file,
6008 	 * try prepending expected disk paths and partition numbers.
6009 	 */
6010 	(void) strlcpy(path, dev, sizeof (path));
6011 	if (dev[0] != '/' && stat64(path, &statbuf) != 0) {
6012 		int error;
6013 
6014 		error = zfs_resolve_shortname(dev, path, MAXPATHLEN);
6015 		if (error == 0 && zfs_dev_is_whole_disk(path)) {
6016 			if (zfs_append_partition(path, MAXPATHLEN) == -1)
6017 				error = ENOENT;
6018 		}
6019 
6020 		if (error || (stat64(path, &statbuf) != 0)) {
6021 			(void) printf("failed to find device %s, try "
6022 			    "specifying absolute path instead\n", dev);
6023 			return (1);
6024 		}
6025 	}
6026 
6027 	if ((fd = open64(path, O_RDONLY)) < 0) {
6028 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
6029 		zdb_exit(1);
6030 	}
6031 
6032 	if (fstat64_blk(fd, &statbuf) != 0) {
6033 		(void) printf("failed to stat '%s': %s\n", path,
6034 		    strerror(errno));
6035 		(void) close(fd);
6036 		zdb_exit(1);
6037 	}
6038 
6039 	if (S_ISBLK(statbuf.st_mode) && zfs_dev_flush(fd) != 0)
6040 		(void) printf("failed to invalidate cache '%s' : %s\n", path,
6041 		    strerror(errno));
6042 
6043 	avl_create(&config_tree, cksum_record_compare,
6044 	    sizeof (cksum_record_t), offsetof(cksum_record_t, link));
6045 	avl_create(&uberblock_tree, cksum_record_compare,
6046 	    sizeof (cksum_record_t), offsetof(cksum_record_t, link));
6047 
6048 	psize = statbuf.st_size;
6049 	psize = P2ALIGN_TYPED(psize, sizeof (vdev_label_t), uint64_t);
6050 	ashift = SPA_MINBLOCKSHIFT;
6051 
6052 	/*
6053 	 * 1. Read the label from disk
6054 	 * 2. Verify label cksum
6055 	 * 3. Unpack the configuration and insert in config tree.
6056 	 * 4. Traverse all uberblocks and insert in uberblock tree.
6057 	 */
6058 	for (int l = 0; l < VDEV_LABELS; l++) {
6059 		zdb_label_t *label = &labels[l];
6060 		char *buf = label->label.vl_vdev_phys.vp_nvlist;
6061 		size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist);
6062 		nvlist_t *config;
6063 		cksum_record_t *rec;
6064 		zio_cksum_t cksum;
6065 		vdev_t vd;
6066 
6067 		label->label_offset = vdev_label_offset(psize, l, 0);
6068 
6069 		if (pread64(fd, &label->label, sizeof (label->label),
6070 		    label->label_offset) != sizeof (label->label)) {
6071 			if (!dump_opt['q'])
6072 				(void) printf("failed to read label %d\n", l);
6073 			label->read_failed = B_TRUE;
6074 			error = B_TRUE;
6075 			continue;
6076 		}
6077 
6078 		label->read_failed = B_FALSE;
6079 		label->cksum_valid = label_cksum_valid(&label->label,
6080 		    label->label_offset);
6081 
6082 		if (nvlist_unpack(buf, buflen, &config, 0) == 0) {
6083 			nvlist_t *vdev_tree = NULL;
6084 			size_t size;
6085 
6086 			if ((nvlist_lookup_nvlist(config,
6087 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
6088 			    (nvlist_lookup_uint64(vdev_tree,
6089 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
6090 				ashift = SPA_MINBLOCKSHIFT;
6091 
6092 			if (nvlist_size(config, &size, NV_ENCODE_XDR) != 0)
6093 				size = buflen;
6094 
6095 			/* If the device is a cache device read the header. */
6096 			if (!read_l2arc_header) {
6097 				if (nvlist_lookup_uint64(config,
6098 				    ZPOOL_CONFIG_POOL_STATE, &l2cache) == 0 &&
6099 				    l2cache == POOL_STATE_L2CACHE) {
6100 					read_l2arc_header = B_TRUE;
6101 				}
6102 			}
6103 
6104 			fletcher_4_native_varsize(buf, size, &cksum);
6105 			rec = cksum_record_insert(&config_tree, &cksum, l);
6106 
6107 			label->config = rec;
6108 			label->config_nv = config;
6109 			config_found = B_TRUE;
6110 		} else {
6111 			error = B_TRUE;
6112 		}
6113 
6114 		vd.vdev_ashift = ashift;
6115 		vd.vdev_top = &vd;
6116 
6117 		for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) {
6118 			uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i);
6119 			uberblock_t *ub = (void *)((char *)label + uoff);
6120 
6121 			if (uberblock_verify(ub))
6122 				continue;
6123 
6124 			fletcher_4_native_varsize(ub, sizeof (*ub), &cksum);
6125 			rec = cksum_record_insert(&uberblock_tree, &cksum, l);
6126 
6127 			label->uberblocks[i] = rec;
6128 		}
6129 	}
6130 
6131 	/*
6132 	 * Dump the label and uberblocks.
6133 	 */
6134 	for (int l = 0; l < VDEV_LABELS; l++) {
6135 		zdb_label_t *label = &labels[l];
6136 		size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist);
6137 
6138 		if (label->read_failed == B_TRUE)
6139 			continue;
6140 
6141 		if (label->config_nv) {
6142 			dump_config_from_label(label, buflen, l);
6143 		} else {
6144 			if (!dump_opt['q'])
6145 				(void) printf("failed to unpack label %d\n", l);
6146 		}
6147 
6148 		if (dump_opt['u'])
6149 			dump_label_uberblocks(label, ashift, l);
6150 
6151 		nvlist_free(label->config_nv);
6152 	}
6153 
6154 	/*
6155 	 * Dump the L2ARC header, if existent.
6156 	 */
6157 	if (read_l2arc_header)
6158 		error |= dump_l2arc_header(fd);
6159 
6160 	cookie = NULL;
6161 	while ((node = avl_destroy_nodes(&config_tree, &cookie)) != NULL)
6162 		umem_free(node, sizeof (cksum_record_t));
6163 
6164 	cookie = NULL;
6165 	while ((node = avl_destroy_nodes(&uberblock_tree, &cookie)) != NULL)
6166 		umem_free(node, sizeof (cksum_record_t));
6167 
6168 	avl_destroy(&config_tree);
6169 	avl_destroy(&uberblock_tree);
6170 
6171 	(void) close(fd);
6172 
6173 	return (config_found == B_FALSE ? 2 :
6174 	    (error == B_TRUE ? 1 : 0));
6175 }
6176 
6177 static uint64_t dataset_feature_count[SPA_FEATURES];
6178 static uint64_t global_feature_count[SPA_FEATURES];
6179 static uint64_t remap_deadlist_count = 0;
6180 
6181 static int
6182 dump_one_objset(const char *dsname, void *arg)
6183 {
6184 	(void) arg;
6185 	int error;
6186 	objset_t *os;
6187 	spa_feature_t f;
6188 
6189 	error = open_objset(dsname, FTAG, &os);
6190 	if (error != 0)
6191 		return (0);
6192 
6193 	for (f = 0; f < SPA_FEATURES; f++) {
6194 		if (!dsl_dataset_feature_is_active(dmu_objset_ds(os), f))
6195 			continue;
6196 		ASSERT(spa_feature_table[f].fi_flags &
6197 		    ZFEATURE_FLAG_PER_DATASET);
6198 		dataset_feature_count[f]++;
6199 	}
6200 
6201 	if (dsl_dataset_remap_deadlist_exists(dmu_objset_ds(os))) {
6202 		remap_deadlist_count++;
6203 	}
6204 
6205 	for (dsl_bookmark_node_t *dbn =
6206 	    avl_first(&dmu_objset_ds(os)->ds_bookmarks); dbn != NULL;
6207 	    dbn = AVL_NEXT(&dmu_objset_ds(os)->ds_bookmarks, dbn)) {
6208 		mos_obj_refd(dbn->dbn_phys.zbm_redaction_obj);
6209 		if (dbn->dbn_phys.zbm_redaction_obj != 0) {
6210 			global_feature_count[
6211 			    SPA_FEATURE_REDACTION_BOOKMARKS]++;
6212 			objset_t *mos = os->os_spa->spa_meta_objset;
6213 			dnode_t *rl;
6214 			VERIFY0(dnode_hold(mos,
6215 			    dbn->dbn_phys.zbm_redaction_obj, FTAG, &rl));
6216 			if (rl->dn_have_spill) {
6217 				global_feature_count[
6218 				    SPA_FEATURE_REDACTION_LIST_SPILL]++;
6219 			}
6220 		}
6221 		if (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN)
6222 			global_feature_count[SPA_FEATURE_BOOKMARK_WRITTEN]++;
6223 	}
6224 
6225 	if (dsl_deadlist_is_open(&dmu_objset_ds(os)->ds_dir->dd_livelist) &&
6226 	    !dmu_objset_is_snapshot(os)) {
6227 		global_feature_count[SPA_FEATURE_LIVELIST]++;
6228 	}
6229 	dump_objset(os);
6230 	close_objset(os, FTAG);
6231 	fuid_table_destroy();
6232 	return (0);
6233 }
6234 
6235 /*
6236  * Block statistics.
6237  */
6238 #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
6239 typedef struct zdb_blkstats {
6240 	uint64_t zb_asize;
6241 	uint64_t zb_lsize;
6242 	uint64_t zb_psize;
6243 	uint64_t zb_count;
6244 	uint64_t zb_gangs;
6245 	uint64_t zb_ditto_samevdev;
6246 	uint64_t zb_ditto_same_ms;
6247 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
6248 } zdb_blkstats_t;
6249 
6250 /*
6251  * Extended object types to report deferred frees, dedup auto-ditto blocks
6252  * and pending DDT-log frees.
6253  */
6254 #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
6255 #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
6256 #define	ZDB_OT_DDT_PENDING	(DMU_OT_NUMTYPES + 2)
6257 #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 3)
6258 #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 4)
6259 
6260 static const char *zdb_ot_extname[] = {
6261 	"deferred free",
6262 	"dedup ditto",
6263 	"DDT pending free",
6264 	"other",
6265 	"Total",
6266 };
6267 
6268 #define	ZB_TOTAL	DN_MAX_LEVELS
6269 #define	SPA_MAX_FOR_16M	(SPA_MAXBLOCKSHIFT+1)
6270 
6271 typedef struct zdb_brt_entry {
6272 	dva_t		zbre_dva;
6273 	uint64_t	zbre_refcount;
6274 	avl_node_t	zbre_node;
6275 } zdb_brt_entry_t;
6276 
6277 typedef struct zdb_cb {
6278 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
6279 	uint64_t	zcb_removing_size;
6280 	uint64_t	zcb_checkpoint_size;
6281 	uint64_t	zcb_dedup_asize;
6282 	uint64_t	zcb_dedup_blocks;
6283 	uint64_t	zcb_clone_asize;
6284 	uint64_t	zcb_clone_blocks;
6285 	uint64_t	zcb_psize_count[SPA_MAX_FOR_16M];
6286 	uint64_t	zcb_lsize_count[SPA_MAX_FOR_16M];
6287 	uint64_t	zcb_asize_count[SPA_MAX_FOR_16M];
6288 	uint64_t	zcb_psize_len[SPA_MAX_FOR_16M];
6289 	uint64_t	zcb_lsize_len[SPA_MAX_FOR_16M];
6290 	uint64_t	zcb_asize_len[SPA_MAX_FOR_16M];
6291 	uint64_t	zcb_psize_total;
6292 	uint64_t	zcb_lsize_total;
6293 	uint64_t	zcb_asize_total;
6294 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
6295 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
6296 	    [BPE_PAYLOAD_SIZE + 1];
6297 	uint64_t	zcb_start;
6298 	hrtime_t	zcb_lastprint;
6299 	uint64_t	zcb_totalasize;
6300 	uint64_t	zcb_errors[256];
6301 	int		zcb_readfails;
6302 	int		zcb_haderrors;
6303 	spa_t		*zcb_spa;
6304 	uint32_t	**zcb_vd_obsolete_counts;
6305 	avl_tree_t	zcb_brt;
6306 	boolean_t	zcb_brt_is_active;
6307 } zdb_cb_t;
6308 
6309 /* test if two DVA offsets from same vdev are within the same metaslab */
6310 static boolean_t
6311 same_metaslab(spa_t *spa, uint64_t vdev, uint64_t off1, uint64_t off2)
6312 {
6313 	vdev_t *vd = vdev_lookup_top(spa, vdev);
6314 	uint64_t ms_shift = vd->vdev_ms_shift;
6315 
6316 	return ((off1 >> ms_shift) == (off2 >> ms_shift));
6317 }
6318 
6319 /*
6320  * Used to simplify reporting of the histogram data.
6321  */
6322 typedef struct one_histo {
6323 	const char *name;
6324 	uint64_t *count;
6325 	uint64_t *len;
6326 	uint64_t cumulative;
6327 } one_histo_t;
6328 
6329 /*
6330  * The number of separate histograms processed for psize, lsize and asize.
6331  */
6332 #define	NUM_HISTO 3
6333 
6334 /*
6335  * This routine will create a fixed column size output of three different
6336  * histograms showing by blocksize of 512 - 2^ SPA_MAX_FOR_16M
6337  * the count, length and cumulative length of the psize, lsize and
6338  * asize blocks.
6339  *
6340  * All three types of blocks are listed on a single line
6341  *
6342  * By default the table is printed in nicenumber format (e.g. 123K) but
6343  * if the '-P' parameter is specified then the full raw number (parseable)
6344  * is printed out.
6345  */
6346 static void
6347 dump_size_histograms(zdb_cb_t *zcb)
6348 {
6349 	/*
6350 	 * A temporary buffer that allows us to convert a number into
6351 	 * a string using zdb_nicenumber to allow either raw or human
6352 	 * readable numbers to be output.
6353 	 */
6354 	char numbuf[32];
6355 
6356 	/*
6357 	 * Define titles which are used in the headers of the tables
6358 	 * printed by this routine.
6359 	 */
6360 	const char blocksize_title1[] = "block";
6361 	const char blocksize_title2[] = "size";
6362 	const char count_title[] = "Count";
6363 	const char length_title[] = "Size";
6364 	const char cumulative_title[] = "Cum.";
6365 
6366 	/*
6367 	 * Setup the histogram arrays (psize, lsize, and asize).
6368 	 */
6369 	one_histo_t parm_histo[NUM_HISTO];
6370 
6371 	parm_histo[0].name = "psize";
6372 	parm_histo[0].count = zcb->zcb_psize_count;
6373 	parm_histo[0].len = zcb->zcb_psize_len;
6374 	parm_histo[0].cumulative = 0;
6375 
6376 	parm_histo[1].name = "lsize";
6377 	parm_histo[1].count = zcb->zcb_lsize_count;
6378 	parm_histo[1].len = zcb->zcb_lsize_len;
6379 	parm_histo[1].cumulative = 0;
6380 
6381 	parm_histo[2].name = "asize";
6382 	parm_histo[2].count = zcb->zcb_asize_count;
6383 	parm_histo[2].len = zcb->zcb_asize_len;
6384 	parm_histo[2].cumulative = 0;
6385 
6386 
6387 	(void) printf("\nBlock Size Histogram\n");
6388 	switch (block_bin_mode) {
6389 	case BIN_PSIZE:
6390 		printf("(note: all categories are binned by %s)\n", "psize");
6391 		break;
6392 	case BIN_LSIZE:
6393 		printf("(note: all categories are binned by %s)\n", "lsize");
6394 		break;
6395 	case BIN_ASIZE:
6396 		printf("(note: all categories are binned by %s)\n", "asize");
6397 		break;
6398 	default:
6399 		printf("(note: all categories are binned separately)\n");
6400 		break;
6401 	}
6402 	if (block_classes != 0) {
6403 		char buf[256] = "";
6404 		if (block_classes & CLASS_NORMAL)
6405 			strlcat(buf, "\"normal\", ", sizeof (buf));
6406 		if (block_classes & CLASS_SPECIAL)
6407 			strlcat(buf, "\"special\", ", sizeof (buf));
6408 		if (block_classes & CLASS_DEDUP)
6409 			strlcat(buf, "\"dedup\", ", sizeof (buf));
6410 		if (block_classes & CLASS_OTHER)
6411 			strlcat(buf, "\"other\", ", sizeof (buf));
6412 		buf[strlen(buf)-2] = '\0';
6413 		printf("(note: only blocks in these classes are counted: %s)\n",
6414 		    buf);
6415 	}
6416 	/*
6417 	 * Print the first line titles
6418 	 */
6419 	if (dump_opt['P'])
6420 		(void) printf("\n%s\t", blocksize_title1);
6421 	else
6422 		(void) printf("\n%7s   ", blocksize_title1);
6423 
6424 	for (int j = 0; j < NUM_HISTO; j++) {
6425 		if (dump_opt['P']) {
6426 			if (j < NUM_HISTO - 1) {
6427 				(void) printf("%s\t\t\t", parm_histo[j].name);
6428 			} else {
6429 				/* Don't print trailing spaces */
6430 				(void) printf("  %s", parm_histo[j].name);
6431 			}
6432 		} else {
6433 			if (j < NUM_HISTO - 1) {
6434 				/* Left aligned strings in the output */
6435 				(void) printf("%-7s              ",
6436 				    parm_histo[j].name);
6437 			} else {
6438 				/* Don't print trailing spaces */
6439 				(void) printf("%s", parm_histo[j].name);
6440 			}
6441 		}
6442 	}
6443 	(void) printf("\n");
6444 
6445 	/*
6446 	 * Print the second line titles
6447 	 */
6448 	if (dump_opt['P']) {
6449 		(void) printf("%s\t", blocksize_title2);
6450 	} else {
6451 		(void) printf("%7s ", blocksize_title2);
6452 	}
6453 
6454 	for (int i = 0; i < NUM_HISTO; i++) {
6455 		if (dump_opt['P']) {
6456 			(void) printf("%s\t%s\t%s\t",
6457 			    count_title, length_title, cumulative_title);
6458 		} else {
6459 			(void) printf("%7s%7s%7s",
6460 			    count_title, length_title, cumulative_title);
6461 		}
6462 	}
6463 	(void) printf("\n");
6464 
6465 	/*
6466 	 * Print the rows
6467 	 */
6468 	for (int i = SPA_MINBLOCKSHIFT; i < SPA_MAX_FOR_16M; i++) {
6469 
6470 		/*
6471 		 * Print the first column showing the blocksize
6472 		 */
6473 		zdb_nicenum((1ULL << i), numbuf, sizeof (numbuf));
6474 
6475 		if (dump_opt['P']) {
6476 			printf("%s", numbuf);
6477 		} else {
6478 			printf("%7s:", numbuf);
6479 		}
6480 
6481 		/*
6482 		 * Print the remaining set of 3 columns per size:
6483 		 * for psize, lsize and asize
6484 		 */
6485 		for (int j = 0; j < NUM_HISTO; j++) {
6486 			parm_histo[j].cumulative += parm_histo[j].len[i];
6487 
6488 			zdb_nicenum(parm_histo[j].count[i],
6489 			    numbuf, sizeof (numbuf));
6490 			if (dump_opt['P'])
6491 				(void) printf("\t%s", numbuf);
6492 			else
6493 				(void) printf("%7s", numbuf);
6494 
6495 			zdb_nicenum(parm_histo[j].len[i],
6496 			    numbuf, sizeof (numbuf));
6497 			if (dump_opt['P'])
6498 				(void) printf("\t%s", numbuf);
6499 			else
6500 				(void) printf("%7s", numbuf);
6501 
6502 			zdb_nicenum(parm_histo[j].cumulative,
6503 			    numbuf, sizeof (numbuf));
6504 			if (dump_opt['P'])
6505 				(void) printf("\t%s", numbuf);
6506 			else
6507 				(void) printf("%7s", numbuf);
6508 		}
6509 		(void) printf("\n");
6510 	}
6511 }
6512 
6513 static void
6514 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
6515     dmu_object_type_t type)
6516 {
6517 	int i;
6518 	boolean_t claimed = B_FALSE;
6519 	boolean_t ddt_block = B_FALSE;
6520 	boolean_t brt_block = B_FALSE;
6521 
6522 	ASSERT(type < ZDB_OT_TOTAL);
6523 
6524 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
6525 		return;
6526 
6527 	spa_config_enter(zcb->zcb_spa, SCL_CONFIG, FTAG, RW_READER);
6528 
6529 	blkptr_t tempbp;
6530 	if (BP_GET_DEDUP(bp)) {
6531 		/*
6532 		 * Dedup'd blocks are special. We need to count them, so we can
6533 		 * later uncount them when reporting leaked space, and we must
6534 		 * only claim them once.
6535 		 *
6536 		 * We use the existing dedup system to track what we've seen.
6537 		 * The first time we see a block, we do a ddt_lookup() to see
6538 		 * if it exists in the DDT. If we're doing leak tracking, we
6539 		 * claim the block at this time.
6540 		 *
6541 		 * Each time we see a block, we reduce the refcount in the
6542 		 * entry by one, and add to the size and count of dedup'd
6543 		 * blocks to report at the end.
6544 		 */
6545 
6546 		ddt_t *ddt = ddt_select(zcb->zcb_spa, bp);
6547 
6548 		ddt_enter(ddt);
6549 
6550 		/*
6551 		 * Find the block. This will create the entry in memory, but
6552 		 * we'll know if that happened by its refcount.
6553 		 */
6554 		ddt_entry_t *dde = ddt_lookup(ddt, bp, B_TRUE);
6555 
6556 		/*
6557 		 * ddt_lookup() can return NULL when unique entries are pruned
6558 		 * from the DDT.
6559 		 */
6560 		if (dde == NULL) {
6561 			ddt_exit(ddt);
6562 			goto ddt_done;
6563 		}
6564 
6565 		/* Get the phys for this variant */
6566 		ddt_phys_variant_t v = ddt_phys_select(ddt, dde, bp);
6567 
6568 		/*
6569 		 * DDT_PHYS_NONE means the block has the dedup bit set but
6570 		 * its DVA doesn't match any phys in the entry.  This can
6571 		 * happen when a DVA was evicted from the DDT and re-added
6572 		 * on a hash collision.  The block may still have a BRT entry.
6573 		 */
6574 		if (v == DDT_PHYS_NONE) {
6575 			ddt_exit(ddt);
6576 			goto ddt_done;
6577 		}
6578 
6579 		ddt_block = B_TRUE;
6580 
6581 		/*
6582 		 * This entry may have multiple sets of DVAs. We must claim
6583 		 * each set the first time we see them in a real block on disk,
6584 		 * or count them on subsequent occurences. We don't have a
6585 		 * convenient way to track the first time we see each variant,
6586 		 * so we repurpose dde_io as a set of "seen" flag bits. We can
6587 		 * do this safely in zdb because it never writes, so it will
6588 		 * never have a writing zio for this block in that pointer.
6589 		 */
6590 		boolean_t seen = !!(((uintptr_t)dde->dde_io) & (1 << v));
6591 		if (!seen)
6592 			dde->dde_io =
6593 			    (void *)(((uintptr_t)dde->dde_io) | (1 << v));
6594 
6595 		/*
6596 		 * Consume a reference.  If this variant's refcount is already
6597 		 * zero, the DDT tracking is exhausted — more filesystem
6598 		 * references exist than the DDT accounts for.
6599 		 */
6600 		boolean_t ddt_refcnt_exhausted =
6601 		    (ddt_phys_refcnt(dde->dde_phys, v) == 0);
6602 		if (!ddt_refcnt_exhausted)
6603 			ddt_phys_decref(dde->dde_phys, v);
6604 
6605 		/*
6606 		 * If this entry has a single flat phys, it may have been
6607 		 * extended with additional DVAs at some time in its life.
6608 		 * This block might be from before it was fully extended, and
6609 		 * so have fewer DVAs.
6610 		 *
6611 		 * If this is the first time we've seen this block, and we
6612 		 * claimed it as-is, then we would miss the claim on some
6613 		 * number of DVAs, which would then be seen as leaked.
6614 		 *
6615 		 * In all cases, if we've had fewer DVAs, then the asize would
6616 		 * be too small, and would lead to the pool apparently using
6617 		 * more space than allocated.
6618 		 *
6619 		 * To handle this, we copy the canonical set of DVAs from the
6620 		 * entry back to the block pointer before we claim it.
6621 		 */
6622 		if (v == DDT_PHYS_FLAT) {
6623 			ASSERT3U(BP_GET_PHYSICAL_BIRTH(bp), ==,
6624 			    ddt_phys_birth(dde->dde_phys, v));
6625 			tempbp = *bp;
6626 			ddt_bp_fill(dde->dde_phys, v, &tempbp,
6627 			    BP_GET_PHYSICAL_BIRTH(bp));
6628 			bp = &tempbp;
6629 		}
6630 
6631 		if (seen && !ddt_refcnt_exhausted) {
6632 			/*
6633 			 * The second or later time we see this block,
6634 			 * it's a duplicate and we count it.
6635 			 */
6636 			zcb->zcb_dedup_asize += BP_GET_ASIZE(bp);
6637 			zcb->zcb_dedup_blocks++;
6638 			claimed = B_TRUE;
6639 		}
6640 
6641 		ddt_exit(ddt);
6642 	}
6643 
6644 ddt_done:
6645 	if (!claimed && zcb->zcb_brt_is_active &&
6646 	    brt_maybe_exists(zcb->zcb_spa, bp)) {
6647 		/*
6648 		 * Cloned blocks are special. We need to count them, so we can
6649 		 * later uncount them when reporting leaked space, and we must
6650 		 * only claim them once.
6651 		 *
6652 		 * To do this, we keep our own in-memory BRT. For each block
6653 		 * we haven't seen before, we look it up in the real BRT. If
6654 		 * we see the block again, we count it as a clone.
6655 		 */
6656 		zdb_brt_entry_t zbre_search, *zbre;
6657 		avl_index_t where;
6658 
6659 		zbre_search.zbre_dva = bp->blk_dva[0];
6660 		zbre = avl_find(&zcb->zcb_brt, &zbre_search, &where);
6661 		if (zbre == NULL) {
6662 			uint64_t refcnt =
6663 			    brt_entry_get_refcount(zcb->zcb_spa, bp);
6664 			if (refcnt > 0) {
6665 				brt_block = B_TRUE;
6666 				zbre = umem_zalloc(sizeof (zdb_brt_entry_t),
6667 				    UMEM_NOFAIL);
6668 				zbre->zbre_dva = bp->blk_dva[0];
6669 				zbre->zbre_refcount = refcnt;
6670 				avl_insert(&zcb->zcb_brt, zbre, where);
6671 			}
6672 		} else {
6673 			brt_block = B_TRUE;
6674 			if (zbre->zbre_refcount > 0) {
6675 				zcb->zcb_clone_asize += BP_GET_ASIZE(bp);
6676 				zcb->zcb_clone_blocks++;
6677 				zbre->zbre_refcount--;
6678 				claimed = B_TRUE;
6679 			}
6680 		}
6681 	}
6682 
6683 	for (i = 0; i < 4; i++) {
6684 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
6685 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
6686 		int equal;
6687 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
6688 
6689 		zb->zb_asize += BP_GET_ASIZE(bp);
6690 		zb->zb_lsize += BP_GET_LSIZE(bp);
6691 		zb->zb_psize += BP_GET_PSIZE(bp);
6692 		zb->zb_count++;
6693 
6694 		/*
6695 		 * The histogram is only big enough to record blocks up to
6696 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
6697 		 * "other", bucket.
6698 		 */
6699 		unsigned idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
6700 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
6701 		zb->zb_psize_histogram[idx]++;
6702 
6703 		zb->zb_gangs += BP_COUNT_GANG(bp);
6704 
6705 		switch (BP_GET_NDVAS(bp)) {
6706 		case 2:
6707 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
6708 			    DVA_GET_VDEV(&bp->blk_dva[1])) {
6709 				zb->zb_ditto_samevdev++;
6710 
6711 				if (same_metaslab(zcb->zcb_spa,
6712 				    DVA_GET_VDEV(&bp->blk_dva[0]),
6713 				    DVA_GET_OFFSET(&bp->blk_dva[0]),
6714 				    DVA_GET_OFFSET(&bp->blk_dva[1])))
6715 					zb->zb_ditto_same_ms++;
6716 			}
6717 			break;
6718 		case 3:
6719 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
6720 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
6721 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
6722 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
6723 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
6724 			    DVA_GET_VDEV(&bp->blk_dva[2]));
6725 			if (equal != 0) {
6726 				zb->zb_ditto_samevdev++;
6727 
6728 				if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
6729 				    DVA_GET_VDEV(&bp->blk_dva[1]) &&
6730 				    same_metaslab(zcb->zcb_spa,
6731 				    DVA_GET_VDEV(&bp->blk_dva[0]),
6732 				    DVA_GET_OFFSET(&bp->blk_dva[0]),
6733 				    DVA_GET_OFFSET(&bp->blk_dva[1])))
6734 					zb->zb_ditto_same_ms++;
6735 				else if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
6736 				    DVA_GET_VDEV(&bp->blk_dva[2]) &&
6737 				    same_metaslab(zcb->zcb_spa,
6738 				    DVA_GET_VDEV(&bp->blk_dva[0]),
6739 				    DVA_GET_OFFSET(&bp->blk_dva[0]),
6740 				    DVA_GET_OFFSET(&bp->blk_dva[2])))
6741 					zb->zb_ditto_same_ms++;
6742 				else if (DVA_GET_VDEV(&bp->blk_dva[1]) ==
6743 				    DVA_GET_VDEV(&bp->blk_dva[2]) &&
6744 				    same_metaslab(zcb->zcb_spa,
6745 				    DVA_GET_VDEV(&bp->blk_dva[1]),
6746 				    DVA_GET_OFFSET(&bp->blk_dva[1]),
6747 				    DVA_GET_OFFSET(&bp->blk_dva[2])))
6748 					zb->zb_ditto_same_ms++;
6749 			}
6750 			break;
6751 		}
6752 	}
6753 
6754 	spa_config_exit(zcb->zcb_spa, SCL_CONFIG, FTAG);
6755 
6756 	if (BP_IS_EMBEDDED(bp)) {
6757 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
6758 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
6759 		    [BPE_GET_PSIZE(bp)]++;
6760 		return;
6761 	}
6762 
6763 	if (block_classes != 0) {
6764 		spa_config_enter(zcb->zcb_spa, SCL_CONFIG, FTAG, RW_READER);
6765 
6766 		uint64_t vdev = DVA_GET_VDEV(&bp->blk_dva[0]);
6767 		uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[0]);
6768 		vdev_t *vd = vdev_lookup_top(zcb->zcb_spa, vdev);
6769 		ASSERT(vd != NULL);
6770 
6771 		int class;
6772 		if (vd->vdev_ops == &vdev_indirect_ops) {
6773 			/*
6774 			 * A removed vdev has no metaslabs of its own. Without
6775 			 * -L we synthesize them (see
6776 			 * zdb_leak_init_prepare_indirect_vdevs()), but under
6777 			 * -L there is no metaslab array to index. Classify
6778 			 * from the allocation bias, which determines the
6779 			 * vdev's primary metaslab group, and does not depend
6780 			 * on whether those synthetic metaslabs happen to
6781 			 * exist.
6782 			 */
6783 			switch (vd->vdev_alloc_bias) {
6784 			case VDEV_BIAS_SPECIAL:
6785 				class = CLASS_SPECIAL;
6786 				break;
6787 			case VDEV_BIAS_DEDUP:
6788 				class = CLASS_DEDUP;
6789 				break;
6790 			case VDEV_BIAS_LOG:
6791 				class = CLASS_OTHER;
6792 				break;
6793 			default:
6794 				class = CLASS_NORMAL;
6795 				break;
6796 			}
6797 			spa_config_exit(zcb->zcb_spa, SCL_CONFIG, FTAG);
6798 		} else {
6799 			metaslab_t *ms =
6800 			    vd->vdev_ms[offset >> vd->vdev_ms_shift];
6801 			ASSERT(ms != NULL);
6802 			metaslab_group_t *mg = ms->ms_group;
6803 			ASSERT(mg != NULL);
6804 			metaslab_class_t *mc = mg->mg_class;
6805 			ASSERT(mc != NULL);
6806 
6807 			spa_config_exit(zcb->zcb_spa, SCL_CONFIG, FTAG);
6808 
6809 			if (mc == spa_normal_class(zcb->zcb_spa)) {
6810 				class = CLASS_NORMAL;
6811 			} else if (mc == spa_special_class(zcb->zcb_spa)) {
6812 				class = CLASS_SPECIAL;
6813 			} else if (mc == spa_dedup_class(zcb->zcb_spa)) {
6814 				class = CLASS_DEDUP;
6815 			} else {
6816 				class = CLASS_OTHER;
6817 			}
6818 		}
6819 
6820 		if (!(block_classes & class)) {
6821 			goto hist_skipped;
6822 		}
6823 	}
6824 
6825 	/*
6826 	 * The binning histogram bins by powers of two up to
6827 	 * SPA_MAXBLOCKSIZE rather than creating bins for
6828 	 * every possible blocksize found in the pool.
6829 	 */
6830 	int bin;
6831 
6832 	/*
6833 	 * Binning strategy: each bin includes blocks up to and including
6834 	 * the given size (excluding blocks that fit into the previous bin).
6835 	 * This way, the "4K" bin includes blocks within the (2K; 4K] range.
6836 	 */
6837 #define	BIN(size) (highbit64((size) - 1))
6838 
6839 	switch (block_bin_mode) {
6840 	case BIN_PSIZE: bin = BIN(BP_GET_PSIZE(bp)); break;
6841 	case BIN_LSIZE: bin = BIN(BP_GET_LSIZE(bp)); break;
6842 	case BIN_ASIZE: bin = BIN(BP_GET_ASIZE(bp)); break;
6843 	case BIN_AUTO: break;
6844 	default: PANIC("bad block_bin_mode"); abort();
6845 	}
6846 
6847 	if (block_bin_mode == BIN_AUTO)
6848 		bin = BIN(BP_GET_PSIZE(bp));
6849 
6850 	zcb->zcb_psize_count[bin]++;
6851 	zcb->zcb_psize_len[bin] += BP_GET_PSIZE(bp);
6852 	zcb->zcb_psize_total += BP_GET_PSIZE(bp);
6853 
6854 	if (block_bin_mode == BIN_AUTO)
6855 		bin = BIN(BP_GET_LSIZE(bp));
6856 
6857 	zcb->zcb_lsize_count[bin]++;
6858 	zcb->zcb_lsize_len[bin] += BP_GET_LSIZE(bp);
6859 	zcb->zcb_lsize_total += BP_GET_LSIZE(bp);
6860 
6861 	if (block_bin_mode == BIN_AUTO)
6862 		bin = BIN(BP_GET_ASIZE(bp));
6863 
6864 	zcb->zcb_asize_count[bin]++;
6865 	zcb->zcb_asize_len[bin] += BP_GET_ASIZE(bp);
6866 	zcb->zcb_asize_total += BP_GET_ASIZE(bp);
6867 
6868 #undef BIN
6869 
6870 hist_skipped:
6871 	if (claimed || dump_opt['L'])
6872 		return;
6873 
6874 	int claim_err = zio_wait(zio_claim(NULL, zcb->zcb_spa,
6875 	    spa_min_claim_txg(zcb->zcb_spa), bp, NULL, NULL,
6876 	    ZIO_FLAG_CANFAIL));
6877 	if (claim_err != 0) {
6878 		char blkbuf[BP_SPRINTF_LEN];
6879 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
6880 		(void) printf("block claim error %d%s%s: %s\n",
6881 		    claim_err, brt_block ? " (BRT)" : "",
6882 		    ddt_block ? " (DDT)" : "", blkbuf);
6883 		zcb->zcb_haderrors = 1;
6884 		zcb->zcb_errors[claim_err]++;
6885 	}
6886 }
6887 
6888 static void
6889 zdb_blkptr_done(zio_t *zio)
6890 {
6891 	spa_t *spa = zio->io_spa;
6892 	blkptr_t *bp = zio->io_bp;
6893 	int ioerr = zio->io_error;
6894 	zdb_cb_t *zcb = zio->io_private;
6895 	zbookmark_phys_t *zb = &zio->io_bookmark;
6896 
6897 	mutex_enter(&spa->spa_scrub_lock);
6898 	spa->spa_load_verify_bytes -= BP_GET_PSIZE(bp);
6899 	cv_broadcast(&spa->spa_scrub_io_cv);
6900 
6901 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
6902 		char blkbuf[BP_SPRINTF_LEN];
6903 
6904 		zcb->zcb_haderrors = 1;
6905 		zcb->zcb_errors[ioerr]++;
6906 
6907 		if (dump_opt['b'] >= 2)
6908 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
6909 		else
6910 			blkbuf[0] = '\0';
6911 
6912 		(void) printf("zdb_blkptr_cb: "
6913 		    "Got error %d reading "
6914 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
6915 		    ioerr,
6916 		    (u_longlong_t)zb->zb_objset,
6917 		    (u_longlong_t)zb->zb_object,
6918 		    (u_longlong_t)zb->zb_level,
6919 		    (u_longlong_t)zb->zb_blkid,
6920 		    blkbuf);
6921 	}
6922 	mutex_exit(&spa->spa_scrub_lock);
6923 
6924 	abd_free(zio->io_abd);
6925 }
6926 
6927 static int
6928 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
6929     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
6930 {
6931 	zdb_cb_t *zcb = arg;
6932 	dmu_object_type_t type;
6933 	boolean_t is_metadata;
6934 
6935 	if (zb->zb_level == ZB_DNODE_LEVEL)
6936 		return (0);
6937 
6938 	if (dump_opt['b'] >= 5 && BP_GET_BIRTH(bp) > 0) {
6939 		char blkbuf[BP_SPRINTF_LEN];
6940 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
6941 		(void) printf("objset %llu object %llu "
6942 		    "level %lld offset 0x%llx %s\n",
6943 		    (u_longlong_t)zb->zb_objset,
6944 		    (u_longlong_t)zb->zb_object,
6945 		    (longlong_t)zb->zb_level,
6946 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
6947 		    blkbuf);
6948 	}
6949 
6950 	if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp))
6951 		return (0);
6952 
6953 	type = BP_GET_TYPE(bp);
6954 
6955 	zdb_count_block(zcb, zilog, bp,
6956 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
6957 
6958 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
6959 
6960 	if (!BP_IS_EMBEDDED(bp) &&
6961 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
6962 		size_t size = BP_GET_PSIZE(bp);
6963 		abd_t *abd = abd_alloc(size, B_FALSE);
6964 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
6965 
6966 		/* If it's an intent log block, failure is expected. */
6967 		if (zb->zb_level == ZB_ZIL_LEVEL)
6968 			flags |= ZIO_FLAG_SPECULATIVE;
6969 
6970 		mutex_enter(&spa->spa_scrub_lock);
6971 		while (spa->spa_load_verify_bytes > max_inflight_bytes)
6972 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
6973 		spa->spa_load_verify_bytes += size;
6974 		mutex_exit(&spa->spa_scrub_lock);
6975 
6976 		zio_nowait(zio_read(NULL, spa, bp, abd, size,
6977 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
6978 	}
6979 
6980 	zcb->zcb_readfails = 0;
6981 
6982 	/* only call gethrtime() every 100 blocks */
6983 	static int iters;
6984 	if (++iters > 100)
6985 		iters = 0;
6986 	else
6987 		return (0);
6988 
6989 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
6990 		uint64_t now = gethrtime();
6991 		char buf[10];
6992 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
6993 		uint64_t kb_per_sec =
6994 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
6995 		uint64_t sec_remaining =
6996 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
6997 
6998 		/* make sure nicenum has enough space */
6999 		_Static_assert(sizeof (buf) >= NN_NUMBUF_SZ, "buf truncated");
7000 
7001 		zfs_nicebytes(bytes, buf, sizeof (buf));
7002 		(void) fprintf(stderr,
7003 		    "\r%5s completed (%4"PRIu64"MB/s) "
7004 		    "estimated time remaining: "
7005 		    "%"PRIu64"hr %02"PRIu64"min %02"PRIu64"sec        ",
7006 		    buf, kb_per_sec / 1024,
7007 		    sec_remaining / 60 / 60,
7008 		    sec_remaining / 60 % 60,
7009 		    sec_remaining % 60);
7010 
7011 		zcb->zcb_lastprint = now;
7012 	}
7013 
7014 	return (0);
7015 }
7016 
7017 static void
7018 zdb_leak(void *arg, uint64_t start, uint64_t size)
7019 {
7020 	vdev_t *vd = arg;
7021 
7022 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
7023 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
7024 }
7025 
7026 static metaslab_ops_t zdb_metaslab_ops = {
7027 	NULL	/* alloc */
7028 };
7029 
7030 static int
7031 load_unflushed_svr_segs_cb(spa_t *spa, space_map_entry_t *sme,
7032     uint64_t txg, void *arg)
7033 {
7034 	spa_vdev_removal_t *svr = arg;
7035 
7036 	uint64_t offset = sme->sme_offset;
7037 	uint64_t size = sme->sme_run;
7038 
7039 	/* skip vdevs we don't care about */
7040 	if (sme->sme_vdev != svr->svr_vdev_id)
7041 		return (0);
7042 
7043 	vdev_t *vd = vdev_lookup_top(spa, sme->sme_vdev);
7044 	metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
7045 	ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE);
7046 
7047 	if (txg < metaslab_unflushed_txg(ms))
7048 		return (0);
7049 
7050 	if (sme->sme_type == SM_ALLOC)
7051 		zfs_range_tree_add(svr->svr_allocd_segs, offset, size);
7052 	else
7053 		zfs_range_tree_remove(svr->svr_allocd_segs, offset, size);
7054 
7055 	return (0);
7056 }
7057 
7058 static void
7059 claim_segment_impl_cb(uint64_t inner_offset, vdev_t *vd, uint64_t offset,
7060     uint64_t size, void *arg)
7061 {
7062 	(void) inner_offset, (void) arg;
7063 
7064 	/*
7065 	 * This callback was called through a remap from
7066 	 * a device being removed. Therefore, the vdev that
7067 	 * this callback is applied to is a concrete
7068 	 * vdev.
7069 	 */
7070 	ASSERT(vdev_is_concrete(vd));
7071 
7072 	VERIFY0(metaslab_claim_impl(vd, offset, size,
7073 	    spa_min_claim_txg(vd->vdev_spa)));
7074 }
7075 
7076 static void
7077 claim_segment_cb(void *arg, uint64_t offset, uint64_t size)
7078 {
7079 	vdev_t *vd = arg;
7080 
7081 	vdev_indirect_ops.vdev_op_remap(vd, offset, size,
7082 	    claim_segment_impl_cb, NULL);
7083 }
7084 
7085 /*
7086  * After accounting for all allocated blocks that are directly referenced,
7087  * we might have missed a reference to a block from a partially complete
7088  * (and thus unused) indirect mapping object. We perform a secondary pass
7089  * through the metaslabs we have already mapped and claim the destination
7090  * blocks.
7091  */
7092 static void
7093 zdb_claim_removing(spa_t *spa, zdb_cb_t *zcb)
7094 {
7095 	if (dump_opt['L'])
7096 		return;
7097 
7098 	if (spa->spa_vdev_removal == NULL)
7099 		return;
7100 
7101 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
7102 
7103 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
7104 	vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
7105 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
7106 
7107 	ASSERT0(zfs_range_tree_space(svr->svr_allocd_segs));
7108 
7109 	zfs_range_tree_t *allocs = zfs_range_tree_create_flags(
7110 	    NULL, ZFS_RANGE_SEG64, NULL, 0, 0,
7111 	    0, "zdb_claim_removing:allocs");
7112 	for (uint64_t msi = 0; msi < vd->vdev_ms_count; msi++) {
7113 		metaslab_t *msp = vd->vdev_ms[msi];
7114 
7115 		ASSERT0(zfs_range_tree_space(allocs));
7116 		if (msp->ms_sm != NULL)
7117 			VERIFY0(space_map_load(msp->ms_sm, allocs, SM_ALLOC));
7118 		zfs_range_tree_vacate(allocs, zfs_range_tree_add,
7119 		    svr->svr_allocd_segs);
7120 	}
7121 	zfs_range_tree_destroy(allocs);
7122 
7123 	iterate_through_spacemap_logs(spa, load_unflushed_svr_segs_cb, svr);
7124 
7125 	/*
7126 	 * Clear everything past what has been synced,
7127 	 * because we have not allocated mappings for
7128 	 * it yet.
7129 	 */
7130 	zfs_range_tree_clear(svr->svr_allocd_segs,
7131 	    vdev_indirect_mapping_max_offset(vim),
7132 	    vd->vdev_asize - vdev_indirect_mapping_max_offset(vim));
7133 
7134 	zcb->zcb_removing_size += zfs_range_tree_space(svr->svr_allocd_segs);
7135 	zfs_range_tree_vacate(svr->svr_allocd_segs, claim_segment_cb, vd);
7136 
7137 	spa_config_exit(spa, SCL_CONFIG, FTAG);
7138 }
7139 
7140 static int
7141 increment_indirect_mapping_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed,
7142     dmu_tx_t *tx)
7143 {
7144 	(void) tx;
7145 	zdb_cb_t *zcb = arg;
7146 	spa_t *spa = zcb->zcb_spa;
7147 	vdev_t *vd;
7148 	const dva_t *dva = &bp->blk_dva[0];
7149 
7150 	ASSERT(!bp_freed);
7151 	ASSERT(!dump_opt['L']);
7152 	ASSERT3U(BP_GET_NDVAS(bp), ==, 1);
7153 
7154 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
7155 	vd = vdev_lookup_top(zcb->zcb_spa, DVA_GET_VDEV(dva));
7156 	ASSERT3P(vd, !=, NULL);
7157 	spa_config_exit(spa, SCL_VDEV, FTAG);
7158 
7159 	ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0);
7160 	ASSERT3P(zcb->zcb_vd_obsolete_counts[vd->vdev_id], !=, NULL);
7161 
7162 	vdev_indirect_mapping_increment_obsolete_count(
7163 	    vd->vdev_indirect_mapping,
7164 	    DVA_GET_OFFSET(dva), DVA_GET_ASIZE(dva),
7165 	    zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
7166 
7167 	return (0);
7168 }
7169 
7170 static uint32_t *
7171 zdb_load_obsolete_counts(vdev_t *vd)
7172 {
7173 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
7174 	spa_t *spa = vd->vdev_spa;
7175 	spa_condensing_indirect_phys_t *scip =
7176 	    &spa->spa_condensing_indirect_phys;
7177 	uint64_t obsolete_sm_object;
7178 	uint32_t *counts;
7179 
7180 	VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object));
7181 	EQUIV(obsolete_sm_object != 0, vd->vdev_obsolete_sm != NULL);
7182 	counts = vdev_indirect_mapping_load_obsolete_counts(vim);
7183 	if (vd->vdev_obsolete_sm != NULL) {
7184 		vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
7185 		    vd->vdev_obsolete_sm);
7186 	}
7187 	if (scip->scip_vdev == vd->vdev_id &&
7188 	    scip->scip_prev_obsolete_sm_object != 0) {
7189 		space_map_t *prev_obsolete_sm = NULL;
7190 		VERIFY0(space_map_open(&prev_obsolete_sm, spa->spa_meta_objset,
7191 		    scip->scip_prev_obsolete_sm_object, 0, vd->vdev_asize, 0));
7192 		vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
7193 		    prev_obsolete_sm);
7194 		space_map_close(prev_obsolete_sm);
7195 	}
7196 	return (counts);
7197 }
7198 
7199 typedef struct checkpoint_sm_exclude_entry_arg {
7200 	vdev_t *cseea_vd;
7201 	uint64_t cseea_checkpoint_size;
7202 } checkpoint_sm_exclude_entry_arg_t;
7203 
7204 static int
7205 checkpoint_sm_exclude_entry_cb(space_map_entry_t *sme, void *arg)
7206 {
7207 	checkpoint_sm_exclude_entry_arg_t *cseea = arg;
7208 	vdev_t *vd = cseea->cseea_vd;
7209 	metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift];
7210 	uint64_t end = sme->sme_offset + sme->sme_run;
7211 
7212 	ASSERT(sme->sme_type == SM_FREE);
7213 
7214 	/*
7215 	 * Since the vdev_checkpoint_sm exists in the vdev level
7216 	 * and the ms_sm space maps exist in the metaslab level,
7217 	 * an entry in the checkpoint space map could theoretically
7218 	 * cross the boundaries of the metaslab that it belongs.
7219 	 *
7220 	 * In reality, because of the way that we populate and
7221 	 * manipulate the checkpoint's space maps currently,
7222 	 * there shouldn't be any entries that cross metaslabs.
7223 	 * Hence the assertion below.
7224 	 *
7225 	 * That said, there is no fundamental requirement that
7226 	 * the checkpoint's space map entries should not cross
7227 	 * metaslab boundaries. So if needed we could add code
7228 	 * that handles metaslab-crossing segments in the future.
7229 	 */
7230 	VERIFY3U(sme->sme_offset, >=, ms->ms_start);
7231 	VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
7232 
7233 	/*
7234 	 * By removing the entry from the allocated segments we
7235 	 * also verify that the entry is there to begin with.
7236 	 */
7237 	mutex_enter(&ms->ms_lock);
7238 	zfs_range_tree_remove(ms->ms_allocatable, sme->sme_offset,
7239 	    sme->sme_run);
7240 	mutex_exit(&ms->ms_lock);
7241 
7242 	cseea->cseea_checkpoint_size += sme->sme_run;
7243 	return (0);
7244 }
7245 
7246 static void
7247 zdb_leak_init_vdev_exclude_checkpoint(vdev_t *vd, zdb_cb_t *zcb)
7248 {
7249 	spa_t *spa = vd->vdev_spa;
7250 	space_map_t *checkpoint_sm = NULL;
7251 	uint64_t checkpoint_sm_obj;
7252 
7253 	/*
7254 	 * If there is no vdev_top_zap, we are in a pool whose
7255 	 * version predates the pool checkpoint feature.
7256 	 */
7257 	if (vd->vdev_top_zap == 0)
7258 		return;
7259 
7260 	/*
7261 	 * If there is no reference of the vdev_checkpoint_sm in
7262 	 * the vdev_top_zap, then one of the following scenarios
7263 	 * is true:
7264 	 *
7265 	 * 1] There is no checkpoint
7266 	 * 2] There is a checkpoint, but no checkpointed blocks
7267 	 *    have been freed yet
7268 	 * 3] The current vdev is indirect
7269 	 *
7270 	 * In these cases we return immediately.
7271 	 */
7272 	if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
7273 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
7274 		return;
7275 
7276 	VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
7277 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1,
7278 	    &checkpoint_sm_obj));
7279 
7280 	checkpoint_sm_exclude_entry_arg_t cseea;
7281 	cseea.cseea_vd = vd;
7282 	cseea.cseea_checkpoint_size = 0;
7283 
7284 	VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
7285 	    checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
7286 
7287 	VERIFY0(space_map_iterate(checkpoint_sm,
7288 	    space_map_length(checkpoint_sm),
7289 	    checkpoint_sm_exclude_entry_cb, &cseea));
7290 	space_map_close(checkpoint_sm);
7291 
7292 	zcb->zcb_checkpoint_size += cseea.cseea_checkpoint_size;
7293 }
7294 
7295 static void
7296 zdb_leak_init_exclude_checkpoint(spa_t *spa, zdb_cb_t *zcb)
7297 {
7298 	ASSERT(!dump_opt['L']);
7299 
7300 	vdev_t *rvd = spa->spa_root_vdev;
7301 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
7302 		ASSERT3U(c, ==, rvd->vdev_child[c]->vdev_id);
7303 		zdb_leak_init_vdev_exclude_checkpoint(rvd->vdev_child[c], zcb);
7304 	}
7305 }
7306 
7307 static int
7308 count_unflushed_space_cb(spa_t *spa, space_map_entry_t *sme,
7309     uint64_t txg, void *arg)
7310 {
7311 	int64_t *ualloc_space = arg;
7312 
7313 	uint64_t offset = sme->sme_offset;
7314 	uint64_t vdev_id = sme->sme_vdev;
7315 
7316 	vdev_t *vd = vdev_lookup_top(spa, vdev_id);
7317 	if (!vdev_is_concrete(vd))
7318 		return (0);
7319 
7320 	metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
7321 	ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE);
7322 
7323 	if (txg < metaslab_unflushed_txg(ms))
7324 		return (0);
7325 
7326 	if (sme->sme_type == SM_ALLOC)
7327 		*ualloc_space += sme->sme_run;
7328 	else
7329 		*ualloc_space -= sme->sme_run;
7330 
7331 	return (0);
7332 }
7333 
7334 static int64_t
7335 get_unflushed_alloc_space(spa_t *spa)
7336 {
7337 	if (dump_opt['L'])
7338 		return (0);
7339 
7340 	int64_t ualloc_space = 0;
7341 	iterate_through_spacemap_logs(spa, count_unflushed_space_cb,
7342 	    &ualloc_space);
7343 	return (ualloc_space);
7344 }
7345 
7346 static int
7347 load_unflushed_cb(spa_t *spa, space_map_entry_t *sme, uint64_t txg, void *arg)
7348 {
7349 	maptype_t *uic_maptype = arg;
7350 
7351 	uint64_t offset = sme->sme_offset;
7352 	uint64_t size = sme->sme_run;
7353 	uint64_t vdev_id = sme->sme_vdev;
7354 
7355 	vdev_t *vd = vdev_lookup_top(spa, vdev_id);
7356 
7357 	/* skip indirect vdevs */
7358 	if (!vdev_is_concrete(vd))
7359 		return (0);
7360 
7361 	metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
7362 
7363 	ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE);
7364 	ASSERT(*uic_maptype == SM_ALLOC || *uic_maptype == SM_FREE);
7365 
7366 	if (txg < metaslab_unflushed_txg(ms))
7367 		return (0);
7368 
7369 	if (*uic_maptype == sme->sme_type)
7370 		zfs_range_tree_add(ms->ms_allocatable, offset, size);
7371 	else
7372 		zfs_range_tree_remove(ms->ms_allocatable, offset, size);
7373 
7374 	return (0);
7375 }
7376 
7377 static void
7378 load_unflushed_to_ms_allocatables(spa_t *spa, maptype_t maptype)
7379 {
7380 	iterate_through_spacemap_logs(spa, load_unflushed_cb, &maptype);
7381 }
7382 
7383 static void
7384 load_concrete_ms_allocatable_trees(spa_t *spa, maptype_t maptype)
7385 {
7386 	vdev_t *rvd = spa->spa_root_vdev;
7387 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
7388 		vdev_t *vd = rvd->vdev_child[i];
7389 
7390 		ASSERT3U(i, ==, vd->vdev_id);
7391 
7392 		if (vd->vdev_ops == &vdev_indirect_ops)
7393 			continue;
7394 
7395 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
7396 			metaslab_t *msp = vd->vdev_ms[m];
7397 
7398 			(void) fprintf(stderr,
7399 			    "\rloading concrete vdev %llu, "
7400 			    "metaslab %llu of %llu ...",
7401 			    (longlong_t)vd->vdev_id,
7402 			    (longlong_t)msp->ms_id,
7403 			    (longlong_t)vd->vdev_ms_count);
7404 
7405 			mutex_enter(&msp->ms_lock);
7406 			zfs_range_tree_vacate(msp->ms_allocatable, NULL, NULL);
7407 
7408 			/*
7409 			 * We don't want to spend the CPU manipulating the
7410 			 * size-ordered tree, so clear the range_tree ops.
7411 			 */
7412 			msp->ms_allocatable->rt_ops = NULL;
7413 
7414 			if (msp->ms_sm != NULL) {
7415 				VERIFY0(space_map_load(msp->ms_sm,
7416 				    msp->ms_allocatable, maptype));
7417 			}
7418 			if (!msp->ms_loaded)
7419 				msp->ms_loaded = B_TRUE;
7420 			mutex_exit(&msp->ms_lock);
7421 		}
7422 	}
7423 
7424 	load_unflushed_to_ms_allocatables(spa, maptype);
7425 }
7426 
7427 /*
7428  * vm_idxp is an in-out parameter which (for indirect vdevs) is the
7429  * index in vim_entries that has the first entry in this metaslab.
7430  * On return, it will be set to the first entry after this metaslab.
7431  */
7432 static void
7433 load_indirect_ms_allocatable_tree(vdev_t *vd, metaslab_t *msp,
7434     uint64_t *vim_idxp)
7435 {
7436 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
7437 
7438 	mutex_enter(&msp->ms_lock);
7439 	zfs_range_tree_vacate(msp->ms_allocatable, NULL, NULL);
7440 
7441 	/*
7442 	 * We don't want to spend the CPU manipulating the
7443 	 * size-ordered tree, so clear the range_tree ops.
7444 	 */
7445 	msp->ms_allocatable->rt_ops = NULL;
7446 
7447 	for (; *vim_idxp < vdev_indirect_mapping_num_entries(vim);
7448 	    (*vim_idxp)++) {
7449 		vdev_indirect_mapping_entry_phys_t *vimep =
7450 		    &vim->vim_entries[*vim_idxp];
7451 		uint64_t ent_offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
7452 		uint64_t ent_len = DVA_GET_ASIZE(&vimep->vimep_dst);
7453 		ASSERT3U(ent_offset, >=, msp->ms_start);
7454 		if (ent_offset >= msp->ms_start + msp->ms_size)
7455 			break;
7456 
7457 		/*
7458 		 * Mappings do not cross metaslab boundaries,
7459 		 * because we create them by walking the metaslabs.
7460 		 */
7461 		ASSERT3U(ent_offset + ent_len, <=,
7462 		    msp->ms_start + msp->ms_size);
7463 		zfs_range_tree_add(msp->ms_allocatable, ent_offset, ent_len);
7464 	}
7465 
7466 	if (!msp->ms_loaded)
7467 		msp->ms_loaded = B_TRUE;
7468 	mutex_exit(&msp->ms_lock);
7469 }
7470 
7471 static void
7472 zdb_leak_init_prepare_indirect_vdevs(spa_t *spa, zdb_cb_t *zcb)
7473 {
7474 	ASSERT(!dump_opt['L']);
7475 
7476 	vdev_t *rvd = spa->spa_root_vdev;
7477 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
7478 		vdev_t *vd = rvd->vdev_child[c];
7479 
7480 		ASSERT3U(c, ==, vd->vdev_id);
7481 
7482 		if (vd->vdev_ops != &vdev_indirect_ops)
7483 			continue;
7484 
7485 		/*
7486 		 * Note: we don't check for mapping leaks on
7487 		 * removing vdevs because their ms_allocatable's
7488 		 * are used to look for leaks in allocated space.
7489 		 */
7490 		zcb->zcb_vd_obsolete_counts[c] = zdb_load_obsolete_counts(vd);
7491 
7492 		/*
7493 		 * Normally, indirect vdevs don't have any
7494 		 * metaslabs.  We want to set them up for
7495 		 * zio_claim().
7496 		 */
7497 		vdev_metaslab_group_create(vd);
7498 		VERIFY0(vdev_metaslab_init(vd, 0));
7499 
7500 		vdev_indirect_mapping_t *vim __maybe_unused =
7501 		    vd->vdev_indirect_mapping;
7502 		uint64_t vim_idx = 0;
7503 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
7504 
7505 			(void) fprintf(stderr,
7506 			    "\rloading indirect vdev %llu, "
7507 			    "metaslab %llu of %llu ...",
7508 			    (longlong_t)vd->vdev_id,
7509 			    (longlong_t)vd->vdev_ms[m]->ms_id,
7510 			    (longlong_t)vd->vdev_ms_count);
7511 
7512 			load_indirect_ms_allocatable_tree(vd, vd->vdev_ms[m],
7513 			    &vim_idx);
7514 		}
7515 		ASSERT3U(vim_idx, ==, vdev_indirect_mapping_num_entries(vim));
7516 	}
7517 }
7518 
7519 static void
7520 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
7521 {
7522 	zcb->zcb_spa = spa;
7523 
7524 	if (dump_opt['L'])
7525 		return;
7526 
7527 	dsl_pool_t *dp = spa->spa_dsl_pool;
7528 	vdev_t *rvd = spa->spa_root_vdev;
7529 
7530 	/*
7531 	 * We are going to be changing the meaning of the metaslab's
7532 	 * ms_allocatable.  Ensure that the allocator doesn't try to
7533 	 * use the tree.
7534 	 */
7535 	spa->spa_normal_class->mc_ops = &zdb_metaslab_ops;
7536 	spa->spa_log_class->mc_ops = &zdb_metaslab_ops;
7537 	spa->spa_embedded_log_class->mc_ops = &zdb_metaslab_ops;
7538 	spa->spa_special_embedded_log_class->mc_ops = &zdb_metaslab_ops;
7539 
7540 	zcb->zcb_vd_obsolete_counts =
7541 	    umem_zalloc(rvd->vdev_children * sizeof (uint32_t *),
7542 	    UMEM_NOFAIL);
7543 
7544 	/*
7545 	 * For leak detection, we overload the ms_allocatable trees
7546 	 * to contain allocated segments instead of free segments.
7547 	 * As a result, we can't use the normal metaslab_load/unload
7548 	 * interfaces.
7549 	 */
7550 	zdb_leak_init_prepare_indirect_vdevs(spa, zcb);
7551 	load_concrete_ms_allocatable_trees(spa, SM_ALLOC);
7552 
7553 	/*
7554 	 * On load_concrete_ms_allocatable_trees() we loaded all the
7555 	 * allocated entries from the ms_sm to the ms_allocatable for
7556 	 * each metaslab. If the pool has a checkpoint or is in the
7557 	 * middle of discarding a checkpoint, some of these blocks
7558 	 * may have been freed but their ms_sm may not have been
7559 	 * updated because they are referenced by the checkpoint. In
7560 	 * order to avoid false-positives during leak-detection, we
7561 	 * go through the vdev's checkpoint space map and exclude all
7562 	 * its entries from their relevant ms_allocatable.
7563 	 *
7564 	 * We also aggregate the space held by the checkpoint and add
7565 	 * it to zcb_checkpoint_size.
7566 	 *
7567 	 * Note that at this point we are also verifying that all the
7568 	 * entries on the checkpoint_sm are marked as allocated in
7569 	 * the ms_sm of their relevant metaslab.
7570 	 * [see comment in checkpoint_sm_exclude_entry_cb()]
7571 	 */
7572 	zdb_leak_init_exclude_checkpoint(spa, zcb);
7573 	ASSERT3U(zcb->zcb_checkpoint_size, ==, spa_get_checkpoint_space(spa));
7574 
7575 	/* for cleaner progress output */
7576 	(void) fprintf(stderr, "\n");
7577 
7578 	if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
7579 		ASSERT(spa_feature_is_enabled(spa,
7580 		    SPA_FEATURE_DEVICE_REMOVAL));
7581 		(void) bpobj_iterate_nofree(&dp->dp_obsolete_bpobj,
7582 		    increment_indirect_mapping_cb, zcb, NULL);
7583 	}
7584 }
7585 
7586 static boolean_t
7587 zdb_check_for_obsolete_leaks(vdev_t *vd, zdb_cb_t *zcb)
7588 {
7589 	boolean_t leaks = B_FALSE;
7590 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
7591 	uint64_t total_leaked = 0;
7592 	boolean_t are_precise = B_FALSE;
7593 
7594 	ASSERT(vim != NULL);
7595 
7596 	for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
7597 		vdev_indirect_mapping_entry_phys_t *vimep =
7598 		    &vim->vim_entries[i];
7599 		uint64_t obsolete_bytes = 0;
7600 		uint64_t offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
7601 		metaslab_t *msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
7602 
7603 		/*
7604 		 * This is not very efficient but it's easy to
7605 		 * verify correctness.
7606 		 */
7607 		for (uint64_t inner_offset = 0;
7608 		    inner_offset < DVA_GET_ASIZE(&vimep->vimep_dst);
7609 		    inner_offset += 1ULL << vd->vdev_ashift) {
7610 			if (zfs_range_tree_contains(msp->ms_allocatable,
7611 			    offset + inner_offset, 1ULL << vd->vdev_ashift)) {
7612 				obsolete_bytes += 1ULL << vd->vdev_ashift;
7613 			}
7614 		}
7615 
7616 		int64_t bytes_leaked = obsolete_bytes -
7617 		    zcb->zcb_vd_obsolete_counts[vd->vdev_id][i];
7618 		ASSERT3U(DVA_GET_ASIZE(&vimep->vimep_dst), >=,
7619 		    zcb->zcb_vd_obsolete_counts[vd->vdev_id][i]);
7620 
7621 		VERIFY0(vdev_obsolete_counts_are_precise(vd, &are_precise));
7622 		if (bytes_leaked != 0 && (are_precise || dump_opt['d'] >= 5)) {
7623 			(void) printf("obsolete indirect mapping count "
7624 			    "mismatch on %llu:%llx:%llx : %llx bytes leaked\n",
7625 			    (u_longlong_t)vd->vdev_id,
7626 			    (u_longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
7627 			    (u_longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
7628 			    (u_longlong_t)bytes_leaked);
7629 		}
7630 		total_leaked += ABS(bytes_leaked);
7631 	}
7632 
7633 	VERIFY0(vdev_obsolete_counts_are_precise(vd, &are_precise));
7634 	if (!are_precise && total_leaked > 0) {
7635 		int pct_leaked = total_leaked * 100 /
7636 		    vdev_indirect_mapping_bytes_mapped(vim);
7637 		(void) printf("cannot verify obsolete indirect mapping "
7638 		    "counts of vdev %llu because precise feature was not "
7639 		    "enabled when it was removed: %d%% (%llx bytes) of mapping"
7640 		    "unreferenced\n",
7641 		    (u_longlong_t)vd->vdev_id, pct_leaked,
7642 		    (u_longlong_t)total_leaked);
7643 	} else if (total_leaked > 0) {
7644 		(void) printf("obsolete indirect mapping count mismatch "
7645 		    "for vdev %llu -- %llx total bytes mismatched\n",
7646 		    (u_longlong_t)vd->vdev_id,
7647 		    (u_longlong_t)total_leaked);
7648 		leaks |= B_TRUE;
7649 	}
7650 
7651 	vdev_indirect_mapping_free_obsolete_counts(vim,
7652 	    zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
7653 	zcb->zcb_vd_obsolete_counts[vd->vdev_id] = NULL;
7654 
7655 	return (leaks);
7656 }
7657 
7658 static boolean_t
7659 zdb_leak_fini(spa_t *spa, zdb_cb_t *zcb)
7660 {
7661 	boolean_t leaks = B_FALSE;
7662 
7663 	/*
7664 	 * Report leaked BRT entries whose refcount was not fully consumed by
7665 	 * the traversal.
7666 	 */
7667 	if (zcb->zcb_brt_is_active) {
7668 		void *cookie = NULL;
7669 		zdb_brt_entry_t *zbre;
7670 		while ((zbre = avl_destroy_nodes(
7671 		    &zcb->zcb_brt, &cookie)) != NULL) {
7672 			if (!dump_opt['L'] && zbre->zbre_refcount != 0) {
7673 				(void) printf("BRT leak: vdev %llu, "
7674 				    "offset 0x%llx, refcount %llu\n",
7675 				    (u_longlong_t)DVA_GET_VDEV(
7676 				    &zbre->zbre_dva),
7677 				    (u_longlong_t)DVA_GET_OFFSET(
7678 				    &zbre->zbre_dva),
7679 				    (u_longlong_t)zbre->zbre_refcount);
7680 				leaks = B_TRUE;
7681 			}
7682 			umem_free(zbre, sizeof (zdb_brt_entry_t));
7683 		}
7684 		avl_destroy(&zcb->zcb_brt);
7685 	}
7686 
7687 	if (dump_opt['L'])
7688 		return (leaks);
7689 
7690 	/*
7691 	 * Report leaked DDT entries whose refcount was not fully consumed by
7692 	 * the traversal.  Entries in the DDT ZAP that were never looked up
7693 	 * are not detected here.
7694 	 */
7695 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
7696 		ddt_t *ddt = spa->spa_ddt[c];
7697 		if (ddt == NULL)
7698 			continue;
7699 		ddt_enter(ddt);
7700 		for (ddt_entry_t *dde = avl_first(&ddt->ddt_tree); dde != NULL;
7701 		    dde = AVL_NEXT(&ddt->ddt_tree, dde)) {
7702 			for (int p = 0; p < DDT_NPHYS(ddt); p++) {
7703 				ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
7704 				uint64_t refcnt = ddt_phys_refcnt(dde->dde_phys,
7705 				    v);
7706 				if (refcnt == 0)
7707 					continue;
7708 				blkptr_t blk;
7709 				char blkbuf[BP_SPRINTF_LEN];
7710 				ddt_bp_create(ddt->ddt_checksum, &dde->dde_key,
7711 				    dde->dde_phys, v, &blk);
7712 				snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
7713 				(void) printf("DDT leak: refcount %llu %s\n",
7714 				    (u_longlong_t)refcnt, blkbuf);
7715 				leaks = B_TRUE;
7716 			}
7717 		}
7718 		ddt_exit(ddt);
7719 	}
7720 
7721 	vdev_t *rvd = spa->spa_root_vdev;
7722 	for (unsigned c = 0; c < rvd->vdev_children; c++) {
7723 		vdev_t *vd = rvd->vdev_child[c];
7724 
7725 		if (zcb->zcb_vd_obsolete_counts[c] != NULL) {
7726 			leaks |= zdb_check_for_obsolete_leaks(vd, zcb);
7727 		}
7728 
7729 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
7730 			metaslab_t *msp = vd->vdev_ms[m];
7731 			ASSERT3P(msp->ms_group, ==, (msp->ms_group->mg_class ==
7732 			    spa_embedded_log_class(spa) ||
7733 			    msp->ms_group->mg_class ==
7734 			    spa_special_embedded_log_class(spa)) ?
7735 			    vd->vdev_log_mg : vd->vdev_mg);
7736 
7737 			/*
7738 			 * ms_allocatable has been overloaded
7739 			 * to contain allocated segments. Now that
7740 			 * we finished traversing all blocks, any
7741 			 * block that remains in the ms_allocatable
7742 			 * represents an allocated block that we
7743 			 * did not claim during the traversal.
7744 			 * Claimed blocks would have been removed
7745 			 * from the ms_allocatable.  For indirect
7746 			 * vdevs, space remaining in the tree
7747 			 * represents parts of the mapping that are
7748 			 * not referenced, which is not a bug.
7749 			 */
7750 			if (vd->vdev_ops == &vdev_indirect_ops) {
7751 				zfs_range_tree_vacate(msp->ms_allocatable,
7752 				    NULL, NULL);
7753 			} else {
7754 				zfs_range_tree_vacate(msp->ms_allocatable,
7755 				    zdb_leak, vd);
7756 			}
7757 			if (msp->ms_loaded) {
7758 				msp->ms_loaded = B_FALSE;
7759 			}
7760 		}
7761 	}
7762 
7763 	umem_free(zcb->zcb_vd_obsolete_counts,
7764 	    rvd->vdev_children * sizeof (uint32_t *));
7765 	zcb->zcb_vd_obsolete_counts = NULL;
7766 
7767 	return (leaks);
7768 }
7769 
7770 static int
7771 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
7772 {
7773 	(void) tx;
7774 	zdb_cb_t *zcb = arg;
7775 
7776 	if (dump_opt['b'] >= 5) {
7777 		char blkbuf[BP_SPRINTF_LEN];
7778 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
7779 		(void) printf("[%s] %s\n",
7780 		    "deferred free", blkbuf);
7781 	}
7782 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
7783 	return (0);
7784 }
7785 
7786 /*
7787  * Iterate over livelists which have been destroyed by the user but
7788  * are still present in the MOS, waiting to be freed
7789  */
7790 static void
7791 iterate_deleted_livelists(spa_t *spa, ll_iter_t func, void *arg)
7792 {
7793 	objset_t *mos = spa->spa_meta_objset;
7794 	uint64_t zap_obj;
7795 	int err = zap_lookup(mos, DMU_POOL_DIRECTORY_OBJECT,
7796 	    DMU_POOL_DELETED_CLONES, sizeof (uint64_t), 1, &zap_obj);
7797 	if (err == ENOENT)
7798 		return;
7799 	ASSERT0(err);
7800 
7801 	zap_cursor_t zc;
7802 	zap_attribute_t *attrp = zap_attribute_alloc();
7803 	dsl_deadlist_t ll;
7804 	/* NULL out os prior to dsl_deadlist_open in case it's garbage */
7805 	ll.dl_os = NULL;
7806 	for (zap_cursor_init(&zc, mos, zap_obj);
7807 	    zap_cursor_retrieve(&zc, attrp) == 0;
7808 	    (void) zap_cursor_advance(&zc)) {
7809 		VERIFY0(dsl_deadlist_open(&ll, mos, attrp->za_first_integer));
7810 		func(&ll, arg);
7811 		dsl_deadlist_close(&ll);
7812 	}
7813 	zap_cursor_fini(&zc);
7814 	zap_attribute_free(attrp);
7815 }
7816 
7817 static int
7818 bpobj_count_block_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed,
7819     dmu_tx_t *tx)
7820 {
7821 	ASSERT(!bp_freed);
7822 	return (count_block_cb(arg, bp, tx));
7823 }
7824 
7825 /*
7826  * With fast dedup, the last decref of a block lands in the DDT log, and
7827  * the physical free happens only when the log entry is flushed back into
7828  * the DDT (see ddt_sync_flush_entry()). Until then the block is not
7829  * referenced by any block pointer but is still allocated, so the
7830  * traversal would misreport it as leaked. Count the phys that the flush
7831  * will free here, the same way as the deferred-free bplist: they are
7832  * frees in flight. Entries behind the log checkpoint are already flushed
7833  * and are not loaded into the in-memory log trees, so everything found
7834  * here is genuinely pending. ddt_log_load() merge-normalizes duplicate
7835  * keys across the active and flushing logs, and ddt_lookup() searches the
7836  * logs before stored DDT objects, so these trees need no DDT ZAP replay.
7837  */
7838 static void
7839 zdb_count_ddt_log_frees(spa_t *spa, zdb_cb_t *zcb)
7840 {
7841 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
7842 		ddt_t *ddt = spa->spa_ddt[c];
7843 		if (ddt == NULL || !(ddt->ddt_flags & DDT_FLAG_LOG))
7844 			continue;
7845 		for (int n = 0; n < 2; n++) {
7846 			ddt_log_t *ddl = &ddt->ddt_log[n];
7847 			for (ddt_log_entry_t *ddle = avl_first(&ddl->ddl_tree);
7848 			    ddle; ddle = AVL_NEXT(&ddl->ddl_tree, ddle)) {
7849 				ddt_lightweight_entry_t ddlwe;
7850 				DDT_LOG_ENTRY_TO_LIGHTWEIGHT(ddt, ddle,
7851 				    &ddlwe);
7852 				for (int p = 0; p < DDT_NPHYS(ddt); p++) {
7853 					ddt_phys_variant_t v =
7854 					    DDT_PHYS_VARIANT(ddt, p);
7855 					/*
7856 					 * Mirror ddt_sync_flush_entry(): an
7857 					 * unborn phys owns nothing, an
7858 					 * obsolete ditto slot is freed
7859 					 * whatever its refcount, and any
7860 					 * other slot is freed once its last
7861 					 * reference is gone.
7862 					 */
7863 					if (ddt_phys_birth(&ddlwe.ddlwe_phys,
7864 					    v) == 0)
7865 						continue;
7866 					if (!DDT_PHYS_IS_DITTO(ddt, p) &&
7867 					    ddt_phys_refcnt(&ddlwe.ddlwe_phys,
7868 					    v) != 0)
7869 						continue;
7870 					blkptr_t blk;
7871 					ddt_bp_create(ddt->ddt_checksum,
7872 					    &ddlwe.ddlwe_key, &ddlwe.ddlwe_phys,
7873 					    v, &blk);
7874 					/* As ddt_phys_free() does at flush. */
7875 					BP_SET_DEDUP(&blk, 0);
7876 					zdb_count_block(zcb, NULL, &blk,
7877 					    ZDB_OT_DDT_PENDING);
7878 				}
7879 			}
7880 		}
7881 	}
7882 }
7883 
7884 static int
7885 livelist_entry_count_blocks_cb(void *args, dsl_deadlist_entry_t *dle)
7886 {
7887 	zdb_cb_t *zbc = args;
7888 	bplist_t blks;
7889 	bplist_create(&blks);
7890 	/* determine which blocks have been alloc'd but not freed */
7891 	VERIFY0(dsl_process_sub_livelist(&dle->dle_bpobj, &blks, NULL, NULL));
7892 	/* count those blocks */
7893 	(void) bplist_iterate(&blks, count_block_cb, zbc, NULL);
7894 	bplist_destroy(&blks);
7895 	return (0);
7896 }
7897 
7898 static void
7899 livelist_count_blocks(dsl_deadlist_t *ll, void *arg)
7900 {
7901 	dsl_deadlist_iterate(ll, livelist_entry_count_blocks_cb, arg);
7902 }
7903 
7904 /*
7905  * Count the blocks in the livelists that have been destroyed by the user
7906  * but haven't yet been freed.
7907  */
7908 static void
7909 deleted_livelists_count_blocks(spa_t *spa, zdb_cb_t *zbc)
7910 {
7911 	iterate_deleted_livelists(spa, livelist_count_blocks, zbc);
7912 }
7913 
7914 static void
7915 dump_livelist_cb(dsl_deadlist_t *ll, void *arg)
7916 {
7917 	ASSERT0P(arg);
7918 	global_feature_count[SPA_FEATURE_LIVELIST]++;
7919 	dump_blkptr_list(ll, "Deleted Livelist");
7920 	dsl_deadlist_iterate(ll, sublivelist_verify_lightweight, NULL);
7921 }
7922 
7923 /*
7924  * Print out, register object references to, and increment feature counts for
7925  * livelists that have been destroyed by the user but haven't yet been freed.
7926  */
7927 static void
7928 deleted_livelists_dump_mos(spa_t *spa)
7929 {
7930 	uint64_t zap_obj;
7931 	objset_t *mos = spa->spa_meta_objset;
7932 	int err = zap_lookup(mos, DMU_POOL_DIRECTORY_OBJECT,
7933 	    DMU_POOL_DELETED_CLONES, sizeof (uint64_t), 1, &zap_obj);
7934 	if (err == ENOENT)
7935 		return;
7936 	mos_obj_refd(zap_obj);
7937 	iterate_deleted_livelists(spa, dump_livelist_cb, NULL);
7938 }
7939 
7940 static int
7941 zdb_brt_entry_compare(const void *zcn1, const void *zcn2)
7942 {
7943 	const dva_t *dva1 = &((const zdb_brt_entry_t *)zcn1)->zbre_dva;
7944 	const dva_t *dva2 = &((const zdb_brt_entry_t *)zcn2)->zbre_dva;
7945 	int cmp;
7946 
7947 	cmp = TREE_CMP(DVA_GET_VDEV(dva1), DVA_GET_VDEV(dva2));
7948 	if (cmp == 0)
7949 		cmp = TREE_CMP(DVA_GET_OFFSET(dva1), DVA_GET_OFFSET(dva2));
7950 
7951 	return (cmp);
7952 }
7953 
7954 static int
7955 dump_block_stats(spa_t *spa)
7956 {
7957 	zdb_cb_t *zcb;
7958 	zdb_blkstats_t *zb, *tzb;
7959 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
7960 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
7961 	    TRAVERSE_NO_DECRYPT | TRAVERSE_HARD;
7962 	boolean_t leaks = B_FALSE;
7963 	int e, c, err;
7964 	bp_embedded_type_t i;
7965 
7966 	ddt_prefetch_all(spa);
7967 
7968 	zcb = umem_zalloc(sizeof (zdb_cb_t), UMEM_NOFAIL);
7969 
7970 	if (spa_feature_is_active(spa, SPA_FEATURE_BLOCK_CLONING)) {
7971 		avl_create(&zcb->zcb_brt, zdb_brt_entry_compare,
7972 		    sizeof (zdb_brt_entry_t),
7973 		    offsetof(zdb_brt_entry_t, zbre_node));
7974 		zcb->zcb_brt_is_active = B_TRUE;
7975 	}
7976 
7977 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
7978 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
7979 	    (dump_opt['c'] == 1) ? "metadata " : "",
7980 	    dump_opt['c'] ? "checksums " : "",
7981 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
7982 	    !dump_opt['L'] ? "nothing leaked " : "");
7983 
7984 	/*
7985 	 * When leak detection is enabled we load all space maps as SM_ALLOC
7986 	 * maps, then traverse the pool claiming each block we discover. If
7987 	 * the pool is perfectly consistent, the segment trees will be empty
7988 	 * when we're done. Anything left over is a leak; any block we can't
7989 	 * claim (because it's not part of any space map) is a double
7990 	 * allocation, reference to a freed block, or an unclaimed log block.
7991 	 *
7992 	 * When leak detection is disabled (-L option) we still traverse the
7993 	 * pool claiming each block we discover, but we skip opening any space
7994 	 * maps.
7995 	 */
7996 	zdb_leak_init(spa, zcb);
7997 
7998 	/*
7999 	 * If there's a deferred-free bplist, process that first.
8000 	 */
8001 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
8002 	    bpobj_count_block_cb, zcb, NULL);
8003 
8004 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
8005 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
8006 		    bpobj_count_block_cb, zcb, NULL);
8007 	}
8008 
8009 	zdb_count_ddt_log_frees(spa, zcb);
8010 
8011 	zdb_claim_removing(spa, zcb);
8012 
8013 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
8014 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
8015 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
8016 		    zcb, NULL));
8017 	}
8018 
8019 	deleted_livelists_count_blocks(spa, zcb);
8020 
8021 	if (dump_opt['c'] > 1)
8022 		flags |= TRAVERSE_PREFETCH_DATA;
8023 
8024 	zcb->zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
8025 	zcb->zcb_totalasize += metaslab_class_get_alloc(spa_special_class(spa));
8026 	zcb->zcb_totalasize += metaslab_class_get_alloc(spa_dedup_class(spa));
8027 	zcb->zcb_totalasize +=
8028 	    metaslab_class_get_alloc(spa_embedded_log_class(spa));
8029 	zcb->zcb_totalasize +=
8030 	    metaslab_class_get_alloc(spa_special_embedded_log_class(spa));
8031 	zcb->zcb_start = zcb->zcb_lastprint = gethrtime();
8032 	err = traverse_pool(spa, 0, flags, zdb_blkptr_cb, zcb);
8033 
8034 	/*
8035 	 * If we've traversed the data blocks then we need to wait for those
8036 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
8037 	 * all async I/Os to complete.
8038 	 */
8039 	if (dump_opt['c']) {
8040 		for (c = 0; c < max_ncpus; c++) {
8041 			(void) zio_wait(spa->spa_async_zio_root[c]);
8042 			spa->spa_async_zio_root[c] = zio_root(spa, NULL, NULL,
8043 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
8044 			    ZIO_FLAG_GODFATHER);
8045 		}
8046 	}
8047 	ASSERT0(spa->spa_load_verify_bytes);
8048 
8049 	/*
8050 	 * Done after zio_wait() since zcb_haderrors is modified in
8051 	 * zdb_blkptr_done()
8052 	 */
8053 	zcb->zcb_haderrors |= err;
8054 
8055 	if (zcb->zcb_haderrors) {
8056 		(void) printf("\nError counts:\n\n");
8057 		(void) printf("\t%5s  %s\n", "errno", "count");
8058 		for (e = 0; e < 256; e++) {
8059 			if (zcb->zcb_errors[e] != 0) {
8060 				(void) printf("\t%5d  %llu\n",
8061 				    e, (u_longlong_t)zcb->zcb_errors[e]);
8062 			}
8063 		}
8064 	}
8065 
8066 	/*
8067 	 * Report any leaked segments.
8068 	 */
8069 	leaks |= zdb_leak_fini(spa, zcb);
8070 
8071 	tzb = &zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
8072 
8073 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
8074 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
8075 
8076 	total_alloc = norm_alloc +
8077 	    metaslab_class_get_alloc(spa_log_class(spa)) +
8078 	    metaslab_class_get_alloc(spa_embedded_log_class(spa)) +
8079 	    metaslab_class_get_alloc(spa_special_embedded_log_class(spa)) +
8080 	    metaslab_class_get_alloc(spa_special_class(spa)) +
8081 	    metaslab_class_get_alloc(spa_dedup_class(spa)) +
8082 	    get_unflushed_alloc_space(spa);
8083 	total_found =
8084 	    tzb->zb_asize - zcb->zcb_dedup_asize - zcb->zcb_clone_asize +
8085 	    zcb->zcb_removing_size + zcb->zcb_checkpoint_size;
8086 
8087 	if (total_found == total_alloc && !dump_opt['L']) {
8088 		(void) printf("\n\tNo leaks (block sum matches space"
8089 		    " maps exactly)\n");
8090 	} else if (!dump_opt['L']) {
8091 		(void) printf("block traversal size %llu != alloc %llu "
8092 		    "(%s %lld)\n",
8093 		    (u_longlong_t)total_found,
8094 		    (u_longlong_t)total_alloc,
8095 		    (dump_opt['L']) ? "unreachable" : "leaked",
8096 		    (longlong_t)(total_alloc - total_found));
8097 	}
8098 
8099 	if (tzb->zb_count == 0) {
8100 		umem_free(zcb, sizeof (zdb_cb_t));
8101 		return (2);
8102 	}
8103 
8104 	(void) printf("\n");
8105 	(void) printf("\t%-16s %14llu\n", "bp count:",
8106 	    (u_longlong_t)tzb->zb_count);
8107 	(void) printf("\t%-16s %14llu\n", "ganged count:",
8108 	    (longlong_t)tzb->zb_gangs);
8109 	(void) printf("\t%-16s %14llu      avg: %6llu\n", "bp logical:",
8110 	    (u_longlong_t)tzb->zb_lsize,
8111 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
8112 	(void) printf("\t%-16s %14llu      avg: %6llu     compression: %6.2f\n",
8113 	    "bp physical:", (u_longlong_t)tzb->zb_psize,
8114 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
8115 	    (double)tzb->zb_lsize / tzb->zb_psize);
8116 	(void) printf("\t%-16s %14llu      avg: %6llu     compression: %6.2f\n",
8117 	    "bp allocated:", (u_longlong_t)tzb->zb_asize,
8118 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
8119 	    (double)tzb->zb_lsize / tzb->zb_asize);
8120 	(void) printf("\t%-16s %14llu    ref>1: %6llu   deduplication: %6.2f\n",
8121 	    "bp deduped:", (u_longlong_t)zcb->zcb_dedup_asize,
8122 	    (u_longlong_t)zcb->zcb_dedup_blocks,
8123 	    (double)zcb->zcb_dedup_asize / tzb->zb_asize + 1.0);
8124 	(void) printf("\t%-16s %14llu    count: %6llu\n",
8125 	    "bp cloned:", (u_longlong_t)zcb->zcb_clone_asize,
8126 	    (u_longlong_t)zcb->zcb_clone_blocks);
8127 	(void) printf("\t%-16s %14llu     used: %5.2f%%\n", "Normal class:",
8128 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
8129 
8130 	if (spa_special_class(spa)->mc_allocator[0].mca_rotor != NULL) {
8131 		uint64_t alloc = metaslab_class_get_alloc(
8132 		    spa_special_class(spa));
8133 		uint64_t space = metaslab_class_get_space(
8134 		    spa_special_class(spa));
8135 
8136 		(void) printf("\t%-16s %14llu     used: %5.2f%%\n",
8137 		    "Special class", (u_longlong_t)alloc,
8138 		    100.0 * alloc / space);
8139 	}
8140 
8141 	if (spa_dedup_class(spa)->mc_allocator[0].mca_rotor != NULL) {
8142 		uint64_t alloc = metaslab_class_get_alloc(
8143 		    spa_dedup_class(spa));
8144 		uint64_t space = metaslab_class_get_space(
8145 		    spa_dedup_class(spa));
8146 
8147 		(void) printf("\t%-16s %14llu     used: %5.2f%%\n",
8148 		    "Dedup class", (u_longlong_t)alloc,
8149 		    100.0 * alloc / space);
8150 	}
8151 
8152 	if (spa_embedded_log_class(spa)->mc_allocator[0].mca_rotor != NULL) {
8153 		uint64_t alloc = metaslab_class_get_alloc(
8154 		    spa_embedded_log_class(spa));
8155 		uint64_t space = metaslab_class_get_space(
8156 		    spa_embedded_log_class(spa));
8157 
8158 		(void) printf("\t%-16s %14llu     used: %5.2f%%\n",
8159 		    "Embedded log class", (u_longlong_t)alloc,
8160 		    100.0 * alloc / space);
8161 	}
8162 
8163 	if (spa_special_embedded_log_class(spa)->mc_allocator[0].mca_rotor
8164 	    != NULL) {
8165 		uint64_t alloc = metaslab_class_get_alloc(
8166 		    spa_special_embedded_log_class(spa));
8167 		uint64_t space = metaslab_class_get_space(
8168 		    spa_special_embedded_log_class(spa));
8169 
8170 		(void) printf("\t%-16s %14llu     used: %5.2f%%\n",
8171 		    "Special embedded log", (u_longlong_t)alloc,
8172 		    100.0 * alloc / space);
8173 	}
8174 
8175 	for (i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
8176 		if (zcb->zcb_embedded_blocks[i] == 0)
8177 			continue;
8178 		(void) printf("\n");
8179 		(void) printf("\tadditional, non-pointer bps of type %u: "
8180 		    "%10llu\n",
8181 		    i, (u_longlong_t)zcb->zcb_embedded_blocks[i]);
8182 
8183 		if (dump_opt['b'] >= 3) {
8184 			(void) printf("\t number of (compressed) bytes:  "
8185 			    "number of bps\n");
8186 			dump_histogram(zcb->zcb_embedded_histogram[i],
8187 			    sizeof (zcb->zcb_embedded_histogram[i]) /
8188 			    sizeof (zcb->zcb_embedded_histogram[i][0]), 0);
8189 		}
8190 	}
8191 
8192 	if (tzb->zb_ditto_samevdev != 0) {
8193 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
8194 		    (longlong_t)tzb->zb_ditto_samevdev);
8195 	}
8196 	if (tzb->zb_ditto_same_ms != 0) {
8197 		(void) printf("\tDittoed blocks in same metaslab: %llu\n",
8198 		    (longlong_t)tzb->zb_ditto_same_ms);
8199 	}
8200 
8201 	for (uint64_t v = 0; v < spa->spa_root_vdev->vdev_children; v++) {
8202 		vdev_t *vd = spa->spa_root_vdev->vdev_child[v];
8203 		vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
8204 
8205 		if (vim == NULL) {
8206 			continue;
8207 		}
8208 
8209 		char mem[32];
8210 		zdb_nicenum(vdev_indirect_mapping_num_entries(vim),
8211 		    mem, vdev_indirect_mapping_size(vim));
8212 
8213 		(void) printf("\tindirect vdev id %llu has %llu segments "
8214 		    "(%s in memory)\n",
8215 		    (longlong_t)vd->vdev_id,
8216 		    (longlong_t)vdev_indirect_mapping_num_entries(vim), mem);
8217 	}
8218 
8219 	if (dump_opt['b'] >= 2) {
8220 		int l, t, level;
8221 		char csize[32], lsize[32], psize[32], asize[32];
8222 		char avg[32], gang[32];
8223 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
8224 		    "\t  avg\t comp\t%%Total\tType\n");
8225 
8226 		zfs_blkstat_t *mdstats = umem_zalloc(sizeof (zfs_blkstat_t),
8227 		    UMEM_NOFAIL);
8228 
8229 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
8230 			const char *typename;
8231 
8232 			/* make sure nicenum has enough space */
8233 			_Static_assert(sizeof (csize) >= NN_NUMBUF_SZ,
8234 			    "csize truncated");
8235 			_Static_assert(sizeof (lsize) >= NN_NUMBUF_SZ,
8236 			    "lsize truncated");
8237 			_Static_assert(sizeof (psize) >= NN_NUMBUF_SZ,
8238 			    "psize truncated");
8239 			_Static_assert(sizeof (asize) >= NN_NUMBUF_SZ,
8240 			    "asize truncated");
8241 			_Static_assert(sizeof (avg) >= NN_NUMBUF_SZ,
8242 			    "avg truncated");
8243 			_Static_assert(sizeof (gang) >= NN_NUMBUF_SZ,
8244 			    "gang truncated");
8245 
8246 			if (t < DMU_OT_NUMTYPES)
8247 				typename = dmu_ot[t].ot_name;
8248 			else
8249 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
8250 
8251 			if (zcb->zcb_type[ZB_TOTAL][t].zb_asize == 0) {
8252 				(void) printf("%6s\t%5s\t%5s\t%5s"
8253 				    "\t%5s\t%5s\t%6s\t%s\n",
8254 				    "-",
8255 				    "-",
8256 				    "-",
8257 				    "-",
8258 				    "-",
8259 				    "-",
8260 				    "-",
8261 				    typename);
8262 				continue;
8263 			}
8264 
8265 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
8266 				level = (l == -1 ? ZB_TOTAL : l);
8267 				zb = &zcb->zcb_type[level][t];
8268 
8269 				if (zb->zb_asize == 0)
8270 					continue;
8271 
8272 				if (level != ZB_TOTAL && t < DMU_OT_NUMTYPES &&
8273 				    (level > 0 || DMU_OT_IS_METADATA(t))) {
8274 					mdstats->zb_count += zb->zb_count;
8275 					mdstats->zb_lsize += zb->zb_lsize;
8276 					mdstats->zb_psize += zb->zb_psize;
8277 					mdstats->zb_asize += zb->zb_asize;
8278 					mdstats->zb_gangs += zb->zb_gangs;
8279 				}
8280 
8281 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
8282 					continue;
8283 
8284 				if (level == 0 && zb->zb_asize ==
8285 				    zcb->zcb_type[ZB_TOTAL][t].zb_asize)
8286 					continue;
8287 
8288 				zdb_nicenum(zb->zb_count, csize,
8289 				    sizeof (csize));
8290 				zdb_nicenum(zb->zb_lsize, lsize,
8291 				    sizeof (lsize));
8292 				zdb_nicenum(zb->zb_psize, psize,
8293 				    sizeof (psize));
8294 				zdb_nicenum(zb->zb_asize, asize,
8295 				    sizeof (asize));
8296 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg,
8297 				    sizeof (avg));
8298 				zdb_nicenum(zb->zb_gangs, gang, sizeof (gang));
8299 
8300 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
8301 				    "\t%5.2f\t%6.2f\t",
8302 				    csize, lsize, psize, asize, avg,
8303 				    (double)zb->zb_lsize / zb->zb_psize,
8304 				    100.0 * zb->zb_asize / tzb->zb_asize);
8305 
8306 				if (level == ZB_TOTAL)
8307 					(void) printf("%s\n", typename);
8308 				else
8309 					(void) printf("    L%d %s\n",
8310 					    level, typename);
8311 
8312 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
8313 					(void) printf("\t number of ganged "
8314 					    "blocks: %s\n", gang);
8315 				}
8316 
8317 				if (dump_opt['b'] >= 4) {
8318 					(void) printf("psize "
8319 					    "(in 512-byte sectors): "
8320 					    "number of blocks\n");
8321 					dump_histogram(zb->zb_psize_histogram,
8322 					    PSIZE_HISTO_SIZE, 0);
8323 				}
8324 			}
8325 		}
8326 		zdb_nicenum(mdstats->zb_count, csize,
8327 		    sizeof (csize));
8328 		zdb_nicenum(mdstats->zb_lsize, lsize,
8329 		    sizeof (lsize));
8330 		zdb_nicenum(mdstats->zb_psize, psize,
8331 		    sizeof (psize));
8332 		zdb_nicenum(mdstats->zb_asize, asize,
8333 		    sizeof (asize));
8334 		zdb_nicenum(mdstats->zb_asize / mdstats->zb_count, avg,
8335 		    sizeof (avg));
8336 		zdb_nicenum(mdstats->zb_gangs, gang, sizeof (gang));
8337 
8338 		(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
8339 		    "\t%5.2f\t%6.2f\t",
8340 		    csize, lsize, psize, asize, avg,
8341 		    (double)mdstats->zb_lsize / mdstats->zb_psize,
8342 		    100.0 * mdstats->zb_asize / tzb->zb_asize);
8343 		(void) printf("%s\n", "Metadata Total");
8344 
8345 		/* Output a table summarizing block sizes in the pool */
8346 		if (dump_opt['b'] >= 2) {
8347 			dump_size_histograms(zcb);
8348 		}
8349 
8350 		umem_free(mdstats, sizeof (zfs_blkstat_t));
8351 	}
8352 
8353 	(void) printf("\n");
8354 
8355 	if (leaks) {
8356 		umem_free(zcb, sizeof (zdb_cb_t));
8357 		return (2);
8358 	}
8359 
8360 	if (zcb->zcb_haderrors) {
8361 		umem_free(zcb, sizeof (zdb_cb_t));
8362 		return (3);
8363 	}
8364 
8365 	umem_free(zcb, sizeof (zdb_cb_t));
8366 	return (0);
8367 }
8368 
8369 typedef struct zdb_ddt_entry {
8370 	/* key must be first for ddt_key_compare */
8371 	ddt_key_t	zdde_key;
8372 	uint64_t	zdde_ref_blocks;
8373 	uint64_t	zdde_ref_lsize;
8374 	uint64_t	zdde_ref_psize;
8375 	uint64_t	zdde_ref_dsize;
8376 	avl_node_t	zdde_node;
8377 } zdb_ddt_entry_t;
8378 
8379 static int
8380 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
8381     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
8382 {
8383 	(void) zilog, (void) dnp;
8384 	avl_tree_t *t = arg;
8385 	avl_index_t where;
8386 	zdb_ddt_entry_t *zdde, zdde_search;
8387 
8388 	if (zb->zb_level == ZB_DNODE_LEVEL || BP_IS_HOLE(bp) ||
8389 	    BP_IS_EMBEDDED(bp))
8390 		return (0);
8391 
8392 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
8393 		(void) printf("traversing objset %llu, %llu objects, "
8394 		    "%lu blocks so far\n",
8395 		    (u_longlong_t)zb->zb_objset,
8396 		    (u_longlong_t)BP_GET_FILL(bp),
8397 		    avl_numnodes(t));
8398 	}
8399 
8400 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
8401 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
8402 		return (0);
8403 
8404 	ddt_key_fill(&zdde_search.zdde_key, bp);
8405 
8406 	zdde = avl_find(t, &zdde_search, &where);
8407 
8408 	if (zdde == NULL) {
8409 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
8410 		zdde->zdde_key = zdde_search.zdde_key;
8411 		avl_insert(t, zdde, where);
8412 	}
8413 
8414 	zdde->zdde_ref_blocks += 1;
8415 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
8416 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
8417 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
8418 
8419 	return (0);
8420 }
8421 
8422 static void
8423 dump_simulated_ddt(spa_t *spa)
8424 {
8425 	avl_tree_t t;
8426 	void *cookie = NULL;
8427 	zdb_ddt_entry_t *zdde;
8428 	ddt_histogram_t ddh_total = {{{0}}};
8429 	ddt_stat_t dds_total = {0};
8430 
8431 	avl_create(&t, ddt_key_compare,
8432 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
8433 
8434 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
8435 
8436 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
8437 	    TRAVERSE_NO_DECRYPT, zdb_ddt_add_cb, &t);
8438 
8439 	spa_config_exit(spa, SCL_CONFIG, FTAG);
8440 
8441 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
8442 		uint64_t refcnt = zdde->zdde_ref_blocks;
8443 		ASSERT(refcnt != 0);
8444 
8445 		ddt_stat_t *dds = &ddh_total.ddh_stat[highbit64(refcnt) - 1];
8446 
8447 		dds->dds_blocks += zdde->zdde_ref_blocks / refcnt;
8448 		dds->dds_lsize += zdde->zdde_ref_lsize / refcnt;
8449 		dds->dds_psize += zdde->zdde_ref_psize / refcnt;
8450 		dds->dds_dsize += zdde->zdde_ref_dsize / refcnt;
8451 
8452 		dds->dds_ref_blocks += zdde->zdde_ref_blocks;
8453 		dds->dds_ref_lsize += zdde->zdde_ref_lsize;
8454 		dds->dds_ref_psize += zdde->zdde_ref_psize;
8455 		dds->dds_ref_dsize += zdde->zdde_ref_dsize;
8456 
8457 		umem_free(zdde, sizeof (*zdde));
8458 	}
8459 
8460 	avl_destroy(&t);
8461 
8462 	ddt_histogram_total(&dds_total, &ddh_total);
8463 
8464 	(void) printf("Simulated DDT histogram:\n");
8465 
8466 	zpool_dump_ddt(&dds_total, &ddh_total, dump_opt['P'] > 0);
8467 
8468 	dump_dedup_ratio(&dds_total);
8469 }
8470 
8471 static int
8472 verify_device_removal_feature_counts(spa_t *spa)
8473 {
8474 	uint64_t dr_feature_refcount = 0;
8475 	uint64_t oc_feature_refcount = 0;
8476 	uint64_t indirect_vdev_count = 0;
8477 	uint64_t precise_vdev_count = 0;
8478 	uint64_t obsolete_counts_object_count = 0;
8479 	uint64_t obsolete_sm_count = 0;
8480 	uint64_t obsolete_counts_count = 0;
8481 	uint64_t scip_count = 0;
8482 	uint64_t obsolete_bpobj_count = 0;
8483 	int ret = 0;
8484 
8485 	spa_condensing_indirect_phys_t *scip =
8486 	    &spa->spa_condensing_indirect_phys;
8487 	if (scip->scip_next_mapping_object != 0) {
8488 		vdev_t *vd = spa->spa_root_vdev->vdev_child[scip->scip_vdev];
8489 		ASSERT(scip->scip_prev_obsolete_sm_object != 0);
8490 		ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
8491 
8492 		(void) printf("Condensing indirect vdev %llu: new mapping "
8493 		    "object %llu, prev obsolete sm %llu\n",
8494 		    (u_longlong_t)scip->scip_vdev,
8495 		    (u_longlong_t)scip->scip_next_mapping_object,
8496 		    (u_longlong_t)scip->scip_prev_obsolete_sm_object);
8497 		if (scip->scip_prev_obsolete_sm_object != 0) {
8498 			space_map_t *prev_obsolete_sm = NULL;
8499 			VERIFY0(space_map_open(&prev_obsolete_sm,
8500 			    spa->spa_meta_objset,
8501 			    scip->scip_prev_obsolete_sm_object,
8502 			    0, vd->vdev_asize, 0));
8503 			dump_spacemap(spa->spa_meta_objset, prev_obsolete_sm);
8504 			(void) printf("\n");
8505 			space_map_close(prev_obsolete_sm);
8506 		}
8507 
8508 		scip_count += 2;
8509 	}
8510 
8511 	for (uint64_t i = 0; i < spa->spa_root_vdev->vdev_children; i++) {
8512 		vdev_t *vd = spa->spa_root_vdev->vdev_child[i];
8513 		vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
8514 
8515 		if (vic->vic_mapping_object != 0) {
8516 			ASSERT(vd->vdev_ops == &vdev_indirect_ops ||
8517 			    vd->vdev_removing);
8518 			indirect_vdev_count++;
8519 
8520 			if (vd->vdev_indirect_mapping->vim_havecounts) {
8521 				obsolete_counts_count++;
8522 			}
8523 		}
8524 
8525 		boolean_t are_precise;
8526 		VERIFY0(vdev_obsolete_counts_are_precise(vd, &are_precise));
8527 		if (are_precise) {
8528 			ASSERT(vic->vic_mapping_object != 0);
8529 			precise_vdev_count++;
8530 		}
8531 
8532 		uint64_t obsolete_sm_object;
8533 		VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object));
8534 		if (obsolete_sm_object != 0) {
8535 			ASSERT(vic->vic_mapping_object != 0);
8536 			obsolete_sm_count++;
8537 		}
8538 	}
8539 
8540 	(void) feature_get_refcount(spa,
8541 	    &spa_feature_table[SPA_FEATURE_DEVICE_REMOVAL],
8542 	    &dr_feature_refcount);
8543 	(void) feature_get_refcount(spa,
8544 	    &spa_feature_table[SPA_FEATURE_OBSOLETE_COUNTS],
8545 	    &oc_feature_refcount);
8546 
8547 	if (dr_feature_refcount != indirect_vdev_count) {
8548 		ret = 1;
8549 		(void) printf("Number of indirect vdevs (%llu) " \
8550 		    "does not match feature count (%llu)\n",
8551 		    (u_longlong_t)indirect_vdev_count,
8552 		    (u_longlong_t)dr_feature_refcount);
8553 	} else {
8554 		(void) printf("Verified device_removal feature refcount " \
8555 		    "of %llu is correct\n",
8556 		    (u_longlong_t)dr_feature_refcount);
8557 	}
8558 
8559 	if (zap_contains(spa_meta_objset(spa), DMU_POOL_DIRECTORY_OBJECT,
8560 	    DMU_POOL_OBSOLETE_BPOBJ) == 0) {
8561 		obsolete_bpobj_count++;
8562 	}
8563 
8564 
8565 	obsolete_counts_object_count = precise_vdev_count;
8566 	obsolete_counts_object_count += obsolete_sm_count;
8567 	obsolete_counts_object_count += obsolete_counts_count;
8568 	obsolete_counts_object_count += scip_count;
8569 	obsolete_counts_object_count += obsolete_bpobj_count;
8570 	obsolete_counts_object_count += remap_deadlist_count;
8571 
8572 	if (oc_feature_refcount != obsolete_counts_object_count) {
8573 		ret = 1;
8574 		(void) printf("Number of obsolete counts objects (%llu) " \
8575 		    "does not match feature count (%llu)\n",
8576 		    (u_longlong_t)obsolete_counts_object_count,
8577 		    (u_longlong_t)oc_feature_refcount);
8578 		(void) printf("pv:%llu os:%llu oc:%llu sc:%llu "
8579 		    "ob:%llu rd:%llu\n",
8580 		    (u_longlong_t)precise_vdev_count,
8581 		    (u_longlong_t)obsolete_sm_count,
8582 		    (u_longlong_t)obsolete_counts_count,
8583 		    (u_longlong_t)scip_count,
8584 		    (u_longlong_t)obsolete_bpobj_count,
8585 		    (u_longlong_t)remap_deadlist_count);
8586 	} else {
8587 		(void) printf("Verified indirect_refcount feature refcount " \
8588 		    "of %llu is correct\n",
8589 		    (u_longlong_t)oc_feature_refcount);
8590 	}
8591 	return (ret);
8592 }
8593 
8594 static void
8595 zdb_set_skip_mmp(char *target)
8596 {
8597 	spa_t *spa;
8598 
8599 	/*
8600 	 * Disable the activity check to allow examination of
8601 	 * active pools.
8602 	 */
8603 	spa_namespace_enter(FTAG);
8604 	if ((spa = spa_lookup(target)) != NULL) {
8605 		spa->spa_import_flags |= ZFS_IMPORT_SKIP_MMP;
8606 	}
8607 	spa_namespace_exit(FTAG);
8608 }
8609 
8610 #define	BOGUS_SUFFIX "_CHECKPOINTED_UNIVERSE"
8611 /*
8612  * Import the checkpointed state of the pool specified by the target
8613  * parameter as readonly. The function also accepts a pool config
8614  * as an optional parameter, else it attempts to infer the config by
8615  * the name of the target pool.
8616  *
8617  * Note that the checkpointed state's pool name will be the name of
8618  * the original pool with the above suffix appended to it. In addition,
8619  * if the target is not a pool name (e.g. a path to a dataset) then
8620  * the new_path parameter is populated with the updated path to
8621  * reflect the fact that we are looking into the checkpointed state.
8622  *
8623  * The function returns a newly-allocated copy of the name of the
8624  * pool containing the checkpointed state. When this copy is no
8625  * longer needed it should be freed with free(3C). Same thing
8626  * applies to the new_path parameter if allocated.
8627  */
8628 static char *
8629 import_checkpointed_state(char *target, nvlist_t *cfg, boolean_t target_is_spa,
8630     char **new_path)
8631 {
8632 	int error = 0;
8633 	char *poolname, *bogus_name = NULL;
8634 	boolean_t freecfg = B_FALSE;
8635 
8636 	/* If the target is not a pool, the extract the pool name */
8637 	char *path_start = strchr(target, '/');
8638 	if (target_is_spa || path_start == NULL) {
8639 		poolname = target;
8640 	} else {
8641 		size_t poolname_len = path_start - target;
8642 		poolname = strndup(target, poolname_len);
8643 	}
8644 
8645 	if (cfg == NULL) {
8646 		zdb_set_skip_mmp(poolname);
8647 		error = spa_get_stats(poolname, &cfg, NULL, 0);
8648 		if (error != 0) {
8649 			fatal("Tried to read config of pool \"%s\" but "
8650 			    "spa_get_stats() failed with error %d\n",
8651 			    poolname, error);
8652 		}
8653 		freecfg = B_TRUE;
8654 	}
8655 
8656 	if (asprintf(&bogus_name, "%s%s", poolname, BOGUS_SUFFIX) == -1) {
8657 		if (target != poolname)
8658 			free(poolname);
8659 		return (NULL);
8660 	}
8661 	fnvlist_add_string(cfg, ZPOOL_CONFIG_POOL_NAME, bogus_name);
8662 
8663 	error = spa_import(bogus_name, cfg, NULL,
8664 	    ZFS_IMPORT_MISSING_LOG | ZFS_IMPORT_CHECKPOINT |
8665 	    ZFS_IMPORT_SKIP_MMP);
8666 	if (freecfg)
8667 		nvlist_free(cfg);
8668 	if (error != 0) {
8669 		fatal("Tried to import pool \"%s\" but spa_import() failed "
8670 		    "with error %d\n", bogus_name, error);
8671 	}
8672 
8673 	if (new_path != NULL && !target_is_spa) {
8674 		if (asprintf(new_path, "%s%s", bogus_name,
8675 		    path_start != NULL ? path_start : "") == -1) {
8676 			free(bogus_name);
8677 			if (!target_is_spa && path_start != NULL)
8678 				free(poolname);
8679 			return (NULL);
8680 		}
8681 	}
8682 
8683 	if (target != poolname)
8684 		free(poolname);
8685 
8686 	return (bogus_name);
8687 }
8688 
8689 typedef struct verify_checkpoint_sm_entry_cb_arg {
8690 	vdev_t *vcsec_vd;
8691 
8692 	/* the following fields are only used for printing progress */
8693 	uint64_t vcsec_entryid;
8694 	uint64_t vcsec_num_entries;
8695 } verify_checkpoint_sm_entry_cb_arg_t;
8696 
8697 #define	ENTRIES_PER_PROGRESS_UPDATE 10000
8698 
8699 static int
8700 verify_checkpoint_sm_entry_cb(space_map_entry_t *sme, void *arg)
8701 {
8702 	verify_checkpoint_sm_entry_cb_arg_t *vcsec = arg;
8703 	vdev_t *vd = vcsec->vcsec_vd;
8704 	metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift];
8705 	uint64_t end = sme->sme_offset + sme->sme_run;
8706 
8707 	ASSERT(sme->sme_type == SM_FREE);
8708 
8709 	if ((vcsec->vcsec_entryid % ENTRIES_PER_PROGRESS_UPDATE) == 0) {
8710 		(void) fprintf(stderr,
8711 		    "\rverifying vdev %llu, space map entry %llu of %llu ...",
8712 		    (longlong_t)vd->vdev_id,
8713 		    (longlong_t)vcsec->vcsec_entryid,
8714 		    (longlong_t)vcsec->vcsec_num_entries);
8715 	}
8716 	vcsec->vcsec_entryid++;
8717 
8718 	/*
8719 	 * See comment in checkpoint_sm_exclude_entry_cb()
8720 	 */
8721 	VERIFY3U(sme->sme_offset, >=, ms->ms_start);
8722 	VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
8723 
8724 	/*
8725 	 * The entries in the vdev_checkpoint_sm should be marked as
8726 	 * allocated in the checkpointed state of the pool, therefore
8727 	 * their respective ms_allocateable trees should not contain them.
8728 	 */
8729 	mutex_enter(&ms->ms_lock);
8730 	zfs_range_tree_verify_not_present(ms->ms_allocatable,
8731 	    sme->sme_offset, sme->sme_run);
8732 	mutex_exit(&ms->ms_lock);
8733 
8734 	return (0);
8735 }
8736 
8737 /*
8738  * Verify that all segments in the vdev_checkpoint_sm are allocated
8739  * according to the checkpoint's ms_sm (i.e. are not in the checkpoint's
8740  * ms_allocatable).
8741  *
8742  * Do so by comparing the checkpoint space maps (vdev_checkpoint_sm) of
8743  * each vdev in the current state of the pool to the metaslab space maps
8744  * (ms_sm) of the checkpointed state of the pool.
8745  *
8746  * Note that the function changes the state of the ms_allocatable
8747  * trees of the current spa_t. The entries of these ms_allocatable
8748  * trees are cleared out and then repopulated from with the free
8749  * entries of their respective ms_sm space maps.
8750  */
8751 static void
8752 verify_checkpoint_vdev_spacemaps(spa_t *checkpoint, spa_t *current)
8753 {
8754 	vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
8755 	vdev_t *current_rvd = current->spa_root_vdev;
8756 
8757 	load_concrete_ms_allocatable_trees(checkpoint, SM_FREE);
8758 
8759 	for (uint64_t c = 0; c < ckpoint_rvd->vdev_children; c++) {
8760 		vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[c];
8761 		vdev_t *current_vd = current_rvd->vdev_child[c];
8762 
8763 		space_map_t *checkpoint_sm = NULL;
8764 		uint64_t checkpoint_sm_obj;
8765 
8766 		if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
8767 			/*
8768 			 * Since we don't allow device removal in a pool
8769 			 * that has a checkpoint, we expect that all removed
8770 			 * vdevs were removed from the pool before the
8771 			 * checkpoint.
8772 			 */
8773 			ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
8774 			continue;
8775 		}
8776 
8777 		/*
8778 		 * If the checkpoint space map doesn't exist, then nothing
8779 		 * here is checkpointed so there's nothing to verify.
8780 		 */
8781 		if (current_vd->vdev_top_zap == 0 ||
8782 		    zap_contains(spa_meta_objset(current),
8783 		    current_vd->vdev_top_zap,
8784 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
8785 			continue;
8786 
8787 		VERIFY0(zap_lookup(spa_meta_objset(current),
8788 		    current_vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
8789 		    sizeof (uint64_t), 1, &checkpoint_sm_obj));
8790 
8791 		VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(current),
8792 		    checkpoint_sm_obj, 0, current_vd->vdev_asize,
8793 		    current_vd->vdev_ashift));
8794 
8795 		verify_checkpoint_sm_entry_cb_arg_t vcsec;
8796 		vcsec.vcsec_vd = ckpoint_vd;
8797 		vcsec.vcsec_entryid = 0;
8798 		vcsec.vcsec_num_entries =
8799 		    space_map_length(checkpoint_sm) / sizeof (uint64_t);
8800 		VERIFY0(space_map_iterate(checkpoint_sm,
8801 		    space_map_length(checkpoint_sm),
8802 		    verify_checkpoint_sm_entry_cb, &vcsec));
8803 		if (dump_opt['m'] > 3)
8804 			dump_spacemap(current->spa_meta_objset, checkpoint_sm);
8805 		space_map_close(checkpoint_sm);
8806 	}
8807 
8808 	/*
8809 	 * If we've added vdevs since we took the checkpoint, ensure
8810 	 * that their checkpoint space maps are empty.
8811 	 */
8812 	if (ckpoint_rvd->vdev_children < current_rvd->vdev_children) {
8813 		for (uint64_t c = ckpoint_rvd->vdev_children;
8814 		    c < current_rvd->vdev_children; c++) {
8815 			vdev_t *current_vd = current_rvd->vdev_child[c];
8816 			VERIFY0P(current_vd->vdev_checkpoint_sm);
8817 		}
8818 	}
8819 
8820 	/* for cleaner progress output */
8821 	(void) fprintf(stderr, "\n");
8822 }
8823 
8824 /*
8825  * Verifies that all space that's allocated in the checkpoint is
8826  * still allocated in the current version, by checking that everything
8827  * in checkpoint's ms_allocatable (which is actually allocated, not
8828  * allocatable/free) is not present in current's ms_allocatable.
8829  *
8830  * Note that the function changes the state of the ms_allocatable
8831  * trees of both spas when called. The entries of all ms_allocatable
8832  * trees are cleared out and then repopulated from their respective
8833  * ms_sm space maps. In the checkpointed state we load the allocated
8834  * entries, and in the current state we load the free entries.
8835  */
8836 static void
8837 verify_checkpoint_ms_spacemaps(spa_t *checkpoint, spa_t *current)
8838 {
8839 	vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
8840 	vdev_t *current_rvd = current->spa_root_vdev;
8841 
8842 	load_concrete_ms_allocatable_trees(checkpoint, SM_ALLOC);
8843 	load_concrete_ms_allocatable_trees(current, SM_FREE);
8844 
8845 	for (uint64_t i = 0; i < ckpoint_rvd->vdev_children; i++) {
8846 		vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[i];
8847 		vdev_t *current_vd = current_rvd->vdev_child[i];
8848 
8849 		if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
8850 			/*
8851 			 * See comment in verify_checkpoint_vdev_spacemaps()
8852 			 */
8853 			ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
8854 			continue;
8855 		}
8856 
8857 		for (uint64_t m = 0; m < ckpoint_vd->vdev_ms_count; m++) {
8858 			metaslab_t *ckpoint_msp = ckpoint_vd->vdev_ms[m];
8859 			metaslab_t *current_msp = current_vd->vdev_ms[m];
8860 
8861 			(void) fprintf(stderr,
8862 			    "\rverifying vdev %llu of %llu, "
8863 			    "metaslab %llu of %llu ...",
8864 			    (longlong_t)current_vd->vdev_id,
8865 			    (longlong_t)current_rvd->vdev_children,
8866 			    (longlong_t)current_vd->vdev_ms[m]->ms_id,
8867 			    (longlong_t)current_vd->vdev_ms_count);
8868 
8869 			/*
8870 			 * We walk through the ms_allocatable trees that
8871 			 * are loaded with the allocated blocks from the
8872 			 * ms_sm spacemaps of the checkpoint. For each
8873 			 * one of these ranges we ensure that none of them
8874 			 * exists in the ms_allocatable trees of the
8875 			 * current state which are loaded with the ranges
8876 			 * that are currently free.
8877 			 *
8878 			 * This way we ensure that none of the blocks that
8879 			 * are part of the checkpoint were freed by mistake.
8880 			 */
8881 			zfs_range_tree_walk(ckpoint_msp->ms_allocatable,
8882 			    (zfs_range_tree_func_t *)
8883 			    zfs_range_tree_verify_not_present,
8884 			    current_msp->ms_allocatable);
8885 		}
8886 	}
8887 
8888 	/* for cleaner progress output */
8889 	(void) fprintf(stderr, "\n");
8890 }
8891 
8892 static void
8893 verify_checkpoint_blocks(spa_t *spa)
8894 {
8895 	ASSERT(!dump_opt['L']);
8896 
8897 	spa_t *checkpoint_spa;
8898 	char *checkpoint_pool;
8899 	int error = 0;
8900 
8901 	/*
8902 	 * We import the checkpointed state of the pool (under a different
8903 	 * name) so we can do verification on it against the current state
8904 	 * of the pool.
8905 	 */
8906 	checkpoint_pool = import_checkpointed_state(spa->spa_name, NULL, B_TRUE,
8907 	    NULL);
8908 	ASSERT(strcmp(spa->spa_name, checkpoint_pool) != 0);
8909 
8910 	error = spa_open(checkpoint_pool, &checkpoint_spa, FTAG);
8911 	if (error != 0) {
8912 		fatal("Tried to open pool \"%s\" but spa_open() failed with "
8913 		    "error %d\n", checkpoint_pool, error);
8914 	}
8915 
8916 	/*
8917 	 * Ensure that ranges in the checkpoint space maps of each vdev
8918 	 * are allocated according to the checkpointed state's metaslab
8919 	 * space maps.
8920 	 */
8921 	verify_checkpoint_vdev_spacemaps(checkpoint_spa, spa);
8922 
8923 	/*
8924 	 * Ensure that allocated ranges in the checkpoint's metaslab
8925 	 * space maps remain allocated in the metaslab space maps of
8926 	 * the current state.
8927 	 */
8928 	verify_checkpoint_ms_spacemaps(checkpoint_spa, spa);
8929 
8930 	/*
8931 	 * Once we are done, we get rid of the checkpointed state.
8932 	 */
8933 	spa_close(checkpoint_spa, FTAG);
8934 	free(checkpoint_pool);
8935 }
8936 
8937 static void
8938 dump_leftover_checkpoint_blocks(spa_t *spa)
8939 {
8940 	vdev_t *rvd = spa->spa_root_vdev;
8941 
8942 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
8943 		vdev_t *vd = rvd->vdev_child[i];
8944 
8945 		space_map_t *checkpoint_sm = NULL;
8946 		uint64_t checkpoint_sm_obj;
8947 
8948 		if (vd->vdev_top_zap == 0)
8949 			continue;
8950 
8951 		if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
8952 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
8953 			continue;
8954 
8955 		VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
8956 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
8957 		    sizeof (uint64_t), 1, &checkpoint_sm_obj));
8958 
8959 		VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
8960 		    checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
8961 		dump_spacemap(spa->spa_meta_objset, checkpoint_sm);
8962 		space_map_close(checkpoint_sm);
8963 	}
8964 }
8965 
8966 static int
8967 verify_checkpoint(spa_t *spa)
8968 {
8969 	uberblock_t checkpoint;
8970 	int error;
8971 
8972 	if (!spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT))
8973 		return (0);
8974 
8975 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
8976 	    DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t),
8977 	    sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint);
8978 
8979 	if (error == ENOENT && !dump_opt['L']) {
8980 		/*
8981 		 * If the feature is active but the uberblock is missing
8982 		 * then we must be in the middle of discarding the
8983 		 * checkpoint.
8984 		 */
8985 		(void) printf("\nPartially discarded checkpoint "
8986 		    "state found:\n");
8987 		if (dump_opt['m'] > 3)
8988 			dump_leftover_checkpoint_blocks(spa);
8989 		return (0);
8990 	} else if (error != 0) {
8991 		(void) printf("lookup error %d when looking for "
8992 		    "checkpointed uberblock in MOS\n", error);
8993 		return (error);
8994 	}
8995 	dump_uberblock(&checkpoint, "\nCheckpointed uberblock found:\n", "\n");
8996 
8997 	if (checkpoint.ub_checkpoint_txg == 0) {
8998 		(void) printf("\nub_checkpoint_txg not set in checkpointed "
8999 		    "uberblock\n");
9000 		error = 3;
9001 	}
9002 
9003 	if (error == 0 && !dump_opt['L'])
9004 		verify_checkpoint_blocks(spa);
9005 
9006 	return (error);
9007 }
9008 
9009 static void
9010 mos_leaks_cb(void *arg, uint64_t start, uint64_t size)
9011 {
9012 	(void) arg;
9013 	for (uint64_t i = start; i < size; i++) {
9014 		(void) printf("MOS object %llu referenced but not allocated\n",
9015 		    (u_longlong_t)i);
9016 	}
9017 }
9018 
9019 static void
9020 mos_obj_refd(uint64_t obj)
9021 {
9022 	if (obj != 0 && mos_refd_objs != NULL)
9023 		zfs_range_tree_add(mos_refd_objs, obj, 1);
9024 }
9025 
9026 /*
9027  * Call on a MOS object that may already have been referenced.
9028  */
9029 static void
9030 mos_obj_refd_multiple(uint64_t obj)
9031 {
9032 	if (obj != 0 && mos_refd_objs != NULL &&
9033 	    !zfs_range_tree_contains(mos_refd_objs, obj, 1))
9034 		zfs_range_tree_add(mos_refd_objs, obj, 1);
9035 }
9036 
9037 static void
9038 dump_mos_leaked_object_details(objset_t *mos, uint64_t object,
9039     const dmu_object_info_t *doi)
9040 {
9041 	if (dump_opt['d'] < 2)
9042 		return;
9043 
9044 	if (doi->doi_type == DMU_OT_DSL_CLONES) {
9045 		uint64_t entries = 0;
9046 		int error = zap_count(mos, object, &entries);
9047 		if (error == 0) {
9048 			(void) printf("\tleak detail: clone entries=%llu\n",
9049 			    (u_longlong_t)entries);
9050 		} else {
9051 			(void) printf("\tleak detail: clone entry count "
9052 			    "failed: %s\n", strerror(error));
9053 		}
9054 		return;
9055 	}
9056 
9057 	if (doi->doi_type == DMU_OT_SPACE_MAP &&
9058 	    doi->doi_bonus_size == sizeof (space_map_phys_t)) {
9059 		dmu_buf_t *db = NULL;
9060 		int error = dmu_bonus_hold(mos, object, FTAG, &db);
9061 		if (error != 0) {
9062 			(void) printf("\tleak detail: space map bonus hold "
9063 			    "failed: %s\n", strerror(error));
9064 			return;
9065 		}
9066 
9067 		space_map_phys_t *smp = db->db_data;
9068 		(void) printf("\tleak detail: smp_alloc=0x%llx "
9069 		    "smp_length=0x%llx\n", (u_longlong_t)smp->smp_alloc,
9070 		    (u_longlong_t)smp->smp_length);
9071 		dmu_buf_rele(db, FTAG);
9072 	}
9073 }
9074 
9075 static void
9076 mos_leak_vdev_top_zap(vdev_t *vd)
9077 {
9078 	uint64_t ms_flush_data_obj;
9079 	int error = zap_lookup(spa_meta_objset(vd->vdev_spa),
9080 	    vd->vdev_top_zap, VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS,
9081 	    sizeof (ms_flush_data_obj), 1, &ms_flush_data_obj);
9082 	if (error == ENOENT)
9083 		return;
9084 	ASSERT0(error);
9085 
9086 	mos_obj_refd(ms_flush_data_obj);
9087 }
9088 
9089 static void
9090 mos_leak_vdev(vdev_t *vd)
9091 {
9092 	mos_obj_refd(vd->vdev_dtl_object);
9093 	mos_obj_refd(vd->vdev_ms_array);
9094 	mos_obj_refd(vd->vdev_indirect_config.vic_births_object);
9095 	mos_obj_refd(vd->vdev_indirect_config.vic_mapping_object);
9096 	mos_obj_refd(vd->vdev_leaf_zap);
9097 	if (vd->vdev_checkpoint_sm != NULL)
9098 		mos_obj_refd(vd->vdev_checkpoint_sm->sm_object);
9099 	if (vd->vdev_indirect_mapping != NULL) {
9100 		mos_obj_refd(vd->vdev_indirect_mapping->
9101 		    vim_phys->vimp_counts_object);
9102 	}
9103 	if (vd->vdev_obsolete_sm != NULL)
9104 		mos_obj_refd(vd->vdev_obsolete_sm->sm_object);
9105 
9106 	for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
9107 		metaslab_t *ms = vd->vdev_ms[m];
9108 		mos_obj_refd(space_map_object(ms->ms_sm));
9109 	}
9110 
9111 	if (vd->vdev_root_zap != 0)
9112 		mos_obj_refd(vd->vdev_root_zap);
9113 
9114 	if (vd->vdev_top_zap != 0) {
9115 		mos_obj_refd(vd->vdev_top_zap);
9116 		mos_leak_vdev_top_zap(vd);
9117 	}
9118 
9119 	for (uint64_t c = 0; c < vd->vdev_children; c++) {
9120 		mos_leak_vdev(vd->vdev_child[c]);
9121 	}
9122 }
9123 
9124 static void
9125 mos_leak_log_spacemaps(spa_t *spa)
9126 {
9127 	uint64_t spacemap_zap;
9128 	int error = zap_lookup(spa_meta_objset(spa),
9129 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_LOG_SPACEMAP_ZAP,
9130 	    sizeof (spacemap_zap), 1, &spacemap_zap);
9131 	if (error == ENOENT)
9132 		return;
9133 	ASSERT0(error);
9134 
9135 	mos_obj_refd(spacemap_zap);
9136 	for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg);
9137 	    sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls))
9138 		mos_obj_refd(sls->sls_sm_obj);
9139 }
9140 
9141 static void
9142 errorlog_count_refd(objset_t *mos, uint64_t errlog)
9143 {
9144 	zap_cursor_t zc;
9145 	zap_attribute_t *za = zap_attribute_alloc();
9146 	for (zap_cursor_init(&zc, mos, errlog);
9147 	    zap_cursor_retrieve(&zc, za) == 0;
9148 	    zap_cursor_advance(&zc)) {
9149 		mos_obj_refd(za->za_first_integer);
9150 	}
9151 	zap_cursor_fini(&zc);
9152 	zap_attribute_free(za);
9153 }
9154 
9155 static int
9156 dump_mos_leaks(spa_t *spa)
9157 {
9158 	int rv = 0;
9159 	objset_t *mos = spa->spa_meta_objset;
9160 	dsl_pool_t *dp = spa->spa_dsl_pool;
9161 
9162 	/* Visit and mark all referenced objects in the MOS */
9163 
9164 	mos_obj_refd(DMU_POOL_DIRECTORY_OBJECT);
9165 	mos_obj_refd(spa->spa_pool_props_object);
9166 	mos_obj_refd(spa->spa_config_object);
9167 	mos_obj_refd(spa->spa_ddt_stat_object);
9168 	mos_obj_refd(spa->spa_feat_desc_obj);
9169 	mos_obj_refd(spa->spa_feat_enabled_txg_obj);
9170 	mos_obj_refd(spa->spa_feat_for_read_obj);
9171 	mos_obj_refd(spa->spa_feat_for_write_obj);
9172 	mos_obj_refd(spa->spa_history);
9173 	mos_obj_refd(spa->spa_errlog_last);
9174 	mos_obj_refd(spa->spa_errlog_scrub);
9175 
9176 	if (spa_feature_is_enabled(spa, SPA_FEATURE_HEAD_ERRLOG)) {
9177 		errorlog_count_refd(mos, spa->spa_errlog_last);
9178 		errorlog_count_refd(mos, spa->spa_errlog_scrub);
9179 	}
9180 
9181 	mos_obj_refd(spa->spa_all_vdev_zaps);
9182 	mos_obj_refd(spa->spa_dsl_pool->dp_bptree_obj);
9183 	mos_obj_refd(spa->spa_dsl_pool->dp_tmp_userrefs_obj);
9184 	mos_obj_refd(spa->spa_dsl_pool->dp_scan->scn_phys.scn_queue_obj);
9185 	bpobj_count_refd(&spa->spa_deferred_bpobj);
9186 	mos_obj_refd(dp->dp_empty_bpobj);
9187 	bpobj_count_refd(&dp->dp_obsolete_bpobj);
9188 	bpobj_count_refd(&dp->dp_free_bpobj);
9189 	mos_obj_refd(spa->spa_l2cache.sav_object);
9190 	mos_obj_refd(spa->spa_spares.sav_object);
9191 
9192 	if (spa->spa_syncing_log_sm != NULL)
9193 		mos_obj_refd(spa->spa_syncing_log_sm->sm_object);
9194 	mos_leak_log_spacemaps(spa);
9195 
9196 	mos_obj_refd(spa->spa_condensing_indirect_phys.
9197 	    scip_next_mapping_object);
9198 	mos_obj_refd(spa->spa_condensing_indirect_phys.
9199 	    scip_prev_obsolete_sm_object);
9200 	if (spa->spa_condensing_indirect_phys.scip_next_mapping_object != 0) {
9201 		vdev_indirect_mapping_t *vim =
9202 		    vdev_indirect_mapping_open(mos,
9203 		    spa->spa_condensing_indirect_phys.scip_next_mapping_object);
9204 		mos_obj_refd(vim->vim_phys->vimp_counts_object);
9205 		vdev_indirect_mapping_close(vim);
9206 	}
9207 	deleted_livelists_dump_mos(spa);
9208 
9209 	if (dp->dp_origin_snap != NULL) {
9210 		dsl_dataset_t *ds;
9211 
9212 		dsl_pool_config_enter(dp, FTAG);
9213 		VERIFY0(dsl_dataset_hold_obj(dp,
9214 		    dsl_dataset_phys(dp->dp_origin_snap)->ds_next_snap_obj,
9215 		    FTAG, &ds));
9216 		count_ds_mos_objects(ds);
9217 		dump_blkptr_list(&ds->ds_deadlist, "Deadlist");
9218 		dsl_dataset_rele(ds, FTAG);
9219 		dsl_pool_config_exit(dp, FTAG);
9220 
9221 		count_ds_mos_objects(dp->dp_origin_snap);
9222 		dump_blkptr_list(&dp->dp_origin_snap->ds_deadlist, "Deadlist");
9223 	}
9224 	count_dir_mos_objects(dp->dp_mos_dir);
9225 	if (dp->dp_free_dir != NULL)
9226 		count_dir_mos_objects(dp->dp_free_dir);
9227 	if (dp->dp_leak_dir != NULL)
9228 		count_dir_mos_objects(dp->dp_leak_dir);
9229 
9230 	mos_leak_vdev(spa->spa_root_vdev);
9231 
9232 	for (uint64_t c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
9233 		ddt_t *ddt = spa->spa_ddt[c];
9234 		if (!ddt || ddt->ddt_version == DDT_VERSION_UNCONFIGURED)
9235 			continue;
9236 
9237 		/* DDT store objects */
9238 		for (ddt_type_t type = 0; type < DDT_TYPES; type++) {
9239 			for (ddt_class_t class = 0; class < DDT_CLASSES;
9240 			    class++) {
9241 				mos_obj_refd(ddt->ddt_object[type][class]);
9242 			}
9243 		}
9244 
9245 		/* FDT container */
9246 		if (ddt->ddt_version == DDT_VERSION_FDT)
9247 			mos_obj_refd(ddt->ddt_dir_object);
9248 
9249 		/* FDT log objects */
9250 		if (ddt->ddt_flags & DDT_FLAG_LOG) {
9251 			mos_obj_refd(ddt->ddt_log[0].ddl_object);
9252 			mos_obj_refd(ddt->ddt_log[1].ddl_object);
9253 		}
9254 	}
9255 
9256 	for (uint64_t vdevid = 0; vdevid < spa->spa_brt_nvdevs; vdevid++) {
9257 		brt_vdev_t *brtvd = spa->spa_brt_vdevs[vdevid];
9258 		if (brtvd->bv_initiated) {
9259 			mos_obj_refd(brtvd->bv_mos_brtvdev);
9260 			mos_obj_refd(brtvd->bv_mos_entries);
9261 		}
9262 	}
9263 
9264 	/*
9265 	 * Visit all allocated objects and make sure they are referenced.
9266 	 */
9267 	uint64_t object = 0;
9268 	while (dmu_object_next(mos, &object, B_FALSE, 0) == 0) {
9269 		if (zfs_range_tree_contains(mos_refd_objs, object, 1)) {
9270 			zfs_range_tree_remove(mos_refd_objs, object, 1);
9271 		} else {
9272 			dmu_object_info_t doi;
9273 			const char *name;
9274 			VERIFY0(dmu_object_info(mos, object, &doi));
9275 			if (doi.doi_type & DMU_OT_NEWTYPE) {
9276 				dmu_object_byteswap_t bswap =
9277 				    DMU_OT_BYTESWAP(doi.doi_type);
9278 				name = dmu_ot_byteswap[bswap].ob_name;
9279 			} else {
9280 				name = dmu_ot[doi.doi_type].ot_name;
9281 			}
9282 
9283 			(void) printf("MOS object %llu (%s) leaked\n",
9284 			    (u_longlong_t)object, name);
9285 			dump_mos_leaked_object_details(mos, object,
9286 			    &doi);
9287 			rv = 2;
9288 		}
9289 	}
9290 	(void) zfs_range_tree_walk(mos_refd_objs, mos_leaks_cb, NULL);
9291 	if (!zfs_range_tree_is_empty(mos_refd_objs))
9292 		rv = 2;
9293 	zfs_range_tree_vacate(mos_refd_objs, NULL, NULL);
9294 	zfs_range_tree_destroy(mos_refd_objs);
9295 	return (rv);
9296 }
9297 
9298 typedef struct log_sm_obsolete_stats_arg {
9299 	uint64_t lsos_current_txg;
9300 
9301 	uint64_t lsos_total_entries;
9302 	uint64_t lsos_valid_entries;
9303 
9304 	uint64_t lsos_sm_entries;
9305 	uint64_t lsos_valid_sm_entries;
9306 } log_sm_obsolete_stats_arg_t;
9307 
9308 static int
9309 log_spacemap_obsolete_stats_cb(spa_t *spa, space_map_entry_t *sme,
9310     uint64_t txg, void *arg)
9311 {
9312 	log_sm_obsolete_stats_arg_t *lsos = arg;
9313 
9314 	uint64_t offset = sme->sme_offset;
9315 	uint64_t vdev_id = sme->sme_vdev;
9316 
9317 	if (lsos->lsos_current_txg == 0) {
9318 		/* this is the first log */
9319 		lsos->lsos_current_txg = txg;
9320 	} else if (lsos->lsos_current_txg < txg) {
9321 		/* we just changed log - print stats and reset */
9322 		(void) printf("%-8llu valid entries out of %-8llu - txg %llu\n",
9323 		    (u_longlong_t)lsos->lsos_valid_sm_entries,
9324 		    (u_longlong_t)lsos->lsos_sm_entries,
9325 		    (u_longlong_t)lsos->lsos_current_txg);
9326 		lsos->lsos_valid_sm_entries = 0;
9327 		lsos->lsos_sm_entries = 0;
9328 		lsos->lsos_current_txg = txg;
9329 	}
9330 	ASSERT3U(lsos->lsos_current_txg, ==, txg);
9331 
9332 	lsos->lsos_sm_entries++;
9333 	lsos->lsos_total_entries++;
9334 
9335 	vdev_t *vd = vdev_lookup_top(spa, vdev_id);
9336 	if (!vdev_is_concrete(vd))
9337 		return (0);
9338 
9339 	metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
9340 	ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE);
9341 
9342 	if (txg < metaslab_unflushed_txg(ms))
9343 		return (0);
9344 	lsos->lsos_valid_sm_entries++;
9345 	lsos->lsos_valid_entries++;
9346 	return (0);
9347 }
9348 
9349 static void
9350 dump_log_spacemap_obsolete_stats(spa_t *spa)
9351 {
9352 	if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))
9353 		return;
9354 
9355 	log_sm_obsolete_stats_arg_t lsos = {0};
9356 
9357 	(void) printf("Log Space Map Obsolete Entry Statistics:\n");
9358 
9359 	iterate_through_spacemap_logs(spa,
9360 	    log_spacemap_obsolete_stats_cb, &lsos);
9361 
9362 	/* print stats for latest log */
9363 	(void) printf("%-8llu valid entries out of %-8llu - txg %llu\n",
9364 	    (u_longlong_t)lsos.lsos_valid_sm_entries,
9365 	    (u_longlong_t)lsos.lsos_sm_entries,
9366 	    (u_longlong_t)lsos.lsos_current_txg);
9367 
9368 	(void) printf("%-8llu valid entries out of %-8llu - total\n\n",
9369 	    (u_longlong_t)lsos.lsos_valid_entries,
9370 	    (u_longlong_t)lsos.lsos_total_entries);
9371 }
9372 
9373 static void
9374 dump_zpool(spa_t *spa)
9375 {
9376 	dsl_pool_t *dp = spa_get_dsl(spa);
9377 	int rc = 0;
9378 
9379 	if (dump_opt['y']) {
9380 		livelist_metaslab_validate(spa);
9381 	}
9382 
9383 	if (dump_opt['S']) {
9384 		dump_simulated_ddt(spa);
9385 		return;
9386 	}
9387 
9388 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
9389 		(void) printf("\nCached configuration:\n");
9390 		dump_nvlist(spa->spa_config, 8);
9391 	}
9392 
9393 	if (dump_opt['C'])
9394 		dump_config(spa);
9395 
9396 	if (dump_opt['u'])
9397 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
9398 
9399 	if (dump_opt['D'])
9400 		dump_all_ddts(spa);
9401 
9402 	if (dump_opt['T'])
9403 		dump_brt(spa);
9404 
9405 	if (dump_opt['d'] > 2 || dump_opt['m'])
9406 		dump_metaslabs(spa);
9407 	if (dump_opt['M'])
9408 		dump_metaslab_groups(spa, dump_opt['M'] > 1);
9409 	if (dump_opt['d'] > 2 || dump_opt['m']) {
9410 		dump_log_spacemaps(spa);
9411 		dump_log_spacemap_obsolete_stats(spa);
9412 	}
9413 
9414 	if (dump_opt['d'] || dump_opt['i']) {
9415 		spa_feature_t f;
9416 		mos_refd_objs = zfs_range_tree_create_flags(
9417 		    NULL, ZFS_RANGE_SEG64, NULL, 0, 0,
9418 		    0, "dump_zpool:mos_refd_objs");
9419 		dump_objset(dp->dp_meta_objset);
9420 
9421 		if (dump_opt['d'] >= 3) {
9422 			dsl_pool_t *dp = spa->spa_dsl_pool;
9423 			dump_full_bpobj(&spa->spa_deferred_bpobj,
9424 			    "Deferred frees", 0);
9425 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
9426 				dump_full_bpobj(&dp->dp_free_bpobj,
9427 				    "Pool snapshot frees", 0);
9428 			}
9429 			if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
9430 				ASSERT(spa_feature_is_enabled(spa,
9431 				    SPA_FEATURE_DEVICE_REMOVAL));
9432 				dump_full_bpobj(&dp->dp_obsolete_bpobj,
9433 				    "Pool obsolete blocks", 0);
9434 			}
9435 
9436 			if (spa_feature_is_active(spa,
9437 			    SPA_FEATURE_ASYNC_DESTROY)) {
9438 				dump_bptree(spa->spa_meta_objset,
9439 				    dp->dp_bptree_obj,
9440 				    "Pool dataset frees");
9441 			}
9442 			dump_dtl(spa->spa_root_vdev, 0);
9443 		}
9444 
9445 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++)
9446 			global_feature_count[f] = UINT64_MAX;
9447 		global_feature_count[SPA_FEATURE_REDACTION_BOOKMARKS] = 0;
9448 		global_feature_count[SPA_FEATURE_REDACTION_LIST_SPILL] = 0;
9449 		global_feature_count[SPA_FEATURE_BOOKMARK_WRITTEN] = 0;
9450 		global_feature_count[SPA_FEATURE_LIVELIST] = 0;
9451 
9452 		(void) dmu_objset_find(spa_name(spa), dump_one_objset,
9453 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
9454 
9455 		if (rc == 0 && !dump_opt['L'])
9456 			rc = dump_mos_leaks(spa);
9457 
9458 		for (f = 0; f < SPA_FEATURES; f++) {
9459 			uint64_t refcount;
9460 
9461 			uint64_t *arr;
9462 			if (!(spa_feature_table[f].fi_flags &
9463 			    ZFEATURE_FLAG_PER_DATASET)) {
9464 				if (global_feature_count[f] == UINT64_MAX)
9465 					continue;
9466 				if (!spa_feature_is_enabled(spa, f)) {
9467 					ASSERT0(global_feature_count[f]);
9468 					continue;
9469 				}
9470 				arr = global_feature_count;
9471 			} else {
9472 				if (!spa_feature_is_enabled(spa, f)) {
9473 					ASSERT0(dataset_feature_count[f]);
9474 					continue;
9475 				}
9476 				arr = dataset_feature_count;
9477 			}
9478 			if (feature_get_refcount(spa, &spa_feature_table[f],
9479 			    &refcount) == ENOTSUP)
9480 				continue;
9481 			if (arr[f] != refcount) {
9482 				(void) printf("%s feature refcount mismatch: "
9483 				    "%lld consumers != %lld refcount\n",
9484 				    spa_feature_table[f].fi_uname,
9485 				    (longlong_t)arr[f], (longlong_t)refcount);
9486 				rc = 2;
9487 			} else {
9488 				(void) printf("Verified %s feature refcount "
9489 				    "of %llu is correct\n",
9490 				    spa_feature_table[f].fi_uname,
9491 				    (longlong_t)refcount);
9492 			}
9493 		}
9494 
9495 		if (rc == 0)
9496 			rc = verify_device_removal_feature_counts(spa);
9497 	}
9498 
9499 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
9500 		rc = dump_block_stats(spa);
9501 
9502 	if (rc == 0)
9503 		rc = verify_spacemap_refcounts(spa);
9504 
9505 	if (dump_opt['s'])
9506 		show_pool_stats(spa);
9507 
9508 	if (dump_opt['h'])
9509 		dump_history(spa);
9510 
9511 	if (rc == 0)
9512 		rc = verify_checkpoint(spa);
9513 
9514 	if (rc != 0) {
9515 		dump_debug_buffer();
9516 		zdb_exit(rc);
9517 	}
9518 }
9519 
9520 #define	ZDB_FLAG_CHECKSUM	0x0001
9521 #define	ZDB_FLAG_DECOMPRESS	0x0002
9522 #define	ZDB_FLAG_BSWAP		0x0004
9523 #define	ZDB_FLAG_GBH		0x0008
9524 #define	ZDB_FLAG_INDIRECT	0x0010
9525 #define	ZDB_FLAG_RAW		0x0020
9526 #define	ZDB_FLAG_PRINT_BLKPTR	0x0040
9527 #define	ZDB_FLAG_VERBOSE	0x0080
9528 
9529 static int flagbits[256];
9530 static char flagbitstr[16];
9531 
9532 static void
9533 zdb_print_blkptr(const blkptr_t *bp, int flags)
9534 {
9535 	char blkbuf[BP_SPRINTF_LEN];
9536 
9537 	if (flags & ZDB_FLAG_BSWAP)
9538 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
9539 
9540 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
9541 	(void) printf("%s\n", blkbuf);
9542 }
9543 
9544 static void
9545 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
9546 {
9547 	int i;
9548 
9549 	for (i = 0; i < nbps; i++)
9550 		zdb_print_blkptr(&bp[i], flags);
9551 }
9552 
9553 static void
9554 zdb_dump_gbh(void *buf, uint64_t size, int flags)
9555 {
9556 	zdb_dump_indirect((blkptr_t *)buf, gbh_nblkptrs(size), flags);
9557 }
9558 
9559 static void
9560 zdb_dump_block_raw(void *buf, uint64_t size, int flags)
9561 {
9562 	if (flags & ZDB_FLAG_BSWAP)
9563 		byteswap_uint64_array(buf, size);
9564 	VERIFY(write(fileno(stdout), buf, size) == size);
9565 }
9566 
9567 static void
9568 zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
9569 {
9570 	uint64_t *d = (uint64_t *)buf;
9571 	unsigned nwords = size / sizeof (uint64_t);
9572 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
9573 	unsigned i, j;
9574 	const char *hdr;
9575 	char *c;
9576 
9577 
9578 	if (do_bswap)
9579 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
9580 	else
9581 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
9582 
9583 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
9584 
9585 #ifdef _ZFS_LITTLE_ENDIAN
9586 	/* correct the endianness */
9587 	do_bswap = !do_bswap;
9588 #endif
9589 	for (i = 0; i < nwords; i += 2) {
9590 		(void) printf("%06llx:  %016llx  %016llx  ",
9591 		    (u_longlong_t)(i * sizeof (uint64_t)),
9592 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
9593 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
9594 
9595 		c = (char *)&d[i];
9596 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
9597 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
9598 		(void) printf("\n");
9599 	}
9600 }
9601 
9602 /*
9603  * There are two acceptable formats:
9604  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
9605  *	child[.child]*    - For example: 0.1.1
9606  *
9607  * The second form can be used to specify arbitrary vdevs anywhere
9608  * in the hierarchy.  For example, in a pool with a mirror of
9609  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
9610  */
9611 static vdev_t *
9612 zdb_vdev_lookup(vdev_t *vdev, const char *path)
9613 {
9614 	char *s, *p, *q;
9615 	unsigned i;
9616 
9617 	if (vdev == NULL)
9618 		return (NULL);
9619 
9620 	/* First, assume the x.x.x.x format */
9621 	i = strtoul(path, &s, 10);
9622 	if (s == path || (s && *s != '.' && *s != '\0'))
9623 		goto name;
9624 	if (i >= vdev->vdev_children)
9625 		return (NULL);
9626 
9627 	vdev = vdev->vdev_child[i];
9628 	if (s && *s == '\0')
9629 		return (vdev);
9630 	return (zdb_vdev_lookup(vdev, s+1));
9631 
9632 name:
9633 	for (i = 0; i < vdev->vdev_children; i++) {
9634 		vdev_t *vc = vdev->vdev_child[i];
9635 
9636 		if (vc->vdev_path == NULL) {
9637 			vc = zdb_vdev_lookup(vc, path);
9638 			if (vc == NULL)
9639 				continue;
9640 			else
9641 				return (vc);
9642 		}
9643 
9644 		p = strrchr(vc->vdev_path, '/');
9645 		p = p ? p + 1 : vc->vdev_path;
9646 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
9647 
9648 		if (strcmp(vc->vdev_path, path) == 0)
9649 			return (vc);
9650 		if (strcmp(p, path) == 0)
9651 			return (vc);
9652 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
9653 			return (vc);
9654 	}
9655 
9656 	return (NULL);
9657 }
9658 
9659 static int
9660 name_from_objset_id(spa_t *spa, uint64_t objset_id, char *outstr)
9661 {
9662 	dsl_dataset_t *ds;
9663 
9664 	dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
9665 	int error = dsl_dataset_hold_obj(spa->spa_dsl_pool, objset_id,
9666 	    NULL, &ds);
9667 	if (error != 0) {
9668 		(void) fprintf(stderr, "failed to hold objset %llu: %s\n",
9669 		    (u_longlong_t)objset_id, strerror(error));
9670 		dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
9671 		return (error);
9672 	}
9673 	dsl_dataset_name(ds, outstr);
9674 	dsl_dataset_rele(ds, NULL);
9675 	dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
9676 	return (0);
9677 }
9678 
9679 static boolean_t
9680 zdb_parse_block_sizes(char *sizes, uint64_t *lsize, uint64_t *psize)
9681 {
9682 	char *s0, *s1, *tmp = NULL;
9683 
9684 	if (sizes == NULL)
9685 		return (B_FALSE);
9686 
9687 	s0 = strtok_r(sizes, "/", &tmp);
9688 	if (s0 == NULL)
9689 		return (B_FALSE);
9690 	s1 = strtok_r(NULL, "/", &tmp);
9691 	*lsize = strtoull(s0, NULL, 16);
9692 	*psize = s1 ? strtoull(s1, NULL, 16) : *lsize;
9693 	return (*lsize >= *psize && *psize > 0);
9694 }
9695 
9696 #define	ZIO_COMPRESS_MASK(alg)	(1ULL << (ZIO_COMPRESS_##alg))
9697 
9698 static boolean_t
9699 try_decompress_block(abd_t *pabd, uint64_t lsize, uint64_t psize,
9700     int flags, int cfunc, void *lbuf, void *lbuf2)
9701 {
9702 	if (flags & ZDB_FLAG_VERBOSE) {
9703 		(void) fprintf(stderr,
9704 		    "Trying %05llx -> %05llx (%s)\n",
9705 		    (u_longlong_t)psize,
9706 		    (u_longlong_t)lsize,
9707 		    zio_compress_table[cfunc].ci_name);
9708 	}
9709 
9710 	/*
9711 	 * We set lbuf to all zeros and lbuf2 to all
9712 	 * ones, then decompress to both buffers and
9713 	 * compare their contents. This way we can
9714 	 * know if decompression filled exactly to
9715 	 * lsize or if it left some bytes unwritten.
9716 	 */
9717 
9718 	memset(lbuf, 0x00, lsize);
9719 	memset(lbuf2, 0xff, lsize);
9720 
9721 	abd_t labd, labd2;
9722 	abd_get_from_buf_struct(&labd, lbuf, lsize);
9723 	abd_get_from_buf_struct(&labd2, lbuf2, lsize);
9724 
9725 	boolean_t ret = B_FALSE;
9726 	if (zio_decompress_data(cfunc, pabd,
9727 	    &labd, psize, lsize, NULL) == 0 &&
9728 	    zio_decompress_data(cfunc, pabd,
9729 	    &labd2, psize, lsize, NULL) == 0 &&
9730 	    memcmp(lbuf, lbuf2, lsize) == 0)
9731 		ret = B_TRUE;
9732 
9733 	abd_free(&labd2);
9734 	abd_free(&labd);
9735 
9736 	return (ret);
9737 }
9738 
9739 static uint64_t
9740 zdb_decompress_block(abd_t *pabd, void *buf, void *lbuf, uint64_t lsize,
9741     uint64_t psize, int flags)
9742 {
9743 	(void) buf;
9744 	uint64_t orig_lsize = lsize;
9745 	boolean_t tryzle = ((getenv("ZDB_NO_ZLE") == NULL));
9746 	/*
9747 	 * We don't know how the data was compressed, so just try
9748 	 * every decompress function at every inflated blocksize.
9749 	 */
9750 	void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
9751 	int cfuncs[ZIO_COMPRESS_FUNCTIONS] = { 0 };
9752 	int *cfuncp = cfuncs;
9753 	uint64_t maxlsize = SPA_MAXBLOCKSIZE;
9754 	uint64_t mask = ZIO_COMPRESS_MASK(ON) | ZIO_COMPRESS_MASK(OFF) |
9755 	    ZIO_COMPRESS_MASK(INHERIT) | ZIO_COMPRESS_MASK(EMPTY) |
9756 	    ZIO_COMPRESS_MASK(ZLE);
9757 	*cfuncp++ = ZIO_COMPRESS_LZ4;
9758 	*cfuncp++ = ZIO_COMPRESS_LZJB;
9759 	mask |= ZIO_COMPRESS_MASK(LZ4) | ZIO_COMPRESS_MASK(LZJB);
9760 	/*
9761 	 * Every gzip level has the same decompressor, no need to
9762 	 * run it 9 times per bruteforce attempt.
9763 	 */
9764 	mask |= ZIO_COMPRESS_MASK(GZIP_2) | ZIO_COMPRESS_MASK(GZIP_3);
9765 	mask |= ZIO_COMPRESS_MASK(GZIP_4) | ZIO_COMPRESS_MASK(GZIP_5);
9766 	mask |= ZIO_COMPRESS_MASK(GZIP_6) | ZIO_COMPRESS_MASK(GZIP_7);
9767 	mask |= ZIO_COMPRESS_MASK(GZIP_8) | ZIO_COMPRESS_MASK(GZIP_9);
9768 	for (int c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++)
9769 		if (((1ULL << c) & mask) == 0)
9770 			*cfuncp++ = c;
9771 
9772 	/*
9773 	 * On the one hand, with SPA_MAXBLOCKSIZE at 16MB, this
9774 	 * could take a while and we should let the user know
9775 	 * we are not stuck.  On the other hand, printing progress
9776 	 * info gets old after a while.  User can specify 'v' flag
9777 	 * to see the progression.
9778 	 */
9779 	if (lsize == psize)
9780 		lsize += SPA_MINBLOCKSIZE;
9781 	else
9782 		maxlsize = lsize;
9783 
9784 	for (; lsize <= maxlsize; lsize += SPA_MINBLOCKSIZE) {
9785 		for (cfuncp = cfuncs; *cfuncp; cfuncp++) {
9786 			if (try_decompress_block(pabd, lsize, psize, flags,
9787 			    *cfuncp, lbuf, lbuf2)) {
9788 				tryzle = B_FALSE;
9789 				break;
9790 			}
9791 		}
9792 		if (*cfuncp != 0)
9793 			break;
9794 	}
9795 	if (tryzle) {
9796 		for (lsize = orig_lsize; lsize <= maxlsize;
9797 		    lsize += SPA_MINBLOCKSIZE) {
9798 			if (try_decompress_block(pabd, lsize, psize, flags,
9799 			    ZIO_COMPRESS_ZLE, lbuf, lbuf2)) {
9800 				*cfuncp = ZIO_COMPRESS_ZLE;
9801 				break;
9802 			}
9803 		}
9804 	}
9805 	umem_free(lbuf2, SPA_MAXBLOCKSIZE);
9806 
9807 	if (*cfuncp == ZIO_COMPRESS_ZLE) {
9808 		printf("\nZLE decompression was selected. If you "
9809 		    "suspect the results are wrong,\ntry avoiding ZLE "
9810 		    "by setting and exporting ZDB_NO_ZLE=\"true\"\n");
9811 	}
9812 
9813 	return (lsize > maxlsize ? -1 : lsize);
9814 }
9815 
9816 /*
9817  * Read a block from a pool and print it out.  The syntax of the
9818  * block descriptor is:
9819  *
9820  *	pool:vdev_specifier:offset:[lsize/]psize[:flags]
9821  *
9822  *	pool           - The name of the pool you wish to read from
9823  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
9824  *	offset         - offset, in hex, in bytes
9825  *	size           - Amount of data to read, in hex, in bytes
9826  *	flags          - A string of characters specifying options
9827  *		 b: Decode a blkptr at given offset within block
9828  *		 c: Calculate and display checksums
9829  *		 d: Decompress data before dumping
9830  *		 e: Byteswap data before dumping
9831  *		 g: Display data as a gang block header
9832  *		 i: Display as an indirect block
9833  *		 r: Dump raw data to stdout
9834  *		 v: Verbose
9835  *
9836  */
9837 static void
9838 zdb_read_block(char *thing, spa_t *spa)
9839 {
9840 	blkptr_t blk, *bp = &blk;
9841 	dva_t *dva = bp->blk_dva;
9842 	int flags = 0;
9843 	uint64_t offset = 0, psize = 0, lsize = 0, blkptr_offset = 0;
9844 	zio_t *zio;
9845 	vdev_t *vd;
9846 	abd_t *pabd;
9847 	void *lbuf, *buf;
9848 	char *s, *p, *dup, *flagstr, *sizes, *tmp = NULL;
9849 	const char *vdev, *errmsg = NULL;
9850 	int i, len, error;
9851 	boolean_t borrowed = B_FALSE, found = B_FALSE;
9852 
9853 	dup = strdup(thing);
9854 	s = strtok_r(dup, ":", &tmp);
9855 	vdev = s ?: "";
9856 	s = strtok_r(NULL, ":", &tmp);
9857 	offset = strtoull(s ? s : "", NULL, 16);
9858 	sizes = strtok_r(NULL, ":", &tmp);
9859 	s = strtok_r(NULL, ":", &tmp);
9860 	flagstr = strdup(s ?: "");
9861 
9862 	if (!zdb_parse_block_sizes(sizes, &lsize, &psize))
9863 		errmsg = "invalid size(s)";
9864 	if (!IS_P2ALIGNED(psize, DEV_BSIZE) || !IS_P2ALIGNED(lsize, DEV_BSIZE))
9865 		errmsg = "size must be a multiple of sector size";
9866 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
9867 		errmsg = "offset must be a multiple of sector size";
9868 	if (errmsg) {
9869 		(void) printf("Invalid block specifier: %s  - %s\n",
9870 		    thing, errmsg);
9871 		goto done;
9872 	}
9873 
9874 	tmp = NULL;
9875 	for (s = strtok_r(flagstr, ":", &tmp);
9876 	    s != NULL;
9877 	    s = strtok_r(NULL, ":", &tmp)) {
9878 		len = strlen(flagstr);
9879 		for (i = 0; i < len; i++) {
9880 			int bit = flagbits[(uchar_t)flagstr[i]];
9881 
9882 			if (bit == 0) {
9883 				(void) printf("***Ignoring flag: %c\n",
9884 				    (uchar_t)flagstr[i]);
9885 				continue;
9886 			}
9887 			found = B_TRUE;
9888 			flags |= bit;
9889 
9890 			p = &flagstr[i + 1];
9891 			if (*p != ':' && *p != '\0') {
9892 				int j = 0, nextbit = flagbits[(uchar_t)*p];
9893 				char *end, offstr[8] = { 0 };
9894 				if ((bit == ZDB_FLAG_PRINT_BLKPTR) &&
9895 				    (nextbit == 0)) {
9896 					/* look ahead to isolate the offset */
9897 					while (nextbit == 0 &&
9898 					    strchr(flagbitstr, *p) == NULL) {
9899 						offstr[j] = *p;
9900 						j++;
9901 						if (i + j > strlen(flagstr))
9902 							break;
9903 						p++;
9904 						nextbit = flagbits[(uchar_t)*p];
9905 					}
9906 					blkptr_offset = strtoull(offstr, &end,
9907 					    16);
9908 					i += j;
9909 				} else if (nextbit == 0) {
9910 					(void) printf("***Ignoring flag arg:"
9911 					    " '%c'\n", (uchar_t)*p);
9912 				}
9913 			}
9914 		}
9915 	}
9916 	if (blkptr_offset % sizeof (blkptr_t)) {
9917 		printf("Block pointer offset 0x%llx "
9918 		    "must be divisible by 0x%x\n",
9919 		    (longlong_t)blkptr_offset, (int)sizeof (blkptr_t));
9920 		goto done;
9921 	}
9922 	if (found == B_FALSE && strlen(flagstr) > 0) {
9923 		printf("Invalid flag arg: '%s'\n", flagstr);
9924 		goto done;
9925 	}
9926 
9927 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
9928 	if (vd == NULL) {
9929 		(void) printf("***Invalid vdev: %s\n", vdev);
9930 		goto done;
9931 	} else {
9932 		if (vd->vdev_path)
9933 			(void) fprintf(stderr, "Found vdev: %s\n",
9934 			    vd->vdev_path);
9935 		else
9936 			(void) fprintf(stderr, "Found vdev type: %s\n",
9937 			    vd->vdev_ops->vdev_op_type);
9938 	}
9939 
9940 	pabd = abd_alloc_for_io(SPA_MAXBLOCKSIZE, B_FALSE);
9941 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
9942 
9943 	BP_ZERO(bp);
9944 
9945 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
9946 	DVA_SET_OFFSET(&dva[0], offset);
9947 	DVA_SET_GANG(&dva[0], 0);
9948 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
9949 
9950 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
9951 
9952 	BP_SET_LSIZE(bp, lsize);
9953 	BP_SET_PSIZE(bp, psize);
9954 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
9955 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
9956 	BP_SET_TYPE(bp, DMU_OT_NONE);
9957 	BP_SET_LEVEL(bp, 0);
9958 	BP_SET_DEDUP(bp, 0);
9959 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
9960 
9961 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
9962 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
9963 
9964 	if (vd == vd->vdev_top) {
9965 		/*
9966 		 * Treat this as a normal block read.
9967 		 */
9968 		zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL,
9969 		    ZIO_PRIORITY_SYNC_READ,
9970 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
9971 	} else {
9972 		/*
9973 		 * Treat this as a vdev child I/O.
9974 		 */
9975 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd,
9976 		    psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
9977 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
9978 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW | ZIO_FLAG_OPTIONAL,
9979 		    NULL, NULL));
9980 	}
9981 
9982 	error = zio_wait(zio);
9983 	spa_config_exit(spa, SCL_STATE, FTAG);
9984 
9985 	if (error) {
9986 		(void) printf("Read of %s failed, error: %d\n", thing, error);
9987 		goto out;
9988 	}
9989 
9990 	uint64_t orig_lsize = lsize;
9991 	buf = lbuf;
9992 	if (flags & ZDB_FLAG_DECOMPRESS) {
9993 		lsize = zdb_decompress_block(pabd, buf, lbuf,
9994 		    lsize, psize, flags);
9995 		if (lsize == -1) {
9996 			(void) printf("Decompress of %s failed\n", thing);
9997 			goto out;
9998 		}
9999 	} else {
10000 		buf = abd_borrow_buf_copy(pabd, lsize);
10001 		borrowed = B_TRUE;
10002 	}
10003 	/*
10004 	 * Try to detect invalid block pointer.  If invalid, try
10005 	 * decompressing.
10006 	 */
10007 	if ((flags & ZDB_FLAG_PRINT_BLKPTR || flags & ZDB_FLAG_INDIRECT) &&
10008 	    !(flags & ZDB_FLAG_DECOMPRESS)) {
10009 		const blkptr_t *b = (const blkptr_t *)(void *)
10010 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset);
10011 		if (zfs_blkptr_verify(spa, b,
10012 		    BLK_CONFIG_NEEDED, BLK_VERIFY_ONLY)) {
10013 			abd_return_buf_copy(pabd, buf, lsize);
10014 			borrowed = B_FALSE;
10015 			buf = lbuf;
10016 			lsize = zdb_decompress_block(pabd, buf,
10017 			    lbuf, lsize, psize, flags);
10018 			b = (const blkptr_t *)(void *)
10019 			    ((uintptr_t)buf + (uintptr_t)blkptr_offset);
10020 			if (lsize == -1 || zfs_blkptr_verify(spa, b,
10021 			    BLK_CONFIG_NEEDED, BLK_VERIFY_LOG)) {
10022 				printf("invalid block pointer at this DVA\n");
10023 				goto out;
10024 			}
10025 		}
10026 	}
10027 
10028 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
10029 		zdb_print_blkptr((blkptr_t *)(void *)
10030 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
10031 	else if (flags & ZDB_FLAG_RAW)
10032 		zdb_dump_block_raw(buf, lsize, flags);
10033 	else if (flags & ZDB_FLAG_INDIRECT)
10034 		zdb_dump_indirect((blkptr_t *)buf,
10035 		    orig_lsize / sizeof (blkptr_t), flags);
10036 	else if (flags & ZDB_FLAG_GBH)
10037 		zdb_dump_gbh(buf, lsize, flags);
10038 	else
10039 		zdb_dump_block(thing, buf, lsize, flags);
10040 
10041 	/*
10042 	 * If :c was specified, iterate through the checksum table to
10043 	 * calculate and display each checksum for our specified
10044 	 * DVA and length.
10045 	 */
10046 	if ((flags & ZDB_FLAG_CHECKSUM) && !(flags & ZDB_FLAG_RAW) &&
10047 	    !(flags & ZDB_FLAG_GBH)) {
10048 		zio_t *czio;
10049 		(void) printf("\n");
10050 		for (enum zio_checksum ck = ZIO_CHECKSUM_LABEL;
10051 		    ck < ZIO_CHECKSUM_FUNCTIONS; ck++) {
10052 
10053 			if ((zio_checksum_table[ck].ci_flags &
10054 			    ZCHECKSUM_FLAG_EMBEDDED) ||
10055 			    ck == ZIO_CHECKSUM_NOPARITY) {
10056 				continue;
10057 			}
10058 			BP_SET_CHECKSUM(bp, ck);
10059 			spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
10060 			czio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
10061 			if (vd == vd->vdev_top) {
10062 				zio_nowait(zio_read(czio, spa, bp, pabd, psize,
10063 				    NULL, NULL,
10064 				    ZIO_PRIORITY_SYNC_READ,
10065 				    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW |
10066 				    ZIO_FLAG_DONT_RETRY, NULL));
10067 			} else {
10068 				zio_nowait(zio_vdev_child_io(czio, bp, vd,
10069 				    offset, pabd, psize, ZIO_TYPE_READ,
10070 				    ZIO_PRIORITY_SYNC_READ,
10071 				    ZIO_FLAG_DONT_PROPAGATE |
10072 				    ZIO_FLAG_DONT_RETRY |
10073 				    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW |
10074 				    ZIO_FLAG_SPECULATIVE |
10075 				    ZIO_FLAG_OPTIONAL, NULL, NULL));
10076 			}
10077 			error = zio_wait(czio);
10078 			if (error == 0 || error == ECKSUM) {
10079 				zio_t *ck_zio = zio_null(NULL, spa, NULL,
10080 				    NULL, NULL, 0);
10081 				ck_zio->io_offset =
10082 				    DVA_GET_OFFSET(&bp->blk_dva[0]);
10083 				ck_zio->io_bp = bp;
10084 				zio_checksum_compute(ck_zio, ck, pabd, psize);
10085 				printf(
10086 				    "%12s\t"
10087 				    "cksum=%016llx:%016llx:%016llx:%016llx\n",
10088 				    zio_checksum_table[ck].ci_name,
10089 				    (u_longlong_t)bp->blk_cksum.zc_word[0],
10090 				    (u_longlong_t)bp->blk_cksum.zc_word[1],
10091 				    (u_longlong_t)bp->blk_cksum.zc_word[2],
10092 				    (u_longlong_t)bp->blk_cksum.zc_word[3]);
10093 				zio_wait(ck_zio);
10094 			} else {
10095 				printf("error %d reading block\n", error);
10096 			}
10097 			spa_config_exit(spa, SCL_STATE, FTAG);
10098 		}
10099 	}
10100 
10101 	if (borrowed)
10102 		abd_return_buf_copy(pabd, buf, lsize);
10103 
10104 out:
10105 	abd_free(pabd);
10106 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
10107 done:
10108 	free(flagstr);
10109 	free(dup);
10110 }
10111 
10112 static void
10113 zdb_embedded_block(char *thing)
10114 {
10115 	blkptr_t bp = {{{{0}}}};
10116 	unsigned long long *words = (void *)&bp;
10117 	char *buf;
10118 	int err;
10119 
10120 	err = sscanf(thing, "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx:"
10121 	    "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx",
10122 	    words + 0, words + 1, words + 2, words + 3,
10123 	    words + 4, words + 5, words + 6, words + 7,
10124 	    words + 8, words + 9, words + 10, words + 11,
10125 	    words + 12, words + 13, words + 14, words + 15);
10126 	if (err != 16) {
10127 		(void) fprintf(stderr, "invalid input format\n");
10128 		zdb_exit(1);
10129 	}
10130 	ASSERT3U(BPE_GET_LSIZE(&bp), <=, SPA_MAXBLOCKSIZE);
10131 	buf = malloc(SPA_MAXBLOCKSIZE);
10132 	if (buf == NULL) {
10133 		(void) fprintf(stderr, "out of memory\n");
10134 		zdb_exit(1);
10135 	}
10136 	err = decode_embedded_bp(&bp, buf, BPE_GET_LSIZE(&bp));
10137 	if (err != 0) {
10138 		(void) fprintf(stderr, "decode failed: %u\n", err);
10139 		zdb_exit(1);
10140 	}
10141 	zdb_dump_block_raw(buf, BPE_GET_LSIZE(&bp), 0);
10142 	free(buf);
10143 }
10144 
10145 /* check for valid hex or decimal numeric string */
10146 static boolean_t
10147 zdb_numeric(char *str)
10148 {
10149 	int i = 0, len;
10150 
10151 	len = strlen(str);
10152 	if (len == 0)
10153 		return (B_FALSE);
10154 	if (strncmp(str, "0x", 2) == 0 || strncmp(str, "0X", 2) == 0)
10155 		i = 2;
10156 	for (; i < len; i++) {
10157 		if (!isxdigit(str[i]))
10158 			return (B_FALSE);
10159 	}
10160 	return (B_TRUE);
10161 }
10162 
10163 static int
10164 dummy_get_file_info(dmu_object_type_t bonustype, const void *data,
10165     zfs_file_info_t *zoi)
10166 {
10167 	(void) data, (void) zoi;
10168 
10169 	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
10170 		return (ENOENT);
10171 
10172 	(void) fprintf(stderr, "dummy_get_file_info: not implemented");
10173 	abort();
10174 }
10175 
10176 int
10177 main(int argc, char **argv)
10178 {
10179 	int c;
10180 	int dump_all = 1;
10181 	int verbose = 0;
10182 	int error = 0;
10183 	char **searchdirs = NULL;
10184 	int nsearch = 0;
10185 	char *target, *target_pool, dsname[ZFS_MAX_DATASET_NAME_LEN];
10186 	nvlist_t *policy = NULL;
10187 	uint64_t max_txg = UINT64_MAX;
10188 	int64_t objset_id = -1;
10189 	uint64_t object;
10190 	int flags = ZFS_IMPORT_MISSING_LOG;
10191 	int rewind = ZPOOL_NEVER_REWIND;
10192 	char *spa_config_path_env, *objset_str;
10193 	boolean_t target_is_spa = B_TRUE, dataset_lookup = B_FALSE;
10194 	nvlist_t *cfg = NULL;
10195 	struct sigaction action;
10196 	boolean_t force_import = B_FALSE;
10197 	boolean_t config_path_console = B_FALSE;
10198 	char pbuf[MAXPATHLEN];
10199 
10200 	dprintf_setup(&argc, argv);
10201 
10202 	/*
10203 	 * Set up signal handlers, so if we crash due to bad on-disk data we
10204 	 * can get more info. Unlike ztest, we don't bail out if we can't set
10205 	 * up signal handlers, because zdb is very useful without them.
10206 	 */
10207 	action.sa_handler = sig_handler;
10208 	sigemptyset(&action.sa_mask);
10209 	action.sa_flags = 0;
10210 	if (sigaction(SIGSEGV, &action, NULL) < 0) {
10211 		(void) fprintf(stderr, "zdb: cannot catch SIGSEGV: %s\n",
10212 		    strerror(errno));
10213 	}
10214 	if (sigaction(SIGABRT, &action, NULL) < 0) {
10215 		(void) fprintf(stderr, "zdb: cannot catch SIGABRT: %s\n",
10216 		    strerror(errno));
10217 	}
10218 
10219 	/*
10220 	 * If there is an environment variable SPA_CONFIG_PATH it overrides
10221 	 * default spa_config_path setting. If -U flag is specified it will
10222 	 * override this environment variable settings once again.
10223 	 */
10224 	spa_config_path_env = getenv("SPA_CONFIG_PATH");
10225 	if (spa_config_path_env != NULL)
10226 		spa_config_path = spa_config_path_env;
10227 
10228 	/*
10229 	 * For performance reasons, we set this tunable down. We do so before
10230 	 * the arg parsing section so that the user can override this value if
10231 	 * they choose.
10232 	 */
10233 	zfs_btree_verify_intensity = 3;
10234 
10235 	struct option long_options[] = {
10236 		{"ignore-assertions",	no_argument,		NULL, 'A'},
10237 		{"block-stats",		no_argument,		NULL, 'b'},
10238 		{"backup",		no_argument,		NULL, 'B'},
10239 		{"checksum",		no_argument,		NULL, 'c'},
10240 		{"config",		no_argument,		NULL, 'C'},
10241 		{"datasets",		no_argument,		NULL, 'd'},
10242 		{"dedup-stats",		no_argument,		NULL, 'D'},
10243 		{"exported",		no_argument,		NULL, 'e'},
10244 		{"embedded-block-pointer",	no_argument,	NULL, 'E'},
10245 		{"file-layout",		no_argument,		NULL, 'f'},
10246 		{"automatic-rewind",	no_argument,		NULL, 'F'},
10247 		{"dump-debug-msg",	no_argument,		NULL, 'G'},
10248 		{"history",		no_argument,		NULL, 'h'},
10249 		{"scripting-mode",	no_argument,		NULL, 'H'},
10250 		{"intent-logs",		no_argument,		NULL, 'i'},
10251 		{"inflight",		required_argument,	NULL, 'I'},
10252 		{"checkpointed-state",	no_argument,		NULL, 'k'},
10253 		{"key",			required_argument,	NULL, 'K'},
10254 		{"label",		no_argument,		NULL, 'l'},
10255 		{"disable-leak-tracking",	no_argument,	NULL, 'L'},
10256 		{"metaslabs",		no_argument,		NULL, 'm'},
10257 		{"metaslab-groups",	no_argument,		NULL, 'M'},
10258 		{"numeric",		no_argument,		NULL, 'N'},
10259 		{"option",		required_argument,	NULL, 'o'},
10260 		{"object-lookups",	no_argument,		NULL, 'O'},
10261 		{"path",		required_argument,	NULL, 'p'},
10262 		{"parseable",		no_argument,		NULL, 'P'},
10263 		{"skip-label",		no_argument,		NULL, 'q'},
10264 		{"copy-object",		no_argument,		NULL, 'r'},
10265 		{"read-block",		no_argument,		NULL, 'R'},
10266 		{"io-stats",		no_argument,		NULL, 's'},
10267 		{"simulate-dedup",	no_argument,		NULL, 'S'},
10268 		{"txg",			required_argument,	NULL, 't'},
10269 		{"brt-stats",		no_argument,		NULL, 'T'},
10270 		{"uberblock",		no_argument,		NULL, 'u'},
10271 		{"cachefile",		required_argument,	NULL, 'U'},
10272 		{"verbose",		no_argument,		NULL, 'v'},
10273 		{"verbatim",		no_argument,		NULL, 'V'},
10274 		{"dump-blocks",		required_argument,	NULL, 'x'},
10275 		{"extreme-rewind",	no_argument,		NULL, 'X'},
10276 		{"all-reconstruction",	no_argument,		NULL, 'Y'},
10277 		{"livelist",		no_argument,		NULL, 'y'},
10278 		{"zstd-headers",	no_argument,		NULL, 'Z'},
10279 		{"allocated-map",	no_argument,		NULL,
10280 		    ARG_ALLOCATED},
10281 		{"bin",			required_argument,	NULL,
10282 		    ARG_BLOCK_BIN_MODE},
10283 		{"class",		required_argument,	NULL,
10284 		    ARG_BLOCK_CLASSES},
10285 		{0, 0, 0, 0}
10286 	};
10287 
10288 	while ((c = getopt_long(argc, argv,
10289 	    "AbBcCdDeEfFGhHiI:kK:lLmMNo:Op:PqrRsSt:TuU:vVx:XYyZ",
10290 	    long_options, NULL)) != -1) {
10291 		switch (c) {
10292 		case 'b':
10293 		case 'B':
10294 		case 'c':
10295 		case 'C':
10296 		case 'd':
10297 		case 'D':
10298 		case 'E':
10299 		case 'f':
10300 		case 'G':
10301 		case 'h':
10302 		case 'i':
10303 		case 'l':
10304 		case 'm':
10305 		case 'M':
10306 		case 'N':
10307 		case 'O':
10308 		case 'r':
10309 		case 'R':
10310 		case 's':
10311 		case 'S':
10312 		case 'T':
10313 		case 'u':
10314 		case 'y':
10315 		case 'Z':
10316 		case ARG_ALLOCATED:
10317 			dump_opt[c]++;
10318 			dump_all = 0;
10319 			break;
10320 		case 'A':
10321 		case 'e':
10322 		case 'F':
10323 		case 'H':
10324 		case 'k':
10325 		case 'L':
10326 		case 'P':
10327 		case 'q':
10328 		case 'X':
10329 			dump_opt[c]++;
10330 			break;
10331 		case 'Y':
10332 			zfs_reconstruct_indirect_combinations_max = INT_MAX;
10333 			zfs_deadman_enabled = 0;
10334 			break;
10335 		/* NB: Sort single match options below. */
10336 		case 'I':
10337 			max_inflight_bytes = strtoull(optarg, NULL, 0);
10338 			if (max_inflight_bytes == 0) {
10339 				(void) fprintf(stderr, "maximum number "
10340 				    "of inflight bytes must be greater "
10341 				    "than 0\n");
10342 				usage();
10343 			}
10344 			break;
10345 		case 'K':
10346 			dump_opt[c]++;
10347 			key_material = strdup(optarg);
10348 			/* redact key material in process table */
10349 			while (*optarg != '\0') { *optarg++ = '*'; }
10350 			break;
10351 		case 'o':
10352 			dump_opt[c]++;
10353 			dump_all = 0;
10354 			error = handle_tunable_option(optarg, B_FALSE);
10355 			if (error != 0)
10356 				zdb_exit(1);
10357 			break;
10358 		case 'p':
10359 			if (searchdirs == NULL) {
10360 				searchdirs = umem_alloc(sizeof (char *),
10361 				    UMEM_NOFAIL);
10362 			} else {
10363 				char **tmp = umem_alloc((nsearch + 1) *
10364 				    sizeof (char *), UMEM_NOFAIL);
10365 				memcpy(tmp, searchdirs, nsearch *
10366 				    sizeof (char *));
10367 				umem_free(searchdirs,
10368 				    nsearch * sizeof (char *));
10369 				searchdirs = tmp;
10370 			}
10371 			searchdirs[nsearch++] = optarg;
10372 			break;
10373 		case 't':
10374 			max_txg = strtoull(optarg, NULL, 0);
10375 			if (max_txg < TXG_INITIAL) {
10376 				(void) fprintf(stderr, "incorrect txg "
10377 				    "specified: %s\n", optarg);
10378 				usage();
10379 			}
10380 			break;
10381 		case 'U':
10382 			config_path_console = B_TRUE;
10383 			spa_config_path = optarg;
10384 			if (spa_config_path[0] != '/') {
10385 				(void) fprintf(stderr,
10386 				    "cachefile must be an absolute path "
10387 				    "(i.e. start with a slash)\n");
10388 				usage();
10389 			}
10390 			break;
10391 		case 'v':
10392 			verbose++;
10393 			break;
10394 		case 'V':
10395 			flags = ZFS_IMPORT_VERBATIM;
10396 			break;
10397 		case 'x':
10398 			vn_dumpdir = optarg;
10399 			break;
10400 		case ARG_BLOCK_BIN_MODE:
10401 			if (strcmp(optarg, "lsize") == 0) {
10402 				block_bin_mode = BIN_LSIZE;
10403 			} else if (strcmp(optarg, "psize") == 0) {
10404 				block_bin_mode = BIN_PSIZE;
10405 			} else if (strcmp(optarg, "asize") == 0) {
10406 				block_bin_mode = BIN_ASIZE;
10407 			} else {
10408 				(void) fprintf(stderr,
10409 				    "--bin=\"%s\" must be one of \"lsize\", "
10410 				    "\"psize\" or \"asize\"\n", optarg);
10411 				usage();
10412 			}
10413 			break;
10414 
10415 		case ARG_BLOCK_CLASSES: {
10416 			char *buf = strdup(optarg), *tok = buf, *next,
10417 			    *save = NULL;
10418 
10419 			while ((next = strtok_r(tok, ",", &save)) != NULL) {
10420 				tok = NULL;
10421 
10422 				if (strcmp(next, "normal") == 0) {
10423 					block_classes |= CLASS_NORMAL;
10424 				} else if (strcmp(next, "special") == 0) {
10425 					block_classes |= CLASS_SPECIAL;
10426 				} else if (strcmp(next, "dedup") == 0) {
10427 					block_classes |= CLASS_DEDUP;
10428 				} else if (strcmp(next, "other") == 0) {
10429 					block_classes |= CLASS_OTHER;
10430 				} else {
10431 					(void) fprintf(stderr,
10432 					    "--class=\"%s\" must be a "
10433 					    "comma-separated list of either "
10434 					    "\"normal\", \"special\", "
10435 					    "\"asize\" or \"other\"; "
10436 					    "got \"%s\"\n",
10437 					    optarg, next);
10438 					usage();
10439 				}
10440 			}
10441 
10442 			if (block_classes == 0) {
10443 				(void) fprintf(stderr,
10444 				    "--class= must be a comma-separated "
10445 				    "list of either \"normal\", \"special\", "
10446 				    "\"asize\" or \"other\"; got empty\n");
10447 				usage();
10448 			}
10449 
10450 			free(buf);
10451 			break;
10452 		}
10453 		default:
10454 			usage();
10455 			break;
10456 		}
10457 	}
10458 
10459 	if (!dump_opt['e'] && searchdirs != NULL) {
10460 		(void) fprintf(stderr, "-p option requires use of -e\n");
10461 		usage();
10462 	}
10463 	if (dump_opt['H'] && !dump_opt['f']) {
10464 		(void) fprintf(stderr, "-H option requires use of -f\n");
10465 		usage();
10466 	}
10467 #if defined(_LP64)
10468 	/*
10469 	 * ZDB does not typically re-read blocks; therefore limit the ARC
10470 	 * to 256 MB, which can be used entirely for metadata.
10471 	 */
10472 	zfs_arc_min = 2ULL << SPA_MAXBLOCKSHIFT;
10473 	zfs_arc_max = 256 * 1024 * 1024;
10474 #endif
10475 
10476 	/*
10477 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
10478 	 * "zdb -b" uses traversal prefetch which uses async reads.
10479 	 * For good performance, let several of them be active at once.
10480 	 */
10481 	zfs_vdev_async_read_max_active = 10;
10482 
10483 	/*
10484 	 * Disable reference tracking for better performance.
10485 	 */
10486 	reference_tracking_enable = B_FALSE;
10487 
10488 	/*
10489 	 * Do not fail spa_load when spa_load_verify fails. This is needed
10490 	 * to load non-idle pools.
10491 	 */
10492 	spa_load_verify_dryrun = B_TRUE;
10493 
10494 	/*
10495 	 * ZDB should have ability to read spacemaps.
10496 	 */
10497 	spa_mode_readable_spacemaps = B_TRUE;
10498 
10499 	libspl_set_assert_ok((dump_opt['A'] == 1) || (dump_opt['A'] > 2));
10500 	zfs_recover = (dump_opt['A'] > 1);
10501 
10502 	if (dump_all)
10503 		verbose = MAX(verbose, 1);
10504 
10505 	for (c = 0; c < 256; c++) {
10506 		if (dump_all && strchr("ABeEfFkKlLNOPrRSXy", c) == NULL)
10507 			dump_opt[c] = 1;
10508 		if (dump_opt[c])
10509 			dump_opt[c] += verbose;
10510 	}
10511 
10512 	argc -= optind;
10513 	argv += optind;
10514 	if (argc < 2 && dump_opt['R'])
10515 		usage();
10516 
10517 	target = argv[0];
10518 
10519 	/*
10520 	 * Automate cachefile
10521 	 */
10522 	if (!spa_config_path_env && !config_path_console && target &&
10523 	    !dump_opt['l'] && libzfs_core_init() == 0) {
10524 		char *pname = strdup(target);
10525 		const char *value;
10526 		nvlist_t *pnvl = NULL;
10527 		nvlist_t *vnvl = NULL;
10528 
10529 		if (strpbrk(pname, "/@") != NULL)
10530 			*strpbrk(pname, "/@") = '\0';
10531 
10532 		if (pname && lzc_get_props(pname, &pnvl) == 0) {
10533 			if (nvlist_lookup_nvlist(pnvl, "cachefile",
10534 			    &vnvl) == 0) {
10535 				value = fnvlist_lookup_string(vnvl,
10536 				    ZPROP_VALUE);
10537 			} else {
10538 				value = "-";
10539 			}
10540 			strlcpy(pbuf, value, sizeof (pbuf));
10541 			if (pbuf[0] != '\0') {
10542 				if (pbuf[0] == '/') {
10543 					if (access(pbuf, F_OK) == 0)
10544 						spa_config_path = pbuf;
10545 					else
10546 						force_import = B_TRUE;
10547 				} else if ((strcmp(pbuf, "-") == 0 &&
10548 				    access(ZPOOL_CACHE, F_OK) != 0) ||
10549 				    strcmp(pbuf, "none") == 0) {
10550 					force_import = B_TRUE;
10551 				}
10552 			}
10553 			nvlist_free(vnvl);
10554 		}
10555 
10556 		free(pname);
10557 		nvlist_free(pnvl);
10558 		libzfs_core_fini();
10559 	}
10560 
10561 	dmu_objset_register_type(DMU_OST_ZFS, dummy_get_file_info);
10562 	kernel_init(SPA_MODE_READ);
10563 	kernel_init_done = B_TRUE;
10564 
10565 	if (dump_opt['E']) {
10566 		if (argc != 1)
10567 			usage();
10568 		zdb_embedded_block(argv[0]);
10569 		error = 0;
10570 		goto fini;
10571 	}
10572 
10573 	if (argc < 1) {
10574 		if (!dump_opt['e'] && dump_opt['C']) {
10575 			dump_cachefile(spa_config_path);
10576 			error = 0;
10577 			goto fini;
10578 		}
10579 		if (dump_opt['o'])
10580 			/*
10581 			 * Avoid blasting tunable options off the top of the
10582 			 * screen.
10583 			 */
10584 			zdb_exit(1);
10585 		usage();
10586 	}
10587 
10588 	if (dump_opt['l']) {
10589 		error = dump_label(argv[0]);
10590 		goto fini;
10591 	}
10592 
10593 	if (dump_opt['X'] || dump_opt['F'])
10594 		rewind = ZPOOL_DO_REWIND |
10595 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
10596 
10597 	/* -N implies -d */
10598 	if (dump_opt['N'] && dump_opt['d'] == 0)
10599 		dump_opt['d'] = dump_opt['N'];
10600 
10601 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
10602 	    nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, max_txg) != 0 ||
10603 	    nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, rewind) != 0)
10604 		fatal("internal error: %s", strerror(ENOMEM));
10605 
10606 	error = 0;
10607 
10608 	if (strpbrk(target, "/@") != NULL) {
10609 		size_t targetlen;
10610 
10611 		target_pool = strdup(target);
10612 		*strpbrk(target_pool, "/@") = '\0';
10613 
10614 		target_is_spa = B_FALSE;
10615 		targetlen = strlen(target);
10616 		if (targetlen && target[targetlen - 1] == '/')
10617 			target[targetlen - 1] = '\0';
10618 
10619 		/*
10620 		 * See if an objset ID was supplied (-d <pool>/<objset ID>).
10621 		 * To disambiguate tank/100, consider the 100 as objsetID
10622 		 * if -N was given, otherwise 100 is an objsetID iff
10623 		 * tank/100 as a named dataset fails on lookup.
10624 		 */
10625 		objset_str = strchr(target, '/');
10626 		if (objset_str && strlen(objset_str) > 1 &&
10627 		    zdb_numeric(objset_str + 1)) {
10628 			char *endptr;
10629 			errno = 0;
10630 			objset_str++;
10631 			objset_id = strtoull(objset_str, &endptr, 0);
10632 			/* dataset 0 is the same as opening the pool */
10633 			if (errno == 0 && endptr != objset_str &&
10634 			    objset_id != 0) {
10635 				if (dump_opt['N'])
10636 					dataset_lookup = B_TRUE;
10637 			}
10638 			/* normal dataset name not an objset ID */
10639 			if (endptr == objset_str) {
10640 				objset_id = -1;
10641 			}
10642 		} else if (objset_str && !zdb_numeric(objset_str + 1) &&
10643 		    dump_opt['N']) {
10644 			printf("Supply a numeric objset ID with -N\n");
10645 			error = 2;
10646 			goto fini;
10647 		}
10648 	} else {
10649 		target_pool = target;
10650 	}
10651 
10652 	if (dump_opt['e'] || force_import) {
10653 		importargs_t args = { 0 };
10654 
10655 		/*
10656 		 * If path is not provided, search in /dev
10657 		 */
10658 		if (searchdirs == NULL) {
10659 			searchdirs = umem_alloc(sizeof (char *), UMEM_NOFAIL);
10660 			searchdirs[nsearch++] = (char *)ZFS_DEVDIR;
10661 		}
10662 
10663 		args.paths = nsearch;
10664 		args.path = searchdirs;
10665 		args.can_be_active = B_TRUE;
10666 
10667 		libpc_handle_t lpch = {
10668 			.lpc_lib_handle = NULL,
10669 			.lpc_ops = &libzpool_config_ops,
10670 			.lpc_printerr = B_TRUE
10671 		};
10672 		error = zpool_find_config(&lpch, target_pool, &cfg, &args);
10673 
10674 		if (error == 0) {
10675 
10676 			if (nvlist_add_nvlist(cfg,
10677 			    ZPOOL_LOAD_POLICY, policy) != 0) {
10678 				fatal("can't open '%s': %s",
10679 				    target, strerror(ENOMEM));
10680 			}
10681 
10682 			if (dump_opt['C'] > 1) {
10683 				(void) printf("\nConfiguration for import:\n");
10684 				dump_nvlist(cfg, 8);
10685 			}
10686 
10687 			/*
10688 			 * Disable the activity check to allow examination of
10689 			 * active pools.
10690 			 */
10691 			error = spa_import(target_pool, cfg, NULL,
10692 			    flags | ZFS_IMPORT_SKIP_MMP);
10693 		}
10694 	}
10695 
10696 	if (searchdirs != NULL) {
10697 		umem_free(searchdirs, nsearch * sizeof (char *));
10698 		searchdirs = NULL;
10699 	}
10700 
10701 	/*
10702 	 * We need to make sure to process -O option or call
10703 	 * dump_path after the -e option has been processed,
10704 	 * which imports the pool to the namespace if it's
10705 	 * not in the cachefile.
10706 	 */
10707 	if (dump_opt['O'] && !dump_opt['r']) {
10708 		if (argc != 2)
10709 			usage();
10710 		dump_opt['v'] = verbose + 3;
10711 		error = dump_path(argv[0], argv[1], NULL);
10712 		goto fini;
10713 	}
10714 
10715 	if (dump_opt['r']) {
10716 		target_is_spa = B_FALSE;
10717 		if (argc != 3)
10718 			usage();
10719 		dump_opt['v'] = verbose;
10720 		if (dump_opt['O']) {
10721 			object = strtoull(argv[1], NULL, 0);
10722 		} else {
10723 			error = dump_path(argv[0], argv[1], &object);
10724 		}
10725 		if (error != 0)
10726 			fatal("internal error: %s", strerror(error));
10727 	}
10728 
10729 	/*
10730 	 * import_checkpointed_state makes the assumption that the
10731 	 * target pool that we pass it is already part of the spa
10732 	 * namespace. Because of that we need to make sure to call
10733 	 * it always after the -e option has been processed, which
10734 	 * imports the pool to the namespace if it's not in the
10735 	 * cachefile.
10736 	 */
10737 	char *checkpoint_pool = NULL;
10738 	char *checkpoint_target = NULL;
10739 	if (dump_opt['k']) {
10740 		checkpoint_pool = import_checkpointed_state(target, cfg,
10741 		    target_is_spa, &checkpoint_target);
10742 
10743 		if (checkpoint_target != NULL)
10744 			target = checkpoint_target;
10745 	}
10746 
10747 	if (cfg != NULL) {
10748 		nvlist_free(cfg);
10749 		cfg = NULL;
10750 	}
10751 
10752 	if (target_pool != target)
10753 		free(target_pool);
10754 
10755 	if (error == 0) {
10756 		if (dump_opt['k'] && (target_is_spa || dump_opt['R'])) {
10757 			ASSERT(checkpoint_pool != NULL);
10758 			ASSERT0P(checkpoint_target);
10759 
10760 			error = spa_open(checkpoint_pool, &spa, FTAG);
10761 			if (error != 0) {
10762 				fatal("Tried to open pool \"%s\" but "
10763 				    "spa_open() failed with error %d\n",
10764 				    checkpoint_pool, error);
10765 			}
10766 
10767 		} else if (target_is_spa || dump_opt['R'] || dump_opt['B'] ||
10768 		    objset_id == 0) {
10769 			zdb_set_skip_mmp(target);
10770 			error = spa_open_rewind(target, &spa, FTAG, policy,
10771 			    NULL);
10772 			if (error) {
10773 				/*
10774 				 * If we're missing the log device then
10775 				 * try opening the pool after clearing the
10776 				 * log state.  Keep the global spa NULL
10777 				 * meanwhile: the failed open left it that
10778 				 * way, we hold no reference on what the
10779 				 * lookup returns, and zdb_exit() would
10780 				 * spa_close() it on the way out.
10781 				 */
10782 				spa_t *found;
10783 
10784 				spa_namespace_enter(FTAG);
10785 				if ((found = spa_lookup(target)) != NULL &&
10786 				    found->spa_log_state == SPA_LOG_MISSING) {
10787 					found->spa_log_state = SPA_LOG_CLEAR;
10788 					error = 0;
10789 				}
10790 				spa_namespace_exit(FTAG);
10791 
10792 				if (!error) {
10793 					error = spa_open_rewind(target, &spa,
10794 					    FTAG, policy, NULL);
10795 				}
10796 			}
10797 		} else if (strpbrk(target, "#") != NULL) {
10798 			dsl_pool_t *dp;
10799 			error = dsl_pool_hold(target, FTAG, &dp);
10800 			if (error != 0) {
10801 				fatal("can't dump '%s': %s", target,
10802 				    strerror(error));
10803 			}
10804 			error = dump_bookmark(dp, target, B_TRUE, verbose > 1);
10805 			dsl_pool_rele(dp, FTAG);
10806 			if (error != 0) {
10807 				fatal("can't dump '%s': %s", target,
10808 				    strerror(error));
10809 			}
10810 			goto fini;
10811 		} else {
10812 			target_pool = strdup(target);
10813 			if (strpbrk(target, "/@") != NULL)
10814 				*strpbrk(target_pool, "/@") = '\0';
10815 
10816 			zdb_set_skip_mmp(target);
10817 			/*
10818 			 * If -N was supplied, the user has indicated that
10819 			 * zdb -d <pool>/<objsetID> is in effect.  Otherwise
10820 			 * we first assume that the dataset string is the
10821 			 * dataset name.  If dmu_objset_hold fails with the
10822 			 * dataset string, and we have an objset_id, retry the
10823 			 * lookup with the objsetID.
10824 			 */
10825 			boolean_t retry = B_TRUE;
10826 retry_lookup:
10827 			if (dataset_lookup == B_TRUE) {
10828 				/*
10829 				 * Use the supplied id to get the name
10830 				 * for open_objset.
10831 				 */
10832 				error = spa_open(target_pool, &spa, FTAG);
10833 				if (error == 0) {
10834 					error = name_from_objset_id(spa,
10835 					    objset_id, dsname);
10836 					spa_close(spa, FTAG);
10837 					if (error == 0)
10838 						target = dsname;
10839 				}
10840 			}
10841 			if (error == 0) {
10842 				if (objset_id > 0 && retry) {
10843 					int err = dmu_objset_hold(target, FTAG,
10844 					    &os);
10845 					if (err) {
10846 						dataset_lookup = B_TRUE;
10847 						retry = B_FALSE;
10848 						goto retry_lookup;
10849 					} else {
10850 						dmu_objset_rele(os, FTAG);
10851 					}
10852 				}
10853 				error = open_objset(target, FTAG, &os);
10854 			}
10855 			if (error == 0)
10856 				spa = dmu_objset_spa(os);
10857 			free(target_pool);
10858 		}
10859 	}
10860 	nvlist_free(policy);
10861 
10862 	if (error)
10863 		fatal("can't open '%s': %s", target, strerror(error));
10864 
10865 	/*
10866 	 * Set the pool failure mode to panic in order to prevent the pool
10867 	 * from suspending.  A suspended I/O will have no way to resume and
10868 	 * can prevent the zdb(8) command from terminating as expected.
10869 	 */
10870 	if (spa != NULL)
10871 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
10872 
10873 	argv++;
10874 	argc--;
10875 	if (dump_opt['r']) {
10876 		error = zdb_copy_object(os, object, argv[1]);
10877 	} else if (!dump_opt['R']) {
10878 		flagbits['d'] = ZOR_FLAG_DIRECTORY;
10879 		flagbits['f'] = ZOR_FLAG_PLAIN_FILE;
10880 		flagbits['m'] = ZOR_FLAG_SPACE_MAP;
10881 		flagbits['v'] = ZOR_FLAG_ZVOL;
10882 		flagbits['z'] = ZOR_FLAG_ZAP;
10883 		flagbits['A'] = ZOR_FLAG_ALL_TYPES;
10884 
10885 		if (argc > 0 && (dump_opt['d'] || dump_opt['f'])) {
10886 			zopt_object_args = argc;
10887 			zopt_object_ranges = calloc(zopt_object_args,
10888 			    sizeof (zopt_object_range_t));
10889 			for (unsigned i = 0; i < zopt_object_args; i++) {
10890 				int err;
10891 				const char *msg = NULL;
10892 
10893 				err = parse_object_range(argv[i],
10894 				    &zopt_object_ranges[i], &msg);
10895 				if (err != 0)
10896 					fatal("Bad object or range: '%s': %s\n",
10897 					    argv[i], msg ?: "");
10898 			}
10899 		} else if (argc > 0 && dump_opt['m']) {
10900 			zopt_metaslab_args = argc;
10901 			zopt_metaslab = calloc(zopt_metaslab_args,
10902 			    sizeof (uint64_t));
10903 			for (unsigned i = 0; i < zopt_metaslab_args; i++) {
10904 				errno = 0;
10905 				zopt_metaslab[i] = strtoull(argv[i], NULL, 0);
10906 				if (zopt_metaslab[i] == 0 && errno != 0)
10907 					fatal("bad number %s: %s", argv[i],
10908 					    strerror(errno));
10909 			}
10910 		}
10911 
10912 		if (dump_opt['f'] && os != NULL) {
10913 			dump_opt['v'] = verbose;
10914 			dump_file_data_layout(os);
10915 		} else if (dump_opt['B']) {
10916 			dump_backup(target, objset_id,
10917 			    argc > 0 ? argv[0] : NULL);
10918 		} else if (os != NULL) {
10919 			dump_objset(os);
10920 		} else if (zopt_object_args > 0 && !dump_opt['m']) {
10921 			dump_objset(spa->spa_meta_objset);
10922 		} else {
10923 			dump_zpool(spa);
10924 		}
10925 	} else {
10926 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
10927 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
10928 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
10929 		flagbits['e'] = ZDB_FLAG_BSWAP;
10930 		flagbits['g'] = ZDB_FLAG_GBH;
10931 		flagbits['i'] = ZDB_FLAG_INDIRECT;
10932 		flagbits['r'] = ZDB_FLAG_RAW;
10933 		flagbits['v'] = ZDB_FLAG_VERBOSE;
10934 
10935 		for (int i = 0; i < argc; i++)
10936 			zdb_read_block(argv[i], spa);
10937 	}
10938 
10939 	if (dump_opt['k']) {
10940 		free(checkpoint_pool);
10941 		if (!target_is_spa)
10942 			free(checkpoint_target);
10943 	}
10944 
10945 fini:
10946 	if (spa != NULL)
10947 		zdb_ddt_cleanup(spa);
10948 
10949 	if (os != NULL) {
10950 		close_objset(os, FTAG);
10951 	} else if (spa != NULL) {
10952 		spa_close(spa, FTAG);
10953 	}
10954 
10955 	fuid_table_destroy();
10956 
10957 	dump_debug_buffer();
10958 
10959 	if (kernel_init_done)
10960 		kernel_fini();
10961 
10962 	if (corruption_found && error == 0)
10963 		error = 3;
10964 
10965 	return (error);
10966 }
10967