xref: /illumos-gate/usr/src/cmd/zdb/zdb.c (revision e75b2cb046700ccab0100afdcd81f2b860fcca87)
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 http://www.opensolaris.org/os/licensing.
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, 2018 by Delphix. All rights reserved.
25  * Copyright (c) 2014 Integros [integros.com]
26  * Copyright 2017 Nexenta Systems, Inc.
27  * Copyright (c) 2017, 2018 Lawrence Livermore National Security, LLC.
28  * Copyright 2017 RackTop Systems.
29  */
30 
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <stdio_ext.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <sys/zfs_context.h>
37 #include <sys/spa.h>
38 #include <sys/spa_impl.h>
39 #include <sys/dmu.h>
40 #include <sys/zap.h>
41 #include <sys/fs/zfs.h>
42 #include <sys/zfs_znode.h>
43 #include <sys/zfs_sa.h>
44 #include <sys/sa.h>
45 #include <sys/sa_impl.h>
46 #include <sys/vdev.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/metaslab_impl.h>
49 #include <sys/dmu_objset.h>
50 #include <sys/dsl_dir.h>
51 #include <sys/dsl_dataset.h>
52 #include <sys/dsl_pool.h>
53 #include <sys/dbuf.h>
54 #include <sys/zil.h>
55 #include <sys/zil_impl.h>
56 #include <sys/stat.h>
57 #include <sys/resource.h>
58 #include <sys/dmu_traverse.h>
59 #include <sys/zio_checksum.h>
60 #include <sys/zio_compress.h>
61 #include <sys/zfs_fuid.h>
62 #include <sys/arc.h>
63 #include <sys/ddt.h>
64 #include <sys/zfeature.h>
65 #include <sys/abd.h>
66 #include <sys/blkptr.h>
67 #include <sys/dsl_scan.h>
68 #include <sys/dsl_crypt.h>
69 #include <zfs_comutil.h>
70 #include <libcmdutils.h>
71 #undef verify
72 #include <libzfs.h>
73 
74 #include "zdb.h"
75 
76 #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
77 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
78 #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
79 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
80 #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
81 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
82 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
83 #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
84 	(idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA ?	\
85 	DMU_OT_ZAP_OTHER : \
86 	(idx) == DMU_OTN_UINT64_DATA || (idx) == DMU_OTN_UINT64_METADATA ? \
87 	DMU_OT_UINT64_OTHER : DMU_OT_NUMTYPES)
88 
89 #ifndef lint
90 extern int reference_tracking_enable;
91 extern boolean_t zfs_recover;
92 extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
93 extern int zfs_vdev_async_read_max_active;
94 extern int aok;
95 extern boolean_t spa_load_verify_dryrun;
96 #else
97 int reference_tracking_enable;
98 boolean_t zfs_recover;
99 uint64_t zfs_arc_max, zfs_arc_meta_limit;
100 int zfs_vdev_async_read_max_active;
101 int aok;
102 boolean_t spa_load_verify_dryrun;
103 #endif
104 
105 static const char cmdname[] = "zdb";
106 uint8_t dump_opt[256];
107 
108 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
109 
110 uint64_t *zopt_object = NULL;
111 static unsigned zopt_objects = 0;
112 libzfs_handle_t *g_zfs;
113 uint64_t max_inflight = 1000;
114 static int leaked_objects = 0;
115 
116 static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
117 static void mos_obj_refd(uint64_t);
118 
119 /*
120  * These libumem hooks provide a reasonable set of defaults for the allocator's
121  * debugging facilities.
122  */
123 const char *
124 _umem_debug_init()
125 {
126 	return ("default,verbose"); /* $UMEM_DEBUG setting */
127 }
128 
129 const char *
130 _umem_logging_init(void)
131 {
132 	return ("fail,contents"); /* $UMEM_LOGGING setting */
133 }
134 
135 static void
136 usage(void)
137 {
138 	(void) fprintf(stderr,
139 	    "Usage:\t%s [-AbcdDFGhikLMPsvX] [-e [-V] [-p <path> ...]] "
140 	    "[-I <inflight I/Os>]\n"
141 	    "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
142 	    "\t\t[<poolname> [<object> ...]]\n"
143 	    "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>] <dataset> "
144 	    "[<object> ...]\n"
145 	    "\t%s -C [-A] [-U <cache>]\n"
146 	    "\t%s -l [-Aqu] <device>\n"
147 	    "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] "
148 	    "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n"
149 	    "\t%s -O <dataset> <path>\n"
150 	    "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
151 	    "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n"
152 	    "\t%s -E [-A] word0:word1:...:word15\n"
153 	    "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] "
154 	    "<poolname>\n\n",
155 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname,
156 	    cmdname, cmdname);
157 
158 	(void) fprintf(stderr, "    Dataset name must include at least one "
159 	    "separator character '/' or '@'\n");
160 	(void) fprintf(stderr, "    If dataset name is specified, only that "
161 	    "dataset is dumped\n");
162 	(void) fprintf(stderr, "    If object numbers are specified, only "
163 	    "those objects are dumped\n\n");
164 	(void) fprintf(stderr, "    Options to control amount of output:\n");
165 	(void) fprintf(stderr, "        -b block statistics\n");
166 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
167 	    "all data) blocks\n");
168 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
169 	(void) fprintf(stderr, "        -d dataset(s)\n");
170 	(void) fprintf(stderr, "        -D dedup statistics\n");
171 	(void) fprintf(stderr, "        -E decode and display block from an "
172 	    "embedded block pointer\n");
173 	(void) fprintf(stderr, "        -h pool history\n");
174 	(void) fprintf(stderr, "        -i intent logs\n");
175 	(void) fprintf(stderr, "        -l read label contents\n");
176 	(void) fprintf(stderr, "        -k examine the checkpointed state "
177 	    "of the pool\n");
178 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
179 	    "load spacemaps)\n");
180 	(void) fprintf(stderr, "        -m metaslabs\n");
181 	(void) fprintf(stderr, "        -M metaslab groups\n");
182 	(void) fprintf(stderr, "        -O perform object lookups by path\n");
183 	(void) fprintf(stderr, "        -R read and display block from a "
184 	    "device\n");
185 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
186 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
187 	(void) fprintf(stderr, "        -v verbose (applies to all "
188 	    "others)\n\n");
189 	(void) fprintf(stderr, "    Below options are intended for use "
190 	    "with other options:\n");
191 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
192 	    "panic recovery (-AA) or both (-AAA)\n");
193 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
194 	    "has altroot/not in a cachefile\n");
195 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
196 	    "safe range of transaction groups\n");
197 	(void) fprintf(stderr, "        -G dump zfs_dbgmsg buffer before "
198 	    "exiting\n");
199 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
200 	    "specify the maximum number of "
201 	    "checksumming I/Os [default is 200]\n");
202 	(void) fprintf(stderr, "        -o <variable>=<value> set global "
203 	    "variable to an unsigned 32-bit integer value\n");
204 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
205 	    "-e to specify path to vdev dir\n");
206 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
207 	(void) fprintf(stderr, "        -q don't print label contents\n");
208 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
209 	    "searching for uberblocks\n");
210 	(void) fprintf(stderr, "        -u uberblock\n");
211 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
212 	    "cachefile\n");
213 	(void) fprintf(stderr, "        -V do verbatim import\n");
214 	(void) fprintf(stderr, "        -x <dumpdir> -- "
215 	    "dump all read blocks into specified directory\n");
216 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
217 	    "work with dataset)\n\n");
218 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
219 	    "to make only that option verbose\n");
220 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
221 	exit(1);
222 }
223 
224 static void
225 dump_debug_buffer()
226 {
227 	if (dump_opt['G']) {
228 		(void) printf("\n");
229 		zfs_dbgmsg_print("zdb");
230 	}
231 }
232 
233 /*
234  * Called for usage errors that are discovered after a call to spa_open(),
235  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
236  */
237 
238 static void
239 fatal(const char *fmt, ...)
240 {
241 	va_list ap;
242 
243 	va_start(ap, fmt);
244 	(void) fprintf(stderr, "%s: ", cmdname);
245 	(void) vfprintf(stderr, fmt, ap);
246 	va_end(ap);
247 	(void) fprintf(stderr, "\n");
248 
249 	dump_debug_buffer();
250 
251 	exit(1);
252 }
253 
254 /* ARGSUSED */
255 static void
256 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
257 {
258 	nvlist_t *nv;
259 	size_t nvsize = *(uint64_t *)data;
260 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
261 
262 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
263 
264 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
265 
266 	umem_free(packed, nvsize);
267 
268 	dump_nvlist(nv, 8);
269 
270 	nvlist_free(nv);
271 }
272 
273 /* ARGSUSED */
274 static void
275 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
276 {
277 	spa_history_phys_t *shp = data;
278 
279 	if (shp == NULL)
280 		return;
281 
282 	(void) printf("\t\tpool_create_len = %llu\n",
283 	    (u_longlong_t)shp->sh_pool_create_len);
284 	(void) printf("\t\tphys_max_off = %llu\n",
285 	    (u_longlong_t)shp->sh_phys_max_off);
286 	(void) printf("\t\tbof = %llu\n",
287 	    (u_longlong_t)shp->sh_bof);
288 	(void) printf("\t\teof = %llu\n",
289 	    (u_longlong_t)shp->sh_eof);
290 	(void) printf("\t\trecords_lost = %llu\n",
291 	    (u_longlong_t)shp->sh_records_lost);
292 }
293 
294 static void
295 zdb_nicenum(uint64_t num, char *buf, size_t buflen)
296 {
297 	if (dump_opt['P'])
298 		(void) snprintf(buf, buflen, "%llu", (longlong_t)num);
299 	else
300 		nicenum(num, buf, sizeof (buf));
301 }
302 
303 static const char histo_stars[] = "****************************************";
304 static const uint64_t histo_width = sizeof (histo_stars) - 1;
305 
306 static void
307 dump_histogram(const uint64_t *histo, int size, int offset)
308 {
309 	int i;
310 	int minidx = size - 1;
311 	int maxidx = 0;
312 	uint64_t max = 0;
313 
314 	for (i = 0; i < size; i++) {
315 		if (histo[i] > max)
316 			max = histo[i];
317 		if (histo[i] > 0 && i > maxidx)
318 			maxidx = i;
319 		if (histo[i] > 0 && i < minidx)
320 			minidx = i;
321 	}
322 
323 	if (max < histo_width)
324 		max = histo_width;
325 
326 	for (i = minidx; i <= maxidx; i++) {
327 		(void) printf("\t\t\t%3u: %6llu %s\n",
328 		    i + offset, (u_longlong_t)histo[i],
329 		    &histo_stars[(max - histo[i]) * histo_width / max]);
330 	}
331 }
332 
333 static void
334 dump_zap_stats(objset_t *os, uint64_t object)
335 {
336 	int error;
337 	zap_stats_t zs;
338 
339 	error = zap_get_stats(os, object, &zs);
340 	if (error)
341 		return;
342 
343 	if (zs.zs_ptrtbl_len == 0) {
344 		ASSERT(zs.zs_num_blocks == 1);
345 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
346 		    (u_longlong_t)zs.zs_blocksize,
347 		    (u_longlong_t)zs.zs_num_entries);
348 		return;
349 	}
350 
351 	(void) printf("\tFat ZAP stats:\n");
352 
353 	(void) printf("\t\tPointer table:\n");
354 	(void) printf("\t\t\t%llu elements\n",
355 	    (u_longlong_t)zs.zs_ptrtbl_len);
356 	(void) printf("\t\t\tzt_blk: %llu\n",
357 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
358 	(void) printf("\t\t\tzt_numblks: %llu\n",
359 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
360 	(void) printf("\t\t\tzt_shift: %llu\n",
361 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
362 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
363 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
364 	(void) printf("\t\t\tzt_nextblk: %llu\n",
365 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
366 
367 	(void) printf("\t\tZAP entries: %llu\n",
368 	    (u_longlong_t)zs.zs_num_entries);
369 	(void) printf("\t\tLeaf blocks: %llu\n",
370 	    (u_longlong_t)zs.zs_num_leafs);
371 	(void) printf("\t\tTotal blocks: %llu\n",
372 	    (u_longlong_t)zs.zs_num_blocks);
373 	(void) printf("\t\tzap_block_type: 0x%llx\n",
374 	    (u_longlong_t)zs.zs_block_type);
375 	(void) printf("\t\tzap_magic: 0x%llx\n",
376 	    (u_longlong_t)zs.zs_magic);
377 	(void) printf("\t\tzap_salt: 0x%llx\n",
378 	    (u_longlong_t)zs.zs_salt);
379 
380 	(void) printf("\t\tLeafs with 2^n pointers:\n");
381 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
382 
383 	(void) printf("\t\tBlocks with n*5 entries:\n");
384 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
385 
386 	(void) printf("\t\tBlocks n/10 full:\n");
387 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
388 
389 	(void) printf("\t\tEntries with n chunks:\n");
390 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
391 
392 	(void) printf("\t\tBuckets with n entries:\n");
393 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
394 }
395 
396 /*ARGSUSED*/
397 static void
398 dump_none(objset_t *os, uint64_t object, void *data, size_t size)
399 {
400 }
401 
402 /*ARGSUSED*/
403 static void
404 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
405 {
406 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
407 }
408 
409 /*ARGSUSED*/
410 static void
411 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
412 {
413 }
414 
415 /*ARGSUSED*/
416 static void
417 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
418 {
419 }
420 
421 /*ARGSUSED*/
422 static void
423 dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
424 {
425 	zap_cursor_t zc;
426 	zap_attribute_t attr;
427 	void *prop;
428 	unsigned i;
429 
430 	dump_zap_stats(os, object);
431 	(void) printf("\n");
432 
433 	for (zap_cursor_init(&zc, os, object);
434 	    zap_cursor_retrieve(&zc, &attr) == 0;
435 	    zap_cursor_advance(&zc)) {
436 		(void) printf("\t\t%s = ", attr.za_name);
437 		if (attr.za_num_integers == 0) {
438 			(void) printf("\n");
439 			continue;
440 		}
441 		prop = umem_zalloc(attr.za_num_integers *
442 		    attr.za_integer_length, UMEM_NOFAIL);
443 		(void) zap_lookup(os, object, attr.za_name,
444 		    attr.za_integer_length, attr.za_num_integers, prop);
445 		if (attr.za_integer_length == 1) {
446 			(void) printf("%s", (char *)prop);
447 		} else {
448 			for (i = 0; i < attr.za_num_integers; i++) {
449 				switch (attr.za_integer_length) {
450 				case 2:
451 					(void) printf("%u ",
452 					    ((uint16_t *)prop)[i]);
453 					break;
454 				case 4:
455 					(void) printf("%u ",
456 					    ((uint32_t *)prop)[i]);
457 					break;
458 				case 8:
459 					(void) printf("%lld ",
460 					    (u_longlong_t)((int64_t *)prop)[i]);
461 					break;
462 				}
463 			}
464 		}
465 		(void) printf("\n");
466 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
467 	}
468 	zap_cursor_fini(&zc);
469 }
470 
471 static void
472 dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
473 {
474 	bpobj_phys_t *bpop = data;
475 	char bytes[32], comp[32], uncomp[32];
476 
477 	/* make sure the output won't get truncated */
478 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
479 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
480 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
481 
482 	if (bpop == NULL)
483 		return;
484 
485 	zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
486 	zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
487 	zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
488 
489 	(void) printf("\t\tnum_blkptrs = %llu\n",
490 	    (u_longlong_t)bpop->bpo_num_blkptrs);
491 	(void) printf("\t\tbytes = %s\n", bytes);
492 	if (size >= BPOBJ_SIZE_V1) {
493 		(void) printf("\t\tcomp = %s\n", comp);
494 		(void) printf("\t\tuncomp = %s\n", uncomp);
495 	}
496 	if (size >= sizeof (*bpop)) {
497 		(void) printf("\t\tsubobjs = %llu\n",
498 		    (u_longlong_t)bpop->bpo_subobjs);
499 		(void) printf("\t\tnum_subobjs = %llu\n",
500 		    (u_longlong_t)bpop->bpo_num_subobjs);
501 	}
502 
503 	if (dump_opt['d'] < 5)
504 		return;
505 
506 	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
507 		char blkbuf[BP_SPRINTF_LEN];
508 		blkptr_t bp;
509 
510 		int err = dmu_read(os, object,
511 		    i * sizeof (bp), sizeof (bp), &bp, 0);
512 		if (err != 0) {
513 			(void) printf("got error %u from dmu_read\n", err);
514 			break;
515 		}
516 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
517 		(void) printf("\t%s\n", blkbuf);
518 	}
519 }
520 
521 /* ARGSUSED */
522 static void
523 dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
524 {
525 	dmu_object_info_t doi;
526 
527 	VERIFY0(dmu_object_info(os, object, &doi));
528 	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
529 
530 	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
531 	if (err != 0) {
532 		(void) printf("got error %u from dmu_read\n", err);
533 		kmem_free(subobjs, doi.doi_max_offset);
534 		return;
535 	}
536 
537 	int64_t last_nonzero = -1;
538 	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
539 		if (subobjs[i] != 0)
540 			last_nonzero = i;
541 	}
542 
543 	for (int64_t i = 0; i <= last_nonzero; i++) {
544 		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
545 	}
546 	kmem_free(subobjs, doi.doi_max_offset);
547 }
548 
549 /*ARGSUSED*/
550 static void
551 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
552 {
553 	dump_zap_stats(os, object);
554 	/* contents are printed elsewhere, properly decoded */
555 }
556 
557 /*ARGSUSED*/
558 static void
559 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
560 {
561 	zap_cursor_t zc;
562 	zap_attribute_t attr;
563 
564 	dump_zap_stats(os, object);
565 	(void) printf("\n");
566 
567 	for (zap_cursor_init(&zc, os, object);
568 	    zap_cursor_retrieve(&zc, &attr) == 0;
569 	    zap_cursor_advance(&zc)) {
570 		(void) printf("\t\t%s = ", attr.za_name);
571 		if (attr.za_num_integers == 0) {
572 			(void) printf("\n");
573 			continue;
574 		}
575 		(void) printf(" %llx : [%d:%d:%d]\n",
576 		    (u_longlong_t)attr.za_first_integer,
577 		    (int)ATTR_LENGTH(attr.za_first_integer),
578 		    (int)ATTR_BSWAP(attr.za_first_integer),
579 		    (int)ATTR_NUM(attr.za_first_integer));
580 	}
581 	zap_cursor_fini(&zc);
582 }
583 
584 /*ARGSUSED*/
585 static void
586 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
587 {
588 	zap_cursor_t zc;
589 	zap_attribute_t attr;
590 	uint16_t *layout_attrs;
591 	unsigned i;
592 
593 	dump_zap_stats(os, object);
594 	(void) printf("\n");
595 
596 	for (zap_cursor_init(&zc, os, object);
597 	    zap_cursor_retrieve(&zc, &attr) == 0;
598 	    zap_cursor_advance(&zc)) {
599 		(void) printf("\t\t%s = [", attr.za_name);
600 		if (attr.za_num_integers == 0) {
601 			(void) printf("\n");
602 			continue;
603 		}
604 
605 		VERIFY(attr.za_integer_length == 2);
606 		layout_attrs = umem_zalloc(attr.za_num_integers *
607 		    attr.za_integer_length, UMEM_NOFAIL);
608 
609 		VERIFY(zap_lookup(os, object, attr.za_name,
610 		    attr.za_integer_length,
611 		    attr.za_num_integers, layout_attrs) == 0);
612 
613 		for (i = 0; i != attr.za_num_integers; i++)
614 			(void) printf(" %d ", (int)layout_attrs[i]);
615 		(void) printf("]\n");
616 		umem_free(layout_attrs,
617 		    attr.za_num_integers * attr.za_integer_length);
618 	}
619 	zap_cursor_fini(&zc);
620 }
621 
622 /*ARGSUSED*/
623 static void
624 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
625 {
626 	zap_cursor_t zc;
627 	zap_attribute_t attr;
628 	const char *typenames[] = {
629 		/* 0 */ "not specified",
630 		/* 1 */ "FIFO",
631 		/* 2 */ "Character Device",
632 		/* 3 */ "3 (invalid)",
633 		/* 4 */ "Directory",
634 		/* 5 */ "5 (invalid)",
635 		/* 6 */ "Block Device",
636 		/* 7 */ "7 (invalid)",
637 		/* 8 */ "Regular File",
638 		/* 9 */ "9 (invalid)",
639 		/* 10 */ "Symbolic Link",
640 		/* 11 */ "11 (invalid)",
641 		/* 12 */ "Socket",
642 		/* 13 */ "Door",
643 		/* 14 */ "Event Port",
644 		/* 15 */ "15 (invalid)",
645 	};
646 
647 	dump_zap_stats(os, object);
648 	(void) printf("\n");
649 
650 	for (zap_cursor_init(&zc, os, object);
651 	    zap_cursor_retrieve(&zc, &attr) == 0;
652 	    zap_cursor_advance(&zc)) {
653 		(void) printf("\t\t%s = %lld (type: %s)\n",
654 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
655 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
656 	}
657 	zap_cursor_fini(&zc);
658 }
659 
660 static int
661 get_dtl_refcount(vdev_t *vd)
662 {
663 	int refcount = 0;
664 
665 	if (vd->vdev_ops->vdev_op_leaf) {
666 		space_map_t *sm = vd->vdev_dtl_sm;
667 
668 		if (sm != NULL &&
669 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
670 			return (1);
671 		return (0);
672 	}
673 
674 	for (unsigned c = 0; c < vd->vdev_children; c++)
675 		refcount += get_dtl_refcount(vd->vdev_child[c]);
676 	return (refcount);
677 }
678 
679 static int
680 get_metaslab_refcount(vdev_t *vd)
681 {
682 	int refcount = 0;
683 
684 	if (vd->vdev_top == vd) {
685 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
686 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
687 
688 			if (sm != NULL &&
689 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
690 				refcount++;
691 		}
692 	}
693 	for (unsigned c = 0; c < vd->vdev_children; c++)
694 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
695 
696 	return (refcount);
697 }
698 
699 static int
700 get_obsolete_refcount(vdev_t *vd)
701 {
702 	int refcount = 0;
703 
704 	uint64_t obsolete_sm_obj = vdev_obsolete_sm_object(vd);
705 	if (vd->vdev_top == vd && obsolete_sm_obj != 0) {
706 		dmu_object_info_t doi;
707 		VERIFY0(dmu_object_info(vd->vdev_spa->spa_meta_objset,
708 		    obsolete_sm_obj, &doi));
709 		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
710 			refcount++;
711 		}
712 	} else {
713 		ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
714 		ASSERT3U(obsolete_sm_obj, ==, 0);
715 	}
716 	for (unsigned c = 0; c < vd->vdev_children; c++) {
717 		refcount += get_obsolete_refcount(vd->vdev_child[c]);
718 	}
719 
720 	return (refcount);
721 }
722 
723 static int
724 get_prev_obsolete_spacemap_refcount(spa_t *spa)
725 {
726 	uint64_t prev_obj =
727 	    spa->spa_condensing_indirect_phys.scip_prev_obsolete_sm_object;
728 	if (prev_obj != 0) {
729 		dmu_object_info_t doi;
730 		VERIFY0(dmu_object_info(spa->spa_meta_objset, prev_obj, &doi));
731 		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
732 			return (1);
733 		}
734 	}
735 	return (0);
736 }
737 
738 static int
739 get_checkpoint_refcount(vdev_t *vd)
740 {
741 	int refcount = 0;
742 
743 	if (vd->vdev_top == vd && vd->vdev_top_zap != 0 &&
744 	    zap_contains(spa_meta_objset(vd->vdev_spa),
745 	    vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) == 0)
746 		refcount++;
747 
748 	for (uint64_t c = 0; c < vd->vdev_children; c++)
749 		refcount += get_checkpoint_refcount(vd->vdev_child[c]);
750 
751 	return (refcount);
752 }
753 
754 static int
755 verify_spacemap_refcounts(spa_t *spa)
756 {
757 	uint64_t expected_refcount = 0;
758 	uint64_t actual_refcount;
759 
760 	(void) feature_get_refcount(spa,
761 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
762 	    &expected_refcount);
763 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
764 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
765 	actual_refcount += get_obsolete_refcount(spa->spa_root_vdev);
766 	actual_refcount += get_prev_obsolete_spacemap_refcount(spa);
767 	actual_refcount += get_checkpoint_refcount(spa->spa_root_vdev);
768 
769 	if (expected_refcount != actual_refcount) {
770 		(void) printf("space map refcount mismatch: expected %lld != "
771 		    "actual %lld\n",
772 		    (longlong_t)expected_refcount,
773 		    (longlong_t)actual_refcount);
774 		return (2);
775 	}
776 	return (0);
777 }
778 
779 static void
780 dump_spacemap(objset_t *os, space_map_t *sm)
781 {
782 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
783 	    "INVALID", "INVALID", "INVALID", "INVALID" };
784 
785 	if (sm == NULL)
786 		return;
787 
788 	(void) printf("space map object %llu:\n",
789 	    (longlong_t)sm->sm_object);
790 	(void) printf("  smp_length = 0x%llx\n",
791 	    (longlong_t)sm->sm_phys->smp_length);
792 	(void) printf("  smp_alloc = 0x%llx\n",
793 	    (longlong_t)sm->sm_phys->smp_alloc);
794 
795 	if (dump_opt['d'] < 6 && dump_opt['m'] < 4)
796 		return;
797 
798 	/*
799 	 * Print out the freelist entries in both encoded and decoded form.
800 	 */
801 	uint8_t mapshift = sm->sm_shift;
802 	int64_t alloc = 0;
803 	uint64_t word, entry_id = 0;
804 	for (uint64_t offset = 0; offset < space_map_length(sm);
805 	    offset += sizeof (word)) {
806 
807 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
808 		    sizeof (word), &word, DMU_READ_PREFETCH));
809 
810 		if (sm_entry_is_debug(word)) {
811 			(void) printf("\t    [%6llu] %s: txg %llu pass %llu\n",
812 			    (u_longlong_t)entry_id,
813 			    ddata[SM_DEBUG_ACTION_DECODE(word)],
814 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(word),
815 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(word));
816 			entry_id++;
817 			continue;
818 		}
819 
820 		uint8_t words;
821 		char entry_type;
822 		uint64_t entry_off, entry_run, entry_vdev = SM_NO_VDEVID;
823 
824 		if (sm_entry_is_single_word(word)) {
825 			entry_type = (SM_TYPE_DECODE(word) == SM_ALLOC) ?
826 			    'A' : 'F';
827 			entry_off = (SM_OFFSET_DECODE(word) << mapshift) +
828 			    sm->sm_start;
829 			entry_run = SM_RUN_DECODE(word) << mapshift;
830 			words = 1;
831 		} else {
832 			/* it is a two-word entry so we read another word */
833 			ASSERT(sm_entry_is_double_word(word));
834 
835 			uint64_t extra_word;
836 			offset += sizeof (extra_word);
837 			VERIFY0(dmu_read(os, space_map_object(sm), offset,
838 			    sizeof (extra_word), &extra_word,
839 			    DMU_READ_PREFETCH));
840 
841 			ASSERT3U(offset, <=, space_map_length(sm));
842 
843 			entry_run = SM2_RUN_DECODE(word) << mapshift;
844 			entry_vdev = SM2_VDEV_DECODE(word);
845 			entry_type = (SM2_TYPE_DECODE(extra_word) == SM_ALLOC) ?
846 			    'A' : 'F';
847 			entry_off = (SM2_OFFSET_DECODE(extra_word) <<
848 			    mapshift) + sm->sm_start;
849 			words = 2;
850 		}
851 
852 		(void) printf("\t    [%6llu]    %c  range:"
853 		    " %010llx-%010llx  size: %06llx vdev: %06llu words: %u\n",
854 		    (u_longlong_t)entry_id,
855 		    entry_type, (u_longlong_t)entry_off,
856 		    (u_longlong_t)(entry_off + entry_run),
857 		    (u_longlong_t)entry_run,
858 		    (u_longlong_t)entry_vdev, words);
859 
860 		if (entry_type == 'A')
861 			alloc += entry_run;
862 		else
863 			alloc -= entry_run;
864 		entry_id++;
865 	}
866 	if (alloc != space_map_allocated(sm)) {
867 		(void) printf("space_map_object alloc (%lld) INCONSISTENT "
868 		    "with space map summary (%lld)\n",
869 		    (longlong_t)space_map_allocated(sm), (longlong_t)alloc);
870 	}
871 }
872 
873 static void
874 dump_metaslab_stats(metaslab_t *msp)
875 {
876 	char maxbuf[32];
877 	range_tree_t *rt = msp->ms_allocatable;
878 	avl_tree_t *t = &msp->ms_allocatable_by_size;
879 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
880 
881 	/* max sure nicenum has enough space */
882 	CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ);
883 
884 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf, sizeof (maxbuf));
885 
886 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
887 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
888 	    "freepct", free_pct);
889 	(void) printf("\tIn-memory histogram:\n");
890 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
891 }
892 
893 static void
894 dump_metaslab(metaslab_t *msp)
895 {
896 	vdev_t *vd = msp->ms_group->mg_vd;
897 	spa_t *spa = vd->vdev_spa;
898 	space_map_t *sm = msp->ms_sm;
899 	char freebuf[32];
900 
901 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf,
902 	    sizeof (freebuf));
903 
904 	(void) printf(
905 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
906 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
907 	    (u_longlong_t)space_map_object(sm), freebuf);
908 
909 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
910 		mutex_enter(&msp->ms_lock);
911 		VERIFY0(metaslab_load(msp));
912 		range_tree_stat_verify(msp->ms_allocatable);
913 		dump_metaslab_stats(msp);
914 		metaslab_unload(msp);
915 		mutex_exit(&msp->ms_lock);
916 	}
917 
918 	if (dump_opt['m'] > 1 && sm != NULL &&
919 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
920 		/*
921 		 * The space map histogram represents free space in chunks
922 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
923 		 */
924 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
925 		    (u_longlong_t)msp->ms_fragmentation);
926 		dump_histogram(sm->sm_phys->smp_histogram,
927 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
928 	}
929 
930 	ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
931 	dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
932 }
933 
934 static void
935 print_vdev_metaslab_header(vdev_t *vd)
936 {
937 	vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
938 	const char *bias_str;
939 
940 	bias_str = (alloc_bias == VDEV_BIAS_LOG || vd->vdev_islog) ?
941 	    VDEV_ALLOC_BIAS_LOG :
942 	    (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL :
943 	    (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP :
944 	    vd->vdev_islog ? "log" : "";
945 
946 	(void) printf("\tvdev %10llu   %s\n"
947 	    "\t%-10s%5llu   %-19s   %-15s   %-12s\n",
948 	    (u_longlong_t)vd->vdev_id, bias_str,
949 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
950 	    "offset", "spacemap", "free");
951 	(void) printf("\t%15s   %19s   %15s   %12s\n",
952 	    "---------------", "-------------------",
953 	    "---------------", "------------");
954 }
955 
956 static void
957 dump_metaslab_groups(spa_t *spa)
958 {
959 	vdev_t *rvd = spa->spa_root_vdev;
960 	metaslab_class_t *mc = spa_normal_class(spa);
961 	uint64_t fragmentation;
962 
963 	metaslab_class_histogram_verify(mc);
964 
965 	for (unsigned c = 0; c < rvd->vdev_children; c++) {
966 		vdev_t *tvd = rvd->vdev_child[c];
967 		metaslab_group_t *mg = tvd->vdev_mg;
968 
969 		if (mg == NULL || mg->mg_class != mc)
970 			continue;
971 
972 		metaslab_group_histogram_verify(mg);
973 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
974 
975 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
976 		    "fragmentation",
977 		    (u_longlong_t)tvd->vdev_id,
978 		    (u_longlong_t)tvd->vdev_ms_count);
979 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
980 			(void) printf("%3s\n", "-");
981 		} else {
982 			(void) printf("%3llu%%\n",
983 			    (u_longlong_t)mg->mg_fragmentation);
984 		}
985 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
986 	}
987 
988 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
989 	fragmentation = metaslab_class_fragmentation(mc);
990 	if (fragmentation == ZFS_FRAG_INVALID)
991 		(void) printf("\t%3s\n", "-");
992 	else
993 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
994 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
995 }
996 
997 static void
998 print_vdev_indirect(vdev_t *vd)
999 {
1000 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
1001 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
1002 	vdev_indirect_births_t *vib = vd->vdev_indirect_births;
1003 
1004 	if (vim == NULL) {
1005 		ASSERT3P(vib, ==, NULL);
1006 		return;
1007 	}
1008 
1009 	ASSERT3U(vdev_indirect_mapping_object(vim), ==,
1010 	    vic->vic_mapping_object);
1011 	ASSERT3U(vdev_indirect_births_object(vib), ==,
1012 	    vic->vic_births_object);
1013 
1014 	(void) printf("indirect births obj %llu:\n",
1015 	    (longlong_t)vic->vic_births_object);
1016 	(void) printf("    vib_count = %llu\n",
1017 	    (longlong_t)vdev_indirect_births_count(vib));
1018 	for (uint64_t i = 0; i < vdev_indirect_births_count(vib); i++) {
1019 		vdev_indirect_birth_entry_phys_t *cur_vibe =
1020 		    &vib->vib_entries[i];
1021 		(void) printf("\toffset %llx -> txg %llu\n",
1022 		    (longlong_t)cur_vibe->vibe_offset,
1023 		    (longlong_t)cur_vibe->vibe_phys_birth_txg);
1024 	}
1025 	(void) printf("\n");
1026 
1027 	(void) printf("indirect mapping obj %llu:\n",
1028 	    (longlong_t)vic->vic_mapping_object);
1029 	(void) printf("    vim_max_offset = 0x%llx\n",
1030 	    (longlong_t)vdev_indirect_mapping_max_offset(vim));
1031 	(void) printf("    vim_bytes_mapped = 0x%llx\n",
1032 	    (longlong_t)vdev_indirect_mapping_bytes_mapped(vim));
1033 	(void) printf("    vim_count = %llu\n",
1034 	    (longlong_t)vdev_indirect_mapping_num_entries(vim));
1035 
1036 	if (dump_opt['d'] <= 5 && dump_opt['m'] <= 3)
1037 		return;
1038 
1039 	uint32_t *counts = vdev_indirect_mapping_load_obsolete_counts(vim);
1040 
1041 	for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
1042 		vdev_indirect_mapping_entry_phys_t *vimep =
1043 		    &vim->vim_entries[i];
1044 		(void) printf("\t<%llx:%llx:%llx> -> "
1045 		    "<%llx:%llx:%llx> (%x obsolete)\n",
1046 		    (longlong_t)vd->vdev_id,
1047 		    (longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
1048 		    (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
1049 		    (longlong_t)DVA_GET_VDEV(&vimep->vimep_dst),
1050 		    (longlong_t)DVA_GET_OFFSET(&vimep->vimep_dst),
1051 		    (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
1052 		    counts[i]);
1053 	}
1054 	(void) printf("\n");
1055 
1056 	uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd);
1057 	if (obsolete_sm_object != 0) {
1058 		objset_t *mos = vd->vdev_spa->spa_meta_objset;
1059 		(void) printf("obsolete space map object %llu:\n",
1060 		    (u_longlong_t)obsolete_sm_object);
1061 		ASSERT(vd->vdev_obsolete_sm != NULL);
1062 		ASSERT3U(space_map_object(vd->vdev_obsolete_sm), ==,
1063 		    obsolete_sm_object);
1064 		dump_spacemap(mos, vd->vdev_obsolete_sm);
1065 		(void) printf("\n");
1066 	}
1067 }
1068 
1069 static void
1070 dump_metaslabs(spa_t *spa)
1071 {
1072 	vdev_t *vd, *rvd = spa->spa_root_vdev;
1073 	uint64_t m, c = 0, children = rvd->vdev_children;
1074 
1075 	(void) printf("\nMetaslabs:\n");
1076 
1077 	if (!dump_opt['d'] && zopt_objects > 0) {
1078 		c = zopt_object[0];
1079 
1080 		if (c >= children)
1081 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
1082 
1083 		if (zopt_objects > 1) {
1084 			vd = rvd->vdev_child[c];
1085 			print_vdev_metaslab_header(vd);
1086 
1087 			for (m = 1; m < zopt_objects; m++) {
1088 				if (zopt_object[m] < vd->vdev_ms_count)
1089 					dump_metaslab(
1090 					    vd->vdev_ms[zopt_object[m]]);
1091 				else
1092 					(void) fprintf(stderr, "bad metaslab "
1093 					    "number %llu\n",
1094 					    (u_longlong_t)zopt_object[m]);
1095 			}
1096 			(void) printf("\n");
1097 			return;
1098 		}
1099 		children = c + 1;
1100 	}
1101 	for (; c < children; c++) {
1102 		vd = rvd->vdev_child[c];
1103 		print_vdev_metaslab_header(vd);
1104 
1105 		print_vdev_indirect(vd);
1106 
1107 		for (m = 0; m < vd->vdev_ms_count; m++)
1108 			dump_metaslab(vd->vdev_ms[m]);
1109 		(void) printf("\n");
1110 	}
1111 }
1112 
1113 static void
1114 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
1115 {
1116 	const ddt_phys_t *ddp = dde->dde_phys;
1117 	const ddt_key_t *ddk = &dde->dde_key;
1118 	const char *types[4] = { "ditto", "single", "double", "triple" };
1119 	char blkbuf[BP_SPRINTF_LEN];
1120 	blkptr_t blk;
1121 
1122 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1123 		if (ddp->ddp_phys_birth == 0)
1124 			continue;
1125 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
1126 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
1127 		(void) printf("index %llx refcnt %llu %s %s\n",
1128 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
1129 		    types[p], blkbuf);
1130 	}
1131 }
1132 
1133 static void
1134 dump_dedup_ratio(const ddt_stat_t *dds)
1135 {
1136 	double rL, rP, rD, D, dedup, compress, copies;
1137 
1138 	if (dds->dds_blocks == 0)
1139 		return;
1140 
1141 	rL = (double)dds->dds_ref_lsize;
1142 	rP = (double)dds->dds_ref_psize;
1143 	rD = (double)dds->dds_ref_dsize;
1144 	D = (double)dds->dds_dsize;
1145 
1146 	dedup = rD / D;
1147 	compress = rL / rP;
1148 	copies = rD / rP;
1149 
1150 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
1151 	    "dedup * compress / copies = %.2f\n\n",
1152 	    dedup, compress, copies, dedup * compress / copies);
1153 }
1154 
1155 static void
1156 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
1157 {
1158 	char name[DDT_NAMELEN];
1159 	ddt_entry_t dde;
1160 	uint64_t walk = 0;
1161 	dmu_object_info_t doi;
1162 	uint64_t count, dspace, mspace;
1163 	int error;
1164 
1165 	error = ddt_object_info(ddt, type, class, &doi);
1166 
1167 	if (error == ENOENT)
1168 		return;
1169 	ASSERT(error == 0);
1170 
1171 	if ((count = ddt_object_count(ddt, type, class)) == 0)
1172 		return;
1173 
1174 	dspace = doi.doi_physical_blocks_512 << 9;
1175 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
1176 
1177 	ddt_object_name(ddt, type, class, name);
1178 
1179 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
1180 	    name,
1181 	    (u_longlong_t)count,
1182 	    (u_longlong_t)(dspace / count),
1183 	    (u_longlong_t)(mspace / count));
1184 
1185 	if (dump_opt['D'] < 3)
1186 		return;
1187 
1188 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
1189 
1190 	if (dump_opt['D'] < 4)
1191 		return;
1192 
1193 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
1194 		return;
1195 
1196 	(void) printf("%s contents:\n\n", name);
1197 
1198 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
1199 		dump_dde(ddt, &dde, walk);
1200 
1201 	ASSERT3U(error, ==, ENOENT);
1202 
1203 	(void) printf("\n");
1204 }
1205 
1206 static void
1207 dump_all_ddts(spa_t *spa)
1208 {
1209 	ddt_histogram_t ddh_total;
1210 	ddt_stat_t dds_total;
1211 
1212 	bzero(&ddh_total, sizeof (ddh_total));
1213 	bzero(&dds_total, sizeof (dds_total));
1214 
1215 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1216 		ddt_t *ddt = spa->spa_ddt[c];
1217 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
1218 			for (enum ddt_class class = 0; class < DDT_CLASSES;
1219 			    class++) {
1220 				dump_ddt(ddt, type, class);
1221 			}
1222 		}
1223 	}
1224 
1225 	ddt_get_dedup_stats(spa, &dds_total);
1226 
1227 	if (dds_total.dds_blocks == 0) {
1228 		(void) printf("All DDTs are empty\n");
1229 		return;
1230 	}
1231 
1232 	(void) printf("\n");
1233 
1234 	if (dump_opt['D'] > 1) {
1235 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
1236 		ddt_get_dedup_histogram(spa, &ddh_total);
1237 		zpool_dump_ddt(&dds_total, &ddh_total);
1238 	}
1239 
1240 	dump_dedup_ratio(&dds_total);
1241 }
1242 
1243 static void
1244 dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
1245 {
1246 	char *prefix = arg;
1247 
1248 	(void) printf("%s [%llu,%llu) length %llu\n",
1249 	    prefix,
1250 	    (u_longlong_t)start,
1251 	    (u_longlong_t)(start + size),
1252 	    (u_longlong_t)(size));
1253 }
1254 
1255 static void
1256 dump_dtl(vdev_t *vd, int indent)
1257 {
1258 	spa_t *spa = vd->vdev_spa;
1259 	boolean_t required;
1260 	const char *name[DTL_TYPES] = { "missing", "partial", "scrub",
1261 		"outage" };
1262 	char prefix[256];
1263 
1264 	spa_vdev_state_enter(spa, SCL_NONE);
1265 	required = vdev_dtl_required(vd);
1266 	(void) spa_vdev_state_exit(spa, NULL, 0);
1267 
1268 	if (indent == 0)
1269 		(void) printf("\nDirty time logs:\n\n");
1270 
1271 	(void) printf("\t%*s%s [%s]\n", indent, "",
1272 	    vd->vdev_path ? vd->vdev_path :
1273 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
1274 	    required ? "DTL-required" : "DTL-expendable");
1275 
1276 	for (int t = 0; t < DTL_TYPES; t++) {
1277 		range_tree_t *rt = vd->vdev_dtl[t];
1278 		if (range_tree_space(rt) == 0)
1279 			continue;
1280 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
1281 		    indent + 2, "", name[t]);
1282 		range_tree_walk(rt, dump_dtl_seg, prefix);
1283 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
1284 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1285 	}
1286 
1287 	for (unsigned c = 0; c < vd->vdev_children; c++)
1288 		dump_dtl(vd->vdev_child[c], indent + 4);
1289 }
1290 
1291 static void
1292 dump_history(spa_t *spa)
1293 {
1294 	nvlist_t **events = NULL;
1295 	uint64_t resid, len, off = 0;
1296 	uint_t num = 0;
1297 	int error;
1298 	time_t tsec;
1299 	struct tm t;
1300 	char tbuf[30];
1301 	char internalstr[MAXPATHLEN];
1302 
1303 	char *buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
1304 	do {
1305 		len = SPA_MAXBLOCKSIZE;
1306 
1307 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
1308 			(void) fprintf(stderr, "Unable to read history: "
1309 			    "error %d\n", error);
1310 			umem_free(buf, SPA_MAXBLOCKSIZE);
1311 			return;
1312 		}
1313 
1314 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
1315 			break;
1316 
1317 		off -= resid;
1318 	} while (len != 0);
1319 	umem_free(buf, SPA_MAXBLOCKSIZE);
1320 
1321 	(void) printf("\nHistory:\n");
1322 	for (unsigned i = 0; i < num; i++) {
1323 		uint64_t time, txg, ievent;
1324 		char *cmd, *intstr;
1325 		boolean_t printed = B_FALSE;
1326 
1327 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
1328 		    &time) != 0)
1329 			goto next;
1330 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
1331 		    &cmd) != 0) {
1332 			if (nvlist_lookup_uint64(events[i],
1333 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
1334 				goto next;
1335 			verify(nvlist_lookup_uint64(events[i],
1336 			    ZPOOL_HIST_TXG, &txg) == 0);
1337 			verify(nvlist_lookup_string(events[i],
1338 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
1339 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
1340 				goto next;
1341 
1342 			(void) snprintf(internalstr,
1343 			    sizeof (internalstr),
1344 			    "[internal %s txg:%ju] %s",
1345 			    zfs_history_event_names[ievent], (uintmax_t)txg,
1346 			    intstr);
1347 			cmd = internalstr;
1348 		}
1349 		tsec = time;
1350 		(void) localtime_r(&tsec, &t);
1351 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
1352 		(void) printf("%s %s\n", tbuf, cmd);
1353 		printed = B_TRUE;
1354 
1355 next:
1356 		if (dump_opt['h'] > 1) {
1357 			if (!printed)
1358 				(void) printf("unrecognized record:\n");
1359 			dump_nvlist(events[i], 2);
1360 		}
1361 	}
1362 }
1363 
1364 /*ARGSUSED*/
1365 static void
1366 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1367 {
1368 }
1369 
1370 static uint64_t
1371 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
1372     const zbookmark_phys_t *zb)
1373 {
1374 	if (dnp == NULL) {
1375 		ASSERT(zb->zb_level < 0);
1376 		if (zb->zb_object == 0)
1377 			return (zb->zb_blkid);
1378 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1379 	}
1380 
1381 	ASSERT(zb->zb_level >= 0);
1382 
1383 	return ((zb->zb_blkid <<
1384 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1385 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1386 }
1387 
1388 static void
1389 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
1390 {
1391 	const dva_t *dva = bp->blk_dva;
1392 	unsigned int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1393 
1394 	if (dump_opt['b'] >= 6) {
1395 		snprintf_blkptr(blkbuf, buflen, bp);
1396 		return;
1397 	}
1398 
1399 	if (BP_IS_EMBEDDED(bp)) {
1400 		(void) sprintf(blkbuf,
1401 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
1402 		    (int)BPE_GET_ETYPE(bp),
1403 		    (u_longlong_t)BPE_GET_LSIZE(bp),
1404 		    (u_longlong_t)BPE_GET_PSIZE(bp),
1405 		    (u_longlong_t)bp->blk_birth);
1406 		return;
1407 	}
1408 
1409 	blkbuf[0] = '\0';
1410 	for (unsigned int i = 0; i < ndvas; i++)
1411 		(void) snprintf(blkbuf + strlen(blkbuf),
1412 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
1413 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
1414 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
1415 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
1416 
1417 	if (BP_IS_HOLE(bp)) {
1418 		(void) snprintf(blkbuf + strlen(blkbuf),
1419 		    buflen - strlen(blkbuf),
1420 		    "%llxL B=%llu",
1421 		    (u_longlong_t)BP_GET_LSIZE(bp),
1422 		    (u_longlong_t)bp->blk_birth);
1423 	} else {
1424 		(void) snprintf(blkbuf + strlen(blkbuf),
1425 		    buflen - strlen(blkbuf),
1426 		    "%llxL/%llxP F=%llu B=%llu/%llu",
1427 		    (u_longlong_t)BP_GET_LSIZE(bp),
1428 		    (u_longlong_t)BP_GET_PSIZE(bp),
1429 		    (u_longlong_t)BP_GET_FILL(bp),
1430 		    (u_longlong_t)bp->blk_birth,
1431 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
1432 	}
1433 }
1434 
1435 static void
1436 print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
1437     const dnode_phys_t *dnp)
1438 {
1439 	char blkbuf[BP_SPRINTF_LEN];
1440 	int l;
1441 
1442 	if (!BP_IS_EMBEDDED(bp)) {
1443 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1444 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
1445 	}
1446 
1447 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1448 
1449 	ASSERT(zb->zb_level >= 0);
1450 
1451 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1452 		if (l == zb->zb_level) {
1453 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1454 		} else {
1455 			(void) printf(" ");
1456 		}
1457 	}
1458 
1459 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1460 	(void) printf("%s\n", blkbuf);
1461 }
1462 
1463 static int
1464 visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
1465     blkptr_t *bp, const zbookmark_phys_t *zb)
1466 {
1467 	int err = 0;
1468 
1469 	if (bp->blk_birth == 0)
1470 		return (0);
1471 
1472 	print_indirect(bp, zb, dnp);
1473 
1474 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
1475 		arc_flags_t flags = ARC_FLAG_WAIT;
1476 		int i;
1477 		blkptr_t *cbp;
1478 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
1479 		arc_buf_t *buf;
1480 		uint64_t fill = 0;
1481 
1482 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
1483 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
1484 		if (err)
1485 			return (err);
1486 		ASSERT(buf->b_data);
1487 
1488 		/* recursively visit blocks below this */
1489 		cbp = buf->b_data;
1490 		for (i = 0; i < epb; i++, cbp++) {
1491 			zbookmark_phys_t czb;
1492 
1493 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
1494 			    zb->zb_level - 1,
1495 			    zb->zb_blkid * epb + i);
1496 			err = visit_indirect(spa, dnp, cbp, &czb);
1497 			if (err)
1498 				break;
1499 			fill += BP_GET_FILL(cbp);
1500 		}
1501 		if (!err)
1502 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
1503 		arc_buf_destroy(buf, &buf);
1504 	}
1505 
1506 	return (err);
1507 }
1508 
1509 /*ARGSUSED*/
1510 static void
1511 dump_indirect(dnode_t *dn)
1512 {
1513 	dnode_phys_t *dnp = dn->dn_phys;
1514 	int j;
1515 	zbookmark_phys_t czb;
1516 
1517 	(void) printf("Indirect blocks:\n");
1518 
1519 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
1520 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
1521 	for (j = 0; j < dnp->dn_nblkptr; j++) {
1522 		czb.zb_blkid = j;
1523 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
1524 		    &dnp->dn_blkptr[j], &czb);
1525 	}
1526 
1527 	(void) printf("\n");
1528 }
1529 
1530 /*ARGSUSED*/
1531 static void
1532 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1533 {
1534 	dsl_dir_phys_t *dd = data;
1535 	time_t crtime;
1536 	char nice[32];
1537 
1538 	/* make sure nicenum has enough space */
1539 	CTASSERT(sizeof (nice) >= NN_NUMBUF_SZ);
1540 
1541 	if (dd == NULL)
1542 		return;
1543 
1544 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1545 
1546 	crtime = dd->dd_creation_time;
1547 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1548 	(void) printf("\t\thead_dataset_obj = %llu\n",
1549 	    (u_longlong_t)dd->dd_head_dataset_obj);
1550 	(void) printf("\t\tparent_dir_obj = %llu\n",
1551 	    (u_longlong_t)dd->dd_parent_obj);
1552 	(void) printf("\t\torigin_obj = %llu\n",
1553 	    (u_longlong_t)dd->dd_origin_obj);
1554 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1555 	    (u_longlong_t)dd->dd_child_dir_zapobj);
1556 	zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice));
1557 	(void) printf("\t\tused_bytes = %s\n", nice);
1558 	zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice));
1559 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
1560 	zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice));
1561 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
1562 	zdb_nicenum(dd->dd_quota, nice, sizeof (nice));
1563 	(void) printf("\t\tquota = %s\n", nice);
1564 	zdb_nicenum(dd->dd_reserved, nice, sizeof (nice));
1565 	(void) printf("\t\treserved = %s\n", nice);
1566 	(void) printf("\t\tprops_zapobj = %llu\n",
1567 	    (u_longlong_t)dd->dd_props_zapobj);
1568 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1569 	    (u_longlong_t)dd->dd_deleg_zapobj);
1570 	(void) printf("\t\tflags = %llx\n",
1571 	    (u_longlong_t)dd->dd_flags);
1572 
1573 #define	DO(which) \
1574 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \
1575 	    sizeof (nice)); \
1576 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
1577 	DO(HEAD);
1578 	DO(SNAP);
1579 	DO(CHILD);
1580 	DO(CHILD_RSRV);
1581 	DO(REFRSRV);
1582 #undef DO
1583 	(void) printf("\t\tclones = %llu\n",
1584 	    (u_longlong_t)dd->dd_clones);
1585 }
1586 
1587 /*ARGSUSED*/
1588 static void
1589 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1590 {
1591 	dsl_dataset_phys_t *ds = data;
1592 	time_t crtime;
1593 	char used[32], compressed[32], uncompressed[32], unique[32];
1594 	char blkbuf[BP_SPRINTF_LEN];
1595 
1596 	/* make sure nicenum has enough space */
1597 	CTASSERT(sizeof (used) >= NN_NUMBUF_SZ);
1598 	CTASSERT(sizeof (compressed) >= NN_NUMBUF_SZ);
1599 	CTASSERT(sizeof (uncompressed) >= NN_NUMBUF_SZ);
1600 	CTASSERT(sizeof (unique) >= NN_NUMBUF_SZ);
1601 
1602 	if (ds == NULL)
1603 		return;
1604 
1605 	ASSERT(size == sizeof (*ds));
1606 	crtime = ds->ds_creation_time;
1607 	zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used));
1608 	zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed));
1609 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed,
1610 	    sizeof (uncompressed));
1611 	zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique));
1612 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1613 
1614 	(void) printf("\t\tdir_obj = %llu\n",
1615 	    (u_longlong_t)ds->ds_dir_obj);
1616 	(void) printf("\t\tprev_snap_obj = %llu\n",
1617 	    (u_longlong_t)ds->ds_prev_snap_obj);
1618 	(void) printf("\t\tprev_snap_txg = %llu\n",
1619 	    (u_longlong_t)ds->ds_prev_snap_txg);
1620 	(void) printf("\t\tnext_snap_obj = %llu\n",
1621 	    (u_longlong_t)ds->ds_next_snap_obj);
1622 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1623 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1624 	(void) printf("\t\tnum_children = %llu\n",
1625 	    (u_longlong_t)ds->ds_num_children);
1626 	(void) printf("\t\tuserrefs_obj = %llu\n",
1627 	    (u_longlong_t)ds->ds_userrefs_obj);
1628 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1629 	(void) printf("\t\tcreation_txg = %llu\n",
1630 	    (u_longlong_t)ds->ds_creation_txg);
1631 	(void) printf("\t\tdeadlist_obj = %llu\n",
1632 	    (u_longlong_t)ds->ds_deadlist_obj);
1633 	(void) printf("\t\tused_bytes = %s\n", used);
1634 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1635 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1636 	(void) printf("\t\tunique = %s\n", unique);
1637 	(void) printf("\t\tfsid_guid = %llu\n",
1638 	    (u_longlong_t)ds->ds_fsid_guid);
1639 	(void) printf("\t\tguid = %llu\n",
1640 	    (u_longlong_t)ds->ds_guid);
1641 	(void) printf("\t\tflags = %llx\n",
1642 	    (u_longlong_t)ds->ds_flags);
1643 	(void) printf("\t\tnext_clones_obj = %llu\n",
1644 	    (u_longlong_t)ds->ds_next_clones_obj);
1645 	(void) printf("\t\tprops_obj = %llu\n",
1646 	    (u_longlong_t)ds->ds_props_obj);
1647 	(void) printf("\t\tbp = %s\n", blkbuf);
1648 }
1649 
1650 /* ARGSUSED */
1651 static int
1652 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1653 {
1654 	char blkbuf[BP_SPRINTF_LEN];
1655 
1656 	if (bp->blk_birth != 0) {
1657 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1658 		(void) printf("\t%s\n", blkbuf);
1659 	}
1660 	return (0);
1661 }
1662 
1663 static void
1664 dump_bptree(objset_t *os, uint64_t obj, const char *name)
1665 {
1666 	char bytes[32];
1667 	bptree_phys_t *bt;
1668 	dmu_buf_t *db;
1669 
1670 	/* make sure nicenum has enough space */
1671 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1672 
1673 	if (dump_opt['d'] < 3)
1674 		return;
1675 
1676 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1677 	bt = db->db_data;
1678 	zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes));
1679 	(void) printf("\n    %s: %llu datasets, %s\n",
1680 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1681 	dmu_buf_rele(db, FTAG);
1682 
1683 	if (dump_opt['d'] < 5)
1684 		return;
1685 
1686 	(void) printf("\n");
1687 
1688 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1689 }
1690 
1691 /* ARGSUSED */
1692 static int
1693 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1694 {
1695 	char blkbuf[BP_SPRINTF_LEN];
1696 
1697 	ASSERT(bp->blk_birth != 0);
1698 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1699 	(void) printf("\t%s\n", blkbuf);
1700 	return (0);
1701 }
1702 
1703 static void
1704 dump_full_bpobj(bpobj_t *bpo, const char *name, int indent)
1705 {
1706 	char bytes[32];
1707 	char comp[32];
1708 	char uncomp[32];
1709 
1710 	/* make sure nicenum has enough space */
1711 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1712 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1713 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1714 
1715 	if (dump_opt['d'] < 3)
1716 		return;
1717 
1718 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes));
1719 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1720 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp));
1721 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp));
1722 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1723 		    "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1724 		    indent * 8, name,
1725 		    (u_longlong_t)bpo->bpo_object,
1726 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1727 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1728 		    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
1729 		    bytes, comp, uncomp);
1730 
1731 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1732 			uint64_t subobj;
1733 			bpobj_t subbpo;
1734 			int error;
1735 			VERIFY0(dmu_read(bpo->bpo_os,
1736 			    bpo->bpo_phys->bpo_subobjs,
1737 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1738 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1739 			if (error != 0) {
1740 				(void) printf("ERROR %u while trying to open "
1741 				    "subobj id %llu\n",
1742 				    error, (u_longlong_t)subobj);
1743 				continue;
1744 			}
1745 			dump_full_bpobj(&subbpo, "subobj", indent + 1);
1746 			bpobj_close(&subbpo);
1747 		}
1748 	} else {
1749 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1750 		    indent * 8, name,
1751 		    (u_longlong_t)bpo->bpo_object,
1752 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1753 		    bytes);
1754 	}
1755 
1756 	if (dump_opt['d'] < 5)
1757 		return;
1758 
1759 
1760 	if (indent == 0) {
1761 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1762 		(void) printf("\n");
1763 	}
1764 }
1765 
1766 static void
1767 bpobj_count_refd(bpobj_t *bpo)
1768 {
1769 	mos_obj_refd(bpo->bpo_object);
1770 
1771 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1772 		mos_obj_refd(bpo->bpo_phys->bpo_subobjs);
1773 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1774 			uint64_t subobj;
1775 			bpobj_t subbpo;
1776 			int error;
1777 			VERIFY0(dmu_read(bpo->bpo_os,
1778 			    bpo->bpo_phys->bpo_subobjs,
1779 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1780 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1781 			if (error != 0) {
1782 				(void) printf("ERROR %u while trying to open "
1783 				    "subobj id %llu\n",
1784 				    error, (u_longlong_t)subobj);
1785 				continue;
1786 			}
1787 			bpobj_count_refd(&subbpo);
1788 			bpobj_close(&subbpo);
1789 		}
1790 	}
1791 }
1792 
1793 static void
1794 dump_deadlist(dsl_deadlist_t *dl)
1795 {
1796 	dsl_deadlist_entry_t *dle;
1797 	uint64_t unused;
1798 	char bytes[32];
1799 	char comp[32];
1800 	char uncomp[32];
1801 	uint64_t empty_bpobj =
1802 	    dmu_objset_spa(dl->dl_os)->spa_dsl_pool->dp_empty_bpobj;
1803 
1804 	/* force the tree to be loaded */
1805 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1806 
1807 	if (dl->dl_oldfmt) {
1808 		if (dl->dl_bpobj.bpo_object != empty_bpobj)
1809 			bpobj_count_refd(&dl->dl_bpobj);
1810 	} else {
1811 		mos_obj_refd(dl->dl_object);
1812 		for (dle = avl_first(&dl->dl_tree); dle;
1813 		    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1814 			if (dle->dle_bpobj.bpo_object != empty_bpobj)
1815 				bpobj_count_refd(&dle->dle_bpobj);
1816 		}
1817 	}
1818 
1819 	/* make sure nicenum has enough space */
1820 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1821 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1822 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1823 
1824 	if (dump_opt['d'] < 3)
1825 		return;
1826 
1827 	if (dl->dl_oldfmt) {
1828 		dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
1829 		return;
1830 	}
1831 
1832 	zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes));
1833 	zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp));
1834 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp));
1835 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1836 	    bytes, comp, uncomp);
1837 
1838 	if (dump_opt['d'] < 4)
1839 		return;
1840 
1841 	(void) printf("\n");
1842 
1843 	for (dle = avl_first(&dl->dl_tree); dle;
1844 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1845 		if (dump_opt['d'] >= 5) {
1846 			char buf[128];
1847 			(void) snprintf(buf, sizeof (buf),
1848 			    "mintxg %llu -> obj %llu",
1849 			    (longlong_t)dle->dle_mintxg,
1850 			    (longlong_t)dle->dle_bpobj.bpo_object);
1851 
1852 			dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1853 		} else {
1854 			(void) printf("mintxg %llu -> obj %llu\n",
1855 			    (longlong_t)dle->dle_mintxg,
1856 			    (longlong_t)dle->dle_bpobj.bpo_object);
1857 		}
1858 	}
1859 }
1860 
1861 static avl_tree_t idx_tree;
1862 static avl_tree_t domain_tree;
1863 static boolean_t fuid_table_loaded;
1864 static objset_t *sa_os = NULL;
1865 static sa_attr_type_t *sa_attr_table = NULL;
1866 
1867 static int
1868 open_objset(const char *path, dmu_objset_type_t type, void *tag, objset_t **osp)
1869 {
1870 	int err;
1871 	uint64_t sa_attrs = 0;
1872 	uint64_t version = 0;
1873 
1874 	VERIFY3P(sa_os, ==, NULL);
1875 	err = dmu_objset_own(path, type, B_TRUE, B_FALSE, tag, osp);
1876 	if (err != 0) {
1877 		(void) fprintf(stderr, "failed to own dataset '%s': %s\n", path,
1878 		    strerror(err));
1879 		return (err);
1880 	}
1881 
1882 	if (dmu_objset_type(*osp) == DMU_OST_ZFS && !(*osp)->os_encrypted) {
1883 		(void) zap_lookup(*osp, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1884 		    8, 1, &version);
1885 		if (version >= ZPL_VERSION_SA) {
1886 			(void) zap_lookup(*osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1887 			    8, 1, &sa_attrs);
1888 		}
1889 		err = sa_setup(*osp, sa_attrs, zfs_attr_table, ZPL_END,
1890 		    &sa_attr_table);
1891 		if (err != 0) {
1892 			(void) fprintf(stderr, "sa_setup failed: %s\n",
1893 			    strerror(err));
1894 			dmu_objset_disown(*osp, B_FALSE, tag);
1895 			*osp = NULL;
1896 		}
1897 	}
1898 	sa_os = *osp;
1899 
1900 	return (0);
1901 }
1902 
1903 static void
1904 close_objset(objset_t *os, void *tag)
1905 {
1906 	VERIFY3P(os, ==, sa_os);
1907 	if (os->os_sa != NULL)
1908 		sa_tear_down(os);
1909 	dmu_objset_disown(os, B_FALSE, tag);
1910 	sa_attr_table = NULL;
1911 	sa_os = NULL;
1912 }
1913 
1914 static void
1915 fuid_table_destroy()
1916 {
1917 	if (fuid_table_loaded) {
1918 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1919 		fuid_table_loaded = B_FALSE;
1920 	}
1921 }
1922 
1923 /*
1924  * print uid or gid information.
1925  * For normal POSIX id just the id is printed in decimal format.
1926  * For CIFS files with FUID the fuid is printed in hex followed by
1927  * the domain-rid string.
1928  */
1929 static void
1930 print_idstr(uint64_t id, const char *id_type)
1931 {
1932 	if (FUID_INDEX(id)) {
1933 		char *domain;
1934 
1935 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1936 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1937 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1938 	} else {
1939 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1940 	}
1941 
1942 }
1943 
1944 static void
1945 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1946 {
1947 	uint32_t uid_idx, gid_idx;
1948 
1949 	uid_idx = FUID_INDEX(uid);
1950 	gid_idx = FUID_INDEX(gid);
1951 
1952 	/* Load domain table, if not already loaded */
1953 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1954 		uint64_t fuid_obj;
1955 
1956 		/* first find the fuid object.  It lives in the master node */
1957 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1958 		    8, 1, &fuid_obj) == 0);
1959 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1960 		(void) zfs_fuid_table_load(os, fuid_obj,
1961 		    &idx_tree, &domain_tree);
1962 		fuid_table_loaded = B_TRUE;
1963 	}
1964 
1965 	print_idstr(uid, "uid");
1966 	print_idstr(gid, "gid");
1967 }
1968 
1969 /*ARGSUSED*/
1970 static void
1971 dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1972 {
1973 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
1974 	sa_handle_t *hdl;
1975 	uint64_t xattr, rdev, gen;
1976 	uint64_t uid, gid, mode, fsize, parent, links;
1977 	uint64_t pflags;
1978 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
1979 	time_t z_crtime, z_atime, z_mtime, z_ctime;
1980 	sa_bulk_attr_t bulk[12];
1981 	int idx = 0;
1982 	int error;
1983 
1984 	VERIFY3P(os, ==, sa_os);
1985 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
1986 		(void) printf("Failed to get handle for SA znode\n");
1987 		return;
1988 	}
1989 
1990 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
1991 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
1992 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
1993 	    &links, 8);
1994 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
1995 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
1996 	    &mode, 8);
1997 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
1998 	    NULL, &parent, 8);
1999 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
2000 	    &fsize, 8);
2001 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
2002 	    acctm, 16);
2003 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
2004 	    modtm, 16);
2005 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
2006 	    crtm, 16);
2007 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
2008 	    chgtm, 16);
2009 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
2010 	    &pflags, 8);
2011 
2012 	if (sa_bulk_lookup(hdl, bulk, idx)) {
2013 		(void) sa_handle_destroy(hdl);
2014 		return;
2015 	}
2016 
2017 	z_crtime = (time_t)crtm[0];
2018 	z_atime = (time_t)acctm[0];
2019 	z_mtime = (time_t)modtm[0];
2020 	z_ctime = (time_t)chgtm[0];
2021 
2022 	if (dump_opt['d'] > 4) {
2023 		error = zfs_obj_to_path(os, object, path, sizeof (path));
2024 		if (error == ESTALE) {
2025 			(void) snprintf(path, sizeof (path), "on delete queue");
2026 		} else if (error != 0) {
2027 			leaked_objects++;
2028 			(void) snprintf(path, sizeof (path),
2029 			    "path not found, possibly leaked");
2030 		}
2031 		(void) printf("\tpath	%s\n", path);
2032 	}
2033 	dump_uidgid(os, uid, gid);
2034 	(void) printf("\tatime	%s", ctime(&z_atime));
2035 	(void) printf("\tmtime	%s", ctime(&z_mtime));
2036 	(void) printf("\tctime	%s", ctime(&z_ctime));
2037 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
2038 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
2039 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
2040 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
2041 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
2042 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
2043 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
2044 	if (dmu_objset_projectquota_enabled(os) && (pflags & ZFS_PROJID)) {
2045 		uint64_t projid;
2046 
2047 		if (sa_lookup(hdl, sa_attr_table[ZPL_PROJID], &projid,
2048 		    sizeof (uint64_t)) == 0)
2049 			(void) printf("\tprojid	%llu\n", (u_longlong_t)projid);
2050 	}
2051 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
2052 	    sizeof (uint64_t)) == 0)
2053 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
2054 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
2055 	    sizeof (uint64_t)) == 0)
2056 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
2057 	sa_handle_destroy(hdl);
2058 }
2059 
2060 /*ARGSUSED*/
2061 static void
2062 dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
2063 {
2064 }
2065 
2066 /*ARGSUSED*/
2067 static void
2068 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
2069 {
2070 }
2071 
2072 
2073 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
2074 	dump_none,		/* unallocated			*/
2075 	dump_zap,		/* object directory		*/
2076 	dump_uint64,		/* object array			*/
2077 	dump_none,		/* packed nvlist		*/
2078 	dump_packed_nvlist,	/* packed nvlist size		*/
2079 	dump_none,		/* bpobj			*/
2080 	dump_bpobj,		/* bpobj header			*/
2081 	dump_none,		/* SPA space map header		*/
2082 	dump_none,		/* SPA space map		*/
2083 	dump_none,		/* ZIL intent log		*/
2084 	dump_dnode,		/* DMU dnode			*/
2085 	dump_dmu_objset,	/* DMU objset			*/
2086 	dump_dsl_dir,		/* DSL directory		*/
2087 	dump_zap,		/* DSL directory child map	*/
2088 	dump_zap,		/* DSL dataset snap map		*/
2089 	dump_zap,		/* DSL props			*/
2090 	dump_dsl_dataset,	/* DSL dataset			*/
2091 	dump_znode,		/* ZFS znode			*/
2092 	dump_acl,		/* ZFS V0 ACL			*/
2093 	dump_uint8,		/* ZFS plain file		*/
2094 	dump_zpldir,		/* ZFS directory		*/
2095 	dump_zap,		/* ZFS master node		*/
2096 	dump_zap,		/* ZFS delete queue		*/
2097 	dump_uint8,		/* zvol object			*/
2098 	dump_zap,		/* zvol prop			*/
2099 	dump_uint8,		/* other uint8[]		*/
2100 	dump_uint64,		/* other uint64[]		*/
2101 	dump_zap,		/* other ZAP			*/
2102 	dump_zap,		/* persistent error log		*/
2103 	dump_uint8,		/* SPA history			*/
2104 	dump_history_offsets,	/* SPA history offsets		*/
2105 	dump_zap,		/* Pool properties		*/
2106 	dump_zap,		/* DSL permissions		*/
2107 	dump_acl,		/* ZFS ACL			*/
2108 	dump_uint8,		/* ZFS SYSACL			*/
2109 	dump_none,		/* FUID nvlist			*/
2110 	dump_packed_nvlist,	/* FUID nvlist size		*/
2111 	dump_zap,		/* DSL dataset next clones	*/
2112 	dump_zap,		/* DSL scrub queue		*/
2113 	dump_zap,		/* ZFS user/group/project used	*/
2114 	dump_zap,		/* ZFS user/group/project quota	*/
2115 	dump_zap,		/* snapshot refcount tags	*/
2116 	dump_ddt_zap,		/* DDT ZAP object		*/
2117 	dump_zap,		/* DDT statistics		*/
2118 	dump_znode,		/* SA object			*/
2119 	dump_zap,		/* SA Master Node		*/
2120 	dump_sa_attrs,		/* SA attribute registration	*/
2121 	dump_sa_layouts,	/* SA attribute layouts		*/
2122 	dump_zap,		/* DSL scrub translations	*/
2123 	dump_none,		/* fake dedup BP		*/
2124 	dump_zap,		/* deadlist			*/
2125 	dump_none,		/* deadlist hdr			*/
2126 	dump_zap,		/* dsl clones			*/
2127 	dump_bpobj_subobjs,	/* bpobj subobjs		*/
2128 	dump_unknown,		/* Unknown type, must be last	*/
2129 };
2130 
2131 static void
2132 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header,
2133     uint64_t *dnode_slots_used)
2134 {
2135 	dmu_buf_t *db = NULL;
2136 	dmu_object_info_t doi;
2137 	dnode_t *dn;
2138 	boolean_t dnode_held = B_FALSE;
2139 	void *bonus = NULL;
2140 	size_t bsize = 0;
2141 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32], dnsize[32];
2142 	char bonus_size[32];
2143 	char aux[50];
2144 	int error;
2145 
2146 	/* make sure nicenum has enough space */
2147 	CTASSERT(sizeof (iblk) >= NN_NUMBUF_SZ);
2148 	CTASSERT(sizeof (dblk) >= NN_NUMBUF_SZ);
2149 	CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
2150 	CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
2151 	CTASSERT(sizeof (bonus_size) >= NN_NUMBUF_SZ);
2152 
2153 	if (*print_header) {
2154 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %6s  %5s  %6s  %s\n",
2155 		    "Object", "lvl", "iblk", "dblk", "dsize", "dnsize",
2156 		    "lsize", "%full", "type");
2157 		*print_header = 0;
2158 	}
2159 
2160 	if (object == 0) {
2161 		dn = DMU_META_DNODE(os);
2162 		dmu_object_info_from_dnode(dn, &doi);
2163 	} else {
2164 		/*
2165 		 * Encrypted datasets will have sensitive bonus buffers
2166 		 * encrypted. Therefore we cannot hold the bonus buffer and
2167 		 * must hold the dnode itself instead.
2168 		 */
2169 		error = dmu_object_info(os, object, &doi);
2170 		if (error)
2171 			fatal("dmu_object_info() failed, errno %u", error);
2172 
2173 		if (os->os_encrypted &&
2174 		    DMU_OT_IS_ENCRYPTED(doi.doi_bonus_type)) {
2175 			error = dnode_hold(os, object, FTAG, &dn);
2176 			if (error)
2177 				fatal("dnode_hold() failed, errno %u", error);
2178 			dnode_held = B_TRUE;
2179 		} else {
2180 			error = dmu_bonus_hold(os, object, FTAG, &db);
2181 			if (error)
2182 				fatal("dmu_bonus_hold(%llu) failed, errno %u",
2183 				    object, error);
2184 			bonus = db->db_data;
2185 			bsize = db->db_size;
2186 			dn = DB_DNODE((dmu_buf_impl_t *)db);
2187 		}
2188 	}
2189 
2190 	if (dnode_slots_used != NULL)
2191 		*dnode_slots_used = doi.doi_dnodesize / DNODE_MIN_SIZE;
2192 
2193 	zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk));
2194 	zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk));
2195 	zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize));
2196 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize));
2197 	zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size));
2198 	zdb_nicenum(doi.doi_dnodesize, dnsize, sizeof (dnsize));
2199 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
2200 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
2201 	    doi.doi_max_offset);
2202 
2203 	aux[0] = '\0';
2204 
2205 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
2206 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
2207 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
2208 	}
2209 
2210 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
2211 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
2212 		    ZDB_COMPRESS_NAME(doi.doi_compress));
2213 	}
2214 
2215 	(void) printf("%10" PRIu64
2216 	    "  %3u  %5s  %5s  %5s  %5s  %5s  %6s  %s%s\n",
2217 	    object, doi.doi_indirection, iblk, dblk,
2218 	    asize, dnsize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
2219 
2220 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
2221 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %5s  %6s  %s\n",
2222 		    "", "", "", "", "", "", bonus_size, "bonus",
2223 		    ZDB_OT_NAME(doi.doi_bonus_type));
2224 	}
2225 
2226 	if (verbosity >= 4) {
2227 		(void) printf("\tdnode flags: %s%s%s%s\n",
2228 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
2229 		    "USED_BYTES " : "",
2230 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
2231 		    "USERUSED_ACCOUNTED " : "",
2232 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) ?
2233 		    "USEROBJUSED_ACCOUNTED " : "",
2234 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
2235 		    "SPILL_BLKPTR" : "");
2236 		(void) printf("\tdnode maxblkid: %llu\n",
2237 		    (longlong_t)dn->dn_phys->dn_maxblkid);
2238 
2239 		if (!dnode_held) {
2240 			object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os,
2241 			    object, bonus, bsize);
2242 		} else {
2243 			(void) printf("\t\t(bonus encrypted)\n");
2244 		}
2245 
2246 		if (!os->os_encrypted || !DMU_OT_IS_ENCRYPTED(doi.doi_type)) {
2247 			object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object,
2248 			    NULL, 0);
2249 		} else {
2250 			(void) printf("\t\t(object encrypted)\n");
2251 		}
2252 
2253 		*print_header = 1;
2254 	}
2255 
2256 	if (verbosity >= 5)
2257 		dump_indirect(dn);
2258 
2259 	if (verbosity >= 5) {
2260 		/*
2261 		 * Report the list of segments that comprise the object.
2262 		 */
2263 		uint64_t start = 0;
2264 		uint64_t end;
2265 		uint64_t blkfill = 1;
2266 		int minlvl = 1;
2267 
2268 		if (dn->dn_type == DMU_OT_DNODE) {
2269 			minlvl = 0;
2270 			blkfill = DNODES_PER_BLOCK;
2271 		}
2272 
2273 		for (;;) {
2274 			char segsize[32];
2275 			/* make sure nicenum has enough space */
2276 			CTASSERT(sizeof (segsize) >= NN_NUMBUF_SZ);
2277 			error = dnode_next_offset(dn,
2278 			    0, &start, minlvl, blkfill, 0);
2279 			if (error)
2280 				break;
2281 			end = start;
2282 			error = dnode_next_offset(dn,
2283 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
2284 			zdb_nicenum(end - start, segsize, sizeof (segsize));
2285 			(void) printf("\t\tsegment [%016llx, %016llx)"
2286 			    " size %5s\n", (u_longlong_t)start,
2287 			    (u_longlong_t)end, segsize);
2288 			if (error)
2289 				break;
2290 			start = end;
2291 		}
2292 	}
2293 
2294 	if (db != NULL)
2295 		dmu_buf_rele(db, FTAG);
2296 	if (dnode_held)
2297 		dnode_rele(dn, FTAG);
2298 }
2299 
2300 static void
2301 count_dir_mos_objects(dsl_dir_t *dd)
2302 {
2303 	mos_obj_refd(dd->dd_object);
2304 	mos_obj_refd(dsl_dir_phys(dd)->dd_child_dir_zapobj);
2305 	mos_obj_refd(dsl_dir_phys(dd)->dd_deleg_zapobj);
2306 	mos_obj_refd(dsl_dir_phys(dd)->dd_props_zapobj);
2307 	mos_obj_refd(dsl_dir_phys(dd)->dd_clones);
2308 }
2309 
2310 static void
2311 count_ds_mos_objects(dsl_dataset_t *ds)
2312 {
2313 	mos_obj_refd(ds->ds_object);
2314 	mos_obj_refd(dsl_dataset_phys(ds)->ds_next_clones_obj);
2315 	mos_obj_refd(dsl_dataset_phys(ds)->ds_props_obj);
2316 	mos_obj_refd(dsl_dataset_phys(ds)->ds_userrefs_obj);
2317 	mos_obj_refd(dsl_dataset_phys(ds)->ds_snapnames_zapobj);
2318 
2319 	if (!dsl_dataset_is_snapshot(ds)) {
2320 		count_dir_mos_objects(ds->ds_dir);
2321 	}
2322 }
2323 
2324 static const char *objset_types[DMU_OST_NUMTYPES] = {
2325 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
2326 
2327 static void
2328 dump_dir(objset_t *os)
2329 {
2330 	dmu_objset_stats_t dds;
2331 	uint64_t object, object_count;
2332 	uint64_t refdbytes, usedobjs, scratch;
2333 	char numbuf[32];
2334 	char blkbuf[BP_SPRINTF_LEN + 20];
2335 	char osname[ZFS_MAX_DATASET_NAME_LEN];
2336 	const char *type = "UNKNOWN";
2337 	int verbosity = dump_opt['d'];
2338 	int print_header = 1;
2339 	unsigned i;
2340 	int error;
2341 	uint64_t total_slots_used = 0;
2342 	uint64_t max_slot_used = 0;
2343 	uint64_t dnode_slots;
2344 
2345 	/* make sure nicenum has enough space */
2346 	CTASSERT(sizeof (numbuf) >= NN_NUMBUF_SZ);
2347 
2348 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2349 	dmu_objset_fast_stat(os, &dds);
2350 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2351 
2352 	if (dds.dds_type < DMU_OST_NUMTYPES)
2353 		type = objset_types[dds.dds_type];
2354 
2355 	if (dds.dds_type == DMU_OST_META) {
2356 		dds.dds_creation_txg = TXG_INITIAL;
2357 		usedobjs = BP_GET_FILL(os->os_rootbp);
2358 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
2359 		    dd_used_bytes;
2360 	} else {
2361 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
2362 	}
2363 
2364 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
2365 
2366 	zdb_nicenum(refdbytes, numbuf, sizeof (numbuf));
2367 
2368 	if (verbosity >= 4) {
2369 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
2370 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
2371 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
2372 	} else {
2373 		blkbuf[0] = '\0';
2374 	}
2375 
2376 	dmu_objset_name(os, osname);
2377 
2378 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
2379 	    "%s, %llu objects%s%s\n",
2380 	    osname, type, (u_longlong_t)dmu_objset_id(os),
2381 	    (u_longlong_t)dds.dds_creation_txg,
2382 	    numbuf, (u_longlong_t)usedobjs, blkbuf,
2383 	    (dds.dds_inconsistent) ? " (inconsistent)" : "");
2384 
2385 	if (zopt_objects != 0) {
2386 		for (i = 0; i < zopt_objects; i++)
2387 			dump_object(os, zopt_object[i], verbosity,
2388 			    &print_header, NULL);
2389 		(void) printf("\n");
2390 		return;
2391 	}
2392 
2393 	if (dump_opt['i'] != 0 || verbosity >= 2)
2394 		dump_intent_log(dmu_objset_zil(os));
2395 
2396 	if (dmu_objset_ds(os) != NULL) {
2397 		dsl_dataset_t *ds = dmu_objset_ds(os);
2398 		dump_deadlist(&ds->ds_deadlist);
2399 
2400 		if (dsl_dataset_remap_deadlist_exists(ds)) {
2401 			(void) printf("ds_remap_deadlist:\n");
2402 			dump_deadlist(&ds->ds_remap_deadlist);
2403 		}
2404 		count_ds_mos_objects(ds);
2405 	}
2406 
2407 	if (verbosity < 2)
2408 		return;
2409 
2410 	if (BP_IS_HOLE(os->os_rootbp))
2411 		return;
2412 
2413 	dump_object(os, 0, verbosity, &print_header, NULL);
2414 	object_count = 0;
2415 	if (DMU_USERUSED_DNODE(os) != NULL &&
2416 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
2417 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header,
2418 		    NULL);
2419 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header,
2420 		    NULL);
2421 	}
2422 
2423 	if (DMU_PROJECTUSED_DNODE(os) != NULL &&
2424 	    DMU_PROJECTUSED_DNODE(os)->dn_type != 0)
2425 		dump_object(os, DMU_PROJECTUSED_OBJECT, verbosity,
2426 		    &print_header, NULL);
2427 
2428 	object = 0;
2429 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
2430 		dump_object(os, object, verbosity, &print_header, &dnode_slots);
2431 		object_count++;
2432 		total_slots_used += dnode_slots;
2433 		max_slot_used = object + dnode_slots - 1;
2434 	}
2435 
2436 	(void) printf("\n");
2437 
2438 	(void) printf("    Dnode slots:\n");
2439 	(void) printf("\tTotal used:    %10llu\n",
2440 	    (u_longlong_t)total_slots_used);
2441 	(void) printf("\tMax used:      %10llu\n",
2442 	    (u_longlong_t)max_slot_used);
2443 	(void) printf("\tPercent empty: %10lf\n",
2444 	    (double)(max_slot_used - total_slots_used)*100 /
2445 	    (double)max_slot_used);
2446 
2447 	(void) printf("\n");
2448 
2449 	if (error != ESRCH) {
2450 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2451 		abort();
2452 	}
2453 	if (leaked_objects != 0) {
2454 		(void) printf("%d potentially leaked objects detected\n",
2455 		    leaked_objects);
2456 		leaked_objects = 0;
2457 	}
2458 
2459 	ASSERT3U(object_count, ==, usedobjs);
2460 }
2461 
2462 static void
2463 dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2464 {
2465 	time_t timestamp = ub->ub_timestamp;
2466 
2467 	(void) printf("%s", header ? header : "");
2468 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2469 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2470 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2471 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2472 	(void) printf("\ttimestamp = %llu UTC = %s",
2473 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
2474 
2475 	(void) printf("\tmmp_magic = %016llx\n",
2476 	    (u_longlong_t)ub->ub_mmp_magic);
2477 	if (ub->ub_mmp_magic == MMP_MAGIC)
2478 		(void) printf("\tmmp_delay = %0llu\n",
2479 		    (u_longlong_t)ub->ub_mmp_delay);
2480 
2481 	if (dump_opt['u'] >= 3) {
2482 		char blkbuf[BP_SPRINTF_LEN];
2483 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2484 		(void) printf("\trootbp = %s\n", blkbuf);
2485 	}
2486 	(void) printf("\tcheckpoint_txg = %llu\n",
2487 	    (u_longlong_t)ub->ub_checkpoint_txg);
2488 	(void) printf("%s", footer ? footer : "");
2489 }
2490 
2491 static void
2492 dump_config(spa_t *spa)
2493 {
2494 	dmu_buf_t *db;
2495 	size_t nvsize = 0;
2496 	int error = 0;
2497 
2498 
2499 	error = dmu_bonus_hold(spa->spa_meta_objset,
2500 	    spa->spa_config_object, FTAG, &db);
2501 
2502 	if (error == 0) {
2503 		nvsize = *(uint64_t *)db->db_data;
2504 		dmu_buf_rele(db, FTAG);
2505 
2506 		(void) printf("\nMOS Configuration:\n");
2507 		dump_packed_nvlist(spa->spa_meta_objset,
2508 		    spa->spa_config_object, (void *)&nvsize, 1);
2509 	} else {
2510 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
2511 		    (u_longlong_t)spa->spa_config_object, error);
2512 	}
2513 }
2514 
2515 static void
2516 dump_cachefile(const char *cachefile)
2517 {
2518 	int fd;
2519 	struct stat64 statbuf;
2520 	char *buf;
2521 	nvlist_t *config;
2522 
2523 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2524 		(void) printf("cannot open '%s': %s\n", cachefile,
2525 		    strerror(errno));
2526 		exit(1);
2527 	}
2528 
2529 	if (fstat64(fd, &statbuf) != 0) {
2530 		(void) printf("failed to stat '%s': %s\n", cachefile,
2531 		    strerror(errno));
2532 		exit(1);
2533 	}
2534 
2535 	if ((buf = malloc(statbuf.st_size)) == NULL) {
2536 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
2537 		    (u_longlong_t)statbuf.st_size);
2538 		exit(1);
2539 	}
2540 
2541 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2542 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2543 		    (u_longlong_t)statbuf.st_size);
2544 		exit(1);
2545 	}
2546 
2547 	(void) close(fd);
2548 
2549 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2550 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2551 		exit(1);
2552 	}
2553 
2554 	free(buf);
2555 
2556 	dump_nvlist(config, 0);
2557 
2558 	nvlist_free(config);
2559 }
2560 
2561 #define	ZDB_MAX_UB_HEADER_SIZE 32
2562 
2563 static void
2564 dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
2565 {
2566 	vdev_t vd;
2567 	vdev_t *vdp = &vd;
2568 	char header[ZDB_MAX_UB_HEADER_SIZE];
2569 
2570 	vd.vdev_ashift = ashift;
2571 	vdp->vdev_top = vdp;
2572 
2573 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
2574 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
2575 		uberblock_t *ub = (void *)((char *)lbl + uoff);
2576 
2577 		if (uberblock_verify(ub))
2578 			continue;
2579 
2580 		if ((dump_opt['u'] < 4) &&
2581 		    (ub->ub_mmp_magic == MMP_MAGIC) && ub->ub_mmp_delay &&
2582 		    (i >= VDEV_UBERBLOCK_COUNT(&vd) - MMP_BLOCKS_PER_LABEL))
2583 			continue;
2584 
2585 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
2586 		    "Uberblock[%d]\n", i);
2587 		dump_uberblock(ub, header, "");
2588 	}
2589 }
2590 
2591 static char curpath[PATH_MAX];
2592 
2593 /*
2594  * Iterate through the path components, recursively passing
2595  * current one's obj and remaining path until we find the obj
2596  * for the last one.
2597  */
2598 static int
2599 dump_path_impl(objset_t *os, uint64_t obj, char *name)
2600 {
2601 	int err;
2602 	int header = 1;
2603 	uint64_t child_obj;
2604 	char *s;
2605 	dmu_buf_t *db;
2606 	dmu_object_info_t doi;
2607 
2608 	if ((s = strchr(name, '/')) != NULL)
2609 		*s = '\0';
2610 	err = zap_lookup(os, obj, name, 8, 1, &child_obj);
2611 
2612 	(void) strlcat(curpath, name, sizeof (curpath));
2613 
2614 	if (err != 0) {
2615 		(void) fprintf(stderr, "failed to lookup %s: %s\n",
2616 		    curpath, strerror(err));
2617 		return (err);
2618 	}
2619 
2620 	child_obj = ZFS_DIRENT_OBJ(child_obj);
2621 	err = sa_buf_hold(os, child_obj, FTAG, &db);
2622 	if (err != 0) {
2623 		(void) fprintf(stderr,
2624 		    "failed to get SA dbuf for obj %llu: %s\n",
2625 		    (u_longlong_t)child_obj, strerror(err));
2626 		return (EINVAL);
2627 	}
2628 	dmu_object_info_from_db(db, &doi);
2629 	sa_buf_rele(db, FTAG);
2630 
2631 	if (doi.doi_bonus_type != DMU_OT_SA &&
2632 	    doi.doi_bonus_type != DMU_OT_ZNODE) {
2633 		(void) fprintf(stderr, "invalid bonus type %d for obj %llu\n",
2634 		    doi.doi_bonus_type, (u_longlong_t)child_obj);
2635 		return (EINVAL);
2636 	}
2637 
2638 	if (dump_opt['v'] > 6) {
2639 		(void) printf("obj=%llu %s type=%d bonustype=%d\n",
2640 		    (u_longlong_t)child_obj, curpath, doi.doi_type,
2641 		    doi.doi_bonus_type);
2642 	}
2643 
2644 	(void) strlcat(curpath, "/", sizeof (curpath));
2645 
2646 	switch (doi.doi_type) {
2647 	case DMU_OT_DIRECTORY_CONTENTS:
2648 		if (s != NULL && *(s + 1) != '\0')
2649 			return (dump_path_impl(os, child_obj, s + 1));
2650 		/*FALLTHROUGH*/
2651 	case DMU_OT_PLAIN_FILE_CONTENTS:
2652 		dump_object(os, child_obj, dump_opt['v'], &header, NULL);
2653 		return (0);
2654 	default:
2655 		(void) fprintf(stderr, "object %llu has non-file/directory "
2656 		    "type %d\n", (u_longlong_t)obj, doi.doi_type);
2657 		break;
2658 	}
2659 
2660 	return (EINVAL);
2661 }
2662 
2663 /*
2664  * Dump the blocks for the object specified by path inside the dataset.
2665  */
2666 static int
2667 dump_path(char *ds, char *path)
2668 {
2669 	int err;
2670 	objset_t *os;
2671 	uint64_t root_obj;
2672 
2673 	err = open_objset(ds, DMU_OST_ZFS, FTAG, &os);
2674 	if (err != 0)
2675 		return (err);
2676 
2677 	err = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &root_obj);
2678 	if (err != 0) {
2679 		(void) fprintf(stderr, "can't lookup root znode: %s\n",
2680 		    strerror(err));
2681 		dmu_objset_disown(os, B_FALSE, FTAG);
2682 		return (EINVAL);
2683 	}
2684 
2685 	(void) snprintf(curpath, sizeof (curpath), "dataset=%s path=/", ds);
2686 
2687 	err = dump_path_impl(os, root_obj, path);
2688 
2689 	close_objset(os, FTAG);
2690 	return (err);
2691 }
2692 
2693 static int
2694 dump_label(const char *dev)
2695 {
2696 	int fd;
2697 	vdev_label_t label;
2698 	char path[MAXPATHLEN];
2699 	char *buf = label.vl_vdev_phys.vp_nvlist;
2700 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2701 	struct stat64 statbuf;
2702 	uint64_t psize, ashift;
2703 	boolean_t label_found = B_FALSE;
2704 
2705 	(void) strlcpy(path, dev, sizeof (path));
2706 	if (dev[0] == '/') {
2707 		if (strncmp(dev, ZFS_DISK_ROOTD,
2708 		    strlen(ZFS_DISK_ROOTD)) == 0) {
2709 			(void) snprintf(path, sizeof (path), "%s%s",
2710 			    ZFS_RDISK_ROOTD, dev + strlen(ZFS_DISK_ROOTD));
2711 		}
2712 	} else if (stat64(path, &statbuf) != 0) {
2713 		char *s;
2714 
2715 		(void) snprintf(path, sizeof (path), "%s%s", ZFS_RDISK_ROOTD,
2716 		    dev);
2717 		if (((s = strrchr(dev, 's')) == NULL &&
2718 		    (s = strchr(dev, 'p')) == NULL) ||
2719 		    !isdigit(*(s + 1)))
2720 			(void) strlcat(path, "s0", sizeof (path));
2721 	}
2722 
2723 	if ((fd = open64(path, O_RDONLY)) < 0) {
2724 		(void) fprintf(stderr, "cannot open '%s': %s\n", path,
2725 		    strerror(errno));
2726 		exit(1);
2727 	}
2728 
2729 	if (fstat64(fd, &statbuf) != 0) {
2730 		(void) fprintf(stderr, "failed to stat '%s': %s\n", path,
2731 		    strerror(errno));
2732 		(void) close(fd);
2733 		exit(1);
2734 	}
2735 
2736 	if (S_ISBLK(statbuf.st_mode)) {
2737 		(void) fprintf(stderr,
2738 		    "cannot use '%s': character device required\n", path);
2739 		(void) close(fd);
2740 		exit(1);
2741 	}
2742 
2743 	psize = statbuf.st_size;
2744 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2745 
2746 	for (int l = 0; l < VDEV_LABELS; l++) {
2747 		nvlist_t *config = NULL;
2748 
2749 		if (!dump_opt['q']) {
2750 			(void) printf("------------------------------------\n");
2751 			(void) printf("LABEL %d\n", l);
2752 			(void) printf("------------------------------------\n");
2753 		}
2754 
2755 		if (pread64(fd, &label, sizeof (label),
2756 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2757 			if (!dump_opt['q'])
2758 				(void) printf("failed to read label %d\n", l);
2759 			continue;
2760 		}
2761 
2762 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2763 			if (!dump_opt['q'])
2764 				(void) printf("failed to unpack label %d\n", l);
2765 			ashift = SPA_MINBLOCKSHIFT;
2766 		} else {
2767 			nvlist_t *vdev_tree = NULL;
2768 
2769 			if (!dump_opt['q'])
2770 				dump_nvlist(config, 4);
2771 			if ((nvlist_lookup_nvlist(config,
2772 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
2773 			    (nvlist_lookup_uint64(vdev_tree,
2774 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
2775 				ashift = SPA_MINBLOCKSHIFT;
2776 			nvlist_free(config);
2777 			label_found = B_TRUE;
2778 		}
2779 		if (dump_opt['u'])
2780 			dump_label_uberblocks(&label, ashift);
2781 	}
2782 
2783 	(void) close(fd);
2784 
2785 	return (label_found ? 0 : 2);
2786 }
2787 
2788 static uint64_t dataset_feature_count[SPA_FEATURES];
2789 static uint64_t remap_deadlist_count = 0;
2790 
2791 /*ARGSUSED*/
2792 static int
2793 dump_one_dir(const char *dsname, void *arg)
2794 {
2795 	int error;
2796 	objset_t *os;
2797 
2798 	error = open_objset(dsname, DMU_OST_ANY, FTAG, &os);
2799 	if (error != 0)
2800 		return (0);
2801 
2802 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2803 		if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2804 			continue;
2805 		ASSERT(spa_feature_table[f].fi_flags &
2806 		    ZFEATURE_FLAG_PER_DATASET);
2807 		dataset_feature_count[f]++;
2808 	}
2809 
2810 	if (dsl_dataset_remap_deadlist_exists(dmu_objset_ds(os))) {
2811 		remap_deadlist_count++;
2812 	}
2813 
2814 	dump_dir(os);
2815 	close_objset(os, FTAG);
2816 	fuid_table_destroy();
2817 	return (0);
2818 }
2819 
2820 /*
2821  * Block statistics.
2822  */
2823 #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2824 typedef struct zdb_blkstats {
2825 	uint64_t zb_asize;
2826 	uint64_t zb_lsize;
2827 	uint64_t zb_psize;
2828 	uint64_t zb_count;
2829 	uint64_t zb_gangs;
2830 	uint64_t zb_ditto_samevdev;
2831 	uint64_t zb_ditto_same_ms;
2832 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2833 } zdb_blkstats_t;
2834 
2835 /*
2836  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2837  */
2838 #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2839 #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2840 #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2841 #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2842 
2843 static const char *zdb_ot_extname[] = {
2844 	"deferred free",
2845 	"dedup ditto",
2846 	"other",
2847 	"Total",
2848 };
2849 
2850 #define	ZB_TOTAL	DN_MAX_LEVELS
2851 
2852 typedef struct zdb_cb {
2853 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2854 	uint64_t	zcb_removing_size;
2855 	uint64_t	zcb_checkpoint_size;
2856 	uint64_t	zcb_dedup_asize;
2857 	uint64_t	zcb_dedup_blocks;
2858 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
2859 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
2860 	    [BPE_PAYLOAD_SIZE];
2861 	uint64_t	zcb_start;
2862 	hrtime_t	zcb_lastprint;
2863 	uint64_t	zcb_totalasize;
2864 	uint64_t	zcb_errors[256];
2865 	int		zcb_readfails;
2866 	int		zcb_haderrors;
2867 	spa_t		*zcb_spa;
2868 	uint32_t	**zcb_vd_obsolete_counts;
2869 } zdb_cb_t;
2870 
2871 /* test if two DVA offsets from same vdev are within the same metaslab */
2872 static boolean_t
2873 same_metaslab(spa_t *spa, uint64_t vdev, uint64_t off1, uint64_t off2)
2874 {
2875 	vdev_t *vd = vdev_lookup_top(spa, vdev);
2876 	uint64_t ms_shift = vd->vdev_ms_shift;
2877 
2878 	return ((off1 >> ms_shift) == (off2 >> ms_shift));
2879 }
2880 
2881 static void
2882 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2883     dmu_object_type_t type)
2884 {
2885 	uint64_t refcnt = 0;
2886 
2887 	ASSERT(type < ZDB_OT_TOTAL);
2888 
2889 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2890 		return;
2891 
2892 	spa_config_enter(zcb->zcb_spa, SCL_CONFIG, FTAG, RW_READER);
2893 
2894 	for (int i = 0; i < 4; i++) {
2895 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2896 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2897 		int equal;
2898 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2899 
2900 		zb->zb_asize += BP_GET_ASIZE(bp);
2901 		zb->zb_lsize += BP_GET_LSIZE(bp);
2902 		zb->zb_psize += BP_GET_PSIZE(bp);
2903 		zb->zb_count++;
2904 
2905 		/*
2906 		 * The histogram is only big enough to record blocks up to
2907 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2908 		 * "other", bucket.
2909 		 */
2910 		unsigned idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2911 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2912 		zb->zb_psize_histogram[idx]++;
2913 
2914 		zb->zb_gangs += BP_COUNT_GANG(bp);
2915 
2916 		switch (BP_GET_NDVAS(bp)) {
2917 		case 2:
2918 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2919 			    DVA_GET_VDEV(&bp->blk_dva[1])) {
2920 				zb->zb_ditto_samevdev++;
2921 
2922 				if (same_metaslab(zcb->zcb_spa,
2923 				    DVA_GET_VDEV(&bp->blk_dva[0]),
2924 				    DVA_GET_OFFSET(&bp->blk_dva[0]),
2925 				    DVA_GET_OFFSET(&bp->blk_dva[1])))
2926 					zb->zb_ditto_same_ms++;
2927 			}
2928 			break;
2929 		case 3:
2930 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2931 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2932 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2933 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2934 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2935 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2936 			if (equal != 0) {
2937 				zb->zb_ditto_samevdev++;
2938 
2939 				if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2940 				    DVA_GET_VDEV(&bp->blk_dva[1]) &&
2941 				    same_metaslab(zcb->zcb_spa,
2942 				    DVA_GET_VDEV(&bp->blk_dva[0]),
2943 				    DVA_GET_OFFSET(&bp->blk_dva[0]),
2944 				    DVA_GET_OFFSET(&bp->blk_dva[1])))
2945 					zb->zb_ditto_same_ms++;
2946 				else if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2947 				    DVA_GET_VDEV(&bp->blk_dva[2]) &&
2948 				    same_metaslab(zcb->zcb_spa,
2949 				    DVA_GET_VDEV(&bp->blk_dva[0]),
2950 				    DVA_GET_OFFSET(&bp->blk_dva[0]),
2951 				    DVA_GET_OFFSET(&bp->blk_dva[2])))
2952 					zb->zb_ditto_same_ms++;
2953 				else if (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2954 				    DVA_GET_VDEV(&bp->blk_dva[2]) &&
2955 				    same_metaslab(zcb->zcb_spa,
2956 				    DVA_GET_VDEV(&bp->blk_dva[1]),
2957 				    DVA_GET_OFFSET(&bp->blk_dva[1]),
2958 				    DVA_GET_OFFSET(&bp->blk_dva[2])))
2959 					zb->zb_ditto_same_ms++;
2960 			}
2961 			break;
2962 		}
2963 	}
2964 
2965 	spa_config_exit(zcb->zcb_spa, SCL_CONFIG, FTAG);
2966 
2967 	if (BP_IS_EMBEDDED(bp)) {
2968 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
2969 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
2970 		    [BPE_GET_PSIZE(bp)]++;
2971 		return;
2972 	}
2973 
2974 	if (dump_opt['L'])
2975 		return;
2976 
2977 	if (BP_GET_DEDUP(bp)) {
2978 		ddt_t *ddt;
2979 		ddt_entry_t *dde;
2980 
2981 		ddt = ddt_select(zcb->zcb_spa, bp);
2982 		ddt_enter(ddt);
2983 		dde = ddt_lookup(ddt, bp, B_FALSE);
2984 
2985 		if (dde == NULL) {
2986 			refcnt = 0;
2987 		} else {
2988 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2989 			ddt_phys_decref(ddp);
2990 			refcnt = ddp->ddp_refcnt;
2991 			if (ddt_phys_total_refcnt(dde) == 0)
2992 				ddt_remove(ddt, dde);
2993 		}
2994 		ddt_exit(ddt);
2995 	}
2996 
2997 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2998 	    refcnt ? 0 : spa_min_claim_txg(zcb->zcb_spa),
2999 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
3000 }
3001 
3002 static void
3003 zdb_blkptr_done(zio_t *zio)
3004 {
3005 	spa_t *spa = zio->io_spa;
3006 	blkptr_t *bp = zio->io_bp;
3007 	int ioerr = zio->io_error;
3008 	zdb_cb_t *zcb = zio->io_private;
3009 	zbookmark_phys_t *zb = &zio->io_bookmark;
3010 
3011 	abd_free(zio->io_abd);
3012 
3013 	mutex_enter(&spa->spa_scrub_lock);
3014 	spa->spa_load_verify_ios--;
3015 	cv_broadcast(&spa->spa_scrub_io_cv);
3016 
3017 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
3018 		char blkbuf[BP_SPRINTF_LEN];
3019 
3020 		zcb->zcb_haderrors = 1;
3021 		zcb->zcb_errors[ioerr]++;
3022 
3023 		if (dump_opt['b'] >= 2)
3024 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3025 		else
3026 			blkbuf[0] = '\0';
3027 
3028 		(void) printf("zdb_blkptr_cb: "
3029 		    "Got error %d reading "
3030 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
3031 		    ioerr,
3032 		    (u_longlong_t)zb->zb_objset,
3033 		    (u_longlong_t)zb->zb_object,
3034 		    (u_longlong_t)zb->zb_level,
3035 		    (u_longlong_t)zb->zb_blkid,
3036 		    blkbuf);
3037 	}
3038 	mutex_exit(&spa->spa_scrub_lock);
3039 }
3040 
3041 static int
3042 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
3043     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
3044 {
3045 	zdb_cb_t *zcb = arg;
3046 	dmu_object_type_t type;
3047 	boolean_t is_metadata;
3048 
3049 	if (bp == NULL)
3050 		return (0);
3051 
3052 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
3053 		char blkbuf[BP_SPRINTF_LEN];
3054 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3055 		(void) printf("objset %llu object %llu "
3056 		    "level %lld offset 0x%llx %s\n",
3057 		    (u_longlong_t)zb->zb_objset,
3058 		    (u_longlong_t)zb->zb_object,
3059 		    (longlong_t)zb->zb_level,
3060 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
3061 		    blkbuf);
3062 	}
3063 
3064 	if (BP_IS_HOLE(bp))
3065 		return (0);
3066 
3067 	type = BP_GET_TYPE(bp);
3068 
3069 	zdb_count_block(zcb, zilog, bp,
3070 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
3071 
3072 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
3073 
3074 	if (!BP_IS_EMBEDDED(bp) &&
3075 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
3076 		size_t size = BP_GET_PSIZE(bp);
3077 		abd_t *abd = abd_alloc(size, B_FALSE);
3078 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
3079 
3080 		/* If it's an intent log block, failure is expected. */
3081 		if (zb->zb_level == ZB_ZIL_LEVEL)
3082 			flags |= ZIO_FLAG_SPECULATIVE;
3083 
3084 		mutex_enter(&spa->spa_scrub_lock);
3085 		while (spa->spa_load_verify_ios > max_inflight)
3086 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
3087 		spa->spa_load_verify_ios++;
3088 		mutex_exit(&spa->spa_scrub_lock);
3089 
3090 		zio_nowait(zio_read(NULL, spa, bp, abd, size,
3091 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
3092 	}
3093 
3094 	zcb->zcb_readfails = 0;
3095 
3096 	/* only call gethrtime() every 100 blocks */
3097 	static int iters;
3098 	if (++iters > 100)
3099 		iters = 0;
3100 	else
3101 		return (0);
3102 
3103 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
3104 		uint64_t now = gethrtime();
3105 		char buf[10];
3106 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
3107 		int kb_per_sec =
3108 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
3109 		int sec_remaining =
3110 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
3111 
3112 		/* make sure nicenum has enough space */
3113 		CTASSERT(sizeof (buf) >= NN_NUMBUF_SZ);
3114 
3115 		zfs_nicenum(bytes, buf, sizeof (buf));
3116 		(void) fprintf(stderr,
3117 		    "\r%5s completed (%4dMB/s) "
3118 		    "estimated time remaining: %uhr %02umin %02usec        ",
3119 		    buf, kb_per_sec / 1024,
3120 		    sec_remaining / 60 / 60,
3121 		    sec_remaining / 60 % 60,
3122 		    sec_remaining % 60);
3123 
3124 		zcb->zcb_lastprint = now;
3125 	}
3126 
3127 	return (0);
3128 }
3129 
3130 static void
3131 zdb_leak(void *arg, uint64_t start, uint64_t size)
3132 {
3133 	vdev_t *vd = arg;
3134 
3135 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
3136 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
3137 }
3138 
3139 static metaslab_ops_t zdb_metaslab_ops = {
3140 	NULL	/* alloc */
3141 };
3142 
3143 static void
3144 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
3145 {
3146 	ddt_bookmark_t ddb;
3147 	ddt_entry_t dde;
3148 	int error;
3149 
3150 	ASSERT(!dump_opt['L']);
3151 
3152 	bzero(&ddb, sizeof (ddb));
3153 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
3154 		blkptr_t blk;
3155 		ddt_phys_t *ddp = dde.dde_phys;
3156 
3157 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
3158 			return;
3159 
3160 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
3161 
3162 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
3163 			if (ddp->ddp_phys_birth == 0)
3164 				continue;
3165 			ddt_bp_create(ddb.ddb_checksum,
3166 			    &dde.dde_key, ddp, &blk);
3167 			if (p == DDT_PHYS_DITTO) {
3168 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
3169 			} else {
3170 				zcb->zcb_dedup_asize +=
3171 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
3172 				zcb->zcb_dedup_blocks++;
3173 			}
3174 		}
3175 		ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
3176 		ddt_enter(ddt);
3177 		VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
3178 		ddt_exit(ddt);
3179 	}
3180 
3181 	ASSERT(error == ENOENT);
3182 }
3183 
3184 /* ARGSUSED */
3185 static void
3186 claim_segment_impl_cb(uint64_t inner_offset, vdev_t *vd, uint64_t offset,
3187     uint64_t size, void *arg)
3188 {
3189 	/*
3190 	 * This callback was called through a remap from
3191 	 * a device being removed. Therefore, the vdev that
3192 	 * this callback is applied to is a concrete
3193 	 * vdev.
3194 	 */
3195 	ASSERT(vdev_is_concrete(vd));
3196 
3197 	VERIFY0(metaslab_claim_impl(vd, offset, size,
3198 	    spa_min_claim_txg(vd->vdev_spa)));
3199 }
3200 
3201 static void
3202 claim_segment_cb(void *arg, uint64_t offset, uint64_t size)
3203 {
3204 	vdev_t *vd = arg;
3205 
3206 	vdev_indirect_ops.vdev_op_remap(vd, offset, size,
3207 	    claim_segment_impl_cb, NULL);
3208 }
3209 
3210 /*
3211  * After accounting for all allocated blocks that are directly referenced,
3212  * we might have missed a reference to a block from a partially complete
3213  * (and thus unused) indirect mapping object. We perform a secondary pass
3214  * through the metaslabs we have already mapped and claim the destination
3215  * blocks.
3216  */
3217 static void
3218 zdb_claim_removing(spa_t *spa, zdb_cb_t *zcb)
3219 {
3220 	if (dump_opt['L'])
3221 		return;
3222 
3223 	if (spa->spa_vdev_removal == NULL)
3224 		return;
3225 
3226 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3227 
3228 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
3229 	vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
3230 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3231 
3232 	for (uint64_t msi = 0; msi < vd->vdev_ms_count; msi++) {
3233 		metaslab_t *msp = vd->vdev_ms[msi];
3234 
3235 		if (msp->ms_start >= vdev_indirect_mapping_max_offset(vim))
3236 			break;
3237 
3238 		ASSERT0(range_tree_space(svr->svr_allocd_segs));
3239 
3240 		if (msp->ms_sm != NULL) {
3241 			VERIFY0(space_map_load(msp->ms_sm,
3242 			    svr->svr_allocd_segs, SM_ALLOC));
3243 
3244 			/*
3245 			 * Clear everything past what has been synced unless
3246 			 * it's past the spacemap, because we have not allocated
3247 			 * mappings for it yet.
3248 			 */
3249 			uint64_t vim_max_offset =
3250 			    vdev_indirect_mapping_max_offset(vim);
3251 			uint64_t sm_end = msp->ms_sm->sm_start +
3252 			    msp->ms_sm->sm_size;
3253 			if (sm_end > vim_max_offset)
3254 				range_tree_clear(svr->svr_allocd_segs,
3255 				    vim_max_offset, sm_end - vim_max_offset);
3256 		}
3257 
3258 		zcb->zcb_removing_size +=
3259 		    range_tree_space(svr->svr_allocd_segs);
3260 		range_tree_vacate(svr->svr_allocd_segs, claim_segment_cb, vd);
3261 	}
3262 
3263 	spa_config_exit(spa, SCL_CONFIG, FTAG);
3264 }
3265 
3266 /* ARGSUSED */
3267 static int
3268 increment_indirect_mapping_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
3269 {
3270 	zdb_cb_t *zcb = arg;
3271 	spa_t *spa = zcb->zcb_spa;
3272 	vdev_t *vd;
3273 	const dva_t *dva = &bp->blk_dva[0];
3274 
3275 	ASSERT(!dump_opt['L']);
3276 	ASSERT3U(BP_GET_NDVAS(bp), ==, 1);
3277 
3278 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3279 	vd = vdev_lookup_top(zcb->zcb_spa, DVA_GET_VDEV(dva));
3280 	ASSERT3P(vd, !=, NULL);
3281 	spa_config_exit(spa, SCL_VDEV, FTAG);
3282 
3283 	ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0);
3284 	ASSERT3P(zcb->zcb_vd_obsolete_counts[vd->vdev_id], !=, NULL);
3285 
3286 	vdev_indirect_mapping_increment_obsolete_count(
3287 	    vd->vdev_indirect_mapping,
3288 	    DVA_GET_OFFSET(dva), DVA_GET_ASIZE(dva),
3289 	    zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
3290 
3291 	return (0);
3292 }
3293 
3294 static uint32_t *
3295 zdb_load_obsolete_counts(vdev_t *vd)
3296 {
3297 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3298 	spa_t *spa = vd->vdev_spa;
3299 	spa_condensing_indirect_phys_t *scip =
3300 	    &spa->spa_condensing_indirect_phys;
3301 	uint32_t *counts;
3302 
3303 	EQUIV(vdev_obsolete_sm_object(vd) != 0, vd->vdev_obsolete_sm != NULL);
3304 	counts = vdev_indirect_mapping_load_obsolete_counts(vim);
3305 	if (vd->vdev_obsolete_sm != NULL) {
3306 		vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
3307 		    vd->vdev_obsolete_sm);
3308 	}
3309 	if (scip->scip_vdev == vd->vdev_id &&
3310 	    scip->scip_prev_obsolete_sm_object != 0) {
3311 		space_map_t *prev_obsolete_sm = NULL;
3312 		VERIFY0(space_map_open(&prev_obsolete_sm, spa->spa_meta_objset,
3313 		    scip->scip_prev_obsolete_sm_object, 0, vd->vdev_asize, 0));
3314 		vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
3315 		    prev_obsolete_sm);
3316 		space_map_close(prev_obsolete_sm);
3317 	}
3318 	return (counts);
3319 }
3320 
3321 typedef struct checkpoint_sm_exclude_entry_arg {
3322 	vdev_t *cseea_vd;
3323 	uint64_t cseea_checkpoint_size;
3324 } checkpoint_sm_exclude_entry_arg_t;
3325 
3326 static int
3327 checkpoint_sm_exclude_entry_cb(space_map_entry_t *sme, void *arg)
3328 {
3329 	checkpoint_sm_exclude_entry_arg_t *cseea = arg;
3330 	vdev_t *vd = cseea->cseea_vd;
3331 	metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift];
3332 	uint64_t end = sme->sme_offset + sme->sme_run;
3333 
3334 	ASSERT(sme->sme_type == SM_FREE);
3335 
3336 	/*
3337 	 * Since the vdev_checkpoint_sm exists in the vdev level
3338 	 * and the ms_sm space maps exist in the metaslab level,
3339 	 * an entry in the checkpoint space map could theoretically
3340 	 * cross the boundaries of the metaslab that it belongs.
3341 	 *
3342 	 * In reality, because of the way that we populate and
3343 	 * manipulate the checkpoint's space maps currently,
3344 	 * there shouldn't be any entries that cross metaslabs.
3345 	 * Hence the assertion below.
3346 	 *
3347 	 * That said, there is no fundamental requirement that
3348 	 * the checkpoint's space map entries should not cross
3349 	 * metaslab boundaries. So if needed we could add code
3350 	 * that handles metaslab-crossing segments in the future.
3351 	 */
3352 	VERIFY3U(sme->sme_offset, >=, ms->ms_start);
3353 	VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
3354 
3355 	/*
3356 	 * By removing the entry from the allocated segments we
3357 	 * also verify that the entry is there to begin with.
3358 	 */
3359 	mutex_enter(&ms->ms_lock);
3360 	range_tree_remove(ms->ms_allocatable, sme->sme_offset, sme->sme_run);
3361 	mutex_exit(&ms->ms_lock);
3362 
3363 	cseea->cseea_checkpoint_size += sme->sme_run;
3364 	return (0);
3365 }
3366 
3367 static void
3368 zdb_leak_init_vdev_exclude_checkpoint(vdev_t *vd, zdb_cb_t *zcb)
3369 {
3370 	spa_t *spa = vd->vdev_spa;
3371 	space_map_t *checkpoint_sm = NULL;
3372 	uint64_t checkpoint_sm_obj;
3373 
3374 	/*
3375 	 * If there is no vdev_top_zap, we are in a pool whose
3376 	 * version predates the pool checkpoint feature.
3377 	 */
3378 	if (vd->vdev_top_zap == 0)
3379 		return;
3380 
3381 	/*
3382 	 * If there is no reference of the vdev_checkpoint_sm in
3383 	 * the vdev_top_zap, then one of the following scenarios
3384 	 * is true:
3385 	 *
3386 	 * 1] There is no checkpoint
3387 	 * 2] There is a checkpoint, but no checkpointed blocks
3388 	 *    have been freed yet
3389 	 * 3] The current vdev is indirect
3390 	 *
3391 	 * In these cases we return immediately.
3392 	 */
3393 	if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
3394 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
3395 		return;
3396 
3397 	VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
3398 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1,
3399 	    &checkpoint_sm_obj));
3400 
3401 	checkpoint_sm_exclude_entry_arg_t cseea;
3402 	cseea.cseea_vd = vd;
3403 	cseea.cseea_checkpoint_size = 0;
3404 
3405 	VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
3406 	    checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
3407 
3408 	VERIFY0(space_map_iterate(checkpoint_sm,
3409 	    space_map_length(checkpoint_sm),
3410 	    checkpoint_sm_exclude_entry_cb, &cseea));
3411 	space_map_close(checkpoint_sm);
3412 
3413 	zcb->zcb_checkpoint_size += cseea.cseea_checkpoint_size;
3414 }
3415 
3416 static void
3417 zdb_leak_init_exclude_checkpoint(spa_t *spa, zdb_cb_t *zcb)
3418 {
3419 	ASSERT(!dump_opt['L']);
3420 
3421 	vdev_t *rvd = spa->spa_root_vdev;
3422 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
3423 		ASSERT3U(c, ==, rvd->vdev_child[c]->vdev_id);
3424 		zdb_leak_init_vdev_exclude_checkpoint(rvd->vdev_child[c], zcb);
3425 	}
3426 }
3427 
3428 static void
3429 load_concrete_ms_allocatable_trees(spa_t *spa, maptype_t maptype)
3430 {
3431 	vdev_t *rvd = spa->spa_root_vdev;
3432 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
3433 		vdev_t *vd = rvd->vdev_child[i];
3434 
3435 		ASSERT3U(i, ==, vd->vdev_id);
3436 
3437 		if (vd->vdev_ops == &vdev_indirect_ops)
3438 			continue;
3439 
3440 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
3441 			metaslab_t *msp = vd->vdev_ms[m];
3442 
3443 			(void) fprintf(stderr,
3444 			    "\rloading concrete vdev %llu, "
3445 			    "metaslab %llu of %llu ...",
3446 			    (longlong_t)vd->vdev_id,
3447 			    (longlong_t)msp->ms_id,
3448 			    (longlong_t)vd->vdev_ms_count);
3449 
3450 			mutex_enter(&msp->ms_lock);
3451 			metaslab_unload(msp);
3452 
3453 			/*
3454 			 * We don't want to spend the CPU manipulating the
3455 			 * size-ordered tree, so clear the range_tree ops.
3456 			 */
3457 			msp->ms_allocatable->rt_ops = NULL;
3458 
3459 			if (msp->ms_sm != NULL) {
3460 				VERIFY0(space_map_load(msp->ms_sm,
3461 				    msp->ms_allocatable, maptype));
3462 			}
3463 			if (!msp->ms_loaded)
3464 				msp->ms_loaded = B_TRUE;
3465 			mutex_exit(&msp->ms_lock);
3466 		}
3467 	}
3468 }
3469 
3470 /*
3471  * vm_idxp is an in-out parameter which (for indirect vdevs) is the
3472  * index in vim_entries that has the first entry in this metaslab.
3473  * On return, it will be set to the first entry after this metaslab.
3474  */
3475 static void
3476 load_indirect_ms_allocatable_tree(vdev_t *vd, metaslab_t *msp,
3477     uint64_t *vim_idxp)
3478 {
3479 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3480 
3481 	mutex_enter(&msp->ms_lock);
3482 	metaslab_unload(msp);
3483 
3484 	/*
3485 	 * We don't want to spend the CPU manipulating the
3486 	 * size-ordered tree, so clear the range_tree ops.
3487 	 */
3488 	msp->ms_allocatable->rt_ops = NULL;
3489 
3490 	for (; *vim_idxp < vdev_indirect_mapping_num_entries(vim);
3491 	    (*vim_idxp)++) {
3492 		vdev_indirect_mapping_entry_phys_t *vimep =
3493 		    &vim->vim_entries[*vim_idxp];
3494 		uint64_t ent_offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
3495 		uint64_t ent_len = DVA_GET_ASIZE(&vimep->vimep_dst);
3496 		ASSERT3U(ent_offset, >=, msp->ms_start);
3497 		if (ent_offset >= msp->ms_start + msp->ms_size)
3498 			break;
3499 
3500 		/*
3501 		 * Mappings do not cross metaslab boundaries,
3502 		 * because we create them by walking the metaslabs.
3503 		 */
3504 		ASSERT3U(ent_offset + ent_len, <=,
3505 		    msp->ms_start + msp->ms_size);
3506 		range_tree_add(msp->ms_allocatable, ent_offset, ent_len);
3507 	}
3508 
3509 	if (!msp->ms_loaded)
3510 		msp->ms_loaded = B_TRUE;
3511 	mutex_exit(&msp->ms_lock);
3512 }
3513 
3514 static void
3515 zdb_leak_init_prepare_indirect_vdevs(spa_t *spa, zdb_cb_t *zcb)
3516 {
3517 	ASSERT(!dump_opt['L']);
3518 
3519 	vdev_t *rvd = spa->spa_root_vdev;
3520 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
3521 		vdev_t *vd = rvd->vdev_child[c];
3522 
3523 		ASSERT3U(c, ==, vd->vdev_id);
3524 
3525 		if (vd->vdev_ops != &vdev_indirect_ops)
3526 			continue;
3527 
3528 		/*
3529 		 * Note: we don't check for mapping leaks on
3530 		 * removing vdevs because their ms_allocatable's
3531 		 * are used to look for leaks in allocated space.
3532 		 */
3533 		zcb->zcb_vd_obsolete_counts[c] = zdb_load_obsolete_counts(vd);
3534 
3535 		/*
3536 		 * Normally, indirect vdevs don't have any
3537 		 * metaslabs.  We want to set them up for
3538 		 * zio_claim().
3539 		 */
3540 		VERIFY0(vdev_metaslab_init(vd, 0));
3541 
3542 		vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3543 		uint64_t vim_idx = 0;
3544 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
3545 
3546 			(void) fprintf(stderr,
3547 			    "\rloading indirect vdev %llu, "
3548 			    "metaslab %llu of %llu ...",
3549 			    (longlong_t)vd->vdev_id,
3550 			    (longlong_t)vd->vdev_ms[m]->ms_id,
3551 			    (longlong_t)vd->vdev_ms_count);
3552 
3553 			load_indirect_ms_allocatable_tree(vd, vd->vdev_ms[m],
3554 			    &vim_idx);
3555 		}
3556 		ASSERT3U(vim_idx, ==, vdev_indirect_mapping_num_entries(vim));
3557 	}
3558 }
3559 
3560 static void
3561 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
3562 {
3563 	zcb->zcb_spa = spa;
3564 
3565 	if (dump_opt['L'])
3566 		return;
3567 
3568 	dsl_pool_t *dp = spa->spa_dsl_pool;
3569 	vdev_t *rvd = spa->spa_root_vdev;
3570 
3571 	/*
3572 	 * We are going to be changing the meaning of the metaslab's
3573 	 * ms_allocatable.  Ensure that the allocator doesn't try to
3574 	 * use the tree.
3575 	 */
3576 	spa->spa_normal_class->mc_ops = &zdb_metaslab_ops;
3577 	spa->spa_log_class->mc_ops = &zdb_metaslab_ops;
3578 
3579 	zcb->zcb_vd_obsolete_counts =
3580 	    umem_zalloc(rvd->vdev_children * sizeof (uint32_t *),
3581 	    UMEM_NOFAIL);
3582 
3583 	/*
3584 	 * For leak detection, we overload the ms_allocatable trees
3585 	 * to contain allocated segments instead of free segments.
3586 	 * As a result, we can't use the normal metaslab_load/unload
3587 	 * interfaces.
3588 	 */
3589 	zdb_leak_init_prepare_indirect_vdevs(spa, zcb);
3590 	load_concrete_ms_allocatable_trees(spa, SM_ALLOC);
3591 
3592 	/*
3593 	 * On load_concrete_ms_allocatable_trees() we loaded all the
3594 	 * allocated entries from the ms_sm to the ms_allocatable for
3595 	 * each metaslab. If the pool has a checkpoint or is in the
3596 	 * middle of discarding a checkpoint, some of these blocks
3597 	 * may have been freed but their ms_sm may not have been
3598 	 * updated because they are referenced by the checkpoint. In
3599 	 * order to avoid false-positives during leak-detection, we
3600 	 * go through the vdev's checkpoint space map and exclude all
3601 	 * its entries from their relevant ms_allocatable.
3602 	 *
3603 	 * We also aggregate the space held by the checkpoint and add
3604 	 * it to zcb_checkpoint_size.
3605 	 *
3606 	 * Note that at this point we are also verifying that all the
3607 	 * entries on the checkpoint_sm are marked as allocated in
3608 	 * the ms_sm of their relevant metaslab.
3609 	 * [see comment in checkpoint_sm_exclude_entry_cb()]
3610 	 */
3611 	zdb_leak_init_exclude_checkpoint(spa, zcb);
3612 	ASSERT3U(zcb->zcb_checkpoint_size, ==, spa_get_checkpoint_space(spa));
3613 
3614 	/* for cleaner progress output */
3615 	(void) fprintf(stderr, "\n");
3616 
3617 	if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
3618 		ASSERT(spa_feature_is_enabled(spa,
3619 		    SPA_FEATURE_DEVICE_REMOVAL));
3620 		(void) bpobj_iterate_nofree(&dp->dp_obsolete_bpobj,
3621 		    increment_indirect_mapping_cb, zcb, NULL);
3622 	}
3623 
3624 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3625 	zdb_ddt_leak_init(spa, zcb);
3626 	spa_config_exit(spa, SCL_CONFIG, FTAG);
3627 }
3628 
3629 static boolean_t
3630 zdb_check_for_obsolete_leaks(vdev_t *vd, zdb_cb_t *zcb)
3631 {
3632 	boolean_t leaks = B_FALSE;
3633 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3634 	uint64_t total_leaked = 0;
3635 
3636 	ASSERT(vim != NULL);
3637 
3638 	for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
3639 		vdev_indirect_mapping_entry_phys_t *vimep =
3640 		    &vim->vim_entries[i];
3641 		uint64_t obsolete_bytes = 0;
3642 		uint64_t offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
3643 		metaslab_t *msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
3644 
3645 		/*
3646 		 * This is not very efficient but it's easy to
3647 		 * verify correctness.
3648 		 */
3649 		for (uint64_t inner_offset = 0;
3650 		    inner_offset < DVA_GET_ASIZE(&vimep->vimep_dst);
3651 		    inner_offset += 1 << vd->vdev_ashift) {
3652 			if (range_tree_contains(msp->ms_allocatable,
3653 			    offset + inner_offset, 1 << vd->vdev_ashift)) {
3654 				obsolete_bytes += 1 << vd->vdev_ashift;
3655 			}
3656 		}
3657 
3658 		int64_t bytes_leaked = obsolete_bytes -
3659 		    zcb->zcb_vd_obsolete_counts[vd->vdev_id][i];
3660 		ASSERT3U(DVA_GET_ASIZE(&vimep->vimep_dst), >=,
3661 		    zcb->zcb_vd_obsolete_counts[vd->vdev_id][i]);
3662 		if (bytes_leaked != 0 &&
3663 		    (vdev_obsolete_counts_are_precise(vd) ||
3664 		    dump_opt['d'] >= 5)) {
3665 			(void) printf("obsolete indirect mapping count "
3666 			    "mismatch on %llu:%llx:%llx : %llx bytes leaked\n",
3667 			    (u_longlong_t)vd->vdev_id,
3668 			    (u_longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
3669 			    (u_longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
3670 			    (u_longlong_t)bytes_leaked);
3671 		}
3672 		total_leaked += ABS(bytes_leaked);
3673 	}
3674 
3675 	if (!vdev_obsolete_counts_are_precise(vd) && total_leaked > 0) {
3676 		int pct_leaked = total_leaked * 100 /
3677 		    vdev_indirect_mapping_bytes_mapped(vim);
3678 		(void) printf("cannot verify obsolete indirect mapping "
3679 		    "counts of vdev %llu because precise feature was not "
3680 		    "enabled when it was removed: %d%% (%llx bytes) of mapping"
3681 		    "unreferenced\n",
3682 		    (u_longlong_t)vd->vdev_id, pct_leaked,
3683 		    (u_longlong_t)total_leaked);
3684 	} else if (total_leaked > 0) {
3685 		(void) printf("obsolete indirect mapping count mismatch "
3686 		    "for vdev %llu -- %llx total bytes mismatched\n",
3687 		    (u_longlong_t)vd->vdev_id,
3688 		    (u_longlong_t)total_leaked);
3689 		leaks |= B_TRUE;
3690 	}
3691 
3692 	vdev_indirect_mapping_free_obsolete_counts(vim,
3693 	    zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
3694 	zcb->zcb_vd_obsolete_counts[vd->vdev_id] = NULL;
3695 
3696 	return (leaks);
3697 }
3698 
3699 static boolean_t
3700 zdb_leak_fini(spa_t *spa, zdb_cb_t *zcb)
3701 {
3702 	if (dump_opt['L'])
3703 		return (B_FALSE);
3704 
3705 	boolean_t leaks = B_FALSE;
3706 
3707 	vdev_t *rvd = spa->spa_root_vdev;
3708 	for (unsigned c = 0; c < rvd->vdev_children; c++) {
3709 		vdev_t *vd = rvd->vdev_child[c];
3710 #if DEBUG
3711 		metaslab_group_t *mg = vd->vdev_mg;
3712 #endif
3713 
3714 		if (zcb->zcb_vd_obsolete_counts[c] != NULL) {
3715 			leaks |= zdb_check_for_obsolete_leaks(vd, zcb);
3716 		}
3717 
3718 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
3719 			metaslab_t *msp = vd->vdev_ms[m];
3720 			ASSERT3P(mg, ==, msp->ms_group);
3721 
3722 			/*
3723 			 * ms_allocatable has been overloaded
3724 			 * to contain allocated segments. Now that
3725 			 * we finished traversing all blocks, any
3726 			 * block that remains in the ms_allocatable
3727 			 * represents an allocated block that we
3728 			 * did not claim during the traversal.
3729 			 * Claimed blocks would have been removed
3730 			 * from the ms_allocatable.  For indirect
3731 			 * vdevs, space remaining in the tree
3732 			 * represents parts of the mapping that are
3733 			 * not referenced, which is not a bug.
3734 			 */
3735 			if (vd->vdev_ops == &vdev_indirect_ops) {
3736 				range_tree_vacate(msp->ms_allocatable,
3737 				    NULL, NULL);
3738 			} else {
3739 				range_tree_vacate(msp->ms_allocatable,
3740 				    zdb_leak, vd);
3741 			}
3742 
3743 			if (msp->ms_loaded) {
3744 				msp->ms_loaded = B_FALSE;
3745 			}
3746 		}
3747 
3748 	}
3749 
3750 	umem_free(zcb->zcb_vd_obsolete_counts,
3751 	    rvd->vdev_children * sizeof (uint32_t *));
3752 	zcb->zcb_vd_obsolete_counts = NULL;
3753 
3754 	return (leaks);
3755 }
3756 
3757 /* ARGSUSED */
3758 static int
3759 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
3760 {
3761 	zdb_cb_t *zcb = arg;
3762 
3763 	if (dump_opt['b'] >= 5) {
3764 		char blkbuf[BP_SPRINTF_LEN];
3765 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3766 		(void) printf("[%s] %s\n",
3767 		    "deferred free", blkbuf);
3768 	}
3769 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
3770 	return (0);
3771 }
3772 
3773 static int
3774 dump_block_stats(spa_t *spa)
3775 {
3776 	zdb_cb_t zcb;
3777 	zdb_blkstats_t *zb, *tzb;
3778 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
3779 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
3780 	    TRAVERSE_NO_DECRYPT | TRAVERSE_HARD;
3781 	boolean_t leaks = B_FALSE;
3782 	int err;
3783 
3784 	bzero(&zcb, sizeof (zcb));
3785 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
3786 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
3787 	    (dump_opt['c'] == 1) ? "metadata " : "",
3788 	    dump_opt['c'] ? "checksums " : "",
3789 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
3790 	    !dump_opt['L'] ? "nothing leaked " : "");
3791 
3792 	/*
3793 	 * When leak detection is enabled we load all space maps as SM_ALLOC
3794 	 * maps, then traverse the pool claiming each block we discover. If
3795 	 * the pool is perfectly consistent, the segment trees will be empty
3796 	 * when we're done. Anything left over is a leak; any block we can't
3797 	 * claim (because it's not part of any space map) is a double
3798 	 * allocation, reference to a freed block, or an unclaimed log block.
3799 	 *
3800 	 * When leak detection is disabled (-L option) we still traverse the
3801 	 * pool claiming each block we discover, but we skip opening any space
3802 	 * maps.
3803 	 */
3804 	bzero(&zcb, sizeof (zdb_cb_t));
3805 	zdb_leak_init(spa, &zcb);
3806 
3807 	/*
3808 	 * If there's a deferred-free bplist, process that first.
3809 	 */
3810 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
3811 	    count_block_cb, &zcb, NULL);
3812 
3813 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3814 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
3815 		    count_block_cb, &zcb, NULL);
3816 	}
3817 
3818 	zdb_claim_removing(spa, &zcb);
3819 
3820 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
3821 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
3822 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
3823 		    &zcb, NULL));
3824 	}
3825 
3826 	if (dump_opt['c'] > 1)
3827 		flags |= TRAVERSE_PREFETCH_DATA;
3828 
3829 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
3830 	zcb.zcb_totalasize += metaslab_class_get_alloc(spa_special_class(spa));
3831 	zcb.zcb_totalasize += metaslab_class_get_alloc(spa_dedup_class(spa));
3832 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
3833 	err = traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
3834 
3835 	/*
3836 	 * If we've traversed the data blocks then we need to wait for those
3837 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
3838 	 * all async I/Os to complete.
3839 	 */
3840 	if (dump_opt['c']) {
3841 		for (int i = 0; i < max_ncpus; i++) {
3842 			(void) zio_wait(spa->spa_async_zio_root[i]);
3843 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
3844 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3845 			    ZIO_FLAG_GODFATHER);
3846 		}
3847 	}
3848 
3849 	/*
3850 	 * Done after zio_wait() since zcb_haderrors is modified in
3851 	 * zdb_blkptr_done()
3852 	 */
3853 	zcb.zcb_haderrors |= err;
3854 
3855 	if (zcb.zcb_haderrors) {
3856 		(void) printf("\nError counts:\n\n");
3857 		(void) printf("\t%5s  %s\n", "errno", "count");
3858 		for (int e = 0; e < 256; e++) {
3859 			if (zcb.zcb_errors[e] != 0) {
3860 				(void) printf("\t%5d  %llu\n",
3861 				    e, (u_longlong_t)zcb.zcb_errors[e]);
3862 			}
3863 		}
3864 	}
3865 
3866 	/*
3867 	 * Report any leaked segments.
3868 	 */
3869 	leaks |= zdb_leak_fini(spa, &zcb);
3870 
3871 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
3872 
3873 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
3874 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
3875 
3876 	total_alloc = norm_alloc +
3877 	    metaslab_class_get_alloc(spa_log_class(spa)) +
3878 	    metaslab_class_get_alloc(spa_special_class(spa)) +
3879 	    metaslab_class_get_alloc(spa_dedup_class(spa));
3880 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize +
3881 	    zcb.zcb_removing_size + zcb.zcb_checkpoint_size;
3882 
3883 	if (total_found == total_alloc && !dump_opt['L']) {
3884 		(void) printf("\n\tNo leaks (block sum matches space"
3885 		    " maps exactly)\n");
3886 	} else if (!dump_opt['L']) {
3887 		(void) printf("block traversal size %llu != alloc %llu "
3888 		    "(%s %lld)\n",
3889 		    (u_longlong_t)total_found,
3890 		    (u_longlong_t)total_alloc,
3891 		    (dump_opt['L']) ? "unreachable" : "leaked",
3892 		    (longlong_t)(total_alloc - total_found));
3893 		leaks = B_TRUE;
3894 	}
3895 
3896 	if (tzb->zb_count == 0)
3897 		return (2);
3898 
3899 	(void) printf("\n");
3900 	(void) printf("\t%-16s %14llu\n", "bp count:",
3901 	    (u_longlong_t)tzb->zb_count);
3902 	(void) printf("\t%-16s %14llu\n", "ganged count:",
3903 	    (longlong_t)tzb->zb_gangs);
3904 	(void) printf("\t%-16s %14llu      avg: %6llu\n", "bp logical:",
3905 	    (u_longlong_t)tzb->zb_lsize,
3906 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
3907 	(void) printf("\t%-16s %14llu      avg: %6llu     compression: %6.2f\n",
3908 	    "bp physical:", (u_longlong_t)tzb->zb_psize,
3909 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
3910 	    (double)tzb->zb_lsize / tzb->zb_psize);
3911 	(void) printf("\t%-16s %14llu      avg: %6llu     compression: %6.2f\n",
3912 	    "bp allocated:", (u_longlong_t)tzb->zb_asize,
3913 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
3914 	    (double)tzb->zb_lsize / tzb->zb_asize);
3915 	(void) printf("\t%-16s %14llu    ref>1: %6llu   deduplication: %6.2f\n",
3916 	    "bp deduped:", (u_longlong_t)zcb.zcb_dedup_asize,
3917 	    (u_longlong_t)zcb.zcb_dedup_blocks,
3918 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
3919 	(void) printf("\t%-16s %14llu     used: %5.2f%%\n", "Normal class:",
3920 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
3921 
3922 	if (spa_special_class(spa)->mc_rotor != NULL) {
3923 		uint64_t alloc = metaslab_class_get_alloc(
3924 		    spa_special_class(spa));
3925 		uint64_t space = metaslab_class_get_space(
3926 		    spa_special_class(spa));
3927 
3928 		(void) printf("\t%-16s %14llu     used: %5.2f%%\n",
3929 		    "Special class", (u_longlong_t)alloc,
3930 		    100.0 * alloc / space);
3931 	}
3932 
3933 	if (spa_dedup_class(spa)->mc_rotor != NULL) {
3934 		uint64_t alloc = metaslab_class_get_alloc(
3935 		    spa_dedup_class(spa));
3936 		uint64_t space = metaslab_class_get_space(
3937 		    spa_dedup_class(spa));
3938 
3939 		(void) printf("\t%-16s %14llu     used: %5.2f%%\n",
3940 		    "Dedup class", (u_longlong_t)alloc,
3941 		    100.0 * alloc / space);
3942 	}
3943 
3944 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
3945 		if (zcb.zcb_embedded_blocks[i] == 0)
3946 			continue;
3947 		(void) printf("\n");
3948 		(void) printf("\tadditional, non-pointer bps of type %u: "
3949 		    "%10llu\n",
3950 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
3951 
3952 		if (dump_opt['b'] >= 3) {
3953 			(void) printf("\t number of (compressed) bytes:  "
3954 			    "number of bps\n");
3955 			dump_histogram(zcb.zcb_embedded_histogram[i],
3956 			    sizeof (zcb.zcb_embedded_histogram[i]) /
3957 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
3958 		}
3959 	}
3960 
3961 	if (tzb->zb_ditto_samevdev != 0) {
3962 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
3963 		    (longlong_t)tzb->zb_ditto_samevdev);
3964 	}
3965 	if (tzb->zb_ditto_same_ms != 0) {
3966 		(void) printf("\tDittoed blocks in same metaslab: %llu\n",
3967 		    (longlong_t)tzb->zb_ditto_same_ms);
3968 	}
3969 
3970 	for (uint64_t v = 0; v < spa->spa_root_vdev->vdev_children; v++) {
3971 		vdev_t *vd = spa->spa_root_vdev->vdev_child[v];
3972 		vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3973 
3974 		if (vim == NULL) {
3975 			continue;
3976 		}
3977 
3978 		char mem[32];
3979 		zdb_nicenum(vdev_indirect_mapping_num_entries(vim),
3980 		    mem, vdev_indirect_mapping_size(vim));
3981 
3982 		(void) printf("\tindirect vdev id %llu has %llu segments "
3983 		    "(%s in memory)\n",
3984 		    (longlong_t)vd->vdev_id,
3985 		    (longlong_t)vdev_indirect_mapping_num_entries(vim), mem);
3986 	}
3987 
3988 	if (dump_opt['b'] >= 2) {
3989 		int l, t, level;
3990 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
3991 		    "\t  avg\t comp\t%%Total\tType\n");
3992 
3993 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
3994 			char csize[32], lsize[32], psize[32], asize[32];
3995 			char avg[32], gang[32];
3996 			const char *typename;
3997 
3998 			/* make sure nicenum has enough space */
3999 			CTASSERT(sizeof (csize) >= NN_NUMBUF_SZ);
4000 			CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
4001 			CTASSERT(sizeof (psize) >= NN_NUMBUF_SZ);
4002 			CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
4003 			CTASSERT(sizeof (avg) >= NN_NUMBUF_SZ);
4004 			CTASSERT(sizeof (gang) >= NN_NUMBUF_SZ);
4005 
4006 			if (t < DMU_OT_NUMTYPES)
4007 				typename = dmu_ot[t].ot_name;
4008 			else
4009 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
4010 
4011 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
4012 				(void) printf("%6s\t%5s\t%5s\t%5s"
4013 				    "\t%5s\t%5s\t%6s\t%s\n",
4014 				    "-",
4015 				    "-",
4016 				    "-",
4017 				    "-",
4018 				    "-",
4019 				    "-",
4020 				    "-",
4021 				    typename);
4022 				continue;
4023 			}
4024 
4025 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
4026 				level = (l == -1 ? ZB_TOTAL : l);
4027 				zb = &zcb.zcb_type[level][t];
4028 
4029 				if (zb->zb_asize == 0)
4030 					continue;
4031 
4032 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
4033 					continue;
4034 
4035 				if (level == 0 && zb->zb_asize ==
4036 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
4037 					continue;
4038 
4039 				zdb_nicenum(zb->zb_count, csize,
4040 				    sizeof (csize));
4041 				zdb_nicenum(zb->zb_lsize, lsize,
4042 				    sizeof (lsize));
4043 				zdb_nicenum(zb->zb_psize, psize,
4044 				    sizeof (psize));
4045 				zdb_nicenum(zb->zb_asize, asize,
4046 				    sizeof (asize));
4047 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg,
4048 				    sizeof (avg));
4049 				zdb_nicenum(zb->zb_gangs, gang, sizeof (gang));
4050 
4051 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
4052 				    "\t%5.2f\t%6.2f\t",
4053 				    csize, lsize, psize, asize, avg,
4054 				    (double)zb->zb_lsize / zb->zb_psize,
4055 				    100.0 * zb->zb_asize / tzb->zb_asize);
4056 
4057 				if (level == ZB_TOTAL)
4058 					(void) printf("%s\n", typename);
4059 				else
4060 					(void) printf("    L%d %s\n",
4061 					    level, typename);
4062 
4063 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
4064 					(void) printf("\t number of ganged "
4065 					    "blocks: %s\n", gang);
4066 				}
4067 
4068 				if (dump_opt['b'] >= 4) {
4069 					(void) printf("psize "
4070 					    "(in 512-byte sectors): "
4071 					    "number of blocks\n");
4072 					dump_histogram(zb->zb_psize_histogram,
4073 					    PSIZE_HISTO_SIZE, 0);
4074 				}
4075 			}
4076 		}
4077 	}
4078 
4079 	(void) printf("\n");
4080 
4081 	if (leaks)
4082 		return (2);
4083 
4084 	if (zcb.zcb_haderrors)
4085 		return (3);
4086 
4087 	return (0);
4088 }
4089 
4090 typedef struct zdb_ddt_entry {
4091 	ddt_key_t	zdde_key;
4092 	uint64_t	zdde_ref_blocks;
4093 	uint64_t	zdde_ref_lsize;
4094 	uint64_t	zdde_ref_psize;
4095 	uint64_t	zdde_ref_dsize;
4096 	avl_node_t	zdde_node;
4097 } zdb_ddt_entry_t;
4098 
4099 /* ARGSUSED */
4100 static int
4101 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
4102     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
4103 {
4104 	avl_tree_t *t = arg;
4105 	avl_index_t where;
4106 	zdb_ddt_entry_t *zdde, zdde_search;
4107 
4108 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
4109 		return (0);
4110 
4111 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
4112 		(void) printf("traversing objset %llu, %llu objects, "
4113 		    "%lu blocks so far\n",
4114 		    (u_longlong_t)zb->zb_objset,
4115 		    (u_longlong_t)BP_GET_FILL(bp),
4116 		    avl_numnodes(t));
4117 	}
4118 
4119 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
4120 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
4121 		return (0);
4122 
4123 	ddt_key_fill(&zdde_search.zdde_key, bp);
4124 
4125 	zdde = avl_find(t, &zdde_search, &where);
4126 
4127 	if (zdde == NULL) {
4128 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
4129 		zdde->zdde_key = zdde_search.zdde_key;
4130 		avl_insert(t, zdde, where);
4131 	}
4132 
4133 	zdde->zdde_ref_blocks += 1;
4134 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
4135 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
4136 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
4137 
4138 	return (0);
4139 }
4140 
4141 static void
4142 dump_simulated_ddt(spa_t *spa)
4143 {
4144 	avl_tree_t t;
4145 	void *cookie = NULL;
4146 	zdb_ddt_entry_t *zdde;
4147 	ddt_histogram_t ddh_total;
4148 	ddt_stat_t dds_total;
4149 
4150 	bzero(&ddh_total, sizeof (ddh_total));
4151 	bzero(&dds_total, sizeof (dds_total));
4152 	avl_create(&t, ddt_entry_compare,
4153 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
4154 
4155 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4156 
4157 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
4158 	    TRAVERSE_NO_DECRYPT, zdb_ddt_add_cb, &t);
4159 
4160 	spa_config_exit(spa, SCL_CONFIG, FTAG);
4161 
4162 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
4163 		ddt_stat_t dds;
4164 		uint64_t refcnt = zdde->zdde_ref_blocks;
4165 		ASSERT(refcnt != 0);
4166 
4167 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
4168 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
4169 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
4170 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
4171 
4172 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
4173 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
4174 		dds.dds_ref_psize = zdde->zdde_ref_psize;
4175 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
4176 
4177 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
4178 		    &dds, 0);
4179 
4180 		umem_free(zdde, sizeof (*zdde));
4181 	}
4182 
4183 	avl_destroy(&t);
4184 
4185 	ddt_histogram_stat(&dds_total, &ddh_total);
4186 
4187 	(void) printf("Simulated DDT histogram:\n");
4188 
4189 	zpool_dump_ddt(&dds_total, &ddh_total);
4190 
4191 	dump_dedup_ratio(&dds_total);
4192 }
4193 
4194 static int
4195 verify_device_removal_feature_counts(spa_t *spa)
4196 {
4197 	uint64_t dr_feature_refcount = 0;
4198 	uint64_t oc_feature_refcount = 0;
4199 	uint64_t indirect_vdev_count = 0;
4200 	uint64_t precise_vdev_count = 0;
4201 	uint64_t obsolete_counts_object_count = 0;
4202 	uint64_t obsolete_sm_count = 0;
4203 	uint64_t obsolete_counts_count = 0;
4204 	uint64_t scip_count = 0;
4205 	uint64_t obsolete_bpobj_count = 0;
4206 	int ret = 0;
4207 
4208 	spa_condensing_indirect_phys_t *scip =
4209 	    &spa->spa_condensing_indirect_phys;
4210 	if (scip->scip_next_mapping_object != 0) {
4211 		vdev_t *vd = spa->spa_root_vdev->vdev_child[scip->scip_vdev];
4212 		ASSERT(scip->scip_prev_obsolete_sm_object != 0);
4213 		ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
4214 
4215 		(void) printf("Condensing indirect vdev %llu: new mapping "
4216 		    "object %llu, prev obsolete sm %llu\n",
4217 		    (u_longlong_t)scip->scip_vdev,
4218 		    (u_longlong_t)scip->scip_next_mapping_object,
4219 		    (u_longlong_t)scip->scip_prev_obsolete_sm_object);
4220 		if (scip->scip_prev_obsolete_sm_object != 0) {
4221 			space_map_t *prev_obsolete_sm = NULL;
4222 			VERIFY0(space_map_open(&prev_obsolete_sm,
4223 			    spa->spa_meta_objset,
4224 			    scip->scip_prev_obsolete_sm_object,
4225 			    0, vd->vdev_asize, 0));
4226 			dump_spacemap(spa->spa_meta_objset, prev_obsolete_sm);
4227 			(void) printf("\n");
4228 			space_map_close(prev_obsolete_sm);
4229 		}
4230 
4231 		scip_count += 2;
4232 	}
4233 
4234 	for (uint64_t i = 0; i < spa->spa_root_vdev->vdev_children; i++) {
4235 		vdev_t *vd = spa->spa_root_vdev->vdev_child[i];
4236 		vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
4237 
4238 		if (vic->vic_mapping_object != 0) {
4239 			ASSERT(vd->vdev_ops == &vdev_indirect_ops ||
4240 			    vd->vdev_removing);
4241 			indirect_vdev_count++;
4242 
4243 			if (vd->vdev_indirect_mapping->vim_havecounts) {
4244 				obsolete_counts_count++;
4245 			}
4246 		}
4247 		if (vdev_obsolete_counts_are_precise(vd)) {
4248 			ASSERT(vic->vic_mapping_object != 0);
4249 			precise_vdev_count++;
4250 		}
4251 		if (vdev_obsolete_sm_object(vd) != 0) {
4252 			ASSERT(vic->vic_mapping_object != 0);
4253 			obsolete_sm_count++;
4254 		}
4255 	}
4256 
4257 	(void) feature_get_refcount(spa,
4258 	    &spa_feature_table[SPA_FEATURE_DEVICE_REMOVAL],
4259 	    &dr_feature_refcount);
4260 	(void) feature_get_refcount(spa,
4261 	    &spa_feature_table[SPA_FEATURE_OBSOLETE_COUNTS],
4262 	    &oc_feature_refcount);
4263 
4264 	if (dr_feature_refcount != indirect_vdev_count) {
4265 		ret = 1;
4266 		(void) printf("Number of indirect vdevs (%llu) " \
4267 		    "does not match feature count (%llu)\n",
4268 		    (u_longlong_t)indirect_vdev_count,
4269 		    (u_longlong_t)dr_feature_refcount);
4270 	} else {
4271 		(void) printf("Verified device_removal feature refcount " \
4272 		    "of %llu is correct\n",
4273 		    (u_longlong_t)dr_feature_refcount);
4274 	}
4275 
4276 	if (zap_contains(spa_meta_objset(spa), DMU_POOL_DIRECTORY_OBJECT,
4277 	    DMU_POOL_OBSOLETE_BPOBJ) == 0) {
4278 		obsolete_bpobj_count++;
4279 	}
4280 
4281 
4282 	obsolete_counts_object_count = precise_vdev_count;
4283 	obsolete_counts_object_count += obsolete_sm_count;
4284 	obsolete_counts_object_count += obsolete_counts_count;
4285 	obsolete_counts_object_count += scip_count;
4286 	obsolete_counts_object_count += obsolete_bpobj_count;
4287 	obsolete_counts_object_count += remap_deadlist_count;
4288 
4289 	if (oc_feature_refcount != obsolete_counts_object_count) {
4290 		ret = 1;
4291 		(void) printf("Number of obsolete counts objects (%llu) " \
4292 		    "does not match feature count (%llu)\n",
4293 		    (u_longlong_t)obsolete_counts_object_count,
4294 		    (u_longlong_t)oc_feature_refcount);
4295 		(void) printf("pv:%llu os:%llu oc:%llu sc:%llu "
4296 		    "ob:%llu rd:%llu\n",
4297 		    (u_longlong_t)precise_vdev_count,
4298 		    (u_longlong_t)obsolete_sm_count,
4299 		    (u_longlong_t)obsolete_counts_count,
4300 		    (u_longlong_t)scip_count,
4301 		    (u_longlong_t)obsolete_bpobj_count,
4302 		    (u_longlong_t)remap_deadlist_count);
4303 	} else {
4304 		(void) printf("Verified indirect_refcount feature refcount " \
4305 		    "of %llu is correct\n",
4306 		    (u_longlong_t)oc_feature_refcount);
4307 	}
4308 	return (ret);
4309 }
4310 
4311 static void
4312 zdb_set_skip_mmp(char *target)
4313 {
4314 	spa_t *spa;
4315 
4316 	/*
4317 	 * Disable the activity check to allow examination of
4318 	 * active pools.
4319 	 */
4320 	mutex_enter(&spa_namespace_lock);
4321 	if ((spa = spa_lookup(target)) != NULL) {
4322 		spa->spa_import_flags |= ZFS_IMPORT_SKIP_MMP;
4323 	}
4324 	mutex_exit(&spa_namespace_lock);
4325 }
4326 
4327 #define	BOGUS_SUFFIX "_CHECKPOINTED_UNIVERSE"
4328 /*
4329  * Import the checkpointed state of the pool specified by the target
4330  * parameter as readonly. The function also accepts a pool config
4331  * as an optional parameter, else it attempts to infer the config by
4332  * the name of the target pool.
4333  *
4334  * Note that the checkpointed state's pool name will be the name of
4335  * the original pool with the above suffix appened to it. In addition,
4336  * if the target is not a pool name (e.g. a path to a dataset) then
4337  * the new_path parameter is populated with the updated path to
4338  * reflect the fact that we are looking into the checkpointed state.
4339  *
4340  * The function returns a newly-allocated copy of the name of the
4341  * pool containing the checkpointed state. When this copy is no
4342  * longer needed it should be freed with free(3C). Same thing
4343  * applies to the new_path parameter if allocated.
4344  */
4345 static char *
4346 import_checkpointed_state(char *target, nvlist_t *cfg, char **new_path)
4347 {
4348 	int error = 0;
4349 	char *poolname, *bogus_name;
4350 
4351 	/* If the target is not a pool, the extract the pool name */
4352 	char *path_start = strchr(target, '/');
4353 	if (path_start != NULL) {
4354 		size_t poolname_len = path_start - target;
4355 		poolname = strndup(target, poolname_len);
4356 	} else {
4357 		poolname = target;
4358 	}
4359 
4360 	if (cfg == NULL) {
4361 		zdb_set_skip_mmp(poolname);
4362 		error = spa_get_stats(poolname, &cfg, NULL, 0);
4363 		if (error != 0) {
4364 			fatal("Tried to read config of pool \"%s\" but "
4365 			    "spa_get_stats() failed with error %d\n",
4366 			    poolname, error);
4367 		}
4368 	}
4369 
4370 	(void) asprintf(&bogus_name, "%s%s", poolname, BOGUS_SUFFIX);
4371 	fnvlist_add_string(cfg, ZPOOL_CONFIG_POOL_NAME, bogus_name);
4372 
4373 	error = spa_import(bogus_name, cfg, NULL,
4374 	    ZFS_IMPORT_MISSING_LOG | ZFS_IMPORT_CHECKPOINT |
4375 	    ZFS_IMPORT_SKIP_MMP);
4376 	if (error != 0) {
4377 		fatal("Tried to import pool \"%s\" but spa_import() failed "
4378 		    "with error %d\n", bogus_name, error);
4379 	}
4380 
4381 	if (new_path != NULL && path_start != NULL)
4382 		(void) asprintf(new_path, "%s%s", bogus_name, path_start);
4383 
4384 	if (target != poolname)
4385 		free(poolname);
4386 
4387 	return (bogus_name);
4388 }
4389 
4390 typedef struct verify_checkpoint_sm_entry_cb_arg {
4391 	vdev_t *vcsec_vd;
4392 
4393 	/* the following fields are only used for printing progress */
4394 	uint64_t vcsec_entryid;
4395 	uint64_t vcsec_num_entries;
4396 } verify_checkpoint_sm_entry_cb_arg_t;
4397 
4398 #define	ENTRIES_PER_PROGRESS_UPDATE 10000
4399 
4400 static int
4401 verify_checkpoint_sm_entry_cb(space_map_entry_t *sme, void *arg)
4402 {
4403 	verify_checkpoint_sm_entry_cb_arg_t *vcsec = arg;
4404 	vdev_t *vd = vcsec->vcsec_vd;
4405 	metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift];
4406 	uint64_t end = sme->sme_offset + sme->sme_run;
4407 
4408 	ASSERT(sme->sme_type == SM_FREE);
4409 
4410 	if ((vcsec->vcsec_entryid % ENTRIES_PER_PROGRESS_UPDATE) == 0) {
4411 		(void) fprintf(stderr,
4412 		    "\rverifying vdev %llu, space map entry %llu of %llu ...",
4413 		    (longlong_t)vd->vdev_id,
4414 		    (longlong_t)vcsec->vcsec_entryid,
4415 		    (longlong_t)vcsec->vcsec_num_entries);
4416 	}
4417 	vcsec->vcsec_entryid++;
4418 
4419 	/*
4420 	 * See comment in checkpoint_sm_exclude_entry_cb()
4421 	 */
4422 	VERIFY3U(sme->sme_offset, >=, ms->ms_start);
4423 	VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
4424 
4425 	/*
4426 	 * The entries in the vdev_checkpoint_sm should be marked as
4427 	 * allocated in the checkpointed state of the pool, therefore
4428 	 * their respective ms_allocateable trees should not contain them.
4429 	 */
4430 	mutex_enter(&ms->ms_lock);
4431 	range_tree_verify_not_present(ms->ms_allocatable,
4432 	    sme->sme_offset, sme->sme_run);
4433 	mutex_exit(&ms->ms_lock);
4434 
4435 	return (0);
4436 }
4437 
4438 /*
4439  * Verify that all segments in the vdev_checkpoint_sm are allocated
4440  * according to the checkpoint's ms_sm (i.e. are not in the checkpoint's
4441  * ms_allocatable).
4442  *
4443  * Do so by comparing the checkpoint space maps (vdev_checkpoint_sm) of
4444  * each vdev in the current state of the pool to the metaslab space maps
4445  * (ms_sm) of the checkpointed state of the pool.
4446  *
4447  * Note that the function changes the state of the ms_allocatable
4448  * trees of the current spa_t. The entries of these ms_allocatable
4449  * trees are cleared out and then repopulated from with the free
4450  * entries of their respective ms_sm space maps.
4451  */
4452 static void
4453 verify_checkpoint_vdev_spacemaps(spa_t *checkpoint, spa_t *current)
4454 {
4455 	vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
4456 	vdev_t *current_rvd = current->spa_root_vdev;
4457 
4458 	load_concrete_ms_allocatable_trees(checkpoint, SM_FREE);
4459 
4460 	for (uint64_t c = 0; c < ckpoint_rvd->vdev_children; c++) {
4461 		vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[c];
4462 		vdev_t *current_vd = current_rvd->vdev_child[c];
4463 
4464 		space_map_t *checkpoint_sm = NULL;
4465 		uint64_t checkpoint_sm_obj;
4466 
4467 		if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
4468 			/*
4469 			 * Since we don't allow device removal in a pool
4470 			 * that has a checkpoint, we expect that all removed
4471 			 * vdevs were removed from the pool before the
4472 			 * checkpoint.
4473 			 */
4474 			ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
4475 			continue;
4476 		}
4477 
4478 		/*
4479 		 * If the checkpoint space map doesn't exist, then nothing
4480 		 * here is checkpointed so there's nothing to verify.
4481 		 */
4482 		if (current_vd->vdev_top_zap == 0 ||
4483 		    zap_contains(spa_meta_objset(current),
4484 		    current_vd->vdev_top_zap,
4485 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
4486 			continue;
4487 
4488 		VERIFY0(zap_lookup(spa_meta_objset(current),
4489 		    current_vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
4490 		    sizeof (uint64_t), 1, &checkpoint_sm_obj));
4491 
4492 		VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(current),
4493 		    checkpoint_sm_obj, 0, current_vd->vdev_asize,
4494 		    current_vd->vdev_ashift));
4495 
4496 		verify_checkpoint_sm_entry_cb_arg_t vcsec;
4497 		vcsec.vcsec_vd = ckpoint_vd;
4498 		vcsec.vcsec_entryid = 0;
4499 		vcsec.vcsec_num_entries =
4500 		    space_map_length(checkpoint_sm) / sizeof (uint64_t);
4501 		VERIFY0(space_map_iterate(checkpoint_sm,
4502 		    space_map_length(checkpoint_sm),
4503 		    verify_checkpoint_sm_entry_cb, &vcsec));
4504 		dump_spacemap(current->spa_meta_objset, checkpoint_sm);
4505 		space_map_close(checkpoint_sm);
4506 	}
4507 
4508 	/*
4509 	 * If we've added vdevs since we took the checkpoint, ensure
4510 	 * that their checkpoint space maps are empty.
4511 	 */
4512 	if (ckpoint_rvd->vdev_children < current_rvd->vdev_children) {
4513 		for (uint64_t c = ckpoint_rvd->vdev_children;
4514 		    c < current_rvd->vdev_children; c++) {
4515 			vdev_t *current_vd = current_rvd->vdev_child[c];
4516 			ASSERT3P(current_vd->vdev_checkpoint_sm, ==, NULL);
4517 		}
4518 	}
4519 
4520 	/* for cleaner progress output */
4521 	(void) fprintf(stderr, "\n");
4522 }
4523 
4524 /*
4525  * Verifies that all space that's allocated in the checkpoint is
4526  * still allocated in the current version, by checking that everything
4527  * in checkpoint's ms_allocatable (which is actually allocated, not
4528  * allocatable/free) is not present in current's ms_allocatable.
4529  *
4530  * Note that the function changes the state of the ms_allocatable
4531  * trees of both spas when called. The entries of all ms_allocatable
4532  * trees are cleared out and then repopulated from their respective
4533  * ms_sm space maps. In the checkpointed state we load the allocated
4534  * entries, and in the current state we load the free entries.
4535  */
4536 static void
4537 verify_checkpoint_ms_spacemaps(spa_t *checkpoint, spa_t *current)
4538 {
4539 	vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
4540 	vdev_t *current_rvd = current->spa_root_vdev;
4541 
4542 	load_concrete_ms_allocatable_trees(checkpoint, SM_ALLOC);
4543 	load_concrete_ms_allocatable_trees(current, SM_FREE);
4544 
4545 	for (uint64_t i = 0; i < ckpoint_rvd->vdev_children; i++) {
4546 		vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[i];
4547 		vdev_t *current_vd = current_rvd->vdev_child[i];
4548 
4549 		if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
4550 			/*
4551 			 * See comment in verify_checkpoint_vdev_spacemaps()
4552 			 */
4553 			ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
4554 			continue;
4555 		}
4556 
4557 		for (uint64_t m = 0; m < ckpoint_vd->vdev_ms_count; m++) {
4558 			metaslab_t *ckpoint_msp = ckpoint_vd->vdev_ms[m];
4559 			metaslab_t *current_msp = current_vd->vdev_ms[m];
4560 
4561 			(void) fprintf(stderr,
4562 			    "\rverifying vdev %llu of %llu, "
4563 			    "metaslab %llu of %llu ...",
4564 			    (longlong_t)current_vd->vdev_id,
4565 			    (longlong_t)current_rvd->vdev_children,
4566 			    (longlong_t)current_vd->vdev_ms[m]->ms_id,
4567 			    (longlong_t)current_vd->vdev_ms_count);
4568 
4569 			/*
4570 			 * We walk through the ms_allocatable trees that
4571 			 * are loaded with the allocated blocks from the
4572 			 * ms_sm spacemaps of the checkpoint. For each
4573 			 * one of these ranges we ensure that none of them
4574 			 * exists in the ms_allocatable trees of the
4575 			 * current state which are loaded with the ranges
4576 			 * that are currently free.
4577 			 *
4578 			 * This way we ensure that none of the blocks that
4579 			 * are part of the checkpoint were freed by mistake.
4580 			 */
4581 			range_tree_walk(ckpoint_msp->ms_allocatable,
4582 			    (range_tree_func_t *)range_tree_verify_not_present,
4583 			    current_msp->ms_allocatable);
4584 		}
4585 	}
4586 
4587 	/* for cleaner progress output */
4588 	(void) fprintf(stderr, "\n");
4589 }
4590 
4591 static void
4592 verify_checkpoint_blocks(spa_t *spa)
4593 {
4594 	ASSERT(!dump_opt['L']);
4595 
4596 	spa_t *checkpoint_spa;
4597 	char *checkpoint_pool;
4598 	nvlist_t *config = NULL;
4599 	int error = 0;
4600 
4601 	/*
4602 	 * We import the checkpointed state of the pool (under a different
4603 	 * name) so we can do verification on it against the current state
4604 	 * of the pool.
4605 	 */
4606 	checkpoint_pool = import_checkpointed_state(spa->spa_name, config,
4607 	    NULL);
4608 	ASSERT(strcmp(spa->spa_name, checkpoint_pool) != 0);
4609 
4610 	error = spa_open(checkpoint_pool, &checkpoint_spa, FTAG);
4611 	if (error != 0) {
4612 		fatal("Tried to open pool \"%s\" but spa_open() failed with "
4613 		    "error %d\n", checkpoint_pool, error);
4614 	}
4615 
4616 	/*
4617 	 * Ensure that ranges in the checkpoint space maps of each vdev
4618 	 * are allocated according to the checkpointed state's metaslab
4619 	 * space maps.
4620 	 */
4621 	verify_checkpoint_vdev_spacemaps(checkpoint_spa, spa);
4622 
4623 	/*
4624 	 * Ensure that allocated ranges in the checkpoint's metaslab
4625 	 * space maps remain allocated in the metaslab space maps of
4626 	 * the current state.
4627 	 */
4628 	verify_checkpoint_ms_spacemaps(checkpoint_spa, spa);
4629 
4630 	/*
4631 	 * Once we are done, we get rid of the checkpointed state.
4632 	 */
4633 	spa_close(checkpoint_spa, FTAG);
4634 	free(checkpoint_pool);
4635 }
4636 
4637 static void
4638 dump_leftover_checkpoint_blocks(spa_t *spa)
4639 {
4640 	vdev_t *rvd = spa->spa_root_vdev;
4641 
4642 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
4643 		vdev_t *vd = rvd->vdev_child[i];
4644 
4645 		space_map_t *checkpoint_sm = NULL;
4646 		uint64_t checkpoint_sm_obj;
4647 
4648 		if (vd->vdev_top_zap == 0)
4649 			continue;
4650 
4651 		if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
4652 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
4653 			continue;
4654 
4655 		VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
4656 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
4657 		    sizeof (uint64_t), 1, &checkpoint_sm_obj));
4658 
4659 		VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
4660 		    checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
4661 		dump_spacemap(spa->spa_meta_objset, checkpoint_sm);
4662 		space_map_close(checkpoint_sm);
4663 	}
4664 }
4665 
4666 static int
4667 verify_checkpoint(spa_t *spa)
4668 {
4669 	uberblock_t checkpoint;
4670 	int error;
4671 
4672 	if (!spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT))
4673 		return (0);
4674 
4675 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
4676 	    DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t),
4677 	    sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint);
4678 
4679 	if (error == ENOENT && !dump_opt['L']) {
4680 		/*
4681 		 * If the feature is active but the uberblock is missing
4682 		 * then we must be in the middle of discarding the
4683 		 * checkpoint.
4684 		 */
4685 		(void) printf("\nPartially discarded checkpoint "
4686 		    "state found:\n");
4687 		dump_leftover_checkpoint_blocks(spa);
4688 		return (0);
4689 	} else if (error != 0) {
4690 		(void) printf("lookup error %d when looking for "
4691 		    "checkpointed uberblock in MOS\n", error);
4692 		return (error);
4693 	}
4694 	dump_uberblock(&checkpoint, "\nCheckpointed uberblock found:\n", "\n");
4695 
4696 	if (checkpoint.ub_checkpoint_txg == 0) {
4697 		(void) printf("\nub_checkpoint_txg not set in checkpointed "
4698 		    "uberblock\n");
4699 		error = 3;
4700 	}
4701 
4702 	if (error == 0 && !dump_opt['L'])
4703 		verify_checkpoint_blocks(spa);
4704 
4705 	return (error);
4706 }
4707 
4708 /* ARGSUSED */
4709 static void
4710 mos_leaks_cb(void *arg, uint64_t start, uint64_t size)
4711 {
4712 	for (uint64_t i = start; i < size; i++) {
4713 		(void) printf("MOS object %llu referenced but not allocated\n",
4714 		    (u_longlong_t)i);
4715 	}
4716 }
4717 
4718 static range_tree_t *mos_refd_objs;
4719 
4720 static void
4721 mos_obj_refd(uint64_t obj)
4722 {
4723 	if (obj != 0 && mos_refd_objs != NULL)
4724 		range_tree_add(mos_refd_objs, obj, 1);
4725 }
4726 
4727 static void
4728 mos_leak_vdev(vdev_t *vd)
4729 {
4730 	mos_obj_refd(vd->vdev_dtl_object);
4731 	mos_obj_refd(vd->vdev_ms_array);
4732 	mos_obj_refd(vd->vdev_top_zap);
4733 	mos_obj_refd(vd->vdev_indirect_config.vic_births_object);
4734 	mos_obj_refd(vd->vdev_indirect_config.vic_mapping_object);
4735 	mos_obj_refd(vd->vdev_leaf_zap);
4736 	if (vd->vdev_checkpoint_sm != NULL)
4737 		mos_obj_refd(vd->vdev_checkpoint_sm->sm_object);
4738 	if (vd->vdev_indirect_mapping != NULL) {
4739 		mos_obj_refd(vd->vdev_indirect_mapping->
4740 		    vim_phys->vimp_counts_object);
4741 	}
4742 	if (vd->vdev_obsolete_sm != NULL)
4743 		mos_obj_refd(vd->vdev_obsolete_sm->sm_object);
4744 
4745 	for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
4746 		metaslab_t *ms = vd->vdev_ms[m];
4747 		mos_obj_refd(space_map_object(ms->ms_sm));
4748 	}
4749 
4750 	for (uint64_t c = 0; c < vd->vdev_children; c++) {
4751 		mos_leak_vdev(vd->vdev_child[c]);
4752 	}
4753 }
4754 
4755 static int
4756 dump_mos_leaks(spa_t *spa)
4757 {
4758 	int rv = 0;
4759 	objset_t *mos = spa->spa_meta_objset;
4760 	dsl_pool_t *dp = spa->spa_dsl_pool;
4761 
4762 	/* Visit and mark all referenced objects in the MOS */
4763 
4764 	mos_obj_refd(DMU_POOL_DIRECTORY_OBJECT);
4765 	mos_obj_refd(spa->spa_pool_props_object);
4766 	mos_obj_refd(spa->spa_config_object);
4767 	mos_obj_refd(spa->spa_ddt_stat_object);
4768 	mos_obj_refd(spa->spa_feat_desc_obj);
4769 	mos_obj_refd(spa->spa_feat_enabled_txg_obj);
4770 	mos_obj_refd(spa->spa_feat_for_read_obj);
4771 	mos_obj_refd(spa->spa_feat_for_write_obj);
4772 	mos_obj_refd(spa->spa_history);
4773 	mos_obj_refd(spa->spa_errlog_last);
4774 	mos_obj_refd(spa->spa_errlog_scrub);
4775 	mos_obj_refd(spa->spa_all_vdev_zaps);
4776 	mos_obj_refd(spa->spa_dsl_pool->dp_bptree_obj);
4777 	mos_obj_refd(spa->spa_dsl_pool->dp_tmp_userrefs_obj);
4778 	mos_obj_refd(spa->spa_dsl_pool->dp_scan->scn_phys.scn_queue_obj);
4779 	bpobj_count_refd(&spa->spa_deferred_bpobj);
4780 	mos_obj_refd(dp->dp_empty_bpobj);
4781 	bpobj_count_refd(&dp->dp_obsolete_bpobj);
4782 	bpobj_count_refd(&dp->dp_free_bpobj);
4783 	mos_obj_refd(spa->spa_l2cache.sav_object);
4784 	mos_obj_refd(spa->spa_spares.sav_object);
4785 
4786 	mos_obj_refd(spa->spa_condensing_indirect_phys.
4787 	    scip_next_mapping_object);
4788 	mos_obj_refd(spa->spa_condensing_indirect_phys.
4789 	    scip_prev_obsolete_sm_object);
4790 	if (spa->spa_condensing_indirect_phys.scip_next_mapping_object != 0) {
4791 		vdev_indirect_mapping_t *vim =
4792 		    vdev_indirect_mapping_open(mos,
4793 		    spa->spa_condensing_indirect_phys.scip_next_mapping_object);
4794 		mos_obj_refd(vim->vim_phys->vimp_counts_object);
4795 		vdev_indirect_mapping_close(vim);
4796 	}
4797 
4798 	if (dp->dp_origin_snap != NULL) {
4799 		dsl_dataset_t *ds;
4800 
4801 		dsl_pool_config_enter(dp, FTAG);
4802 		VERIFY0(dsl_dataset_hold_obj(dp,
4803 		    dsl_dataset_phys(dp->dp_origin_snap)->ds_next_snap_obj,
4804 		    FTAG, &ds));
4805 		count_ds_mos_objects(ds);
4806 		dump_deadlist(&ds->ds_deadlist);
4807 		dsl_dataset_rele(ds, FTAG);
4808 		dsl_pool_config_exit(dp, FTAG);
4809 
4810 		count_ds_mos_objects(dp->dp_origin_snap);
4811 		dump_deadlist(&dp->dp_origin_snap->ds_deadlist);
4812 	}
4813 	count_dir_mos_objects(dp->dp_mos_dir);
4814 	if (dp->dp_free_dir != NULL)
4815 		count_dir_mos_objects(dp->dp_free_dir);
4816 	if (dp->dp_leak_dir != NULL)
4817 		count_dir_mos_objects(dp->dp_leak_dir);
4818 
4819 	mos_leak_vdev(spa->spa_root_vdev);
4820 
4821 	for (uint64_t class = 0; class < DDT_CLASSES; class++) {
4822 		for (uint64_t type = 0; type < DDT_TYPES; type++) {
4823 			for (uint64_t cksum = 0;
4824 			    cksum < ZIO_CHECKSUM_FUNCTIONS; cksum++) {
4825 				ddt_t *ddt = spa->spa_ddt[cksum];
4826 				mos_obj_refd(ddt->ddt_object[type][class]);
4827 			}
4828 		}
4829 	}
4830 
4831 	/*
4832 	 * Visit all allocated objects and make sure they are referenced.
4833 	 */
4834 	uint64_t object = 0;
4835 	while (dmu_object_next(mos, &object, B_FALSE, 0) == 0) {
4836 		if (range_tree_contains(mos_refd_objs, object, 1)) {
4837 			range_tree_remove(mos_refd_objs, object, 1);
4838 		} else {
4839 			dmu_object_info_t doi;
4840 			const char *name;
4841 			dmu_object_info(mos, object, &doi);
4842 			if (doi.doi_type & DMU_OT_NEWTYPE) {
4843 				dmu_object_byteswap_t bswap =
4844 				    DMU_OT_BYTESWAP(doi.doi_type);
4845 				name = dmu_ot_byteswap[bswap].ob_name;
4846 			} else {
4847 				name = dmu_ot[doi.doi_type].ot_name;
4848 			}
4849 
4850 			(void) printf("MOS object %llu (%s) leaked\n",
4851 			    (u_longlong_t)object, name);
4852 			rv = 2;
4853 		}
4854 	}
4855 	(void) range_tree_walk(mos_refd_objs, mos_leaks_cb, NULL);
4856 	if (!range_tree_is_empty(mos_refd_objs))
4857 		rv = 2;
4858 	range_tree_vacate(mos_refd_objs, NULL, NULL);
4859 	range_tree_destroy(mos_refd_objs);
4860 	return (rv);
4861 }
4862 
4863 static void
4864 dump_zpool(spa_t *spa)
4865 {
4866 	dsl_pool_t *dp = spa_get_dsl(spa);
4867 	int rc = 0;
4868 
4869 	if (dump_opt['S']) {
4870 		dump_simulated_ddt(spa);
4871 		return;
4872 	}
4873 
4874 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
4875 		(void) printf("\nCached configuration:\n");
4876 		dump_nvlist(spa->spa_config, 8);
4877 	}
4878 
4879 	if (dump_opt['C'])
4880 		dump_config(spa);
4881 
4882 	if (dump_opt['u'])
4883 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
4884 
4885 	if (dump_opt['D'])
4886 		dump_all_ddts(spa);
4887 
4888 	if (dump_opt['d'] > 2 || dump_opt['m'])
4889 		dump_metaslabs(spa);
4890 	if (dump_opt['M'])
4891 		dump_metaslab_groups(spa);
4892 
4893 	if (dump_opt['d'] || dump_opt['i']) {
4894 		mos_refd_objs = range_tree_create(NULL, NULL);
4895 		dump_dir(dp->dp_meta_objset);
4896 
4897 		if (dump_opt['d'] >= 3) {
4898 			dsl_pool_t *dp = spa->spa_dsl_pool;
4899 			dump_full_bpobj(&spa->spa_deferred_bpobj,
4900 			    "Deferred frees", 0);
4901 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
4902 				dump_full_bpobj(&dp->dp_free_bpobj,
4903 				    "Pool snapshot frees", 0);
4904 			}
4905 			if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
4906 				ASSERT(spa_feature_is_enabled(spa,
4907 				    SPA_FEATURE_DEVICE_REMOVAL));
4908 				dump_full_bpobj(&dp->dp_obsolete_bpobj,
4909 				    "Pool obsolete blocks", 0);
4910 			}
4911 
4912 			if (spa_feature_is_active(spa,
4913 			    SPA_FEATURE_ASYNC_DESTROY)) {
4914 				dump_bptree(spa->spa_meta_objset,
4915 				    dp->dp_bptree_obj,
4916 				    "Pool dataset frees");
4917 			}
4918 			dump_dtl(spa->spa_root_vdev, 0);
4919 		}
4920 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
4921 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
4922 
4923 		if (rc == 0 && !dump_opt['L'])
4924 			rc = dump_mos_leaks(spa);
4925 
4926 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
4927 			uint64_t refcount;
4928 
4929 			if (!(spa_feature_table[f].fi_flags &
4930 			    ZFEATURE_FLAG_PER_DATASET) ||
4931 			    !spa_feature_is_enabled(spa, f)) {
4932 				ASSERT0(dataset_feature_count[f]);
4933 				continue;
4934 			}
4935 			(void) feature_get_refcount(spa,
4936 			    &spa_feature_table[f], &refcount);
4937 			if (dataset_feature_count[f] != refcount) {
4938 				(void) printf("%s feature refcount mismatch: "
4939 				    "%lld datasets != %lld refcount\n",
4940 				    spa_feature_table[f].fi_uname,
4941 				    (longlong_t)dataset_feature_count[f],
4942 				    (longlong_t)refcount);
4943 				rc = 2;
4944 			} else {
4945 				(void) printf("Verified %s feature refcount "
4946 				    "of %llu is correct\n",
4947 				    spa_feature_table[f].fi_uname,
4948 				    (longlong_t)refcount);
4949 			}
4950 		}
4951 
4952 		if (rc == 0) {
4953 			rc = verify_device_removal_feature_counts(spa);
4954 		}
4955 	}
4956 
4957 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
4958 		rc = dump_block_stats(spa);
4959 
4960 	if (rc == 0)
4961 		rc = verify_spacemap_refcounts(spa);
4962 
4963 	if (dump_opt['s'])
4964 		show_pool_stats(spa);
4965 
4966 	if (dump_opt['h'])
4967 		dump_history(spa);
4968 
4969 	if (rc == 0)
4970 		rc = verify_checkpoint(spa);
4971 
4972 	if (rc != 0) {
4973 		dump_debug_buffer();
4974 		exit(rc);
4975 	}
4976 }
4977 
4978 #define	ZDB_FLAG_CHECKSUM	0x0001
4979 #define	ZDB_FLAG_DECOMPRESS	0x0002
4980 #define	ZDB_FLAG_BSWAP		0x0004
4981 #define	ZDB_FLAG_GBH		0x0008
4982 #define	ZDB_FLAG_INDIRECT	0x0010
4983 #define	ZDB_FLAG_PHYS		0x0020
4984 #define	ZDB_FLAG_RAW		0x0040
4985 #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
4986 
4987 static int flagbits[256];
4988 
4989 static void
4990 zdb_print_blkptr(blkptr_t *bp, int flags)
4991 {
4992 	char blkbuf[BP_SPRINTF_LEN];
4993 
4994 	if (flags & ZDB_FLAG_BSWAP)
4995 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
4996 
4997 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
4998 	(void) printf("%s\n", blkbuf);
4999 }
5000 
5001 static void
5002 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
5003 {
5004 	int i;
5005 
5006 	for (i = 0; i < nbps; i++)
5007 		zdb_print_blkptr(&bp[i], flags);
5008 }
5009 
5010 static void
5011 zdb_dump_gbh(void *buf, int flags)
5012 {
5013 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
5014 }
5015 
5016 static void
5017 zdb_dump_block_raw(void *buf, uint64_t size, int flags)
5018 {
5019 	if (flags & ZDB_FLAG_BSWAP)
5020 		byteswap_uint64_array(buf, size);
5021 	(void) write(1, buf, size);
5022 }
5023 
5024 static void
5025 zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
5026 {
5027 	uint64_t *d = (uint64_t *)buf;
5028 	unsigned nwords = size / sizeof (uint64_t);
5029 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
5030 	unsigned i, j;
5031 	const char *hdr;
5032 	char *c;
5033 
5034 
5035 	if (do_bswap)
5036 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
5037 	else
5038 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
5039 
5040 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
5041 
5042 	for (i = 0; i < nwords; i += 2) {
5043 		(void) printf("%06llx:  %016llx  %016llx  ",
5044 		    (u_longlong_t)(i * sizeof (uint64_t)),
5045 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
5046 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
5047 
5048 		c = (char *)&d[i];
5049 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
5050 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
5051 		(void) printf("\n");
5052 	}
5053 }
5054 
5055 /*
5056  * There are two acceptable formats:
5057  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
5058  *	child[.child]*    - For example: 0.1.1
5059  *
5060  * The second form can be used to specify arbitrary vdevs anywhere
5061  * in the heirarchy.  For example, in a pool with a mirror of
5062  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
5063  */
5064 static vdev_t *
5065 zdb_vdev_lookup(vdev_t *vdev, const char *path)
5066 {
5067 	char *s, *p, *q;
5068 	unsigned i;
5069 
5070 	if (vdev == NULL)
5071 		return (NULL);
5072 
5073 	/* First, assume the x.x.x.x format */
5074 	i = strtoul(path, &s, 10);
5075 	if (s == path || (s && *s != '.' && *s != '\0'))
5076 		goto name;
5077 	if (i >= vdev->vdev_children)
5078 		return (NULL);
5079 
5080 	vdev = vdev->vdev_child[i];
5081 	if (*s == '\0')
5082 		return (vdev);
5083 	return (zdb_vdev_lookup(vdev, s+1));
5084 
5085 name:
5086 	for (i = 0; i < vdev->vdev_children; i++) {
5087 		vdev_t *vc = vdev->vdev_child[i];
5088 
5089 		if (vc->vdev_path == NULL) {
5090 			vc = zdb_vdev_lookup(vc, path);
5091 			if (vc == NULL)
5092 				continue;
5093 			else
5094 				return (vc);
5095 		}
5096 
5097 		p = strrchr(vc->vdev_path, '/');
5098 		p = p ? p + 1 : vc->vdev_path;
5099 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
5100 
5101 		if (strcmp(vc->vdev_path, path) == 0)
5102 			return (vc);
5103 		if (strcmp(p, path) == 0)
5104 			return (vc);
5105 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
5106 			return (vc);
5107 	}
5108 
5109 	return (NULL);
5110 }
5111 
5112 /* ARGSUSED */
5113 static int
5114 random_get_pseudo_bytes_cb(void *buf, size_t len, void *unused)
5115 {
5116 	return (random_get_pseudo_bytes(buf, len));
5117 }
5118 
5119 /*
5120  * Read a block from a pool and print it out.  The syntax of the
5121  * block descriptor is:
5122  *
5123  *	pool:vdev_specifier:offset:size[:flags]
5124  *
5125  *	pool           - The name of the pool you wish to read from
5126  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
5127  *	offset         - offset, in hex, in bytes
5128  *	size           - Amount of data to read, in hex, in bytes
5129  *	flags          - A string of characters specifying options
5130  *		 b: Decode a blkptr at given offset within block
5131  *		*c: Calculate and display checksums
5132  *		 d: Decompress data before dumping
5133  *		 e: Byteswap data before dumping
5134  *		 g: Display data as a gang block header
5135  *		 i: Display as an indirect block
5136  *		 p: Do I/O to physical offset
5137  *		 r: Dump raw data to stdout
5138  *
5139  *              * = not yet implemented
5140  */
5141 static void
5142 zdb_read_block(char *thing, spa_t *spa)
5143 {
5144 	blkptr_t blk, *bp = &blk;
5145 	dva_t *dva = bp->blk_dva;
5146 	int flags = 0;
5147 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
5148 	zio_t *zio;
5149 	vdev_t *vd;
5150 	abd_t *pabd;
5151 	void *lbuf, *buf;
5152 	const char *s, *vdev;
5153 	char *p, *dup, *flagstr;
5154 	int i, error;
5155 
5156 	dup = strdup(thing);
5157 	s = strtok(dup, ":");
5158 	vdev = s ? s : "";
5159 	s = strtok(NULL, ":");
5160 	offset = strtoull(s ? s : "", NULL, 16);
5161 	s = strtok(NULL, ":");
5162 	size = strtoull(s ? s : "", NULL, 16);
5163 	s = strtok(NULL, ":");
5164 	if (s)
5165 		flagstr = strdup(s);
5166 	else
5167 		flagstr = strdup("");
5168 
5169 	s = NULL;
5170 	if (size == 0)
5171 		s = "size must not be zero";
5172 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
5173 		s = "size must be a multiple of sector size";
5174 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
5175 		s = "offset must be a multiple of sector size";
5176 	if (s) {
5177 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
5178 		free(dup);
5179 		return;
5180 	}
5181 
5182 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
5183 		for (i = 0; flagstr[i]; i++) {
5184 			int bit = flagbits[(uchar_t)flagstr[i]];
5185 
5186 			if (bit == 0) {
5187 				(void) printf("***Invalid flag: %c\n",
5188 				    flagstr[i]);
5189 				continue;
5190 			}
5191 			flags |= bit;
5192 
5193 			/* If it's not something with an argument, keep going */
5194 			if ((bit & (ZDB_FLAG_CHECKSUM |
5195 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
5196 				continue;
5197 
5198 			p = &flagstr[i + 1];
5199 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
5200 				blkptr_offset = strtoull(p, &p, 16);
5201 			if (*p != ':' && *p != '\0') {
5202 				(void) printf("***Invalid flag arg: '%s'\n", s);
5203 				free(dup);
5204 				return;
5205 			}
5206 		}
5207 	}
5208 	free(flagstr);
5209 
5210 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
5211 	if (vd == NULL) {
5212 		(void) printf("***Invalid vdev: %s\n", vdev);
5213 		free(dup);
5214 		return;
5215 	} else {
5216 		if (vd->vdev_path)
5217 			(void) fprintf(stderr, "Found vdev: %s\n",
5218 			    vd->vdev_path);
5219 		else
5220 			(void) fprintf(stderr, "Found vdev type: %s\n",
5221 			    vd->vdev_ops->vdev_op_type);
5222 	}
5223 
5224 	psize = size;
5225 	lsize = size;
5226 
5227 	pabd = abd_alloc_linear(SPA_MAXBLOCKSIZE, B_FALSE);
5228 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
5229 
5230 	BP_ZERO(bp);
5231 
5232 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
5233 	DVA_SET_OFFSET(&dva[0], offset);
5234 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
5235 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
5236 
5237 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
5238 
5239 	BP_SET_LSIZE(bp, lsize);
5240 	BP_SET_PSIZE(bp, psize);
5241 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
5242 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
5243 	BP_SET_TYPE(bp, DMU_OT_NONE);
5244 	BP_SET_LEVEL(bp, 0);
5245 	BP_SET_DEDUP(bp, 0);
5246 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
5247 
5248 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5249 	zio = zio_root(spa, NULL, NULL, 0);
5250 
5251 	if (vd == vd->vdev_top) {
5252 		/*
5253 		 * Treat this as a normal block read.
5254 		 */
5255 		zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL,
5256 		    ZIO_PRIORITY_SYNC_READ,
5257 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
5258 	} else {
5259 		/*
5260 		 * Treat this as a vdev child I/O.
5261 		 */
5262 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd,
5263 		    psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
5264 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
5265 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
5266 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW | ZIO_FLAG_OPTIONAL,
5267 		    NULL, NULL));
5268 	}
5269 
5270 	error = zio_wait(zio);
5271 	spa_config_exit(spa, SCL_STATE, FTAG);
5272 
5273 	if (error) {
5274 		(void) printf("Read of %s failed, error: %d\n", thing, error);
5275 		goto out;
5276 	}
5277 
5278 	if (flags & ZDB_FLAG_DECOMPRESS) {
5279 		/*
5280 		 * We don't know how the data was compressed, so just try
5281 		 * every decompress function at every inflated blocksize.
5282 		 */
5283 		enum zio_compress c;
5284 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
5285 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
5286 
5287 		abd_copy_to_buf(pbuf2, pabd, psize);
5288 
5289 		VERIFY0(abd_iterate_func(pabd, psize, SPA_MAXBLOCKSIZE - psize,
5290 		    random_get_pseudo_bytes_cb, NULL));
5291 
5292 		VERIFY0(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
5293 		    SPA_MAXBLOCKSIZE - psize));
5294 
5295 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
5296 		    lsize -= SPA_MINBLOCKSIZE) {
5297 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
5298 				if (zio_decompress_data(c, pabd,
5299 				    lbuf, psize, lsize) == 0 &&
5300 				    zio_decompress_data_buf(c, pbuf2,
5301 				    lbuf2, psize, lsize) == 0 &&
5302 				    bcmp(lbuf, lbuf2, lsize) == 0)
5303 					break;
5304 			}
5305 			if (c != ZIO_COMPRESS_FUNCTIONS)
5306 				break;
5307 			lsize -= SPA_MINBLOCKSIZE;
5308 		}
5309 
5310 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
5311 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
5312 
5313 		if (lsize <= psize) {
5314 			(void) printf("Decompress of %s failed\n", thing);
5315 			goto out;
5316 		}
5317 		buf = lbuf;
5318 		size = lsize;
5319 	} else {
5320 		buf = abd_to_buf(pabd);
5321 		size = psize;
5322 	}
5323 
5324 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
5325 		zdb_print_blkptr((blkptr_t *)(void *)
5326 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
5327 	else if (flags & ZDB_FLAG_RAW)
5328 		zdb_dump_block_raw(buf, size, flags);
5329 	else if (flags & ZDB_FLAG_INDIRECT)
5330 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
5331 		    flags);
5332 	else if (flags & ZDB_FLAG_GBH)
5333 		zdb_dump_gbh(buf, flags);
5334 	else
5335 		zdb_dump_block(thing, buf, size, flags);
5336 
5337 out:
5338 	abd_free(pabd);
5339 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
5340 	free(dup);
5341 }
5342 
5343 static void
5344 zdb_embedded_block(char *thing)
5345 {
5346 	blkptr_t bp;
5347 	unsigned long long *words = (void *)&bp;
5348 	char *buf;
5349 	int err;
5350 
5351 	bzero(&bp, sizeof (bp));
5352 	err = sscanf(thing, "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx:"
5353 	    "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx",
5354 	    words + 0, words + 1, words + 2, words + 3,
5355 	    words + 4, words + 5, words + 6, words + 7,
5356 	    words + 8, words + 9, words + 10, words + 11,
5357 	    words + 12, words + 13, words + 14, words + 15);
5358 	if (err != 16) {
5359 		(void) fprintf(stderr, "invalid input format\n");
5360 		exit(1);
5361 	}
5362 	ASSERT3U(BPE_GET_LSIZE(&bp), <=, SPA_MAXBLOCKSIZE);
5363 	buf = malloc(SPA_MAXBLOCKSIZE);
5364 	if (buf == NULL) {
5365 		(void) fprintf(stderr, "out of memory\n");
5366 		exit(1);
5367 	}
5368 	err = decode_embedded_bp(&bp, buf, BPE_GET_LSIZE(&bp));
5369 	if (err != 0) {
5370 		(void) fprintf(stderr, "decode failed: %u\n", err);
5371 		exit(1);
5372 	}
5373 	zdb_dump_block_raw(buf, BPE_GET_LSIZE(&bp), 0);
5374 	free(buf);
5375 }
5376 
5377 int
5378 main(int argc, char **argv)
5379 {
5380 	int c;
5381 	struct rlimit rl = { 1024, 1024 };
5382 	spa_t *spa = NULL;
5383 	objset_t *os = NULL;
5384 	int dump_all = 1;
5385 	int verbose = 0;
5386 	int error = 0;
5387 	char **searchdirs = NULL;
5388 	int nsearch = 0;
5389 	char *target, *target_pool;
5390 	nvlist_t *policy = NULL;
5391 	uint64_t max_txg = UINT64_MAX;
5392 	int flags = ZFS_IMPORT_MISSING_LOG;
5393 	int rewind = ZPOOL_NEVER_REWIND;
5394 	char *spa_config_path_env;
5395 	boolean_t target_is_spa = B_TRUE;
5396 	nvlist_t *cfg = NULL;
5397 
5398 	(void) setrlimit(RLIMIT_NOFILE, &rl);
5399 	(void) enable_extended_FILE_stdio(-1, -1);
5400 
5401 	dprintf_setup(&argc, argv);
5402 
5403 	/*
5404 	 * If there is an environment variable SPA_CONFIG_PATH it overrides
5405 	 * default spa_config_path setting. If -U flag is specified it will
5406 	 * override this environment variable settings once again.
5407 	 */
5408 	spa_config_path_env = getenv("SPA_CONFIG_PATH");
5409 	if (spa_config_path_env != NULL)
5410 		spa_config_path = spa_config_path_env;
5411 
5412 	while ((c = getopt(argc, argv,
5413 	    "AbcCdDeEFGhiI:klLmMo:Op:PqRsSt:uU:vVx:X")) != -1) {
5414 		switch (c) {
5415 		case 'b':
5416 		case 'c':
5417 		case 'C':
5418 		case 'd':
5419 		case 'D':
5420 		case 'E':
5421 		case 'G':
5422 		case 'h':
5423 		case 'i':
5424 		case 'l':
5425 		case 'm':
5426 		case 'M':
5427 		case 'O':
5428 		case 'R':
5429 		case 's':
5430 		case 'S':
5431 		case 'u':
5432 			dump_opt[c]++;
5433 			dump_all = 0;
5434 			break;
5435 		case 'A':
5436 		case 'e':
5437 		case 'F':
5438 		case 'k':
5439 		case 'L':
5440 		case 'P':
5441 		case 'q':
5442 		case 'X':
5443 			dump_opt[c]++;
5444 			break;
5445 		/* NB: Sort single match options below. */
5446 		case 'I':
5447 			max_inflight = strtoull(optarg, NULL, 0);
5448 			if (max_inflight == 0) {
5449 				(void) fprintf(stderr, "maximum number "
5450 				    "of inflight I/Os must be greater "
5451 				    "than 0\n");
5452 				usage();
5453 			}
5454 			break;
5455 		case 'o':
5456 			error = set_global_var(optarg);
5457 			if (error != 0)
5458 				usage();
5459 			break;
5460 		case 'p':
5461 			if (searchdirs == NULL) {
5462 				searchdirs = umem_alloc(sizeof (char *),
5463 				    UMEM_NOFAIL);
5464 			} else {
5465 				char **tmp = umem_alloc((nsearch + 1) *
5466 				    sizeof (char *), UMEM_NOFAIL);
5467 				bcopy(searchdirs, tmp, nsearch *
5468 				    sizeof (char *));
5469 				umem_free(searchdirs,
5470 				    nsearch * sizeof (char *));
5471 				searchdirs = tmp;
5472 			}
5473 			searchdirs[nsearch++] = optarg;
5474 			break;
5475 		case 't':
5476 			max_txg = strtoull(optarg, NULL, 0);
5477 			if (max_txg < TXG_INITIAL) {
5478 				(void) fprintf(stderr, "incorrect txg "
5479 				    "specified: %s\n", optarg);
5480 				usage();
5481 			}
5482 			break;
5483 		case 'U':
5484 			spa_config_path = optarg;
5485 			if (spa_config_path[0] != '/') {
5486 				(void) fprintf(stderr,
5487 				    "cachefile must be an absolute path "
5488 				    "(i.e. start with a slash)\n");
5489 				usage();
5490 			}
5491 			break;
5492 		case 'v':
5493 			verbose++;
5494 			break;
5495 		case 'V':
5496 			flags = ZFS_IMPORT_VERBATIM;
5497 			break;
5498 		case 'x':
5499 			vn_dumpdir = optarg;
5500 			break;
5501 		default:
5502 			usage();
5503 			break;
5504 		}
5505 	}
5506 
5507 	if (!dump_opt['e'] && searchdirs != NULL) {
5508 		(void) fprintf(stderr, "-p option requires use of -e\n");
5509 		usage();
5510 	}
5511 
5512 	/*
5513 	 * ZDB does not typically re-read blocks; therefore limit the ARC
5514 	 * to 256 MB, which can be used entirely for metadata.
5515 	 */
5516 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
5517 
5518 	/*
5519 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
5520 	 * "zdb -b" uses traversal prefetch which uses async reads.
5521 	 * For good performance, let several of them be active at once.
5522 	 */
5523 	zfs_vdev_async_read_max_active = 10;
5524 
5525 	/*
5526 	 * Disable reference tracking for better performance.
5527 	 */
5528 	reference_tracking_enable = B_FALSE;
5529 
5530 	/*
5531 	 * Do not fail spa_load when spa_load_verify fails. This is needed
5532 	 * to load non-idle pools.
5533 	 */
5534 	spa_load_verify_dryrun = B_TRUE;
5535 
5536 	kernel_init(FREAD);
5537 	g_zfs = libzfs_init();
5538 	ASSERT(g_zfs != NULL);
5539 
5540 	if (dump_all)
5541 		verbose = MAX(verbose, 1);
5542 
5543 	for (c = 0; c < 256; c++) {
5544 		if (dump_all && strchr("AeEFklLOPRSX", c) == NULL)
5545 			dump_opt[c] = 1;
5546 		if (dump_opt[c])
5547 			dump_opt[c] += verbose;
5548 	}
5549 
5550 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
5551 	zfs_recover = (dump_opt['A'] > 1);
5552 
5553 	argc -= optind;
5554 	argv += optind;
5555 
5556 	if (argc < 2 && dump_opt['R'])
5557 		usage();
5558 
5559 	if (dump_opt['E']) {
5560 		if (argc != 1)
5561 			usage();
5562 		zdb_embedded_block(argv[0]);
5563 		return (0);
5564 	}
5565 
5566 	if (argc < 1) {
5567 		if (!dump_opt['e'] && dump_opt['C']) {
5568 			dump_cachefile(spa_config_path);
5569 			return (0);
5570 		}
5571 		usage();
5572 	}
5573 
5574 	if (dump_opt['l'])
5575 		return (dump_label(argv[0]));
5576 
5577 	if (dump_opt['O']) {
5578 		if (argc != 2)
5579 			usage();
5580 		dump_opt['v'] = verbose + 3;
5581 		return (dump_path(argv[0], argv[1]));
5582 	}
5583 
5584 	if (dump_opt['X'] || dump_opt['F'])
5585 		rewind = ZPOOL_DO_REWIND |
5586 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
5587 
5588 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
5589 	    nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, max_txg) != 0 ||
5590 	    nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, rewind) != 0)
5591 		fatal("internal error: %s", strerror(ENOMEM));
5592 
5593 	error = 0;
5594 	target = argv[0];
5595 
5596 	if (strpbrk(target, "/@") != NULL) {
5597 		size_t targetlen;
5598 
5599 		target_pool = strdup(target);
5600 		*strpbrk(target_pool, "/@") = '\0';
5601 
5602 		target_is_spa = B_FALSE;
5603 		targetlen = strlen(target);
5604 		if (targetlen && target[targetlen - 1] == '/')
5605 			target[targetlen - 1] = '\0';
5606 	} else {
5607 		target_pool = target;
5608 	}
5609 
5610 	if (dump_opt['e']) {
5611 		importargs_t args = { 0 };
5612 
5613 		args.paths = nsearch;
5614 		args.path = searchdirs;
5615 		args.can_be_active = B_TRUE;
5616 
5617 		error = zpool_tryimport(g_zfs, target_pool, &cfg, &args);
5618 
5619 		if (error == 0) {
5620 
5621 			if (nvlist_add_nvlist(cfg,
5622 			    ZPOOL_LOAD_POLICY, policy) != 0) {
5623 				fatal("can't open '%s': %s",
5624 				    target, strerror(ENOMEM));
5625 			}
5626 
5627 			if (dump_opt['C'] > 1) {
5628 				(void) printf("\nConfiguration for import:\n");
5629 				dump_nvlist(cfg, 8);
5630 			}
5631 
5632 			/*
5633 			 * Disable the activity check to allow examination of
5634 			 * active pools.
5635 			 */
5636 			error = spa_import(target_pool, cfg, NULL,
5637 			    flags | ZFS_IMPORT_SKIP_MMP);
5638 		}
5639 	}
5640 
5641 	char *checkpoint_pool = NULL;
5642 	char *checkpoint_target = NULL;
5643 	if (dump_opt['k']) {
5644 		checkpoint_pool = import_checkpointed_state(target, cfg,
5645 		    &checkpoint_target);
5646 
5647 		if (checkpoint_target != NULL)
5648 			target = checkpoint_target;
5649 
5650 	}
5651 
5652 	if (error == 0) {
5653 		if (dump_opt['k'] && (target_is_spa || dump_opt['R'])) {
5654 			ASSERT(checkpoint_pool != NULL);
5655 			ASSERT(checkpoint_target == NULL);
5656 
5657 			error = spa_open(checkpoint_pool, &spa, FTAG);
5658 			if (error != 0) {
5659 				fatal("Tried to open pool \"%s\" but "
5660 				    "spa_open() failed with error %d\n",
5661 				    checkpoint_pool, error);
5662 			}
5663 
5664 		} else if (target_is_spa || dump_opt['R']) {
5665 			zdb_set_skip_mmp(target);
5666 			error = spa_open_rewind(target, &spa, FTAG, policy,
5667 			    NULL);
5668 			if (error) {
5669 				/*
5670 				 * If we're missing the log device then
5671 				 * try opening the pool after clearing the
5672 				 * log state.
5673 				 */
5674 				mutex_enter(&spa_namespace_lock);
5675 				if ((spa = spa_lookup(target)) != NULL &&
5676 				    spa->spa_log_state == SPA_LOG_MISSING) {
5677 					spa->spa_log_state = SPA_LOG_CLEAR;
5678 					error = 0;
5679 				}
5680 				mutex_exit(&spa_namespace_lock);
5681 
5682 				if (!error) {
5683 					error = spa_open_rewind(target, &spa,
5684 					    FTAG, policy, NULL);
5685 				}
5686 			}
5687 		} else {
5688 			zdb_set_skip_mmp(target);
5689 			error = open_objset(target, DMU_OST_ANY, FTAG, &os);
5690 		}
5691 	}
5692 	nvlist_free(policy);
5693 
5694 	if (error)
5695 		fatal("can't open '%s': %s", target, strerror(error));
5696 
5697 	argv++;
5698 	argc--;
5699 	if (!dump_opt['R']) {
5700 		if (argc > 0) {
5701 			zopt_objects = argc;
5702 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
5703 			for (unsigned i = 0; i < zopt_objects; i++) {
5704 				errno = 0;
5705 				zopt_object[i] = strtoull(argv[i], NULL, 0);
5706 				if (zopt_object[i] == 0 && errno != 0)
5707 					fatal("bad number %s: %s",
5708 					    argv[i], strerror(errno));
5709 			}
5710 		}
5711 		if (os != NULL) {
5712 			dump_dir(os);
5713 		} else if (zopt_objects > 0 && !dump_opt['m']) {
5714 			dump_dir(spa->spa_meta_objset);
5715 		} else {
5716 			dump_zpool(spa);
5717 		}
5718 	} else {
5719 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
5720 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
5721 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
5722 		flagbits['e'] = ZDB_FLAG_BSWAP;
5723 		flagbits['g'] = ZDB_FLAG_GBH;
5724 		flagbits['i'] = ZDB_FLAG_INDIRECT;
5725 		flagbits['p'] = ZDB_FLAG_PHYS;
5726 		flagbits['r'] = ZDB_FLAG_RAW;
5727 
5728 		for (int i = 0; i < argc; i++)
5729 			zdb_read_block(argv[i], spa);
5730 	}
5731 
5732 	if (dump_opt['k']) {
5733 		free(checkpoint_pool);
5734 		if (!target_is_spa)
5735 			free(checkpoint_target);
5736 	}
5737 
5738 	if (os != NULL)
5739 		close_objset(os, FTAG);
5740 	else
5741 		spa_close(spa, FTAG);
5742 
5743 	fuid_table_destroy();
5744 
5745 	dump_debug_buffer();
5746 
5747 	libzfs_fini(g_zfs);
5748 	kernel_fini();
5749 
5750 	return (error);
5751 }
5752