xref: /illumos-gate/usr/src/cmd/zdb/zdb.c (revision 2d37a1a3)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
238f2529deSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24663207adSDon Brady  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
25c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
26ed61ec1dSYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
27e0f1c0afSOlaf Faaland  * Copyright (c) 2017, 2018 Lawrence Livermore National Security, LLC.
28f06dce2cSAndrew Stormont  * Copyright 2017 RackTop Systems.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <stdio.h>
32490d05b9SMatthew Ahrens #include <unistd.h>
33004388ebScasper #include <stdio_ext.h>
34fa9e4066Sahrens #include <stdlib.h>
3544cd46caSbillm #include <ctype.h>
36fa9e4066Sahrens #include <sys/zfs_context.h>
37fa9e4066Sahrens #include <sys/spa.h>
38fa9e4066Sahrens #include <sys/spa_impl.h>
39fa9e4066Sahrens #include <sys/dmu.h>
40fa9e4066Sahrens #include <sys/zap.h>
41fa9e4066Sahrens #include <sys/fs/zfs.h>
42fa9e4066Sahrens #include <sys/zfs_znode.h>
430a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
440a586ceaSMark Shellenbaum #include <sys/sa.h>
450a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
46fa9e4066Sahrens #include <sys/vdev.h>
47fa9e4066Sahrens #include <sys/vdev_impl.h>
48fa9e4066Sahrens #include <sys/metaslab_impl.h>
49fa9e4066Sahrens #include <sys/dmu_objset.h>
50fa9e4066Sahrens #include <sys/dsl_dir.h>
51fa9e4066Sahrens #include <sys/dsl_dataset.h>
52fa9e4066Sahrens #include <sys/dsl_pool.h>
53fa9e4066Sahrens #include <sys/dbuf.h>
54fa9e4066Sahrens #include <sys/zil.h>
55fa9e4066Sahrens #include <sys/zil_impl.h>
56fa9e4066Sahrens #include <sys/stat.h>
57fa9e4066Sahrens #include <sys/resource.h>
58fa9e4066Sahrens #include <sys/dmu_traverse.h>
59fa9e4066Sahrens #include <sys/zio_checksum.h>
60fa9e4066Sahrens #include <sys/zio_compress.h>
61e0d35c44Smarks #include <sys/zfs_fuid.h>
6288b7b0f2SMatthew Ahrens #include <sys/arc.h>
63b24ab676SJeff Bonwick #include <sys/ddt.h>
64ad135b5dSChristopher Siden #include <sys/zfeature.h>
65770499e1SDan Kimmel #include <sys/abd.h>
664923c69fSMatthew Ahrens #include <sys/blkptr.h>
6717fb938fSMatthew Ahrens #include <sys/dsl_scan.h>
68eb633035STom Caputi #include <sys/dsl_crypt.h>
694445fffbSMatthew Ahrens #include <zfs_comutil.h>
700a055120SJason King #include <libcmdutils.h>
71de6628f0Sck #undef verify
72de6628f0Sck #include <libzfs.h>
73fa9e4066Sahrens 
743f7978d0SAlan Somers #include "zdb.h"
753f7978d0SAlan Somers 
76e690fb27SChristopher Siden #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
77e690fb27SChristopher Siden 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
78e690fb27SChristopher Siden #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
79e690fb27SChristopher Siden 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
80e690fb27SChristopher Siden #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
81e690fb27SChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
82e690fb27SChristopher Siden 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
83e690fb27SChristopher Siden #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
845cabbc6bSPrashanth Sreenivasa 	(idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA ?	\
855cabbc6bSPrashanth Sreenivasa 	DMU_OT_ZAP_OTHER : \
865cabbc6bSPrashanth Sreenivasa 	(idx) == DMU_OTN_UINT64_DATA || (idx) == DMU_OTN_UINT64_METADATA ? \
875cabbc6bSPrashanth Sreenivasa 	DMU_OT_UINT64_OTHER : DMU_OT_NUMTYPES)
886de8f417SVictor Latushkin 
89feef89cfSVictor Latushkin #ifndef lint
904dd77f9eSMatthew Ahrens extern int reference_tracking_enable;
917fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
9206be9802SMatthew Ahrens extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
93f7950bf1SMatthew Ahrens extern int zfs_vdev_async_read_max_active;
94f06dce2cSAndrew Stormont extern int aok;
95e144c4e6SPavel Zakharov extern boolean_t spa_load_verify_dryrun;
96feef89cfSVictor Latushkin #else
974dd77f9eSMatthew Ahrens int reference_tracking_enable;
987fd05ac4SMatthew Ahrens boolean_t zfs_recover;
9906be9802SMatthew Ahrens uint64_t zfs_arc_max, zfs_arc_meta_limit;
100f7950bf1SMatthew Ahrens int zfs_vdev_async_read_max_active;
101f06dce2cSAndrew Stormont int aok;
102e144c4e6SPavel Zakharov boolean_t spa_load_verify_dryrun;
103feef89cfSVictor Latushkin #endif
104feef89cfSVictor Latushkin 
1053f7978d0SAlan Somers static const char cmdname[] = "zdb";
106fa9e4066Sahrens uint8_t dump_opt[256];
107fa9e4066Sahrens 
108fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
109fa9e4066Sahrens 
110fa9e4066Sahrens uint64_t *zopt_object = NULL;
1113f7978d0SAlan Somers static unsigned zopt_objects = 0;
112de6628f0Sck libzfs_handle_t *g_zfs;
11306be9802SMatthew Ahrens uint64_t max_inflight = 1000;
11420b5dafbSPaul Dagnelie static int leaked_objects = 0;
115fa9e4066Sahrens 
116732885fcSMatthew Ahrens static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
11717fb938fSMatthew Ahrens static void mos_obj_refd(uint64_t);
118732885fcSMatthew Ahrens 
119fa9e4066Sahrens /*
120fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
121fa9e4066Sahrens  * debugging facilities.
122fa9e4066Sahrens  */
123fa9e4066Sahrens const char *
124fa9e4066Sahrens _umem_debug_init()
125fa9e4066Sahrens {
126fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
127fa9e4066Sahrens }
128fa9e4066Sahrens 
129fa9e4066Sahrens const char *
130fa9e4066Sahrens _umem_logging_init(void)
131fa9e4066Sahrens {
132fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
133fa9e4066Sahrens }
134fa9e4066Sahrens 
135fa9e4066Sahrens static void
136fa9e4066Sahrens usage(void)
137fa9e4066Sahrens {
138fa9e4066Sahrens 	(void) fprintf(stderr,
13986714001SSerapheim Dimitropoulos 	    "Usage:\t%s [-AbcdDFGhikLMPsvX] [-e [-V] [-p <path> ...]] "
140ed61ec1dSYuri Pankov 	    "[-I <inflight I/Os>]\n"
141ed61ec1dSYuri Pankov 	    "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
142ed61ec1dSYuri Pankov 	    "\t\t[<poolname> [<object> ...]]\n"
143dfd5965fSRichard Yao 	    "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>] <dataset> "
144ed61ec1dSYuri Pankov 	    "[<object> ...]\n"
145ed61ec1dSYuri Pankov 	    "\t%s -C [-A] [-U <cache>]\n"
146ed61ec1dSYuri Pankov 	    "\t%s -l [-Aqu] <device>\n"
147dfd5965fSRichard Yao 	    "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] "
148dfd5965fSRichard Yao 	    "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n"
149ed61ec1dSYuri Pankov 	    "\t%s -O <dataset> <path>\n"
150dfd5965fSRichard Yao 	    "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
151ed61ec1dSYuri Pankov 	    "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n"
1524923c69fSMatthew Ahrens 	    "\t%s -E [-A] word0:word1:...:word15\n"
153dfd5965fSRichard Yao 	    "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] "
154dfd5965fSRichard Yao 	    "<poolname>\n\n",
155ed61ec1dSYuri Pankov 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname,
1564923c69fSMatthew Ahrens 	    cmdname, cmdname);
1573ad6c7f9SVictor Latushkin 
1583ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1593ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1603ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1613ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1623ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1633ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1643ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
1653ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -b block statistics\n");
1663ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1676365109dSVictor Latushkin 	    "all data) blocks\n");
168ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
169ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -d dataset(s)\n");
170f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
1714923c69fSMatthew Ahrens 	(void) fprintf(stderr, "        -E decode and display block from an "
1724923c69fSMatthew Ahrens 	    "embedded block pointer\n");
173ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -h pool history\n");
174ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -i intent logs\n");
17564723e36SYuri Pankov 	(void) fprintf(stderr, "        -l read label contents\n");
17686714001SSerapheim Dimitropoulos 	(void) fprintf(stderr, "        -k examine the checkpointed state "
17786714001SSerapheim Dimitropoulos 	    "of the pool\n");
17882a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
17982a0a985SVictor Latushkin 	    "load spacemaps)\n");
180ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -m metaslabs\n");
181ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -M metaslab groups\n");
182ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -O perform object lookups by path\n");
183d41e7643Sek 	(void) fprintf(stderr, "        -R read and display block from a "
184ed61ec1dSYuri Pankov 	    "device\n");
185ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
186ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
187ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -v verbose (applies to all "
188ed61ec1dSYuri Pankov 	    "others)\n\n");
1893ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
190df15e419SMatthew Ahrens 	    "with other options:\n");
191feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
192feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
1933ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1943ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
195ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
196ed61ec1dSYuri Pankov 	    "safe range of transaction groups\n");
197ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -G dump zfs_dbgmsg buffer before "
198ed61ec1dSYuri Pankov 	    "exiting\n");
1992e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
200df15e419SMatthew Ahrens 	    "specify the maximum number of "
201df15e419SMatthew Ahrens 	    "checksumming I/Os [default is 200]\n");
2020e60744cSPavel Zakharov 	(void) fprintf(stderr, "        -o <variable>=<value> set global "
2030e60744cSPavel Zakharov 	    "variable to an unsigned 32-bit integer value\n");
204ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
205ed61ec1dSYuri Pankov 	    "-e to specify path to vdev dir\n");
206ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
20764723e36SYuri Pankov 	(void) fprintf(stderr, "        -q don't print label contents\n");
208ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
209ed61ec1dSYuri Pankov 	    "searching for uberblocks\n");
21064723e36SYuri Pankov 	(void) fprintf(stderr, "        -u uberblock\n");
211ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
212ed61ec1dSYuri Pankov 	    "cachefile\n");
213dfd5965fSRichard Yao 	(void) fprintf(stderr, "        -V do verbatim import\n");
214ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -x <dumpdir> -- "
215ed61ec1dSYuri Pankov 	    "dump all read blocks into specified directory\n");
216ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
217ed61ec1dSYuri Pankov 	    "work with dataset)\n\n");
218fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
219fa9e4066Sahrens 	    "to make only that option verbose\n");
220fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
221fa9e4066Sahrens 	exit(1);
222fa9e4066Sahrens }
223fa9e4066Sahrens 
22429bdd2f9SPavel Zakharov static void
22529bdd2f9SPavel Zakharov dump_debug_buffer()
22629bdd2f9SPavel Zakharov {
22729bdd2f9SPavel Zakharov 	if (dump_opt['G']) {
22829bdd2f9SPavel Zakharov 		(void) printf("\n");
22929bdd2f9SPavel Zakharov 		zfs_dbgmsg_print("zdb");
23029bdd2f9SPavel Zakharov 	}
23129bdd2f9SPavel Zakharov }
23229bdd2f9SPavel Zakharov 
233ccba0801SRich Morris /*
234ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
235ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
236ccba0801SRich Morris  */
237ccba0801SRich Morris 
238fa9e4066Sahrens static void
239fa9e4066Sahrens fatal(const char *fmt, ...)
240fa9e4066Sahrens {
241fa9e4066Sahrens 	va_list ap;
242fa9e4066Sahrens 
243fa9e4066Sahrens 	va_start(ap, fmt);
244fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
245fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
246fa9e4066Sahrens 	va_end(ap);
247fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
248fa9e4066Sahrens 
24929bdd2f9SPavel Zakharov 	dump_debug_buffer();
25029bdd2f9SPavel Zakharov 
251ccba0801SRich Morris 	exit(1);
252fa9e4066Sahrens }
253fa9e4066Sahrens 
254fa9e4066Sahrens /* ARGSUSED */
255fa9e4066Sahrens static void
256fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
257fa9e4066Sahrens {
258fa9e4066Sahrens 	nvlist_t *nv;
259fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
260fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
261fa9e4066Sahrens 
2627bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
263fa9e4066Sahrens 
264fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
265fa9e4066Sahrens 
266fa9e4066Sahrens 	umem_free(packed, nvsize);
267fa9e4066Sahrens 
268fa9e4066Sahrens 	dump_nvlist(nv, 8);
269fa9e4066Sahrens 
270fa9e4066Sahrens 	nvlist_free(nv);
271fa9e4066Sahrens }
272fa9e4066Sahrens 
2734445fffbSMatthew Ahrens /* ARGSUSED */
2744445fffbSMatthew Ahrens static void
2754445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2764445fffbSMatthew Ahrens {
2774445fffbSMatthew Ahrens 	spa_history_phys_t *shp = data;
2784445fffbSMatthew Ahrens 
2794445fffbSMatthew Ahrens 	if (shp == NULL)
2804445fffbSMatthew Ahrens 		return;
2814445fffbSMatthew Ahrens 
2824445fffbSMatthew Ahrens 	(void) printf("\t\tpool_create_len = %llu\n",
2834445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_pool_create_len);
2844445fffbSMatthew Ahrens 	(void) printf("\t\tphys_max_off = %llu\n",
2854445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_phys_max_off);
2864445fffbSMatthew Ahrens 	(void) printf("\t\tbof = %llu\n",
2874445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_bof);
2884445fffbSMatthew Ahrens 	(void) printf("\t\teof = %llu\n",
2894445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_eof);
2904445fffbSMatthew Ahrens 	(void) printf("\t\trecords_lost = %llu\n",
2914445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_records_lost);
2924445fffbSMatthew Ahrens }
2934445fffbSMatthew Ahrens 
2943f9d6ad7SLin Ling static void
2950a055120SJason King zdb_nicenum(uint64_t num, char *buf, size_t buflen)
2963f9d6ad7SLin Ling {
2973f9d6ad7SLin Ling 	if (dump_opt['P'])
2980a055120SJason King 		(void) snprintf(buf, buflen, "%llu", (longlong_t)num);
2993f9d6ad7SLin Ling 	else
3000a055120SJason King 		nicenum(num, buf, sizeof (buf));
3013f9d6ad7SLin Ling }
3023f9d6ad7SLin Ling 
3033f7978d0SAlan Somers static const char histo_stars[] = "****************************************";
3043f7978d0SAlan Somers static const uint64_t histo_width = sizeof (histo_stars) - 1;
305fa9e4066Sahrens 
306fa9e4066Sahrens static void
3070713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
308fa9e4066Sahrens {
309fa9e4066Sahrens 	int i;
310490d05b9SMatthew Ahrens 	int minidx = size - 1;
311fa9e4066Sahrens 	int maxidx = 0;
312fa9e4066Sahrens 	uint64_t max = 0;
313fa9e4066Sahrens 
314490d05b9SMatthew Ahrens 	for (i = 0; i < size; i++) {
315fa9e4066Sahrens 		if (histo[i] > max)
316fa9e4066Sahrens 			max = histo[i];
317fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
318fa9e4066Sahrens 			maxidx = i;
319fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
320fa9e4066Sahrens 			minidx = i;
321fa9e4066Sahrens 	}
322fa9e4066Sahrens 
323490d05b9SMatthew Ahrens 	if (max < histo_width)
324490d05b9SMatthew Ahrens 		max = histo_width;
325fa9e4066Sahrens 
326490d05b9SMatthew Ahrens 	for (i = minidx; i <= maxidx; i++) {
327490d05b9SMatthew Ahrens 		(void) printf("\t\t\t%3u: %6llu %s\n",
3280713e232SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
329490d05b9SMatthew Ahrens 		    &histo_stars[(max - histo[i]) * histo_width / max]);
330490d05b9SMatthew Ahrens 	}
331fa9e4066Sahrens }
332fa9e4066Sahrens 
333fa9e4066Sahrens static void
334fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
335fa9e4066Sahrens {
336fa9e4066Sahrens 	int error;
337fa9e4066Sahrens 	zap_stats_t zs;
338fa9e4066Sahrens 
339fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
340fa9e4066Sahrens 	if (error)
341fa9e4066Sahrens 		return;
342fa9e4066Sahrens 
343fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
344fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
345fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
346fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
347fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
348fa9e4066Sahrens 		return;
349fa9e4066Sahrens 	}
350fa9e4066Sahrens 
351fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
3528248818dSnd 
3538248818dSnd 	(void) printf("\t\tPointer table:\n");
3548248818dSnd 	(void) printf("\t\t\t%llu elements\n",
355fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3568248818dSnd 	(void) printf("\t\t\tzt_blk: %llu\n",
3578248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3588248818dSnd 	(void) printf("\t\t\tzt_numblks: %llu\n",
3598248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3608248818dSnd 	(void) printf("\t\t\tzt_shift: %llu\n",
3618248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3628248818dSnd 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3638248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3648248818dSnd 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3658248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3668248818dSnd 
367fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
368fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
369fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
370fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
371fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
372fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
3738248818dSnd 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3748248818dSnd 	    (u_longlong_t)zs.zs_block_type);
3758248818dSnd 	(void) printf("\t\tzap_magic: 0x%llx\n",
3768248818dSnd 	    (u_longlong_t)zs.zs_magic);
3778248818dSnd 	(void) printf("\t\tzap_salt: 0x%llx\n",
3788248818dSnd 	    (u_longlong_t)zs.zs_salt);
379fa9e4066Sahrens 
380fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
3810713e232SGeorge Wilson 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
382fa9e4066Sahrens 
383fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
3840713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
385fa9e4066Sahrens 
386fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
3870713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
388fa9e4066Sahrens 
389fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
3900713e232SGeorge Wilson 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
391fa9e4066Sahrens 
392fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
3930713e232SGeorge Wilson 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
394fa9e4066Sahrens }
395fa9e4066Sahrens 
396fa9e4066Sahrens /*ARGSUSED*/
397fa9e4066Sahrens static void
398fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
399fa9e4066Sahrens {
400fa9e4066Sahrens }
401fa9e4066Sahrens 
4026de8f417SVictor Latushkin /*ARGSUSED*/
4036de8f417SVictor Latushkin static void
4046de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
4056de8f417SVictor Latushkin {
4066de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
4076de8f417SVictor Latushkin }
4086de8f417SVictor Latushkin 
409fa9e4066Sahrens /*ARGSUSED*/
4103f7978d0SAlan Somers static void
411fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
412fa9e4066Sahrens {
413fa9e4066Sahrens }
414fa9e4066Sahrens 
415fa9e4066Sahrens /*ARGSUSED*/
416fa9e4066Sahrens static void
417fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
418fa9e4066Sahrens {
419fa9e4066Sahrens }
420fa9e4066Sahrens 
421fa9e4066Sahrens /*ARGSUSED*/
422fa9e4066Sahrens static void
423fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
424fa9e4066Sahrens {
425fa9e4066Sahrens 	zap_cursor_t zc;
426fa9e4066Sahrens 	zap_attribute_t attr;
427fa9e4066Sahrens 	void *prop;
4283f7978d0SAlan Somers 	unsigned i;
429fa9e4066Sahrens 
430fa9e4066Sahrens 	dump_zap_stats(os, object);
431fa9e4066Sahrens 	(void) printf("\n");
432fa9e4066Sahrens 
433fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
434fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
435fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
436fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
437fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
438fa9e4066Sahrens 			(void) printf("\n");
439fa9e4066Sahrens 			continue;
440fa9e4066Sahrens 		}
441fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
442fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
443fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
444fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
445fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
446*2d37a1a3SToomas Soome 			if (strcmp(attr.za_name,
447*2d37a1a3SToomas Soome 			    DSL_CRYPTO_KEY_MASTER_KEY) == 0 ||
448*2d37a1a3SToomas Soome 			    strcmp(attr.za_name,
449*2d37a1a3SToomas Soome 			    DSL_CRYPTO_KEY_HMAC_KEY) == 0 ||
450*2d37a1a3SToomas Soome 			    strcmp(attr.za_name, DSL_CRYPTO_KEY_IV) == 0 ||
451*2d37a1a3SToomas Soome 			    strcmp(attr.za_name, DSL_CRYPTO_KEY_MAC) == 0) {
452*2d37a1a3SToomas Soome 				uint8_t *u8 = prop;
453*2d37a1a3SToomas Soome 
454*2d37a1a3SToomas Soome 				for (i = 0; i < attr.za_num_integers; i++) {
455*2d37a1a3SToomas Soome 					(void) printf("%02x", u8[i]);
456*2d37a1a3SToomas Soome 				}
457*2d37a1a3SToomas Soome 			} else {
458*2d37a1a3SToomas Soome 				(void) printf("%s", (char *)prop);
459*2d37a1a3SToomas Soome 			}
460fa9e4066Sahrens 		} else {
461fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
462fa9e4066Sahrens 				switch (attr.za_integer_length) {
463fa9e4066Sahrens 				case 2:
464fa9e4066Sahrens 					(void) printf("%u ",
465fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
466fa9e4066Sahrens 					break;
467fa9e4066Sahrens 				case 4:
468fa9e4066Sahrens 					(void) printf("%u ",
469fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
470fa9e4066Sahrens 					break;
471fa9e4066Sahrens 				case 8:
472fa9e4066Sahrens 					(void) printf("%lld ",
473fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
474fa9e4066Sahrens 					break;
475fa9e4066Sahrens 				}
476fa9e4066Sahrens 			}
477fa9e4066Sahrens 		}
478fa9e4066Sahrens 		(void) printf("\n");
479fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
480fa9e4066Sahrens 	}
48187e5029aSahrens 	zap_cursor_fini(&zc);
482fa9e4066Sahrens }
483fa9e4066Sahrens 
484732885fcSMatthew Ahrens static void
485732885fcSMatthew Ahrens dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
486732885fcSMatthew Ahrens {
487732885fcSMatthew Ahrens 	bpobj_phys_t *bpop = data;
488732885fcSMatthew Ahrens 	char bytes[32], comp[32], uncomp[32];
489732885fcSMatthew Ahrens 
4900a055120SJason King 	/* make sure the output won't get truncated */
4910a055120SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
4920a055120SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
4930a055120SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
4940a055120SJason King 
495732885fcSMatthew Ahrens 	if (bpop == NULL)
496732885fcSMatthew Ahrens 		return;
497732885fcSMatthew Ahrens 
4980a055120SJason King 	zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
4990a055120SJason King 	zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
5000a055120SJason King 	zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
501732885fcSMatthew Ahrens 
502732885fcSMatthew Ahrens 	(void) printf("\t\tnum_blkptrs = %llu\n",
503732885fcSMatthew Ahrens 	    (u_longlong_t)bpop->bpo_num_blkptrs);
504732885fcSMatthew Ahrens 	(void) printf("\t\tbytes = %s\n", bytes);
505732885fcSMatthew Ahrens 	if (size >= BPOBJ_SIZE_V1) {
506732885fcSMatthew Ahrens 		(void) printf("\t\tcomp = %s\n", comp);
507732885fcSMatthew Ahrens 		(void) printf("\t\tuncomp = %s\n", uncomp);
508732885fcSMatthew Ahrens 	}
509732885fcSMatthew Ahrens 	if (size >= sizeof (*bpop)) {
510732885fcSMatthew Ahrens 		(void) printf("\t\tsubobjs = %llu\n",
511732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_subobjs);
512732885fcSMatthew Ahrens 		(void) printf("\t\tnum_subobjs = %llu\n",
513732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_num_subobjs);
514732885fcSMatthew Ahrens 	}
515732885fcSMatthew Ahrens 
516732885fcSMatthew Ahrens 	if (dump_opt['d'] < 5)
517732885fcSMatthew Ahrens 		return;
518732885fcSMatthew Ahrens 
519732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
520732885fcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
521732885fcSMatthew Ahrens 		blkptr_t bp;
522732885fcSMatthew Ahrens 
523732885fcSMatthew Ahrens 		int err = dmu_read(os, object,
524732885fcSMatthew Ahrens 		    i * sizeof (bp), sizeof (bp), &bp, 0);
525732885fcSMatthew Ahrens 		if (err != 0) {
526732885fcSMatthew Ahrens 			(void) printf("got error %u from dmu_read\n", err);
527732885fcSMatthew Ahrens 			break;
528732885fcSMatthew Ahrens 		}
529732885fcSMatthew Ahrens 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
530732885fcSMatthew Ahrens 		(void) printf("\t%s\n", blkbuf);
531732885fcSMatthew Ahrens 	}
532732885fcSMatthew Ahrens }
533732885fcSMatthew Ahrens 
534732885fcSMatthew Ahrens /* ARGSUSED */
535732885fcSMatthew Ahrens static void
536732885fcSMatthew Ahrens dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
537732885fcSMatthew Ahrens {
538732885fcSMatthew Ahrens 	dmu_object_info_t doi;
539732885fcSMatthew Ahrens 
540732885fcSMatthew Ahrens 	VERIFY0(dmu_object_info(os, object, &doi));
541732885fcSMatthew Ahrens 	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
542732885fcSMatthew Ahrens 
543732885fcSMatthew Ahrens 	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
544732885fcSMatthew Ahrens 	if (err != 0) {
545732885fcSMatthew Ahrens 		(void) printf("got error %u from dmu_read\n", err);
546732885fcSMatthew Ahrens 		kmem_free(subobjs, doi.doi_max_offset);
547732885fcSMatthew Ahrens 		return;
548732885fcSMatthew Ahrens 	}
549732885fcSMatthew Ahrens 
550732885fcSMatthew Ahrens 	int64_t last_nonzero = -1;
551732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
552732885fcSMatthew Ahrens 		if (subobjs[i] != 0)
553732885fcSMatthew Ahrens 			last_nonzero = i;
554732885fcSMatthew Ahrens 	}
555732885fcSMatthew Ahrens 
556732885fcSMatthew Ahrens 	for (int64_t i = 0; i <= last_nonzero; i++) {
557732885fcSMatthew Ahrens 		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
558732885fcSMatthew Ahrens 	}
559732885fcSMatthew Ahrens 	kmem_free(subobjs, doi.doi_max_offset);
560732885fcSMatthew Ahrens }
561732885fcSMatthew Ahrens 
562486ae710SMatthew Ahrens /*ARGSUSED*/
563486ae710SMatthew Ahrens static void
564486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
565486ae710SMatthew Ahrens {
566486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
567486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
568486ae710SMatthew Ahrens }
569486ae710SMatthew Ahrens 
5700a586ceaSMark Shellenbaum /*ARGSUSED*/
5710a586ceaSMark Shellenbaum static void
5720a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5730a586ceaSMark Shellenbaum {
5740a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5750a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5760a586ceaSMark Shellenbaum 
5770a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5780a586ceaSMark Shellenbaum 	(void) printf("\n");
5790a586ceaSMark Shellenbaum 
5800a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5810a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5820a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5830a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
5840a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5850a586ceaSMark Shellenbaum 			(void) printf("\n");
5860a586ceaSMark Shellenbaum 			continue;
5870a586ceaSMark Shellenbaum 		}
5880a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
5890a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
5900a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
5910a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
5920a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
5930a586ceaSMark Shellenbaum 	}
5940a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5950a586ceaSMark Shellenbaum }
5960a586ceaSMark Shellenbaum 
5970a586ceaSMark Shellenbaum /*ARGSUSED*/
5980a586ceaSMark Shellenbaum static void
5990a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
6000a586ceaSMark Shellenbaum {
6010a586ceaSMark Shellenbaum 	zap_cursor_t zc;
6020a586ceaSMark Shellenbaum 	zap_attribute_t attr;
6030a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
6043f7978d0SAlan Somers 	unsigned i;
6050a586ceaSMark Shellenbaum 
6060a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
6070a586ceaSMark Shellenbaum 	(void) printf("\n");
6080a586ceaSMark Shellenbaum 
6090a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
6100a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
6110a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
6120a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
6130a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
6140a586ceaSMark Shellenbaum 			(void) printf("\n");
6150a586ceaSMark Shellenbaum 			continue;
6160a586ceaSMark Shellenbaum 		}
6170a586ceaSMark Shellenbaum 
6180a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
6190a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
6200a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
6210a586ceaSMark Shellenbaum 
6220a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
6230a586ceaSMark Shellenbaum 		    attr.za_integer_length,
6240a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
6250a586ceaSMark Shellenbaum 
6260a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
6270a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
6280a586ceaSMark Shellenbaum 		(void) printf("]\n");
6290a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
6300a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
6310a586ceaSMark Shellenbaum 	}
6320a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
6330a586ceaSMark Shellenbaum }
6340a586ceaSMark Shellenbaum 
635e7437265Sahrens /*ARGSUSED*/
636e7437265Sahrens static void
637e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
638e7437265Sahrens {
639e7437265Sahrens 	zap_cursor_t zc;
640e7437265Sahrens 	zap_attribute_t attr;
641e7437265Sahrens 	const char *typenames[] = {
642e7437265Sahrens 		/* 0 */ "not specified",
643e7437265Sahrens 		/* 1 */ "FIFO",
644e7437265Sahrens 		/* 2 */ "Character Device",
645e7437265Sahrens 		/* 3 */ "3 (invalid)",
646e7437265Sahrens 		/* 4 */ "Directory",
647e7437265Sahrens 		/* 5 */ "5 (invalid)",
648e7437265Sahrens 		/* 6 */ "Block Device",
649e7437265Sahrens 		/* 7 */ "7 (invalid)",
650e7437265Sahrens 		/* 8 */ "Regular File",
651e7437265Sahrens 		/* 9 */ "9 (invalid)",
652e7437265Sahrens 		/* 10 */ "Symbolic Link",
653e7437265Sahrens 		/* 11 */ "11 (invalid)",
654e7437265Sahrens 		/* 12 */ "Socket",
655e7437265Sahrens 		/* 13 */ "Door",
656e7437265Sahrens 		/* 14 */ "Event Port",
657e7437265Sahrens 		/* 15 */ "15 (invalid)",
658e7437265Sahrens 	};
659e7437265Sahrens 
660e7437265Sahrens 	dump_zap_stats(os, object);
661e7437265Sahrens 	(void) printf("\n");
662e7437265Sahrens 
663e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
664e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
665e7437265Sahrens 	    zap_cursor_advance(&zc)) {
666e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
667e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
668e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
669e7437265Sahrens 	}
670e7437265Sahrens 	zap_cursor_fini(&zc);
671e7437265Sahrens }
672e7437265Sahrens 
6733f7978d0SAlan Somers static int
6740713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
6750713e232SGeorge Wilson {
6760713e232SGeorge Wilson 	int refcount = 0;
6770713e232SGeorge Wilson 
6780713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
6790713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
6800713e232SGeorge Wilson 
6810713e232SGeorge Wilson 		if (sm != NULL &&
6820713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6830713e232SGeorge Wilson 			return (1);
6840713e232SGeorge Wilson 		return (0);
6850713e232SGeorge Wilson 	}
6860713e232SGeorge Wilson 
6873f7978d0SAlan Somers 	for (unsigned c = 0; c < vd->vdev_children; c++)
6880713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
6890713e232SGeorge Wilson 	return (refcount);
6900713e232SGeorge Wilson }
6910713e232SGeorge Wilson 
6923f7978d0SAlan Somers static int
6930713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
6940713e232SGeorge Wilson {
6950713e232SGeorge Wilson 	int refcount = 0;
6960713e232SGeorge Wilson 
6975cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_top == vd) {
6985cabbc6bSPrashanth Sreenivasa 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
6990713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
7000713e232SGeorge Wilson 
7010713e232SGeorge Wilson 			if (sm != NULL &&
7020713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
7030713e232SGeorge Wilson 				refcount++;
7040713e232SGeorge Wilson 		}
7050713e232SGeorge Wilson 	}
7063f7978d0SAlan Somers 	for (unsigned c = 0; c < vd->vdev_children; c++)
7070713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
7080713e232SGeorge Wilson 
7090713e232SGeorge Wilson 	return (refcount);
7100713e232SGeorge Wilson }
7110713e232SGeorge Wilson 
7125cabbc6bSPrashanth Sreenivasa static int
7135cabbc6bSPrashanth Sreenivasa get_obsolete_refcount(vdev_t *vd)
7145cabbc6bSPrashanth Sreenivasa {
7155cabbc6bSPrashanth Sreenivasa 	int refcount = 0;
7165cabbc6bSPrashanth Sreenivasa 
7175cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_obj = vdev_obsolete_sm_object(vd);
7185cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_top == vd && obsolete_sm_obj != 0) {
7195cabbc6bSPrashanth Sreenivasa 		dmu_object_info_t doi;
7205cabbc6bSPrashanth Sreenivasa 		VERIFY0(dmu_object_info(vd->vdev_spa->spa_meta_objset,
7215cabbc6bSPrashanth Sreenivasa 		    obsolete_sm_obj, &doi));
7225cabbc6bSPrashanth Sreenivasa 		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
7235cabbc6bSPrashanth Sreenivasa 			refcount++;
7245cabbc6bSPrashanth Sreenivasa 		}
7255cabbc6bSPrashanth Sreenivasa 	} else {
7265cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
7275cabbc6bSPrashanth Sreenivasa 		ASSERT3U(obsolete_sm_obj, ==, 0);
7285cabbc6bSPrashanth Sreenivasa 	}
7295cabbc6bSPrashanth Sreenivasa 	for (unsigned c = 0; c < vd->vdev_children; c++) {
7305cabbc6bSPrashanth Sreenivasa 		refcount += get_obsolete_refcount(vd->vdev_child[c]);
7315cabbc6bSPrashanth Sreenivasa 	}
7325cabbc6bSPrashanth Sreenivasa 
7335cabbc6bSPrashanth Sreenivasa 	return (refcount);
7345cabbc6bSPrashanth Sreenivasa }
7355cabbc6bSPrashanth Sreenivasa 
7365cabbc6bSPrashanth Sreenivasa static int
7375cabbc6bSPrashanth Sreenivasa get_prev_obsolete_spacemap_refcount(spa_t *spa)
7385cabbc6bSPrashanth Sreenivasa {
7395cabbc6bSPrashanth Sreenivasa 	uint64_t prev_obj =
7405cabbc6bSPrashanth Sreenivasa 	    spa->spa_condensing_indirect_phys.scip_prev_obsolete_sm_object;
7415cabbc6bSPrashanth Sreenivasa 	if (prev_obj != 0) {
7425cabbc6bSPrashanth Sreenivasa 		dmu_object_info_t doi;
7435cabbc6bSPrashanth Sreenivasa 		VERIFY0(dmu_object_info(spa->spa_meta_objset, prev_obj, &doi));
7445cabbc6bSPrashanth Sreenivasa 		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
7455cabbc6bSPrashanth Sreenivasa 			return (1);
7465cabbc6bSPrashanth Sreenivasa 		}
7475cabbc6bSPrashanth Sreenivasa 	}
7485cabbc6bSPrashanth Sreenivasa 	return (0);
7495cabbc6bSPrashanth Sreenivasa }
7505cabbc6bSPrashanth Sreenivasa 
75186714001SSerapheim Dimitropoulos static int
75286714001SSerapheim Dimitropoulos get_checkpoint_refcount(vdev_t *vd)
75386714001SSerapheim Dimitropoulos {
75486714001SSerapheim Dimitropoulos 	int refcount = 0;
75586714001SSerapheim Dimitropoulos 
75686714001SSerapheim Dimitropoulos 	if (vd->vdev_top == vd && vd->vdev_top_zap != 0 &&
75786714001SSerapheim Dimitropoulos 	    zap_contains(spa_meta_objset(vd->vdev_spa),
75886714001SSerapheim Dimitropoulos 	    vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) == 0)
75986714001SSerapheim Dimitropoulos 		refcount++;
76086714001SSerapheim Dimitropoulos 
76186714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < vd->vdev_children; c++)
76286714001SSerapheim Dimitropoulos 		refcount += get_checkpoint_refcount(vd->vdev_child[c]);
76386714001SSerapheim Dimitropoulos 
76486714001SSerapheim Dimitropoulos 	return (refcount);
76586714001SSerapheim Dimitropoulos }
76686714001SSerapheim Dimitropoulos 
7670713e232SGeorge Wilson static int
7680713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
7690713e232SGeorge Wilson {
7702acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
7712acef22dSMatthew Ahrens 	uint64_t actual_refcount;
7720713e232SGeorge Wilson 
7732acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
7742acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
7752acef22dSMatthew Ahrens 	    &expected_refcount);
7760713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
7770713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
7785cabbc6bSPrashanth Sreenivasa 	actual_refcount += get_obsolete_refcount(spa->spa_root_vdev);
7795cabbc6bSPrashanth Sreenivasa 	actual_refcount += get_prev_obsolete_spacemap_refcount(spa);
78086714001SSerapheim Dimitropoulos 	actual_refcount += get_checkpoint_refcount(spa->spa_root_vdev);
7810713e232SGeorge Wilson 
7820713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
7832acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
7842acef22dSMatthew Ahrens 		    "actual %lld\n",
7852acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
7862acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
7870713e232SGeorge Wilson 		return (2);
7880713e232SGeorge Wilson 	}
7890713e232SGeorge Wilson 	return (0);
7900713e232SGeorge Wilson }
7910713e232SGeorge Wilson 
792fa9e4066Sahrens static void
7930713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
794fa9e4066Sahrens {
7955cabbc6bSPrashanth Sreenivasa 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
7965cabbc6bSPrashanth Sreenivasa 	    "INVALID", "INVALID", "INVALID", "INVALID" };
797fa9e4066Sahrens 
7980713e232SGeorge Wilson 	if (sm == NULL)
799fa9e4066Sahrens 		return;
800fa9e4066Sahrens 
8015cabbc6bSPrashanth Sreenivasa 	(void) printf("space map object %llu:\n",
802555d674dSSerapheim Dimitropoulos 	    (longlong_t)sm->sm_object);
803555d674dSSerapheim Dimitropoulos 	(void) printf("  smp_length = 0x%llx\n",
804555d674dSSerapheim Dimitropoulos 	    (longlong_t)sm->sm_phys->smp_length);
8055cabbc6bSPrashanth Sreenivasa 	(void) printf("  smp_alloc = 0x%llx\n",
8065cabbc6bSPrashanth Sreenivasa 	    (longlong_t)sm->sm_phys->smp_alloc);
8075cabbc6bSPrashanth Sreenivasa 
808555d674dSSerapheim Dimitropoulos 	if (dump_opt['d'] < 6 && dump_opt['m'] < 4)
809555d674dSSerapheim Dimitropoulos 		return;
810555d674dSSerapheim Dimitropoulos 
811fa9e4066Sahrens 	/*
812fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
813fa9e4066Sahrens 	 */
81417f11284SSerapheim Dimitropoulos 	uint8_t mapshift = sm->sm_shift;
81517f11284SSerapheim Dimitropoulos 	int64_t alloc = 0;
816555d674dSSerapheim Dimitropoulos 	uint64_t word, entry_id = 0;
81717f11284SSerapheim Dimitropoulos 	for (uint64_t offset = 0; offset < space_map_length(sm);
81817f11284SSerapheim Dimitropoulos 	    offset += sizeof (word)) {
8190713e232SGeorge Wilson 
8200713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
82117f11284SSerapheim Dimitropoulos 		    sizeof (word), &word, DMU_READ_PREFETCH));
8220713e232SGeorge Wilson 
82317f11284SSerapheim Dimitropoulos 		if (sm_entry_is_debug(word)) {
824555d674dSSerapheim Dimitropoulos 			(void) printf("\t    [%6llu] %s: txg %llu pass %llu\n",
825555d674dSSerapheim Dimitropoulos 			    (u_longlong_t)entry_id,
82617f11284SSerapheim Dimitropoulos 			    ddata[SM_DEBUG_ACTION_DECODE(word)],
82717f11284SSerapheim Dimitropoulos 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(word),
82817f11284SSerapheim Dimitropoulos 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(word));
829555d674dSSerapheim Dimitropoulos 			entry_id++;
83017f11284SSerapheim Dimitropoulos 			continue;
83117f11284SSerapheim Dimitropoulos 		}
83217f11284SSerapheim Dimitropoulos 
83317f11284SSerapheim Dimitropoulos 		uint8_t words;
83417f11284SSerapheim Dimitropoulos 		char entry_type;
83517f11284SSerapheim Dimitropoulos 		uint64_t entry_off, entry_run, entry_vdev = SM_NO_VDEVID;
83617f11284SSerapheim Dimitropoulos 
83717f11284SSerapheim Dimitropoulos 		if (sm_entry_is_single_word(word)) {
83817f11284SSerapheim Dimitropoulos 			entry_type = (SM_TYPE_DECODE(word) == SM_ALLOC) ?
83917f11284SSerapheim Dimitropoulos 			    'A' : 'F';
84017f11284SSerapheim Dimitropoulos 			entry_off = (SM_OFFSET_DECODE(word) << mapshift) +
84117f11284SSerapheim Dimitropoulos 			    sm->sm_start;
84217f11284SSerapheim Dimitropoulos 			entry_run = SM_RUN_DECODE(word) << mapshift;
84317f11284SSerapheim Dimitropoulos 			words = 1;
844fa9e4066Sahrens 		} else {
84517f11284SSerapheim Dimitropoulos 			/* it is a two-word entry so we read another word */
84617f11284SSerapheim Dimitropoulos 			ASSERT(sm_entry_is_double_word(word));
84717f11284SSerapheim Dimitropoulos 
84817f11284SSerapheim Dimitropoulos 			uint64_t extra_word;
84917f11284SSerapheim Dimitropoulos 			offset += sizeof (extra_word);
85017f11284SSerapheim Dimitropoulos 			VERIFY0(dmu_read(os, space_map_object(sm), offset,
85117f11284SSerapheim Dimitropoulos 			    sizeof (extra_word), &extra_word,
85217f11284SSerapheim Dimitropoulos 			    DMU_READ_PREFETCH));
85317f11284SSerapheim Dimitropoulos 
85417f11284SSerapheim Dimitropoulos 			ASSERT3U(offset, <=, space_map_length(sm));
85517f11284SSerapheim Dimitropoulos 
85617f11284SSerapheim Dimitropoulos 			entry_run = SM2_RUN_DECODE(word) << mapshift;
85717f11284SSerapheim Dimitropoulos 			entry_vdev = SM2_VDEV_DECODE(word);
85817f11284SSerapheim Dimitropoulos 			entry_type = (SM2_TYPE_DECODE(extra_word) == SM_ALLOC) ?
85917f11284SSerapheim Dimitropoulos 			    'A' : 'F';
86017f11284SSerapheim Dimitropoulos 			entry_off = (SM2_OFFSET_DECODE(extra_word) <<
86117f11284SSerapheim Dimitropoulos 			    mapshift) + sm->sm_start;
86217f11284SSerapheim Dimitropoulos 			words = 2;
863fa9e4066Sahrens 		}
86417f11284SSerapheim Dimitropoulos 
86517f11284SSerapheim Dimitropoulos 		(void) printf("\t    [%6llu]    %c  range:"
86617f11284SSerapheim Dimitropoulos 		    " %010llx-%010llx  size: %06llx vdev: %06llu words: %u\n",
867555d674dSSerapheim Dimitropoulos 		    (u_longlong_t)entry_id,
86817f11284SSerapheim Dimitropoulos 		    entry_type, (u_longlong_t)entry_off,
86917f11284SSerapheim Dimitropoulos 		    (u_longlong_t)(entry_off + entry_run),
87017f11284SSerapheim Dimitropoulos 		    (u_longlong_t)entry_run,
87117f11284SSerapheim Dimitropoulos 		    (u_longlong_t)entry_vdev, words);
87217f11284SSerapheim Dimitropoulos 
87317f11284SSerapheim Dimitropoulos 		if (entry_type == 'A')
87417f11284SSerapheim Dimitropoulos 			alloc += entry_run;
87517f11284SSerapheim Dimitropoulos 		else
87617f11284SSerapheim Dimitropoulos 			alloc -= entry_run;
877555d674dSSerapheim Dimitropoulos 		entry_id++;
878fa9e4066Sahrens 	}
879555d674dSSerapheim Dimitropoulos 	if (alloc != space_map_allocated(sm)) {
88017f11284SSerapheim Dimitropoulos 		(void) printf("space_map_object alloc (%lld) INCONSISTENT "
88117f11284SSerapheim Dimitropoulos 		    "with space map summary (%lld)\n",
88217f11284SSerapheim Dimitropoulos 		    (longlong_t)space_map_allocated(sm), (longlong_t)alloc);
883fa9e4066Sahrens 	}
884fa9e4066Sahrens }
885fa9e4066Sahrens 
886d6e555bdSGeorge Wilson static void
887d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
888d6e555bdSGeorge Wilson {
8893f9d6ad7SLin Ling 	char maxbuf[32];
89086714001SSerapheim Dimitropoulos 	range_tree_t *rt = msp->ms_allocatable;
89186714001SSerapheim Dimitropoulos 	avl_tree_t *t = &msp->ms_allocatable_by_size;
8920713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
893d6e555bdSGeorge Wilson 
8940a055120SJason King 	/* max sure nicenum has enough space */
8950a055120SJason King 	CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ);
8960a055120SJason King 
8970a055120SJason King 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf, sizeof (maxbuf));
898d6e555bdSGeorge Wilson 
89987219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
900d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
901d6e555bdSGeorge Wilson 	    "freepct", free_pct);
9020713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
9030713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
904d6e555bdSGeorge Wilson }
905d6e555bdSGeorge Wilson 
906fa9e4066Sahrens static void
907fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
908fa9e4066Sahrens {
909fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
910fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
9110713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
9123f9d6ad7SLin Ling 	char freebuf[32];
913fa9e4066Sahrens 
9140a055120SJason King 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf,
9150a055120SJason King 	    sizeof (freebuf));
916fa9e4066Sahrens 
917fa9e4066Sahrens 	(void) printf(
91887219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
9190713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
9200713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
921fa9e4066Sahrens 
9220713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
923d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
924a0b03b16SSerapheim Dimitropoulos 		VERIFY0(metaslab_load(msp));
925a0b03b16SSerapheim Dimitropoulos 		range_tree_stat_verify(msp->ms_allocatable);
926bc3975b5SVictor Latushkin 		dump_metaslab_stats(msp);
9270713e232SGeorge Wilson 		metaslab_unload(msp);
928d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
929d6e555bdSGeorge Wilson 	}
930d6e555bdSGeorge Wilson 
9310713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
9322acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
9330713e232SGeorge Wilson 		/*
9340713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
9350713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
9360713e232SGeorge Wilson 		 */
9372e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
9382e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
9390713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
9402e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
9410713e232SGeorge Wilson 	}
9420713e232SGeorge Wilson 
943555d674dSSerapheim Dimitropoulos 	ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
944555d674dSSerapheim Dimitropoulos 	dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
94587219db7SVictor Latushkin }
946fa9e4066Sahrens 
94787219db7SVictor Latushkin static void
94887219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
94987219db7SVictor Latushkin {
950663207adSDon Brady 	vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
951663207adSDon Brady 	const char *bias_str;
952663207adSDon Brady 
953663207adSDon Brady 	bias_str = (alloc_bias == VDEV_BIAS_LOG || vd->vdev_islog) ?
954663207adSDon Brady 	    VDEV_ALLOC_BIAS_LOG :
955663207adSDon Brady 	    (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL :
956663207adSDon Brady 	    (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP :
957663207adSDon Brady 	    vd->vdev_islog ? "log" : "";
958663207adSDon Brady 
959663207adSDon Brady 	(void) printf("\tvdev %10llu   %s\n"
960663207adSDon Brady 	    "\t%-10s%5llu   %-19s   %-15s   %-12s\n",
961663207adSDon Brady 	    (u_longlong_t)vd->vdev_id, bias_str,
96287219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
96387219db7SVictor Latushkin 	    "offset", "spacemap", "free");
964663207adSDon Brady 	(void) printf("\t%15s   %19s   %15s   %12s\n",
96587219db7SVictor Latushkin 	    "---------------", "-------------------",
966663207adSDon Brady 	    "---------------", "------------");
967fa9e4066Sahrens }
968fa9e4066Sahrens 
9692e4c9986SGeorge Wilson static void
9702e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
9712e4c9986SGeorge Wilson {
9722e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
9732e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
9742e4c9986SGeorge Wilson 	uint64_t fragmentation;
9752e4c9986SGeorge Wilson 
9762e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
9772e4c9986SGeorge Wilson 
9783f7978d0SAlan Somers 	for (unsigned c = 0; c < rvd->vdev_children; c++) {
9792e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
9802e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
9812e4c9986SGeorge Wilson 
982663207adSDon Brady 		if (mg == NULL || mg->mg_class != mc)
9832e4c9986SGeorge Wilson 			continue;
9842e4c9986SGeorge Wilson 
9852e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
9862e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
9872e4c9986SGeorge Wilson 
9882e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
9892e4c9986SGeorge Wilson 		    "fragmentation",
9902e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
9912e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
9922e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
9932e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
9942e4c9986SGeorge Wilson 		} else {
9952e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
9962e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
9972e4c9986SGeorge Wilson 		}
9982e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
9992e4c9986SGeorge Wilson 	}
10002e4c9986SGeorge Wilson 
10012e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
10022e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
10032e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
10042e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
10052e4c9986SGeorge Wilson 	else
10062e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
10072e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
10082e4c9986SGeorge Wilson }
10092e4c9986SGeorge Wilson 
10105cabbc6bSPrashanth Sreenivasa static void
10115cabbc6bSPrashanth Sreenivasa print_vdev_indirect(vdev_t *vd)
10125cabbc6bSPrashanth Sreenivasa {
10135cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
10145cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
10155cabbc6bSPrashanth Sreenivasa 	vdev_indirect_births_t *vib = vd->vdev_indirect_births;
10165cabbc6bSPrashanth Sreenivasa 
10175cabbc6bSPrashanth Sreenivasa 	if (vim == NULL) {
10185cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vib, ==, NULL);
10195cabbc6bSPrashanth Sreenivasa 		return;
10205cabbc6bSPrashanth Sreenivasa 	}
10215cabbc6bSPrashanth Sreenivasa 
10225cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vdev_indirect_mapping_object(vim), ==,
10235cabbc6bSPrashanth Sreenivasa 	    vic->vic_mapping_object);
10245cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vdev_indirect_births_object(vib), ==,
10255cabbc6bSPrashanth Sreenivasa 	    vic->vic_births_object);
10265cabbc6bSPrashanth Sreenivasa 
10275cabbc6bSPrashanth Sreenivasa 	(void) printf("indirect births obj %llu:\n",
10285cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vic->vic_births_object);
10295cabbc6bSPrashanth Sreenivasa 	(void) printf("    vib_count = %llu\n",
10305cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vdev_indirect_births_count(vib));
10315cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < vdev_indirect_births_count(vib); i++) {
10325cabbc6bSPrashanth Sreenivasa 		vdev_indirect_birth_entry_phys_t *cur_vibe =
10335cabbc6bSPrashanth Sreenivasa 		    &vib->vib_entries[i];
10345cabbc6bSPrashanth Sreenivasa 		(void) printf("\toffset %llx -> txg %llu\n",
10355cabbc6bSPrashanth Sreenivasa 		    (longlong_t)cur_vibe->vibe_offset,
10365cabbc6bSPrashanth Sreenivasa 		    (longlong_t)cur_vibe->vibe_phys_birth_txg);
10375cabbc6bSPrashanth Sreenivasa 	}
10385cabbc6bSPrashanth Sreenivasa 	(void) printf("\n");
10395cabbc6bSPrashanth Sreenivasa 
10405cabbc6bSPrashanth Sreenivasa 	(void) printf("indirect mapping obj %llu:\n",
10415cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vic->vic_mapping_object);
10425cabbc6bSPrashanth Sreenivasa 	(void) printf("    vim_max_offset = 0x%llx\n",
10435cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vdev_indirect_mapping_max_offset(vim));
10445cabbc6bSPrashanth Sreenivasa 	(void) printf("    vim_bytes_mapped = 0x%llx\n",
10455cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vdev_indirect_mapping_bytes_mapped(vim));
10465cabbc6bSPrashanth Sreenivasa 	(void) printf("    vim_count = %llu\n",
10475cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vdev_indirect_mapping_num_entries(vim));
10485cabbc6bSPrashanth Sreenivasa 
10495cabbc6bSPrashanth Sreenivasa 	if (dump_opt['d'] <= 5 && dump_opt['m'] <= 3)
10505cabbc6bSPrashanth Sreenivasa 		return;
10515cabbc6bSPrashanth Sreenivasa 
10525cabbc6bSPrashanth Sreenivasa 	uint32_t *counts = vdev_indirect_mapping_load_obsolete_counts(vim);
10535cabbc6bSPrashanth Sreenivasa 
10545cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
10555cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_entry_phys_t *vimep =
10565cabbc6bSPrashanth Sreenivasa 		    &vim->vim_entries[i];
10575cabbc6bSPrashanth Sreenivasa 		(void) printf("\t<%llx:%llx:%llx> -> "
10585cabbc6bSPrashanth Sreenivasa 		    "<%llx:%llx:%llx> (%x obsolete)\n",
10595cabbc6bSPrashanth Sreenivasa 		    (longlong_t)vd->vdev_id,
10605cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
10615cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
10625cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_GET_VDEV(&vimep->vimep_dst),
10635cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_GET_OFFSET(&vimep->vimep_dst),
10645cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
10655cabbc6bSPrashanth Sreenivasa 		    counts[i]);
10665cabbc6bSPrashanth Sreenivasa 	}
10675cabbc6bSPrashanth Sreenivasa 	(void) printf("\n");
10685cabbc6bSPrashanth Sreenivasa 
10695cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd);
10705cabbc6bSPrashanth Sreenivasa 	if (obsolete_sm_object != 0) {
10715cabbc6bSPrashanth Sreenivasa 		objset_t *mos = vd->vdev_spa->spa_meta_objset;
10725cabbc6bSPrashanth Sreenivasa 		(void) printf("obsolete space map object %llu:\n",
10735cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_sm_object);
10745cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_obsolete_sm != NULL);
10755cabbc6bSPrashanth Sreenivasa 		ASSERT3U(space_map_object(vd->vdev_obsolete_sm), ==,
10765cabbc6bSPrashanth Sreenivasa 		    obsolete_sm_object);
10775cabbc6bSPrashanth Sreenivasa 		dump_spacemap(mos, vd->vdev_obsolete_sm);
10785cabbc6bSPrashanth Sreenivasa 		(void) printf("\n");
10795cabbc6bSPrashanth Sreenivasa 	}
10805cabbc6bSPrashanth Sreenivasa }
10815cabbc6bSPrashanth Sreenivasa 
1082fa9e4066Sahrens static void
1083fa9e4066Sahrens dump_metaslabs(spa_t *spa)
1084fa9e4066Sahrens {
108587219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
108687219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
1087fa9e4066Sahrens 
1088fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
1089fa9e4066Sahrens 
109087219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
109187219db7SVictor Latushkin 		c = zopt_object[0];
109287219db7SVictor Latushkin 
109387219db7SVictor Latushkin 		if (c >= children)
109487219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
1095fa9e4066Sahrens 
109687219db7SVictor Latushkin 		if (zopt_objects > 1) {
109787219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
109887219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
109987219db7SVictor Latushkin 
110087219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
110187219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
110287219db7SVictor Latushkin 					dump_metaslab(
110387219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
110487219db7SVictor Latushkin 				else
110587219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
110687219db7SVictor Latushkin 					    "number %llu\n",
110787219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
110887219db7SVictor Latushkin 			}
110987219db7SVictor Latushkin 			(void) printf("\n");
111087219db7SVictor Latushkin 			return;
111187219db7SVictor Latushkin 		}
111287219db7SVictor Latushkin 		children = c + 1;
111387219db7SVictor Latushkin 	}
111487219db7SVictor Latushkin 	for (; c < children; c++) {
111587219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
111687219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
1117fa9e4066Sahrens 
11185cabbc6bSPrashanth Sreenivasa 		print_vdev_indirect(vd);
11195cabbc6bSPrashanth Sreenivasa 
1120fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
1121fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
1122fa9e4066Sahrens 		(void) printf("\n");
1123fa9e4066Sahrens 	}
1124fa9e4066Sahrens }
1125fa9e4066Sahrens 
1126b24ab676SJeff Bonwick static void
1127b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
1128b24ab676SJeff Bonwick {
1129b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
1130b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
11313f7978d0SAlan Somers 	const char *types[4] = { "ditto", "single", "double", "triple" };
1132b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
1133b24ab676SJeff Bonwick 	blkptr_t blk;
1134b24ab676SJeff Bonwick 
1135b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1136b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
1137b24ab676SJeff Bonwick 			continue;
1138bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
113943466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
1140b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
1141b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
1142b24ab676SJeff Bonwick 		    types[p], blkbuf);
1143b24ab676SJeff Bonwick 	}
1144b24ab676SJeff Bonwick }
1145b24ab676SJeff Bonwick 
1146b24ab676SJeff Bonwick static void
1147b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
1148b24ab676SJeff Bonwick {
1149b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
1150b24ab676SJeff Bonwick 
1151b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
1152b24ab676SJeff Bonwick 		return;
1153b24ab676SJeff Bonwick 
1154b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
1155b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
1156b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
1157b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
1158b24ab676SJeff Bonwick 
1159b24ab676SJeff Bonwick 	dedup = rD / D;
1160b24ab676SJeff Bonwick 	compress = rL / rP;
1161b24ab676SJeff Bonwick 	copies = rD / rP;
1162b24ab676SJeff Bonwick 
1163b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
1164b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
1165b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
1166b24ab676SJeff Bonwick }
1167b24ab676SJeff Bonwick 
1168b24ab676SJeff Bonwick static void
1169b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
1170b24ab676SJeff Bonwick {
1171b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
1172b24ab676SJeff Bonwick 	ddt_entry_t dde;
1173b24ab676SJeff Bonwick 	uint64_t walk = 0;
1174b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1175b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
1176b24ab676SJeff Bonwick 	int error;
1177b24ab676SJeff Bonwick 
1178b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
1179b24ab676SJeff Bonwick 
1180b24ab676SJeff Bonwick 	if (error == ENOENT)
1181b24ab676SJeff Bonwick 		return;
1182b24ab676SJeff Bonwick 	ASSERT(error == 0);
1183b24ab676SJeff Bonwick 
11847448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
11857448a079SGeorge Wilson 		return;
11867448a079SGeorge Wilson 
1187b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
1188b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
1189b24ab676SJeff Bonwick 
1190b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
1191b24ab676SJeff Bonwick 
1192b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
1193b24ab676SJeff Bonwick 	    name,
1194b24ab676SJeff Bonwick 	    (u_longlong_t)count,
1195b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
1196b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
1197b24ab676SJeff Bonwick 
1198b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
1199b24ab676SJeff Bonwick 		return;
1200b24ab676SJeff Bonwick 
12019eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
1202b24ab676SJeff Bonwick 
1203b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
1204b24ab676SJeff Bonwick 		return;
1205b24ab676SJeff Bonwick 
1206b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
1207b24ab676SJeff Bonwick 		return;
1208b24ab676SJeff Bonwick 
1209b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
1210b24ab676SJeff Bonwick 
1211bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
1212b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
1213b24ab676SJeff Bonwick 
121417f11284SSerapheim Dimitropoulos 	ASSERT3U(error, ==, ENOENT);
1215b24ab676SJeff Bonwick 
1216b24ab676SJeff Bonwick 	(void) printf("\n");
1217b24ab676SJeff Bonwick }
1218b24ab676SJeff Bonwick 
1219b24ab676SJeff Bonwick static void
1220b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
1221b24ab676SJeff Bonwick {
12223f7978d0SAlan Somers 	ddt_histogram_t ddh_total;
12233f7978d0SAlan Somers 	ddt_stat_t dds_total;
12243f7978d0SAlan Somers 
12253f7978d0SAlan Somers 	bzero(&ddh_total, sizeof (ddh_total));
12263f7978d0SAlan Somers 	bzero(&dds_total, sizeof (dds_total));
1227b24ab676SJeff Bonwick 
1228b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1229b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
1230b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
1231b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
1232b24ab676SJeff Bonwick 			    class++) {
1233b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
1234b24ab676SJeff Bonwick 			}
1235b24ab676SJeff Bonwick 		}
1236b24ab676SJeff Bonwick 	}
1237b24ab676SJeff Bonwick 
12389eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
1239b24ab676SJeff Bonwick 
1240b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
1241b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
1242b24ab676SJeff Bonwick 		return;
1243b24ab676SJeff Bonwick 	}
1244b24ab676SJeff Bonwick 
1245b24ab676SJeff Bonwick 	(void) printf("\n");
1246b24ab676SJeff Bonwick 
1247b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
1248b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
12499eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
12509eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
1251b24ab676SJeff Bonwick 	}
1252b24ab676SJeff Bonwick 
1253b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
1254b24ab676SJeff Bonwick }
1255b24ab676SJeff Bonwick 
12568ad4d6ddSJeff Bonwick static void
12570713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
12588ad4d6ddSJeff Bonwick {
12590713e232SGeorge Wilson 	char *prefix = arg;
12608ad4d6ddSJeff Bonwick 
12618ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
12628ad4d6ddSJeff Bonwick 	    prefix,
12638ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
12648ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
12658ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
12668ad4d6ddSJeff Bonwick }
12678ad4d6ddSJeff Bonwick 
1268fa9e4066Sahrens static void
1269fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
1270fa9e4066Sahrens {
12718ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
12728ad4d6ddSJeff Bonwick 	boolean_t required;
12733f7978d0SAlan Somers 	const char *name[DTL_TYPES] = { "missing", "partial", "scrub",
12743f7978d0SAlan Somers 		"outage" };
12758ad4d6ddSJeff Bonwick 	char prefix[256];
12768ad4d6ddSJeff Bonwick 
12778f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
12788ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
12798ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
1280fa9e4066Sahrens 
1281fa9e4066Sahrens 	if (indent == 0)
1282fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
1283fa9e4066Sahrens 
12848ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
1285e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
12868ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
12878ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
1288fa9e4066Sahrens 
12898ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
12900713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
12910713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
12928ad4d6ddSJeff Bonwick 			continue;
12938ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
12948ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
12950713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
12968ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
12970713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1298fa9e4066Sahrens 	}
1299fa9e4066Sahrens 
13003f7978d0SAlan Somers 	for (unsigned c = 0; c < vd->vdev_children; c++)
1301fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
1302fa9e4066Sahrens }
1303fa9e4066Sahrens 
13048f18d1faSGeorge Wilson static void
13058f18d1faSGeorge Wilson dump_history(spa_t *spa)
13068f18d1faSGeorge Wilson {
13078f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
1308e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
13098f18d1faSGeorge Wilson 	uint_t num = 0;
13108f18d1faSGeorge Wilson 	int error;
13118f18d1faSGeorge Wilson 	time_t tsec;
13128f18d1faSGeorge Wilson 	struct tm t;
13138f18d1faSGeorge Wilson 	char tbuf[30];
13148f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
13158f18d1faSGeorge Wilson 
13163f84190cSMatthew Ahrens 	char *buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
13178f18d1faSGeorge Wilson 	do {
13183f84190cSMatthew Ahrens 		len = SPA_MAXBLOCKSIZE;
1319e4161df6SVictor Latushkin 
13208f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
13218f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
13228f18d1faSGeorge Wilson 			    "error %d\n", error);
13233f84190cSMatthew Ahrens 			umem_free(buf, SPA_MAXBLOCKSIZE);
13248f18d1faSGeorge Wilson 			return;
13258f18d1faSGeorge Wilson 		}
13268f18d1faSGeorge Wilson 
13278f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
13288f18d1faSGeorge Wilson 			break;
13298f18d1faSGeorge Wilson 
13308f18d1faSGeorge Wilson 		off -= resid;
13318f18d1faSGeorge Wilson 	} while (len != 0);
13323f84190cSMatthew Ahrens 	umem_free(buf, SPA_MAXBLOCKSIZE);
13338f18d1faSGeorge Wilson 
13348f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
13353f7978d0SAlan Somers 	for (unsigned i = 0; i < num; i++) {
13368f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
13378f18d1faSGeorge Wilson 		char *cmd, *intstr;
13384445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
13398f18d1faSGeorge Wilson 
13408f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
13418f18d1faSGeorge Wilson 		    &time) != 0)
13424445fffbSMatthew Ahrens 			goto next;
13438f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
13448f18d1faSGeorge Wilson 		    &cmd) != 0) {
13458f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
13468f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
13474445fffbSMatthew Ahrens 				goto next;
13488f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
13498f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
13508f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
13518f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
13524445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
13534445fffbSMatthew Ahrens 				goto next;
13548f18d1faSGeorge Wilson 
13558f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
13568f18d1faSGeorge Wilson 			    sizeof (internalstr),
13573f7978d0SAlan Somers 			    "[internal %s txg:%ju] %s",
13583f7978d0SAlan Somers 			    zfs_history_event_names[ievent], (uintmax_t)txg,
13598f18d1faSGeorge Wilson 			    intstr);
13608f18d1faSGeorge Wilson 			cmd = internalstr;
13618f18d1faSGeorge Wilson 		}
13628f18d1faSGeorge Wilson 		tsec = time;
13638f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
13648f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
13658f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
13664445fffbSMatthew Ahrens 		printed = B_TRUE;
13674445fffbSMatthew Ahrens 
13684445fffbSMatthew Ahrens next:
13694445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
13704445fffbSMatthew Ahrens 			if (!printed)
13714445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
13724445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
13734445fffbSMatthew Ahrens 		}
13748f18d1faSGeorge Wilson 	}
13758f18d1faSGeorge Wilson }
13768f18d1faSGeorge Wilson 
1377fa9e4066Sahrens /*ARGSUSED*/
1378fa9e4066Sahrens static void
1379fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1380fa9e4066Sahrens {
1381fa9e4066Sahrens }
1382fa9e4066Sahrens 
1383fa9e4066Sahrens static uint64_t
13847802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
13857802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1386fa9e4066Sahrens {
1387b24ab676SJeff Bonwick 	if (dnp == NULL) {
1388b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1389b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1390b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1391b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1392b24ab676SJeff Bonwick 	}
1393b24ab676SJeff Bonwick 
1394b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1395fa9e4066Sahrens 
1396b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1397b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1398fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1399fa9e4066Sahrens }
1400fa9e4066Sahrens 
140144cd46caSbillm static void
140243466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
140344cd46caSbillm {
1404cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1405eb633035STom Caputi 	unsigned int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1406b24ab676SJeff Bonwick 
1407490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
140843466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1409b24ab676SJeff Bonwick 		return;
1410b24ab676SJeff Bonwick 	}
141144cd46caSbillm 
14125d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
14135d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
14145d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
14155d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
14165d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
14175d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
14185d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
14195d7b4d43SMatthew Ahrens 		return;
14205d7b4d43SMatthew Ahrens 	}
142144cd46caSbillm 
14225d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1423eb633035STom Caputi 	for (unsigned int i = 0; i < ndvas; i++)
142443466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
142543466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
142644cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
142744cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
142844cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
142944cd46caSbillm 
143043466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
143143466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
143270163ac5SPrakash Surya 		    buflen - strlen(blkbuf),
143370163ac5SPrakash Surya 		    "%llxL B=%llu",
143470163ac5SPrakash Surya 		    (u_longlong_t)BP_GET_LSIZE(bp),
143543466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
143643466aaeSMax Grossman 	} else {
143743466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
143843466aaeSMax Grossman 		    buflen - strlen(blkbuf),
143943466aaeSMax Grossman 		    "%llxL/%llxP F=%llu B=%llu/%llu",
144043466aaeSMax Grossman 		    (u_longlong_t)BP_GET_LSIZE(bp),
144143466aaeSMax Grossman 		    (u_longlong_t)BP_GET_PSIZE(bp),
14425d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
144343466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth,
144443466aaeSMax Grossman 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
144543466aaeSMax Grossman 	}
144644cd46caSbillm }
144744cd46caSbillm 
144888b7b0f2SMatthew Ahrens static void
14497802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
145088b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1451fa9e4066Sahrens {
145288b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1453fa9e4066Sahrens 	int l;
1454fa9e4066Sahrens 
14555d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
14565d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
14575d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
14585d7b4d43SMatthew Ahrens 	}
1459fa9e4066Sahrens 
1460b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1461fa9e4066Sahrens 
1462fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1463fa9e4066Sahrens 
1464fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1465fa9e4066Sahrens 		if (l == zb->zb_level) {
146688b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1467fa9e4066Sahrens 		} else {
146888b7b0f2SMatthew Ahrens 			(void) printf(" ");
1469fa9e4066Sahrens 		}
1470fa9e4066Sahrens 	}
1471fa9e4066Sahrens 
147243466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
147388b7b0f2SMatthew Ahrens 	(void) printf("%s\n", blkbuf);
147488b7b0f2SMatthew Ahrens }
147588b7b0f2SMatthew Ahrens 
147688b7b0f2SMatthew Ahrens static int
147788b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
14787802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
147988b7b0f2SMatthew Ahrens {
1480e4161df6SVictor Latushkin 	int err = 0;
148188b7b0f2SMatthew Ahrens 
148288b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
148388b7b0f2SMatthew Ahrens 		return (0);
148488b7b0f2SMatthew Ahrens 
148588b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
148688b7b0f2SMatthew Ahrens 
148743466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
14887adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
148988b7b0f2SMatthew Ahrens 		int i;
149088b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
149188b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
149288b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
149388b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
149488b7b0f2SMatthew Ahrens 
14951b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
149688b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
149788b7b0f2SMatthew Ahrens 		if (err)
149888b7b0f2SMatthew Ahrens 			return (err);
14993f9d6ad7SLin Ling 		ASSERT(buf->b_data);
150088b7b0f2SMatthew Ahrens 
150188b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
150288b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
150388b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
15047802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
150588b7b0f2SMatthew Ahrens 
150688b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
150788b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
150888b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
150988b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
151088b7b0f2SMatthew Ahrens 			if (err)
151188b7b0f2SMatthew Ahrens 				break;
15125d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
151388b7b0f2SMatthew Ahrens 		}
15148ad4d6ddSJeff Bonwick 		if (!err)
15155d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
1516dcbf3bd6SGeorge Wilson 		arc_buf_destroy(buf, &buf);
1517fa9e4066Sahrens 	}
1518fa9e4066Sahrens 
151988b7b0f2SMatthew Ahrens 	return (err);
1520fa9e4066Sahrens }
1521fa9e4066Sahrens 
1522fa9e4066Sahrens /*ARGSUSED*/
1523fa9e4066Sahrens static void
152488b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1525fa9e4066Sahrens {
152688b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
152788b7b0f2SMatthew Ahrens 	int j;
15287802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1529fa9e4066Sahrens 
1530fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1531fa9e4066Sahrens 
1532503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
153388b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
153488b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
153588b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1536503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
153788b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
153888b7b0f2SMatthew Ahrens 	}
1539fa9e4066Sahrens 
1540fa9e4066Sahrens 	(void) printf("\n");
1541fa9e4066Sahrens }
1542fa9e4066Sahrens 
1543fa9e4066Sahrens /*ARGSUSED*/
1544fa9e4066Sahrens static void
1545fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1546fa9e4066Sahrens {
1547fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1548fa9e4066Sahrens 	time_t crtime;
15493f9d6ad7SLin Ling 	char nice[32];
1550fa9e4066Sahrens 
15510a055120SJason King 	/* make sure nicenum has enough space */
15520a055120SJason King 	CTASSERT(sizeof (nice) >= NN_NUMBUF_SZ);
15530a055120SJason King 
1554fa9e4066Sahrens 	if (dd == NULL)
1555fa9e4066Sahrens 		return;
1556fa9e4066Sahrens 
1557da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1558fa9e4066Sahrens 
1559fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1560fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1561fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1562fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1563fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1564fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
15653cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
15663cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1567fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1568fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
15690a055120SJason King 	zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice));
157074e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
15710a055120SJason King 	zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice));
157274e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
15730a055120SJason King 	zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice));
157474e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
15750a055120SJason King 	zdb_nicenum(dd->dd_quota, nice, sizeof (nice));
157674e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
15770a055120SJason King 	zdb_nicenum(dd->dd_reserved, nice, sizeof (nice));
157874e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1579fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1580fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1581ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1582ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
158374e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
158474e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
158574e7dc98SMatthew Ahrens 
158674e7dc98SMatthew Ahrens #define	DO(which) \
15870a055120SJason King 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \
15880a055120SJason King 	    sizeof (nice)); \
158974e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
159074e7dc98SMatthew Ahrens 	DO(HEAD);
159174e7dc98SMatthew Ahrens 	DO(SNAP);
159274e7dc98SMatthew Ahrens 	DO(CHILD);
159374e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
159474e7dc98SMatthew Ahrens 	DO(REFRSRV);
159574e7dc98SMatthew Ahrens #undef DO
159617fb938fSMatthew Ahrens 	(void) printf("\t\tclones = %llu\n",
159717fb938fSMatthew Ahrens 	    (u_longlong_t)dd->dd_clones);
1598fa9e4066Sahrens }
1599fa9e4066Sahrens 
1600fa9e4066Sahrens /*ARGSUSED*/
1601fa9e4066Sahrens static void
1602fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1603fa9e4066Sahrens {
1604fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1605fa9e4066Sahrens 	time_t crtime;
16063f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1607fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1608fa9e4066Sahrens 
16090a055120SJason King 	/* make sure nicenum has enough space */
16100a055120SJason King 	CTASSERT(sizeof (used) >= NN_NUMBUF_SZ);
16110a055120SJason King 	CTASSERT(sizeof (compressed) >= NN_NUMBUF_SZ);
16120a055120SJason King 	CTASSERT(sizeof (uncompressed) >= NN_NUMBUF_SZ);
16130a055120SJason King 	CTASSERT(sizeof (unique) >= NN_NUMBUF_SZ);
16140a055120SJason King 
1615fa9e4066Sahrens 	if (ds == NULL)
1616fa9e4066Sahrens 		return;
1617fa9e4066Sahrens 
1618fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1619fa9e4066Sahrens 	crtime = ds->ds_creation_time;
16200a055120SJason King 	zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used));
16210a055120SJason King 	zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed));
16220a055120SJason King 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed,
16230a055120SJason King 	    sizeof (uncompressed));
16240a055120SJason King 	zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique));
162543466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1626fa9e4066Sahrens 
1627088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1628fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1629fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1630fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1631fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1632fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1633fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1634fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1635fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1636fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1637fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1638fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1639842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1640842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1641fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1642fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1643fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1644fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1645fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1646fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1647fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1648fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1649fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1650fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1651fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1652fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1653fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
165499653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
165599653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1656088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1657088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1658bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1659bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1660fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1661fa9e4066Sahrens }
1662fa9e4066Sahrens 
1663ad135b5dSChristopher Siden /* ARGSUSED */
1664ad135b5dSChristopher Siden static int
1665ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1666ad135b5dSChristopher Siden {
1667ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1668ad135b5dSChristopher Siden 
1669ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
167043466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1671ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1672ad135b5dSChristopher Siden 	}
1673ad135b5dSChristopher Siden 	return (0);
1674ad135b5dSChristopher Siden }
1675ad135b5dSChristopher Siden 
1676ad135b5dSChristopher Siden static void
16773f7978d0SAlan Somers dump_bptree(objset_t *os, uint64_t obj, const char *name)
1678ad135b5dSChristopher Siden {
1679ad135b5dSChristopher Siden 	char bytes[32];
1680ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1681ad135b5dSChristopher Siden 	dmu_buf_t *db;
1682ad135b5dSChristopher Siden 
16830a055120SJason King 	/* make sure nicenum has enough space */
16840a055120SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
16850a055120SJason King 
1686ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1687ad135b5dSChristopher Siden 		return;
1688ad135b5dSChristopher Siden 
1689b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1690ad135b5dSChristopher Siden 	bt = db->db_data;
16910a055120SJason King 	zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes));
1692ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1693ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1694ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1695ad135b5dSChristopher Siden 
1696ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1697ad135b5dSChristopher Siden 		return;
1698ad135b5dSChristopher Siden 
1699ad135b5dSChristopher Siden 	(void) printf("\n");
1700ad135b5dSChristopher Siden 
1701ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1702ad135b5dSChristopher Siden }
1703ad135b5dSChristopher Siden 
1704cde58dbcSMatthew Ahrens /* ARGSUSED */
1705cde58dbcSMatthew Ahrens static int
1706cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1707cde58dbcSMatthew Ahrens {
1708cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1709cde58dbcSMatthew Ahrens 
1710cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
171143466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1712cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1713cde58dbcSMatthew Ahrens 	return (0);
1714cde58dbcSMatthew Ahrens }
1715cde58dbcSMatthew Ahrens 
1716fa9e4066Sahrens static void
17173f7978d0SAlan Somers dump_full_bpobj(bpobj_t *bpo, const char *name, int indent)
1718fa9e4066Sahrens {
17193f9d6ad7SLin Ling 	char bytes[32];
17203f9d6ad7SLin Ling 	char comp[32];
17213f9d6ad7SLin Ling 	char uncomp[32];
1722fa9e4066Sahrens 
17230a055120SJason King 	/* make sure nicenum has enough space */
17240a055120SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
17250a055120SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
17260a055120SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
17270a055120SJason King 
1728fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1729fa9e4066Sahrens 		return;
1730fa9e4066Sahrens 
17310a055120SJason King 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes));
1732d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
17330a055120SJason King 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp));
17340a055120SJason King 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp));
1735d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1736732885fcSMatthew Ahrens 		    "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1737d0475637SMatthew Ahrens 		    indent * 8, name,
1738d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1739d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1740cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1741732885fcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
174299653d4eSeschrock 		    bytes, comp, uncomp);
1743d0475637SMatthew Ahrens 
1744d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1745d0475637SMatthew Ahrens 			uint64_t subobj;
1746d0475637SMatthew Ahrens 			bpobj_t subbpo;
1747d0475637SMatthew Ahrens 			int error;
1748d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1749d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1750d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1751d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1752d0475637SMatthew Ahrens 			if (error != 0) {
1753d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1754d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1755d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1756d0475637SMatthew Ahrens 				continue;
1757d0475637SMatthew Ahrens 			}
1758732885fcSMatthew Ahrens 			dump_full_bpobj(&subbpo, "subobj", indent + 1);
175977061867SMatthew Ahrens 			bpobj_close(&subbpo);
1760d0475637SMatthew Ahrens 		}
176199653d4eSeschrock 	} else {
1762d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1763d0475637SMatthew Ahrens 		    indent * 8, name,
1764d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1765d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1766d0475637SMatthew Ahrens 		    bytes);
176799653d4eSeschrock 	}
1768fa9e4066Sahrens 
1769cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1770fa9e4066Sahrens 		return;
1771fa9e4066Sahrens 
1772fa9e4066Sahrens 
1773d0475637SMatthew Ahrens 	if (indent == 0) {
1774d0475637SMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1775d0475637SMatthew Ahrens 		(void) printf("\n");
1776d0475637SMatthew Ahrens 	}
1777cde58dbcSMatthew Ahrens }
177844cd46caSbillm 
177917fb938fSMatthew Ahrens static void
178017fb938fSMatthew Ahrens bpobj_count_refd(bpobj_t *bpo)
178117fb938fSMatthew Ahrens {
178217fb938fSMatthew Ahrens 	mos_obj_refd(bpo->bpo_object);
178317fb938fSMatthew Ahrens 
178417fb938fSMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
178517fb938fSMatthew Ahrens 		mos_obj_refd(bpo->bpo_phys->bpo_subobjs);
178617fb938fSMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
178717fb938fSMatthew Ahrens 			uint64_t subobj;
178817fb938fSMatthew Ahrens 			bpobj_t subbpo;
178917fb938fSMatthew Ahrens 			int error;
179017fb938fSMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
179117fb938fSMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
179217fb938fSMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
179317fb938fSMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
179417fb938fSMatthew Ahrens 			if (error != 0) {
179517fb938fSMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
179617fb938fSMatthew Ahrens 				    "subobj id %llu\n",
179717fb938fSMatthew Ahrens 				    error, (u_longlong_t)subobj);
179817fb938fSMatthew Ahrens 				continue;
179917fb938fSMatthew Ahrens 			}
180017fb938fSMatthew Ahrens 			bpobj_count_refd(&subbpo);
180117fb938fSMatthew Ahrens 			bpobj_close(&subbpo);
180217fb938fSMatthew Ahrens 		}
180317fb938fSMatthew Ahrens 	}
180417fb938fSMatthew Ahrens }
180517fb938fSMatthew Ahrens 
1806cde58dbcSMatthew Ahrens static void
1807cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1808cde58dbcSMatthew Ahrens {
1809cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1810d0475637SMatthew Ahrens 	uint64_t unused;
1811cde58dbcSMatthew Ahrens 	char bytes[32];
1812cde58dbcSMatthew Ahrens 	char comp[32];
1813cde58dbcSMatthew Ahrens 	char uncomp[32];
181417fb938fSMatthew Ahrens 	uint64_t empty_bpobj =
181517fb938fSMatthew Ahrens 	    dmu_objset_spa(dl->dl_os)->spa_dsl_pool->dp_empty_bpobj;
181617fb938fSMatthew Ahrens 
181717fb938fSMatthew Ahrens 	/* force the tree to be loaded */
181817fb938fSMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
181917fb938fSMatthew Ahrens 
182017fb938fSMatthew Ahrens 	if (dl->dl_oldfmt) {
182117fb938fSMatthew Ahrens 		if (dl->dl_bpobj.bpo_object != empty_bpobj)
182217fb938fSMatthew Ahrens 			bpobj_count_refd(&dl->dl_bpobj);
182317fb938fSMatthew Ahrens 	} else {
182417fb938fSMatthew Ahrens 		mos_obj_refd(dl->dl_object);
182517fb938fSMatthew Ahrens 		for (dle = avl_first(&dl->dl_tree); dle;
182617fb938fSMatthew Ahrens 		    dle = AVL_NEXT(&dl->dl_tree, dle)) {
182717fb938fSMatthew Ahrens 			if (dle->dle_bpobj.bpo_object != empty_bpobj)
182817fb938fSMatthew Ahrens 				bpobj_count_refd(&dle->dle_bpobj);
182917fb938fSMatthew Ahrens 		}
183017fb938fSMatthew Ahrens 	}
1831cde58dbcSMatthew Ahrens 
18320a055120SJason King 	/* make sure nicenum has enough space */
18330a055120SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
18340a055120SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
18350a055120SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
18360a055120SJason King 
1837cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1838cde58dbcSMatthew Ahrens 		return;
1839cde58dbcSMatthew Ahrens 
184090c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
1841732885fcSMatthew Ahrens 		dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
184290c76c66SMatthew Ahrens 		return;
184390c76c66SMatthew Ahrens 	}
184490c76c66SMatthew Ahrens 
18450a055120SJason King 	zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes));
18460a055120SJason King 	zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp));
18470a055120SJason King 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp));
1848cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1849cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1850cde58dbcSMatthew Ahrens 
1851cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1852cde58dbcSMatthew Ahrens 		return;
1853cde58dbcSMatthew Ahrens 
1854cde58dbcSMatthew Ahrens 	(void) printf("\n");
1855cde58dbcSMatthew Ahrens 
1856cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1857cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1858d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1859d0475637SMatthew Ahrens 			char buf[128];
1860de05b588SRichard Yao 			(void) snprintf(buf, sizeof (buf),
1861de05b588SRichard Yao 			    "mintxg %llu -> obj %llu",
1862d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1863d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1864fa9e4066Sahrens 
1865732885fcSMatthew Ahrens 			dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1866d0475637SMatthew Ahrens 		} else {
1867d0475637SMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1868d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1869d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1870d0475637SMatthew Ahrens 		}
1871cde58dbcSMatthew Ahrens 	}
1872fa9e4066Sahrens }
1873fa9e4066Sahrens 
1874e0d35c44Smarks static avl_tree_t idx_tree;
1875e0d35c44Smarks static avl_tree_t domain_tree;
1876e0d35c44Smarks static boolean_t fuid_table_loaded;
1877ed61ec1dSYuri Pankov static objset_t *sa_os = NULL;
1878ed61ec1dSYuri Pankov static sa_attr_type_t *sa_attr_table = NULL;
1879ed61ec1dSYuri Pankov 
1880ed61ec1dSYuri Pankov static int
1881ed61ec1dSYuri Pankov open_objset(const char *path, dmu_objset_type_t type, void *tag, objset_t **osp)
1882ed61ec1dSYuri Pankov {
1883ed61ec1dSYuri Pankov 	int err;
1884ed61ec1dSYuri Pankov 	uint64_t sa_attrs = 0;
1885ed61ec1dSYuri Pankov 	uint64_t version = 0;
1886ed61ec1dSYuri Pankov 
1887ed61ec1dSYuri Pankov 	VERIFY3P(sa_os, ==, NULL);
1888eb633035STom Caputi 	err = dmu_objset_own(path, type, B_TRUE, B_FALSE, tag, osp);
1889ed61ec1dSYuri Pankov 	if (err != 0) {
1890ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "failed to own dataset '%s': %s\n", path,
1891ed61ec1dSYuri Pankov 		    strerror(err));
1892ed61ec1dSYuri Pankov 		return (err);
1893ed61ec1dSYuri Pankov 	}
1894ed61ec1dSYuri Pankov 
1895eb633035STom Caputi 	if (dmu_objset_type(*osp) == DMU_OST_ZFS && !(*osp)->os_encrypted) {
1896ed61ec1dSYuri Pankov 		(void) zap_lookup(*osp, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1897ed61ec1dSYuri Pankov 		    8, 1, &version);
1898ed61ec1dSYuri Pankov 		if (version >= ZPL_VERSION_SA) {
1899ed61ec1dSYuri Pankov 			(void) zap_lookup(*osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1900ed61ec1dSYuri Pankov 			    8, 1, &sa_attrs);
1901ed61ec1dSYuri Pankov 		}
1902ed61ec1dSYuri Pankov 		err = sa_setup(*osp, sa_attrs, zfs_attr_table, ZPL_END,
1903ed61ec1dSYuri Pankov 		    &sa_attr_table);
1904ed61ec1dSYuri Pankov 		if (err != 0) {
1905ed61ec1dSYuri Pankov 			(void) fprintf(stderr, "sa_setup failed: %s\n",
1906ed61ec1dSYuri Pankov 			    strerror(err));
1907eb633035STom Caputi 			dmu_objset_disown(*osp, B_FALSE, tag);
1908ed61ec1dSYuri Pankov 			*osp = NULL;
1909ed61ec1dSYuri Pankov 		}
1910ed61ec1dSYuri Pankov 	}
1911ed61ec1dSYuri Pankov 	sa_os = *osp;
1912ed61ec1dSYuri Pankov 
1913ed61ec1dSYuri Pankov 	return (0);
1914ed61ec1dSYuri Pankov }
1915ed61ec1dSYuri Pankov 
1916ed61ec1dSYuri Pankov static void
1917ed61ec1dSYuri Pankov close_objset(objset_t *os, void *tag)
1918ed61ec1dSYuri Pankov {
1919ed61ec1dSYuri Pankov 	VERIFY3P(os, ==, sa_os);
1920ed61ec1dSYuri Pankov 	if (os->os_sa != NULL)
1921ed61ec1dSYuri Pankov 		sa_tear_down(os);
1922eb633035STom Caputi 	dmu_objset_disown(os, B_FALSE, tag);
1923ed61ec1dSYuri Pankov 	sa_attr_table = NULL;
1924ed61ec1dSYuri Pankov 	sa_os = NULL;
1925ed61ec1dSYuri Pankov }
1926e0d35c44Smarks 
1927e0d35c44Smarks static void
1928e0d35c44Smarks fuid_table_destroy()
1929e0d35c44Smarks {
1930e0d35c44Smarks 	if (fuid_table_loaded) {
1931e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1932e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1933e0d35c44Smarks 	}
1934e0d35c44Smarks }
1935e0d35c44Smarks 
1936e0d35c44Smarks /*
1937e0d35c44Smarks  * print uid or gid information.
1938e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1939e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1940d0475637SMatthew Ahrens  * the domain-rid string.
1941e0d35c44Smarks  */
1942e0d35c44Smarks static void
1943e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1944e0d35c44Smarks {
1945e0d35c44Smarks 	if (FUID_INDEX(id)) {
1946e0d35c44Smarks 		char *domain;
1947e0d35c44Smarks 
1948e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1949e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1950e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1951e0d35c44Smarks 	} else {
1952e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1953e0d35c44Smarks 	}
1954e0d35c44Smarks 
1955e0d35c44Smarks }
1956e0d35c44Smarks 
1957e0d35c44Smarks static void
19580a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1959e0d35c44Smarks {
1960e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1961e0d35c44Smarks 
19620a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
19630a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1964e0d35c44Smarks 
1965e0d35c44Smarks 	/* Load domain table, if not already loaded */
1966e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1967e0d35c44Smarks 		uint64_t fuid_obj;
1968e0d35c44Smarks 
1969e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1970e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1971e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
197289459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1973e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1974e0d35c44Smarks 		    &idx_tree, &domain_tree);
1975e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1976e0d35c44Smarks 	}
1977e0d35c44Smarks 
19780a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
19790a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1980e0d35c44Smarks }
1981e0d35c44Smarks 
1982fa9e4066Sahrens /*ARGSUSED*/
1983fa9e4066Sahrens static void
1984fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1985fa9e4066Sahrens {
1986fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
19870a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
19880a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
19890a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
19908f2529deSMark Shellenbaum 	uint64_t pflags;
19910a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
19920a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
19938f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
19940a586ceaSMark Shellenbaum 	int idx = 0;
199555434c77Sek 	int error;
1996fa9e4066Sahrens 
1997ed61ec1dSYuri Pankov 	VERIFY3P(os, ==, sa_os);
19980a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
19990a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
20000a586ceaSMark Shellenbaum 		return;
20010a586ceaSMark Shellenbaum 	}
20020a586ceaSMark Shellenbaum 
20030a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
20040a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
20050a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
20060a586ceaSMark Shellenbaum 	    &links, 8);
20070a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
20080a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
20090a586ceaSMark Shellenbaum 	    &mode, 8);
20100a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
20110a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
20120a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
20130a586ceaSMark Shellenbaum 	    &fsize, 8);
20140a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
20150a586ceaSMark Shellenbaum 	    acctm, 16);
20160a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
20170a586ceaSMark Shellenbaum 	    modtm, 16);
20180a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
20190a586ceaSMark Shellenbaum 	    crtm, 16);
20200a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
20210a586ceaSMark Shellenbaum 	    chgtm, 16);
20228f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
20238f2529deSMark Shellenbaum 	    &pflags, 8);
20240a586ceaSMark Shellenbaum 
20250a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
20260a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
20270a586ceaSMark Shellenbaum 		return;
20280a586ceaSMark Shellenbaum 	}
2029fa9e4066Sahrens 
20300a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
20310a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
20320a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
20330a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
2034fa9e4066Sahrens 
2035e548d2faSAlan Somers 	if (dump_opt['d'] > 4) {
2036e548d2faSAlan Somers 		error = zfs_obj_to_path(os, object, path, sizeof (path));
203720b5dafbSPaul Dagnelie 		if (error == ESTALE) {
203820b5dafbSPaul Dagnelie 			(void) snprintf(path, sizeof (path), "on delete queue");
203920b5dafbSPaul Dagnelie 		} else if (error != 0) {
204020b5dafbSPaul Dagnelie 			leaked_objects++;
2041e548d2faSAlan Somers 			(void) snprintf(path, sizeof (path),
204220b5dafbSPaul Dagnelie 			    "path not found, possibly leaked");
2043e548d2faSAlan Somers 		}
2044e548d2faSAlan Somers 		(void) printf("\tpath	%s\n", path);
2045e548d2faSAlan Somers 	}
20460a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
2047fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
2048fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
2049fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
2050fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
20510a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
20520a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
20530a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
20540a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
20550a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
20568f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
2057f67950b2SNasf-Fan 	if (dmu_objset_projectquota_enabled(os) && (pflags & ZFS_PROJID)) {
2058f67950b2SNasf-Fan 		uint64_t projid;
2059f67950b2SNasf-Fan 
2060f67950b2SNasf-Fan 		if (sa_lookup(hdl, sa_attr_table[ZPL_PROJID], &projid,
2061f67950b2SNasf-Fan 		    sizeof (uint64_t)) == 0)
2062f67950b2SNasf-Fan 			(void) printf("\tprojid	%llu\n", (u_longlong_t)projid);
2063f67950b2SNasf-Fan 	}
20640a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
20650a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
20660a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
20670a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
20680a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
20690a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
20700a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
2071fa9e4066Sahrens }
2072fa9e4066Sahrens 
2073fa9e4066Sahrens /*ARGSUSED*/
2074fa9e4066Sahrens static void
2075fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
2076fa9e4066Sahrens {
2077fa9e4066Sahrens }
2078fa9e4066Sahrens 
2079fa9e4066Sahrens /*ARGSUSED*/
2080fa9e4066Sahrens static void
2081fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
2082fa9e4066Sahrens {
2083fa9e4066Sahrens }
2084fa9e4066Sahrens 
2085eb633035STom Caputi 
20866de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
2087fa9e4066Sahrens 	dump_none,		/* unallocated			*/
2088fa9e4066Sahrens 	dump_zap,		/* object directory		*/
2089fa9e4066Sahrens 	dump_uint64,		/* object array			*/
2090fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
2091fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
2092732885fcSMatthew Ahrens 	dump_none,		/* bpobj			*/
2093732885fcSMatthew Ahrens 	dump_bpobj,		/* bpobj header			*/
2094fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
2095fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
2096fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
2097fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
2098fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
2099ea8dc4b6Seschrock 	dump_dsl_dir,		/* DSL directory		*/
2100fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
2101fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
2102fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
2103fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
2104fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
2105da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
2106fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
2107e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
2108fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
2109fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
2110fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
2111fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
2112fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
2113fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
2114fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
2115ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
211606eeb2adSek 	dump_uint8,		/* SPA history			*/
21174445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
2118b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
2119ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
2120da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
2121da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
2122da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
2123da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
2124088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
2125088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
2126f67950b2SNasf-Fan 	dump_zap,		/* ZFS user/group/project used	*/
2127f67950b2SNasf-Fan 	dump_zap,		/* ZFS user/group/project quota	*/
2128842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
2129486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
2130b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
21310a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
21320a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
21330a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
21340a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
21353f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
21363f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
2137cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
2138cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
2139cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
2140732885fcSMatthew Ahrens 	dump_bpobj_subobjs,	/* bpobj subobjs		*/
21410a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
2142fa9e4066Sahrens };
2143fa9e4066Sahrens 
2144fa9e4066Sahrens static void
214554811da5SToomas Soome dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header,
214654811da5SToomas Soome     uint64_t *dnode_slots_used)
2147fa9e4066Sahrens {
2148fa9e4066Sahrens 	dmu_buf_t *db = NULL;
2149fa9e4066Sahrens 	dmu_object_info_t doi;
2150fa9e4066Sahrens 	dnode_t *dn;
2151eb633035STom Caputi 	boolean_t dnode_held = B_FALSE;
2152fa9e4066Sahrens 	void *bonus = NULL;
2153fa9e4066Sahrens 	size_t bsize = 0;
215454811da5SToomas Soome 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32], dnsize[32];
21553f9d6ad7SLin Ling 	char bonus_size[32];
2156fa9e4066Sahrens 	char aux[50];
2157fa9e4066Sahrens 	int error;
2158fa9e4066Sahrens 
21590a055120SJason King 	/* make sure nicenum has enough space */
21600a055120SJason King 	CTASSERT(sizeof (iblk) >= NN_NUMBUF_SZ);
21610a055120SJason King 	CTASSERT(sizeof (dblk) >= NN_NUMBUF_SZ);
21620a055120SJason King 	CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
21630a055120SJason King 	CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
21640a055120SJason King 	CTASSERT(sizeof (bonus_size) >= NN_NUMBUF_SZ);
21650a055120SJason King 
2166fa9e4066Sahrens 	if (*print_header) {
216754811da5SToomas Soome 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %6s  %5s  %6s  %s\n",
216854811da5SToomas Soome 		    "Object", "lvl", "iblk", "dblk", "dsize", "dnsize",
216954811da5SToomas Soome 		    "lsize", "%full", "type");
2170fa9e4066Sahrens 		*print_header = 0;
2171fa9e4066Sahrens 	}
2172fa9e4066Sahrens 
2173fa9e4066Sahrens 	if (object == 0) {
2174744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
2175eb633035STom Caputi 		dmu_object_info_from_dnode(dn, &doi);
2176fa9e4066Sahrens 	} else {
2177eb633035STom Caputi 		/*
2178eb633035STom Caputi 		 * Encrypted datasets will have sensitive bonus buffers
2179eb633035STom Caputi 		 * encrypted. Therefore we cannot hold the bonus buffer and
2180eb633035STom Caputi 		 * must hold the dnode itself instead.
2181eb633035STom Caputi 		 */
2182eb633035STom Caputi 		error = dmu_object_info(os, object, &doi);
2183ea8dc4b6Seschrock 		if (error)
2184eb633035STom Caputi 			fatal("dmu_object_info() failed, errno %u", error);
2185eb633035STom Caputi 
2186eb633035STom Caputi 		if (os->os_encrypted &&
2187eb633035STom Caputi 		    DMU_OT_IS_ENCRYPTED(doi.doi_bonus_type)) {
2188eb633035STom Caputi 			error = dnode_hold(os, object, FTAG, &dn);
2189eb633035STom Caputi 			if (error)
2190eb633035STom Caputi 				fatal("dnode_hold() failed, errno %u", error);
2191eb633035STom Caputi 			dnode_held = B_TRUE;
2192eb633035STom Caputi 		} else {
2193eb633035STom Caputi 			error = dmu_bonus_hold(os, object, FTAG, &db);
2194eb633035STom Caputi 			if (error)
2195eb633035STom Caputi 				fatal("dmu_bonus_hold(%llu) failed, errno %u",
2196eb633035STom Caputi 				    object, error);
2197eb633035STom Caputi 			bonus = db->db_data;
2198eb633035STom Caputi 			bsize = db->db_size;
2199eb633035STom Caputi 			dn = DB_DNODE((dmu_buf_impl_t *)db);
2200eb633035STom Caputi 		}
2201fa9e4066Sahrens 	}
2202fa9e4066Sahrens 
220354811da5SToomas Soome 	if (dnode_slots_used != NULL)
220454811da5SToomas Soome 		*dnode_slots_used = doi.doi_dnodesize / DNODE_MIN_SIZE;
220554811da5SToomas Soome 
22060a055120SJason King 	zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk));
22070a055120SJason King 	zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk));
22080a055120SJason King 	zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize));
22090a055120SJason King 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize));
22100a055120SJason King 	zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size));
221154811da5SToomas Soome 	zdb_nicenum(doi.doi_dnodesize, dnsize, sizeof (dnsize));
2212b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
2213bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
2214bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
2215fa9e4066Sahrens 
2216fa9e4066Sahrens 	aux[0] = '\0';
2217fa9e4066Sahrens 
2218e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
2219fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
22206de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
2221e7437265Sahrens 	}
2222fa9e4066Sahrens 
2223e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
2224fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
22256de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
2226e7437265Sahrens 	}
2227fa9e4066Sahrens 
222854811da5SToomas Soome 	(void) printf("%10" PRIu64
222954811da5SToomas Soome 	    "  %3u  %5s  %5s  %5s  %5s  %5s  %6s  %s%s\n",
223054811da5SToomas Soome 	    object, doi.doi_indirection, iblk, dblk,
223154811da5SToomas Soome 	    asize, dnsize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
2232fa9e4066Sahrens 
2233fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
223454811da5SToomas Soome 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %5s  %6s  %s\n",
223554811da5SToomas Soome 		    "", "", "", "", "", "", bonus_size, "bonus",
22366de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
2237fa9e4066Sahrens 	}
2238fa9e4066Sahrens 
2239fa9e4066Sahrens 	if (verbosity >= 4) {
2240f67950b2SNasf-Fan 		(void) printf("\tdnode flags: %s%s%s%s\n",
224114843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
224214843421SMatthew Ahrens 		    "USED_BYTES " : "",
224314843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
22440a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
2245f67950b2SNasf-Fan 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) ?
2246f67950b2SNasf-Fan 		    "USEROBJUSED_ACCOUNTED " : "",
22470a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
22480a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
224914843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
225014843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
225114843421SMatthew Ahrens 
2252eb633035STom Caputi 		if (!dnode_held) {
2253eb633035STom Caputi 			object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os,
2254eb633035STom Caputi 			    object, bonus, bsize);
2255eb633035STom Caputi 		} else {
2256eb633035STom Caputi 			(void) printf("\t\t(bonus encrypted)\n");
2257eb633035STom Caputi 		}
2258eb633035STom Caputi 
2259eb633035STom Caputi 		if (!os->os_encrypted || !DMU_OT_IS_ENCRYPTED(doi.doi_type)) {
2260eb633035STom Caputi 			object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object,
2261eb633035STom Caputi 			    NULL, 0);
2262eb633035STom Caputi 		} else {
2263eb633035STom Caputi 			(void) printf("\t\t(object encrypted)\n");
2264eb633035STom Caputi 		}
2265eb633035STom Caputi 
2266fa9e4066Sahrens 		*print_header = 1;
2267fa9e4066Sahrens 	}
2268fa9e4066Sahrens 
2269fa9e4066Sahrens 	if (verbosity >= 5)
227088b7b0f2SMatthew Ahrens 		dump_indirect(dn);
2271fa9e4066Sahrens 
2272fa9e4066Sahrens 	if (verbosity >= 5) {
2273fa9e4066Sahrens 		/*
2274fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
2275fa9e4066Sahrens 		 */
2276fa9e4066Sahrens 		uint64_t start = 0;
2277fa9e4066Sahrens 		uint64_t end;
2278fa9e4066Sahrens 		uint64_t blkfill = 1;
2279fa9e4066Sahrens 		int minlvl = 1;
2280fa9e4066Sahrens 
2281fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
2282fa9e4066Sahrens 			minlvl = 0;
2283fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
2284fa9e4066Sahrens 		}
2285fa9e4066Sahrens 
2286fa9e4066Sahrens 		for (;;) {
22873f9d6ad7SLin Ling 			char segsize[32];
22880a055120SJason King 			/* make sure nicenum has enough space */
22890a055120SJason King 			CTASSERT(sizeof (segsize) >= NN_NUMBUF_SZ);
2290cdb0ab79Smaybee 			error = dnode_next_offset(dn,
2291cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
2292fa9e4066Sahrens 			if (error)
2293fa9e4066Sahrens 				break;
2294fa9e4066Sahrens 			end = start;
2295cdb0ab79Smaybee 			error = dnode_next_offset(dn,
2296cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
22970a055120SJason King 			zdb_nicenum(end - start, segsize, sizeof (segsize));
2298fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
2299fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
2300fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
2301fa9e4066Sahrens 			if (error)
2302fa9e4066Sahrens 				break;
2303fa9e4066Sahrens 			start = end;
2304fa9e4066Sahrens 		}
2305fa9e4066Sahrens 	}
2306fa9e4066Sahrens 
2307fa9e4066Sahrens 	if (db != NULL)
2308ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
2309eb633035STom Caputi 	if (dnode_held)
2310eb633035STom Caputi 		dnode_rele(dn, FTAG);
2311fa9e4066Sahrens }
2312fa9e4066Sahrens 
231317fb938fSMatthew Ahrens static void
231417fb938fSMatthew Ahrens count_dir_mos_objects(dsl_dir_t *dd)
231517fb938fSMatthew Ahrens {
231617fb938fSMatthew Ahrens 	mos_obj_refd(dd->dd_object);
231717fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dir_phys(dd)->dd_child_dir_zapobj);
231817fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dir_phys(dd)->dd_deleg_zapobj);
231917fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dir_phys(dd)->dd_props_zapobj);
232017fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dir_phys(dd)->dd_clones);
232117fb938fSMatthew Ahrens }
232217fb938fSMatthew Ahrens 
232317fb938fSMatthew Ahrens static void
232417fb938fSMatthew Ahrens count_ds_mos_objects(dsl_dataset_t *ds)
232517fb938fSMatthew Ahrens {
232617fb938fSMatthew Ahrens 	mos_obj_refd(ds->ds_object);
232717fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dataset_phys(ds)->ds_next_clones_obj);
232817fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dataset_phys(ds)->ds_props_obj);
232917fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dataset_phys(ds)->ds_userrefs_obj);
233017fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dataset_phys(ds)->ds_snapnames_zapobj);
233117fb938fSMatthew Ahrens 
233217fb938fSMatthew Ahrens 	if (!dsl_dataset_is_snapshot(ds)) {
233317fb938fSMatthew Ahrens 		count_dir_mos_objects(ds->ds_dir);
233417fb938fSMatthew Ahrens 	}
233517fb938fSMatthew Ahrens }
233617fb938fSMatthew Ahrens 
23373f7978d0SAlan Somers static const char *objset_types[DMU_OST_NUMTYPES] = {
2338fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
2339fa9e4066Sahrens 
2340fa9e4066Sahrens static void
2341fa9e4066Sahrens dump_dir(objset_t *os)
2342fa9e4066Sahrens {
2343fa9e4066Sahrens 	dmu_objset_stats_t dds;
2344fa9e4066Sahrens 	uint64_t object, object_count;
2345a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
23463f9d6ad7SLin Ling 	char numbuf[32];
234714843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
23489adfa60dSMatthew Ahrens 	char osname[ZFS_MAX_DATASET_NAME_LEN];
23493f7978d0SAlan Somers 	const char *type = "UNKNOWN";
2350fa9e4066Sahrens 	int verbosity = dump_opt['d'];
2351fa9e4066Sahrens 	int print_header = 1;
23523f7978d0SAlan Somers 	unsigned i;
23533f7978d0SAlan Somers 	int error;
235454811da5SToomas Soome 	uint64_t total_slots_used = 0;
235554811da5SToomas Soome 	uint64_t max_slot_used = 0;
235654811da5SToomas Soome 	uint64_t dnode_slots;
2357fa9e4066Sahrens 
23580a055120SJason King 	/* make sure nicenum has enough space */
23590a055120SJason King 	CTASSERT(sizeof (numbuf) >= NN_NUMBUF_SZ);
23600a055120SJason King 
23613b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2362a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
23633b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2364fa9e4066Sahrens 
2365fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
2366fa9e4066Sahrens 		type = objset_types[dds.dds_type];
2367fa9e4066Sahrens 
2368fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
2369fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
23705d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
2371c1379625SJustin T. Gibbs 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
2372c1379625SJustin T. Gibbs 		    dd_used_bytes;
2373a2eea2e1Sahrens 	} else {
2374a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
2375fa9e4066Sahrens 	}
2376fa9e4066Sahrens 
23775d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
2378fa9e4066Sahrens 
23790a055120SJason King 	zdb_nicenum(refdbytes, numbuf, sizeof (numbuf));
2380fa9e4066Sahrens 
2381fa9e4066Sahrens 	if (verbosity >= 4) {
238243466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
238343466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
238443466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
2385fa9e4066Sahrens 	} else {
2386fa9e4066Sahrens 		blkbuf[0] = '\0';
2387fa9e4066Sahrens 	}
2388fa9e4066Sahrens 
2389fa9e4066Sahrens 	dmu_objset_name(os, osname);
2390fa9e4066Sahrens 
2391a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
2392ade2c828SSerapheim Dimitropoulos 	    "%s, %llu objects%s%s\n",
2393fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
2394fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
2395ade2c828SSerapheim Dimitropoulos 	    numbuf, (u_longlong_t)usedobjs, blkbuf,
2396ade2c828SSerapheim Dimitropoulos 	    (dds.dds_inconsistent) ? " (inconsistent)" : "");
2397fa9e4066Sahrens 
2398b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
2399b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
2400b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
240154811da5SToomas Soome 			    &print_header, NULL);
2402b24ab676SJeff Bonwick 		(void) printf("\n");
2403b24ab676SJeff Bonwick 		return;
2404b24ab676SJeff Bonwick 	}
2405b24ab676SJeff Bonwick 
2406b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
2407b24ab676SJeff Bonwick 		dump_intent_log(dmu_objset_zil(os));
2408fa9e4066Sahrens 
24095cabbc6bSPrashanth Sreenivasa 	if (dmu_objset_ds(os) != NULL) {
24105cabbc6bSPrashanth Sreenivasa 		dsl_dataset_t *ds = dmu_objset_ds(os);
24115cabbc6bSPrashanth Sreenivasa 		dump_deadlist(&ds->ds_deadlist);
24125cabbc6bSPrashanth Sreenivasa 
24135cabbc6bSPrashanth Sreenivasa 		if (dsl_dataset_remap_deadlist_exists(ds)) {
24145cabbc6bSPrashanth Sreenivasa 			(void) printf("ds_remap_deadlist:\n");
24155cabbc6bSPrashanth Sreenivasa 			dump_deadlist(&ds->ds_remap_deadlist);
24165cabbc6bSPrashanth Sreenivasa 		}
241717fb938fSMatthew Ahrens 		count_ds_mos_objects(ds);
24185cabbc6bSPrashanth Sreenivasa 	}
2419fa9e4066Sahrens 
2420fa9e4066Sahrens 	if (verbosity < 2)
2421fa9e4066Sahrens 		return;
2422fa9e4066Sahrens 
242343466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
2424088f3894Sahrens 		return;
2425088f3894Sahrens 
242654811da5SToomas Soome 	dump_object(os, 0, verbosity, &print_header, NULL);
242714843421SMatthew Ahrens 	object_count = 0;
2428744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
2429744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
243054811da5SToomas Soome 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header,
243154811da5SToomas Soome 		    NULL);
243254811da5SToomas Soome 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header,
243354811da5SToomas Soome 		    NULL);
243414843421SMatthew Ahrens 	}
2435fa9e4066Sahrens 
2436f67950b2SNasf-Fan 	if (DMU_PROJECTUSED_DNODE(os) != NULL &&
2437f67950b2SNasf-Fan 	    DMU_PROJECTUSED_DNODE(os)->dn_type != 0)
2438f67950b2SNasf-Fan 		dump_object(os, DMU_PROJECTUSED_OBJECT, verbosity,
2439f67950b2SNasf-Fan 		    &print_header, NULL);
2440f67950b2SNasf-Fan 
2441fa9e4066Sahrens 	object = 0;
24426754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
244354811da5SToomas Soome 		dump_object(os, object, verbosity, &print_header, &dnode_slots);
2444fa9e4066Sahrens 		object_count++;
244554811da5SToomas Soome 		total_slots_used += dnode_slots;
244654811da5SToomas Soome 		max_slot_used = object + dnode_slots - 1;
2447fa9e4066Sahrens 	}
2448fa9e4066Sahrens 
2449fa9e4066Sahrens 	(void) printf("\n");
2450fa9e4066Sahrens 
245154811da5SToomas Soome 	(void) printf("    Dnode slots:\n");
245254811da5SToomas Soome 	(void) printf("\tTotal used:    %10llu\n",
245354811da5SToomas Soome 	    (u_longlong_t)total_slots_used);
245454811da5SToomas Soome 	(void) printf("\tMax used:      %10llu\n",
245554811da5SToomas Soome 	    (u_longlong_t)max_slot_used);
245654811da5SToomas Soome 	(void) printf("\tPercent empty: %10lf\n",
245754811da5SToomas Soome 	    (double)(max_slot_used - total_slots_used)*100 /
245854811da5SToomas Soome 	    (double)max_slot_used);
245954811da5SToomas Soome 
246054811da5SToomas Soome 	(void) printf("\n");
246154811da5SToomas Soome 
2462ccba0801SRich Morris 	if (error != ESRCH) {
2463ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2464ccba0801SRich Morris 		abort();
2465ccba0801SRich Morris 	}
246620b5dafbSPaul Dagnelie 	if (leaked_objects != 0) {
246720b5dafbSPaul Dagnelie 		(void) printf("%d potentially leaked objects detected\n",
246820b5dafbSPaul Dagnelie 		    leaked_objects);
246920b5dafbSPaul Dagnelie 		leaked_objects = 0;
247020b5dafbSPaul Dagnelie 	}
2471a3874b8bSToomas Soome 
2472a3874b8bSToomas Soome 	ASSERT3U(object_count, ==, usedobjs);
2473fa9e4066Sahrens }
2474fa9e4066Sahrens 
2475fa9e4066Sahrens static void
247653b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2477fa9e4066Sahrens {
2478fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
2479fa9e4066Sahrens 
24803f7978d0SAlan Somers 	(void) printf("%s", header ? header : "");
2481fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2482fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2483fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2484fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2485fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
2486fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
2487e0f1c0afSOlaf Faaland 
2488e0f1c0afSOlaf Faaland 	(void) printf("\tmmp_magic = %016llx\n",
2489e0f1c0afSOlaf Faaland 	    (u_longlong_t)ub->ub_mmp_magic);
2490e0f1c0afSOlaf Faaland 	if (ub->ub_mmp_magic == MMP_MAGIC)
2491e0f1c0afSOlaf Faaland 		(void) printf("\tmmp_delay = %0llu\n",
2492e0f1c0afSOlaf Faaland 		    (u_longlong_t)ub->ub_mmp_delay);
2493e0f1c0afSOlaf Faaland 
2494fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
2495fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
249643466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2497fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
2498fa9e4066Sahrens 	}
249986714001SSerapheim Dimitropoulos 	(void) printf("\tcheckpoint_txg = %llu\n",
250086714001SSerapheim Dimitropoulos 	    (u_longlong_t)ub->ub_checkpoint_txg);
25013f7978d0SAlan Somers 	(void) printf("%s", footer ? footer : "");
2502fa9e4066Sahrens }
2503fa9e4066Sahrens 
2504fa9e4066Sahrens static void
250507428bdfSVictor Latushkin dump_config(spa_t *spa)
2506fa9e4066Sahrens {
250707428bdfSVictor Latushkin 	dmu_buf_t *db;
250807428bdfSVictor Latushkin 	size_t nvsize = 0;
250907428bdfSVictor Latushkin 	int error = 0;
251007428bdfSVictor Latushkin 
2511fa9e4066Sahrens 
251207428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
251307428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
251407428bdfSVictor Latushkin 
251507428bdfSVictor Latushkin 	if (error == 0) {
251607428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
251707428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
251807428bdfSVictor Latushkin 
251907428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
252007428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
252107428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
252207428bdfSVictor Latushkin 	} else {
252307428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
252407428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
2525fa9e4066Sahrens 	}
2526fa9e4066Sahrens }
2527fa9e4066Sahrens 
2528c5904d13Seschrock static void
2529c5904d13Seschrock dump_cachefile(const char *cachefile)
2530c5904d13Seschrock {
2531c5904d13Seschrock 	int fd;
2532c5904d13Seschrock 	struct stat64 statbuf;
2533c5904d13Seschrock 	char *buf;
2534c5904d13Seschrock 	nvlist_t *config;
2535c5904d13Seschrock 
2536c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2537c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
2538c5904d13Seschrock 		    strerror(errno));
2539c5904d13Seschrock 		exit(1);
2540c5904d13Seschrock 	}
2541c5904d13Seschrock 
2542c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
2543c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
2544c5904d13Seschrock 		    strerror(errno));
2545c5904d13Seschrock 		exit(1);
2546c5904d13Seschrock 	}
2547c5904d13Seschrock 
2548c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
2549c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
2550c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2551c5904d13Seschrock 		exit(1);
2552c5904d13Seschrock 	}
2553c5904d13Seschrock 
2554c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2555c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2556c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2557c5904d13Seschrock 		exit(1);
2558c5904d13Seschrock 	}
2559c5904d13Seschrock 
2560c5904d13Seschrock 	(void) close(fd);
2561c5904d13Seschrock 
2562c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2563c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2564c5904d13Seschrock 		exit(1);
2565c5904d13Seschrock 	}
2566c5904d13Seschrock 
2567c5904d13Seschrock 	free(buf);
2568c5904d13Seschrock 
2569c5904d13Seschrock 	dump_nvlist(config, 0);
2570c5904d13Seschrock 
2571c5904d13Seschrock 	nvlist_free(config);
2572c5904d13Seschrock }
2573c5904d13Seschrock 
257453b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
257553b9a4a9SVictor Latushkin 
257653b9a4a9SVictor Latushkin static void
257753b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
257853b9a4a9SVictor Latushkin {
257953b9a4a9SVictor Latushkin 	vdev_t vd;
258053b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
258153b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
258253b9a4a9SVictor Latushkin 
258353b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
258453b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
258553b9a4a9SVictor Latushkin 
258653b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
258753b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
258853b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
258953b9a4a9SVictor Latushkin 
259053b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
259153b9a4a9SVictor Latushkin 			continue;
2592e0f1c0afSOlaf Faaland 
2593e0f1c0afSOlaf Faaland 		if ((dump_opt['u'] < 4) &&
2594e0f1c0afSOlaf Faaland 		    (ub->ub_mmp_magic == MMP_MAGIC) && ub->ub_mmp_delay &&
2595e0f1c0afSOlaf Faaland 		    (i >= VDEV_UBERBLOCK_COUNT(&vd) - MMP_BLOCKS_PER_LABEL))
2596e0f1c0afSOlaf Faaland 			continue;
2597e0f1c0afSOlaf Faaland 
259853b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
259953b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
260053b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
260153b9a4a9SVictor Latushkin 	}
260253b9a4a9SVictor Latushkin }
260353b9a4a9SVictor Latushkin 
2604ed61ec1dSYuri Pankov static char curpath[PATH_MAX];
2605ed61ec1dSYuri Pankov 
2606ed61ec1dSYuri Pankov /*
2607ed61ec1dSYuri Pankov  * Iterate through the path components, recursively passing
2608ed61ec1dSYuri Pankov  * current one's obj and remaining path until we find the obj
2609ed61ec1dSYuri Pankov  * for the last one.
2610ed61ec1dSYuri Pankov  */
2611ed61ec1dSYuri Pankov static int
2612ed61ec1dSYuri Pankov dump_path_impl(objset_t *os, uint64_t obj, char *name)
2613ed61ec1dSYuri Pankov {
2614ed61ec1dSYuri Pankov 	int err;
2615ed61ec1dSYuri Pankov 	int header = 1;
2616ed61ec1dSYuri Pankov 	uint64_t child_obj;
2617ed61ec1dSYuri Pankov 	char *s;
2618ed61ec1dSYuri Pankov 	dmu_buf_t *db;
2619ed61ec1dSYuri Pankov 	dmu_object_info_t doi;
2620ed61ec1dSYuri Pankov 
2621ed61ec1dSYuri Pankov 	if ((s = strchr(name, '/')) != NULL)
2622ed61ec1dSYuri Pankov 		*s = '\0';
2623ed61ec1dSYuri Pankov 	err = zap_lookup(os, obj, name, 8, 1, &child_obj);
2624ed61ec1dSYuri Pankov 
2625ed61ec1dSYuri Pankov 	(void) strlcat(curpath, name, sizeof (curpath));
2626ed61ec1dSYuri Pankov 
2627ed61ec1dSYuri Pankov 	if (err != 0) {
2628ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "failed to lookup %s: %s\n",
2629ed61ec1dSYuri Pankov 		    curpath, strerror(err));
2630ed61ec1dSYuri Pankov 		return (err);
2631ed61ec1dSYuri Pankov 	}
2632ed61ec1dSYuri Pankov 
2633ed61ec1dSYuri Pankov 	child_obj = ZFS_DIRENT_OBJ(child_obj);
2634ed61ec1dSYuri Pankov 	err = sa_buf_hold(os, child_obj, FTAG, &db);
2635ed61ec1dSYuri Pankov 	if (err != 0) {
2636ed61ec1dSYuri Pankov 		(void) fprintf(stderr,
2637ed61ec1dSYuri Pankov 		    "failed to get SA dbuf for obj %llu: %s\n",
2638ed61ec1dSYuri Pankov 		    (u_longlong_t)child_obj, strerror(err));
2639ed61ec1dSYuri Pankov 		return (EINVAL);
2640ed61ec1dSYuri Pankov 	}
2641ed61ec1dSYuri Pankov 	dmu_object_info_from_db(db, &doi);
2642ed61ec1dSYuri Pankov 	sa_buf_rele(db, FTAG);
2643ed61ec1dSYuri Pankov 
2644ed61ec1dSYuri Pankov 	if (doi.doi_bonus_type != DMU_OT_SA &&
2645ed61ec1dSYuri Pankov 	    doi.doi_bonus_type != DMU_OT_ZNODE) {
2646ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "invalid bonus type %d for obj %llu\n",
2647ed61ec1dSYuri Pankov 		    doi.doi_bonus_type, (u_longlong_t)child_obj);
2648ed61ec1dSYuri Pankov 		return (EINVAL);
2649ed61ec1dSYuri Pankov 	}
2650ed61ec1dSYuri Pankov 
2651ed61ec1dSYuri Pankov 	if (dump_opt['v'] > 6) {
2652ed61ec1dSYuri Pankov 		(void) printf("obj=%llu %s type=%d bonustype=%d\n",
2653ed61ec1dSYuri Pankov 		    (u_longlong_t)child_obj, curpath, doi.doi_type,
2654ed61ec1dSYuri Pankov 		    doi.doi_bonus_type);
2655ed61ec1dSYuri Pankov 	}
2656ed61ec1dSYuri Pankov 
2657ed61ec1dSYuri Pankov 	(void) strlcat(curpath, "/", sizeof (curpath));
2658ed61ec1dSYuri Pankov 
2659ed61ec1dSYuri Pankov 	switch (doi.doi_type) {
2660ed61ec1dSYuri Pankov 	case DMU_OT_DIRECTORY_CONTENTS:
2661ed61ec1dSYuri Pankov 		if (s != NULL && *(s + 1) != '\0')
2662ed61ec1dSYuri Pankov 			return (dump_path_impl(os, child_obj, s + 1));
2663ed61ec1dSYuri Pankov 		/*FALLTHROUGH*/
2664ed61ec1dSYuri Pankov 	case DMU_OT_PLAIN_FILE_CONTENTS:
266554811da5SToomas Soome 		dump_object(os, child_obj, dump_opt['v'], &header, NULL);
2666ed61ec1dSYuri Pankov 		return (0);
2667ed61ec1dSYuri Pankov 	default:
2668ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "object %llu has non-file/directory "
2669ed61ec1dSYuri Pankov 		    "type %d\n", (u_longlong_t)obj, doi.doi_type);
2670ed61ec1dSYuri Pankov 		break;
2671ed61ec1dSYuri Pankov 	}
2672ed61ec1dSYuri Pankov 
2673ed61ec1dSYuri Pankov 	return (EINVAL);
2674ed61ec1dSYuri Pankov }
2675ed61ec1dSYuri Pankov 
2676ed61ec1dSYuri Pankov /*
2677ed61ec1dSYuri Pankov  * Dump the blocks for the object specified by path inside the dataset.
2678ed61ec1dSYuri Pankov  */
2679ed61ec1dSYuri Pankov static int
2680ed61ec1dSYuri Pankov dump_path(char *ds, char *path)
2681ed61ec1dSYuri Pankov {
2682ed61ec1dSYuri Pankov 	int err;
2683ed61ec1dSYuri Pankov 	objset_t *os;
2684ed61ec1dSYuri Pankov 	uint64_t root_obj;
2685ed61ec1dSYuri Pankov 
2686ed61ec1dSYuri Pankov 	err = open_objset(ds, DMU_OST_ZFS, FTAG, &os);
2687ed61ec1dSYuri Pankov 	if (err != 0)
2688ed61ec1dSYuri Pankov 		return (err);
2689ed61ec1dSYuri Pankov 
2690ed61ec1dSYuri Pankov 	err = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &root_obj);
2691ed61ec1dSYuri Pankov 	if (err != 0) {
2692ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "can't lookup root znode: %s\n",
2693ed61ec1dSYuri Pankov 		    strerror(err));
2694eb633035STom Caputi 		dmu_objset_disown(os, B_FALSE, FTAG);
2695ed61ec1dSYuri Pankov 		return (EINVAL);
2696ed61ec1dSYuri Pankov 	}
2697ed61ec1dSYuri Pankov 
2698ed61ec1dSYuri Pankov 	(void) snprintf(curpath, sizeof (curpath), "dataset=%s path=/", ds);
2699ed61ec1dSYuri Pankov 
2700ed61ec1dSYuri Pankov 	err = dump_path_impl(os, root_obj, path);
2701ed61ec1dSYuri Pankov 
2702ed61ec1dSYuri Pankov 	close_objset(os, FTAG);
2703ed61ec1dSYuri Pankov 	return (err);
2704ed61ec1dSYuri Pankov }
2705ed61ec1dSYuri Pankov 
270664723e36SYuri Pankov static int
2707fa9e4066Sahrens dump_label(const char *dev)
2708fa9e4066Sahrens {
2709fa9e4066Sahrens 	int fd;
2710fa9e4066Sahrens 	vdev_label_t label;
271164723e36SYuri Pankov 	char path[MAXPATHLEN];
271264723e36SYuri Pankov 	char *buf = label.vl_vdev_phys.vp_nvlist;
2713fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2714fa9e4066Sahrens 	struct stat64 statbuf;
271553b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
271664723e36SYuri Pankov 	boolean_t label_found = B_FALSE;
271764723e36SYuri Pankov 
271864723e36SYuri Pankov 	(void) strlcpy(path, dev, sizeof (path));
271964723e36SYuri Pankov 	if (dev[0] == '/') {
272064723e36SYuri Pankov 		if (strncmp(dev, ZFS_DISK_ROOTD,
272164723e36SYuri Pankov 		    strlen(ZFS_DISK_ROOTD)) == 0) {
272264723e36SYuri Pankov 			(void) snprintf(path, sizeof (path), "%s%s",
272364723e36SYuri Pankov 			    ZFS_RDISK_ROOTD, dev + strlen(ZFS_DISK_ROOTD));
272464723e36SYuri Pankov 		}
272564723e36SYuri Pankov 	} else if (stat64(path, &statbuf) != 0) {
272664723e36SYuri Pankov 		char *s;
2727c6065d0fSGeorge Wilson 
272864723e36SYuri Pankov 		(void) snprintf(path, sizeof (path), "%s%s", ZFS_RDISK_ROOTD,
272964723e36SYuri Pankov 		    dev);
273022c8b958SYuri Pankov 		if (((s = strrchr(dev, 's')) == NULL &&
273122c8b958SYuri Pankov 		    (s = strchr(dev, 'p')) == NULL) ||
273222c8b958SYuri Pankov 		    !isdigit(*(s + 1)))
273364723e36SYuri Pankov 			(void) strlcat(path, "s0", sizeof (path));
2734fa9e4066Sahrens 	}
2735fa9e4066Sahrens 
273622c8b958SYuri Pankov 	if ((fd = open64(path, O_RDONLY)) < 0) {
273722c8b958SYuri Pankov 		(void) fprintf(stderr, "cannot open '%s': %s\n", path,
2738fa9e4066Sahrens 		    strerror(errno));
2739c6065d0fSGeorge Wilson 		exit(1);
2740c6065d0fSGeorge Wilson 	}
2741c6065d0fSGeorge Wilson 
274222c8b958SYuri Pankov 	if (fstat64(fd, &statbuf) != 0) {
274322c8b958SYuri Pankov 		(void) fprintf(stderr, "failed to stat '%s': %s\n", path,
274422c8b958SYuri Pankov 		    strerror(errno));
274522c8b958SYuri Pankov 		(void) close(fd);
274664723e36SYuri Pankov 		exit(1);
274764723e36SYuri Pankov 	}
274864723e36SYuri Pankov 
274922c8b958SYuri Pankov 	if (S_ISBLK(statbuf.st_mode)) {
275022c8b958SYuri Pankov 		(void) fprintf(stderr,
275122c8b958SYuri Pankov 		    "cannot use '%s': character device required\n", path);
275222c8b958SYuri Pankov 		(void) close(fd);
2753c6065d0fSGeorge Wilson 		exit(1);
2754fa9e4066Sahrens 	}
2755fa9e4066Sahrens 
2756fa9e4066Sahrens 	psize = statbuf.st_size;
2757fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2758fa9e4066Sahrens 
275953b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
2760fa9e4066Sahrens 		nvlist_t *config = NULL;
2761fa9e4066Sahrens 
276264723e36SYuri Pankov 		if (!dump_opt['q']) {
276364723e36SYuri Pankov 			(void) printf("------------------------------------\n");
276464723e36SYuri Pankov 			(void) printf("LABEL %d\n", l);
276564723e36SYuri Pankov 			(void) printf("------------------------------------\n");
276664723e36SYuri Pankov 		}
2767fa9e4066Sahrens 
27680d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
2769fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
277064723e36SYuri Pankov 			if (!dump_opt['q'])
277164723e36SYuri Pankov 				(void) printf("failed to read label %d\n", l);
2772fa9e4066Sahrens 			continue;
2773fa9e4066Sahrens 		}
2774fa9e4066Sahrens 
2775fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
277664723e36SYuri Pankov 			if (!dump_opt['q'])
277764723e36SYuri Pankov 				(void) printf("failed to unpack label %d\n", l);
277853b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
277953b9a4a9SVictor Latushkin 		} else {
278053b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
278153b9a4a9SVictor Latushkin 
278264723e36SYuri Pankov 			if (!dump_opt['q'])
278364723e36SYuri Pankov 				dump_nvlist(config, 4);
278453b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
278553b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
278653b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
278753b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
278853b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
278953b9a4a9SVictor Latushkin 			nvlist_free(config);
279064723e36SYuri Pankov 			label_found = B_TRUE;
2791fa9e4066Sahrens 		}
279253b9a4a9SVictor Latushkin 		if (dump_opt['u'])
279353b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
2794fa9e4066Sahrens 	}
2795c6065d0fSGeorge Wilson 
2796c6065d0fSGeorge Wilson 	(void) close(fd);
279764723e36SYuri Pankov 
279864723e36SYuri Pankov 	return (label_found ? 0 : 2);
2799fa9e4066Sahrens }
2800fa9e4066Sahrens 
2801ca0cc391SMatthew Ahrens static uint64_t dataset_feature_count[SPA_FEATURES];
28025cabbc6bSPrashanth Sreenivasa static uint64_t remap_deadlist_count = 0;
2803b5152584SMatthew Ahrens 
2804fa9e4066Sahrens /*ARGSUSED*/
28051d452cf5Sahrens static int
2806fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2807fa9e4066Sahrens {
2808fa9e4066Sahrens 	int error;
2809fa9e4066Sahrens 	objset_t *os;
2810fa9e4066Sahrens 
2811ed61ec1dSYuri Pankov 	error = open_objset(dsname, DMU_OST_ANY, FTAG, &os);
2812ed61ec1dSYuri Pankov 	if (error != 0)
28131d452cf5Sahrens 		return (0);
2814ca0cc391SMatthew Ahrens 
2815ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2816ca0cc391SMatthew Ahrens 		if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2817ca0cc391SMatthew Ahrens 			continue;
2818ca0cc391SMatthew Ahrens 		ASSERT(spa_feature_table[f].fi_flags &
2819ca0cc391SMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET);
2820ca0cc391SMatthew Ahrens 		dataset_feature_count[f]++;
2821ca0cc391SMatthew Ahrens 	}
2822ca0cc391SMatthew Ahrens 
28235cabbc6bSPrashanth Sreenivasa 	if (dsl_dataset_remap_deadlist_exists(dmu_objset_ds(os))) {
28245cabbc6bSPrashanth Sreenivasa 		remap_deadlist_count++;
28255cabbc6bSPrashanth Sreenivasa 	}
28265cabbc6bSPrashanth Sreenivasa 
2827fa9e4066Sahrens 	dump_dir(os);
2828ed61ec1dSYuri Pankov 	close_objset(os, FTAG);
2829e0d35c44Smarks 	fuid_table_destroy();
28301d452cf5Sahrens 	return (0);
2831fa9e4066Sahrens }
2832fa9e4066Sahrens 
2833fa9e4066Sahrens /*
2834b24ab676SJeff Bonwick  * Block statistics.
2835fa9e4066Sahrens  */
2836b5152584SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2837fa9e4066Sahrens typedef struct zdb_blkstats {
2838490d05b9SMatthew Ahrens 	uint64_t zb_asize;
2839490d05b9SMatthew Ahrens 	uint64_t zb_lsize;
2840490d05b9SMatthew Ahrens 	uint64_t zb_psize;
2841490d05b9SMatthew Ahrens 	uint64_t zb_count;
2842d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2843d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2844663207adSDon Brady 	uint64_t zb_ditto_same_ms;
2845490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2846fa9e4066Sahrens } zdb_blkstats_t;
2847fa9e4066Sahrens 
2848b24ab676SJeff Bonwick /*
2849b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2850b24ab676SJeff Bonwick  */
2851b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2852b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2853ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2854ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2855b24ab676SJeff Bonwick 
28563f7978d0SAlan Somers static const char *zdb_ot_extname[] = {
2857b24ab676SJeff Bonwick 	"deferred free",
2858b24ab676SJeff Bonwick 	"dedup ditto",
2859ad135b5dSChristopher Siden 	"other",
2860b24ab676SJeff Bonwick 	"Total",
2861b24ab676SJeff Bonwick };
2862fa9e4066Sahrens 
286388b7b0f2SMatthew Ahrens #define	ZB_TOTAL	DN_MAX_LEVELS
2864fa9e4066Sahrens 
2865fa9e4066Sahrens typedef struct zdb_cb {
2866b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
28675cabbc6bSPrashanth Sreenivasa 	uint64_t	zcb_removing_size;
286886714001SSerapheim Dimitropoulos 	uint64_t	zcb_checkpoint_size;
2869b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2870b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
28715d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
28725d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
28735d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2874490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
28753f7978d0SAlan Somers 	hrtime_t	zcb_lastprint;
2876490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2877fa9e4066Sahrens 	uint64_t	zcb_errors[256];
2878fa9e4066Sahrens 	int		zcb_readfails;
2879fa9e4066Sahrens 	int		zcb_haderrors;
2880cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
28815cabbc6bSPrashanth Sreenivasa 	uint32_t	**zcb_vd_obsolete_counts;
2882fa9e4066Sahrens } zdb_cb_t;
2883fa9e4066Sahrens 
2884663207adSDon Brady /* test if two DVA offsets from same vdev are within the same metaslab */
2885663207adSDon Brady static boolean_t
2886663207adSDon Brady same_metaslab(spa_t *spa, uint64_t vdev, uint64_t off1, uint64_t off2)
2887663207adSDon Brady {
2888663207adSDon Brady 	vdev_t *vd = vdev_lookup_top(spa, vdev);
2889663207adSDon Brady 	uint64_t ms_shift = vd->vdev_ms_shift;
2890663207adSDon Brady 
2891663207adSDon Brady 	return ((off1 >> ms_shift) == (off2 >> ms_shift));
2892663207adSDon Brady }
2893663207adSDon Brady 
2894fa9e4066Sahrens static void
2895cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2896b24ab676SJeff Bonwick     dmu_object_type_t type)
2897fa9e4066Sahrens {
2898b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2899b24ab676SJeff Bonwick 
2900b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2901b24ab676SJeff Bonwick 
2902b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2903b24ab676SJeff Bonwick 		return;
2904b24ab676SJeff Bonwick 
2905663207adSDon Brady 	spa_config_enter(zcb->zcb_spa, SCL_CONFIG, FTAG, RW_READER);
2906663207adSDon Brady 
2907e14bb325SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2908fa9e4066Sahrens 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2909b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2910d5ee8a13SMatthew Ahrens 		int equal;
2911fa9e4066Sahrens 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2912fa9e4066Sahrens 
2913fa9e4066Sahrens 		zb->zb_asize += BP_GET_ASIZE(bp);
2914fa9e4066Sahrens 		zb->zb_lsize += BP_GET_LSIZE(bp);
2915fa9e4066Sahrens 		zb->zb_psize += BP_GET_PSIZE(bp);
2916fa9e4066Sahrens 		zb->zb_count++;
2917b5152584SMatthew Ahrens 
2918b5152584SMatthew Ahrens 		/*
2919b5152584SMatthew Ahrens 		 * The histogram is only big enough to record blocks up to
2920b5152584SMatthew Ahrens 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2921b5152584SMatthew Ahrens 		 * "other", bucket.
2922b5152584SMatthew Ahrens 		 */
29233f7978d0SAlan Somers 		unsigned idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2924b5152584SMatthew Ahrens 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2925b5152584SMatthew Ahrens 		zb->zb_psize_histogram[idx]++;
2926d5ee8a13SMatthew Ahrens 
2927d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2928d5ee8a13SMatthew Ahrens 
2929d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2930d5ee8a13SMatthew Ahrens 		case 2:
2931d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2932663207adSDon Brady 			    DVA_GET_VDEV(&bp->blk_dva[1])) {
2933d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2934663207adSDon Brady 
2935663207adSDon Brady 				if (same_metaslab(zcb->zcb_spa,
2936663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[0]),
2937663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[0]),
2938663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[1])))
2939663207adSDon Brady 					zb->zb_ditto_same_ms++;
2940663207adSDon Brady 			}
2941d5ee8a13SMatthew Ahrens 			break;
2942d5ee8a13SMatthew Ahrens 		case 3:
2943d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2944d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2945d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2946d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2947d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2948d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2949663207adSDon Brady 			if (equal != 0) {
2950d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2951663207adSDon Brady 
2952663207adSDon Brady 				if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2953663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[1]) &&
2954663207adSDon Brady 				    same_metaslab(zcb->zcb_spa,
2955663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[0]),
2956663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[0]),
2957663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[1])))
2958663207adSDon Brady 					zb->zb_ditto_same_ms++;
2959663207adSDon Brady 				else if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2960663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[2]) &&
2961663207adSDon Brady 				    same_metaslab(zcb->zcb_spa,
2962663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[0]),
2963663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[0]),
2964663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[2])))
2965663207adSDon Brady 					zb->zb_ditto_same_ms++;
2966663207adSDon Brady 				else if (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2967663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[2]) &&
2968663207adSDon Brady 				    same_metaslab(zcb->zcb_spa,
2969663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[1]),
2970663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[1]),
2971663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[2])))
2972663207adSDon Brady 					zb->zb_ditto_same_ms++;
2973663207adSDon Brady 			}
2974d5ee8a13SMatthew Ahrens 			break;
2975d5ee8a13SMatthew Ahrens 		}
2976fa9e4066Sahrens 	}
2977fa9e4066Sahrens 
2978663207adSDon Brady 	spa_config_exit(zcb->zcb_spa, SCL_CONFIG, FTAG);
2979663207adSDon Brady 
29805d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
29815d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
29825d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
29835d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
29845d7b4d43SMatthew Ahrens 		return;
29855d7b4d43SMatthew Ahrens 	}
29865d7b4d43SMatthew Ahrens 
2987b24ab676SJeff Bonwick 	if (dump_opt['L'])
2988b24ab676SJeff Bonwick 		return;
2989b24ab676SJeff Bonwick 
2990b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2991b24ab676SJeff Bonwick 		ddt_t *ddt;
2992b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2993b24ab676SJeff Bonwick 
2994cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2995b24ab676SJeff Bonwick 		ddt_enter(ddt);
2996b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2997b24ab676SJeff Bonwick 
2998b24ab676SJeff Bonwick 		if (dde == NULL) {
2999b24ab676SJeff Bonwick 			refcnt = 0;
3000b24ab676SJeff Bonwick 		} else {
3001b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
3002b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
3003b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
3004b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
3005b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
3006d41e7643Sek 		}
3007b24ab676SJeff Bonwick 		ddt_exit(ddt);
3008d41e7643Sek 	}
3009d41e7643Sek 
3010cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
301186714001SSerapheim Dimitropoulos 	    refcnt ? 0 : spa_min_claim_txg(zcb->zcb_spa),
3012b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
3013fa9e4066Sahrens }
3014fa9e4066Sahrens 
301531d7e8faSGeorge Wilson static void
301631d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
301731d7e8faSGeorge Wilson {
301831d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
301931d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
302031d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
302131d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
30227802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
302331d7e8faSGeorge Wilson 
3024770499e1SDan Kimmel 	abd_free(zio->io_abd);
302531d7e8faSGeorge Wilson 
302631d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
3027a3874b8bSToomas Soome 	spa->spa_load_verify_ios--;
302831d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
302931d7e8faSGeorge Wilson 
303031d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
303131d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
303231d7e8faSGeorge Wilson 
303331d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
303431d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
303531d7e8faSGeorge Wilson 
303631d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
303743466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
303831d7e8faSGeorge Wilson 		else
303931d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
304031d7e8faSGeorge Wilson 
304131d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
304231d7e8faSGeorge Wilson 		    "Got error %d reading "
304331d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
304431d7e8faSGeorge Wilson 		    ioerr,
304531d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
304631d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
304731d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
304831d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
304931d7e8faSGeorge Wilson 		    blkbuf);
305031d7e8faSGeorge Wilson 	}
305131d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
305231d7e8faSGeorge Wilson }
305331d7e8faSGeorge Wilson 
3054fa9e4066Sahrens static int
30551b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
30567802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
3057fa9e4066Sahrens {
3058fa9e4066Sahrens 	zdb_cb_t *zcb = arg;
30596365109dSVictor Latushkin 	dmu_object_type_t type;
3060468c413aSTim Haley 	boolean_t is_metadata;
3061fa9e4066Sahrens 
3062a2cdcdd2SPaul Dagnelie 	if (bp == NULL)
3063a2cdcdd2SPaul Dagnelie 		return (0);
3064a2cdcdd2SPaul Dagnelie 
306543466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
306643466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
306743466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
306843466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
306943466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
307043466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
307143466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
307243466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
307343466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
307443466aaeSMax Grossman 		    blkbuf);
307543466aaeSMax Grossman 	}
307643466aaeSMax Grossman 
307743466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
307888b7b0f2SMatthew Ahrens 		return (0);
3079e14bb325SJeff Bonwick 
30806365109dSVictor Latushkin 	type = BP_GET_TYPE(bp);
3081e14bb325SJeff Bonwick 
3082ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
3083ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
30846365109dSVictor Latushkin 
3085ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
30866365109dSVictor Latushkin 
30875d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
30885d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
3089468c413aSTim Haley 		size_t size = BP_GET_PSIZE(bp);
3090770499e1SDan Kimmel 		abd_t *abd = abd_alloc(size, B_FALSE);
3091b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
3092b24ab676SJeff Bonwick 
3093b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
3094b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
3095b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
3096b24ab676SJeff Bonwick 
309731d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
3098a3874b8bSToomas Soome 		while (spa->spa_load_verify_ios > max_inflight)
309931d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
3100a3874b8bSToomas Soome 		spa->spa_load_verify_ios++;
310131d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
3102b24ab676SJeff Bonwick 
3103770499e1SDan Kimmel 		zio_nowait(zio_read(NULL, spa, bp, abd, size,
310431d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
3105fa9e4066Sahrens 	}
3106fa9e4066Sahrens 
3107fa9e4066Sahrens 	zcb->zcb_readfails = 0;
3108fa9e4066Sahrens 
3109f7950bf1SMatthew Ahrens 	/* only call gethrtime() every 100 blocks */
3110f7950bf1SMatthew Ahrens 	static int iters;
3111f7950bf1SMatthew Ahrens 	if (++iters > 100)
3112f7950bf1SMatthew Ahrens 		iters = 0;
3113f7950bf1SMatthew Ahrens 	else
3114f7950bf1SMatthew Ahrens 		return (0);
3115f7950bf1SMatthew Ahrens 
3116f7950bf1SMatthew Ahrens 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
3117490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
3118490d05b9SMatthew Ahrens 		char buf[10];
3119490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
3120490d05b9SMatthew Ahrens 		int kb_per_sec =
3121490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
3122490d05b9SMatthew Ahrens 		int sec_remaining =
3123490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
3124490d05b9SMatthew Ahrens 
31250a055120SJason King 		/* make sure nicenum has enough space */
31260a055120SJason King 		CTASSERT(sizeof (buf) >= NN_NUMBUF_SZ);
31270a055120SJason King 
3128490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
3129490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
3130490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
3131490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
3132490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
3133490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
3134490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
3135490d05b9SMatthew Ahrens 		    sec_remaining % 60);
3136490d05b9SMatthew Ahrens 
3137490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
3138490d05b9SMatthew Ahrens 	}
3139490d05b9SMatthew Ahrens 
3140fa9e4066Sahrens 	return (0);
3141fa9e4066Sahrens }
3142fa9e4066Sahrens 
3143b24ab676SJeff Bonwick static void
31440713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
3145b24ab676SJeff Bonwick {
31460713e232SGeorge Wilson 	vdev_t *vd = arg;
3147b24ab676SJeff Bonwick 
3148b24ab676SJeff Bonwick 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
3149b24ab676SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
3150b24ab676SJeff Bonwick }
3151b24ab676SJeff Bonwick 
31520713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
31532e4c9986SGeorge Wilson 	NULL	/* alloc */
3154b24ab676SJeff Bonwick };
3155b24ab676SJeff Bonwick 
3156b24ab676SJeff Bonwick static void
3157bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
3158b24ab676SJeff Bonwick {
31593f7978d0SAlan Somers 	ddt_bookmark_t ddb;
3160b24ab676SJeff Bonwick 	ddt_entry_t dde;
3161b24ab676SJeff Bonwick 	int error;
3162b24ab676SJeff Bonwick 
3163555d674dSSerapheim Dimitropoulos 	ASSERT(!dump_opt['L']);
3164555d674dSSerapheim Dimitropoulos 
31653f7978d0SAlan Somers 	bzero(&ddb, sizeof (ddb));
3166bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
3167b24ab676SJeff Bonwick 		blkptr_t blk;
3168b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
3169bbfd46c4SJeff Bonwick 
3170bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
3171bbfd46c4SJeff Bonwick 			return;
3172bbfd46c4SJeff Bonwick 
3173b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
3174bbfd46c4SJeff Bonwick 
3175b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
3176b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
3177b24ab676SJeff Bonwick 				continue;
3178bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
3179bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
3180b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
3181cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
3182b24ab676SJeff Bonwick 			} else {
3183b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
3184b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
3185b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
3186b24ab676SJeff Bonwick 			}
3187b24ab676SJeff Bonwick 		}
3188555d674dSSerapheim Dimitropoulos 		ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
3189555d674dSSerapheim Dimitropoulos 		ddt_enter(ddt);
3190555d674dSSerapheim Dimitropoulos 		VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
3191555d674dSSerapheim Dimitropoulos 		ddt_exit(ddt);
3192b24ab676SJeff Bonwick 	}
3193b24ab676SJeff Bonwick 
3194b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
3195b24ab676SJeff Bonwick }
3196b24ab676SJeff Bonwick 
31975cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
31985cabbc6bSPrashanth Sreenivasa static void
31995cabbc6bSPrashanth Sreenivasa claim_segment_impl_cb(uint64_t inner_offset, vdev_t *vd, uint64_t offset,
32005cabbc6bSPrashanth Sreenivasa     uint64_t size, void *arg)
32015cabbc6bSPrashanth Sreenivasa {
32025cabbc6bSPrashanth Sreenivasa 	/*
32035cabbc6bSPrashanth Sreenivasa 	 * This callback was called through a remap from
32045cabbc6bSPrashanth Sreenivasa 	 * a device being removed. Therefore, the vdev that
32055cabbc6bSPrashanth Sreenivasa 	 * this callback is applied to is a concrete
32065cabbc6bSPrashanth Sreenivasa 	 * vdev.
32075cabbc6bSPrashanth Sreenivasa 	 */
32085cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
32095cabbc6bSPrashanth Sreenivasa 
32105cabbc6bSPrashanth Sreenivasa 	VERIFY0(metaslab_claim_impl(vd, offset, size,
321186714001SSerapheim Dimitropoulos 	    spa_min_claim_txg(vd->vdev_spa)));
32125cabbc6bSPrashanth Sreenivasa }
32135cabbc6bSPrashanth Sreenivasa 
32145cabbc6bSPrashanth Sreenivasa static void
32155cabbc6bSPrashanth Sreenivasa claim_segment_cb(void *arg, uint64_t offset, uint64_t size)
32165cabbc6bSPrashanth Sreenivasa {
32175cabbc6bSPrashanth Sreenivasa 	vdev_t *vd = arg;
32185cabbc6bSPrashanth Sreenivasa 
32195cabbc6bSPrashanth Sreenivasa 	vdev_indirect_ops.vdev_op_remap(vd, offset, size,
32205cabbc6bSPrashanth Sreenivasa 	    claim_segment_impl_cb, NULL);
32215cabbc6bSPrashanth Sreenivasa }
32225cabbc6bSPrashanth Sreenivasa 
32235cabbc6bSPrashanth Sreenivasa /*
32245cabbc6bSPrashanth Sreenivasa  * After accounting for all allocated blocks that are directly referenced,
32255cabbc6bSPrashanth Sreenivasa  * we might have missed a reference to a block from a partially complete
32265cabbc6bSPrashanth Sreenivasa  * (and thus unused) indirect mapping object. We perform a secondary pass
32275cabbc6bSPrashanth Sreenivasa  * through the metaslabs we have already mapped and claim the destination
32285cabbc6bSPrashanth Sreenivasa  * blocks.
32295cabbc6bSPrashanth Sreenivasa  */
32305cabbc6bSPrashanth Sreenivasa static void
32315cabbc6bSPrashanth Sreenivasa zdb_claim_removing(spa_t *spa, zdb_cb_t *zcb)
32325cabbc6bSPrashanth Sreenivasa {
3233555d674dSSerapheim Dimitropoulos 	if (dump_opt['L'])
3234555d674dSSerapheim Dimitropoulos 		return;
3235555d674dSSerapheim Dimitropoulos 
32365cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal == NULL)
32375cabbc6bSPrashanth Sreenivasa 		return;
32385cabbc6bSPrashanth Sreenivasa 
32395cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
32405cabbc6bSPrashanth Sreenivasa 
32415cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
32423a4b1be9SMatthew Ahrens 	vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
32435cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
32445cabbc6bSPrashanth Sreenivasa 
32455cabbc6bSPrashanth Sreenivasa 	for (uint64_t msi = 0; msi < vd->vdev_ms_count; msi++) {
32465cabbc6bSPrashanth Sreenivasa 		metaslab_t *msp = vd->vdev_ms[msi];
32475cabbc6bSPrashanth Sreenivasa 
32485cabbc6bSPrashanth Sreenivasa 		if (msp->ms_start >= vdev_indirect_mapping_max_offset(vim))
32495cabbc6bSPrashanth Sreenivasa 			break;
32505cabbc6bSPrashanth Sreenivasa 
32515cabbc6bSPrashanth Sreenivasa 		ASSERT0(range_tree_space(svr->svr_allocd_segs));
32525cabbc6bSPrashanth Sreenivasa 
32535cabbc6bSPrashanth Sreenivasa 		if (msp->ms_sm != NULL) {
32545cabbc6bSPrashanth Sreenivasa 			VERIFY0(space_map_load(msp->ms_sm,
32555cabbc6bSPrashanth Sreenivasa 			    svr->svr_allocd_segs, SM_ALLOC));
32565cabbc6bSPrashanth Sreenivasa 
32575cabbc6bSPrashanth Sreenivasa 			/*
32583a4b1be9SMatthew Ahrens 			 * Clear everything past what has been synced unless
32593a4b1be9SMatthew Ahrens 			 * it's past the spacemap, because we have not allocated
32603a4b1be9SMatthew Ahrens 			 * mappings for it yet.
32615cabbc6bSPrashanth Sreenivasa 			 */
32623a4b1be9SMatthew Ahrens 			uint64_t vim_max_offset =
32633a4b1be9SMatthew Ahrens 			    vdev_indirect_mapping_max_offset(vim);
32643a4b1be9SMatthew Ahrens 			uint64_t sm_end = msp->ms_sm->sm_start +
32653a4b1be9SMatthew Ahrens 			    msp->ms_sm->sm_size;
32663a4b1be9SMatthew Ahrens 			if (sm_end > vim_max_offset)
32673a4b1be9SMatthew Ahrens 				range_tree_clear(svr->svr_allocd_segs,
32683a4b1be9SMatthew Ahrens 				    vim_max_offset, sm_end - vim_max_offset);
32695cabbc6bSPrashanth Sreenivasa 		}
32705cabbc6bSPrashanth Sreenivasa 
32715cabbc6bSPrashanth Sreenivasa 		zcb->zcb_removing_size +=
32725cabbc6bSPrashanth Sreenivasa 		    range_tree_space(svr->svr_allocd_segs);
32735cabbc6bSPrashanth Sreenivasa 		range_tree_vacate(svr->svr_allocd_segs, claim_segment_cb, vd);
32745cabbc6bSPrashanth Sreenivasa 	}
32755cabbc6bSPrashanth Sreenivasa 
32765cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_CONFIG, FTAG);
32775cabbc6bSPrashanth Sreenivasa }
32785cabbc6bSPrashanth Sreenivasa 
32795cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
32805cabbc6bSPrashanth Sreenivasa static int
32815cabbc6bSPrashanth Sreenivasa increment_indirect_mapping_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
32825cabbc6bSPrashanth Sreenivasa {
32835cabbc6bSPrashanth Sreenivasa 	zdb_cb_t *zcb = arg;
32845cabbc6bSPrashanth Sreenivasa 	spa_t *spa = zcb->zcb_spa;
32855cabbc6bSPrashanth Sreenivasa 	vdev_t *vd;
32865cabbc6bSPrashanth Sreenivasa 	const dva_t *dva = &bp->blk_dva[0];
32875cabbc6bSPrashanth Sreenivasa 
32885cabbc6bSPrashanth Sreenivasa 	ASSERT(!dump_opt['L']);
32895cabbc6bSPrashanth Sreenivasa 	ASSERT3U(BP_GET_NDVAS(bp), ==, 1);
32905cabbc6bSPrashanth Sreenivasa 
32915cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
32925cabbc6bSPrashanth Sreenivasa 	vd = vdev_lookup_top(zcb->zcb_spa, DVA_GET_VDEV(dva));
32935cabbc6bSPrashanth Sreenivasa 	ASSERT3P(vd, !=, NULL);
32945cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_VDEV, FTAG);
32955cabbc6bSPrashanth Sreenivasa 
32965cabbc6bSPrashanth Sreenivasa 	ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0);
32975cabbc6bSPrashanth Sreenivasa 	ASSERT3P(zcb->zcb_vd_obsolete_counts[vd->vdev_id], !=, NULL);
32985cabbc6bSPrashanth Sreenivasa 
32995cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_increment_obsolete_count(
33005cabbc6bSPrashanth Sreenivasa 	    vd->vdev_indirect_mapping,
33015cabbc6bSPrashanth Sreenivasa 	    DVA_GET_OFFSET(dva), DVA_GET_ASIZE(dva),
33025cabbc6bSPrashanth Sreenivasa 	    zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
33035cabbc6bSPrashanth Sreenivasa 
33045cabbc6bSPrashanth Sreenivasa 	return (0);
33055cabbc6bSPrashanth Sreenivasa }
33065cabbc6bSPrashanth Sreenivasa 
33075cabbc6bSPrashanth Sreenivasa static uint32_t *
33085cabbc6bSPrashanth Sreenivasa zdb_load_obsolete_counts(vdev_t *vd)
33095cabbc6bSPrashanth Sreenivasa {
33105cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
33115cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
33125cabbc6bSPrashanth Sreenivasa 	spa_condensing_indirect_phys_t *scip =
33135cabbc6bSPrashanth Sreenivasa 	    &spa->spa_condensing_indirect_phys;
33145cabbc6bSPrashanth Sreenivasa 	uint32_t *counts;
33155cabbc6bSPrashanth Sreenivasa 
33165cabbc6bSPrashanth Sreenivasa 	EQUIV(vdev_obsolete_sm_object(vd) != 0, vd->vdev_obsolete_sm != NULL);
33175cabbc6bSPrashanth Sreenivasa 	counts = vdev_indirect_mapping_load_obsolete_counts(vim);
33185cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_obsolete_sm != NULL) {
33195cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
33205cabbc6bSPrashanth Sreenivasa 		    vd->vdev_obsolete_sm);
33215cabbc6bSPrashanth Sreenivasa 	}
33225cabbc6bSPrashanth Sreenivasa 	if (scip->scip_vdev == vd->vdev_id &&
33235cabbc6bSPrashanth Sreenivasa 	    scip->scip_prev_obsolete_sm_object != 0) {
33245cabbc6bSPrashanth Sreenivasa 		space_map_t *prev_obsolete_sm = NULL;
33255cabbc6bSPrashanth Sreenivasa 		VERIFY0(space_map_open(&prev_obsolete_sm, spa->spa_meta_objset,
33265cabbc6bSPrashanth Sreenivasa 		    scip->scip_prev_obsolete_sm_object, 0, vd->vdev_asize, 0));
33275cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
33285cabbc6bSPrashanth Sreenivasa 		    prev_obsolete_sm);
33295cabbc6bSPrashanth Sreenivasa 		space_map_close(prev_obsolete_sm);
33305cabbc6bSPrashanth Sreenivasa 	}
33315cabbc6bSPrashanth Sreenivasa 	return (counts);
33325cabbc6bSPrashanth Sreenivasa }
33335cabbc6bSPrashanth Sreenivasa 
333486714001SSerapheim Dimitropoulos typedef struct checkpoint_sm_exclude_entry_arg {
333586714001SSerapheim Dimitropoulos 	vdev_t *cseea_vd;
333686714001SSerapheim Dimitropoulos 	uint64_t cseea_checkpoint_size;
333786714001SSerapheim Dimitropoulos } checkpoint_sm_exclude_entry_arg_t;
333886714001SSerapheim Dimitropoulos 
333986714001SSerapheim Dimitropoulos static int
334017f11284SSerapheim Dimitropoulos checkpoint_sm_exclude_entry_cb(space_map_entry_t *sme, void *arg)
334186714001SSerapheim Dimitropoulos {
334286714001SSerapheim Dimitropoulos 	checkpoint_sm_exclude_entry_arg_t *cseea = arg;
334386714001SSerapheim Dimitropoulos 	vdev_t *vd = cseea->cseea_vd;
334417f11284SSerapheim Dimitropoulos 	metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift];
334517f11284SSerapheim Dimitropoulos 	uint64_t end = sme->sme_offset + sme->sme_run;
334686714001SSerapheim Dimitropoulos 
334717f11284SSerapheim Dimitropoulos 	ASSERT(sme->sme_type == SM_FREE);
334886714001SSerapheim Dimitropoulos 
334986714001SSerapheim Dimitropoulos 	/*
335086714001SSerapheim Dimitropoulos 	 * Since the vdev_checkpoint_sm exists in the vdev level
335186714001SSerapheim Dimitropoulos 	 * and the ms_sm space maps exist in the metaslab level,
335286714001SSerapheim Dimitropoulos 	 * an entry in the checkpoint space map could theoretically
335386714001SSerapheim Dimitropoulos 	 * cross the boundaries of the metaslab that it belongs.
335486714001SSerapheim Dimitropoulos 	 *
335586714001SSerapheim Dimitropoulos 	 * In reality, because of the way that we populate and
335686714001SSerapheim Dimitropoulos 	 * manipulate the checkpoint's space maps currently,
335786714001SSerapheim Dimitropoulos 	 * there shouldn't be any entries that cross metaslabs.
335886714001SSerapheim Dimitropoulos 	 * Hence the assertion below.
335986714001SSerapheim Dimitropoulos 	 *
336086714001SSerapheim Dimitropoulos 	 * That said, there is no fundamental requirement that
336186714001SSerapheim Dimitropoulos 	 * the checkpoint's space map entries should not cross
336286714001SSerapheim Dimitropoulos 	 * metaslab boundaries. So if needed we could add code
336386714001SSerapheim Dimitropoulos 	 * that handles metaslab-crossing segments in the future.
336486714001SSerapheim Dimitropoulos 	 */
336517f11284SSerapheim Dimitropoulos 	VERIFY3U(sme->sme_offset, >=, ms->ms_start);
336686714001SSerapheim Dimitropoulos 	VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
336786714001SSerapheim Dimitropoulos 
336886714001SSerapheim Dimitropoulos 	/*
336986714001SSerapheim Dimitropoulos 	 * By removing the entry from the allocated segments we
337086714001SSerapheim Dimitropoulos 	 * also verify that the entry is there to begin with.
337186714001SSerapheim Dimitropoulos 	 */
337286714001SSerapheim Dimitropoulos 	mutex_enter(&ms->ms_lock);
337317f11284SSerapheim Dimitropoulos 	range_tree_remove(ms->ms_allocatable, sme->sme_offset, sme->sme_run);
337486714001SSerapheim Dimitropoulos 	mutex_exit(&ms->ms_lock);
337586714001SSerapheim Dimitropoulos 
337617f11284SSerapheim Dimitropoulos 	cseea->cseea_checkpoint_size += sme->sme_run;
337786714001SSerapheim Dimitropoulos 	return (0);
337886714001SSerapheim Dimitropoulos }
337986714001SSerapheim Dimitropoulos 
338086714001SSerapheim Dimitropoulos static void
338186714001SSerapheim Dimitropoulos zdb_leak_init_vdev_exclude_checkpoint(vdev_t *vd, zdb_cb_t *zcb)
338286714001SSerapheim Dimitropoulos {
338386714001SSerapheim Dimitropoulos 	spa_t *spa = vd->vdev_spa;
338486714001SSerapheim Dimitropoulos 	space_map_t *checkpoint_sm = NULL;
338586714001SSerapheim Dimitropoulos 	uint64_t checkpoint_sm_obj;
338686714001SSerapheim Dimitropoulos 
338786714001SSerapheim Dimitropoulos 	/*
338886714001SSerapheim Dimitropoulos 	 * If there is no vdev_top_zap, we are in a pool whose
338986714001SSerapheim Dimitropoulos 	 * version predates the pool checkpoint feature.
339086714001SSerapheim Dimitropoulos 	 */
339186714001SSerapheim Dimitropoulos 	if (vd->vdev_top_zap == 0)
339286714001SSerapheim Dimitropoulos 		return;
339386714001SSerapheim Dimitropoulos 
339486714001SSerapheim Dimitropoulos 	/*
339586714001SSerapheim Dimitropoulos 	 * If there is no reference of the vdev_checkpoint_sm in
339686714001SSerapheim Dimitropoulos 	 * the vdev_top_zap, then one of the following scenarios
339786714001SSerapheim Dimitropoulos 	 * is true:
339886714001SSerapheim Dimitropoulos 	 *
339986714001SSerapheim Dimitropoulos 	 * 1] There is no checkpoint
340086714001SSerapheim Dimitropoulos 	 * 2] There is a checkpoint, but no checkpointed blocks
340186714001SSerapheim Dimitropoulos 	 *    have been freed yet
340286714001SSerapheim Dimitropoulos 	 * 3] The current vdev is indirect
340386714001SSerapheim Dimitropoulos 	 *
340486714001SSerapheim Dimitropoulos 	 * In these cases we return immediately.
340586714001SSerapheim Dimitropoulos 	 */
340686714001SSerapheim Dimitropoulos 	if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
340786714001SSerapheim Dimitropoulos 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
340886714001SSerapheim Dimitropoulos 		return;
340986714001SSerapheim Dimitropoulos 
341086714001SSerapheim Dimitropoulos 	VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
341186714001SSerapheim Dimitropoulos 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1,
341286714001SSerapheim Dimitropoulos 	    &checkpoint_sm_obj));
341386714001SSerapheim Dimitropoulos 
341486714001SSerapheim Dimitropoulos 	checkpoint_sm_exclude_entry_arg_t cseea;
341586714001SSerapheim Dimitropoulos 	cseea.cseea_vd = vd;
341686714001SSerapheim Dimitropoulos 	cseea.cseea_checkpoint_size = 0;
341786714001SSerapheim Dimitropoulos 
341886714001SSerapheim Dimitropoulos 	VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
341986714001SSerapheim Dimitropoulos 	    checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
342086714001SSerapheim Dimitropoulos 
342186714001SSerapheim Dimitropoulos 	VERIFY0(space_map_iterate(checkpoint_sm,
3422555d674dSSerapheim Dimitropoulos 	    space_map_length(checkpoint_sm),
342386714001SSerapheim Dimitropoulos 	    checkpoint_sm_exclude_entry_cb, &cseea));
342486714001SSerapheim Dimitropoulos 	space_map_close(checkpoint_sm);
342586714001SSerapheim Dimitropoulos 
342686714001SSerapheim Dimitropoulos 	zcb->zcb_checkpoint_size += cseea.cseea_checkpoint_size;
342786714001SSerapheim Dimitropoulos }
342886714001SSerapheim Dimitropoulos 
342986714001SSerapheim Dimitropoulos static void
343086714001SSerapheim Dimitropoulos zdb_leak_init_exclude_checkpoint(spa_t *spa, zdb_cb_t *zcb)
343186714001SSerapheim Dimitropoulos {
3432555d674dSSerapheim Dimitropoulos 	ASSERT(!dump_opt['L']);
3433555d674dSSerapheim Dimitropoulos 
343486714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
343586714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
343686714001SSerapheim Dimitropoulos 		ASSERT3U(c, ==, rvd->vdev_child[c]->vdev_id);
343786714001SSerapheim Dimitropoulos 		zdb_leak_init_vdev_exclude_checkpoint(rvd->vdev_child[c], zcb);
343886714001SSerapheim Dimitropoulos 	}
343986714001SSerapheim Dimitropoulos }
344086714001SSerapheim Dimitropoulos 
344186714001SSerapheim Dimitropoulos static void
344286714001SSerapheim Dimitropoulos load_concrete_ms_allocatable_trees(spa_t *spa, maptype_t maptype)
344386714001SSerapheim Dimitropoulos {
344486714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
344586714001SSerapheim Dimitropoulos 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
344686714001SSerapheim Dimitropoulos 		vdev_t *vd = rvd->vdev_child[i];
344786714001SSerapheim Dimitropoulos 
344886714001SSerapheim Dimitropoulos 		ASSERT3U(i, ==, vd->vdev_id);
344986714001SSerapheim Dimitropoulos 
345086714001SSerapheim Dimitropoulos 		if (vd->vdev_ops == &vdev_indirect_ops)
345186714001SSerapheim Dimitropoulos 			continue;
345286714001SSerapheim Dimitropoulos 
345386714001SSerapheim Dimitropoulos 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
345486714001SSerapheim Dimitropoulos 			metaslab_t *msp = vd->vdev_ms[m];
345586714001SSerapheim Dimitropoulos 
345686714001SSerapheim Dimitropoulos 			(void) fprintf(stderr,
345786714001SSerapheim Dimitropoulos 			    "\rloading concrete vdev %llu, "
345886714001SSerapheim Dimitropoulos 			    "metaslab %llu of %llu ...",
345986714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_id,
346086714001SSerapheim Dimitropoulos 			    (longlong_t)msp->ms_id,
346186714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_ms_count);
346286714001SSerapheim Dimitropoulos 
346386714001SSerapheim Dimitropoulos 			mutex_enter(&msp->ms_lock);
346486714001SSerapheim Dimitropoulos 			metaslab_unload(msp);
346586714001SSerapheim Dimitropoulos 
346686714001SSerapheim Dimitropoulos 			/*
346786714001SSerapheim Dimitropoulos 			 * We don't want to spend the CPU manipulating the
346886714001SSerapheim Dimitropoulos 			 * size-ordered tree, so clear the range_tree ops.
346986714001SSerapheim Dimitropoulos 			 */
347086714001SSerapheim Dimitropoulos 			msp->ms_allocatable->rt_ops = NULL;
347186714001SSerapheim Dimitropoulos 
347286714001SSerapheim Dimitropoulos 			if (msp->ms_sm != NULL) {
347386714001SSerapheim Dimitropoulos 				VERIFY0(space_map_load(msp->ms_sm,
347486714001SSerapheim Dimitropoulos 				    msp->ms_allocatable, maptype));
347586714001SSerapheim Dimitropoulos 			}
347686714001SSerapheim Dimitropoulos 			if (!msp->ms_loaded)
347786714001SSerapheim Dimitropoulos 				msp->ms_loaded = B_TRUE;
347886714001SSerapheim Dimitropoulos 			mutex_exit(&msp->ms_lock);
347986714001SSerapheim Dimitropoulos 		}
348086714001SSerapheim Dimitropoulos 	}
348186714001SSerapheim Dimitropoulos }
348286714001SSerapheim Dimitropoulos 
348386714001SSerapheim Dimitropoulos /*
348486714001SSerapheim Dimitropoulos  * vm_idxp is an in-out parameter which (for indirect vdevs) is the
348586714001SSerapheim Dimitropoulos  * index in vim_entries that has the first entry in this metaslab.
348686714001SSerapheim Dimitropoulos  * On return, it will be set to the first entry after this metaslab.
348786714001SSerapheim Dimitropoulos  */
348886714001SSerapheim Dimitropoulos static void
348986714001SSerapheim Dimitropoulos load_indirect_ms_allocatable_tree(vdev_t *vd, metaslab_t *msp,
349086714001SSerapheim Dimitropoulos     uint64_t *vim_idxp)
349186714001SSerapheim Dimitropoulos {
349286714001SSerapheim Dimitropoulos 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
349386714001SSerapheim Dimitropoulos 
349486714001SSerapheim Dimitropoulos 	mutex_enter(&msp->ms_lock);
349586714001SSerapheim Dimitropoulos 	metaslab_unload(msp);
349686714001SSerapheim Dimitropoulos 
349786714001SSerapheim Dimitropoulos 	/*
349886714001SSerapheim Dimitropoulos 	 * We don't want to spend the CPU manipulating the
349986714001SSerapheim Dimitropoulos 	 * size-ordered tree, so clear the range_tree ops.
350086714001SSerapheim Dimitropoulos 	 */
350186714001SSerapheim Dimitropoulos 	msp->ms_allocatable->rt_ops = NULL;
350286714001SSerapheim Dimitropoulos 
350386714001SSerapheim Dimitropoulos 	for (; *vim_idxp < vdev_indirect_mapping_num_entries(vim);
350486714001SSerapheim Dimitropoulos 	    (*vim_idxp)++) {
350586714001SSerapheim Dimitropoulos 		vdev_indirect_mapping_entry_phys_t *vimep =
350686714001SSerapheim Dimitropoulos 		    &vim->vim_entries[*vim_idxp];
350786714001SSerapheim Dimitropoulos 		uint64_t ent_offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
350886714001SSerapheim Dimitropoulos 		uint64_t ent_len = DVA_GET_ASIZE(&vimep->vimep_dst);
350986714001SSerapheim Dimitropoulos 		ASSERT3U(ent_offset, >=, msp->ms_start);
351086714001SSerapheim Dimitropoulos 		if (ent_offset >= msp->ms_start + msp->ms_size)
351186714001SSerapheim Dimitropoulos 			break;
351286714001SSerapheim Dimitropoulos 
351386714001SSerapheim Dimitropoulos 		/*
351486714001SSerapheim Dimitropoulos 		 * Mappings do not cross metaslab boundaries,
351586714001SSerapheim Dimitropoulos 		 * because we create them by walking the metaslabs.
351686714001SSerapheim Dimitropoulos 		 */
351786714001SSerapheim Dimitropoulos 		ASSERT3U(ent_offset + ent_len, <=,
351886714001SSerapheim Dimitropoulos 		    msp->ms_start + msp->ms_size);
351986714001SSerapheim Dimitropoulos 		range_tree_add(msp->ms_allocatable, ent_offset, ent_len);
352086714001SSerapheim Dimitropoulos 	}
352186714001SSerapheim Dimitropoulos 
352286714001SSerapheim Dimitropoulos 	if (!msp->ms_loaded)
352386714001SSerapheim Dimitropoulos 		msp->ms_loaded = B_TRUE;
352486714001SSerapheim Dimitropoulos 	mutex_exit(&msp->ms_lock);
352586714001SSerapheim Dimitropoulos }
352686714001SSerapheim Dimitropoulos 
352786714001SSerapheim Dimitropoulos static void
352886714001SSerapheim Dimitropoulos zdb_leak_init_prepare_indirect_vdevs(spa_t *spa, zdb_cb_t *zcb)
352986714001SSerapheim Dimitropoulos {
3530555d674dSSerapheim Dimitropoulos 	ASSERT(!dump_opt['L']);
3531555d674dSSerapheim Dimitropoulos 
353286714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
353386714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
353486714001SSerapheim Dimitropoulos 		vdev_t *vd = rvd->vdev_child[c];
353586714001SSerapheim Dimitropoulos 
353686714001SSerapheim Dimitropoulos 		ASSERT3U(c, ==, vd->vdev_id);
353786714001SSerapheim Dimitropoulos 
353886714001SSerapheim Dimitropoulos 		if (vd->vdev_ops != &vdev_indirect_ops)
353986714001SSerapheim Dimitropoulos 			continue;
354086714001SSerapheim Dimitropoulos 
354186714001SSerapheim Dimitropoulos 		/*
354286714001SSerapheim Dimitropoulos 		 * Note: we don't check for mapping leaks on
354386714001SSerapheim Dimitropoulos 		 * removing vdevs because their ms_allocatable's
354486714001SSerapheim Dimitropoulos 		 * are used to look for leaks in allocated space.
354586714001SSerapheim Dimitropoulos 		 */
354686714001SSerapheim Dimitropoulos 		zcb->zcb_vd_obsolete_counts[c] = zdb_load_obsolete_counts(vd);
354786714001SSerapheim Dimitropoulos 
354886714001SSerapheim Dimitropoulos 		/*
354986714001SSerapheim Dimitropoulos 		 * Normally, indirect vdevs don't have any
355086714001SSerapheim Dimitropoulos 		 * metaslabs.  We want to set them up for
355186714001SSerapheim Dimitropoulos 		 * zio_claim().
355286714001SSerapheim Dimitropoulos 		 */
355386714001SSerapheim Dimitropoulos 		VERIFY0(vdev_metaslab_init(vd, 0));
355486714001SSerapheim Dimitropoulos 
355586714001SSerapheim Dimitropoulos 		vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
355686714001SSerapheim Dimitropoulos 		uint64_t vim_idx = 0;
355786714001SSerapheim Dimitropoulos 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
355886714001SSerapheim Dimitropoulos 
355986714001SSerapheim Dimitropoulos 			(void) fprintf(stderr,
356086714001SSerapheim Dimitropoulos 			    "\rloading indirect vdev %llu, "
356186714001SSerapheim Dimitropoulos 			    "metaslab %llu of %llu ...",
356286714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_id,
356386714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_ms[m]->ms_id,
356486714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_ms_count);
356586714001SSerapheim Dimitropoulos 
356686714001SSerapheim Dimitropoulos 			load_indirect_ms_allocatable_tree(vd, vd->vdev_ms[m],
356786714001SSerapheim Dimitropoulos 			    &vim_idx);
356886714001SSerapheim Dimitropoulos 		}
356986714001SSerapheim Dimitropoulos 		ASSERT3U(vim_idx, ==, vdev_indirect_mapping_num_entries(vim));
357086714001SSerapheim Dimitropoulos 	}
357186714001SSerapheim Dimitropoulos }
357286714001SSerapheim Dimitropoulos 
3573b24ab676SJeff Bonwick static void
3574b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
3575b24ab676SJeff Bonwick {
3576cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
3577cde58dbcSMatthew Ahrens 
3578555d674dSSerapheim Dimitropoulos 	if (dump_opt['L'])
3579555d674dSSerapheim Dimitropoulos 		return;
35808363e80aSGeorge Wilson 
3581555d674dSSerapheim Dimitropoulos 	dsl_pool_t *dp = spa->spa_dsl_pool;
3582555d674dSSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
35838363e80aSGeorge Wilson 
3584555d674dSSerapheim Dimitropoulos 	/*
3585555d674dSSerapheim Dimitropoulos 	 * We are going to be changing the meaning of the metaslab's
3586555d674dSSerapheim Dimitropoulos 	 * ms_allocatable.  Ensure that the allocator doesn't try to
3587555d674dSSerapheim Dimitropoulos 	 * use the tree.
3588555d674dSSerapheim Dimitropoulos 	 */
3589555d674dSSerapheim Dimitropoulos 	spa->spa_normal_class->mc_ops = &zdb_metaslab_ops;
3590555d674dSSerapheim Dimitropoulos 	spa->spa_log_class->mc_ops = &zdb_metaslab_ops;
35915cabbc6bSPrashanth Sreenivasa 
3592555d674dSSerapheim Dimitropoulos 	zcb->zcb_vd_obsolete_counts =
3593555d674dSSerapheim Dimitropoulos 	    umem_zalloc(rvd->vdev_children * sizeof (uint32_t *),
3594555d674dSSerapheim Dimitropoulos 	    UMEM_NOFAIL);
35955cabbc6bSPrashanth Sreenivasa 
3596555d674dSSerapheim Dimitropoulos 	/*
3597555d674dSSerapheim Dimitropoulos 	 * For leak detection, we overload the ms_allocatable trees
3598555d674dSSerapheim Dimitropoulos 	 * to contain allocated segments instead of free segments.
3599555d674dSSerapheim Dimitropoulos 	 * As a result, we can't use the normal metaslab_load/unload
3600555d674dSSerapheim Dimitropoulos 	 * interfaces.
3601555d674dSSerapheim Dimitropoulos 	 */
3602555d674dSSerapheim Dimitropoulos 	zdb_leak_init_prepare_indirect_vdevs(spa, zcb);
3603555d674dSSerapheim Dimitropoulos 	load_concrete_ms_allocatable_trees(spa, SM_ALLOC);
36045cabbc6bSPrashanth Sreenivasa 
3605555d674dSSerapheim Dimitropoulos 	/*
3606555d674dSSerapheim Dimitropoulos 	 * On load_concrete_ms_allocatable_trees() we loaded all the
3607555d674dSSerapheim Dimitropoulos 	 * allocated entries from the ms_sm to the ms_allocatable for
3608555d674dSSerapheim Dimitropoulos 	 * each metaslab. If the pool has a checkpoint or is in the
3609555d674dSSerapheim Dimitropoulos 	 * middle of discarding a checkpoint, some of these blocks
3610555d674dSSerapheim Dimitropoulos 	 * may have been freed but their ms_sm may not have been
3611555d674dSSerapheim Dimitropoulos 	 * updated because they are referenced by the checkpoint. In
3612555d674dSSerapheim Dimitropoulos 	 * order to avoid false-positives during leak-detection, we
3613555d674dSSerapheim Dimitropoulos 	 * go through the vdev's checkpoint space map and exclude all
3614555d674dSSerapheim Dimitropoulos 	 * its entries from their relevant ms_allocatable.
3615555d674dSSerapheim Dimitropoulos 	 *
3616555d674dSSerapheim Dimitropoulos 	 * We also aggregate the space held by the checkpoint and add
3617555d674dSSerapheim Dimitropoulos 	 * it to zcb_checkpoint_size.
3618555d674dSSerapheim Dimitropoulos 	 *
3619555d674dSSerapheim Dimitropoulos 	 * Note that at this point we are also verifying that all the
3620555d674dSSerapheim Dimitropoulos 	 * entries on the checkpoint_sm are marked as allocated in
3621555d674dSSerapheim Dimitropoulos 	 * the ms_sm of their relevant metaslab.
3622555d674dSSerapheim Dimitropoulos 	 * [see comment in checkpoint_sm_exclude_entry_cb()]
3623555d674dSSerapheim Dimitropoulos 	 */
3624555d674dSSerapheim Dimitropoulos 	zdb_leak_init_exclude_checkpoint(spa, zcb);
3625555d674dSSerapheim Dimitropoulos 	ASSERT3U(zcb->zcb_checkpoint_size, ==, spa_get_checkpoint_space(spa));
36265cabbc6bSPrashanth Sreenivasa 
3627555d674dSSerapheim Dimitropoulos 	/* for cleaner progress output */
3628555d674dSSerapheim Dimitropoulos 	(void) fprintf(stderr, "\n");
3629555d674dSSerapheim Dimitropoulos 
3630555d674dSSerapheim Dimitropoulos 	if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
3631555d674dSSerapheim Dimitropoulos 		ASSERT(spa_feature_is_enabled(spa,
3632555d674dSSerapheim Dimitropoulos 		    SPA_FEATURE_DEVICE_REMOVAL));
3633555d674dSSerapheim Dimitropoulos 		(void) bpobj_iterate_nofree(&dp->dp_obsolete_bpobj,
3634555d674dSSerapheim Dimitropoulos 		    increment_indirect_mapping_cb, zcb, NULL);
3635b24ab676SJeff Bonwick 	}
3636b24ab676SJeff Bonwick 
3637b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3638bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
3639b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
3640b24ab676SJeff Bonwick }
3641b24ab676SJeff Bonwick 
36425cabbc6bSPrashanth Sreenivasa static boolean_t
36435cabbc6bSPrashanth Sreenivasa zdb_check_for_obsolete_leaks(vdev_t *vd, zdb_cb_t *zcb)
36445cabbc6bSPrashanth Sreenivasa {
36455cabbc6bSPrashanth Sreenivasa 	boolean_t leaks = B_FALSE;
36465cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
36475cabbc6bSPrashanth Sreenivasa 	uint64_t total_leaked = 0;
36485cabbc6bSPrashanth Sreenivasa 
36495cabbc6bSPrashanth Sreenivasa 	ASSERT(vim != NULL);
36505cabbc6bSPrashanth Sreenivasa 
36515cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
36525cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_entry_phys_t *vimep =
36535cabbc6bSPrashanth Sreenivasa 		    &vim->vim_entries[i];
36545cabbc6bSPrashanth Sreenivasa 		uint64_t obsolete_bytes = 0;
36555cabbc6bSPrashanth Sreenivasa 		uint64_t offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
36565cabbc6bSPrashanth Sreenivasa 		metaslab_t *msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
36575cabbc6bSPrashanth Sreenivasa 
36585cabbc6bSPrashanth Sreenivasa 		/*
36595cabbc6bSPrashanth Sreenivasa 		 * This is not very efficient but it's easy to
36605cabbc6bSPrashanth Sreenivasa 		 * verify correctness.
36615cabbc6bSPrashanth Sreenivasa 		 */
36625cabbc6bSPrashanth Sreenivasa 		for (uint64_t inner_offset = 0;
36635cabbc6bSPrashanth Sreenivasa 		    inner_offset < DVA_GET_ASIZE(&vimep->vimep_dst);
36645cabbc6bSPrashanth Sreenivasa 		    inner_offset += 1 << vd->vdev_ashift) {
366586714001SSerapheim Dimitropoulos 			if (range_tree_contains(msp->ms_allocatable,
36665cabbc6bSPrashanth Sreenivasa 			    offset + inner_offset, 1 << vd->vdev_ashift)) {
36675cabbc6bSPrashanth Sreenivasa 				obsolete_bytes += 1 << vd->vdev_ashift;
36685cabbc6bSPrashanth Sreenivasa 			}
36695cabbc6bSPrashanth Sreenivasa 		}
36705cabbc6bSPrashanth Sreenivasa 
36715cabbc6bSPrashanth Sreenivasa 		int64_t bytes_leaked = obsolete_bytes -
36725cabbc6bSPrashanth Sreenivasa 		    zcb->zcb_vd_obsolete_counts[vd->vdev_id][i];
36735cabbc6bSPrashanth Sreenivasa 		ASSERT3U(DVA_GET_ASIZE(&vimep->vimep_dst), >=,
36745cabbc6bSPrashanth Sreenivasa 		    zcb->zcb_vd_obsolete_counts[vd->vdev_id][i]);
36755cabbc6bSPrashanth Sreenivasa 		if (bytes_leaked != 0 &&
36765cabbc6bSPrashanth Sreenivasa 		    (vdev_obsolete_counts_are_precise(vd) ||
36775cabbc6bSPrashanth Sreenivasa 		    dump_opt['d'] >= 5)) {
36785cabbc6bSPrashanth Sreenivasa 			(void) printf("obsolete indirect mapping count "
36795cabbc6bSPrashanth Sreenivasa 			    "mismatch on %llu:%llx:%llx : %llx bytes leaked\n",
36805cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)vd->vdev_id,
36815cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
36825cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
36835cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)bytes_leaked);
36845cabbc6bSPrashanth Sreenivasa 		}
36855cabbc6bSPrashanth Sreenivasa 		total_leaked += ABS(bytes_leaked);
36865cabbc6bSPrashanth Sreenivasa 	}
36875cabbc6bSPrashanth Sreenivasa 
36885cabbc6bSPrashanth Sreenivasa 	if (!vdev_obsolete_counts_are_precise(vd) && total_leaked > 0) {
36895cabbc6bSPrashanth Sreenivasa 		int pct_leaked = total_leaked * 100 /
36905cabbc6bSPrashanth Sreenivasa 		    vdev_indirect_mapping_bytes_mapped(vim);
36915cabbc6bSPrashanth Sreenivasa 		(void) printf("cannot verify obsolete indirect mapping "
36925cabbc6bSPrashanth Sreenivasa 		    "counts of vdev %llu because precise feature was not "
36935cabbc6bSPrashanth Sreenivasa 		    "enabled when it was removed: %d%% (%llx bytes) of mapping"
36945cabbc6bSPrashanth Sreenivasa 		    "unreferenced\n",
36955cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)vd->vdev_id, pct_leaked,
36965cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)total_leaked);
36975cabbc6bSPrashanth Sreenivasa 	} else if (total_leaked > 0) {
36985cabbc6bSPrashanth Sreenivasa 		(void) printf("obsolete indirect mapping count mismatch "
36995cabbc6bSPrashanth Sreenivasa 		    "for vdev %llu -- %llx total bytes mismatched\n",
37005cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)vd->vdev_id,
37015cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)total_leaked);
37025cabbc6bSPrashanth Sreenivasa 		leaks |= B_TRUE;
37035cabbc6bSPrashanth Sreenivasa 	}
37045cabbc6bSPrashanth Sreenivasa 
37055cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_free_obsolete_counts(vim,
37065cabbc6bSPrashanth Sreenivasa 	    zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
37075cabbc6bSPrashanth Sreenivasa 	zcb->zcb_vd_obsolete_counts[vd->vdev_id] = NULL;
37085cabbc6bSPrashanth Sreenivasa 
37095cabbc6bSPrashanth Sreenivasa 	return (leaks);
37105cabbc6bSPrashanth Sreenivasa }
37115cabbc6bSPrashanth Sreenivasa 
37125cabbc6bSPrashanth Sreenivasa static boolean_t
37135cabbc6bSPrashanth Sreenivasa zdb_leak_fini(spa_t *spa, zdb_cb_t *zcb)
3714b24ab676SJeff Bonwick {
3715555d674dSSerapheim Dimitropoulos 	if (dump_opt['L'])
3716555d674dSSerapheim Dimitropoulos 		return (B_FALSE);
3717555d674dSSerapheim Dimitropoulos 
37185cabbc6bSPrashanth Sreenivasa 	boolean_t leaks = B_FALSE;
37195cabbc6bSPrashanth Sreenivasa 
3720555d674dSSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
3721555d674dSSerapheim Dimitropoulos 	for (unsigned c = 0; c < rvd->vdev_children; c++) {
3722555d674dSSerapheim Dimitropoulos 		vdev_t *vd = rvd->vdev_child[c];
3723555d674dSSerapheim Dimitropoulos #if DEBUG
3724555d674dSSerapheim Dimitropoulos 		metaslab_group_t *mg = vd->vdev_mg;
3725555d674dSSerapheim Dimitropoulos #endif
37268363e80aSGeorge Wilson 
3727555d674dSSerapheim Dimitropoulos 		if (zcb->zcb_vd_obsolete_counts[c] != NULL) {
3728555d674dSSerapheim Dimitropoulos 			leaks |= zdb_check_for_obsolete_leaks(vd, zcb);
3729555d674dSSerapheim Dimitropoulos 		}
3730555d674dSSerapheim Dimitropoulos 
3731555d674dSSerapheim Dimitropoulos 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
3732555d674dSSerapheim Dimitropoulos 			metaslab_t *msp = vd->vdev_ms[m];
3733555d674dSSerapheim Dimitropoulos 			ASSERT3P(mg, ==, msp->ms_group);
3734555d674dSSerapheim Dimitropoulos 
3735555d674dSSerapheim Dimitropoulos 			/*
3736555d674dSSerapheim Dimitropoulos 			 * ms_allocatable has been overloaded
3737555d674dSSerapheim Dimitropoulos 			 * to contain allocated segments. Now that
3738555d674dSSerapheim Dimitropoulos 			 * we finished traversing all blocks, any
3739555d674dSSerapheim Dimitropoulos 			 * block that remains in the ms_allocatable
3740555d674dSSerapheim Dimitropoulos 			 * represents an allocated block that we
3741555d674dSSerapheim Dimitropoulos 			 * did not claim during the traversal.
3742555d674dSSerapheim Dimitropoulos 			 * Claimed blocks would have been removed
3743555d674dSSerapheim Dimitropoulos 			 * from the ms_allocatable.  For indirect
3744555d674dSSerapheim Dimitropoulos 			 * vdevs, space remaining in the tree
3745555d674dSSerapheim Dimitropoulos 			 * represents parts of the mapping that are
3746555d674dSSerapheim Dimitropoulos 			 * not referenced, which is not a bug.
3747555d674dSSerapheim Dimitropoulos 			 */
3748555d674dSSerapheim Dimitropoulos 			if (vd->vdev_ops == &vdev_indirect_ops) {
3749555d674dSSerapheim Dimitropoulos 				range_tree_vacate(msp->ms_allocatable,
3750555d674dSSerapheim Dimitropoulos 				    NULL, NULL);
3751555d674dSSerapheim Dimitropoulos 			} else {
3752555d674dSSerapheim Dimitropoulos 				range_tree_vacate(msp->ms_allocatable,
3753555d674dSSerapheim Dimitropoulos 				    zdb_leak, vd);
3754555d674dSSerapheim Dimitropoulos 			}
3755555d674dSSerapheim Dimitropoulos 
3756555d674dSSerapheim Dimitropoulos 			if (msp->ms_loaded) {
3757555d674dSSerapheim Dimitropoulos 				msp->ms_loaded = B_FALSE;
3758b24ab676SJeff Bonwick 			}
3759b24ab676SJeff Bonwick 		}
37605cabbc6bSPrashanth Sreenivasa 
3761b24ab676SJeff Bonwick 	}
3762555d674dSSerapheim Dimitropoulos 
3763555d674dSSerapheim Dimitropoulos 	umem_free(zcb->zcb_vd_obsolete_counts,
3764555d674dSSerapheim Dimitropoulos 	    rvd->vdev_children * sizeof (uint32_t *));
3765555d674dSSerapheim Dimitropoulos 	zcb->zcb_vd_obsolete_counts = NULL;
3766555d674dSSerapheim Dimitropoulos 
37675cabbc6bSPrashanth Sreenivasa 	return (leaks);
3768b24ab676SJeff Bonwick }
3769b24ab676SJeff Bonwick 
3770cde58dbcSMatthew Ahrens /* ARGSUSED */
3771cde58dbcSMatthew Ahrens static int
3772cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
3773cde58dbcSMatthew Ahrens {
3774cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
3775cde58dbcSMatthew Ahrens 
3776490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
3777cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
377843466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3779cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
3780cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
3781cde58dbcSMatthew Ahrens 	}
3782cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
3783cde58dbcSMatthew Ahrens 	return (0);
3784cde58dbcSMatthew Ahrens }
3785cde58dbcSMatthew Ahrens 
3786fa9e4066Sahrens static int
3787fa9e4066Sahrens dump_block_stats(spa_t *spa)
3788fa9e4066Sahrens {
37893f7978d0SAlan Somers 	zdb_cb_t zcb;
3790fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
3791b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
3792eb633035STom Caputi 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
3793eb633035STom Caputi 	    TRAVERSE_NO_DECRYPT | TRAVERSE_HARD;
37945d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
3795663207adSDon Brady 	int err;
3796fa9e4066Sahrens 
37973f7978d0SAlan Somers 	bzero(&zcb, sizeof (zcb));
3798490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
3799b24ab676SJeff Bonwick 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
3800b24ab676SJeff Bonwick 	    (dump_opt['c'] == 1) ? "metadata " : "",
3801b24ab676SJeff Bonwick 	    dump_opt['c'] ? "checksums " : "",
3802b24ab676SJeff Bonwick 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
3803b24ab676SJeff Bonwick 	    !dump_opt['L'] ? "nothing leaked " : "");
3804fa9e4066Sahrens 
3805fa9e4066Sahrens 	/*
3806555d674dSSerapheim Dimitropoulos 	 * When leak detection is enabled we load all space maps as SM_ALLOC
3807555d674dSSerapheim Dimitropoulos 	 * maps, then traverse the pool claiming each block we discover. If
3808555d674dSSerapheim Dimitropoulos 	 * the pool is perfectly consistent, the segment trees will be empty
3809555d674dSSerapheim Dimitropoulos 	 * when we're done. Anything left over is a leak; any block we can't
3810555d674dSSerapheim Dimitropoulos 	 * claim (because it's not part of any space map) is a double
3811555d674dSSerapheim Dimitropoulos 	 * allocation, reference to a freed block, or an unclaimed log block.
3812555d674dSSerapheim Dimitropoulos 	 *
3813555d674dSSerapheim Dimitropoulos 	 * When leak detection is disabled (-L option) we still traverse the
3814555d674dSSerapheim Dimitropoulos 	 * pool claiming each block we discover, but we skip opening any space
3815555d674dSSerapheim Dimitropoulos 	 * maps.
3816fa9e4066Sahrens 	 */
3817555d674dSSerapheim Dimitropoulos 	bzero(&zcb, sizeof (zdb_cb_t));
3818b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
3819fa9e4066Sahrens 
3820fa9e4066Sahrens 	/*
3821fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
3822fa9e4066Sahrens 	 */
3823cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
3824cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
38255cabbc6bSPrashanth Sreenivasa 
38263b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
38273b2aab18SMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
38283b2aab18SMatthew Ahrens 		    count_block_cb, &zcb, NULL);
38293b2aab18SMatthew Ahrens 	}
38305cabbc6bSPrashanth Sreenivasa 
38315cabbc6bSPrashanth Sreenivasa 	zdb_claim_removing(spa, &zcb);
38325cabbc6bSPrashanth Sreenivasa 
38332acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
3834b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
3835ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
3836ad135b5dSChristopher Siden 		    &zcb, NULL));
3837ad135b5dSChristopher Siden 	}
3838fa9e4066Sahrens 
3839bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
3840bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
3841bbfd46c4SJeff Bonwick 
3842490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
3843663207adSDon Brady 	zcb.zcb_totalasize += metaslab_class_get_alloc(spa_special_class(spa));
3844663207adSDon Brady 	zcb.zcb_totalasize += metaslab_class_get_alloc(spa_dedup_class(spa));
3845490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
3846663207adSDon Brady 	err = traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
3847fa9e4066Sahrens 
384831d7e8faSGeorge Wilson 	/*
384931d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
385031d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
385131d7e8faSGeorge Wilson 	 * all async I/Os to complete.
385231d7e8faSGeorge Wilson 	 */
385331d7e8faSGeorge Wilson 	if (dump_opt['c']) {
38546f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
38556f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
38566f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
38576f834bc1SMatthew Ahrens 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
38586f834bc1SMatthew Ahrens 			    ZIO_FLAG_GODFATHER);
38596f834bc1SMatthew Ahrens 		}
386031d7e8faSGeorge Wilson 	}
386131d7e8faSGeorge Wilson 
3862663207adSDon Brady 	/*
3863663207adSDon Brady 	 * Done after zio_wait() since zcb_haderrors is modified in
3864663207adSDon Brady 	 * zdb_blkptr_done()
3865663207adSDon Brady 	 */
3866663207adSDon Brady 	zcb.zcb_haderrors |= err;
3867663207adSDon Brady 
3868b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
3869fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
3870fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
3871b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
3872fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
3873fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
3874fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
3875fa9e4066Sahrens 			}
3876fa9e4066Sahrens 		}
3877fa9e4066Sahrens 	}
3878fa9e4066Sahrens 
3879fa9e4066Sahrens 	/*
3880fa9e4066Sahrens 	 * Report any leaked segments.
3881fa9e4066Sahrens 	 */
38825cabbc6bSPrashanth Sreenivasa 	leaks |= zdb_leak_fini(spa, &zcb);
3883fa9e4066Sahrens 
3884b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
38858654d025Sperrin 
3886b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
3887b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
38888654d025Sperrin 
3889663207adSDon Brady 	total_alloc = norm_alloc +
3890663207adSDon Brady 	    metaslab_class_get_alloc(spa_log_class(spa)) +
3891663207adSDon Brady 	    metaslab_class_get_alloc(spa_special_class(spa)) +
3892663207adSDon Brady 	    metaslab_class_get_alloc(spa_dedup_class(spa));
38935cabbc6bSPrashanth Sreenivasa 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize +
389486714001SSerapheim Dimitropoulos 	    zcb.zcb_removing_size + zcb.zcb_checkpoint_size;
3895fa9e4066Sahrens 
3896555d674dSSerapheim Dimitropoulos 	if (total_found == total_alloc && !dump_opt['L']) {
3897555d674dSSerapheim Dimitropoulos 		(void) printf("\n\tNo leaks (block sum matches space"
3898555d674dSSerapheim Dimitropoulos 		    " maps exactly)\n");
3899555d674dSSerapheim Dimitropoulos 	} else if (!dump_opt['L']) {
3900fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
390182a0a985SVictor Latushkin 		    "(%s %lld)\n",
3902b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
3903b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
390482a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
3905b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
39065d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
3907fa9e4066Sahrens 	}
3908fa9e4066Sahrens 
3909fa9e4066Sahrens 	if (tzb->zb_count == 0)
3910fa9e4066Sahrens 		return (2);
3911fa9e4066Sahrens 
3912fa9e4066Sahrens 	(void) printf("\n");
3913663207adSDon Brady 	(void) printf("\t%-16s %14llu\n", "bp count:",
3914fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
3915663207adSDon Brady 	(void) printf("\t%-16s %14llu\n", "ganged count:",
3916d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
3917663207adSDon Brady 	(void) printf("\t%-16s %14llu      avg: %6llu\n", "bp logical:",
3918fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
3919fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
3920663207adSDon Brady 	(void) printf("\t%-16s %14llu      avg: %6llu     compression: %6.2f\n",
3921663207adSDon Brady 	    "bp physical:", (u_longlong_t)tzb->zb_psize,
3922fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
3923fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
3924663207adSDon Brady 	(void) printf("\t%-16s %14llu      avg: %6llu     compression: %6.2f\n",
3925663207adSDon Brady 	    "bp allocated:", (u_longlong_t)tzb->zb_asize,
3926fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
3927fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
3928663207adSDon Brady 	(void) printf("\t%-16s %14llu    ref>1: %6llu   deduplication: %6.2f\n",
3929663207adSDon Brady 	    "bp deduped:", (u_longlong_t)zcb.zcb_dedup_asize,
3930b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
3931b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
3932663207adSDon Brady 	(void) printf("\t%-16s %14llu     used: %5.2f%%\n", "Normal class:",
3933b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
3934fa9e4066Sahrens 
3935663207adSDon Brady 	if (spa_special_class(spa)->mc_rotor != NULL) {
3936663207adSDon Brady 		uint64_t alloc = metaslab_class_get_alloc(
3937663207adSDon Brady 		    spa_special_class(spa));
3938663207adSDon Brady 		uint64_t space = metaslab_class_get_space(
3939663207adSDon Brady 		    spa_special_class(spa));
3940663207adSDon Brady 
3941663207adSDon Brady 		(void) printf("\t%-16s %14llu     used: %5.2f%%\n",
3942663207adSDon Brady 		    "Special class", (u_longlong_t)alloc,
3943663207adSDon Brady 		    100.0 * alloc / space);
3944663207adSDon Brady 	}
3945663207adSDon Brady 
3946663207adSDon Brady 	if (spa_dedup_class(spa)->mc_rotor != NULL) {
3947663207adSDon Brady 		uint64_t alloc = metaslab_class_get_alloc(
3948663207adSDon Brady 		    spa_dedup_class(spa));
3949663207adSDon Brady 		uint64_t space = metaslab_class_get_space(
3950663207adSDon Brady 		    spa_dedup_class(spa));
3951663207adSDon Brady 
3952663207adSDon Brady 		(void) printf("\t%-16s %14llu     used: %5.2f%%\n",
3953663207adSDon Brady 		    "Dedup class", (u_longlong_t)alloc,
3954663207adSDon Brady 		    100.0 * alloc / space);
3955663207adSDon Brady 	}
3956663207adSDon Brady 
39575d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
39585d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
39595d7b4d43SMatthew Ahrens 			continue;
39605d7b4d43SMatthew Ahrens 		(void) printf("\n");
39615d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
39625d7b4d43SMatthew Ahrens 		    "%10llu\n",
39635d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
39645d7b4d43SMatthew Ahrens 
39655d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
39665d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
39675d7b4d43SMatthew Ahrens 			    "number of bps\n");
39685d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
39695d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
39705d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
39715d7b4d43SMatthew Ahrens 		}
39725d7b4d43SMatthew Ahrens 	}
39735d7b4d43SMatthew Ahrens 
3974d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
3975d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
3976d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
3977d5ee8a13SMatthew Ahrens 	}
3978663207adSDon Brady 	if (tzb->zb_ditto_same_ms != 0) {
3979663207adSDon Brady 		(void) printf("\tDittoed blocks in same metaslab: %llu\n",
3980663207adSDon Brady 		    (longlong_t)tzb->zb_ditto_same_ms);
3981663207adSDon Brady 	}
3982d5ee8a13SMatthew Ahrens 
39835cabbc6bSPrashanth Sreenivasa 	for (uint64_t v = 0; v < spa->spa_root_vdev->vdev_children; v++) {
39845cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = spa->spa_root_vdev->vdev_child[v];
39855cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
39865cabbc6bSPrashanth Sreenivasa 
39875cabbc6bSPrashanth Sreenivasa 		if (vim == NULL) {
39885cabbc6bSPrashanth Sreenivasa 			continue;
39895cabbc6bSPrashanth Sreenivasa 		}
39905cabbc6bSPrashanth Sreenivasa 
39915cabbc6bSPrashanth Sreenivasa 		char mem[32];
39925cabbc6bSPrashanth Sreenivasa 		zdb_nicenum(vdev_indirect_mapping_num_entries(vim),
39935cabbc6bSPrashanth Sreenivasa 		    mem, vdev_indirect_mapping_size(vim));
39945cabbc6bSPrashanth Sreenivasa 
39955cabbc6bSPrashanth Sreenivasa 		(void) printf("\tindirect vdev id %llu has %llu segments "
39965cabbc6bSPrashanth Sreenivasa 		    "(%s in memory)\n",
39975cabbc6bSPrashanth Sreenivasa 		    (longlong_t)vd->vdev_id,
39985cabbc6bSPrashanth Sreenivasa 		    (longlong_t)vdev_indirect_mapping_num_entries(vim), mem);
39995cabbc6bSPrashanth Sreenivasa 	}
40005cabbc6bSPrashanth Sreenivasa 
4001fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
4002fa9e4066Sahrens 		int l, t, level;
4003fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
4004fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
4005fa9e4066Sahrens 
4006b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
40073f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
4008d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
40093f7978d0SAlan Somers 			const char *typename;
4010fa9e4066Sahrens 
40110a055120SJason King 			/* make sure nicenum has enough space */
40120a055120SJason King 			CTASSERT(sizeof (csize) >= NN_NUMBUF_SZ);
40130a055120SJason King 			CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
40140a055120SJason King 			CTASSERT(sizeof (psize) >= NN_NUMBUF_SZ);
40150a055120SJason King 			CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
40160a055120SJason King 			CTASSERT(sizeof (avg) >= NN_NUMBUF_SZ);
40170a055120SJason King 			CTASSERT(sizeof (gang) >= NN_NUMBUF_SZ);
40180a055120SJason King 
4019b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
4020b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
4021b24ab676SJeff Bonwick 			else
4022b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
4023fa9e4066Sahrens 
4024fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
4025fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
4026fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
4027fa9e4066Sahrens 				    "-",
4028fa9e4066Sahrens 				    "-",
4029fa9e4066Sahrens 				    "-",
4030fa9e4066Sahrens 				    "-",
4031fa9e4066Sahrens 				    "-",
4032fa9e4066Sahrens 				    "-",
4033fa9e4066Sahrens 				    "-",
4034fa9e4066Sahrens 				    typename);
4035fa9e4066Sahrens 				continue;
4036fa9e4066Sahrens 			}
4037fa9e4066Sahrens 
4038fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
4039fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
4040fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
4041fa9e4066Sahrens 
4042fa9e4066Sahrens 				if (zb->zb_asize == 0)
4043fa9e4066Sahrens 					continue;
4044fa9e4066Sahrens 
4045fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
4046fa9e4066Sahrens 					continue;
4047fa9e4066Sahrens 
4048fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
4049fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
4050fa9e4066Sahrens 					continue;
4051fa9e4066Sahrens 
40520a055120SJason King 				zdb_nicenum(zb->zb_count, csize,
40530a055120SJason King 				    sizeof (csize));
40540a055120SJason King 				zdb_nicenum(zb->zb_lsize, lsize,
40550a055120SJason King 				    sizeof (lsize));
40560a055120SJason King 				zdb_nicenum(zb->zb_psize, psize,
40570a055120SJason King 				    sizeof (psize));
40580a055120SJason King 				zdb_nicenum(zb->zb_asize, asize,
40590a055120SJason King 				    sizeof (asize));
40600a055120SJason King 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg,
40610a055120SJason King 				    sizeof (avg));
40620a055120SJason King 				zdb_nicenum(zb->zb_gangs, gang, sizeof (gang));
4063fa9e4066Sahrens 
4064fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
4065fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
4066fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
4067fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
4068fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
4069fa9e4066Sahrens 
4070fa9e4066Sahrens 				if (level == ZB_TOTAL)
4071fa9e4066Sahrens 					(void) printf("%s\n", typename);
4072fa9e4066Sahrens 				else
4073fa9e4066Sahrens 					(void) printf("    L%d %s\n",
4074fa9e4066Sahrens 					    level, typename);
4075490d05b9SMatthew Ahrens 
4076d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
4077d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
4078d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
4079d5ee8a13SMatthew Ahrens 				}
4080d5ee8a13SMatthew Ahrens 
4081490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
4082490d05b9SMatthew Ahrens 					(void) printf("psize "
4083490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
4084490d05b9SMatthew Ahrens 					    "number of blocks\n");
4085490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
40860713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
4087490d05b9SMatthew Ahrens 				}
4088fa9e4066Sahrens 			}
4089fa9e4066Sahrens 		}
4090fa9e4066Sahrens 	}
4091fa9e4066Sahrens 
4092fa9e4066Sahrens 	(void) printf("\n");
4093fa9e4066Sahrens 
4094fa9e4066Sahrens 	if (leaks)
4095fa9e4066Sahrens 		return (2);
4096fa9e4066Sahrens 
4097fa9e4066Sahrens 	if (zcb.zcb_haderrors)
4098fa9e4066Sahrens 		return (3);
4099fa9e4066Sahrens 
4100fa9e4066Sahrens 	return (0);
4101fa9e4066Sahrens }
4102fa9e4066Sahrens 
4103b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
4104b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
4105b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
4106b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
4107b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
4108b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
4109b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
4110b24ab676SJeff Bonwick } zdb_ddt_entry_t;
4111b24ab676SJeff Bonwick 
4112b24ab676SJeff Bonwick /* ARGSUSED */
4113b24ab676SJeff Bonwick static int
4114b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
41157802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
4116b24ab676SJeff Bonwick {
4117b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
4118b24ab676SJeff Bonwick 	avl_index_t where;
4119b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
4120b24ab676SJeff Bonwick 
4121a2cdcdd2SPaul Dagnelie 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
4122b24ab676SJeff Bonwick 		return (0);
4123b24ab676SJeff Bonwick 
4124b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
4125b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
4126b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
4127b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
41285d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
4129b24ab676SJeff Bonwick 		    avl_numnodes(t));
4130b24ab676SJeff Bonwick 	}
4131b24ab676SJeff Bonwick 
4132bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
4133ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
4134b24ab676SJeff Bonwick 		return (0);
4135b24ab676SJeff Bonwick 
4136b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
4137b24ab676SJeff Bonwick 
4138b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
4139b24ab676SJeff Bonwick 
4140b24ab676SJeff Bonwick 	if (zdde == NULL) {
4141b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
4142b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
4143b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
4144b24ab676SJeff Bonwick 	}
4145b24ab676SJeff Bonwick 
4146b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
4147b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
4148b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
4149b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
4150b24ab676SJeff Bonwick 
4151b24ab676SJeff Bonwick 	return (0);
4152b24ab676SJeff Bonwick }
4153b24ab676SJeff Bonwick 
4154b24ab676SJeff Bonwick static void
4155b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
4156b24ab676SJeff Bonwick {
4157b24ab676SJeff Bonwick 	avl_tree_t t;
4158b24ab676SJeff Bonwick 	void *cookie = NULL;
4159b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
41603f7978d0SAlan Somers 	ddt_histogram_t ddh_total;
41613f7978d0SAlan Somers 	ddt_stat_t dds_total;
4162b24ab676SJeff Bonwick 
41633f7978d0SAlan Somers 	bzero(&ddh_total, sizeof (ddh_total));
41643f7978d0SAlan Somers 	bzero(&dds_total, sizeof (dds_total));
4165b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
4166b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
4167b24ab676SJeff Bonwick 
4168b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4169b24ab676SJeff Bonwick 
4170eb633035STom Caputi 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
4171eb633035STom Caputi 	    TRAVERSE_NO_DECRYPT, zdb_ddt_add_cb, &t);
4172b24ab676SJeff Bonwick 
4173b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
4174b24ab676SJeff Bonwick 
4175b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
4176b24ab676SJeff Bonwick 		ddt_stat_t dds;
4177b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
4178b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
4179b24ab676SJeff Bonwick 
4180b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
4181b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
4182b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
4183b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
4184b24ab676SJeff Bonwick 
4185b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
4186b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
4187b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
4188b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
4189b24ab676SJeff Bonwick 
4190bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
4191bf16b11eSMatthew Ahrens 		    &dds, 0);
4192b24ab676SJeff Bonwick 
4193b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
4194b24ab676SJeff Bonwick 	}
4195b24ab676SJeff Bonwick 
4196b24ab676SJeff Bonwick 	avl_destroy(&t);
4197b24ab676SJeff Bonwick 
4198b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
4199b24ab676SJeff Bonwick 
4200b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
4201b24ab676SJeff Bonwick 
42029eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
4203b24ab676SJeff Bonwick 
4204b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
4205b24ab676SJeff Bonwick }
4206b24ab676SJeff Bonwick 
42075cabbc6bSPrashanth Sreenivasa static int
42085cabbc6bSPrashanth Sreenivasa verify_device_removal_feature_counts(spa_t *spa)
42095cabbc6bSPrashanth Sreenivasa {
42105cabbc6bSPrashanth Sreenivasa 	uint64_t dr_feature_refcount = 0;
42115cabbc6bSPrashanth Sreenivasa 	uint64_t oc_feature_refcount = 0;
42125cabbc6bSPrashanth Sreenivasa 	uint64_t indirect_vdev_count = 0;
42135cabbc6bSPrashanth Sreenivasa 	uint64_t precise_vdev_count = 0;
42145cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_counts_object_count = 0;
42155cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_count = 0;
42165cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_counts_count = 0;
42175cabbc6bSPrashanth Sreenivasa 	uint64_t scip_count = 0;
42185cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_bpobj_count = 0;
42195cabbc6bSPrashanth Sreenivasa 	int ret = 0;
42205cabbc6bSPrashanth Sreenivasa 
42215cabbc6bSPrashanth Sreenivasa 	spa_condensing_indirect_phys_t *scip =
42225cabbc6bSPrashanth Sreenivasa 	    &spa->spa_condensing_indirect_phys;
42235cabbc6bSPrashanth Sreenivasa 	if (scip->scip_next_mapping_object != 0) {
42245cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = spa->spa_root_vdev->vdev_child[scip->scip_vdev];
42255cabbc6bSPrashanth Sreenivasa 		ASSERT(scip->scip_prev_obsolete_sm_object != 0);
42265cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
42275cabbc6bSPrashanth Sreenivasa 
42285cabbc6bSPrashanth Sreenivasa 		(void) printf("Condensing indirect vdev %llu: new mapping "
42295cabbc6bSPrashanth Sreenivasa 		    "object %llu, prev obsolete sm %llu\n",
42305cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)scip->scip_vdev,
42315cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)scip->scip_next_mapping_object,
42325cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)scip->scip_prev_obsolete_sm_object);
42335cabbc6bSPrashanth Sreenivasa 		if (scip->scip_prev_obsolete_sm_object != 0) {
42345cabbc6bSPrashanth Sreenivasa 			space_map_t *prev_obsolete_sm = NULL;
42355cabbc6bSPrashanth Sreenivasa 			VERIFY0(space_map_open(&prev_obsolete_sm,
42365cabbc6bSPrashanth Sreenivasa 			    spa->spa_meta_objset,
42375cabbc6bSPrashanth Sreenivasa 			    scip->scip_prev_obsolete_sm_object,
42385cabbc6bSPrashanth Sreenivasa 			    0, vd->vdev_asize, 0));
42395cabbc6bSPrashanth Sreenivasa 			dump_spacemap(spa->spa_meta_objset, prev_obsolete_sm);
42405cabbc6bSPrashanth Sreenivasa 			(void) printf("\n");
42415cabbc6bSPrashanth Sreenivasa 			space_map_close(prev_obsolete_sm);
42425cabbc6bSPrashanth Sreenivasa 		}
42435cabbc6bSPrashanth Sreenivasa 
42445cabbc6bSPrashanth Sreenivasa 		scip_count += 2;
42455cabbc6bSPrashanth Sreenivasa 	}
42465cabbc6bSPrashanth Sreenivasa 
42475cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < spa->spa_root_vdev->vdev_children; i++) {
42485cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = spa->spa_root_vdev->vdev_child[i];
42495cabbc6bSPrashanth Sreenivasa 		vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
42505cabbc6bSPrashanth Sreenivasa 
42515cabbc6bSPrashanth Sreenivasa 		if (vic->vic_mapping_object != 0) {
42525cabbc6bSPrashanth Sreenivasa 			ASSERT(vd->vdev_ops == &vdev_indirect_ops ||
42535cabbc6bSPrashanth Sreenivasa 			    vd->vdev_removing);
42545cabbc6bSPrashanth Sreenivasa 			indirect_vdev_count++;
42555cabbc6bSPrashanth Sreenivasa 
42565cabbc6bSPrashanth Sreenivasa 			if (vd->vdev_indirect_mapping->vim_havecounts) {
42575cabbc6bSPrashanth Sreenivasa 				obsolete_counts_count++;
42585cabbc6bSPrashanth Sreenivasa 			}
42595cabbc6bSPrashanth Sreenivasa 		}
42605cabbc6bSPrashanth Sreenivasa 		if (vdev_obsolete_counts_are_precise(vd)) {
42615cabbc6bSPrashanth Sreenivasa 			ASSERT(vic->vic_mapping_object != 0);
42625cabbc6bSPrashanth Sreenivasa 			precise_vdev_count++;
42635cabbc6bSPrashanth Sreenivasa 		}
42645cabbc6bSPrashanth Sreenivasa 		if (vdev_obsolete_sm_object(vd) != 0) {
42655cabbc6bSPrashanth Sreenivasa 			ASSERT(vic->vic_mapping_object != 0);
42665cabbc6bSPrashanth Sreenivasa 			obsolete_sm_count++;
42675cabbc6bSPrashanth Sreenivasa 		}
42685cabbc6bSPrashanth Sreenivasa 	}
42695cabbc6bSPrashanth Sreenivasa 
42705cabbc6bSPrashanth Sreenivasa 	(void) feature_get_refcount(spa,
42715cabbc6bSPrashanth Sreenivasa 	    &spa_feature_table[SPA_FEATURE_DEVICE_REMOVAL],
42725cabbc6bSPrashanth Sreenivasa 	    &dr_feature_refcount);
42735cabbc6bSPrashanth Sreenivasa 	(void) feature_get_refcount(spa,
42745cabbc6bSPrashanth Sreenivasa 	    &spa_feature_table[SPA_FEATURE_OBSOLETE_COUNTS],
42755cabbc6bSPrashanth Sreenivasa 	    &oc_feature_refcount);
42765cabbc6bSPrashanth Sreenivasa 
42775cabbc6bSPrashanth Sreenivasa 	if (dr_feature_refcount != indirect_vdev_count) {
42785cabbc6bSPrashanth Sreenivasa 		ret = 1;
42795cabbc6bSPrashanth Sreenivasa 		(void) printf("Number of indirect vdevs (%llu) " \
42805cabbc6bSPrashanth Sreenivasa 		    "does not match feature count (%llu)\n",
42815cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)indirect_vdev_count,
42825cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)dr_feature_refcount);
42835cabbc6bSPrashanth Sreenivasa 	} else {
42845cabbc6bSPrashanth Sreenivasa 		(void) printf("Verified device_removal feature refcount " \
42855cabbc6bSPrashanth Sreenivasa 		    "of %llu is correct\n",
42865cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)dr_feature_refcount);
42875cabbc6bSPrashanth Sreenivasa 	}
42885cabbc6bSPrashanth Sreenivasa 
42895cabbc6bSPrashanth Sreenivasa 	if (zap_contains(spa_meta_objset(spa), DMU_POOL_DIRECTORY_OBJECT,
42905cabbc6bSPrashanth Sreenivasa 	    DMU_POOL_OBSOLETE_BPOBJ) == 0) {
42915cabbc6bSPrashanth Sreenivasa 		obsolete_bpobj_count++;
42925cabbc6bSPrashanth Sreenivasa 	}
42935cabbc6bSPrashanth Sreenivasa 
42945cabbc6bSPrashanth Sreenivasa 
42955cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count = precise_vdev_count;
42965cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += obsolete_sm_count;
42975cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += obsolete_counts_count;
42985cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += scip_count;
42995cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += obsolete_bpobj_count;
43005cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += remap_deadlist_count;
43015cabbc6bSPrashanth Sreenivasa 
43025cabbc6bSPrashanth Sreenivasa 	if (oc_feature_refcount != obsolete_counts_object_count) {
43035cabbc6bSPrashanth Sreenivasa 		ret = 1;
43045cabbc6bSPrashanth Sreenivasa 		(void) printf("Number of obsolete counts objects (%llu) " \
43055cabbc6bSPrashanth Sreenivasa 		    "does not match feature count (%llu)\n",
43065cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_counts_object_count,
43075cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)oc_feature_refcount);
43085cabbc6bSPrashanth Sreenivasa 		(void) printf("pv:%llu os:%llu oc:%llu sc:%llu "
43095cabbc6bSPrashanth Sreenivasa 		    "ob:%llu rd:%llu\n",
43105cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)precise_vdev_count,
43115cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_sm_count,
43125cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_counts_count,
43135cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)scip_count,
43145cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_bpobj_count,
43155cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)remap_deadlist_count);
43165cabbc6bSPrashanth Sreenivasa 	} else {
43175cabbc6bSPrashanth Sreenivasa 		(void) printf("Verified indirect_refcount feature refcount " \
43185cabbc6bSPrashanth Sreenivasa 		    "of %llu is correct\n",
43195cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)oc_feature_refcount);
43205cabbc6bSPrashanth Sreenivasa 	}
43215cabbc6bSPrashanth Sreenivasa 	return (ret);
43225cabbc6bSPrashanth Sreenivasa }
43235cabbc6bSPrashanth Sreenivasa 
4324e0f1c0afSOlaf Faaland static void
4325e0f1c0afSOlaf Faaland zdb_set_skip_mmp(char *target)
4326e0f1c0afSOlaf Faaland {
4327e0f1c0afSOlaf Faaland 	spa_t *spa;
4328e0f1c0afSOlaf Faaland 
4329e0f1c0afSOlaf Faaland 	/*
4330e0f1c0afSOlaf Faaland 	 * Disable the activity check to allow examination of
4331e0f1c0afSOlaf Faaland 	 * active pools.
4332e0f1c0afSOlaf Faaland 	 */
4333e0f1c0afSOlaf Faaland 	mutex_enter(&spa_namespace_lock);
4334e0f1c0afSOlaf Faaland 	if ((spa = spa_lookup(target)) != NULL) {
4335e0f1c0afSOlaf Faaland 		spa->spa_import_flags |= ZFS_IMPORT_SKIP_MMP;
4336e0f1c0afSOlaf Faaland 	}
4337e0f1c0afSOlaf Faaland 	mutex_exit(&spa_namespace_lock);
4338e0f1c0afSOlaf Faaland }
4339e0f1c0afSOlaf Faaland 
434086714001SSerapheim Dimitropoulos #define	BOGUS_SUFFIX "_CHECKPOINTED_UNIVERSE"
434186714001SSerapheim Dimitropoulos /*
434286714001SSerapheim Dimitropoulos  * Import the checkpointed state of the pool specified by the target
434386714001SSerapheim Dimitropoulos  * parameter as readonly. The function also accepts a pool config
434486714001SSerapheim Dimitropoulos  * as an optional parameter, else it attempts to infer the config by
434586714001SSerapheim Dimitropoulos  * the name of the target pool.
434686714001SSerapheim Dimitropoulos  *
434786714001SSerapheim Dimitropoulos  * Note that the checkpointed state's pool name will be the name of
434886714001SSerapheim Dimitropoulos  * the original pool with the above suffix appened to it. In addition,
434986714001SSerapheim Dimitropoulos  * if the target is not a pool name (e.g. a path to a dataset) then
435086714001SSerapheim Dimitropoulos  * the new_path parameter is populated with the updated path to
435186714001SSerapheim Dimitropoulos  * reflect the fact that we are looking into the checkpointed state.
435286714001SSerapheim Dimitropoulos  *
435386714001SSerapheim Dimitropoulos  * The function returns a newly-allocated copy of the name of the
435486714001SSerapheim Dimitropoulos  * pool containing the checkpointed state. When this copy is no
435586714001SSerapheim Dimitropoulos  * longer needed it should be freed with free(3C). Same thing
435686714001SSerapheim Dimitropoulos  * applies to the new_path parameter if allocated.
435786714001SSerapheim Dimitropoulos  */
435886714001SSerapheim Dimitropoulos static char *
435986714001SSerapheim Dimitropoulos import_checkpointed_state(char *target, nvlist_t *cfg, char **new_path)
436086714001SSerapheim Dimitropoulos {
436186714001SSerapheim Dimitropoulos 	int error = 0;
436286714001SSerapheim Dimitropoulos 	char *poolname, *bogus_name;
436386714001SSerapheim Dimitropoulos 
436486714001SSerapheim Dimitropoulos 	/* If the target is not a pool, the extract the pool name */
436586714001SSerapheim Dimitropoulos 	char *path_start = strchr(target, '/');
436686714001SSerapheim Dimitropoulos 	if (path_start != NULL) {
436786714001SSerapheim Dimitropoulos 		size_t poolname_len = path_start - target;
436886714001SSerapheim Dimitropoulos 		poolname = strndup(target, poolname_len);
436986714001SSerapheim Dimitropoulos 	} else {
437086714001SSerapheim Dimitropoulos 		poolname = target;
437186714001SSerapheim Dimitropoulos 	}
437286714001SSerapheim Dimitropoulos 
437386714001SSerapheim Dimitropoulos 	if (cfg == NULL) {
4374e0f1c0afSOlaf Faaland 		zdb_set_skip_mmp(poolname);
437586714001SSerapheim Dimitropoulos 		error = spa_get_stats(poolname, &cfg, NULL, 0);
437686714001SSerapheim Dimitropoulos 		if (error != 0) {
437786714001SSerapheim Dimitropoulos 			fatal("Tried to read config of pool \"%s\" but "
437886714001SSerapheim Dimitropoulos 			    "spa_get_stats() failed with error %d\n",
437986714001SSerapheim Dimitropoulos 			    poolname, error);
438086714001SSerapheim Dimitropoulos 		}
438186714001SSerapheim Dimitropoulos 	}
438286714001SSerapheim Dimitropoulos 
438386714001SSerapheim Dimitropoulos 	(void) asprintf(&bogus_name, "%s%s", poolname, BOGUS_SUFFIX);
438486714001SSerapheim Dimitropoulos 	fnvlist_add_string(cfg, ZPOOL_CONFIG_POOL_NAME, bogus_name);
438586714001SSerapheim Dimitropoulos 
438686714001SSerapheim Dimitropoulos 	error = spa_import(bogus_name, cfg, NULL,
4387e0f1c0afSOlaf Faaland 	    ZFS_IMPORT_MISSING_LOG | ZFS_IMPORT_CHECKPOINT |
4388e0f1c0afSOlaf Faaland 	    ZFS_IMPORT_SKIP_MMP);
438986714001SSerapheim Dimitropoulos 	if (error != 0) {
439086714001SSerapheim Dimitropoulos 		fatal("Tried to import pool \"%s\" but spa_import() failed "
439186714001SSerapheim Dimitropoulos 		    "with error %d\n", bogus_name, error);
439286714001SSerapheim Dimitropoulos 	}
439386714001SSerapheim Dimitropoulos 
439486714001SSerapheim Dimitropoulos 	if (new_path != NULL && path_start != NULL)
439586714001SSerapheim Dimitropoulos 		(void) asprintf(new_path, "%s%s", bogus_name, path_start);
439686714001SSerapheim Dimitropoulos 
439786714001SSerapheim Dimitropoulos 	if (target != poolname)
439886714001SSerapheim Dimitropoulos 		free(poolname);
439986714001SSerapheim Dimitropoulos 
440086714001SSerapheim Dimitropoulos 	return (bogus_name);
440186714001SSerapheim Dimitropoulos }
440286714001SSerapheim Dimitropoulos 
440386714001SSerapheim Dimitropoulos typedef struct verify_checkpoint_sm_entry_cb_arg {
440486714001SSerapheim Dimitropoulos 	vdev_t *vcsec_vd;
440586714001SSerapheim Dimitropoulos 
440686714001SSerapheim Dimitropoulos 	/* the following fields are only used for printing progress */
440786714001SSerapheim Dimitropoulos 	uint64_t vcsec_entryid;
440886714001SSerapheim Dimitropoulos 	uint64_t vcsec_num_entries;
440986714001SSerapheim Dimitropoulos } verify_checkpoint_sm_entry_cb_arg_t;
441086714001SSerapheim Dimitropoulos 
441186714001SSerapheim Dimitropoulos #define	ENTRIES_PER_PROGRESS_UPDATE 10000
441286714001SSerapheim Dimitropoulos 
441386714001SSerapheim Dimitropoulos static int
441417f11284SSerapheim Dimitropoulos verify_checkpoint_sm_entry_cb(space_map_entry_t *sme, void *arg)
441586714001SSerapheim Dimitropoulos {
441686714001SSerapheim Dimitropoulos 	verify_checkpoint_sm_entry_cb_arg_t *vcsec = arg;
441786714001SSerapheim Dimitropoulos 	vdev_t *vd = vcsec->vcsec_vd;
441817f11284SSerapheim Dimitropoulos 	metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift];
441917f11284SSerapheim Dimitropoulos 	uint64_t end = sme->sme_offset + sme->sme_run;
442086714001SSerapheim Dimitropoulos 
442117f11284SSerapheim Dimitropoulos 	ASSERT(sme->sme_type == SM_FREE);
442286714001SSerapheim Dimitropoulos 
442386714001SSerapheim Dimitropoulos 	if ((vcsec->vcsec_entryid % ENTRIES_PER_PROGRESS_UPDATE) == 0) {
442486714001SSerapheim Dimitropoulos 		(void) fprintf(stderr,
442586714001SSerapheim Dimitropoulos 		    "\rverifying vdev %llu, space map entry %llu of %llu ...",
442686714001SSerapheim Dimitropoulos 		    (longlong_t)vd->vdev_id,
442786714001SSerapheim Dimitropoulos 		    (longlong_t)vcsec->vcsec_entryid,
442886714001SSerapheim Dimitropoulos 		    (longlong_t)vcsec->vcsec_num_entries);
442986714001SSerapheim Dimitropoulos 	}
443086714001SSerapheim Dimitropoulos 	vcsec->vcsec_entryid++;
443186714001SSerapheim Dimitropoulos 
443286714001SSerapheim Dimitropoulos 	/*
443386714001SSerapheim Dimitropoulos 	 * See comment in checkpoint_sm_exclude_entry_cb()
443486714001SSerapheim Dimitropoulos 	 */
443517f11284SSerapheim Dimitropoulos 	VERIFY3U(sme->sme_offset, >=, ms->ms_start);
443686714001SSerapheim Dimitropoulos 	VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
443786714001SSerapheim Dimitropoulos 
443886714001SSerapheim Dimitropoulos 	/*
443986714001SSerapheim Dimitropoulos 	 * The entries in the vdev_checkpoint_sm should be marked as
444086714001SSerapheim Dimitropoulos 	 * allocated in the checkpointed state of the pool, therefore
444186714001SSerapheim Dimitropoulos 	 * their respective ms_allocateable trees should not contain them.
444286714001SSerapheim Dimitropoulos 	 */
444386714001SSerapheim Dimitropoulos 	mutex_enter(&ms->ms_lock);
4444555d674dSSerapheim Dimitropoulos 	range_tree_verify_not_present(ms->ms_allocatable,
4445555d674dSSerapheim Dimitropoulos 	    sme->sme_offset, sme->sme_run);
444686714001SSerapheim Dimitropoulos 	mutex_exit(&ms->ms_lock);
444786714001SSerapheim Dimitropoulos 
444886714001SSerapheim Dimitropoulos 	return (0);
444986714001SSerapheim Dimitropoulos }
445086714001SSerapheim Dimitropoulos 
445186714001SSerapheim Dimitropoulos /*
445286714001SSerapheim Dimitropoulos  * Verify that all segments in the vdev_checkpoint_sm are allocated
445386714001SSerapheim Dimitropoulos  * according to the checkpoint's ms_sm (i.e. are not in the checkpoint's
445486714001SSerapheim Dimitropoulos  * ms_allocatable).
445586714001SSerapheim Dimitropoulos  *
445686714001SSerapheim Dimitropoulos  * Do so by comparing the checkpoint space maps (vdev_checkpoint_sm) of
445786714001SSerapheim Dimitropoulos  * each vdev in the current state of the pool to the metaslab space maps
445886714001SSerapheim Dimitropoulos  * (ms_sm) of the checkpointed state of the pool.
445986714001SSerapheim Dimitropoulos  *
446086714001SSerapheim Dimitropoulos  * Note that the function changes the state of the ms_allocatable
446186714001SSerapheim Dimitropoulos  * trees of the current spa_t. The entries of these ms_allocatable
446286714001SSerapheim Dimitropoulos  * trees are cleared out and then repopulated from with the free
446386714001SSerapheim Dimitropoulos  * entries of their respective ms_sm space maps.
446486714001SSerapheim Dimitropoulos  */
446586714001SSerapheim Dimitropoulos static void
446686714001SSerapheim Dimitropoulos verify_checkpoint_vdev_spacemaps(spa_t *checkpoint, spa_t *current)
446786714001SSerapheim Dimitropoulos {
446886714001SSerapheim Dimitropoulos 	vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
446986714001SSerapheim Dimitropoulos 	vdev_t *current_rvd = current->spa_root_vdev;
447086714001SSerapheim Dimitropoulos 
447186714001SSerapheim Dimitropoulos 	load_concrete_ms_allocatable_trees(checkpoint, SM_FREE);
447286714001SSerapheim Dimitropoulos 
447386714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < ckpoint_rvd->vdev_children; c++) {
447486714001SSerapheim Dimitropoulos 		vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[c];
447586714001SSerapheim Dimitropoulos 		vdev_t *current_vd = current_rvd->vdev_child[c];
447686714001SSerapheim Dimitropoulos 
447786714001SSerapheim Dimitropoulos 		space_map_t *checkpoint_sm = NULL;
447886714001SSerapheim Dimitropoulos 		uint64_t checkpoint_sm_obj;
447986714001SSerapheim Dimitropoulos 
448086714001SSerapheim Dimitropoulos 		if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
448186714001SSerapheim Dimitropoulos 			/*
448286714001SSerapheim Dimitropoulos 			 * Since we don't allow device removal in a pool
448386714001SSerapheim Dimitropoulos 			 * that has a checkpoint, we expect that all removed
448486714001SSerapheim Dimitropoulos 			 * vdevs were removed from the pool before the
448586714001SSerapheim Dimitropoulos 			 * checkpoint.
448686714001SSerapheim Dimitropoulos 			 */
448786714001SSerapheim Dimitropoulos 			ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
448886714001SSerapheim Dimitropoulos 			continue;
448986714001SSerapheim Dimitropoulos 		}
449086714001SSerapheim Dimitropoulos 
449186714001SSerapheim Dimitropoulos 		/*
449286714001SSerapheim Dimitropoulos 		 * If the checkpoint space map doesn't exist, then nothing
449386714001SSerapheim Dimitropoulos 		 * here is checkpointed so there's nothing to verify.
449486714001SSerapheim Dimitropoulos 		 */
449586714001SSerapheim Dimitropoulos 		if (current_vd->vdev_top_zap == 0 ||
449686714001SSerapheim Dimitropoulos 		    zap_contains(spa_meta_objset(current),
449786714001SSerapheim Dimitropoulos 		    current_vd->vdev_top_zap,
449886714001SSerapheim Dimitropoulos 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
449986714001SSerapheim Dimitropoulos 			continue;
450086714001SSerapheim Dimitropoulos 
450186714001SSerapheim Dimitropoulos 		VERIFY0(zap_lookup(spa_meta_objset(current),
450286714001SSerapheim Dimitropoulos 		    current_vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
450386714001SSerapheim Dimitropoulos 		    sizeof (uint64_t), 1, &checkpoint_sm_obj));
450486714001SSerapheim Dimitropoulos 
450586714001SSerapheim Dimitropoulos 		VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(current),
450686714001SSerapheim Dimitropoulos 		    checkpoint_sm_obj, 0, current_vd->vdev_asize,
450786714001SSerapheim Dimitropoulos 		    current_vd->vdev_ashift));
450886714001SSerapheim Dimitropoulos 
450986714001SSerapheim Dimitropoulos 		verify_checkpoint_sm_entry_cb_arg_t vcsec;
451086714001SSerapheim Dimitropoulos 		vcsec.vcsec_vd = ckpoint_vd;
451186714001SSerapheim Dimitropoulos 		vcsec.vcsec_entryid = 0;
451286714001SSerapheim Dimitropoulos 		vcsec.vcsec_num_entries =
451386714001SSerapheim Dimitropoulos 		    space_map_length(checkpoint_sm) / sizeof (uint64_t);
451486714001SSerapheim Dimitropoulos 		VERIFY0(space_map_iterate(checkpoint_sm,
4515555d674dSSerapheim Dimitropoulos 		    space_map_length(checkpoint_sm),
451686714001SSerapheim Dimitropoulos 		    verify_checkpoint_sm_entry_cb, &vcsec));
451786714001SSerapheim Dimitropoulos 		dump_spacemap(current->spa_meta_objset, checkpoint_sm);
451886714001SSerapheim Dimitropoulos 		space_map_close(checkpoint_sm);
451986714001SSerapheim Dimitropoulos 	}
452086714001SSerapheim Dimitropoulos 
452186714001SSerapheim Dimitropoulos 	/*
452286714001SSerapheim Dimitropoulos 	 * If we've added vdevs since we took the checkpoint, ensure
452386714001SSerapheim Dimitropoulos 	 * that their checkpoint space maps are empty.
452486714001SSerapheim Dimitropoulos 	 */
452586714001SSerapheim Dimitropoulos 	if (ckpoint_rvd->vdev_children < current_rvd->vdev_children) {
452686714001SSerapheim Dimitropoulos 		for (uint64_t c = ckpoint_rvd->vdev_children;
452786714001SSerapheim Dimitropoulos 		    c < current_rvd->vdev_children; c++) {
452886714001SSerapheim Dimitropoulos 			vdev_t *current_vd = current_rvd->vdev_child[c];
452986714001SSerapheim Dimitropoulos 			ASSERT3P(current_vd->vdev_checkpoint_sm, ==, NULL);
453086714001SSerapheim Dimitropoulos 		}
453186714001SSerapheim Dimitropoulos 	}
453286714001SSerapheim Dimitropoulos 
453386714001SSerapheim Dimitropoulos 	/* for cleaner progress output */
453486714001SSerapheim Dimitropoulos 	(void) fprintf(stderr, "\n");
453586714001SSerapheim Dimitropoulos }
453686714001SSerapheim Dimitropoulos 
453786714001SSerapheim Dimitropoulos /*
453886714001SSerapheim Dimitropoulos  * Verifies that all space that's allocated in the checkpoint is
453986714001SSerapheim Dimitropoulos  * still allocated in the current version, by checking that everything
454086714001SSerapheim Dimitropoulos  * in checkpoint's ms_allocatable (which is actually allocated, not
454186714001SSerapheim Dimitropoulos  * allocatable/free) is not present in current's ms_allocatable.
454286714001SSerapheim Dimitropoulos  *
454386714001SSerapheim Dimitropoulos  * Note that the function changes the state of the ms_allocatable
454486714001SSerapheim Dimitropoulos  * trees of both spas when called. The entries of all ms_allocatable
454586714001SSerapheim Dimitropoulos  * trees are cleared out and then repopulated from their respective
454686714001SSerapheim Dimitropoulos  * ms_sm space maps. In the checkpointed state we load the allocated
454786714001SSerapheim Dimitropoulos  * entries, and in the current state we load the free entries.
454886714001SSerapheim Dimitropoulos  */
454986714001SSerapheim Dimitropoulos static void
455086714001SSerapheim Dimitropoulos verify_checkpoint_ms_spacemaps(spa_t *checkpoint, spa_t *current)
455186714001SSerapheim Dimitropoulos {
455286714001SSerapheim Dimitropoulos 	vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
455386714001SSerapheim Dimitropoulos 	vdev_t *current_rvd = current->spa_root_vdev;
455486714001SSerapheim Dimitropoulos 
455586714001SSerapheim Dimitropoulos 	load_concrete_ms_allocatable_trees(checkpoint, SM_ALLOC);
455686714001SSerapheim Dimitropoulos 	load_concrete_ms_allocatable_trees(current, SM_FREE);
455786714001SSerapheim Dimitropoulos 
455886714001SSerapheim Dimitropoulos 	for (uint64_t i = 0; i < ckpoint_rvd->vdev_children; i++) {
455986714001SSerapheim Dimitropoulos 		vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[i];
456086714001SSerapheim Dimitropoulos 		vdev_t *current_vd = current_rvd->vdev_child[i];
456186714001SSerapheim Dimitropoulos 
456286714001SSerapheim Dimitropoulos 		if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
456386714001SSerapheim Dimitropoulos 			/*
456486714001SSerapheim Dimitropoulos 			 * See comment in verify_checkpoint_vdev_spacemaps()
456586714001SSerapheim Dimitropoulos 			 */
456686714001SSerapheim Dimitropoulos 			ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
456786714001SSerapheim Dimitropoulos 			continue;
456886714001SSerapheim Dimitropoulos 		}
456986714001SSerapheim Dimitropoulos 
457086714001SSerapheim Dimitropoulos 		for (uint64_t m = 0; m < ckpoint_vd->vdev_ms_count; m++) {
457186714001SSerapheim Dimitropoulos 			metaslab_t *ckpoint_msp = ckpoint_vd->vdev_ms[m];
457286714001SSerapheim Dimitropoulos 			metaslab_t *current_msp = current_vd->vdev_ms[m];
457386714001SSerapheim Dimitropoulos 
457486714001SSerapheim Dimitropoulos 			(void) fprintf(stderr,
457586714001SSerapheim Dimitropoulos 			    "\rverifying vdev %llu of %llu, "
457686714001SSerapheim Dimitropoulos 			    "metaslab %llu of %llu ...",
457786714001SSerapheim Dimitropoulos 			    (longlong_t)current_vd->vdev_id,
457886714001SSerapheim Dimitropoulos 			    (longlong_t)current_rvd->vdev_children,
457986714001SSerapheim Dimitropoulos 			    (longlong_t)current_vd->vdev_ms[m]->ms_id,
458086714001SSerapheim Dimitropoulos 			    (longlong_t)current_vd->vdev_ms_count);
458186714001SSerapheim Dimitropoulos 
458286714001SSerapheim Dimitropoulos 			/*
458386714001SSerapheim Dimitropoulos 			 * We walk through the ms_allocatable trees that
458486714001SSerapheim Dimitropoulos 			 * are loaded with the allocated blocks from the
458586714001SSerapheim Dimitropoulos 			 * ms_sm spacemaps of the checkpoint. For each
458686714001SSerapheim Dimitropoulos 			 * one of these ranges we ensure that none of them
458786714001SSerapheim Dimitropoulos 			 * exists in the ms_allocatable trees of the
458886714001SSerapheim Dimitropoulos 			 * current state which are loaded with the ranges
458986714001SSerapheim Dimitropoulos 			 * that are currently free.
459086714001SSerapheim Dimitropoulos 			 *
459186714001SSerapheim Dimitropoulos 			 * This way we ensure that none of the blocks that
459286714001SSerapheim Dimitropoulos 			 * are part of the checkpoint were freed by mistake.
459386714001SSerapheim Dimitropoulos 			 */
459486714001SSerapheim Dimitropoulos 			range_tree_walk(ckpoint_msp->ms_allocatable,
4595555d674dSSerapheim Dimitropoulos 			    (range_tree_func_t *)range_tree_verify_not_present,
459686714001SSerapheim Dimitropoulos 			    current_msp->ms_allocatable);
459786714001SSerapheim Dimitropoulos 		}
459886714001SSerapheim Dimitropoulos 	}
459986714001SSerapheim Dimitropoulos 
460086714001SSerapheim Dimitropoulos 	/* for cleaner progress output */
460186714001SSerapheim Dimitropoulos 	(void) fprintf(stderr, "\n");
460286714001SSerapheim Dimitropoulos }
460386714001SSerapheim Dimitropoulos 
460486714001SSerapheim Dimitropoulos static void
460586714001SSerapheim Dimitropoulos verify_checkpoint_blocks(spa_t *spa)
460686714001SSerapheim Dimitropoulos {
4607555d674dSSerapheim Dimitropoulos 	ASSERT(!dump_opt['L']);
4608555d674dSSerapheim Dimitropoulos 
460986714001SSerapheim Dimitropoulos 	spa_t *checkpoint_spa;
461086714001SSerapheim Dimitropoulos 	char *checkpoint_pool;
461186714001SSerapheim Dimitropoulos 	nvlist_t *config = NULL;
461286714001SSerapheim Dimitropoulos 	int error = 0;
461386714001SSerapheim Dimitropoulos 
461486714001SSerapheim Dimitropoulos 	/*
461586714001SSerapheim Dimitropoulos 	 * We import the checkpointed state of the pool (under a different
461686714001SSerapheim Dimitropoulos 	 * name) so we can do verification on it against the current state
461786714001SSerapheim Dimitropoulos 	 * of the pool.
461886714001SSerapheim Dimitropoulos 	 */
461986714001SSerapheim Dimitropoulos 	checkpoint_pool = import_checkpointed_state(spa->spa_name, config,
462086714001SSerapheim Dimitropoulos 	    NULL);
462186714001SSerapheim Dimitropoulos 	ASSERT(strcmp(spa->spa_name, checkpoint_pool) != 0);
462286714001SSerapheim Dimitropoulos 
462386714001SSerapheim Dimitropoulos 	error = spa_open(checkpoint_pool, &checkpoint_spa, FTAG);
462486714001SSerapheim Dimitropoulos 	if (error != 0) {
462586714001SSerapheim Dimitropoulos 		fatal("Tried to open pool \"%s\" but spa_open() failed with "
462686714001SSerapheim Dimitropoulos 		    "error %d\n", checkpoint_pool, error);
462786714001SSerapheim Dimitropoulos 	}
462886714001SSerapheim Dimitropoulos 
462986714001SSerapheim Dimitropoulos 	/*
463086714001SSerapheim Dimitropoulos 	 * Ensure that ranges in the checkpoint space maps of each vdev
463186714001SSerapheim Dimitropoulos 	 * are allocated according to the checkpointed state's metaslab
463286714001SSerapheim Dimitropoulos 	 * space maps.
463386714001SSerapheim Dimitropoulos 	 */
463486714001SSerapheim Dimitropoulos 	verify_checkpoint_vdev_spacemaps(checkpoint_spa, spa);
463586714001SSerapheim Dimitropoulos 
463686714001SSerapheim Dimitropoulos 	/*
463786714001SSerapheim Dimitropoulos 	 * Ensure that allocated ranges in the checkpoint's metaslab
463886714001SSerapheim Dimitropoulos 	 * space maps remain allocated in the metaslab space maps of
463986714001SSerapheim Dimitropoulos 	 * the current state.
464086714001SSerapheim Dimitropoulos 	 */
464186714001SSerapheim Dimitropoulos 	verify_checkpoint_ms_spacemaps(checkpoint_spa, spa);
464286714001SSerapheim Dimitropoulos 
464386714001SSerapheim Dimitropoulos 	/*
464486714001SSerapheim Dimitropoulos 	 * Once we are done, we get rid of the checkpointed state.
464586714001SSerapheim Dimitropoulos 	 */
464686714001SSerapheim Dimitropoulos 	spa_close(checkpoint_spa, FTAG);
464786714001SSerapheim Dimitropoulos 	free(checkpoint_pool);
464886714001SSerapheim Dimitropoulos }
464986714001SSerapheim Dimitropoulos 
465086714001SSerapheim Dimitropoulos static void
465186714001SSerapheim Dimitropoulos dump_leftover_checkpoint_blocks(spa_t *spa)
465286714001SSerapheim Dimitropoulos {
465386714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
465486714001SSerapheim Dimitropoulos 
465586714001SSerapheim Dimitropoulos 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
465686714001SSerapheim Dimitropoulos 		vdev_t *vd = rvd->vdev_child[i];
465786714001SSerapheim Dimitropoulos 
465886714001SSerapheim Dimitropoulos 		space_map_t *checkpoint_sm = NULL;
465986714001SSerapheim Dimitropoulos 		uint64_t checkpoint_sm_obj;
466086714001SSerapheim Dimitropoulos 
466186714001SSerapheim Dimitropoulos 		if (vd->vdev_top_zap == 0)
466286714001SSerapheim Dimitropoulos 			continue;
466386714001SSerapheim Dimitropoulos 
466486714001SSerapheim Dimitropoulos 		if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
466586714001SSerapheim Dimitropoulos 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
466686714001SSerapheim Dimitropoulos 			continue;
466786714001SSerapheim Dimitropoulos 
466886714001SSerapheim Dimitropoulos 		VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
466986714001SSerapheim Dimitropoulos 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
467086714001SSerapheim Dimitropoulos 		    sizeof (uint64_t), 1, &checkpoint_sm_obj));
467186714001SSerapheim Dimitropoulos 
467286714001SSerapheim Dimitropoulos 		VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
467386714001SSerapheim Dimitropoulos 		    checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
467486714001SSerapheim Dimitropoulos 		dump_spacemap(spa->spa_meta_objset, checkpoint_sm);
467586714001SSerapheim Dimitropoulos 		space_map_close(checkpoint_sm);
467686714001SSerapheim Dimitropoulos 	}
467786714001SSerapheim Dimitropoulos }
467886714001SSerapheim Dimitropoulos 
467986714001SSerapheim Dimitropoulos static int
468086714001SSerapheim Dimitropoulos verify_checkpoint(spa_t *spa)
468186714001SSerapheim Dimitropoulos {
468286714001SSerapheim Dimitropoulos 	uberblock_t checkpoint;
468386714001SSerapheim Dimitropoulos 	int error;
468486714001SSerapheim Dimitropoulos 
468586714001SSerapheim Dimitropoulos 	if (!spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT))
468686714001SSerapheim Dimitropoulos 		return (0);
468786714001SSerapheim Dimitropoulos 
468886714001SSerapheim Dimitropoulos 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
468986714001SSerapheim Dimitropoulos 	    DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t),
469086714001SSerapheim Dimitropoulos 	    sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint);
469186714001SSerapheim Dimitropoulos 
469217f11284SSerapheim Dimitropoulos 	if (error == ENOENT && !dump_opt['L']) {
469386714001SSerapheim Dimitropoulos 		/*
469486714001SSerapheim Dimitropoulos 		 * If the feature is active but the uberblock is missing
469586714001SSerapheim Dimitropoulos 		 * then we must be in the middle of discarding the
469686714001SSerapheim Dimitropoulos 		 * checkpoint.
469786714001SSerapheim Dimitropoulos 		 */
469886714001SSerapheim Dimitropoulos 		(void) printf("\nPartially discarded checkpoint "
469986714001SSerapheim Dimitropoulos 		    "state found:\n");
470086714001SSerapheim Dimitropoulos 		dump_leftover_checkpoint_blocks(spa);
470186714001SSerapheim Dimitropoulos 		return (0);
470286714001SSerapheim Dimitropoulos 	} else if (error != 0) {
470386714001SSerapheim Dimitropoulos 		(void) printf("lookup error %d when looking for "
470486714001SSerapheim Dimitropoulos 		    "checkpointed uberblock in MOS\n", error);
470586714001SSerapheim Dimitropoulos 		return (error);
470686714001SSerapheim Dimitropoulos 	}
470786714001SSerapheim Dimitropoulos 	dump_uberblock(&checkpoint, "\nCheckpointed uberblock found:\n", "\n");
470886714001SSerapheim Dimitropoulos 
470986714001SSerapheim Dimitropoulos 	if (checkpoint.ub_checkpoint_txg == 0) {
471086714001SSerapheim Dimitropoulos 		(void) printf("\nub_checkpoint_txg not set in checkpointed "
471186714001SSerapheim Dimitropoulos 		    "uberblock\n");
471286714001SSerapheim Dimitropoulos 		error = 3;
471386714001SSerapheim Dimitropoulos 	}
471486714001SSerapheim Dimitropoulos 
471517f11284SSerapheim Dimitropoulos 	if (error == 0 && !dump_opt['L'])
471686714001SSerapheim Dimitropoulos 		verify_checkpoint_blocks(spa);
471786714001SSerapheim Dimitropoulos 
471886714001SSerapheim Dimitropoulos 	return (error);
471986714001SSerapheim Dimitropoulos }
472086714001SSerapheim Dimitropoulos 
472117fb938fSMatthew Ahrens /* ARGSUSED */
472217fb938fSMatthew Ahrens static void
472317fb938fSMatthew Ahrens mos_leaks_cb(void *arg, uint64_t start, uint64_t size)
472417fb938fSMatthew Ahrens {
472517fb938fSMatthew Ahrens 	for (uint64_t i = start; i < size; i++) {
472617fb938fSMatthew Ahrens 		(void) printf("MOS object %llu referenced but not allocated\n",
472717fb938fSMatthew Ahrens 		    (u_longlong_t)i);
472817fb938fSMatthew Ahrens 	}
472917fb938fSMatthew Ahrens }
473017fb938fSMatthew Ahrens 
473117fb938fSMatthew Ahrens static range_tree_t *mos_refd_objs;
473217fb938fSMatthew Ahrens 
473317fb938fSMatthew Ahrens static void
473417fb938fSMatthew Ahrens mos_obj_refd(uint64_t obj)
473517fb938fSMatthew Ahrens {
473617fb938fSMatthew Ahrens 	if (obj != 0 && mos_refd_objs != NULL)
473717fb938fSMatthew Ahrens 		range_tree_add(mos_refd_objs, obj, 1);
473817fb938fSMatthew Ahrens }
473917fb938fSMatthew Ahrens 
474017fb938fSMatthew Ahrens static void
474117fb938fSMatthew Ahrens mos_leak_vdev(vdev_t *vd)
474217fb938fSMatthew Ahrens {
474317fb938fSMatthew Ahrens 	mos_obj_refd(vd->vdev_dtl_object);
474417fb938fSMatthew Ahrens 	mos_obj_refd(vd->vdev_ms_array);
474517fb938fSMatthew Ahrens 	mos_obj_refd(vd->vdev_top_zap);
474617fb938fSMatthew Ahrens 	mos_obj_refd(vd->vdev_indirect_config.vic_births_object);
474717fb938fSMatthew Ahrens 	mos_obj_refd(vd->vdev_indirect_config.vic_mapping_object);
474817fb938fSMatthew Ahrens 	mos_obj_refd(vd->vdev_leaf_zap);
474917fb938fSMatthew Ahrens 	if (vd->vdev_checkpoint_sm != NULL)
475017fb938fSMatthew Ahrens 		mos_obj_refd(vd->vdev_checkpoint_sm->sm_object);
475117fb938fSMatthew Ahrens 	if (vd->vdev_indirect_mapping != NULL) {
475217fb938fSMatthew Ahrens 		mos_obj_refd(vd->vdev_indirect_mapping->
475317fb938fSMatthew Ahrens 		    vim_phys->vimp_counts_object);
475417fb938fSMatthew Ahrens 	}
475517fb938fSMatthew Ahrens 	if (vd->vdev_obsolete_sm != NULL)
475617fb938fSMatthew Ahrens 		mos_obj_refd(vd->vdev_obsolete_sm->sm_object);
475717fb938fSMatthew Ahrens 
475817fb938fSMatthew Ahrens 	for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
475917fb938fSMatthew Ahrens 		metaslab_t *ms = vd->vdev_ms[m];
476017fb938fSMatthew Ahrens 		mos_obj_refd(space_map_object(ms->ms_sm));
476117fb938fSMatthew Ahrens 	}
476217fb938fSMatthew Ahrens 
476317fb938fSMatthew Ahrens 	for (uint64_t c = 0; c < vd->vdev_children; c++) {
476417fb938fSMatthew Ahrens 		mos_leak_vdev(vd->vdev_child[c]);
476517fb938fSMatthew Ahrens 	}
476617fb938fSMatthew Ahrens }
476717fb938fSMatthew Ahrens 
476817fb938fSMatthew Ahrens static int
476917fb938fSMatthew Ahrens dump_mos_leaks(spa_t *spa)
477017fb938fSMatthew Ahrens {
477117fb938fSMatthew Ahrens 	int rv = 0;
477217fb938fSMatthew Ahrens 	objset_t *mos = spa->spa_meta_objset;
477317fb938fSMatthew Ahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
477417fb938fSMatthew Ahrens 
477517fb938fSMatthew Ahrens 	/* Visit and mark all referenced objects in the MOS */
477617fb938fSMatthew Ahrens 
477717fb938fSMatthew Ahrens 	mos_obj_refd(DMU_POOL_DIRECTORY_OBJECT);
477817fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_pool_props_object);
477917fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_config_object);
478017fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_ddt_stat_object);
478117fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_feat_desc_obj);
478217fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_feat_enabled_txg_obj);
478317fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_feat_for_read_obj);
478417fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_feat_for_write_obj);
478517fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_history);
478617fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_errlog_last);
478717fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_errlog_scrub);
478817fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_all_vdev_zaps);
478917fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_dsl_pool->dp_bptree_obj);
479017fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_dsl_pool->dp_tmp_userrefs_obj);
479117fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_dsl_pool->dp_scan->scn_phys.scn_queue_obj);
479217fb938fSMatthew Ahrens 	bpobj_count_refd(&spa->spa_deferred_bpobj);
479317fb938fSMatthew Ahrens 	mos_obj_refd(dp->dp_empty_bpobj);
479417fb938fSMatthew Ahrens 	bpobj_count_refd(&dp->dp_obsolete_bpobj);
479517fb938fSMatthew Ahrens 	bpobj_count_refd(&dp->dp_free_bpobj);
479617fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_l2cache.sav_object);
479717fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_spares.sav_object);
479817fb938fSMatthew Ahrens 
479917fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_condensing_indirect_phys.
480017fb938fSMatthew Ahrens 	    scip_next_mapping_object);
480117fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_condensing_indirect_phys.
480217fb938fSMatthew Ahrens 	    scip_prev_obsolete_sm_object);
480317fb938fSMatthew Ahrens 	if (spa->spa_condensing_indirect_phys.scip_next_mapping_object != 0) {
480417fb938fSMatthew Ahrens 		vdev_indirect_mapping_t *vim =
480517fb938fSMatthew Ahrens 		    vdev_indirect_mapping_open(mos,
480617fb938fSMatthew Ahrens 		    spa->spa_condensing_indirect_phys.scip_next_mapping_object);
480717fb938fSMatthew Ahrens 		mos_obj_refd(vim->vim_phys->vimp_counts_object);
480817fb938fSMatthew Ahrens 		vdev_indirect_mapping_close(vim);
480917fb938fSMatthew Ahrens 	}
481017fb938fSMatthew Ahrens 
481117fb938fSMatthew Ahrens 	if (dp->dp_origin_snap != NULL) {
481217fb938fSMatthew Ahrens 		dsl_dataset_t *ds;
481317fb938fSMatthew Ahrens 
481417fb938fSMatthew Ahrens 		dsl_pool_config_enter(dp, FTAG);
481517fb938fSMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
481617fb938fSMatthew Ahrens 		    dsl_dataset_phys(dp->dp_origin_snap)->ds_next_snap_obj,
481717fb938fSMatthew Ahrens 		    FTAG, &ds));
481817fb938fSMatthew Ahrens 		count_ds_mos_objects(ds);
481917fb938fSMatthew Ahrens 		dump_deadlist(&ds->ds_deadlist);
482017fb938fSMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
482117fb938fSMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
482217fb938fSMatthew Ahrens 
482317fb938fSMatthew Ahrens 		count_ds_mos_objects(dp->dp_origin_snap);
482417fb938fSMatthew Ahrens 		dump_deadlist(&dp->dp_origin_snap->ds_deadlist);
482517fb938fSMatthew Ahrens 	}
482617fb938fSMatthew Ahrens 	count_dir_mos_objects(dp->dp_mos_dir);
482717fb938fSMatthew Ahrens 	if (dp->dp_free_dir != NULL)
482817fb938fSMatthew Ahrens 		count_dir_mos_objects(dp->dp_free_dir);
482917fb938fSMatthew Ahrens 	if (dp->dp_leak_dir != NULL)
483017fb938fSMatthew Ahrens 		count_dir_mos_objects(dp->dp_leak_dir);
483117fb938fSMatthew Ahrens 
483217fb938fSMatthew Ahrens 	mos_leak_vdev(spa->spa_root_vdev);
483317fb938fSMatthew Ahrens 
483417fb938fSMatthew Ahrens 	for (uint64_t class = 0; class < DDT_CLASSES; class++) {
483517fb938fSMatthew Ahrens 		for (uint64_t type = 0; type < DDT_TYPES; type++) {
483617fb938fSMatthew Ahrens 			for (uint64_t cksum = 0;
483717fb938fSMatthew Ahrens 			    cksum < ZIO_CHECKSUM_FUNCTIONS; cksum++) {
483817fb938fSMatthew Ahrens 				ddt_t *ddt = spa->spa_ddt[cksum];
483917fb938fSMatthew Ahrens 				mos_obj_refd(ddt->ddt_object[type][class]);
484017fb938fSMatthew Ahrens 			}
484117fb938fSMatthew Ahrens 		}
484217fb938fSMatthew Ahrens 	}
484317fb938fSMatthew Ahrens 
484417fb938fSMatthew Ahrens 	/*
484517fb938fSMatthew Ahrens 	 * Visit all allocated objects and make sure they are referenced.
484617fb938fSMatthew Ahrens 	 */
484717fb938fSMatthew Ahrens 	uint64_t object = 0;
484817fb938fSMatthew Ahrens 	while (dmu_object_next(mos, &object, B_FALSE, 0) == 0) {
484917fb938fSMatthew Ahrens 		if (range_tree_contains(mos_refd_objs, object, 1)) {
485017fb938fSMatthew Ahrens 			range_tree_remove(mos_refd_objs, object, 1);
485117fb938fSMatthew Ahrens 		} else {
485217fb938fSMatthew Ahrens 			dmu_object_info_t doi;
485317fb938fSMatthew Ahrens 			const char *name;
485417fb938fSMatthew Ahrens 			dmu_object_info(mos, object, &doi);
485517fb938fSMatthew Ahrens 			if (doi.doi_type & DMU_OT_NEWTYPE) {
485617fb938fSMatthew Ahrens 				dmu_object_byteswap_t bswap =
485717fb938fSMatthew Ahrens 				    DMU_OT_BYTESWAP(doi.doi_type);
485817fb938fSMatthew Ahrens 				name = dmu_ot_byteswap[bswap].ob_name;
485917fb938fSMatthew Ahrens 			} else {
486017fb938fSMatthew Ahrens 				name = dmu_ot[doi.doi_type].ot_name;
486117fb938fSMatthew Ahrens 			}
486217fb938fSMatthew Ahrens 
486317fb938fSMatthew Ahrens 			(void) printf("MOS object %llu (%s) leaked\n",
486417fb938fSMatthew Ahrens 			    (u_longlong_t)object, name);
486517fb938fSMatthew Ahrens 			rv = 2;
486617fb938fSMatthew Ahrens 		}
486717fb938fSMatthew Ahrens 	}
486817fb938fSMatthew Ahrens 	(void) range_tree_walk(mos_refd_objs, mos_leaks_cb, NULL);
486917fb938fSMatthew Ahrens 	if (!range_tree_is_empty(mos_refd_objs))
487017fb938fSMatthew Ahrens 		rv = 2;
487117fb938fSMatthew Ahrens 	range_tree_vacate(mos_refd_objs, NULL, NULL);
487217fb938fSMatthew Ahrens 	range_tree_destroy(mos_refd_objs);
487317fb938fSMatthew Ahrens 	return (rv);
487417fb938fSMatthew Ahrens }
487517fb938fSMatthew Ahrens 
4876fa9e4066Sahrens static void
4877fa9e4066Sahrens dump_zpool(spa_t *spa)
4878fa9e4066Sahrens {
4879fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
4880fa9e4066Sahrens 	int rc = 0;
4881fa9e4066Sahrens 
4882b24ab676SJeff Bonwick 	if (dump_opt['S']) {
4883b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
4884b24ab676SJeff Bonwick 		return;
4885b24ab676SJeff Bonwick 	}
4886b24ab676SJeff Bonwick 
488707428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
488807428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
488907428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
489007428bdfSVictor Latushkin 	}
489107428bdfSVictor Latushkin 
489207428bdfSVictor Latushkin 	if (dump_opt['C'])
489307428bdfSVictor Latushkin 		dump_config(spa);
489407428bdfSVictor Latushkin 
4895fa9e4066Sahrens 	if (dump_opt['u'])
489653b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
4897fa9e4066Sahrens 
4898b24ab676SJeff Bonwick 	if (dump_opt['D'])
4899b24ab676SJeff Bonwick 		dump_all_ddts(spa);
4900b24ab676SJeff Bonwick 
490187219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
490287219db7SVictor Latushkin 		dump_metaslabs(spa);
49032e4c9986SGeorge Wilson 	if (dump_opt['M'])
49042e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
490587219db7SVictor Latushkin 
490687219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
490717fb938fSMatthew Ahrens 		mos_refd_objs = range_tree_create(NULL, NULL);
4908fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
490917fb938fSMatthew Ahrens 
4910fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
49115cabbc6bSPrashanth Sreenivasa 			dsl_pool_t *dp = spa->spa_dsl_pool;
4912732885fcSMatthew Ahrens 			dump_full_bpobj(&spa->spa_deferred_bpobj,
4913d0475637SMatthew Ahrens 			    "Deferred frees", 0);
4914cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
49155cabbc6bSPrashanth Sreenivasa 				dump_full_bpobj(&dp->dp_free_bpobj,
4916d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
4917ad135b5dSChristopher Siden 			}
49185cabbc6bSPrashanth Sreenivasa 			if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
49195cabbc6bSPrashanth Sreenivasa 				ASSERT(spa_feature_is_enabled(spa,
49205cabbc6bSPrashanth Sreenivasa 				    SPA_FEATURE_DEVICE_REMOVAL));
49215cabbc6bSPrashanth Sreenivasa 				dump_full_bpobj(&dp->dp_obsolete_bpobj,
49225cabbc6bSPrashanth Sreenivasa 				    "Pool obsolete blocks", 0);
49235cabbc6bSPrashanth Sreenivasa 			}
4924ad135b5dSChristopher Siden 
4925ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
49262acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
4927ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
49285cabbc6bSPrashanth Sreenivasa 				    dp->dp_bptree_obj,
4929ad135b5dSChristopher Siden 				    "Pool dataset frees");
4930cde58dbcSMatthew Ahrens 			}
4931fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
4932fa9e4066Sahrens 		}
493307428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
493407428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
4935b5152584SMatthew Ahrens 
493617fb938fSMatthew Ahrens 		if (rc == 0 && !dump_opt['L'])
493717fb938fSMatthew Ahrens 			rc = dump_mos_leaks(spa);
493817fb938fSMatthew Ahrens 
4939ca0cc391SMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
4940ca0cc391SMatthew Ahrens 			uint64_t refcount;
4941ca0cc391SMatthew Ahrens 
4942ca0cc391SMatthew Ahrens 			if (!(spa_feature_table[f].fi_flags &
4943c5d1600cSMatthew Ahrens 			    ZFEATURE_FLAG_PER_DATASET) ||
4944c5d1600cSMatthew Ahrens 			    !spa_feature_is_enabled(spa, f)) {
4945ca0cc391SMatthew Ahrens 				ASSERT0(dataset_feature_count[f]);
4946ca0cc391SMatthew Ahrens 				continue;
4947ca0cc391SMatthew Ahrens 			}
4948ca0cc391SMatthew Ahrens 			(void) feature_get_refcount(spa,
4949ca0cc391SMatthew Ahrens 			    &spa_feature_table[f], &refcount);
4950ca0cc391SMatthew Ahrens 			if (dataset_feature_count[f] != refcount) {
4951ca0cc391SMatthew Ahrens 				(void) printf("%s feature refcount mismatch: "
4952ca0cc391SMatthew Ahrens 				    "%lld datasets != %lld refcount\n",
4953ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
4954ca0cc391SMatthew Ahrens 				    (longlong_t)dataset_feature_count[f],
4955ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
4956ca0cc391SMatthew Ahrens 				rc = 2;
4957ca0cc391SMatthew Ahrens 			} else {
4958ca0cc391SMatthew Ahrens 				(void) printf("Verified %s feature refcount "
4959ca0cc391SMatthew Ahrens 				    "of %llu is correct\n",
4960ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
4961ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
4962ca0cc391SMatthew Ahrens 			}
4963b5152584SMatthew Ahrens 		}
49645cabbc6bSPrashanth Sreenivasa 
49655cabbc6bSPrashanth Sreenivasa 		if (rc == 0) {
49665cabbc6bSPrashanth Sreenivasa 			rc = verify_device_removal_feature_counts(spa);
49675cabbc6bSPrashanth Sreenivasa 		}
4968fa9e4066Sahrens 	}
496917fb938fSMatthew Ahrens 
4970b5152584SMatthew Ahrens 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
4971fa9e4066Sahrens 		rc = dump_block_stats(spa);
4972fa9e4066Sahrens 
49730713e232SGeorge Wilson 	if (rc == 0)
49740713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
49750713e232SGeorge Wilson 
4976fa9e4066Sahrens 	if (dump_opt['s'])
4977fa9e4066Sahrens 		show_pool_stats(spa);
4978fa9e4066Sahrens 
49798f18d1faSGeorge Wilson 	if (dump_opt['h'])
49808f18d1faSGeorge Wilson 		dump_history(spa);
49818f18d1faSGeorge Wilson 
498217f11284SSerapheim Dimitropoulos 	if (rc == 0)
498386714001SSerapheim Dimitropoulos 		rc = verify_checkpoint(spa);
498486714001SSerapheim Dimitropoulos 
498529bdd2f9SPavel Zakharov 	if (rc != 0) {
498629bdd2f9SPavel Zakharov 		dump_debug_buffer();
4987fa9e4066Sahrens 		exit(rc);
498829bdd2f9SPavel Zakharov 	}
4989fa9e4066Sahrens }
4990fa9e4066Sahrens 
499144cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
499244cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
499344cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
499444cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
499544cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
499644cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
499744cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
499844cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
499944cd46caSbillm 
50003f7978d0SAlan Somers static int flagbits[256];
500144cd46caSbillm 
500244cd46caSbillm static void
500344cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
500444cd46caSbillm {
5005b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
500644cd46caSbillm 
500744cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
500844cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
5009b24ab676SJeff Bonwick 
501043466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
5011b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
501244cd46caSbillm }
501344cd46caSbillm 
501444cd46caSbillm static void
501544cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
501644cd46caSbillm {
501744cd46caSbillm 	int i;
501844cd46caSbillm 
501944cd46caSbillm 	for (i = 0; i < nbps; i++)
502044cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
502144cd46caSbillm }
502244cd46caSbillm 
502344cd46caSbillm static void
502444cd46caSbillm zdb_dump_gbh(void *buf, int flags)
502544cd46caSbillm {
502644cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
502744cd46caSbillm }
502844cd46caSbillm 
502944cd46caSbillm static void
503044cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
503144cd46caSbillm {
503244cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
503344cd46caSbillm 		byteswap_uint64_array(buf, size);
5034b24ab676SJeff Bonwick 	(void) write(1, buf, size);
503544cd46caSbillm }
503644cd46caSbillm 
503744cd46caSbillm static void
503844cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
503944cd46caSbillm {
504044cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
50413f7978d0SAlan Somers 	unsigned nwords = size / sizeof (uint64_t);
504244cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
50433f7978d0SAlan Somers 	unsigned i, j;
50443f7978d0SAlan Somers 	const char *hdr;
50453f7978d0SAlan Somers 	char *c;
504644cd46caSbillm 
504744cd46caSbillm 
504844cd46caSbillm 	if (do_bswap)
504944cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
505044cd46caSbillm 	else
505144cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
505244cd46caSbillm 
505344cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
505444cd46caSbillm 
505544cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
505644cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
505744cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
505844cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
505944cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
506044cd46caSbillm 
506144cd46caSbillm 		c = (char *)&d[i];
506244cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
506344cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
506444cd46caSbillm 		(void) printf("\n");
506544cd46caSbillm 	}
506644cd46caSbillm }
506744cd46caSbillm 
506844cd46caSbillm /*
506944cd46caSbillm  * There are two acceptable formats:
507044cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
507144cd46caSbillm  *	child[.child]*    - For example: 0.1.1
507244cd46caSbillm  *
507344cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
507444cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
507544cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
507644cd46caSbillm  */
507744cd46caSbillm static vdev_t *
50783f7978d0SAlan Somers zdb_vdev_lookup(vdev_t *vdev, const char *path)
507944cd46caSbillm {
508044cd46caSbillm 	char *s, *p, *q;
50813f7978d0SAlan Somers 	unsigned i;
508244cd46caSbillm 
508344cd46caSbillm 	if (vdev == NULL)
508444cd46caSbillm 		return (NULL);
508544cd46caSbillm 
508644cd46caSbillm 	/* First, assume the x.x.x.x format */
50873f7978d0SAlan Somers 	i = strtoul(path, &s, 10);
508844cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
508944cd46caSbillm 		goto name;
50903f7978d0SAlan Somers 	if (i >= vdev->vdev_children)
509144cd46caSbillm 		return (NULL);
509244cd46caSbillm 
509344cd46caSbillm 	vdev = vdev->vdev_child[i];
509444cd46caSbillm 	if (*s == '\0')
509544cd46caSbillm 		return (vdev);
509644cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
509744cd46caSbillm 
509844cd46caSbillm name:
509944cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
510044cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
510144cd46caSbillm 
510244cd46caSbillm 		if (vc->vdev_path == NULL) {
510344cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
510444cd46caSbillm 			if (vc == NULL)
510544cd46caSbillm 				continue;
510644cd46caSbillm 			else
510744cd46caSbillm 				return (vc);
510844cd46caSbillm 		}
510944cd46caSbillm 
511044cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
511144cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
511244cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
511344cd46caSbillm 
511444cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
511544cd46caSbillm 			return (vc);
511644cd46caSbillm 		if (strcmp(p, path) == 0)
511744cd46caSbillm 			return (vc);
511844cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
511944cd46caSbillm 			return (vc);
512044cd46caSbillm 	}
512144cd46caSbillm 
512244cd46caSbillm 	return (NULL);
512344cd46caSbillm }
512444cd46caSbillm 
5125770499e1SDan Kimmel /* ARGSUSED */
5126770499e1SDan Kimmel static int
5127770499e1SDan Kimmel random_get_pseudo_bytes_cb(void *buf, size_t len, void *unused)
5128770499e1SDan Kimmel {
5129770499e1SDan Kimmel 	return (random_get_pseudo_bytes(buf, len));
5130770499e1SDan Kimmel }
5131770499e1SDan Kimmel 
513244cd46caSbillm /*
513344cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
513444cd46caSbillm  * block descriptor is:
513544cd46caSbillm  *
513644cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
513744cd46caSbillm  *
513844cd46caSbillm  *	pool           - The name of the pool you wish to read from
513944cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
514044cd46caSbillm  *	offset         - offset, in hex, in bytes
514144cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
514244cd46caSbillm  *	flags          - A string of characters specifying options
514344cd46caSbillm  *		 b: Decode a blkptr at given offset within block
514444cd46caSbillm  *		*c: Calculate and display checksums
5145b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
514644cd46caSbillm  *		 e: Byteswap data before dumping
5147b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
5148b24ab676SJeff Bonwick  *		 i: Display as an indirect block
514944cd46caSbillm  *		 p: Do I/O to physical offset
515044cd46caSbillm  *		 r: Dump raw data to stdout
515144cd46caSbillm  *
515244cd46caSbillm  *              * = not yet implemented
515344cd46caSbillm  */
515444cd46caSbillm static void
515507428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
515644cd46caSbillm {
5157b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
5158b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
515944cd46caSbillm 	int flags = 0;
5160b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
516144cd46caSbillm 	zio_t *zio;
516244cd46caSbillm 	vdev_t *vd;
5163770499e1SDan Kimmel 	abd_t *pabd;
5164770499e1SDan Kimmel 	void *lbuf, *buf;
51653f7978d0SAlan Somers 	const char *s, *vdev;
51663f7978d0SAlan Somers 	char *p, *dup, *flagstr;
5167b24ab676SJeff Bonwick 	int i, error;
516844cd46caSbillm 
516944cd46caSbillm 	dup = strdup(thing);
517044cd46caSbillm 	s = strtok(dup, ":");
517144cd46caSbillm 	vdev = s ? s : "";
517244cd46caSbillm 	s = strtok(NULL, ":");
517344cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
517444cd46caSbillm 	s = strtok(NULL, ":");
517544cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
517644cd46caSbillm 	s = strtok(NULL, ":");
51773f7978d0SAlan Somers 	if (s)
51783f7978d0SAlan Somers 		flagstr = strdup(s);
51793f7978d0SAlan Somers 	else
51803f7978d0SAlan Somers 		flagstr = strdup("");
518144cd46caSbillm 
518244cd46caSbillm 	s = NULL;
518344cd46caSbillm 	if (size == 0)
518444cd46caSbillm 		s = "size must not be zero";
518544cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
518644cd46caSbillm 		s = "size must be a multiple of sector size";
518744cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
518844cd46caSbillm 		s = "offset must be a multiple of sector size";
518944cd46caSbillm 	if (s) {
519044cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
519144cd46caSbillm 		free(dup);
519244cd46caSbillm 		return;
519344cd46caSbillm 	}
519444cd46caSbillm 
519544cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
519644cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
51975ad82045Snd 			int bit = flagbits[(uchar_t)flagstr[i]];
519844cd46caSbillm 
519944cd46caSbillm 			if (bit == 0) {
520044cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
520144cd46caSbillm 				    flagstr[i]);
520244cd46caSbillm 				continue;
520344cd46caSbillm 			}
520444cd46caSbillm 			flags |= bit;
520544cd46caSbillm 
520644cd46caSbillm 			/* If it's not something with an argument, keep going */
5207b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
520844cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
520944cd46caSbillm 				continue;
521044cd46caSbillm 
521144cd46caSbillm 			p = &flagstr[i + 1];
521244cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
521344cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
521444cd46caSbillm 			if (*p != ':' && *p != '\0') {
521544cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
521644cd46caSbillm 				free(dup);
521744cd46caSbillm 				return;
521844cd46caSbillm 			}
521944cd46caSbillm 		}
522044cd46caSbillm 	}
52213f7978d0SAlan Somers 	free(flagstr);
522244cd46caSbillm 
522344cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
522444cd46caSbillm 	if (vd == NULL) {
522544cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
522644cd46caSbillm 		free(dup);
522744cd46caSbillm 		return;
522844cd46caSbillm 	} else {
522944cd46caSbillm 		if (vd->vdev_path)
5230b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
5231b24ab676SJeff Bonwick 			    vd->vdev_path);
523244cd46caSbillm 		else
5233b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
523444cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
523544cd46caSbillm 	}
523644cd46caSbillm 
5237b24ab676SJeff Bonwick 	psize = size;
5238b24ab676SJeff Bonwick 	lsize = size;
523944cd46caSbillm 
5240770499e1SDan Kimmel 	pabd = abd_alloc_linear(SPA_MAXBLOCKSIZE, B_FALSE);
5241b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
5242b24ab676SJeff Bonwick 
5243b24ab676SJeff Bonwick 	BP_ZERO(bp);
5244b24ab676SJeff Bonwick 
5245b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
5246b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
5247b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
5248b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
5249b24ab676SJeff Bonwick 
5250b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
5251b24ab676SJeff Bonwick 
5252b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
5253b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
5254b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
5255b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
5256b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
5257b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
5258b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
5259b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
526044cd46caSbillm 
5261e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
526244cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
5263b24ab676SJeff Bonwick 
5264b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
5265b24ab676SJeff Bonwick 		/*
5266b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
5267b24ab676SJeff Bonwick 		 */
5268770499e1SDan Kimmel 		zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL,
5269b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
5270b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
5271b24ab676SJeff Bonwick 	} else {
5272b24ab676SJeff Bonwick 		/*
5273b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
5274b24ab676SJeff Bonwick 		 */
5275770499e1SDan Kimmel 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd,
5276770499e1SDan Kimmel 		    psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
5277b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
5278b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
52795cabbc6bSPrashanth Sreenivasa 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW | ZIO_FLAG_OPTIONAL,
52805cabbc6bSPrashanth Sreenivasa 		    NULL, NULL));
5281b24ab676SJeff Bonwick 	}
5282b24ab676SJeff Bonwick 
528344cd46caSbillm 	error = zio_wait(zio);
5284e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
528544cd46caSbillm 
528644cd46caSbillm 	if (error) {
528744cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
528844cd46caSbillm 		goto out;
528944cd46caSbillm 	}
529044cd46caSbillm 
5291b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
5292b24ab676SJeff Bonwick 		/*
5293b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
5294b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
5295b24ab676SJeff Bonwick 		 */
5296b24ab676SJeff Bonwick 		enum zio_compress c;
5297b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
5298b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
5299b24ab676SJeff Bonwick 
5300770499e1SDan Kimmel 		abd_copy_to_buf(pbuf2, pabd, psize);
5301b24ab676SJeff Bonwick 
5302770499e1SDan Kimmel 		VERIFY0(abd_iterate_func(pabd, psize, SPA_MAXBLOCKSIZE - psize,
5303770499e1SDan Kimmel 		    random_get_pseudo_bytes_cb, NULL));
5304b24ab676SJeff Bonwick 
5305770499e1SDan Kimmel 		VERIFY0(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
5306770499e1SDan Kimmel 		    SPA_MAXBLOCKSIZE - psize));
5307b24ab676SJeff Bonwick 
5308b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
5309b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
5310b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
5311770499e1SDan Kimmel 				if (zio_decompress_data(c, pabd,
5312770499e1SDan Kimmel 				    lbuf, psize, lsize) == 0 &&
5313770499e1SDan Kimmel 				    zio_decompress_data_buf(c, pbuf2,
5314770499e1SDan Kimmel 				    lbuf2, psize, lsize) == 0 &&
5315b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
5316b24ab676SJeff Bonwick 					break;
5317b24ab676SJeff Bonwick 			}
5318b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
5319b24ab676SJeff Bonwick 				break;
5320b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
5321b24ab676SJeff Bonwick 		}
5322b24ab676SJeff Bonwick 
5323b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
5324b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
5325b24ab676SJeff Bonwick 
5326b24ab676SJeff Bonwick 		if (lsize <= psize) {
5327b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
5328b24ab676SJeff Bonwick 			goto out;
5329b24ab676SJeff Bonwick 		}
5330b24ab676SJeff Bonwick 		buf = lbuf;
5331b24ab676SJeff Bonwick 		size = lsize;
5332b24ab676SJeff Bonwick 	} else {
5333770499e1SDan Kimmel 		buf = abd_to_buf(pabd);
5334b24ab676SJeff Bonwick 		size = psize;
5335b24ab676SJeff Bonwick 	}
5336b24ab676SJeff Bonwick 
533744cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
533844cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
533944cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
534044cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
534144cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
534244cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
534344cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
534444cd46caSbillm 		    flags);
534544cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
534644cd46caSbillm 		zdb_dump_gbh(buf, flags);
534744cd46caSbillm 	else
534844cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
534944cd46caSbillm 
535044cd46caSbillm out:
5351770499e1SDan Kimmel 	abd_free(pabd);
5352b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
535344cd46caSbillm 	free(dup);
535444cd46caSbillm }
535544cd46caSbillm 
53564923c69fSMatthew Ahrens static void
53574923c69fSMatthew Ahrens zdb_embedded_block(char *thing)
53584923c69fSMatthew Ahrens {
53593f7978d0SAlan Somers 	blkptr_t bp;
53604923c69fSMatthew Ahrens 	unsigned long long *words = (void *)&bp;
536103a4c2f4SJorgen Lundman 	char *buf;
53624923c69fSMatthew Ahrens 	int err;
53634923c69fSMatthew Ahrens 
53643f7978d0SAlan Somers 	bzero(&bp, sizeof (bp));
53654923c69fSMatthew Ahrens 	err = sscanf(thing, "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx:"
53664923c69fSMatthew Ahrens 	    "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx",
53674923c69fSMatthew Ahrens 	    words + 0, words + 1, words + 2, words + 3,
53684923c69fSMatthew Ahrens 	    words + 4, words + 5, words + 6, words + 7,
53694923c69fSMatthew Ahrens 	    words + 8, words + 9, words + 10, words + 11,
53704923c69fSMatthew Ahrens 	    words + 12, words + 13, words + 14, words + 15);
53714923c69fSMatthew Ahrens 	if (err != 16) {
537203a4c2f4SJorgen Lundman 		(void) fprintf(stderr, "invalid input format\n");
53734923c69fSMatthew Ahrens 		exit(1);
53744923c69fSMatthew Ahrens 	}
53754923c69fSMatthew Ahrens 	ASSERT3U(BPE_GET_LSIZE(&bp), <=, SPA_MAXBLOCKSIZE);
537603a4c2f4SJorgen Lundman 	buf = malloc(SPA_MAXBLOCKSIZE);
537703a4c2f4SJorgen Lundman 	if (buf == NULL) {
537803a4c2f4SJorgen Lundman 		(void) fprintf(stderr, "out of memory\n");
537903a4c2f4SJorgen Lundman 		exit(1);
538003a4c2f4SJorgen Lundman 	}
53814923c69fSMatthew Ahrens 	err = decode_embedded_bp(&bp, buf, BPE_GET_LSIZE(&bp));
53824923c69fSMatthew Ahrens 	if (err != 0) {
538303a4c2f4SJorgen Lundman 		(void) fprintf(stderr, "decode failed: %u\n", err);
53844923c69fSMatthew Ahrens 		exit(1);
53854923c69fSMatthew Ahrens 	}
53864923c69fSMatthew Ahrens 	zdb_dump_block_raw(buf, BPE_GET_LSIZE(&bp), 0);
538703a4c2f4SJorgen Lundman 	free(buf);
53884923c69fSMatthew Ahrens }
53894923c69fSMatthew Ahrens 
5390fa9e4066Sahrens int
5391fa9e4066Sahrens main(int argc, char **argv)
5392fa9e4066Sahrens {
53933f7978d0SAlan Somers 	int c;
5394fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
53953ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
5396fa9e4066Sahrens 	objset_t *os = NULL;
5397fa9e4066Sahrens 	int dump_all = 1;
5398fa9e4066Sahrens 	int verbose = 0;
5399c8ee1847SVictor Latushkin 	int error = 0;
54003ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
54013ad6c7f9SVictor Latushkin 	int nsearch = 0;
5402e0f1c0afSOlaf Faaland 	char *target, *target_pool;
5403468c413aSTim Haley 	nvlist_t *policy = NULL;
5404468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
5405dfd5965fSRichard Yao 	int flags = ZFS_IMPORT_MISSING_LOG;
5406c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
5407ae24175bSCyril Plisko 	char *spa_config_path_env;
5408b702644aSTim Chase 	boolean_t target_is_spa = B_TRUE;
540986714001SSerapheim Dimitropoulos 	nvlist_t *cfg = NULL;
5410fa9e4066Sahrens 
5411fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
5412004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
5413fa9e4066Sahrens 
5414fa9e4066Sahrens 	dprintf_setup(&argc, argv);
5415fa9e4066Sahrens 
5416ae24175bSCyril Plisko 	/*
5417ae24175bSCyril Plisko 	 * If there is an environment variable SPA_CONFIG_PATH it overrides
5418ae24175bSCyril Plisko 	 * default spa_config_path setting. If -U flag is specified it will
5419ae24175bSCyril Plisko 	 * override this environment variable settings once again.
5420ae24175bSCyril Plisko 	 */
5421ae24175bSCyril Plisko 	spa_config_path_env = getenv("SPA_CONFIG_PATH");
5422ae24175bSCyril Plisko 	if (spa_config_path_env != NULL)
5423ae24175bSCyril Plisko 		spa_config_path = spa_config_path_env;
5424ae24175bSCyril Plisko 
5425df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
542686714001SSerapheim Dimitropoulos 	    "AbcCdDeEFGhiI:klLmMo:Op:PqRsSt:uU:vVx:X")) != -1) {
5427fa9e4066Sahrens 		switch (c) {
5428fa9e4066Sahrens 		case 'b':
5429fa9e4066Sahrens 		case 'c':
5430ed61ec1dSYuri Pankov 		case 'C':
5431b24ab676SJeff Bonwick 		case 'd':
5432ed61ec1dSYuri Pankov 		case 'D':
54334923c69fSMatthew Ahrens 		case 'E':
5434ed61ec1dSYuri Pankov 		case 'G':
5435b24ab676SJeff Bonwick 		case 'h':
5436b24ab676SJeff Bonwick 		case 'i':
5437b24ab676SJeff Bonwick 		case 'l':
5438d6e555bdSGeorge Wilson 		case 'm':
54392e4c9986SGeorge Wilson 		case 'M':
5440ed61ec1dSYuri Pankov 		case 'O':
544144cd46caSbillm 		case 'R':
5442ed61ec1dSYuri Pankov 		case 's':
5443b24ab676SJeff Bonwick 		case 'S':
5444ed61ec1dSYuri Pankov 		case 'u':
5445fa9e4066Sahrens 			dump_opt[c]++;
5446fa9e4066Sahrens 			dump_all = 0;
5447fa9e4066Sahrens 			break;
5448feef89cfSVictor Latushkin 		case 'A':
5449ed61ec1dSYuri Pankov 		case 'e':
5450c8ee1847SVictor Latushkin 		case 'F':
545186714001SSerapheim Dimitropoulos 		case 'k':
545282a0a985SVictor Latushkin 		case 'L':
54533f9d6ad7SLin Ling 		case 'P':
545464723e36SYuri Pankov 		case 'q':
5455ed61ec1dSYuri Pankov 		case 'X':
545682a0a985SVictor Latushkin 			dump_opt[c]++;
545782a0a985SVictor Latushkin 			break;
5458ed61ec1dSYuri Pankov 		/* NB: Sort single match options below. */
54592e4c9986SGeorge Wilson 		case 'I':
546031d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
546131d7e8faSGeorge Wilson 			if (max_inflight == 0) {
546231d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
546331d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
546431d7e8faSGeorge Wilson 				    "than 0\n");
546531d7e8faSGeorge Wilson 				usage();
546631d7e8faSGeorge Wilson 			}
546731d7e8faSGeorge Wilson 			break;
5468ed61ec1dSYuri Pankov 		case 'o':
5469ed61ec1dSYuri Pankov 			error = set_global_var(optarg);
5470ed61ec1dSYuri Pankov 			if (error != 0)
5471ed61ec1dSYuri Pankov 				usage();
5472ed61ec1dSYuri Pankov 			break;
5473de6628f0Sck 		case 'p':
54743ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
54753ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
54763ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
54773ad6c7f9SVictor Latushkin 			} else {
54783ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
54793ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
54803ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
54813ad6c7f9SVictor Latushkin 				    sizeof (char *));
54823ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
54833ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
54843ad6c7f9SVictor Latushkin 				searchdirs = tmp;
54853ad6c7f9SVictor Latushkin 			}
54863ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
5487de6628f0Sck 			break;
54882e551927SVictor Latushkin 		case 't':
5489468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
5490468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
54912e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
54922e551927SVictor Latushkin 				    "specified: %s\n", optarg);
54932e551927SVictor Latushkin 				usage();
54942e551927SVictor Latushkin 			}
54952e551927SVictor Latushkin 			break;
5496b24ab676SJeff Bonwick 		case 'U':
5497b24ab676SJeff Bonwick 			spa_config_path = optarg;
54984923c69fSMatthew Ahrens 			if (spa_config_path[0] != '/') {
54994923c69fSMatthew Ahrens 				(void) fprintf(stderr,
55004923c69fSMatthew Ahrens 				    "cachefile must be an absolute path "
55014923c69fSMatthew Ahrens 				    "(i.e. start with a slash)\n");
55024923c69fSMatthew Ahrens 				usage();
55034923c69fSMatthew Ahrens 			}
5504b24ab676SJeff Bonwick 			break;
55052e4c9986SGeorge Wilson 		case 'v':
55062e4c9986SGeorge Wilson 			verbose++;
55072e4c9986SGeorge Wilson 			break;
5508dfd5965fSRichard Yao 		case 'V':
5509dfd5965fSRichard Yao 			flags = ZFS_IMPORT_VERBATIM;
5510dfd5965fSRichard Yao 			break;
55112e4c9986SGeorge Wilson 		case 'x':
55122e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
55132e4c9986SGeorge Wilson 			break;
5514fa9e4066Sahrens 		default:
5515fa9e4066Sahrens 			usage();
5516fa9e4066Sahrens 			break;
5517fa9e4066Sahrens 		}
5518fa9e4066Sahrens 	}
5519fa9e4066Sahrens 
55203ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
552188b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
552288b7b0f2SMatthew Ahrens 		usage();
552388b7b0f2SMatthew Ahrens 	}
5524de6628f0Sck 
552506be9802SMatthew Ahrens 	/*
552606be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
552706be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
552806be9802SMatthew Ahrens 	 */
552906be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
553006be9802SMatthew Ahrens 
5531f7950bf1SMatthew Ahrens 	/*
5532f7950bf1SMatthew Ahrens 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
5533f7950bf1SMatthew Ahrens 	 * "zdb -b" uses traversal prefetch which uses async reads.
5534f7950bf1SMatthew Ahrens 	 * For good performance, let several of them be active at once.
5535f7950bf1SMatthew Ahrens 	 */
5536f7950bf1SMatthew Ahrens 	zfs_vdev_async_read_max_active = 10;
5537f7950bf1SMatthew Ahrens 
55384dd77f9eSMatthew Ahrens 	/*
55394dd77f9eSMatthew Ahrens 	 * Disable reference tracking for better performance.
55404dd77f9eSMatthew Ahrens 	 */
55414dd77f9eSMatthew Ahrens 	reference_tracking_enable = B_FALSE;
55424dd77f9eSMatthew Ahrens 
5543e144c4e6SPavel Zakharov 	/*
5544e144c4e6SPavel Zakharov 	 * Do not fail spa_load when spa_load_verify fails. This is needed
5545e144c4e6SPavel Zakharov 	 * to load non-idle pools.
5546e144c4e6SPavel Zakharov 	 */
5547e144c4e6SPavel Zakharov 	spa_load_verify_dryrun = B_TRUE;
5548e144c4e6SPavel Zakharov 
5549fa9e4066Sahrens 	kernel_init(FREAD);
5550de6628f0Sck 	g_zfs = libzfs_init();
555191ebeef5Sahrens 	ASSERT(g_zfs != NULL);
5552fa9e4066Sahrens 
5553b24ab676SJeff Bonwick 	if (dump_all)
5554b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
5555b24ab676SJeff Bonwick 
5556fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
555786714001SSerapheim Dimitropoulos 		if (dump_all && strchr("AeEFklLOPRSX", c) == NULL)
5558fa9e4066Sahrens 			dump_opt[c] = 1;
5559fa9e4066Sahrens 		if (dump_opt[c])
5560fa9e4066Sahrens 			dump_opt[c] += verbose;
5561fa9e4066Sahrens 	}
5562fa9e4066Sahrens 
5563feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
5564feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
5565feef89cfSVictor Latushkin 
5566fa9e4066Sahrens 	argc -= optind;
5567fa9e4066Sahrens 	argv += optind;
5568fa9e4066Sahrens 
556907428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
557007428bdfSVictor Latushkin 		usage();
55714923c69fSMatthew Ahrens 
55724923c69fSMatthew Ahrens 	if (dump_opt['E']) {
55734923c69fSMatthew Ahrens 		if (argc != 1)
55744923c69fSMatthew Ahrens 			usage();
55754923c69fSMatthew Ahrens 		zdb_embedded_block(argv[0]);
55764923c69fSMatthew Ahrens 		return (0);
55774923c69fSMatthew Ahrens 	}
55784923c69fSMatthew Ahrens 
5579fa9e4066Sahrens 	if (argc < 1) {
55803ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
5581e829d913Sck 			dump_cachefile(spa_config_path);
5582fa9e4066Sahrens 			return (0);
5583fa9e4066Sahrens 		}
5584fa9e4066Sahrens 		usage();
5585fa9e4066Sahrens 	}
5586fa9e4066Sahrens 
558764723e36SYuri Pankov 	if (dump_opt['l'])
558864723e36SYuri Pankov 		return (dump_label(argv[0]));
5589fa9e4066Sahrens 
5590ed61ec1dSYuri Pankov 	if (dump_opt['O']) {
5591ed61ec1dSYuri Pankov 		if (argc != 2)
5592ed61ec1dSYuri Pankov 			usage();
5593ed61ec1dSYuri Pankov 		dump_opt['v'] = verbose + 3;
5594ed61ec1dSYuri Pankov 		return (dump_path(argv[0], argv[1]));
5595ed61ec1dSYuri Pankov 	}
5596ed61ec1dSYuri Pankov 
5597c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
5598c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
5599c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
5600c8ee1847SVictor Latushkin 
5601c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
56025dafeea3SPavel Zakharov 	    nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, max_txg) != 0 ||
56035dafeea3SPavel Zakharov 	    nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, rewind) != 0)
5604c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
5605c8ee1847SVictor Latushkin 
5606c5904d13Seschrock 	error = 0;
56073ad6c7f9SVictor Latushkin 	target = argv[0];
5608990b4856Slling 
5609e0f1c0afSOlaf Faaland 	if (strpbrk(target, "/@") != NULL) {
5610e0f1c0afSOlaf Faaland 		size_t targetlen;
5611e0f1c0afSOlaf Faaland 
5612e0f1c0afSOlaf Faaland 		target_pool = strdup(target);
5613e0f1c0afSOlaf Faaland 		*strpbrk(target_pool, "/@") = '\0';
5614e0f1c0afSOlaf Faaland 
5615e0f1c0afSOlaf Faaland 		target_is_spa = B_FALSE;
5616e0f1c0afSOlaf Faaland 		targetlen = strlen(target);
5617e0f1c0afSOlaf Faaland 		if (targetlen && target[targetlen - 1] == '/')
5618e0f1c0afSOlaf Faaland 			target[targetlen - 1] = '\0';
5619e0f1c0afSOlaf Faaland 	} else {
5620e0f1c0afSOlaf Faaland 		target_pool = target;
5621e0f1c0afSOlaf Faaland 	}
5622e0f1c0afSOlaf Faaland 
56233ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
5624e0f1c0afSOlaf Faaland 		importargs_t args = { 0 };
5625990b4856Slling 
5626e0f1c0afSOlaf Faaland 		args.paths = nsearch;
5627e0f1c0afSOlaf Faaland 		args.path = searchdirs;
5628e0f1c0afSOlaf Faaland 		args.can_be_active = B_TRUE;
5629e0f1c0afSOlaf Faaland 
5630e0f1c0afSOlaf Faaland 		error = zpool_tryimport(g_zfs, target_pool, &cfg, &args);
5631e0f1c0afSOlaf Faaland 
5632e0f1c0afSOlaf Faaland 		if (error == 0) {
563386714001SSerapheim Dimitropoulos 
5634c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
56355dafeea3SPavel Zakharov 			    ZPOOL_LOAD_POLICY, policy) != 0) {
5636468c413aSTim Haley 				fatal("can't open '%s': %s",
5637468c413aSTim Haley 				    target, strerror(ENOMEM));
5638468c413aSTim Haley 			}
5639e0f1c0afSOlaf Faaland 
5640e0f1c0afSOlaf Faaland 			if (dump_opt['C'] > 1) {
5641e0f1c0afSOlaf Faaland 				(void) printf("\nConfiguration for import:\n");
5642e0f1c0afSOlaf Faaland 				dump_nvlist(cfg, 8);
5643e0f1c0afSOlaf Faaland 			}
5644e0f1c0afSOlaf Faaland 
5645e0f1c0afSOlaf Faaland 			/*
5646e0f1c0afSOlaf Faaland 			 * Disable the activity check to allow examination of
5647e0f1c0afSOlaf Faaland 			 * active pools.
5648e0f1c0afSOlaf Faaland 			 */
5649e0f1c0afSOlaf Faaland 			error = spa_import(target_pool, cfg, NULL,
5650e0f1c0afSOlaf Faaland 			    flags | ZFS_IMPORT_SKIP_MMP);
5651de6628f0Sck 		}
5652c5904d13Seschrock 	}
5653c5904d13Seschrock 
565486714001SSerapheim Dimitropoulos 	char *checkpoint_pool = NULL;
565586714001SSerapheim Dimitropoulos 	char *checkpoint_target = NULL;
565686714001SSerapheim Dimitropoulos 	if (dump_opt['k']) {
565786714001SSerapheim Dimitropoulos 		checkpoint_pool = import_checkpointed_state(target, cfg,
565886714001SSerapheim Dimitropoulos 		    &checkpoint_target);
565986714001SSerapheim Dimitropoulos 
566086714001SSerapheim Dimitropoulos 		if (checkpoint_target != NULL)
566186714001SSerapheim Dimitropoulos 			target = checkpoint_target;
566286714001SSerapheim Dimitropoulos 
566386714001SSerapheim Dimitropoulos 	}
566486714001SSerapheim Dimitropoulos 
5665c5904d13Seschrock 	if (error == 0) {
566686714001SSerapheim Dimitropoulos 		if (dump_opt['k'] && (target_is_spa || dump_opt['R'])) {
566786714001SSerapheim Dimitropoulos 			ASSERT(checkpoint_pool != NULL);
566886714001SSerapheim Dimitropoulos 			ASSERT(checkpoint_target == NULL);
566986714001SSerapheim Dimitropoulos 
567086714001SSerapheim Dimitropoulos 			error = spa_open(checkpoint_pool, &spa, FTAG);
567186714001SSerapheim Dimitropoulos 			if (error != 0) {
567286714001SSerapheim Dimitropoulos 				fatal("Tried to open pool \"%s\" but "
567386714001SSerapheim Dimitropoulos 				    "spa_open() failed with error %d\n",
567486714001SSerapheim Dimitropoulos 				    checkpoint_pool, error);
567586714001SSerapheim Dimitropoulos 			}
567686714001SSerapheim Dimitropoulos 
567786714001SSerapheim Dimitropoulos 		} else if (target_is_spa || dump_opt['R']) {
5678e0f1c0afSOlaf Faaland 			zdb_set_skip_mmp(target);
567980eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
568080eb36f2SGeorge Wilson 			    NULL);
56818f18d1faSGeorge Wilson 			if (error) {
56828f18d1faSGeorge Wilson 				/*
56838f18d1faSGeorge Wilson 				 * If we're missing the log device then
56848f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
56858f18d1faSGeorge Wilson 				 * log state.
56868f18d1faSGeorge Wilson 				 */
56878f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
56883ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
56898f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
56908f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
56918f18d1faSGeorge Wilson 					error = 0;
56928f18d1faSGeorge Wilson 				}
56938f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
56948f18d1faSGeorge Wilson 
569580eb36f2SGeorge Wilson 				if (!error) {
569680eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
569780eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
569880eb36f2SGeorge Wilson 				}
56998f18d1faSGeorge Wilson 			}
570007428bdfSVictor Latushkin 		} else {
5701e0f1c0afSOlaf Faaland 			zdb_set_skip_mmp(target);
5702ed61ec1dSYuri Pankov 			error = open_objset(target, DMU_OST_ANY, FTAG, &os);
5703c5904d13Seschrock 		}
5704fa9e4066Sahrens 	}
570580eb36f2SGeorge Wilson 	nvlist_free(policy);
570680eb36f2SGeorge Wilson 
5707fa9e4066Sahrens 	if (error)
57083ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
5709fa9e4066Sahrens 
5710fa9e4066Sahrens 	argv++;
571107428bdfSVictor Latushkin 	argc--;
571207428bdfSVictor Latushkin 	if (!dump_opt['R']) {
571307428bdfSVictor Latushkin 		if (argc > 0) {
571407428bdfSVictor Latushkin 			zopt_objects = argc;
571507428bdfSVictor Latushkin 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
57163f7978d0SAlan Somers 			for (unsigned i = 0; i < zopt_objects; i++) {
571707428bdfSVictor Latushkin 				errno = 0;
571807428bdfSVictor Latushkin 				zopt_object[i] = strtoull(argv[i], NULL, 0);
571907428bdfSVictor Latushkin 				if (zopt_object[i] == 0 && errno != 0)
572087219db7SVictor Latushkin 					fatal("bad number %s: %s",
572107428bdfSVictor Latushkin 					    argv[i], strerror(errno));
572207428bdfSVictor Latushkin 			}
5723fa9e4066Sahrens 		}
5724e690fb27SChristopher Siden 		if (os != NULL) {
5725e690fb27SChristopher Siden 			dump_dir(os);
5726e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
5727e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
5728e690fb27SChristopher Siden 		} else {
5729e690fb27SChristopher Siden 			dump_zpool(spa);
5730e690fb27SChristopher Siden 		}
5731fa9e4066Sahrens 	} else {
573207428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
573307428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
573407428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
573507428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
573607428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
573707428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
573807428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
573907428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
574007428bdfSVictor Latushkin 
57413f7978d0SAlan Somers 		for (int i = 0; i < argc; i++)
574207428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
5743fa9e4066Sahrens 	}
5744fa9e4066Sahrens 
574586714001SSerapheim Dimitropoulos 	if (dump_opt['k']) {
574686714001SSerapheim Dimitropoulos 		free(checkpoint_pool);
574786714001SSerapheim Dimitropoulos 		if (!target_is_spa)
574886714001SSerapheim Dimitropoulos 			free(checkpoint_target);
574986714001SSerapheim Dimitropoulos 	}
575086714001SSerapheim Dimitropoulos 
5751ed61ec1dSYuri Pankov 	if (os != NULL)
5752ed61ec1dSYuri Pankov 		close_objset(os, FTAG);
5753ed61ec1dSYuri Pankov 	else
5754ed61ec1dSYuri Pankov 		spa_close(spa, FTAG);
575507428bdfSVictor Latushkin 
5756e0d35c44Smarks 	fuid_table_destroy();
5757e0d35c44Smarks 
575829bdd2f9SPavel Zakharov 	dump_debug_buffer();
575929bdd2f9SPavel Zakharov 
5760de6628f0Sck 	libzfs_fini(g_zfs);
5761fa9e4066Sahrens 	kernel_fini();
5762fa9e4066Sahrens 
576320b5dafbSPaul Dagnelie 	return (error);
5764fa9e4066Sahrens }
5765