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