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