xref: /illumos-gate/usr/src/cmd/zdb/zdb.c (revision 8671400134a11c848244896ca51a7db4d0f69da4)
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.
245cabbc6bSPrashanth Sreenivasa  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
25c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
26ed61ec1dSYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
27f06dce2cSAndrew Stormont  * Copyright 2017 RackTop Systems.
28fa9e4066Sahrens  */
29fa9e4066Sahrens 
30fa9e4066Sahrens #include <stdio.h>
31490d05b9SMatthew Ahrens #include <unistd.h>
32004388ebScasper #include <stdio_ext.h>
33fa9e4066Sahrens #include <stdlib.h>
3444cd46caSbillm #include <ctype.h>
35fa9e4066Sahrens #include <sys/zfs_context.h>
36fa9e4066Sahrens #include <sys/spa.h>
37fa9e4066Sahrens #include <sys/spa_impl.h>
38fa9e4066Sahrens #include <sys/dmu.h>
39fa9e4066Sahrens #include <sys/zap.h>
40fa9e4066Sahrens #include <sys/fs/zfs.h>
41fa9e4066Sahrens #include <sys/zfs_znode.h>
420a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
430a586ceaSMark Shellenbaum #include <sys/sa.h>
440a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
45fa9e4066Sahrens #include <sys/vdev.h>
46fa9e4066Sahrens #include <sys/vdev_impl.h>
47fa9e4066Sahrens #include <sys/metaslab_impl.h>
48fa9e4066Sahrens #include <sys/dmu_objset.h>
49fa9e4066Sahrens #include <sys/dsl_dir.h>
50fa9e4066Sahrens #include <sys/dsl_dataset.h>
51fa9e4066Sahrens #include <sys/dsl_pool.h>
52fa9e4066Sahrens #include <sys/dbuf.h>
53fa9e4066Sahrens #include <sys/zil.h>
54fa9e4066Sahrens #include <sys/zil_impl.h>
55fa9e4066Sahrens #include <sys/stat.h>
56fa9e4066Sahrens #include <sys/resource.h>
57fa9e4066Sahrens #include <sys/dmu_traverse.h>
58fa9e4066Sahrens #include <sys/zio_checksum.h>
59fa9e4066Sahrens #include <sys/zio_compress.h>
60e0d35c44Smarks #include <sys/zfs_fuid.h>
6188b7b0f2SMatthew Ahrens #include <sys/arc.h>
62b24ab676SJeff Bonwick #include <sys/ddt.h>
63ad135b5dSChristopher Siden #include <sys/zfeature.h>
64770499e1SDan Kimmel #include <sys/abd.h>
654923c69fSMatthew Ahrens #include <sys/blkptr.h>
664445fffbSMatthew Ahrens #include <zfs_comutil.h>
670a055120SJason King #include <libcmdutils.h>
68de6628f0Sck #undef verify
69de6628f0Sck #include <libzfs.h>
70fa9e4066Sahrens 
713f7978d0SAlan Somers #include "zdb.h"
723f7978d0SAlan Somers 
73e690fb27SChristopher Siden #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
74e690fb27SChristopher Siden 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
75e690fb27SChristopher Siden #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
76e690fb27SChristopher Siden 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
77e690fb27SChristopher Siden #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
78e690fb27SChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
79e690fb27SChristopher Siden 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
80e690fb27SChristopher Siden #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
815cabbc6bSPrashanth Sreenivasa 	(idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA ?	\
825cabbc6bSPrashanth Sreenivasa 	DMU_OT_ZAP_OTHER : \
835cabbc6bSPrashanth Sreenivasa 	(idx) == DMU_OTN_UINT64_DATA || (idx) == DMU_OTN_UINT64_METADATA ? \
845cabbc6bSPrashanth Sreenivasa 	DMU_OT_UINT64_OTHER : DMU_OT_NUMTYPES)
856de8f417SVictor Latushkin 
86feef89cfSVictor Latushkin #ifndef lint
874dd77f9eSMatthew Ahrens extern int reference_tracking_enable;
887fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
8906be9802SMatthew Ahrens extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
90f7950bf1SMatthew Ahrens extern int zfs_vdev_async_read_max_active;
91f06dce2cSAndrew Stormont extern int aok;
92e144c4e6SPavel Zakharov extern boolean_t spa_load_verify_dryrun;
93feef89cfSVictor Latushkin #else
944dd77f9eSMatthew Ahrens int reference_tracking_enable;
957fd05ac4SMatthew Ahrens boolean_t zfs_recover;
9606be9802SMatthew Ahrens uint64_t zfs_arc_max, zfs_arc_meta_limit;
97f7950bf1SMatthew Ahrens int zfs_vdev_async_read_max_active;
98f06dce2cSAndrew Stormont int aok;
99e144c4e6SPavel Zakharov boolean_t spa_load_verify_dryrun;
100feef89cfSVictor Latushkin #endif
101feef89cfSVictor Latushkin 
1023f7978d0SAlan Somers static const char cmdname[] = "zdb";
103fa9e4066Sahrens uint8_t dump_opt[256];
104fa9e4066Sahrens 
105fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
106fa9e4066Sahrens 
107fa9e4066Sahrens uint64_t *zopt_object = NULL;
1083f7978d0SAlan Somers static unsigned zopt_objects = 0;
109de6628f0Sck libzfs_handle_t *g_zfs;
11006be9802SMatthew Ahrens uint64_t max_inflight = 1000;
111fa9e4066Sahrens 
112732885fcSMatthew Ahrens static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
113732885fcSMatthew Ahrens 
114fa9e4066Sahrens /*
115fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
116fa9e4066Sahrens  * debugging facilities.
117fa9e4066Sahrens  */
118fa9e4066Sahrens const char *
119fa9e4066Sahrens _umem_debug_init()
120fa9e4066Sahrens {
121fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
122fa9e4066Sahrens }
123fa9e4066Sahrens 
124fa9e4066Sahrens const char *
125fa9e4066Sahrens _umem_logging_init(void)
126fa9e4066Sahrens {
127fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
128fa9e4066Sahrens }
129fa9e4066Sahrens 
130fa9e4066Sahrens static void
131fa9e4066Sahrens usage(void)
132fa9e4066Sahrens {
133fa9e4066Sahrens 	(void) fprintf(stderr,
134*86714001SSerapheim Dimitropoulos 	    "Usage:\t%s [-AbcdDFGhikLMPsvX] [-e [-V] [-p <path> ...]] "
135ed61ec1dSYuri Pankov 	    "[-I <inflight I/Os>]\n"
136ed61ec1dSYuri Pankov 	    "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
137ed61ec1dSYuri Pankov 	    "\t\t[<poolname> [<object> ...]]\n"
138dfd5965fSRichard Yao 	    "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>] <dataset> "
139ed61ec1dSYuri Pankov 	    "[<object> ...]\n"
140ed61ec1dSYuri Pankov 	    "\t%s -C [-A] [-U <cache>]\n"
141ed61ec1dSYuri Pankov 	    "\t%s -l [-Aqu] <device>\n"
142dfd5965fSRichard Yao 	    "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] "
143dfd5965fSRichard Yao 	    "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n"
144ed61ec1dSYuri Pankov 	    "\t%s -O <dataset> <path>\n"
145dfd5965fSRichard Yao 	    "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
146ed61ec1dSYuri Pankov 	    "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n"
1474923c69fSMatthew Ahrens 	    "\t%s -E [-A] word0:word1:...:word15\n"
148dfd5965fSRichard Yao 	    "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] "
149dfd5965fSRichard Yao 	    "<poolname>\n\n",
150ed61ec1dSYuri Pankov 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname,
1514923c69fSMatthew Ahrens 	    cmdname, cmdname);
1523ad6c7f9SVictor Latushkin 
1533ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1543ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1553ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1563ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1573ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1583ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1593ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
1603ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -b block statistics\n");
1613ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1626365109dSVictor Latushkin 	    "all data) blocks\n");
163ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
164ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -d dataset(s)\n");
165f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
1664923c69fSMatthew Ahrens 	(void) fprintf(stderr, "        -E decode and display block from an "
1674923c69fSMatthew Ahrens 	    "embedded block pointer\n");
168ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -h pool history\n");
169ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -i intent logs\n");
17064723e36SYuri Pankov 	(void) fprintf(stderr, "        -l read label contents\n");
171*86714001SSerapheim Dimitropoulos 	(void) fprintf(stderr, "        -k examine the checkpointed state "
172*86714001SSerapheim Dimitropoulos 	    "of the pool\n");
17382a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
17482a0a985SVictor Latushkin 	    "load spacemaps)\n");
175ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -m metaslabs\n");
176ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -M metaslab groups\n");
177ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -O perform object lookups by path\n");
178d41e7643Sek 	(void) fprintf(stderr, "        -R read and display block from a "
179ed61ec1dSYuri Pankov 	    "device\n");
180ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
181ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
182ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -v verbose (applies to all "
183ed61ec1dSYuri Pankov 	    "others)\n\n");
1843ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
185df15e419SMatthew Ahrens 	    "with other options:\n");
186feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
187feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
1883ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1893ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
190ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
191ed61ec1dSYuri Pankov 	    "safe range of transaction groups\n");
192ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -G dump zfs_dbgmsg buffer before "
193ed61ec1dSYuri Pankov 	    "exiting\n");
1942e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
195df15e419SMatthew Ahrens 	    "specify the maximum number of "
196df15e419SMatthew Ahrens 	    "checksumming I/Os [default is 200]\n");
1970e60744cSPavel Zakharov 	(void) fprintf(stderr, "        -o <variable>=<value> set global "
1980e60744cSPavel Zakharov 	    "variable to an unsigned 32-bit integer value\n");
199ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
200ed61ec1dSYuri Pankov 	    "-e to specify path to vdev dir\n");
201ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
20264723e36SYuri Pankov 	(void) fprintf(stderr, "        -q don't print label contents\n");
203ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
204ed61ec1dSYuri Pankov 	    "searching for uberblocks\n");
20564723e36SYuri Pankov 	(void) fprintf(stderr, "        -u uberblock\n");
206ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
207ed61ec1dSYuri Pankov 	    "cachefile\n");
208dfd5965fSRichard Yao 	(void) fprintf(stderr, "        -V do verbatim import\n");
209ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -x <dumpdir> -- "
210ed61ec1dSYuri Pankov 	    "dump all read blocks into specified directory\n");
211ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
212ed61ec1dSYuri Pankov 	    "work with dataset)\n\n");
213fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
214fa9e4066Sahrens 	    "to make only that option verbose\n");
215fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
216fa9e4066Sahrens 	exit(1);
217fa9e4066Sahrens }
218fa9e4066Sahrens 
21929bdd2f9SPavel Zakharov static void
22029bdd2f9SPavel Zakharov dump_debug_buffer()
22129bdd2f9SPavel Zakharov {
22229bdd2f9SPavel Zakharov 	if (dump_opt['G']) {
22329bdd2f9SPavel Zakharov 		(void) printf("\n");
22429bdd2f9SPavel Zakharov 		zfs_dbgmsg_print("zdb");
22529bdd2f9SPavel Zakharov 	}
22629bdd2f9SPavel Zakharov }
22729bdd2f9SPavel Zakharov 
228ccba0801SRich Morris /*
229ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
230ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
231ccba0801SRich Morris  */
232ccba0801SRich Morris 
233fa9e4066Sahrens static void
234fa9e4066Sahrens fatal(const char *fmt, ...)
235fa9e4066Sahrens {
236fa9e4066Sahrens 	va_list ap;
237fa9e4066Sahrens 
238fa9e4066Sahrens 	va_start(ap, fmt);
239fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
240fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
241fa9e4066Sahrens 	va_end(ap);
242fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
243fa9e4066Sahrens 
24429bdd2f9SPavel Zakharov 	dump_debug_buffer();
24529bdd2f9SPavel Zakharov 
246ccba0801SRich Morris 	exit(1);
247fa9e4066Sahrens }
248fa9e4066Sahrens 
249fa9e4066Sahrens /* ARGSUSED */
250fa9e4066Sahrens static void
251fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
252fa9e4066Sahrens {
253fa9e4066Sahrens 	nvlist_t *nv;
254fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
255fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
256fa9e4066Sahrens 
2577bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
258fa9e4066Sahrens 
259fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
260fa9e4066Sahrens 
261fa9e4066Sahrens 	umem_free(packed, nvsize);
262fa9e4066Sahrens 
263fa9e4066Sahrens 	dump_nvlist(nv, 8);
264fa9e4066Sahrens 
265fa9e4066Sahrens 	nvlist_free(nv);
266fa9e4066Sahrens }
267fa9e4066Sahrens 
2684445fffbSMatthew Ahrens /* ARGSUSED */
2694445fffbSMatthew Ahrens static void
2704445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2714445fffbSMatthew Ahrens {
2724445fffbSMatthew Ahrens 	spa_history_phys_t *shp = data;
2734445fffbSMatthew Ahrens 
2744445fffbSMatthew Ahrens 	if (shp == NULL)
2754445fffbSMatthew Ahrens 		return;
2764445fffbSMatthew Ahrens 
2774445fffbSMatthew Ahrens 	(void) printf("\t\tpool_create_len = %llu\n",
2784445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_pool_create_len);
2794445fffbSMatthew Ahrens 	(void) printf("\t\tphys_max_off = %llu\n",
2804445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_phys_max_off);
2814445fffbSMatthew Ahrens 	(void) printf("\t\tbof = %llu\n",
2824445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_bof);
2834445fffbSMatthew Ahrens 	(void) printf("\t\teof = %llu\n",
2844445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_eof);
2854445fffbSMatthew Ahrens 	(void) printf("\t\trecords_lost = %llu\n",
2864445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_records_lost);
2874445fffbSMatthew Ahrens }
2884445fffbSMatthew Ahrens 
2893f9d6ad7SLin Ling static void
2900a055120SJason King zdb_nicenum(uint64_t num, char *buf, size_t buflen)
2913f9d6ad7SLin Ling {
2923f9d6ad7SLin Ling 	if (dump_opt['P'])
2930a055120SJason King 		(void) snprintf(buf, buflen, "%llu", (longlong_t)num);
2943f9d6ad7SLin Ling 	else
2950a055120SJason King 		nicenum(num, buf, sizeof (buf));
2963f9d6ad7SLin Ling }
2973f9d6ad7SLin Ling 
2983f7978d0SAlan Somers static const char histo_stars[] = "****************************************";
2993f7978d0SAlan Somers static const uint64_t histo_width = sizeof (histo_stars) - 1;
300fa9e4066Sahrens 
301fa9e4066Sahrens static void
3020713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
303fa9e4066Sahrens {
304fa9e4066Sahrens 	int i;
305490d05b9SMatthew Ahrens 	int minidx = size - 1;
306fa9e4066Sahrens 	int maxidx = 0;
307fa9e4066Sahrens 	uint64_t max = 0;
308fa9e4066Sahrens 
309490d05b9SMatthew Ahrens 	for (i = 0; i < size; i++) {
310fa9e4066Sahrens 		if (histo[i] > max)
311fa9e4066Sahrens 			max = histo[i];
312fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
313fa9e4066Sahrens 			maxidx = i;
314fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
315fa9e4066Sahrens 			minidx = i;
316fa9e4066Sahrens 	}
317fa9e4066Sahrens 
318490d05b9SMatthew Ahrens 	if (max < histo_width)
319490d05b9SMatthew Ahrens 		max = histo_width;
320fa9e4066Sahrens 
321490d05b9SMatthew Ahrens 	for (i = minidx; i <= maxidx; i++) {
322490d05b9SMatthew Ahrens 		(void) printf("\t\t\t%3u: %6llu %s\n",
3230713e232SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
324490d05b9SMatthew Ahrens 		    &histo_stars[(max - histo[i]) * histo_width / max]);
325490d05b9SMatthew Ahrens 	}
326fa9e4066Sahrens }
327fa9e4066Sahrens 
328fa9e4066Sahrens static void
329fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
330fa9e4066Sahrens {
331fa9e4066Sahrens 	int error;
332fa9e4066Sahrens 	zap_stats_t zs;
333fa9e4066Sahrens 
334fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
335fa9e4066Sahrens 	if (error)
336fa9e4066Sahrens 		return;
337fa9e4066Sahrens 
338fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
339fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
340fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
341fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
342fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
343fa9e4066Sahrens 		return;
344fa9e4066Sahrens 	}
345fa9e4066Sahrens 
346fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
3478248818dSnd 
3488248818dSnd 	(void) printf("\t\tPointer table:\n");
3498248818dSnd 	(void) printf("\t\t\t%llu elements\n",
350fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3518248818dSnd 	(void) printf("\t\t\tzt_blk: %llu\n",
3528248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3538248818dSnd 	(void) printf("\t\t\tzt_numblks: %llu\n",
3548248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3558248818dSnd 	(void) printf("\t\t\tzt_shift: %llu\n",
3568248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3578248818dSnd 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3588248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3598248818dSnd 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3608248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3618248818dSnd 
362fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
363fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
364fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
365fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
366fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
367fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
3688248818dSnd 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3698248818dSnd 	    (u_longlong_t)zs.zs_block_type);
3708248818dSnd 	(void) printf("\t\tzap_magic: 0x%llx\n",
3718248818dSnd 	    (u_longlong_t)zs.zs_magic);
3728248818dSnd 	(void) printf("\t\tzap_salt: 0x%llx\n",
3738248818dSnd 	    (u_longlong_t)zs.zs_salt);
374fa9e4066Sahrens 
375fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
3760713e232SGeorge Wilson 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
377fa9e4066Sahrens 
378fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
3790713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
380fa9e4066Sahrens 
381fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
3820713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
383fa9e4066Sahrens 
384fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
3850713e232SGeorge Wilson 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
386fa9e4066Sahrens 
387fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
3880713e232SGeorge Wilson 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
389fa9e4066Sahrens }
390fa9e4066Sahrens 
391fa9e4066Sahrens /*ARGSUSED*/
392fa9e4066Sahrens static void
393fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
394fa9e4066Sahrens {
395fa9e4066Sahrens }
396fa9e4066Sahrens 
3976de8f417SVictor Latushkin /*ARGSUSED*/
3986de8f417SVictor Latushkin static void
3996de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
4006de8f417SVictor Latushkin {
4016de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
4026de8f417SVictor Latushkin }
4036de8f417SVictor Latushkin 
404fa9e4066Sahrens /*ARGSUSED*/
4053f7978d0SAlan Somers static void
406fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
407fa9e4066Sahrens {
408fa9e4066Sahrens }
409fa9e4066Sahrens 
410fa9e4066Sahrens /*ARGSUSED*/
411fa9e4066Sahrens static void
412fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
413fa9e4066Sahrens {
414fa9e4066Sahrens }
415fa9e4066Sahrens 
416fa9e4066Sahrens /*ARGSUSED*/
417fa9e4066Sahrens static void
418fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
419fa9e4066Sahrens {
420fa9e4066Sahrens 	zap_cursor_t zc;
421fa9e4066Sahrens 	zap_attribute_t attr;
422fa9e4066Sahrens 	void *prop;
4233f7978d0SAlan Somers 	unsigned i;
424fa9e4066Sahrens 
425fa9e4066Sahrens 	dump_zap_stats(os, object);
426fa9e4066Sahrens 	(void) printf("\n");
427fa9e4066Sahrens 
428fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
429fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
430fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
431fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
432fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
433fa9e4066Sahrens 			(void) printf("\n");
434fa9e4066Sahrens 			continue;
435fa9e4066Sahrens 		}
436fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
437fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
438fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
439fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
440fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
441fa9e4066Sahrens 			(void) printf("%s", (char *)prop);
442fa9e4066Sahrens 		} else {
443fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
444fa9e4066Sahrens 				switch (attr.za_integer_length) {
445fa9e4066Sahrens 				case 2:
446fa9e4066Sahrens 					(void) printf("%u ",
447fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
448fa9e4066Sahrens 					break;
449fa9e4066Sahrens 				case 4:
450fa9e4066Sahrens 					(void) printf("%u ",
451fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
452fa9e4066Sahrens 					break;
453fa9e4066Sahrens 				case 8:
454fa9e4066Sahrens 					(void) printf("%lld ",
455fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
456fa9e4066Sahrens 					break;
457fa9e4066Sahrens 				}
458fa9e4066Sahrens 			}
459fa9e4066Sahrens 		}
460fa9e4066Sahrens 		(void) printf("\n");
461fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
462fa9e4066Sahrens 	}
46387e5029aSahrens 	zap_cursor_fini(&zc);
464fa9e4066Sahrens }
465fa9e4066Sahrens 
466732885fcSMatthew Ahrens static void
467732885fcSMatthew Ahrens dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
468732885fcSMatthew Ahrens {
469732885fcSMatthew Ahrens 	bpobj_phys_t *bpop = data;
470732885fcSMatthew Ahrens 	char bytes[32], comp[32], uncomp[32];
471732885fcSMatthew Ahrens 
4720a055120SJason King 	/* make sure the output won't get truncated */
4730a055120SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
4740a055120SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
4750a055120SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
4760a055120SJason King 
477732885fcSMatthew Ahrens 	if (bpop == NULL)
478732885fcSMatthew Ahrens 		return;
479732885fcSMatthew Ahrens 
4800a055120SJason King 	zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
4810a055120SJason King 	zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
4820a055120SJason King 	zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
483732885fcSMatthew Ahrens 
484732885fcSMatthew Ahrens 	(void) printf("\t\tnum_blkptrs = %llu\n",
485732885fcSMatthew Ahrens 	    (u_longlong_t)bpop->bpo_num_blkptrs);
486732885fcSMatthew Ahrens 	(void) printf("\t\tbytes = %s\n", bytes);
487732885fcSMatthew Ahrens 	if (size >= BPOBJ_SIZE_V1) {
488732885fcSMatthew Ahrens 		(void) printf("\t\tcomp = %s\n", comp);
489732885fcSMatthew Ahrens 		(void) printf("\t\tuncomp = %s\n", uncomp);
490732885fcSMatthew Ahrens 	}
491732885fcSMatthew Ahrens 	if (size >= sizeof (*bpop)) {
492732885fcSMatthew Ahrens 		(void) printf("\t\tsubobjs = %llu\n",
493732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_subobjs);
494732885fcSMatthew Ahrens 		(void) printf("\t\tnum_subobjs = %llu\n",
495732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_num_subobjs);
496732885fcSMatthew Ahrens 	}
497732885fcSMatthew Ahrens 
498732885fcSMatthew Ahrens 	if (dump_opt['d'] < 5)
499732885fcSMatthew Ahrens 		return;
500732885fcSMatthew Ahrens 
501732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
502732885fcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
503732885fcSMatthew Ahrens 		blkptr_t bp;
504732885fcSMatthew Ahrens 
505732885fcSMatthew Ahrens 		int err = dmu_read(os, object,
506732885fcSMatthew Ahrens 		    i * sizeof (bp), sizeof (bp), &bp, 0);
507732885fcSMatthew Ahrens 		if (err != 0) {
508732885fcSMatthew Ahrens 			(void) printf("got error %u from dmu_read\n", err);
509732885fcSMatthew Ahrens 			break;
510732885fcSMatthew Ahrens 		}
511732885fcSMatthew Ahrens 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
512732885fcSMatthew Ahrens 		(void) printf("\t%s\n", blkbuf);
513732885fcSMatthew Ahrens 	}
514732885fcSMatthew Ahrens }
515732885fcSMatthew Ahrens 
516732885fcSMatthew Ahrens /* ARGSUSED */
517732885fcSMatthew Ahrens static void
518732885fcSMatthew Ahrens dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
519732885fcSMatthew Ahrens {
520732885fcSMatthew Ahrens 	dmu_object_info_t doi;
521732885fcSMatthew Ahrens 
522732885fcSMatthew Ahrens 	VERIFY0(dmu_object_info(os, object, &doi));
523732885fcSMatthew Ahrens 	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
524732885fcSMatthew Ahrens 
525732885fcSMatthew Ahrens 	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
526732885fcSMatthew Ahrens 	if (err != 0) {
527732885fcSMatthew Ahrens 		(void) printf("got error %u from dmu_read\n", err);
528732885fcSMatthew Ahrens 		kmem_free(subobjs, doi.doi_max_offset);
529732885fcSMatthew Ahrens 		return;
530732885fcSMatthew Ahrens 	}
531732885fcSMatthew Ahrens 
532732885fcSMatthew Ahrens 	int64_t last_nonzero = -1;
533732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
534732885fcSMatthew Ahrens 		if (subobjs[i] != 0)
535732885fcSMatthew Ahrens 			last_nonzero = i;
536732885fcSMatthew Ahrens 	}
537732885fcSMatthew Ahrens 
538732885fcSMatthew Ahrens 	for (int64_t i = 0; i <= last_nonzero; i++) {
539732885fcSMatthew Ahrens 		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
540732885fcSMatthew Ahrens 	}
541732885fcSMatthew Ahrens 	kmem_free(subobjs, doi.doi_max_offset);
542732885fcSMatthew Ahrens }
543732885fcSMatthew Ahrens 
544486ae710SMatthew Ahrens /*ARGSUSED*/
545486ae710SMatthew Ahrens static void
546486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
547486ae710SMatthew Ahrens {
548486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
549486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
550486ae710SMatthew Ahrens }
551486ae710SMatthew Ahrens 
5520a586ceaSMark Shellenbaum /*ARGSUSED*/
5530a586ceaSMark Shellenbaum static void
5540a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5550a586ceaSMark Shellenbaum {
5560a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5570a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5580a586ceaSMark Shellenbaum 
5590a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5600a586ceaSMark Shellenbaum 	(void) printf("\n");
5610a586ceaSMark Shellenbaum 
5620a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5630a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5640a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5650a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
5660a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5670a586ceaSMark Shellenbaum 			(void) printf("\n");
5680a586ceaSMark Shellenbaum 			continue;
5690a586ceaSMark Shellenbaum 		}
5700a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
5710a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
5720a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
5730a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
5740a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
5750a586ceaSMark Shellenbaum 	}
5760a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5770a586ceaSMark Shellenbaum }
5780a586ceaSMark Shellenbaum 
5790a586ceaSMark Shellenbaum /*ARGSUSED*/
5800a586ceaSMark Shellenbaum static void
5810a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
5820a586ceaSMark Shellenbaum {
5830a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5840a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5850a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
5863f7978d0SAlan Somers 	unsigned i;
5870a586ceaSMark Shellenbaum 
5880a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5890a586ceaSMark Shellenbaum 	(void) printf("\n");
5900a586ceaSMark Shellenbaum 
5910a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5920a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5930a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5940a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
5950a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5960a586ceaSMark Shellenbaum 			(void) printf("\n");
5970a586ceaSMark Shellenbaum 			continue;
5980a586ceaSMark Shellenbaum 		}
5990a586ceaSMark Shellenbaum 
6000a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
6010a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
6020a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
6030a586ceaSMark Shellenbaum 
6040a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
6050a586ceaSMark Shellenbaum 		    attr.za_integer_length,
6060a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
6070a586ceaSMark Shellenbaum 
6080a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
6090a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
6100a586ceaSMark Shellenbaum 		(void) printf("]\n");
6110a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
6120a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
6130a586ceaSMark Shellenbaum 	}
6140a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
6150a586ceaSMark Shellenbaum }
6160a586ceaSMark Shellenbaum 
617e7437265Sahrens /*ARGSUSED*/
618e7437265Sahrens static void
619e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
620e7437265Sahrens {
621e7437265Sahrens 	zap_cursor_t zc;
622e7437265Sahrens 	zap_attribute_t attr;
623e7437265Sahrens 	const char *typenames[] = {
624e7437265Sahrens 		/* 0 */ "not specified",
625e7437265Sahrens 		/* 1 */ "FIFO",
626e7437265Sahrens 		/* 2 */ "Character Device",
627e7437265Sahrens 		/* 3 */ "3 (invalid)",
628e7437265Sahrens 		/* 4 */ "Directory",
629e7437265Sahrens 		/* 5 */ "5 (invalid)",
630e7437265Sahrens 		/* 6 */ "Block Device",
631e7437265Sahrens 		/* 7 */ "7 (invalid)",
632e7437265Sahrens 		/* 8 */ "Regular File",
633e7437265Sahrens 		/* 9 */ "9 (invalid)",
634e7437265Sahrens 		/* 10 */ "Symbolic Link",
635e7437265Sahrens 		/* 11 */ "11 (invalid)",
636e7437265Sahrens 		/* 12 */ "Socket",
637e7437265Sahrens 		/* 13 */ "Door",
638e7437265Sahrens 		/* 14 */ "Event Port",
639e7437265Sahrens 		/* 15 */ "15 (invalid)",
640e7437265Sahrens 	};
641e7437265Sahrens 
642e7437265Sahrens 	dump_zap_stats(os, object);
643e7437265Sahrens 	(void) printf("\n");
644e7437265Sahrens 
645e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
646e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
647e7437265Sahrens 	    zap_cursor_advance(&zc)) {
648e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
649e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
650e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
651e7437265Sahrens 	}
652e7437265Sahrens 	zap_cursor_fini(&zc);
653e7437265Sahrens }
654e7437265Sahrens 
6553f7978d0SAlan Somers static int
6560713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
6570713e232SGeorge Wilson {
6580713e232SGeorge Wilson 	int refcount = 0;
6590713e232SGeorge Wilson 
6600713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
6610713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
6620713e232SGeorge Wilson 
6630713e232SGeorge Wilson 		if (sm != NULL &&
6640713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6650713e232SGeorge Wilson 			return (1);
6660713e232SGeorge Wilson 		return (0);
6670713e232SGeorge Wilson 	}
6680713e232SGeorge Wilson 
6693f7978d0SAlan Somers 	for (unsigned c = 0; c < vd->vdev_children; c++)
6700713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
6710713e232SGeorge Wilson 	return (refcount);
6720713e232SGeorge Wilson }
6730713e232SGeorge Wilson 
6743f7978d0SAlan Somers static int
6750713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
6760713e232SGeorge Wilson {
6770713e232SGeorge Wilson 	int refcount = 0;
6780713e232SGeorge Wilson 
6795cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_top == vd) {
6805cabbc6bSPrashanth Sreenivasa 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
6810713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
6820713e232SGeorge Wilson 
6830713e232SGeorge Wilson 			if (sm != NULL &&
6840713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6850713e232SGeorge Wilson 				refcount++;
6860713e232SGeorge Wilson 		}
6870713e232SGeorge Wilson 	}
6883f7978d0SAlan Somers 	for (unsigned c = 0; c < vd->vdev_children; c++)
6890713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
6900713e232SGeorge Wilson 
6910713e232SGeorge Wilson 	return (refcount);
6920713e232SGeorge Wilson }
6930713e232SGeorge Wilson 
6945cabbc6bSPrashanth Sreenivasa static int
6955cabbc6bSPrashanth Sreenivasa get_obsolete_refcount(vdev_t *vd)
6965cabbc6bSPrashanth Sreenivasa {
6975cabbc6bSPrashanth Sreenivasa 	int refcount = 0;
6985cabbc6bSPrashanth Sreenivasa 
6995cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_obj = vdev_obsolete_sm_object(vd);
7005cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_top == vd && obsolete_sm_obj != 0) {
7015cabbc6bSPrashanth Sreenivasa 		dmu_object_info_t doi;
7025cabbc6bSPrashanth Sreenivasa 		VERIFY0(dmu_object_info(vd->vdev_spa->spa_meta_objset,
7035cabbc6bSPrashanth Sreenivasa 		    obsolete_sm_obj, &doi));
7045cabbc6bSPrashanth Sreenivasa 		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
7055cabbc6bSPrashanth Sreenivasa 			refcount++;
7065cabbc6bSPrashanth Sreenivasa 		}
7075cabbc6bSPrashanth Sreenivasa 	} else {
7085cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
7095cabbc6bSPrashanth Sreenivasa 		ASSERT3U(obsolete_sm_obj, ==, 0);
7105cabbc6bSPrashanth Sreenivasa 	}
7115cabbc6bSPrashanth Sreenivasa 	for (unsigned c = 0; c < vd->vdev_children; c++) {
7125cabbc6bSPrashanth Sreenivasa 		refcount += get_obsolete_refcount(vd->vdev_child[c]);
7135cabbc6bSPrashanth Sreenivasa 	}
7145cabbc6bSPrashanth Sreenivasa 
7155cabbc6bSPrashanth Sreenivasa 	return (refcount);
7165cabbc6bSPrashanth Sreenivasa }
7175cabbc6bSPrashanth Sreenivasa 
7185cabbc6bSPrashanth Sreenivasa static int
7195cabbc6bSPrashanth Sreenivasa get_prev_obsolete_spacemap_refcount(spa_t *spa)
7205cabbc6bSPrashanth Sreenivasa {
7215cabbc6bSPrashanth Sreenivasa 	uint64_t prev_obj =
7225cabbc6bSPrashanth Sreenivasa 	    spa->spa_condensing_indirect_phys.scip_prev_obsolete_sm_object;
7235cabbc6bSPrashanth Sreenivasa 	if (prev_obj != 0) {
7245cabbc6bSPrashanth Sreenivasa 		dmu_object_info_t doi;
7255cabbc6bSPrashanth Sreenivasa 		VERIFY0(dmu_object_info(spa->spa_meta_objset, prev_obj, &doi));
7265cabbc6bSPrashanth Sreenivasa 		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
7275cabbc6bSPrashanth Sreenivasa 			return (1);
7285cabbc6bSPrashanth Sreenivasa 		}
7295cabbc6bSPrashanth Sreenivasa 	}
7305cabbc6bSPrashanth Sreenivasa 	return (0);
7315cabbc6bSPrashanth Sreenivasa }
7325cabbc6bSPrashanth Sreenivasa 
733*86714001SSerapheim Dimitropoulos static int
734*86714001SSerapheim Dimitropoulos get_checkpoint_refcount(vdev_t *vd)
735*86714001SSerapheim Dimitropoulos {
736*86714001SSerapheim Dimitropoulos 	int refcount = 0;
737*86714001SSerapheim Dimitropoulos 
738*86714001SSerapheim Dimitropoulos 	if (vd->vdev_top == vd && vd->vdev_top_zap != 0 &&
739*86714001SSerapheim Dimitropoulos 	    zap_contains(spa_meta_objset(vd->vdev_spa),
740*86714001SSerapheim Dimitropoulos 	    vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) == 0)
741*86714001SSerapheim Dimitropoulos 		refcount++;
742*86714001SSerapheim Dimitropoulos 
743*86714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < vd->vdev_children; c++)
744*86714001SSerapheim Dimitropoulos 		refcount += get_checkpoint_refcount(vd->vdev_child[c]);
745*86714001SSerapheim Dimitropoulos 
746*86714001SSerapheim Dimitropoulos 	return (refcount);
747*86714001SSerapheim Dimitropoulos }
748*86714001SSerapheim Dimitropoulos 
7490713e232SGeorge Wilson static int
7500713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
7510713e232SGeorge Wilson {
7522acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
7532acef22dSMatthew Ahrens 	uint64_t actual_refcount;
7540713e232SGeorge Wilson 
7552acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
7562acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
7572acef22dSMatthew Ahrens 	    &expected_refcount);
7580713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
7590713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
7605cabbc6bSPrashanth Sreenivasa 	actual_refcount += get_obsolete_refcount(spa->spa_root_vdev);
7615cabbc6bSPrashanth Sreenivasa 	actual_refcount += get_prev_obsolete_spacemap_refcount(spa);
762*86714001SSerapheim Dimitropoulos 	actual_refcount += get_checkpoint_refcount(spa->spa_root_vdev);
7630713e232SGeorge Wilson 
7640713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
7652acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
7662acef22dSMatthew Ahrens 		    "actual %lld\n",
7672acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
7682acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
7690713e232SGeorge Wilson 		return (2);
7700713e232SGeorge Wilson 	}
7710713e232SGeorge Wilson 	return (0);
7720713e232SGeorge Wilson }
7730713e232SGeorge Wilson 
774fa9e4066Sahrens static void
7750713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
776fa9e4066Sahrens {
777fa9e4066Sahrens 	uint64_t alloc, offset, entry;
7785cabbc6bSPrashanth Sreenivasa 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
7795cabbc6bSPrashanth Sreenivasa 	    "INVALID", "INVALID", "INVALID", "INVALID" };
780fa9e4066Sahrens 
7810713e232SGeorge Wilson 	if (sm == NULL)
782fa9e4066Sahrens 		return;
783fa9e4066Sahrens 
7845cabbc6bSPrashanth Sreenivasa 	(void) printf("space map object %llu:\n",
7855cabbc6bSPrashanth Sreenivasa 	    (longlong_t)sm->sm_phys->smp_object);
7865cabbc6bSPrashanth Sreenivasa 	(void) printf("  smp_objsize = 0x%llx\n",
7875cabbc6bSPrashanth Sreenivasa 	    (longlong_t)sm->sm_phys->smp_objsize);
7885cabbc6bSPrashanth Sreenivasa 	(void) printf("  smp_alloc = 0x%llx\n",
7895cabbc6bSPrashanth Sreenivasa 	    (longlong_t)sm->sm_phys->smp_alloc);
7905cabbc6bSPrashanth Sreenivasa 
791fa9e4066Sahrens 	/*
792fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
793fa9e4066Sahrens 	 */
794fa9e4066Sahrens 	alloc = 0;
7950713e232SGeorge Wilson 	for (offset = 0; offset < space_map_length(sm);
7960713e232SGeorge Wilson 	    offset += sizeof (entry)) {
7970713e232SGeorge Wilson 		uint8_t mapshift = sm->sm_shift;
7980713e232SGeorge Wilson 
7990713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
8007bfdf011SNeil Perrin 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
801fa9e4066Sahrens 		if (SM_DEBUG_DECODE(entry)) {
8020713e232SGeorge Wilson 
80387219db7SVictor Latushkin 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
804fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
805fa9e4066Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
8065ad82045Snd 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
8075ad82045Snd 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
808fa9e4066Sahrens 		} else {
80987219db7SVictor Latushkin 			(void) printf("\t    [%6llu]    %c  range:"
81087219db7SVictor Latushkin 			    " %010llx-%010llx  size: %06llx\n",
811fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
812fa9e4066Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
8135ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
8140713e232SGeorge Wilson 			    mapshift) + sm->sm_start),
8155ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
8160713e232SGeorge Wilson 			    mapshift) + sm->sm_start +
8170713e232SGeorge Wilson 			    (SM_RUN_DECODE(entry) << mapshift)),
8185ad82045Snd 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
819fa9e4066Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
820fa9e4066Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
821fa9e4066Sahrens 			else
822fa9e4066Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
823fa9e4066Sahrens 		}
824fa9e4066Sahrens 	}
8250713e232SGeorge Wilson 	if (alloc != space_map_allocated(sm)) {
826fa9e4066Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
827fa9e4066Sahrens 		    "with space map summary (%llu)\n",
8280713e232SGeorge Wilson 		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
829fa9e4066Sahrens 	}
830fa9e4066Sahrens }
831fa9e4066Sahrens 
832d6e555bdSGeorge Wilson static void
833d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
834d6e555bdSGeorge Wilson {
8353f9d6ad7SLin Ling 	char maxbuf[32];
836*86714001SSerapheim Dimitropoulos 	range_tree_t *rt = msp->ms_allocatable;
837*86714001SSerapheim Dimitropoulos 	avl_tree_t *t = &msp->ms_allocatable_by_size;
8380713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
839d6e555bdSGeorge Wilson 
8400a055120SJason King 	/* max sure nicenum has enough space */
8410a055120SJason King 	CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ);
8420a055120SJason King 
8430a055120SJason King 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf, sizeof (maxbuf));
844d6e555bdSGeorge Wilson 
84587219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
846d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
847d6e555bdSGeorge Wilson 	    "freepct", free_pct);
8480713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
8490713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
850d6e555bdSGeorge Wilson }
851d6e555bdSGeorge Wilson 
852fa9e4066Sahrens static void
853fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
854fa9e4066Sahrens {
855fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
856fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
8570713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
8583f9d6ad7SLin Ling 	char freebuf[32];
859fa9e4066Sahrens 
8600a055120SJason King 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf,
8610a055120SJason King 	    sizeof (freebuf));
862fa9e4066Sahrens 
863fa9e4066Sahrens 	(void) printf(
86487219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
8650713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
8660713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
867fa9e4066Sahrens 
8680713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
869d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
8700713e232SGeorge Wilson 		metaslab_load_wait(msp);
8710713e232SGeorge Wilson 		if (!msp->ms_loaded) {
8720713e232SGeorge Wilson 			VERIFY0(metaslab_load(msp));
873*86714001SSerapheim Dimitropoulos 			range_tree_stat_verify(msp->ms_allocatable);
8740713e232SGeorge Wilson 		}
875bc3975b5SVictor Latushkin 		dump_metaslab_stats(msp);
8760713e232SGeorge Wilson 		metaslab_unload(msp);
877d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
878d6e555bdSGeorge Wilson 	}
879d6e555bdSGeorge Wilson 
8800713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
8812acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
8820713e232SGeorge Wilson 		/*
8830713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
8840713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
8850713e232SGeorge Wilson 		 */
8862e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
8872e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
8880713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
8892e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
8900713e232SGeorge Wilson 	}
8910713e232SGeorge Wilson 
8920713e232SGeorge Wilson 	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
8930713e232SGeorge Wilson 		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
894d6e555bdSGeorge Wilson 
8950713e232SGeorge Wilson 		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
896d6e555bdSGeorge Wilson 	}
89787219db7SVictor Latushkin }
898fa9e4066Sahrens 
89987219db7SVictor Latushkin static void
90087219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
90187219db7SVictor Latushkin {
90287219db7SVictor Latushkin 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
90387219db7SVictor Latushkin 	    (u_longlong_t)vd->vdev_id,
90487219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
90587219db7SVictor Latushkin 	    "offset", "spacemap", "free");
90687219db7SVictor Latushkin 	(void) printf("\t%15s   %19s   %15s   %10s\n",
90787219db7SVictor Latushkin 	    "---------------", "-------------------",
90887219db7SVictor Latushkin 	    "---------------", "-------------");
909fa9e4066Sahrens }
910fa9e4066Sahrens 
9112e4c9986SGeorge Wilson static void
9122e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
9132e4c9986SGeorge Wilson {
9142e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
9152e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
9162e4c9986SGeorge Wilson 	uint64_t fragmentation;
9172e4c9986SGeorge Wilson 
9182e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
9192e4c9986SGeorge Wilson 
9203f7978d0SAlan Somers 	for (unsigned c = 0; c < rvd->vdev_children; c++) {
9212e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
9222e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
9232e4c9986SGeorge Wilson 
9242e4c9986SGeorge Wilson 		if (mg->mg_class != mc)
9252e4c9986SGeorge Wilson 			continue;
9262e4c9986SGeorge Wilson 
9272e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
9282e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
9292e4c9986SGeorge Wilson 
9302e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
9312e4c9986SGeorge Wilson 		    "fragmentation",
9322e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
9332e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
9342e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
9352e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
9362e4c9986SGeorge Wilson 		} else {
9372e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
9382e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
9392e4c9986SGeorge Wilson 		}
9402e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
9412e4c9986SGeorge Wilson 	}
9422e4c9986SGeorge Wilson 
9432e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
9442e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
9452e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
9462e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
9472e4c9986SGeorge Wilson 	else
9482e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
9492e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
9502e4c9986SGeorge Wilson }
9512e4c9986SGeorge Wilson 
9525cabbc6bSPrashanth Sreenivasa static void
9535cabbc6bSPrashanth Sreenivasa print_vdev_indirect(vdev_t *vd)
9545cabbc6bSPrashanth Sreenivasa {
9555cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
9565cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
9575cabbc6bSPrashanth Sreenivasa 	vdev_indirect_births_t *vib = vd->vdev_indirect_births;
9585cabbc6bSPrashanth Sreenivasa 
9595cabbc6bSPrashanth Sreenivasa 	if (vim == NULL) {
9605cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vib, ==, NULL);
9615cabbc6bSPrashanth Sreenivasa 		return;
9625cabbc6bSPrashanth Sreenivasa 	}
9635cabbc6bSPrashanth Sreenivasa 
9645cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vdev_indirect_mapping_object(vim), ==,
9655cabbc6bSPrashanth Sreenivasa 	    vic->vic_mapping_object);
9665cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vdev_indirect_births_object(vib), ==,
9675cabbc6bSPrashanth Sreenivasa 	    vic->vic_births_object);
9685cabbc6bSPrashanth Sreenivasa 
9695cabbc6bSPrashanth Sreenivasa 	(void) printf("indirect births obj %llu:\n",
9705cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vic->vic_births_object);
9715cabbc6bSPrashanth Sreenivasa 	(void) printf("    vib_count = %llu\n",
9725cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vdev_indirect_births_count(vib));
9735cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < vdev_indirect_births_count(vib); i++) {
9745cabbc6bSPrashanth Sreenivasa 		vdev_indirect_birth_entry_phys_t *cur_vibe =
9755cabbc6bSPrashanth Sreenivasa 		    &vib->vib_entries[i];
9765cabbc6bSPrashanth Sreenivasa 		(void) printf("\toffset %llx -> txg %llu\n",
9775cabbc6bSPrashanth Sreenivasa 		    (longlong_t)cur_vibe->vibe_offset,
9785cabbc6bSPrashanth Sreenivasa 		    (longlong_t)cur_vibe->vibe_phys_birth_txg);
9795cabbc6bSPrashanth Sreenivasa 	}
9805cabbc6bSPrashanth Sreenivasa 	(void) printf("\n");
9815cabbc6bSPrashanth Sreenivasa 
9825cabbc6bSPrashanth Sreenivasa 	(void) printf("indirect mapping obj %llu:\n",
9835cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vic->vic_mapping_object);
9845cabbc6bSPrashanth Sreenivasa 	(void) printf("    vim_max_offset = 0x%llx\n",
9855cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vdev_indirect_mapping_max_offset(vim));
9865cabbc6bSPrashanth Sreenivasa 	(void) printf("    vim_bytes_mapped = 0x%llx\n",
9875cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vdev_indirect_mapping_bytes_mapped(vim));
9885cabbc6bSPrashanth Sreenivasa 	(void) printf("    vim_count = %llu\n",
9895cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vdev_indirect_mapping_num_entries(vim));
9905cabbc6bSPrashanth Sreenivasa 
9915cabbc6bSPrashanth Sreenivasa 	if (dump_opt['d'] <= 5 && dump_opt['m'] <= 3)
9925cabbc6bSPrashanth Sreenivasa 		return;
9935cabbc6bSPrashanth Sreenivasa 
9945cabbc6bSPrashanth Sreenivasa 	uint32_t *counts = vdev_indirect_mapping_load_obsolete_counts(vim);
9955cabbc6bSPrashanth Sreenivasa 
9965cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
9975cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_entry_phys_t *vimep =
9985cabbc6bSPrashanth Sreenivasa 		    &vim->vim_entries[i];
9995cabbc6bSPrashanth Sreenivasa 		(void) printf("\t<%llx:%llx:%llx> -> "
10005cabbc6bSPrashanth Sreenivasa 		    "<%llx:%llx:%llx> (%x obsolete)\n",
10015cabbc6bSPrashanth Sreenivasa 		    (longlong_t)vd->vdev_id,
10025cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
10035cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
10045cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_GET_VDEV(&vimep->vimep_dst),
10055cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_GET_OFFSET(&vimep->vimep_dst),
10065cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
10075cabbc6bSPrashanth Sreenivasa 		    counts[i]);
10085cabbc6bSPrashanth Sreenivasa 	}
10095cabbc6bSPrashanth Sreenivasa 	(void) printf("\n");
10105cabbc6bSPrashanth Sreenivasa 
10115cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd);
10125cabbc6bSPrashanth Sreenivasa 	if (obsolete_sm_object != 0) {
10135cabbc6bSPrashanth Sreenivasa 		objset_t *mos = vd->vdev_spa->spa_meta_objset;
10145cabbc6bSPrashanth Sreenivasa 		(void) printf("obsolete space map object %llu:\n",
10155cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_sm_object);
10165cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_obsolete_sm != NULL);
10175cabbc6bSPrashanth Sreenivasa 		ASSERT3U(space_map_object(vd->vdev_obsolete_sm), ==,
10185cabbc6bSPrashanth Sreenivasa 		    obsolete_sm_object);
10195cabbc6bSPrashanth Sreenivasa 		dump_spacemap(mos, vd->vdev_obsolete_sm);
10205cabbc6bSPrashanth Sreenivasa 		(void) printf("\n");
10215cabbc6bSPrashanth Sreenivasa 	}
10225cabbc6bSPrashanth Sreenivasa }
10235cabbc6bSPrashanth Sreenivasa 
1024fa9e4066Sahrens static void
1025fa9e4066Sahrens dump_metaslabs(spa_t *spa)
1026fa9e4066Sahrens {
102787219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
102887219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
1029fa9e4066Sahrens 
1030fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
1031fa9e4066Sahrens 
103287219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
103387219db7SVictor Latushkin 		c = zopt_object[0];
103487219db7SVictor Latushkin 
103587219db7SVictor Latushkin 		if (c >= children)
103687219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
1037fa9e4066Sahrens 
103887219db7SVictor Latushkin 		if (zopt_objects > 1) {
103987219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
104087219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
104187219db7SVictor Latushkin 
104287219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
104387219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
104487219db7SVictor Latushkin 					dump_metaslab(
104587219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
104687219db7SVictor Latushkin 				else
104787219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
104887219db7SVictor Latushkin 					    "number %llu\n",
104987219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
105087219db7SVictor Latushkin 			}
105187219db7SVictor Latushkin 			(void) printf("\n");
105287219db7SVictor Latushkin 			return;
105387219db7SVictor Latushkin 		}
105487219db7SVictor Latushkin 		children = c + 1;
105587219db7SVictor Latushkin 	}
105687219db7SVictor Latushkin 	for (; c < children; c++) {
105787219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
105887219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
1059fa9e4066Sahrens 
10605cabbc6bSPrashanth Sreenivasa 		print_vdev_indirect(vd);
10615cabbc6bSPrashanth Sreenivasa 
1062fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
1063fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
1064fa9e4066Sahrens 		(void) printf("\n");
1065fa9e4066Sahrens 	}
1066fa9e4066Sahrens }
1067fa9e4066Sahrens 
1068b24ab676SJeff Bonwick static void
1069b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
1070b24ab676SJeff Bonwick {
1071b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
1072b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
10733f7978d0SAlan Somers 	const char *types[4] = { "ditto", "single", "double", "triple" };
1074b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
1075b24ab676SJeff Bonwick 	blkptr_t blk;
1076b24ab676SJeff Bonwick 
1077b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1078b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
1079b24ab676SJeff Bonwick 			continue;
1080bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
108143466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
1082b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
1083b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
1084b24ab676SJeff Bonwick 		    types[p], blkbuf);
1085b24ab676SJeff Bonwick 	}
1086b24ab676SJeff Bonwick }
1087b24ab676SJeff Bonwick 
1088b24ab676SJeff Bonwick static void
1089b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
1090b24ab676SJeff Bonwick {
1091b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
1092b24ab676SJeff Bonwick 
1093b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
1094b24ab676SJeff Bonwick 		return;
1095b24ab676SJeff Bonwick 
1096b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
1097b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
1098b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
1099b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
1100b24ab676SJeff Bonwick 
1101b24ab676SJeff Bonwick 	dedup = rD / D;
1102b24ab676SJeff Bonwick 	compress = rL / rP;
1103b24ab676SJeff Bonwick 	copies = rD / rP;
1104b24ab676SJeff Bonwick 
1105b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
1106b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
1107b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
1108b24ab676SJeff Bonwick }
1109b24ab676SJeff Bonwick 
1110b24ab676SJeff Bonwick static void
1111b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
1112b24ab676SJeff Bonwick {
1113b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
1114b24ab676SJeff Bonwick 	ddt_entry_t dde;
1115b24ab676SJeff Bonwick 	uint64_t walk = 0;
1116b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1117b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
1118b24ab676SJeff Bonwick 	int error;
1119b24ab676SJeff Bonwick 
1120b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
1121b24ab676SJeff Bonwick 
1122b24ab676SJeff Bonwick 	if (error == ENOENT)
1123b24ab676SJeff Bonwick 		return;
1124b24ab676SJeff Bonwick 	ASSERT(error == 0);
1125b24ab676SJeff Bonwick 
11267448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
11277448a079SGeorge Wilson 		return;
11287448a079SGeorge Wilson 
1129b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
1130b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
1131b24ab676SJeff Bonwick 
1132b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
1133b24ab676SJeff Bonwick 
1134b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
1135b24ab676SJeff Bonwick 	    name,
1136b24ab676SJeff Bonwick 	    (u_longlong_t)count,
1137b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
1138b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
1139b24ab676SJeff Bonwick 
1140b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
1141b24ab676SJeff Bonwick 		return;
1142b24ab676SJeff Bonwick 
11439eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
1144b24ab676SJeff Bonwick 
1145b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
1146b24ab676SJeff Bonwick 		return;
1147b24ab676SJeff Bonwick 
1148b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
1149b24ab676SJeff Bonwick 		return;
1150b24ab676SJeff Bonwick 
1151b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
1152b24ab676SJeff Bonwick 
1153bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
1154b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
1155b24ab676SJeff Bonwick 
1156b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
1157b24ab676SJeff Bonwick 
1158b24ab676SJeff Bonwick 	(void) printf("\n");
1159b24ab676SJeff Bonwick }
1160b24ab676SJeff Bonwick 
1161b24ab676SJeff Bonwick static void
1162b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
1163b24ab676SJeff Bonwick {
11643f7978d0SAlan Somers 	ddt_histogram_t ddh_total;
11653f7978d0SAlan Somers 	ddt_stat_t dds_total;
11663f7978d0SAlan Somers 
11673f7978d0SAlan Somers 	bzero(&ddh_total, sizeof (ddh_total));
11683f7978d0SAlan Somers 	bzero(&dds_total, sizeof (dds_total));
1169b24ab676SJeff Bonwick 
1170b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1171b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
1172b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
1173b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
1174b24ab676SJeff Bonwick 			    class++) {
1175b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
1176b24ab676SJeff Bonwick 			}
1177b24ab676SJeff Bonwick 		}
1178b24ab676SJeff Bonwick 	}
1179b24ab676SJeff Bonwick 
11809eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
1181b24ab676SJeff Bonwick 
1182b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
1183b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
1184b24ab676SJeff Bonwick 		return;
1185b24ab676SJeff Bonwick 	}
1186b24ab676SJeff Bonwick 
1187b24ab676SJeff Bonwick 	(void) printf("\n");
1188b24ab676SJeff Bonwick 
1189b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
1190b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
11919eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
11929eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
1193b24ab676SJeff Bonwick 	}
1194b24ab676SJeff Bonwick 
1195b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
1196b24ab676SJeff Bonwick }
1197b24ab676SJeff Bonwick 
11988ad4d6ddSJeff Bonwick static void
11990713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
12008ad4d6ddSJeff Bonwick {
12010713e232SGeorge Wilson 	char *prefix = arg;
12028ad4d6ddSJeff Bonwick 
12038ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
12048ad4d6ddSJeff Bonwick 	    prefix,
12058ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
12068ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
12078ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
12088ad4d6ddSJeff Bonwick }
12098ad4d6ddSJeff Bonwick 
1210fa9e4066Sahrens static void
1211fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
1212fa9e4066Sahrens {
12138ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
12148ad4d6ddSJeff Bonwick 	boolean_t required;
12153f7978d0SAlan Somers 	const char *name[DTL_TYPES] = { "missing", "partial", "scrub",
12163f7978d0SAlan Somers 		"outage" };
12178ad4d6ddSJeff Bonwick 	char prefix[256];
12188ad4d6ddSJeff Bonwick 
12198f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
12208ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
12218ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
1222fa9e4066Sahrens 
1223fa9e4066Sahrens 	if (indent == 0)
1224fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
1225fa9e4066Sahrens 
12268ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
1227e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
12288ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
12298ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
1230fa9e4066Sahrens 
12318ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
12320713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
12330713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
12348ad4d6ddSJeff Bonwick 			continue;
12358ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
12368ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
12370713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
12388ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
12390713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1240fa9e4066Sahrens 	}
1241fa9e4066Sahrens 
12423f7978d0SAlan Somers 	for (unsigned c = 0; c < vd->vdev_children; c++)
1243fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
1244fa9e4066Sahrens }
1245fa9e4066Sahrens 
12468f18d1faSGeorge Wilson static void
12478f18d1faSGeorge Wilson dump_history(spa_t *spa)
12488f18d1faSGeorge Wilson {
12498f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
1250e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
12518f18d1faSGeorge Wilson 	uint_t num = 0;
12528f18d1faSGeorge Wilson 	int error;
12538f18d1faSGeorge Wilson 	time_t tsec;
12548f18d1faSGeorge Wilson 	struct tm t;
12558f18d1faSGeorge Wilson 	char tbuf[30];
12568f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
12578f18d1faSGeorge Wilson 
12583f84190cSMatthew Ahrens 	char *buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
12598f18d1faSGeorge Wilson 	do {
12603f84190cSMatthew Ahrens 		len = SPA_MAXBLOCKSIZE;
1261e4161df6SVictor Latushkin 
12628f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
12638f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
12648f18d1faSGeorge Wilson 			    "error %d\n", error);
12653f84190cSMatthew Ahrens 			umem_free(buf, SPA_MAXBLOCKSIZE);
12668f18d1faSGeorge Wilson 			return;
12678f18d1faSGeorge Wilson 		}
12688f18d1faSGeorge Wilson 
12698f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
12708f18d1faSGeorge Wilson 			break;
12718f18d1faSGeorge Wilson 
12728f18d1faSGeorge Wilson 		off -= resid;
12738f18d1faSGeorge Wilson 	} while (len != 0);
12743f84190cSMatthew Ahrens 	umem_free(buf, SPA_MAXBLOCKSIZE);
12758f18d1faSGeorge Wilson 
12768f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
12773f7978d0SAlan Somers 	for (unsigned i = 0; i < num; i++) {
12788f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
12798f18d1faSGeorge Wilson 		char *cmd, *intstr;
12804445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
12818f18d1faSGeorge Wilson 
12828f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
12838f18d1faSGeorge Wilson 		    &time) != 0)
12844445fffbSMatthew Ahrens 			goto next;
12858f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
12868f18d1faSGeorge Wilson 		    &cmd) != 0) {
12878f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
12888f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
12894445fffbSMatthew Ahrens 				goto next;
12908f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
12918f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
12928f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
12938f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
12944445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
12954445fffbSMatthew Ahrens 				goto next;
12968f18d1faSGeorge Wilson 
12978f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
12988f18d1faSGeorge Wilson 			    sizeof (internalstr),
12993f7978d0SAlan Somers 			    "[internal %s txg:%ju] %s",
13003f7978d0SAlan Somers 			    zfs_history_event_names[ievent], (uintmax_t)txg,
13018f18d1faSGeorge Wilson 			    intstr);
13028f18d1faSGeorge Wilson 			cmd = internalstr;
13038f18d1faSGeorge Wilson 		}
13048f18d1faSGeorge Wilson 		tsec = time;
13058f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
13068f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
13078f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
13084445fffbSMatthew Ahrens 		printed = B_TRUE;
13094445fffbSMatthew Ahrens 
13104445fffbSMatthew Ahrens next:
13114445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
13124445fffbSMatthew Ahrens 			if (!printed)
13134445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
13144445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
13154445fffbSMatthew Ahrens 		}
13168f18d1faSGeorge Wilson 	}
13178f18d1faSGeorge Wilson }
13188f18d1faSGeorge Wilson 
1319fa9e4066Sahrens /*ARGSUSED*/
1320fa9e4066Sahrens static void
1321fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1322fa9e4066Sahrens {
1323fa9e4066Sahrens }
1324fa9e4066Sahrens 
1325fa9e4066Sahrens static uint64_t
13267802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
13277802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1328fa9e4066Sahrens {
1329b24ab676SJeff Bonwick 	if (dnp == NULL) {
1330b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1331b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1332b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1333b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1334b24ab676SJeff Bonwick 	}
1335b24ab676SJeff Bonwick 
1336b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1337fa9e4066Sahrens 
1338b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1339b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1340fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1341fa9e4066Sahrens }
1342fa9e4066Sahrens 
134344cd46caSbillm static void
134443466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
134544cd46caSbillm {
1346cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1347b24ab676SJeff Bonwick 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1348b24ab676SJeff Bonwick 
1349490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
135043466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1351b24ab676SJeff Bonwick 		return;
1352b24ab676SJeff Bonwick 	}
135344cd46caSbillm 
13545d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
13555d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
13565d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
13575d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
13585d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
13595d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
13605d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
13615d7b4d43SMatthew Ahrens 		return;
13625d7b4d43SMatthew Ahrens 	}
136344cd46caSbillm 
13645d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1365b24ab676SJeff Bonwick 	for (int i = 0; i < ndvas; i++)
136643466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
136743466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
136844cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
136944cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
137044cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
137144cd46caSbillm 
137243466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
137343466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
137470163ac5SPrakash Surya 		    buflen - strlen(blkbuf),
137570163ac5SPrakash Surya 		    "%llxL B=%llu",
137670163ac5SPrakash Surya 		    (u_longlong_t)BP_GET_LSIZE(bp),
137743466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
137843466aaeSMax Grossman 	} else {
137943466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
138043466aaeSMax Grossman 		    buflen - strlen(blkbuf),
138143466aaeSMax Grossman 		    "%llxL/%llxP F=%llu B=%llu/%llu",
138243466aaeSMax Grossman 		    (u_longlong_t)BP_GET_LSIZE(bp),
138343466aaeSMax Grossman 		    (u_longlong_t)BP_GET_PSIZE(bp),
13845d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
138543466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth,
138643466aaeSMax Grossman 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
138743466aaeSMax Grossman 	}
138844cd46caSbillm }
138944cd46caSbillm 
139088b7b0f2SMatthew Ahrens static void
13917802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
139288b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1393fa9e4066Sahrens {
139488b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1395fa9e4066Sahrens 	int l;
1396fa9e4066Sahrens 
13975d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
13985d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
13995d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
14005d7b4d43SMatthew Ahrens 	}
1401fa9e4066Sahrens 
1402b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1403fa9e4066Sahrens 
1404fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1405fa9e4066Sahrens 
1406fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1407fa9e4066Sahrens 		if (l == zb->zb_level) {
140888b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1409fa9e4066Sahrens 		} else {
141088b7b0f2SMatthew Ahrens 			(void) printf(" ");
1411fa9e4066Sahrens 		}
1412fa9e4066Sahrens 	}
1413fa9e4066Sahrens 
141443466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
141588b7b0f2SMatthew Ahrens 	(void) printf("%s\n", blkbuf);
141688b7b0f2SMatthew Ahrens }
141788b7b0f2SMatthew Ahrens 
141888b7b0f2SMatthew Ahrens static int
141988b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
14207802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
142188b7b0f2SMatthew Ahrens {
1422e4161df6SVictor Latushkin 	int err = 0;
142388b7b0f2SMatthew Ahrens 
142488b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
142588b7b0f2SMatthew Ahrens 		return (0);
142688b7b0f2SMatthew Ahrens 
142788b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
142888b7b0f2SMatthew Ahrens 
142943466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
14307adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
143188b7b0f2SMatthew Ahrens 		int i;
143288b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
143388b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
143488b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
143588b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
143688b7b0f2SMatthew Ahrens 
14371b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
143888b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
143988b7b0f2SMatthew Ahrens 		if (err)
144088b7b0f2SMatthew Ahrens 			return (err);
14413f9d6ad7SLin Ling 		ASSERT(buf->b_data);
144288b7b0f2SMatthew Ahrens 
144388b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
144488b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
144588b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
14467802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
144788b7b0f2SMatthew Ahrens 
144888b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
144988b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
145088b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
145188b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
145288b7b0f2SMatthew Ahrens 			if (err)
145388b7b0f2SMatthew Ahrens 				break;
14545d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
145588b7b0f2SMatthew Ahrens 		}
14568ad4d6ddSJeff Bonwick 		if (!err)
14575d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
1458dcbf3bd6SGeorge Wilson 		arc_buf_destroy(buf, &buf);
1459fa9e4066Sahrens 	}
1460fa9e4066Sahrens 
146188b7b0f2SMatthew Ahrens 	return (err);
1462fa9e4066Sahrens }
1463fa9e4066Sahrens 
1464fa9e4066Sahrens /*ARGSUSED*/
1465fa9e4066Sahrens static void
146688b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1467fa9e4066Sahrens {
146888b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
146988b7b0f2SMatthew Ahrens 	int j;
14707802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1471fa9e4066Sahrens 
1472fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1473fa9e4066Sahrens 
1474503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
147588b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
147688b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
147788b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1478503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
147988b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
148088b7b0f2SMatthew Ahrens 	}
1481fa9e4066Sahrens 
1482fa9e4066Sahrens 	(void) printf("\n");
1483fa9e4066Sahrens }
1484fa9e4066Sahrens 
1485fa9e4066Sahrens /*ARGSUSED*/
1486fa9e4066Sahrens static void
1487fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1488fa9e4066Sahrens {
1489fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1490fa9e4066Sahrens 	time_t crtime;
14913f9d6ad7SLin Ling 	char nice[32];
1492fa9e4066Sahrens 
14930a055120SJason King 	/* make sure nicenum has enough space */
14940a055120SJason King 	CTASSERT(sizeof (nice) >= NN_NUMBUF_SZ);
14950a055120SJason King 
1496fa9e4066Sahrens 	if (dd == NULL)
1497fa9e4066Sahrens 		return;
1498fa9e4066Sahrens 
1499da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1500fa9e4066Sahrens 
1501fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1502fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1503fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1504fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1505fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1506fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
15073cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
15083cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1509fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1510fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
15110a055120SJason King 	zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice));
151274e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
15130a055120SJason King 	zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice));
151474e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
15150a055120SJason King 	zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice));
151674e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
15170a055120SJason King 	zdb_nicenum(dd->dd_quota, nice, sizeof (nice));
151874e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
15190a055120SJason King 	zdb_nicenum(dd->dd_reserved, nice, sizeof (nice));
152074e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1521fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1522fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1523ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1524ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
152574e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
152674e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
152774e7dc98SMatthew Ahrens 
152874e7dc98SMatthew Ahrens #define	DO(which) \
15290a055120SJason King 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \
15300a055120SJason King 	    sizeof (nice)); \
153174e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
153274e7dc98SMatthew Ahrens 	DO(HEAD);
153374e7dc98SMatthew Ahrens 	DO(SNAP);
153474e7dc98SMatthew Ahrens 	DO(CHILD);
153574e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
153674e7dc98SMatthew Ahrens 	DO(REFRSRV);
153774e7dc98SMatthew Ahrens #undef DO
1538fa9e4066Sahrens }
1539fa9e4066Sahrens 
1540fa9e4066Sahrens /*ARGSUSED*/
1541fa9e4066Sahrens static void
1542fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1543fa9e4066Sahrens {
1544fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1545fa9e4066Sahrens 	time_t crtime;
15463f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1547fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1548fa9e4066Sahrens 
15490a055120SJason King 	/* make sure nicenum has enough space */
15500a055120SJason King 	CTASSERT(sizeof (used) >= NN_NUMBUF_SZ);
15510a055120SJason King 	CTASSERT(sizeof (compressed) >= NN_NUMBUF_SZ);
15520a055120SJason King 	CTASSERT(sizeof (uncompressed) >= NN_NUMBUF_SZ);
15530a055120SJason King 	CTASSERT(sizeof (unique) >= NN_NUMBUF_SZ);
15540a055120SJason King 
1555fa9e4066Sahrens 	if (ds == NULL)
1556fa9e4066Sahrens 		return;
1557fa9e4066Sahrens 
1558fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1559fa9e4066Sahrens 	crtime = ds->ds_creation_time;
15600a055120SJason King 	zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used));
15610a055120SJason King 	zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed));
15620a055120SJason King 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed,
15630a055120SJason King 	    sizeof (uncompressed));
15640a055120SJason King 	zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique));
156543466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1566fa9e4066Sahrens 
1567088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1568fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1569fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1570fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1571fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1572fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1573fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1574fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1575fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1576fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1577fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1578fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1579842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1580842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1581fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1582fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1583fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1584fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1585fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1586fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1587fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1588fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1589fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1590fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1591fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1592fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1593fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
159499653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
159599653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1596088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1597088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1598bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1599bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1600fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1601fa9e4066Sahrens }
1602fa9e4066Sahrens 
1603ad135b5dSChristopher Siden /* ARGSUSED */
1604ad135b5dSChristopher Siden static int
1605ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1606ad135b5dSChristopher Siden {
1607ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1608ad135b5dSChristopher Siden 
1609ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
161043466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1611ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1612ad135b5dSChristopher Siden 	}
1613ad135b5dSChristopher Siden 	return (0);
1614ad135b5dSChristopher Siden }
1615ad135b5dSChristopher Siden 
1616ad135b5dSChristopher Siden static void
16173f7978d0SAlan Somers dump_bptree(objset_t *os, uint64_t obj, const char *name)
1618ad135b5dSChristopher Siden {
1619ad135b5dSChristopher Siden 	char bytes[32];
1620ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1621ad135b5dSChristopher Siden 	dmu_buf_t *db;
1622ad135b5dSChristopher Siden 
16230a055120SJason King 	/* make sure nicenum has enough space */
16240a055120SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
16250a055120SJason King 
1626ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1627ad135b5dSChristopher Siden 		return;
1628ad135b5dSChristopher Siden 
1629b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1630ad135b5dSChristopher Siden 	bt = db->db_data;
16310a055120SJason King 	zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes));
1632ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1633ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1634ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1635ad135b5dSChristopher Siden 
1636ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1637ad135b5dSChristopher Siden 		return;
1638ad135b5dSChristopher Siden 
1639ad135b5dSChristopher Siden 	(void) printf("\n");
1640ad135b5dSChristopher Siden 
1641ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1642ad135b5dSChristopher Siden }
1643ad135b5dSChristopher Siden 
1644cde58dbcSMatthew Ahrens /* ARGSUSED */
1645cde58dbcSMatthew Ahrens static int
1646cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1647cde58dbcSMatthew Ahrens {
1648cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1649cde58dbcSMatthew Ahrens 
1650cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
165143466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1652cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1653cde58dbcSMatthew Ahrens 	return (0);
1654cde58dbcSMatthew Ahrens }
1655cde58dbcSMatthew Ahrens 
1656fa9e4066Sahrens static void
16573f7978d0SAlan Somers dump_full_bpobj(bpobj_t *bpo, const char *name, int indent)
1658fa9e4066Sahrens {
16593f9d6ad7SLin Ling 	char bytes[32];
16603f9d6ad7SLin Ling 	char comp[32];
16613f9d6ad7SLin Ling 	char uncomp[32];
1662fa9e4066Sahrens 
16630a055120SJason King 	/* make sure nicenum has enough space */
16640a055120SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
16650a055120SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
16660a055120SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
16670a055120SJason King 
1668fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1669fa9e4066Sahrens 		return;
1670fa9e4066Sahrens 
16710a055120SJason King 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes));
1672d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
16730a055120SJason King 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp));
16740a055120SJason King 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp));
1675d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1676732885fcSMatthew Ahrens 		    "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1677d0475637SMatthew Ahrens 		    indent * 8, name,
1678d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1679d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1680cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1681732885fcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
168299653d4eSeschrock 		    bytes, comp, uncomp);
1683d0475637SMatthew Ahrens 
1684d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1685d0475637SMatthew Ahrens 			uint64_t subobj;
1686d0475637SMatthew Ahrens 			bpobj_t subbpo;
1687d0475637SMatthew Ahrens 			int error;
1688d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1689d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1690d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1691d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1692d0475637SMatthew Ahrens 			if (error != 0) {
1693d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1694d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1695d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1696d0475637SMatthew Ahrens 				continue;
1697d0475637SMatthew Ahrens 			}
1698732885fcSMatthew Ahrens 			dump_full_bpobj(&subbpo, "subobj", indent + 1);
169977061867SMatthew Ahrens 			bpobj_close(&subbpo);
1700d0475637SMatthew Ahrens 		}
170199653d4eSeschrock 	} else {
1702d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1703d0475637SMatthew Ahrens 		    indent * 8, name,
1704d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1705d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1706d0475637SMatthew Ahrens 		    bytes);
170799653d4eSeschrock 	}
1708fa9e4066Sahrens 
1709cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1710fa9e4066Sahrens 		return;
1711fa9e4066Sahrens 
1712fa9e4066Sahrens 
1713d0475637SMatthew Ahrens 	if (indent == 0) {
1714d0475637SMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1715d0475637SMatthew Ahrens 		(void) printf("\n");
1716d0475637SMatthew Ahrens 	}
1717cde58dbcSMatthew Ahrens }
171844cd46caSbillm 
1719cde58dbcSMatthew Ahrens static void
1720cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1721cde58dbcSMatthew Ahrens {
1722cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1723d0475637SMatthew Ahrens 	uint64_t unused;
1724cde58dbcSMatthew Ahrens 	char bytes[32];
1725cde58dbcSMatthew Ahrens 	char comp[32];
1726cde58dbcSMatthew Ahrens 	char uncomp[32];
1727cde58dbcSMatthew Ahrens 
17280a055120SJason King 	/* make sure nicenum has enough space */
17290a055120SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
17300a055120SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
17310a055120SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
17320a055120SJason King 
1733cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1734cde58dbcSMatthew Ahrens 		return;
1735cde58dbcSMatthew Ahrens 
173690c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
1737732885fcSMatthew Ahrens 		dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
173890c76c66SMatthew Ahrens 		return;
173990c76c66SMatthew Ahrens 	}
174090c76c66SMatthew Ahrens 
17410a055120SJason King 	zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes));
17420a055120SJason King 	zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp));
17430a055120SJason King 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp));
1744cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1745cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1746cde58dbcSMatthew Ahrens 
1747cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1748cde58dbcSMatthew Ahrens 		return;
1749cde58dbcSMatthew Ahrens 
1750cde58dbcSMatthew Ahrens 	(void) printf("\n");
1751cde58dbcSMatthew Ahrens 
1752d0475637SMatthew Ahrens 	/* force the tree to be loaded */
1753d0475637SMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1754d0475637SMatthew Ahrens 
1755cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1756cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1757d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1758d0475637SMatthew Ahrens 			char buf[128];
1759de05b588SRichard Yao 			(void) snprintf(buf, sizeof (buf),
1760de05b588SRichard Yao 			    "mintxg %llu -> obj %llu",
1761d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1762d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1763fa9e4066Sahrens 
1764732885fcSMatthew Ahrens 			dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1765d0475637SMatthew Ahrens 		} else {
1766d0475637SMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1767d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1768d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1769d0475637SMatthew Ahrens 
1770d0475637SMatthew Ahrens 		}
1771cde58dbcSMatthew Ahrens 	}
1772fa9e4066Sahrens }
1773fa9e4066Sahrens 
1774e0d35c44Smarks static avl_tree_t idx_tree;
1775e0d35c44Smarks static avl_tree_t domain_tree;
1776e0d35c44Smarks static boolean_t fuid_table_loaded;
1777ed61ec1dSYuri Pankov static objset_t *sa_os = NULL;
1778ed61ec1dSYuri Pankov static sa_attr_type_t *sa_attr_table = NULL;
1779ed61ec1dSYuri Pankov 
1780ed61ec1dSYuri Pankov static int
1781ed61ec1dSYuri Pankov open_objset(const char *path, dmu_objset_type_t type, void *tag, objset_t **osp)
1782ed61ec1dSYuri Pankov {
1783ed61ec1dSYuri Pankov 	int err;
1784ed61ec1dSYuri Pankov 	uint64_t sa_attrs = 0;
1785ed61ec1dSYuri Pankov 	uint64_t version = 0;
1786ed61ec1dSYuri Pankov 
1787ed61ec1dSYuri Pankov 	VERIFY3P(sa_os, ==, NULL);
1788ed61ec1dSYuri Pankov 	err = dmu_objset_own(path, type, B_TRUE, tag, osp);
1789ed61ec1dSYuri Pankov 	if (err != 0) {
1790ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "failed to own dataset '%s': %s\n", path,
1791ed61ec1dSYuri Pankov 		    strerror(err));
1792ed61ec1dSYuri Pankov 		return (err);
1793ed61ec1dSYuri Pankov 	}
1794ed61ec1dSYuri Pankov 
1795ed61ec1dSYuri Pankov 	if (dmu_objset_type(*osp) == DMU_OST_ZFS) {
1796ed61ec1dSYuri Pankov 		(void) zap_lookup(*osp, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1797ed61ec1dSYuri Pankov 		    8, 1, &version);
1798ed61ec1dSYuri Pankov 		if (version >= ZPL_VERSION_SA) {
1799ed61ec1dSYuri Pankov 			(void) zap_lookup(*osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1800ed61ec1dSYuri Pankov 			    8, 1, &sa_attrs);
1801ed61ec1dSYuri Pankov 		}
1802ed61ec1dSYuri Pankov 		err = sa_setup(*osp, sa_attrs, zfs_attr_table, ZPL_END,
1803ed61ec1dSYuri Pankov 		    &sa_attr_table);
1804ed61ec1dSYuri Pankov 		if (err != 0) {
1805ed61ec1dSYuri Pankov 			(void) fprintf(stderr, "sa_setup failed: %s\n",
1806ed61ec1dSYuri Pankov 			    strerror(err));
1807ed61ec1dSYuri Pankov 			dmu_objset_disown(*osp, tag);
1808ed61ec1dSYuri Pankov 			*osp = NULL;
1809ed61ec1dSYuri Pankov 		}
1810ed61ec1dSYuri Pankov 	}
1811ed61ec1dSYuri Pankov 	sa_os = *osp;
1812ed61ec1dSYuri Pankov 
1813ed61ec1dSYuri Pankov 	return (0);
1814ed61ec1dSYuri Pankov }
1815ed61ec1dSYuri Pankov 
1816ed61ec1dSYuri Pankov static void
1817ed61ec1dSYuri Pankov close_objset(objset_t *os, void *tag)
1818ed61ec1dSYuri Pankov {
1819ed61ec1dSYuri Pankov 	VERIFY3P(os, ==, sa_os);
1820ed61ec1dSYuri Pankov 	if (os->os_sa != NULL)
1821ed61ec1dSYuri Pankov 		sa_tear_down(os);
1822ed61ec1dSYuri Pankov 	dmu_objset_disown(os, tag);
1823ed61ec1dSYuri Pankov 	sa_attr_table = NULL;
1824ed61ec1dSYuri Pankov 	sa_os = NULL;
1825ed61ec1dSYuri Pankov }
1826e0d35c44Smarks 
1827e0d35c44Smarks static void
1828e0d35c44Smarks fuid_table_destroy()
1829e0d35c44Smarks {
1830e0d35c44Smarks 	if (fuid_table_loaded) {
1831e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1832e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1833e0d35c44Smarks 	}
1834e0d35c44Smarks }
1835e0d35c44Smarks 
1836e0d35c44Smarks /*
1837e0d35c44Smarks  * print uid or gid information.
1838e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1839e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1840d0475637SMatthew Ahrens  * the domain-rid string.
1841e0d35c44Smarks  */
1842e0d35c44Smarks static void
1843e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1844e0d35c44Smarks {
1845e0d35c44Smarks 	if (FUID_INDEX(id)) {
1846e0d35c44Smarks 		char *domain;
1847e0d35c44Smarks 
1848e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1849e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1850e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1851e0d35c44Smarks 	} else {
1852e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1853e0d35c44Smarks 	}
1854e0d35c44Smarks 
1855e0d35c44Smarks }
1856e0d35c44Smarks 
1857e0d35c44Smarks static void
18580a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1859e0d35c44Smarks {
1860e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1861e0d35c44Smarks 
18620a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
18630a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1864e0d35c44Smarks 
1865e0d35c44Smarks 	/* Load domain table, if not already loaded */
1866e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1867e0d35c44Smarks 		uint64_t fuid_obj;
1868e0d35c44Smarks 
1869e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1870e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1871e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
187289459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1873e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1874e0d35c44Smarks 		    &idx_tree, &domain_tree);
1875e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1876e0d35c44Smarks 	}
1877e0d35c44Smarks 
18780a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
18790a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1880e0d35c44Smarks }
1881e0d35c44Smarks 
1882fa9e4066Sahrens /*ARGSUSED*/
1883fa9e4066Sahrens static void
1884fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1885fa9e4066Sahrens {
1886fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
18870a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
18880a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
18890a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
18908f2529deSMark Shellenbaum 	uint64_t pflags;
18910a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
18920a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
18938f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
18940a586ceaSMark Shellenbaum 	int idx = 0;
189555434c77Sek 	int error;
1896fa9e4066Sahrens 
1897ed61ec1dSYuri Pankov 	VERIFY3P(os, ==, sa_os);
18980a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
18990a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
19000a586ceaSMark Shellenbaum 		return;
19010a586ceaSMark Shellenbaum 	}
19020a586ceaSMark Shellenbaum 
19030a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
19040a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
19050a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
19060a586ceaSMark Shellenbaum 	    &links, 8);
19070a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
19080a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
19090a586ceaSMark Shellenbaum 	    &mode, 8);
19100a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
19110a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
19120a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
19130a586ceaSMark Shellenbaum 	    &fsize, 8);
19140a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
19150a586ceaSMark Shellenbaum 	    acctm, 16);
19160a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
19170a586ceaSMark Shellenbaum 	    modtm, 16);
19180a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
19190a586ceaSMark Shellenbaum 	    crtm, 16);
19200a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
19210a586ceaSMark Shellenbaum 	    chgtm, 16);
19228f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
19238f2529deSMark Shellenbaum 	    &pflags, 8);
19240a586ceaSMark Shellenbaum 
19250a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
19260a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
19270a586ceaSMark Shellenbaum 		return;
19280a586ceaSMark Shellenbaum 	}
1929fa9e4066Sahrens 
19300a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
19310a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
19320a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
19330a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
1934fa9e4066Sahrens 
1935e548d2faSAlan Somers 	if (dump_opt['d'] > 4) {
1936e548d2faSAlan Somers 		error = zfs_obj_to_path(os, object, path, sizeof (path));
1937e548d2faSAlan Somers 		if (error != 0) {
1938e548d2faSAlan Somers 			(void) snprintf(path, sizeof (path),
1939e548d2faSAlan Somers 			    "\?\?\?<object#%llu>", (u_longlong_t)object);
1940e548d2faSAlan Somers 		}
1941e548d2faSAlan Somers 		(void) printf("\tpath	%s\n", path);
1942e548d2faSAlan Somers 	}
19430a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
1944fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1945fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1946fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1947fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
19480a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
19490a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
19500a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
19510a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
19520a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
19538f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
19540a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
19550a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
19560a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
19570a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
19580a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
19590a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
19600a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
1961fa9e4066Sahrens }
1962fa9e4066Sahrens 
1963fa9e4066Sahrens /*ARGSUSED*/
1964fa9e4066Sahrens static void
1965fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1966fa9e4066Sahrens {
1967fa9e4066Sahrens }
1968fa9e4066Sahrens 
1969fa9e4066Sahrens /*ARGSUSED*/
1970fa9e4066Sahrens static void
1971fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1972fa9e4066Sahrens {
1973fa9e4066Sahrens }
1974fa9e4066Sahrens 
19756de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1976fa9e4066Sahrens 	dump_none,		/* unallocated			*/
1977fa9e4066Sahrens 	dump_zap,		/* object directory		*/
1978fa9e4066Sahrens 	dump_uint64,		/* object array			*/
1979fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
1980fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1981732885fcSMatthew Ahrens 	dump_none,		/* bpobj			*/
1982732885fcSMatthew Ahrens 	dump_bpobj,		/* bpobj header			*/
1983fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
1984fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
1985fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
1986fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
1987fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
1988ea8dc4b6Seschrock 	dump_dsl_dir,		/* DSL directory		*/
1989fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
1990fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1991fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
1992fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1993fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
1994da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
1995fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
1996e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
1997fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
1998fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
1999fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
2000fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
2001fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
2002fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
2003fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
2004ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
200506eeb2adSek 	dump_uint8,		/* SPA history			*/
20064445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
2007b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
2008ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
2009da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
2010da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
2011da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
2012da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
2013088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
2014088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
201514843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group used		*/
201614843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group quota		*/
2017842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
2018486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
2019b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
20200a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
20210a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
20220a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
20230a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
20243f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
20253f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
2026cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
2027cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
2028cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
2029732885fcSMatthew Ahrens 	dump_bpobj_subobjs,	/* bpobj subobjs		*/
20300a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
2031fa9e4066Sahrens };
2032fa9e4066Sahrens 
2033fa9e4066Sahrens static void
2034fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
2035fa9e4066Sahrens {
2036fa9e4066Sahrens 	dmu_buf_t *db = NULL;
2037fa9e4066Sahrens 	dmu_object_info_t doi;
2038fa9e4066Sahrens 	dnode_t *dn;
2039fa9e4066Sahrens 	void *bonus = NULL;
2040fa9e4066Sahrens 	size_t bsize = 0;
20413f9d6ad7SLin Ling 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
20423f9d6ad7SLin Ling 	char bonus_size[32];
2043fa9e4066Sahrens 	char aux[50];
2044fa9e4066Sahrens 	int error;
2045fa9e4066Sahrens 
20460a055120SJason King 	/* make sure nicenum has enough space */
20470a055120SJason King 	CTASSERT(sizeof (iblk) >= NN_NUMBUF_SZ);
20480a055120SJason King 	CTASSERT(sizeof (dblk) >= NN_NUMBUF_SZ);
20490a055120SJason King 	CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
20500a055120SJason King 	CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
20510a055120SJason King 	CTASSERT(sizeof (bonus_size) >= NN_NUMBUF_SZ);
20520a055120SJason King 
2053fa9e4066Sahrens 	if (*print_header) {
2054b24ab676SJeff Bonwick 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
2055b24ab676SJeff Bonwick 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
2056b24ab676SJeff Bonwick 		    "%full", "type");
2057fa9e4066Sahrens 		*print_header = 0;
2058fa9e4066Sahrens 	}
2059fa9e4066Sahrens 
2060fa9e4066Sahrens 	if (object == 0) {
2061744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
2062fa9e4066Sahrens 	} else {
2063ea8dc4b6Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
2064ea8dc4b6Seschrock 		if (error)
2065ea8dc4b6Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
2066ea8dc4b6Seschrock 			    object, error);
2067fa9e4066Sahrens 		bonus = db->db_data;
2068fa9e4066Sahrens 		bsize = db->db_size;
2069744947dcSTom Erickson 		dn = DB_DNODE((dmu_buf_impl_t *)db);
2070fa9e4066Sahrens 	}
2071fa9e4066Sahrens 	dmu_object_info_from_dnode(dn, &doi);
2072fa9e4066Sahrens 
20730a055120SJason King 	zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk));
20740a055120SJason King 	zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk));
20750a055120SJason King 	zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize));
20760a055120SJason King 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize));
20770a055120SJason King 	zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size));
2078b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
2079bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
2080bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
2081fa9e4066Sahrens 
2082fa9e4066Sahrens 	aux[0] = '\0';
2083fa9e4066Sahrens 
2084e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
2085fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
20866de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
2087e7437265Sahrens 	}
2088fa9e4066Sahrens 
2089e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
2090fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
20916de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
2092e7437265Sahrens 	}
2093fa9e4066Sahrens 
2094b24ab676SJeff Bonwick 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
2095b24ab676SJeff Bonwick 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
2096b24ab676SJeff Bonwick 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
2097fa9e4066Sahrens 
2098fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
2099b24ab676SJeff Bonwick 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
2100b24ab676SJeff Bonwick 		    "", "", "", "", "", bonus_size, "bonus",
21016de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
2102fa9e4066Sahrens 	}
2103fa9e4066Sahrens 
2104fa9e4066Sahrens 	if (verbosity >= 4) {
21050a586ceaSMark Shellenbaum 		(void) printf("\tdnode flags: %s%s%s\n",
210614843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
210714843421SMatthew Ahrens 		    "USED_BYTES " : "",
210814843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
21090a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
21100a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
21110a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
211214843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
211314843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
211414843421SMatthew Ahrens 
21156de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
21166de8f417SVictor Latushkin 		    bonus, bsize);
21176de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
2118fa9e4066Sahrens 		*print_header = 1;
2119fa9e4066Sahrens 	}
2120fa9e4066Sahrens 
2121fa9e4066Sahrens 	if (verbosity >= 5)
212288b7b0f2SMatthew Ahrens 		dump_indirect(dn);
2123fa9e4066Sahrens 
2124fa9e4066Sahrens 	if (verbosity >= 5) {
2125fa9e4066Sahrens 		/*
2126fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
2127fa9e4066Sahrens 		 */
2128fa9e4066Sahrens 		uint64_t start = 0;
2129fa9e4066Sahrens 		uint64_t end;
2130fa9e4066Sahrens 		uint64_t blkfill = 1;
2131fa9e4066Sahrens 		int minlvl = 1;
2132fa9e4066Sahrens 
2133fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
2134fa9e4066Sahrens 			minlvl = 0;
2135fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
2136fa9e4066Sahrens 		}
2137fa9e4066Sahrens 
2138fa9e4066Sahrens 		for (;;) {
21393f9d6ad7SLin Ling 			char segsize[32];
21400a055120SJason King 			/* make sure nicenum has enough space */
21410a055120SJason King 			CTASSERT(sizeof (segsize) >= NN_NUMBUF_SZ);
2142cdb0ab79Smaybee 			error = dnode_next_offset(dn,
2143cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
2144fa9e4066Sahrens 			if (error)
2145fa9e4066Sahrens 				break;
2146fa9e4066Sahrens 			end = start;
2147cdb0ab79Smaybee 			error = dnode_next_offset(dn,
2148cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
21490a055120SJason King 			zdb_nicenum(end - start, segsize, sizeof (segsize));
2150fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
2151fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
2152fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
2153fa9e4066Sahrens 			if (error)
2154fa9e4066Sahrens 				break;
2155fa9e4066Sahrens 			start = end;
2156fa9e4066Sahrens 		}
2157fa9e4066Sahrens 	}
2158fa9e4066Sahrens 
2159fa9e4066Sahrens 	if (db != NULL)
2160ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
2161fa9e4066Sahrens }
2162fa9e4066Sahrens 
21633f7978d0SAlan Somers static const char *objset_types[DMU_OST_NUMTYPES] = {
2164fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
2165fa9e4066Sahrens 
2166fa9e4066Sahrens static void
2167fa9e4066Sahrens dump_dir(objset_t *os)
2168fa9e4066Sahrens {
2169fa9e4066Sahrens 	dmu_objset_stats_t dds;
2170fa9e4066Sahrens 	uint64_t object, object_count;
2171a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
21723f9d6ad7SLin Ling 	char numbuf[32];
217314843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
21749adfa60dSMatthew Ahrens 	char osname[ZFS_MAX_DATASET_NAME_LEN];
21753f7978d0SAlan Somers 	const char *type = "UNKNOWN";
2176fa9e4066Sahrens 	int verbosity = dump_opt['d'];
2177fa9e4066Sahrens 	int print_header = 1;
21783f7978d0SAlan Somers 	unsigned i;
21793f7978d0SAlan Somers 	int error;
2180fa9e4066Sahrens 
21810a055120SJason King 	/* make sure nicenum has enough space */
21820a055120SJason King 	CTASSERT(sizeof (numbuf) >= NN_NUMBUF_SZ);
21830a055120SJason King 
21843b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2185a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
21863b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2187fa9e4066Sahrens 
2188fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
2189fa9e4066Sahrens 		type = objset_types[dds.dds_type];
2190fa9e4066Sahrens 
2191fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
2192fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
21935d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
2194c1379625SJustin T. Gibbs 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
2195c1379625SJustin T. Gibbs 		    dd_used_bytes;
2196a2eea2e1Sahrens 	} else {
2197a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
2198fa9e4066Sahrens 	}
2199fa9e4066Sahrens 
22005d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
2201fa9e4066Sahrens 
22020a055120SJason King 	zdb_nicenum(refdbytes, numbuf, sizeof (numbuf));
2203fa9e4066Sahrens 
2204fa9e4066Sahrens 	if (verbosity >= 4) {
220543466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
220643466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
220743466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
2208fa9e4066Sahrens 	} else {
2209fa9e4066Sahrens 		blkbuf[0] = '\0';
2210fa9e4066Sahrens 	}
2211fa9e4066Sahrens 
2212fa9e4066Sahrens 	dmu_objset_name(os, osname);
2213fa9e4066Sahrens 
2214a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
2215fa9e4066Sahrens 	    "%s, %llu objects%s\n",
2216fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
2217fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
2218a2eea2e1Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
2219fa9e4066Sahrens 
2220b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
2221b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
2222b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
2223b24ab676SJeff Bonwick 			    &print_header);
2224b24ab676SJeff Bonwick 		(void) printf("\n");
2225b24ab676SJeff Bonwick 		return;
2226b24ab676SJeff Bonwick 	}
2227b24ab676SJeff Bonwick 
2228b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
2229b24ab676SJeff Bonwick 		dump_intent_log(dmu_objset_zil(os));
2230fa9e4066Sahrens 
22315cabbc6bSPrashanth Sreenivasa 	if (dmu_objset_ds(os) != NULL) {
22325cabbc6bSPrashanth Sreenivasa 		dsl_dataset_t *ds = dmu_objset_ds(os);
22335cabbc6bSPrashanth Sreenivasa 		dump_deadlist(&ds->ds_deadlist);
22345cabbc6bSPrashanth Sreenivasa 
22355cabbc6bSPrashanth Sreenivasa 		if (dsl_dataset_remap_deadlist_exists(ds)) {
22365cabbc6bSPrashanth Sreenivasa 			(void) printf("ds_remap_deadlist:\n");
22375cabbc6bSPrashanth Sreenivasa 			dump_deadlist(&ds->ds_remap_deadlist);
22385cabbc6bSPrashanth Sreenivasa 		}
22395cabbc6bSPrashanth Sreenivasa 	}
2240fa9e4066Sahrens 
2241fa9e4066Sahrens 	if (verbosity < 2)
2242fa9e4066Sahrens 		return;
2243fa9e4066Sahrens 
224443466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
2245088f3894Sahrens 		return;
2246088f3894Sahrens 
2247fa9e4066Sahrens 	dump_object(os, 0, verbosity, &print_header);
224814843421SMatthew Ahrens 	object_count = 0;
2249744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
2250744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
225114843421SMatthew Ahrens 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
225214843421SMatthew Ahrens 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
225314843421SMatthew Ahrens 	}
2254fa9e4066Sahrens 
2255fa9e4066Sahrens 	object = 0;
22566754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
2257fa9e4066Sahrens 		dump_object(os, object, verbosity, &print_header);
2258fa9e4066Sahrens 		object_count++;
2259fa9e4066Sahrens 	}
2260fa9e4066Sahrens 
2261a2eea2e1Sahrens 	ASSERT3U(object_count, ==, usedobjs);
2262fa9e4066Sahrens 
2263fa9e4066Sahrens 	(void) printf("\n");
2264fa9e4066Sahrens 
2265ccba0801SRich Morris 	if (error != ESRCH) {
2266ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2267ccba0801SRich Morris 		abort();
2268ccba0801SRich Morris 	}
2269fa9e4066Sahrens }
2270fa9e4066Sahrens 
2271fa9e4066Sahrens static void
227253b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2273fa9e4066Sahrens {
2274fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
2275fa9e4066Sahrens 
22763f7978d0SAlan Somers 	(void) printf("%s", header ? header : "");
2277fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2278fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2279fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2280fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2281fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
2282fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
2283fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
2284fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
228543466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2286fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
2287fa9e4066Sahrens 	}
2288*86714001SSerapheim Dimitropoulos 	(void) printf("\tcheckpoint_txg = %llu\n",
2289*86714001SSerapheim Dimitropoulos 	    (u_longlong_t)ub->ub_checkpoint_txg);
22903f7978d0SAlan Somers 	(void) printf("%s", footer ? footer : "");
2291fa9e4066Sahrens }
2292fa9e4066Sahrens 
2293fa9e4066Sahrens static void
229407428bdfSVictor Latushkin dump_config(spa_t *spa)
2295fa9e4066Sahrens {
229607428bdfSVictor Latushkin 	dmu_buf_t *db;
229707428bdfSVictor Latushkin 	size_t nvsize = 0;
229807428bdfSVictor Latushkin 	int error = 0;
229907428bdfSVictor Latushkin 
2300fa9e4066Sahrens 
230107428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
230207428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
230307428bdfSVictor Latushkin 
230407428bdfSVictor Latushkin 	if (error == 0) {
230507428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
230607428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
230707428bdfSVictor Latushkin 
230807428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
230907428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
231007428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
231107428bdfSVictor Latushkin 	} else {
231207428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
231307428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
2314fa9e4066Sahrens 	}
2315fa9e4066Sahrens }
2316fa9e4066Sahrens 
2317c5904d13Seschrock static void
2318c5904d13Seschrock dump_cachefile(const char *cachefile)
2319c5904d13Seschrock {
2320c5904d13Seschrock 	int fd;
2321c5904d13Seschrock 	struct stat64 statbuf;
2322c5904d13Seschrock 	char *buf;
2323c5904d13Seschrock 	nvlist_t *config;
2324c5904d13Seschrock 
2325c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2326c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
2327c5904d13Seschrock 		    strerror(errno));
2328c5904d13Seschrock 		exit(1);
2329c5904d13Seschrock 	}
2330c5904d13Seschrock 
2331c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
2332c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
2333c5904d13Seschrock 		    strerror(errno));
2334c5904d13Seschrock 		exit(1);
2335c5904d13Seschrock 	}
2336c5904d13Seschrock 
2337c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
2338c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
2339c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2340c5904d13Seschrock 		exit(1);
2341c5904d13Seschrock 	}
2342c5904d13Seschrock 
2343c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2344c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2345c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2346c5904d13Seschrock 		exit(1);
2347c5904d13Seschrock 	}
2348c5904d13Seschrock 
2349c5904d13Seschrock 	(void) close(fd);
2350c5904d13Seschrock 
2351c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2352c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2353c5904d13Seschrock 		exit(1);
2354c5904d13Seschrock 	}
2355c5904d13Seschrock 
2356c5904d13Seschrock 	free(buf);
2357c5904d13Seschrock 
2358c5904d13Seschrock 	dump_nvlist(config, 0);
2359c5904d13Seschrock 
2360c5904d13Seschrock 	nvlist_free(config);
2361c5904d13Seschrock }
2362c5904d13Seschrock 
236353b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
236453b9a4a9SVictor Latushkin 
236553b9a4a9SVictor Latushkin static void
236653b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
236753b9a4a9SVictor Latushkin {
236853b9a4a9SVictor Latushkin 	vdev_t vd;
236953b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
237053b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
237153b9a4a9SVictor Latushkin 
237253b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
237353b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
237453b9a4a9SVictor Latushkin 
237553b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
237653b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
237753b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
237853b9a4a9SVictor Latushkin 
237953b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
238053b9a4a9SVictor Latushkin 			continue;
238153b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
238253b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
238353b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
238453b9a4a9SVictor Latushkin 	}
238553b9a4a9SVictor Latushkin }
238653b9a4a9SVictor Latushkin 
2387ed61ec1dSYuri Pankov static char curpath[PATH_MAX];
2388ed61ec1dSYuri Pankov 
2389ed61ec1dSYuri Pankov /*
2390ed61ec1dSYuri Pankov  * Iterate through the path components, recursively passing
2391ed61ec1dSYuri Pankov  * current one's obj and remaining path until we find the obj
2392ed61ec1dSYuri Pankov  * for the last one.
2393ed61ec1dSYuri Pankov  */
2394ed61ec1dSYuri Pankov static int
2395ed61ec1dSYuri Pankov dump_path_impl(objset_t *os, uint64_t obj, char *name)
2396ed61ec1dSYuri Pankov {
2397ed61ec1dSYuri Pankov 	int err;
2398ed61ec1dSYuri Pankov 	int header = 1;
2399ed61ec1dSYuri Pankov 	uint64_t child_obj;
2400ed61ec1dSYuri Pankov 	char *s;
2401ed61ec1dSYuri Pankov 	dmu_buf_t *db;
2402ed61ec1dSYuri Pankov 	dmu_object_info_t doi;
2403ed61ec1dSYuri Pankov 
2404ed61ec1dSYuri Pankov 	if ((s = strchr(name, '/')) != NULL)
2405ed61ec1dSYuri Pankov 		*s = '\0';
2406ed61ec1dSYuri Pankov 	err = zap_lookup(os, obj, name, 8, 1, &child_obj);
2407ed61ec1dSYuri Pankov 
2408ed61ec1dSYuri Pankov 	(void) strlcat(curpath, name, sizeof (curpath));
2409ed61ec1dSYuri Pankov 
2410ed61ec1dSYuri Pankov 	if (err != 0) {
2411ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "failed to lookup %s: %s\n",
2412ed61ec1dSYuri Pankov 		    curpath, strerror(err));
2413ed61ec1dSYuri Pankov 		return (err);
2414ed61ec1dSYuri Pankov 	}
2415ed61ec1dSYuri Pankov 
2416ed61ec1dSYuri Pankov 	child_obj = ZFS_DIRENT_OBJ(child_obj);
2417ed61ec1dSYuri Pankov 	err = sa_buf_hold(os, child_obj, FTAG, &db);
2418ed61ec1dSYuri Pankov 	if (err != 0) {
2419ed61ec1dSYuri Pankov 		(void) fprintf(stderr,
2420ed61ec1dSYuri Pankov 		    "failed to get SA dbuf for obj %llu: %s\n",
2421ed61ec1dSYuri Pankov 		    (u_longlong_t)child_obj, strerror(err));
2422ed61ec1dSYuri Pankov 		return (EINVAL);
2423ed61ec1dSYuri Pankov 	}
2424ed61ec1dSYuri Pankov 	dmu_object_info_from_db(db, &doi);
2425ed61ec1dSYuri Pankov 	sa_buf_rele(db, FTAG);
2426ed61ec1dSYuri Pankov 
2427ed61ec1dSYuri Pankov 	if (doi.doi_bonus_type != DMU_OT_SA &&
2428ed61ec1dSYuri Pankov 	    doi.doi_bonus_type != DMU_OT_ZNODE) {
2429ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "invalid bonus type %d for obj %llu\n",
2430ed61ec1dSYuri Pankov 		    doi.doi_bonus_type, (u_longlong_t)child_obj);
2431ed61ec1dSYuri Pankov 		return (EINVAL);
2432ed61ec1dSYuri Pankov 	}
2433ed61ec1dSYuri Pankov 
2434ed61ec1dSYuri Pankov 	if (dump_opt['v'] > 6) {
2435ed61ec1dSYuri Pankov 		(void) printf("obj=%llu %s type=%d bonustype=%d\n",
2436ed61ec1dSYuri Pankov 		    (u_longlong_t)child_obj, curpath, doi.doi_type,
2437ed61ec1dSYuri Pankov 		    doi.doi_bonus_type);
2438ed61ec1dSYuri Pankov 	}
2439ed61ec1dSYuri Pankov 
2440ed61ec1dSYuri Pankov 	(void) strlcat(curpath, "/", sizeof (curpath));
2441ed61ec1dSYuri Pankov 
2442ed61ec1dSYuri Pankov 	switch (doi.doi_type) {
2443ed61ec1dSYuri Pankov 	case DMU_OT_DIRECTORY_CONTENTS:
2444ed61ec1dSYuri Pankov 		if (s != NULL && *(s + 1) != '\0')
2445ed61ec1dSYuri Pankov 			return (dump_path_impl(os, child_obj, s + 1));
2446ed61ec1dSYuri Pankov 		/*FALLTHROUGH*/
2447ed61ec1dSYuri Pankov 	case DMU_OT_PLAIN_FILE_CONTENTS:
2448ed61ec1dSYuri Pankov 		dump_object(os, child_obj, dump_opt['v'], &header);
2449ed61ec1dSYuri Pankov 		return (0);
2450ed61ec1dSYuri Pankov 	default:
2451ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "object %llu has non-file/directory "
2452ed61ec1dSYuri Pankov 		    "type %d\n", (u_longlong_t)obj, doi.doi_type);
2453ed61ec1dSYuri Pankov 		break;
2454ed61ec1dSYuri Pankov 	}
2455ed61ec1dSYuri Pankov 
2456ed61ec1dSYuri Pankov 	return (EINVAL);
2457ed61ec1dSYuri Pankov }
2458ed61ec1dSYuri Pankov 
2459ed61ec1dSYuri Pankov /*
2460ed61ec1dSYuri Pankov  * Dump the blocks for the object specified by path inside the dataset.
2461ed61ec1dSYuri Pankov  */
2462ed61ec1dSYuri Pankov static int
2463ed61ec1dSYuri Pankov dump_path(char *ds, char *path)
2464ed61ec1dSYuri Pankov {
2465ed61ec1dSYuri Pankov 	int err;
2466ed61ec1dSYuri Pankov 	objset_t *os;
2467ed61ec1dSYuri Pankov 	uint64_t root_obj;
2468ed61ec1dSYuri Pankov 
2469ed61ec1dSYuri Pankov 	err = open_objset(ds, DMU_OST_ZFS, FTAG, &os);
2470ed61ec1dSYuri Pankov 	if (err != 0)
2471ed61ec1dSYuri Pankov 		return (err);
2472ed61ec1dSYuri Pankov 
2473ed61ec1dSYuri Pankov 	err = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &root_obj);
2474ed61ec1dSYuri Pankov 	if (err != 0) {
2475ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "can't lookup root znode: %s\n",
2476ed61ec1dSYuri Pankov 		    strerror(err));
2477ed61ec1dSYuri Pankov 		dmu_objset_disown(os, FTAG);
2478ed61ec1dSYuri Pankov 		return (EINVAL);
2479ed61ec1dSYuri Pankov 	}
2480ed61ec1dSYuri Pankov 
2481ed61ec1dSYuri Pankov 	(void) snprintf(curpath, sizeof (curpath), "dataset=%s path=/", ds);
2482ed61ec1dSYuri Pankov 
2483ed61ec1dSYuri Pankov 	err = dump_path_impl(os, root_obj, path);
2484ed61ec1dSYuri Pankov 
2485ed61ec1dSYuri Pankov 	close_objset(os, FTAG);
2486ed61ec1dSYuri Pankov 	return (err);
2487ed61ec1dSYuri Pankov }
2488ed61ec1dSYuri Pankov 
248964723e36SYuri Pankov static int
2490fa9e4066Sahrens dump_label(const char *dev)
2491fa9e4066Sahrens {
2492fa9e4066Sahrens 	int fd;
2493fa9e4066Sahrens 	vdev_label_t label;
249464723e36SYuri Pankov 	char path[MAXPATHLEN];
249564723e36SYuri Pankov 	char *buf = label.vl_vdev_phys.vp_nvlist;
2496fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2497fa9e4066Sahrens 	struct stat64 statbuf;
249853b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
249964723e36SYuri Pankov 	boolean_t label_found = B_FALSE;
250064723e36SYuri Pankov 
250164723e36SYuri Pankov 	(void) strlcpy(path, dev, sizeof (path));
250264723e36SYuri Pankov 	if (dev[0] == '/') {
250364723e36SYuri Pankov 		if (strncmp(dev, ZFS_DISK_ROOTD,
250464723e36SYuri Pankov 		    strlen(ZFS_DISK_ROOTD)) == 0) {
250564723e36SYuri Pankov 			(void) snprintf(path, sizeof (path), "%s%s",
250664723e36SYuri Pankov 			    ZFS_RDISK_ROOTD, dev + strlen(ZFS_DISK_ROOTD));
250764723e36SYuri Pankov 		}
250864723e36SYuri Pankov 	} else if (stat64(path, &statbuf) != 0) {
250964723e36SYuri Pankov 		char *s;
2510c6065d0fSGeorge Wilson 
251164723e36SYuri Pankov 		(void) snprintf(path, sizeof (path), "%s%s", ZFS_RDISK_ROOTD,
251264723e36SYuri Pankov 		    dev);
251322c8b958SYuri Pankov 		if (((s = strrchr(dev, 's')) == NULL &&
251422c8b958SYuri Pankov 		    (s = strchr(dev, 'p')) == NULL) ||
251522c8b958SYuri Pankov 		    !isdigit(*(s + 1)))
251664723e36SYuri Pankov 			(void) strlcat(path, "s0", sizeof (path));
2517fa9e4066Sahrens 	}
2518fa9e4066Sahrens 
251922c8b958SYuri Pankov 	if ((fd = open64(path, O_RDONLY)) < 0) {
252022c8b958SYuri Pankov 		(void) fprintf(stderr, "cannot open '%s': %s\n", path,
2521fa9e4066Sahrens 		    strerror(errno));
2522c6065d0fSGeorge Wilson 		exit(1);
2523c6065d0fSGeorge Wilson 	}
2524c6065d0fSGeorge Wilson 
252522c8b958SYuri Pankov 	if (fstat64(fd, &statbuf) != 0) {
252622c8b958SYuri Pankov 		(void) fprintf(stderr, "failed to stat '%s': %s\n", path,
252722c8b958SYuri Pankov 		    strerror(errno));
252822c8b958SYuri Pankov 		(void) close(fd);
252964723e36SYuri Pankov 		exit(1);
253064723e36SYuri Pankov 	}
253164723e36SYuri Pankov 
253222c8b958SYuri Pankov 	if (S_ISBLK(statbuf.st_mode)) {
253322c8b958SYuri Pankov 		(void) fprintf(stderr,
253422c8b958SYuri Pankov 		    "cannot use '%s': character device required\n", path);
253522c8b958SYuri Pankov 		(void) close(fd);
2536c6065d0fSGeorge Wilson 		exit(1);
2537fa9e4066Sahrens 	}
2538fa9e4066Sahrens 
2539fa9e4066Sahrens 	psize = statbuf.st_size;
2540fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2541fa9e4066Sahrens 
254253b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
2543fa9e4066Sahrens 		nvlist_t *config = NULL;
2544fa9e4066Sahrens 
254564723e36SYuri Pankov 		if (!dump_opt['q']) {
254664723e36SYuri Pankov 			(void) printf("------------------------------------\n");
254764723e36SYuri Pankov 			(void) printf("LABEL %d\n", l);
254864723e36SYuri Pankov 			(void) printf("------------------------------------\n");
254964723e36SYuri Pankov 		}
2550fa9e4066Sahrens 
25510d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
2552fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
255364723e36SYuri Pankov 			if (!dump_opt['q'])
255464723e36SYuri Pankov 				(void) printf("failed to read label %d\n", l);
2555fa9e4066Sahrens 			continue;
2556fa9e4066Sahrens 		}
2557fa9e4066Sahrens 
2558fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
255964723e36SYuri Pankov 			if (!dump_opt['q'])
256064723e36SYuri Pankov 				(void) printf("failed to unpack label %d\n", l);
256153b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
256253b9a4a9SVictor Latushkin 		} else {
256353b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
256453b9a4a9SVictor Latushkin 
256564723e36SYuri Pankov 			if (!dump_opt['q'])
256664723e36SYuri Pankov 				dump_nvlist(config, 4);
256753b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
256853b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
256953b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
257053b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
257153b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
257253b9a4a9SVictor Latushkin 			nvlist_free(config);
257364723e36SYuri Pankov 			label_found = B_TRUE;
2574fa9e4066Sahrens 		}
257553b9a4a9SVictor Latushkin 		if (dump_opt['u'])
257653b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
2577fa9e4066Sahrens 	}
2578c6065d0fSGeorge Wilson 
2579c6065d0fSGeorge Wilson 	(void) close(fd);
258064723e36SYuri Pankov 
258164723e36SYuri Pankov 	return (label_found ? 0 : 2);
2582fa9e4066Sahrens }
2583fa9e4066Sahrens 
2584ca0cc391SMatthew Ahrens static uint64_t dataset_feature_count[SPA_FEATURES];
25855cabbc6bSPrashanth Sreenivasa static uint64_t remap_deadlist_count = 0;
2586b5152584SMatthew Ahrens 
2587fa9e4066Sahrens /*ARGSUSED*/
25881d452cf5Sahrens static int
2589fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2590fa9e4066Sahrens {
2591fa9e4066Sahrens 	int error;
2592fa9e4066Sahrens 	objset_t *os;
2593fa9e4066Sahrens 
2594ed61ec1dSYuri Pankov 	error = open_objset(dsname, DMU_OST_ANY, FTAG, &os);
2595ed61ec1dSYuri Pankov 	if (error != 0)
25961d452cf5Sahrens 		return (0);
2597ca0cc391SMatthew Ahrens 
2598ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2599ca0cc391SMatthew Ahrens 		if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2600ca0cc391SMatthew Ahrens 			continue;
2601ca0cc391SMatthew Ahrens 		ASSERT(spa_feature_table[f].fi_flags &
2602ca0cc391SMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET);
2603ca0cc391SMatthew Ahrens 		dataset_feature_count[f]++;
2604ca0cc391SMatthew Ahrens 	}
2605ca0cc391SMatthew Ahrens 
26065cabbc6bSPrashanth Sreenivasa 	if (dsl_dataset_remap_deadlist_exists(dmu_objset_ds(os))) {
26075cabbc6bSPrashanth Sreenivasa 		remap_deadlist_count++;
26085cabbc6bSPrashanth Sreenivasa 	}
26095cabbc6bSPrashanth Sreenivasa 
2610fa9e4066Sahrens 	dump_dir(os);
2611ed61ec1dSYuri Pankov 	close_objset(os, FTAG);
2612e0d35c44Smarks 	fuid_table_destroy();
26131d452cf5Sahrens 	return (0);
2614fa9e4066Sahrens }
2615fa9e4066Sahrens 
2616fa9e4066Sahrens /*
2617b24ab676SJeff Bonwick  * Block statistics.
2618fa9e4066Sahrens  */
2619b5152584SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2620fa9e4066Sahrens typedef struct zdb_blkstats {
2621490d05b9SMatthew Ahrens 	uint64_t zb_asize;
2622490d05b9SMatthew Ahrens 	uint64_t zb_lsize;
2623490d05b9SMatthew Ahrens 	uint64_t zb_psize;
2624490d05b9SMatthew Ahrens 	uint64_t zb_count;
2625d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2626d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2627490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2628fa9e4066Sahrens } zdb_blkstats_t;
2629fa9e4066Sahrens 
2630b24ab676SJeff Bonwick /*
2631b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2632b24ab676SJeff Bonwick  */
2633b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2634b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2635ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2636ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2637b24ab676SJeff Bonwick 
26383f7978d0SAlan Somers static const char *zdb_ot_extname[] = {
2639b24ab676SJeff Bonwick 	"deferred free",
2640b24ab676SJeff Bonwick 	"dedup ditto",
2641ad135b5dSChristopher Siden 	"other",
2642b24ab676SJeff Bonwick 	"Total",
2643b24ab676SJeff Bonwick };
2644fa9e4066Sahrens 
264588b7b0f2SMatthew Ahrens #define	ZB_TOTAL	DN_MAX_LEVELS
2646fa9e4066Sahrens 
2647fa9e4066Sahrens typedef struct zdb_cb {
2648b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
26495cabbc6bSPrashanth Sreenivasa 	uint64_t	zcb_removing_size;
2650*86714001SSerapheim Dimitropoulos 	uint64_t	zcb_checkpoint_size;
2651b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2652b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
26535d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
26545d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
26555d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2656490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
26573f7978d0SAlan Somers 	hrtime_t	zcb_lastprint;
2658490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2659fa9e4066Sahrens 	uint64_t	zcb_errors[256];
2660fa9e4066Sahrens 	int		zcb_readfails;
2661fa9e4066Sahrens 	int		zcb_haderrors;
2662cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
26635cabbc6bSPrashanth Sreenivasa 	uint32_t	**zcb_vd_obsolete_counts;
2664fa9e4066Sahrens } zdb_cb_t;
2665fa9e4066Sahrens 
2666fa9e4066Sahrens static void
2667cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2668b24ab676SJeff Bonwick     dmu_object_type_t type)
2669fa9e4066Sahrens {
2670b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2671b24ab676SJeff Bonwick 
2672b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2673b24ab676SJeff Bonwick 
2674b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2675b24ab676SJeff Bonwick 		return;
2676b24ab676SJeff Bonwick 
2677e14bb325SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2678fa9e4066Sahrens 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2679b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2680d5ee8a13SMatthew Ahrens 		int equal;
2681fa9e4066Sahrens 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2682fa9e4066Sahrens 
2683fa9e4066Sahrens 		zb->zb_asize += BP_GET_ASIZE(bp);
2684fa9e4066Sahrens 		zb->zb_lsize += BP_GET_LSIZE(bp);
2685fa9e4066Sahrens 		zb->zb_psize += BP_GET_PSIZE(bp);
2686fa9e4066Sahrens 		zb->zb_count++;
2687b5152584SMatthew Ahrens 
2688b5152584SMatthew Ahrens 		/*
2689b5152584SMatthew Ahrens 		 * The histogram is only big enough to record blocks up to
2690b5152584SMatthew Ahrens 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2691b5152584SMatthew Ahrens 		 * "other", bucket.
2692b5152584SMatthew Ahrens 		 */
26933f7978d0SAlan Somers 		unsigned idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2694b5152584SMatthew Ahrens 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2695b5152584SMatthew Ahrens 		zb->zb_psize_histogram[idx]++;
2696d5ee8a13SMatthew Ahrens 
2697d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2698d5ee8a13SMatthew Ahrens 
2699d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2700d5ee8a13SMatthew Ahrens 		case 2:
2701d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2702d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2703d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2704d5ee8a13SMatthew Ahrens 			break;
2705d5ee8a13SMatthew Ahrens 		case 3:
2706d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2707d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2708d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2709d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2710d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2711d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2712d5ee8a13SMatthew Ahrens 			if (equal != 0)
2713d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2714d5ee8a13SMatthew Ahrens 			break;
2715d5ee8a13SMatthew Ahrens 		}
2716d5ee8a13SMatthew Ahrens 
2717fa9e4066Sahrens 	}
2718fa9e4066Sahrens 
27195d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
27205d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
27215d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
27225d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
27235d7b4d43SMatthew Ahrens 		return;
27245d7b4d43SMatthew Ahrens 	}
27255d7b4d43SMatthew Ahrens 
2726b24ab676SJeff Bonwick 	if (dump_opt['L'])
2727b24ab676SJeff Bonwick 		return;
2728b24ab676SJeff Bonwick 
2729b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2730b24ab676SJeff Bonwick 		ddt_t *ddt;
2731b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2732b24ab676SJeff Bonwick 
2733cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2734b24ab676SJeff Bonwick 		ddt_enter(ddt);
2735b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2736b24ab676SJeff Bonwick 
2737b24ab676SJeff Bonwick 		if (dde == NULL) {
2738b24ab676SJeff Bonwick 			refcnt = 0;
2739b24ab676SJeff Bonwick 		} else {
2740b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2741b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
2742b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
2743b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
2744b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
2745d41e7643Sek 		}
2746b24ab676SJeff Bonwick 		ddt_exit(ddt);
2747d41e7643Sek 	}
2748d41e7643Sek 
2749cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2750*86714001SSerapheim Dimitropoulos 	    refcnt ? 0 : spa_min_claim_txg(zcb->zcb_spa),
2751b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2752fa9e4066Sahrens }
2753fa9e4066Sahrens 
275431d7e8faSGeorge Wilson static void
275531d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
275631d7e8faSGeorge Wilson {
275731d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
275831d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
275931d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
276031d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
27617802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
276231d7e8faSGeorge Wilson 
2763770499e1SDan Kimmel 	abd_free(zio->io_abd);
276431d7e8faSGeorge Wilson 
276531d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
276631d7e8faSGeorge Wilson 	spa->spa_scrub_inflight--;
276731d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
276831d7e8faSGeorge Wilson 
276931d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
277031d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
277131d7e8faSGeorge Wilson 
277231d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
277331d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
277431d7e8faSGeorge Wilson 
277531d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
277643466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
277731d7e8faSGeorge Wilson 		else
277831d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
277931d7e8faSGeorge Wilson 
278031d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
278131d7e8faSGeorge Wilson 		    "Got error %d reading "
278231d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
278331d7e8faSGeorge Wilson 		    ioerr,
278431d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
278531d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
278631d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
278731d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
278831d7e8faSGeorge Wilson 		    blkbuf);
278931d7e8faSGeorge Wilson 	}
279031d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
279131d7e8faSGeorge Wilson }
279231d7e8faSGeorge Wilson 
2793fa9e4066Sahrens static int
27941b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
27957802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2796fa9e4066Sahrens {
2797fa9e4066Sahrens 	zdb_cb_t *zcb = arg;
27986365109dSVictor Latushkin 	dmu_object_type_t type;
2799468c413aSTim Haley 	boolean_t is_metadata;
2800fa9e4066Sahrens 
2801a2cdcdd2SPaul Dagnelie 	if (bp == NULL)
2802a2cdcdd2SPaul Dagnelie 		return (0);
2803a2cdcdd2SPaul Dagnelie 
280443466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
280543466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
280643466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
280743466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
280843466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
280943466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
281043466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
281143466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
281243466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
281343466aaeSMax Grossman 		    blkbuf);
281443466aaeSMax Grossman 	}
281543466aaeSMax Grossman 
281643466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
281788b7b0f2SMatthew Ahrens 		return (0);
2818e14bb325SJeff Bonwick 
28196365109dSVictor Latushkin 	type = BP_GET_TYPE(bp);
2820e14bb325SJeff Bonwick 
2821ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2822ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
28236365109dSVictor Latushkin 
2824ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
28256365109dSVictor Latushkin 
28265d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
28275d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2828468c413aSTim Haley 		size_t size = BP_GET_PSIZE(bp);
2829770499e1SDan Kimmel 		abd_t *abd = abd_alloc(size, B_FALSE);
2830b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2831b24ab676SJeff Bonwick 
2832b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2833b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2834b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2835b24ab676SJeff Bonwick 
283631d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
283731d7e8faSGeorge Wilson 		while (spa->spa_scrub_inflight > max_inflight)
283831d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
283931d7e8faSGeorge Wilson 		spa->spa_scrub_inflight++;
284031d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
2841b24ab676SJeff Bonwick 
2842770499e1SDan Kimmel 		zio_nowait(zio_read(NULL, spa, bp, abd, size,
284331d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2844fa9e4066Sahrens 	}
2845fa9e4066Sahrens 
2846fa9e4066Sahrens 	zcb->zcb_readfails = 0;
2847fa9e4066Sahrens 
2848f7950bf1SMatthew Ahrens 	/* only call gethrtime() every 100 blocks */
2849f7950bf1SMatthew Ahrens 	static int iters;
2850f7950bf1SMatthew Ahrens 	if (++iters > 100)
2851f7950bf1SMatthew Ahrens 		iters = 0;
2852f7950bf1SMatthew Ahrens 	else
2853f7950bf1SMatthew Ahrens 		return (0);
2854f7950bf1SMatthew Ahrens 
2855f7950bf1SMatthew Ahrens 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2856490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
2857490d05b9SMatthew Ahrens 		char buf[10];
2858490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2859490d05b9SMatthew Ahrens 		int kb_per_sec =
2860490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2861490d05b9SMatthew Ahrens 		int sec_remaining =
2862490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2863490d05b9SMatthew Ahrens 
28640a055120SJason King 		/* make sure nicenum has enough space */
28650a055120SJason King 		CTASSERT(sizeof (buf) >= NN_NUMBUF_SZ);
28660a055120SJason King 
2867490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
2868490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
2869490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
2870490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
2871490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
2872490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
2873490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
2874490d05b9SMatthew Ahrens 		    sec_remaining % 60);
2875490d05b9SMatthew Ahrens 
2876490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
2877490d05b9SMatthew Ahrens 	}
2878490d05b9SMatthew Ahrens 
2879fa9e4066Sahrens 	return (0);
2880fa9e4066Sahrens }
2881fa9e4066Sahrens 
2882b24ab676SJeff Bonwick static void
28830713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2884b24ab676SJeff Bonwick {
28850713e232SGeorge Wilson 	vdev_t *vd = arg;
2886b24ab676SJeff Bonwick 
2887b24ab676SJeff Bonwick 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2888b24ab676SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2889b24ab676SJeff Bonwick }
2890b24ab676SJeff Bonwick 
28910713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
28922e4c9986SGeorge Wilson 	NULL	/* alloc */
2893b24ab676SJeff Bonwick };
2894b24ab676SJeff Bonwick 
2895b24ab676SJeff Bonwick static void
2896bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2897b24ab676SJeff Bonwick {
28983f7978d0SAlan Somers 	ddt_bookmark_t ddb;
2899b24ab676SJeff Bonwick 	ddt_entry_t dde;
2900b24ab676SJeff Bonwick 	int error;
2901b24ab676SJeff Bonwick 
29023f7978d0SAlan Somers 	bzero(&ddb, sizeof (ddb));
2903bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2904b24ab676SJeff Bonwick 		blkptr_t blk;
2905b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2906bbfd46c4SJeff Bonwick 
2907bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2908bbfd46c4SJeff Bonwick 			return;
2909bbfd46c4SJeff Bonwick 
2910b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2911bbfd46c4SJeff Bonwick 
2912b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2913b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2914b24ab676SJeff Bonwick 				continue;
2915bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2916bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2917b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2918cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2919b24ab676SJeff Bonwick 			} else {
2920b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2921b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2922b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2923b24ab676SJeff Bonwick 			}
2924b24ab676SJeff Bonwick 		}
2925b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2926bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2927b24ab676SJeff Bonwick 			ddt_enter(ddt);
2928b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2929b24ab676SJeff Bonwick 			ddt_exit(ddt);
2930b24ab676SJeff Bonwick 		}
2931b24ab676SJeff Bonwick 	}
2932b24ab676SJeff Bonwick 
2933b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2934b24ab676SJeff Bonwick }
2935b24ab676SJeff Bonwick 
29365cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
29375cabbc6bSPrashanth Sreenivasa static void
29385cabbc6bSPrashanth Sreenivasa claim_segment_impl_cb(uint64_t inner_offset, vdev_t *vd, uint64_t offset,
29395cabbc6bSPrashanth Sreenivasa     uint64_t size, void *arg)
29405cabbc6bSPrashanth Sreenivasa {
29415cabbc6bSPrashanth Sreenivasa 	/*
29425cabbc6bSPrashanth Sreenivasa 	 * This callback was called through a remap from
29435cabbc6bSPrashanth Sreenivasa 	 * a device being removed. Therefore, the vdev that
29445cabbc6bSPrashanth Sreenivasa 	 * this callback is applied to is a concrete
29455cabbc6bSPrashanth Sreenivasa 	 * vdev.
29465cabbc6bSPrashanth Sreenivasa 	 */
29475cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
29485cabbc6bSPrashanth Sreenivasa 
29495cabbc6bSPrashanth Sreenivasa 	VERIFY0(metaslab_claim_impl(vd, offset, size,
2950*86714001SSerapheim Dimitropoulos 	    spa_min_claim_txg(vd->vdev_spa)));
29515cabbc6bSPrashanth Sreenivasa }
29525cabbc6bSPrashanth Sreenivasa 
29535cabbc6bSPrashanth Sreenivasa static void
29545cabbc6bSPrashanth Sreenivasa claim_segment_cb(void *arg, uint64_t offset, uint64_t size)
29555cabbc6bSPrashanth Sreenivasa {
29565cabbc6bSPrashanth Sreenivasa 	vdev_t *vd = arg;
29575cabbc6bSPrashanth Sreenivasa 
29585cabbc6bSPrashanth Sreenivasa 	vdev_indirect_ops.vdev_op_remap(vd, offset, size,
29595cabbc6bSPrashanth Sreenivasa 	    claim_segment_impl_cb, NULL);
29605cabbc6bSPrashanth Sreenivasa }
29615cabbc6bSPrashanth Sreenivasa 
29625cabbc6bSPrashanth Sreenivasa /*
29635cabbc6bSPrashanth Sreenivasa  * After accounting for all allocated blocks that are directly referenced,
29645cabbc6bSPrashanth Sreenivasa  * we might have missed a reference to a block from a partially complete
29655cabbc6bSPrashanth Sreenivasa  * (and thus unused) indirect mapping object. We perform a secondary pass
29665cabbc6bSPrashanth Sreenivasa  * through the metaslabs we have already mapped and claim the destination
29675cabbc6bSPrashanth Sreenivasa  * blocks.
29685cabbc6bSPrashanth Sreenivasa  */
29695cabbc6bSPrashanth Sreenivasa static void
29705cabbc6bSPrashanth Sreenivasa zdb_claim_removing(spa_t *spa, zdb_cb_t *zcb)
29715cabbc6bSPrashanth Sreenivasa {
29725cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal == NULL)
29735cabbc6bSPrashanth Sreenivasa 		return;
29745cabbc6bSPrashanth Sreenivasa 
29755cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
29765cabbc6bSPrashanth Sreenivasa 
29775cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
29785cabbc6bSPrashanth Sreenivasa 	vdev_t *vd = svr->svr_vdev;
29795cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
29805cabbc6bSPrashanth Sreenivasa 
29815cabbc6bSPrashanth Sreenivasa 	for (uint64_t msi = 0; msi < vd->vdev_ms_count; msi++) {
29825cabbc6bSPrashanth Sreenivasa 		metaslab_t *msp = vd->vdev_ms[msi];
29835cabbc6bSPrashanth Sreenivasa 
29845cabbc6bSPrashanth Sreenivasa 		if (msp->ms_start >= vdev_indirect_mapping_max_offset(vim))
29855cabbc6bSPrashanth Sreenivasa 			break;
29865cabbc6bSPrashanth Sreenivasa 
29875cabbc6bSPrashanth Sreenivasa 		ASSERT0(range_tree_space(svr->svr_allocd_segs));
29885cabbc6bSPrashanth Sreenivasa 
29895cabbc6bSPrashanth Sreenivasa 		if (msp->ms_sm != NULL) {
29905cabbc6bSPrashanth Sreenivasa 			VERIFY0(space_map_load(msp->ms_sm,
29915cabbc6bSPrashanth Sreenivasa 			    svr->svr_allocd_segs, SM_ALLOC));
29925cabbc6bSPrashanth Sreenivasa 
29935cabbc6bSPrashanth Sreenivasa 			/*
29945cabbc6bSPrashanth Sreenivasa 			 * Clear everything past what has been synced,
29955cabbc6bSPrashanth Sreenivasa 			 * because we have not allocated mappings for it yet.
29965cabbc6bSPrashanth Sreenivasa 			 */
29975cabbc6bSPrashanth Sreenivasa 			range_tree_clear(svr->svr_allocd_segs,
29985cabbc6bSPrashanth Sreenivasa 			    vdev_indirect_mapping_max_offset(vim),
29995cabbc6bSPrashanth Sreenivasa 			    msp->ms_sm->sm_start + msp->ms_sm->sm_size -
30005cabbc6bSPrashanth Sreenivasa 			    vdev_indirect_mapping_max_offset(vim));
30015cabbc6bSPrashanth Sreenivasa 		}
30025cabbc6bSPrashanth Sreenivasa 
30035cabbc6bSPrashanth Sreenivasa 		zcb->zcb_removing_size +=
30045cabbc6bSPrashanth Sreenivasa 		    range_tree_space(svr->svr_allocd_segs);
30055cabbc6bSPrashanth Sreenivasa 		range_tree_vacate(svr->svr_allocd_segs, claim_segment_cb, vd);
30065cabbc6bSPrashanth Sreenivasa 	}
30075cabbc6bSPrashanth Sreenivasa 
30085cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_CONFIG, FTAG);
30095cabbc6bSPrashanth Sreenivasa }
30105cabbc6bSPrashanth Sreenivasa 
30115cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
30125cabbc6bSPrashanth Sreenivasa static int
30135cabbc6bSPrashanth Sreenivasa increment_indirect_mapping_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
30145cabbc6bSPrashanth Sreenivasa {
30155cabbc6bSPrashanth Sreenivasa 	zdb_cb_t *zcb = arg;
30165cabbc6bSPrashanth Sreenivasa 	spa_t *spa = zcb->zcb_spa;
30175cabbc6bSPrashanth Sreenivasa 	vdev_t *vd;
30185cabbc6bSPrashanth Sreenivasa 	const dva_t *dva = &bp->blk_dva[0];
30195cabbc6bSPrashanth Sreenivasa 
30205cabbc6bSPrashanth Sreenivasa 	ASSERT(!dump_opt['L']);
30215cabbc6bSPrashanth Sreenivasa 	ASSERT3U(BP_GET_NDVAS(bp), ==, 1);
30225cabbc6bSPrashanth Sreenivasa 
30235cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
30245cabbc6bSPrashanth Sreenivasa 	vd = vdev_lookup_top(zcb->zcb_spa, DVA_GET_VDEV(dva));
30255cabbc6bSPrashanth Sreenivasa 	ASSERT3P(vd, !=, NULL);
30265cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_VDEV, FTAG);
30275cabbc6bSPrashanth Sreenivasa 
30285cabbc6bSPrashanth Sreenivasa 	ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0);
30295cabbc6bSPrashanth Sreenivasa 	ASSERT3P(zcb->zcb_vd_obsolete_counts[vd->vdev_id], !=, NULL);
30305cabbc6bSPrashanth Sreenivasa 
30315cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_increment_obsolete_count(
30325cabbc6bSPrashanth Sreenivasa 	    vd->vdev_indirect_mapping,
30335cabbc6bSPrashanth Sreenivasa 	    DVA_GET_OFFSET(dva), DVA_GET_ASIZE(dva),
30345cabbc6bSPrashanth Sreenivasa 	    zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
30355cabbc6bSPrashanth Sreenivasa 
30365cabbc6bSPrashanth Sreenivasa 	return (0);
30375cabbc6bSPrashanth Sreenivasa }
30385cabbc6bSPrashanth Sreenivasa 
30395cabbc6bSPrashanth Sreenivasa static uint32_t *
30405cabbc6bSPrashanth Sreenivasa zdb_load_obsolete_counts(vdev_t *vd)
30415cabbc6bSPrashanth Sreenivasa {
30425cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
30435cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
30445cabbc6bSPrashanth Sreenivasa 	spa_condensing_indirect_phys_t *scip =
30455cabbc6bSPrashanth Sreenivasa 	    &spa->spa_condensing_indirect_phys;
30465cabbc6bSPrashanth Sreenivasa 	uint32_t *counts;
30475cabbc6bSPrashanth Sreenivasa 
30485cabbc6bSPrashanth Sreenivasa 	EQUIV(vdev_obsolete_sm_object(vd) != 0, vd->vdev_obsolete_sm != NULL);
30495cabbc6bSPrashanth Sreenivasa 	counts = vdev_indirect_mapping_load_obsolete_counts(vim);
30505cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_obsolete_sm != NULL) {
30515cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
30525cabbc6bSPrashanth Sreenivasa 		    vd->vdev_obsolete_sm);
30535cabbc6bSPrashanth Sreenivasa 	}
30545cabbc6bSPrashanth Sreenivasa 	if (scip->scip_vdev == vd->vdev_id &&
30555cabbc6bSPrashanth Sreenivasa 	    scip->scip_prev_obsolete_sm_object != 0) {
30565cabbc6bSPrashanth Sreenivasa 		space_map_t *prev_obsolete_sm = NULL;
30575cabbc6bSPrashanth Sreenivasa 		VERIFY0(space_map_open(&prev_obsolete_sm, spa->spa_meta_objset,
30585cabbc6bSPrashanth Sreenivasa 		    scip->scip_prev_obsolete_sm_object, 0, vd->vdev_asize, 0));
30595cabbc6bSPrashanth Sreenivasa 		space_map_update(prev_obsolete_sm);
30605cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
30615cabbc6bSPrashanth Sreenivasa 		    prev_obsolete_sm);
30625cabbc6bSPrashanth Sreenivasa 		space_map_close(prev_obsolete_sm);
30635cabbc6bSPrashanth Sreenivasa 	}
30645cabbc6bSPrashanth Sreenivasa 	return (counts);
30655cabbc6bSPrashanth Sreenivasa }
30665cabbc6bSPrashanth Sreenivasa 
3067*86714001SSerapheim Dimitropoulos typedef struct checkpoint_sm_exclude_entry_arg {
3068*86714001SSerapheim Dimitropoulos 	vdev_t *cseea_vd;
3069*86714001SSerapheim Dimitropoulos 	uint64_t cseea_checkpoint_size;
3070*86714001SSerapheim Dimitropoulos } checkpoint_sm_exclude_entry_arg_t;
3071*86714001SSerapheim Dimitropoulos 
3072*86714001SSerapheim Dimitropoulos static int
3073*86714001SSerapheim Dimitropoulos checkpoint_sm_exclude_entry_cb(maptype_t type, uint64_t offset, uint64_t size,
3074*86714001SSerapheim Dimitropoulos     void *arg)
3075*86714001SSerapheim Dimitropoulos {
3076*86714001SSerapheim Dimitropoulos 	checkpoint_sm_exclude_entry_arg_t *cseea = arg;
3077*86714001SSerapheim Dimitropoulos 	vdev_t *vd = cseea->cseea_vd;
3078*86714001SSerapheim Dimitropoulos 	metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
3079*86714001SSerapheim Dimitropoulos 	uint64_t end = offset + size;
3080*86714001SSerapheim Dimitropoulos 
3081*86714001SSerapheim Dimitropoulos 	ASSERT(type == SM_FREE);
3082*86714001SSerapheim Dimitropoulos 
3083*86714001SSerapheim Dimitropoulos 	/*
3084*86714001SSerapheim Dimitropoulos 	 * Since the vdev_checkpoint_sm exists in the vdev level
3085*86714001SSerapheim Dimitropoulos 	 * and the ms_sm space maps exist in the metaslab level,
3086*86714001SSerapheim Dimitropoulos 	 * an entry in the checkpoint space map could theoretically
3087*86714001SSerapheim Dimitropoulos 	 * cross the boundaries of the metaslab that it belongs.
3088*86714001SSerapheim Dimitropoulos 	 *
3089*86714001SSerapheim Dimitropoulos 	 * In reality, because of the way that we populate and
3090*86714001SSerapheim Dimitropoulos 	 * manipulate the checkpoint's space maps currently,
3091*86714001SSerapheim Dimitropoulos 	 * there shouldn't be any entries that cross metaslabs.
3092*86714001SSerapheim Dimitropoulos 	 * Hence the assertion below.
3093*86714001SSerapheim Dimitropoulos 	 *
3094*86714001SSerapheim Dimitropoulos 	 * That said, there is no fundamental requirement that
3095*86714001SSerapheim Dimitropoulos 	 * the checkpoint's space map entries should not cross
3096*86714001SSerapheim Dimitropoulos 	 * metaslab boundaries. So if needed we could add code
3097*86714001SSerapheim Dimitropoulos 	 * that handles metaslab-crossing segments in the future.
3098*86714001SSerapheim Dimitropoulos 	 */
3099*86714001SSerapheim Dimitropoulos 	VERIFY3U(offset, >=, ms->ms_start);
3100*86714001SSerapheim Dimitropoulos 	VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
3101*86714001SSerapheim Dimitropoulos 
3102*86714001SSerapheim Dimitropoulos 	/*
3103*86714001SSerapheim Dimitropoulos 	 * By removing the entry from the allocated segments we
3104*86714001SSerapheim Dimitropoulos 	 * also verify that the entry is there to begin with.
3105*86714001SSerapheim Dimitropoulos 	 */
3106*86714001SSerapheim Dimitropoulos 	mutex_enter(&ms->ms_lock);
3107*86714001SSerapheim Dimitropoulos 	range_tree_remove(ms->ms_allocatable, offset, size);
3108*86714001SSerapheim Dimitropoulos 	mutex_exit(&ms->ms_lock);
3109*86714001SSerapheim Dimitropoulos 
3110*86714001SSerapheim Dimitropoulos 	cseea->cseea_checkpoint_size += size;
3111*86714001SSerapheim Dimitropoulos 	return (0);
3112*86714001SSerapheim Dimitropoulos }
3113*86714001SSerapheim Dimitropoulos 
3114*86714001SSerapheim Dimitropoulos static void
3115*86714001SSerapheim Dimitropoulos zdb_leak_init_vdev_exclude_checkpoint(vdev_t *vd, zdb_cb_t *zcb)
3116*86714001SSerapheim Dimitropoulos {
3117*86714001SSerapheim Dimitropoulos 	spa_t *spa = vd->vdev_spa;
3118*86714001SSerapheim Dimitropoulos 	space_map_t *checkpoint_sm = NULL;
3119*86714001SSerapheim Dimitropoulos 	uint64_t checkpoint_sm_obj;
3120*86714001SSerapheim Dimitropoulos 
3121*86714001SSerapheim Dimitropoulos 	/*
3122*86714001SSerapheim Dimitropoulos 	 * If there is no vdev_top_zap, we are in a pool whose
3123*86714001SSerapheim Dimitropoulos 	 * version predates the pool checkpoint feature.
3124*86714001SSerapheim Dimitropoulos 	 */
3125*86714001SSerapheim Dimitropoulos 	if (vd->vdev_top_zap == 0)
3126*86714001SSerapheim Dimitropoulos 		return;
3127*86714001SSerapheim Dimitropoulos 
3128*86714001SSerapheim Dimitropoulos 	/*
3129*86714001SSerapheim Dimitropoulos 	 * If there is no reference of the vdev_checkpoint_sm in
3130*86714001SSerapheim Dimitropoulos 	 * the vdev_top_zap, then one of the following scenarios
3131*86714001SSerapheim Dimitropoulos 	 * is true:
3132*86714001SSerapheim Dimitropoulos 	 *
3133*86714001SSerapheim Dimitropoulos 	 * 1] There is no checkpoint
3134*86714001SSerapheim Dimitropoulos 	 * 2] There is a checkpoint, but no checkpointed blocks
3135*86714001SSerapheim Dimitropoulos 	 *    have been freed yet
3136*86714001SSerapheim Dimitropoulos 	 * 3] The current vdev is indirect
3137*86714001SSerapheim Dimitropoulos 	 *
3138*86714001SSerapheim Dimitropoulos 	 * In these cases we return immediately.
3139*86714001SSerapheim Dimitropoulos 	 */
3140*86714001SSerapheim Dimitropoulos 	if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
3141*86714001SSerapheim Dimitropoulos 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
3142*86714001SSerapheim Dimitropoulos 		return;
3143*86714001SSerapheim Dimitropoulos 
3144*86714001SSerapheim Dimitropoulos 	VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
3145*86714001SSerapheim Dimitropoulos 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1,
3146*86714001SSerapheim Dimitropoulos 	    &checkpoint_sm_obj));
3147*86714001SSerapheim Dimitropoulos 
3148*86714001SSerapheim Dimitropoulos 	checkpoint_sm_exclude_entry_arg_t cseea;
3149*86714001SSerapheim Dimitropoulos 	cseea.cseea_vd = vd;
3150*86714001SSerapheim Dimitropoulos 	cseea.cseea_checkpoint_size = 0;
3151*86714001SSerapheim Dimitropoulos 
3152*86714001SSerapheim Dimitropoulos 	VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
3153*86714001SSerapheim Dimitropoulos 	    checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
3154*86714001SSerapheim Dimitropoulos 	space_map_update(checkpoint_sm);
3155*86714001SSerapheim Dimitropoulos 
3156*86714001SSerapheim Dimitropoulos 	VERIFY0(space_map_iterate(checkpoint_sm,
3157*86714001SSerapheim Dimitropoulos 	    checkpoint_sm_exclude_entry_cb, &cseea));
3158*86714001SSerapheim Dimitropoulos 	space_map_close(checkpoint_sm);
3159*86714001SSerapheim Dimitropoulos 
3160*86714001SSerapheim Dimitropoulos 	zcb->zcb_checkpoint_size += cseea.cseea_checkpoint_size;
3161*86714001SSerapheim Dimitropoulos }
3162*86714001SSerapheim Dimitropoulos 
3163*86714001SSerapheim Dimitropoulos static void
3164*86714001SSerapheim Dimitropoulos zdb_leak_init_exclude_checkpoint(spa_t *spa, zdb_cb_t *zcb)
3165*86714001SSerapheim Dimitropoulos {
3166*86714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
3167*86714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
3168*86714001SSerapheim Dimitropoulos 		ASSERT3U(c, ==, rvd->vdev_child[c]->vdev_id);
3169*86714001SSerapheim Dimitropoulos 		zdb_leak_init_vdev_exclude_checkpoint(rvd->vdev_child[c], zcb);
3170*86714001SSerapheim Dimitropoulos 	}
3171*86714001SSerapheim Dimitropoulos }
3172*86714001SSerapheim Dimitropoulos 
3173*86714001SSerapheim Dimitropoulos static void
3174*86714001SSerapheim Dimitropoulos load_concrete_ms_allocatable_trees(spa_t *spa, maptype_t maptype)
3175*86714001SSerapheim Dimitropoulos {
3176*86714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
3177*86714001SSerapheim Dimitropoulos 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
3178*86714001SSerapheim Dimitropoulos 		vdev_t *vd = rvd->vdev_child[i];
3179*86714001SSerapheim Dimitropoulos 
3180*86714001SSerapheim Dimitropoulos 		ASSERT3U(i, ==, vd->vdev_id);
3181*86714001SSerapheim Dimitropoulos 
3182*86714001SSerapheim Dimitropoulos 		if (vd->vdev_ops == &vdev_indirect_ops)
3183*86714001SSerapheim Dimitropoulos 			continue;
3184*86714001SSerapheim Dimitropoulos 
3185*86714001SSerapheim Dimitropoulos 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
3186*86714001SSerapheim Dimitropoulos 			metaslab_t *msp = vd->vdev_ms[m];
3187*86714001SSerapheim Dimitropoulos 
3188*86714001SSerapheim Dimitropoulos 			(void) fprintf(stderr,
3189*86714001SSerapheim Dimitropoulos 			    "\rloading concrete vdev %llu, "
3190*86714001SSerapheim Dimitropoulos 			    "metaslab %llu of %llu ...",
3191*86714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_id,
3192*86714001SSerapheim Dimitropoulos 			    (longlong_t)msp->ms_id,
3193*86714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_ms_count);
3194*86714001SSerapheim Dimitropoulos 
3195*86714001SSerapheim Dimitropoulos 			mutex_enter(&msp->ms_lock);
3196*86714001SSerapheim Dimitropoulos 			metaslab_unload(msp);
3197*86714001SSerapheim Dimitropoulos 
3198*86714001SSerapheim Dimitropoulos 			/*
3199*86714001SSerapheim Dimitropoulos 			 * We don't want to spend the CPU manipulating the
3200*86714001SSerapheim Dimitropoulos 			 * size-ordered tree, so clear the range_tree ops.
3201*86714001SSerapheim Dimitropoulos 			 */
3202*86714001SSerapheim Dimitropoulos 			msp->ms_allocatable->rt_ops = NULL;
3203*86714001SSerapheim Dimitropoulos 
3204*86714001SSerapheim Dimitropoulos 			if (msp->ms_sm != NULL) {
3205*86714001SSerapheim Dimitropoulos 				VERIFY0(space_map_load(msp->ms_sm,
3206*86714001SSerapheim Dimitropoulos 				    msp->ms_allocatable, maptype));
3207*86714001SSerapheim Dimitropoulos 			}
3208*86714001SSerapheim Dimitropoulos 			if (!msp->ms_loaded)
3209*86714001SSerapheim Dimitropoulos 				msp->ms_loaded = B_TRUE;
3210*86714001SSerapheim Dimitropoulos 			mutex_exit(&msp->ms_lock);
3211*86714001SSerapheim Dimitropoulos 		}
3212*86714001SSerapheim Dimitropoulos 	}
3213*86714001SSerapheim Dimitropoulos }
3214*86714001SSerapheim Dimitropoulos 
3215*86714001SSerapheim Dimitropoulos /*
3216*86714001SSerapheim Dimitropoulos  * vm_idxp is an in-out parameter which (for indirect vdevs) is the
3217*86714001SSerapheim Dimitropoulos  * index in vim_entries that has the first entry in this metaslab.
3218*86714001SSerapheim Dimitropoulos  * On return, it will be set to the first entry after this metaslab.
3219*86714001SSerapheim Dimitropoulos  */
3220*86714001SSerapheim Dimitropoulos static void
3221*86714001SSerapheim Dimitropoulos load_indirect_ms_allocatable_tree(vdev_t *vd, metaslab_t *msp,
3222*86714001SSerapheim Dimitropoulos     uint64_t *vim_idxp)
3223*86714001SSerapheim Dimitropoulos {
3224*86714001SSerapheim Dimitropoulos 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3225*86714001SSerapheim Dimitropoulos 
3226*86714001SSerapheim Dimitropoulos 	mutex_enter(&msp->ms_lock);
3227*86714001SSerapheim Dimitropoulos 	metaslab_unload(msp);
3228*86714001SSerapheim Dimitropoulos 
3229*86714001SSerapheim Dimitropoulos 	/*
3230*86714001SSerapheim Dimitropoulos 	 * We don't want to spend the CPU manipulating the
3231*86714001SSerapheim Dimitropoulos 	 * size-ordered tree, so clear the range_tree ops.
3232*86714001SSerapheim Dimitropoulos 	 */
3233*86714001SSerapheim Dimitropoulos 	msp->ms_allocatable->rt_ops = NULL;
3234*86714001SSerapheim Dimitropoulos 
3235*86714001SSerapheim Dimitropoulos 	for (; *vim_idxp < vdev_indirect_mapping_num_entries(vim);
3236*86714001SSerapheim Dimitropoulos 	    (*vim_idxp)++) {
3237*86714001SSerapheim Dimitropoulos 		vdev_indirect_mapping_entry_phys_t *vimep =
3238*86714001SSerapheim Dimitropoulos 		    &vim->vim_entries[*vim_idxp];
3239*86714001SSerapheim Dimitropoulos 		uint64_t ent_offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
3240*86714001SSerapheim Dimitropoulos 		uint64_t ent_len = DVA_GET_ASIZE(&vimep->vimep_dst);
3241*86714001SSerapheim Dimitropoulos 		ASSERT3U(ent_offset, >=, msp->ms_start);
3242*86714001SSerapheim Dimitropoulos 		if (ent_offset >= msp->ms_start + msp->ms_size)
3243*86714001SSerapheim Dimitropoulos 			break;
3244*86714001SSerapheim Dimitropoulos 
3245*86714001SSerapheim Dimitropoulos 		/*
3246*86714001SSerapheim Dimitropoulos 		 * Mappings do not cross metaslab boundaries,
3247*86714001SSerapheim Dimitropoulos 		 * because we create them by walking the metaslabs.
3248*86714001SSerapheim Dimitropoulos 		 */
3249*86714001SSerapheim Dimitropoulos 		ASSERT3U(ent_offset + ent_len, <=,
3250*86714001SSerapheim Dimitropoulos 		    msp->ms_start + msp->ms_size);
3251*86714001SSerapheim Dimitropoulos 		range_tree_add(msp->ms_allocatable, ent_offset, ent_len);
3252*86714001SSerapheim Dimitropoulos 	}
3253*86714001SSerapheim Dimitropoulos 
3254*86714001SSerapheim Dimitropoulos 	if (!msp->ms_loaded)
3255*86714001SSerapheim Dimitropoulos 		msp->ms_loaded = B_TRUE;
3256*86714001SSerapheim Dimitropoulos 	mutex_exit(&msp->ms_lock);
3257*86714001SSerapheim Dimitropoulos }
3258*86714001SSerapheim Dimitropoulos 
3259*86714001SSerapheim Dimitropoulos static void
3260*86714001SSerapheim Dimitropoulos zdb_leak_init_prepare_indirect_vdevs(spa_t *spa, zdb_cb_t *zcb)
3261*86714001SSerapheim Dimitropoulos {
3262*86714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
3263*86714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
3264*86714001SSerapheim Dimitropoulos 		vdev_t *vd = rvd->vdev_child[c];
3265*86714001SSerapheim Dimitropoulos 
3266*86714001SSerapheim Dimitropoulos 		ASSERT3U(c, ==, vd->vdev_id);
3267*86714001SSerapheim Dimitropoulos 
3268*86714001SSerapheim Dimitropoulos 		if (vd->vdev_ops != &vdev_indirect_ops)
3269*86714001SSerapheim Dimitropoulos 			continue;
3270*86714001SSerapheim Dimitropoulos 
3271*86714001SSerapheim Dimitropoulos 		/*
3272*86714001SSerapheim Dimitropoulos 		 * Note: we don't check for mapping leaks on
3273*86714001SSerapheim Dimitropoulos 		 * removing vdevs because their ms_allocatable's
3274*86714001SSerapheim Dimitropoulos 		 * are used to look for leaks in allocated space.
3275*86714001SSerapheim Dimitropoulos 		 */
3276*86714001SSerapheim Dimitropoulos 		zcb->zcb_vd_obsolete_counts[c] = zdb_load_obsolete_counts(vd);
3277*86714001SSerapheim Dimitropoulos 
3278*86714001SSerapheim Dimitropoulos 		/*
3279*86714001SSerapheim Dimitropoulos 		 * Normally, indirect vdevs don't have any
3280*86714001SSerapheim Dimitropoulos 		 * metaslabs.  We want to set them up for
3281*86714001SSerapheim Dimitropoulos 		 * zio_claim().
3282*86714001SSerapheim Dimitropoulos 		 */
3283*86714001SSerapheim Dimitropoulos 		VERIFY0(vdev_metaslab_init(vd, 0));
3284*86714001SSerapheim Dimitropoulos 
3285*86714001SSerapheim Dimitropoulos 		vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3286*86714001SSerapheim Dimitropoulos 		uint64_t vim_idx = 0;
3287*86714001SSerapheim Dimitropoulos 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
3288*86714001SSerapheim Dimitropoulos 
3289*86714001SSerapheim Dimitropoulos 			(void) fprintf(stderr,
3290*86714001SSerapheim Dimitropoulos 			    "\rloading indirect vdev %llu, "
3291*86714001SSerapheim Dimitropoulos 			    "metaslab %llu of %llu ...",
3292*86714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_id,
3293*86714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_ms[m]->ms_id,
3294*86714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_ms_count);
3295*86714001SSerapheim Dimitropoulos 
3296*86714001SSerapheim Dimitropoulos 			load_indirect_ms_allocatable_tree(vd, vd->vdev_ms[m],
3297*86714001SSerapheim Dimitropoulos 			    &vim_idx);
3298*86714001SSerapheim Dimitropoulos 		}
3299*86714001SSerapheim Dimitropoulos 		ASSERT3U(vim_idx, ==, vdev_indirect_mapping_num_entries(vim));
3300*86714001SSerapheim Dimitropoulos 	}
3301*86714001SSerapheim Dimitropoulos }
3302*86714001SSerapheim Dimitropoulos 
3303b24ab676SJeff Bonwick static void
3304b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
3305b24ab676SJeff Bonwick {
3306cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
3307cde58dbcSMatthew Ahrens 
3308b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
33095cabbc6bSPrashanth Sreenivasa 		dsl_pool_t *dp = spa->spa_dsl_pool;
3310b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
33118363e80aSGeorge Wilson 
33128363e80aSGeorge Wilson 		/*
33138363e80aSGeorge Wilson 		 * We are going to be changing the meaning of the metaslab's
3314*86714001SSerapheim Dimitropoulos 		 * ms_allocatable.  Ensure that the allocator doesn't try to
33158363e80aSGeorge Wilson 		 * use the tree.
33168363e80aSGeorge Wilson 		 */
33178363e80aSGeorge Wilson 		spa->spa_normal_class->mc_ops = &zdb_metaslab_ops;
33188363e80aSGeorge Wilson 		spa->spa_log_class->mc_ops = &zdb_metaslab_ops;
33198363e80aSGeorge Wilson 
33205cabbc6bSPrashanth Sreenivasa 		zcb->zcb_vd_obsolete_counts =
33215cabbc6bSPrashanth Sreenivasa 		    umem_zalloc(rvd->vdev_children * sizeof (uint32_t *),
33225cabbc6bSPrashanth Sreenivasa 		    UMEM_NOFAIL);
33235cabbc6bSPrashanth Sreenivasa 
3324*86714001SSerapheim Dimitropoulos 		/*
3325*86714001SSerapheim Dimitropoulos 		 * For leak detection, we overload the ms_allocatable trees
3326*86714001SSerapheim Dimitropoulos 		 * to contain allocated segments instead of free segments.
3327*86714001SSerapheim Dimitropoulos 		 * As a result, we can't use the normal metaslab_load/unload
3328*86714001SSerapheim Dimitropoulos 		 * interfaces.
3329*86714001SSerapheim Dimitropoulos 		 */
3330*86714001SSerapheim Dimitropoulos 		zdb_leak_init_prepare_indirect_vdevs(spa, zcb);
3331*86714001SSerapheim Dimitropoulos 		load_concrete_ms_allocatable_trees(spa, SM_ALLOC);
33325cabbc6bSPrashanth Sreenivasa 
3333*86714001SSerapheim Dimitropoulos 		/*
3334*86714001SSerapheim Dimitropoulos 		 * On load_concrete_ms_allocatable_trees() we loaded all the
3335*86714001SSerapheim Dimitropoulos 		 * allocated entries from the ms_sm to the ms_allocatable for
3336*86714001SSerapheim Dimitropoulos 		 * each metaslab. If the pool has a checkpoint or is in the
3337*86714001SSerapheim Dimitropoulos 		 * middle of discarding a checkpoint, some of these blocks
3338*86714001SSerapheim Dimitropoulos 		 * may have been freed but their ms_sm may not have been
3339*86714001SSerapheim Dimitropoulos 		 * updated because they are referenced by the checkpoint. In
3340*86714001SSerapheim Dimitropoulos 		 * order to avoid false-positives during leak-detection, we
3341*86714001SSerapheim Dimitropoulos 		 * go through the vdev's checkpoint space map and exclude all
3342*86714001SSerapheim Dimitropoulos 		 * its entries from their relevant ms_allocatable.
3343*86714001SSerapheim Dimitropoulos 		 *
3344*86714001SSerapheim Dimitropoulos 		 * We also aggregate the space held by the checkpoint and add
3345*86714001SSerapheim Dimitropoulos 		 * it to zcb_checkpoint_size.
3346*86714001SSerapheim Dimitropoulos 		 *
3347*86714001SSerapheim Dimitropoulos 		 * Note that at this point we are also verifying that all the
3348*86714001SSerapheim Dimitropoulos 		 * entries on the checkpoint_sm are marked as allocated in
3349*86714001SSerapheim Dimitropoulos 		 * the ms_sm of their relevant metaslab.
3350*86714001SSerapheim Dimitropoulos 		 * [see comment in checkpoint_sm_exclude_entry_cb()]
3351*86714001SSerapheim Dimitropoulos 		 */
3352*86714001SSerapheim Dimitropoulos 		zdb_leak_init_exclude_checkpoint(spa, zcb);
33535cabbc6bSPrashanth Sreenivasa 
3354*86714001SSerapheim Dimitropoulos 		/* for cleaner progress output */
335506be9802SMatthew Ahrens 		(void) fprintf(stderr, "\n");
33565cabbc6bSPrashanth Sreenivasa 
33575cabbc6bSPrashanth Sreenivasa 		if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
33585cabbc6bSPrashanth Sreenivasa 			ASSERT(spa_feature_is_enabled(spa,
33595cabbc6bSPrashanth Sreenivasa 			    SPA_FEATURE_DEVICE_REMOVAL));
33605cabbc6bSPrashanth Sreenivasa 			(void) bpobj_iterate_nofree(&dp->dp_obsolete_bpobj,
33615cabbc6bSPrashanth Sreenivasa 			    increment_indirect_mapping_cb, zcb, NULL);
33625cabbc6bSPrashanth Sreenivasa 		}
3363*86714001SSerapheim Dimitropoulos 	} else {
3364*86714001SSerapheim Dimitropoulos 		/*
3365*86714001SSerapheim Dimitropoulos 		 * If leak tracing is disabled, we still need to consider
3366*86714001SSerapheim Dimitropoulos 		 * any checkpointed space in our space verification.
3367*86714001SSerapheim Dimitropoulos 		 */
3368*86714001SSerapheim Dimitropoulos 		zcb->zcb_checkpoint_size += spa_get_checkpoint_space(spa);
3369b24ab676SJeff Bonwick 	}
3370b24ab676SJeff Bonwick 
3371b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3372bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
3373b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
3374b24ab676SJeff Bonwick }
3375b24ab676SJeff Bonwick 
33765cabbc6bSPrashanth Sreenivasa static boolean_t
33775cabbc6bSPrashanth Sreenivasa zdb_check_for_obsolete_leaks(vdev_t *vd, zdb_cb_t *zcb)
33785cabbc6bSPrashanth Sreenivasa {
33795cabbc6bSPrashanth Sreenivasa 	boolean_t leaks = B_FALSE;
33805cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
33815cabbc6bSPrashanth Sreenivasa 	uint64_t total_leaked = 0;
33825cabbc6bSPrashanth Sreenivasa 
33835cabbc6bSPrashanth Sreenivasa 	ASSERT(vim != NULL);
33845cabbc6bSPrashanth Sreenivasa 
33855cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
33865cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_entry_phys_t *vimep =
33875cabbc6bSPrashanth Sreenivasa 		    &vim->vim_entries[i];
33885cabbc6bSPrashanth Sreenivasa 		uint64_t obsolete_bytes = 0;
33895cabbc6bSPrashanth Sreenivasa 		uint64_t offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
33905cabbc6bSPrashanth Sreenivasa 		metaslab_t *msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
33915cabbc6bSPrashanth Sreenivasa 
33925cabbc6bSPrashanth Sreenivasa 		/*
33935cabbc6bSPrashanth Sreenivasa 		 * This is not very efficient but it's easy to
33945cabbc6bSPrashanth Sreenivasa 		 * verify correctness.
33955cabbc6bSPrashanth Sreenivasa 		 */
33965cabbc6bSPrashanth Sreenivasa 		for (uint64_t inner_offset = 0;
33975cabbc6bSPrashanth Sreenivasa 		    inner_offset < DVA_GET_ASIZE(&vimep->vimep_dst);
33985cabbc6bSPrashanth Sreenivasa 		    inner_offset += 1 << vd->vdev_ashift) {
3399*86714001SSerapheim Dimitropoulos 			if (range_tree_contains(msp->ms_allocatable,
34005cabbc6bSPrashanth Sreenivasa 			    offset + inner_offset, 1 << vd->vdev_ashift)) {
34015cabbc6bSPrashanth Sreenivasa 				obsolete_bytes += 1 << vd->vdev_ashift;
34025cabbc6bSPrashanth Sreenivasa 			}
34035cabbc6bSPrashanth Sreenivasa 		}
34045cabbc6bSPrashanth Sreenivasa 
34055cabbc6bSPrashanth Sreenivasa 		int64_t bytes_leaked = obsolete_bytes -
34065cabbc6bSPrashanth Sreenivasa 		    zcb->zcb_vd_obsolete_counts[vd->vdev_id][i];
34075cabbc6bSPrashanth Sreenivasa 		ASSERT3U(DVA_GET_ASIZE(&vimep->vimep_dst), >=,
34085cabbc6bSPrashanth Sreenivasa 		    zcb->zcb_vd_obsolete_counts[vd->vdev_id][i]);
34095cabbc6bSPrashanth Sreenivasa 		if (bytes_leaked != 0 &&
34105cabbc6bSPrashanth Sreenivasa 		    (vdev_obsolete_counts_are_precise(vd) ||
34115cabbc6bSPrashanth Sreenivasa 		    dump_opt['d'] >= 5)) {
34125cabbc6bSPrashanth Sreenivasa 			(void) printf("obsolete indirect mapping count "
34135cabbc6bSPrashanth Sreenivasa 			    "mismatch on %llu:%llx:%llx : %llx bytes leaked\n",
34145cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)vd->vdev_id,
34155cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
34165cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
34175cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)bytes_leaked);
34185cabbc6bSPrashanth Sreenivasa 		}
34195cabbc6bSPrashanth Sreenivasa 		total_leaked += ABS(bytes_leaked);
34205cabbc6bSPrashanth Sreenivasa 	}
34215cabbc6bSPrashanth Sreenivasa 
34225cabbc6bSPrashanth Sreenivasa 	if (!vdev_obsolete_counts_are_precise(vd) && total_leaked > 0) {
34235cabbc6bSPrashanth Sreenivasa 		int pct_leaked = total_leaked * 100 /
34245cabbc6bSPrashanth Sreenivasa 		    vdev_indirect_mapping_bytes_mapped(vim);
34255cabbc6bSPrashanth Sreenivasa 		(void) printf("cannot verify obsolete indirect mapping "
34265cabbc6bSPrashanth Sreenivasa 		    "counts of vdev %llu because precise feature was not "
34275cabbc6bSPrashanth Sreenivasa 		    "enabled when it was removed: %d%% (%llx bytes) of mapping"
34285cabbc6bSPrashanth Sreenivasa 		    "unreferenced\n",
34295cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)vd->vdev_id, pct_leaked,
34305cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)total_leaked);
34315cabbc6bSPrashanth Sreenivasa 	} else if (total_leaked > 0) {
34325cabbc6bSPrashanth Sreenivasa 		(void) printf("obsolete indirect mapping count mismatch "
34335cabbc6bSPrashanth Sreenivasa 		    "for vdev %llu -- %llx total bytes mismatched\n",
34345cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)vd->vdev_id,
34355cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)total_leaked);
34365cabbc6bSPrashanth Sreenivasa 		leaks |= B_TRUE;
34375cabbc6bSPrashanth Sreenivasa 	}
34385cabbc6bSPrashanth Sreenivasa 
34395cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_free_obsolete_counts(vim,
34405cabbc6bSPrashanth Sreenivasa 	    zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
34415cabbc6bSPrashanth Sreenivasa 	zcb->zcb_vd_obsolete_counts[vd->vdev_id] = NULL;
34425cabbc6bSPrashanth Sreenivasa 
34435cabbc6bSPrashanth Sreenivasa 	return (leaks);
34445cabbc6bSPrashanth Sreenivasa }
34455cabbc6bSPrashanth Sreenivasa 
34465cabbc6bSPrashanth Sreenivasa static boolean_t
34475cabbc6bSPrashanth Sreenivasa zdb_leak_fini(spa_t *spa, zdb_cb_t *zcb)
3448b24ab676SJeff Bonwick {
34495cabbc6bSPrashanth Sreenivasa 	boolean_t leaks = B_FALSE;
3450b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
3451b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
34523f7978d0SAlan Somers 		for (unsigned c = 0; c < rvd->vdev_children; c++) {
3453b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
34548363e80aSGeorge Wilson 			metaslab_group_t *mg = vd->vdev_mg;
34555cabbc6bSPrashanth Sreenivasa 
34565cabbc6bSPrashanth Sreenivasa 			if (zcb->zcb_vd_obsolete_counts[c] != NULL) {
34575cabbc6bSPrashanth Sreenivasa 				leaks |= zdb_check_for_obsolete_leaks(vd, zcb);
34585cabbc6bSPrashanth Sreenivasa 			}
34595cabbc6bSPrashanth Sreenivasa 
34605cabbc6bSPrashanth Sreenivasa 			for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
3461b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
34628363e80aSGeorge Wilson 				ASSERT3P(mg, ==, msp->ms_group);
34630713e232SGeorge Wilson 
34640713e232SGeorge Wilson 				/*
3465*86714001SSerapheim Dimitropoulos 				 * ms_allocatable has been overloaded
3466*86714001SSerapheim Dimitropoulos 				 * to contain allocated segments. Now that
3467*86714001SSerapheim Dimitropoulos 				 * we finished traversing all blocks, any
3468*86714001SSerapheim Dimitropoulos 				 * block that remains in the ms_allocatable
34690713e232SGeorge Wilson 				 * represents an allocated block that we
34700713e232SGeorge Wilson 				 * did not claim during the traversal.
34710713e232SGeorge Wilson 				 * Claimed blocks would have been removed
3472*86714001SSerapheim Dimitropoulos 				 * from the ms_allocatable.  For indirect
3473*86714001SSerapheim Dimitropoulos 				 * vdevs, space remaining in the tree
3474*86714001SSerapheim Dimitropoulos 				 * represents parts of the mapping that are
3475*86714001SSerapheim Dimitropoulos 				 * not referenced, which is not a bug.
34760713e232SGeorge Wilson 				 */
34775cabbc6bSPrashanth Sreenivasa 				if (vd->vdev_ops == &vdev_indirect_ops) {
3478*86714001SSerapheim Dimitropoulos 					range_tree_vacate(msp->ms_allocatable,
34795cabbc6bSPrashanth Sreenivasa 					    NULL, NULL);
34805cabbc6bSPrashanth Sreenivasa 				} else {
3481*86714001SSerapheim Dimitropoulos 					range_tree_vacate(msp->ms_allocatable,
34825cabbc6bSPrashanth Sreenivasa 					    zdb_leak, vd);
34835cabbc6bSPrashanth Sreenivasa 				}
34848363e80aSGeorge Wilson 
34858363e80aSGeorge Wilson 				if (msp->ms_loaded) {
34868363e80aSGeorge Wilson 					msp->ms_loaded = B_FALSE;
34878363e80aSGeorge Wilson 				}
3488b24ab676SJeff Bonwick 			}
3489b24ab676SJeff Bonwick 		}
34905cabbc6bSPrashanth Sreenivasa 
34915cabbc6bSPrashanth Sreenivasa 		umem_free(zcb->zcb_vd_obsolete_counts,
34925cabbc6bSPrashanth Sreenivasa 		    rvd->vdev_children * sizeof (uint32_t *));
34935cabbc6bSPrashanth Sreenivasa 		zcb->zcb_vd_obsolete_counts = NULL;
3494b24ab676SJeff Bonwick 	}
34955cabbc6bSPrashanth Sreenivasa 	return (leaks);
3496b24ab676SJeff Bonwick }
3497b24ab676SJeff Bonwick 
3498cde58dbcSMatthew Ahrens /* ARGSUSED */
3499cde58dbcSMatthew Ahrens static int
3500cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
3501cde58dbcSMatthew Ahrens {
3502cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
3503cde58dbcSMatthew Ahrens 
3504490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
3505cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
350643466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3507cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
3508cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
3509cde58dbcSMatthew Ahrens 	}
3510cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
3511cde58dbcSMatthew Ahrens 	return (0);
3512cde58dbcSMatthew Ahrens }
3513cde58dbcSMatthew Ahrens 
3514fa9e4066Sahrens static int
3515fa9e4066Sahrens dump_block_stats(spa_t *spa)
3516fa9e4066Sahrens {
35173f7978d0SAlan Somers 	zdb_cb_t zcb;
3518fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
3519b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
3520cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
35215d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
3522fa9e4066Sahrens 
35233f7978d0SAlan Somers 	bzero(&zcb, sizeof (zcb));
3524490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
3525b24ab676SJeff Bonwick 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
3526b24ab676SJeff Bonwick 	    (dump_opt['c'] == 1) ? "metadata " : "",
3527b24ab676SJeff Bonwick 	    dump_opt['c'] ? "checksums " : "",
3528b24ab676SJeff Bonwick 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
3529b24ab676SJeff Bonwick 	    !dump_opt['L'] ? "nothing leaked " : "");
3530fa9e4066Sahrens 
3531fa9e4066Sahrens 	/*
3532e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
3533e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
3534e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
3535e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
3536e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
3537e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
3538fa9e4066Sahrens 	 */
3539b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
3540fa9e4066Sahrens 
3541fa9e4066Sahrens 	/*
3542fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
3543fa9e4066Sahrens 	 */
3544cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
3545cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
35465cabbc6bSPrashanth Sreenivasa 
35473b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
35483b2aab18SMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
35493b2aab18SMatthew Ahrens 		    count_block_cb, &zcb, NULL);
35503b2aab18SMatthew Ahrens 	}
35515cabbc6bSPrashanth Sreenivasa 
35525cabbc6bSPrashanth Sreenivasa 	zdb_claim_removing(spa, &zcb);
35535cabbc6bSPrashanth Sreenivasa 
35542acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
3555b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
3556ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
3557ad135b5dSChristopher Siden 		    &zcb, NULL));
3558ad135b5dSChristopher Siden 	}
3559fa9e4066Sahrens 
3560bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
3561bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
3562bbfd46c4SJeff Bonwick 
3563490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
3564490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
3565bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
3566fa9e4066Sahrens 
356731d7e8faSGeorge Wilson 	/*
356831d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
356931d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
357031d7e8faSGeorge Wilson 	 * all async I/Os to complete.
357131d7e8faSGeorge Wilson 	 */
357231d7e8faSGeorge Wilson 	if (dump_opt['c']) {
35736f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
35746f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
35756f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
35766f834bc1SMatthew Ahrens 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
35776f834bc1SMatthew Ahrens 			    ZIO_FLAG_GODFATHER);
35786f834bc1SMatthew Ahrens 		}
357931d7e8faSGeorge Wilson 	}
358031d7e8faSGeorge Wilson 
3581b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
3582fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
3583fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
3584b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
3585fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
3586fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
3587fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
3588fa9e4066Sahrens 			}
3589fa9e4066Sahrens 		}
3590fa9e4066Sahrens 	}
3591fa9e4066Sahrens 
3592fa9e4066Sahrens 	/*
3593fa9e4066Sahrens 	 * Report any leaked segments.
3594fa9e4066Sahrens 	 */
35955cabbc6bSPrashanth Sreenivasa 	leaks |= zdb_leak_fini(spa, &zcb);
3596fa9e4066Sahrens 
3597b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
35988654d025Sperrin 
3599b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
3600b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
36018654d025Sperrin 
3602b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
36035cabbc6bSPrashanth Sreenivasa 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize +
3604*86714001SSerapheim Dimitropoulos 	    zcb.zcb_removing_size + zcb.zcb_checkpoint_size;
3605fa9e4066Sahrens 
3606b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
360782a0a985SVictor Latushkin 		if (!dump_opt['L'])
360882a0a985SVictor Latushkin 			(void) printf("\n\tNo leaks (block sum matches space"
360982a0a985SVictor Latushkin 			    " maps exactly)\n");
3610fa9e4066Sahrens 	} else {
3611fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
361282a0a985SVictor Latushkin 		    "(%s %lld)\n",
3613b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
3614b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
361582a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
3616b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
36175d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
3618fa9e4066Sahrens 	}
3619fa9e4066Sahrens 
3620fa9e4066Sahrens 	if (tzb->zb_count == 0)
3621fa9e4066Sahrens 		return (2);
3622fa9e4066Sahrens 
3623fa9e4066Sahrens 	(void) printf("\n");
3624fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
3625fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
3626d5ee8a13SMatthew Ahrens 	(void) printf("\tganged count:  %10llu\n",
3627d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
3628b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
3629fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
3630fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
3631b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
3632b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
3633fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
3634fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
3635fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
3636b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
3637b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
3638fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
3639fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
3640fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
3641b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
3642b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
3643b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
3644b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
3645b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
3646b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
3647b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
3648fa9e4066Sahrens 
36495d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
36505d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
36515d7b4d43SMatthew Ahrens 			continue;
36525d7b4d43SMatthew Ahrens 		(void) printf("\n");
36535d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
36545d7b4d43SMatthew Ahrens 		    "%10llu\n",
36555d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
36565d7b4d43SMatthew Ahrens 
36575d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
36585d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
36595d7b4d43SMatthew Ahrens 			    "number of bps\n");
36605d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
36615d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
36625d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
36635d7b4d43SMatthew Ahrens 		}
36645d7b4d43SMatthew Ahrens 	}
36655d7b4d43SMatthew Ahrens 
3666d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
3667d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
3668d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
3669d5ee8a13SMatthew Ahrens 	}
3670d5ee8a13SMatthew Ahrens 
36715cabbc6bSPrashanth Sreenivasa 	for (uint64_t v = 0; v < spa->spa_root_vdev->vdev_children; v++) {
36725cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = spa->spa_root_vdev->vdev_child[v];
36735cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
36745cabbc6bSPrashanth Sreenivasa 
36755cabbc6bSPrashanth Sreenivasa 		if (vim == NULL) {
36765cabbc6bSPrashanth Sreenivasa 			continue;
36775cabbc6bSPrashanth Sreenivasa 		}
36785cabbc6bSPrashanth Sreenivasa 
36795cabbc6bSPrashanth Sreenivasa 		char mem[32];
36805cabbc6bSPrashanth Sreenivasa 		zdb_nicenum(vdev_indirect_mapping_num_entries(vim),
36815cabbc6bSPrashanth Sreenivasa 		    mem, vdev_indirect_mapping_size(vim));
36825cabbc6bSPrashanth Sreenivasa 
36835cabbc6bSPrashanth Sreenivasa 		(void) printf("\tindirect vdev id %llu has %llu segments "
36845cabbc6bSPrashanth Sreenivasa 		    "(%s in memory)\n",
36855cabbc6bSPrashanth Sreenivasa 		    (longlong_t)vd->vdev_id,
36865cabbc6bSPrashanth Sreenivasa 		    (longlong_t)vdev_indirect_mapping_num_entries(vim), mem);
36875cabbc6bSPrashanth Sreenivasa 	}
36885cabbc6bSPrashanth Sreenivasa 
3689fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
3690fa9e4066Sahrens 		int l, t, level;
3691fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
3692fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
3693fa9e4066Sahrens 
3694b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
36953f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
3696d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
36973f7978d0SAlan Somers 			const char *typename;
3698fa9e4066Sahrens 
36990a055120SJason King 			/* make sure nicenum has enough space */
37000a055120SJason King 			CTASSERT(sizeof (csize) >= NN_NUMBUF_SZ);
37010a055120SJason King 			CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
37020a055120SJason King 			CTASSERT(sizeof (psize) >= NN_NUMBUF_SZ);
37030a055120SJason King 			CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
37040a055120SJason King 			CTASSERT(sizeof (avg) >= NN_NUMBUF_SZ);
37050a055120SJason King 			CTASSERT(sizeof (gang) >= NN_NUMBUF_SZ);
37060a055120SJason King 
3707b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
3708b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
3709b24ab676SJeff Bonwick 			else
3710b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
3711fa9e4066Sahrens 
3712fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
3713fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
3714fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
3715fa9e4066Sahrens 				    "-",
3716fa9e4066Sahrens 				    "-",
3717fa9e4066Sahrens 				    "-",
3718fa9e4066Sahrens 				    "-",
3719fa9e4066Sahrens 				    "-",
3720fa9e4066Sahrens 				    "-",
3721fa9e4066Sahrens 				    "-",
3722fa9e4066Sahrens 				    typename);
3723fa9e4066Sahrens 				continue;
3724fa9e4066Sahrens 			}
3725fa9e4066Sahrens 
3726fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
3727fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
3728fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
3729fa9e4066Sahrens 
3730fa9e4066Sahrens 				if (zb->zb_asize == 0)
3731fa9e4066Sahrens 					continue;
3732fa9e4066Sahrens 
3733fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
3734fa9e4066Sahrens 					continue;
3735fa9e4066Sahrens 
3736fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
3737fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
3738fa9e4066Sahrens 					continue;
3739fa9e4066Sahrens 
37400a055120SJason King 				zdb_nicenum(zb->zb_count, csize,
37410a055120SJason King 				    sizeof (csize));
37420a055120SJason King 				zdb_nicenum(zb->zb_lsize, lsize,
37430a055120SJason King 				    sizeof (lsize));
37440a055120SJason King 				zdb_nicenum(zb->zb_psize, psize,
37450a055120SJason King 				    sizeof (psize));
37460a055120SJason King 				zdb_nicenum(zb->zb_asize, asize,
37470a055120SJason King 				    sizeof (asize));
37480a055120SJason King 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg,
37490a055120SJason King 				    sizeof (avg));
37500a055120SJason King 				zdb_nicenum(zb->zb_gangs, gang, sizeof (gang));
3751fa9e4066Sahrens 
3752fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
3753fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
3754fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
3755fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
3756fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
3757fa9e4066Sahrens 
3758fa9e4066Sahrens 				if (level == ZB_TOTAL)
3759fa9e4066Sahrens 					(void) printf("%s\n", typename);
3760fa9e4066Sahrens 				else
3761fa9e4066Sahrens 					(void) printf("    L%d %s\n",
3762fa9e4066Sahrens 					    level, typename);
3763490d05b9SMatthew Ahrens 
3764d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
3765d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
3766d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
3767d5ee8a13SMatthew Ahrens 				}
3768d5ee8a13SMatthew Ahrens 
3769490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
3770490d05b9SMatthew Ahrens 					(void) printf("psize "
3771490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
3772490d05b9SMatthew Ahrens 					    "number of blocks\n");
3773490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
37740713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
3775490d05b9SMatthew Ahrens 				}
3776fa9e4066Sahrens 			}
3777fa9e4066Sahrens 		}
3778fa9e4066Sahrens 	}
3779fa9e4066Sahrens 
3780fa9e4066Sahrens 	(void) printf("\n");
3781fa9e4066Sahrens 
3782fa9e4066Sahrens 	if (leaks)
3783fa9e4066Sahrens 		return (2);
3784fa9e4066Sahrens 
3785fa9e4066Sahrens 	if (zcb.zcb_haderrors)
3786fa9e4066Sahrens 		return (3);
3787fa9e4066Sahrens 
3788fa9e4066Sahrens 	return (0);
3789fa9e4066Sahrens }
3790fa9e4066Sahrens 
3791b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
3792b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
3793b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
3794b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
3795b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
3796b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
3797b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
3798b24ab676SJeff Bonwick } zdb_ddt_entry_t;
3799b24ab676SJeff Bonwick 
3800b24ab676SJeff Bonwick /* ARGSUSED */
3801b24ab676SJeff Bonwick static int
3802b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
38037802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
3804b24ab676SJeff Bonwick {
3805b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
3806b24ab676SJeff Bonwick 	avl_index_t where;
3807b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
3808b24ab676SJeff Bonwick 
3809a2cdcdd2SPaul Dagnelie 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
3810b24ab676SJeff Bonwick 		return (0);
3811b24ab676SJeff Bonwick 
3812b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
3813b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
3814b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
3815b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
38165d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
3817b24ab676SJeff Bonwick 		    avl_numnodes(t));
3818b24ab676SJeff Bonwick 	}
3819b24ab676SJeff Bonwick 
3820bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
3821ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
3822b24ab676SJeff Bonwick 		return (0);
3823b24ab676SJeff Bonwick 
3824b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
3825b24ab676SJeff Bonwick 
3826b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
3827b24ab676SJeff Bonwick 
3828b24ab676SJeff Bonwick 	if (zdde == NULL) {
3829b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
3830b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
3831b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
3832b24ab676SJeff Bonwick 	}
3833b24ab676SJeff Bonwick 
3834b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
3835b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
3836b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
3837b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
3838b24ab676SJeff Bonwick 
3839b24ab676SJeff Bonwick 	return (0);
3840b24ab676SJeff Bonwick }
3841b24ab676SJeff Bonwick 
3842b24ab676SJeff Bonwick static void
3843b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
3844b24ab676SJeff Bonwick {
3845b24ab676SJeff Bonwick 	avl_tree_t t;
3846b24ab676SJeff Bonwick 	void *cookie = NULL;
3847b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
38483f7978d0SAlan Somers 	ddt_histogram_t ddh_total;
38493f7978d0SAlan Somers 	ddt_stat_t dds_total;
3850b24ab676SJeff Bonwick 
38513f7978d0SAlan Somers 	bzero(&ddh_total, sizeof (ddh_total));
38523f7978d0SAlan Somers 	bzero(&dds_total, sizeof (dds_total));
3853b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
3854b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
3855b24ab676SJeff Bonwick 
3856b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3857b24ab676SJeff Bonwick 
3858bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
3859bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
3860b24ab676SJeff Bonwick 
3861b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
3862b24ab676SJeff Bonwick 
3863b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
3864b24ab676SJeff Bonwick 		ddt_stat_t dds;
3865b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
3866b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
3867b24ab676SJeff Bonwick 
3868b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
3869b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
3870b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
3871b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
3872b24ab676SJeff Bonwick 
3873b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
3874b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
3875b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
3876b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
3877b24ab676SJeff Bonwick 
3878bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
3879bf16b11eSMatthew Ahrens 		    &dds, 0);
3880b24ab676SJeff Bonwick 
3881b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
3882b24ab676SJeff Bonwick 	}
3883b24ab676SJeff Bonwick 
3884b24ab676SJeff Bonwick 	avl_destroy(&t);
3885b24ab676SJeff Bonwick 
3886b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
3887b24ab676SJeff Bonwick 
3888b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
3889b24ab676SJeff Bonwick 
38909eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
3891b24ab676SJeff Bonwick 
3892b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
3893b24ab676SJeff Bonwick }
3894b24ab676SJeff Bonwick 
38955cabbc6bSPrashanth Sreenivasa static int
38965cabbc6bSPrashanth Sreenivasa verify_device_removal_feature_counts(spa_t *spa)
38975cabbc6bSPrashanth Sreenivasa {
38985cabbc6bSPrashanth Sreenivasa 	uint64_t dr_feature_refcount = 0;
38995cabbc6bSPrashanth Sreenivasa 	uint64_t oc_feature_refcount = 0;
39005cabbc6bSPrashanth Sreenivasa 	uint64_t indirect_vdev_count = 0;
39015cabbc6bSPrashanth Sreenivasa 	uint64_t precise_vdev_count = 0;
39025cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_counts_object_count = 0;
39035cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_count = 0;
39045cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_counts_count = 0;
39055cabbc6bSPrashanth Sreenivasa 	uint64_t scip_count = 0;
39065cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_bpobj_count = 0;
39075cabbc6bSPrashanth Sreenivasa 	int ret = 0;
39085cabbc6bSPrashanth Sreenivasa 
39095cabbc6bSPrashanth Sreenivasa 	spa_condensing_indirect_phys_t *scip =
39105cabbc6bSPrashanth Sreenivasa 	    &spa->spa_condensing_indirect_phys;
39115cabbc6bSPrashanth Sreenivasa 	if (scip->scip_next_mapping_object != 0) {
39125cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = spa->spa_root_vdev->vdev_child[scip->scip_vdev];
39135cabbc6bSPrashanth Sreenivasa 		ASSERT(scip->scip_prev_obsolete_sm_object != 0);
39145cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
39155cabbc6bSPrashanth Sreenivasa 
39165cabbc6bSPrashanth Sreenivasa 		(void) printf("Condensing indirect vdev %llu: new mapping "
39175cabbc6bSPrashanth Sreenivasa 		    "object %llu, prev obsolete sm %llu\n",
39185cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)scip->scip_vdev,
39195cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)scip->scip_next_mapping_object,
39205cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)scip->scip_prev_obsolete_sm_object);
39215cabbc6bSPrashanth Sreenivasa 		if (scip->scip_prev_obsolete_sm_object != 0) {
39225cabbc6bSPrashanth Sreenivasa 			space_map_t *prev_obsolete_sm = NULL;
39235cabbc6bSPrashanth Sreenivasa 			VERIFY0(space_map_open(&prev_obsolete_sm,
39245cabbc6bSPrashanth Sreenivasa 			    spa->spa_meta_objset,
39255cabbc6bSPrashanth Sreenivasa 			    scip->scip_prev_obsolete_sm_object,
39265cabbc6bSPrashanth Sreenivasa 			    0, vd->vdev_asize, 0));
39275cabbc6bSPrashanth Sreenivasa 			space_map_update(prev_obsolete_sm);
39285cabbc6bSPrashanth Sreenivasa 			dump_spacemap(spa->spa_meta_objset, prev_obsolete_sm);
39295cabbc6bSPrashanth Sreenivasa 			(void) printf("\n");
39305cabbc6bSPrashanth Sreenivasa 			space_map_close(prev_obsolete_sm);
39315cabbc6bSPrashanth Sreenivasa 		}
39325cabbc6bSPrashanth Sreenivasa 
39335cabbc6bSPrashanth Sreenivasa 		scip_count += 2;
39345cabbc6bSPrashanth Sreenivasa 	}
39355cabbc6bSPrashanth Sreenivasa 
39365cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < spa->spa_root_vdev->vdev_children; i++) {
39375cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = spa->spa_root_vdev->vdev_child[i];
39385cabbc6bSPrashanth Sreenivasa 		vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
39395cabbc6bSPrashanth Sreenivasa 
39405cabbc6bSPrashanth Sreenivasa 		if (vic->vic_mapping_object != 0) {
39415cabbc6bSPrashanth Sreenivasa 			ASSERT(vd->vdev_ops == &vdev_indirect_ops ||
39425cabbc6bSPrashanth Sreenivasa 			    vd->vdev_removing);
39435cabbc6bSPrashanth Sreenivasa 			indirect_vdev_count++;
39445cabbc6bSPrashanth Sreenivasa 
39455cabbc6bSPrashanth Sreenivasa 			if (vd->vdev_indirect_mapping->vim_havecounts) {
39465cabbc6bSPrashanth Sreenivasa 				obsolete_counts_count++;
39475cabbc6bSPrashanth Sreenivasa 			}
39485cabbc6bSPrashanth Sreenivasa 		}
39495cabbc6bSPrashanth Sreenivasa 		if (vdev_obsolete_counts_are_precise(vd)) {
39505cabbc6bSPrashanth Sreenivasa 			ASSERT(vic->vic_mapping_object != 0);
39515cabbc6bSPrashanth Sreenivasa 			precise_vdev_count++;
39525cabbc6bSPrashanth Sreenivasa 		}
39535cabbc6bSPrashanth Sreenivasa 		if (vdev_obsolete_sm_object(vd) != 0) {
39545cabbc6bSPrashanth Sreenivasa 			ASSERT(vic->vic_mapping_object != 0);
39555cabbc6bSPrashanth Sreenivasa 			obsolete_sm_count++;
39565cabbc6bSPrashanth Sreenivasa 		}
39575cabbc6bSPrashanth Sreenivasa 	}
39585cabbc6bSPrashanth Sreenivasa 
39595cabbc6bSPrashanth Sreenivasa 	(void) feature_get_refcount(spa,
39605cabbc6bSPrashanth Sreenivasa 	    &spa_feature_table[SPA_FEATURE_DEVICE_REMOVAL],
39615cabbc6bSPrashanth Sreenivasa 	    &dr_feature_refcount);
39625cabbc6bSPrashanth Sreenivasa 	(void) feature_get_refcount(spa,
39635cabbc6bSPrashanth Sreenivasa 	    &spa_feature_table[SPA_FEATURE_OBSOLETE_COUNTS],
39645cabbc6bSPrashanth Sreenivasa 	    &oc_feature_refcount);
39655cabbc6bSPrashanth Sreenivasa 
39665cabbc6bSPrashanth Sreenivasa 	if (dr_feature_refcount != indirect_vdev_count) {
39675cabbc6bSPrashanth Sreenivasa 		ret = 1;
39685cabbc6bSPrashanth Sreenivasa 		(void) printf("Number of indirect vdevs (%llu) " \
39695cabbc6bSPrashanth Sreenivasa 		    "does not match feature count (%llu)\n",
39705cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)indirect_vdev_count,
39715cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)dr_feature_refcount);
39725cabbc6bSPrashanth Sreenivasa 	} else {
39735cabbc6bSPrashanth Sreenivasa 		(void) printf("Verified device_removal feature refcount " \
39745cabbc6bSPrashanth Sreenivasa 		    "of %llu is correct\n",
39755cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)dr_feature_refcount);
39765cabbc6bSPrashanth Sreenivasa 	}
39775cabbc6bSPrashanth Sreenivasa 
39785cabbc6bSPrashanth Sreenivasa 	if (zap_contains(spa_meta_objset(spa), DMU_POOL_DIRECTORY_OBJECT,
39795cabbc6bSPrashanth Sreenivasa 	    DMU_POOL_OBSOLETE_BPOBJ) == 0) {
39805cabbc6bSPrashanth Sreenivasa 		obsolete_bpobj_count++;
39815cabbc6bSPrashanth Sreenivasa 	}
39825cabbc6bSPrashanth Sreenivasa 
39835cabbc6bSPrashanth Sreenivasa 
39845cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count = precise_vdev_count;
39855cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += obsolete_sm_count;
39865cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += obsolete_counts_count;
39875cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += scip_count;
39885cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += obsolete_bpobj_count;
39895cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += remap_deadlist_count;
39905cabbc6bSPrashanth Sreenivasa 
39915cabbc6bSPrashanth Sreenivasa 	if (oc_feature_refcount != obsolete_counts_object_count) {
39925cabbc6bSPrashanth Sreenivasa 		ret = 1;
39935cabbc6bSPrashanth Sreenivasa 		(void) printf("Number of obsolete counts objects (%llu) " \
39945cabbc6bSPrashanth Sreenivasa 		    "does not match feature count (%llu)\n",
39955cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_counts_object_count,
39965cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)oc_feature_refcount);
39975cabbc6bSPrashanth Sreenivasa 		(void) printf("pv:%llu os:%llu oc:%llu sc:%llu "
39985cabbc6bSPrashanth Sreenivasa 		    "ob:%llu rd:%llu\n",
39995cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)precise_vdev_count,
40005cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_sm_count,
40015cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_counts_count,
40025cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)scip_count,
40035cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_bpobj_count,
40045cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)remap_deadlist_count);
40055cabbc6bSPrashanth Sreenivasa 	} else {
40065cabbc6bSPrashanth Sreenivasa 		(void) printf("Verified indirect_refcount feature refcount " \
40075cabbc6bSPrashanth Sreenivasa 		    "of %llu is correct\n",
40085cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)oc_feature_refcount);
40095cabbc6bSPrashanth Sreenivasa 	}
40105cabbc6bSPrashanth Sreenivasa 	return (ret);
40115cabbc6bSPrashanth Sreenivasa }
40125cabbc6bSPrashanth Sreenivasa 
4013*86714001SSerapheim Dimitropoulos #define	BOGUS_SUFFIX "_CHECKPOINTED_UNIVERSE"
4014*86714001SSerapheim Dimitropoulos /*
4015*86714001SSerapheim Dimitropoulos  * Import the checkpointed state of the pool specified by the target
4016*86714001SSerapheim Dimitropoulos  * parameter as readonly. The function also accepts a pool config
4017*86714001SSerapheim Dimitropoulos  * as an optional parameter, else it attempts to infer the config by
4018*86714001SSerapheim Dimitropoulos  * the name of the target pool.
4019*86714001SSerapheim Dimitropoulos  *
4020*86714001SSerapheim Dimitropoulos  * Note that the checkpointed state's pool name will be the name of
4021*86714001SSerapheim Dimitropoulos  * the original pool with the above suffix appened to it. In addition,
4022*86714001SSerapheim Dimitropoulos  * if the target is not a pool name (e.g. a path to a dataset) then
4023*86714001SSerapheim Dimitropoulos  * the new_path parameter is populated with the updated path to
4024*86714001SSerapheim Dimitropoulos  * reflect the fact that we are looking into the checkpointed state.
4025*86714001SSerapheim Dimitropoulos  *
4026*86714001SSerapheim Dimitropoulos  * The function returns a newly-allocated copy of the name of the
4027*86714001SSerapheim Dimitropoulos  * pool containing the checkpointed state. When this copy is no
4028*86714001SSerapheim Dimitropoulos  * longer needed it should be freed with free(3C). Same thing
4029*86714001SSerapheim Dimitropoulos  * applies to the new_path parameter if allocated.
4030*86714001SSerapheim Dimitropoulos  */
4031*86714001SSerapheim Dimitropoulos static char *
4032*86714001SSerapheim Dimitropoulos import_checkpointed_state(char *target, nvlist_t *cfg, char **new_path)
4033*86714001SSerapheim Dimitropoulos {
4034*86714001SSerapheim Dimitropoulos 	int error = 0;
4035*86714001SSerapheim Dimitropoulos 	char *poolname, *bogus_name;
4036*86714001SSerapheim Dimitropoulos 
4037*86714001SSerapheim Dimitropoulos 	/* If the target is not a pool, the extract the pool name */
4038*86714001SSerapheim Dimitropoulos 	char *path_start = strchr(target, '/');
4039*86714001SSerapheim Dimitropoulos 	if (path_start != NULL) {
4040*86714001SSerapheim Dimitropoulos 		size_t poolname_len = path_start - target;
4041*86714001SSerapheim Dimitropoulos 		poolname = strndup(target, poolname_len);
4042*86714001SSerapheim Dimitropoulos 	} else {
4043*86714001SSerapheim Dimitropoulos 		poolname = target;
4044*86714001SSerapheim Dimitropoulos 	}
4045*86714001SSerapheim Dimitropoulos 
4046*86714001SSerapheim Dimitropoulos 	if (cfg == NULL) {
4047*86714001SSerapheim Dimitropoulos 		error = spa_get_stats(poolname, &cfg, NULL, 0);
4048*86714001SSerapheim Dimitropoulos 		if (error != 0) {
4049*86714001SSerapheim Dimitropoulos 			fatal("Tried to read config of pool \"%s\" but "
4050*86714001SSerapheim Dimitropoulos 			    "spa_get_stats() failed with error %d\n",
4051*86714001SSerapheim Dimitropoulos 			    poolname, error);
4052*86714001SSerapheim Dimitropoulos 		}
4053*86714001SSerapheim Dimitropoulos 	}
4054*86714001SSerapheim Dimitropoulos 
4055*86714001SSerapheim Dimitropoulos 	(void) asprintf(&bogus_name, "%s%s", poolname, BOGUS_SUFFIX);
4056*86714001SSerapheim Dimitropoulos 	fnvlist_add_string(cfg, ZPOOL_CONFIG_POOL_NAME, bogus_name);
4057*86714001SSerapheim Dimitropoulos 
4058*86714001SSerapheim Dimitropoulos 	error = spa_import(bogus_name, cfg, NULL,
4059*86714001SSerapheim Dimitropoulos 	    ZFS_IMPORT_MISSING_LOG | ZFS_IMPORT_CHECKPOINT);
4060*86714001SSerapheim Dimitropoulos 	if (error != 0) {
4061*86714001SSerapheim Dimitropoulos 		fatal("Tried to import pool \"%s\" but spa_import() failed "
4062*86714001SSerapheim Dimitropoulos 		    "with error %d\n", bogus_name, error);
4063*86714001SSerapheim Dimitropoulos 	}
4064*86714001SSerapheim Dimitropoulos 
4065*86714001SSerapheim Dimitropoulos 	if (new_path != NULL && path_start != NULL)
4066*86714001SSerapheim Dimitropoulos 		(void) asprintf(new_path, "%s%s", bogus_name, path_start);
4067*86714001SSerapheim Dimitropoulos 
4068*86714001SSerapheim Dimitropoulos 	if (target != poolname)
4069*86714001SSerapheim Dimitropoulos 		free(poolname);
4070*86714001SSerapheim Dimitropoulos 
4071*86714001SSerapheim Dimitropoulos 	return (bogus_name);
4072*86714001SSerapheim Dimitropoulos }
4073*86714001SSerapheim Dimitropoulos 
4074*86714001SSerapheim Dimitropoulos typedef struct verify_checkpoint_sm_entry_cb_arg {
4075*86714001SSerapheim Dimitropoulos 	vdev_t *vcsec_vd;
4076*86714001SSerapheim Dimitropoulos 
4077*86714001SSerapheim Dimitropoulos 	/* the following fields are only used for printing progress */
4078*86714001SSerapheim Dimitropoulos 	uint64_t vcsec_entryid;
4079*86714001SSerapheim Dimitropoulos 	uint64_t vcsec_num_entries;
4080*86714001SSerapheim Dimitropoulos } verify_checkpoint_sm_entry_cb_arg_t;
4081*86714001SSerapheim Dimitropoulos 
4082*86714001SSerapheim Dimitropoulos #define	ENTRIES_PER_PROGRESS_UPDATE 10000
4083*86714001SSerapheim Dimitropoulos 
4084*86714001SSerapheim Dimitropoulos static int
4085*86714001SSerapheim Dimitropoulos verify_checkpoint_sm_entry_cb(maptype_t type, uint64_t offset, uint64_t size,
4086*86714001SSerapheim Dimitropoulos     void *arg)
4087*86714001SSerapheim Dimitropoulos {
4088*86714001SSerapheim Dimitropoulos 	verify_checkpoint_sm_entry_cb_arg_t *vcsec = arg;
4089*86714001SSerapheim Dimitropoulos 	vdev_t *vd = vcsec->vcsec_vd;
4090*86714001SSerapheim Dimitropoulos 	metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
4091*86714001SSerapheim Dimitropoulos 	uint64_t end = offset + size;
4092*86714001SSerapheim Dimitropoulos 
4093*86714001SSerapheim Dimitropoulos 	ASSERT(type == SM_FREE);
4094*86714001SSerapheim Dimitropoulos 
4095*86714001SSerapheim Dimitropoulos 	if ((vcsec->vcsec_entryid % ENTRIES_PER_PROGRESS_UPDATE) == 0) {
4096*86714001SSerapheim Dimitropoulos 		(void) fprintf(stderr,
4097*86714001SSerapheim Dimitropoulos 		    "\rverifying vdev %llu, space map entry %llu of %llu ...",
4098*86714001SSerapheim Dimitropoulos 		    (longlong_t)vd->vdev_id,
4099*86714001SSerapheim Dimitropoulos 		    (longlong_t)vcsec->vcsec_entryid,
4100*86714001SSerapheim Dimitropoulos 		    (longlong_t)vcsec->vcsec_num_entries);
4101*86714001SSerapheim Dimitropoulos 	}
4102*86714001SSerapheim Dimitropoulos 	vcsec->vcsec_entryid++;
4103*86714001SSerapheim Dimitropoulos 
4104*86714001SSerapheim Dimitropoulos 	/*
4105*86714001SSerapheim Dimitropoulos 	 * See comment in checkpoint_sm_exclude_entry_cb()
4106*86714001SSerapheim Dimitropoulos 	 */
4107*86714001SSerapheim Dimitropoulos 	VERIFY3U(offset, >=, ms->ms_start);
4108*86714001SSerapheim Dimitropoulos 	VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
4109*86714001SSerapheim Dimitropoulos 
4110*86714001SSerapheim Dimitropoulos 	/*
4111*86714001SSerapheim Dimitropoulos 	 * The entries in the vdev_checkpoint_sm should be marked as
4112*86714001SSerapheim Dimitropoulos 	 * allocated in the checkpointed state of the pool, therefore
4113*86714001SSerapheim Dimitropoulos 	 * their respective ms_allocateable trees should not contain them.
4114*86714001SSerapheim Dimitropoulos 	 */
4115*86714001SSerapheim Dimitropoulos 	mutex_enter(&ms->ms_lock);
4116*86714001SSerapheim Dimitropoulos 	range_tree_verify(ms->ms_allocatable, offset, size);
4117*86714001SSerapheim Dimitropoulos 	mutex_exit(&ms->ms_lock);
4118*86714001SSerapheim Dimitropoulos 
4119*86714001SSerapheim Dimitropoulos 	return (0);
4120*86714001SSerapheim Dimitropoulos }
4121*86714001SSerapheim Dimitropoulos 
4122*86714001SSerapheim Dimitropoulos /*
4123*86714001SSerapheim Dimitropoulos  * Verify that all segments in the vdev_checkpoint_sm are allocated
4124*86714001SSerapheim Dimitropoulos  * according to the checkpoint's ms_sm (i.e. are not in the checkpoint's
4125*86714001SSerapheim Dimitropoulos  * ms_allocatable).
4126*86714001SSerapheim Dimitropoulos  *
4127*86714001SSerapheim Dimitropoulos  * Do so by comparing the checkpoint space maps (vdev_checkpoint_sm) of
4128*86714001SSerapheim Dimitropoulos  * each vdev in the current state of the pool to the metaslab space maps
4129*86714001SSerapheim Dimitropoulos  * (ms_sm) of the checkpointed state of the pool.
4130*86714001SSerapheim Dimitropoulos  *
4131*86714001SSerapheim Dimitropoulos  * Note that the function changes the state of the ms_allocatable
4132*86714001SSerapheim Dimitropoulos  * trees of the current spa_t. The entries of these ms_allocatable
4133*86714001SSerapheim Dimitropoulos  * trees are cleared out and then repopulated from with the free
4134*86714001SSerapheim Dimitropoulos  * entries of their respective ms_sm space maps.
4135*86714001SSerapheim Dimitropoulos  */
4136*86714001SSerapheim Dimitropoulos static void
4137*86714001SSerapheim Dimitropoulos verify_checkpoint_vdev_spacemaps(spa_t *checkpoint, spa_t *current)
4138*86714001SSerapheim Dimitropoulos {
4139*86714001SSerapheim Dimitropoulos 	vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
4140*86714001SSerapheim Dimitropoulos 	vdev_t *current_rvd = current->spa_root_vdev;
4141*86714001SSerapheim Dimitropoulos 
4142*86714001SSerapheim Dimitropoulos 	load_concrete_ms_allocatable_trees(checkpoint, SM_FREE);
4143*86714001SSerapheim Dimitropoulos 
4144*86714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < ckpoint_rvd->vdev_children; c++) {
4145*86714001SSerapheim Dimitropoulos 		vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[c];
4146*86714001SSerapheim Dimitropoulos 		vdev_t *current_vd = current_rvd->vdev_child[c];
4147*86714001SSerapheim Dimitropoulos 
4148*86714001SSerapheim Dimitropoulos 		space_map_t *checkpoint_sm = NULL;
4149*86714001SSerapheim Dimitropoulos 		uint64_t checkpoint_sm_obj;
4150*86714001SSerapheim Dimitropoulos 
4151*86714001SSerapheim Dimitropoulos 		if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
4152*86714001SSerapheim Dimitropoulos 			/*
4153*86714001SSerapheim Dimitropoulos 			 * Since we don't allow device removal in a pool
4154*86714001SSerapheim Dimitropoulos 			 * that has a checkpoint, we expect that all removed
4155*86714001SSerapheim Dimitropoulos 			 * vdevs were removed from the pool before the
4156*86714001SSerapheim Dimitropoulos 			 * checkpoint.
4157*86714001SSerapheim Dimitropoulos 			 */
4158*86714001SSerapheim Dimitropoulos 			ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
4159*86714001SSerapheim Dimitropoulos 			continue;
4160*86714001SSerapheim Dimitropoulos 		}
4161*86714001SSerapheim Dimitropoulos 
4162*86714001SSerapheim Dimitropoulos 		/*
4163*86714001SSerapheim Dimitropoulos 		 * If the checkpoint space map doesn't exist, then nothing
4164*86714001SSerapheim Dimitropoulos 		 * here is checkpointed so there's nothing to verify.
4165*86714001SSerapheim Dimitropoulos 		 */
4166*86714001SSerapheim Dimitropoulos 		if (current_vd->vdev_top_zap == 0 ||
4167*86714001SSerapheim Dimitropoulos 		    zap_contains(spa_meta_objset(current),
4168*86714001SSerapheim Dimitropoulos 		    current_vd->vdev_top_zap,
4169*86714001SSerapheim Dimitropoulos 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
4170*86714001SSerapheim Dimitropoulos 			continue;
4171*86714001SSerapheim Dimitropoulos 
4172*86714001SSerapheim Dimitropoulos 		VERIFY0(zap_lookup(spa_meta_objset(current),
4173*86714001SSerapheim Dimitropoulos 		    current_vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
4174*86714001SSerapheim Dimitropoulos 		    sizeof (uint64_t), 1, &checkpoint_sm_obj));
4175*86714001SSerapheim Dimitropoulos 
4176*86714001SSerapheim Dimitropoulos 		VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(current),
4177*86714001SSerapheim Dimitropoulos 		    checkpoint_sm_obj, 0, current_vd->vdev_asize,
4178*86714001SSerapheim Dimitropoulos 		    current_vd->vdev_ashift));
4179*86714001SSerapheim Dimitropoulos 		space_map_update(checkpoint_sm);
4180*86714001SSerapheim Dimitropoulos 
4181*86714001SSerapheim Dimitropoulos 		verify_checkpoint_sm_entry_cb_arg_t vcsec;
4182*86714001SSerapheim Dimitropoulos 		vcsec.vcsec_vd = ckpoint_vd;
4183*86714001SSerapheim Dimitropoulos 		vcsec.vcsec_entryid = 0;
4184*86714001SSerapheim Dimitropoulos 		vcsec.vcsec_num_entries =
4185*86714001SSerapheim Dimitropoulos 		    space_map_length(checkpoint_sm) / sizeof (uint64_t);
4186*86714001SSerapheim Dimitropoulos 		VERIFY0(space_map_iterate(checkpoint_sm,
4187*86714001SSerapheim Dimitropoulos 		    verify_checkpoint_sm_entry_cb, &vcsec));
4188*86714001SSerapheim Dimitropoulos 		dump_spacemap(current->spa_meta_objset, checkpoint_sm);
4189*86714001SSerapheim Dimitropoulos 		space_map_close(checkpoint_sm);
4190*86714001SSerapheim Dimitropoulos 	}
4191*86714001SSerapheim Dimitropoulos 
4192*86714001SSerapheim Dimitropoulos 	/*
4193*86714001SSerapheim Dimitropoulos 	 * If we've added vdevs since we took the checkpoint, ensure
4194*86714001SSerapheim Dimitropoulos 	 * that their checkpoint space maps are empty.
4195*86714001SSerapheim Dimitropoulos 	 */
4196*86714001SSerapheim Dimitropoulos 	if (ckpoint_rvd->vdev_children < current_rvd->vdev_children) {
4197*86714001SSerapheim Dimitropoulos 		for (uint64_t c = ckpoint_rvd->vdev_children;
4198*86714001SSerapheim Dimitropoulos 		    c < current_rvd->vdev_children; c++) {
4199*86714001SSerapheim Dimitropoulos 			vdev_t *current_vd = current_rvd->vdev_child[c];
4200*86714001SSerapheim Dimitropoulos 			ASSERT3P(current_vd->vdev_checkpoint_sm, ==, NULL);
4201*86714001SSerapheim Dimitropoulos 		}
4202*86714001SSerapheim Dimitropoulos 	}
4203*86714001SSerapheim Dimitropoulos 
4204*86714001SSerapheim Dimitropoulos 	/* for cleaner progress output */
4205*86714001SSerapheim Dimitropoulos 	(void) fprintf(stderr, "\n");
4206*86714001SSerapheim Dimitropoulos }
4207*86714001SSerapheim Dimitropoulos 
4208*86714001SSerapheim Dimitropoulos /*
4209*86714001SSerapheim Dimitropoulos  * Verifies that all space that's allocated in the checkpoint is
4210*86714001SSerapheim Dimitropoulos  * still allocated in the current version, by checking that everything
4211*86714001SSerapheim Dimitropoulos  * in checkpoint's ms_allocatable (which is actually allocated, not
4212*86714001SSerapheim Dimitropoulos  * allocatable/free) is not present in current's ms_allocatable.
4213*86714001SSerapheim Dimitropoulos  *
4214*86714001SSerapheim Dimitropoulos  * Note that the function changes the state of the ms_allocatable
4215*86714001SSerapheim Dimitropoulos  * trees of both spas when called. The entries of all ms_allocatable
4216*86714001SSerapheim Dimitropoulos  * trees are cleared out and then repopulated from their respective
4217*86714001SSerapheim Dimitropoulos  * ms_sm space maps. In the checkpointed state we load the allocated
4218*86714001SSerapheim Dimitropoulos  * entries, and in the current state we load the free entries.
4219*86714001SSerapheim Dimitropoulos  */
4220*86714001SSerapheim Dimitropoulos static void
4221*86714001SSerapheim Dimitropoulos verify_checkpoint_ms_spacemaps(spa_t *checkpoint, spa_t *current)
4222*86714001SSerapheim Dimitropoulos {
4223*86714001SSerapheim Dimitropoulos 	vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
4224*86714001SSerapheim Dimitropoulos 	vdev_t *current_rvd = current->spa_root_vdev;
4225*86714001SSerapheim Dimitropoulos 
4226*86714001SSerapheim Dimitropoulos 	load_concrete_ms_allocatable_trees(checkpoint, SM_ALLOC);
4227*86714001SSerapheim Dimitropoulos 	load_concrete_ms_allocatable_trees(current, SM_FREE);
4228*86714001SSerapheim Dimitropoulos 
4229*86714001SSerapheim Dimitropoulos 	for (uint64_t i = 0; i < ckpoint_rvd->vdev_children; i++) {
4230*86714001SSerapheim Dimitropoulos 		vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[i];
4231*86714001SSerapheim Dimitropoulos 		vdev_t *current_vd = current_rvd->vdev_child[i];
4232*86714001SSerapheim Dimitropoulos 
4233*86714001SSerapheim Dimitropoulos 		if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
4234*86714001SSerapheim Dimitropoulos 			/*
4235*86714001SSerapheim Dimitropoulos 			 * See comment in verify_checkpoint_vdev_spacemaps()
4236*86714001SSerapheim Dimitropoulos 			 */
4237*86714001SSerapheim Dimitropoulos 			ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
4238*86714001SSerapheim Dimitropoulos 			continue;
4239*86714001SSerapheim Dimitropoulos 		}
4240*86714001SSerapheim Dimitropoulos 
4241*86714001SSerapheim Dimitropoulos 		for (uint64_t m = 0; m < ckpoint_vd->vdev_ms_count; m++) {
4242*86714001SSerapheim Dimitropoulos 			metaslab_t *ckpoint_msp = ckpoint_vd->vdev_ms[m];
4243*86714001SSerapheim Dimitropoulos 			metaslab_t *current_msp = current_vd->vdev_ms[m];
4244*86714001SSerapheim Dimitropoulos 
4245*86714001SSerapheim Dimitropoulos 			(void) fprintf(stderr,
4246*86714001SSerapheim Dimitropoulos 			    "\rverifying vdev %llu of %llu, "
4247*86714001SSerapheim Dimitropoulos 			    "metaslab %llu of %llu ...",
4248*86714001SSerapheim Dimitropoulos 			    (longlong_t)current_vd->vdev_id,
4249*86714001SSerapheim Dimitropoulos 			    (longlong_t)current_rvd->vdev_children,
4250*86714001SSerapheim Dimitropoulos 			    (longlong_t)current_vd->vdev_ms[m]->ms_id,
4251*86714001SSerapheim Dimitropoulos 			    (longlong_t)current_vd->vdev_ms_count);
4252*86714001SSerapheim Dimitropoulos 
4253*86714001SSerapheim Dimitropoulos 			/*
4254*86714001SSerapheim Dimitropoulos 			 * We walk through the ms_allocatable trees that
4255*86714001SSerapheim Dimitropoulos 			 * are loaded with the allocated blocks from the
4256*86714001SSerapheim Dimitropoulos 			 * ms_sm spacemaps of the checkpoint. For each
4257*86714001SSerapheim Dimitropoulos 			 * one of these ranges we ensure that none of them
4258*86714001SSerapheim Dimitropoulos 			 * exists in the ms_allocatable trees of the
4259*86714001SSerapheim Dimitropoulos 			 * current state which are loaded with the ranges
4260*86714001SSerapheim Dimitropoulos 			 * that are currently free.
4261*86714001SSerapheim Dimitropoulos 			 *
4262*86714001SSerapheim Dimitropoulos 			 * This way we ensure that none of the blocks that
4263*86714001SSerapheim Dimitropoulos 			 * are part of the checkpoint were freed by mistake.
4264*86714001SSerapheim Dimitropoulos 			 */
4265*86714001SSerapheim Dimitropoulos 			range_tree_walk(ckpoint_msp->ms_allocatable,
4266*86714001SSerapheim Dimitropoulos 			    (range_tree_func_t *)range_tree_verify,
4267*86714001SSerapheim Dimitropoulos 			    current_msp->ms_allocatable);
4268*86714001SSerapheim Dimitropoulos 		}
4269*86714001SSerapheim Dimitropoulos 	}
4270*86714001SSerapheim Dimitropoulos 
4271*86714001SSerapheim Dimitropoulos 	/* for cleaner progress output */
4272*86714001SSerapheim Dimitropoulos 	(void) fprintf(stderr, "\n");
4273*86714001SSerapheim Dimitropoulos }
4274*86714001SSerapheim Dimitropoulos 
4275*86714001SSerapheim Dimitropoulos static void
4276*86714001SSerapheim Dimitropoulos verify_checkpoint_blocks(spa_t *spa)
4277*86714001SSerapheim Dimitropoulos {
4278*86714001SSerapheim Dimitropoulos 	spa_t *checkpoint_spa;
4279*86714001SSerapheim Dimitropoulos 	char *checkpoint_pool;
4280*86714001SSerapheim Dimitropoulos 	nvlist_t *config = NULL;
4281*86714001SSerapheim Dimitropoulos 	int error = 0;
4282*86714001SSerapheim Dimitropoulos 
4283*86714001SSerapheim Dimitropoulos 	/*
4284*86714001SSerapheim Dimitropoulos 	 * We import the checkpointed state of the pool (under a different
4285*86714001SSerapheim Dimitropoulos 	 * name) so we can do verification on it against the current state
4286*86714001SSerapheim Dimitropoulos 	 * of the pool.
4287*86714001SSerapheim Dimitropoulos 	 */
4288*86714001SSerapheim Dimitropoulos 	checkpoint_pool = import_checkpointed_state(spa->spa_name, config,
4289*86714001SSerapheim Dimitropoulos 	    NULL);
4290*86714001SSerapheim Dimitropoulos 	ASSERT(strcmp(spa->spa_name, checkpoint_pool) != 0);
4291*86714001SSerapheim Dimitropoulos 
4292*86714001SSerapheim Dimitropoulos 	error = spa_open(checkpoint_pool, &checkpoint_spa, FTAG);
4293*86714001SSerapheim Dimitropoulos 	if (error != 0) {
4294*86714001SSerapheim Dimitropoulos 		fatal("Tried to open pool \"%s\" but spa_open() failed with "
4295*86714001SSerapheim Dimitropoulos 		    "error %d\n", checkpoint_pool, error);
4296*86714001SSerapheim Dimitropoulos 	}
4297*86714001SSerapheim Dimitropoulos 
4298*86714001SSerapheim Dimitropoulos 	/*
4299*86714001SSerapheim Dimitropoulos 	 * Ensure that ranges in the checkpoint space maps of each vdev
4300*86714001SSerapheim Dimitropoulos 	 * are allocated according to the checkpointed state's metaslab
4301*86714001SSerapheim Dimitropoulos 	 * space maps.
4302*86714001SSerapheim Dimitropoulos 	 */
4303*86714001SSerapheim Dimitropoulos 	verify_checkpoint_vdev_spacemaps(checkpoint_spa, spa);
4304*86714001SSerapheim Dimitropoulos 
4305*86714001SSerapheim Dimitropoulos 	/*
4306*86714001SSerapheim Dimitropoulos 	 * Ensure that allocated ranges in the checkpoint's metaslab
4307*86714001SSerapheim Dimitropoulos 	 * space maps remain allocated in the metaslab space maps of
4308*86714001SSerapheim Dimitropoulos 	 * the current state.
4309*86714001SSerapheim Dimitropoulos 	 */
4310*86714001SSerapheim Dimitropoulos 	verify_checkpoint_ms_spacemaps(checkpoint_spa, spa);
4311*86714001SSerapheim Dimitropoulos 
4312*86714001SSerapheim Dimitropoulos 	/*
4313*86714001SSerapheim Dimitropoulos 	 * Once we are done, we get rid of the checkpointed state.
4314*86714001SSerapheim Dimitropoulos 	 */
4315*86714001SSerapheim Dimitropoulos 	spa_close(checkpoint_spa, FTAG);
4316*86714001SSerapheim Dimitropoulos 	free(checkpoint_pool);
4317*86714001SSerapheim Dimitropoulos }
4318*86714001SSerapheim Dimitropoulos 
4319*86714001SSerapheim Dimitropoulos static void
4320*86714001SSerapheim Dimitropoulos dump_leftover_checkpoint_blocks(spa_t *spa)
4321*86714001SSerapheim Dimitropoulos {
4322*86714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
4323*86714001SSerapheim Dimitropoulos 
4324*86714001SSerapheim Dimitropoulos 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
4325*86714001SSerapheim Dimitropoulos 		vdev_t *vd = rvd->vdev_child[i];
4326*86714001SSerapheim Dimitropoulos 
4327*86714001SSerapheim Dimitropoulos 		space_map_t *checkpoint_sm = NULL;
4328*86714001SSerapheim Dimitropoulos 		uint64_t checkpoint_sm_obj;
4329*86714001SSerapheim Dimitropoulos 
4330*86714001SSerapheim Dimitropoulos 		if (vd->vdev_top_zap == 0)
4331*86714001SSerapheim Dimitropoulos 			continue;
4332*86714001SSerapheim Dimitropoulos 
4333*86714001SSerapheim Dimitropoulos 		if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
4334*86714001SSerapheim Dimitropoulos 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
4335*86714001SSerapheim Dimitropoulos 			continue;
4336*86714001SSerapheim Dimitropoulos 
4337*86714001SSerapheim Dimitropoulos 		VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
4338*86714001SSerapheim Dimitropoulos 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
4339*86714001SSerapheim Dimitropoulos 		    sizeof (uint64_t), 1, &checkpoint_sm_obj));
4340*86714001SSerapheim Dimitropoulos 
4341*86714001SSerapheim Dimitropoulos 		VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
4342*86714001SSerapheim Dimitropoulos 		    checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
4343*86714001SSerapheim Dimitropoulos 		space_map_update(checkpoint_sm);
4344*86714001SSerapheim Dimitropoulos 		dump_spacemap(spa->spa_meta_objset, checkpoint_sm);
4345*86714001SSerapheim Dimitropoulos 		space_map_close(checkpoint_sm);
4346*86714001SSerapheim Dimitropoulos 	}
4347*86714001SSerapheim Dimitropoulos }
4348*86714001SSerapheim Dimitropoulos 
4349*86714001SSerapheim Dimitropoulos static int
4350*86714001SSerapheim Dimitropoulos verify_checkpoint(spa_t *spa)
4351*86714001SSerapheim Dimitropoulos {
4352*86714001SSerapheim Dimitropoulos 	uberblock_t checkpoint;
4353*86714001SSerapheim Dimitropoulos 	int error;
4354*86714001SSerapheim Dimitropoulos 
4355*86714001SSerapheim Dimitropoulos 	if (!spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT))
4356*86714001SSerapheim Dimitropoulos 		return (0);
4357*86714001SSerapheim Dimitropoulos 
4358*86714001SSerapheim Dimitropoulos 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
4359*86714001SSerapheim Dimitropoulos 	    DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t),
4360*86714001SSerapheim Dimitropoulos 	    sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint);
4361*86714001SSerapheim Dimitropoulos 
4362*86714001SSerapheim Dimitropoulos 	if (error == ENOENT) {
4363*86714001SSerapheim Dimitropoulos 		/*
4364*86714001SSerapheim Dimitropoulos 		 * If the feature is active but the uberblock is missing
4365*86714001SSerapheim Dimitropoulos 		 * then we must be in the middle of discarding the
4366*86714001SSerapheim Dimitropoulos 		 * checkpoint.
4367*86714001SSerapheim Dimitropoulos 		 */
4368*86714001SSerapheim Dimitropoulos 		(void) printf("\nPartially discarded checkpoint "
4369*86714001SSerapheim Dimitropoulos 		    "state found:\n");
4370*86714001SSerapheim Dimitropoulos 		dump_leftover_checkpoint_blocks(spa);
4371*86714001SSerapheim Dimitropoulos 		return (0);
4372*86714001SSerapheim Dimitropoulos 	} else if (error != 0) {
4373*86714001SSerapheim Dimitropoulos 		(void) printf("lookup error %d when looking for "
4374*86714001SSerapheim Dimitropoulos 		    "checkpointed uberblock in MOS\n", error);
4375*86714001SSerapheim Dimitropoulos 		return (error);
4376*86714001SSerapheim Dimitropoulos 	}
4377*86714001SSerapheim Dimitropoulos 	dump_uberblock(&checkpoint, "\nCheckpointed uberblock found:\n", "\n");
4378*86714001SSerapheim Dimitropoulos 
4379*86714001SSerapheim Dimitropoulos 	if (checkpoint.ub_checkpoint_txg == 0) {
4380*86714001SSerapheim Dimitropoulos 		(void) printf("\nub_checkpoint_txg not set in checkpointed "
4381*86714001SSerapheim Dimitropoulos 		    "uberblock\n");
4382*86714001SSerapheim Dimitropoulos 		error = 3;
4383*86714001SSerapheim Dimitropoulos 	}
4384*86714001SSerapheim Dimitropoulos 
4385*86714001SSerapheim Dimitropoulos 	if (error == 0)
4386*86714001SSerapheim Dimitropoulos 		verify_checkpoint_blocks(spa);
4387*86714001SSerapheim Dimitropoulos 
4388*86714001SSerapheim Dimitropoulos 	return (error);
4389*86714001SSerapheim Dimitropoulos }
4390*86714001SSerapheim Dimitropoulos 
4391fa9e4066Sahrens static void
4392fa9e4066Sahrens dump_zpool(spa_t *spa)
4393fa9e4066Sahrens {
4394fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
4395fa9e4066Sahrens 	int rc = 0;
4396fa9e4066Sahrens 
4397b24ab676SJeff Bonwick 	if (dump_opt['S']) {
4398b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
4399b24ab676SJeff Bonwick 		return;
4400b24ab676SJeff Bonwick 	}
4401b24ab676SJeff Bonwick 
440207428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
440307428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
440407428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
440507428bdfSVictor Latushkin 	}
440607428bdfSVictor Latushkin 
440707428bdfSVictor Latushkin 	if (dump_opt['C'])
440807428bdfSVictor Latushkin 		dump_config(spa);
440907428bdfSVictor Latushkin 
4410fa9e4066Sahrens 	if (dump_opt['u'])
441153b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
4412fa9e4066Sahrens 
4413b24ab676SJeff Bonwick 	if (dump_opt['D'])
4414b24ab676SJeff Bonwick 		dump_all_ddts(spa);
4415b24ab676SJeff Bonwick 
441687219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
441787219db7SVictor Latushkin 		dump_metaslabs(spa);
44182e4c9986SGeorge Wilson 	if (dump_opt['M'])
44192e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
442087219db7SVictor Latushkin 
442187219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
4422fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
4423fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
44245cabbc6bSPrashanth Sreenivasa 			dsl_pool_t *dp = spa->spa_dsl_pool;
4425732885fcSMatthew Ahrens 			dump_full_bpobj(&spa->spa_deferred_bpobj,
4426d0475637SMatthew Ahrens 			    "Deferred frees", 0);
4427cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
44285cabbc6bSPrashanth Sreenivasa 				dump_full_bpobj(&dp->dp_free_bpobj,
4429d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
4430ad135b5dSChristopher Siden 			}
44315cabbc6bSPrashanth Sreenivasa 			if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
44325cabbc6bSPrashanth Sreenivasa 				ASSERT(spa_feature_is_enabled(spa,
44335cabbc6bSPrashanth Sreenivasa 				    SPA_FEATURE_DEVICE_REMOVAL));
44345cabbc6bSPrashanth Sreenivasa 				dump_full_bpobj(&dp->dp_obsolete_bpobj,
44355cabbc6bSPrashanth Sreenivasa 				    "Pool obsolete blocks", 0);
44365cabbc6bSPrashanth Sreenivasa 			}
4437ad135b5dSChristopher Siden 
4438ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
44392acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
4440ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
44415cabbc6bSPrashanth Sreenivasa 				    dp->dp_bptree_obj,
4442ad135b5dSChristopher Siden 				    "Pool dataset frees");
4443cde58dbcSMatthew Ahrens 			}
4444fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
4445fa9e4066Sahrens 		}
444607428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
444707428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
4448b5152584SMatthew Ahrens 
4449ca0cc391SMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
4450ca0cc391SMatthew Ahrens 			uint64_t refcount;
4451ca0cc391SMatthew Ahrens 
4452ca0cc391SMatthew Ahrens 			if (!(spa_feature_table[f].fi_flags &
4453c5d1600cSMatthew Ahrens 			    ZFEATURE_FLAG_PER_DATASET) ||
4454c5d1600cSMatthew Ahrens 			    !spa_feature_is_enabled(spa, f)) {
4455ca0cc391SMatthew Ahrens 				ASSERT0(dataset_feature_count[f]);
4456ca0cc391SMatthew Ahrens 				continue;
4457ca0cc391SMatthew Ahrens 			}
4458ca0cc391SMatthew Ahrens 			(void) feature_get_refcount(spa,
4459ca0cc391SMatthew Ahrens 			    &spa_feature_table[f], &refcount);
4460ca0cc391SMatthew Ahrens 			if (dataset_feature_count[f] != refcount) {
4461ca0cc391SMatthew Ahrens 				(void) printf("%s feature refcount mismatch: "
4462ca0cc391SMatthew Ahrens 				    "%lld datasets != %lld refcount\n",
4463ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
4464ca0cc391SMatthew Ahrens 				    (longlong_t)dataset_feature_count[f],
4465ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
4466ca0cc391SMatthew Ahrens 				rc = 2;
4467ca0cc391SMatthew Ahrens 			} else {
4468ca0cc391SMatthew Ahrens 				(void) printf("Verified %s feature refcount "
4469ca0cc391SMatthew Ahrens 				    "of %llu is correct\n",
4470ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
4471ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
4472ca0cc391SMatthew Ahrens 			}
4473b5152584SMatthew Ahrens 		}
44745cabbc6bSPrashanth Sreenivasa 
44755cabbc6bSPrashanth Sreenivasa 		if (rc == 0) {
44765cabbc6bSPrashanth Sreenivasa 			rc = verify_device_removal_feature_counts(spa);
44775cabbc6bSPrashanth Sreenivasa 		}
4478fa9e4066Sahrens 	}
4479b5152584SMatthew Ahrens 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
4480fa9e4066Sahrens 		rc = dump_block_stats(spa);
4481fa9e4066Sahrens 
44820713e232SGeorge Wilson 	if (rc == 0)
44830713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
44840713e232SGeorge Wilson 
4485fa9e4066Sahrens 	if (dump_opt['s'])
4486fa9e4066Sahrens 		show_pool_stats(spa);
4487fa9e4066Sahrens 
44888f18d1faSGeorge Wilson 	if (dump_opt['h'])
44898f18d1faSGeorge Wilson 		dump_history(spa);
44908f18d1faSGeorge Wilson 
4491*86714001SSerapheim Dimitropoulos 	if (rc == 0 && !dump_opt['L'])
4492*86714001SSerapheim Dimitropoulos 		rc = verify_checkpoint(spa);
4493*86714001SSerapheim Dimitropoulos 
449429bdd2f9SPavel Zakharov 	if (rc != 0) {
449529bdd2f9SPavel Zakharov 		dump_debug_buffer();
4496fa9e4066Sahrens 		exit(rc);
449729bdd2f9SPavel Zakharov 	}
4498fa9e4066Sahrens }
4499fa9e4066Sahrens 
450044cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
450144cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
450244cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
450344cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
450444cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
450544cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
450644cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
450744cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
450844cd46caSbillm 
45093f7978d0SAlan Somers static int flagbits[256];
451044cd46caSbillm 
451144cd46caSbillm static void
451244cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
451344cd46caSbillm {
4514b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
451544cd46caSbillm 
451644cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
451744cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
4518b24ab676SJeff Bonwick 
451943466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
4520b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
452144cd46caSbillm }
452244cd46caSbillm 
452344cd46caSbillm static void
452444cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
452544cd46caSbillm {
452644cd46caSbillm 	int i;
452744cd46caSbillm 
452844cd46caSbillm 	for (i = 0; i < nbps; i++)
452944cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
453044cd46caSbillm }
453144cd46caSbillm 
453244cd46caSbillm static void
453344cd46caSbillm zdb_dump_gbh(void *buf, int flags)
453444cd46caSbillm {
453544cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
453644cd46caSbillm }
453744cd46caSbillm 
453844cd46caSbillm static void
453944cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
454044cd46caSbillm {
454144cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
454244cd46caSbillm 		byteswap_uint64_array(buf, size);
4543b24ab676SJeff Bonwick 	(void) write(1, buf, size);
454444cd46caSbillm }
454544cd46caSbillm 
454644cd46caSbillm static void
454744cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
454844cd46caSbillm {
454944cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
45503f7978d0SAlan Somers 	unsigned nwords = size / sizeof (uint64_t);
455144cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
45523f7978d0SAlan Somers 	unsigned i, j;
45533f7978d0SAlan Somers 	const char *hdr;
45543f7978d0SAlan Somers 	char *c;
455544cd46caSbillm 
455644cd46caSbillm 
455744cd46caSbillm 	if (do_bswap)
455844cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
455944cd46caSbillm 	else
456044cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
456144cd46caSbillm 
456244cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
456344cd46caSbillm 
456444cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
456544cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
456644cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
456744cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
456844cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
456944cd46caSbillm 
457044cd46caSbillm 		c = (char *)&d[i];
457144cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
457244cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
457344cd46caSbillm 		(void) printf("\n");
457444cd46caSbillm 	}
457544cd46caSbillm }
457644cd46caSbillm 
457744cd46caSbillm /*
457844cd46caSbillm  * There are two acceptable formats:
457944cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
458044cd46caSbillm  *	child[.child]*    - For example: 0.1.1
458144cd46caSbillm  *
458244cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
458344cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
458444cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
458544cd46caSbillm  */
458644cd46caSbillm static vdev_t *
45873f7978d0SAlan Somers zdb_vdev_lookup(vdev_t *vdev, const char *path)
458844cd46caSbillm {
458944cd46caSbillm 	char *s, *p, *q;
45903f7978d0SAlan Somers 	unsigned i;
459144cd46caSbillm 
459244cd46caSbillm 	if (vdev == NULL)
459344cd46caSbillm 		return (NULL);
459444cd46caSbillm 
459544cd46caSbillm 	/* First, assume the x.x.x.x format */
45963f7978d0SAlan Somers 	i = strtoul(path, &s, 10);
459744cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
459844cd46caSbillm 		goto name;
45993f7978d0SAlan Somers 	if (i >= vdev->vdev_children)
460044cd46caSbillm 		return (NULL);
460144cd46caSbillm 
460244cd46caSbillm 	vdev = vdev->vdev_child[i];
460344cd46caSbillm 	if (*s == '\0')
460444cd46caSbillm 		return (vdev);
460544cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
460644cd46caSbillm 
460744cd46caSbillm name:
460844cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
460944cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
461044cd46caSbillm 
461144cd46caSbillm 		if (vc->vdev_path == NULL) {
461244cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
461344cd46caSbillm 			if (vc == NULL)
461444cd46caSbillm 				continue;
461544cd46caSbillm 			else
461644cd46caSbillm 				return (vc);
461744cd46caSbillm 		}
461844cd46caSbillm 
461944cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
462044cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
462144cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
462244cd46caSbillm 
462344cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
462444cd46caSbillm 			return (vc);
462544cd46caSbillm 		if (strcmp(p, path) == 0)
462644cd46caSbillm 			return (vc);
462744cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
462844cd46caSbillm 			return (vc);
462944cd46caSbillm 	}
463044cd46caSbillm 
463144cd46caSbillm 	return (NULL);
463244cd46caSbillm }
463344cd46caSbillm 
4634770499e1SDan Kimmel /* ARGSUSED */
4635770499e1SDan Kimmel static int
4636770499e1SDan Kimmel random_get_pseudo_bytes_cb(void *buf, size_t len, void *unused)
4637770499e1SDan Kimmel {
4638770499e1SDan Kimmel 	return (random_get_pseudo_bytes(buf, len));
4639770499e1SDan Kimmel }
4640770499e1SDan Kimmel 
464144cd46caSbillm /*
464244cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
464344cd46caSbillm  * block descriptor is:
464444cd46caSbillm  *
464544cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
464644cd46caSbillm  *
464744cd46caSbillm  *	pool           - The name of the pool you wish to read from
464844cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
464944cd46caSbillm  *	offset         - offset, in hex, in bytes
465044cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
465144cd46caSbillm  *	flags          - A string of characters specifying options
465244cd46caSbillm  *		 b: Decode a blkptr at given offset within block
465344cd46caSbillm  *		*c: Calculate and display checksums
4654b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
465544cd46caSbillm  *		 e: Byteswap data before dumping
4656b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
4657b24ab676SJeff Bonwick  *		 i: Display as an indirect block
465844cd46caSbillm  *		 p: Do I/O to physical offset
465944cd46caSbillm  *		 r: Dump raw data to stdout
466044cd46caSbillm  *
466144cd46caSbillm  *              * = not yet implemented
466244cd46caSbillm  */
466344cd46caSbillm static void
466407428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
466544cd46caSbillm {
4666b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
4667b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
466844cd46caSbillm 	int flags = 0;
4669b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
467044cd46caSbillm 	zio_t *zio;
467144cd46caSbillm 	vdev_t *vd;
4672770499e1SDan Kimmel 	abd_t *pabd;
4673770499e1SDan Kimmel 	void *lbuf, *buf;
46743f7978d0SAlan Somers 	const char *s, *vdev;
46753f7978d0SAlan Somers 	char *p, *dup, *flagstr;
4676b24ab676SJeff Bonwick 	int i, error;
467744cd46caSbillm 
467844cd46caSbillm 	dup = strdup(thing);
467944cd46caSbillm 	s = strtok(dup, ":");
468044cd46caSbillm 	vdev = s ? s : "";
468144cd46caSbillm 	s = strtok(NULL, ":");
468244cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
468344cd46caSbillm 	s = strtok(NULL, ":");
468444cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
468544cd46caSbillm 	s = strtok(NULL, ":");
46863f7978d0SAlan Somers 	if (s)
46873f7978d0SAlan Somers 		flagstr = strdup(s);
46883f7978d0SAlan Somers 	else
46893f7978d0SAlan Somers 		flagstr = strdup("");
469044cd46caSbillm 
469144cd46caSbillm 	s = NULL;
469244cd46caSbillm 	if (size == 0)
469344cd46caSbillm 		s = "size must not be zero";
469444cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
469544cd46caSbillm 		s = "size must be a multiple of sector size";
469644cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
469744cd46caSbillm 		s = "offset must be a multiple of sector size";
469844cd46caSbillm 	if (s) {
469944cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
470044cd46caSbillm 		free(dup);
470144cd46caSbillm 		return;
470244cd46caSbillm 	}
470344cd46caSbillm 
470444cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
470544cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
47065ad82045Snd 			int bit = flagbits[(uchar_t)flagstr[i]];
470744cd46caSbillm 
470844cd46caSbillm 			if (bit == 0) {
470944cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
471044cd46caSbillm 				    flagstr[i]);
471144cd46caSbillm 				continue;
471244cd46caSbillm 			}
471344cd46caSbillm 			flags |= bit;
471444cd46caSbillm 
471544cd46caSbillm 			/* If it's not something with an argument, keep going */
4716b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
471744cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
471844cd46caSbillm 				continue;
471944cd46caSbillm 
472044cd46caSbillm 			p = &flagstr[i + 1];
472144cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
472244cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
472344cd46caSbillm 			if (*p != ':' && *p != '\0') {
472444cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
472544cd46caSbillm 				free(dup);
472644cd46caSbillm 				return;
472744cd46caSbillm 			}
472844cd46caSbillm 		}
472944cd46caSbillm 	}
47303f7978d0SAlan Somers 	free(flagstr);
473144cd46caSbillm 
473244cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
473344cd46caSbillm 	if (vd == NULL) {
473444cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
473544cd46caSbillm 		free(dup);
473644cd46caSbillm 		return;
473744cd46caSbillm 	} else {
473844cd46caSbillm 		if (vd->vdev_path)
4739b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
4740b24ab676SJeff Bonwick 			    vd->vdev_path);
474144cd46caSbillm 		else
4742b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
474344cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
474444cd46caSbillm 	}
474544cd46caSbillm 
4746b24ab676SJeff Bonwick 	psize = size;
4747b24ab676SJeff Bonwick 	lsize = size;
474844cd46caSbillm 
4749770499e1SDan Kimmel 	pabd = abd_alloc_linear(SPA_MAXBLOCKSIZE, B_FALSE);
4750b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
4751b24ab676SJeff Bonwick 
4752b24ab676SJeff Bonwick 	BP_ZERO(bp);
4753b24ab676SJeff Bonwick 
4754b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
4755b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
4756b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
4757b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
4758b24ab676SJeff Bonwick 
4759b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
4760b24ab676SJeff Bonwick 
4761b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
4762b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
4763b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
4764b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
4765b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
4766b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
4767b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
4768b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
476944cd46caSbillm 
4770e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
477144cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
4772b24ab676SJeff Bonwick 
4773b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
4774b24ab676SJeff Bonwick 		/*
4775b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
4776b24ab676SJeff Bonwick 		 */
4777770499e1SDan Kimmel 		zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL,
4778b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
4779b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
4780b24ab676SJeff Bonwick 	} else {
4781b24ab676SJeff Bonwick 		/*
4782b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
4783b24ab676SJeff Bonwick 		 */
4784770499e1SDan Kimmel 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd,
4785770499e1SDan Kimmel 		    psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
4786b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
4787b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
47885cabbc6bSPrashanth Sreenivasa 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW | ZIO_FLAG_OPTIONAL,
47895cabbc6bSPrashanth Sreenivasa 		    NULL, NULL));
4790b24ab676SJeff Bonwick 	}
4791b24ab676SJeff Bonwick 
479244cd46caSbillm 	error = zio_wait(zio);
4793e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
479444cd46caSbillm 
479544cd46caSbillm 	if (error) {
479644cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
479744cd46caSbillm 		goto out;
479844cd46caSbillm 	}
479944cd46caSbillm 
4800b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
4801b24ab676SJeff Bonwick 		/*
4802b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
4803b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
4804b24ab676SJeff Bonwick 		 */
4805b24ab676SJeff Bonwick 		enum zio_compress c;
4806b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
4807b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
4808b24ab676SJeff Bonwick 
4809770499e1SDan Kimmel 		abd_copy_to_buf(pbuf2, pabd, psize);
4810b24ab676SJeff Bonwick 
4811770499e1SDan Kimmel 		VERIFY0(abd_iterate_func(pabd, psize, SPA_MAXBLOCKSIZE - psize,
4812770499e1SDan Kimmel 		    random_get_pseudo_bytes_cb, NULL));
4813b24ab676SJeff Bonwick 
4814770499e1SDan Kimmel 		VERIFY0(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
4815770499e1SDan Kimmel 		    SPA_MAXBLOCKSIZE - psize));
4816b24ab676SJeff Bonwick 
4817b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
4818b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
4819b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
4820770499e1SDan Kimmel 				if (zio_decompress_data(c, pabd,
4821770499e1SDan Kimmel 				    lbuf, psize, lsize) == 0 &&
4822770499e1SDan Kimmel 				    zio_decompress_data_buf(c, pbuf2,
4823770499e1SDan Kimmel 				    lbuf2, psize, lsize) == 0 &&
4824b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
4825b24ab676SJeff Bonwick 					break;
4826b24ab676SJeff Bonwick 			}
4827b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
4828b24ab676SJeff Bonwick 				break;
4829b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
4830b24ab676SJeff Bonwick 		}
4831b24ab676SJeff Bonwick 
4832b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
4833b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
4834b24ab676SJeff Bonwick 
4835b24ab676SJeff Bonwick 		if (lsize <= psize) {
4836b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
4837b24ab676SJeff Bonwick 			goto out;
4838b24ab676SJeff Bonwick 		}
4839b24ab676SJeff Bonwick 		buf = lbuf;
4840b24ab676SJeff Bonwick 		size = lsize;
4841b24ab676SJeff Bonwick 	} else {
4842770499e1SDan Kimmel 		buf = abd_to_buf(pabd);
4843b24ab676SJeff Bonwick 		size = psize;
4844b24ab676SJeff Bonwick 	}
4845b24ab676SJeff Bonwick 
484644cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
484744cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
484844cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
484944cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
485044cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
485144cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
485244cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
485344cd46caSbillm 		    flags);
485444cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
485544cd46caSbillm 		zdb_dump_gbh(buf, flags);
485644cd46caSbillm 	else
485744cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
485844cd46caSbillm 
485944cd46caSbillm out:
4860770499e1SDan Kimmel 	abd_free(pabd);
4861b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
486244cd46caSbillm 	free(dup);
486344cd46caSbillm }
486444cd46caSbillm 
48654923c69fSMatthew Ahrens static void
48664923c69fSMatthew Ahrens zdb_embedded_block(char *thing)
48674923c69fSMatthew Ahrens {
48683f7978d0SAlan Somers 	blkptr_t bp;
48694923c69fSMatthew Ahrens 	unsigned long long *words = (void *)&bp;
48704923c69fSMatthew Ahrens 	char buf[SPA_MAXBLOCKSIZE];
48714923c69fSMatthew Ahrens 	int err;
48724923c69fSMatthew Ahrens 
48733f7978d0SAlan Somers 	bzero(&bp, sizeof (bp));
48744923c69fSMatthew Ahrens 	err = sscanf(thing, "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx:"
48754923c69fSMatthew Ahrens 	    "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx",
48764923c69fSMatthew Ahrens 	    words + 0, words + 1, words + 2, words + 3,
48774923c69fSMatthew Ahrens 	    words + 4, words + 5, words + 6, words + 7,
48784923c69fSMatthew Ahrens 	    words + 8, words + 9, words + 10, words + 11,
48794923c69fSMatthew Ahrens 	    words + 12, words + 13, words + 14, words + 15);
48804923c69fSMatthew Ahrens 	if (err != 16) {
48814923c69fSMatthew Ahrens 		(void) printf("invalid input format\n");
48824923c69fSMatthew Ahrens 		exit(1);
48834923c69fSMatthew Ahrens 	}
48844923c69fSMatthew Ahrens 	ASSERT3U(BPE_GET_LSIZE(&bp), <=, SPA_MAXBLOCKSIZE);
48854923c69fSMatthew Ahrens 	err = decode_embedded_bp(&bp, buf, BPE_GET_LSIZE(&bp));
48864923c69fSMatthew Ahrens 	if (err != 0) {
48874923c69fSMatthew Ahrens 		(void) printf("decode failed: %u\n", err);
48884923c69fSMatthew Ahrens 		exit(1);
48894923c69fSMatthew Ahrens 	}
48904923c69fSMatthew Ahrens 	zdb_dump_block_raw(buf, BPE_GET_LSIZE(&bp), 0);
48914923c69fSMatthew Ahrens }
48924923c69fSMatthew Ahrens 
4893de6628f0Sck static boolean_t
48943ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
4895de6628f0Sck {
48963ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
4897de6628f0Sck 	char *s;
4898de6628f0Sck 
4899de6628f0Sck 	if (guid != 0) {
49003ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
49013ad6c7f9SVictor Latushkin 			return (v == guid);
4902de6628f0Sck 	} else {
49033ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
49043ad6c7f9SVictor Latushkin 			return (strcmp(s, tgt) == 0);
4905de6628f0Sck 	}
49063ad6c7f9SVictor Latushkin 	return (B_FALSE);
4907de6628f0Sck }
4908de6628f0Sck 
49093ad6c7f9SVictor Latushkin static char *
49103ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
4911de6628f0Sck {
4912de6628f0Sck 	nvlist_t *pools;
4913de6628f0Sck 	nvlist_t *match = NULL;
49143ad6c7f9SVictor Latushkin 	char *name = NULL;
49153ad6c7f9SVictor Latushkin 	char *sepp = NULL;
49169adfa60dSMatthew Ahrens 	char sep = '\0';
49173ad6c7f9SVictor Latushkin 	int count = 0;
49183f7978d0SAlan Somers 	importargs_t args;
4919d41c4376SMark J Musante 
49203f7978d0SAlan Somers 	bzero(&args, sizeof (args));
4921d41c4376SMark J Musante 	args.paths = dirc;
4922d41c4376SMark J Musante 	args.path = dirv;
4923d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
4924de6628f0Sck 
49253ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
49263ad6c7f9SVictor Latushkin 		sep = *sepp;
49273ad6c7f9SVictor Latushkin 		*sepp = '\0';
49283ad6c7f9SVictor Latushkin 	}
49293ad6c7f9SVictor Latushkin 
4930d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
4931de6628f0Sck 
4932de6628f0Sck 	if (pools != NULL) {
4933de6628f0Sck 		nvpair_t *elem = NULL;
4934de6628f0Sck 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
4935de6628f0Sck 			verify(nvpair_value_nvlist(elem, configp) == 0);
49363ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
49373ad6c7f9SVictor Latushkin 				count++;
4938de6628f0Sck 				if (match != NULL) {
49393ad6c7f9SVictor Latushkin 					/* print previously found config */
49403ad6c7f9SVictor Latushkin 					if (name != NULL) {
49413ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
49423ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
49433ad6c7f9SVictor Latushkin 						name = NULL;
49443ad6c7f9SVictor Latushkin 					}
49453ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
49463ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
49473ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
4948de6628f0Sck 				} else {
4949de6628f0Sck 					match = *configp;
49503ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
4951de6628f0Sck 				}
4952de6628f0Sck 			}
4953de6628f0Sck 		}
4954de6628f0Sck 	}
49553ad6c7f9SVictor Latushkin 	if (count > 1)
49563ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
49573ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
49583ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
49593ad6c7f9SVictor Latushkin 
49603ad6c7f9SVictor Latushkin 	if (sepp)
49613ad6c7f9SVictor Latushkin 		*sepp = sep;
49623ad6c7f9SVictor Latushkin 	/*
49633ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
49643ad6c7f9SVictor Latushkin 	 */
49653ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
49663ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
49673ad6c7f9SVictor Latushkin 
49683ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
49693ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
49703ad6c7f9SVictor Latushkin 	}
4971de6628f0Sck 
49723ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
4973de6628f0Sck 
49743ad6c7f9SVictor Latushkin 	return (name);
4975de6628f0Sck }
4976de6628f0Sck 
4977fa9e4066Sahrens int
4978fa9e4066Sahrens main(int argc, char **argv)
4979fa9e4066Sahrens {
49803f7978d0SAlan Somers 	int c;
4981fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
49823ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
4983fa9e4066Sahrens 	objset_t *os = NULL;
4984fa9e4066Sahrens 	int dump_all = 1;
4985fa9e4066Sahrens 	int verbose = 0;
4986c8ee1847SVictor Latushkin 	int error = 0;
49873ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
49883ad6c7f9SVictor Latushkin 	int nsearch = 0;
49893ad6c7f9SVictor Latushkin 	char *target;
4990468c413aSTim Haley 	nvlist_t *policy = NULL;
4991468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
4992dfd5965fSRichard Yao 	int flags = ZFS_IMPORT_MISSING_LOG;
4993c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
4994ae24175bSCyril Plisko 	char *spa_config_path_env;
4995b702644aSTim Chase 	boolean_t target_is_spa = B_TRUE;
4996*86714001SSerapheim Dimitropoulos 	nvlist_t *cfg = NULL;
4997fa9e4066Sahrens 
4998fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
4999004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
5000fa9e4066Sahrens 
5001fa9e4066Sahrens 	dprintf_setup(&argc, argv);
5002fa9e4066Sahrens 
5003ae24175bSCyril Plisko 	/*
5004ae24175bSCyril Plisko 	 * If there is an environment variable SPA_CONFIG_PATH it overrides
5005ae24175bSCyril Plisko 	 * default spa_config_path setting. If -U flag is specified it will
5006ae24175bSCyril Plisko 	 * override this environment variable settings once again.
5007ae24175bSCyril Plisko 	 */
5008ae24175bSCyril Plisko 	spa_config_path_env = getenv("SPA_CONFIG_PATH");
5009ae24175bSCyril Plisko 	if (spa_config_path_env != NULL)
5010ae24175bSCyril Plisko 		spa_config_path = spa_config_path_env;
5011ae24175bSCyril Plisko 
5012df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
5013*86714001SSerapheim Dimitropoulos 	    "AbcCdDeEFGhiI:klLmMo:Op:PqRsSt:uU:vVx:X")) != -1) {
5014fa9e4066Sahrens 		switch (c) {
5015fa9e4066Sahrens 		case 'b':
5016fa9e4066Sahrens 		case 'c':
5017ed61ec1dSYuri Pankov 		case 'C':
5018b24ab676SJeff Bonwick 		case 'd':
5019ed61ec1dSYuri Pankov 		case 'D':
50204923c69fSMatthew Ahrens 		case 'E':
5021ed61ec1dSYuri Pankov 		case 'G':
5022b24ab676SJeff Bonwick 		case 'h':
5023b24ab676SJeff Bonwick 		case 'i':
5024b24ab676SJeff Bonwick 		case 'l':
5025d6e555bdSGeorge Wilson 		case 'm':
50262e4c9986SGeorge Wilson 		case 'M':
5027ed61ec1dSYuri Pankov 		case 'O':
502844cd46caSbillm 		case 'R':
5029ed61ec1dSYuri Pankov 		case 's':
5030b24ab676SJeff Bonwick 		case 'S':
5031ed61ec1dSYuri Pankov 		case 'u':
5032fa9e4066Sahrens 			dump_opt[c]++;
5033fa9e4066Sahrens 			dump_all = 0;
5034fa9e4066Sahrens 			break;
5035feef89cfSVictor Latushkin 		case 'A':
5036ed61ec1dSYuri Pankov 		case 'e':
5037c8ee1847SVictor Latushkin 		case 'F':
5038*86714001SSerapheim Dimitropoulos 		case 'k':
503982a0a985SVictor Latushkin 		case 'L':
50403f9d6ad7SLin Ling 		case 'P':
504164723e36SYuri Pankov 		case 'q':
5042ed61ec1dSYuri Pankov 		case 'X':
504382a0a985SVictor Latushkin 			dump_opt[c]++;
504482a0a985SVictor Latushkin 			break;
5045ed61ec1dSYuri Pankov 		/* NB: Sort single match options below. */
50462e4c9986SGeorge Wilson 		case 'I':
504731d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
504831d7e8faSGeorge Wilson 			if (max_inflight == 0) {
504931d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
505031d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
505131d7e8faSGeorge Wilson 				    "than 0\n");
505231d7e8faSGeorge Wilson 				usage();
505331d7e8faSGeorge Wilson 			}
505431d7e8faSGeorge Wilson 			break;
5055ed61ec1dSYuri Pankov 		case 'o':
5056ed61ec1dSYuri Pankov 			error = set_global_var(optarg);
5057ed61ec1dSYuri Pankov 			if (error != 0)
5058ed61ec1dSYuri Pankov 				usage();
5059ed61ec1dSYuri Pankov 			break;
5060de6628f0Sck 		case 'p':
50613ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
50623ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
50633ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
50643ad6c7f9SVictor Latushkin 			} else {
50653ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
50663ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
50673ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
50683ad6c7f9SVictor Latushkin 				    sizeof (char *));
50693ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
50703ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
50713ad6c7f9SVictor Latushkin 				searchdirs = tmp;
50723ad6c7f9SVictor Latushkin 			}
50733ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
5074de6628f0Sck 			break;
50752e551927SVictor Latushkin 		case 't':
5076468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
5077468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
50782e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
50792e551927SVictor Latushkin 				    "specified: %s\n", optarg);
50802e551927SVictor Latushkin 				usage();
50812e551927SVictor Latushkin 			}
50822e551927SVictor Latushkin 			break;
5083b24ab676SJeff Bonwick 		case 'U':
5084b24ab676SJeff Bonwick 			spa_config_path = optarg;
50854923c69fSMatthew Ahrens 			if (spa_config_path[0] != '/') {
50864923c69fSMatthew Ahrens 				(void) fprintf(stderr,
50874923c69fSMatthew Ahrens 				    "cachefile must be an absolute path "
50884923c69fSMatthew Ahrens 				    "(i.e. start with a slash)\n");
50894923c69fSMatthew Ahrens 				usage();
50904923c69fSMatthew Ahrens 			}
5091b24ab676SJeff Bonwick 			break;
50922e4c9986SGeorge Wilson 		case 'v':
50932e4c9986SGeorge Wilson 			verbose++;
50942e4c9986SGeorge Wilson 			break;
5095dfd5965fSRichard Yao 		case 'V':
5096dfd5965fSRichard Yao 			flags = ZFS_IMPORT_VERBATIM;
5097dfd5965fSRichard Yao 			break;
50982e4c9986SGeorge Wilson 		case 'x':
50992e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
51002e4c9986SGeorge Wilson 			break;
5101fa9e4066Sahrens 		default:
5102fa9e4066Sahrens 			usage();
5103fa9e4066Sahrens 			break;
5104fa9e4066Sahrens 		}
5105fa9e4066Sahrens 	}
5106fa9e4066Sahrens 
51073ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
510888b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
510988b7b0f2SMatthew Ahrens 		usage();
511088b7b0f2SMatthew Ahrens 	}
5111de6628f0Sck 
511206be9802SMatthew Ahrens 	/*
511306be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
511406be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
511506be9802SMatthew Ahrens 	 */
511606be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
511706be9802SMatthew Ahrens 
5118f7950bf1SMatthew Ahrens 	/*
5119f7950bf1SMatthew Ahrens 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
5120f7950bf1SMatthew Ahrens 	 * "zdb -b" uses traversal prefetch which uses async reads.
5121f7950bf1SMatthew Ahrens 	 * For good performance, let several of them be active at once.
5122f7950bf1SMatthew Ahrens 	 */
5123f7950bf1SMatthew Ahrens 	zfs_vdev_async_read_max_active = 10;
5124f7950bf1SMatthew Ahrens 
51254dd77f9eSMatthew Ahrens 	/*
51264dd77f9eSMatthew Ahrens 	 * Disable reference tracking for better performance.
51274dd77f9eSMatthew Ahrens 	 */
51284dd77f9eSMatthew Ahrens 	reference_tracking_enable = B_FALSE;
51294dd77f9eSMatthew Ahrens 
5130e144c4e6SPavel Zakharov 	/*
5131e144c4e6SPavel Zakharov 	 * Do not fail spa_load when spa_load_verify fails. This is needed
5132e144c4e6SPavel Zakharov 	 * to load non-idle pools.
5133e144c4e6SPavel Zakharov 	 */
5134e144c4e6SPavel Zakharov 	spa_load_verify_dryrun = B_TRUE;
5135e144c4e6SPavel Zakharov 
5136fa9e4066Sahrens 	kernel_init(FREAD);
5137de6628f0Sck 	g_zfs = libzfs_init();
513891ebeef5Sahrens 	ASSERT(g_zfs != NULL);
5139fa9e4066Sahrens 
5140b24ab676SJeff Bonwick 	if (dump_all)
5141b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
5142b24ab676SJeff Bonwick 
5143fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
5144*86714001SSerapheim Dimitropoulos 		if (dump_all && strchr("AeEFklLOPRSX", c) == NULL)
5145fa9e4066Sahrens 			dump_opt[c] = 1;
5146fa9e4066Sahrens 		if (dump_opt[c])
5147fa9e4066Sahrens 			dump_opt[c] += verbose;
5148fa9e4066Sahrens 	}
5149fa9e4066Sahrens 
5150feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
5151feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
5152feef89cfSVictor Latushkin 
5153fa9e4066Sahrens 	argc -= optind;
5154fa9e4066Sahrens 	argv += optind;
5155fa9e4066Sahrens 
515607428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
515707428bdfSVictor Latushkin 		usage();
51584923c69fSMatthew Ahrens 
51594923c69fSMatthew Ahrens 	if (dump_opt['E']) {
51604923c69fSMatthew Ahrens 		if (argc != 1)
51614923c69fSMatthew Ahrens 			usage();
51624923c69fSMatthew Ahrens 		zdb_embedded_block(argv[0]);
51634923c69fSMatthew Ahrens 		return (0);
51644923c69fSMatthew Ahrens 	}
51654923c69fSMatthew Ahrens 
5166fa9e4066Sahrens 	if (argc < 1) {
51673ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
5168e829d913Sck 			dump_cachefile(spa_config_path);
5169fa9e4066Sahrens 			return (0);
5170fa9e4066Sahrens 		}
5171fa9e4066Sahrens 		usage();
5172fa9e4066Sahrens 	}
5173fa9e4066Sahrens 
517464723e36SYuri Pankov 	if (dump_opt['l'])
517564723e36SYuri Pankov 		return (dump_label(argv[0]));
5176fa9e4066Sahrens 
5177ed61ec1dSYuri Pankov 	if (dump_opt['O']) {
5178ed61ec1dSYuri Pankov 		if (argc != 2)
5179ed61ec1dSYuri Pankov 			usage();
5180ed61ec1dSYuri Pankov 		dump_opt['v'] = verbose + 3;
5181ed61ec1dSYuri Pankov 		return (dump_path(argv[0], argv[1]));
5182ed61ec1dSYuri Pankov 	}
5183ed61ec1dSYuri Pankov 
5184c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
5185c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
5186c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
5187c8ee1847SVictor Latushkin 
5188c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
5189c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
5190c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
5191c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
5192c8ee1847SVictor Latushkin 
5193c5904d13Seschrock 	error = 0;
51943ad6c7f9SVictor Latushkin 	target = argv[0];
5195990b4856Slling 
51963ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
51973ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
5198990b4856Slling 
51993ad6c7f9SVictor Latushkin 		error = ENOENT;
52003ad6c7f9SVictor Latushkin 		if (name) {
520107428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
520207428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
520307428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
520407428bdfSVictor Latushkin 			}
5205*86714001SSerapheim Dimitropoulos 
5206c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
5207468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
5208468c413aSTim Haley 				fatal("can't open '%s': %s",
5209468c413aSTim Haley 				    target, strerror(ENOMEM));
5210468c413aSTim Haley 			}
5211dfd5965fSRichard Yao 			error = spa_import(name, cfg, NULL, flags);
5212de6628f0Sck 		}
5213c5904d13Seschrock 	}
5214c5904d13Seschrock 
5215*86714001SSerapheim Dimitropoulos 	char *checkpoint_pool = NULL;
5216*86714001SSerapheim Dimitropoulos 	char *checkpoint_target = NULL;
5217*86714001SSerapheim Dimitropoulos 	if (dump_opt['k']) {
5218*86714001SSerapheim Dimitropoulos 		checkpoint_pool = import_checkpointed_state(target, cfg,
5219*86714001SSerapheim Dimitropoulos 		    &checkpoint_target);
5220*86714001SSerapheim Dimitropoulos 
5221*86714001SSerapheim Dimitropoulos 		if (checkpoint_target != NULL)
5222*86714001SSerapheim Dimitropoulos 			target = checkpoint_target;
5223*86714001SSerapheim Dimitropoulos 
5224*86714001SSerapheim Dimitropoulos 	}
5225*86714001SSerapheim Dimitropoulos 
5226b702644aSTim Chase 	if (strpbrk(target, "/@") != NULL) {
5227b702644aSTim Chase 		size_t targetlen;
5228b702644aSTim Chase 
5229b702644aSTim Chase 		target_is_spa = B_FALSE;
5230b702644aSTim Chase 		/*
5231b702644aSTim Chase 		 * Remove any trailing slash.  Later code would get confused
5232b702644aSTim Chase 		 * by it, but we want to allow it so that "pool/" can
5233b702644aSTim Chase 		 * indicate that we want to dump the topmost filesystem,
5234b702644aSTim Chase 		 * rather than the whole pool.
5235b702644aSTim Chase 		 */
5236b702644aSTim Chase 		targetlen = strlen(target);
5237b702644aSTim Chase 		if (targetlen != 0 && target[targetlen - 1] == '/')
5238b702644aSTim Chase 			target[targetlen - 1] = '\0';
5239b702644aSTim Chase 	}
5240b702644aSTim Chase 
5241c5904d13Seschrock 	if (error == 0) {
5242*86714001SSerapheim Dimitropoulos 		if (dump_opt['k'] && (target_is_spa || dump_opt['R'])) {
5243*86714001SSerapheim Dimitropoulos 			ASSERT(checkpoint_pool != NULL);
5244*86714001SSerapheim Dimitropoulos 			ASSERT(checkpoint_target == NULL);
5245*86714001SSerapheim Dimitropoulos 
5246*86714001SSerapheim Dimitropoulos 			error = spa_open(checkpoint_pool, &spa, FTAG);
5247*86714001SSerapheim Dimitropoulos 			if (error != 0) {
5248*86714001SSerapheim Dimitropoulos 				fatal("Tried to open pool \"%s\" but "
5249*86714001SSerapheim Dimitropoulos 				    "spa_open() failed with error %d\n",
5250*86714001SSerapheim Dimitropoulos 				    checkpoint_pool, error);
5251*86714001SSerapheim Dimitropoulos 			}
5252*86714001SSerapheim Dimitropoulos 
5253*86714001SSerapheim Dimitropoulos 		} else if (target_is_spa || dump_opt['R']) {
525480eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
525580eb36f2SGeorge Wilson 			    NULL);
52568f18d1faSGeorge Wilson 			if (error) {
52578f18d1faSGeorge Wilson 				/*
52588f18d1faSGeorge Wilson 				 * If we're missing the log device then
52598f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
52608f18d1faSGeorge Wilson 				 * log state.
52618f18d1faSGeorge Wilson 				 */
52628f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
52633ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
52648f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
52658f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
52668f18d1faSGeorge Wilson 					error = 0;
52678f18d1faSGeorge Wilson 				}
52688f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
52698f18d1faSGeorge Wilson 
527080eb36f2SGeorge Wilson 				if (!error) {
527180eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
527280eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
527380eb36f2SGeorge Wilson 				}
52748f18d1faSGeorge Wilson 			}
527507428bdfSVictor Latushkin 		} else {
5276ed61ec1dSYuri Pankov 			error = open_objset(target, DMU_OST_ANY, FTAG, &os);
5277c5904d13Seschrock 		}
5278fa9e4066Sahrens 	}
527980eb36f2SGeorge Wilson 	nvlist_free(policy);
528080eb36f2SGeorge Wilson 
5281fa9e4066Sahrens 	if (error)
52823ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
5283fa9e4066Sahrens 
5284fa9e4066Sahrens 	argv++;
528507428bdfSVictor Latushkin 	argc--;
528607428bdfSVictor Latushkin 	if (!dump_opt['R']) {
528707428bdfSVictor Latushkin 		if (argc > 0) {
528807428bdfSVictor Latushkin 			zopt_objects = argc;
528907428bdfSVictor Latushkin 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
52903f7978d0SAlan Somers 			for (unsigned i = 0; i < zopt_objects; i++) {
529107428bdfSVictor Latushkin 				errno = 0;
529207428bdfSVictor Latushkin 				zopt_object[i] = strtoull(argv[i], NULL, 0);
529307428bdfSVictor Latushkin 				if (zopt_object[i] == 0 && errno != 0)
529487219db7SVictor Latushkin 					fatal("bad number %s: %s",
529507428bdfSVictor Latushkin 					    argv[i], strerror(errno));
529607428bdfSVictor Latushkin 			}
5297fa9e4066Sahrens 		}
5298e690fb27SChristopher Siden 		if (os != NULL) {
5299e690fb27SChristopher Siden 			dump_dir(os);
5300e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
5301e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
5302e690fb27SChristopher Siden 		} else {
5303e690fb27SChristopher Siden 			dump_zpool(spa);
5304e690fb27SChristopher Siden 		}
5305fa9e4066Sahrens 	} else {
530607428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
530707428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
530807428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
530907428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
531007428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
531107428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
531207428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
531307428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
531407428bdfSVictor Latushkin 
53153f7978d0SAlan Somers 		for (int i = 0; i < argc; i++)
531607428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
5317fa9e4066Sahrens 	}
5318fa9e4066Sahrens 
5319*86714001SSerapheim Dimitropoulos 	if (dump_opt['k']) {
5320*86714001SSerapheim Dimitropoulos 		free(checkpoint_pool);
5321*86714001SSerapheim Dimitropoulos 		if (!target_is_spa)
5322*86714001SSerapheim Dimitropoulos 			free(checkpoint_target);
5323*86714001SSerapheim Dimitropoulos 	}
5324*86714001SSerapheim Dimitropoulos 
5325ed61ec1dSYuri Pankov 	if (os != NULL)
5326ed61ec1dSYuri Pankov 		close_objset(os, FTAG);
5327ed61ec1dSYuri Pankov 	else
5328ed61ec1dSYuri Pankov 		spa_close(spa, FTAG);
532907428bdfSVictor Latushkin 
5330e0d35c44Smarks 	fuid_table_destroy();
5331e0d35c44Smarks 
533229bdd2f9SPavel Zakharov 	dump_debug_buffer();
533329bdd2f9SPavel Zakharov 
5334de6628f0Sck 	libzfs_fini(g_zfs);
5335fa9e4066Sahrens 	kernel_fini();
5336fa9e4066Sahrens 
5337fa9e4066Sahrens 	return (0);
5338fa9e4066Sahrens }
5339